Skip to content

introduce a new option for configuring ignored routes #679

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ ignored.
| `--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. |
| `--ignored-routes`| `IGNORED_ROUTES` | A route expression for requests that should not be routed the function. An empty 404 response will be returned. This is set to `/favicon.ico|/robots.txt` by default for `http` functions. |
You can set command-line flags in your `package.json` via the `start` script.
For example:
Expand Down
13 changes: 12 additions & 1 deletion src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ export interface FrameworkOptions {
* The request timeout.
*/
timeoutMilliseconds: number;
/**
* Routes that should return a 404 without invoking the function.
*/
ignoredRoutes: string | null;
}

/**
Expand Down Expand Up @@ -86,7 +90,7 @@ class ConfigurableOption<T> {

parse(cliArgs: minimist.ParsedArgs, envVars: NodeJS.ProcessEnv): T {
return this.validator(
cliArgs[this.cliOption] || envVars[this.envVar] || this.defaultValue
cliArgs[this.cliOption] ?? envVars[this.envVar] ?? this.defaultValue
);
}
}
Expand Down Expand Up @@ -130,6 +134,11 @@ const TimeoutOption = new ConfigurableOption(
return x * 1000;
}
);
const IgnoredRoutesOption = new ConfigurableOption<string | null>(
'ignored-routes',
'IGNORED_ROUTES',
null // null by default so we can detect if it is explicitly set to ""
);

export const requiredNodeJsVersionForLogExecutionID = '13.0.0';
const ExecutionIdOption = new ConfigurableOption(
Expand Down Expand Up @@ -181,6 +190,7 @@ export const parseOptions = (
SignatureOption.cliOption,
SourceLocationOption.cliOption,
TimeoutOption.cliOption,
IgnoredRoutesOption.cliOption,
],
});
return {
Expand All @@ -191,5 +201,6 @@ export const parseOptions = (
timeoutMilliseconds: TimeoutOption.parse(argv, envVars),
printHelp: cliArgs[2] === '-h' || cliArgs[2] === '--help',
enableExecutionId: ExecutionIdOption.parse(argv, envVars),
ignoredRoutes: IgnoredRoutesOption.parse(argv, envVars),
};
};
15 changes: 12 additions & 3 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,23 @@ export function getServer(
if (options.signatureType === 'cloudevent') {
app.use(backgroundEventToCloudEventMiddleware);
}

if (options.signatureType === 'http') {
if (options.ignoredRoutes !== null) {
// Ignored routes is a configuration option that allows requests to specific paths to be prevented
// from invoking the user's function.
app.use(options.ignoredRoutes, (req, res) => {
res.status(404).send(null);
});
} else if (options.signatureType === 'http') {
// We configure some default ignored routes, only for HTTP functions.
app.use('/favicon.ico|/robots.txt', (req, res) => {
// Neither crawlers nor browsers attempting to pull the icon find the body
// contents particularly useful, so we send nothing in the response body.
// contents particularly useful, so we filter these requests out from invoking
// the user's function by default.
res.status(404).send(null);
});
}

if (options.signatureType === 'http') {
app.use('/*', (req, res, next) => {
onFinished(res, (err, res) => {
res.locals.functionExecutionFinished = true;
Expand Down
1 change: 1 addition & 0 deletions src/testing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,6 @@ export const getTestServer = (functionName: string): Server => {
target: '',
sourceLocation: '',
printHelp: false,
ignoredRoutes: null,
});
};
1 change: 1 addition & 0 deletions test/integration/legacy_event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const testOptions = {
target: '',
sourceLocation: '',
printHelp: false,
ignoredRoutes: null,
};

describe('Event Function', () => {
Expand Down
8 changes: 8 additions & 0 deletions test/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ describe('parseOptions', () => {
printHelp: false,
enableExecutionId: false,
timeoutMilliseconds: 0,
ignoredRoutes: null,
},
},
{
Expand All @@ -75,6 +76,7 @@ describe('parseOptions', () => {
'--source=/source',
'--timeout',
'6',
'--ignored-routes=banana',
],
envVars: {},
expectedOptions: {
Expand All @@ -85,6 +87,7 @@ describe('parseOptions', () => {
printHelp: false,
enableExecutionId: false,
timeoutMilliseconds: 6000,
ignoredRoutes: 'banana',
},
},
{
Expand All @@ -96,6 +99,7 @@ describe('parseOptions', () => {
FUNCTION_SIGNATURE_TYPE: 'cloudevent',
FUNCTION_SOURCE: '/source',
CLOUD_RUN_TIMEOUT_SECONDS: '2',
IGNORED_ROUTES: '',
},
expectedOptions: {
port: '1234',
Expand All @@ -105,6 +109,7 @@ describe('parseOptions', () => {
printHelp: false,
enableExecutionId: false,
timeoutMilliseconds: 2000,
ignoredRoutes: '',
},
},
{
Expand All @@ -119,13 +124,15 @@ describe('parseOptions', () => {
'cloudevent',
'--source=/source',
'--timeout=3',
'--ignored-routes=avocado',
],
envVars: {
PORT: '4567',
FUNCTION_TARGET: 'fooBar',
FUNCTION_SIGNATURE_TYPE: 'event',
FUNCTION_SOURCE: '/somewhere/else',
CLOUD_RUN_TIMEOUT_SECONDS: '5',
IGNORED_ROUTES: 'banana',
},
expectedOptions: {
port: '1234',
Expand All @@ -135,6 +142,7 @@ describe('parseOptions', () => {
printHelp: false,
enableExecutionId: false,
timeoutMilliseconds: 3000,
ignoredRoutes: 'avocado',
},
},
];
Expand Down
Loading