@cj-tech-master/excelts 4.2.0 → 4.2.1-canary.20260111102127.f808a37

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.
Files changed (241) hide show
  1. package/THIRD_PARTY_NOTICES.md +0 -31
  2. package/dist/browser/index.browser.d.ts +1 -0
  3. package/dist/browser/index.browser.js +12 -0
  4. package/dist/{types/modules/archive → browser/modules/archive/compression}/compress.base.d.ts +1 -0
  5. package/dist/browser/modules/archive/{compress.base.js → compression/compress.base.js} +2 -1
  6. package/dist/{types/modules/archive → browser/modules/archive/compression}/compress.browser.d.ts +10 -8
  7. package/dist/{esm/modules/archive → browser/modules/archive/compression}/compress.browser.js +18 -19
  8. package/dist/browser/modules/archive/{compress.d.ts → compression/compress.d.ts} +2 -2
  9. package/dist/browser/modules/archive/{compress.js → compression/compress.js} +1 -1
  10. package/dist/browser/modules/archive/{crc32.browser.d.ts → compression/crc32.browser.d.ts} +1 -1
  11. package/dist/browser/modules/archive/{crc32.d.ts → compression/crc32.d.ts} +1 -1
  12. package/dist/browser/modules/archive/{crc32.js → compression/crc32.js} +1 -1
  13. package/dist/browser/modules/archive/{deflate-fallback.js → compression/deflate-fallback.js} +1 -1
  14. package/dist/browser/modules/archive/{streaming-compress.browser.d.ts → compression/streaming-compress.browser.d.ts} +2 -2
  15. package/dist/browser/modules/archive/{streaming-compress.browser.js → compression/streaming-compress.browser.js} +3 -3
  16. package/dist/browser/modules/archive/{streaming-compress.d.ts → compression/streaming-compress.d.ts} +2 -2
  17. package/dist/browser/modules/archive/{streaming-compress.js → compression/streaming-compress.js} +2 -2
  18. package/dist/browser/modules/archive/defaults.d.ts +1 -0
  19. package/dist/browser/modules/archive/defaults.js +6 -3
  20. package/dist/browser/modules/archive/index.base.d.ts +4 -4
  21. package/dist/browser/modules/archive/index.base.js +3 -6
  22. package/dist/browser/modules/archive/index.browser.d.ts +3 -4
  23. package/dist/browser/modules/archive/index.browser.js +3 -7
  24. package/dist/browser/modules/archive/index.d.ts +3 -4
  25. package/dist/browser/modules/archive/index.js +3 -5
  26. package/dist/browser/modules/archive/internal/byte-queue.d.ts +33 -0
  27. package/dist/browser/modules/archive/internal/byte-queue.js +407 -0
  28. package/dist/browser/modules/archive/io/archive-sink.d.ts +9 -0
  29. package/dist/browser/modules/archive/io/archive-sink.js +77 -0
  30. package/dist/browser/modules/archive/io/archive-source.d.ts +8 -0
  31. package/dist/browser/modules/archive/io/archive-source.js +107 -0
  32. package/dist/browser/modules/archive/{extract.d.ts → unzip/extract.d.ts} +2 -2
  33. package/dist/browser/modules/archive/unzip/index.d.ts +40 -0
  34. package/dist/browser/modules/archive/unzip/index.js +164 -0
  35. package/dist/browser/modules/archive/{parse.base.d.ts → unzip/stream.base.d.ts} +58 -3
  36. package/dist/browser/modules/archive/unzip/stream.base.js +1022 -0
  37. package/dist/browser/modules/archive/{parse.browser.d.ts → unzip/stream.browser.d.ts} +1 -1
  38. package/dist/browser/modules/archive/{parse.browser.js → unzip/stream.browser.js} +376 -110
  39. package/dist/browser/modules/archive/{parse.d.ts → unzip/stream.d.ts} +2 -2
  40. package/dist/{esm/modules/archive/parse.js → browser/modules/archive/unzip/stream.js} +7 -6
  41. package/dist/{types/modules/archive → browser/modules/archive/unzip}/zip-parser.d.ts +1 -1
  42. package/dist/{esm/modules/archive → browser/modules/archive/unzip}/zip-parser.js +38 -24
  43. package/dist/browser/modules/archive/utils/async-queue.d.ts +7 -0
  44. package/dist/browser/modules/archive/utils/async-queue.js +103 -0
  45. package/dist/browser/modules/archive/utils/bytes.js +16 -16
  46. package/dist/browser/modules/archive/utils/compressibility.d.ts +10 -0
  47. package/dist/browser/modules/archive/utils/compressibility.js +57 -0
  48. package/dist/browser/modules/archive/utils/parse-buffer.js +21 -23
  49. package/dist/browser/modules/archive/utils/pattern-scanner.d.ts +21 -0
  50. package/dist/browser/modules/archive/utils/pattern-scanner.js +27 -0
  51. package/dist/browser/modules/archive/utils/timestamps.js +62 -1
  52. package/dist/browser/modules/archive/utils/zip-extra-fields.d.ts +1 -1
  53. package/dist/browser/modules/archive/utils/zip-extra-fields.js +26 -14
  54. package/dist/browser/modules/archive/zip/index.d.ts +42 -0
  55. package/dist/browser/modules/archive/zip/index.js +157 -0
  56. package/dist/browser/modules/archive/{streaming-zip.d.ts → zip/stream.d.ts} +28 -5
  57. package/dist/browser/modules/archive/{streaming-zip.js → zip/stream.js} +192 -48
  58. package/dist/browser/modules/archive/zip/zip-bytes.d.ts +73 -0
  59. package/dist/browser/modules/archive/zip/zip-bytes.js +239 -0
  60. package/dist/{esm/modules/archive → browser/modules/archive/zip}/zip-entry-metadata.js +3 -3
  61. package/dist/browser/modules/archive/{zip-records.d.ts → zip-spec/zip-records.d.ts} +20 -0
  62. package/dist/browser/modules/archive/zip-spec/zip-records.js +126 -0
  63. package/dist/browser/modules/excel/form-control.d.ts +2 -0
  64. package/dist/browser/modules/excel/form-control.js +54 -16
  65. package/dist/browser/modules/excel/stream/workbook-reader.browser.js +1 -1
  66. package/dist/browser/modules/excel/stream/workbook-writer.browser.d.ts +1 -1
  67. package/dist/browser/modules/excel/stream/workbook-writer.browser.js +1 -1
  68. package/dist/browser/modules/excel/xlsx/xform/sheet/worksheet-xform.js +17 -3
  69. package/dist/browser/modules/excel/xlsx/xlsx.browser.js +3 -6
  70. package/dist/browser/modules/excel/xlsx/xlsx.js +1 -1
  71. package/dist/browser/modules/stream/streams.browser.d.ts +28 -28
  72. package/dist/browser/modules/stream/streams.browser.js +850 -697
  73. package/dist/browser/modules/stream/streams.js +140 -58
  74. package/dist/cjs/modules/archive/{compress.base.js → compression/compress.base.js} +2 -1
  75. package/dist/cjs/modules/archive/{compress.browser.js → compression/compress.browser.js} +18 -19
  76. package/dist/cjs/modules/archive/{compress.js → compression/compress.js} +1 -1
  77. package/dist/cjs/modules/archive/{crc32.js → compression/crc32.js} +1 -1
  78. package/dist/cjs/modules/archive/{deflate-fallback.js → compression/deflate-fallback.js} +1 -1
  79. package/dist/cjs/modules/archive/{streaming-compress.browser.js → compression/streaming-compress.browser.js} +3 -3
  80. package/dist/cjs/modules/archive/{streaming-compress.js → compression/streaming-compress.js} +2 -2
  81. package/dist/cjs/modules/archive/defaults.js +7 -4
  82. package/dist/cjs/modules/archive/index.base.js +9 -19
  83. package/dist/cjs/modules/archive/index.browser.js +4 -10
  84. package/dist/cjs/modules/archive/index.js +4 -8
  85. package/dist/cjs/modules/archive/internal/byte-queue.js +411 -0
  86. package/dist/cjs/modules/archive/io/archive-sink.js +82 -0
  87. package/dist/cjs/modules/archive/io/archive-source.js +114 -0
  88. package/dist/cjs/modules/archive/unzip/index.js +170 -0
  89. package/dist/cjs/modules/archive/unzip/stream.base.js +1044 -0
  90. package/dist/cjs/modules/archive/{parse.browser.js → unzip/stream.browser.js} +377 -111
  91. package/dist/cjs/modules/archive/{parse.js → unzip/stream.js} +9 -8
  92. package/dist/cjs/modules/archive/{zip-parser.js → unzip/zip-parser.js} +47 -33
  93. package/dist/cjs/modules/archive/utils/async-queue.js +106 -0
  94. package/dist/cjs/modules/archive/utils/bytes.js +16 -16
  95. package/dist/cjs/modules/archive/utils/compressibility.js +60 -0
  96. package/dist/cjs/modules/archive/utils/parse-buffer.js +21 -23
  97. package/dist/cjs/modules/archive/utils/pattern-scanner.js +31 -0
  98. package/dist/cjs/modules/archive/utils/timestamps.js +64 -3
  99. package/dist/cjs/modules/archive/utils/zip-extra-fields.js +26 -14
  100. package/dist/cjs/modules/archive/zip/index.js +162 -0
  101. package/dist/cjs/modules/archive/{streaming-zip.js → zip/stream.js} +194 -50
  102. package/dist/cjs/modules/archive/zip/zip-bytes.js +242 -0
  103. package/dist/cjs/modules/archive/{zip-entry-metadata.js → zip/zip-entry-metadata.js} +5 -5
  104. package/dist/cjs/modules/archive/zip-spec/zip-records.js +136 -0
  105. package/dist/cjs/modules/excel/form-control.js +54 -16
  106. package/dist/cjs/modules/excel/stream/workbook-reader.browser.js +2 -2
  107. package/dist/cjs/modules/excel/stream/workbook-writer.browser.js +4 -4
  108. package/dist/cjs/modules/excel/xlsx/xform/sheet/worksheet-xform.js +17 -3
  109. package/dist/cjs/modules/excel/xlsx/xlsx.browser.js +6 -9
  110. package/dist/cjs/modules/excel/xlsx/xlsx.js +2 -2
  111. package/dist/cjs/modules/stream/streams.browser.js +850 -697
  112. package/dist/cjs/modules/stream/streams.js +140 -58
  113. package/dist/esm/index.browser.js +12 -0
  114. package/dist/esm/modules/archive/{compress.base.js → compression/compress.base.js} +2 -1
  115. package/dist/{browser/modules/archive → esm/modules/archive/compression}/compress.browser.js +18 -19
  116. package/dist/esm/modules/archive/{compress.js → compression/compress.js} +1 -1
  117. package/dist/esm/modules/archive/{crc32.js → compression/crc32.js} +1 -1
  118. package/dist/esm/modules/archive/{deflate-fallback.js → compression/deflate-fallback.js} +1 -1
  119. package/dist/esm/modules/archive/{streaming-compress.browser.js → compression/streaming-compress.browser.js} +3 -3
  120. package/dist/esm/modules/archive/{streaming-compress.js → compression/streaming-compress.js} +2 -2
  121. package/dist/esm/modules/archive/defaults.js +6 -3
  122. package/dist/esm/modules/archive/index.base.js +3 -6
  123. package/dist/esm/modules/archive/index.browser.js +3 -7
  124. package/dist/esm/modules/archive/index.js +3 -5
  125. package/dist/esm/modules/archive/internal/byte-queue.js +407 -0
  126. package/dist/esm/modules/archive/io/archive-sink.js +77 -0
  127. package/dist/esm/modules/archive/io/archive-source.js +107 -0
  128. package/dist/esm/modules/archive/unzip/index.js +164 -0
  129. package/dist/esm/modules/archive/unzip/stream.base.js +1022 -0
  130. package/dist/esm/modules/archive/{parse.browser.js → unzip/stream.browser.js} +376 -110
  131. package/dist/{browser/modules/archive/parse.js → esm/modules/archive/unzip/stream.js} +7 -6
  132. package/dist/{browser/modules/archive → esm/modules/archive/unzip}/zip-parser.js +38 -24
  133. package/dist/esm/modules/archive/utils/async-queue.js +103 -0
  134. package/dist/esm/modules/archive/utils/bytes.js +16 -16
  135. package/dist/esm/modules/archive/utils/compressibility.js +57 -0
  136. package/dist/esm/modules/archive/utils/parse-buffer.js +21 -23
  137. package/dist/esm/modules/archive/utils/pattern-scanner.js +27 -0
  138. package/dist/esm/modules/archive/utils/timestamps.js +62 -1
  139. package/dist/esm/modules/archive/utils/zip-extra-fields.js +26 -14
  140. package/dist/esm/modules/archive/zip/index.js +157 -0
  141. package/dist/esm/modules/archive/{streaming-zip.js → zip/stream.js} +192 -48
  142. package/dist/esm/modules/archive/zip/zip-bytes.js +239 -0
  143. package/dist/{browser/modules/archive → esm/modules/archive/zip}/zip-entry-metadata.js +3 -3
  144. package/dist/esm/modules/archive/zip-spec/zip-records.js +126 -0
  145. package/dist/esm/modules/excel/form-control.js +54 -16
  146. package/dist/esm/modules/excel/stream/workbook-reader.browser.js +1 -1
  147. package/dist/esm/modules/excel/stream/workbook-writer.browser.js +1 -1
  148. package/dist/esm/modules/excel/xlsx/xform/sheet/worksheet-xform.js +17 -3
  149. package/dist/esm/modules/excel/xlsx/xlsx.browser.js +3 -6
  150. package/dist/esm/modules/excel/xlsx/xlsx.js +1 -1
  151. package/dist/esm/modules/stream/streams.browser.js +850 -697
  152. package/dist/esm/modules/stream/streams.js +140 -58
  153. package/dist/iife/THIRD_PARTY_NOTICES.md +81 -0
  154. package/dist/iife/excelts.iife.js +4777 -2863
  155. package/dist/iife/excelts.iife.js.map +1 -1
  156. package/dist/iife/excelts.iife.min.js +103 -31
  157. package/dist/types/index.browser.d.ts +1 -0
  158. package/dist/{browser/modules/archive → types/modules/archive/compression}/compress.base.d.ts +1 -0
  159. package/dist/{browser/modules/archive → types/modules/archive/compression}/compress.browser.d.ts +10 -8
  160. package/dist/types/modules/archive/{streaming-compress.browser.d.ts → compression/streaming-compress.browser.d.ts} +1 -1
  161. package/dist/types/modules/archive/defaults.d.ts +1 -0
  162. package/dist/types/modules/archive/index.base.d.ts +4 -4
  163. package/dist/types/modules/archive/index.browser.d.ts +3 -4
  164. package/dist/types/modules/archive/index.d.ts +3 -4
  165. package/dist/types/modules/archive/internal/byte-queue.d.ts +33 -0
  166. package/dist/types/modules/archive/io/archive-sink.d.ts +9 -0
  167. package/dist/types/modules/archive/io/archive-source.d.ts +8 -0
  168. package/dist/types/modules/archive/unzip/index.d.ts +40 -0
  169. package/dist/types/modules/archive/{parse.base.d.ts → unzip/stream.base.d.ts} +60 -5
  170. package/dist/types/modules/archive/{parse.browser.d.ts → unzip/stream.browser.d.ts} +2 -2
  171. package/dist/types/modules/archive/{parse.d.ts → unzip/stream.d.ts} +3 -3
  172. package/dist/{browser/modules/archive → types/modules/archive/unzip}/zip-parser.d.ts +1 -1
  173. package/dist/types/modules/archive/utils/async-queue.d.ts +7 -0
  174. package/dist/types/modules/archive/utils/compressibility.d.ts +10 -0
  175. package/dist/types/modules/archive/utils/pattern-scanner.d.ts +21 -0
  176. package/dist/types/modules/archive/utils/zip-extra-fields.d.ts +1 -1
  177. package/dist/types/modules/archive/zip/index.d.ts +42 -0
  178. package/dist/types/modules/archive/{streaming-zip.d.ts → zip/stream.d.ts} +29 -6
  179. package/dist/types/modules/archive/zip/zip-bytes.d.ts +73 -0
  180. package/dist/types/modules/archive/{zip-entry-metadata.d.ts → zip/zip-entry-metadata.d.ts} +1 -1
  181. package/dist/types/modules/archive/{zip-records.d.ts → zip-spec/zip-records.d.ts} +20 -0
  182. package/dist/types/modules/excel/form-control.d.ts +2 -0
  183. package/dist/types/modules/excel/stream/workbook-writer.browser.d.ts +1 -1
  184. package/dist/types/modules/stream/streams.browser.d.ts +28 -28
  185. package/package.json +10 -6
  186. package/dist/browser/modules/archive/byte-queue.d.ts +0 -18
  187. package/dist/browser/modules/archive/byte-queue.js +0 -125
  188. package/dist/browser/modules/archive/parse.base.js +0 -610
  189. package/dist/browser/modules/archive/utils/zip-extra.d.ts +0 -18
  190. package/dist/browser/modules/archive/utils/zip-extra.js +0 -68
  191. package/dist/browser/modules/archive/zip-builder.d.ts +0 -117
  192. package/dist/browser/modules/archive/zip-builder.js +0 -292
  193. package/dist/browser/modules/archive/zip-constants.d.ts +0 -18
  194. package/dist/browser/modules/archive/zip-constants.js +0 -23
  195. package/dist/browser/modules/archive/zip-records.js +0 -84
  196. package/dist/cjs/modules/archive/byte-queue.js +0 -129
  197. package/dist/cjs/modules/archive/parse.base.js +0 -632
  198. package/dist/cjs/modules/archive/utils/zip-extra.js +0 -74
  199. package/dist/cjs/modules/archive/zip-builder.js +0 -297
  200. package/dist/cjs/modules/archive/zip-constants.js +0 -26
  201. package/dist/cjs/modules/archive/zip-records.js +0 -90
  202. package/dist/esm/modules/archive/byte-queue.js +0 -125
  203. package/dist/esm/modules/archive/parse.base.js +0 -610
  204. package/dist/esm/modules/archive/utils/zip-extra.js +0 -68
  205. package/dist/esm/modules/archive/zip-builder.js +0 -292
  206. package/dist/esm/modules/archive/zip-constants.js +0 -23
  207. package/dist/esm/modules/archive/zip-records.js +0 -84
  208. package/dist/types/modules/archive/byte-queue.d.ts +0 -18
  209. package/dist/types/modules/archive/utils/zip-extra.d.ts +0 -18
  210. package/dist/types/modules/archive/zip-builder.d.ts +0 -117
  211. package/dist/types/modules/archive/zip-constants.d.ts +0 -18
  212. /package/dist/browser/modules/archive/{crc32.base.d.ts → compression/crc32.base.d.ts} +0 -0
  213. /package/dist/browser/modules/archive/{crc32.base.js → compression/crc32.base.js} +0 -0
  214. /package/dist/browser/modules/archive/{crc32.browser.js → compression/crc32.browser.js} +0 -0
  215. /package/dist/browser/modules/archive/{deflate-fallback.d.ts → compression/deflate-fallback.d.ts} +0 -0
  216. /package/dist/browser/modules/archive/{streaming-compress.base.d.ts → compression/streaming-compress.base.d.ts} +0 -0
  217. /package/dist/browser/modules/archive/{streaming-compress.base.js → compression/streaming-compress.base.js} +0 -0
  218. /package/dist/browser/modules/archive/{extract.js → unzip/extract.js} +0 -0
  219. /package/dist/browser/modules/archive/{zip-entry-metadata.d.ts → zip/zip-entry-metadata.d.ts} +0 -0
  220. /package/dist/browser/modules/archive/{zip-entry-info.d.ts → zip-spec/zip-entry-info.d.ts} +0 -0
  221. /package/dist/browser/modules/archive/{zip-entry-info.js → zip-spec/zip-entry-info.js} +0 -0
  222. /package/dist/cjs/modules/archive/{crc32.base.js → compression/crc32.base.js} +0 -0
  223. /package/dist/cjs/modules/archive/{crc32.browser.js → compression/crc32.browser.js} +0 -0
  224. /package/dist/cjs/modules/archive/{streaming-compress.base.js → compression/streaming-compress.base.js} +0 -0
  225. /package/dist/cjs/modules/archive/{extract.js → unzip/extract.js} +0 -0
  226. /package/dist/cjs/modules/archive/{zip-entry-info.js → zip-spec/zip-entry-info.js} +0 -0
  227. /package/dist/esm/modules/archive/{crc32.base.js → compression/crc32.base.js} +0 -0
  228. /package/dist/esm/modules/archive/{crc32.browser.js → compression/crc32.browser.js} +0 -0
  229. /package/dist/esm/modules/archive/{streaming-compress.base.js → compression/streaming-compress.base.js} +0 -0
  230. /package/dist/esm/modules/archive/{extract.js → unzip/extract.js} +0 -0
  231. /package/dist/esm/modules/archive/{zip-entry-info.js → zip-spec/zip-entry-info.js} +0 -0
  232. /package/dist/{LICENSE → iife/LICENSE} +0 -0
  233. /package/dist/types/modules/archive/{compress.d.ts → compression/compress.d.ts} +0 -0
  234. /package/dist/types/modules/archive/{crc32.base.d.ts → compression/crc32.base.d.ts} +0 -0
  235. /package/dist/types/modules/archive/{crc32.browser.d.ts → compression/crc32.browser.d.ts} +0 -0
  236. /package/dist/types/modules/archive/{crc32.d.ts → compression/crc32.d.ts} +0 -0
  237. /package/dist/types/modules/archive/{deflate-fallback.d.ts → compression/deflate-fallback.d.ts} +0 -0
  238. /package/dist/types/modules/archive/{streaming-compress.base.d.ts → compression/streaming-compress.base.d.ts} +0 -0
  239. /package/dist/types/modules/archive/{streaming-compress.d.ts → compression/streaming-compress.d.ts} +0 -0
  240. /package/dist/types/modules/archive/{extract.d.ts → unzip/extract.d.ts} +0 -0
  241. /package/dist/types/modules/archive/{zip-entry-info.d.ts → zip-spec/zip-entry-info.d.ts} +0 -0
