Bug 1947431 - Add "zstd" as a CompressionFormat r=saschanaz,webidl

This patch adds a new variant for "zstd" to the CompressionFormat
enum in WebIDL. It also adds a couple Mozilla-specific WPTs to ensure
that the construction of CompressionStream and DecompressionStream
objects with this format are restricted to the correct contexts.

Differential Revision: https://phabricator.services.mozilla.com/D238135
This commit is contained in:
Erik Nordin
2025-03-03 21:26:52 +00:00
parent 7a96584917
commit 681c5f3692
5 changed files with 85 additions and 0 deletions

View File

@@ -228,6 +228,13 @@ JSObject* CompressionStream::WrapObject(JSContext* aCx,
// https://wicg.github.io/compression/#dom-compressionstream-compressionstream
already_AddRefed<CompressionStream> CompressionStream::Constructor(
const GlobalObject& aGlobal, CompressionFormat aFormat, ErrorResult& aRv) {
if (aFormat == CompressionFormat::Zstd) {
aRv.ThrowTypeError(
"'zstd' (value of argument 1) is not a valid value for enumeration "
"CompressionFormat.");
return nullptr;
}
// Step 1: If format is unsupported in CompressionStream, then throw a
// TypeError.
// XXX: Skipped as we are using enum for this

View File

@@ -270,6 +270,14 @@ JSObject* DecompressionStream::WrapObject(JSContext* aCx,
// https://wicg.github.io/compression/#dom-decompressionstream-decompressionstream
already_AddRefed<DecompressionStream> DecompressionStream::Constructor(
const GlobalObject& aGlobal, CompressionFormat aFormat, ErrorResult& aRv) {
if (aFormat == CompressionFormat::Zstd &&
aGlobal.CallerType() != CallerType::System) {
aRv.ThrowTypeError(
"'zstd' (value of argument 1) is not a valid value for enumeration "
"CompressionFormat.");
return nullptr;
}
// Step 1: If format is unsupported in DecompressionStream, then throw a
// TypeError.
// XXX: Skipped as we are using enum for this

View File

@@ -11,6 +11,7 @@ enum CompressionFormat {
"deflate",
"deflate-raw",
"gzip",
"zstd",
};
[Exposed=*]

View File

@@ -0,0 +1,34 @@
// META: global=window
"use strict";
test(t => {
assert_throws_js(
TypeError,
() => new CompressionStream("zstd"),
'Constructor given "zstd" should throw'
);
}, '"zstd" should be an invalid CompressionStream option');
promise_test(async t => {
const constructorFailed = await SpecialPowers.spawnChrome([], () => {
let cs;
try {
cs = new this.browsingContext.topChromeWindow.CompressionStream("zstd");
} catch {
return true;
}
if (cs) {
throw new Error(
'Constructed a CompressionStream with "zstd" format, when that should be disallowed'
);
}
});
assert_true(
constructorFailed,
'Constructor given "zstd" should throw, even in a privileged context'
);
}, '"zstd" should be an invalid CompressionStream format, even in a privileged context');

View File

@@ -0,0 +1,35 @@
// META: global=window
"use strict";
test(t => {
assert_throws_js(
TypeError,
() => new DecompressionStream("zstd"),
'Constructor given "zstd" should throw.'
);
}, '"zstd" should be an invalid DecompressionStream format in an unprivileged context.');
promise_test(async t => {
const constructorSucceeded = await SpecialPowers.spawnChrome(
[],
function create_decompression_stream_in_chrome_context() {
const ds = new this.browsingContext.topChromeWindow.DecompressionStream(
"zstd"
);
if (!ds) {
throw new Error(
'Failed to construct DecompressionStream with "zstd" format in privileged context.'
);
}
return true;
}
);
assert_true(
constructorSucceeded,
'Constructor given "zstd" should succeed in a privileged context'
);
}, '"zstd" should be a valid DecompressionStream format in a privileged context.');