@pdf-viewer/react
Version:
The PDF Viewer component for React and Next.js
109 lines (108 loc) • 3.94 kB
JavaScript
import { useState as g, useCallback as h, useEffect as A } from "react";
import { appConsole as c } from "../appConsole.js";
const b = /* @__PURE__ */ new Date("2025-06-17T09:54:30.209Z"), l = "Please visit https://www.react-pdf.dev/manage-license/ to generate a new license key.", s = {
invalidLicense: `You are currently using without a valid license. ${l}`,
mismatchedDomain: `Your license key is not valid for the current domain / IP. ${l}`,
expired: `Your license key has expired. ${l}`,
exceededVersion: "Your license does not support the current version of React PDF. The library version you are using exceeds the supported range of your license.",
invalidSignature: "Invalid license key: Signature mismatch"
};
function L(r) {
const t = r.replace(/-----BEGIN PUBLIC KEY-----/, "").replace(/-----END PUBLIC KEY-----/, "").replace(/\s+/g, ""), e = atob(t), n = new Uint8Array(e.length);
for (let i = 0; i < e.length; i++)
n[i] = e.charCodeAt(i);
return n.buffer;
}
function S(r) {
const t = atob(r), e = new Uint8Array(t.length);
for (let n = 0; n < t.length; n++)
e[n] = t.charCodeAt(n);
return e.buffer;
}
async function I(r, t = "SHA-256") {
try {
const { data: e, signature: n } = JSON.parse(atob(r)), i = L(`-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvIokvErJ0Fctu0jduSAx
gr+5Har/VSuZLlOunS28hnlqqA+OF1apHL7RYkjBosS15yvviYdLrVWYHpHnoY4b
HLQ9I6YX6FMB9T/VbB4xLdVdKvfi8r49aTScl5EKuSpgpPgz2VSJTfvVGcRuth/Y
Bm7PPGYdL2l2gvKDxchmmsHFPukGki9L4JOUeIVX0GXAxabENckaEH/iC1NZhX+W
vXnpHeHuIIKlZ16LSivXNgo6BH2Z3GFdg4G3cAaXr6qNP8fsacYNqv2SpTW1viFD
vzcGRabN25gpJAp9/syeScefWH3OiJY1lmw88oTiJbjIoiq6AkPhfn3G9mF54nvR
kwIDAQAB
-----END PUBLIC KEY-----`), a = await window.crypto.subtle.importKey(
"spki",
// str2ab(publicKeyContent), // Convert PEM string to ArrayBuffer
i,
{ name: "RSA-PSS", hash: t },
!0,
["verify"]
);
if (!await window.crypto.subtle.verify(
{ name: "RSA-PSS", saltLength: 32 },
a,
S(n),
// Convert signature to ArrayBuffer
new TextEncoder().encode(atob(e))
))
throw new Error(s.invalidSignature);
return JSON.parse(atob(e));
} catch {
throw new Error(s.invalidLicense);
}
}
const y = (r, t) => r === "specific" ? t === window.location.host : window.location.host.includes(t), d = {
isValid: !0,
invalidatedMessage: s.invalidLicense,
type: void 0
}, x = (r) => {
const [t, e] = g(d), n = h(async () => {
if (!r)
throw e(d), new Error(s.invalidLicense);
let i;
try {
i = await I(r);
} catch (m) {
throw c.warn(m.message), new Error(m.message);
}
c.debug(">>> validatedLicense", i);
const { avu: a, exp: o, dmt: w, dm: p, t: v } = i;
if (!o)
throw new Error("License is missing expiration timestamp");
if (o > Number.MAX_SAFE_INTEGER / 1e3)
throw new Error("Invalid expiration timestamp: value too large");
const f = new Date(o * 1e3), E = (/* @__PURE__ */ new Date()).getTime();
if (f.getTime() < E)
throw new Error(s.expired);
if (!a)
throw new Error("License is missing available until version timestamp");
if (a > Number.MAX_SAFE_INTEGER / 1e3)
throw new Error("Invalid available until version timestamp: value too large");
const u = new Date(a * 1e3);
if (c.debug("availableUntilTimestamp", u), u.getTime() < b.getTime())
throw new Error(s.exceededVersion);
if (!y(w, p))
throw new Error(s.mismatchedDomain);
return {
isValid: !0,
type: v,
invalidatedMessage: void 0
};
}, [r]);
return A(() => {
n().then(({ isValid: i, type: a, invalidatedMessage: o }) => {
e({
isValid: i,
type: a,
invalidatedMessage: o
});
}).catch((i) => {
e({
...d,
isValid: !1
}), c.warn(i.message);
});
}, [n]), t;
};
export {
x as useLicense
};