-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
61 lines (51 loc) · 1.18 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
const Emitter = require('events').EventEmitter
module.exports = mr
/**
* Respond myth in an http request.
*
* @param {Function} bundler
* @return {Function}
* @api public
*/
function mr(bundler) {
var prevError = null
var pending = null
var buffer = null
build()
// bundler.on('update', update)
return handler
/**
* Build the CSS and pass it to `buffer`.
*
* @api private
*/
function build() {
const p = pending = new Emitter()
buffer = bundler.toString()
if (p !== pending) return
pending.emit('ready', null, pending = false)
}
/**
* Response middleware. Sends `buffer` to
* either `next()` or `res.body()`.
*
* @param {Request} req
* @param {Response} res
* @param {Function} next
* @api private
*/
function handler(req, res, next) {
next = next || send
if (pending) return pending.once('ready', function(err) {
if (err) return next(err)
return handler(req, res, next)
})
if (prevError) return next(prevError)
res.setHeader('content-type', 'text/css')
return next(null, buffer)
function send(err, body) {
if (err) return res.emit(err)
res.end(body)
}
}
}