javascript-excel 2.0.0-alpha.1 → 2.0.0-alpha.2
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 +20 -1
- package/dist/csv-DyVH9y7S.cjs +647 -0
- package/dist/csv-pXfaputs.js +618 -0
- package/dist/csv.cjs +6 -622
- package/dist/csv.js +1 -618
- package/dist/dom.cjs +3 -27
- package/dist/dom.js +3 -27
- package/dist/index.cjs +561 -1358
- package/dist/index.d.cts +119 -4
- package/dist/index.d.ts +119 -4
- package/dist/index.js +556 -1356
- package/dist/iso-date-3TY1OlGd.cjs +57 -0
- package/dist/iso-date-CXnn-9mT.js +46 -0
- package/dist/{numfmt-DEm0sYBJ.js → numfmt-BBgB7iQA.js} +39 -9
- package/dist/{numfmt-B-TlCgr7.cjs → numfmt-Dv6AjopS.cjs} +44 -8
- package/dist/read-C6L7Nyy3.d.cts +78 -0
- package/dist/read-DDN5ZsPe.js +2909 -0
- package/dist/read-Ddkks7qC.d.ts +78 -0
- package/dist/read-Ym1voUct.cjs +2932 -0
- package/dist/read.cjs +5 -2898
- package/dist/read.d.cts +2 -75
- package/dist/read.d.ts +2 -75
- package/dist/read.js +1 -2895
- package/dist/{sheet-names-Cm-IhbBk.cjs → sheet-names-qpXsvMpM.cjs} +3 -2
- package/dist/{sheet-names-CDsVK15c.js → sheet-names-uT2EH5Ui.js} +3 -2
- package/dist/worker.cjs +236 -7
- package/dist/worker.d.cts +62 -5
- package/dist/worker.d.ts +62 -5
- package/dist/worker.js +235 -7
- package/dist/write-BOh9jfu1.cjs +1487 -0
- package/dist/write-DubvcG-x.js +1470 -0
- package/package.json +46 -12
- package/dist/iso-date-B22IvwL1.js +0 -21
- package/dist/iso-date-BSUsPSWO.cjs +0 -26
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
const require_errors = require("./errors-DOPrrGS4.cjs");
|
|
2
|
-
const require_iso_date = require("./iso-date-
|
|
2
|
+
const require_iso_date = require("./iso-date-3TY1OlGd.cjs");
|
|
3
3
|
// src/internal/xml.ts
|
|
4
4
|
/**
|
|
5
5
|
* XML text helpers for the writer.
|
|
@@ -50,8 +50,9 @@ function xmlAttr(s) {
|
|
|
50
50
|
return s.replace(INVALID_XML_CHAR, "").replace(MARKUP_ATTR, entity);
|
|
51
51
|
}
|
|
52
52
|
/** True when text needs `xml:space="preserve"` to keep its whitespace. */
|
|
53
|
+
const WS = /\s/;
|
|
53
54
|
function needsPreserve(s) {
|
|
54
|
-
return s.length > 0 && (
|
|
55
|
+
return s.length > 0 && (WS.test(s[0]) || WS.test(s[s.length - 1]) || s.includes("\n") || s.includes(" "));
|
|
55
56
|
}
|
|
56
57
|
const XML_DECL = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n";
|
|
57
58
|
/**
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { o as ValidationError } from "./errors-CvGlfw0E.js";
|
|
2
|
-
import { t as isoDate } from "./iso-date-
|
|
2
|
+
import { t as isoDate } from "./iso-date-CXnn-9mT.js";
|
|
3
3
|
// src/internal/xml.ts
|
|
4
4
|
/**
|
|
5
5
|
* XML text helpers for the writer.
|
|
@@ -50,8 +50,9 @@ function xmlAttr(s) {
|
|
|
50
50
|
return s.replace(INVALID_XML_CHAR, "").replace(MARKUP_ATTR, entity);
|
|
51
51
|
}
|
|
52
52
|
/** True when text needs `xml:space="preserve"` to keep its whitespace. */
|
|
53
|
+
const WS = /\s/;
|
|
53
54
|
function needsPreserve(s) {
|
|
54
|
-
return s.length > 0 && (
|
|
55
|
+
return s.length > 0 && (WS.test(s[0]) || WS.test(s[s.length - 1]) || s.includes("\n") || s.includes(" "));
|
|
55
56
|
}
|
|
56
57
|
const XML_DECL = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n";
|
|
57
58
|
/**
|
package/dist/worker.cjs
CHANGED
|
@@ -1,11 +1,240 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_csv = require("./csv-DyVH9y7S.cjs");
|
|
3
|
+
const require_errors = require("./errors-DOPrrGS4.cjs");
|
|
4
|
+
const require_read = require("./read-Ym1voUct.cjs");
|
|
5
|
+
const require_write = require("./write-BOh9jfu1.cjs");
|
|
2
6
|
// src/worker.ts
|
|
7
|
+
/** Marks the library's messages, so an app can use the same worker for its own messages too. */
|
|
8
|
+
const TAG = "javascript-excel/worker@1";
|
|
9
|
+
function isTagged(v) {
|
|
10
|
+
return typeof v === "object" && v !== null && v.tag === TAG && typeof v.id === "number";
|
|
11
|
+
}
|
|
12
|
+
function abortError(message) {
|
|
13
|
+
const err = new Error(message);
|
|
14
|
+
err.name = "AbortError";
|
|
15
|
+
return err;
|
|
16
|
+
}
|
|
3
17
|
/**
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
* Placeholder module so the exports map, type resolution and tree-shaking are
|
|
7
|
-
* tested from the start. The real implementation lands in a later pull request.
|
|
18
|
+
* Serves requests from `createWorkerClient` on `scope` (default: the worker's
|
|
19
|
+
* global scope, `self`). Call it once at the top of the worker file.
|
|
8
20
|
*/
|
|
9
|
-
|
|
10
|
-
const
|
|
11
|
-
|
|
21
|
+
function exposeWorker(scope) {
|
|
22
|
+
const ep = scope ?? globalThis;
|
|
23
|
+
if (typeof ep.postMessage !== "function" || typeof ep.addEventListener !== "function") throw new require_errors.EnvironmentError("ENVIRONMENT_UNSUPPORTED", "exposeWorker() must run inside a worker (no postMessage/addEventListener here).", { details: { api: "postMessage" } });
|
|
24
|
+
const endpoint = ep;
|
|
25
|
+
const controllers = /* @__PURE__ */ new Map();
|
|
26
|
+
let queue = Promise.resolve();
|
|
27
|
+
endpoint.addEventListener("message", (event) => {
|
|
28
|
+
const msg = event.data;
|
|
29
|
+
if (!isTagged(msg)) return;
|
|
30
|
+
const req = msg;
|
|
31
|
+
if (req.cancel) {
|
|
32
|
+
const c = controllers.get(req.id);
|
|
33
|
+
if (c) {
|
|
34
|
+
c.aborted = true;
|
|
35
|
+
c.reason = abortError("The operation was aborted.");
|
|
36
|
+
}
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
const state = {
|
|
40
|
+
aborted: false,
|
|
41
|
+
reason: void 0
|
|
42
|
+
};
|
|
43
|
+
controllers.set(req.id, state);
|
|
44
|
+
queue = queue.then(() => run(endpoint, req, state)).finally(() => controllers.delete(req.id));
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
async function run(ep, req, signal) {
|
|
48
|
+
const post = (r, transfer) => ep.postMessage({
|
|
49
|
+
tag: TAG,
|
|
50
|
+
id: req.id,
|
|
51
|
+
...r
|
|
52
|
+
}, transfer);
|
|
53
|
+
try {
|
|
54
|
+
if (signal.aborted) throw signal.reason;
|
|
55
|
+
const options = {
|
|
56
|
+
...req.options ?? {},
|
|
57
|
+
signal
|
|
58
|
+
};
|
|
59
|
+
if (req.progress) options["onProgress"] = (p) => post({ progress: p });
|
|
60
|
+
if (req.warnings) options["onWarning"] = (w) => post({ warning: w });
|
|
61
|
+
switch (req.op) {
|
|
62
|
+
case "write": {
|
|
63
|
+
const bytes = await require_write.toUint8Array(req.input, options);
|
|
64
|
+
post({
|
|
65
|
+
done: true,
|
|
66
|
+
result: bytes
|
|
67
|
+
}, [bytes.buffer]);
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
case "read":
|
|
71
|
+
post({
|
|
72
|
+
done: true,
|
|
73
|
+
result: await require_read.read(req.input, options)
|
|
74
|
+
});
|
|
75
|
+
return;
|
|
76
|
+
case "toCsv":
|
|
77
|
+
delete options["signal"];
|
|
78
|
+
post({
|
|
79
|
+
done: true,
|
|
80
|
+
result: require_csv.toCsv(req.input, options)
|
|
81
|
+
});
|
|
82
|
+
return;
|
|
83
|
+
case "parseCsv":
|
|
84
|
+
post({
|
|
85
|
+
done: true,
|
|
86
|
+
result: await require_csv.readCsv(req.input, options)
|
|
87
|
+
});
|
|
88
|
+
return;
|
|
89
|
+
default: throw new TypeError(`Unknown worker operation ${JSON.stringify(req.op)}`);
|
|
90
|
+
}
|
|
91
|
+
} catch (err) {
|
|
92
|
+
post({
|
|
93
|
+
done: true,
|
|
94
|
+
error: serializeError(err)
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
function serializeError(err) {
|
|
99
|
+
if (require_errors.isExcelError(err)) {
|
|
100
|
+
const out = {
|
|
101
|
+
name: err.name,
|
|
102
|
+
message: err.message,
|
|
103
|
+
code: err.code,
|
|
104
|
+
excel: true
|
|
105
|
+
};
|
|
106
|
+
if (err.details !== void 0) out.details = err.details;
|
|
107
|
+
return out;
|
|
108
|
+
}
|
|
109
|
+
if (err instanceof Error) return {
|
|
110
|
+
name: err.name,
|
|
111
|
+
message: err.message,
|
|
112
|
+
excel: false
|
|
113
|
+
};
|
|
114
|
+
return {
|
|
115
|
+
name: "Error",
|
|
116
|
+
message: String(err),
|
|
117
|
+
excel: false
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
const EXCEL_CLASSES = {
|
|
121
|
+
ValidationError: require_errors.ValidationError,
|
|
122
|
+
FormatError: require_errors.FormatError,
|
|
123
|
+
LimitError: require_errors.LimitError,
|
|
124
|
+
CsvError: require_errors.CsvError,
|
|
125
|
+
EnvironmentError: require_errors.EnvironmentError,
|
|
126
|
+
ExcelError: require_errors.ExcelError
|
|
127
|
+
};
|
|
128
|
+
function deserializeError(e) {
|
|
129
|
+
if (e.excel) return new (Object.prototype.hasOwnProperty.call(EXCEL_CLASSES, e.name) ? EXCEL_CLASSES[e.name] : require_errors.ExcelError)(e.code, e.message, e.details === void 0 ? void 0 : { details: e.details });
|
|
130
|
+
const Std = {
|
|
131
|
+
TypeError,
|
|
132
|
+
RangeError,
|
|
133
|
+
SyntaxError,
|
|
134
|
+
ReferenceError
|
|
135
|
+
};
|
|
136
|
+
const err = new (Object.prototype.hasOwnProperty.call(Std, e.name) ? Std[e.name] : Error)(e.message);
|
|
137
|
+
if (err.name !== e.name) err.name = e.name;
|
|
138
|
+
return err;
|
|
139
|
+
}
|
|
140
|
+
/** Wraps a worker that runs `exposeWorker()`. */
|
|
141
|
+
function createWorkerClient(worker) {
|
|
142
|
+
let nextId = 1;
|
|
143
|
+
const pending = /* @__PURE__ */ new Map();
|
|
144
|
+
worker.addEventListener("message", (event) => {
|
|
145
|
+
const msg = event.data;
|
|
146
|
+
if (!isTagged(msg)) return;
|
|
147
|
+
const res = msg;
|
|
148
|
+
const p = pending.get(res.id);
|
|
149
|
+
if (!p) return;
|
|
150
|
+
if (res.progress) p.onProgress?.(res.progress);
|
|
151
|
+
if (res.warning) p.onWarning?.(res.warning);
|
|
152
|
+
if (!res.done) return;
|
|
153
|
+
pending.delete(res.id);
|
|
154
|
+
p.cleanup();
|
|
155
|
+
if (res.error) p.reject(deserializeError(res.error));
|
|
156
|
+
else p.resolve(res.result);
|
|
157
|
+
});
|
|
158
|
+
worker.addEventListener("error", (event) => {
|
|
159
|
+
const reason = new require_errors.EnvironmentError("ENVIRONMENT_UNSUPPORTED", "The worker failed (see the worker's console for details).", { details: {
|
|
160
|
+
api: "Worker",
|
|
161
|
+
event: String(event.message ?? "")
|
|
162
|
+
} });
|
|
163
|
+
for (const [id, p] of pending) {
|
|
164
|
+
pending.delete(id);
|
|
165
|
+
p.cleanup();
|
|
166
|
+
p.reject(reason);
|
|
167
|
+
}
|
|
168
|
+
});
|
|
169
|
+
function call(op, input, options) {
|
|
170
|
+
const o = { ...options ?? {} };
|
|
171
|
+
const signal = o["signal"];
|
|
172
|
+
const onProgress = o["onProgress"];
|
|
173
|
+
const onWarning = o["onWarning"];
|
|
174
|
+
const transfer = o["transfer"] === true;
|
|
175
|
+
delete o["signal"];
|
|
176
|
+
delete o["onProgress"];
|
|
177
|
+
delete o["onWarning"];
|
|
178
|
+
delete o["transfer"];
|
|
179
|
+
for (const [k, v] of Object.entries(o)) if (typeof v === "function") return Promise.reject(/* @__PURE__ */ new TypeError(`options.${k} is a function, which cannot be sent to a worker`));
|
|
180
|
+
if (signal?.aborted) return Promise.reject(signal.reason ?? abortError("The operation was aborted."));
|
|
181
|
+
const id = nextId++;
|
|
182
|
+
return new Promise((resolve, reject) => {
|
|
183
|
+
const onAbort = () => {
|
|
184
|
+
if (!pending.has(id)) return;
|
|
185
|
+
pending.delete(id);
|
|
186
|
+
worker.postMessage({
|
|
187
|
+
tag: TAG,
|
|
188
|
+
id,
|
|
189
|
+
cancel: true
|
|
190
|
+
});
|
|
191
|
+
reject(signal?.reason ?? abortError("The operation was aborted."));
|
|
192
|
+
};
|
|
193
|
+
signal?.addEventListener?.("abort", onAbort, { once: true });
|
|
194
|
+
pending.set(id, {
|
|
195
|
+
resolve,
|
|
196
|
+
reject,
|
|
197
|
+
onProgress,
|
|
198
|
+
onWarning,
|
|
199
|
+
cleanup: () => signal?.removeEventListener?.("abort", onAbort)
|
|
200
|
+
});
|
|
201
|
+
const req = {
|
|
202
|
+
tag: TAG,
|
|
203
|
+
id,
|
|
204
|
+
op,
|
|
205
|
+
input,
|
|
206
|
+
options: o,
|
|
207
|
+
progress: onProgress !== void 0,
|
|
208
|
+
warnings: onWarning !== void 0
|
|
209
|
+
};
|
|
210
|
+
const list = [];
|
|
211
|
+
if (transfer) {
|
|
212
|
+
if (input instanceof ArrayBuffer) list.push(input);
|
|
213
|
+
else if (input instanceof Uint8Array) list.push(input.buffer);
|
|
214
|
+
}
|
|
215
|
+
try {
|
|
216
|
+
worker.postMessage(req, list);
|
|
217
|
+
} catch (err) {
|
|
218
|
+
pending.delete(id);
|
|
219
|
+
signal?.removeEventListener?.("abort", onAbort);
|
|
220
|
+
reject(err);
|
|
221
|
+
}
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
return {
|
|
225
|
+
write: (wb, options) => call("write", wb, options),
|
|
226
|
+
read: (input, options) => call("read", input, options),
|
|
227
|
+
toCsv: (source, options) => call("toCsv", source, options),
|
|
228
|
+
parseCsv: (input, options) => call("parseCsv", input, options),
|
|
229
|
+
terminate: () => {
|
|
230
|
+
worker.terminate();
|
|
231
|
+
for (const [id, p] of pending) {
|
|
232
|
+
pending.delete(id);
|
|
233
|
+
p.cleanup();
|
|
234
|
+
p.reject(abortError("The worker was terminated."));
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
exports.createWorkerClient = createWorkerClient;
|
|
240
|
+
exports.exposeWorker = exposeWorker;
|
package/dist/worker.d.cts
CHANGED
|
@@ -1,8 +1,65 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* javascript-excel/worker: Web Worker
|
|
2
|
+
* javascript-excel/worker: run big jobs in a Web Worker (docs/api.md §5.5).
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
4
|
+
* The app writes the worker file itself (two lines):
|
|
5
|
+
*
|
|
6
|
+
* // excel.worker.ts
|
|
7
|
+
* import { exposeWorker } from 'javascript-excel/worker';
|
|
8
|
+
* exposeWorker();
|
|
9
|
+
*
|
|
10
|
+
* and talks to it from the main thread:
|
|
11
|
+
*
|
|
12
|
+
* const worker = new Worker(new URL('./excel.worker.ts', import.meta.url), { type: 'module' });
|
|
13
|
+
* const excel = createWorkerClient(worker);
|
|
14
|
+
* const bytes = await excel.write(workbook, { onProgress, signal });
|
|
15
|
+
*
|
|
16
|
+
* `onProgress`, `onWarning` and `signal` work across the boundary. Results
|
|
17
|
+
* (`Uint8Array`) are transferred, not copied; inputs are copied unless
|
|
18
|
+
* `transfer: true`. Calls on one client run one after another. Code that
|
|
19
|
+
* imports only `createWorkerClient` does not ship the engine: the writer,
|
|
20
|
+
* reader and CSV code are referenced from `exposeWorker` alone.
|
|
21
|
+
*/
|
|
22
|
+
import { I as Workbook, M as Sheet, f as CellInput, r as BinaryInput } from "./warnings-C_12PT4t.cjs";
|
|
23
|
+
import { CsvParseOptions, CsvParseResult, CsvReadOptions, CsvWriteOptions } from "./csv.cjs";
|
|
24
|
+
import { a as ReadResult, i as ReadOptions } from "./read-C6L7Nyy3.cjs";
|
|
25
|
+
import { n as WriteOptions, t as Progress } from "./write-C5JgeIar.cjs";
|
|
26
|
+
import { t as AbortSignalLike } from "./signal-Dz0ZYe6d.cjs";
|
|
27
|
+
/** The side of a message channel the library uses: a `Worker`, the worker's `self`, or a `MessagePort`. */
|
|
28
|
+
export interface MessageEndpoint {
|
|
29
|
+
postMessage(message: unknown, transfer?: unknown[]): void;
|
|
30
|
+
addEventListener(type: "message", listener: (event: {
|
|
31
|
+
data: unknown;
|
|
32
|
+
}) => void): void;
|
|
33
|
+
removeEventListener?(type: "message", listener: (event: {
|
|
34
|
+
data: unknown;
|
|
35
|
+
}) => void): void;
|
|
36
|
+
}
|
|
37
|
+
/** A `Worker` (or anything with the same members). */
|
|
38
|
+
export interface WorkerLike extends MessageEndpoint {
|
|
39
|
+
terminate(): void;
|
|
40
|
+
addEventListener(type: "message" | "error", listener: (event: {
|
|
41
|
+
data: unknown;
|
|
42
|
+
}) => void): void;
|
|
43
|
+
}
|
|
44
|
+
/** Options every client call accepts, on top of the call's own options. */
|
|
45
|
+
export interface TransferOption {
|
|
46
|
+
/** Transfer an input `ArrayBuffer` or `Uint8Array` to the worker instead of copying it; it is detached in the caller. Default false. */
|
|
47
|
+
transfer?: boolean | undefined;
|
|
48
|
+
}
|
|
49
|
+
/** The main-thread side of a worker running `exposeWorker()`. */
|
|
50
|
+
export interface WorkerClient {
|
|
51
|
+
write(wb: Workbook, options?: WriteOptions & TransferOption): Promise<Uint8Array>;
|
|
52
|
+
read(input: BinaryInput, options?: ReadOptions & TransferOption): Promise<ReadResult>;
|
|
53
|
+
toCsv(source: Sheet | CellInput[][], options?: CsvWriteOptions): Promise<string>;
|
|
54
|
+
parseCsv(input: string | BinaryInput, options?: (CsvParseOptions & Pick<CsvReadOptions, "encoding" | "signal">) & TransferOption): Promise<CsvParseResult>;
|
|
55
|
+
/** Terminates the worker. Calls still in flight reject with an `AbortError`. */
|
|
56
|
+
terminate(): void;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Serves requests from `createWorkerClient` on `scope` (default: the worker's
|
|
60
|
+
* global scope, `self`). Call it once at the top of the worker file.
|
|
6
61
|
*/
|
|
7
|
-
|
|
8
|
-
|
|
62
|
+
export declare function exposeWorker(scope?: MessageEndpoint): void;
|
|
63
|
+
/** Wraps a worker that runs `exposeWorker()`. */
|
|
64
|
+
export declare function createWorkerClient(worker: WorkerLike): WorkerClient;
|
|
65
|
+
export type { AbortSignalLike, Progress };
|
package/dist/worker.d.ts
CHANGED
|
@@ -1,8 +1,65 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* javascript-excel/worker: Web Worker
|
|
2
|
+
* javascript-excel/worker: run big jobs in a Web Worker (docs/api.md §5.5).
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
4
|
+
* The app writes the worker file itself (two lines):
|
|
5
|
+
*
|
|
6
|
+
* // excel.worker.ts
|
|
7
|
+
* import { exposeWorker } from 'javascript-excel/worker';
|
|
8
|
+
* exposeWorker();
|
|
9
|
+
*
|
|
10
|
+
* and talks to it from the main thread:
|
|
11
|
+
*
|
|
12
|
+
* const worker = new Worker(new URL('./excel.worker.ts', import.meta.url), { type: 'module' });
|
|
13
|
+
* const excel = createWorkerClient(worker);
|
|
14
|
+
* const bytes = await excel.write(workbook, { onProgress, signal });
|
|
15
|
+
*
|
|
16
|
+
* `onProgress`, `onWarning` and `signal` work across the boundary. Results
|
|
17
|
+
* (`Uint8Array`) are transferred, not copied; inputs are copied unless
|
|
18
|
+
* `transfer: true`. Calls on one client run one after another. Code that
|
|
19
|
+
* imports only `createWorkerClient` does not ship the engine: the writer,
|
|
20
|
+
* reader and CSV code are referenced from `exposeWorker` alone.
|
|
21
|
+
*/
|
|
22
|
+
import { N as Workbook, P as BinaryInput, k as Sheet, l as CellInput } from "./warnings-DXJKe1lx.js";
|
|
23
|
+
import { CsvParseOptions, CsvParseResult, CsvReadOptions, CsvWriteOptions } from "./csv.js";
|
|
24
|
+
import { a as ReadResult, i as ReadOptions } from "./read-Ddkks7qC.js";
|
|
25
|
+
import { n as WriteOptions, t as Progress } from "./write-CYpLGuzl.js";
|
|
26
|
+
import { t as AbortSignalLike } from "./signal-Dz0ZYe6d.js";
|
|
27
|
+
/** The side of a message channel the library uses: a `Worker`, the worker's `self`, or a `MessagePort`. */
|
|
28
|
+
export interface MessageEndpoint {
|
|
29
|
+
postMessage(message: unknown, transfer?: unknown[]): void;
|
|
30
|
+
addEventListener(type: "message", listener: (event: {
|
|
31
|
+
data: unknown;
|
|
32
|
+
}) => void): void;
|
|
33
|
+
removeEventListener?(type: "message", listener: (event: {
|
|
34
|
+
data: unknown;
|
|
35
|
+
}) => void): void;
|
|
36
|
+
}
|
|
37
|
+
/** A `Worker` (or anything with the same members). */
|
|
38
|
+
export interface WorkerLike extends MessageEndpoint {
|
|
39
|
+
terminate(): void;
|
|
40
|
+
addEventListener(type: "message" | "error", listener: (event: {
|
|
41
|
+
data: unknown;
|
|
42
|
+
}) => void): void;
|
|
43
|
+
}
|
|
44
|
+
/** Options every client call accepts, on top of the call's own options. */
|
|
45
|
+
export interface TransferOption {
|
|
46
|
+
/** Transfer an input `ArrayBuffer` or `Uint8Array` to the worker instead of copying it; it is detached in the caller. Default false. */
|
|
47
|
+
transfer?: boolean | undefined;
|
|
48
|
+
}
|
|
49
|
+
/** The main-thread side of a worker running `exposeWorker()`. */
|
|
50
|
+
export interface WorkerClient {
|
|
51
|
+
write(wb: Workbook, options?: WriteOptions & TransferOption): Promise<Uint8Array>;
|
|
52
|
+
read(input: BinaryInput, options?: ReadOptions & TransferOption): Promise<ReadResult>;
|
|
53
|
+
toCsv(source: Sheet | CellInput[][], options?: CsvWriteOptions): Promise<string>;
|
|
54
|
+
parseCsv(input: string | BinaryInput, options?: (CsvParseOptions & Pick<CsvReadOptions, "encoding" | "signal">) & TransferOption): Promise<CsvParseResult>;
|
|
55
|
+
/** Terminates the worker. Calls still in flight reject with an `AbortError`. */
|
|
56
|
+
terminate(): void;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Serves requests from `createWorkerClient` on `scope` (default: the worker's
|
|
60
|
+
* global scope, `self`). Call it once at the top of the worker file.
|
|
6
61
|
*/
|
|
7
|
-
|
|
8
|
-
|
|
62
|
+
export declare function exposeWorker(scope?: MessageEndpoint): void;
|
|
63
|
+
/** Wraps a worker that runs `exposeWorker()`. */
|
|
64
|
+
export declare function createWorkerClient(worker: WorkerLike): WorkerClient;
|
|
65
|
+
export type { AbortSignalLike, Progress };
|
package/dist/worker.js
CHANGED
|
@@ -1,10 +1,238 @@
|
|
|
1
|
+
import { i as toCsv, r as readCsv } from "./csv-pXfaputs.js";
|
|
2
|
+
import { a as LimitError, i as FormatError, n as EnvironmentError, o as ValidationError, r as ExcelError, s as isExcelError, t as CsvError } from "./errors-CvGlfw0E.js";
|
|
3
|
+
import { r as read } from "./read-DDN5ZsPe.js";
|
|
4
|
+
import { r as toUint8Array } from "./write-DubvcG-x.js";
|
|
1
5
|
// src/worker.ts
|
|
6
|
+
/** Marks the library's messages, so an app can use the same worker for its own messages too. */
|
|
7
|
+
const TAG = "javascript-excel/worker@1";
|
|
8
|
+
function isTagged(v) {
|
|
9
|
+
return typeof v === "object" && v !== null && v.tag === TAG && typeof v.id === "number";
|
|
10
|
+
}
|
|
11
|
+
function abortError(message) {
|
|
12
|
+
const err = new Error(message);
|
|
13
|
+
err.name = "AbortError";
|
|
14
|
+
return err;
|
|
15
|
+
}
|
|
2
16
|
/**
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
* Placeholder module so the exports map, type resolution and tree-shaking are
|
|
6
|
-
* tested from the start. The real implementation lands in a later pull request.
|
|
17
|
+
* Serves requests from `createWorkerClient` on `scope` (default: the worker's
|
|
18
|
+
* global scope, `self`). Call it once at the top of the worker file.
|
|
7
19
|
*/
|
|
8
|
-
|
|
9
|
-
const
|
|
10
|
-
|
|
20
|
+
function exposeWorker(scope) {
|
|
21
|
+
const ep = scope ?? globalThis;
|
|
22
|
+
if (typeof ep.postMessage !== "function" || typeof ep.addEventListener !== "function") throw new EnvironmentError("ENVIRONMENT_UNSUPPORTED", "exposeWorker() must run inside a worker (no postMessage/addEventListener here).", { details: { api: "postMessage" } });
|
|
23
|
+
const endpoint = ep;
|
|
24
|
+
const controllers = /* @__PURE__ */ new Map();
|
|
25
|
+
let queue = Promise.resolve();
|
|
26
|
+
endpoint.addEventListener("message", (event) => {
|
|
27
|
+
const msg = event.data;
|
|
28
|
+
if (!isTagged(msg)) return;
|
|
29
|
+
const req = msg;
|
|
30
|
+
if (req.cancel) {
|
|
31
|
+
const c = controllers.get(req.id);
|
|
32
|
+
if (c) {
|
|
33
|
+
c.aborted = true;
|
|
34
|
+
c.reason = abortError("The operation was aborted.");
|
|
35
|
+
}
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
const state = {
|
|
39
|
+
aborted: false,
|
|
40
|
+
reason: void 0
|
|
41
|
+
};
|
|
42
|
+
controllers.set(req.id, state);
|
|
43
|
+
queue = queue.then(() => run(endpoint, req, state)).finally(() => controllers.delete(req.id));
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
async function run(ep, req, signal) {
|
|
47
|
+
const post = (r, transfer) => ep.postMessage({
|
|
48
|
+
tag: TAG,
|
|
49
|
+
id: req.id,
|
|
50
|
+
...r
|
|
51
|
+
}, transfer);
|
|
52
|
+
try {
|
|
53
|
+
if (signal.aborted) throw signal.reason;
|
|
54
|
+
const options = {
|
|
55
|
+
...req.options ?? {},
|
|
56
|
+
signal
|
|
57
|
+
};
|
|
58
|
+
if (req.progress) options["onProgress"] = (p) => post({ progress: p });
|
|
59
|
+
if (req.warnings) options["onWarning"] = (w) => post({ warning: w });
|
|
60
|
+
switch (req.op) {
|
|
61
|
+
case "write": {
|
|
62
|
+
const bytes = await toUint8Array(req.input, options);
|
|
63
|
+
post({
|
|
64
|
+
done: true,
|
|
65
|
+
result: bytes
|
|
66
|
+
}, [bytes.buffer]);
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
case "read":
|
|
70
|
+
post({
|
|
71
|
+
done: true,
|
|
72
|
+
result: await read(req.input, options)
|
|
73
|
+
});
|
|
74
|
+
return;
|
|
75
|
+
case "toCsv":
|
|
76
|
+
delete options["signal"];
|
|
77
|
+
post({
|
|
78
|
+
done: true,
|
|
79
|
+
result: toCsv(req.input, options)
|
|
80
|
+
});
|
|
81
|
+
return;
|
|
82
|
+
case "parseCsv":
|
|
83
|
+
post({
|
|
84
|
+
done: true,
|
|
85
|
+
result: await readCsv(req.input, options)
|
|
86
|
+
});
|
|
87
|
+
return;
|
|
88
|
+
default: throw new TypeError(`Unknown worker operation ${JSON.stringify(req.op)}`);
|
|
89
|
+
}
|
|
90
|
+
} catch (err) {
|
|
91
|
+
post({
|
|
92
|
+
done: true,
|
|
93
|
+
error: serializeError(err)
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
function serializeError(err) {
|
|
98
|
+
if (isExcelError(err)) {
|
|
99
|
+
const out = {
|
|
100
|
+
name: err.name,
|
|
101
|
+
message: err.message,
|
|
102
|
+
code: err.code,
|
|
103
|
+
excel: true
|
|
104
|
+
};
|
|
105
|
+
if (err.details !== void 0) out.details = err.details;
|
|
106
|
+
return out;
|
|
107
|
+
}
|
|
108
|
+
if (err instanceof Error) return {
|
|
109
|
+
name: err.name,
|
|
110
|
+
message: err.message,
|
|
111
|
+
excel: false
|
|
112
|
+
};
|
|
113
|
+
return {
|
|
114
|
+
name: "Error",
|
|
115
|
+
message: String(err),
|
|
116
|
+
excel: false
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
const EXCEL_CLASSES = {
|
|
120
|
+
ValidationError,
|
|
121
|
+
FormatError,
|
|
122
|
+
LimitError,
|
|
123
|
+
CsvError,
|
|
124
|
+
EnvironmentError,
|
|
125
|
+
ExcelError
|
|
126
|
+
};
|
|
127
|
+
function deserializeError(e) {
|
|
128
|
+
if (e.excel) return new (Object.prototype.hasOwnProperty.call(EXCEL_CLASSES, e.name) ? EXCEL_CLASSES[e.name] : ExcelError)(e.code, e.message, e.details === void 0 ? void 0 : { details: e.details });
|
|
129
|
+
const Std = {
|
|
130
|
+
TypeError,
|
|
131
|
+
RangeError,
|
|
132
|
+
SyntaxError,
|
|
133
|
+
ReferenceError
|
|
134
|
+
};
|
|
135
|
+
const err = new (Object.prototype.hasOwnProperty.call(Std, e.name) ? Std[e.name] : Error)(e.message);
|
|
136
|
+
if (err.name !== e.name) err.name = e.name;
|
|
137
|
+
return err;
|
|
138
|
+
}
|
|
139
|
+
/** Wraps a worker that runs `exposeWorker()`. */
|
|
140
|
+
function createWorkerClient(worker) {
|
|
141
|
+
let nextId = 1;
|
|
142
|
+
const pending = /* @__PURE__ */ new Map();
|
|
143
|
+
worker.addEventListener("message", (event) => {
|
|
144
|
+
const msg = event.data;
|
|
145
|
+
if (!isTagged(msg)) return;
|
|
146
|
+
const res = msg;
|
|
147
|
+
const p = pending.get(res.id);
|
|
148
|
+
if (!p) return;
|
|
149
|
+
if (res.progress) p.onProgress?.(res.progress);
|
|
150
|
+
if (res.warning) p.onWarning?.(res.warning);
|
|
151
|
+
if (!res.done) return;
|
|
152
|
+
pending.delete(res.id);
|
|
153
|
+
p.cleanup();
|
|
154
|
+
if (res.error) p.reject(deserializeError(res.error));
|
|
155
|
+
else p.resolve(res.result);
|
|
156
|
+
});
|
|
157
|
+
worker.addEventListener("error", (event) => {
|
|
158
|
+
const reason = new EnvironmentError("ENVIRONMENT_UNSUPPORTED", "The worker failed (see the worker's console for details).", { details: {
|
|
159
|
+
api: "Worker",
|
|
160
|
+
event: String(event.message ?? "")
|
|
161
|
+
} });
|
|
162
|
+
for (const [id, p] of pending) {
|
|
163
|
+
pending.delete(id);
|
|
164
|
+
p.cleanup();
|
|
165
|
+
p.reject(reason);
|
|
166
|
+
}
|
|
167
|
+
});
|
|
168
|
+
function call(op, input, options) {
|
|
169
|
+
const o = { ...options ?? {} };
|
|
170
|
+
const signal = o["signal"];
|
|
171
|
+
const onProgress = o["onProgress"];
|
|
172
|
+
const onWarning = o["onWarning"];
|
|
173
|
+
const transfer = o["transfer"] === true;
|
|
174
|
+
delete o["signal"];
|
|
175
|
+
delete o["onProgress"];
|
|
176
|
+
delete o["onWarning"];
|
|
177
|
+
delete o["transfer"];
|
|
178
|
+
for (const [k, v] of Object.entries(o)) if (typeof v === "function") return Promise.reject(/* @__PURE__ */ new TypeError(`options.${k} is a function, which cannot be sent to a worker`));
|
|
179
|
+
if (signal?.aborted) return Promise.reject(signal.reason ?? abortError("The operation was aborted."));
|
|
180
|
+
const id = nextId++;
|
|
181
|
+
return new Promise((resolve, reject) => {
|
|
182
|
+
const onAbort = () => {
|
|
183
|
+
if (!pending.has(id)) return;
|
|
184
|
+
pending.delete(id);
|
|
185
|
+
worker.postMessage({
|
|
186
|
+
tag: TAG,
|
|
187
|
+
id,
|
|
188
|
+
cancel: true
|
|
189
|
+
});
|
|
190
|
+
reject(signal?.reason ?? abortError("The operation was aborted."));
|
|
191
|
+
};
|
|
192
|
+
signal?.addEventListener?.("abort", onAbort, { once: true });
|
|
193
|
+
pending.set(id, {
|
|
194
|
+
resolve,
|
|
195
|
+
reject,
|
|
196
|
+
onProgress,
|
|
197
|
+
onWarning,
|
|
198
|
+
cleanup: () => signal?.removeEventListener?.("abort", onAbort)
|
|
199
|
+
});
|
|
200
|
+
const req = {
|
|
201
|
+
tag: TAG,
|
|
202
|
+
id,
|
|
203
|
+
op,
|
|
204
|
+
input,
|
|
205
|
+
options: o,
|
|
206
|
+
progress: onProgress !== void 0,
|
|
207
|
+
warnings: onWarning !== void 0
|
|
208
|
+
};
|
|
209
|
+
const list = [];
|
|
210
|
+
if (transfer) {
|
|
211
|
+
if (input instanceof ArrayBuffer) list.push(input);
|
|
212
|
+
else if (input instanceof Uint8Array) list.push(input.buffer);
|
|
213
|
+
}
|
|
214
|
+
try {
|
|
215
|
+
worker.postMessage(req, list);
|
|
216
|
+
} catch (err) {
|
|
217
|
+
pending.delete(id);
|
|
218
|
+
signal?.removeEventListener?.("abort", onAbort);
|
|
219
|
+
reject(err);
|
|
220
|
+
}
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
return {
|
|
224
|
+
write: (wb, options) => call("write", wb, options),
|
|
225
|
+
read: (input, options) => call("read", input, options),
|
|
226
|
+
toCsv: (source, options) => call("toCsv", source, options),
|
|
227
|
+
parseCsv: (input, options) => call("parseCsv", input, options),
|
|
228
|
+
terminate: () => {
|
|
229
|
+
worker.terminate();
|
|
230
|
+
for (const [id, p] of pending) {
|
|
231
|
+
pending.delete(id);
|
|
232
|
+
p.cleanup();
|
|
233
|
+
p.reject(abortError("The worker was terminated."));
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
};
|
|
237
|
+
}
|
|
238
|
+
export { createWorkerClient, exposeWorker };
|