-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathproxy.ts
More file actions
170 lines (147 loc) · 5.27 KB
/
Copy pathproxy.ts
File metadata and controls
170 lines (147 loc) · 5.27 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import { NextResponse, type NextRequest } from "next/server";
import {
clearDashboardAuthHintCookieHeader,
dashboardAuthHintCookieHeader,
hasDashboardAuthHint,
hasDashboardSessionCookie,
requestUsesSecureCookies,
} from "./lib/dashboard-auth-hint.ts";
import {
WATCH_BOOTSTRAP_HEADER,
WATCH_BOOTSTRAP_MS_HEADER,
encodeWatchBootstrapHeader,
} from "./lib/watch-bootstrap.ts";
const GEO_HEADER_NAMES = [
"x-vercel-id",
"x-vercel-ip-country",
"x-vercel-ip-country-region",
"x-vercel-ip-continent",
"x-vercel-ip-latitude",
"x-vercel-ip-longitude",
];
const FORWARDED_CONTEXT_HEADER_NAMES = [
"x-forwarded-host",
"x-forwarded-port",
"x-forwarded-proto",
];
function playerAssetId(pathname: string) {
const parts = pathname.split("/").filter(Boolean);
if (parts.length !== 2 || (parts[0] !== "watch" && parts[0] !== "embed")) return null;
const assetId = parts[1]?.trim().toLowerCase();
return assetId && /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/.test(assetId)
? assetId
: null;
}
function forwardedBootstrapHeaders(request: NextRequest) {
const headers = new Headers({
accept: "application/json",
"cache-control": "no-store",
pragma: "no-cache",
});
for (const name of GEO_HEADER_NAMES) {
const value = request.headers.get(name);
if (value) headers.set(name, value);
}
for (const name of FORWARDED_CONTEXT_HEADER_NAMES) {
const value = request.headers.get(name);
if (value) headers.set(name, value);
}
return headers;
}
const DEFAULT_BOOTSTRAP_TIMEOUT_MS = 1500;
function envString(name: string) {
return (process.env[name] || "").trim();
}
function bootstrapTimeoutMs() {
const raw = Number(envString("REND_WATCH_BOOTSTRAP_TIMEOUT_MS"));
if (Number.isFinite(raw) && raw > 0) return Math.min(raw, 3_000);
return DEFAULT_BOOTSTRAP_TIMEOUT_MS;
}
function internalBootstrapOrigin(request: NextRequest) {
const configured = envString("REND_WATCH_BOOTSTRAP_ORIGIN");
if (!configured) return request.url;
try {
const parsed = new URL(configured);
if (!["http:", "https:"].includes(parsed.protocol)) return request.url;
if (parsed.username || parsed.password || parsed.search || parsed.hash) return request.url;
parsed.pathname = parsed.pathname.replace(/\/+$/, "");
return parsed.toString();
} catch {
return request.url;
}
}
function setCookieHeaders(headers: Headers) {
const values = (headers as Headers & { getSetCookie?: () => string[] }).getSetCookie?.();
if (values?.length) return values;
const value = headers.get("set-cookie");
return value ? [value] : [];
}
function dashboardAuthHintSetCookie(request: NextRequest) {
const cookieHeader = request.headers.get("cookie");
const hasHint = hasDashboardAuthHint(cookieHeader);
const hasSessionCookie = hasDashboardSessionCookie(cookieHeader);
const secure = requestUsesSecureCookies(request);
if (hasSessionCookie && !hasHint) return dashboardAuthHintCookieHeader({ secure });
if (!hasSessionCookie && hasHint) return clearDashboardAuthHintCookieHeader({ secure });
return null;
}
export async function proxy(request: NextRequest) {
const requestHeaders = new Headers(request.headers);
const dashboardSetCookie = dashboardAuthHintSetCookie(request);
const assetId = playerAssetId(request.nextUrl.pathname);
if (!assetId) {
const response = NextResponse.next({
request: {
headers: requestHeaders,
},
});
if (dashboardSetCookie) response.headers.append("set-cookie", dashboardSetCookie);
return response;
}
const startedAt = Date.now();
const bootstrapUrl = new URL(`/api/player/${encodeURIComponent(assetId)}`, internalBootstrapOrigin(request));
const playbackBaseUrl = request.nextUrl.searchParams.get("playbackBaseUrl");
if (playbackBaseUrl) bootstrapUrl.searchParams.set("playbackBaseUrl", playbackBaseUrl);
const bootstrapResponse = await fetch(bootstrapUrl, {
cache: "no-store",
headers: forwardedBootstrapHeaders(request),
signal: AbortSignal.timeout(bootstrapTimeoutMs()),
}).catch(() => null);
const responseHeaders = new Headers();
if (dashboardSetCookie) responseHeaders.append("set-cookie", dashboardSetCookie);
if (!bootstrapResponse) {
const response = NextResponse.next({
request: {
headers: requestHeaders,
},
headers: responseHeaders,
});
response.headers.set("cache-control", "no-store");
return response;
}
for (const setCookie of setCookieHeaders(bootstrapResponse.headers)) {
responseHeaders.append("set-cookie", setCookie);
}
const data = await bootstrapResponse.json().catch(() => null);
const encoded = encodeWatchBootstrapHeader(data);
if (encoded) {
requestHeaders.set(WATCH_BOOTSTRAP_HEADER, encoded);
requestHeaders.set(WATCH_BOOTSTRAP_MS_HEADER, String(Math.max(0, Date.now() - startedAt)));
}
const response = NextResponse.next({
request: {
headers: requestHeaders,
},
headers: responseHeaders,
});
response.headers.set("cache-control", "no-store");
response.headers.append("server-timing", `rendwatchbootstrap;dur=${Math.max(0, Date.now() - startedAt)}`);
return response;
}
export const config = {
matcher: [
"/watch/:path*",
"/embed/:path*",
"/((?!api|dashboard|operator|watch|embed|_next/static|_next/image|.*\\..*).*)",
],
};