@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/parser.js CHANGED
@@ -2,144 +2,160 @@ import { NativeCsvBatch } from "./batches.js";
2
2
  import {
3
3
  EMPTY_BUFFER,
4
4
  native,
5
- optionalPreparedParserNative,
6
5
  u64ToSafeNumber
7
6
  } from "./native.js";
8
7
  import {
9
8
  encodingCode,
10
9
  normalizeChunk,
11
10
  normalizeColumns,
12
- normalizeEqualsFilter,
13
11
  normalizeFixedColumnsCount,
14
- normalizeInFilter,
15
12
  normalizeNativeFilterProgram,
16
13
  normalizeNativeFilters,
17
- normalizeStartsWithFilter
14
+ projectionFilterProgram,
15
+ rejectSelectedColumns
18
16
  } from "./normalize.js";
17
+ import {
18
+ strictSchemaValidator
19
+ } from "./strict-schema.js";
19
20
  function destroyNativeCsvParser(handle) {
20
21
  native.symbols.csv_parser_destroy(handle);
21
22
  }
22
- const nativeCsvParserFinalizer = new FinalizationRegistry(destroyNativeCsvParser);
23
- export function createPreparedParser(options, selectedColumns, program, projected = !1) {
24
- if (!projected && selectedColumns === void 0 && program === void 0)
25
- return new NativeCsvParser(options);
26
- if (options.strict === !0)
27
- throw Error("strict CSV validation is not supported for projected batches");
28
- if (options.selectedColumns !== selectedColumns)
29
- normalizeColumns(options.selectedColumns);
30
- normalizeFixedColumnsCount(options.fixedColumns, "fixed column count");
31
- return new PreparedNativeCsvParser(options, selectedColumns, program);
23
+ export const nativeCsvParserFinalizer = new FinalizationRegistry(destroyNativeCsvParser);
24
+ export async function* parseFileChunks(input, parser, limit) {
25
+ let remaining = limit;
26
+ if (remaining !== 0)
27
+ for await (const chunk of input.chunks()) {
28
+ const batch = parser.writeBatch(chunk);
29
+ const count = batch.rowCount;
30
+ if (count > 0)
31
+ yield batch;
32
+ else
33
+ batch.close();
34
+ if (remaining !== void 0) {
35
+ remaining -= count;
36
+ if (remaining === 0)
37
+ break;
38
+ }
39
+ }
40
+ const final = parser.endBatch();
41
+ if (final.rowCount > 0)
42
+ yield final;
43
+ else
44
+ final.close();
32
45
  }
33
- export function prepareNativeParser(handle, selectedColumns, program) {
34
- const columns = normalizeColumns(selectedColumns);
35
- const hasProjection = selectedColumns !== void 0;
36
- const columnCount = BigInt(selectedColumns?.length ?? 0);
46
+ export async function countFileChunks(input, parser, options) {
47
+ let total = 0;
48
+ if (options.strict === !0) {
49
+ for await (using batch of parseFileChunks(input, parser, options.limit))
50
+ total += batch.rowCount;
51
+ return total;
52
+ }
53
+ if (options.limit !== 0)
54
+ for await (const chunk of input.chunks()) {
55
+ total += parser.writeCount(chunk);
56
+ if (total === options.limit)
57
+ break;
58
+ }
59
+ return total + parser.endCount();
60
+ }
61
+ export function createPreparedParser(options, columns, program) {
62
+ if (options.columns !== columns)
63
+ normalizeColumns(options.columns);
64
+ return NativeCsvParser[createSchemaParser]({
65
+ encoding: options.encoding,
66
+ delimiter: options.delimiter,
67
+ fixedColumns: options.fixedColumns,
68
+ strict: options.strict,
69
+ skipRows: options.skipRows,
70
+ limit: options.limit,
71
+ expectedHeaders: options.expectedHeaders,
72
+ requireHeader: options.requireHeader,
73
+ minDataRows: options.minDataRows,
74
+ columns,
75
+ filterProgram: program
76
+ });
77
+ }
78
+ export function prepareNativeParser(handle, columns, program) {
79
+ const normalizedColumns = normalizeColumns(columns);
80
+ const hasProjection = columns !== void 0;
81
+ const columnCount = BigInt(normalizedColumns.length);
37
82
  const normalized = program === void 0 ? normalizeNativeFilters(void 0) : normalizeNativeFilterProgram(program);
38
83
  const filterCount = BigInt(normalized.filterCount);
39
84
  const valueCount = BigInt(normalized.valueCount);
40
- const prepared = optionalPreparedParserNative();
41
- if (prepared !== null) {
42
- if (!prepared.symbols.csv_parser_prepare(handle, hasProjection, columns, columnCount, normalized.descriptors, filterCount, normalized.valuesData, normalized.valuesData, normalized.valueOffsets, valueCount))
43
- throw Error(`native CSV parser failed: ${native.symbols.csv_parser_last_error(handle) ?? "native parser error unavailable"}`);
44
- return {
45
- writeBatch: (input, final) => prepared.symbols.csv_parser_write_prepared_batch(handle, input, input, final),
46
- writeCount: (input, final) => {
47
- const count = prepared.symbols.csv_parser_write_prepared_count(handle, input, input, final);
48
- if (count === 0xffffffffffffffffn)
49
- throw Error(`native CSV parser failed: ${native.symbols.csv_parser_last_error(handle) ?? "native parser error unavailable"}`);
50
- return count;
51
- }
52
- };
53
- }
85
+ if (!native.symbols.csv_parser_prepare(handle, hasProjection, normalizedColumns, columnCount, normalized.descriptors, filterCount, normalized.valuesData, normalized.valuesData, normalized.valueOffsets, valueCount))
86
+ throw Error(`native CSV parser failed: ${native.symbols.csv_parser_last_error(handle) ?? "native parser error unavailable"}`);
54
87
  return {
55
- writeBatch: (input, final) => native.symbols.csv_parser_write_projected_batch_where_all(handle, input, input, final, hasProjection, columns, columnCount, normalized.descriptors, filterCount, normalized.valuesData, normalized.valuesData, normalized.valueOffsets, valueCount),
88
+ writeBatch: (input, final) => native.symbols.csv_parser_write_prepared_batch(handle, input, input, final),
56
89
  writeCount: (input, final) => {
57
- const count = native.symbols.csv_parser_write_count_where_all(handle, input, input, final, normalized.descriptors, filterCount, normalized.valuesData, normalized.valuesData, normalized.valueOffsets, valueCount);
58
- const error = native.symbols.csv_parser_last_error(handle);
59
- if (error !== null && error.length > 0)
60
- throw Error(`native CSV parser failed: ${error}`);
90
+ const count = native.symbols.csv_parser_write_prepared_count(handle, input, input, final);
91
+ if (count === 0xffffffffffffffffn)
92
+ throw Error(`native CSV parser failed: ${native.symbols.csv_parser_last_error(handle) ?? "native parser error unavailable"}`);
61
93
  return count;
62
94
  }
63
95
  };
64
96
  }
65
-
66
- class PreparedNativeCsvParser {
67
- #handle;
68
- #writer;
69
- #fixedValidator;
70
- constructor(options, selectedColumns, program) {
71
- const handle = createNativeCsvParser(options);
72
- try {
73
- this.#writer = prepareNativeParser(handle, selectedColumns, program);
74
- this.#fixedValidator = options.fixedColumns === void 0 ? void 0 : new NativeCsvParser(options);
75
- } catch (error) {
76
- destroyNativeCsvParser(handle);
77
- throw error;
78
- }
79
- this.#handle = handle;
80
- nativeCsvParserFinalizer.register(this, handle, this);
81
- }
82
- writeBatch(chunk, final = !1) {
83
- const handle = this.#requireHandle();
84
- if (this.#fixedValidator !== void 0)
85
- this.#fixedValidator.writeBatch(chunk, final).close();
86
- const batch = this.#writer.writeBatch(normalizeChunk(chunk), final);
87
- if (batch === null)
88
- throw Error(`native CSV parser failed: ${native.symbols.csv_parser_last_error(handle) ?? "native parser error unavailable"}`);
89
- return new NativeCsvBatch(batch);
90
- }
91
- endBatch() {
92
- return this.writeBatch(EMPTY_BUFFER, !0);
93
- }
94
- writeCount(chunk, final = !1) {
95
- this.#requireHandle();
96
- if (this.#fixedValidator !== void 0)
97
- this.#fixedValidator.writeBatch(chunk, final).close();
98
- const count = this.#writer.writeCount(normalizeChunk(chunk), final);
99
- return u64ToSafeNumber(count, "CSV filtered row count");
100
- }
101
- endCount() {
102
- return this.writeCount(EMPTY_BUFFER, !0);
103
- }
104
- close() {
105
- const handle = this.#handle;
106
- if (handle === null)
107
- return;
108
- this.#handle = null;
109
- nativeCsvParserFinalizer.unregister(this);
110
- this.#fixedValidator?.close();
111
- destroyNativeCsvParser(handle);
112
- }
113
- [Symbol.dispose]() {
114
- this.close();
115
- }
116
- #requireHandle() {
117
- if (this.#handle === null)
118
- throw Error("native CSV parser is closed");
119
- return this.#handle;
120
- }
97
+ export function validateRowSelection({ skipRows, limit }) {
98
+ if (skipRows !== void 0 && (!Number.isSafeInteger(skipRows) || skipRows < 0))
99
+ throw RangeError("skipRows must be a non-negative safe integer");
100
+ if (limit !== void 0 && (!Number.isSafeInteger(limit) || limit < 0))
101
+ throw RangeError("limit must be a non-negative safe integer");
102
+ }
103
+ export function setRowSelection(handle, options, captureHeader = !1) {
104
+ if (options.skipRows !== void 0 || options.limit !== void 0)
105
+ native.symbols.csv_parser_set_row_selection(handle, BigInt(options.skipRows ?? 0), options.limit === void 0 ? 0xffffffffffffffffn : BigInt(options.limit), captureHeader);
121
106
  }
122
- function createNativeCsvParser(options) {
107
+ function createNativeCsvParser(options, prepared = !1) {
108
+ const { skipRows, limit } = options;
109
+ validateRowSelection({ skipRows, limit });
123
110
  const delimiter = options.delimiter ?? ",";
124
111
  if (delimiter === "auto")
125
112
  throw Error("automatic delimiter detection is only supported by file APIs");
126
113
  if (delimiter.length !== 1)
127
114
  throw Error("delimiter must be one character");
128
- const handle = native.symbols.csv_parser_create(encodingCode(options.encoding), delimiter.charCodeAt(0));
115
+ const encoding = encodingCode(options.encoding);
116
+ const handle = prepared ? native.symbols.csv_parser_create_prepared(encoding, delimiter.charCodeAt(0), options.fixedColumns ?? 0) : native.symbols.csv_parser_create(encoding, delimiter.charCodeAt(0));
129
117
  if (handle === null)
130
118
  throw Error("failed to create native CSV parser");
119
+ setRowSelection(handle, { skipRows, limit });
131
120
  return handle;
132
121
  }
122
+ const createSchemaParser = Symbol("createSchemaParser");
133
123
 
134
124
  export class NativeCsvParser {
135
125
  #handle;
136
126
  #strict;
137
127
  #fixedColumns;
128
+ #writer;
129
+ #validator;
130
+ #captureSkippedHeader = !1;
131
+ static [createSchemaParser](options) {
132
+ const validator = strictSchemaValidator(options);
133
+ const parser = new NativeCsvParser(options);
134
+ parser.#validator = validator;
135
+ if (validator !== void 0 && (options.skipRows ?? 0) > 0) {
136
+ parser.#captureSkippedHeader = !0;
137
+ setRowSelection(parser.#requireHandle(), options, !0);
138
+ }
139
+ return parser;
140
+ }
138
141
  constructor(options = {}) {
142
+ rejectSelectedColumns(options);
139
143
  this.#strict = options.strict === !0;
140
- normalizeColumns(options.selectedColumns);
141
144
  this.#fixedColumns = normalizeFixedColumnsCount(options.fixedColumns, "fixed column count");
142
- const handle = createNativeCsvParser(options);
145
+ const { columns, filterProgram } = options;
146
+ if (filterProgram !== void 0 && (options.equalsFilter !== void 0 || options.filters !== void 0))
147
+ throw Error("filterProgram cannot be combined with equalsFilter or filters");
148
+ const program = filterProgram === void 0 ? projectionFilterProgram(options) : filterProgram;
149
+ if (this.#strict && (columns !== void 0 || program !== void 0))
150
+ throw Error("strict CSV validation is not supported for projected batches or filters");
151
+ const prepared = columns !== void 0 || program !== void 0;
152
+ const handle = createNativeCsvParser(options, prepared);
153
+ try {
154
+ this.#writer = prepared ? prepareNativeParser(handle, columns, program) : void 0;
155
+ } catch (error) {
156
+ destroyNativeCsvParser(handle);
157
+ throw error;
158
+ }
143
159
  this.#handle = handle;
144
160
  nativeCsvParserFinalizer.register(this, handle, this);
145
161
  }
@@ -155,29 +171,12 @@ export class NativeCsvParser {
155
171
  }
156
172
  }
157
173
  writeBatch(chunk, final = !1) {
158
- if (chunk.byteLength === 0 && final)
159
- return this.endBatch();
160
174
  const handle = this.#requireHandle();
161
175
  const input = normalizeChunk(chunk);
162
- const batch = this.#strict && this.#fixedColumns !== void 0 ? native.symbols.csv_parser_write_strict_fixed_batch(handle, input, input, final, this.#fixedColumns) : this.#strict ? native.symbols.csv_parser_write_strict_batch(handle, input, input, final) : this.#fixedColumns !== void 0 ? native.symbols.csv_parser_write_fixed_batch(handle, input, input, final, this.#fixedColumns) : native.symbols.csv_parser_write_batch(handle, input, input, final);
176
+ const batch = this.#writer !== void 0 ? this.#writer.writeBatch(input, final) : this.#strict && this.#fixedColumns !== void 0 ? native.symbols.csv_parser_write_strict_fixed_batch(handle, input, input, final, this.#fixedColumns) : this.#strict ? native.symbols.csv_parser_write_strict_batch(handle, input, input, final) : this.#fixedColumns !== void 0 ? native.symbols.csv_parser_write_fixed_batch(handle, input, input, final, this.#fixedColumns) : native.symbols.csv_parser_write_batch(handle, input, input, final);
163
177
  if (batch === null)
164
178
  throw Error(`native CSV parser failed: ${this.#lastError()}`);
165
- return new NativeCsvBatch(batch);
166
- }
167
- writeProjectedBatch(chunk, options = {}, final = !1) {
168
- this.#rejectStrictUnsupported("projected batches");
169
- this.#rejectProjectionFilterConflict(options);
170
- if (chunk.byteLength === 0 && final)
171
- return this.endProjectedBatch(options);
172
- const handle = this.#requireHandle();
173
- const input = normalizeChunk(chunk);
174
- const columns = normalizeColumns(options.selectedColumns);
175
- const filter = normalizeEqualsFilter(options.equalsFilter);
176
- const filters = normalizeNativeFilters(options.filters);
177
- const batch = options.filters === void 0 ? native.symbols.csv_parser_write_projected_batch(handle, input, input, final, options.selectedColumns !== void 0, columns, BigInt(options.selectedColumns?.length ?? 0), filter.enabled, filter.column, filter.value, filter.value) : native.symbols.csv_parser_write_projected_batch_where_all(handle, input, input, final, options.selectedColumns !== void 0, columns, BigInt(options.selectedColumns?.length ?? 0), filters.descriptors, BigInt(filters.filterCount), filters.valuesData, filters.valuesData, filters.valueOffsets, BigInt(filters.valueCount));
178
- if (batch === null)
179
- throw Error(`native CSV parser failed: ${this.#lastError()}`);
180
- return new NativeCsvBatch(batch);
179
+ return this.#validatedBatch(batch, final);
181
180
  }
182
181
  end() {
183
182
  const batch = this.endBatch();
@@ -188,144 +187,24 @@ export class NativeCsvParser {
188
187
  }
189
188
  }
190
189
  endBatch() {
191
- const batch = this.#strict && this.#fixedColumns !== void 0 ? native.symbols.csv_parser_finish_strict_fixed_batch(this.#requireHandle(), this.#fixedColumns) : this.#strict ? native.symbols.csv_parser_finish_strict_batch(this.#requireHandle()) : this.#fixedColumns !== void 0 ? native.symbols.csv_parser_finish_fixed_batch(this.#requireHandle(), this.#fixedColumns) : native.symbols.csv_parser_finish_batch(this.#requireHandle());
192
- if (batch === null)
193
- throw Error(`native CSV parser failed: ${this.#lastError()}`);
194
- return new NativeCsvBatch(batch);
195
- }
196
- endProjectedBatch(options = {}) {
197
- this.#rejectStrictUnsupported("projected batches");
198
- this.#rejectProjectionFilterConflict(options);
199
- const columns = normalizeColumns(options.selectedColumns);
200
- const filter = normalizeEqualsFilter(options.equalsFilter);
201
- const filters = normalizeNativeFilters(options.filters);
202
- const batch = options.filters === void 0 ? native.symbols.csv_parser_finish_projected_batch(this.#requireHandle(), options.selectedColumns !== void 0, columns, BigInt(options.selectedColumns?.length ?? 0), filter.enabled, filter.column, filter.value, filter.value) : native.symbols.csv_parser_finish_projected_batch_where_all(this.#requireHandle(), options.selectedColumns !== void 0, columns, BigInt(options.selectedColumns?.length ?? 0), filters.descriptors, BigInt(filters.filterCount), filters.valuesData, filters.valuesData, filters.valueOffsets, BigInt(filters.valueCount));
203
- if (batch === null)
204
- throw Error(`native CSV parser failed: ${this.#lastError()}`);
205
- return new NativeCsvBatch(batch);
206
- }
207
- writeProjectedBatchWhere(chunk, selectedColumns, program, final = !1) {
208
- this.#rejectStrictUnsupported("projected batches");
209
- if (chunk.byteLength === 0 && final)
210
- return this.endProjectedBatchWhere(selectedColumns, program);
211
- const handle = this.#requireHandle();
212
- const input = normalizeChunk(chunk);
213
- const columns = normalizeColumns(selectedColumns);
214
- const normalized = normalizeNativeFilterProgram(program);
215
- const batch = native.symbols.csv_parser_write_projected_batch_where_all(handle, input, input, final, selectedColumns !== void 0, columns, BigInt(selectedColumns?.length ?? 0), normalized.descriptors, BigInt(normalized.filterCount), normalized.valuesData, normalized.valuesData, normalized.valueOffsets, BigInt(normalized.valueCount));
216
- if (batch === null)
217
- throw Error(`native CSV parser failed: ${this.#lastError()}`);
218
- return new NativeCsvBatch(batch);
219
- }
220
- endProjectedBatchWhere(selectedColumns, program) {
221
- this.#rejectStrictUnsupported("projected batches");
222
- const columns = normalizeColumns(selectedColumns);
223
- const normalized = normalizeNativeFilterProgram(program);
224
- const batch = native.symbols.csv_parser_finish_projected_batch_where_all(this.#requireHandle(), selectedColumns !== void 0, columns, BigInt(selectedColumns?.length ?? 0), normalized.descriptors, BigInt(normalized.filterCount), normalized.valuesData, normalized.valuesData, normalized.valueOffsets, BigInt(normalized.valueCount));
225
- if (batch === null)
226
- throw Error(`native CSV parser failed: ${this.#lastError()}`);
227
- return new NativeCsvBatch(batch);
190
+ return this.writeBatch(EMPTY_BUFFER, !0);
228
191
  }
229
192
  writeCount(chunk, final = !1) {
230
- this.#rejectStrictUnsupported("count");
231
- if (this.#fixedColumns !== void 0) {
193
+ if (this.#strict)
194
+ throw Error("strict CSV validation is not supported for count");
195
+ if (this.#writer === void 0 && this.#fixedColumns !== void 0) {
232
196
  using batch = this.writeBatch(chunk, final);
233
197
  return batch.rowCount;
234
198
  }
235
- if (chunk.byteLength === 0 && final)
236
- return this.endCount();
237
199
  const handle = this.#requireHandle();
238
200
  const input = normalizeChunk(chunk);
239
- return u64ToSafeNumber(native.symbols.csv_parser_write_count(handle, input, input, final), "CSV parsed row count");
201
+ return u64ToSafeNumber(this.#writer === void 0 ? native.symbols.csv_parser_write_count(handle, input, input, final) : this.#writer.writeCount(input, final), "CSV parsed row count");
240
202
  }
241
203
  endCount() {
242
- this.#rejectStrictUnsupported("count");
243
- if (this.#fixedColumns !== void 0) {
244
- using batch = this.endBatch();
245
- return batch.rowCount;
246
- }
247
- return u64ToSafeNumber(native.symbols.csv_parser_finish_count(this.#requireHandle()), "CSV parsed row count");
248
- }
249
- writeCountWhereEquals(chunk, filter, final = !1) {
250
- this.#rejectStrictUnsupported("count filters");
251
- if (chunk.byteLength === 0 && final)
252
- return this.endCountWhereEquals(filter);
253
- const handle = this.#requireHandle();
254
- const input = normalizeChunk(chunk);
255
- const normalized = normalizeEqualsFilter(filter);
256
- return u64ToSafeNumber(native.symbols.csv_parser_write_count_where_equals(handle, input, input, final, normalized.column, normalized.value, normalized.value), "CSV filtered row count");
257
- }
258
- endCountWhereEquals(filter) {
259
- this.#rejectStrictUnsupported("count filters");
260
- const normalized = normalizeEqualsFilter(filter);
261
- return u64ToSafeNumber(native.symbols.csv_parser_finish_count_where_equals(this.#requireHandle(), normalized.column, normalized.value, normalized.value), "CSV filtered row count");
262
- }
263
- writeCountWhereIn(chunk, filter, final = !1) {
264
- this.#rejectStrictUnsupported("count filters");
265
- if (chunk.byteLength === 0 && final)
266
- return this.endCountWhereIn(filter);
267
- const normalized = normalizeInFilter(filter);
268
- const handle = this.#requireHandle();
269
- const input = normalizeChunk(chunk);
270
- return u64ToSafeNumber(native.symbols.csv_parser_write_count_where_in(handle, input, input, final, normalized.column, normalized.valuesData, normalized.valuesData, normalized.offsets, BigInt(normalized.valueCount)), "CSV filtered row count");
271
- }
272
- endCountWhereIn(filter) {
273
- this.#rejectStrictUnsupported("count filters");
274
- const normalized = normalizeInFilter(filter);
275
- return u64ToSafeNumber(native.symbols.csv_parser_finish_count_where_in(this.#requireHandle(), normalized.column, normalized.valuesData, normalized.valuesData, normalized.offsets, BigInt(normalized.valueCount)), "CSV filtered row count");
276
- }
277
- writeCountWhereStartsWith(chunk, filter, final = !1) {
278
- this.#rejectStrictUnsupported("count filters");
279
- if (chunk.byteLength === 0 && final)
280
- return this.endCountWhereStartsWith(filter);
281
- const normalized = normalizeStartsWithFilter(filter);
282
- const handle = this.#requireHandle();
283
- const input = normalizeChunk(chunk);
284
- return u64ToSafeNumber(native.symbols.csv_parser_write_count_where_starts_with(handle, input, input, final, normalized.column, normalized.value, normalized.value), "CSV filtered row count");
285
- }
286
- endCountWhereStartsWith(filter) {
287
- this.#rejectStrictUnsupported("count filters");
288
- const normalized = normalizeStartsWithFilter(filter);
289
- return u64ToSafeNumber(native.symbols.csv_parser_finish_count_where_starts_with(this.#requireHandle(), normalized.column, normalized.value, normalized.value), "CSV filtered row count");
290
- }
291
- writeCountWhereAll(chunk, filters, final = !1) {
292
- this.#rejectStrictUnsupported("count filters");
293
- if (chunk.byteLength === 0 && final)
294
- return this.endCountWhereAll(filters);
295
- const normalized = normalizeNativeFilters(filters);
296
- const handle = this.#requireHandle();
297
- const input = normalizeChunk(chunk);
298
- const count = native.symbols.csv_parser_write_count_where_all(handle, input, input, final, normalized.descriptors, BigInt(normalized.filterCount), normalized.valuesData, normalized.valuesData, normalized.valueOffsets, BigInt(normalized.valueCount));
299
- this.#throwIfNativeError();
300
- return u64ToSafeNumber(count, "CSV filtered row count");
301
- }
302
- endCountWhereAll(filters) {
303
- this.#rejectStrictUnsupported("count filters");
304
- const normalized = normalizeNativeFilters(filters);
305
- const count = native.symbols.csv_parser_finish_count_where_all(this.#requireHandle(), normalized.descriptors, BigInt(normalized.filterCount), normalized.valuesData, normalized.valuesData, normalized.valueOffsets, BigInt(normalized.valueCount));
306
- this.#throwIfNativeError();
307
- return u64ToSafeNumber(count, "CSV filtered row count");
308
- }
309
- writeCountWhere(chunk, program, final = !1) {
310
- this.#rejectStrictUnsupported("count filters");
311
- if (chunk.byteLength === 0 && final)
312
- return this.endCountWhere(program);
313
- const normalized = normalizeNativeFilterProgram(program);
314
- const handle = this.#requireHandle();
315
- const input = normalizeChunk(chunk);
316
- const count = native.symbols.csv_parser_write_count_where_all(handle, input, input, final, normalized.descriptors, BigInt(normalized.filterCount), normalized.valuesData, normalized.valuesData, normalized.valueOffsets, BigInt(normalized.valueCount));
317
- this.#throwIfNativeError();
318
- return u64ToSafeNumber(count, "CSV filtered row count");
319
- }
320
- endCountWhere(program) {
321
- this.#rejectStrictUnsupported("count filters");
322
- const normalized = normalizeNativeFilterProgram(program);
323
- const count = native.symbols.csv_parser_finish_count_where_all(this.#requireHandle(), normalized.descriptors, BigInt(normalized.filterCount), normalized.valuesData, normalized.valuesData, normalized.valueOffsets, BigInt(normalized.valueCount));
324
- this.#throwIfNativeError();
325
- return u64ToSafeNumber(count, "CSV filtered row count");
204
+ return this.writeCount(EMPTY_BUFFER, !0);
326
205
  }
327
206
  reset() {
328
- native.symbols.csv_parser_reset(this.#requireHandle());
207
+ native.symbols.csv_parser_reset_stream(this.#requireHandle());
329
208
  }
330
209
  close() {
331
210
  const handle = this.#handle;
@@ -335,9 +214,6 @@ export class NativeCsvParser {
335
214
  nativeCsvParserFinalizer.unregister(this);
336
215
  destroyNativeCsvParser(handle);
337
216
  }
338
- dispose() {
339
- this.close();
340
- }
341
217
  [Symbol.dispose]() {
342
218
  this.close();
343
219
  }
@@ -347,21 +223,30 @@ export class NativeCsvParser {
347
223
  return this.#handle;
348
224
  }
349
225
  #lastError() {
350
- if (this.#handle === null)
351
- return "parser is closed";
352
- return native.symbols.csv_parser_last_error(this.#handle) ?? "native parser error unavailable";
353
- }
354
- #throwIfNativeError() {
355
- const error = this.#lastError();
356
- if (error.length > 0)
357
- throw Error(`native CSV parser failed: ${error}`);
358
- }
359
- #rejectStrictUnsupported(operation) {
360
- if (this.#strict)
361
- throw Error(`strict CSV validation is not supported for ${operation}`);
226
+ return native.symbols.csv_parser_last_error(this.#requireHandle()) ?? "native parser error unavailable";
362
227
  }
363
- #rejectProjectionFilterConflict(options) {
364
- if (options.equalsFilter !== void 0 && options.filters !== void 0)
365
- throw Error("use equalsFilter or filters, not both");
228
+ #validatedBatch(pointer, final) {
229
+ const batch = new NativeCsvBatch(pointer);
230
+ if (this.#validator === void 0)
231
+ return batch;
232
+ try {
233
+ const handle = this.#requireHandle();
234
+ if (this.#captureSkippedHeader) {
235
+ const header = native.symbols.csv_parser_take_skipped_header(handle);
236
+ if (header !== null) {
237
+ using first = new NativeCsvBatch(header);
238
+ this.#validator.validateBatch(first);
239
+ }
240
+ }
241
+ this.#validator.validateBatch(batch);
242
+ if (final) {
243
+ const skipped = this.#captureSkippedHeader ? u64ToSafeNumber(native.symbols.csv_parser_skipped_rows(handle), "CSV skipped row count") : 0;
244
+ this.#validator.finish(Math.max(0, skipped - 1));
245
+ }
246
+ return batch;
247
+ } catch (error) {
248
+ batch.close();
249
+ throw error;
250
+ }
366
251
  }
367
252
  }
@@ -1,12 +1,14 @@
1
1
  import type { NativeCsvBatch } from './batches.js';
2
2
  import type { CsvParserOptions, CsvRow } from './types.js';
3
+ type StrictSchemaOptions = Pick<CsvParserOptions, 'strict' | 'expectedHeaders' | 'requireHeader' | 'minDataRows'>;
3
4
  export declare class CsvStrictSchemaValidator {
4
5
  #private;
5
- constructor(suppliedOptions: CsvParserOptions);
6
+ constructor(suppliedOptions: StrictSchemaOptions);
6
7
  get enabled(): boolean;
7
8
  validateRows(rows: readonly CsvRow[]): void;
8
9
  validateBatch(batch: NativeCsvBatch): void;
9
- finish(): void;
10
+ finish(skippedDataRows?: number): void;
10
11
  }
11
- export declare function strictSchemaValidator(options: CsvParserOptions): CsvStrictSchemaValidator | undefined;
12
- export declare function rejectStrictSchemaUnsupported(options: CsvParserOptions, operation: string): void;
12
+ export declare function strictSchemaValidator(options: StrictSchemaOptions): CsvStrictSchemaValidator | undefined;
13
+ export declare function rejectStrictSchemaUnsupported(options: StrictSchemaOptions, operation: string): void;
14
+ export {};
@@ -24,14 +24,15 @@ export class CsvStrictSchemaValidator {
24
24
  for (let rowIndex = 0;rowIndex < rowCount; ++rowIndex)
25
25
  this.#validateHeaderFromBatch(batch, rowIndex);
26
26
  }
27
- finish() {
27
+ finish(skippedDataRows = 0) {
28
28
  if (!this.enabled)
29
29
  return;
30
30
  if (!this.#seenHeader && (this.#options.requireHeader === !0 || this.#options.expectedHeaders !== void 0))
31
31
  throw Error("strict CSV schema error: missing header row");
32
32
  const minDataRows = this.#options.minDataRows;
33
- if (minDataRows !== void 0 && this.#dataRows < minDataRows)
34
- throw Error(`strict CSV schema error: expected at least ${String(minDataRows)} data row(s), got ${String(this.#dataRows)}`);
33
+ const dataRows = this.#dataRows + skippedDataRows;
34
+ if (minDataRows !== void 0 && dataRows < minDataRows)
35
+ throw Error(`strict CSV schema error: expected at least ${String(minDataRows)} data row(s), got ${String(dataRows)}`);
35
36
  }
36
37
  #validateRow(row) {
37
38
  if (!this.#seenHeader) {