-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserverlessRequest.ts
62 lines (51 loc) · 1.47 KB
/
serverlessRequest.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import { IncomingMessage } from 'http';
import { Socket } from 'net';
const HTTPS_PORT = 443;
interface ServerlessRequestOptions {
method: string;
url: string;
path: string;
headers: { [key: string]: string | number };
body: Buffer | string | undefined;
remoteAddress: string;
isBase64Encoded: boolean;
}
const NO_OP: (...args: unknown[]) => unknown = () => void 0;
export default class ServerlessRequest extends IncomingMessage {
ip: string;
body: Buffer | string | undefined;
isBase64Encoded: boolean;
constructor(request: ServerlessRequestOptions) {
super({
encrypted: true,
readable: false,
remoteAddress: request.remoteAddress,
address: () => ({ port: HTTPS_PORT }),
end: NO_OP,
destroy: NO_OP,
path: request.path,
headers: request.headers,
} as unknown as Socket);
const combinedHeaders = Object.fromEntries(
Object.entries({
...request.headers,
'content-length': Buffer.byteLength(request.body ?? '').toString(),
}).map(([key, value]) => [key.toLowerCase(), value]),
);
Object.assign(this, {
...request,
complete: true,
httpVersion: '1.1',
httpVersionMajor: '1',
httpVersionMinor: '1',
headers: combinedHeaders,
});
this.body = request.body;
this.ip = request.remoteAddress;
this.isBase64Encoded = request.isBase64Encoded;
this._read = () => {
this.push(request.body);
this.push(null);
};
}
}