1
1
import assert from "assert" ;
2
2
import { readFileSync } from "fs" ;
3
3
import fs from "fs/promises" ;
4
+ import { platform } from "node:os" ;
4
5
import path from "path" ;
5
6
import { Readable } from "stream" ;
6
7
import tls from "tls" ;
@@ -18,6 +19,7 @@ import {
18
19
ServiceDesignator ,
19
20
supportedCompatibilityDate ,
20
21
Worker_Binding ,
22
+ Worker_ContainerEngine ,
21
23
Worker_DurableObjectNamespace ,
22
24
Worker_Module ,
23
25
} from "../../runtime" ;
@@ -166,6 +168,16 @@ const CoreOptionsSchemaInput = z.intersection(
166
168
167
169
// Strip the CF-Connecting-IP header from outbound fetches
168
170
stripCfConnectingIp : z . boolean ( ) . default ( true ) ,
171
+
172
+ /** Configuration used to connect to the container engine */
173
+ containerEngine : z
174
+ . union ( [
175
+ z . object ( {
176
+ localDocker : z . object ( { socketPath : z . string ( ) } ) ,
177
+ } ) ,
178
+ z . string ( ) ,
179
+ ] )
180
+ . optional ( ) ,
169
181
} )
170
182
) ;
171
183
export const CoreOptionsSchema = CoreOptionsSchemaInput . transform ( ( value ) => {
@@ -736,28 +748,32 @@ export const CORE_PLUGIN: Plugin<
736
748
classNamesEntries . map < Worker_DurableObjectNamespace > (
737
749
( [
738
750
className ,
739
- { enableSql, unsafeUniqueKey, unsafePreventEviction } ,
740
- ] ) => {
741
- if ( unsafeUniqueKey === kUnsafeEphemeralUniqueKey ) {
742
- return {
743
- className,
744
- enableSql,
745
- ephemeralLocal : kVoid ,
746
- preventEviction : unsafePreventEviction ,
747
- } ;
748
- } else {
749
- return {
750
- className,
751
- enableSql,
752
- // This `uniqueKey` will (among other things) be used as part of the
753
- // path when persisting to the file-system. `-` is invalid in
754
- // JavaScript class names, but safe on filesystems (incl. Windows).
755
- uniqueKey :
756
- unsafeUniqueKey ?? `${ options . name ?? "" } -${ className } ` ,
757
- preventEviction : unsafePreventEviction ,
758
- } ;
759
- }
760
- }
751
+ {
752
+ enableSql,
753
+ unsafeUniqueKey,
754
+ unsafePreventEviction : preventEviction ,
755
+ container,
756
+ } ,
757
+ ] ) =>
758
+ unsafeUniqueKey === kUnsafeEphemeralUniqueKey
759
+ ? {
760
+ className,
761
+ enableSql,
762
+ ephemeralLocal : kVoid ,
763
+ preventEviction,
764
+ container,
765
+ }
766
+ : {
767
+ className,
768
+ enableSql,
769
+ // This `uniqueKey` will (among other things) be used as part of the
770
+ // path when persisting to the file-system. `-` is invalid in
771
+ // JavaScript class names, but safe on filesystems (incl. Windows).
772
+ uniqueKey :
773
+ unsafeUniqueKey ?? `${ options . name ?? "" } -${ className } ` ,
774
+ preventEviction,
775
+ container,
776
+ }
761
777
) ,
762
778
durableObjectStorage :
763
779
classNamesEntries . length === 0
@@ -787,6 +803,7 @@ export const CORE_PLUGIN: Plugin<
787
803
options . hasAssetsAndIsVitest
788
804
) ;
789
805
} ) ,
806
+ containerEngine : getContainerEngine ( options . containerEngine ) ,
790
807
} ,
791
808
} ) ;
792
809
}
@@ -1022,6 +1039,29 @@ function getWorkerScript(
1022
1039
}
1023
1040
}
1024
1041
1042
+ /**
1043
+ * Returns the Container engine configuration
1044
+ * @param engineOrSocketPath Either a full engine config or a unix socket
1045
+ * @returns The container engine, default to local Docker at `unix:/var/run/docker.sock`
1046
+ */
1047
+ function getContainerEngine (
1048
+ engineOrSocketPath : Worker_ContainerEngine | string | undefined
1049
+ ) : Worker_ContainerEngine {
1050
+ if ( ! engineOrSocketPath ) {
1051
+ // TODO: workerd does not support win named pipes
1052
+ engineOrSocketPath =
1053
+ platform ( ) === "win32"
1054
+ ? "//./pipe/docker_engine"
1055
+ : "unix:/var/run/docker.sock" ;
1056
+ }
1057
+
1058
+ if ( typeof engineOrSocketPath === "string" ) {
1059
+ return { localDocker : { socketPath : engineOrSocketPath } } ;
1060
+ }
1061
+
1062
+ return engineOrSocketPath ;
1063
+ }
1064
+
1025
1065
export * from "./errors" ;
1026
1066
export * from "./proxy" ;
1027
1067
export * from "./constants" ;
0 commit comments