-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrequest.ts
222 lines (187 loc) · 6.95 KB
/
request.ts
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import { parseAccountHeader } from "./account.ts";
import { BlobsMetadata, parseBlobsMetadata } from "./blobs.ts";
import { Account, Deploy, Geo, Site } from "./context.ts";
import { parseGeoHeader } from "./geo.ts";
import {
conditionals as conditionalHeaders,
InternalHeaders,
StandardHeaders,
} from "./headers.ts";
import { FeatureFlags, parseFeatureFlagsHeader } from "./feature_flags.ts";
import {
detachedLogger,
LogLevel,
type StructuredLogger,
} from "./log/logger.ts";
import { parseSiteHeader } from "./site.ts";
import { OriginResponse } from "./response.ts";
export const loggerSymbol = Symbol("Netlify Logger");
export const internalsSymbol = Symbol("Netlify Internals");
export const enum CacheMode {
Manual = "manual",
Off = "off",
}
interface EdgeRequestInternals {
account: Account;
blobs: BlobsMetadata;
bypassSettings: string | null;
cacheMode: string | null;
cdnLoop: string | null;
deploy: Deploy;
featureFlags: FeatureFlags;
forwardedHost: string | null;
forwardedProtocol: string | null;
geo: Geo;
ip: string;
passthrough: string | null;
passthroughHost: string | null;
passthroughProtocol: string | null;
passthroughHeaders?: Headers;
requestID: string | null;
site: Site;
purgeAPIToken: string | null;
}
const makeInternals = (headers: Headers): EdgeRequestInternals => {
const site = parseSiteHeader(headers.get(InternalHeaders.SiteInfo));
const deploy: Deploy = {
context: headers.get(InternalHeaders.DeployContext) ?? undefined,
id: headers.get(InternalHeaders.DeployID) ?? undefined,
published: headers.get(InternalHeaders.DeployIsPublished) === "1",
};
return {
account: parseAccountHeader(
headers.get(InternalHeaders.AccountInfo),
),
blobs: parseBlobsMetadata(headers.get(InternalHeaders.BlobsInfo)),
bypassSettings: headers.get(InternalHeaders.EdgeFunctionBypass),
cacheMode: headers.get(InternalHeaders.EdgeFunctionCache),
cdnLoop: headers.get(StandardHeaders.CDNLoop),
deploy,
featureFlags: parseFeatureFlagsHeader(
headers.get(InternalHeaders.FeatureFlags),
),
forwardedHost: headers.get(InternalHeaders.ForwardedHost),
forwardedProtocol: headers.get(InternalHeaders.ForwardedProtocol),
geo: parseGeoHeader(headers.get(InternalHeaders.Geo)),
ip: headers.get(InternalHeaders.IP) ?? "",
passthrough: headers.get(InternalHeaders.Passthrough),
passthroughHost: headers.get(InternalHeaders.PassthroughHost),
passthroughProtocol: headers.get(InternalHeaders.PassthroughProtocol),
requestID: headers.get(InternalHeaders.RequestID),
purgeAPIToken: headers.get(InternalHeaders.PurgeAPIToken),
site,
};
};
export class EdgeRequest extends Request {
[internalsSymbol]: EdgeRequestInternals;
[loggerSymbol]: StructuredLogger;
constructor(input: RequestInfo | URL, init?: RequestInit) {
const base = input instanceof URL ? new Request(input, init) : input;
super(base);
const internals = init instanceof EdgeRequest
? init[internalsSymbol]
: makeInternals(this.headers);
this[internalsSymbol] = internals;
const requestID = this.headers.get(InternalHeaders.RequestID);
const logLevel = this.headers.has(InternalHeaders.DebugLogging)
? LogLevel.Debug
: LogLevel.Log;
this[loggerSymbol] = detachedLogger.withRequestID(requestID).withLogLevel(
logLevel,
);
[
InternalHeaders.AccountInfo,
InternalHeaders.ForwardedHost,
InternalHeaders.ForwardedProtocol,
InternalHeaders.Geo,
InternalHeaders.IP,
InternalHeaders.EdgeFunctions,
InternalHeaders.LegacyEdgeFunctions,
InternalHeaders.InvocationMetadata,
InternalHeaders.Passthrough,
InternalHeaders.LegacyPassthrough,
InternalHeaders.PassthroughHost,
InternalHeaders.PassthroughProtocol,
InternalHeaders.FeatureFlags,
InternalHeaders.EdgeFunctionBypass,
InternalHeaders.SiteInfo,
].forEach((header) => {
this.headers.delete(header);
});
}
}
export const getAccount = (request: EdgeRequest) =>
request[internalsSymbol].account;
export const getBlobs = (request: EdgeRequest) =>
request[internalsSymbol].blobs;
export const getCacheMode = (request: EdgeRequest) =>
request[internalsSymbol].cacheMode === CacheMode.Manual
? CacheMode.Manual
: CacheMode.Off;
export const getDeploy = (request: EdgeRequest) =>
request[internalsSymbol].deploy;
export const getGeoLocation = (request: EdgeRequest) =>
request[internalsSymbol].geo;
export const getIP = (request: EdgeRequest) => request[internalsSymbol].ip;
export const getLogger = (request: EdgeRequest) => request[loggerSymbol];
export const getRequestID = (request: EdgeRequest) =>
request[internalsSymbol].requestID ?? "";
export const getBypassSettings = (request: EdgeRequest) =>
request[internalsSymbol].bypassSettings;
export const getPassthroughHeaders = (request: EdgeRequest) =>
request[internalsSymbol].passthroughHeaders ?? new Headers();
export const getPurgeAPIToken = (request: EdgeRequest) =>
request[internalsSymbol].purgeAPIToken;
export const setPassthroughHeaders = (
request: EdgeRequest,
originResponse: OriginResponse,
) => {
request[internalsSymbol].passthroughHeaders =
originResponse[internalsSymbol].passthroughHeaders;
};
/**
* Returns all feature flags for the request.
* Only flags with prefix edge_functions_bootstrap_ are returned.
* Beware: Only for Netlify-Internal use!
*/
export const getFeatureFlags = (request: EdgeRequest) =>
request[internalsSymbol].featureFlags;
export const getSite = (request: EdgeRequest) => request[internalsSymbol].site;
interface PassthroughRequestOptions {
req: EdgeRequest;
stripConditionalHeaders?: boolean;
url?: URL;
}
export class PassthroughRequest extends Request {
constructor({
req,
stripConditionalHeaders = false,
url = new URL(req.url),
}: PassthroughRequestOptions) {
const passthroughHeader = req[internalsSymbol].passthrough;
const requestIDHeader = req[internalsSymbol].requestID;
// The edge node can pass these headers to tell the isolate which host it
// should use for the origin call.
url.host = req[internalsSymbol].passthroughHost ?? url.host;
url.protocol = req[internalsSymbol].passthroughProtocol ?? url.protocol;
let reqInit: Request = req;
if (req.body && req.bodyUsed) {
console.warn(
"Request body already used. To use the body in further processing, pass the request to `context.next()`. See https://ntl.fyi/request-body-used for more information.",
);
reqInit = new Request(req, { body: "" });
}
super(new Request(url.toString(), reqInit));
if (passthroughHeader !== null) {
this.headers.set(InternalHeaders.Passthrough, passthroughHeader);
}
if (requestIDHeader !== null) {
this.headers.set(InternalHeaders.RequestID, requestIDHeader);
}
if (stripConditionalHeaders) {
conditionalHeaders.forEach((name) => {
this.headers.delete(name);
});
}
}
}