Repository navigation
Expand file tree
/
Copy pathredirects.js
More file actions
121 lines (108 loc) · 5.38 KB
/
Copy pathredirects.js
File metadata and controls
121 lines (108 loc) · 5.38 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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// CloudFront Function (viewer-request) for registry URL rewriting.
//
// Responsibilities, in order:
// 1. 301 redirects for package URLs that no longer exist: legacy versioned
// packages (e.g. aws-v6 -> aws@6.x) and removed Install & config pages.
// 2. Normalize the Accept header to two buckets -- "text/markdown" if the
// client accepts markdown, otherwise the header is removed. The default
// cache behavior includes Accept in the cache key, so normalization
// prevents every unique browser Accept string from creating its own
// cache entry.
// 3. Markdown content negotiation for hand-edited /registry/ pages:
// - /foo.md -> internally rewrite to Hugo's /foo/index.md output.
// - Accept: text/markdown on /foo/ or /foo/index.html -> internally
// rewrite to /foo/index.md. The client's URL stays put; Accept is
// in the cache key, so the HTML and markdown variants do not
// pollute each other's cache entries.
// API-generated pages under /api-docs/ are skipped: they have no backing
// index.md, so rewrites would produce 404s.
var EXACT_REDIRECTS = [
{ match: "/registry/packages/aws-v6/how-to-guides/aws-py-oidc-provider-pulumi-cloud/index.html", target: "/docs/esc/guides/configuring-oidc/aws/" },
{ match: "/registry/packages/azure-native-v2/how-to-guides/azure-py-oidc-provider-pulumi-cloud/index.html", target: "/docs/esc/guides/configuring-oidc/azure/" },
];
var PREFIX_REDIRECTS = [
{ match: /^\/registry\/packages\/aws-v6\/?(.*)/, replace: "/registry/packages/aws@6.x/$1" },
{ match: /^\/registry\/packages\/azure-native-v2\/?(.*)/, replace: "/registry/packages/azure-native@2.x/$1" },
// Packages whose Install & config page was deleted because it duplicated
// the Overview page, or was merged into it. See pulumi/registry#12225,
// #12226 and #12227.
//
// Hugo `aliases:` can't do this: resourcedocsgen rewrites each _index.md
// wholesale on every release, erasing front matter. This table isn't
// generated, so a rule here survives.
//
// The suffix group catches every form the URL arrives in: bare, trailing
// slash, /index.html, and /index.md or .md from markdown negotiation.
// Markdown clients keep Accept across the 301, so they still get markdown.
{
match: /^\/registry\/packages\/(aiven|alicloud|artifactory|auth0|azuredevops|cloudamqp|cloudflare|cloudinit|confluentcloud|consul|databricks|datadog|dbtcloud|digitalocean|dnsimple|docker|f5bigip|fastly|github|gitlab|hcloud|kafka|keycloak|kong|linode|mailgun|meraki|minio|mongodbatlas|mysql|newrelic|nomad|ns1|okta|opsgenie|pagerduty|postgresql|pulumiservice|rabbitmq|rancher2|random|signalfx|snowflake|splunk|spotinst|tailscale|tls|vault|venafi|vsphere)\/installation-configuration(?:\.md|\/.*)?$/,
replace: "/registry/packages/$1/",
},
];
function redirect(location) {
return {
statusCode: 301,
statusDescription: "Moved Permanently",
headers: {
location: { value: location },
"cache-control": { value: "max-age=604800" },
},
};
}
function handler(event) {
var request = event.request;
var uri = request.uri;
var i, rule;
for (i = 0; i < EXACT_REDIRECTS.length; i++) {
rule = EXACT_REDIRECTS[i];
if (uri === rule.match) {
return redirect(rule.target);
}
}
for (i = 0; i < PREFIX_REDIRECTS.length; i++) {
rule = PREFIX_REDIRECTS[i];
if (rule.match.test(uri)) {
return redirect(uri.replace(rule.match, rule.replace));
}
}
// Normalize Accept for cache-key stability. Must happen before any
// decision that reads Accept, since we also use the normalized value
// for the markdown-negotiation branch below.
var rawAccept = request.headers["accept"] ? request.headers["accept"].value : "";
var wantsMarkdown = rawAccept.indexOf("text/markdown") !== -1;
if (wantsMarkdown) {
request.headers["accept"] = { value: "text/markdown" };
} else if (request.headers["accept"]) {
delete request.headers["accept"];
}
// Markdown handling is scoped to /registry/* (plus the /registry.md alias
// for the registry landing). Other paths on this distribution (/uploads/,
// /fingerprinted/, etc.) are unrelated.
if (!uri.startsWith("/registry/") && uri !== "/registry.md") {
return request;
}
// API-generated pages have no backing index.md. Leave them alone.
if (uri.indexOf("/api-docs/") !== -1) {
return request;
}
// /foo.md -> origin fetches /foo/index.md. Skip URIs that already end in
// /index.md (direct-file access) so we don't rewrite to /foo/index/index.md.
if (uri.endsWith(".md") && !uri.endsWith("/index.md")) {
request.uri = uri.slice(0, -".md".length) + "/index.md";
return request;
}
// Accept: text/markdown on a directory or /index.html URL -> serve the
// sibling /index.md from origin. Cache key includes the normalized Accept,
// so HTML and markdown variants of the same URI get separate cache entries.
if (wantsMarkdown) {
if (uri.endsWith("/index.html")) {
request.uri = uri.slice(0, -"/index.html".length) + "/index.md";
return request;
}
if (uri.endsWith("/")) {
request.uri = uri + "index.md";
return request;
}
}
return request;
}