-
Notifications
You must be signed in to change notification settings - Fork 659
/
http-function-runtime-v3.js
103 lines (80 loc) · 2.84 KB
/
http-function-runtime-v3.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
const url = require('url')
const { getCommaDelimitedHeaders, parseCookie } = require('../utils')
function getRequestValuesFromHttpFunctionEvent ({ event }) {
const context = event.req
const method = context.method
const urlObject = new url.URL(context.url)
const path = urlObject.pathname + urlObject.search
const headers = { cookies: context.headers.cookie }
Object.entries(context.headers).forEach(([headerKey, headerValue]) => {
headers[headerKey.toLowerCase()] = headerValue
})
const remoteAddress = headers['x-forwarded-for']
const body = context.rawBody
if (body) {
headers['content-length'] = Buffer.byteLength(body, 'utf8')
}
return {
method,
headers,
body,
remoteAddress,
path
}
}
function getResponseToHttpFunction ({ statusCode, body, headers = {}, isBase64Encoded = false, response = {} }) {
const responseToHttpFunction = {
statusCode,
body,
isBase64Encoded
}
if (isBase64Encoded) {
responseToHttpFunction.body = Buffer.from(body, 'base64')
responseToHttpFunction.isBase64Encoded = false
}
const cookies = []
// headers['set-cookie'] can be a string of one cookie, or an array of cookies
// headerCookies should always be an array
const headerCookies = [].concat(headers['set-cookie'] || [])
// Convert 'set-cookie' to Azure Function 3.x cookie object array
// https://github.com/Azure/azure-functions-nodejs-worker/blob/v3.x/types/index.d.ts#L150
if (headerCookies.length > 0) {
for (const headerCookie of headerCookies) {
const parsedCookie = parseCookie(headerCookie)
const nameValueTuple = headerCookie.split(';')[0].split('=')
const cookie = { name: nameValueTuple[0], value: nameValueTuple[1] }
if (headerCookie.toLowerCase().includes('httponly')) {
cookie.httpOnly = true
}
if (headerCookie.toLowerCase().includes('secure')) {
cookie.secure = true
}
if (parsedCookie['max-age']) {
cookie.maxAge = parsedCookie['max-age']
}
if (parsedCookie.samesite) {
cookie.sameSite = parsedCookie.samesite
}
if (parsedCookie.expires && typeof parsedCookie.expires === 'string') {
cookie.expires = new Date(parsedCookie.expires)
} else if (parsedCookie.expires && typeof value === 'number') {
cookie.expires = parsedCookie.expires
}
if (parsedCookie.path) {
cookie.path = parsedCookie.path
}
if (parsedCookie.domain) {
cookie.domain = parsedCookie.domain
}
cookies.push(cookie)
}
responseToHttpFunction.cookies = cookies
delete headers['set-cookie']
}
responseToHttpFunction.headers = getCommaDelimitedHeaders({ headersMap: headers })
return responseToHttpFunction
}
module.exports = {
getRequest: getRequestValuesFromHttpFunctionEvent,
getResponse: getResponseToHttpFunction
}