@konstit/csv 0.4.1 → 0.4.3
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.
- package/README.md +136 -463
- package/dist/api.d.ts +356 -21
- package/dist/api.js +64 -93
- package/dist/batches.d.ts +291 -0
- package/dist/batches.js +72 -43
- package/dist/file-stream.d.ts +2 -2
- package/dist/file-stream.js +5 -2
- package/dist/files.d.ts +148 -2
- package/dist/files.js +30 -24
- package/dist/filter.d.ts +82 -0
- package/dist/latin1-rows.d.ts +3 -0
- package/dist/latin1-rows.js +84 -0
- package/dist/materialize-field.d.ts +1 -0
- package/dist/materialize-field.js +26 -0
- package/dist/native.d.ts +29 -0
- package/dist/native.js +44 -3
- package/dist/normalize.d.ts +18 -1
- package/dist/normalize.js +10 -0
- package/dist/options-snapshot.d.ts +3 -0
- package/dist/options-snapshot.js +50 -0
- package/dist/options.d.ts +44 -3
- package/dist/parser.d.ts +307 -1
- package/dist/parser.js +121 -8
- package/dist/strict-schema.d.ts +1 -1
- package/dist/strict-schema.js +4 -1
- package/dist/types.d.ts +435 -67
- package/dist/worker-count.d.ts +33 -1
- package/dist/worker-count.js +6 -1
- package/dist/worker-pool.d.ts +77 -1
- package/dist/worker-pool.js +11 -3
- package/dist/worker-rows.d.ts +31 -3
- package/dist/worker-rows.js +11 -2
- package/dist/workers/count.worker.js +13 -26
- package/dist/workers/rows.worker.js +23 -52
- package/package.json +23 -10
- package/prebuilds/darwin-arm64/libcsv_native.dylib +0 -0
- package/prebuilds/darwin-x64/libcsv_native.dylib +0 -0
- package/prebuilds/linux-x64/libcsv_native.so +0 -0
- package/prebuilds/manifest.json +8 -8
- package/prebuilds/provenance/darwin-arm64.json +1 -1
- package/prebuilds/provenance/darwin-x64.json +1 -1
- package/prebuilds/provenance/linux-x64.json +1 -1
package/dist/parser.d.ts
CHANGED
|
@@ -1,32 +1,338 @@
|
|
|
1
1
|
import { NativeCsvBatch } from './batches.js';
|
|
2
|
+
import { type NativePointer } from './native.js';
|
|
2
3
|
import type { CsvNativeFilterProgramEntry } from './normalize.js';
|
|
3
|
-
import type { CsvEqualsFilter, CsvInFilter, CsvNativeFilter, CsvNativeProjectionOptions, CsvParserOptions, CsvRow, CsvStartsWithFilter } from './types.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;
|
|
33
|
+
interface PreparedNativeWriter {
|
|
34
|
+
writeBatch(input: NodeJS.TypedArray | DataView, final: boolean): NativePointer | null;
|
|
35
|
+
writeCount(input: NodeJS.TypedArray | DataView, final: boolean): bigint | number;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Prepares native projection and filter buffers for an existing parser handle.
|
|
39
|
+
*
|
|
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.
|
|
43
|
+
*
|
|
44
|
+
* @param handle Open native parser handle.
|
|
45
|
+
* @param selectedColumns Optional zero-based output columns.
|
|
46
|
+
* @param program Optional postfix sequence of filter predicates and boolean
|
|
47
|
+
* operators. It is validated and normalized before preparation.
|
|
48
|
+
* @returns Prepared write functions for batches and counts.
|
|
49
|
+
* @throws {Error} If the native library rejects the configuration.
|
|
50
|
+
*/
|
|
51
|
+
export declare function prepareNativeParser(handle: NativePointer, selectedColumns: CsvColumns | undefined, program: readonly CsvNativeFilterProgramEntry[] | undefined): PreparedNativeWriter;
|
|
52
|
+
/**
|
|
53
|
+
* Incrementally parses caller-owned CSV chunks through the native parser.
|
|
54
|
+
*
|
|
55
|
+
* Use this class when the application controls buffering. A write may return a
|
|
56
|
+
* `NativeCsvBatch` containing complete rows while retaining an incomplete row
|
|
57
|
+
* for the next write. Every returned batch owns native memory and must be
|
|
58
|
+
* closed or disposed. `write()` and `end()` are convenience methods that
|
|
59
|
+
* materialize rows and close their temporary batches automatically.
|
|
60
|
+
*
|
|
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.
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* ```ts
|
|
67
|
+
* using parser = new NativeCsvParser({ delimiter: ';' });
|
|
68
|
+
* using first = parser.writeBatch(Buffer.from('1;Ada\n2;Bob'), false);
|
|
69
|
+
* console.log(first.rows()); // [['1', 'Ada']]
|
|
70
|
+
* using final = parser.endBatch();
|
|
71
|
+
* console.log(final.rows()); // [['2', 'Bob']]
|
|
72
|
+
* ```
|
|
73
|
+
*/
|
|
4
74
|
export declare class NativeCsvParser {
|
|
5
75
|
#private;
|
|
76
|
+
/**
|
|
77
|
+
* Creates an open parser.
|
|
78
|
+
*
|
|
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.
|
|
87
|
+
* @throws {Error} If the delimiter is not one character or native parser
|
|
88
|
+
* creation fails.
|
|
89
|
+
* @throws {RangeError} If selected or fixed column settings are out of range.
|
|
90
|
+
*/
|
|
6
91
|
constructor(options?: CsvParserOptions);
|
|
92
|
+
/** Whether the native parser handle has been released. */
|
|
7
93
|
get closed(): boolean;
|
|
94
|
+
/**
|
|
95
|
+
* Parses a chunk and materializes all complete rows emitted by that call.
|
|
96
|
+
*
|
|
97
|
+
* The temporary native batch is closed before this method returns. Use
|
|
98
|
+
* `writeBatch()` when byte views or low-allocation access are required.
|
|
99
|
+
*
|
|
100
|
+
* @param chunk Input bytes. The parser may retain an incomplete final row for
|
|
101
|
+
* a later call.
|
|
102
|
+
* @param final Set to `true` when this is the last input chunk; buffered row
|
|
103
|
+
* state is flushed before returning.
|
|
104
|
+
* @returns Materialized UTF-8 rows emitted by this call.
|
|
105
|
+
* @throws {Error} If the parser is closed or native parsing fails.
|
|
106
|
+
*/
|
|
8
107
|
write(chunk: NodeJS.TypedArray | DataView, final?: boolean): CsvRow[];
|
|
108
|
+
/**
|
|
109
|
+
* Parses a chunk and returns an owned batch of complete rows.
|
|
110
|
+
*
|
|
111
|
+
* Call `batch.close()` or use `using` for the returned batch. The parser may
|
|
112
|
+
* retain an incomplete row between calls. `final: true` marks the input end
|
|
113
|
+
* and flushes that row.
|
|
114
|
+
*
|
|
115
|
+
* @param chunk Input bytes.
|
|
116
|
+
* @param final Whether this is the final input chunk.
|
|
117
|
+
* @returns A native batch that the caller owns.
|
|
118
|
+
* @throws {Error} If the parser is closed or native parsing fails.
|
|
119
|
+
*/
|
|
9
120
|
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
|
+
*/
|
|
10
136
|
writeProjectedBatch(chunk: NodeJS.TypedArray | DataView, options?: CsvNativeProjectionOptions, final?: boolean): NativeCsvBatch;
|
|
137
|
+
/**
|
|
138
|
+
* Flushes the parser and materializes its final rows.
|
|
139
|
+
*
|
|
140
|
+
* The temporary final batch is closed before this method returns. Use
|
|
141
|
+
* `endBatch()` when the native batch representation is needed.
|
|
142
|
+
*
|
|
143
|
+
* @returns Materialized UTF-8 rows pending at end of input.
|
|
144
|
+
* @throws {Error} If the parser is closed or the final row is invalid.
|
|
145
|
+
*/
|
|
11
146
|
end(): CsvRow[];
|
|
147
|
+
/**
|
|
148
|
+
* Flushes buffered parser state and returns the final owned batch.
|
|
149
|
+
*
|
|
150
|
+
* @returns A native batch that the caller owns and must close.
|
|
151
|
+
* @throws {Error} If the parser is closed or the final row is invalid.
|
|
152
|
+
*/
|
|
12
153
|
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
|
+
*/
|
|
13
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
|
+
*/
|
|
14
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
|
+
*/
|
|
15
192
|
endProjectedBatchWhere(selectedColumns: readonly number[] | undefined, program: readonly CsvNativeFilterProgramEntry[]): NativeCsvBatch;
|
|
193
|
+
/**
|
|
194
|
+
* Counts complete rows emitted by a chunk without materializing fields.
|
|
195
|
+
*
|
|
196
|
+
* Sum the results from each write and call `endCount()` for the final
|
|
197
|
+
* buffered row when processing split input.
|
|
198
|
+
*
|
|
199
|
+
* @param chunk Input bytes.
|
|
200
|
+
* @param final Whether this is the final input chunk.
|
|
201
|
+
* @returns The number of rows emitted by this call.
|
|
202
|
+
* @throws {Error} In strict mode, because count mode has no strict validator,
|
|
203
|
+
* or if the parser is closed/native parsing fails.
|
|
204
|
+
*/
|
|
16
205
|
writeCount(chunk: NodeJS.TypedArray | DataView, final?: boolean): number;
|
|
206
|
+
/**
|
|
207
|
+
* Flushes buffered state and returns the final unfiltered row count.
|
|
208
|
+
*
|
|
209
|
+
* @returns The number of rows emitted while finishing the stream.
|
|
210
|
+
* @throws {Error} If strict mode is enabled, the parser is closed, or native
|
|
211
|
+
* parsing fails.
|
|
212
|
+
*/
|
|
17
213
|
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
|
+
*/
|
|
18
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
|
+
*/
|
|
19
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
|
+
*/
|
|
20
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
|
+
*/
|
|
21
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
|
+
*/
|
|
22
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
|
+
*/
|
|
23
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
|
+
*/
|
|
24
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
|
+
*/
|
|
25
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
|
+
*/
|
|
26
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
|
+
*/
|
|
27
316
|
endCountWhere(program: readonly CsvNativeFilterProgramEntry[]): number;
|
|
317
|
+
/**
|
|
318
|
+
* Discards buffered row state and parser errors while keeping this parser open.
|
|
319
|
+
*
|
|
320
|
+
* Callers must supply projection or filter settings again after a reset.
|
|
321
|
+
*
|
|
322
|
+
* @throws {Error} If the parser is already closed.
|
|
323
|
+
*/
|
|
28
324
|
reset(): void;
|
|
325
|
+
/**
|
|
326
|
+
* Releases the native parser handle.
|
|
327
|
+
*
|
|
328
|
+
* Closing is idempotent. Subsequent parsing calls throw; batches already
|
|
329
|
+
* returned by the parser retain their own lifetime and must be closed
|
|
330
|
+
* separately.
|
|
331
|
+
*/
|
|
29
332
|
close(): void;
|
|
333
|
+
/** Alias for {@link close}, suitable for explicit resource management. */
|
|
30
334
|
dispose(): void;
|
|
335
|
+
/** Implements `using` / explicit resource management. */
|
|
31
336
|
[Symbol.dispose](): void;
|
|
32
337
|
}
|
|
338
|
+
export {};
|
package/dist/parser.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { NativeCsvBatch } from "./batches.js";
|
|
2
2
|
import {
|
|
3
|
+
EMPTY_BUFFER,
|
|
3
4
|
native,
|
|
5
|
+
optionalPreparedParserNative,
|
|
4
6
|
u64ToSafeNumber
|
|
5
7
|
} from "./native.js";
|
|
6
8
|
import {
|
|
@@ -18,23 +20,126 @@ function destroyNativeCsvParser(handle) {
|
|
|
18
20
|
native.symbols.csv_parser_destroy(handle);
|
|
19
21
|
}
|
|
20
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);
|
|
32
|
+
}
|
|
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);
|
|
37
|
+
const normalized = program === void 0 ? normalizeNativeFilters(void 0) : normalizeNativeFilterProgram(program);
|
|
38
|
+
const filterCount = BigInt(normalized.filterCount);
|
|
39
|
+
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
|
+
}
|
|
54
|
+
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),
|
|
56
|
+
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}`);
|
|
61
|
+
return count;
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
}
|
|
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
|
+
}
|
|
121
|
+
}
|
|
122
|
+
function createNativeCsvParser(options) {
|
|
123
|
+
const delimiter = options.delimiter ?? ",";
|
|
124
|
+
if (delimiter === "auto")
|
|
125
|
+
throw Error("automatic delimiter detection is only supported by file APIs");
|
|
126
|
+
if (delimiter.length !== 1)
|
|
127
|
+
throw Error("delimiter must be one character");
|
|
128
|
+
const handle = native.symbols.csv_parser_create(encodingCode(options.encoding), delimiter.charCodeAt(0));
|
|
129
|
+
if (handle === null)
|
|
130
|
+
throw Error("failed to create native CSV parser");
|
|
131
|
+
return handle;
|
|
132
|
+
}
|
|
21
133
|
|
|
22
134
|
export class NativeCsvParser {
|
|
23
135
|
#handle;
|
|
24
136
|
#strict;
|
|
25
137
|
#fixedColumns;
|
|
26
138
|
constructor(options = {}) {
|
|
27
|
-
const delimiter = options.delimiter ?? ",";
|
|
28
|
-
if (delimiter === "auto")
|
|
29
|
-
throw Error("automatic delimiter detection is only supported by file APIs");
|
|
30
|
-
if (delimiter.length !== 1)
|
|
31
|
-
throw Error("delimiter must be one character");
|
|
32
139
|
this.#strict = options.strict === !0;
|
|
33
140
|
normalizeColumns(options.selectedColumns);
|
|
34
141
|
this.#fixedColumns = normalizeFixedColumnsCount(options.fixedColumns, "fixed column count");
|
|
35
|
-
const handle =
|
|
36
|
-
if (handle === null)
|
|
37
|
-
throw Error("failed to create native CSV parser");
|
|
142
|
+
const handle = createNativeCsvParser(options);
|
|
38
143
|
this.#handle = handle;
|
|
39
144
|
nativeCsvParserFinalizer.register(this, handle, this);
|
|
40
145
|
}
|
|
@@ -123,6 +228,10 @@ export class NativeCsvParser {
|
|
|
123
228
|
}
|
|
124
229
|
writeCount(chunk, final = !1) {
|
|
125
230
|
this.#rejectStrictUnsupported("count");
|
|
231
|
+
if (this.#fixedColumns !== void 0) {
|
|
232
|
+
using batch = this.writeBatch(chunk, final);
|
|
233
|
+
return batch.rowCount;
|
|
234
|
+
}
|
|
126
235
|
if (chunk.byteLength === 0 && final)
|
|
127
236
|
return this.endCount();
|
|
128
237
|
const handle = this.#requireHandle();
|
|
@@ -131,6 +240,10 @@ export class NativeCsvParser {
|
|
|
131
240
|
}
|
|
132
241
|
endCount() {
|
|
133
242
|
this.#rejectStrictUnsupported("count");
|
|
243
|
+
if (this.#fixedColumns !== void 0) {
|
|
244
|
+
using batch = this.endBatch();
|
|
245
|
+
return batch.rowCount;
|
|
246
|
+
}
|
|
134
247
|
return u64ToSafeNumber(native.symbols.csv_parser_finish_count(this.#requireHandle()), "CSV parsed row count");
|
|
135
248
|
}
|
|
136
249
|
writeCountWhereEquals(chunk, filter, final = !1) {
|
package/dist/strict-schema.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import type { NativeCsvBatch } from './batches.js';
|
|
|
2
2
|
import type { CsvParserOptions, CsvRow } from './types.js';
|
|
3
3
|
export declare class CsvStrictSchemaValidator {
|
|
4
4
|
#private;
|
|
5
|
-
constructor(
|
|
5
|
+
constructor(suppliedOptions: CsvParserOptions);
|
|
6
6
|
get enabled(): boolean;
|
|
7
7
|
validateRows(rows: readonly CsvRow[]): void;
|
|
8
8
|
validateBatch(batch: NativeCsvBatch): void;
|
package/dist/strict-schema.js
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
|
+
import { snapshotCsvOptions } from "./options-snapshot.js";
|
|
2
|
+
|
|
1
3
|
export class CsvStrictSchemaValidator {
|
|
2
4
|
#options;
|
|
3
5
|
#seenHeader = !1;
|
|
4
6
|
#dataRows = 0;
|
|
5
|
-
constructor(
|
|
7
|
+
constructor(suppliedOptions) {
|
|
8
|
+
const options = snapshotCsvOptions(suppliedOptions);
|
|
6
9
|
this.#options = {
|
|
7
10
|
expectedHeaders: options.expectedHeaders,
|
|
8
11
|
minDataRows: normalizeMinDataRows(options.minDataRows),
|