Commit f95b21cc485b7ef795741513fe3ee31b1a37dd50
bootstrap: reuse registry HTTP API
cel committed on 8/31/2018, 5:06:00 AMParent: 216ac844c0cba7f0570d5f98f0d5c49241fdc309
Files changed
bootstrap/bin.js | changed |
bootstrap/bin.js | ||
---|---|---|
@@ -20,9 +20,9 @@ | ||
20 | 20 … | var configPath = path.join(ssbPath, 'config') |
21 | 21 … | var blobsTmpPath = path.join(blobsPath, 'tmp') |
22 | 22 … | var numTmpBlobs = 0 |
23 | 23 … | |
24 | -var host = null | |
24 … | +var host = 'localhost' | |
25 | 25 … | var port = null |
26 | 26 … | var msgsUrl = null |
27 | 27 … | var blobsUrl = null |
28 | 28 … | var server |
@@ -91,32 +91,28 @@ | ||
91 | 91 … | msgsUrl = viewerUrl + '/%s.json' |
92 | 92 … | } |
93 | 93 … | |
94 | 94 … | var doExec = cmdArgs.length > 0 || cmd != null |
95 | - if (!doExec) port = 8990 | |
95 … | + if (!doExec) port = 8044 | |
96 | 96 … | |
97 | - server = http.createServer(serve) | |
97 … | + server = http.createServer() | |
98 | 98 … | server.listen(port, host, function () { |
99 | 99 … | port = server.address().port |
100 | - serveNpm1 = SsbNpmRegistry.respond({ | |
100 … | + server.on('request', SsbNpmRegistry.respond({ | |
101 | 101 … | whoami: whoami, |
102 | 102 … | get: ssbGet, |
103 | 103 … | blobs: { |
104 | 104 … | size: blobsSize, |
105 | 105 … | want: blobsWant, |
106 | 106 … | get: blobsGet, |
107 | 107 … | } |
108 | 108 … | }, { |
109 | - ws: { | |
110 | - port: port, | |
111 | - }, | |
112 | - host: host, | |
113 | 109 … | npm: ssbNpmConfig |
114 | - }) | |
110 … | + })) | |
115 | 111 … | |
116 | 112 … | 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(',') | |
119 | 115 … | var env = {} |
120 | 116 … | for (var k in process.env) env[k] = process.env[k] |
121 | 117 … | env.npm_config_registry = registryUrl |
122 | 118 … | if (!cmd) cmd = 'npm' |
@@ -157,8 +153,24 @@ | ||
157 | 153 … | : addr.address + ':' + addr.port |
158 | 154 … | console.log('Listening on http://' + listenHostname) |
159 | 155 … | } |
160 | 156 … | |
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 … | + | |
161 | 173 … | function serveStatus(res, code, message) { |
162 | 174 … | res.writeHead(code, message) |
163 | 175 … | res.end(message) |
164 | 176 … | } |
@@ -237,9 +249,9 @@ | ||
237 | 249 … | res.pipe(hashThrough).pipe(writeStream, {end: false}) |
238 | 250 … | res.on('error', function (err) { |
239 | 251 … | writeStream.end(function (err1) { |
240 | 252 … | fs.unlink(blobTmpPath, function (err2) { |
241 | - cb(err || err1 || err2) | |
253 … | + cb(err2 || err1 || err) | |
242 | 254 … | }) |
243 | 255 … | }) |
244 | 256 … | }) |
245 | 257 … | hashThrough.on('end', function () { |
@@ -253,9 +265,9 @@ | ||
253 | 265 … | } else { |
254 | 266 … | res.unpipe(hashThrough) |
255 | 267 … | rename(blobTmpPath, filename, function (err) { |
256 | 268 … | 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}))) | |
258 | 270 … | else fs.close(fd, function (err) { |
259 | 271 … | if (err) return cb(err) |
260 | 272 … | cb(null) |
261 | 273 … | }) |
@@ -271,36 +283,12 @@ | ||
271 | 283 … | function getAddBlob(id, hash, filename, cb) { |
272 | 284 … | fs.access(filename, fs.constants.R_OK, function (err) { |
273 | 285 … | if (err && err.code === 'ENOENT') return fetchAddBlob(id, hash, filename, {}, cb) |
274 | 286 … | if (err) return cb(err) |
275 | - cb(null, fs.createReadStream(filename)) | |
287 … | + cb(null, pullFile(filename)) | |
276 | 288 … | }) |
277 | 289 … | } |
278 | 290 … | |
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 | - | |
303 | 291 … | function whoami(cb) { |
304 | 292 … | cb(null, {id: 'foo'}) |
305 | 293 … | } |
306 | 294 … | |
@@ -364,31 +352,18 @@ | ||
364 | 352 … | }) |
365 | 353 … | } |
366 | 354 … | |
367 | 355 … | 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 … | + }) | |
371 | 361 … | } |
372 | 362 … | |
373 | 363 … | var ssbNpmConfig |
374 | 364 … | try { |
375 | 365 … | ssbNpmConfig = JSON.stringify(fs.readFileSync(configPath)).npm |
376 | 366 … | } catch(e) { |
377 | 367 … | } |
378 | 368 … | |
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 | - | |
394 | 369 … | main(process.argv.slice(2)) |
Built with git-ssb-web