chromiumly
Version:
A lightweight Typescript library that interacts with Gotenberg's different modules to convert a variety of document formats to PDF files.
87 lines • 3.93 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.GotenbergUtils = void 0;
const tslib_1 = require("tslib");
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 fetch(endpoint, data, username, password, customHttpHeaders) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const headers = Object.assign({}, customHttpHeaders);
if (username && password) {
const authHeader = 'Basic ' +
Buffer.from(username + ':' + password).toString('base64');
headers['Authorization'] = authHeader;
}
const response = yield fetch(endpoint, {
method: 'POST',
body: data,
headers
});
if (!response.ok) {
const body = yield 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 = yield 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 addFile(data, file, name) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
if (Buffer.isBuffer(file)) {
data.append('files', new Blob([file]), name);
}
else if (file instanceof fs_1.ReadStream) {
const content = yield (0, consumers_1.blob)(file);
data.append('files', content, name);
}
else {
yield fs_1.promises.access(file, fs_1.constants.R_OK);
const content = yield (0, fs_1.openAsBlob)(file);
data.append('files', content, name);
}
});
}
}
exports.GotenbergUtils = GotenbergUtils;
//# sourceMappingURL=gotenberg.utils.js.map