etassia 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/README.md +14 -2
- package/dist/etassia.wasm +0 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.js +77 -0
- package/package.json +20 -4
package/README.md
CHANGED
|
@@ -1,3 +1,15 @@
|
|
|
1
|
-
#
|
|
1
|
+
# etassia-ts
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
TypeScript binding for the Etassia Rust runtime: `crates/etassia-wasm` (etassia-core on
|
|
4
|
+
`wasm32-unknown-unknown`, no wasm-bindgen glue) plus a thin loader. All rendering,
|
|
5
|
+
including number formatting, happens in Rust; JS only moves bytes.
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
import { render, detect, UnportedError, EtassiaError } from "etassia-ts";
|
|
9
|
+
try { const svg = render("pie\n \"a\": 1\n"); }
|
|
10
|
+
catch (e) { if (e instanceof UnportedError) { /* use the Ruby fallback */ } }
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
`npm test` builds the wasm (needs the `wasm32-unknown-unknown` rust target), then
|
|
14
|
+
renders every `golden/` case (or `ETASSIA_GOLDEN_DIR`) and byte-compares it with Ruby's SVG.
|
|
15
|
+
Not published; version unchanged.
|
|
Binary file
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/** Thrown when the Rust runtime has no renderer for this diagram type or construct; use the Ruby fallback. */
|
|
2
|
+
export declare class UnportedError extends Error {
|
|
3
|
+
name: string;
|
|
4
|
+
}
|
|
5
|
+
/** Thrown when the source is invalid (parse error, undetectable type, bad encoding). */
|
|
6
|
+
export declare class EtassiaError extends Error {
|
|
7
|
+
readonly errorClass: string;
|
|
8
|
+
readonly line: number;
|
|
9
|
+
readonly col: number;
|
|
10
|
+
constructor(message: string, errorClass: string, line: number, col: number);
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Render a Mermaid diagram to SVG. The SVG is produced entirely by the Rust
|
|
14
|
+
* runtime (no JS-side reformatting). Throws UnportedError when the Ruby
|
|
15
|
+
* fallback must be used, EtassiaError for invalid input.
|
|
16
|
+
*/
|
|
17
|
+
export declare function render(source: string): string;
|
|
18
|
+
/** Detect the diagram type name of `source`. Throws EtassiaError when undetectable. */
|
|
19
|
+
export declare function detect(source: string): string;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
/** Thrown when the Rust runtime has no renderer for this diagram type or construct; use the Ruby fallback. */
|
|
3
|
+
export class UnportedError extends Error {
|
|
4
|
+
name = "UnportedError";
|
|
5
|
+
}
|
|
6
|
+
/** Thrown when the source is invalid (parse error, undetectable type, bad encoding). */
|
|
7
|
+
export class EtassiaError extends Error {
|
|
8
|
+
errorClass;
|
|
9
|
+
line;
|
|
10
|
+
col;
|
|
11
|
+
constructor(message, errorClass, line, col) {
|
|
12
|
+
super(message);
|
|
13
|
+
this.errorClass = errorClass;
|
|
14
|
+
this.line = line;
|
|
15
|
+
this.col = col;
|
|
16
|
+
this.name = "EtassiaError";
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
// The wasm keeps wasm-bindgen import stubs from parsanol's getrandom dependency
|
|
20
|
+
// (hash seeding). In this unprocessed module getRandomValues receives a plain
|
|
21
|
+
// (ptr, len) pair into linear memory, so it is filled here; every other stub is
|
|
22
|
+
// unreachable on the render path and throws if called.
|
|
23
|
+
let memory;
|
|
24
|
+
const imports = new Proxy({}, {
|
|
25
|
+
get: (_t, mod) => new Proxy({}, {
|
|
26
|
+
get: (_u, name) => {
|
|
27
|
+
if (name.startsWith("__wbg_getRandomValues_")) {
|
|
28
|
+
return (ptr, len) => {
|
|
29
|
+
globalThis.crypto.getRandomValues(new Uint8Array(memory.buffer, ptr, len));
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
return () => {
|
|
33
|
+
throw new Error(`unexpected wasm import ${mod}.${name}`);
|
|
34
|
+
};
|
|
35
|
+
},
|
|
36
|
+
}),
|
|
37
|
+
});
|
|
38
|
+
let instance;
|
|
39
|
+
function wasm() {
|
|
40
|
+
if (!instance) {
|
|
41
|
+
instance = new WebAssembly.Instance(new WebAssembly.Module(readFileSync(new URL("./etassia.wasm", import.meta.url))), imports).exports;
|
|
42
|
+
memory = instance.memory;
|
|
43
|
+
}
|
|
44
|
+
return instance;
|
|
45
|
+
}
|
|
46
|
+
function call(fn, source) {
|
|
47
|
+
const w = wasm();
|
|
48
|
+
const bytes = new TextEncoder().encode(source);
|
|
49
|
+
const ptr = w.etassia_alloc(bytes.length);
|
|
50
|
+
new Uint8Array(w.memory.buffer, ptr, bytes.length).set(bytes);
|
|
51
|
+
let status;
|
|
52
|
+
try {
|
|
53
|
+
status = w[fn](ptr, bytes.length);
|
|
54
|
+
}
|
|
55
|
+
finally {
|
|
56
|
+
w.etassia_dealloc(ptr, bytes.length);
|
|
57
|
+
}
|
|
58
|
+
const out = new TextDecoder().decode(new Uint8Array(w.memory.buffer, w.etassia_result_ptr(), w.etassia_result_len()));
|
|
59
|
+
if (status === 0)
|
|
60
|
+
return out;
|
|
61
|
+
const e = JSON.parse(out);
|
|
62
|
+
if (e.class === "Unported")
|
|
63
|
+
throw new UnportedError(e.message);
|
|
64
|
+
throw new EtassiaError(e.message, e.class, e.line, e.col);
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Render a Mermaid diagram to SVG. The SVG is produced entirely by the Rust
|
|
68
|
+
* runtime (no JS-side reformatting). Throws UnportedError when the Ruby
|
|
69
|
+
* fallback must be used, EtassiaError for invalid input.
|
|
70
|
+
*/
|
|
71
|
+
export function render(source) {
|
|
72
|
+
return call("etassia_render", source);
|
|
73
|
+
}
|
|
74
|
+
/** Detect the diagram type name of `source`. Throws EtassiaError when undetectable. */
|
|
75
|
+
export function detect(source) {
|
|
76
|
+
return call("etassia_detect", source);
|
|
77
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,22 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "etassia",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
}
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "TypeScript binding for the Etassia Rust runtime (etassia-core compiled to WebAssembly)",
|
|
5
|
+
"license": "BSD-2-Clause",
|
|
6
|
+
"repository": { "type": "git", "url": "git+https://github.com/etassia/etassia-rs.git", "directory": "bindings/ts" },
|
|
7
|
+
"type": "module",
|
|
8
|
+
"main": "dist/index.js",
|
|
9
|
+
"types": "dist/index.d.ts",
|
|
10
|
+
"exports": { ".": { "types": "./dist/index.d.ts", "default": "./dist/index.js" } },
|
|
11
|
+
"files": ["dist"],
|
|
12
|
+
"engines": { "node": ">=20" },
|
|
13
|
+
"scripts": {
|
|
14
|
+
"build:wasm": "node scripts/build-wasm.mjs",
|
|
15
|
+
"build": "npm run build:wasm && tsc -p tsconfig.json",
|
|
16
|
+
"test": "npm run build && node --test test/golden.test.mjs"
|
|
17
|
+
},
|
|
18
|
+
"devDependencies": {
|
|
19
|
+
"@types/node": "^22.0.0",
|
|
20
|
+
"typescript": "^5.6.0"
|
|
21
|
+
}
|
|
22
|
+
}
|