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

Compare changes

Choose any two refs to compare.

+19 -9
+7
CHANGELOG.md
··· 1 1 # minifetch 2 2 3 + ## 0.4.6 4 + 5 + ### Patch Changes 6 + 7 + - Replace undici `Response` with `node:stream/consumers` in body helper 8 + Submitted by [@kitten](https://github.com/kitten) (See [#32](https://github.com/kitten/fetch-nodeshim/pull/32)) 9 + 3 10 ## 0.4.5 4 11 5 12 ### Patch Changes
+1 -1
package.json
··· 1 1 { 2 2 "name": "fetch-nodeshim", 3 - "version": "0.4.5", 3 + "version": "0.4.6", 4 4 "description": "A Node.js fetch shim using built-in Request, Response, and Headers (but without native fetch)", 5 5 "author": "Phil Pluckthun <phil@kitten.sh>", 6 6 "source": "./src/index.ts",
+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 }