|
| 1 | +import { patchCode } from "@opennextjs/aws/build/patch/astCodePatcher.js"; |
| 2 | +import { |
| 3 | + createEmptyBodyRule, |
| 4 | + errorInspectRule, |
| 5 | +} from "@opennextjs/aws/build/patch/patches/dropBabel.js"; |
| 6 | +import { describe, expect, test } from "vitest"; |
| 7 | + |
| 8 | +describe("babel-drop", () => { |
| 9 | + test("Drop body", () => { |
| 10 | + const code = ` |
| 11 | +class NextNodeServer extends _baseserver.default { |
| 12 | + constructor(options){ |
| 13 | + // Initialize super class |
| 14 | + super(options); |
| 15 | + this.handleNextImageRequest = async (req, res, parsedUrl) => { /* ... */ }; |
| 16 | + } |
| 17 | + async handleUpgrade() { |
| 18 | + // The web server does not support web sockets, it's only used for HMR in |
| 19 | + // development. |
| 20 | + } |
| 21 | + getEnabledDirectories(dev) { |
| 22 | + const dir = dev ? this.dir : this.serverDistDir; |
| 23 | + return { |
| 24 | + app: (0, _findpagesdir.findDir)(dir, "app") ? true : false, |
| 25 | + pages: (0, _findpagesdir.findDir)(dir, "pages") ? true : false |
| 26 | + }; |
| 27 | + } |
| 28 | + /** |
| 29 | + * This method gets all middleware matchers and execute them when the request |
| 30 | + * matches. It will make sure that each middleware exists and is compiled and |
| 31 | + * ready to be invoked. The development server will decorate it to add warns |
| 32 | + * and errors with rich traces. |
| 33 | + */ async runMiddleware(params) { |
| 34 | + if (process.env.NEXT_MINIMAL) { |
| 35 | + throw new Error('invariant: runMiddleware should not be called in minimal mode'); |
| 36 | + } |
| 37 | + // Middleware is skipped for on-demand revalidate requests |
| 38 | + if ((0, _apiutils.checkIsOnDemandRevalidate)(params.request, this.renderOpts.previewProps).isOnDemandRevalidate) { |
| 39 | + return { |
| 40 | + response: new Response(null, { |
| 41 | + headers: { |
| 42 | + 'x-middleware-next': '1' |
| 43 | + } |
| 44 | + }) |
| 45 | + }; |
| 46 | + } |
| 47 | + // ... |
| 48 | + } |
| 49 | + async runEdgeFunction(params) { |
| 50 | + if (process.env.NEXT_MINIMAL) { |
| 51 | + throw new Error('Middleware is not supported in minimal mode.'); |
| 52 | + } |
| 53 | + let edgeInfo; |
| 54 | + const { query, page, match } = params; |
| 55 | + if (!match) await this.ensureEdgeFunction({ |
| 56 | + page, |
| 57 | + appPaths: params.appPaths, |
| 58 | + url: params.req.url |
| 59 | + }); |
| 60 | + // ... |
| 61 | + } |
| 62 | + // ... |
| 63 | +}`; |
| 64 | + |
| 65 | + expect( |
| 66 | + patchCode(code, createEmptyBodyRule("runMiddleware")), |
| 67 | + ).toMatchInlineSnapshot(` |
| 68 | + "class NextNodeServer extends _baseserver.default { |
| 69 | + constructor(options){ |
| 70 | + // Initialize super class |
| 71 | + super(options); |
| 72 | + this.handleNextImageRequest = async (req, res, parsedUrl) => { /* ... */ }; |
| 73 | + } |
| 74 | + async handleUpgrade() { |
| 75 | + // The web server does not support web sockets, it's only used for HMR in |
| 76 | + // development. |
| 77 | + } |
| 78 | + getEnabledDirectories(dev) { |
| 79 | + const dir = dev ? this.dir : this.serverDistDir; |
| 80 | + return { |
| 81 | + app: (0, _findpagesdir.findDir)(dir, "app") ? true : false, |
| 82 | + pages: (0, _findpagesdir.findDir)(dir, "pages") ? true : false |
| 83 | + }; |
| 84 | + } |
| 85 | + /** |
| 86 | + * This method gets all middleware matchers and execute them when the request |
| 87 | + * matches. It will make sure that each middleware exists and is compiled and |
| 88 | + * ready to be invoked. The development server will decorate it to add warns |
| 89 | + * and errors with rich traces. |
| 90 | + */ async runMiddleware(params) { |
| 91 | + throw new Error("runMiddleware should not be called with OpenNext"); |
| 92 | + } |
| 93 | + async runEdgeFunction(params) { |
| 94 | + if (process.env.NEXT_MINIMAL) { |
| 95 | + throw new Error('Middleware is not supported in minimal mode.'); |
| 96 | + } |
| 97 | + let edgeInfo; |
| 98 | + const { query, page, match } = params; |
| 99 | + if (!match) await this.ensureEdgeFunction({ |
| 100 | + page, |
| 101 | + appPaths: params.appPaths, |
| 102 | + url: params.req.url |
| 103 | + }); |
| 104 | + // ... |
| 105 | + } |
| 106 | + // ... |
| 107 | + }" |
| 108 | + `); |
| 109 | + |
| 110 | + expect( |
| 111 | + patchCode(code, createEmptyBodyRule("runEdgeFunction")), |
| 112 | + ).toMatchInlineSnapshot(` |
| 113 | + "class NextNodeServer extends _baseserver.default { |
| 114 | + constructor(options){ |
| 115 | + // Initialize super class |
| 116 | + super(options); |
| 117 | + this.handleNextImageRequest = async (req, res, parsedUrl) => { /* ... */ }; |
| 118 | + } |
| 119 | + async handleUpgrade() { |
| 120 | + // The web server does not support web sockets, it's only used for HMR in |
| 121 | + // development. |
| 122 | + } |
| 123 | + getEnabledDirectories(dev) { |
| 124 | + const dir = dev ? this.dir : this.serverDistDir; |
| 125 | + return { |
| 126 | + app: (0, _findpagesdir.findDir)(dir, "app") ? true : false, |
| 127 | + pages: (0, _findpagesdir.findDir)(dir, "pages") ? true : false |
| 128 | + }; |
| 129 | + } |
| 130 | + /** |
| 131 | + * This method gets all middleware matchers and execute them when the request |
| 132 | + * matches. It will make sure that each middleware exists and is compiled and |
| 133 | + * ready to be invoked. The development server will decorate it to add warns |
| 134 | + * and errors with rich traces. |
| 135 | + */ async runMiddleware(params) { |
| 136 | + if (process.env.NEXT_MINIMAL) { |
| 137 | + throw new Error('invariant: runMiddleware should not be called in minimal mode'); |
| 138 | + } |
| 139 | + // Middleware is skipped for on-demand revalidate requests |
| 140 | + if ((0, _apiutils.checkIsOnDemandRevalidate)(params.request, this.renderOpts.previewProps).isOnDemandRevalidate) { |
| 141 | + return { |
| 142 | + response: new Response(null, { |
| 143 | + headers: { |
| 144 | + 'x-middleware-next': '1' |
| 145 | + } |
| 146 | + }) |
| 147 | + }; |
| 148 | + } |
| 149 | + // ... |
| 150 | + } |
| 151 | + async runEdgeFunction(params) { |
| 152 | + throw new Error("runEdgeFunction should not be called with OpenNext"); |
| 153 | + } |
| 154 | + // ... |
| 155 | + }" |
| 156 | + `); |
| 157 | + }); |
| 158 | + |
| 159 | + test("Error Inspect", () => { |
| 160 | + const code = ` |
| 161 | +// This file should be imported before any others. It sets up the environment |
| 162 | +// for later imports to work properly. |
| 163 | +"use strict"; |
| 164 | +Object.defineProperty(exports, "__esModule", { |
| 165 | + value: true |
| 166 | +}); |
| 167 | +require("./node-environment-baseline"); |
| 168 | +require("./node-environment-extensions/error-inspect"); |
| 169 | +require("./node-environment-extensions/random"); |
| 170 | +require("./node-environment-extensions/date"); |
| 171 | +require("./node-environment-extensions/web-crypto"); |
| 172 | +require("./node-environment-extensions/node-crypto"); |
| 173 | +//# sourceMappingURL=node-environment.js.map |
| 174 | +}`; |
| 175 | + |
| 176 | + expect(patchCode(code, errorInspectRule)).toMatchInlineSnapshot(` |
| 177 | + "// This file should be imported before any others. It sets up the environment |
| 178 | + // for later imports to work properly. |
| 179 | + "use strict"; |
| 180 | + Object.defineProperty(exports, "__esModule", { |
| 181 | + value: true |
| 182 | + }); |
| 183 | + require("./node-environment-baseline"); |
| 184 | + // Removed by OpenNext |
| 185 | + // require("./node-environment-extensions/error-inspect"); |
| 186 | + require("./node-environment-extensions/random"); |
| 187 | + require("./node-environment-extensions/date"); |
| 188 | + require("./node-environment-extensions/web-crypto"); |
| 189 | + require("./node-environment-extensions/node-crypto"); |
| 190 | + //# sourceMappingURL=node-environment.js.map |
| 191 | + }" |
| 192 | + `); |
| 193 | + }); |
| 194 | +}); |
0 commit comments