@opennextjs/aws 3.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +94 -0
- package/dist/adapters/binary.d.ts +1 -0
- package/dist/adapters/binary.js +63 -0
- package/dist/adapters/cache.d.ts +96 -0
- package/dist/adapters/cache.js +278 -0
- package/dist/adapters/config/index.d.ts +27 -0
- package/dist/adapters/config/index.js +21 -0
- package/dist/adapters/config/util.d.ts +28 -0
- package/dist/adapters/config/util.js +84 -0
- package/dist/adapters/dynamo-provider.d.ts +8 -0
- package/dist/adapters/dynamo-provider.js +42 -0
- package/dist/adapters/edge-adapter.d.ts +8 -0
- package/dist/adapters/edge-adapter.js +54 -0
- package/dist/adapters/image-optimization-adapter.d.ts +4 -0
- package/dist/adapters/image-optimization-adapter.js +193 -0
- package/dist/adapters/logger.d.ts +13 -0
- package/dist/adapters/logger.js +51 -0
- package/dist/adapters/middleware.d.ts +5 -0
- package/dist/adapters/middleware.js +84 -0
- package/dist/adapters/plugins/14.1/util.d.ts +1 -0
- package/dist/adapters/plugins/14.1/util.js +29 -0
- package/dist/adapters/plugins/image-optimization/image-optimization.d.ts +11 -0
- package/dist/adapters/plugins/image-optimization/image-optimization.js +14 -0
- package/dist/adapters/plugins/image-optimization/image-optimization.replacement.d.ts +11 -0
- package/dist/adapters/plugins/image-optimization/image-optimization.replacement.js +22 -0
- package/dist/adapters/plugins/without-routing/requestHandler.d.ts +2 -0
- package/dist/adapters/plugins/without-routing/requestHandler.js +10 -0
- package/dist/adapters/revalidate.d.ts +9 -0
- package/dist/adapters/revalidate.js +71 -0
- package/dist/adapters/server-adapter.d.ts +4 -0
- package/dist/adapters/server-adapter.js +27 -0
- package/dist/adapters/util.d.ts +10 -0
- package/dist/adapters/util.js +28 -0
- package/dist/adapters/warmer-function.d.ts +12 -0
- package/dist/adapters/warmer-function.js +80 -0
- package/dist/build/bundleNextServer.d.ts +1 -0
- package/dist/build/bundleNextServer.js +87 -0
- package/dist/build/compileConfig.d.ts +3 -0
- package/dist/build/compileConfig.js +64 -0
- package/dist/build/copyTracedFiles.d.ts +1 -0
- package/dist/build/copyTracedFiles.js +207 -0
- package/dist/build/createServerBundle.d.ts +3 -0
- package/dist/build/createServerBundle.js +228 -0
- package/dist/build/edge/createEdgeBundle.d.ts +20 -0
- package/dist/build/edge/createEdgeBundle.js +149 -0
- package/dist/build/generateOutput.d.ts +2 -0
- package/dist/build/generateOutput.js +239 -0
- package/dist/build/helper.d.ts +33 -0
- package/dist/build/helper.js +193 -0
- package/dist/build/patch/patchedAsyncStorage.d.ts +5 -0
- package/dist/build/patch/patchedAsyncStorage.js +16 -0
- package/dist/build/validateConfig.d.ts +2 -0
- package/dist/build/validateConfig.js +76 -0
- package/dist/build.d.ts +7 -0
- package/dist/build.js +533 -0
- package/dist/cache/incremental/s3-lite.d.ts +3 -0
- package/dist/cache/incremental/s3-lite.js +72 -0
- package/dist/cache/incremental/s3.d.ts +3 -0
- package/dist/cache/incremental/s3.js +44 -0
- package/dist/cache/incremental/types.d.ts +34 -0
- package/dist/cache/incremental/types.js +1 -0
- package/dist/cache/next-types.d.ts +62 -0
- package/dist/cache/next-types.js +1 -0
- package/dist/cache/tag/constants.d.ts +2 -0
- package/dist/cache/tag/constants.js +17 -0
- package/dist/cache/tag/dynamodb-lite.d.ts +3 -0
- package/dist/cache/tag/dynamodb-lite.js +174 -0
- package/dist/cache/tag/dynamodb.d.ts +3 -0
- package/dist/cache/tag/dynamodb.js +129 -0
- package/dist/cache/tag/types.d.ts +11 -0
- package/dist/cache/tag/types.js +1 -0
- package/dist/converters/aws-apigw-v1.d.ts +3 -0
- package/dist/converters/aws-apigw-v1.js +89 -0
- package/dist/converters/aws-apigw-v2.d.ts +3 -0
- package/dist/converters/aws-apigw-v2.js +99 -0
- package/dist/converters/aws-cloudfront.d.ts +3 -0
- package/dist/converters/aws-cloudfront.js +170 -0
- package/dist/converters/dummy.d.ts +7 -0
- package/dist/converters/dummy.js +16 -0
- package/dist/converters/edge.d.ts +6 -0
- package/dist/converters/edge.js +88 -0
- package/dist/converters/node.d.ts +3 -0
- package/dist/converters/node.js +45 -0
- package/dist/converters/sqs-revalidate.d.ts +4 -0
- package/dist/converters/sqs-revalidate.js +22 -0
- package/dist/converters/utils.d.ts +1 -0
- package/dist/converters/utils.js +9 -0
- package/dist/core/createGenericHandler.d.ts +11 -0
- package/dist/core/createGenericHandler.js +16 -0
- package/dist/core/createMainHandler.d.ts +18 -0
- package/dist/core/createMainHandler.js +24 -0
- package/dist/core/edgeFunctionHandler.d.ts +54 -0
- package/dist/core/edgeFunctionHandler.js +21 -0
- package/dist/core/patchAsyncStorage.d.ts +1 -0
- package/dist/core/patchAsyncStorage.js +16 -0
- package/dist/core/requestHandler.d.ts +3 -0
- package/dist/core/requestHandler.js +162 -0
- package/dist/core/require-hooks.d.ts +3 -0
- package/dist/core/require-hooks.js +102 -0
- package/dist/core/resolve.d.ts +31 -0
- package/dist/core/resolve.js +81 -0
- package/dist/core/routing/cacheInterceptor.d.ts +2 -0
- package/dist/core/routing/cacheInterceptor.js +162 -0
- package/dist/core/routing/i18n/accept-header.d.ts +1 -0
- package/dist/core/routing/i18n/accept-header.js +101 -0
- package/dist/core/routing/i18n/index.d.ts +2 -0
- package/dist/core/routing/i18n/index.js +36 -0
- package/dist/core/routing/matcher.d.ts +25 -0
- package/dist/core/routing/matcher.js +294 -0
- package/dist/core/routing/middleware.d.ts +7 -0
- package/dist/core/routing/middleware.js +167 -0
- package/dist/core/routing/util.d.ts +105 -0
- package/dist/core/routing/util.js +415 -0
- package/dist/core/routingHandler.d.ts +8 -0
- package/dist/core/routingHandler.js +127 -0
- package/dist/core/util.d.ts +5 -0
- package/dist/core/util.js +84 -0
- package/dist/helpers/withCloudflare.d.ts +65 -0
- package/dist/helpers/withCloudflare.js +58 -0
- package/dist/helpers/withSST.d.ts +54 -0
- package/dist/helpers/withSST.js +28 -0
- package/dist/http/index.d.ts +2 -0
- package/dist/http/index.js +4 -0
- package/dist/http/openNextResponse.d.ts +78 -0
- package/dist/http/openNextResponse.js +281 -0
- package/dist/http/request.d.ts +12 -0
- package/dist/http/request.js +35 -0
- package/dist/http/util.d.ts +4 -0
- package/dist/http/util.js +34 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +37 -0
- package/dist/logger.d.ts +12 -0
- package/dist/logger.js +13 -0
- package/dist/minimize-js.d.ts +1 -0
- package/dist/minimize-js.js +101 -0
- package/dist/overrides/imageLoader/host.d.ts +3 -0
- package/dist/overrides/imageLoader/host.js +29 -0
- package/dist/overrides/imageLoader/s3.d.ts +3 -0
- package/dist/overrides/imageLoader/s3.js +33 -0
- package/dist/plugins/edge.d.ts +15 -0
- package/dist/plugins/edge.js +157 -0
- package/dist/plugins/replacement.d.ts +42 -0
- package/dist/plugins/replacement.js +74 -0
- package/dist/plugins/resolve.d.ts +18 -0
- package/dist/plugins/resolve.js +45 -0
- package/dist/queue/sqs-lite.d.ts +3 -0
- package/dist/queue/sqs-lite.js +52 -0
- package/dist/queue/sqs.d.ts +3 -0
- package/dist/queue/sqs.js +20 -0
- package/dist/queue/types.d.ts +12 -0
- package/dist/queue/types.js +1 -0
- package/dist/types/aws-lambda.d.ts +22 -0
- package/dist/types/aws-lambda.js +1 -0
- package/dist/types/next-types.d.ts +155 -0
- package/dist/types/next-types.js +2 -0
- package/dist/types/open-next.d.ts +303 -0
- package/dist/types/open-next.js +1 -0
- package/dist/utils/error.d.ts +23 -0
- package/dist/utils/error.js +32 -0
- package/dist/utils/fetch.d.ts +2 -0
- package/dist/utils/fetch.js +23 -0
- package/dist/utils/promise.d.ts +18 -0
- package/dist/utils/promise.js +48 -0
- package/dist/utils/stream.d.ts +5 -0
- package/dist/utils/stream.js +30 -0
- package/dist/wrappers/aws-lambda-streaming.d.ts +7 -0
- package/dist/wrappers/aws-lambda-streaming.js +88 -0
- package/dist/wrappers/aws-lambda.d.ts +7 -0
- package/dist/wrappers/aws-lambda.js +39 -0
- package/dist/wrappers/cloudflare.d.ts +11 -0
- package/dist/wrappers/cloudflare.js +20 -0
- package/dist/wrappers/node.d.ts +7 -0
- package/dist/wrappers/node.js +57 -0
- package/package.json +66 -0
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import { parseCookies } from "../http/util";
|
|
2
|
+
import { fromReadableStream } from "../utils/stream";
|
|
3
|
+
import { debug } from "../adapters/logger";
|
|
4
|
+
import { convertRes, convertToQuery, convertToQueryString, createServerResponse, proxyRequest, } from "../core/routing/util";
|
|
5
|
+
const CloudFrontBlacklistedHeaders = [
|
|
6
|
+
// Disallowed headers, see: https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/edge-function-restrictions-all.html#function-restrictions-disallowed-headers
|
|
7
|
+
"connection",
|
|
8
|
+
"expect",
|
|
9
|
+
"keep-alive",
|
|
10
|
+
"proxy-authenticate",
|
|
11
|
+
"proxy-authorization",
|
|
12
|
+
"proxy-connection",
|
|
13
|
+
"trailer",
|
|
14
|
+
"upgrade",
|
|
15
|
+
"x-accel-buffering",
|
|
16
|
+
"x-accel-charset",
|
|
17
|
+
"x-accel-limit-rate",
|
|
18
|
+
"x-accel-redirect",
|
|
19
|
+
/x-amz-cf-(.*)/,
|
|
20
|
+
"x-amzn-auth",
|
|
21
|
+
"x-amzn-cf-billing",
|
|
22
|
+
"x-amzn-cf-id",
|
|
23
|
+
"x-amzn-cf-xff",
|
|
24
|
+
"x-amzn-errortype",
|
|
25
|
+
"x-amzn-fle-profile",
|
|
26
|
+
"x-amzn-header-count",
|
|
27
|
+
"x-amzn-header-order",
|
|
28
|
+
"x-amzn-lambda-integration-tag",
|
|
29
|
+
"x-amzn-requestid",
|
|
30
|
+
/x-edge-(.*)/,
|
|
31
|
+
"x-cache",
|
|
32
|
+
"x-forwarded-proto",
|
|
33
|
+
"x-real-ip",
|
|
34
|
+
];
|
|
35
|
+
// Read-only headers, see: https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/edge-function-restrictions-all.html#function-restrictions-read-only-headers
|
|
36
|
+
// We should only remove these headers when directly responding in lambda@edge, not for the external middleware
|
|
37
|
+
const cloudfrontReadOnlyHeaders = [
|
|
38
|
+
"accept-encoding",
|
|
39
|
+
"content-length",
|
|
40
|
+
"if-modified-since",
|
|
41
|
+
"if-none-match",
|
|
42
|
+
"if-range",
|
|
43
|
+
"if-unmodified-since",
|
|
44
|
+
"transfer-encoding",
|
|
45
|
+
"via",
|
|
46
|
+
];
|
|
47
|
+
function normalizeCloudFrontRequestEventHeaders(rawHeaders) {
|
|
48
|
+
const headers = {};
|
|
49
|
+
for (const [key, values] of Object.entries(rawHeaders)) {
|
|
50
|
+
for (const { value } of values) {
|
|
51
|
+
if (value) {
|
|
52
|
+
headers[key.toLowerCase()] = value;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return headers;
|
|
57
|
+
}
|
|
58
|
+
async function convertFromCloudFrontRequestEvent(event) {
|
|
59
|
+
const { method, uri, querystring, body, headers, clientIp } = event.Records[0].cf.request;
|
|
60
|
+
return {
|
|
61
|
+
type: "core",
|
|
62
|
+
method,
|
|
63
|
+
rawPath: uri,
|
|
64
|
+
url: uri + (querystring ? `?${querystring}` : ""),
|
|
65
|
+
body: Buffer.from(body?.data ?? "", body?.encoding === "base64" ? "base64" : "utf8"),
|
|
66
|
+
headers: normalizeCloudFrontRequestEventHeaders(headers),
|
|
67
|
+
remoteAddress: clientIp,
|
|
68
|
+
query: convertToQuery(querystring),
|
|
69
|
+
cookies: headers.cookie?.reduce((acc, cur) => {
|
|
70
|
+
const { key, value } = cur;
|
|
71
|
+
return { ...acc, [key ?? ""]: value };
|
|
72
|
+
}, {}) ?? {},
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
function convertToCloudfrontHeaders(headers, directResponse) {
|
|
76
|
+
const cloudfrontHeaders = {};
|
|
77
|
+
Object.entries(headers)
|
|
78
|
+
.filter(([key]) => !CloudFrontBlacklistedHeaders.some((header) => typeof header === "string" ? header === key : header.test(key)) &&
|
|
79
|
+
// Only remove read-only headers when directly responding in lambda@edge
|
|
80
|
+
(directResponse ? !cloudfrontReadOnlyHeaders.includes(key) : true))
|
|
81
|
+
.forEach(([key, value]) => {
|
|
82
|
+
if (key === "set-cookie") {
|
|
83
|
+
const cookies = parseCookies(`${value}`);
|
|
84
|
+
if (cookies) {
|
|
85
|
+
cloudfrontHeaders[key] = cookies.map((cookie) => ({
|
|
86
|
+
key,
|
|
87
|
+
value: cookie,
|
|
88
|
+
}));
|
|
89
|
+
}
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
cloudfrontHeaders[key] = [
|
|
93
|
+
...(cloudfrontHeaders[key] || []),
|
|
94
|
+
...(Array.isArray(value)
|
|
95
|
+
? value.map((v) => ({ key, value: v }))
|
|
96
|
+
: [{ key, value: value.toString() }]),
|
|
97
|
+
];
|
|
98
|
+
});
|
|
99
|
+
return cloudfrontHeaders;
|
|
100
|
+
}
|
|
101
|
+
async function convertToCloudFrontRequestResult(result, originalRequest) {
|
|
102
|
+
let responseHeaders = result.type === "middleware"
|
|
103
|
+
? result.internalEvent.headers
|
|
104
|
+
: result.headers;
|
|
105
|
+
if (result.type === "middleware") {
|
|
106
|
+
const { method, clientIp, origin } = originalRequest.Records[0].cf.request;
|
|
107
|
+
// Handle external rewrite
|
|
108
|
+
if (result.isExternalRewrite) {
|
|
109
|
+
const serverResponse = createServerResponse(result.internalEvent, {});
|
|
110
|
+
await proxyRequest(result.internalEvent, serverResponse);
|
|
111
|
+
const externalResult = convertRes(serverResponse);
|
|
112
|
+
const body = await fromReadableStream(externalResult.body, externalResult.isBase64Encoded);
|
|
113
|
+
const cloudfrontResult = {
|
|
114
|
+
status: externalResult.statusCode.toString(),
|
|
115
|
+
statusDescription: "OK",
|
|
116
|
+
headers: convertToCloudfrontHeaders(externalResult.headers, true),
|
|
117
|
+
bodyEncoding: externalResult.isBase64Encoded
|
|
118
|
+
? "base64"
|
|
119
|
+
: "text",
|
|
120
|
+
body,
|
|
121
|
+
};
|
|
122
|
+
debug("externalResult", cloudfrontResult);
|
|
123
|
+
return cloudfrontResult;
|
|
124
|
+
}
|
|
125
|
+
let customOrigin = origin?.custom;
|
|
126
|
+
let host = responseHeaders["host"] ?? responseHeaders["Host"];
|
|
127
|
+
if (result.origin) {
|
|
128
|
+
customOrigin = {
|
|
129
|
+
...customOrigin,
|
|
130
|
+
domainName: result.origin.host,
|
|
131
|
+
port: result.origin.port ?? 443,
|
|
132
|
+
protocol: result.origin.protocol ?? "https",
|
|
133
|
+
customHeaders: {},
|
|
134
|
+
};
|
|
135
|
+
host = result.origin.host;
|
|
136
|
+
}
|
|
137
|
+
const response = {
|
|
138
|
+
clientIp,
|
|
139
|
+
method,
|
|
140
|
+
uri: result.internalEvent.rawPath,
|
|
141
|
+
querystring: convertToQueryString(result.internalEvent.query).replace("?", ""),
|
|
142
|
+
headers: convertToCloudfrontHeaders({
|
|
143
|
+
...responseHeaders,
|
|
144
|
+
host,
|
|
145
|
+
}),
|
|
146
|
+
origin: origin?.custom
|
|
147
|
+
? {
|
|
148
|
+
custom: customOrigin,
|
|
149
|
+
}
|
|
150
|
+
: origin,
|
|
151
|
+
};
|
|
152
|
+
debug("response rewrite", response);
|
|
153
|
+
return response;
|
|
154
|
+
}
|
|
155
|
+
const body = await fromReadableStream(result.body, result.isBase64Encoded);
|
|
156
|
+
const response = {
|
|
157
|
+
status: result.statusCode.toString(),
|
|
158
|
+
statusDescription: "OK",
|
|
159
|
+
headers: convertToCloudfrontHeaders(responseHeaders, true),
|
|
160
|
+
bodyEncoding: result.isBase64Encoded ? "base64" : "text",
|
|
161
|
+
body,
|
|
162
|
+
};
|
|
163
|
+
debug(response);
|
|
164
|
+
return response;
|
|
165
|
+
}
|
|
166
|
+
export default {
|
|
167
|
+
convertFrom: convertFromCloudFrontRequestEvent,
|
|
168
|
+
convertTo: convertToCloudFrontRequestResult,
|
|
169
|
+
name: "aws-cloudfront",
|
|
170
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
const converter = {
|
|
2
|
+
convertFrom(event) {
|
|
3
|
+
return Promise.resolve({
|
|
4
|
+
type: "dummy",
|
|
5
|
+
original: event,
|
|
6
|
+
});
|
|
7
|
+
},
|
|
8
|
+
convertTo(internalResult) {
|
|
9
|
+
return Promise.resolve({
|
|
10
|
+
type: "dummy",
|
|
11
|
+
original: internalResult,
|
|
12
|
+
});
|
|
13
|
+
},
|
|
14
|
+
name: "dummy",
|
|
15
|
+
};
|
|
16
|
+
export default converter;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { Converter, InternalEvent, InternalResult } from "../types/open-next";
|
|
2
|
+
import { MiddlewareOutputEvent } from "../core/routingHandler";
|
|
3
|
+
declare const converter: Converter<InternalEvent, InternalResult | ({
|
|
4
|
+
type: "middleware";
|
|
5
|
+
} & MiddlewareOutputEvent)>;
|
|
6
|
+
export default converter;
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { Buffer } from "node:buffer";
|
|
2
|
+
import { parseCookies } from "../http/util";
|
|
3
|
+
const converter = {
|
|
4
|
+
convertFrom: async (event) => {
|
|
5
|
+
const searchParams = new URL(event.url).searchParams;
|
|
6
|
+
const query = {};
|
|
7
|
+
for (const [key, value] of searchParams.entries()) {
|
|
8
|
+
if (query[key]) {
|
|
9
|
+
if (Array.isArray(query[key])) {
|
|
10
|
+
query[key].push(value);
|
|
11
|
+
}
|
|
12
|
+
else {
|
|
13
|
+
query[key] = [query[key], value];
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
else {
|
|
17
|
+
query[key] = value;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
//Transform body into Buffer
|
|
21
|
+
const body = await event.arrayBuffer();
|
|
22
|
+
const headers = {};
|
|
23
|
+
event.headers.forEach((value, key) => {
|
|
24
|
+
headers[key] = value;
|
|
25
|
+
});
|
|
26
|
+
const rawPath = new URL(event.url).pathname;
|
|
27
|
+
const method = event.method;
|
|
28
|
+
const shouldHaveBody = method !== "GET" && method !== "HEAD";
|
|
29
|
+
return {
|
|
30
|
+
type: "core",
|
|
31
|
+
method,
|
|
32
|
+
rawPath,
|
|
33
|
+
url: event.url,
|
|
34
|
+
body: shouldHaveBody ? Buffer.from(body) : undefined,
|
|
35
|
+
headers: headers,
|
|
36
|
+
remoteAddress: event.headers.get("x-forwarded-for") ?? "::1",
|
|
37
|
+
query,
|
|
38
|
+
cookies: Object.fromEntries(parseCookies(event.headers.get("cookie") ?? "")?.map((cookie) => {
|
|
39
|
+
const [key, value] = cookie.split("=");
|
|
40
|
+
return [key, value];
|
|
41
|
+
}) ?? []),
|
|
42
|
+
};
|
|
43
|
+
},
|
|
44
|
+
convertTo: async (result) => {
|
|
45
|
+
if ("internalEvent" in result) {
|
|
46
|
+
let url = result.internalEvent.url;
|
|
47
|
+
if (!result.isExternalRewrite) {
|
|
48
|
+
if (result.origin) {
|
|
49
|
+
url = `${result.origin.protocol}://${result.origin.host}${result.origin.port ? `:${result.origin.port}` : ""}${url}`;
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
url = `https://${result.internalEvent.headers.host}${url}`;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
const req = new Request(url, {
|
|
56
|
+
body: result.internalEvent.body,
|
|
57
|
+
method: result.internalEvent.method,
|
|
58
|
+
headers: {
|
|
59
|
+
...result.internalEvent.headers,
|
|
60
|
+
"x-forwarded-host": result.internalEvent.headers.host,
|
|
61
|
+
},
|
|
62
|
+
});
|
|
63
|
+
const cfCache = (result.isISR ||
|
|
64
|
+
result.internalEvent.rawPath.startsWith("/_next/image")) &&
|
|
65
|
+
process.env.DISABLE_CACHE !== "true"
|
|
66
|
+
? { cacheEverything: true }
|
|
67
|
+
: {};
|
|
68
|
+
return fetch(req, {
|
|
69
|
+
// This is a hack to make sure that the response is cached by Cloudflare
|
|
70
|
+
// See https://developers.cloudflare.com/workers/examples/cache-using-fetch/#caching-html-resources
|
|
71
|
+
// @ts-expect-error - This is a Cloudflare specific option
|
|
72
|
+
cf: cfCache,
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
else {
|
|
76
|
+
const headers = new Headers();
|
|
77
|
+
for (const [key, value] of Object.entries(result.headers)) {
|
|
78
|
+
headers.set(key, Array.isArray(value) ? value.join(",") : value);
|
|
79
|
+
}
|
|
80
|
+
return new Response(result.body, {
|
|
81
|
+
status: result.statusCode,
|
|
82
|
+
headers: headers,
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
},
|
|
86
|
+
name: "edge",
|
|
87
|
+
};
|
|
88
|
+
export default converter;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { parseCookies } from "../http/util";
|
|
2
|
+
const converter = {
|
|
3
|
+
convertFrom: async (req) => {
|
|
4
|
+
const body = await new Promise((resolve) => {
|
|
5
|
+
const chunks = [];
|
|
6
|
+
req.on("data", (chunk) => {
|
|
7
|
+
chunks.push(chunk);
|
|
8
|
+
});
|
|
9
|
+
req.on("end", () => {
|
|
10
|
+
resolve(Buffer.concat(chunks));
|
|
11
|
+
});
|
|
12
|
+
});
|
|
13
|
+
const url = new URL(req.url, `http://${req.headers.host}`);
|
|
14
|
+
const query = Object.fromEntries(url.searchParams.entries());
|
|
15
|
+
return {
|
|
16
|
+
type: "core",
|
|
17
|
+
method: req.method ?? "GET",
|
|
18
|
+
rawPath: url.pathname,
|
|
19
|
+
url: url.pathname + url.search,
|
|
20
|
+
body,
|
|
21
|
+
headers: Object.fromEntries(Object.entries(req.headers ?? {})
|
|
22
|
+
.map(([key, value]) => [
|
|
23
|
+
key.toLowerCase(),
|
|
24
|
+
Array.isArray(value) ? value.join(",") : value,
|
|
25
|
+
])
|
|
26
|
+
.filter(([key]) => key)),
|
|
27
|
+
remoteAddress: req.headers["x-forwarded-for"] ??
|
|
28
|
+
req.socket.remoteAddress ??
|
|
29
|
+
"::1",
|
|
30
|
+
query,
|
|
31
|
+
cookies: Object.fromEntries(parseCookies(req.headers["cookie"])?.map((cookie) => {
|
|
32
|
+
const [key, value] = cookie.split("=");
|
|
33
|
+
return [key, value];
|
|
34
|
+
}) ?? []),
|
|
35
|
+
};
|
|
36
|
+
},
|
|
37
|
+
// Nothing to do here, it's streaming
|
|
38
|
+
convertTo: async (internalResult) => ({
|
|
39
|
+
body: internalResult.body,
|
|
40
|
+
headers: internalResult.headers,
|
|
41
|
+
statusCode: internalResult.statusCode,
|
|
42
|
+
}),
|
|
43
|
+
name: "node",
|
|
44
|
+
};
|
|
45
|
+
export default converter;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
const converter = {
|
|
2
|
+
convertFrom(event) {
|
|
3
|
+
const records = event.Records.map((record) => {
|
|
4
|
+
const { host, url } = JSON.parse(record.body);
|
|
5
|
+
return { host, url, id: record.messageId };
|
|
6
|
+
});
|
|
7
|
+
return Promise.resolve({
|
|
8
|
+
type: "revalidate",
|
|
9
|
+
records,
|
|
10
|
+
});
|
|
11
|
+
},
|
|
12
|
+
convertTo(revalidateEvent) {
|
|
13
|
+
return Promise.resolve({
|
|
14
|
+
type: "revalidate",
|
|
15
|
+
batchItemFailures: revalidateEvent.records.map((record) => ({
|
|
16
|
+
itemIdentifier: record.id,
|
|
17
|
+
})),
|
|
18
|
+
});
|
|
19
|
+
},
|
|
20
|
+
name: "sqs-revalidate",
|
|
21
|
+
};
|
|
22
|
+
export default converter;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function removeUndefinedFromQuery(query: Record<string, string | string[] | undefined>): Record<string, string | string[]>;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { BaseEventOrResult, InternalEvent, InternalResult, OpenNextConfig, OpenNextHandler } from "../types/open-next";
|
|
2
|
+
declare global {
|
|
3
|
+
var openNextConfig: Partial<OpenNextConfig>;
|
|
4
|
+
}
|
|
5
|
+
type HandlerType = "imageOptimization" | "revalidate" | "warmer" | "middleware" | "initializationFunction";
|
|
6
|
+
type GenericHandler<Type extends HandlerType, E extends BaseEventOrResult = InternalEvent, R extends BaseEventOrResult = InternalResult> = {
|
|
7
|
+
handler: OpenNextHandler<E, R>;
|
|
8
|
+
type: Type;
|
|
9
|
+
};
|
|
10
|
+
export declare function createGenericHandler<Type extends HandlerType, E extends BaseEventOrResult = InternalEvent, R extends BaseEventOrResult = InternalResult>(handler: GenericHandler<Type, E, R>): Promise<(...args: any[]) => any>;
|
|
11
|
+
export {};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { debug } from "../adapters/logger";
|
|
2
|
+
import { resolveConverter, resolveWrapper } from "./resolve";
|
|
3
|
+
export async function createGenericHandler(handler) {
|
|
4
|
+
//First we load the config
|
|
5
|
+
// @ts-expect-error
|
|
6
|
+
const config = await import("./open-next.config.mjs").then((m) => m.default);
|
|
7
|
+
globalThis.openNextConfig = config;
|
|
8
|
+
const override = config[handler.type]
|
|
9
|
+
?.override;
|
|
10
|
+
// From the config, we create the adapter
|
|
11
|
+
const adapter = await resolveConverter(override?.converter);
|
|
12
|
+
// Then we create the handler
|
|
13
|
+
const wrapper = await resolveWrapper(override?.wrapper);
|
|
14
|
+
debug("Using wrapper", wrapper.name);
|
|
15
|
+
return wrapper.wrapper(handler.handler, adapter);
|
|
16
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import type { AsyncLocalStorage } from "node:async_hooks";
|
|
3
|
+
import { DetachedPromiseRunner } from "../utils/promise";
|
|
4
|
+
import type { IncrementalCache } from "../cache/incremental/types";
|
|
5
|
+
import type { Queue } from "../queue/types";
|
|
6
|
+
declare global {
|
|
7
|
+
var queue: Queue;
|
|
8
|
+
var incrementalCache: IncrementalCache;
|
|
9
|
+
var fnName: string | undefined;
|
|
10
|
+
var serverId: string;
|
|
11
|
+
var __als: AsyncLocalStorage<{
|
|
12
|
+
requestId: string;
|
|
13
|
+
pendingPromiseRunner: DetachedPromiseRunner;
|
|
14
|
+
isISRRevalidation?: boolean;
|
|
15
|
+
mergeHeadersPriority?: "middleware" | "handler";
|
|
16
|
+
}>;
|
|
17
|
+
}
|
|
18
|
+
export declare function createMainHandler(): Promise<(...args: any[]) => any>;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { debug } from "../adapters/logger";
|
|
2
|
+
import { generateUniqueId } from "../adapters/util";
|
|
3
|
+
import { openNextHandler } from "./requestHandler.js";
|
|
4
|
+
import { resolveConverter, resolveIncrementalCache, resolveQueue, resolveTagCache, resolveWrapper, } from "./resolve";
|
|
5
|
+
export async function createMainHandler() {
|
|
6
|
+
//First we load the config
|
|
7
|
+
const config = await import(process.cwd() + "/open-next.config.mjs").then((m) => m.default);
|
|
8
|
+
const thisFunction = globalThis.fnName
|
|
9
|
+
? config.functions[globalThis.fnName]
|
|
10
|
+
: config.default;
|
|
11
|
+
globalThis.serverId = generateUniqueId();
|
|
12
|
+
globalThis.openNextConfig = config;
|
|
13
|
+
// Default queue
|
|
14
|
+
globalThis.queue = await resolveQueue(thisFunction.override?.queue);
|
|
15
|
+
globalThis.incrementalCache = await resolveIncrementalCache(thisFunction.override?.incrementalCache);
|
|
16
|
+
globalThis.tagCache = await resolveTagCache(thisFunction.override?.tagCache);
|
|
17
|
+
globalThis.lastModified = {};
|
|
18
|
+
// From the config, we create the adapter
|
|
19
|
+
const adapter = await resolveConverter(thisFunction.override?.converter);
|
|
20
|
+
// Then we create the handler
|
|
21
|
+
const wrapper = await resolveWrapper(thisFunction.override?.wrapper);
|
|
22
|
+
debug("Using wrapper", wrapper.name);
|
|
23
|
+
return wrapper.wrapper(openNextHandler, adapter);
|
|
24
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import type { OutgoingHttpHeaders } from "http";
|
|
3
|
+
interface RequestData {
|
|
4
|
+
geo?: {
|
|
5
|
+
city?: string;
|
|
6
|
+
country?: string;
|
|
7
|
+
region?: string;
|
|
8
|
+
latitude?: string;
|
|
9
|
+
longitude?: string;
|
|
10
|
+
};
|
|
11
|
+
headers: OutgoingHttpHeaders;
|
|
12
|
+
ip?: string;
|
|
13
|
+
method: string;
|
|
14
|
+
nextConfig?: {
|
|
15
|
+
basePath?: string;
|
|
16
|
+
i18n?: any;
|
|
17
|
+
trailingSlash?: boolean;
|
|
18
|
+
};
|
|
19
|
+
page?: {
|
|
20
|
+
name?: string;
|
|
21
|
+
params?: {
|
|
22
|
+
[key: string]: string | string[];
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
url: string;
|
|
26
|
+
body?: ReadableStream<Uint8Array>;
|
|
27
|
+
signal: AbortSignal;
|
|
28
|
+
}
|
|
29
|
+
interface Entries {
|
|
30
|
+
[k: string]: {
|
|
31
|
+
default: (props: {
|
|
32
|
+
page: string;
|
|
33
|
+
request: RequestData;
|
|
34
|
+
}) => Promise<{
|
|
35
|
+
response: Response;
|
|
36
|
+
waitUntil: Promise<void>;
|
|
37
|
+
}>;
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
declare global {
|
|
41
|
+
var _ENTRIES: Entries;
|
|
42
|
+
var _ROUTES: EdgeRoute[];
|
|
43
|
+
var __storage__: Map<unknown, unknown>;
|
|
44
|
+
var AsyncContext: any;
|
|
45
|
+
var AsyncLocalStorage: any;
|
|
46
|
+
}
|
|
47
|
+
export interface EdgeRoute {
|
|
48
|
+
name: string;
|
|
49
|
+
page: string;
|
|
50
|
+
regex: string[];
|
|
51
|
+
}
|
|
52
|
+
type EdgeRequest = Omit<RequestData, "page">;
|
|
53
|
+
export default function edgeFunctionHandler(request: EdgeRequest): Promise<Response>;
|
|
54
|
+
export {};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// Necessary files will be imported here with banner in esbuild
|
|
2
|
+
export default async function edgeFunctionHandler(request) {
|
|
3
|
+
const path = new URL(request.url).pathname;
|
|
4
|
+
const routes = globalThis._ROUTES;
|
|
5
|
+
const correspondingRoute = routes.find((route) => route.regex.some((r) => new RegExp(r).test(path)));
|
|
6
|
+
if (!correspondingRoute) {
|
|
7
|
+
throw new Error(`No route found for ${request.url}`);
|
|
8
|
+
}
|
|
9
|
+
const result = await self._ENTRIES[`middleware_${correspondingRoute.name}`].default({
|
|
10
|
+
page: correspondingRoute.page,
|
|
11
|
+
request: {
|
|
12
|
+
...request,
|
|
13
|
+
page: {
|
|
14
|
+
name: correspondingRoute.name,
|
|
15
|
+
},
|
|
16
|
+
},
|
|
17
|
+
});
|
|
18
|
+
await result.waitUntil;
|
|
19
|
+
const response = result.response;
|
|
20
|
+
return response;
|
|
21
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function patchAsyncStorage(): void;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
const mod = require("module");
|
|
2
|
+
const resolveFilename = mod._resolveFilename;
|
|
3
|
+
export function patchAsyncStorage() {
|
|
4
|
+
mod._resolveFilename = function (originalResolveFilename, request, parent, isMain, options) {
|
|
5
|
+
if (request.endsWith("static-generation-async-storage.external") ||
|
|
6
|
+
request.endsWith("static-generation-async-storage.external.js")) {
|
|
7
|
+
return require.resolve("./patchedAsyncStorage.cjs");
|
|
8
|
+
}
|
|
9
|
+
else if (request.endsWith("static-generation-async-storage.external.original")) {
|
|
10
|
+
return originalResolveFilename.call(mod, request.replace(".original", ".js"), parent, isMain, options);
|
|
11
|
+
}
|
|
12
|
+
else
|
|
13
|
+
return originalResolveFilename.call(mod, request, parent, isMain, options);
|
|
14
|
+
// We use `bind` here to avoid referencing outside variables to create potential memory leaks.
|
|
15
|
+
}.bind(null, resolveFilename);
|
|
16
|
+
}
|