@vector-im/matrix-wysiwyg 2.37.13 → 2.38.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.
@@ -0,0 +1,61 @@
1
+ # `wysiwyg-wasm`
2
+
3
+ WASM/JavaScript bindings for wysiwyg-rust.
4
+
5
+ ## Building
6
+
7
+ * [Install Rust](https://www.rust-lang.org/tools/install)
8
+ * [Install NodeJS and NPM](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm)
9
+ * [Install wasm-pack](https://rustwasm.github.io/wasm-pack/installer/)
10
+ * Run:
11
+
12
+ ```sh
13
+ cd bindings/wysiwyg-wasm
14
+ yarn
15
+ yarn build
16
+ #yarn test (no tests yet)
17
+ ```
18
+
19
+ This will generate:
20
+
21
+ ```
22
+ pkg/matrix_sdk_wysiwyg_bg.wasm
23
+ pkg/matrix_sdk_wysiwyg_bg.wasm.d.ts
24
+ pkg/matrix_sdk_wysiwyg.d.ts
25
+ pkg/matrix_sdk_wysiwyg.js
26
+ ... plus other files
27
+ ```
28
+
29
+ You can then consume these files in your project by linking the package in your package.json:
30
+
31
+ ```json
32
+ {
33
+ "dependencies": {
34
+ "@matrix-org/matrix-sdk-wysiwyg-wasm": "link:../../bindings/wysiwyg-wasm"
35
+ }
36
+ }
37
+ ```
38
+
39
+ And consume with code like this:
40
+
41
+ ```html
42
+ <script type="module">
43
+ import { initAsync, some_method_from_rust } from '@matrix-org/matrix-sdk-wysiwyg-wasm';
44
+
45
+ async function run() {
46
+ await initAsync();
47
+ some_method_from_rust();
48
+ }
49
+
50
+ run();
51
+ </script>
52
+ ```
53
+
54
+ ## Profiling
55
+
56
+ To generate a debugging/profiling Wasm module, use the following command
57
+ instead of `yarn build`:
58
+
59
+ ```sh
60
+ $ yarn dev-build
61
+ ```
@@ -0,0 +1,64 @@
1
+ /*
2
+ Copyright 2024 New Vector Ltd.
3
+
4
+ SPDX-License-Identifier: AGPL-3.0-only
5
+ Please see LICENSE in the repository root for full details.
6
+ */
7
+
8
+ // @ts-check
9
+
10
+ /**
11
+ * This is the entrypoint on non-node ESM environments which support the ES Module Integration Proposal for WebAssembly [1]
12
+ * (such as Element Web).
13
+ *
14
+ * [1]: https://github.com/webassembly/esm-integration
15
+ */
16
+
17
+ import * as bindings from './pkg/wysiwyg_bg.js';
18
+
19
+ // We want to throw an error if the user tries to use the bindings before
20
+ // calling `initAsync`.
21
+ bindings.__wbg_set_wasm(
22
+ new Proxy(
23
+ {},
24
+ {
25
+ get() {
26
+ throw new Error(
27
+ '@element-hq/matrix-wysiwyg was used before it was initialized. Call `initAsync` first.',
28
+ );
29
+ },
30
+ },
31
+ ),
32
+ );
33
+
34
+ /**
35
+ * Stores a promise of the `loadModule` call
36
+ * @type {Promise<void> | null}
37
+ */
38
+ let modPromise = null;
39
+
40
+ /**
41
+ * Loads the WASM module asynchronously
42
+ *
43
+ * @returns {Promise<void>}
44
+ */
45
+ async function loadModule() {
46
+ const wasm = await import('./pkg/wysiwyg_bg.wasm');
47
+ bindings.__wbg_set_wasm(wasm);
48
+ wasm.__wbindgen_start();
49
+ }
50
+
51
+ /**
52
+ * Load the WebAssembly module in the background, if it has not already been loaded.
53
+ *
54
+ * Returns a promise which will resolve once the other methods are ready.
55
+ *
56
+ * @returns {Promise<void>}
57
+ */
58
+ export async function initAsync() {
59
+ if (!modPromise) modPromise = loadModule();
60
+ await modPromise;
61
+ }
62
+
63
+ // Re-export everything from the generated javascript wrappers
64
+ export * from './pkg/wysiwyg_bg.js';
@@ -0,0 +1,85 @@
1
+ /*
2
+ Copyright 2024 New Vector Ltd.
3
+
4
+ SPDX-License-Identifier: AGPL-3.0-only
5
+ Please see LICENSE in the repository root for full details.
6
+ */
7
+
8
+ // @ts-check
9
+
10
+ /**
11
+ * This is the entrypoint on non-node CommonJS environments.
12
+ * `initAsync` will load the WASM module using a `fetch` call.
13
+ */
14
+
15
+ const bindings = require("./pkg/wysiwyg_bg.cjs");
16
+
17
+ const moduleUrl = require.resolve("./pkg/wysiwyg_bg.wasm");
18
+
19
+ // We want to throw an error if the user tries to use the bindings before
20
+ // calling `initAsync`.
21
+ bindings.__wbg_set_wasm(
22
+ new Proxy(
23
+ {},
24
+ {
25
+ get() {
26
+ throw new Error(
27
+ '@element-hq/matrix-wysiwyg was used before it was initialized. Call `initAsync` first.',
28
+ );
29
+ },
30
+ },
31
+ ),
32
+ );
33
+
34
+ /**
35
+ * Stores a promise of the `loadModule` call
36
+ * @type {Promise<void> | null}
37
+ */
38
+ let modPromise = null;
39
+
40
+ /**
41
+ * Loads the WASM module asynchronously
42
+ *
43
+ * @returns {Promise<void>}
44
+ */
45
+ async function loadModule() {
46
+ let mod;
47
+ if (typeof WebAssembly.compileStreaming === 'function') {
48
+ mod = await WebAssembly.compileStreaming(fetch(moduleUrl));
49
+ } else {
50
+ // Fallback to fetch and compile
51
+ const response = await fetch(moduleUrl);
52
+ if (!response.ok) {
53
+ throw new Error(`Failed to fetch wasm module: ${moduleUrl}`);
54
+ }
55
+ const bytes = await response.arrayBuffer();
56
+ mod = await WebAssembly.compile(bytes);
57
+ }
58
+
59
+ /** @type {{exports: typeof import("./pkg/wysiwyg_bg.wasm.d.ts")}} */
60
+ // @ts-expect-error: Typescript doesn't know what the instance exports exactly
61
+ const instance = await WebAssembly.instantiate(mod, {
62
+ './wysiwyg_bg.js': bindings,
63
+ });
64
+
65
+ bindings.__wbg_set_wasm(instance.exports);
66
+ instance.exports.__wbindgen_start();
67
+ }
68
+
69
+ /**
70
+ * Load the WebAssembly module in the background, if it has not already been loaded.
71
+ *
72
+ * Returns a promise which will resolve once the other methods are ready.
73
+ *
74
+ * @returns {Promise<void>}
75
+ */
76
+ async function initAsync() {
77
+ if (!modPromise) modPromise = loadModule();
78
+ await modPromise;
79
+ }
80
+
81
+ module.exports = {
82
+ // Re-export everything from the generated javascript wrappers
83
+ ...bindings,
84
+ initAsync,
85
+ };
@@ -0,0 +1,17 @@
1
+ /*
2
+ Copyright 2024 New Vector Ltd.
3
+
4
+ SPDX-License-Identifier: AGPL-3.0-only
5
+ Please see LICENSE in the repository root for full details.
6
+ */
7
+
8
+ export * from './pkg/wysiwyg.d';
9
+
10
+ /**
11
+ * Load the WebAssembly module in the background, if it has not already been loaded.
12
+ *
13
+ * Returns a promise which will resolve once the other methods are ready.
14
+ *
15
+ * @returns {Promise<void>}
16
+ */
17
+ export function initAsync(): Promise<void>;
@@ -0,0 +1,85 @@
1
+ /*
2
+ Copyright 2024 New Vector Ltd.
3
+
4
+ SPDX-License-Identifier: AGPL-3.0-only
5
+ Please see LICENSE in the repository root for full details.
6
+ */
7
+
8
+ // @ts-check
9
+
10
+ /**
11
+ * This is the entrypoint on non-node ESM environments.
12
+ * `initAsync` will load the WASM module using a `fetch` call.
13
+ */
14
+
15
+ import * as bindings from './pkg/wysiwyg_bg.js';
16
+
17
+ const moduleUrl = new URL(
18
+ './pkg/wysiwyg_bg.wasm?url',
19
+ import.meta.url,
20
+ );
21
+
22
+ // We want to throw an error if the user tries to use the bindings before
23
+ // calling `initAsync`.
24
+ bindings.__wbg_set_wasm(
25
+ new Proxy(
26
+ {},
27
+ {
28
+ get() {
29
+ throw new Error(
30
+ '@element-hq/matrix-wysiwyg was used before it was initialized. Call `initAsync` first.',
31
+ );
32
+ },
33
+ },
34
+ ),
35
+ );
36
+
37
+ /**
38
+ * Stores a promise of the `loadModule` call
39
+ * @type {Promise<void> | null}
40
+ */
41
+ let modPromise = null;
42
+
43
+ /**
44
+ * Loads the WASM module asynchronously
45
+ *
46
+ * @returns {Promise<void>}
47
+ */
48
+ async function loadModule() {
49
+ let mod;
50
+ if (typeof WebAssembly.compileStreaming === 'function') {
51
+ mod = await WebAssembly.compileStreaming(fetch(moduleUrl));
52
+ } else {
53
+ // Fallback to fetch and compile
54
+ const response = await fetch(moduleUrl);
55
+ if (!response.ok) {
56
+ throw new Error(`Failed to fetch wasm module: ${moduleUrl}`);
57
+ }
58
+ const bytes = await response.arrayBuffer();
59
+ mod = await WebAssembly.compile(bytes);
60
+ }
61
+
62
+ /** @type {{exports: typeof import("./pkg/wysiwyg_bg.wasm.d.ts")}} */
63
+ // @ts-expect-error: Typescript doesn't know what the instance exports exactly
64
+ const instance = await WebAssembly.instantiate(mod, {
65
+ './wysiwyg_bg.js': bindings,
66
+ });
67
+
68
+ bindings.__wbg_set_wasm(instance.exports);
69
+ instance.exports.__wbindgen_start();
70
+ }
71
+
72
+ /**
73
+ * Load the WebAssembly module in the background, if it has not already been loaded.
74
+ *
75
+ * Returns a promise which will resolve once the other methods are ready.
76
+ *
77
+ * @returns {Promise<void>}
78
+ */
79
+ export async function initAsync() {
80
+ if (!modPromise) modPromise = loadModule();
81
+ await modPromise;
82
+ }
83
+
84
+ // Re-export everything from the generated javascript wrappers
85
+ export * from './pkg/wysiwyg_bg.js';
@@ -0,0 +1,120 @@
1
+ /*
2
+ Copyright 2024 New Vector Ltd.
3
+
4
+ SPDX-License-Identifier: AGPL-3.0-only
5
+ Please see LICENSE in the repository root for full details.
6
+ */
7
+
8
+ // @ts-check
9
+
10
+ /**
11
+ * This is the entrypoint on node-compatible CommonJS environments.
12
+ * `asyncLoad` will use `fs.readFile` to load the WASM module.
13
+ */
14
+
15
+ const { readFileSync } = require("node:fs");
16
+ const { readFile } = require("node:fs/promises");
17
+ const path = require("node:path");
18
+ const bindings = require("./pkg/wysiwyg_bg.cjs");
19
+
20
+ const filename = path.join(__dirname, "pkg/wysiwyg_bg.wasm");
21
+
22
+ // In node environments, we want to automatically load the WASM module
23
+ // synchronously if the consumer did not call `initAsync`. To do so, we install
24
+ // a `Proxy` that will intercept calls to the WASM module.
25
+ bindings.__wbg_set_wasm(
26
+ new Proxy(
27
+ {},
28
+ {
29
+ get(_target, prop) {
30
+ const instance = loadModuleSync();
31
+ return instance[prop];
32
+ },
33
+ },
34
+ ),
35
+ );
36
+
37
+ /**
38
+ * Stores a promise of the `loadModule` call
39
+ * @type {Promise<void> | null}
40
+ */
41
+ let modPromise = null;
42
+
43
+ /**
44
+ * Tracks whether the module has been instantiated or not
45
+ * @type {boolean}
46
+ */
47
+ let initialised = false;
48
+
49
+ /**
50
+ * Loads and instantiates the WASM module synchronously
51
+ *
52
+ * It will throw if there is an attempt to load the module asynchronously running
53
+ *
54
+ * @returns {typeof import("./pkg/wysiwyg_bg.wasm.d")}
55
+ */
56
+ function loadModuleSync() {
57
+ if (modPromise) throw new Error("The WASM module is being loaded asynchronously but hasn't finished");
58
+ const bytes = readFileSync(filename);
59
+ const mod = new WebAssembly.Module(bytes);
60
+
61
+ const instance = new WebAssembly.Instance(mod, {
62
+ // @ts-expect-error: The bindings don't exactly match the 'ExportValue' type
63
+ "./wysiwyg_bg.js": bindings,
64
+ });
65
+
66
+ initInstance(instance);
67
+
68
+ // @ts-expect-error: Typescript doesn't know what the instance exports exactly
69
+ return instance.exports;
70
+ }
71
+
72
+ /**
73
+ * Loads the WASM module asynchronously
74
+ *
75
+ * @returns {Promise<void>}
76
+ */
77
+ async function loadModuleAsync() {
78
+ const bytes = await readFile(filename);
79
+ const { instance } = await WebAssembly.instantiate(bytes, {
80
+ // @ts-expect-error: The bindings don't exactly match the 'ExportValue' type
81
+ "./wysiwyg_bg.js": bindings,
82
+ });
83
+
84
+ initInstance(instance);
85
+
86
+ // @ts-expect-error: Typescript doesn't know what the instance exports exactly
87
+ return instance.exports;
88
+ }
89
+
90
+ /**
91
+ * Initializes the WASM module and returns the exports from the WASM module.
92
+ *
93
+ * @param {WebAssembly.Instance} instance
94
+ */
95
+ function initInstance(instance) {
96
+ if (initialised) throw new Error("initInstance called twice");
97
+ bindings.__wbg_set_wasm(instance.exports);
98
+ // @ts-expect-error: Typescript doesn't know what the instance exports exactly
99
+ instance.exports.__wbindgen_start();
100
+ initialised = true;
101
+ }
102
+
103
+ /**
104
+ * Load the WebAssembly module in the background, if it has not already been loaded.
105
+ *
106
+ * Returns a promise which will resolve once the other methods are ready.
107
+ *
108
+ * @returns {Promise<void>}
109
+ */
110
+ async function initAsync() {
111
+ if (initialised) return;
112
+ if (!modPromise) modPromise = loadModuleAsync();
113
+ await modPromise;
114
+ }
115
+
116
+ module.exports = {
117
+ // Re-export everything from the generated javascript wrappers
118
+ ...bindings,
119
+ initAsync,
120
+ };
@@ -0,0 +1,117 @@
1
+ /*
2
+ Copyright 2024 New Vector Ltd.
3
+
4
+ SPDX-License-Identifier: AGPL-3.0-only
5
+ Please see LICENSE in the repository root for full details.
6
+ */
7
+
8
+ // @ts-check
9
+
10
+ /**
11
+ * This is the entrypoint on node-compatible ESM environments.
12
+ * `asyncLoad` will use `fs.readFile` to load the WASM module.
13
+ */
14
+
15
+ import { fileURLToPath } from "node:url";
16
+ import { join, dirname } from "node:path";
17
+ import { readFileSync } from "node:fs";
18
+ import { readFile } from "node:fs/promises";
19
+ import * as bindings from "./pkg/wysiwyg_bg.js";
20
+
21
+ const filename = join(dirname(fileURLToPath(import.meta.url)), "pkg", "wysiwyg_bg.wasm");
22
+
23
+ // In node environments, we want to automatically load the WASM module
24
+ // synchronously if the consumer did not call `initAsync`. To do so, we install
25
+ // a `Proxy` that will intercept calls to the WASM module.
26
+ bindings.__wbg_set_wasm(
27
+ new Proxy(
28
+ {},
29
+ {
30
+ get(_target, prop) {
31
+ const instance = loadModuleSync();
32
+ return instance[prop];
33
+ },
34
+ },
35
+ ),
36
+ );
37
+
38
+ /**
39
+ * Stores a promise of the `loadModule` call
40
+ * @type {Promise<void> | null}
41
+ */
42
+ let modPromise = null;
43
+
44
+ /**
45
+ * Tracks whether the module has been instantiated or not
46
+ * @type {boolean}
47
+ */
48
+ let initialised = false;
49
+
50
+ /**
51
+ * Loads and instantiates the WASM module synchronously
52
+ *
53
+ * It will throw if there is an attempt to load the module asynchronously running
54
+ *
55
+ * @returns {typeof import("./pkg/wysiwyg_bg.wasm.d")}
56
+ */
57
+ function loadModuleSync() {
58
+ if (modPromise) throw new Error("The WASM module is being loaded asynchronously but hasn't finished");
59
+ const bytes = readFileSync(filename);
60
+ const mod = new WebAssembly.Module(bytes);
61
+
62
+ const instance = new WebAssembly.Instance(mod, {
63
+ // @ts-expect-error: The bindings don't exactly match the 'ExportValue' type
64
+ "./wysiwyg_bg.js": bindings,
65
+ });
66
+
67
+ initInstance(instance);
68
+
69
+ // @ts-expect-error: Typescript doesn't know what the instance exports exactly
70
+ return instance.exports;
71
+ }
72
+
73
+ /**
74
+ * Loads the WASM module asynchronously
75
+ *
76
+ * @returns {Promise<void>}
77
+ */
78
+ async function loadModuleAsync() {
79
+ const bytes = await readFile(filename);
80
+ const { instance } = await WebAssembly.instantiate(bytes, {
81
+ // @ts-expect-error: The bindings don't exactly match the 'ExportValue' type
82
+ "./wysiwyg_bg.js": bindings,
83
+ });
84
+
85
+ initInstance(instance);
86
+
87
+ // @ts-expect-error: Typescript doesn't know what the instance exports exactly
88
+ return instance.exports;
89
+ }
90
+
91
+ /**
92
+ * Initializes the WASM module and returns the exports from the WASM module.
93
+ *
94
+ * @param {WebAssembly.Instance} instance
95
+ */
96
+ function initInstance(instance) {
97
+ if (initialised) throw new Error("initInstance called twice");
98
+ bindings.__wbg_set_wasm(instance.exports);
99
+ // @ts-expect-error: Typescript doesn't know what the instance exports exactly
100
+ instance.exports.__wbindgen_start();
101
+ initialised = true;
102
+ }
103
+
104
+ /**
105
+ * Load the WebAssembly module in the background, if it has not already been loaded.
106
+ *
107
+ * Returns a promise which will resolve once the other methods are ready.
108
+ *
109
+ * @returns {Promise<void>}
110
+ */
111
+ export async function initAsync() {
112
+ if (initialised) return;
113
+ if (!modPromise) modPromise = loadModuleAsync();
114
+ await modPromise;
115
+ }
116
+
117
+ export * from "./pkg/wysiwyg_bg.js";
@@ -0,0 +1,73 @@
1
+ {
2
+ "name": "@vector-im/matrix-wysiwyg-wasm",
3
+ "version": "2.38.0",
4
+ "homepage": "https://gitlab.com/andybalaam/wysiwyg-rust",
5
+ "description": "WASM bindings for wysiwyg-rust",
6
+ "license": "AGPL-3.0",
7
+ "type": "module",
8
+ "collaborators": [
9
+ "Andy Balaam <andy.balaam@matrix.org>"
10
+ ],
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "https://gitlab.com/andybalaam/wysiwyg-rust"
14
+ },
15
+ "keywords": [
16
+ "matrix",
17
+ "chat",
18
+ "messaging",
19
+ "wysiwyg"
20
+ ],
21
+ "exports": {
22
+ ".": {
23
+ "matrix-org:wasm-esm": {
24
+ "types": "./index.d.ts",
25
+ "default": "./index-wasm-esm.js"
26
+ },
27
+ "require": {
28
+ "types": "./index.d.ts",
29
+ "node": "./node.cjs",
30
+ "default": "./index.cjs"
31
+ },
32
+ "import": {
33
+ "types": "./index.d.ts",
34
+ "node": "./node.js",
35
+ "default": "./index.js"
36
+ }
37
+ }
38
+ },
39
+ "files": [
40
+ "pkg/wysiwyg_bg.js",
41
+ "pkg/wysiwyg_bg.cjs",
42
+ "pkg/wysiwyg_bg.wasm",
43
+ "pkg/wysiwyg_bg.wasm.d.ts",
44
+ "pkg/wysiwyg.d.ts",
45
+ "index.d.ts",
46
+ "index.js",
47
+ "index.cjs",
48
+ "index-wasm-esm.js",
49
+ "node.js",
50
+ "node.cjs"
51
+ ],
52
+ "devDependencies": {
53
+ "@babel/cli": "^7.23.5",
54
+ "@babel/plugin-transform-modules-commonjs": "^7.25.9",
55
+ "@types/node": "^22.10.2",
56
+ "jest": "^28.1.0",
57
+ "typedoc": "^0.26.0",
58
+ "typescript": "^5.7.2",
59
+ "wasm-pack": "^0.13.1"
60
+ },
61
+ "engines": {
62
+ "node": ">= 10"
63
+ },
64
+ "scripts": {
65
+ "dev-build": "WASM_BINDGEN_WEAKREF=1 wasm-pack build --profiling --target bundler --out-name wysiwyg --out-dir ./pkg",
66
+ "build": "yarn build:esm && yarn build:cjs && yarn lint",
67
+ "build:esm": "RUSTFLAGS='-C opt-level=s' WASM_BINDGEN_WEAKREF=1 wasm-pack build --release --target bundler --out-name wysiwyg --out-dir ./pkg",
68
+ "build:cjs": "babel pkg/wysiwyg_bg.js --out-dir pkg --out-file-extension .cjs --plugins @babel/plugin-transform-modules-commonjs",
69
+ "lint": "tsc --noEmit",
70
+ "test": "jest --verbose",
71
+ "doc": "typedoc --tsconfig ."
72
+ }
73
+ }