From a9feab3b1f5335b14fc0feba4da2b9fe40aa1224 Mon Sep 17 00:00:00 2001 From: cel Date: Wed, 20 Sep 2017 07:50:23 -1000 Subject: Render npm package publishers --- lib/app.js | 4 ++- lib/render-msg.js | 15 +++++++---- lib/render.js | 78 ++++++++++++++++++++++++++++++++++--------------------- lib/serve.js | 37 ++++++++++++++++++++------ lib/util.js | 11 ++++++++ 5 files changed, 101 insertions(+), 44 deletions(-) (limited to 'lib') diff --git a/lib/app.js b/lib/app.js index 3255851..65d404a 100644 --- a/lib/app.js +++ b/lib/app.js @@ -549,9 +549,11 @@ App.prototype.streamPrivate = function (opts) { App.prototype.blobMentions = function (opts) { if (!this.sbot.links2) return pull.error(new Error( 'missing ssb-links plugin')) + var filter = {rel: ['mentions', opts.name]} + if (opts.author) filter.source = opts.author return this.sbot.links2.read({ query: [ - {$filter: {rel: ['mentions', opts.name]}}, + {$filter: filter}, {$filter: {dest: {$prefix: '&'}}}, {$map: { name: ['rel', 1], diff --git a/lib/render-msg.js b/lib/render-msg.js index 2e58610..1cb0936 100644 --- a/lib/render-msg.js +++ b/lib/render-msg.js @@ -536,7 +536,11 @@ RenderMsg.prototype.gitUpdate = function (cb) { var size = [].concat(self.c.packs, self.c.indexes) .map(function (o) { return o && o.size }) .reduce(function (total, s) { return total + s }) - self.link(self.c.repo, function (err, a) { + + var done = multicb({pluck: 1, spread: true}) + self.link(self.c.repo, done()) + self.render.npmPackageMentions(self.c.mentions, done()) + done(function (err, a, pkgMentionsEl) { if (err) return cb(err) self.wrap(h('div.ssb-git-update', 'git push ', a, ' ', @@ -579,7 +583,7 @@ RenderMsg.prototype.gitUpdate = function (cb) { '+ ' + self.c.commits_more + ' more commits') : '', self.c.tags_more ? h('div', '+ ' + self.c.tags_more + ' more tags') : '', - self.render.npmPackageMentions(self.c.mentions) + pkgMentionsEl ), cb) }) } @@ -949,9 +953,10 @@ RenderMsg.prototype.npmPublish = function (cb) { RenderMsg.prototype.npmPackages = function (cb) { var self = this - self.wrap([ - self.render.npmPackageMentions(self.c.mentions) - ], cb) + self.render.npmPackageMentions(self.c.mentions, function (err, el) { + if (err) return cb(err) + self.wrap(el, cb) + }) } RenderMsg.prototype.npmPublishTitle = function (cb) { diff --git a/lib/render.js b/lib/render.js index 9bba5bd..6001184 100644 --- a/lib/render.js +++ b/lib/render.js @@ -390,13 +390,18 @@ Render.prototype.highlight = function (code, lang) { } } -Render.prototype.npmPackageMentions = function (links) { +Render.prototype.npmPackageMentions = function (links, cb) { var self = this var pkgLinks = u.toArray(links).filter(function (link) { return /^npm:/.test(link.name) }) - return pkgLinks.length > 0 ? h('div', - h('table', + if (pkgLinks.length === 0) return cb(null, '') + var done = multicb({pluck: 1}) + pkgLinks.forEach(function (link) { + self.npmPackageMention(link, {}, done()) + }) + done(function (err, mentionEls) { + cb(null, h('table', h('thead', h('tr', h('td', 'package'), h('td', 'version'), @@ -404,37 +409,50 @@ Render.prototype.npmPackageMentions = function (links) { h('td', 'size'), h('td', 'tarball') )), - h('tbody', pkgLinks.map(function (link) { - return self.npmPackageMention(link) - })) - ) - ) : '' + h('tbody', mentionEls) + )) + }) } -Render.prototype.npmPackageMention = function (link) { +Render.prototype.npmPackageMention = function (link, opts, cb) { var parts = String(link.name).replace(/\.tgz$/, '').split(':') var name = parts[1] var version = parts[2] var distTag = parts[3] - return h('tr', [ - h('td', h('a', { - href: this.toUrl('/npm/' + name), - title: 'package name' - }, name), ' '), - h('td', version ? [h('a', { - href: this.toUrl('/npm/' + name + '/' + version + '/'), - title: 'package version' - }, version), ' '] : ''), - h('td', distTag ? [h('a', { - href: this.toUrl('/npm/' + name + '//' + distTag + '/'), - title: 'dist-tag' - }, distTag), ' '] : ''), - h('td', {align: 'right'}, link.size != null ? [h('span', { - title: 'tarball size' - }, this.formatSize(link.size)), ' '] : ''), - h('td', typeof link.link === 'string' ? [h('a', { - href: this.toUrl('/links/' + link.link), - title: 'package tarball' - }, link.link.substr(0, 8) + '…')] : '') - ]) + var self = this + var done = multicb({pluck: 1, spread: true}) + var base = '/npm/' + (opts.author ? u.escapeId(link.author) + '/' : '') + var pathWithAuthor = '/npm/' + + u.escapeId(link.author) + '/' + + (opts.name ? opts.name + '/' + + (opts.version ? opts.version + '/' + + (opts.distTag ? opts.distTag + '/' : '') : '') : '') + self.app.getAbout(link.author, function (err, about) { + if (err) return cb(err) + cb(null, h('tr', [ + opts.withAuthor ? h('td', h('a', { + href: self.toUrl(pathWithAuthor), + title: 'publisher' + }, about.name), ' ') : '', + h('td', h('a', { + href: self.toUrl(base + name), + title: 'package name' + }, name), ' '), + h('td', version ? [h('a', { + href: self.toUrl(base + name + '/' + version + '/'), + title: 'package version' + }, version), ' '] : ''), + h('td', distTag ? [h('a', { + href: self.toUrl(base + name + '//' + distTag + '/'), + title: 'dist-tag' + }, distTag), ' '] : ''), + h('td', {align: 'right'}, link.size != null ? [h('span', { + title: 'tarball size' + }, self.formatSize(link.size)), ' '] : ''), + h('td', typeof link.link === 'string' ? h('code', h('a', { + href: self.toUrl('/links/' + link.link), + title: 'package tarball' + }, link.link.substr(0, 8) + '…')) : '') + ])) + }) } diff --git a/lib/serve.js b/lib/serve.js index b3f886f..5ebfa47 100644 --- a/lib/serve.js +++ b/lib/serve.js @@ -1876,6 +1876,8 @@ Serve.prototype.gitObjectLinks = function (headMsgId, type) { Serve.prototype.npm = function (url) { var self = this var parts = url.split('/') + var author = parts[1] && parts[1][0] === '@' + ? u.unescapeId(parts.splice(1, 1)[0]) : null var name = parts[1] var version = parts[2] var distTag = parts[3] @@ -1884,15 +1886,27 @@ Serve.prototype.npm = function (url) { (version ? version + ':' + (distTag ? distTag + ':' : '') : '') : '') + var render = self.app.render + var base = '/npm/' + (author ? u.escapeId(author) + '/' : '') + var pathWithoutAuthor = '/npm/' + + (name ? name + '/' + + (version ? version + '/' + + (distTag ? distTag + '/' : '') : '') : '') return pull( ph('section', {}, [ - ph('h3', ['npm: ', - name ? [ph('code', name), ' '] : '', - version ? [ph('code', version), ' '] : '', - distTag ? [ph('code', distTag), ' '] : '' + ph('h3', [ph('a', {href: render.toUrl('/npm/')}, 'npm'), ' : ', + author ? [ + self.phIdLink(author), ' ', + ph('sub', ph('a', {href: render.toUrl(pathWithoutAuthor)}, '×')), + ' : ' + ] : '', + name ? [ph('a', {href: render.toUrl(base + name)}, name), ' : '] : '', + version ? [ph('a', {href: render.toUrl(base + name + '/' + version)}, version), ' : '] : '', + distTag ? [ph('a', {href: render.toUrl(base + name + '/' + version + '/' + distTag)}, distTag)] : '' ]), ph('table', [ ph('thead', ph('tr', [ + !author ? ph('td', 'publisher') : '', ph('td', 'package'), ph('td', 'version'), ph('td', 'tag'), @@ -1901,14 +1915,21 @@ Serve.prototype.npm = function (url) { ])), ph('tbody', pull( self.app.blobMentions({ - name: {$prefix: prefix} + name: {$prefix: prefix}, + author: author, }), distTag && !version && pull.filter(function (link) { return link.name.split(':')[3] === distTag }), - pull.map(function (link) { - return self.app.render.npmPackageMention(link) - }), + paramap(function (link, cb) { + self.app.render.npmPackageMention(link, { + withAuthor: !author, + author: author, + name: name, + version: version, + distTag: distTag, + }, cb) + }, 4), pull.map(u.toHTML) )) ]) diff --git a/lib/util.js b/lib/util.js index c6a4aa2..fb0b13f 100644 --- a/lib/util.js +++ b/lib/util.js @@ -1,6 +1,7 @@ var pull = require('pull-stream') var cat = require('pull-cat') var h = require('hyperscript') +var b64url = require('base64-url') var u = exports u.ssbRefRegex = /((?:@|%|&|ssb:\/\/%)[A-Za-z0-9\/+]{43}=\.[\w\d]+)/g @@ -206,3 +207,13 @@ u.mergeOpts = function (a, b) { } return obj } + +u.escapeId = function (id) { + return b64url.escape(id) +} + +u.unescapeId = function (str) { + var m = /^(.)(.*)(\..*)$/.exec(str) + if (!m) return b64url.unescape(str) + return m[1] + b64url.unescape(m[2]) + m[3] +} -- cgit v1.2.3