diff options
-rw-r--r-- | doc/api/http2.md | 26 | ||||
-rw-r--r-- | lib/internal/http2/core.js | 12 | ||||
-rw-r--r-- | test/parallel/test-http2-update-settings.js | 59 |
3 files changed, 97 insertions, 0 deletions
diff --git a/doc/api/http2.md b/doc/api/http2.md index bc3bebd11b9..c109d7db72a 100644 --- a/doc/api/http2.md +++ b/doc/api/http2.md @@ -1879,6 +1879,19 @@ A value of `0` will disable the timeout behavior on incoming connections. The socket timeout logic is set up on connection, so changing this value only affects new connections to the server, not any existing connections. +#### `server.updateSettings([settings])` +<!-- YAML +added: REPLACEME +--> + +* `settings` {HTTP/2 Settings Object} + +Used to update the server with the provided settings. + +Throws `ERR_HTTP2_INVALID_SETTING_VALUE` for invalid `settings` values. + +Throws `ERR_INVALID_ARG_TYPE` for invalid `settings` argument. + ### Class: `Http2SecureServer` <!-- YAML added: v8.4.0 @@ -2058,6 +2071,19 @@ A value of `0` will disable the timeout behavior on incoming connections. The socket timeout logic is set up on connection, so changing this value only affects new connections to the server, not any existing connections. +#### `server.updateSettings([settings])` +<!-- YAML +added: REPLACEME +--> + +* `settings` {HTTP/2 Settings Object} + +Used to update the server with the provided settings. + +Throws `ERR_HTTP2_INVALID_SETTING_VALUE` for invalid `settings` values. + +Throws `ERR_INVALID_ARG_TYPE` for invalid `settings` argument. + ### `http2.createServer(options[, onRequestHandler])` <!-- YAML added: v8.4.0 diff --git a/lib/internal/http2/core.js b/lib/internal/http2/core.js index ab961c05918..61b8466d6f5 100644 --- a/lib/internal/http2/core.js +++ b/lib/internal/http2/core.js @@ -2992,6 +2992,12 @@ class Http2SecureServer extends TLSServer { } return this; } + + updateSettings(settings) { + assertIsObject(settings, 'settings'); + validateSettings(settings); + this[kOptions].settings = { ...this[kOptions].settings, ...settings }; + } } class Http2Server extends NETServer { @@ -3014,6 +3020,12 @@ class Http2Server extends NETServer { } return this; } + + updateSettings(settings) { + assertIsObject(settings, 'settings'); + validateSettings(settings); + this[kOptions].settings = { ...this[kOptions].settings, ...settings }; + } } Http2Server.prototype[EventEmitter.captureRejectionSymbol] = function( diff --git a/test/parallel/test-http2-update-settings.js b/test/parallel/test-http2-update-settings.js new file mode 100644 index 00000000000..e99ee2bcf15 --- /dev/null +++ b/test/parallel/test-http2-update-settings.js @@ -0,0 +1,59 @@ +'use strict'; + +// This test ensures that the Http2SecureServer and Http2Server +// settings are updated when the setting object is valid. +// When the setting object is invalid, this test ensures that +// updateSettings throws an exception. + +const common = require('../common'); +if (!common.hasCrypto) { common.skip('missing crypto'); } +const assert = require('assert'); +const http2 = require('http2'); + +testUpdateSettingsWith({ + server: http2.createSecureServer(), + newServerSettings: { + 'headerTableSize': 1, + 'initialWindowSize': 1, + 'maxConcurrentStreams': 1, + 'maxHeaderListSize': 1, + 'maxFrameSize': 16385, + 'enablePush': false, + 'enableConnectProtocol': true + } +}); +testUpdateSettingsWith({ + server: http2.createServer(), + newServerSettings: { + 'enablePush': false + } +}); + +function testUpdateSettingsWith({ server, newServerSettings }) { + const oldServerSettings = getServerSettings(server); + assert.notDeepStrictEqual(oldServerSettings, newServerSettings); + server.updateSettings(newServerSettings); + const updatedServerSettings = getServerSettings(server); + assert.deepStrictEqual(updatedServerSettings, { ...oldServerSettings, + ...newServerSettings }); + assert.throws(() => server.updateSettings(''), { + message: 'The "settings" argument must be of type object. ' + + 'Received type string (\'\')', + code: 'ERR_INVALID_ARG_TYPE', + name: 'TypeError' + }); + assert.throws(() => server.updateSettings({ + 'maxHeaderListSize': 'foo' + }), { + message: 'Invalid value for setting "maxHeaderListSize": foo', + code: 'ERR_HTTP2_INVALID_SETTING_VALUE', + name: 'RangeError' + }); +} + +function getServerSettings(server) { + const options = Object + .getOwnPropertySymbols(server) + .find((s) => s.toString() === 'Symbol(options)'); + return server[options].settings; +} |