chromiumly
Version:
A lightweight Typescript library that interacts with Gotenberg's different modules to convert a variety of document formats to PDF files.
139 lines • 5.94 kB
JavaScript
;
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 {
static normalizeDownloadFrom(downloadFrom) {
return Array.isArray(downloadFrom) ? downloadFrom : [downloadFrom];
}
static buildWebhookHeaders(options) {
if (!options) {
return undefined;
}
const headers = {
'Gotenberg-Webhook-Url': options.webhookUrl,
'Gotenberg-Webhook-Error-Url': options.webhookErrorUrl
};
if (options.webhookMethod) {
headers['Gotenberg-Webhook-Method'] = options.webhookMethod;
}
if (options.webhookErrorMethod) {
headers['Gotenberg-Webhook-Error-Method'] =
options.webhookErrorMethod;
}
if (options.webhookExtraHttpHeaders) {
headers['Gotenberg-Webhook-Extra-Http-Headers'] = JSON.stringify(options.webhookExtraHttpHeaders);
}
if (options.webhookEventsUrl) {
headers['Gotenberg-Webhook-Events-Url'] = options.webhookEventsUrl;
}
return headers;
}
static buildRequestHeaders(username, password, customHttpHeaders, apiKey, requestHttpHeaders) {
const headers = {
...customHttpHeaders,
...requestHttpHeaders
};
if (apiKey) {
headers['X-Api-Key'] = apiKey;
}
else if (username && password) {
const authHeader = 'Basic ' +
Buffer.from(username + ':' + password).toString('base64');
headers['Authorization'] = authHeader;
}
return headers;
}
/**
* 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 {FormData} data - The FormData object to be sent in the POST request.
* @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 {string} [apiKey] - The API key for X-Api-Key authentication. When set, takes precedence over basic auth.
* @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, apiKey, requestHttpHeaders) {
const headers = this.buildRequestHeaders(username, password, customHttpHeaders, apiKey, requestHttpHeaders);
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);
}
static async fetchWithoutBody(endpoint, method, username, password, customHttpHeaders, apiKey) {
const headers = this.buildRequestHeaders(username, password, customHttpHeaders, apiKey);
const response = await fetch(endpoint, {
method,
headers
});
if (!response.ok) {
const body = method === 'HEAD' ? '' : 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}`);
}
if (method === 'HEAD') {
return Buffer.alloc(0);
}
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