Comparing version 2.0.0 to 2.0.1
163
index.js
@@ -1,159 +0,14 @@ | ||
const lrScript = require('inject-lr-script-stream') | ||
const htmlIndex = require('simple-html-index') | ||
const stream = require('readable-stream') | ||
const errorify = require('errorify') | ||
const watchify = require('watchify') | ||
const Emitter = require('events') | ||
const assert = require('assert') | ||
const xtend = require('xtend') | ||
const path = require('path') | ||
const bl = require('bl') | ||
const env = process.env.NODE_ENV | ||
const state = new Emitter() | ||
var jsRegistered = false | ||
var cssReady = false | ||
const bus = new Emitter() | ||
state.cssStream = new stream.PassThrough() | ||
state.jsRegistered = false | ||
state.cssReady = false | ||
state.cssOpts = null | ||
state.cssBuf = null | ||
var cssOpts = null | ||
var cssBuf = null | ||
// create html stream | ||
// obj? -> (req, res) -> rstream | ||
exports.html = function html (opts) { | ||
opts = opts || {} | ||
const defaultOpts = { entry: '/bundle.js', css: '/bundle.css' } | ||
const htmlOpts = xtend(defaultOpts, opts) | ||
const html = htmlIndex(htmlOpts) | ||
const htmlBuf = (env === 'development') | ||
? html.pipe(lrScript()).pipe(bl()) | ||
: html.pipe(bl()) | ||
return function (req, res) { | ||
res.setHeader('Content-Type', 'text/html') | ||
return htmlBuf.duplicate() | ||
} | ||
} | ||
// create css stream | ||
// obj? -> (req, res) -> rstream | ||
exports.css = function css (opts) { | ||
opts = opts || {} | ||
assert.equal(typeof opts, 'object', 'opts must be an object') | ||
cssOpts = opts | ||
if (jsRegistered) throw new Error('css must be registered before js to work') | ||
return function (req, res) { | ||
res.setHeader('Content-Type', 'text/css') | ||
if (!cssBuf) throw new Error('no css found, did you register bankai.js?') | ||
if (!cssReady) { | ||
const ts = new stream.PassThrough() | ||
bus.once('css:ready', function () { | ||
cssBuf.duplicate().pipe(ts) | ||
}) | ||
return ts | ||
} else { | ||
return cssBuf.duplicate() | ||
} | ||
} | ||
} | ||
// create js stream | ||
// (fn, str, obj?) -> (req, res) -> rstream | ||
exports.js = function js (browserify, src, opts) { | ||
assert.equal(typeof browserify, 'function', 'browserify should be a fn') | ||
assert.equal(typeof src, 'string', 'src should be a location') | ||
opts = opts || {} | ||
jsRegistered = true | ||
const defaultOpts = { | ||
cache: {}, | ||
packageCache: {}, | ||
entries: [ require.resolve(src) ], | ||
fullPaths: true | ||
} | ||
var b = browserify(xtend(defaultOpts, opts)) | ||
// enable css if registered | ||
if (cssOpts) { | ||
if (!cssBuf || process.env.NODE_ENV === 'development') { | ||
cssBuf = bl() | ||
} | ||
const styleOpts = xtend(cssOpts, { | ||
out: cssBuf, | ||
basedir: path.dirname(module.parent) | ||
}) | ||
b.transform('sheetify/transform', styleOpts) | ||
} | ||
if (process.env.NODE_ENV === 'development') { | ||
b.plugin(errorify) | ||
b = watchify(b) | ||
} | ||
const handler = wreq(b, function () { | ||
cssBuf.end() | ||
}) | ||
return function (req, res) { | ||
const ts = new stream.PassThrough() | ||
handler(req, res, function (err, js) { | ||
if (err) return ts.emit('error', err) | ||
cssBuf.end() | ||
res.setHeader('Content-Type', 'application/javascript') | ||
ts.end(js) | ||
}) | ||
return ts | ||
} | ||
} | ||
// handle watchify updates | ||
// (obj, fn) -> null | ||
function wreq (bundler, startFn) { | ||
var prevError = null | ||
var pending = null | ||
var buffer = null | ||
var started = false | ||
update() | ||
bundler.on('update', update) | ||
return handler | ||
// run the bundler and cache output | ||
function update () { | ||
var p = pending = new Emitter() | ||
const r = bundler.bundle() | ||
if (!started) { | ||
started = true | ||
r.once('end', startFn) | ||
r.once('end', function () { | ||
cssReady = true | ||
bus.emit('css:ready') | ||
}) | ||
} | ||
r.pipe(bl(function (err, _buffer) { | ||
if (p !== pending) return | ||
buffer = _buffer | ||
pending.emit('ready', prevError = err, pending = false) | ||
})) | ||
} | ||
// call the handler function | ||
function handler (req, res, next) { | ||
if (pending) { | ||
pending.once('ready', function (err) { | ||
if (err) return next(err) | ||
handler(req, res, next) | ||
}) | ||
} else if (prevError) { | ||
next(prevError) | ||
} else { | ||
next(null, buffer) | ||
} | ||
} | ||
} | ||
exports.html = require('./handler-html')(state) | ||
exports.css = require('./handler-css')(state) | ||
exports.js = require('./handler-js')(state) |
{ | ||
"name": "bankai", | ||
"version": "2.0.0", | ||
"version": "2.0.1", | ||
"description": "DIY asset server", | ||
@@ -30,3 +30,3 @@ "main": "index.js", | ||
"readable-stream": "^2.0.5", | ||
"simple-html-index": "^1.1.2", | ||
"simple-html-index": "^1.2.0", | ||
"watchify": "^3.6.1", | ||
@@ -48,4 +48,5 @@ "xtend": "^4.0.1" | ||
"index.js", | ||
"bin/*" | ||
"bin/*", | ||
"handler-*" | ||
] | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
10085
7
176
1
Updatedsimple-html-index@^1.2.0