Repository: nodejs/node
File: lib/internal/http2/core.js (JavaScript)

Code before:
    socket.destroy(err);
}

/**
 * This function closes all active sessions gracefully.
 * @param {*} server the underlying server whose sessions to be closed
 */
function closeAllSessions(server) {
  const sessions = server[kSessions];
  if (sessions.size > 0) {
    for (const session of sessions) {
      session.close();
    }
  }
}

class Http2SecureServer extends TLSServer {
  constructor(options, requestListener) {
    options = initializeTLSOptions(options);
    super(options, connectionListener);
    this[kOptions] = options;
    this[kSessions] = new SafeSet();
    this.timeout = 0;
    this.on('newListener', setupCompat);
    if (options.allowHTTP1 === true) {
      storeHTTPOptions.call(this, options);
      this.shouldUpgradeCallback = function() {
        return this.listenerCount('upgrade') > 0;
      };
      this.on('listening', setupConnectionsTracking);
    }
    if (typeof requestListener === 'function')
      this.on('request', requestListener);
    this.on('tlsClientError', onErrorSecureServerSession);
  }

  setTimeout(msecs, callback) {
    this.timeout = msecs;
    if (callback !== undefined) {
      validateFunction(callback, 'callback');
      this.on('timeout', callback);
    }
    return this;
  }

  updateSettings(settings) {
    assertIsObject(settings, 'settings');
    validateSettings(settings);
    this[kOptions].settings = { ...this[kOptions].settings, ...settings };
  }


Review comment (suggestion):
I think this does need to use a sub option. You mentioned:

> Regarding the dedicated http1Options bag, i agree it's a great idea for the long term to avoid namespace collisions, but I think it might be out of scope for this specific fix.

Unfortunately, in a project like node, we can't merge external APIs and change them later. If we add support for options at the top level, they have to stay there forever (more or less) or we'll break people's code that uses this. Backward compatibility is a challenge! We have to commit to a specific options approach now.

If we change this to:


```suggestion
      storeHTTPOptions.call(this, options.http1Options);
```

Then I think everything will work nicely.

We should then also:

* Update the docs to list this new `http1Options` option and link to the HTTP/1 method where the options are defined.
* Change the test below to set one of the HTTP/1 options explicitly there, to confirm we can override the defaults as expected.
* Change the two `Http1IncomingMessage/ServerResponse` options to `options.http1.IncomingMessage/ServerResponse` instead, and make sure that works. This might require a change elsewhere, since that logic must exist already but isn't here.
* Mark those two options as deprecated in the docs, with a note to use `http1Options` instead.

Code after:
  }

  if (servername !== undefined && !options.servername)
    options.servername = servername;
  return options;
}

function onErrorSecureServerSession(err, socket) {
  if (!this.emit('clientError', err, socket))
    socket.destroy(err);
}

/**
 * This function closes all active sessions gracefully.
 * @param {*} server the underlying server whose sessions to be closed
 */
function closeAllSessions(server) {
  const sessions = server[kSessions];
  if (sessions.size > 0) {
    for (const session of sessions) {
      session.close();
    }
  }
}

class Http2SecureServer extends TLSServer {
  constructor(options, requestListener) {
    options = initializeTLSOptions(options);
    super(options, connectionListener);
    this[kOptions] = options;
    this[kSessions] = new SafeSet();
    this.timeout = 0;
    this.on('newListener', setupCompat);
    if (options.allowHTTP1 === true) {
      storeHTTPOptions.call(this, { ...options, ...options.http1Options });
      this.shouldUpgradeCallback = function() {
        return this.listenerCount('upgrade') > 0;
      };
      this.on('listening', setupConnectionsTracking);
    }
    if (typeof requestListener === 'function')
      this.on('request', requestListener);
    this.on('tlsClientError', onErrorSecureServerSession);
  }

  setTimeout(msecs, callback) {
    this.timeout = msecs;
    if (callback !== undefined) {
      validateFunction(callback, 'callback');
      this.on('timeout', callback);
    }