@edgeone/opennextjs-pages 0.0.1

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.
Files changed (35) hide show
  1. package/dist/build/advanced-api-routes.js +147 -0
  2. package/dist/build/cache.js +36 -0
  3. package/dist/build/content/next-shims/telemetry-storage.cjs +55 -0
  4. package/dist/build/content/prerendered.js +292 -0
  5. package/dist/build/content/server.js +236 -0
  6. package/dist/build/content/static.js +119 -0
  7. package/dist/build/functions/server.js +133 -0
  8. package/dist/build/plugin-context.js +367 -0
  9. package/dist/build/routes.js +127 -0
  10. package/dist/build/templates/handler-monorepo.tmpl.js +210 -0
  11. package/dist/build/templates/handler.tmpl.js +206 -0
  12. package/dist/esm-chunks/chunk-5J3FID2N.js +5551 -0
  13. package/dist/esm-chunks/chunk-6BT4RYQJ.js +43 -0
  14. package/dist/esm-chunks/chunk-FKDTZJRV.js +832 -0
  15. package/dist/esm-chunks/chunk-TLQCAGE2.js +1921 -0
  16. package/dist/index.js +50 -0
  17. package/dist/run/config.js +37 -0
  18. package/dist/run/constants.js +19 -0
  19. package/dist/run/handlers/cache.cjs +369 -0
  20. package/dist/run/handlers/request-context.cjs +148 -0
  21. package/dist/run/handlers/server.js +3213 -0
  22. package/dist/run/handlers/tags-handler.cjs +92 -0
  23. package/dist/run/handlers/tracer.cjs +916 -0
  24. package/dist/run/handlers/use-cache-handler.js +1538 -0
  25. package/dist/run/handlers/wait-until.cjs +39 -0
  26. package/dist/run/headers.js +81 -0
  27. package/dist/run/next.cjs +100 -0
  28. package/dist/run/revalidate.js +34 -0
  29. package/dist/run/storage/regional-blob-store.cjs +64 -0
  30. package/dist/run/storage/request-scoped-in-memory-cache.cjs +1496 -0
  31. package/dist/run/storage/storage.cjs +37 -0
  32. package/dist/shared/blob-types.cjs +37 -0
  33. package/dist/shared/blobkey.js +25 -0
  34. package/dist/shared/cache-types.cjs +33 -0
  35. package/package.json +55 -0
