Skip to content

Commit d61c084

Browse files
authored
Adjust path handling (#121)
* Remove --dry-run option. * feat: send 404 for favicon.ico and robots.txt. BREAKING CHANGE: paths with favicon.ico and robots.txt will send 404 and no longer invoke function.
1 parent fc63000 commit d61c084

File tree

3 files changed

+54
-24
lines changed

3 files changed

+54
-24
lines changed

src/index.ts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ const FLAG = {
4343
TARGET: 'target',
4444
SIGNATURE_TYPE: 'signature-type', // dash
4545
SOURCE: 'source',
46-
DRY_RUN: 'dry-run',
4746
};
4847

4948
// Supported environment variables
@@ -52,15 +51,14 @@ const ENV = {
5251
TARGET: 'FUNCTION_TARGET',
5352
SIGNATURE_TYPE: 'FUNCTION_SIGNATURE_TYPE', // underscore
5453
SOURCE: 'FUNCTION_SOURCE',
55-
DRY_RUN: 'DRY_RUN',
5654
};
5755

5856
enum NodeEnv {
5957
PRODUCTION = 'production',
6058
}
6159

6260
const argv = minimist(process.argv, {
63-
string: [FLAG.PORT, FLAG.TARGET, FLAG.SIGNATURE_TYPE, FLAG.DRY_RUN],
61+
string: [FLAG.PORT, FLAG.TARGET, FLAG.SIGNATURE_TYPE],
6462
});
6563

6664
const CODE_LOCATION = resolve(
@@ -79,7 +77,6 @@ if (SIGNATURE_TYPE === undefined) {
7977
console.error(`Function signature type must be one of 'http' or 'event'.`);
8078
process.exit(1);
8179
}
82-
const DRY_RUN = argv[FLAG.DRY_RUN] || process.env[ENV.DRY_RUN] || false;
8380

8481
// CLI Help Flag
8582
if (process.argv[2] === '-h' || process.argv[2] === '--help') {
@@ -101,13 +98,6 @@ if (!USER_FUNCTION) {
10198
const SERVER = getServer(USER_FUNCTION!, SIGNATURE_TYPE!);
10299
const ERROR_HANDLER = new ErrorHandler(SERVER);
103100

104-
if (DRY_RUN) {
105-
console.log(`Function: ${TARGET}`);
106-
console.log(`URL: http://localhost:${PORT}/`);
107-
console.log('Dry run successful, shutting down.');
108-
process.exit(0);
109-
}
110-
111101
SERVER.listen(PORT, () => {
112102
ERROR_HANDLER.register();
113103
if (process.env.NODE_ENV !== NodeEnv.PRODUCTION) {

src/invoker.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,10 @@ function registerFunctionRoutes(
480480
functionSignatureType: SignatureType
481481
) {
482482
if (isHttpFunction(userFunction!, functionSignatureType)) {
483+
app.use('/favicon.ico|/robots.txt', (req, res, next) => {
484+
res.sendStatus(404);
485+
});
486+
483487
app.use('/*', (req, res, next) => {
484488
onFinished(res, (err, res) => {
485489
res.locals.functionExecutionFinished = true;

test/invoker.ts

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,55 @@ describe('loading function', () => {
2929
});
3030

3131
describe('request to HTTP function', () => {
32-
it('should return transformed body', () => {
33-
const server = invoker.getServer(
34-
(req: express.Request, res: express.Response) => {
35-
res.send(req.body.text.toUpperCase());
36-
},
37-
invoker.SignatureType.HTTP
38-
);
39-
return supertest(server)
40-
.post('/')
41-
.send({ text: 'hello' })
42-
.set('Content-Type', 'application/json')
43-
.expect('HELLO')
44-
.expect(200);
32+
interface TestData {
33+
name: string;
34+
path: string;
35+
text: string;
36+
status: number;
37+
}
38+
39+
const testData: TestData[] = [
40+
{
41+
name: 'empty path',
42+
path: '/',
43+
text: 'HELLO',
44+
status: 200,
45+
},
46+
{
47+
name: 'simple path',
48+
path: '/foo',
49+
text: 'HELLO',
50+
status: 200,
51+
},
52+
{
53+
name: 'with favicon.ico',
54+
path: '/favicon.ico',
55+
text: 'Not Found',
56+
status: 404,
57+
},
58+
{
59+
name: 'with robots.txt',
60+
path: '/robots.txt',
61+
text: 'Not Found',
62+
status: 404,
63+
},
64+
];
65+
66+
testData.forEach(test => {
67+
it(`should return transformed body: ${test.name}`, () => {
68+
const server = invoker.getServer(
69+
(req: express.Request, res: express.Response) => {
70+
res.send(req.body.text.toUpperCase());
71+
},
72+
invoker.SignatureType.HTTP
73+
);
74+
return supertest(server)
75+
.post(test.path)
76+
.send({ text: 'hello' })
77+
.set('Content-Type', 'application/json')
78+
.expect(test.text)
79+
.expect(test.status);
80+
});
4581
});
4682
});
4783

0 commit comments

Comments
 (0)