@open-artifact/wasm-canvas 0.0.0-stage → 0.1.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/LICENSE +202 -0
- package/NOTICE +5 -0
- package/README.md +5 -2
- package/generated/bytes.d.ts +3 -0
- package/generated/bytes.js +2 -0
- package/generated/open_artifact_canvas.d.ts +172 -0
- package/generated/open_artifact_canvas.js +910 -0
- package/package.json +34 -4
- package/src/index.d.ts +20 -0
- package/src/index.js +32 -0
package/package.json
CHANGED
|
@@ -1,6 +1,36 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@open-artifact/wasm-canvas",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "crates/open-artifact-canvas compiled to WebAssembly (generated/, built by scripts/build-wasm.mjs).",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"types": "./src/index.d.ts",
|
|
9
|
+
"default": "./src/index.js"
|
|
10
|
+
},
|
|
11
|
+
"./package.json": "./package.json"
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"src",
|
|
15
|
+
"generated",
|
|
16
|
+
"LICENSE",
|
|
17
|
+
"NOTICE"
|
|
18
|
+
],
|
|
19
|
+
"license": "Apache-2.0",
|
|
20
|
+
"author": {
|
|
21
|
+
"name": "Jim Liu 宝玉",
|
|
22
|
+
"url": "https://github.com/JimLiu"
|
|
23
|
+
},
|
|
24
|
+
"homepage": "https://github.com/JimLiu/open-artifact#readme",
|
|
25
|
+
"repository": {
|
|
26
|
+
"type": "git",
|
|
27
|
+
"url": "git+https://github.com/JimLiu/open-artifact.git",
|
|
28
|
+
"directory": "packages/wasm-canvas"
|
|
29
|
+
},
|
|
30
|
+
"bugs": {
|
|
31
|
+
"url": "https://github.com/JimLiu/open-artifact/issues"
|
|
32
|
+
},
|
|
33
|
+
"publishConfig": {
|
|
34
|
+
"access": "public"
|
|
35
|
+
}
|
|
36
|
+
}
|
package/src/index.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The canvas engine core (`crates/open-artifact-canvas`) compiled to WebAssembly. The bytes are
|
|
3
|
+
* embedded (base64), so this works the same in Node and in the editor pages.
|
|
4
|
+
*
|
|
5
|
+
* Loading is asynchronous: the module is compiled with `WebAssembly.compile` (off
|
|
6
|
+
* the main thread's critical path in browsers) and instantiated once. Use
|
|
7
|
+
* `@open-artifact/canvas-engine`, not this package, from editors.
|
|
8
|
+
*/
|
|
9
|
+
import * as glue from '../generated/open_artifact_canvas.js';
|
|
10
|
+
export type { CanvasCore } from '../generated/open_artifact_canvas.js';
|
|
11
|
+
/** The module's exports once loaded. */
|
|
12
|
+
export interface CanvasWasm {
|
|
13
|
+
CanvasCore: typeof glue.CanvasCore;
|
|
14
|
+
/** The instance's linear memory (for size reporting). */
|
|
15
|
+
memory: WebAssembly.Memory;
|
|
16
|
+
}
|
|
17
|
+
/** Compiles and instantiates the module on the first call; later calls share it. */
|
|
18
|
+
export declare function loadCanvasWasm(): Promise<CanvasWasm>;
|
|
19
|
+
/** Size of the embedded module in bytes (decoded). */
|
|
20
|
+
export declare function canvasWasmSize(): number;
|
package/src/index.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The canvas engine core (`crates/open-artifact-canvas`) compiled to WebAssembly. The bytes are
|
|
3
|
+
* embedded (base64), so this works the same in Node and in the editor pages.
|
|
4
|
+
*
|
|
5
|
+
* Loading is asynchronous: the module is compiled with `WebAssembly.compile` (off
|
|
6
|
+
* the main thread's critical path in browsers) and instantiated once. Use
|
|
7
|
+
* `@open-artifact/canvas-engine`, not this package, from editors.
|
|
8
|
+
*/
|
|
9
|
+
import * as glue from '../generated/open_artifact_canvas.js';
|
|
10
|
+
import { WASM_BASE64 } from '../generated/bytes.js';
|
|
11
|
+
let loading;
|
|
12
|
+
function decode(base64) {
|
|
13
|
+
const binary = atob(base64);
|
|
14
|
+
const bytes = new Uint8Array(binary.length);
|
|
15
|
+
for (let index = 0; index < binary.length; index++)
|
|
16
|
+
bytes[index] = binary.charCodeAt(index);
|
|
17
|
+
return bytes;
|
|
18
|
+
}
|
|
19
|
+
/** Compiles and instantiates the module on the first call; later calls share it. */
|
|
20
|
+
export function loadCanvasWasm() {
|
|
21
|
+
loading ??= (async () => {
|
|
22
|
+
const module = await WebAssembly.compile(decode(WASM_BASE64));
|
|
23
|
+
const exports = glue.initSync({ module });
|
|
24
|
+
return { CanvasCore: glue.CanvasCore, memory: exports.memory };
|
|
25
|
+
})();
|
|
26
|
+
return loading;
|
|
27
|
+
}
|
|
28
|
+
/** Size of the embedded module in bytes (decoded). */
|
|
29
|
+
export function canvasWasmSize() {
|
|
30
|
+
const padding = WASM_BASE64.endsWith('==') ? 2 : WASM_BASE64.endsWith('=') ? 1 : 0;
|
|
31
|
+
return (WASM_BASE64.length * 3) / 4 - padding;
|
|
32
|
+
}
|