|
| 1 | +// Copyright (c) 2017-2026 Cloudflare, Inc. |
| 2 | +// Licensed under the Apache 2.0 license found in the LICENSE file or at: |
| 3 | +// https://opensource.org/licenses/Apache-2.0 |
| 4 | +import { createRequire } from 'node:module'; |
| 5 | +import { strictEqual, ok, doesNotThrow } from 'node:assert'; |
| 6 | + |
| 7 | +const require = createRequire('/'); |
| 8 | + |
| 9 | +export const testTimersPromisesMutable = { |
| 10 | + test() { |
| 11 | + const timersPromises = require('node:timers/promises'); |
| 12 | + const originalSetImmediate = timersPromises.setImmediate; |
| 13 | + ok(typeof originalSetImmediate === 'function'); |
| 14 | + |
| 15 | + const patchedSetImmediate = async function patchedSetImmediate() { |
| 16 | + return 'patched'; |
| 17 | + }; |
| 18 | + |
| 19 | + doesNotThrow(() => { |
| 20 | + timersPromises.setImmediate = patchedSetImmediate; |
| 21 | + }); |
| 22 | + |
| 23 | + strictEqual(timersPromises.setImmediate, patchedSetImmediate); |
| 24 | + timersPromises.setImmediate = originalSetImmediate; |
| 25 | + strictEqual(timersPromises.setImmediate, originalSetImmediate); |
| 26 | + }, |
| 27 | +}; |
| 28 | + |
| 29 | +export const testTimersMutable = { |
| 30 | + test() { |
| 31 | + const timers = require('node:timers'); |
| 32 | + const originalSetTimeout = timers.setTimeout; |
| 33 | + ok(typeof originalSetTimeout === 'function'); |
| 34 | + |
| 35 | + const patchedSetTimeout = function patchedSetTimeout() { |
| 36 | + return 'patched'; |
| 37 | + }; |
| 38 | + |
| 39 | + doesNotThrow(() => { |
| 40 | + timers.setTimeout = patchedSetTimeout; |
| 41 | + }); |
| 42 | + |
| 43 | + strictEqual(timers.setTimeout, patchedSetTimeout); |
| 44 | + timers.setTimeout = originalSetTimeout; |
| 45 | + }, |
| 46 | +}; |
| 47 | + |
| 48 | +export const testBufferMutable = { |
| 49 | + test() { |
| 50 | + const buffer = require('node:buffer'); |
| 51 | + const originalBuffer = buffer.Buffer; |
| 52 | + ok(typeof originalBuffer === 'function'); |
| 53 | + |
| 54 | + const patchedBuffer = function PatchedBuffer() { |
| 55 | + return 'patched'; |
| 56 | + }; |
| 57 | + |
| 58 | + doesNotThrow(() => { |
| 59 | + buffer.Buffer = patchedBuffer; |
| 60 | + }); |
| 61 | + |
| 62 | + strictEqual(buffer.Buffer, patchedBuffer); |
| 63 | + buffer.Buffer = originalBuffer; |
| 64 | + }, |
| 65 | +}; |
| 66 | + |
| 67 | +export const testUtilMutable = { |
| 68 | + test() { |
| 69 | + const util = require('node:util'); |
| 70 | + const originalPromisify = util.promisify; |
| 71 | + ok(typeof originalPromisify === 'function'); |
| 72 | + |
| 73 | + const patchedPromisify = function patchedPromisify() { |
| 74 | + return 'patched'; |
| 75 | + }; |
| 76 | + |
| 77 | + doesNotThrow(() => { |
| 78 | + util.promisify = patchedPromisify; |
| 79 | + }); |
| 80 | + |
| 81 | + strictEqual(util.promisify, patchedPromisify); |
| 82 | + util.promisify = originalPromisify; |
| 83 | + }, |
| 84 | +}; |
| 85 | + |
| 86 | +export const testRequireCachesMutableObject = { |
| 87 | + test() { |
| 88 | + const timersPromises1 = require('node:timers/promises'); |
| 89 | + const timersPromises2 = require('node:timers/promises'); |
| 90 | + |
| 91 | + strictEqual(timersPromises1, timersPromises2); |
| 92 | + |
| 93 | + const patchedSetImmediate = async function patched() { |
| 94 | + return 'patched'; |
| 95 | + }; |
| 96 | + const original = timersPromises1.setImmediate; |
| 97 | + |
| 98 | + timersPromises1.setImmediate = patchedSetImmediate; |
| 99 | + strictEqual(timersPromises2.setImmediate, patchedSetImmediate); |
| 100 | + timersPromises1.setImmediate = original; |
| 101 | + }, |
| 102 | +}; |
| 103 | + |
| 104 | +// When require_returns_default_export is enabled, require() should return the |
| 105 | +// default export directly (which is the object with all the functions), |
| 106 | +// not the namespace wrapper with both `default` and named exports. |
| 107 | +export const testRequireReturnsDefaultExport = { |
| 108 | + test() { |
| 109 | + const timers = require('node:timers'); |
| 110 | + // With require_returns_default_export enabled, timers should be the |
| 111 | + // default export object directly, not the namespace wrapper. |
| 112 | + // The default export IS the object with setTimeout, setInterval, etc. |
| 113 | + ok(typeof timers.setTimeout === 'function'); |
| 114 | + ok(typeof timers.setInterval === 'function'); |
| 115 | + ok(typeof timers.clearTimeout === 'function'); |
| 116 | + ok(typeof timers.clearInterval === 'function'); |
| 117 | + // The namespace wrapper would have a 'default' property, but when |
| 118 | + // we return the default export directly, there's no 'default' property |
| 119 | + // on the returned object (unless the default export itself has one). |
| 120 | + strictEqual(timers.default, undefined); |
| 121 | + }, |
| 122 | +}; |
0 commit comments