CoolFace
Datasetpublic

basant307/AI_Governance_Project

sourceHugging Faceapache-2.0updated 2mo agoView on Hugging Face
0likes48downloads
blobHelpers.js81 linesDownload Raw Back to cjs
1"use strict";2/*! Based on fetch-blob. MIT License. Jimmy Wärting <https://jimmy.warting.se/opensource> & David Frank */3Object.defineProperty(exports, "__esModule", { value: true });4exports.sliceBlob = exports.consumeBlobParts = void 0;5const isFunction_1 = require("./isFunction");6const CHUNK_SIZE = 65536;7async function* clonePart(part) {8    const end = part.byteOffset + part.byteLength;9    let position = part.byteOffset;10    while (position !== end) {11        const size = Math.min(end - position, CHUNK_SIZE);12        const chunk = part.buffer.slice(position, position + size);13        position += chunk.byteLength;14        yield new Uint8Array(chunk);15    }16}17async function* consumeNodeBlob(blob) {18    let position = 0;19    while (position !== blob.size) {20        const chunk = blob.slice(position, Math.min(blob.size, position + CHUNK_SIZE));21        const buffer = await chunk.arrayBuffer();22        position += buffer.byteLength;23        yield new Uint8Array(buffer);24    }25}26async function* consumeBlobParts(parts, clone = false) {27    for (const part of parts) {28        if (ArrayBuffer.isView(part)) {29            if (clone) {30                yield* clonePart(part);31            }32            else {33                yield part;34            }35        }36        else if ((0, isFunction_1.isFunction)(part.stream)) {37            yield* part.stream();38        }39        else {40            yield* consumeNodeBlob(part);41        }42    }43}44exports.consumeBlobParts = consumeBlobParts;45function* sliceBlob(blobParts, blobSize, start = 0, end) {46    end !== null && end !== void 0 ? end : (end = blobSize);47    let relativeStart = start < 048        ? Math.max(blobSize + start, 0)49        : Math.min(start, blobSize);50    let relativeEnd = end < 051        ? Math.max(blobSize + end, 0)52        : Math.min(end, blobSize);53    const span = Math.max(relativeEnd - relativeStart, 0);54    let added = 0;55    for (const part of blobParts) {56        if (added >= span) {57            break;58        }59        const partSize = ArrayBuffer.isView(part) ? part.byteLength : part.size;60        if (relativeStart && partSize <= relativeStart) {61            relativeStart -= partSize;62            relativeEnd -= partSize;63        }64        else {65            let chunk;66            if (ArrayBuffer.isView(part)) {67                chunk = part.subarray(relativeStart, Math.min(partSize, relativeEnd));68                added += chunk.byteLength;69            }70            else {71                chunk = part.slice(relativeStart, Math.min(partSize, relativeEnd));72                added += chunk.size;73            }74            relativeEnd -= partSize;75            relativeStart = 0;76            yield chunk;77        }78    }79}80exports.sliceBlob = sliceBlob;81 
basant307/AI_Governance_Project · CoolFace