javascript-excel 2.2.0 → 2.3.0

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.
@@ -17,8 +17,23 @@ const DEFAULT_LIMITS = Object.freeze({
17
17
  maxCells: 1e7,
18
18
  maxItems: 1e7,
19
19
  maxSharedStrings: 5e6,
20
+ maxCellTextLength: 1048576,
20
21
  maxWarnings: 1e3
21
22
  });
23
+ function head(s) {
24
+ let end = 64;
25
+ const c = s.charCodeAt(end - 1);
26
+ if (c >= 55296 && c <= 56319) end--;
27
+ return s.slice(0, end);
28
+ }
29
+ /** `s` itself when short; else its start, `…` and its length: `abc… (16000000 characters)`. */
30
+ function excerpt(s) {
31
+ return s.length <= 128 ? s : `${head(s)}… (${s.length} characters)`;
32
+ }
33
+ /** `JSON.stringify(s)` when short; else the quoted start, `…` and its length: `"abc"… (16000000 characters)`. */
34
+ function quoted(s) {
35
+ return s.length <= 128 ? JSON.stringify(s) : `${JSON.stringify(head(s))}… (${s.length} characters)`;
36
+ }
22
37
  // src/internal/zip/read.ts
23
38
  /**
24
39
  * Zip container reader (docs/api.md §6, §8). Internal.
@@ -221,15 +236,15 @@ function openZip(bytes, options = {}) {
221
236
  }
222
237
  if (diskStart !== 0) throw invalid("Multi-disk zip archives are not supported.");
223
238
  const name = decodeName(rawName, flags, extra);
224
- if (flags & 65 || method === METHOD_AES) throw new require_errors.FormatError("ZIP_ENCRYPTED", `Entry ${name} is encrypted.`, { details: { entry: name } });
225
- if (method !== 0 && method !== 8) throw new require_errors.FormatError("ZIP_UNSUPPORTED_METHOD", `Entry ${name} uses compression method ${method}.`, { details: {
239
+ if (flags & 65 || method === METHOD_AES) throw new require_errors.FormatError("ZIP_ENCRYPTED", `Entry ${excerpt(name)} is encrypted.`, { details: { entry: name } });
240
+ if (method !== 0 && method !== 8) throw new require_errors.FormatError("ZIP_UNSUPPORTED_METHOD", `Entry ${excerpt(name)} uses compression method ${method}.`, { details: {
226
241
  entry: name,
227
242
  method
228
243
  } });
229
- if (method === 0 && compressedSize !== size) throw invalid(`Stored entry ${name} has mismatched sizes.`);
230
- if (byName.has(name)) throw invalid(`Duplicate entry name: ${name}`, { entry: name });
244
+ if (method === 0 && compressedSize !== size) throw invalid(`Stored entry ${excerpt(name)} has mismatched sizes.`);
245
+ if (byName.has(name)) throw invalid(`Duplicate entry name: ${excerpt(name)}`, { entry: name });
231
246
  const l = new Cursor(bytes, localOffset);
232
- if (localOffset + 30 > cdOffset || l.u32() !== SIG_LOCAL) throw invalid(`Bad local header for ${name}.`, { entry: name });
247
+ if (localOffset + 30 > cdOffset || l.u32() !== SIG_LOCAL) throw invalid(`Bad local header for ${excerpt(name)}.`, { entry: name });
233
248
  l.u16();
234
249
  const lFlags = l.u16();
235
250
  const lMethod = l.u16();
@@ -244,10 +259,10 @@ function openZip(bytes, options = {}) {
244
259
  l.take(lExtraLength);
245
260
  let agrees = lNameLength === nameLength && lName.every((b, k) => b === rawName[k]) && lMethod === method && (lFlags & FLAG_ENCRYPTED) === 0;
246
261
  if (agrees && !(lFlags & FLAG_DATA_DESCRIPTOR)) agrees = lCrc === crc && (lCompressed === compressedSize || lCompressed === 4294967295) && (lSize === size || lSize === 4294967295);
247
- if (!agrees) throw invalid(`Local header disagrees with the central directory for ${name}.`, { entry: name });
262
+ if (!agrees) throw invalid(`Local header disagrees with the central directory for ${excerpt(name)}.`, { entry: name });
248
263
  const dataOffset = l.pos;
249
264
  const dataEnd = dataOffset + compressedSize;
250
- if (dataEnd > cdOffset) throw invalid(`Data of ${name} runs into the central directory.`, { entry: name });
265
+ if (dataEnd > cdOffset) throw invalid(`Data of ${excerpt(name)} runs into the central directory.`, { entry: name });
251
266
  spans.push({
252
267
  start: localOffset,
253
268
  end: dataEnd,
@@ -267,7 +282,7 @@ function openZip(bytes, options = {}) {
267
282
  byName.set(name, info);
268
283
  }
269
284
  spans.sort((a, b) => a.start - b.start);
270
- for (let i = 1; i < spans.length; i++) if (spans[i].start < spans[i - 1].end) throw invalid(`Entries ${spans[i - 1].name} and ${spans[i].name} overlap.`);
285
+ for (let i = 1; i < spans.length; i++) if (spans[i].start < spans[i - 1].end) throw invalid(`Entries ${excerpt(spans[i - 1].name)} and ${excerpt(spans[i].name)} overlap.`);
271
286
  let totalBytesRead = 0;
272
287
  const resolve = (entry) => {
273
288
  const info = typeof entry === "string" ? byName.get(entry) : entry;
@@ -281,7 +296,7 @@ function openZip(bytes, options = {}) {
281
296
  async function* stream(entry, signal) {
282
297
  const info = resolve(entry);
283
298
  require_dos_time.throwIfAborted(signal);
284
- if (info.size > limits.maxEntryBytes) throw limitError("maxEntryBytes", info.size, limits.maxEntryBytes, `Entry ${info.name}`);
299
+ if (info.size > limits.maxEntryBytes) throw limitError("maxEntryBytes", info.size, limits.maxEntryBytes, `Entry ${excerpt(info.name)}`);
285
300
  const data = bytes.subarray(info.dataOffset, info.dataOffset + info.compressedSize);
286
301
  let crc = 0;
287
302
  let produced = 0;
@@ -302,12 +317,12 @@ function openZip(bytes, options = {}) {
302
317
  }, signal);
303
318
  for await (const chunk of chunks) {
304
319
  produced += chunk.length;
305
- if (produced > info.size) throw invalid(`Entry ${info.name} is larger than declared.`, { entry: info.name });
320
+ if (produced > info.size) throw invalid(`Entry ${excerpt(info.name)} is larger than declared.`, { entry: info.name });
306
321
  countTotal(chunk.length);
307
322
  crc = require_dos_time.crc32(chunk, crc);
308
323
  yield chunk;
309
324
  }
310
- if (produced !== info.size) throw invalid(`Entry ${info.name} is smaller than declared.`, { entry: info.name });
325
+ if (produced !== info.size) throw invalid(`Entry ${excerpt(info.name)} is smaller than declared.`, { entry: info.name });
311
326
  if (crc !== info.crc32) throw crcMismatch(info);
312
327
  }
313
328
  return {
@@ -325,7 +340,7 @@ function openZip(bytes, options = {}) {
325
340
  };
326
341
  }
327
342
  function crcMismatch(info) {
328
- return new require_errors.FormatError("ZIP_CRC_MISMATCH", `CRC-32 mismatch in ${info.name}.`, { details: { entry: info.name } });
343
+ return new require_errors.FormatError("ZIP_CRC_MISMATCH", `CRC-32 mismatch in ${excerpt(info.name)}.`, { details: { entry: info.name } });
329
344
  }
330
345
  // src/internal/read/xml-reader.ts
331
346
  /**
@@ -466,9 +481,9 @@ var XmlTokenizer = class {
466
481
  end() {
467
482
  if (this.wait !== Wait.None) this.unsuspend();
468
483
  this.pump(true);
469
- if (this.skipTag !== void 0) throw malformed(`Element <${this.skipTag.slice(2)}> is not closed.`);
484
+ if (this.skipTag !== void 0) throw malformed(`Element <${excerpt(this.skipTag.slice(2))}> is not closed.`);
470
485
  if (this.buf.trim() !== "") throw malformed("Unexpected end of XML data.");
471
- if (this.stack.length > 0) throw malformed(`Element <${this.stack.at(-1)}> is not closed.`);
486
+ if (this.stack.length > 0) throw malformed(`Element <${excerpt(this.stack.at(-1))}> is not closed.`);
472
487
  if (!this.rootSeen) throw malformed("XML part has no root element.");
473
488
  }
474
489
  /**
@@ -540,7 +555,7 @@ var XmlTokenizer = class {
540
555
  const tag = this.skipTag;
541
556
  const end = this.skipTo(buf, i);
542
557
  if (end === -1) {
543
- if (final) throw malformed(`Element <${tag.slice(2)}> is not closed.`);
558
+ if (final) throw malformed(`Element <${excerpt(tag.slice(2))}> is not closed.`);
544
559
  i = len;
545
560
  break;
546
561
  }
@@ -607,7 +622,7 @@ var XmlTokenizer = class {
607
622
  this.checkToken(e + 1 - i);
608
623
  const qname = buf.slice(i + 2, e).trim();
609
624
  const open = this.stack.pop();
610
- if (open === void 0 || open !== qname) throw malformed(open === void 0 ? `Unexpected end tag </${qname}>.` : `End tag </${qname}> does not match <${open}>.`);
625
+ if (open === void 0 || open !== qname) throw malformed(open === void 0 ? `Unexpected end tag </${excerpt(qname)}>.` : `End tag </${excerpt(qname)}> does not match <${excerpt(open)}>.`);
611
626
  if (this.stack.length === 0) this.rootClosed = true;
612
627
  this.handler.close(localName(qname));
613
628
  i = e + 1;
@@ -691,15 +706,15 @@ var XmlTokenizer = class {
691
706
  while (p < stop && buf.charCodeAt(p) !== 61 && !isSpace(buf.charCodeAt(p))) p++;
692
707
  const aname = buf.slice(nameStart, p);
693
708
  while (p < stop && isSpace(buf.charCodeAt(p))) p++;
694
- if (buf.charCodeAt(p) !== 61 || aname.length === 0) throw malformed(`Malformed attribute in <${qname}>.`);
709
+ if (buf.charCodeAt(p) !== 61 || aname.length === 0) throw malformed(`Malformed attribute in <${excerpt(qname)}>.`);
695
710
  p++;
696
711
  while (p < stop && isSpace(buf.charCodeAt(p))) p++;
697
712
  const q = buf.charCodeAt(p);
698
- if (q !== 34 && q !== 39) throw malformed(`Unquoted attribute value in <${qname}>.`);
713
+ if (q !== 34 && q !== 39) throw malformed(`Unquoted attribute value in <${excerpt(qname)}>.`);
699
714
  const close = buf.indexOf(q === 34 ? "\"" : "'", p + 1);
700
- if (close === -1 || close >= stop) throw malformed(`Unterminated attribute value in <${qname}>.`);
715
+ if (close === -1 || close >= stop) throw malformed(`Unterminated attribute value in <${excerpt(qname)}>.`);
701
716
  const raw = buf.slice(p + 1, close);
702
- if (raw.indexOf("<") !== -1) throw malformed(`'<' in an attribute value of <${qname}>.`);
717
+ if (raw.indexOf("<") !== -1) throw malformed(`'<' in an attribute value of <${excerpt(qname)}>.`);
703
718
  items.push(aname, normaliseAttr(raw));
704
719
  p = close + 1;
705
720
  }
@@ -944,7 +959,7 @@ var ReadContext = class {
944
959
  }
945
960
  };
946
961
  function withPart(err, part) {
947
- if (err instanceof require_errors.FormatError && (err.code === "XML_MALFORMED" || err.code === "XML_DTD_FORBIDDEN") && !err.details?.["part"]) return new require_errors.FormatError(err.code, `${part}: ${err.message}`, {
962
+ if (err instanceof require_errors.FormatError && (err.code === "XML_MALFORMED" || err.code === "XML_DTD_FORBIDDEN") && !err.details?.["part"]) return new require_errors.FormatError(err.code, `${excerpt(part)}: ${err.message}`, {
948
963
  details: {
949
964
  ...err.details,
950
965
  part
@@ -952,7 +967,7 @@ function withPart(err, part) {
952
967
  cause: err
953
968
  });
954
969
  const limit = err instanceof require_errors.LimitError ? err.details?.["limit"] : void 0;
955
- if (err instanceof require_errors.LimitError && (limit === "maxXmlDepth" || limit === "maxXmlTokenLength") && !err.details?.["part"]) return new require_errors.LimitError(err.code, `${part}: ${err.message}`, {
970
+ if (err instanceof require_errors.LimitError && (limit === "maxXmlDepth" || limit === "maxXmlTokenLength" || limit === "maxCellTextLength") && !err.details?.["part"]) return new require_errors.LimitError(err.code, `${excerpt(part)}: ${err.message}`, {
956
971
  details: {
957
972
  ...err.details,
958
973
  part
@@ -1081,7 +1096,7 @@ var Package = class {
1081
1096
  for (const e of ctx.zip.entries) {
1082
1097
  if (e.name.endsWith("/")) continue;
1083
1098
  const key = e.name.toLowerCase();
1084
- if (this.parts.has(key)) throw new require_errors.FormatError("ZIP_INVALID", `Part names ${this.parts.get(key).name} and ${e.name} differ only in case.`, { details: { entry: e.name } });
1099
+ if (this.parts.has(key)) throw new require_errors.FormatError("ZIP_INVALID", `Part names ${excerpt(this.parts.get(key).name)} and ${excerpt(e.name)} differ only in case.`, { details: { entry: e.name } });
1085
1100
  this.parts.set(key, e);
1086
1101
  }
1087
1102
  }
@@ -1786,15 +1801,105 @@ function decodeXstring(s) {
1786
1801
  return s.replace(/_x([0-9A-Fa-f]{4})_/g, (_, hex) => String.fromCharCode(parseInt(hex, 16)));
1787
1802
  }
1788
1803
  /**
1804
+ * The length a text will have once `decodeXstring` has run on it, counted
1805
+ * chunk by chunk as the text streams in (JSE-58), so a cell's text can be
1806
+ * refused before it is held whole. `committed` never exceeds the final
1807
+ * length: up to six trailing characters that may start an escape are held
1808
+ * back until the next chunk or `end()`. With `decode` false the text is
1809
+ * counted as it is (`<v>` of number, date, error and boolean cells).
1810
+ */
1811
+ var XstringLength = class {
1812
+ decode;
1813
+ /** Characters known so far. */
1814
+ committed = 0;
1815
+ tail = "";
1816
+ constructor(decode = true) {
1817
+ this.decode = decode;
1818
+ }
1819
+ add(chunk) {
1820
+ if (!this.decode) {
1821
+ this.committed += chunk.length;
1822
+ return;
1823
+ }
1824
+ const s = this.tail.length ? this.tail + chunk : chunk;
1825
+ const n = s.length;
1826
+ let i = 0;
1827
+ let escapes = 0;
1828
+ for (let j = s.indexOf("_x"); j !== -1 && j + 7 <= n; j = s.indexOf("_x", i)) if (isHex(s.charCodeAt(j + 2)) && isHex(s.charCodeAt(j + 3)) && isHex(s.charCodeAt(j + 4)) && isHex(s.charCodeAt(j + 5)) && s.charCodeAt(j + 6) === 95) {
1829
+ escapes++;
1830
+ i = j + 7;
1831
+ } else i = j + 1;
1832
+ const cut = Math.max(i, n - 6);
1833
+ this.committed += cut - 6 * escapes;
1834
+ this.tail = cut < n ? s.slice(cut) : "";
1835
+ }
1836
+ /** Settles the held-back characters and returns the final length. */
1837
+ end() {
1838
+ this.committed += this.tail.length;
1839
+ this.tail = "";
1840
+ return this.committed;
1841
+ }
1842
+ };
1843
+ function isHex(c) {
1844
+ return c >= 48 && c <= 57 || c >= 65 && c <= 70 || c >= 97 && c <= 102;
1845
+ }
1846
+ /** The LimitError for a cell's text over maxCellTextLength; `value` is max + 1, "more than max" (JSE-58). */
1847
+ function cellTextTooLong(max, details = {}) {
1848
+ const where = typeof details["ref"] === "string" && details["ref"] !== "" ? `Cell ${details["ref"]}: its` : "A cell's";
1849
+ return new require_errors.LimitError("LIMIT_EXCEEDED", `${where} text is longer than maxCellTextLength (${max}) characters.`, { details: {
1850
+ limit: "maxCellTextLength",
1851
+ value: max + 1,
1852
+ max,
1853
+ ...details
1854
+ } });
1855
+ }
1856
+ /**
1857
+ * Counts text the reader keeps outside cells (header and footer text, a
1858
+ * defined name's formula, a document property's value) against
1859
+ * maxCellTextLength as it streams in, so it is refused before it is held
1860
+ * whole (JSE-58 review). This text is kept as written, so it is counted as
1861
+ * written. `what` names it in the error message.
1862
+ */
1863
+ var TextBudget = class {
1864
+ max;
1865
+ what;
1866
+ n = 0;
1867
+ constructor(max, what) {
1868
+ this.max = max;
1869
+ this.what = what;
1870
+ }
1871
+ add(s) {
1872
+ this.n += s.length;
1873
+ if (this.n > this.max) throw new require_errors.LimitError("LIMIT_EXCEEDED", `${this.what} is longer than maxCellTextLength (${this.max}) characters.`, { details: {
1874
+ limit: "maxCellTextLength",
1875
+ value: this.max + 1,
1876
+ max: this.max
1877
+ } });
1878
+ }
1879
+ };
1880
+ /**
1789
1881
  * Collects one string item (<si> or <is>). Feed it the events between the
1790
1882
  * item's open and close; `value()` gives the string or rich text.
1791
1883
  */
