Comparing version 2.0.0-8 to 2.0.0-9
95
index.js
@@ -15,20 +15,3 @@ const util = require('./lib/util') | ||
/** | ||
* Returns a listening HTTP/HTTPS/HTTP2 server. | ||
* @param [options] {object} - Server options | ||
* @param [options.port] {number} - Port | ||
* @param [options.hostname] {string} -The hostname (or IP address) to listen on. Defaults to 0.0.0.0. | ||
* @param [options.maxConnections] {number} - The maximum number of concurrent connections supported by the server. | ||
* @param [options.keepAliveTimeout] {number} - The period (in milliseconds) of inactivity a connection will remain open before being destroyed. Set to `0` to keep connections open indefinitely. | ||
* @param [options.configFile] {string} - Config file path, defaults to 'lws.config.js'. | ||
* @param [options.https] {boolean} - Enable HTTPS using a built-in key and cert registered to the domain 127.0.0.1. | ||
* @param [options.key] {string} - SSL key file path. Supply along with --cert to launch a https server. | ||
* @param [options.cert] {string} - SSL cert file path. Supply along with --key to launch a https server. | ||
* @param [options.pfx] {string} - Path to an PFX or PKCS12 encoded private key and certificate chain. An alternative to providing --key and --cert. | ||
* @param [options.ciphers] {string} - Optional cipher suite specification, replacing the default. | ||
* @param [options.secureProtocol] {string} - Optional SSL method to use, default is "SSLv23_method". | ||
* @param [options.stack] {string[]|Middlewares[]} - Array of middleware classes, or filenames of modules exporting a middleware class. | ||
* @param [options.moduleDir] {string[]} - One or more directories to search for middleware modules. | ||
* @param [options.modulePrefix] {string} - An optional string to prefix to module names when loading middleware modules Defaults to 'lws-'. | ||
* @param [options.view] {object} - View instance. | ||
* @returns {Server} | ||
* @param {LwsConfig} - Server config. | ||
*/ | ||
@@ -51,7 +34,7 @@ constructor (config) { | ||
* Active config. | ||
* @type {object} | ||
* @type {LwsConfig} | ||
*/ | ||
this.config = null | ||
this.setConfig(config) | ||
this._setConfig(config) | ||
} | ||
@@ -62,4 +45,5 @@ | ||
* @returns {object} | ||
* @ignore | ||
*/ | ||
getDefaultConfig () { | ||
_getDefaultConfig () { | ||
return { | ||
@@ -76,6 +60,7 @@ port: 8000, | ||
* @returns {object} | ||
* @ignore | ||
*/ | ||
setConfig (config = {}) { | ||
_setConfig (config = {}) { | ||
this.config = util.deepMerge( | ||
this.getDefaultConfig(), | ||
this._getDefaultConfig(), | ||
util.getStoredConfig(config.configFile), | ||
@@ -88,4 +73,5 @@ config | ||
* Sets the middleware stack, loading plugins if supplied. | ||
* @ignore | ||
*/ | ||
setStack () { | ||
_setStack () { | ||
const arrayify = require('array-back') | ||
@@ -140,9 +126,8 @@ const Stack = require('./lib/middleware-stack') | ||
* Attach the Middleware stack to the server. | ||
* @param [options] {object} - Arbitrary options to be passed into the middleware functions. | ||
*/ | ||
useMiddlewareStack () { | ||
if (!this.server) throw new Error('Create server first') | ||
this.setStack() | ||
const middlewares = this.stack.getMiddlewareFunctions(this.config) | ||
this.server.on('request', this.getRequestHandler(middlewares)) | ||
this._setStack() | ||
const middlewares = this.stack.getMiddlewareFunctions(this.config, this) | ||
this.server.on('request', this._getRequestHandler(middlewares)) | ||
} | ||
@@ -154,4 +139,5 @@ | ||
* @returns {function} | ||
* @ignore | ||
*/ | ||
getRequestHandler (middlewares = []) { | ||
_getRequestHandler (middlewares = []) { | ||
/* build Koa application using the supplied middleware */ | ||
@@ -162,2 +148,9 @@ const Koa = require('koa') | ||
app.on('error', err => { | ||
/** | ||
* Highly-verbose debug information event stream. | ||
* | ||
* @event module:lws#verbose | ||
* @param key {string} - An identifying string, e.g. `server.socket.data`. | ||
* @param value {*} - The value, e.g. `{ socketId: 1, bytesRead: '3 Kb' }`. | ||
*/ | ||
this.emit('verbose', 'middleware.error', err) | ||
@@ -174,8 +167,2 @@ }) | ||
_propagateServerEvents () { | ||
const write = (name, value) => { | ||
return () => { | ||
this.emit('verbose', name, value) | ||
} | ||
} | ||
function socketProperties (socket) { | ||
@@ -194,27 +181,31 @@ const byteSize = require('byte-size') | ||
const server = this.server | ||
server.on('connection', (socket) => { | ||
server.on('connection', socket => { | ||
socket.id = cId++ | ||
write('server.socket.new', socketProperties(socket))() | ||
socket.on('connect', write('server.socket.connect', socketProperties(socket, cId))) | ||
socket.on('data', function () { | ||
write('server.socket.data', socketProperties(this))() | ||
this.emit('verbose', 'server.socket.new', socketProperties(socket)) | ||
socket.on('connect', () => { | ||
this.emit('verbose', 'server.socket.connect', socketProperties(socket)) | ||
}) | ||
socket.on('drain', function () { | ||
write('server.socket.drain', socketProperties(this))() | ||
socket.on('data', () => { | ||
this.emit('verbose', 'server.socket.data', socketProperties(socket)) | ||
}) | ||
socket.on('timeout', function () { | ||
write('server.socket.timeout', socketProperties(this))() | ||
socket.on('drain', () => { | ||
this.emit('verbose', 'server.socket.drain', socketProperties(socket)) | ||
}) | ||
socket.on('close', function () { | ||
write('server.socket.close', socketProperties(this))() | ||
socket.on('timeout', () => { | ||
this.emit('verbose', 'server.socket.timeout', socketProperties(socket)) | ||
}) | ||
socket.on('close', () => { | ||
this.emit('verbose', 'server.socket.close', socketProperties(socket)) | ||
}) | ||
socket.on('end', () => { | ||
this.emit('verbose', 'server.socket.end', socketProperties(socket)) | ||
}) | ||
socket.on('error', function (err) { | ||
write('server.socket.error', { err })() | ||
this.emit('verbose', 'server.socket.error', { err }) | ||
}) | ||
socket.on('end', write('server.socket.end', socketProperties(socket, cId))) | ||
socket.on('lookup', write('server.socket.connect', socketProperties(socket, cId))) | ||
}) | ||
/* stream server events */ | ||
server.on('close', write('server.close')) | ||
server.on('close', () => { | ||
this.emit('verbose', 'server.close') | ||
}) | ||
@@ -236,3 +227,3 @@ let requestId = 1 | ||
} | ||
write('server.listening', ipList)() | ||
this.emit('verbose', 'server.listening', ipList) | ||
}) | ||
@@ -239,0 +230,0 @@ |
class LwsCli { | ||
constructor (options) { | ||
options = options || {} | ||
constructor (options = {}) { | ||
this.logError = options.logError || console.error | ||
@@ -37,3 +36,3 @@ this.log = options.log || console.log | ||
header: 'lws', | ||
content: 'The modular web server for productive full-stack development.' | ||
content: 'Personalised web server for productive full-stack development.' | ||
}, | ||
@@ -199,14 +198,13 @@ { | ||
showConfig (options) { | ||
options = Object.assign({}, options) | ||
delete options.config | ||
delete options.moduleDir | ||
delete options.modulePrefix | ||
showConfig (config) { | ||
config = Object.assign({}, config) | ||
delete config.config | ||
delete config._unknown | ||
const util = require('util') | ||
for (const middleware of options.stack) { | ||
middleware[util.inspect.custom] = function (depth, options) { | ||
return options.stylize(this.constructor.name, 'special') | ||
for (const middleware of config.stack) { | ||
middleware[util.inspect.custom] = function (depth, config) { | ||
return config.stylize(this.constructor.name, 'special') | ||
} | ||
} | ||
this.log(require('util').inspect(options, { depth: 1, colors: true })) | ||
this.log(require('util').inspect(config, { depth: 1, colors: true })) | ||
} | ||
@@ -213,0 +211,0 @@ |
@@ -6,3 +6,3 @@ module.exports = [ | ||
type: Number, | ||
description: 'Web server port.', | ||
description: 'The port number to listen on.', | ||
section: 'server' | ||
@@ -13,3 +13,3 @@ }, | ||
type: String, | ||
description: 'The hostname (or IP address) to listen on. Defaults to 0.0.0.0 (any host).', | ||
description: 'The hostname or IP address to bind to. Defaults to 0.0.0.0 (any host).', | ||
section: 'server' | ||
@@ -106,3 +106,3 @@ }, | ||
type: String, | ||
description: 'Config filename to use, defaults to "lws.config.js".', | ||
description: 'Filename to retrieve stored config from. Defaults to "lws.config.js".', | ||
typeLabel: '{underline file}', | ||
@@ -109,0 +109,0 @@ section: 'core' |
@@ -40,8 +40,9 @@ /** | ||
* Return one of more Koa middleware functions. Optionally, emit `verbose` events to `ctx.app`. | ||
* @params [options] {object} - A config object. | ||
* @params {object} - The active config object. | ||
* @params {Lws} - The active `lws` instance. | ||
* @returns {function|function[]} | ||
*/ | ||
middleware (options) {} | ||
middleware (config, lws) {} | ||
} | ||
module.exports = MiddlewarePlugin |
@@ -15,6 +15,6 @@ const flatten = require('reduce-flatten') | ||
*/ | ||
getMiddlewareFunctions (options) { | ||
getMiddlewareFunctions (config, lws) { | ||
return this | ||
.filter(mw => mw.middleware) | ||
.map(mw => mw.middleware(Object.assign({}, options))) | ||
.map(mw => mw.middleware(Object.assign({}, config), lws)) | ||
.reduce(flatten, []) | ||
@@ -21,0 +21,0 @@ .filter(mw => mw) |
@@ -23,3 +23,5 @@ const path = require('path') | ||
} | ||
this.emit('verbose', 'server.config', serverOptions) | ||
if (Object.keys(serverOptions).length) { | ||
this.emit('verbose', 'server.config', serverOptions) | ||
} | ||
return server | ||
@@ -26,0 +28,0 @@ } |
@@ -26,3 +26,5 @@ const nodeVersionMatches = require('node-version-matches') | ||
if (t.isDefined(options.keepAliveTimeout)) throw new Error('--keep-alive-timeout has no effect with http2') | ||
this.emit('verbose', 'server.config', serverOptions) | ||
if (Object.keys(serverOptions).length) { | ||
this.emit('verbose', 'server.config', serverOptions) | ||
} | ||
return http2.createSecureServer(serverOptions) | ||
@@ -29,0 +31,0 @@ } |
@@ -27,3 +27,5 @@ const HttpFactory = require('./http') | ||
} | ||
this.emit('verbose', 'server.config', serverOptions) | ||
if (Object.keys(serverOptions).length) { | ||
this.emit('verbose', 'server.config', serverOptions) | ||
} | ||
return server | ||
@@ -30,0 +32,0 @@ } |
@@ -36,3 +36,3 @@ class CliView { | ||
.join(', ') | ||
this.logError(`Serving at ${ipList}`) | ||
this.logError(`Listening at ${ipList}`) | ||
} | ||
@@ -39,0 +39,0 @@ |
{ | ||
"name": "lws", | ||
"author": "Lloyd Brookes <75pound@gmail.com>", | ||
"version": "2.0.0-8", | ||
"version": "2.0.0-9", | ||
"description": "The modular web server for productive full-stack development", | ||
@@ -11,3 +11,10 @@ "repository": "https://github.com/lwsjs/lws.git", | ||
}, | ||
"keywords": [], | ||
"keywords": [ | ||
"lws", | ||
"server", | ||
"http", | ||
"http2", | ||
"https", | ||
"backend" | ||
], | ||
"engines": { | ||
@@ -18,4 +25,4 @@ "node": ">=8" | ||
"test": "test-runner test/*.js", | ||
"cover": "nyc npm test && nyc report --reporter=text-lcov | coveralls", | ||
"docs": "jsdoc2md lib/middleware-plugin.js > doc/middleware-plugin.md && jsdoc2md index.js > doc/lws.md && jsdoc2md lib/view/view-plugin.js > doc/view-plugin.md" | ||
"cover": "TESTOPEN=true nyc -r html -r text npm test && nyc report --reporter=text-lcov | coveralls", | ||
"docs": "jsdoc2md lib/middleware-plugin.js > doc/middleware-plugin.md && jsdoc2md index.js > doc/lws.md && jsdoc2md lib/view/view-plugin.js > doc/view-plugin.md && jsdoc2md lib/config.js > doc/config.md" | ||
}, | ||
@@ -34,2 +41,3 @@ "files": [ | ||
"command-line-usage": "^5.0.5", | ||
"create-mixin": "^2.0.1", | ||
"koa": "^2.7.0", | ||
@@ -36,0 +44,0 @@ "load-module": "^2.0.0", |
@@ -12,4 +12,31 @@ [![view on npm](https://img.shields.io/npm/v/lws.svg)](https://www.npmjs.org/package/lws) | ||
Lws is tool designed for quickly launching a personalised Node.js HTTP, HTTPS or HTTP2 server on the command line. It's intended to facilitate rapid, full-stack Javascript development. | ||
On top of launching a server you can: | ||
* Attach one or more middleware plugins to handle requests in the manner desired. | ||
* Attach a view to visualise activity | ||
* Store config at any level - project, user or system. | ||
## Synopsis | ||
Launch an HTTP server on the default port of 8000. | ||
``` | ||
$ lws | ||
Listening at http://mba4.local:8000, http://127.0.0.1:8000, http://192.168.0.200:8000 | ||
``` | ||
Add some middleware to serve static files and directory listings. | ||
``` | ||
$ npm install --save-dev lws-static lws-index | ||
$ lws --stack lws-static lws-index | ||
Listening at http://mba4.local:8000, http://127.0.0.1:8000, http://192.168.0.200:8000 | ||
``` | ||
The file system from the current directory will now be available to explore at http://127.0.0.1:8000. | ||
* * * | ||
© 2016-19 Lloyd Brookes \<75pound@gmail.com\>. Documented by [jsdoc-to-markdown](https://github.com/jsdoc2md/jsdoc-to-markdown). |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
92950
21
1027
42
14
+ Addedcreate-mixin@^2.0.1
+ Addedcreate-mixin@2.0.1(transitive)