Skip to content
File

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

javascript255 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//
5// Ported from edgeworker streams-iocontext.ew-test
6 
7import { strictEqual } from 'node:assert';
8 
9// global-scope-readablestream
10const enc1 = new TextEncoder();
11const rs1 = new ReadableStream({
12 start(c) {
13 c.enqueue(enc1.encode('ok'));
14 c.close();
15 },
16});
17 
18export const globalScopeReadablestream = {
19 async test(ctrl, env) {
20 const response = await env.self.fetch('http://test/1');
21 const text = await response.text();
22 strictEqual(text, 'ok');
23 },
24};
25 
26// global-scope-readablestream-2
27const enc2 = new TextEncoder();
28const dec2 = new TextDecoder();
29const rs2 = new ReadableStream({
30 start(c) {
31 c.enqueue(enc2.encode('ok'));
32 c.close();
33 },
34});
35let text2 = '';
36const modulePromise2 = (async () => {
37 for await (const chunk of rs2) {
38 text2 += dec2.decode(chunk);
39 }
40 if (text2 !== 'ok') {
41 throw new Error('Stream should have been readable at global scope');
42 }
43})();
44 
45export const globalScopeReadablestream2 = {
46 async test(ctrl, env) {
47 await modulePromise2;
48 const response = await env.self.fetch('http://test/2');
49 const text = await response.text();
50 strictEqual(text, 'ok');
51 },
52};
53 
54// global-scope-readablestream-3
55const enc3 = new TextEncoder();
56const dec3 = new TextDecoder();
57const rs3 = new ReadableStream({
58 start(c) {
59 c.enqueue(enc3.encode('ok'));
60 c.close();
61 },
62});
63 
64export const globalScopeReadablestream3 = {
65 async test(ctrl, env) {
66 const response = await env.self.fetch('http://test/3');
67 const text = await response.text();
68 strictEqual(text, 'ok');
69 },
70};
71 
72// global-scope-readablestream-4
73const enc4 = new TextEncoder();
74const rs4 = new ReadableStream({
75 start(c) {
76 c.enqueue(enc4.encode('ok'));
77 c.close();
78 },
79});
80 
81export const globalScopeReadablestream4 = {
82 async test(ctrl, env) {
83 const response = await env.self.fetch('http://test/4');
84 const text = await response.text();
85 strictEqual(text, 'ok');
86 },
87};
88 
89// global-scope-readablestream-5
90const enc5 = new TextEncoder();
91const rs5 = new ReadableStream({
92 start(c) {
93 c.enqueue(enc5.encode('ok'));
94 c.close();
95 },
96});
97const _res5 = new Response(rs5);
98 
99export const globalScopeReadablestream5 = {
100 async test(ctrl, env) {
101 const response = await env.self.fetch('http://test/5');
102 const text = await response.text();
103 strictEqual(text, 'ok');
104 },
105};
106 
107// global-scope-readablestream-6
108const rs6 = new ReadableStream({
109 start(c) {
110 globalThis.controller6 = c;
111 },
112});
113 
114export const globalScopeReadablestream6 = {
115 async test(ctrl, env) {
116 const responseA = await env.self.fetch('http://test/6/a');
117 strictEqual(await responseA.text(), 'ok');
118 
119 const responseB = await env.self.fetch('http://test/6/b');
120 strictEqual(await responseB.text(), 'madness');
121 },
122};
123 
124// global-scope-readablestream-7
125let resolve7;
126const promise7 = new Promise((r) => (resolve7 = r));
127const rs7 = new ReadableStream({
128 async pull(c) {
129 const pullFunction = await promise7;
130 return pullFunction(c);
131 },
132});
133 
134export const globalScopeReadablestream7 = {
135 async test(ctrl, env) {
136 const responseA = await env.self.fetch('http://test/7/a');
137 strictEqual(await responseA.text(), 'ok');
138 
139 const responseB = await env.self.fetch('http://test/7/b');
140 strictEqual(await responseB.text(), 'madness');
141 },
142};
143 
144// global-scope-readablestream-8
145let resolve8;
146const promise8 = new Promise((r) => (resolve8 = r));
147const rs8 = new ReadableStream({
148 async start(c) {
149 globalThis.controller8 = c;
150 await promise8;
151 },
152});
153 
154export const globalScopeReadablestream8 = {
155 async test(ctrl, env) {
156 const responseA = await env.self.fetch('http://test/8/a');
157 strictEqual(await responseA.text(), 'ok');
158 
159 const responseB = await env.self.fetch('http://test/8/b');
160 strictEqual(await responseB.text(), 'madness');
161 },
162};
163 
164export default {
165 async fetch(req) {
166 const url = new URL(req.url);
167 
168 // global-scope-readablestream
169 if (url.pathname === '/1') {
170 return new Response(rs1);
171 }
172 
173 // global-scope-readablestream-2
174 if (url.pathname === '/2') {
175 return new Response('ok');
176 }
177 
178 // global-scope-readablestream-3
179 if (url.pathname === '/3') {
180 let text = '';
181 for await (const chunk of rs3) {
182 text += dec3.decode(chunk);
183 }
184 if (text !== 'ok') {
185 throw new Error('Global scope stream should have been readable');
186 }
187 return new Response('ok');
188 }
189 
190 // global-scope-readablestream-4
191 if (url.pathname === '/4') {
192 return new Response(rs4.pipeThrough(new TransformStream()));
193 }
194 
195 // global-scope-readablestream-5
196 if (url.pathname === '/5') {
197 return new Response('ok');
198 }
199 
200 // global-scope-readablestream-6
201 if (url.pathname === '/6/a') {
202 const enc = new TextEncoder();
203 globalThis.controller6.enqueue(enc.encode('madness'));
204 globalThis.controller6.close();
205 return new Response('ok');
206 }
207 if (url.pathname === '/6/b') {
208 let text = '';
209 const dec = new TextDecoder();
210 for await (const chunk of rs6) {
211 text += dec.decode(chunk);
212 }
213 return new Response(text);
214 }
215 
216 // global-scope-readablestream-7
217 if (url.pathname === '/7/a') {
218 const enc = new TextEncoder();
219 resolve7((c) => {
220 c.enqueue(enc.encode('madness'));
221 c.close();
222 });
223 return new Response('ok');
224 }
225 if (url.pathname === '/7/b') {
226 let text = '';
227 const dec = new TextDecoder();
228 for await (const chunk of rs7) {
229 text += dec.decode(chunk);
230 }
231 return new Response(text);
232 }
233 
234 // global-scope-readablestream-8
235 if (url.pathname === '/8/a') {
236 const enc = new TextEncoder();
237 await scheduler.wait(10);
238 resolve8();
239 globalThis.controller8.enqueue(enc.encode('madness'));
240 globalThis.controller8.close();
241 return new Response('ok');
242 }
243 if (url.pathname === '/8/b') {
244 let text = '';
245 const dec = new TextDecoder();
246 for await (const chunk of rs8) {
247 text += dec.decode(chunk);
248 }
249 return new Response(text);
250 }
251 
252 throw new Error('boom');
253 },
254};