{"id":13787904,"url":"https://github.com/open-policy-agent/npm-opa-wasm","last_synced_at":"2025-04-12T23:33:00.171Z","repository":{"id":34895938,"uuid":"186678915","full_name":"open-policy-agent/npm-opa-wasm","owner":"open-policy-agent","description":"Open Policy Agent WebAssembly NPM module (opa-wasm)","archived":false,"fork":false,"pushed_at":"2024-04-12T11:42:30.000Z","size":1676,"stargazers_count":124,"open_issues_count":16,"forks_count":40,"subscribers_count":11,"default_branch":"main","last_synced_at":"2024-04-14T12:51:52.344Z","etag":null,"topics":["authorization","browser","declarative","deno","nodejs","opa","open-policy-agent","policy","wasm","webassembly"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/open-policy-agent.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2019-05-14T18:33:20.000Z","updated_at":"2024-04-23T12:55:19.641Z","dependencies_parsed_at":"2023-02-17T18:46:06.901Z","dependency_job_id":"6d77889a-4e54-4379-9b62-a42a59eb97ed","html_url":"https://github.com/open-policy-agent/npm-opa-wasm","commit_stats":{"total_commits":235,"total_committers":22,"mean_commits":"10.681818181818182","dds":0.4212765957446809,"last_synced_commit":"7c5ec075df7e54cef2b569efc905150a1c44bb39"},"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/open-policy-agent%2Fnpm-opa-wasm","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/open-policy-agent%2Fnpm-opa-wasm/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/open-policy-agent%2Fnpm-opa-wasm/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/open-policy-agent%2Fnpm-opa-wasm/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/open-policy-agent","download_url":"https://codeload.github.com/open-policy-agent/npm-opa-wasm/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248647258,"owners_count":21139081,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["authorization","browser","declarative","deno","nodejs","opa","open-policy-agent","policy","wasm","webassembly"],"created_at":"2024-08-03T21:00:33.089Z","updated_at":"2025-04-12T23:33:00.147Z","avatar_url":"https://github.com/open-policy-agent.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"**Work in Progress -- Contributions welcome!!**\n\n# Open Policy Agent WebAssemby NPM Module\n\nThis is the source for the\n[@open-policy-agent/opa-wasm](https://www.npmjs.com/package/@open-policy-agent/opa-wasm)\nNPM module which is a small SDK for using WebAssembly (wasm) compiled\n[Open Policy Agent](https://www.openpolicyagent.org/) Rego policies.\n\n# Getting Started\n\n## Install the module\n\n```\nnpm install @open-policy-agent/opa-wasm\n```\n\n## Usage\n\nThere are only a couple of steps required to start evaluating the policy.\n\n### Import the module\n\n```javascript\nconst { loadPolicy } = require(\"@open-policy-agent/opa-wasm\");\n```\n\n### Load the policy\n\n```javascript\nloadPolicy(policyWasm);\n```\n\nThe `loadPolicy` function returns a Promise with the loaded policy. Typically\nthis means loading it in an `async` function like:\n\n```javascript\nconst policy = await loadPolicy(policyWasm);\n```\n\nOr something like:\n\n```javascript\nloadPolicy(policyWasm).then((policy) =\u003e {\n  // evaluate or save the policy\n}, (error) =\u003e {\n  console.error(\"Failed to load policy: \" + error);\n});\n```\n\nThe `policyWasm` needs to be either the raw byte array of the compiled policy\nWasm file, or a WebAssembly module.\n\nFor example:\n\n```javascript\nconst fs = require(\"fs\");\n\nconst policyWasm = fs.readFileSync(\"policy.wasm\");\n```\n\nAlternatively the bytes can be pulled in remotely from a `fetch` or in some\ncases (like CloudFlare Workers) the Wasm binary can be loaded directly into the\njavascript context through external APIs.\n\n### Evaluate the Policy\n\nThe loaded policy object returned from `loadPolicy()` has a couple of important\nAPIs for policy evaluation:\n\n`setData(data)` -- Provide an external `data` document for policy evaluation.\n\n- `data` MUST be a serializable object or `ArrayBuffer`, which assumed to be a\n  well-formed stringified JSON\n\n`evaluate(input)` -- Evaluates the policy using any loaded data and the supplied\n`input` document.\n\n- `input` parameter MAY be an `object`, primitive literal or `ArrayBuffer`,\n  which assumed to be a well-formed stringified JSON\n\n\u003e `ArrayBuffer` supported in the APIs above as a performance optimisation\n\u003e feature, given that either network or file system provided contents can easily\n\u003e be represented as `ArrayBuffer` in a very performant way.\n\nExample:\n\n```javascript\ninput = '{\"path\": \"/\", \"role\": \"admin\"}';\n\nloadPolicy(policyWasm).then((policy) =\u003e {\n  resultSet = policy.evaluate(input);\n  if (resultSet == null) {\n    console.error(\"evaluation error\");\n  } else if (resultSet.length == 0) {\n    console.log(\"undefined\");\n  } else {\n    console.log(\"allowed = \" + resultSet[0].result);\n  }\n}).catch((error) =\u003e {\n  console.error(\"Failed to load policy: \", error);\n});\n```\n\n\u003e For any `opa build` created WASM binaries the result set, when defined, will\n\u003e contain a `result` key with the value of the compiled entrypoint. See\n\u003e [https://www.openpolicyagent.org/docs/latest/wasm/](https://www.openpolicyagent.org/docs/latest/wasm/)\n\u003e for more details.\n\n### Writing the policy\n\nSee\n[https://www.openpolicyagent.org/docs/latest/how-do-i-write-policies/](https://www.openpolicyagent.org/docs/latest/how-do-i-write-policies/)\n\n### Compiling the policy\n\nEither use the\n[Compile REST API](https://www.openpolicyagent.org/docs/latest/rest-api/#compile-api)\nor `opa build` CLI tool.\n\nFor example, with OPA v0.20.5+:\n\n```bash\nopa build -t wasm -e example/allow example.rego\n```\n\nWhich is compiling the `example.rego` policy file with the result set to\n`data.example.allow`. The result will be an OPA bundle with the `policy.wasm`\nbinary included. See [./examples](./examples) for a more comprehensive example.\n\nSee `opa build --help` for more details.\n\n## Development\n\n### Lint and Format checks\n\nThis project is using Deno's\n[lint](https://deno.land/manual@v1.14.0/tools/linter) and\n[formatter](https://deno.land/manual@v1.14.0/tools/formatter) tools in CI. With\n`deno`\n[installed locally](https://deno.land/manual@v1.14.0/getting_started/installation),\nthe same checks can be invoked using `npm`:\n\n- `npm run lint`\n- `npm run fmt` -- this will fix the formatting\n- `npm run fmt:check` -- this happens in CI\n\nAll of these operate on git-tracked files, so make sure you've committed the\ncode you'd like to see checked. Alternatively, you can invoke\n`deno lint my_new_file.js` directly, too.\n\n### Build\n\nThe published package provides four different entrypoints for consumption:\n\n1. A CommonJS module for consumption with older versions of Node or those using\n   `require()`:\n   ```js\n   const { loadPolicy } = require(\"@open-policy-agent/opa-wasm\");\n   ```\n1. An ESM module for consumption with newer versions of Node:\n   ```js\n   import { loadPolicy } from \"@open-policy-agent/opa-wasm\";\n   ```\n1. An ESM module for consumption in modern browsers (this will contain all\n   dependencies already bundled and can be used standalone).\n   ```html\n   \u003cscript type=\"module\"\u003e\n   import opa from 'https://unpkg.com/@open-policy-agent/opa-wasm@latest/dist/opa-wasm-browser.esm.js';\n   opa.loadPolicy(...);\n   \u003c/script\u003e\n   ```\n1. A script for consumption in all browsers (this will export an `opa` global\n   variable).\n   ```js\n   \u003cscript src=\"https://unpkg.com/@open-policy-agent/opa-wasm@latest/dist/opa-wasm-browser.js\"\u003e\u003c/script\u003e\n   \u003cscript\u003e\n   opa.loadPolicy(...);\n   \u003c/script\u003e\n   ```\n\nThe browser builds are generated in the `./build.sh` script and use\n[`esbuild`][esbuild]. All exports are defined in the `exports` field in the\npackage.json file. More detials on how these work are described in the\n[Conditional Exports][conditional-exports] documentation.\n\nFor TypeScript projects we also generate an opa.d.ts declaration file that will\ngive correct typings and is also defined under the `types` field in the\npackage.json.\n\n[esbuild]: https://esbuild.github.io/\n[conditional-exports]: https://nodejs.org/api/packages.html#conditional-exports\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fopen-policy-agent%2Fnpm-opa-wasm","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fopen-policy-agent%2Fnpm-opa-wasm","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fopen-policy-agent%2Fnpm-opa-wasm/lists"}