@vercel/microfrontends 0.12.1 → 0.14.0
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/README.md +3 -3
- package/dist/bin/cli.cjs +241 -176
- package/dist/config/client.d.ts +1 -1
- package/dist/config.cjs.map +1 -1
- package/dist/config.d.ts +1 -1
- package/dist/config.js.map +1 -1
- package/dist/{index-83133f2d.d.ts → index-bf67a461.d.ts} +3 -10
- package/dist/next/client.cjs +1 -1
- package/dist/next/client.cjs.map +1 -1
- package/dist/next/client.js +1 -1
- package/dist/next/client.js.map +1 -1
- package/dist/next/endpoints.cjs.map +1 -1
- package/dist/next/endpoints.d.ts +1 -1
- package/dist/next/endpoints.js.map +1 -1
- package/dist/next/middleware.cjs +0 -29
- package/dist/next/middleware.cjs.map +1 -1
- package/dist/next/middleware.js +0 -29
- package/dist/next/middleware.js.map +1 -1
- package/dist/{types-a995174e.d.ts → types-a29d224a.d.ts} +1 -7
- package/dist/{types-15b7f215.d.ts → types-cfe3308b.d.ts} +1 -1
- package/dist/types-fc30696d.d.ts +11 -0
- package/dist/utils/mfe-port.cjs +2704 -0
- package/dist/utils/mfe-port.cjs.map +1 -0
- package/dist/utils/mfe-port.d.ts +8 -0
- package/dist/utils/mfe-port.js +2669 -0
- package/dist/utils/mfe-port.js.map +1 -0
- package/dist/v2/config.cjs +0 -14
- package/dist/v2/config.cjs.map +1 -1
- package/dist/v2/config.d.ts +4 -3
- package/dist/v2/config.js +0 -14
- package/dist/v2/config.js.map +1 -1
- package/dist/v2/microfrontends/server.cjs +68 -54
- package/dist/v2/microfrontends/server.cjs.map +1 -1
- package/dist/v2/microfrontends/server.d.ts +6 -5
- package/dist/v2/microfrontends/server.js +65 -51
- package/dist/v2/microfrontends/server.js.map +1 -1
- package/dist/v2/microfrontends.cjs +0 -14
- package/dist/v2/microfrontends.cjs.map +1 -1
- package/dist/v2/microfrontends.d.ts +4 -3
- package/dist/v2/microfrontends.js +0 -14
- package/dist/v2/microfrontends.js.map +1 -1
- package/dist/v2/next/client.cjs +1 -1
- package/dist/v2/next/client.cjs.map +1 -1
- package/dist/v2/next/client.js +1 -1
- package/dist/v2/next/client.js.map +1 -1
- package/dist/v2/next/config.cjs +116 -65
- package/dist/v2/next/config.cjs.map +1 -1
- package/dist/v2/next/config.js +113 -62
- package/dist/v2/next/config.js.map +1 -1
- package/dist/v2/next/endpoints.cjs.map +1 -1
- package/dist/v2/next/endpoints.d.ts +13 -2
- package/dist/v2/next/endpoints.js.map +1 -1
- package/dist/v2/next/middleware.cjs +20 -59
- package/dist/v2/next/middleware.cjs.map +1 -1
- package/dist/v2/next/middleware.d.ts +7 -2
- package/dist/v2/next/middleware.js +20 -59
- package/dist/v2/next/middleware.js.map +1 -1
- package/dist/v2/next/testing.cjs +992 -0
- package/dist/v2/next/testing.cjs.map +1 -0
- package/dist/v2/next/testing.d.ts +55 -0
- package/dist/v2/next/testing.js +961 -0
- package/dist/v2/next/testing.js.map +1 -0
- package/dist/v2/overrides.d.ts +3 -3
- package/dist/v2/routing.cjs +19 -0
- package/dist/v2/routing.cjs.map +1 -0
- package/dist/v2/routing.d.ts +26 -0
- package/dist/v2/routing.js +1 -0
- package/dist/v2/routing.js.map +1 -0
- package/dist/v2/schema.cjs.map +1 -1
- package/dist/v2/schema.d.ts +1 -1
- package/dist/validation.cjs +0 -4
- package/dist/validation.cjs.map +1 -1
- package/dist/validation.d.ts +0 -6
- package/dist/validation.js +0 -4
- package/dist/validation.js.map +1 -1
- package/package.json +25 -3
- package/schema/schema-v2.json +0 -4
|
@@ -0,0 +1,992 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/next-v2/testing/index.ts
|
|
21
|
+
var testing_exports = {};
|
|
22
|
+
__export(testing_exports, {
|
|
23
|
+
expandWildcards: () => expandWildcards,
|
|
24
|
+
getAllChildApplicationNames: () => getAllChildApplicationNames,
|
|
25
|
+
getAllMultiZonesPaths: () => getAllMultiZonesPaths,
|
|
26
|
+
getExpectedDomainForApp: () => getExpectedDomainForApp,
|
|
27
|
+
getFlaggedPathsForApp: () => getFlaggedPathsForApp,
|
|
28
|
+
getLaunchedPathsForApp: () => getLaunchedPathsForApp,
|
|
29
|
+
loadMicrofrontendConfigForEdge: () => loadMicrofrontendConfigForEdge,
|
|
30
|
+
validateMiddlewareConfig: () => validateMiddlewareConfig,
|
|
31
|
+
validateMiddlewareOnFlaggedPaths: () => validateMiddlewareOnFlaggedPaths
|
|
32
|
+
});
|
|
33
|
+
module.exports = __toCommonJS(testing_exports);
|
|
34
|
+
var import_node_fs = require("fs");
|
|
35
|
+
var import_server = require("next/server");
|
|
36
|
+
var import_path_to_regexp3 = require("path-to-regexp");
|
|
37
|
+
var import_jsonc_parser2 = require("jsonc-parser");
|
|
38
|
+
var import_prepare_destination = require("next/dist/shared/lib/router/utils/prepare-destination");
|
|
39
|
+
var import_adapter = require("next/dist/server/web/adapter");
|
|
40
|
+
var import_web = require("next/dist/server/base-http/web");
|
|
41
|
+
|
|
42
|
+
// src/config-v2/microfrontends-config/isomorphic/index.ts
|
|
43
|
+
var import_jsonc_parser = require("jsonc-parser");
|
|
44
|
+
|
|
45
|
+
// src/config-v2/errors.ts
|
|
46
|
+
var MicrofrontendError = class extends Error {
|
|
47
|
+
constructor(message, opts) {
|
|
48
|
+
super(message);
|
|
49
|
+
this.name = "MicrofrontendsError";
|
|
50
|
+
this.source = (opts == null ? void 0 : opts.source) ?? "@vercel/microfrontends";
|
|
51
|
+
this.type = (opts == null ? void 0 : opts.type) ?? "unknown";
|
|
52
|
+
this.subtype = opts == null ? void 0 : opts.subtype;
|
|
53
|
+
Error.captureStackTrace(this, MicrofrontendError);
|
|
54
|
+
}
|
|
55
|
+
isKnown() {
|
|
56
|
+
return this.type !== "unknown";
|
|
57
|
+
}
|
|
58
|
+
isUnknown() {
|
|
59
|
+
return !this.isKnown();
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Converts an error to a MicrofrontendsError.
|
|
63
|
+
* @param original - The original error to convert.
|
|
64
|
+
* @returns The converted MicrofrontendsError.
|
|
65
|
+
*/
|
|
66
|
+
static convert(original, opts) {
|
|
67
|
+
if (opts == null ? void 0 : opts.fileName) {
|
|
68
|
+
const err = MicrofrontendError.convertFSError(original, opts.fileName);
|
|
69
|
+
if (err) {
|
|
70
|
+
return err;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
if (original.message.includes(
|
|
74
|
+
"Code generation from strings disallowed for this context"
|
|
75
|
+
)) {
|
|
76
|
+
return new MicrofrontendError(original.message, {
|
|
77
|
+
type: "config",
|
|
78
|
+
subtype: "unsupported_validation_env",
|
|
79
|
+
source: "ajv"
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
return new MicrofrontendError(original.message);
|
|
83
|
+
}
|
|
84
|
+
static convertFSError(original, fileName) {
|
|
85
|
+
if (original instanceof Error && "code" in original) {
|
|
86
|
+
if (original.code === "ENOENT") {
|
|
87
|
+
return new MicrofrontendError(`Could not find "${fileName}"`, {
|
|
88
|
+
type: "config",
|
|
89
|
+
subtype: "unable_to_read_file",
|
|
90
|
+
source: "fs"
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
if (original.code === "EACCES") {
|
|
94
|
+
return new MicrofrontendError(
|
|
95
|
+
`Permission denied while accessing "${fileName}"`,
|
|
96
|
+
{
|
|
97
|
+
type: "config",
|
|
98
|
+
subtype: "invalid_permissions",
|
|
99
|
+
source: "fs"
|
|
100
|
+
}
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
if (original instanceof SyntaxError) {
|
|
105
|
+
return new MicrofrontendError(
|
|
106
|
+
`Failed to parse "${fileName}": Invalid JSON format.`,
|
|
107
|
+
{
|
|
108
|
+
type: "config",
|
|
109
|
+
subtype: "invalid_syntax",
|
|
110
|
+
source: "fs"
|
|
111
|
+
}
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
return null;
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Handles an unknown error and returns a MicrofrontendsError instance.
|
|
118
|
+
* @param err - The error to handle.
|
|
119
|
+
* @returns A MicrofrontendsError instance.
|
|
120
|
+
*/
|
|
121
|
+
static handle(err, opts) {
|
|
122
|
+
if (err instanceof MicrofrontendError) {
|
|
123
|
+
return err;
|
|
124
|
+
}
|
|
125
|
+
if (err instanceof Error) {
|
|
126
|
+
return MicrofrontendError.convert(err, opts);
|
|
127
|
+
}
|
|
128
|
+
if (typeof err === "object" && err !== null) {
|
|
129
|
+
if ("message" in err && typeof err.message === "string") {
|
|
130
|
+
return MicrofrontendError.convert(new Error(err.message), opts);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
return new MicrofrontendError("An unknown error occurred");
|
|
134
|
+
}
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
// src/config-v2/microfrontends-config/utils/get-config-from-env.ts
|
|
138
|
+
function getConfigStringFromEnv() {
|
|
139
|
+
const config = process.env.MFE_CONFIG;
|
|
140
|
+
if (!config) {
|
|
141
|
+
throw new MicrofrontendError(`Missing "MFE_CONFIG" in environment.`, {
|
|
142
|
+
type: "config",
|
|
143
|
+
subtype: "not_found_in_env"
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
return config;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// src/config-v2/schema/utils/is-main-config.ts
|
|
150
|
+
function isMainConfig(c) {
|
|
151
|
+
return !("partOf" in c);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// src/config-v2/schema/utils/is-default-app.ts
|
|
155
|
+
function isDefaultApp(a) {
|
|
156
|
+
return !("routing" in a);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// src/config-v2/microfrontends-config/client/index.ts
|
|
160
|
+
var import_path_to_regexp = require("path-to-regexp");
|
|
161
|
+
var MicrofrontendConfigClient = class {
|
|
162
|
+
constructor(config, opts) {
|
|
163
|
+
this.pathCache = {};
|
|
164
|
+
this.serialized = config;
|
|
165
|
+
if (opts == null ? void 0 : opts.removeFlaggedPaths) {
|
|
166
|
+
for (const app of Object.values(config.applications)) {
|
|
167
|
+
if (app.routing) {
|
|
168
|
+
app.routing = app.routing.filter((match2) => !match2.flag);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
this.applications = config.applications;
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Create a new `MicrofrontendConfigClient` from a JSON string.
|
|
176
|
+
* Config must be passed in to remain framework agnostic
|
|
177
|
+
*/
|
|
178
|
+
static fromEnv(config, opts) {
|
|
179
|
+
if (!config) {
|
|
180
|
+
throw new Error("No microfrontends configuration found");
|
|
181
|
+
}
|
|
182
|
+
return new MicrofrontendConfigClient(
|
|
183
|
+
JSON.parse(config),
|
|
184
|
+
opts
|
|
185
|
+
);
|
|
186
|
+
}
|
|
187
|
+
isEqual(other) {
|
|
188
|
+
return JSON.stringify(this.applications) === JSON.stringify(other.applications);
|
|
189
|
+
}
|
|
190
|
+
getApplicationNameForPath(path) {
|
|
191
|
+
if (!path.startsWith("/")) {
|
|
192
|
+
throw new Error(`Path must start with a /`);
|
|
193
|
+
}
|
|
194
|
+
if (this.pathCache[path]) {
|
|
195
|
+
return this.pathCache[path];
|
|
196
|
+
}
|
|
197
|
+
const pathname = new URL(path, "https://example.com").pathname;
|
|
198
|
+
for (const [name, application] of Object.entries(this.applications)) {
|
|
199
|
+
if (application.routing) {
|
|
200
|
+
for (const group of application.routing) {
|
|
201
|
+
for (const childPath of group.paths) {
|
|
202
|
+
const regexp = (0, import_path_to_regexp.pathToRegexp)(childPath);
|
|
203
|
+
if (regexp.test(pathname)) {
|
|
204
|
+
this.pathCache[path] = name;
|
|
205
|
+
return name;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
const defaultApplication = Object.entries(this.applications).find(
|
|
212
|
+
([, application]) => application.default
|
|
213
|
+
);
|
|
214
|
+
if (!defaultApplication) {
|
|
215
|
+
return null;
|
|
216
|
+
}
|
|
217
|
+
this.pathCache[path] = defaultApplication[0];
|
|
218
|
+
return defaultApplication[0];
|
|
219
|
+
}
|
|
220
|
+
serialize() {
|
|
221
|
+
return this.serialized;
|
|
222
|
+
}
|
|
223
|
+
};
|
|
224
|
+
|
|
225
|
+
// src/config-v2/overrides/constants.ts
|
|
226
|
+
var OVERRIDES_COOKIE_PREFIX = "vercel-micro-frontends-override";
|
|
227
|
+
var OVERRIDES_ENV_COOKIE_PREFIX = `${OVERRIDES_COOKIE_PREFIX}:env:`;
|
|
228
|
+
|
|
229
|
+
// src/config-v2/overrides/is-override-cookie.ts
|
|
230
|
+
function isOverrideCookie(cookie) {
|
|
231
|
+
var _a;
|
|
232
|
+
return Boolean((_a = cookie.name) == null ? void 0 : _a.startsWith(OVERRIDES_COOKIE_PREFIX));
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
// src/config-v2/overrides/get-override-from-cookie.ts
|
|
236
|
+
function getOverrideFromCookie(cookie) {
|
|
237
|
+
if (!isOverrideCookie(cookie) || !cookie.value)
|
|
238
|
+
return;
|
|
239
|
+
return {
|
|
240
|
+
application: cookie.name.replace(OVERRIDES_ENV_COOKIE_PREFIX, ""),
|
|
241
|
+
host: cookie.value
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
// src/config-v2/overrides/parse-overrides.ts
|
|
246
|
+
function parseOverrides(cookies) {
|
|
247
|
+
const overridesConfig = { applications: {} };
|
|
248
|
+
cookies.forEach((cookie) => {
|
|
249
|
+
const override = getOverrideFromCookie(cookie);
|
|
250
|
+
if (!override)
|
|
251
|
+
return;
|
|
252
|
+
overridesConfig.applications[override.application] = {
|
|
253
|
+
environment: { host: override.host }
|
|
254
|
+
};
|
|
255
|
+
});
|
|
256
|
+
return overridesConfig;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
// src/config-v2/microfrontends-config/isomorphic/validation.ts
|
|
260
|
+
var import_path_to_regexp2 = require("path-to-regexp");
|
|
261
|
+
var SUPPORTED_VERSIONS = ["2"];
|
|
262
|
+
var validateConfigVersion = (version) => {
|
|
263
|
+
if (!SUPPORTED_VERSIONS.includes(version)) {
|
|
264
|
+
throw new MicrofrontendError(
|
|
265
|
+
`Unsupported version: ${version}. Supported versions are: ${SUPPORTED_VERSIONS.join(
|
|
266
|
+
", "
|
|
267
|
+
)}`,
|
|
268
|
+
{ type: "config", subtype: "unsupported_version" }
|
|
269
|
+
);
|
|
270
|
+
}
|
|
271
|
+
};
|
|
272
|
+
var validateConfigPaths = (applicationConfigsById) => {
|
|
273
|
+
if (!applicationConfigsById) {
|
|
274
|
+
return;
|
|
275
|
+
}
|
|
276
|
+
const pathsByApplicationId = /* @__PURE__ */ new Map();
|
|
277
|
+
const errors = [];
|
|
278
|
+
for (const [id, app] of Object.entries(applicationConfigsById)) {
|
|
279
|
+
if (isDefaultApp(app)) {
|
|
280
|
+
continue;
|
|
281
|
+
}
|
|
282
|
+
for (const pathMatch of app.routing) {
|
|
283
|
+
for (const path of pathMatch.paths) {
|
|
284
|
+
const tokens = (0, import_path_to_regexp2.parse)(path);
|
|
285
|
+
for (const token of tokens.slice(0, -1)) {
|
|
286
|
+
if (typeof token !== "string") {
|
|
287
|
+
errors.push(
|
|
288
|
+
`Path ${path} may only have a :wildcard in the last path component`
|
|
289
|
+
);
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
const existing = pathsByApplicationId.get(path);
|
|
293
|
+
if (existing) {
|
|
294
|
+
existing.applications.push(id);
|
|
295
|
+
} else {
|
|
296
|
+
pathsByApplicationId.set(path, {
|
|
297
|
+
applications: [id],
|
|
298
|
+
matcher: (0, import_path_to_regexp2.pathToRegexp)(path),
|
|
299
|
+
applicationId: id
|
|
300
|
+
});
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
const entries = Array.from(pathsByApplicationId.entries());
|
|
306
|
+
entries.forEach(([path, { applications: ids, matcher, applicationId }]) => {
|
|
307
|
+
if (ids.length > 1) {
|
|
308
|
+
errors.push(
|
|
309
|
+
`Duplicate path "${path}" for applications "${ids.join(", ")}"`
|
|
310
|
+
);
|
|
311
|
+
}
|
|
312
|
+
entries.forEach(
|
|
313
|
+
([
|
|
314
|
+
matchPath,
|
|
315
|
+
{ applications: matchIds, applicationId: matchApplicationId }
|
|
316
|
+
]) => {
|
|
317
|
+
if (path === matchPath) {
|
|
318
|
+
return;
|
|
319
|
+
}
|
|
320
|
+
if (applicationId === matchApplicationId) {
|
|
321
|
+
return;
|
|
322
|
+
}
|
|
323
|
+
if (matcher.test(matchPath)) {
|
|
324
|
+
const source = `"${path}" of application${ids.length > 0 ? "s" : ""} ${ids.join(", ")}`;
|
|
325
|
+
const destination = `"${matchPath}" of application${matchIds.length > 0 ? "s" : ""} ${matchIds.join(", ")}`;
|
|
326
|
+
errors.push(
|
|
327
|
+
`Overlapping path detected between ${source} and ${destination}`
|
|
328
|
+
);
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
);
|
|
332
|
+
});
|
|
333
|
+
if (errors.length) {
|
|
334
|
+
throw new MicrofrontendError(`Invalid paths: ${errors.join(", ")}`, {
|
|
335
|
+
type: "config",
|
|
336
|
+
subtype: "conflicting_paths"
|
|
337
|
+
});
|
|
338
|
+
}
|
|
339
|
+
};
|
|
340
|
+
var validateAppPaths = (name, app) => {
|
|
341
|
+
for (const group of app.routing) {
|
|
342
|
+
for (const p of group.paths) {
|
|
343
|
+
if (p === "/") {
|
|
344
|
+
continue;
|
|
345
|
+
}
|
|
346
|
+
if (p.endsWith("/")) {
|
|
347
|
+
throw new MicrofrontendError(
|
|
348
|
+
`Invalid path for application "${name}". ${p} must not end with a slash.`,
|
|
349
|
+
{ type: "application", subtype: "invalid_path" }
|
|
350
|
+
);
|
|
351
|
+
}
|
|
352
|
+
if (!p.startsWith("/")) {
|
|
353
|
+
throw new MicrofrontendError(
|
|
354
|
+
`Invalid path for application "${name}". ${p} must start with a slash.`,
|
|
355
|
+
{ type: "application", subtype: "invalid_path" }
|
|
356
|
+
);
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
};
|
|
361
|
+
var validateConfigDefaultApplication = (applicationConfigsById) => {
|
|
362
|
+
if (!applicationConfigsById) {
|
|
363
|
+
return;
|
|
364
|
+
}
|
|
365
|
+
const applicationsWithRouting = Object.entries(applicationConfigsById).filter(
|
|
366
|
+
([, app]) => !isDefaultApp(app)
|
|
367
|
+
);
|
|
368
|
+
const applicationsWithRoutingNames = applicationsWithRouting.map(
|
|
369
|
+
([key]) => key
|
|
370
|
+
);
|
|
371
|
+
const numApplications = Object.keys(applicationConfigsById).length;
|
|
372
|
+
const numApplicationsWithRouting = applicationsWithRoutingNames.length;
|
|
373
|
+
const numApplicationsWithoutRouting = numApplications - numApplicationsWithRouting;
|
|
374
|
+
if (numApplicationsWithoutRouting === 0) {
|
|
375
|
+
throw new MicrofrontendError(
|
|
376
|
+
`No default application found. At least one application needs to be the default by omitting routing.`,
|
|
377
|
+
{ type: "config", subtype: "no_default_application" }
|
|
378
|
+
);
|
|
379
|
+
}
|
|
380
|
+
if (numApplicationsWithoutRouting > 1) {
|
|
381
|
+
throw new MicrofrontendError(
|
|
382
|
+
`Only one application can omit "routing". Found ${applicationsWithRoutingNames.length - Object.keys(applicationConfigsById).length > 1}.`,
|
|
383
|
+
{ type: "config", subtype: "multiple_default_applications" }
|
|
384
|
+
);
|
|
385
|
+
}
|
|
386
|
+
};
|
|
387
|
+
|
|
388
|
+
// src/config-v2/microfrontends-config/isomorphic/utils/generate-asset-prefix.ts
|
|
389
|
+
var PREFIX = "vc-ap";
|
|
390
|
+
function generateAssetPrefixFromName({
|
|
391
|
+
name
|
|
392
|
+
}) {
|
|
393
|
+
if (!name) {
|
|
394
|
+
throw new Error("Name is required to generate an asset prefix");
|
|
395
|
+
}
|
|
396
|
+
return `${PREFIX}-${name}`;
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
// src/config-v2/microfrontends-config/isomorphic/utils/generate-port.ts
|
|
400
|
+
function generatePortFromName({
|
|
401
|
+
name,
|
|
402
|
+
minPort = 3e3,
|
|
403
|
+
maxPort = 8e3
|
|
404
|
+
}) {
|
|
405
|
+
if (!name) {
|
|
406
|
+
throw new Error("Name is required to generate a port");
|
|
407
|
+
}
|
|
408
|
+
let hash = 0;
|
|
409
|
+
for (let i = 0; i < name.length; i++) {
|
|
410
|
+
hash = (hash << 5) - hash + name.charCodeAt(i);
|
|
411
|
+
hash |= 0;
|
|
412
|
+
}
|
|
413
|
+
hash = Math.abs(hash);
|
|
414
|
+
const range = maxPort - minPort;
|
|
415
|
+
const port = minPort + hash % range;
|
|
416
|
+
return port;
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
// src/config-v2/microfrontends-config/isomorphic/host.ts
|
|
420
|
+
var Host = class {
|
|
421
|
+
constructor(hostConfig, options) {
|
|
422
|
+
const { protocol = "https", host, port } = hostConfig;
|
|
423
|
+
this.protocol = protocol;
|
|
424
|
+
this.host = host;
|
|
425
|
+
this.port = Host.getPort({ port, protocol: this.protocol });
|
|
426
|
+
this.local = options == null ? void 0 : options.isLocal;
|
|
427
|
+
}
|
|
428
|
+
isLocal() {
|
|
429
|
+
return this.local || this.host === "localhost" || this.host === "127.0.0.1";
|
|
430
|
+
}
|
|
431
|
+
static getPort({
|
|
432
|
+
protocol,
|
|
433
|
+
port
|
|
434
|
+
}) {
|
|
435
|
+
if (!port) {
|
|
436
|
+
if (protocol === "http") {
|
|
437
|
+
return 80;
|
|
438
|
+
}
|
|
439
|
+
return 443;
|
|
440
|
+
}
|
|
441
|
+
return port;
|
|
442
|
+
}
|
|
443
|
+
isDefaultPort() {
|
|
444
|
+
return this.port === Host.getPort({ protocol: this.protocol });
|
|
445
|
+
}
|
|
446
|
+
toString(opts = {}) {
|
|
447
|
+
const url = this.toUrl(opts);
|
|
448
|
+
return url.toString().replace(/\/$/, "");
|
|
449
|
+
}
|
|
450
|
+
toUrl(opts = {}) {
|
|
451
|
+
const { includeDefaultPort } = opts;
|
|
452
|
+
const url = `${this.protocol}://${this.host}${this.isDefaultPort() && !includeDefaultPort ? "" : `:${this.port}`}`;
|
|
453
|
+
return new URL(url);
|
|
454
|
+
}
|
|
455
|
+
};
|
|
456
|
+
var LocalHost = class extends Host {
|
|
457
|
+
constructor({
|
|
458
|
+
appName,
|
|
459
|
+
...hostConfig
|
|
460
|
+
}) {
|
|
461
|
+
const host = hostConfig.host ?? "localhost";
|
|
462
|
+
const port = hostConfig.port ?? generatePortFromName({ name: appName });
|
|
463
|
+
const protocol = hostConfig.protocol ?? "http";
|
|
464
|
+
super({ protocol, host, port });
|
|
465
|
+
}
|
|
466
|
+
};
|
|
467
|
+
|
|
468
|
+
// src/config-v2/microfrontends-config/isomorphic/application.ts
|
|
469
|
+
var Application = class {
|
|
470
|
+
constructor(name, {
|
|
471
|
+
app,
|
|
472
|
+
overrides,
|
|
473
|
+
isDefault
|
|
474
|
+
}) {
|
|
475
|
+
var _a, _b;
|
|
476
|
+
this.name = name;
|
|
477
|
+
this.development = {
|
|
478
|
+
local: new LocalHost({
|
|
479
|
+
appName: name,
|
|
480
|
+
...(_a = app.development) == null ? void 0 : _a.local
|
|
481
|
+
}),
|
|
482
|
+
fallback: ((_b = app.development) == null ? void 0 : _b.fallback) ? new Host(app.development.fallback) : void 0
|
|
483
|
+
};
|
|
484
|
+
this.production = app.production ? new Host(app.production) : void 0;
|
|
485
|
+
this.vercel = app.vercel;
|
|
486
|
+
this.overrides = (overrides == null ? void 0 : overrides.environment) ? {
|
|
487
|
+
environment: new Host(overrides.environment)
|
|
488
|
+
} : void 0;
|
|
489
|
+
this.default = isDefault ?? false;
|
|
490
|
+
this.serialized = app;
|
|
491
|
+
}
|
|
492
|
+
isDefault() {
|
|
493
|
+
return this.default;
|
|
494
|
+
}
|
|
495
|
+
getAssetPrefix() {
|
|
496
|
+
return generateAssetPrefixFromName({ name: this.name });
|
|
497
|
+
}
|
|
498
|
+
serialize() {
|
|
499
|
+
return this.serialized;
|
|
500
|
+
}
|
|
501
|
+
};
|
|
502
|
+
var DefaultApplication = class extends Application {
|
|
503
|
+
constructor(name, {
|
|
504
|
+
app,
|
|
505
|
+
overrides
|
|
506
|
+
}) {
|
|
507
|
+
super(name, {
|
|
508
|
+
app,
|
|
509
|
+
overrides,
|
|
510
|
+
isDefault: true
|
|
511
|
+
});
|
|
512
|
+
this.default = true;
|
|
513
|
+
this.production = new Host(app.production);
|
|
514
|
+
}
|
|
515
|
+
getAssetPrefix() {
|
|
516
|
+
return "";
|
|
517
|
+
}
|
|
518
|
+
};
|
|
519
|
+
var ChildApplication = class extends Application {
|
|
520
|
+
constructor(name, {
|
|
521
|
+
app,
|
|
522
|
+
overrides
|
|
523
|
+
}) {
|
|
524
|
+
ChildApplication.validate(name, app);
|
|
525
|
+
super(name, {
|
|
526
|
+
app,
|
|
527
|
+
overrides,
|
|
528
|
+
isDefault: false
|
|
529
|
+
});
|
|
530
|
+
this.default = false;
|
|
531
|
+
this.routing = app.routing;
|
|
532
|
+
}
|
|
533
|
+
static validate(name, app) {
|
|
534
|
+
validateAppPaths(name, app);
|
|
535
|
+
}
|
|
536
|
+
};
|
|
537
|
+
|
|
538
|
+
// src/config-v2/microfrontends-config/isomorphic/constants.ts
|
|
539
|
+
var DEFAULT_LOCAL_PROXY_PORT = 3024;
|
|
540
|
+
|
|
541
|
+
// src/config-v2/microfrontends-config/isomorphic/index.ts
|
|
542
|
+
var MicrofrontendConfigIsomorphic = class {
|
|
543
|
+
constructor({
|
|
544
|
+
config,
|
|
545
|
+
overrides,
|
|
546
|
+
meta
|
|
547
|
+
}) {
|
|
548
|
+
this.childApplications = {};
|
|
549
|
+
var _a, _b, _c, _d;
|
|
550
|
+
MicrofrontendConfigIsomorphic.validate(config);
|
|
551
|
+
const disableOverrides = ((_b = (_a = config.options) == null ? void 0 : _a.vercel) == null ? void 0 : _b.disableOverrides) ?? false;
|
|
552
|
+
this.overrides = overrides && !disableOverrides ? overrides : void 0;
|
|
553
|
+
this.isMainConfig = isMainConfig(config);
|
|
554
|
+
if (isMainConfig(config)) {
|
|
555
|
+
for (const [appId, appConfig] of Object.entries(config.applications)) {
|
|
556
|
+
const appOverrides = !disableOverrides ? (_c = this.overrides) == null ? void 0 : _c.applications[appId] : void 0;
|
|
557
|
+
if (isDefaultApp(appConfig)) {
|
|
558
|
+
this.defaultApplication = new DefaultApplication(appId, {
|
|
559
|
+
app: appConfig,
|
|
560
|
+
overrides: appOverrides
|
|
561
|
+
});
|
|
562
|
+
} else {
|
|
563
|
+
this.childApplications[appId] = new ChildApplication(appId, {
|
|
564
|
+
app: appConfig,
|
|
565
|
+
overrides: appOverrides
|
|
566
|
+
});
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
} else {
|
|
570
|
+
this.partOf = config.partOf;
|
|
571
|
+
const appOverrides = !disableOverrides ? (_d = this.overrides) == null ? void 0 : _d.applications[meta.fromApp] : void 0;
|
|
572
|
+
this.childApplications[meta.fromApp] = new ChildApplication(
|
|
573
|
+
meta.fromApp,
|
|
574
|
+
{
|
|
575
|
+
// we don't know routing because we're not in the main config
|
|
576
|
+
app: { routing: [] },
|
|
577
|
+
overrides: appOverrides
|
|
578
|
+
}
|
|
579
|
+
);
|
|
580
|
+
}
|
|
581
|
+
if (isMainConfig(config) && !this.defaultApplication) {
|
|
582
|
+
throw new MicrofrontendError(
|
|
583
|
+
`Could not find default application in microfrontends configuration`,
|
|
584
|
+
{
|
|
585
|
+
type: "application",
|
|
586
|
+
subtype: "not_found"
|
|
587
|
+
}
|
|
588
|
+
);
|
|
589
|
+
}
|
|
590
|
+
this.config = config;
|
|
591
|
+
this.options = config.options;
|
|
592
|
+
this.serialized = {
|
|
593
|
+
config,
|
|
594
|
+
overrides,
|
|
595
|
+
meta
|
|
596
|
+
};
|
|
597
|
+
}
|
|
598
|
+
static validate(config) {
|
|
599
|
+
const c = typeof config === "string" ? (0, import_jsonc_parser.parse)(config) : config;
|
|
600
|
+
if (isMainConfig(c)) {
|
|
601
|
+
validateConfigVersion(c.version);
|
|
602
|
+
validateConfigPaths(c.applications);
|
|
603
|
+
validateConfigDefaultApplication(c.applications);
|
|
604
|
+
}
|
|
605
|
+
return c;
|
|
606
|
+
}
|
|
607
|
+
static fromEnv({
|
|
608
|
+
meta,
|
|
609
|
+
cookies
|
|
610
|
+
}) {
|
|
611
|
+
return new MicrofrontendConfigIsomorphic({
|
|
612
|
+
config: (0, import_jsonc_parser.parse)(getConfigStringFromEnv()),
|
|
613
|
+
overrides: parseOverrides(cookies ?? []),
|
|
614
|
+
meta
|
|
615
|
+
});
|
|
616
|
+
}
|
|
617
|
+
isOverridesDisabled() {
|
|
618
|
+
var _a, _b;
|
|
619
|
+
return ((_b = (_a = this.options) == null ? void 0 : _a.vercel) == null ? void 0 : _b.disableOverrides) ?? false;
|
|
620
|
+
}
|
|
621
|
+
getConfig() {
|
|
622
|
+
return this.config;
|
|
623
|
+
}
|
|
624
|
+
getApplicationsByType() {
|
|
625
|
+
return {
|
|
626
|
+
defaultApplication: this.defaultApplication,
|
|
627
|
+
applications: Object.values(this.childApplications)
|
|
628
|
+
};
|
|
629
|
+
}
|
|
630
|
+
getChildApplications() {
|
|
631
|
+
return Object.values(this.childApplications);
|
|
632
|
+
}
|
|
633
|
+
getAllApplications() {
|
|
634
|
+
return [
|
|
635
|
+
this.defaultApplication,
|
|
636
|
+
...Object.values(this.childApplications)
|
|
637
|
+
].filter(Boolean);
|
|
638
|
+
}
|
|
639
|
+
getApplication(name) {
|
|
640
|
+
var _a;
|
|
641
|
+
if (((_a = this.defaultApplication) == null ? void 0 : _a.name) === name) {
|
|
642
|
+
return this.defaultApplication;
|
|
643
|
+
}
|
|
644
|
+
const app = this.childApplications[name];
|
|
645
|
+
if (!app) {
|
|
646
|
+
throw new MicrofrontendError(
|
|
647
|
+
`Could not find microfrontends configuration for application "${name}"`,
|
|
648
|
+
{
|
|
649
|
+
type: "application",
|
|
650
|
+
subtype: "not_found"
|
|
651
|
+
}
|
|
652
|
+
);
|
|
653
|
+
}
|
|
654
|
+
return app;
|
|
655
|
+
}
|
|
656
|
+
getApplicationByProjectId(projectId) {
|
|
657
|
+
var _a, _b;
|
|
658
|
+
if (((_b = (_a = this.defaultApplication) == null ? void 0 : _a.vercel) == null ? void 0 : _b.projectId) === projectId) {
|
|
659
|
+
return this.defaultApplication;
|
|
660
|
+
}
|
|
661
|
+
return Object.values(this.childApplications).find(
|
|
662
|
+
(app) => {
|
|
663
|
+
var _a2;
|
|
664
|
+
return ((_a2 = app.vercel) == null ? void 0 : _a2.projectId) === projectId;
|
|
665
|
+
}
|
|
666
|
+
);
|
|
667
|
+
}
|
|
668
|
+
/**
|
|
669
|
+
* Returns the default application. This can throw if the default application
|
|
670
|
+
* is undefined ( )
|
|
671
|
+
*/
|
|
672
|
+
getDefaultApplication() {
|
|
673
|
+
if (!this.defaultApplication) {
|
|
674
|
+
throw new MicrofrontendError(
|
|
675
|
+
`Could not find default application in microfrontends configuration`,
|
|
676
|
+
{
|
|
677
|
+
type: "application",
|
|
678
|
+
subtype: "not_found"
|
|
679
|
+
}
|
|
680
|
+
);
|
|
681
|
+
}
|
|
682
|
+
return this.defaultApplication;
|
|
683
|
+
}
|
|
684
|
+
/**
|
|
685
|
+
* Returns the configured port for the local proxy
|
|
686
|
+
*/
|
|
687
|
+
getLocalProxyPort() {
|
|
688
|
+
var _a, _b;
|
|
689
|
+
return ((_b = (_a = this.config.options) == null ? void 0 : _a.localProxy) == null ? void 0 : _b.port) ?? DEFAULT_LOCAL_PROXY_PORT;
|
|
690
|
+
}
|
|
691
|
+
/**
|
|
692
|
+
* Serializes the class back to the Schema type.
|
|
693
|
+
*
|
|
694
|
+
* NOTE: This is used when writing the config to disk and must always match the input Schema
|
|
695
|
+
*/
|
|
696
|
+
toSchemaJson() {
|
|
697
|
+
return this.serialized.config;
|
|
698
|
+
}
|
|
699
|
+
toClientConfig() {
|
|
700
|
+
const applications = Object.fromEntries(
|
|
701
|
+
Object.entries(this.childApplications).map(([name, application]) => [
|
|
702
|
+
name,
|
|
703
|
+
{
|
|
704
|
+
default: false,
|
|
705
|
+
routing: application.routing
|
|
706
|
+
}
|
|
707
|
+
])
|
|
708
|
+
);
|
|
709
|
+
if (this.defaultApplication) {
|
|
710
|
+
applications[this.defaultApplication.name] = {
|
|
711
|
+
default: true
|
|
712
|
+
};
|
|
713
|
+
}
|
|
714
|
+
return new MicrofrontendConfigClient({
|
|
715
|
+
applications
|
|
716
|
+
});
|
|
717
|
+
}
|
|
718
|
+
serialize() {
|
|
719
|
+
return this.serialized;
|
|
720
|
+
}
|
|
721
|
+
};
|
|
722
|
+
|
|
723
|
+
// src/routing-v2/get-domain-from-environment.ts
|
|
724
|
+
function getDomainFromEnvironment({
|
|
725
|
+
app,
|
|
726
|
+
target
|
|
727
|
+
}) {
|
|
728
|
+
var _a;
|
|
729
|
+
const mfeProjects = JSON.parse(
|
|
730
|
+
process.env.VERCEL_MICROFRONTENDS_PROJECTS ?? "[]"
|
|
731
|
+
);
|
|
732
|
+
if (mfeProjects.length === 0) {
|
|
733
|
+
throw new Error("Missing related microfrontends project information");
|
|
734
|
+
}
|
|
735
|
+
if (!((_a = app.vercel) == null ? void 0 : _a.projectId)) {
|
|
736
|
+
throw new Error(`Missing applications[${app.name}].vercel.projectId`);
|
|
737
|
+
}
|
|
738
|
+
const vercelProject = mfeProjects.find(
|
|
739
|
+
(p) => {
|
|
740
|
+
var _a2;
|
|
741
|
+
return p.project.id === ((_a2 = app.vercel) == null ? void 0 : _a2.projectId);
|
|
742
|
+
}
|
|
743
|
+
);
|
|
744
|
+
if (!vercelProject) {
|
|
745
|
+
throw new Error(
|
|
746
|
+
`Missing related microfrontends project information for application "${app.name}"`
|
|
747
|
+
);
|
|
748
|
+
}
|
|
749
|
+
const domain = target === "preview" && vercelProject.preview.branch ? vercelProject.preview.branch : vercelProject.production.alias ?? vercelProject.production.url;
|
|
750
|
+
if (!domain) {
|
|
751
|
+
throw new Error(
|
|
752
|
+
`Missing domain for target "${target}" in application "${app.name}"`
|
|
753
|
+
);
|
|
754
|
+
}
|
|
755
|
+
return domain.startsWith("https://") ? domain : `https://${domain}`;
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
// src/next-v2/testing/index.ts
|
|
759
|
+
function expandWildcards(path) {
|
|
760
|
+
if (path.includes("/:path*") || path.includes("/:slug*")) {
|
|
761
|
+
return [
|
|
762
|
+
path === "/:path*" || path === "/:slug*" ? "/" : path.replace("/:path*", "").replace("/:slug*", ""),
|
|
763
|
+
path.replace("/:path*", "/foo").replace("/:slug*", "/foo"),
|
|
764
|
+
path.replace("/:path*", "/foo/bar").replace("/:slug*", "/foo/bar")
|
|
765
|
+
];
|
|
766
|
+
}
|
|
767
|
+
if (path.includes("/:path+") || path.includes("/:slug+")) {
|
|
768
|
+
return [
|
|
769
|
+
path.replace("/:path+", "/foo").replace("/:slug+", "/foo"),
|
|
770
|
+
path.replace("/:path+", "/foo/bar").replace("/:slug+", "/foo/bar")
|
|
771
|
+
];
|
|
772
|
+
}
|
|
773
|
+
if (path.includes("/:path") || path.includes("/:slug")) {
|
|
774
|
+
return [path.replace("/:path", "/foo").replace("/:slug", "/foo")];
|
|
775
|
+
}
|
|
776
|
+
return [path];
|
|
777
|
+
}
|
|
778
|
+
function loadMicrofrontendConfigForEdge(path) {
|
|
779
|
+
const rawMfConfig = (0, import_jsonc_parser2.parse)((0, import_node_fs.readFileSync)(path, "utf-8"));
|
|
780
|
+
return new MicrofrontendConfigIsomorphic({
|
|
781
|
+
config: rawMfConfig,
|
|
782
|
+
meta: { fromApp: "test" }
|
|
783
|
+
});
|
|
784
|
+
}
|
|
785
|
+
function getAllChildApplicationNames(mfConfig) {
|
|
786
|
+
return mfConfig.getChildApplications().map((app) => app.name);
|
|
787
|
+
}
|
|
788
|
+
function getLaunchedPathsForApp(mfConfig, appName) {
|
|
789
|
+
const app = mfConfig.getApplication(appName);
|
|
790
|
+
if (app instanceof DefaultApplication) {
|
|
791
|
+
return [];
|
|
792
|
+
}
|
|
793
|
+
return [
|
|
794
|
+
`/${app.getAssetPrefix()}/_next/static`,
|
|
795
|
+
...app.routing.filter((group) => !group.flag).flatMap((group) => group.paths.flatMap(expandWildcards))
|
|
796
|
+
];
|
|
797
|
+
}
|
|
798
|
+
function getFlaggedPathsForApp(mfConfig, appName) {
|
|
799
|
+
const app = mfConfig.getApplication(appName);
|
|
800
|
+
if (app instanceof DefaultApplication) {
|
|
801
|
+
return [];
|
|
802
|
+
}
|
|
803
|
+
return app.routing.filter((group) => Boolean(group.flag)).flatMap((group) => group.paths.flatMap(expandWildcards));
|
|
804
|
+
}
|
|
805
|
+
function getExpectedDomainForApp(mfConfig, appName, env) {
|
|
806
|
+
const app = mfConfig.getApplication(appName);
|
|
807
|
+
const defaultApp = mfConfig.getDefaultApplication();
|
|
808
|
+
if (env === "development") {
|
|
809
|
+
return app.development.local.toString();
|
|
810
|
+
}
|
|
811
|
+
if (["preview", "production"].includes(env)) {
|
|
812
|
+
const target = env;
|
|
813
|
+
return getDomainFromEnvironment({ app, target });
|
|
814
|
+
}
|
|
815
|
+
return defaultApp.production.toString();
|
|
816
|
+
}
|
|
817
|
+
function getAllMultiZonesPaths(mfConfig) {
|
|
818
|
+
return mfConfig.getChildApplications().flatMap((app) => {
|
|
819
|
+
return app.routing.flatMap((group) => group.paths.flatMap(expandWildcards));
|
|
820
|
+
});
|
|
821
|
+
}
|
|
822
|
+
function urlMatches(middlewareConfig, path, host) {
|
|
823
|
+
if (!middlewareConfig.matcher) {
|
|
824
|
+
return false;
|
|
825
|
+
}
|
|
826
|
+
const matchers = Array.isArray(middlewareConfig.matcher) ? middlewareConfig.matcher : [middlewareConfig.matcher];
|
|
827
|
+
for (let matcher of matchers) {
|
|
828
|
+
matcher = typeof matcher === "string" ? { source: matcher } : matcher;
|
|
829
|
+
if ((0, import_path_to_regexp3.match)(matcher.source)(path)) {
|
|
830
|
+
if ((0, import_prepare_destination.matchHas)(
|
|
831
|
+
new import_web.WebNextRequest(
|
|
832
|
+
new import_adapter.NextRequestHint({
|
|
833
|
+
init: { headers: { host } },
|
|
834
|
+
input: `https://${host}${path}`,
|
|
835
|
+
page: "unused_placeholder"
|
|
836
|
+
})
|
|
837
|
+
),
|
|
838
|
+
{},
|
|
839
|
+
matcher.has,
|
|
840
|
+
matcher.missing
|
|
841
|
+
)) {
|
|
842
|
+
return true;
|
|
843
|
+
}
|
|
844
|
+
}
|
|
845
|
+
}
|
|
846
|
+
return false;
|
|
847
|
+
}
|
|
848
|
+
function validateMiddlewareConfig(middlewareConfig, microfrontendConfigOrPath, extraProductionMatches) {
|
|
849
|
+
const microfrontendConfig = typeof microfrontendConfigOrPath === "string" ? loadMicrofrontendConfigForEdge(microfrontendConfigOrPath) : microfrontendConfigOrPath;
|
|
850
|
+
const errors = [];
|
|
851
|
+
const usedExtraProductionMatches = /* @__PURE__ */ new Set();
|
|
852
|
+
for (const application of microfrontendConfig.getChildApplications()) {
|
|
853
|
+
const matches = [...application.routing];
|
|
854
|
+
matches.push({
|
|
855
|
+
paths: [`/${application.getAssetPrefix()}/_next/:path+`]
|
|
856
|
+
});
|
|
857
|
+
for (const aMatch of matches) {
|
|
858
|
+
const isFlagged = Boolean(aMatch.flag);
|
|
859
|
+
for (const path of aMatch.paths) {
|
|
860
|
+
const pathsToTest = expandWildcards(path);
|
|
861
|
+
for (const testPath of pathsToTest) {
|
|
862
|
+
const productionHost = microfrontendConfig.getDefaultApplication().production.host;
|
|
863
|
+
const pathForDisplay = `${testPath}${path === testPath ? "" : ` (synthesized from ${path})`}`;
|
|
864
|
+
if (!urlMatches(middlewareConfig, testPath, "test.nonproduction.host")) {
|
|
865
|
+
errors.push(
|
|
866
|
+
`Matcher misconfigured for ${pathForDisplay}. This path should have matched the middleware config on a non-production host, but did not. Microfrontends require a middleware config matcher that matches on this host everywhere but in production. That can be configured with a configuration like this:
|
|
867
|
+
${getSampleMatcher(path, productionHost)}`
|
|
868
|
+
);
|
|
869
|
+
break;
|
|
870
|
+
}
|
|
871
|
+
const productionUrlMatches = urlMatches(
|
|
872
|
+
middlewareConfig,
|
|
873
|
+
testPath,
|
|
874
|
+
productionHost
|
|
875
|
+
);
|
|
876
|
+
if (isFlagged) {
|
|
877
|
+
if (!productionUrlMatches) {
|
|
878
|
+
errors.push(
|
|
879
|
+
`Matcher misconfigured for ${pathForDisplay}. Middleware config matchers for flagged paths should ALWAYS match.`
|
|
880
|
+
);
|
|
881
|
+
break;
|
|
882
|
+
}
|
|
883
|
+
} else if (productionUrlMatches) {
|
|
884
|
+
if (extraProductionMatches == null ? void 0 : extraProductionMatches.includes(path)) {
|
|
885
|
+
usedExtraProductionMatches.add(path);
|
|
886
|
+
} else {
|
|
887
|
+
errors.push(
|
|
888
|
+
`Matcher misconfigured for ${pathForDisplay}. This path matched the middleware config on a production host, but should not have. Microfrontends require a middleware config matcher that matches on this host everywhere but in production. If this is desired, you can add the path the to extraProductionMatches parameter. Otherwise, you can set up a configuration like this:
|
|
889
|
+
${getSampleMatcher(path, productionHost)}`
|
|
890
|
+
);
|
|
891
|
+
}
|
|
892
|
+
break;
|
|
893
|
+
}
|
|
894
|
+
}
|
|
895
|
+
}
|
|
896
|
+
}
|
|
897
|
+
}
|
|
898
|
+
const unusedExtraProductionMatches = extraProductionMatches == null ? void 0 : extraProductionMatches.filter(
|
|
899
|
+
(x) => !usedExtraProductionMatches.has(x)
|
|
900
|
+
);
|
|
901
|
+
if (unusedExtraProductionMatches == null ? void 0 : unusedExtraProductionMatches.length) {
|
|
902
|
+
errors.push(
|
|
903
|
+
`The following paths were passed to the extraProductionMatches parameter but were unused. You probably want to remove them from the extraProductionMatches parameter: ${unusedExtraProductionMatches.join(", ")}`
|
|
904
|
+
);
|
|
905
|
+
}
|
|
906
|
+
if (errors.length > 0) {
|
|
907
|
+
const message = `Found the following inconsistencies between your microfrontend config ${typeof microfrontendConfigOrPath === "string" ? `(at ${microfrontendConfigOrPath}) ` : ""}and middleware config:
|
|
908
|
+
|
|
909
|
+
- `;
|
|
910
|
+
throw new Error(message + errors.join("\n\n- "));
|
|
911
|
+
}
|
|
912
|
+
}
|
|
913
|
+
function getSampleMatcher(path, host) {
|
|
914
|
+
return ` {
|
|
915
|
+
source: '${path}', // This can also be a broader regular expression.
|
|
916
|
+
missing: [
|
|
917
|
+
type: 'header',
|
|
918
|
+
key: 'host',
|
|
919
|
+
value '${host}',
|
|
920
|
+
],
|
|
921
|
+
}`;
|
|
922
|
+
}
|
|
923
|
+
async function validateMiddlewareOnFlaggedPaths(microfrontendConfigOrPath, middleware) {
|
|
924
|
+
const initialEnv = process.env.VERCEL_ENV;
|
|
925
|
+
const initialMfePreviewDomains = process.env.MFE_PREVIEW_DOMAINS;
|
|
926
|
+
try {
|
|
927
|
+
const microfrontendConfig = typeof microfrontendConfigOrPath === "string" ? loadMicrofrontendConfigForEdge(microfrontendConfigOrPath) : microfrontendConfigOrPath;
|
|
928
|
+
const allAppNames = getAllChildApplicationNames(microfrontendConfig);
|
|
929
|
+
const errors = [];
|
|
930
|
+
for (const appName of allAppNames) {
|
|
931
|
+
const flaggedPaths = getFlaggedPathsForApp(microfrontendConfig, appName);
|
|
932
|
+
if (flaggedPaths.length) {
|
|
933
|
+
for (const env of ["preview", "production"]) {
|
|
934
|
+
process.env.VERCEL_ENV = env;
|
|
935
|
+
for (const path of flaggedPaths) {
|
|
936
|
+
const expectedHost = getExpectedDomainForApp(
|
|
937
|
+
microfrontendConfig,
|
|
938
|
+
appName,
|
|
939
|
+
env
|
|
940
|
+
);
|
|
941
|
+
const expectedDefaultHost = getExpectedDomainForApp(
|
|
942
|
+
microfrontendConfig,
|
|
943
|
+
microfrontendConfig.getDefaultApplication().name,
|
|
944
|
+
env
|
|
945
|
+
);
|
|
946
|
+
const requestPath = `${expectedDefaultHost}${path}`;
|
|
947
|
+
const request = new import_server.NextRequest(requestPath, {
|
|
948
|
+
headers: { "x-vercel-skip-deployment-existence-check": "1" }
|
|
949
|
+
});
|
|
950
|
+
const response = await middleware(
|
|
951
|
+
request,
|
|
952
|
+
{}
|
|
953
|
+
);
|
|
954
|
+
const expectedUrl = `${expectedHost}${path}`;
|
|
955
|
+
if (!response) {
|
|
956
|
+
errors.push(
|
|
957
|
+
`middleware did not action for ${requestPath} in ${env}. Expected a rewrite to ${expectedUrl}`
|
|
958
|
+
);
|
|
959
|
+
} else if (response.status !== 200) {
|
|
960
|
+
errors.push(
|
|
961
|
+
`expected 200 status for ${requestPath} in ${env} but got ${response.status}`
|
|
962
|
+
);
|
|
963
|
+
} else if (response.headers.get("x-middleware-rewrite") !== expectedUrl) {
|
|
964
|
+
errors.push(
|
|
965
|
+
`expected rewrite from ${requestPath} to ${expectedUrl} in ${env}, but got ${response.headers.get("x-middleware-rewrite")}`
|
|
966
|
+
);
|
|
967
|
+
}
|
|
968
|
+
}
|
|
969
|
+
}
|
|
970
|
+
}
|
|
971
|
+
}
|
|
972
|
+
if (errors.length) {
|
|
973
|
+
throw new Error(errors.join("\n"));
|
|
974
|
+
}
|
|
975
|
+
} finally {
|
|
976
|
+
process.env.VERCEL_ENV = initialEnv;
|
|
977
|
+
process.env.MFE_PREVIEW_DOMAINS = initialMfePreviewDomains;
|
|
978
|
+
}
|
|
979
|
+
}
|
|
980
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
981
|
+
0 && (module.exports = {
|
|
982
|
+
expandWildcards,
|
|
983
|
+
getAllChildApplicationNames,
|
|
984
|
+
getAllMultiZonesPaths,
|
|
985
|
+
getExpectedDomainForApp,
|
|
986
|
+
getFlaggedPathsForApp,
|
|
987
|
+
getLaunchedPathsForApp,
|
|
988
|
+
loadMicrofrontendConfigForEdge,
|
|
989
|
+
validateMiddlewareConfig,
|
|
990
|
+
validateMiddlewareOnFlaggedPaths
|
|
991
|
+
});
|
|
992
|
+
//# sourceMappingURL=testing.cjs.map
|