-
Notifications
You must be signed in to change notification settings - Fork 30.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
module: allow omitting context in synchronous next hooks
This aligns the behavior of synchronous hooks with asynchronous hooks by allowing omission of the context parameter in the invocation of next hooks. The contexts are merged along the chain. PR-URL: #57056 Fixes: #57030 Reviewed-By: Jacob Smith <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]>
- Loading branch information
1 parent
5a74568
commit cc1cafd
Showing
9 changed files
with
211 additions
and
8 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
31 changes: 31 additions & 0 deletions
31
test/module-hooks/test-module-hooks-load-context-merged-esm.mjs
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,31 @@ | ||
// Test that the context parameter will be merged in multiple load hooks. | ||
|
||
import * as common from '../common/index.mjs'; | ||
import assert from 'node:assert'; | ||
import { registerHooks } from 'node:module'; | ||
|
||
const hook1 = registerHooks({ | ||
load: common.mustCall(function(url, context, nextLoad) { | ||
assert.strictEqual(context.testProp, 'custom'); // It should be merged from hook 2 and 3. | ||
return nextLoad(url, context); | ||
}, 1), | ||
}); | ||
|
||
const hook2 = registerHooks({ | ||
load: common.mustCall(function(url, context, nextLoad) { | ||
assert.strictEqual(context.testProp, 'custom'); // It should be merged from hook 3. | ||
return nextLoad(url); // Omit the context. | ||
}, 1), | ||
}); | ||
|
||
const hook3 = registerHooks({ | ||
load: common.mustCall(function(url, context, nextLoad) { | ||
return nextLoad(url, { testProp: 'custom' }); // Add a custom property | ||
}, 1), | ||
}); | ||
|
||
await import('../fixtures/es-modules/message.mjs'); | ||
|
||
hook3.deregister(); | ||
hook2.deregister(); | ||
hook1.deregister(); |
33 changes: 33 additions & 0 deletions
33
test/module-hooks/test-module-hooks-load-context-merged.js
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,33 @@ | ||
'use strict'; | ||
|
||
// Test that the context parameter will be merged in multiple load hooks. | ||
|
||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const { registerHooks } = require('module'); | ||
|
||
const hook1 = registerHooks({ | ||
load: common.mustCall(function(url, context, nextLoad) { | ||
assert.strictEqual(context.testProp, 'custom'); // It should be merged from hook 2 and 3. | ||
return nextLoad(url, context); | ||
}, 1), | ||
}); | ||
|
||
const hook2 = registerHooks({ | ||
load: common.mustCall(function(url, context, nextLoad) { | ||
assert.strictEqual(context.testProp, 'custom'); // It should be merged from hook 3. | ||
return nextLoad(url); // Omit the context. | ||
}, 1), | ||
}); | ||
|
||
const hook3 = registerHooks({ | ||
load: common.mustCall(function(url, context, nextLoad) { | ||
return nextLoad(url, { testProp: 'custom' }); // Add a custom property | ||
}, 1), | ||
}); | ||
|
||
require('../fixtures/empty.js'); | ||
|
||
hook3.deregister(); | ||
hook2.deregister(); | ||
hook1.deregister(); |
14 changes: 14 additions & 0 deletions
14
test/module-hooks/test-module-hooks-load-context-optional-esm.mjs
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,14 @@ | ||
// Test that the context parameter can be omitted in the nextLoad invocation. | ||
|
||
import * as common from '../common/index.mjs'; | ||
import { registerHooks } from 'node:module'; | ||
|
||
const hook = registerHooks({ | ||
load: common.mustCall(function(url, context, nextLoad) { | ||
return nextLoad(url); | ||
}, 1), | ||
}); | ||
|
||
await import('../fixtures/es-modules/message.mjs'); | ||
|
||
hook.deregister(); |
16 changes: 16 additions & 0 deletions
16
test/module-hooks/test-module-hooks-load-context-optional.js
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,16 @@ | ||
'use strict'; | ||
|
||
// Test that the context parameter can be omitted in the nextLoad invocation. | ||
|
||
const common = require('../common'); | ||
const { registerHooks } = require('module'); | ||
|
||
const hook = registerHooks({ | ||
load: common.mustCall(function(url, context, nextLoad) { | ||
return nextLoad(url); | ||
}, 1), | ||
}); | ||
|
||
require('../fixtures/empty.js'); | ||
|
||
hook.deregister(); |
32 changes: 32 additions & 0 deletions
32
test/module-hooks/test-module-hooks-resolve-context-merged-esm.mjs
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,32 @@ | ||
// Test that the context parameter will be merged in multiple resolve hooks. | ||
|
||
import * as common from '../common/index.mjs'; | ||
import assert from 'node:assert'; | ||
import { registerHooks } from 'node:module'; | ||
|
||
const hook1 = registerHooks({ | ||
resolve: common.mustCall(function(specifier, context, nextResolve) { | ||
assert.strictEqual(context.testProp, 'custom'); // It should be merged from hook 2 and 3. | ||
const result = nextResolve(specifier, context); | ||
return result; | ||
}, 1), | ||
}); | ||
|
||
const hook2 = registerHooks({ | ||
resolve: common.mustCall(function(specifier, context, nextResolve) { | ||
assert.strictEqual(context.testProp, 'custom'); // It should be merged from hook 3. | ||
return nextResolve(specifier); // Omit the context. | ||
}, 1), | ||
}); | ||
|
||
const hook3 = registerHooks({ | ||
resolve: common.mustCall(function(specifier, context, nextResolve) { | ||
return nextResolve(specifier, { testProp: 'custom' }); // Add a custom property | ||
}, 1), | ||
}); | ||
|
||
await import('../fixtures/es-modules/message.mjs'); | ||
|
||
hook3.deregister(); | ||
hook2.deregister(); | ||
hook1.deregister(); |
34 changes: 34 additions & 0 deletions
34
test/module-hooks/test-module-hooks-resolve-context-merged.js
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,34 @@ | ||
'use strict'; | ||
|
||
// Test that the context parameter will be merged in multiple resolve hooks. | ||
|
||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const { registerHooks } = require('module'); | ||
|
||
const hook1 = registerHooks({ | ||
resolve: common.mustCall(function(specifier, context, nextResolve) { | ||
assert.strictEqual(context.testProp, 'custom'); // It should be merged from hook 2 and 3. | ||
const result = nextResolve(specifier, context); | ||
return result; | ||
}, 1), | ||
}); | ||
|
||
const hook2 = registerHooks({ | ||
resolve: common.mustCall(function(specifier, context, nextResolve) { | ||
assert.strictEqual(context.testProp, 'custom'); // It should be merged from hook 3. | ||
return nextResolve(specifier); // Omit the context. | ||
}, 1), | ||
}); | ||
|
||
const hook3 = registerHooks({ | ||
resolve: common.mustCall(function(specifier, context, nextResolve) { | ||
return nextResolve(specifier, { testProp: 'custom' }); // Add a custom property | ||
}, 1), | ||
}); | ||
|
||
require('../fixtures/empty.js'); | ||
|
||
hook3.deregister(); | ||
hook2.deregister(); | ||
hook1.deregister(); |
14 changes: 14 additions & 0 deletions
14
test/module-hooks/test-module-hooks-resolve-context-optional-esm.mjs
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,14 @@ | ||
// Test that the context parameter can be omitted in the nextResolve invocation. | ||
|
||
import * as common from '../common/index.mjs'; | ||
import { registerHooks } from 'node:module'; | ||
|
||
const hook = registerHooks({ | ||
resolve: common.mustCall(function(specifier, context, nextResolve) { | ||
return nextResolve(specifier); | ||
}, 1), | ||
}); | ||
|
||
await import('../fixtures/es-modules/message.mjs'); | ||
|
||
hook.deregister(); |
16 changes: 16 additions & 0 deletions
16
test/module-hooks/test-module-hooks-resolve-context-optional.js
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,16 @@ | ||
'use strict'; | ||
|
||
// Test that the context parameter can be omitted in the nextResolve invocation. | ||
|
||
const common = require('../common'); | ||
const { registerHooks } = require('module'); | ||
|
||
const hook = registerHooks({ | ||
resolve: common.mustCall(function(specifier, context, nextResolve) { | ||
return nextResolve(specifier); | ||
}, 1), | ||
}); | ||
|
||
require('../fixtures/empty.js'); | ||
|
||
hook.deregister(); |