Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions cedar-wasm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ An implementation of various cedar functions to enable developers to write types

## Installing

Installing is simple, just run `npm i @cedar-policy/cedar-wasm` or install with whatever your favorite package manager is.

Installing is simple, just run `npm i @cedar-policy/cedar-wasm --save` or install with whatever your favorite package manager is.

## Loading in webpack 5:

Expand Down Expand Up @@ -133,3 +132,20 @@ import('@cedar-policy/cedar-wasm').then(mod => {
});
```

## Alternate loading strategies

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).

The `nodejs` subpackage uses `fs` and CommonJS modules. To use it, you can import it like so:

```
const cedar = require('@cedar-policy/cedar-wasm/nodejs')
```

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:

```
const wasmBuffer = ... // `fetch` it or use `fs` to read it from `node_modules` in jest setupTests
import * as cedarJsBindings from '@cedar-policy/cedar-wasm/web';
cedarJsBindings.initSync(wasmBuffer);
```
87 changes: 62 additions & 25 deletions cedar-wasm/build-wasm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,67 @@
# limitations under the License.

# This script calls wasm-pack build and post-processes the generated TS types to fix them.
# It also produces three sets of outputs for different needs of different consumers
# Without this, the built wasm still works, but the Typescript definitions made by tsify don't.
set -e
cargo build
wasm-pack build --scope cedar-policy --target bundler

sed -i "s/[{]\s*!: /{ \"!\": /g" pkg/cedar_wasm.d.ts
sed -i "s/[{]\s*==: /{ \"==\": /g" pkg/cedar_wasm.d.ts
sed -i "s/[{]\s*!=: /{ \"!=\": /g" pkg/cedar_wasm.d.ts
sed -i "s/[{]\s*<: /{ \"<\": /g" pkg/cedar_wasm.d.ts
sed -i "s/[{]\s*<=: /{ \"<=\": /g" pkg/cedar_wasm.d.ts
sed -i "s/[{]\s*>: /{ \">\": /g" pkg/cedar_wasm.d.ts
sed -i "s/[{]\s*>=: /{ \">=\": /g" pkg/cedar_wasm.d.ts
sed -i "s/[{]\s*&&: /{ \"\&\&\": /g" pkg/cedar_wasm.d.ts
sed -i "s/[{]\s*||: /{ \"||\": /g" pkg/cedar_wasm.d.ts
sed -i "s/[{]\s*[+]: /{ \"+\": /g" pkg/cedar_wasm.d.ts
sed -i "s/[{]\s*-: /{ \"-\": /g" pkg/cedar_wasm.d.ts
sed -i "s/[{]\s*[*]: /{ \"*\": /g" pkg/cedar_wasm.d.ts
sed -i "s/[{]\s*\.: /{ \".\": /g" pkg/cedar_wasm.d.ts
sed -i "s/ | __skip//g" pkg/cedar_wasm.d.ts
sed -i "s/SchemaFragment/Schema/g" pkg/cedar_wasm.d.ts

echo "type SmolStr = string;" >> pkg/cedar_wasm.d.ts
echo "type Name = string;" >> pkg/cedar_wasm.d.ts
echo "type Id = string;" >> pkg/cedar_wasm.d.ts
echo "export type TypeOfAttribute = SchemaType & { required?: boolean };" >> pkg/cedar_wasm.d.ts
echo "export type Context = Record<string, CedarValueJson>;" >> pkg/cedar_wasm.d.ts
echo "Finished post-processing types file"
main () {
rm -rf pkg || true
mkdir pkg
cargo build
wasm-pack build --scope cedar-policy --target bundler --out-dir pkg/esm
wasm-pack build --scope cedar-policy --target nodejs --out-dir pkg/nodejs
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The readme says the subpackage is "node" but does this line mean it's actually "nodejs"?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perhaps that's ugly... what controls the subpackage name is package.json

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed the name of the subpackage in package.json to align with the naem of the wasm target

wasm-pack build --scope cedar-policy --target web --out-dir pkg/web
cp pkg/esm/README.md pkg/README.md

fix_package_json_files

process_types_file "pkg/esm/cedar_wasm.d.ts"
process_types_file "pkg/nodejs/cedar_wasm.d.ts"
process_types_file "pkg/web/cedar_wasm.d.ts"
}

fix_package_json_files() {
jq -s '.[0] * .[1]' pkg/esm/package.json package.json.patch > pkg/package.json
echo "Created root package.json"
mv pkg/esm/package.json pkg/esm/package.json.bak
mv pkg/web/package.json pkg/web/package.json.bak
mv pkg/nodejs/package.json pkg/nodejs/package.json.bak
jq '. + {"type": "module"}' pkg/esm/package.json.bak > pkg/esm/package.json
jq '. + {"type": "module"}' pkg/web/package.json.bak > pkg/web/package.json
jq '. + {"type": "commonjs"}' pkg/nodejs/package.json.bak > pkg/nodejs/package.json
rm pkg/esm/package.json.bak
rm pkg/web/package.json.bak
rm pkg/nodejs/package.json.bak
echo "Patched sub-package json files"
}

process_types_file() {
local types_file="$1"
echo "processing types file: $1"
sed -i "s/[{]\s*!: /{ \"!\": /g" "$types_file"
sed -i "s/[{]\s*==: /{ \"==\": /g" "$types_file"
sed -i "s/[{]\s*!=: /{ \"!=\": /g" "$types_file"
sed -i "s/[{]\s*<: /{ \"<\": /g" "$types_file"
sed -i "s/[{]\s*<=: /{ \"<=\": /g" "$types_file"
sed -i "s/[{]\s*>: /{ \">\": /g" "$types_file"
sed -i "s/[{]\s*>=: /{ \">=\": /g" "$types_file"
sed -i "s/[{]\s*&&: /{ \"\&\&\": /g" "$types_file"
sed -i "s/[{]\s*||: /{ \"||\": /g" "$types_file"
sed -i "s/[{]\s*[+]: /{ \"+\": /g" "$types_file"
sed -i "s/[{]\s*-: /{ \"-\": /g" "$types_file"
sed -i "s/[{]\s*[*]: /{ \"*\": /g" "$types_file"
sed -i "s/[{]\s*\.: /{ \".\": /g" "$types_file"
sed -i "s/ | __skip//g" "$types_file"
sed -i "s/SchemaFragment/SchemaJson/g" "$types_file"
sed -i "s/[{] json: JsonValueWithNoDuplicateKeys /{ json: SchemaJson /g" "$types_file"

echo "type SmolStr = string;" >> "$types_file"
echo "type Name = string;" >> "$types_file"
echo "type Id = string;" >> "$types_file"
echo "export type TypeOfAttribute = SchemaType & { required?: boolean };" >> "$types_file"
echo "export type Context = Record<string, CedarValueJson>;" >> "$types_file"
}


main
echo "Finished custom build script"
20 changes: 20 additions & 0 deletions cedar-wasm/package.json.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"files": ["esm/*", "nodejs/*", "web/*"],
"sideEffects": ["./snippets/*"],
"module": "esm/cedar_wasm.js",
"types": "esm/cedar_wasm.d.ts",
"exports": {
".": {
"import": "esm/cedar_wasm.js",
"types": "esm/cedar_wasm.d.ts"
},
"./nodejs": {
"import": "nodejs/cedar_wasm.js",
"types": "nodejs/cedar_wasm.d.ts"
},
"./web": {
"import": "web/cedar_wasm.js",
"types": "web/cedar_wasm.d.ts"
}
}
}