This repository was archived by the owner on Nov 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcloudFront_function.js
More file actions
73 lines (64 loc) · 2.46 KB
/
cloudFront_function.js
File metadata and controls
73 lines (64 loc) · 2.46 KB
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
// The JS runtime is limited: https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/functions-javascript-runtime-features.html
function handler(event) {
// Request event format: https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/functions-event-structure.html
// Response format: https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/functions-event-structure.html#functions-event-structure-response, https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/example-function-redirect-url.html
var request = event.request
var redirectUri = getRedirectTarget(request.uri, request.querystring)
return {
statusCode: 301,
statusDescription: 'Moved Permanently',
headers: {
location: { value: redirectUri },
},
};
}
function getRedirectTarget(path, query) {
// path always starts with a forward slash
// Redirect subversions to the main version
var match = /^\/pro\/v\/[^\/]*(\/.*)?$/.exec(path)
if (match) {
path = '/pro' + (match[1] || '')
}
// Initial page
match = /^\/pro\/?$/.exec(path)
if (match) {
return 'https://dev.fingerprint.com/docs/introduction' + queryToString(query)
}
// JS agent guides
match = /^\/pro\/js-agent\/(npm|cdn)\/?$/.exec(path)
if (match) {
return 'https://dev.fingerprint.com/docs/js-agent' + queryToString(query)
}
// Other 2nd level articles
match = /^\/pro\/[^\/]*\/([^\/]+)\/?$/.exec(path)
if (match) {
return 'https://dev.fingerprint.com/docs/' + match[1] + queryToString(query)
}
// Other /pro pages, including root-level articles
match = /^\/pro\/(.*)?$/.exec(path)
if (match) {
return 'https://dev.fingerprint.com/docs/' + match[1] + queryToString(query)
}
// Other
return 'https://dev.fingerprint.com' + path + queryToString(query)
}
function queryToString(query) {
// The value format description: https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/functions-event-structure.html#functions-event-structure-query-header-cookie
if (!query) {
return ''
}
var result = Object.keys(query)
.map(function (key) {
if (query[key].multiValue) {
return query[key].multiValue
.map(function (value) {
return encodeURIComponent(key) + '=' + encodeURIComponent(value.value)
})
.join('&')
} else {
return encodeURIComponent(key) + '=' + encodeURIComponent(query[key].value)
}
})
.join('&')
return (result && '?') + result
}