( userFunction: HandlerFunction, options: FrameworkOptions, )
| 35 | * @return HTTP server |
| 36 | */ |
| 37 | export function getServer( |
| 38 | userFunction: HandlerFunction, |
| 39 | options: FrameworkOptions, |
| 40 | ): http.Server { |
| 41 | // App to use for function executions. |
| 42 | const app = express(); |
| 43 | |
| 44 | // Express middleware |
| 45 | |
| 46 | // Set request-specific values in the very first middleware. |
| 47 | app.use('/{*splat}', (req, res, next) => { |
| 48 | setLatestRes(res); |
| 49 | res.locals.functionExecutionFinished = false; |
| 50 | next(); |
| 51 | }); |
| 52 | |
| 53 | /** |
| 54 | * Retains a reference to the raw body buffer to allow access to the raw body |
| 55 | * for things like request signature validation. This is used as the "verify" |
| 56 | * function in body-parser options. |
| 57 | * @param req - Express request object |
| 58 | * @param res - Express response object |
| 59 | * @param buf - Buffer to be saved |
| 60 | */ |
| 61 | function rawBodySaver(req: Request, res: Response, buf: Buffer) { |
| 62 | req.rawBody = buf; |
| 63 | } |
| 64 | |
| 65 | // Set limit to a value larger than 32MB, which is maximum limit of higher |
| 66 | // level layers anyway. |
| 67 | const requestLimit = '1024mb'; |
| 68 | const defaultBodySavingOptions = { |
| 69 | limit: requestLimit, |
| 70 | verify: rawBodySaver, |
| 71 | }; |
| 72 | const cloudEventsBodySavingOptions = { |
| 73 | type: 'application/cloudevents+json', |
| 74 | limit: requestLimit, |
| 75 | verify: rawBodySaver, |
| 76 | }; |
| 77 | const rawBodySavingOptions = { |
| 78 | limit: requestLimit, |
| 79 | verify: rawBodySaver, |
| 80 | type: '*/*', |
| 81 | }; |
| 82 | |
| 83 | // Use extended query string parsing for URL-encoded bodies. |
| 84 | const urlEncodedOptions = { |
| 85 | limit: requestLimit, |
| 86 | verify: rawBodySaver, |
| 87 | extended: true, |
| 88 | }; |
| 89 | |
| 90 | // Apply middleware |
| 91 | app.use(bodyParser.json(cloudEventsBodySavingOptions)); |
| 92 | app.use(bodyParser.json(defaultBodySavingOptions)); |
| 93 | app.use(bodyParser.text(defaultBodySavingOptions)); |
| 94 | app.use(bodyParser.urlencoded(urlEncodedOptions)); |
no test coverage detected