@@ -0,0 +1,236 @@
1
+
2
+ var require = await (async () => {
3
+ var { createRequire } = await import("node:module");
4
+ return createRequire(import.meta.url);
5
+ })();
6
+
7
+ import {
8
+ require_out
9
+ } from "../../esm-chunks/chunk-5J3FID2N.js";
10
+ import {
11
+ trace,
12
+ wrapTracer
13
+ } from "../../esm-chunks/chunk-FKDTZJRV.js";
14
+ import {
15
+ require_semver
16
+ } from "../../esm-chunks/chunk-TLQCAGE2.js";
17
+ import {
18
+ __toESM
19
+ } from "../../esm-chunks/chunk-6BT4RYQJ.js";
20
+
21
+ // src/build/content/server.ts
22
+ import { existsSync } from "node:fs";
23
+ import {
24
+ access,
25
+ cp,
26
+ mkdir,
27
+ readdir,
28
+ readFile,
29
+ readlink,
30
+ symlink,
31
+ writeFile
32
+ } from "node:fs/promises";
33
+ import { createRequire } from "node:module";
34
+ import { dirname, join, resolve, sep } from "node:path";
35
+ import { join as posixJoin, sep as posixSep } from "node:path/posix";
36
+ var import_fast_glob = __toESM(require_out(), 1);
37
+ var import_semver = __toESM(require_semver(), 1);
38
+ var tracer = wrapTracer(trace.getTracer("Next runtime"));
39
+ var RUN_CONFIG_FILE = "run-config.json";
40
+ var toPosixPath = (path) => path.split(sep).join(posixSep);
41
+ function isError(error) {
42
+ return error instanceof Error;
43
+ }
44
+ var copyNextServerCode = async (ctx) => {
45
+ const reqServerFilesPath = join(
46
+ ctx.standaloneRootDir,
47
+ ctx.relativeAppDir,
48
+ ctx.requiredServerFiles.config.distDir,
49
+ "required-server-files.json"
50
+ );
51
+ try {
52
+ await access(reqServerFilesPath);
53
+ } catch (error) {
54
+ if (isError(error) && error.code === "ENOENT") {
55
+ ctx.failBuild(
56
+ `Failed creating server handler. required-server-files.json file not found at expected location "${reqServerFilesPath}". Your repository setup is currently not yet supported.`
57
+ );
58
+ } else {
59
+ throw error;
60
+ }
61
+ }
62
+ const reqServerFiles = JSON.parse(
63
+ await readFile(reqServerFilesPath, "utf-8")
64
+ );
65
+ if (toPosixPath(ctx.distDir).replace(new RegExp(`^${ctx.relativeAppDir}/?`), "") !== reqServerFiles.config.distDir) {
66
+ reqServerFiles.config.distDir = ctx.nextDistDir;
67
+ await writeFile(reqServerFilesPath, JSON.stringify(reqServerFiles));
68
+ }
69
+ await mkdir(ctx.serverHandlerDir, { recursive: true });
70
+ await writeFile(
71
+ join(ctx.serverHandlerDir, RUN_CONFIG_FILE),
72
+ JSON.stringify({
73
+ nextConfig: reqServerFiles.config,
74
+ // only enable setting up 'use cache' handler when Next.js supports CacheHandlerV2 as we don't have V1 compatible implementation
75
+ // see https://github.com/vercel/next.js/pull/76687 first released in v15.3.0-canary.13
76
+ enableUseCacheHandler: ctx.nextVersion ? (0, import_semver.satisfies)(ctx.nextVersion, ">=15.3.0-canary.13", {
77
+ includePrerelease: true
78
+ }) : false
79
+ }),
80
+ "utf-8"
81
+ );
82
+ const srcDir = join(ctx.standaloneDir, ctx.nextDistDir);
83
+ const nextFolder = toPosixPath(ctx.distDir) === toPosixPath(ctx.buildConfig.distDir) ? ctx.distDir : ctx.nextDistDir;
84
+ const destDir = join(ctx.serverHandlerDir, nextFolder);
85
+ const paths = await (0, import_fast_glob.default)(
86
+ [`*`, `server/*`, `server/**/*`, `server/edge-chunks/*`, `server/+(app|pages)/**/*.js`],
87
+ {
88
+ cwd: srcDir,
89
+ extglob: true
90
+ }
91
+ );
92
+ await Promise.all(
93
+ paths.map(async (path) => {
94
+ const srcPath = join(srcDir, path);
95
+ const destPath = join(destDir, path);
96
+ if (path === "server/middleware-manifest.json") {
97
+ try {
98
+ await replaceMiddlewareManifest(srcPath, destPath);
99
+ } catch (error) {
100
+ throw new Error("Could not patch middleware manifest file", { cause: error });
101
+ }
102
+ return;
103
+ }
104
+ await cp(srcPath, destPath, { recursive: true, force: true });
105
+ })
106
+ );
107
+ };
108
+ async function recreateNodeModuleSymlinks(src, dest, org) {
109
+ const dirents = await readdir(join(src, org || ""), { withFileTypes: true });
110
+ await Promise.all(
111
+ dirents.map(async (dirent) => {
112
+ if (dirent.name.startsWith("@")) {
113
+ return recreateNodeModuleSymlinks(src, dest, dirent.name);
114
+ }
115
+ if (dirent.isSymbolicLink()) {
116
+ const symlinkSrc = join(dest, org || "", dirent.name);
117
+ const symlinkTarget = await readlink(join(src, org || "", dirent.name));
118
+ const symlinkDest = join(dest, org || "", symlinkTarget);
119
+ if (existsSync(symlinkDest) && !existsSync(symlinkSrc)) {
120
+ if (org) {
121
+ await mkdir(join(dest, org), { recursive: true });
122
+ }
123
+ await symlink(symlinkTarget, symlinkSrc);
124
+ }
125
+ }
126
+ })
127
+ );
128
+ }
129
+ var nextInternalModuleReplacements = [
130
+ {
131
+ // standalone is loading expensive Telemetry module that is not actually used
132
+ // so this replace that module with lightweight no-op shim that doesn't load additional modules
133
+ // see https://github.com/vercel/next.js/pull/63574 that removed need for this shim
134
+ ongoing: false,
135
+ minVersion: "13.5.0-canary.0",
136
+ // perf released in https://github.com/vercel/next.js/releases/tag/v14.2.0-canary.43
137
+ maxVersion: "14.2.0-canary.42",
138
+ nextModule: "next/dist/telemetry/storage.js",
139
+ shimModule: "./next-shims/telemetry-storage.cjs"
140
+ }
141
+ ];
142
+ function getPatchesToApply(nextVersion, patches = nextInternalModuleReplacements) {
143
+ return patches.filter((patch) => {
144
+ if ((0, import_semver.lt)(nextVersion, patch.minVersion)) {
145
+ return false;
146
+ }
147
+ if (patch.ongoing) {
148
+ if ((0, import_semver.prerelease)(nextVersion)) {
149
+ return true;
150
+ }
151
+ return (0, import_semver.lte)(nextVersion, patch.maxStableVersion);
152
+ }
153
+ return (0, import_semver.lte)(nextVersion, patch.maxVersion);
154
+ });
155
+ }
156
+ async function patchNextModules(ctx, nextVersion, serverHandlerRequireResolve) {
157
+ const moduleReplacementsToApply = getPatchesToApply(nextVersion);
158
+ if (moduleReplacementsToApply.length !== 0) {
159
+ await Promise.all(
160
+ moduleReplacementsToApply.map(async ({ nextModule, shimModule }) => {
161
+ try {
162
+ const nextModulePath = serverHandlerRequireResolve(nextModule);
163
+ const shimModulePath = posixJoin(ctx.pluginDir, "dist", "build", "content", shimModule);
164
+ await cp(shimModulePath, nextModulePath, { force: true });
165
+ } catch {
166
+ }
167
+ })
168
+ );
169
+ }
170
+ }
171
+ var copyNextDependencies = async (ctx) => {
172
+ const entries = await readdir(ctx.standaloneDir);
173
+ const promises = entries.map(async (entry) => {
174
+ if (entry === ctx.nextDistDir) {
175
+ return;
176
+ }
177
+ const src = join(ctx.standaloneDir, entry);
178
+ const dest = join(ctx.serverHandlerDir, entry);
179
+ await cp(src, dest, { recursive: true, verbatimSymlinks: true, force: true });
180
+ if (entry === "node_modules") {
181
+ await recreateNodeModuleSymlinks(ctx.resolveFromSiteDir("node_modules"), dest);
182
+ }
183
+ });
184
+ const rootSrcDir = join(ctx.standaloneRootDir, "node_modules");
185
+ const rootDestDir = join(ctx.serverHandlerRootDir, "node_modules");
186
+ if (existsSync(rootSrcDir) && ctx.standaloneRootDir !== ctx.standaloneDir) {
187
+ promises.push(
188
+ cp(rootSrcDir, rootDestDir, { recursive: true, verbatimSymlinks: true }).then(
189
+ () => recreateNodeModuleSymlinks(resolve("node_modules"), rootDestDir)
190
+ )
191
+ );
192
+ }
193
+ await Promise.all(promises);
194
+ const serverHandlerRequire = createRequire(posixJoin(ctx.serverHandlerDir, ":internal:"));
195
+ if (ctx.nextVersion) {
196
+ await patchNextModules(ctx, ctx.nextVersion, serverHandlerRequire.resolve);
197
+ }
198
+ try {
199
+ const nextEntryAbsolutePath = serverHandlerRequire.resolve("next");
200
+ const nextRequire = createRequire(nextEntryAbsolutePath);
201
+ nextRequire.resolve("styled-jsx");
202
+ } catch {
203
+ throw new Error(
204
+ "node_modules are not installed correctly, if you are using pnpm please set the public hoist pattern to: `public-hoist-pattern[]=*`.\n"
205
+ );
206
+ }
207
+ };
208
+ var replaceMiddlewareManifest = async (sourcePath, destPath) => {
209
+ await mkdir(dirname(destPath), { recursive: true });
210
+ const data = await readFile(sourcePath, "utf8");
211
+ const manifest = JSON.parse(data);
212
+ const newManifest = {
213
+ ...manifest,
214
+ middleware: {}
215
+ };
216
+ const newData = JSON.stringify(newManifest);
217
+ await writeFile(destPath, newData);
218
+ };
219
+ var verifyHandlerDirStructure = async (ctx) => {
220
+ const { nextConfig } = JSON.parse(
221
+ await readFile(join(ctx.serverHandlerDir, RUN_CONFIG_FILE), "utf-8")
222
+ );
223
+ const expectedBuildIDPath = join(ctx.serverHandlerDir, nextConfig.distDir, "BUILD_ID");
224
+ if (!existsSync(expectedBuildIDPath)) {
225
+ ctx.failBuild(
226
+ `Failed creating server handler. BUILD_ID file not found at expected location "${expectedBuildIDPath}".`
227
+ );
228
+ }
229
+ };
230
+ export {
231
+ RUN_CONFIG_FILE,
232
+ copyNextDependencies,
233
+ copyNextServerCode,
234
+ getPatchesToApply,
235
+ verifyHandlerDirStructure
236
+ };
@@ -0,0 +1,119 @@
1
+
2
+ var require = await (async () => {
3
+ var { createRequire } = await import("node:module");
4
+ return createRequire(import.meta.url);
5
+ })();
6
+
7
+ import {
8
+ require_out
9
+ } from "../../esm-chunks/chunk-5J3FID2N.js";
10
+ import {
11
+ trace,
12
+ wrapTracer
13
+ } from "../../esm-chunks/chunk-FKDTZJRV.js";
14
+ import {
15
+ __toESM
16
+ } from "../../esm-chunks/chunk-6BT4RYQJ.js";
17
+
18
+ // src/build/content/static.ts
19
+ import { existsSync } from "node:fs";
20
+ import { cp, mkdir, readFile, rename, rm, writeFile } from "node:fs/promises";
21
+ import { basename, join, dirname } from "node:path";
22
+ var import_fast_glob = __toESM(require_out(), 1);
23
+ var tracer = wrapTracer(trace.getTracer("Next runtime"));
24
+ var copyStaticContent = async (ctx) => {
25
+ return tracer.withActiveSpan("copyStaticContent", async () => {
26
+ const srcDir = join(ctx.publishDir, "server/pages");
27
+ const destDir = ctx.staticDir;
28
+ const paths = await (0, import_fast_glob.default)("**/*.+(html|json)", {
29
+ cwd: srcDir,
30
+ extglob: true
31
+ });
32
+ try {
33
+ await mkdir(destDir, { recursive: true });
34
+ await Promise.all(
35
+ paths.filter((path) => !paths.includes(`${path.slice(0, -5)}.json`)).map(async (path) => {
36
+ const html = await readFile(join(srcDir, path));
37
+ await mkdir(dirname(join(destDir, path)), { recursive: true });
38
+ await writeFile(join(destDir, path), html);
39
+ })
40
+ );
41
+ } catch (error) {
42
+ ctx.failBuild("Failed assembling static pages for upload", error);
43
+ }
44
+ });
45
+ };
46
+ var copyStaticAssets = async (ctx) => {
47
+ return tracer.withActiveSpan(
48
+ "copyStaticAssets",
49
+ async (span) => {
50
+ try {
51
+ const { basePath } = await ctx.getRoutesManifest();
52
+ if (existsSync(ctx.resolveFromSiteDir("public"))) {
53
+ await cp(
54
+ ctx.resolveFromSiteDir("public"),
55
+ join(ctx.staticDir, basePath),
56
+ {
57
+ recursive: true
58
+ }
59
+ );
60
+ }
61
+ if (existsSync(join(ctx.publishDir, "static"))) {
62
+ await cp(
63
+ join(ctx.publishDir, "static"),
64
+ join(ctx.staticDir, basePath, "_next/static"),
65
+ {
66
+ recursive: true
67
+ }
68
+ );
69
+ }
70
+ } catch (error) {
71
+ span.end();
72
+ ctx.failBuild("Failed copying static assets", error);
73
+ }
74
+ }
75
+ );
76
+ };
77
+ var copyStaticExport = async (ctx) => {
78
+ await tracer.withActiveSpan("copyStaticExport", async () => {
79
+ if (!ctx.exportDetail?.outDirectory) {
80
+ ctx.failBuild("Export directory not found");
81
+ }
82
+ try {
83
+ await cp(ctx.exportDetail.outDirectory, ctx.staticDir, {
84
+ recursive: true
85
+ });
86
+ } catch (error) {
87
+ ctx.failBuild("Failed copying static export", error);
88
+ }
89
+ });
90
+ };
91
+ var publishStaticDir = async (ctx) => {
92
+ try {
93
+ await rm(ctx.tempPublishDir, { recursive: true, force: true });
94
+ await mkdir(basename(ctx.tempPublishDir), { recursive: true });
95
+ await rename(ctx.publishDir, ctx.tempPublishDir);
96
+ await rename(ctx.staticDir, ctx.publishDir);
97
+ } catch (error) {
98
+ ctx.failBuild(
99
+ "Failed publishing static content",
100
+ error instanceof Error ? { error } : {}
101
+ );
102
+ }
103
+ };
104
+ var unpublishStaticDir = async (ctx) => {
105
+ try {
106
+ if (existsSync(ctx.tempPublishDir)) {
107
+ await rename(ctx.publishDir, ctx.staticDir);
108
+ await rename(ctx.tempPublishDir, ctx.publishDir);
109
+ }
110
+ } catch {
111
+ }
112
+ };
113
+ export {
114
+ copyStaticAssets,
115
+ copyStaticContent,
116
+ copyStaticExport,
117
+ publishStaticDir,
118
+ unpublishStaticDir
119
+ };
@@ -0,0 +1,133 @@
1
+
2
+ var require = await (async () => {
3
+ var { createRequire } = await import("node:module");
4
+ return createRequire(import.meta.url);
5
+ })();
6
+
7
+ import {
8
+ require_out
9
+ } from "../../esm-chunks/chunk-5J3FID2N.js";
10
+ import {
11
+ trace,
12
+ wrapTracer
13
+ } from "../../esm-chunks/chunk-FKDTZJRV.js";
14
+ import {
15
+ __toESM
16
+ } from "../../esm-chunks/chunk-6BT4RYQJ.js";
17
+
18
+ // src/build/functions/server.ts
19
+ import { cp, mkdir, readFile, rm, writeFile } from "node:fs/promises";
20
+ import { join, relative } from "node:path";
21
+ import { join as posixJoin } from "node:path/posix";
22
+ var import_fast_glob = __toESM(require_out(), 1);
23
+ import {
24
+ copyNextDependencies,
25
+ copyNextServerCode,
26
+ verifyHandlerDirStructure
27
+ } from "../content/server.js";
28
+ var tracer = wrapTracer(trace.getTracer("Next runtime"));
29
+ var copyHandlerDependencies = async (ctx) => {
30
+ const promises = [];
31
+ const { included_files: includedFiles = [] } = {};
32
+ includedFiles.push(
33
+ posixJoin(ctx.relativeAppDir, ".env"),
34
+ posixJoin(ctx.relativeAppDir, ".env.production"),
35
+ posixJoin(ctx.relativeAppDir, ".env.local"),
36
+ posixJoin(ctx.relativeAppDir, ".env.production.local")
37
+ );
38
+ const resolvedFiles = await Promise.all(
39
+ includedFiles.map((globPattern) => (0, import_fast_glob.glob)(globPattern, { cwd: process.cwd() }))
40
+ );
41
+ for (const filePath of resolvedFiles.flat()) {
42
+ promises.push(
43
+ cp(
44
+ join(process.cwd(), filePath),
45
+ // the serverHandlerDir is aware of the dist dir.
46
+ // The distDir must not be the package path therefore we need to rely on the
47
+ // serverHandlerDir instead of the serverHandlerRootDir
48
+ // therefore we need to remove the package path from the filePath
49
+ join(ctx.serverHandlerDir, relative(ctx.relativeAppDir, filePath)),
50
+ {
51
+ recursive: true,
52
+ force: true
53
+ }
54
+ )
55
+ );
56
+ }
57
+ promises.push(
58
+ writeFile(
59
+ join(ctx.serverHandlerRuntimeModulesDir, "package.json"),
60
+ JSON.stringify({ type: "module" })
61
+ )
62
+ );
63
+ const fileList = await (0, import_fast_glob.glob)("dist/**/*", { cwd: ctx.pluginDir });
64
+ for (const filePath of fileList) {
65
+ promises.push(
66
+ cp(join(ctx.pluginDir, filePath), join(ctx.serverHandlerRuntimeModulesDir, filePath), {
67
+ recursive: true,
68
+ force: true
69
+ })
70
+ );
71
+ }
72
+ await Promise.all(promises);
73
+ };
74
+ var writeHandlerManifest = async (ctx) => {
75
+ await writeFile(
76
+ join(ctx.serverHandlerRootDir, `app.json`),
77
+ JSON.stringify({
78
+ config: {
79
+ name: "Next.js Server Handler",
80
+ generator: `${ctx.pluginName}@${ctx.pluginVersion}`,
81
+ nodeBundler: "none",
82
+ // the folders can vary in monorepos based on the folder structure of the user so we have to glob all
83
+ includedFiles: ["**"],
84
+ includedFilesBasePath: ctx.serverHandlerRootDir
85
+ },
86
+ version: 1
87
+ }),
88
+ "utf-8"
89
+ );
90
+ };
91
+ var applyTemplateVariables = (template, variables) => {
92
+ return Object.entries(variables).reduce((acc, [key, value]) => {
93
+ return acc.replaceAll(key, value);
94
+ }, template);
95
+ };
96
+ var getHandlerFile = async (ctx) => {
97
+ const templatesDir = join(ctx.pluginDir, "dist/build/templates");
98
+ const templateVariables = {
99
+ "{{useRegionalBlobs}}": ctx.useRegionalBlobs.toString()
100
+ };
101
+ if (ctx.relativeAppDir.length !== 0) {
102
+ const template = await readFile(join(templatesDir, "handler-monorepo.tmpl.js"), "utf-8");
103
+ console.log("ctx.lambdaWorkingDirectory", ctx.lambdaWorkingDirectory);
104
+ console.log("ctx.nextServerHandler", ctx.nextServerHandler);
105
+ templateVariables["{{cwd}}"] = posixJoin(ctx.lambdaWorkingDirectory);
106
+ templateVariables["{{nextServerHandler}}"] = posixJoin(ctx.nextServerHandler);
107
+ return applyTemplateVariables(template, templateVariables);
108
+ }
109
+ return applyTemplateVariables(
110
+ await readFile(join(templatesDir, "handler.tmpl.js"), "utf-8"),
111
+ templateVariables
112
+ );
113
+ };
114
+ var writeHandlerFile = async (ctx) => {
115
+ const handler = await getHandlerFile(ctx);
116
+ await writeFile(join(ctx.serverHandlerRootDir, `index.mjs`), handler);
117
+ };
118
+ var clearStaleServerHandlers = async (ctx) => {
119
+ await rm(ctx.serverFunctionsDir, { recursive: true, force: true });
120
+ };
121
+ var createServerHandler = async (ctx) => {
122
+ await mkdir(join(ctx.serverHandlerRuntimeModulesDir), { recursive: true });
123
+ await copyNextServerCode(ctx);
124
+ await copyNextDependencies(ctx);
125
+ await copyHandlerDependencies(ctx);
126
+ await writeHandlerManifest(ctx);
127
+ await writeHandlerFile(ctx);
128
+ await verifyHandlerDirStructure(ctx);
129
+ };
130
+ export {
131
+ clearStaleServerHandlers,
132
+ createServerHandler
133
+ };