@rivetkit/engine-test-runner 2.0.21 → 2.2.1-pr.4600.0c0c39c
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +14 -14
- package/dist/index.js +72 -43
- package/dist/index.js.map +1 -1
- package/package.json +26 -26
- package/src/index.ts +93 -40
- package/src/log.ts +5 -19
- package/.turbo/turbo-check-types.log +0 -4
- package/Dockerfile +0 -26
- package/LICENSE +0 -203
package/.turbo/turbo-build.log
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
|
|
2
|
-
> @rivetkit/engine-test-runner@2.
|
|
2
|
+
> @rivetkit/engine-test-runner@2.2.1 build /home/runner/work/rivet/rivet/engine/sdks/typescript/test-runner
|
|
3
3
|
> tsup src/index.ts
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
5
|
+
[34mCLI[39m Building entry: src/index.ts
|
|
6
|
+
[34mCLI[39m Using tsconfig: tsconfig.json
|
|
7
|
+
[34mCLI[39m tsup v8.5.1
|
|
8
|
+
[34mCLI[39m Using tsup config: /home/runner/work/rivet/rivet/engine/sdks/typescript/test-runner/tsup.config.ts
|
|
9
|
+
[34mCLI[39m Target: node16
|
|
10
|
+
[34mCLI[39m Cleaning output folder
|
|
11
|
+
[34mESM[39m Build start
|
|
12
|
+
[32mESM[39m [1mdist/index.js [22m[32m10.71 KB[39m
|
|
13
|
+
[32mESM[39m [1mdist/index.js.map [22m[32m20.96 KB[39m
|
|
14
|
+
[32mESM[39m ⚡️ Build success in 8ms
|
|
15
|
+
[34mDTS[39m Build start
|
|
16
|
+
[32mDTS[39m ⚡️ Build success in 730ms
|
|
17
|
+
[32mDTS[39m [1mdist/index.d.ts [22m[32m175.00 B[39m
|
package/dist/index.js
CHANGED
|
@@ -5,21 +5,14 @@ import { Hono } from "hono";
|
|
|
5
5
|
import { streamSSE } from "hono/streaming";
|
|
6
6
|
|
|
7
7
|
// src/log.ts
|
|
8
|
+
import { inspect } from "util";
|
|
8
9
|
import {
|
|
9
10
|
pino,
|
|
10
11
|
stdTimeFunctions
|
|
11
12
|
} from "pino";
|
|
12
|
-
import { inspect } from "util";
|
|
13
13
|
var baseLogger;
|
|
14
|
-
var configuredLogLevel;
|
|
15
14
|
var loggerCache = /* @__PURE__ */ new Map();
|
|
16
|
-
function getPinoLevel(
|
|
17
|
-
if (logLevel) {
|
|
18
|
-
return logLevel;
|
|
19
|
-
}
|
|
20
|
-
if (configuredLogLevel) {
|
|
21
|
-
return configuredLogLevel;
|
|
22
|
-
}
|
|
15
|
+
function getPinoLevel() {
|
|
23
16
|
return (process.env["LOG_LEVEL"] || "warn").toString().toLowerCase();
|
|
24
17
|
}
|
|
25
18
|
function getIncludeTarget() {
|
|
@@ -50,12 +43,9 @@ function customWrite(level, o) {
|
|
|
50
43
|
});
|
|
51
44
|
console.log(output);
|
|
52
45
|
}
|
|
53
|
-
async function configureDefaultLogger(
|
|
54
|
-
if (logLevel) {
|
|
55
|
-
configuredLogLevel = logLevel;
|
|
56
|
-
}
|
|
46
|
+
async function configureDefaultLogger() {
|
|
57
47
|
baseLogger = pino({
|
|
58
|
-
level: getPinoLevel(
|
|
48
|
+
level: getPinoLevel(),
|
|
59
49
|
messageKey: "msg",
|
|
60
50
|
// Do not include pid/hostname in output
|
|
61
51
|
base: {},
|
|
@@ -129,17 +119,17 @@ function getLogger(name = "default") {
|
|
|
129
119
|
var INTERNAL_SERVER_PORT = process.env.INTERNAL_SERVER_PORT ? Number(process.env.INTERNAL_SERVER_PORT) : 5051;
|
|
130
120
|
var RIVET_NAMESPACE = process.env.RIVET_NAMESPACE ?? "default";
|
|
131
121
|
var RIVET_RUNNER_NAME = process.env.RIVET_RUNNER_NAME ?? "test-runner";
|
|
132
|
-
var RIVET_RUNNER_KEY = process.env.RIVET_RUNNER_KEY ?? `key-${Math.floor(Math.random() * 1e4)}`;
|
|
133
122
|
var RIVET_RUNNER_VERSION = process.env.RIVET_RUNNER_VERSION ? Number(process.env.RIVET_RUNNER_VERSION) : 1;
|
|
134
|
-
var RIVET_RUNNER_TOTAL_SLOTS = process.env.RIVET_RUNNER_TOTAL_SLOTS
|
|
123
|
+
var RIVET_RUNNER_TOTAL_SLOTS = parseInt(process.env.RIVET_RUNNER_TOTAL_SLOTS ?? "1");
|
|
135
124
|
var RIVET_ENDPOINT = process.env.RIVET_ENDPOINT ?? "http://127.0.0.1:6420";
|
|
136
125
|
var RIVET_TOKEN = process.env.RIVET_TOKEN ?? "dev";
|
|
137
|
-
var AUTOSTART_SERVER = process.env.
|
|
138
|
-
var AUTOSTART_RUNNER = process.env.
|
|
126
|
+
var AUTOSTART_SERVER = (process.env.AUTOSTART_SERVER ?? "1") == "1";
|
|
127
|
+
var AUTOSTART_RUNNER = (process.env.AUTOSTART_RUNNER ?? "0") == "1";
|
|
128
|
+
var AUTOCONFIGURE_SERVERLESS = (process.env.AUTOCONFIGURE_SERVERLESS ?? "1") == "1";
|
|
139
129
|
var runnerStarted = Promise.withResolvers();
|
|
140
130
|
var runnerStopped = Promise.withResolvers();
|
|
141
131
|
var runner = null;
|
|
142
|
-
var
|
|
132
|
+
var websocketLastMsgIndexes = /* @__PURE__ */ new Map();
|
|
143
133
|
var app = new Hono();
|
|
144
134
|
function loggerMiddleware(logger) {
|
|
145
135
|
return async (c, next) => {
|
|
@@ -162,8 +152,8 @@ function loggerMiddleware(logger) {
|
|
|
162
152
|
}
|
|
163
153
|
app.use("*", loggerMiddleware(getLogger()));
|
|
164
154
|
app.get("/wait-ready", async (c) => {
|
|
165
|
-
await runnerStarted.promise;
|
|
166
|
-
return c.json(
|
|
155
|
+
const runner2 = await runnerStarted.promise;
|
|
156
|
+
return c.json(runner2.runnerId);
|
|
167
157
|
});
|
|
168
158
|
app.get("/has-actor", async (c) => {
|
|
169
159
|
const actorIdQuery = c.req.query("actor");
|
|
@@ -174,13 +164,18 @@ app.get("/has-actor", async (c) => {
|
|
|
174
164
|
}
|
|
175
165
|
return c.text("ok");
|
|
176
166
|
});
|
|
167
|
+
app.get("/health", (c) => {
|
|
168
|
+
return c.text("ok");
|
|
169
|
+
});
|
|
177
170
|
app.get("/shutdown", async (c) => {
|
|
178
171
|
await (runner == null ? void 0 : runner.shutdown(true));
|
|
179
172
|
return c.text("ok");
|
|
180
173
|
});
|
|
181
|
-
app.get("/start", async (c) => {
|
|
174
|
+
app.get("/api/rivet/start", async (c) => {
|
|
182
175
|
return streamSSE(c, async (stream) => {
|
|
183
|
-
const
|
|
176
|
+
const runnerStarted2 = Promise.withResolvers();
|
|
177
|
+
const runnerStopped2 = Promise.withResolvers();
|
|
178
|
+
const runner2 = await startRunner(runnerStarted2, runnerStopped2);
|
|
184
179
|
c.req.raw.signal.addEventListener("abort", () => {
|
|
185
180
|
getLogger().debug("SSE aborted, shutting down runner");
|
|
186
181
|
runner2.shutdown(true);
|
|
@@ -190,7 +185,13 @@ app.get("/start", async (c) => {
|
|
|
190
185
|
await runnerStopped2.promise;
|
|
191
186
|
});
|
|
192
187
|
});
|
|
193
|
-
|
|
188
|
+
app.get("/api/rivet/metadata", async (c) => {
|
|
189
|
+
return c.json({
|
|
190
|
+
// Not actually rivetkit
|
|
191
|
+
runtime: "rivetkit",
|
|
192
|
+
version: "1"
|
|
193
|
+
});
|
|
194
|
+
});
|
|
194
195
|
if (AUTOSTART_SERVER) {
|
|
195
196
|
serve({
|
|
196
197
|
fetch: app.fetch,
|
|
@@ -200,11 +201,25 @@ if (AUTOSTART_SERVER) {
|
|
|
200
201
|
`Internal HTTP server listening on port ${INTERNAL_SERVER_PORT}`
|
|
201
202
|
);
|
|
202
203
|
}
|
|
203
|
-
if (AUTOSTART_RUNNER)
|
|
204
|
-
|
|
204
|
+
if (AUTOSTART_RUNNER) {
|
|
205
|
+
runner = await startRunner(runnerStarted, runnerStopped);
|
|
206
|
+
} else if (AUTOCONFIGURE_SERVERLESS) {
|
|
207
|
+
await autoConfigureServerless();
|
|
208
|
+
}
|
|
209
|
+
process.on("SIGTERM", async () => {
|
|
210
|
+
getLogger().debug("received SIGTERM, force exiting in 3s");
|
|
211
|
+
await new Promise((res) => setTimeout(res, 3e3));
|
|
212
|
+
process.exit(0);
|
|
213
|
+
});
|
|
214
|
+
process.on("SIGINT", async () => {
|
|
215
|
+
getLogger().debug("received SIGTERM, force exiting in 3s");
|
|
216
|
+
await new Promise((res) => setTimeout(res, 3e3));
|
|
217
|
+
process.exit(0);
|
|
218
|
+
});
|
|
205
219
|
async function autoConfigureServerless() {
|
|
220
|
+
getLogger().info("Configuring serverless");
|
|
206
221
|
const res = await fetch(
|
|
207
|
-
|
|
222
|
+
`${RIVET_ENDPOINT}/runner-configs/${RIVET_RUNNER_NAME}?namespace=${RIVET_NAMESPACE}`,
|
|
208
223
|
{
|
|
209
224
|
method: "PUT",
|
|
210
225
|
headers: {
|
|
@@ -215,10 +230,10 @@ async function autoConfigureServerless() {
|
|
|
215
230
|
datacenters: {
|
|
216
231
|
default: {
|
|
217
232
|
serverless: {
|
|
218
|
-
url: `http://localhost:${INTERNAL_SERVER_PORT}`,
|
|
219
|
-
max_runners:
|
|
233
|
+
url: `http://localhost:${INTERNAL_SERVER_PORT}/api/rivet`,
|
|
234
|
+
max_runners: 1e4,
|
|
220
235
|
slots_per_runner: 1,
|
|
221
|
-
request_lifespan:
|
|
236
|
+
request_lifespan: 300
|
|
222
237
|
}
|
|
223
238
|
}
|
|
224
239
|
}
|
|
@@ -232,10 +247,9 @@ ${await res.text()}`
|
|
|
232
247
|
);
|
|
233
248
|
}
|
|
234
249
|
}
|
|
235
|
-
async function startRunner() {
|
|
250
|
+
async function startRunner(runnerStarted2, runnerStopped2) {
|
|
236
251
|
getLogger().info("Starting runner");
|
|
237
|
-
|
|
238
|
-
const runnerStopped2 = Promise.withResolvers();
|
|
252
|
+
let runner2;
|
|
239
253
|
const config = {
|
|
240
254
|
logger: getLogger(),
|
|
241
255
|
version: RIVET_RUNNER_VERSION,
|
|
@@ -243,18 +257,17 @@ async function startRunner() {
|
|
|
243
257
|
token: RIVET_TOKEN,
|
|
244
258
|
namespace: RIVET_NAMESPACE,
|
|
245
259
|
runnerName: RIVET_RUNNER_NAME,
|
|
246
|
-
runnerKey: RIVET_RUNNER_KEY,
|
|
247
260
|
totalSlots: RIVET_RUNNER_TOTAL_SLOTS,
|
|
248
261
|
prepopulateActorNames: {},
|
|
249
262
|
onConnected: () => {
|
|
250
|
-
runnerStarted2.resolve(
|
|
263
|
+
runnerStarted2.resolve(runner2);
|
|
251
264
|
},
|
|
252
265
|
onDisconnected: () => {
|
|
253
266
|
},
|
|
254
267
|
onShutdown: () => {
|
|
255
|
-
runnerStopped2.resolve(
|
|
268
|
+
runnerStopped2.resolve(runner2);
|
|
256
269
|
},
|
|
257
|
-
fetch: async (runner3, actorId, request) => {
|
|
270
|
+
fetch: async (runner3, actorId, _gatewayId, _requestId, request) => {
|
|
258
271
|
getLogger().info(
|
|
259
272
|
`Fetch called for actor ${actorId}, URL: ${request.url}`
|
|
260
273
|
);
|
|
@@ -288,20 +301,31 @@ async function startRunner() {
|
|
|
288
301
|
`Actor ${_actorId} stopped (generation ${_generation})`
|
|
289
302
|
);
|
|
290
303
|
},
|
|
291
|
-
websocket: async (
|
|
304
|
+
websocket: async (runner3, actorId, ws, _gatewayId, _requestId, _request) => {
|
|
292
305
|
getLogger().info(`WebSocket connected for actor ${actorId}`);
|
|
293
|
-
actorWebSockets.set(actorId, ws);
|
|
294
306
|
ws.addEventListener("message", (event) => {
|
|
295
307
|
const data = event.data;
|
|
296
308
|
getLogger().info({
|
|
297
309
|
msg: `WebSocket message from actor ${actorId}`,
|
|
298
|
-
data
|
|
310
|
+
data,
|
|
311
|
+
index: event.rivetMessageIndex
|
|
299
312
|
});
|
|
300
313
|
ws.send(`Echo: ${data}`);
|
|
314
|
+
const websocketId = Buffer.from(
|
|
315
|
+
event.rivetRequestId
|
|
316
|
+
).toString("base64");
|
|
317
|
+
websocketLastMsgIndexes.set(
|
|
318
|
+
websocketId,
|
|
319
|
+
event.rivetMessageIndex
|
|
320
|
+
);
|
|
321
|
+
runner3.sendHibernatableWebSocketMessageAck(
|
|
322
|
+
event.rivetGatewayId,
|
|
323
|
+
event.rivetRequestId,
|
|
324
|
+
event.rivetMessageIndex
|
|
325
|
+
);
|
|
301
326
|
});
|
|
302
327
|
ws.addEventListener("close", () => {
|
|
303
328
|
getLogger().info(`WebSocket closed for actor ${actorId}`);
|
|
304
|
-
actorWebSockets.delete(actorId);
|
|
305
329
|
});
|
|
306
330
|
ws.addEventListener("error", (error) => {
|
|
307
331
|
getLogger().error({
|
|
@@ -309,14 +333,19 @@ async function startRunner() {
|
|
|
309
333
|
error
|
|
310
334
|
});
|
|
311
335
|
});
|
|
336
|
+
},
|
|
337
|
+
hibernatableWebSocket: {
|
|
338
|
+
canHibernate() {
|
|
339
|
+
return true;
|
|
340
|
+
}
|
|
312
341
|
}
|
|
313
342
|
};
|
|
314
|
-
|
|
343
|
+
runner2 = new Runner(config);
|
|
315
344
|
await runner2.start();
|
|
316
345
|
getLogger().info("Waiting runner start...");
|
|
317
346
|
await runnerStarted2.promise;
|
|
318
347
|
getLogger().info("Runner started");
|
|
319
|
-
return
|
|
348
|
+
return runner2;
|
|
320
349
|
}
|
|
321
350
|
var index_default = app;
|
|
322
351
|
export {
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/log.ts"],"sourcesContent":["import { serve } from \"@hono/node-server\";\nimport type { ActorConfig, RunnerConfig } from \"@rivetkit/engine-runner\";\nimport { Runner } from \"@rivetkit/engine-runner\";\nimport { Hono, type Context as HonoContext, type Next } from \"hono\";\nimport { streamSSE } from \"hono/streaming\";\nimport type { Logger } from \"pino\";\nimport type WebSocket from \"ws\";\nimport { getLogger } from \"./log\";\n\nconst INTERNAL_SERVER_PORT = process.env.INTERNAL_SERVER_PORT\n\t? Number(process.env.INTERNAL_SERVER_PORT)\n\t: 5051;\nconst RIVET_NAMESPACE = process.env.RIVET_NAMESPACE ?? \"default\";\nconst RIVET_RUNNER_NAME = process.env.RIVET_RUNNER_NAME ?? \"test-runner\";\nconst RIVET_RUNNER_KEY =\n\tprocess.env.RIVET_RUNNER_KEY ?? `key-${Math.floor(Math.random() * 10000)}`;\nconst RIVET_RUNNER_VERSION = process.env.RIVET_RUNNER_VERSION\n\t? Number(process.env.RIVET_RUNNER_VERSION)\n\t: 1;\nconst RIVET_RUNNER_TOTAL_SLOTS = process.env.RIVET_RUNNER_TOTAL_SLOTS\n\t? Number(process.env.RIVET_RUNNER_TOTAL_SLOTS)\n\t: 100;\nconst RIVET_ENDPOINT = process.env.RIVET_ENDPOINT ?? \"http://127.0.0.1:6420\";\nconst RIVET_TOKEN = process.env.RIVET_TOKEN ?? \"dev\";\nconst AUTOSTART_SERVER = process.env.NO_AUTOSTART_SERVER === undefined;\nconst AUTOSTART_RUNNER = process.env.NO_AUTOSTART_RUNNER === undefined;\n\nlet runnerStarted = Promise.withResolvers();\nlet runnerStopped = Promise.withResolvers();\nlet runner: Runner | null = null;\nconst actorWebSockets = new Map<string, WebSocket>();\n\n// Create internal server\nconst app = new Hono();\n\nfunction loggerMiddleware(logger: Logger) {\n\treturn async (c: HonoContext, next: Next) => {\n\t\tconst method = c.req.method;\n\t\tconst path = c.req.path;\n\t\tconst startTime = Date.now();\n\n\t\tawait next();\n\n\t\tconst duration = Date.now() - startTime;\n\t\tlogger.debug({\n\t\t\tmsg: \"http request\",\n\t\t\tmethod,\n\t\t\tpath,\n\t\t\tstatus: c.res.status,\n\t\t\tdt: `${duration}ms`,\n\t\t\treqSize: c.req.header(\"content-length\"),\n\t\t\tresSize: c.res.headers.get(\"content-length\"),\n\t\t\tuserAgent: c.req.header(\"user-agent\"),\n\t\t});\n\t};\n}\napp.use(\"*\", loggerMiddleware(getLogger()));\n\napp.get(\"/wait-ready\", async (c) => {\n\tawait runnerStarted.promise;\n\treturn c.json(runner?.runnerId);\n});\n\napp.get(\"/has-actor\", async (c) => {\n\tconst actorIdQuery = c.req.query(\"actor\");\n\tconst generationQuery = c.req.query(\"generation\");\n\tconst generation = generationQuery ? Number(generationQuery) : undefined;\n\n\tif (!actorIdQuery || !runner?.hasActor(actorIdQuery, generation)) {\n\t\treturn c.text(\"\", 404);\n\t}\n\treturn c.text(\"ok\");\n});\n\napp.get(\"/shutdown\", async (c) => {\n\tawait runner?.shutdown(true);\n\treturn c.text(\"ok\");\n});\n\napp.get(\"/start\", async (c) => {\n\treturn streamSSE(c, async (stream) => {\n\t\tconst [runner, runnerStarted, runnerStopped] = await startRunner();\n\n\t\tc.req.raw.signal.addEventListener(\"abort\", () => {\n\t\t\tgetLogger().debug(\"SSE aborted, shutting down runner\");\n\t\t\trunner.shutdown(true);\n\t\t});\n\n\t\tawait runnerStarted.promise;\n\n\t\tstream.writeSSE({ data: runner.getServerlessInitPacket()! });\n\n\t\tawait runnerStopped.promise;\n\t});\n});\n\nawait autoConfigureServerless();\n\nif (AUTOSTART_SERVER) {\n\tserve({\n\t\tfetch: app.fetch,\n\t\tport: INTERNAL_SERVER_PORT,\n\t});\n\tgetLogger().info(\n\t\t`Internal HTTP server listening on port ${INTERNAL_SERVER_PORT}`,\n\t);\n}\n\nif (AUTOSTART_RUNNER)\n\t[runner, runnerStarted, runnerStopped] = await startRunner();\n\nasync function autoConfigureServerless() {\n\tconst res = await fetch(\n\t\t`http://127.0.0.1:6420/runner-configs/${RIVET_RUNNER_NAME}?namespace=${RIVET_NAMESPACE}`,\n\t\t{\n\t\t\tmethod: \"PUT\",\n\t\t\theaders: {\n\t\t\t\tAuthorization: `Bearer ${RIVET_TOKEN}`,\n\t\t\t\t\"Content-Type\": \"application/json\",\n\t\t\t},\n\t\t\tbody: JSON.stringify({\n\t\t\t\tdatacenters: {\n\t\t\t\t\tdefault: {\n\t\t\t\t\t\tserverless: {\n\t\t\t\t\t\t\turl: `http://localhost:${INTERNAL_SERVER_PORT}`,\n\t\t\t\t\t\t\tmax_runners: 10,\n\t\t\t\t\t\t\tslots_per_runner: 1,\n\t\t\t\t\t\t\trequest_lifespan: 15,\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t}),\n\t\t},\n\t);\n\n\tif (!res.ok) {\n\t\tthrow new Error(\n\t\t\t`request failed: ${res.statusText} (${res.status}):\\n${await res.text()}`,\n\t\t);\n\t}\n}\n\nasync function startRunner(): Promise<\n\t[Runner, PromiseWithResolvers<unknown>, PromiseWithResolvers<unknown>]\n> {\n\tgetLogger().info(\"Starting runner\");\n\n\tconst runnerStarted = Promise.withResolvers();\n\tconst runnerStopped = Promise.withResolvers();\n\n\tconst config: RunnerConfig = {\n\t\tlogger: getLogger(),\n\t\tversion: RIVET_RUNNER_VERSION,\n\t\tendpoint: RIVET_ENDPOINT,\n\t\ttoken: RIVET_TOKEN,\n\t\tnamespace: RIVET_NAMESPACE,\n\t\trunnerName: RIVET_RUNNER_NAME,\n\t\trunnerKey: RIVET_RUNNER_KEY,\n\t\ttotalSlots: RIVET_RUNNER_TOTAL_SLOTS,\n\t\tprepopulateActorNames: {},\n\t\tonConnected: () => {\n\t\t\trunnerStarted.resolve(undefined);\n\t\t},\n\t\tonDisconnected: () => {},\n\t\tonShutdown: () => {\n\t\t\trunnerStopped.resolve(undefined);\n\t\t},\n\t\tfetch: async (runner: Runner, actorId: string, request: Request) => {\n\t\t\tgetLogger().info(\n\t\t\t\t`Fetch called for actor ${actorId}, URL: ${request.url}`,\n\t\t\t);\n\t\t\tconst url = new URL(request.url);\n\t\t\tif (url.pathname === \"/ping\") {\n\t\t\t\t// Return the actor ID in response\n\t\t\t\tconst responseData = {\n\t\t\t\t\tactorId,\n\t\t\t\t\tstatus: \"ok\",\n\t\t\t\t\ttimestamp: Date.now(),\n\t\t\t\t};\n\n\t\t\t\treturn new Response(JSON.stringify(responseData), {\n\t\t\t\t\tstatus: 200,\n\t\t\t\t\theaders: { \"Content-Type\": \"application/json\" },\n\t\t\t\t});\n\t\t\t} else if (url.pathname === \"/sleep\") {\n\t\t\t\trunner.sleepActor(actorId);\n\n\t\t\t\treturn new Response(\"ok\", {\n\t\t\t\t\tstatus: 200,\n\t\t\t\t\theaders: { \"Content-Type\": \"application/json\" },\n\t\t\t\t});\n\t\t\t}\n\n\t\t\treturn new Response(\"ok\", { status: 200 });\n\t\t},\n\t\tonActorStart: async (\n\t\t\t_actorId: string,\n\t\t\t_generation: number,\n\t\t\t_config: ActorConfig,\n\t\t) => {\n\t\t\tgetLogger().info(\n\t\t\t\t`Actor ${_actorId} started (generation ${_generation})`,\n\t\t\t);\n\t\t},\n\t\tonActorStop: async (_actorId: string, _generation: number) => {\n\t\t\tgetLogger().info(\n\t\t\t\t`Actor ${_actorId} stopped (generation ${_generation})`,\n\t\t\t);\n\t\t},\n\t\twebsocket: async (\n\t\t\t_runner: Runner,\n\t\t\tactorId: string,\n\t\t\tws: WebSocket,\n\t\t\trequest: Request,\n\t\t) => {\n\t\t\tgetLogger().info(`WebSocket connected for actor ${actorId}`);\n\t\t\tactorWebSockets.set(actorId, ws);\n\n\t\t\t// Echo server - send back any messages received\n\t\t\tws.addEventListener(\"message\", (event) => {\n\t\t\t\tconst data = event.data;\n\t\t\t\tgetLogger().info({\n\t\t\t\t\tmsg: `WebSocket message from actor ${actorId}`,\n\t\t\t\t\tdata,\n\t\t\t\t});\n\t\t\t\tws.send(`Echo: ${data}`);\n\t\t\t});\n\n\t\t\tws.addEventListener(\"close\", () => {\n\t\t\t\tgetLogger().info(`WebSocket closed for actor ${actorId}`);\n\t\t\t\tactorWebSockets.delete(actorId);\n\t\t\t});\n\n\t\t\tws.addEventListener(\"error\", (error) => {\n\t\t\t\tgetLogger().error({\n\t\t\t\t\tmsg: `WebSocket error for actor ${actorId}:`,\n\t\t\t\t\terror,\n\t\t\t\t});\n\t\t\t});\n\t\t},\n\t};\n\n\tconst runner = new Runner(config);\n\n\t// Start runner\n\tawait runner.start();\n\n\t// Wait for runner to be ready\n\tgetLogger().info(\"Waiting runner start...\");\n\tawait runnerStarted.promise;\n\n\tgetLogger().info(\"Runner started\");\n\n\treturn [runner, runnerStarted, runnerStopped];\n}\n\nexport default app;\n","import {\n\ttype Level,\n\ttype LevelWithSilent,\n\ttype Logger,\n\tpino,\n\tstdTimeFunctions,\n} from \"pino\";\nimport { inspect } from \"util\";\n\nexport type { Logger } from \"pino\";\n\nlet baseLogger: Logger | undefined;\nlet configuredLogLevel: Level | undefined;\n\n/** Cache of child loggers by logger name. */\nconst loggerCache = new Map<string, Logger>();\n\nexport function getPinoLevel(logLevel?: Level): LevelWithSilent {\n\t// Priority: provided > configured > env > default\n\tif (logLevel) {\n\t\treturn logLevel;\n\t}\n\n\tif (configuredLogLevel) {\n\t\treturn configuredLogLevel;\n\t}\n\n\treturn (process.env[\"LOG_LEVEL\"] || \"warn\")\n\t\t.toString()\n\t\t.toLowerCase() as LevelWithSilent;\n}\n\nexport function getIncludeTarget(): boolean {\n\treturn process.env[\"LOG_TARGET\"] === \"1\";\n}\n\n/**\n * Configure a custom base logger.\n */\nexport function configureBaseLogger(logger: Logger): void {\n\tbaseLogger = logger;\n\tloggerCache.clear();\n}\n\n// TODO: This can be simplified in logfmt.ts\nfunction customWrite(level: string, o: any) {\n\tconst entries: any = {};\n\n\t// Add timestamp if enabled\n\tif (process.env[\"LOG_TIMESTAMP\"] === \"1\" && o.time) {\n\t\tconst date = typeof o.time === \"number\" ? new Date(o.time) : new Date();\n\t\tentries.ts = date;\n\t}\n\n\t// Add level\n\tentries.level = level.toUpperCase();\n\n\t// Add target if present\n\tif (o.target) {\n\t\tentries.target = o.target;\n\t}\n\n\t// Add message\n\tif (o.msg) {\n\t\tentries.msg = o.msg;\n\t}\n\n\t// Add other properties\n\tfor (const [key, value] of Object.entries(o)) {\n\t\tif (\n\t\t\tkey !== \"time\" &&\n\t\t\tkey !== \"level\" &&\n\t\t\tkey !== \"target\" &&\n\t\t\tkey !== \"msg\" &&\n\t\t\tkey !== \"pid\" &&\n\t\t\tkey !== \"hostname\"\n\t\t) {\n\t\t\tentries[key] = value;\n\t\t}\n\t}\n\n\tconst output = inspect(entries, {\n\t\tcompact: true,\n\t\tbreakLength: Infinity,\n\t\tcolors: true,\n\t});\n\tconsole.log(output);\n}\n\n/**\n * Configure the default logger with optional log level.\n */\nexport async function configureDefaultLogger(logLevel?: Level): Promise<void> {\n\t// Store the configured log level\n\tif (logLevel) {\n\t\tconfiguredLogLevel = logLevel;\n\t}\n\n\tbaseLogger = pino({\n\t\tlevel: getPinoLevel(logLevel),\n\t\tmessageKey: \"msg\",\n\t\t// Do not include pid/hostname in output\n\t\tbase: {},\n\t\t// Keep a string level in the output\n\t\tformatters: {\n\t\t\tlevel(_label: string, number: number) {\n\t\t\t\treturn { level: number };\n\t\t\t},\n\t\t},\n\t\ttimestamp:\n\t\t\tprocess.env[\"LOG_TIMESTAMP\"] === \"1\"\n\t\t\t\t? stdTimeFunctions.epochTime\n\t\t\t\t: false,\n\t\tbrowser: {\n\t\t\twrite: {\n\t\t\t\tfatal: customWrite.bind(null, \"fatal\"),\n\t\t\t\terror: customWrite.bind(null, \"error\"),\n\t\t\t\twarn: customWrite.bind(null, \"warn\"),\n\t\t\t\tinfo: customWrite.bind(null, \"info\"),\n\t\t\t\tdebug: customWrite.bind(null, \"debug\"),\n\t\t\t\ttrace: customWrite.bind(null, \"trace\"),\n\t\t\t},\n\t\t},\n\t\thooks: {\n\t\t\tlogMethod(inputArgs, _method, level) {\n\t\t\t\t// TODO: This is a hack to not implement our own transport target. We can get better perf if we have our own transport target.\n\n\t\t\t\tconst levelMap: Record<number, string> = {\n\t\t\t\t\t10: \"trace\",\n\t\t\t\t\t20: \"debug\",\n\t\t\t\t\t30: \"info\",\n\t\t\t\t\t40: \"warn\",\n\t\t\t\t\t50: \"error\",\n\t\t\t\t\t60: \"fatal\",\n\t\t\t\t};\n\t\t\t\tconst levelName = levelMap[level] || \"info\";\n\t\t\t\tconst time =\n\t\t\t\t\tprocess.env[\"LOG_TIMESTAMP\"] === \"1\"\n\t\t\t\t\t\t? Date.now()\n\t\t\t\t\t\t: undefined;\n\t\t\t\t// TODO: This can be simplified in logfmt.ts\n\t\t\t\tif (inputArgs.length >= 2) {\n\t\t\t\t\tconst [objOrMsg, msg] = inputArgs;\n\t\t\t\t\tif (typeof objOrMsg === \"object\" && objOrMsg !== null) {\n\t\t\t\t\t\tcustomWrite(levelName, { ...objOrMsg, msg, time });\n\t\t\t\t\t} else {\n\t\t\t\t\t\tcustomWrite(levelName, { msg: String(objOrMsg), time });\n\t\t\t\t\t}\n\t\t\t\t} else if (inputArgs.length === 1) {\n\t\t\t\t\tconst [objOrMsg] = inputArgs;\n\t\t\t\t\tif (typeof objOrMsg === \"object\" && objOrMsg !== null) {\n\t\t\t\t\t\tcustomWrite(levelName, { ...objOrMsg, time });\n\t\t\t\t\t} else {\n\t\t\t\t\t\tcustomWrite(levelName, { msg: String(objOrMsg), time });\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t},\n\t\t},\n\t});\n\n\tloggerCache.clear();\n}\n\n/**\n * Get or initialize the base logger.\n */\nexport function getBaseLogger(): Logger {\n\tif (!baseLogger) {\n\t\tconfigureDefaultLogger();\n\t}\n\treturn baseLogger!;\n}\n\n/**\n * Returns a child logger with `target` bound for the given name.\n */\nexport function getLogger(name = \"default\"): Logger {\n\t// Check cache first\n\tconst cached = loggerCache.get(name);\n\tif (cached) {\n\t\treturn cached;\n\t}\n\n\t// Create\n\tconst base = getBaseLogger();\n\n\t// Add target to log if enabled\n\tconst child = getIncludeTarget() ? base.child({ target: name }) : base;\n\n\t// Cache the logger\n\tloggerCache.set(name, child);\n\n\treturn child;\n}\n"],"mappings":";AAAA,SAAS,aAAa;AAEtB,SAAS,cAAc;AACvB,SAAS,YAAoD;AAC7D,SAAS,iBAAiB;;;ACJ1B;AAAA,E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U,EAAE,KAAK,iCAAiC,OAAO,EAAE;AAC3D,sBAAgB,IAAI,SAAS,EAAE;AAG/B,SAAG,iBAAiB,WAAW,CAAC,UAAU;AACzC,cAAM,OAAO,MAAM;AACnB,kBAAU,EAAE,KAAK;AAAA,UAChB,KAAK,gCAAgC,OAAO;AAAA,UAC5C;AAAA,QACD,CAAC;AACD,WAAG,KAAK,SAAS,IAAI,EAAE;AAAA,MACxB,CAAC;AAED,SAAG,iBAAiB,SAAS,MAAM;AAClC,kBAAU,EAAE,KAAK,8BAA8B,OAAO,EAAE;AACxD,wBAAgB,OAAO,OAAO;AAAA,MAC/B,CAAC;AAED,SAAG,iBAAiB,SAAS,CAAC,UAAU;AACvC,kBAAU,EAAE,MAAM;AAAA,UACjB,KAAK,6BAA6B,OAAO;AAAA,UACzC;AAAA,QACD,CAAC;AAAA,MACF,CAAC;AAAA,IACF;AAAA,EACD;AAEA,QAAMA,UAAS,IAAI,OAAO,MAAM;AAGhC,QAAMA,QAAO,MAAM;AAGnB,YAAU,EAAE,KAAK,yBAAyB;AAC1C,QAAMC,eAAc;AAEpB,YAAU,EAAE,KAAK,gBAAgB;AAEjC,SAAO,CAACD,SAAQC,gBAAeC,cAAa;AAC7C;AAEA,IAAO,gBAAQ;","names":["runner","runnerStarted","runnerStopped"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/log.ts"],"sourcesContent":["import { serve } from \"@hono/node-server\";\nimport type { ActorConfig, RunnerConfig } from \"@rivetkit/engine-runner\";\nimport { Runner } from \"@rivetkit/engine-runner\";\nimport { Hono, type Context as HonoContext, type Next } from \"hono\";\nimport { streamSSE } from \"hono/streaming\";\nimport type { Logger } from \"pino\";\nimport type WebSocket from \"ws\";\nimport { getLogger } from \"./log\";\n\nconst INTERNAL_SERVER_PORT = process.env.INTERNAL_SERVER_PORT\n\t? Number(process.env.INTERNAL_SERVER_PORT)\n\t: 5051;\nconst RIVET_NAMESPACE = process.env.RIVET_NAMESPACE ?? \"default\";\nconst RIVET_RUNNER_NAME = process.env.RIVET_RUNNER_NAME ?? \"test-runner\";\nconst RIVET_RUNNER_VERSION = process.env.RIVET_RUNNER_VERSION\n\t? Number(process.env.RIVET_RUNNER_VERSION)\n\t: 1;\nconst RIVET_RUNNER_TOTAL_SLOTS = parseInt(process.env.RIVET_RUNNER_TOTAL_SLOTS ?? \"1\");\nconst RIVET_ENDPOINT = process.env.RIVET_ENDPOINT ?? \"http://127.0.0.1:6420\";\nconst RIVET_TOKEN = process.env.RIVET_TOKEN ?? \"dev\";\nconst AUTOSTART_SERVER = (process.env.AUTOSTART_SERVER ?? \"1\") == \"1\";\nconst AUTOSTART_RUNNER = (process.env.AUTOSTART_RUNNER ?? \"0\") == \"1\";\nconst AUTOCONFIGURE_SERVERLESS = (process.env.AUTOCONFIGURE_SERVERLESS ?? \"1\") == \"1\";\n\nconst runnerStarted = Promise.withResolvers<Runner>();\nconst runnerStopped = Promise.withResolvers<Runner>();\nlet runner: Runner | null = null;\nconst websocketLastMsgIndexes: Map<string, number> = new Map();\n\n// Create internal server\nconst app = new Hono();\n\nfunction loggerMiddleware(logger: Logger) {\n\treturn async (c: HonoContext, next: Next) => {\n\t\tconst method = c.req.method;\n\t\tconst path = c.req.path;\n\t\tconst startTime = Date.now();\n\n\t\tawait next();\n\n\t\tconst duration = Date.now() - startTime;\n\t\tlogger.debug({\n\t\t\tmsg: \"http request\",\n\t\t\tmethod,\n\t\t\tpath,\n\t\t\tstatus: c.res.status,\n\t\t\tdt: `${duration}ms`,\n\t\t\treqSize: c.req.header(\"content-length\"),\n\t\t\tresSize: c.res.headers.get(\"content-length\"),\n\t\t\tuserAgent: c.req.header(\"user-agent\"),\n\t\t});\n\t};\n}\napp.use(\"*\", loggerMiddleware(getLogger()));\n\napp.get(\"/wait-ready\", async (c) => {\n\tconst runner = await runnerStarted.promise;\n\treturn c.json(runner.runnerId);\n});\n\napp.get(\"/has-actor\", async (c) => {\n\tconst actorIdQuery = c.req.query(\"actor\");\n\tconst generationQuery = c.req.query(\"generation\");\n\tconst generation = generationQuery ? Number(generationQuery) : undefined;\n\n\tif (!actorIdQuery || !runner?.hasActor(actorIdQuery, generation)) {\n\t\treturn c.text(\"\", 404);\n\t}\n\treturn c.text(\"ok\");\n});\n\napp.get(\"/health\", (c) => {\n\treturn c.text(\"ok\");\n});\n\napp.get(\"/shutdown\", async (c) => {\n\tawait runner?.shutdown(true);\n\treturn c.text(\"ok\");\n});\n\napp.get(\"/api/rivet/start\", async (c) => {\n\treturn streamSSE(c, async (stream) => {\n\t\tconst runnerStarted = Promise.withResolvers<Runner>();\n\t\tconst runnerStopped = Promise.withResolvers<Runner>();\n\t\tconst runner = await startRunner(runnerStarted, runnerStopped);\n\n\t\tc.req.raw.signal.addEventListener(\"abort\", () => {\n\t\t\tgetLogger().debug(\"SSE aborted, shutting down runner\");\n\t\t\trunner!.shutdown(true);\n\t\t});\n\n\t\tawait runnerStarted.promise;\n\n\t\tstream.writeSSE({ data: runner.getServerlessInitPacket()! });\n\n\t\tawait runnerStopped.promise;\n\t});\n});\n\napp.get(\"/api/rivet/metadata\", async (c) => {\n\treturn c.json({\n\t\t// Not actually rivetkit\n\t\truntime: \"rivetkit\",\n\t\tversion: \"1\",\n\t});\n});\n\nif (AUTOSTART_SERVER) {\n\tserve({\n\t\tfetch: app.fetch,\n\t\tport: INTERNAL_SERVER_PORT,\n\t});\n\tgetLogger().info(\n\t\t`Internal HTTP server listening on port ${INTERNAL_SERVER_PORT}`,\n\t);\n}\n\nif (AUTOSTART_RUNNER) {\n\trunner = await startRunner(runnerStarted, runnerStopped);\n} else if (AUTOCONFIGURE_SERVERLESS) {\n\tawait autoConfigureServerless();\n}\n\nprocess.on(\"SIGTERM\", async () => {\n\tgetLogger().debug(\"received SIGTERM, force exiting in 3s\");\n\n\tawait new Promise(res => setTimeout(res, 3000));\n\n\tprocess.exit(0);\n});\nprocess.on(\"SIGINT\", async () => {\n\tgetLogger().debug(\"received SIGTERM, force exiting in 3s\");\n\n\tawait new Promise(res => setTimeout(res, 3000));\n\n\tprocess.exit(0);\n});\n\nasync function autoConfigureServerless() {\n\tgetLogger().info(\"Configuring serverless\");\n\n\tconst res = await fetch(\n\t\t`${RIVET_ENDPOINT}/runner-configs/${RIVET_RUNNER_NAME}?namespace=${RIVET_NAMESPACE}`,\n\t\t{\n\t\t\tmethod: \"PUT\",\n\t\t\theaders: {\n\t\t\t\tAuthorization: `Bearer ${RIVET_TOKEN}`,\n\t\t\t\t\"Content-Type\": \"application/json\",\n\t\t\t},\n\t\t\tbody: JSON.stringify({\n\t\t\t\tdatacenters: {\n\t\t\t\t\tdefault: {\n\t\t\t\t\t\tserverless: {\n\t\t\t\t\t\t\turl: `http://localhost:${INTERNAL_SERVER_PORT}/api/rivet`,\n\t\t\t\t\t\t\tmax_runners: 10000,\n\t\t\t\t\t\t\tslots_per_runner: 1,\n\t\t\t\t\t\t\trequest_lifespan: 300,\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t}),\n\t\t},\n\t);\n\n\tif (!res.ok) {\n\t\tthrow new Error(\n\t\t\t`request failed: ${res.statusText} (${res.status}):\\n${await res.text()}`,\n\t\t);\n\t}\n}\n\nasync function startRunner(\n\trunnerStarted: PromiseWithResolvers<Runner>,\n\trunnerStopped: PromiseWithResolvers<Runner>,\n): Promise<Runner> {\n\tgetLogger().info(\"Starting runner\");\n\tlet runner: Runner;\n\tconst config: RunnerConfig = {\n\t\tlogger: getLogger(),\n\t\tversion: RIVET_RUNNER_VERSION,\n\t\tendpoint: RIVET_ENDPOINT,\n\t\ttoken: RIVET_TOKEN,\n\t\tnamespace: RIVET_NAMESPACE,\n\t\trunnerName: RIVET_RUNNER_NAME,\n\t\ttotalSlots: RIVET_RUNNER_TOTAL_SLOTS,\n\t\tprepopulateActorNames: {},\n\t\tonConnected: () => {\n\t\t\trunnerStarted.resolve(runner);\n\t\t},\n\t\tonDisconnected: () => { },\n\t\tonShutdown: () => {\n\t\t\trunnerStopped.resolve(runner);\n\t\t},\n\t\tfetch: async (\n\t\t\trunner: Runner,\n\t\t\tactorId: string,\n\t\t\t_gatewayId: ArrayBuffer,\n\t\t\t_requestId: ArrayBuffer,\n\t\t\trequest: Request,\n\t\t) => {\n\t\t\tgetLogger().info(\n\t\t\t\t`Fetch called for actor ${actorId}, URL: ${request.url}`,\n\t\t\t);\n\t\t\tconst url = new URL(request.url);\n\t\t\tif (url.pathname === \"/ping\") {\n\t\t\t\t// Return the actor ID in response\n\t\t\t\tconst responseData = {\n\t\t\t\t\tactorId,\n\t\t\t\t\tstatus: \"ok\",\n\t\t\t\t\ttimestamp: Date.now(),\n\t\t\t\t};\n\n\t\t\t\treturn new Response(JSON.stringify(responseData), {\n\t\t\t\t\tstatus: 200,\n\t\t\t\t\theaders: { \"Content-Type\": \"application/json\" },\n\t\t\t\t});\n\t\t\t} else if (url.pathname === \"/sleep\") {\n\t\t\t\trunner.sleepActor(actorId);\n\n\t\t\t\treturn new Response(\"ok\", {\n\t\t\t\t\tstatus: 200,\n\t\t\t\t\theaders: { \"Content-Type\": \"application/json\" },\n\t\t\t\t});\n\t\t\t}\n\n\t\t\treturn new Response(\"ok\", { status: 200 });\n\t\t},\n\t\tonActorStart: async (\n\t\t\t_actorId: string,\n\t\t\t_generation: number,\n\t\t\t_config: ActorConfig,\n\t\t) => {\n\t\t\tgetLogger().info(\n\t\t\t\t`Actor ${_actorId} started (generation ${_generation})`,\n\t\t\t);\n\t\t},\n\t\tonActorStop: async (_actorId: string, _generation: number) => {\n\t\t\tgetLogger().info(\n\t\t\t\t`Actor ${_actorId} stopped (generation ${_generation})`,\n\t\t\t);\n\t\t},\n\t\twebsocket: async (\n\t\t\trunner: Runner,\n\t\t\tactorId: string,\n\t\t\tws: WebSocket,\n\t\t\t_gatewayId: ArrayBuffer,\n\t\t\t_requestId: ArrayBuffer,\n\t\t\t_request: Request,\n\t\t) => {\n\t\t\tgetLogger().info(`WebSocket connected for actor ${actorId}`);\n\n\t\t\t// Echo server - send back any messages received\n\t\t\tws.addEventListener(\"message\", (event) => {\n\t\t\t\tconst data = event.data;\n\t\t\t\tgetLogger().info({\n\t\t\t\t\tmsg: `WebSocket message from actor ${actorId}`,\n\t\t\t\t\tdata,\n\t\t\t\t\tindex: (event as any).rivetMessageIndex,\n\t\t\t\t});\n\n\t\t\t\tws.send(`Echo: ${data}`);\n\n\t\t\t\t// Ack\n\t\t\t\tconst websocketId = Buffer.from(\n\t\t\t\t\t(event as any).rivetRequestId,\n\t\t\t\t).toString(\"base64\");\n\t\t\t\twebsocketLastMsgIndexes.set(\n\t\t\t\t\twebsocketId,\n\t\t\t\t\t(event as any).rivetMessageIndex,\n\t\t\t\t);\n\t\t\t\trunner.sendHibernatableWebSocketMessageAck(\n\t\t\t\t\t(event as any).rivetGatewayId,\n\t\t\t\t\t(event as any).rivetRequestId,\n\t\t\t\t\t(event as any).rivetMessageIndex,\n\t\t\t\t);\n\t\t\t});\n\n\t\t\tws.addEventListener(\"close\", () => {\n\t\t\t\tgetLogger().info(`WebSocket closed for actor ${actorId}`);\n\t\t\t});\n\n\t\t\tws.addEventListener(\"error\", (error) => {\n\t\t\t\tgetLogger().error({\n\t\t\t\t\tmsg: `WebSocket error for actor ${actorId}:`,\n\t\t\t\t\terror,\n\t\t\t\t});\n\t\t\t});\n\t\t},\n\t\thibernatableWebSocket: {\n\t\t\tcanHibernate() {\n\t\t\t\treturn true;\n\t\t\t},\n\t\t},\n\t};\n\n\trunner = new Runner(config);\n\n\t// Start runner\n\tawait runner.start();\n\n\t// Wait for runner to be ready\n\tgetLogger().info(\"Waiting runner start...\");\n\tawait runnerStarted.promise;\n\n\tgetLogger().info(\"Runner started\");\n\n\treturn runner;\n}\n\nexport default app;\n","import { inspect } from \"node:util\";\nimport {\n\ttype Level,\n\ttype LevelWithSilent,\n\ttype Logger,\n\tpino,\n\tstdTimeFunctions,\n} from \"pino\";\n\nexport type { Logger } from \"pino\";\n\nlet baseLogger: Logger | undefined;\n\n/** Cache of child loggers by logger name. */\nconst loggerCache = new Map<string, Logger>();\n\nexport function getPinoLevel(): LevelWithSilent {\n\t// Priority: env > default\n\treturn (process.env[\"LOG_LEVEL\"] || \"warn\")\n\t\t.toString()\n\t\t.toLowerCase() as LevelWithSilent;\n}\n\nexport function getIncludeTarget(): boolean {\n\treturn process.env[\"LOG_TARGET\"] === \"1\";\n}\n\n/**\n * Configure a custom base logger.\n */\nexport function configureBaseLogger(logger: Logger): void {\n\tbaseLogger = logger;\n\tloggerCache.clear();\n}\n\n// TODO: This can be simplified in logfmt.ts\nfunction customWrite(level: string, o: any) {\n\tconst entries: any = {};\n\n\t// Add timestamp if enabled\n\tif (process.env[\"LOG_TIMESTAMP\"] === \"1\" && o.time) {\n\t\tconst date = typeof o.time === \"number\" ? new Date(o.time) : new Date();\n\t\tentries.ts = date;\n\t}\n\n\t// Add level\n\tentries.level = level.toUpperCase();\n\n\t// Add target if present\n\tif (o.target) {\n\t\tentries.target = o.target;\n\t}\n\n\t// Add message\n\tif (o.msg) {\n\t\tentries.msg = o.msg;\n\t}\n\n\t// Add other properties\n\tfor (const [key, value] of Object.entries(o)) {\n\t\tif (\n\t\t\tkey !== \"time\" &&\n\t\t\tkey !== \"level\" &&\n\t\t\tkey !== \"target\" &&\n\t\t\tkey !== \"msg\" &&\n\t\t\tkey !== \"pid\" &&\n\t\t\tkey !== \"hostname\"\n\t\t) {\n\t\t\tentries[key] = value;\n\t\t}\n\t}\n\n\tconst output = inspect(entries, {\n\t\tcompact: true,\n\t\tbreakLength: Infinity,\n\t\tcolors: true,\n\t});\n\tconsole.log(output);\n}\n\n/**\n * Configure the default logger with optional log level.\n */\nexport async function configureDefaultLogger(): Promise<void> {\n\tbaseLogger = pino({\n\t\tlevel: getPinoLevel(),\n\t\tmessageKey: \"msg\",\n\t\t// Do not include pid/hostname in output\n\t\tbase: {},\n\t\t// Keep a string level in the output\n\t\tformatters: {\n\t\t\tlevel(_label: string, number: number) {\n\t\t\t\treturn { level: number };\n\t\t\t},\n\t\t},\n\t\ttimestamp:\n\t\t\tprocess.env[\"LOG_TIMESTAMP\"] === \"1\"\n\t\t\t\t? stdTimeFunctions.epochTime\n\t\t\t\t: false,\n\t\tbrowser: {\n\t\t\twrite: {\n\t\t\t\tfatal: customWrite.bind(null, \"fatal\"),\n\t\t\t\terror: customWrite.bind(null, \"error\"),\n\t\t\t\twarn: customWrite.bind(null, \"warn\"),\n\t\t\t\tinfo: customWrite.bind(null, \"info\"),\n\t\t\t\tdebug: customWrite.bind(null, \"debug\"),\n\t\t\t\ttrace: customWrite.bind(null, \"trace\"),\n\t\t\t},\n\t\t},\n\t\thooks: {\n\t\t\tlogMethod(inputArgs, _method, level) {\n\t\t\t\t// TODO: This is a hack to not implement our own transport target. We can get better perf if we have our own transport target.\n\n\t\t\t\tconst levelMap: Record<number, string> = {\n\t\t\t\t\t10: \"trace\",\n\t\t\t\t\t20: \"debug\",\n\t\t\t\t\t30: \"info\",\n\t\t\t\t\t40: \"warn\",\n\t\t\t\t\t50: \"error\",\n\t\t\t\t\t60: \"fatal\",\n\t\t\t\t};\n\t\t\t\tconst levelName = levelMap[level] || \"info\";\n\t\t\t\tconst time =\n\t\t\t\t\tprocess.env[\"LOG_TIMESTAMP\"] === \"1\"\n\t\t\t\t\t\t? Date.now()\n\t\t\t\t\t\t: undefined;\n\t\t\t\t// TODO: This can be simplified in logfmt.ts\n\t\t\t\tif (inputArgs.length >= 2) {\n\t\t\t\t\tconst [objOrMsg, msg] = inputArgs;\n\t\t\t\t\tif (typeof objOrMsg === \"object\" && objOrMsg !== null) {\n\t\t\t\t\t\tcustomWrite(levelName, { ...objOrMsg, msg, time });\n\t\t\t\t\t} else {\n\t\t\t\t\t\tcustomWrite(levelName, { msg: String(objOrMsg), time });\n\t\t\t\t\t}\n\t\t\t\t} else if (inputArgs.length === 1) {\n\t\t\t\t\tconst [objOrMsg] = inputArgs;\n\t\t\t\t\tif (typeof objOrMsg === \"object\" && objOrMsg !== null) {\n\t\t\t\t\t\tcustomWrite(levelName, { ...objOrMsg, time });\n\t\t\t\t\t} else {\n\t\t\t\t\t\tcustomWrite(levelName, { msg: String(objOrMsg), time });\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t},\n\t\t},\n\t});\n\n\tloggerCache.clear();\n}\n\n/**\n * Get or initialize the base logger.\n */\nexport function getBaseLogger(): Logger {\n\tif (!baseLogger) {\n\t\tconfigureDefaultLogger();\n\t}\n\treturn baseLogger!;\n}\n\n/**\n * Returns a child logger with `target` bound for the given name.\n */\nexport function getLogger(name = \"default\"): Logger {\n\t// Check cache first\n\tconst cached = loggerCache.get(name);\n\tif (cached) {\n\t\treturn cached;\n\t}\n\n\t// Create\n\tconst base = getBaseLogger();\n\n\t// Add target to log if enabled\n\tconst child = getIncludeTarget() ? base.child({ target: name }) : base;\n\n\t// Cache the logger\n\tloggerCache.set(name, child);\n\n\treturn child;\n}\n"],"mappings":";AAAA,SAAS,aAAa;AAEtB,SAAS,cAAc;AACvB,SAAS,YAAoD;AAC7D,SAAS,iBAAiB;;;ACJ1B,SAAS,eAAe;AACxB;AAAA,EAIC;AAAA,EACA;AAAA,OACM;AAIP,IAAI;AAGJ,IAAM,cAAc,oBAAI,IAAoB;AAErC,SAAS,eAAgC;AAE/C,UAAQ,QAAQ,IAAI,WAAW,KAAK,QAClC,SAAS,EACT,YAAY;AACf;AAEO,SAAS,mBAA4B;AAC3C,SAAO,QAAQ,IAAI,YAAY,MAAM;AACtC;AAWA,SAAS,YAAY,OAAe,GAAQ;AAC3C,QAAM,UAAe,CAAC;AAGtB,MAAI,QAAQ,IAAI,eAAe,MAAM,OAAO,EAAE,MAAM;AACnD,UAAM,OAAO,OAAO,EAAE,SAAS,WAAW,IAAI,KAAK,EAAE,IAAI,IAAI,oBAAI,KAAK;AACtE,YAAQ,KAAK;AAAA,EACd;AAGA,UAAQ,QAAQ,MAAM,YAAY;AAGlC,MAAI,EAAE,QAAQ;AACb,YAAQ,SAAS,EAAE;AAAA,EACpB;AAGA,MAAI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|
+
"name": "@rivetkit/engine-test-runner",
|
|
3
|
+
"version": "2.2.1-pr.4600.0c0c39c",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"scripts": {
|
|
6
|
+
"start": "tsx src/index.ts",
|
|
7
|
+
"build": "tsup src/index.ts",
|
|
8
|
+
"check-types": "tsc --noEmit"
|
|
9
|
+
},
|
|
10
|
+
"dependencies": {
|
|
11
|
+
"@hono/node-server": "^1.19.1",
|
|
12
|
+
"@rivetkit/engine-runner": "2.2.1-pr.4600.0c0c39c",
|
|
13
|
+
"@rivetkit/engine-runner-protocol": "2.2.1-pr.4600.0c0c39c",
|
|
14
|
+
"hono": "^4.7.0",
|
|
15
|
+
"pino": "^9.9.5",
|
|
16
|
+
"ws": "^8.18.3"
|
|
17
|
+
},
|
|
18
|
+
"devDependencies": {
|
|
19
|
+
"@types/node": "^22.18.1",
|
|
20
|
+
"@types/ws": "^8.18.1",
|
|
21
|
+
"tinybench": "^5.0.1",
|
|
22
|
+
"tsup": "^8.5.0",
|
|
23
|
+
"tsx": "^4.20.5",
|
|
24
|
+
"typescript": "^5.9.2",
|
|
25
|
+
"vitest": "^1.6.1"
|
|
26
|
+
}
|
|
27
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -12,23 +12,20 @@ const INTERNAL_SERVER_PORT = process.env.INTERNAL_SERVER_PORT
|
|
|
12
12
|
: 5051;
|
|
13
13
|
const RIVET_NAMESPACE = process.env.RIVET_NAMESPACE ?? "default";
|
|
14
14
|
const RIVET_RUNNER_NAME = process.env.RIVET_RUNNER_NAME ?? "test-runner";
|
|
15
|
-
const RIVET_RUNNER_KEY =
|
|
16
|
-
process.env.RIVET_RUNNER_KEY ?? `key-${Math.floor(Math.random() * 10000)}`;
|
|
17
15
|
const RIVET_RUNNER_VERSION = process.env.RIVET_RUNNER_VERSION
|
|
18
16
|
? Number(process.env.RIVET_RUNNER_VERSION)
|
|
19
17
|
: 1;
|
|
20
|
-
const RIVET_RUNNER_TOTAL_SLOTS = process.env.RIVET_RUNNER_TOTAL_SLOTS
|
|
21
|
-
? Number(process.env.RIVET_RUNNER_TOTAL_SLOTS)
|
|
22
|
-
: 100;
|
|
18
|
+
const RIVET_RUNNER_TOTAL_SLOTS = parseInt(process.env.RIVET_RUNNER_TOTAL_SLOTS ?? "1");
|
|
23
19
|
const RIVET_ENDPOINT = process.env.RIVET_ENDPOINT ?? "http://127.0.0.1:6420";
|
|
24
20
|
const RIVET_TOKEN = process.env.RIVET_TOKEN ?? "dev";
|
|
25
|
-
const AUTOSTART_SERVER = process.env.
|
|
26
|
-
const AUTOSTART_RUNNER = process.env.
|
|
21
|
+
const AUTOSTART_SERVER = (process.env.AUTOSTART_SERVER ?? "1") == "1";
|
|
22
|
+
const AUTOSTART_RUNNER = (process.env.AUTOSTART_RUNNER ?? "0") == "1";
|
|
23
|
+
const AUTOCONFIGURE_SERVERLESS = (process.env.AUTOCONFIGURE_SERVERLESS ?? "1") == "1";
|
|
27
24
|
|
|
28
|
-
|
|
29
|
-
|
|
25
|
+
const runnerStarted = Promise.withResolvers<Runner>();
|
|
26
|
+
const runnerStopped = Promise.withResolvers<Runner>();
|
|
30
27
|
let runner: Runner | null = null;
|
|
31
|
-
const
|
|
28
|
+
const websocketLastMsgIndexes: Map<string, number> = new Map();
|
|
32
29
|
|
|
33
30
|
// Create internal server
|
|
34
31
|
const app = new Hono();
|
|
@@ -57,8 +54,8 @@ function loggerMiddleware(logger: Logger) {
|
|
|
57
54
|
app.use("*", loggerMiddleware(getLogger()));
|
|
58
55
|
|
|
59
56
|
app.get("/wait-ready", async (c) => {
|
|
60
|
-
await runnerStarted.promise;
|
|
61
|
-
return c.json(runner
|
|
57
|
+
const runner = await runnerStarted.promise;
|
|
58
|
+
return c.json(runner.runnerId);
|
|
62
59
|
});
|
|
63
60
|
|
|
64
61
|
app.get("/has-actor", async (c) => {
|
|
@@ -72,18 +69,24 @@ app.get("/has-actor", async (c) => {
|
|
|
72
69
|
return c.text("ok");
|
|
73
70
|
});
|
|
74
71
|
|
|
72
|
+
app.get("/health", (c) => {
|
|
73
|
+
return c.text("ok");
|
|
74
|
+
});
|
|
75
|
+
|
|
75
76
|
app.get("/shutdown", async (c) => {
|
|
76
77
|
await runner?.shutdown(true);
|
|
77
78
|
return c.text("ok");
|
|
78
79
|
});
|
|
79
80
|
|
|
80
|
-
app.get("/start", async (c) => {
|
|
81
|
+
app.get("/api/rivet/start", async (c) => {
|
|
81
82
|
return streamSSE(c, async (stream) => {
|
|
82
|
-
const
|
|
83
|
+
const runnerStarted = Promise.withResolvers<Runner>();
|
|
84
|
+
const runnerStopped = Promise.withResolvers<Runner>();
|
|
85
|
+
const runner = await startRunner(runnerStarted, runnerStopped);
|
|
83
86
|
|
|
84
87
|
c.req.raw.signal.addEventListener("abort", () => {
|
|
85
88
|
getLogger().debug("SSE aborted, shutting down runner");
|
|
86
|
-
runner
|
|
89
|
+
runner!.shutdown(true);
|
|
87
90
|
});
|
|
88
91
|
|
|
89
92
|
await runnerStarted.promise;
|
|
@@ -94,7 +97,13 @@ app.get("/start", async (c) => {
|
|
|
94
97
|
});
|
|
95
98
|
});
|
|
96
99
|
|
|
97
|
-
|
|
100
|
+
app.get("/api/rivet/metadata", async (c) => {
|
|
101
|
+
return c.json({
|
|
102
|
+
// Not actually rivetkit
|
|
103
|
+
runtime: "rivetkit",
|
|
104
|
+
version: "1",
|
|
105
|
+
});
|
|
106
|
+
});
|
|
98
107
|
|
|
99
108
|
if (AUTOSTART_SERVER) {
|
|
100
109
|
serve({
|
|
@@ -106,12 +115,32 @@ if (AUTOSTART_SERVER) {
|
|
|
106
115
|
);
|
|
107
116
|
}
|
|
108
117
|
|
|
109
|
-
if (AUTOSTART_RUNNER)
|
|
110
|
-
|
|
118
|
+
if (AUTOSTART_RUNNER) {
|
|
119
|
+
runner = await startRunner(runnerStarted, runnerStopped);
|
|
120
|
+
} else if (AUTOCONFIGURE_SERVERLESS) {
|
|
121
|
+
await autoConfigureServerless();
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
process.on("SIGTERM", async () => {
|
|
125
|
+
getLogger().debug("received SIGTERM, force exiting in 3s");
|
|
126
|
+
|
|
127
|
+
await new Promise(res => setTimeout(res, 3000));
|
|
128
|
+
|
|
129
|
+
process.exit(0);
|
|
130
|
+
});
|
|
131
|
+
process.on("SIGINT", async () => {
|
|
132
|
+
getLogger().debug("received SIGTERM, force exiting in 3s");
|
|
133
|
+
|
|
134
|
+
await new Promise(res => setTimeout(res, 3000));
|
|
135
|
+
|
|
136
|
+
process.exit(0);
|
|
137
|
+
});
|
|
111
138
|
|
|
112
139
|
async function autoConfigureServerless() {
|
|
140
|
+
getLogger().info("Configuring serverless");
|
|
141
|
+
|
|
113
142
|
const res = await fetch(
|
|
114
|
-
|
|
143
|
+
`${RIVET_ENDPOINT}/runner-configs/${RIVET_RUNNER_NAME}?namespace=${RIVET_NAMESPACE}`,
|
|
115
144
|
{
|
|
116
145
|
method: "PUT",
|
|
117
146
|
headers: {
|
|
@@ -122,10 +151,10 @@ async function autoConfigureServerless() {
|
|
|
122
151
|
datacenters: {
|
|
123
152
|
default: {
|
|
124
153
|
serverless: {
|
|
125
|
-
url: `http://localhost:${INTERNAL_SERVER_PORT}`,
|
|
126
|
-
max_runners:
|
|
154
|
+
url: `http://localhost:${INTERNAL_SERVER_PORT}/api/rivet`,
|
|
155
|
+
max_runners: 10000,
|
|
127
156
|
slots_per_runner: 1,
|
|
128
|
-
request_lifespan:
|
|
157
|
+
request_lifespan: 300,
|
|
129
158
|
},
|
|
130
159
|
},
|
|
131
160
|
},
|
|
@@ -140,14 +169,12 @@ async function autoConfigureServerless() {
|
|
|
140
169
|
}
|
|
141
170
|
}
|
|
142
171
|
|
|
143
|
-
async function startRunner(
|
|
144
|
-
|
|
145
|
-
|
|
172
|
+
async function startRunner(
|
|
173
|
+
runnerStarted: PromiseWithResolvers<Runner>,
|
|
174
|
+
runnerStopped: PromiseWithResolvers<Runner>,
|
|
175
|
+
): Promise<Runner> {
|
|
146
176
|
getLogger().info("Starting runner");
|
|
147
|
-
|
|
148
|
-
const runnerStarted = Promise.withResolvers();
|
|
149
|
-
const runnerStopped = Promise.withResolvers();
|
|
150
|
-
|
|
177
|
+
let runner: Runner;
|
|
151
178
|
const config: RunnerConfig = {
|
|
152
179
|
logger: getLogger(),
|
|
153
180
|
version: RIVET_RUNNER_VERSION,
|
|
@@ -155,17 +182,22 @@ async function startRunner(): Promise<
|
|
|
155
182
|
token: RIVET_TOKEN,
|
|
156
183
|
namespace: RIVET_NAMESPACE,
|
|
157
184
|
runnerName: RIVET_RUNNER_NAME,
|
|
158
|
-
runnerKey: RIVET_RUNNER_KEY,
|
|
159
185
|
totalSlots: RIVET_RUNNER_TOTAL_SLOTS,
|
|
160
186
|
prepopulateActorNames: {},
|
|
161
187
|
onConnected: () => {
|
|
162
|
-
runnerStarted.resolve(
|
|
188
|
+
runnerStarted.resolve(runner);
|
|
163
189
|
},
|
|
164
|
-
onDisconnected: () => {},
|
|
190
|
+
onDisconnected: () => { },
|
|
165
191
|
onShutdown: () => {
|
|
166
|
-
runnerStopped.resolve(
|
|
192
|
+
runnerStopped.resolve(runner);
|
|
167
193
|
},
|
|
168
|
-
fetch: async (
|
|
194
|
+
fetch: async (
|
|
195
|
+
runner: Runner,
|
|
196
|
+
actorId: string,
|
|
197
|
+
_gatewayId: ArrayBuffer,
|
|
198
|
+
_requestId: ArrayBuffer,
|
|
199
|
+
request: Request,
|
|
200
|
+
) => {
|
|
169
201
|
getLogger().info(
|
|
170
202
|
`Fetch called for actor ${actorId}, URL: ${request.url}`,
|
|
171
203
|
);
|
|
@@ -208,13 +240,14 @@ async function startRunner(): Promise<
|
|
|
208
240
|
);
|
|
209
241
|
},
|
|
210
242
|
websocket: async (
|
|
211
|
-
|
|
243
|
+
runner: Runner,
|
|
212
244
|
actorId: string,
|
|
213
245
|
ws: WebSocket,
|
|
214
|
-
|
|
246
|
+
_gatewayId: ArrayBuffer,
|
|
247
|
+
_requestId: ArrayBuffer,
|
|
248
|
+
_request: Request,
|
|
215
249
|
) => {
|
|
216
250
|
getLogger().info(`WebSocket connected for actor ${actorId}`);
|
|
217
|
-
actorWebSockets.set(actorId, ws);
|
|
218
251
|
|
|
219
252
|
// Echo server - send back any messages received
|
|
220
253
|
ws.addEventListener("message", (event) => {
|
|
@@ -222,13 +255,28 @@ async function startRunner(): Promise<
|
|
|
222
255
|
getLogger().info({
|
|
223
256
|
msg: `WebSocket message from actor ${actorId}`,
|
|
224
257
|
data,
|
|
258
|
+
index: (event as any).rivetMessageIndex,
|
|
225
259
|
});
|
|
260
|
+
|
|
226
261
|
ws.send(`Echo: ${data}`);
|
|
262
|
+
|
|
263
|
+
// Ack
|
|
264
|
+
const websocketId = Buffer.from(
|
|
265
|
+
(event as any).rivetRequestId,
|
|
266
|
+
).toString("base64");
|
|
267
|
+
websocketLastMsgIndexes.set(
|
|
268
|
+
websocketId,
|
|
269
|
+
(event as any).rivetMessageIndex,
|
|
270
|
+
);
|
|
271
|
+
runner.sendHibernatableWebSocketMessageAck(
|
|
272
|
+
(event as any).rivetGatewayId,
|
|
273
|
+
(event as any).rivetRequestId,
|
|
274
|
+
(event as any).rivetMessageIndex,
|
|
275
|
+
);
|
|
227
276
|
});
|
|
228
277
|
|
|
229
278
|
ws.addEventListener("close", () => {
|
|
230
279
|
getLogger().info(`WebSocket closed for actor ${actorId}`);
|
|
231
|
-
actorWebSockets.delete(actorId);
|
|
232
280
|
});
|
|
233
281
|
|
|
234
282
|
ws.addEventListener("error", (error) => {
|
|
@@ -238,9 +286,14 @@ async function startRunner(): Promise<
|
|
|
238
286
|
});
|
|
239
287
|
});
|
|
240
288
|
},
|
|
289
|
+
hibernatableWebSocket: {
|
|
290
|
+
canHibernate() {
|
|
291
|
+
return true;
|
|
292
|
+
},
|
|
293
|
+
},
|
|
241
294
|
};
|
|
242
295
|
|
|
243
|
-
|
|
296
|
+
runner = new Runner(config);
|
|
244
297
|
|
|
245
298
|
// Start runner
|
|
246
299
|
await runner.start();
|
|
@@ -251,7 +304,7 @@ async function startRunner(): Promise<
|
|
|
251
304
|
|
|
252
305
|
getLogger().info("Runner started");
|
|
253
306
|
|
|
254
|
-
return
|
|
307
|
+
return runner;
|
|
255
308
|
}
|
|
256
309
|
|
|
257
310
|
export default app;
|
package/src/log.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { inspect } from "node:util";
|
|
1
2
|
import {
|
|
2
3
|
type Level,
|
|
3
4
|
type LevelWithSilent,
|
|
@@ -5,26 +6,16 @@ import {
|
|
|
5
6
|
pino,
|
|
6
7
|
stdTimeFunctions,
|
|
7
8
|
} from "pino";
|
|
8
|
-
import { inspect } from "util";
|
|
9
9
|
|
|
10
10
|
export type { Logger } from "pino";
|
|
11
11
|
|
|
12
12
|
let baseLogger: Logger | undefined;
|
|
13
|
-
let configuredLogLevel: Level | undefined;
|
|
14
13
|
|
|
15
14
|
/** Cache of child loggers by logger name. */
|
|
16
15
|
const loggerCache = new Map<string, Logger>();
|
|
17
16
|
|
|
18
|
-
export function getPinoLevel(
|
|
19
|
-
// Priority:
|
|
20
|
-
if (logLevel) {
|
|
21
|
-
return logLevel;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
if (configuredLogLevel) {
|
|
25
|
-
return configuredLogLevel;
|
|
26
|
-
}
|
|
27
|
-
|
|
17
|
+
export function getPinoLevel(): LevelWithSilent {
|
|
18
|
+
// Priority: env > default
|
|
28
19
|
return (process.env["LOG_LEVEL"] || "warn")
|
|
29
20
|
.toString()
|
|
30
21
|
.toLowerCase() as LevelWithSilent;
|
|
@@ -90,14 +81,9 @@ function customWrite(level: string, o: any) {
|
|
|
90
81
|
/**
|
|
91
82
|
* Configure the default logger with optional log level.
|
|
92
83
|
*/
|
|
93
|
-
export async function configureDefaultLogger(
|
|
94
|
-
// Store the configured log level
|
|
95
|
-
if (logLevel) {
|
|
96
|
-
configuredLogLevel = logLevel;
|
|
97
|
-
}
|
|
98
|
-
|
|
84
|
+
export async function configureDefaultLogger(): Promise<void> {
|
|
99
85
|
baseLogger = pino({
|
|
100
|
-
level: getPinoLevel(
|
|
86
|
+
level: getPinoLevel(),
|
|
101
87
|
messageKey: "msg",
|
|
102
88
|
// Do not include pid/hostname in output
|
|
103
89
|
base: {},
|
package/Dockerfile
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
FROM node:22-slim
|
|
2
|
-
|
|
3
|
-
WORKDIR /app
|
|
4
|
-
|
|
5
|
-
# Install pnpm (match workspace version) and set CI for non-interactive behavior
|
|
6
|
-
RUN corepack enable && corepack prepare pnpm@10.13.1 --activate
|
|
7
|
-
ENV CI=true
|
|
8
|
-
|
|
9
|
-
# Copy and build SDK
|
|
10
|
-
COPY package.json pnpm-lock.yaml pnpm-workspace.yaml tsup.base.ts tsconfig.base.json turbo.json .
|
|
11
|
-
COPY sdks/typescript/runner-protocol ./sdks/typescript/runner-protocol/
|
|
12
|
-
COPY sdks/typescript/runner ./sdks/typescript/runner/
|
|
13
|
-
|
|
14
|
-
# Install dependencies
|
|
15
|
-
COPY sdks/typescript/test-runner/package.json ./sdks/typescript/test-runner/
|
|
16
|
-
RUN pnpm install --no-frozen-lockfile
|
|
17
|
-
|
|
18
|
-
# Build the application
|
|
19
|
-
COPY sdks/typescript/test-runner/ ./sdks/typescript/test-runner/
|
|
20
|
-
RUN pnpm build -F @rivetkit/engine-test-runner
|
|
21
|
-
|
|
22
|
-
# Expose the HTTP server port
|
|
23
|
-
EXPOSE 5050
|
|
24
|
-
|
|
25
|
-
# Run the application
|
|
26
|
-
CMD ["node", "sdks/typescript/test-runner/dist/index.js"]
|
package/LICENSE
DELETED
|
@@ -1,203 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
Apache License
|
|
3
|
-
Version 2.0, January 2004
|
|
4
|
-
http://www.apache.org/licenses/
|
|
5
|
-
|
|
6
|
-
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
-
|
|
8
|
-
1. Definitions.
|
|
9
|
-
|
|
10
|
-
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
-
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
-
|
|
13
|
-
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
-
the copyright owner that is granting the License.
|
|
15
|
-
|
|
16
|
-
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
-
other entities that control, are controlled by, or are under common
|
|
18
|
-
control with that entity. For the purposes of this definition,
|
|
19
|
-
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
-
direction or management of such entity, whether by contract or
|
|
21
|
-
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
-
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
-
|
|
24
|
-
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
-
exercising permissions granted by this License.
|
|
26
|
-
|
|
27
|
-
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
-
including but not limited to software source code, documentation
|
|
29
|
-
source, and configuration files.
|
|
30
|
-
|
|
31
|
-
"Object" form shall mean any form resulting from mechanical
|
|
32
|
-
transformation or translation of a Source form, including but
|
|
33
|
-
not limited to compiled object code, generated documentation,
|
|
34
|
-
and conversions to other media types.
|
|
35
|
-
|
|
36
|
-
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
-
Object form, made available under the License, as indicated by a
|
|
38
|
-
copyright notice that is included in or attached to the work
|
|
39
|
-
(an example is provided in the Appendix below).
|
|
40
|
-
|
|
41
|
-
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
-
form, that is based on (or derived from) the Work and for which the
|
|
43
|
-
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
-
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
-
of this License, Derivative Works shall not include works that remain
|
|
46
|
-
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
-
the Work and Derivative Works thereof.
|
|
48
|
-
|
|
49
|
-
"Contribution" shall mean any work of authorship, including
|
|
50
|
-
the original version of the Work and any modifications or additions
|
|
51
|
-
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
-
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
-
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
-
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
-
means any form of electronic, verbal, or written communication sent
|
|
56
|
-
to the Licensor or its representatives, including but not limited to
|
|
57
|
-
communication on electronic mailing lists, source code control systems,
|
|
58
|
-
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
-
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
-
excluding communication that is conspicuously marked or otherwise
|
|
61
|
-
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
-
|
|
63
|
-
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
-
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
-
subsequently incorporated within the Work.
|
|
66
|
-
|
|
67
|
-
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
-
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
-
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
-
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
-
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
-
Work and such Derivative Works in Source or Object form.
|
|
73
|
-
|
|
74
|
-
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
-
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
-
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
-
(except as stated in this section) patent license to make, have made,
|
|
78
|
-
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
-
where such license applies only to those patent claims licensable
|
|
80
|
-
by such Contributor that are necessarily infringed by their
|
|
81
|
-
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
-
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
-
institute patent litigation against any entity (including a
|
|
84
|
-
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
-
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
-
or contributory patent infringement, then any patent licenses
|
|
87
|
-
granted to You under this License for that Work shall terminate
|
|
88
|
-
as of the date such litigation is filed.
|
|
89
|
-
|
|
90
|
-
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
-
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
-
modifications, and in Source or Object form, provided that You
|
|
93
|
-
meet the following conditions:
|
|
94
|
-
|
|
95
|
-
(a) You must give any other recipients of the Work or
|
|
96
|
-
Derivative Works a copy of this License; and
|
|
97
|
-
|
|
98
|
-
(b) You must cause any modified files to carry prominent notices
|
|
99
|
-
stating that You changed the files; and
|
|
100
|
-
|
|
101
|
-
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
-
that You distribute, all copyright, patent, trademark, and
|
|
103
|
-
attribution notices from the Source form of the Work,
|
|
104
|
-
excluding those notices that do not pertain to any part of
|
|
105
|
-
the Derivative Works; and
|
|
106
|
-
|
|
107
|
-
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
-
distribution, then any Derivative Works that You distribute must
|
|
109
|
-
include a readable copy of the attribution notices contained
|
|
110
|
-
within such NOTICE file, excluding those notices that do not
|
|
111
|
-
pertain to any part of the Derivative Works, in at least one
|
|
112
|
-
of the following places: within a NOTICE text file distributed
|
|
113
|
-
as part of the Derivative Works; within the Source form or
|
|
114
|
-
documentation, if provided along with the Derivative Works; or,
|
|
115
|
-
within a display generated by the Derivative Works, if and
|
|
116
|
-
wherever such third-party notices normally appear. The contents
|
|
117
|
-
of the NOTICE file are for informational purposes only and
|
|
118
|
-
do not modify the License. You may add Your own attribution
|
|
119
|
-
notices within Derivative Works that You distribute, alongside
|
|
120
|
-
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
-
that such additional attribution notices cannot be construed
|
|
122
|
-
as modifying the License.
|
|
123
|
-
|
|
124
|
-
You may add Your own copyright statement to Your modifications and
|
|
125
|
-
may provide additional or different license terms and conditions
|
|
126
|
-
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
-
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
-
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
-
the conditions stated in this License.
|
|
130
|
-
|
|
131
|
-
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
-
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
-
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
-
this License, without any additional terms or conditions.
|
|
135
|
-
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
-
the terms of any separate license agreement you may have executed
|
|
137
|
-
with Licensor regarding such Contributions.
|
|
138
|
-
|
|
139
|
-
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
-
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
-
except as required for reasonable and customary use in describing the
|
|
142
|
-
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
-
|
|
144
|
-
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
-
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
-
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
-
implied, including, without limitation, any warranties or conditions
|
|
149
|
-
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
-
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
-
appropriateness of using or redistributing the Work and assume any
|
|
152
|
-
risks associated with Your exercise of permissions under this License.
|
|
153
|
-
|
|
154
|
-
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
-
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
-
unless required by applicable law (such as deliberate and grossly
|
|
157
|
-
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
-
liable to You for damages, including any direct, indirect, special,
|
|
159
|
-
incidental, or consequential damages of any character arising as a
|
|
160
|
-
result of this License or out of the use or inability to use the
|
|
161
|
-
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
-
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
-
other commercial damages or losses), even if such Contributor
|
|
164
|
-
has been advised of the possibility of such damages.
|
|
165
|
-
|
|
166
|
-
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
-
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
-
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
-
or other liability obligations and/or rights consistent with this
|
|
170
|
-
License. However, in accepting such obligations, You may act only
|
|
171
|
-
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
-
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
-
defend, and hold each Contributor harmless for any liability
|
|
174
|
-
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
-
of your accepting any such warranty or additional liability.
|
|
176
|
-
|
|
177
|
-
END OF TERMS AND CONDITIONS
|
|
178
|
-
|
|
179
|
-
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
-
|
|
181
|
-
To apply the Apache License to your work, attach the following
|
|
182
|
-
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
-
replaced with your own identifying information. (Don't include
|
|
184
|
-
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
-
comment syntax for the file format. We also recommend that a
|
|
186
|
-
file or class name and description of purpose be included on the
|
|
187
|
-
same "printed page" as the copyright notice for easier
|
|
188
|
-
identification within third-party archives.
|
|
189
|
-
|
|
190
|
-
Copyright 2023 Rivet Gaming, Inc.
|
|
191
|
-
|
|
192
|
-
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
-
you may not use this file except in compliance with the License.
|
|
194
|
-
You may obtain a copy of the License at
|
|
195
|
-
|
|
196
|
-
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
-
|
|
198
|
-
Unless required by applicable law or agreed to in writing, software
|
|
199
|
-
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
-
See the License for the specific language governing permissions and
|
|
202
|
-
limitations under the License.
|
|
203
|
-
|