UNPKG

@sheetxl/io

Version:

IO - Import/Export Libraries for SheetXL.

107 lines (106 loc) 3.98 kB
/** * @license @sheetxl/io - IO - Import/Export Libraries for SheetXL. - v0.7.27 * * (C) 2025-present SheetXL Inc. & Michael T. Ford * License: The license can be found at https://www.sheetxl.com/license. */ import { Workbook as e } from "@sheetxl/sdk"; const t = async (e2, t2) => { const r2 = await e2.toJSON(), n = t2?.whiteSpace ?? 0; let i; t2?.beforeWrite?.(e2, r2), i = 0 === n ? JSON.stringify(r2) : (function(e3, t3 = 2) { const r3 = []; function n2(e4) { const n3 = t3 * e4; return void 0 === r3[n3] && (r3[n3] = " ".repeat(n3)), r3[n3]; } return (function e4(t4, r4, i2, f2) { if (null == t4 || "object" != typeof t4) return JSON.stringify(t4); const o2 = n2(r4), a = n2(r4 + 1); if (Array.isArray(t4)) { const n3 = t4.length; if (0 === n3) return "[]"; if (0 !== i2) return JSON.stringify(t4); let f3 = "[\n"; for (let o3 = 0; o3 < n3; o3++) f3 += a + e4(t4[o3], r4 + 1, i2 + 1, true), o3 < n3 - 1 && (f3 += ",\n"); return f3 += "\n" + o2 + "]", f3; } const s = Object.keys(t4), c = s.length; if (0 === c) return "{}"; if (!(i2 < 2)) return JSON.stringify(t4); let u = "{\n"; f2 || (i2 = 0); for (let n3 = 0; n3 < c; n3++) { const f3 = s[n3]; u += a + JSON.stringify(f3) + ": " + e4(t4[f3], r4 + 1, i2, false), n3 < c - 1 && (u += ",\n"); } return u += "\n" + o2 + "}", u; })(e3, 0, 0, false); })(r2, n); let f = new TextEncoder().encode(i).buffer; const o = t2?.compress ?? true; return o && (f = await (async function(e3, t3 = "gzip") { let r3; "undefined" != typeof SharedArrayBuffer && e3 instanceof SharedArrayBuffer ? (r3 = new ArrayBuffer(e3.byteLength), new Uint8Array(r3).set(new Uint8Array(e3))) : r3 = e3; const n2 = await import("zlib"), { promisify: i2 } = await import("util"), f2 = Buffer.from(r3); let o2; if ("gzip" === t3) { const e4 = i2(n2.gzip); o2 = await e4(f2); } else if ("deflate" === t3) { const e4 = i2(n2.deflate); o2 = await e4(f2); } else { if ("deflate-raw" !== t3) throw new Error(`Unsupported compression format: ${t3}`); { const e4 = i2(n2.deflateRaw); o2 = await e4(f2); } } const a = new ArrayBuffer(o2.byteLength); return new Uint8Array(a).set(o2), a; })(f, "string" == typeof o ? o : void 0)), f; }, r = async (t2, r2) => { let n = t2; (function(e2) { if (e2.byteLength < 4) return false; const t3 = new Uint8Array(e2), r3 = { gzip: [31, 139], zip: [80, 75, 3, 4], bz2: [66, 90, 104], br: [206, 178, 207, 129] }, n2 = Object.keys(r3); for (let e3 = 0; e3 < n2.length; e3++) { const i2 = n2[e3], f2 = r3[i2]; let o2 = true; for (let e4 = 0; e4 < f2.length; e4++) if (t3[e4] !== f2[e4]) { o2 = false; break; } if (o2) return i2; } return false; })(n) && (n = await (async function(e2, t3 = "gzip") { let r3; "undefined" != typeof SharedArrayBuffer && e2 instanceof SharedArrayBuffer ? (r3 = new ArrayBuffer(e2.byteLength), new Uint8Array(r3).set(new Uint8Array(e2))) : r3 = e2; const n2 = await import("zlib"), { promisify: i2 } = await import("util"), f2 = Buffer.from(r3); let o2; if ("gzip" === t3) { const e3 = i2(n2.gunzip); o2 = await e3(f2); } else if ("deflate" === t3) { const e3 = i2(n2.inflate); o2 = await e3(f2); } else { if ("deflate-raw" !== t3) throw new Error(`Unsupported decompression format: ${t3}`); { const e3 = i2(n2.inflateRaw); o2 = await e3(f2); } } const a = new ArrayBuffer(o2.byteLength); return new Uint8Array(a).set(o2), a; })(n)); let i = new TextDecoder().decode(n); const f = JSON.parse(i), o = { name: r2?.name, ...r2?.createWorkbookOptions ?? {}, json: f }; return new e(o); }; export { r as readBufferSXL, t as writeBufferSXL };