@konstit/csv 0.5.0 → 0.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +9 -3
- package/dist/api.js +20 -94
- package/dist/batches.d.ts +3 -4
- package/dist/batches.js +12 -10
- package/dist/cli-command.js +42 -77
- package/dist/cli-schema.d.ts +21 -5
- package/dist/cli-schema.js +37 -22
- package/dist/file-stream.d.ts +7 -3
- package/dist/file-stream.js +13 -12
- package/dist/files.js +33 -76
- package/dist/filter.d.ts +1 -2
- package/dist/index.d.ts +1 -1
- package/dist/inspect-file.d.ts +16 -6
- package/dist/inspect-file.js +67 -17
- package/dist/latin1-rows.d.ts +2 -3
- package/dist/latin1-rows.js +13 -3
- package/dist/native.d.ts +21 -69
- package/dist/native.js +9 -83
- package/dist/normalize.d.ts +1 -40
- package/dist/normalize.js +25 -62
- package/dist/parser.d.ts +17 -202
- package/dist/parser.js +131 -230
- package/dist/strict-schema.d.ts +1 -1
- package/dist/strict-schema.js +4 -3
- package/dist/types.d.ts +37 -5
- package/dist/zip-reader.d.ts +2 -0
- package/dist/zip-reader.js +31 -21
- package/package.json +3 -2
- 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.js
CHANGED
|
@@ -8,32 +8,77 @@ import {
|
|
|
8
8
|
encodingCode,
|
|
9
9
|
normalizeChunk,
|
|
10
10
|
normalizeColumns,
|
|
11
|
-
normalizeEqualsFilter,
|
|
12
11
|
normalizeFixedColumnsCount,
|
|
13
|
-
normalizeInFilter,
|
|
14
12
|
normalizeNativeFilterProgram,
|
|
15
13
|
normalizeNativeFilters,
|
|
16
|
-
|
|
14
|
+
projectionFilterProgram,
|
|
17
15
|
rejectSelectedColumns
|
|
18
16
|
} from "./normalize.js";
|
|
17
|
+
import {
|
|
18
|
+
strictSchemaValidator
|
|
19
|
+
} from "./strict-schema.js";
|
|
19
20
|
function destroyNativeCsvParser(handle) {
|
|
20
21
|
native.symbols.csv_parser_destroy(handle);
|
|
21
22
|
}
|
|
22
23
|
export const nativeCsvParserFinalizer = new FinalizationRegistry(destroyNativeCsvParser);
|
|
23
|
-
export function
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
24
|
+
export async function* parseFileChunks(input, parser, limit) {
|
|
25
|
+
let remaining = limit;
|
|
26
|
+
if (remaining !== 0)
|
|
27
|
+
for await (const chunk of input.chunks()) {
|
|
28
|
+
const batch = parser.writeBatch(chunk);
|
|
29
|
+
const count = batch.rowCount;
|
|
30
|
+
if (count > 0)
|
|
31
|
+
yield batch;
|
|
32
|
+
else
|
|
33
|
+
batch.close();
|
|
34
|
+
if (remaining !== void 0) {
|
|
35
|
+
remaining -= count;
|
|
36
|
+
if (remaining === 0)
|
|
37
|
+
break;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
const final = parser.endBatch();
|
|
41
|
+
if (final.rowCount > 0)
|
|
42
|
+
yield final;
|
|
43
|
+
else
|
|
44
|
+
final.close();
|
|
45
|
+
}
|
|
46
|
+
export async function countFileChunks(input, parser, options) {
|
|
47
|
+
let total = 0;
|
|
48
|
+
if (options.strict === !0) {
|
|
49
|
+
for await (using batch of parseFileChunks(input, parser, options.limit))
|
|
50
|
+
total += batch.rowCount;
|
|
51
|
+
return total;
|
|
52
|
+
}
|
|
53
|
+
if (options.limit !== 0)
|
|
54
|
+
for await (const chunk of input.chunks()) {
|
|
55
|
+
total += parser.writeCount(chunk);
|
|
56
|
+
if (total === options.limit)
|
|
57
|
+
break;
|
|
58
|
+
}
|
|
59
|
+
return total + parser.endCount();
|
|
60
|
+
}
|
|
61
|
+
export function createPreparedParser(options, columns, program) {
|
|
28
62
|
if (options.columns !== columns)
|
|
29
63
|
normalizeColumns(options.columns);
|
|
30
|
-
|
|
31
|
-
|
|
64
|
+
return NativeCsvParser[createSchemaParser]({
|
|
65
|
+
encoding: options.encoding,
|
|
66
|
+
delimiter: options.delimiter,
|
|
67
|
+
fixedColumns: options.fixedColumns,
|
|
68
|
+
strict: options.strict,
|
|
69
|
+
skipRows: options.skipRows,
|
|
70
|
+
limit: options.limit,
|
|
71
|
+
expectedHeaders: options.expectedHeaders,
|
|
72
|
+
requireHeader: options.requireHeader,
|
|
73
|
+
minDataRows: options.minDataRows,
|
|
74
|
+
columns,
|
|
75
|
+
filterProgram: program
|
|
76
|
+
});
|
|
32
77
|
}
|
|
33
78
|
export function prepareNativeParser(handle, columns, program) {
|
|
34
79
|
const normalizedColumns = normalizeColumns(columns);
|
|
35
80
|
const hasProjection = columns !== void 0;
|
|
36
|
-
const columnCount = BigInt(
|
|
81
|
+
const columnCount = BigInt(normalizedColumns.length);
|
|
37
82
|
const normalized = program === void 0 ? normalizeNativeFilters(void 0) : normalizeNativeFilterProgram(program);
|
|
38
83
|
const filterCount = BigInt(normalized.filterCount);
|
|
39
84
|
const valueCount = BigInt(normalized.valueCount);
|
|
@@ -49,85 +94,68 @@ export function prepareNativeParser(handle, columns, program) {
|
|
|
49
94
|
}
|
|
50
95
|
};
|
|
51
96
|
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
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
|
-
}
|
|
97
|
+
export function validateRowSelection({ skipRows, limit }) {
|
|
98
|
+
if (skipRows !== void 0 && (!Number.isSafeInteger(skipRows) || skipRows < 0))
|
|
99
|
+
throw RangeError("skipRows must be a non-negative safe integer");
|
|
100
|
+
if (limit !== void 0 && (!Number.isSafeInteger(limit) || limit < 0))
|
|
101
|
+
throw RangeError("limit must be a non-negative safe integer");
|
|
108
102
|
}
|
|
109
|
-
function
|
|
103
|
+
export function setRowSelection(handle, options, captureHeader = !1) {
|
|
104
|
+
if (options.skipRows !== void 0 || options.limit !== void 0)
|
|
105
|
+
native.symbols.csv_parser_set_row_selection(handle, BigInt(options.skipRows ?? 0), options.limit === void 0 ? 0xffffffffffffffffn : BigInt(options.limit), captureHeader);
|
|
106
|
+
}
|
|
107
|
+
function createNativeCsvParser(options, prepared = !1) {
|
|
108
|
+
const { skipRows, limit } = options;
|
|
109
|
+
validateRowSelection({ skipRows, limit });
|
|
110
110
|
const delimiter = options.delimiter ?? ",";
|
|
111
111
|
if (delimiter === "auto")
|
|
112
112
|
throw Error("automatic delimiter detection is only supported by file APIs");
|
|
113
113
|
if (delimiter.length !== 1)
|
|
114
114
|
throw Error("delimiter must be one character");
|
|
115
|
-
const
|
|
115
|
+
const encoding = encodingCode(options.encoding);
|
|
116
|
+
const handle = prepared ? native.symbols.csv_parser_create_prepared(encoding, delimiter.charCodeAt(0), options.fixedColumns ?? 0) : native.symbols.csv_parser_create(encoding, delimiter.charCodeAt(0));
|
|
116
117
|
if (handle === null)
|
|
117
118
|
throw Error("failed to create native CSV parser");
|
|
119
|
+
setRowSelection(handle, { skipRows, limit });
|
|
118
120
|
return handle;
|
|
119
121
|
}
|
|
122
|
+
const createSchemaParser = Symbol("createSchemaParser");
|
|
120
123
|
|
|
121
124
|
export class NativeCsvParser {
|
|
122
125
|
#handle;
|
|
123
126
|
#strict;
|
|
124
127
|
#fixedColumns;
|
|
128
|
+
#writer;
|
|
129
|
+
#validator;
|
|
130
|
+
#captureSkippedHeader = !1;
|
|
131
|
+
static [createSchemaParser](options) {
|
|
132
|
+
const validator = strictSchemaValidator(options);
|
|
133
|
+
const parser = new NativeCsvParser(options);
|
|
134
|
+
parser.#validator = validator;
|
|
135
|
+
if (validator !== void 0 && (options.skipRows ?? 0) > 0) {
|
|
136
|
+
parser.#captureSkippedHeader = !0;
|
|
137
|
+
setRowSelection(parser.#requireHandle(), options, !0);
|
|
138
|
+
}
|
|
139
|
+
return parser;
|
|
140
|
+
}
|
|
125
141
|
constructor(options = {}) {
|
|
126
142
|
rejectSelectedColumns(options);
|
|
127
143
|
this.#strict = options.strict === !0;
|
|
128
|
-
normalizeColumns(options.columns);
|
|
129
144
|
this.#fixedColumns = normalizeFixedColumnsCount(options.fixedColumns, "fixed column count");
|
|
130
|
-
const
|
|
145
|
+
const { columns, filterProgram } = options;
|
|
146
|
+
if (filterProgram !== void 0 && (options.equalsFilter !== void 0 || options.filters !== void 0))
|
|
147
|
+
throw Error("filterProgram cannot be combined with equalsFilter or filters");
|
|
148
|
+
const program = filterProgram === void 0 ? projectionFilterProgram(options) : filterProgram;
|
|
149
|
+
if (this.#strict && (columns !== void 0 || program !== void 0))
|
|
150
|
+
throw Error("strict CSV validation is not supported for projected batches or filters");
|
|
151
|
+
const prepared = columns !== void 0 || program !== void 0;
|
|
152
|
+
const handle = createNativeCsvParser(options, prepared);
|
|
153
|
+
try {
|
|
154
|
+
this.#writer = prepared ? prepareNativeParser(handle, columns, program) : void 0;
|
|
155
|
+
} catch (error) {
|
|
156
|
+
destroyNativeCsvParser(handle);
|
|
157
|
+
throw error;
|
|
158
|
+
}
|
|
131
159
|
this.#handle = handle;
|
|
132
160
|
nativeCsvParserFinalizer.register(this, handle, this);
|
|
133
161
|
}
|
|
@@ -143,29 +171,12 @@ export class NativeCsvParser {
|
|
|
143
171
|
}
|
|
144
172
|
}
|
|
145
173
|
writeBatch(chunk, final = !1) {
|
|
146
|
-
if (chunk.byteLength === 0 && final)
|
|
147
|
-
return this.endBatch();
|
|
148
174
|
const handle = this.#requireHandle();
|
|
149
175
|
const input = normalizeChunk(chunk);
|
|
150
|
-
const batch = this.#strict && this.#fixedColumns !== void 0 ? native.symbols.csv_parser_write_strict_fixed_batch(handle, input, input, final, this.#fixedColumns) : this.#strict ? native.symbols.csv_parser_write_strict_batch(handle, input, input, final) : this.#fixedColumns !== void 0 ? native.symbols.csv_parser_write_fixed_batch(handle, input, input, final, this.#fixedColumns) : native.symbols.csv_parser_write_batch(handle, input, input, final);
|
|
176
|
+
const batch = this.#writer !== void 0 ? this.#writer.writeBatch(input, final) : this.#strict && this.#fixedColumns !== void 0 ? native.symbols.csv_parser_write_strict_fixed_batch(handle, input, input, final, this.#fixedColumns) : this.#strict ? native.symbols.csv_parser_write_strict_batch(handle, input, input, final) : this.#fixedColumns !== void 0 ? native.symbols.csv_parser_write_fixed_batch(handle, input, input, final, this.#fixedColumns) : native.symbols.csv_parser_write_batch(handle, input, input, final);
|
|
151
177
|
if (batch === null)
|
|
152
178
|
throw Error(`native CSV parser failed: ${this.#lastError()}`);
|
|
153
|
-
return
|
|
154
|
-
}
|
|
155
|
-
writeProjectedBatch(chunk, options = {}, final = !1) {
|
|
156
|
-
this.#rejectStrictUnsupported("projected batches");
|
|
157
|
-
this.#validateProjectionOptions(options);
|
|
158
|
-
if (chunk.byteLength === 0 && final)
|
|
159
|
-
return this.endProjectedBatch(options);
|
|
160
|
-
const handle = this.#requireHandle();
|
|
161
|
-
const input = normalizeChunk(chunk);
|
|
162
|
-
const columns = normalizeColumns(options.columns);
|
|
163
|
-
const filter = normalizeEqualsFilter(options.equalsFilter);
|
|
164
|
-
const filters = normalizeNativeFilters(options.filters);
|
|
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));
|
|
166
|
-
if (batch === null)
|
|
167
|
-
throw Error(`native CSV parser failed: ${this.#lastError()}`);
|
|
168
|
-
return new NativeCsvBatch(batch);
|
|
179
|
+
return this.#validatedBatch(batch, final);
|
|
169
180
|
}
|
|
170
181
|
end() {
|
|
171
182
|
const batch = this.endBatch();
|
|
@@ -176,144 +187,24 @@ export class NativeCsvParser {
|
|
|
176
187
|
}
|
|
177
188
|
}
|
|
178
189
|
endBatch() {
|
|
179
|
-
|
|
180
|
-
if (batch === null)
|
|
181
|
-
throw Error(`native CSV parser failed: ${this.#lastError()}`);
|
|
182
|
-
return new NativeCsvBatch(batch);
|
|
183
|
-
}
|
|
184
|
-
endProjectedBatch(options = {}) {
|
|
185
|
-
this.#rejectStrictUnsupported("projected batches");
|
|
186
|
-
this.#validateProjectionOptions(options);
|
|
187
|
-
const columns = normalizeColumns(options.columns);
|
|
188
|
-
const filter = normalizeEqualsFilter(options.equalsFilter);
|
|
189
|
-
const filters = normalizeNativeFilters(options.filters);
|
|
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));
|
|
191
|
-
if (batch === null)
|
|
192
|
-
throw Error(`native CSV parser failed: ${this.#lastError()}`);
|
|
193
|
-
return new NativeCsvBatch(batch);
|
|
194
|
-
}
|
|
195
|
-
writeProjectedBatchWhere(chunk, columns, program, final = !1) {
|
|
196
|
-
this.#rejectStrictUnsupported("projected batches");
|
|
197
|
-
if (chunk.byteLength === 0 && final)
|
|
198
|
-
return this.endProjectedBatchWhere(columns, program);
|
|
199
|
-
const handle = this.#requireHandle();
|
|
200
|
-
const input = normalizeChunk(chunk);
|
|
201
|
-
const normalizedColumns = normalizeColumns(columns);
|
|
202
|
-
const normalized = normalizeNativeFilterProgram(program);
|
|
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));
|
|
204
|
-
if (batch === null)
|
|
205
|
-
throw Error(`native CSV parser failed: ${this.#lastError()}`);
|
|
206
|
-
return new NativeCsvBatch(batch);
|
|
207
|
-
}
|
|
208
|
-
endProjectedBatchWhere(columns, program) {
|
|
209
|
-
this.#rejectStrictUnsupported("projected batches");
|
|
210
|
-
const normalizedColumns = normalizeColumns(columns);
|
|
211
|
-
const normalized = normalizeNativeFilterProgram(program);
|
|
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));
|
|
213
|
-
if (batch === null)
|
|
214
|
-
throw Error(`native CSV parser failed: ${this.#lastError()}`);
|
|
215
|
-
return new NativeCsvBatch(batch);
|
|
190
|
+
return this.writeBatch(EMPTY_BUFFER, !0);
|
|
216
191
|
}
|
|
217
192
|
writeCount(chunk, final = !1) {
|
|
218
|
-
this.#
|
|
219
|
-
|
|
193
|
+
if (this.#strict)
|
|
194
|
+
throw Error("strict CSV validation is not supported for count");
|
|
195
|
+
if (this.#writer === void 0 && this.#fixedColumns !== void 0) {
|
|
220
196
|
using batch = this.writeBatch(chunk, final);
|
|
221
197
|
return batch.rowCount;
|
|
222
198
|
}
|
|
223
|
-
if (chunk.byteLength === 0 && final)
|
|
224
|
-
return this.endCount();
|
|
225
199
|
const handle = this.#requireHandle();
|
|
226
200
|
const input = normalizeChunk(chunk);
|
|
227
|
-
return u64ToSafeNumber(native.symbols.csv_parser_write_count(handle, input, input, final), "CSV parsed row count");
|
|
201
|
+
return u64ToSafeNumber(this.#writer === void 0 ? native.symbols.csv_parser_write_count(handle, input, input, final) : this.#writer.writeCount(input, final), "CSV parsed row count");
|
|
228
202
|
}
|
|
229
203
|
endCount() {
|
|
230
|
-
this
|
|
231
|
-
if (this.#fixedColumns !== void 0) {
|
|
232
|
-
using batch = this.endBatch();
|
|
233
|
-
return batch.rowCount;
|
|
234
|
-
}
|
|
235
|
-
return u64ToSafeNumber(native.symbols.csv_parser_finish_count(this.#requireHandle()), "CSV parsed row count");
|
|
236
|
-
}
|
|
237
|
-
writeCountWhereEquals(chunk, filter, final = !1) {
|
|
238
|
-
this.#rejectStrictUnsupported("count filters");
|
|
239
|
-
if (chunk.byteLength === 0 && final)
|
|
240
|
-
return this.endCountWhereEquals(filter);
|
|
241
|
-
const handle = this.#requireHandle();
|
|
242
|
-
const input = normalizeChunk(chunk);
|
|
243
|
-
const normalized = normalizeEqualsFilter(filter);
|
|
244
|
-
return u64ToSafeNumber(native.symbols.csv_parser_write_count_where_equals(handle, input, input, final, normalized.column, normalized.value, normalized.value), "CSV filtered row count");
|
|
245
|
-
}
|
|
246
|
-
endCountWhereEquals(filter) {
|
|
247
|
-
this.#rejectStrictUnsupported("count filters");
|
|
248
|
-
const normalized = normalizeEqualsFilter(filter);
|
|
249
|
-
return u64ToSafeNumber(native.symbols.csv_parser_finish_count_where_equals(this.#requireHandle(), normalized.column, normalized.value, normalized.value), "CSV filtered row count");
|
|
250
|
-
}
|
|
251
|
-
writeCountWhereIn(chunk, filter, final = !1) {
|
|
252
|
-
this.#rejectStrictUnsupported("count filters");
|
|
253
|
-
if (chunk.byteLength === 0 && final)
|
|
254
|
-
return this.endCountWhereIn(filter);
|
|
255
|
-
const normalized = normalizeInFilter(filter);
|
|
256
|
-
const handle = this.#requireHandle();
|
|
257
|
-
const input = normalizeChunk(chunk);
|
|
258
|
-
return u64ToSafeNumber(native.symbols.csv_parser_write_count_where_in(handle, input, input, final, normalized.column, normalized.valuesData, normalized.valuesData, normalized.offsets, BigInt(normalized.valueCount)), "CSV filtered row count");
|
|
259
|
-
}
|
|
260
|
-
endCountWhereIn(filter) {
|
|
261
|
-
this.#rejectStrictUnsupported("count filters");
|
|
262
|
-
const normalized = normalizeInFilter(filter);
|
|
263
|
-
return u64ToSafeNumber(native.symbols.csv_parser_finish_count_where_in(this.#requireHandle(), normalized.column, normalized.valuesData, normalized.valuesData, normalized.offsets, BigInt(normalized.valueCount)), "CSV filtered row count");
|
|
264
|
-
}
|
|
265
|
-
writeCountWhereStartsWith(chunk, filter, final = !1) {
|
|
266
|
-
this.#rejectStrictUnsupported("count filters");
|
|
267
|
-
if (chunk.byteLength === 0 && final)
|
|
268
|
-
return this.endCountWhereStartsWith(filter);
|
|
269
|
-
const normalized = normalizeStartsWithFilter(filter);
|
|
270
|
-
const handle = this.#requireHandle();
|
|
271
|
-
const input = normalizeChunk(chunk);
|
|
272
|
-
return u64ToSafeNumber(native.symbols.csv_parser_write_count_where_starts_with(handle, input, input, final, normalized.column, normalized.value, normalized.value), "CSV filtered row count");
|
|
273
|
-
}
|
|
274
|
-
endCountWhereStartsWith(filter) {
|
|
275
|
-
this.#rejectStrictUnsupported("count filters");
|
|
276
|
-
const normalized = normalizeStartsWithFilter(filter);
|
|
277
|
-
return u64ToSafeNumber(native.symbols.csv_parser_finish_count_where_starts_with(this.#requireHandle(), normalized.column, normalized.value, normalized.value), "CSV filtered row count");
|
|
278
|
-
}
|
|
279
|
-
writeCountWhereAll(chunk, filters, final = !1) {
|
|
280
|
-
this.#rejectStrictUnsupported("count filters");
|
|
281
|
-
if (chunk.byteLength === 0 && final)
|
|
282
|
-
return this.endCountWhereAll(filters);
|
|
283
|
-
const normalized = normalizeNativeFilters(filters);
|
|
284
|
-
const handle = this.#requireHandle();
|
|
285
|
-
const input = normalizeChunk(chunk);
|
|
286
|
-
const count = native.symbols.csv_parser_write_count_where_all(handle, input, input, final, normalized.descriptors, BigInt(normalized.filterCount), normalized.valuesData, normalized.valuesData, normalized.valueOffsets, BigInt(normalized.valueCount));
|
|
287
|
-
this.#throwIfNativeError();
|
|
288
|
-
return u64ToSafeNumber(count, "CSV filtered row count");
|
|
289
|
-
}
|
|
290
|
-
endCountWhereAll(filters) {
|
|
291
|
-
this.#rejectStrictUnsupported("count filters");
|
|
292
|
-
const normalized = normalizeNativeFilters(filters);
|
|
293
|
-
const count = native.symbols.csv_parser_finish_count_where_all(this.#requireHandle(), normalized.descriptors, BigInt(normalized.filterCount), normalized.valuesData, normalized.valuesData, normalized.valueOffsets, BigInt(normalized.valueCount));
|
|
294
|
-
this.#throwIfNativeError();
|
|
295
|
-
return u64ToSafeNumber(count, "CSV filtered row count");
|
|
296
|
-
}
|
|
297
|
-
writeCountWhere(chunk, program, final = !1) {
|
|
298
|
-
this.#rejectStrictUnsupported("count filters");
|
|
299
|
-
if (chunk.byteLength === 0 && final)
|
|
300
|
-
return this.endCountWhere(program);
|
|
301
|
-
const normalized = normalizeNativeFilterProgram(program);
|
|
302
|
-
const handle = this.#requireHandle();
|
|
303
|
-
const input = normalizeChunk(chunk);
|
|
304
|
-
const count = native.symbols.csv_parser_write_count_where_all(handle, input, input, final, normalized.descriptors, BigInt(normalized.filterCount), normalized.valuesData, normalized.valuesData, normalized.valueOffsets, BigInt(normalized.valueCount));
|
|
305
|
-
this.#throwIfNativeError();
|
|
306
|
-
return u64ToSafeNumber(count, "CSV filtered row count");
|
|
307
|
-
}
|
|
308
|
-
endCountWhere(program) {
|
|
309
|
-
this.#rejectStrictUnsupported("count filters");
|
|
310
|
-
const normalized = normalizeNativeFilterProgram(program);
|
|
311
|
-
const count = native.symbols.csv_parser_finish_count_where_all(this.#requireHandle(), normalized.descriptors, BigInt(normalized.filterCount), normalized.valuesData, normalized.valuesData, normalized.valueOffsets, BigInt(normalized.valueCount));
|
|
312
|
-
this.#throwIfNativeError();
|
|
313
|
-
return u64ToSafeNumber(count, "CSV filtered row count");
|
|
204
|
+
return this.writeCount(EMPTY_BUFFER, !0);
|
|
314
205
|
}
|
|
315
206
|
reset() {
|
|
316
|
-
native.symbols.
|
|
207
|
+
native.symbols.csv_parser_reset_stream(this.#requireHandle());
|
|
317
208
|
}
|
|
318
209
|
close() {
|
|
319
210
|
const handle = this.#handle;
|
|
@@ -334,18 +225,28 @@ export class NativeCsvParser {
|
|
|
334
225
|
#lastError() {
|
|
335
226
|
return native.symbols.csv_parser_last_error(this.#requireHandle()) ?? "native parser error unavailable";
|
|
336
227
|
}
|
|
337
|
-
#
|
|
338
|
-
const
|
|
339
|
-
if (
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
228
|
+
#validatedBatch(pointer, final) {
|
|
229
|
+
const batch = new NativeCsvBatch(pointer);
|
|
230
|
+
if (this.#validator === void 0)
|
|
231
|
+
return batch;
|
|
232
|
+
try {
|
|
233
|
+
const handle = this.#requireHandle();
|
|
234
|
+
if (this.#captureSkippedHeader) {
|
|
235
|
+
const header = native.symbols.csv_parser_take_skipped_header(handle);
|
|
236
|
+
if (header !== null) {
|
|
237
|
+
using first = new NativeCsvBatch(header);
|
|
238
|
+
this.#validator.validateBatch(first);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
this.#validator.validateBatch(batch);
|
|
242
|
+
if (final) {
|
|
243
|
+
const skipped = this.#captureSkippedHeader ? u64ToSafeNumber(native.symbols.csv_parser_skipped_rows(handle), "CSV skipped row count") : 0;
|
|
244
|
+
this.#validator.finish(Math.max(0, skipped - 1));
|
|
245
|
+
}
|
|
246
|
+
return batch;
|
|
247
|
+
} catch (error) {
|
|
248
|
+
batch.close();
|
|
249
|
+
throw error;
|
|
250
|
+
}
|
|
350
251
|
}
|
|
351
252
|
}
|
package/dist/strict-schema.d.ts
CHANGED
|
@@ -7,7 +7,7 @@ export declare class CsvStrictSchemaValidator {
|
|
|
7
7
|
get enabled(): boolean;
|
|
8
8
|
validateRows(rows: readonly CsvRow[]): void;
|
|
9
9
|
validateBatch(batch: NativeCsvBatch): void;
|
|
10
|
-
finish(): void;
|
|
10
|
+
finish(skippedDataRows?: number): void;
|
|
11
11
|
}
|
|
12
12
|
export declare function strictSchemaValidator(options: StrictSchemaOptions): CsvStrictSchemaValidator | undefined;
|
|
13
13
|
export declare function rejectStrictSchemaUnsupported(options: StrictSchemaOptions, operation: string): void;
|
package/dist/strict-schema.js
CHANGED
|
@@ -24,14 +24,15 @@ export class CsvStrictSchemaValidator {
|
|
|
24
24
|
for (let rowIndex = 0;rowIndex < rowCount; ++rowIndex)
|
|
25
25
|
this.#validateHeaderFromBatch(batch, rowIndex);
|
|
26
26
|
}
|
|
27
|
-
finish() {
|
|
27
|
+
finish(skippedDataRows = 0) {
|
|
28
28
|
if (!this.enabled)
|
|
29
29
|
return;
|
|
30
30
|
if (!this.#seenHeader && (this.#options.requireHeader === !0 || this.#options.expectedHeaders !== void 0))
|
|
31
31
|
throw Error("strict CSV schema error: missing header row");
|
|
32
32
|
const minDataRows = this.#options.minDataRows;
|
|
33
|
-
|
|
34
|
-
|
|
33
|
+
const dataRows = this.#dataRows + skippedDataRows;
|
|
34
|
+
if (minDataRows !== void 0 && dataRows < minDataRows)
|
|
35
|
+
throw Error(`strict CSV schema error: expected at least ${String(minDataRows)} data row(s), got ${String(dataRows)}`);
|
|
35
36
|
}
|
|
36
37
|
#validateRow(row) {
|
|
37
38
|
if (!this.#seenHeader) {
|
package/dist/types.d.ts
CHANGED
|
@@ -189,6 +189,12 @@ export interface CsvShard {
|
|
|
189
189
|
/** Inclusive end byte offset in the file. */
|
|
190
190
|
end: number;
|
|
191
191
|
}
|
|
192
|
+
interface CsvRowSelectionOptions {
|
|
193
|
+
/** Number of initial input records to skip, including any header, before filtering. Defaults to zero. */
|
|
194
|
+
readonly skipRows?: number;
|
|
195
|
+
/** Maximum number of matching rows to emit or count across the entire input. Defaults to unlimited. */
|
|
196
|
+
readonly limit?: number;
|
|
197
|
+
}
|
|
192
198
|
/**
|
|
193
199
|
* Parser configuration shared by in-memory parsing and file APIs.
|
|
194
200
|
*
|
|
@@ -199,7 +205,7 @@ export interface CsvShard {
|
|
|
199
205
|
* @typeParam TEncoding - Defaults to the native parser's labels. File and buffer
|
|
200
206
|
* parsing accept the full {@link CsvEncoding} set.
|
|
201
207
|
*/
|
|
202
|
-
export interface CsvParserOptions<TEncoding extends CsvFileEncoding = CsvNativeEncoding> {
|
|
208
|
+
export interface CsvParserOptions<TEncoding extends CsvFileEncoding = CsvNativeEncoding> extends CsvRowSelectionOptions {
|
|
203
209
|
/** Input field encoding used when decoding bytes into JavaScript strings. Defaults to `'utf8'`. */
|
|
204
210
|
readonly encoding?: TEncoding;
|
|
205
211
|
/** Field separator. Defaults to comma. File parsing also accepts `'auto'`. */
|
|
@@ -237,7 +243,7 @@ export interface CsvFileOptions extends CsvParserOptions<CsvFileEncoding> {
|
|
|
237
243
|
* Compression is excluded because compressed byte streams cannot be split into
|
|
238
244
|
* independently readable file ranges.
|
|
239
245
|
*/
|
|
240
|
-
export type CsvShardingOptions = Omit<CsvFileOptions, 'compression' | 'encoding'> & {
|
|
246
|
+
export type CsvShardingOptions = Omit<CsvFileOptions, 'compression' | 'encoding' | keyof CsvRowSelectionOptions> & {
|
|
241
247
|
/** Compression is unsupported for byte-offset sharding. */
|
|
242
248
|
readonly compression?: never;
|
|
243
249
|
/** Sharding scans raw file bytes and supports native encodings only. */
|
|
@@ -290,7 +296,7 @@ export interface CsvRegexFilter {
|
|
|
290
296
|
*
|
|
291
297
|
* High-level row and count APIs normally use the opaque `CsvFilter` returned by
|
|
292
298
|
* the filter factories instead. High-level options copy operands at invocation
|
|
293
|
-
* time
|
|
299
|
+
* time. The manual parser captures its operands at construction.
|
|
294
300
|
*/
|
|
295
301
|
export type CsvNativeFilter = CsvEqualsFilter | CsvInFilter | CsvNotInFilter | CsvNotEqualsFilter | CsvRegexFilter | CsvStartsWithFilter;
|
|
296
302
|
/**
|
|
@@ -302,11 +308,37 @@ export type CsvNativeFilter = CsvEqualsFilter | CsvInFilter | CsvNotInFilter | C
|
|
|
302
308
|
export interface CsvNativeProjectionOptions {
|
|
303
309
|
/** Physical columns to emit, in output order. */
|
|
304
310
|
readonly columns?: CsvColumns;
|
|
305
|
-
/** One equality predicate
|
|
311
|
+
/** One equality predicate on a physical column. */
|
|
306
312
|
readonly equalsFilter?: CsvEqualsFilter;
|
|
307
313
|
/** Native predicates combined for the operation. */
|
|
308
314
|
readonly filters?: readonly CsvNativeFilter[];
|
|
309
315
|
}
|
|
316
|
+
/**
|
|
317
|
+
* Boolean operation in a postfix filter program accepted by `NativeCsvParser`.
|
|
318
|
+
*
|
|
319
|
+
* Predicates push results onto a stack. An operator consumes the most recent
|
|
320
|
+
* results and pushes one combined result. Missing columns produce an unknown
|
|
321
|
+
* result, which remains unknown under `not` and does not select a row by itself.
|
|
322
|
+
*/
|
|
323
|
+
export interface CsvNativeFilterOperator {
|
|
324
|
+
/** Number of preceding stack results to consume; positive, and exactly one for `not`. */
|
|
325
|
+
readonly operandCount: number;
|
|
326
|
+
/** Combine operands with AND, OR, or unary negation. */
|
|
327
|
+
readonly operator: 'all' | 'any' | 'not';
|
|
328
|
+
}
|
|
329
|
+
/**
|
|
330
|
+
* Predicate or boolean operator in a low-level postfix filter program.
|
|
331
|
+
*
|
|
332
|
+
* Place predicates before the operators that consume them. A complete program
|
|
333
|
+
* must leave exactly one result on the stack and contain at most 4096 entries.
|
|
334
|
+
* Most applications can use `csv.column()` and its boolean combinators instead.
|
|
335
|
+
*/
|
|
336
|
+
export type CsvNativeFilterProgramEntry = CsvNativeFilter | CsvNativeFilterOperator;
|
|
337
|
+
/** Projection and filter settings captured when a manual parser is constructed. */
|
|
338
|
+
export interface NativeCsvParserOptions extends CsvParserOptions, CsvNativeProjectionOptions {
|
|
339
|
+
/** Postfix Boolean program. Supply only one of `filterProgram`, `filters`, or `equalsFilter`. */
|
|
340
|
+
readonly filterProgram?: readonly CsvNativeFilterProgramEntry[];
|
|
341
|
+
}
|
|
310
342
|
/** Common file options for the high-level `csv` API. */
|
|
311
343
|
export interface CsvApiFileOptions extends CsvFileOptions {
|
|
312
344
|
/** Opaque filter created by `csv.column()`, `csv.all()`, `csv.any()`, or `csv.not()`. */
|
|
@@ -318,7 +350,7 @@ export interface CsvColumnSelection<TColumns extends CsvColumns | undefined> {
|
|
|
318
350
|
readonly columns?: TColumns;
|
|
319
351
|
}
|
|
320
352
|
/** Shared operation properties have declarations that editors can use for hover documentation. */
|
|
321
|
-
interface CsvOperationOptions<TColumns extends CsvColumns | undefined> extends CsvColumnSelection<TColumns
|
|
353
|
+
interface CsvOperationOptions<TColumns extends CsvColumns | undefined> extends CsvColumnSelection<TColumns>, CsvRowSelectionOptions {
|
|
322
354
|
/** Field separator. Defaults to comma. File parsing also accepts `'auto'`. */
|
|
323
355
|
readonly delimiter?: CsvDelimiter;
|
|
324
356
|
/** Require exactly this many fields per row; malformed rows fail instead of being padded or truncated. */
|
package/dist/zip-reader.d.ts
CHANGED
|
@@ -3,9 +3,11 @@ export declare class NativeZipReader {
|
|
|
3
3
|
#private;
|
|
4
4
|
constructor(path: string, options: CsvZipCompression);
|
|
5
5
|
get status(): number;
|
|
6
|
+
get entryName(): string;
|
|
6
7
|
enableReadAhead(chunkSize: number): void;
|
|
7
8
|
read(output: Uint8Array): number;
|
|
8
9
|
lastError(): string;
|
|
10
|
+
chunks(outputSize: number, readAhead?: boolean): Generator<Uint8Array, void>;
|
|
9
11
|
close(): void;
|
|
10
12
|
[Symbol.dispose](): void;
|
|
11
13
|
}
|
package/dist/zip-reader.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import {
|
|
2
2
|
native,
|
|
3
|
+
requirePtr,
|
|
4
|
+
toArrayBuffer,
|
|
3
5
|
u64ToSafeNumber
|
|
4
6
|
} from "./native.js";
|
|
5
7
|
const DEFAULT_MAXIMUM_DECOMPRESSED_BYTES = 68719476736;
|
|
@@ -35,6 +37,11 @@ export class NativeZipReader {
|
|
|
35
37
|
get status() {
|
|
36
38
|
return native.symbols.csv_zip_reader_status(this.#requireHandle());
|
|
37
39
|
}
|
|
40
|
+
get entryName() {
|
|
41
|
+
const length = new Uint32Array(1);
|
|
42
|
+
const name = native.symbols.csv_zip_reader_entry_name(this.#requireHandle(), length);
|
|
43
|
+
return Buffer.from(toArrayBuffer(requirePtr(name), 0, length[0])).toString("utf8");
|
|
44
|
+
}
|
|
38
45
|
enableReadAhead(chunkSize) {
|
|
39
46
|
native.symbols.csv_zip_reader_enable_read_ahead(this.#requireHandle(), chunkSize);
|
|
40
47
|
}
|
|
@@ -44,6 +51,29 @@ export class NativeZipReader {
|
|
|
44
51
|
lastError() {
|
|
45
52
|
return native.symbols.csv_zip_reader_last_error(this.#requireHandle())?.toString() ?? "ZIP reader error unavailable";
|
|
46
53
|
}
|
|
54
|
+
*chunks(outputSize, readAhead = !1) {
|
|
55
|
+
const reusableBuffer = readAhead ? new Uint8Array(outputSize) : void 0;
|
|
56
|
+
let startProducer = readAhead;
|
|
57
|
+
while (!0) {
|
|
58
|
+
const output = reusableBuffer ?? new Uint8Array(outputSize);
|
|
59
|
+
const written = this.read(output);
|
|
60
|
+
const status = this.status;
|
|
61
|
+
if (status === ZIP_READER_ERROR)
|
|
62
|
+
throw Error(`native ZIP reader failed: ${this.lastError()}`);
|
|
63
|
+
if (written > output.byteLength)
|
|
64
|
+
throw Error("native ZIP reader returned an invalid output length");
|
|
65
|
+
if (written !== 0)
|
|
66
|
+
yield output.subarray(0, written);
|
|
67
|
+
if (status === ZIP_READER_DONE)
|
|
68
|
+
return;
|
|
69
|
+
if (written === 0)
|
|
70
|
+
throw Error("native ZIP reader made no progress");
|
|
71
|
+
if (startProducer) {
|
|
72
|
+
this.enableReadAhead(outputSize);
|
|
73
|
+
startProducer = !1;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
47
77
|
close() {
|
|
48
78
|
const handle = this.#handle;
|
|
49
79
|
if (handle === null)
|
|
@@ -64,27 +94,7 @@ export class NativeZipReader {
|
|
|
64
94
|
export async function* readZipEntryChunks(path, options, chunkSize, readAhead = !1) {
|
|
65
95
|
const outputSize = positiveUint32(chunkSize, "ZIP output chunk size");
|
|
66
96
|
using reader = new NativeZipReader(path, options);
|
|
67
|
-
|
|
68
|
-
let startProducer = readAhead;
|
|
69
|
-
while (!0) {
|
|
70
|
-
const output = reusableBuffer ?? new Uint8Array(outputSize);
|
|
71
|
-
const written = reader.read(output);
|
|
72
|
-
const status = reader.status;
|
|
73
|
-
if (status === ZIP_READER_ERROR)
|
|
74
|
-
throw Error(`native ZIP reader failed: ${reader.lastError()}`);
|
|
75
|
-
if (written > output.byteLength)
|
|
76
|
-
throw Error("native ZIP reader returned an invalid output length");
|
|
77
|
-
if (written !== 0)
|
|
78
|
-
yield output.subarray(0, written);
|
|
79
|
-
if (status === ZIP_READER_DONE)
|
|
80
|
-
return;
|
|
81
|
-
if (written === 0)
|
|
82
|
-
throw Error("native ZIP reader made no progress");
|
|
83
|
-
if (startProducer) {
|
|
84
|
-
reader.enableReadAhead(outputSize);
|
|
85
|
-
startProducer = !1;
|
|
86
|
-
}
|
|
87
|
-
}
|
|
97
|
+
yield* reader.chunks(outputSize, readAhead);
|
|
88
98
|
}
|
|
89
99
|
function positiveSafeInteger(value, label) {
|
|
90
100
|
if (!Number.isSafeInteger(value) || value <= 0)
|