-
Notifications
You must be signed in to change notification settings - Fork 670
/
Copy pathrequest.js
44 lines (38 loc) · 1.16 KB
/
request.js
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
// ATTRIBUTION: https://github.com/dougmoscrop/serverless-http
const http = require('http')
const HTTPS_PORT = 443
module.exports = class ServerlessRequest extends http.IncomingMessage {
constructor ({ method, url, headers, body, remoteAddress }) {
super({
encrypted: true,
readable: true,
remoteAddress,
address: () => ({ port: HTTPS_PORT }),
end: Function.prototype,
destroy: Function.prototype
})
// IncomingMessage has a lot of logic for when to lowercase or alias well-known header names,
// so we delegate to that logic here
const headerEntries = Object.entries(headers)
const rawHeaders = new Array(headerEntries.length * 2)
for (let i = 0; i < headerEntries.length; i++) {
rawHeaders[i * 2] = headerEntries[i][0]
rawHeaders[i * 2 + 1] = headerEntries[i][1]
}
this._addHeaderLines(rawHeaders, rawHeaders.length)
Object.assign(this, {
ip: remoteAddress,
complete: true,
httpVersion: '1.1',
httpVersionMajor: '1',
httpVersionMinor: '1',
method,
body,
url
})
this._read = () => {
this.push(body)
this.push(null)
}
}
}