Skip to content

Commit 224e7e0

Browse files
NathanFlurryNicholasKissel
authored andcommitted
chore(rivetkit-typescript): remove dependency on node modules
1 parent 392dab4 commit 224e7e0

File tree

19 files changed

+382
-99
lines changed

19 files changed

+382
-99
lines changed

biome.json

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,56 @@
4141
"noExplicitAny": "off"
4242
}
4343
}
44-
}
44+
},
45+
"overrides": [
46+
{
47+
"includes": [
48+
"rivetkit-typescript/packages/rivetkit/src/**/*",
49+
"!rivetkit-typescript/packages/rivetkit/src/test/**/*"
50+
],
51+
"linter": {
52+
"rules": {
53+
"style": {
54+
"noRestrictedImports": {
55+
"level": "error",
56+
"options": {
57+
"paths": {
58+
"node:crypto": "Use '@/utils/node' getNodeCrypto() instead. Direct Node.js imports are only allowed in src/utils/node.ts",
59+
"node:fs": "Use '@/utils/node' getNodeFsSync() instead. Direct Node.js imports are only allowed in src/utils/node.ts",
60+
"node:fs/promises": "Use '@/utils/node' getNodeFs() instead. Direct Node.js imports are only allowed in src/utils/node.ts",
61+
"node:path": "Use '@/utils/node' getNodePath() instead. Direct Node.js imports are only allowed in src/utils/node.ts",
62+
"node:os": "Use '@/utils/node' getNodeOs() instead. Direct Node.js imports are only allowed in src/utils/node.ts",
63+
"node:child_process": "Use '@/utils/node' getNodeChildProcess() instead. Direct Node.js imports are only allowed in src/utils/node.ts",
64+
"node:stream": "Use '@/utils/node' getNodeStream() instead. Direct Node.js imports are only allowed in src/utils/node.ts",
65+
"node:net": "Use '@/utils/node' instead. Direct Node.js imports are only allowed in src/utils/node.ts",
66+
"node:url": "Use '@/utils/node' instead. Direct Node.js imports are only allowed in src/utils/node.ts",
67+
"crypto": "Use '@/utils/node' getNodeCrypto() instead. Direct Node.js imports are only allowed in src/utils/node.ts",
68+
"fs": "Use '@/utils/node' getNodeFsSync() or getNodeFs() instead. Direct Node.js imports are only allowed in src/utils/node.ts",
69+
"fs/promises": "Use '@/utils/node' getNodeFs() instead. Direct Node.js imports are only allowed in src/utils/node.ts",
70+
"path": "Use '@/utils/node' getNodePath() instead. Direct Node.js imports are only allowed in src/utils/node.ts",
71+
"os": "Use '@/utils/node' getNodeOs() instead. Direct Node.js imports are only allowed in src/utils/node.ts",
72+
"child_process": "Use '@/utils/node' getNodeChildProcess() instead. Direct Node.js imports are only allowed in src/utils/node.ts",
73+
"stream": "Use '@/utils/node' getNodeStream() instead. Direct Node.js imports are only allowed in src/utils/node.ts",
74+
"net": "Use '@/utils/node' instead. Direct Node.js imports are only allowed in src/utils/node.ts",
75+
"url": "Use '@/utils/node' instead. Direct Node.js imports are only allowed in src/utils/node.ts"
76+
}
77+
}
78+
}
79+
}
80+
}
81+
}
82+
},
83+
{
84+
"includes": [
85+
"rivetkit-typescript/packages/rivetkit/src/utils/node.ts"
86+
],
87+
"linter": {
88+
"rules": {
89+
"style": {
90+
"noRestrictedImports": "off"
91+
}
92+
}
93+
}
94+
}
95+
]
4596
}

engine/package.json

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,11 @@
11
{
22
"name": "@rivetkit/engine",
3-
"private": true,
3+
"version": "1.0.0",
4+
"keywords": [],
5+
"author": "",
6+
"license": "ISC",
47
"packageManager": "[email protected]",
5-
"scripts": {
6-
"start": "npx turbo watch build",
7-
"build": "npx turbo build",
8-
"test": "npx turbo test",
9-
"test:watch": "npx turbo watch test",
10-
"check-types": "npx turbo check-types",
11-
"fmt": "pnpm biome check --write --diagnostic-level=error ."
12-
},
13-
"devDependencies": {
14-
"@bare-ts/tools": "0.15.0",
15-
"@biomejs/biome": "^2.2.3",
16-
"lefthook": "^1.12.4",
17-
"tsup": "^8.5.0",
18-
"turbo": "^2.5.6",
19-
"typescript": "^5.9.2"
20-
},
218
"dependencies": {
22-
"@sentry/vite-plugin": "^2.23.1"
23-
},
24-
"resolutions": {
25-
"rivetkit": "workspace:*",
26-
"@clerk/shared": "3.27.1"
9+
"@vbare/compiler": "^0.0.3"
2710
}
2811
}

