@whatwg-node/node-fetch
Advanced tools
Comparing version 0.0.1-alpha-20221005124650-55319ef to 0.0.1-alpha-20221005131815-b0ce77a
export declare class PonyfillAbortError extends Error { | ||
constructor(); | ||
constructor(reason?: any); | ||
get reason(): unknown; | ||
} |
@@ -0,1 +1,2 @@ | ||
import { EventTarget } from '@whatwg-node/events'; | ||
export declare class PonyfillAbortSignal extends EventTarget implements AbortSignal { | ||
@@ -2,0 +3,0 @@ aborted: boolean; |
@@ -1,5 +0,6 @@ | ||
export declare const PonyfillBlob: { | ||
new (blobParts?: BlobPart[] | undefined, options?: BlobPropertyBag | undefined): Blob; | ||
prototype: Blob; | ||
}; | ||
export declare type PonyfillBlob = Blob; | ||
/// <reference types="node" /> | ||
import { Blob as NodeBlob } from 'buffer'; | ||
export declare class PonyfillBlob extends NodeBlob implements Blob { | ||
stream(): any; | ||
slice(...args: any[]): any; | ||
} |
158
index.js
@@ -7,13 +7,20 @@ 'use strict'; | ||
const https = require('https'); | ||
const events = require('@whatwg-node/events'); | ||
const buffer = require('buffer'); | ||
const stream = require('stream'); | ||
// Will be removed after v14 reaches EOL | ||
class PonyfillAbortError extends Error { | ||
constructor() { | ||
super('The operation was aborted.'); | ||
constructor(reason) { | ||
super('The operation was aborted.', { | ||
cause: reason, | ||
}); | ||
this.name = 'AbortError'; | ||
} | ||
get reason() { | ||
return this.cause; | ||
} | ||
} | ||
class PonyfillAbortSignal extends EventTarget { | ||
class PonyfillAbortSignal extends events.EventTarget { | ||
constructor() { | ||
@@ -38,2 +45,3 @@ super(...arguments); | ||
// Will be removed after v14 reaches EOL | ||
class PonyfillAbortController { | ||
@@ -44,65 +52,6 @@ constructor() { | ||
abort(reason) { | ||
this.signal.dispatchEvent(Object.assign(new Event('abort'), { reason })); | ||
this.signal.dispatchEvent(new events.CustomEvent('abort', { detail: reason })); | ||
} | ||
} | ||
const PonyfillBlob = buffer.Blob; | ||
class PonyfillFile extends PonyfillBlob { | ||
constructor(fileBits, name, options) { | ||
super(fileBits, options); | ||
this.name = name; | ||
this.webkitRelativePath = ''; | ||
this.lastModified = (options === null || options === void 0 ? void 0 : options.lastModified) || Date.now(); | ||
} | ||
} | ||
class PonyfillFormData { | ||
constructor() { | ||
this.map = new Map(); | ||
} | ||
append(name, value, fileName) { | ||
let values = this.map.get(name); | ||
if (!values) { | ||
values = []; | ||
this.map.set(name, values); | ||
} | ||
const entry = value instanceof PonyfillBlob ? this.getNormalizedFile(name, value, fileName) : value; | ||
values.push(entry); | ||
} | ||
delete(name) { | ||
this.map.delete(name); | ||
} | ||
get(name) { | ||
const values = this.map.get(name); | ||
return values ? values[0] : null; | ||
} | ||
getAll(name) { | ||
return this.map.get(name) || []; | ||
} | ||
has(name) { | ||
return this.map.has(name); | ||
} | ||
set(name, value, fileName) { | ||
const entry = value instanceof PonyfillBlob ? this.getNormalizedFile(name, value, fileName) : value; | ||
this.map.set(name, [entry]); | ||
} | ||
getNormalizedFile(name, blob, fileName) { | ||
if (blob instanceof PonyfillFile) { | ||
if (fileName == null) { | ||
return new PonyfillFile([blob], name, { type: blob.type, lastModified: blob.lastModified }); | ||
} | ||
return blob; | ||
} | ||
return new PonyfillFile([blob], fileName || name, { type: blob.type }); | ||
} | ||
forEach(callback) { | ||
for (const [key, values] of this.map) { | ||
for (const value of values) { | ||
callback(value, key, this); | ||
} | ||
} | ||
} | ||
} | ||
function createController(desiredSize, readable) { | ||
@@ -225,2 +174,77 @@ return { | ||
// Will be removed after v14 reaches EOL | ||
// Needed because v14 doesn't have .stream() implemented | ||
class PonyfillBlob extends buffer.Blob { | ||
stream() { | ||
return new PonyfillReadableStream({ | ||
start: async (controller) => { | ||
const arrayBuffer = await this.arrayBuffer(); | ||
const buffer = Buffer.from(arrayBuffer); | ||
controller.enqueue(buffer); | ||
controller.close(); | ||
}, | ||
}); | ||
} | ||
slice(...args) { | ||
return super.slice(...args); | ||
} | ||
} | ||
class PonyfillFile extends PonyfillBlob { | ||
constructor(fileBits, name, options) { | ||
super(fileBits, options); | ||
this.name = name; | ||
this.webkitRelativePath = ''; | ||
this.lastModified = (options === null || options === void 0 ? void 0 : options.lastModified) || Date.now(); | ||
} | ||
} | ||
class PonyfillFormData { | ||
constructor() { | ||
this.map = new Map(); | ||
} | ||
append(name, value, fileName) { | ||
let values = this.map.get(name); | ||
if (!values) { | ||
values = []; | ||
this.map.set(name, values); | ||
} | ||
const entry = value instanceof PonyfillBlob ? this.getNormalizedFile(name, value, fileName) : value; | ||
values.push(entry); | ||
} | ||
delete(name) { | ||
this.map.delete(name); | ||
} | ||
get(name) { | ||
const values = this.map.get(name); | ||
return values ? values[0] : null; | ||
} | ||
getAll(name) { | ||
return this.map.get(name) || []; | ||
} | ||
has(name) { | ||
return this.map.has(name); | ||
} | ||
set(name, value, fileName) { | ||
const entry = value instanceof PonyfillBlob ? this.getNormalizedFile(name, value, fileName) : value; | ||
this.map.set(name, [entry]); | ||
} | ||
getNormalizedFile(name, blob, fileName) { | ||
if (blob instanceof PonyfillFile) { | ||
if (fileName == null) { | ||
return new PonyfillFile([blob], name, { type: blob.type, lastModified: blob.lastModified }); | ||
} | ||
return blob; | ||
} | ||
return new PonyfillFile([blob], fileName || name, { type: blob.type }); | ||
} | ||
forEach(callback) { | ||
for (const [key, values] of this.map) { | ||
for (const value of values) { | ||
callback(value, key, this); | ||
} | ||
} | ||
} | ||
} | ||
var BodyInitType; | ||
@@ -576,10 +600,11 @@ (function (BodyInitType) { | ||
const nodeHeaders = getHeadersObj(fetchRequest.headers); | ||
const abortListener = function abortListener() { | ||
reject(new PonyfillAbortError()); | ||
const abortListener = function abortListener(event) { | ||
nodeRequest.destroy(); | ||
reject(new PonyfillAbortError(event.detail)); | ||
}; | ||
fetchRequest.signal.addEventListener('abort', abortListener); | ||
const nodeRequest = requestFn(fetchRequest.url, { | ||
// signal: fetchRequest.signal will be added when v14 reaches EOL | ||
method: fetchRequest.method, | ||
headers: nodeHeaders, | ||
signal: fetchRequest.signal, | ||
}, nodeResponse => { | ||
@@ -609,2 +634,3 @@ if (nodeResponse.headers.location) { | ||
}); | ||
nodeRequest.on('error', reject); | ||
if (nodeReadable) { | ||
@@ -611,0 +637,0 @@ nodeReadable.pipe(nodeRequest); |
{ | ||
"name": "@whatwg-node/node-fetch", | ||
"version": "0.0.1-alpha-20221005124650-55319ef", | ||
"version": "0.0.1-alpha-20221005131815-b0ce77a", | ||
"description": "Fetch API implementation for Node", | ||
@@ -10,2 +10,3 @@ "sideEffects": false, | ||
"dependencies": { | ||
"@whatwg-node/events": "0.0.2", | ||
"tslib": "^2.3.1" | ||
@@ -12,0 +13,0 @@ }, |
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
52628
1420
3
+ Added@whatwg-node/events@0.0.2
+ Added@whatwg-node/events@0.0.2(transitive)