@konstit/csv 0.4.1 → 0.5.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.
- package/README.md +152 -463
- package/THIRD_PARTY_NOTICES.md +7 -1
- package/dist/api.d.ts +169 -44
- package/dist/api.js +75 -368
- package/dist/batches.d.ts +250 -7
- package/dist/batches.js +135 -83
- package/dist/cli-command.d.ts +7 -0
- package/dist/cli-command.js +350 -0
- package/dist/cli-schema.d.ts +111 -0
- package/dist/cli-schema.js +219 -0
- package/dist/cli.js +2 -604
- package/dist/file-stream.d.ts +57 -2
- package/dist/file-stream.js +244 -36
- package/dist/files.d.ts +149 -3
- package/dist/files.js +37 -29
- package/dist/filter.d.ts +80 -0
- package/dist/format-bytes.d.ts +1 -0
- package/dist/format-bytes.js +11 -0
- package/dist/index.d.ts +3 -3
- package/dist/index.js +2 -14
- package/dist/inspect-file.d.ts +12 -0
- package/dist/inspect-file.js +88 -0
- package/dist/latin1-rows.d.ts +5 -0
- package/dist/latin1-rows.js +122 -0
- package/dist/materialize-field.d.ts +3 -0
- package/dist/materialize-field.js +31 -0
- package/dist/native.d.ts +51 -2
- package/dist/native.js +43 -4
- package/dist/normalize.d.ts +32 -2
- package/dist/normalize.js +51 -2
- package/dist/options-snapshot.d.ts +3 -0
- package/dist/options-snapshot.js +51 -0
- package/dist/options.d.ts +25 -10
- package/dist/options.js +0 -6
- package/dist/parser.d.ts +309 -4
- package/dist/parser.js +128 -31
- package/dist/strict-schema.d.ts +5 -3
- package/dist/strict-schema.js +4 -1
- package/dist/types.d.ts +355 -121
- package/dist/zip-reader.d.ts +11 -1
- package/dist/zip-reader.js +12 -3
- package/package.json +44 -29
- 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 +9 -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/worker-count.d.ts +0 -2
- package/dist/worker-count.js +0 -96
- package/dist/worker-factory.d.ts +0 -3
- package/dist/worker-factory.js +0 -9
- package/dist/worker-filter.d.ts +0 -29
- package/dist/worker-filter.js +0 -46
- package/dist/worker-module.d.ts +0 -1
- package/dist/worker-module.js +0 -4
- package/dist/worker-pool.d.ts +0 -17
- package/dist/worker-pool.js +0 -290
- package/dist/worker-rows.d.ts +0 -8
- package/dist/worker-rows.js +0 -118
- package/dist/workers/count.worker.d.ts +0 -1
- package/dist/workers/count.worker.js +0 -49
- package/dist/workers/rows.worker.d.ts +0 -1
- package/dist/workers/rows.worker.js +0 -85
package/dist/types.d.ts
CHANGED
|
@@ -1,181 +1,415 @@
|
|
|
1
1
|
import type { NativeCsvRowView } from './batches.js';
|
|
2
|
-
|
|
2
|
+
/**
|
|
3
|
+
* Text encoding used when CSV fields are decoded into JavaScript strings.
|
|
4
|
+
*
|
|
5
|
+
* The values are labels accepted by Bun's `TextDecoder`. `utf8` is the
|
|
6
|
+
* default. The `latin1`, `iso88591`, and `iso-8859-1` spellings retain the
|
|
7
|
+
* parser's native Latin-1 byte mapping.
|
|
8
|
+
*/
|
|
9
|
+
export type CsvEncoding = Bun.Encoding;
|
|
10
|
+
/** File readers accept explicit encoding labels or infer an encoding from the decompressed prefix. */
|
|
11
|
+
export type CsvFileEncoding = CsvEncoding | 'auto';
|
|
12
|
+
/** Encoding labels that can be passed directly to the native parser. */
|
|
13
|
+
export type CsvNativeEncoding = 'unicode-1-1-utf-8' | 'unicode11utf8' | 'unicode20utf8' | 'utf-8' | 'utf8' | 'x-unicode20utf8' | 'latin1' | 'iso88591' | 'iso-8859-1';
|
|
14
|
+
/**
|
|
15
|
+
* Selects one CSV entry from a ZIP archive.
|
|
16
|
+
*
|
|
17
|
+
* ZIP input is available to file parsing operations. The `entry` name must
|
|
18
|
+
* match the archive member exactly. `'*'` selects the only file after ignoring
|
|
19
|
+
* directories and OS metadata. This includes files in `__MACOSX/` or
|
|
20
|
+
* `.AppleDouble/` directories, `._*` files, and the case-insensitive filenames
|
|
21
|
+
* `.DS_Store`, `Thumbs.db`, `ehthumbs.db`, `ehthumbs_vista.db`, and `desktop.ini`.
|
|
22
|
+
*/
|
|
3
23
|
export interface CsvZipCompression {
|
|
4
|
-
format
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
24
|
+
/** ZIP is the selected container format. */
|
|
25
|
+
readonly format: 'zip';
|
|
26
|
+
/** Exact archive member name, or `'*'` for the only file after ignoring directories and OS metadata. */
|
|
27
|
+
readonly entry: string;
|
|
28
|
+
/**
|
|
29
|
+
* Maximum allowed decompressed-to-compressed byte ratio for the selected entry.
|
|
30
|
+
*
|
|
31
|
+
* Must be a positive integer no greater than `4_294_967_295`. The default is
|
|
32
|
+
* `1000`; lower values tighten protection against highly compressed entries.
|
|
33
|
+
*/
|
|
34
|
+
readonly maxCompressionRatio?: number;
|
|
35
|
+
/**
|
|
36
|
+
* Maximum decompressed bytes accepted from the entry.
|
|
37
|
+
*
|
|
38
|
+
* Must be a positive safe integer. The default is 64 GiB.
|
|
39
|
+
*/
|
|
40
|
+
readonly maxDecompressedBytes?: number;
|
|
8
41
|
}
|
|
9
|
-
|
|
42
|
+
/**
|
|
43
|
+
* Compression accepted for file input.
|
|
44
|
+
*
|
|
45
|
+
* The default, `'auto'`, detects compression, reads recognized text files
|
|
46
|
+
* directly, and selects the only ZIP data file after ignoring directories and
|
|
47
|
+
* OS metadata. ZIP input requires a regular file. Use `'none'` to read raw bytes.
|
|
48
|
+
* A ZIP object selects an exact entry or custom ZIP limits. Byte-offset sharding
|
|
49
|
+
* requires an uncompressed file.
|
|
50
|
+
*/
|
|
51
|
+
export type CsvCompression = Bun.CompressionFormat | 'auto' | 'none' | CsvZipCompression;
|
|
52
|
+
/**
|
|
53
|
+
* Field delimiter used by the CSV parser.
|
|
54
|
+
*
|
|
55
|
+
* Omit the option to use comma. `'auto'` is supported by file parsing
|
|
56
|
+
* operations and probes the input for a stable common delimiter. Other strings
|
|
57
|
+
* are accepted by TypeScript, but runtime parsing requires one character.
|
|
58
|
+
*/
|
|
10
59
|
export type CsvDelimiter = 'auto' | ',' | ';' | '\t' | '|' | ':' | '^' | '~' | (string & {});
|
|
60
|
+
/** A materialized CSV row, with one decoded string per field. */
|
|
11
61
|
export type CsvRow = string[];
|
|
62
|
+
/**
|
|
63
|
+
* Zero-based physical CSV column indexes.
|
|
64
|
+
*
|
|
65
|
+
* Projections may contain at most 2024 unique indexes. The array is readonly
|
|
66
|
+
* in the public type because each operation captures its configuration at
|
|
67
|
+
* invocation time, before asynchronous iteration begins.
|
|
68
|
+
*/
|
|
12
69
|
export type CsvColumns = readonly number[];
|
|
70
|
+
/** A field operand accepted by native filters: text or raw field bytes. */
|
|
13
71
|
export type CsvFieldValue = string | Buffer | Uint8Array;
|
|
14
72
|
declare const csvRegexBrand: unique symbol;
|
|
15
73
|
declare const csvFilterBrand: unique symbol;
|
|
74
|
+
/**
|
|
75
|
+
* Validated regular-expression metadata created by `csv.re()`.
|
|
76
|
+
*
|
|
77
|
+
* This branded type intentionally cannot be constructed by an object literal;
|
|
78
|
+
* use the factory so the pattern and flags are checked before parsing starts.
|
|
79
|
+
*/
|
|
16
80
|
export interface CsvRegex {
|
|
81
|
+
/** Pattern source after JavaScript regular-expression parsing. */
|
|
17
82
|
readonly source: string;
|
|
83
|
+
/** Supported flags (`i`, `m`, `s`, and `u`). */
|
|
18
84
|
readonly flags: string;
|
|
19
85
|
readonly [csvRegexBrand]: true;
|
|
20
86
|
}
|
|
87
|
+
/**
|
|
88
|
+
* Opaque immutable predicate created by the CSV filter factories.
|
|
89
|
+
*
|
|
90
|
+
* Pass filters through the `where` option or combine them with `csv.all()`,
|
|
91
|
+
* `csv.any()`, and `csv.not()`. Object-literal predicates are not accepted.
|
|
92
|
+
*/
|
|
21
93
|
export interface CsvFilter {
|
|
22
94
|
readonly [csvFilterBrand]: true;
|
|
23
95
|
}
|
|
96
|
+
/**
|
|
97
|
+
* Reusable condition builder for one zero-based physical CSV column.
|
|
98
|
+
*
|
|
99
|
+
* Each method returns a complete immutable `CsvFilter`; the builder itself can
|
|
100
|
+
* be retained and used to create several predicates for the same column.
|
|
101
|
+
*/
|
|
24
102
|
export interface CsvFilterColumn {
|
|
103
|
+
/**
|
|
104
|
+
* Match fields equal to `value`. A missing field does not match.
|
|
105
|
+
*
|
|
106
|
+
* Binary operands are copied when the filter is created.
|
|
107
|
+
*
|
|
108
|
+
* @param value - Text or bytes that must equal the field.
|
|
109
|
+
* @returns An immutable equality filter for this column.
|
|
110
|
+
* @throws {TypeError} If `value` is not a string, `Buffer`, or `Uint8Array` at runtime.
|
|
111
|
+
*/
|
|
25
112
|
readonly equals: (value: CsvFieldValue) => CsvFilter;
|
|
113
|
+
/**
|
|
114
|
+
* Match fields different from `value`. A missing field does not match.
|
|
115
|
+
*
|
|
116
|
+
* Binary operands are copied when the filter is created.
|
|
117
|
+
*
|
|
118
|
+
* @param value - Text or bytes that must differ from the field.
|
|
119
|
+
* @returns An immutable inequality filter for this column.
|
|
120
|
+
* @throws {TypeError} If `value` is not a string, `Buffer`, or `Uint8Array` at runtime.
|
|
121
|
+
*/
|
|
26
122
|
readonly doesNotEqual: (value: CsvFieldValue) => CsvFilter;
|
|
123
|
+
/**
|
|
124
|
+
* Match fields whose value is one of `values`.
|
|
125
|
+
*
|
|
126
|
+
* The array and all binary operands are copied when the filter is created.
|
|
127
|
+
*
|
|
128
|
+
* @param values - Non-empty set of text or byte values to match.
|
|
129
|
+
* @returns An immutable membership filter for this column.
|
|
130
|
+
* @throws {TypeError} If `values` or one of its members has an invalid runtime type.
|
|
131
|
+
* @throws {RangeError} If `values` is empty.
|
|
132
|
+
*/
|
|
27
133
|
readonly isOneOf: (values: readonly CsvFieldValue[]) => CsvFilter;
|
|
134
|
+
/**
|
|
135
|
+
* Match fields whose value is none of `values`. A missing field does not match.
|
|
136
|
+
*
|
|
137
|
+
* The array and all binary operands are copied when the filter is created.
|
|
138
|
+
*
|
|
139
|
+
* @param values - Non-empty set of text or byte values to exclude.
|
|
140
|
+
* @returns An immutable negative-membership filter for this column.
|
|
141
|
+
* @throws {TypeError} If `values` or one of its members has an invalid runtime type.
|
|
142
|
+
* @throws {RangeError} If `values` is empty.
|
|
143
|
+
*/
|
|
28
144
|
readonly isNoneOf: (values: readonly CsvFieldValue[]) => CsvFilter;
|
|
145
|
+
/**
|
|
146
|
+
* Match fields beginning with `prefix`.
|
|
147
|
+
*
|
|
148
|
+
* Binary operands are copied when the filter is created.
|
|
149
|
+
*
|
|
150
|
+
* @param prefix - Text or bytes that must occur at the start of the field.
|
|
151
|
+
* @returns An immutable prefix filter for this column.
|
|
152
|
+
* @throws {TypeError} If `prefix` is not a string, `Buffer`, or `Uint8Array` at runtime.
|
|
153
|
+
*/
|
|
29
154
|
readonly startsWith: (prefix: CsvFieldValue) => CsvFilter;
|
|
155
|
+
/**
|
|
156
|
+
* Match fields searched by the validated RE2-compatible `pattern`.
|
|
157
|
+
*
|
|
158
|
+
* The pattern is copied and validated immediately. Supported flags are `i`,
|
|
159
|
+
* `m`, `s`, and `u`; global, indices, sticky, and `v` flags are rejected.
|
|
160
|
+
* RE2 lookaround and backreferences are unsupported.
|
|
161
|
+
*
|
|
162
|
+
* @param pattern - JavaScript regular expression to validate and use for matching.
|
|
163
|
+
* @returns An immutable regular-expression filter for this column.
|
|
164
|
+
* @throws {TypeError} If `pattern` is not a `RegExp` at runtime.
|
|
165
|
+
* @throws {Error} If the flags are unsupported.
|
|
166
|
+
* @throws {SyntaxError} If the pattern is invalid for the native RE2 engine.
|
|
167
|
+
* @throws {RangeError} If the UTF-8 pattern exceeds 4096 bytes.
|
|
168
|
+
*/
|
|
30
169
|
readonly hasMatch: (pattern: RegExp) => CsvFilter;
|
|
31
170
|
}
|
|
171
|
+
/**
|
|
172
|
+
* Half-open byte range into a batch's backing data (`[start, end)`).
|
|
173
|
+
*
|
|
174
|
+
* The offsets are safe JavaScript numbers validated against the backing data;
|
|
175
|
+
* use them with `dataView().subarray(start, end)` when reading a batch view.
|
|
176
|
+
*/
|
|
32
177
|
export interface CsvFieldRange {
|
|
178
|
+
/** Inclusive start byte offset. */
|
|
33
179
|
start: number;
|
|
180
|
+
/** Exclusive end byte offset. */
|
|
34
181
|
end: number;
|
|
35
182
|
}
|
|
183
|
+
/**
|
|
184
|
+
* Inclusive byte range containing complete records from an uncompressed CSV file.
|
|
185
|
+
*/
|
|
36
186
|
export interface CsvShard {
|
|
187
|
+
/** Inclusive start byte offset in the file. */
|
|
37
188
|
start: number;
|
|
189
|
+
/** Inclusive end byte offset in the file. */
|
|
38
190
|
end: number;
|
|
39
191
|
}
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
export interface
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
192
|
+
/**
|
|
193
|
+
* Parser configuration shared by in-memory parsing and file APIs.
|
|
194
|
+
*
|
|
195
|
+
* Options are readonly and are captured at operation invocation time, before
|
|
196
|
+
* asynchronous iteration begins. Start a new operation to apply changed arrays
|
|
197
|
+
* or values.
|
|
198
|
+
*
|
|
199
|
+
* @typeParam TEncoding - Defaults to the native parser's labels. File and buffer
|
|
200
|
+
* parsing accept the full {@link CsvEncoding} set.
|
|
201
|
+
*/
|
|
202
|
+
export interface CsvParserOptions<TEncoding extends CsvFileEncoding = CsvNativeEncoding> {
|
|
203
|
+
/** Input field encoding used when decoding bytes into JavaScript strings. Defaults to `'utf8'`. */
|
|
204
|
+
readonly encoding?: TEncoding;
|
|
205
|
+
/** Field separator. Defaults to comma. File parsing also accepts `'auto'`. */
|
|
206
|
+
readonly delimiter?: CsvDelimiter;
|
|
207
|
+
/** Physical columns to emit from each row, in the requested output order. */
|
|
208
|
+
readonly columns?: CsvColumns;
|
|
209
|
+
/** Require exactly this many fields per row; malformed rows fail instead of being padded or truncated. */
|
|
210
|
+
readonly fixedColumns?: number;
|
|
211
|
+
/** Enable RFC-style quote and schema validation. Defaults to `false`. */
|
|
212
|
+
readonly strict?: boolean;
|
|
213
|
+
/** Expected first-row field values, checked in strict mode. */
|
|
214
|
+
readonly expectedHeaders?: readonly string[];
|
|
215
|
+
/** Require a first row when strict schema validation is enabled. */
|
|
216
|
+
readonly requireHeader?: boolean;
|
|
217
|
+
/** Minimum number of data rows after the header, checked in strict mode. */
|
|
218
|
+
readonly minDataRows?: number;
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* File input options, including parser settings and stream controls.
|
|
222
|
+
*
|
|
223
|
+
* Compression is streamed into the parser and does not load the complete file
|
|
224
|
+
* into memory. Byte-offset sharding requires an uncompressed file.
|
|
225
|
+
*/
|
|
226
|
+
export interface CsvFileOptions extends CsvParserOptions<CsvFileEncoding> {
|
|
227
|
+
/** Defaults to `'auto'`, probing up to 64 KiB after decompression, independently of the delimiter. */
|
|
228
|
+
readonly encoding?: CsvFileEncoding;
|
|
229
|
+
/** Read-stream chunk size in bytes. Defaults to 1 MiB. */
|
|
230
|
+
readonly chunkSize?: number;
|
|
231
|
+
/** Compression format or ZIP entry selection. Defaults to `'auto'`; `'none'` skips detection. */
|
|
232
|
+
readonly compression?: CsvCompression;
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* File options accepted by byte-offset sharding helpers.
|
|
236
|
+
*
|
|
237
|
+
* Compression is excluded because compressed byte streams cannot be split into
|
|
238
|
+
* independently readable file ranges.
|
|
239
|
+
*/
|
|
240
|
+
export type CsvShardingOptions = Omit<CsvFileOptions, 'compression' | 'encoding'> & {
|
|
241
|
+
/** Compression is unsupported for byte-offset sharding. */
|
|
242
|
+
readonly compression?: never;
|
|
243
|
+
/** Sharding scans raw file bytes and supports native encodings only. */
|
|
244
|
+
readonly encoding?: CsvNativeEncoding;
|
|
56
245
|
};
|
|
246
|
+
/** Native equality predicate used by projection and count fast paths. */
|
|
57
247
|
export interface CsvEqualsFilter {
|
|
58
|
-
column
|
|
59
|
-
|
|
248
|
+
/** Zero-based physical column to inspect. */
|
|
249
|
+
readonly column: number;
|
|
250
|
+
/** Field bytes or text that must equal the input field. */
|
|
251
|
+
readonly value: CsvFieldValue;
|
|
60
252
|
}
|
|
253
|
+
/** Native membership predicate used by projection and count fast paths. */
|
|
61
254
|
export interface CsvInFilter {
|
|
62
|
-
column
|
|
63
|
-
|
|
255
|
+
/** Zero-based physical column to inspect. */
|
|
256
|
+
readonly column: number;
|
|
257
|
+
/** Values for which the input field matches. */
|
|
258
|
+
readonly values: readonly CsvFieldValue[];
|
|
64
259
|
}
|
|
260
|
+
/** Native inequality predicate used by projection and count fast paths. */
|
|
65
261
|
export interface CsvNotEqualsFilter {
|
|
66
|
-
column
|
|
67
|
-
|
|
262
|
+
/** Zero-based physical column to inspect. */
|
|
263
|
+
readonly column: number;
|
|
264
|
+
/** Field bytes or text that must differ from the input field. */
|
|
265
|
+
readonly notEquals: CsvFieldValue;
|
|
68
266
|
}
|
|
267
|
+
/** Native negative-membership predicate used by projection and count fast paths. */
|
|
69
268
|
export interface CsvNotInFilter {
|
|
70
|
-
column
|
|
71
|
-
|
|
269
|
+
/** Zero-based physical column to inspect. */
|
|
270
|
+
readonly column: number;
|
|
271
|
+
/** Values that the input field must not match. */
|
|
272
|
+
readonly notIn: readonly CsvFieldValue[];
|
|
72
273
|
}
|
|
274
|
+
/** Native prefix predicate used by projection and count fast paths. */
|
|
73
275
|
export interface CsvStartsWithFilter {
|
|
74
|
-
column
|
|
75
|
-
|
|
276
|
+
/** Zero-based physical column to inspect. */
|
|
277
|
+
readonly column: number;
|
|
278
|
+
/** Field bytes or text that must occur at the start of the input field. */
|
|
279
|
+
readonly prefix: CsvFieldValue;
|
|
76
280
|
}
|
|
281
|
+
/** Native regular-expression predicate used by projection and count fast paths. */
|
|
77
282
|
export interface CsvRegexFilter {
|
|
78
|
-
column
|
|
79
|
-
|
|
283
|
+
/** Zero-based physical column to inspect. */
|
|
284
|
+
readonly column: number;
|
|
285
|
+
/** Validated pattern created by `csv.re()`. */
|
|
286
|
+
readonly regex: CsvRegex;
|
|
80
287
|
}
|
|
288
|
+
/**
|
|
289
|
+
* Native predicate shape accepted by low-level projection options.
|
|
290
|
+
*
|
|
291
|
+
* High-level row and count APIs normally use the opaque `CsvFilter` returned by
|
|
292
|
+
* the filter factories instead. High-level options copy operands at invocation
|
|
293
|
+
* time; low-level parser calls normalize their operands for each write.
|
|
294
|
+
*/
|
|
81
295
|
export type CsvNativeFilter = CsvEqualsFilter | CsvInFilter | CsvNotInFilter | CsvNotEqualsFilter | CsvRegexFilter | CsvStartsWithFilter;
|
|
296
|
+
/**
|
|
297
|
+
* Native projection and predicate configuration used by low-level parser APIs.
|
|
298
|
+
*
|
|
299
|
+
* `equalsFilter` and `filters` are mutually exclusive. When `filters` contains
|
|
300
|
+
* several entries, the native path combines them with an AND operation.
|
|
301
|
+
*/
|
|
82
302
|
export interface CsvNativeProjectionOptions {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
303
|
+
/** Physical columns to emit, in output order. */
|
|
304
|
+
readonly columns?: CsvColumns;
|
|
305
|
+
/** One equality predicate for the direct native fast path. */
|
|
306
|
+
readonly equalsFilter?: CsvEqualsFilter;
|
|
307
|
+
/** Native predicates combined for the operation. */
|
|
308
|
+
readonly filters?: readonly CsvNativeFilter[];
|
|
86
309
|
}
|
|
310
|
+
/** Common file options for the high-level `csv` API. */
|
|
87
311
|
export interface CsvApiFileOptions extends CsvFileOptions {
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
export
|
|
93
|
-
columns
|
|
94
|
-
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
strict
|
|
312
|
+
/** Opaque filter created by `csv.column()`, `csv.all()`, `csv.any()`, or `csv.not()`. */
|
|
313
|
+
readonly where?: CsvFilter;
|
|
314
|
+
}
|
|
315
|
+
/** Retains the literal input indexes and their order for projected row types. */
|
|
316
|
+
export interface CsvColumnSelection<TColumns extends CsvColumns | undefined> {
|
|
317
|
+
/** Physical columns to emit from each row, in the requested output order. */
|
|
318
|
+
readonly columns?: TColumns;
|
|
319
|
+
}
|
|
320
|
+
/** Shared operation properties have declarations that editors can use for hover documentation. */
|
|
321
|
+
interface CsvOperationOptions<TColumns extends CsvColumns | undefined> extends CsvColumnSelection<TColumns> {
|
|
322
|
+
/** Field separator. Defaults to comma. File parsing also accepts `'auto'`. */
|
|
323
|
+
readonly delimiter?: CsvDelimiter;
|
|
324
|
+
/** Require exactly this many fields per row; malformed rows fail instead of being padded or truncated. */
|
|
325
|
+
readonly fixedColumns?: number;
|
|
326
|
+
/** Expected first-row field values, checked in strict mode. */
|
|
327
|
+
readonly expectedHeaders?: readonly string[];
|
|
328
|
+
/** Require a first row when strict schema validation is enabled. */
|
|
329
|
+
readonly requireHeader?: boolean;
|
|
330
|
+
/** Minimum number of data rows after the header, checked in strict mode. */
|
|
331
|
+
readonly minDataRows?: number;
|
|
332
|
+
/** Read-stream chunk size in bytes. Defaults to 1 MiB. Used by file operations only. */
|
|
333
|
+
readonly chunkSize?: number;
|
|
334
|
+
}
|
|
335
|
+
/** File operations support encoding detection and streamed decompression. */
|
|
336
|
+
interface CsvFileOperationOptions<TColumns extends CsvColumns | undefined> extends CsvOperationOptions<TColumns> {
|
|
337
|
+
/** Defaults to `'auto'`, probing up to 64 KiB after decompression, independently of the delimiter. */
|
|
338
|
+
readonly encoding?: CsvFileEncoding;
|
|
339
|
+
/** Compression format or ZIP entry selection. Defaults to `'auto'`; `'none'` skips detection. */
|
|
340
|
+
readonly compression?: CsvCompression;
|
|
341
|
+
}
|
|
342
|
+
/** Non-strict operations can filter input records. */
|
|
343
|
+
interface CsvFilteredOperationOptions {
|
|
344
|
+
/** Enable RFC-style quote and schema validation. Defaults to `false`. Requires unfiltered input. */
|
|
345
|
+
readonly strict?: false | undefined;
|
|
346
|
+
/** Filter records with `csv.column()`, `csv.all()`, `csv.any()`, or `csv.not()`. Requires `strict` to be false or omitted. */
|
|
347
|
+
readonly where?: CsvFilter | undefined;
|
|
348
|
+
}
|
|
349
|
+
/** Strict validation requires unfiltered input. */
|
|
350
|
+
interface CsvStrictOperationOptions {
|
|
351
|
+
/** Enable RFC-style quote and schema validation. Requires unfiltered input. */
|
|
352
|
+
readonly strict: true;
|
|
353
|
+
/** Filtering is unavailable with strict validation, which checks every input record. */
|
|
354
|
+
readonly where?: undefined;
|
|
355
|
+
}
|
|
356
|
+
/** Each branch keeps its documented properties and enforces the strict/filter constraint. */
|
|
357
|
+
type CsvValidationOptions = CsvFilteredOperationOptions | CsvStrictOperationOptions;
|
|
358
|
+
/** Options for parsing an in-memory buffer into materialized rows. */
|
|
359
|
+
export type CsvParseOptions<TColumns extends CsvColumns | undefined = undefined> = CsvOperationOptions<TColumns> & CsvValidationOptions & {
|
|
360
|
+
/** Input field encoding used when decoding bytes into JavaScript strings. Defaults to `'utf8'`. */
|
|
361
|
+
readonly encoding?: CsvEncoding;
|
|
123
362
|
};
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
}) | (Omit<CsvApiOptionsWithoutSelection, 'where' | 'workerCount' | 'strict'> & CsvColumnSelection<CsvColumns | undefined> & {
|
|
129
|
-
strict: true;
|
|
130
|
-
where?: undefined;
|
|
131
|
-
workerCount?: 1 | undefined;
|
|
132
|
-
});
|
|
133
|
-
export type CsvParallelCountOptions = Omit<CsvApiOptionsWithoutSelection, 'compression' | 'where' | 'workerCount' | 'strict'> & CsvColumnSelection<CsvColumns | undefined> & {
|
|
134
|
-
compression?: never;
|
|
135
|
-
where?: CsvFilter | undefined;
|
|
136
|
-
workerCount: number;
|
|
137
|
-
strict?: false | undefined;
|
|
363
|
+
/** Options for streaming materialized rows in file order. */
|
|
364
|
+
export type CsvRowsOptions<TColumns extends CsvColumns | undefined = undefined> = CsvFileOperationOptions<TColumns> & CsvValidationOptions & {
|
|
365
|
+
/** Refill the batch and its row arrays across iterations. Copy any row you want to keep. Defaults to false. */
|
|
366
|
+
readonly reuseArrays?: boolean;
|
|
138
367
|
};
|
|
139
|
-
|
|
140
|
-
export type
|
|
368
|
+
/** Strict native batches require unfiltered, unprojected input. */
|
|
369
|
+
export type CsvBatchesOptions = (CsvFileOperationOptions<CsvColumns | undefined> & CsvFilteredOperationOptions) | (CsvFileOperationOptions<undefined> & CsvStrictOperationOptions);
|
|
370
|
+
/** Options accepted by `csv.count()`. */
|
|
371
|
+
export type CsvCountOptions = CsvFileOperationOptions<CsvColumns | undefined> & CsvValidationOptions;
|
|
372
|
+
/**
|
|
373
|
+
* Materialized row type inferred from a literal projection tuple.
|
|
374
|
+
*
|
|
375
|
+
* For example, `CsvProjectedRow<readonly [0, 2]>` is a two-element mutable
|
|
376
|
+
* string tuple. Without a projection tuple the result is the variable-length
|
|
377
|
+
* `CsvRow` shape.
|
|
378
|
+
*/
|
|
141
379
|
export type CsvProjectedRow<TColumns extends CsvColumns | undefined> = TColumns extends readonly unknown[] ? {
|
|
142
380
|
-readonly [Index in keyof TColumns]: string;
|
|
143
381
|
} : CsvRow;
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
};
|
|
154
|
-
export type CsvRowViewCallback<TSelectedColumns extends CsvColumns | undefined = undefined> = (row: CsvRowView<TSelectedColumns>, rowIndex: number) => void;
|
|
382
|
+
/**
|
|
383
|
+
* Synchronous callback invoked by `NativeCsvBatch.forEachRow()`.
|
|
384
|
+
*
|
|
385
|
+
* The same native row-view object is moved to each row in sequence; copy any
|
|
386
|
+
* values needed after the callback returns.
|
|
387
|
+
*
|
|
388
|
+
* @param row - Reusable view positioned at `rowIndex`.
|
|
389
|
+
* @param rowIndex - Zero-based row index within the batch.
|
|
390
|
+
*/
|
|
155
391
|
export type NativeCsvRowCallback = (row: NativeCsvRowView, rowIndex: number) => void;
|
|
392
|
+
/**
|
|
393
|
+
* Receives the byte range for one field in a columnar batch.
|
|
394
|
+
*
|
|
395
|
+
* `start` is inclusive and `end` is exclusive; both offsets address the batch's
|
|
396
|
+
* `data()` buffer. Rows that do not contain the requested column are skipped.
|
|
397
|
+
*/
|
|
156
398
|
export type CsvColumnRangeCallback = (rowIndex: number, start: number, end: number) => void;
|
|
399
|
+
/**
|
|
400
|
+
* Receives raw bytes for one field in a columnar batch.
|
|
401
|
+
*
|
|
402
|
+
* The `Uint8Array` may alias the batch backing storage and should be copied if
|
|
403
|
+
* it must outlive the callback or batch.
|
|
404
|
+
*/
|
|
157
405
|
export type CsvColumnBytesCallback = (rowIndex: number, bytes: Uint8Array) => void;
|
|
406
|
+
/**
|
|
407
|
+
* Receives ranges for several columns in one row without allocating per-field
|
|
408
|
+
* objects.
|
|
409
|
+
*
|
|
410
|
+
* `ranges` contains `[start, end]` pairs in the same order as `columns`, with
|
|
411
|
+
* `[-1, -1]` for a missing field. The range values address `data`; the typed
|
|
412
|
+
* array and buffer are reused for each row.
|
|
413
|
+
*/
|
|
158
414
|
export type CsvScanColumnsCallback = (rowIndex: number, ranges: Float64Array, data: Buffer) => void;
|
|
159
|
-
export interface CsvColumnarBatchView<TSelectedColumns extends CsvColumns | undefined = undefined> {
|
|
160
|
-
readonly rowCount: number;
|
|
161
|
-
readonly totalFields: number;
|
|
162
|
-
readonly dataLength: number;
|
|
163
|
-
/**
|
|
164
|
-
* Selected output columns requested for this batch. Unlike `CsvRowView`, column indexes on the
|
|
165
|
-
* batch are relative to the projected output when `selectedColumns` is defined.
|
|
166
|
-
*/
|
|
167
|
-
readonly selectedColumns: TSelectedColumns;
|
|
168
|
-
data(): Buffer;
|
|
169
|
-
dataView(): Uint8Array;
|
|
170
|
-
rowOffsets(): BigUint64Array;
|
|
171
|
-
fieldOffsets(): BigUint64Array;
|
|
172
|
-
rowFieldCount(rowIndex: number): number;
|
|
173
|
-
fieldRange(rowIndex: number, columnIndex: number): CsvFieldRange | null;
|
|
174
|
-
fieldBytes(rowIndex: number, columnIndex: number): Uint8Array | null;
|
|
175
|
-
fieldBuffer(rowIndex: number, columnIndex: number): Buffer | null;
|
|
176
|
-
forEachColumnRange(columnIndex: number, callback: CsvColumnRangeCallback, startRow?: number, endRow?: number): void;
|
|
177
|
-
forEachColumnBytes(columnIndex: number, callback: CsvColumnBytesCallback, startRow?: number, endRow?: number): void;
|
|
178
|
-
scanColumns(columns: CsvColumns, callback: CsvScanColumnsCallback, startRow?: number, endRow?: number): void;
|
|
179
|
-
}
|
|
180
|
-
export type CsvColumnarBatchCallback<TSelectedColumns extends CsvColumns | undefined = undefined> = (batch: CsvColumnarBatchView<TSelectedColumns>, batchIndex: number) => void;
|
|
181
415
|
export {};
|
package/dist/zip-reader.d.ts
CHANGED
|
@@ -1,2 +1,12 @@
|
|
|
1
1
|
import type { CsvZipCompression } from './types.js';
|
|
2
|
-
export declare
|
|
2
|
+
export declare class NativeZipReader {
|
|
3
|
+
#private;
|
|
4
|
+
constructor(path: string, options: CsvZipCompression);
|
|
5
|
+
get status(): number;
|
|
6
|
+
enableReadAhead(chunkSize: number): void;
|
|
7
|
+
read(output: Uint8Array): number;
|
|
8
|
+
lastError(): string;
|
|
9
|
+
close(): void;
|
|
10
|
+
[Symbol.dispose](): void;
|
|
11
|
+
}
|
|
12
|
+
export declare function readZipEntryChunks(path: string, options: CsvZipCompression, chunkSize: number, readAhead?: boolean): AsyncGenerator<Uint8Array, void>;
|
package/dist/zip-reader.js
CHANGED
|
@@ -11,7 +11,7 @@ function destroyNativeZipReader(handle) {
|
|
|
11
11
|
native.symbols.csv_zip_reader_destroy(handle);
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
-
class NativeZipReader {
|
|
14
|
+
export class NativeZipReader {
|
|
15
15
|
#handle;
|
|
16
16
|
constructor(path, options) {
|
|
17
17
|
if (path.includes("\x00"))
|
|
@@ -35,6 +35,9 @@ class NativeZipReader {
|
|
|
35
35
|
get status() {
|
|
36
36
|
return native.symbols.csv_zip_reader_status(this.#requireHandle());
|
|
37
37
|
}
|
|
38
|
+
enableReadAhead(chunkSize) {
|
|
39
|
+
native.symbols.csv_zip_reader_enable_read_ahead(this.#requireHandle(), chunkSize);
|
|
40
|
+
}
|
|
38
41
|
read(output) {
|
|
39
42
|
return u64ToSafeNumber(native.symbols.csv_zip_reader_read(this.#requireHandle(), output, output), "ZIP decompressed chunk length");
|
|
40
43
|
}
|
|
@@ -58,11 +61,13 @@ class NativeZipReader {
|
|
|
58
61
|
return this.#handle;
|
|
59
62
|
}
|
|
60
63
|
}
|
|
61
|
-
export async function* readZipEntryChunks(path, options, chunkSize) {
|
|
64
|
+
export async function* readZipEntryChunks(path, options, chunkSize, readAhead = !1) {
|
|
62
65
|
const outputSize = positiveUint32(chunkSize, "ZIP output chunk size");
|
|
63
66
|
using reader = new NativeZipReader(path, options);
|
|
67
|
+
const reusableBuffer = readAhead ? new Uint8Array(outputSize) : void 0;
|
|
68
|
+
let startProducer = readAhead;
|
|
64
69
|
while (!0) {
|
|
65
|
-
const output = new Uint8Array(outputSize);
|
|
70
|
+
const output = reusableBuffer ?? new Uint8Array(outputSize);
|
|
66
71
|
const written = reader.read(output);
|
|
67
72
|
const status = reader.status;
|
|
68
73
|
if (status === ZIP_READER_ERROR)
|
|
@@ -75,6 +80,10 @@ export async function* readZipEntryChunks(path, options, chunkSize) {
|
|
|
75
80
|
return;
|
|
76
81
|
if (written === 0)
|
|
77
82
|
throw Error("native ZIP reader made no progress");
|
|
83
|
+
if (startProducer) {
|
|
84
|
+
reader.enableReadAhead(outputSize);
|
|
85
|
+
startProducer = !1;
|
|
86
|
+
}
|
|
78
87
|
}
|
|
79
88
|
}
|
|
80
89
|
function positiveSafeInteger(value, label) {
|