| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 6c21575 commit 11c2f9c
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -243,6 +243,7 @@ changes: | |||
| 243 | 243 | * `deregister()` {Function} Remove the registered hooks so that they are no | |
| 244 | 244 | longer called. Hooks are otherwise retained for the lifetime of the running | |
| 245 | 245 | process. | |
| 246 | + * `[Symbol.dispose]` {Function} The same as `deregister`. | ||
| 246 | 247 | ||
| 247 | 248 | Register [hooks][] that customize Node.js module resolution and loading behavior. | |
| 248 | 249 | See [Customization hooks][]. The returned object can be used to | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -9,6 +9,7 @@ const { | |||
| 9 | 9 | StringPrototypeSlice, | |
| 10 | 10 | StringPrototypeStartsWith, | |
| 11 | 11 | Symbol, | |
| 12 | + SymbolDispose, | ||
| 12 | 13 | } = primordials; | |
| 13 | 14 | const { | |
| 14 | 15 | isAnyArrayBuffer, | |
@@ -83,10 +84,7 @@ class ModuleHooks { | |||
| 83 | 84 | ||
| 84 | 85 | ObjectFreeze(this); | |
| 85 | 86 | } | |
| 86 | - // TODO(joyeecheung): we may want methods that allow disabling/enabling temporarily | ||
| 87 | - // which just sets the item in the array to undefined temporarily. | ||
| 88 | - // TODO(joyeecheung): this can be the [Symbol.dispose] implementation to pair with | ||
| 89 | - // `using` when the explicit resource management proposal is shipped by V8. | ||
| 87 | + | ||
| 90 | 88 | /** | |
| 91 | 89 | * Deregister the hook instance. | |
| 92 | 90 | */ | |
@@ -103,6 +101,8 @@ class ModuleHooks { | |||
| 103 | 101 | } | |
| 104 | 102 | }; | |
| 105 | 103 | ||
| 104 | + ModuleHooks.prototype[SymbolDispose] = ModuleHooks.prototype.deregister; | ||
| 105 | + | ||
| 106 | 106 | /** | |
| 107 | 107 | * TODO(joyeecheung): taken an optional description? | |
| 108 | 108 | * @param {{ resolve?: ResolveHook, load?: LoadHook }} hooks User-provided hooks | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,26 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + | ||
| 3 | + const common = require('../common'); | ||
| 4 | + const assert = require('assert'); | ||
| 5 | + const { registerHooks } = require('module'); | ||
| 6 | + | ||
| 7 | + // Test that using syntax works. | ||
| 8 | + { | ||
| 9 | + // eslint-disable-next-line no-unused-vars | ||
| 10 | + using hook = registerHooks({ | ||
| 11 | + load: common.mustCall((url, context, nextLoad) => { | ||
| 12 | + const result = nextLoad(url, context); | ||
| 13 | + assert.strictEqual(result.source, ''); | ||
| 14 | + return { | ||
| 15 | + source: 'export const hello = "world"', | ||
| 16 | + }; | ||
| 17 | + }), | ||
| 18 | + }); | ||
| 19 | + | ||
| 20 | + const mod = require('../fixtures/empty.js'); | ||
| 21 | + assert.strictEqual(mod.hello, 'world'); | ||
| 22 | + } | ||
| 23 | + | ||
| 24 | + delete require.cache[require.resolve('../fixtures/empty.js')]; | ||
| 25 | + const mod = require('../fixtures/empty.js'); | ||
| 26 | + assert.deepStrictEqual(mod, {}); | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments