@konstit/csv 0.4.3 → 0.5.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.
Files changed (64) hide show
  1. package/README.md +50 -28
  2. package/THIRD_PARTY_NOTICES.md +7 -1
  3. package/dist/api.d.ts +39 -249
  4. package/dist/api.js +56 -394
  5. package/dist/batches.d.ts +8 -57
  6. package/dist/batches.js +77 -52
  7. package/dist/cli-command.d.ts +7 -0
  8. package/dist/cli-command.js +315 -0
  9. package/dist/cli-schema.d.ts +127 -0
  10. package/dist/cli-schema.js +234 -0
  11. package/dist/cli.js +2 -604
  12. package/dist/file-stream.d.ts +62 -3
  13. package/dist/file-stream.js +242 -36
  14. package/dist/files.d.ts +11 -11
  15. package/dist/files.js +38 -79
  16. package/dist/filter.d.ts +5 -8
  17. package/dist/format-bytes.d.ts +1 -0
  18. package/dist/format-bytes.js +11 -0
  19. package/dist/index.d.ts +3 -3
  20. package/dist/index.js +2 -14
  21. package/dist/inspect-file.d.ts +22 -0
  22. package/dist/inspect-file.js +138 -0
  23. package/dist/latin1-rows.d.ts +4 -3
  24. package/dist/latin1-rows.js +72 -24
  25. package/dist/materialize-field.d.ts +2 -0
  26. package/dist/materialize-field.js +20 -15
  27. package/dist/native.d.ts +50 -78
  28. package/dist/native.js +36 -112
  29. package/dist/normalize.d.ts +14 -40
  30. package/dist/normalize.js +66 -64
  31. package/dist/options-snapshot.js +3 -2
  32. package/dist/options.d.ts +4 -30
  33. package/dist/options.js +0 -6
  34. package/dist/parser.d.ts +23 -209
  35. package/dist/parser.js +146 -261
  36. package/dist/strict-schema.d.ts +6 -4
  37. package/dist/strict-schema.js +4 -3
  38. package/dist/types.d.ts +119 -221
  39. package/dist/zip-reader.d.ts +13 -1
  40. package/dist/zip-reader.js +36 -17
  41. package/package.json +36 -33
  42. package/prebuilds/darwin-arm64/libcsv_native.dylib +0 -0
  43. package/prebuilds/darwin-x64/libcsv_native.dylib +0 -0
  44. package/prebuilds/linux-x64/libcsv_native.so +0 -0
  45. package/prebuilds/manifest.json +9 -8
  46. package/prebuilds/provenance/darwin-arm64.json +1 -1
  47. package/prebuilds/provenance/darwin-x64.json +1 -1
  48. package/prebuilds/provenance/linux-x64.json +1 -1
  49. package/dist/worker-count.d.ts +0 -34
  50. package/dist/worker-count.js +0 -101
  51. package/dist/worker-factory.d.ts +0 -3
  52. package/dist/worker-factory.js +0 -9
  53. package/dist/worker-filter.d.ts +0 -29
  54. package/dist/worker-filter.js +0 -46
  55. package/dist/worker-module.d.ts +0 -1
  56. package/dist/worker-module.js +0 -4
  57. package/dist/worker-pool.d.ts +0 -93
  58. package/dist/worker-pool.js +0 -298
  59. package/dist/worker-rows.d.ts +0 -36
  60. package/dist/worker-rows.js +0 -127
  61. package/dist/workers/count.worker.d.ts +0 -1
  62. package/dist/workers/count.worker.js +0 -36
  63. package/dist/workers/rows.worker.d.ts +0 -1
  64. package/dist/workers/rows.worker.js +0 -56
package/dist/api.js CHANGED
@@ -1,4 +1,3 @@
1
- import { NativeCsvRowView } from "./batches.js";
2
1
  import {
3
2
  prepareCsvFileInput,
4
3
  rejectAutoDelimiterSharding,
@@ -17,23 +16,20 @@ import {
17
16
  re
18
17
  } from "./filter.js";
19
18
  import { latin1Rows } from "./latin1-rows.js";
20
- import { optionalLatin1RowsNative } from "./native.js";
19
+ import { latin1RowsParser } from "./native.js";
21
20
  import {
22
- encodingCode,
23
- normalizeColumns
21
+ normalizeFixedColumnsCount,
22
+ prepareCsvBufferInput,
23
+ resolveInputEncoding
24
24
  } from "./normalize.js";
25
25
  import { snapshotCsvOptions } from "./options-snapshot.js";
26
- import { createPreparedParser } from "./parser.js";
27
26
  import {
28
- rejectStrictSchemaUnsupported,
29
- strictSchemaValidator
30
- } from "./strict-schema.js";
31
- import { parallelCount } from "./worker-count.js";
32
- import {
33
- createWorkerPool,
34
- CsvWorkerPool
35
- } from "./worker-pool.js";
36
- import { parallelRows } from "./worker-rows.js";
27
+ countFileChunks,
28
+ createPreparedParser,
29
+ parseFileChunks,
30
+ validateRowSelection
31
+ } from "./parser.js";
32
+ import { rejectStrictSchemaUnsupported } from "./strict-schema.js";
37
33
  export async function parse(buffer, suppliedOptions = {}) {
38
34
  const options = snapshotCsvOptions(suppliedOptions);
39
35
  if ("compression" in options && options.compression !== void 0)
@@ -42,14 +38,12 @@ export async function parse(buffer, suppliedOptions = {}) {
42
38
  throw Error("parse() does not support automatic delimiter detection; specify delimiter or use a file API");
43
39
  rejectFilteredStrictSchema(options);
44
40
  const filters = toNativeFilters(options.where);
45
- const columns = selectedColumns(options);
41
+ const columns = options.columns;
46
42
  const projected = filters !== void 0 || options.strict !== !0 && columns !== void 0;
47
- using parser = createPreparedParser(toParserOptions(options), projected ? columns : void 0, filters);
48
- const validator = strictSchemaValidator(options);
49
- const batch = parser.writeBatch(buffer, !0);
43
+ const input = prepareCsvBufferInput(buffer, options.encoding, options.delimiter);
44
+ using parser = createPreparedParser(toParserOptions(options, input.parserEncoding), projected ? columns : void 0, filters);
45
+ const batch = parser.writeBatch(input.bytes, !0);
50
46
  try {
51
- validator?.validateBatch(batch);
52
- validator?.finish();
53
47
  return materializeRows(batch, columns, projected);
54
48
  } finally {
55
49
  batch.close();
@@ -58,240 +52,62 @@ export async function parse(buffer, suppliedOptions = {}) {
58
52
  export function rows(path, options = {}) {
59
53
  return iterateRows(path, snapshotCsvOptions(options));
60
54
  }
61
- async function* iterateRows(path, options) {
62
- if ((options.workerCount ?? 1) > 1) {
63
- rejectCompressedSharding(options, "parallel row parsing");
64
- rejectAutoDelimiterSharding(options, "parallel row parsing");
65
- yield* parallelRows(path, options);
66
- return;
67
- }
55
+ export async function* iterateRows(path, options, probeFormat) {
68
56
  rejectFilteredStrictSchema(options);
69
57
  const filters = toNativeFilters(options.where);
70
- const columns = selectedColumns(options);
58
+ const columns = options.columns;
71
59
  const projected = filters !== void 0 || options.strict !== !0 && columns !== void 0;
72
- await using input = await prepareCsvFileInput(path, options);
73
- const parserOptions = toParserOptions(options, input.delimiter);
74
- const validator = strictSchemaValidator(options);
75
- if (options.strict !== !0 && options.fixedColumns === void 0 && validator === void 0 && encodingCode(options.encoding) === 1 && optionalLatin1RowsNative() !== null) {
76
- yield* latin1Rows(input.chunks(), input.delimiter, columns, filters);
60
+ await using input = await prepareCsvFileInput(path, options, "read-ahead", probeFormat);
61
+ const parserOptions = toParserOptions(options, input.parserEncoding, input.delimiter);
62
+ const reusableRows = options.reuseArrays === !0 ? [] : void 0;
63
+ if (options.strict !== !0 && input.parserEncoding === "latin1") {
64
+ validateRowSelection(options);
65
+ const fixedColumns = normalizeFixedColumnsCount(options.fixedColumns, "fixed column count");
66
+ const createParser = latin1RowsParser(fixedColumns);
67
+ const chunks = options.limit === 0 ? [] : input.chunks();
68
+ yield* latin1Rows(chunks, input.delimiter, columns, filters, createParser, reusableRows, parserOptions);
77
69
  return;
78
70
  }
79
71
  using parser = createPreparedParser(parserOptions, projected ? columns : void 0, filters);
80
- for await (const chunk of input.chunks()) {
81
- const batch = parser.writeBatch(chunk);
82
- if (batch.rowCount > 0)
83
- try {
84
- validator?.validateBatch(batch);
85
- const values = materializeRows(batch, columns, projected);
86
- if (values.length > 0)
87
- yield values;
88
- } finally {
89
- batch.close();
90
- }
91
- else
92
- batch.close();
93
- }
94
- const batch = parser.endBatch();
95
- if (batch.rowCount > 0)
96
- try {
97
- validator?.validateBatch(batch);
98
- validator?.finish();
99
- const values = materializeRows(batch, columns, projected);
100
- if (values.length > 0)
101
- yield values;
102
- } finally {
103
- batch.close();
104
- }
105
- else {
106
- validator?.finish();
107
- batch.close();
108
- }
72
+ for await (using batch of parseFileChunks(input, parser, options.limit))
73
+ yield materializeRows(batch, columns, projected, reusableRows);
109
74
  }
110
-
111
- export { parallelRows };
112
- export { CsvWorkerPool };
113
75
  export function batches(path, options = {}) {
114
76
  return iterateBatches(path, snapshotCsvOptions(options));
115
77
  }
116
78
  async function* iterateBatches(path, options) {
117
79
  rejectFilteredStrictSchema(options);
118
80
  const filters = toNativeFilters(options.where);
119
- const columns = filters === void 0 ? void 0 : selectedColumns(options);
81
+ const columns = options.columns;
82
+ if (options.strict === !0 && columns !== void 0)
83
+ throw Error("strict CSV validation is not supported for projected batches; use csv.rows() for strict projection");
120
84
  await using input = await prepareCsvFileInput(path, options);
121
- using parser = createPreparedParser(toParserOptions(options, input.delimiter), columns, filters);
122
- const validator = strictSchemaValidator(options);
123
- for await (const chunk of input.chunks()) {
124
- const batch = parser.writeBatch(chunk);
125
- if (batch.rowCount > 0) {
126
- try {
127
- validator?.validateBatch(batch);
128
- } catch (error) {
129
- batch.close();
130
- throw error;
131
- }
132
- yield batch;
133
- } else
134
- batch.close();
135
- }
136
- const batch = parser.endBatch();
137
- if (batch.rowCount > 0) {
138
- try {
139
- validator?.validateBatch(batch);
140
- validator?.finish();
141
- } catch (error) {
142
- batch.close();
143
- throw error;
144
- }
145
- yield batch;
146
- } else {
147
- validator?.finish();
148
- batch.close();
149
- }
150
- }
151
- export async function withBatches(path, options, callback) {
152
- for await (const batch of batches(path, options))
153
- try {
154
- await callback(batch);
155
- } finally {
156
- batch.close();
157
- }
158
- }
159
- export async function forEachColumnarBatches(path, suppliedOptions = {}, callback) {
160
- const options = snapshotCsvOptions(suppliedOptions);
161
- ensureColumnarBatchesSupported(options);
162
- const filters = toNativeFilters(options.where);
163
- const columns = selectedColumns(options);
164
- const scopedBatch = new ScopedCsvColumnarBatchView(columns);
165
- let batchIndex = 0;
166
- await using input = await prepareCsvFileInput(path, options);
167
- using parser = createPreparedParser(toParserOptions(options, input.delimiter), columns, filters);
168
- const validator = strictSchemaValidator(options);
169
- for await (const chunk of input.chunks()) {
170
- const batch = parser.writeBatch(chunk);
171
- if (batch.rowCount > 0)
172
- try {
173
- validator?.validateBatch(batch);
174
- scopedBatch.bind(batch);
175
- try {
176
- const result = callback(scopedBatch, batchIndex);
177
- if (isPromiseLike(result))
178
- throw TypeError("columnar batch callback must be synchronous");
179
- } finally {
180
- scopedBatch.release();
181
- }
182
- ++batchIndex;
183
- } finally {
184
- batch.close();
185
- }
186
- else
187
- batch.close();
188
- }
189
- const batch = parser.endBatch();
190
- if (batch.rowCount > 0)
191
- try {
192
- validator?.validateBatch(batch);
193
- validator?.finish();
194
- scopedBatch.bind(batch);
195
- try {
196
- const result = callback(scopedBatch, batchIndex);
197
- if (isPromiseLike(result))
198
- throw TypeError("columnar batch callback must be synchronous");
199
- } finally {
200
- scopedBatch.release();
201
- }
202
- } finally {
203
- batch.close();
204
- }
205
- else {
206
- validator?.finish();
207
- batch.close();
208
- }
209
- }
210
- export function withColumnarBatches(path, options, callback) {
211
- return forEachColumnarBatches(path, options, callback);
212
- }
213
- export async function forEachRowViews(path, suppliedOptions = {}, callback) {
214
- const options = snapshotCsvOptions(suppliedOptions);
215
- ensureRowViewsSupported(options);
216
- const scopedRowView = new ScopedCsvRowView(selectedColumns(options));
217
- for await (const batch of batches(path, options))
218
- try {
219
- const rowCount = batch.rowCount;
220
- if (rowCount === 0)
221
- continue;
222
- const reusable = new NativeCsvRowView(batch.data(), batch.rowOffsets(), batch.fieldOffsets());
223
- for (let rowIndex = 0;rowIndex < rowCount; ++rowIndex) {
224
- scopedRowView.bind(reusable.moveTo(rowIndex));
225
- try {
226
- const result = callback(scopedRowView, rowIndex);
227
- if (isPromiseLike(result))
228
- throw TypeError("row view callback must be synchronous");
229
- } finally {
230
- scopedRowView.release();
231
- }
232
- }
233
- } finally {
234
- batch.close();
235
- }
236
- }
237
- export function withRowViews(path, options, callback) {
238
- return forEachRowViews(path, options, callback);
85
+ using parser = createPreparedParser(toParserOptions(options, input.parserEncoding, input.delimiter), columns, filters);
86
+ yield* parseFileChunks(input, parser, options.limit);
239
87
  }
240
88
  export async function count(path, suppliedOptions = {}) {
241
89
  const options = snapshotCsvOptions(suppliedOptions);
242
- if ((options.workerCount ?? 1) > 1) {
243
- rejectCompressedSharding(options, "parallel counting");
244
- rejectAutoDelimiterSharding(options, "parallel counting");
245
- return parallelCount(path, options);
246
- }
247
90
  const filters = toNativeFilters(options.where);
248
- await using input = await prepareCsvFileInput(path, options);
91
+ await using input = await prepareCsvFileInput(path, options, "count");
249
92
  if (options.strict === !0 && filters !== void 0)
250
93
  throw Error("strict CSV validation is not supported for count filters");
251
- using parser = createPreparedParser(toParserOptions(options, input.delimiter), void 0, filters);
252
- const validator = strictSchemaValidator(options);
253
- let total = 0;
254
- if (options.strict === !0 && filters === void 0) {
255
- for await (const chunk of input.chunks()) {
256
- const batch = parser.writeBatch(chunk);
257
- try {
258
- validator?.validateBatch(batch);
259
- total += batch.rowCount;
260
- } finally {
261
- batch.close();
262
- }
263
- }
264
- const batch = parser.endBatch();
265
- try {
266
- validator?.validateBatch(batch);
267
- validator?.finish();
268
- total += batch.rowCount;
269
- } finally {
270
- batch.close();
271
- }
272
- return total;
273
- }
274
- for await (const chunk of input.chunks())
275
- total += parser.writeCount(chunk);
276
- total += parser.endCount();
277
- return total;
94
+ using parser = createPreparedParser(toParserOptions(options, input.parserEncoding, input.delimiter), void 0, filters);
95
+ return await countFileChunks(input, parser, options);
278
96
  }
279
-
280
- export { parallelCount };
281
- export function findCsvSafeSplitOffsets(path, shardCount, suppliedOptions = {}) {
97
+ function findCsvSafeSplitOffsets(path, shardCount, suppliedOptions = {}) {
282
98
  const options = snapshotCsvOptions(suppliedOptions);
283
- rejectCompressedSharding(options, "CSV split offset scanning");
284
- rejectAutoDelimiterSharding(options, "CSV split offset scanning");
285
- return findCsvSafeSplitOffsetsNative(path, shardCount, options.delimiter ?? ",");
99
+ return findCsvSafeSplitOffsetsNative(path, shardCount, shardingDelimiter(options, "CSV split offset scanning"));
286
100
  }
287
- export function findCsvSafeShards(path, shardCount, suppliedOptions = {}) {
101
+ function findCsvSafeShards(path, shardCount, suppliedOptions = {}) {
288
102
  const options = snapshotCsvOptions(suppliedOptions);
289
- rejectCompressedSharding(options, "CSV shard scanning");
290
- rejectAutoDelimiterSharding(options, "CSV shard scanning");
291
- return findCsvSafeShardsNative(path, shardCount, options.delimiter ?? ",");
103
+ return findCsvSafeShardsNative(path, shardCount, shardingDelimiter(options, "CSV shard scanning"));
292
104
  }
293
- export function workerPool(path, options) {
294
- return createWorkerPool(path, options);
105
+ function shardingDelimiter(options, operation) {
106
+ rejectCompressedSharding(options, operation);
107
+ rejectAutoDelimiterSharding(options, operation);
108
+ if (typeof resolveInputEncoding(options.encoding) !== "string")
109
+ throw Error(`${operation} requires a native UTF-8 or Latin-1 encoding because offsets refer to the source file`);
110
+ return options.delimiter ?? ",";
295
111
  }
296
112
  export const csv = {
297
113
  all,
@@ -299,188 +115,34 @@ export const csv = {
299
115
  column,
300
116
  not,
301
117
  re,
302
- workerPool,
303
118
  parse,
304
119
  rows,
305
- parallelCount,
306
- parallelRows,
307
120
  batches,
308
- withBatches,
309
- forEachColumnarBatches,
310
- withColumnarBatches,
311
- forEachRowViews,
312
- withRowViews,
313
121
  count,
314
122
  findCsvSafeSplitOffsets,
315
123
  findCsvSafeShards
316
124
  };
317
- function toParserOptions(options, delimiter = options.delimiter) {
318
- const selectedColumns = "columns" in options && options.columns !== void 0 ? options.columns : options.selectedColumns;
319
- if ("columns" in options && options.columns !== void 0 && options.selectedColumns !== void 0)
320
- throw Error("use columns or selectedColumns, not both");
125
+ function toParserOptions(options, encoding, delimiter = options.delimiter) {
321
126
  return {
322
127
  delimiter,
323
- encoding: options.encoding,
128
+ encoding,
324
129
  strict: options.strict,
325
- selectedColumns,
326
- fixedColumns: options.fixedColumns
130
+ columns: options.columns,
131
+ fixedColumns: options.fixedColumns,
132
+ skipRows: options.skipRows,
133
+ limit: options.limit,
134
+ expectedHeaders: options.expectedHeaders,
135
+ requireHeader: options.requireHeader,
136
+ minDataRows: options.minDataRows
327
137
  };
328
138
  }
329
- function selectedColumns(options) {
330
- if (options.columns !== void 0 && options.selectedColumns !== void 0)
331
- throw Error("use columns or selectedColumns, not both");
332
- const columns = options.columns ?? options.selectedColumns;
333
- normalizeColumns(columns);
334
- return columns;
335
- }
336
- function materializeRows(batch, columns, projected) {
337
- if (projected)
338
- return batch.rowsInto([]);
339
- return batch.rowsInto([], columns);
340
- }
341
- function ensureRowViewsSupported(options) {
342
- if ((options.workerCount ?? 1) > 1)
343
- throw Error("row view callbacks do not support workerCount; use rows()/parallelRows() for materialized rows or withBatches() for low-level streaming");
344
- }
345
- function ensureColumnarBatchesSupported(options) {
346
- ensureRowViewsSupported(options);
347
- if (options.strict === !0 && selectedColumns(options) !== void 0)
348
- throw Error("columnar batch callbacks do not support strict selectedColumns; use rows() for projected validation or withBatches() for manual low-level handling");
139
+ function materializeRows(batch, columns, projected, target = []) {
140
+ return batch.rowsInto(target, projected ? void 0 : columns);
349
141
  }
350
142
  function rejectFilteredStrictSchema(options) {
351
143
  if (options.where !== void 0)
352
144
  rejectStrictSchemaUnsupported(options, "filtered rows");
353
145
  }
354
- function isPromiseLike(value) {
355
- return typeof value === "object" && value !== null && "then" in value && typeof value.then === "function";
356
- }
357
-
358
- class ScopedCsvRowView {
359
- #selectedColumns;
360
- #rowView = null;
361
- constructor(selectedColumns) {
362
- this.#selectedColumns = selectedColumns;
363
- }
364
- bind(rowView) {
365
- this.#rowView = rowView;
366
- return this;
367
- }
368
- release() {
369
- this.#rowView = null;
370
- }
371
- get rowIndex() {
372
- return this.#requireRowView().rowIndex;
373
- }
374
- get fieldCount() {
375
- return this.#requireRowView().fieldCount;
376
- }
377
- get selectedColumns() {
378
- return this.#selectedColumns;
379
- }
380
- fieldRange(columnIndex) {
381
- return this.#requireRowView().fieldRange(columnIndex);
382
- }
383
- range(columnIndex) {
384
- return this.#requireRowView().range(columnIndex);
385
- }
386
- fieldBytes(columnIndex) {
387
- return this.#requireRowView().fieldBytes(columnIndex);
388
- }
389
- bytes(columnIndex) {
390
- return this.#requireRowView().bytes(columnIndex);
391
- }
392
- fieldBuffer(columnIndex) {
393
- return this.#requireRowView().fieldBuffer(columnIndex);
394
- }
395
- buffer(columnIndex) {
396
- return this.#requireRowView().buffer(columnIndex);
397
- }
398
- fieldString(columnIndex) {
399
- return this.#requireRowView().fieldString(columnIndex);
400
- }
401
- getPhysical(columnIndex) {
402
- return this.#requireRowView().getPhysical(columnIndex);
403
- }
404
- get(columnIndex) {
405
- return this.#requireRowView().get(columnIndex);
406
- }
407
- pickPhysical(columns) {
408
- return this.#requireRowView().pickPhysical(columns);
409
- }
410
- pick(columns) {
411
- return this.#requireRowView().pick(columns);
412
- }
413
- #requireRowView() {
414
- if (this.#rowView === null)
415
- throw Error("row view is only valid during row view callback");
416
- return this.#rowView;
417
- }
418
- }
419
-
420
- class ScopedCsvColumnarBatchView {
421
- #selectedColumns;
422
- #batch = null;
423
- constructor(selectedColumns) {
424
- this.#selectedColumns = selectedColumns;
425
- }
426
- bind(batch) {
427
- this.#batch = batch;
428
- return this;
429
- }
430
- release() {
431
- this.#batch = null;
432
- }
433
- get rowCount() {
434
- return this.#requireBatch().rowCount;
435
- }
436
- get totalFields() {
437
- return this.#requireBatch().totalFields;
438
- }
439
- get dataLength() {
440
- return this.#requireBatch().dataLength;
441
- }
442
- get selectedColumns() {
443
- return this.#selectedColumns;
444
- }
445
- data() {
446
- return this.#requireBatch().data();
447
- }
448
- dataView() {
449
- return this.#requireBatch().dataView();
450
- }
451
- rowOffsets() {
452
- return this.#requireBatch().rowOffsets();
453
- }
454
- fieldOffsets() {
455
- return this.#requireBatch().fieldOffsets();
456
- }
457
- rowFieldCount(rowIndex) {
458
- return this.#requireBatch().rowFieldCount(rowIndex);
459
- }
460
- fieldRange(rowIndex, columnIndex) {
461
- return this.#requireBatch().fieldRange(rowIndex, columnIndex);
462
- }
463
- fieldBytes(rowIndex, columnIndex) {
464
- return this.#requireBatch().fieldBytes(rowIndex, columnIndex);
465
- }
466
- fieldBuffer(rowIndex, columnIndex) {
467
- return this.#requireBatch().fieldBuffer(rowIndex, columnIndex);
468
- }
469
- forEachColumnRange(columnIndex, callback, startRow, endRow) {
470
- return this.#requireBatch().forEachColumnRange(columnIndex, callback, startRow, endRow);
471
- }
472
- forEachColumnBytes(columnIndex, callback, startRow, endRow) {
473
- return this.#requireBatch().forEachColumnBytes(columnIndex, callback, startRow, endRow);
474
- }
475
- scanColumns(columns, callback, startRow, endRow) {
476
- return this.#requireBatch().scanColumns(columns, callback, startRow, endRow);
477
- }
478
- #requireBatch() {
479
- if (this.#batch === null)
480
- throw Error("columnar batch view is only valid during columnar batch callback");
481
- return this.#batch;
482
- }
483
- }
484
146
  function toNativeFilters(where) {
485
147
  return nativeFilterProgramFor(where);
486
148
  }
package/dist/batches.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { type NativePointer } from './native.js';
2
2
  import type { CsvColumnBytesCallback, CsvColumnRangeCallback, CsvColumns, CsvFieldRange, CsvRow, CsvScanColumnsCallback, NativeCsvRowCallback } from './types.js';
3
+ declare const visitBatchRows: unique symbol;
3
4
  /**
4
5
  * A reusable, low-allocation view over one row in a {@link NativeCsvBatch}.
5
6
  *
@@ -49,13 +50,6 @@ export declare class NativeCsvRowView {
49
50
  * @returns A half-open byte range, or `null` when the row has no such field.
50
51
  * @throws {RangeError} If the row or offset table is malformed.
51
52
  */
52
- fieldRange(columnIndex: number): CsvFieldRange | null;
53
- /**
54
- * Returns the current row's field range as a half-open byte interval.
55
- *
56
- * @param columnIndex Zero-based field index in the current row.
57
- * @returns Offsets into the backing data, or `null` when the field is absent.
58
- */
59
53
  range(columnIndex: number): CsvFieldRange | null;
60
54
  /**
61
55
  * Returns a borrowed byte view for a field in the current row.
@@ -66,14 +60,6 @@ export declare class NativeCsvRowView {
66
60
  * @param columnIndex Zero-based field index.
67
61
  * @returns A borrowed byte view, or `null` when the field is absent.
68
62
  */
69
- fieldBytes(columnIndex: number): Uint8Array | null;
70
- /**
71
- * Returns borrowed bytes for a field in the current row.
72
- *
73
- * @param columnIndex Zero-based field index in the current row.
74
- * @returns A view sharing the batch storage, or `null` when the field is absent.
75
- * The view is valid only while the originating batch remains open.
76
- */
77
63
  bytes(columnIndex: number): Uint8Array | null;
78
64
  /**
79
65
  * Returns a borrowed `Buffer` view for a field in the current row.
@@ -84,14 +70,6 @@ export declare class NativeCsvRowView {
84
70
  * @param columnIndex Zero-based field index.
85
71
  * @returns A borrowed buffer, or `null` when the field is absent.
86
72
  */
87
- fieldBuffer(columnIndex: number): Buffer | null;
88
- /**
89
- * Returns a borrowed `Buffer` for a field in the current row.
90
- *
91
- * @param columnIndex Zero-based field index in the current row.
92
- * @returns A buffer sharing the batch storage, or `null` when the field is absent.
93
- * The buffer is valid only while the originating batch remains open.
94
- */
95
73
  buffer(columnIndex: number): Buffer | null;
96
74
  /**
97
75
  * Decodes a field as UTF-8 text.
@@ -99,42 +77,17 @@ export declare class NativeCsvRowView {
99
77
  * @param columnIndex Zero-based field index.
100
78
  * @returns The decoded field, or `null` when the field is absent.
101
79
  */
102
- fieldString(columnIndex: number): string | null;
103
- /**
104
- * Reads a field from the current row as UTF-8 text.
105
- *
106
- * @param columnIndex Zero-based field index in the current row.
107
- * @returns Decoded text, or `null` when the field is absent.
108
- */
109
- getPhysical(columnIndex: number): string | null;
110
- /**
111
- * Reads a field from the current row as UTF-8 text.
112
- *
113
- * This is the concise accessor; use `getPhysical()` when the source-column
114
- * interpretation should be explicit at the call site.
115
- *
116
- * @param columnIndex Zero-based field index in the current row.
117
- * @returns Decoded text, or `null` when the field is absent.
118
- */
119
80
  get(columnIndex: number): string | null;
120
81
  /**
121
82
  * Materializes selected fields from the current row in the requested order.
122
83
  *
123
84
  * Missing fields are represented by empty strings, matching `rowsInto()`.
124
85
  *
125
- * @param columns Zero-based field indexes. Duplicate indexes are allowed for
126
- * this low-level accessor.
86
+ * @param columns Zero-based field indexes captured before the first read. Duplicate indexes are allowed.
127
87
  * @returns A newly allocated array of decoded UTF-8 strings.
128
88
  */
129
- pickPhysical(columns: CsvColumns): string[];
130
- /**
131
- * Materializes selected fields from the current row in the requested order.
132
- *
133
- * @param columns Zero-based field indexes in the current row. Duplicate
134
- * indexes are allowed for this low-level accessor.
135
- * @returns A new array of decoded UTF-8 strings. Missing fields are `''`.
136
- */
137
89
  pick(columns: CsvColumns): string[];
90
+ static [visitBatchRows](batch: NativeCsvBatch, callback: NativeCsvRowCallback): void;
138
91
  }
139
92
  /**
140
93
  * Owns one native parser output batch and exposes low-allocation field views.
@@ -179,9 +132,8 @@ export declare class NativeCsvBatch {
179
132
  /**
180
133
  * Visits each row through one reusable {@link NativeCsvRowView}.
181
134
  *
182
- * The callback is invoked synchronously. The view object and its borrowed
183
- * byte results must not be retained for use after the callback or batch
184
- * lifetime.
135
+ * The callback must be synchronous. Row access is valid only during its call.
136
+ * Copy borrowed bytes before closing the batch.
185
137
  *
186
138
  * @param callback Function invoked with the reusable row view and its row index.
187
139
  */
@@ -195,7 +147,7 @@ export declare class NativeCsvBatch {
195
147
  * missing field is represented by `''`.
196
148
  *
197
149
  * @param target Array to fill. It is truncated or extended to `rowCount`.
198
- * @param columns Optional zero-based field indexes relative to this batch.
150
+ * @param columns Optional zero-based field indexes captured before writing to the target.
199
151
  * Duplicate indexes are rejected.
200
152
  * @returns The same `target` array passed by the caller.
201
153
  * @throws {RangeError} If a column index is invalid, repeated, or an offset
@@ -306,7 +258,7 @@ export declare class NativeCsvBatch {
306
258
  * reported as `[-1, -1]`. Both `ranges` and `data` are reused/borrowed and
307
259
  * must be consumed synchronously by the callback.
308
260
  *
309
- * @param columns Zero-based field indexes relative to each row.
261
+ * @param columns Zero-based field indexes relative to each row, copied once at the start of the scan.
310
262
  * @param callback Receives `(rowIndex, ranges, data)` for each row.
311
263
  * @param startRow Inclusive first row; defaults to `0`.
312
264
  * @param endRow Exclusive end row; defaults to `rowCount`.
@@ -331,8 +283,7 @@ export declare class NativeCsvBatch {
331
283
  * Calling `close()` more than once is safe.
332
284
  */
333
285
  close(): void;
334
- /** Alias for {@link close}, suitable for explicit resource management. */
335
- dispose(): void;
336
286
  /** Implements `using` / explicit resource management. */
337
287
  [Symbol.dispose](): void;
338
288
  }
289
+ export {};