Skip to content
This repository was archived by the owner on Oct 22, 2025. It is now read-only.

Commit 9264aaf

Browse files
committed
feat: serverless
1 parent 49ed6fb commit 9264aaf

File tree

7 files changed

+127
-39
lines changed

7 files changed

+127
-39
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
import { toNextHandler } from "@rivetkit/next-js";
22
import { registry } from "@/rivet/registry";
33

4-
export const { GET, POST, HEAD, PATCH, OPTIONS } = toNextHandler(registry);
4+
export const { GET, POST, PUT, PATCH, HEAD, OPTIONS } = toNextHandler(registry);

packages/next-js/src/mod.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,22 @@ export const toNextHandler = (
99

1010
const { fetch } = registry.start(inputConfig);
1111

12+
const fetchWrapper = async (request: Request, { params }: { params: Promise<{ all: string[] }> }) => {
13+
const { all } = await params;
14+
15+
const newUrl = new URL(request.url);
16+
newUrl.pathname = all.join('/');
17+
const newReq = new Request(newUrl, request);
18+
19+
return await fetch(newReq);
20+
};
21+
1222
return {
13-
GET: fetch,
14-
POST: fetch,
15-
PATCH: fetch,
16-
HEAD: fetch,
17-
OPTIONS: fetch,
23+
GET: fetchWrapper,
24+
POST: fetchWrapper,
25+
PUT: fetchWrapper,
26+
PATCH: fetchWrapper,
27+
HEAD: fetchWrapper,
28+
OPTIONS: fetchWrapper,
1829
};
1930
};

packages/rivetkit/src/common/versioned-data.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export interface VersionedDataConfig<T> {
1313
}
1414

1515
export class VersionedDataHandler<T> {
16-
constructor(private config: VersionedDataConfig<T>) {}
16+
constructor(private config: VersionedDataConfig<T>) { }
1717

1818
serializeWithEmbeddedVersion(data: T): Uint8Array {
1919
const versioned: VersionedData<Uint8Array> = {
@@ -63,8 +63,8 @@ export class VersionedDataHandler<T> {
6363
}
6464

6565
private embedVersion(data: VersionedData<Uint8Array>): Uint8Array {
66-
const versionBytes = new Uint8Array(4);
67-
new DataView(versionBytes.buffer).setUint32(0, data.version, true);
66+
const versionBytes = new Uint8Array(2);
67+
new DataView(versionBytes.buffer).setUint16(0, data.version, true);
6868

6969
const result = new Uint8Array(versionBytes.length + data.data.length);
7070
result.set(versionBytes);
@@ -74,15 +74,15 @@ export class VersionedDataHandler<T> {
7474
}
7575

7676
private extractVersion(bytes: Uint8Array): VersionedData<Uint8Array> {
77-
if (bytes.length < 4) {
77+
if (bytes.length < 2) {
7878
throw new Error("Invalid versioned data: too short");
7979
}
8080

81-
const version = new DataView(bytes.buffer, bytes.byteOffset).getUint32(
81+
const version = new DataView(bytes.buffer, bytes.byteOffset).getUint16(
8282
0,
8383
true,
8484
);
85-
const data = bytes.slice(4);
85+
const data = bytes.slice(2);
8686

8787
return { version, data };
8888
}

packages/rivetkit/src/manager/router.ts

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ function buildOpenApiResponses<T>(schema: T, validateBody: boolean) {
5454
description: "Success",
5555
content: validateBody
5656
? {
57-
"application/json": {
58-
schema,
59-
},
60-
}
57+
"application/json": {
58+
schema,
59+
},
60+
}
6161
: {},
6262
},
6363
400: {
@@ -151,6 +151,7 @@ export function createManagerRouter(
151151
method: c.req.raw.method,
152152
headers: proxyHeaders,
153153
body: c.req.raw.body,
154+
duplex: "half",
154155
signal: c.req.raw.signal,
155156
});
156157

@@ -211,10 +212,10 @@ export function createManagerRouter(
211212
body: {
212213
content: validateBody
213214
? {
214-
"application/json": {
215-
schema: ActorsGetOrCreateByIdRequestSchema,
216-
},
217-
}
215+
"application/json": {
216+
schema: ActorsGetOrCreateByIdRequestSchema,
217+
},
218+
}
218219
: {},
219220
},
220221
},
@@ -323,10 +324,10 @@ export function createManagerRouter(
323324
body: {
324325
content: validateBody
325326
? {
326-
"application/json": {
327-
schema: ActorsCreateRequestSchema,
328-
},
329-
}
327+
"application/json": {
328+
schema: ActorsCreateRequestSchema,
329+
},
330+
}
330331
: {},
331332
},
332333
},
@@ -500,6 +501,7 @@ export function createManagerRouter(
500501
method: c.req.method,
501502
headers: c.req.raw.headers,
502503
body: c.req.raw.body,
504+
duplex: "half",
503505
}),
504506
);
505507

@@ -598,9 +600,9 @@ async function createTestWebSocketProxy(
598600
onOpen: (_evt, serverWs) => {
599601
serverWs.close(1011, "Failed to establish connection");
600602
},
601-
onMessage: () => {},
602-
onError: () => {},
603-
onClose: () => {},
603+
onMessage: () => { },
604+
onError: () => { },
605+
onClose: () => { },
604606
};
605607
}
606608

