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