engine/sdks/typescript/runner-protocol/src/index.ts

Lines changed: 7 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pnpm-workspace.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
packages:
2+
- engine
23
- engine/docker/template
34
- engine/sdks/typescript/api-full
45
- engine/sdks/typescript/runner

rivetkit-typescript/packages/rivetkit/package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,10 @@
155155
"build": "tsup src/mod.ts src/client/mod.ts src/common/log.ts src/common/websocket.ts src/actor/errors.ts src/topologies/coordinate/mod.ts src/topologies/partition/mod.ts src/utils.ts src/driver-helpers/mod.ts src/driver-test-suite/mod.ts src/test/mod.ts src/inspector/mod.ts",
156156
"build:schema": "./scripts/compile-bare.ts compile schemas/client-protocol/v1.bare -o dist/schemas/client-protocol/v1.ts && ./scripts/compile-bare.ts compile schemas/file-system-driver/v1.bare -o dist/schemas/file-system-driver/v1.ts && ./scripts/compile-bare.ts compile schemas/actor-persist/v1.bare -o dist/schemas/actor-persist/v1.ts",
157157
"check-types": "tsc --noEmit",
158+
"lint": "biome check .",
159+
"lint:fix": "biome check --write .",
160+
"format": "biome format .",
161+
"format:write": "biome format --write .",
158162
"test": "vitest run",
159163
"test:watch": "vitest",
160164
"dump-openapi": "tsx scripts/dump-openapi.ts"
@@ -176,6 +180,7 @@
176180
},
177181
"devDependencies": {
178182
"@bare-ts/tools": "^0.13.0",
183+
"@biomejs/biome": "^2.2.3",
179184
"@hono/node-server": "^1.18.2",
180185
"@hono/node-ws": "^1.1.1",
181186
"@types/invariant": "^2",

rivetkit-typescript/packages/rivetkit/scripts/dump-openapi.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ import {
1313
import { type RunnerConfig, RunnerConfigSchema } from "@/registry/run-config";
1414
import { VERSION } from "@/utils";
1515

16-
function main() {
16+
async function main() {
1717
const registryConfig: RegistryConfig = RegistryConfigSchema.parse({
1818
use: {},
1919
});
2020
const registry = setup(registryConfig);
2121

2222
const driverConfig: RunnerConfig = RunnerConfigSchema.parse({
23-
driver: createFileSystemOrMemoryDriver(false),
23+
driver: await createFileSystemOrMemoryDriver(false),
2424
getUpgradeWebSocket: () => () => unimplemented(),
2525
inspector: {
2626
enabled: false,
@@ -70,7 +70,7 @@ function main() {
7070
"rivetkit-openapi",
7171
"openapi.json",
7272
);
73-
fs.writeFile(outputPath, JSON.stringify(openApiDoc, null, 2));
73+
await fs.writeFile(outputPath, JSON.stringify(openApiDoc, null, 2));
7474
console.log("Dumped OpenAPI to", outputPath);
7575
}
7676

rivetkit-typescript/packages/rivetkit/src/drivers/default.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import type { DriverConfig, RunnerConfig } from "@/registry/run-config";
77
/**
88
* Chooses the appropriate driver based on the run configuration.
99
*/
10-
export function chooseDefaultDriver(runConfig: RunnerConfig): DriverConfig {
10+
export async function chooseDefaultDriver(
11+
runConfig: RunnerConfig,
12+
): Promise<DriverConfig> {
1113
if (runConfig.endpoint && runConfig.driver) {
1214
throw new UserError(
1315
"Cannot specify both 'endpoint' and 'driver' in configuration",
@@ -31,5 +33,5 @@ export function chooseDefaultDriver(runConfig: RunnerConfig): DriverConfig {
3133
}
3234

3335
loggerWithoutContext().debug({ msg: "using default file system driver" });
34-
return createFileSystemOrMemoryDriver(true);
36+
return await createFileSystemOrMemoryDriver(true);
3537
}

rivetkit-typescript/packages/rivetkit/src/drivers/file-system/global-state.ts

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
import * as crypto from "node:crypto";
2-
import * as fsSync from "node:fs";
3-
import * as fs from "node:fs/promises";
4-
import * as path from "node:path";
51
import invariant from "invariant";
62
import { lookupInRegistry } from "@/actor/definition";
73
import { ActorAlreadyExists } from "@/actor/errors";
@@ -27,6 +23,12 @@ import {
2723
setLongTimeout,
2824
stringifyError,
2925
} from "@/utils";
26+
import {
27+
getNodeCrypto,
28+
getNodeFs,
29+
getNodeFsSync,
30+
getNodePath,
31+
} from "@/utils/node";
3032
import { logger } from "./log";
3133
import {
3234
ensureDirectoryExists,
@@ -93,6 +95,7 @@ export class FileSystemGlobalState {
9395
constructor(persist: boolean = true, customPath?: string) {
9496
this.#persist = persist;
9597
this.#storagePath = persist ? getStoragePath(customPath) : "/tmp";
98+
const path = getNodePath();
9699
this.#stateDir = path.join(this.#storagePath, "state");
97100
this.#dbsDir = path.join(this.#storagePath, "databases");
98101
this.#alarmsDir = path.join(this.#storagePath, "alarms");
@@ -104,6 +107,7 @@ export class FileSystemGlobalState {
104107
ensureDirectoryExistsSync(this.#alarmsDir);
105108

106109
try {
110+
const fsSync = getNodeFsSync();
107111
const actorIds = fsSync.readdirSync(this.#stateDir);
108112
this.#actorCountOnStartup = actorIds.length;
109113
} catch (error) {
@@ -131,15 +135,15 @@ export class FileSystemGlobalState {
131135
}
132136

133137
getActorStatePath(actorId: string): string {
134-
return path.join(this.#stateDir, actorId);
138+
return getNodePath().join(this.#stateDir, actorId);
135139
}
136140

137141
getActorDbPath(actorId: string): string {
138-
return path.join(this.#dbsDir, `${actorId}.db`);
142+
return getNodePath().join(this.#dbsDir, `${actorId}.db`);
139143
}
140144

141145
getActorAlarmPath(actorId: string): string {
142-
return path.join(this.#alarmsDir, actorId);
146+
return getNodePath().join(this.#alarmsDir, actorId);
143147
}
144148

145149
async *getActorsIterator(params: {
@@ -148,6 +152,7 @@ export class FileSystemGlobalState {
148152
let actorIds = Array.from(this.#actors.keys()).sort();
149153

150154
// Check if state directory exists first
155+
const fsSync = getNodeFsSync();
151156
if (fsSync.existsSync(this.#stateDir)) {
152157
actorIds = fsSync
153158
.readdirSync(this.#stateDir)
@@ -258,6 +263,7 @@ export class FileSystemGlobalState {
258263

259264
// Read & parse file
260265
try {
266+
const fs = getNodeFs();
261267
const stateData = await fs.readFile(stateFilePath);
262268

263269
// Cache the loaded state in handler
@@ -352,8 +358,10 @@ export class FileSystemGlobalState {
352358
// Persist alarm to disk
353359
if (this.#persist) {
354360
const alarmPath = this.getActorAlarmPath(actorId);
361+
const crypto = getNodeCrypto();
355362
const tempPath = `${alarmPath}.tmp.${crypto.randomUUID()}`;
356363
try {
364+
const path = getNodePath();
357365
await ensureDirectoryExists(path.dirname(alarmPath));
358366
const alarmData: schema.ActorAlarm = {
359367
actorId,
@@ -363,10 +371,12 @@ export class FileSystemGlobalState {
363371
ACTOR_ALARM_VERSIONED.serializeWithEmbeddedVersion(
364372
alarmData,
365373
);
374+
const fs = getNodeFs();
366375
await fs.writeFile(tempPath, data);
367376
await fs.rename(tempPath, alarmPath);
368377
} catch (error) {
369378
try {
379+
const fs = getNodeFs();
370380
await fs.unlink(tempPath);
371381
} catch {}
372382
logger().error({
@@ -391,10 +401,12 @@ export class FileSystemGlobalState {
391401
): Promise<void> {
392402
const dataPath = this.getActorStatePath(actorId);
393403
// Generate unique temp filename to prevent any race conditions
404+
const crypto = getNodeCrypto();
394405
const tempPath = `${dataPath}.tmp.${crypto.randomUUID()}`;
395406

396407
try {
397408
// Create directory if needed
409+
const path = getNodePath();
398410
await ensureDirectoryExists(path.dirname(dataPath));
399411

400412
// Convert to BARE types for serialization
@@ -409,11 +421,13 @@ export class FileSystemGlobalState {
409421
// Perform atomic write
410422
const serializedState =
411423
ACTOR_STATE_VERSIONED.serializeWithEmbeddedVersion(bareState);
424+
const fs = getNodeFs();
412425
await fs.writeFile(tempPath, serializedState);
413426
await fs.rename(tempPath, dataPath);
414427
} catch (error) {
415428
// Cleanup temp file on error
416429
try {
430+
const fs = getNodeFs();
417431
await fs.unlink(tempPath);
418432
} catch {
419433
// Ignore cleanup errors
@@ -548,12 +562,14 @@ export class FileSystemGlobalState {
548562
*/
549563
#loadAlarmsSync(): void {
550564
try {
565+
const fsSync = getNodeFsSync();
551566
const files = fsSync.existsSync(this.#alarmsDir)
552567
? fsSync.readdirSync(this.#alarmsDir)
553568
: [];
554569
for (const file of files) {
555570
// Skip temp files
556571
if (file.includes(".tmp.")) continue;
572+
const path = getNodePath();
557573
const fullPath = path.join(this.#alarmsDir, file);
558574
try {
559575
const buf = fsSync.readFileSync(fullPath);
@@ -622,6 +638,7 @@ export class FileSystemGlobalState {
622638
// On trigger: remove persisted alarm file
623639
if (this.#persist) {
624640
try {
641+
const fs = getNodeFs();
625642
await fs.unlink(this.getActorAlarmPath(actorId));
626643
} catch (err: any) {
627644
if (err?.code !== "ENOENT") {
@@ -668,6 +685,8 @@ export class FileSystemGlobalState {
668685
}
669686

670687
getOrCreateInspectorAccessToken(): string {
688+
const path = getNodePath();
689+
const fsSync = getNodeFsSync();
671690
const tokenPath = path.join(this.#storagePath, "inspector-token");
672691
if (fsSync.existsSync(tokenPath)) {
673692
return fsSync.readFileSync(tokenPath, "utf-8");
@@ -683,13 +702,15 @@ export class FileSystemGlobalState {
683702
*/
684703
#cleanupTempFilesSync(): void {
685704
try {
705+
const fsSync = getNodeFsSync();
686706
const files = fsSync.readdirSync(this.#stateDir);
687707
const tempFiles = files.filter((f) => f.includes(".tmp."));
688708

689709
const oneHourAgo = Date.now() - 3600000; // 1 hour in ms
690710

691711
for (const tempFile of tempFiles) {
692712
try {
713+
const path = getNodePath();
693714
const fullPath = path.join(this.#stateDir, tempFile);
694715
const stat = fsSync.statSync(fullPath);
695716

rivetkit-typescript/packages/rivetkit/src/drivers/file-system/mod.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { DriverConfig } from "@/registry/run-config";
2+
import { importNodeDependencies } from "@/utils/node";
23
import { FileSystemActorDriver } from "./actor";
34
import { FileSystemGlobalState } from "./global-state";
45
import { FileSystemManagerDriver } from "./manager";
@@ -8,10 +9,13 @@ export { FileSystemGlobalState } from "./global-state";
89
export { FileSystemManagerDriver } from "./manager";
910
export { getStoragePath } from "./utils";
1011

11-
export function createFileSystemOrMemoryDriver(
12+
export async function createFileSystemOrMemoryDriver(
1213
persist: boolean = true,
1314
customPath?: string,
14-
): DriverConfig {
15+
): Promise<DriverConfig> {
16+
// Import Node.js dependencies before creating the state
17+
await importNodeDependencies();
18+
1519
const state = new FileSystemGlobalState(persist, customPath);
1620
const driverConfig: DriverConfig = {
1721
name: persist ? "file-system" : "memory",
@@ -44,10 +48,12 @@ export function createFileSystemOrMemoryDriver(
4448
return driverConfig;
4549
}
4650

47-
export function createFileSystemDriver(opts?: { path?: string }): DriverConfig {
51+
export async function createFileSystemDriver(opts?: {
52+
path?: string;
53+
}): Promise<DriverConfig> {
4854
return createFileSystemOrMemoryDriver(true, opts?.path);
4955
}
5056

51-
export function createMemoryDriver(): DriverConfig {
57+
export async function createMemoryDriver(): Promise<DriverConfig> {
5258
return createFileSystemOrMemoryDriver(false);
5359
}

0 commit comments

Comments
 (0)