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" ;
@@ -168,6 +170,16 @@ const CoreOptionsSchemaInput = z.intersection(
168
170
// There is an issue with the connect() API and the globalOutbound workerd setting that impacts TCP ingress
169
171
// We should default it to true once https://github.com/cloudflare/workerd/pull/4145 is resolved
170
172
stripCfConnectingIp : z . boolean ( ) . default ( false ) ,
173
+
174
+ /** Configuration used to connect to the container engine */
175
+ containerEngine : z
176
+ . union ( [
177
+ z . object ( {
178
+ localDocker : z . object ( { socketPath : z . string ( ) } ) ,
179
+ } ) ,
180
+ z . string ( ) ,
181
+ ] )
182
+ . optional ( ) ,
171
183
} )
172
184
) ;
173
185
export const CoreOptionsSchema = CoreOptionsSchemaInput . transform ( ( value ) => {
@@ -738,28 +750,32 @@ export const CORE_PLUGIN: Plugin<
738
750
classNamesEntries . map < Worker_DurableObjectNamespace > (
739
751
( [
740
752
className ,
741
- { enableSql, unsafeUniqueKey, unsafePreventEviction } ,
742
- ] ) => {
743
- if ( unsafeUniqueKey === kUnsafeEphemeralUniqueKey ) {
744
- return {
745
- className,
746
- enableSql,
747
- ephemeralLocal : kVoid ,
748
- preventEviction : unsafePreventEviction ,
749
- } ;
750
- } else {
751
- return {
752
- className,
753
- enableSql,
754
- // This `uniqueKey` will (among other things) be used as part of the
755
- // path when persisting to the file-system. `-` is invalid in
756
- // JavaScript class names, but safe on filesystems (incl. Windows).
757
- uniqueKey :
758
- unsafeUniqueKey ?? `${ options . name ?? "" } -${ className } ` ,
759
- preventEviction : unsafePreventEviction ,
760
- } ;
761
- }
762
- }
753
+ {
754
+ enableSql,
755
+ unsafeUniqueKey,
756
+ unsafePreventEviction : preventEviction ,
757
+ container,
758
+ } ,
759
+ ] ) =>
760
+ unsafeUniqueKey === kUnsafeEphemeralUniqueKey
761
+ ? {
762
+ className,
763
+ enableSql,
764
+ ephemeralLocal : kVoid ,
765
+ preventEviction,
766
+ container,
767
+ }
768
+ : {
769
+ className,
770
+ enableSql,
771
+ // This `uniqueKey` will (among other things) be used as part of the
772
+ // path when persisting to the file-system. `-` is invalid in
773
+ // JavaScript class names, but safe on filesystems (incl. Windows).
774
+ uniqueKey :
775
+ unsafeUniqueKey ?? `${ options . name ?? "" } -${ className } ` ,
776
+ preventEviction,
777
+ container,
778
+ }
763
779
) ,
764
780
durableObjectStorage :
765
781
classNamesEntries . length === 0
@@ -789,6 +805,7 @@ export const CORE_PLUGIN: Plugin<
789
805
options . hasAssetsAndIsVitest
790
806
) ;
791
807
} ) ,
808
+ containerEngine : getContainerEngine ( options . containerEngine ) ,
792
809
} ,
793
810
} ) ;
794
811
}
@@ -1023,6 +1040,29 @@ function getWorkerScript(
1023
1040
}
1024
1041
}
1025
1042
1043
+ /**
1044
+ * Returns the Container engine configuration
1045
+ * @param engineOrSocketPath Either a full engine config or a unix socket
1046
+ * @returns The container engine, default to local Docker at `unix:/var/run/docker.sock`
1047
+ */
1048
+ function getContainerEngine (
1049
+ engineOrSocketPath : Worker_ContainerEngine | string | undefined
1050
+ ) : Worker_ContainerEngine {
1051
+ if ( ! engineOrSocketPath ) {
1052
+ // TODO: workerd does not support win named pipes
1053
+ engineOrSocketPath =
1054
+ platform ( ) === "win32"
1055
+ ? "//./pipe/docker_engine"
1056
+ : "unix:/var/run/docker.sock" ;
1057
+ }
1058
+
1059
+ if ( typeof engineOrSocketPath === "string" ) {
1060
+ return { localDocker : { socketPath : engineOrSocketPath } } ;
1061
+ }
1062
+
1063
+ return engineOrSocketPath ;
1064
+ }
1065
+
1026
1066
export * from "./errors" ;
1027
1067
export * from "./proxy" ;
1028
1068
export * from "./constants" ;
0 commit comments