Commit 3f7a2ecb3525af1f733b7d7d738395dd9e7dc16c
Merge branch 'master' into msg-scope
cel committed on 1/19/2018, 11:20:46 PMParent: 0960788465dddaaebcb17cb2cf22f4fb26fce452
Parent: 8c33afd9030e727b707d6b093f7200abbe672fcf
Files changed
index.js | changed |
package-lock.json | changed |
package.json | changed |
index.js | ||
---|---|---|
@@ -337,8 +337,9 @@ | ||
337 | 337 | for (var k in link) newLink[k] = link[k] |
338 | 338 | getPackageJsonFromTarballBlob(sbot, id, function (err, pkg) { |
339 | 339 | if (err) return cb(err) |
340 | 340 | for (var k in props) newLink[k] = pkg[k] |
341 | + if (props.shasum && !pkg.shasum) newLink.shasum = pkg._shasum | |
341 | 342 | if (!--waiting) next() |
342 | 343 | }) |
343 | 344 | return newLink |
344 | 345 | }) |
@@ -353,8 +354,9 @@ | ||
353 | 354 | this.config = config |
354 | 355 | this.npmConfig = config.npm || {} |
355 | 356 | this.host = this.npmConfig.host || 'localhost' |
356 | 357 | this.fetchAll = this.npmConfig.fetchAll |
358 | + this.needShasum = this.npmConfig.needShasum | |
357 | 359 | this.links2 = sbot.links2 |
358 | 360 | if (!this.links2) throw new Error('missing ssb-links2 scuttlebot plugin') |
359 | 361 | this.wsPort = config.ws && Number(config.ws.port) || '8989' |
360 | 362 | this.blobsPrefix = 'http://' + (config.host || 'localhost') + ':' |
@@ -544,12 +546,18 @@ | ||
544 | 546 | this.req = req |
545 | 547 | this.res = res |
546 | 548 | this.blobsToPush = [] |
547 | 549 | 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) | |
548 | 555 | } |
549 | 556 | |
550 | 557 | Req.prototype.serve = function () { |
551 | 558 | console.log(this.req.method, this.req.url, this.req.socket.remoteAddress.replace(/^::ffff:/, '')) |
559 | + this.res.setTimeout(0) | |
552 | 560 | var pathname = this.req.url.replace(/\?.*/, '') |
553 | 561 | var m |
554 | 562 | if ((m = /^\/(%25.*sha256)(\/.*)$/.exec(pathname))) { |
555 | 563 | try { |
@@ -830,16 +838,18 @@ | ||
830 | 838 | var attachments = data._attachments || {} |
831 | 839 | var links = {/* <name>-<version>.tgz: {link: <BlobId>, size: number} */} |
832 | 840 | var done = multicb() |
833 | 841 | 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') | |
835 | 844 | var length = attachments[filename].length |
836 | 845 | if (length && length !== tarball.length) return self.respondError(400, |
837 | 846 | 'Length mismatch for attachment \'' + filename + '\'') |
838 | 847 | self.server.sbot.blobs.add(function (err, id) { |
839 | 848 | if (err) return cb(err) |
840 | 849 | 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} | |
842 | 852 | cb() |
843 | 853 | })(pull.once(tarball)) |
844 | 854 | } |
845 | 855 | for (var filename in attachments) { |
@@ -900,8 +910,9 @@ | ||
900 | 910 | mentions.push({ |
901 | 911 | name: 'npm:' + name + ':' + link.version + (link.tag ? ':' + link.tag : ''), |
902 | 912 | link: link.link, |
903 | 913 | size: link.size, |
914 | + shasum: link.shasum, | |
904 | 915 | dependencies: link.dependencies, |
905 | 916 | bundledDependencies: link.bundledDependencies, |
906 | 917 | }) |
907 | 918 | } |
@@ -910,9 +921,9 @@ | ||
910 | 921 | |
911 | 922 | Req.prototype.publishPkgs = function (mentions) { |
912 | 923 | var self = this |
913 | 924 | 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) | |
915 | 926 | self.server.pushBlobs(self.blobsToPush, function (err) { |
916 | 927 | if (err) console.error('[npm-registry] Failed to push blob ' + id + ': ' + (err.stack || err)) |
917 | 928 | self.respond(201) |
918 | 929 | console.log(msgs.map(function (msg) { return msg.key }).join('\n')) |
@@ -920,28 +931,30 @@ | ||
920 | 931 | }) |
921 | 932 | } |
922 | 933 | |
923 | 934 | Req.prototype.populatePackageJson = function (obj, cb) { |
935 | + var self = this | |
924 | 936 | var blobId = obj.dist.tarball.replace(/.*\/blobs\/get\//, '') |
925 | - var self = this | |
926 | - var deps, bundledDeps | |
937 | + var deps, bundledDeps, shasum | |
927 | 938 | |
928 | 939 | // look for dependencies in links. |
929 | 940 | // then fallback to getting it from the tarball blob |
930 | 941 | |
931 | 942 | pull( |
932 | 943 | self.getMentionLinks(blobId), |
933 | 944 | pull.drain(function (link) { |
934 | 945 | if (link.dependencies) deps = link.dependencies |
946 | + if (link.shasum) shasum = link.shasum | |
935 | 947 | bundledDeps = link.bundledDependencies || link.bundleDependencies || bundledDeps |
936 | 948 | // how to handle multiple assignments of dependencies to a package? |
937 | 949 | }, function (err) { |
938 | 950 | if (err) return cb(new Error(err.stack || err)) |
939 | - if (deps) { | |
951 | + if (deps && (shasum || !self.needShasum)) { | |
940 | 952 | // assume that the dependencies in the links to the blob are |
941 | 953 | // correct. |
942 | 954 | obj.dependencies = deps |
943 | 955 | obj.bundledDependencies = bundledDeps |
956 | + if (shasum) obj.dist.shasum = obj._shasum = shasum | |
944 | 957 | cb(null, obj) |
945 | 958 | } else { |
946 | 959 | // get dependencies from the tarball |
947 | 960 | getPackageJsonFromTarballBlob(self.server.sbot, blobId, function (err, pkg) { |
Built with git-ssb-web