From 0a52e12ad5643408db033dbeeed415eeef0e8a85 Mon Sep 17 00:00:00 2001 From: abcxff <79597906+abcxff@users.noreply.github.com> Date: Mon, 22 Sep 2025 15:03:30 -0400 Subject: [PATCH] feat(examples): deno counter --- examples/counter/package.json | 4 +-- examples/deno/.gitignore | 2 ++ examples/deno/README.md | 39 +++++++++++++++++++++++++++++ examples/deno/deno.json | 15 +++++++++++ examples/deno/package.json | 21 ++++++++++++++++ examples/deno/scripts/connect.ts | 17 +++++++++++++ examples/deno/src/registry.ts | 23 +++++++++++++++++ examples/deno/src/server.ts | 12 +++++++++ examples/deno/tsconfig.json | 43 ++++++++++++++++++++++++++++++++ packages/rivetkit/package.json | 2 +- 10 files changed, 175 insertions(+), 3 deletions(-) create mode 100644 examples/deno/.gitignore create mode 100644 examples/deno/README.md create mode 100644 examples/deno/deno.json create mode 100644 examples/deno/package.json create mode 100644 examples/deno/scripts/connect.ts create mode 100644 examples/deno/src/registry.ts create mode 100644 examples/deno/src/server.ts create mode 100644 examples/deno/tsconfig.json diff --git a/examples/counter/package.json b/examples/counter/package.json index 30c1ffe36..6f8cf175b 100644 --- a/examples/counter/package.json +++ b/examples/counter/package.json @@ -1,6 +1,6 @@ { - "name": "counter", - "version": "2.0.9", + "name": "example-counter", + "version": "2.0.8", "private": true, "type": "module", "scripts": { diff --git a/examples/deno/.gitignore b/examples/deno/.gitignore new file mode 100644 index 000000000..79b7a1192 --- /dev/null +++ b/examples/deno/.gitignore @@ -0,0 +1,2 @@ +.actorcore +node_modules \ No newline at end of file diff --git a/examples/deno/README.md b/examples/deno/README.md new file mode 100644 index 000000000..aa297f886 --- /dev/null +++ b/examples/deno/README.md @@ -0,0 +1,39 @@ +# Deno Example for RivetKit + +Example project demonstrating basic actor state management and RPC calls with [RivetKit](https://rivetkit.org) using Deno runtime. + +[Learn More →](https://github.com/rivet-dev/rivetkit) + +[Discord](https://rivet.dev/discord) — [Documentation](https://rivetkit.org) — [Issues](https://github.com/rivet-dev/rivetkit/issues) + +## Getting Started + +### Prerequisites + +- Deno + +### Installation + +```sh +git clone https://github.com/rivet-dev/rivetkit +cd rivetkit/examples/deno +pnpm install +``` + +**Notice:** We use `pnpm install` here as Deno offers compatability with package.json via npm/pnpm. Some packages used in rivetkit are simpler to install with npm/pnpm. + +### Development + +```sh +deno task dev +``` + +Run the connect script to interact with the counter: + +```sh +deno task connect +``` + +## License + +Apache 2.0 \ No newline at end of file diff --git a/examples/deno/deno.json b/examples/deno/deno.json new file mode 100644 index 000000000..04fbcc788 --- /dev/null +++ b/examples/deno/deno.json @@ -0,0 +1,15 @@ +{ + "tasks": { + "dev": "deno run --allow-env --allow-sys --allow-read --allow-ffi --allow-net src/server.ts", + "check-types": "deno check", + "test": "deno test --allow-env --allow-sys --allow-read --allow-ffi --allow-net", + "connect": "deno run --allow-env --allow-sys --allow-read --allow-ffi --allow-net scripts/connect.ts" + }, + "imports": { + "os": "node:os", + "path": "node:path", + "fs": "node:fs", + "fs/promises": "node:fs/promises", + "crypto": "node:crypto" + } +} diff --git a/examples/deno/package.json b/examples/deno/package.json new file mode 100644 index 000000000..8343abebe --- /dev/null +++ b/examples/deno/package.json @@ -0,0 +1,21 @@ +{ + "//": "This package.json is required, once is released with updated engine-runner packages, it can be converted to deno.json", + "name": "example-deno", + "version": "2.0.6", + "private": true, + "type": "module", + "scripts": { + "dev": "deno run --allow-all src/server.ts", + "check-types": "deno check", + "connect": "deno run --allow-all scripts/connect.ts" + }, + "devDependencies": { + "@types/deno": "^2.3.0", + "@types/node": "^24.5.2" + }, + "stableVersion": "0.8.0", + "dependencies": { + "hono": "4.9.8", + "rivetkit": "workspace:*" + } +} diff --git a/examples/deno/scripts/connect.ts b/examples/deno/scripts/connect.ts new file mode 100644 index 000000000..ca63be72e --- /dev/null +++ b/examples/deno/scripts/connect.ts @@ -0,0 +1,17 @@ +import { createClient } from "rivetkit/client"; +import type { Registry } from "../src/registry.ts"; + +async function main() { + const client = createClient("http://localhost:8080"); + + const counter = client.counter.getOrCreate().connect(); + + for (let i = 0; i < 5; i++) { + const out = await counter.increment(5); + console.log("RPC:", out); + + await new Promise((resolve) => setTimeout(resolve, 1000)); + } +} + +main(); diff --git a/examples/deno/src/registry.ts b/examples/deno/src/registry.ts new file mode 100644 index 000000000..44707e2fe --- /dev/null +++ b/examples/deno/src/registry.ts @@ -0,0 +1,23 @@ +import { actor, setup } from "rivetkit"; + +const counter = actor({ + state: { + count: 0, + }, + actions: { + increment: (c, x: number) => { + c.state.count += x; + c.broadcast("newCount", c.state.count); + return c.state.count; + }, + getCount: (c) => { + return c.state.count; + }, + }, +}); + +export const registry = setup({ + use: { counter }, +}); + +export type Registry = typeof registry; diff --git a/examples/deno/src/server.ts b/examples/deno/src/server.ts new file mode 100644 index 000000000..abb16c29b --- /dev/null +++ b/examples/deno/src/server.ts @@ -0,0 +1,12 @@ +import { upgradeWebSocket } from "hono/deno"; +import { registry } from "./registry.ts"; + +const { fetch } = registry.start({ + // Deno requires using Deno.serve + disableServer: true, + // Specify Deno-specific upgradeWebSocket + getUpgradeWebSocket: () => upgradeWebSocket, +}); + +// Start server +Deno.serve({ port: 8080 }, fetch); diff --git a/examples/deno/tsconfig.json b/examples/deno/tsconfig.json new file mode 100644 index 000000000..26becb209 --- /dev/null +++ b/examples/deno/tsconfig.json @@ -0,0 +1,43 @@ +{ + "compilerOptions": { + /* Visit https://aka.ms/tsconfig.json to read more about this file */ + + /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */ + "target": "esnext", + /* Specify a set of bundled library declaration files that describe the target runtime environment. */ + "lib": ["deno.ns", "esnext", "DOM"], + /* Specify what JSX code is generated. */ + "jsx": "react-jsx", + + /* Specify what module code is generated. */ + "module": "esnext", + /* Specify how TypeScript looks up a file from a given module specifier. */ + "moduleResolution": "bundler", + /* Specify type package names to be included without being referenced in a source file. */ + "types": ["node"], + /* Enable importing .json files */ + "resolveJsonModule": true, + + /* Allow JavaScript files to be a part of your program. Use the `checkJS` option to get errors from these files. */ + "allowJs": true, + /* Enable error reporting in type-checked JavaScript files. */ + "checkJs": false, + + /* Disable emitting files from a compilation. */ + "noEmit": true, + + /* Ensure that each file can be safely transpiled without relying on other imports. */ + "isolatedModules": true, + /* Allow 'import x from y' when a module doesn't have a default export. */ + "allowSyntheticDefaultImports": true, + /* Ensure that casing is correct in imports. */ + "forceConsistentCasingInFileNames": true, + + /* Enable all strict type-checking options. */ + "strict": true, + + /* Skip type checking all .d.ts files. */ + "skipLibCheck": true + }, + "include": ["src/**/*.ts", "scripts/**/*.ts", "tests/**/*.ts"] +} diff --git a/packages/rivetkit/package.json b/packages/rivetkit/package.json index e623a1ab3..6bf240776 100644 --- a/packages/rivetkit/package.json +++ b/packages/rivetkit/package.json @@ -170,8 +170,8 @@ "invariant": "^2.2.4", "nanoevents": "^9.1.0", "on-change": "^5.0.1", - "pino": "^9.5.0", "p-retry": "^6.2.1", + "pino": "^9.5.0", "zod": "^3.25.76" }, "devDependencies": {