@@ -124,15 +124,43 @@ export interface FunctionsEmulatorArgs {
124
124
projectAlias ?: string ;
125
125
}
126
126
127
- // FunctionsRuntimeInstance is the handler for a running function invocation
127
+ /**
128
+ * IPC connection info of a Function Runtime.
129
+ */
130
+ export class IPCConn {
131
+ constructor ( readonly socketPath : string ) { }
132
+
133
+ httpReqOpts ( ) : http . RequestOptions {
134
+ return {
135
+ path : `/` ,
136
+ socketPath : this . socketPath ,
137
+ } ;
138
+ }
139
+ }
140
+
141
+ /**
142
+ * TCP/IP connection info of a Function Runtime.
143
+ */
144
+ export class TCPConn {
145
+ constructor ( readonly host : string , readonly port : number ) { }
146
+
147
+ httpReqOpts ( ) : http . RequestOptions {
148
+ return {
149
+ path : `/` ,
150
+ host : this . host ,
151
+ port : this . port ,
152
+ } ;
153
+ }
154
+ }
155
+
128
156
export interface FunctionsRuntimeInstance {
129
157
process : ChildProcess ;
130
158
// An emitter which sends our EmulatorLog events from the runtime.
131
159
events : EventEmitter ;
132
160
// A cwd of the process
133
161
cwd : string ;
134
- // Path to socket file used for HTTP-over-IPC comms.
135
- socketPath : string ;
162
+ // Communication info for the runtime
163
+ conn : IPCConn | TCPConn ;
136
164
}
137
165
138
166
export interface InvokeRuntimeOpts {
@@ -355,8 +383,8 @@ export class FunctionsEmulator implements EmulatorInstance {
355
383
return new Promise ( ( resolve , reject ) => {
356
384
const req = http . request (
357
385
{
386
+ ...worker . runtime . conn . httpReqOpts ( ) ,
358
387
path : `/` ,
359
- socketPath : worker . runtime . socketPath ,
360
388
headers : headers ,
361
389
} ,
362
390
resolve
@@ -1269,14 +1297,14 @@ export class FunctionsEmulator implements EmulatorInstance {
1269
1297
process : childProcess ,
1270
1298
events : new EventEmitter ( ) ,
1271
1299
cwd : backend . functionsDir ,
1272
- socketPath,
1300
+ conn : new IPCConn ( socketPath ) ,
1273
1301
} ) ;
1274
1302
}
1275
1303
1276
1304
async startPython (
1277
1305
backend : EmulatableBackend ,
1278
1306
envs : Record < string , string >
1279
- ) : Promise < ChildProcess > {
1307
+ ) : Promise < FunctionsRuntimeInstance > {
1280
1308
const args = [ path . join ( __dirname , "functionsEmulatorRuntime" ) ] ;
1281
1309
1282
1310
if ( this . args . debugPort ) {
@@ -1294,18 +1322,23 @@ export class FunctionsEmulator implements EmulatorInstance {
1294
1322
) ;
1295
1323
}
1296
1324
1297
- // Unfortunately, there isn't platform-neutral support for Unix Domain Socket or Nameed Pipe in the python
1298
- // ecosystem. Use regular IP+PORT combination instead.
1325
+ // Unfortunately, there isn't platform-neutral support for Unix Domain Socket or Named Pipe in the python
1326
+ // ecosystem. Use TCP/IP stack instead.
1299
1327
const port = await portfinder . getPortPromise ( {
1300
1328
port : 8081 ,
1301
1329
} ) ;
1302
- return Promise . resolve (
1303
- runWithVirtualEnv ( [ bin , ...args ] , backend . functionsDir , {
1304
- ...process . env ,
1305
- ...envs ,
1306
- PORT : port . toString ( ) ,
1307
- } )
1308
- ) ;
1330
+ const childProcess = runWithVirtualEnv ( [ bin , ...args ] , backend . functionsDir , {
1331
+ ...process . env ,
1332
+ ...envs ,
1333
+ PORT : port . toString ( ) ,
1334
+ } ) ;
1335
+
1336
+ return Promise . resolve ( {
1337
+ process : childProcess ,
1338
+ events : new EventEmitter ( ) ,
1339
+ cwd : backend . functionsDir ,
1340
+ conn : new TCPConn ( "localhost" , port ) ,
1341
+ } ) ;
1309
1342
}
1310
1343
1311
1344
async startRuntime (
@@ -1315,7 +1348,12 @@ export class FunctionsEmulator implements EmulatorInstance {
1315
1348
const runtimeEnv = this . getRuntimeEnvs ( backend , trigger ) ;
1316
1349
const secretEnvs = await this . resolveSecretEnvs ( backend , trigger ) ;
1317
1350
1318
- const runtime = await this . startNode ( backend , { ...runtimeEnv , ...secretEnvs } ) ;
1351
+ let runtime ;
1352
+ if ( backend . runtime ! . startsWith ( "python" ) ) {
1353
+ runtime = await this . startPython ( backend , { ...runtimeEnv , ...secretEnvs } ) ;
1354
+ } else {
1355
+ runtime = await this . startNode ( backend , { ...runtimeEnv , ...secretEnvs } ) ;
1356
+ }
1319
1357
const extensionLogInfo = {
1320
1358
instanceId : backend . extensionInstanceId ,
1321
1359
ref : backend . extensionVersion ?. ref ,
0 commit comments