Files: fb32aba68ca6fa8bb5c6aa03f81d9f3cc71a50d4 / index.js
2710 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')) return cb(e) |
32 | var m = /(sha256)\/([0-9a-f]{2})\/([0-9a-f]{62})/.exec(e.path || e.message) |
33 | if (!m) return cb(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 | } |
44 | |
45 | sbot.ws.use(function (req, res, next) { |
46 | if (prefix !== req.url.substr(0, prefix.length)) return next() |
47 | if (prefix === req.url) { |
48 | res.writeHead(302, {Location: path.basename(prefix) + '/'}) |
49 | return res.end() |
50 | } |
51 | try { |
52 | req.uri = url.parse(req.url.substr(prefix.length), true) |
53 | var resolved = require.resolve(path.join(dir, req.uri.pathname)) |
54 | var prevMtime = mtimes[resolved] |
55 | var mtime = fs.statSync(resolved).mtime.getTime() |
56 | if (mtime !== prevMtime) { |
57 | delete require.cache[resolved] |
58 | } |
59 | getModule(resolved, false, gotModule) |
60 | } catch(e) { |
61 | return gotError(e) |
62 | } |
63 | |
64 | function gotError(err) { |
65 | try { |
66 | res.writeHead(err.code === 'ENOENT' || err.code === 'MODULE_NOT_FOUND' ? 404 : 500) |
67 | } finally { |
68 | res.end(err.stack || err) |
69 | } |
70 | } |
71 | |
72 | function gotModule(err, module) { |
73 | if (err) return gotError(err) |
74 | try { |
75 | module(req, res, sbot, config) |
76 | } catch(e) { |
77 | gotError(e) |
78 | } |
79 | } |
80 | }) |
81 | } |
82 |
Built with git-ssb-web