@@ -18,42 +18,49 @@ export const DOContainerOptionsSchema = z.object({
18
18
} ) ;
19
19
export type DOContainerOptions = z . infer < typeof DOContainerOptionsSchema > ;
20
20
21
+ const DurableObjectOptions = z . object ( {
22
+ className : z . string ( ) ,
23
+ scriptName : z . string ( ) . optional ( ) ,
24
+ useSQLite : z . boolean ( ) . optional ( ) ,
25
+ // Allow `uniqueKey` to be customised. We use in Wrangler when setting
26
+ // up stub Durable Objects that proxy requests to Durable Objects in
27
+ // another `workerd` process, to ensure the IDs created by the stub
28
+ // object can be used by the real object too.
29
+ unsafeUniqueKey : z
30
+ . union ( [ z . string ( ) , z . literal ( kUnsafeEphemeralUniqueKey ) ] )
31
+ . optional ( ) ,
32
+ // Prevents the Durable Object being evicted.
33
+ unsafePreventEviction : z . boolean ( ) . optional ( ) ,
34
+ remoteProxyConnectionString : z
35
+ . custom < RemoteProxyConnectionString > ( )
36
+ . optional ( ) ,
37
+ container : z . custom < DOContainerOptions > ( ) . optional ( ) ,
38
+ } ) ;
39
+
21
40
export const DurableObjectsOptionsSchema = z . object ( {
22
41
durableObjects : z
23
- . record (
24
- z . union ( [
25
- z . string ( ) ,
26
- z . object ( {
27
- className : z . string ( ) ,
28
- scriptName : z . string ( ) . optional ( ) ,
29
- useSQLite : z . boolean ( ) . optional ( ) ,
30
- // Allow `uniqueKey` to be customised. We use in Wrangler when setting
31
- // up stub Durable Objects that proxy requests to Durable Objects in
32
- // another `workerd` process, to ensure the IDs created by the stub
33
- // object can be used by the real object too.
34
- unsafeUniqueKey : z
35
- . union ( [ z . string ( ) , z . literal ( kUnsafeEphemeralUniqueKey ) ] )
36
- . optional ( ) ,
37
- // Prevents the Durable Object being evicted.
38
- unsafePreventEviction : z . boolean ( ) . optional ( ) ,
39
- remoteProxyConnectionString : z
40
- . custom < RemoteProxyConnectionString > ( )
41
- . optional ( ) ,
42
- container : z . custom < DOContainerOptions > ( ) . optional ( ) ,
43
- } ) ,
44
- ] )
45
- )
42
+ . union ( [
43
+ z . array (
44
+ DurableObjectOptions . extend ( {
45
+ // What binding name should this DO have? This is optional because not all DOs are configured as bindings
46
+ // Some might just be configured via migrations, but should still be allocated storage for e..g ctx.exports support
47
+ binding : z . string ( ) . optional ( ) ,
48
+ } )
49
+ ) ,
50
+ z . record ( z . union ( [ z . string ( ) , DurableObjectOptions ] ) ) ,
51
+ ] )
46
52
. optional ( ) ,
47
53
} ) ;
48
54
export const DurableObjectsSharedOptionsSchema = z . object ( {
49
55
durableObjectsPersist : PersistenceSchema ,
50
56
} ) ;
51
57
52
- export function normaliseDurableObject (
53
- designator : NonNullable <
58
+ export function normaliseDurableObjects (
59
+ durableObjects : NonNullable <
54
60
z . infer < typeof DurableObjectsOptionsSchema > [ "durableObjects" ]
55
- > [ string ]
61
+ >
56
62
) : {
63
+ binding : string | undefined ;
57
64
className : string ;
58
65
scriptName : string | undefined ;
59
66
serviceName : string | undefined ;
@@ -62,33 +69,43 @@ export function normaliseDurableObject(
62
69
unsafePreventEviction : boolean | undefined ;
63
70
remoteProxyConnectionString : RemoteProxyConnectionString | undefined ;
64
71
container : DOContainerOptions | undefined ;
65
- } {
66
- const isObject = typeof designator === "object" ;
67
- const className = isObject ? designator . className : designator ;
68
- const scriptName =
69
- isObject && designator . scriptName !== undefined
70
- ? designator . scriptName
72
+ } [ ] {
73
+ let normalised : [
74
+ string | undefined ,
75
+ z . infer < typeof DurableObjectOptions > | string ,
76
+ ] [ ] = Array . isArray ( durableObjects )
77
+ ? durableObjects . map ( ( d ) => [ d . binding , d ] )
78
+ : Object . entries ( durableObjects ) ;
79
+
80
+ return normalised . map ( ( [ binding , designator ] ) => {
81
+ const isObject = typeof designator === "object" ;
82
+ const className = isObject ? designator . className : designator ;
83
+ const scriptName =
84
+ isObject && designator . scriptName !== undefined
85
+ ? designator . scriptName
86
+ : undefined ;
87
+ const serviceName = scriptName ? getUserServiceName ( scriptName ) : undefined ;
88
+ const enableSql = isObject ? designator . useSQLite : undefined ;
89
+ const unsafeUniqueKey = isObject ? designator . unsafeUniqueKey : undefined ;
90
+ const unsafePreventEviction = isObject
91
+ ? designator . unsafePreventEviction
92
+ : undefined ;
93
+ const remoteProxyConnectionString = isObject
94
+ ? designator . remoteProxyConnectionString
71
95
: undefined ;
72
- const serviceName = scriptName ? getUserServiceName ( scriptName ) : undefined ;
73
- const enableSql = isObject ? designator . useSQLite : undefined ;
74
- const unsafeUniqueKey = isObject ? designator . unsafeUniqueKey : undefined ;
75
- const unsafePreventEviction = isObject
76
- ? designator . unsafePreventEviction
77
- : undefined ;
78
- const remoteProxyConnectionString = isObject
79
- ? designator . remoteProxyConnectionString
80
- : undefined ;
81
- const container = isObject ? designator . container : undefined ;
82
- return {
83
- className,
84
- scriptName,
85
- serviceName,
86
- enableSql,
87
- unsafeUniqueKey,
88
- unsafePreventEviction,
89
- remoteProxyConnectionString,
90
- container,
91
- } ;
96
+ const container = isObject ? designator . container : undefined ;
97
+ return {
98
+ binding,
99
+ className,
100
+ scriptName,
101
+ serviceName,
102
+ enableSql,
103
+ unsafeUniqueKey,
104
+ unsafePreventEviction,
105
+ remoteProxyConnectionString,
106
+ container,
107
+ } ;
108
+ } ) ;
92
109
}
93
110
94
111
export const DURABLE_OBJECTS_PLUGIN_NAME = "do" ;
@@ -102,15 +119,14 @@ export const DURABLE_OBJECTS_PLUGIN: Plugin<
102
119
options : DurableObjectsOptionsSchema ,
103
120
sharedOptions : DurableObjectsSharedOptionsSchema ,
104
121
getBindings ( options ) {
105
- return Object . entries ( options . durableObjects ?? { } ) . map < Worker_Binding > (
106
- ( [ name , klass ] ) => {
107
- const { className, serviceName } = normaliseDurableObject ( klass ) ;
122
+ return normaliseDurableObjects ( options . durableObjects ?? { } )
123
+ . filter ( ( d ) => d . binding )
124
+ . map < Worker_Binding > ( ( { binding , className, serviceName } ) => {
108
125
return {
109
- name,
126
+ name : binding ,
110
127
durableObjectNamespace : { className, serviceName } ,
111
128
} ;
112
- }
113
- ) ;
129
+ } ) ;
114
130
} ,
115
131
getNodeBindings ( options ) {
116
132
const objects = Object . keys ( options . durableObjects ?? { } ) ;
0 commit comments