UNPKG

hashion

Version:
53 lines (52 loc) 1.86 kB
var z = Object.defineProperty; var U = (e, r, t) => r in e ? z(e, r, { enumerable: !0, configurable: !0, writable: !0, value: t }) : e[r] = t; var s = (e, r, t) => U(e, typeof r != "symbol" ? r + "" : r, t); class p { constructor(r) { s(this, "algorithm"); s(this, "name"); this.algorithm = (r == null ? void 0 : r.algorithm) || "SHA-256", this.name = "SHA"; } computeHash(r, t) { const { file: f, chunkSize: u } = r, h = f.size, a = new FileReader(), b = Date.now(), g = Math.ceil(h / u); let i = 0, d = !1, m = 0; const w = new ArrayBuffer(h), H = new Uint8Array(w), A = new AbortController(), l = A.signal; l.addEventListener("abort", () => { d || (a.abort(), t(new Error("Hash calculation cancelled"), { progress: 0 })); }); function y() { if (l.aborted) return; const n = i * u, c = Math.min(n + u, h), o = f.slice(n, c); a.readAsArrayBuffer(o); } return a.onload = async (n) => { var c; if (!l.aborted) try { const o = new Uint8Array((c = n.target) == null ? void 0 : c.result); if (H.set(o, m), m += o.length, i++, i < g) y(), t(null, { progress: i / g * 100 }); else { const S = await crypto.subtle.digest(this.algorithm, w), B = Array.from(new Uint8Array(S)).map((C) => C.toString(16).padStart(2, "0")).join(""); d = !0, t(null, { hash: B, time: Date.now() - b, progress: 100 }); } } catch (o) { t(o, { progress: 0 }); } }, a.onerror = (n) => { l.aborted || (console.warn("SHA-256: Hash calculation error"), t(n, { progress: 0 })); }, y(), { abort: () => A.abort() }; } } s(p, "pluginName", "hash-plugin"), s(p, "name", "SHA"); export { p as Sha };