beaker-plugin-dat
Version:
Dat-protocol plugin for the Beaker browser
33 lines (29 loc) • 868 B
JavaScript
const through2 = require('through2')
const identify = require('identify-filetype')
const mime = require('mime')
const log = require('loglevel')
// config default mimetype
mime.default_type = 'text/plain'
exports.identifyStream = function (name, cb) {
var first = true
return through2(function (chunk, enc, cb2) {
if (first) {
first = false
// try to identify the type by the chunk contents
var mimeType
var identifiedExt = identify(chunk)
if (identifiedExt)
mimeType = mime.lookup(identifiedExt)
if (mimeType)
log.debug('[DAT] Identified entry mimetype as', mimeType)
else {
// fallback to using the entry name
mimeType = mime.lookup(name)
log.debug('[DAT] Assumed mimetype from entry name', mimeType)
}
cb(mimeType)
}
this.push(chunk)
cb2()
})
}