@konstit/csv 0.5.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/README.md +36 -3
  2. package/THIRD_PARTY_NOTICES.md +99 -0
  3. package/dist/api.d.ts +3 -0
  4. package/dist/api.js +34 -108
  5. package/dist/batches.d.ts +3 -4
  6. package/dist/batches.js +103 -29
  7. package/dist/cli-command.js +145 -75
  8. package/dist/cli-schema.d.ts +25 -5
  9. package/dist/cli-schema.js +48 -23
  10. package/dist/file-stream.d.ts +10 -3
  11. package/dist/file-stream.js +22 -17
  12. package/dist/files.js +32 -76
  13. package/dist/filter.d.ts +2 -3
  14. package/dist/filter.js +2 -0
  15. package/dist/index.d.ts +1 -1
  16. package/dist/inspect-file.d.ts +16 -6
  17. package/dist/inspect-file.js +67 -17
  18. package/dist/latin1-rows.d.ts +3 -3
  19. package/dist/latin1-rows.js +65 -15
  20. package/dist/materialize-field.d.ts +1 -0
  21. package/dist/materialize-field.js +10 -1
  22. package/dist/native.d.ts +47 -71
  23. package/dist/native.js +28 -86
  24. package/dist/normalize.d.ts +1 -40
  25. package/dist/normalize.js +25 -62
  26. package/dist/options-snapshot.d.ts +2 -2
  27. package/dist/options-snapshot.js +2 -0
  28. package/dist/parser.d.ts +17 -202
  29. package/dist/parser.js +152 -230
  30. package/dist/strict-schema.d.ts +1 -1
  31. package/dist/strict-schema.js +4 -3
  32. package/dist/transforms.d.ts +11 -0
  33. package/dist/transforms.js +192 -0
  34. package/dist/types.d.ts +77 -9
  35. package/dist/zip-reader.d.ts +2 -0
  36. package/dist/zip-reader.js +31 -21
  37. package/package.json +3 -2
  38. package/prebuilds/darwin-arm64/libcsv_native.dylib +0 -0
  39. package/prebuilds/darwin-x64/libcsv_native.dylib +0 -0
  40. package/prebuilds/linux-x64/libcsv_native.so +0 -0
  41. package/prebuilds/manifest.json +14 -8
  42. package/prebuilds/provenance/darwin-arm64.json +1 -1
  43. package/prebuilds/provenance/darwin-x64.json +1 -1
  44. package/prebuilds/provenance/linux-x64.json +1 -1
package/dist/parser.js CHANGED
@@ -8,32 +8,85 @@ import {
8
8
  encodingCode,
9
9
  normalizeChunk,
10
10
  normalizeColumns,
11
- normalizeEqualsFilter,
12
11
  normalizeFixedColumnsCount,
13
- normalizeInFilter,
14
12
  normalizeNativeFilterProgram,
15
13
  normalizeNativeFilters,
16
- normalizeStartsWithFilter,
14
+ projectionFilterProgram,
17
15
  rejectSelectedColumns
18
16
  } from "./normalize.js";
17
+ import {
18
+ strictSchemaValidator
19
+ } from "./strict-schema.js";
20
+ import {
21
+ prepareTransforms,
22
+ selectTransforms,
23
+ snapshotTransforms
24
+ } from "./transforms.js";
19
25
  function destroyNativeCsvParser(handle) {
20
26
  native.symbols.csv_parser_destroy(handle);
21
27
  }
22
28
  export const nativeCsvParserFinalizer = new FinalizationRegistry(destroyNativeCsvParser);
