@tailglow/cli 0.7.1 → 0.8.1

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.
@@ -0,0 +1,4731 @@
1
+ // @ts-nocheck
2
+ // Generated by scripts/build/import.build.js. Do not edit directly.
3
+ /*! Bundled Papa Parse
4
+ The MIT License (MIT)
5
+
6
+ Copyright (c) 2015 Matthew Holt
7
+
8
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
9
+ this software and associated documentation files (the "Software"), to deal in
10
+ the Software without restriction, including without limitation the rights to
11
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
12
+ the Software, and to permit persons to whom the Software is furnished to do so,
13
+ subject to the following conditions:
14
+
15
+ The above copyright notice and this permission notice shall be included in all
16
+ copies or substantial portions of the Software.
17
+
18
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
20
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
21
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
22
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
+ */
25
+ import { createRequire } from "node:module";
26
+ var __create = Object.create;
27
+ var __getProtoOf = Object.getPrototypeOf;
28
+ var __defProp = Object.defineProperty;
29
+ var __getOwnPropNames = Object.getOwnPropertyNames;
30
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
31
+ function __accessProp(key) {
32
+ return this[key];
33
+ }
34
+ var __toESMCache_node;
35
+ var __toESMCache_esm;
36
+ var __toESM = (mod, isNodeMode, target) => {
37
+ var canCache = mod != null && typeof mod === "object";
38
+ if (canCache) {
39
+ var cache = isNodeMode ? __toESMCache_node ??= new WeakMap : __toESMCache_esm ??= new WeakMap;
40
+ var cached = cache.get(mod);
41
+ if (cached)
42
+ return cached;
43
+ }
44
+ target = mod != null ? __create(__getProtoOf(mod)) : {};
45
+ const to = isNodeMode || !mod || !mod.__esModule || !__hasOwnProp.call(mod, "default") ? __defProp(target, "default", { value: mod, enumerable: true }) : target;
46
+ if (mod && typeof mod === "object" || typeof mod === "function") {
47
+ for (let key of __getOwnPropNames(mod))
48
+ if (!__hasOwnProp.call(to, key))
49
+ __defProp(to, key, {
50
+ get: __accessProp.bind(mod, key),
51
+ enumerable: true
52
+ });
53
+ }
54
+ if (canCache)
55
+ cache.set(mod, to);
56
+ return to;
57
+ };
58
+ var __commonJS = (cb, mod) => () => (mod || cb((mod = { exports: {} }).exports, mod), mod.exports);
59
+ var __require = /* @__PURE__ */ createRequire(import.meta.url);
60
+
61
+ // node_modules/.bun/papaparse@5.7.0/node_modules/papaparse/papaparse.js
62
+ var require_papaparse = __commonJS(function(exports, module) {
63
+ (function(root, factory) {
64
+ if (typeof define === "function" && define.amd) {
65
+ define([], factory);
66
+ } else if (typeof module === "object" && typeof exports !== "undefined") {
67
+ module.exports = factory();
68
+ } else {
69
+ root.Papa = factory();
70
+ }
71
+ })(exports, function moduleFactory() {
72
+ var global = function() {
73
+ if (typeof self !== "undefined") {
74
+ return self;
75
+ }
76
+ if (typeof window !== "undefined") {
77
+ return window;
78
+ }
79
+ if (typeof global !== "undefined") {
80
+ return global;
81
+ }
82
+ return {};
83
+ }();
84
+ function getWorkerBlob() {
85
+ var URL2 = global.URL || global.webkitURL || null;
86
+ var code = moduleFactory.toString();
87
+ return Papa.BLOB_URL || (Papa.BLOB_URL = URL2.createObjectURL(new Blob(["var global = (function() { if (typeof self !== 'undefined') { return self; } if (typeof window !== 'undefined') { return window; } if (typeof global !== 'undefined') { return global; } return {}; })(); global.IS_PAPA_WORKER=true; ", "(", code, ")();"], { type: "text/javascript" })));
88
+ }
89
+ var IS_WORKER = !global.document && !!global.postMessage, IS_PAPA_WORKER = global.IS_PAPA_WORKER || false;
90
+ var workers = {}, workerIdCounter = 0;
91
+ var Papa = {};
92
+ Papa.parse = CsvToJson;
93
+ Papa.unparse = JsonToCsv;
94
+ Papa.RECORD_SEP = String.fromCharCode(30);
95
+ Papa.UNIT_SEP = String.fromCharCode(31);
96
+ Papa.BYTE_ORDER_MARK = "\uFEFF";
97
+ Papa.BAD_DELIMITERS = ["\r", `
98
+ `, '"', Papa.BYTE_ORDER_MARK];
99
+ Papa.WORKERS_SUPPORTED = !IS_WORKER && !!global.Worker;
100
+ Papa.NODE_STREAM_INPUT = 1;
101
+ Papa.LocalChunkSize = 1024 * 1024 * 10;
102
+ Papa.RemoteChunkSize = 1024 * 1024 * 5;
103
+ Papa.DefaultDelimiter = ",";
104
+ Papa.Parser = Parser;
105
+ Papa.ParserHandle = ParserHandle;
106
+ Papa.NetworkStreamer = NetworkStreamer;
107
+ Papa.FileStreamer = FileStreamer;
108
+ Papa.StringStreamer = StringStreamer;
109
+ Papa.ReadableStreamStreamer = ReadableStreamStreamer;
110
+ if (typeof PAPA_BROWSER_CONTEXT === "undefined") {
111
+ Papa.DuplexStreamStreamer = DuplexStreamStreamer;
112
+ }
113
+ if (IS_PAPA_WORKER) {
114
+ global.onmessage = workerThreadReceivedMessage;
115
+ }
116
+ function stripBom(string) {
117
+ if (string.charCodeAt(0) === 65279) {
118
+ return string.slice(1);
119
+ }
120
+ return string;
121
+ }
122
+ function CsvToJson(_input, _config) {
123
+ _config = _config || {};
124
+ var dynamicTyping = _config.dynamicTyping || false;
125
+ if (isFunction(dynamicTyping)) {
126
+ _config.dynamicTypingFunction = dynamicTyping;
127
+ dynamicTyping = {};
128
+ }
129
+ _config.dynamicTyping = dynamicTyping;
130
+ _config.transform = isFunction(_config.transform) ? _config.transform : false;
131
+ if (_config.downloadTimeout !== undefined) {
132
+ var downloadTimeout = parseInt(_config.downloadTimeout);
133
+ if (isNaN(downloadTimeout)) {
134
+ throw new Error("Config downloadTimeout value (" + _config.downloadTimeout + ") not parsable by parseInt(val).");
135
+ }
136
+ _config.downloadTimeout = downloadTimeout;
137
+ }
138
+ if (_config.worker && Papa.WORKERS_SUPPORTED) {
139
+ var w = newWorker();
140
+ w.userStep = _config.step;
141
+ w.userChunk = _config.chunk;
142
+ w.userComplete = _config.complete;
143
+ w.userError = _config.error;
144
+ _config.step = isFunction(_config.step);
145
+ _config.chunk = isFunction(_config.chunk);
146
+ _config.complete = isFunction(_config.complete);
147
+ _config.error = isFunction(_config.error);
148
+ delete _config.worker;
149
+ w.postMessage({
150
+ input: _input,
151
+ config: _config,
152
+ workerId: w.id
153
+ });
154
+ return;
155
+ }
156
+ var streamer = null;
157
+ if (_input === Papa.NODE_STREAM_INPUT && typeof PAPA_BROWSER_CONTEXT === "undefined") {
158
+ streamer = new DuplexStreamStreamer(_config);
159
+ return streamer.getStream();
160
+ } else if (typeof _input === "string") {
161
+ _input = stripBom(_input);
162
+ if (_config.download)
163
+ streamer = new NetworkStreamer(_config);
164
+ else
165
+ streamer = new StringStreamer(_config);
166
+ } else if (_input.readable === true && isFunction(_input.read) && isFunction(_input.on)) {
167
+ streamer = new ReadableStreamStreamer(_config);
168
+ } else if (global.File && _input instanceof File || _input instanceof Object)
169
+ streamer = new FileStreamer(_config);
170
+ return streamer.stream(_input);
171
+ }
172
+ function JsonToCsv(_input, _config) {
173
+ var _quotes = false;
174
+ var _writeHeader = true;
175
+ var _delimiter = ",";
176
+ var _newline = `\r
177
+ `;
178
+ var _quoteChar = '"';
179
+ var _escapedQuote = _quoteChar + _quoteChar;
180
+ var _skipEmptyLines = false;
181
+ var _columns = null;
182
+ var _escapeFormulae = false;
183
+ unpackConfig();
184
+ var quoteCharRegex = new RegExp(escapeRegExp(_quoteChar), "g");
185
+ if (typeof _input === "string")
186
+ _input = JSON.parse(_input);
187
+ if (Array.isArray(_input)) {
188
+ if (!_input.length || Array.isArray(_input[0]))
189
+ return serialize(null, _input, _skipEmptyLines);
190
+ else if (typeof _input[0] === "object")
191
+ return serialize(_columns || Object.keys(_input[0]), _input, _skipEmptyLines);
192
+ } else if (typeof _input === "object") {
193
+ if (typeof _input.data === "string")
194
+ _input.data = JSON.parse(_input.data);
195
+ if (Array.isArray(_input.data)) {
196
+ if (!_input.fields)
197
+ _input.fields = _input.meta && _input.meta.fields || _columns;
198
+ if (!_input.fields)
199
+ _input.fields = Array.isArray(_input.data[0]) ? _input.fields : typeof _input.data[0] === "object" ? Object.keys(_input.data[0]) : [];
200
+ if (!Array.isArray(_input.data[0]) && typeof _input.data[0] !== "object")
201
+ _input.data = [_input.data];
202
+ }
203
+ return serialize(_input.fields || [], _input.data || [], _skipEmptyLines);
204
+ }
205
+ throw new Error("Unable to serialize unrecognized input");
206
+ function unpackConfig() {
207
+ if (typeof _config !== "object")
208
+ return;
209
+ if (typeof _config.delimiter === "string" && !Papa.BAD_DELIMITERS.filter(function(value) {
210
+ return _config.delimiter.indexOf(value) !== -1;
211
+ }).length) {
212
+ _delimiter = _config.delimiter;
213
+ }
214
+ if (typeof _config.quotes === "boolean" || typeof _config.quotes === "function" || Array.isArray(_config.quotes))
215
+ _quotes = _config.quotes;
216
+ if (typeof _config.skipEmptyLines === "boolean" || typeof _config.skipEmptyLines === "string")
217
+ _skipEmptyLines = _config.skipEmptyLines;
218
+ if (typeof _config.newline === "string")
219
+ _newline = _config.newline;
220
+ if (typeof _config.quoteChar === "string") {
221
+ _quoteChar = _config.quoteChar;
222
+ _escapedQuote = _quoteChar + _quoteChar;
223
+ }
224
+ if (typeof _config.header === "boolean")
225
+ _writeHeader = _config.header;
226
+ if (Array.isArray(_config.columns)) {
227
+ if (_config.columns.length === 0)
228
+ throw new Error("Option columns is empty");
229
+ _columns = _config.columns;
230
+ }
231
+ if (_config.escapeChar !== undefined) {
232
+ _escapedQuote = _config.escapeChar + _quoteChar;
233
+ }
234
+ if (_config.escapeFormulae instanceof RegExp) {
235
+ _escapeFormulae = _config.escapeFormulae;
236
+ } else if (typeof _config.escapeFormulae === "boolean" && _config.escapeFormulae) {
237
+ _escapeFormulae = /^[=+\-@\t\r].*$/;
238
+ }
239
+ }
240
+ function serialize(fields, data, skipEmptyLines) {
241
+ var csv = "";
242
+ if (typeof fields === "string")
243
+ fields = JSON.parse(fields);
244
+ if (typeof data === "string")
245
+ data = JSON.parse(data);
246
+ var hasHeader = Array.isArray(fields) && fields.length > 0;
247
+ var dataKeyedByField = !Array.isArray(data[0]);
248
+ if (hasHeader && _writeHeader) {
249
+ for (var i = 0;i < fields.length; i++) {
250
+ if (i > 0)
251
+ csv += _delimiter;
252
+ csv += safe(fields[i], i);
253
+ }
254
+ if (data.length > 0)
255
+ csv += _newline;
256
+ }
257
+ for (var row = 0;row < data.length; row++) {
258
+ var maxCol = hasHeader ? fields.length : data[row].length;
259
+ var emptyLine = false;
260
+ var nullLine = hasHeader ? Object.keys(data[row]).length === 0 : data[row].length === 0;
261
+ if (skipEmptyLines && !hasHeader) {
262
+ emptyLine = skipEmptyLines === "greedy" ? data[row].join("").trim() === "" : data[row].length === 1 && data[row][0].length === 0;
263
+ }
264
+ if (skipEmptyLines === "greedy" && hasHeader) {
265
+ var line = [];
266
+ for (var c = 0;c < maxCol; c++) {
267
+ var cx = dataKeyedByField ? fields[c] : c;
268
+ line.push(data[row][cx]);
269
+ }
270
+ emptyLine = line.join("").trim() === "";
271
+ }
272
+ if (!emptyLine) {
273
+ for (var col = 0;col < maxCol; col++) {
274
+ if (col > 0 && !nullLine)
275
+ csv += _delimiter;
276
+ var colIdx = hasHeader && dataKeyedByField ? fields[col] : col;
277
+ csv += safe(data[row][colIdx], col);
278
+ }
279
+ if (row < data.length - 1 && (!skipEmptyLines || maxCol > 0 && !nullLine)) {
280
+ csv += _newline;
281
+ }
282
+ }
283
+ }
284
+ return csv;
285
+ }
286
+ function safe(str, col) {
287
+ if (typeof str === "undefined" || str === null)
288
+ return "";
289
+ if (str.constructor === Date) {
290
+ if (isNaN(str.getTime()))
291
+ return "";
292
+ return str.toISOString();
293
+ }
294
+ var needsQuotes = false;
295
+ if (_escapeFormulae && typeof str === "string" && _escapeFormulae.test(str)) {
296
+ str = "'" + str;
297
+ needsQuotes = true;
298
+ }
299
+ var strValue = str.toString();
300
+ var escapedQuoteStr = strValue.replace(quoteCharRegex, _escapedQuote);
301
+ needsQuotes = needsQuotes || _quotes === true || typeof _quotes === "function" && _quotes(str, col) || Array.isArray(_quotes) && _quotes[col] || hasAny(escapedQuoteStr, Papa.BAD_DELIMITERS) || escapedQuoteStr.indexOf(_delimiter) > -1 || strValue.indexOf(_quoteChar) > -1 || escapedQuoteStr.charAt(0) === " " || escapedQuoteStr.charAt(escapedQuoteStr.length - 1) === " ";
302
+ return needsQuotes ? _quoteChar + escapedQuoteStr + _quoteChar : escapedQuoteStr;
303
+ }
304
+ function hasAny(str, substrings) {
305
+ for (var i = 0;i < substrings.length; i++)
306
+ if (str.indexOf(substrings[i]) > -1)
307
+ return true;
308
+ return false;
309
+ }
310
+ }
311
+ function ChunkStreamer(config) {
312
+ this._handle = null;
313
+ this._finished = false;
314
+ this._completed = false;
315
+ this._halted = false;
316
+ this._input = null;
317
+ this._baseIndex = 0;
318
+ this._partialLine = "";
319
+ this._rowCount = 0;
320
+ this._start = 0;
321
+ this._nextChunk = null;
322
+ this.isFirstChunk = true;
323
+ this._completeResults = {
324
+ data: [],
325
+ errors: [],
326
+ meta: {}
327
+ };
328
+ replaceConfig.call(this, config);
329
+ this.parseChunk = function(chunk, isFakeChunk) {
330
+ const skipFirstNLines = parseInt(this._config.skipFirstNLines) || 0;
331
+ if (this.isFirstChunk && skipFirstNLines > 0) {
332
+ let _newline = this._config.newline;
333
+ if (!_newline) {
334
+ const quoteChar = this._config.quoteChar || '"';
335
+ _newline = this._handle.guessLineEndings(chunk, quoteChar);
336
+ }
337
+ const splitChunk = chunk.split(_newline);
338
+ chunk = [...splitChunk.slice(skipFirstNLines)].join(_newline);
339
+ }
340
+ if (this.isFirstChunk && isFunction(this._config.beforeFirstChunk)) {
341
+ var modifiedChunk = this._config.beforeFirstChunk(chunk);
342
+ if (modifiedChunk !== undefined)
343
+ chunk = modifiedChunk;
344
+ }
345
+ this.isFirstChunk = false;
346
+ this._halted = false;
347
+ var aggregate = this._partialLine + chunk;
348
+ this._partialLine = "";
349
+ var results = this._handle.parse(aggregate, this._baseIndex, !this._finished);
350
+ if (this._handle.paused() || this._handle.aborted()) {
351
+ this._halted = true;
352
+ return;
353
+ }
354
+ var lastIndex = results.meta.cursor;
355
+ if (!this._finished) {
356
+ this._partialLine = aggregate.substring(lastIndex - this._baseIndex);
357
+ this._baseIndex = lastIndex;
358
+ }
359
+ if (results && results.data)
360
+ this._rowCount += results.data.length;
361
+ var finishedIncludingPreview = this._finished || this._config.preview && this._rowCount >= this._config.preview;
362
+ if (IS_PAPA_WORKER) {
363
+ global.postMessage({
364
+ results,
365
+ workerId: Papa.WORKER_ID,
366
+ finished: finishedIncludingPreview
367
+ });
368
+ } else if (isFunction(this._config.chunk) && !isFakeChunk) {
369
+ this._config.chunk(results, this._handle);
370
+ if (this._handle.paused() || this._handle.aborted()) {
371
+ this._halted = true;
372
+ return;
373
+ }
374
+ results = undefined;
375
+ this._completeResults = undefined;
376
+ }
377
+ if (!this._config.step && !this._config.chunk) {
378
+ this._completeResults.data = this._completeResults.data.concat(results.data);
379
+ this._completeResults.errors = this._completeResults.errors.concat(results.errors);
380
+ this._completeResults.meta = results.meta;
381
+ }
382
+ if (!this._completed && finishedIncludingPreview && isFunction(this._config.complete) && (!results || !results.meta.aborted)) {
383
+ this._config.complete(this._completeResults, this._input);
384
+ this._completed = true;
385
+ }
386
+ if (!finishedIncludingPreview && (!results || !results.meta.paused))
387
+ this._nextChunk();
388
+ return results;
389
+ };
390
+ this._sendError = function(error) {
391
+ if (isFunction(this._config.error))
392
+ this._config.error(error);
393
+ else if (IS_PAPA_WORKER && this._config.error) {
394
+ global.postMessage({
395
+ workerId: Papa.WORKER_ID,
396
+ error,
397
+ finished: false
398
+ });
399
+ }
400
+ };
401
+ function replaceConfig(config) {
402
+ var configCopy = copy(config);
403
+ configCopy.chunkSize = parseInt(configCopy.chunkSize);
404
+ if (!config.step && !config.chunk)
405
+ configCopy.chunkSize = null;
406
+ this._handle = new ParserHandle(configCopy);
407
+ this._handle.streamer = this;
408
+ this._config = configCopy;
409
+ }
410
+ }
411
+ function NetworkStreamer(config) {
412
+ config = config || {};
413
+ if (!config.chunkSize)
414
+ config.chunkSize = Papa.RemoteChunkSize;
415
+ ChunkStreamer.call(this, config);
416
+ var xhr;
417
+ if (IS_WORKER) {
418
+ this._nextChunk = function() {
419
+ this._readChunk();
420
+ this._chunkLoaded();
421
+ };
422
+ } else {
423
+ this._nextChunk = function() {
424
+ this._readChunk();
425
+ };
426
+ }
427
+ this.stream = function(url) {
428
+ this._input = url;
429
+ this._nextChunk();
430
+ };
431
+ this._readChunk = function() {
432
+ if (this._finished) {
433
+ this._chunkLoaded();
434
+ return;
435
+ }
436
+ xhr = new XMLHttpRequest;
437
+ if (this._config.withCredentials) {
438
+ xhr.withCredentials = this._config.withCredentials;
439
+ }
440
+ if (!IS_WORKER) {
441
+ xhr.onload = bindFunction(this._chunkLoaded, this);
442
+ xhr.onerror = bindFunction(this._chunkError, this);
443
+ }
444
+ xhr.ontimeout = bindFunction(this._chunkTimeout, this);
445
+ xhr.open(this._config.downloadRequestBody ? "POST" : "GET", this._input, !IS_WORKER);
446
+ if (this._config.downloadTimeout && !IS_WORKER) {
447
+ xhr.timeout = this._config.downloadTimeout;
448
+ }
449
+ if (this._config.downloadRequestHeaders) {
450
+ var headers = this._config.downloadRequestHeaders;
451
+ for (var headerName in headers) {
452
+ xhr.setRequestHeader(headerName, headers[headerName]);
453
+ }
454
+ }
455
+ if (this._config.chunkSize) {
456
+ var end = this._start + this._config.chunkSize - 1;
457
+ xhr.setRequestHeader("Range", "bytes=" + this._start + "-" + end);
458
+ }
459
+ try {
460
+ xhr.send(this._config.downloadRequestBody);
461
+ } catch (err) {
462
+ this._chunkError(err.message);
463
+ }
464
+ if (IS_WORKER && xhr.status === 0)
465
+ this._chunkError();
466
+ };
467
+ this._chunkLoaded = function() {
468
+ if (xhr.readyState !== 4)
469
+ return;
470
+ if (xhr.status < 200 || xhr.status >= 400) {
471
+ this._chunkError();
472
+ return;
473
+ }
474
+ this._start += this._config.chunkSize ? this._config.chunkSize : xhr.responseText.length;
475
+ this._finished = !this._config.chunkSize || this._start >= getFileSize(xhr);
476
+ this.parseChunk(xhr.responseText);
477
+ };
478
+ this._chunkError = function(errorMessage) {
479
+ var errorText = xhr.statusText || errorMessage;
480
+ this._sendError(new Error(errorText));
481
+ };
482
+ this._chunkTimeout = function() {
483
+ this._chunkError("Request timed out after " + this._config.downloadTimeout + "ms");
484
+ };
485
+ function getFileSize(xhr) {
486
+ var contentRange = xhr.getResponseHeader("Content-Range");
487
+ if (contentRange === null) {
488
+ return -1;
489
+ }
490
+ return parseInt(contentRange.substring(contentRange.lastIndexOf("/") + 1));
491
+ }
492
+ }
493
+ NetworkStreamer.prototype = Object.create(ChunkStreamer.prototype);
494
+ NetworkStreamer.prototype.constructor = NetworkStreamer;
495
+ function FileStreamer(config) {
496
+ config = config || {};
497
+ if (!config.chunkSize)
498
+ config.chunkSize = Papa.LocalChunkSize;
499
+ ChunkStreamer.call(this, config);
500
+ var reader, slice;
501
+ var usingAsyncReader = typeof FileReader !== "undefined";
502
+ this.stream = function(file) {
503
+ this._input = file;
504
+ slice = file.slice || file.webkitSlice || file.mozSlice;
505
+ if (usingAsyncReader) {
506
+ reader = new FileReader;
507
+ reader.onload = bindFunction(this._chunkLoaded, this);
508
+ reader.onerror = bindFunction(this._chunkError, this);
509
+ } else
510
+ reader = new FileReaderSync;
511
+ this._nextChunk();
512
+ };
513
+ this._nextChunk = function() {
514
+ if (!this._finished && (!this._config.preview || this._rowCount < this._config.preview))
515
+ this._readChunk();
516
+ };
517
+ this._readChunk = function() {
518
+ var input = this._input;
519
+ if (this._config.chunkSize) {
520
+ var end = Math.min(this._start + this._config.chunkSize, this._input.size);
521
+ input = slice.call(input, this._start, end);
522
+ }
523
+ var txt = reader.readAsText(input, this._config.encoding);
524
+ if (!usingAsyncReader)
525
+ this._chunkLoaded({ target: { result: txt } });
526
+ };
527
+ this._chunkLoaded = function(event) {
528
+ this._start += this._config.chunkSize;
529
+ this._finished = !this._config.chunkSize || this._start >= this._input.size;
530
+ this.parseChunk(event.target.result);
531
+ };
532
+ this._chunkError = function() {
533
+ this._sendError(reader.error);
534
+ };
535
+ }
536
+ FileStreamer.prototype = Object.create(ChunkStreamer.prototype);
537
+ FileStreamer.prototype.constructor = FileStreamer;
538
+ function StringStreamer(config) {
539
+ config = config || {};
540
+ ChunkStreamer.call(this, config);
541
+ var remaining;
542
+ this.stream = function(s) {
543
+ remaining = s;
544
+ return this._nextChunk();
545
+ };
546
+ this._nextChunk = function() {
547
+ if (this._finished)
548
+ return;
549
+ var size = this._config.chunkSize;
550
+ var chunk;
551
+ if (size) {
552
+ chunk = remaining.substring(0, size);
553
+ remaining = remaining.substring(size);
554
+ } else {
555
+ chunk = remaining;
556
+ remaining = "";
557
+ }
558
+ this._finished = !remaining;
559
+ return this.parseChunk(chunk);
560
+ };
561
+ }
562
+ StringStreamer.prototype = Object.create(StringStreamer.prototype);
563
+ StringStreamer.prototype.constructor = StringStreamer;
564
+ function ReadableStreamStreamer(config) {
565
+ config = config || {};
566
+ ChunkStreamer.call(this, config);
567
+ var queue = [];
568
+ var parseOnData = true;
569
+ var streamHasEnded = false;
570
+ this.pause = function() {
571
+ ChunkStreamer.prototype.pause.apply(this, arguments);
572
+ this._input.pause();
573
+ };
574
+ this.resume = function() {
575
+ ChunkStreamer.prototype.resume.apply(this, arguments);
576
+ this._input.resume();
577
+ };
578
+ this.stream = function(stream) {
579
+ this._input = stream;
580
+ this._input.on("data", this._streamData);
581
+ this._input.on("end", this._streamEnd);
582
+ this._input.on("error", this._streamError);
583
+ };
584
+ this._checkIsFinished = function() {
585
+ if (streamHasEnded && queue.length === 1) {
586
+ this._finished = true;
587
+ }
588
+ };
589
+ this._nextChunk = function() {
590
+ this._checkIsFinished();
591
+ if (queue.length) {
592
+ this.parseChunk(queue.shift());
593
+ } else {
594
+ parseOnData = true;
595
+ }
596
+ };
597
+ this._streamData = bindFunction(function(chunk) {
598
+ try {
599
+ queue.push(typeof chunk === "string" ? chunk : chunk.toString(this._config.encoding));
600
+ if (parseOnData) {
601
+ parseOnData = false;
602
+ this._checkIsFinished();
603
+ this.parseChunk(queue.shift());
604
+ }
605
+ } catch (error) {
606
+ this._streamError(error);
607
+ }
608
+ }, this);
609
+ this._streamError = bindFunction(function(error) {
610
+ this._streamCleanUp();
611
+ this._sendError(error);
612
+ }, this);
613
+ this._streamEnd = bindFunction(function() {
614
+ this._streamCleanUp();
615
+ streamHasEnded = true;
616
+ this._streamData("");
617
+ }, this);
618
+ this._streamCleanUp = bindFunction(function() {
619
+ this._input.removeListener("data", this._streamData);
620
+ this._input.removeListener("end", this._streamEnd);
621
+ this._input.removeListener("error", this._streamError);
622
+ }, this);
623
+ }
624
+ ReadableStreamStreamer.prototype = Object.create(ChunkStreamer.prototype);
625
+ ReadableStreamStreamer.prototype.constructor = ReadableStreamStreamer;
626
+ function DuplexStreamStreamer(_config) {
627
+ var Duplex = __require("stream").Duplex;
628
+ var config = copy(_config);
629
+ var parseOnWrite = true;
630
+ var writeStreamHasFinished = false;
631
+ var parseCallbackQueue = [];
632
+ var stream = null;
633
+ this._onCsvData = function(results) {
634
+ var data = results.data;
635
+ if (!stream.push(data) && !this._handle.paused()) {
636
+ this._handle.pause();
637
+ }
638
+ };
639
+ this._onCsvComplete = function() {
640
+ stream.push(null);
641
+ };
642
+ config.step = bindFunction(this._onCsvData, this);
643
+ config.complete = bindFunction(this._onCsvComplete, this);
644
+ ChunkStreamer.call(this, config);
645
+ this._nextChunk = function() {
646
+ if (writeStreamHasFinished && parseCallbackQueue.length === 1) {
647
+ this._finished = true;
648
+ }
649
+ if (parseCallbackQueue.length) {
650
+ parseCallbackQueue.shift()();
651
+ } else {
652
+ parseOnWrite = true;
653
+ }
654
+ };
655
+ this._addToParseQueue = function(chunk, callback) {
656
+ parseCallbackQueue.push(bindFunction(function() {
657
+ this.parseChunk(typeof chunk === "string" ? chunk : chunk.toString(config.encoding));
658
+ if (isFunction(callback)) {
659
+ return callback();
660
+ }
661
+ }, this));
662
+ if (parseOnWrite) {
663
+ parseOnWrite = false;
664
+ this._nextChunk();
665
+ }
666
+ };
667
+ this._onRead = function() {
668
+ if (this._handle.paused()) {
669
+ this._handle.resume();
670
+ }
671
+ };
672
+ this._onWrite = function(chunk, encoding, callback) {
673
+ this._addToParseQueue(chunk, callback);
674
+ };
675
+ this._onWriteComplete = function() {
676
+ writeStreamHasFinished = true;
677
+ this._addToParseQueue("");
678
+ };
679
+ this.getStream = function() {
680
+ return stream;
681
+ };
682
+ stream = new Duplex({
683
+ readableObjectMode: true,
684
+ decodeStrings: false,
685
+ read: bindFunction(this._onRead, this),
686
+ write: bindFunction(this._onWrite, this)
687
+ });
688
+ stream.once("finish", bindFunction(this._onWriteComplete, this));
689
+ }
690
+ if (typeof PAPA_BROWSER_CONTEXT === "undefined") {
691
+ DuplexStreamStreamer.prototype = Object.create(ChunkStreamer.prototype);
692
+ DuplexStreamStreamer.prototype.constructor = DuplexStreamStreamer;
693
+ }
694
+ function ParserHandle(_config) {
695
+ var MAX_FLOAT = Math.pow(2, 53);
696
+ var MIN_FLOAT = -MAX_FLOAT;
697
+ var FLOAT = /^\s*-?(\d+\.?|\.\d+|\d+\.\d+)([eE][-+]?\d+)?\s*$/;
698
+ var ISO_DATE = /^((\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d\.\d+([+-][0-2]\d:[0-5]\d|Z))|(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d([+-][0-2]\d:[0-5]\d|Z))|(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d([+-][0-2]\d:[0-5]\d|Z)))$/;
699
+ var self2 = this;
700
+ var _stepCounter = 0;
701
+ var _rowCounter = 0;
702
+ var _input;
703
+ var _parser;
704
+ var _paused = false;
705
+ var _aborted = false;
706
+ var _delimiterError;
707
+ var _fields = [];
708
+ var _results = {
709
+ data: [],
710
+ errors: [],
711
+ meta: {}
712
+ };
713
+ if (isFunction(_config.step)) {
714
+ var userStep = _config.step;
715
+ _config.step = function(results) {
716
+ _results = results;
717
+ if (needsHeaderRow())
718
+ processResults();
719
+ else {
720
+ processResults();
721
+ if (_results.data.length === 0)
722
+ return;
723
+ _stepCounter += results.data.length;
724
+ if (_config.preview && _stepCounter > _config.preview)
725
+ _parser.abort();
726
+ else {
727
+ _results.data = _results.data[0];
728
+ userStep(_results, self2);
729
+ }
730
+ }
731
+ };
732
+ }
733
+ this.parse = function(input, baseIndex, ignoreLastRow) {
734
+ var quoteChar = _config.quoteChar || '"';
735
+ if (!_config.newline)
736
+ _config.newline = this.guessLineEndings(input, quoteChar);
737
+ _delimiterError = false;
738
+ if (!_config.delimiter) {
739
+ var delimGuess = guessDelimiter(input, _config.newline, _config.skipEmptyLines, _config.comments, _config.delimitersToGuess);
740
+ if (delimGuess.successful)
741
+ _config.delimiter = delimGuess.bestDelimiter;
742
+ else {
743
+ _delimiterError = true;
744
+ _config.delimiter = Papa.DefaultDelimiter;
745
+ }
746
+ _results.meta.delimiter = _config.delimiter;
747
+ } else if (isFunction(_config.delimiter)) {
748
+ _config.delimiter = _config.delimiter(input);
749
+ _results.meta.delimiter = _config.delimiter;
750
+ }
751
+ var parserConfig = copy(_config);
752
+ parserConfig.header = needsHeaderRow();
753
+ if (_config.preview && _config.header)
754
+ parserConfig.preview++;
755
+ _input = input;
756
+ _parser = new Parser(parserConfig);
757
+ _results = _parser.parse(_input, baseIndex, ignoreLastRow);
758
+ processResults();
759
+ return _paused ? { meta: { paused: true } } : _results || { meta: { paused: false } };
760
+ };
761
+ this.paused = function() {
762
+ return _paused;
763
+ };
764
+ this.pause = function() {
765
+ _paused = true;
766
+ _parser.abort();
767
+ _input = isFunction(_config.chunk) ? "" : _input.substring(_parser.getCharIndex());
768
+ };
769
+ this.resume = function() {
770
+ if (self2.streamer._halted) {
771
+ _paused = false;
772
+ self2.streamer.parseChunk(_input, true);
773
+ } else {
774
+ setTimeout(self2.resume, 3);
775
+ }
776
+ };
777
+ this.aborted = function() {
778
+ return _aborted;
779
+ };
780
+ this.abort = function() {
781
+ _aborted = true;
782
+ _parser.abort();
783
+ _results.meta.aborted = true;
784
+ if (isFunction(_config.complete))
785
+ _config.complete(_results);
786
+ _input = "";
787
+ };
788
+ this.guessLineEndings = function(input, quoteChar) {
789
+ input = input.substring(0, 1024 * 1024);
790
+ var re = new RegExp(escapeRegExp(quoteChar) + "([^]*?)" + escapeRegExp(quoteChar), "gm");
791
+ input = input.replace(re, "");
792
+ var r = input.split("\r");
793
+ var n = input.split(`
794
+ `);
795
+ var nAppearsFirst = n.length > 1 && n[0].length < r[0].length;
796
+ if (r.length === 1 || nAppearsFirst)
797
+ return `
798
+ `;
799
+ var numWithN = 0;
800
+ for (var i = 0;i < r.length; i++) {
801
+ if (r[i][0] === `
802
+ `)
803
+ numWithN++;
804
+ }
805
+ return numWithN >= r.length / 2 ? `\r
806
+ ` : "\r";
807
+ };
808
+ function testEmptyLine(s) {
809
+ return _config.skipEmptyLines === "greedy" ? s.join("").trim() === "" : s.length === 1 && s[0].length === 0;
810
+ }
811
+ function testFloat(s) {
812
+ if (FLOAT.test(s)) {
813
+ var floatValue = parseFloat(s);
814
+ if (floatValue > MIN_FLOAT && floatValue < MAX_FLOAT) {
815
+ return true;
816
+ }
817
+ }
818
+ return false;
819
+ }
820
+ function processResults() {
821
+ if (_results && _delimiterError) {
822
+ addError("Delimiter", "UndetectableDelimiter", "Unable to auto-detect delimiting character; defaulted to '" + Papa.DefaultDelimiter + "'");
823
+ _delimiterError = false;
824
+ }
825
+ if (_config.skipEmptyLines) {
826
+ _results.data = _results.data.filter(function(d) {
827
+ return !testEmptyLine(d);
828
+ });
829
+ }
830
+ if (needsHeaderRow())
831
+ fillHeaderFields();
832
+ return applyHeaderAndDynamicTypingAndTransformation();
833
+ }
834
+ function needsHeaderRow() {
835
+ return _config.header && _fields.length === 0;
836
+ }
837
+ function fillHeaderFields() {
838
+ if (!_results)
839
+ return;
840
+ function addHeader(header) {
841
+ _fields.push(header);
842
+ }
843
+ if (Array.isArray(_results.data[0])) {
844
+ for (var i = 0;needsHeaderRow() && i < _results.data.length; i++)
845
+ _results.data[i].forEach(addHeader);
846
+ _results.data.splice(0, 1);
847
+ } else
848
+ _results.data.forEach(addHeader);
849
+ }
850
+ function shouldApplyDynamicTyping(field) {
851
+ if (_config.dynamicTypingFunction && _config.dynamicTyping[field] === undefined) {
852
+ _config.dynamicTyping[field] = _config.dynamicTypingFunction(field);
853
+ }
854
+ return (_config.dynamicTyping[field] || _config.dynamicTyping) === true;
855
+ }
856
+ function parseDynamic(field, value) {
857
+ if (shouldApplyDynamicTyping(field)) {
858
+ if (value === "true" || value === "TRUE")
859
+ return true;
860
+ else if (value === "false" || value === "FALSE")
861
+ return false;
862
+ else if (testFloat(value))
863
+ return parseFloat(value);
864
+ else if (ISO_DATE.test(value))
865
+ return new Date(value);
866
+ else
867
+ return value === "" ? null : value;
868
+ }
869
+ return value;
870
+ }
871
+ function applyHeaderAndDynamicTypingAndTransformation() {
872
+ if (!_results || !_config.header && !_config.dynamicTyping && !_config.transform)
873
+ return _results;
874
+ function processRow(rowSource, i) {
875
+ var row = _config.header ? {} : [];
876
+ var j;
877
+ for (j = 0;j < rowSource.length; j++) {
878
+ var field = j;
879
+ var value = rowSource[j];
880
+ if (_config.header)
881
+ field = j >= _fields.length ? "__parsed_extra" : _fields[j];
882
+ if (_config.transform)
883
+ value = _config.transform(value, field);
884
+ value = parseDynamic(field, value);
885
+ if (field === "__parsed_extra") {
886
+ row[field] = row[field] || [];
887
+ row[field].push(value);
888
+ } else
889
+ row[field] = value;
890
+ }
891
+ if (_config.header) {
892
+ if (j > _fields.length)
893
+ addError("FieldMismatch", "TooManyFields", "Too many fields: expected " + _fields.length + " fields but parsed " + j, _rowCounter + i);
894
+ else if (j < _fields.length)
895
+ addError("FieldMismatch", "TooFewFields", "Too few fields: expected " + _fields.length + " fields but parsed " + j, _rowCounter + i);
896
+ }
897
+ return row;
898
+ }
899
+ var incrementBy = 1;
900
+ if (!_results.data.length || Array.isArray(_results.data[0])) {
901
+ _results.data = _results.data.map(processRow);
902
+ incrementBy = _results.data.length;
903
+ } else
904
+ _results.data = processRow(_results.data, 0);
905
+ if (_config.header && _results.meta)
906
+ _results.meta.fields = _fields;
907
+ _rowCounter += incrementBy;
908
+ return _results;
909
+ }
910
+ function guessDelimiter(input, newline, skipEmptyLines, comments, delimitersToGuess) {
911
+ var bestDelim, bestDelta, fieldCountPrevRow, maxFieldCount;
912
+ delimitersToGuess = delimitersToGuess || [",", "\t", "|", ";", Papa.RECORD_SEP, Papa.UNIT_SEP];
913
+ for (var i = 0;i < delimitersToGuess.length; i++) {
914
+ var delim = delimitersToGuess[i];
915
+ var delta = 0, avgFieldCount = 0, emptyLinesCount = 0;
916
+ fieldCountPrevRow = undefined;
917
+ var preview = new Parser({
918
+ comments,
919
+ delimiter: delim,
920
+ newline,
921
+ preview: 10
922
+ }).parse(input);
923
+ for (var j = 0;j < preview.data.length; j++) {
924
+ if (skipEmptyLines && testEmptyLine(preview.data[j])) {
925
+ emptyLinesCount++;
926
+ continue;
927
+ }
928
+ var fieldCount = preview.data[j].length;
929
+ avgFieldCount += fieldCount;
930
+ if (typeof fieldCountPrevRow === "undefined") {
931
+ fieldCountPrevRow = fieldCount;
932
+ continue;
933
+ } else if (fieldCount > 0) {
934
+ delta += Math.abs(fieldCount - fieldCountPrevRow);
935
+ fieldCountPrevRow = fieldCount;
936
+ }
937
+ }
938
+ if (preview.data.length > 0)
939
+ avgFieldCount /= preview.data.length - emptyLinesCount;
940
+ if (avgFieldCount > 1.99 && (typeof bestDelta === "undefined" || delta < bestDelta || delta === bestDelta && avgFieldCount > maxFieldCount)) {
941
+ bestDelta = delta;
942
+ bestDelim = delim;
943
+ maxFieldCount = avgFieldCount;
944
+ }
945
+ }
946
+ _config.delimiter = bestDelim;
947
+ return {
948
+ successful: !!bestDelim,
949
+ bestDelimiter: bestDelim
950
+ };
951
+ }
952
+ function addError(type, code, msg, row) {
953
+ var error = {
954
+ type,
955
+ code,
956
+ message: msg
957
+ };
958
+ if (row !== undefined) {
959
+ error.row = row;
960
+ }
961
+ _results.errors.push(error);
962
+ }
963
+ }
964
+ function escapeRegExp(string) {
965
+ return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
966
+ }
967
+ function Parser(config) {
968
+ config = config || {};
969
+ var delim = config.delimiter;
970
+ var newline = config.newline;
971
+ var comments = config.comments;
972
+ var step = config.step;
973
+ var preview = config.preview;
974
+ var fastMode = config.fastMode;
975
+ var quoteChar;
976
+ var renamedHeaders = null;
977
+ var headerParsed = false;
978
+ if (config.quoteChar === undefined || config.quoteChar === null) {
979
+ quoteChar = '"';
980
+ } else {
981
+ quoteChar = config.quoteChar;
982
+ }
983
+ var escapeChar = quoteChar;
984
+ if (config.escapeChar !== undefined) {
985
+ escapeChar = config.escapeChar;
986
+ }
987
+ if (typeof delim !== "string" || Papa.BAD_DELIMITERS.indexOf(delim) > -1)
988
+ delim = ",";
989
+ if (comments === delim)
990
+ throw new Error("Comment character same as delimiter");
991
+ else if (comments === true)
992
+ comments = "#";
993
+ else if (typeof comments !== "string" || Papa.BAD_DELIMITERS.indexOf(comments) > -1)
994
+ comments = false;
995
+ if (newline !== `
996
+ ` && newline !== "\r" && newline !== `\r
997
+ `)
998
+ newline = `
999
+ `;
1000
+ var cursor = 0;
1001
+ var aborted = false;
1002
+ this.parse = function(input, baseIndex, ignoreLastRow) {
1003
+ if (typeof input !== "string")
1004
+ throw new Error("Input must be a string");
1005
+ var inputLen = input.length, delimLen = delim.length, newlineLen = newline.length, commentsLen = comments.length;
1006
+ var stepIsFunction = isFunction(step);
1007
+ cursor = 0;
1008
+ var data = [], errors = [], row = [], lastCursor = 0;
1009
+ if (!input)
1010
+ return returnable();
1011
+ if (fastMode || fastMode !== false && input.indexOf(quoteChar) === -1) {
1012
+ var rows = input.split(newline);
1013
+ for (var i = 0;i < rows.length; i++) {
1014
+ row = rows[i];
1015
+ cursor += row.length;
1016
+ if (i !== rows.length - 1)
1017
+ cursor += newline.length;
1018
+ else if (ignoreLastRow)
1019
+ return returnable();
1020
+ if (comments && row.substring(0, commentsLen) === comments)
1021
+ continue;
1022
+ if (stepIsFunction) {
1023
+ data = [];
1024
+ pushRow(row.split(delim));
1025
+ doStep();
1026
+ if (aborted)
1027
+ return returnable();
1028
+ } else
1029
+ pushRow(row.split(delim));
1030
+ if (preview && i >= preview) {
1031
+ data = data.slice(0, preview);
1032
+ return returnable(true);
1033
+ }
1034
+ }
1035
+ return returnable();
1036
+ }
1037
+ var nextDelim = input.indexOf(delim, cursor);
1038
+ var nextNewline = input.indexOf(newline, cursor);
1039
+ var quoteCharRegex = new RegExp(escapeRegExp(escapeChar) + escapeRegExp(quoteChar), "g");
1040
+ var quoteSearch = input.indexOf(quoteChar, cursor);
1041
+ for (;; ) {
1042
+ if (input[cursor] === quoteChar) {
1043
+ quoteSearch = cursor;
1044
+ cursor++;
1045
+ for (;; ) {
1046
+ quoteSearch = input.indexOf(quoteChar, quoteSearch + 1);
1047
+ if (quoteSearch === -1) {
1048
+ if (!ignoreLastRow) {
1049
+ errors.push({
1050
+ type: "Quotes",
1051
+ code: "MissingQuotes",
1052
+ message: "Quoted field unterminated",
1053
+ row: data.length,
1054
+ index: cursor
1055
+ });
1056
+ }
1057
+ return finish();
1058
+ }
1059
+ if (quoteSearch === inputLen - 1) {
1060
+ var value = input.substring(cursor, quoteSearch).replace(quoteCharRegex, quoteChar);
1061
+ return finish(value);
1062
+ }
1063
+ if (quoteChar === escapeChar && input[quoteSearch + 1] === escapeChar) {
1064
+ quoteSearch++;
1065
+ continue;
1066
+ }
1067
+ if (quoteChar !== escapeChar && quoteSearch !== 0 && input[quoteSearch - 1] === escapeChar) {
1068
+ continue;
1069
+ }
1070
+ if (nextDelim !== -1 && nextDelim < quoteSearch + 1) {
1071
+ nextDelim = input.indexOf(delim, quoteSearch + 1);
1072
+ }
1073
+ if (nextNewline !== -1 && nextNewline < quoteSearch + 1) {
1074
+ nextNewline = input.indexOf(newline, quoteSearch + 1);
1075
+ }
1076
+ var checkUpTo = nextNewline === -1 ? nextDelim : Math.min(nextDelim, nextNewline);
1077
+ var spacesBetweenQuoteAndDelimiter = extraSpaces(checkUpTo);
1078
+ if (input.substr(quoteSearch + 1 + spacesBetweenQuoteAndDelimiter, delimLen) === delim) {
1079
+ row.push(input.substring(cursor, quoteSearch).replace(quoteCharRegex, quoteChar));
1080
+ cursor = quoteSearch + 1 + spacesBetweenQuoteAndDelimiter + delimLen;
1081
+ if (input[quoteSearch + 1 + spacesBetweenQuoteAndDelimiter + delimLen] !== quoteChar) {
1082
+ quoteSearch = input.indexOf(quoteChar, cursor);
1083
+ }
1084
+ nextDelim = input.indexOf(delim, cursor);
1085
+ nextNewline = input.indexOf(newline, cursor);
1086
+ break;
1087
+ }
1088
+ var spacesBetweenQuoteAndNewLine = extraSpaces(nextNewline);
1089
+ if (input.substring(quoteSearch + 1 + spacesBetweenQuoteAndNewLine, quoteSearch + 1 + spacesBetweenQuoteAndNewLine + newlineLen) === newline) {
1090
+ row.push(input.substring(cursor, quoteSearch).replace(quoteCharRegex, quoteChar));
1091
+ saveRow(quoteSearch + 1 + spacesBetweenQuoteAndNewLine + newlineLen);
1092
+ nextDelim = input.indexOf(delim, cursor);
1093
+ quoteSearch = input.indexOf(quoteChar, cursor);
1094
+ if (stepIsFunction) {
1095
+ doStep();
1096
+ if (aborted)
1097
+ return returnable();
1098
+ }
1099
+ if (preview && data.length >= preview)
1100
+ return returnable(true);
1101
+ break;
1102
+ }
1103
+ errors.push({
1104
+ type: "Quotes",
1105
+ code: "InvalidQuotes",
1106
+ message: "Trailing quote on quoted field is malformed",
1107
+ row: data.length,
1108
+ index: cursor
1109
+ });
1110
+ quoteSearch++;
1111
+ continue;
1112
+ }
1113
+ continue;
1114
+ }
1115
+ if (comments && row.length === 0 && input.substring(cursor, cursor + commentsLen) === comments) {
1116
+ if (nextNewline === -1)
1117
+ return returnable();
1118
+ cursor = nextNewline + newlineLen;
1119
+ nextNewline = input.indexOf(newline, cursor);
1120
+ nextDelim = input.indexOf(delim, cursor);
1121
+ continue;
1122
+ }
1123
+ if (nextDelim !== -1 && (nextDelim < nextNewline || nextNewline === -1)) {
1124
+ row.push(input.substring(cursor, nextDelim));
1125
+ cursor = nextDelim + delimLen;
1126
+ nextDelim = input.indexOf(delim, cursor);
1127
+ continue;
1128
+ }
1129
+ if (nextNewline !== -1) {
1130
+ row.push(input.substring(cursor, nextNewline));
1131
+ saveRow(nextNewline + newlineLen);
1132
+ if (stepIsFunction) {
1133
+ doStep();
1134
+ if (aborted)
1135
+ return returnable();
1136
+ }
1137
+ if (preview && data.length >= preview)
1138
+ return returnable(true);
1139
+ continue;
1140
+ }
1141
+ break;
1142
+ }
1143
+ return finish();
1144
+ function pushRow(row) {
1145
+ data.push(row);
1146
+ lastCursor = cursor;
1147
+ }
1148
+ function extraSpaces(index) {
1149
+ var spaceLength = 0;
1150
+ if (index !== -1) {
1151
+ var textBetweenClosingQuoteAndIndex = input.substring(quoteSearch + 1, index);
1152
+ if (textBetweenClosingQuoteAndIndex && textBetweenClosingQuoteAndIndex.trim() === "") {
1153
+ spaceLength = textBetweenClosingQuoteAndIndex.length;
1154
+ }
1155
+ }
1156
+ return spaceLength;
1157
+ }
1158
+ function finish(value) {
1159
+ if (ignoreLastRow)
1160
+ return returnable();
1161
+ if (typeof value === "undefined")
1162
+ value = input.substring(cursor);
1163
+ row.push(value);
1164
+ cursor = inputLen;
1165
+ pushRow(row);
1166
+ if (stepIsFunction)
1167
+ doStep();
1168
+ return returnable();
1169
+ }
1170
+ function saveRow(newCursor) {
1171
+ cursor = newCursor;
1172
+ pushRow(row);
1173
+ row = [];
1174
+ nextNewline = input.indexOf(newline, cursor);
1175
+ }
1176
+ function returnable(stopped) {
1177
+ if (config.header && !baseIndex && data.length && !headerParsed) {
1178
+ const result = data[0];
1179
+ const headerCount = Object.create(null);
1180
+ const usedHeaders = new Set(result);
1181
+ let duplicateHeaders = false;
1182
+ for (let i = 0;i < result.length; i++) {
1183
+ let header = stripBom(result[i]);
1184
+ if (isFunction(config.transformHeader))
1185
+ header = config.transformHeader(header, i);
1186
+ if (!headerCount[header]) {
1187
+ headerCount[header] = 1;
1188
+ result[i] = header;
1189
+ } else {
1190
+ let newHeader;
1191
+ let suffixCount = headerCount[header];
1192
+ do {
1193
+ newHeader = `${header}_${suffixCount}`;
1194
+ suffixCount++;
1195
+ } while (usedHeaders.has(newHeader));
1196
+ usedHeaders.add(newHeader);
1197
+ result[i] = newHeader;
1198
+ headerCount[header]++;
1199
+ duplicateHeaders = true;
1200
+ if (renamedHeaders === null) {
1201
+ renamedHeaders = {};
1202
+ }
1203
+ renamedHeaders[newHeader] = header;
1204
+ }
1205
+ usedHeaders.add(header);
1206
+ }
1207
+ if (duplicateHeaders) {
1208
+ console.warn("Duplicate headers found and renamed.");
1209
+ }
1210
+ headerParsed = true;
1211
+ }
1212
+ return {
1213
+ data,
1214
+ errors,
1215
+ meta: {
1216
+ delimiter: delim,
1217
+ linebreak: newline,
1218
+ aborted,
1219
+ truncated: !!stopped,
1220
+ cursor: lastCursor + (baseIndex || 0),
1221
+ renamedHeaders
1222
+ }
1223
+ };
1224
+ }
1225
+ function doStep() {
1226
+ step(returnable());
1227
+ data = [];
1228
+ errors = [];
1229
+ }
1230
+ };
1231
+ this.abort = function() {
1232
+ aborted = true;
1233
+ };
1234
+ this.getCharIndex = function() {
1235
+ return cursor;
1236
+ };
1237
+ }
1238
+ function newWorker() {
1239
+ if (!Papa.WORKERS_SUPPORTED)
1240
+ return false;
1241
+ var workerUrl = getWorkerBlob();
1242
+ var w = new global.Worker(workerUrl);
1243
+ w.onmessage = mainThreadReceivedMessage;
1244
+ w.id = workerIdCounter++;
1245
+ workers[w.id] = w;
1246
+ return w;
1247
+ }
1248
+ function mainThreadReceivedMessage(e) {
1249
+ var msg = e.data;
1250
+ var worker = workers[msg.workerId];
1251
+ var aborted = false;
1252
+ if (msg.error)
1253
+ worker.userError(msg.error, msg.file);
1254
+ else if (msg.results && msg.results.data) {
1255
+ var abort = function() {
1256
+ aborted = true;
1257
+ completeWorker(msg.workerId, { data: [], errors: [], meta: { aborted: true } });
1258
+ };
1259
+ var handle = {
1260
+ abort,
1261
+ pause: notImplemented,
1262
+ resume: notImplemented
1263
+ };
1264
+ if (isFunction(worker.userStep)) {
1265
+ for (var i = 0;i < msg.results.data.length; i++) {
1266
+ worker.userStep({
1267
+ data: msg.results.data[i],
1268
+ errors: msg.results.errors,
1269
+ meta: msg.results.meta
1270
+ }, handle);
1271
+ if (aborted)
1272
+ break;
1273
+ }
1274
+ delete msg.results;
1275
+ } else if (isFunction(worker.userChunk)) {
1276
+ worker.userChunk(msg.results, handle, msg.file);
1277
+ delete msg.results;
1278
+ }
1279
+ }
1280
+ if (msg.finished && !aborted)
1281
+ completeWorker(msg.workerId, msg.results);
1282
+ }
1283
+ function completeWorker(workerId, results) {
1284
+ var worker = workers[workerId];
1285
+ if (isFunction(worker.userComplete))
1286
+ worker.userComplete(results);
1287
+ worker.terminate();
1288
+ delete workers[workerId];
1289
+ }
1290
+ function notImplemented() {
1291
+ throw new Error("Not implemented.");
1292
+ }
1293
+ function workerThreadReceivedMessage(e) {
1294
+ var msg = e.data;
1295
+ if (typeof Papa.WORKER_ID === "undefined" && msg)
1296
+ Papa.WORKER_ID = msg.workerId;
1297
+ if (typeof msg.input === "string") {
1298
+ global.postMessage({
1299
+ workerId: Papa.WORKER_ID,
1300
+ results: Papa.parse(msg.input, msg.config),
1301
+ finished: true
1302
+ });
1303
+ } else if (global.File && msg.input instanceof File || msg.input instanceof Object) {
1304
+ var results = Papa.parse(msg.input, msg.config);
1305
+ if (results)
1306
+ global.postMessage({
1307
+ workerId: Papa.WORKER_ID,
1308
+ results,
1309
+ finished: true
1310
+ });
1311
+ }
1312
+ }
1313
+ function copy(obj) {
1314
+ if (typeof obj !== "object" || obj === null)
1315
+ return obj;
1316
+ var cpy = Array.isArray(obj) ? [] : {};
1317
+ for (var key in obj)
1318
+ cpy[key] = copy(obj[key]);
1319
+ return cpy;
1320
+ }
1321
+ function bindFunction(f, self2) {
1322
+ return function() {
1323
+ f.apply(self2, arguments);
1324
+ };
1325
+ }
1326
+ function isFunction(func) {
1327
+ return typeof func === "function";
1328
+ }
1329
+ return Papa;
1330
+ });
1331
+ });
1332
+
1333
+ // packages/xml-parser/node.js
1334
+ import { parentPort, Worker } from "node:worker_threads";
1335
+ function toTransferList(transfer) {
1336
+ return transfer;
1337
+ }
1338
+ function createNodeXmlWorker(entry) {
1339
+ const worker = new Worker(entry);
1340
+ let is_terminated = false;
1341
+ const error_listeners = [];
1342
+ worker.on("exit", (code) => {
1343
+ if (is_terminated)
1344
+ return;
1345
+ for (const listener of error_listeners)
1346
+ listener({ message: `The worker exited with code ${code}.` });
1347
+ });
1348
+ return {
1349
+ postMessage(message, transfer) {
1350
+ worker.postMessage(message, toTransferList(transfer));
1351
+ },
1352
+ addEventListener(type, listener) {
1353
+ if (type === "message")
1354
+ worker.on("message", (data) => listener({ data }));
1355
+ else if (type === "messageerror")
1356
+ worker.on("messageerror", (error) => listener({ error }));
1357
+ else if (type === "error") {
1358
+ error_listeners.push(listener);
1359
+ worker.on("error", (error) => listener({ message: error?.message, error }));
1360
+ }
1361
+ },
1362
+ terminate() {
1363
+ is_terminated = true;
1364
+ return worker.terminate();
1365
+ }
1366
+ };
1367
+ }
1368
+
1369
+ // packages/import/src/constants.js
1370
+ var IMPORT_BATCH_BYTES = 1024 * 1024;
1371
+ var IMPORT_BATCH_ROWS = 500;
1372
+ var IMPORT_JSON_BYTES = 50 * 1024 * 1024;
1373
+ var IMPORT_CHUNK_BYTES = 16 * 1024;
1374
+ var IMPORT_SNIFF_BYTES = 256 * 1024;
1375
+ var IMPORT_SAMPLE_BYTES = 2 * 1024 * 1024;
1376
+ var IMPORT_SAMPLE_ROWS = 1000;
1377
+ var IMPORT_MAX_WORKERS = 16;
1378
+ var IMPORT_AUTO_WORKERS = 2;
1379
+ var IMPORT_XML_SEGMENT_MAX_BYTES = 2 * IMPORT_BATCH_BYTES;
1380
+ var IMPORT_XML_READ_BYTES = 256 * 1024;
1381
+ var IMPORT_WORKER_MIN_BYTES = 32 * 1024 * 1024;
1382
+ var IMPORT_FORMATS = Object.freeze({
1383
+ xml: "xml",
1384
+ jsonl: "jsonl",
1385
+ json: "json",
1386
+ csv: "csv",
1387
+ tsv: "tsv"
1388
+ });
1389
+ var IMPORT_REJECTED_STATUSES = Object.freeze([
1390
+ 400,
1391
+ 401,
1392
+ 403,
1393
+ 404,
1394
+ 405,
1395
+ 409,
1396
+ 410,
1397
+ 413,
1398
+ 415,
1399
+ 422,
1400
+ 429
1401
+ ]);
1402
+
1403
+ // packages/import/src/source.js
1404
+ function checkImportAbort(signal) {
1405
+ if (signal?.aborted)
1406
+ throw signal.reason ?? new DOMException("Import cancelled", "AbortError");
1407
+ }
1408
+ function createBlobSource(blob, name = "name" in blob ? String(blob.name) : "input") {
1409
+ return {
1410
+ name,
1411
+ size: blob.size,
1412
+ async* open() {
1413
+ for (let offset = 0;offset < blob.size; offset += IMPORT_CHUNK_BYTES) {
1414
+ yield new Uint8Array(await blob.slice(offset, offset + IMPORT_CHUNK_BYTES).arrayBuffer());
1415
+ }
1416
+ },
1417
+ async read(start, end) {
1418
+ return new Uint8Array(await blob.slice(start, end).arrayBuffer());
1419
+ }
1420
+ };
1421
+ }
1422
+ async function* readImportBytes(source, signal) {
1423
+ checkImportAbort(signal);
1424
+ const opened = source.open();
1425
+ const reader = "getReader" in opened ? opened.getReader() : null;
1426
+ const iterator = reader ? null : opened[Symbol.asyncIterator]();
1427
+ let is_complete = false;
1428
+ try {
1429
+ while (true) {
1430
+ checkImportAbort(signal);
1431
+ const result = await awaitImportRead(reader ? reader.read() : iterator.next(), signal);
1432
+ if (result.done) {
1433
+ is_complete = true;
1434
+ return;
1435
+ }
1436
+ if (!(result.value instanceof Uint8Array))
1437
+ throw new TypeError("Import sources must yield Uint8Array chunks.");
1438
+ for (let offset = 0;offset < result.value.length; offset += IMPORT_CHUNK_BYTES) {
1439
+ checkImportAbort(signal);
1440
+ yield result.value.subarray(offset, offset + IMPORT_CHUNK_BYTES);
1441
+ }
1442
+ }
1443
+ } finally {
1444
+ try {
1445
+ if (!is_complete) {
1446
+ const closing = reader ? reader.cancel() : iterator.return?.();
1447
+ if (signal?.aborted)
1448
+ Promise.resolve(closing).catch(() => {});
1449
+ else
1450
+ await closing;
1451
+ }
1452
+ } finally {
1453
+ reader?.releaseLock();
1454
+ }
1455
+ }
1456
+ }
1457
+ async function* decodeImportBytes(chunks) {
1458
+ const decoder = new TextDecoder("utf-8", { fatal: true });
1459
+ try {
1460
+ for await (const chunk of chunks)
1461
+ yield decoder.decode(chunk, { stream: true });
1462
+ const tail = decoder.decode();
1463
+ if (tail)
1464
+ yield tail;
1465
+ } catch (error) {
1466
+ if (error instanceof TypeError && /encoded data|encoding|utf-?8/i.test(error.message)) {
1467
+ throw new Error("This file is not valid UTF-8.");
1468
+ }
1469
+ throw error;
1470
+ }
1471
+ }
1472
+ function awaitImportRead(pending, signal) {
1473
+ if (!signal)
1474
+ return Promise.resolve(pending);
1475
+ return new Promise((resolve, reject) => {
1476
+ const onabort = () => reject(signal.reason ?? new DOMException("Import cancelled", "AbortError"));
1477
+ signal.addEventListener("abort", onabort, { once: true });
1478
+ Promise.resolve(pending).then((value) => {
1479
+ signal.removeEventListener("abort", onabort);
1480
+ resolve(value);
1481
+ }, (error) => {
1482
+ signal.removeEventListener("abort", onabort);
1483
+ reject(error);
1484
+ });
1485
+ if (signal.aborted) {
1486
+ signal.removeEventListener("abort", onabort);
1487
+ onabort();
1488
+ }
1489
+ });
1490
+ }
1491
+ // packages/xml-parser/src/parser.js
1492
+ var NAME_START = ":A-Z_a-z\\u00C0-\\u00D6\\u00D8-\\u00F6\\u00F8-\\u02FF\\u0370-\\u037D\\u037F-\\u1FFF\\u200C-\\u200D\\u2070-\\u218F\\u2C00-\\u2FEF\\u3001-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFFD\\u{10000}-\\u{EFFFF}";
1493
+ var NAME = new RegExp(`^[${NAME_START}][${NAME_START}\\-.0-9\\u00B7\\u0300-\\u036F\\u203F-\\u2040]*$`, "u");
1494
+ var ASCII_NAME = new Uint8Array(128);
1495
+ for (let code = 0;code < 128; code++) {
1496
+ const char = String.fromCharCode(code);
1497
+ if (/[:A-Z_a-z]/.test(char))
1498
+ ASCII_NAME[code] = 3;
1499
+ else if (/[-.0-9]/.test(char))
1500
+ ASCII_NAME[code] = 2;
1501
+ }
1502
+ var SUSPECT_CHAR = new RegExp("[\\0-\\x08\\x0B\\x0C\\x0E-\\x1F\\uD800-\\uDFFF\\uFFFE\\uFFFF]");
1503
+ var INVALID_CHAR = new RegExp("[\\0-\\x08\\x0B\\x0C\\x0E-\\x1F\\uFFFE\\uFFFF]|[\\uD800-\\uDBFF](?![\\uDC00-\\uDFFF])|" + "(?:^|[^\\uD800-\\uDBFF])[\\uDC00-\\uDFFF]");
1504
+ var SPACE = /^[ \t\r\n]*$/;
1505
+ var TAG_DELIMITER = /["'>]/g;
1506
+ var LINE_END = /\r\n?/g;
1507
+ var ATTRIBUTE_SPACE = /\r\n?|[\t\n]/g;
1508
+ var ENTITIES = Object.assign(Object.create(null), {
1509
+ amp: "&",
1510
+ lt: "<",
1511
+ gt: ">",
1512
+ apos: "'",
1513
+ quot: '"'
1514
+ });
1515
+ var NAME_CACHE_SIZE = 64;
1516
+ var DEFAULTS = {
1517
+ maxDepth: 256,
1518
+ maxTokenLength: 4 * 1024 * 1024,
1519
+ maxNameLength: 1024,
1520
+ maxAttributes: 4096
1521
+ };
1522
+ function normalizeLineEnds(value) {
1523
+ return value.includes("\r") ? value.replace(LINE_END, `
1524
+ `) : value;
1525
+ }
1526
+ function isXmlChar(cp) {
1527
+ if (cp < 32)
1528
+ return cp === 9 || cp === 10 || cp === 13;
1529
+ return cp <= 55295 || cp >= 57344 && cp <= 65533 || cp >= 65536 && cp <= 1114111;
1530
+ }
1531
+ function decodeReference(source, start, end) {
1532
+ if (source.charCodeAt(start) !== 35)
1533
+ return ENTITIES[source.slice(start, end)] ?? null;
1534
+ let i = start + 1;
1535
+ const is_hex = source.charCodeAt(i) === 120;
1536
+ if (is_hex)
1537
+ i++;
1538
+ if (i >= end)
1539
+ return null;
1540
+ let cp = 0;
1541
+ for (;i < end; i++) {
1542
+ const code = source.charCodeAt(i);
1543
+ let digit = code - 48;
1544
+ if (digit < 0 || digit > 9) {
1545
+ const lower = code | 32;
1546
+ if (!is_hex || lower < 97 || lower > 102)
1547
+ return null;
1548
+ digit = lower - 87;
1549
+ }
1550
+ cp = cp * (is_hex ? 16 : 10) + digit;
1551
+ if (cp > 1114111)
1552
+ return null;
1553
+ }
1554
+ return isXmlChar(cp) ? String.fromCodePoint(cp) : null;
1555
+ }
1556
+ function decodeReferences(value) {
1557
+ let result = "";
1558
+ let cursor = 0;
1559
+ for (let amp = value.indexOf("&");amp >= 0; amp = value.indexOf("&", cursor)) {
1560
+ const semicolon = value.indexOf(";", amp + 1);
1561
+ if (semicolon < 0)
1562
+ return null;
1563
+ const decoded = decodeReference(value, amp + 1, semicolon);
1564
+ if (decoded === null)
1565
+ return null;
1566
+ result += value.slice(cursor, amp) + decoded;
1567
+ cursor = semicolon + 1;
1568
+ }
1569
+ return result + value.slice(cursor);
1570
+ }
1571
+ function endsAttributeName(code) {
1572
+ return code === 61 || code === 32 || code === 9 || code === 10 || code === 13;
1573
+ }
1574
+ function skipSpace(source, i, limit) {
1575
+ while (i < limit) {
1576
+ const code = source.charCodeAt(i);
1577
+ if (code !== 32 && code !== 9 && code !== 10 && code !== 13)
1578
+ break;
1579
+ i++;
1580
+ }
1581
+ return i;
1582
+ }
1583
+
1584
+ class XmlParseError extends Error {
1585
+ constructor(message, code, offset) {
1586
+ super(message);
1587
+ this.name = "XmlParseError";
1588
+ this.code = code;
1589
+ this.offset = offset;
1590
+ }
1591
+ }
1592
+
1593
+ class XmlStreamParser {
1594
+ constructor(handlers = {}, options = {}) {
1595
+ this.handlers = handlers;
1596
+ this.options = { ...DEFAULTS, ...options, doctype: options.doctype ?? "prohibit" };
1597
+ for (const key of Object.keys(DEFAULTS)) {
1598
+ const value = this.options[key];
1599
+ if (!Number.isInteger(value) || value <= 0)
1600
+ throw new TypeError(`${key} must be a positive finite integer`);
1601
+ }
1602
+ if (!["prohibit", "ignore"].includes(this.options.doctype))
1603
+ throw new TypeError('doctype must be "prohibit" or "ignore"');
1604
+ this._offset = 0;
1605
+ this._mode = "";
1606
+ this._decoder = null;
1607
+ this._surrogate = "";
1608
+ this._ended = false;
1609
+ this._failed = false;
1610
+ this._busy = false;
1611
+ this._stack = [];
1612
+ this._root = false;
1613
+ this._is_declaration_allowed = true;
1614
+ this._doctype = false;
1615
+ this._kind = "";
1616
+ this._parts = [];
1617
+ this._length = 0;
1618
+ this._head = "";
1619
+ this._tail = "";
1620
+ this._quote = "";
1621
+ this._brackets = 0;
1622
+ this._dtdContext = "";
1623
+ this._entityParts = null;
1624
+ this._entityLength = 0;
1625
+ this._textBrackets = 0;
1626
+ this._textCR = false;
1627
+ this._scannedName = "";
1628
+ this._scannedAttributes = null;
1629
+ this._scannedEmpty = false;
1630
+ this._attributeNames = [];
1631
+ }
1632
+ get offset() {
1633
+ return this._offset;
1634
+ }
1635
+ write(chunk) {
1636
+ this._guard();
1637
+ this._busy = true;
1638
+ try {
1639
+ this._input(chunk);
1640
+ } catch (error) {
1641
+ this._failed = true;
1642
+ throw error;
1643
+ } finally {
1644
+ this._busy = false;
1645
+ }
1646
+ return this;
1647
+ }
1648
+ end(chunk) {
1649
+ this._guard();
1650
+ this._busy = true;
1651
+ try {
1652
+ if (chunk !== undefined)
1653
+ this._input(chunk);
1654
+ if (this._decoder) {
1655
+ let final;
1656
+ try {
1657
+ final = this._decoder.decode();
1658
+ } catch {
1659
+ this._error("Truncated UTF-8 input", "UTF8");
1660
+ }
1661
+ if (final)
1662
+ this._decoded(final);
1663
+ }
1664
+ if (this._surrogate)
1665
+ this._error("Unpaired UTF-16 surrogate", "CHARACTER");
1666
+ if (this._kind || this._entityParts || this._stack.length || !this._root)
1667
+ this._error("Incomplete XML document", "EOF");
1668
+ } catch (error) {
1669
+ this._failed = true;
1670
+ throw error;
1671
+ } finally {
1672
+ this._ended = true;
1673
+ this._busy = false;
1674
+ }
1675
+ }
1676
+ _guard() {
1677
+ if (this._ended || this._failed || this._busy) {
1678
+ this._failed = true;
1679
+ this._error("Parser is ended, failed, or already executing a callback", "STATE");
1680
+ }
1681
+ }
1682
+ _error(message, code = "SYNTAX", offset = this._offset) {
1683
+ throw new XmlParseError(message, code, offset);
1684
+ }
1685
+ _emit(name, ...args) {
1686
+ this.handlers[name]?.(...args);
1687
+ this._afterCallback();
1688
+ }
1689
+ _emitText(value, kind = "literal") {
1690
+ this.handlers.text?.(value, kind);
1691
+ this._afterCallback();
1692
+ }
1693
+ _afterCallback() {
1694
+ if (this._failed)
1695
+ this._error("Parser failed during callback", "STATE");
1696
+ }
1697
+ _input(chunk) {
1698
+ const mode = typeof chunk === "string" ? "string" : chunk instanceof Uint8Array ? "bytes" : "";
1699
+ if (!mode)
1700
+ throw new TypeError("XML input must be a string or Uint8Array");
1701
+ if (!chunk.length)
1702
+ return;
1703
+ if (this._mode && this._mode !== mode)
1704
+ this._error("Cannot mix strings and UTF-8 bytes", "INPUT_MODE");
1705
+ this._mode = mode;
1706
+ if (mode === "bytes") {
1707
+ this._decoder ??= new TextDecoder("utf-8", { fatal: true, ignoreBOM: true });
1708
+ try {
1709
+ chunk = this._decoder.decode(chunk, { stream: true });
1710
+ } catch {
1711
+ this._error("Invalid UTF-8 input", "UTF8");
1712
+ }
1713
+ }
1714
+ this._decoded(chunk);
1715
+ }
1716
+ _decoded(value) {
1717
+ if (!value)
1718
+ return;
1719
+ if (this._surrogate) {
1720
+ value = this._surrogate + value;
1721
+ this._surrogate = "";
1722
+ }
1723
+ const last = value.charCodeAt(value.length - 1);
1724
+ if (last >= 55296 && last <= 56319) {
1725
+ this._surrogate = value.slice(-1);
1726
+ value = value.slice(0, -1);
1727
+ }
1728
+ if (SUSPECT_CHAR.test(value)) {
1729
+ const invalid = INVALID_CHAR.exec(value);
1730
+ if (invalid)
1731
+ this._error("Invalid XML character", "CHARACTER", this._offset + invalid.index + invalid[0].length - 1);
1732
+ }
1733
+ let i = 0;
1734
+ if (this._offset === 0 && value.charCodeAt(0) === 65279) {
1735
+ i++;
1736
+ this._offset++;
1737
+ }
1738
+ while (i < value.length) {
1739
+ if (this._kind) {
1740
+ i = this._continueToken(value, i);
1741
+ continue;
1742
+ }
1743
+ const open = value.indexOf("<", i);
1744
+ const end = open < 0 ? value.length : open;
1745
+ if (end > i) {
1746
+ this._text(value.slice(i, end));
1747
+ this._offset += end - i;
1748
+ i = end;
1749
+ }
1750
+ if (i === value.length)
1751
+ break;
1752
+ if (this._entityParts)
1753
+ this._error("Unterminated entity reference", "ENTITY");
1754
+ this._textBrackets = 0;
1755
+ this._textCR = false;
1756
+ const next = this._markup(value, i);
1757
+ if (next >= 0) {
1758
+ i = next;
1759
+ continue;
1760
+ }
1761
+ this._kind = "prefix";
1762
+ this._tokenStart = this._offset;
1763
+ this._parts = [];
1764
+ this._length = 0;
1765
+ this._head = "";
1766
+ this._tail = "";
1767
+ this._quote = "";
1768
+ }
1769
+ }
1770
+ _markup(value, open) {
1771
+ const limit = Math.min(value.length, open + this.options.maxTokenLength);
1772
+ const next = value.charCodeAt(open + 1);
1773
+ let end;
1774
+ if (next === 33) {
1775
+ if (value.startsWith("<!--", open)) {
1776
+ const close = value.indexOf("--", open + 4);
1777
+ end = close + 3;
1778
+ if (close < 0 || end > limit || value.charCodeAt(close + 2) !== 62)
1779
+ return -1;
1780
+ this._offset += end - open;
1781
+ this._is_declaration_allowed = false;
1782
+ this._comment(value, open + 4, close);
1783
+ return end;
1784
+ }
1785
+ if (!this._stack.length || !value.startsWith("<![CDATA[", open))
1786
+ return -1;
1787
+ const close = value.indexOf("]]>", open + 9);
1788
+ end = close + 3;
1789
+ if (close < 0 || end > limit)
1790
+ return -1;
1791
+ this._offset += end - open;
1792
+ this._is_declaration_allowed = false;
1793
+ this._cdata(value, open + 9, close);
1794
+ return end;
1795
+ }
1796
+ if (next === 63) {
1797
+ const close = value.indexOf("?>", open + 1);
1798
+ end = close + 2;
1799
+ if (close < 0 || end > limit)
1800
+ return -1;
1801
+ this._offset += end - open;
1802
+ this._token(value.slice(open, end), "pi");
1803
+ return end;
1804
+ }
1805
+ end = this._scanTag(value, open, limit);
1806
+ if (end < 0)
1807
+ return -1;
1808
+ this._offset += end - open;
1809
+ this._is_declaration_allowed = false;
1810
+ this._applyTag();
1811
+ return end;
1812
+ }
1813
+ _scanTag(source, start, limit) {
1814
+ let i = start + 1;
1815
+ if (source.charCodeAt(i) === 47) {
1816
+ const name = this._stack[this._stack.length - 1];
1817
+ if (name === undefined || !source.startsWith(name, ++i))
1818
+ return -1;
1819
+ i = skipSpace(source, i + name.length, limit);
1820
+ if (i >= limit || source.charCodeAt(i) !== 62)
1821
+ return -1;
1822
+ this._scannedName = name;
1823
+ this._scannedAttributes = null;
1824
+ return i + 1;
1825
+ }
1826
+ const nameEnd = this._nameEnd(source, i, limit);
1827
+ if (nameEnd < 0)
1828
+ return -1;
1829
+ const attributes = Object.create(null);
1830
+ let count = 0;
1831
+ let is_empty = false;
1832
+ i = nameEnd;
1833
+ for (;; ) {
1834
+ const before = i;
1835
+ i = skipSpace(source, i, limit);
1836
+ if (i >= limit)
1837
+ return -1;
1838
+ const code = source.charCodeAt(i);
1839
+ if (code === 62)
1840
+ break;
1841
+ if (code === 47) {
1842
+ if (i + 1 >= limit || source.charCodeAt(i + 1) !== 62)
1843
+ return -1;
1844
+ is_empty = true;
1845
+ i++;
1846
+ break;
1847
+ }
1848
+ if (i === before)
1849
+ return -1;
1850
+ let attribute = count < NAME_CACHE_SIZE ? this._attributeNames[count] : undefined;
1851
+ let attribute_end = attribute === undefined ? -1 : i + attribute.length;
1852
+ if (attribute_end < 0 || !endsAttributeName(source.charCodeAt(attribute_end)) || !source.startsWith(attribute, i)) {
1853
+ attribute_end = this._nameEnd(source, i, limit);
1854
+ if (attribute_end < 0)
1855
+ return -1;
1856
+ attribute = source.slice(i, attribute_end);
1857
+ if (count < NAME_CACHE_SIZE)
1858
+ this._attributeNames[count] = attribute;
1859
+ }
1860
+ if (attributes[attribute] !== undefined || ++count > this.options.maxAttributes)
1861
+ return -1;
1862
+ i = skipSpace(source, attribute_end, limit);
1863
+ if (i >= limit || source.charCodeAt(i) !== 61)
1864
+ return -1;
1865
+ i = skipSpace(source, i + 1, limit);
1866
+ if (i >= limit)
1867
+ return -1;
1868
+ const quote = source.charCodeAt(i);
1869
+ if (quote !== 34 && quote !== 39)
1870
+ return -1;
1871
+ const value_start = ++i;
1872
+ let has_spaces = false;
1873
+ let has_references = false;
1874
+ for (;i < limit; i++) {
1875
+ const unit = source.charCodeAt(i);
1876
+ if (unit === quote)
1877
+ break;
1878
+ if (unit <= 38) {
1879
+ if (unit === 38)
1880
+ has_references = true;
1881
+ else if (unit === 9 || unit === 10 || unit === 13)
1882
+ has_spaces = true;
1883
+ } else if (unit === 60)
1884
+ return -1;
1885
+ }
1886
+ if (i >= limit)
1887
+ return -1;
1888
+ let value = source.slice(value_start, i);
1889
+ if (has_spaces)
1890
+ value = value.replace(ATTRIBUTE_SPACE, " ");
1891
+ if (has_references) {
1892
+ value = decodeReferences(value);
1893
+ if (value === null)
1894
+ return -1;
1895
+ }
1896
+ attributes[attribute] = value;
1897
+ i++;
1898
+ }
1899
+ this._scannedName = source.slice(start + 1, nameEnd);
1900
+ this._scannedAttributes = attributes;
1901
+ this._scannedEmpty = is_empty;
1902
+ return i + 1;
1903
+ }
1904
+ _nameEnd(source, start, limit) {
1905
+ if (start >= limit)
1906
+ return -1;
1907
+ let code = source.charCodeAt(start);
1908
+ let is_ascii = code < 128;
1909
+ if (is_ascii && !(ASCII_NAME[code] & 1))
1910
+ return -1;
1911
+ let i = start + 1;
1912
+ for (;i < limit; i++) {
1913
+ code = source.charCodeAt(i);
1914
+ if (code >= 128)
1915
+ is_ascii = false;
1916
+ else if (!(ASCII_NAME[code] & 2))
1917
+ break;
1918
+ }
1919
+ if (i >= limit || i - start > this.options.maxNameLength)
1920
+ return -1;
1921
+ if (!is_ascii && !NAME.test(source.slice(start, i)))
1922
+ return -1;
1923
+ return i;
1924
+ }
1925
+ _applyTag() {
1926
+ const attributes = this._scannedAttributes;
1927
+ if (attributes === null) {
1928
+ this._closeElement(this._scannedName);
1929
+ return;
1930
+ }
1931
+ this._scannedAttributes = null;
1932
+ this._openElement(this._scannedName, attributes, this._scannedEmpty);
1933
+ }
1934
+ _openElement(name, attributes, is_empty) {
1935
+ if (!this._stack.length && this._root)
1936
+ this._error("Multiple document elements");
1937
+ if (this._stack.length + 1 > this.options.maxDepth)
1938
+ this._error("Element exceeds maxDepth", "LIMIT");
1939
+ this._root = true;
1940
+ if (!is_empty)
1941
+ this._stack.push(name);
1942
+ this.handlers.startTag?.(name, attributes);
1943
+ this._afterCallback();
1944
+ if (is_empty) {
1945
+ this.handlers.endTag?.(name);
1946
+ this._afterCallback();
1947
+ }
1948
+ }
1949
+ _closeElement(name) {
1950
+ if (this._stack.pop() !== name)
1951
+ this._error("Mismatched end tag");
1952
+ this.handlers.endTag?.(name);
1953
+ this._afterCallback();
1954
+ }
1955
+ _comment(source, start, end) {
1956
+ const handler = this.handlers.comment;
1957
+ if (handler != null)
1958
+ Reflect.apply(handler, this.handlers, [normalizeLineEnds(source.slice(start, end))]);
1959
+ this._afterCallback();
1960
+ }
1961
+ _cdata(source, start, end) {
1962
+ if (end > start)
1963
+ this._emitText(normalizeLineEnds(source.slice(start, end)), "cdata");
1964
+ }
1965
+ _continueToken(value, i) {
1966
+ const start = i;
1967
+ let is_done = false;
1968
+ while (i < value.length && !is_done) {
1969
+ const kind = this._kind;
1970
+ if (kind === "comment" || kind === "cdata" || kind === "pi") {
1971
+ const end = this._sectionEnd(value, i);
1972
+ is_done = end >= 0;
1973
+ const next = is_done ? end : value.length;
1974
+ this._consumeSection(next - i);
1975
+ i = next;
1976
+ continue;
1977
+ }
1978
+ if (kind === "tag") {
1979
+ let next;
1980
+ if (this._quote) {
1981
+ next = value.indexOf(this._quote, i);
1982
+ if (next >= 0)
1983
+ this._quote = "";
1984
+ } else {
1985
+ TAG_DELIMITER.lastIndex = i;
1986
+ const delimiter = TAG_DELIMITER.exec(value);
1987
+ next = delimiter ? delimiter.index : -1;
1988
+ if (delimiter) {
1989
+ if (delimiter[0] === ">")
1990
+ is_done = true;
1991
+ else
1992
+ this._quote = delimiter[0];
1993
+ }
1994
+ }
1995
+ next = next < 0 ? value.length : next + 1;
1996
+ this._length += next - i;
1997
+ this._offset += next - i;
1998
+ i = next;
1999
+ if (this._length > this.options.maxTokenLength)
2000
+ this._error("XML token exceeds maxTokenLength", "LIMIT");
2001
+ continue;
2002
+ }
2003
+ const c = value[i++];
2004
+ this._offset++;
2005
+ if (++this._length > this.options.maxTokenLength)
2006
+ this._error("XML token exceeds maxTokenLength", "LIMIT");
2007
+ is_done = this._frame(c);
2008
+ }
2009
+ this._parts.push(value.slice(start, i));
2010
+ if (is_done) {
2011
+ const token = this._parts.length === 1 ? this._parts[0] : this._parts.join("");
2012
+ const kind = this._kind;
2013
+ this._kind = "";
2014
+ this._parts = [];
2015
+ this._token(token, kind);
2016
+ }
2017
+ return i;
2018
+ }
2019
+ _sectionEnd(value, i) {
2020
+ const tail = this._tail;
2021
+ const text = tail ? tail + value.slice(i) : value;
2022
+ const from = tail ? 0 : i;
2023
+ const shift = tail ? i - tail.length : 0;
2024
+ let end = -1;
2025
+ if (this._kind === "comment") {
2026
+ const hyphens = text.indexOf("--", from);
2027
+ if (hyphens >= 0 && hyphens + 2 < text.length) {
2028
+ end = hyphens + 3 + shift;
2029
+ if (text.charCodeAt(hyphens + 2) !== 62) {
2030
+ this._consumeSection(end - i);
2031
+ this._error("Double hyphen in comment");
2032
+ }
2033
+ }
2034
+ } else {
2035
+ const terminator = this._kind === "cdata" ? "]]>" : "?>";
2036
+ const found = text.indexOf(terminator, from);
2037
+ if (found >= 0)
2038
+ end = found + terminator.length + shift;
2039
+ }
2040
+ if (end < 0)
2041
+ this._tail = text.slice(Math.max(from, text.length - 2));
2042
+ return end;
2043
+ }
2044
+ _consumeSection(count) {
2045
+ const allowed = this.options.maxTokenLength - this._length;
2046
+ if (count > allowed)
2047
+ this._error("XML token exceeds maxTokenLength", "LIMIT", this._offset + allowed + 1);
2048
+ this._length += count;
2049
+ this._offset += count;
2050
+ }
2051
+ _frame(c) {
2052
+ const tail = this._tail;
2053
+ this._tail = (tail + c).slice(-3);
2054
+ if (this._kind === "prefix") {
2055
+ this._head += c;
2056
+ const h = this._head;
2057
+ if (h.length === 2 && c !== "!") {
2058
+ this._kind = c === "?" ? "pi" : "tag";
2059
+ return c === ">";
2060
+ }
2061
+ if (h === "<!--") {
2062
+ this._kind = "comment";
2063
+ this._tail = "";
2064
+ } else if (h === "<![CDATA[")
2065
+ this._kind = "cdata";
2066
+ else if (h === "<!DOCTYPE") {
2067
+ if (this.options.doctype === "prohibit")
2068
+ this._error("DOCTYPE is prohibited", "DOCTYPE");
2069
+ this._kind = "doctype";
2070
+ this._brackets = 0;
2071
+ this._dtdContext = "";
2072
+ } else if (!["<!--", "<![CDATA[", "<!DOCTYPE"].some((prefix) => prefix.startsWith(h)) && h.length > 1) {
2073
+ this._error("Invalid markup declaration");
2074
+ }
2075
+ return false;
2076
+ }
2077
+ if (this._dtdContext === "comment") {
2078
+ if (tail.endsWith("--") && c !== ">")
2079
+ this._error("Double hyphen in DTD comment");
2080
+ if (this._tail === "-->")
2081
+ this._dtdContext = "";
2082
+ return false;
2083
+ }
2084
+ if (this._dtdContext === "pi") {
2085
+ if (this._tail.endsWith("?>"))
2086
+ this._dtdContext = "";
2087
+ return false;
2088
+ }
2089
+ if (!this._quote && tail === "<!-" && c === "-") {
2090
+ this._dtdContext = "comment";
2091
+ this._tail = "";
2092
+ return false;
2093
+ }
2094
+ if (!this._quote && tail.endsWith("<") && c === "?") {
2095
+ this._dtdContext = "pi";
2096
+ return false;
2097
+ }
2098
+ if (this._quote) {
2099
+ if (c === this._quote)
2100
+ this._quote = "";
2101
+ return false;
2102
+ }
2103
+ if (c === '"' || c === "'") {
2104
+ this._quote = c;
2105
+ return false;
2106
+ }
2107
+ if (c === "[")
2108
+ this._brackets++;
2109
+ if (c === "]" && --this._brackets < 0)
2110
+ this._error("Unmatched DOCTYPE bracket");
2111
+ return c === ">" && this._brackets === 0;
2112
+ }
2113
+ _text(value) {
2114
+ this._is_declaration_allowed = false;
2115
+ if (!this._stack.length) {
2116
+ if (!SPACE.test(value))
2117
+ this._error("Non-whitespace outside document element");
2118
+ this._literal(value);
2119
+ return;
2120
+ }
2121
+ let i = 0;
2122
+ if (this._entityParts) {
2123
+ const semicolon = value.indexOf(";");
2124
+ const end = semicolon < 0 ? value.length : semicolon;
2125
+ this._entityLength += end;
2126
+ if (this._entityLength + 2 > this.options.maxTokenLength)
2127
+ this._error("Entity exceeds maxTokenLength", "LIMIT");
2128
+ this._entityParts.push(value.slice(0, end));
2129
+ if (semicolon < 0)
2130
+ return;
2131
+ const name = this._entityParts.join("");
2132
+ this._entityParts = null;
2133
+ this._reference(this._entity(name));
2134
+ i = semicolon + 1;
2135
+ }
2136
+ while (i < value.length) {
2137
+ const amp = value.indexOf("&", i);
2138
+ if (amp < 0) {
2139
+ this._literal(i ? value.slice(i) : value);
2140
+ return;
2141
+ }
2142
+ if (amp > i)
2143
+ this._literal(value.slice(i, amp));
2144
+ i = amp + 1;
2145
+ if (i === value.length) {
2146
+ this._entityParts = [];
2147
+ this._entityLength = 0;
2148
+ return;
2149
+ }
2150
+ const semicolon = value.indexOf(";", i);
2151
+ const end = semicolon < 0 ? value.length : semicolon;
2152
+ if (end - i + 2 > this.options.maxTokenLength)
2153
+ this._error("Entity exceeds maxTokenLength", "LIMIT");
2154
+ if (semicolon < 0) {
2155
+ this._entityParts = [value.slice(i)];
2156
+ this._entityLength = end - i;
2157
+ return;
2158
+ }
2159
+ this._reference(decodeReference(value, i, semicolon) ?? this._entity(value.slice(i, semicolon)));
2160
+ i = semicolon + 1;
2161
+ }
2162
+ }
2163
+ _reference(value) {
2164
+ this._textBrackets = 0;
2165
+ this._textCR = false;
2166
+ this._emitText(value, "reference");
2167
+ }
2168
+ _literal(value) {
2169
+ const length = value.length;
2170
+ const first = value.charCodeAt(0);
2171
+ const brackets = this._textBrackets;
2172
+ const is_straddling = brackets > 0 && (brackets === 2 && first === 62 || first === 93 && value.charCodeAt(1) === 62);
2173
+ if (is_straddling || value.includes("]]>"))
2174
+ this._error("CDATA close delimiter in text");
2175
+ let trailing = 0;
2176
+ while (trailing < 2 && trailing < length && value.charCodeAt(length - 1 - trailing) === 93)
2177
+ trailing++;
2178
+ this._textBrackets = trailing === length ? Math.min(2, brackets + trailing) : trailing;
2179
+ const has_pending_cr = this._textCR;
2180
+ this._textCR = value.charCodeAt(length - 1) === 13;
2181
+ if (has_pending_cr && first === 10)
2182
+ value = value.slice(1);
2183
+ value = normalizeLineEnds(value);
2184
+ if (value)
2185
+ this._emitText(value);
2186
+ }
2187
+ _entity(name) {
2188
+ const value = decodeReference(name, 0, name.length);
2189
+ if (value !== null)
2190
+ return value;
2191
+ if (!/^#(?:[0-9]+|x[0-9a-fA-F]+)$/.test(name))
2192
+ this._error("Unknown or malformed entity reference", "ENTITY");
2193
+ const cp = name[1] === "x" ? Number.parseInt(name.slice(2), 16) : Number(name.slice(1));
2194
+ if (!Number.isSafeInteger(cp) || cp > 1114111 || cp < 0)
2195
+ this._error("Invalid character reference", "ENTITY");
2196
+ this._error("Invalid XML character reference", "CHARACTER");
2197
+ }
2198
+ _name(name) {
2199
+ if (name.length > this.options.maxNameLength)
2200
+ this._error("Name exceeds maxNameLength", "LIMIT");
2201
+ if (!NAME.test(name))
2202
+ this._error("Invalid XML name");
2203
+ return name;
2204
+ }
2205
+ _token(raw, kind) {
2206
+ const is_declaration_allowed = this._is_declaration_allowed;
2207
+ this._is_declaration_allowed = false;
2208
+ if (kind === "tag") {
2209
+ if (this._scanTag(raw, 0, raw.length) === raw.length)
2210
+ this._applyTag();
2211
+ else
2212
+ this._tag(normalizeLineEnds(raw));
2213
+ return;
2214
+ }
2215
+ if (kind === "comment") {
2216
+ this._comment(raw, 4, raw.length - 3);
2217
+ return;
2218
+ }
2219
+ if (kind === "cdata") {
2220
+ if (!this._stack.length)
2221
+ this._error("CDATA outside document element");
2222
+ this._cdata(raw, 9, raw.length - 3);
2223
+ return;
2224
+ }
2225
+ const token = normalizeLineEnds(raw);
2226
+ if (kind === "pi") {
2227
+ const match = /^<\?([^ \t\n?]+)(?:[ \t\n]+([\s\S]*))?\?>$/.exec(token);
2228
+ if (!match)
2229
+ this._error("Malformed processing instruction");
2230
+ const target = this._name(match[1]);
2231
+ if (target.toLowerCase() === "xml") {
2232
+ if (target !== "xml" || !is_declaration_allowed)
2233
+ this._error("Misplaced XML declaration");
2234
+ const declaration = /^<\?xml[ \t\n]+version[ \t\n]*=[ \t\n]*(['"])1\.0\1(?:[ \t\n]+encoding[ \t\n]*=[ \t\n]*(['"])([A-Za-z][A-Za-z0-9._-]*)\2)?(?:[ \t\n]+standalone[ \t\n]*=[ \t\n]*(['"])(?:yes|no)\4)?[ \t\n]*\?>$/.exec(token);
2235
+ if (!declaration)
2236
+ this._error("Unsupported or malformed XML declaration", "DECLARATION");
2237
+ if (this._mode === "bytes" && declaration[3] && declaration[3].toLowerCase() !== "utf-8")
2238
+ this._error("Only UTF-8 byte encoding is supported", "ENCODING");
2239
+ } else
2240
+ this._emit("processingInstruction", target, match[2] ?? "");
2241
+ return;
2242
+ }
2243
+ this._parseDoctype(token, raw);
2244
+ }
2245
+ _parseDoctype(token, raw) {
2246
+ if (this._doctype || this._root)
2247
+ this._error("Misplaced or duplicate DOCTYPE", "DOCTYPE");
2248
+ const header = /^<!DOCTYPE[ \t\n]+([^ \t\n\[>]+)([\s\S]*)>$/.exec(token);
2249
+ if (!header)
2250
+ this._error("Malformed DOCTYPE", "DOCTYPE");
2251
+ this._name(header[1]);
2252
+ let rest = header[2];
2253
+ const external = /^[ \t\n]+(?:SYSTEM[ \t\n]+(?:"[^"]*"|'[^']*')|PUBLIC[ \t\n]+(?:"[\r\na-zA-Z0-9 \-'()+,./:=?;!*#@$_%]*"|'[\r\na-zA-Z0-9 \-()+,./:=?;!*#@$_%]*')[ \t\n]+(?:"[^"]*"|'[^']*'))/.exec(rest);
2254
+ if (external)
2255
+ rest = rest.slice(external[0].length);
2256
+ const start = skipSpace(rest, 0, rest.length);
2257
+ let end = rest.length;
2258
+ while (end > start) {
2259
+ const code = rest.charCodeAt(end - 1);
2260
+ if (code !== 32 && code !== 9 && code !== 10 && code !== 13)
2261
+ break;
2262
+ end--;
2263
+ }
2264
+ rest = rest.slice(start, end);
2265
+ if (rest && !(rest.startsWith("[") && rest.endsWith("]")))
2266
+ this._error("Malformed DOCTYPE framing", "DOCTYPE");
2267
+ this._doctype = true;
2268
+ this._emit("doctype", raw);
2269
+ }
2270
+ _tag(token) {
2271
+ if (token.startsWith("</")) {
2272
+ const match = /^<\/([^ \t\n>]+)[ \t\n]*>$/.exec(token);
2273
+ if (!match)
2274
+ this._error("Malformed end tag");
2275
+ this._closeElement(this._name(match[1]));
2276
+ return;
2277
+ }
2278
+ const is_empty = token.endsWith("/>");
2279
+ const end = token.length - (is_empty ? 2 : 1);
2280
+ let i = 1;
2281
+ while (i < end && !/[ \t\n]/.test(token[i]))
2282
+ i++;
2283
+ const name = this._name(token.slice(1, i));
2284
+ const attrs = Object.create(null);
2285
+ let count = 0;
2286
+ while (i < end) {
2287
+ const before = i;
2288
+ while (i < end && /[ \t\n]/.test(token[i]))
2289
+ i++;
2290
+ if (i === end)
2291
+ break;
2292
+ if (i === before)
2293
+ this._error("Missing whitespace before attribute");
2294
+ const start = i;
2295
+ while (i < end && !/[ \t\n=]/.test(token[i]))
2296
+ i++;
2297
+ const attr = this._name(token.slice(start, i));
2298
+ if (Object.hasOwn(attrs, attr))
2299
+ this._error("Duplicate attribute");
2300
+ if (++count > this.options.maxAttributes)
2301
+ this._error("Too many attributes", "LIMIT");
2302
+ while (i < end && /[ \t\n]/.test(token[i]))
2303
+ i++;
2304
+ if (token[i++] !== "=")
2305
+ this._error("Expected attribute equals sign");
2306
+ while (i < end && /[ \t\n]/.test(token[i]))
2307
+ i++;
2308
+ const quote = token[i++];
2309
+ if (quote !== '"' && quote !== "'")
2310
+ this._error("Expected quoted attribute value");
2311
+ const close = token.indexOf(quote, i);
2312
+ if (close < 0 || close >= end)
2313
+ this._error("Unterminated attribute value");
2314
+ let value = token.slice(i, close);
2315
+ if (value.includes("<"))
2316
+ this._error("Literal less-than in attribute");
2317
+ value = value.replace(ATTRIBUTE_SPACE, " ");
2318
+ if (value.includes("&")) {
2319
+ let cursor = 0;
2320
+ const pieces = [];
2321
+ while (cursor < value.length) {
2322
+ const amp = value.indexOf("&", cursor);
2323
+ if (amp < 0) {
2324
+ pieces.push(value.slice(cursor));
2325
+ break;
2326
+ }
2327
+ pieces.push(value.slice(cursor, amp));
2328
+ const semi = value.indexOf(";", amp + 1);
2329
+ if (semi < 0)
2330
+ this._error("Unterminated attribute entity", "ENTITY");
2331
+ pieces.push(this._entity(value.slice(amp + 1, semi)));
2332
+ cursor = semi + 1;
2333
+ }
2334
+ value = pieces.join("");
2335
+ }
2336
+ attrs[attr] = value;
2337
+ i = close + 1;
2338
+ }
2339
+ this._openElement(name, attrs, is_empty);
2340
+ }
2341
+ }
2342
+ // packages/xml-parser/src/whitespace.js
2343
+ function createWhitespaceFilter(mode = "preserve") {
2344
+ if (mode === "preserve")
2345
+ return;
2346
+ if (mode !== "strip-indentation")
2347
+ throw new TypeError("Unsupported XML whitespace mode");
2348
+ const stack = [];
2349
+ return {
2350
+ enter(attributes) {
2351
+ const space = attributes["xml:space"];
2352
+ stack.push({
2353
+ is_preserved: space === "preserve" || space !== "default" && Boolean(stack.at(-1)?.is_preserved),
2354
+ has_content: false,
2355
+ has_protected_text: false
2356
+ });
2357
+ },
2358
+ text(value, kind) {
2359
+ const frame = stack.at(-1);
2360
+ if (!frame)
2361
+ return;
2362
+ frame.has_content ||= /[^ \t\r\n]/.test(value);
2363
+ frame.has_protected_text ||= kind !== "literal";
2364
+ },
2365
+ leave(record) {
2366
+ const frame = stack.pop();
2367
+ if (!record || !frame || frame.is_preserved || frame.has_content || frame.has_protected_text)
2368
+ return;
2369
+ if (!record.children.some((child) => typeof child !== "string"))
2370
+ return;
2371
+ record.children = record.children.filter((child) => typeof child !== "string" || !/^[ \t\r\n]*[\r\n][ \t\r\n]*$/.test(child));
2372
+ }
2373
+ };
2374
+ }
2375
+
2376
+ // packages/xml-parser/src/streams.js
2377
+ var SLICE_SIZE = 16 * 1024;
2378
+ function validatePositiveInteger(value, name) {
2379
+ if (!Number.isSafeInteger(value) || value <= 0) {
2380
+ throw new RangeError(`${name} must be a positive safe integer`);
2381
+ }
2382
+ return value;
2383
+ }
2384
+ async function* readSourceValues(source) {
2385
+ if (source != null && typeof source === "object" && "getReader" in source && typeof source.getReader === "function") {
2386
+ const reader = source.getReader();
2387
+ let is_finished = false;
2388
+ let has_failed = false;
2389
+ try {
2390
+ while (true) {
2391
+ const result = await reader.read();
2392
+ if (result.done) {
2393
+ is_finished = true;
2394
+ return;
2395
+ }
2396
+ yield result.value;
2397
+ }
2398
+ } catch (error) {
2399
+ has_failed = true;
2400
+ throw error;
2401
+ } finally {
2402
+ try {
2403
+ if (!is_finished)
2404
+ await reader.cancel();
2405
+ } catch (error) {
2406
+ if (!has_failed)
2407
+ throw error;
2408
+ } finally {
2409
+ reader.releaseLock();
2410
+ }
2411
+ }
2412
+ } else {
2413
+ yield* source;
2414
+ }
2415
+ }
2416
+ async function* xmlRecords(source, options = {}) {
2417
+ const whitespace = createWhitespaceFilter(options.whitespace);
2418
+ const record_depth = validatePositiveInteger(options.recordDepth ?? 2, "recordDepth");
2419
+ const max_chars = validatePositiveInteger(options.maxRecordChars ?? 4 * 1024 * 1024, "maxRecordChars");
2420
+ const max_nodes = validatePositiveInteger(options.maxRecordNodes ?? 1e5, "maxRecordNodes");
2421
+ if (options.select !== undefined && typeof options.select !== "function") {
2422
+ throw new TypeError("select must be a function");
2423
+ }
2424
+ const path = [];
2425
+ const stack = [];
2426
+ let ready = [];
2427
+ let chars = 0;
2428
+ let nodes = 0;
2429
+ function account(added_chars, added_nodes) {
2430
+ chars += added_chars;
2431
+ nodes += added_nodes;
2432
+ if (chars > max_chars)
2433
+ throw new RangeError("maxRecordChars exceeded");
2434
+ if (nodes > max_nodes)
2435
+ throw new RangeError("maxRecordNodes exceeded");
2436
+ }
2437
+ const parser = new XmlStreamParser({
2438
+ startTag(name, attributes) {
2439
+ path.push(name);
2440
+ whitespace?.enter(attributes);
2441
+ if (!stack.length) {
2442
+ const is_selected = options.select ? options.select(path.slice(), attributes) : path.length === record_depth;
2443
+ if (typeof is_selected !== "boolean")
2444
+ throw new TypeError("select must return a boolean");
2445
+ if (!is_selected)
2446
+ return;
2447
+ chars = 0;
2448
+ nodes = 0;
2449
+ }
2450
+ let length = name.length;
2451
+ for (const key of Object.keys(attributes))
2452
+ length += key.length + attributes[key].length;
2453
+ account(length, 1);
2454
+ const record = { name, attributes, children: [] };
2455
+ if (stack.length)
2456
+ stack[stack.length - 1].children.push(record);
2457
+ stack.push(record);
2458
+ },
2459
+ endTag() {
2460
+ if (stack.length) {
2461
+ const record = stack.pop();
2462
+ whitespace?.leave(record);
2463
+ if (!stack.length)
2464
+ ready.push(record);
2465
+ } else
2466
+ whitespace?.leave();
2467
+ path.pop();
2468
+ },
2469
+ text(value, kind) {
2470
+ whitespace?.text(value, kind);
2471
+ if (!stack.length || !value.length)
2472
+ return;
2473
+ const children = stack[stack.length - 1].children;
2474
+ const last = children.length - 1;
2475
+ const is_merging = typeof children[last] === "string";
2476
+ account(value.length, is_merging ? 0 : 1);
2477
+ if (is_merging)
2478
+ children[last] += value;
2479
+ else
2480
+ children.push(value);
2481
+ }
2482
+ }, options.parserOptions);
2483
+ for await (const chunk of readSourceValues(source)) {
2484
+ if (typeof chunk !== "string" && !(chunk instanceof Uint8Array)) {
2485
+ throw new TypeError("XML source chunks must be strings or Uint8Array values");
2486
+ }
2487
+ for (let offset = 0;offset < chunk.length; offset += SLICE_SIZE) {
2488
+ parser.write(typeof chunk === "string" ? chunk.slice(offset, offset + SLICE_SIZE) : chunk.subarray(offset, offset + SLICE_SIZE));
2489
+ for (const record of ready)
2490
+ yield record;
2491
+ ready = [];
2492
+ }
2493
+ }
2494
+ parser.end();
2495
+ for (const record of ready)
2496
+ yield record;
2497
+ }
2498
+ async function* xmlDocumentRecords(source, options = {}) {
2499
+ const whitespace = createWhitespaceFilter(options.whitespace);
2500
+ const max_chars = validatePositiveInteger(options.maxRecordChars ?? 4 * 1024 * 1024, "maxRecordChars");
2501
+ const max_nodes = validatePositiveInteger(options.maxRecordNodes ?? 1e5, "maxRecordNodes");
2502
+ if (options.onRoot !== undefined && typeof options.onRoot !== "function")
2503
+ throw new TypeError("onRoot must be a function");
2504
+ let root = null;
2505
+ let root_chars = 0;
2506
+ let root_nodes = 1;
2507
+ let root_text = "";
2508
+ let is_root_text_protected = false;
2509
+ let has_element_children = false;
2510
+ let has_root_attributes = false;
2511
+ let depth = 0;
2512
+ const stack = [];
2513
+ let ready = [];
2514
+ let chars = 0;
2515
+ let nodes = 0;
2516
+ function checkLimits(count, node_count) {
2517
+ if (count > max_chars)
2518
+ throw new RangeError("maxRecordChars exceeded");
2519
+ if (node_count > max_nodes)
2520
+ throw new RangeError("maxRecordNodes exceeded");
2521
+ }
2522
+ function finishRootText() {
2523
+ if (root && root_text && (!has_element_children || is_root_text_protected || /[^ \t\r\n]/.test(root_text))) {
2524
+ root.children.push(root_text);
2525
+ root_chars += root_text.length;
2526
+ root_nodes++;
2527
+ }
2528
+ root_text = "";
2529
+ is_root_text_protected = false;
2530
+ }
2531
+ const parser = new XmlStreamParser({
2532
+ startTag(name, attributes) {
2533
+ depth++;
2534
+ whitespace?.enter(attributes);
2535
+ let length = name.length;
2536
+ for (const key of Object.keys(attributes))
2537
+ length += key.length + attributes[key].length;
2538
+ if (depth === 1) {
2539
+ options.onRoot?.(name, attributes);
2540
+ root = { name, attributes, children: [] };
2541
+ root_chars = length;
2542
+ has_root_attributes = Object.keys(attributes).length > 0;
2543
+ checkLimits(root_chars, root_nodes);
2544
+ return;
2545
+ }
2546
+ if (depth === 2) {
2547
+ has_element_children = true;
2548
+ finishRootText();
2549
+ chars = 0;
2550
+ nodes = 0;
2551
+ }
2552
+ chars += length;
2553
+ nodes++;
2554
+ checkLimits(chars, nodes);
2555
+ const record = { name, attributes, children: [] };
2556
+ if (stack.length)
2557
+ stack[stack.length - 1].children.push(record);
2558
+ stack.push(record);
2559
+ },
2560
+ endTag() {
2561
+ if (depth === 1) {
2562
+ finishRootText();
2563
+ whitespace?.leave();
2564
+ } else {
2565
+ const record = stack.pop();
2566
+ whitespace?.leave(record);
2567
+ if (!stack.length)
2568
+ ready.push(record);
2569
+ }
2570
+ depth--;
2571
+ },
2572
+ text(value, kind) {
2573
+ whitespace?.text(value, kind);
2574
+ if (!value.length)
2575
+ return;
2576
+ if (depth === 1) {
2577
+ checkLimits(root_chars + root_text.length + value.length, root_nodes + 1);
2578
+ root_text += value;
2579
+ is_root_text_protected ||= kind !== "literal";
2580
+ } else if (stack.length) {
2581
+ const children = stack[stack.length - 1].children;
2582
+ const last = children.length - 1;
2583
+ const is_merging = typeof children[last] === "string";
2584
+ chars += value.length;
2585
+ if (!is_merging)
2586
+ nodes++;
2587
+ checkLimits(chars, nodes);
2588
+ if (is_merging)
2589
+ children[last] += value;
2590
+ else
2591
+ children.push(value);
2592
+ }
2593
+ }
2594
+ }, options.parserOptions);
2595
+ for await (const chunk of readSourceValues(source)) {
2596
+ if (typeof chunk !== "string" && !(chunk instanceof Uint8Array))
2597
+ throw new TypeError("XML source chunks must be strings or Uint8Array values");
2598
+ for (let offset = 0;offset < chunk.length; offset += SLICE_SIZE) {
2599
+ parser.write(typeof chunk === "string" ? chunk.slice(offset, offset + SLICE_SIZE) : chunk.subarray(offset, offset + SLICE_SIZE));
2600
+ for (const record of ready)
2601
+ yield record;
2602
+ ready = [];
2603
+ }
2604
+ }
2605
+ parser.end();
2606
+ for (const record of ready)
2607
+ yield record;
2608
+ const context = root;
2609
+ if (context && (!has_element_children || has_root_attributes || context.children.length))
2610
+ yield context;
2611
+ }
2612
+ // packages/xml-parser/src/parallel/reader.js
2613
+ function checkXmlAbort(signal) {
2614
+ if (signal?.aborted)
2615
+ throw signal.reason ?? new DOMException("XML read cancelled", "AbortError");
2616
+ }
2617
+ function awaitXmlSignal(pending, signal) {
2618
+ if (!signal)
2619
+ return Promise.resolve(pending);
2620
+ return new Promise((resolve, reject) => {
2621
+ const onabort = () => reject(signal.reason ?? new DOMException("XML read cancelled", "AbortError"));
2622
+ signal.addEventListener("abort", onabort, { once: true });
2623
+ Promise.resolve(pending).then((value) => {
2624
+ signal.removeEventListener("abort", onabort);
2625
+ resolve(value);
2626
+ }, (error) => {
2627
+ signal.removeEventListener("abort", onabort);
2628
+ reject(error);
2629
+ });
2630
+ if (signal.aborted) {
2631
+ signal.removeEventListener("abort", onabort);
2632
+ onabort();
2633
+ }
2634
+ });
2635
+ }
2636
+
2637
+ class XmlSourceReader {
2638
+ constructor(source, start, read_bytes, signal) {
2639
+ this.source = source;
2640
+ this.position = start;
2641
+ this.read_bytes = read_bytes;
2642
+ this.signal = signal;
2643
+ this.pending = null;
2644
+ }
2645
+ read() {
2646
+ const start = this.position;
2647
+ if (start >= this.source.size)
2648
+ return Promise.resolve(null);
2649
+ const end = Math.min(this.source.size, Math.floor((start + this.read_bytes) / SLICE_SIZE) * SLICE_SIZE);
2650
+ this.position = end;
2651
+ const reading = Promise.resolve(this.source.read(start, end)).then((bytes) => {
2652
+ if (!(bytes instanceof Uint8Array) || bytes.length !== end - start)
2653
+ throw new Error("The XML source returned a different number of bytes than requested.");
2654
+ return bytes;
2655
+ });
2656
+ reading.catch(() => {});
2657
+ return reading;
2658
+ }
2659
+ async next() {
2660
+ checkXmlAbort(this.signal);
2661
+ const current = this.pending ?? this.read();
2662
+ this.pending = null;
2663
+ const chunk = await awaitXmlSignal(current, this.signal);
2664
+ if (chunk)
2665
+ this.pending = this.read();
2666
+ return chunk;
2667
+ }
2668
+ }
2669
+
2670
+ // packages/xml-parser/src/parallel/segment.js
2671
+ var encoder;
2672
+ function describeXmlError(error) {
2673
+ const value = error ?? {};
2674
+ const description = {
2675
+ name: typeof value.name === "string" ? value.name : "Error",
2676
+ message: typeof value.message === "string" ? value.message : String(error)
2677
+ };
2678
+ if (typeof value.code === "string")
2679
+ description.code = value.code;
2680
+ if (typeof value.offset === "number")
2681
+ description.offset = value.offset;
2682
+ return description;
2683
+ }
2684
+ function createXmlSegmentProcessor(config, state, sink, describeRecordError) {
2685
+ const whitespace = createWhitespaceFilter(config.whitespace);
2686
+ const max_chars = config.max_record_chars;
2687
+ const max_nodes = config.max_record_nodes;
2688
+ function checkLimits(count, node_count) {
2689
+ if (count > max_chars)
2690
+ throw new RangeError("maxRecordChars exceeded");
2691
+ if (node_count > max_nodes)
2692
+ throw new RangeError("maxRecordNodes exceeded");
2693
+ }
2694
+ let ready = [];
2695
+ const stack = [];
2696
+ let chars = 0;
2697
+ let nodes = 0;
2698
+ let root_text = "";
2699
+ let is_root_text_protected = false;
2700
+ let root_chars = state.root_chars;
2701
+ let root_nodes = state.root_nodes;
2702
+ let has_element_children = state.has_element_children;
2703
+ const kept = [];
2704
+ const root_check = { chars: -1, nodes: -1 };
2705
+ let is_root_limit = false;
2706
+ function checkRoot(count, node_count) {
2707
+ root_check.chars = Math.max(root_check.chars, count - state.root_chars);
2708
+ root_check.nodes = Math.max(root_check.nodes, node_count - state.root_nodes);
2709
+ try {
2710
+ checkLimits(count, node_count);
2711
+ } catch (error) {
2712
+ is_root_limit = true;
2713
+ throw error;
2714
+ }
2715
+ }
2716
+ function finishRootText() {
2717
+ if (root_text && (!has_element_children || is_root_text_protected || /[^ \t\r\n]/.test(root_text))) {
2718
+ kept.push(root_text);
2719
+ root_chars += root_text.length;
2720
+ root_nodes++;
2721
+ }
2722
+ root_text = "";
2723
+ is_root_text_protected = false;
2724
+ }
2725
+ function addChildText(value) {
2726
+ const children = stack[stack.length - 1].children;
2727
+ const last = children.length - 1;
2728
+ const is_merging = typeof children[last] === "string";
2729
+ chars += value.length;
2730
+ if (!is_merging)
2731
+ nodes++;
2732
+ checkLimits(chars, nodes);
2733
+ if (is_merging)
2734
+ children[last] += value;
2735
+ else
2736
+ children.push(value);
2737
+ }
2738
+ function openRecord(name, attributes) {
2739
+ let length = name.length;
2740
+ for (const key of Object.keys(attributes))
2741
+ length += key.length + attributes[key].length;
2742
+ chars += length;
2743
+ nodes++;
2744
+ checkLimits(chars, nodes);
2745
+ const record = { name, attributes, children: [] };
2746
+ if (stack.length)
2747
+ stack[stack.length - 1].children.push(record);
2748
+ stack.push(record);
2749
+ }
2750
+ function closeRecord() {
2751
+ const record = stack.pop();
2752
+ whitespace?.leave(record);
2753
+ if (!stack.length)
2754
+ ready.push(record);
2755
+ }
2756
+ let depth = 0;
2757
+ const path = [];
2758
+ const select_path = config.select_path;
2759
+ let handlers;
2760
+ if (!select_path) {
2761
+ handlers = {
2762
+ startTag(name, attributes) {
2763
+ depth++;
2764
+ whitespace?.enter(attributes);
2765
+ if (depth === 1)
2766
+ return;
2767
+ if (depth === 2) {
2768
+ has_element_children = true;
2769
+ finishRootText();
2770
+ chars = 0;
2771
+ nodes = 0;
2772
+ }
2773
+ openRecord(name, attributes);
2774
+ },
2775
+ endTag() {
2776
+ if (depth === 1) {
2777
+ finishRootText();
2778
+ whitespace?.leave();
2779
+ } else
2780
+ closeRecord();
2781
+ depth--;
2782
+ },
2783
+ text(value, kind) {
2784
+ whitespace?.text(value, kind);
2785
+ if (!value.length)
2786
+ return;
2787
+ if (depth === 1) {
2788
+ checkRoot(root_chars + root_text.length + value.length, root_nodes + 1);
2789
+ root_text += value;
2790
+ is_root_text_protected ||= kind !== "literal";
2791
+ } else if (stack.length)
2792
+ addChildText(value);
2793
+ }
2794
+ };
2795
+ } else {
2796
+ handlers = {
2797
+ startTag(name, attributes) {
2798
+ path.push(name);
2799
+ whitespace?.enter(attributes);
2800
+ if (!stack.length) {
2801
+ if (path.length !== select_path.length)
2802
+ return;
2803
+ for (let index = 0;index < path.length; index++)
2804
+ if (path[index] !== select_path[index])
2805
+ return;
2806
+ chars = 0;
2807
+ nodes = 0;
2808
+ }
2809
+ openRecord(name, attributes);
2810
+ },
2811
+ endTag() {
2812
+ if (stack.length)
2813
+ closeRecord();
2814
+ else
2815
+ whitespace?.leave();
2816
+ path.pop();
2817
+ },
2818
+ text(value, kind) {
2819
+ whitespace?.text(value, kind);
2820
+ if (stack.length && value.length)
2821
+ addChildText(value);
2822
+ }
2823
+ };
2824
+ }
2825
+ const parser = new XmlStreamParser(handlers, config.parser_options);
2826
+ parser.write(config.root_tag);
2827
+ const base_offset = parser.offset;
2828
+ let position = state.start;
2829
+ let last_write_offset = 0;
2830
+ let length = 0;
2831
+ let count = 0;
2832
+ let failure = null;
2833
+ function drain(slice) {
2834
+ const completed = ready;
2835
+ ready = [];
2836
+ for (const record of completed) {
2837
+ try {
2838
+ sink.add(record);
2839
+ } catch (error) {
2840
+ failure = { phase: "record", slice, error: describeRecordError(error) };
2841
+ return false;
2842
+ }
2843
+ count++;
2844
+ }
2845
+ return true;
2846
+ }
2847
+ function failWrite(phase, slice, error) {
2848
+ const description = describeXmlError(error);
2849
+ if (description.offset !== undefined)
2850
+ description.offset -= base_offset;
2851
+ failure = { phase, slice, error: description };
2852
+ if (is_root_limit)
2853
+ failure.is_root_limit = true;
2854
+ }
2855
+ return {
2856
+ push(bytes) {
2857
+ let offset = 0;
2858
+ while (offset < bytes.length && !failure) {
2859
+ const slice = Math.floor(position / SLICE_SIZE);
2860
+ const boundary = (slice + 1) * SLICE_SIZE;
2861
+ const size = Math.min(bytes.length - offset, boundary - position);
2862
+ last_write_offset = parser.offset - base_offset;
2863
+ try {
2864
+ parser.write(bytes.subarray(offset, offset + size));
2865
+ } catch (error) {
2866
+ failWrite("write", slice, error);
2867
+ return false;
2868
+ }
2869
+ offset += size;
2870
+ position += size;
2871
+ if (position === boundary && !drain(slice))
2872
+ return false;
2873
+ }
2874
+ return !failure;
2875
+ },
2876
+ close() {
2877
+ if (failure)
2878
+ return false;
2879
+ length = parser.offset - base_offset;
2880
+ const slice = Math.floor((position - 1) / SLICE_SIZE);
2881
+ const is_drained = position % SLICE_SIZE === 0 || drain(slice);
2882
+ try {
2883
+ if (state.is_final) {
2884
+ if (!is_drained)
2885
+ return false;
2886
+ parser.end();
2887
+ return drain(slice);
2888
+ }
2889
+ if (root_text)
2890
+ throw new Error("The segment ended inside root text.");
2891
+ parser.end((encoder ??= new TextEncoder).encode(`</${config.root_name}>`));
2892
+ } catch (error) {
2893
+ failWrite(state.is_final ? "end" : "boundary", slice, error);
2894
+ return false;
2895
+ }
2896
+ return is_drained;
2897
+ },
2898
+ get failure() {
2899
+ return failure;
2900
+ },
2901
+ get length() {
2902
+ return length;
2903
+ },
2904
+ get last_write_offset() {
2905
+ return last_write_offset;
2906
+ },
2907
+ take() {
2908
+ const output = { output: sink.take(), count };
2909
+ count = 0;
2910
+ return output;
2911
+ },
2912
+ summary() {
2913
+ return {
2914
+ kept,
2915
+ has_element_children,
2916
+ root_check: root_check.chars >= 0 ? root_check : null
2917
+ };
2918
+ }
2919
+ };
2920
+ }
2921
+
2922
+ // packages/xml-parser/src/parallel/split.js
2923
+ var LT = 60;
2924
+ var GT = 62;
2925
+ var SLASH = 47;
2926
+ var BANG = 33;
2927
+ var QUESTION = 63;
2928
+ var DOUBLE_QUOTE = 34;
2929
+ var SINGLE_QUOTE = 39;
2930
+ var DASH = 45;
2931
+ var OPEN_BRACKET = 91;
2932
+ var CLOSE_BRACKET = 93;
2933
+ var CDATA_NAME = [67, 68, 65, 84, 65, 91];
2934
+ var MAX_PROLOG_BYTES = 16 * 1024 * 1024;
2935
+ var SCAN_MORE = 0;
2936
+ var SCAN_CUT = 1;
2937
+ var SCAN_ROOT_END = 2;
2938
+ var SCAN_UNSUPPORTED = 3;
2939
+ async function readXmlProlog(reader, options) {
2940
+ let root = null;
2941
+ let is_root_closed = false;
2942
+ const parser = new XmlStreamParser({
2943
+ startTag(name, attributes) {
2944
+ if (root)
2945
+ return;
2946
+ root = { name, attributes };
2947
+ options.onRoot?.(name, attributes);
2948
+ },
2949
+ endTag() {
2950
+ is_root_closed = true;
2951
+ }
2952
+ }, options.parserOptions);
2953
+ const pieces = [];
2954
+ let length = 0;
2955
+ let rest = new Uint8Array(0);
2956
+ while (!root) {
2957
+ const chunk = await reader.next();
2958
+ if (!chunk)
2959
+ return { reason: "the input ends before the root element" };
2960
+ let index = 0;
2961
+ try {
2962
+ while (index < chunk.length && !root) {
2963
+ const gt = chunk.indexOf(GT, index);
2964
+ const end = gt < 0 ? chunk.length : gt + 1;
2965
+ parser.write(chunk.subarray(index, end));
2966
+ index = end;
2967
+ }
2968
+ } catch {
2969
+ return { reason: "the prolog or root start tag was rejected" };
2970
+ }
2971
+ pieces.push(chunk.subarray(0, index));
2972
+ length += index;
2973
+ if (root)
2974
+ rest = chunk.subarray(index);
2975
+ if (length > MAX_PROLOG_BYTES)
2976
+ return { reason: "the prolog exceeds 16 MiB" };
2977
+ }
2978
+ if (is_root_closed)
2979
+ return { reason: "the root element is empty" };
2980
+ const prolog = new Uint8Array(length);
2981
+ let offset = 0;
2982
+ for (const piece of pieces) {
2983
+ prolog.set(piece, offset);
2984
+ offset += piece.length;
2985
+ }
2986
+ const accepted = root;
2987
+ const slice_start = Math.floor(length / SLICE_SIZE) * SLICE_SIZE;
2988
+ const slice_offset = new TextDecoder("utf-8", { ignoreBOM: true }).decode(prolog.subarray(0, slice_start), { stream: true }).length;
2989
+ return {
2990
+ prolog: {
2991
+ root_name: accepted.name,
2992
+ root_attributes: accepted.attributes,
2993
+ root_tag: prolog.slice(prolog.lastIndexOf(LT)),
2994
+ body_start: length,
2995
+ body_offset: parser.offset,
2996
+ slice_offset,
2997
+ rest
2998
+ }
2999
+ };
3000
+ }
3001
+ function scanBody(bytes, index, cut_from, state) {
3002
+ const end = bytes.length;
3003
+ let depth = state.depth;
3004
+ for (;; ) {
3005
+ const lt = bytes.indexOf(LT, index);
3006
+ if (lt < 0 || lt + 1 >= end) {
3007
+ state.depth = depth;
3008
+ state.result = SCAN_MORE;
3009
+ return lt < 0 ? end : lt;
3010
+ }
3011
+ const next = bytes[lt + 1];
3012
+ let close = -1;
3013
+ if (next === SLASH) {
3014
+ const gt = bytes.indexOf(GT, lt + 2);
3015
+ if (gt >= 0) {
3016
+ close = gt + 1;
3017
+ if (--depth === 0) {
3018
+ state.depth = 0;
3019
+ state.result = SCAN_ROOT_END;
3020
+ return lt;
3021
+ }
3022
+ if (depth === 1 && close >= cut_from) {
3023
+ state.depth = depth;
3024
+ state.result = SCAN_CUT;
3025
+ return close;
3026
+ }
3027
+ }
3028
+ } else if (next === BANG) {
3029
+ if (lt + 3 >= end)
3030
+ close = -1;
3031
+ else if (bytes[lt + 2] === DASH) {
3032
+ if (bytes[lt + 3] !== DASH)
3033
+ return markUnsupported(state, depth, lt);
3034
+ for (let position = lt + 4;; position++) {
3035
+ position = bytes.indexOf(DASH, position);
3036
+ if (position < 0 || position + 2 >= end)
3037
+ break;
3038
+ if (bytes[position + 1] === DASH) {
3039
+ if (bytes[position + 2] !== GT)
3040
+ return markUnsupported(state, depth, lt);
3041
+ close = position + 3;
3042
+ break;
3043
+ }
3044
+ }
3045
+ } else if (bytes[lt + 2] === OPEN_BRACKET) {
3046
+ if (lt + 9 > end)
3047
+ close = -1;
3048
+ else {
3049
+ for (let offset = 0;offset < CDATA_NAME.length; offset++)
3050
+ if (bytes[lt + 3 + offset] !== CDATA_NAME[offset])
3051
+ return markUnsupported(state, depth, lt);
3052
+ for (let position = lt + 11;; position++) {
3053
+ position = bytes.indexOf(GT, position);
3054
+ if (position < 0)
3055
+ break;
3056
+ if (bytes[position - 1] === CLOSE_BRACKET && bytes[position - 2] === CLOSE_BRACKET) {
3057
+ close = position + 1;
3058
+ break;
3059
+ }
3060
+ }
3061
+ }
3062
+ } else
3063
+ return markUnsupported(state, depth, lt);
3064
+ } else if (next === QUESTION) {
3065
+ for (let position = lt + 2;; position++) {
3066
+ position = bytes.indexOf(GT, position);
3067
+ if (position < 0)
3068
+ break;
3069
+ if (bytes[position - 1] === QUESTION) {
3070
+ close = position + 1;
3071
+ break;
3072
+ }
3073
+ }
3074
+ } else {
3075
+ let position = lt + 1;
3076
+ let quote = 0;
3077
+ for (;position < end; position++) {
3078
+ const unit = bytes[position];
3079
+ if (quote !== 0) {
3080
+ if (unit === quote)
3081
+ quote = 0;
3082
+ } else if (unit === GT)
3083
+ break;
3084
+ else if (unit === DOUBLE_QUOTE || unit === SINGLE_QUOTE)
3085
+ quote = unit;
3086
+ }
3087
+ if (position < end) {
3088
+ close = position + 1;
3089
+ if (bytes[position - 1] !== SLASH)
3090
+ depth++;
3091
+ else if (depth === 1 && close >= cut_from) {
3092
+ state.depth = depth;
3093
+ state.result = SCAN_CUT;
3094
+ return close;
3095
+ }
3096
+ }
3097
+ }
3098
+ if (close < 0) {
3099
+ state.depth = depth;
3100
+ state.result = SCAN_MORE;
3101
+ return lt;
3102
+ }
3103
+ index = close;
3104
+ }
3105
+ }
3106
+ function markUnsupported(state, depth, at) {
3107
+ state.depth = depth;
3108
+ state.result = SCAN_UNSUPPORTED;
3109
+ return at;
3110
+ }
3111
+ async function* splitXmlBody(reader, first, body_start, options) {
3112
+ const state = { depth: 1, result: SCAN_MORE };
3113
+ let buffer = first;
3114
+ let base = body_start;
3115
+ let segment = 0;
3116
+ let position = 0;
3117
+ let is_eof = false;
3118
+ for (;; ) {
3119
+ position = scanBody(buffer, position, segment + options.segment_bytes, state);
3120
+ if (state.result === SCAN_CUT) {
3121
+ if (position - segment > options.max_segment_bytes) {
3122
+ yield {
3123
+ start: base + segment,
3124
+ fallback: "a direct child of the root exceeds the segment limit"
3125
+ };
3126
+ return;
3127
+ }
3128
+ yield {
3129
+ start: base + segment,
3130
+ bytes: new Uint8Array(buffer.subarray(segment, position)),
3131
+ is_final: false
3132
+ };
3133
+ segment = position;
3134
+ continue;
3135
+ }
3136
+ if (state.result === SCAN_UNSUPPORTED) {
3137
+ yield {
3138
+ start: base + segment,
3139
+ fallback: "the body contains markup the splitter does not frame"
3140
+ };
3141
+ return;
3142
+ }
3143
+ if (state.result === SCAN_MORE && !is_eof) {
3144
+ if (buffer.length - segment > options.max_segment_bytes) {
3145
+ yield {
3146
+ start: base + segment,
3147
+ fallback: "a direct child of the root exceeds the segment limit"
3148
+ };
3149
+ return;
3150
+ }
3151
+ const chunk = await reader.next();
3152
+ if (!chunk) {
3153
+ is_eof = true;
3154
+ continue;
3155
+ }
3156
+ const kept = buffer.length - segment;
3157
+ const next = new Uint8Array(kept + chunk.length);
3158
+ next.set(buffer.subarray(segment));
3159
+ next.set(chunk, kept);
3160
+ base += segment;
3161
+ position -= segment;
3162
+ segment = 0;
3163
+ buffer = next;
3164
+ continue;
3165
+ }
3166
+ const parts = [buffer.subarray(segment)];
3167
+ let length = parts[0].length;
3168
+ if (length > options.max_segment_bytes) {
3169
+ yield { start: base + segment, fallback: "the final segment exceeds the segment limit" };
3170
+ return;
3171
+ }
3172
+ for (let chunk = await reader.next();chunk; chunk = await reader.next()) {
3173
+ parts.push(chunk);
3174
+ length += chunk.length;
3175
+ if (length > options.max_segment_bytes) {
3176
+ yield { start: base + segment, fallback: "the final segment exceeds the segment limit" };
3177
+ return;
3178
+ }
3179
+ }
3180
+ const bytes = new Uint8Array(length);
3181
+ let offset = 0;
3182
+ for (const part of parts) {
3183
+ bytes.set(part, offset);
3184
+ offset += part.length;
3185
+ }
3186
+ yield { start: base + segment, bytes, is_final: true };
3187
+ return;
3188
+ }
3189
+ }
3190
+
3191
+ // packages/xml-parser/src/parallel/worker.js
3192
+ var XML_WORKER_PROTOCOL = 2;
3193
+
3194
+ // packages/xml-parser/src/parallel/coordinator.js
3195
+ var MAX_WORKERS = 64;
3196
+ var MAX_DEFAULT_SEGMENT_BYTES = 1024 * 1024;
3197
+ var MIN_DEFAULT_SEGMENT_BYTES = 64 * 1024;
3198
+ var DEFAULT_READ_BYTES = 4 * 1024 * 1024;
3199
+ var DEFAULT_MAX_SEGMENT_BYTES = 64 * 1024 * 1024;
3200
+ var DEFAULT_STARTUP_TIMEOUT_MS = 1e4;
3201
+ var MAX_SEGMENTS_PER_WORKER = 2;
3202
+ function validateInteger(value, name, min, max = Number.MAX_SAFE_INTEGER) {
3203
+ if (typeof value !== "number" || !Number.isSafeInteger(value) || value < min || value > max)
3204
+ throw new RangeError(`${name} must be an integer from ${min} to ${max}`);
3205
+ return value;
3206
+ }
3207
+ function restoreXmlError(description, offset = 0) {
3208
+ if (description.name === "XmlParseError")
3209
+ return new XmlParseError(description.message, description.code ?? "SYNTAX", offset);
3210
+ if (description.name === "RangeError")
3211
+ return new RangeError(description.message);
3212
+ if (description.name === "TypeError")
3213
+ return new TypeError(description.message);
3214
+ return new Error(description.message);
3215
+ }
3216
+ function describeReason(error) {
3217
+ if (error && typeof error === "object" && "message" in error && error.message)
3218
+ return String(error.message);
3219
+ return "unknown error";
3220
+ }
3221
+ function startXmlWorkers({ count, createWorker, task_name, signal }) {
3222
+ const slots = [];
3223
+ const results = new Map;
3224
+ const failures = [];
3225
+ let fatal = null;
3226
+ let is_terminated = false;
3227
+ let wake = () => {};
3228
+ let woken = new Promise((resolve) => wake = resolve);
3229
+ function notify() {
3230
+ const resolve = wake;
3231
+ woken = new Promise((next) => wake = next);
3232
+ resolve();
3233
+ }
3234
+ function terminate() {
3235
+ if (is_terminated)
3236
+ return;
3237
+ is_terminated = true;
3238
+ signal?.removeEventListener("abort", onAbort);
3239
+ for (const slot of slots)
3240
+ stop(slot.worker);
3241
+ results.clear();
3242
+ }
3243
+ function onAbort() {
3244
+ terminate();
3245
+ notify();
3246
+ }
3247
+ signal?.addEventListener("abort", onAbort, { once: true });
3248
+ function stop(worker) {
3249
+ try {
3250
+ Promise.resolve(worker.terminate()).catch(() => {});
3251
+ } catch {}
3252
+ }
3253
+ for (let index = 0;index < count; index++) {
3254
+ let worker;
3255
+ try {
3256
+ worker = createWorker();
3257
+ } catch (error) {
3258
+ failures.push(describeReason(error));
3259
+ continue;
3260
+ }
3261
+ const slot = { worker, busy: 0, state: "starting" };
3262
+ slots.push(slot);
3263
+ const fail = (reason) => {
3264
+ if (is_terminated)
3265
+ return;
3266
+ if (slot.state === "ready")
3267
+ fatal ??= new Error(`An XML worker failed: ${reason}`);
3268
+ else if (slot.state === "starting") {
3269
+ slot.state = "failed";
3270
+ failures.push(reason);
3271
+ stop(worker);
3272
+ }
3273
+ notify();
3274
+ };
3275
+ try {
3276
+ worker.addEventListener("message", (event) => {
3277
+ if (is_terminated)
3278
+ return;
3279
+ const data = event?.data;
3280
+ if (slot.state === "starting") {
3281
+ if (data?.type === "ready" && data.protocol === XML_WORKER_PROTOCOL && data.task === task_name) {
3282
+ slot.state = "ready";
3283
+ notify();
3284
+ } else if (data?.type === "refused")
3285
+ fail("the worker entry serves another task or protocol version");
3286
+ else
3287
+ fail("the worker sent an unexpected message before it was ready");
3288
+ return;
3289
+ }
3290
+ if (slot.state !== "ready")
3291
+ return;
3292
+ if (data?.type === "segment") {
3293
+ slot.busy--;
3294
+ results.set(data.seq, data.result);
3295
+ } else if (data?.type === "error")
3296
+ fatal ??= new Error(`An XML worker failed: ${data.error?.message}`);
3297
+ else
3298
+ fatal ??= new Error("An XML worker sent an unexpected message.");
3299
+ notify();
3300
+ });
3301
+ worker.addEventListener("error", (event) => {
3302
+ event?.preventDefault?.();
3303
+ fail(event?.message || describeReason(event?.error));
3304
+ });
3305
+ worker.addEventListener("messageerror", () => fail("a worker message could not be read"));
3306
+ worker.postMessage({ type: "hello", protocol: XML_WORKER_PROTOCOL, task: task_name }, []);
3307
+ } catch (error) {
3308
+ fail(describeReason(error));
3309
+ }
3310
+ }
3311
+ return {
3312
+ results,
3313
+ get ready() {
3314
+ return slots.filter((slot) => slot.state === "ready");
3315
+ },
3316
+ get reason() {
3317
+ if (!failures.length)
3318
+ return;
3319
+ return `${failures.length} of ${count} XML workers did not start: ${failures[0]}`;
3320
+ },
3321
+ async whenStarted(timeout_ms) {
3322
+ let is_timed_out = false;
3323
+ const timer = setTimeout(() => {
3324
+ is_timed_out = true;
3325
+ notify();
3326
+ }, timeout_ms);
3327
+ try {
3328
+ while (!is_timed_out && slots.some((slot) => slot.state === "starting")) {
3329
+ checkXmlAbort(signal);
3330
+ await woken;
3331
+ }
3332
+ checkXmlAbort(signal);
3333
+ } finally {
3334
+ clearTimeout(timer);
3335
+ }
3336
+ for (const slot of slots) {
3337
+ if (slot.state !== "starting")
3338
+ continue;
3339
+ slot.state = "failed";
3340
+ failures.push(`no ready reply within ${timeout_ms} ms`);
3341
+ stop(slot.worker);
3342
+ }
3343
+ },
3344
+ configure(config) {
3345
+ for (const slot of slots)
3346
+ if (slot.state === "ready")
3347
+ slot.worker.postMessage({ type: "config", config }, []);
3348
+ },
3349
+ leastBusy() {
3350
+ const ready = slots.filter((slot) => slot.state === "ready");
3351
+ return ready.reduce((best, slot) => slot.busy < best.busy ? slot : best, ready[0]);
3352
+ },
3353
+ dispatch(slot, message, transfer) {
3354
+ slot.busy++;
3355
+ slot.worker.postMessage(message, transfer);
3356
+ },
3357
+ throwIfFailed() {
3358
+ if (fatal)
3359
+ throw fatal;
3360
+ },
3361
+ wait() {
3362
+ return woken;
3363
+ },
3364
+ terminate
3365
+ };
3366
+ }
3367
+ async function* readXmlSegments(source, options) {
3368
+ const task = options.task;
3369
+ if (!task || typeof task.name !== "string" || typeof task.createSink !== "function")
3370
+ throw new TypeError("task must provide a name and createSink(options)");
3371
+ if (typeof options.createWorker !== "function")
3372
+ throw new TypeError("createWorker must be a function");
3373
+ if (!source || !Number.isSafeInteger(source.size) || source.size < 0 || typeof source.read !== "function")
3374
+ throw new TypeError("source must provide size and read(start, end)");
3375
+ const requested = validateInteger(options.workers, "workers", 1, MAX_WORKERS);
3376
+ const select_path = options.selectPath ? Array.from(options.selectPath) : null;
3377
+ if (select_path && (!select_path.length || select_path.some((name) => typeof name !== "string" || !name)))
3378
+ throw new TypeError("selectPath must list element names from the root");
3379
+ if (options.onRoot !== undefined && typeof options.onRoot !== "function")
3380
+ throw new TypeError("onRoot must be a function");
3381
+ const max_record_chars = validateInteger(options.maxRecordChars ?? 4 * 1024 * 1024, "maxRecordChars", 1);
3382
+ createWhitespaceFilter(options.whitespace);
3383
+ const max_record_nodes = validateInteger(options.maxRecordNodes ?? 1e5, "maxRecordNodes", 1);
3384
+ const read_bytes = validateInteger(options.readBytes ?? DEFAULT_READ_BYTES, "readBytes", SLICE_SIZE);
3385
+ if (read_bytes % SLICE_SIZE)
3386
+ throw new RangeError(`readBytes must be a multiple of ${SLICE_SIZE}`);
3387
+ if (options.segmentBytes !== undefined)
3388
+ validateInteger(options.segmentBytes, "segmentBytes", SLICE_SIZE);
3389
+ if (options.maxSegmentBytes !== undefined)
3390
+ validateInteger(options.maxSegmentBytes, "maxSegmentBytes", options.segmentBytes ?? SLICE_SIZE);
3391
+ if (options.maxInFlight !== undefined)
3392
+ validateInteger(options.maxInFlight, "maxInFlight", 1);
3393
+ const startup_timeout = validateInteger(options.startupTimeout ?? DEFAULT_STARTUP_TIMEOUT_MS, "startupTimeout", 1);
3394
+ const signal = options.signal;
3395
+ const describeRecordError = task.describeError ?? describeXmlError;
3396
+ function restoreRecordError(description, record_number) {
3397
+ if (task.restoreError)
3398
+ return task.restoreError(description, record_number);
3399
+ return restoreXmlError(description);
3400
+ }
3401
+ checkXmlAbort(signal);
3402
+ const pool = startXmlWorkers({
3403
+ count: requested,
3404
+ createWorker: options.createWorker,
3405
+ task_name: task.name,
3406
+ signal
3407
+ });
3408
+ function checkActive() {
3409
+ checkXmlAbort(signal);
3410
+ pool.throwIfFailed();
3411
+ }
3412
+ let segments = null;
3413
+ try {
3414
+ let captureState = function(start) {
3415
+ return { start, doc_offset, has_element_children, root_chars, root_nodes };
3416
+ }, restoreFailure = function(failure, base, slice_offset = null) {
3417
+ if (failure.phase === "record")
3418
+ return restoreRecordError(failure.error, emitted + 1);
3419
+ const description = failure.error;
3420
+ if (description.code === "UTF8" && slice_offset !== null)
3421
+ return restoreXmlError(description, slice_offset);
3422
+ return restoreXmlError(description, base + (description.offset ?? 0));
3423
+ }, isRootContextStale = function(result) {
3424
+ const dispatched = result.dispatched;
3425
+ if (!is_document || !dispatched)
3426
+ return false;
3427
+ if (dispatched.root_chars === root_chars && dispatched.root_nodes === root_nodes)
3428
+ return false;
3429
+ const check = result.summary.root_check;
3430
+ return Boolean(result.failure?.is_root_limit || check && (root_chars + check.chars > max_record_chars || root_nodes + check.nodes > max_record_nodes));
3431
+ }, advance = function(result) {
3432
+ doc_offset += result.length;
3433
+ for (const text of result.summary.kept) {
3434
+ kept.push(text);
3435
+ root_chars += text.length;
3436
+ root_nodes++;
3437
+ }
3438
+ has_element_children = result.summary.has_element_children;
3439
+ }, createContinuation = function(state) {
3440
+ const continuation_reader = new XmlSourceReader(source, state.start, read_bytes, signal);
3441
+ const processor = createXmlSegmentProcessor(config, {
3442
+ start: state.start,
3443
+ is_final: true,
3444
+ has_element_children: state.has_element_children,
3445
+ root_chars: state.root_chars,
3446
+ root_nodes: state.root_nodes
3447
+ }, task.createSink(options.taskOptions), describeRecordError);
3448
+ let position = state.start;
3449
+ const empty = { output: undefined, count: 0 };
3450
+ return {
3451
+ async next() {
3452
+ const start = position;
3453
+ const chunk = await continuation_reader.next();
3454
+ if (chunk) {
3455
+ processor.push(chunk);
3456
+ position += chunk.length;
3457
+ return {
3458
+ start,
3459
+ end: position,
3460
+ is_final: false,
3461
+ has_shared_tail: false,
3462
+ base: state.doc_offset,
3463
+ body: processor.take(),
3464
+ tail: empty,
3465
+ failure: processor.failure,
3466
+ length: 0,
3467
+ last_write_offset: 0,
3468
+ summary: {
3469
+ kept: [],
3470
+ has_element_children: processor.summary().has_element_children,
3471
+ root_check: null
3472
+ }
3473
+ };
3474
+ }
3475
+ processor.close();
3476
+ return {
3477
+ start,
3478
+ end: position,
3479
+ is_final: true,
3480
+ has_shared_tail: false,
3481
+ base: state.doc_offset,
3482
+ body: empty,
3483
+ tail: processor.take(),
3484
+ failure: processor.failure,
3485
+ length: 0,
3486
+ last_write_offset: 0,
3487
+ summary: processor.summary()
3488
+ };
3489
+ }
3490
+ };
3491
+ };
3492
+ const reader = new XmlSourceReader(source, 0, read_bytes, signal);
3493
+ const framed = await readXmlProlog(reader, {
3494
+ parserOptions: options.parserOptions,
3495
+ onRoot: options.onRoot
3496
+ });
3497
+ const prolog = framed.prolog;
3498
+ let reason = framed.reason;
3499
+ let root_chars = 0;
3500
+ let root_nodes = 1;
3501
+ if (prolog) {
3502
+ root_chars = prolog.root_name.length;
3503
+ for (const key of Object.keys(prolog.root_attributes))
3504
+ root_chars += key.length + prolog.root_attributes[key].length;
3505
+ if (select_path && select_path.length < 2)
3506
+ reason = "selectPath selects the root element";
3507
+ else if (!select_path && root_chars > max_record_chars)
3508
+ reason = "the root start tag exceeds maxRecordChars";
3509
+ }
3510
+ if (!prolog || reason) {
3511
+ checkActive();
3512
+ yield { type: "sequential", reason: `the document cannot be split: ${reason}` };
3513
+ checkActive();
3514
+ return;
3515
+ }
3516
+ await pool.whenStarted(startup_timeout);
3517
+ const ready_count = pool.ready.length;
3518
+ if (!ready_count) {
3519
+ checkActive();
3520
+ yield { type: "sequential", reason: pool.reason ?? "no XML worker started" };
3521
+ checkActive();
3522
+ return;
3523
+ }
3524
+ checkActive();
3525
+ yield { type: "workers", actual: ready_count, ...pool.reason ? { reason: pool.reason } : {} };
3526
+ checkActive();
3527
+ const segment_bytes = options.segmentBytes ?? Math.min(MAX_DEFAULT_SEGMENT_BYTES, Math.max(MIN_DEFAULT_SEGMENT_BYTES, Math.ceil(source.size / (8 * ready_count))));
3528
+ const max_segment_bytes = options.maxSegmentBytes ?? Math.max(DEFAULT_MAX_SEGMENT_BYTES, 4 * segment_bytes);
3529
+ const max_in_flight = options.maxInFlight ?? 2 * ready_count;
3530
+ const config = {
3531
+ select_path,
3532
+ whitespace: options.whitespace,
3533
+ parser_options: options.parserOptions,
3534
+ max_record_chars,
3535
+ max_record_nodes,
3536
+ root_tag: prolog.root_tag,
3537
+ root_name: prolog.root_name,
3538
+ task_options: options.taskOptions
3539
+ };
3540
+ pool.configure(config);
3541
+ const is_document = !select_path;
3542
+ let emitted = 0;
3543
+ let doc_offset = prolog.body_offset;
3544
+ let has_element_children = false;
3545
+ const kept = [];
3546
+ let held = {
3547
+ slice: Math.floor(prolog.body_start / SLICE_SIZE),
3548
+ offset: prolog.slice_offset,
3549
+ outputs: [],
3550
+ failure: null,
3551
+ restart: null
3552
+ };
3553
+ function* release(outputs) {
3554
+ for (const entry of outputs) {
3555
+ checkActive();
3556
+ if (!entry.count)
3557
+ continue;
3558
+ emitted += entry.count;
3559
+ yield { type: "records", output: entry.output, count: entry.count, end: entry.end };
3560
+ checkActive();
3561
+ }
3562
+ }
3563
+ function* merge(result) {
3564
+ if (result.failure?.phase === "boundary")
3565
+ return {
3566
+ ...captureState(result.start),
3567
+ reason: "a segment did not end at a clean root-child boundary"
3568
+ };
3569
+ if (isRootContextStale(result))
3570
+ return {
3571
+ ...captureState(result.start),
3572
+ reason: "root text limits depend on earlier segments"
3573
+ };
3574
+ const base = result.base ?? doc_offset;
3575
+ const head_slice = Math.floor(result.start / SLICE_SIZE);
3576
+ const head_failure = held && result.start % SLICE_SIZE !== 0 && held.slice === head_slice && result.failure?.phase === "write" && result.failure.slice === head_slice ? result.failure : null;
3577
+ if (held) {
3578
+ const previous = held;
3579
+ held = null;
3580
+ if (previous.failure) {
3581
+ if (head_failure) {
3582
+ if (previous.failure.failure.phase !== "record")
3583
+ return {
3584
+ ...previous.restart,
3585
+ reason: "errors on both sides of a segment boundary"
3586
+ };
3587
+ throw restoreFailure(head_failure, base, previous.offset);
3588
+ }
3589
+ yield* release(previous.outputs);
3590
+ throw restoreFailure(previous.failure.failure, previous.failure.base);
3591
+ }
3592
+ if (head_failure)
3593
+ throw restoreFailure(head_failure, base, previous.offset);
3594
+ yield* release(previous.outputs);
3595
+ }
3596
+ const tail_slice = Math.floor((result.end - 1) / SLICE_SIZE);
3597
+ const body = {
3598
+ ...result.body,
3599
+ end: Math.max(result.start, Math.floor(result.end / SLICE_SIZE) * SLICE_SIZE)
3600
+ };
3601
+ const tail = { ...result.tail, end: result.end };
3602
+ const failure = result.failure;
3603
+ if (failure) {
3604
+ const is_shared = result.has_shared_tail && failure.slice === tail_slice && !(failure.phase === "write" && failure.error.code === "UTF8");
3605
+ if (!is_shared) {
3606
+ yield* release([body, tail]);
3607
+ throw restoreFailure(failure, base);
3608
+ }
3609
+ const offset = base + result.last_write_offset;
3610
+ if (failure.phase === "record") {
3611
+ yield* release([body]);
3612
+ advance(result);
3613
+ held = {
3614
+ slice: tail_slice,
3615
+ offset,
3616
+ outputs: [tail],
3617
+ failure: { failure, base },
3618
+ restart: null
3619
+ };
3620
+ } else
3621
+ held = {
3622
+ slice: tail_slice,
3623
+ offset,
3624
+ outputs: [body, tail],
3625
+ failure: { failure, base },
3626
+ restart: captureState(result.start)
3627
+ };
3628
+ return null;
3629
+ }
3630
+ yield* release([body]);
3631
+ advance(result);
3632
+ held = {
3633
+ slice: tail_slice,
3634
+ offset: base + result.last_write_offset,
3635
+ outputs: [tail],
3636
+ failure: null,
3637
+ restart: null
3638
+ };
3639
+ if (!result.is_final)
3640
+ return null;
3641
+ const last = held;
3642
+ held = null;
3643
+ yield* release(last.outputs);
3644
+ return "final";
3645
+ }
3646
+ let next_seq = 0;
3647
+ let dispatch_seq = 0;
3648
+ const dispatched = new Map;
3649
+ segments = splitXmlBody(reader, prolog.rest, prolog.body_start, {
3650
+ segment_bytes,
3651
+ max_segment_bytes
3652
+ });
3653
+ let is_split_done = false;
3654
+ let split_fallback = null;
3655
+ let fallback = null;
3656
+ let continuation = null;
3657
+ let is_complete = false;
3658
+ while (!is_complete) {
3659
+ checkActive();
3660
+ if (fallback) {
3661
+ pool.terminate();
3662
+ dispatched.clear();
3663
+ is_split_done = true;
3664
+ await segments.return(undefined);
3665
+ continuation = createContinuation(fallback);
3666
+ checkActive();
3667
+ yield { type: "fallback", start: fallback.start, reason: fallback.reason };
3668
+ checkActive();
3669
+ fallback = null;
3670
+ }
3671
+ if (continuation) {
3672
+ const action = yield* merge(await continuation.next());
3673
+ if (action === "final")
3674
+ is_complete = true;
3675
+ else if (action)
3676
+ fallback = action;
3677
+ continue;
3678
+ }
3679
+ pool.throwIfFailed();
3680
+ while (!is_split_done && dispatch_seq - next_seq < max_in_flight && !(held?.failure && dispatch_seq > next_seq)) {
3681
+ const slot = pool.leastBusy();
3682
+ if (slot.busy >= MAX_SEGMENTS_PER_WORKER)
3683
+ break;
3684
+ const { value, done } = await segments.next();
3685
+ checkActive();
3686
+ if (done) {
3687
+ is_split_done = true;
3688
+ break;
3689
+ }
3690
+ if (value.fallback !== undefined) {
3691
+ is_split_done = true;
3692
+ split_fallback = { start: value.start, reason: value.fallback };
3693
+ break;
3694
+ }
3695
+ const seq = dispatch_seq++;
3696
+ const state = {
3697
+ start: value.start,
3698
+ is_final: value.is_final,
3699
+ has_element_children: seq > 0,
3700
+ root_chars,
3701
+ root_nodes
3702
+ };
3703
+ dispatched.set(seq, { ...state, end: value.start + value.bytes.length });
3704
+ pool.dispatch(slot, { type: "segment", seq, state, bytes: value.bytes }, [
3705
+ value.bytes.buffer
3706
+ ]);
3707
+ }
3708
+ let is_merged = false;
3709
+ while (pool.results.has(next_seq)) {
3710
+ const result = pool.results.get(next_seq);
3711
+ const meta = dispatched.get(next_seq);
3712
+ pool.results.delete(next_seq);
3713
+ dispatched.delete(next_seq);
3714
+ next_seq++;
3715
+ is_merged = true;
3716
+ const action = yield* merge({
3717
+ ...result,
3718
+ start: meta.start,
3719
+ end: meta.end,
3720
+ is_final: meta.is_final,
3721
+ has_shared_tail: !meta.is_final && meta.end % SLICE_SIZE !== 0,
3722
+ dispatched: meta
3723
+ });
3724
+ if (action === "final")
3725
+ is_complete = true;
3726
+ else if (action)
3727
+ fallback = action;
3728
+ if (action)
3729
+ break;
3730
+ }
3731
+ if (is_complete || fallback)
3732
+ continue;
3733
+ if (is_split_done && next_seq === dispatch_seq) {
3734
+ if (!split_fallback)
3735
+ throw new Error("The XML splitter ended without a final segment.");
3736
+ fallback = { ...captureState(split_fallback.start), reason: split_fallback.reason };
3737
+ continue;
3738
+ }
3739
+ if (!is_merged) {
3740
+ checkXmlAbort(signal);
3741
+ pool.throwIfFailed();
3742
+ await pool.wait();
3743
+ }
3744
+ }
3745
+ checkActive();
3746
+ if (is_document) {
3747
+ const has_root_attributes = Object.keys(prolog.root_attributes).length > 0;
3748
+ if (!has_element_children || has_root_attributes || kept.length) {
3749
+ const sink = task.createSink(options.taskOptions);
3750
+ try {
3751
+ sink.add({ name: prolog.root_name, attributes: prolog.root_attributes, children: kept });
3752
+ } catch (error) {
3753
+ throw restoreRecordError(describeRecordError(error), emitted + 1);
3754
+ }
3755
+ emitted++;
3756
+ checkActive();
3757
+ yield { type: "records", output: sink.take(), count: 1, end: source.size };
3758
+ checkActive();
3759
+ }
3760
+ }
3761
+ } finally {
3762
+ pool.terminate();
3763
+ await segments?.return(undefined);
3764
+ }
3765
+ }
3766
+ // packages/import/src/detect.js
3767
+ var import_papaparse = __toESM(require_papaparse(), 1);
3768
+ async function detectImport(source, options = {}) {
3769
+ if (options.format && options.format !== "auto" && !Object.hasOwn(IMPORT_FORMATS, options.format))
3770
+ throw new Error(`Unsupported import format: ${options.format}`);
3771
+ const decoder = new TextDecoder("utf-8", { fatal: true });
3772
+ let text = "";
3773
+ let bytes_read = 0;
3774
+ let is_sampled = false;
3775
+ for await (const bytes of readImportBytes(source, options.signal)) {
3776
+ const part = bytes.subarray(0, IMPORT_SNIFF_BYTES - bytes_read);
3777
+ bytes_read += part.length;
3778
+ text += decoder.decode(part, { stream: true });
3779
+ if (bytes_read >= IMPORT_SNIFF_BYTES) {
3780
+ is_sampled = true;
3781
+ break;
3782
+ }
3783
+ }
3784
+ if (!is_sampled)
3785
+ text += decoder.decode();
3786
+ const trimmed = text.trimStart();
3787
+ if (!trimmed && (!is_sampled || !options.format || options.format === "auto"))
3788
+ throw new Error("This file is empty or has no recognizable data in its first 256 KiB.");
3789
+ let format = options.format === "auto" ? undefined : options.format;
3790
+ const warnings = [];
3791
+ if (!format) {
3792
+ if (trimmed.startsWith("<"))
3793
+ format = "xml";
3794
+ else if (/^[\[{]/.test(trimmed)) {
3795
+ if (trimmed.startsWith("["))
3796
+ format = "json";
3797
+ else {
3798
+ const lines = trimmed.split(/\r?\n/).filter((line) => line.trim());
3799
+ if (is_sampled)
3800
+ lines.pop();
3801
+ let has_json_lines = lines.length > 1;
3802
+ try {
3803
+ for (const line of lines) {
3804
+ const value = JSON.parse(line);
3805
+ if (!value || typeof value !== "object" || Array.isArray(value))
3806
+ has_json_lines = false;
3807
+ }
3808
+ } catch {
3809
+ has_json_lines = false;
3810
+ }
3811
+ const has_jsonl_hint = /\.(?:jsonl|ndjson)$/i.test(source.name) && (lines.length <= 1 || has_json_lines);
3812
+ format = has_json_lines || has_jsonl_hint ? "jsonl" : "json";
3813
+ }
3814
+ } else {
3815
+ const candidates = [IMPORT_FORMATS.csv, IMPORT_FORMATS.tsv].filter((candidate) => {
3816
+ const result = import_papaparse.default.parse(text, {
3817
+ delimiter: candidate === "tsv" ? "\t" : ",",
3818
+ preview: 4,
3819
+ skipEmptyLines: "greedy"
3820
+ });
3821
+ const rows = result.data;
3822
+ return rows.length >= 1 && rows[0].length > 1 && !result.errors.some((error) => error.code !== "MissingQuotes" && error.code !== "InvalidQuotes") && rows.slice(1, is_sampled ? -1 : undefined).every((row) => row.length === rows[0].length);
3823
+ });
3824
+ if (candidates.length === 1)
3825
+ format = candidates[0];
3826
+ else if (candidates.length > 1) {
3827
+ const extension = source.name.split(".").pop()?.toLowerCase();
3828
+ if (extension === "csv" || extension === "tsv") {
3829
+ format = extension;
3830
+ warnings.push("Both delimiters occur in this sample; the filename selected the CSV/TSV dialect. Verify the preview.");
3831
+ } else
3832
+ throw new Error("The delimiter is ambiguous. Choose CSV or TSV explicitly.");
3833
+ } else
3834
+ throw new Error("Could not detect XML, JSONL, JSON, CSV or TSV from the content. Select a format explicitly for single-column CSV/TSV.");
3835
+ }
3836
+ }
3837
+ const detection = { format, is_sampled, bytes_read, warnings };
3838
+ if (format === "xml") {
3839
+ const discovery = createXmlDiscovery();
3840
+ discovery.parser.write(text);
3841
+ if (!is_sampled)
3842
+ discovery.parser.end();
3843
+ const result = discovery.finish();
3844
+ if (!result.xml.root_name) {
3845
+ if (!is_sampled)
3846
+ throw new Error("No XML root element was found in the input.");
3847
+ warnings.push("No XML root element was found in the first 256 KiB; recognized XML content will be validated while reading the remaining input.");
3848
+ }
3849
+ detection.xml = result.xml;
3850
+ warnings.push(...result.warnings);
3851
+ }
3852
+ if (is_sampled)
3853
+ warnings.push("Detection used a bounded content sample; it does not validate the complete file.");
3854
+ return detection;
3855
+ }
3856
+ function createXmlDiscovery() {
3857
+ const path = [];
3858
+ const paths = new Map;
3859
+ let root_name = null;
3860
+ let has_truncated_paths = false;
3861
+ let has_doctype = false;
3862
+ const parser = new XmlStreamParser({
3863
+ startTag(name) {
3864
+ path.push(name);
3865
+ if (path.length === 1) {
3866
+ root_name = name;
3867
+ if (name.toLowerCase().split(":").pop() === "html")
3868
+ throw new Error("HTML documents are not supported XML imports.");
3869
+ } else {
3870
+ const key = "/" + path.join("/");
3871
+ if (paths.has(key))
3872
+ paths.set(key, (paths.get(key) ?? 0) + 1);
3873
+ else if (paths.size < 256 && key.length <= 4096)
3874
+ paths.set(key, 1);
3875
+ else
3876
+ has_truncated_paths = true;
3877
+ }
3878
+ },
3879
+ endTag() {
3880
+ path.pop();
3881
+ },
3882
+ doctype() {
3883
+ has_doctype = true;
3884
+ }
3885
+ }, { doctype: "ignore" });
3886
+ function finish() {
3887
+ const record_paths = Array.from(paths, ([path, count]) => ({ path, count }));
3888
+ const repeated = record_paths.filter((entry) => entry.count > 1).sort((a, b) => b.count - a.count || a.path.split("/").length - b.path.split("/").length);
3889
+ const direct = record_paths.filter((entry) => entry.path.split("/").length === 3);
3890
+ let recommended_path = null;
3891
+ if (repeated.length && (repeated.length === 1 || repeated[0].count > repeated[1].count || repeated[0].path.split("/").length < repeated[1].path.split("/").length))
3892
+ recommended_path = repeated[0].path;
3893
+ else if (direct.length === 1)
3894
+ recommended_path = direct[0].path;
3895
+ const warnings = [];
3896
+ if (has_truncated_paths)
3897
+ warnings.push("XML path discovery was truncated at 256 paths or 4096 characters per path.");
3898
+ if (has_doctype)
3899
+ warnings.push("The XML DOCTYPE is framed but not applied; external resources and declared entities are not loaded.");
3900
+ return { xml: { root_name, record_paths, recommended_path }, warnings };
3901
+ }
3902
+ return { parser, finish };
3903
+ }
3904
+ // packages/import/src/read.js
3905
+ var import_papaparse2 = __toESM(require_papaparse(), 1);
3906
+
3907
+ // packages/import/src/errors.js
3908
+ class ImportRowError extends Error {
3909
+ constructor(index, detail) {
3910
+ super(`Row ${index}${detail}`);
3911
+ this.index = index;
3912
+ this.detail = detail;
3913
+ }
3914
+ }
3915
+
3916
+ // packages/import/src/timestamp.js
3917
+ function getImportField(record, path) {
3918
+ if (!path.startsWith("/"))
3919
+ return Object.hasOwn(record, path) ? record[path] : undefined;
3920
+ let value = record;
3921
+ for (const raw of path.slice(1).split("/")) {
3922
+ if (/~(?:[^01]|$)/.test(raw))
3923
+ return;
3924
+ const key = raw.replace(/~1/g, "/").replace(/~0/g, "~");
3925
+ if (value === null || typeof value !== "object" || !Object.hasOwn(value, key))
3926
+ return;
3927
+ value = value[key];
3928
+ }
3929
+ return value;
3930
+ }
3931
+ function parseImportTimestamp(value) {
3932
+ if (typeof value === "number" || typeof value === "string" && /^\d{10}(?:\d{3})?$/.test(value)) {
3933
+ const number = Number(value);
3934
+ if (!Number.isSafeInteger(number) || number < 1e9)
3935
+ return null;
3936
+ const digits = String(number).length;
3937
+ if (digits !== 10 && digits !== 13)
3938
+ return null;
3939
+ const date = new Date(digits === 10 ? number * 1000 : number);
3940
+ return Number.isNaN(date.valueOf()) ? null : date.toISOString();
3941
+ }
3942
+ if (typeof value !== "string")
3943
+ return null;
3944
+ const match = /^(\d{4})-(\d{2})-(\d{2})[T ](\d{2}):(\d{2}):(\d{2})(?:\.(\d{1,3}))? ?(Z|[+-]\d{2}:?\d{2})$/.exec(value);
3945
+ if (!match)
3946
+ return null;
3947
+ const [
3948
+ ,
3949
+ year_text,
3950
+ month_text,
3951
+ day_text,
3952
+ hour_text,
3953
+ minute_text,
3954
+ second_text,
3955
+ fraction = "",
3956
+ zone
3957
+ ] = match;
3958
+ const [year, month, day, hour, minute, second] = [
3959
+ year_text,
3960
+ month_text,
3961
+ day_text,
3962
+ hour_text,
3963
+ minute_text,
3964
+ second_text
3965
+ ].map(Number);
3966
+ if (year < 1 || month < 1 || month > 12 || day < 1 || hour > 23 || minute > 59 || second > 59)
3967
+ return null;
3968
+ const date = new Date(0);
3969
+ date.setUTCFullYear(year, month - 1, day);
3970
+ date.setUTCHours(hour, minute, second, Number(fraction.padEnd(3, "0")));
3971
+ if (date.getUTCFullYear() !== year || date.getUTCMonth() !== month - 1 || date.getUTCDate() !== day)
3972
+ return null;
3973
+ let offset = 0;
3974
+ if (zone !== "Z") {
3975
+ const digits = zone.slice(1).replace(":", "");
3976
+ const hours = Number(digits.slice(0, 2));
3977
+ const minutes = Number(digits.slice(2));
3978
+ if (hours > 14 || minutes > 59 || hours === 14 && minutes !== 0 || /^-00:?00$/.test(zone))
3979
+ return null;
3980
+ offset = (hours * 60 + minutes) * (zone[0] === "-" ? -1 : 1);
3981
+ }
3982
+ return new Date(date.valueOf() - offset * 60000).toISOString();
3983
+ }
3984
+ function isAmbiguousTimestamp(value) {
3985
+ return typeof value === "string" && (/^\d{4}-\d{2}-\d{2}(?:[T ]\d{2}:\d{2}(?::\d{2}(?:\.\d+)?)?)?$/.test(value) || /-00:?00$/.test(value));
3986
+ }
3987
+ function getRowTimestamp(row, timestamp_field) {
3988
+ if (!timestamp_field)
3989
+ return null;
3990
+ const value = getImportField(row.record, timestamp_field);
3991
+ const timestamp = parseImportTimestamp(value);
3992
+ if (!timestamp)
3993
+ throw new ImportRowError(row.index, `: timestamp field ${JSON.stringify(timestamp_field)} is missing, invalid or timezone-ambiguous. Choose an explicit timezone or a different field.`);
3994
+ const milliseconds = Date.parse(timestamp);
3995
+ if (milliseconds < 0)
3996
+ throw new ImportRowError(row.index, `: timestamp field ${JSON.stringify(timestamp_field)} is before 1970-01-01T00:00:00Z and cannot be used as an import timestamp.`);
3997
+ if (milliseconds > Date.now())
3998
+ throw new ImportRowError(row.index, `: timestamp field ${JSON.stringify(timestamp_field)} is in the future and cannot be used as an import timestamp.`);
3999
+ return timestamp;
4000
+ }
4001
+
4002
+ // packages/import/src/batch.js
4003
+ var encoder2 = new TextEncoder;
4004
+ function serializeImportRow(row) {
4005
+ const line = JSON.stringify(row.record);
4006
+ if (line === undefined || line.length + 1 > IMPORT_BATCH_BYTES)
4007
+ throw new ImportRowError(row.index, " exceeds the 1 MiB record limit.");
4008
+ const bytes = encoder2.encode(line).byteLength + 1;
4009
+ if (bytes > IMPORT_BATCH_BYTES)
4010
+ throw new ImportRowError(row.index, " exceeds the 1 MiB record limit.");
4011
+ return { row, line, bytes };
4012
+ }
4013
+ async function* createImportBatches(rows, options = {}) {
4014
+ const max_bytes = options.max_bytes ?? IMPORT_BATCH_BYTES;
4015
+ const max_rows = options.max_rows ?? IMPORT_BATCH_ROWS;
4016
+ if (!Number.isSafeInteger(max_bytes) || max_bytes < 1 || max_bytes > IMPORT_BATCH_BYTES)
4017
+ throw new RangeError("max_bytes must be between 1 and 1048576.");
4018
+ if (!Number.isSafeInteger(max_rows) || max_rows < 1 || max_rows > IMPORT_BATCH_ROWS)
4019
+ throw new RangeError("max_rows must be between 1 and 500.");
4020
+ let batch_rows = [];
4021
+ let body = "";
4022
+ let bytes = 0;
4023
+ let index = 0;
4024
+ let timestamp = null;
4025
+ function finish() {
4026
+ const batch = { index: ++index, body, bytes, rows: batch_rows, timestamp };
4027
+ batch_rows = [];
4028
+ body = "";
4029
+ bytes = 0;
4030
+ return batch;
4031
+ }
4032
+ for await (const row of rows) {
4033
+ const entry = serializeImportRow(row);
4034
+ const next_timestamp = getRowTimestamp(row, options.timestamp_field);
4035
+ if (entry.bytes > max_bytes)
4036
+ throw new Error(`Row ${row.index} exceeds max_bytes (${max_bytes}).`);
4037
+ if (batch_rows.length && (bytes + entry.bytes > max_bytes || timestamp !== next_timestamp))
4038
+ yield finish();
4039
+ timestamp = next_timestamp;
4040
+ batch_rows.push(row);
4041
+ body += entry.line + `
4042
+ `;
4043
+ bytes += entry.bytes;
4044
+ if (batch_rows.length === max_rows || bytes === max_bytes)
4045
+ yield finish();
4046
+ }
4047
+ if (batch_rows.length)
4048
+ yield finish();
4049
+ }
4050
+
4051
+ // packages/import/src/row.js
4052
+ function createImportRow(record, index) {
4053
+ if (!record || typeof record !== "object" || Array.isArray(record))
4054
+ throw new ImportRowError(index, " must be a JSON object.");
4055
+ const row = { index, record };
4056
+ serializeImportRow(row);
4057
+ return row;
4058
+ }
4059
+
4060
+ // packages/import/src/scan-profile.js
4061
+ var IMPORT_PROFILE_FIELDS = 256;
4062
+ var MAX_DEPTH = 16;
4063
+ var MAX_PATH_CHARS = 4096;
4064
+ var MAX_VISITED_FIELDS = 2048;
4065
+ var PREVIEW_ROWS = 12;
4066
+ function createScanProfile({ timestamp_field, max_fields = IMPORT_PROFILE_FIELDS } = {}) {
4067
+ const fields = new Map;
4068
+ const preview = [];
4069
+ let rows = 0;
4070
+ let max_row_bytes = 0;
4071
+ let has_row_limits = false;
4072
+ let has_long_paths = false;
4073
+ let has_field_limit = false;
4074
+ function profile(record) {
4075
+ let visited = 0;
4076
+ function visit(object, prefix, depth) {
4077
+ for (const key of Object.keys(object)) {
4078
+ if (++visited > MAX_VISITED_FIELDS) {
4079
+ has_row_limits = true;
4080
+ return;
4081
+ }
4082
+ const path = prefix + "/" + key.replace(/~/g, "~0").replace(/\//g, "~1");
4083
+ if (path.length > MAX_PATH_CHARS) {
4084
+ has_long_paths = true;
4085
+ continue;
4086
+ }
4087
+ const value = object[key];
4088
+ let entry = fields.get(path);
4089
+ if (!entry) {
4090
+ if (fields.size >= max_fields) {
4091
+ has_field_limit = true;
4092
+ continue;
4093
+ }
4094
+ entry = {
4095
+ field: {
4096
+ path,
4097
+ label: path.slice(1).replace(/~1/g, "/").replace(/~0/g, "~"),
4098
+ types: [],
4099
+ present: 0,
4100
+ missing: 0
4101
+ },
4102
+ candidate: {
4103
+ path,
4104
+ label: key,
4105
+ valid: 0,
4106
+ invalid: 0,
4107
+ missing: 0,
4108
+ ambiguous: 0,
4109
+ min: null,
4110
+ max: null
4111
+ },
4112
+ has_evidence: /(?:date|time|(?:^|_)at$|timestamp)/i.test(key)
4113
+ };
4114
+ fields.set(path, entry);
4115
+ }
4116
+ if (value && typeof value === "object" && !Array.isArray(value)) {
4117
+ if (depth >= MAX_DEPTH)
4118
+ has_row_limits = true;
4119
+ else
4120
+ visit(value, path, depth + 1);
4121
+ }
4122
+ }
4123
+ }
4124
+ visit(record, "", 1);
4125
+ for (const entry of fields.values()) {
4126
+ const value = getImportField(record, entry.field.path);
4127
+ if (value === undefined)
4128
+ continue;
4129
+ entry.field.present++;
4130
+ let type = typeof value;
4131
+ if (value === null)
4132
+ type = "null";
4133
+ else if (Array.isArray(value))
4134
+ type = "array";
4135
+ if (!entry.field.types.includes(type))
4136
+ entry.field.types.push(type);
4137
+ if (value !== undefined && value !== null && value !== "") {
4138
+ const timestamp = parseImportTimestamp(value);
4139
+ if (typeof value === "string" && /^\d{4}-\d{2}-\d{2}(?:[T ]|$)/.test(value))
4140
+ entry.has_evidence = true;
4141
+ if (timestamp) {
4142
+ entry.candidate.valid++;
4143
+ if (!entry.candidate.min || timestamp < entry.candidate.min)
4144
+ entry.candidate.min = timestamp;
4145
+ if (!entry.candidate.max || timestamp > entry.candidate.max)
4146
+ entry.candidate.max = timestamp;
4147
+ } else if (isAmbiguousTimestamp(value))
4148
+ entry.candidate.ambiguous++;
4149
+ else
4150
+ entry.candidate.invalid++;
4151
+ }
4152
+ }
4153
+ }
4154
+ return {
4155
+ get rows() {
4156
+ return rows;
4157
+ },
4158
+ get max_row_bytes() {
4159
+ return max_row_bytes;
4160
+ },
4161
+ get preview() {
4162
+ return preview;
4163
+ },
4164
+ get is_truncated() {
4165
+ return has_row_limits || has_long_paths || has_field_limit;
4166
+ },
4167
+ add(row) {
4168
+ const entry = serializeImportRow(row);
4169
+ getRowTimestamp(row, timestamp_field);
4170
+ rows++;
4171
+ max_row_bytes = Math.max(max_row_bytes, entry.bytes);
4172
+ if (preview.length < PREVIEW_ROWS)
4173
+ preview.push(row);
4174
+ profile(row.record);
4175
+ },
4176
+ merge(partial) {
4177
+ for (let index = 0;index < partial.preview.length && preview.length < PREVIEW_ROWS; index++)
4178
+ preview.push({ index: rows + index + 1, record: partial.preview[index] });
4179
+ rows += partial.rows;
4180
+ max_row_bytes = Math.max(max_row_bytes, partial.max_row_bytes);
4181
+ has_row_limits ||= partial.has_row_limits;
4182
+ has_long_paths ||= partial.has_long_paths;
4183
+ has_field_limit ||= partial.has_field_limit;
4184
+ for (const local of partial.entries) {
4185
+ const path = local.field.path;
4186
+ let entry = fields.get(path);
4187
+ if (!entry) {
4188
+ if (fields.size >= max_fields) {
4189
+ has_field_limit = true;
4190
+ continue;
4191
+ }
4192
+ entry = {
4193
+ field: { ...local.field, types: [], present: 0, missing: 0 },
4194
+ candidate: {
4195
+ ...local.candidate,
4196
+ valid: 0,
4197
+ invalid: 0,
4198
+ ambiguous: 0,
4199
+ min: null,
4200
+ max: null
4201
+ },
4202
+ has_evidence: false
4203
+ };
4204
+ fields.set(path, entry);
4205
+ }
4206
+ entry.field.present += local.field.present;
4207
+ for (const type of local.field.types)
4208
+ if (!entry.field.types.includes(type))
4209
+ entry.field.types.push(type);
4210
+ const candidate = entry.candidate;
4211
+ candidate.valid += local.candidate.valid;
4212
+ candidate.invalid += local.candidate.invalid;
4213
+ candidate.ambiguous += local.candidate.ambiguous;
4214
+ if (local.candidate.min && (!candidate.min || local.candidate.min < candidate.min))
4215
+ candidate.min = local.candidate.min;
4216
+ if (local.candidate.max && (!candidate.max || local.candidate.max > candidate.max))
4217
+ candidate.max = local.candidate.max;
4218
+ entry.has_evidence ||= local.has_evidence;
4219
+ }
4220
+ },
4221
+ partial() {
4222
+ return {
4223
+ rows,
4224
+ max_row_bytes,
4225
+ preview: preview.map((row) => row.record),
4226
+ entries: Array.from(fields.values()),
4227
+ has_row_limits,
4228
+ has_long_paths,
4229
+ has_field_limit
4230
+ };
4231
+ },
4232
+ finish(report) {
4233
+ report.row_count = rows;
4234
+ report.max_row_bytes = max_row_bytes;
4235
+ report.preview = preview;
4236
+ for (const { field, candidate, has_evidence } of fields.values()) {
4237
+ field.missing = rows - field.present;
4238
+ report.fields.push(field);
4239
+ if (has_evidence) {
4240
+ candidate.missing = rows - candidate.valid - candidate.invalid - candidate.ambiguous;
4241
+ report.timestamp_candidates.push(candidate);
4242
+ }
4243
+ }
4244
+ }
4245
+ };
4246
+ }
4247
+
4248
+ // packages/import/src/xml-task.js
4249
+ var PARTIAL_PROFILE_FIELDS = 1024;
4250
+ var IMPORT_XML_OPTIONS = Object.freeze({
4251
+ whitespace: "strip-indentation",
4252
+ parserOptions: Object.freeze({ doctype: "ignore" }),
4253
+ maxRecordChars: IMPORT_BATCH_BYTES,
4254
+ maxRecordNodes: 1e5
4255
+ });
4256
+ function checkImportRecordPath(record_path) {
4257
+ if (record_path && !/^\/[^/]+(?:\/[^/]+)*$/.test(record_path))
4258
+ throw new Error("XML record_path must be an absolute element path such as /Export/Record.");
4259
+ }
4260
+ function checkImportXmlRoot(name) {
4261
+ if (name.toLowerCase().split(":").pop() === "html")
4262
+ throw new Error("HTML documents are not supported XML imports.");
4263
+ }
4264
+ function restoreXmlRecord(record) {
4265
+ Object.setPrototypeOf(record.attributes, null);
4266
+ for (const child of record.children)
4267
+ if (typeof child !== "string")
4268
+ restoreXmlRecord(child);
4269
+ return record;
4270
+ }
4271
+ var IMPORT_XML_TASK = {
4272
+ name: "tailglow-import-xml",
4273
+ createSink(options) {
4274
+ let index = 0;
4275
+ if (options.kind === "rows") {
4276
+ let records = [];
4277
+ return {
4278
+ add(record) {
4279
+ createImportRow(record, ++index);
4280
+ records.push(record);
4281
+ },
4282
+ take() {
4283
+ const output = { records };
4284
+ records = [];
4285
+ return output;
4286
+ }
4287
+ };
4288
+ }
4289
+ const createProfile = () => createScanProfile({
4290
+ timestamp_field: options.timestamp_field,
4291
+ max_fields: PARTIAL_PROFILE_FIELDS
4292
+ });
4293
+ let profile = createProfile();
4294
+ return {
4295
+ add(record) {
4296
+ profile.add(createImportRow(record, ++index));
4297
+ },
4298
+ take() {
4299
+ const partial = profile.partial();
4300
+ profile = createProfile();
4301
+ return partial;
4302
+ }
4303
+ };
4304
+ },
4305
+ describeError(error) {
4306
+ if (error instanceof ImportRowError)
4307
+ return { detail: error.detail };
4308
+ const value = error ?? {};
4309
+ return {
4310
+ name: typeof value.name === "string" ? value.name : "Error",
4311
+ message: typeof value.message === "string" ? value.message : String(error)
4312
+ };
4313
+ },
4314
+ restoreError(description, record_number) {
4315
+ const value = description;
4316
+ if (typeof value.detail === "string")
4317
+ return new ImportRowError(record_number, value.detail);
4318
+ if (value.name === "RangeError")
4319
+ return new RangeError(value.message);
4320
+ if (value.name === "TypeError")
4321
+ return new TypeError(value.message);
4322
+ return new Error(value.message);
4323
+ }
4324
+ };
4325
+
4326
+ // packages/import/src/xml-workers.js
4327
+ var AUTO_MIN_PROCESSORS = 4;
4328
+ function validateImportWorkers(value = "auto") {
4329
+ if (value === "auto")
4330
+ return value;
4331
+ if (typeof value === "number" && Number.isSafeInteger(value) && value >= 0 && value <= IMPORT_MAX_WORKERS)
4332
+ return value;
4333
+ throw new RangeError(`workers must be "auto" or an integer from 0 to ${IMPORT_MAX_WORKERS}.`);
4334
+ }
4335
+ function planImportWorkers({ source, options, format, is_complete_read }) {
4336
+ const workers = validateImportWorkers(options.workers);
4337
+ if (workers === 0)
4338
+ return { requested: 0, reason: "workers is 0" };
4339
+ if (format !== "xml")
4340
+ return { requested: 0, reason: "only XML is read with workers" };
4341
+ if (!is_complete_read)
4342
+ return { requested: 0, reason: "sampled scans read sequentially" };
4343
+ if (typeof source.read !== "function")
4344
+ return { requested: 0, reason: "the source has no random access" };
4345
+ if (typeof options.create_worker !== "function")
4346
+ return { requested: 0, reason: "no create_worker factory was given" };
4347
+ if (workers !== "auto")
4348
+ return { requested: workers };
4349
+ if (source.size < IMPORT_WORKER_MIN_BYTES)
4350
+ return { requested: 0, reason: "auto uses workers for files of 32 MiB or more" };
4351
+ const processors = globalThis.navigator?.hardwareConcurrency;
4352
+ if (!processors || processors < AUTO_MIN_PROCESSORS)
4353
+ return {
4354
+ requested: 0,
4355
+ reason: `auto needs at least ${AUTO_MIN_PROCESSORS} logical processors`
4356
+ };
4357
+ return { requested: IMPORT_AUTO_WORKERS };
4358
+ }
4359
+ function readImportXmlSegments(source, options, requested, task_options) {
4360
+ const read = source.read;
4361
+ return readXmlSegments({ size: source.size, read: (start, end) => read.call(source, start, end) }, {
4362
+ workers: requested,
4363
+ maxSegmentBytes: IMPORT_XML_SEGMENT_MAX_BYTES,
4364
+ readBytes: IMPORT_XML_READ_BYTES,
4365
+ createWorker: options.create_worker,
4366
+ task: IMPORT_XML_TASK,
4367
+ taskOptions: task_options,
4368
+ ...options.record_path ? { selectPath: options.record_path.slice(1).split("/") } : {},
4369
+ ...IMPORT_XML_OPTIONS,
4370
+ onRoot: checkImportXmlRoot,
4371
+ signal: options.signal
4372
+ });
4373
+ }
4374
+ function reportWorkerEvent(event, state, options) {
4375
+ if (event.type === "sequential") {
4376
+ options.onworkers?.({ ...state.status, reason: event.reason });
4377
+ return false;
4378
+ }
4379
+ if (event.type === "workers") {
4380
+ state.status = { requested: state.status.requested, actual: event.actual };
4381
+ if (event.reason)
4382
+ state.status.reason = event.reason;
4383
+ options.onworkers?.(state.status);
4384
+ } else if (event.type === "fallback")
4385
+ options.onworkers?.({ ...state.status, fallback: event.reason });
4386
+ return true;
4387
+ }
4388
+ async function* readXmlRowsWithWorkers(source, options, requested) {
4389
+ const state = { status: { requested, actual: 0 } };
4390
+ let rows = 0;
4391
+ for await (const event of readImportXmlSegments(source, options, requested, { kind: "rows" })) {
4392
+ if (!reportWorkerEvent(event, state, options))
4393
+ return false;
4394
+ if (event.type !== "records")
4395
+ continue;
4396
+ const output = event.output;
4397
+ for (const record of output.records) {
4398
+ checkImportAbort(options.signal);
4399
+ const restored = restoreXmlRecord(record);
4400
+ yield { index: ++rows, record: restored };
4401
+ }
4402
+ options.onprogress?.({ bytes_read: event.end, total_bytes: source.size, rows });
4403
+ }
4404
+ return true;
4405
+ }
4406
+ async function scanXmlWithWorkers(source, options, requested) {
4407
+ const state = { status: { requested, actual: 0 } };
4408
+ const profile = createScanProfile({ timestamp_field: options.timestamp_field });
4409
+ for await (const event of readImportXmlSegments(source, options, requested, {
4410
+ kind: "scan",
4411
+ timestamp_field: options.timestamp_field
4412
+ })) {
4413
+ if (!reportWorkerEvent(event, state, options))
4414
+ return null;
4415
+ if (event.type !== "records")
4416
+ continue;
4417
+ const partial = event.output;
4418
+ if (partial.has_row_limits || partial.has_field_limit) {
4419
+ options.onworkers?.({
4420
+ ...state.status,
4421
+ fallback: "a part's schema profile cannot merge exactly; scanning sequentially"
4422
+ });
4423
+ return null;
4424
+ }
4425
+ for (const record of partial.preview)
4426
+ restoreXmlRecord(record);
4427
+ profile.merge(partial);
4428
+ options.onprogress?.({ bytes_read: event.end, total_bytes: source.size, rows: profile.rows });
4429
+ }
4430
+ return profile;
4431
+ }
4432
+
4433
+ // packages/import/src/read.js
4434
+ async function* readImportRows(source, options = {}) {
4435
+ validateImportWorkers(options.workers);
4436
+ const format = !options.format || options.format === "auto" ? (await detectImport(source, options)).format : options.format;
4437
+ if (format === "xml")
4438
+ checkImportRecordPath(options.record_path);
4439
+ const plan = planImportWorkers({ source, options, format, is_complete_read: true });
4440
+ if (!plan.requested)
4441
+ options.onworkers?.({ requested: 0, actual: 0, reason: plan.reason });
4442
+ else if (yield* readXmlRowsWithWorkers(source, options, plan.requested))
4443
+ return;
4444
+ let bytes_read = 0;
4445
+ let rows = 0;
4446
+ async function* bytes() {
4447
+ for await (const chunk of readImportBytes(source, options.signal)) {
4448
+ bytes_read += chunk.length;
4449
+ options.onprogress?.({ bytes_read, total_bytes: source.size, rows });
4450
+ yield chunk;
4451
+ }
4452
+ }
4453
+ async function* records() {
4454
+ if (format === "xml") {
4455
+ const chunks = alignXmlChunks(bytes());
4456
+ const record_path = options.record_path;
4457
+ if (!record_path) {
4458
+ yield* xmlDocumentRecords(chunks, { ...IMPORT_XML_OPTIONS, onRoot: checkImportXmlRoot });
4459
+ } else {
4460
+ let has_root = false;
4461
+ yield* xmlRecords(chunks, {
4462
+ ...IMPORT_XML_OPTIONS,
4463
+ select(path) {
4464
+ if (!has_root) {
4465
+ has_root = true;
4466
+ checkImportXmlRoot(path[0]);
4467
+ }
4468
+ return "/" + path.join("/") === record_path;
4469
+ }
4470
+ });
4471
+ }
4472
+ return;
4473
+ }
4474
+ if (format === "json") {
4475
+ if (source.size > IMPORT_JSON_BYTES)
4476
+ throw new Error("Whole-document JSON is limited to 50 MiB. Use JSONL for larger files.");
4477
+ let text = "";
4478
+ for await (const part of decodeImportBytes(bytes())) {
4479
+ if (bytes_read > IMPORT_JSON_BYTES)
4480
+ throw new Error("Whole-document JSON is limited to 50 MiB. Use JSONL for larger files.");
4481
+ text += part;
4482
+ }
4483
+ let value;
4484
+ try {
4485
+ value = JSON.parse(text);
4486
+ } catch {
4487
+ throw new Error("This file is not valid JSON.");
4488
+ }
4489
+ if (Array.isArray(value))
4490
+ yield* value;
4491
+ else
4492
+ yield value;
4493
+ return;
4494
+ }
4495
+ if (format === "jsonl") {
4496
+ let parseLine = function(line) {
4497
+ line_number++;
4498
+ if (!line.trim())
4499
+ return;
4500
+ if (line.length > IMPORT_BATCH_BYTES)
4501
+ throw new Error(`JSONL line ${line_number} exceeds the 1 MiB record limit.`);
4502
+ try {
4503
+ return JSON.parse(line);
4504
+ } catch {
4505
+ throw new Error(`JSONL line ${line_number} is not valid JSON.`);
4506
+ }
4507
+ };
4508
+ let partial = "";
4509
+ let line_number = 0;
4510
+ for await (const part of decodeImportBytes(bytes())) {
4511
+ const input = partial + part;
4512
+ let start = 0;
4513
+ let end;
4514
+ while ((end = input.indexOf(`
4515
+ `, start)) !== -1) {
4516
+ const record = parseLine(input.slice(start, end));
4517
+ if (record !== undefined)
4518
+ yield record;
4519
+ start = end + 1;
4520
+ }
4521
+ partial = input.slice(start);
4522
+ if (partial.length > IMPORT_BATCH_BYTES)
4523
+ throw new Error(`JSONL line ${line_number + 1} exceeds the 1 MiB record limit.`);
4524
+ }
4525
+ if (partial) {
4526
+ const record = parseLine(partial);
4527
+ if (record !== undefined)
4528
+ yield record;
4529
+ }
4530
+ return;
4531
+ }
4532
+ if (format !== "csv" && format !== "tsv")
4533
+ throw new Error(`Unsupported import format: ${format}`);
4534
+ let headers = null;
4535
+ for await (const result of readCsvResults(decodeImportBytes(bytes()), format === "tsv" ? "\t" : ",")) {
4536
+ for (const values of result.data) {
4537
+ if (!headers) {
4538
+ headers = values.map((value) => value.trim().replace(/^\uFEFF/, ""));
4539
+ if (headers.some((header) => !header))
4540
+ throw new Error("Every CSV/TSV column needs a header.");
4541
+ if (new Set(headers).size !== headers.length)
4542
+ throw new Error("Two CSV/TSV columns have the same header.");
4543
+ } else {
4544
+ if (values.length !== headers.length)
4545
+ throw new Error(`Row ${rows + 1} has ${values.length} cells; the header has ${headers.length}.`);
4546
+ yield Object.fromEntries(headers.map((header, index) => [header, values[index]]));
4547
+ }
4548
+ }
4549
+ }
4550
+ }
4551
+ for await (const record of records()) {
4552
+ checkImportAbort(options.signal);
4553
+ yield createImportRow(record, ++rows);
4554
+ }
4555
+ options.onprogress?.({ bytes_read, total_bytes: source.size, rows });
4556
+ }
4557
+ async function* readCsvResults(chunks, delimiter) {
4558
+ let parser;
4559
+ let partial = "";
4560
+ let cursor = 0;
4561
+ function parse(text, is_last) {
4562
+ let input = partial + text;
4563
+ const has_pending_cr = !is_last && input.endsWith("\r");
4564
+ if (has_pending_cr)
4565
+ input = input.slice(0, -1);
4566
+ const candidate = parser ?? new import_papaparse2.default.ParserHandle({
4567
+ delimiter,
4568
+ skipEmptyLines: "greedy",
4569
+ dynamicTyping: false
4570
+ });
4571
+ const results = candidate.parse(input, cursor, !is_last);
4572
+ if (results.data.length || is_last)
4573
+ parser = candidate;
4574
+ const error = results.errors.find((item) => item.code !== "UndetectableDelimiter" && (is_last || !(item.index >= results.meta.cursor - cursor)));
4575
+ if (error)
4576
+ throw new Error(`CSV could not be read: ${error.message}`);
4577
+ partial = input.slice(results.meta.cursor - cursor) + (has_pending_cr ? "\r" : "");
4578
+ if (partial.length > 4 * IMPORT_BATCH_BYTES)
4579
+ throw new Error("A CSV row is too large. Each record must fit within 1 MiB.");
4580
+ cursor = results.meta.cursor;
4581
+ return results;
4582
+ }
4583
+ for await (const text of chunks)
4584
+ yield parse(text, false);
4585
+ yield parse("", true);
4586
+ }
4587
+ async function* alignXmlChunks(chunks) {
4588
+ let pending = new Uint8Array(IMPORT_CHUNK_BYTES);
4589
+ let length = 0;
4590
+ for await (const chunk of chunks) {
4591
+ if (!length && chunk.length === IMPORT_CHUNK_BYTES) {
4592
+ yield chunk;
4593
+ continue;
4594
+ }
4595
+ let offset = 0;
4596
+ while (offset < chunk.length) {
4597
+ const count = Math.min(IMPORT_CHUNK_BYTES - length, chunk.length - offset);
4598
+ pending.set(chunk.subarray(offset, offset + count), length);
4599
+ length += count;
4600
+ offset += count;
4601
+ if (length === IMPORT_CHUNK_BYTES) {
4602
+ yield pending;
4603
+ pending = new Uint8Array(IMPORT_CHUNK_BYTES);
4604
+ length = 0;
4605
+ }
4606
+ }
4607
+ }
4608
+ if (length)
4609
+ yield pending.subarray(0, length);
4610
+ }
4611
+ // packages/import/src/scan.js
4612
+ async function scanImport(source, options = {}) {
4613
+ validateImportWorkers(options.workers);
4614
+ const detection = await detectImport(source, options);
4615
+ const report = {
4616
+ format: detection.format,
4617
+ is_sampled: false,
4618
+ is_complete: false,
4619
+ bytes_read: 0,
4620
+ total_bytes: source.size,
4621
+ row_count: 0,
4622
+ preview: [],
4623
+ fields: [],
4624
+ timestamp_candidates: [],
4625
+ warnings: detection.warnings.filter((warning) => !warning.startsWith("Detection used")),
4626
+ max_row_bytes: 0
4627
+ };
4628
+ if (detection.xml)
4629
+ report.xml = {
4630
+ ...detection.xml,
4631
+ ...options.record_path ? { record_path: options.record_path } : {}
4632
+ };
4633
+ if (detection.xml && detection.is_sampled)
4634
+ report.warnings.push("XML path discovery counts cover only the first 256 KiB; a complete selected-row scan does not make those path counts complete.");
4635
+ if (detection.format === "xml" && !options.record_path)
4636
+ report.warnings.push("Automatic XML import includes every direct root child; root attributes and meaningful direct text become a final context record. A root without child elements becomes one record.");
4637
+ if (detection.format === "xml")
4638
+ checkImportRecordPath(options.record_path);
4639
+ const plan = planImportWorkers({
4640
+ source,
4641
+ options,
4642
+ format: detection.format,
4643
+ is_complete_read: Boolean(options.is_full)
4644
+ });
4645
+ if (!plan.requested)
4646
+ options.onworkers?.({ requested: 0, actual: 0, reason: plan.reason });
4647
+ let profile = plan.requested ? await scanXmlWithWorkers(source, options, plan.requested) : null;
4648
+ if (profile)
4649
+ report.bytes_read = source.size;
4650
+ else
4651
+ profile = await scanSequentially(source, options, detection.format, report);
4652
+ report.is_complete = !report.is_sampled;
4653
+ if (report.is_complete && !profile.rows)
4654
+ throw new Error("This selection has no data rows. Check the XML record_path or file contents.");
4655
+ profile.finish(report);
4656
+ if (profile.is_truncated)
4657
+ report.warnings.push("Schema discovery was truncated (256 fields, 16 levels, 4096 characters per path or 2048 visited fields per row). Counts cover scanned rows only.");
4658
+ if (report.is_sampled)
4659
+ report.warnings.push("This bounded sample does not certify full file validity or schema coverage. Records must be validated as they are imported.");
4660
+ if (options.timestamp_field)
4661
+ report.warnings.push("The selected timestamp is sent at request level. Changes between rows split batches and may produce many small requests.");
4662
+ options.onprogress?.({
4663
+ bytes_read: report.bytes_read,
4664
+ total_bytes: source.size,
4665
+ rows: report.row_count
4666
+ });
4667
+ return report;
4668
+ }
4669
+ async function scanSequentially(source, options, format, report) {
4670
+ const sample_limit = new Error("Sample complete");
4671
+ const has_byte_limit = !options.is_full && format !== "json";
4672
+ const scanned_source = {
4673
+ name: source.name,
4674
+ size: source.size,
4675
+ async* open() {
4676
+ for await (const chunk of readImportBytes(source, options.signal)) {
4677
+ const remaining = has_byte_limit ? IMPORT_SAMPLE_BYTES - report.bytes_read : chunk.length;
4678
+ const part = chunk.subarray(0, remaining);
4679
+ report.bytes_read += part.length;
4680
+ if (part.length)
4681
+ yield part;
4682
+ if (has_byte_limit && report.bytes_read >= IMPORT_SAMPLE_BYTES)
4683
+ throw sample_limit;
4684
+ }
4685
+ }
4686
+ };
4687
+ const profile = createScanProfile({ timestamp_field: options.timestamp_field });
4688
+ report.bytes_read = 0;
4689
+ try {
4690
+ for await (const row of readImportRows(scanned_source, {
4691
+ ...options,
4692
+ format,
4693
+ workers: 0,
4694
+ onworkers: undefined,
4695
+ onprogress(progress) {
4696
+ options.onprogress?.({ ...progress, total_bytes: source.size });
4697
+ }
4698
+ })) {
4699
+ profile.add(row);
4700
+ if (!options.is_full && profile.rows >= IMPORT_SAMPLE_ROWS) {
4701
+ report.is_sampled = true;
4702
+ break;
4703
+ }
4704
+ }
4705
+ } catch (error) {
4706
+ if (error !== sample_limit)
4707
+ throw error;
4708
+ report.is_sampled = true;
4709
+ }
4710
+ return profile;
4711
+ }
4712
+ // packages/import/node.js
4713
+ function createNodeImportWorker(entry = new URL("./src/worker.node.js", import.meta.url)) {
4714
+ return createNodeXmlWorker(entry);
4715
+ }
4716
+ export {
4717
+ IMPORT_BATCH_BYTES,
4718
+ IMPORT_BATCH_ROWS,
4719
+ IMPORT_JSON_BYTES,
4720
+ IMPORT_MAX_WORKERS,
4721
+ IMPORT_REJECTED_STATUSES,
4722
+ createBlobSource,
4723
+ createImportBatches,
4724
+ createNodeImportWorker,
4725
+ detectImport,
4726
+ getImportField,
4727
+ parseImportTimestamp,
4728
+ readImportRows,
4729
+ scanImport,
4730
+ serializeImportRow
4731
+ };