@cedar-policy/cedar-wasm

中文文档
4.14.0发布于 2026/10/19Apache-2.0TypeScriptESM + CJS零依赖构建来源已验证近 30 天下载 171.1万

Wasm bindings and typescript types for Cedar lib

原始使用文档
文档目录5 节
  1. Installing
  2. Loading in bare nodeJs without a bundler
  3. Loading in webpack 5:
  4. Loading in vite 5:
  5. Alternate loading strategies

cedar-wasm

An implementation of various cedar functions to enable developers to write typescript and javascript applications using Cedar and wasm.

Installing

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

Loading is much more complicated. It depends on your environment. We offer three subpackages:

  • es modules (default). It loads wasm in a way that will be bundled into a single file if you use dynamic imports, or embedded into your main bundle if you use regular imports.
  • commonjs (for node). It loads wasm using node's fs module, synchronously. Not really designed for bundling or shipping to the browser.
  • web: more customizable. This one is for when you need to load the wasm in some totally custom way. More details in the "alternate loading strategies" section.

These sub-packages are named @cedar-policy/cedar-wasm, @cedar-policy/cedar-wasm/nodejs, and @cedar-policy/cedar-wasm/web, respectively.

Loading in bare nodeJs without a bundler

Node uses CommonJs so you have to import with require, or with dynamic import().

Importing the CJS export:

const cedar = require('@cedar-policy/cedar-wasm/nodejs');
console.log(cedar.getCedarVersion());

Importing the esm version using esm async import:

import('@cedar-policy/cedar-wasm/nodejs')
  .then(cedar => console.log(cedar.getCedarVersion()));

Loading in webpack 5:

Minimal package.json for webpack including dev server:

{
  "name": "webpack-ts-tester",
  "version": "1.0.0",
  "description": "", 
  "private": true,
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "webpack",
    "dev": "webpack serve"
  },  
  "keywords": [], 
  "author": "", 
  "license": "ISC",
  "dependencies": {
    "@cedar-policy/cedar-wasm": "3.2.0"
  },  
  "devDependencies": {
    "ts-loader": "^9.5.1",
    "typescript": "^5.4.5",
    "webpack": "^5.91.0",
    "webpack-cli": "^5.1.4",
    "webpack-dev-server": "^5.0.4",
    "html-webpack-plugin": "^5.6.0"
  }
}

Minimal tsconfig:

{
  "compilerOptions": {
    "outDir": "./dist/",
    "noImplicitAny": true,
    "module": "es2020",
    "target": "es5",
    "jsx": "react",
    "allowJs": true,
    "moduleResolution": "node"
  }
}

Configure webpack.config.js:

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = { 
  mode: 'development', // change this to suit you
  entry: './src/index.ts',
  module: {
    rules: [
      {   
        test: /\.tsx?$/,
        use: 'ts-loader',
        exclude: /node_modules/,
      },  
    ],  
  },  
  resolve: {
    extensions: ['.tsx', '.ts', '.js'],
  },  
  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'dist'),
  },  
  experiments: {
    asyncWebAssembly: true, // enables wasm support in webpack
  },
  plugins: [new HtmlWebpackPlugin()],
  devServer: {
    static: {
      directory: path.join(__dirname, 'dist'),
    },  
    compress: true,
    port: 8000,
  }
};

Finally, load the code from your index.ts file. We recommend dynamic imports:

import('@cedar-policy/cedar-wasm').then(mod => {
  // cache it globally here or invoke functions like mod.getCedarVersion();
});

Loading in vite 5:

Starting from the vite typescript template, install these two dependencies to enable wasm:

npm i --save-dev vite-plugin-top-level-await vite-plugin-wasm

Then add those two plugins to your vite config in vite.config.js:

import wasm from 'vite-plugin-wasm';
import topLevelAwait from 'vite-plugin-top-level-await';
import { defineConfig } from 'vite';

export default defineConfig({
  plugins: [
    wasm(),
    topLevelAwait()
  ]
});

Finally, load the code. We recommend dynamic imports:

import('@cedar-policy/cedar-wasm').then(mod => {
  // cache it globally here or invoke functions like mod.getCedarVersion();
});

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

版本与发布记录

@cedar-policy/cedar-wasm 共发布 33 个版本,首次发布于 2024/05/22;过去 12 个月发布了 16 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
4.14.12026/11/036 天前
4.14.0latest2026/10/1920 天前
4.13.02026/09/161 个月前
4.12.02026/07/293 个月前
3.4.3v3.4-x2026/06/254 个月前
4.11.22026/06/234 个月前

关于 @cedar-policy/cedar-wasm 的常见问题

@cedar-policy/cedar-wasm 是什么?

Wasm bindings and typescript types for Cedar lib

如何安装 @cedar-policy/cedar-wasm?

在项目目录运行 npm install @cedar-policy/cedar-wasm,或使用 pnpm add / yarn add / bun add。

@cedar-policy/cedar-wasm 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@cedar-policy/cedar-wasm 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@cedar-policy/cedar-wasm 可以商用吗?

@cedar-policy/cedar-wasm 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@cedar-policy/cedar-wasm 的下载量有多少?

根据 npm 官方下载统计,@cedar-policy/cedar-wasm 在 2026/10/10 – 2026/11/08 这 30 天内被下载 1,710,805 次,日均约 57,027 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@cedar-policy/cedar-wasm 还在维护吗?

最新版本 4.14.0 发布于 2026/10/19(20 天前);至今共发布 33 个版本,其中过去 12 个月发布了 16 个;首次发布于 2024/05/22。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。