@whatwg-node/node-fetch
Advanced tools
Comparing version 0.0.7-alpha-20230208150005-0bd8aaf to 0.0.7-alpha-20230209144328-8bdce99
/// <reference types="node" /> | ||
import { Blob as NodeBlob } from 'buffer'; | ||
declare const BaseBlob: typeof NodeBlob; | ||
export declare class PonyfillBlob extends BaseBlob implements Blob { | ||
declare const PonyfillBlob_base: typeof NodeBlob; | ||
export declare class PonyfillBlob extends PonyfillBlob_base 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; | ||
private mapIsBuilt; | ||
private objectKeysOfeadersInit; | ||
constructor(headersInit?: PonyfillHeadersInit | undefined); | ||
private _get; | ||
private getMap; | ||
constructor(headersInit?: PonyfillHeadersInit); | ||
append(name: string, value: string): void; | ||
@@ -11,0 +6,0 @@ get(name: string): string | null; |
134
index.js
@@ -36,6 +36,13 @@ 'use strict'; | ||
let _closed = false; | ||
let flushed = false; | ||
return { | ||
desiredSize, | ||
enqueue(chunk) { | ||
chunks.push(Buffer.from(chunk)); | ||
const buf = typeof chunk === 'string' ? Buffer.from(chunk) : chunk; | ||
if (!flushed) { | ||
chunks.push(buf); | ||
} | ||
else { | ||
readable.push(buf); | ||
} | ||
}, | ||
@@ -59,2 +66,3 @@ close() { | ||
_flush() { | ||
flushed = true; | ||
if (chunks.length > 0) { | ||
@@ -107,14 +115,25 @@ const concatenated = Buffer.concat(chunks); | ||
let started = false; | ||
let ongoing = false; | ||
this.readable = new stream.Readable({ | ||
async read(desiredSize) { | ||
var _a, _b; | ||
const controller = createController(desiredSize, this); | ||
if (!started) { | ||
started = true; | ||
await ((_a = underlyingSource === null || underlyingSource === void 0 ? void 0 : underlyingSource.start) === null || _a === void 0 ? void 0 : _a.call(underlyingSource, controller)); | ||
read(desiredSize) { | ||
if (ongoing) { | ||
return; | ||
} | ||
if (!controller._closed) { | ||
ongoing = true; | ||
return Promise.resolve().then(async () => { | ||
var _a, _b; | ||
if (!started) { | ||
const controller = createController(desiredSize, this); | ||
started = true; | ||
await ((_a = underlyingSource === null || underlyingSource === void 0 ? void 0 : underlyingSource.start) === null || _a === void 0 ? void 0 : _a.call(underlyingSource, controller)); | ||
controller._flush(); | ||
if (controller._closed) { | ||
return; | ||
} | ||
} | ||
const controller = createController(desiredSize, this); | ||
await ((_b = underlyingSource === null || underlyingSource === void 0 ? void 0 : underlyingSource.pull) === null || _b === void 0 ? void 0 : _b.call(underlyingSource, controller)); | ||
} | ||
controller._flush(); | ||
controller._flush(); | ||
ongoing = false; | ||
}); | ||
}, | ||
@@ -190,6 +209,5 @@ async destroy(err, callback) { | ||
} | ||
const BaseBlob = buffer.Blob || DummyBlob; | ||
// Will be removed after v14 reaches EOL | ||
// Needed because v14 doesn't have .stream() implemented | ||
class PonyfillBlob extends BaseBlob { | ||
class PonyfillBlob extends (buffer.Blob || DummyBlob) { | ||
stream() { | ||
@@ -569,11 +587,2 @@ 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('utf-8' /* use this.contentType soon */); | ||
} | ||
const blob = await this.blob(); | ||
@@ -736,51 +745,9 @@ return blob.text(); | ||
constructor(headersInit) { | ||
this.headersInit = headersInit; | ||
this.map = new Map(); | ||
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; | ||
if (headersInit != null) { | ||
if (Array.isArray(headersInit)) { | ||
this.map = new Map(headersInit); | ||
} | ||
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) => { | ||
else if (isHeadersLike(headersInit)) { | ||
headersInit.forEach((value, key) => { | ||
this.map.set(key, value); | ||
@@ -790,4 +757,4 @@ }); | ||
else { | ||
for (const initKey in this.headersInit) { | ||
const initValue = this.headersInit[initKey]; | ||
for (const initKey in headersInit) { | ||
const initValue = headersInit[initKey]; | ||
if (initValue != null) { | ||
@@ -801,36 +768,27 @@ const normalizedValue = Array.isArray(initValue) ? initValue.join(', ') : initValue; | ||
} | ||
this.mapIsBuilt = true; | ||
return this.map; | ||
} | ||
append(name, value) { | ||
const key = name.toLowerCase(); | ||
const existingValue = this.getMap().get(key); | ||
const existingValue = this.map.get(key); | ||
const finalValue = existingValue ? `${existingValue}, ${value}` : value; | ||
this.getMap().set(key, finalValue); | ||
this.map.set(key, finalValue); | ||
} | ||
get(name) { | ||
const key = name.toLowerCase(); | ||
const value = this._get(key); | ||
if (value == null) { | ||
return null; | ||
} | ||
if (Array.isArray(value)) { | ||
return value.join(', '); | ||
} | ||
return value; | ||
return this.map.get(key) || null; | ||
} | ||
has(name) { | ||
const key = name.toLowerCase(); | ||
return !!this._get(key); // we might need to check if header exists and not just check if it's not nullable | ||
return this.map.has(key); | ||
} | ||
set(name, value) { | ||
const key = name.toLowerCase(); | ||
this.getMap().set(key, value); | ||
this.map.set(key, value); | ||
} | ||
delete(name) { | ||
const key = name.toLowerCase(); | ||
this.getMap().delete(key); | ||
this.map.delete(key); | ||
} | ||
forEach(callback) { | ||
this.getMap().forEach((value, key) => { | ||
this.map.forEach((value, key) => { | ||
callback(value, key, this); | ||
@@ -840,6 +798,6 @@ }); | ||
entries() { | ||
return this.getMap().entries(); | ||
return this.map.entries(); | ||
} | ||
[Symbol.iterator]() { | ||
return this.getMap().entries(); | ||
return this.map.entries(); | ||
} | ||
@@ -846,0 +804,0 @@ } |
{ | ||
"name": "@whatwg-node/node-fetch", | ||
"version": "0.0.7-alpha-20230208150005-0bd8aaf", | ||
"version": "0.0.7-alpha-20230209144328-8bdce99", | ||
"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
88181
2356