@whatwg-node/node-fetch
Advanced tools
Comparing version 0.0.6 to 0.0.7-alpha-20230208145746-08ecb64
/// <reference types="node" /> | ||
import { Blob as NodeBlob } from 'buffer'; | ||
declare const PonyfillBlob_base: typeof NodeBlob; | ||
export declare class PonyfillBlob extends PonyfillBlob_base implements Blob { | ||
declare const BaseBlob: typeof NodeBlob; | ||
export declare class PonyfillBlob extends BaseBlob implements Blob { | ||
stream(): any; | ||
@@ -6,0 +6,0 @@ slice(...args: any[]): any; |
export type PonyfillHeadersInit = [string, string][] | Record<string, string | string[] | undefined> | Headers; | ||
export declare class PonyfillHeaders implements Headers { | ||
private headersInit?; | ||
private map; | ||
constructor(headersInit?: PonyfillHeadersInit); | ||
private mapIsBuilt; | ||
private objectKeysOfeadersInit; | ||
constructor(headersInit?: PonyfillHeadersInit | undefined); | ||
private _get; | ||
private getMap; | ||
append(name: string, value: string): void; | ||
@@ -6,0 +11,0 @@ get(name: string): string | null; |
95
index.js
@@ -187,5 +187,6 @@ 'use strict'; | ||
} | ||
const BaseBlob = buffer.Blob || DummyBlob; | ||
// Will be removed after v14 reaches EOL | ||
// Needed because v14 doesn't have .stream() implemented | ||
class PonyfillBlob extends (buffer.Blob || DummyBlob) { | ||
class PonyfillBlob extends BaseBlob { | ||
stream() { | ||
@@ -565,2 +566,11 @@ return new PonyfillReadableStream({ | ||
} | ||
// perf: avoid reading the stream twice (we read from Readable to create Blob and then we read from Blob to create a string) | ||
const _body = this.generateBody(); | ||
if (_body) { | ||
const chunks = []; | ||
for await (const chunk of _body.readable) { | ||
chunks.push(chunk); | ||
} | ||
return Buffer.concat(chunks).toString(this.contentType || 'utf-8'); | ||
} | ||
const blob = await this.blob(); | ||
@@ -723,9 +733,51 @@ return blob.text(); | ||
constructor(headersInit) { | ||
this.headersInit = headersInit; | ||
this.map = new Map(); | ||
if (headersInit != null) { | ||
if (Array.isArray(headersInit)) { | ||
this.map = new Map(headersInit); | ||
this.mapIsBuilt = false; | ||
this.objectKeysOfeadersInit = []; | ||
} | ||
// perf: we don't need to build `this.map` for Requests, as we can access the headers directly | ||
_get(key) { | ||
// If the map is built, reuse it | ||
if (this.mapIsBuilt) { | ||
return this.map.get(key.toLowerCase()) || null; | ||
} | ||
// If the map is not built, try to get the value from the this.headersInit | ||
if (this.headersInit == null) { | ||
return null; | ||
} | ||
const normalized = key.toLowerCase(); | ||
if (Array.isArray(this.headersInit)) { | ||
return this.headersInit.find(header => header[0] === normalized); | ||
} | ||
else if (isHeadersLike(this.headersInit)) { | ||
return this.headersInit.get(normalized); | ||
} | ||
else { | ||
const initValue = this.headersInit[key] || this.headersInit[normalized]; | ||
if (initValue != null) { | ||
return initValue; | ||
} | ||
else if (isHeadersLike(headersInit)) { | ||
headersInit.forEach((value, key) => { | ||
if (!this.objectKeysOfeadersInit.length) { | ||
this.objectKeysOfeadersInit = Object.keys(this.headersInit).map(k => k.toLowerCase()); | ||
} | ||
const index = this.objectKeysOfeadersInit.indexOf(normalized); | ||
if (index === -1) { | ||
return null; | ||
} | ||
return this.headersInit[index]; | ||
} | ||
} | ||
// perf: Build the map of headers lazily, only when we need to access all headers or write to it. | ||
// I could do a getter here, but I'm too lazy to type `getter`. | ||
getMap() { | ||
if (this.mapIsBuilt) { | ||
return this.map; | ||
} | ||
if (this.headersInit != null) { | ||
if (Array.isArray(this.headersInit)) { | ||
this.map = new Map(this.headersInit); | ||
} | ||
else if (isHeadersLike(this.headersInit)) { | ||
this.headersInit.forEach((value, key) => { | ||
this.map.set(key, value); | ||
@@ -735,4 +787,4 @@ }); | ||
else { | ||
for (const initKey in headersInit) { | ||
const initValue = headersInit[initKey]; | ||
for (const initKey in this.headersInit) { | ||
const initValue = this.headersInit[initKey]; | ||
if (initValue != null) { | ||
@@ -746,27 +798,36 @@ const normalizedValue = Array.isArray(initValue) ? initValue.join(', ') : initValue; | ||
} | ||
this.mapIsBuilt = true; | ||
return this.map; | ||
} | ||
append(name, value) { | ||
const key = name.toLowerCase(); | ||
const existingValue = this.map.get(key); | ||
const existingValue = this.getMap().get(key); | ||
const finalValue = existingValue ? `${existingValue}, ${value}` : value; | ||
this.map.set(key, finalValue); | ||
this.getMap().set(key, finalValue); | ||
} | ||
get(name) { | ||
const key = name.toLowerCase(); | ||
return this.map.get(key) || null; | ||
const value = this._get(key); | ||
if (value == null) { | ||
return null; | ||
} | ||
if (Array.isArray(value)) { | ||
return value.join(', '); | ||
} | ||
return value; | ||
} | ||
has(name) { | ||
const key = name.toLowerCase(); | ||
return this.map.has(key); | ||
return !!this._get(key); // we might need to check if header exists and not just check if it's not nullable | ||
} | ||
set(name, value) { | ||
const key = name.toLowerCase(); | ||
this.map.set(key, value); | ||
this.getMap().set(key, value); | ||
} | ||
delete(name) { | ||
const key = name.toLowerCase(); | ||
this.map.delete(key); | ||
this.getMap().delete(key); | ||
} | ||
forEach(callback) { | ||
this.map.forEach((value, key) => { | ||
this.getMap().forEach((value, key) => { | ||
callback(value, key, this); | ||
@@ -776,6 +837,6 @@ }); | ||
entries() { | ||
return this.map.entries(); | ||
return this.getMap().entries(); | ||
} | ||
[Symbol.iterator]() { | ||
return this.map.entries(); | ||
return this.getMap().entries(); | ||
} | ||
@@ -782,0 +843,0 @@ } |
{ | ||
"name": "@whatwg-node/node-fetch", | ||
"version": "0.0.6", | ||
"version": "0.0.7-alpha-20230208145746-08ecb64", | ||
"description": "Fetch API implementation for Node", | ||
@@ -5,0 +5,0 @@ "sideEffects": false, |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
91793
2445