pymetadata-core 0.0.0-stage → 0.2.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 +7 -0
- package/README.md +70 -2
- package/loader.mjs +428 -0
- package/package.json +25 -4
- package/runtime/pymetadata-core.wasm +0 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
Copyright (c) 2026 Matthias König
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
|
+
|
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,71 @@
|
|
|
1
|
-
#
|
|
1
|
+
# pymetadata-core
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Read and write COMBINE archives (OMEX) in the browser, with a WebAssembly module and no server. No dependency, and the module imports nothing, so there is no shim to set up. The archive is read lazily: opening one parses its directory, and a member is inflated when it is asked for.
|
|
4
|
+
|
|
5
|
+
```javascript
|
|
6
|
+
import { createRuntime } from "pymetadata-core";
|
|
7
|
+
|
|
8
|
+
// serve node_modules/pymetadata-core/runtime/pymetadata-core.wasm, the file the
|
|
9
|
+
// package exports as "./pymetadata-core.wasm", with the type application/wasm
|
|
10
|
+
const module = await WebAssembly.compileStreaming(fetch("/pymetadata-core.wasm"));
|
|
11
|
+
const omex = await createRuntime({ module });
|
|
12
|
+
|
|
13
|
+
omex.open(new Uint8Array(await file.arrayBuffer()));
|
|
14
|
+
const entries = omex.manifest(); // [{ location, format, master }, ...]
|
|
15
|
+
const model = omex.entry("./model.xml"); // Uint8Array
|
|
16
|
+
omex.setEntry("data.csv", null, false, new TextEncoder().encode("a,b\n1,2\n"));
|
|
17
|
+
omex.removeEntry("./README.md");
|
|
18
|
+
const bytes = omex.write(); // Uint8Array, members which did not change are copied as they are
|
|
19
|
+
|
|
20
|
+
omex.create(); // an empty archive, with `.` and `./manifest.xml` and no member
|
|
21
|
+
omex.setEntry("models/model.xml", null, true, new TextEncoder().encode("<sbml/>\n"));
|
|
22
|
+
const made = omex.write(); // an archive built in the page, from nothing
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
`createRuntime({ module })` takes a compiled `WebAssembly.Module` or the bytes of the file and is the only asynchronous call. A runtime holds one archive, which `open` reads from bytes and `create` makes empty, and either of them drops the archive before it; make one runtime per Web Worker to work on several at once, and never call it from inside one of its own calls. Every array it returns is a copy which the page owns.
|
|
26
|
+
|
|
27
|
+
## Errors
|
|
28
|
+
|
|
29
|
+
A failed call throws an `OmexError` with a `kind` and a `message`:
|
|
30
|
+
|
|
31
|
+
| kind | meaning |
|
|
32
|
+
| --- | --- |
|
|
33
|
+
| `malformed` | the archive is not a readable archive |
|
|
34
|
+
| `refused` | a limit or a rule of the format, for instance a location outside of the archive |
|
|
35
|
+
| `unsupported` | the archive uses something 0.1.0 does not read, for instance a compression method |
|
|
36
|
+
| `not_found` | the location is not in the archive |
|
|
37
|
+
| `internal` | the request cannot be carried out, for instance a read before `open`; also any exception which is no trap of the module (a stack overflow, a memory which could not grow, a failure of the loader), and that one spends the instance |
|
|
38
|
+
| `panic` | a bug of the build, with the message of the panic |
|
|
39
|
+
| `trap` | the module trapped without a message, for instance an allocation it could not serve |
|
|
40
|
+
| `spent` | any call after a `panic` or a `trap` |
|
|
41
|
+
|
|
42
|
+
All but `panic`, `trap` and `spent` leave the runtime usable, apart from the exceptions named for `internal`. Any exception of a call spends the instance too, and says what it was: `trap` for a `WebAssembly.RuntimeError` without a message from the module, `internal` for any other exception, whether the module or the loader caused it. A `panic` or a `trap` leaves the instance in an unknown state: `runtime.spent` is then `true`, every later call throws `spent`, and the page creates a new runtime.
|
|
43
|
+
|
|
44
|
+
The module offers no way to clear its output buffer, so the loader replaces it with a harmless answer before every call and only reads a panic message from a trap of the call itself. A trap of that replacing call is reported as a plain `trap`, even when the module panicked, rather than risk reading what an earlier answer left, which may be bytes a member of the archive chose.
|
|
45
|
+
|
|
46
|
+
Two limitations of 0.1.0: `open` takes no password, so an encrypted archive cannot be opened, and a member which inflates to more than 1 GiB is `refused`.
|
|
47
|
+
|
|
48
|
+
## What differs from the python package
|
|
49
|
+
|
|
50
|
+
The same core answers both, so an archive reads and writes the same way. These are differences of the two hosts:
|
|
51
|
+
|
|
52
|
+
| | this package | `pymetadata-core` on PyPI |
|
|
53
|
+
| --- | --- | --- |
|
|
54
|
+
| a password | `open` takes none, so a ZipCrypto archive cannot be opened | `Archive.from_bytes(data, password)` |
|
|
55
|
+
| the format of a new member | `setEntry(location, null, ...)` guesses it from the suffix and the first bytes | `set_entry` takes a format URI, and `guess_format(suffix, head)` is the guess, with the suffix taken by the caller |
|
|
56
|
+
| the raw `manifest.xml` | not available; `manifest()` gives the entries it is written from | `manifest_xml()`, beside the entries |
|
|
57
|
+
| the helpers around a location | none | `normalize_location`, `guess_format`, `lookup_format` |
|
|
58
|
+
| removing an entry which is not there | `removeEntry` throws `not_found` | `remove_entry` returns `False` |
|
|
59
|
+
|
|
60
|
+
A location outside of the archive, such as `../x.xml`, is `refused` by every call which takes one, here as there.
|
|
61
|
+
|
|
62
|
+
## Developing
|
|
63
|
+
|
|
64
|
+
```bash
|
|
65
|
+
node build.mjs # build the module into runtime/ and copy the test fixture
|
|
66
|
+
node build.mjs --check # fail when the module is missing or older than anything under `crates/`, `Cargo.lock`, the root `Cargo.toml`, `rust-toolchain.toml` or `.cargo/config.toml`
|
|
67
|
+
npm test
|
|
68
|
+
npm pack --dry-run
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
`runtime/` and `test/fixtures/` are built and not committed: run `node build.mjs` before `npm test` on a fresh checkout, or the tests fail on the missing module and fixture. One test builds a second module with the `panic-probe` feature of `pymetadata-wasm` into `target/panic-probe`, which panics on purpose, to prove the panic path in the real engine; it needs `cargo` and the `wasm32-unknown-unknown` target (`rust-toolchain.toml` installs it).
|
package/loader.mjs
ADDED
|
@@ -0,0 +1,428 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The loader of the pymetadata-core WebAssembly module: a COMBINE archive opened, changed and
|
|
3
|
+
* written in a browser, a Web Worker or any other JavaScript engine, with no server.
|
|
4
|
+
*
|
|
5
|
+
* This file uses no API of any one host and has no dependency. The module it loads imports
|
|
6
|
+
* nothing, so it is instantiated with an empty import object.
|
|
7
|
+
*
|
|
8
|
+
* Two rules of the module shape everything below.
|
|
9
|
+
*
|
|
10
|
+
* - The linear memory grows, and when it does `memory.buffer` is replaced and every typed array
|
|
11
|
+
* over the old buffer is detached. So no view is ever kept: `view()` makes one over the
|
|
12
|
+
* current buffer, and every use of it comes right after the call it belongs to and before the
|
|
13
|
+
* next call into the module.
|
|
14
|
+
* - A trap leaves the instance spent. The module has no unwinding: a panic runs its hook, which
|
|
15
|
+
* writes a message into the output buffer, and then traps; an allocation it cannot serve
|
|
16
|
+
* traps without a message. After either the module's state is unknown, so the runtime
|
|
17
|
+
* refuses every later call with the kind `spent` and a page creates a new runtime.
|
|
18
|
+
*
|
|
19
|
+
* @module
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
/** The kind of the error of a call which trapped without leaving a message of the module. */
|
|
23
|
+
const TRAP = "trap";
|
|
24
|
+
|
|
25
|
+
/** The kind of the error of every call after a trap, which the runtime refuses itself. */
|
|
26
|
+
const SPENT = "spent";
|
|
27
|
+
|
|
28
|
+
/** The kind the module's panic hook writes. */
|
|
29
|
+
const PANIC = "panic";
|
|
30
|
+
|
|
31
|
+
/** The kind of a failure the module cannot carry out, which the loader also uses for its own. */
|
|
32
|
+
const INTERNAL = "internal";
|
|
33
|
+
|
|
34
|
+
/** The exports the loader calls, as the module declares them. */
|
|
35
|
+
const FUNCTIONS = [
|
|
36
|
+
"omex_alloc",
|
|
37
|
+
"omex_output_ptr",
|
|
38
|
+
"omex_output_len",
|
|
39
|
+
"omex_new",
|
|
40
|
+
"omex_open",
|
|
41
|
+
"omex_manifest",
|
|
42
|
+
"omex_entry",
|
|
43
|
+
"omex_set_entry",
|
|
44
|
+
"omex_remove_entry",
|
|
45
|
+
"omex_write",
|
|
46
|
+
"omex_about",
|
|
47
|
+
];
|
|
48
|
+
|
|
49
|
+
const encoder = new TextEncoder();
|
|
50
|
+
const decoder = new TextDecoder();
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* The error of a failed call.
|
|
54
|
+
*
|
|
55
|
+
* `kind` is what a page routes on. The module reports `malformed`, `refused`, `unsupported` and
|
|
56
|
+
* `not_found` for the archive, `internal` for a request it cannot carry out and `panic` for a
|
|
57
|
+
* bug of the build. The loader adds `trap`, a trap which left no message, and `spent`, a call
|
|
58
|
+
* on an instance which trapped before.
|
|
59
|
+
*/
|
|
60
|
+
export class OmexError extends Error {
|
|
61
|
+
/**
|
|
62
|
+
* @param {string} kind The kind of the failure.
|
|
63
|
+
* @param {string} message What went wrong, for a person.
|
|
64
|
+
*/
|
|
65
|
+
constructor(kind, message) {
|
|
66
|
+
super(message);
|
|
67
|
+
this.name = "OmexError";
|
|
68
|
+
this.kind = kind;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Bytes of a page, as a `Uint8Array` over the same memory.
|
|
74
|
+
*
|
|
75
|
+
* @param {unknown} value A `Uint8Array`, another typed array or `DataView`, or an `ArrayBuffer`.
|
|
76
|
+
* @param {string} what The name of the argument, for the message.
|
|
77
|
+
* @returns {Uint8Array}
|
|
78
|
+
*/
|
|
79
|
+
function toBytes(value, what) {
|
|
80
|
+
if (value instanceof Uint8Array) return value;
|
|
81
|
+
if (ArrayBuffer.isView(value)) {
|
|
82
|
+
return new Uint8Array(value.buffer, value.byteOffset, value.byteLength);
|
|
83
|
+
}
|
|
84
|
+
if (value instanceof ArrayBuffer) return new Uint8Array(value);
|
|
85
|
+
throw new TypeError(`${what} must be a Uint8Array or an ArrayBuffer`);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Check that an argument is a string.
|
|
90
|
+
*
|
|
91
|
+
* @param {unknown} value
|
|
92
|
+
* @param {string} what
|
|
93
|
+
* @returns {string}
|
|
94
|
+
*/
|
|
95
|
+
function toText(value, what) {
|
|
96
|
+
if (typeof value !== "string") throw new TypeError(`${what} must be a string`);
|
|
97
|
+
return value;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Instantiate the module and wrap its exports.
|
|
102
|
+
*
|
|
103
|
+
* Asynchronous because instantiating is: the runtime it resolves to is synchronous, one call
|
|
104
|
+
* after the other. An archive is one object, so a runtime holds one archive at a time, and a
|
|
105
|
+
* page which wants two instantiates two runtimes, one per Web Worker if it wants them in
|
|
106
|
+
* parallel. A call must not be made from inside another call.
|
|
107
|
+
*
|
|
108
|
+
* @param {{module: WebAssembly.Module | BufferSource}} options The compiled module, or the bytes
|
|
109
|
+
* of `runtime/pymetadata-core.wasm`.
|
|
110
|
+
* @returns {Promise<Runtime>}
|
|
111
|
+
*/
|
|
112
|
+
export async function createRuntime({ module }) {
|
|
113
|
+
const result = await WebAssembly.instantiate(module, {});
|
|
114
|
+
return new Runtime((result.instance ?? result).exports);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* One instance of the module and the archive it holds.
|
|
119
|
+
*
|
|
120
|
+
* Make one with {@link createRuntime}. The constructor checks the exports itself, so there is
|
|
121
|
+
* one way in whichever way a runtime is made.
|
|
122
|
+
*/
|
|
123
|
+
export class Runtime {
|
|
124
|
+
#exports;
|
|
125
|
+
#spent = false;
|
|
126
|
+
|
|
127
|
+
/** @param {WebAssembly.Exports} exports The exports of an instance of the module. */
|
|
128
|
+
constructor(exports) {
|
|
129
|
+
for (const name of ["memory", ...FUNCTIONS]) {
|
|
130
|
+
if (exports?.[name] === undefined) {
|
|
131
|
+
throw new TypeError(`the module does not export ${name}: it is not pymetadata-core`);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
this.#exports = exports;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Whether the instance trapped, after which every call but this getter throws `spent`.
|
|
139
|
+
*
|
|
140
|
+
* @returns {boolean}
|
|
141
|
+
*/
|
|
142
|
+
get spent() {
|
|
143
|
+
return this.#spent;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Create an empty archive, dropping the one open before.
|
|
148
|
+
*
|
|
149
|
+
* The archive holds the two entries a COMBINE archive must have, `.` and `./manifest.xml`,
|
|
150
|
+
* and no member: `setEntry` adds the members and `write` gives the bytes of the `.omex` file.
|
|
151
|
+
* This is how a page builds an archive without opening one first, and it is `Archive()` of the
|
|
152
|
+
* python package.
|
|
153
|
+
*/
|
|
154
|
+
create() {
|
|
155
|
+
this.#call(() => {
|
|
156
|
+
this.#finish(this.#exports.omex_new());
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Open the archive in `bytes`, dropping the one open before.
|
|
162
|
+
*
|
|
163
|
+
* A failed open leaves no archive open. The module keeps its own copy of the bytes, so the
|
|
164
|
+
* array stays the page's.
|
|
165
|
+
*
|
|
166
|
+
* @param {Uint8Array | ArrayBuffer} bytes The archive.
|
|
167
|
+
*/
|
|
168
|
+
open(bytes) {
|
|
169
|
+
const archive = toBytes(bytes, "the archive");
|
|
170
|
+
this.#call(() => {
|
|
171
|
+
this.#stage(archive);
|
|
172
|
+
this.#finish(this.#exports.omex_open());
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* The entries of the open archive, in the order of its manifest.
|
|
178
|
+
*
|
|
179
|
+
* @returns {{location: string, format: string, master: boolean}[]}
|
|
180
|
+
*/
|
|
181
|
+
manifest() {
|
|
182
|
+
return this.#call(() => JSON.parse(decoder.decode(this.#answer(this.#exports.omex_manifest()))))
|
|
183
|
+
.entries;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* The inflated bytes of one member, a copy which the page owns.
|
|
188
|
+
*
|
|
189
|
+
* @param {string} location The location in the manifest, `./model.xml` or `model.xml`.
|
|
190
|
+
* @returns {Uint8Array}
|
|
191
|
+
*/
|
|
192
|
+
entry(location) {
|
|
193
|
+
const request = encoder.encode(toText(location, "the location"));
|
|
194
|
+
return this.#call(() => {
|
|
195
|
+
const ptr = this.#stage(request);
|
|
196
|
+
return this.#answer(this.#exports.omex_entry(ptr, request.length));
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Add a member, or replace the member of the same location.
|
|
202
|
+
*
|
|
203
|
+
* @param {string} location Where the member goes in the archive.
|
|
204
|
+
* @param {string | null} format The format URI, or `null` to guess it from the location.
|
|
205
|
+
* @param {boolean} master Whether the entry is the master file.
|
|
206
|
+
* @param {Uint8Array | ArrayBuffer} bytes The content.
|
|
207
|
+
*/
|
|
208
|
+
setEntry(location, format, master, bytes) {
|
|
209
|
+
const content = toBytes(bytes, "the content");
|
|
210
|
+
// exactly the four keys of the request: the module refuses any other
|
|
211
|
+
const header = encoder.encode(
|
|
212
|
+
JSON.stringify({
|
|
213
|
+
location: toText(location, "the location"),
|
|
214
|
+
format: format === null || format === undefined ? null : toText(format, "the format"),
|
|
215
|
+
master: Boolean(master),
|
|
216
|
+
len: content.length,
|
|
217
|
+
}),
|
|
218
|
+
);
|
|
219
|
+
const request = new Uint8Array(header.length + content.length);
|
|
220
|
+
request.set(header);
|
|
221
|
+
request.set(content, header.length);
|
|
222
|
+
this.#call(() => {
|
|
223
|
+
const ptr = this.#stage(request);
|
|
224
|
+
this.#finish(this.#exports.omex_set_entry(ptr, request.length));
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* Remove a member and its entry.
|
|
230
|
+
*
|
|
231
|
+
* A location the archive does not hold throws `not_found`, which is also what `.` and
|
|
232
|
+
* `./manifest.xml` are, since a COMBINE archive keeps those two; the python package answers
|
|
233
|
+
* `False` for the same calls. A page which does not care whether there was an entry catches
|
|
234
|
+
* that kind.
|
|
235
|
+
*
|
|
236
|
+
* @param {string} location The location in the manifest.
|
|
237
|
+
*/
|
|
238
|
+
removeEntry(location) {
|
|
239
|
+
const request = encoder.encode(toText(location, "the location"));
|
|
240
|
+
this.#call(() => {
|
|
241
|
+
const ptr = this.#stage(request);
|
|
242
|
+
this.#finish(this.#exports.omex_remove_entry(ptr, request.length));
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* Write the archive.
|
|
248
|
+
*
|
|
249
|
+
* @param {{recompress?: boolean}} [options] `recompress` deflates every member again; by
|
|
250
|
+
* default the members which were not replaced are copied as the compressed blobs they are.
|
|
251
|
+
* @returns {Uint8Array} The archive, a copy which the page owns.
|
|
252
|
+
*/
|
|
253
|
+
write(options = {}) {
|
|
254
|
+
if (options === null || typeof options !== "object") {
|
|
255
|
+
throw new TypeError("write takes an options object such as { recompress: true }");
|
|
256
|
+
}
|
|
257
|
+
const recompress = Boolean(options.recompress);
|
|
258
|
+
return this.#call(() => this.#answer(this.#exports.omex_write(recompress ? 1 : 0)));
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* The version and the deflate backend of the module.
|
|
263
|
+
*
|
|
264
|
+
* @returns {{version: string, backend: string}}
|
|
265
|
+
*/
|
|
266
|
+
about() {
|
|
267
|
+
return this.#call(() => JSON.parse(decoder.decode(this.#answer(this.#exports.omex_about()))));
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* Run one operation on the instance, and turn a trap into a spent runtime.
|
|
272
|
+
*
|
|
273
|
+
* The operation starts with `omex_about`, which cannot fail and replaces the output of the
|
|
274
|
+
* call before it: a trap leaves the output buffer as it was, so without this a trap which
|
|
275
|
+
* says nothing would be read as whatever the call before it answered, which can be bytes a
|
|
276
|
+
* member of an archive chose. The one window is a trap of that `omex_about` itself, which the
|
|
277
|
+
* ABI gives no way to clear the buffer before: the output is then whatever the call before
|
|
278
|
+
* left, so it is not read, and the error is a plain `trap` even when the module panicked.
|
|
279
|
+
*
|
|
280
|
+
* Any exception of an operation spends the instance, which is the conservative reading: a
|
|
281
|
+
* `WebAssembly.RuntimeError` is a trap, anything else is a failure of the loader around the
|
|
282
|
+
* module and says so, since the module's state is unknown either way.
|
|
283
|
+
*
|
|
284
|
+
* @template T
|
|
285
|
+
* @param {() => T} operation
|
|
286
|
+
* @returns {T}
|
|
287
|
+
*/
|
|
288
|
+
#call(operation) {
|
|
289
|
+
if (this.#spent) {
|
|
290
|
+
throw new OmexError(
|
|
291
|
+
SPENT,
|
|
292
|
+
"the instance trapped on an earlier call and is spent; create a new runtime",
|
|
293
|
+
);
|
|
294
|
+
}
|
|
295
|
+
let reset = false;
|
|
296
|
+
try {
|
|
297
|
+
this.#exports.omex_about();
|
|
298
|
+
reset = true;
|
|
299
|
+
return operation();
|
|
300
|
+
} catch (cause) {
|
|
301
|
+
if (cause instanceof OmexError) throw cause;
|
|
302
|
+
throw this.#failed(cause, reset);
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* The error of a call which threw, and the end of this instance.
|
|
308
|
+
*
|
|
309
|
+
* @param {unknown} cause What was thrown.
|
|
310
|
+
* @param {boolean} reset Whether the output buffer was replaced by `omex_about` before it.
|
|
311
|
+
* @returns {OmexError}
|
|
312
|
+
*/
|
|
313
|
+
#failed(cause, reset) {
|
|
314
|
+
this.#spent = true;
|
|
315
|
+
const reason = cause instanceof Error ? `${cause.name}: ${cause.message}` : String(cause);
|
|
316
|
+
if (!(cause instanceof WebAssembly.RuntimeError)) {
|
|
317
|
+
return new OmexError(
|
|
318
|
+
INTERNAL,
|
|
319
|
+
`the call failed with ${reason}, which is no trap of the module with a message: for instance a ` +
|
|
320
|
+
"stack overflow or a memory which could not grow, or a failure of the loader; the " +
|
|
321
|
+
"module's state is unknown, so " +
|
|
322
|
+
"the instance is spent, create a new runtime",
|
|
323
|
+
);
|
|
324
|
+
}
|
|
325
|
+
// the output buffer is only trusted when this call replaced it first
|
|
326
|
+
if (reset) {
|
|
327
|
+
try {
|
|
328
|
+
const output = JSON.parse(decoder.decode(this.#output()));
|
|
329
|
+
// the panic hook wrote this just before the trap; `#call` made the output the answer of
|
|
330
|
+
// `omex_about` first, so nothing else can be here
|
|
331
|
+
if (output.kind === PANIC) return new OmexError(PANIC, String(output.message));
|
|
332
|
+
} catch {
|
|
333
|
+
// no readable message of the module: the trap was an abort, or the instance is unusable
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
return new OmexError(
|
|
337
|
+
TRAP,
|
|
338
|
+
`the instance trapped (${reason}) and left no message, for instance an allocation it ` +
|
|
339
|
+
"could not serve; it is spent, create a new runtime",
|
|
340
|
+
);
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/**
|
|
344
|
+
* A view of the linear memory, over the buffer it has right now.
|
|
345
|
+
*
|
|
346
|
+
* The only way the loader reads or writes the memory, and never kept: any call into the module
|
|
347
|
+
* can grow the memory and detach it.
|
|
348
|
+
*
|
|
349
|
+
* @param {number} ptr
|
|
350
|
+
* @param {number} length
|
|
351
|
+
* @returns {Uint8Array}
|
|
352
|
+
*/
|
|
353
|
+
#view(ptr, length) {
|
|
354
|
+
return new Uint8Array(this.#exports.memory.buffer, ptr, length);
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
/**
|
|
358
|
+
* Write a request into the input buffer of the module and give its address.
|
|
359
|
+
*
|
|
360
|
+
* @param {Uint8Array} bytes
|
|
361
|
+
* @returns {number} The address, 0 for no bytes.
|
|
362
|
+
*/
|
|
363
|
+
#stage(bytes) {
|
|
364
|
+
const ptr = this.#exports.omex_alloc(bytes.length);
|
|
365
|
+
if (bytes.length === 0) return 0;
|
|
366
|
+
if (ptr === 0) {
|
|
367
|
+
throw new OmexError(
|
|
368
|
+
INTERNAL,
|
|
369
|
+
`the instance cannot reserve ${bytes.length} bytes for the request`,
|
|
370
|
+
);
|
|
371
|
+
}
|
|
372
|
+
// the allocation may have grown the memory: the view comes after it
|
|
373
|
+
this.#view(ptr, bytes.length).set(bytes);
|
|
374
|
+
return ptr;
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
/**
|
|
378
|
+
* A copy of the output of the last call.
|
|
379
|
+
*
|
|
380
|
+
* @returns {Uint8Array}
|
|
381
|
+
*/
|
|
382
|
+
#output() {
|
|
383
|
+
const length = this.#exports.omex_output_len();
|
|
384
|
+
if (length === 0) return new Uint8Array(0);
|
|
385
|
+
return this.#view(this.#exports.omex_output_ptr(), length).slice();
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
/**
|
|
389
|
+
* The output of a call which answered, or its error.
|
|
390
|
+
*
|
|
391
|
+
* @param {number} code The return code of the export.
|
|
392
|
+
* @returns {Uint8Array}
|
|
393
|
+
*/
|
|
394
|
+
#answer(code) {
|
|
395
|
+
if (code === 0) return this.#output();
|
|
396
|
+
throw this.#failure();
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
/**
|
|
400
|
+
* Check the code of a call which has no answer but success.
|
|
401
|
+
*
|
|
402
|
+
* @param {number} code The return code of the export.
|
|
403
|
+
*/
|
|
404
|
+
#finish(code) {
|
|
405
|
+
if (code !== 0) throw this.#failure();
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
/**
|
|
409
|
+
* The error the module put in the output buffer.
|
|
410
|
+
*
|
|
411
|
+
* @returns {OmexError}
|
|
412
|
+
*/
|
|
413
|
+
#failure() {
|
|
414
|
+
let response;
|
|
415
|
+
try {
|
|
416
|
+
response = JSON.parse(decoder.decode(this.#output()));
|
|
417
|
+
} catch {
|
|
418
|
+
response = undefined;
|
|
419
|
+
}
|
|
420
|
+
if (typeof response?.kind !== "string") {
|
|
421
|
+
return new OmexError(INTERNAL, "the module failed and its error output is unreadable");
|
|
422
|
+
}
|
|
423
|
+
// a panic always traps after its hook, so an answer of this kind is not expected; if one
|
|
424
|
+
// arrives the instance is not trusted either
|
|
425
|
+
if (response.kind === PANIC) this.#spent = true;
|
|
426
|
+
return new OmexError(response.kind, String(response.message));
|
|
427
|
+
}
|
|
428
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,27 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pymetadata-core",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Read and write COMBINE archives (OMEX) in the browser with a WebAssembly module, no server and no dependency",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "Matthias König",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/matthiaskoenig/pymetadata-core.git",
|
|
10
|
+
"directory": "js"
|
|
11
|
+
},
|
|
12
|
+
"keywords": ["combine", "omex", "sbml", "sedml", "wasm"],
|
|
13
|
+
"type": "module",
|
|
14
|
+
"engines": { "node": ">=20" },
|
|
15
|
+
"exports": {
|
|
16
|
+
".": "./loader.mjs",
|
|
17
|
+
"./pymetadata-core.wasm": "./runtime/pymetadata-core.wasm"
|
|
18
|
+
},
|
|
19
|
+
"files": ["loader.mjs", "runtime/pymetadata-core.wasm", "LICENSE", "README.md"],
|
|
20
|
+
"scripts": {
|
|
21
|
+
"build": "node build.mjs",
|
|
22
|
+
"check": "node build.mjs --check",
|
|
23
|
+
"test": "node --test test/*.test.mjs",
|
|
24
|
+
"prepack": "node prepack.mjs"
|
|
25
|
+
},
|
|
26
|
+
"publishConfig": { "access": "public", "provenance": true }
|
|
27
|
+
}
|
|
Binary file
|