Skip to content
File

Blob: src/workerd/api/tests/encoding-streams-test.js

javascript117 lines
1// Copyright (c) 2025 Cloudflare, Inc.
2// Licensed under the Apache 2.0 license found in the LICENSE file or at:
3// https://opensource.org/licenses/Apache-2.0
4 
5import { strictEqual } from 'node:assert';
6 
7// Test TextEncoderStream/TextDecoderStream pipeline
8export const textEncoderDecoderPipeline = {
9 async test() {
10 // encIn accepts a string, outputs a Uint8Array.
11 // dec accepts a Uint8Array, outputs a string.
12 // encOut accepts a string, outputs a Uint8Array.
13 const encIn = new TextEncoderStream();
14 const dec = new TextDecoderStream();
15 const encOut = new TextEncoderStream();
16 
17 const mid = encIn.readable.pipeThrough(dec);
18 const end = mid.pipeThrough(encOut);
19 
20 const writer = encIn.writable.getWriter();
21 await writer.write('hello');
22 await writer.close();
23 
24 const reader = end.getReader();
25 const result = await reader.read();
26 
27 const decoder = new TextDecoder();
28 strictEqual(decoder.decode(result.value), 'hello');
29 },
30};
31 
32// Test TextEncoderStream accepts anything that can be coerced to a string
33export const textEncoderStreamTypes = {
34 async test() {
35 const decoder = new TextDecoder();
36 const enc = new TextEncoderStream();
37 const writer = enc.writable.getWriter();
38 const reader = enc.readable.getReader();
39 
40 // Write all values then read them - need to interleave writes and reads properly
41 // by using Promise.all to avoid deadlocks from backpressure
42 
43 // Test undefined
44 {
45 const [, result] = await Promise.all([
46 writer.write(undefined),
47 reader.read(),
48 ]);
49 strictEqual(decoder.decode(result.value), 'undefined');
50 }
51 
52 // Test numbers
53 {
54 const [, result] = await Promise.all([writer.write(1), reader.read()]);
55 strictEqual(decoder.decode(result.value), '1');
56 }
57 
58 // Test objects
59 {
60 const [, result] = await Promise.all([writer.write({}), reader.read()]);
61 strictEqual(decoder.decode(result.value), '[object Object]');
62 }
63 
64 await writer.close();
65 },
66};
67 
68// Test TextDecoderStream with Big5 encoding
69export const textDecoderStreamBig5 = {
70 async test() {
71 const dec = new TextDecoderStream('big5');
72 const input = [0xa4, 0xa4, 0xb0, 0xea, 0xa4, 0x48];
73 const check = new Uint8Array([
74 0xe4, 0xb8, 0xad, 0xe5, 0x9c, 0x8b, 0xe4, 0xba, 0xba,
75 ]);
76 
77 const writer = dec.writable.getWriter();
78 
79 for (const byte of input) {
80 writer.write(new Uint8Array([byte]));
81 }
82 writer.close();
83 
84 let result = '';
85 for await (const chunk of dec.readable) {
86 result += chunk;
87 }
88 
89 strictEqual(result, '中國人');
90 
91 const decoder = new TextDecoder('utf8');
92 strictEqual(result, decoder.decode(check));
93 },
94};
95 
96// Test TextEncoderStream encoding property
97export const textEncoderStreamEncoding = {
98 test() {
99 const enc = new TextEncoderStream();
100 strictEqual(enc.encoding, 'utf-8');
101 },
102};
103 
104// Test TextDecoderStream with various encodings
105export const textDecoderStreamEncodings = {
106 test() {
107 // Test UTF-16
108 const stream = new TextDecoderStream('utf-16', {
109 fatal: true,
110 ignoreBOM: true,
111 });
112 strictEqual(stream.encoding, 'utf-16le');
113 strictEqual(stream.fatal, true);
114 strictEqual(stream.ignoreBOM, true);
115 },
116};