• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import '../common/index.mjs';
2import assert from 'assert';
3import { syncBuiltinESMExports } from 'module';
4
5import fs, { readFile, readFileSync } from 'fs';
6import events, { defaultMaxListeners } from 'events';
7import util from 'util';
8
9const readFileDescriptor = Reflect.getOwnPropertyDescriptor(fs, 'readFile');
10const readFileSyncDescriptor =
11  Reflect.getOwnPropertyDescriptor(fs, 'readFileSync');
12
13const s = Symbol();
14const fn = () => s;
15
16Reflect.deleteProperty(fs, 'readFile');
17syncBuiltinESMExports();
18
19assert.deepStrictEqual([fs.readFile, readFile], [undefined, undefined]);
20
21fs.readFile = fn;
22syncBuiltinESMExports();
23
24assert.deepStrictEqual([fs.readFile(), readFile()], [s, s]);
25
26Reflect.defineProperty(fs, 'readFile', {
27  value: fn,
28  configurable: true,
29  writable: true,
30});
31syncBuiltinESMExports();
32
33assert.deepStrictEqual([fs.readFile(), readFile()], [s, s]);
34
35Reflect.deleteProperty(fs, 'readFile');
36syncBuiltinESMExports();
37
38assert.deepStrictEqual([fs.readFile, readFile], [undefined, undefined]);
39
40let count = 0;
41
42Reflect.defineProperty(fs, 'readFile', {
43  get() { return count; },
44  configurable: true,
45});
46syncBuiltinESMExports();
47
48assert.deepStrictEqual([readFile, fs.readFile], [0, 0]);
49
50count++;
51syncBuiltinESMExports();
52
53assert.deepStrictEqual([fs.readFile, readFile], [1, 1]);
54
55let otherValue;
56
57Reflect.defineProperty(fs, 'readFile', { // eslint-disable-line accessor-pairs
58  set(value) {
59    Reflect.deleteProperty(fs, 'readFile');
60    otherValue = value;
61  },
62  configurable: true,
63});
64
65Reflect.defineProperty(fs, 'readFileSync', {
66  get() {
67    return otherValue;
68  },
69  configurable: true,
70});
71
72fs.readFile = 2;
73syncBuiltinESMExports();
74
75assert.deepStrictEqual([readFile, readFileSync], [undefined, 2]);
76
77Reflect.defineProperty(fs, 'readFile', readFileDescriptor);
78Reflect.defineProperty(fs, 'readFileSync', readFileSyncDescriptor);
79
80const originDefaultMaxListeners = events.defaultMaxListeners;
81const utilProto = util.__proto__; // eslint-disable-line no-proto
82
83count = 0;
84
85Reflect.defineProperty(Function.prototype, 'defaultMaxListeners', {
86  configurable: true,
87  enumerable: true,
88  get: function() { return ++count; },
89  set: function(v) {
90    Reflect.defineProperty(this, 'defaultMaxListeners', {
91      configurable: true,
92      enumerable: true,
93      writable: true,
94      value: v,
95    });
96  },
97});
98syncBuiltinESMExports();
99
100assert.strictEqual(defaultMaxListeners, originDefaultMaxListeners);
101assert.strictEqual(events.defaultMaxListeners, originDefaultMaxListeners);
102
103events.defaultMaxListeners += 1;
104
105assert.strictEqual(events.defaultMaxListeners,
106                   originDefaultMaxListeners + 1);
107
108syncBuiltinESMExports();
109
110assert.strictEqual(defaultMaxListeners, originDefaultMaxListeners + 1);
111assert.strictEqual(Function.prototype.defaultMaxListeners, 1);
112
113Function.prototype.defaultMaxListeners = 'foo';
114syncBuiltinESMExports();
115
116assert.strictEqual(Function.prototype.defaultMaxListeners, 'foo');
117assert.strictEqual(events.defaultMaxListeners, originDefaultMaxListeners + 1);
118assert.strictEqual(defaultMaxListeners, originDefaultMaxListeners + 1);
119
120count = 0;
121
122const p = {
123  get foo() { return ++count; },
124  set foo(v) {
125    Reflect.defineProperty(this, 'foo', {
126      configurable: true,
127      enumerable: true,
128      writable: true,
129      value: v,
130    });
131  },
132};
133
134util.__proto__ = p; // eslint-disable-line no-proto
135syncBuiltinESMExports();
136
137assert.strictEqual(util.foo, 1);
138
139util.foo = 'bar';
140syncBuiltinESMExports();
141
142assert.strictEqual(count, 1);
143assert.strictEqual(util.foo, 'bar');
144assert.strictEqual(p.foo, 2);
145
146p.foo = 'foo';
147syncBuiltinESMExports();
148
149assert.strictEqual(p.foo, 'foo');
150
151events.defaultMaxListeners = originDefaultMaxListeners;
152util.__proto__ = utilProto; // eslint-disable-line no-proto
153
154Reflect.deleteProperty(util, 'foo');
155Reflect.deleteProperty(Function.prototype, 'defaultMaxListeners');
156syncBuiltinESMExports();
157
158assert.throws(
159  () => Object.defineProperty(events, 'defaultMaxListeners', { value: 3 }),
160  /TypeError: Cannot redefine/
161);
162