@arqlet/engine-client 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-APACHE +202 -0
- package/LICENSE-MIT +21 -0
- package/README.md +21 -2
- package/arqlet.wasm +0 -0
- package/elk.d.ts +67 -0
- package/elk.js +16 -0
- package/engine.d.ts +32 -0
- package/engine.js +53 -0
- package/package.json +70 -3
- package/presets.d.ts +26 -0
- package/presets.js +33 -0
- package/wasm-client.d.ts +23 -0
- package/wasm-client.js +26 -0
- package/worker.js +15 -0
package/LICENSE-APACHE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/LICENSE-MIT
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Benjamin Becker
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,22 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @arqlet/engine-client
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The Arqlet engine as WebAssembly (`arqlet.wasm`, included) and its JavaScript access:
|
|
4
|
+
|
|
5
|
+
- `engine.js`: `loadEngine(url?)` (browser via `fetch`, Node from disk), `sceneOf(run, snapshot)`, `createEngineWorker(url?)`;
|
|
6
|
+
- `wasm-client.js`: `createEngine(bytes, {onPublish})`, the raw binding;
|
|
7
|
+
- `elk.js`: `createElkLayout(run)`, an `elk.layout(graph)`-style call;
|
|
8
|
+
- `presets.js`: `presetState` and `applyPreset`.
|
|
9
|
+
|
|
10
|
+
Most users want the [`arqlet`](https://www.npmjs.com/package/arqlet) package, which re-exports these.
|
|
11
|
+
Bundlers that understand `new URL('./arqlet.wasm', import.meta.url)` (Vite, webpack 5,
|
|
12
|
+
esbuild with a loader) copy the engine along; otherwise pass its URL to `loadEngine`.
|
|
13
|
+
|
|
14
|
+
> **Status: prototype.** The API, the JSON protocol and the snapshot format are
|
|
15
|
+
> provisional and may change in any release.
|
|
16
|
+
|
|
17
|
+
Part of [Arqlet](https://github.com/intentweave/arqlet), an incremental graph-layout
|
|
18
|
+
engine in Rust. See the repository for documentation and the current status.
|
|
19
|
+
|
|
20
|
+
## License
|
|
21
|
+
|
|
22
|
+
Licensed under either of Apache License, Version 2.0 or MIT license at your option.
|
package/arqlet.wasm
ADDED
|
Binary file
|
package/elk.d.ts
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import type {EngineRequest, EngineResponse} from './wasm-client.js';
|
|
2
|
+
|
|
3
|
+
/** A node, port, label or graph in ELK's JSON form. Only the fields Arqlet reads or writes are named. */
|
|
4
|
+
export interface ElkShape {
|
|
5
|
+
id: string;
|
|
6
|
+
x?: number;
|
|
7
|
+
y?: number;
|
|
8
|
+
width?: number;
|
|
9
|
+
height?: number;
|
|
10
|
+
labels?: ElkShape[];
|
|
11
|
+
layoutOptions?: Record<string, string>;
|
|
12
|
+
[field: string]: unknown;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface ElkPoint {
|
|
16
|
+
x: number;
|
|
17
|
+
y: number;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface ElkEdgeSection {
|
|
21
|
+
id: string;
|
|
22
|
+
startPoint: ElkPoint;
|
|
23
|
+
endPoint: ElkPoint;
|
|
24
|
+
bendPoints?: ElkPoint[];
|
|
25
|
+
incomingShape?: string;
|
|
26
|
+
outgoingShape?: string;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface ElkEdge {
|
|
30
|
+
id: string;
|
|
31
|
+
sources: string[];
|
|
32
|
+
targets: string[];
|
|
33
|
+
sections?: ElkEdgeSection[];
|
|
34
|
+
junctionPoints?: ElkPoint[];
|
|
35
|
+
labels?: ElkShape[];
|
|
36
|
+
layoutOptions?: Record<string, string>;
|
|
37
|
+
[field: string]: unknown;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface ElkNode extends ElkShape {
|
|
41
|
+
children?: ElkNode[];
|
|
42
|
+
ports?: ElkShape[];
|
|
43
|
+
edges?: ElkEdge[];
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export interface ElkLayoutOptions {
|
|
47
|
+
/** Arqlet layout options; they win over the ones translated from `layoutOptions`. */
|
|
48
|
+
options?: Record<string, unknown>;
|
|
49
|
+
/** Runs the refinement after the layout. Default `false`. */
|
|
50
|
+
refine?: boolean;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export interface ElkLayout {
|
|
54
|
+
/** ELK options without an Arqlet counterpart, reported by the last `layout` call. */
|
|
55
|
+
diagnostics: string[];
|
|
56
|
+
/** Resolves with a copy of `graph`, laid out: positions, sizes, ports, labels and edge `sections`. */
|
|
57
|
+
layout(graph: ElkNode): Promise<ElkNode>;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* An `elk.layout(graph)`-style call on any Arqlet engine function: the WASM engine, a worker or a backend. Not a 1:1
|
|
62
|
+
* ELK. Arqlet contains no ELK code and is not affiliated with the Eclipse Foundation.
|
|
63
|
+
*/
|
|
64
|
+
export function createElkLayout(
|
|
65
|
+
run: (request: EngineRequest) => EngineResponse | Promise<EngineResponse>,
|
|
66
|
+
options?: ElkLayoutOptions,
|
|
67
|
+
): ElkLayout;
|
package/elk.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// An ELK-style layout call on any Arqlet engine function (`run(request) -> response`: the WASM engine of
|
|
2
|
+
// `createEngine`, a worker or a backend): `layout(graph)` resolves with the graph in ELK's JSON form laid out (`x`, `y`,
|
|
3
|
+
// sizes, ports, labels and edge `sections`), as `elk.layout(graph)` does. Not a 1:1 ELK: the options without a counterpart are
|
|
4
|
+
// listed in `diagnostics` after each call (see crates/elk). `options` are Arqlet options that win over the translated
|
|
5
|
+
// ones; `refine` runs the refinement after the layout.
|
|
6
|
+
export function createElkLayout(run,{options,refine=false}={}) {
|
|
7
|
+
return {
|
|
8
|
+
diagnostics:[],
|
|
9
|
+
async layout(graph) {
|
|
10
|
+
const response=await run({op:'elkLayout',graph,...(options?{options}:{}),refine});
|
|
11
|
+
if(!response.ok) throw new Error(response.error);
|
|
12
|
+
this.diagnostics=response.diagnostics;
|
|
13
|
+
return response.graph;
|
|
14
|
+
},
|
|
15
|
+
};
|
|
16
|
+
}
|
package/engine.d.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type {Scene, Snapshot} from '@arqlet/contracts';
|
|
2
|
+
import type {Run} from './wasm-client.js';
|
|
3
|
+
|
|
4
|
+
export type {EngineRequest, EngineResponse, Run} from './wasm-client.js';
|
|
5
|
+
|
|
6
|
+
/** URL of the engine shipped with this package (`arqlet.wasm` beside this module). */
|
|
7
|
+
export const defaultWasmUrl: URL;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Loads the WASM engine, by default the one shipped with this package. In a browser it is fetched; from a `file:` URL
|
|
11
|
+
* (Node) it is read from disk. `fetchBytes` replaces the loader, for example in tests.
|
|
12
|
+
*/
|
|
13
|
+
export function loadEngine(
|
|
14
|
+
url?: string | URL,
|
|
15
|
+
fetchBytes?: (url: string | URL) => Promise<BufferSource>,
|
|
16
|
+
): Promise<Run>;
|
|
17
|
+
|
|
18
|
+
/** Scene v1 for a snapshot, computed by the engine and cached per snapshot object. Throws the engine's error. */
|
|
19
|
+
export function sceneOf(run: Run, snapshot: Snapshot): Scene;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Starts a module worker that answers engine requests (browser only). Post `{requestId, request}`; the worker answers
|
|
23
|
+
* `{requestId, ...response}` and, while a request runs, `{requestId, publication, emittedAt, bytes}` for each
|
|
24
|
+
* progressive publication.
|
|
25
|
+
*/
|
|
26
|
+
export function createEngineWorker(wasmUrl?: string | URL): Worker;
|
|
27
|
+
|
|
28
|
+
/** A message to the worker of `createEngineWorker`. */
|
|
29
|
+
export interface WorkerRequest {
|
|
30
|
+
requestId: string | number;
|
|
31
|
+
request: import('./wasm-client.js').EngineRequest;
|
|
32
|
+
}
|
package/engine.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Browser-side access to the Rust engine for scene requests. The engine is the only producer of scene
|
|
3
|
+
* geometry; hosts call `sceneOf` and pass the result to a renderer.
|
|
4
|
+
*/
|
|
5
|
+
import {createEngine} from './wasm-client.js';
|
|
6
|
+
|
|
7
|
+
/** The engine shipped with this package. Bundlers that understand `new URL(..., import.meta.url)` copy it along. */
|
|
8
|
+
export const defaultWasmUrl = new URL('./arqlet.wasm', import.meta.url);
|
|
9
|
+
|
|
10
|
+
// Reads the module: over HTTP(S) with `fetch`, and from a `file:` URL (Node) from disk. The module name is held in a
|
|
11
|
+
// variable so that browser bundlers do not try to resolve the Node built-in.
|
|
12
|
+
async function readWasm(url) {
|
|
13
|
+
const href = new URL(url, globalThis.document?.baseURI ?? import.meta.url);
|
|
14
|
+
if (href.protocol === 'file:') {
|
|
15
|
+
const fs = 'node:fs/promises';
|
|
16
|
+
const {readFile} = await import(fs);
|
|
17
|
+
return readFile(href);
|
|
18
|
+
}
|
|
19
|
+
const response = await fetch(href, {cache: 'no-store'});
|
|
20
|
+
if (!response.ok) throw Error(`Engine not available: ${href}`);
|
|
21
|
+
return response.arrayBuffer();
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** Loads the WASM engine, by default the one shipped with this package. `fetchBytes` may be replaced in tests. */
|
|
25
|
+
export async function loadEngine(url = defaultWasmUrl, fetchBytes = readWasm) {
|
|
26
|
+
return createEngine(await fetchBytes(url));
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// The scene of a snapshot object, per engine. A snapshot is replaced by every change and never edited in place, so the
|
|
30
|
+
// object identifies its scene; the drawing, the statistics and the tree then share one engine call.
|
|
31
|
+
const scenes = new WeakMap();
|
|
32
|
+
|
|
33
|
+
/** Scene v1 for a snapshot, computed by the engine. Throws the engine's error explicitly. */
|
|
34
|
+
export function sceneOf(run, snapshot) {
|
|
35
|
+
let perSnapshot = scenes.get(run);
|
|
36
|
+
if (!perSnapshot) scenes.set(run, perSnapshot = new WeakMap());
|
|
37
|
+
const known = perSnapshot.get(snapshot);
|
|
38
|
+
if (known) return known;
|
|
39
|
+
const result = run({op: 'scene', snapshot});
|
|
40
|
+
if (!result.ok) throw Error(result.error);
|
|
41
|
+
perSnapshot.set(snapshot, result.scene);
|
|
42
|
+
return result.scene;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Starts a module worker that answers engine requests for the WASM at `wasmUrl`. The worker's own URL is
|
|
47
|
+
* resolved from this module, so the same package works from any host page or import map.
|
|
48
|
+
*/
|
|
49
|
+
export function createEngineWorker(wasmUrl = defaultWasmUrl) {
|
|
50
|
+
const url = new URL('./worker.js', import.meta.url);
|
|
51
|
+
url.searchParams.set('wasm', new URL(wasmUrl, document.baseURI).href);
|
|
52
|
+
return new Worker(url, {type: 'module'});
|
|
53
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,73 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@arqlet/engine-client",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "The Arqlet graph-layout engine as WebAssembly, with a loader, a worker protocol, an ELK-style layout call and preset sequencing. Provisional.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"graph",
|
|
7
|
+
"layout",
|
|
8
|
+
"graph-layout",
|
|
9
|
+
"diagram",
|
|
10
|
+
"wasm",
|
|
11
|
+
"elk",
|
|
12
|
+
"worker"
|
|
13
|
+
],
|
|
14
|
+
"license": "(MIT OR Apache-2.0)",
|
|
15
|
+
"author": "Benjamin Becker",
|
|
16
|
+
"homepage": "https://github.com/intentweave/arqlet#readme",
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "git+https://github.com/intentweave/arqlet.git",
|
|
20
|
+
"directory": "packages/engine-client"
|
|
21
|
+
},
|
|
22
|
+
"bugs": {
|
|
23
|
+
"url": "https://github.com/intentweave/arqlet/issues"
|
|
24
|
+
},
|
|
25
|
+
"type": "module",
|
|
26
|
+
"sideEffects": [
|
|
27
|
+
"./worker.js"
|
|
28
|
+
],
|
|
29
|
+
"exports": {
|
|
30
|
+
"./engine.js": {
|
|
31
|
+
"types": "./engine.d.ts",
|
|
32
|
+
"default": "./engine.js"
|
|
33
|
+
},
|
|
34
|
+
"./wasm-client.js": {
|
|
35
|
+
"types": "./wasm-client.d.ts",
|
|
36
|
+
"default": "./wasm-client.js"
|
|
37
|
+
},
|
|
38
|
+
"./presets.js": {
|
|
39
|
+
"types": "./presets.d.ts",
|
|
40
|
+
"default": "./presets.js"
|
|
41
|
+
},
|
|
42
|
+
"./elk.js": {
|
|
43
|
+
"types": "./elk.d.ts",
|
|
44
|
+
"default": "./elk.js"
|
|
45
|
+
},
|
|
46
|
+
"./worker.js": "./worker.js",
|
|
47
|
+
"./arqlet.wasm": "./arqlet.wasm"
|
|
48
|
+
},
|
|
49
|
+
"files": [
|
|
50
|
+
"engine.js",
|
|
51
|
+
"wasm-client.js",
|
|
52
|
+
"presets.js",
|
|
53
|
+
"elk.js",
|
|
54
|
+
"engine.d.ts",
|
|
55
|
+
"wasm-client.d.ts",
|
|
56
|
+
"presets.d.ts",
|
|
57
|
+
"elk.d.ts",
|
|
58
|
+
"worker.js",
|
|
59
|
+
"arqlet.wasm",
|
|
60
|
+
"README.md",
|
|
61
|
+
"LICENSE-APACHE",
|
|
62
|
+
"LICENSE-MIT"
|
|
63
|
+
],
|
|
64
|
+
"publishConfig": {
|
|
65
|
+
"access": "public"
|
|
66
|
+
},
|
|
67
|
+
"dependencies": {
|
|
68
|
+
"@arqlet/contracts": "^0.1.0"
|
|
69
|
+
},
|
|
70
|
+
"scripts": {
|
|
71
|
+
"test": "node --test"
|
|
72
|
+
}
|
|
6
73
|
}
|
package/presets.d.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type {ConfigSet, Snapshot} from '@arqlet/contracts';
|
|
2
|
+
import type {EngineRequest, EngineResponse} from './wasm-client.js';
|
|
3
|
+
|
|
4
|
+
/** A versioned preset, as in the repository's `presets/*.json`. Provisional. */
|
|
5
|
+
export interface Preset {
|
|
6
|
+
id: string;
|
|
7
|
+
version: number;
|
|
8
|
+
title?: string;
|
|
9
|
+
description?: string;
|
|
10
|
+
graph: {defaultDirection: string; defaultStrategy?: string; defaultRouting?: string};
|
|
11
|
+
refine?: boolean;
|
|
12
|
+
budgetMs?: number;
|
|
13
|
+
configSet: ConfigSet;
|
|
14
|
+
[field: string]: unknown;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** Which preset a snapshot is in: `preset` only for the exact `<id>@<version>` in `presets`; an older one is `stale`. */
|
|
18
|
+
export function presetState(snapshot: Snapshot, presets: Map<string, Preset>): {preset: Preset | null; stale: boolean};
|
|
19
|
+
|
|
20
|
+
/** Applies a preset as two engine operations (direction, then configuration). Nothing is retried. */
|
|
21
|
+
export function applyPreset(args: {
|
|
22
|
+
preset: Preset;
|
|
23
|
+
snapshot: Snapshot;
|
|
24
|
+
run: (request: EngineRequest) => EngineResponse | Promise<EngineResponse>;
|
|
25
|
+
stillCurrent: () => boolean;
|
|
26
|
+
}): Promise<{ok: true; snapshot: Snapshot} | {ok: false; error: string}>;
|
package/presets.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// Preset logic shared by the SVG demo and the React Flow reference. Pure: no DOM, no fetch.
|
|
2
|
+
// A preset is active only if the snapshot names this exact version; an older version is reported as stale.
|
|
3
|
+
|
|
4
|
+
// Which preset a snapshot is in: `preset` only for the exact `<id>@<version>` currently shipped.
|
|
5
|
+
export function presetState(snapshot, presets) {
|
|
6
|
+
const name = snapshot?.options?.configId;
|
|
7
|
+
if (!name) return {preset: null, stale: false};
|
|
8
|
+
const [id, version] = name.split('@');
|
|
9
|
+
const known = presets.get(id) ?? null;
|
|
10
|
+
if (!known) return {preset: null, stale: false};
|
|
11
|
+
const current = String(known.version) === version;
|
|
12
|
+
return {preset: current ? known : null, stale: !current};
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// Applies a preset as two engine operations: the direction, then the config set. The second step starts
|
|
16
|
+
// only if the first succeeded and the view is still the one the sequence began with (`stillCurrent`).
|
|
17
|
+
// `run(request)` resolves to {ok, snapshot?, error?}. Nothing is retried, and no refinement runs in between.
|
|
18
|
+
export async function applyPreset({preset, snapshot, run, stillCurrent}) {
|
|
19
|
+
const failed = reason => ({ok: false, error: `Preset not applied: ${reason}`});
|
|
20
|
+
let current = snapshot;
|
|
21
|
+
// `hybrid` was the compact choice per area, which a free direction is now: the preset sets `auto`.
|
|
22
|
+
const wanted = preset.graph.defaultDirection === 'hybrid' ? 'auto' : preset.graph.defaultDirection;
|
|
23
|
+
if ((current.sourceGraph ?? current.graph).defaultDirection !== wanted) {
|
|
24
|
+
const step = await run({op: 'direction', snapshot: current, nodeId: null, direction: wanted});
|
|
25
|
+
if (!step.ok) return failed(step.error ?? 'Direction');
|
|
26
|
+
if (!stillCurrent()) return failed('The view changed in the meantime.');
|
|
27
|
+
current = step.snapshot;
|
|
28
|
+
}
|
|
29
|
+
const step = await run({op: 'configure', snapshot: current, options: {configSet: preset.configSet}});
|
|
30
|
+
if (!step.ok) return failed(step.error ?? 'Configuration');
|
|
31
|
+
if (!stillCurrent()) return failed('The view changed in the meantime.');
|
|
32
|
+
return {ok: true, snapshot: step.snapshot};
|
|
33
|
+
}
|
package/wasm-client.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/** A request to the engine's dispatcher: an operation name and its fields (see the repository's docs). Provisional. */
|
|
2
|
+
export interface EngineRequest {
|
|
3
|
+
op: string;
|
|
4
|
+
/** Worker threads for native builds; ignored in WASM, where jobs run serially. Output is identical for any value. */
|
|
5
|
+
threads?: number;
|
|
6
|
+
[field: string]: unknown;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/** The dispatcher's answer: `ok: true` with the operation's fields (`snapshot`, `scene`, `graph`, ...), or an error. */
|
|
10
|
+
export type EngineResponse =
|
|
11
|
+
| ({ok: true} & {[field: string]: any})
|
|
12
|
+
| {ok: false; error: string; [field: string]: unknown};
|
|
13
|
+
|
|
14
|
+
/** Runs one request synchronously on the engine. */
|
|
15
|
+
export type Run = (request: EngineRequest) => EngineResponse;
|
|
16
|
+
|
|
17
|
+
export interface CreateEngineOptions {
|
|
18
|
+
/** Receives each progressive publication (JSON text) while the call that produced it is still running. */
|
|
19
|
+
onPublish?: (message: string) => void;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** Instantiates the WASM engine from its bytes. */
|
|
23
|
+
export function createEngine(bytes: BufferSource, options?: CreateEngineOptions): Promise<Run>;
|
package/wasm-client.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// `onPublish(message)` receives each progressive publication as JSON while the call that produced it is still running.
|
|
2
|
+
// The module always imports `lw_publish`; without a callback the messages are dropped.
|
|
3
|
+
export async function createEngine(bytes,{onPublish}={}) {
|
|
4
|
+
const decoder=new TextDecoder();
|
|
5
|
+
let api;
|
|
6
|
+
const imports={env:{
|
|
7
|
+
lw_publish:(ptr,len)=>{
|
|
8
|
+
if(!onPublish) return;
|
|
9
|
+
// Copy out of linear memory now: the module releases the buffer when this returns.
|
|
10
|
+
onPublish(decoder.decode(new Uint8Array(api.memory.buffer,ptr,len)));
|
|
11
|
+
},
|
|
12
|
+
}};
|
|
13
|
+
const {instance}=await WebAssembly.instantiate(bytes,imports);
|
|
14
|
+
api=instance.exports;
|
|
15
|
+
return input=>{
|
|
16
|
+
const encoded=new TextEncoder().encode(JSON.stringify(input));
|
|
17
|
+
const ptr=api.lw_alloc(encoded.length);
|
|
18
|
+
let out=0,len=0;
|
|
19
|
+
try {
|
|
20
|
+
new Uint8Array(api.memory.buffer,ptr,encoded.length).set(encoded);
|
|
21
|
+
const result=api.lw_run(ptr,encoded.length);
|
|
22
|
+
out=Number(result & 0xffffffffn);len=Number(result >> 32n);
|
|
23
|
+
return JSON.parse(decoder.decode(new Uint8Array(api.memory.buffer,out,len)));
|
|
24
|
+
} finally {api.lw_free(ptr,encoded.length);if(out)api.lw_free(out,len);}
|
|
25
|
+
};
|
|
26
|
+
}
|
package/worker.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import {createEngine} from './wasm-client.js';
|
|
2
|
+
// The host passes the WASM URL in the worker's query string (see createEngineWorker).
|
|
3
|
+
const wasmUrl=new URL(self.location.href).searchParams.get('wasm');
|
|
4
|
+
// The request being run. Publications are forwarded while its call is still executing; the final reply follows it.
|
|
5
|
+
let running=null;
|
|
6
|
+
const engine=fetch(wasmUrl,{cache:'no-store'}).then(r=>{if(!r.ok)throw Error(`Engine not available: ${wasmUrl}`);return r.arrayBuffer();})
|
|
7
|
+
.then(bytes=>createEngine(bytes,{onPublish:text=>{
|
|
8
|
+
if(running===null) return;
|
|
9
|
+
// emittedAt and bytes let the page split the latency into computation, transfer and display.
|
|
10
|
+
self.postMessage({requestId:running,publication:JSON.parse(text).publication,emittedAt:Date.now(),bytes:text.length});
|
|
11
|
+
}}));
|
|
12
|
+
self.onmessage=async({data})=>{
|
|
13
|
+
try{const run=await engine;running=data.requestId;const result=run(data.request);running=null;self.postMessage({requestId:data.requestId,...result});}
|
|
14
|
+
catch(e){running=null;self.postMessage({requestId:data.requestId,ok:false,error:e.message});}
|
|
15
|
+
};
|