Skip to content
Myr

request-stream-support-runtimes.ts

This file

Path

test/request-stream-support-runtimes.ts

48 lines · 1.6 KiB · TypeScript

Last changed by

Detect request stream support in Bun and Deno, and cover the pipeline with testsSindre Sorhusbefore adoption83c711c

History of this file

Take it

Detect request stream support in Bun and Deno, and cover the pipeline with testsSindre Sorhusbefore adoption83c711c

lines 1–48why, one step back
1import test from 'ava';
2import {createHttpTestServer} from './helpers/create-http-test-server.js';34test.serial('request streams are used in runtimes that do not read the duplex option', async t => {5 const OriginalRequest = globalThis.Request;6 // Bun and Deno accept streaming bodies without ever reading `duplex`, so the constructor looks the same as in WebKit, which cannot upload streams.7 globalThis.Request = class extends OriginalRequest {8 constructor(input: RequestInfo | URL, options?: RequestInit) {9 if (options?.body instanceof ReadableStream) {10 const copiedOptions: Record<string, unknown> = {};11 for (const key of Object.keys(options)) {12 if (key !== 'duplex') {13 copiedOptions[key] = options[key as keyof RequestInit];14 }15 }1617 super(input, {...copiedOptions, duplex: 'half'} as RequestInit);18 return;19 }2021 super(input, options);22 }23 };24 (globalThis as Record<string, unknown>).Deno = {};25 t.teardown(() => {26 globalThis.Request = OriginalRequest;27 delete (globalThis as Record<string, unknown>).Deno;28 });2930 const {default: ky} = await import('../source/index.js');31 const server = await createHttpTestServer(t, {bodyParser: false});32 server.post('/', (request, response) => {33 request.pipe(response);34 });3536 let progressCallCount = 0;37 t.is(await ky.post(server.url, {38 body: 'payload',39 retry: 0,40 onUploadProgress() {41 progressCallCount++;42 },43 }).text(), 'payload');44 t.true(progressCallCount > 0);4546 const input = new Request(server.url, {method: 'POST', body: 'from request'});47 t.is(await ky(input, {searchParams: {q: 1}, retry: 0}).text(), 'from request');48});