UNPKG

@huggingface/blob

Version:

Utilities to convert URLs and files to Blobs, internally used by Hugging Face libs

112 lines (110 loc) 3.52 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/utils/FileBlob.ts var FileBlob_exports = {}; __export(FileBlob_exports, { FileBlob: () => FileBlob }); module.exports = __toCommonJS(FileBlob_exports); var import_node_fs = require("fs"); var import_promises = require("fs/promises"); var import_node_stream = require("stream"); var import_node_url = require("url"); var FileBlob = class extends Blob { /** * Creates a new FileBlob on the provided file. * * @param path Path to the file to be lazy readed */ static async create(path) { path = path instanceof URL ? (0, import_node_url.fileURLToPath)(path) : path; const { size } = await (0, import_promises.stat)(path); const fileBlob = new FileBlob(path, 0, size); return fileBlob; } path; start; end; constructor(path, start, end) { super(); this.path = path; this.start = start; this.end = end; } /** * Returns the size of the blob. */ get size() { return this.end - this.start; } /** * Returns a new instance of FileBlob that is a slice of the current one. * * The slice is inclusive of the start and exclusive of the end. * * The slice method does not supports negative start/end. * * @param start beginning of the slice * @param end end of the slice */ slice(start = 0, end = this.size) { if (start < 0 || end < 0) { new TypeError("Unsupported negative start/end on FileBlob.slice"); } const slice = new FileBlob(this.path, this.start + start, Math.min(this.start + end, this.end)); return slice; } /** * Read the part of the file delimited by the FileBlob and returns it as an ArrayBuffer. */ async arrayBuffer() { const slice = await this.execute((file) => file.read(Buffer.alloc(this.size), 0, this.size, this.start)); return slice.buffer; } /** * Read the part of the file delimited by the FileBlob and returns it as a string. */ async text() { const buffer = await this.arrayBuffer(); return buffer.toString("utf8"); } /** * Returns a stream around the part of the file delimited by the FileBlob. */ stream() { return import_node_stream.Readable.toWeb((0, import_node_fs.createReadStream)(this.path, { start: this.start, end: this.end - 1 })); } /** * We are opening and closing the file for each action to prevent file descriptor leaks. * * It is an intended choice of developer experience over performances. */ async execute(action) { const file = await (0, import_promises.open)(this.path, "r"); try { return await action(file); } finally { await file.close(); } } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { FileBlob });