GeezerVison is showing github.com — http: runtime deprecate instantiating without new · nodejs/node@25c0c8e
File treeExpand file treeCollapse file tree Open diff view settings Some content is hiddenLarge Commits have some content hidden by default. Use the searchbox below for content that may be hidden. Dismiss bannerExpand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: doc/api/deprecations.md +5-2Lines changed: 5 additions & 2 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -4341,18 +4341,21 @@ npx codemod@latest @nodejs/http2-priority-signaling | 4341 | 4341 |
| 4342 | 4342 | <!-- YAML | 4343 | 4343 | changes: | | 4344 | + - version: REPLACEME | | 4345 | + pr-url: https://github.com/nodejs/node/pull/64853 | | 4346 | + description: Runtime deprecation. | 4344 | 4347 | - version: | 4345 | 4348 | - v24.2.0 | 4346 | 4349 | - v22.17.0 | 4347 | 4350 | pr-url: https://github.com/nodejs/node/pull/58518 | 4348 | 4351 | description: Documentation-only deprecation. | 4349 | 4352 | --> | 4350 | 4353 |
| 4351 | | -Type: Documentation-only | | 4354 | +Type: Runtime | 4352 | 4355 |
| 4353 | 4356 | Instantiating classes without the `new` qualifier exported by the `node:http` module is deprecated. | 4354 | 4357 | It is recommended to use the `new` qualifier instead. This applies to all http classes, such as | 4355 | | -`OutgoingMessage`, `IncomingMessage`, `ServerResponse` and `ClientRequest`. | | 4358 | +`OutgoingMessage`, `IncomingMessage`, `ServerResponse`, `ClientRequest`, `Server`, and `Agent`. | 4356 | 4359 |
| 4357 | 4360 | An automated migration is available ([source](https://github.com/nodejs/userland-migrations/tree/main/recipes/http-classes-with-new)): | 4358 | 4361 |
| |
Collapse file Copy file name to clipboardExpand all lines: lib/_http_agent.js +4-2Lines changed: 4 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -48,6 +48,7 @@ const { | 48 | 48 | owner_symbol, | 49 | 49 | } = require('internal/async_hooks').symbols; | 50 | 50 | const { | | 51 | + deprecateInstantiation, | 51 | 52 | getLazy, | 52 | 53 | kEmptyObject, | 53 | 54 | once, | @@ -151,8 +152,9 @@ function removeFreeSocketDataGuard(socket) { | 151 | 152 | } | 152 | 153 |
| 153 | 154 | function Agent(options) { | 154 | | - if (!(this instanceof Agent)) | 155 | | - return new Agent(options); | | 155 | + if (!(this instanceof Agent)) { | | 156 | + return deprecateInstantiation(Agent, 'DEP0195', options); | | 157 | + } | 156 | 158 |
| 157 | 159 | EventEmitter.call(this); | 158 | 160 |
| |
Collapse file Copy file name to clipboardExpand all lines: lib/_http_client.js +5Lines changed: 5 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -38,6 +38,7 @@ const { | 38 | 38 | const net = require('net'); | 39 | 39 | const assert = require('internal/assert'); | 40 | 40 | const { | | 41 | + deprecateInstantiation, | 41 | 42 | kEmptyObject, | 42 | 43 | once, | 43 | 44 | } = require('internal/util'); | @@ -329,6 +330,10 @@ function rewriteForProxiedHttp(req, reqOptions, proxyAuthority, userHostHeader, | 329 | 330 | }; | 330 | 331 |
| 331 | 332 | function ClientRequest(input, options, cb) { | | 333 | + if (!(this instanceof ClientRequest)) { | | 334 | + return deprecateInstantiation(ClientRequest, 'DEP0195', input, options, cb); | | 335 | + } | | 336 | + | 332 | 337 | OutgoingMessage.call(this); | 333 | 338 |
| 334 | 339 | if (typeof input === 'string') { | |
Collapse file Copy file name to clipboardExpand all lines: lib/_http_incoming.js +7Lines changed: 7 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -28,6 +28,9 @@ const { | 28 | 28 | } = primordials; | 29 | 29 |
| 30 | 30 | const { Readable, finished } = require('stream'); | | 31 | +const { | | 32 | + deprecateInstantiation, | | 33 | +} = require('internal/util'); | 31 | 34 |
| 32 | 35 | const { AbortController } = require('internal/abort_controller'); | 33 | 36 |
| @@ -56,6 +59,10 @@ function readStop(socket) { | 56 | 59 |
| 57 | 60 | /* Abstract base class for ServerRequest and ClientResponse. */ | 58 | 61 | function IncomingMessage(socket) { | | 62 | + if (!(this instanceof IncomingMessage)) { | | 63 | + return deprecateInstantiation(IncomingMessage, 'DEP0195', socket); | | 64 | + } | | 65 | + | 59 | 66 | let streamOptions; | 60 | 67 |
| 61 | 68 | if (socket) { | |
Collapse file Copy file name to clipboardExpand all lines: lib/_http_outgoing.js +8-1Lines changed: 8 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -71,7 +71,10 @@ const { | 71 | 71 | hideStackFrames, | 72 | 72 | } = require('internal/errors'); | 73 | 73 | const { validateString } = require('internal/validators'); | 74 | | -const { assignFunctionName } = require('internal/util'); | | 74 | +const { | | 75 | + assignFunctionName, | | 76 | + deprecateInstantiation, | | 77 | +} = require('internal/util'); | 75 | 78 | const { isUint8Array } = require('internal/util/types'); | 76 | 79 |
| 77 | 80 | let debug = require('internal/util/debuglog').debuglog('http', (fn) => { | @@ -104,6 +107,10 @@ function isContentDispositionField(s) { | 104 | 107 | } | 105 | 108 |
| 106 | 109 | function OutgoingMessage(options) { | | 110 | + if (!(this instanceof OutgoingMessage)) { | | 111 | + return deprecateInstantiation(OutgoingMessage, 'DEP0195', options); | | 112 | + } | | 113 | + | 107 | 114 | Stream.call(this); | 108 | 115 |
| 109 | 116 | // Queue that holds all currently pending data, until the response will be | |
Collapse file Copy file name to clipboardExpand all lines: lib/_http_server.js +8-1Lines changed: 8 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -89,6 +89,7 @@ const { | 89 | 89 | } = require('internal/errors'); | 90 | 90 | const { | 91 | 91 | assignFunctionName, | | 92 | + deprecateInstantiation, | 92 | 93 | kEmptyObject, | 93 | 94 | promisify, | 94 | 95 | } = require('internal/util'); | @@ -205,6 +206,10 @@ class HTTPServerAsyncResource { | 205 | 206 | } | 206 | 207 |
| 207 | 208 | function ServerResponse(req, options) { | | 209 | + if (!(this instanceof ServerResponse)) { | | 210 | + return deprecateInstantiation(ServerResponse, 'DEP0195', req, options); | | 211 | + } | | 212 | + | 208 | 213 | OutgoingMessage.call(this, options); | 209 | 214 |
| 210 | 215 | if (req.method === 'HEAD') this._hasBody = false; | @@ -630,7 +635,9 @@ function httpServerPreClose(server) { | 630 | 635 | } | 631 | 636 |
| 632 | 637 | function Server(options, requestListener) { | 633 | | - if (!(this instanceof Server)) return new Server(options, requestListener); | | 638 | + if (!(this instanceof Server)) { | | 639 | + return deprecateInstantiation(Server, 'DEP0195', options, requestListener); | | 640 | + } | 634 | 641 |
| 635 | 642 | if (typeof options === 'function') { | 636 | 643 | requestListener = options; | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-cluster-basic.js +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -50,7 +50,8 @@ function forEach(obj, fn) { | 50 | 50 |
| 51 | 51 |
| 52 | 52 | if (cluster.isWorker) { | 53 | | - require('http').Server(common.mustNotCall()).listen(0, '127.0.0.1'); | | 53 | + const http = require('http'); | | 54 | + new http.Server(common.mustNotCall()).listen(0, '127.0.0.1'); | 54 | 55 | } else if (cluster.isPrimary) { | 55 | 56 |
| 56 | 57 | const checks = { | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-cluster-primary-error.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -29,7 +29,7 @@ const totalWorkers = 2; | 29 | 29 | // Cluster setup | 30 | 30 | if (cluster.isWorker) { | 31 | 31 | const http = require('http'); | 32 | | - http.Server(() => {}).listen(0, '127.0.0.1'); | | 32 | + new http.Server(() => {}).listen(0, '127.0.0.1'); | 33 | 33 | } else if (process.argv[2] === 'cluster') { | 34 | 34 | // Send PID to testcase process | 35 | 35 | let forkNum = 0; | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-cluster-primary-kill.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -28,7 +28,7 @@ if (cluster.isWorker) { | 28 | 28 |
| 29 | 29 | // Keep the worker alive | 30 | 30 | const http = require('http'); | 31 | | - http.Server().listen(0, '127.0.0.1'); | | 31 | + new http.Server().listen(0, '127.0.0.1'); | 32 | 32 |
| 33 | 33 | } else if (process.argv[2] === 'cluster') { | 34 | 34 |
| |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-cluster-rr-domain-listen.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -32,7 +32,7 @@ if (cluster.isWorker) { | 32 | 32 | d.run(() => {}); | 33 | 33 |
| 34 | 34 | const http = require('http'); | 35 | | - http.Server(() => {}).listen(0, '127.0.0.1'); | | 35 | + new http.Server(() => {}).listen(0, '127.0.0.1'); | 36 | 36 |
| 37 | 37 | } else if (cluster.isPrimary) { | 38 | 38 |
| |
|