Skip to content

Commit 7ebc8c2

Browse files
panvatargos
authored andcommitted
test,stream: enable compression WPTs
PR-URL: #50631 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
1 parent 0bd694a commit 7ebc8c2

33 files changed

+1448
-7
lines changed

‎test/common/wpt.js‎

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ class ResourceLoader{
210210
constdata=awaitfsPromises.readFile(file);
211211
return{
212212
ok: true,
213+
arrayBuffer(){returndata.buffer;},
213214
json(){returnJSON.parse(data.toString());},
214215
text(){returndata.toString();},
215216
};
@@ -382,7 +383,7 @@ const kIntlRequirement ={
382383
// TODO(joyeecheung): we may need to deal with --with-intl=system-icu
383384
};
384385

385-
classIntlRequirement{
386+
classBuildRequirement{
386387
constructor(){
387388
this.currentIntl=kIntlRequirement.none;
388389
if(process.config.variables.v8_enable_i18n_support===0){
@@ -395,6 +396,9 @@ class IntlRequirement{
395396
}else{
396397
this.currentIntl=kIntlRequirement.full;
397398
}
399+
// Not using common.hasCrypto because of the global leak checks
400+
this.hasCrypto=Boolean(process.versions.openssl)&&
401+
!process.env.NODE_SKIP_CRYPTO;
398402
}
399403

400404
/**
@@ -409,11 +413,14 @@ class IntlRequirement{
409413
if(requires.has('small-icu')&&current<kIntlRequirement.small){
410414
return'small-icu';
411415
}
416+
if(requires.has('crypto')&&!this.hasCrypto){
417+
return'crypto';
418+
}
412419
returnfalse;
413420
}
414421
}
415422

416-
constintlRequirements=newIntlRequirement();
423+
constbuildRequirements=newBuildRequirement();
417424

418425
classStatusLoader{
419426
/**
@@ -440,7 +447,7 @@ class StatusLoader{
440447
constlist=this.grep(filepath);
441448
result=result.concat(list);
442449
}else{
443-
if(!(/\.\w+\.js$/.test(filepath))||filepath.endsWith('.helper.js')){
450+
if(!(/\.\w+\.js$/.test(filepath))){
444451
continue;
445452
}
446453
result.push(filepath);
@@ -945,9 +952,9 @@ class WPTRunner{
945952
continue;
946953
}
947954

948-
constlackingIntl=intlRequirements.isLacking(spec.requires);
949-
if(lackingIntl){
950-
this.skip(spec,[`requires ${lackingIntl}`]);
955+
constlackingSupport=buildRequirements.isLacking(spec.requires);
956+
if(lackingSupport){
957+
this.skip(spec,[`requires ${lackingSupport}`]);
951958
continue;
952959
}
953960

‎test/fixtures/wpt/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ See [test/wpt](../../wpt/README.md) for information on how these tests are run.
1111
Last update:
1212

1313
- common: https://github.com/web-platform-tests/wpt/tree/dbd648158d/common
14+
- compression: https://github.com/web-platform-tests/wpt/tree/c82521cfa5/compression
1415
- console: https://github.com/web-platform-tests/wpt/tree/767ae35464/console
1516
- dom/abort: https://github.com/web-platform-tests/wpt/tree/d1f1ecbd52/dom/abort
1617
- dom/events: https://github.com/web-platform-tests/wpt/tree/ab8999891c/dom/events
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
spec: https://wicg.github.io/compression/
2+
suggested_reviewers:
3+
- ricea
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// META: global=window,worker,shadowrealm
2+
3+
'use strict';
4+
5+
constbadChunks=[
6+
{
7+
name: 'undefined',
8+
value: undefined
9+
},
10+
{
11+
name: 'null',
12+
value: null
13+
},
14+
{
15+
name: 'numeric',
16+
value: 3.14
17+
},
18+
{
19+
name: 'object, not BufferSource',
20+
value: {}
21+
},
22+
{
23+
name: 'array',
24+
value: [65]
25+
},
26+
{
27+
name: 'SharedArrayBuffer',
28+
// Use a getter to postpone construction so that all tests don't fail where
29+
// SharedArrayBuffer is not yet implemented.
30+
getvalue(){
31+
// See https://github.com/whatwg/html/issues/5380 for why not `new SharedArrayBuffer()`
32+
returnnewWebAssembly.Memory({shared:true,initial:1,maximum:1}).buffer;
33+
}
34+
},
35+
{
36+
name: 'shared Uint8Array',
37+
getvalue(){
38+
// See https://github.com/whatwg/html/issues/5380 for why not `new SharedArrayBuffer()`
39+
returnnewUint8Array(newWebAssembly.Memory({shared:true,initial:1,maximum:1}).buffer)
40+
}
41+
},
42+
];
43+
44+
for(constchunkofbadChunks){
45+
promise_test(asynct=>{
46+
constcs=newCompressionStream('gzip');
47+
constreader=cs.readable.getReader();
48+
constwriter=cs.writable.getWriter();
49+
constwritePromise=writer.write(chunk.value);
50+
constreadPromise=reader.read();
51+
awaitpromise_rejects_js(t,TypeError,writePromise,'write should reject');
52+
awaitpromise_rejects_js(t,TypeError,readPromise,'read should reject');
53+
},`chunk of type ${chunk.name} should error the stream for gzip`);
54+
55+
promise_test(asynct=>{
56+
constcs=newCompressionStream('deflate');
57+
constreader=cs.readable.getReader();
58+
constwriter=cs.writable.getWriter();
59+
constwritePromise=writer.write(chunk.value);
60+
constreadPromise=reader.read();
61+
awaitpromise_rejects_js(t,TypeError,writePromise,'write should reject');
62+
awaitpromise_rejects_js(t,TypeError,readPromise,'read should reject');
63+
},`chunk of type ${chunk.name} should error the stream for deflate`);
64+
65+
promise_test(asynct=>{
66+
constcs=newCompressionStream('deflate-raw');
67+
constreader=cs.readable.getReader();
68+
constwriter=cs.writable.getWriter();
69+
constwritePromise=writer.write(chunk.value);
70+
constreadPromise=reader.read();
71+
awaitpromise_rejects_js(t,TypeError,writePromise,'write should reject');
72+
awaitpromise_rejects_js(t,TypeError,readPromise,'read should reject');
73+
},`chunk of type ${chunk.name} should error the stream for deflate-raw`);
74+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// META: global=window,worker,shadowrealm
2+
3+
'use strict';
4+
5+
test(t=>{
6+
assert_throws_js(TypeError,()=>newCompressionStream('a'),'constructor should throw');
7+
},'"a" should cause the constructor to throw');
8+
9+
test(t=>{
10+
assert_throws_js(TypeError,()=>newCompressionStream(),'constructor should throw');
11+
},'no input should cause the constructor to throw');
12+
13+
test(t=>{
14+
assert_throws_js(Error,()=>newCompressionStream({toString(){throwError();}}),'constructor should throw');
15+
},'non-string input should cause the constructor to throw');
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// META: global=window,worker,shadowrealm
2+
// META: script=third_party/pako/pako_inflate.min.js
3+
// META: timeout=long
4+
5+
'use strict';
6+
7+
// This test asserts that compressing '' doesn't affect the compressed data.
8+
// Example: compressing ['Hello', '', 'Hello'] results in 'HelloHello'
9+
10+
asyncfunctioncompressChunkList(chunkList,format){
11+
constcs=newCompressionStream(format);
12+
constwriter=cs.writable.getWriter();
13+
for(constchunkofchunkList){
14+
constchunkByte=newTextEncoder().encode(chunk);
15+
writer.write(chunkByte);
16+
}
17+
constclosePromise=writer.close();
18+
constout=[];
19+
constreader=cs.readable.getReader();
20+
lettotalSize=0;
21+
while(true){
22+
const{ value, done }=awaitreader.read();
23+
if(done)
24+
break;
25+
out.push(value);
26+
totalSize+=value.byteLength;
27+
}
28+
awaitclosePromise;
29+
constconcatenated=newUint8Array(totalSize);
30+
letoffset=0;
31+
for(constarrayofout){
32+
concatenated.set(array,offset);
33+
offset+=array.byteLength;
34+
}
35+
returnconcatenated;
36+
}
37+
38+
constchunkLists=[
39+
['','Hello','Hello'],
40+
['Hello','','Hello'],
41+
['Hello','Hello','']
42+
];
43+
constexpectedValue=newTextEncoder().encode('HelloHello');
44+
45+
for(constchunkListofchunkLists){
46+
promise_test(asynct=>{
47+
constcompressedData=awaitcompressChunkList(chunkList,'deflate');
48+
// decompress with pako, and check that we got the same result as our original string
49+
assert_array_equals(expectedValue,pako.inflate(compressedData),'value should match');
50+
},`the result of compressing [${chunkList}] with deflate should be 'HelloHello'`);
51+
52+
promise_test(asynct=>{
53+
constcompressedData=awaitcompressChunkList(chunkList,'gzip');
54+
// decompress with pako, and check that we got the same result as our original string
55+
assert_array_equals(expectedValue,pako.inflate(compressedData),'value should match');
56+
},`the result of compressing [${chunkList}] with gzip should be 'HelloHello'`);
57+
58+
promise_test(asynct=>{
59+
constcompressedData=awaitcompressChunkList(chunkList,'deflate-raw');
60+
// decompress with pako, and check that we got the same result as our original string
61+
assert_array_equals(expectedValue,pako.inflateRaw(compressedData),'value should match');
62+
},`the result of compressing [${chunkList}] with deflate-raw should be 'HelloHello'`);
63+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// META: global=window,worker,shadowrealm
2+
// META: script=third_party/pako/pako_inflate.min.js
3+
// META: script=resources/concatenate-stream.js
4+
// META: timeout=long
5+
6+
'use strict';
7+
8+
// This test verifies that a large flush output will not truncate the
9+
// final results.
10+
11+
asyncfunctioncompressData(chunk,format){
12+
constcs=newCompressionStream(format);
13+
constwriter=cs.writable.getWriter();
14+
writer.write(chunk);
15+
writer.close();
16+
returnawaitconcatenateStream(cs.readable);
17+
}
18+
19+
// JSON-encoded array of 10 thousands numbers ("[0,1,2,...]"). This produces 48_891 bytes of data.
20+
constfullData=newTextEncoder().encode(JSON.stringify(Array.from({length: 10_000},(_,i)=>i)));
21+
constdata=fullData.subarray(0,35_579);
22+
constexpectedValue=data;
23+
24+
promise_test(asynct=>{
25+
constcompressedData=awaitcompressData(data,'deflate');
26+
// decompress with pako, and check that we got the same result as our original string
27+
assert_array_equals(expectedValue,pako.inflate(compressedData),'value should match');
28+
},`deflate compression with large flush output`);
29+
30+
promise_test(asynct=>{
31+
constcompressedData=awaitcompressData(data,'gzip');
32+
// decompress with pako, and check that we got the same result as our original string
33+
assert_array_equals(expectedValue,pako.inflate(compressedData),'value should match');
34+
},`gzip compression with large flush output`);
35+
36+
promise_test(asynct=>{
37+
constcompressedData=awaitcompressData(data,'deflate-raw');
38+
// decompress with pako, and check that we got the same result as our original string
39+
assert_array_equals(expectedValue,pako.inflateRaw(compressedData),'value should match');
40+
},`deflate-raw compression with large flush output`);
41+
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// META: global=window,worker,shadowrealm
2+
// META: script=third_party/pako/pako_inflate.min.js
3+
// META: timeout=long
4+
5+
'use strict';
6+
7+
// This test asserts that compressing multiple chunks should work.
8+
9+
// Example: ('Hello', 3) => TextEncoder().encode('HelloHelloHello')
10+
functionmakeExpectedChunk(input,numberOfChunks){
11+
constexpectedChunk=input.repeat(numberOfChunks);
12+
returnnewTextEncoder().encode(expectedChunk);
13+
}
14+
15+
// Example: ('Hello', 3, 'deflate') => compress ['Hello', 'Hello', Hello']
16+
asyncfunctioncompressMultipleChunks(input,numberOfChunks,format){
17+
constcs=newCompressionStream(format);
18+
constwriter=cs.writable.getWriter();
19+
constchunk=newTextEncoder().encode(input);
20+
for(leti=0;i<numberOfChunks;++i){
21+
writer.write(chunk);
22+
}
23+
constclosePromise=writer.close();
24+
constout=[];
25+
constreader=cs.readable.getReader();
26+
lettotalSize=0;
27+
while(true){
28+
const{ value, done }=awaitreader.read();
29+
if(done)
30+
break;
31+
out.push(value);
32+
totalSize+=value.byteLength;
33+
}
34+
awaitclosePromise;
35+
constconcatenated=newUint8Array(totalSize);
36+
letoffset=0;
37+
for(constarrayofout){
38+
concatenated.set(array,offset);
39+
offset+=array.byteLength;
40+
}
41+
returnconcatenated;
42+
}
43+
44+
consthello='Hello';
45+
46+
for(letnumberOfChunks=2;numberOfChunks<=16;++numberOfChunks){
47+
promise_test(asynct=>{
48+
constcompressedData=awaitcompressMultipleChunks(hello,numberOfChunks,'deflate');
49+
constexpectedValue=makeExpectedChunk(hello,numberOfChunks);
50+
// decompress with pako, and check that we got the same result as our original string
51+
assert_array_equals(expectedValue,pako.inflate(compressedData),'value should match');
52+
},`compressing ${numberOfChunks} chunks with deflate should work`);
53+
54+
promise_test(asynct=>{
55+
constcompressedData=awaitcompressMultipleChunks(hello,numberOfChunks,'gzip');
56+
constexpectedValue=makeExpectedChunk(hello,numberOfChunks);
57+
// decompress with pako, and check that we got the same result as our original string
58+
assert_array_equals(expectedValue,pako.inflate(compressedData),'value should match');
59+
},`compressing ${numberOfChunks} chunks with gzip should work`);
60+
61+
promise_test(asynct=>{
62+
constcompressedData=awaitcompressMultipleChunks(hello,numberOfChunks,'deflate-raw');
63+
constexpectedValue=makeExpectedChunk(hello,numberOfChunks);
64+
// decompress with pako, and check that we got the same result as our original string
65+
assert_array_equals(expectedValue,pako.inflateRaw(compressedData),'value should match');
66+
},`compressing ${numberOfChunks} chunks with deflate-raw should work`);
67+
}

0 commit comments

Comments
(0)