-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
child_process: allow 'http_parser' monkey patching again
Lazy load _http_common and HTTPParser so that the 'http_parser' binding can be monkey patched before any internal modules require it. This also probably improves startup performance minimally for programs that never require the HTTP stack. Fixes: #23716 Fixes: creationix/http-parser-js#57 PR-URL: #24006 Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
- Loading branch information
1 parent
c37b319
commit 04f8d6b
Showing
2 changed files
with
48 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Flags: --expose-internals | ||
|
||
'use strict'; | ||
|
||
const { internalBinding } = require('internal/test/binding'); | ||
|
||
// Monkey patch before requiring anything | ||
class DummyParser { | ||
constructor(type) { | ||
this.test_type = type; | ||
} | ||
} | ||
DummyParser.REQUEST = Symbol(); | ||
internalBinding('http_parser').HTTPParser = DummyParser; | ||
|
||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const { spawn } = require('child_process'); | ||
const { parsers } = require('_http_common'); | ||
|
||
// Test _http_common was not loaded before monkey patching | ||
const parser = parsers.alloc(); | ||
assert.strictEqual(parser instanceof DummyParser, true); | ||
assert.strictEqual(parser.test_type, DummyParser.REQUEST); | ||
|
||
if (process.argv[2] !== 'child') { | ||
// Also test in a child process with IPC (specific case of https://github.com/nodejs/node/issues/23716) | ||
const child = spawn(process.execPath, [ | ||
'--expose-internals', __filename, 'child' | ||
], { | ||
stdio: ['inherit', 'inherit', 'inherit', 'ipc'] | ||
}); | ||
child.on('exit', common.mustCall((code, signal) => { | ||
assert.strictEqual(code, 0); | ||
assert.strictEqual(signal, null); | ||
})); | ||
} |