Skip to content

Commit 5cccb4c

Browse files
authored
refactor: split files (#135)
Signed-off-by: Grant Timmerman <[email protected]>
1 parent 93ec924 commit 5cccb4c

File tree

5 files changed

+123
-76
lines changed

5 files changed

+123
-76
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cloudevents.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2019 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
import * as express from 'express';
16+
17+
/**
18+
* Checks whether the incoming request is a CloudEvents event in binary content
19+
* mode. This is verified by checking the presence of required headers.
20+
*
21+
* @link https://github.com/cloudevents/spec/blob/master/http-transport-binding.md#31-binary-content-mode
22+
*
23+
* @param req Express request object.
24+
* @return True if the request is a CloudEvents event in binary content mode,
25+
* false otherwise.
26+
*/
27+
export function isBinaryCloudEvent(req: express.Request): boolean {
28+
return !!(
29+
req.header('ce-type') &&
30+
req.header('ce-specversion') &&
31+
req.header('ce-source') &&
32+
req.header('ce-id')
33+
);
34+
}

src/invoker.ts

Lines changed: 27 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ import * as express from 'express';
2626
import * as http from 'http';
2727
import * as onFinished from 'on-finished';
2828

29-
// HTTP header field that is added to Worker response to signalize problems with
30-
// executing the client function.
31-
const FUNCTION_STATUS_HEADER_FIELD = 'X-Google-Status';
29+
import { FUNCTION_STATUS_HEADER_FIELD } from './types';
30+
import { logAndSendError } from './logger';
31+
import { isBinaryCloudEvent } from './cloudevents';
3232

3333
/**
3434
* The Cloud Functions context object for the event.
@@ -267,34 +267,6 @@ function sendResponse(result: any, err: Error | null, res: express.Response) {
267267
}
268268
}
269269

270-
/**
271-
* Logs an error message and sends back an error response to the incoming
272-
* request.
273-
* @param err Error to be logged and sent.
274-
* @param res Express response object.
275-
* @param callback A function to be called synchronously.
276-
*/
277-
function logAndSendError(
278-
// tslint:disable-next-line:no-any
279-
err: Error | any,
280-
res: express.Response | null,
281-
callback?: Function
282-
) {
283-
console.error(err.stack || err);
284-
285-
// If user function has already sent response headers, the response with
286-
// error message cannot be sent. This check is done inside the callback,
287-
// right before sending the response, to make sure that no concurrent
288-
// execution sends the response between the check and 'send' call below.
289-
if (res && !res.headersSent) {
290-
res.set(FUNCTION_STATUS_HEADER_FIELD, 'crash');
291-
res.send(err.message || err);
292-
}
293-
if (callback) {
294-
callback();
295-
}
296-
}
297-
298270
// Set limit to a value larger than 32MB, which is maximum limit of higher level
299271
// layers anyway.
300272
const requestLimit = '1024mb';
@@ -315,31 +287,6 @@ function rawBodySaver(
315287
req.rawBody = buf;
316288
}
317289

318-
const defaultBodySavingOptions = {
319-
limit: requestLimit,
320-
verify: rawBodySaver,
321-
};
322-
323-
const cloudEventsBodySavingOptions = {
324-
type: 'application/cloudevents+json',
325-
limit: requestLimit,
326-
verify: rawBodySaver,
327-
};
328-
329-
// The parser will process ALL content types so must come last.
330-
const rawBodySavingOptions = {
331-
limit: requestLimit,
332-
verify: rawBodySaver,
333-
type: '*/*',
334-
};
335-
336-
// Use extended query string parsing for URL-encoded bodies.
337-
const urlEncodedOptions = {
338-
limit: requestLimit,
339-
verify: rawBodySaver,
340-
extended: true,
341-
};
342-
343290
/**
344291
* Wraps the provided function into an Express handler function with additional
345292
* instrumentation logic.
@@ -366,25 +313,6 @@ function makeHttpHandler(execute: HttpFunction): express.RequestHandler {
366313
};
367314
}
368315

369-
/**
370-
* Checks whether the incoming request is a CloudEvents event in binary content
371-
* mode. This is verified by checking the presence of required headers.
372-
*
373-
* @link https://github.com/cloudevents/spec/blob/master/http-transport-binding.md#31-binary-content-mode
374-
*
375-
* @param req Express request object.
376-
* @return True if the request is a CloudEvents event in binary content mode,
377-
* false otherwise.
378-
*/
379-
function isBinaryCloudEvent(req: express.Request): boolean {
380-
return !!(
381-
req.header('ce-type') &&
382-
req.header('ce-specversion') &&
383-
req.header('ce-source') &&
384-
req.header('ce-id')
385-
);
386-
}
387-
388316
/**
389317
* Returns a CloudEvents context from the given CloudEvents request. Context
390318
* attributes are retrieved from request headers.
@@ -567,6 +495,30 @@ export function getServer(
567495
next();
568496
});
569497

498+
const defaultBodySavingOptions = {
499+
limit: requestLimit,
500+
verify: rawBodySaver,
501+
};
502+
const cloudEventsBodySavingOptions = {
503+
type: 'application/cloudevents+json',
504+
limit: requestLimit,
505+
verify: rawBodySaver,
506+
};
507+
508+
// The parser will process ALL content types so must come last.
509+
const rawBodySavingOptions = {
510+
limit: requestLimit,
511+
verify: rawBodySaver,
512+
type: '*/*',
513+
};
514+
515+
// Use extended query string parsing for URL-encoded bodies.
516+
const urlEncodedOptions = {
517+
limit: requestLimit,
518+
verify: rawBodySaver,
519+
extended: true,
520+
};
521+
570522
app.use(bodyParser.json(cloudEventsBodySavingOptions));
571523
app.use(bodyParser.json(defaultBodySavingOptions));
572524
app.use(bodyParser.text(defaultBodySavingOptions));

src/logger.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright 2019 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
import * as express from 'express';
16+
import { FUNCTION_STATUS_HEADER_FIELD } from './types';
17+
18+
/**
19+
* Logs an error message and sends back an error response to the incoming
20+
* request.
21+
* @param err Error to be logged and sent.
22+
* @param res Express response object.
23+
* @param callback A function to be called synchronously.
24+
*/
25+
export function logAndSendError(
26+
// tslint:disable-next-line:no-any
27+
err: Error | any,
28+
res: express.Response | null,
29+
callback?: Function
30+
) {
31+
console.error(err.stack || err);
32+
33+
// If user function has already sent response headers, the response with
34+
// error message cannot be sent. This check is done inside the callback,
35+
// right before sending the response, to make sure that no concurrent
36+
// execution sends the response between the check and 'send' call below.
37+
if (res && !res.headersSent) {
38+
res.set(FUNCTION_STATUS_HEADER_FIELD, 'crash');
39+
res.send(err.message || err);
40+
}
41+
if (callback) {
42+
callback();
43+
}
44+
}

src/types.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2019 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// HTTP header field that is added to Worker response to signalize problems with
16+
// executing the client function.
17+
export const FUNCTION_STATUS_HEADER_FIELD = 'X-Google-Status';

0 commit comments

Comments
 (0)