Skip to content

fixes #33: Only listen to functionTarget. #81

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 5 commits into from
Sep 6, 2019
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
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ if (!USER_FUNCTION) {
process.exit(1);
}

const SERVER = getServer(USER_FUNCTION!, SIGNATURE_TYPE!);
const SERVER = getServer(USER_FUNCTION!, SIGNATURE_TYPE!, TARGET);
const ERROR_HANDLER = new ErrorHandler(SERVER);
SERVER.listen(PORT, () => {
ERROR_HANDLER.register();
Expand Down
19 changes: 13 additions & 6 deletions src/invoker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -477,22 +477,23 @@ function wrapEventFunction(
function registerFunctionRoutes(
app: express.Application,
userFunction: HandlerFunction,
functionTarget: string,
functionSignatureType: SignatureType
) {
if (isHttpFunction(userFunction!, functionSignatureType)) {
app.use('/*', (req, res, next) => {
app.use(`/${functionTarget}`, (req, res, next) => {
onFinished(res, (err, res) => {
res.locals.functionExecutionFinished = true;
});
next();
});

app.all('/*', (req, res, next) => {
app.all(`/${functionTarget}`, (req, res, next) => {
const handler = makeHttpHandler(userFunction);
handler(req, res, next);
});
} else {
app.post('/*', (req, res, next) => {
app.post(`/${functionTarget}`, (req, res, next) => {
const wrappedUserFunction = wrapEventFunction(userFunction);
const handler = makeHttpHandler(wrappedUserFunction);
handler(req, res, next);
Expand Down Expand Up @@ -548,13 +549,14 @@ export class ErrorHandler {
*/
export function getServer(
userFunction: HandlerFunction,
functionSignatureType: SignatureType
functionSignatureType: SignatureType,
functionTarget: string
): http.Server {
// App to use for function executions.
const app = express();

// Set request-specific values in the very first middleware.
app.use('/*', (req, res, next) => {
app.use(`/${functionTarget}`, (req, res, next) => {
latestRes = res;
res.locals.functionExecutionFinished = false;
next();
Expand All @@ -569,7 +571,12 @@ export function getServer(
// skipped when one is matched.
app.use(bodyParser.raw(rawBodySavingOptions));

registerFunctionRoutes(app, userFunction, functionSignatureType);
registerFunctionRoutes(
app,
userFunction,
functionTarget,
functionSignatureType
);

app.enable('trust proxy'); // To respect X-Forwarded-For header.

Expand Down
33 changes: 21 additions & 12 deletions test/invoker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@ describe('request to HTTP function', () => {
(req: express.Request, res: express.Response) => {
res.send(req.body.text.toUpperCase());
},
invoker.SignatureType.HTTP
invoker.SignatureType.HTTP,
'testFunction'
);
return supertest(server)
.post('/')
.post('/testFunction')
.send({ text: 'hello' })
.set('Content-Type', 'application/json')
.expect('HELLO')
Expand Down Expand Up @@ -102,12 +103,16 @@ describe('GCF event request to event function', () => {
it(`should receive data and context from ${test.name}`, async () => {
let receivedData: {} | null = null;
let receivedContext: invoker.CloudFunctionsContext | null = null;
const server = invoker.getServer((data: {}, context: invoker.Context) => {
receivedData = data;
receivedContext = context as invoker.CloudFunctionsContext;
}, invoker.SignatureType.EVENT);
const server = invoker.getServer(
(data: {}, context: invoker.Context) => {
receivedData = data;
receivedContext = context as invoker.CloudFunctionsContext;
},
invoker.SignatureType.EVENT,
'testFunction'
);
await supertest(server)
.post('/')
.post('/testFunction')
.send(test.body)
.set('Content-Type', 'application/json')
.expect(204);
Expand Down Expand Up @@ -165,12 +170,16 @@ describe('CloudEvents request to event function', () => {
it(`should receive data and context from ${test.name}`, async () => {
let receivedData: {} | null = null;
let receivedContext: invoker.CloudEventsContext | null = null;
const server = invoker.getServer((data: {}, context: invoker.Context) => {
receivedData = data;
receivedContext = context as invoker.CloudEventsContext;
}, invoker.SignatureType.EVENT);
const server = invoker.getServer(
(data: {}, context: invoker.Context) => {
receivedData = data;
receivedContext = context as invoker.CloudEventsContext;
},
invoker.SignatureType.EVENT,
'testFunction'
);
await supertest(server)
.post('/')
.post('/testFunction')
.set(test.headers)
.send(test.body)
.expect(204);
Expand Down