├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── index.js ├── package.json └── test.js /.gitignore: -------------------------------------------------------------------------------- 1 | # tmp files 2 | lib-cov 3 | *.seed 4 | *.log 5 | *.csv 6 | *.dat 7 | *.out 8 | *.pid 9 | *.gz 10 | 11 | # tmp folders 12 | pids/ 13 | logs/ 14 | results/ 15 | coverage/ 16 | 17 | # node.js 18 | node_modules/ 19 | npm-debug.log 20 | 21 | # osx 22 | .DS_Store 23 | 24 | derp/ 25 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | node_js: 2 | - "0.10" 3 | - "1" 4 | sudo: false 5 | language: node_js 6 | script: "npm run test-cov" 7 | after_script: "npm install coveralls@2 && cat ./coverage/lcov.info | coveralls" 8 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # brick-router 2 | [![NPM version][npm-image]][npm-url] 3 | [![build status][travis-image]][travis-url] 4 | [![Test coverage][coveralls-image]][coveralls-url] 5 | [![Downloads][downloads-image]][downloads-url] 6 | [![js-standard-style][standard-image]][standard-url] 7 | 8 | Static asset server that can write to files. 9 | 10 | ## Installation 11 | ```bash 12 | $ npm install brick-router 13 | ``` 14 | 15 | ## Usage 16 | ```js 17 | const brick = require('brick-router') 18 | const fs = require('fs') 19 | 20 | const router = brick() 21 | 22 | router.on('/index.html', cb => { 23 | const rs = fs.createReadStream('index.html') 24 | cb(null, rs) 25 | }) 26 | 27 | // use as router 28 | router.match('/index.html', (err, res) => { 29 | if (err) throw err 30 | res.pipe(process.stdout) 31 | }) 32 | 33 | // write to file 34 | router.build(__dirname + '/my-dirname') 35 | ``` 36 | 37 | ## Why? 38 | In development an application usually goes through 3 stages: 39 | - __experiment__ - some html, css, js to toy around locally 40 | - __static__ - static files, usually hosted on GitHub pages 41 | - __server__ - application with a working backend 42 | 43 | When switching stages it's common to throw out your build process, and start 44 | from scratch. `brick-router` allows you to keep the same build process by 45 | serving files both in-memory (for experimentation and servers) and being able 46 | to write to the filesystem (for static pages). 47 | 48 | ## API 49 | ### router = brick() 50 | Create a new router. 51 | 52 | ### router.on(filename, cb(err, data|stream)) 53 | Register a new path in the router. The callback either accepts data or a 54 | ReadableStream. 55 | 56 | ### router.match(filename, cb(err, res)) 57 | Match a path on the router, pass in an optional callback to the router which 58 | can later be called. 59 | 60 | ### router.build(directory, cb(err, res)) 61 | Execute all routes and write the output to a directory tree so it can be served 62 | statically. Calls an optional callback on completion. 63 | 64 | ## See Also 65 | - [wayfarer](https://github.com/yoshuawuyts/wayfarer) - composable trie based router 66 | - [chokidar](https://github.com/paulmillr/chokidar) - wrapper around node.js fs.watch / fs.watchFile 67 | - [brick-server](https://github.com/yoshuawuyts/brick-server) - HTTP frontend 68 | 69 | ## License 70 | [MIT](https://tldrlegal.com/license/mit-license) 71 | 72 | [npm-image]: https://img.shields.io/npm/v/brick-router.svg?style=flat-square 73 | [npm-url]: https://npmjs.org/package/brick-router 74 | [travis-image]: https://img.shields.io/travis/yoshuawuyts/brick-router.svg?style=flat-square 75 | [travis-url]: https://travis-ci.org/yoshuawuyts/brick-router 76 | [coveralls-image]: https://img.shields.io/coveralls/yoshuawuyts/brick-router.svg?style=flat-square 77 | [coveralls-url]: https://coveralls.io/r/yoshuawuyts/brick-router?branch=master 78 | [downloads-image]: http://img.shields.io/npm/dm/brick-router.svg?style=flat-square 79 | [downloads-url]: https://npmjs.org/package/brick-router 80 | [standard-image]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square 81 | [standard-url]: https://github.com/feross/standard 82 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const parallel = require('run-parallel') 2 | const isStream = require('is-stream') 3 | const error = require('http-errors') 4 | const assert = require('assert') 5 | const mkdirp = require('mkdirp') 6 | const noop = require('noop2') 7 | const path = require('path') 8 | const fs = require('fs') 9 | 10 | const brick = Brick.prototype 11 | 12 | module.exports = Brick 13 | 14 | // create a new router 15 | // -> null 16 | function Brick () { 17 | if (!(this instanceof Brick)) return new Brick() 18 | this.router = {} 19 | } 20 | 21 | // define a new path 22 | // str, fn -> obj 23 | brick.on = function (path, cb) { 24 | assert.equal(typeof path, 'string', 'path should be a string') 25 | cb = cb || noop 26 | this.router[path] = cb 27 | return this 28 | } 29 | 30 | // match a route against the paths 31 | // str, fn -> null 32 | brick.match = function (path, cb) { 33 | assert.equal(typeof path, 'string', 'path should be a string') 34 | cb = cb || noop 35 | const route = this.router[path] 36 | if (!route) return cb(error(404)) 37 | route(cb) 38 | } 39 | 40 | // execute all paths and write 41 | // the results to a directory tree 42 | // str, fn -> null 43 | brick.build = function (dir, cb) { 44 | assert.equal(typeof dir, 'string', 'dir should be a string') 45 | cb = cb || noop 46 | const ctx = this 47 | 48 | const fns = Object.keys(this.router).map(function (route) { 49 | return function (innerCb) { 50 | const out = path.join(dir, route) 51 | const split = route.split('/') 52 | split.pop() 53 | const loc = split.join('/') 54 | ctx.router[route](handler) 55 | 56 | // resolution callback that is passed to router fns 57 | // any, str -> null 58 | function handler (err, next) { 59 | if (err) return cb(err) 60 | if (!next) return cb('no data retrieved') 61 | 62 | mkdirp(path.join(dir, loc), function (err) { 63 | if (err) cb(err) 64 | }) 65 | 66 | if (isStream(next)) { 67 | const ws = fs.createWriteStream(out) 68 | ws.once('close', innerCb) 69 | return next.pipe(ws) 70 | } 71 | 72 | fs.writeFile(out, next, function (err, cb) { 73 | if (err) return innerCb(err) 74 | innerCb() 75 | }) 76 | } 77 | } 78 | }) 79 | 80 | parallel(fns, cb) 81 | } 82 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "brick-router", 3 | "version": "2.1.3", 4 | "description": "Modular router for serving static assets", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "standard && NODE_ENV=test node test", 8 | "test-cov": "standard && NODE_ENV=test istanbul cover test.js" 9 | }, 10 | "repository": "yoshuawuyts/brick-router", 11 | "keywords": [ 12 | "router", 13 | "build process", 14 | "modular", 15 | "static", 16 | "browserify", 17 | "myth" 18 | ], 19 | "license": "MIT", 20 | "dependencies": { 21 | "http-errors": "^1.3.1", 22 | "is-stream": "^1.0.1", 23 | "mkdirp": "^0.5.0", 24 | "noop2": "^1.0.1", 25 | "run-parallel": "^1.1.0" 26 | }, 27 | "devDependencies": { 28 | "from2": "^1.3.0", 29 | "istanbul": "^0.3.13", 30 | "proxyquire": "^1.4.0", 31 | "rimraf": "^2.3.2", 32 | "standard": "^3.6.1", 33 | "tape": "^4.0.0" 34 | }, 35 | "files": [ 36 | "LICENSE", 37 | "index.js", 38 | "README.md" 39 | ] 40 | } 41 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | const rimraf = require('rimraf') 2 | const from = require('from2') 3 | const test = require('tape') 4 | const fs = require('fs') 5 | 6 | const brick = require('./') 7 | 8 | test('should assert input types', function (t) { 9 | t.plan(1) 10 | const router = brick() 11 | t.throws(router.on, /should be a string/) 12 | }) 13 | 14 | test('should save paths as object keys', function (t) { 15 | t.plan(1) 16 | const router = brick() 17 | router.on('/foo', function () { 18 | t.pass('call fn') 19 | }) 20 | router.router['/foo']() 21 | }) 22 | 23 | test('.match() should assert input types', function (t) { 24 | t.plan(1) 25 | const router = brick() 26 | t.throws(router.match, /should be a string/) 27 | }) 28 | 29 | test('.match() should call object keys', function (t) { 30 | t.plan(2) 31 | var count = 0 32 | const router = brick() 33 | router.on('/foo', function (cb) { 34 | count += 1 35 | t.pass('call fn') 36 | cb() 37 | }) 38 | router.match('/foo', function () { 39 | t.equal(count, 1, 'count') 40 | }) 41 | }) 42 | 43 | test('.build() should assert input types', function (t) { 44 | t.plan(1) 45 | const router = brick() 46 | t.throws(router.build, /should be a string/) 47 | }) 48 | 49 | test('.build() should write file results to disk', function (t) { 50 | t.plan(5) 51 | const router = brick() 52 | router.on('/foo.txt', function (cb) { 53 | cb(null, 'my amazing data') 54 | }) 55 | 56 | router.build(__dirname + '/derp', function (err, res) { 57 | t.ifError(err) 58 | fs.readFile(__dirname + '/derp/foo.txt', 'utf8', function (err, res) { 59 | t.ifError(err) 60 | t.equal(res, 'my amazing data') 61 | rimraf(__dirname + '/derp', function (err) { 62 | t.ifError(err) 63 | t.pass('rm files') 64 | }) 65 | }) 66 | }) 67 | }) 68 | 69 | test('.build() should handle streams in callback responses', function (t) { 70 | t.plan(5) 71 | const router = brick() 72 | router.on('/foo.txt', function (cb) { 73 | const stream = fromString('my amazing data') 74 | cb(null, stream) 75 | }) 76 | 77 | router.build(__dirname + '/derp', function (err, res) { 78 | t.ifError(err) 79 | fs.readFile(__dirname + '/derp/foo.txt', 'utf8', function (err, res) { 80 | t.ifError(err) 81 | t.equal(res, 'my amazing data') 82 | rimraf(__dirname + '/derp', function (err) { 83 | t.ifError(err) 84 | t.pass('rm files') 85 | }) 86 | }) 87 | }) 88 | }) 89 | 90 | function fromString (string) { 91 | return from(function (size, next) { 92 | if (string.length <= 0) return next() 93 | var chunk = string.slice(0, size) 94 | string = string.slice(size) 95 | next(null, chunk) 96 | }) 97 | } 98 | --------------------------------------------------------------------------------