23
- export function createPreparedParser(options, columns, program, projected = !1) {
24
- if (!projected && columns === 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");
29
+ export async function* parseFileChunks(input, parser, limit) {
30
+ let remaining = limit;
31
+ if (remaining !== 0)
32
+ for await (const chunk of input.chunks()) {
33
+ const batch = parser.writeBatch(chunk);
34
+ const count = batch.rowCount;
35
+ if (count > 0)
36
+ yield batch;
37
+ else
38
+ batch.close();
39
+ if (remaining !== void 0) {
40
+ remaining -= count;
41
+ if (remaining === 0)
42
+ break;
43
+ }
44
+ }
45
+ const final = parser.endBatch();
46
+ if (final.rowCount > 0)
47
+ yield final;
48
+ else
49
+ final.close();
50
+ }
51
+ export async function countFileChunks(input, options, filters) {
52
+ const columns = options.fixedColumns !== void 0 && options.strict !== !0 && filters === void 0 ? [] : void 0;
53
+ using parser = createPreparedParser(options, columns, filters);
54
+ let total = 0;
55
+ if (options.strict === !0) {
56
+ for await (using batch of parseFileChunks(input, parser, options.limit))
57
+ total += batch.rowCount;
58
+ return total;
59
+ }
60
+ if (options.limit !== 0)
61
+ for await (const chunk of input.chunks()) {
62
+ total += parser.writeCount(chunk);
63
+ if (total === options.limit)
64
+ break;
65
+ }
66
+ return total + parser.endCount();
67
+ }
68
+ export function createPreparedParser(options, columns, program, transform, output = "batches") {
28
69
  if (options.columns !== columns)
29
70
  normalizeColumns(options.columns);
30
- normalizeFixedColumnsCount(options.fixedColumns, "fixed column count");
31
- return new PreparedNativeCsvParser(options, columns, program);
71
+ return NativeCsvParser[createSchemaParser]({
72
+ encoding: options.encoding,
73
+ delimiter: options.delimiter,
74
+ fixedColumns: options.fixedColumns,
75
+ strict: options.strict,
76
+ skipRows: options.skipRows,
77
+ limit: options.limit,
78
+ expectedHeaders: options.expectedHeaders,
79
+ requireHeader: options.requireHeader,
80
+ minDataRows: options.minDataRows,
81
+ columns,
82
+ filterProgram: program,
83
+ transform: options.strict && options.columns && transform ? selectTransforms(transform, options.columns) : transform
84
+ }, output);
32
85
  }
33
86
  export function prepareNativeParser(handle, columns, program) {
34
87
  const normalizedColumns = normalizeColumns(columns);
35
88
  const hasProjection = columns !== void 0;
36
- const columnCount = BigInt(columns?.length ?? 0);
89
+ const columnCount = BigInt(normalizedColumns.length);
37
90
  const normalized = program === void 0 ? normalizeNativeFilters(void 0) : normalizeNativeFilterProgram(program);
38
91
  const filterCount = BigInt(normalized.filterCount);
39
92
  const valueCount = BigInt(normalized.valueCount);
@@ -49,85 +102,74 @@ export function prepareNativeParser(handle, columns, program) {
49
102
  }
50
103
  };
51
104
  }
52
-
53
- class PreparedNativeCsvParser {
54
- #handle;
55
- #writer;
56
- #fixedValidator;
57
- constructor(options, columns, program) {
58
- const handle = createNativeCsvParser(options);
59
- try {
60
- this.#writer = prepareNativeParser(handle, columns, program);
61
- this.#fixedValidator = options.fixedColumns === void 0 ? void 0 : new NativeCsvParser(options);
62
- } catch (error) {
63
- destroyNativeCsvParser(handle);
64
- throw error;
65
- }
66
- this.#handle = handle;
67
- nativeCsvParserFinalizer.register(this, handle, this);
68
- }
69
- writeBatch(chunk, final = !1) {
70
- const handle = this.#requireHandle();
71
- if (this.#fixedValidator !== void 0)
72
- this.#fixedValidator.writeBatch(chunk, final).close();
73
- const batch = this.#writer.writeBatch(normalizeChunk(chunk), final);
74
- if (batch === null)
75
- throw Error(`native CSV parser failed: ${native.symbols.csv_parser_last_error(handle) ?? "native parser error unavailable"}`);
76
- return new NativeCsvBatch(batch);
77
- }
78
- endBatch() {
79
- return this.writeBatch(EMPTY_BUFFER, !0);
80
- }
81
- writeCount(chunk, final = !1) {
82
- this.#requireHandle();
83
- if (this.#fixedValidator !== void 0)
84
- this.#fixedValidator.writeBatch(chunk, final).close();
85
- const count = this.#writer.writeCount(normalizeChunk(chunk), final);
86
- return u64ToSafeNumber(count, "CSV filtered row count");
87
- }
88
- endCount() {
89
- return this.writeCount(EMPTY_BUFFER, !0);
90
- }
91
- close() {
92
- const handle = this.#handle;
93
- if (handle === null)
94
- return;
95
- this.#handle = null;
96
- nativeCsvParserFinalizer.unregister(this);
97
- this.#fixedValidator?.close();
98
- destroyNativeCsvParser(handle);
99
- }
100
- [Symbol.dispose]() {
101
- this.close();
102
- }
103
- #requireHandle() {
104
- if (this.#handle === null)
105
- throw Error("native CSV parser is closed");
106
- return this.#handle;
107
- }
105
+ export function validateRowSelection({ skipRows, limit }) {
106
+ if (skipRows !== void 0 && (!Number.isSafeInteger(skipRows) || skipRows < 0))
107
+ throw RangeError("skipRows must be a non-negative safe integer");
108
+ if (limit !== void 0 && (!Number.isSafeInteger(limit) || limit < 0))
109
+ throw RangeError("limit must be a non-negative safe integer");
108
110
  }
109
- function createNativeCsvParser(options) {
111
+ export function setRowSelection(handle, options, captureHeader = !1) {
112
+ if (options.skipRows !== void 0 || options.limit !== void 0)
113
+ native.symbols.csv_parser_set_row_selection(handle, BigInt(options.skipRows ?? 0), options.limit === void 0 ? 0xffffffffffffffffn : BigInt(options.limit), captureHeader);
114
+ }
115
+ function createNativeCsvParser(options, prepared = !1) {
116
+ const { skipRows, limit } = options;
117
+ validateRowSelection({ skipRows, limit });
110
118
  const delimiter = options.delimiter ?? ",";
111
119
  if (delimiter === "auto")
112
120
  throw Error("automatic delimiter detection is only supported by file APIs");
113
121
  if (delimiter.length !== 1)
114
122
  throw Error("delimiter must be one character");
115
- const handle = native.symbols.csv_parser_create(encodingCode(options.encoding), delimiter.charCodeAt(0));
123
+ const encoding = encodingCode(options.encoding);
124
+ 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));
116
125
  if (handle === null)
117
126
  throw Error("failed to create native CSV parser");
127
+ setRowSelection(handle, { skipRows, limit });
118
128
  return handle;
119
129
  }
130
+ const createSchemaParser = Symbol("createSchemaParser");
120
131
 
121
132
  export class NativeCsvParser {
122
133
  #handle;
123
134
  #strict;
124
135
  #fixedColumns;
136
+ #writer;
137
+ #transformStrictBatch;
138
+ #validator;
139
+ #captureSkippedHeader = !1;
140
+ static [createSchemaParser](options, output) {
141
+ const validator = strictSchemaValidator(options);
142
+ const parser = new NativeCsvParser(options);
143
+ if (output === "rows" && !parser.#strict && options.transform?.length)
144
+ native.symbols.csv_parser_enable_transformed_quoted_spans(parser.#requireHandle());
145
+ parser.#validator = validator;
146
+ if (validator !== void 0 && (options.skipRows ?? 0) > 0) {
147
+ parser.#captureSkippedHeader = !0;
148
+ setRowSelection(parser.#requireHandle(), options, !0);
149
+ }
150
+ return parser;
151
+ }
125
152
  constructor(options = {}) {
126
153
  rejectSelectedColumns(options);
127
154
  this.#strict = options.strict === !0;
128
- normalizeColumns(options.columns);
129
155
  this.#fixedColumns = normalizeFixedColumnsCount(options.fixedColumns, "fixed column count");
130
- const handle = createNativeCsvParser(options);
156
+ const { columns, filterProgram } = options;
157
+ if (filterProgram !== void 0 && (options.equalsFilter !== void 0 || options.filters !== void 0))
158
+ throw Error("filterProgram cannot be combined with equalsFilter or filters");
159
+ const program = filterProgram === void 0 ? projectionFilterProgram(options) : filterProgram;
160
+ if (this.#strict && (columns !== void 0 || program !== void 0))
161
+ throw Error("strict CSV validation is not supported for projected batches or filters");
162
+ const transforms = snapshotTransforms(options.transform);
163
+ const prepared = columns !== void 0 || program !== void 0 || !this.#strict && Boolean(transforms?.length);
164
+ const handle = createNativeCsvParser(options, prepared);
165
+ try {
166
+ const active = transforms?.length ? prepareTransforms(handle, transforms, columns) : !1;
167
+ this.#transformStrictBatch = this.#strict && active;
168
+ this.#writer = prepared ? prepareNativeParser(handle, columns, program) : void 0;
169
+ } catch (error) {
170
+ destroyNativeCsvParser(handle);
171
+ throw error;
172
+ }
131
173
  this.#handle = handle;
132
174
  nativeCsvParserFinalizer.register(this, handle, this);
133
175
  }
@@ -143,29 +185,19 @@ export class NativeCsvParser {
143
185
  }
144
186
  }
145
187
  writeBatch(chunk, final = !1) {
146
- if (chunk.byteLength === 0 && final)
147
- return this.endBatch();
148
188
  const handle = this.#requireHandle();
149
189
  const input = normalizeChunk(chunk);
150
- 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);
190
+ 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);
151
191
  if (batch === null)
152
192
  throw Error(`native CSV parser failed: ${this.#lastError()}`);
153
- return new NativeCsvBatch(batch);
154
- }
155
- writeProjectedBatch(chunk, options = {}, final = !1) {
156
- this.#rejectStrictUnsupported("projected batches");
157
- this.#validateProjectionOptions(options);
158
- if (chunk.byteLength === 0 && final)
159
- return this.endProjectedBatch(options);
160
- const handle = this.#requireHandle();
161
- const input = normalizeChunk(chunk);
162
- const columns = normalizeColumns(options.columns);
163
- const filter = normalizeEqualsFilter(options.equalsFilter);
164
- const filters = normalizeNativeFilters(options.filters);
165
- const batch = options.filters === void 0 ? native.symbols.csv_parser_write_projected_batch(handle, input, input, final, options.columns !== void 0, columns, BigInt(options.columns?.length ?? 0), filter.enabled, filter.column, filter.value, filter.value) : native.symbols.csv_parser_write_projected_batch_where_all(handle, input, input, final, options.columns !== void 0, columns, BigInt(options.columns?.length ?? 0), filters.descriptors, BigInt(filters.filterCount), filters.valuesData, filters.valuesData, filters.valueOffsets, BigInt(filters.valueCount));
166
- if (batch === null)
167
- throw Error(`native CSV parser failed: ${this.#lastError()}`);
168
- return new NativeCsvBatch(batch);
193
+ if (this.#transformStrictBatch) {
194
+ using _original = this.#validatedBatch(batch, final);
195
+ const result = native.symbols.csv_parser_transform_validated_batch(handle, batch);
196
+ if (!result)
197
+ throw Error(`native CSV parser failed: ${this.#lastError()}`);
198
+ return new NativeCsvBatch(result);
199
+ }
200
+ return this.#validatedBatch(batch, final);
169
201
  }
170
202
  end() {
171
203
  const batch = this.endBatch();
@@ -176,144 +208,24 @@ export class NativeCsvParser {
176
208
  }
177
209
  }
178
210
  endBatch() {
179
- 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());
180
- if (batch === null)
181
- throw Error(`native CSV parser failed: ${this.#lastError()}`);
182
- return new NativeCsvBatch(batch);
183
- }
184
- endProjectedBatch(options = {}) {
185
- this.#rejectStrictUnsupported("projected batches");
186
- this.#validateProjectionOptions(options);
187
- const columns = normalizeColumns(options.columns);
188
- const filter = normalizeEqualsFilter(options.equalsFilter);
189
- const filters = normalizeNativeFilters(options.filters);
190
- const batch = options.filters === void 0 ? native.symbols.csv_parser_finish_projected_batch(this.#requireHandle(), options.columns !== void 0, columns, BigInt(options.columns?.length ?? 0), filter.enabled, filter.column, filter.value, filter.value) : native.symbols.csv_parser_finish_projected_batch_where_all(this.#requireHandle(), options.columns !== void 0, columns, BigInt(options.columns?.length ?? 0), filters.descriptors, BigInt(filters.filterCount), filters.valuesData, filters.valuesData, filters.valueOffsets, BigInt(filters.valueCount));
191
- if (batch === null)
192
- throw Error(`native CSV parser failed: ${this.#lastError()}`);
193
- return new NativeCsvBatch(batch);
194
- }
195
- writeProjectedBatchWhere(chunk, columns, program, final = !1) {
196
- this.#rejectStrictUnsupported("projected batches");
197
- if (chunk.byteLength === 0 && final)
198
- return this.endProjectedBatchWhere(columns, program);
199
- const handle = this.#requireHandle();
200
- const input = normalizeChunk(chunk);
201
- const normalizedColumns = normalizeColumns(columns);
202
- const normalized = normalizeNativeFilterProgram(program);
203
- const batch = native.symbols.csv_parser_write_projected_batch_where_all(handle, input, input, final, columns !== void 0, normalizedColumns, BigInt(columns?.length ?? 0), normalized.descriptors, BigInt(normalized.filterCount), normalized.valuesData, normalized.valuesData, normalized.valueOffsets, BigInt(normalized.valueCount));
204
- if (batch === null)
205
- throw Error(`native CSV parser failed: ${this.#lastError()}`);
206
- return new NativeCsvBatch(batch);
207
- }
208
- endProjectedBatchWhere(columns, program) {
209
- this.#rejectStrictUnsupported("projected batches");
210
- const normalizedColumns = normalizeColumns(columns);
211
- const normalized = normalizeNativeFilterProgram(program);
212
- const batch = native.symbols.csv_parser_finish_projected_batch_where_all(this.#requireHandle(), columns !== void 0, normalizedColumns, BigInt(columns?.length ?? 0), normalized.descriptors, BigInt(normalized.filterCount), normalized.valuesData, normalized.valuesData, normalized.valueOffsets, BigInt(normalized.valueCount));
213
- if (batch === null)
214
- throw Error(`native CSV parser failed: ${this.#lastError()}`);
215
- return new NativeCsvBatch(batch);
211
+ return this.writeBatch(EMPTY_BUFFER, !0);
216
212
  }
217
213
  writeCount(chunk, final = !1) {
218
- this.#rejectStrictUnsupported("count");
219
- if (this.#fixedColumns !== void 0) {
214
+ if (this.#strict)
215
+ throw Error("strict CSV validation is not supported for count");
216
+ if (this.#writer === void 0 && this.#fixedColumns !== void 0) {
220
217
  using batch = this.writeBatch(chunk, final);
221
218
  return batch.rowCount;
222
219
  }
223
- if (chunk.byteLength === 0 && final)
224
- return this.endCount();
225
220
  const handle = this.#requireHandle();
226
221
  const input = normalizeChunk(chunk);
227
- return u64ToSafeNumber(native.symbols.csv_parser_write_count(handle, input, input, final), "CSV parsed row count");
222
+ 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");
228
223
  }
229
224
  endCount() {
230
- this.#rejectStrictUnsupported("count");
231
- if (this.#fixedColumns !== void 0) {
232
- using batch = this.endBatch();
233
- return batch.rowCount;
234
- }
235
- return u64ToSafeNumber(native.symbols.csv_parser_finish_count(this.#requireHandle()), "CSV parsed row count");
236
- }
237
- writeCountWhereEquals(chunk, filter, final = !1) {
238
- this.#rejectStrictUnsupported("count filters");
239
- if (chunk.byteLength === 0 && final)
240
- return this.endCountWhereEquals(filter);
241
- const handle = this.#requireHandle();
242
- const input = normalizeChunk(chunk);
243
- const normalized = normalizeEqualsFilter(filter);
244
- return u64ToSafeNumber(native.symbols.csv_parser_write_count_where_equals(handle, input, input, final, normalized.column, normalized.value, normalized.value), "CSV filtered row count");
245
- }
246
- endCountWhereEquals(filter) {
247
- this.#rejectStrictUnsupported("count filters");
248
- const normalized = normalizeEqualsFilter(filter);
249
- return u64ToSafeNumber(native.symbols.csv_parser_finish_count_where_equals(this.#requireHandle(), normalized.column, normalized.value, normalized.value), "CSV filtered row count");
250
- }
251
- writeCountWhereIn(chunk, filter, final = !1) {
252
- this.#rejectStrictUnsupported("count filters");
253
- if (chunk.byteLength === 0 && final)
254
- return this.endCountWhereIn(filter);
255
- const normalized = normalizeInFilter(filter);
256
- const handle = this.#requireHandle();
257
- const input = normalizeChunk(chunk);
258
- 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");
259
- }
260
- endCountWhereIn(filter) {
261
- this.#rejectStrictUnsupported("count filters");
262
- const normalized = normalizeInFilter(filter);
263
- 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");
264
- }
265
- writeCountWhereStartsWith(chunk, filter, final = !1) {
266
- this.#rejectStrictUnsupported("count filters");
267
- if (chunk.byteLength === 0 && final)
268
- return this.endCountWhereStartsWith(filter);
269
- const normalized = normalizeStartsWithFilter(filter);
270
- const handle = this.#requireHandle();
271
- const input = normalizeChunk(chunk);
272
- 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");
273
- }
274
- endCountWhereStartsWith(filter) {
275
- this.#rejectStrictUnsupported("count filters");
276
- const normalized = normalizeStartsWithFilter(filter);
277
- return u64ToSafeNumber(native.symbols.csv_parser_finish_count_where_starts_with(this.#requireHandle(), normalized.column, normalized.value, normalized.value), "CSV filtered row count");
278
- }
279
- writeCountWhereAll(chunk, filters, final = !1) {
280
- this.#rejectStrictUnsupported("count filters");
281
- if (chunk.byteLength === 0 && final)
282
- return this.endCountWhereAll(filters);
283
- const normalized = normalizeNativeFilters(filters);
284
- const handle = this.#requireHandle();
285
- const input = normalizeChunk(chunk);
286
- 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));
287
- this.#throwIfNativeError();
288
- return u64ToSafeNumber(count, "CSV filtered row count");
289
- }
290
- endCountWhereAll(filters) {
291
- this.#rejectStrictUnsupported("count filters");
292
- const normalized = normalizeNativeFilters(filters);
293
- 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));
294
- this.#throwIfNativeError();
295
- return u64ToSafeNumber(count, "CSV filtered row count");
296
- }
297
- writeCountWhere(chunk, program, final = !1) {
298
- this.#rejectStrictUnsupported("count filters");
299
- if (chunk.byteLength === 0 && final)
300
- return this.endCountWhere(program);
301
- const normalized = normalizeNativeFilterProgram(program);
302
- const handle = this.#requireHandle();
303
- const input = normalizeChunk(chunk);
304
- 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));
305
- this.#throwIfNativeError();
306
- return u64ToSafeNumber(count, "CSV filtered row count");
307
- }
308
- endCountWhere(program) {
309
- this.#rejectStrictUnsupported("count filters");
310
- const normalized = normalizeNativeFilterProgram(program);
311
- 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));
312
- this.#throwIfNativeError();
313
- return u64ToSafeNumber(count, "CSV filtered row count");
225
+ return this.writeCount(EMPTY_BUFFER, !0);
314
226
  }
315
227
  reset() {
316
- native.symbols.csv_parser_reset(this.#requireHandle());
228
+ native.symbols.csv_parser_reset_stream(this.#requireHandle());
317
229
  }
318
230
  close() {
319
231
  const handle = this.#handle;
@@ -334,18 +246,28 @@ export class NativeCsvParser {
334
246
  #lastError() {
335
247
  return native.symbols.csv_parser_last_error(this.#requireHandle()) ?? "native parser error unavailable";
336
248
  }
337
- #throwIfNativeError() {
338
- const error = this.#lastError();
339
- if (error.length > 0)
340
- throw Error(`native CSV parser failed: ${error}`);
341
- }
342
- #rejectStrictUnsupported(operation) {
343
- if (this.#strict)
344
- throw Error(`strict CSV validation is not supported for ${operation}`);
345
- }
346
- #validateProjectionOptions(options) {
347
- rejectSelectedColumns(options);
348
- if (options.equalsFilter !== void 0 && options.filters !== void 0)
349
- throw Error("use equalsFilter or filters, not both");
249
+ #validatedBatch(pointer, final) {
250
+ const batch = new NativeCsvBatch(pointer);
251
+ if (this.#validator === void 0)
252
+ return batch;
253
+ try {
254
+ const handle = this.#requireHandle();
255
+ if (this.#captureSkippedHeader) {
256
+ const header = native.symbols.csv_parser_take_skipped_header(handle);
257
+ if (header !== null) {
258
+ using first = new NativeCsvBatch(header);
259
+ this.#validator.validateBatch(first);
260
+ }
261
+ }
262
+ this.#validator.validateBatch(batch);
263
+ if (final) {
264
+ const skipped = this.#captureSkippedHeader ? u64ToSafeNumber(native.symbols.csv_parser_skipped_rows(handle), "CSV skipped row count") : 0;
265
+ this.#validator.finish(Math.max(0, skipped - 1));
266
+ }
267
+ return batch;
268
+ } catch (error) {
269
+ batch.close();
270
+ throw error;
271
+ }
350
272
  }
351
273
  }
@@ -7,7 +7,7 @@ export declare class CsvStrictSchemaValidator {
7
7
  get enabled(): boolean;
8
8
  validateRows(rows: readonly CsvRow[]): void;
9
9
  validateBatch(batch: NativeCsvBatch): void;
10
- finish(): void;
10
+ finish(skippedDataRows?: number): void;
11
11
  }
12
12
  export declare function strictSchemaValidator(options: StrictSchemaOptions): CsvStrictSchemaValidator | undefined;
13
13
  export declare function rejectStrictSchemaUnsupported(options: StrictSchemaOptions, operation: string): void;
@@ -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) {
@@ -0,0 +1,11 @@
1
+ import { type NativePointer } from './native.js';
2
+ import type { CsvColumns, CsvTransform, CsvTransformChain, CsvTransformMethods } from './types.js';
3
+ /** Start a reusable chain. Choose physical columns after defining its steps. */
4
+ export declare function transform(): CsvTransformMethods<CsvTransformChain>;
5
+ export declare function transformMethods(column: number): CsvTransformMethods;
6
+ export declare function snapshotTransforms(transforms: readonly CsvTransform[] | undefined): readonly CsvTransform[] | undefined;
7
+ export declare function hasOnlyCleanupTransforms(tokens: readonly CsvTransform[]): boolean;
8
+ /** Strict row projection happens after full-width validation and transformation. */
9
+ export declare function selectTransforms(tokens: readonly CsvTransform[], columns: CsvColumns): readonly CsvTransform[];
10
+ /** Copy each selected chain into its parser. Native writes never reference caller-owned data. */
11
+ export declare function prepareTransforms(handle: NativePointer, tokens: readonly CsvTransform[], selected: CsvColumns | undefined): boolean;