Skip to content

Commit

Permalink
Issue #345: Make types for template more liberal: what works with Buf…
Browse files Browse the repository at this point in the history
…fer should work with ArrayBuffer, with the benefit that browsers don't need a polyfill.
  • Loading branch information
jjhbw committed Jan 12, 2024
1 parent 6de5ef1 commit 4c1c007
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/__tests__/images.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: () => ({
Expand Down
6 changes: 3 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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<CommandSummary[]> {
const opts: CreateReportOptions = {
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/processTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
6 changes: 3 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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 };
Expand Down

0 comments on commit 4c1c007

Please sign in to comment.