-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement compression for "gzip" and "deflate". The implementation is hidden behind the ExperimentalCompressionStream flag. For unit tests, we compress and stream and uncompress it with pako, and we make sure its output is the same as our original data. The original CL was reverted in https://chromium-review.googlesource.com/c/chromium/src/+/1849376 due to failures on the MSAN and leak bots. The compression-stream.any.js test has been modified from the original version of this CL to improve performance, and also marked as slow. Bug: 999091 Change-Id: I3952f500cd1ea614397fdf47771e037546583a16
- Loading branch information
1 parent
7c15d5b
commit 0fa438b
Showing
4 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// META: global=worker | ||
// META: script=pako/pako_inflate.min.js | ||
// META: timeout=long | ||
|
||
'use strict'; | ||
|
||
const SMALL_FILE = "/media/foo.vtt"; | ||
const LARGE_FILE = "/media/test-av-384k-44100Hz-1ch-320x240-30fps-10kfr.webm"; | ||
|
||
async function compressArrayBuffer(input, format) { | ||
const cs = new CompressionStream(format); | ||
const writer = cs.writable.getWriter(); | ||
writer.write(input); | ||
const closePromise = writer.close(); | ||
const out = []; | ||
const reader = cs.readable.getReader(); | ||
let totalSize = 0; | ||
while (true) { | ||
const { value, done } = await reader.read(); | ||
if (done) | ||
break; | ||
out.push(value); | ||
totalSize += value.byteLength; | ||
} | ||
await closePromise; | ||
const concatenated = new Uint8Array(totalSize); | ||
let offset = 0; | ||
for (const array of out) { | ||
concatenated.set(array, offset); | ||
offset += array.byteLength; | ||
} | ||
return concatenated; | ||
} | ||
|
||
test(() => { | ||
assert_throws(new TypeError(), () => { | ||
const transformer = new CompressionStream("nonvalid"); | ||
}, "non supported format should throw"); | ||
}, "CompressionStream constructor should throw on invalid format"); | ||
|
||
promise_test(async () => { | ||
const buffer = new ArrayBuffer(0); | ||
const bufferView = new Uint8Array(buffer); | ||
const compressedData = await compressArrayBuffer(bufferView, "deflate"); | ||
// decompress with pako, and check that we got the same result as our original string | ||
assert_array_equals(bufferView, pako.inflate(compressedData)); | ||
}, "deflated empty data should be reinflated back to its origin"); | ||
|
||
promise_test(async () => { | ||
const response = await fetch(SMALL_FILE) | ||
const buffer = await response.arrayBuffer(); | ||
const bufferView = new Uint8Array(buffer); | ||
const compressedData = await compressArrayBuffer(bufferView, "deflate"); | ||
// decompress with pako, and check that we got the same result as our original string | ||
assert_array_equals(bufferView, pako.inflate(compressedData)); | ||
}, "deflated small amount data should be reinflated back to its origin"); | ||
|
||
promise_test(async () => { | ||
const response = await fetch(LARGE_FILE) | ||
const buffer = await response.arrayBuffer(); | ||
const bufferView = new Uint8Array(buffer); | ||
const compressedData = await compressArrayBuffer(bufferView, "deflate"); | ||
// decompress with pako, and check that we got the same result as our original string | ||
assert_array_equals(bufferView, pako.inflate(compressedData)); | ||
}, "deflated large amount data should be reinflated back to its origin"); | ||
|
||
promise_test(async () => { | ||
const buffer = new ArrayBuffer(0); | ||
const bufferView = new Uint8Array(buffer); | ||
const compressedData = await compressArrayBuffer(bufferView, "gzip"); | ||
// decompress with pako, and check that we got the same result as our original string | ||
assert_array_equals(bufferView, pako.inflate(compressedData)); | ||
}, "gzipped empty data should be reinflated back to its origin"); | ||
|
||
promise_test(async () => { | ||
const response = await fetch(SMALL_FILE) | ||
const buffer = await response.arrayBuffer(); | ||
const bufferView = new Uint8Array(buffer); | ||
const compressedData = await compressArrayBuffer(bufferView, "gzip"); | ||
// decompress with pako, and check that we got the same result as our original string | ||
assert_array_equals(bufferView, pako.inflate(compressedData)); | ||
}, "gzipped small amount data should be reinflated back to its origin"); | ||
|
||
promise_test(async () => { | ||
const response = await fetch(LARGE_FILE) | ||
const buffer = await response.arrayBuffer(); | ||
const bufferView = new Uint8Array(buffer); | ||
const compressedData = await compressArrayBuffer(bufferView, "gzip"); | ||
// decompress with pako, and check that we got the same result as our original string | ||
assert_array_equals(bufferView, pako.inflate(compressedData)); | ||
}, "gzipped large amount data should be reinflated back to its origin"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
(The MIT License) | ||
|
||
Copyright (C) 2014-2017 by Vitaly Puzrin and Andrei Tuputcyn | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
original repository: | ||
https://github.com/nodeca/pako |
Oops, something went wrong.