├── .gitignore ├── .npmignore ├── LICENSE.md ├── README.md ├── index.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | *.log 3 | .DS_Store 4 | bundle.js 5 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | *.log 3 | .DS_Store 4 | bundle.js 5 | test 6 | test.js 7 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | This software is released under the MIT license: 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of 4 | this software and associated documentation files (the "Software"), to deal in 5 | the Software without restriction, including without limitation the rights to 6 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 7 | the Software, and to permit persons to whom the Software is furnished to do so, 8 | subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 15 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 16 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 17 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 18 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 19 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # watchify-request [![experimental](http://badges.github.io/stability-badges/dist/experimental.svg)](http://github.com/badges/stability-badges) 2 | 3 | Wraps a [watchify](http://github.com/substack/watchify) instance into an HTTP 4 | request handler, performing the minimal amount of rebundling required and 5 | pausing requests mid-build to avoid getting old versions of your script on 6 | reload. 7 | 8 | ## Usage 9 | 10 | [![NPM](https://nodei.co/npm/watchify-request.png)](https://nodei.co/npm/watchify-request/) 11 | 12 | ### `handler = watchifyRequest(bundler)` 13 | 14 | Creates a request handler out of a `watchify` or `browserify` instance. 15 | 16 | ### `handler(req, res[, done])` 17 | 18 | Handles the request, where `req` is an `HTTPRequest` instance and `res` is its 19 | matching `HTTPResponse` instance. `done(err, body)` is optional, and called if 20 | you want to add a custom error handler or manipulate the request after 21 | `browserify` is done. 22 | 23 | ## See also 24 | - [myth-request](https://github.com/yoshuawuyts/myth-request) 25 | - [koa-watchify](https://github.com/yoshuawuyts/koa-watchify) 26 | 27 | ## License 28 | 29 | MIT. See [LICENSE.md](http://github.com/hughsk/watchify-request/blob/master/LICENSE.md) for details. 30 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var Emitter = require('events/') 2 | var bl = require('bl') 3 | 4 | module.exports = wreq 5 | 6 | function wreq(bundler) { 7 | var prevError = null 8 | var pending = null 9 | var buffer = null 10 | 11 | update() 12 | bundler.on('update', update) 13 | 14 | return handler 15 | 16 | function update() { 17 | var p = pending = new Emitter 18 | 19 | bundler.bundle().pipe(bl(function(err, _buffer) { 20 | if (p !== pending) return 21 | buffer = _buffer 22 | pending.emit('ready', prevError = err, pending = false) 23 | })) 24 | } 25 | 26 | function handler(req, res, next) { 27 | next = next || send 28 | 29 | if (pending) { 30 | return pending.once('ready', function(err) { 31 | if (err) return next(err) 32 | return handler(req, res, next) 33 | }) 34 | } 35 | 36 | if (prevError) return next(prevError) 37 | 38 | if (res) res.setHeader('content-type', 'text/javascript') 39 | 40 | return next(null, buffer) 41 | 42 | function send(err, body) { 43 | if (err) return res.emit(err) 44 | res.end(body) 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "watchify-request", 3 | "version": "2.1.0", 4 | "description": "Wraps a watchify instance into an HTTP request handler", 5 | "main": "index.js", 6 | "license": "MIT", 7 | "scripts": {}, 8 | "author": { 9 | "name": "Hugh Kennedy", 10 | "email": "hughskennedy@gmail.com", 11 | "url": "http://hughsk.io/" 12 | }, 13 | "dependencies": { 14 | "bl": "^0.9.3", 15 | "events": "^1.0.2" 16 | }, 17 | "devDependencies": {}, 18 | "repository": { 19 | "type": "git", 20 | "url": "git://github.com/hughsk/watchify-request.git" 21 | }, 22 | "keywords": [ 23 | "watchify", 24 | "browserify", 25 | "http", 26 | "request", 27 | "server" 28 | ], 29 | "homepage": "https://github.com/hughsk/watchify-request", 30 | "bugs": { 31 | "url": "https://github.com/hughsk/watchify-request/issues" 32 | } 33 | } 34 | --------------------------------------------------------------------------------