Skip to content

Commit

Permalink
lib: move kEnumerableProperty to internal/util
Browse files Browse the repository at this point in the history
PR-URL: #41877
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
  • Loading branch information
Mesteery authored and danielleadams committed Apr 24, 2022
1 parent 2d16565 commit b1f0cc6
Show file tree
Hide file tree
Showing 14 changed files with 217 additions and 61 deletions.
7 changes: 4 additions & 3 deletions lib/internal/abort_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const {
} = require('internal/event_target');
const {
customInspectSymbol,
kEnumerableProperty,
} = require('internal/util');
const { inspect } = require('internal/util/inspect');
const {
Expand Down Expand Up @@ -253,7 +254,7 @@ function ClonedAbortSignal() {
ClonedAbortSignal.prototype[kDeserialize] = () => {};

ObjectDefineProperties(AbortSignal.prototype, {
aborted: { enumerable: true }
aborted: kEnumerableProperty,
});

ObjectDefineProperty(AbortSignal.prototype, SymbolToStringTag, {
Expand Down Expand Up @@ -322,8 +323,8 @@ class AbortController {
}

ObjectDefineProperties(AbortController.prototype, {
signal: { enumerable: true },
abort: { enumerable: true }
signal: kEnumerableProperty,
abort: kEnumerableProperty,
});

ObjectDefineProperty(AbortController.prototype, SymbolToStringTag, {
Expand Down
7 changes: 2 additions & 5 deletions lib/internal/crypto/webcrypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const {
JSONParse,
JSONStringify,
ObjectDefineProperties,
ObjectGetOwnPropertyDescriptor,
SafeSet,
SymbolToStringTag,
StringPrototypeRepeat,
Expand Down Expand Up @@ -60,6 +59,7 @@ const {
} = require('internal/crypto/util');

const {
kEnumerableProperty,
lazyDOMException,
} = require('internal/util');

Expand Down Expand Up @@ -703,10 +703,7 @@ ObjectDefineProperties(
writable: false,
value: 'Crypto',
},
subtle: {
...ObjectGetOwnPropertyDescriptor(Crypto.prototype, 'subtle'),
enumerable: true,
},
subtle: kEnumerableProperty,
getRandomValues: {
enumerable: true,
configurable: true,
Expand Down
11 changes: 6 additions & 5 deletions lib/internal/encoding.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ const kEncoder = Symbol('encoder');

const {
getConstructorOf,
customInspectSymbol: inspect
customInspectSymbol: inspect,
kEnumerableProperty,
} = require('internal/util');

const {
Expand Down Expand Up @@ -358,9 +359,9 @@ class TextEncoder {

ObjectDefineProperties(
TextEncoder.prototype, {
'encode': { enumerable: true },
'encodeInto': { enumerable: true },
'encoding': { enumerable: true },
'encode': kEnumerableProperty,
'encodeInto': kEnumerableProperty,
'encoding': kEnumerableProperty,
[SymbolToStringTag]: { configurable: true, value: 'TextEncoder' },
});

Expand Down Expand Up @@ -569,7 +570,7 @@ ObjectDefineProperties(
);

ObjectDefineProperties(TextDecoder.prototype, {
decode: { enumerable: true },
decode: kEnumerableProperty,
[inspect]: { enumerable: false },
[SymbolToStringTag]: {
configurable: true,
Expand Down
6 changes: 1 addition & 5 deletions lib/internal/event_target.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ const {
FunctionPrototypeCall,
NumberIsInteger,
ObjectAssign,
ObjectCreate,
ObjectDefineProperties,
ObjectDefineProperty,
ObjectGetOwnPropertyDescriptor,
Expand Down Expand Up @@ -36,7 +35,7 @@ const {
} = require('internal/errors');
const { validateObject, validateString } = require('internal/validators');

const { customInspectSymbol } = require('internal/util');
const { customInspectSymbol, kEnumerableProperty } = require('internal/util');
const { inspect } = require('util');

const kIsEventTarget = SymbolFor('nodejs.event_target');
Expand Down Expand Up @@ -298,9 +297,6 @@ class Event {
static BUBBLING_PHASE = 3;
}

const kEnumerableProperty = ObjectCreate(null);
kEnumerableProperty.enumerable = true;

ObjectDefineProperties(
Event.prototype, {
[SymbolToStringTag]: {
Expand Down
53 changes: 27 additions & 26 deletions lib/internal/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const {
getConstructorOf,
removeColors,
toUSVString,
kEnumerableProperty,
} = require('internal/util');

const {
Expand Down Expand Up @@ -507,18 +508,18 @@ class URLSearchParams {
}

ObjectDefineProperties(URLSearchParams.prototype, {
append: { enumerable: true },
delete: { enumerable: true },
get: { enumerable: true },
getAll: { enumerable: true },
has: { enumerable: true },
set: { enumerable: true },
sort: { enumerable: true },
entries: { enumerable: true },
forEach: { enumerable: true },
keys: { enumerable: true },
values: { enumerable: true },
toString: { enumerable: true },
append: kEnumerableProperty,
delete: kEnumerableProperty,
get: kEnumerableProperty,
getAll: kEnumerableProperty,
has: kEnumerableProperty,
set: kEnumerableProperty,
sort: kEnumerableProperty,
entries: kEnumerableProperty,
forEach: kEnumerableProperty,
keys: kEnumerableProperty,
values: kEnumerableProperty,
toString: kEnumerableProperty,
[SymbolToStringTag]: { configurable: true, value: 'URLSearchParams' },

// https://heycam.github.io/webidl/#es-iterable-entries
Expand Down Expand Up @@ -982,20 +983,20 @@ class URL {
ObjectDefineProperties(URL.prototype, {
[kFormat]: { configurable: false, writable: false },
[SymbolToStringTag]: { configurable: true, value: 'URL' },
toString: { enumerable: true },
href: { enumerable: true },
origin: { enumerable: true },
protocol: { enumerable: true },
username: { enumerable: true },
password: { enumerable: true },
host: { enumerable: true },
hostname: { enumerable: true },
port: { enumerable: true },
pathname: { enumerable: true },
search: { enumerable: true },
searchParams: { enumerable: true },
hash: { enumerable: true },
toJSON: { enumerable: true },
toString: kEnumerableProperty,
href: kEnumerableProperty,
origin: kEnumerableProperty,
protocol: kEnumerableProperty,
username: kEnumerableProperty,
password: kEnumerableProperty,
host: kEnumerableProperty,
hostname: kEnumerableProperty,
port: kEnumerableProperty,
pathname: kEnumerableProperty,
search: kEnumerableProperty,
searchParams: kEnumerableProperty,
hash: kEnumerableProperty,
toJSON: kEnumerableProperty,
});

function update(url, params) {
Expand Down
7 changes: 6 additions & 1 deletion lib/internal/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,9 @@ function structuredClone(value) {
return des.readValue();
}

const kEnumerableProperty = ObjectCreate(null);
kEnumerableProperty.enumerable = true;

module.exports = {
assertCrypto,
cachedResult,
Expand Down Expand Up @@ -546,5 +549,7 @@ module.exports = {
// Used by the buffer module to capture an internal reference to the
// default isEncoding implementation, just in case userland overrides it.
kIsEncodingSymbol: Symbol('kIsEncodingSymbol'),
kVmBreakFirstLineSymbol: Symbol('kVmBreakFirstLineSymbol')
kVmBreakFirstLineSymbol: Symbol('kVmBreakFirstLineSymbol'),

kEnumerableProperty,
};
162 changes: 162 additions & 0 deletions lib/internal/webstreams/compression.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
'use strict';

const {
ObjectDefineProperties,
Symbol,
} = primordials;

const {
codes: {
ERR_INVALID_ARG_VALUE,
ERR_INVALID_THIS,
},
} = require('internal/errors');

const {
newReadableWritablePairFromDuplex,
} = require('internal/webstreams/adapters');

const { customInspect } = require('internal/webstreams/util');

const {
customInspectSymbol: kInspect,
kEnumerableProperty,
} = require('internal/util');

let zlib;
function lazyZlib() {
zlib ??= require('zlib');
return zlib;
}

const kHandle = Symbol('kHandle');
const kTransform = Symbol('kTransform');
const kType = Symbol('kType');

/**
* @typedef {import('./readablestream').ReadableStream} ReadableStream
* @typedef {import('./writablestream').WritableStream} WritableStream
*/

function isCompressionStream(value) {
return typeof value?.[kHandle] === 'object' &&
value?.[kType] === 'CompressionStream';
}

function isDecompressionStream(value) {
return typeof value?.[kHandle] === 'object' &&
value?.[kType] === 'DecompressionStream';
}

class CompressionStream {
/**
* @param {'deflate'|'gzip'} format
*/
constructor(format) {
this[kType] = 'CompressionStream';
switch (format) {
case 'deflate':
this[kHandle] = lazyZlib().createDeflate();
break;
case 'gzip':
this[kHandle] = lazyZlib().createGzip();
break;
default:
throw new ERR_INVALID_ARG_VALUE('format', format);
}
this[kTransform] = newReadableWritablePairFromDuplex(this[kHandle]);
}

/**
* @readonly
* @type {ReadableStream}
*/
get readable() {
if (!isCompressionStream(this))
throw new ERR_INVALID_THIS('CompressionStream');
return this[kTransform].readable;
}

/**
* @readonly
* @type {WritableStream}
*/
get writable() {
if (!isCompressionStream(this))
throw new ERR_INVALID_THIS('CompressionStream');
return this[kTransform].writable;
}

[kInspect](depth, options) {
if (!isCompressionStream(this))
throw new ERR_INVALID_THIS('CompressionStream');
customInspect(depth, options, 'CompressionStream', {
readable: this[kTransform].readable,
writable: this[kTransform].writable,
});
}
}

class DecompressionStream {
/**
* @param {'deflate'|'gzip'} format
*/
constructor(format) {
this[kType] = 'DecompressionStream';
switch (format) {
case 'deflate':
this[kHandle] = lazyZlib().createInflate();
break;
case 'gzip':
this[kHandle] = lazyZlib().createGunzip();
break;
default:
throw new ERR_INVALID_ARG_VALUE('format', format);
}
this[kTransform] = newReadableWritablePairFromDuplex(this[kHandle]);
}

/**
* @readonly
* @type {ReadableStream}
*/
get readable() {
if (!isDecompressionStream(this))
throw new ERR_INVALID_THIS('DecompressionStream');
return this[kTransform].readable;
}

/**
* @readonly
* @type {WritableStream}
*/
get writable() {
if (!isDecompressionStream(this))
throw new ERR_INVALID_THIS('DecompressionStream');
return this[kTransform].writable;
}

[kInspect](depth, options) {
if (!isDecompressionStream(this))
throw new ERR_INVALID_THIS('DecompressionStream');
customInspect(depth, options, 'DecompressionStream', {
readable: this[kTransform].readable,
writable: this[kTransform].writable,
});
}
}

ObjectDefineProperties(CompressionStream.prototype, {
readable: kEnumerableProperty,
writable: kEnumerableProperty,
});

ObjectDefineProperties(DecompressionStream.prototype, {
readable: kEnumerableProperty,
writable: kEnumerableProperty,
});

module.exports = {
CompressionStream,
DecompressionStream,
};
8 changes: 3 additions & 5 deletions lib/internal/webstreams/encoding.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ const {
TransformStream,
} = require('internal/webstreams/transformstream');

const {
customInspect,
kEnumerableProperty,
} = require('internal/webstreams/util');
const { customInspect } = require('internal/webstreams/util');

const {
codes: {
Expand All @@ -26,7 +23,8 @@ const {
} = require('internal/errors');

const {
customInspectSymbol: kInspect
customInspectSymbol: kInspect,
kEnumerableProperty,
} = require('internal/util');

const kHandle = Symbol('kHandle');
Expand Down
Loading

0 comments on commit b1f0cc6

Please sign in to comment.