UNPKG

rivetkit

Version:

Lightweight libraries for building stateful actors on edge platforms

103 lines (97 loc) 3.42 kB
import { getLogger } from "./chunk-7E5K3375.js"; // src/inspector/protocol/actor.ts import { hc } from "hono/client"; var client = hc(""); var createActorInspectorClient = (...args) => hc(...args); // src/inspector/protocol/manager.ts import { hc as hc2 } from "hono/client"; var client2 = hc2(""); var createManagerInspectorClient = (...args) => hc2(...args); // src/inspector/utils.ts import crypto from "crypto"; import { createMiddleware } from "hono/factory"; // src/inspector/log.ts function inspectorLogger() { return getLogger("inspector"); } // src/inspector/utils.ts function compareSecrets(providedSecret, validSecret) { if (providedSecret.length !== validSecret.length) { return false; } const encoder = new TextEncoder(); const a = encoder.encode(providedSecret); const b = encoder.encode(validSecret); if (a.byteLength !== b.byteLength) { return false; } if (!crypto.timingSafeEqual(a, b)) { return false; } return true; } var secureInspector = (runConfig) => createMiddleware(async (c, next) => { var _a, _b, _c; const userToken = (_a = c.req.header("Authorization")) == null ? void 0 : _a.replace("Bearer ", ""); if (!userToken) { return c.text("Unauthorized", 401); } const inspectorToken = (_c = (_b = runConfig.inspector).token) == null ? void 0 : _c.call(_b); if (!inspectorToken) { return c.text("Unauthorized", 401); } const isValid = compareSecrets(userToken, inspectorToken); if (!isValid) { return c.text("Unauthorized", 401); } await next(); }); function getInspectorUrl(runConfig) { var _a, _b, _c, _d; if (!((_a = runConfig == null ? void 0 : runConfig.inspector) == null ? void 0 : _a.enabled)) { return "disabled"; } const accessToken = (_c = (_b = runConfig == null ? void 0 : runConfig.inspector) == null ? void 0 : _b.token) == null ? void 0 : _c.call(_b); if (!accessToken) { inspectorLogger().warn( "Inspector Token is not set, but Inspector is enabled. Please set it in the run configuration `inspector.token` or via `RIVETKIT_INSPECTOR_TOKEN` environment variable. Inspector will not be accessible." ); return "disabled"; } const url = new URL("https://inspect.rivet.dev"); url.searchParams.set("t", accessToken); const overrideDefaultEndpoint = ((_d = runConfig == null ? void 0 : runConfig.inspector) == null ? void 0 : _d.defaultEndpoint) ?? runConfig.overrideServerAddress; if (overrideDefaultEndpoint) { url.searchParams.set("u", overrideDefaultEndpoint); } return url.href; } var isInspectorEnabled = (runConfig, context) => { var _a, _b; if (typeof ((_a = runConfig.inspector) == null ? void 0 : _a.enabled) === "boolean") { return runConfig.inspector.enabled; } else if (typeof ((_b = runConfig.inspector) == null ? void 0 : _b.enabled) === "object") { return runConfig.inspector.enabled[context]; } return false; }; var configureInspectorAccessToken = (runConfig, managerDriver) => { var _a; if (!((_a = runConfig.inspector) == null ? void 0 : _a.token())) { const token = managerDriver.getOrCreateInspectorAccessToken(); runConfig.inspector.token = () => token; } }; export { inspectorLogger, compareSecrets, secureInspector, getInspectorUrl, isInspectorEnabled, configureInspectorAccessToken, createActorInspectorClient, createManagerInspectorClient }; //# sourceMappingURL=chunk-KL4V2ULR.js.map