Skip to content

Commit 316fffe

Browse files
karol-fsindresorhus
andauthoredSep 2, 2022
Fix Chrome 105 compatibility (#451)
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
1 parent 6b2b1b2 commit 316fffe

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed
 

‎source/core/Ky.ts

+5
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,11 @@ export class Ky {
155155

156156
this.request = new globalThis.Request(this._input as RequestInfo, this._options as RequestInit);
157157

158+
if (supportsStreams) {
159+
// @ts-expect-error - Types are outdated.
160+
this.request.duplex = 'half';
161+
}
162+
158163
if (this._options.searchParams) {
159164
// eslint-disable-next-line unicorn/prevent-abbreviations
160165
const textSearchParams = typeof this._options.searchParams === 'string'

‎source/core/constants.ts

+20-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,27 @@
11
import type {Expect, Equal} from '@type-challenges/utils';
22
import {HttpMethod} from '../types/options.js';
33

4+
export const supportsStreams = (() => {
5+
let duplexAccessed = false;
6+
let hasContentType = false;
7+
const supportsReadableStream = typeof globalThis.ReadableStream === 'function';
8+
9+
if (supportsReadableStream) {
10+
hasContentType = new globalThis.Request('', {
11+
body: new globalThis.ReadableStream(),
12+
method: 'POST',
13+
// @ts-expect-error - Types are outdated.
14+
get duplex() {
15+
duplexAccessed = true;
16+
return 'half';
17+
},
18+
}).headers.has('Content-Type');
19+
}
20+
21+
return duplexAccessed && !hasContentType;
22+
})();
23+
424
export const supportsAbortController = typeof globalThis.AbortController === 'function';
5-
export const supportsStreams = typeof globalThis.ReadableStream === 'function';
625
export const supportsFormData = typeof globalThis.FormData === 'function';
726

827
export const requestMethods = ['get', 'post', 'put', 'patch', 'head', 'delete'] as const;

0 commit comments

Comments
 (0)