cloesce 0.0.5-unstable.1 → 0.0.5-unstable.11
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/dist/ast.d.ts +96 -106
- package/dist/ast.d.ts.map +1 -1
- package/dist/ast.js +12 -12
- package/dist/cli.d.ts +1 -1
- package/dist/cli.js +330 -368
- package/dist/common.d.ts +23 -0
- package/dist/common.d.ts.map +1 -0
- package/dist/common.js +78 -0
- package/dist/extractor/err.d.ts +25 -26
- package/dist/extractor/err.d.ts.map +1 -1
- package/dist/extractor/err.js +95 -129
- package/dist/extractor/extract.d.ts +24 -61
- package/dist/extractor/extract.d.ts.map +1 -1
- package/dist/extractor/extract.js +1006 -836
- package/dist/generator.wasm +0 -0
- package/dist/orm.wasm +0 -0
- package/dist/router/crud.d.ts +2 -3
- package/dist/router/crud.d.ts.map +1 -1
- package/dist/router/crud.js +58 -48
- package/dist/router/orm.d.ts +66 -0
- package/dist/router/orm.d.ts.map +1 -0
- package/dist/router/orm.js +447 -0
- package/dist/router/router.d.ts +93 -139
- package/dist/router/router.d.ts.map +1 -1
- package/dist/router/router.js +389 -432
- package/dist/router/validator.d.ts +7 -12
- package/dist/router/validator.d.ts.map +1 -1
- package/dist/router/validator.js +190 -159
- package/dist/router/wasm.d.ts +26 -67
- package/dist/router/wasm.d.ts.map +1 -1
- package/dist/router/wasm.js +52 -103
- package/dist/ui/backend.d.ts +103 -382
- package/dist/ui/backend.d.ts.map +1 -1
- package/dist/ui/backend.js +143 -430
- package/package.json +4 -10
- package/dist/ui/client.d.ts +0 -7
- package/dist/ui/client.d.ts.map +0 -1
- package/dist/ui/client.js +0 -2
- package/dist/ui/common.d.ts +0 -126
- package/dist/ui/common.d.ts.map +0 -1
- package/dist/ui/common.js +0 -203
package/dist/router/wasm.js
CHANGED
|
@@ -1,47 +1,47 @@
|
|
|
1
|
-
import {
|
|
2
|
-
// Requires the ORM binary to have been built
|
|
1
|
+
import { Either } from "../common.js";
|
|
2
|
+
// NOTE: Requires the ORM binary to have been built
|
|
3
3
|
import mod from "../orm.wasm";
|
|
4
|
-
import { Either } from "../ui/common.js";
|
|
5
|
-
/**
|
|
6
|
-
* RAII for wasm memory
|
|
7
|
-
*/
|
|
8
4
|
export class WasmResource {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
5
|
+
wasm;
|
|
6
|
+
ptr;
|
|
7
|
+
len;
|
|
8
|
+
constructor(wasm, ptr, len) {
|
|
9
|
+
this.wasm = wasm;
|
|
10
|
+
this.ptr = ptr;
|
|
11
|
+
this.len = len;
|
|
12
|
+
}
|
|
13
|
+
free() {
|
|
14
|
+
this.wasm.dealloc(this.ptr, this.len);
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Copies a value from TS memory to WASM memory.
|
|
18
|
+
*
|
|
19
|
+
* A subsequent call to `free` is necessary.
|
|
20
|
+
*/
|
|
21
|
+
static fromString(str, wasm) {
|
|
22
|
+
// TODO: Would be interesting to optimize this to avoid the intermediate copy
|
|
23
|
+
const encoder = new TextEncoder();
|
|
24
|
+
const bytes = encoder.encode(str);
|
|
25
|
+
const ptr = wasm.alloc(bytes.length);
|
|
26
|
+
const mem = new Uint8Array(wasm.memory.buffer, ptr, bytes.length);
|
|
27
|
+
mem.set(bytes);
|
|
28
|
+
return new this(wasm, ptr, bytes.length);
|
|
29
|
+
}
|
|
31
30
|
}
|
|
32
31
|
export async function loadOrmWasm(ast, wasm) {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
JSON.stringify(ast.models),
|
|
37
|
-
wasmInstance.exports,
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
32
|
+
// Load WASM
|
|
33
|
+
const wasmInstance = (wasm ??
|
|
34
|
+
(await WebAssembly.instantiate(mod)));
|
|
35
|
+
const modelMeta = WasmResource.fromString(JSON.stringify(ast.models), wasmInstance.exports);
|
|
36
|
+
if (wasmInstance.exports.set_meta_ptr(modelMeta.ptr, modelMeta.len) != 0) {
|
|
37
|
+
modelMeta.free();
|
|
38
|
+
const resPtr = wasmInstance.exports.get_return_ptr();
|
|
39
|
+
const resLen = wasmInstance.exports.get_return_len();
|
|
40
|
+
const errorMsg = new TextDecoder().decode(new Uint8Array(wasmInstance.exports.memory.buffer, resPtr, resLen));
|
|
41
|
+
throw Error(`"The WASM Module failed to load due to an invalid CIDL: ${errorMsg}`);
|
|
42
|
+
}
|
|
43
|
+
// Intentionally leak `modelMeta`, it should exist for the programs lifetime.
|
|
44
|
+
return wasmInstance.exports;
|
|
45
45
|
}
|
|
46
46
|
/**
|
|
47
47
|
* Invokes a WASM ORM function with the provided arguments, handling memory
|
|
@@ -50,69 +50,18 @@ export async function loadOrmWasm(ast, wasm) {
|
|
|
50
50
|
* Returns an Either where Left is an error message and Right the raw string result.
|
|
51
51
|
*/
|
|
52
52
|
export function invokeOrmWasm(fn, args, wasm) {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
);
|
|
62
|
-
return failed ? Either.left(result) : Either.right(result);
|
|
63
|
-
} finally {
|
|
64
|
-
args.forEach((a) => a.free());
|
|
65
|
-
if (resPtr && resLen) wasm.dealloc(resPtr, resLen);
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
/**
|
|
69
|
-
* Calls the object relational mapping function to turn a row of SQL records into
|
|
70
|
-
* an instantiated object.
|
|
71
|
-
*/
|
|
72
|
-
export function mapSql(ctor, records, includeTree) {
|
|
73
|
-
const { ast, constructorRegistry, wasm } = RuntimeContainer.get();
|
|
74
|
-
const args = [
|
|
75
|
-
WasmResource.fromString(ctor.name, wasm),
|
|
76
|
-
WasmResource.fromString(JSON.stringify(records), wasm),
|
|
77
|
-
WasmResource.fromString(JSON.stringify(includeTree), wasm),
|
|
78
|
-
];
|
|
79
|
-
const jsonResults = invokeOrmWasm(wasm.map_sql, args, wasm);
|
|
80
|
-
if (jsonResults.isLeft()) return jsonResults;
|
|
81
|
-
const parsed = JSON.parse(jsonResults.value);
|
|
82
|
-
return Either.right(
|
|
83
|
-
parsed.map((obj) =>
|
|
84
|
-
instantiateDepthFirst(obj, ast.models[ctor.name], includeTree),
|
|
85
|
-
),
|
|
86
|
-
);
|
|
87
|
-
// TODO: Lazy instantiation via Proxy?
|
|
88
|
-
function instantiateDepthFirst(m, meta, includeTree) {
|
|
89
|
-
m = Object.assign(new constructorRegistry[meta.name](), m);
|
|
90
|
-
if (!includeTree) {
|
|
91
|
-
return m;
|
|
53
|
+
let resPtr;
|
|
54
|
+
let resLen;
|
|
55
|
+
try {
|
|
56
|
+
const failed = fn(...args.flatMap((a) => [a.ptr, a.len]));
|
|
57
|
+
resPtr = wasm.get_return_ptr();
|
|
58
|
+
resLen = wasm.get_return_len();
|
|
59
|
+
const result = new TextDecoder().decode(new Uint8Array(wasm.memory.buffer, resPtr, resLen));
|
|
60
|
+
return failed ? Either.left(result) : Either.right(result);
|
|
92
61
|
}
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
// One to Many, Many to Many
|
|
98
|
-
if (Array.isArray(m[navProp.var_name])) {
|
|
99
|
-
for (let i = 0; i < m[navProp.var_name].length; i++) {
|
|
100
|
-
m[navProp.var_name][i] = instantiateDepthFirst(
|
|
101
|
-
m[navProp.var_name][i],
|
|
102
|
-
nestedMeta,
|
|
103
|
-
nestedIncludeTree,
|
|
104
|
-
);
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
// One to one
|
|
108
|
-
else if (m[navProp.var_name]) {
|
|
109
|
-
m[navProp.var_name] = instantiateDepthFirst(
|
|
110
|
-
m[navProp.var_name],
|
|
111
|
-
nestedMeta,
|
|
112
|
-
nestedIncludeTree,
|
|
113
|
-
);
|
|
114
|
-
}
|
|
62
|
+
finally {
|
|
63
|
+
args.forEach((a) => a.free());
|
|
64
|
+
if (resPtr && resLen)
|
|
65
|
+
wasm.dealloc(resPtr, resLen);
|
|
115
66
|
}
|
|
116
|
-
return m;
|
|
117
|
-
}
|
|
118
67
|
}
|