AI_botter/node_modules/formdata-node/lib/cjs/blobHelpers.js

81 lines
2.7 KiB
JavaScript
Raw Normal View History

2024-05-01 02:46:10 +01:00
"use strict";
/*! Based on fetch-blob. MIT License. Jimmy Wärting <https://jimmy.warting.se/opensource> & David Frank */
Object.defineProperty(exports, "__esModule", { value: true });
exports.sliceBlob = exports.consumeBlobParts = void 0;
const isFunction_1 = require("./isFunction");
const CHUNK_SIZE = 65536;
async function* clonePart(part) {
const end = part.byteOffset + part.byteLength;
let position = part.byteOffset;
while (position !== end) {
const size = Math.min(end - position, CHUNK_SIZE);
const chunk = part.buffer.slice(position, position + size);
position += chunk.byteLength;
yield new Uint8Array(chunk);
}
}
async function* consumeNodeBlob(blob) {
let position = 0;
while (position !== blob.size) {
const chunk = blob.slice(position, Math.min(blob.size, position + CHUNK_SIZE));
const buffer = await chunk.arrayBuffer();
position += buffer.byteLength;
yield new Uint8Array(buffer);
}
}
async function* consumeBlobParts(parts, clone = false) {
for (const part of parts) {
if (ArrayBuffer.isView(part)) {
if (clone) {
yield* clonePart(part);
}
else {
yield part;
}
}
else if ((0, isFunction_1.isFunction)(part.stream)) {
yield* part.stream();
}
else {
yield* consumeNodeBlob(part);
}
}
}
exports.consumeBlobParts = consumeBlobParts;
function* sliceBlob(blobParts, blobSize, start = 0, end) {
end !== null && end !== void 0 ? end : (end = blobSize);
let relativeStart = start < 0
? Math.max(blobSize + start, 0)
: Math.min(start, blobSize);
let relativeEnd = end < 0
? Math.max(blobSize + end, 0)
: Math.min(end, blobSize);
const span = Math.max(relativeEnd - relativeStart, 0);
let added = 0;
for (const part of blobParts) {
if (added >= span) {
break;
}
const partSize = ArrayBuffer.isView(part) ? part.byteLength : part.size;
if (relativeStart && partSize <= relativeStart) {
relativeStart -= partSize;
relativeEnd -= partSize;
}
else {
let chunk;
if (ArrayBuffer.isView(part)) {
chunk = part.subarray(relativeStart, Math.min(partSize, relativeEnd));
added += chunk.byteLength;
}
else {
chunk = part.slice(relativeStart, Math.min(partSize, relativeEnd));
added += chunk.size;
}
relativeEnd -= partSize;
relativeStart = 0;
yield chunk;
}
}
}
exports.sliceBlob = sliceBlob;