@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/parser.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { NativeCsvBatch } from "./batches.js";
|
|
2
2
|
import {
|
|
3
|
+
EMPTY_BUFFER,
|
|
3
4
|
native,
|
|
4
5
|
u64ToSafeNumber
|
|
5
6
|
} from "./native.js";
|
|
@@ -12,29 +13,121 @@ import {
|
|
|
12
13
|
normalizeInFilter,
|
|
13
14
|
normalizeNativeFilterProgram,
|
|
14
15
|
normalizeNativeFilters,
|
|
15
|
-
normalizeStartsWithFilter
|
|
16
|
+
normalizeStartsWithFilter,
|
|
17
|
+
rejectSelectedColumns
|
|
16
18
|
} from "./normalize.js";
|
|
17
19
|
function destroyNativeCsvParser(handle) {
|
|
18
20
|
native.symbols.csv_parser_destroy(handle);
|
|
19
21
|
}
|
|
20
|
-
const nativeCsvParserFinalizer = new FinalizationRegistry(destroyNativeCsvParser);
|
|
22
|
+
export const nativeCsvParserFinalizer = new FinalizationRegistry(destroyNativeCsvParser);
|
|
23
|
+
export function createPreparedParser(options, columns, program, projected = !1) {
|
|
24
|
+
if (!projected && columns === 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.columns !== columns)
|
|
29
|
+
normalizeColumns(options.columns);
|
|
30
|
+
normalizeFixedColumnsCount(options.fixedColumns, "fixed column count");
|
|
31
|
+
return new PreparedNativeCsvParser(options, columns, program);
|
|
32
|
+
}
|
|
33
|
+
export function prepareNativeParser(handle, columns, program) {
|
|
34
|
+
const normalizedColumns = normalizeColumns(columns);
|
|
35
|
+
const hasProjection = columns !== void 0;
|
|
36
|
+
const columnCount = BigInt(columns?.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
|
+
if (!native.symbols.csv_parser_prepare(handle, hasProjection, normalizedColumns, columnCount, normalized.descriptors, filterCount, normalized.valuesData, normalized.valuesData, normalized.valueOffsets, valueCount))
|
|
41
|
+
throw Error(`native CSV parser failed: ${native.symbols.csv_parser_last_error(handle) ?? "native parser error unavailable"}`);
|
|
42
|
+
return {
|
|
43
|
+
writeBatch: (input, final) => native.symbols.csv_parser_write_prepared_batch(handle, input, input, final),
|
|
44
|
+
writeCount: (input, final) => {
|
|
45
|
+
const count = native.symbols.csv_parser_write_prepared_count(handle, input, input, final);
|
|
46
|
+
if (count === 0xffffffffffffffffn)
|
|
47
|
+
throw Error(`native CSV parser failed: ${native.symbols.csv_parser_last_error(handle) ?? "native parser error unavailable"}`);
|
|
48
|
+
return count;
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
class PreparedNativeCsvParser {
|
|
54
|
+
#handle;
|
|
55
|
+
#writer;
|
|
56
|
+
#fixedValidator;
|
|
57
|
+
constructor(options, columns, program) {
|
|
58
|
+
const handle = createNativeCsvParser(options);
|
|
59
|
+
try {
|
|
60
|
+
this.#writer = prepareNativeParser(handle, columns, program);
|
|
61
|
+
this.#fixedValidator = options.fixedColumns === void 0 ? void 0 : new NativeCsvParser(options);
|
|
62
|
+
} catch (error) {
|
|
63
|
+
destroyNativeCsvParser(handle);
|
|
64
|
+
throw error;
|
|
65
|
+
}
|
|
66
|
+
this.#handle = handle;
|
|
67
|
+
nativeCsvParserFinalizer.register(this, handle, this);
|
|
68
|
+
}
|
|
69
|
+
writeBatch(chunk, final = !1) {
|
|
70
|
+
const handle = this.#requireHandle();
|
|
71
|
+
if (this.#fixedValidator !== void 0)
|
|
72
|
+
this.#fixedValidator.writeBatch(chunk, final).close();
|
|
73
|
+
const batch = this.#writer.writeBatch(normalizeChunk(chunk), final);
|
|
74
|
+
if (batch === null)
|
|
75
|
+
throw Error(`native CSV parser failed: ${native.symbols.csv_parser_last_error(handle) ?? "native parser error unavailable"}`);
|
|
76
|
+
return new NativeCsvBatch(batch);
|
|
77
|
+
}
|
|
78
|
+
endBatch() {
|
|
79
|
+
return this.writeBatch(EMPTY_BUFFER, !0);
|
|
80
|
+
}
|
|
81
|
+
writeCount(chunk, final = !1) {
|
|
82
|
+
this.#requireHandle();
|
|
83
|
+
if (this.#fixedValidator !== void 0)
|
|
84
|
+
this.#fixedValidator.writeBatch(chunk, final).close();
|
|
85
|
+
const count = this.#writer.writeCount(normalizeChunk(chunk), final);
|
|
86
|
+
return u64ToSafeNumber(count, "CSV filtered row count");
|
|
87
|
+
}
|
|
88
|
+
endCount() {
|
|
89
|
+
return this.writeCount(EMPTY_BUFFER, !0);
|
|
90
|
+
}
|
|
91
|
+
close() {
|
|
92
|
+
const handle = this.#handle;
|
|
93
|
+
if (handle === null)
|
|
94
|
+
return;
|
|
95
|
+
this.#handle = null;
|
|
96
|
+
nativeCsvParserFinalizer.unregister(this);
|
|
97
|
+
this.#fixedValidator?.close();
|
|
98
|
+
destroyNativeCsvParser(handle);
|
|
99
|
+
}
|
|
100
|
+
[Symbol.dispose]() {
|
|
101
|
+
this.close();
|
|
102
|
+
}
|
|
103
|
+
#requireHandle() {
|
|
104
|
+
if (this.#handle === null)
|
|
105
|
+
throw Error("native CSV parser is closed");
|
|
106
|
+
return this.#handle;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
function createNativeCsvParser(options) {
|
|
110
|
+
const delimiter = options.delimiter ?? ",";
|
|
111
|
+
if (delimiter === "auto")
|
|
112
|
+
throw Error("automatic delimiter detection is only supported by file APIs");
|
|
113
|
+
if (delimiter.length !== 1)
|
|
114
|
+
throw Error("delimiter must be one character");
|
|
115
|
+
const handle = native.symbols.csv_parser_create(encodingCode(options.encoding), delimiter.charCodeAt(0));
|
|
116
|
+
if (handle === null)
|
|
117
|
+
throw Error("failed to create native CSV parser");
|
|
118
|
+
return handle;
|
|
119
|
+
}
|
|
21
120
|
|
|
22
121
|
export class NativeCsvParser {
|
|
23
122
|
#handle;
|
|
24
123
|
#strict;
|
|
25
124
|
#fixedColumns;
|
|
26
125
|
constructor(options = {}) {
|
|
27
|
-
|
|
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");
|
|
126
|
+
rejectSelectedColumns(options);
|
|
32
127
|
this.#strict = options.strict === !0;
|
|
33
|
-
normalizeColumns(options.
|
|
128
|
+
normalizeColumns(options.columns);
|
|
34
129
|
this.#fixedColumns = normalizeFixedColumnsCount(options.fixedColumns, "fixed column count");
|
|
35
|
-
const handle =
|
|
36
|
-
if (handle === null)
|
|
37
|
-
throw Error("failed to create native CSV parser");
|
|
130
|
+
const handle = createNativeCsvParser(options);
|
|
38
131
|
this.#handle = handle;
|
|
39
132
|
nativeCsvParserFinalizer.register(this, handle, this);
|
|
40
133
|
}
|
|
@@ -61,15 +154,15 @@ export class NativeCsvParser {
|
|
|
61
154
|
}
|
|
62
155
|
writeProjectedBatch(chunk, options = {}, final = !1) {
|
|
63
156
|
this.#rejectStrictUnsupported("projected batches");
|
|
64
|
-
this.#
|
|
157
|
+
this.#validateProjectionOptions(options);
|
|
65
158
|
if (chunk.byteLength === 0 && final)
|
|
66
159
|
return this.endProjectedBatch(options);
|
|
67
160
|
const handle = this.#requireHandle();
|
|
68
161
|
const input = normalizeChunk(chunk);
|
|
69
|
-
const columns = normalizeColumns(options.
|
|
162
|
+
const columns = normalizeColumns(options.columns);
|
|
70
163
|
const filter = normalizeEqualsFilter(options.equalsFilter);
|
|
71
164
|
const filters = normalizeNativeFilters(options.filters);
|
|
72
|
-
const batch = options.filters === void 0 ? native.symbols.csv_parser_write_projected_batch(handle, input, input, final, options.
|
|
165
|
+
const batch = options.filters === void 0 ? native.symbols.csv_parser_write_projected_batch(handle, input, input, final, options.columns !== void 0, columns, BigInt(options.columns?.length ?? 0), filter.enabled, filter.column, filter.value, filter.value) : native.symbols.csv_parser_write_projected_batch_where_all(handle, input, input, final, options.columns !== void 0, columns, BigInt(options.columns?.length ?? 0), filters.descriptors, BigInt(filters.filterCount), filters.valuesData, filters.valuesData, filters.valueOffsets, BigInt(filters.valueCount));
|
|
73
166
|
if (batch === null)
|
|
74
167
|
throw Error(`native CSV parser failed: ${this.#lastError()}`);
|
|
75
168
|
return new NativeCsvBatch(batch);
|
|
@@ -90,39 +183,43 @@ export class NativeCsvParser {
|
|
|
90
183
|
}
|
|
91
184
|
endProjectedBatch(options = {}) {
|
|
92
185
|
this.#rejectStrictUnsupported("projected batches");
|
|
93
|
-
this.#
|
|
94
|
-
const columns = normalizeColumns(options.
|
|
186
|
+
this.#validateProjectionOptions(options);
|
|
187
|
+
const columns = normalizeColumns(options.columns);
|
|
95
188
|
const filter = normalizeEqualsFilter(options.equalsFilter);
|
|
96
189
|
const filters = normalizeNativeFilters(options.filters);
|
|
97
|
-
const batch = options.filters === void 0 ? native.symbols.csv_parser_finish_projected_batch(this.#requireHandle(), options.
|
|
190
|
+
const batch = options.filters === void 0 ? native.symbols.csv_parser_finish_projected_batch(this.#requireHandle(), options.columns !== void 0, columns, BigInt(options.columns?.length ?? 0), filter.enabled, filter.column, filter.value, filter.value) : native.symbols.csv_parser_finish_projected_batch_where_all(this.#requireHandle(), options.columns !== void 0, columns, BigInt(options.columns?.length ?? 0), filters.descriptors, BigInt(filters.filterCount), filters.valuesData, filters.valuesData, filters.valueOffsets, BigInt(filters.valueCount));
|
|
98
191
|
if (batch === null)
|
|
99
192
|
throw Error(`native CSV parser failed: ${this.#lastError()}`);
|
|
100
193
|
return new NativeCsvBatch(batch);
|
|
101
194
|
}
|
|
102
|
-
writeProjectedBatchWhere(chunk,
|
|
195
|
+
writeProjectedBatchWhere(chunk, columns, program, final = !1) {
|
|
103
196
|
this.#rejectStrictUnsupported("projected batches");
|
|
104
197
|
if (chunk.byteLength === 0 && final)
|
|
105
|
-
return this.endProjectedBatchWhere(
|
|
198
|
+
return this.endProjectedBatchWhere(columns, program);
|
|
106
199
|
const handle = this.#requireHandle();
|
|
107
200
|
const input = normalizeChunk(chunk);
|
|
108
|
-
const
|
|
201
|
+
const normalizedColumns = normalizeColumns(columns);
|
|
109
202
|
const normalized = normalizeNativeFilterProgram(program);
|
|
110
|
-
const batch = native.symbols.csv_parser_write_projected_batch_where_all(handle, input, input, final,
|
|
203
|
+
const batch = native.symbols.csv_parser_write_projected_batch_where_all(handle, input, input, final, columns !== void 0, normalizedColumns, BigInt(columns?.length ?? 0), normalized.descriptors, BigInt(normalized.filterCount), normalized.valuesData, normalized.valuesData, normalized.valueOffsets, BigInt(normalized.valueCount));
|
|
111
204
|
if (batch === null)
|
|
112
205
|
throw Error(`native CSV parser failed: ${this.#lastError()}`);
|
|
113
206
|
return new NativeCsvBatch(batch);
|
|
114
207
|
}
|
|
115
|
-
endProjectedBatchWhere(
|
|
208
|
+
endProjectedBatchWhere(columns, program) {
|
|
116
209
|
this.#rejectStrictUnsupported("projected batches");
|
|
117
|
-
const
|
|
210
|
+
const normalizedColumns = normalizeColumns(columns);
|
|
118
211
|
const normalized = normalizeNativeFilterProgram(program);
|
|
119
|
-
const batch = native.symbols.csv_parser_finish_projected_batch_where_all(this.#requireHandle(),
|
|
212
|
+
const batch = native.symbols.csv_parser_finish_projected_batch_where_all(this.#requireHandle(), columns !== void 0, normalizedColumns, BigInt(columns?.length ?? 0), normalized.descriptors, BigInt(normalized.filterCount), normalized.valuesData, normalized.valuesData, normalized.valueOffsets, BigInt(normalized.valueCount));
|
|
120
213
|
if (batch === null)
|
|
121
214
|
throw Error(`native CSV parser failed: ${this.#lastError()}`);
|
|
122
215
|
return new NativeCsvBatch(batch);
|
|
123
216
|
}
|
|
124
217
|
writeCount(chunk, final = !1) {
|
|
125
218
|
this.#rejectStrictUnsupported("count");
|
|
219
|
+
if (this.#fixedColumns !== void 0) {
|
|
220
|
+
using batch = this.writeBatch(chunk, final);
|
|
221
|
+
return batch.rowCount;
|
|
222
|
+
}
|
|
126
223
|
if (chunk.byteLength === 0 && final)
|
|
127
224
|
return this.endCount();
|
|
128
225
|
const handle = this.#requireHandle();
|
|
@@ -131,6 +228,10 @@ export class NativeCsvParser {
|
|
|
131
228
|
}
|
|
132
229
|
endCount() {
|
|
133
230
|
this.#rejectStrictUnsupported("count");
|
|
231
|
+
if (this.#fixedColumns !== void 0) {
|
|
232
|
+
using batch = this.endBatch();
|
|
233
|
+
return batch.rowCount;
|
|
234
|
+
}
|
|
134
235
|
return u64ToSafeNumber(native.symbols.csv_parser_finish_count(this.#requireHandle()), "CSV parsed row count");
|
|
135
236
|
}
|
|
136
237
|
writeCountWhereEquals(chunk, filter, final = !1) {
|
|
@@ -222,9 +323,6 @@ export class NativeCsvParser {
|
|
|
222
323
|
nativeCsvParserFinalizer.unregister(this);
|
|
223
324
|
destroyNativeCsvParser(handle);
|
|
224
325
|
}
|
|
225
|
-
dispose() {
|
|
226
|
-
this.close();
|
|
227
|
-
}
|
|
228
326
|
[Symbol.dispose]() {
|
|
229
327
|
this.close();
|
|
230
328
|
}
|
|
@@ -234,9 +332,7 @@ export class NativeCsvParser {
|
|
|
234
332
|
return this.#handle;
|
|
235
333
|
}
|
|
236
334
|
#lastError() {
|
|
237
|
-
|
|
238
|
-
return "parser is closed";
|
|
239
|
-
return native.symbols.csv_parser_last_error(this.#handle) ?? "native parser error unavailable";
|
|
335
|
+
return native.symbols.csv_parser_last_error(this.#requireHandle()) ?? "native parser error unavailable";
|
|
240
336
|
}
|
|
241
337
|
#throwIfNativeError() {
|
|
242
338
|
const error = this.#lastError();
|
|
@@ -247,7 +343,8 @@ export class NativeCsvParser {
|
|
|
247
343
|
if (this.#strict)
|
|
248
344
|
throw Error(`strict CSV validation is not supported for ${operation}`);
|
|
249
345
|
}
|
|
250
|
-
#
|
|
346
|
+
#validateProjectionOptions(options) {
|
|
347
|
+
rejectSelectedColumns(options);
|
|
251
348
|
if (options.equalsFilter !== void 0 && options.filters !== void 0)
|
|
252
349
|
throw Error("use equalsFilter or filters, not both");
|
|
253
350
|
}
|
package/dist/strict-schema.d.ts
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
import type { NativeCsvBatch } from './batches.js';
|
|
2
2
|
import type { CsvParserOptions, CsvRow } from './types.js';
|
|
3
|
+
type StrictSchemaOptions = Pick<CsvParserOptions, 'strict' | 'expectedHeaders' | 'requireHeader' | 'minDataRows'>;
|
|
3
4
|
export declare class CsvStrictSchemaValidator {
|
|
4
5
|
#private;
|
|
5
|
-
constructor(
|
|
6
|
+
constructor(suppliedOptions: StrictSchemaOptions);
|
|
6
7
|
get enabled(): boolean;
|
|
7
8
|
validateRows(rows: readonly CsvRow[]): void;
|
|
8
9
|
validateBatch(batch: NativeCsvBatch): void;
|
|
9
10
|
finish(): void;
|
|
10
11
|
}
|
|
11
|
-
export declare function strictSchemaValidator(options:
|
|
12
|
-
export declare function rejectStrictSchemaUnsupported(options:
|
|
12
|
+
export declare function strictSchemaValidator(options: StrictSchemaOptions): CsvStrictSchemaValidator | undefined;
|
|
13
|
+
export declare function rejectStrictSchemaUnsupported(options: StrictSchemaOptions, operation: string): void;
|
|
14
|
+
export {};
|
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),
|