git ssb

3+

cel / ssb-npm-registry



Commit 3f7a2ecb3525af1f733b7d7d738395dd9e7dc16c

Merge branch 'master' into msg-scope

cel committed on 1/19/2018, 11:20:46 PM
Parent: 0960788465dddaaebcb17cb2cf22f4fb26fce452
Parent: 8c33afd9030e727b707d6b093f7200abbe672fcf

Files changed

index.jschanged
package-lock.jsonchanged
package.jsonchanged
index.jsView
@@ -337,8 +337,9 @@
337337 for (var k in link) newLink[k] = link[k]
338338 getPackageJsonFromTarballBlob(sbot, id, function (err, pkg) {
339339 if (err) return cb(err)
340340 for (var k in props) newLink[k] = pkg[k]
341+ if (props.shasum && !pkg.shasum) newLink.shasum = pkg._shasum
341342 if (!--waiting) next()
342343 })
343344 return newLink
344345 })
@@ -353,8 +354,9 @@
353354 this.config = config
354355 this.npmConfig = config.npm || {}
355356 this.host = this.npmConfig.host || 'localhost'
356357 this.fetchAll = this.npmConfig.fetchAll
358+ this.needShasum = this.npmConfig.needShasum
357359 this.links2 = sbot.links2
358360 if (!this.links2) throw new Error('missing ssb-links2 scuttlebot plugin')
359361 this.wsPort = config.ws && Number(config.ws.port) || '8989'
360362 this.blobsPrefix = 'http://' + (config.host || 'localhost') + ':'
@@ -544,12 +546,18 @@
544546 this.req = req
545547 this.res = res
546548 this.blobsToPush = []
547549 this.fetchAll = server.fetchAll != null ? server.fetchAll : true
550+ var ua = this.req.headers['user-agent']
551+ var m = /\bnpm\/([0-9]*)/.exec(ua)
552+ var npmVersion = m && m[1]
553+ this.needShasum = server.needShasum != null ? server.needShasum :
554+ (npmVersion && npmVersion < 5)
548555 }
549556
550557 Req.prototype.serve = function () {
551558 console.log(this.req.method, this.req.url, this.req.socket.remoteAddress.replace(/^::ffff:/, ''))
559+ this.res.setTimeout(0)
552560 var pathname = this.req.url.replace(/\?.*/, '')
553561 var m
554562 if ((m = /^\/(%25.*sha256)(\/.*)$/.exec(pathname))) {
555563 try {
@@ -830,16 +838,18 @@
830838 var attachments = data._attachments || {}
831839 var links = {/* <name>-<version>.tgz: {link: <BlobId>, size: number} */}
832840 var done = multicb()
833841 function addAttachmentAsBlob(filename, cb) {
834- var tarball = new Buffer(attachments[filename].data, 'base64')
842+ var data = attachments[filename].data
843+ var tarball = new Buffer(data, 'base64')
835844 var length = attachments[filename].length
836845 if (length && length !== tarball.length) return self.respondError(400,
837846 'Length mismatch for attachment \'' + filename + '\'')
838847 self.server.sbot.blobs.add(function (err, id) {
839848 if (err) return cb(err)
840849 self.blobsToPush.push(id)
841- links[filename] = {link: id, size: tarball.length}
850+ var shasum = crypto.createHash('sha1').update(tarball).digest('hex')
851+ links[filename] = {link: id, size: tarball.length, shasum: shasum}
842852 cb()
843853 })(pull.once(tarball))
844854 }
845855 for (var filename in attachments) {
@@ -900,8 +910,9 @@
900910 mentions.push({
901911 name: 'npm:' + name + ':' + link.version + (link.tag ? ':' + link.tag : ''),
902912 link: link.link,
903913 size: link.size,
914+ shasum: link.shasum,
904915 dependencies: link.dependencies,
905916 bundledDependencies: link.bundledDependencies,
906917 })
907918 }
@@ -910,9 +921,9 @@
910921
911922 Req.prototype.publishPkgs = function (mentions) {
912923 var self = this
913924 exports.publishPkgMentions(self.server.sbot, mentions, function (err, msgs) {
914- if (err) self.respondError(500, err.stack || err)
925+ if (err) return self.respondError(500, err.stack || err)
915926 self.server.pushBlobs(self.blobsToPush, function (err) {
916927 if (err) console.error('[npm-registry] Failed to push blob ' + id + ': ' + (err.stack || err))
917928 self.respond(201)
918929 console.log(msgs.map(function (msg) { return msg.key }).join('\n'))
@@ -920,28 +931,30 @@
920931 })
921932 }
922933
923934 Req.prototype.populatePackageJson = function (obj, cb) {
935+ var self = this
924936 var blobId = obj.dist.tarball.replace(/.*\/blobs\/get\//, '')
925- var self = this
926- var deps, bundledDeps
937+ var deps, bundledDeps, shasum
927938
928939 // look for dependencies in links.
929940 // then fallback to getting it from the tarball blob
930941
931942 pull(
932943 self.getMentionLinks(blobId),
933944 pull.drain(function (link) {
934945 if (link.dependencies) deps = link.dependencies
946+ if (link.shasum) shasum = link.shasum
935947 bundledDeps = link.bundledDependencies || link.bundleDependencies || bundledDeps
936948 // how to handle multiple assignments of dependencies to a package?
937949 }, function (err) {
938950 if (err) return cb(new Error(err.stack || err))
939- if (deps) {
951+ if (deps && (shasum || !self.needShasum)) {
940952 // assume that the dependencies in the links to the blob are
941953 // correct.
942954 obj.dependencies = deps
943955 obj.bundledDependencies = bundledDeps
956+ if (shasum) obj.dist.shasum = obj._shasum = shasum
944957 cb(null, obj)
945958 } else {
946959 // get dependencies from the tarball
947960 getPackageJsonFromTarballBlob(self.server.sbot, blobId, function (err, pkg) {
package-lock.jsonView
@@ -1,7 +1,7 @@
11 {
22 "name": "ssb-npm-registry",
3- "version": "1.6.1",
3+ "version": "1.7.1",
44 "lockfileVersion": 1,
55 "requires": true,
66 "dependencies": {
77 "asyncmemo": {
package.jsonView
@@ -1,7 +1,7 @@
11 {
22 "name": "ssb-npm-registry",
3- "version": "1.6.1",
3+ "version": "1.7.1",
44 "description": "npm registry server backed by ssb",
55 "author": "cel",
66 "license": "AGPL-3.0+",
77 "main": "index.js",

Built with git-ssb-web