You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
3.0 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. const Lws = require('lws')
  2. const path = require('path')
  3. /**
  4. * @module local-web-server
  5. * @emits module:local-web-server#verbose
  6. * @example
  7. * const LocalWebServer = require('local-web-server')
  8. * const localWebServer = new LocalWebServer()
  9. * const server = localWebServer.listen({
  10. * port: 8050,
  11. * https: true,
  12. * directory: 'src',
  13. * spa: 'index.html',
  14. * websocket: 'src/websocket-server.js'
  15. * })
  16. * // secure, SPA server with listening websocket now ready on port 8050
  17. */
  18. /**
  19. * @alias module:local-web-server
  20. */
  21. class LocalWebServer extends Lws {
  22. /**
  23. * Returns a listening HTTP/HTTPS server.
  24. * @param [options] {object} - Server options
  25. * @param [options.port] {number} - Port
  26. * @param [options.hostname] {string} -The hostname (or IP address) to listen on. Defaults to 0.0.0.0.
  27. * @param [options.maxConnections] {number} - The maximum number of concurrent connections supported by the server.
  28. * @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.
  29. * @param [options.configFile] {string} - Config file path, defaults to 'lws.config.js'.
  30. * @param [options.https] {boolean} - Enable HTTPS using a built-in key and cert registered to the domain 127.0.0.1.
  31. * @param [options.key] {string} - SSL key file path. Supply along with --cert to launch a https server.
  32. * @param [options.cert] {string} - SSL cert file path. Supply along with --key to launch a https server.
  33. * @param [options.pfx] {string} - Path to an PFX or PKCS12 encoded private key and certificate chain. An alternative to providing --key and --cert.
  34. * @param [options.ciphers] {string} - Optional cipher suite specification, replacing the default.
  35. * @param [options.secureProtocol] {string} - Optional SSL method to use, default is "SSLv23_method".
  36. * @param [options.stack] {string[]|Middlewares[]} - Array of feature classes, or filenames of modules exporting a feature class.
  37. * @param [options.server] {string|ServerFactory} - Custom server factory, e.g. lws-http2.
  38. * @param [options.websocket] {string|Websocket} - Path to a websocket module
  39. * @param [options.moduleDir] {string[]} - One or more directories to search for modules.
  40. * @returns {Server}
  41. */
  42. listen (options) {
  43. const usage = require('lws/lib/usage')
  44. usage.defaults
  45. .set('an', 'ws')
  46. .set('av', require('../package').version)
  47. .set('cd4', 'api')
  48. options = Object.assign({
  49. moduleDir: path.resolve(__dirname, `../node_modules`),
  50. modulePrefix: 'lws-',
  51. stack: require('./default-stack')
  52. }, options)
  53. return super.listen(options)
  54. /**
  55. * Highly-verbose debug information event stream.
  56. *
  57. * @event module:local-web-server#verbose
  58. * @param key {string} - An identifying string, e.g. `server.socket.data`.
  59. * @param value {*} - The value, e.g. `{ socketId: 1, bytesRead: '3 Kb' }`.
  60. */
  61. }
  62. }
  63. module.exports = LocalWebServer