Skip to content

Commit 146ad9c

Browse files
panvatargos
authored andcommitted
stream: treat compression web stream format per its WebIDL definition
PR-URL: #50631 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
1 parent 7ebc8c2 commit 146ad9c

File tree

4 files changed

+60
-59
lines changed

4 files changed

+60
-59
lines changed

lib/internal/crypto/webidl.js

+5-36
Original file line numberDiff line numberDiff line change
@@ -19,44 +19,29 @@ const {
1919
MathTrunc,
2020
Number,
2121
NumberIsFinite,
22-
ObjectAssign,
2322
ObjectPrototypeIsPrototypeOf,
2423
SafeArrayIterator,
25-
SafeSet,
2624
String,
2725
SymbolIterator,
2826
TypedArrayPrototypeGetBuffer,
2927
TypedArrayPrototypeGetSymbolToStringTag,
30-
TypeError,
3128
globalThis: {
3229
SharedArrayBuffer,
3330
},
3431
} = primordials;
3532

33+
const {
34+
makeException,
35+
createEnumConverter,
36+
} = require('internal/webidl');
37+
3638
const {
3739
kEmptyObject,
3840
setOwnProperty,
3941
} = require('internal/util');
4042
const { CryptoKey } = require('internal/crypto/webcrypto');
4143
const { getDataViewOrTypedArrayBuffer } = require('internal/crypto/util');
4244

