Skip to content

Commit

Permalink
Update to use globalThis.Response to getReader instead of Blob
Browse files Browse the repository at this point in the history
  • Loading branch information
jadedevin13 authored Sep 6, 2024
1 parent ca5bc8a commit 80f0b4e
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions source/core/Ky.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,31 +384,34 @@ export class Ky {
);
}

protected _getTotalBytes(body: BodyInit): number {
if (body instanceof Blob) {
protected _getTotalBytes(body: globalThis.BodyInit): number {
if (body instanceof globalThis.Blob) {
return body.size;
}
if (body instanceof ArrayBuffer) {
if (body instanceof globalThis.ArrayBuffer) {

Check failure on line 391 in source/core/Ky.ts

View workflow job for this annotation

GitHub Actions / Node.js 18

Expected blank line before this statement.
return body.byteLength;
}
if (typeof body === 'string') {

Check failure on line 394 in source/core/Ky.ts

View workflow job for this annotation

GitHub Actions / Node.js 18

Expected blank line before this statement.
return new Blob([body]).size;
return new globalThis.TextEncoder().encode(body).length;
}
if (body instanceof URLSearchParams) {

Check failure on line 397 in source/core/Ky.ts

View workflow job for this annotation

GitHub Actions / Node.js 18

Expected blank line before this statement.
return new Blob([body.toString()]).size;
return new globalThis.TextEncoder().encode(body.toString()).length;
}
if (body instanceof globalThis.FormData) {

Check failure on line 400 in source/core/Ky.ts

View workflow job for this annotation

GitHub Actions / Node.js 18

Expected blank line before this statement.
// This is an approximation, as FormData size calculation is not straightforward
return Array.from(body.entries()).reduce((acc, [_, value]) => {

Check failure on line 402 in source/core/Ky.ts

View workflow job for this annotation

GitHub Actions / Node.js 18

Prefer the spread operator over `Array.from(…)`.

Check failure on line 402 in source/core/Ky.ts

View workflow job for this annotation

GitHub Actions / Node.js 18

`Array#reduce()` is not allowed

Check failure on line 402 in source/core/Ky.ts

View workflow job for this annotation

GitHub Actions / Node.js 18

The variable `acc` should be named `accumulator`. A more descriptive name will do too.
if (typeof value === 'string') {
return acc + new Blob([value]).size;
return acc + new globalThis.TextEncoder().encode(value).length;
}
if (value instanceof Blob) {

Check failure on line 406 in source/core/Ky.ts

View workflow job for this annotation

GitHub Actions / Node.js 18

Expected blank line before this statement.
return acc + value.size;
}
return acc;

Check failure on line 409 in source/core/Ky.ts

View workflow job for this annotation

GitHub Actions / Node.js 18

Expected blank line before this statement.
}, 0);
}
if ('byteLength' in body) {

Check failure on line 412 in source/core/Ky.ts

View workflow job for this annotation

GitHub Actions / Node.js 18

Expected blank line before this statement.
return (body as globalThis.ArrayBufferView).byteLength;
}
return 0; // Default case, unable to determine size
}

Expand All @@ -421,7 +424,7 @@ export class Ky {

return new globalThis.ReadableStream({
async start(controller) {
const reader = body instanceof globalThis.ReadableStream ? body.getReader() : new Blob([body]).stream().getReader();
const reader = body instanceof globalThis.ReadableStream ? body.getReader() : new globalThis.Response(body).body!.getReader();

async function read() {
const { done, value } = await reader.read();
Expand Down

0 comments on commit 80f0b4e

Please sign in to comment.