hashion
Version:
51 lines (50 loc) • 1.61 kB
JavaScript
var b = Object.defineProperty;
var y = (n, e, r) => e in n ? b(n, e, { enumerable: !0, configurable: !0, writable: !0, value: r }) : n[e] = r;
var u = (n, e, r) => y(n, typeof e != "symbol" ? e + "" : e, r);
import k from "spark-md5";
const z = File.prototype.slice || File.prototype.mozSlice || File.prototype.webkitSlice;
class g {
constructor() {
u(this, "name");
this.name = "spark-md5";
}
async computeHash(e, r) {
const { file: o, chunkSize: s } = e;
let p = !1;
const d = new k.ArrayBuffer(), a = new FileReader(), f = Math.ceil(o.size / s), w = Date.now();
let i = 0;
const m = new AbortController(), l = m.signal;
l.addEventListener("abort", () => {
p || (a.abort(), r(new Error("Hash calculation cancelled"), { progress: 0 }));
}), a.onload = function(t) {
var c;
if (!l.aborted)
if (d.append((c = t.target) == null ? void 0 : c.result), i++, i < f)
h(), r(null, {
progress: i / f * 100
});
else {
p = !0, r(null, {
hash: d.end(),
time: Date.now() - w,
progress: 100
});
return;
}
}, a.onerror = function(t) {
l.aborted || (console.warn("spark-md5: Hash calculation error"), r(t, { progress: 0 }));
};
function h() {
if (l.aborted) return;
const t = i * s, c = t + s >= o.size ? o.size : t + s;
a.readAsArrayBuffer(z.call(o, t, c));
}
return h(), {
abort: () => m.abort()
};
}
}
u(g, "pluginName", "hash-plugin"), u(g, "name", "spark-md5");
export {
g as Spark
};