convert-buddy-js 0.9.5 → 0.9.6
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 +325 -55
- package/dist/src/browser.d.ts +103 -3
- package/dist/src/browser.js +66 -2
- package/dist/src/browser.js.map +1 -1
- package/dist/src/index.d.ts +112 -2
- package/dist/src/index.js +350 -23
- package/dist/src/index.js.map +1 -1
- package/dist/src/node.d.ts +30 -3
- package/dist/src/node.js +15 -1
- package/dist/src/node.js.map +1 -1
- package/dist/wasm/nodejs/convert_buddy.d.ts +9 -5
- package/dist/wasm/nodejs/convert_buddy.js +28 -19
- package/dist/wasm/nodejs/convert_buddy_bg.wasm +0 -0
- package/dist/wasm/nodejs/convert_buddy_bg.wasm.d.ts +1 -0
- package/dist/wasm/web/convert_buddy.d.ts +10 -5
- package/dist/wasm/web/convert_buddy.js +28 -19
- package/dist/wasm/web/convert_buddy_bg.wasm +0 -0
- package/dist/wasm/web/convert_buddy_bg.wasm.d.ts +1 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
> ⚠️ **In Development** - This project is currently under active development and subject to breaking changes. Experimental state, could change heavily.
|
|
2
|
+
|
|
1
3
|
# convert-buddy-js
|
|
2
4
|
|
|
3
5
|
A high-performance, streaming-first parser and converter for CSV, XML, NDJSON, and JSON. `convert-buddy-js` is a TypeScript wrapper around a Rust/WASM core, offering fast parsing and multiple usage styles for Node.js and browsers.
|
|
@@ -18,6 +20,51 @@ npm install convert-buddy-js
|
|
|
18
20
|
|
|
19
21
|
## Quick Start
|
|
20
22
|
|
|
23
|
+
### The Simplest Way - Auto-Detect Everything
|
|
24
|
+
|
|
25
|
+
```ts
|
|
26
|
+
import { convert, convertToString } from "convert-buddy-js";
|
|
27
|
+
|
|
28
|
+
// From URL (auto-detects format)
|
|
29
|
+
const result = await convertToString("https://example.com/data.csv", {
|
|
30
|
+
outputFormat: "json"
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
// From File (browser)
|
|
34
|
+
const file = fileInput.files[0];
|
|
35
|
+
const json = await convertToString(file, { outputFormat: "json" });
|
|
36
|
+
|
|
37
|
+
// From string data
|
|
38
|
+
const csv = "name,age\nAda,36";
|
|
39
|
+
const ndjson = await convertToString(csv, { outputFormat: "ndjson" });
|
|
40
|
+
|
|
41
|
+
// Returns Uint8Array instead of string
|
|
42
|
+
const bytes = await convert(file, { outputFormat: "json" });
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
### Instance-Based API with Global Config
|
|
46
|
+
|
|
47
|
+
```ts
|
|
48
|
+
import { ConvertBuddy } from "convert-buddy-js";
|
|
49
|
+
|
|
50
|
+
// Create a buddy with global settings
|
|
51
|
+
const buddy = new ConvertBuddy({
|
|
52
|
+
maxMemoryMB: 512,
|
|
53
|
+
debug: true,
|
|
54
|
+
onProgress: (stats) => console.log(`${stats.recordsProcessed} records`)
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
// Convert anything - URL, File, Buffer, string, stream
|
|
58
|
+
const result = await buddy.convert("https://example.com/data.csv", {
|
|
59
|
+
outputFormat: "json"
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
const result2 = await buddy.convert(file, {
|
|
63
|
+
inputFormat: "csv", // optional, auto-detected if omitted
|
|
64
|
+
outputFormat: "ndjson"
|
|
65
|
+
});
|
|
66
|
+
```
|
|
67
|
+
|
|
21
68
|
### Browser - Convert File Input
|
|
22
69
|
|
|
23
70
|
```ts
|
|
@@ -37,18 +84,98 @@ fileInput.addEventListener('change', async (e) => {
|
|
|
37
84
|
### Node.js - Convert File
|
|
38
85
|
|
|
39
86
|
```ts
|
|
40
|
-
import {
|
|
87
|
+
import { convert } from "convert-buddy-js/node";
|
|
41
88
|
|
|
42
|
-
|
|
43
|
-
|
|
89
|
+
// From file path (auto-detects format)
|
|
90
|
+
const result = await convert("input.csv", {
|
|
44
91
|
outputFormat: "json"
|
|
45
92
|
});
|
|
46
|
-
|
|
93
|
+
|
|
94
|
+
// From URL
|
|
95
|
+
const result2 = await convert("https://api.example.com/data.csv", {
|
|
96
|
+
outputFormat: "ndjson"
|
|
97
|
+
});
|
|
47
98
|
```
|
|
48
99
|
|
|
49
100
|
## Usage
|
|
50
101
|
|
|
51
|
-
|
|
102
|
+
Convert Buddy offers three API styles, from simplest to most powerful:
|
|
103
|
+
|
|
104
|
+
### 1. Ultra-Simple API (Recommended for Most Use Cases)
|
|
105
|
+
|
|
106
|
+
The easiest way to convert data. Just pass anything and specify the output format:
|
|
107
|
+
|
|
108
|
+
```ts
|
|
109
|
+
import { convert, convertToString } from "convert-buddy-js";
|
|
110
|
+
// Or platform-specific: "convert-buddy-js/browser" or "convert-buddy-js/node"
|
|
111
|
+
|
|
112
|
+
// Auto-detects input type AND format
|
|
113
|
+
const json = await convertToString(input, { outputFormat: "json" });
|
|
114
|
+
|
|
115
|
+
// Works with:
|
|
116
|
+
// - URLs: "https://example.com/data.csv"
|
|
117
|
+
// - Files: file from <input type="file">
|
|
118
|
+
// - Buffers: Uint8Array or Buffer
|
|
119
|
+
// - Strings: raw CSV, JSON, XML, NDJSON data
|
|
120
|
+
// - Streams: ReadableStream<Uint8Array>
|
|
121
|
+
// - File paths (Node.js): "path/to/data.csv"
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
**Examples:**
|
|
125
|
+
|
|
126
|
+
```ts
|
|
127
|
+
// From URL
|
|
128
|
+
const json = await convertToString("https://example.com/data.csv", {
|
|
129
|
+
outputFormat: "json"
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
// From file upload (browser)
|
|
133
|
+
const file = document.querySelector('input[type="file"]').files[0];
|
|
134
|
+
const ndjson = await convertToString(file, { outputFormat: "ndjson" });
|
|
135
|
+
|
|
136
|
+
// From file path (Node.js)
|
|
137
|
+
import { convertToString } from "convert-buddy-js/node";
|
|
138
|
+
const result = await convertToString("./data.csv", { outputFormat: "json" });
|
|
139
|
+
|
|
140
|
+
// From string data
|
|
141
|
+
const csvString = "name,age\nAda,36\nLinus,54";
|
|
142
|
+
const json = await convertToString(csvString, { outputFormat: "json" });
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
### 2. Instance-Based API (For Reusable Configuration)
|
|
146
|
+
|
|
147
|
+
Create a ConvertBuddy instance with global settings, then convert multiple inputs:
|
|
148
|
+
|
|
149
|
+
```ts
|
|
150
|
+
import { ConvertBuddy } from "convert-buddy-js";
|
|
151
|
+
|
|
152
|
+
const buddy = new ConvertBuddy({
|
|
153
|
+
maxMemoryMB: 512, // Future: memory limits
|
|
154
|
+
debug: true, // Enable debug logging
|
|
155
|
+
profile: true, // Show performance stats
|
|
156
|
+
onProgress: (stats) => {
|
|
157
|
+
console.log(`${stats.recordsProcessed} records processed`);
|
|
158
|
+
console.log(`${stats.throughputMbPerSec.toFixed(2)} MB/s`);
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
// Convert different inputs with the same config
|
|
163
|
+
const result1 = await buddy.convert("https://example.com/data.csv", {
|
|
164
|
+
outputFormat: "json"
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
const result2 = await buddy.convert(file, {
|
|
168
|
+
outputFormat: "ndjson",
|
|
169
|
+
csvConfig: { delimiter: ";" } // Override per-conversion
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
// Decode to string
|
|
173
|
+
const jsonString = new TextDecoder().decode(result1);
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
### 3. High-Level API (Platform-Specific Helpers)
|
|
177
|
+
|
|
178
|
+
Platform-specific helpers for common use cases:
|
|
52
179
|
|
|
53
180
|
#### Browser Helpers
|
|
54
181
|
|
|
@@ -59,7 +186,14 @@ import {
|
|
|
59
186
|
convertFileToString,
|
|
60
187
|
convertFile,
|
|
61
188
|
convertFileToFile,
|
|
62
|
-
convertFileStream
|
|
189
|
+
convertFileStream,
|
|
190
|
+
convertAndSave,
|
|
191
|
+
convertStreamToWritable,
|
|
192
|
+
autoConvertStream,
|
|
193
|
+
isFileSystemAccessSupported,
|
|
194
|
+
getMimeType,
|
|
195
|
+
getExtension,
|
|
196
|
+
getSuggestedFilename
|
|
63
197
|
} from "convert-buddy-js/browser";
|
|
64
198
|
|
|
65
199
|
// Convert to string
|
|
@@ -68,17 +202,50 @@ const json = await convertFileToString(file, {
|
|
|
68
202
|
outputFormat: "json"
|
|
69
203
|
});
|
|
70
204
|
|
|
71
|
-
// Convert and download
|
|
205
|
+
// Convert and download (legacy browser support)
|
|
72
206
|
await convertFileToFile(file, "output.json", {
|
|
73
207
|
inputFormat: "csv",
|
|
74
208
|
outputFormat: "json"
|
|
75
209
|
});
|
|
76
210
|
|
|
211
|
+
// Convert and save with File System Access API (better UX)
|
|
212
|
+
// User chooses save location, no automatic downloads
|
|
213
|
+
await convertAndSave(file, {
|
|
214
|
+
inputFormat: "csv",
|
|
215
|
+
outputFormat: "json",
|
|
216
|
+
suggestedName: "output.json"
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
// Auto-detect format and convert
|
|
220
|
+
const stream = await autoConvertStream(file, {
|
|
221
|
+
outputFormat: "json"
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
// Stream to File System Access API writable
|
|
225
|
+
if (isFileSystemAccessSupported()) {
|
|
226
|
+
const handle = await window.showSaveFilePicker({
|
|
227
|
+
suggestedName: "output.json"
|
|
228
|
+
});
|
|
229
|
+
const writable = await handle.createWritable();
|
|
230
|
+
|
|
231
|
+
await convertStreamToWritable(file, writable, {
|
|
232
|
+
inputFormat: "csv",
|
|
233
|
+
outputFormat: "json",
|
|
234
|
+
onProgress: (stats) => console.log(`${stats.bytesIn} bytes processed`)
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
|
|
77
238
|
// Get streaming API
|
|
78
239
|
const stream = await convertFileStream(file, {
|
|
79
240
|
inputFormat: "csv",
|
|
80
241
|
outputFormat: "ndjson"
|
|
81
242
|
});
|
|
243
|
+
|
|
244
|
+
// Format helpers
|
|
245
|
+
const mimeType = getMimeType("json"); // "application/json"
|
|
246
|
+
const extension = getExtension("csv"); // "csv"
|
|
247
|
+
const filename = getSuggestedFilename("data.csv", "json"); // "data.json"
|
|
248
|
+
|
|
82
249
|
```
|
|
83
250
|
|
|
84
251
|
#### Node.js Helpers
|
|
@@ -112,55 +279,9 @@ const result = await convertStream(inputStream, {
|
|
|
112
279
|
});
|
|
113
280
|
```
|
|
114
281
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
Monitor long-running conversions and allow cancellation:
|
|
118
|
-
|
|
119
|
-
```ts
|
|
120
|
-
const buddy = await ConvertBuddy.create({
|
|
121
|
-
inputFormat: "csv",
|
|
122
|
-
outputFormat: "json",
|
|
123
|
-
onProgress: (stats) => {
|
|
124
|
-
console.log(`${stats.recordsProcessed} records processed`);
|
|
125
|
-
console.log(`${stats.throughputMbPerSec.toFixed(2)} MB/s`);
|
|
126
|
-
},
|
|
127
|
-
progressIntervalBytes: 1024 * 1024 // Every 1MB
|
|
128
|
-
});
|
|
129
|
-
|
|
130
|
-
// User cancels
|
|
131
|
-
cancelButton.addEventListener('click', () => buddy.abort());
|
|
132
|
-
```
|
|
133
|
-
|
|
134
|
-
#### Auto-Detection
|
|
282
|
+
### 4. Low-Level API (Advanced Use Cases)
|
|
135
283
|
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
```ts
|
|
139
|
-
const result = await convertFileToString(file, {
|
|
140
|
-
inputFormat: "auto", // Auto-detect CSV, JSON, NDJSON, XML
|
|
141
|
-
outputFormat: "json",
|
|
142
|
-
csvConfig: { // Optional: still apply config
|
|
143
|
-
delimiter: ","
|
|
144
|
-
}
|
|
145
|
-
});
|
|
146
|
-
```
|
|
147
|
-
|
|
148
|
-
### Low-Level API
|
|
149
|
-
|
|
150
|
-
#### Convert a full string or buffer
|
|
151
|
-
|
|
152
|
-
```ts
|
|
153
|
-
import { convertToString } from "convert-buddy-js";
|
|
154
|
-
|
|
155
|
-
const csv = `name,age\nAda,36\nLinus,54`;
|
|
156
|
-
|
|
157
|
-
const output = await convertToString(csv, {
|
|
158
|
-
inputFormat: "csv",
|
|
159
|
-
outputFormat: "ndjson",
|
|
160
|
-
});
|
|
161
|
-
|
|
162
|
-
console.log(output);
|
|
163
|
-
```
|
|
284
|
+
For maximum control over the conversion process:
|
|
164
285
|
|
|
165
286
|
#### Manual streaming (chunked)
|
|
166
287
|
|
|
@@ -213,6 +334,155 @@ const response = await fetch("/data.csv");
|
|
|
213
334
|
const outputStream = response.body?.pipeThrough(transform);
|
|
214
335
|
```
|
|
215
336
|
|
|
337
|
+
---
|
|
338
|
+
|
|
339
|
+
### Additional Features
|
|
340
|
+
|
|
341
|
+
#### Progress Tracking & Control
|
|
342
|
+
|
|
343
|
+
Monitor long-running conversions and allow cancellation:
|
|
344
|
+
|
|
345
|
+
```ts
|
|
346
|
+
import { ConvertBuddy } from "convert-buddy-js";
|
|
347
|
+
|
|
348
|
+
const buddy = new ConvertBuddy({
|
|
349
|
+
onProgress: (stats) => {
|
|
350
|
+
console.log(`${stats.recordsProcessed} records processed`);
|
|
351
|
+
console.log(`${stats.throughputMbPerSec.toFixed(2)} MB/s`);
|
|
352
|
+
},
|
|
353
|
+
progressIntervalBytes: 1024 * 1024 // Every 1MB
|
|
354
|
+
});
|
|
355
|
+
|
|
356
|
+
const result = await buddy.convert(largeFile, { outputFormat: "json" });
|
|
357
|
+
|
|
358
|
+
// Or with the low-level API
|
|
359
|
+
const converter = await ConvertBuddy.create({
|
|
360
|
+
inputFormat: "csv",
|
|
361
|
+
outputFormat: "json",
|
|
362
|
+
onProgress: (stats) => {
|
|
363
|
+
console.log(`${stats.recordsProcessed} records processed`);
|
|
364
|
+
console.log(`${stats.throughputMbPerSec.toFixed(2)} MB/s`);
|
|
365
|
+
},
|
|
366
|
+
progressIntervalBytes: 1024 * 1024
|
|
367
|
+
});
|
|
368
|
+
|
|
369
|
+
// User cancels
|
|
370
|
+
cancelButton.addEventListener('click', () => converter.abort());
|
|
371
|
+
```
|
|
372
|
+
|
|
373
|
+
#### Format Utilities
|
|
374
|
+
|
|
375
|
+
Helper functions for working with file formats, MIME types, and extensions:
|
|
376
|
+
|
|
377
|
+
```ts
|
|
378
|
+
import {
|
|
379
|
+
getMimeType,
|
|
380
|
+
getExtension,
|
|
381
|
+
getSuggestedFilename,
|
|
382
|
+
getFileTypeConfig
|
|
383
|
+
} from "convert-buddy-js";
|
|
384
|
+
|
|
385
|
+
// Get MIME type for a format
|
|
386
|
+
const mimeType = getMimeType("json"); // "application/json"
|
|
387
|
+
|
|
388
|
+
// Get file extension
|
|
389
|
+
const ext = getExtension("ndjson"); // "ndjson"
|
|
390
|
+
|
|
391
|
+
// Generate output filename
|
|
392
|
+
const filename = getSuggestedFilename("data.csv", "json");
|
|
393
|
+
// "data.json"
|
|
394
|
+
|
|
395
|
+
const timestamped = getSuggestedFilename("data.csv", "json", true);
|
|
396
|
+
// "data_converted_1234567890.json"
|
|
397
|
+
|
|
398
|
+
// Get File System Access API config
|
|
399
|
+
const types = getFileTypeConfig("json");
|
|
400
|
+
// [{ description: "JSON Files", accept: { "application/json": [".json"] } }]
|
|
401
|
+
|
|
402
|
+
const handle = await window.showSaveFilePicker({
|
|
403
|
+
suggestedName: "output.json",
|
|
404
|
+
types
|
|
405
|
+
});
|
|
406
|
+
```
|
|
407
|
+
|
|
408
|
+
#### Advanced Browser Streaming
|
|
409
|
+
|
|
410
|
+
For maximum efficiency with large files, use streaming APIs to avoid loading entire files into memory:
|
|
411
|
+
|
|
412
|
+
```ts
|
|
413
|
+
import {
|
|
414
|
+
convertStreamToWritable,
|
|
415
|
+
autoConvertStream,
|
|
416
|
+
isFileSystemAccessSupported
|
|
417
|
+
} from "convert-buddy-js/browser";
|
|
418
|
+
|
|
419
|
+
// Auto-detect and stream conversion
|
|
420
|
+
const outputStream = await autoConvertStream(file, {
|
|
421
|
+
outputFormat: "json",
|
|
422
|
+
onProgress: (stats) => {
|
|
423
|
+
console.log(`Progress: ${stats.bytesIn} bytes in, ${stats.bytesOut} bytes out`);
|
|
424
|
+
console.log(`Throughput: ${stats.throughputMbPerSec.toFixed(2)} MB/s`);
|
|
425
|
+
}
|
|
426
|
+
});
|
|
427
|
+
|
|
428
|
+
// Stream directly to File System Access API writable
|
|
429
|
+
if (isFileSystemAccessSupported()) {
|
|
430
|
+
const fileHandle = await window.showSaveFilePicker({
|
|
431
|
+
suggestedName: "output.json",
|
|
432
|
+
types: [{
|
|
433
|
+
description: "JSON Files",
|
|
434
|
+
accept: { "application/json": [".json"] }
|
|
435
|
+
}]
|
|
436
|
+
});
|
|
437
|
+
|
|
438
|
+
const writable = await fileHandle.createWritable();
|
|
439
|
+
|
|
440
|
+
await convertStreamToWritable(file, writable, {
|
|
441
|
+
inputFormat: "csv",
|
|
442
|
+
outputFormat: "json"
|
|
443
|
+
});
|
|
444
|
+
|
|
445
|
+
console.log("Conversion complete!");
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
// Or pipe to any WritableStream
|
|
449
|
+
const customWritable = new WritableStream({
|
|
450
|
+
write(chunk) {
|
|
451
|
+
// Process each output chunk
|
|
452
|
+
console.log("Received chunk:", chunk);
|
|
453
|
+
}
|
|
454
|
+
});
|
|
455
|
+
|
|
456
|
+
await convertStreamToWritable(file, customWritable, {
|
|
457
|
+
inputFormat: "auto",
|
|
458
|
+
outputFormat: "ndjson"
|
|
459
|
+
});
|
|
460
|
+
```
|
|
461
|
+
|
|
462
|
+
#### Auto-Detection
|
|
463
|
+
|
|
464
|
+
Auto-detection is enabled by default with the new API. You can also use it explicitly:
|
|
465
|
+
|
|
466
|
+
```ts
|
|
467
|
+
import { convert } from "convert-buddy-js";
|
|
468
|
+
|
|
469
|
+
// Automatic (default)
|
|
470
|
+
const result = await convert(input, { outputFormat: "json" });
|
|
471
|
+
|
|
472
|
+
// Explicit auto-detection
|
|
473
|
+
const result2 = await convert(input, {
|
|
474
|
+
inputFormat: "auto",
|
|
475
|
+
outputFormat: "json",
|
|
476
|
+
csvConfig: { delimiter: "," } // Optional: still apply config
|
|
477
|
+
});
|
|
478
|
+
|
|
479
|
+
// Or specify the format
|
|
480
|
+
const result3 = await convert(input, {
|
|
481
|
+
inputFormat: "csv",
|
|
482
|
+
outputFormat: "json"
|
|
483
|
+
});
|
|
484
|
+
```
|
|
485
|
+
|
|
216
486
|
#### Detect format and CSV fields/delimiter
|
|
217
487
|
|
|
218
488
|
Use streaming inputs to keep detection fast on large files.
|
package/dist/src/browser.d.ts
CHANGED
|
@@ -1,7 +1,33 @@
|
|
|
1
|
-
import { ConvertBuddyOptions } from './index.js';
|
|
2
|
-
export { ConvertBuddy, ConvertBuddyTransformStream, CsvConfig, CsvDetection, DetectInput, DetectOptions,
|
|
1
|
+
import { ConvertBuddyOptions, ConvertOptions, Format } from './index.js';
|
|
2
|
+
export { ConvertBuddy, ConvertBuddyTransformStream, CsvConfig, CsvDetection, DetectInput, DetectOptions, ProgressCallback, Stats, XmlConfig, XmlDetection, autoDetectConfig, convertAny, convertAnyToString, detectCsvFieldsAndDelimiter, detectFormat, detectXmlElements, getExtension, getFileTypeConfig, getMimeType, getSuggestedFilename } from './index.js';
|
|
3
3
|
|
|
4
4
|
type BrowserConvertOptions = ConvertBuddyOptions & {};
|
|
5
|
+
/**
|
|
6
|
+
* Ultra-simple convert function for browser.
|
|
7
|
+
* Auto-detects input type (File, URL, string, etc.) and format.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* // From File
|
|
11
|
+
* const file = fileInput.files[0];
|
|
12
|
+
* const result = await convert(file, { outputFormat: "json" });
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* // From URL
|
|
16
|
+
* const result = await convert("https://example.com/data.csv", { outputFormat: "json" });
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* // From string
|
|
20
|
+
* const result = await convert(csvString, { outputFormat: "json" });
|
|
21
|
+
*/
|
|
22
|
+
declare function convert(input: string | Uint8Array | File | Blob | ReadableStream<Uint8Array>, opts: ConvertOptions): Promise<Uint8Array>;
|
|
23
|
+
/**
|
|
24
|
+
* Ultra-simple convert function that returns a string.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* const json = await convertToString(file, { outputFormat: "json" });
|
|
28
|
+
* console.log(JSON.parse(json));
|
|
29
|
+
*/
|
|
30
|
+
declare function convertToString(input: string | Uint8Array | File | Blob | ReadableStream<Uint8Array>, opts: ConvertOptions): Promise<string>;
|
|
5
31
|
/**
|
|
6
32
|
* Convert a browser File or Blob object to a string.
|
|
7
33
|
* Handles streaming internally using FileReader and web streams.
|
|
@@ -70,5 +96,79 @@ declare function convertFileToFile(inputFile: File | Blob, outputFilename: strin
|
|
|
70
96
|
* }
|
|
71
97
|
*/
|
|
72
98
|
declare function convertFileStream(file: File | Blob, opts?: BrowserConvertOptions): Promise<ReadableStream<Uint8Array>>;
|
|
99
|
+
/**
|
|
100
|
+
* Auto-detect format and create a ReadableStream for conversion.
|
|
101
|
+
* This is a convenience helper that combines format detection with streaming conversion.
|
|
102
|
+
*
|
|
103
|
+
* @example
|
|
104
|
+
* const file = fileInput.files[0];
|
|
105
|
+
* const stream = await autoConvertStream(file, { outputFormat: "json" });
|
|
106
|
+
*
|
|
107
|
+
* // Pipe to response
|
|
108
|
+
* return new Response(stream);
|
|
109
|
+
*/
|
|
110
|
+
declare function autoConvertStream(file: File | Blob, opts: Omit<BrowserConvertOptions, "inputFormat"> & {
|
|
111
|
+
outputFormat: Format;
|
|
112
|
+
}): Promise<ReadableStream<Uint8Array>>;
|
|
113
|
+
/**
|
|
114
|
+
* Stream a file conversion directly to a writable destination.
|
|
115
|
+
* This is useful for streaming large conversions to disk using File System Access API
|
|
116
|
+
* or to other writable streams.
|
|
117
|
+
*
|
|
118
|
+
* @example
|
|
119
|
+
* // Using File System Access API
|
|
120
|
+
* const fileInput = document.querySelector('input[type="file"]');
|
|
121
|
+
* const file = fileInput.files[0];
|
|
122
|
+
*
|
|
123
|
+
* const fileHandle = await window.showSaveFilePicker({
|
|
124
|
+
* suggestedName: "output.json",
|
|
125
|
+
* types: [{ description: "JSON", accept: { "application/json": [".json"] } }]
|
|
126
|
+
* });
|
|
127
|
+
* const writable = await fileHandle.createWritable();
|
|
128
|
+
*
|
|
129
|
+
* await convertStreamToWritable(file, writable, {
|
|
130
|
+
* inputFormat: "csv",
|
|
131
|
+
* outputFormat: "json",
|
|
132
|
+
* onProgress: (stats) => console.log(`${stats.bytesIn} bytes processed`)
|
|
133
|
+
* });
|
|
134
|
+
*
|
|
135
|
+
* @example
|
|
136
|
+
* // With auto-detection
|
|
137
|
+
* await convertStreamToWritable(file, writable, {
|
|
138
|
+
* inputFormat: "auto",
|
|
139
|
+
* outputFormat: "ndjson"
|
|
140
|
+
* });
|
|
141
|
+
*/
|
|
142
|
+
declare function convertStreamToWritable(file: File | Blob, writable: WritableStream<Uint8Array> | FileSystemWritableFileStream, opts?: BrowserConvertOptions): Promise<void>;
|
|
143
|
+
/**
|
|
144
|
+
* Check if File System Access API is supported in the current browser.
|
|
145
|
+
* Use this to determine if you can use File System Access API features.
|
|
146
|
+
*
|
|
147
|
+
* @example
|
|
148
|
+
* if (isFileSystemAccessSupported()) {
|
|
149
|
+
* // Use File System Access API
|
|
150
|
+
* const handle = await window.showSaveFilePicker();
|
|
151
|
+
* } else {
|
|
152
|
+
* // Fall back to download link
|
|
153
|
+
* await convertFileToFile(file, "output.json", opts);
|
|
154
|
+
* }
|
|
155
|
+
*/
|
|
156
|
+
declare function isFileSystemAccessSupported(): boolean;
|
|
157
|
+
/**
|
|
158
|
+
* Convert a file and save it using File System Access API with a user-selected location.
|
|
159
|
+
* This provides a better UX than automatic downloads by letting users choose where to save.
|
|
160
|
+
* Falls back to regular download if File System Access API is not supported.
|
|
161
|
+
*
|
|
162
|
+
* @example
|
|
163
|
+
* const file = fileInput.files[0];
|
|
164
|
+
* await convertAndSave(file, {
|
|
165
|
+
* inputFormat: "csv",
|
|
166
|
+
* outputFormat: "json",
|
|
167
|
+
* suggestedName: "output.json"
|
|
168
|
+
* });
|
|
169
|
+
*/
|
|
170
|
+
declare function convertAndSave(file: File | Blob, opts?: BrowserConvertOptions & {
|
|
171
|
+
suggestedName?: string;
|
|
172
|
+
}): Promise<void>;
|
|
73
173
|
|
|
74
|
-
export { type BrowserConvertOptions, ConvertBuddyOptions, convertFile, convertFileStream, convertFileToFile, convertFileToString };
|
|
174
|
+
export { type BrowserConvertOptions, ConvertBuddyOptions, ConvertOptions, Format, autoConvertStream, convert, convertAndSave, convertFile, convertFileStream, convertFileToFile, convertFileToString, convertStreamToWritable, convertToString, isFileSystemAccessSupported };
|
package/dist/src/browser.js
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
|
-
import { ConvertBuddy, autoDetectConfig } from "./index.js";
|
|
1
|
+
import { ConvertBuddy, autoDetectConfig, getFileTypeConfig, convertAny as convertAnyCore, convertAnyToString as convertAnyToStringCore } from "./index.js";
|
|
2
2
|
export * from "./index.js";
|
|
3
|
+
async function convert(input, opts) {
|
|
4
|
+
return convertAnyCore(input, opts);
|
|
5
|
+
}
|
|
6
|
+
async function convertToString(input, opts) {
|
|
7
|
+
return convertAnyToStringCore(input, opts);
|
|
8
|
+
}
|
|
3
9
|
async function convertFileToString(file, opts = {}) {
|
|
4
10
|
const bytes = await convertFile(file, opts);
|
|
5
11
|
return new TextDecoder().decode(bytes);
|
|
@@ -27,6 +33,22 @@ async function convertFile(file, opts = {}) {
|
|
|
27
33
|
throw new Error("Could not auto-detect input format. Please specify inputFormat explicitly.");
|
|
28
34
|
}
|
|
29
35
|
}
|
|
36
|
+
if (!actualOpts.chunkTargetBytes) {
|
|
37
|
+
const fileSize = file.size;
|
|
38
|
+
actualOpts.chunkTargetBytes = Math.max(
|
|
39
|
+
512 * 1024,
|
|
40
|
+
// minimum: 512KB
|
|
41
|
+
Math.min(
|
|
42
|
+
1 * 1024 * 1024,
|
|
43
|
+
// maximum: 1MB (conservative for memory)
|
|
44
|
+
Math.floor(fileSize / 16)
|
|
45
|
+
// 1/16 of file size
|
|
46
|
+
)
|
|
47
|
+
);
|
|
48
|
+
if (opts.debug) {
|
|
49
|
+
console.log(`[convert-buddy] Adaptive chunk sizing: ${fileSize} bytes \u2192 ${actualOpts.chunkTargetBytes} byte chunks`);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
30
52
|
const buddy = await ConvertBuddy.create(actualOpts);
|
|
31
53
|
const stream = file.stream();
|
|
32
54
|
const reader = stream.getReader();
|
|
@@ -131,10 +153,52 @@ async function convertFileStream(file, opts = {}) {
|
|
|
131
153
|
}
|
|
132
154
|
});
|
|
133
155
|
}
|
|
156
|
+
async function autoConvertStream(file, opts) {
|
|
157
|
+
return convertFileStream(file, {
|
|
158
|
+
...opts,
|
|
159
|
+
inputFormat: "auto"
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
async function convertStreamToWritable(file, writable, opts = {}) {
|
|
163
|
+
const outputStream = await convertFileStream(file, opts);
|
|
164
|
+
await outputStream.pipeTo(writable);
|
|
165
|
+
}
|
|
166
|
+
function isFileSystemAccessSupported() {
|
|
167
|
+
return typeof window !== "undefined" && "showSaveFilePicker" in window && "showOpenFilePicker" in window;
|
|
168
|
+
}
|
|
169
|
+
async function convertAndSave(file, opts = {}) {
|
|
170
|
+
if (!isFileSystemAccessSupported()) {
|
|
171
|
+
const filename = opts.suggestedName || "converted";
|
|
172
|
+
await convertFileToFile(file, filename, opts);
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
const outputFormat = opts.outputFormat || "json";
|
|
176
|
+
const suggestedName = opts.suggestedName || `converted.${outputFormat}`;
|
|
177
|
+
const types = getFileTypeConfig(outputFormat);
|
|
178
|
+
try {
|
|
179
|
+
const fileHandle = await window.showSaveFilePicker({
|
|
180
|
+
suggestedName,
|
|
181
|
+
types
|
|
182
|
+
});
|
|
183
|
+
const writable = await fileHandle.createWritable();
|
|
184
|
+
await convertStreamToWritable(file, writable, opts);
|
|
185
|
+
} catch (error) {
|
|
186
|
+
if (error?.name === "AbortError") {
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
throw error;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
134
192
|
export {
|
|
193
|
+
autoConvertStream,
|
|
194
|
+
convert,
|
|
195
|
+
convertAndSave,
|
|
135
196
|
convertFile,
|
|
136
197
|
convertFileStream,
|
|
137
198
|
convertFileToFile,
|
|
138
|
-
convertFileToString
|
|
199
|
+
convertFileToString,
|
|
200
|
+
convertStreamToWritable,
|
|
201
|
+
convertToString,
|
|
202
|
+
isFileSystemAccessSupported
|
|
139
203
|
};
|
|
140
204
|
//# sourceMappingURL=browser.js.map
|