git ssb

3+

cel / ssb-npm-registry



Commit 92267ec828419ae24882f44ece54c5fb0b5ec4d3

Serve blobs and messages

cel committed on 8/30/2018, 3:21:35 AM
Parent: 49058f5f9a406656f03018486b4535f0bcb5250b

Files changed

README.mdchanged
index.jschanged
README.mdView
@@ -100,8 +100,16 @@
100100 ### `/-/bootstrap` - Bootstrap shell snippet
101101
102102 Serves a shell script to for installing `ssb-npm-registry` and using it to install other `ssb-npm` packages by running in a bootstrap mode where it does not require running `scuttlebot`.
103103
104 +### `/-/blobs/get/:id` - Blobs
105 +
106 +Fetch a SSB blob by its id.
107 +
108 +### `/-/msg/:id` - Blobs
109 +
110 +Fetch a SSB message by its id, as JSON.
111 +
104112 ### `/-/prebuild/:name` - Prebuild blobs
105113
106114 `name` should be the name of a blob in the format
107115 `{name}-v{version}-{runtime}-v{abi}-{platform}{libc}-{arch}.tar.gz`.
index.jsView
@@ -359,13 +359,17 @@
359359
360360 function getBlob(sbot, id, cb) {
361361 var blobs = sbot.blobs
362362 blobs.size(id, function (err, size) {
363- if (typeof size === 'number') cb(null, blobs.get(id))
363 + if (err) return cb(err)
364 + if (typeof size === 'number') cb(null, blobs.get(id), size)
364365 else blobs.want(id, function (err, got) {
365366 if (err) cb(err)
366367 else if (!got) cb('missing blob ' + id)
367- else cb(null, blobs.get(id))
368 + else blobs.size(id, function (err, size) {
369 + if (err) return cb(err)
370 + cb(null, blobs.get(id), size)
371 + })
368372 })
369373 })
370374 }
371375
@@ -477,8 +481,10 @@
477481 if (pathname === '/-/bootstrap') return this.serveBootstrap()
478482 if (pathname === '/-/whoami') return this.serveWhoami()
479483 if (pathname === '/-/ping') return this.respond(200, true)
480484 if (pathname === '/-/user/org.couchdb.user:1') return this.serveUser1()
485 + if (pathname.startsWith('/-/blobs/get/')) return this.serveBlob(pathname.substr(13))
486 + if (pathname.startsWith('/-/msg/')) return this.serveMsg(pathname.substr(7))
481487 if ((m = /^\/-\/prebuild\/(.*)$/.exec(pathname))) return this.servePrebuild(m[1])
482488 if (!/^\/-\//.test(pathname)) return this.servePkg(pathname.substr(1))
483489 return this.respond(404, {error: 'Not found'})
484490 }
@@ -496,8 +502,13 @@
496502 this.res.writeHead(status, {'content-type': 'text/plain'})
497503 this.res.end(err.stack || err)
498504 }
499505
506 +Req.prototype.respondRaw = function (status, body) {
507 + this.res.writeHead(status)
508 + this.res.end(body)
509 +}
510 +
500511 Req.prototype.serveHome = function () {
501512 var self = this
502513 self.res.writeHead(200, {'content-type': 'text/html'})
503514 var port = 8044
@@ -633,8 +644,48 @@
633644 Req.prototype.serveUser1 = function () {
634645 this.respond(this.req.method === 'PUT' ? 201 : 200, {token: '1'})
635646 }
636647
648 +Req.prototype.serveBlob = function (id) {
649 + var self = this
650 + if (self.req.headers['if-none-match'] === id) return self.respondRaw(304)
651 + getBlob(self.server.sbot, id, function (err, readBlob, size) {
652 + if (err) {
653 + if (/^invalid/.test(err.message)) return self.respondErrorStr(400, err.message)
654 + else return self.respondErrorStr(500, err.message || err)
655 + }
656 + self.res.writeHead(200, {
657 + 'Cache-Control': 'public, max-age=315360000',
658 + 'Content-Length': size,
659 + 'etag': id
660 + })
661 + pull(
662 + readBlob,
663 + toPull(self.res, function (err) {
664 + if (err) console.error('[npm-registry]', err)
665 + })
666 + )
667 + })
668 +}
669 +
670 +Req.prototype.serveMsg = function (id) {
671 + var self = this
672 + try { id = decodeURIComponent(id) }
673 + catch (e) {}
674 + if (self.req.headers['if-none-match'] === id) return self.respondRaw(304)
675 + self.server.sbot.get(id, function (err, value) {
676 + if (err) return self.respondError(500, err.message || err)
677 + var out = new Buffer(JSON.stringify({key: id, value: value}, null, 2), 'utf8')
678 + self.res.writeHead(200, {
679 + 'Content-Type': 'application/json',
680 + 'Cache-Control': 'public, max-age=315360000',
681 + 'Content-Length': out.length,
682 + 'etag': id
683 + })
684 + self.res.end(out)
685 + })
686 +}
687 +
637688 function decodeName(name) {
638689 var parts = String(name).replace(/\.tgz$/, '').split(':')
639690 return {
640691 name: parts[1],

Built with git-ssb-web