From b34bbadf083467c227bec8a5c182e00bb80b5b11 Mon Sep 17 00:00:00 2001 From: lukascaska Date: Fri, 18 Oct 2024 15:53:46 -0300 Subject: [PATCH 1/2] feat: added new host option --- README.md | 3 ++- src/main.ts | 22 ++++++++++++++-------- src/options.ts | 6 ++++++ src/testing.ts | 1 + test/integration/legacy_event.ts | 1 + 5 files changed, 24 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index ac94300cb..5633db1e2 100644 --- a/README.md +++ b/README.md @@ -187,10 +187,11 @@ ignored. | Command-line flag | Environment variable | Description | | ------------------ | ------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | `--port` | `PORT` | The port on which the Functions Framework listens for requests. Default: `8080` | +| `--host` | `HOST` | The host on which the Functions Framework listens for requests. Default: `127.0.0.1` | | `--target` | `FUNCTION_TARGET` | The name of the exported function to be invoked in response to requests. Default: `function` | | `--signature-type` | `FUNCTION_SIGNATURE_TYPE` | The signature used when writing your function. Controls unmarshalling rules and determines which arguments are used to invoke your function. Default: `http`; accepted values: `http` or `event` or `cloudevent` | | `--source` | `FUNCTION_SOURCE` | The path to the directory of your function. Default: `cwd` (the current working directory) | -| `--log-execution-id`| `LOG_EXECUTION_ID` | Enables execution IDs in logs, either `true` or `false`. When not specified, default to disable. Requires Node.js 13.0.0 or later. | +| `--log-execution-id`| `LOG_EXECUTION_ID` | Enables execution IDs in logs, either `true` or `false`. When not specified, default to disable. Requires Node.js 13.0.0 or later. | You can set command-line flags in your `package.json` via the `start` script. For example: diff --git a/src/main.ts b/src/main.ts index 2f1684c77..d2707f17e 100644 --- a/src/main.ts +++ b/src/main.ts @@ -57,15 +57,21 @@ export const main = async () => { const server = getServer(userFunction!, options); const errorHandler = new ErrorHandler(server); server - .listen(options.port, () => { - errorHandler.register(); - if (process.env.NODE_ENV !== 'production') { - console.log('Serving function...'); - console.log(`Function: ${options.target}`); - console.log(`Signature type: ${signatureType}`); - console.log(`URL: http://localhost:${options.port}/`); + .listen( + { + port: options.port, + host: options.host, + }, + () => { + errorHandler.register(); + if (process.env.NODE_ENV !== 'production') { + console.log('Serving function...'); + console.log(`Function: ${options.target}`); + console.log(`Signature type: ${signatureType}`); + console.log(`URL: http://localhost:${options.port}/`); + } } - }) + ) .setTimeout(0); // Disable automatic timeout on incoming connections. } catch (e) { if (e instanceof OptionsError) { diff --git a/src/options.ts b/src/options.ts index 96de71c87..c5bc9d7ff 100644 --- a/src/options.ts +++ b/src/options.ts @@ -31,6 +31,10 @@ export interface FrameworkOptions { * The port on which this server listens to all HTTP requests. */ port: string; + /** + * The host on which this server listens for all HTTP requests. + */ + host: string; /** * The name of the function within user's node module to execute. If such a * function is not defined, then falls back to 'function' name. @@ -92,6 +96,7 @@ class ConfigurableOption { } const PortOption = new ConfigurableOption('port', 'PORT', '8080'); +const HostOption = new ConfigurableOption('host', 'HOST', '127.0.0.1'); const FunctionTargetOption = new ConfigurableOption( 'target', 'FUNCTION_TARGET', @@ -185,6 +190,7 @@ export const parseOptions = ( }); return { port: PortOption.parse(argv, envVars), + host: HostOption.parse(argv, envVars), target: FunctionTargetOption.parse(argv, envVars), sourceLocation: SourceLocationOption.parse(argv, envVars), signatureType: SignatureOption.parse(argv, envVars), diff --git a/src/testing.ts b/src/testing.ts index 9cb62c9db..49af7a4ab 100644 --- a/src/testing.ts +++ b/src/testing.ts @@ -53,6 +53,7 @@ export const getTestServer = (functionName: string): Server => { enableExecutionId: false, timeoutMilliseconds: 0, port: '0', + host: '127.0.0.1', target: '', sourceLocation: '', printHelp: false, diff --git a/test/integration/legacy_event.ts b/test/integration/legacy_event.ts index a25250e64..44a12efb8 100644 --- a/test/integration/legacy_event.ts +++ b/test/integration/legacy_event.ts @@ -37,6 +37,7 @@ const testOptions = { enableExecutionId: false, timeoutMilliseconds: 0, port: '0', + host: '127.0.0.1', target: '', sourceLocation: '', printHelp: false, From bd172eb5636ec36f8f7700618a3873c7b413e607 Mon Sep 17 00:00:00 2001 From: lukascaska Date: Tue, 22 Oct 2024 10:05:33 -0300 Subject: [PATCH 2/2] fix: default value --- README.md | 2 +- src/main.ts | 28 ++++++++++++++-------------- src/options.ts | 2 +- src/testing.ts | 2 +- test/integration/legacy_event.ts | 2 +- test/options.ts | 10 ++++++++++ 6 files changed, 28 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 5633db1e2..f896ecc74 100644 --- a/README.md +++ b/README.md @@ -187,7 +187,7 @@ ignored. | Command-line flag | Environment variable | Description | | ------------------ | ------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | `--port` | `PORT` | The port on which the Functions Framework listens for requests. Default: `8080` | -| `--host` | `HOST` | The host on which the Functions Framework listens for requests. Default: `127.0.0.1` | +| `--host` | `HOST` | The host on which the Functions Framework listens for requests. Default: `''` [Check node documentation for the default behavior](https://nodejs.org/api/net.html#serverlistenport-host-backlog-callback) | | `--target` | `FUNCTION_TARGET` | The name of the exported function to be invoked in response to requests. Default: `function` | | `--signature-type` | `FUNCTION_SIGNATURE_TYPE` | The signature used when writing your function. Controls unmarshalling rules and determines which arguments are used to invoke your function. Default: `http`; accepted values: `http` or `event` or `cloudevent` | | `--source` | `FUNCTION_SOURCE` | The path to the directory of your function. Default: `cwd` (the current working directory) | diff --git a/src/main.ts b/src/main.ts index d2707f17e..d0fe0a6e2 100644 --- a/src/main.ts +++ b/src/main.ts @@ -56,22 +56,22 @@ export const main = async () => { options.signatureType = signatureType; const server = getServer(userFunction!, options); const errorHandler = new ErrorHandler(server); + const {host, port} = options; + + const serverOptions = { + port, + ...(host && {host}), + }; server - .listen( - { - port: options.port, - host: options.host, - }, - () => { - errorHandler.register(); - if (process.env.NODE_ENV !== 'production') { - console.log('Serving function...'); - console.log(`Function: ${options.target}`); - console.log(`Signature type: ${signatureType}`); - console.log(`URL: http://localhost:${options.port}/`); - } + .listen(serverOptions, () => { + errorHandler.register(); + if (process.env.NODE_ENV !== 'production') { + console.log('Serving function...'); + console.log(`Function: ${options.target}`); + console.log(`Signature type: ${signatureType}`); + console.log(`URL: http://localhost:${options.port}/`); } - ) + }) .setTimeout(0); // Disable automatic timeout on incoming connections. } catch (e) { if (e instanceof OptionsError) { diff --git a/src/options.ts b/src/options.ts index c5bc9d7ff..a602b9c61 100644 --- a/src/options.ts +++ b/src/options.ts @@ -96,7 +96,7 @@ class ConfigurableOption { } const PortOption = new ConfigurableOption('port', 'PORT', '8080'); -const HostOption = new ConfigurableOption('host', 'HOST', '127.0.0.1'); +const HostOption = new ConfigurableOption('host', 'HOST', ''); const FunctionTargetOption = new ConfigurableOption( 'target', 'FUNCTION_TARGET', diff --git a/src/testing.ts b/src/testing.ts index 49af7a4ab..2e3ac2583 100644 --- a/src/testing.ts +++ b/src/testing.ts @@ -53,7 +53,7 @@ export const getTestServer = (functionName: string): Server => { enableExecutionId: false, timeoutMilliseconds: 0, port: '0', - host: '127.0.0.1', + host: '', target: '', sourceLocation: '', printHelp: false, diff --git a/test/integration/legacy_event.ts b/test/integration/legacy_event.ts index 44a12efb8..0b7d4ce39 100644 --- a/test/integration/legacy_event.ts +++ b/test/integration/legacy_event.ts @@ -37,7 +37,7 @@ const testOptions = { enableExecutionId: false, timeoutMilliseconds: 0, port: '0', - host: '127.0.0.1', + host: '', target: '', sourceLocation: '', printHelp: false, diff --git a/test/options.ts b/test/options.ts index bf1619d05..f7556a9c8 100644 --- a/test/options.ts +++ b/test/options.ts @@ -54,6 +54,7 @@ describe('parseOptions', () => { envVars: {}, expectedOptions: { port: '8080', + host: '', target: 'function', sourceLocation: resolve(''), signatureType: 'http', @@ -75,10 +76,13 @@ describe('parseOptions', () => { '--source=/source', '--timeout', '6', + '--host', + '0.0.0.0', ], envVars: {}, expectedOptions: { port: '1234', + host: '0.0.0.0', target: 'helloWorld', sourceLocation: resolve('/source'), signatureType: 'cloudevent', @@ -92,6 +96,7 @@ describe('parseOptions', () => { cliOpts: ['bin/node', '/index.js'], envVars: { PORT: '1234', + HOST: '0.0.0.0', FUNCTION_TARGET: 'helloWorld', FUNCTION_SIGNATURE_TYPE: 'cloudevent', FUNCTION_SOURCE: '/source', @@ -99,6 +104,7 @@ describe('parseOptions', () => { }, expectedOptions: { port: '1234', + host: '0.0.0.0', target: 'helloWorld', sourceLocation: resolve('/source'), signatureType: 'cloudevent', @@ -119,9 +125,12 @@ describe('parseOptions', () => { 'cloudevent', '--source=/source', '--timeout=3', + '--host', + '0.0.0.0', ], envVars: { PORT: '4567', + HOST: '127.0.0.1', FUNCTION_TARGET: 'fooBar', FUNCTION_SIGNATURE_TYPE: 'event', FUNCTION_SOURCE: '/somewhere/else', @@ -129,6 +138,7 @@ describe('parseOptions', () => { }, expectedOptions: { port: '1234', + host: '0.0.0.0', target: 'helloWorld', sourceLocation: resolve('/source'), signatureType: 'cloudevent',