aboutsummaryrefslogtreecommitdiff
path: root/test/js/web/fetch/body-stream.test.ts
blob: 8e2baf92a5d24c9dd9f60cc8bd3f535047948b11 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
// @ts-nocheck
import { gc, ServeOptions } from "bun";
import { afterAll, describe, expect, it, test } from "bun:test";

var port = 0;

{
  const BodyMixin = [
    Request.prototype.arrayBuffer,
    Request.prototype.blob,
    Request.prototype.text,
    Request.prototype.json,
  ];
  const useRequestObjectValues = [true, false];

  for (let RequestPrototypeMixin of BodyMixin) {
    for (let useRequestObject of useRequestObjectValues) {
      describe(`Request.prototoype.${RequestPrototypeMixin.name}() ${
        useRequestObject ? "fetch(req)" : "fetch(url)"
      }`, () => {
        const inputFixture = [
          [JSON.stringify("Hello World"), JSON.stringify("Hello World")],
          [JSON.stringify("Hello World 123"), Buffer.from(JSON.stringify("Hello World 123")).buffer],
          [JSON.stringify("Hello World 456"), Buffer.from(JSON.stringify("Hello World 456"))],
          [
            JSON.stringify("EXTREMELY LONG VERY LONG STRING WOW SO LONG YOU WONT BELIEVE IT! ".repeat(100)),
            Buffer.from(
              JSON.stringify("EXTREMELY LONG VERY LONG STRING WOW SO LONG YOU WONT BELIEVE IT! ".repeat(100)),
            ),
          ],
          [
            JSON.stringify("EXTREMELY LONG 🔥 UTF16 🔥 VERY LONG STRING WOW SO LONG YOU WONT BELIEVE IT! ".repeat(100)),
            Buffer.from(
              JSON.stringify(
                "EXTREMELY LONG 🔥 UTF16 🔥 VERY LONG STRING WOW SO LONG YOU WONT BELIEVE IT! ".repeat(100),
              ),
            ),
          ],
        ];

        for (const [name, input] of inputFixture) {
          test(`${name.slice(0, Math.min(name.length ?? name.byteLength, 64))}`, async () => {
            await runInServer(
              {
                async fetch(req) {
                  var result = await RequestPrototypeMixin.call(req);
                  if (RequestPrototypeMixin === Request.prototype.json) {
                    result = JSON.stringify(result);
                  }
                  if (typeof result === "string") {
                    expect(result.length).toBe(name.length);
                    expect(result).toBe(name);
                  } else if (result && result instanceof Blob) {
                    expect(result.size).toBe(new TextEncoder().encode(name).byteLength);
                    expect(await result.text()).toBe(name);
                  } else {
                    expect(result.byteLength).toBe(Buffer.from(input).byteLength);
                    expect(Bun.SHA1.hash(result, "base64")).toBe(Bun.SHA1.hash(input, "base64"));
                  }
                  return new Response(result, {
                    headers: req.headers,
                  });
                },
              },
              async url => {
                var response;

                // once, then batch of 5

                if (useRequestObject) {
                  response = await fetch(
                    new Request({
                      body: input,
                      method: "POST",
                      url: url,
                      headers: {
                        "content-type": "text/plain",
                      },
                    }),
                  );
                } else {
                  response = await fetch(url, {
                    body: input,
                    method: "POST",
                    headers: {
                      "content-type": "text/plain",
                    },
                  });
                }

                expect(response.status).toBe(200);
                expect(response.headers.get("content-length")).toBe(String(Buffer.from(input).byteLength));
                expect(response.headers.get("content-type")).toBe("text/plain");
                expect(await response.text()).toBe(name);

                var promises = new Array(5);
                for (let i = 0; i < 5; i++) {
                  if (useRequestObject) {
                    promises[i] = await fetch(
                      new Request({
                        body: input,
                        method: "POST",
                        url: url,
                        headers: {
                          "content-type": "text/plain",
                          "x-counter": i,
                        },
                      }),
                    );
                  } else {
                    promises[i] = await fetch(url, {
                      body: input,
                      method: "POST",
                      headers: {
                        "content-type": "text/plain",
                        "x-counter": i,
                      },
                    });
                  }
                }

                const results = await Promise.all(promises);
                for (let i = 0; i < 5; i++) {
                  const response = results[i];
                  expect(response.status).toBe(200);
                  expect(response.headers.get("content-length")).toBe(String(Buffer.from(input).byteLength));
                  expect(response.headers.get("content-type")).toBe("text/plain");
                  expect(response.headers.get("x-counter")).toBe(String(i));
                  expect(await response.text()).toBe(name);
                }
              },
            );
          });
        }
      });
    }
  }
}

