@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/types.d.ts
CHANGED
|
@@ -1,181 +1,549 @@
|
|
|
1
1
|
import type { NativeCsvRowView } from './batches.js';
|
|
2
|
+
/**
|
|
3
|
+
* Text encoding used when CSV fields are decoded into JavaScript strings.
|
|
4
|
+
*
|
|
5
|
+
* `utf8` is the default. The `latin1`, `iso88591`, and `iso-8859-1` spellings
|
|
6
|
+
* select the same single-byte decoding path.
|
|
7
|
+
*/
|
|
2
8
|
export type CsvEncoding = 'utf8' | 'latin1' | 'iso88591' | 'iso-8859-1';
|
|
9
|
+
/**
|
|
10
|
+
* Selects one CSV entry from a ZIP archive.
|
|
11
|
+
*
|
|
12
|
+
* ZIP input is available to serial file operations. The `entry` name must
|
|
13
|
+
* match the archive member exactly; use `'*'` only when the archive contains
|
|
14
|
+
* exactly one file entry.
|
|
15
|
+
*/
|
|
3
16
|
export interface CsvZipCompression {
|
|
4
|
-
format
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
17
|
+
/** ZIP is the selected container format. */
|
|
18
|
+
readonly format: 'zip';
|
|
19
|
+
/** Archive member containing the CSV data, or `'*'` for an unambiguous single-file archive. */
|
|
20
|
+
readonly entry: string;
|
|
21
|
+
/**
|
|
22
|
+
* Maximum allowed decompressed-to-compressed byte ratio for the selected entry.
|
|
23
|
+
*
|
|
24
|
+
* Must be a positive integer no greater than `4_294_967_295`. The default is
|
|
25
|
+
* `1000`; lower values tighten protection against highly compressed entries.
|
|
26
|
+
*/
|
|
27
|
+
readonly maxCompressionRatio?: number;
|
|
28
|
+
/**
|
|
29
|
+
* Maximum decompressed bytes accepted from the entry.
|
|
30
|
+
*
|
|
31
|
+
* Must be a positive safe integer. The default is 64 GiB.
|
|
32
|
+
*/
|
|
33
|
+
readonly maxDecompressedBytes?: number;
|
|
8
34
|
}
|
|
35
|
+
/**
|
|
36
|
+
* Compression accepted for file input.
|
|
37
|
+
*
|
|
38
|
+
* `'auto'` detects the supported compression format from the input prefix and
|
|
39
|
+
* filename. A ZIP object selects one archive entry. Compression is supported
|
|
40
|
+
* for serial file operations; worker, parallel, and byte-offset sharding APIs
|
|
41
|
+
* require uncompressed input.
|
|
42
|
+
*/
|
|
9
43
|
export type CsvCompression = Bun.CompressionFormat | 'auto' | CsvZipCompression;
|
|
44
|
+
/**
|
|
45
|
+
* Field delimiter used by the CSV parser.
|
|
46
|
+
*
|
|
47
|
+
* Omit the option to use comma. `'auto'` is supported by serial file
|
|
48
|
+
* operations and probes the input for a stable common delimiter. Other strings
|
|
49
|
+
* are accepted by TypeScript, but runtime parsing requires one character.
|
|
50
|
+
*/
|
|
10
51
|
export type CsvDelimiter = 'auto' | ',' | ';' | '\t' | '|' | ':' | '^' | '~' | (string & {});
|
|
52
|
+
/** A materialized CSV row, with one decoded string per field. */
|
|
11
53
|
export type CsvRow = string[];
|
|
54
|
+
/**
|
|
55
|
+
* Zero-based physical CSV column indexes.
|
|
56
|
+
*
|
|
57
|
+
* Projections may contain at most 2024 unique indexes. The array is readonly
|
|
58
|
+
* in the public type because each operation captures its configuration at
|
|
59
|
+
* invocation time, before asynchronous iteration begins.
|
|
60
|
+
*/
|
|
12
61
|
export type CsvColumns = readonly number[];
|
|
62
|
+
/** A field operand accepted by native filters: text or raw field bytes. */
|
|
13
63
|
export type CsvFieldValue = string | Buffer | Uint8Array;
|
|
14
64
|
declare const csvRegexBrand: unique symbol;
|
|
15
65
|
declare const csvFilterBrand: unique symbol;
|
|
66
|
+
/**
|
|
67
|
+
* Validated regular-expression metadata created by `csv.re()`.
|
|
68
|
+
*
|
|
69
|
+
* This branded type intentionally cannot be constructed by an object literal;
|
|
70
|
+
* use the factory so the pattern and flags are checked before parsing starts.
|
|
71
|
+
*/
|
|
16
72
|
export interface CsvRegex {
|
|
73
|
+
/** Pattern source after JavaScript regular-expression parsing. */
|
|
17
74
|
readonly source: string;
|
|
75
|
+
/** Supported flags (`i`, `m`, `s`, and `u`). */
|
|
18
76
|
readonly flags: string;
|
|
19
77
|
readonly [csvRegexBrand]: true;
|
|
20
78
|
}
|
|
79
|
+
/**
|
|
80
|
+
* Opaque immutable predicate created by the CSV filter factories.
|
|
81
|
+
*
|
|
82
|
+
* Pass filters through the `where` option or combine them with `csv.all()`,
|
|
83
|
+
* `csv.any()`, and `csv.not()`. Object-literal predicates are not accepted.
|
|
84
|
+
*/
|
|
21
85
|
export interface CsvFilter {
|
|
22
86
|
readonly [csvFilterBrand]: true;
|
|
23
87
|
}
|
|
88
|
+
/**
|
|
89
|
+
* Reusable condition builder for one zero-based physical CSV column.
|
|
90
|
+
*
|
|
91
|
+
* Each method returns a complete immutable `CsvFilter`; the builder itself can
|
|
92
|
+
* be retained and used to create several predicates for the same column.
|
|
93
|
+
*/
|
|
24
94
|
export interface CsvFilterColumn {
|
|
95
|
+
/**
|
|
96
|
+
* Match fields equal to `value`. A missing field does not match.
|
|
97
|
+
*
|
|
98
|
+
* Binary operands are copied when the filter is created.
|
|
99
|
+
*
|
|
100
|
+
* @param value - Text or bytes that must equal the field.
|
|
101
|
+
* @returns An immutable equality filter for this column.
|
|
102
|
+
* @throws {TypeError} If `value` is not a string, `Buffer`, or `Uint8Array` at runtime.
|
|
103
|
+
*/
|
|
25
104
|
readonly equals: (value: CsvFieldValue) => CsvFilter;
|
|
105
|
+
/**
|
|
106
|
+
* Match fields different from `value`. A missing field does not match.
|
|
107
|
+
*
|
|
108
|
+
* Binary operands are copied when the filter is created.
|
|
109
|
+
*
|
|
110
|
+
* @param value - Text or bytes that must differ from the field.
|
|
111
|
+
* @returns An immutable inequality filter for this column.
|
|
112
|
+
* @throws {TypeError} If `value` is not a string, `Buffer`, or `Uint8Array` at runtime.
|
|
113
|
+
*/
|
|
26
114
|
readonly doesNotEqual: (value: CsvFieldValue) => CsvFilter;
|
|
115
|
+
/**
|
|
116
|
+
* Match fields whose value is one of `values`.
|
|
117
|
+
*
|
|
118
|
+
* The array and all binary operands are copied when the filter is created.
|
|
119
|
+
*
|
|
120
|
+
* @param values - Non-empty set of text or byte values to match.
|
|
121
|
+
* @returns An immutable membership filter for this column.
|
|
122
|
+
* @throws {TypeError} If `values` or one of its members has an invalid runtime type.
|
|
123
|
+
* @throws {RangeError} If `values` is empty.
|
|
124
|
+
*/
|
|
27
125
|
readonly isOneOf: (values: readonly CsvFieldValue[]) => CsvFilter;
|
|
126
|
+
/**
|
|
127
|
+
* Match fields whose value is none of `values`. A missing field does not match.
|
|
128
|
+
*
|
|
129
|
+
* The array and all binary operands are copied when the filter is created.
|
|
130
|
+
*
|
|
131
|
+
* @param values - Non-empty set of text or byte values to exclude.
|
|
132
|
+
* @returns An immutable negative-membership filter for this column.
|
|
133
|
+
* @throws {TypeError} If `values` or one of its members has an invalid runtime type.
|
|
134
|
+
* @throws {RangeError} If `values` is empty.
|
|
135
|
+
*/
|
|
28
136
|
readonly isNoneOf: (values: readonly CsvFieldValue[]) => CsvFilter;
|
|
137
|
+
/**
|
|
138
|
+
* Match fields beginning with `prefix`.
|
|
139
|
+
*
|
|
140
|
+
* Binary operands are copied when the filter is created.
|
|
141
|
+
*
|
|
142
|
+
* @param prefix - Text or bytes that must occur at the start of the field.
|
|
143
|
+
* @returns An immutable prefix filter for this column.
|
|
144
|
+
* @throws {TypeError} If `prefix` is not a string, `Buffer`, or `Uint8Array` at runtime.
|
|
145
|
+
*/
|
|
29
146
|
readonly startsWith: (prefix: CsvFieldValue) => CsvFilter;
|
|
147
|
+
/**
|
|
148
|
+
* Match fields searched by the validated RE2-compatible `pattern`.
|
|
149
|
+
*
|
|
150
|
+
* The pattern is copied and validated immediately. Supported flags are `i`,
|
|
151
|
+
* `m`, `s`, and `u`; global, indices, sticky, and `v` flags are rejected.
|
|
152
|
+
* RE2 lookaround and backreferences are unsupported.
|
|
153
|
+
*
|
|
154
|
+
* @param pattern - JavaScript regular expression to validate and use for matching.
|
|
155
|
+
* @returns An immutable regular-expression filter for this column.
|
|
156
|
+
* @throws {TypeError} If `pattern` is not a `RegExp` at runtime.
|
|
157
|
+
* @throws {Error} If the flags are unsupported.
|
|
158
|
+
* @throws {SyntaxError} If the pattern is invalid for the native RE2 engine.
|
|
159
|
+
* @throws {RangeError} If the UTF-8 pattern exceeds 4096 bytes.
|
|
160
|
+
*/
|
|
30
161
|
readonly hasMatch: (pattern: RegExp) => CsvFilter;
|
|
31
162
|
}
|
|
163
|
+
/**
|
|
164
|
+
* Half-open byte range into a batch's backing data (`[start, end)`).
|
|
165
|
+
*
|
|
166
|
+
* The offsets are safe JavaScript numbers validated against the backing data;
|
|
167
|
+
* use them with `dataView().subarray(start, end)` when reading a batch view.
|
|
168
|
+
*/
|
|
32
169
|
export interface CsvFieldRange {
|
|
170
|
+
/** Inclusive start byte offset. */
|
|
33
171
|
start: number;
|
|
172
|
+
/** Exclusive end byte offset. */
|
|
34
173
|
end: number;
|
|
35
174
|
}
|
|
175
|
+
/**
|
|
176
|
+
* Inclusive byte range assigned to one worker shard of an uncompressed CSV file.
|
|
177
|
+
*
|
|
178
|
+
* The range boundaries are chosen at record boundaries, so a worker can parse
|
|
179
|
+
* the shard independently.
|
|
180
|
+
*/
|
|
36
181
|
export interface CsvShard {
|
|
182
|
+
/** Inclusive start byte offset in the file. */
|
|
37
183
|
start: number;
|
|
184
|
+
/** Inclusive end byte offset in the file. */
|
|
38
185
|
end: number;
|
|
39
186
|
}
|
|
187
|
+
/**
|
|
188
|
+
* Parser configuration shared by in-memory parsing and file APIs.
|
|
189
|
+
*
|
|
190
|
+
* Options are readonly and are captured at operation invocation time, before
|
|
191
|
+
* asynchronous iteration begins. Start a new operation to apply changed arrays
|
|
192
|
+
* or values.
|
|
193
|
+
*/
|
|
40
194
|
export interface CsvParserOptions {
|
|
41
|
-
encoding
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
195
|
+
/** Input field encoding used when decoding bytes into JavaScript strings. Defaults to `'utf8'`. */
|
|
196
|
+
readonly encoding?: CsvEncoding;
|
|
197
|
+
/** Field separator. Defaults to comma; `'auto'` is for serial file input. */
|
|
198
|
+
readonly delimiter?: CsvDelimiter;
|
|
199
|
+
/** Physical columns to emit from each row, in the requested output order. */
|
|
200
|
+
readonly selectedColumns?: CsvColumns;
|
|
201
|
+
/** Require exactly this many fields per row; malformed rows fail instead of being padded or truncated. */
|
|
202
|
+
readonly fixedColumns?: number;
|
|
203
|
+
/** Enable RFC-style quote and schema validation. Defaults to `false`. */
|
|
204
|
+
readonly strict?: boolean;
|
|
205
|
+
/** Expected first-row field values, checked in strict mode. */
|
|
206
|
+
readonly expectedHeaders?: readonly string[];
|
|
207
|
+
/** Require a first row when strict schema validation is enabled. */
|
|
208
|
+
readonly requireHeader?: boolean;
|
|
209
|
+
/** Minimum number of data rows after the header, checked in strict mode. */
|
|
210
|
+
readonly minDataRows?: number;
|
|
49
211
|
}
|
|
212
|
+
/**
|
|
213
|
+
* File input options, including parser settings and stream controls.
|
|
214
|
+
*
|
|
215
|
+
* Compression is streamed into the parser and does not load the complete file
|
|
216
|
+
* into memory. Compressed input cannot be used with parallel or sharded APIs.
|
|
217
|
+
*/
|
|
50
218
|
export interface CsvFileOptions extends CsvParserOptions {
|
|
51
|
-
|
|
52
|
-
|
|
219
|
+
/** Read-stream chunk size in bytes. Defaults to 1 MiB. */
|
|
220
|
+
readonly chunkSize?: number;
|
|
221
|
+
/** Optional compression format or ZIP entry selection. */
|
|
222
|
+
readonly compression?: CsvCompression;
|
|
53
223
|
}
|
|
224
|
+
/**
|
|
225
|
+
* File options accepted by byte-offset sharding helpers.
|
|
226
|
+
*
|
|
227
|
+
* Compression is excluded because compressed byte streams cannot be split into
|
|
228
|
+
* independently readable file ranges.
|
|
229
|
+
*/
|
|
54
230
|
export type CsvShardingOptions = Omit<CsvFileOptions, 'compression'> & {
|
|
55
|
-
|
|
231
|
+
/** Compression is unsupported for byte-offset sharding. */
|
|
232
|
+
readonly compression?: never;
|
|
56
233
|
};
|
|
234
|
+
/** Native equality predicate used by projection and count fast paths. */
|
|
57
235
|
export interface CsvEqualsFilter {
|
|
58
|
-
column
|
|
59
|
-
|
|
236
|
+
/** Zero-based physical column to inspect. */
|
|
237
|
+
readonly column: number;
|
|
238
|
+
/** Field bytes or text that must equal the input field. */
|
|
239
|
+
readonly value: CsvFieldValue;
|
|
60
240
|
}
|
|
241
|
+
/** Native membership predicate used by projection and count fast paths. */
|
|
61
242
|
export interface CsvInFilter {
|
|
62
|
-
column
|
|
63
|
-
|
|
243
|
+
/** Zero-based physical column to inspect. */
|
|
244
|
+
readonly column: number;
|
|
245
|
+
/** Values for which the input field matches. */
|
|
246
|
+
readonly values: readonly CsvFieldValue[];
|
|
64
247
|
}
|
|
248
|
+
/** Native inequality predicate used by projection and count fast paths. */
|
|
65
249
|
export interface CsvNotEqualsFilter {
|
|
66
|
-
column
|
|
67
|
-
|
|
250
|
+
/** Zero-based physical column to inspect. */
|
|
251
|
+
readonly column: number;
|
|
252
|
+
/** Field bytes or text that must differ from the input field. */
|
|
253
|
+
readonly notEquals: CsvFieldValue;
|
|
68
254
|
}
|
|
255
|
+
/** Native negative-membership predicate used by projection and count fast paths. */
|
|
69
256
|
export interface CsvNotInFilter {
|
|
70
|
-
column
|
|
71
|
-
|
|
257
|
+
/** Zero-based physical column to inspect. */
|
|
258
|
+
readonly column: number;
|
|
259
|
+
/** Values that the input field must not match. */
|
|
260
|
+
readonly notIn: readonly CsvFieldValue[];
|
|
72
261
|
}
|
|
262
|
+
/** Native prefix predicate used by projection and count fast paths. */
|
|
73
263
|
export interface CsvStartsWithFilter {
|
|
74
|
-
column
|
|
75
|
-
|
|
264
|
+
/** Zero-based physical column to inspect. */
|
|
265
|
+
readonly column: number;
|
|
266
|
+
/** Field bytes or text that must occur at the start of the input field. */
|
|
267
|
+
readonly prefix: CsvFieldValue;
|
|
76
268
|
}
|
|
269
|
+
/** Native regular-expression predicate used by projection and count fast paths. */
|
|
77
270
|
export interface CsvRegexFilter {
|
|
78
|
-
column
|
|
79
|
-
|
|
271
|
+
/** Zero-based physical column to inspect. */
|
|
272
|
+
readonly column: number;
|
|
273
|
+
/** Validated pattern created by `csv.re()`. */
|
|
274
|
+
readonly regex: CsvRegex;
|
|
80
275
|
}
|
|
276
|
+
/**
|
|
277
|
+
* Native predicate shape accepted by low-level projection options.
|
|
278
|
+
*
|
|
279
|
+
* High-level row and count APIs normally use the opaque `CsvFilter` returned by
|
|
280
|
+
* the filter factories instead. High-level options copy operands at invocation
|
|
281
|
+
* time; low-level parser calls normalize their operands for each write.
|
|
282
|
+
*/
|
|
81
283
|
export type CsvNativeFilter = CsvEqualsFilter | CsvInFilter | CsvNotInFilter | CsvNotEqualsFilter | CsvRegexFilter | CsvStartsWithFilter;
|
|
284
|
+
/**
|
|
285
|
+
* Native projection and predicate configuration used by low-level parser APIs.
|
|
286
|
+
*
|
|
287
|
+
* `equalsFilter` and `filters` are mutually exclusive. When `filters` contains
|
|
288
|
+
* several entries, the native path combines them with an AND operation.
|
|
289
|
+
*/
|
|
82
290
|
export interface CsvNativeProjectionOptions {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
291
|
+
/** Physical columns to emit, in output order. */
|
|
292
|
+
readonly selectedColumns?: CsvColumns;
|
|
293
|
+
/** One equality predicate for the direct native fast path. */
|
|
294
|
+
readonly equalsFilter?: CsvEqualsFilter;
|
|
295
|
+
/** Native predicates combined for the operation. */
|
|
296
|
+
readonly filters?: readonly CsvNativeFilter[];
|
|
86
297
|
}
|
|
298
|
+
/**
|
|
299
|
+
* Common file options for the high-level `csv` API.
|
|
300
|
+
*
|
|
301
|
+
* Use `columns` for the preferred projection spelling. `selectedColumns` is a
|
|
302
|
+
* legacy alias; supplying both is rejected.
|
|
303
|
+
*/
|
|
87
304
|
export interface CsvApiFileOptions extends CsvFileOptions {
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
305
|
+
/** Preferred physical-column projection for materialized row APIs. */
|
|
306
|
+
readonly columns?: CsvColumns;
|
|
307
|
+
/** Number of workers for parallel operations; values greater than one enable sharding. */
|
|
308
|
+
readonly workerCount?: number;
|
|
309
|
+
/** Opaque filter created by `csv.column()`, `csv.all()`, `csv.any()`, or `csv.not()`. */
|
|
310
|
+
readonly where?: CsvFilter;
|
|
91
311
|
}
|
|
312
|
+
/**
|
|
313
|
+
* Type-safe mutually exclusive projection fields.
|
|
314
|
+
*
|
|
315
|
+
* The generic preserves a literal tuple such as `readonly [0, 2]`, allowing
|
|
316
|
+
* materialized rows and row views to retain their projected positions in the
|
|
317
|
+
* resulting TypeScript type.
|
|
318
|
+
*/
|
|
92
319
|
export type CsvColumnSelection<TColumns extends CsvColumns | undefined> = {
|
|
93
|
-
|
|
94
|
-
|
|
320
|
+
/** Preferred projection spelling; indexes are emitted in this order. */
|
|
321
|
+
readonly columns?: TColumns;
|
|
322
|
+
/** Deliberately excluded when `columns` is supplied. */
|
|
323
|
+
readonly selectedColumns?: never;
|
|
95
324
|
} | {
|
|
96
|
-
|
|
97
|
-
|
|
325
|
+
/** Deliberately excluded when the legacy alias is supplied. */
|
|
326
|
+
readonly columns?: never;
|
|
327
|
+
/** Legacy projection spelling; indexes are emitted in this order. */
|
|
328
|
+
readonly selectedColumns?: TColumns;
|
|
98
329
|
};
|
|
330
|
+
/** Internal common API options after removing the mutually exclusive projection fields. */
|
|
99
331
|
type CsvApiOptionsWithoutSelection = Omit<CsvApiFileOptions, 'columns' | 'selectedColumns'>;
|
|
332
|
+
/** Apply `Omit` to each member of a union independently. */
|
|
100
333
|
type CsvDistributiveOmit<T, TKeys extends PropertyKey> = T extends unknown ? Omit<T, TKeys> : never;
|
|
334
|
+
/** Internal serial option base shared by row and batch APIs. */
|
|
101
335
|
type CsvOptionsWithoutWhereWorkerCount<TColumns extends CsvColumns | undefined> = Omit<CsvApiOptionsWithoutSelection, 'where' | 'workerCount'> & CsvColumnSelection<TColumns>;
|
|
336
|
+
/**
|
|
337
|
+
* Options for one-process row parsing, row views, and batch streaming.
|
|
338
|
+
*
|
|
339
|
+
* `strict: true` selects validated parsing and cannot be combined with `where`.
|
|
340
|
+
* `workerCount` is restricted to `1` for this serial shape. The generic keeps a
|
|
341
|
+
* literal projection tuple when supplied.
|
|
342
|
+
*/
|
|
102
343
|
export type CsvSingleThreadRowsOptions<TColumns extends CsvColumns | undefined = undefined> = (CsvDistributiveOmit<CsvOptionsWithoutWhereWorkerCount<TColumns>, 'strict'> & {
|
|
103
|
-
strict
|
|
104
|
-
|
|
105
|
-
|
|
344
|
+
/** Disable strict quote/schema validation (the default serial mode). */
|
|
345
|
+
readonly strict?: false | undefined;
|
|
346
|
+
/** Optional immutable filter created by the CSV filter factories. */
|
|
347
|
+
readonly where?: CsvFilter | undefined;
|
|
348
|
+
/** Serial mode uses one worker; omit this field to use the default. */
|
|
349
|
+
readonly workerCount?: 1 | undefined;
|
|
106
350
|
}) | (CsvDistributiveOmit<CsvOptionsWithoutWhereWorkerCount<TColumns>, 'strict'> & {
|
|
107
|
-
strict
|
|
108
|
-
|
|
109
|
-
|
|
351
|
+
/** Enable strict quote/schema validation; strict mode cannot use `where`. */
|
|
352
|
+
readonly strict: true;
|
|
353
|
+
/** Filters are excluded when strict validation is enabled. */
|
|
354
|
+
readonly where?: undefined;
|
|
355
|
+
/** Strict parsing remains single-threaded. */
|
|
356
|
+
readonly workerCount?: 1 | undefined;
|
|
110
357
|
});
|
|
358
|
+
/**
|
|
359
|
+
* Options for sharded row parsing with multiple workers.
|
|
360
|
+
*
|
|
361
|
+
* Set `workerCount` to the desired number of workers. Parallel operations do
|
|
362
|
+
* not support compression, automatic delimiter detection, or strict parsing.
|
|
363
|
+
*/
|
|
111
364
|
export type CsvParallelRowsOptions<TColumns extends CsvColumns | undefined = undefined> = Omit<CsvApiOptionsWithoutSelection, 'compression' | 'where' | 'workerCount' | 'strict'> & CsvColumnSelection<TColumns> & {
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
365
|
+
/** Compressed input cannot be byte-offset sharded. */
|
|
366
|
+
readonly compression?: never;
|
|
367
|
+
/** Optional immutable filter applied in each worker. */
|
|
368
|
+
readonly where?: CsvFilter | undefined;
|
|
369
|
+
/** Required worker count; must be an integer greater than one. */
|
|
370
|
+
readonly workerCount: number;
|
|
371
|
+
/** Strict validation is unsupported for parallel parsing. */
|
|
372
|
+
readonly strict?: false | undefined;
|
|
116
373
|
};
|
|
374
|
+
/** Options for parsing an in-memory buffer into materialized rows. */
|
|
117
375
|
export type CsvParseOptions<TColumns extends CsvColumns | undefined = undefined> = CsvDistributiveOmit<CsvSingleThreadRowsOptions<TColumns>, 'compression'>;
|
|
376
|
+
/** Options accepted by `csv.rows()`, serial or parallel. */
|
|
118
377
|
export type CsvRowsOptions<TColumns extends CsvColumns | undefined = undefined> = CsvSingleThreadRowsOptions<TColumns> | CsvParallelRowsOptions<TColumns>;
|
|
378
|
+
/** Options accepted by `csv.batches()` and `csv.withBatches()`. */
|
|
119
379
|
export type CsvBatchesOptions = CsvSingleThreadRowsOptions<CsvColumns | undefined>;
|
|
380
|
+
/** Options accepted by reusable row-view callbacks. */
|
|
120
381
|
export type CsvRowViewsOptions<TColumns extends CsvColumns | undefined = undefined> = CsvSingleThreadRowsOptions<TColumns>;
|
|
382
|
+
/**
|
|
383
|
+
* Options accepted by columnar-batch callbacks.
|
|
384
|
+
*
|
|
385
|
+
* Columnar batches expose raw projected bytes and therefore always use
|
|
386
|
+
* non-strict parsing when a projection tuple is supplied.
|
|
387
|
+
*/
|
|
121
388
|
export type CsvColumnarBatchOptions<TColumns extends CsvColumns | undefined = undefined> = [TColumns] extends [undefined] ? CsvSingleThreadRowsOptions<TColumns> : CsvDistributiveOmit<CsvSingleThreadRowsOptions<TColumns>, 'strict'> & {
|
|
122
|
-
|
|
389
|
+
/** Strict validation is unavailable for projected columnar batches. */
|
|
390
|
+
readonly strict?: false | undefined;
|
|
123
391
|
};
|
|
392
|
+
/** Internal serial count option union used by the exported `CsvCountOptions`. */
|
|
124
393
|
type CsvCountSingleThreadOptions = (Omit<CsvApiOptionsWithoutSelection, 'where' | 'workerCount' | 'strict'> & CsvColumnSelection<CsvColumns | undefined> & {
|
|
125
|
-
strict
|
|
126
|
-
|
|
127
|
-
|
|
394
|
+
/** Disable strict validation (the default serial count mode). */
|
|
395
|
+
readonly strict?: false | undefined;
|
|
396
|
+
/** Optional immutable filter evaluated by the native count path. */
|
|
397
|
+
readonly where?: CsvFilter | undefined;
|
|
398
|
+
/** Serial mode uses one worker; omit this field to use the default. */
|
|
399
|
+
readonly workerCount?: 1 | undefined;
|
|
128
400
|
}) | (Omit<CsvApiOptionsWithoutSelection, 'where' | 'workerCount' | 'strict'> & CsvColumnSelection<CsvColumns | undefined> & {
|
|
129
|
-
strict
|
|
130
|
-
|
|
131
|
-
|
|
401
|
+
/** Enable strict quote/schema validation; filtered strict counts are unsupported. */
|
|
402
|
+
readonly strict: true;
|
|
403
|
+
/** Filters are excluded when strict validation is enabled. */
|
|
404
|
+
readonly where?: undefined;
|
|
405
|
+
/** Strict counting remains single-threaded. */
|
|
406
|
+
readonly workerCount?: 1 | undefined;
|
|
132
407
|
});
|
|
408
|
+
/** Options for counting rows with multiple workers. */
|
|
133
409
|
export type CsvParallelCountOptions = Omit<CsvApiOptionsWithoutSelection, 'compression' | 'where' | 'workerCount' | 'strict'> & CsvColumnSelection<CsvColumns | undefined> & {
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
410
|
+
/** Compressed input cannot be byte-offset sharded. */
|
|
411
|
+
readonly compression?: never;
|
|
412
|
+
/** Optional immutable filter evaluated in each worker. */
|
|
413
|
+
readonly where?: CsvFilter | undefined;
|
|
414
|
+
/** Required worker count; must be an integer greater than one. */
|
|
415
|
+
readonly workerCount: number;
|
|
416
|
+
/** Strict validation is unsupported for parallel counting. */
|
|
417
|
+
readonly strict?: false | undefined;
|
|
138
418
|
};
|
|
419
|
+
/** Options accepted by `csv.count()`, serial or parallel. */
|
|
139
420
|
export type CsvCountOptions = CsvCountSingleThreadOptions | CsvParallelCountOptions;
|
|
421
|
+
/** Options captured when a reusable `CsvWorkerPool` is created. */
|
|
140
422
|
export type CsvWorkerPoolOptions<TColumns extends CsvColumns | undefined = undefined> = CsvParallelRowsOptions<TColumns>;
|
|
423
|
+
/**
|
|
424
|
+
* Materialized row type inferred from a literal projection tuple.
|
|
425
|
+
*
|
|
426
|
+
* For example, `CsvProjectedRow<readonly [0, 2]>` is a two-element mutable
|
|
427
|
+
* string tuple. Without a projection tuple the result is the variable-length
|
|
428
|
+
* `CsvRow` shape.
|
|
429
|
+
*/
|
|
141
430
|
export type CsvProjectedRow<TColumns extends CsvColumns | undefined> = TColumns extends readonly unknown[] ? {
|
|
142
431
|
-readonly [Index in keyof TColumns]: string;
|
|
143
432
|
} : CsvRow;
|
|
433
|
+
/**
|
|
434
|
+
* Reusable, low-allocation view of one materialized row.
|
|
435
|
+
*
|
|
436
|
+
* The view is valid only during the synchronous callback that receives it. Its
|
|
437
|
+
* accessors address the fields stored in the current batch: unfiltered rows
|
|
438
|
+
* use physical CSV indexes, while a filtered operation with a projection uses
|
|
439
|
+
* projected output positions. `selectedColumns` maps those output positions
|
|
440
|
+
* back to physical indexes. Missing fields return `null` from scalar accessors
|
|
441
|
+
* and become empty strings in `pick()` and `pickPhysical()`.
|
|
442
|
+
*
|
|
443
|
+
* @typeParam TSelectedColumns - Literal selected-column indexes, when supplied.
|
|
444
|
+
*/
|
|
144
445
|
export type CsvRowView<TSelectedColumns extends CsvColumns | undefined = undefined> = Pick<NativeCsvRowView, 'rowIndex' | 'fieldCount' | 'fieldRange' | 'range' | 'fieldBytes' | 'bytes' | 'fieldBuffer' | 'buffer' | 'fieldString' | 'getPhysical' | 'get' | 'pickPhysical' | 'pick'> & {
|
|
145
|
-
/**
|
|
146
|
-
* Selected output columns requested for the stream, when present.
|
|
147
|
-
*
|
|
148
|
-
* `CsvRowView` still reads physical CSV column indexes through `get()`, `fieldString()`,
|
|
149
|
-
* `bytes()`, `range()`, and `pick()`. This metadata is useful when you need to map projected
|
|
150
|
-
* output positions back to source column indexes.
|
|
151
|
-
*/
|
|
446
|
+
/** Selected output columns requested for the stream, when present. */
|
|
152
447
|
readonly selectedColumns: TSelectedColumns;
|
|
153
448
|
};
|
|
449
|
+
/**
|
|
450
|
+
* Synchronous callback invoked for each reusable row view.
|
|
451
|
+
*
|
|
452
|
+
* Copy values that must outlive the callback; the view is rebound for the next
|
|
453
|
+
* row and released when the callback returns.
|
|
454
|
+
*
|
|
455
|
+
* @typeParam TSelectedColumns - Literal selected-column indexes, when supplied.
|
|
456
|
+
* @param row - Reusable view positioned at `rowIndex`.
|
|
457
|
+
* @param rowIndex - Zero-based row index within the current batch.
|
|
458
|
+
*/
|
|
154
459
|
export type CsvRowViewCallback<TSelectedColumns extends CsvColumns | undefined = undefined> = (row: CsvRowView<TSelectedColumns>, rowIndex: number) => void;
|
|
460
|
+
/**
|
|
461
|
+
* Synchronous callback invoked by `NativeCsvBatch.forEachRow()`.
|
|
462
|
+
*
|
|
463
|
+
* The same native row-view object is moved to each row in sequence; copy any
|
|
464
|
+
* values needed after the callback returns.
|
|
465
|
+
*
|
|
466
|
+
* @param row - Reusable view positioned at `rowIndex`.
|
|
467
|
+
* @param rowIndex - Zero-based row index within the batch.
|
|
468
|
+
*/
|
|
155
469
|
export type NativeCsvRowCallback = (row: NativeCsvRowView, rowIndex: number) => void;
|
|
470
|
+
/**
|
|
471
|
+
* Receives the byte range for one field in a columnar batch.
|
|
472
|
+
*
|
|
473
|
+
* `start` is inclusive and `end` is exclusive; both offsets address the batch's
|
|
474
|
+
* `data()` buffer. Rows that do not contain the requested column are skipped.
|
|
475
|
+
*/
|
|
156
476
|
export type CsvColumnRangeCallback = (rowIndex: number, start: number, end: number) => void;
|
|
477
|
+
/**
|
|
478
|
+
* Receives raw bytes for one field in a columnar batch.
|
|
479
|
+
*
|
|
480
|
+
* The `Uint8Array` may alias the batch backing storage and should be copied if
|
|
481
|
+
* it must outlive the callback or batch.
|
|
482
|
+
*/
|
|
157
483
|
export type CsvColumnBytesCallback = (rowIndex: number, bytes: Uint8Array) => void;
|
|
484
|
+
/**
|
|
485
|
+
* Receives ranges for several columns in one row without allocating per-field
|
|
486
|
+
* objects.
|
|
487
|
+
*
|
|
488
|
+
* `ranges` contains `[start, end]` pairs in the same order as `columns`, with
|
|
489
|
+
* `[-1, -1]` for a missing field. The range values address `data`; the typed
|
|
490
|
+
* array and buffer are reused for each row.
|
|
491
|
+
*/
|
|
158
492
|
export type CsvScanColumnsCallback = (rowIndex: number, ranges: Float64Array, data: Buffer) => void;
|
|
493
|
+
/**
|
|
494
|
+
* Reusable raw-data view over one columnar batch.
|
|
495
|
+
*
|
|
496
|
+
* This view is valid only during the synchronous callback that receives it and
|
|
497
|
+
* is rebound for the next batch. Its arrays and byte views alias native batch
|
|
498
|
+
* storage, so copy data that must outlive the callback.
|
|
499
|
+
*
|
|
500
|
+
* @typeParam TSelectedColumns - Literal selected-column indexes, when supplied.
|
|
501
|
+
*/
|
|
159
502
|
export interface CsvColumnarBatchView<TSelectedColumns extends CsvColumns | undefined = undefined> {
|
|
503
|
+
/** Number of rows represented by the batch. */
|
|
160
504
|
readonly rowCount: number;
|
|
505
|
+
/** Total number of fields across all rows in the batch. */
|
|
161
506
|
readonly totalFields: number;
|
|
507
|
+
/** Number of bytes in the backing field-data buffer. */
|
|
162
508
|
readonly dataLength: number;
|
|
163
509
|
/**
|
|
164
|
-
* Selected output columns requested for this batch
|
|
165
|
-
*
|
|
510
|
+
* Selected output columns requested for this batch, mapping output positions
|
|
511
|
+
* to physical CSV indexes.
|
|
512
|
+
*
|
|
513
|
+
* Column indexes passed to this view's methods address stored batch fields;
|
|
514
|
+
* when this property is defined, those indexes are projected output
|
|
515
|
+
* positions.
|
|
166
516
|
*/
|
|
167
517
|
readonly selectedColumns: TSelectedColumns;
|
|
518
|
+
/** Return the batch's field bytes as a `Buffer` view. */
|
|
168
519
|
data(): Buffer;
|
|
520
|
+
/** Return the batch's field bytes as a `Uint8Array` view. */
|
|
169
521
|
dataView(): Uint8Array;
|
|
522
|
+
/** Return `rowCount + 1` cumulative field indexes for row boundaries. */
|
|
170
523
|
rowOffsets(): BigUint64Array;
|
|
524
|
+
/** Return `totalFields + 1` cumulative byte offsets for field boundaries. */
|
|
171
525
|
fieldOffsets(): BigUint64Array;
|
|
526
|
+
/** Return the number of fields in one row. */
|
|
172
527
|
rowFieldCount(rowIndex: number): number;
|
|
528
|
+
/** Return a field's half-open byte range, or `null` when the column is missing. */
|
|
173
529
|
fieldRange(rowIndex: number, columnIndex: number): CsvFieldRange | null;
|
|
530
|
+
/** Return a field's raw bytes, or `null` when the column is missing. */
|
|
174
531
|
fieldBytes(rowIndex: number, columnIndex: number): Uint8Array | null;
|
|
532
|
+
/** Return a field's `Buffer` view, or `null` when the column is missing. */
|
|
175
533
|
fieldBuffer(rowIndex: number, columnIndex: number): Buffer | null;
|
|
534
|
+
/** Visit byte ranges for one column in the half-open row interval `[startRow, endRow)`. */
|
|
176
535
|
forEachColumnRange(columnIndex: number, callback: CsvColumnRangeCallback, startRow?: number, endRow?: number): void;
|
|
536
|
+
/** Visit raw bytes for one column in the half-open row interval `[startRow, endRow)`. */
|
|
177
537
|
forEachColumnBytes(columnIndex: number, callback: CsvColumnBytesCallback, startRow?: number, endRow?: number): void;
|
|
538
|
+
/** Visit several column ranges per row; missing fields are reported as `[-1, -1]`. */
|
|
178
539
|
scanColumns(columns: CsvColumns, callback: CsvScanColumnsCallback, startRow?: number, endRow?: number): void;
|
|
179
540
|
}
|
|
541
|
+
/**
|
|
542
|
+
* Synchronous callback invoked for each reusable columnar batch view.
|
|
543
|
+
*
|
|
544
|
+
* @typeParam TSelectedColumns - Literal selected-column indexes, when supplied.
|
|
545
|
+
* @param batch - Reusable raw-data view for the current batch.
|
|
546
|
+
* @param batchIndex - Zero-based batch index for the operation.
|
|
547
|
+
*/
|
|
180
548
|
export type CsvColumnarBatchCallback<TSelectedColumns extends CsvColumns | undefined = undefined> = (batch: CsvColumnarBatchView<TSelectedColumns>, batchIndex: number) => void;
|
|
181
549
|
export {};
|
package/dist/worker-count.d.ts
CHANGED
|
@@ -1,2 +1,34 @@
|
|
|
1
1
|
import type { CsvParallelCountOptions } from './types.js';
|
|
2
|
-
|
|
2
|
+
/**
|
|
3
|
+
* Count CSV records across record-safe byte shards in Bun workers.
|
|
4
|
+
*
|
|
5
|
+
* Each worker parses one uncompressed file range and the main thread sums the
|
|
6
|
+
* shard-local counts. The operation preserves count semantics for quoted
|
|
7
|
+
* records and can evaluate an immutable native `where` filter, but it does
|
|
8
|
+
* not materialize rows. Worker startup and message overhead make this most
|
|
9
|
+
* useful for files large enough to amortize fan-out.
|
|
10
|
+
*
|
|
11
|
+
* `workerCount` must be greater than one. Compressed input and automatic
|
|
12
|
+
* delimiter detection are not supported because byte offsets are resolved
|
|
13
|
+
* before workers open the file. Strict validation is also unsupported by the
|
|
14
|
+
* worker parser. Options are snapshotted at invocation, including filter
|
|
15
|
+
* values and arrays.
|
|
16
|
+
*
|
|
17
|
+
* @param path Path to an uncompressed CSV file.
|
|
18
|
+
* @param suppliedOptions Parallel count options, including the required worker count.
|
|
19
|
+
* @returns A promise for the total logical record count.
|
|
20
|
+
* @throws {RangeError} If `workerCount` is not an integer greater than one or the total exceeds `Number.MAX_SAFE_INTEGER`.
|
|
21
|
+
* @throws If compressed/automatic-delimiter input, strict mode, worker startup, file I/O, or CSV parsing fails.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```ts
|
|
25
|
+
* import { parallelCount, csv } from '@konstit/csv';
|
|
26
|
+
*
|
|
27
|
+
* const activeRows = await parallelCount('data.csv', {
|
|
28
|
+
* delimiter: ';',
|
|
29
|
+
* workerCount: 4,
|
|
30
|
+
* where: csv.column(2).equals('active'),
|
|
31
|
+
* });
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
export declare function parallelCount(path: string, suppliedOptions: CsvParallelCountOptions): Promise<number>;
|