@konstit/csv 0.4.3 → 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.
Files changed (64) hide show
  1. package/README.md +50 -28
  2. package/THIRD_PARTY_NOTICES.md +7 -1
  3. package/dist/api.d.ts +39 -249
  4. package/dist/api.js +56 -394
  5. package/dist/batches.d.ts +8 -57
  6. package/dist/batches.js +77 -52
  7. package/dist/cli-command.d.ts +7 -0
  8. package/dist/cli-command.js +315 -0
  9. package/dist/cli-schema.d.ts +127 -0
  10. package/dist/cli-schema.js +234 -0
  11. package/dist/cli.js +2 -604
  12. package/dist/file-stream.d.ts +62 -3
  13. package/dist/file-stream.js +242 -36
  14. package/dist/files.d.ts +11 -11
  15. package/dist/files.js +38 -79
  16. package/dist/filter.d.ts +5 -8
  17. package/dist/format-bytes.d.ts +1 -0
  18. package/dist/format-bytes.js +11 -0
  19. package/dist/index.d.ts +3 -3
  20. package/dist/index.js +2 -14
  21. package/dist/inspect-file.d.ts +22 -0
  22. package/dist/inspect-file.js +138 -0
  23. package/dist/latin1-rows.d.ts +4 -3
  24. package/dist/latin1-rows.js +72 -24
  25. package/dist/materialize-field.d.ts +2 -0
  26. package/dist/materialize-field.js +20 -15
  27. package/dist/native.d.ts +50 -78
  28. package/dist/native.js +36 -112
  29. package/dist/normalize.d.ts +14 -40
  30. package/dist/normalize.js +66 -64
  31. package/dist/options-snapshot.js +3 -2
  32. package/dist/options.d.ts +4 -30
  33. package/dist/options.js +0 -6
  34. package/dist/parser.d.ts +23 -209
  35. package/dist/parser.js +146 -261
  36. package/dist/strict-schema.d.ts +6 -4
  37. package/dist/strict-schema.js +4 -3
  38. package/dist/types.d.ts +119 -221
  39. package/dist/zip-reader.d.ts +13 -1
  40. package/dist/zip-reader.js +36 -17
  41. package/package.json +36 -33
  42. package/prebuilds/darwin-arm64/libcsv_native.dylib +0 -0
  43. package/prebuilds/darwin-x64/libcsv_native.dylib +0 -0
  44. package/prebuilds/linux-x64/libcsv_native.so +0 -0
  45. package/prebuilds/manifest.json +9 -8
  46. package/prebuilds/provenance/darwin-arm64.json +1 -1
  47. package/prebuilds/provenance/darwin-x64.json +1 -1
  48. package/prebuilds/provenance/linux-x64.json +1 -1
  49. package/dist/worker-count.d.ts +0 -34
  50. package/dist/worker-count.js +0 -101
  51. package/dist/worker-factory.d.ts +0 -3
  52. package/dist/worker-factory.js +0 -9
  53. package/dist/worker-filter.d.ts +0 -29
  54. package/dist/worker-filter.js +0 -46
  55. package/dist/worker-module.d.ts +0 -1
  56. package/dist/worker-module.js +0 -4
  57. package/dist/worker-pool.d.ts +0 -93
  58. package/dist/worker-pool.js +0 -298
  59. package/dist/worker-rows.d.ts +0 -36
  60. package/dist/worker-rows.js +0 -127
  61. package/dist/workers/count.worker.d.ts +0 -1
  62. package/dist/workers/count.worker.js +0 -36
  63. package/dist/workers/rows.worker.d.ts +0 -1
  64. package/dist/workers/rows.worker.js +0 -56
package/dist/native.js CHANGED
@@ -19,6 +19,16 @@ export const EMPTY_BUFFER = EMPTY_BUFFER_STORAGE.subarray(0, 0);
19
19
  export const EMPTY_U32 = new Uint32Array(1);
20
20
  export const DEFAULT_CHUNK_SIZE = 1048576;
