Skip to content

Commit

Permalink
Fix demo and improtve warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
hyzyla committed Aug 31, 2024
1 parent aa80da1 commit ed97b89
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 10 deletions.
2 changes: 1 addition & 1 deletion docs/src/components/demo.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type PDFiumDocument, PDFiumLibrary } from "@hyzyla/pdfium/browser/cdn";
import { type PDFiumDocument, PDFiumLibrary } from "@hyzyla/pdfium/browser/base64";
import { QueryClient, QueryClientProvider, useQuery } from "@tanstack/react-query";
import React, { useCallback, useEffect, useRef, useState } from "react";

Expand Down
11 changes: 10 additions & 1 deletion src/index.esm.base64.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,17 @@ function base64ToUint8Array(base64: string) {
return bytes;
}

const BASE64_WARNING = "@hyzyla/pdfium: Using base64-encoded WASM binary.\n" +
"\n" +
`This can lead to larger bundle size or slower load time. Use "@hyzyla/pdfium" with a proper ` +
"setup for better performance. You can also disable this warning by passing { disableBase64Warning: true } " +
"to PDFiumLibrary.init() method";

export class PDFiumLibrary extends _PDFiumLibrary {
static async init() {
static async init(options?: { disableBase64Warning?: boolean }) {
if (!options?.disableBase64Warning) {
console.warn(BASE64_WARNING);
}
const base64 = await import("./vendor/pdfium.wasm.base64");
const wasmBinary = base64ToUint8Array(base64.PDFIUM_WASM_BASE64);
return await _PDFiumLibrary.initBase({
Expand Down
10 changes: 7 additions & 3 deletions src/index.esm.cdn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@ declare const __WASM_SHA265_B64__: string;

const CDN_WASM_LINK = `https://cdn.jsdelivr.net/npm/@hyzyla/pdfium@${__PACKAGE_VERSION__}/dist/pdfium.wasm`;
const WASM_INTEGRITY = `sha256-${__WASM_SHA265_B64__}`;
const CDN_WARNING = `
@hyzyla/pdfium: The PDFium WASM binary is loaded from a CDN. This may be slower than loading the binary from the local server. You can disable this warning by setting the 'disableCDNWarning' option to 'true' when calling PDFiumLibrary.init().
`;

const CDN_WARNING = "@hyzyla/pdfium: Fetching wasm binary from a CDN.\n" +
"\n" +
`This can lead to slower load time or potential network issues. Use "@hyzyla/pdfium" with a local wasm binary ` +
"for better performance and reliability. You can also disable this warning by passing { disableCDNWarning: true } " +
"to PDFiumLibrary.init() method";

export class PDFiumLibrary extends _PDFiumLibrary {

static _cache: ArrayBuffer | null = null;

static async init(options?: { disableCDNWarning?: boolean }) {
Expand Down
9 changes: 4 additions & 5 deletions src/library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ import { FPDFErrorCode } from "./constants";
import { PDFiumDocument } from "./document";
import { lengthBytesUTF8, stringToUTF8 } from "./emscripten";

const NO_OPTION_WARNING = `
@hyzyla/pdfium: wasmUrl, wasmBinary is required for browser environment.
const NO_OPTION_WARNING = "@hyzyla/pdfium: wasmUrl, wasmBinary is required for browser environment. \n\n"
+ "Please provide the wasm binary or URL to the init method. You can also use '@hyzyla/pdfium/browser/cdn'"
+ "or '@hyzyla/pdfium/browser/base64' for quick setup, but it's not recommended for production use.";

You can also use "@hyzyla/pdfium/browser/base64" or "@hyzyla/pdfium/browser/cdn" for quick setup, but keep in mind that it can lead to larger bundle size or slower load time.
`;

/**
* Converts a JavaScript string to a null-terminated C string and returns
Expand Down Expand Up @@ -50,7 +49,7 @@ export class PDFiumLibrary {
// Node.js will use wasm binary from node_modules, but for browser environment,
// user must provide the wasm binary or URL
if (typeof window !== "undefined") {
console.warn(NO_OPTION_WARNING);
console.error(NO_OPTION_WARNING);
throw new Error(NO_OPTION_WARNING);
}
}
Expand Down

0 comments on commit ed97b89

Please sign in to comment.