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.

62 lines
2.8 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 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. options = Object.assign({
  44. moduleDir: path.resolve(__dirname, `./node_modules`),
  45. modulePrefix: 'lws-',
  46. stack: require('./lib/default-stack')
  47. }, options)
  48. return super.listen(options)
  49. /**
  50. * Highly-verbose debug information event stream.
  51. *
  52. * @event module:local-web-server#verbose
  53. * @param key {string} - An identifying string, e.g. `server.socket.data`.
  54. * @param value {*} - The value, e.g. `{ socketId: 1, bytesRead: '3 Kb' }`.
  55. */
  56. }
  57. }
  58. module.exports = LocalWebServer