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.

58 lines
2.7 KiB

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