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.

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