Mirror: A Node.js fetch shim using built-in Request, Response, and Headers (but without native fetch)
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

refactor: Remove reliance on `new Response` from `Body` wrapper (#32)

authored by

Phil Pluckthun and committed by
GitHub
2c4c4c24 46caa984

+16 -8
+5
.changeset/bitter-wings-argue.md
··· 1 + --- 2 + 'fetch-nodeshim': patch 3 + --- 4 + 5 + Replace undici `Response` with `node:stream/consumers` in body helper
+11 -8
src/body.ts
··· 1 1 import { Readable } from 'node:stream'; 2 + import { arrayBuffer, blob, text } from 'node:stream/consumers'; 2 3 import { isAnyArrayBuffer } from 'node:util/types'; 3 4 import { randomBytes } from 'node:crypto'; 4 5 import { Response, Blob, FormData, URLSearchParams } from './webstd'; ··· 223 224 224 225 async arrayBuffer() { 225 226 const { body } = this[kBodyInternals]; 226 - return isAnyArrayBuffer(body) 227 - ? body 228 - : new Response(this.body).arrayBuffer(); 227 + return body != null && !isAnyArrayBuffer(body) ? arrayBuffer(body) : body; 229 228 } 230 229 231 230 async formData() { ··· 237 236 } 238 237 239 238 async blob() { 240 - const { contentType } = this[kBodyInternals]; 241 - return new Blob([await this.arrayBuffer()], { 239 + const { body, contentType } = this[kBodyInternals]; 240 + const chunks = 241 + body !== null ? [!isAnyArrayBuffer(body) ? await blob(body) : body] : []; 242 + return new Blob(chunks, { 242 243 type: contentType ?? undefined, 243 244 }); 244 245 } 245 246 246 247 async json() { 247 - const text = await this.text(); 248 - return JSON.parse(text); 248 + return JSON.parse(await this.text()); 249 249 } 250 250 251 251 async text() { 252 - return new TextDecoder().decode(await this.arrayBuffer()); 252 + const { body } = this[kBodyInternals]; 253 + return body == null || isAnyArrayBuffer(body) 254 + ? new TextDecoder().decode(await this.arrayBuffer()) 255 + : text(body); 253 256 } 254 257 }