@@ -0,0 +1,407 @@
1
+ import { indexOfUint8ArrayPattern } from "../utils/bytes.js";
2
+ export class ByteQueue {
3
+ constructor(initial) {
4
+ // Store data as immutable chunks to avoid copying on append.
5
+ this._chunks = [];
6
+ this._headOffset = 0;
7
+ this._length = 0;
8
+ // Lazily materialized contiguous view (used only by callers that require a single buffer).
9
+ this._cachedView = null;
10
+ this._cachedLength = 0;
11
+ if (initial && initial.length > 0) {
12
+ this.reset(initial);
13
+ }
14
+ }
15
+ get length() {
16
+ return this._length;
17
+ }
18
+ isEmpty() {
19
+ return this.length === 0;
20
+ }
21
+ view() {
22
+ if (this._length === 0) {
23
+ return ByteQueue.EMPTY;
24
+ }
25
+ // Fast path: single chunk.
26
+ if (this._chunks.length === 1) {
27
+ const c = this._chunks[0];
28
+ return c.subarray(this._headOffset, this._headOffset + this._length);
29
+ }
30
+ if (this._cachedView && this._cachedLength === this._length) {
31
+ return this._cachedView;
32
+ }
33
+ const out = new Uint8Array(this._length);
34
+ let offset = 0;
35
+ for (let i = 0; i < this._chunks.length; i++) {
36
+ const c = this._chunks[i];
37
+ const start = i === 0 ? this._headOffset : 0;
38
+ const end = i === this._chunks.length - 1 ? start + (this._length - offset) : c.length;
39
+ out.set(c.subarray(start, end), offset);
40
+ offset += end - start;
41
+ if (offset >= out.length) {
42
+ break;
43
+ }
44
+ }
45
+ this._cachedView = out;
46
+ this._cachedLength = this._length;
47
+ return out;
48
+ }
49
+ reset(data) {
50
+ this._cachedView = null;
51
+ this._cachedLength = 0;
52
+ this._chunks = [];
53
+ this._headOffset = 0;
54
+ this._length = 0;
55
+ if (!data || data.length === 0) {
56
+ return;
57
+ }
58
+ // Keep a private copy to ensure future writes cannot mutate the source.
59
+ const copy = new Uint8Array(data.length);
60
+ copy.set(data);
61
+ this._chunks = [copy];
62
+ this._headOffset = 0;
63
+ this._length = copy.length;
64
+ }
65
+ append(chunk) {
66
+ if (chunk.length === 0) {
67
+ return;
68
+ }
69
+ this._cachedView = null;
70
+ this._cachedLength = 0;
71
+ this._chunks.push(chunk);
72
+ this._length += chunk.length;
73
+ }
74
+ read(length) {
75
+ if (length <= 0) {
76
+ return new Uint8Array(0);
77
+ }
78
+ if (length > this._length) {
79
+ throw new RangeError("ByteQueue: read beyond available data");
80
+ }
81
+ this._cachedView = null;
82
+ this._cachedLength = 0;
83
+ if (this._chunks.length === 1) {
84
+ const c = this._chunks[0];
85
+ const start = this._headOffset;
86
+ const end = start + length;
87
+ const out = c.subarray(start, end);
88
+ this._headOffset = end;
89
+ this._length -= length;
90
+ if (this._length === 0) {
91
+ this._chunks = [];
92
+ this._headOffset = 0;
93
+ }
94
+ else if (this._headOffset >= c.length) {
95
+ this._chunks.shift();
96
+ this._headOffset = 0;
97
+ }
98
+ return out;
99
+ }
100
+ // Slow path: spans multiple chunks, copy into a single output buffer.
101
+ const out = new Uint8Array(length);
102
+ let outOffset = 0;
103
+ let remaining = length;
104
+ while (remaining > 0) {
105
+ const c = this._chunks[0];
106
+ const start = this._headOffset;
107
+ const available = c.length - start;
108
+ const toCopy = Math.min(available, remaining);
109
+ out.set(c.subarray(start, start + toCopy), outOffset);
110
+ outOffset += toCopy;
111
+ remaining -= toCopy;
112
+ this._headOffset += toCopy;
113
+ this._length -= toCopy;
114
+ if (this._headOffset >= c.length) {
115
+ this._chunks.shift();
116
+ this._headOffset = 0;
117
+ }
118
+ }
119
+ if (this._length === 0) {
120
+ this._chunks = [];
121
+ this._headOffset = 0;
122
+ }
123
+ return out;
124
+ }
125
+ /**
126
+ * Return a list of chunk views totaling `length` bytes without consuming.
127
+ *
128
+ * This avoids materializing a contiguous buffer for streaming write paths.
129
+ */
130
+ peekChunks(length) {
131
+ if (length <= 0) {
132
+ return [];
133
+ }
134
+ if (length > this._length) {
135
+ throw new RangeError("ByteQueue: peek beyond available data");
136
+ }
137
+ // Fast path: single chunk.
138
+ if (this._chunks.length === 1) {
139
+ const c = this._chunks[0];
140
+ const start = this._headOffset;
141
+ return [c.subarray(start, start + length)];
142
+ }
143
+ const parts = [];
144
+ let remaining = length;
145
+ for (let i = 0; i < this._chunks.length && remaining > 0; i++) {
146
+ const c = this._chunks[i];
147
+ const start = i === 0 ? this._headOffset : 0;
148
+ const avail = c.length - start;
149
+ if (avail <= 0) {
150
+ continue;
151
+ }
152
+ const toTake = Math.min(avail, remaining);
153
+ parts.push(c.subarray(start, start + toTake));
154
+ remaining -= toTake;
155
+ }
156
+ return parts;
157
+ }
158
+ discard(length) {
159
+ if (length <= 0) {
160
+ return;
161
+ }
162
+ if (length >= this._length) {
163
+ this._chunks = [];
164
+ this._headOffset = 0;
165
+ this._length = 0;
166
+ this._cachedView = null;
167
+ this._cachedLength = 0;
168
+ return;
169
+ }
170
+ this._cachedView = null;
171
+ this._cachedLength = 0;
172
+ let remaining = length;
173
+ while (remaining > 0) {
174
+ const c = this._chunks[0];
175
+ const start = this._headOffset;
176
+ const available = c.length - start;
177
+ const toDrop = Math.min(available, remaining);
178
+ this._headOffset += toDrop;
179
+ this._length -= toDrop;
180
+ remaining -= toDrop;
181
+ if (this._headOffset >= c.length) {
182
+ this._chunks.shift();
183
+ this._headOffset = 0;
184
+ }
185
+ }
186
+ if (this._length === 0) {
187
+ this._chunks = [];
188
+ this._headOffset = 0;
189
+ }
190
+ }
191
+ /**
192
+ * Find the first index of `pattern` within the queue.
193
+ *
194
+ * This avoids materializing a contiguous `view()` for common small patterns
195
+ * (ZIP signatures are typically 2-4 bytes).
196
+ */
197
+ indexOfPattern(pattern, startIndex = 0) {
198
+ const patLen = pattern.length;
199
+ if (patLen === 0) {
200
+ return 0;
201
+ }
202
+ const len = this._length;
203
+ if (patLen > len) {
204
+ return -1;
205
+ }
206
+ let start = startIndex | 0;
207
+ if (start < 0) {
208
+ start = 0;
209
+ }
210
+ if (start > len - patLen) {
211
+ return -1;
212
+ }
213
+ // Fast path: single chunk.
214
+ if (this._chunks.length === 1) {
215
+ const c = this._chunks[0];
216
+ const base = this._headOffset;
217
+ const view = c.subarray(base, base + len);
218
+ // Delegate to native indexOf checks for 1..4 bytes.
219
+ if (patLen === 1) {
220
+ return view.indexOf(pattern[0], start);
221
+ }
222
+ return indexOfUint8ArrayPattern(view, pattern, start);
223
+ }
224
+ // Multi-chunk: optimize only for very common small patterns.
225
+ if (patLen > 4) {
226
+ // Rare: materialize view.
227
+ const v = this.view();
228
+ return indexOfUint8ArrayPattern(v, pattern, start);
229
+ }
230
+ const b0 = pattern[0];
231
+ const b1 = patLen >= 2 ? pattern[1] : 0;
232
+ const b2 = patLen >= 3 ? pattern[2] : 0;
233
+ const b3 = patLen >= 4 ? pattern[3] : 0;
234
+ const chunks = this._chunks;
235
+ const peekByteAcrossChunks = (chunkIndex, absoluteIndex) => {
236
+ let ci = chunkIndex;
237
+ let idx = absoluteIndex;
238
+ while (ci < chunks.length) {
239
+ const c = chunks[ci];
240
+ if (idx < c.length) {
241
+ return c[idx] | 0;
242
+ }
243
+ idx -= c.length;
244
+ ci++;
245
+ }
246
+ return null;
247
+ };
248
+ let globalBase = 0;
249
+ for (let ci = 0; ci < chunks.length; ci++) {
250
+ const c = chunks[ci];
251
+ const chunkOffset = ci === 0 ? this._headOffset : 0;
252
+ const chunkLen = c.length - chunkOffset;
253
+ if (chunkLen <= 0) {
254
+ continue;
255
+ }
256
+ const chunkStartGlobal = globalBase;
257
+ const chunkEndGlobal = chunkStartGlobal + chunkLen;
258
+ // Compute local start for this chunk.
259
+ const localStart = start <= chunkStartGlobal
260
+ ? chunkOffset
261
+ : start >= chunkEndGlobal
262
+ ? c.length
263
+ : chunkOffset + (start - chunkStartGlobal);
264
+ if (localStart > c.length - 1) {
265
+ globalBase += chunkLen;
266
+ continue;
267
+ }
268
+ const lastLocal = c.length - 1;
269
+ let i = c.indexOf(b0, localStart);
270
+ while (i !== -1 && i <= lastLocal) {
271
+ const globalPos = chunkStartGlobal + (i - chunkOffset);
272
+ if (globalPos > len - patLen) {
273
+ return -1;
274
+ }
275
+ if (patLen === 1) {
276
+ return globalPos;
277
+ }
278
+ // Fast path: match stays fully inside the current chunk.
279
+ // Avoid calling peekByte() which walks the chunk list per byte.
280
+ const staysInChunk = i + patLen <= c.length;
281
+ if (staysInChunk) {
282
+ if (c[i + 1] !== b1) {
283
+ i = c.indexOf(b0, i + 1);
284
+ continue;
285
+ }
286
+ if (patLen === 2) {
287
+ return globalPos;
288
+ }
289
+ if (c[i + 2] !== b2) {
290
+ i = c.indexOf(b0, i + 1);
291
+ continue;
292
+ }
293
+ if (patLen === 3) {
294
+ return globalPos;
295
+ }
296
+ if (c[i + 3] !== b3) {
297
+ i = c.indexOf(b0, i + 1);
298
+ continue;
299
+ }
300
+ return globalPos;
301
+ }
302
+ // Slow path: pattern spans chunks.
303
+ const b1v = peekByteAcrossChunks(ci, i + 1);
304
+ if (b1v === null || b1v !== b1) {
305
+ i = c.indexOf(b0, i + 1);
306
+ continue;
307
+ }
308
+ if (patLen === 2) {
309
+ return globalPos;
310
+ }
311
+ const b2v = peekByteAcrossChunks(ci, i + 2);
312
+ if (b2v === null || b2v !== b2) {
313
+ i = c.indexOf(b0, i + 1);
314
+ continue;
315
+ }
316
+ if (patLen === 3) {
317
+ return globalPos;
318
+ }
319
+ const b3v = peekByteAcrossChunks(ci, i + 3);
320
+ if (b3v === null || b3v !== b3) {
321
+ i = c.indexOf(b0, i + 1);
322
+ continue;
323
+ }
324
+ return globalPos;
325
+ }
326
+ globalBase += chunkLen;
327
+ }
328
+ return -1;
329
+ }
330
+ /** Peek a little-endian uint32 at `offset` without consuming bytes. Returns null if not enough bytes. */
331
+ peekUint32LE(offset) {
332
+ const off = offset | 0;
333
+ if (off < 0 || off + 4 > this._length) {
334
+ return null;
335
+ }
336
+ // Try to read contiguously from a single chunk to avoid 4x chunk-walk.
337
+ const chunks = this._chunks;
338
+ let remaining = off;
339
+ for (let i = 0; i < chunks.length; i++) {
340
+ const c = chunks[i];
341
+ const start = i === 0 ? this._headOffset : 0;
342
+ const avail = c.length - start;
343
+ if (remaining < avail) {
344
+ const idx = start + remaining;
345
+ if (idx + 4 <= c.length) {
346
+ const b0 = c[idx] | 0;
347
+ const b1 = c[idx + 1] | 0;
348
+ const b2 = c[idx + 2] | 0;
349
+ const b3 = c[idx + 3] | 0;
350
+ return (b0 | (b1 << 8) | (b2 << 16) | (b3 << 24)) >>> 0;
351
+ }
352
+ // Cross-chunk read (rare): walk forward across chunks once.
353
+ const b0 = c[idx] | 0;
354
+ let b1 = 0;
355
+ let b2 = 0;
356
+ let b3 = 0;
357
+ let ci = i;
358
+ let pos = idx + 1;
359
+ for (let k = 1; k < 4; k++) {
360
+ while (ci < chunks.length) {
361
+ const cc = chunks[ci];
362
+ if (pos < cc.length) {
363
+ const v = cc[pos] | 0;
364
+ if (k === 1) {
365
+ b1 = v;
366
+ }
367
+ else if (k === 2) {
368
+ b2 = v;
369
+ }
370
+ else {
371
+ b3 = v;
372
+ }
373
+ pos++;
374
+ break;
375
+ }
376
+ ci++;
377
+ pos = 0;
378
+ }
379
+ }
380
+ return (b0 | (b1 << 8) | (b2 << 16) | (b3 << 24)) >>> 0;
381
+ }
382
+ remaining -= avail;
383
+ }
384
+ // Should be unreachable due to bounds check above.
385
+ return null;
386
+ }
387
+ /** Peek a single byte at `offset` without consuming bytes. */
388
+ peekByte(offset) {
389
+ const off = offset | 0;
390
+ if (off < 0 || off >= this._length) {
391
+ throw new RangeError("ByteQueue: peek beyond available data");
392
+ }
393
+ let remaining = off;
394
+ for (let i = 0; i < this._chunks.length; i++) {
395
+ const c = this._chunks[i];
396
+ const start = i === 0 ? this._headOffset : 0;
397
+ const avail = c.length - start;
398
+ if (remaining < avail) {
399
+ return c[start + remaining] | 0;
400
+ }
401
+ remaining -= avail;
402
+ }
403
+ // Should be unreachable.
404
+ throw new RangeError("ByteQueue: peek beyond available data");
405
+ }
406
+ }
407
+ ByteQueue.EMPTY = new Uint8Array(0);
@@ -0,0 +1,77 @@
1
+ export function isWritableStream(value) {
2
+ return !!value && typeof value === "object" && typeof value.getWriter === "function";
3
+ }
4
+ function once(emitter, event) {
5
+ return new Promise((resolve, reject) => {
6
+ const onError = (err) => {
7
+ cleanup();
8
+ reject(err instanceof Error ? err : new Error(String(err)));
9
+ };
10
+ const onDone = () => {
11
+ cleanup();
12
+ resolve();
13
+ };
14
+ const cleanup = () => {
15
+ emitter.off?.("error", onError);
16
+ emitter.off?.(event, onDone);
17
+ emitter.removeListener?.("error", onError);
18
+ emitter.removeListener?.(event, onDone);
19
+ };
20
+ emitter.on?.("error", onError);
21
+ emitter.on?.(event, onDone);
22
+ });
23
+ }
24
+ export async function pipeIterableToSink(iterable, sink) {
25
+ if (isWritableStream(sink)) {
26
+ const writer = sink.getWriter();
27
+ try {
28
+ for await (const chunk of iterable) {
29
+ await writer.write(chunk);
30
+ }
31
+ await writer.close();
32
+ }
33
+ finally {
34
+ try {
35
+ writer.releaseLock();
36
+ }
37
+ catch {
38
+ // Ignore
39
+ }
40
+ }
41
+ return;
42
+ }
43
+ // Node-style Writable
44
+ for await (const chunk of iterable) {
45
+ const ok = sink.write(chunk);
46
+ if (ok === false && typeof sink.once === "function") {
47
+ await once(sink, "drain");
48
+ }
49
+ }
50
+ if (typeof sink.end === "function") {
51
+ sink.end();
52
+ }
53
+ if (typeof sink.once === "function") {
54
+ await Promise.race([once(sink, "finish"), once(sink, "close")]);
55
+ }
56
+ }
57
+ export async function collect(iterable) {
58
+ const chunks = [];
59
+ let total = 0;
60
+ for await (const chunk of iterable) {
61
+ chunks.push(chunk);
62
+ total += chunk.length;
63
+ }
64
+ if (chunks.length === 0) {
65
+ return new Uint8Array(0);
66
+ }
67
+ if (chunks.length === 1) {
68
+ return chunks[0];
69
+ }
70
+ const out = new Uint8Array(total);
71
+ let offset = 0;
72
+ for (const chunk of chunks) {
73
+ out.set(chunk, offset);
74
+ offset += chunk.length;
75
+ }
76
+ return out;
77
+ }
@@ -0,0 +1,107 @@
1
+ import { encodeUtf8 } from "../utils/text.js";
2
+ export function isReadableStream(value) {
3
+ return !!value && typeof value === "object" && typeof value.getReader === "function";
4
+ }
5
+ export function isAsyncIterable(value) {
6
+ return (!!value &&
7
+ (typeof value === "object" || typeof value === "function") &&
8
+ typeof value[Symbol.asyncIterator] === "function");
9
+ }
10
+ function normalizeChunk(value) {
11
+ if (!value) {
12
+ return null;
13
+ }
14
+ if (value instanceof Uint8Array) {
15
+ return value.length ? value : null;
16
+ }
17
+ if (typeof value === "string") {
18
+ const bytes = encodeUtf8(value);
19
+ return bytes.length ? bytes : null;
20
+ }
21
+ if (value instanceof ArrayBuffer) {
22
+ return value.byteLength ? new Uint8Array(value) : null;
23
+ }
24
+ if (ArrayBuffer.isView(value)) {
25
+ return value.byteLength
26
+ ? new Uint8Array(value.buffer, value.byteOffset, value.byteLength)
27
+ : null;
28
+ }
29
+ // Best-effort: treat unknown chunk as Uint8Array-like.
30
+ if (typeof value.length === "number" && value.length) {
31
+ return value;
32
+ }
33
+ return null;
34
+ }
35
+ export function toUint8ArraySync(source) {
36
+ if (source instanceof Uint8Array) {
37
+ return source;
38
+ }
39
+ if (typeof source === "string") {
40
+ return encodeUtf8(source);
41
+ }
42
+ return new Uint8Array(source);
43
+ }
44
+ export async function toUint8Array(source) {
45
+ if (source instanceof Uint8Array) {
46
+ return source;
47
+ }
48
+ if (typeof source === "string") {
49
+ return encodeUtf8(source);
50
+ }
51
+ if (source instanceof ArrayBuffer) {
52
+ return new Uint8Array(source);
53
+ }
54
+ const buf = await source.arrayBuffer();
55
+ return new Uint8Array(buf);
56
+ }
57
+ export async function* toAsyncIterable(source) {
58
+ if (source instanceof Uint8Array) {
59
+ yield source;
60
+ return;
61
+ }
62
+ if (typeof source === "string") {
63
+ yield encodeUtf8(source);
64
+ return;
65
+ }
66
+ if (source instanceof ArrayBuffer) {
67
+ yield new Uint8Array(source);
68
+ return;
69
+ }
70
+ if (typeof Blob !== "undefined" && source instanceof Blob) {
71
+ yield await toUint8Array(source);
72
+ return;
73
+ }
74
+ if (isReadableStream(source)) {
75
+ const reader = source.getReader();
76
+ try {
77
+ while (true) {
78
+ const { done, value } = await reader.read();
79
+ if (done) {
80
+ return;
81
+ }
82
+ const chunk = normalizeChunk(value);
83
+ if (chunk) {
84
+ yield chunk;
85
+ }
86
+ }
87
+ }
88
+ finally {
89
+ try {
90
+ reader.releaseLock();
91
+ }
92
+ catch {
93
+ // Ignore
94
+ }
95
+ }
96
+ }
97
+ if (isAsyncIterable(source)) {
98
+ for await (const value of source) {
99
+ const chunk = normalizeChunk(value);
100
+ if (chunk) {
101
+ yield chunk;
102
+ }
103
+ }
104
+ return;
105
+ }
106
+ throw new Error("Unsupported archive source");
107
+ }