Skip to content

Commit

Permalink
Fix Chrome 105 compatibility (#451)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
karol-f and sindresorhus committed Sep 2, 2022
1 parent 6b2b1b2 commit 316fffe
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
5 changes: 5 additions & 0 deletions source/core/Ky.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,11 @@ export class Ky {

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

if (supportsStreams) {
// @ts-expect-error - Types are outdated.
this.request.duplex = 'half';
}

if (this._options.searchParams) {
// eslint-disable-next-line unicorn/prevent-abbreviations
const textSearchParams = typeof this._options.searchParams === 'string'
Expand Down
21 changes: 20 additions & 1 deletion source/core/constants.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,27 @@
import type {Expect, Equal} from '@type-challenges/utils';
import {HttpMethod} from '../types/options.js';

export const supportsStreams = (() => {
let duplexAccessed = false;
let hasContentType = false;
const supportsReadableStream = typeof globalThis.ReadableStream === 'function';

if (supportsReadableStream) {
hasContentType = new globalThis.Request('', {
body: new globalThis.ReadableStream(),
method: 'POST',
// @ts-expect-error - Types are outdated.
get duplex() {
duplexAccessed = true;
return 'half';
},
}).headers.has('Content-Type');
}

return duplexAccessed && !hasContentType;
})();

export const supportsAbortController = typeof globalThis.AbortController === 'function';
export const supportsStreams = typeof globalThis.ReadableStream === 'function';
export const supportsFormData = typeof globalThis.FormData === 'function';

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

0 comments on commit 316fffe

Please sign in to comment.