-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 5a8d1d3
Showing
5 changed files
with
180 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"extends": ["standard"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# Logs | ||
logs | ||
*.log | ||
|
||
# Runtime data | ||
pids | ||
*.pid | ||
*.seed | ||
|
||
# Directory for instrumented libs generated by jscoverage/JSCover | ||
lib-cov | ||
|
||
# Coverage directory used by tools like istanbul | ||
coverage | ||
|
||
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) | ||
.grunt | ||
|
||
# node-waf configuration | ||
.lock-wscript | ||
|
||
# Compiled binary addons (http://nodejs.org/api/addons.html) | ||
build/Release | ||
|
||
# Dependency directory | ||
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git | ||
node_modules | ||
|
||
lib/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#!/usr/bin/env node | ||
|
||
var path = require('path') | ||
|
||
require(path.join(process.cwd(), 'frockfile.js')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
{ | ||
"name": "frock", | ||
"version": "0.0.0", | ||
"description": "A configurable mock HTTP server", | ||
"main": "lib/index.js", | ||
"bin": { | ||
"frock": "./bin/cli.js" | ||
}, | ||
"scripts": { | ||
"test": "npm run lint && npm run compile && babel test/index.js | tape", | ||
"prepublish": "npm run compile", | ||
"compile": "babel src --out-dir lib", | ||
"lint": "standard src/**/*.js test/**/*.js" | ||
}, | ||
"author": "Urban Airship", | ||
"license": "Apache-2.0", | ||
"devDependencies": { | ||
"babel": "^5.6.14", | ||
"eslint": "^1.1.0", | ||
"eslint-config-standard": "^4.1.0", | ||
"eslint-config-standard-react": "^1.0.4", | ||
"eslint-plugin-react": "^3.2.2", | ||
"eslint-plugin-standard": "^1.2.0", | ||
"standard": "^5.1.0", | ||
"tape": "^4.0.0" | ||
}, | ||
"dependencies": { | ||
"arrify": "^1.0.0", | ||
"commuter": "^1.0.2" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import http from 'http' | ||
|
||
import commuter from 'commuter' | ||
import arrayify from 'arrify' | ||
|
||
export default createFrockInstance | ||
|
||
function createFrockInstance (config = {}) { | ||
const frock = {} | ||
const handlers = new Map() | ||
const servers = [] | ||
|
||
frock.run = run | ||
frock.stop = stop | ||
frock.reload = reload | ||
frock.registerHandler = registerHandler | ||
|
||
return frock | ||
|
||
function run (ready = noop) { | ||
let count = 0 | ||
|
||
config.servers.forEach(s => { | ||
const router = commuter(defaultRoute, s.baseUrl) | ||
const server = http.createServer(router) | ||
const boundHandlers = [] | ||
|
||
s.routes.forEach(r => { | ||
const methods = arrayify(r.methods).map(m => m.toLowerCase()) | ||
|
||
methods.forEach(m => { | ||
const handler = handlers.get(r.handler)( | ||
frock, | ||
logger.bind(null, r.handler), | ||
r.options | ||
) | ||
|
||
router[m](r.path, handler) | ||
boundHandlers.push(handler) | ||
}) | ||
}) | ||
|
||
servers.push({server, handlers: boundHandlers}) | ||
server.listen(s.port, done) | ||
}) | ||
|
||
function done () { | ||
++count | ||
|
||
if (count >= config.servers.length) { | ||
ready() | ||
} | ||
} | ||
} | ||
|
||
function registerHandler (name, handler) { | ||
handlers.set(name, handler) | ||
} | ||
|
||
function reload (ready = noop) { | ||
stop(() => run(ready)) | ||
} | ||
|
||
function stop (ready = noop) { | ||
servers.forEach(s => { | ||
s.handlers.forEach(h => { | ||
h.end(innerDone) | ||
}) | ||
|
||
function innerDone (handler) { | ||
const idx = s.handlers.indexOf(handler) | ||
|
||
if (idx) { | ||
s.handlers.splice(idx, 1) | ||
} else { | ||
throw new Error('No handler to remove, throwing to avoid infinite loop') | ||
} | ||
|
||
if (!s.handlers.length) { | ||
s.server.close(() => done(s)) | ||
} | ||
} | ||
}) | ||
|
||
function done (server) { | ||
const idx = servers.indexOf(server) | ||
|
||
if (idx) { | ||
servers.splice(idx, 1) | ||
} else { | ||
throw new Error('No server to remove, throwing to avoid infinite loop') | ||
} | ||
|
||
if (!servers.length) { | ||
ready() | ||
} | ||
} | ||
} | ||
} | ||
|
||
function defaultRoute (req, res) { | ||
res.statusCode = 404 | ||
res.end('not found') | ||
} | ||
|
||
function logger (handler, level, msg, extra) { | ||
console.log(`${handler}: [${level.toUpperCase()}] ${msg}`) | ||
} | ||
|
||
function noop () { | ||
// nooperations | ||
} |