@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.
Files changed (65) hide show
  1. package/README.md +152 -463
  2. package/THIRD_PARTY_NOTICES.md +7 -1
  3. package/dist/api.d.ts +169 -44
  4. package/dist/api.js +75 -368
  5. package/dist/batches.d.ts +250 -7
  6. package/dist/batches.js +135 -83
  7. package/dist/cli-command.d.ts +7 -0
  8. package/dist/cli-command.js +350 -0
  9. package/dist/cli-schema.d.ts +111 -0
  10. package/dist/cli-schema.js +219 -0
  11. package/dist/cli.js +2 -604
  12. package/dist/file-stream.d.ts +57 -2
  13. package/dist/file-stream.js +244 -36
  14. package/dist/files.d.ts +149 -3
  15. package/dist/files.js +37 -29
  16. package/dist/filter.d.ts +80 -0
  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 +12 -0
  22. package/dist/inspect-file.js +88 -0
  23. package/dist/latin1-rows.d.ts +5 -0
  24. package/dist/latin1-rows.js +122 -0
  25. package/dist/materialize-field.d.ts +3 -0
  26. package/dist/materialize-field.js +31 -0
  27. package/dist/native.d.ts +51 -2
  28. package/dist/native.js +43 -4
  29. package/dist/normalize.d.ts +32 -2
  30. package/dist/normalize.js +51 -2
  31. package/dist/options-snapshot.d.ts +3 -0
  32. package/dist/options-snapshot.js +51 -0
  33. package/dist/options.d.ts +25 -10
  34. package/dist/options.js +0 -6
  35. package/dist/parser.d.ts +309 -4
  36. package/dist/parser.js +128 -31
  37. package/dist/strict-schema.d.ts +5 -3
  38. package/dist/strict-schema.js +4 -1
  39. package/dist/types.d.ts +355 -121
  40. package/dist/zip-reader.d.ts +11 -1
  41. package/dist/zip-reader.js +12 -3
  42. package/package.json +44 -29
  43. package/prebuilds/darwin-arm64/libcsv_native.dylib +0 -0
  44. package/prebuilds/darwin-x64/libcsv_native.dylib +0 -0
  45. package/prebuilds/linux-x64/libcsv_native.so +0 -0
  46. package/prebuilds/manifest.json +9 -8
  47. package/prebuilds/provenance/darwin-arm64.json +1 -1
  48. package/prebuilds/provenance/darwin-x64.json +1 -1
  49. package/prebuilds/provenance/linux-x64.json +1 -1
  50. package/dist/worker-count.d.ts +0 -2
  51. package/dist/worker-count.js +0 -96
  52. package/dist/worker-factory.d.ts +0 -3
  53. package/dist/worker-factory.js +0 -9
  54. package/dist/worker-filter.d.ts +0 -29
  55. package/dist/worker-filter.js +0 -46
  56. package/dist/worker-module.d.ts +0 -1
  57. package/dist/worker-module.js +0 -4
  58. package/dist/worker-pool.d.ts +0 -17
  59. package/dist/worker-pool.js +0 -290
  60. package/dist/worker-rows.d.ts +0 -8
  61. package/dist/worker-rows.js +0 -118
  62. package/dist/workers/count.worker.d.ts +0 -1
  63. package/dist/workers/count.worker.js +0 -49
  64. package/dist/workers/rows.worker.d.ts +0 -1
  65. package/dist/workers/rows.worker.js +0 -85
