@konstit/csv 0.6.0 → 0.7.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 +113 -101
- package/THIRD_PARTY_NOTICES.md +27 -0
- package/dist/api.d.ts +84 -57
- package/dist/api.js +211 -40
- package/dist/batch-pipeline.d.ts +10 -0
- package/dist/batch-pipeline.js +83 -0
- package/dist/batches.d.ts +36 -1
- package/dist/batches.js +241 -5
- package/dist/cli-command.d.ts +7 -7
- package/dist/cli-command.js +325 -164
- package/dist/cli-schema.d.ts +51 -21
- package/dist/cli-schema.js +126 -65
- package/dist/cli.js +1 -1
- package/dist/file-stream.d.ts +66 -18
- package/dist/file-stream.js +534 -199
- package/dist/files.d.ts +17 -37
- package/dist/files.js +50 -21
- package/dist/gzip-reader.d.ts +2 -0
- package/dist/gzip-reader.js +37 -0
- package/dist/index.d.ts +3 -3
- package/dist/index.js +2 -2
- package/dist/inspect-file.d.ts +26 -3
- package/dist/inspect-file.js +169 -74
- package/dist/inspect-zip.d.ts +26 -0
- package/dist/inspect-zip.js +59 -0
- package/dist/latin1-rows.d.ts +11 -3
- package/dist/latin1-rows.js +276 -75
- package/dist/materialize-field.d.ts +6 -0
- package/dist/materialize-field.js +9 -0
- package/dist/native.d.ts +118 -20
- package/dist/native.js +53 -23
- package/dist/normalize.d.ts +7 -1
- package/dist/normalize.js +49 -9
- package/dist/options.d.ts +27 -12
- package/dist/parser.d.ts +34 -14
- package/dist/parser.js +296 -102
- package/dist/strict-schema.d.ts +7 -8
- package/dist/strict-schema.js +45 -83
- package/dist/transforms.d.ts +1 -3
- package/dist/transforms.js +17 -16
- package/dist/types.d.ts +192 -75
- package/dist/zip-entry.d.ts +14 -0
- package/dist/zip-entry.js +155 -0
- package/dist/zip-reader.d.ts +14 -3
- package/dist/zip-reader.js +65 -23
- package/package.json +34 -23
- 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 +18 -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/batches.js
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
import { read } from "bun:ffi";
|
|
2
2
|
import { isUtf8 } from "node:buffer";
|
|
3
3
|
import {
|
|
4
|
+
copyLatin1Range,
|
|
4
5
|
decodeMaterializedField,
|
|
5
|
-
|
|
6
|
+
latin1Pair,
|
|
7
|
+
prepareRow,
|
|
8
|
+
RowStorage
|
|
6
9
|
} from "./materialize-field.js";
|
|
7
10
|
import {
|
|
8
11
|
EMPTY_BUFFER,
|
|
@@ -16,6 +19,9 @@ import {
|
|
|
16
19
|
normalizeFilterColumn
|
|
17
20
|
} from "./normalize.js";
|
|
18
21
|
const visitBatchRows = Symbol("visitBatchRows");
|
|
22
|
+
export const mappedBatchRows = Symbol("mappedBatchRows");
|
|
23
|
+
export const utf8BatchSlices = Symbol("utf8BatchSlices");
|
|
24
|
+
export const recycleBatch = Symbol("recycleBatch");
|
|
19
25
|
const WINDOW_BYTES = 1024;
|
|
20
26
|
const MAX_WINDOW_FIELD_BYTES = 64;
|
|
21
27
|
|
|
@@ -69,7 +75,7 @@ export class NativeCsvRowView {
|
|
|
69
75
|
const range = this.range(columnIndex);
|
|
70
76
|
if (range === null)
|
|
71
77
|
return null;
|
|
72
|
-
return this.#data
|
|
78
|
+
return range.end < range.start ? "" : decodeMaterializedField(this.#data, range.start, range.end, "utf8");
|
|
73
79
|
}
|
|
74
80
|
pick(columns) {
|
|
75
81
|
this.#requireActive();
|
|
@@ -83,8 +89,8 @@ export class NativeCsvRowView {
|
|
|
83
89
|
#rowRange(rowIndex) {
|
|
84
90
|
this.#requireActive();
|
|
85
91
|
const maxFieldIndex = this.#fieldOffsets.length - 1;
|
|
86
|
-
const rowStart = offsetAt(this.#rowOffsetWords, rowIndex,
|
|
87
|
-
const rowEnd = offsetAt(this.#rowOffsetWords, rowIndex + 1,
|
|
92
|
+
const rowStart = offsetAt(this.#rowOffsetWords, rowIndex, rowIndex, maxFieldIndex);
|
|
93
|
+
const rowEnd = offsetAt(this.#rowOffsetWords, rowIndex + 1, rowIndex, maxFieldIndex);
|
|
88
94
|
if (rowEnd < rowStart)
|
|
89
95
|
throw RangeError(`row ${rowIndex} offsets are not monotonic`);
|
|
90
96
|
return [rowStart, rowEnd];
|
|
@@ -225,6 +231,168 @@ export class NativeCsvBatch {
|
|
|
225
231
|
}
|
|
226
232
|
return target;
|
|
227
233
|
}
|
|
234
|
+
*[utf8BatchSlices](maxBatchRows, layout) {
|
|
235
|
+
if (layout !== void 0) {
|
|
236
|
+
yield* this.#mappedUtf8Slices(maxBatchRows, layout);
|
|
237
|
+
return;
|
|
238
|
+
}
|
|
239
|
+
const rowCount = this.rowCount;
|
|
240
|
+
const totalFields = this.totalFields;
|
|
241
|
+
const rowOffsetWords = this.#rowOffsetWordsView();
|
|
242
|
+
const fieldOffsetWords = this.#fieldOffsetWordsView();
|
|
243
|
+
const data = this.data();
|
|
244
|
+
const dataLength = data.byteLength;
|
|
245
|
+
let windowStart = 0;
|
|
246
|
+
let windowEnd = 0;
|
|
247
|
+
let text = "";
|
|
248
|
+
let byteIndexed = !0;
|
|
249
|
+
let utf16Offsets;
|
|
250
|
+
const flags = native.symbols.csv_batch_quoted_rows_ptr(this.#requireHandle());
|
|
251
|
+
const quotedRows = flags === null ? void 0 : new Uint8Array(toArrayBuffer(flags, 0, rowCount));
|
|
252
|
+
let fieldStart = offsetAt(rowOffsetWords, 0, 0, totalFields);
|
|
253
|
+
let start = offsetAt(fieldOffsetWords, fieldStart, "field offset", dataLength);
|
|
254
|
+
for (let first = 0;first < rowCount; first += maxBatchRows) {
|
|
255
|
+
const stop = Math.min(first + maxBatchRows, rowCount);
|
|
256
|
+
const rows = Array(stop - first);
|
|
257
|
+
for (let rowIndex = first;rowIndex < stop; ++rowIndex) {
|
|
258
|
+
const fieldEnd = offsetAt(rowOffsetWords, rowIndex + 1, rowIndex, totalFields);
|
|
259
|
+
const row = prepareRow(void 0, fieldEnd - fieldStart);
|
|
260
|
+
const leading = quotedRows?.[rowIndex] ?? 0;
|
|
261
|
+
const trailing = leading * 2;
|
|
262
|
+
for (let fieldIndex = fieldStart;fieldIndex < fieldEnd; ++fieldIndex) {
|
|
263
|
+
const rawEnd = offsetAt(fieldOffsetWords, fieldIndex + 1, "field offset", dataLength);
|
|
264
|
+
const end = rawEnd - trailing;
|
|
265
|
+
start += leading;
|
|
266
|
+
const column = fieldIndex - fieldStart;
|
|
267
|
+
const length = end - start;
|
|
268
|
+
let value;
|
|
269
|
+
if (length > 2 && length <= MAX_WINDOW_FIELD_BYTES) {
|
|
270
|
+
if (start < windowStart || end > windowEnd) {
|
|
271
|
+
windowStart = start;
|
|
272
|
+
windowEnd = utf8WindowEnd(data, start);
|
|
273
|
+
text = decodeMaterializedField(data, start, windowEnd, "utf8");
|
|
274
|
+
byteIndexed = text.length === windowEnd - start;
|
|
275
|
+
if (!byteIndexed) {
|
|
276
|
+
utf16Offsets ??= new Int16Array(WINDOW_BYTES + 1);
|
|
277
|
+
if (!indexUtf16Boundaries(data.subarray(start, windowEnd), utf16Offsets))
|
|
278
|
+
text = "";
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
const from = start - windowStart;
|
|
282
|
+
const to = end - windowStart;
|
|
283
|
+
if (byteIndexed)
|
|
284
|
+
value = text.slice(from, to);
|
|
285
|
+
else if (text.length > 0 && utf16Offsets !== void 0) {
|
|
286
|
+
const first = utf16Offsets[from] ?? -1;
|
|
287
|
+
const last = utf16Offsets[to] ?? -1;
|
|
288
|
+
if (first >= 0 && last >= 0)
|
|
289
|
+
value = text.slice(first, last);
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
row[column] = value ?? decodeMaterializedField(data, start, end, "utf8");
|
|
293
|
+
start = rawEnd;
|
|
294
|
+
}
|
|
295
|
+
rows[rowIndex - first] = row;
|
|
296
|
+
fieldStart = fieldEnd;
|
|
297
|
+
}
|
|
298
|
+
if (stop === rowCount)
|
|
299
|
+
this.close();
|
|
300
|
+
yield rows;
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
*#mappedUtf8Slices(maxBatchRows, { keys, template }) {
|
|
304
|
+
const rowCount = this.rowCount;
|
|
305
|
+
const totalFields = this.totalFields;
|
|
306
|
+
const rowOffsetWords = this.#rowOffsetWordsView();
|
|
307
|
+
const fieldOffsetWords = this.#fieldOffsetWordsView();
|
|
308
|
+
const data = this.data();
|
|
309
|
+
const dataLength = data.byteLength;
|
|
310
|
+
const flags = native.symbols.csv_batch_quoted_rows_ptr(this.#requireHandle());
|
|
311
|
+
const storageFlags = flags === null ? void 0 : new Uint8Array(toArrayBuffer(flags, 0, rowCount));
|
|
312
|
+
let fieldStart = offsetAt(rowOffsetWords, 0, 0, totalFields);
|
|
313
|
+
let start = offsetAt(fieldOffsetWords, fieldStart, "field offset", dataLength);
|
|
314
|
+
for (let first = 0;first < rowCount; first += maxBatchRows) {
|
|
315
|
+
const stop = Math.min(first + maxBatchRows, rowCount);
|
|
316
|
+
const rows = Array(stop - first);
|
|
317
|
+
for (let rowIndex = first;rowIndex < stop; ++rowIndex) {
|
|
318
|
+
const row = template === void 0 ? {} : { ...template };
|
|
319
|
+
const fieldEnd = offsetAt(rowOffsetWords, rowIndex + 1, rowIndex, totalFields);
|
|
320
|
+
const available = Math.min(keys.length, fieldEnd - fieldStart);
|
|
321
|
+
const leading = storageFlags?.[rowIndex] === RowStorage.QuotedSpan ? 1 : 0;
|
|
322
|
+
const trailing = leading * 2;
|
|
323
|
+
let column = 0;
|
|
324
|
+
for (;column < available; ++column) {
|
|
325
|
+
const rawEnd = offsetAt(fieldOffsetWords, fieldStart + column + 1, "field offset", dataLength);
|
|
326
|
+
row[keys[column]] = decodeMaterializedField(data, start + leading, rawEnd - trailing, "utf8");
|
|
327
|
+
start = rawEnd;
|
|
328
|
+
}
|
|
329
|
+
for (;column < keys.length; ++column)
|
|
330
|
+
row[keys[column]] = "";
|
|
331
|
+
if (fieldStart + available < fieldEnd)
|
|
332
|
+
start = offsetAt(fieldOffsetWords, fieldEnd, "field offset", dataLength);
|
|
333
|
+
rows[rowIndex - first] = row;
|
|
334
|
+
fieldStart = fieldEnd;
|
|
335
|
+
}
|
|
336
|
+
if (stop === rowCount)
|
|
337
|
+
this.close();
|
|
338
|
+
yield rows;
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
[mappedBatchRows](layout, encoding) {
|
|
342
|
+
const { keys, template } = layout;
|
|
343
|
+
const rowCount = this.rowCount;
|
|
344
|
+
const totalFields = this.totalFields;
|
|
345
|
+
const rowOffsets = this.#rowOffsetWordsView();
|
|
346
|
+
const fieldOffsets = this.#fieldOffsetWordsView();
|
|
347
|
+
const data = this.data();
|
|
348
|
+
const dataLength = data.byteLength;
|
|
349
|
+
const flags = native.symbols.csv_batch_quoted_rows_ptr(this.#requireHandle());
|
|
350
|
+
const storageFlags = flags === null ? void 0 : new Uint8Array(toArrayBuffer(flags, 0, rowCount));
|
|
351
|
+
const rows = [];
|
|
352
|
+
rows.length = rowCount;
|
|
353
|
+
let fieldStart = offsetAt(rowOffsets, 0, 0, totalFields);
|
|
354
|
+
let start = offsetAt(fieldOffsets, fieldStart, "field offset", dataLength);
|
|
355
|
+
let windowEnd = 0;
|
|
356
|
+
let windowStart = 0;
|
|
357
|
+
let text = "";
|
|
358
|
+
for (let rowIndex = 0;rowIndex < rowCount; ++rowIndex) {
|
|
359
|
+
const row = template === void 0 ? {} : { ...template };
|
|
360
|
+
const fieldEnd = offsetAt(rowOffsets, rowIndex + 1, rowIndex, totalFields);
|
|
361
|
+
const available = Math.min(keys.length, fieldEnd - fieldStart);
|
|
362
|
+
const storage = storageFlags?.[rowIndex];
|
|
363
|
+
const leading = storage === RowStorage.QuotedSpan ? 1 : 0;
|
|
364
|
+
const trailing = leading * 2;
|
|
365
|
+
const fieldEncoding = storage === RowStorage.PackedUtf8 ? "utf8" : encoding;
|
|
366
|
+
let column = 0;
|
|
367
|
+
for (;column < available; ++column) {
|
|
368
|
+
const rawEnd = offsetAt(fieldOffsets, fieldStart + column + 1, "field offset", dataLength);
|
|
369
|
+
const end = rawEnd - trailing;
|
|
370
|
+
start += leading;
|
|
371
|
+
const length = end - start;
|
|
372
|
+
let value;
|
|
373
|
+
if (fieldEncoding === "latin1" && length > 2 && length <= MAX_WINDOW_FIELD_BYTES) {
|
|
374
|
+
if (end > windowEnd) {
|
|
375
|
+
windowStart = start;
|
|
376
|
+
windowEnd = Math.min(start + WINDOW_BYTES, dataLength);
|
|
377
|
+
text = copyLatin1Range(data, windowStart, windowEnd);
|
|
378
|
+
}
|
|
379
|
+
value = text.slice(start - windowStart, end - windowStart);
|
|
380
|
+
} else if (fieldEncoding === "latin1" && length === 2)
|
|
381
|
+
value = latin1Pair(data.readUInt16LE(start));
|
|
382
|
+
else
|
|
383
|
+
value = decodeMaterializedField(data, start, end, fieldEncoding);
|
|
384
|
+
row[keys[column]] = value;
|
|
385
|
+
start = rawEnd;
|
|
386
|
+
}
|
|
387
|
+
for (;column < keys.length; ++column)
|
|
388
|
+
row[keys[column]] = "";
|
|
389
|
+
if (fieldStart + available < fieldEnd)
|
|
390
|
+
start = offsetAt(fieldOffsets, fieldEnd, "field offset", dataLength);
|
|
391
|
+
rows[rowIndex] = row;
|
|
392
|
+
fieldStart = fieldEnd;
|
|
393
|
+
}
|
|
394
|
+
return rows;
|
|
395
|
+
}
|
|
228
396
|
data() {
|
|
229
397
|
if (this.#data !== void 0)
|
|
230
398
|
return this.#data;
|
|
@@ -368,8 +536,14 @@ export class NativeCsvBatch {
|
|
|
368
536
|
return u64ToSafeNumber(native.symbols.csv_batch_count_where_equals(this.#requireHandle(), columnIndex, input, input), "CSV batch filtered row count");
|
|
369
537
|
}
|
|
370
538
|
close() {
|
|
539
|
+
this.#release();
|
|
540
|
+
}
|
|
541
|
+
[recycleBatch](recycle) {
|
|
542
|
+
this.#release(recycle);
|
|
543
|
+
}
|
|
544
|
+
#release(release = native.symbols.csv_batch_destroy) {
|
|
371
545
|
if (this.#handle !== null) {
|
|
372
|
-
|
|
546
|
+
release(this.#handle);
|
|
373
547
|
this.#handle = null;
|
|
374
548
|
this.#data = void 0;
|
|
375
549
|
this.#dataView = void 0;
|
|
@@ -419,6 +593,68 @@ export class NativeCsvBatch {
|
|
|
419
593
|
throw RangeError(`row index out of range: ${endRow}`);
|
|
420
594
|
}
|
|
421
595
|
}
|
|
596
|
+
|
|
597
|
+
export class NativeCsvRawRecordBatch {
|
|
598
|
+
#handle;
|
|
599
|
+
#rowCount;
|
|
600
|
+
#data;
|
|
601
|
+
#byteOffsets;
|
|
602
|
+
#fieldCounts;
|
|
603
|
+
constructor(handle) {
|
|
604
|
+
this.#handle = handle;
|
|
605
|
+
}
|
|
606
|
+
get closed() {
|
|
607
|
+
return this.#handle === null;
|
|
608
|
+
}
|
|
609
|
+
get rowCount() {
|
|
610
|
+
this.#rowCount ??= u64ToSafeNumber(native.symbols.csv_batch_row_count(this.#requireHandle()), "CSV batch row count");
|
|
611
|
+
return this.#rowCount;
|
|
612
|
+
}
|
|
613
|
+
rowBytes(rowIndex) {
|
|
614
|
+
this.#validateRowIndex(rowIndex);
|
|
615
|
+
if (this.#data === void 0) {
|
|
616
|
+
const handle = this.#requireHandle();
|
|
617
|
+
this.#data = new Uint8Array(toArrayBuffer(requirePtr(native.symbols.csv_batch_data_ptr(handle)), 0, u64ToSafeNumber(native.symbols.csv_batch_data_len(handle), "CSV batch data length")));
|
|
618
|
+
}
|
|
619
|
+
this.#byteOffsets ??= new Uint32Array(toArrayBuffer(requirePtr(native.symbols.csv_batch_field_offsets_ptr(this.#requireHandle())), 0, (this.rowCount + 1) * BigUint64Array.BYTES_PER_ELEMENT));
|
|
620
|
+
const data = this.#data;
|
|
621
|
+
const offsets = this.#byteOffsets;
|
|
622
|
+
const start = offsetAt(offsets, rowIndex, "raw row byte offset", data.length);
|
|
623
|
+
const end = offsetAt(offsets, rowIndex + 1, "raw row byte offset", data.length);
|
|
624
|
+
return data.subarray(start, end);
|
|
625
|
+
}
|
|
626
|
+
rowFieldCount(rowIndex) {
|
|
627
|
+
this.#validateRowIndex(rowIndex);
|
|
628
|
+
this.#fieldCounts ??= new Uint32Array(toArrayBuffer(requirePtr(native.symbols.csv_batch_row_offsets_ptr(this.#requireHandle())), 0, (this.rowCount + 1) * BigUint64Array.BYTES_PER_ELEMENT));
|
|
629
|
+
const offsets = this.#fieldCounts;
|
|
630
|
+
const start = offsetAt(offsets, rowIndex, "raw row field offset", Number.MAX_SAFE_INTEGER);
|
|
631
|
+
const end = offsetAt(offsets, rowIndex + 1, "raw row field offset", Number.MAX_SAFE_INTEGER);
|
|
632
|
+
return end - start;
|
|
633
|
+
}
|
|
634
|
+
close() {
|
|
635
|
+
if (this.#handle !== null) {
|
|
636
|
+
native.symbols.csv_batch_destroy(this.#handle);
|
|
637
|
+
this.#handle = null;
|
|
638
|
+
this.#rowCount = void 0;
|
|
639
|
+
this.#data = void 0;
|
|
640
|
+
this.#byteOffsets = void 0;
|
|
641
|
+
this.#fieldCounts = void 0;
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
[Symbol.dispose]() {
|
|
645
|
+
this.close();
|
|
646
|
+
}
|
|
647
|
+
#validateRowIndex(rowIndex) {
|
|
648
|
+
const count = this.rowCount;
|
|
649
|
+
if (!Number.isInteger(rowIndex) || rowIndex < 0 || rowIndex >= count)
|
|
650
|
+
throw RangeError(`row index out of range: ${rowIndex}`);
|
|
651
|
+
}
|
|
652
|
+
#requireHandle() {
|
|
653
|
+
if (this.#handle === null)
|
|
654
|
+
throw Error("native CSV raw-record batch is closed");
|
|
655
|
+
return this.#handle;
|
|
656
|
+
}
|
|
657
|
+
}
|
|
422
658
|
const UINT32_BASE = 4294967296;
|
|
423
659
|
const UINT32_BASE_BIGINT = 0x100000000n;
|
|
424
660
|
const MAX_SAFE_OFFSET_HIGH_WORD = 2097151;
|
package/dist/cli-command.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
export declare function
|
|
7
|
-
export
|
|
1
|
+
import { type HandlerContext } from '@konstit/cli';
|
|
2
|
+
import type { CountArguments, LinesArguments, ProbeArguments } from './cli-schema.js';
|
|
3
|
+
export declare function runProbe(args: ProbeArguments, { stdout, stderr }: HandlerContext): Promise<number>;
|
|
4
|
+
export declare function runCount(args: CountArguments, context: HandlerContext): Promise<void>;
|
|
5
|
+
export declare function runLines(args: LinesArguments, context: HandlerContext): Promise<void>;
|
|
6
|
+
export declare function parseColumnIndex(value: string, label: string): number;
|
|
7
|
+
export declare function formatUsageError(error: unknown, commandPath: readonly string[]): string;
|