From bc53f03275d7bb2f4fc5e654f80b507821af60ec Mon Sep 17 00:00:00 2001 From: Jurriaan Barkey Wolf Date: Fri, 12 Jan 2024 14:03:07 +0100 Subject: [PATCH] Issue #345: Make types for template more liberal: what works with Buffer should work with ArrayBuffer, with the benefit that browsers don't need a polyfill. --- src/__tests__/images.test.ts | 2 +- src/main.ts | 6 +++--- src/processTemplate.ts | 4 ++-- src/types.ts | 6 +++--- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/__tests__/images.test.ts b/src/__tests__/images.test.ts index 9a1a08a3..8e637c9e 100644 --- a/src/__tests__/images.test.ts +++ b/src/__tests__/images.test.ts @@ -423,7 +423,7 @@ it('010: can inject an image in a document that already contains images inserted `); const reportB = await createReport({ - template: Buffer.from(reportA), + template: reportA, cmdDelimiter: '---', data: { injectImg: () => ({ diff --git a/src/main.ts b/src/main.ts index 75f71f03..86e46fce 100644 --- a/src/main.ts +++ b/src/main.ts @@ -39,7 +39,7 @@ const CONTENT_TYPES_PATH = '[Content_Types].xml' as const; const TEMPLATE_PATH = 'word' as const; const XML_FILE_REGEX = new RegExp(`${TEMPLATE_PATH}\\/[^\\/]+\\.xml`); -export async function parseTemplate(template: Buffer) { +export async function parseTemplate(template: ArrayBuffer) { logger.debug('Unzipping...'); const zip = await zipLoad(template); @@ -319,7 +319,7 @@ async function createReport( * @param delimiter the command delimiter (defaults to ['+++', '+++']) */ export async function listCommands( - template: Buffer, + template: ArrayBuffer, delimiter?: string | [string, string] ): Promise { const opts: CreateReportOptions = { @@ -371,7 +371,7 @@ export async function listCommands( * Extract metadata from a document, such as the number of pages or words. * @param template the docx template as a Buffer-like object */ -export async function getMetadata(template: Buffer) { +export async function getMetadata(template: ArrayBuffer) { const app_xml_path = `docProps/app.xml`; const core_xml_path = `docProps/core.xml`; const zip = await zipLoad(template); diff --git a/src/processTemplate.ts b/src/processTemplate.ts index 2ef90a60..5b06b066 100755 --- a/src/processTemplate.ts +++ b/src/processTemplate.ts @@ -848,13 +848,13 @@ const imageToContext = (ctx: Context, img: Image) => { function validateImage(img: Image) { if ( !( - img.data instanceof Buffer || + img.data instanceof Uint8Array || img.data instanceof ArrayBuffer || typeof img.data === 'string' ) ) { throw new Error( - 'image .data property needs to be provided as Buffer, ArrayBuffer, or as a base64-encoded string' + 'image .data property needs to be provided as Uint8Array (e.g. Buffer, ArrayBuffer), or as a base64-encoded string' ); } if (!ImageExtensions.includes(img.extension)) { diff --git a/src/types.ts b/src/types.ts index 455b8efe..bad3e503 100755 --- a/src/types.ts +++ b/src/types.ts @@ -44,9 +44,9 @@ type RunJSFunc = (o: { sandbox: SandBox; ctx: Context }) => { export type UserOptions = { /** - * Docx file template as a NodeJS Buffer or Buffer-like object in Browsers. + * Docx file template as a Uint8Array (or e.g. ArrayBuffer or NodeJS Buffer). */ - template: Buffer; + template: Uint8Array; /** * Object of data to be injected or a (async) function that resolves to the data. The function gets as an argument the contents of the QUERY command as a string. */ @@ -181,7 +181,7 @@ export const ImageExtensions = [ type ImageExtension = (typeof ImageExtensions)[number]; export type Image = { extension: ImageExtension; - data: Buffer | ArrayBuffer | string; + data: ArrayBuffer | string; }; export type Links = { [id: string]: Link }; type Link = { url: string };