|
| 1 | +import { format } from 'url' |
| 2 | +import { Request, Response } from 'servie' |
| 3 | + |
| 4 | +export type Middleware = (req: Request, res: Response, next: () => Promise<void>) => Promise<void> |
| 5 | + |
| 6 | +/** |
| 7 | + * AWS Lambda event object. |
| 8 | + */ |
| 9 | +export interface Event { |
| 10 | + path: string |
| 11 | + httpMethod: string |
| 12 | + body: string | null |
| 13 | + isBase64Encoded?: boolean |
| 14 | + resource: string |
| 15 | + headers: { |
| 16 | + [key: string]: string |
| 17 | + } | null |
| 18 | + queryStringParameters: { |
| 19 | + [key: string]: string |
| 20 | + } | null |
| 21 | + pathParameters: { |
| 22 | + [key: string]: string |
| 23 | + } | null |
| 24 | + requestContext: { |
| 25 | + identity: { |
| 26 | + sourceIp: string |
| 27 | + } |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +/** |
| 32 | + * AWS lambda context object. |
| 33 | + */ |
| 34 | +export interface Context { |
| 35 | + functionName: string |
| 36 | + memoryLimitInMB: string |
| 37 | + functionVersion: string |
| 38 | + invokeid: string |
| 39 | + awsRequestId: string |
| 40 | + invokedFunctionArn: string |
| 41 | +} |
| 42 | + |
| 43 | +/** |
| 44 | + * Standard lambda HTTP response. |
| 45 | + */ |
| 46 | +export interface Result { |
| 47 | + body?: string |
| 48 | + statusCode?: number |
| 49 | + headers?: { |
| 50 | + [key: string]: string | string[] |
| 51 | + } |
| 52 | + isBase64Encoded?: boolean |
| 53 | +} |
| 54 | + |
| 55 | +/** |
| 56 | + * Lambda server options. |
| 57 | + */ |
| 58 | +export interface Options { |
| 59 | + isBinary?: (res: Response) => boolean |
| 60 | +} |
| 61 | + |
| 62 | +/** |
| 63 | + * Create a server for handling AWS Lambda requests. |
| 64 | + */ |
| 65 | +export function createServer (fn: Middleware, options: Options = {}) { |
| 66 | + return function (event: Event, _context: Context, cb: (err: Error | null, res?: Result) => void): void { |
| 67 | + const { httpMethod: method, headers, isBase64Encoded } = event |
| 68 | + const url = format({ pathname: event.path, query: event.queryStringParameters }) |
| 69 | + const body = event.body ? new Buffer(event.body, isBase64Encoded ? 'base64' : 'utf8') : undefined |
| 70 | + |
| 71 | + const connection = { encrypted: true, remoteAddress: event.requestContext.identity.sourceIp } |
| 72 | + |
| 73 | + const request = new Request({ method, url, connection, headers, body }) |
| 74 | + const response = new Response(request, {}) |
| 75 | + |
| 76 | + // Handle request and response errors. |
| 77 | + request.events.on('error', done) |
| 78 | + response.events.on('error', done) |
| 79 | + |
| 80 | + // Marked request as finished. |
| 81 | + request.started = true |
| 82 | + request.finished = true |
| 83 | + request.bytesTransferred = body ? body.length : 0 |
| 84 | + |
| 85 | + function done (err: Error | null, res?: Result) { |
| 86 | + if (err && (request.aborted || response.started)) { |
| 87 | + console.error(err) |
| 88 | + return |
| 89 | + } |
| 90 | + |
| 91 | + response.started = true |
| 92 | + response.finished = true |
| 93 | + |
| 94 | + return cb(err, res) |
| 95 | + } |
| 96 | + |
| 97 | + fn(request, response, finalhandler(request, response)) |
| 98 | + .then((): void | Promise<void> => { |
| 99 | + if (request.aborted || response.started) { |
| 100 | + return |
| 101 | + } |
| 102 | + |
| 103 | + return response.buffer().then((body) => { |
| 104 | + const isBase64Encoded = options.isBinary ? options.isBinary(response) : false |
| 105 | + |
| 106 | + // Set bytes transferred. |
| 107 | + response.bytesTransferred = body ? body.length : 0 |
| 108 | + |
| 109 | + return done(null, { |
| 110 | + statusCode: response.status, |
| 111 | + body: body ? (isBase64Encoded ? body.toString('base64') : body.toString('utf8')) : undefined, |
| 112 | + headers: response.headers.object(), |
| 113 | + isBase64Encoded |
| 114 | + }) |
| 115 | + }) |
| 116 | + }) |
| 117 | + .catch((err) => done(err)) |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +/** |
| 122 | + * Final throwback server handler. |
| 123 | + */ |
| 124 | +function finalhandler (req: Request, res: Response) { |
| 125 | + return function () { |
| 126 | + res.status = 404 |
| 127 | + res.type = 'text/plain' |
| 128 | + res.body = `Cannot ${req.method} ${req.url}` |
| 129 | + |
| 130 | + return Promise.resolve() |
| 131 | + } |
| 132 | +} |
0 commit comments