Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

bankai

Package Overview
Dependencies
Maintainers
3
Versions
114
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bankai - npm Package Compare versions

Comparing version 2.0.0 to 2.0.1

handler-css.js

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-*"
]
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc