|
| 1 | +import assert from "node:assert"; |
| 2 | +import SCRIPT_LOCAL_EXPLORER from "worker:local-explorer/explorer"; |
| 3 | +import { Service, Worker_Binding } from "../../runtime"; |
| 4 | +import { OUTBOUND_DO_PROXY_SERVICE_NAME } from "../../shared/external-service"; |
| 5 | +import { CoreBindings } from "../../workers"; |
| 6 | +import { |
| 7 | + DurableObjectClassNames, |
| 8 | + WORKER_BINDING_SERVICE_LOOPBACK, |
| 9 | +} from "../shared"; |
| 10 | +import { |
| 11 | + getUserServiceName, |
| 12 | + LOCAL_EXPLORER_DISK, |
| 13 | + SERVICE_LOCAL_EXPLORER, |
| 14 | +} from "./constants"; |
| 15 | +import type { BindingIdMap } from "./types"; |
| 16 | + |
| 17 | +export interface ExplorerServicesOptions { |
| 18 | + localExplorerUiPath: string; |
| 19 | + proxyBindings: Worker_Binding[]; |
| 20 | + bindingIdMap: BindingIdMap; |
| 21 | + hasDurableObjects: boolean; |
| 22 | +} |
| 23 | + |
| 24 | +/** |
| 25 | + * Creates the services needed for the local explorer feature. |
| 26 | + */ |
| 27 | +export function getExplorerServices( |
| 28 | + options: ExplorerServicesOptions |
| 29 | +): Service[] { |
| 30 | + const { |
| 31 | + localExplorerUiPath, |
| 32 | + proxyBindings, |
| 33 | + bindingIdMap, |
| 34 | + hasDurableObjects, |
| 35 | + } = options; |
| 36 | + |
| 37 | + const explorerBindings: Worker_Binding[] = [ |
| 38 | + // Gives explorer access to all user resource bindings |
| 39 | + ...proxyBindings, |
| 40 | + { |
| 41 | + name: CoreBindings.JSON_LOCAL_EXPLORER_BINDING_MAP, |
| 42 | + json: JSON.stringify(bindingIdMap), |
| 43 | + }, |
| 44 | + { |
| 45 | + name: CoreBindings.EXPLORER_DISK, |
| 46 | + service: { name: LOCAL_EXPLORER_DISK }, |
| 47 | + }, |
| 48 | + ]; |
| 49 | + |
| 50 | + if (hasDurableObjects) { |
| 51 | + // Add loopback service binding if DOs are configured |
| 52 | + // The explorer worker uses this to call the /core/do-storage endpoint |
| 53 | + // which reads the filesystem using Node.js (bypassing workerd disk service issues on Windows) |
| 54 | + explorerBindings.push(WORKER_BINDING_SERVICE_LOOPBACK); |
| 55 | + |
| 56 | + // Add Durable Object namespace bindings for the explorer |
| 57 | + // Yes we are binding to 'unbound' DOs, but that has no effect |
| 58 | + // on the user's access via ctx.exports |
| 59 | + for (const durableObject of Object.values(bindingIdMap.do)) { |
| 60 | + explorerBindings.push({ |
| 61 | + name: durableObject.binding, |
| 62 | + durableObjectNamespace: { |
| 63 | + className: durableObject.className, |
| 64 | + serviceName: getUserServiceName(durableObject.scriptName), |
| 65 | + }, |
| 66 | + }); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + return [ |
| 71 | + // Disk service for serving explorer UI assets |
| 72 | + { |
| 73 | + name: LOCAL_EXPLORER_DISK, |
| 74 | + disk: { path: localExplorerUiPath, writable: false }, |
| 75 | + }, |
| 76 | + { |
| 77 | + name: SERVICE_LOCAL_EXPLORER, |
| 78 | + worker: { |
| 79 | + compatibilityDate: "2026-01-01", |
| 80 | + compatibilityFlags: ["nodejs_compat"], |
| 81 | + modules: [ |
| 82 | + { |
| 83 | + name: "explorer.worker.js", |
| 84 | + esModule: SCRIPT_LOCAL_EXPLORER(), |
| 85 | + }, |
| 86 | + ], |
| 87 | + bindings: explorerBindings, |
| 88 | + }, |
| 89 | + }, |
| 90 | + ]; |
| 91 | +} |
| 92 | + |
| 93 | +/** |
| 94 | + * Build binding ID map from proxyBindings and durableObjectClassNames |
| 95 | + * Maps resource IDs to binding information for the local explorer |
| 96 | + */ |
| 97 | +export function constructExplorerBindingMap( |
| 98 | + proxyBindings: Worker_Binding[], |
| 99 | + durableObjectClassNames: DurableObjectClassNames |
| 100 | +): BindingIdMap { |
| 101 | + const IDToBindingName: BindingIdMap = { |
| 102 | + d1: {}, |
| 103 | + kv: {}, |
| 104 | + do: {}, |
| 105 | + }; |
| 106 | + |
| 107 | + for (const binding of proxyBindings) { |
| 108 | + // D1 bindings: name = "MINIFLARE_PROXY:d1:worker-*:BINDING", wrapped.innerBindings[0].service.name = "d1:db:ID" |
| 109 | + if ( |
| 110 | + binding.name?.startsWith( |
| 111 | + `${CoreBindings.DURABLE_OBJECT_NAMESPACE_PROXY}:d1:` |
| 112 | + ) && |
| 113 | + "wrapped" in binding |
| 114 | + ) { |
| 115 | + const [innerBinding] = binding.wrapped?.innerBindings ?? []; |
| 116 | + assert(innerBinding && "service" in innerBinding); |
| 117 | + |
| 118 | + const databaseId = innerBinding.service?.name?.replace(/^d1:db:/, ""); |
| 119 | + assert(databaseId); |
| 120 | + |
| 121 | + IDToBindingName.d1[databaseId] = binding.name; |
| 122 | + } |
| 123 | + |
| 124 | + // KV bindings: name = "MINIFLARE_PROXY:kv:worker:BINDING", kvNamespace.name = "kv:ns:ID" |
| 125 | + if ( |
| 126 | + binding.name?.startsWith( |
| 127 | + `${CoreBindings.DURABLE_OBJECT_NAMESPACE_PROXY}:kv:` |
| 128 | + ) && |
| 129 | + "kvNamespace" in binding && |
| 130 | + binding.kvNamespace?.name?.startsWith("kv:ns:") |
| 131 | + ) { |
| 132 | + // Extract ID from service name "kv:ns:ID" |
| 133 | + const namespaceId = binding.kvNamespace.name.replace(/^kv:ns:/, ""); |
| 134 | + IDToBindingName.kv[namespaceId] = binding.name; |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + // Handle DOs separately, since we need more information than is |
| 139 | + // present in proxy bindings. |
| 140 | + // durableObjectClassNames includes internal and unbound DOs, |
| 141 | + // but not external ones, which we don't want anyway. |
| 142 | + for (const [serviceName, classMap] of durableObjectClassNames) { |
| 143 | + // Skip the outbound DO proxy service (used for external DOs) |
| 144 | + if (serviceName === getUserServiceName(OUTBOUND_DO_PROXY_SERVICE_NAME)) { |
| 145 | + continue; |
| 146 | + } |
| 147 | + const scriptName = serviceName.replace(/^core:user:/, ""); |
| 148 | + for (const [className, classInfo] of classMap) { |
| 149 | + const uniqueKey = `${scriptName}-${className}`; |
| 150 | + IDToBindingName.do[uniqueKey] = { |
| 151 | + className, |
| 152 | + scriptName, |
| 153 | + useSQLite: classInfo.enableSql ?? false, |
| 154 | + binding: `EXPLORER_DO_${uniqueKey}`, |
| 155 | + }; |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + return IDToBindingName; |
| 160 | +} |
0 commit comments