convert-buddy-js 0.9.4 → 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.
Files changed (37) hide show
  1. package/README.md +417 -10
  2. package/dist/src/browser.d.ts +174 -0
  3. package/dist/src/browser.js +204 -0
  4. package/dist/src/browser.js.map +1 -0
  5. package/dist/src/index.d.ts +133 -3
  6. package/dist/src/index.js +427 -26
  7. package/dist/src/index.js.map +1 -1
  8. package/dist/src/node.d.ts +111 -3
  9. package/dist/src/node.js +284 -2
  10. package/dist/src/node.js.map +1 -1
  11. package/dist/tests/edge-cases/control-features.test.d.ts +2 -0
  12. package/dist/tests/edge-cases/control-features.test.js +87 -0
  13. package/dist/tests/edge-cases/control-features.test.js.map +1 -0
  14. package/dist/tests/edge-cases/csv-edge-cases.test.d.ts +2 -0
  15. package/dist/tests/edge-cases/csv-edge-cases.test.js +547 -0
  16. package/dist/tests/edge-cases/csv-edge-cases.test.js.map +1 -0
  17. package/dist/tests/edge-cases/detection.test.d.ts +2 -0
  18. package/dist/tests/edge-cases/detection.test.js +129 -0
  19. package/dist/tests/edge-cases/detection.test.js.map +1 -0
  20. package/dist/tests/edge-cases/error-handling.test.d.ts +2 -0
  21. package/dist/tests/edge-cases/error-handling.test.js +448 -0
  22. package/dist/tests/edge-cases/error-handling.test.js.map +1 -0
  23. package/dist/tests/edge-cases/ndjson-edge-cases.test.d.ts +2 -0
  24. package/dist/tests/edge-cases/ndjson-edge-cases.test.js +539 -0
  25. package/dist/tests/edge-cases/ndjson-edge-cases.test.js.map +1 -0
  26. package/dist/tests/edge-cases/node-helpers.test.d.ts +2 -0
  27. package/dist/tests/edge-cases/node-helpers.test.js +114 -0
  28. package/dist/tests/edge-cases/node-helpers.test.js.map +1 -0
  29. package/dist/wasm/nodejs/convert_buddy.d.ts +9 -5
  30. package/dist/wasm/nodejs/convert_buddy.js +28 -19
  31. package/dist/wasm/nodejs/convert_buddy_bg.wasm +0 -0
  32. package/dist/wasm/nodejs/convert_buddy_bg.wasm.d.ts +1 -0
  33. package/dist/wasm/web/convert_buddy.d.ts +10 -5
  34. package/dist/wasm/web/convert_buddy.js +28 -19
  35. package/dist/wasm/web/convert_buddy_bg.wasm +0 -0
  36. package/dist/wasm/web/convert_buddy_bg.wasm.d.ts +1 -0
  37. package/package.json +6 -2
