index.jsView |
---|
20 | 20 | var PullRequests = require('ssb-pull-requests') |
21 | 21 | var paramap = require('pull-paramap') |
22 | 22 | var Mentions = require('ssb-mentions') |
23 | 23 | var many = require('pull-many') |
| 24 | +var ident = require('pull-identify-filetype') |
| 25 | +var mime = require('mime-types') |
24 | 26 | |
25 | 27 | var hlCssPath = path.resolve(require.resolve('highlight.js'), '../../styles') |
26 | 28 | |
27 | 29 | function ParamError(msg) { |
861 | 863 | return u.readNext(function (cb) { |
862 | 864 | self.getBlob(req, key, function (err, read) { |
863 | 865 | if (err) cb(null, self.serveError(req, err)) |
864 | 866 | else if (!read) cb(null, self.serve404(req)) |
865 | | - else cb(null, self.serveRaw()(read)) |
| 867 | + else cb(null, identToResp(read)) |
866 | 868 | }) |
867 | 869 | }) |
868 | 870 | } |
| 871 | + |
| 872 | +function identToResp(read) { |
| 873 | + var ended, type, queue |
| 874 | + var id = ident(function (_type) { |
| 875 | + type = _type && mime.lookup(_type) |
| 876 | + })(read) |
| 877 | + return function (end, cb) { |
| 878 | + if (ended) return cb(ended) |
| 879 | + if (end) id(end, function (end) { |
| 880 | + cb(end === true ? null : end) |
| 881 | + }) |
| 882 | + else if (queue) { |
| 883 | + var _queue = queue |
| 884 | + queue = null |
| 885 | + cb(null, _queue) |
| 886 | + } |
| 887 | + else if (!type) |
| 888 | + id(null, function (end, data) { |
| 889 | + if (ended = end) return cb(end) |
| 890 | + queue = data |
| 891 | + cb(null, [200, { |
| 892 | + 'Content-Type': type || 'text/plain; charset=utf-8', |
| 893 | + 'Cache-Control': 'max-age=31536000' |
| 894 | + }]) |
| 895 | + }) |
| 896 | + else |
| 897 | + id(null, cb) |
| 898 | + } |
| 899 | +} |