var existingServer;
async function runInServer(opts: ServeOptions, cb: (url: string) => void | Promise<void>) {
  var server;
  const handler = {
    ...opts,
    port: 0,
    fetch(req) {
      try {
        return opts.fetch(req);
      } catch (e) {
        console.error(e.message);
        console.log(e.stack);
        throw e;
      }
    },
    error(err) {
      console.log(err.message);
      console.log(err.stack);
      throw err;
    },
  };

  if (!existingServer) {
    existingServer = server = Bun.serve(handler);
  } else {
    server = existingServer;
    server.reload(handler);
  }

  try {
    await cb(`http://${server.hostname}:${server.port}`);
  } catch (e) {
    throw e;
  } finally {
  }
}

afterAll(() => {
  existingServer && existingServer.stop();
  existingServer = null;
});

function fillRepeating(dstBuffer, start, end) {
  let len = dstBuffer.length,
    sLen = end - start,
    p = sLen;
  while (p < len) {
    if (p + sLen > len) sLen = len - p;
    dstBuffer.copyWithin(p, start, sLen);
    p += sLen;
    sLen <<= 1;
  }
}

function gc() {
  Bun.gc(true);
}

describe("reader", function () {
  for (let withDelay of [false, true]) {
    try {
      // - 1 byte
      // - less than the InlineBlob limit
      // - multiple chunks
      // - backpressure

      for (let inputLength of [1, 2, 12, 95, 1024, 1024 * 1024, 1024 * 1024 * 2]) {
        var bytes = new Uint8Array(inputLength);
        {
          const chunk = Math.min(bytes.length, 256);
          for (var i = 0; i < chunk; i++) {
            bytes[i] = 255 - i;
          }
        }

        if (bytes.length > 255) fillRepeating(bytes, 0, bytes.length);

        for (const huge_ of [
          bytes,
          bytes.buffer,
          new DataView(bytes.buffer),
          new Int8Array(bytes),
          new Blob([bytes]),

          new Uint16Array(bytes),
          new Uint32Array(bytes),
          new Float64Array(bytes),

          new Int16Array(bytes),
          new Int32Array(bytes),
          new Float32Array(bytes),

          // make sure we handle subarray() as expected when reading
          // typed arrays from native code
          new Int16Array(bytes).subarray(1),
          new Int16Array(bytes).subarray(0, new Int16Array(bytes).byteLength - 1),
          new Int32Array(bytes).subarray(1),
          new Int32Array(bytes).subarray(0, new Int32Array(bytes).byteLength - 1),
          new Float32Array(bytes).subarray(1),
          new Float32Array(bytes).subarray(0, new Float32Array(bytes).byteLength - 1),
          new Int16Array(bytes).subarray(0, 1),
          new Int32Array(bytes).subarray(0, 1),
          new Float32Array(bytes).subarray(0, 1),
        ]) {
          gc();
          const thisArray = huge_;
          if (Number(thisArray.byteLength ?? thisArray.size) === 0) continue;

          it(
            `works with ${thisArray.constructor.name}(${
              thisArray.byteLength ?? thisArray.size
            }:${inputLength}) via req.body.getReader() in chunks` + (withDelay ? " with delay" : ""),
            async () => {
              var huge = thisArray;
              var called = false;
              gc();

              const expectedHash =
                huge instanceof Blob
                  ? Bun.SHA1.hash(new Uint8Array(await huge.arrayBuffer()), "base64")
                  : Bun.SHA1.hash(huge, "base64");
              const expectedSize = huge instanceof Blob ? huge.size : huge.byteLength;

              const out = await runInServer(
                {
                  async fetch(req) {
                    try {
                      if (withDelay) await 1;

                      expect(req.headers.get("x-custom")).toBe("hello");
                      expect(req.headers.get("content-type")).toBe("text/plain");
                      expect(req.headers.get("user-agent")).toBe(navigator.userAgent);

                      gc();
                      expect(req.headers.get("x-custom")).toBe("hello");
                      expect(req.headers.get("content-type")).toBe("text/plain");
                      expect(req.headers.get("user-agent")).toBe(navigator.userAgent);

                      var reader = req.body.getReader();
                      called = true;
                      var buffers = [];
                      while (true) {
                        var { done, value } = await reader.read();
                        if (done) break;
                        buffers.push(value);
                      }
                      const out = new Blob(buffers);
                      gc();
                      expect(out.size).toBe(expectedSize);
                      expect(Bun.SHA1.hash(await out.arrayBuffer(), "base64")).toBe(expectedHash);
                      expect(req.headers.get("x-custom")).toBe("hello");
                      expect(req.headers.get("content-type")).toBe("text/plain");
                      expect(req.headers.get("user-agent")).toBe(navigator.userAgent);
                      gc();
                      return new Response(out, {
                        headers: req.headers,
                      });
                    } catch (e) {
                      console.error(e);
                      throw e;
                    }
                  },
                },
                async url => {
                  gc();
                  if (withDelay) await 1;
                  const pendingResponse = await fetch(url, {
                    body: huge,
                    method: "POST",
                    headers: {
                      "content-type": "text/plain",
                      "x-custom": "hello",
                      "x-typed-array": thisArray.constructor.name,
                    },
                  });
                  if (withDelay) {
                    await 1;
                  }
                  const response = await pendingResponse;
                  huge = undefined;
                  expect(response.status).toBe(200);
                  const response_body = new Uint8Array(await response.arrayBuffer());

                  expect(response_body.byteLength).toBe(expectedSize);
                  expect(Bun.SHA1.hash(response_body, "base64")).toBe(expectedHash);

                  gc();
                  expect(response.headers.get("content-type")).toBe("text/plain");
                  gc();
                },
              );
              expect(called).toBe(true);
              gc();
              return out;
            },
          );

          for (let isDirectStream of [true, false]) {
            const positions = ["begin", "end"];
            const inner = thisArray => {
              for (let position of positions) {
                it(`streaming back ${thisArray.constructor.name}(${
                  thisArray.byteLength ?? thisArray.size
                }:${inputLength}) starting request.body.getReader() at ${position}`, async () => {
                  var huge = thisArray;
                  var called = false;
                  gc();

                  const expectedHash =
                    huge instanceof Blob
                      ? Bun.SHA1.hash(new Uint8Array(await huge.arrayBuffer()), "base64")
                      : Bun.SHA1.hash(huge, "base64");
                  const expectedSize = huge instanceof Blob ? huge.size : huge.byteLength;

                  const out = await runInServer(
                    {
                      async fetch(req) {
                        try {
                          var reader;

                          if (withDelay) await 1;

                          if (position === "begin") {
                            reader = req.body.getReader();
                          }

                          if (position === "end") {
                            await 1;
                            reader = req.body.getReader();
                          }

                          expect(req.headers.get("x-custom")).toBe("hello");
                          expect(req.headers.get("content-type")).toBe("text/plain");
                          expect(req.headers.get("user-agent")).toBe(navigator.userAgent);

                          gc();
                          expect(req.headers.get("x-custom")).toBe("hello");
                          expect(req.headers.get("content-type")).toBe("text/plain");
                          expect(req.headers.get("user-agent")).toBe(navigator.userAgent);

                          const direct = {
                            type: "direct",
                            async pull(controller) {
                              if (withDelay) await 1;

                              while (true) {
                                const { done, value } = await reader.read();
                                if (done) {
                                  called = true;
                                  controller.end();

                                  return;
                                }
                                controller.write(value);
                              }
                            },
                          };

                          const web = {
                            async start() {
                              if (withDelay) await 1;
                            },
                            async pull(controller) {
                              while (true) {
                                const { done, value } = await reader.read();
                                if (done) {
                                  called = true;
                                  controller.close();
                                  return;
                                }
                                controller.enqueue(value);
                              }
                            },
                          };

                          return new Response(new ReadableStream(isDirectStream ? direct : web), {
                            headers: req.headers,
                          });
                        } catch (e) {
                          console.error(e);
                          throw e;
                        }
                      },
                    },
                    async url => {
                      gc();
                      const response = await fetch(url, {
                        body: huge,
                        method: "POST",
                        headers: {
                          "content-type": "text/plain",
                          "x-custom": "hello",
                          "x-typed-array": thisArray.constructor.name,
                        },
                      });
                      huge = undefined;
                      expect(response.status).toBe(200);
                      const response_body = new Uint8Array(await response.arrayBuffer());

                      expect(response_body.byteLength).toBe(expectedSize);
                      expect(Bun.SHA1.hash(response_body, "base64")).toBe(expectedHash);

                      gc();
                      if (!response.headers.has("content-type")) {
                        console.error(Object.fromEntries(response.headers.entries()));
                      }

                      expect(response.headers.get("content-type")).toBe("text/plain");
                      gc();
                    },
                  );
                  expect(called).toBe(true);
                  gc();
                  return out;
                });
              }
            };

            if (isDirectStream) {
              describe(" direct stream", () => inner(thisArray));
            } else {
              describe("default stream", () => inner(thisArray));
            }
          }
        }
      }
    } catch (e) {
      console.error(e);
      throw e;
    }
  }
});