| 192 | * ``` |
| 193 | */ |
| 194 | export class DynamicWorkerExecutor implements Executor { |
| 195 | #loader: WorkerLoader; |
| 196 | #timeout: number; |
| 197 | #globalOutbound: Fetcher | null; |
| 198 | #modules: Record<string, string>; |
| 199 | |
| 200 | constructor(options: DynamicWorkerExecutorOptions) { |
| 201 | this.#loader = options.loader; |
| 202 | this.#timeout = options.timeout ?? 30000; |
| 203 | this.#globalOutbound = options.globalOutbound ?? null; |
| 204 | const { "executor.js": _, ...safeModules } = options.modules ?? {}; |
| 205 | this.#modules = safeModules; |
| 206 | } |
| 207 | |
| 208 | async execute( |
| 209 | code: string, |
| 210 | providersOrFns: |
| 211 | | ResolvedProvider[] |
| 212 | | Record<string, (...args: unknown[]) => Promise<unknown>> |
| 213 | ): Promise<ExecuteResult> { |
| 214 | // Backwards compat: detect old `execute(code, fns)` signature. |
| 215 | let providers: ResolvedProvider[]; |
| 216 | if (!Array.isArray(providersOrFns)) { |
| 217 | console.warn( |
| 218 | "[@cloudflare/codemode] Passing raw fns to executor.execute() is deprecated. " + |
| 219 | "Use ResolvedProvider[] instead. This will be removed in the next major version." |
| 220 | ); |
| 221 | providers = [{ name: "codemode", fns: providersOrFns }]; |
| 222 | } else { |
| 223 | providers = providersOrFns; |
| 224 | } |
| 225 | |
| 226 | const normalized = normalizeCode(code); |
| 227 | const timeoutMs = this.#timeout; |
| 228 | |
| 229 | // Validate provider names. |
| 230 | const RESERVED_NAMES = new Set(["__dispatchers", "__logs"]); |
| 231 | const VALID_IDENT = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/; |
| 232 | const seenNames = new Set<string>(); |
| 233 | for (const provider of providers) { |
| 234 | if (RESERVED_NAMES.has(provider.name)) { |
| 235 | return { |
| 236 | result: undefined, |
| 237 | error: `Provider name "${provider.name}" is reserved` |
| 238 | }; |
| 239 | } |
| 240 | if (!VALID_IDENT.test(provider.name)) { |
| 241 | return { |
| 242 | result: undefined, |
| 243 | error: `Provider name "${provider.name}" is not a valid JavaScript identifier` |
| 244 | }; |
| 245 | } |
| 246 | if (seenNames.has(provider.name)) { |
| 247 | return { |
| 248 | result: undefined, |
| 249 | error: `Duplicate provider name "${provider.name}"` |
| 250 | }; |
| 251 | } |
nothing calls this directly
no outgoing calls
no test coverage detected