forked from oomol-lab/open-connector
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcloudflare.ts
More file actions
155 lines (141 loc) · 6.65 KB
/
Copy pathcloudflare.ts
File metadata and controls
155 lines (141 loc) · 6.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import type { CatalogStore } from "../catalog-store.ts";
import type { AssetsBinding, KVNamespaceBinding, R2BucketBinding } from "./cloudflare/cloudflare-bindings.ts";
import type { CloudflareEnv } from "./cloudflare/cloudflare-env.ts";
import type { ConnectApp } from "./connect-app.ts";
import type { Logger } from "./logger.ts";
import type { ISecretCodec } from "./secrets/secret-codec-core.ts";
import { ActionPolicyService, parseActionPolicyList } from "../core/action-policy.ts";
import { parsePrivateNetworkAccessFlag, setPrivateNetworkAccessAllowed } from "../core/request.ts";
import { ProviderLoader } from "../providers/provider-loader.ts";
import { executorModules } from "../providers/registry.cloudflare.generated.ts";
import { isConsoleShellPath } from "./api/console-paths.ts";
import { loadCatalogFromAssets } from "./cloudflare/catalog-assets.ts";
import { readPositiveInteger, resolvePublicOrigin } from "./cloudflare/cloudflare-env.ts";
import { createConnectApp } from "./connect-app.ts";
import { KVTransitFileService } from "./files/kv-transit-files.ts";
import { R2TransitFileService } from "./files/r2-transit-files.ts";
import { createWorkerSecretCodec } from "./secrets/worker-secret-codec.ts";
import { D1RuntimeDatabase } from "./storage/d1-runtime-store.ts";
import { DEFAULT_RUN_LIMIT } from "./storage/runtime-store.ts";
interface CloudflareExecutionContext {
waitUntil(promise: Promise<unknown>): void;
passThroughOnException(): void;
}
let catalogPromise: Promise<CatalogStore> | undefined;
let cachedSecretCodec: { key: string; codec: Promise<ISecretCodec> } | undefined;
let cachedApp: { key: string; app: Promise<ConnectApp> } | undefined;
export default {
async fetch(request: Request, env: CloudflareEnv, _ctx: CloudflareExecutionContext): Promise<Response> {
setPrivateNetworkAccessAllowed(parsePrivateNetworkAccessFlag(env.OOMOL_CONNECT_ALLOW_PRIVATE_NETWORK));
const publicOrigin = resolvePublicOrigin(request, env);
const cacheKey = createCacheKey(env, publicOrigin);
if (!cachedApp || cachedApp.key !== cacheKey) {
cachedApp = { key: cacheKey, app: createCloudflareApp(env, publicOrigin) };
}
const { app } = await cachedApp.app;
const response = await app.fetch(request, env);
if (response.status === 404 && env.ASSETS && shouldServeAsset(request)) {
return env.ASSETS.fetch(request);
}
return response;
},
};
async function createCloudflareApp(env: CloudflareEnv, publicOrigin: string): Promise<ConnectApp> {
const assets = env.ASSETS;
if (!assets) {
throw new Error("Cloudflare ASSETS binding is required to load the catalog");
}
const secretCodec = await createSecretCodec(env.OOMOL_CONNECT_ENCRYPTION_KEY);
return await createConnectApp({
catalog: await loadCatalogOnce(assets),
providerLoader: new ProviderLoader(executorModules),
runtimeDatabase: new D1RuntimeDatabase(env.DB, {
secretCodec,
runLimit: readPositiveInteger(env.OOMOL_CONNECT_RUN_LIMIT, DEFAULT_RUN_LIMIT),
}),
transitFiles: (() => {
const transitFileOptions = {
publicOrigin,
ttlSeconds: readPositiveInteger(env.OOMOL_CONNECT_TRANSIT_FILE_TTL_SECONDS, 86_400),
maxBytes: readPositiveInteger(env.OOMOL_CONNECT_TRANSIT_FILE_MAX_BYTES, 100 * 1024 * 1024),
};
return env.TRANSIT_FILES_BACKEND === "kv"
? new KVTransitFileService({
namespace: env.TRANSIT_FILES as KVNamespaceBinding,
...transitFileOptions,
})
: new R2TransitFileService({
bucket: env.TRANSIT_FILES as R2BucketBinding,
...transitFileOptions,
});
})(),
publicOrigin,
secretCodec,
adminToken: env.OOMOL_CONNECT_ADMIN_TOKEN,
runtimeToken: env.OOMOL_CONNECT_RUNTIME_TOKEN,
actionPolicy: new ActionPolicyService({
allowedActions: parseActionPolicyList(env.OOMOL_CONNECT_ALLOWED_ACTIONS),
blockedActions: parseActionPolicyList(env.OOMOL_CONNECT_BLOCKED_ACTIONS),
allowedProxies: parseActionPolicyList(env.OOMOL_CONNECT_ALLOWED_PROXIES),
blockedProxies: parseActionPolicyList(env.OOMOL_CONNECT_BLOCKED_PROXIES),
}),
logger: workerLogger,
computeRuntimeAuthConfigured: false,
// Cloudflare compresses on egress itself: Response defaults to
// `encodeBody: "automatic"`, so the runtime re-encodes a body that Hono's
// compress() already gzipped. Depending on what the client negotiates, the
// wire then carries gzip-in-gzip or gzip bytes with no Content-Encoding at
// all, and dashboard JSON stops parsing either way. Hono's middleware
// rebuilds the Response from the previous one and cannot pass
// `encodeBody: "manual"`, so application-level compression stays off here.
// https://developers.cloudflare.com/workers/runtime-apis/response/
compressApiResponses: false,
});
}
const workerLogger = {
error: writeWorkerLog("error"),
info: writeWorkerLog("info"),
warn: writeWorkerLog("warn"),
} as unknown as Logger;
function writeWorkerLog(level: "error" | "info" | "warn"): (fields: unknown, message?: string) => void {
return (fields, message) => {
const write = level === "error" ? console.error : level === "warn" ? console.warn : console.info;
if (message) {
write(message, fields);
return;
}
write(fields);
};
}
function loadCatalogOnce(assets: AssetsBinding): Promise<CatalogStore> {
catalogPromise ??= loadCatalogFromAssets(assets, {
executableServices: Object.keys(executorModules),
});
return catalogPromise;
}
function createSecretCodec(encryptionKey: string | undefined): Promise<ISecretCodec> {
const key = encryptionKey ?? "";
if (!cachedSecretCodec || cachedSecretCodec.key !== key) {
cachedSecretCodec = { key, codec: createWorkerSecretCodec(encryptionKey) };
}
return cachedSecretCodec.codec;
}
function createCacheKey(env: CloudflareEnv, publicOrigin: string): string {
return JSON.stringify({
publicOrigin,
adminToken: env.OOMOL_CONNECT_ADMIN_TOKEN ?? "",
runtimeToken: env.OOMOL_CONNECT_RUNTIME_TOKEN ?? "",
encryptionKey: env.OOMOL_CONNECT_ENCRYPTION_KEY ?? "",
allowedActions: env.OOMOL_CONNECT_ALLOWED_ACTIONS ?? "",
blockedActions: env.OOMOL_CONNECT_BLOCKED_ACTIONS ?? "",
allowedProxies: env.OOMOL_CONNECT_ALLOWED_PROXIES ?? "",
blockedProxies: env.OOMOL_CONNECT_BLOCKED_PROXIES ?? "",
transitFileTtlSeconds: env.OOMOL_CONNECT_TRANSIT_FILE_TTL_SECONDS ?? "",
transitFileMaxBytes: env.OOMOL_CONNECT_TRANSIT_FILE_MAX_BYTES ?? "",
runLimit: env.OOMOL_CONNECT_RUN_LIMIT ?? "",
});
}
function shouldServeAsset(request: Request): boolean {
const { pathname } = new URL(request.url);
return !pathname.startsWith("/catalog") && isConsoleShellPath(pathname);
}