-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support hooking process.getBuiltinModule (#92)
Also, run CI tests with Node 22, drop v19 testing
- Loading branch information
Showing
4 changed files
with
111 additions
and
6 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
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,65 @@ | ||
'use strict' | ||
|
||
const test = require('tape') | ||
|
||
const { Hook } = require('../') | ||
|
||
// The `process.getBuiltinModule(id) function was added in Node.js 22.3.0 | ||
const skip = !process.getBuiltinModule | ||
|
||
test('process.getBuiltinModule should be patched', { skip }, function (t) { | ||
let numOnRequireCalls = 0 | ||
|
||
const hook = new Hook(['http'], function (exports, name, basedir) { | ||
numOnRequireCalls++ | ||
return exports | ||
}) | ||
|
||
const a = process.getBuiltinModule('http') | ||
t.equal(numOnRequireCalls, 1) | ||
|
||
const b = require('http') | ||
t.equal(numOnRequireCalls, 1) | ||
|
||
t.strictEqual(a, b, 'modules are the same') | ||
|
||
t.end() | ||
hook.unhook() | ||
}) | ||
|
||
test('patched process.getBuiltinModule should work with node: prefix', { skip }, function (t) { | ||
let numOnRequireCalls = 0 | ||
|
||
const hook = new Hook(['http'], function (exports, name, basedir) { | ||
numOnRequireCalls++ | ||
return exports | ||
}) | ||
|
||
process.getBuiltinModule('node:http') | ||
t.equal(numOnRequireCalls, 1) | ||
t.end() | ||
hook.unhook() | ||
}) | ||
|
||
test('patched process.getBuiltinModule should preserve default behavior for non-builtin modules', { skip }, function (t) { | ||
const beforePatching = process.getBuiltinModule('ipp-printer') | ||
|
||
const hook = new Hook(['ipp-printer'], function (exports, name, basedir) { | ||
t.fail('should not call hook') | ||
}) | ||
|
||
const afterPatching = process.getBuiltinModule('ipp-printer') | ||
|
||
t.strictEqual(beforePatching, afterPatching, 'modules are the same') | ||
t.end() | ||
hook.unhook() | ||
}) | ||
|
||
test('hook.unhook() works for process.getBuiltinModule', { skip }, function (t) { | ||
const hook = new Hook(['http'], function (exports, name, basedir) { | ||
t.fail('should not call onrequire') | ||
}) | ||
hook.unhook() | ||
process.getBuiltinModule('http') | ||
t.end() | ||
}) |