@whatwg-node/node-fetch
Advanced tools
Comparing version 0.5.12 to 0.5.13-rc-20240720155335-786d406edf2a8e2def3091aa22b969e5384fe7ea
121
cjs/Blob.js
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.PonyfillBlob = void 0; | ||
exports.hasBufferMethod = hasBufferMethod; | ||
exports.hasArrayBufferMethod = hasArrayBufferMethod; | ||
exports.hasBytesMethod = hasBytesMethod; | ||
exports.hasTextMethod = hasTextMethod; | ||
exports.hasSizeProperty = hasSizeProperty; | ||
exports.hasStreamMethod = hasStreamMethod; | ||
exports.hasBlobSignature = hasBlobSignature; | ||
/* eslint-disable @typescript-eslint/no-unsafe-declaration-merging */ | ||
@@ -21,5 +28,23 @@ const ReadableStream_js_1 = require("./ReadableStream.js"); | ||
} | ||
function isBlob(obj) { | ||
function hasBufferMethod(obj) { | ||
return obj != null && obj.buffer != null; | ||
} | ||
function hasArrayBufferMethod(obj) { | ||
return obj != null && obj.arrayBuffer != null; | ||
} | ||
function hasBytesMethod(obj) { | ||
return obj != null && obj.bytes != null; | ||
} | ||
function hasTextMethod(obj) { | ||
return obj != null && obj.text != null; | ||
} | ||
function hasSizeProperty(obj) { | ||
return obj != null && typeof obj.size === 'number'; | ||
} | ||
function hasStreamMethod(obj) { | ||
return obj != null && obj.stream != null; | ||
} | ||
function hasBlobSignature(obj) { | ||
return obj != null && obj[Symbol.toStringTag] === 'Blob'; | ||
} | ||
// Will be removed after v14 reaches EOL | ||
@@ -31,20 +56,47 @@ // Needed because v14 doesn't have .stream() implemented | ||
this._size = null; | ||
this._buffer = null; | ||
this._text = null; | ||
this.type = options?.type || 'application/octet-stream'; | ||
this.encoding = options?.encoding || 'utf8'; | ||
this._size = options?.size || null; | ||
if (blobParts.length === 1 && isBlob(blobParts[0])) { | ||
if (blobParts.length === 1 && hasBlobSignature(blobParts[0])) { | ||
return blobParts[0]; | ||
} | ||
} | ||
arrayBuffer() { | ||
buffer() { | ||
if (this._buffer) { | ||
return (0, utils_js_1.fakePromise)(this._buffer); | ||
} | ||
if (this.blobParts.length === 1) { | ||
const blobPart = this.blobParts[0]; | ||
if (isBlob(blobPart)) { | ||
return blobPart.arrayBuffer(); | ||
if (hasBufferMethod(blobPart)) { | ||
return blobPart.buffer().then(buf => { | ||
this._buffer = buf; | ||
return this._buffer; | ||
}); | ||
} | ||
return (0, utils_js_1.fakePromise)(getBlobPartAsBuffer(blobPart)); | ||
if (hasBytesMethod(blobPart)) { | ||
return blobPart.bytes().then(bytes => { | ||
this._buffer = Buffer.from(bytes); | ||
return this._buffer; | ||
}); | ||
} | ||
if (hasArrayBufferMethod(blobPart)) { | ||
return blobPart.arrayBuffer().then(arrayBuf => { | ||
this._buffer = Buffer.from(arrayBuf, undefined, blobPart.size); | ||
return this._buffer; | ||
}); | ||
} | ||
this._buffer = getBlobPartAsBuffer(blobPart); | ||
return (0, utils_js_1.fakePromise)(this._buffer); | ||
} | ||
const jobs = []; | ||
const bufferChunks = this.blobParts.map((blobPart, i) => { | ||
if (isBlob(blobPart)) { | ||
if (hasBufferMethod(blobPart)) { | ||
jobs.push(blobPart.buffer().then(buf => { | ||
bufferChunks[i] = buf; | ||
})); | ||
return undefined; | ||
} | ||
else if (hasArrayBufferMethod(blobPart)) { | ||
jobs.push(blobPart.arrayBuffer().then(arrayBuf => { | ||
@@ -55,2 +107,8 @@ bufferChunks[i] = Buffer.from(arrayBuf, undefined, blobPart.size); | ||
} | ||
else if (hasBytesMethod(blobPart)) { | ||
jobs.push(blobPart.bytes().then(bytes => { | ||
bufferChunks[i] = Buffer.from(bytes); | ||
})); | ||
return undefined; | ||
} | ||
else { | ||
@@ -65,15 +123,29 @@ return getBlobPartAsBuffer(blobPart); | ||
} | ||
arrayBuffer() { | ||
return this.buffer(); | ||
} | ||
text() { | ||
if (this._text) { | ||
return (0, utils_js_1.fakePromise)(this._text); | ||
} | ||
if (this.blobParts.length === 1) { | ||
const blobPart = this.blobParts[0]; | ||
if (typeof blobPart === 'string') { | ||
return (0, utils_js_1.fakePromise)(blobPart); | ||
this._text = blobPart; | ||
return (0, utils_js_1.fakePromise)(this._text); | ||
} | ||
if (isBlob(blobPart)) { | ||
return blobPart.text(); | ||
if (hasTextMethod(blobPart)) { | ||
return blobPart.text().then(text => { | ||
this._text = text; | ||
return this._text; | ||
}); | ||
} | ||
const buf = getBlobPartAsBuffer(blobPart); | ||
return (0, utils_js_1.fakePromise)(buf.toString(this.encoding)); | ||
this._text = buf.toString(this.encoding); | ||
return (0, utils_js_1.fakePromise)(this._text); | ||
} | ||
return this.arrayBuffer().then(buf => buf.toString(this.encoding)); | ||
return this.buffer().then(buf => { | ||
this._text = buf.toString(this.encoding); | ||
return this._text; | ||
}); | ||
} | ||
@@ -87,3 +159,3 @@ get size() { | ||
} | ||
else if (isBlob(blobPart)) { | ||
else if (hasSizeProperty(blobPart)) { | ||
this._size += blobPart.size; | ||
@@ -101,3 +173,3 @@ } | ||
const blobPart = this.blobParts[0]; | ||
if (isBlob(blobPart)) { | ||
if (hasStreamMethod(blobPart)) { | ||
return blobPart.stream(); | ||
@@ -113,2 +185,10 @@ } | ||
} | ||
if (this._buffer != null) { | ||
return new ReadableStream_js_1.PonyfillReadableStream({ | ||
start: controller => { | ||
controller.enqueue(this._buffer); | ||
controller.close(); | ||
}, | ||
}); | ||
} | ||
let blobPartIterator; | ||
@@ -130,3 +210,14 @@ return new ReadableStream_js_1.PonyfillReadableStream({ | ||
if (blobPart) { | ||
if (isBlob(blobPart)) { | ||
if (hasBufferMethod(blobPart)) { | ||
return blobPart.buffer().then(buf => { | ||
controller.enqueue(buf); | ||
}); | ||
} | ||
if (hasBytesMethod(blobPart)) { | ||
return blobPart.bytes().then(bytes => { | ||
const buf = Buffer.from(bytes); | ||
controller.enqueue(buf); | ||
}); | ||
} | ||
if (hasArrayBufferMethod(blobPart)) { | ||
return blobPart.arrayBuffer().then(arrayBuffer => { | ||
@@ -133,0 +224,0 @@ const buf = Buffer.from(arrayBuffer, undefined, blobPart.size); |
@@ -192,3 +192,3 @@ "use strict"; | ||
} | ||
arrayBuffer() { | ||
buffer() { | ||
if (this._buffer) { | ||
@@ -198,4 +198,4 @@ return (0, utils_js_1.fakePromise)(this._buffer); | ||
if (this.bodyType === BodyInitType.Blob) { | ||
if (this.bodyInit instanceof Blob_js_1.PonyfillBlob) { | ||
return this.bodyInit.arrayBuffer().then(buf => { | ||
if ((0, Blob_js_1.hasBufferMethod)(this.bodyInit)) { | ||
return this.bodyInit.buffer().then(buf => { | ||
this._buffer = buf; | ||
@@ -205,7 +205,14 @@ return this._buffer; | ||
} | ||
const bodyInitTyped = this.bodyInit; | ||
return bodyInitTyped.arrayBuffer().then(arrayBuffer => { | ||
this._buffer = Buffer.from(arrayBuffer, undefined, bodyInitTyped.size); | ||
return this._buffer; | ||
}); | ||
if ((0, Blob_js_1.hasBytesMethod)(this.bodyInit)) { | ||
return this.bodyInit.bytes().then(bytes => { | ||
this._buffer = Buffer.from(bytes); | ||
return this._buffer; | ||
}); | ||
} | ||
if ((0, Blob_js_1.hasArrayBufferMethod)(this.bodyInit)) { | ||
return this.bodyInit.arrayBuffer().then(buf => { | ||
this._buffer = Buffer.from(buf, undefined, buf.byteLength); | ||
return this._buffer; | ||
}); | ||
} | ||
} | ||
@@ -221,2 +228,8 @@ return this._collectChunksFromReadable().then(chunks => { | ||
} | ||
bytes() { | ||
return this.buffer(); | ||
} | ||
arrayBuffer() { | ||
return this.buffer(); | ||
} | ||
json() { | ||
@@ -227,3 +240,11 @@ if (this._json) { | ||
return this.text().then(text => { | ||
this._json = JSON.parse(text); | ||
try { | ||
this._json = JSON.parse(text); | ||
} | ||
catch (e) { | ||
if (e instanceof SyntaxError) { | ||
e.message += `, "${text}" is not valid JSON`; | ||
} | ||
throw e; | ||
} | ||
return this._json; | ||
@@ -240,3 +261,3 @@ }); | ||
} | ||
return this.arrayBuffer().then(buffer => { | ||
return this.buffer().then(buffer => { | ||
this._text = buffer.toString('utf-8'); | ||
@@ -243,0 +264,0 @@ return this._text; |
114
esm/Blob.js
@@ -18,5 +18,23 @@ /* eslint-disable @typescript-eslint/no-unsafe-declaration-merging */ | ||
} | ||
function isBlob(obj) { | ||
export function hasBufferMethod(obj) { | ||
return obj != null && obj.buffer != null; | ||
} | ||
export function hasArrayBufferMethod(obj) { | ||
return obj != null && obj.arrayBuffer != null; | ||
} | ||
export function hasBytesMethod(obj) { | ||
return obj != null && obj.bytes != null; | ||
} | ||
export function hasTextMethod(obj) { | ||
return obj != null && obj.text != null; | ||
} | ||
export function hasSizeProperty(obj) { | ||
return obj != null && typeof obj.size === 'number'; | ||
} | ||
export function hasStreamMethod(obj) { | ||
return obj != null && obj.stream != null; | ||
} | ||
export function hasBlobSignature(obj) { | ||
return obj != null && obj[Symbol.toStringTag] === 'Blob'; | ||
} | ||
// Will be removed after v14 reaches EOL | ||
@@ -28,20 +46,47 @@ // Needed because v14 doesn't have .stream() implemented | ||
this._size = null; | ||
this._buffer = null; | ||
this._text = null; | ||
this.type = options?.type || 'application/octet-stream'; | ||
this.encoding = options?.encoding || 'utf8'; | ||
this._size = options?.size || null; | ||
if (blobParts.length === 1 && isBlob(blobParts[0])) { | ||
if (blobParts.length === 1 && hasBlobSignature(blobParts[0])) { | ||
return blobParts[0]; | ||
} | ||
} | ||
arrayBuffer() { | ||
buffer() { | ||
if (this._buffer) { | ||
return fakePromise(this._buffer); | ||
} | ||
if (this.blobParts.length === 1) { | ||
const blobPart = this.blobParts[0]; | ||
if (isBlob(blobPart)) { | ||
return blobPart.arrayBuffer(); | ||
if (hasBufferMethod(blobPart)) { | ||
return blobPart.buffer().then(buf => { | ||
this._buffer = buf; | ||
return this._buffer; | ||
}); | ||
} | ||
return fakePromise(getBlobPartAsBuffer(blobPart)); | ||
if (hasBytesMethod(blobPart)) { | ||
return blobPart.bytes().then(bytes => { | ||
this._buffer = Buffer.from(bytes); | ||
return this._buffer; | ||
}); | ||
} | ||
if (hasArrayBufferMethod(blobPart)) { | ||
return blobPart.arrayBuffer().then(arrayBuf => { | ||
this._buffer = Buffer.from(arrayBuf, undefined, blobPart.size); | ||
return this._buffer; | ||
}); | ||
} | ||
this._buffer = getBlobPartAsBuffer(blobPart); | ||
return fakePromise(this._buffer); | ||
} | ||
const jobs = []; | ||
const bufferChunks = this.blobParts.map((blobPart, i) => { | ||
if (isBlob(blobPart)) { | ||
if (hasBufferMethod(blobPart)) { | ||
jobs.push(blobPart.buffer().then(buf => { | ||
bufferChunks[i] = buf; | ||
})); | ||
return undefined; | ||
} | ||
else if (hasArrayBufferMethod(blobPart)) { | ||
jobs.push(blobPart.arrayBuffer().then(arrayBuf => { | ||
@@ -52,2 +97,8 @@ bufferChunks[i] = Buffer.from(arrayBuf, undefined, blobPart.size); | ||
} | ||
else if (hasBytesMethod(blobPart)) { | ||
jobs.push(blobPart.bytes().then(bytes => { | ||
bufferChunks[i] = Buffer.from(bytes); | ||
})); | ||
return undefined; | ||
} | ||
else { | ||
@@ -62,15 +113,29 @@ return getBlobPartAsBuffer(blobPart); | ||
} | ||
arrayBuffer() { | ||
return this.buffer(); | ||
} | ||
text() { | ||
if (this._text) { | ||
return fakePromise(this._text); | ||
} | ||
if (this.blobParts.length === 1) { | ||
const blobPart = this.blobParts[0]; | ||
if (typeof blobPart === 'string') { | ||
return fakePromise(blobPart); | ||
this._text = blobPart; | ||
return fakePromise(this._text); | ||
} | ||
if (isBlob(blobPart)) { | ||
return blobPart.text(); | ||
if (hasTextMethod(blobPart)) { | ||
return blobPart.text().then(text => { | ||
this._text = text; | ||
return this._text; | ||
}); | ||
} | ||
const buf = getBlobPartAsBuffer(blobPart); | ||
return fakePromise(buf.toString(this.encoding)); | ||
this._text = buf.toString(this.encoding); | ||
return fakePromise(this._text); | ||
} | ||
return this.arrayBuffer().then(buf => buf.toString(this.encoding)); | ||
return this.buffer().then(buf => { | ||
this._text = buf.toString(this.encoding); | ||
return this._text; | ||
}); | ||
} | ||
@@ -84,3 +149,3 @@ get size() { | ||
} | ||
else if (isBlob(blobPart)) { | ||
else if (hasSizeProperty(blobPart)) { | ||
this._size += blobPart.size; | ||
@@ -98,3 +163,3 @@ } | ||
const blobPart = this.blobParts[0]; | ||
if (isBlob(blobPart)) { | ||
if (hasStreamMethod(blobPart)) { | ||
return blobPart.stream(); | ||
@@ -110,2 +175,10 @@ } | ||
} | ||
if (this._buffer != null) { | ||
return new PonyfillReadableStream({ | ||
start: controller => { | ||
controller.enqueue(this._buffer); | ||
controller.close(); | ||
}, | ||
}); | ||
} | ||
let blobPartIterator; | ||
@@ -127,3 +200,14 @@ return new PonyfillReadableStream({ | ||
if (blobPart) { | ||
if (isBlob(blobPart)) { | ||
if (hasBufferMethod(blobPart)) { | ||
return blobPart.buffer().then(buf => { | ||
controller.enqueue(buf); | ||
}); | ||
} | ||
if (hasBytesMethod(blobPart)) { | ||
return blobPart.bytes().then(bytes => { | ||
const buf = Buffer.from(bytes); | ||
controller.enqueue(buf); | ||
}); | ||
} | ||
if (hasArrayBufferMethod(blobPart)) { | ||
return blobPart.arrayBuffer().then(arrayBuffer => { | ||
@@ -130,0 +214,0 @@ const buf = Buffer.from(arrayBuffer, undefined, blobPart.size); |
import { Readable } from 'stream'; | ||
import busboy from 'busboy'; | ||
import { PonyfillBlob } from './Blob.js'; | ||
import { hasArrayBufferMethod, hasBufferMethod, hasBytesMethod, PonyfillBlob } from './Blob.js'; | ||
import { PonyfillFile } from './File.js'; | ||
@@ -188,3 +188,3 @@ import { getStreamFromFormData, PonyfillFormData } from './FormData.js'; | ||
} | ||
arrayBuffer() { | ||
buffer() { | ||
if (this._buffer) { | ||
@@ -194,4 +194,4 @@ return fakePromise(this._buffer); | ||
if (this.bodyType === BodyInitType.Blob) { | ||
if (this.bodyInit instanceof PonyfillBlob) { | ||
return this.bodyInit.arrayBuffer().then(buf => { | ||
if (hasBufferMethod(this.bodyInit)) { | ||
return this.bodyInit.buffer().then(buf => { | ||
this._buffer = buf; | ||
@@ -201,7 +201,14 @@ return this._buffer; | ||
} | ||
const bodyInitTyped = this.bodyInit; | ||
return bodyInitTyped.arrayBuffer().then(arrayBuffer => { | ||
this._buffer = Buffer.from(arrayBuffer, undefined, bodyInitTyped.size); | ||
return this._buffer; | ||
}); | ||
if (hasBytesMethod(this.bodyInit)) { | ||
return this.bodyInit.bytes().then(bytes => { | ||
this._buffer = Buffer.from(bytes); | ||
return this._buffer; | ||
}); | ||
} | ||
if (hasArrayBufferMethod(this.bodyInit)) { | ||
return this.bodyInit.arrayBuffer().then(buf => { | ||
this._buffer = Buffer.from(buf, undefined, buf.byteLength); | ||
return this._buffer; | ||
}); | ||
} | ||
} | ||
@@ -217,2 +224,8 @@ return this._collectChunksFromReadable().then(chunks => { | ||
} | ||
bytes() { | ||
return this.buffer(); | ||
} | ||
arrayBuffer() { | ||
return this.buffer(); | ||
} | ||
json() { | ||
@@ -223,3 +236,11 @@ if (this._json) { | ||
return this.text().then(text => { | ||
this._json = JSON.parse(text); | ||
try { | ||
this._json = JSON.parse(text); | ||
} | ||
catch (e) { | ||
if (e instanceof SyntaxError) { | ||
e.message += `, "${text}" is not valid JSON`; | ||
} | ||
throw e; | ||
} | ||
return this._json; | ||
@@ -236,3 +257,3 @@ }); | ||
} | ||
return this.arrayBuffer().then(buffer => { | ||
return this.buffer().then(buffer => { | ||
this._text = buffer.toString('utf-8'); | ||
@@ -239,0 +260,0 @@ return this._text; |
{ | ||
"name": "@whatwg-node/node-fetch", | ||
"version": "0.5.12", | ||
"version": "0.5.13-rc-20240720155335-786d406edf2a8e2def3091aa22b969e5384fe7ea", | ||
"description": "Fetch API implementation for Node", | ||
@@ -5,0 +5,0 @@ "sideEffects": false, |
@@ -17,2 +17,21 @@ interface BlobOptions { | ||
} | ||
export declare function hasBufferMethod(obj: any): obj is { | ||
buffer(): Promise<Buffer>; | ||
}; | ||
export declare function hasArrayBufferMethod(obj: any): obj is { | ||
arrayBuffer(): Promise<ArrayBuffer>; | ||
}; | ||
export declare function hasBytesMethod(obj: any): obj is { | ||
bytes(): Promise<Uint8Array>; | ||
}; | ||
export declare function hasTextMethod(obj: any): obj is { | ||
text(): Promise<string>; | ||
}; | ||
export declare function hasSizeProperty(obj: any): obj is { | ||
size: number; | ||
}; | ||
export declare function hasStreamMethod(obj: any): obj is { | ||
stream(): any; | ||
}; | ||
export declare function hasBlobSignature(obj: any): obj is Blob; | ||
export declare class PonyfillBlob implements Blob { | ||
@@ -24,3 +43,6 @@ private blobParts; | ||
constructor(blobParts: BlobPart[], options?: BlobOptions); | ||
arrayBuffer(): Promise<Buffer>; | ||
_buffer: Buffer | null; | ||
buffer(): Promise<Buffer>; | ||
arrayBuffer(): Promise<ArrayBuffer>; | ||
_text: string | null; | ||
text(): Promise<string>; | ||
@@ -27,0 +49,0 @@ get size(): number; |
@@ -39,3 +39,5 @@ import { Readable } from 'stream'; | ||
}): Promise<PonyfillFormData>; | ||
arrayBuffer(): Promise<Buffer>; | ||
buffer(): Promise<Buffer>; | ||
bytes(): Promise<Uint8Array>; | ||
arrayBuffer(): Promise<ArrayBuffer>; | ||
_json: TJSON | null; | ||
@@ -42,0 +44,0 @@ json(): Promise<TJSON>; |
Sorry, the diff of this file is not supported yet
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
166345
4257