git ssb

30+

cel / git-ssb-web



Commit 7f17070bd0ea7f8d0c5e6ab971e847e2a09e28ce

Identify blob mime types when serving

Charles Lehner committed on 5/21/2016, 3:32:36 AM
Parent: 07fef9c401badeda1ef42db7e82998eed495bbd9

Files changed

index.jschanged
package.jsonchanged
index.jsView
@@ -20,8 +20,10 @@
2020 var PullRequests = require('ssb-pull-requests')
2121 var paramap = require('pull-paramap')
2222 var Mentions = require('ssb-mentions')
2323 var many = require('pull-many')
24+var ident = require('pull-identify-filetype')
25+var mime = require('mime-types')
2426
2527 var hlCssPath = path.resolve(require.resolve('highlight.js'), '../../styles')
2628
2729 function ParamError(msg) {
@@ -861,8 +863,37 @@
861863 return u.readNext(function (cb) {
862864 self.getBlob(req, key, function (err, read) {
863865 if (err) cb(null, self.serveError(req, err))
864866 else if (!read) cb(null, self.serve404(req))
865- else cb(null, self.serveRaw()(read))
867+ else cb(null, identToResp(read))
866868 })
867869 })
868870 }
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+}
package.jsonView
@@ -6,13 +6,15 @@
66 "dependencies": {
77 "asyncmemo": "^0.1.0",
88 "diff": "^2.2.2",
99 "highlight.js": "^9.2.0",
10+ "mime-types": "^2.1.11",
1011 "multicb": "^1.2.1",
1112 "node-polyglot": "^1.0.0",
1213 "pull-cat": "^1.1.8",
1314 "pull-git-pack": "^0.2.0",
1415 "pull-git-repo": "^0.4.1",
16+ "pull-identify-filetype": "^1.1.0",
1517 "pull-many": "^1.0.6",
1618 "pull-paramap": "^1.1.6",
1719 "pull-stream": "^3.1.0",
1820 "ssb-client": "^3.0.1",

Built with git-ssb-web