@@ -20,7 +20,7 @@ export type Entry = { file: string; directory: string; format: CfScriptFormat };
20
20
export async function getEntry (
21
21
args : { script ?: string ; format ?: CfScriptFormat | undefined } ,
22
22
config : Config ,
23
- command : string
23
+ command : "dev" | "publish"
24
24
) : Promise < Entry > {
25
25
let file : string ;
26
26
let directory = process . cwd ( ) ;
@@ -49,12 +49,24 @@ export async function getEntry(
49
49
args . format ?? config . build ?. upload ?. format
50
50
) ;
51
51
52
- if ( format === "service-worker" && hasDurableObjectImplementations ( config ) ) {
52
+ const { localBindings, remoteBindings } =
53
+ partitionDurableObjectBindings ( config ) ;
54
+
55
+ if ( command === "dev" && remoteBindings . length > 0 ) {
56
+ console . warn (
57
+ "WARNING: You have Durable Object bindings, which are not defined locally in the worker being developed.\n" +
58
+ "Be aware that changes to the data stored in these Durable Objects will be permanent and affect the live instances.\n" +
59
+ "Remote Durable Objects that are affected:\n" +
60
+ remoteBindings . map ( ( b ) => `- ${ JSON . stringify ( b ) } ` ) . join ( "\n" )
61
+ ) ;
62
+ }
63
+
64
+ if ( format === "service-worker" && localBindings . length > 0 ) {
53
65
const errorMessage =
54
66
"You seem to be trying to use Durable Objects in a Worker written with Service Worker syntax." ;
55
67
const addScriptName =
56
68
"You can use Durable Objects defined in other Workers by specifying a `script_name` in your wrangler.toml, where `script_name` is the name of the Worker that implements that Durable Object. For example:" ;
57
- const addScriptNameExamples = generateAddScriptNameExamples ( config ) ;
69
+ const addScriptNameExamples = generateAddScriptNameExamples ( localBindings ) ;
58
70
const migrateText =
59
71
"Alternatively, migrate your worker to ES Module syntax to implement a Durable Object in this Worker:" ;
60
72
const migrateUrl =
@@ -174,29 +186,41 @@ export function fileExists(filePath: string): boolean {
174
186
return false ;
175
187
}
176
188
189
+ type DurableObjectBindings = Config [ "durable_objects" ] [ "bindings" ] ;
190
+
177
191
/**
178
- * Returns true if the given config contains Durable Object bindings that are implemented
179
- * in this worker instead of being implemented elsewhere, and bound via a `script_name`
180
- * property in wrangler.toml
192
+ * Groups the durable object bindings into two lists:
193
+ * those that are defined locally and those that refer to a durable object defined in another script.
181
194
*/
182
- function hasDurableObjectImplementations ( config : Config ) : boolean {
183
- return config . durable_objects . bindings . some (
184
- ( binding ) => binding . script_name === undefined
185
- ) ;
195
+ function partitionDurableObjectBindings ( config : Config ) : {
196
+ localBindings : DurableObjectBindings ;
197
+ remoteBindings : DurableObjectBindings ;
198
+ } {
199
+ const localBindings : DurableObjectBindings = [ ] ;
200
+ const remoteBindings : DurableObjectBindings = [ ] ;
201
+ for ( const binding of config . durable_objects . bindings ) {
202
+ if ( binding . script_name === undefined ) {
203
+ localBindings . push ( binding ) ;
204
+ } else {
205
+ remoteBindings . push ( binding ) ;
206
+ }
207
+ }
208
+ return { localBindings, remoteBindings } ;
186
209
}
187
210
188
211
/**
189
212
* Generates some help text based on the Durable Object bindings in a given
190
213
* config indicating how the user can add a `script_name` field to bind an
191
214
* externally defined Durable Object.
192
215
*/
193
- function generateAddScriptNameExamples ( config : Config ) : string {
216
+ function generateAddScriptNameExamples (
217
+ localBindings : DurableObjectBindings
218
+ ) : string {
194
219
function exampleScriptName ( binding_name : string ) : string {
195
220
return `${ binding_name . toLowerCase ( ) . replaceAll ( "_" , "-" ) } -worker` ;
196
221
}
197
222
198
- return config . durable_objects . bindings
199
- . filter ( ( binding ) => binding . script_name === undefined )
223
+ return localBindings
200
224
. map ( ( { name, class_name } ) => {
201
225
const script_name = exampleScriptName ( name ) ;
202
226
const currentBinding = `{ name = ${ name } , class_name = ${ class_name } }` ;
0 commit comments