11import assert from "assert" ;
22import { readFileSync } from "fs" ;
33import fs from "fs/promises" ;
4+ import { platform } from "node:os" ;
45import path from "path" ;
56import { Readable } from "stream" ;
67import tls from "tls" ;
@@ -18,6 +19,7 @@ import {
1819 ServiceDesignator ,
1920 supportedCompatibilityDate ,
2021 Worker_Binding ,
22+ Worker_ContainerEngine ,
2123 Worker_DurableObjectNamespace ,
2224 Worker_Module ,
2325} from "../../runtime" ;
@@ -166,6 +168,16 @@ const CoreOptionsSchemaInput = z.intersection(
166168
167169 // Strip the CF-Connecting-IP header from outbound fetches
168170 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 ( ) ,
169181 } )
170182) ;
171183export const CoreOptionsSchema = CoreOptionsSchemaInput . transform ( ( value ) => {
@@ -736,28 +748,32 @@ export const CORE_PLUGIN: Plugin<
736748 classNamesEntries . map < Worker_DurableObjectNamespace > (
737749 ( [
738750 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+ }
761777 ) ,
762778 durableObjectStorage :
763779 classNamesEntries . length === 0
@@ -787,6 +803,7 @@ export const CORE_PLUGIN: Plugin<
787803 options . hasAssetsAndIsVitest
788804 ) ;
789805 } ) ,
806+ containerEngine : getContainerEngine ( options . containerEngine ) ,
790807 } ,
791808 } ) ;
792809 }
@@ -1022,6 +1039,29 @@ function getWorkerScript(
10221039 }
10231040}
10241041
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+
10251065export * from "./errors" ;
10261066export * from "./proxy" ;
10271067export * from "./constants" ;
0 commit comments