@@ -0,0 +1,204 @@
1
+ import { ConvertBuddy, autoDetectConfig, getFileTypeConfig, convertAny as convertAnyCore, convertAnyToString as convertAnyToStringCore } from "./index.js";
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
+ }
9
+ async function convertFileToString(file, opts = {}) {
10
+ const bytes = await convertFile(file, opts);
11
+ return new TextDecoder().decode(bytes);
12
+ }
13
+ async function convertFile(file, opts = {}) {
14
+ let actualOpts = { ...opts };
15
+ if (opts.inputFormat === "auto") {
16
+ const sampleSize = 256 * 1024;
17
+ const sampleBlob = file.slice(0, sampleSize);
18
+ const sampleBuffer = await sampleBlob.arrayBuffer();
19
+ const sample = new Uint8Array(sampleBuffer);
20
+ const detected = await autoDetectConfig(sample, { debug: opts.debug });
21
+ if (detected.format !== "unknown") {
22
+ actualOpts.inputFormat = detected.format;
23
+ if (detected.csvConfig) {
24
+ actualOpts.csvConfig = opts.csvConfig ? { ...detected.csvConfig, ...opts.csvConfig } : detected.csvConfig;
25
+ }
26
+ if (detected.xmlConfig) {
27
+ actualOpts.xmlConfig = opts.xmlConfig ? { ...detected.xmlConfig, ...opts.xmlConfig } : detected.xmlConfig;
28
+ }
29
+ if (opts.debug) {
30
+ console.log("[convert-buddy] Auto-detected format:", detected.format);
31
+ }
32
+ } else {
33
+ throw new Error("Could not auto-detect input format. Please specify inputFormat explicitly.");
34
+ }
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
+ }
52
+ const buddy = await ConvertBuddy.create(actualOpts);
53
+ const stream = file.stream();
54
+ const reader = stream.getReader();
55
+ const chunks = [];
56
+ try {
57
+ while (true) {
58
+ const { done, value } = await reader.read();
59
+ if (done) break;
60
+ if (buddy.isAborted()) {
61
+ throw new Error("Conversion aborted");
62
+ }
63
+ const output = buddy.push(value);
64
+ if (output.length > 0) {
65
+ chunks.push(output);
66
+ }
67
+ }
68
+ const final = buddy.finish();
69
+ if (final.length > 0) {
70
+ chunks.push(final);
71
+ }
72
+ const totalLength = chunks.reduce((sum, chunk) => sum + chunk.length, 0);
73
+ const result = new Uint8Array(totalLength);
74
+ let offset = 0;
75
+ for (const chunk of chunks) {
76
+ result.set(chunk, offset);
77
+ offset += chunk.length;
78
+ }
79
+ if (opts.profile) {
80
+ console.log("[convert-buddy] Performance Stats:", buddy.stats());
81
+ }
82
+ return result;
83
+ } catch (error) {
84
+ reader.releaseLock();
85
+ throw error;
86
+ }
87
+ }
88
+ async function convertFileToFile(inputFile, outputFilename, opts = {}) {
89
+ const result = await convertFile(inputFile, opts);
90
+ const blob = new Blob([new Uint8Array(result)], { type: "application/octet-stream" });
91
+ const url = URL.createObjectURL(blob);
92
+ const link = document.createElement("a");
93
+ link.href = url;
94
+ link.download = outputFilename;
95
+ link.style.display = "none";
96
+ document.body.appendChild(link);
97
+ link.click();
98
+ setTimeout(() => {
99
+ document.body.removeChild(link);
100
+ URL.revokeObjectURL(url);
101
+ }, 100);
102
+ }
103
+ async function convertFileStream(file, opts = {}) {
104
+ let actualOpts = { ...opts };
105
+ if (opts.inputFormat === "auto") {
106
+ const sampleSize = 256 * 1024;
107
+ const sampleBlob = file.slice(0, sampleSize);
108
+ const sampleBuffer = await sampleBlob.arrayBuffer();
109
+ const sample = new Uint8Array(sampleBuffer);
110
+ const detected = await autoDetectConfig(sample, { debug: opts.debug });
111
+ if (detected.format !== "unknown") {
112
+ actualOpts.inputFormat = detected.format;
113
+ if (detected.csvConfig) {
114
+ actualOpts.csvConfig = opts.csvConfig ? { ...detected.csvConfig, ...opts.csvConfig } : detected.csvConfig;
115
+ }
116
+ if (detected.xmlConfig) {
117
+ actualOpts.xmlConfig = opts.xmlConfig ? { ...detected.xmlConfig, ...opts.xmlConfig } : detected.xmlConfig;
118
+ }
119
+ } else {
120
+ throw new Error("Could not auto-detect input format.");
121
+ }
122
+ }
123
+ const buddy = await ConvertBuddy.create(actualOpts);
124
+ const inputStream = file.stream();
125
+ return new ReadableStream({
126
+ async start(controller) {
127
+ const reader = inputStream.getReader();
128
+ try {
129
+ while (true) {
130
+ const { done, value } = await reader.read();
131
+ if (done) {
132
+ const final = buddy.finish();
133
+ if (final.length > 0) {
134
+ controller.enqueue(final);
135
+ }
136
+ controller.close();
137
+ break;
138
+ }
139
+ if (buddy.isAborted()) {
140
+ controller.error(new Error("Conversion aborted"));
141
+ break;
142
+ }
143
+ const output = buddy.push(value);
144
+ if (output.length > 0) {
145
+ controller.enqueue(output);
146
+ }
147
+ }
148
+ } catch (error) {
149
+ controller.error(error);
150
+ } finally {
151
+ reader.releaseLock();
152
+ }
153
+ }
154
+ });
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
+ }
192
+ export {
193
+ autoConvertStream,
194
+ convert,
195
+ convertAndSave,
196
+ convertFile,
197
+ convertFileStream,
198
+ convertFileToFile,
199
+ convertFileToString,
200
+ convertStreamToWritable,
201
+ convertToString,
202
+ isFileSystemAccessSupported
203
+ };
204
+ //# sourceMappingURL=browser.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/browser.ts"],"sourcesContent":["import { ConvertBuddy, type ConvertBuddyOptions, type ConvertOptions, type Format, autoDetectConfig, detectFormat, getMimeType, getFileTypeConfig, convertAny as convertAnyCore, convertAnyToString as convertAnyToStringCore } from \"./index.js\";\r\n\r\nexport * from \"./index.js\";\r\n\r\nexport type BrowserConvertOptions = ConvertBuddyOptions & {\r\n // Additional browser-specific options can go here\r\n};\r\n\r\n/**\r\n * Ultra-simple convert function for browser.\r\n * Auto-detects input type (File, URL, string, etc.) and format.\r\n * \r\n * @example\r\n * // From File\r\n * const file = fileInput.files[0];\r\n * const result = await convert(file, { outputFormat: \"json\" });\r\n * \r\n * @example\r\n * // From URL\r\n * const result = await convert(\"https://example.com/data.csv\", { outputFormat: \"json\" });\r\n * \r\n * @example\r\n * // From string\r\n * const result = await convert(csvString, { outputFormat: \"json\" });\r\n */\r\nexport async function convert(\r\n input: string | Uint8Array | File | Blob | ReadableStream<Uint8Array>,\r\n opts: ConvertOptions\r\n): Promise<Uint8Array> {\r\n return convertAnyCore(input, opts);\r\n}\r\n\r\n/**\r\n * Ultra-simple convert function that returns a string.\r\n * \r\n * @example\r\n * const json = await convertToString(file, { outputFormat: \"json\" });\r\n * console.log(JSON.parse(json));\r\n */\r\nexport async function convertToString(\r\n input: string | Uint8Array | File | Blob | ReadableStream<Uint8Array>,\r\n opts: ConvertOptions\r\n): Promise<string> {\r\n return convertAnyToStringCore(input, opts);\r\n}\r\n\r\n/**\r\n * Convert a browser File or Blob object to a string.\r\n * Handles streaming internally using FileReader and web streams.\r\n * \r\n * @example\r\n * // From file input\r\n * const fileInput = document.querySelector('input[type=\"file\"]');\r\n * const file = fileInput.files[0];\r\n * const result = await convertFileToString(file, {\r\n * inputFormat: \"csv\",\r\n * outputFormat: \"json\"\r\n * });\r\n * \r\n * @example\r\n * // With auto-detection\r\n * const result = await convertFileToString(file, {\r\n * inputFormat: \"auto\",\r\n * outputFormat: \"ndjson\"\r\n * });\r\n */\r\nexport async function convertFileToString(\r\n file: File | Blob,\r\n opts: BrowserConvertOptions = {}\r\n): Promise<string> {\r\n const bytes = await convertFile(file, opts);\r\n return new TextDecoder().decode(bytes);\r\n}\r\n\r\n/**\r\n * Convert a browser File or Blob object to a Uint8Array.\r\n * Handles streaming internally using FileReader and web streams.\r\n * \r\n * @example\r\n * const file = fileInput.files[0];\r\n * const result = await convertFile(file, {\r\n * inputFormat: \"csv\",\r\n * outputFormat: \"json\",\r\n * onProgress: (stats) => {\r\n * console.log(`Progress: ${stats.bytesIn} bytes processed`);\r\n * }\r\n * });\r\n */\r\nexport async function convertFile(\r\n file: File | Blob,\r\n opts: BrowserConvertOptions = {}\r\n): Promise<Uint8Array> {\r\n // Handle auto-detection\r\n let actualOpts = { ...opts };\r\n \r\n if (opts.inputFormat === \"auto\") {\r\n // Read a sample for auto-detection\r\n const sampleSize = 256 * 1024; // 256KB\r\n const sampleBlob = file.slice(0, sampleSize);\r\n const sampleBuffer = await sampleBlob.arrayBuffer();\r\n const sample = new Uint8Array(sampleBuffer as ArrayBuffer);\r\n \r\n const detected = await autoDetectConfig(sample, { debug: opts.debug });\r\n \r\n if (detected.format !== \"unknown\") {\r\n actualOpts.inputFormat = detected.format as Format;\r\n \r\n if (detected.csvConfig) {\r\n actualOpts.csvConfig = opts.csvConfig ? { ...detected.csvConfig, ...opts.csvConfig } : detected.csvConfig;\r\n }\r\n \r\n if (detected.xmlConfig) {\r\n actualOpts.xmlConfig = opts.xmlConfig ? { ...detected.xmlConfig, ...opts.xmlConfig } : detected.xmlConfig;\r\n }\r\n \r\n if (opts.debug) {\r\n console.log(\"[convert-buddy] Auto-detected format:\", detected.format);\r\n }\r\n } else {\r\n throw new Error(\"Could not auto-detect input format. Please specify inputFormat explicitly.\");\r\n }\r\n }\r\n \r\n // Adaptive chunk sizing based on file size\r\n // Tuned to balance WASM boundary crossing reduction with memory efficiency\r\n if (!actualOpts.chunkTargetBytes) {\r\n const fileSize = file.size;\r\n actualOpts.chunkTargetBytes = Math.max(\r\n 512 * 1024, // minimum: 512KB\r\n Math.min(\r\n 1 * 1024 * 1024, // maximum: 1MB (conservative for memory)\r\n Math.floor(fileSize / 16) // 1/16 of file size\r\n )\r\n );\r\n if (opts.debug) {\r\n console.log(`[convert-buddy] Adaptive chunk sizing: ${fileSize} bytes → ${actualOpts.chunkTargetBytes} byte chunks`);\r\n }\r\n }\r\n \r\n const buddy = await ConvertBuddy.create(actualOpts);\r\n \r\n // Use streams API for efficient processing\r\n const stream = file.stream();\r\n const reader = stream.getReader();\r\n \r\n const chunks: Uint8Array[] = [];\r\n \r\n try {\r\n while (true) {\r\n const { done, value } = await reader.read();\r\n \r\n if (done) break;\r\n \r\n if (buddy.isAborted()) {\r\n throw new Error(\"Conversion aborted\");\r\n }\r\n \r\n const output = buddy.push(value);\r\n if (output.length > 0) {\r\n chunks.push(output);\r\n }\r\n }\r\n \r\n const final = buddy.finish();\r\n if (final.length > 0) {\r\n chunks.push(final);\r\n }\r\n \r\n // Combine all chunks\r\n const totalLength = chunks.reduce((sum, chunk) => sum + chunk.length, 0);\r\n const result = new Uint8Array(totalLength);\r\n let offset = 0;\r\n for (const chunk of chunks) {\r\n result.set(chunk, offset);\r\n offset += chunk.length;\r\n }\r\n \r\n if (opts.profile) {\r\n console.log(\"[convert-buddy] Performance Stats:\", buddy.stats());\r\n }\r\n \r\n return result;\r\n } catch (error) {\r\n reader.releaseLock();\r\n throw error;\r\n }\r\n}\r\n\r\n/**\r\n * Convert a browser File or Blob and download the result as a file.\r\n * \r\n * @example\r\n * const fileInput = document.querySelector('input[type=\"file\"]');\r\n * const file = fileInput.files[0];\r\n * await convertFileToFile(file, \"output.json\", {\r\n * inputFormat: \"csv\",\r\n * outputFormat: \"json\"\r\n * });\r\n */\r\nexport async function convertFileToFile(\r\n inputFile: File | Blob,\r\n outputFilename: string,\r\n opts: BrowserConvertOptions = {}\r\n): Promise<void> {\r\n const result = await convertFile(inputFile, opts);\r\n \r\n // Create a download link\r\n const blob = new Blob([new Uint8Array(result)], { type: \"application/octet-stream\" });\r\n const url = URL.createObjectURL(blob);\r\n \r\n const link = document.createElement(\"a\");\r\n link.href = url;\r\n link.download = outputFilename;\r\n link.style.display = \"none\";\r\n \r\n document.body.appendChild(link);\r\n link.click();\r\n \r\n // Cleanup\r\n setTimeout(() => {\r\n document.body.removeChild(link);\r\n URL.revokeObjectURL(url);\r\n }, 100);\r\n}\r\n\r\n/**\r\n * Create a ReadableStream that converts data on-the-fly from a File or Blob.\r\n * Useful for piping through other stream processors.\r\n * \r\n * @example\r\n * const file = fileInput.files[0];\r\n * const stream = convertFileStream(file, {\r\n * inputFormat: \"csv\",\r\n * outputFormat: \"ndjson\"\r\n * });\r\n * \r\n * const reader = stream.getReader();\r\n * while (true) {\r\n * const { done, value } = await reader.read();\r\n * if (done) break;\r\n * // Process each chunk as it's converted\r\n * console.log(new TextDecoder().decode(value));\r\n * }\r\n */\r\nexport async function convertFileStream(\r\n file: File | Blob,\r\n opts: BrowserConvertOptions = {}\r\n): Promise<ReadableStream<Uint8Array>> {\r\n // Handle auto-detection\r\n let actualOpts = { ...opts };\r\n \r\n if (opts.inputFormat === \"auto\") {\r\n const sampleSize = 256 * 1024;\r\n const sampleBlob = file.slice(0, sampleSize);\r\n const sampleBuffer = await sampleBlob.arrayBuffer();\r\n const sample = new Uint8Array(sampleBuffer);\r\n \r\n const detected = await autoDetectConfig(sample, { debug: opts.debug });\r\n \r\n if (detected.format !== \"unknown\") {\r\n actualOpts.inputFormat = detected.format as Format;\r\n if (detected.csvConfig) {\r\n actualOpts.csvConfig = opts.csvConfig ? { ...detected.csvConfig, ...opts.csvConfig } : detected.csvConfig;\r\n }\r\n if (detected.xmlConfig) {\r\n actualOpts.xmlConfig = opts.xmlConfig ? { ...detected.xmlConfig, ...opts.xmlConfig } : detected.xmlConfig;\r\n }\r\n } else {\r\n throw new Error(\"Could not auto-detect input format.\");\r\n }\r\n }\r\n \r\n const buddy = await ConvertBuddy.create(actualOpts);\r\n const inputStream = file.stream();\r\n \r\n return new ReadableStream<Uint8Array>({\r\n async start(controller) {\r\n const reader = inputStream.getReader();\r\n \r\n try {\r\n while (true) {\r\n const { done, value } = await reader.read();\r\n \r\n if (done) {\r\n const final = buddy.finish();\r\n if (final.length > 0) {\r\n controller.enqueue(final);\r\n }\r\n controller.close();\r\n break;\r\n }\r\n \r\n if (buddy.isAborted()) {\r\n controller.error(new Error(\"Conversion aborted\"));\r\n break;\r\n }\r\n \r\n const output = buddy.push(value);\r\n if (output.length > 0) {\r\n controller.enqueue(output);\r\n }\r\n }\r\n } catch (error) {\r\n controller.error(error);\r\n } finally {\r\n reader.releaseLock();\r\n }\r\n },\r\n });\r\n}\r\n/**\r\n * Auto-detect format and create a ReadableStream for conversion.\r\n * This is a convenience helper that combines format detection with streaming conversion.\r\n * \r\n * @example\r\n * const file = fileInput.files[0];\r\n * const stream = await autoConvertStream(file, { outputFormat: \"json\" });\r\n * \r\n * // Pipe to response\r\n * return new Response(stream);\r\n */\r\nexport async function autoConvertStream(\r\n file: File | Blob,\r\n opts: Omit<BrowserConvertOptions, \"inputFormat\"> & { outputFormat: Format }\r\n): Promise<ReadableStream<Uint8Array>> {\r\n return convertFileStream(file, {\r\n ...opts,\r\n inputFormat: \"auto\",\r\n });\r\n}\r\n\r\n/**\r\n * Stream a file conversion directly to a writable destination.\r\n * This is useful for streaming large conversions to disk using File System Access API\r\n * or to other writable streams.\r\n * \r\n * @example\r\n * // Using File System Access API\r\n * const fileInput = document.querySelector('input[type=\"file\"]');\r\n * const file = fileInput.files[0];\r\n * \r\n * const fileHandle = await window.showSaveFilePicker({\r\n * suggestedName: \"output.json\",\r\n * types: [{ description: \"JSON\", accept: { \"application/json\": [\".json\"] } }]\r\n * });\r\n * const writable = await fileHandle.createWritable();\r\n * \r\n * await convertStreamToWritable(file, writable, {\r\n * inputFormat: \"csv\",\r\n * outputFormat: \"json\",\r\n * onProgress: (stats) => console.log(`${stats.bytesIn} bytes processed`)\r\n * });\r\n * \r\n * @example\r\n * // With auto-detection\r\n * await convertStreamToWritable(file, writable, {\r\n * inputFormat: \"auto\",\r\n * outputFormat: \"ndjson\"\r\n * });\r\n */\r\nexport async function convertStreamToWritable(\r\n file: File | Blob,\r\n writable: WritableStream<Uint8Array> | FileSystemWritableFileStream,\r\n opts: BrowserConvertOptions = {}\r\n): Promise<void> {\r\n const outputStream = await convertFileStream(file, opts);\r\n await outputStream.pipeTo(writable);\r\n}\r\n\r\n/**\r\n * Check if File System Access API is supported in the current browser.\r\n * Use this to determine if you can use File System Access API features.\r\n * \r\n * @example\r\n * if (isFileSystemAccessSupported()) {\r\n * // Use File System Access API\r\n * const handle = await window.showSaveFilePicker();\r\n * } else {\r\n * // Fall back to download link\r\n * await convertFileToFile(file, \"output.json\", opts);\r\n * }\r\n */\r\nexport function isFileSystemAccessSupported(): boolean {\r\n return (\r\n typeof window !== \"undefined\" &&\r\n \"showSaveFilePicker\" in window &&\r\n \"showOpenFilePicker\" in window\r\n );\r\n}\r\n\r\n/**\r\n * Convert a file and save it using File System Access API with a user-selected location.\r\n * This provides a better UX than automatic downloads by letting users choose where to save.\r\n * Falls back to regular download if File System Access API is not supported.\r\n * \r\n * @example\r\n * const file = fileInput.files[0];\r\n * await convertAndSave(file, {\r\n * inputFormat: \"csv\",\r\n * outputFormat: \"json\",\r\n * suggestedName: \"output.json\"\r\n * });\r\n */\r\nexport async function convertAndSave(\r\n file: File | Blob,\r\n opts: BrowserConvertOptions & {\r\n suggestedName?: string;\r\n } = {}\r\n): Promise<void> {\r\n if (!isFileSystemAccessSupported()) {\r\n // Fall back to regular download\r\n const filename = opts.suggestedName || \"converted\";\r\n await convertFileToFile(file, filename, opts);\r\n return;\r\n }\r\n\r\n const outputFormat = opts.outputFormat || \"json\";\r\n const suggestedName = opts.suggestedName || `converted.${outputFormat}`;\r\n const types = getFileTypeConfig(outputFormat);\r\n\r\n try {\r\n const fileHandle = await (window as any).showSaveFilePicker({\r\n suggestedName,\r\n types,\r\n });\r\n\r\n const writable = await fileHandle.createWritable();\r\n await convertStreamToWritable(file, writable, opts);\r\n } catch (error: any) {\r\n // User cancelled or error occurred\r\n if (error?.name === \"AbortError\") {\r\n // User cancelled - this is fine, just return\r\n return;\r\n }\r\n // Re-throw other errors\r\n throw error;\r\n }\r\n}"],"mappings":"AAAA,SAAS,cAA0E,kBAA6C,mBAAmB,cAAc,gBAAgB,sBAAsB,8BAA8B;AAErO,cAAc;AAuBd,eAAsB,QACpB,OACA,MACqB;AACrB,SAAO,eAAe,OAAO,IAAI;AACnC;AASA,eAAsB,gBACpB,OACA,MACiB;AACjB,SAAO,uBAAuB,OAAO,IAAI;AAC3C;AAsBA,eAAsB,oBACpB,MACA,OAA8B,CAAC,GACd;AACjB,QAAM,QAAQ,MAAM,YAAY,MAAM,IAAI;AAC1C,SAAO,IAAI,YAAY,EAAE,OAAO,KAAK;AACvC;AAgBA,eAAsB,YACpB,MACA,OAA8B,CAAC,GACV;AAErB,MAAI,aAAa,EAAE,G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B;AAAA,QACF;AAAA,MACF,SAAS,OAAO;AACd,mBAAW,MAAM,KAAK;AAAA,MACxB,UAAE;AACA,eAAO,YAAY;AAAA,MACrB;AAAA,IACF;AAAA,EACF,CAAC;AACH;AAYA,eAAsB,kBACpB,MACA,MACqC;AACrC,SAAO,kBAAkB,MAAM;AAAA,IAC7B,GAAG;AAAA,IACH,aAAa;AAAA,EACf,CAAC;AACH;AA+BA,eAAsB,wBACpB,MACA,UACA,OAA8B,CAAC,GAChB;AACf,QAAM,eAAe,MAAM,kBAAkB,MAAM,IAAI;AACvD,QAAM,aAAa,OAAO,QAAQ;AACpC;AAeO,SAAS,8BAAuC;AACrD,SACE,OAAO,WAAW,eAClB,wBAAwB,UACxB,wBAAwB;AAE5B;AAeA,eAAsB,eACpB,MACA,OAEI,CAAC,GACU;AACf,MAAI,CAAC,4BAA4B,GAAG;AAElC,UAAM,WAAW,KAAK,iBAAiB;AACvC,UAAM,kBAAkB,MAAM,UAAU,IAAI;AAC5C;AAAA,EACF;AAEA,QAAM,eAAe,KAAK,gBAAgB;AAC1C,QAAM,gBAAgB,KAAK,iBAAiB,aAAa,YAAY;AACrE,QAAM,QAAQ,kBAAkB,YAAY;AAE5C,MAAI;AACF,UAAM,aAAa,MAAO,OAAe,mBAAmB;AAAA,MAC1D;AAAA,MACA;AAAA,IACF,CAAC;AAED,UAAM,WAAW,MAAM,WAAW,eAAe;AACjD,UAAM,wBAAwB,MAAM,UAAU,IAAI;AAAA,EACpD,SAAS,OAAY;AAEnB,QAAI,OAAO,SAAS,cAAc;AAEhC;AAAA,IACF;AAEA,UAAM;AAAA,EACR;AACF;","names":[]}
@@ -12,15 +12,26 @@ type DetectOptions = {
12
12
  maxBytes?: number;
13
13
  debug?: boolean;
14
14
  };
