wasm-cel 0.2.1 → 0.3.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 +177 -7
- package/dist/browser.d.ts +42 -0
- package/dist/browser.d.ts.map +1 -0
- package/dist/browser.js +155 -0
- package/dist/browser.js.map +1 -0
- package/dist/core.d.ts +160 -0
- package/dist/core.d.ts.map +1 -0
- package/dist/core.js +568 -0
- package/dist/core.js.map +1 -0
- package/dist/functions.d.ts +12 -13
- package/dist/functions.d.ts.map +1 -1
- package/dist/functions.js +4 -15
- package/dist/functions.js.map +1 -1
- package/dist/index.d.ts +2 -160
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +17 -595
- package/dist/index.js.map +1 -1
- package/dist/node.d.ts +12 -0
- package/dist/node.d.ts.map +1 -0
- package/dist/node.js +73 -0
- package/dist/node.js.map +1 -0
- package/dist/types.d.ts +6 -4
- package/dist/types.d.ts.map +1 -1
- package/main.wasm +0 -0
- package/package.json +21 -7
package/README.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# wasm-cel
|
|
2
2
|
|
|
3
3
|
WebAssembly module for evaluating CEL (Common Expression Language) expressions
|
|
4
|
-
in Node.js.
|
|
4
|
+
in Node.js and browsers.
|
|
5
5
|
|
|
6
6
|
## Installation
|
|
7
7
|
|
|
@@ -15,8 +15,10 @@ yarn add wasm-cel
|
|
|
15
15
|
|
|
16
16
|
## Usage
|
|
17
17
|
|
|
18
|
-
|
|
19
|
-
|
|
18
|
+
### Node.js
|
|
19
|
+
|
|
20
|
+
In Node.js, the library automatically loads the WASM module. Just import and
|
|
21
|
+
use:
|
|
20
22
|
|
|
21
23
|
```typescript
|
|
22
24
|
import { Env } from "wasm-cel";
|
|
@@ -50,6 +52,118 @@ const result3 = await program2.eval({ name: "Alice", age: 30 });
|
|
|
50
52
|
console.log(result3); // "Alice is 30 years old"
|
|
51
53
|
```
|
|
52
54
|
|
|
55
|
+
### Browser
|
|
56
|
+
|
|
57
|
+
In browsers, you need to initialize the WASM module first by providing the WASM
|
|
58
|
+
bytes or URL.
|
|
59
|
+
|
|
60
|
+
#### With Vite (Recommended)
|
|
61
|
+
|
|
62
|
+
Vite can process and optimize the WASM file automatically:
|
|
63
|
+
|
|
64
|
+
```typescript
|
|
65
|
+
import { init, Env } from "wasm-cel";
|
|
66
|
+
import wasmUrl from "wasm-cel/main.wasm?url";
|
|
67
|
+
|
|
68
|
+
// Initialize with Vite-processed WASM URL
|
|
69
|
+
await init(wasmUrl);
|
|
70
|
+
|
|
71
|
+
// Now use the library normally
|
|
72
|
+
const env = await Env.new({
|
|
73
|
+
variables: [{ name: "x", type: "int" }],
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
const program = await env.compile("x + 10");
|
|
77
|
+
const result = await program.eval({ x: 5 });
|
|
78
|
+
console.log(result); // 15
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
**Loading wasm_exec.js with Vite:**
|
|
82
|
+
|
|
83
|
+
If you need to load `wasm_exec.js` dynamically (it's usually loaded via script
|
|
84
|
+
tag):
|
|
85
|
+
|
|
86
|
+
```typescript
|
|
87
|
+
import { init, Env } from "wasm-cel";
|
|
88
|
+
import wasmUrl from "wasm-cel/main.wasm?url";
|
|
89
|
+
import wasmExecUrl from "wasm-cel/wasm_exec.js?url";
|
|
90
|
+
|
|
91
|
+
// Initialize with both WASM and wasm_exec URLs
|
|
92
|
+
await init(wasmUrl, wasmExecUrl);
|
|
93
|
+
|
|
94
|
+
// Use the library
|
|
95
|
+
const env = await Env.new({ variables: [{ name: "x", type: "int" }] });
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
#### Without a Bundler (Native ES Modules)
|
|
99
|
+
|
|
100
|
+
For native ES modules without a bundler, you can import directly:
|
|
101
|
+
|
|
102
|
+
```html
|
|
103
|
+
<script type="module">
|
|
104
|
+
import { init, Env } from "./node_modules/wasm-cel/dist/browser.js";
|
|
105
|
+
|
|
106
|
+
// Initialize with WASM URL
|
|
107
|
+
await init("/path/to/main.wasm");
|
|
108
|
+
|
|
109
|
+
// Or load wasm_exec.js first via script tag, then just init with WASM
|
|
110
|
+
await init("/path/to/main.wasm");
|
|
111
|
+
|
|
112
|
+
// Use the library
|
|
113
|
+
const env = await Env.new({ variables: [{ name: "x", type: "int" }] });
|
|
114
|
+
</script>
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
**Loading wasm_exec.js:**
|
|
118
|
+
|
|
119
|
+
You can either:
|
|
120
|
+
|
|
121
|
+
1. Load it via script tag before initializing:
|
|
122
|
+
|
|
123
|
+
```html
|
|
124
|
+
<script src="/path/to/wasm_exec.js"></script>
|
|
125
|
+
<script type="module">
|
|
126
|
+
import { init, Env } from "./node_modules/wasm-cel/dist/browser.js";
|
|
127
|
+
await init("/path/to/main.wasm");
|
|
128
|
+
</script>
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
2. Or pass the URL to `init()`:
|
|
132
|
+
```typescript
|
|
133
|
+
await init("/path/to/main.wasm", "/path/to/wasm_exec.js");
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
#### Other Browser Patterns
|
|
137
|
+
|
|
138
|
+
```typescript
|
|
139
|
+
import { init, Env } from "wasm-cel";
|
|
140
|
+
|
|
141
|
+
// Using a URL string
|
|
142
|
+
await init("/path/to/main.wasm");
|
|
143
|
+
|
|
144
|
+
// Using a URL object
|
|
145
|
+
await init(new URL("/main.wasm", window.location.origin));
|
|
146
|
+
|
|
147
|
+
// Using direct bytes (Uint8Array)
|
|
148
|
+
const response = await fetch("/main.wasm");
|
|
149
|
+
const bytes = new Uint8Array(await response.arrayBuffer());
|
|
150
|
+
await init(bytes);
|
|
151
|
+
|
|
152
|
+
// Using Response object
|
|
153
|
+
const response = await fetch("/main.wasm");
|
|
154
|
+
await init(response);
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
### Available Exports
|
|
158
|
+
|
|
159
|
+
The package exports the following for direct imports:
|
|
160
|
+
|
|
161
|
+
- `wasm-cel` - Main entry point (auto-selects Node.js or browser)
|
|
162
|
+
- `wasm-cel/browser` - Browser-specific entry point
|
|
163
|
+
- `wasm-cel/main.wasm` - WASM module file
|
|
164
|
+
- `wasm-cel/wasm_exec.js` - Go WASM runtime (for browsers)
|
|
165
|
+
- `wasm-cel/wasm_exec.cjs` - Go WASM runtime (CommonJS, for Node.js)
|
|
166
|
+
|
|
53
167
|
## CEL Environment Options
|
|
54
168
|
|
|
55
169
|
The library supports configurable CEL environment options to enable additional
|
|
@@ -481,10 +595,50 @@ program.destroy();
|
|
|
481
595
|
await expect(program.eval()).rejects.toThrow();
|
|
482
596
|
```
|
|
483
597
|
|
|
484
|
-
### `init(): Promise<void>`
|
|
598
|
+
### `init(wasmBytes?, wasmExecUrl?): Promise<void>`
|
|
599
|
+
|
|
600
|
+
Initializes the WASM module.
|
|
601
|
+
|
|
602
|
+
**Node.js:**
|
|
485
603
|
|
|
486
|
-
|
|
487
|
-
but can be called manually to
|
|
604
|
+
- No parameters needed - automatically loads from file system
|
|
605
|
+
- Called automatically by API functions, but can be called manually to
|
|
606
|
+
pre-initialize
|
|
607
|
+
|
|
608
|
+
**Browser:**
|
|
609
|
+
|
|
610
|
+
- **Required**: `wasmBytes` - The WASM module. Can be:
|
|
611
|
+
- `Uint8Array` - Direct bytes
|
|
612
|
+
- `string` - URL to fetch the WASM file from (supports Vite-processed URLs)
|
|
613
|
+
- `URL` - URL object pointing to the WASM file
|
|
614
|
+
- `Response` - Fetch Response object containing WASM bytes
|
|
615
|
+
- `Promise<Uint8Array>` - Async import of WASM bytes
|
|
616
|
+
- **Optional**: `wasmExecUrl` - URL to `wasm_exec.js` if it needs to be loaded
|
|
617
|
+
dynamically
|
|
618
|
+
- Must be called before using the library
|
|
619
|
+
|
|
620
|
+
**Examples:**
|
|
621
|
+
|
|
622
|
+
```typescript
|
|
623
|
+
// Node.js - no parameters
|
|
624
|
+
await init();
|
|
625
|
+
|
|
626
|
+
// Browser - with Vite
|
|
627
|
+
import wasmUrl from "wasm-cel/main.wasm?url";
|
|
628
|
+
await init(wasmUrl);
|
|
629
|
+
|
|
630
|
+
// Browser - with URL
|
|
631
|
+
await init("/path/to/main.wasm");
|
|
632
|
+
|
|
633
|
+
// Browser - with wasm_exec.js URL
|
|
634
|
+
await init("/path/to/main.wasm", "/path/to/wasm_exec.js");
|
|
635
|
+
|
|
636
|
+
// Browser - with direct bytes
|
|
637
|
+
const bytes = new Uint8Array(
|
|
638
|
+
await fetch("/main.wasm").then((r) => r.arrayBuffer()),
|
|
639
|
+
);
|
|
640
|
+
await init(bytes);
|
|
641
|
+
```
|
|
488
642
|
|
|
489
643
|
## Memory Management
|
|
490
644
|
|
|
@@ -629,7 +783,8 @@ pnpm run example
|
|
|
629
783
|
|
|
630
784
|
## Requirements
|
|
631
785
|
|
|
632
|
-
- Node.js >= 18.0.0
|
|
786
|
+
- **Node.js**: >= 18.0.0
|
|
787
|
+
- **Browsers**: Modern browsers with WebAssembly support (all current browsers)
|
|
633
788
|
|
|
634
789
|
## Package Type
|
|
635
790
|
|
|
@@ -638,6 +793,21 @@ resolution. If you're using TypeScript, make sure your `tsconfig.json` has
|
|
|
638
793
|
`"module": "NodeNext"` or `"moduleResolution": "NodeNext"` for proper type
|
|
639
794
|
resolution.
|
|
640
795
|
|
|
796
|
+
## Environment Detection
|
|
797
|
+
|
|
798
|
+
The library automatically detects the environment:
|
|
799
|
+
|
|
800
|
+
- **With bundlers** (Vite, Webpack, Rollup, etc.): Uses `package.json`
|
|
801
|
+
conditional exports to select the correct entry point at build time
|
|
802
|
+
- **Native ES modules**: Uses runtime detection to select the appropriate module
|
|
803
|
+
- **Node.js**: Automatically uses the Node.js entry point with file system
|
|
804
|
+
access
|
|
805
|
+
- **Browsers**: Uses the browser entry point that requires explicit WASM
|
|
806
|
+
initialization
|
|
807
|
+
|
|
808
|
+
You don't need to change your import statements - the library handles the
|
|
809
|
+
environment detection automatically.
|
|
810
|
+
|
|
641
811
|
## License
|
|
642
812
|
|
|
643
813
|
MIT
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Initialize the WASM module (Browser version - requires WASM bytes)
|
|
3
|
+
* @param wasmBytes - The WASM module bytes. Can be:
|
|
4
|
+
* - Uint8Array: Direct bytes
|
|
5
|
+
* - string: URL to fetch the WASM file from (supports Vite-processed URLs)
|
|
6
|
+
* - URL: URL object pointing to the WASM file
|
|
7
|
+
* - Promise<Uint8Array>: Async import of WASM bytes (for Vite `?init` pattern)
|
|
8
|
+
* - Response: Fetch Response object containing WASM bytes
|
|
9
|
+
* @param wasmExecUrl - Optional URL to wasm_exec.js if it needs to be loaded dynamically
|
|
10
|
+
* @returns {Promise<void>}
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* // With Vite - recommended pattern using ?url query parameter
|
|
15
|
+
* // Vite will process and optimize the WASM file, returning a URL string
|
|
16
|
+
* import wasmUrl from 'wasm-cel/main.wasm?url';
|
|
17
|
+
* await init(wasmUrl);
|
|
18
|
+
*
|
|
19
|
+
* // With Vite - alternative: fetch the processed URL
|
|
20
|
+
* import wasmUrl from 'wasm-cel/main.wasm?url';
|
|
21
|
+
* const response = await fetch(wasmUrl);
|
|
22
|
+
* await init(response);
|
|
23
|
+
*
|
|
24
|
+
* // Traditional URL (works in any environment)
|
|
25
|
+
* await init('/path/to/main.wasm');
|
|
26
|
+
*
|
|
27
|
+
* // Direct bytes
|
|
28
|
+
* const bytes = await fetch('/main.wasm').then(r => r.arrayBuffer());
|
|
29
|
+
* await init(new Uint8Array(bytes));
|
|
30
|
+
*
|
|
31
|
+
* // URL object
|
|
32
|
+
* await init(new URL('/main.wasm', window.location.origin));
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
declare function init(wasmBytes: Uint8Array | string | URL | Promise<Uint8Array> | Response, wasmExecUrl?: string | URL): Promise<void>;
|
|
36
|
+
export { Program, Env } from "./core.js";
|
|
37
|
+
export type { CELType, CELTypeDef, CELListType, CELMapType, CELFunctionDefinition, CELFunctionParam, EnvOptions, VariableDeclaration, TypeCheckResult, CompilationIssue, CompilationResult, } from "./types.js";
|
|
38
|
+
export { listType, mapType, CELFunction } from "./functions.js";
|
|
39
|
+
export { Options } from "./options/index.js";
|
|
40
|
+
export type { EnvOptionConfig, OptionalTypesConfig, ValidationIssue, ValidationContext, ValidatorResult, ASTValidatorFunction, ASTValidatorsConfig, } from "./options/index.js";
|
|
41
|
+
export { init };
|
|
42
|
+
//# sourceMappingURL=browser.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"browser.d.ts","sourceRoot":"","sources":["../lib/browser.ts"],"names":[],"mappings":"AA0EA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AACH,iBAAe,IAAI,CACjB,SAAS,EAAE,UAAU,GAAG,MAAM,GAAG,GAAG,GAAG,OAAO,CAAC,UAAU,CAAC,GAAG,QAAQ,EACrE,WAAW,CAAC,EAAE,MAAM,GAAG,GAAG,GACzB,OAAO,CAAC,IAAI,CAAC,CAwEf;AAMD,OAAO,EAAE,OAAO,EAAE,GAAG,EAAE,MAAM,WAAW,CAAC;AAGzC,YAAY,EACV,OAAO,EACP,UAAU,EACV,WAAW,EACX,UAAU,EACV,qBAAqB,EACrB,gBAAgB,EAChB,UAAU,EACV,mBAAmB,EACnB,eAAe,EACf,gBAAgB,EAChB,iBAAiB,GAClB,MAAM,YAAY,CAAC;AAEpB,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAChE,OAAO,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAC7C,YAAY,EACV,eAAe,EACf,mBAAmB,EACnB,eAAe,EACf,iBAAiB,EACjB,eAAe,EACf,oBAAoB,EACpB,mBAAmB,GACpB,MAAM,oBAAoB,CAAC;AAG5B,OAAO,EAAE,IAAI,EAAE,CAAC"}
|
package/dist/browser.js
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import { setInitFunction } from "./core.js";
|
|
2
|
+
let isInitialized = false;
|
|
3
|
+
let initPromise = null;
|
|
4
|
+
/**
|
|
5
|
+
* Load wasm_exec.js from a URL (if not already loaded)
|
|
6
|
+
* @param wasmExecUrl - URL to wasm_exec.js (optional, assumes already loaded if not provided)
|
|
7
|
+
* @returns Promise that resolves when wasm_exec.js is loaded
|
|
8
|
+
*/
|
|
9
|
+
async function loadWasmExec(wasmExecUrl) {
|
|
10
|
+
// Check if Go is already available
|
|
11
|
+
const globalObj = typeof globalThis !== "undefined" ? globalThis : global;
|
|
12
|
+
if (typeof globalObj.Go !== "undefined") {
|
|
13
|
+
return; // Already loaded
|
|
14
|
+
}
|
|
15
|
+
if (!wasmExecUrl) {
|
|
16
|
+
throw new Error("Go WASM runtime (wasm_exec.js) not found. Please load it before initializing the WASM module. " +
|
|
17
|
+
"You can either:\n" +
|
|
18
|
+
"1. Load it via script tag: <script src='path/to/wasm_exec.js'></script>\n" +
|
|
19
|
+
"2. Or pass wasmExecUrl to init() to load it dynamically.");
|
|
20
|
+
}
|
|
21
|
+
// Load wasm_exec.js dynamically
|
|
22
|
+
const url = typeof wasmExecUrl === "string" ? wasmExecUrl : wasmExecUrl.href;
|
|
23
|
+
// Check if we're in an environment with document (browser main thread)
|
|
24
|
+
if (typeof document === "undefined") {
|
|
25
|
+
throw new Error("document is not available. In environments like Web Workers, please load wasm_exec.js manually before calling init().");
|
|
26
|
+
}
|
|
27
|
+
const script = document.createElement("script");
|
|
28
|
+
script.src = url;
|
|
29
|
+
script.type = "text/javascript";
|
|
30
|
+
return new Promise((resolve, reject) => {
|
|
31
|
+
script.onload = () => {
|
|
32
|
+
if (typeof globalObj.Go === "undefined") {
|
|
33
|
+
reject(new Error("wasm_exec.js loaded but Go class not found. Make sure you're using the correct wasm_exec.js file."));
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
resolve();
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
script.onerror = () => {
|
|
40
|
+
reject(new Error(`Failed to load wasm_exec.js from ${url}. Make sure the URL is correct and accessible.`));
|
|
41
|
+
};
|
|
42
|
+
document.head.appendChild(script);
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Internal init function that core will call
|
|
47
|
+
*/
|
|
48
|
+
async function internalInit() {
|
|
49
|
+
if (!isInitialized) {
|
|
50
|
+
throw new Error("WASM module not initialized. In browsers, you must call init(wasmBytes) with the WASM module bytes or URL before using the library.");
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Initialize the WASM module (Browser version - requires WASM bytes)
|
|
55
|
+
* @param wasmBytes - The WASM module bytes. Can be:
|
|
56
|
+
* - Uint8Array: Direct bytes
|
|
57
|
+
* - string: URL to fetch the WASM file from (supports Vite-processed URLs)
|
|
58
|
+
* - URL: URL object pointing to the WASM file
|
|
59
|
+
* - Promise<Uint8Array>: Async import of WASM bytes (for Vite `?init` pattern)
|
|
60
|
+
* - Response: Fetch Response object containing WASM bytes
|
|
61
|
+
* @param wasmExecUrl - Optional URL to wasm_exec.js if it needs to be loaded dynamically
|
|
62
|
+
* @returns {Promise<void>}
|
|
63
|
+
*
|
|
64
|
+
* @example
|
|
65
|
+
* ```typescript
|
|
66
|
+
* // With Vite - recommended pattern using ?url query parameter
|
|
67
|
+
* // Vite will process and optimize the WASM file, returning a URL string
|
|
68
|
+
* import wasmUrl from 'wasm-cel/main.wasm?url';
|
|
69
|
+
* await init(wasmUrl);
|
|
70
|
+
*
|
|
71
|
+
* // With Vite - alternative: fetch the processed URL
|
|
72
|
+
* import wasmUrl from 'wasm-cel/main.wasm?url';
|
|
73
|
+
* const response = await fetch(wasmUrl);
|
|
74
|
+
* await init(response);
|
|
75
|
+
*
|
|
76
|
+
* // Traditional URL (works in any environment)
|
|
77
|
+
* await init('/path/to/main.wasm');
|
|
78
|
+
*
|
|
79
|
+
* // Direct bytes
|
|
80
|
+
* const bytes = await fetch('/main.wasm').then(r => r.arrayBuffer());
|
|
81
|
+
* await init(new Uint8Array(bytes));
|
|
82
|
+
*
|
|
83
|
+
* // URL object
|
|
84
|
+
* await init(new URL('/main.wasm', window.location.origin));
|
|
85
|
+
* ```
|
|
86
|
+
*/
|
|
87
|
+
async function init(wasmBytes, wasmExecUrl) {
|
|
88
|
+
if (isInitialized) {
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
if (initPromise) {
|
|
92
|
+
return initPromise;
|
|
93
|
+
}
|
|
94
|
+
initPromise = (async () => {
|
|
95
|
+
// Load wasm_exec.js if needed
|
|
96
|
+
await loadWasmExec(wasmExecUrl);
|
|
97
|
+
// Get the Go class
|
|
98
|
+
const globalObj = typeof globalThis !== "undefined" ? globalThis : global;
|
|
99
|
+
if (typeof globalObj.Go === "undefined") {
|
|
100
|
+
throw new Error("Go WASM runtime not available. Please ensure wasm_exec.js is loaded.");
|
|
101
|
+
}
|
|
102
|
+
// Load WASM bytes
|
|
103
|
+
let wasmBuffer;
|
|
104
|
+
if (wasmBytes instanceof Uint8Array) {
|
|
105
|
+
// Direct bytes
|
|
106
|
+
wasmBuffer = wasmBytes;
|
|
107
|
+
}
|
|
108
|
+
else if (wasmBytes instanceof Response) {
|
|
109
|
+
// Response object
|
|
110
|
+
const arrayBuffer = await wasmBytes.arrayBuffer();
|
|
111
|
+
wasmBuffer = new Uint8Array(arrayBuffer);
|
|
112
|
+
}
|
|
113
|
+
else if (wasmBytes instanceof Promise) {
|
|
114
|
+
// Promise resolving to Uint8Array (for Vite ?init pattern or async imports)
|
|
115
|
+
wasmBuffer = await wasmBytes;
|
|
116
|
+
}
|
|
117
|
+
else if (wasmBytes instanceof URL) {
|
|
118
|
+
// URL object
|
|
119
|
+
const response = await fetch(wasmBytes.href);
|
|
120
|
+
if (!response.ok) {
|
|
121
|
+
throw new Error(`Failed to fetch WASM file from ${wasmBytes.href}: ${response.statusText}`);
|
|
122
|
+
}
|
|
123
|
+
const arrayBuffer = await response.arrayBuffer();
|
|
124
|
+
wasmBuffer = new Uint8Array(arrayBuffer);
|
|
125
|
+
}
|
|
126
|
+
else if (typeof wasmBytes === "string") {
|
|
127
|
+
// String URL (supports Vite-processed URLs)
|
|
128
|
+
const response = await fetch(wasmBytes);
|
|
129
|
+
if (!response.ok) {
|
|
130
|
+
throw new Error(`Failed to fetch WASM file from ${wasmBytes}: ${response.statusText}`);
|
|
131
|
+
}
|
|
132
|
+
const arrayBuffer = await response.arrayBuffer();
|
|
133
|
+
wasmBuffer = new Uint8Array(arrayBuffer);
|
|
134
|
+
}
|
|
135
|
+
else {
|
|
136
|
+
throw new Error(`Invalid wasmBytes type. Expected Uint8Array, string, URL, Response, or Promise<Uint8Array>, got ${typeof wasmBytes}`);
|
|
137
|
+
}
|
|
138
|
+
// Instantiate WASM
|
|
139
|
+
const go = new globalObj.Go();
|
|
140
|
+
// TypeScript needs help with overload resolution - cast through unknown
|
|
141
|
+
const result = (await WebAssembly.instantiate(wasmBuffer, go.importObject));
|
|
142
|
+
go.run(result.instance);
|
|
143
|
+
isInitialized = true;
|
|
144
|
+
})();
|
|
145
|
+
return initPromise;
|
|
146
|
+
}
|
|
147
|
+
// Set the init function in core (this is what core will call to check initialization)
|
|
148
|
+
setInitFunction(internalInit);
|
|
149
|
+
// Re-export everything from core
|
|
150
|
+
export { Program, Env } from "./core.js";
|
|
151
|
+
export { listType, mapType, CELFunction } from "./functions.js";
|
|
152
|
+
export { Options } from "./options/index.js";
|
|
153
|
+
// Export init function for browser (requires wasmBytes parameter)
|
|
154
|
+
export { init };
|
|
155
|
+
//# sourceMappingURL=browser.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"browser.js","sourceRoot":"","sources":["../lib/browser.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAgB,MAAM,WAAW,CAAC;AAE1D,IAAI,aAAa,GAAG,KAAK,CAAC;AAC1B,IAA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aAAa,GAAG,IAAI,CAAC;IACvB,CAAC,CAAC,EAAE,CAAC;IAEL,OAAO,WAAW,CAAC;AACrB,CAAC;AAED,sFAAsF;AACtF,eAAe,CAAC,YAAY,CAAC,CAAC;AAE9B,iCAAiC;AACjC,OAAO,EAAE,OAAO,EAAE,GAAG,EAAE,MAAM,WAAW,CAAC;AAiBzC,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAChE,OAAO,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAW7C,kEAAkE;AAClE,OAAO,EAAE,IAAI,EAAE,CAAC"}
|
package/dist/core.d.ts
ADDED
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import type { EnvOptions, TypeCheckResult } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Set the initialization function (called by node.ts or browser.ts)
|
|
4
|
+
* @internal
|
|
5
|
+
*/
|
|
6
|
+
export declare function setInitFunction(fn: () => Promise<void>): void;
|
|
7
|
+
/**
|
|
8
|
+
* A compiled CEL program that can be evaluated with variables
|
|
9
|
+
*/
|
|
10
|
+
export declare class Program {
|
|
11
|
+
private programID;
|
|
12
|
+
private destroyed;
|
|
13
|
+
constructor(programID: string);
|
|
14
|
+
/**
|
|
15
|
+
* Evaluate the compiled program with the given variables
|
|
16
|
+
* @param vars - Variables to use in the evaluation
|
|
17
|
+
* @returns Promise resolving to the evaluation result
|
|
18
|
+
* @throws Error if evaluation fails or program has been destroyed
|
|
19
|
+
*/
|
|
20
|
+
eval(vars?: Record<string, any> | null): Promise<any>;
|
|
21
|
+
/**
|
|
22
|
+
* Destroy this program and free associated WASM resources.
|
|
23
|
+
* After calling destroy(), this program instance should not be used.
|
|
24
|
+
* If FinalizationRegistry is available, resources will be automatically
|
|
25
|
+
* cleaned up when the object is garbage collected, but explicit cleanup
|
|
26
|
+
* is recommended.
|
|
27
|
+
*/
|
|
28
|
+
destroy(): void;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* A CEL environment that holds variable declarations and function definitions
|
|
32
|
+
*/
|
|
33
|
+
export declare class Env {
|
|
34
|
+
private envID;
|
|
35
|
+
private destroyed;
|
|
36
|
+
private constructor();
|
|
37
|
+
/**
|
|
38
|
+
* Get the environment ID (useful for debugging or advanced use cases)
|
|
39
|
+
*/
|
|
40
|
+
getID(): string;
|
|
41
|
+
/**
|
|
42
|
+
* Create a new CEL environment
|
|
43
|
+
* @param options - Options including variable declarations, function definitions, and environment options
|
|
44
|
+
* @returns Promise resolving to a new Env instance
|
|
45
|
+
* @throws Error if environment creation fails
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* ```typescript
|
|
49
|
+
* const env = await Env.new({
|
|
50
|
+
* variables: [
|
|
51
|
+
* { name: "x", type: "int" },
|
|
52
|
+
* { name: "y", type: "int" }
|
|
53
|
+
* ],
|
|
54
|
+
* functions: [
|
|
55
|
+
* CELFunction.new("add")
|
|
56
|
+
* .param("a", "int")
|
|
57
|
+
* .param("b", "int")
|
|
58
|
+
* .returns("int")
|
|
59
|
+
* .implement((a, b) => a + b)
|
|
60
|
+
* ],
|
|
61
|
+
* options: [
|
|
62
|
+
* Options.optionalTypes()
|
|
63
|
+
* ]
|
|
64
|
+
* });
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
67
|
+
static new(options?: EnvOptions): Promise<Env>;
|
|
68
|
+
/**
|
|
69
|
+
* Compile a CEL expression in this environment
|
|
70
|
+
* @param expr - The CEL expression to compile
|
|
71
|
+
* @returns Promise resolving to a compiled Program
|
|
72
|
+
* @throws Error if compilation fails or environment has been destroyed
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
* ```typescript
|
|
76
|
+
* const env = await Env.new({
|
|
77
|
+
* variables: [{ name: "x", type: "int" }]
|
|
78
|
+
* });
|
|
79
|
+
* const program = await env.compile("x + 10");
|
|
80
|
+
* const result = await program.eval({ x: 5 });
|
|
81
|
+
* console.log(result); // 15
|
|
82
|
+
* ```
|
|
83
|
+
*/
|
|
84
|
+
compile(expr: string): Promise<Program>;
|
|
85
|
+
/**
|
|
86
|
+
* Compile a CEL expression with detailed results including warnings and issues
|
|
87
|
+
* @param expr - The CEL expression to compile
|
|
88
|
+
* @returns Promise resolving to detailed compilation results
|
|
89
|
+
* @throws Error if environment has been destroyed
|
|
90
|
+
*
|
|
91
|
+
* @example
|
|
92
|
+
* ```typescript
|
|
93
|
+
* const result = await env.compileDetailed("x + y");
|
|
94
|
+
* if (result.success) {
|
|
95
|
+
* console.log("Compiled successfully");
|
|
96
|
+
* if (result.issues.length > 0) {
|
|
97
|
+
* console.log("Warnings:", result.issues);
|
|
98
|
+
* }
|
|
99
|
+
* const evalResult = await result.program.eval({ x: 10, y: 20 });
|
|
100
|
+
* } else {
|
|
101
|
+
* console.log("Compilation failed:", result.error);
|
|
102
|
+
* console.log("All issues:", result.issues);
|
|
103
|
+
* }
|
|
104
|
+
* ```
|
|
105
|
+
*/
|
|
106
|
+
compileDetailed(expr: string): Promise<import("./types.js").CompilationResult>;
|
|
107
|
+
/**
|
|
108
|
+
* Typecheck a CEL expression in this environment without compiling it
|
|
109
|
+
* @param expr - The CEL expression to typecheck
|
|
110
|
+
* @returns Promise resolving to the type information
|
|
111
|
+
* @throws Error if typechecking fails or environment has been destroyed
|
|
112
|
+
*
|
|
113
|
+
* @example
|
|
114
|
+
* ```typescript
|
|
115
|
+
* const env = await Env.new({
|
|
116
|
+
* variables: [{ name: "x", type: "int" }, { name: "y", type: "int" }]
|
|
117
|
+
* });
|
|
118
|
+
* const typeInfo = await env.typecheck("x + y");
|
|
119
|
+
* console.log(typeInfo.type); // "int"
|
|
120
|
+
*
|
|
121
|
+
* const listType = await env.typecheck("[1, 2, 3]");
|
|
122
|
+
* console.log(listType.type); // { kind: "list", elementType: "int" }
|
|
123
|
+
* ```
|
|
124
|
+
*/
|
|
125
|
+
typecheck(expr: string): Promise<TypeCheckResult>;
|
|
126
|
+
/**
|
|
127
|
+
* Extend this environment with additional CEL environment options
|
|
128
|
+
* @param options - Array of CEL environment option configurations or complex options with setup
|
|
129
|
+
* @returns Promise that resolves when the environment has been extended
|
|
130
|
+
* @throws Error if extension fails or environment has been destroyed
|
|
131
|
+
*
|
|
132
|
+
* @example
|
|
133
|
+
* ```typescript
|
|
134
|
+
* const env = await Env.new({
|
|
135
|
+
* variables: [{ name: "x", type: "int" }]
|
|
136
|
+
* });
|
|
137
|
+
*
|
|
138
|
+
* // Add options after creation
|
|
139
|
+
* await env.extend([Options.optionalTypes()]);
|
|
140
|
+
* ```
|
|
141
|
+
*/
|
|
142
|
+
extend(options: import("./options/index.js").EnvOptionInput[]): Promise<void>;
|
|
143
|
+
/**
|
|
144
|
+
* Internal method to extend environment with options
|
|
145
|
+
* This method delegates to options that implement OptionWithSetup for complex operations
|
|
146
|
+
* @private
|
|
147
|
+
*/
|
|
148
|
+
private _extendWithOptions;
|
|
149
|
+
/**
|
|
150
|
+
* Destroy this environment and free associated WASM resources.
|
|
151
|
+
* This will also clean up any registered JavaScript functions associated
|
|
152
|
+
* with this environment.
|
|
153
|
+
* After calling destroy(), this environment instance should not be used.
|
|
154
|
+
* If FinalizationRegistry is available, resources will be automatically
|
|
155
|
+
* cleaned up when the object is garbage collected, but explicit cleanup
|
|
156
|
+
* is recommended.
|
|
157
|
+
*/
|
|
158
|
+
destroy(): void;
|
|
159
|
+
}
|
|
160
|
+
//# sourceMappingURL=core.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"core.d.ts","sourceRoot":"","sources":["../lib/core.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAGV,UAAU,EACV,eAAe,EAChB,MAAM,YAAY,CAAC;AAKpB;;;GAGG;AACH,wBAAgB,eAAe,CAAC,EAAE,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI,CAE7D;AA+ID;;GAEG;AACH,qBAAa,OAAO;IAClB,OAAO,CAAC,SAAS,CAAS;IAC1B,OA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