@whatwg-node/node-fetch
Advanced tools
Comparing version 0.0.7-alpha-20230209150810-7d2cb9f to 0.0.7-alpha-20230209162208-3a05ba1
/// <reference types="node" /> | ||
import { Blob as NodeBlob } from 'buffer'; | ||
declare const PonyfillBlob_base: typeof NodeBlob; | ||
export declare class PonyfillBlob extends PonyfillBlob_base implements Blob { | ||
import { BlobOptions } from 'buffer'; | ||
export declare class PonyfillBlob implements Blob { | ||
private blobParts; | ||
type: string; | ||
private encoding; | ||
constructor(blobParts: BlobPart[], options?: BlobOptions); | ||
arrayBuffer(): Promise<ArrayBuffer>; | ||
text(): Promise<string>; | ||
get size(): number; | ||
stream(): any; | ||
slice(...args: any[]): any; | ||
slice(): any; | ||
} | ||
@@ -12,2 +18,1 @@ export interface PonyfillBlob { | ||
} | ||
export {}; |
148
index.js
@@ -12,3 +12,2 @@ 'use strict'; | ||
const url = require('url'); | ||
const buffer = require('buffer'); | ||
const events = require('@whatwg-node/events'); | ||
@@ -202,22 +201,123 @@ const busboy = _interopDefault(require('busboy')); | ||
class DummyBlob { | ||
constructor() { | ||
throw new Error('Blob is not supported in this environment, if you are using an older version of Node v14, please upgrade to v14.17.0 or higher'); | ||
function getHeadersObj(headers) { | ||
if (headers == null || !('forEach' in headers)) { | ||
return headers; | ||
} | ||
const obj = {}; | ||
headers.forEach((value, key) => { | ||
obj[key] = value; | ||
}); | ||
return obj; | ||
} | ||
function uint8ArrayToBuffer(uint8array) { | ||
return uint8array.buffer.slice(uint8array.byteOffset, uint8array.byteOffset + uint8array.byteLength); | ||
} | ||
function getBlobPartAsBuffer(blobPart) { | ||
if (typeof blobPart === 'string') { | ||
return Buffer.from(blobPart); | ||
} | ||
else if (Buffer.isBuffer(blobPart)) { | ||
return blobPart; | ||
} | ||
else if (blobPart instanceof Uint8Array) { | ||
return Buffer.from(blobPart); | ||
} | ||
else if ('buffer' in blobPart) { | ||
return Buffer.from(blobPart.buffer, blobPart.byteOffset, blobPart.byteLength); | ||
} | ||
else { | ||
return Buffer.from(blobPart); | ||
} | ||
} | ||
function isBlob(obj) { | ||
return obj != null && typeof obj === 'object' && obj.stream != null; | ||
} | ||
// Will be removed after v14 reaches EOL | ||
// Needed because v14 doesn't have .stream() implemented | ||
class PonyfillBlob extends (buffer.Blob || DummyBlob) { | ||
class PonyfillBlob { | ||
constructor(blobParts, options) { | ||
this.blobParts = blobParts; | ||
this.type = (options === null || options === void 0 ? void 0 : options.type) || 'application/octet-stream'; | ||
this.encoding = (options === null || options === void 0 ? void 0 : options.encoding) || 'utf8'; | ||
} | ||
async arrayBuffer() { | ||
const bufferChunks = []; | ||
for (const blobPart of this.blobParts) { | ||
if (isBlob(blobPart)) { | ||
const arrayBuf = await blobPart.arrayBuffer(); | ||
const buf = Buffer.from(arrayBuf, undefined, blobPart.size); | ||
bufferChunks.push(buf); | ||
} | ||
else { | ||
const buf = getBlobPartAsBuffer(blobPart); | ||
bufferChunks.push(buf); | ||
} | ||
} | ||
return uint8ArrayToBuffer(Buffer.concat(bufferChunks)); | ||
} | ||
async text() { | ||
let text = ''; | ||
for (const blobPart of this.blobParts) { | ||
if (typeof blobPart === 'string') { | ||
text += blobPart; | ||
} | ||
else if ('text' in blobPart) { | ||
text += await blobPart.text(); | ||
} | ||
else { | ||
const buf = getBlobPartAsBuffer(blobPart); | ||
text += buf.toString(this.encoding); | ||
} | ||
} | ||
return text; | ||
} | ||
get size() { | ||
let size = 0; | ||
for (const blobPart of this.blobParts) { | ||
if (typeof blobPart === 'string') { | ||
size += Buffer.byteLength(blobPart); | ||
} | ||
else if (isBlob(blobPart)) { | ||
size += blobPart.size; | ||
} | ||
else if ('length' in blobPart) { | ||
size += blobPart.length; | ||
} | ||
else if ('byteLength' in blobPart) { | ||
size += blobPart.byteLength; | ||
} | ||
} | ||
return size; | ||
} | ||
stream() { | ||
let partQueue = []; | ||
return new PonyfillReadableStream({ | ||
start: async (controller) => { | ||
const arrayBuffer = await this.arrayBuffer(); | ||
const buffer = Buffer.from(arrayBuffer); | ||
controller.enqueue(buffer); | ||
controller.close(); | ||
start: controller => { | ||
partQueue = [...this.blobParts]; | ||
if (partQueue.length === 0) { | ||
controller.close(); | ||
} | ||
}, | ||
pull: async (controller) => { | ||
const blobPart = partQueue.pop(); | ||
if (blobPart) { | ||
if (isBlob(blobPart)) { | ||
for await (const chunk of blobPart.stream()) { | ||
controller.enqueue(chunk); | ||
} | ||
} | ||
else { | ||
const buf = getBlobPartAsBuffer(blobPart); | ||
controller.enqueue(buf); | ||
} | ||
} | ||
else { | ||
controller.close(); | ||
} | ||
}, | ||
}); | ||
} | ||
slice(...args) { | ||
return super.slice(...args); | ||
slice() { | ||
throw new Error('Not implemented'); | ||
} | ||
@@ -287,3 +387,3 @@ } | ||
} | ||
const entry = isBlob(value) | ||
const entry = isBlob$1(value) | ||
? getNormalizedFile(name, value, fileName) | ||
@@ -307,3 +407,3 @@ : value; | ||
set(name, value, fileName) { | ||
const entry = isBlob(value) | ||
const entry = isBlob$1(value) | ||
? getNormalizedFile(name, value, fileName) | ||
@@ -390,3 +490,3 @@ : value; | ||
} | ||
function isBlob(value) { | ||
function isBlob$1(value) { | ||
return value != null && typeof value === 'object' && typeof value.arrayBuffer === 'function'; | ||
@@ -460,3 +560,4 @@ } | ||
if (this.bodyType === BodyInitType.Uint8Array || this.bodyType === BodyInitType.Buffer) { | ||
return this.bodyInit.buffer; | ||
const typedBodyInit = this.bodyInit; | ||
return uint8ArrayToBuffer(typedBodyInit); | ||
} | ||
@@ -662,3 +763,3 @@ const blob = await this.blob(); | ||
bodyFactory() { | ||
const buffer = Buffer.from(bodyInit.buffer, bodyInit.byteOffset, bodyInit.byteLength); | ||
const buffer = Buffer.from(bodyInit); | ||
const readable = stream.Readable.from(buffer); | ||
@@ -937,13 +1038,2 @@ const body = new PonyfillReadableStream(readable); | ||
function getHeadersObj(headers) { | ||
if (headers == null || !('forEach' in headers)) { | ||
return headers; | ||
} | ||
const obj = {}; | ||
headers.forEach((value, key) => { | ||
obj[key] = value; | ||
}); | ||
return obj; | ||
} | ||
function getResponseForFile(url$1) { | ||
@@ -977,3 +1067,3 @@ const path = url.fileURLToPath(url$1); | ||
if (mimeType.endsWith(BASE64_SUFFIX)) { | ||
const buffer = Buffer.from(data, 'base64'); | ||
const buffer = Buffer.from(data, 'base64url'); | ||
const realMimeType = mimeType.slice(0, -BASE64_SUFFIX.length); | ||
@@ -980,0 +1070,0 @@ const file = new PonyfillBlob([buffer], { type: realMimeType }); |
{ | ||
"name": "@whatwg-node/node-fetch", | ||
"version": "0.0.7-alpha-20230209150810-7d2cb9f", | ||
"version": "0.0.7-alpha-20230209162208-3a05ba1", | ||
"description": "Fetch API implementation for Node", | ||
@@ -5,0 +5,0 @@ "sideEffects": false, |
export declare function getHeadersObj(headers: Headers): Record<string, string>; | ||
export declare function uint8ArrayToBuffer(uint8array: Uint8Array): ArrayBuffer; |
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
94258
2542