Files: 0746ae0dd03e573e8cccf7ec2ed1cf97d6f7a044 / index.js
2740 bytesRaw
1 | var url = require('url') |
2 | var path = require('path') |
3 | var fs = require('fs') |
4 | |
5 | var blobsDir = path.resolve(__dirname, '../../blobs') |
6 | |
7 | exports.requireBlob = function (id) { |
8 | var m = /^\&([0-9a-zA-Z\/+=]{44})\.(sha256)$/.exec(id) |
9 | if (!m) throw new Error('invalid blob id: ' + id) |
10 | var hex = new Buffer(m[1], 'base64').toString('hex') |
11 | return require(path.join(blobsDir, m[2], hex.substr(0, 2), hex.substr(2))) |
12 | } |
13 | |
14 | exports.name = 'blob-modules' |
15 | exports.version = '1.0.0' |
16 | exports.manifest = { |
17 | } |
18 | exports.init = function (sbot, config) { |
19 | if (!sbot.ws || !sbot.ws.use) return console.trace('missing ssb-ws') |
20 | var prefix = '/blob-modules' |
21 | var dir = path.join(config.path, 'blob-modules') |
22 | blobsDir = path.join(config.path, 'blobs') |
23 | var mtimes = {} |
24 | console.log('[blob-modules] init. dir:', dir, 'prefix:', prefix) |
25 | |
26 | function getModule(resolved, tried, cb) { |
27 | try { |
28 | var module = require(resolved) |
29 | return cb(null, module) |
30 | } catch(e) { |
31 | if (!tried && e && (e.code === 'ENOENT' || e.code === 'MODULE_NOT_FOUND')) { |
32 | var m = /(sha256)\/([0-9a-f]{2})\/([0-9a-f]{62})/.exec(e.path || e.message) |
33 | if (!m) throw e |
34 | var blobId = '&' + new Buffer(m[2] + m[3], 'hex').toString('base64') + '.' + m[1] |
35 | if (!sbot.blobs) return cb(new Error('Missing ssb-blobs plugin')) |
36 | if (typeof sbot.blobs.want !== 'function') return cb(new Error('missing blobs.want method')) |
37 | return sbot.blobs.want(blobId, function (err, has) { |
38 | if (err) return cb(new Error('Unable to get blob: ' + (err.stack || err))) |
39 | if (!has) return cb(new Error('Unable to get blob')) |
40 | getModule(resolved, true, cb) |
41 | }) |
42 | } |
43 | return cb(e) |
44 | } |
45 | } |
46 | |
47 | sbot.ws.use(function (req, res, next) { |
48 | if (prefix !== req.url.substr(0, prefix.length)) return next() |
49 | if (prefix === req.url) { |
50 | res.writeHead(302, {Location: path.basename(prefix) + '/'}) |
51 | return res.end() |
52 | } |
53 | try { |
54 | req.uri = url.parse(req.url.substr(prefix.length), true) |
55 | var resolved = require.resolve(path.join(dir, req.uri.pathname)) |
56 | var prevMtime = mtimes[resolved] |
57 | var mtime = fs.statSync(resolved).mtime.getTime() |
58 | if (mtime !== prevMtime) { |
59 | delete require.cache[resolved] |
60 | } |
61 | getModule(resolved, false, gotModule) |
62 | } catch(e) { |
63 | return gotError(e) |
64 | } |
65 | |
66 | function gotError(err) { |
67 | try { |
68 | res.writeHead(err.code === 'ENOENT' || err.code === 'MODULE_NOT_FOUND' ? 404 : 500) |
69 | } finally { |
70 | res.end(err.stack || err) |
71 | } |
72 | } |
73 | |
74 | function gotModule(err, module) { |
75 | if (err) return gotError(err) |
76 | try { |
77 | module(req, res, sbot, config) |
78 | } catch(e) { |
79 | gotError(e) |
80 | } |
81 | } |
82 | }) |
83 | } |
84 |
Built with git-ssb-web