packages/rivetkit/src/remote-manager-driver/actor-http-client.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ export async function sendHttpRequestToActor(
1717
const guardHeaders = buildGuardHeadersForHttp(actorRequest, actorId);
1818

1919
if (
20-
actorRequest.body &&
2120
actorRequest.method !== "GET" &&
2221
actorRequest.method !== "HEAD"
2322
) {
@@ -27,17 +26,19 @@ export async function sendHttpRequestToActor(
2726

2827
// TODO: This buffers the entire request in memory every time. We
2928
// need to properly implement streaming bodies.
30-
// Clone and read the body to ensure it can be sent
31-
const clonedRequest = actorRequest.clone();
32-
bodyToSend = await clonedRequest.arrayBuffer();
29+
const reqBody = await actorRequest.arrayBuffer();
3330

34-
// If this is a streaming request, we need to convert the headers
35-
// for the basic array buffer
36-
guardHeaders.delete("transfer-encoding");
37-
guardHeaders.set(
38-
"content-length",
39-
String((bodyToSend as ArrayBuffer).byteLength),
40-
);
31+
if (reqBody.byteLength != 0) {
32+
bodyToSend = reqBody;
33+
34+
// If this is a streaming request, we need to convert the headers
35+
// for the basic array buffer
36+
guardHeaders.delete("transfer-encoding");
37+
guardHeaders.set(
38+
"content-length",
39+
String(bodyToSend.byteLength),
40+
);
41+
}
4142
}
4243

4344
const guardRequest = new Request(guardUrl, {

packages/rivetkit/src/remote-manager-driver/mod.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ export class RemoteManagerDriver implements ManagerDriver {
149149
name,
150150
key: serializeActorKey(key),
151151
runner_name_selector: this.#config.runnerName,
152-
input: input ? cbor.encode(actorInput).toString("base64") : undefined,
152+
input: actorInput ? cbor.encode(actorInput).toString("base64") : undefined,
153153
crash_policy: "sleep",
154154
});
155155

scripts/nuke-cache.sh

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#!/bin/bash
2+
3+
# Clear JavaScript-related cache folders recursively
4+
# Usage: ./clear_js_cache.sh [directory]
5+
6+
# Set the target directory (default to current directory)
7+
TARGET_DIR="${1:-.}"
8+
9+
# Colors for output
10+
RED='\033[0;31m'
11+
GREEN='\033[0;32m'
12+
YELLOW='\033[1;33m'
13+
NC='\033[0m' # No Color
14+
15+
echo -e "${YELLOW}Clearing JavaScript cache folders in: $TARGET_DIR${NC}"
16+
17+
# Counter for removed folders
18+
removed_count=0
19+
20+
# Function to safely remove directory
21+
remove_dir() {
22+
local dir="$1"
23+
if [ -d "$dir" ]; then
24+
echo -e "Removing: ${RED}$dir${NC}"
25+
rm -rf "$dir"
26+
((removed_count++))
27+
fi
28+
}
29+
30+
# Find and remove cache folders
31+
while IFS= read -r -d '' dir; do
32+
remove_dir "$dir"
33+
done < <(find "$TARGET_DIR" -type d \( \
34+
-name "node_modules" -o \
35+
-name ".next" -o \
36+
-name ".nuxt" -o \
37+
-name "dist" -o \
38+
-name "build" -o \
39+
-name ".cache" -o \
40+
-name ".parcel-cache" -o \
41+
-name ".webpack" -o \
42+
-name ".rollup.cache" -o \
43+
-name ".vite" -o \
44+
-name ".turbo" -o \
45+
-name ".nx" -o \
46+
-name "coverage" -o \
47+
-name ".nyc_output" -o \
48+
-name "out" -o \
49+
-name ".output" -o \
50+
-name ".vercel" -o \
51+
-name ".netlify" -o \
52+
-name "storybook-static" \
53+
\) -print0)
54+
55+
# Also remove common cache files
56+
echo -e "${YELLOW}Removing cache files...${NC}"
57+
find "$TARGET_DIR" -type f \( \
58+
-name "*.log" -o \
59+
-name ".DS_Store" -o \
60+
-name "Thumbs.db" -o \
61+
-name "*.tmp" -o \
62+
-name "*.temp" \
63+
\) -delete 2>/dev/null
64+
65+
# Remove package-lock.json and yarn.lock (optional - uncomment if needed)
66+
# find "$TARGET_DIR" -name "package-lock.json" -delete 2>/dev/null
67+
# find "$TARGET_DIR" -name "yarn.lock" -delete 2>/dev/null
68+
69+
echo -e "${GREEN}✓ Cleanup complete! Removed $removed_count cache folders.${NC}"
70+
71+
# Optional: Show disk space freed (requires du command)
72+
if command -v du >/dev/null 2>&1; then
73+
echo -e "${YELLOW}Disk space check completed.${NC}"
74+
fi

0 commit comments

Comments
 (0)