@kupcad/wasm 0.1.0 → 0.1.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 +3 -1
- package/dist/kupcad.wasm +0 -0
- package/package.json +2 -2
- package/src/client.js +5 -3
- package/src/create.js +23 -0
- package/src/index.js +2 -1
- package/src/kupcad.js +7 -8
- package/src/serve.js +7 -6
- package/src/testing.js +1 -1
- package/src/types.d.ts +2 -2
package/README.md
CHANGED
|
@@ -38,7 +38,9 @@ Both take `wasm` (the module: a URL, its bytes or a compiled `WebAssembly.Module
|
|
|
38
38
|
`createKupcad` a fetch `Response` too, which can't be posted to a worker; by default the
|
|
39
39
|
`kupcad.wasm` this package ships) and `log` (where the module's log goes, a line at a time; by
|
|
40
40
|
default `console.debug`). A worker is given a relative URL resolved against the page, as the
|
|
41
|
-
page means it.
|
|
41
|
+
page means it. The worker is always handed its module, this package's by default: only the
|
|
42
|
+
page's side names the shipped `kupcad.wasm` (`src/create.js`), so a site's bundler, which copies
|
|
43
|
+
the file for each bundle that names it, ships it once rather than once more for the worker.
|
|
42
44
|
|
|
43
45
|
## What a call can fail with
|
|
44
46
|
|
package/dist/kupcad.wasm
CHANGED
|
Binary file
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kupcad/wasm",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"description": "The KupCAD core compiled to WebAssembly: preview, build, check, format and read the parameters of a script in the browser or Node, directly or in a Web Worker.",
|
|
5
5
|
"license": "AGPL-3.0-only",
|
|
6
6
|
"repository": {
|
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
"access": "public"
|
|
33
33
|
},
|
|
34
34
|
"dependencies": {
|
|
35
|
-
"@kupcad/protocol": "0.1.
|
|
35
|
+
"@kupcad/protocol": "0.1.2"
|
|
36
36
|
},
|
|
37
37
|
"devDependencies": {
|
|
38
38
|
"@types/node": "^26.6.5",
|
package/src/client.js
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
// The core in a Web Worker, with createKupcad's API: a long build runs there and the page goes
|
|
2
2
|
// on. Calls run one at a time, the next waiting its turn. A call's AbortSignal drops it while
|
|
3
3
|
// it waits, and stops the worker while it runs: the worker is replaced on the next call, as
|
|
4
|
-
// one that crashed is.
|
|
4
|
+
// one that crashed is. The worker is given its module, this package's unless the host's: the
|
|
5
|
+
// page's side alone names it (create.js), so a site's bundler copies it once.
|
|
5
6
|
|
|
6
7
|
import { ProtocolError } from '@kupcad/protocol';
|
|
8
|
+
import { DEFAULT_WASM } from './create.js';
|
|
7
9
|
import { KupcadCrashError, KupcadError } from './errors.js';
|
|
8
10
|
|
|
9
11
|
/** @import { Finding, Preview, UiSchema, VersionInfo } from '@kupcad/protocol/types' */
|
|
@@ -55,7 +57,7 @@ export class KupcadWorker {
|
|
|
55
57
|
/** @param {WorkerOptions} options */
|
|
56
58
|
constructor(options) {
|
|
57
59
|
this.#spawn = options.worker ?? moduleWorker;
|
|
58
|
-
this.#wasm = postable(options.wasm);
|
|
60
|
+
this.#wasm = postable(options.wasm ?? DEFAULT_WASM);
|
|
59
61
|
this.#log = options.log ?? ((line) => console.debug(line));
|
|
60
62
|
}
|
|
61
63
|
|
|
@@ -267,7 +269,7 @@ export class KupcadWorker {
|
|
|
267
269
|
/**
|
|
268
270
|
* The module as the worker is given it: a URL as its href, a relative one resolved against the
|
|
269
271
|
* page (the worker would resolve it against its own script's).
|
|
270
|
-
* @param {WorkerWasmSource
|
|
272
|
+
* @param {WorkerWasmSource} wasm
|
|
271
273
|
* @returns {Extract<ToWorker, { type: 'init' }>['wasm']}
|
|
272
274
|
*/
|
|
273
275
|
function postable(wasm) {
|
package/src/create.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// The module this package ships, and createKupcad, which runs it in this thread by default. The
|
|
2
|
+
// module's URL is named here alone, in what only the page's side of the package loads (this and
|
|
3
|
+
// the client, which hands it to its worker): a site's bundler copies kupcad.wasm for each bundle
|
|
4
|
+
// that names it, so the worker, given its module, names none, and a site ships the file once.
|
|
5
|
+
|
|
6
|
+
import { instantiateKupcad } from './kupcad.js';
|
|
7
|
+
|
|
8
|
+
/** @import { CreateOptions } from './types.js' */
|
|
9
|
+
/** @import { Kupcad } from './kupcad.js' */
|
|
10
|
+
|
|
11
|
+
/** The module this package ships, built by core/build.zig and copied in by `pnpm build`. */
|
|
12
|
+
export const DEFAULT_WASM = new URL('../dist/kupcad.wasm', import.meta.url);
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* The core, instantiated in this thread: on `options.wasm`, else on the module this package
|
|
16
|
+
* ships. A long build blocks it: a page runs the core in a worker (`createKupcadWorker`).
|
|
17
|
+
* @param {CreateOptions} [options]
|
|
18
|
+
* @returns {Promise<Kupcad>}
|
|
19
|
+
* @throws {ProtocolError} when the module speaks a protocol version this package doesn't read
|
|
20
|
+
*/
|
|
21
|
+
export function createKupcad(options = {}) {
|
|
22
|
+
return instantiateKupcad({ ...options, wasm: options.wasm ?? DEFAULT_WASM });
|
|
23
|
+
}
|
package/src/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// @kupcad/wasm: the core compiled to WebAssembly, in this thread (createKupcad) or in a Web
|
|
2
2
|
// Worker (createKupcadWorker), with one API.
|
|
3
3
|
|
|
4
|
-
export { createKupcad
|
|
4
|
+
export { createKupcad } from './create.js';
|
|
5
|
+
export { Kupcad } from './kupcad.js';
|
|
5
6
|
export { createKupcadWorker } from './client.js';
|
|
6
7
|
export { KupcadCrashError, KupcadError } from './errors.js';
|
package/src/kupcad.js
CHANGED
|
@@ -10,10 +10,8 @@ import { loadModule } from './load.js';
|
|
|
10
10
|
import { createWasi, WasiExit } from './wasi.js';
|
|
11
11
|
|
|
12
12
|
/** @import { Finding, Preview, UiSchema, VersionInfo } from '@kupcad/protocol/types' */
|
|
13
|
-
/** @import { BuildOptions, CallOptions, CreateOptions, KupcadApi, KupcadExports, PreviewOptions } from './types.js' */
|
|
13
|
+
/** @import { BuildOptions, CallOptions, CreateOptions, KupcadApi, KupcadExports, PreviewOptions, WasmSource } from './types.js' */
|
|
14
14
|
|
|
15
|
-
/** The module this package ships, built by core/build.zig and copied in by `pnpm build`. */
|
|
16
|
-
const DEFAULT_WASM = new URL('../dist/kupcad.wasm', import.meta.url);
|
|
17
15
|
/** A `usize` of wasm32: the slot an export writes its result's length to. */
|
|
18
16
|
const USIZE_BYTES = 4;
|
|
19
17
|
/** The longest message get_last_error holds (core/src/wasm.zig's MAX_ERROR_MSG_LEN). */
|
|
@@ -33,14 +31,15 @@ const decoder = new TextDecoder();
|
|
|
33
31
|
const address = (ptr) => ptr >>> 0;
|
|
34
32
|
|
|
35
33
|
/**
|
|
36
|
-
* The core, instantiated in this thread
|
|
37
|
-
* worker
|
|
38
|
-
*
|
|
34
|
+
* The core, instantiated in this thread from the module `wasm`: createKupcad's, which names the
|
|
35
|
+
* module this package ships, and the worker's, which is given its module. Naming none here keeps
|
|
36
|
+
* the shipped module's URL out of the worker's bundle (create.js).
|
|
37
|
+
* @param {CreateOptions & { wasm: WasmSource }} options
|
|
39
38
|
* @returns {Promise<Kupcad>}
|
|
40
39
|
* @throws {ProtocolError} when the module speaks a protocol version this package doesn't read
|
|
41
40
|
*/
|
|
42
|
-
export async function
|
|
43
|
-
const module = await loadModule(options.wasm
|
|
41
|
+
export async function instantiateKupcad(options) {
|
|
42
|
+
const module = await loadModule(options.wasm);
|
|
44
43
|
const wasi = createWasi(options.log ?? ((line) => console.debug(line)));
|
|
45
44
|
const instance = await WebAssembly.instantiate(module, {
|
|
46
45
|
wasi_snapshot_preview1: wasi.imports(module),
|
package/src/serve.js
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
// The worker's side: the core instantiated in the worker
|
|
2
|
-
// `port` in the order they come. A result's buffer is
|
|
1
|
+
// The worker's side: the core instantiated in the worker on the module the client gives it,
|
|
2
|
+
// answering the client's calls on `port` in the order they come. A result's buffer is
|
|
3
|
+
// transferred, not copied.
|
|
3
4
|
|
|
4
|
-
import {
|
|
5
|
+
import { instantiateKupcad } from './kupcad.js';
|
|
5
6
|
import { KupcadCrashError } from './errors.js';
|
|
6
7
|
|
|
7
8
|
/** @import { Preview } from '@kupcad/protocol/types' */
|
|
8
|
-
/** @import { CreateOptions, FromWorker, KupcadApi, Method, PortLike, SentError, ToWorker } from './types.js' */
|
|
9
|
+
/** @import { CreateOptions, FromWorker, KupcadApi, Method, PortLike, SentError, ToWorker, WasmSource } from './types.js' */
|
|
9
10
|
|
|
10
11
|
/** @type {Record<Method, (kupcad: KupcadApi, args: any[]) => Promise<unknown>>} */
|
|
11
12
|
const CALLS = {
|
|
@@ -21,9 +22,9 @@ const CALLS = {
|
|
|
21
22
|
* Serves the core on `port`: an `init` message makes it (`create`, the module given), and each
|
|
22
23
|
* `call` is answered with a `reply`. The core's log goes as `log` messages.
|
|
23
24
|
* @param {PortLike} port
|
|
24
|
-
* @param {(options: CreateOptions) => Promise<KupcadApi>} [create]
|
|
25
|
+
* @param {(options: CreateOptions & { wasm: WasmSource }) => Promise<KupcadApi>} [create]
|
|
25
26
|
*/
|
|
26
|
-
export function serve(port, create =
|
|
27
|
+
export function serve(port, create = instantiateKupcad) {
|
|
27
28
|
/** @type {Promise<KupcadApi> | null} */
|
|
28
29
|
let kupcad = null;
|
|
29
30
|
/** @param {FromWorker} message @param {Transferable[]} [transfer] */
|
package/src/testing.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// For the tests of a package that shows what the core makes: the real module, made once a test
|
|
2
2
|
// process and silent, and what it gives of a script. A script that fails fails the test.
|
|
3
3
|
|
|
4
|
-
import { createKupcad } from './
|
|
4
|
+
import { createKupcad } from './create.js';
|
|
5
5
|
|
|
6
6
|
/** @import { Model, UiSchema } from '@kupcad/protocol/types' */
|
|
7
7
|
/** @import { Kupcad } from './kupcad.js' */
|
package/src/types.d.ts
CHANGED
|
@@ -115,8 +115,8 @@ export interface SentError {
|
|
|
115
115
|
export type ToWorker =
|
|
116
116
|
| {
|
|
117
117
|
type: 'init';
|
|
118
|
-
/**
|
|
119
|
-
wasm: Exclude<WorkerWasmSource, URL
|
|
118
|
+
/** The module (a URL as its href): the host's, else the one the package ships. */
|
|
119
|
+
wasm: Exclude<WorkerWasmSource, URL>;
|
|
120
120
|
}
|
|
121
121
|
| { type: 'call'; id: number; method: Method; args: unknown[] };
|
|
122
122
|
|