UNPKG

jorel

Version:

The easiest way to use LLMs, including streams, images, documents, tools and various agent scenarios.

29 lines (28 loc) 1.05 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.fetchFileAsBuffer = fetchFileAsBuffer; const axios_1 = require("axios"); /** * Fetches a file from the given URL and returns it as a buffer. * @param url The URL to fetch the file from. * @param fallbackMimeType Fallback MIME type to use if the response headers do not contain a MIME type. * @returns The file as a buffer and its MIME type. */ async function fetchFileAsBuffer(url, fallbackMimeType) { let response; try { response = await axios_1.default.get(url, { responseType: "arraybuffer" }); } catch (error) { throw new Error(`Failed to fetch file from URL: ${url}. ${error instanceof Error ? error.message : "Unknown error"}`); } const mimeType = response.headers["content-type"] || fallbackMimeType; if (!mimeType) { throw new Error("Unable to detect MIME type from the response headers."); } const buffer = Buffer.from(response.data); return { buffer, mimeType, }; }