UNPKG

chromiumly

Version:

A lightweight Typescript library that interacts with Gotenberg's different modules to convert a variety of document formats to PDF files.

84 lines 3.61 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.GotenbergUtils = void 0; const fs_1 = require("fs"); const consumers_1 = require("node:stream/consumers"); /** * Utility class for common tasks related to the Gotenberg service. */ class GotenbergUtils { /** * Asserts that a condition is true; otherwise, throws an error with the specified message. * * @param {boolean} condition - The condition to assert. * @param {string} message - The error message to throw if the condition is false. * @throws {Error} Throws an error with the specified message if the condition is false. */ static assert(condition, message) { if (!condition) { throw new Error(message); } } /** * Performs a POST request to the specified Gotenberg endpoint with the provided FormData. * * @param {string} endpoint - The Gotenberg endpoint URL. * @param {string} username - The username for basic authentication. * @param {string} password - The password for basic authentication. * @param {Record<string, string>} [customHttpHeaders] - Custom HTTP headers to be sent with the request. * @param {FormData} data - The FormData object to be sent in the POST request. * @param {Record<string, string>} customHeaders - List of custom headers to include in the fetch * @returns {Promise<Buffer>} A Promise that resolves to the response body as a buffer. * @throws {Error} Throws an error if the HTTP response status is not OK. */ static async fetch(endpoint, data, username, password, customHttpHeaders) { const headers = { ...customHttpHeaders }; if (username && password) { const authHeader = 'Basic ' + Buffer.from(username + ':' + password).toString('base64'); headers['Authorization'] = authHeader; } const response = await fetch(endpoint, { method: 'POST', body: data, headers }); if (!response.ok) { const body = await response.text(); const trace = response.headers.get('gotenberg-trace'); throw new Error(`Gotenberg API Error:\n` + `Endpoint: ${endpoint}\n` + `Status: ${response.status} ${response.statusText}\n` + `Trace: ${trace || 'No trace'}\n` + `Body: ${body}`); } const arrayBuffer = await response.arrayBuffer(); return Buffer.from(arrayBuffer); } /** * Adds a file to the FormData object. * * @param {FormData} data - The FormData object to which the file will be added. * @param {PathLikeOrReadStream} file - The file to be added (either a PathLike or a ReadStream). * @param {string} name - The name to be used for the file in the FormData. * @returns {Promise<void>} A Promise that resolves once the file has been added. */ static async addFile(data, file, name) { if (Buffer.isBuffer(file)) { data.append('files', new Blob([file]), name); } else if (file instanceof fs_1.ReadStream) { const content = await (0, consumers_1.blob)(file); data.append('files', content, name); } else { await fs_1.promises.access(file, fs_1.constants.R_OK); const content = await (0, fs_1.openAsBlob)(file); data.append('files', content, name); } } } exports.GotenbergUtils = GotenbergUtils; //# sourceMappingURL=gotenberg.utils.js.map