@@ -2,6 +2,7 @@ import assert from "node:assert";
22import { readFile } from "node:fs/promises" ;
33import { assertNever } from "../../utils/assert-never" ;
44import type { ConfigBindingOptions } from "../../config" ;
5+ import type { WorkerMetadataBinding } from "../../deployment-bundle/create-worker-upload-form" ;
56import type { CfWorkerInit } from "../../deployment-bundle/worker" ;
67import type {
78 Binding ,
@@ -308,8 +309,22 @@ export function convertCfWorkerInitBindingsToBindings(
308309 return output ;
309310}
310311
312+ /**
313+ * Convert either StartDevWorkerOptions["bindings"] or WorkerMetadataBinding[] to CfWorkerInit["bindings"]
314+ * This function is by design temporary, but has lived longer than originally expected.
315+ * For some context, CfWorkerInit is the in-memory representation of a Worker that Wrangler uses,
316+ * WorkerMetadataBinding is the representation of bindings that comes from the API, and StartDevWorkerOptions
317+ * is the "new" in-memory representation of a Worker that's used in Wrangler's dev flow. Over
318+ * time, all uses of CfWorkerInit should transition to StartDevWorkerOptions, but that's a pretty big refactor.
319+ * As such, in the meantime we have conversion functions so that different code paths can deal with the format they
320+ * expect and were written for.
321+ *
322+ * WARNING: Using this with WorkerMetadataBinding[] will lose information about certain
323+ * binding types (i.e. WASM modules, text blobs, and data blobs). These binding types are deprecated
324+ * but may still be used by some Workers in the wild.
325+ */
311326export async function convertBindingsToCfWorkerInitBindings (
312- inputBindings : StartDevWorkerOptions [ "bindings" ]
327+ inputBindings : StartDevWorkerOptions [ "bindings" ] | WorkerMetadataBinding [ ]
313328) : Promise < {
314329 bindings : CfWorkerInit [ "bindings" ] ;
315330 fetchers : Record < string , ServiceFetch > ;
@@ -349,23 +364,35 @@ export async function convertBindingsToCfWorkerInitBindings(
349364
350365 const fetchers : Record < string , ServiceFetch > = { } ;
351366
352- for ( const [ name , binding ] of Object . entries ( inputBindings ?? { } ) ) {
367+ const iterator : [ string , WorkerMetadataBinding | Binding ] [ ] = Array . isArray (
368+ inputBindings
369+ )
370+ ? inputBindings . map ( ( b ) => [ b . name , b ] )
371+ : Object . entries ( inputBindings ?? { } ) ;
372+
373+ for ( const [ name , binding ] of iterator ) {
353374 if ( binding . type === "plain_text" ) {
354375 bindings . vars ??= { } ;
355- bindings . vars [ name ] = binding . value ;
376+ bindings . vars [ name ] = "value" in binding ? binding . value : binding . text ;
356377 } else if ( binding . type === "json" ) {
357378 bindings . vars ??= { } ;
358- bindings . vars [ name ] = binding . value ;
379+ bindings . vars [ name ] = "value" in binding ? binding . value : binding . json ;
359380 } else if ( binding . type === "kv_namespace" ) {
360381 bindings . kv_namespaces ??= [ ] ;
361382 bindings . kv_namespaces . push ( { ...binding , binding : name } ) ;
362383 } else if ( binding . type === "send_email" ) {
363384 bindings . send_email ??= [ ] ;
364385 bindings . send_email . push ( { ...binding , name : name } ) ;
365386 } else if ( binding . type === "wasm_module" ) {
387+ if ( ! ( "source" in binding ) ) {
388+ continue ;
389+ }
366390 bindings . wasm_modules ??= { } ;
367391 bindings . wasm_modules [ name ] = await getBinaryFileContents ( binding . source ) ;
368392 } else if ( binding . type === "text_blob" ) {
393+ if ( ! ( "source" in binding ) ) {
394+ continue ;
395+ }
369396 bindings . text_blobs ??= { } ;
370397
371398 if ( typeof binding . source . path === "string" ) {
@@ -377,6 +404,9 @@ export async function convertBindingsToCfWorkerInitBindings(
377404 ) ;
378405 }
379406 } else if ( binding . type === "data_blob" ) {
407+ if ( ! ( "source" in binding ) ) {
408+ continue ;
409+ }
380410 bindings . data_blobs ??= { } ;
381411 bindings . data_blobs [ name ] = await getBinaryFileContents ( binding . source ) ;
382412 } else if ( binding . type === "browser" ) {
@@ -415,7 +445,14 @@ export async function convertBindingsToCfWorkerInitBindings(
415445 bindings . analytics_engine_datasets . push ( { ...binding , binding : name } ) ;
416446 } else if ( binding . type === "dispatch_namespace" ) {
417447 bindings . dispatch_namespaces ??= [ ] ;
418- bindings . dispatch_namespaces . push ( { ...binding , binding : name } ) ;
448+ bindings . dispatch_namespaces . push ( {
449+ ...binding ,
450+ binding : name ,
451+ outbound :
452+ binding . outbound && "worker" in binding . outbound
453+ ? undefined
454+ : binding . outbound ,
455+ } ) ;
419456 } else if ( binding . type === "mtls_certificate" ) {
420457 bindings . mtls_certificates ??= [ ] ;
421458 bindings . mtls_certificates . push ( { ...binding , binding : name } ) ;
0 commit comments