Skip to content

Commit b1f0cc6

Browse files
Mesteerydanielleadams
authored andcommitted
lib: move kEnumerableProperty to internal/util
PR-URL: #41877 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]>
1 parent 2d16565 commit b1f0cc6

14 files changed

+217
-61
lines changed

lib/internal/abort_controller.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const {
2525
} = require('internal/event_target');
2626
const {
2727
customInspectSymbol,
28+
kEnumerableProperty,
2829
} = require('internal/util');
2930
const { inspect } = require('internal/util/inspect');
3031
const {
@@ -253,7 +254,7 @@ function ClonedAbortSignal() {
253254
ClonedAbortSignal.prototype[kDeserialize] = () => {};
254255

255256
ObjectDefineProperties(AbortSignal.prototype, {
256-
aborted: { enumerable: true }
257+
aborted: kEnumerableProperty,
257258
});
258259

259260
ObjectDefineProperty(AbortSignal.prototype, SymbolToStringTag, {
@@ -322,8 +323,8 @@ class AbortController {
322323
}
323324

324325
ObjectDefineProperties(AbortController.prototype, {
325-
signal: { enumerable: true },
326-
abort: { enumerable: true }
326+
signal: kEnumerableProperty,
327+
abort: kEnumerableProperty,
327328
});
328329

329330
ObjectDefineProperty(AbortController.prototype, SymbolToStringTag, {

lib/internal/crypto/webcrypto.js

+2-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ const {
55
JSONParse,
66
JSONStringify,
77
ObjectDefineProperties,
8-
ObjectGetOwnPropertyDescriptor,
98
SafeSet,
109
SymbolToStringTag,
1110
StringPrototypeRepeat,
@@ -60,6 +59,7 @@ const {
6059
} = require('internal/crypto/util');
6160

6261
const {
62+
kEnumerableProperty,
6363
lazyDOMException,
6464
} = require('internal/util');
6565

@@ -703,10 +703,7 @@ ObjectDefineProperties(
703703
writable: false,
704704
value: 'Crypto',
705705
},
706-
subtle: {
707-
...ObjectGetOwnPropertyDescriptor(Crypto.prototype, 'subtle'),
708-
enumerable: true,
709-
},
706+
subtle: kEnumerableProperty,
710707
getRandomValues: {
711708
enumerable: true,
712709
configurable: true,

lib/internal/encoding.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ const kEncoder = Symbol('encoder');
3030

3131
const {
3232
getConstructorOf,
33-
customInspectSymbol: inspect
33+
customInspectSymbol: inspect,
34+
kEnumerableProperty,
3435
} = require('internal/util');
3536

3637
const {
@@ -358,9 +359,9 @@ class TextEncoder {
358359

359360
ObjectDefineProperties(
360361
TextEncoder.prototype, {
361-
'encode': { enumerable: true },
362-
'encodeInto': { enumerable: true },
363-
'encoding': { enumerable: true },
362+
'encode': kEnumerableProperty,
363+
'encodeInto': kEnumerableProperty,
364+
'encoding': kEnumerableProperty,
364365
[SymbolToStringTag]: { configurable: true, value: 'TextEncoder' },
365366
});
366367

@@ -569,7 +570,7 @@ ObjectDefineProperties(
569570
);
570571

571572
ObjectDefineProperties(TextDecoder.prototype, {
572-
decode: { enumerable: true },
573+
decode: kEnumerableProperty,
573574
[inspect]: { enumerable: false },
574575
[SymbolToStringTag]: {
575576
configurable: true,

lib/internal/event_target.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ const {
88
FunctionPrototypeCall,
99
NumberIsInteger,
1010
ObjectAssign,
11-
ObjectCreate,
1211
ObjectDefineProperties,
1312
ObjectDefineProperty,
1413
ObjectGetOwnPropertyDescriptor,
@@ -36,7 +35,7 @@ const {
3635
} = require('internal/errors');
3736
const { validateObject, validateString } = require('internal/validators');
3837

39-
const { customInspectSymbol } = require('internal/util');
38+
const { customInspectSymbol, kEnumerableProperty } = require('internal/util');
4039
const { inspect } = require('util');
4140

4241
const kIsEventTarget = SymbolFor('nodejs.event_target');
@@ -298,9 +297,6 @@ class Event {
298297
static BUBBLING_PHASE = 3;
299298
}
300299

301-
const kEnumerableProperty = ObjectCreate(null);
302-
kEnumerableProperty.enumerable = true;
303-
304300
ObjectDefineProperties(
305301
Event.prototype, {
306302
[SymbolToStringTag]: {

lib/internal/url.js

+27-26
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ const {
4343
getConstructorOf,
4444
removeColors,
4545
toUSVString,
46+
kEnumerableProperty,
4647
} = require('internal/util');
4748

4849
const {
@@ -507,18 +508,18 @@ class URLSearchParams {
507508
}
508509

509510
ObjectDefineProperties(URLSearchParams.prototype, {
510-
append: { enumerable: true },
511-
delete: { enumerable: true },
512-
get: { enumerable: true },
513-
getAll: { enumerable: true },
514-
has: { enumerable: true },
515-
set: { enumerable: true },
516-
sort: { enumerable: true },
517-
entries: { enumerable: true },
518-
forEach: { enumerable: true },
519-
keys: { enumerable: true },
520-
values: { enumerable: true },
521-
toString: { enumerable: true },
511+
append: kEnumerableProperty,
512+
delete: kEnumerableProperty,
513+
get: kEnumerableProperty,
514+
getAll: kEnumerableProperty,
515+
has: kEnumerableProperty,
516+
set: kEnumerableProperty,
517+
sort: kEnumerableProperty,
518+
entries: kEnumerableProperty,
519+
forEach: kEnumerableProperty,
520+
keys: kEnumerableProperty,
521+
values: kEnumerableProperty,
522+
toString: kEnumerableProperty,
522523
[SymbolToStringTag]: { configurable: true, value: 'URLSearchParams' },
523524

524525
// https://heycam.github.io/webidl/#es-iterable-entries
@@ -982,20 +983,20 @@ class URL {
982983
ObjectDefineProperties(URL.prototype, {
983984
[kFormat]: { configurable: false, writable: false },
984985
[SymbolToStringTag]: { configurable: true, value: 'URL' },
985-
toString: { enumerable: true },
986-
href: { enumerable: true },
987-
origin: { enumerable: true },
988-
protocol: { enumerable: true },
989-
username: { enumerable: true },
990-
password: { enumerable: true },
991-
host: { enumerable: true },
992-
hostname: { enumerable: true },
993-
port: { enumerable: true },
994-
pathname: { enumerable: true },
995-
search: { enumerable: true },
996-
searchParams: { enumerable: true },
997-
hash: { enumerable: true },
998-
toJSON: { enumerable: true },
986+
toString: kEnumerableProperty,
987+
href: kEnumerableProperty,
988+
origin: kEnumerableProperty,
989+
protocol: kEnumerableProperty,
990+
username: kEnumerableProperty,
991+
password: kEnumerableProperty,
992+
host: kEnumerableProperty,
993+
hostname: kEnumerableProperty,
994+
port: kEnumerableProperty,
995+
pathname: kEnumerableProperty,
996+
search: kEnumerableProperty,
997+
searchParams: kEnumerableProperty,
998+
hash: kEnumerableProperty,
999+
toJSON: kEnumerableProperty,
9991000
});
10001001

10011002
function update(url, params) {

lib/internal/util.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,9 @@ function structuredClone(value) {
508508
return des.readValue();
509509
}
510510

511+
const kEnumerableProperty = ObjectCreate(null);
512+
kEnumerableProperty.enumerable = true;
513+
511514
module.exports = {
512515
assertCrypto,
513516
cachedResult,
@@ -546,5 +549,7 @@ module.exports = {
546549
// Used by the buffer module to capture an internal reference to the
547550
// default isEncoding implementation, just in case userland overrides it.
548551
kIsEncodingSymbol: Symbol('kIsEncodingSymbol'),
549-
kVmBreakFirstLineSymbol: Symbol('kVmBreakFirstLineSymbol')
552+
kVmBreakFirstLineSymbol: Symbol('kVmBreakFirstLineSymbol'),
553+
554+
kEnumerableProperty,
550555
};
+162
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
'use strict';
2+
3+
const {
4+
ObjectDefineProperties,
5+
Symbol,
6+
} = primordials;
7+
8+
const {
9+
codes: {
10+
ERR_INVALID_ARG_VALUE,
11+
ERR_INVALID_THIS,
12+
},
13+
} = require('internal/errors');
14+
15+
const {
16+
newReadableWritablePairFromDuplex,
17+
} = require('internal/webstreams/adapters');
18+
19+
const { customInspect } = require('internal/webstreams/util');
20+
21+
const {
22+
customInspectSymbol: kInspect,
23+
kEnumerableProperty,
24+
} = require('internal/util');
25+
26+
let zlib;
27+
function lazyZlib() {
28+
zlib ??= require('zlib');
29+
return zlib;
30+
}
31+
32+
const kHandle = Symbol('kHandle');
33+
const kTransform = Symbol('kTransform');
34+
const kType = Symbol('kType');
35+
36+
/**
37+
* @typedef {import('./readablestream').ReadableStream} ReadableStream
38+
* @typedef {import('./writablestream').WritableStream} WritableStream
39+
*/
40+
41+
function isCompressionStream(value) {
42+
return typeof value?.[kHandle] === 'object' &&
43+
value?.[kType] === 'CompressionStream';
44+
}
45+
46+
function isDecompressionStream(value) {
47+
return typeof value?.[kHandle] === 'object' &&
48+
value?.[kType] === 'DecompressionStream';
49+
}
50+
51+
class CompressionStream {
52+
/**
53+
* @param {'deflate'|'gzip'} format
54+
*/
55+
constructor(format) {
56+
this[kType] = 'CompressionStream';
57+
switch (format) {
58+
case 'deflate':
59+
this[kHandle] = lazyZlib().createDeflate();
60+
break;
61+
case 'gzip':
62+
this[kHandle] = lazyZlib().createGzip();
63+
break;
64+
default:
65+
throw new ERR_INVALID_ARG_VALUE('format', format);
66+
}
67+
this[kTransform] = newReadableWritablePairFromDuplex(this[kHandle]);
68+
}
69+
70+
/**
71+
* @readonly
72+
* @type {ReadableStream}
73+
*/
74+
get readable() {
75+
if (!isCompressionStream(this))
76+
throw new ERR_INVALID_THIS('CompressionStream');
77+
return this[kTransform].readable;
78+
}
79+
80+
/**
81+
* @readonly
82+
* @type {WritableStream}
83+
*/
84+
get writable() {
85+
if (!isCompressionStream(this))
86+
throw new ERR_INVALID_THIS('CompressionStream');
87+
return this[kTransform].writable;
88+
}
89+
90+
[kInspect](depth, options) {
91+
if (!isCompressionStream(this))
92+
throw new ERR_INVALID_THIS('CompressionStream');
93+
customInspect(depth, options, 'CompressionStream', {
94+
readable: this[kTransform].readable,
95+
writable: this[kTransform].writable,
96+
});
97+
}
98+
}
99+
100+
class DecompressionStream {
101+
/**
102+
* @param {'deflate'|'gzip'} format
103+
*/
104+
constructor(format) {
105+
this[kType] = 'DecompressionStream';
106+
switch (format) {
107+
case 'deflate':
108+
this[kHandle] = lazyZlib().createInflate();
109+
break;
110+
case 'gzip':
111+
this[kHandle] = lazyZlib().createGunzip();
112+
break;
113+
default:
114+
throw new ERR_INVALID_ARG_VALUE('format', format);
115+
}
116+
this[kTransform] = newReadableWritablePairFromDuplex(this[kHandle]);
117+
}
118+
119+
/**
120+
* @readonly
121+
* @type {ReadableStream}
122+
*/
123+
get readable() {
124+
if (!isDecompressionStream(this))
125+
throw new ERR_INVALID_THIS('DecompressionStream');
126+
return this[kTransform].readable;
127+
}
128+
129+
/**
130+
* @readonly
131+
* @type {WritableStream}
132+
*/
133+
get writable() {
134+
if (!isDecompressionStream(this))
135+
throw new ERR_INVALID_THIS('DecompressionStream');
136+
return this[kTransform].writable;
137+
}
138+
139+
[kInspect](depth, options) {
140+
if (!isDecompressionStream(this))
141+
throw new ERR_INVALID_THIS('DecompressionStream');
142+
customInspect(depth, options, 'DecompressionStream', {
143+
readable: this[kTransform].readable,
144+
writable: this[kTransform].writable,
145+
});
146+
}
147+
}
148+
149+
ObjectDefineProperties(CompressionStream.prototype, {
150+
readable: kEnumerableProperty,
151+
writable: kEnumerableProperty,
152+
});
153+
154+
ObjectDefineProperties(DecompressionStream.prototype, {
155+
readable: kEnumerableProperty,
156+
writable: kEnumerableProperty,
157+
});
158+
159+
module.exports = {
160+
CompressionStream,
161+
DecompressionStream,
162+
};

lib/internal/webstreams/encoding.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@ const {
1414
TransformStream,
1515
} = require('internal/webstreams/transformstream');
1616

17-
const {
18-
customInspect,
19-
kEnumerableProperty,
20-
} = require('internal/webstreams/util');
17+
const { customInspect } = require('internal/webstreams/util');
2118

2219
const {
2320
codes: {
@@ -26,7 +23,8 @@ const {
2623
} = require('internal/errors');
2724

2825
const {
29-
customInspectSymbol: kInspect
26+
customInspectSymbol: kInspect,
27+
kEnumerableProperty,
3028
} = require('internal/util');
3129

3230
const kHandle = Symbol('kHandle');

0 commit comments

Comments
 (0)