|
| 1 | +import { patchCode } from "@opennextjs/aws/build/patch/astCodePatcher.js"; |
| 2 | +import { describe, expect, test } from "vitest"; |
| 3 | + |
| 4 | +import { buildIdRule, createMiddlewareManifestRule } from "./next-server.js"; |
| 5 | + |
| 6 | +describe("Next Server", () => { |
| 7 | + test("build ID", () => { |
| 8 | + const code = ` |
| 9 | +class NextNodeServer extends _baseserver.default { |
| 10 | + constructor(options){ |
| 11 | + // Initialize super class |
| 12 | + super(options); |
| 13 | + this.handleNextImageRequest = async (req, res, parsedUrl) => { /* ... */ }; |
| 14 | + } |
| 15 | + async handleUpgrade() { |
| 16 | + // The web server does not support web sockets, it's only used for HMR in |
| 17 | + // development. |
| 18 | + } |
| 19 | + loadEnvConfig({ dev, forceReload, silent }) { |
| 20 | + (0, _env.loadEnvConfig)(this.dir, dev, silent ? { |
| 21 | + info: ()=>{}, |
| 22 | + error: ()=>{} |
| 23 | + } : _log, forceReload); |
| 24 | + } |
| 25 | + async hasPage(pathname) { |
| 26 | + var _this_nextConfig_i18n; |
| 27 | + return !!(0, _require.getMaybePagePath)(pathname, this.distDir, (_this_nextConfig_i18n = this.nextConfig.i18n) == null ? void 0 : _this_nextConfig_i18n.locales, this.enabledDirectories.app); |
| 28 | + } |
| 29 | + getBuildId() { |
| 30 | + const buildIdFile = (0, _path.join)(this.distDir, _constants.BUILD_ID_FILE); |
| 31 | + try { |
| 32 | + return _fs.default.readFileSync(buildIdFile, "utf8").trim(); |
| 33 | + } catch (err) { |
| 34 | + if (err.code === "ENOENT") { |
| 35 | + throw new Error(\`Could not find a production build in the '\${this.distDir}' directory. Try building your app with 'next build' before starting the production server. https://nextjs.org/docs/messages/production-start-no-build-id\`); |
| 36 | + } |
| 37 | + throw err; |
| 38 | + } |
| 39 | + } |
| 40 | + getEnabledDirectories(dev) { |
| 41 | + const dir = dev ? this.dir : this.serverDistDir; |
| 42 | + return { |
| 43 | + app: (0, _findpagesdir.findDir)(dir, "app") ? true : false, |
| 44 | + pages: (0, _findpagesdir.findDir)(dir, "pages") ? true : false |
| 45 | + }; |
| 46 | + } |
| 47 | + // ... |
| 48 | +}`; |
| 49 | + |
| 50 | + expect(patchCode(code, buildIdRule)).toMatchInlineSnapshot(` |
| 51 | + "class NextNodeServer extends _baseserver.default { |
| 52 | + constructor(options){ |
| 53 | + // Initialize super class |
| 54 | + super(options); |
| 55 | + this.handleNextImageRequest = async (req, res, parsedUrl) => { /* ... */ }; |
| 56 | + } |
| 57 | + async handleUpgrade() { |
| 58 | + // The web server does not support web sockets, it's only used for HMR in |
| 59 | + // development. |
| 60 | + } |
| 61 | + loadEnvConfig({ dev, forceReload, silent }) { |
| 62 | + (0, _env.loadEnvConfig)(this.dir, dev, silent ? { |
| 63 | + info: ()=>{}, |
| 64 | + error: ()=>{} |
| 65 | + } : _log, forceReload); |
| 66 | + } |
| 67 | + async hasPage(pathname) { |
| 68 | + var _this_nextConfig_i18n; |
| 69 | + return !!(0, _require.getMaybePagePath)(pathname, this.distDir, (_this_nextConfig_i18n = this.nextConfig.i18n) == null ? void 0 : _this_nextConfig_i18n.locales, this.enabledDirectories.app); |
| 70 | + } |
| 71 | + getBuildId() { |
| 72 | + return process.env.NEXT_BUILD_ID; |
| 73 | + } |
| 74 | + getEnabledDirectories(dev) { |
| 75 | + const dir = dev ? this.dir : this.serverDistDir; |
| 76 | + return { |
| 77 | + app: (0, _findpagesdir.findDir)(dir, "app") ? true : false, |
| 78 | + pages: (0, _findpagesdir.findDir)(dir, "pages") ? true : false |
| 79 | + }; |
| 80 | + } |
| 81 | + // ... |
| 82 | + }" |
| 83 | + `); |
| 84 | + }); |
| 85 | + |
| 86 | + test("middleware manifest", () => { |
| 87 | + const code = ` |
| 88 | +class NextNodeServer extends _baseserver.default { |
| 89 | + constructor(options){ |
| 90 | + // Initialize super class |
| 91 | + super(options); |
| 92 | + this.handleNextImageRequest = async (req, res, parsedUrl) => { /* ... */ }; |
| 93 | + } |
| 94 | + async handleUpgrade() { |
| 95 | + // The web server does not support web sockets, it's only used for HMR in |
| 96 | + // development. |
| 97 | + } |
| 98 | + loadEnvConfig({ dev, forceReload, silent }) { |
| 99 | + (0, _env.loadEnvConfig)(this.dir, dev, silent ? { |
| 100 | + info: ()=>{}, |
| 101 | + error: ()=>{} |
| 102 | + } : _log, forceReload); |
| 103 | + } |
| 104 | + async hasPage(pathname) { |
| 105 | + var _this_nextConfig_i18n; |
| 106 | + return !!(0, _require.getMaybePagePath)(pathname, this.distDir, (_this_nextConfig_i18n = this.nextConfig.i18n) == null ? void 0 : _this_nextConfig_i18n.locales, this.enabledDirectories.app); |
| 107 | + } |
| 108 | + getBuildId() { |
| 109 | + const buildIdFile = (0, _path.join)(this.distDir, _constants.BUILD_ID_FILE); |
| 110 | + try { |
| 111 | + return _fs.default.readFileSync(buildIdFile, "utf8").trim(); |
| 112 | + } catch (err) { |
| 113 | + if (err.code === "ENOENT") { |
| 114 | + throw new Error(\`Could not find a production build in the '\${this.distDir}' directory. Try building your app with 'next build' before starting the production server. https://nextjs.org/docs/messages/production-start-no-build-id\`); |
| 115 | + } |
| 116 | + throw err; |
| 117 | + } |
| 118 | + } |
| 119 | + getMiddlewareManifest() { |
| 120 | + if (this.minimalMode) return null; |
| 121 | + const manifest = require(this.middlewareManifestPath); |
| 122 | + return manifest; |
| 123 | + } |
| 124 | + getEnabledDirectories(dev) { |
| 125 | + const dir = dev ? this.dir : this.serverDistDir; |
| 126 | + return { |
| 127 | + app: (0, _findpagesdir.findDir)(dir, "app") ? true : false, |
| 128 | + pages: (0, _findpagesdir.findDir)(dir, "pages") ? true : false |
| 129 | + }; |
| 130 | + } |
| 131 | + // ... |
| 132 | +}`; |
| 133 | + |
| 134 | + expect(patchCode(code, createMiddlewareManifestRule("manifest"))).toMatchInlineSnapshot(` |
| 135 | + "class NextNodeServer extends _baseserver.default { |
| 136 | + constructor(options){ |
| 137 | + // Initialize super class |
| 138 | + super(options); |
| 139 | + this.handleNextImageRequest = async (req, res, parsedUrl) => { /* ... */ }; |
| 140 | + } |
| 141 | + async handleUpgrade() { |
| 142 | + // The web server does not support web sockets, it's only used for HMR in |
| 143 | + // development. |
| 144 | + } |
| 145 | + loadEnvConfig({ dev, forceReload, silent }) { |
| 146 | + (0, _env.loadEnvConfig)(this.dir, dev, silent ? { |
| 147 | + info: ()=>{}, |
| 148 | + error: ()=>{} |
| 149 | + } : _log, forceReload); |
| 150 | + } |
| 151 | + async hasPage(pathname) { |
| 152 | + var _this_nextConfig_i18n; |
| 153 | + return !!(0, _require.getMaybePagePath)(pathname, this.distDir, (_this_nextConfig_i18n = this.nextConfig.i18n) == null ? void 0 : _this_nextConfig_i18n.locales, this.enabledDirectories.app); |
| 154 | + } |
| 155 | + getBuildId() { |
| 156 | + const buildIdFile = (0, _path.join)(this.distDir, _constants.BUILD_ID_FILE); |
| 157 | + try { |
| 158 | + return _fs.default.readFileSync(buildIdFile, "utf8").trim(); |
| 159 | + } catch (err) { |
| 160 | + if (err.code === "ENOENT") { |
| 161 | + throw new Error(\`Could not find a production build in the '\${this.distDir}' directory. Try building your app with 'next build' before starting the production server. https://nextjs.org/docs/messages/production-start-no-build-id\`); |
| 162 | + } |
| 163 | + throw err; |
| 164 | + } |
| 165 | + } |
| 166 | + getMiddlewareManifest() { |
| 167 | + return "manifest"; |
| 168 | + } |
| 169 | + getEnabledDirectories(dev) { |
| 170 | + const dir = dev ? this.dir : this.serverDistDir; |
| 171 | + return { |
| 172 | + app: (0, _findpagesdir.findDir)(dir, "app") ? true : false, |
| 173 | + pages: (0, _findpagesdir.findDir)(dir, "pages") ? true : false |
| 174 | + }; |
| 175 | + } |
| 176 | + // ... |
| 177 | + }" |
| 178 | + `); |
| 179 | + }); |
| 180 | +}); |
0 commit comments