GeezerVison is showing github.com — stream: accept ArrayBuffer in CompressionStream and DecompressionStream · nodejs/node@f90fa9c
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/internal/webstreams/adapters.js +7Lines changed: 7 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -53,6 +53,10 @@ const { | 53 | 53 | Buffer, | 54 | 54 | } = require('buffer'); | 55 | 55 |
| | 56 | +const { | | 57 | + isArrayBuffer, | | 58 | +} = require('internal/util/types'); | | 59 | + | 56 | 60 | const { | 57 | 61 | AbortError, | 58 | 62 | ErrnoException, | @@ -213,6 +217,9 @@ function newWritableStreamFromStreamWritable(streamWritable) { | 213 | 217 | start(c) { controller = c; }, | 214 | 218 |
| 215 | 219 | write(chunk) { | | 220 | + if (!streamWritable.writableObjectMode && isArrayBuffer(chunk)) { | | 221 | + chunk = new Uint8Array(chunk); | | 222 | + } | 216 | 223 | if (streamWritable.writableNeedDrain || !streamWritable.write(chunk)) { | 217 | 224 | backpressurePromise = PromiseWithResolvers(); | 218 | 225 | return SafePromisePrototypeFinally( | |
Collapse file Copy file name to clipboard +42Lines changed: 42 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +'use strict'; | | 2 | +require('../common'); | | 3 | +const assert = require('assert'); | | 4 | +const test = require('node:test'); | | 5 | +const { DecompressionStream, CompressionStream } = require('stream/web'); | | 6 | + | | 7 | +// Minimal gzip-compressed bytes for "hello" | | 8 | +const compressedGzip = new Uint8Array([ | | 9 | + 31, 139, 8, 0, 0, 0, 0, 0, 0, 3, | | 10 | + 203, 72, 205, 201, 201, 7, 0, 134, 166, 16, 54, 5, 0, 0, 0, | | 11 | +]); | | 12 | + | | 13 | +test('DecompressionStream accepts ArrayBuffer chunks', async () => { | | 14 | + const ds = new DecompressionStream('gzip'); | | 15 | + const writer = ds.writable.getWriter(); | | 16 | + | | 17 | + const writePromise = writer.write(compressedGzip.buffer); | | 18 | + writer.close(); | | 19 | + | | 20 | + const chunks = await Array.fromAsync(ds.readable); | | 21 | + await writePromise; | | 22 | + const out = Buffer.concat(chunks.map((c) => Buffer.from(c))); | | 23 | + assert.strictEqual(out.toString(), 'hello'); | | 24 | +}); | | 25 | + | | 26 | +test('CompressionStream round-trip with ArrayBuffer input', async () => { | | 27 | + const cs = new CompressionStream('gzip'); | | 28 | + const ds = new DecompressionStream('gzip'); | | 29 | + | | 30 | + const csWriter = cs.writable.getWriter(); | | 31 | + | | 32 | + const input = new TextEncoder().encode('hello').buffer; | | 33 | + | | 34 | + await csWriter.write(input); | | 35 | + csWriter.close(); | | 36 | + | | 37 | + await cs.readable.pipeTo(ds.writable); | | 38 | + | | 39 | + const out = await Array.fromAsync(ds.readable); | | 40 | + const result = Buffer.concat(out.map((c) => Buffer.from(c))); | | 41 | + assert.strictEqual(result.toString(), 'hello'); | | 42 | +}); |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-whatwg-webstreams-adapters-to-writablestream.js +32Lines changed: 32 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -165,3 +165,35 @@ class TestWritable extends Writable { | 165 | 165 | const writer = writableStream.getWriter(); | 166 | 166 | writer.closed.then(common.mustCall()); | 167 | 167 | } | | 168 | + | | 169 | +{ | | 170 | + const duplex = new PassThrough(); | | 171 | + const writableStream = newWritableStreamFromStreamWritable(duplex); | | 172 | + const ec = new TextEncoder(); | | 173 | + const arrayBuffer = ec.encode('hello').buffer; | | 174 | + writableStream | | 175 | + .getWriter() | | 176 | + .write(arrayBuffer) | | 177 | + .then(common.mustCall()); | | 178 | + | | 179 | + duplex.on('data', common.mustCall((chunk) => { | | 180 | + assert(chunk instanceof Buffer); | | 181 | + assert(chunk.equals(Buffer.from('hello'))); | | 182 | + })); | | 183 | +} | | 184 | + | | 185 | +{ | | 186 | + const duplex = new PassThrough({ objectMode: true }); | | 187 | + const writableStream = newWritableStreamFromStreamWritable(duplex); | | 188 | + const ec = new TextEncoder(); | | 189 | + const arrayBuffer = ec.encode('hello').buffer; | | 190 | + writableStream | | 191 | + .getWriter() | | 192 | + .write(arrayBuffer) | | 193 | + .then(common.mustCall()); | | 194 | + | | 195 | + duplex.on('data', common.mustCall((chunk) => { | | 196 | + assert(chunk instanceof ArrayBuffer); | | 197 | + assert.strictEqual(chunk, arrayBuffer); | | 198 | + })); | | 199 | +} |
Collapse file Copy file name to clipboardExpand all lines: test/wpt/status/compression.json -3Lines changed: 0 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 5 | 5 | "decompression-bad-chunks.tentative.any.js": { | 6 | 6 | "skip": "Execution \"hangs\", ArrayBuffer and TypedArray is not accepted and throws, instead of rejects during writer.write" | 7 | 7 | }, | 8 | | - "decompression-buffersource.tentative.any.js": { | 9 | | - "skip": "ArrayBuffer and TypedArray is not accepted and throws, instead of rejects during writer.write" | 10 | | - }, | 11 | 8 | "compression-with-detach.tentative.window.js": { | 12 | 9 | "requires": ["crypto"] | 13 | 10 | }, | |
|