43-
function codedTypeError(message, errorProperties = kEmptyObject) {
44-
// eslint-disable-next-line no-restricted-syntax
45-
const err = new TypeError(message);
46-
ObjectAssign(err, errorProperties);
47-
return err;
48-
}
49-
50-
function makeException(message, opts = kEmptyObject) {
51-
const prefix = opts.prefix ? opts.prefix + ': ' : '';
52-
const context = opts.context?.length === 0 ?
53-
'' : (opts.context ?? 'Value') + ' ';
54-
return codedTypeError(
55-
`${prefix}${context}${message}`,
56-
{ code: opts.code || 'ERR_INVALID_ARG_TYPE' },
57-
);
58-
}
59-
6045
// https://tc39.es/ecma262/#sec-tonumber
6146
function toNumber(value, opts = kEmptyObject) {
6247
switch (typeof value) {
@@ -308,22 +293,6 @@ function createDictionaryConverter(name, dictionaries) {
308293
};
309294
}
310295

311-
function createEnumConverter(name, values) {
312-
const E = new SafeSet(values);
313-
314-
return function(V, opts = kEmptyObject) {
315-
const S = String(V);
316-
317-
if (!E.has(S)) {
318-
throw makeException(
319-
`value '${S}' is not a valid enum value of type ${name}.`,
320-
{ __proto__: null, ...opts, code: 'ERR_INVALID_ARG_VALUE' });
321-
}
322-
323-
return S;
324-
};
325-
}
326-
327296
function createSequenceConverter(converter) {
328297
return function(V, opts = kEmptyObject) {
329298
if (type(V) !== 'Object') {

lib/internal/webidl.js

+39-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ const {
1010
NumberIsNaN,
1111
NumberMAX_SAFE_INTEGER,
1212
NumberMIN_SAFE_INTEGER,
13+
ObjectAssign,
14+
SafeSet,
1315
String,
16+
TypeError,
1417
} = primordials;
1518

1619
const {
@@ -173,8 +176,43 @@ converters.DOMString = function DOMString(V) {
173176
return String(V);
174177
};
175178

179+
function codedTypeError(message, errorProperties = kEmptyObject) {
180+
// eslint-disable-next-line no-restricted-syntax
181+
const err = new TypeError(message);
182+
ObjectAssign(err, errorProperties);
183+
return err;
184+
}
185+
186+
function makeException(message, opts = kEmptyObject) {
187+
const prefix = opts.prefix ? opts.prefix + ': ' : '';
188+
const context = opts.context?.length === 0 ?
189+
'' : (opts.context ?? 'Value') + ' ';
190+
return codedTypeError(
191+
`${prefix}${context}${message}`,
192+
{ code: opts.code || 'ERR_INVALID_ARG_TYPE' },
193+
);
194+
}
195+
196+
function createEnumConverter(name, values) {
197+
const E = new SafeSet(values);
198+
199+
return function(V, opts = kEmptyObject) {
200+
const S = String(V);
201+
202+
if (!E.has(S)) {
203+
throw makeException(
204+
`value '${S}' is not a valid enum value of type ${name}.`,
205+
{ __proto__: null, ...opts, code: 'ERR_INVALID_ARG_VALUE' });
206+
}
207+
208+
return S;
209+
};
210+
}
211+
176212
module.exports = {
213+
converters,
177214
convertToInt,
215+
createEnumConverter,
178216
evenRound,
179-
converters,
217+
makeException,
180218
};

lib/internal/webstreams/compression.js

+16-8
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@ const {
44
ObjectDefineProperties,
55
} = primordials;
66

7-
const {
8-
codes: { ERR_INVALID_ARG_VALUE },
9-
} = require('internal/errors');
10-
117
const {
128
newReadableWritablePairFromDuplex,
139
} = require('internal/webstreams/adapters');
@@ -19,12 +15,20 @@ const {
1915
kEnumerableProperty,
2016
} = require('internal/util');
2117

18+
const { createEnumConverter } = require('internal/webidl');
19+
2220
let zlib;
2321
function lazyZlib() {
2422
zlib ??= require('zlib');
2523
return zlib;
2624
}
2725

26+
const formatConverter = createEnumConverter('CompressionFormat', [
27+
'deflate',
28+
'deflate-raw',
29+
'gzip',
30+
]);
31+
2832
/**
2933
* @typedef {import('./readablestream').ReadableStream} ReadableStream
3034
* @typedef {import('./writablestream').WritableStream} WritableStream
@@ -38,6 +42,10 @@ class CompressionStream {
3842
* @param {'deflate'|'deflate-raw'|'gzip'} format
3943
*/
4044
constructor(format) {
45+
format = formatConverter(format, {
46+
prefix: "Failed to construct 'CompressionStream'",
47+
context: '1st argument',
48+
});
4149
switch (format) {
4250
case 'deflate':
4351
this.#handle = lazyZlib().createDeflate();
@@ -48,8 +56,6 @@ class CompressionStream {
4856
case 'gzip':
4957
this.#handle = lazyZlib().createGzip();
5058
break;
51-
default:
52-
throw new ERR_INVALID_ARG_VALUE('format', format);
5359
}
5460
this.#transform = newReadableWritablePairFromDuplex(this.#handle);
5561
}
@@ -86,6 +92,10 @@ class DecompressionStream {
8692
* @param {'deflate'|'deflate-raw'|'gzip'} format
8793
*/
8894
constructor(format) {
95+
format = formatConverter(format, {
96+
prefix: "Failed to construct 'DecompressionStream'",
97+
context: '1st argument',
98+
});
8999
switch (format) {
90100
case 'deflate':
91101
this.#handle = lazyZlib().createInflate();
@@ -96,8 +106,6 @@ class DecompressionStream {
96106
case 'gzip':
97107
this.#handle = lazyZlib().createGunzip();
98108
break;
99-
default:
100-
throw new ERR_INVALID_ARG_VALUE('format', format);
101109
}
102110
this.#transform = newReadableWritablePairFromDuplex(this.#handle);
103111
}

test/wpt/status/compression.json

-14
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,12 @@
22
"compression-bad-chunks.tentative.any.js": {
33
"skip": "Execution \"hangs\", ArrayBuffer and TypedArray is not accepted and throws, instead of rejects during writer.write"
44
},
5-
"compression-constructor-error.tentative.any.js": {
6-
"fail": {
7-
"expected": [
8-
"non-string input should cause the constructor to throw"
9-
]
10-
}
11-
},
125
"decompression-bad-chunks.tentative.any.js": {
136
"skip": "Execution \"hangs\", ArrayBuffer and TypedArray is not accepted and throws, instead of rejects during writer.write"
147
},
158
"decompression-buffersource.tentative.any.js": {
169
"skip": "ArrayBuffer and TypedArray is not accepted and throws, instead of rejects during writer.write"
1710
},
18-
"decompression-constructor-error.tentative.any.js": {
19-
"fail": {
20-
"expected": [
21-
"non-string input should cause the constructor to throw"
22-
]
23-
}
24-
},
2511
"compression-with-detach.tentative.window.js": {
2612
"requires": ["crypto"]
2713
},

0 commit comments

Comments
 (0)