@d0paminedriven/xlsxdown-wasm 0.0.0-stage → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,3 +1,132 @@
1
- # Temporary Holding Version
1
+ # @d0paminedriven/xlsxdown-wasm
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ [![CI](https://github.com/DopamineDriven/assetdown/actions/workflows/xlsxdown-node.yml/badge.svg?branch=main)](https://github.com/DopamineDriven/assetdown/actions/workflows/xlsxdown-node.yml)
4
+
5
+ Reads Excel workbooks (`.xlsx`, and the macro-enabled and template kinds, `.xlsm`, `.xltx`, and `.xltm`) as WebAssembly: typed cells with their display text and their formulas, sheet geometry, pages, invoice fields, and counts of what a workbook holds.
6
+
7
+ It is [`@d0paminedriven/xlsxdown`](https://www.npmjs.com/package/@d0paminedriven/xlsxdown) with no native addon behind it: the same Rust parser, built to run in Node, on a page, and in a worker runtime. Reach for it when the code has to run somewhere a native addon cannot, or when you want the same build on every machine. In Node alone, the main package loads a native addon where there is one and falls back to WebAssembly where there is not.
8
+
9
+ ```sh
10
+ npm install @d0paminedriven/xlsxdown-wasm
11
+ yarn add @d0paminedriven/xlsxdown-wasm
12
+ pnpm add @d0paminedriven/xlsxdown-wasm
13
+ ```
14
+
15
+ node ≥ 24. There is nothing else to install, and no flag to pass.
16
+
17
+ ## Which entry to import
18
+
19
+ | Import | What it is | Where it runs |
20
+ | ------------------------------------------ | --------------------------------------------------------------------------------------- | ---------------------------------------------- |
21
+ | `@d0paminedriven/xlsxdown-wasm` | In Node, the build with threads. To a bundler building for a browser, the build without | Node; a page (see the limits below) |
22
+ | `@d0paminedriven/xlsxdown-wasm/worker` | The parser in a Web Worker of its own, answering by a promise | A page. This is the one to use in a browser |
23
+ | `@d0paminedriven/xlsxdown-wasm/threads` | The build with threads | Node, and a page that is cross-origin isolated |
24
+ | `@d0paminedriven/xlsxdown-wasm/threadless` | The build without threads | Anywhere WebAssembly runs |
25
+ | `@d0paminedriven/xlsxdown-wasm/workerd` | The build without threads, started from a module you compiled | A worker runtime such as Cloudflare Workers |
26
+
27
+ Every entry says which build it is in `__napiBindingTarget`: `"wasm32-wasi"` with threads, `"wasm32-wasip1"` without. It is never `"native"`.
28
+
29
+ ## In Node
30
+
31
+ ```ts
32
+ import { readFile } from "node:fs/promises";
33
+
34
+ import {
35
+ XlsxDown,
36
+ __napiBindingTarget,
37
+ spreadsheetCellsAsync
38
+ } from "@d0paminedriven/xlsxdown-wasm";
39
+
40
+ const file = await readFile("costs.xlsx");
41
+
42
+ const sheets = await spreadsheetCellsAsync(file); // every visible sheet's cells
43
+ const workbook = new XlsxDown(file); // parsed once, and asked as often as needed
44
+ workbook.metadata().sheetCount;
45
+ workbook.invoice().fields.invoiceId?.value; // the invoice's number, where it has one
46
+ __napiBindingTarget; // "wasm32-wasi"
47
+ ```
48
+
49
+ In this build the `*Async` verbs work off the main thread.
50
+
51
+ ## On a page
52
+
53
+ ```ts
54
+ import { XlsxWorker } from "@d0paminedriven/xlsxdown-wasm/worker";
55
+
56
+ const xlsx = new XlsxWorker();
57
+
58
+ // `file` is a File, from an <input type="file"> or a drop.
59
+ const bytes = new Uint8Array(await file.arrayBuffer());
60
+
61
+ const sheets = await xlsx.spreadsheetCellsAsync(bytes, { hiddenSheets: true });
62
+
63
+ xlsx.terminate();
64
+ ```
65
+
66
+ - **The verb is the build's own promise verb**, by the same name and with the same answer, so code written against `spreadsheetCellsAsync` runs on the worker unchanged.
67
+ - **The page stays free.** The workbook is read in the worker, and the page's own thread is not held.
68
+ - **No isolation is needed.** The page does not have to be cross-origin isolated, and the server needs no special headers. It does not even have to call the `.wasm` file `application/wasm`.
69
+ - **Bytes go in as a `Uint8Array`.** A page has no `Buffer`.
70
+ - **`xlsx.ready`** resolves with the build's name once the worker can answer, and rejects with the reason if it could not start. Awaiting it is optional: a call made before then is answered once the worker is ready.
71
+ - **`xlsx.terminate()`** ends the worker and gives its memory back, which is the only way WebAssembly memory is ever given back. Calls still waiting, and any made after, are rejected. It is also how to stop a workbook that is taking too long.
72
+ - **Memory** starts at 64 MiB and grows as a workbook needs.
73
+
74
+ The worker is two files, `dist/worker.js` and `dist/worker.wasm`, and is found beside the client that starts it. If your setup serves them from somewhere else, put the two in one folder and say where the script is:
75
+
76
+ ```ts
77
+ const xlsx = new XlsxWorker("/workers/xlsxdown/worker.js");
78
+ ```
79
+
80
+ The worker answers `spreadsheetCellsAsync`, which is the one verb the package has as a function. The rest (the grid, the pages, the invoice, and the counts) are methods of `XlsxDown`, which holds a parsed workbook and does not cross to a worker yet.
81
+
82
+ ### Without the worker
83
+
84
+ Importing `@d0paminedriven/xlsxdown-wasm` on a page, through a bundler that honors the `browser` condition, gives the build without threads on the page's own thread, `XlsxDown` included. It works, with two limits:
85
+
86
+ - the page is held while a workbook is read, the `*Async` verbs included; and
87
+ - memory starts at 250 MiB.
88
+
89
+ The worker has neither.
90
+
91
+ ## In a worker runtime
92
+
93
+ ```ts
94
+ import wasmModule from "@d0paminedriven/xlsxdown-wasm/wasm.wasm";
95
+ import { instantiate } from "@d0paminedriven/xlsxdown-wasm/workerd";
96
+
97
+ const xlsx = await instantiate(wasmModule);
98
+
99
+ xlsx.spreadsheetCells(bytes);
100
+ new xlsx.XlsxDown(bytes).metadata();
101
+ ```
102
+
103
+ This entry takes a `WebAssembly.Module` and reads no file, which is what a runtime that hands modules over at build time wants. Memory starts at 64 MiB.
104
+
105
+ ## The verbs
106
+
107
+ | Verb | Gives |
108
+ | -------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
109
+ | `spreadsheetCells` | The cells of the selected sheets: each typed (text, number, date, time, boolean, error), with its display text, its number format, and its box in the sheet. Its `Async` twin returns a promise |
110
+ | `new XlsxDown(bytes)`, `XlsxDown.openAsync(bytes)` | A workbook parsed once, to ask more than one thing of |
111
+ | `.spreadsheetCells()`, `.spreadsheetGrid()` | The cells, as a list or keyed by row and column |
112
+ | `.layout()` | Each sheet set on a page of positioned words |
113
+ | `.invoice()` | Invoice fields read from those pages, with the total and the line items taken from the formulas, each with where it was found |
114
+ | `.metadata()` | Counts of what the workbook holds, sheet by sheet and in total |
115
+
116
+ Every method of `XlsxDown` has an `Async` twin.
117
+
118
+ A formula gives the value Excel last left beside it, and its text in `formula`: `SUM(B2:B9)*Rate`. A cell that follows a shared formula has it written for itself, with the references moved to it. A workbook in the strict form of the format, and a package that is not a workbook (a Word document, a presentation), is refused by name.
119
+
120
+ ## What has been tried
121
+
122
+ The tests run both builds in Node, by the names above, and run the worker on a page in Chrome, holding its answers against what the same build gives in Node.
123
+
124
+ Not yet tried: Firefox and Safari, a bundler's own handling of the worker's file, the build with threads on a page, and the `workerd` entry inside a worker runtime (it is tested in Node).
125
+
126
+ ## Size
127
+
128
+ The package is about 1.5 MB packed. Each build is about 1.1 MB, the build without threads is in the package twice (once for the worker), and the worker's script is 0.5 MB, 84 kB gzipped.
129
+
130
+ ## License
131
+
132
+ MIT
@@ -0,0 +1,36 @@
1
+ import { AsyncBindingKeys, BindingArgs, BindingKeys, BindingRT, PageVerbs, SyncBindingKeys, Target, Wire } from "./protocol.js";
2
+ /**
3
+ * The parser in a worker of its own: the same verbs, each answered by a
4
+ * promise, with the page left free while a workbook is read.
5
+ *
6
+ * The worker holds its own copy of the WebAssembly build and its memory.
7
+ * `terminate()` ends it and gives all of that back, which is the only way
8
+ * WebAssembly memory is ever given back; calls still waiting, and any made
9
+ * after, are rejected.
10
+ *
11
+ * Its verbs are the build's own promise verbs, by the same names and with the
12
+ * same answers, so that code written against the build runs on this.
13
+ */
14
+ export declare class XlsxWorker implements PageVerbs {
15
+ /** Resolves with the build's name once the worker can answer; rejects if it cannot start. */
16
+ readonly ready: Promise<Target>;
17
+ private readonly worker;
18
+ private readonly waiting;
19
+ private next;
20
+ private ended;
21
+ /**
22
+ * `url` is where the worker's file is served from. It is found beside this
23
+ * one unless told otherwise; an app that copies it elsewhere says where.
24
+ */
25
+ constructor(url?: string | URL);
26
+ spreadsheetCellsAsync(...args: BindingArgs<"spreadsheetCellsAsync">): Promise<BindingRT<"spreadsheetCellsAsync">>;
27
+ /** Ends the worker and gives back its memory. Calls still waiting are rejected. */
28
+ terminate(): void;
29
+ /** Sends one call, and gives the promise its answer settles. */
30
+ private ask;
31
+ private settle;
32
+ private take;
33
+ private fail;
34
+ private end;
35
+ }
36
+ export type { AsyncBindingKeys, BindingArgs, BindingKeys, BindingRT, PageVerbs, SyncBindingKeys, Target, Wire };
package/dist/client.js ADDED
@@ -0,0 +1,102 @@
1
+ // src/client.ts
2
+ /**
3
+ * The parser in a worker of its own: the same verbs, each answered by a
4
+ * promise, with the page left free while a workbook is read.
5
+ *
6
+ * The worker holds its own copy of the WebAssembly build and its memory.
7
+ * `terminate()` ends it and gives all of that back, which is the only way
8
+ * WebAssembly memory is ever given back; calls still waiting, and any made
9
+ * after, are rejected.
10
+ *
11
+ * Its verbs are the build's own promise verbs, by the same names and with the
12
+ * same answers, so that code written against the build runs on this.
13
+ */
14
+ var XlsxWorker = class {
15
+ /** Resolves with the build's name once the worker can answer; rejects if it cannot start. */
16
+ ready;
17
+ worker;
18
+ waiting = { spreadsheetCells: /* @__PURE__ */ new Map() };
19
+ next = 0;
20
+ ended;
21
+ /**
22
+ * `url` is where the worker's file is served from. It is found beside this
23
+ * one unless told otherwise; an app that copies it elsewhere says where.
24
+ */
25
+ constructor(url = new URL("./worker.js", import.meta.url)) {
26
+ const starting = Promise.withResolvers();
27
+ this.ready = starting.promise;
28
+ this.worker = new Worker(url, { type: "module" });
29
+ this.worker.addEventListener("message", ({ data }) => {
30
+ switch (data.type) {
31
+ case "ready":
32
+ starting.resolve(data.target);
33
+ break;
34
+ case "broken":
35
+ this.end(new Error(data.message));
36
+ starting.reject(new Error(data.message));
37
+ break;
38
+ case "answer":
39
+ this.settle(data);
40
+ break;
41
+ case "failed": this.fail(data.id, new Error(data.message));
42
+ }
43
+ });
44
+ this.worker.addEventListener("error", (event) => {
45
+ const error = new Error(event.message || "the worker could not start");
46
+ this.end(error);
47
+ starting.reject(error);
48
+ });
49
+ }
50
+ spreadsheetCellsAsync(...args) {
51
+ return this.ask(this.waiting.spreadsheetCells, (id) => ({
52
+ type: "call",
53
+ id,
54
+ verb: "spreadsheetCells",
55
+ args
56
+ }));
57
+ }
58
+ /** Ends the worker and gives back its memory. Calls still waiting are rejected. */
59
+ terminate() {
60
+ this.worker.terminate();
61
+ this.end(/* @__PURE__ */ new Error("the worker was ended"));
62
+ }
63
+ /** Sends one call, and gives the promise its answer settles. */
64
+ ask(calls, call) {
65
+ const { promise, resolve, reject } = Promise.withResolvers();
66
+ if (this.ended) {
67
+ reject(this.ended);
68
+ return promise;
69
+ }
70
+ const id = this.next++;
71
+ calls.set(id, {
72
+ resolve,
73
+ reject
74
+ });
75
+ this.worker.postMessage(call(id));
76
+ return promise;
77
+ }
78
+ settle(data) {
79
+ switch (data.verb) {
80
+ case "spreadsheetCells": return this.take(this.waiting.spreadsheetCells, data.id)?.resolve(data.value);
81
+ }
82
+ }
83
+ take(calls, id) {
84
+ const call = calls.get(id);
85
+ calls.delete(id);
86
+ return call;
87
+ }
88
+ fail(id, error) {
89
+ for (const calls of Object.values(this.waiting)) {
90
+ calls.get(id)?.reject(error);
91
+ calls.delete(id);
92
+ }
93
+ }
94
+ end(error) {
95
+ this.ended = error;
96
+ for (const calls of Object.values(this.waiting)) {
97
+ for (const call of calls.values()) call.reject(error);
98
+ calls.clear();
99
+ }
100
+ }
101
+ };
102
+ export { XlsxWorker };
@@ -0,0 +1,72 @@
1
+ /** The build the worker runs: the one without threads, which a plain page can load. */
2
+ type Binding = typeof import("@d0paminedriven/xlsxdown-wasm/threadless");
3
+ type Rm<T, P extends keyof T = keyof T> = { [S in keyof T as Exclude<S, P>]: T[S]; };
4
+ /**
5
+ * A value as it reaches the page. The build calls its bytes a `Buffer`, which
6
+ * a page does not have: they arrive as a plain `Uint8Array`.
7
+ */
8
+ type OnThePage<T> = T extends Uint8Array ? Uint8Array : T extends readonly (infer U)[] ? OnThePage<U>[] : T extends object ? { [K in keyof T]: OnThePage<T[K]>; } : T;
9
+ /** A verb's arguments, each as the page gives it. A tuple stays the tuple it is. */
10
+ type EachOnThePage<T extends readonly unknown[]> = { [I in keyof T]: OnThePage<T[I]>; };
11
+ /** The build's verbs: its functions, without its class and its name for itself. */
12
+ type BindingNarrowed = Rm<Binding, "XlsxDown" | "__napiBindingTarget">;
13
+ export type BindingKeys = keyof BindingNarrowed;
14
+ type AsyncBindingKeysWorkup<T> = T extends `${string}Async` ? T : never;
15
+ /** The verbs that answer with a promise: the ones a page calls on the worker. */
16
+ export type AsyncBindingKeys = AsyncBindingKeysWorkup<BindingKeys>;
17
+ /** The verbs that answer at once: the ones the worker runs. */
18
+ export type SyncBindingKeys = Exclude<BindingKeys, AsyncBindingKeys>;
19
+ /**
20
+ * What a verb gives, as it reaches the page. For a verb that gives a promise
21
+ * this is what the promise holds: `Awaited` leaves anything else as it is, so
22
+ * a verb and its `Async` twin give the same.
23
+ */
24
+ export type BindingRT<K extends BindingKeys> = OnThePage<Awaited<ReturnType<BindingNarrowed[K]>>>;
25
+ /** What a verb takes, as the page gives it. */
26
+ export type BindingArgs<K extends BindingKeys> = EachOnThePage<Parameters<BindingNarrowed[K]>>;
27
+ /**
28
+ * The build's own promise verbs, as a page has them. The worker's client is
29
+ * one of these, so code written against the build's promises runs on it.
30
+ */
31
+ export type PageVerbs = { [K in AsyncBindingKeys]: (...args: BindingArgs<K>) => Promise<BindingRT<K>>; };
32
+ /** Which build answered. */
33
+ export type Target = Binding["__napiBindingTarget"];
34
+ export declare namespace Wire {
35
+ namespace PageToWorker {
36
+ /** One call for each verb the worker runs, so that the verb says what `args` are. */
37
+ type Call = { [K in SyncBindingKeys]: {
38
+ type: "call";
39
+ /** Pairs the answer with the call: the worker gives it back unchanged. */
40
+ id: number;
41
+ verb: K;
42
+ args: BindingArgs<K>;
43
+ }; }[SyncBindingKeys];
44
+ }
45
+ namespace WorkerToPage {
46
+ /** The build is loaded, and calls will be answered. */
47
+ interface Ready {
48
+ type: "ready";
49
+ target: Target;
50
+ }
51
+ /** The build did not load: no call will be answered. */
52
+ interface Broken {
53
+ type: "broken";
54
+ message: string;
55
+ }
56
+ /** One answer for each verb, so that the verb says what `value` is. */
57
+ type Answer = { [K in SyncBindingKeys]: {
58
+ type: "answer";
59
+ id: number;
60
+ verb: K;
61
+ value: BindingRT<K>;
62
+ }; }[SyncBindingKeys];
63
+ interface Failed {
64
+ type: "failed";
65
+ id: number;
66
+ message: string;
67
+ }
68
+ }
69
+ type PageToWorker = PageToWorker.Call;
70
+ type WorkerToPage = WorkerToPage.Ready | WorkerToPage.Broken | WorkerToPage.Answer | WorkerToPage.Failed;
71
+ }
72
+ export {};
File without changes