21
21
  const CSV_SYMBOLS = {
22
+ csv_probe_format: {
23
+ args: [
24
+ "buffer",
25
+ "u32",
26
+ "bool",
27
+ "bool",
28
+ "ptr"
29
+ ],
30
+ returns: "u32"
31
+ },
22
32
  csv_runtime_supports_avx2: {
23
33
  args: [],
24
34
  returns: "int"
@@ -27,11 +37,15 @@ const CSV_SYMBOLS = {
27
37
  args: ["int", "u8"],
28
38
  returns: "ptr"
29
39
  },
40
+ csv_parser_create_prepared: { args: ["int", "u8", "u32"], returns: "ptr" },
41
+ csv_parser_set_row_selection: { args: ["ptr", "u64", "u64", "bool"], returns: "void" },
42
+ csv_parser_take_skipped_header: { args: ["ptr"], returns: "ptr" },
43
+ csv_parser_skipped_rows: { args: ["ptr"], returns: "u64" },
30
44
  csv_parser_destroy: {
31
45
  args: ["ptr"],
32
46
  returns: "void"
33
47
  },
34
- csv_parser_reset: {
48
+ csv_parser_reset_stream: {
35
49
  args: ["ptr"],
36
50
  returns: "void"
37
51
  },
@@ -39,64 +53,18 @@ const CSV_SYMBOLS = {
39
53
  args: ["ptr", "buffer", "buffer_length", "bool"],
40
54
  returns: "ptr"
41
55
  },
42
- csv_parser_finish_batch: {
43
- args: ["ptr"],
44
- returns: "ptr"
45
- },
46
56
  csv_parser_write_strict_batch: {
47
57
  args: ["ptr", "buffer", "buffer_length", "bool"],
48
58
  returns: "ptr"
49
59
  },
50
- csv_parser_finish_strict_batch: {
51
- args: ["ptr"],
52
- returns: "ptr"
53
- },
54
60
  csv_parser_write_fixed_batch: {
55
61
  args: ["ptr", "buffer", "buffer_length", "bool", "u32"],
56
62
  returns: "ptr"
57
63
  },
58
- csv_parser_finish_fixed_batch: {
59
- args: ["ptr", "u32"],
60
- returns: "ptr"
61
- },
62
64
  csv_parser_write_strict_fixed_batch: {
63
65
  args: ["ptr", "buffer", "buffer_length", "bool", "u32"],
64
66
  returns: "ptr"
65
67
  },
66
- csv_parser_finish_strict_fixed_batch: {
67
- args: ["ptr", "u32"],
68
- returns: "ptr"
69
- },
70
- csv_parser_write_projected_batch: {
71
- args: ["ptr", "buffer", "buffer_length", "bool", "bool", "buffer", "u64", "bool", "u32", "buffer", "buffer_length"],
72
- returns: "ptr"
73
- },
74
- csv_parser_finish_projected_batch: {
75
- args: ["ptr", "bool", "buffer", "u64", "bool", "u32", "buffer", "buffer_length"],
76
- returns: "ptr"
77
- },
78
- csv_parser_write_projected_batch_where_all: {
79
- args: [
80
- "ptr",
81
- "buffer",
82
- "buffer_length",
83
- "bool",
84
- "bool",
85
- "buffer",
86
- "u64",
87
- "buffer",
88
- "u64",
89
- "buffer",
90
- "buffer_length",
91
- "buffer",
92
- "u64"
93
- ],
94
- returns: "ptr"
95
- },
96
- csv_parser_finish_projected_batch_where_all: {
97
- args: ["ptr", "bool", "buffer", "u64", "buffer", "u64", "buffer", "buffer_length", "buffer", "u64"],
98
- returns: "ptr"
99
- },
100
68
  csv_batch_destroy: {
101
69
  args: ["ptr"],
102
70
  returns: "void"
@@ -157,42 +125,6 @@ const CSV_SYMBOLS = {
157
125
  args: ["ptr"],
158
126
  returns: "ptr"
159
127
  },
160
- csv_parser_finish_count: {
161
- args: ["ptr"],
162
- returns: "u64"
163
- },
164
- csv_parser_write_count_where_all: {
165
- args: ["ptr", "buffer", "buffer_length", "bool", "buffer", "u64", "buffer", "buffer_length", "buffer", "u64"],
166
- returns: "u64"
167
- },
168
- csv_parser_finish_count_where_all: {
169
- args: ["ptr", "buffer", "u64", "buffer", "buffer_length", "buffer", "u64"],
170
- returns: "u64"
171
- },
172
- csv_parser_write_count_where_equals: {
173
- args: ["ptr", "buffer", "buffer_length", "bool", "u32", "buffer", "buffer_length"],
174
- returns: "u64"
175
- },
176
- csv_parser_finish_count_where_equals: {
177
- args: ["ptr", "u32", "buffer", "buffer_length"],
178
- returns: "u64"
179
- },
180
- csv_parser_write_count_where_in: {
181
- args: ["ptr", "buffer", "buffer_length", "bool", "u32", "buffer", "buffer_length", "buffer", "u64"],
182
- returns: "u64"
183
- },
184
- csv_parser_finish_count_where_in: {
185
- args: ["ptr", "u32", "buffer", "buffer_length", "buffer", "u64"],
186
- returns: "u64"
187
- },
188
- csv_parser_write_count_where_starts_with: {
189
- args: ["ptr", "buffer", "buffer_length", "bool", "u32", "buffer", "buffer_length"],
190
- returns: "u64"
191
- },
192
- csv_parser_finish_count_where_starts_with: {
193
- args: ["ptr", "u32", "buffer", "buffer_length"],
194
- returns: "u64"
195
- },
196
128
  csv_zip_reader_create: {
197
129
  args: ["cstring", "buffer", "buffer_length", "u64", "u32"],
198
130
  returns: "ptr"
@@ -201,6 +133,14 @@ const CSV_SYMBOLS = {
201
133
  args: ["ptr"],
202
134
  returns: "void"
203
135
  },
136
+ csv_zip_reader_entry_name: {
137
+ args: ["ptr", "buffer"],
138
+ returns: "ptr"
139
+ },
140
+ csv_zip_reader_enable_read_ahead: {
141
+ args: ["ptr", "u32"],
142
+ returns: "void"
143
+ },
204
144
  csv_zip_reader_read: {
205
145
  args: ["ptr", "buffer", "buffer_length"],
206
146
  returns: "u64"
@@ -216,11 +156,7 @@ const CSV_SYMBOLS = {
216
156
  csv_parser_last_error: {
217
157
  args: ["ptr"],
218
158
  returns: "cstring"
219
- }
220
- };
221
- const nativeLibraryPath = resolveNativeLibraryPath();
222
- export const native = dlopen(nativeLibraryPath, CSV_SYMBOLS);
223
- const PREPARED_PARSER_SYMBOLS = {
159
+ },
224
160
  csv_parser_prepare: {
225
161
  args: ["ptr", "bool", "buffer", "u64", "buffer", "u64", "buffer", "buffer_length", "buffer", "u64"],
226
162
  returns: "bool"
@@ -232,38 +168,26 @@ const PREPARED_PARSER_SYMBOLS = {
232
168
  csv_parser_write_prepared_count: {
233
169
  args: ["ptr", "buffer", "buffer_length", "bool"],
234
170
  returns: "u64"
235
- }
236
- };
237
- let preparedParserNative;
238
- export function optionalPreparedParserNative() {
239
- if (preparedParserNative === void 0)
240
- try {
241
- preparedParserNative = dlopen(nativeLibraryPath, PREPARED_PARSER_SYMBOLS);
242
- } catch {
243
- preparedParserNative = null;
244
- }
245
- return preparedParserNative;
246
- }
247
- const LATIN1_ROWS_SYMBOLS = {
171
+ },
248
172
  csv_parser_create_latin1_rows: {
249
173
  args: ["u8"],
250
174
  returns: "ptr"
175
+ },
176
+ csv_parser_create_latin1_rows_with_quoted_spans: { args: ["u8"], returns: "ptr" },
177
+ csv_parser_create_latin1_quoted_rows: { args: ["u8", "u32"], returns: "ptr" },
178
+ csv_batch_quoted_rows_ptr: { args: ["ptr"], returns: "ptr" },
179
+ csv_parser_create_latin1_rows_with_fixed_columns: {
180
+ args: ["u8", "u32"],
181
+ returns: "ptr"
251
182
  }
252
183
  };
253
- let latin1RowsNative;
254
- export function optionalLatin1RowsNative() {
255
- if (latin1RowsNative === void 0)
256
- try {
257
- latin1RowsNative = dlopen(nativeLibraryPath, LATIN1_ROWS_SYMBOLS);
258
- } catch {
259
- latin1RowsNative = null;
260
- }
261
- return latin1RowsNative;
184
+ export const native = dlopen(resolveNativeLibraryPath(process.env.CSV_NATIVE_LIBRARY_PATH), CSV_SYMBOLS);
185
+ export function latin1RowsParser(fixedColumns) {
186
+ return (delimiter) => native.symbols.csv_parser_create_latin1_quoted_rows(delimiter, fixedColumns ?? 0);
262
187
  }
263
- function resolveNativeLibraryPath() {
188
+ export function resolveNativeLibraryPath(configuredPath) {
264
189
  const root = dirname(dirname(fileURLToPath(import.meta.url)));
265
190
  const target = `${process.platform}-${process.arch}`;
266
- const configuredPath = process.env.CSV_NATIVE_LIBRARY_PATH;
267
191
  const libraryPath = configuredPath === void 0 ? requireNativeLibraryPath(nativeLibraryCandidates(root, target, suffix)) : requireConfiguredNativeLibraryPath(configuredPath);
268
192
  return resolve(libraryPath);
269
193
  }
@@ -1,59 +1,33 @@
1
- import type { CsvColumns, CsvEncoding, CsvEqualsFilter, CsvFieldValue, CsvInFilter, CsvNativeFilter, CsvNativeProjectionOptions, CsvRegex, CsvStartsWithFilter } from './types.js';
1
+ import type { CsvColumns, CsvEncoding, CsvFieldValue, CsvNativeEncoding, CsvNativeFilter, CsvNativeFilterProgramEntry, CsvNativeProjectionOptions, CsvRegex } from './types.js';
2
+ interface PreparedCsvBufferInput {
3
+ readonly bytes: NodeJS.TypedArray | DataView;
4
+ readonly parserEncoding: 'utf8' | 'latin1';
5
+ }
2
6
  export declare const MAX_COLUMN_INDEX = 2024;
3
7
  export declare const MAX_PROJECTION_LENGTH = 2024;
4
8
  export declare const MAX_FILTER_COUNT = 2024;
5
9
  export declare const MAX_REGEX_FILTER_COUNT = 32;
6
10
  export declare const MAX_FILTER_PROGRAM_LENGTH = 4096;
11
+ export declare function projectionFilterProgram(options: CsvNativeProjectionOptions): readonly CsvNativeFilterProgramEntry[] | undefined;
12
+ export declare function encodingCode(encoding?: CsvNativeEncoding): number;
7
13
  /**
8
- * Boolean operation in a postfix filter program accepted by `NativeCsvParser`.
9
- *
10
- * Predicates push results onto a stack. An operator consumes the most recent
11
- * results and pushes one combined result. Missing columns produce an unknown
12
- * result, which remains unknown under `not` and does not select a row by itself.
13
- */
14
- export interface CsvNativeFilterOperator {
15
- /** Number of preceding stack results to consume; positive, and exactly one for `not`. */
16
- readonly operandCount: number;
17
- /** Combine operands with AND, OR, or unary negation. */
18
- readonly operator: 'all' | 'any' | 'not';
19
- }
20
- /**
21
- * Predicate or boolean operator in a low-level postfix filter program.
22
- *
23
- * Place predicates before the operators that consume them. A complete program
24
- * must leave exactly one result on the stack and contain at most 4096 entries.
25
- * Most applications can use `csv.column()` and its boolean combinators instead.
14
+ * Selects the byte encoding used by the native parser or a decoder for the input.
15
+ * The three Latin-1 labels map bytes 0x80–0x9f directly to Unicode control characters.
26
16
  */
27
- export type CsvNativeFilterProgramEntry = CsvNativeFilter | CsvNativeFilterOperator;
28
- export declare function projectionFilterProgram(options: CsvNativeProjectionOptions): readonly CsvNativeFilterProgramEntry[] | undefined;
29
- export declare function encodingCode(encoding?: CsvEncoding): number;
17
+ export declare function resolveInputEncoding(encoding?: string, delimiter?: string): 'utf8' | 'latin1' | TextDecoder;
18
+ /** Converts a complete buffer to bytes accepted by the native parser. */
19
+ export declare function prepareCsvBufferInput(buffer: NodeJS.TypedArray | DataView, encoding?: CsvEncoding, delimiter?: string): PreparedCsvBufferInput;
30
20
  export declare function normalizeChunk(chunk: NodeJS.TypedArray | DataView): NodeJS.TypedArray | DataView;
31
21
  export declare function normalizeFixedColumnsCount(value: number | undefined, label: string): number | undefined;
22
+ /** Reject stale JavaScript options before a requested projection can silently return all fields. */
23
+ export declare function rejectSelectedColumns(options: object): void;
32
24
  export declare function normalizeColumns(columns: CsvColumns | undefined): Uint32Array;
33
25
  export declare function normalizeColumnIndex(column: number, label: string): number;
34
- export declare function normalizeEqualsFilter(filter: CsvEqualsFilter | undefined): {
35
- enabled: boolean;
36
- column: number;
37
- value: Uint8Array;
38
- valueLength: number;
39
- };
40
26
  export declare function normalizeFilterColumn(column: number): number;
41
27
  export declare function normalizeFilterValue(value: CsvFieldValue): Uint8Array;
42
28
  type CsvRegexInput = Pick<CsvRegex, 'flags' | 'source'>;
43
29
  export declare function normalizeRegex(regex: CsvRegexInput): Uint8Array;
44
30
  export declare function validateRegex(regex: CsvRegexInput): void;
45
- export declare function normalizeInFilter(filter: CsvInFilter): {
46
- column: number;
47
- valuesData: Uint8Array;
48
- valuesDataLength: number;
49
- offsets: Uint32Array;
50
- valueCount: number;
51
- };
52
- export declare function normalizeStartsWithFilter(filter: CsvStartsWithFilter): {
53
- column: number;
54
- value: Uint8Array;
55
- valueLength: number;
56
- };
57
31
  export declare function normalizeNativeFilters(filters: readonly CsvNativeFilter[] | undefined): {
58
32
  descriptors: Uint32Array;
59
33
  filterCount: number;
package/dist/normalize.js CHANGED
@@ -3,6 +3,8 @@ import {
3
3
  EMPTY_U32,
4
4
  native
5
5
  } from "./native.js";
6
+ const LATIN1_ENCODINGS = new Set(["latin1", "iso88591", "iso-8859-1"]);
7
+ const EMPTY_COLUMNS = EMPTY_U32.subarray(0, 0);
6
8
  export const MAX_COLUMN_INDEX = 2024;
7
9
  export const MAX_PROJECTION_LENGTH = 2024;
8
10
  export const MAX_FILTER_COUNT = MAX_PROJECTION_LENGTH;
@@ -26,18 +28,58 @@ export function projectionFilterProgram(options) {
26
28
  if (options.equalsFilter !== void 0)
27
29
  return [options.equalsFilter];
28
30
  const filters = options.filters;
29
- if (filters === void 0 || filters.length === 0)
31
+ if (filters === void 0)
32
+ return;
33
+ if (!Array.isArray(filters))
34
+ throw TypeError("filters must be an array of predicates");
35
+ if (filters.length === 0)
30
36
  return;
37
+ if (filters.length > MAX_FILTER_COUNT)
38
+ throw RangeError(`filter count out of range: ${filters.length}`);
31
39
  return filters.length === 1 ? filters : [...filters, { operator: "all", operandCount: filters.length }];
32
40
  }
33
41
  export function encodingCode(encoding = "utf8") {
34
- const normalized = encoding.toLowerCase();
42
+ const normalized = resolveInputEncoding(encoding);
35
43
  if (normalized === "utf8")
36
44
  return 0;
37
- if (normalized === "latin1" || normalized === "iso88591" || normalized === "iso-8859-1")
45
+ if (normalized === "latin1")
38
46
  return 1;
39
47
  throw Error(`unsupported encoding: ${encoding}`);
40
48
  }
49
+ export function resolveInputEncoding(encoding = "utf8", delimiter) {
50
+ if (encoding === "auto")
51
+ throw Error("automatic encoding detection is only supported by file parsing APIs; specify encoding explicitly");
52
+ if (encoding === "utf8" || encoding === "utf-8")
53
+ return "utf8";
54
+ if (LATIN1_ENCODINGS.has(encoding))
55
+ return "latin1";
56
+ if (typeof encoding !== "string")
57
+ throw TypeError("encoding must be a string");
58
+ let decoder;
59
+ try {
60
+ decoder = new TextDecoder(encoding);
61
+ } catch (cause) {
62
+ if (cause instanceof RangeError)
63
+ throw Error(`unsupported encoding: ${encoding}`, { cause });
64
+ throw cause;
65
+ }
66
+ if (decoder.encoding === "utf-8")
67
+ return "utf8";
68
+ if (decoder.encoding === "windows-1252" && LATIN1_ENCODINGS.has(encoding.trim().toLowerCase()))
69
+ return "latin1";
70
+ if (delimiter !== void 0 && delimiter.charCodeAt(0) > 127)
71
+ throw Error(`encoding ${decoder.encoding} requires an ASCII delimiter`);
72
+ return decoder;
73
+ }
74
+ export function prepareCsvBufferInput(buffer, encoding, delimiter) {
75
+ const resolved = resolveInputEncoding(encoding, delimiter);
76
+ if (typeof resolved === "string")
77
+ return { bytes: buffer, parserEncoding: resolved };
78
+ return {
79
+ bytes: new TextEncoder().encode(resolved.decode(buffer)),
80
+ parserEncoding: "utf8"
81
+ };
82
+ }
41
83
  export function normalizeChunk(chunk) {
42
84
  return chunk.byteLength === 0 ? EMPTY_BUFFER : chunk;
43
85
  }
@@ -48,14 +90,21 @@ export function normalizeFixedColumnsCount(value, label) {
48
90
  throw RangeError(`${label} out of range: ${value}`);
49
91
  return value;
50
92
  }
93
+ export function rejectSelectedColumns(options) {
94
+ if ("selectedColumns" in options)
95
+ throw TypeError("selectedColumns is unsupported; use columns");
96
+ }
51
97
  export function normalizeColumns(columns) {
52
98
  if (columns === void 0)
53
- return EMPTY_U32;
54
- if (columns.length > MAX_PROJECTION_LENGTH)
55
- throw RangeError(`selected column count out of range: ${columns.length}`);
56
- const normalized = new Uint32Array(columns.length);
99
+ return EMPTY_COLUMNS;
100
+ const count = columns.length;
101
+ if (count === 0)
102
+ return EMPTY_COLUMNS;
103
+ if (count > MAX_PROJECTION_LENGTH)
104
+ throw RangeError(`selected column count out of range: ${count}`);
105
+ const normalized = new Uint32Array(count);
57
106
  const seen = new Set;
58
- for (let index = 0;index < columns.length; ++index) {
107
+ for (let index = 0;index < count; ++index) {
59
108
  const column = columns[index] ?? NaN;
60
109
  normalizeColumnIndex(column, "selected column");
61
110
  if (seen.has(column))
@@ -63,34 +112,19 @@ export function normalizeColumns(columns) {
63
112
  seen.add(column);
64
113
  normalized[index] = column;
65
114
  }
66
- return normalized.length === 0 ? EMPTY_U32 : normalized;
115
+ return normalized;
67
116
  }
68
117
  export function normalizeColumnIndex(column, label) {
69
118
  if (!Number.isInteger(column) || column < 0 || column > MAX_COLUMN_INDEX)
70
119
  throw RangeError(`${label} out of range: ${column}`);
71
120
  return column;
72
121
  }
73
- export function normalizeEqualsFilter(filter) {
74
- if (filter === void 0)
75
- return {
76
- enabled: !1,
77
- column: 0,
78
- value: EMPTY_BUFFER,
79
- valueLength: 0
80
- };
81
- normalizeColumnIndex(filter.column, "filter column");
82
- const value = typeof filter.value === "string" ? Buffer.from(filter.value) : filter.value;
83
- return {
84
- enabled: !0,
85
- column: filter.column,
86
- value: value.byteLength === 0 ? EMPTY_BUFFER : value,
87
- valueLength: value.byteLength
88
- };
89
- }
90
122
  export function normalizeFilterColumn(column) {
91
123
  return normalizeColumnIndex(column, "filter column");
92
124
  }
93
125
  export function normalizeFilterValue(value) {
126
+ if (typeof value !== "string" && !(value instanceof Uint8Array))
127
+ throw TypeError("filter value must be a string, Buffer, or Uint8Array");
94
128
  return typeof value === "string" ? Buffer.from(value) : value;
95
129
  }
96
130
  export function normalizeRegex(regex) {
@@ -186,44 +220,6 @@ function convertJavaScriptRegexSource(source, unicode) {
186
220
  function isSurrogate(codePoint) {
187
221
  return codePoint >= 55296 && codePoint <= 57343;
188
222
  }
189
- export function normalizeInFilter(filter) {
190
- const column = normalizeFilterColumn(filter.column);
191
- if (filter.values.length === 0)
192
- throw RangeError("filter values must not be empty");
193
- const values = filter.values.map((value) => normalizeFilterValue(value));
194
- let valuesDataLength = 0;
195
- for (const value of values) {
196
- valuesDataLength += value.byteLength;
197
- if (valuesDataLength > 4294967295)
198
- throw RangeError("filter values exceed native offset range");
199
- }
200
- const valuesData = valuesDataLength === 0 ? EMPTY_BUFFER : new Uint8Array(valuesDataLength);
201
- const offsets = new Uint32Array(values.length + 1);
202
- let offset = 0;
203
- for (let index = 0;index < values.length; ++index) {
204
- const value = values[index] ?? EMPTY_BUFFER;
205
- offsets[index] = offset;
206
- valuesData.set(value, offset);
207
- offset += value.byteLength;
208
- }
209
- offsets[values.length] = offset;
210
- return {
211
- column,
212
- valuesData,
213
- valuesDataLength,
214
- offsets,
215
- valueCount: values.length
216
- };
217
- }
218
- export function normalizeStartsWithFilter(filter) {
219
- const column = normalizeFilterColumn(filter.column);
220
- const value = normalizeFilterValue(filter.prefix);
221
- return {
222
- column,
223
- value: value.byteLength === 0 ? EMPTY_BUFFER : value,
224
- valueLength: value.byteLength
225
- };
226
- }
227
223
  export function normalizeNativeFilters(filters) {
228
224
  if (filters === void 0 || filters.length === 0)
229
225
  return {
@@ -311,6 +307,8 @@ export function normalizeNativeFilters(filters) {
311
307
  };
312
308
  }
313
309
  export function normalizeNativeFilterProgram(program) {
310
+ if (!Array.isArray(program))
311
+ throw TypeError("filterProgram must be an array");
314
312
  if (program.length === 0)
315
313
  throw RangeError("filter program must contain at least one predicate");
316
314
  if (program.length > MAX_FILTER_PROGRAM_LENGTH)
@@ -321,6 +319,8 @@ export function normalizeNativeFilterProgram(program) {
321
319
  const entry = program[index];
322
320
  if (entry === void 0)
323
321
  throw TypeError(`filter program entry missing at index ${index}`);
322
+ if (typeof entry !== "object" || entry === null)
323
+ throw TypeError(`invalid filter program entry at index ${index}`);
324
324
  if (!isNativeFilterOperator(entry)) {
325
325
  predicates.push(entry);
326
326
  ++stackDepth;
@@ -371,5 +371,7 @@ function nativeFilterOperatorCode(operator) {
371
371
  return NATIVE_FILTER_ANY;
372
372
  case "not":
373
373
  return NATIVE_FILTER_NOT;
374
+ default:
375
+ throw TypeError(`unknown filter operator: ${String(operator)}`);
374
376
  }
375
377
  }
@@ -1,18 +1,19 @@
1
1
  import {
2
2
  MAX_FILTER_COUNT,
3
- MAX_PROJECTION_LENGTH
3
+ MAX_PROJECTION_LENGTH,
4
+ rejectSelectedColumns
4
5
  } from "./normalize.js";
5
6
  const snapshots = new WeakSet;
6
7
  export function snapshotCsvOptions(options) {
7
8
  if (snapshots.has(options))
8
9
  return options;
10
+ rejectSelectedColumns(options);
9
11
  const captured = { ...options };
10
12
  if (captured.filters !== void 0 && captured.filters.length > MAX_FILTER_COUNT)
11
13
  throw RangeError(`filter count out of range: ${captured.filters.length}`);
12
14
  const snapshot = Object.freeze({
13
15
  ...captured,
14
16
  columns: snapshotColumns(captured.columns),
15
- selectedColumns: snapshotColumns(captured.selectedColumns),
16
17
  expectedHeaders: captured.expectedHeaders === void 0 ? void 0 : Object.freeze([...captured.expectedHeaders]),
17
18
  compression: typeof captured.compression === "object" && captured.compression !== null ? Object.freeze({ ...captured.compression }) : captured.compression,
18
19
  equalsFilter: captured.equalsFilter === void 0 ? void 0 : Object.freeze({ ...captured.equalsFilter, value: copyFieldValue(captured.equalsFilter.value) }),
package/dist/options.d.ts CHANGED
@@ -1,11 +1,10 @@
1
- import type { CsvColumnarBatchOptions, CsvColumns, CsvCountOptions, CsvRowsOptions, CsvRowViewsOptions } from './types.js';
1
+ import type { CsvColumns, CsvCountOptions, CsvRowsOptions } from './types.js';
2
2
  /**
3
3
  * Preserve literal column inference for `csv.rows()` options.
4
4
  *
5
5
  * This identity helper is useful when options are declared separately from the call site. It
6
- * checks the selected-column/strict/worker union while retaining tuple literals for projected row
7
- * types; it does not clone or freeze the object. Each operation snapshots caller-owned option
8
- * values when invoked.
6
+ * checks projection and strict validation options while retaining tuple literals for projected row
7
+ * types. Each operation snapshots caller-owned option values when invoked.
9
8
  *
10
9
  * @typeParam TColumns - Literal selected column indexes, when supplied.
11
10
  * @typeParam TOptions - The complete rows option object to preserve.
@@ -24,35 +23,10 @@ export declare function defineRowsOptions<const TColumns extends CsvColumns | un
24
23
  /**
25
24
  * Preserve literal inference for `csv.count()` options.
26
25
  *
27
- * The helper validates the count option union, including the restrictions that apply to strict
28
- * and parallel counting, while returning the original object unchanged.
26
+ * The helper checks that strict counts use unfiltered input and returns the original object.
29
27
  *
30
28
  * @typeParam TOptions - The complete count option object to preserve.
31
29
  * @param options - Type-checked count options.
32
30
  * @returns The same options object, with its literal type preserved.
33
31
  */
34
32
  export declare function defineCountOptions<const TOptions extends CsvCountOptions>(options: TOptions): TOptions;
35
- /**
36
- * Preserve literal column inference for `csv.withColumnarBatches()` options.
37
- *
38
- * This identity helper retains projected column tuples for columnar batch callbacks and checks
39
- * the callback API's option restrictions, including its non-strict projected mode.
40
- *
41
- * @typeParam TColumns - Literal selected column indexes, when supplied.
42
- * @typeParam TOptions - The complete columnar batch option object to preserve.
43
- * @param options - Type-checked columnar batch options.
44
- * @returns The same options object, with its literal type preserved.
45
- */
46
- export declare function defineColumnarOptions<const TColumns extends CsvColumns | undefined, const TOptions extends CsvColumnarBatchOptions<TColumns>>(options: TOptions): TOptions;
47
- /**
48
- * Preserve literal column inference for `csv.withRowViews()` options.
49
- *
50
- * The helper checks the serial row-view option union and keeps a selected-column tuple visible to
51
- * the callback's `CsvRowView` type. It is an identity function at runtime.
52
- *
53
- * @typeParam TColumns - Literal selected column indexes, when supplied.
54
- * @typeParam TOptions - The complete row-view option object to preserve.
55
- * @param options - Type-checked row-view options.
56
- * @returns The same options object, with its literal type preserved.
57
- */
58
- export declare function defineRowViewOptions<const TColumns extends CsvColumns | undefined, const TOptions extends CsvRowViewsOptions<TColumns>>(options: TOptions): TOptions;
package/dist/options.js CHANGED
@@ -4,9 +4,3 @@ export function defineRowsOptions(options) {
4
4
  export function defineCountOptions(options) {
5
5
  return options;
6
6
  }
7
- export function defineColumnarOptions(options) {
8
- return options;
9
- }
10
- export function defineRowViewOptions(options) {
11
- return options;
12
- }