|
| 1 | +--- |
| 2 | +sidebar_label: Writing policies in TypeScript/JavaScript |
| 3 | +sidebar_position: 010 |
| 4 | +title: Writing policies in TypeScript/JavaScript |
| 5 | +description: A tutorial introduction to writing policies in TypeScript/JavaScript. |
| 6 | +keywords: [kubewarden, kubernetes, writing policies in TypeScript, writing policies in JavaScript] |
| 7 | +doc-type: [tutorial] |
| 8 | +doc-topic: [kubewarden, writing-policies, typescript, javascript, introduction] |
| 9 | +doc-persona: [kubewarden-policy-developer] |
| 10 | +--- |
| 11 | + |
| 12 | +<head> |
| 13 | + <link rel="canonical" href="https://docs.kubewarden.io/tutorials/writing-policies/intro-typescript"/> |
| 14 | +</head> |
| 15 | + |
| 16 | +:::note |
| 17 | +TypeScript/JavaScript support for WebAssembly is rapidly evolving. |
| 18 | +This page was last revised in November 2025. |
| 19 | +::: |
| 20 | + |
| 21 | +As stated on the [official website](https://www.typescriptlang.org/): |
| 22 | + |
| 23 | +> TypeScript extends JavaScript by adding types. |
| 24 | +> |
| 25 | +> By understanding JavaScript, TypeScript saves you time catching errors and |
| 26 | +> providing fixes before you run code. |
| 27 | +
|
| 28 | +Kubewarden uses [Javy](https://github.com/bytecodealliance/javy) (a Bytecode Alliance project) to build WebAssembly binaries from JavaScript and TypeScript. |
| 29 | + |
| 30 | +> Javy takes your JavaScript code and executes it in a WebAssembly context. |
| 31 | +> |
| 32 | +> It features an embedded QuickJS engine compiled to WebAssembly that can execute JavaScript. |
| 33 | +> |
| 34 | +> The project provides both a CLI and a set of APIs for embedding and customizing the behavior when running JavaScript in WebAssembly. |
| 35 | +
|
| 36 | +The Kubewarden project currently uses Javy for these reasons: |
| 37 | + |
| 38 | +- Mature JavaScript engine (QuickJS) compiled to WebAssembly. |
| 39 | +- Support for [WASI interface](../wasi/01-intro-wasi.md) through custom host functions. |
| 40 | +- Smaller binary sizes compared to other JavaScript-to-WebAssembly solutions. |
| 41 | +- Active development and maintenance by the Bytecode Alliance. |
| 42 | + |
| 43 | +## Javy limitations |
| 44 | + |
| 45 | +Javy runs JavaScript in a sandboxed WebAssembly environment with certain constraints: |
| 46 | + |
| 47 | +- **WASI environment only**: Access limited to stdin/stdout/stderr and explicitly provided host capabilities. |
| 48 | +- **No Node.js APIs**: Standard Node.js modules like `fs`, `http`, or `crypto` aren't available. |
| 49 | +- **Limited standard library**: Only core JavaScript features and explicitly enabled APIs are accessible. |
| 50 | +- **Single-threaded execution**: No support for Web Workers or multi-threading. |
| 51 | + |
| 52 | +Despite these limitations, Javy provides sufficient capabilities for writing effective Kubewarden validation policies through the hosts capabilities system. |
| 53 | + |
| 54 | +:::warning |
| 55 | +Writing to STDOUT breaks policies - use STDERR for logging instead. |
| 56 | +::: |
| 57 | + |
| 58 | +## Tooling |
| 59 | + |
| 60 | +Writing Kubewarden policies requires: |
| 61 | + |
| 62 | +- **Node.js**: JavaScript runtime. |
| 63 | +- **npm**: For dependency management. |
| 64 | +- **TypeScript**: Recommended for type safety (optional). |
| 65 | + |
| 66 | +:::warning |
| 67 | +Ensure you're using Node.js 18 or higher. Older versions may not be compatible with the compilation toolchain. |
| 68 | +::: |
| 69 | + |
| 70 | +These TypeScript/JavaScript libraries are useful when writing a Kubewarden policy: |
| 71 | + |
| 72 | +- [Kubewarden JavaScript SDK](https://github.com/kubewarden/policy-sdk-js): Provides structures and functions reducing the amount of code necessary. It also provides test helpers and access to all host capabilities. |
| 73 | +- [Kubernetes TypeScript types](https://github.com/silverlyra/kubernetes-types): Provides TypeScript definitions for all Kubernetes resources, enabling type-safe policy development. |
| 74 | + |
| 75 | +The Kubewarden project provides a [template JavaScript/TypeScript policy project](https://github.com/kubewarden/js-policy-template) you can use to create Kubewarden policies. |
| 76 | + |
| 77 | +## Getting the toolchain |
| 78 | + |
| 79 | +The easiest way to get the toolchain is by using the Kubewarden JavaScript SDK, which includes the Javy compilation plug-in: |
| 80 | + |
| 81 | +```bash |
| 82 | +npm install kubewarden-policy-sdk |
| 83 | +``` |
| 84 | + |
| 85 | +The Javy plug-in binary is automatically included and you can find it at: |
| 86 | + |
| 87 | +``` |
| 88 | +node_modules/kubewarden-policy-sdk/plugin/javy-plugin-kubewarden.wasm |
| 89 | +``` |
| 90 | + |
| 91 | +## Tutorial prerequisites |
| 92 | + |
| 93 | +During this tutorial you need these tools on your development machine: |
| 94 | + |
| 95 | +- **Node.js**: Version 18 or higher with npm for dependency management. |
| 96 | +- [**`bats`**](https://github.com/bats-core/bats-core): Used to write the tests and automate their execution. |
| 97 | +- [**`kwctl`** ≥ `v1.30`](https://github.com/kubewarden/kwctl/releases): CLI tool provided by Kubewarden to run its policies outside of Kubernetes, among other actions. It's covered in [the testing policies section](../../testing-policies/index.md) of the documentation. |
| 98 | +- [**`javy`** ≥ `6.0.0`](https://github.com/bytecodealliance/javy): CLI tool for compiling JavaScript code to WebAssembly modules. |
0 commit comments