@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.
- package/README.md +50 -28
- package/THIRD_PARTY_NOTICES.md +7 -1
- package/dist/api.d.ts +39 -249
- package/dist/api.js +56 -394
- package/dist/batches.d.ts +8 -57
- package/dist/batches.js +77 -52
- package/dist/cli-command.d.ts +7 -0
- package/dist/cli-command.js +315 -0
- package/dist/cli-schema.d.ts +127 -0
- package/dist/cli-schema.js +234 -0
- package/dist/cli.js +2 -604
- package/dist/file-stream.d.ts +62 -3
- package/dist/file-stream.js +242 -36
- package/dist/files.d.ts +11 -11
- package/dist/files.js +38 -79
- package/dist/filter.d.ts +5 -8
- package/dist/format-bytes.d.ts +1 -0
- package/dist/format-bytes.js +11 -0
- package/dist/index.d.ts +3 -3
- package/dist/index.js +2 -14
- package/dist/inspect-file.d.ts +22 -0
- package/dist/inspect-file.js +138 -0
- package/dist/latin1-rows.d.ts +4 -3
- package/dist/latin1-rows.js +72 -24
- package/dist/materialize-field.d.ts +2 -0
- package/dist/materialize-field.js +20 -15
- package/dist/native.d.ts +50 -78
- package/dist/native.js +36 -112
- package/dist/normalize.d.ts +14 -40
- package/dist/normalize.js +66 -64
- package/dist/options-snapshot.js +3 -2
- package/dist/options.d.ts +4 -30
- package/dist/options.js +0 -6
- package/dist/parser.d.ts +23 -209
- package/dist/parser.js +146 -261
- package/dist/strict-schema.d.ts +6 -4
- package/dist/strict-schema.js +4 -3
- package/dist/types.d.ts +119 -221
- package/dist/zip-reader.d.ts +13 -1
- package/dist/zip-reader.js +36 -17
- package/package.json +36 -33
- package/prebuilds/darwin-arm64/libcsv_native.dylib +0 -0
- package/prebuilds/darwin-x64/libcsv_native.dylib +0 -0
- package/prebuilds/linux-x64/libcsv_native.so +0 -0
- package/prebuilds/manifest.json +9 -8
- package/prebuilds/provenance/darwin-arm64.json +1 -1
- package/prebuilds/provenance/darwin-x64.json +1 -1
- package/prebuilds/provenance/linux-x64.json +1 -1
- package/dist/worker-count.d.ts +0 -34
- package/dist/worker-count.js +0 -101
- package/dist/worker-factory.d.ts +0 -3
- package/dist/worker-factory.js +0 -9
- package/dist/worker-filter.d.ts +0 -29
- package/dist/worker-filter.js +0 -46
- package/dist/worker-module.d.ts +0 -1
- package/dist/worker-module.js +0 -4
- package/dist/worker-pool.d.ts +0 -93
- package/dist/worker-pool.js +0 -298
- package/dist/worker-rows.d.ts +0 -36
- package/dist/worker-rows.js +0 -127
- package/dist/workers/count.worker.d.ts +0 -1
- package/dist/workers/count.worker.js +0 -36
- package/dist/workers/rows.worker.d.ts +0 -1
- package/dist/workers/rows.worker.js +0 -56
package/dist/worker-pool.d.ts
DELETED
|
@@ -1,93 +0,0 @@
|
|
|
1
|
-
import type { CsvApiFileOptions, CsvColumns, CsvProjectedRow, CsvWorkerPoolOptions } from './types.js';
|
|
2
|
-
type CsvSelectedColumnsFromOptions<TOptions extends CsvApiFileOptions> = TOptions extends {
|
|
3
|
-
columns: infer TColumns extends CsvColumns;
|
|
4
|
-
} ? TColumns : TOptions extends {
|
|
5
|
-
selectedColumns: infer TColumns extends CsvColumns;
|
|
6
|
-
} ? TColumns : undefined;
|
|
7
|
-
/**
|
|
8
|
-
* Reusable worker pool for repeated parallel scans of one CSV file.
|
|
9
|
-
*
|
|
10
|
-
* The path and options are captured when the pool is constructed. Record-safe
|
|
11
|
-
* shards and the count/row workers are then reused across calls, avoiding
|
|
12
|
-
* repeated worker startup. Operations are serialized: calling `count()` or
|
|
13
|
-
* `rows()` while another operation is active throws instead of racing shared
|
|
14
|
-
* workers.
|
|
15
|
-
*
|
|
16
|
-
* Keep the source file unchanged while reusing a pool; its record-safe shard
|
|
17
|
-
* boundaries are cached after the first operation.
|
|
18
|
-
*
|
|
19
|
-
* Use `close()` or a `using` declaration when the pool is no longer needed.
|
|
20
|
-
* Closing an active pool cancels its operation and terminates its workers.
|
|
21
|
-
* Compressed input and automatic delimiter detection are not supported by
|
|
22
|
-
* byte-offset sharding; strict validation is unsupported by parallel workers.
|
|
23
|
-
*
|
|
24
|
-
* @typeParam TColumns Tuple of selected physical columns used to type projected rows.
|
|
25
|
-
*
|
|
26
|
-
* @example
|
|
27
|
-
* ```ts
|
|
28
|
-
* import { CsvWorkerPool } from '@konstit/csv';
|
|
29
|
-
*
|
|
30
|
-
* using pool = new CsvWorkerPool('data.csv', { workerCount: 4 });
|
|
31
|
-
* console.log(await pool.count());
|
|
32
|
-
* ```
|
|
33
|
-
*/
|
|
34
|
-
export declare class CsvWorkerPool<TColumns extends CsvColumns | undefined = undefined> {
|
|
35
|
-
#private;
|
|
36
|
-
/**
|
|
37
|
-
* Create a pool configured for one uncompressed CSV file.
|
|
38
|
-
*
|
|
39
|
-
* @param path Path to the CSV file.
|
|
40
|
-
* @param suppliedOptions Parallel row options, including `workerCount` greater than one.
|
|
41
|
-
* @throws {RangeError} If `workerCount` is not an integer greater than one.
|
|
42
|
-
* @throws If compressed input or automatic delimiter detection is requested.
|
|
43
|
-
*/
|
|
44
|
-
constructor(path: string, suppliedOptions: CsvWorkerPoolOptions<TColumns>);
|
|
45
|
-
/** Whether `close()` has been called and the pool can no longer start work. */
|
|
46
|
-
get closed(): boolean;
|
|
47
|
-
/**
|
|
48
|
-
* Count logical CSV records across the pool's record-safe shards.
|
|
49
|
-
*
|
|
50
|
-
* Native `where` filters are evaluated in the workers and shard counts are
|
|
51
|
-
* summed on the main thread. Strict validation is not supported. The pool
|
|
52
|
-
* must be open and idle when this method is called.
|
|
53
|
-
*
|
|
54
|
-
* @returns A promise for the total logical record count.
|
|
55
|
-
* @throws {RangeError} If the total cannot be represented as a safe integer.
|
|
56
|
-
* @throws If the pool is closed or busy, strict mode was configured, or a worker, file, or parser error occurs.
|
|
57
|
-
*/
|
|
58
|
-
count(): Promise<number>;
|
|
59
|
-
/**
|
|
60
|
-
* Stream projected row batches from the pool's workers.
|
|
61
|
-
*
|
|
62
|
-
* Batches are yielded as workers report them and therefore are not globally
|
|
63
|
-
* ordered by file position; each shard retains its own row order. The pool
|
|
64
|
-
* must be open and idle when iteration starts. Stopping iteration early
|
|
65
|
-
* terminates the active row workers.
|
|
66
|
-
*
|
|
67
|
-
* @returns An async generator of non-empty projected row batches.
|
|
68
|
-
* @throws If the pool is closed or busy, strict mode was configured, or a worker, file, or parser error occurs.
|
|
69
|
-
*/
|
|
70
|
-
rows(): AsyncGenerator<CsvProjectedRow<TColumns>[], void>;
|
|
71
|
-
/**
|
|
72
|
-
* Cancel active work and terminate all workers owned by this pool.
|
|
73
|
-
*
|
|
74
|
-
* This method is idempotent. A closed pool cannot be used again.
|
|
75
|
-
*/
|
|
76
|
-
close(): void;
|
|
77
|
-
/** Dispose the pool for use with a `using` declaration. */
|
|
78
|
-
[Symbol.dispose](): void;
|
|
79
|
-
}
|
|
80
|
-
/**
|
|
81
|
-
* Construct a reusable {@link CsvWorkerPool} with tuple-aware row types.
|
|
82
|
-
*
|
|
83
|
-
* This factory is the implementation behind the package-level `workerPool`
|
|
84
|
-
* helper. It captures the supplied options when called.
|
|
85
|
-
*
|
|
86
|
-
* @param path Path to the uncompressed CSV file.
|
|
87
|
-
* @param options Parallel row options, including `workerCount` greater than one.
|
|
88
|
-
* @returns A reusable worker pool. Dispose it with `close()` or `using`.
|
|
89
|
-
* @throws {RangeError} If `workerCount` is not an integer greater than one.
|
|
90
|
-
* @throws If compressed input or automatic delimiter detection is requested.
|
|
91
|
-
*/
|
|
92
|
-
export declare function createWorkerPool<TOptions extends CsvWorkerPoolOptions>(path: string, options: TOptions): CsvWorkerPool<CsvSelectedColumnsFromOptions<TOptions>>;
|
|
93
|
-
export {};
|
package/dist/worker-pool.js
DELETED
|
@@ -1,298 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
rejectAutoDelimiterSharding,
|
|
3
|
-
rejectCompressedSharding
|
|
4
|
-
} from "./file-stream.js";
|
|
5
|
-
import { findCsvSafeShards } from "./files.js";
|
|
6
|
-
import { DEFAULT_CHUNK_SIZE } from "./native.js";
|
|
7
|
-
import {
|
|
8
|
-
normalizeColumns,
|
|
9
|
-
normalizeFixedColumnsCount
|
|
10
|
-
} from "./normalize.js";
|
|
11
|
-
import { snapshotCsvOptions } from "./options-snapshot.js";
|
|
12
|
-
import { createWorker } from "./worker-factory.js";
|
|
13
|
-
import {
|
|
14
|
-
toWorkerFilterProgram
|
|
15
|
-
} from "./worker-filter.js";
|
|
16
|
-
import { workerModuleUrl } from "./worker-module.js";
|
|
17
|
-
|
|
18
|
-
export class CsvWorkerPool {
|
|
19
|
-
#path;
|
|
20
|
-
#options;
|
|
21
|
-
#countWorkers;
|
|
22
|
-
#rowsWorkers;
|
|
23
|
-
#shards;
|
|
24
|
-
#cancelActive;
|
|
25
|
-
#closed = !1;
|
|
26
|
-
#busy = !1;
|
|
27
|
-
constructor(path, suppliedOptions) {
|
|
28
|
-
const options = snapshotCsvOptions(suppliedOptions);
|
|
29
|
-
const workerCount = options.workerCount ?? 1;
|
|
30
|
-
if (!Number.isInteger(workerCount) || workerCount <= 1)
|
|
31
|
-
throw RangeError(`worker pool requires workerCount > 1: ${workerCount}`);
|
|
32
|
-
rejectCompressedSharding(options, "worker pool");
|
|
33
|
-
rejectAutoDelimiterSharding(options, "worker pool");
|
|
34
|
-
normalizeFixedColumnsCount(options.fixedColumns, "fixed column count");
|
|
35
|
-
this.#path = path;
|
|
36
|
-
this.#options = options;
|
|
37
|
-
}
|
|
38
|
-
get closed() {
|
|
39
|
-
return this.#closed;
|
|
40
|
-
}
|
|
41
|
-
async count() {
|
|
42
|
-
this.#assertOpen();
|
|
43
|
-
if (this.#options.strict === !0)
|
|
44
|
-
throw Error("parallel count does not support strict CSV validation");
|
|
45
|
-
this.#busy = !0;
|
|
46
|
-
let cancelActive;
|
|
47
|
-
try {
|
|
48
|
-
const filterProgram = toWorkerFilterProgram(this.#options.where);
|
|
49
|
-
const shards = this.#ensureShards();
|
|
50
|
-
if (shards.length === 0)
|
|
51
|
-
return 0;
|
|
52
|
-
const workers = this.#ensureCountWorkers(shards);
|
|
53
|
-
return await new Promise((resolve, reject) => {
|
|
54
|
-
let settled = !1;
|
|
55
|
-
let doneWorkers = 0;
|
|
56
|
-
let total = 0;
|
|
57
|
-
const finish = (value) => {
|
|
58
|
-
if (settled)
|
|
59
|
-
return;
|
|
60
|
-
settled = !0;
|
|
61
|
-
resolve(value);
|
|
62
|
-
};
|
|
63
|
-
const fail = (error) => {
|
|
64
|
-
if (settled)
|
|
65
|
-
return;
|
|
66
|
-
settled = !0;
|
|
67
|
-
this.#invalidateCountWorkers();
|
|
68
|
-
reject(error);
|
|
69
|
-
};
|
|
70
|
-
cancelActive = (error) => fail(error);
|
|
71
|
-
this.#cancelActive = cancelActive;
|
|
72
|
-
try {
|
|
73
|
-
for (const [shardIndex, { shard, worker }] of workers.entries()) {
|
|
74
|
-
if (settled)
|
|
75
|
-
break;
|
|
76
|
-
worker.onmessage = (event) => {
|
|
77
|
-
if (settled)
|
|
78
|
-
return;
|
|
79
|
-
const message = event.data;
|
|
80
|
-
if (message.type === "error") {
|
|
81
|
-
fail(Error(`worker ${message.shardIndex}: ${message.error}`));
|
|
82
|
-
return;
|
|
83
|
-
}
|
|
84
|
-
total += message.rows ?? 0;
|
|
85
|
-
if (!Number.isSafeInteger(total)) {
|
|
86
|
-
fail(RangeError(`parallel row count exceeds Number.MAX_SAFE_INTEGER: ${total}`));
|
|
87
|
-
return;
|
|
88
|
-
}
|
|
89
|
-
++doneWorkers;
|
|
90
|
-
if (doneWorkers === workers.length)
|
|
91
|
-
finish(total);
|
|
92
|
-
};
|
|
93
|
-
worker.onerror = (event) => {
|
|
94
|
-
fail(event.error instanceof Error ? event.error : Error(`worker ${shardIndex} failed`));
|
|
95
|
-
};
|
|
96
|
-
worker.postMessage({
|
|
97
|
-
chunkSize: this.#options.chunkSize ?? DEFAULT_CHUNK_SIZE,
|
|
98
|
-
delimiter: this.#options.delimiter,
|
|
99
|
-
encoding: this.#options.encoding,
|
|
100
|
-
fixedColumns: this.#options.fixedColumns,
|
|
101
|
-
path: this.#path,
|
|
102
|
-
shard,
|
|
103
|
-
shardIndex,
|
|
104
|
-
filterProgram
|
|
105
|
-
});
|
|
106
|
-
}
|
|
107
|
-
} catch (error) {
|
|
108
|
-
fail(error);
|
|
109
|
-
}
|
|
110
|
-
});
|
|
111
|
-
} finally {
|
|
112
|
-
if (this.#cancelActive === cancelActive)
|
|
113
|
-
this.#cancelActive = void 0;
|
|
114
|
-
this.#busy = !1;
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
async* rows() {
|
|
118
|
-
this.#assertOpen();
|
|
119
|
-
rejectWorkerRowsUnsupported(this.#options);
|
|
120
|
-
const filterProgram = toWorkerFilterProgram(this.#options.where);
|
|
121
|
-
this.#busy = !0;
|
|
122
|
-
let cancelActive;
|
|
123
|
-
let completed = !1;
|
|
124
|
-
try {
|
|
125
|
-
const shards = this.#ensureShards();
|
|
126
|
-
if (shards.length === 0)
|
|
127
|
-
return;
|
|
128
|
-
const workers = this.#ensureRowsWorkers(shards);
|
|
129
|
-
const selected = selectedColumns(this.#options);
|
|
130
|
-
const queue = [];
|
|
131
|
-
let pendingResolve;
|
|
132
|
-
const push = (item) => {
|
|
133
|
-
queue.push(item);
|
|
134
|
-
pendingResolve?.();
|
|
135
|
-
pendingResolve = void 0;
|
|
136
|
-
};
|
|
137
|
-
let doneWorkers = 0;
|
|
138
|
-
let failed = !1;
|
|
139
|
-
const fail = (error) => {
|
|
140
|
-
if (failed)
|
|
141
|
-
return;
|
|
142
|
-
failed = !0;
|
|
143
|
-
this.#invalidateRowsWorkers();
|
|
144
|
-
push({ error });
|
|
145
|
-
};
|
|
146
|
-
cancelActive = fail;
|
|
147
|
-
this.#cancelActive = cancelActive;
|
|
148
|
-
try {
|
|
149
|
-
for (const [shardIndex, { shard, worker }] of workers.entries()) {
|
|
150
|
-
if (failed)
|
|
151
|
-
break;
|
|
152
|
-
worker.onmessage = (event) => {
|
|
153
|
-
if (failed)
|
|
154
|
-
return;
|
|
155
|
-
const message = event.data;
|
|
156
|
-
if (message.type === "rows") {
|
|
157
|
-
push({
|
|
158
|
-
rows: message.rows
|
|
159
|
-
});
|
|
160
|
-
return;
|
|
161
|
-
}
|
|
162
|
-
if (message.type === "error") {
|
|
163
|
-
fail(Error(`worker ${message.shardIndex}: ${message.error}`));
|
|
164
|
-
return;
|
|
165
|
-
}
|
|
166
|
-
++doneWorkers;
|
|
167
|
-
if (doneWorkers === workers.length)
|
|
168
|
-
push({ done: !0 });
|
|
169
|
-
};
|
|
170
|
-
worker.onerror = (event) => {
|
|
171
|
-
fail(event.error instanceof Error ? event.error : Error(`worker ${shardIndex} failed`));
|
|
172
|
-
};
|
|
173
|
-
worker.postMessage({
|
|
174
|
-
chunkSize: this.#options.chunkSize ?? DEFAULT_CHUNK_SIZE,
|
|
175
|
-
delimiter: this.#options.delimiter,
|
|
176
|
-
encoding: this.#options.encoding,
|
|
177
|
-
fixedColumns: this.#options.fixedColumns,
|
|
178
|
-
path: this.#path,
|
|
179
|
-
selectedColumns: selected,
|
|
180
|
-
shard,
|
|
181
|
-
shardIndex,
|
|
182
|
-
filterProgram
|
|
183
|
-
});
|
|
184
|
-
}
|
|
185
|
-
} catch (error) {
|
|
186
|
-
fail(error instanceof Error ? error : Error(String(error)));
|
|
187
|
-
}
|
|
188
|
-
while (!0) {
|
|
189
|
-
if (queue.length === 0)
|
|
190
|
-
await new Promise((resolve) => {
|
|
191
|
-
pendingResolve = resolve;
|
|
192
|
-
});
|
|
193
|
-
while (queue.length > 0) {
|
|
194
|
-
const item = queue.shift();
|
|
195
|
-
if (item === void 0)
|
|
196
|
-
continue;
|
|
197
|
-
if ("error" in item)
|
|
198
|
-
throw item.error;
|
|
199
|
-
if ("done" in item) {
|
|
200
|
-
completed = !0;
|
|
201
|
-
return;
|
|
202
|
-
}
|
|
203
|
-
if (item.rows.length > 0)
|
|
204
|
-
yield item.rows;
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
} finally {
|
|
208
|
-
if (!completed)
|
|
209
|
-
this.#invalidateRowsWorkers();
|
|
210
|
-
if (this.#cancelActive === cancelActive)
|
|
211
|
-
this.#cancelActive = void 0;
|
|
212
|
-
this.#busy = !1;
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
close() {
|
|
216
|
-
if (this.#closed)
|
|
217
|
-
return;
|
|
218
|
-
this.#closed = !0;
|
|
219
|
-
this.#cancelActive?.(Error("worker pool is closed"));
|
|
220
|
-
this.#invalidateCountWorkers();
|
|
221
|
-
this.#invalidateRowsWorkers();
|
|
222
|
-
}
|
|
223
|
-
[Symbol.dispose]() {
|
|
224
|
-
this.close();
|
|
225
|
-
}
|
|
226
|
-
#assertOpen() {
|
|
227
|
-
if (this.#closed)
|
|
228
|
-
throw Error("worker pool is closed");
|
|
229
|
-
if (this.#busy)
|
|
230
|
-
throw Error("worker pool is busy");
|
|
231
|
-
}
|
|
232
|
-
#ensureShards() {
|
|
233
|
-
if (this.#shards !== void 0)
|
|
234
|
-
return this.#shards;
|
|
235
|
-
this.#shards = findCsvSafeShards(this.#path, this.#options.workerCount ?? 1, this.#options.delimiter ?? ",");
|
|
236
|
-
return this.#shards;
|
|
237
|
-
}
|
|
238
|
-
#ensureCountWorkers(shards) {
|
|
239
|
-
if (this.#countWorkers !== void 0)
|
|
240
|
-
return this.#countWorkers;
|
|
241
|
-
this.#countWorkers = createShardWorkers(shards, "count");
|
|
242
|
-
return this.#countWorkers;
|
|
243
|
-
}
|
|
244
|
-
#ensureRowsWorkers(shards) {
|
|
245
|
-
if (this.#rowsWorkers !== void 0)
|
|
246
|
-
return this.#rowsWorkers;
|
|
247
|
-
this.#rowsWorkers = createShardWorkers(shards, "rows");
|
|
248
|
-
return this.#rowsWorkers;
|
|
249
|
-
}
|
|
250
|
-
#invalidateCountWorkers() {
|
|
251
|
-
terminateShardWorkers(this.#countWorkers ?? []);
|
|
252
|
-
this.#countWorkers = void 0;
|
|
253
|
-
}
|
|
254
|
-
#invalidateRowsWorkers() {
|
|
255
|
-
terminateShardWorkers(this.#rowsWorkers ?? []);
|
|
256
|
-
this.#rowsWorkers = void 0;
|
|
257
|
-
}
|
|
258
|
-
}
|
|
259
|
-
function createShardWorkers(shards, kind) {
|
|
260
|
-
const workers = [];
|
|
261
|
-
let workersCreated = !1;
|
|
262
|
-
try {
|
|
263
|
-
for (const shard of shards)
|
|
264
|
-
workers.push({
|
|
265
|
-
shard,
|
|
266
|
-
worker: createWorker(workerModuleUrl(kind), {
|
|
267
|
-
preload: [],
|
|
268
|
-
type: "module"
|
|
269
|
-
})
|
|
270
|
-
});
|
|
271
|
-
workersCreated = !0;
|
|
272
|
-
return workers;
|
|
273
|
-
} finally {
|
|
274
|
-
if (!workersCreated)
|
|
275
|
-
terminateShardWorkers(workers);
|
|
276
|
-
}
|
|
277
|
-
}
|
|
278
|
-
function terminateShardWorkers(workers) {
|
|
279
|
-
for (const { worker } of workers) {
|
|
280
|
-
worker.onmessage = null;
|
|
281
|
-
worker.onerror = null;
|
|
282
|
-
worker.terminate();
|
|
283
|
-
}
|
|
284
|
-
}
|
|
285
|
-
export function createWorkerPool(path, options) {
|
|
286
|
-
return new CsvWorkerPool(path, options);
|
|
287
|
-
}
|
|
288
|
-
function selectedColumns(options) {
|
|
289
|
-
if (options.columns !== void 0 && options.selectedColumns !== void 0)
|
|
290
|
-
throw Error("use columns or selectedColumns, not both");
|
|
291
|
-
const columns = options.columns ?? options.selectedColumns;
|
|
292
|
-
normalizeColumns(columns);
|
|
293
|
-
return columns;
|
|
294
|
-
}
|
|
295
|
-
function rejectWorkerRowsUnsupported(options) {
|
|
296
|
-
if (options.strict === !0)
|
|
297
|
-
throw Error("parallel rows do not support strict CSV validation");
|
|
298
|
-
}
|
package/dist/worker-rows.d.ts
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import type { CsvColumns, CsvParallelRowsOptions, CsvProjectedRow } from './types.js';
|
|
2
|
-
/**
|
|
3
|
-
* Stream materialized projected rows through Bun workers.
|
|
4
|
-
*
|
|
5
|
-
* The file is split at CSV record boundaries before workers start. Each
|
|
6
|
-
* yielded batch comes from whichever shard reports next, so batches and rows
|
|
7
|
-
* are not globally ordered by file position. Within a shard, row order is
|
|
8
|
-
* preserved. The worker path evaluates native `where` filters before
|
|
9
|
-
* materializing the selected columns.
|
|
10
|
-
*
|
|
11
|
-
* `workerCount` must be greater than one. Compressed input and automatic
|
|
12
|
-
* delimiter detection are not supported because byte offsets are resolved
|
|
13
|
-
* before workers open the file. Strict validation is also unsupported by the
|
|
14
|
-
* worker parser. Options are snapshotted at invocation, including
|
|
15
|
-
* selected-column and filter arrays.
|
|
16
|
-
*
|
|
17
|
-
* @param path Path to an uncompressed CSV file.
|
|
18
|
-
* @param options Parallel row options. Set `columns` or `selectedColumns` to project physical columns.
|
|
19
|
-
* @returns An async generator of non-empty row batches, delivered as workers finish.
|
|
20
|
-
* @throws {RangeError} When `workerCount` is not an integer greater than one.
|
|
21
|
-
* @throws If compressed/automatic-delimiter input, strict mode, conflicting projections, worker startup, file I/O, or CSV parsing fails during iteration.
|
|
22
|
-
*
|
|
23
|
-
* @example
|
|
24
|
-
* ```ts
|
|
25
|
-
* import { parallelRows } from '@konstit/csv';
|
|
26
|
-
*
|
|
27
|
-
* for await (const rows of parallelRows('data.csv', {
|
|
28
|
-
* columns: [0, 2],
|
|
29
|
-
* delimiter: ';',
|
|
30
|
-
* workerCount: 4,
|
|
31
|
-
* })) {
|
|
32
|
-
* console.log(rows);
|
|
33
|
-
* }
|
|
34
|
-
* ```
|
|
35
|
-
*/
|
|
36
|
-
export declare function parallelRows<TColumns extends CsvColumns | undefined = undefined>(path: string, options: CsvParallelRowsOptions<TColumns>): AsyncGenerator<CsvProjectedRow<TColumns>[], void>;
|
package/dist/worker-rows.js
DELETED
|
@@ -1,127 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
rejectAutoDelimiterSharding,
|
|
3
|
-
rejectCompressedSharding
|
|
4
|
-
} from "./file-stream.js";
|
|
5
|
-
import { findCsvSafeShards } from "./files.js";
|
|
6
|
-
import { DEFAULT_CHUNK_SIZE } from "./native.js";
|
|
7
|
-
import {
|
|
8
|
-
normalizeColumns,
|
|
9
|
-
normalizeFixedColumnsCount
|
|
10
|
-
} from "./normalize.js";
|
|
11
|
-
import { snapshotCsvOptions } from "./options-snapshot.js";
|
|
12
|
-
import { createWorker } from "./worker-factory.js";
|
|
13
|
-
import {
|
|
14
|
-
toWorkerFilterProgram
|
|
15
|
-
} from "./worker-filter.js";
|
|
16
|
-
import { workerModuleUrl } from "./worker-module.js";
|
|
17
|
-
export function parallelRows(path, options) {
|
|
18
|
-
return iterateParallelRows(path, snapshotCsvOptions(options));
|
|
19
|
-
}
|
|
20
|
-
async function* iterateParallelRows(path, options) {
|
|
21
|
-
const workerCount = options.workerCount ?? 1;
|
|
22
|
-
if (!Number.isInteger(workerCount) || workerCount <= 1)
|
|
23
|
-
throw RangeError(`parallel rows require workerCount > 1: ${workerCount}`);
|
|
24
|
-
rejectCompressedSharding(options, "parallel row parsing");
|
|
25
|
-
rejectAutoDelimiterSharding(options, "parallel row parsing");
|
|
26
|
-
rejectWorkerRowsUnsupported(options);
|
|
27
|
-
normalizeFixedColumnsCount(options.fixedColumns, "fixed column count");
|
|
28
|
-
const selected = selectedColumns(options);
|
|
29
|
-
const filterProgram = toWorkerFilterProgram(options.where);
|
|
30
|
-
const shards = findCsvSafeShards(path, workerCount, options.delimiter ?? ",");
|
|
31
|
-
if (shards.length === 0)
|
|
32
|
-
return;
|
|
33
|
-
const shardWorkers = [];
|
|
34
|
-
let workersCreated = !1;
|
|
35
|
-
try {
|
|
36
|
-
for (const shard of shards)
|
|
37
|
-
shardWorkers.push({
|
|
38
|
-
shard,
|
|
39
|
-
worker: createWorker(workerModuleUrl("rows"), {
|
|
40
|
-
preload: [],
|
|
41
|
-
type: "module"
|
|
42
|
-
})
|
|
43
|
-
});
|
|
44
|
-
workersCreated = !0;
|
|
45
|
-
} finally {
|
|
46
|
-
if (!workersCreated)
|
|
47
|
-
for (const { worker } of shardWorkers)
|
|
48
|
-
worker.terminate();
|
|
49
|
-
}
|
|
50
|
-
const queue = [];
|
|
51
|
-
let pendingResolve;
|
|
52
|
-
const push = (item) => {
|
|
53
|
-
queue.push(item);
|
|
54
|
-
pendingResolve?.();
|
|
55
|
-
pendingResolve = void 0;
|
|
56
|
-
};
|
|
57
|
-
let doneWorkers = 0;
|
|
58
|
-
try {
|
|
59
|
-
shardWorkers.forEach(({ shard, worker }, shardIndex) => {
|
|
60
|
-
worker.onmessage = (event) => {
|
|
61
|
-
const message = event.data;
|
|
62
|
-
if (message.type === "rows") {
|
|
63
|
-
push({
|
|
64
|
-
rows: message.rows
|
|
65
|
-
});
|
|
66
|
-
return;
|
|
67
|
-
}
|
|
68
|
-
if (message.type === "error") {
|
|
69
|
-
push({
|
|
70
|
-
error: Error(`worker ${message.shardIndex}: ${message.error}`)
|
|
71
|
-
});
|
|
72
|
-
return;
|
|
73
|
-
}
|
|
74
|
-
++doneWorkers;
|
|
75
|
-
if (doneWorkers === shardWorkers.length)
|
|
76
|
-
push({ done: !0 });
|
|
77
|
-
};
|
|
78
|
-
worker.onerror = (event) => {
|
|
79
|
-
push({
|
|
80
|
-
error: event.error instanceof Error ? event.error : Error(`worker ${shardIndex} failed`)
|
|
81
|
-
});
|
|
82
|
-
};
|
|
83
|
-
worker.postMessage({
|
|
84
|
-
chunkSize: options.chunkSize ?? DEFAULT_CHUNK_SIZE,
|
|
85
|
-
delimiter: options.delimiter,
|
|
86
|
-
encoding: options.encoding,
|
|
87
|
-
fixedColumns: options.fixedColumns,
|
|
88
|
-
path,
|
|
89
|
-
selectedColumns: selected,
|
|
90
|
-
shard,
|
|
91
|
-
shardIndex,
|
|
92
|
-
filterProgram
|
|
93
|
-
});
|
|
94
|
-
});
|
|
95
|
-
while (!0) {
|
|
96
|
-
if (queue.length === 0)
|
|
97
|
-
await new Promise((resolve) => {
|
|
98
|
-
pendingResolve = resolve;
|
|
99
|
-
});
|
|
100
|
-
while (!0) {
|
|
101
|
-
const item = queue.shift();
|
|
102
|
-
if (item === void 0)
|
|
103
|
-
break;
|
|
104
|
-
if ("error" in item)
|
|
105
|
-
throw item.error;
|
|
106
|
-
if ("done" in item)
|
|
107
|
-
return;
|
|
108
|
-
if (item.rows.length > 0)
|
|
109
|
-
yield item.rows;
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
} finally {
|
|
113
|
-
for (const { worker } of shardWorkers)
|
|
114
|
-
worker.terminate();
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
function selectedColumns(options) {
|
|
118
|
-
if (options.columns !== void 0 && options.selectedColumns !== void 0)
|
|
119
|
-
throw Error("use columns or selectedColumns, not both");
|
|
120
|
-
const columns = options.columns ?? options.selectedColumns;
|
|
121
|
-
normalizeColumns(columns);
|
|
122
|
-
return columns;
|
|
123
|
-
}
|
|
124
|
-
function rejectWorkerRowsUnsupported(options) {
|
|
125
|
-
if (options.strict === !0)
|
|
126
|
-
throw Error("parallel rows do not support strict CSV validation");
|
|
127
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import { createReadStream } from "node:fs";
|
|
2
|
-
import { DEFAULT_CHUNK_SIZE } from "../native.js";
|
|
3
|
-
import { createPreparedParser } from "../parser.js";
|
|
4
|
-
addEventListener("message", async (event) => {
|
|
5
|
-
const message = event.data;
|
|
6
|
-
try {
|
|
7
|
-
const rows = await countShard(message);
|
|
8
|
-
postMessage({
|
|
9
|
-
rows,
|
|
10
|
-
shardIndex: message.shardIndex,
|
|
11
|
-
type: "done"
|
|
12
|
-
});
|
|
13
|
-
} catch (error) {
|
|
14
|
-
postMessage({
|
|
15
|
-
error: error instanceof Error ? error.message : String(error),
|
|
16
|
-
shardIndex: message.shardIndex,
|
|
17
|
-
type: "error"
|
|
18
|
-
});
|
|
19
|
-
}
|
|
20
|
-
});
|
|
21
|
-
async function countShard(message) {
|
|
22
|
-
using parser = createPreparedParser({
|
|
23
|
-
delimiter: message.delimiter,
|
|
24
|
-
encoding: message.encoding,
|
|
25
|
-
fixedColumns: message.fixedColumns
|
|
26
|
-
}, void 0, message.filterProgram);
|
|
27
|
-
let rows = 0;
|
|
28
|
-
for await (const chunk of createReadStream(message.path, {
|
|
29
|
-
end: message.shard.end,
|
|
30
|
-
highWaterMark: message.chunkSize ?? DEFAULT_CHUNK_SIZE,
|
|
31
|
-
start: message.shard.start
|
|
32
|
-
}))
|
|
33
|
-
rows += parser.writeCount(chunk);
|
|
34
|
-
rows += parser.endCount();
|
|
35
|
-
return rows;
|
|
36
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
import { createReadStream } from "node:fs";
|
|
2
|
-
import { createPreparedParser } from "../parser.js";
|
|
3
|
-
addEventListener("message", async (event) => {
|
|
4
|
-
const message = event.data;
|
|
5
|
-
try {
|
|
6
|
-
await streamShardRows(message);
|
|
7
|
-
postMessage({
|
|
8
|
-
shardIndex: message.shardIndex,
|
|
9
|
-
type: "done"
|
|
10
|
-
});
|
|
11
|
-
} catch (error) {
|
|
12
|
-
postMessage({
|
|
13
|
-
error: error instanceof Error ? error.message : String(error),
|
|
14
|
-
shardIndex: message.shardIndex,
|
|
15
|
-
type: "error"
|
|
16
|
-
});
|
|
17
|
-
}
|
|
18
|
-
});
|
|
19
|
-
async function streamShardRows(message) {
|
|
20
|
-
using parser = createPreparedParser({
|
|
21
|
-
delimiter: message.delimiter,
|
|
22
|
-
encoding: message.encoding,
|
|
23
|
-
fixedColumns: message.fixedColumns
|
|
24
|
-
}, message.selectedColumns, message.filterProgram);
|
|
25
|
-
const rowsBuffer = [];
|
|
26
|
-
for await (const chunk of createReadStream(message.path, {
|
|
27
|
-
end: message.shard.end,
|
|
28
|
-
highWaterMark: message.chunkSize,
|
|
29
|
-
start: message.shard.start
|
|
30
|
-
})) {
|
|
31
|
-
const batch = parser.writeBatch(chunk);
|
|
32
|
-
try {
|
|
33
|
-
const rows = batch.rowsInto(rowsBuffer);
|
|
34
|
-
if (rows.length > 0)
|
|
35
|
-
postMessage({
|
|
36
|
-
rows,
|
|
37
|
-
shardIndex: message.shardIndex,
|
|
38
|
-
type: "rows"
|
|
39
|
-
});
|
|
40
|
-
} finally {
|
|
41
|
-
batch.close();
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
const batch = parser.endBatch();
|
|
45
|
-
try {
|
|
46
|
-
const rows = batch.rowsInto(rowsBuffer);
|
|
47
|
-
if (rows.length > 0)
|
|
48
|
-
postMessage({
|
|
49
|
-
rows,
|
|
50
|
-
shardIndex: message.shardIndex,
|
|
51
|
-
type: "rows"
|
|
52
|
-
});
|
|
53
|
-
} finally {
|
|
54
|
-
batch.close();
|
|
55
|
-
}
|
|
56
|
-
}
|