Skip to content

Commit b34bbad

Browse files
committed
feat: added new host option
1 parent b4bda93 commit b34bbad

File tree

5 files changed

+24
-9
lines changed

5 files changed

+24
-9
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -187,10 +187,11 @@ ignored.
187187
| Command-line flag | Environment variable | Description |
188188
| ------------------ | ------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
189189
| `--port` | `PORT` | The port on which the Functions Framework listens for requests. Default: `8080` |
190+
| `--host` | `HOST` | The host on which the Functions Framework listens for requests. Default: `127.0.0.1` |
190191
| `--target` | `FUNCTION_TARGET` | The name of the exported function to be invoked in response to requests. Default: `function` |
191192
| `--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` |
192193
| `--source` | `FUNCTION_SOURCE` | The path to the directory of your function. Default: `cwd` (the current working directory) |
193-
| `--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. |
194+
| `--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. |
194195
195196
You can set command-line flags in your `package.json` via the `start` script.
196197
For example:

src/main.ts

+14-8
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,21 @@ export const main = async () => {
5757
const server = getServer(userFunction!, options);
5858
const errorHandler = new ErrorHandler(server);
5959
server
60-
.listen(options.port, () => {
61-
errorHandler.register();
62-
if (process.env.NODE_ENV !== 'production') {
63-
console.log('Serving function...');
64-
console.log(`Function: ${options.target}`);
65-
console.log(`Signature type: ${signatureType}`);
66-
console.log(`URL: http://localhost:${options.port}/`);
60+
.listen(
61+
{
62+
port: options.port,
63+
host: options.host,
64+
},
65+
() => {
66+
errorHandler.register();
67+
if (process.env.NODE_ENV !== 'production') {
68+
console.log('Serving function...');
69+
console.log(`Function: ${options.target}`);
70+
console.log(`Signature type: ${signatureType}`);
71+
console.log(`URL: http://localhost:${options.port}/`);
72+
}
6773
}
68-
})
74+
)
6975
.setTimeout(0); // Disable automatic timeout on incoming connections.
7076
} catch (e) {
7177
if (e instanceof OptionsError) {

src/options.ts

+6
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ export interface FrameworkOptions {
3131
* The port on which this server listens to all HTTP requests.
3232
*/
3333
port: string;
34+
/**
35+
* The host on which this server listens for all HTTP requests.
36+
*/
37+
host: string;
3438
/**
3539
* The name of the function within user's node module to execute. If such a
3640
* function is not defined, then falls back to 'function' name.
@@ -92,6 +96,7 @@ class ConfigurableOption<T> {
9296
}
9397

9498
const PortOption = new ConfigurableOption('port', 'PORT', '8080');
99+
const HostOption = new ConfigurableOption('host', 'HOST', '127.0.0.1');
95100
const FunctionTargetOption = new ConfigurableOption(
96101
'target',
97102
'FUNCTION_TARGET',
@@ -185,6 +190,7 @@ export const parseOptions = (
185190
});
186191
return {
187192
port: PortOption.parse(argv, envVars),
193+
host: HostOption.parse(argv, envVars),
188194
target: FunctionTargetOption.parse(argv, envVars),
189195
sourceLocation: SourceLocationOption.parse(argv, envVars),
190196
signatureType: SignatureOption.parse(argv, envVars),

src/testing.ts

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ export const getTestServer = (functionName: string): Server => {
5353
enableExecutionId: false,
5454
timeoutMilliseconds: 0,
5555
port: '0',
56+
host: '127.0.0.1',
5657
target: '',
5758
sourceLocation: '',
5859
printHelp: false,

test/integration/legacy_event.ts

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ const testOptions = {
3737
enableExecutionId: false,
3838
timeoutMilliseconds: 0,
3939
port: '0',
40+
host: '127.0.0.1',
4041
target: '',
4142
sourceLocation: '',
4243
printHelp: false,

0 commit comments

Comments
 (0)