15
+ type ProgressCallback = (stats: Stats) => void;
15
16
  type ConvertBuddyOptions = {
16
17
  debug?: boolean;
17
18
  profile?: boolean;
18
- inputFormat?: Format;
19
+ inputFormat?: Format | "auto";
19
20
  outputFormat?: Format;
20
21
  chunkTargetBytes?: number;
21
22
  parallelism?: number;
23
+ maxMemoryMB?: number;
22
24
  csvConfig?: CsvConfig;
23
25
  xmlConfig?: XmlConfig;
26
+ onProgress?: ProgressCallback;
27
+ progressIntervalBytes?: number;
28
+ };
29
+ type ConvertOptions = {
30
+ inputFormat?: Format | "auto";
31
+ outputFormat: Format;
32
+ csvConfig?: CsvConfig;
33
+ xmlConfig?: XmlConfig;
34
+ onProgress?: ProgressCallback;
24
35
  };
25
36
  type CsvConfig = {
26
37
  delimiter?: string;
@@ -50,19 +61,138 @@ declare class ConvertBuddy {
50
61
  private converter;
51
62
  private debug;
52
63
  private profile;
53
- private constructor();
64
+ private aborted;
65
+ private paused;
66
+ private onProgress?;
67
+ private progressIntervalBytes;
68
+ private lastProgressBytes;
69
+ private globalConfig;
70
+ private initialized;
71
+ simd: boolean;
72
+ /**
73
+ * Create a new ConvertBuddy instance with global configuration.
74
+ * This is useful when you want to set memory limits, debug mode, or other global settings.
75
+ *
76
+ * @example
77
+ * const buddy = new ConvertBuddy({ maxMemoryMB: 512, debug: true });
78
+ * const result = await buddy.convert(input, { outputFormat: "json" });
79
+ */
80
+ constructor(opts?: ConvertBuddyOptions);
81
+ /**
82
+ * Convert input (string, Buffer, File, URL, etc.) to the desired output format.
83
+ * This is the main method for the new simplified API.
84
+ *
85
+ * @example
86
+ * // Auto-detect everything
87
+ * const buddy = new ConvertBuddy();
88
+ * const result = await buddy.convert("https://example.com/data.csv", { outputFormat: "json" });
89
+ *
90
+ * @example
91
+ * // With configuration
92
+ * const buddy = new ConvertBuddy({ maxMemoryMB: 512 });
93
+ * const result = await buddy.convert(file, { inputFormat: "csv", outputFormat: "json" });
94
+ */
95
+ convert(input: string | Uint8Array | File | Blob | ReadableStream<Uint8Array>, opts: ConvertOptions): Promise<Uint8Array>;
96
+ private convertFromUrl;
97
+ private convertFromString;
98
+ private convertFromBuffer;
99
+ private convertFromFile;
100
+ private convertFromBlob;
101
+ private convertFromStream;
102
+ /**
103
+ * Legacy create method for backward compatibility.
104
+ * Prefer using the constructor: new ConvertBuddy(opts)
105
+ */
54
106
  static create(opts?: ConvertBuddyOptions): Promise<ConvertBuddy>;
55
107
  push(chunk: Uint8Array): Uint8Array;
56
108
  finish(): Uint8Array;
57
109
  stats(): Stats;
110
+ abort(): void;
111
+ pause(): void;
112
+ resume(): void;
113
+ isAborted(): boolean;
114
+ isPaused(): boolean;
58
115
  }
59
116
  declare function detectFormat(input: DetectInput, opts?: DetectOptions): Promise<Format | "unknown">;
60
117
  declare function detectCsvFieldsAndDelimiter(input: DetectInput, opts?: DetectOptions): Promise<CsvDetection | null>;
61
118
  declare function detectXmlElements(input: DetectInput, opts?: DetectOptions): Promise<XmlDetection | null>;
119
+ declare function autoDetectConfig(sample: Uint8Array, opts?: {
120
+ debug?: boolean;
121
+ }): Promise<{
122
+ format: Format | "unknown";
123
+ csvConfig?: CsvConfig;
124
+ xmlConfig?: XmlConfig;
125
+ }>;
62
126
  declare class ConvertBuddyTransformStream extends TransformStream<Uint8Array, Uint8Array> {
63
127
  constructor(opts?: ConvertBuddyOptions);
64
128
  }
65
129
  declare function convert(input: Uint8Array | string, opts?: ConvertBuddyOptions): Promise<Uint8Array>;
66
130
  declare function convertToString(input: Uint8Array | string, opts?: ConvertBuddyOptions): Promise<string>;
131
+ /**
132
+ * Ultra-simple standalone convert function with auto-detection.
133
+ * Accepts any input type (URL, File, Buffer, string, stream) and automatically detects format.
134
+ *
135
+ * @example
136
+ * // From URL
137
+ * import { convertAny } from "convert-buddy-js";
138
+ * const result = await convertAny("https://example.com/data.csv", { outputFormat: "json" });
139
+ *
140
+ * @example
141
+ * // From File (browser)
142
+ * const file = fileInput.files[0];
143
+ * const result = await convertAny(file, { outputFormat: "ndjson" });
144
+ *
145
+ * @example
146
+ * // From string data
147
+ * const result = await convertAny('{"name":"Ada"}', { outputFormat: "csv" });
148
+ */
149
+ declare function convertAny(input: string | Uint8Array | File | Blob | ReadableStream<Uint8Array>, opts: ConvertOptions): Promise<Uint8Array>;
150
+ /**
151
+ * Ultra-simple standalone convert function that returns a string.
152
+ * Same as convertAny but decodes the output to a string.
153
+ *
154
+ * @example
155
+ * import { convertAnyToString } from "convert-buddy-js";
156
+ * const json = await convertAnyToString("https://example.com/data.csv", { outputFormat: "json" });
157
+ * console.log(JSON.parse(json));
158
+ */
159
+ declare function convertAnyToString(input: string | Uint8Array | File | Blob | ReadableStream<Uint8Array>, opts: ConvertOptions): Promise<string>;
160
+ /**
161
+ * Get MIME type for a given format
162
+ *
163
+ * @example
164
+ * const mimeType = getMimeType("json"); // "application/json"
165
+ */
166
+ declare function getMimeType(format: Format): string;
167
+ /**
168
+ * Get file extension for a given format (without the dot)
169
+ *
170
+ * @example
171
+ * const ext = getExtension("json"); // "json"
172
+ */
173
+ declare function getExtension(format: Format): string;
174
+ /**
175
+ * Get suggested filename for a converted file
176
+ *
177
+ * @param originalName - Original filename
178
+ * @param outputFormat - Target format
179
+ * @param includeTimestamp - Whether to include a timestamp (default: false)
180
+ *
181
+ * @example
182
+ * const name = getSuggestedFilename("data.csv", "json"); // "data.json"
183
+ * const name = getSuggestedFilename("data.csv", "json", true); // "data_converted_1234567890.json"
184
+ */
185
+ declare function getSuggestedFilename(originalName: string, outputFormat: Format, includeTimestamp?: boolean): string;
186
+ /**
187
+ * Get File System Access API file type configuration for showSaveFilePicker
188
+ *
189
+ * @example
190
+ * const types = getFileTypeConfig("json");
191
+ * const handle = await showSaveFilePicker({ types });
192
+ */
193
+ declare function getFileTypeConfig(format: Format): Array<{
194
+ description: string;
195
+ accept: Record<string, string[]>;
196
+ }>;
67
197
 
68
- export { ConvertBuddy, type ConvertBuddyOptions, ConvertBuddyTransformStream, type CsvConfig, type CsvDetection, type DetectInput, type DetectOptions, type Format, type Stats, type XmlConfig, type XmlDetection, convert, convertToString, detectCsvFieldsAndDelimiter, detectFormat, detectXmlElements };
198
+ export { ConvertBuddy, type ConvertBuddyOptions, ConvertBuddyTransformStream, type ConvertOptions, type CsvConfig, type CsvDetection, type DetectInput, type DetectOptions, type Format, type ProgressCallback, type Stats, type XmlConfig, type XmlDetection, autoDetectConfig, convert, convertAny, convertAnyToString, convertToString, detectCsvFieldsAndDelimiter, detectFormat, detectXmlElements, getExtension, getFileTypeConfig, getMimeType, getSuggestedFilename };