git ssb

3+

cel / ssb-npm-registry



Commit f95b21cc485b7ef795741513fe3ee31b1a37dd50

bootstrap: reuse registry HTTP API

cel committed on 8/31/2018, 5:06:00 AM
Parent: 216ac844c0cba7f0570d5f98f0d5c49241fdc309

Files changed

bootstrap/bin.jschanged
bootstrap/bin.jsView
@@ -20,9 +20,9 @@
2020 var configPath = path.join(ssbPath, 'config')
2121 var blobsTmpPath = path.join(blobsPath, 'tmp')
2222 var numTmpBlobs = 0
2323
24-var host = null
24 +var host = 'localhost'
2525 var port = null
2626 var msgsUrl = null
2727 var blobsUrl = null
2828 var server
@@ -91,32 +91,28 @@
9191 msgsUrl = viewerUrl + '/%s.json'
9292 }
9393
9494 var doExec = cmdArgs.length > 0 || cmd != null
95- if (!doExec) port = 8990
95 + if (!doExec) port = 8044
9696
97- server = http.createServer(serve)
97 + server = http.createServer()
9898 server.listen(port, host, function () {
9999 port = server.address().port
100- serveNpm1 = SsbNpmRegistry.respond({
100 + server.on('request', SsbNpmRegistry.respond({
101101 whoami: whoami,
102102 get: ssbGet,
103103 blobs: {
104104 size: blobsSize,
105105 want: blobsWant,
106106 get: blobsGet,
107107 }
108108 }, {
109- ws: {
110- port: port,
111- },
112- host: host,
113109 npm: ssbNpmConfig
114- })
110 + }))
115111
116112 if (doExec) {
117- var registryUrl = 'http://' + (host || 'localhost') + ':' + port
118- + '/npm/' + branches.map(encodeURIComponent).join(',')
113 + var registryUrl = 'http://' + host + ':' + port
114 + + '/' + branches.map(encodeURIComponent).join(',')
119115 var env = {}
120116 for (var k in process.env) env[k] = process.env[k]
121117 env.npm_config_registry = registryUrl
122118 if (!cmd) cmd = 'npm'
@@ -157,8 +153,24 @@
157153 : addr.address + ':' + addr.port
158154 console.log('Listening on http://' + listenHostname)
159155 }
160156
157 +function readNext(fn) {
158 + var next
159 + return function (end, cb) {
160 + if (next) return next(end, cb)
161 + try {
162 + fn(function (err, _next) {
163 + if (err) return cb(err)
164 + next = _next
165 + next(null, cb)
166 + })
167 + } catch(e) {
168 + cb(e)
169 + }
170 + }
171 +}
172 +
161173 function serveStatus(res, code, message) {
162174 res.writeHead(code, message)
163175 res.end(message)
164176 }
@@ -237,9 +249,9 @@
237249 res.pipe(hashThrough).pipe(writeStream, {end: false})
238250 res.on('error', function (err) {
239251 writeStream.end(function (err1) {
240252 fs.unlink(blobTmpPath, function (err2) {
241- cb(err || err1 || err2)
253 + cb(err2 || err1 || err)
242254 })
243255 })
244256 })
245257 hashThrough.on('end', function () {
@@ -253,9 +265,9 @@
253265 } else {
254266 res.unpipe(hashThrough)
255267 rename(blobTmpPath, filename, function (err) {
256268 if (err) return console.error(err)
257- if (readIt) cb(null, fs.createReadStream(null, {fd: fd, start: 0}))
269 + if (readIt) cb(null, toPull(fs.createReadStream(null, {fd: fd, start: 0})))
258270 else fs.close(fd, function (err) {
259271 if (err) return cb(err)
260272 cb(null)
261273 })
@@ -271,36 +283,12 @@
271283 function getAddBlob(id, hash, filename, cb) {
272284 fs.access(filename, fs.constants.R_OK, function (err) {
273285 if (err && err.code === 'ENOENT') return fetchAddBlob(id, hash, filename, {}, cb)
274286 if (err) return cb(err)
275- cb(null, fs.createReadStream(filename))
287 + cb(null, pullFile(filename))
276288 })
277289 }
278290
279-function serveBlobsGet(req, res, id) {
280- try { id = decodeURIComponent(id) }
281- catch (e) {}
282- var hash = idToBuf(id)
283- var filename = blobFilename(hash)
284- getAddBlob(id, hash, filename, function (err, stream) {
285- if (err) return serveStatus(res, 500, err.message)
286- if (!stream) return serveStatus(res, 404, 'Blob Not Found')
287- res.writeHead(200)
288- stream.pipe(res)
289- })
290-}
291-
292-function serveMsg(req, res, id) {
293- try { id = decodeURIComponent(id) }
294- catch (e) {}
295- ssbGet(id, function (err, value) {
296- if (err) return serveStatus(res, 400, err.message)
297- if (!msg) return serveStatus(res, 404, 'Msg Not Found')
298- res.writeHead(200, {'Content-Type': 'application/json'})
299- res.end(JSON.stringify({key: id, value: msg}))
300- })
301-}
302-
303291 function whoami(cb) {
304292 cb(null, {id: 'foo'})
305293 }
306294
@@ -364,31 +352,18 @@
364352 })
365353 }
366354
367355 function blobsGet(id) {
368- var filename = blobFilename(idToBuf(id))
369- if (!filename) return cb(new Error('bad id'))
370- return pullFile(filename)
356 + return readNext(function (cb) {
357 + var hash = idToBuf(id)
358 + var filename = blobFilename(hash)
359 + getAddBlob(id, hash, filename, cb)
360 + })
371361 }
372362
373363 var ssbNpmConfig
374364 try {
375365 ssbNpmConfig = JSON.stringify(fs.readFileSync(configPath)).npm
376366 } catch(e) {
377367 }
378368
379-var serveNpm1
380-function serveNpm(req, res, url) {
381- req.url = url
382- serveNpm1(req, res)
383-}
384-
385-function serve(req, res) {
386- res.setTimeout(0)
387- var p = URL.parse(req.url)
388- if (p.pathname.startsWith('/npm/')) return serveNpm(req, res, p.pathname.substr(4))
389- if (p.pathname.startsWith('/msg/')) return serveMsg(req, res, p.pathname.substr(5))
390- if (p.pathname.startsWith('/blobs/get/')) return serveBlobsGet(req, res, p.pathname.substr(11))
391- return serveStatus(res, 404, 'Not Found')
392-}
393-
394369 main(process.argv.slice(2))

Built with git-ssb-web