Skip to content

Commit 76f134b

Browse files
grantswalkowski
authored andcommitted
fix #4: Removes 'function' prefix from CLI args (#16)
* fix #4: Removes 'function' prefix from CLI arg and env vars * fix: Re-add 'FUNCTION_' to env var prefix * fix: Fix flag/env setup * fix: docs comment * fix: Update FUNCTION_ prefix for env var but not consts
1 parent 29354fb commit 76f134b

File tree

4 files changed

+44
-33
lines changed

4 files changed

+44
-33
lines changed

README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ exports.helloWorld = (req, res) => {
5858
Run the following command:
5959

6060
```sh
61-
npx @google-cloud/functions-framework --function-target=helloWorld
61+
npx @google-cloud/functions-framework --target=helloWorld
6262
```
6363

6464
Open http://localhost:8080/ in your browser and see *Hello, World*.
@@ -91,7 +91,7 @@ command-line arguments:
9191

9292
```js
9393
"scripts": {
94-
"start": "functions-framework --function-target=helloWorld"
94+
"start": "functions-framework --target=helloWorld"
9595
}
9696
```
9797

@@ -145,15 +145,15 @@ ignored.
145145
Command-line flag | Environment variable | Description
146146
------------------------- | ------------------------- | -----------
147147
`--port` | `PORT` | The port on which the Functions Framework listens for requests. Default: `8080`
148-
`--function-target` | `FUNCTION_TARGET` | The name of the exported function to be invoked in response to requests. Default: `function`
149-
`--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`
148+
`--target` | `FUNCTION_TARGET` | The name of the exported function to be invoked in response to requests. Default: `function`
149+
`--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`
150150

151151
You can set command-line flags in your `package.json` via the `start` script.
152152
For example:
153153

154154
```js
155155
"scripts": {
156-
"start": "functions-framework --function-target=helloWorld"
156+
"start": "functions-framework --target=helloWorld"
157157
}
158158
```
159159

src/index.ts

+29-17
Original file line numberDiff line numberDiff line change
@@ -28,52 +28,64 @@
2828
// unmarshalled from an incoming request.
2929

3030
import * as minimist from 'minimist';
31+
3132
import {
3233
ErrorHandler,
33-
FunctionSignatureType,
34+
SignatureType,
3435
getServer,
3536
getUserFunction,
3637
} from './invoker';
3738

39+
// Supported command-line flags
40+
const FLAG = {
41+
PORT: 'port',
42+
TARGET: 'target',
43+
SIGNATURE_TYPE: 'signature-type', // dash
44+
};
45+
46+
// Supported environment variables
47+
const ENV = {
48+
PORT: 'PORT',
49+
TARGET: 'FUNCTION_TARGET',
50+
SIGNATURE_TYPE: 'FUNCTION_SIGNATURE_TYPE', // underscore
51+
};
52+
3853
enum NodeEnv {
3954
PRODUCTION = 'production',
4055
}
4156

4257
const argv = minimist(process.argv, {
43-
string: ['port', 'function-target', 'function-signature-type'],
58+
string: [FLAG.PORT, FLAG.TARGET, FLAG.SIGNATURE_TYPE],
4459
});
4560

4661
const CODE_LOCATION = process.cwd();
47-
const PORT = argv['port'] || process.env.PORT || '8080';
48-
const FUNCTION_TARGET =
49-
argv['function-target'] || process.env.FUNCTION_TARGET || 'function';
62+
const PORT = argv[FLAG.PORT] || process.env[ENV.PORT] || '8080';
63+
const TARGET = argv[FLAG.TARGET] || process.env[ENV.TARGET] || 'function';
5064

51-
const FUNCTION_SIGNATURE_TYPE_STRING =
52-
argv['function-signature-type'] ||
53-
process.env.FUNCTION_SIGNATURE_TYPE ||
54-
'http';
55-
const FUNCTION_SIGNATURE_TYPE =
56-
FunctionSignatureType[
57-
FUNCTION_SIGNATURE_TYPE_STRING.toUpperCase() as keyof typeof FunctionSignatureType
65+
const SIGNATURE_TYPE_STRING =
66+
argv[FLAG.SIGNATURE_TYPE] || process.env[ENV.SIGNATURE_TYPE] || 'http';
67+
const SIGNATURE_TYPE =
68+
SignatureType[
69+
SIGNATURE_TYPE_STRING.toUpperCase() as keyof typeof SignatureType
5870
];
59-
if (FUNCTION_SIGNATURE_TYPE === undefined) {
60-
console.error(`FUNCTION_SIGNATURE_TYPE must be one of 'http' or 'event'.`);
71+
if (SIGNATURE_TYPE === undefined) {
72+
console.error(`Function signature type must be one of 'http' or 'event'.`);
6173
process.exit(1);
6274
}
6375

64-
const USER_FUNCTION = getUserFunction(CODE_LOCATION, FUNCTION_TARGET);
76+
const USER_FUNCTION = getUserFunction(CODE_LOCATION, TARGET);
6577
if (!USER_FUNCTION) {
6678
console.error('Could not load the function, shutting down.');
6779
process.exit(1);
6880
}
6981

70-
const SERVER = getServer(USER_FUNCTION!, FUNCTION_SIGNATURE_TYPE!);
82+
const SERVER = getServer(USER_FUNCTION!, SIGNATURE_TYPE!);
7183
const ERROR_HANDLER = new ErrorHandler(SERVER);
7284
SERVER.listen(PORT, () => {
7385
ERROR_HANDLER.register();
7486
if (process.env.NODE_ENV !== NodeEnv.PRODUCTION) {
7587
console.log('Serving function...');
76-
console.log(`Function: ${FUNCTION_TARGET}`);
88+
console.log(`Function: ${TARGET}`);
7789
console.log(`URL: http://localhost:${PORT}/`);
7890
}
7991
}).setTimeout(0); // Disable automatic timeout on incoming connections.

src/invoker.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
import * as bodyParser from 'body-parser';
2424
import * as domain from 'domain';
2525
import * as express from 'express';
26-
import * as onFinished from 'on-finished';
2726
import * as http from 'http';
27+
import * as onFinished from 'on-finished';
2828

2929
// HTTP header field that is added to Worker response to signalize problems with
3030
// executing the client function.
@@ -123,7 +123,7 @@ declare global {
123123
}
124124
}
125125

126-
export enum FunctionSignatureType {
126+
export enum SignatureType {
127127
HTTP,
128128
EVENT,
129129
}
@@ -136,9 +136,9 @@ export enum FunctionSignatureType {
136136
*/
137137
function isHttpFunction(
138138
fn: HandlerFunction,
139-
functionSignatureType: FunctionSignatureType
139+
functionSignatureType: SignatureType
140140
): fn is HttpFunction {
141-
return functionSignatureType === FunctionSignatureType.HTTP;
141+
return functionSignatureType === SignatureType.HTTP;
142142
}
143143

144144
/**
@@ -477,7 +477,7 @@ function wrapEventFunction(
477477
function registerFunctionRoutes(
478478
app: express.Application,
479479
userFunction: HandlerFunction,
480-
functionSignatureType: FunctionSignatureType
480+
functionSignatureType: SignatureType
481481
) {
482482
if (isHttpFunction(userFunction!, functionSignatureType)) {
483483
app.use('/*', (req, res, next) => {
@@ -548,7 +548,7 @@ export class ErrorHandler {
548548
*/
549549
export function getServer(
550550
userFunction: HandlerFunction,
551-
functionSignatureType: FunctionSignatureType
551+
functionSignatureType: SignatureType
552552
): http.Server {
553553
// App to use for function executions.
554554
const app = express();

test/invoker.ts

+4-5
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@
1313
// limitations under the License.
1414

1515
import * as assert from 'assert';
16-
import * as supertest from 'supertest';
1716
import * as express from 'express';
18-
1917
import * as invoker from '../src/invoker';
18+
import * as supertest from 'supertest';
2019

2120
describe('loading function', () => {
2221
it('should load the function', () => {
@@ -35,7 +34,7 @@ describe('request to HTTP function', () => {
3534
(req: express.Request, res: express.Response) => {
3635
res.send(req.body.text.toUpperCase());
3736
},
38-
invoker.FunctionSignatureType.HTTP
37+
invoker.SignatureType.HTTP
3938
);
4039
return supertest(server)
4140
.post('/')
@@ -106,7 +105,7 @@ describe('GCF event request to event function', () => {
106105
const server = invoker.getServer((data: {}, context: invoker.Context) => {
107106
receivedData = data;
108107
receivedContext = context as invoker.CloudFunctionsContext;
109-
}, invoker.FunctionSignatureType.EVENT);
108+
}, invoker.SignatureType.EVENT);
110109
await supertest(server)
111110
.post('/')
112111
.send(test.body)
@@ -169,7 +168,7 @@ describe('CloudEvents request to event function', () => {
169168
const server = invoker.getServer((data: {}, context: invoker.Context) => {
170169
receivedData = data;
171170
receivedContext = context as invoker.CloudEventsContext;
172-
}, invoker.FunctionSignatureType.EVENT);
171+
}, invoker.SignatureType.EVENT);
173172
await supertest(server)
174173
.post('/')
175174
.set(test.headers)

0 commit comments

Comments
 (0)