UNPKG

naive-upload

Version:

<p align="left"> <a href="https://www.npmjs.org/package/naive-upload"> <img src="https://img.shields.io/npm/v/naive-upload.svg"> </a> <a href="https://bundlephobia.com/package/naive-upload@latest"> <img src="https://img.shields.io/bundl

226 lines (225 loc) 9.31 kB
var B = Object.defineProperty; var D = (k, e, s) => e in k ? B(k, e, { enumerable: !0, configurable: !0, writable: !0, value: s }) : k[e] = s; var a = (k, e, s) => (D(k, typeof e != "symbol" ? e + "" : e, s), s); import y from "spark-md5"; import { HashWorkerScript as E } from "./HashWorkerScript.js"; import g from "./UploadError.js"; import U from "./FileReadHelper.js"; import { HashWorkerMessageType as p } from "../Model/HashWorkerMessageType.js"; import m from "../Model/RawFile.js"; class F { constructor(e, s) { a(this, "enableWorker"); a(this, "workerSupported"); a(this, "debug"); a(this, "sparkUnits", []); a(this, "workerUnits", []); a(this, "chunkHandlerQueue", []); a(this, "chunkHandler", []); a(this, "finished", !1); a(this, "canceled", !1); a(this, "paused", !1); a(this, "waitToContinue", []); a(this, "fileReaders", []); this.debug = s, this.enableWorker = e, this.workerSupported = typeof Worker < "u", this.enableWorker && this.debug && console.debug(`HashHelper > WebWoeker ${this.workerSupported ? "\u5DF2\u542F\u7528" : "\u672A\u542F\u7528\uFF08\u5F53\u524D\u6D4F\u89C8\u5668\u4E0D\u652F\u6301\uFF09"}`); } async checkFile(e, s, i) { return new Promise(async (o, t) => { const u = e; e instanceof m && (u.blob = u.file); let d = { preLoaded: 0, loaded: 0, total: e.size }; i(d); const f = 1024 * 1024; for (; u.checkPosition < u.size; ) { if (this.canceled) { await this.reset(), o(); break; } if (this.paused) { const w = async () => new Promise((n, l) => { this.waitToContinue[s] = n; }); this.debug && console.debug("HashHelper > \u5DF2\u6682\u505C", s); const r = await w(); if (this.waitToContinue[s] = null, this.debug && console.debug("HashHelper > \u5DF2\u6062\u590D", s), !r) { o(); return; } } const c = u.blob.slice(u.checkPosition, Math.min(u.checkPosition + f, u.size)); u.checkPosition += c.size, d.preLoaded += c.size, i(d), await this.appendData(c, s), d.loaded += c.size, i(d); } o(await this.getResult(s)); }); } async appendData(e, s) { let i = await this.fileReaders[s].readAsArrayBuffer(e); const o = { type: p.\u4E0B\u884C_\u9644\u52A0\u6570\u636E, data: i }; this.workerSupported ? await this.workerPostMessage(s, o) : this.sparkUnits[s].append(i); } async getResult(e) { const s = { type: p.\u4E0B\u884C_\u83B7\u53D6\u7ED3\u679C }; return this.workerSupported ? await this.workerPostMessage(e, s) : this.sparkUnits[e].end(); } async reset() { if (this.enableWorker && this.workerSupported) for (let e = 0; e < this.workerUnits.length; e++) await this.resetByIndex(e); else for (let e = 0; e < this.sparkUnits.length; e++) await this.resetByIndex(e); } async resetByIndex(e) { if (this.enableWorker && this.workerSupported) { if (!this.workerUnits[e].used) return; await this.workerPostMessage(e, { type: p.\u4E0B\u884C_\u91CD\u7F6E }), this.debug && console.debug("HashHelper > WebWoeker\u5B50\u7EBF\u7A0B\u8BA1\u7B97\u5355\u5143 \u5DF2\u91CD\u7F6E", Object.assign({}, this.workerUnits[e])); } else this.sparkUnits[e].reset(), this.debug && console.debug("HashHelper > Spark\u8BA1\u7B97\u5355\u5143 \u5DF2\u91CD\u7F6E", Object.assign({}, this.sparkUnits[e])); } async closeByIndex(e) { this.fileReaders[e].close(), this.debug && console.debug("HashHelper > FileReadHelper \u5DF2\u5173\u95ED", Object.assign({}, this.fileReaders[e])), this.enableWorker && this.workerSupported ? (await this.workerPostMessage(e, { type: p.\u4E0B\u884C_\u5173\u95ED }), this.debug && console.debug("HashHelper > WebWoeker\u5B50\u7EBF\u7A0B\u8BA1\u7B97\u5355\u5143 \u5DF2\u5173\u95ED", Object.assign({}, this.workerUnits[e]))) : (this.sparkUnits[e].destroy(), this.debug && console.debug("HashHelper > Spark\u8BA1\u7B97\u5355\u5143 \u5DF2\u5173\u95ED", Object.assign({}, this.sparkUnits[e]))); } async workerPostMessage(e, s) { let i = this.workerUnits[e]; return i.used = !0, new Promise((o, t) => { i.worker.onmessage = (u) => { switch (u.data.type) { case p.\u4E0A\u884C_\u7ED3\u679C: o(u.data.data); break; case p.\u4E0A\u884C_\u5E94\u7B54: default: o(); } }, i.worker.onerror = (u) => { i.used = !1, t(new g("WebWorker\u5F02\u5E38", u.error)); }, i.worker.onmessageerror = (u) => { i.used = !1, t(new g(`WebWorker\u901A\u8BAF\u9519\u8BEF\uFF0C${u.data}`)); }, s.type === p.\u4E0B\u884C_\u9644\u52A0\u6570\u636E ? i.worker.postMessage(s, [s.data]) : i.worker.postMessage(JSON.parse(JSON.stringify(s))); }); } static async getInstance(e = 1, s = !0, i = !1) { return new Promise((o) => { let t = new F(s, i); for (; t.fileReaders.length < e; ) t.fileReaders.push(new U()), t.chunkHandler.push(!1), t.waitToContinue.push(null), t.debug && console.debug("HashHelper > FileReadHelper \u5DF2\u521B\u5EFA", Object.assign({}, t.fileReaders[t.fileReaders.length - 1])); if (t.enableWorker && t.workerSupported) { window.URL = window.URL || window.webkitURL; for (let u = 0; u < e; u++) t.workerUnits.push({ worker: new Worker(window.URL.createObjectURL(E)), used: !1 }), t.debug && console.debug("HashHelper > WebWoeker\u5B50\u7EBF\u7A0B\u8BA1\u7B97\u5355\u5143 \u5DF2\u521B\u5EFA", Object.assign({}, t.workerUnits[u].worker)); } else for (; t.sparkUnits.length < e; ) t.sparkUnits.push(new y.ArrayBuffer()), t.debug && console.debug("HashHelper > Spark\u8BA1\u7B97\u5355\u5143 \u5DF2\u521B\u5EFA", Object.assign({}, t.sparkUnits[t.sparkUnits.length - 1])); o(t); }); } async handler(e, s) { return new Promise(async (i, o) => { this.debug && console.debug("HashHelper \u5F00\u59CB\u5904\u7406", Object.assign({}, e)); let t = { preLoaded: 0, loaded: 0, total: e.size * 2 }; const u = (r, n) => { t.preLoaded += r.preLoaded - n.preLoaded, t.loaded += r.loaded - n.loaded, n.preLoaded = r.preLoaded, n.loaded = r.loaded, s(t); }, d = async () => { var n; let r = { preLoaded: 0, loaded: 0, total: e.size }; try { e.md5 = (n = await this.checkFile( e, 0, (l) => { u(l, r); } )) != null ? n : null; } catch (l) { o(l); } }, f = () => { this.finished = !0, this.debug && console.debug("HashHelper \u5904\u7406\u7ED3\u675F", Object.assign({}, e)), i(); }; if (await d(), !e.needSection) { f(); return; } const c = async () => { var b; if (this.finished) return; if (this.chunkHandlerQueue.length === 0) { for (let h = 0; h < this.chunkHandler.length; h++) if (this.chunkHandler[h]) return; f(); return; } let r = null; for (let h = 0; h < this.chunkHandler.length; h++) if (!this.chunkHandler[h]) { r = h; break; } if (r == null) { this.debug && console.debug(`HashHelper \u5DF2\u8FBE\u5230\u6700\u5927\u5E76\u53D1\u6570\uFF1A${this.chunkHandler.length}\uFF0C\u5F53\u524D\u961F\u5217\u957F\u5EA6\uFF1A${this.chunkHandlerQueue.length}`, Object.assign({}, e)); return; } this.chunkHandler[r] = !0; const n = () => { if (s(t), this.canceled) { this.reset().then(i).catch(i); return; } this.chunkHandler[r] = !1, c(); }, l = this.chunkHandlerQueue.shift(); if (l.checked) { t.preLoaded += l.size, t.loaded += l.size, s(t), n(); return; } let H = { preLoaded: 0, loaded: 0, total: l.size }; s(t), l.md5 = (b = await this.checkFile( l, r, (h) => { u(h, H); } )) != null ? b : null, n(); }, w = (r) => { this.chunkHandlerQueue.push(r), c(); }; for (const r of e.chunks) w(r); }); } async cancel() { if (this.canceled = !0, this.paused) { this.paused = !1; for (const e of this.waitToContinue) e && e(!1); } this.debug && console.debug("HashHelper \u5DF2\u53D6\u6D88"); } async pause() { this.paused = !0, this.debug && console.debug("HashHelper \u5DF2\u6682\u505C"); } async continue() { this.paused = !1; for (const e of this.waitToContinue) e && e(!0); this.debug && console.debug("HashHelper \u5DF2\u6062\u590D"); } async close() { if (this.enableWorker && this.workerSupported) for (let e = 0; e < this.workerUnits.length; e++) await this.closeByIndex(e); else for (let e = 0; e < this.sparkUnits.length; e++) await this.closeByIndex(e); this.debug && console.debug("HashHelper \u5DF2\u5173\u95ED"); } } export { F as default }; //# sourceMappingURL=HashHelper.js.map