package/dist/batches.js CHANGED
@@ -1,3 +1,5 @@
1
+ import { read } from "bun:ffi";
2
+ import { decodeMaterializedField } from "./materialize-field.js";
1
3
  import {
2
4
  EMPTY_BUFFER,
3
5
  native,
@@ -9,19 +11,24 @@ import {
9
11
  normalizeColumns,
10
12
  normalizeFilterColumn
11
13
  } from "./normalize.js";
14
+ const visitBatchRows = Symbol("visitBatchRows");
12
15
 
13
16
  export class NativeCsvRowView {
14
17
  #data;
15
- #rowOffsets;
18
+ #rowOffsetWords;
16
19
  #fieldOffsets;
20
+ #fieldOffsetWords;
17
21
  #rowIndex;
22
+ #owner;
18
23
  constructor(data, rowOffsets, fieldOffsets, rowIndex = 0) {
19
24
  this.#data = data;
20
- this.#rowOffsets = rowOffsets;
25
+ this.#rowOffsetWords = offsetWords(rowOffsets);
21
26
  this.#fieldOffsets = fieldOffsets;
27
+ this.#fieldOffsetWords = offsetWords(fieldOffsets);
22
28
  this.#rowIndex = rowIndex;
23
29
  }
24
30
  get rowIndex() {
31
+ this.#requireActive();
25
32
  return this.#rowIndex;
26
33
  }
27
34
  get fieldCount() {
@@ -33,75 +40,82 @@ export class NativeCsvRowView {
33
40
  this.#rowIndex = rowIndex;
34
41
  return this;
35
42
  }
36
- fieldRange(columnIndex) {
43
+ range(columnIndex) {
37
44
  const [rowStart, rowEnd] = this.#rowRange(this.#rowIndex);
38
45
  const fieldIndex = rowStart + columnIndex;
39
46
  if (fieldIndex < rowStart || fieldIndex >= rowEnd)
40
47
  return null;
41
48
  return {
42
- start: offsetAt(this.#fieldOffsets, fieldIndex, "field offset", this.#data.byteLength),
43
- end: offsetAt(this.#fieldOffsets, fieldIndex + 1, "field offset", this.#data.byteLength)
49
+ start: offsetAt(this.#fieldOffsetWords, fieldIndex, "field offset", this.#data.byteLength),
50
+ end: offsetAt(this.#fieldOffsetWords, fieldIndex + 1, "field offset", this.#data.byteLength)
44
51
  };
45
52
  }
46
- range(columnIndex) {
47
- return this.fieldRange(columnIndex);
48
- }
49
- fieldBytes(columnIndex) {
50
- const range = this.fieldRange(columnIndex);
51
- if (range === null)
52
- return null;
53
- return this.#data.subarray(range.start, range.end);
54
- }
55
53
  bytes(columnIndex) {
56
- return this.fieldBytes(columnIndex);
54
+ return this.buffer(columnIndex);
57
55
  }
58
- fieldBuffer(columnIndex) {
59
- const range = this.fieldRange(columnIndex);
56
+ buffer(columnIndex) {
57
+ const range = this.range(columnIndex);
60
58
  if (range === null)
61
59
  return null;
62
60
  return this.#data.subarray(range.start, range.end);
63
61
  }
64
- buffer(columnIndex) {
65
- return this.fieldBuffer(columnIndex);
66
- }
67
- fieldString(columnIndex) {
68
- const range = this.fieldRange(columnIndex);
62
+ get(columnIndex) {
63
+ const range = this.range(columnIndex);
69
64
  if (range === null)
70
65
  return null;
71
66
  return this.#data.toString("utf8", range.start, range.end);
72
67
  }
73
- getPhysical(columnIndex) {
74
- return this.fieldString(columnIndex);
75
- }
76
- get(columnIndex) {
77
- return this.getPhysical(columnIndex);
78
- }
79
- pickPhysical(columns) {
68
+ pick(columns) {
69
+ this.#requireActive();
80
70
  const values = [];
81
71
  values.length = columns.length;
82
72
  for (let index = 0;index < columns.length; ++index)
83
- values[index] = this.fieldString(columns[index] ?? 0) ?? "";
73
+ values[index] = this.get(columns[index] ?? 0) ?? "";
84
74
  return values;
85
75
  }
86
- pick(columns) {
87
- return this.pickPhysical(columns);
88
- }
89
76
  #rowRange(rowIndex) {
77
+ this.#requireActive();
90
78
  const maxFieldIndex = this.#fieldOffsets.length - 1;
91
- const rowStart = offsetAt(this.#rowOffsets, rowIndex, `row ${rowIndex} offset`, maxFieldIndex);
92
- const rowEnd = offsetAt(this.#rowOffsets, rowIndex + 1, `row ${rowIndex} offset`, maxFieldIndex);
79
+ const rowStart = offsetAt(this.#rowOffsetWords, rowIndex, `row ${rowIndex} offset`, maxFieldIndex);
80
+ const rowEnd = offsetAt(this.#rowOffsetWords, rowIndex + 1, `row ${rowIndex} offset`, maxFieldIndex);
93
81
  if (rowEnd < rowStart)
94
82
  throw RangeError(`row ${rowIndex} offsets are not monotonic`);
95
83
  return [rowStart, rowEnd];
96
84
  }
85
+ #requireActive() {
86
+ if (this.#owner === null)
87
+ throw Error("row view is only valid during row view callback");
88
+ if (this.#owner?.closed)
89
+ throw Error("native CSV batch is closed");
90
+ }
91
+ static [visitBatchRows](batch, callback) {
92
+ const rowCount = batch.rowCount;
93
+ const view = new NativeCsvRowView(batch.data(), batch.rowOffsets(), batch.fieldOffsets());
94
+ for (let index = 0;index < rowCount; ++index) {
95
+ view.#owner = batch;
96
+ let result;
97
+ try {
98
+ result = callback(view.moveTo(index), index);
99
+ } finally {
100
+ view.#owner = null;
101
+ }
102
+ if (result !== null && (typeof result === "object" || typeof result === "function") && "then" in result && typeof result.then === "function") {
103
+ void Promise.resolve(result).catch(() => {});
104
+ throw TypeError("row view callback must be synchronous");
105
+ }
106
+ }
107
+ }
97
108
  }
98
109
 
99
110
  export class NativeCsvBatch {
100
111
  #handle;
101
112
  #data;
102
113
  #dataView;
114
+ #rowOffsetLimit;
103
115
  #rowOffsets;
116
+ #rowOffsetWords;
104
117
  #fieldOffsets;
118
+ #fieldOffsetWords;
105
119
  constructor(handle) {
106
120
  this.#handle = handle;
107
121
  }
@@ -121,23 +135,20 @@ export class NativeCsvBatch {
121
135
  return this.rowsInto([]);
122
136
  }
123
137
  forEachRow(callback) {
124
- const rowCount = this.rowCount;
125
- const rowView = new NativeCsvRowView(this.data(), this.rowOffsets(), this.fieldOffsets());
126
- for (let rowIndex = 0;rowIndex < rowCount; ++rowIndex)
127
- callback(rowView.moveTo(rowIndex), rowIndex);
138
+ NativeCsvRowView[visitBatchRows](this, callback);
128
139
  }
129
140
  rowsInto(target, columns) {
130
141
  normalizeColumns(columns);
131
142
  const rowCount = this.rowCount;
132
143
  const totalFields = this.totalFields;
133
- const rowOffsets = this.rowOffsets();
134
- const fieldOffsets = this.fieldOffsets();
144
+ const rowOffsetWords = this.#rowOffsetWordsView();
145
+ const fieldOffsetWords = this.#fieldOffsetWordsView();
135
146
  const data = this.data();
136
147
  target.length = rowCount;
137
148
  if (columns !== void 0) {
138
149
  for (let rowIndex = 0;rowIndex < rowCount; ++rowIndex) {
139
- const fieldStart = offsetAt(rowOffsets, rowIndex, `row ${rowIndex} offset`, totalFields);
140
- const fieldEnd = offsetAt(rowOffsets, rowIndex + 1, `row ${rowIndex} offset`, totalFields);
150
+ const fieldStart = offsetAt(rowOffsetWords, rowIndex, `row ${rowIndex} offset`, totalFields);
151
+ const fieldEnd = offsetAt(rowOffsetWords, rowIndex + 1, `row ${rowIndex} offset`, totalFields);
141
152
  const existing = target[rowIndex];
142
153
  const row = existing === void 0 ? [] : existing;
143
154
  row.length = columns.length;
@@ -148,25 +159,25 @@ export class NativeCsvBatch {
148
159
  row[outputIndex] = "";
149
160
  continue;
150
161
  }
151
- const start = offsetAt(fieldOffsets, fieldIndex, "field offset", data.byteLength);
152
- const end = offsetAt(fieldOffsets, fieldIndex + 1, "field offset", data.byteLength);
153
- row[outputIndex] = data.toString("utf8", start, end);
162
+ const start = offsetAt(fieldOffsetWords, fieldIndex, "field offset", data.byteLength);
163
+ const end = offsetAt(fieldOffsetWords, fieldIndex + 1, "field offset", data.byteLength);
164
+ row[outputIndex] = decodeMaterializedField(data, start, end, "utf8");
154
165
  }
155
166
  target[rowIndex] = row;
156
167
  }
157
168
  return target;
158
169
  }
159
170
  for (let rowIndex = 0;rowIndex < rowCount; ++rowIndex) {
160
- const fieldStart = offsetAt(rowOffsets, rowIndex, `row ${rowIndex} offset`, totalFields);
161
- const fieldEnd = offsetAt(rowOffsets, rowIndex + 1, `row ${rowIndex} offset`, totalFields);
171
+ const fieldStart = offsetAt(rowOffsetWords, rowIndex, `row ${rowIndex} offset`, totalFields);
172
+ const fieldEnd = offsetAt(rowOffsetWords, rowIndex + 1, `row ${rowIndex} offset`, totalFields);
162
173
  const existing = target[rowIndex];
163
174
  const row = existing === void 0 ? [] : existing;
164
175
  row.length = fieldEnd - fieldStart;
165
176
  for (let fieldIndex = fieldStart;fieldIndex < fieldEnd; ++fieldIndex) {
166
- const start = offsetAt(fieldOffsets, fieldIndex, "field offset", data.byteLength);
167
- const end = offsetAt(fieldOffsets, fieldIndex + 1, "field offset", data.byteLength);
177
+ const start = offsetAt(fieldOffsetWords, fieldIndex, "field offset", data.byteLength);
178
+ const end = offsetAt(fieldOffsetWords, fieldIndex + 1, "field offset", data.byteLength);
168
179
  const column = fieldIndex - fieldStart;
169
- row[column] = data.toString("utf8", start, end);
180
+ row[column] = decodeMaterializedField(data, start, end, "utf8");
170
181
  }
171
182
  target[rowIndex] = row;
172
183
  }
@@ -192,10 +203,7 @@ export class NativeCsvBatch {
192
203
  if (this.#rowOffsets !== void 0)
193
204
  return this.#rowOffsets;
194
205
  const rowCount = this.rowCount;
195
- const ptr = native.symbols.csv_batch_row_offsets_ptr(this.#requireHandle());
196
- if (ptr === null)
197
- throw Error("native CSV batch row offsets are null");
198
- this.#rowOffsets = new BigUint64Array(toArrayBuffer(ptr, 0, (rowCount + 1) * BigUint64Array.BYTES_PER_ELEMENT));
206
+ this.#rowOffsets = new BigUint64Array(toArrayBuffer(this.#rowOffsetsPointer(), 0, (rowCount + 1) * BigUint64Array.BYTES_PER_ELEMENT));
199
207
  return this.#rowOffsets;
200
208
  }
201
209
  fieldOffsets() {
@@ -209,24 +217,37 @@ export class NativeCsvBatch {
209
217
  return this.#fieldOffsets;
210
218
  }
211
219
  rowFieldCount(rowIndex) {
212
- const rowOffsets = this.rowOffsets();
213
- const start = offsetAt(rowOffsets, rowIndex, `row ${rowIndex} offset`, this.totalFields);
214
- const end = offsetAt(rowOffsets, rowIndex + 1, `row ${rowIndex} offset`, this.totalFields);
220
+ const firstLookupWithoutView = this.#rowOffsetLimit === void 0 && this.#rowOffsets === void 0;
221
+ this.#rowOffsetLimit ??= this.totalFields;
222
+ const totalFields = this.#rowOffsetLimit;
223
+ if (!Number.isInteger(rowIndex) || rowIndex < 0)
224
+ throw RangeError(`row ${rowIndex} offset index out of range: ${rowIndex}`);
225
+ if (firstLookupWithoutView && rowIndex < MAX_DIRECT_ROW_INDEX_EXCLUSIVE && rowIndex < native.symbols.csv_batch_row_count(this.#requireHandle())) {
226
+ const ptr = this.#rowOffsetsPointer();
227
+ const byteOffset = rowIndex * BigUint64Array.BYTES_PER_ELEMENT;
228
+ const start = Number(read.u64(ptr, byteOffset));
229
+ const end = Number(read.u64(ptr, byteOffset + BigUint64Array.BYTES_PER_ELEMENT));
230
+ if (start <= totalFields && end <= totalFields)
231
+ return end - start;
232
+ }
233
+ const rowOffsetWords = this.#rowOffsetWords ?? this.#rowOffsetWordsView();
234
+ const start = offsetAt(rowOffsetWords, rowIndex, `row ${rowIndex} offset`, totalFields);
235
+ const end = offsetAt(rowOffsetWords, rowIndex + 1, `row ${rowIndex} offset`, totalFields);
215
236
  return end - start;
216
237
  }
217
238
  fieldRange(rowIndex, columnIndex) {
218
- const rowOffsets = this.rowOffsets();
219
- const fieldOffsets = this.fieldOffsets();
239
+ const rowOffsetWords = this.#rowOffsetWordsView();
240
+ const fieldOffsetWords = this.#fieldOffsetWordsView();
220
241
  const totalFields = this.totalFields;
221
242
  const dataLength = this.dataLength;
222
- const rowStart = offsetAt(rowOffsets, rowIndex, `row ${rowIndex} offset`, totalFields);
223
- const rowEnd = offsetAt(rowOffsets, rowIndex + 1, `row ${rowIndex} offset`, totalFields);
243
+ const rowStart = offsetAt(rowOffsetWords, rowIndex, `row ${rowIndex} offset`, totalFields);
244
+ const rowEnd = offsetAt(rowOffsetWords, rowIndex + 1, `row ${rowIndex} offset`, totalFields);
224
245
  const fieldIndex = rowStart + columnIndex;
225
246
  if (fieldIndex < rowStart || fieldIndex >= rowEnd)
226
247
  return null;
227
248
  return {
228
- start: offsetAt(fieldOffsets, fieldIndex, "field offset", dataLength),
229
- end: offsetAt(fieldOffsets, fieldIndex + 1, "field offset", dataLength)
249
+ start: offsetAt(fieldOffsetWords, fieldIndex, "field offset", dataLength),
250
+ end: offsetAt(fieldOffsetWords, fieldIndex + 1, "field offset", dataLength)
230
251
  };
231
252
  }
232
253
  fieldBuffer(rowIndex, columnIndex) {
@@ -248,19 +269,20 @@ export class NativeCsvBatch {
248
269
  return this.data().toString("utf8", range.start, range.end);
249
270
  }
250
271
  forEachColumnRange(columnIndex, callback, startRow = 0, endRow = this.rowCount) {
251
- const rowOffsets = this.rowOffsets();
252
- const fieldOffsets = this.fieldOffsets();
272
+ const rowOffsetWords = this.#rowOffsetWordsView();
273
+ const fieldOffsetWords = this.#fieldOffsetWordsView();
253
274
  const totalFields = this.totalFields;
254
275
  const dataLength = this.dataLength;
255
276
  const resolvedEndRow = this.#resolveEndRow(endRow);
256
277
  this.#validateRowRange(startRow, resolvedEndRow);
257
278
  for (let rowIndex = startRow;rowIndex < resolvedEndRow; ++rowIndex) {
258
- const rowStart = offsetAt(rowOffsets, rowIndex, `row ${rowIndex} offset`, totalFields);
259
- const rowEnd = offsetAt(rowOffsets, rowIndex + 1, `row ${rowIndex} offset`, totalFields);
279
+ this.#requireHandle();
280
+ const rowStart = offsetAt(rowOffsetWords, rowIndex, `row ${rowIndex} offset`, totalFields);
281
+ const rowEnd = offsetAt(rowOffsetWords, rowIndex + 1, `row ${rowIndex} offset`, totalFields);
260
282
  const fieldIndex = rowStart + columnIndex;
261
283
  if (fieldIndex < rowStart || fieldIndex >= rowEnd)
262
284
  continue;
263
- callback(rowIndex, offsetAt(fieldOffsets, fieldIndex, "field offset", dataLength), offsetAt(fieldOffsets, fieldIndex + 1, "field offset", dataLength));
285
+ callback(rowIndex, offsetAt(fieldOffsetWords, fieldIndex, "field offset", dataLength), offsetAt(fieldOffsetWords, fieldIndex + 1, "field offset", dataLength));
264
286
  }
265
287
  }
266
288
  forEachColumnBytes(columnIndex, callback, startRow = 0, endRow = this.rowCount) {
@@ -272,14 +294,15 @@ export class NativeCsvBatch {
272
294
  scanColumns(columns, callback, startRow = 0, endRow = this.rowCount) {
273
295
  const resolvedEndRow = this.#resolveEndRow(endRow);
274
296
  this.#validateRowRange(startRow, resolvedEndRow);
275
- const rowOffsets = this.rowOffsets();
276
- const fieldOffsets = this.fieldOffsets();
297
+ const rowOffsetWords = this.#rowOffsetWordsView();
298
+ const fieldOffsetWords = this.#fieldOffsetWordsView();
277
299
  const data = this.data();
278
300
  const totalFields = this.totalFields;
279
301
  const ranges = new Float64Array(columns.length * 2);
280
302
  for (let rowIndex = startRow;rowIndex < resolvedEndRow; ++rowIndex) {
281
- const rowStart = offsetAt(rowOffsets, rowIndex, `row ${rowIndex} offset`, totalFields);
282
- const rowEnd = offsetAt(rowOffsets, rowIndex + 1, `row ${rowIndex} offset`, totalFields);
303
+ this.#requireHandle();
304
+ const rowStart = offsetAt(rowOffsetWords, rowIndex, `row ${rowIndex} offset`, totalFields);
305
+ const rowEnd = offsetAt(rowOffsetWords, rowIndex + 1, `row ${rowIndex} offset`, totalFields);
283
306
  for (let columnOffset = 0;columnOffset < columns.length; ++columnOffset) {
284
307
  const columnIndex = columns[columnOffset] ?? 0;
285
308
  const fieldIndex = rowStart + columnIndex;
@@ -289,8 +312,8 @@ export class NativeCsvBatch {
289
312
  ranges[rangeIndex + 1] = -1;
290
313
  continue;
291
314
  }
292
- ranges[rangeIndex] = offsetAt(fieldOffsets, fieldIndex, "field offset", data.byteLength);
293
- ranges[rangeIndex + 1] = offsetAt(fieldOffsets, fieldIndex + 1, "field offset", data.byteLength);
315
+ ranges[rangeIndex] = offsetAt(fieldOffsetWords, fieldIndex, "field offset", data.byteLength);
316
+ ranges[rangeIndex + 1] = offsetAt(fieldOffsetWords, fieldIndex + 1, "field offset", data.byteLength);
294
317
  }
295
318
  callback(rowIndex, ranges, data);
296
319
  }
@@ -307,13 +330,13 @@ export class NativeCsvBatch {
307
330
  this.#handle = null;
308
331
  this.#data = void 0;
309
332
  this.#dataView = void 0;
333
+ this.#rowOffsetLimit = void 0;
310
334
  this.#rowOffsets = void 0;
335
+ this.#rowOffsetWords = void 0;
311
336
  this.#fieldOffsets = void 0;
337
+ this.#fieldOffsetWords = void 0;
312
338
  }
313
339
  }
314
- dispose() {
315
- this.close();
316
- }
317
340
  [Symbol.dispose]() {
318
341
  this.close();
319
342
  }
@@ -322,6 +345,24 @@ export class NativeCsvBatch {
322
345
  throw Error("native CSV batch is closed");
323
346
  return this.#handle;
324
347
  }
348
+ #rowOffsetWordsView() {
349
+ const rowOffsets = this.rowOffsets();
350
+ if (this.#rowOffsetWords === void 0)
351
+ this.#rowOffsetWords = offsetWords(rowOffsets);
352
+ return this.#rowOffsetWords;
353
+ }
354
+ #rowOffsetsPointer() {
355
+ const ptr = native.symbols.csv_batch_row_offsets_ptr(this.#requireHandle());
356
+ if (ptr === null)
357
+ throw Error("native CSV batch row offsets are null");
358
+ return ptr;
359
+ }
360
+ #fieldOffsetWordsView() {
361
+ const fieldOffsets = this.fieldOffsets();
362
+ if (this.#fieldOffsetWords === void 0)
363
+ this.#fieldOffsetWords = offsetWords(fieldOffsets);
364
+ return this.#fieldOffsetWords;
365
+ }
325
366
  #resolveEndRow(endRow) {
326
367
  if (Number.isNaN(endRow))
327
368
  throw RangeError(`row index out of range: ${endRow}`);
@@ -335,14 +376,25 @@ export class NativeCsvBatch {
335
376
  throw RangeError(`row index out of range: ${endRow}`);
336
377
  }
337
378
  }
379
+ const UINT32_BASE = 4294967296;
380
+ const UINT32_BASE_BIGINT = 0x100000000n;
381
+ const MAX_SAFE_OFFSET_HIGH_WORD = 2097151;
382
+ const MAX_DIRECT_ROW_INDEX_EXCLUSIVE = Math.floor(2147483647 / BigUint64Array.BYTES_PER_ELEMENT);
383
+ function offsetWords(offsets) {
384
+ return new Uint32Array(offsets.buffer, offsets.byteOffset, offsets.length * 2);
385
+ }
338
386
  function offsetAt(offsets, index, label, upperBound) {
339
- const value = offsets[index];
340
- if (value === void 0)
387
+ const wordIndex = index * 2;
388
+ const low = offsets[wordIndex];
389
+ const high = offsets[wordIndex + 1];
390
+ if (low === void 0 || high === void 0 || wordIndex % 2 !== 0)
341
391
  throw RangeError(`${label} index out of range: ${index}`);
342
- if (value > BigInt(Number.MAX_SAFE_INTEGER))
392
+ if (high > MAX_SAFE_OFFSET_HIGH_WORD) {
393
+ const value = BigInt(high) * UINT32_BASE_BIGINT + BigInt(low);
343
394
  throw RangeError(`${label} exceeds Number.MAX_SAFE_INTEGER: ${value}`);
344
- const offset = Number(value);
395
+ }
396
+ const offset = low + high * UINT32_BASE;
345
397
  if (offset > upperBound)
346
- throw RangeError(`${label} exceeds backing storage: ${value}`);
398
+ throw RangeError(`${label} exceeds backing storage: ${offset}`);
347
399
  return offset;
348
400
  }
@@ -0,0 +1,7 @@
1
+ interface CliOutput {
2
+ log(message: string | number): void;
3
+ error(message: string): void;
4
+ write(chunk: string): Promise<number>;
5
+ }
6
+ export declare function runCli(arguments_: readonly string[], customOutput?: CliOutput): Promise<number>;
7
+ export {};