@@ -11,15 +11,16 @@ import {
1111 buildMiniflareBindingOptions ,
1212 buildSitesOptions ,
1313} from "../../../dev/miniflare" ;
14- import { run } from "../../../experimental-flags" ;
1514import { logger } from "../../../logger" ;
1615import { getSiteAssetPaths } from "../../../sites" ;
1716import { dedent } from "../../../utils/dedent" ;
17+ import { maybeStartOrUpdateRemoteProxySession } from "../../remoteBindings" ;
1818import { CacheStorage } from "./caches" ;
1919import { ExecutionContext } from "./executionContext" ;
2020import { getServiceBindings } from "./services" ;
2121import type { AssetsOptions } from "../../../assets" ;
2222import type { Config , RawConfig , RawEnvironment } from "../../../config" ;
23+ import type { RemoteProxySession } from "../../remoteBindings" ;
2324import type { IncomingRequestCfProperties } from "@cloudflare/workers-types/experimental" ;
2425import type {
2526 MiniflareOptions ,
@@ -58,6 +59,13 @@ export type GetPlatformProxyOptions = {
5859 * If `false` is specified no data is persisted on the filesystem.
5960 */
6061 persist ?: boolean | { path : string } ;
62+ /**
63+ * Experimental flags (note: these can change at any time and are not version-controlled use at your own risk)
64+ */
65+ experimental ?: {
66+ /** whether access to remove bindings should be enabled */
67+ remoteBindings ?: boolean ;
68+ } ;
6169} ;
6270
6371/**
@@ -103,29 +111,32 @@ export async function getPlatformProxy<
103111> (
104112 options : GetPlatformProxyOptions = { }
105113) : Promise < PlatformProxy < Env , CfProperties > > {
114+ const experimentalRemoteBindings = ! ! options . experimental ?. remoteBindings ;
115+
106116 const env = options . environment ;
107117
108- const rawConfig = readConfig ( {
118+ const config = readConfig ( {
109119 config : options . configPath ,
110120 env,
111121 } ) ;
112122
113- const miniflareOptions = await run (
114- {
115- MULTIWORKER : false ,
116- RESOURCES_PROVISION : false ,
117- // TODO: when possible remote bindings should be made available for getPlatformProxy
118- REMOTE_BINDINGS : false ,
119- } ,
120- ( ) => getMiniflareOptionsFromConfig ( rawConfig , env , options )
121- ) ;
123+ let remoteProxySession : RemoteProxySession | undefined = undefined ;
124+ if ( experimentalRemoteBindings && config . configPath ) {
125+ remoteProxySession = (
126+ ( await maybeStartOrUpdateRemoteProxySession ( config . configPath ) ) ?? { }
127+ ) . session ;
128+ }
122129
123- const mf = new Miniflare ( {
124- script : "" ,
125- modules : true ,
126- ...( miniflareOptions as Record < string , unknown > ) ,
130+ const miniflareOptions = await getMiniflareOptionsFromConfig ( {
131+ config,
132+ options,
133+ remoteProxyConnectionString :
134+ remoteProxySession ?. remoteProxyConnectionString ,
135+ remoteBindingsEnabled : experimentalRemoteBindings ,
127136 } ) ;
128137
138+ const mf = new Miniflare ( miniflareOptions ) ;
139+
129140 const bindings : Env = await mf . getBindings ( ) ;
130141
131142 const cf = await mf . getCf ( ) ;
@@ -136,20 +147,40 @@ export async function getPlatformProxy<
136147 cf : cf as CfProperties ,
137148 ctx : new ExecutionContext ( ) ,
138149 caches : new CacheStorage ( ) ,
139- dispose : ( ) => mf . dispose ( ) ,
150+ dispose : async ( ) => {
151+ await remoteProxySession ?. dispose ( ) ;
152+ await mf . dispose ( ) ;
153+ } ,
140154 } ;
141155}
142156
143- // this is only used by getPlatformProxy
144- async function getMiniflareOptionsFromConfig (
145- rawConfig : Config ,
146- env : string | undefined ,
147- options : GetPlatformProxyOptions
148- ) : Promise < Partial < MiniflareOptions > > {
149- const bindings = getBindings ( rawConfig , env , true , { } ) ;
157+ /**
158+ * Builds an options configuration object for the `getPlatformProxy` functionality that
159+ * can be then passed to the Miniflare constructor
160+ *
161+ * @param args.config The wrangler configuration to base the options from
162+ * @param args.options The user provided `getPlatformProxy` options
163+ * @param args.remoteProxyConnectionString The potential remote proxy connection string to be used to connect the remote bindings
164+ * @param args.remoteBindingsEnabled Whether remote bindings are enabled
165+ * @returns an object ready to be passed to the Miniflare constructor
166+ */
167+ async function getMiniflareOptionsFromConfig ( args : {
168+ config : Config ;
169+ options : GetPlatformProxyOptions ;
170+ remoteProxyConnectionString ?: RemoteProxyConnectionString ;
171+ remoteBindingsEnabled : boolean ;
172+ } ) : Promise < MiniflareOptions > {
173+ const {
174+ config,
175+ options,
176+ remoteProxyConnectionString,
177+ remoteBindingsEnabled,
178+ } = args ;
179+
180+ const bindings = getBindings ( config , options . environment , true , { } ) ;
150181
151- if ( rawConfig [ "durable_objects" ] ) {
152- const { localBindings } = partitionDurableObjectBindings ( rawConfig ) ;
182+ if ( config [ "durable_objects" ] ) {
183+ const { localBindings } = partitionDurableObjectBindings ( config ) ;
153184 if ( localBindings . length > 0 ) {
154185 logger . warn ( dedent `
155186 You have defined bindings to the following internal Durable Objects:
@@ -162,29 +193,29 @@ async function getMiniflareOptionsFromConfig(
162193 }
163194 }
164195 const workerDefinitions = await getBoundRegisteredWorkers ( {
165- name : rawConfig . name ,
196+ name : config . name ,
166197 services : bindings . services ,
167- durableObjects : rawConfig [ "durable_objects" ] ,
198+ durableObjects : config [ "durable_objects" ] ,
168199 tailConsumers : [ ] ,
169200 } ) ;
170201
171202 const { bindingOptions, externalWorkers } = buildMiniflareBindingOptions (
172203 {
173- name : rawConfig . name ,
174- complianceRegion : rawConfig . compliance_region ,
204+ name : config . name ,
205+ complianceRegion : config . compliance_region ,
175206 bindings,
176207 workerDefinitions,
177208 queueConsumers : undefined ,
178- services : rawConfig . services ,
209+ services : config . services ,
179210 serviceBindings : { } ,
180- migrations : rawConfig . migrations ,
211+ migrations : config . migrations ,
181212 imagesLocalMode : false ,
182213 tails : [ ] ,
183214 containers : undefined ,
184215 containerBuildId : undefined ,
185216 } ,
186- undefined ,
187- false
217+ remoteProxyConnectionString ,
218+ remoteBindingsEnabled
188219 ) ;
189220
190221 const defaultPersistRoot = getMiniflarePersistRoot ( options . persist ) ;
@@ -196,7 +227,7 @@ async function getMiniflareOptionsFromConfig(
196227 {
197228 script : "" ,
198229 modules : true ,
199- name : rawConfig . name ,
230+ name : config . name ,
200231 ...bindingOptions ,
201232 serviceBindings : {
202233 ...serviceBindings ,
@@ -208,7 +239,11 @@ async function getMiniflareOptionsFromConfig(
208239 defaultPersistRoot,
209240 } ;
210241
211- return miniflareOptions ;
242+ return {
243+ script : "" ,
244+ modules : true ,
245+ ...miniflareOptions ,
246+ } ;
212247}
213248
214249/**
0 commit comments