Commit 216ac844c0cba7f0570d5f98f0d5c49241fdc309
Link to blobs from ssb-ws for local requests
cel committed on 8/31/2018, 2:53:35 AMParent: fa946a64685781d58a9266937da0e1d9c30a20d6
Files changed
index.js | changed |
index.js | |||
---|---|---|---|
@@ -339,8 +339,12 @@ | |||
339 | 339 … | this.fetchAll = this.npmConfig.fetchAll | |
340 | 340 … | this.needShasum = this.npmConfig.needShasum | |
341 | 341 … | this.getMsg = memo({cache: lru(100)}, this.getMsg) | |
342 | 342 … | this.getFeedName = memo({cache: lru(100)}, this.getFeedName) | |
343 … | + if (sbot.ws) { | ||
344 … | + var wsPort = config.ws && Number(config.ws.port) || '8989' | ||
345 … | + this.wsUrl = 'http://localhost:' + wsPort | ||
346 … | + } | ||
343 | 347 … | } | |
344 | 348 … | ||
345 | 349 … | SsbNpmRegistryServer.prototype = Object.create(http.Server.prototype) | |
346 | 350 … | SsbNpmRegistryServer.prototype.constructor = SsbNpmRegistryServer | |
@@ -495,10 +499,22 @@ | |||
495 | 499 … | var hostname = req.headers.host | |
496 | 500 … | || (formatHost(req.socket.localAddress) + ':' + req.socket.localPort) | |
497 | 501 … | this.baseUrl = 'http://' + hostname | |
498 | 502 … | } | |
503 … | + | ||
504 … | + // prefer serving blobs from ssb-ws, for consistency of tarball URLs. | ||
505 … | + this.blobBaseUrl = this.server.wsUrl && this.isLocal() | ||
506 … | + ? this.server.wsUrl + '/blobs/get/' | ||
507 … | + : this.baseUrl + '/-/blobs/get/' | ||
499 | 508 … | } | |
500 | 509 … | ||
510 … | +Req.prototype.isLocal = function () { | ||
511 … | + var remoteAddr = this.req.socket.remoteAddress | ||
512 … | + return remoteAddr === '::1' | ||
513 … | + || remoteAddr === '::ffff:127.0.0.1' | ||
514 … | + || remoteAddr === '127.0.0.1' | ||
515 … | +} | ||
516 … | + | ||
501 | 517 … | Req.prototype.serve = function () { | |
502 | 518 … | console.log(this.req.method, this.req.url, formatHost(this.req.socket.remoteAddress)) | |
503 | 519 … | this.res.setTimeout(0) | |
504 | 520 … | var pathname = this.req.url.replace(/\?.*/, '') | |
@@ -559,9 +575,9 @@ | |||
559 | 575 … | var m = /^&([^.]+)\.([a-z0-9]+)$/.exec(id) | |
560 | 576 … | if (!m) throw new Error('bad blob id: ' + id) | |
561 | 577 … | return { | |
562 | 578 … | integrity: m[2] + '-' + m[1], | |
563 | - tarball: this.baseUrl + '/-/blobs/get/' + id | ||
579 … | + tarball: this.blobBaseUrl + id | ||
564 | 580 … | } | |
565 | 581 … | } | |
566 | 582 … | ||
567 | 583 … | Req.prototype.getMsgIdForBlobMention = function (blobId, feedId, cb) { |
Built with git-ssb-web