@cedar-policy/cedar-wasm 3.2.0 → 3.2.2
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 +18 -2
- package/esm/README.md +151 -0
- package/{cedar_wasm.d.ts → esm/cedar_wasm.d.ts} +52 -52
- package/esm/cedar_wasm_bg.wasm +0 -0
- package/esm/cedar_wasm_bg.wasm.d.ts +19 -0
- package/esm/package.json +20 -0
- package/nodejs/README.md +151 -0
- package/nodejs/cedar_wasm.d.ts +270 -0
- package/nodejs/cedar_wasm.js +310 -0
- package/nodejs/cedar_wasm_bg.wasm +0 -0
- package/nodejs/cedar_wasm_bg.wasm.d.ts +19 -0
- package/nodejs/package.json +15 -0
- package/package.json +39 -11
- package/web/README.md +151 -0
- package/web/cedar_wasm.d.ts +313 -0
- package/web/cedar_wasm.js +381 -0
- package/web/cedar_wasm_bg.wasm +0 -0
- package/web/cedar_wasm_bg.wasm.d.ts +19 -0
- package/web/package.json +18 -0
- package/cedar_wasm_bg.wasm +0 -0
- /package/{cedar_wasm.js → esm/cedar_wasm.js} +0 -0
- /package/{cedar_wasm_bg.js → esm/cedar_wasm_bg.js} +0 -0
package/README.md
CHANGED
|
@@ -4,8 +4,7 @@ An implementation of various cedar functions to enable developers to write types
|
|
|
4
4
|
|
|
5
5
|
## Installing
|
|
6
6
|
|
|
7
|
-
Installing is simple, just run `npm i @cedar-policy/cedar-wasm` or install with whatever your favorite package manager is.
|
|
8
|
-
|
|
7
|
+
Installing is simple, just run `npm i @cedar-policy/cedar-wasm --save` or install with whatever your favorite package manager is.
|
|
9
8
|
|
|
10
9
|
## Loading in webpack 5:
|
|
11
10
|
|
|
@@ -133,3 +132,20 @@ import('@cedar-policy/cedar-wasm').then(mod => {
|
|
|
133
132
|
});
|
|
134
133
|
```
|
|
135
134
|
|
|
135
|
+
## Alternate loading strategies
|
|
136
|
+
|
|
137
|
+
If for some reason you cannot use es modules, we provide alternate sub-packages `web` and `nodejs` (defined as `exports` in the root package.json).
|
|
138
|
+
|
|
139
|
+
The `nodejs` subpackage uses `fs` and CommonJS modules. To use it, you can import it like so:
|
|
140
|
+
|
|
141
|
+
```
|
|
142
|
+
const cedar = require('@cedar-policy/cedar-wasm/nodejs')
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
The `web` subpackage exposes an `initSync` function that you can use to load Cedar in scenarios where you want to load the wasm binary async for whatever reason. Using the `web` subpackage may also be necessary with some `jest` setups. Here's how you use the `web` subpackage:
|
|
146
|
+
|
|
147
|
+
```
|
|
148
|
+
const wasmBuffer = ... // `fetch` it or use `fs` to read it from `node_modules` in jest setupTests
|
|
149
|
+
import * as cedarJsBindings from '@cedar-policy/cedar-wasm/web';
|
|
150
|
+
cedarJsBindings.initSync(wasmBuffer);
|
|
151
|
+
```
|
package/esm/README.md
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
# cedar-wasm
|
|
2
|
+
|
|
3
|
+
An implementation of various cedar functions to enable developers to write typescript and javascript applications using Cedar and wasm.
|
|
4
|
+
|
|
5
|
+
## Installing
|
|
6
|
+
|
|
7
|
+
Installing is simple, just run `npm i @cedar-policy/cedar-wasm --save` or install with whatever your favorite package manager is.
|
|
8
|
+
|
|
9
|
+
## Loading in webpack 5:
|
|
10
|
+
|
|
11
|
+
Minimal package.json for webpack including dev server:
|
|
12
|
+
|
|
13
|
+
```
|
|
14
|
+
{
|
|
15
|
+
"name": "webpack-ts-tester",
|
|
16
|
+
"version": "1.0.0",
|
|
17
|
+
"description": "",
|
|
18
|
+
"private": true,
|
|
19
|
+
"scripts": {
|
|
20
|
+
"test": "echo \"Error: no test specified\" && exit 1",
|
|
21
|
+
"build": "webpack",
|
|
22
|
+
"dev": "webpack serve"
|
|
23
|
+
},
|
|
24
|
+
"keywords": [],
|
|
25
|
+
"author": "",
|
|
26
|
+
"license": "ISC",
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"@cedar-policy/cedar-wasm": "3.2.0"
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"ts-loader": "^9.5.1",
|
|
32
|
+
"typescript": "^5.4.5",
|
|
33
|
+
"webpack": "^5.91.0",
|
|
34
|
+
"webpack-cli": "^5.1.4",
|
|
35
|
+
"webpack-dev-server": "^5.0.4"
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Minimal tsconfig:
|
|
41
|
+
|
|
42
|
+
```
|
|
43
|
+
{
|
|
44
|
+
"compilerOptions": {
|
|
45
|
+
"outDir": "./dist/",
|
|
46
|
+
"noImplicitAny": true,
|
|
47
|
+
"module": "es2020",
|
|
48
|
+
"target": "es5",
|
|
49
|
+
"jsx": "react",
|
|
50
|
+
"allowJs": true,
|
|
51
|
+
"moduleResolution": "node"
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
Configure webpack.config.js:
|
|
57
|
+
|
|
58
|
+
```
|
|
59
|
+
const path = require('path');
|
|
60
|
+
|
|
61
|
+
module.exports = {
|
|
62
|
+
mode: 'development', // change this to suit you
|
|
63
|
+
entry: './src/index.ts',
|
|
64
|
+
module: {
|
|
65
|
+
rules: [
|
|
66
|
+
{
|
|
67
|
+
test: /\.tsx?$/,
|
|
68
|
+
use: 'ts-loader',
|
|
69
|
+
exclude: /node_modules/,
|
|
70
|
+
},
|
|
71
|
+
],
|
|
72
|
+
},
|
|
73
|
+
resolve: {
|
|
74
|
+
extensions: ['.tsx', '.ts', '.js'],
|
|
75
|
+
},
|
|
76
|
+
output: {
|
|
77
|
+
filename: 'bundle.js',
|
|
78
|
+
path: path.resolve(__dirname, 'dist'),
|
|
79
|
+
},
|
|
80
|
+
experiments: {
|
|
81
|
+
asyncWebAssembly: true, // enables wasm support in webpack
|
|
82
|
+
},
|
|
83
|
+
devServer: {
|
|
84
|
+
static: {
|
|
85
|
+
directory: path.join(__dirname, 'dist'),
|
|
86
|
+
},
|
|
87
|
+
compress: true,
|
|
88
|
+
port: 8000,
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
Finally, load the code from your `index.ts` file. We recommend dynamic imports:
|
|
94
|
+
|
|
95
|
+
```
|
|
96
|
+
import('@cedar-policy/cedar-wasm').then(mod => {
|
|
97
|
+
// cache it globally here or invoke functions like mod.getCedarVersion();
|
|
98
|
+
});
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
## Loading in vite 5:
|
|
104
|
+
|
|
105
|
+
Starting from the vite typescript template, install these two dependencies to enable wasm:
|
|
106
|
+
|
|
107
|
+
```
|
|
108
|
+
npm i --save-dev vite-plugin-top-level-await vite-plugin-wasm
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
Then add those two plugins to your vite config in `vite.config.js`:
|
|
112
|
+
|
|
113
|
+
```
|
|
114
|
+
import wasm from 'vite-plugin-wasm';
|
|
115
|
+
import topLevelAwait from 'vite-plugin-top-level-await';
|
|
116
|
+
import { defineConfig } from 'vite';
|
|
117
|
+
|
|
118
|
+
export default defineConfig({
|
|
119
|
+
plugins: [
|
|
120
|
+
wasm(),
|
|
121
|
+
topLevelAwait()
|
|
122
|
+
]
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
Finally, load the code. We recommend dynamic imports:
|
|
128
|
+
|
|
129
|
+
```
|
|
130
|
+
import('@cedar-policy/cedar-wasm').then(mod => {
|
|
131
|
+
// cache it globally here or invoke functions like mod.getCedarVersion();
|
|
132
|
+
});
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
## Alternate loading strategies
|
|
136
|
+
|
|
137
|
+
If for some reason you cannot use es modules, we provide alternate sub-packages `web` and `nodejs` (defined as `exports` in the root package.json).
|
|
138
|
+
|
|
139
|
+
The `nodejs` subpackage uses `fs` and CommonJS modules. To use it, you can import it like so:
|
|
140
|
+
|
|
141
|
+
```
|
|
142
|
+
const cedar = require('@cedar-policy/cedar-wasm/nodejs')
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
The `web` subpackage exposes an `initSync` function that you can use to load Cedar in scenarios where you want to load the wasm binary async for whatever reason. Using the `web` subpackage may also be necessary with some `jest` setups. Here's how you use the `web` subpackage:
|
|
146
|
+
|
|
147
|
+
```
|
|
148
|
+
const wasmBuffer = ... // `fetch` it or use `fs` to read it from `node_modules` in jest setupTests
|
|
149
|
+
import * as cedarJsBindings from '@cedar-policy/cedar-wasm/web';
|
|
150
|
+
cedarJsBindings.initSync(wasmBuffer);
|
|
151
|
+
```
|
|
@@ -71,6 +71,31 @@ export type CheckParseResult = { type: "success" } | { type: "error"; errors: st
|
|
|
71
71
|
|
|
72
72
|
export type FormattingResult = { type: "success"; formatted_policy: string } | { type: "error"; errors: string[] };
|
|
73
73
|
|
|
74
|
+
export type Schema = { human: string } | { json: SchemaJson };
|
|
75
|
+
|
|
76
|
+
export type PolicySet = string | Record<string, string>;
|
|
77
|
+
|
|
78
|
+
export interface SourceLocation {
|
|
79
|
+
start: number;
|
|
80
|
+
end: number;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export interface SourceLabel extends SourceLocation {
|
|
84
|
+
label: string | null;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export type Severity = "advice" | "warning" | "error";
|
|
88
|
+
|
|
89
|
+
export interface DetailedError {
|
|
90
|
+
message: string;
|
|
91
|
+
help: string | null;
|
|
92
|
+
code: string | null;
|
|
93
|
+
url: string | null;
|
|
94
|
+
severity: Severity | null;
|
|
95
|
+
sourceLocations?: SourceLabel[];
|
|
96
|
+
related?: DetailedError[];
|
|
97
|
+
}
|
|
98
|
+
|
|
74
99
|
export type ValidationAnswer = { type: "failure"; errors: DetailedError[]; warnings: DetailedError[] } | { type: "success"; validationErrors: ValidationError[]; validationWarnings: ValidationError[]; otherWarnings: DetailedError[] };
|
|
75
100
|
|
|
76
101
|
export interface ValidationError {
|
|
@@ -116,9 +141,9 @@ export interface EntityUIDStrings {
|
|
|
116
141
|
}
|
|
117
142
|
|
|
118
143
|
export interface AuthorizationCall {
|
|
119
|
-
principal:
|
|
120
|
-
action:
|
|
121
|
-
resource:
|
|
144
|
+
principal: {type: string, id: string};
|
|
145
|
+
action: {type: string, id: string};
|
|
146
|
+
resource: {type: string, id: string};
|
|
122
147
|
context: Record<string, CedarValueJson>;
|
|
123
148
|
schema?: Schema;
|
|
124
149
|
enableRequestValidation?: boolean;
|
|
@@ -142,31 +167,6 @@ export interface Response {
|
|
|
142
167
|
diagnostics: Diagnostics;
|
|
143
168
|
}
|
|
144
169
|
|
|
145
|
-
export type Schema = { human: string } | { json: JsonValueWithNoDuplicateKeys };
|
|
146
|
-
|
|
147
|
-
export type PolicySet = string | Record<string, string>;
|
|
148
|
-
|
|
149
|
-
export interface SourceLocation {
|
|
150
|
-
start: number;
|
|
151
|
-
end: number;
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
export interface SourceLabel extends SourceLocation {
|
|
155
|
-
label: string | null;
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
export type Severity = "advice" | "warning" | "error";
|
|
159
|
-
|
|
160
|
-
export interface DetailedError {
|
|
161
|
-
message: string;
|
|
162
|
-
help: string | null;
|
|
163
|
-
code: string | null;
|
|
164
|
-
url: string | null;
|
|
165
|
-
severity: Severity | null;
|
|
166
|
-
sourceLocations?: SourceLabel[];
|
|
167
|
-
related?: DetailedError[];
|
|
168
|
-
}
|
|
169
|
-
|
|
170
170
|
export type SchemaTypeVariant = { type: "String" } | { type: "Long" } | { type: "Boolean" } | { type: "Set"; element: SchemaType } | { type: "Record"; attributes: Record<SmolStr, TypeOfAttribute>; additionalAttributes: boolean } | { type: "Entity"; name: Name } | { type: "Extension"; name: Id };
|
|
171
171
|
|
|
172
172
|
export type SchemaType = SchemaTypeVariant | { type: Name };
|
|
@@ -201,15 +201,7 @@ export interface NamespaceDefinition {
|
|
|
201
201
|
actions: Record<SmolStr, ActionType>;
|
|
202
202
|
}
|
|
203
203
|
|
|
204
|
-
export type
|
|
205
|
-
|
|
206
|
-
export type Decision = "Allow" | "Deny";
|
|
207
|
-
|
|
208
|
-
export interface EntityJson {
|
|
209
|
-
uid: EntityUidJson;
|
|
210
|
-
attrs: Record<string, CedarValueJson>;
|
|
211
|
-
parents: EntityUidJson[];
|
|
212
|
-
}
|
|
204
|
+
export type SchemaJson = Record<string, NamespaceDefinition>;
|
|
213
205
|
|
|
214
206
|
export type EntityUidJson = { __expr: string } | { __entity: TypeAndId } | TypeAndId;
|
|
215
207
|
|
|
@@ -225,6 +217,29 @@ export interface TypeAndId {
|
|
|
225
217
|
|
|
226
218
|
export type CedarValueJson = { __expr: string } | { __entity: TypeAndId } | { __extn: FnAndArg } | boolean | number | string | CedarValueJson[] | { [key: string]: CedarValueJson } | null;
|
|
227
219
|
|
|
220
|
+
export type Var = "principal" | "action" | "resource" | "context";
|
|
221
|
+
|
|
222
|
+
export type Effect = "permit" | "forbid";
|
|
223
|
+
|
|
224
|
+
export type Clause = { kind: "when"; body: Expr } | { kind: "unless"; body: Expr };
|
|
225
|
+
|
|
226
|
+
export interface Policy {
|
|
227
|
+
effect: Effect;
|
|
228
|
+
principal: PrincipalConstraint;
|
|
229
|
+
action: ActionConstraint;
|
|
230
|
+
resource: ResourceConstraint;
|
|
231
|
+
conditions: Clause[];
|
|
232
|
+
annotations?: Record<string, string>;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
export interface EntityJson {
|
|
236
|
+
uid: EntityUidJson;
|
|
237
|
+
attrs: Record<string, CedarValueJson>;
|
|
238
|
+
parents: EntityUidJson[];
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
export type Decision = "Allow" | "Deny";
|
|
242
|
+
|
|
228
243
|
export type ActionInConstraint = { entity: EntityUidJson } | { entities: EntityUidJson[] };
|
|
229
244
|
|
|
230
245
|
export interface PrincipalOrResourceIsConstraint {
|
|
@@ -248,21 +263,6 @@ export type ExprNoExt = { Value: CedarValueJson } | { Var: Var } | { Slot: strin
|
|
|
248
263
|
|
|
249
264
|
export type Expr = ExprNoExt | ExtFuncCall;
|
|
250
265
|
|
|
251
|
-
export type Var = "principal" | "action" | "resource" | "context";
|
|
252
|
-
|
|
253
|
-
export type Clause = { kind: "when"; body: Expr } | { kind: "unless"; body: Expr };
|
|
254
|
-
|
|
255
|
-
export interface Policy {
|
|
256
|
-
effect: Effect;
|
|
257
|
-
principal: PrincipalConstraint;
|
|
258
|
-
action: ActionConstraint;
|
|
259
|
-
resource: ResourceConstraint;
|
|
260
|
-
conditions: Clause[];
|
|
261
|
-
annotations?: Record<string, string>;
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
export type Effect = "permit" | "forbid";
|
|
265
|
-
|
|
266
266
|
type SmolStr = string;
|
|
267
267
|
type Name = string;
|
|
268
268
|
type Id = string;
|
|
Binary file
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/* tslint:disable */
|
|
2
|
+
/* eslint-disable */
|
|
3
|
+
export const memory: WebAssembly.Memory;
|
|
4
|
+
export function policyTextFromJson(a: number, b: number): number;
|
|
5
|
+
export function policyTextToJson(a: number, b: number): number;
|
|
6
|
+
export function checkParsePolicySet(a: number, b: number): number;
|
|
7
|
+
export function checkParseTemplate(a: number, b: number): number;
|
|
8
|
+
export function checkParseSchema(a: number, b: number): number;
|
|
9
|
+
export function checkParseEntities(a: number, b: number, c: number, d: number): number;
|
|
10
|
+
export function checkParseContext(a: number, b: number, c: number, d: number, e: number, f: number): number;
|
|
11
|
+
export function formatPolicies(a: number, b: number, c: number, d: number): number;
|
|
12
|
+
export function getCedarVersion(a: number): void;
|
|
13
|
+
export function isAuthorized(a: number): number;
|
|
14
|
+
export function validate(a: number): number;
|
|
15
|
+
export function __wbindgen_malloc(a: number, b: number): number;
|
|
16
|
+
export function __wbindgen_realloc(a: number, b: number, c: number, d: number): number;
|
|
17
|
+
export function __wbindgen_add_to_stack_pointer(a: number): number;
|
|
18
|
+
export function __wbindgen_free(a: number, b: number, c: number): void;
|
|
19
|
+
export function __wbindgen_exn_store(a: number): void;
|
package/esm/package.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@cedar-policy/cedar-wasm",
|
|
3
|
+
"description": "Wasm bindings and typescript types for Cedar lib",
|
|
4
|
+
"version": "3.2.2",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"files": [
|
|
7
|
+
"cedar_wasm_bg.wasm",
|
|
8
|
+
"cedar_wasm_bg.wasm.d.ts",
|
|
9
|
+
"cedar_wasm.js",
|
|
10
|
+
"cedar_wasm_bg.js",
|
|
11
|
+
"cedar_wasm.d.ts"
|
|
12
|
+
],
|
|
13
|
+
"module": "cedar_wasm.js",
|
|
14
|
+
"types": "cedar_wasm.d.ts",
|
|
15
|
+
"sideEffects": [
|
|
16
|
+
"./cedar_wasm.js",
|
|
17
|
+
"./snippets/*"
|
|
18
|
+
],
|
|
19
|
+
"type": "module"
|
|
20
|
+
}
|
package/nodejs/README.md
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
# cedar-wasm
|
|
2
|
+
|
|
3
|
+
An implementation of various cedar functions to enable developers to write typescript and javascript applications using Cedar and wasm.
|
|
4
|
+
|
|
5
|
+
## Installing
|
|
6
|
+
|
|
7
|
+
Installing is simple, just run `npm i @cedar-policy/cedar-wasm --save` or install with whatever your favorite package manager is.
|
|
8
|
+
|
|
9
|
+
## Loading in webpack 5:
|
|
10
|
+
|
|
11
|
+
Minimal package.json for webpack including dev server:
|
|
12
|
+
|
|
13
|
+
```
|
|
14
|
+
{
|
|
15
|
+
"name": "webpack-ts-tester",
|
|
16
|
+
"version": "1.0.0",
|
|
17
|
+
"description": "",
|
|
18
|
+
"private": true,
|
|
19
|
+
"scripts": {
|
|
20
|
+
"test": "echo \"Error: no test specified\" && exit 1",
|
|
21
|
+
"build": "webpack",
|
|
22
|
+
"dev": "webpack serve"
|
|
23
|
+
},
|
|
24
|
+
"keywords": [],
|
|
25
|
+
"author": "",
|
|
26
|
+
"license": "ISC",
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"@cedar-policy/cedar-wasm": "3.2.0"
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"ts-loader": "^9.5.1",
|
|
32
|
+
"typescript": "^5.4.5",
|
|
33
|
+
"webpack": "^5.91.0",
|
|
34
|
+
"webpack-cli": "^5.1.4",
|
|
35
|
+
"webpack-dev-server": "^5.0.4"
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Minimal tsconfig:
|
|
41
|
+
|
|
42
|
+
```
|
|
43
|
+
{
|
|
44
|
+
"compilerOptions": {
|
|
45
|
+
"outDir": "./dist/",
|
|
46
|
+
"noImplicitAny": true,
|
|
47
|
+
"module": "es2020",
|
|
48
|
+
"target": "es5",
|
|
49
|
+
"jsx": "react",
|
|
50
|
+
"allowJs": true,
|
|
51
|
+
"moduleResolution": "node"
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
Configure webpack.config.js:
|
|
57
|
+
|
|
58
|
+
```
|
|
59
|
+
const path = require('path');
|
|
60
|
+
|
|
61
|
+
module.exports = {
|
|
62
|
+
mode: 'development', // change this to suit you
|
|
63
|
+
entry: './src/index.ts',
|
|
64
|
+
module: {
|
|
65
|
+
rules: [
|
|
66
|
+
{
|
|
67
|
+
test: /\.tsx?$/,
|
|
68
|
+
use: 'ts-loader',
|
|
69
|
+
exclude: /node_modules/,
|
|
70
|
+
},
|
|
71
|
+
],
|
|
72
|
+
},
|
|
73
|
+
resolve: {
|
|
74
|
+
extensions: ['.tsx', '.ts', '.js'],
|
|
75
|
+
},
|
|
76
|
+
output: {
|
|
77
|
+
filename: 'bundle.js',
|
|
78
|
+
path: path.resolve(__dirname, 'dist'),
|
|
79
|
+
},
|
|
80
|
+
experiments: {
|
|
81
|
+
asyncWebAssembly: true, // enables wasm support in webpack
|
|
82
|
+
},
|
|
83
|
+
devServer: {
|
|
84
|
+
static: {
|
|
85
|
+
directory: path.join(__dirname, 'dist'),
|
|
86
|
+
},
|
|
87
|
+
compress: true,
|
|
88
|
+
port: 8000,
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
Finally, load the code from your `index.ts` file. We recommend dynamic imports:
|
|
94
|
+
|
|
95
|
+
```
|
|
96
|
+
import('@cedar-policy/cedar-wasm').then(mod => {
|
|
97
|
+
// cache it globally here or invoke functions like mod.getCedarVersion();
|
|
98
|
+
});
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
## Loading in vite 5:
|
|
104
|
+
|
|
105
|
+
Starting from the vite typescript template, install these two dependencies to enable wasm:
|
|
106
|
+
|
|
107
|
+
```
|
|
108
|
+
npm i --save-dev vite-plugin-top-level-await vite-plugin-wasm
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
Then add those two plugins to your vite config in `vite.config.js`:
|
|
112
|
+
|
|
113
|
+
```
|
|
114
|
+
import wasm from 'vite-plugin-wasm';
|
|
115
|
+
import topLevelAwait from 'vite-plugin-top-level-await';
|
|
116
|
+
import { defineConfig } from 'vite';
|
|
117
|
+
|
|
118
|
+
export default defineConfig({
|
|
119
|
+
plugins: [
|
|
120
|
+
wasm(),
|
|
121
|
+
topLevelAwait()
|
|
122
|
+
]
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
Finally, load the code. We recommend dynamic imports:
|
|
128
|
+
|
|
129
|
+
```
|
|
130
|
+
import('@cedar-policy/cedar-wasm').then(mod => {
|
|
131
|
+
// cache it globally here or invoke functions like mod.getCedarVersion();
|
|
132
|
+
});
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
## Alternate loading strategies
|
|
136
|
+
|
|
137
|
+
If for some reason you cannot use es modules, we provide alternate sub-packages `web` and `nodejs` (defined as `exports` in the root package.json).
|
|
138
|
+
|
|
139
|
+
The `nodejs` subpackage uses `fs` and CommonJS modules. To use it, you can import it like so:
|
|
140
|
+
|
|
141
|
+
```
|
|
142
|
+
const cedar = require('@cedar-policy/cedar-wasm/nodejs')
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
The `web` subpackage exposes an `initSync` function that you can use to load Cedar in scenarios where you want to load the wasm binary async for whatever reason. Using the `web` subpackage may also be necessary with some `jest` setups. Here's how you use the `web` subpackage:
|
|
146
|
+
|
|
147
|
+
```
|
|
148
|
+
const wasmBuffer = ... // `fetch` it or use `fs` to read it from `node_modules` in jest setupTests
|
|
149
|
+
import * as cedarJsBindings from '@cedar-policy/cedar-wasm/web';
|
|
150
|
+
cedarJsBindings.initSync(wasmBuffer);
|
|
151
|
+
```
|