|
| 1 | +import { BigBuffer as e, Scalar as t } from "ffjavascript"; |
| 2 | +//#region ../fastfile/build/browser/browser.esm.js |
| 3 | +function n(e) { |
| 4 | + let t = e.initialSize || 1 << 20, n = new c(); |
| 5 | + return n.o = e, n.o.data = new Uint8Array(t), n.allocSize = t, n.totalSize = 0, n.readOnly = !1, n.pos = 0, n; |
| 6 | +} |
| 7 | +function r(e) { |
| 8 | + let t = new c(); |
| 9 | + return t.o = e, t.allocSize = e.data.byteLength, t.totalSize = e.data.byteLength, t.readOnly = !0, t.pos = 0, t; |
| 10 | +} |
| 11 | +var i = new Uint8Array(4), a = new DataView(i.buffer), o = new Uint8Array(8), s = new DataView(o.buffer), c = class { |
| 12 | + constructor() { |
| 13 | + this.pageSize = 16384; |
| 14 | + } |
| 15 | + _resizeIfNeeded(e) { |
| 16 | + if (e > this.allocSize) { |
| 17 | + let t = Math.max(this.allocSize + (1 << 20), Math.floor(this.allocSize * 1.1), e), n = new Uint8Array(t); |
| 18 | + n.set(this.o.data), this.o.data = n, this.allocSize = t; |
| 19 | + } |
| 20 | + } |
| 21 | + async write(e, t) { |
| 22 | + if (t === void 0 && (t = this.pos), this.readOnly) throw Error("Writing a read only file"); |
| 23 | + this._resizeIfNeeded(t + e.byteLength), this.o.data.set(e.slice(), t), t + e.byteLength > this.totalSize && (this.totalSize = t + e.byteLength), this.pos = t + e.byteLength; |
| 24 | + } |
| 25 | + async readToBuffer(e, t, n, r) { |
| 26 | + if (r === void 0 && (r = this.pos), this.readOnly && r + n > this.totalSize) throw Error("Reading out of bounds"); |
| 27 | + this._resizeIfNeeded(r + n); |
| 28 | + let i = new Uint8Array(this.o.data.buffer, this.o.data.byteOffset + r, n); |
| 29 | + e.set(i, t), this.pos = r + n; |
| 30 | + } |
| 31 | + async read(e, t) { |
| 32 | + let n = this, r = new Uint8Array(e); |
| 33 | + return await n.readToBuffer(r, 0, e, t), r; |
| 34 | + } |
| 35 | + close() { |
| 36 | + this.o.data.byteLength != this.totalSize && (this.o.data = this.o.data.slice(0, this.totalSize)); |
| 37 | + } |
| 38 | + async discard() {} |
| 39 | + async writeULE32(e, t) { |
| 40 | + let n = this; |
| 41 | + a.setUint32(0, e, !0), await n.write(i, t); |
| 42 | + } |
| 43 | + async writeUBE32(e, t) { |
| 44 | + let n = this; |
| 45 | + a.setUint32(0, e, !1), await n.write(i, t); |
| 46 | + } |
| 47 | + async writeULE64(e, t) { |
| 48 | + let n = this; |
| 49 | + s.setUint32(0, e & 4294967295, !0), s.setUint32(4, Math.floor(e / 4294967296), !0), await n.write(o, t); |
| 50 | + } |
| 51 | + async readULE32(e) { |
| 52 | + let t = await this.read(4, e); |
| 53 | + return new Uint32Array(t.buffer)[0]; |
| 54 | + } |
| 55 | + async readUBE32(e) { |
| 56 | + let t = await this.read(4, e); |
| 57 | + return new DataView(t.buffer).getUint32(0, !1); |
| 58 | + } |
| 59 | + async readULE64(e) { |
| 60 | + let t = await this.read(8, e), n = new Uint32Array(t.buffer); |
| 61 | + return n[1] * 4294967296 + n[0]; |
| 62 | + } |
| 63 | + async readString(e) { |
| 64 | + let t = this, n = e === void 0 ? t.pos : e; |
| 65 | + if (n > this.totalSize) { |
| 66 | + if (this.readOnly) throw Error("Reading out of bounds"); |
| 67 | + this._resizeIfNeeded(e); |
| 68 | + } |
| 69 | + let r = new Uint8Array(t.o.data.buffer, n, this.totalSize - n), i = r.findIndex((e) => e === 0), a = i !== -1, o = ""; |
| 70 | + return a ? (o = new TextDecoder().decode(r.slice(0, i)), t.pos = n + i + 1) : t.pos = n, o; |
| 71 | + } |
| 72 | +}, l = 1 << 22; |
| 73 | +function u(e) { |
| 74 | + let t = e.initialSize || 0, n = new g(); |
| 75 | + n.o = e; |
| 76 | + let r = t ? Math.floor((t - 1) / l) + 1 : 0; |
| 77 | + n.o.data = []; |
| 78 | + for (let e = 0; e < r - 1; e++) n.o.data.push(new Uint8Array(l)); |
| 79 | + return r && n.o.data.push(new Uint8Array(t - l * (r - 1))), n.totalSize = 0, n.readOnly = !1, n.pos = 0, n; |
| 80 | +} |
| 81 | +function d(e) { |
| 82 | + let t = new g(); |
| 83 | + return t.o = e, t.totalSize = (e.data.length - 1) * l + e.data[e.data.length - 1].byteLength, t.readOnly = !0, t.pos = 0, t; |
| 84 | +} |
| 85 | +var f = new Uint8Array(4), p = new DataView(f.buffer), m = new Uint8Array(8), h = new DataView(m.buffer), g = class { |
| 86 | + constructor() { |
| 87 | + this.pageSize = 16384; |
| 88 | + } |
| 89 | + _resizeIfNeeded(e) { |
| 90 | + if (e <= this.totalSize) return; |
| 91 | + if (this.readOnly) throw Error("Reading out of file bounds"); |
| 92 | + let t = Math.floor((e - 1) / l) + 1; |
| 93 | + for (let n = Math.max(this.o.data.length - 1, 0); n < t; n++) { |
| 94 | + let r = n < t - 1 ? l : e - (t - 1) * l, i = new Uint8Array(r); |
| 95 | + n == this.o.data.length - 1 && i.set(this.o.data[n]), this.o.data[n] = i; |
| 96 | + } |
| 97 | + this.totalSize = e; |
| 98 | + } |
| 99 | + async write(e, t) { |
| 100 | + let n = this; |
| 101 | + if (t === void 0 && (t = n.pos), this.readOnly) throw Error("Writing a read only file"); |
| 102 | + this._resizeIfNeeded(t + e.byteLength); |
| 103 | + let r = Math.floor(t / l), i = t % l, a = e.byteLength; |
| 104 | + for (; a > 0;) { |
| 105 | + let t = i + a > l ? l - i : a, o = e.slice(e.byteLength - a, e.byteLength - a + t); |
| 106 | + new Uint8Array(n.o.data[r].buffer, i, t).set(o), a -= t, r++, i = 0; |
| 107 | + } |
| 108 | + this.pos = t + e.byteLength; |
| 109 | + } |
| 110 | + async readToBuffer(e, t, n, r) { |
| 111 | + let i = this; |
| 112 | + if (r === void 0 && (r = i.pos), this.readOnly && r + n > this.totalSize) throw Error("Reading out of bounds"); |
| 113 | + this._resizeIfNeeded(r + n); |
| 114 | + let a = Math.floor(r / l), o = r % l, s = n; |
| 115 | + for (; s > 0;) { |
| 116 | + let r = o + s > l ? l - o : s, c = new Uint8Array(i.o.data[a].buffer, o, r); |
| 117 | + e.set(c, t + n - s), s -= r, a++, o = 0; |
| 118 | + } |
| 119 | + this.pos = r + n; |
| 120 | + } |
| 121 | + async read(e, t) { |
| 122 | + let n = this, r = new Uint8Array(e); |
| 123 | + return await n.readToBuffer(r, 0, e, t), r; |
| 124 | + } |
| 125 | + close() {} |
| 126 | + async discard() {} |
| 127 | + async writeULE32(e, t) { |
| 128 | + let n = this; |
| 129 | + p.setUint32(0, e, !0), await n.write(f, t); |
| 130 | + } |
| 131 | + async writeUBE32(e, t) { |
| 132 | + let n = this; |
| 133 | + p.setUint32(0, e, !1), await n.write(f, t); |
| 134 | + } |
| 135 | + async writeULE64(e, t) { |
| 136 | + let n = this; |
| 137 | + h.setUint32(0, e & 4294967295, !0), h.setUint32(4, Math.floor(e / 4294967296), !0), await n.write(m, t); |
| 138 | + } |
| 139 | + async readULE32(e) { |
| 140 | + let t = await this.read(4, e); |
| 141 | + return new Uint32Array(t.buffer)[0]; |
| 142 | + } |
| 143 | + async readUBE32(e) { |
| 144 | + let t = await this.read(4, e); |
| 145 | + return new DataView(t.buffer).getUint32(0, !1); |
| 146 | + } |
| 147 | + async readULE64(e) { |
| 148 | + let t = await this.read(8, e), n = new Uint32Array(t.buffer); |
| 149 | + return n[1] * 4294967296 + n[0]; |
| 150 | + } |
| 151 | + async readString(e) { |
| 152 | + let t = this, n = e === void 0 ? t.pos : e; |
| 153 | + if (n > this.totalSize) { |
| 154 | + if (this.readOnly) throw Error("Reading out of bounds"); |
| 155 | + this._resizeIfNeeded(e); |
| 156 | + } |
| 157 | + let r = !1, i = ""; |
| 158 | + for (; !r;) { |
| 159 | + let e = Math.floor(n / l), a = n % l; |
| 160 | + if (t.o.data[e] === void 0) throw Error("ERROR"); |
| 161 | + let o = Math.min(2048, t.o.data[e].length - a), s = new Uint8Array(t.o.data[e].buffer, a, o), c = s.findIndex((e) => e === 0); |
| 162 | + r = c !== -1, r ? (i += new TextDecoder().decode(s.slice(0, c)), t.pos = e * l + a + c + 1) : (i += new TextDecoder().decode(s), t.pos = e * l + a + s.length), n = t.pos; |
| 163 | + } |
| 164 | + return i; |
| 165 | + } |
| 166 | +}, _ = 65536; |
| 167 | +function v() { |
| 168 | + throw Error("File I/O is not supported in the browser"); |
| 169 | +} |
| 170 | +function y(e, t) { |
| 171 | + return e instanceof Uint8Array ? { |
| 172 | + type: "mem", |
| 173 | + data: e |
| 174 | + } : typeof e == "string" ? { |
| 175 | + type: "mem", |
| 176 | + initialSize: t || _ |
| 177 | + } : e; |
| 178 | +} |
| 179 | +async function b(e) { |
| 180 | + let t = await fetch(e).then((e) => e.arrayBuffer()); |
| 181 | + return { |
| 182 | + type: "mem", |
| 183 | + data: new Uint8Array(t) |
| 184 | + }; |
| 185 | +} |
| 186 | +function x(e, t, n) { |
| 187 | + if (e.type === "file" && v(), e.type === "mem") return t(e); |
| 188 | + if (e.type === "bigMem") return n(e); |
| 189 | + throw Error("Invalid FastFile type: " + e.type); |
| 190 | +} |
| 191 | +function S(e, t) { |
| 192 | + return x(y(e, t), n, u); |
| 193 | +} |
| 194 | +async function C(e) { |
| 195 | + return e = typeof e == "string" ? await b(e) : y(e), x(e, r, d); |
| 196 | +} |
| 197 | +//#endregion |
| 198 | +//#region src/binfileutils.js |
| 199 | +var w = typeof Buffer < "u" && Buffer.constants && Buffer.constants.MAX_LENGTH ? Buffer.constants.MAX_LENGTH : 1 << 30; |
| 200 | +async function T(e, t, n, r, i) { |
| 201 | + let a = await C(e, r, i), o = await a.read(4), s = ""; |
| 202 | + for (let e = 0; e < 4; e++) s += String.fromCharCode(o[e]); |
| 203 | + if (s != t) throw Error(e + ": Invalid File format"); |
| 204 | + if (await a.readULE32() > n) throw Error("Version not supported"); |
| 205 | + let c = await a.readULE32(), l = []; |
| 206 | + for (let e = 0; e < c; e++) { |
| 207 | + let e = await a.readULE32(), t = await a.readULE64(); |
| 208 | + l[e] === void 0 && (l[e] = []), l[e].push({ |
| 209 | + p: a.pos, |
| 210 | + size: t |
| 211 | + }), a.pos += t; |
| 212 | + } |
| 213 | + return { |
| 214 | + fd: a, |
| 215 | + sections: l |
| 216 | + }; |
| 217 | +} |
| 218 | +async function E(e, t, n, r, i, a) { |
| 219 | + let o = await S(e, i, a), s = new Uint8Array(4); |
| 220 | + for (let e = 0; e < 4; e++) s[e] = t.charCodeAt(e); |
| 221 | + return await o.write(s, 0), await o.writeULE32(n), await o.writeULE32(r), o; |
| 222 | +} |
| 223 | +async function D(e, t) { |
| 224 | + if (e.writingSection !== void 0) throw Error("Already writing a section"); |
| 225 | + await e.writeULE32(t), e.writingSection = { pSectionSize: e.pos }, await e.writeULE64(0); |
| 226 | +} |
| 227 | +async function O(e) { |
| 228 | + if (e.writingSection === void 0) throw Error("Not writing a section"); |
| 229 | + let t = e.pos - e.writingSection.pSectionSize - 8, n = e.pos; |
| 230 | + e.pos = e.writingSection.pSectionSize, await e.writeULE64(t), e.pos = n, delete e.writingSection; |
| 231 | +} |
| 232 | +async function k(e, t, n) { |
| 233 | + if (e.readingSection !== void 0) throw Error("Already reading a section"); |
| 234 | + if (!t[n]) throw Error(e.fileName + ": Missing section " + n); |
| 235 | + if (t[n].length > 1) throw Error(e.fileName + ": Section Duplicated " + n); |
| 236 | + e.pos = t[n][0].p, e.readingSection = t[n][0]; |
| 237 | +} |
| 238 | +async function A(e, t) { |
| 239 | + if (e.readingSection === void 0) throw Error("Not reading a section"); |
| 240 | + if (!t && e.pos - e.readingSection.p != e.readingSection.size) throw Error("Invalid section size reading"); |
| 241 | + delete e.readingSection; |
| 242 | +} |
| 243 | +async function j(e, n, r, i) { |
| 244 | + let a = new Uint8Array(r); |
| 245 | + t.toRprLE(a, 0, n, r), await e.write(a, i); |
| 246 | +} |
| 247 | +async function M(e, n, r) { |
| 248 | + let i = await e.read(n, r); |
| 249 | + return t.fromRprLE(i, 0, n); |
| 250 | +} |
| 251 | +async function N(e, t, n, r, i) { |
| 252 | + i === void 0 && (i = t[r][0].size); |
| 253 | + let a = e.pageSize; |
| 254 | + await k(e, t, r), await D(n, r); |
| 255 | + for (let t = 0; t < i; t += a) { |
| 256 | + let r = Math.min(i - t, a), o = await e.read(r); |
| 257 | + await n.write(o); |
| 258 | + } |
| 259 | + await O(n), await A(e, i != t[r][0].size); |
| 260 | +} |
| 261 | +async function P(t, n, r, i, a) { |
| 262 | + if (i = i === void 0 ? 0 : i, a = a === void 0 ? n[r][0].size - i : a, console.time("readSection idSection=" + r + " offset=" + i + " length=" + a), i + a > n[r][0].size) throw Error("Reading out of the range of the section"); |
| 263 | + let o; |
| 264 | + return o = a < w ? new Uint8Array(a) : new e(a), await t.readToBuffer(o, 0, a, n[r][0].p + i), console.timeEnd("readSection idSection=" + r + " offset=" + i + " length=" + a), o; |
| 265 | +} |
| 266 | +async function F(e, t, n, r, i) { |
| 267 | + let a = e.pageSize * 16; |
| 268 | + if (await k(e, t, i), await k(n, r, i), t[i][0].size != r[i][0].size) return !1; |
| 269 | + let o = t[i][0].size; |
| 270 | + for (let t = 0; t < o; t += a) { |
| 271 | + let r = Math.min(o - t, a), i = await e.read(r), s = await n.read(r); |
| 272 | + for (let e = 0; e < r; e++) if (i[e] != s[e]) return !1; |
| 273 | + } |
| 274 | + return await A(e), await A(n), !0; |
| 275 | +} |
| 276 | +//#endregion |
| 277 | +export { N as copySection, E as createBinFile, A as endReadSection, O as endWriteSection, M as readBigInt, T as readBinFile, P as readSection, F as sectionIsEqual, k as startReadUniqueSection, D as startWriteSection, j as writeBigInt }; |
0 commit comments