net#ListenOptions TypeScript Examples

The following examples show how to use net#ListenOptions. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DevServer.ts    From web with MIT License 6 votes vote down vote up
async start() {
    this.started = true;
    await (promisify<ListenOptions>(this.server.listen).bind(this.server) as any)({
      port: this.config.port,
      // in case of localhost the host should be undefined, otherwise some browsers
      // connect to it via local network. for example safari on browserstack
      host: ['localhost', '127.0.0.1'].includes(this.config.hostname)
        ? undefined
        : this.config.hostname,
    });

    for (const plugin of this.config.plugins ?? []) {
      await plugin.serverStart?.({
        config: this.config,
        app: this.koaApp,
        server: this.server,
        logger: this.logger,
        webSockets: this.webSockets,
        fileWatcher: this.fileWatcher,
      });
    }
  }
Example #2
Source File: classes.ts    From epicgames-freegames-node with MIT License 5 votes vote down vote up
/**
   * Node Net.listen options: https://nodejs.org/api/net.html#net_server_listen_options_callback
   * @default { port: 3000 }
   * @env SERVER_PORT (for `{port: SERVER_PORT}` only)
   */
  @IsOptional()
  @IsObject()
  listenOpts?: ListenOptions;
Example #3
Source File: ExpressReceiver.d.ts    From flect-chime-sdk-demo with Apache License 2.0 5 votes vote down vote up
start(portOrListenOptions: number | ListenOptions, serverOptions?: ServerOptions): Promise<Server>;
Example #4
Source File: ExpressReceiver.d.ts    From flect-chime-sdk-demo with Apache License 2.0 5 votes vote down vote up
start(portOrListenOptions: number | ListenOptions, httpsServerOptions?: HTTPSServerOptions): Promise<HTTPSServer>;
Example #5
Source File: HTTPReceiver.d.ts    From flect-chime-sdk-demo with Apache License 2.0 5 votes vote down vote up
start(portOrListenOptions: number | ListenOptions, serverOptions?: ServerOptions): Promise<Server>;
Example #6
Source File: HTTPReceiver.d.ts    From flect-chime-sdk-demo with Apache License 2.0 5 votes vote down vote up
start(portOrListenOptions: number | ListenOptions, httpsServerOptions?: HTTPSServerOptions): Promise<HTTPSServer>;
Example #7
Source File: createServer.ts    From web with MIT License 4 votes vote down vote up
/**
 * Creates a koa server with middlewares, but does not start it. Returns the koa app and
 * http server instances.
 */
export function createServer(logger: Logger, cfg: DevServerCoreConfig, fileWatcher: FSWatcher) {
  const app = new Koa();
  app.silent = true;
  app.on('error', error => {
    if (['EPIPE', 'ECONNRESET', 'ERR_STREAM_PREMATURE_CLOSE'].includes(error.code)) {
      return;
    }

    console.error('Error while handling server request.');
    console.error(error);
  });
  addPlugins(logger, cfg);

  // special case the legacy plugin, if it is given make sure the resolve module imports plugin
  // runs before the legacy plugin because it compiles away module syntax. ideally we have a
  // generic API for this, but we need to design that a bit more first
  const indexOfLegacy = cfg.plugins!.findIndex(p => p.name === 'legacy');
  let indexOfResolve = cfg.plugins!.findIndex(p => p.name === 'resolve-module-imports');
  if (indexOfLegacy !== -1 && indexOfResolve !== -1) {
    const legacy = cfg.plugins!.splice(indexOfLegacy, 1)[0];
    // recompute after splicing
    indexOfResolve = cfg.plugins!.findIndex(p => p.name === 'resolve-module-imports');
    cfg.plugins!.splice(indexOfResolve, 1, cfg.plugins![indexOfResolve], legacy);
  }

  const middleware = createMiddleware(cfg, logger, fileWatcher);
  for (const m of middleware) {
    app.use(m);
  }

  let server: Server;
  if (cfg.http2) {
    const dir = path.join(__dirname, '..');
    const options = {
      key: fs.readFileSync(
        cfg.sslKey
          ? path.resolve(cfg.sslKey)
          : path.join(dir, '..', '.self-signed-dev-server-ssl.key'),
      ),
      cert: fs.readFileSync(
        cfg.sslCert
          ? path.resolve(cfg.sslCert)
          : path.join(dir, '..', '.self-signed-dev-server-ssl.cert'),
      ),
      allowHTTP1: true,
    };

    const httpsRedirectServer = httpServer.createServer(httpsRedirect);
    server = http2Server.createSecureServer(options, app.callback());
    let appServerPort: number;
    let httpsRedirectServerPort: number;

    /**
     * A connection handler that checks if the connection is using TLS
     */
    const httpRedirectProxy = (socket: Socket) => {
      socket.once('data', buffer => {
        // A TLS handshake record starts with byte 22.
        const address = buffer[0] === 22 ? appServerPort : httpsRedirectServerPort;
        const proxy = (net as any).createConnection(address, () => {
          proxy.write(buffer);
          socket.pipe(proxy).pipe(socket);
        });
      });
    };

    const wrapperServer = net.createServer(httpRedirectProxy);

    wrapperServer.addListener('close', () => {
      httpsRedirectServer.close();
      server.close();
    });

    wrapperServer.addListener('listening', () => {
      const info = server.address();
      if (!info || typeof info === 'string') {
        return;
      }
      const { address, port } = info;
      appServerPort = port + 1;
      httpsRedirectServerPort = port + 2;

      server.listen({ address, port: appServerPort });
      httpsRedirectServer.listen({ address, port: httpsRedirectServerPort });
    });

    const serverListen = wrapperServer.listen.bind(wrapperServer);
    (wrapperServer as any).listen = (config: ListenOptions, callback: () => void) => {
      server.addListener('listening', callback);
      serverListen(config);
      return server;
    };
  } else {
    server = httpServer.createServer(app.callback());
  }

  return {
    server,
    app,
  };
}