javascript-excel 1.0.3 → 2.0.0-alpha.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +69 -2
- package/dist/auto-width-2oBzjeNG.d.cts +11 -0
- package/dist/auto-width-CT-BihTW.d.ts +10 -0
- package/dist/csv-DyVH9y7S.cjs +647 -0
- package/dist/csv-pXfaputs.js +618 -0
- package/dist/csv.cjs +7 -0
- package/dist/csv.d.cts +89 -0
- package/dist/csv.d.ts +89 -0
- package/dist/csv.js +1 -0
- package/dist/dom.cjs +440 -0
- package/dist/dom.d.cts +78 -0
- package/dist/dom.d.ts +78 -0
- package/dist/dom.js +437 -0
- package/dist/errors-CvGlfw0E.js +28 -0
- package/dist/errors-DOPrrGS4.cjs +69 -0
- package/dist/index.cjs +627 -0
- package/dist/index.d.cts +255 -0
- package/dist/index.d.ts +255 -0
- package/dist/index.js +597 -0
- package/dist/infer-BR-J9BZa.js +88 -0
- package/dist/infer-D9EC9vJ-.cjs +105 -0
- package/dist/iso-date-3TY1OlGd.cjs +57 -0
- package/dist/iso-date-CXnn-9mT.js +46 -0
- package/dist/numfmt-BBgB7iQA.js +429 -0
- package/dist/numfmt-Dv6AjopS.cjs +512 -0
- package/dist/read-C6L7Nyy3.d.cts +78 -0
- package/dist/read-DDN5ZsPe.js +2909 -0
- package/dist/read-Ddkks7qC.d.ts +78 -0
- package/dist/read-Ym1voUct.cjs +2932 -0
- package/dist/read.cjs +6 -0
- package/dist/read.d.cts +15 -0
- package/dist/read.d.ts +15 -0
- package/dist/read.js +1 -0
- package/dist/sheet-names-qpXsvMpM.cjs +1005 -0
- package/dist/sheet-names-uT2EH5Ui.js +832 -0
- package/dist/signal-Dz0ZYe6d.d.cts +13 -0
- package/dist/signal-Dz0ZYe6d.d.ts +13 -0
- package/dist/warnings-C_12PT4t.d.cts +410 -0
- package/dist/warnings-DXJKe1lx.d.ts +410 -0
- package/dist/worker.cjs +240 -0
- package/dist/worker.d.cts +65 -0
- package/dist/worker.d.ts +65 -0
- package/dist/worker.js +238 -0
- package/dist/write-BOh9jfu1.cjs +1487 -0
- package/dist/write-C5JgeIar.d.cts +29 -0
- package/dist/write-CYpLGuzl.d.ts +29 -0
- package/dist/write-DubvcG-x.js +1470 -0
- package/package.json +101 -51
- package/.babelrc +0 -10
- package/.circleci/config.yml +0 -39
- package/lib/index.d.ts +0 -1
- package/lib/index.js +0 -22
- package/lib/javascript-excel.js +0 -17703
- package/src/index.tsx +0 -11
- package/test/unit/sample.spec.js +0 -5
- package/tsconfig.json +0 -54
- package/webpack.config.js +0 -19
- /package/{LICENSE.md → LICENSE} +0 -0
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
// src/internal/input.ts
|
|
2
|
+
/** Uint8Array (Node Buffer included), ArrayBuffer, any ArrayBufferView, or a Blob -> Uint8Array. */
|
|
3
|
+
async function toBytes(input, fn, also = "") {
|
|
4
|
+
if (input instanceof Uint8Array) return input;
|
|
5
|
+
if (input instanceof ArrayBuffer) return new Uint8Array(input);
|
|
6
|
+
if (typeof input === "object" && input !== null && typeof input.arrayBuffer === "function") return new Uint8Array(await input.arrayBuffer());
|
|
7
|
+
if (ArrayBuffer.isView(input)) {
|
|
8
|
+
const v = input;
|
|
9
|
+
return new Uint8Array(v.buffer, v.byteOffset, v.byteLength);
|
|
10
|
+
}
|
|
11
|
+
throw new TypeError(`${fn}: input must be ${also}a Uint8Array, ArrayBuffer or Blob`);
|
|
12
|
+
}
|
|
13
|
+
// src/internal/csv/infer.ts
|
|
14
|
+
const NUMBER = /^[+-]?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?$/;
|
|
15
|
+
const FRACTION = /^[+-]?\.\d+(?:[eE][+-]?\d+)?$/;
|
|
16
|
+
const ISO = /^(\d{4})-(\d{2})-(\d{2})(?:[T ](\d{2}):(\d{2})(?::(\d{2})(?:\.(\d{1,3}))?)?(Z|[+-]\d{2}:?\d{2})?)?$/;
|
|
17
|
+
function inferValue(text, zone) {
|
|
18
|
+
const len = text.length;
|
|
19
|
+
if (len === 0 || len > 64) return text;
|
|
20
|
+
const c = text.charCodeAt(0);
|
|
21
|
+
if (c === 84 || c === 116 || c === 70 || c === 102) {
|
|
22
|
+
if (text === "TRUE" || text === "true") return true;
|
|
23
|
+
if (text === "FALSE" || text === "false") return false;
|
|
24
|
+
return text;
|
|
25
|
+
}
|
|
26
|
+
if (!(c >= 48 && c <= 57 || c === 43 || c === 45 || c === 46)) return text;
|
|
27
|
+
if (NUMBER.test(text) || FRACTION.test(text)) {
|
|
28
|
+
const n = Number(text);
|
|
29
|
+
if (!Number.isFinite(n)) return text;
|
|
30
|
+
if (!/[.eE]/.test(text) && !Number.isSafeInteger(n)) return text;
|
|
31
|
+
return n;
|
|
32
|
+
}
|
|
33
|
+
if (len >= 10 && text.charCodeAt(4) === 45) {
|
|
34
|
+
const d = parseIsoDate(text, zone);
|
|
35
|
+
if (d) return d;
|
|
36
|
+
}
|
|
37
|
+
return text;
|
|
38
|
+
}
|
|
39
|
+
/** Parses an ISO 8601 date or date-time; without an offset, `zone` applies. */
|
|
40
|
+
function parseIsoDate(text, zone) {
|
|
41
|
+
const m = ISO.exec(text);
|
|
42
|
+
if (!m) return void 0;
|
|
43
|
+
const y = +m[1];
|
|
44
|
+
const mo = +m[2];
|
|
45
|
+
const d = +m[3];
|
|
46
|
+
const h = m[4] === void 0 ? 0 : +m[4];
|
|
47
|
+
const mi = m[5] === void 0 ? 0 : +m[5];
|
|
48
|
+
const s = m[6] === void 0 ? 0 : +m[6];
|
|
49
|
+
const ms = m[7] === void 0 ? 0 : +m[7].padEnd(3, "0");
|
|
50
|
+
if (mo < 1 || mo > 12 || d < 1 || d > daysInMonth(y, mo) || h > 23 || mi > 59 || s > 59) return void 0;
|
|
51
|
+
const offset = m[8];
|
|
52
|
+
let t;
|
|
53
|
+
if (offset === void 0) {
|
|
54
|
+
if (zone === "utc") t = Date.UTC(y, mo - 1, d, h, mi, s, ms);
|
|
55
|
+
else {
|
|
56
|
+
const local = new Date(y, mo - 1, d, h, mi, s, ms);
|
|
57
|
+
if (y < 100) local.setFullYear(y);
|
|
58
|
+
t = local.getTime();
|
|
59
|
+
}
|
|
60
|
+
if (zone === "utc" && y < 100) {
|
|
61
|
+
const u = new Date(t);
|
|
62
|
+
u.setUTCFullYear(y);
|
|
63
|
+
t = u.getTime();
|
|
64
|
+
}
|
|
65
|
+
} else {
|
|
66
|
+
t = Date.UTC(y, mo - 1, d, h, mi, s, ms);
|
|
67
|
+
if (y < 100) {
|
|
68
|
+
const u = new Date(t);
|
|
69
|
+
u.setUTCFullYear(y);
|
|
70
|
+
t = u.getTime();
|
|
71
|
+
}
|
|
72
|
+
if (offset !== "Z") {
|
|
73
|
+
const sign = offset[0] === "-" ? -1 : 1;
|
|
74
|
+
const digits = offset.slice(1).replace(":", "");
|
|
75
|
+
const oh = +digits.slice(0, 2);
|
|
76
|
+
const om = +digits.slice(2, 4);
|
|
77
|
+
if (oh > 23 || om > 59) return void 0;
|
|
78
|
+
t -= sign * (oh * 60 + om) * 6e4;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
const date = new Date(t);
|
|
82
|
+
return Number.isNaN(date.getTime()) ? void 0 : date;
|
|
83
|
+
}
|
|
84
|
+
function daysInMonth(y, m) {
|
|
85
|
+
if (m === 2) return y % 4 === 0 && y % 100 !== 0 || y % 400 === 0 ? 29 : 28;
|
|
86
|
+
return m === 4 || m === 6 || m === 9 || m === 11 ? 30 : 31;
|
|
87
|
+
}
|
|
88
|
+
Object.defineProperty(exports, "inferValue", {
|
|
89
|
+
enumerable: true,
|
|
90
|
+
get: function() {
|
|
91
|
+
return inferValue;
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
Object.defineProperty(exports, "parseIsoDate", {
|
|
95
|
+
enumerable: true,
|
|
96
|
+
get: function() {
|
|
97
|
+
return parseIsoDate;
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
Object.defineProperty(exports, "toBytes", {
|
|
101
|
+
enumerable: true,
|
|
102
|
+
get: function() {
|
|
103
|
+
return toBytes;
|
|
104
|
+
}
|
|
105
|
+
});
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
// src/internal/iso-date.ts
|
|
2
|
+
/** ISO 8601 date text and parsing, shared by the CSV writer, `cellText`, `fromTable` and `fromDataset`. */
|
|
3
|
+
/** YYYY-MM-DD, or YYYY-MM-DDTHH:mm:ss(.sss) when there is a time of day, in `zone`. */
|
|
4
|
+
function isoDate(d, zone) {
|
|
5
|
+
const utc = zone === "utc";
|
|
6
|
+
const y = utc ? d.getUTCFullYear() : d.getFullYear();
|
|
7
|
+
const mo = (utc ? d.getUTCMonth() : d.getMonth()) + 1;
|
|
8
|
+
const day = utc ? d.getUTCDate() : d.getDate();
|
|
9
|
+
const h = utc ? d.getUTCHours() : d.getHours();
|
|
10
|
+
const mi = utc ? d.getUTCMinutes() : d.getMinutes();
|
|
11
|
+
const s = utc ? d.getUTCSeconds() : d.getSeconds();
|
|
12
|
+
const ms = utc ? d.getUTCMilliseconds() : d.getMilliseconds();
|
|
13
|
+
const date = `${y >= 0 && y <= 9999 ? pad(y, 4) : (y < 0 ? "-" : "+") + pad(Math.abs(y), 6)}-${pad(mo, 2)}-${pad(day, 2)}`;
|
|
14
|
+
if (h === 0 && mi === 0 && s === 0 && ms === 0) return date;
|
|
15
|
+
const time = `${pad(h, 2)}:${pad(mi, 2)}:${pad(s, 2)}`;
|
|
16
|
+
return ms === 0 ? `${date}T${time}` : `${date}T${time}.${pad(ms, 3)}`;
|
|
17
|
+
}
|
|
18
|
+
function pad(n, w) {
|
|
19
|
+
return String(n).padStart(w, "0");
|
|
20
|
+
}
|
|
21
|
+
const ISO_DATE = /^(\d{4})-(\d{2})-(\d{2})(?:[T ](\d{2}):(\d{2})(?::(\d{2})(?:\.(\d{1,3})\d*)?)?(Z|[+-]\d{2}:?\d{2})?)?$/;
|
|
22
|
+
/** ISO 8601 date or date-time. Without a zone, the local wall-clock time (dateZone 'local'). */
|
|
23
|
+
function parseIsoDate(s) {
|
|
24
|
+
const m = ISO_DATE.exec(s.trim());
|
|
25
|
+
if (!m) return void 0;
|
|
26
|
+
const [y, mo, d, h = "0", mi = "0", sec = "0", ms = "0", zone] = m.slice(1);
|
|
27
|
+
const parts = [
|
|
28
|
+
Number(y),
|
|
29
|
+
Number(mo) - 1,
|
|
30
|
+
Number(d),
|
|
31
|
+
Number(h),
|
|
32
|
+
Number(mi),
|
|
33
|
+
Number(sec),
|
|
34
|
+
Number(ms.padEnd(3, "0"))
|
|
35
|
+
];
|
|
36
|
+
let date;
|
|
37
|
+
if (zone) {
|
|
38
|
+
const off = zone === "Z" ? 0 : (zone[0] === "-" ? -1 : 1) * (Number(zone.slice(1, 3)) * 60 + Number(zone.slice(-2)));
|
|
39
|
+
date = /* @__PURE__ */ new Date(Date.UTC(...parts) - off * 6e4);
|
|
40
|
+
} else date = new Date(...parts);
|
|
41
|
+
const wall = zone ? new Date(Date.UTC(...parts)) : date;
|
|
42
|
+
const [month, day] = zone ? [wall.getUTCMonth(), wall.getUTCDate()] : [wall.getMonth(), wall.getDate()];
|
|
43
|
+
if (month !== parts[1] || day !== parts[2]) return void 0;
|
|
44
|
+
return Number.isFinite(date.getTime()) ? date : void 0;
|
|
45
|
+
}
|
|
46
|
+
Object.defineProperty(exports, "isoDate", {
|
|
47
|
+
enumerable: true,
|
|
48
|
+
get: function() {
|
|
49
|
+
return isoDate;
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
Object.defineProperty(exports, "parseIsoDate", {
|
|
53
|
+
enumerable: true,
|
|
54
|
+
get: function() {
|
|
55
|
+
return parseIsoDate;
|
|
56
|
+
}
|
|
57
|
+
});
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// src/internal/iso-date.ts
|
|
2
|
+
/** ISO 8601 date text and parsing, shared by the CSV writer, `cellText`, `fromTable` and `fromDataset`. */
|
|
3
|
+
/** YYYY-MM-DD, or YYYY-MM-DDTHH:mm:ss(.sss) when there is a time of day, in `zone`. */
|
|
4
|
+
function isoDate(d, zone) {
|
|
5
|
+
const utc = zone === "utc";
|
|
6
|
+
const y = utc ? d.getUTCFullYear() : d.getFullYear();
|
|
7
|
+
const mo = (utc ? d.getUTCMonth() : d.getMonth()) + 1;
|
|
8
|
+
const day = utc ? d.getUTCDate() : d.getDate();
|
|
9
|
+
const h = utc ? d.getUTCHours() : d.getHours();
|
|
10
|
+
const mi = utc ? d.getUTCMinutes() : d.getMinutes();
|
|
11
|
+
const s = utc ? d.getUTCSeconds() : d.getSeconds();
|
|
12
|
+
const ms = utc ? d.getUTCMilliseconds() : d.getMilliseconds();
|
|
13
|
+
const date = `${y >= 0 && y <= 9999 ? pad(y, 4) : (y < 0 ? "-" : "+") + pad(Math.abs(y), 6)}-${pad(mo, 2)}-${pad(day, 2)}`;
|
|
14
|
+
if (h === 0 && mi === 0 && s === 0 && ms === 0) return date;
|
|
15
|
+
const time = `${pad(h, 2)}:${pad(mi, 2)}:${pad(s, 2)}`;
|
|
16
|
+
return ms === 0 ? `${date}T${time}` : `${date}T${time}.${pad(ms, 3)}`;
|
|
17
|
+
}
|
|
18
|
+
function pad(n, w) {
|
|
19
|
+
return String(n).padStart(w, "0");
|
|
20
|
+
}
|
|
21
|
+
const ISO_DATE = /^(\d{4})-(\d{2})-(\d{2})(?:[T ](\d{2}):(\d{2})(?::(\d{2})(?:\.(\d{1,3})\d*)?)?(Z|[+-]\d{2}:?\d{2})?)?$/;
|
|
22
|
+
/** ISO 8601 date or date-time. Without a zone, the local wall-clock time (dateZone 'local'). */
|
|
23
|
+
function parseIsoDate(s) {
|
|
24
|
+
const m = ISO_DATE.exec(s.trim());
|
|
25
|
+
if (!m) return void 0;
|
|
26
|
+
const [y, mo, d, h = "0", mi = "0", sec = "0", ms = "0", zone] = m.slice(1);
|
|
27
|
+
const parts = [
|
|
28
|
+
Number(y),
|
|
29
|
+
Number(mo) - 1,
|
|
30
|
+
Number(d),
|
|
31
|
+
Number(h),
|
|
32
|
+
Number(mi),
|
|
33
|
+
Number(sec),
|
|
34
|
+
Number(ms.padEnd(3, "0"))
|
|
35
|
+
];
|
|
36
|
+
let date;
|
|
37
|
+
if (zone) {
|
|
38
|
+
const off = zone === "Z" ? 0 : (zone[0] === "-" ? -1 : 1) * (Number(zone.slice(1, 3)) * 60 + Number(zone.slice(-2)));
|
|
39
|
+
date = /* @__PURE__ */ new Date(Date.UTC(...parts) - off * 6e4);
|
|
40
|
+
} else date = new Date(...parts);
|
|
41
|
+
const wall = zone ? new Date(Date.UTC(...parts)) : date;
|
|
42
|
+
const [month, day] = zone ? [wall.getUTCMonth(), wall.getUTCDate()] : [wall.getMonth(), wall.getDate()];
|
|
43
|
+
if (month !== parts[1] || day !== parts[2]) return void 0;
|
|
44
|
+
return Number.isFinite(date.getTime()) ? date : void 0;
|
|
45
|
+
}
|
|
46
|
+
export { parseIsoDate as n, isoDate as t };
|
|
@@ -0,0 +1,429 @@
|
|
|
1
|
+
import { a as LimitError, i as FormatError, n as EnvironmentError, o as ValidationError } from "./errors-CvGlfw0E.js";
|
|
2
|
+
// src/internal/zip/bytes.ts
|
|
3
|
+
/** Rejects with `signal.reason`, the same as fetch. */
|
|
4
|
+
function throwIfAborted(signal) {
|
|
5
|
+
if (signal?.aborted) throw signal.reason ?? /* @__PURE__ */ new Error("The operation was aborted.");
|
|
6
|
+
}
|
|
7
|
+
function concatBytes(parts) {
|
|
8
|
+
if (parts.length === 1) return parts[0];
|
|
9
|
+
let total = 0;
|
|
10
|
+
for (const p of parts) total += p.length;
|
|
11
|
+
const out = new Uint8Array(total);
|
|
12
|
+
let offset = 0;
|
|
13
|
+
for (const p of parts) {
|
|
14
|
+
out.set(p, offset);
|
|
15
|
+
offset += p.length;
|
|
16
|
+
}
|
|
17
|
+
return out;
|
|
18
|
+
}
|
|
19
|
+
/** Little-endian writer into a fixed-size buffer. */
|
|
20
|
+
var ByteWriter = class {
|
|
21
|
+
bytes;
|
|
22
|
+
view;
|
|
23
|
+
pos = 0;
|
|
24
|
+
constructor(size) {
|
|
25
|
+
this.bytes = new Uint8Array(size);
|
|
26
|
+
this.view = new DataView(this.bytes.buffer);
|
|
27
|
+
}
|
|
28
|
+
u16(v) {
|
|
29
|
+
this.view.setUint16(this.pos, v, true);
|
|
30
|
+
this.pos += 2;
|
|
31
|
+
}
|
|
32
|
+
u32(v) {
|
|
33
|
+
this.view.setUint32(this.pos, v >>> 0, true);
|
|
34
|
+
this.pos += 4;
|
|
35
|
+
}
|
|
36
|
+
raw(b) {
|
|
37
|
+
this.bytes.set(b, this.pos);
|
|
38
|
+
this.pos += b.length;
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
const clock = (() => {
|
|
42
|
+
const perf = globalThis.performance;
|
|
43
|
+
return perf && typeof perf.now === "function" ? () => perf.now() : () => Date.now();
|
|
44
|
+
})();
|
|
45
|
+
/** Wraps a progress callback: monotonic ratio, throttled. Returns undefined when there is no callback. */
|
|
46
|
+
function throttleProgress(cb, intervalMs = 50) {
|
|
47
|
+
if (!cb) return void 0;
|
|
48
|
+
let last = -1e400;
|
|
49
|
+
let lastPhase;
|
|
50
|
+
let lastSheet;
|
|
51
|
+
let ratio = 0;
|
|
52
|
+
return (p) => {
|
|
53
|
+
ratio = Math.min(1, Math.max(ratio, p.ratio));
|
|
54
|
+
const now = clock();
|
|
55
|
+
if (!(p.phase !== lastPhase || p.sheet !== lastSheet) && ratio < 1 && now - last < intervalMs) return;
|
|
56
|
+
last = now;
|
|
57
|
+
lastPhase = p.phase;
|
|
58
|
+
lastSheet = p.sheet;
|
|
59
|
+
const out = {
|
|
60
|
+
phase: p.phase,
|
|
61
|
+
ratio
|
|
62
|
+
};
|
|
63
|
+
if (p.sheet !== void 0) out.sheet = p.sheet;
|
|
64
|
+
cb(out);
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
// src/internal/platform.ts
|
|
68
|
+
/**
|
|
69
|
+
* Minimal structural types for the web platform APIs the engine uses.
|
|
70
|
+
*
|
|
71
|
+
* The package compiles against `lib: ES2022` with no DOM or Node types, so the
|
|
72
|
+
* published declarations never depend on a consumer's `lib` setting. These
|
|
73
|
+
* interfaces describe only the members the engine calls. They are internal and
|
|
74
|
+
* never appear in a public signature.
|
|
75
|
+
*/
|
|
76
|
+
const g = globalThis;
|
|
77
|
+
function missing(api) {
|
|
78
|
+
return new EnvironmentError("ENVIRONMENT_UNSUPPORTED", `${api} is not available in this runtime. javascript-excel needs Node.js 22 or later, or a browser with CompressionStream('deflate-raw') support.`, { details: { api } });
|
|
79
|
+
}
|
|
80
|
+
/** Creates a raw-DEFLATE compression or decompression stream, or throws ENVIRONMENT_UNSUPPORTED. */
|
|
81
|
+
function createStream(kind) {
|
|
82
|
+
const name = kind === "compress" ? "CompressionStream" : "DecompressionStream";
|
|
83
|
+
const Ctor = kind === "compress" ? g.CompressionStream : g.DecompressionStream;
|
|
84
|
+
if (typeof Ctor !== "function") throw missing(name);
|
|
85
|
+
try {
|
|
86
|
+
return new Ctor("deflate-raw");
|
|
87
|
+
} catch (cause) {
|
|
88
|
+
const err = missing(`${name}('deflate-raw')`);
|
|
89
|
+
err.cause = cause;
|
|
90
|
+
throw err;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
let encoder;
|
|
94
|
+
let strictDecoder;
|
|
95
|
+
function encodeUtf8(text) {
|
|
96
|
+
if (!encoder) {
|
|
97
|
+
if (typeof g.TextEncoder !== "function") throw missing("TextEncoder");
|
|
98
|
+
encoder = new g.TextEncoder();
|
|
99
|
+
}
|
|
100
|
+
return encoder.encode(text);
|
|
101
|
+
}
|
|
102
|
+
/** Decodes UTF-8 and throws a TypeError on invalid bytes. */
|
|
103
|
+
function decodeUtf8Strict(bytes) {
|
|
104
|
+
if (!strictDecoder) {
|
|
105
|
+
if (typeof g.TextDecoder !== "function") throw missing("TextDecoder");
|
|
106
|
+
strictDecoder = new g.TextDecoder("utf-8", { fatal: true });
|
|
107
|
+
}
|
|
108
|
+
return strictDecoder.decode(bytes);
|
|
109
|
+
}
|
|
110
|
+
const ignore = () => {};
|
|
111
|
+
async function deflateRaw(chunks, signal) {
|
|
112
|
+
throwIfAborted(signal);
|
|
113
|
+
const stream = createStream("compress");
|
|
114
|
+
const writer = stream.writable.getWriter();
|
|
115
|
+
const reader = stream.readable.getReader();
|
|
116
|
+
const out = [];
|
|
117
|
+
const reading = (async () => {
|
|
118
|
+
for (;;) {
|
|
119
|
+
const r = await reader.read();
|
|
120
|
+
if (r.done) return;
|
|
121
|
+
if (r.value) out.push(r.value);
|
|
122
|
+
}
|
|
123
|
+
})();
|
|
124
|
+
try {
|
|
125
|
+
for await (const chunk of chunks) {
|
|
126
|
+
throwIfAborted(signal);
|
|
127
|
+
if (chunk.length > 0) await writer.write(chunk);
|
|
128
|
+
}
|
|
129
|
+
throwIfAborted(signal);
|
|
130
|
+
await writer.close();
|
|
131
|
+
await reading;
|
|
132
|
+
} catch (err) {
|
|
133
|
+
writer.abort(err).catch(ignore);
|
|
134
|
+
reader.cancel(err).catch(ignore);
|
|
135
|
+
await reading.catch(ignore);
|
|
136
|
+
throw err;
|
|
137
|
+
}
|
|
138
|
+
return concatBytes(out);
|
|
139
|
+
}
|
|
140
|
+
async function* inflateRaw(data, limits, signal) {
|
|
141
|
+
throwIfAborted(signal);
|
|
142
|
+
const stream = createStream("decompress");
|
|
143
|
+
const writer = stream.writable.getWriter();
|
|
144
|
+
const reader = stream.readable.getReader();
|
|
145
|
+
const writing = writer.write(data).then(() => writer.close());
|
|
146
|
+
writing.catch(ignore);
|
|
147
|
+
let produced = 0;
|
|
148
|
+
let finished = false;
|
|
149
|
+
try {
|
|
150
|
+
for (;;) {
|
|
151
|
+
throwIfAborted(signal);
|
|
152
|
+
let r;
|
|
153
|
+
try {
|
|
154
|
+
r = await reader.read();
|
|
155
|
+
} catch (cause) {
|
|
156
|
+
throw new FormatError("ZIP_INVALID", "Invalid or truncated DEFLATE data.", { cause });
|
|
157
|
+
}
|
|
158
|
+
if (r.done) break;
|
|
159
|
+
const chunk = r.value;
|
|
160
|
+
if (!chunk || chunk.length === 0) continue;
|
|
161
|
+
produced += chunk.length;
|
|
162
|
+
if (produced > limits.maxOutputBytes) throw new LimitError("LIMIT_EXCEEDED", `Decompressed entry exceeds ${limits.maxOutputBytes} bytes.`, { details: {
|
|
163
|
+
limit: "maxEntryBytes",
|
|
164
|
+
value: produced,
|
|
165
|
+
max: limits.maxOutputBytes
|
|
166
|
+
} });
|
|
167
|
+
if (produced > 1048576 && produced / Math.max(1, data.length) > limits.maxRatio) throw new LimitError("LIMIT_EXCEEDED", `Compression ratio exceeds ${limits.maxRatio}.`, { details: {
|
|
168
|
+
limit: "maxRatio",
|
|
169
|
+
value: produced / Math.max(1, data.length),
|
|
170
|
+
max: limits.maxRatio
|
|
171
|
+
} });
|
|
172
|
+
yield chunk;
|
|
173
|
+
}
|
|
174
|
+
try {
|
|
175
|
+
await writing;
|
|
176
|
+
} catch (cause) {
|
|
177
|
+
throw new FormatError("ZIP_INVALID", "Invalid or truncated DEFLATE data.", { cause });
|
|
178
|
+
}
|
|
179
|
+
finished = true;
|
|
180
|
+
} finally {
|
|
181
|
+
if (!finished) {
|
|
182
|
+
reader.cancel().catch(ignore);
|
|
183
|
+
writer.abort().catch(ignore);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
/** The default codec: native CompressionStream / DecompressionStream('deflate-raw'). */
|
|
188
|
+
const nativeCodec = {
|
|
189
|
+
deflateRaw,
|
|
190
|
+
inflateRaw
|
|
191
|
+
};
|
|
192
|
+
// src/internal/zip/crc32.ts
|
|
193
|
+
/**
|
|
194
|
+
* CRC-32 as used by zip (ISO 3309 / ITU-T V.42): reflected polynomial
|
|
195
|
+
* 0xEDB88320, initial value and final XOR 0xFFFFFFFF.
|
|
196
|
+
*
|
|
197
|
+
* Slicing-by-8 tables: 8 KiB, built lazily on first use so importing the
|
|
198
|
+
* module costs nothing. Slicing-by-8 measured 1,150–1,270 MB/s against
|
|
199
|
+
* 850–940 MB/s for slicing-by-4 on Node 24 (JSE-8).
|
|
200
|
+
*/
|
|
201
|
+
let tables;
|
|
202
|
+
function buildTables() {
|
|
203
|
+
const t = /* @__PURE__ */ new Uint32Array(2048);
|
|
204
|
+
for (let n = 0; n < 256; n++) {
|
|
205
|
+
let c = n;
|
|
206
|
+
for (let k = 0; k < 8; k++) c = c & 1 ? 3988292384 ^ c >>> 1 : c >>> 1;
|
|
207
|
+
t[n] = c >>> 0;
|
|
208
|
+
}
|
|
209
|
+
for (let n = 0; n < 256; n++) {
|
|
210
|
+
let c = t[n];
|
|
211
|
+
for (let k = 1; k < 8; k++) {
|
|
212
|
+
c = t[c & 255] ^ c >>> 8;
|
|
213
|
+
t[k * 256 + n] = c >>> 0;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
return t;
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* Returns the CRC-32 of `data`. Pass the previous result as `crc` to continue
|
|
220
|
+
* over several chunks: `crc32(b, crc32(a))` equals `crc32(a + b)`.
|
|
221
|
+
*/
|
|
222
|
+
function crc32(data, crc = 0) {
|
|
223
|
+
const t = tables ??= buildTables();
|
|
224
|
+
let c = ~crc >>> 0;
|
|
225
|
+
const n = data.length;
|
|
226
|
+
let i = 0;
|
|
227
|
+
for (const end = n - n % 8; i < end; i += 8) {
|
|
228
|
+
c ^= data[i] | data[i + 1] << 8 | data[i + 2] << 16 | data[i + 3] << 24;
|
|
229
|
+
const hi = data[i + 4] | data[i + 5] << 8 | data[i + 6] << 16 | data[i + 7] << 24;
|
|
230
|
+
c = t[1792 + (c & 255)] ^ t[1536 + (c >>> 8 & 255)] ^ t[1280 + (c >>> 16 & 255)] ^ t[1024 + (c >>> 24)] ^ t[768 + (hi & 255)] ^ t[512 + (hi >>> 8 & 255)] ^ t[256 + (hi >>> 16 & 255)] ^ t[hi >>> 24];
|
|
231
|
+
}
|
|
232
|
+
for (; i < n; i++) c = t[(c ^ data[i]) & 255] ^ c >>> 8;
|
|
233
|
+
return ~c >>> 0;
|
|
234
|
+
}
|
|
235
|
+
// src/internal/zip/dos-time.ts
|
|
236
|
+
const MIN_MS = Date.UTC(1980, 0, 1);
|
|
237
|
+
const MAX_MS = Date.UTC(2107, 11, 31, 23, 59, 58);
|
|
238
|
+
function toDosDateTime(d) {
|
|
239
|
+
let ms = d.getTime();
|
|
240
|
+
if (!Number.isFinite(ms) || ms < MIN_MS) ms = MIN_MS;
|
|
241
|
+
else if (ms > MAX_MS) ms = MAX_MS;
|
|
242
|
+
const u = new Date(ms);
|
|
243
|
+
return {
|
|
244
|
+
date: u.getUTCFullYear() - 1980 << 9 | u.getUTCMonth() + 1 << 5 | u.getUTCDate(),
|
|
245
|
+
time: u.getUTCHours() << 11 | u.getUTCMinutes() << 5 | u.getUTCSeconds() >> 1
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
function fromDosDateTime(date, time) {
|
|
249
|
+
const year = 1980 + (date >>> 9);
|
|
250
|
+
const month = Math.max(1, date >>> 5 & 15) - 1;
|
|
251
|
+
const day = Math.max(1, date & 31);
|
|
252
|
+
return new Date(Date.UTC(year, month, day, time >>> 11, time >>> 5 & 63, (time & 31) * 2));
|
|
253
|
+
}
|
|
254
|
+
// src/dates.ts
|
|
255
|
+
/**
|
|
256
|
+
* Date ↔ serial conversion (docs/api.md §3.4.1, §4.5).
|
|
257
|
+
*
|
|
258
|
+
* 1900 system: serial 1 = 1900-01-01. Excel treats 1900 as a leap year, so
|
|
259
|
+
* serial 60 is the non-existent 1900-02-29: dates before 1900-03-01 use the
|
|
260
|
+
* epoch 1899-12-31, later dates 1899-12-30.
|
|
261
|
+
* 1904 system: serial 0 = 1904-01-01.
|
|
262
|
+
*
|
|
263
|
+
* `dateZone: 'local'` (default) reads and builds dates from their local
|
|
264
|
+
* calendar fields, so `new Date(2024, 0, 15)` is serial 45306 in every zone.
|
|
265
|
+
* `'utc'` uses the UTC fields.
|
|
266
|
+
*/
|
|
267
|
+
const DAY_MS = 864e5;
|
|
268
|
+
const EPOCH_1900 = Date.UTC(1899, 11, 30);
|
|
269
|
+
const EPOCH_1900_EARLY = Date.UTC(1899, 11, 31);
|
|
270
|
+
const LEAP_BUG_START = Date.UTC(1900, 2, 1);
|
|
271
|
+
const EPOCH_1904 = Date.UTC(1904, 0, 1);
|
|
272
|
+
/** Excel's day 0 ("1900-01-00", 1899-12-31): time-only values live there, serials 0 to <1. */
|
|
273
|
+
const MIN_1900 = Date.UTC(1899, 11, 31);
|
|
274
|
+
const MAX = Date.UTC(9999, 11, 31, 23, 59, 59, 999);
|
|
275
|
+
/** Wall-clock time of `d` in the chosen zone, as UTC milliseconds. */
|
|
276
|
+
function wallClock(d, zone) {
|
|
277
|
+
if (zone === "utc") return d.getTime();
|
|
278
|
+
const year = d.getFullYear();
|
|
279
|
+
if (year < 1e3) return NaN;
|
|
280
|
+
return Date.UTC(year, d.getMonth(), d.getDate(), d.getHours(), d.getMinutes(), d.getSeconds(), d.getMilliseconds());
|
|
281
|
+
}
|
|
282
|
+
/**
|
|
283
|
+
* Converts a Date to an Excel serial number.
|
|
284
|
+
* Throws `INVALID_DATE` for an invalid Date or one outside the date system's
|
|
285
|
+
* range: 1899-12-31 (Excel's day 0, where time-only values live, as serials
|
|
286
|
+
* 0 to <1; serialToDate returns those) or 1904-01-01, to 9999-12-31.
|
|
287
|
+
*/
|
|
288
|
+
function dateToSerial(d, options) {
|
|
289
|
+
const ms = Number.isFinite(d.getTime()) ? wallClock(d, options?.dateZone ?? "local") : NaN;
|
|
290
|
+
const min = options?.date1904 ? EPOCH_1904 : MIN_1900;
|
|
291
|
+
if (!Number.isFinite(ms) || ms < min || ms > MAX) throw new ValidationError("INVALID_DATE", `Date ${String(d)} is outside the range Excel can store.`, { details: { value: Number.isFinite(d.getTime()) ? d.toISOString() : "Invalid Date" } });
|
|
292
|
+
if (options?.date1904) return (ms - EPOCH_1904) / DAY_MS;
|
|
293
|
+
return (ms - (ms < LEAP_BUG_START ? EPOCH_1900_EARLY : EPOCH_1900)) / DAY_MS;
|
|
294
|
+
}
|
|
295
|
+
/**
|
|
296
|
+
* Converts an Excel serial number to a Date (inverse of `dateToSerial`).
|
|
297
|
+
* - 1900 system: serials 0 to <1 are Excel's day 0 ("1900-01-00", i.e.
|
|
298
|
+
* 1899-12-31), which is how time-only values such as 0.5 (12:00) are
|
|
299
|
+
* stored. Serial 60 (the fictitious 1900-02-29) has no real date and gives
|
|
300
|
+
* 1900-03-01; callers that must flag it (the reader) check for it themselves.
|
|
301
|
+
* - 1904 system: serial 0 = 1904-01-01.
|
|
302
|
+
* Throws `INVALID_DATE` for negative, non-finite or out-of-range serials.
|
|
303
|
+
*/
|
|
304
|
+
function serialToDate(serial, options) {
|
|
305
|
+
const max = options?.date1904 ? (MAX - EPOCH_1904) / DAY_MS : (MAX - EPOCH_1900) / DAY_MS;
|
|
306
|
+
if (!Number.isFinite(serial) || serial < 0 || serial > max) throw new ValidationError("INVALID_DATE", `Serial ${serial} is not a valid date.`, { details: { value: serial } });
|
|
307
|
+
let ms;
|
|
308
|
+
if (options?.date1904) ms = EPOCH_1904 + Math.round(serial * DAY_MS);
|
|
309
|
+
else if (serial < 60) ms = EPOCH_1900_EARLY + Math.round(serial * DAY_MS);
|
|
310
|
+
else if (serial < 61) ms = EPOCH_1900 + Math.round((serial + 1) * DAY_MS);
|
|
311
|
+
else ms = EPOCH_1900 + Math.round(serial * DAY_MS);
|
|
312
|
+
if ((options?.dateZone ?? "local") === "utc") return new Date(ms);
|
|
313
|
+
const u = new Date(ms);
|
|
314
|
+
return new Date(u.getUTCFullYear(), u.getUTCMonth(), u.getUTCDate(), u.getUTCHours(), u.getUTCMinutes(), u.getUTCSeconds(), u.getUTCMilliseconds());
|
|
315
|
+
}
|
|
316
|
+
// src/numfmt.ts
|
|
317
|
+
/** The built-in number formats ECMA-376 §18.8.30 defines (§11.2). Ids 14–17 and 22 render in the user's locale in Excel. */
|
|
318
|
+
const BUILTIN_NUM_FMTS = Object.freeze({
|
|
319
|
+
0: "General",
|
|
320
|
+
1: "0",
|
|
321
|
+
2: "0.00",
|
|
322
|
+
3: "#,##0",
|
|
323
|
+
4: "#,##0.00",
|
|
324
|
+
9: "0%",
|
|
325
|
+
10: "0.00%",
|
|
326
|
+
11: "0.00E+00",
|
|
327
|
+
12: "# ?/?",
|
|
328
|
+
13: "# ??/??",
|
|
329
|
+
14: "mm-dd-yy",
|
|
330
|
+
15: "d-mmm-yy",
|
|
331
|
+
16: "d-mmm",
|
|
332
|
+
17: "mmm-yy",
|
|
333
|
+
18: "h:mm AM/PM",
|
|
334
|
+
19: "h:mm:ss AM/PM",
|
|
335
|
+
20: "h:mm",
|
|
336
|
+
21: "h:mm:ss",
|
|
337
|
+
22: "m/d/yy h:mm",
|
|
338
|
+
37: "#,##0 ;(#,##0)",
|
|
339
|
+
38: "#,##0 ;[Red](#,##0)",
|
|
340
|
+
39: "#,##0.00;(#,##0.00)",
|
|
341
|
+
40: "#,##0.00;[Red](#,##0.00)",
|
|
342
|
+
45: "mm:ss",
|
|
343
|
+
46: "[h]:mm:ss",
|
|
344
|
+
47: "mmss.0",
|
|
345
|
+
48: "##0.0E+0",
|
|
346
|
+
49: "@"
|
|
347
|
+
});
|
|
348
|
+
/** Built-in ids that are dates or times: 14–22, 45 and 47, and the East Asian locale ids 27–36 and 50–58. 46 is `[h]:mm:ss`, a duration. */
|
|
349
|
+
const DATE_IDS = /* @__PURE__ */ new Set([
|
|
350
|
+
14,
|
|
351
|
+
15,
|
|
352
|
+
16,
|
|
353
|
+
17,
|
|
354
|
+
18,
|
|
355
|
+
19,
|
|
356
|
+
20,
|
|
357
|
+
21,
|
|
358
|
+
22,
|
|
359
|
+
27,
|
|
360
|
+
28,
|
|
361
|
+
29,
|
|
362
|
+
30,
|
|
363
|
+
31,
|
|
364
|
+
32,
|
|
365
|
+
33,
|
|
366
|
+
34,
|
|
367
|
+
35,
|
|
368
|
+
36,
|
|
369
|
+
45,
|
|
370
|
+
47,
|
|
371
|
+
50,
|
|
372
|
+
51,
|
|
373
|
+
52,
|
|
374
|
+
53,
|
|
375
|
+
54,
|
|
376
|
+
55,
|
|
377
|
+
56,
|
|
378
|
+
57,
|
|
379
|
+
58
|
|
380
|
+
]);
|
|
381
|
+
const cache = /* @__PURE__ */ new Map();
|
|
382
|
+
/**
|
|
383
|
+
* True when a number format shows a date or a time of day (§3.4.1): built-in
|
|
384
|
+
* ids 14–22, 45, 47, 27–36 and 50–58, or a custom code that contains a date or
|
|
385
|
+
* time token outside quoted text, escapes and brackets. Elapsed-time formats
|
|
386
|
+
* (`[h]:mm:ss`, built-in 46) are durations and return false.
|
|
387
|
+
*/
|
|
388
|
+
function isDateFormat(fmt) {
|
|
389
|
+
if (fmt === void 0) return false;
|
|
390
|
+
if (typeof fmt === "number") return DATE_IDS.has(fmt);
|
|
391
|
+
let hit = cache.get(fmt);
|
|
392
|
+
if (hit === void 0) {
|
|
393
|
+
hit = detect(fmt);
|
|
394
|
+
if (cache.size < 1e3) cache.set(fmt, hit);
|
|
395
|
+
}
|
|
396
|
+
return hit;
|
|
397
|
+
}
|
|
398
|
+
function detect(code) {
|
|
399
|
+
let s = "";
|
|
400
|
+
let inQuote = false;
|
|
401
|
+
for (let i = 0; i < code.length; i++) {
|
|
402
|
+
const ch = code[i];
|
|
403
|
+
if (inQuote) {
|
|
404
|
+
if (ch === "\"") inQuote = false;
|
|
405
|
+
continue;
|
|
406
|
+
}
|
|
407
|
+
if (ch === "\"") {
|
|
408
|
+
inQuote = true;
|
|
409
|
+
continue;
|
|
410
|
+
}
|
|
411
|
+
if (ch === "\\" || ch === "_" || ch === "*") {
|
|
412
|
+
i++;
|
|
413
|
+
continue;
|
|
414
|
+
}
|
|
415
|
+
if (ch === ";") break;
|
|
416
|
+
if (ch === "[") {
|
|
417
|
+
const end = code.indexOf("]", i + 1);
|
|
418
|
+
if (end === -1) break;
|
|
419
|
+
const inner = code.slice(i + 1, end).toLowerCase();
|
|
420
|
+
if (/^(h+|m+|s+)$/.test(inner)) return false;
|
|
421
|
+
i = end;
|
|
422
|
+
continue;
|
|
423
|
+
}
|
|
424
|
+
s += ch;
|
|
425
|
+
}
|
|
426
|
+
s = s.replace(/general/gi, "").replace(/e[+-]/gi, "");
|
|
427
|
+
return /[ymdhs]|am\/pm|a\/p|[egb]/i.test(s);
|
|
428
|
+
}
|
|
429
|
+
export { fromDosDateTime as a, nativeCodec as c, throttleProgress as d, ByteWriter as f, serialToDate as i, decodeUtf8Strict as l, throwIfAborted as m, isDateFormat as n, toDosDateTime as o, concatBytes as p, dateToSerial as r, crc32 as s, BUILTIN_NUM_FMTS as t, encodeUtf8 as u };
|