1792
1884
  var StringItem = class {
1793
1885
  charge;
1794
- /** @param charge Counts each run after the first against the read's maxItems budget (JSE-52). */
1795
- constructor(charge = () => {}) {
1886
+ max;
1887
+ details;
1888
+ /**
1889
+ * @param charge Counts each run after the first against the read's maxItems budget (JSE-52).
1890
+ * @param max maxCellTextLength: the item's text (all runs together, after decoding) may not be
1891
+ * longer; checked as the text arrives, so a longer item is refused before it is held whole (JSE-58).
1892
+ * @param details Extra `details` for the LimitError (a sheet cell's `ref`).
1893
+ */
1894
+ constructor(charge = () => {}, max = 1e400, details = {}) {
1796
1895
  this.charge = charge;
1896
+ this.max = max;
1897
+ this.details = details;
1797
1898
  }
1899
+ /** Decoded length of the plain text and of the current run; finished runs are in `runsLength`. */
1900
+ plainLength = new XstringLength();
1901
+ runLength = new XstringLength();
1902
+ runsLength = 0;
1798
1903
  runs = [];
1799
1904
  plain = "";
1800
1905
  hasRuns = false;
@@ -1817,6 +1922,7 @@ var StringItem = class {
1817
1922
  this.inRun = true;
1818
1923
  this.hasRuns = true;
1819
1924
  this.runText = "";
1925
+ this.runLength = new XstringLength();
1820
1926
  this.font = void 0;
1821
1927
  return;
1822
1928
  case "rPr":
@@ -1842,6 +1948,8 @@ var StringItem = class {
1842
1948
  this.inRPr = false;
1843
1949
  return;
1844
1950
  case "r": {
1951
+ this.runsLength += this.runLength.end();
1952
+ this.check(0);
1845
1953
  if (this.runs.length > 0) this.charge(1);
1846
1954
  const run = { text: decodeXstring(this.runText) };
1847
1955
  if (this.font && Object.keys(this.font.font).length) run.font = this.font.font;
@@ -1854,10 +1962,23 @@ var StringItem = class {
1854
1962
  }
1855
1963
  text(s) {
1856
1964
  if (!this.inT || this.inRPh > 0) return;
1857
- if (this.inRun) this.runText += s;
1858
- else this.plain += s;
1965
+ if (this.inRun) {
1966
+ this.runLength.add(s);
1967
+ this.check(this.runLength.committed);
1968
+ this.runText += s;
1969
+ } else {
1970
+ this.plainLength.add(s);
1971
+ this.check(0);
1972
+ this.plain += s;
1973
+ }
1974
+ }
1975
+ /** Throws once the text known so far (`current`: the open run's) is longer than max. */
1976
+ check(current) {
1977
+ if (this.plainLength.committed + this.runsLength + current > this.max) throw cellTextTooLong(this.max, this.details);
1859
1978
  }
1860
1979
  value(mode) {
1980
+ this.plainLength.end();
1981
+ this.check(0);
1861
1982
  if (!this.hasRuns) return decodeXstring(this.plain);
1862
1983
  if (mode === "plain") return this.runs.map((r) => r.text).join("");
1863
1984
  return { richText: this.runs };
@@ -1869,6 +1990,7 @@ var SharedStringsParser = class {
1869
1990
  max;
1870
1991
  stopAfter;
1871
1992
  charge;
1993
+ maxText;
1872
1994
  strings = [];
1873
1995
  item;
1874
1996
  depth = 0;
@@ -1877,11 +1999,12 @@ var SharedStringsParser = class {
1877
1999
  * `stopAfter`: decode only the first `stopAfter` items, then jump to the
1878
2000
  * end of the part without tokenizing the rest (previews, ReadOptions.maxRows).
1879
2001
  */
1880
- constructor(mode, max, stopAfter = 1e400, charge = () => {}) {
2002
+ constructor(mode, max, stopAfter = 1e400, charge = () => {}, maxText = 1e400) {
1881
2003
  this.mode = mode;
1882
2004
  this.max = max;
1883
2005
  this.stopAfter = stopAfter;
1884
2006
  this.charge = charge;
2007
+ this.maxText = maxText;
1885
2008
  }
1886
2009
  attach(control) {
1887
2010
  this.control = control;
@@ -1916,7 +2039,7 @@ var SharedStringsParser = class {
1916
2039
  this.strings.push("");
1917
2040
  this.pushed();
1918
2041
  } else {
1919
- this.item = new StringItem(this.charge);
2042
+ this.item = new StringItem(this.charge, this.maxText);
1920
2043
  this.depth = 0;
1921
2044
  }
1922
2045
  }
@@ -2141,7 +2264,7 @@ var SheetParser = class {
2141
2264
  return;
2142
2265
  case "is":
2143
2266
  if (this.cell && !empty) {
2144
- this.inline = new StringItem((n) => this.ctx.charge(n));
2267
+ this.inline = new StringItem((n) => this.ctx.charge(n), this.ctx.limits.maxCellTextLength, this.cell.ref ? { ref: this.cell.ref } : {});
2145
2268
  this.inlineDepth = 0;
2146
2269
  }
2147
2270
  return;
@@ -2247,10 +2370,12 @@ var SheetParser = class {
2247
2370
  this.path.pop();
2248
2371
  switch (name) {
2249
2372
  case "v":
2373
+ this.endText();
2250
2374
  if (this.cell && this.textTarget === "v") this.cell.v = this.buf;
2251
2375
  this.textTarget = void 0;
2252
2376
  return;
2253
2377
  case "f":
2378
+ this.endText();
2254
2379
  if (this.cell && this.textTarget === "f") this.cell.f = decodeXstring(this.buf);
2255
2380
  this.textTarget = void 0;
2256
2381
  return;
@@ -2276,11 +2401,38 @@ var SheetParser = class {
2276
2401
  this.inline.text(s);
2277
2402
  return;
2278
2403
  }
2279
- if (this.textTarget) this.buf += s;
2404
+ if (!this.textTarget) return;
2405
+ if (this.textTarget === "hf") this.hfBudget?.add(s);
2406
+ const len = this.textLength;
2407
+ if (len) {
2408
+ len.add(s);
2409
+ if (len.committed > this.ctx.limits.maxCellTextLength) this.textTooLong();
2410
+ }
2411
+ this.buf += s;
2412
+ }
2413
+ /**
2414
+ * Counts the text of a cell's `<v>` or `<f>` against maxCellTextLength as it
2415
+ * arrives (JSE-58), decoded as the cell's value will be: `_xHHHH_` escapes
2416
+ * count once in strings and formulas; other `<v>` text is kept as written.
2417
+ * Header and footer text is not cell text.
2418
+ */
2419
+ textLength;
2420
+ hfBudget;
2421
+ endText() {
2422
+ const len = this.textLength;
2423
+ this.textLength = void 0;
2424
+ if (len && len.end() > this.ctx.limits.maxCellTextLength) this.textTooLong();
2425
+ }
2426
+ textTooLong() {
2427
+ const ref = this.cell?.ref;
2428
+ throw cellTextTooLong(this.ctx.limits.maxCellTextLength, ref ? { ref } : {});
2280
2429
  }
2281
2430
  startText(target) {
2282
2431
  this.textTarget = target;
2283
2432
  this.buf = "";
2433
+ const t = this.cell?.t;
2434
+ this.textLength = target === "hf" ? void 0 : new XstringLength(target === "f" || t === "str" || t === "inlineStr");
2435
+ this.hfBudget = target === "hf" ? new TextBudget(this.ctx.limits.maxCellTextLength, "Header or footer text") : void 0;
2284
2436
  }
2285
2437
  openRow(a) {
2286
2438
  const r = int(a.get("r"));
@@ -2396,7 +2548,7 @@ var SheetParser = class {
2396
2548
  if (r !== void 0) {
2397
2549
  const pos = parseRef(r);
2398
2550
  if (!pos) {
2399
- this.ctx.warn("INVALID_CELL_REF", `Cell reference "${r}" is not valid; the cell was skipped.`, {
2551
+ this.ctx.warn("INVALID_CELL_REF", `Cell reference "${excerpt(r)}" is not valid; the cell was skipped.`, {
2400
2552
  part: this.part,
2401
2553
  sheet: this.sheet.name
2402
2554
  });
@@ -2413,7 +2565,7 @@ var SheetParser = class {
2413
2565
  return;
2414
2566
  }
2415
2567
  if (row >= 1048576 || col >= 16384) {
2416
- this.ctx.warn("CELL_OUT_OF_BOUNDS", `Cell ${r ?? `${colLetters(col + 1)}${row + 1}`} is beyond the format limits; it was skipped.`, {
2568
+ this.ctx.warn("CELL_OUT_OF_BOUNDS", `Cell ${excerpt(r ?? `${colLetters(col + 1)}${row + 1}`)} is beyond the format limits; it was skipped.`, {
2417
2569
  part: this.part,
2418
2570
  sheet: this.sheet.name
2419
2571
  });
@@ -2439,7 +2591,7 @@ var SheetParser = class {
2439
2591
  truncate() {
2440
2592
  if (this.truncated) return;
2441
2593
  this.truncated = true;
2442
- this.ctx.warn("ROWS_TRUNCATED", `Sheet "${this.sheet.name}" has rows beyond maxRows (${this.opts.maxRows}); they were not read.`, {
2594
+ this.ctx.warn("ROWS_TRUNCATED", `Sheet "${excerpt(this.sheet.name)}" has rows beyond maxRows (${this.opts.maxRows}); they were not read.`, {
2443
2595
  part: this.part,
2444
2596
  sheet: this.sheet.name
2445
2597
  });
@@ -2521,7 +2673,7 @@ var SheetParser = class {
2521
2673
  const sst = this.sharedStrings;
2522
2674
  const str = Number.isInteger(idx) && idx >= 0 ? sst[idx] : void 0;
2523
2675
  if (str === void 0) {
2524
- this.ctx.warn("INVALID_SHARED_STRING", `Cell ${p.ref} refers to shared string ${v}, which does not exist; the cell was left empty.`, {
2676
+ this.ctx.warn("INVALID_SHARED_STRING", `Cell ${p.ref} refers to shared string ${excerpt(v)}, which does not exist; the cell was left empty.`, {
2525
2677
  part: this.part,
2526
2678
  sheet: this.sheet.name,
2527
2679
  ref: p.ref
@@ -2777,7 +2929,7 @@ var SheetParser = class {
2777
2929
  const first = h.ref.split(":")[0];
2778
2930
  const pos = parseRef(first);
2779
2931
  if (!pos) return;
2780
- if (h.ref.includes(":") && h.ref.split(":")[1] !== first) this.ctx.warn("HYPERLINK_RANGE_REDUCED", `The hyperlink on ${h.ref} was attached to its top-left cell ${first}.`, {
2932
+ if (h.ref.includes(":") && h.ref.split(":")[1] !== first) this.ctx.warn("HYPERLINK_RANGE_REDUCED", `The hyperlink on ${excerpt(h.ref)} was attached to its top-left cell ${excerpt(first)}.`, {
2781
2933
  part: this.part,
2782
2934
  sheet: this.sheet.name,
2783
2935
  ref: h.ref
@@ -2810,6 +2962,7 @@ var WorkbookParser = class {
2810
2962
  part;
2811
2963
  checkAbort;
2812
2964
  charge;
2965
+ maxText;
2813
2966
  sheets = [];
2814
2967
  definedNames = [];
2815
2968
  date1904 = false;
@@ -2825,12 +2978,14 @@ var WorkbookParser = class {
2825
2978
  * @param part The workbook part's name, for the error.
2826
2979
  * @param checkAbort Called every SHEET_ABORT_INTERVAL sheet entries, so a long sheet list cannot outrun the signal.
2827
2980
  */
2828
- constructor(maxSheets = 1e400, part = "", checkAbort, charge = () => {}) {
2981
+ constructor(maxSheets = 1e400, part = "", checkAbort, charge = () => {}, maxText = 1e400) {
2829
2982
  this.maxSheets = maxSheets;
2830
2983
  this.part = part;
2831
2984
  this.checkAbort = checkAbort;
2832
2985
  this.charge = charge;
2986
+ this.maxText = maxText;
2833
2987
  }
2988
+ nameBudget;
2834
2989
  open(name, a, empty) {
2835
2990
  switch (name) {
2836
2991
  case "workbook":
@@ -2889,13 +3044,16 @@ var WorkbookParser = class {
2889
3044
  }
2890
3045
  close(name) {
2891
3046
  if (name === "definedName" && this.name) {
3047
+ this.nameBudget = void 0;
2892
3048
  this.name.ref = this.nameText;
2893
3049
  this.definedNames.push(this.name);
2894
3050
  this.name = void 0;
2895
3051
  }
2896
3052
  }
2897
3053
  text(s) {
2898
- if (this.name) this.nameText += s;
3054
+ if (!this.name) return;
3055
+ (this.nameBudget ??= new TextBudget(this.maxText, `The formula of defined name "${excerpt(this.name.name)}"`)).add(s);
3056
+ this.nameText += s;
2899
3057
  }
2900
3058
  };
2901
3059
  const SCHEME_ORDER = [
@@ -2963,18 +3121,23 @@ const CORE_FIELDS = {
2963
3121
  /** docProps/core.xml and docProps/app.xml: simple elements with text. */
2964
3122
  var PropsParser = class {
2965
3123
  props;
3124
+ maxText;
2966
3125
  field;
2967
3126
  value = "";
2968
3127
  depth = 0;
2969
- constructor(props) {
3128
+ /** @param maxText maxCellTextLength, for each property's value (JSE-58 review). */
3129
+ constructor(props, maxText = 1e400) {
2970
3130
  this.props = props;
3131
+ this.maxText = maxText;
2971
3132
  }
3133
+ budget;
2972
3134
  open(name, _a, empty) {
2973
3135
  if (empty) return;
2974
3136
  this.depth++;
2975
3137
  if (this.depth === 2 && Object.prototype.hasOwnProperty.call(CORE_FIELDS, name)) {
2976
3138
  this.field = CORE_FIELDS[name];
2977
3139
  this.value = "";
3140
+ this.budget = new TextBudget(this.maxText, `Document property ${name}`);
2978
3141
  }
2979
3142
  }
2980
3143
  close() {
@@ -2990,17 +3153,23 @@ var PropsParser = class {
2990
3153
  this.depth--;
2991
3154
  }
2992
3155
  text(s) {
2993
- if (this.field) this.value += s;
3156
+ if (!this.field) return;
3157
+ this.budget?.add(s);
3158
+ this.value += s;
2994
3159
  }
2995
3160
  };
2996
3161
  /** docProps/custom.xml. */
2997
3162
  var CustomPropsParser = class {
2998
3163
  charge;
3164
+ maxText;
2999
3165
  items = [];
3000
3166
  /** @param charge Counts each property kept against the read's maxItems budget (JSE-52). */
3001
- constructor(charge = () => {}) {
3167
+ /** @param maxText maxCellTextLength, for each property's value (JSE-58 review). */
3168
+ constructor(charge = () => {}, maxText = 1e400) {
3002
3169
  this.charge = charge;
3170
+ this.maxText = maxText;
3003
3171
  }
3172
+ budget;
3004
3173
  name;
3005
3174
  kind;
3006
3175
  value = "";
@@ -3011,6 +3180,7 @@ var CustomPropsParser = class {
3011
3180
  } else if (this.name !== void 0 && this.kind === void 0) {
3012
3181
  this.kind = name;
3013
3182
  this.value = "";
3183
+ this.budget = new TextBudget(this.maxText, `Custom document property "${excerpt(this.name)}"`);
3014
3184
  }
3015
3185
  }
3016
3186
  close(name) {
@@ -3047,7 +3217,9 @@ var CustomPropsParser = class {
3047
3217
  this.kind = "";
3048
3218
  }
3049
3219
  text(s) {
3050
- if (this.kind) this.value += s;
3220
+ if (!this.kind) return;
3221
+ this.budget?.add(s);
3222
+ this.value += s;
3051
3223
  }
3052
3224
  };
3053
3225
  // src/internal/read/reader.ts
@@ -3127,7 +3299,7 @@ function openZipChecked(bytes, limits) {
3127
3299
  }
3128
3300
  async function parseWorkbook(pkg, part) {
3129
3301
  const ctx = pkg.ctx;
3130
- const wb = new WorkbookParser(ctx.limits.maxSheets, part, () => ctx.checkAbort(), (n) => ctx.charge(n));
3302
+ const wb = new WorkbookParser(ctx.limits.maxSheets, part, () => ctx.checkAbort(), (n) => ctx.charge(n), ctx.limits.maxCellTextLength);
3131
3303
  await ctx.parse(pkg.part(part), wb);
3132
3304
  return wb;
3133
3305
  }
@@ -3257,7 +3429,7 @@ async function readImpl(input, options, internal) {
3257
3429
  const stylesRel = relOf("styles");
3258
3430
  if (stylesRel) {
3259
3431
  const entry = pkg.part(stylesRel.target);
3260
- if (!entry) ctx.warn("MISSING_PART", `The styles part ${stylesRel.target} is missing; default styles were used.`, {
3432
+ if (!entry) ctx.warn("MISSING_PART", `The styles part ${excerpt(stylesRel.target)} is missing; default styles were used.`, {
3261
3433
  part: stylesRel.target,
3262
3434
  partKind: "styles"
3263
3435
  });
@@ -3275,7 +3447,7 @@ async function readImpl(input, options, internal) {
3275
3447
  const themeRel = relOf("theme");
3276
3448
  if (themeRel && o.styles !== false) {
3277
3449
  const entry = pkg.part(themeRel.target);
3278
- if (!entry) ctx.warn("MISSING_PART", `The theme part ${themeRel.target} is missing; the default theme applies.`, {
3450
+ if (!entry) ctx.warn("MISSING_PART", `The theme part ${excerpt(themeRel.target)} is missing; the default theme applies.`, {
3279
3451
  part: themeRel.target,
3280
3452
  partKind: "theme"
3281
3453
  });
@@ -3290,13 +3462,13 @@ async function readImpl(input, options, internal) {
3290
3462
  const sstRel = relOf("sharedStrings");
3291
3463
  if (sstRel) {
3292
3464
  const entry = pkg.part(sstRel.target);
3293
- if (!entry) ctx.warn("MISSING_PART", `The shared strings part ${sstRel.target} is missing; cells that use it are empty.`, {
3465
+ if (!entry) ctx.warn("MISSING_PART", `The shared strings part ${excerpt(sstRel.target)} is missing; cells that use it are empty.`, {
3294
3466
  part: sstRel.target,
3295
3467
  partKind: "sharedStrings"
3296
3468
  });
3297
3469
  else if (deferred) sstEntry = entry;
3298
3470
  else {
3299
- const sp = new SharedStringsParser(o.richText ?? "runs", limits.maxSharedStrings, 1e400, (n) => ctx.charge(n));
3471
+ const sp = new SharedStringsParser(o.richText ?? "runs", limits.maxSharedStrings, 1e400, (n) => ctx.charge(n), limits.maxCellTextLength);
3300
3472
  await ctx.parse(entry, sp);
3301
3473
  sst = sp.strings;
3302
3474
  }
@@ -3329,7 +3501,7 @@ async function readImpl(input, options, internal) {
3329
3501
  parser.deferred = deferred;
3330
3502
  const part = rel ? pkg.part(rel.target) : void 0;
3331
3503
  if (!part) {
3332
- ctx.warn("MISSING_PART", `The part for sheet "${entry.name}" is missing; the sheet is empty.`, {
3504
+ ctx.warn("MISSING_PART", `The part for sheet "${excerpt(entry.name)}" is missing; the sheet is empty.`, {
3333
3505
  sheet: entry.name,
3334
3506
  partKind: "worksheet"
3335
3507
  });
@@ -3363,7 +3535,7 @@ async function readImpl(input, options, internal) {
3363
3535
  if (sstEntry && deferred && deferred.cells.length === 0) ctx.consumed.add(sstEntry.name);
3364
3536
  if (deferred && deferred.cells.length > 0) {
3365
3537
  if (sstEntry) {
3366
- const sp = new SharedStringsParser(o.richText ?? "runs", limits.maxSharedStrings, deferred.maxIndex + 1, (n) => ctx.charge(n));
3538
+ const sp = new SharedStringsParser(o.richText ?? "runs", limits.maxSharedStrings, deferred.maxIndex + 1, (n) => ctx.charge(n), limits.maxCellTextLength);
3367
3539
  await ctx.parse(sstEntry, sp);
3368
3540
  sst = sp.strings;
3369
3541
  }
@@ -3455,7 +3627,7 @@ async function optional(ctx, part, partKind, fn) {
3455
3627
  return true;
3456
3628
  } catch (err) {
3457
3629
  if (err instanceof require_errors.FormatError && err.code === "XML_MALFORMED") {
3458
- ctx.warn("BROKEN_OPTIONAL_PART", `${part} is not well-formed XML and was skipped.`, {
3630
+ ctx.warn("BROKEN_OPTIONAL_PART", `${excerpt(part)} is not well-formed XML and was skipped.`, {
3459
3631
  part,
3460
3632
  partKind
3461
3633
  });
@@ -3475,13 +3647,13 @@ async function readProperties(pkg, ctx) {
3475
3647
  const rel = rels.find((r) => (r.kind === kind || r.type.endsWith(`/${kind}`)) && !r.external);
3476
3648
  const entry = rel ? pkg.part(rel.target) : void 0;
3477
3649
  if (!entry) continue;
3478
- const handler = new PropsParser(props);
3650
+ const handler = new PropsParser(props, ctx.limits.maxCellTextLength);
3479
3651
  await optional(ctx, entry.name, partKind, () => ctx.parse(entry, handler));
3480
3652
  }
3481
3653
  const customRel = rels.find((r) => r.kind === "custom-properties" && !r.external);
3482
3654
  const custom = customRel ? pkg.part(customRel.target) : void 0;
3483
3655
  if (custom) {
3484
- const cp = new CustomPropsParser((n) => ctx.charge(n));
3656
+ const cp = new CustomPropsParser((n) => ctx.charge(n), ctx.limits.maxCellTextLength);
3485
3657
  if (await optional(ctx, custom.name, "customProperties", () => ctx.parse(custom, cp)) && cp.items.length) props.custom = cp.items;
3486
3658
  }
3487
3659
  return Object.keys(props).length ? props : void 0;
@@ -3517,6 +3689,18 @@ Object.defineProperty(exports, "detectFormat", {
3517
3689
  return detectFormat;
3518
3690
  }
3519
3691
  });
3692
+ Object.defineProperty(exports, "excerpt", {
3693
+ enumerable: true,
3694
+ get: function() {
3695
+ return excerpt;
3696
+ }
3697
+ });
3698
+ Object.defineProperty(exports, "quoted", {
3699
+ enumerable: true,
3700
+ get: function() {
3701
+ return quoted;
3702
+ }
3703
+ });
3520
3704
  Object.defineProperty(exports, "read", {
3521
3705
  enumerable: true,
3522
3706
  get: function() {