@@ -17,27 +17,27 @@ import * as express from 'express';
1717import * as http from 'http' ;
1818import * as onFinished from 'on-finished' ;
1919import { HandlerFunction , Request , Response } from './functions' ;
20- import { SignatureType } from './types' ;
2120import { setLatestRes } from './invoker' ;
2221import { legacyPubSubEventMiddleware } from './pubsub_middleware' ;
2322import { cloudEventToBackgroundEventMiddleware } from './middleware/cloud_event_to_background_event' ;
2423import { backgroundEventToCloudEventMiddleware } from './middleware/background_event_to_cloud_event' ;
24+ import { timeoutMiddleware } from './middleware/timeout' ;
2525import { wrapUserFunction } from './function_wrappers' ;
2626import { asyncLocalStorageMiddleware } from './async_local_storage' ;
2727import { executionContextMiddleware } from './execution_context' ;
2828import { errorHandler } from './logger' ;
29+ import { FrameworkOptions } from './options' ;
2930
3031/**
3132 * Creates and configures an Express application and returns an HTTP server
3233 * which will run it.
3334 * @param userFunction User's function.
34- * @param functionSignatureType Type of user's function signature .
35+ * @param options the configured Function Framework options .
3536 * @return HTTP server.
3637 */
3738export function getServer (
3839 userFunction : HandlerFunction ,
39- functionSignatureType : SignatureType ,
40- enableExecutionId : boolean
40+ options : FrameworkOptions
4141) : http . Server {
4242 // App to use for function executions.
4343 const app = express ( ) ;
@@ -89,7 +89,7 @@ export function getServer(
8989 } ;
9090
9191 // Apply middleware
92- if ( functionSignatureType !== 'typed' ) {
92+ if ( options . signatureType !== 'typed' ) {
9393 // If the function is not typed then JSON parsing can be done automatically, otherwise the
9494 // functions format must determine deserialization.
9595 app . use ( bodyParser . json ( cloudEventsBodySavingOptions ) ) ;
@@ -120,23 +120,23 @@ export function getServer(
120120 app . use ( asyncLocalStorageMiddleware ) ;
121121
122122 if (
123- functionSignatureType === 'event' ||
124- functionSignatureType === 'cloudevent'
123+ options . signatureType === 'event' ||
124+ options . signatureType === 'cloudevent'
125125 ) {
126126 // If a Pub/Sub subscription is configured to invoke a user's function directly, the request body
127127 // needs to be marshalled into the structure that wrapEventFunction expects. This unblocks local
128128 // development with the Pub/Sub emulator
129129 app . use ( legacyPubSubEventMiddleware ) ;
130130 }
131131
132- if ( functionSignatureType === 'event' ) {
132+ if ( options . signatureType === 'event' ) {
133133 app . use ( cloudEventToBackgroundEventMiddleware ) ;
134134 }
135- if ( functionSignatureType === 'cloudevent' ) {
135+ if ( options . signatureType === 'cloudevent' ) {
136136 app . use ( backgroundEventToCloudEventMiddleware ) ;
137137 }
138138
139- if ( functionSignatureType === 'http' ) {
139+ if ( options . signatureType === 'http' ) {
140140 app . use ( '/favicon.ico|/robots.txt' , ( req , res ) => {
141141 // Neither crawlers nor browsers attempting to pull the icon find the body
142142 // contents particularly useful, so we send nothing in the response body.
@@ -151,16 +151,18 @@ export function getServer(
151151 } ) ;
152152 }
153153
154+ app . use ( timeoutMiddleware ( options . timeoutMilliseconds ) ) ;
155+
154156 // Set up the routes for the user's function
155- const requestHandler = wrapUserFunction ( userFunction , functionSignatureType ) ;
156- if ( functionSignatureType === 'http' ) {
157+ const requestHandler = wrapUserFunction ( userFunction , options . signatureType ) ;
158+ if ( options . signatureType === 'http' ) {
157159 app . all ( '/*' , requestHandler ) ;
158160 } else {
159161 app . post ( '/*' , requestHandler ) ;
160162 }
161163
162164 // Error Handler
163- if ( enableExecutionId ) {
165+ if ( options . enableExecutionId ) {
164166 app . use ( errorHandler ) ;
165167 }
166168
0 commit comments