@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.d.ts CHANGED
@@ -1,36 +1,11 @@
1
1
  import { NativeCsvBatch } from './batches.js';
2
+ import type { CsvFileInput } from './file-stream.js';
2
3
  import { type NativePointer } from './native.js';
3
- import type { CsvNativeFilterProgramEntry } from './normalize.js';
4
- import type { CsvColumns, CsvEqualsFilter, CsvInFilter, CsvNativeFilter, CsvNativeProjectionOptions, CsvParserOptions, CsvRow, CsvStartsWithFilter } from './types.js';
4
+ import type { CsvColumns, CsvNativeFilterProgramEntry, CsvParserOptions, CsvRow, CsvTransform, NativeCsvParserOptions } from './types.js';
5
5
  export declare const nativeCsvParserFinalizer: FinalizationRegistry<NativePointer>;
6
- /**
7
- * The fixed-configuration parser surface used by high-level streaming APIs.
8
- *
9
- * A prepared parser captures projection and filter state once, so each chunk
10
- * is processed with the same configuration. The parser owns its native handle
11
- * and must be closed or disposed after use.
12
- */
13
- export type PreparedCsvParser = Pick<NativeCsvParser, 'writeBatch' | 'endBatch' | 'writeCount' | 'endCount' | 'close' | typeof Symbol.dispose>;
14
- /**
15
- * Creates a parser with projection and filter configuration prepared once.
16
- *
17
- * This helper is used by the high-level APIs; direct callers that need to
18
- * change projection or filters between writes should use `NativeCsvParser`
19
- * and its explicit per-write methods instead.
20
- *
21
- * @param options Base parser settings such as encoding, delimiter, strictness,
22
- * and fixed column count.
23
- * @param columns Optional zero-based columns to emit in each batch.
24
- * @param program Optional postfix sequence of filter predicates and boolean
25
- * operators. The parser validates and normalizes it before preparation.
26
- * @param projected Whether the caller is explicitly requesting a projected
27
- * path. This is an internal distinction used by the high-level API.
28
- * @returns A parser with `writeBatch`/`writeCount` operations bound to the
29
- * supplied configuration.
30
- * @throws {Error} If strict validation is requested for a projected or
31
- * filtered operation, or native parser setup fails.
32
- */
33
- export declare function createPreparedParser(options: CsvParserOptions, columns?: CsvColumns, program?: readonly CsvNativeFilterProgramEntry[], projected?: boolean): PreparedCsvParser;
6
+ export declare function parseFileChunks(input: CsvFileInput, parser: NativeCsvParser, limit: number | undefined): AsyncGenerator<NativeCsvBatch, void>;
7
+ export declare function countFileChunks(input: CsvFileInput, options: CsvParserOptions, filters?: readonly CsvNativeFilterProgramEntry[]): Promise<number>;
8
+ export declare function createPreparedParser(options: CsvParserOptions, columns?: CsvColumns, program?: readonly CsvNativeFilterProgramEntry[], transform?: readonly CsvTransform[], output?: 'batches' | 'rows'): NativeCsvParser;
34
9
  interface PreparedNativeWriter {
35
10
  writeBatch(input: NodeJS.TypedArray | DataView, final: boolean): NativePointer | null;
36
11
  writeCount(input: NodeJS.TypedArray | DataView, final: boolean): bigint | number;
@@ -50,6 +25,9 @@ interface PreparedNativeWriter {
50
25
  * @throws {Error} If the native library rejects the configuration.
51
26
  */
52
27
  export declare function prepareNativeParser(handle: NativePointer, columns: CsvColumns | undefined, program: readonly CsvNativeFilterProgramEntry[] | undefined): PreparedNativeWriter;
28
+ export declare function validateRowSelection({ skipRows, limit }: Pick<CsvParserOptions, 'skipRows' | 'limit'>): void;
29
+ export declare function setRowSelection(handle: NativePointer, options: CsvParserOptions, captureHeader?: boolean): void;
30
+ declare const createSchemaParser: unique symbol;
53
31
  /**
54
32
  * Incrementally parses caller-owned CSV chunks through the native parser.
55
33
  *
@@ -59,9 +37,8 @@ export declare function prepareNativeParser(handle: NativePointer, columns: CsvC
59
37
  * closed or disposed. `write()` and `end()` are convenience methods that
60
38
  * materialize rows and close their temporary batches automatically.
61
39
  *
62
- * Projection and filter options passed to the explicit projected/count methods
63
- * apply to the current stream position. Keep them consistent while a row is
64
- * split across chunks; complete that row or call `reset()` before changing them.
40
+ * The constructor captures the projection and filters for the lifetime of the parser.
41
+ * Filters use physical column indexes. Batches emit fields in projection order.
65
42
  *
66
43
  * @example
67
44
  * ```ts
@@ -74,22 +51,18 @@ export declare function prepareNativeParser(handle: NativePointer, columns: CsvC
74
51
  */
75
52
  export declare class NativeCsvParser {
76
53
  #private;
54
+ static [createSchemaParser](options: NativeCsvParserOptions, output: 'batches' | 'rows'): NativeCsvParser;
77
55
  /**
78
56
  * Creates an open parser.
79
57
  *
80
- * @param options Base parser settings. `encoding`, `delimiter`, `strict`, and
81
- * `fixedColumns` are captured by this parser; the delimiter defaults to `,`.
82
- * Automatic delimiter detection (`"auto"`) is available through file APIs,
83
- * not this manual parser. `columns` is validated for compatibility
84
- * but does not configure `writeBatch()`; apply projection with one of the
85
- * explicit projected methods instead. `expectedHeaders`, `requireHeader`,
86
- * and `minDataRows` are schema metadata for higher-level APIs and have no
87
- * effect on this low-level parser.
58
+ * @param options Parser settings, output columns and predicates. The delimiter defaults
59
+ * to comma. Strict mode requires unfiltered, unprojected input. File and buffer
60
+ * APIs handle schema metadata such as `expectedHeaders`.
88
61
  * @throws {Error} If the delimiter is not one character or native parser
89
62
  * creation fails.
90
63
  * @throws {RangeError} If selected or fixed column settings are out of range.
91
64
  */
92
- constructor(options?: CsvParserOptions);
65
+ constructor(options?: NativeCsvParserOptions);
93
66
  /** Whether the native parser handle has been released. */
94
67
  get closed(): boolean;
95
68
  /**
@@ -119,22 +92,6 @@ export declare class NativeCsvParser {
119
92
  * @throws {Error} If the parser is closed or native parsing fails.
120
93
  */
121
94
  writeBatch(chunk: NodeJS.TypedArray | DataView, final?: boolean): NativeCsvBatch;
122
- /**
123
- * Parses a chunk with explicit projection and optional native filters.
124
- *
125
- * Returned fields follow `columns` order. `equalsFilter` and
126
- * `filters` are mutually exclusive. Keep the same projection and filter
127
- * configuration for chunks that may belong to one logical row.
128
- *
129
- * @param chunk Input bytes.
130
- * @param options Projection and filter settings for this stream position.
131
- * @param final Whether this is the final input chunk.
132
- * @returns An owned projected batch; close or dispose it after use.
133
- * @throws {Error} If strict mode is enabled, both filter forms are supplied,
134
- * the parser is closed, or native parsing fails.
135
- * @throws {RangeError} If projection or filter values are invalid.
136
- */
137
- writeProjectedBatch(chunk: NodeJS.TypedArray | DataView, options?: CsvNativeProjectionOptions, final?: boolean): NativeCsvBatch;
138
95
  /**
139
96
  * Flushes the parser and materializes its final rows.
140
97
  *
@@ -152,45 +109,6 @@ export declare class NativeCsvParser {
152
109
  * @throws {Error} If the parser is closed or the final row is invalid.
153
110
  */
154
111
  endBatch(): NativeCsvBatch;
155
- /**
156
- * Flushes buffered state using explicit projection and filter settings.
157
- *
158
- * The options must match the configuration used for chunks contributing to
159
- * the final logical row. `equalsFilter` and `filters` cannot be combined.
160
- *
161
- * @param options Projection and filter settings.
162
- * @returns An owned projected batch; close or dispose it after use.
163
- * @throws {Error} If strict mode is enabled, filter forms conflict, the
164
- * parser is closed, or the final row is invalid.
165
- */
166
- endProjectedBatch(options?: CsvNativeProjectionOptions): NativeCsvBatch;
167
- /**
168
- * Parses a chunk with a postfix native filter program.
169
- *
170
- * This is the lower-level counterpart to `writeProjectedBatch()` for callers
171
- * that want to provide predicate and boolean-operator entries directly.
172
- *
173
- * @param chunk Input bytes.
174
- * @param columns Optional zero-based output columns.
175
- * @param program Filter predicates and boolean operators in postfix order.
176
- * The method validates and normalizes the entries before the native call.
177
- * @param final Whether this is the final input chunk.
178
- * @returns An owned projected batch; close or dispose it after use.
179
- * @throws {Error} If strict mode is enabled, the parser is closed, or native
180
- * parsing rejects the program or input.
181
- */
182
- writeProjectedBatchWhere(chunk: NodeJS.TypedArray | DataView, columns: readonly number[] | undefined, program: readonly CsvNativeFilterProgramEntry[], final?: boolean): NativeCsvBatch;
183
- /**
184
- * Flushes buffered state with a postfix native filter program.
185
- *
186
- * @param columns Optional zero-based output columns.
187
- * @param program Filter predicates and boolean operators in postfix order.
188
- * The method validates and normalizes the entries before the native call.
189
- * @returns An owned projected batch; close or dispose it after use.
190
- * @throws {Error} If strict mode is enabled, the parser is closed, or native
191
- * parsing rejects the program or final row.
192
- */
193
- endProjectedBatchWhere(columns: readonly number[] | undefined, program: readonly CsvNativeFilterProgramEntry[]): NativeCsvBatch;
194
112
  /**
195
113
  * Counts complete rows emitted by a chunk without materializing fields.
196
114
  *
@@ -205,120 +123,17 @@ export declare class NativeCsvParser {
205
123
  */
206
124
  writeCount(chunk: NodeJS.TypedArray | DataView, final?: boolean): number;
207
125
  /**
208
- * Flushes buffered state and returns the final unfiltered row count.
126
+ * Flushes buffered state and returns the number of matching final records.
209
127
  *
210
128
  * @returns The number of rows emitted while finishing the stream.
211
129
  * @throws {Error} If strict mode is enabled, the parser is closed, or native
212
130
  * parsing fails.
213
131
  */
214
132
  endCount(): number;
215
- /**
216
- * Counts rows matching an equality filter while parsing a chunk.
217
- *
218
- * @param chunk Input bytes.
219
- * @param filter Column and value to compare.
220
- * @param final Whether this is the final input chunk.
221
- * @returns Matching rows emitted by this call.
222
- * @throws {Error} If strict mode is enabled, the parser is closed, or native
223
- * parsing fails.
224
- * @throws {RangeError} If the filter column or value is invalid.
225
- */
226
- writeCountWhereEquals(chunk: NodeJS.TypedArray | DataView, filter: CsvEqualsFilter, final?: boolean): number;
227
- /**
228
- * Flushes buffered state and counts rows matching an equality filter.
229
- *
230
- * @param filter Column and value to compare.
231
- * @returns Matching rows emitted while finishing the stream.
232
- * @throws {Error} If strict mode is enabled, the parser is closed, or native
233
- * parsing fails.
234
- */
235
- endCountWhereEquals(filter: CsvEqualsFilter): number;
236
- /**
237
- * Counts rows whose field belongs to an inclusion set while parsing a chunk.
238
- *
239
- * @param chunk Input bytes.
240
- * @param filter Column and accepted values.
241
- * @param final Whether this is the final input chunk.
242
- * @returns Matching rows emitted by this call.
243
- * @throws {Error} If strict mode is enabled, the parser is closed, or native
244
- * parsing fails.
245
- */
246
- writeCountWhereIn(chunk: NodeJS.TypedArray | DataView, filter: CsvInFilter, final?: boolean): number;
247
- /**
248
- * Flushes buffered state and counts rows matching an inclusion set.
249
- *
250
- * @param filter Column and accepted values.
251
- * @returns Matching rows emitted while finishing the stream.
252
- * @throws {Error} If strict mode is enabled, the parser is closed, or native
253
- * parsing fails.
254
- */
255
- endCountWhereIn(filter: CsvInFilter): number;
256
- /**
257
- * Counts rows whose field starts with a prefix while parsing a chunk.
258
- *
259
- * @param chunk Input bytes.
260
- * @param filter Column and prefix to compare.
261
- * @param final Whether this is the final input chunk.
262
- * @returns Matching rows emitted by this call.
263
- * @throws {Error} If strict mode is enabled, the parser is closed, or native
264
- * parsing fails.
265
- */
266
- writeCountWhereStartsWith(chunk: NodeJS.TypedArray | DataView, filter: CsvStartsWithFilter, final?: boolean): number;
267
- /**
268
- * Flushes buffered state and counts rows matching a prefix.
269
- *
270
- * @param filter Column and prefix to compare.
271
- * @returns Matching rows emitted while finishing the stream.
272
- * @throws {Error} If strict mode is enabled, the parser is closed, or native
273
- * parsing fails.
274
- */
275
- endCountWhereStartsWith(filter: CsvStartsWithFilter): number;
276
- /**
277
- * Counts rows satisfying all filters while parsing a chunk.
278
- *
279
- * @param chunk Input bytes.
280
- * @param filters Native filter predicates combined with logical AND.
281
- * @param final Whether this is the final input chunk.
282
- * @returns Matching rows emitted by this call.
283
- * @throws {Error} If strict mode is enabled, the parser is closed, or native
284
- * parsing fails.
285
- */
286
- writeCountWhereAll(chunk: NodeJS.TypedArray | DataView, filters: readonly CsvNativeFilter[], final?: boolean): number;
287
- /**
288
- * Flushes buffered state and counts rows satisfying all filters.
289
- *
290
- * @param filters Native filter predicates combined with logical AND.
291
- * @returns Matching rows emitted while finishing the stream.
292
- * @throws {Error} If strict mode is enabled, the parser is closed, or native
293
- * parsing fails.
294
- */
295
- endCountWhereAll(filters: readonly CsvNativeFilter[]): number;
296
- /**
297
- * Counts rows using a postfix native filter program while parsing a chunk.
298
- *
299
- * @param chunk Input bytes.
300
- * @param program Filter predicates and boolean operators in postfix order.
301
- * The method validates and normalizes the entries before the native call.
302
- * @param final Whether this is the final input chunk.
303
- * @returns Matching rows emitted by this call.
304
- * @throws {Error} If strict mode is enabled, the parser is closed, or native
305
- * parsing rejects the program or input.
306
- */
307
- writeCountWhere(chunk: NodeJS.TypedArray | DataView, program: readonly CsvNativeFilterProgramEntry[], final?: boolean): number;
308
- /**
309
- * Flushes buffered state and counts rows using a postfix filter program.
310
- *
311
- * @param program Filter predicates and boolean operators in postfix order.
312
- * The method validates and normalizes the entries before the native call.
313
- * @returns Matching rows emitted while finishing the stream.
314
- * @throws {Error} If strict mode is enabled, the parser is closed, or native
315
- * parsing rejects the program or final row.
316
- */
317
- endCountWhere(program: readonly CsvNativeFilterProgramEntry[]): number;
318
133
  /**
319
134
  * Discards buffered row state and parser errors while keeping this parser open.
320
135
  *
321
- * Callers must supply projection or filter settings again after a reset.
136
+ * Row skipping and limiting restart with the constructor's values. Projection and filters stay active.
322
137
  *
323
138
  * @throws {Error} If the parser is already closed.
324
139
  */