@vercel/microfrontends 0.9.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 +202 -0
- package/cli/index.cjs +21 -0
- package/dist/bin/cli.cjs +1604 -0
- package/dist/config/client.cjs +54 -0
- package/dist/config/client.cjs.map +1 -0
- package/dist/config/client.d.ts +23 -0
- package/dist/config/client.js +28 -0
- package/dist/config/client.js.map +1 -0
- package/dist/config/edge.cjs +508 -0
- package/dist/config/edge.cjs.map +1 -0
- package/dist/config/edge.d.ts +20 -0
- package/dist/config/edge.js +481 -0
- package/dist/config/edge.js.map +1 -0
- package/dist/config.cjs +1254 -0
- package/dist/config.cjs.map +1 -0
- package/dist/config.d.ts +25 -0
- package/dist/config.js +1212 -0
- package/dist/config.js.map +1 -0
- package/dist/index-eff254d8.d.ts +175 -0
- package/dist/micro-frontend-config-42886104.d.ts +154 -0
- package/dist/next/client.cjs +3 -0
- package/dist/next/client.cjs.map +1 -0
- package/dist/next/client.d.ts +45 -0
- package/dist/next/client.js +3 -0
- package/dist/next/client.js.map +1 -0
- package/dist/next/config.cjs +1793 -0
- package/dist/next/config.cjs.map +1 -0
- package/dist/next/config.d.ts +22 -0
- package/dist/next/config.js +1758 -0
- package/dist/next/config.js.map +1 -0
- package/dist/next/endpoints.cjs +82 -0
- package/dist/next/endpoints.cjs.map +1 -0
- package/dist/next/endpoints.d.ts +15 -0
- package/dist/next/endpoints.js +57 -0
- package/dist/next/endpoints.js.map +1 -0
- package/dist/next/middleware.cjs +763 -0
- package/dist/next/middleware.cjs.map +1 -0
- package/dist/next/middleware.d.ts +29 -0
- package/dist/next/middleware.js +735 -0
- package/dist/next/middleware.js.map +1 -0
- package/dist/next/testing.cjs +1430 -0
- package/dist/next/testing.cjs.map +1 -0
- package/dist/next/testing.d.ts +53 -0
- package/dist/next/testing.js +1389 -0
- package/dist/next/testing.js.map +1 -0
- package/dist/overrides.cjs +143 -0
- package/dist/overrides.cjs.map +1 -0
- package/dist/overrides.d.ts +2 -0
- package/dist/overrides.js +115 -0
- package/dist/overrides.js.map +1 -0
- package/dist/schema-83a75e61.d.ts +188 -0
- package/dist/types-4fd1c7c6.d.ts +157 -0
- package/dist/v2/config.cjs +720 -0
- package/dist/v2/config.cjs.map +1 -0
- package/dist/v2/config.d.ts +2 -0
- package/dist/v2/config.js +695 -0
- package/dist/v2/config.js.map +1 -0
- package/dist/v2/microfrontends/server.cjs +1312 -0
- package/dist/v2/microfrontends/server.cjs.map +1 -0
- package/dist/v2/microfrontends/server.d.ts +48 -0
- package/dist/v2/microfrontends/server.js +1277 -0
- package/dist/v2/microfrontends/server.js.map +1 -0
- package/dist/v2/microfrontends.cjs +803 -0
- package/dist/v2/microfrontends.cjs.map +1 -0
- package/dist/v2/microfrontends.d.ts +44 -0
- package/dist/v2/microfrontends.js +776 -0
- package/dist/v2/microfrontends.js.map +1 -0
- package/dist/v2/next/client.cjs +3 -0
- package/dist/v2/next/client.cjs.map +1 -0
- package/dist/v2/next/client.d.ts +45 -0
- package/dist/v2/next/client.js +3 -0
- package/dist/v2/next/client.js.map +1 -0
- package/dist/v2/next/config.cjs +1829 -0
- package/dist/v2/next/config.cjs.map +1 -0
- package/dist/v2/next/config.d.ts +22 -0
- package/dist/v2/next/config.js +1794 -0
- package/dist/v2/next/config.js.map +1 -0
- package/dist/v2/next/endpoints.cjs +138 -0
- package/dist/v2/next/endpoints.cjs.map +1 -0
- package/dist/v2/next/endpoints.d.ts +15 -0
- package/dist/v2/next/endpoints.js +113 -0
- package/dist/v2/next/endpoints.js.map +1 -0
- package/dist/v2/next/middleware.cjs +1110 -0
- package/dist/v2/next/middleware.cjs.map +1 -0
- package/dist/v2/next/middleware.d.ts +29 -0
- package/dist/v2/next/middleware.js +1082 -0
- package/dist/v2/next/middleware.js.map +1 -0
- package/dist/v2/schema.cjs +19 -0
- package/dist/v2/schema.cjs.map +1 -0
- package/dist/v2/schema.d.ts +1 -0
- package/dist/v2/schema.js +1 -0
- package/dist/v2/schema.js.map +1 -0
- package/dist/validation.cjs +754 -0
- package/dist/validation.cjs.map +1 -0
- package/dist/validation.d.ts +163 -0
- package/dist/validation.js +726 -0
- package/dist/validation.js.map +1 -0
- package/package.json +204 -0
- package/schema/schema-v2.json +288 -0
- package/schema/schema.json +352 -0
|
@@ -0,0 +1,763 @@
|
|
|
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/middleware/index.ts
|
|
21
|
+
var middleware_exports = {};
|
|
22
|
+
__export(middleware_exports, {
|
|
23
|
+
getMicroFrontendsMiddleware: () => getMicroFrontendsMiddleware,
|
|
24
|
+
runMicroFrontendsMiddleware: () => runMicroFrontendsMiddleware
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(middleware_exports);
|
|
27
|
+
|
|
28
|
+
// src/next/middleware/middleware.ts
|
|
29
|
+
var import_server = require("next/server");
|
|
30
|
+
var import_path_to_regexp = require("path-to-regexp");
|
|
31
|
+
|
|
32
|
+
// src/config/edge/micro-frontend-config.ts
|
|
33
|
+
var import_jsonc_parser = require("jsonc-parser");
|
|
34
|
+
|
|
35
|
+
// src/config/errors.ts
|
|
36
|
+
var MicroFrontendError = class extends Error {
|
|
37
|
+
constructor(message, opts) {
|
|
38
|
+
super(message);
|
|
39
|
+
this.name = "MicroFrontendsError";
|
|
40
|
+
this.source = (opts == null ? void 0 : opts.source) ?? "@vercel/micro-frontends";
|
|
41
|
+
this.type = (opts == null ? void 0 : opts.type) ?? "unknown";
|
|
42
|
+
this.subtype = opts == null ? void 0 : opts.subtype;
|
|
43
|
+
Error.captureStackTrace(this, MicroFrontendError);
|
|
44
|
+
}
|
|
45
|
+
isKnown() {
|
|
46
|
+
return this.type !== "unknown";
|
|
47
|
+
}
|
|
48
|
+
isUnknown() {
|
|
49
|
+
return !this.isKnown();
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Converts an error to a MicroFrontendsError.
|
|
53
|
+
* @param original - The original error to convert.
|
|
54
|
+
* @returns The converted MicroFrontendsError.
|
|
55
|
+
*/
|
|
56
|
+
static convert(original, opts) {
|
|
57
|
+
if (opts == null ? void 0 : opts.fileName) {
|
|
58
|
+
const err = MicroFrontendError.convertFSError(original, opts.fileName);
|
|
59
|
+
if (err) {
|
|
60
|
+
return err;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
if (original.message.includes(
|
|
64
|
+
"Code generation from strings disallowed for this context"
|
|
65
|
+
)) {
|
|
66
|
+
return new MicroFrontendError(original.message, {
|
|
67
|
+
type: "config",
|
|
68
|
+
subtype: "unsupported_validation_env",
|
|
69
|
+
source: "ajv"
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
return new MicroFrontendError(original.message);
|
|
73
|
+
}
|
|
74
|
+
static convertFSError(original, fileName) {
|
|
75
|
+
if (original instanceof Error && "code" in original) {
|
|
76
|
+
if (original.code === "ENOENT") {
|
|
77
|
+
return new MicroFrontendError(`Could not find "${fileName}"`, {
|
|
78
|
+
type: "config",
|
|
79
|
+
subtype: "unable_to_read_file",
|
|
80
|
+
source: "fs"
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
if (original.code === "EACCES") {
|
|
84
|
+
return new MicroFrontendError(
|
|
85
|
+
`Permission denied while accessing "${fileName}"`,
|
|
86
|
+
{
|
|
87
|
+
type: "config",
|
|
88
|
+
subtype: "invalid_permissions",
|
|
89
|
+
source: "fs"
|
|
90
|
+
}
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
if (original instanceof SyntaxError) {
|
|
95
|
+
return new MicroFrontendError(
|
|
96
|
+
`Failed to parse "${fileName}": Invalid JSON format.`,
|
|
97
|
+
{
|
|
98
|
+
type: "config",
|
|
99
|
+
subtype: "invalid_syntax",
|
|
100
|
+
source: "fs"
|
|
101
|
+
}
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
return null;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Handles an unknown error and returns a MicroFrontendsError instance.
|
|
108
|
+
* @param err - The error to handle.
|
|
109
|
+
* @returns A MicroFrontendsError instance.
|
|
110
|
+
*/
|
|
111
|
+
static handle(err, opts) {
|
|
112
|
+
if (err instanceof MicroFrontendError) {
|
|
113
|
+
return err;
|
|
114
|
+
}
|
|
115
|
+
if (err instanceof Error) {
|
|
116
|
+
return MicroFrontendError.convert(err, opts);
|
|
117
|
+
}
|
|
118
|
+
if (typeof err === "object" && err !== null) {
|
|
119
|
+
if ("message" in err && typeof err.message === "string") {
|
|
120
|
+
return MicroFrontendError.convert(new Error(err.message), opts);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
return new MicroFrontendError("An unknown error occurred");
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
// src/routing/url.ts
|
|
128
|
+
function buildUrlSafeString(givenOpts = {}) {
|
|
129
|
+
const options = {
|
|
130
|
+
joinString: "-",
|
|
131
|
+
lowercaseOnly: true,
|
|
132
|
+
maxLen: 100,
|
|
133
|
+
regexRemovePattern: /(?:(?!(?:[a-z0-9])).)/gi,
|
|
134
|
+
trimWhitespace: true,
|
|
135
|
+
...givenOpts
|
|
136
|
+
};
|
|
137
|
+
return {
|
|
138
|
+
generate: (...args) => {
|
|
139
|
+
const reJoinString = new RegExp(`${options.joinString}+`, "g");
|
|
140
|
+
let tag;
|
|
141
|
+
if (args.length === 0) {
|
|
142
|
+
throw new Error("generate method must be passed at least one argument");
|
|
143
|
+
}
|
|
144
|
+
for (let i = 0; i < args.length; i++) {
|
|
145
|
+
const arg = args[i];
|
|
146
|
+
if (typeof arg !== "string")
|
|
147
|
+
throw new Error("all supplied arguments must be Strings");
|
|
148
|
+
if (options.trimWhitespace) {
|
|
149
|
+
args[i] = arg.trim();
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
tag = args.join(options.joinString);
|
|
153
|
+
tag = tag.replace(/\s/g, options.joinString);
|
|
154
|
+
if (options.lowercaseOnly)
|
|
155
|
+
tag = tag.toLowerCase();
|
|
156
|
+
tag = tag.replace(options.regexRemovePattern, (match) => {
|
|
157
|
+
if (match === options.joinString)
|
|
158
|
+
return match;
|
|
159
|
+
return "";
|
|
160
|
+
});
|
|
161
|
+
if (tag.length > options.maxLen)
|
|
162
|
+
tag = tag.substring(0, options.maxLen);
|
|
163
|
+
tag = tag.replace(reJoinString, options.joinString);
|
|
164
|
+
return tag;
|
|
165
|
+
}
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
var urlSafeString = buildUrlSafeString().generate;
|
|
169
|
+
function makeUrlSafe(name) {
|
|
170
|
+
return urlSafeString(name.replace(/\//g, "-")).replace(/^-*/g, "").replace(/-*$/g, "");
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// src/config/overrides/config.ts
|
|
174
|
+
var OVERRIDES_COOKIE_PREFIX = "vercel-micro-frontends-override";
|
|
175
|
+
var _Overrides = class {
|
|
176
|
+
constructor(config) {
|
|
177
|
+
this.config = config;
|
|
178
|
+
}
|
|
179
|
+
static getAppEnvOverrideCookieName(zone) {
|
|
180
|
+
return `${_Overrides.overrideEnvCookiePrefix}${zone}`;
|
|
181
|
+
}
|
|
182
|
+
static isOverrideCookie(cookie) {
|
|
183
|
+
var _a;
|
|
184
|
+
return Boolean((_a = cookie.name) == null ? void 0 : _a.startsWith(OVERRIDES_COOKIE_PREFIX));
|
|
185
|
+
}
|
|
186
|
+
static getOverrideFromCookie(cookie) {
|
|
187
|
+
if (!_Overrides.isOverrideCookie(cookie) || !cookie.value)
|
|
188
|
+
return;
|
|
189
|
+
return {
|
|
190
|
+
zone: cookie.name.replace(_Overrides.overrideEnvCookiePrefix, ""),
|
|
191
|
+
host: cookie.value
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
static parseOverrides(cookies) {
|
|
195
|
+
const overridesConfig = { applications: {} };
|
|
196
|
+
cookies.forEach((cookie) => {
|
|
197
|
+
const override = _Overrides.getOverrideFromCookie(cookie);
|
|
198
|
+
if (!override)
|
|
199
|
+
return;
|
|
200
|
+
overridesConfig.applications[override.zone] = {
|
|
201
|
+
environment: { host: override.host }
|
|
202
|
+
};
|
|
203
|
+
});
|
|
204
|
+
return overridesConfig;
|
|
205
|
+
}
|
|
206
|
+
static validOverrideDomainsForZone(microFrontendConfig, zone) {
|
|
207
|
+
var _a, _b, _c, _d, _e;
|
|
208
|
+
const projectName = (_a = microFrontendConfig.getZone(zone).vercel) == null ? void 0 : _a.projectName;
|
|
209
|
+
if (!projectName) {
|
|
210
|
+
return [microFrontendConfig.getZone(zone).production.host];
|
|
211
|
+
}
|
|
212
|
+
const parsedProjectName = makeUrlSafe(projectName);
|
|
213
|
+
const previewDeploymentSuffix = (_c = (_b = microFrontendConfig.options) == null ? void 0 : _b.vercel) == null ? void 0 : _c.previewDeploymentSuffix;
|
|
214
|
+
const teamSlug = (_e = (_d = microFrontendConfig.options) == null ? void 0 : _d.vercel) == null ? void 0 : _e.teamSlug;
|
|
215
|
+
if (!teamSlug && !previewDeploymentSuffix) {
|
|
216
|
+
return [microFrontendConfig.getZone(zone).production.host];
|
|
217
|
+
}
|
|
218
|
+
const suffix = previewDeploymentSuffix ? `.${previewDeploymentSuffix}` : `-${teamSlug}.vercel.app`;
|
|
219
|
+
return [
|
|
220
|
+
`${parsedProjectName}-git-([a-zA-Z0-9-]+)${suffix}`,
|
|
221
|
+
microFrontendConfig.getZone(zone).production.host
|
|
222
|
+
];
|
|
223
|
+
}
|
|
224
|
+
static validateOverrideDomain(microFrontendConfig, zone, domain) {
|
|
225
|
+
return new RegExp(
|
|
226
|
+
`^${_Overrides.validOverrideDomainsForZone(microFrontendConfig, zone).join(
|
|
227
|
+
"|"
|
|
228
|
+
)}$`
|
|
229
|
+
).test(domain);
|
|
230
|
+
}
|
|
231
|
+
serialize() {
|
|
232
|
+
return this.config;
|
|
233
|
+
}
|
|
234
|
+
};
|
|
235
|
+
var Overrides = _Overrides;
|
|
236
|
+
Overrides.overrideEnvCookiePrefix = `${OVERRIDES_COOKIE_PREFIX}:env:`;
|
|
237
|
+
|
|
238
|
+
// src/config/common/host.ts
|
|
239
|
+
var Host = class {
|
|
240
|
+
constructor({ protocol, host, port }) {
|
|
241
|
+
this.protocol = protocol || "https";
|
|
242
|
+
this.host = host;
|
|
243
|
+
this.port = Host.getPort({ port, protocol: this.protocol });
|
|
244
|
+
this.serialized = {
|
|
245
|
+
protocol,
|
|
246
|
+
host,
|
|
247
|
+
...port ? { port } : void 0
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
isLocal() {
|
|
251
|
+
return this.host === "localhost" || this.host === "127.0.0.1";
|
|
252
|
+
}
|
|
253
|
+
static getPort({
|
|
254
|
+
protocol,
|
|
255
|
+
port
|
|
256
|
+
}) {
|
|
257
|
+
if (!port) {
|
|
258
|
+
if (protocol === "http") {
|
|
259
|
+
return 80;
|
|
260
|
+
}
|
|
261
|
+
return 443;
|
|
262
|
+
}
|
|
263
|
+
return port;
|
|
264
|
+
}
|
|
265
|
+
isDefaultPort() {
|
|
266
|
+
return this.port === Host.getPort({ protocol: this.protocol });
|
|
267
|
+
}
|
|
268
|
+
toString(opts = {}) {
|
|
269
|
+
const url = this.toUrl(opts);
|
|
270
|
+
return url.toString().replace(/\/$/, "");
|
|
271
|
+
}
|
|
272
|
+
toUrl(opts = {}) {
|
|
273
|
+
const { includeDefaultPort } = opts;
|
|
274
|
+
const url = `${this.protocol}://${this.host}${this.isDefaultPort() && !includeDefaultPort ? "" : `:${this.port}`}`;
|
|
275
|
+
return new URL(url);
|
|
276
|
+
}
|
|
277
|
+
serialize() {
|
|
278
|
+
return this.serialized;
|
|
279
|
+
}
|
|
280
|
+
};
|
|
281
|
+
|
|
282
|
+
// src/config/common/application.ts
|
|
283
|
+
var Application = class {
|
|
284
|
+
constructor(name, {
|
|
285
|
+
app,
|
|
286
|
+
overrides
|
|
287
|
+
}) {
|
|
288
|
+
Application.validate(name, app);
|
|
289
|
+
this.name = name;
|
|
290
|
+
this.default = app.default;
|
|
291
|
+
this.routing = app.routing;
|
|
292
|
+
this.development = {
|
|
293
|
+
local: new Host(app.development.local),
|
|
294
|
+
fallback: app.development.fallback ? new Host(app.development.fallback) : void 0
|
|
295
|
+
};
|
|
296
|
+
this.production = new Host(app.production);
|
|
297
|
+
this.vercel = app.vercel;
|
|
298
|
+
this.overrides = (overrides == null ? void 0 : overrides.environment) ? {
|
|
299
|
+
environment: new Host(overrides.environment)
|
|
300
|
+
} : void 0;
|
|
301
|
+
}
|
|
302
|
+
isDefault() {
|
|
303
|
+
return this.default;
|
|
304
|
+
}
|
|
305
|
+
static validate(name, app) {
|
|
306
|
+
var _a, _b, _c, _d, _e;
|
|
307
|
+
if (((_b = (_a = app.routing) == null ? void 0 : _a.assetPrefix) == null ? void 0 : _b.startsWith("/")) || ((_d = (_c = app.routing) == null ? void 0 : _c.assetPrefix) == null ? void 0 : _d.endsWith("/"))) {
|
|
308
|
+
throw new MicroFrontendError(
|
|
309
|
+
`Invalid assetPrefix for application "${name}". Must not start or end with a slash.`,
|
|
310
|
+
{ type: "zone", subtype: "invalid_asset_prefix" }
|
|
311
|
+
);
|
|
312
|
+
}
|
|
313
|
+
for (const group of ((_e = app.routing) == null ? void 0 : _e.matches) ?? []) {
|
|
314
|
+
for (const p of group.paths) {
|
|
315
|
+
if (p === "/") {
|
|
316
|
+
continue;
|
|
317
|
+
}
|
|
318
|
+
if (p.endsWith("/")) {
|
|
319
|
+
throw new MicroFrontendError(
|
|
320
|
+
`Invalid path for application "${name}". ${p} must not end with a slash.`,
|
|
321
|
+
{ type: "zone", subtype: "invalid_path" }
|
|
322
|
+
);
|
|
323
|
+
}
|
|
324
|
+
if (!p.startsWith("/")) {
|
|
325
|
+
throw new MicroFrontendError(
|
|
326
|
+
`Invalid path for application "${name}". ${p} must start with a slash.`,
|
|
327
|
+
{ type: "zone", subtype: "invalid_path" }
|
|
328
|
+
);
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
serialize() {
|
|
334
|
+
var _a, _b;
|
|
335
|
+
if (this.routing === void 0 || this.default) {
|
|
336
|
+
return {
|
|
337
|
+
default: true,
|
|
338
|
+
development: {
|
|
339
|
+
local: this.development.local.serialize(),
|
|
340
|
+
fallback: (_a = this.development.fallback) == null ? void 0 : _a.serialize()
|
|
341
|
+
},
|
|
342
|
+
production: this.production.serialize(),
|
|
343
|
+
vercel: this.vercel
|
|
344
|
+
};
|
|
345
|
+
}
|
|
346
|
+
return {
|
|
347
|
+
default: false,
|
|
348
|
+
routing: this.routing,
|
|
349
|
+
development: {
|
|
350
|
+
local: this.development.local.serialize(),
|
|
351
|
+
fallback: (_b = this.development.fallback) == null ? void 0 : _b.serialize()
|
|
352
|
+
},
|
|
353
|
+
production: this.production.serialize(),
|
|
354
|
+
vercel: this.vercel
|
|
355
|
+
};
|
|
356
|
+
}
|
|
357
|
+
};
|
|
358
|
+
|
|
359
|
+
// src/config/common/micro-frontend-config.ts
|
|
360
|
+
var SUPPORTED_VERSIONS = ["1"];
|
|
361
|
+
var DEFAULT_LOCAL_PROXY_PORT = 3024;
|
|
362
|
+
var MicroFrontendConfigCommon = class {
|
|
363
|
+
constructor({
|
|
364
|
+
config,
|
|
365
|
+
overrides
|
|
366
|
+
}) {
|
|
367
|
+
this.zones = {};
|
|
368
|
+
var _a, _b, _c;
|
|
369
|
+
if (!SUPPORTED_VERSIONS.includes(config.version)) {
|
|
370
|
+
throw new MicroFrontendError(
|
|
371
|
+
`Unsupported version: ${config.version}. Supported versions are: ${SUPPORTED_VERSIONS.join(
|
|
372
|
+
", "
|
|
373
|
+
)}`,
|
|
374
|
+
{ type: "config", subtype: "unsupported_version" }
|
|
375
|
+
);
|
|
376
|
+
}
|
|
377
|
+
const disableOverrides = ((_b = (_a = config.options) == null ? void 0 : _a.vercel) == null ? void 0 : _b.disableOverrides) ?? false;
|
|
378
|
+
this.overrides = overrides && !disableOverrides ? new Overrides(overrides) : void 0;
|
|
379
|
+
for (const [zoneName, zoneConfig] of Object.entries(config.applications)) {
|
|
380
|
+
this.zones[zoneName] = new Application(zoneName, {
|
|
381
|
+
app: zoneConfig,
|
|
382
|
+
overrides: !disableOverrides ? (_c = this.overrides) == null ? void 0 : _c.config.applications[zoneName] : void 0
|
|
383
|
+
});
|
|
384
|
+
}
|
|
385
|
+
this.config = config;
|
|
386
|
+
this.name = config.name;
|
|
387
|
+
this.version = config.version;
|
|
388
|
+
this.options = config.options;
|
|
389
|
+
this.$schema = config.$schema;
|
|
390
|
+
}
|
|
391
|
+
isOverridesDisabled() {
|
|
392
|
+
var _a, _b;
|
|
393
|
+
return ((_b = (_a = this.options) == null ? void 0 : _a.vercel) == null ? void 0 : _b.disableOverrides) ?? false;
|
|
394
|
+
}
|
|
395
|
+
static getConfigFromEnv() {
|
|
396
|
+
const config = process.env.MFE_CONFIG;
|
|
397
|
+
if (!config) {
|
|
398
|
+
throw new MicroFrontendError(`Missing "MFE_CONFIG" in environment.`, {
|
|
399
|
+
type: "config",
|
|
400
|
+
subtype: "not_found_in_env"
|
|
401
|
+
});
|
|
402
|
+
}
|
|
403
|
+
return config;
|
|
404
|
+
}
|
|
405
|
+
static fromEnv(_) {
|
|
406
|
+
throw new Error("Not implemented");
|
|
407
|
+
}
|
|
408
|
+
getConfig() {
|
|
409
|
+
return this.config;
|
|
410
|
+
}
|
|
411
|
+
getAllApplications() {
|
|
412
|
+
return Object.values(this.zones);
|
|
413
|
+
}
|
|
414
|
+
getZone(name) {
|
|
415
|
+
const zone = this.zones[name];
|
|
416
|
+
if (!zone) {
|
|
417
|
+
throw new MicroFrontendError(
|
|
418
|
+
`Could not find micro-frontends configuration for application "${name}"`,
|
|
419
|
+
{
|
|
420
|
+
type: "zone",
|
|
421
|
+
subtype: "not_found"
|
|
422
|
+
}
|
|
423
|
+
);
|
|
424
|
+
}
|
|
425
|
+
return zone;
|
|
426
|
+
}
|
|
427
|
+
getApplicationByProjectId(projectId) {
|
|
428
|
+
return Object.values(this.zones).find(
|
|
429
|
+
(zone) => {
|
|
430
|
+
var _a;
|
|
431
|
+
return ((_a = zone.vercel) == null ? void 0 : _a.projectId) === projectId;
|
|
432
|
+
}
|
|
433
|
+
);
|
|
434
|
+
}
|
|
435
|
+
getDefaultZone() {
|
|
436
|
+
const zone = Object.values(this.zones).find((z) => z.default);
|
|
437
|
+
if (!zone) {
|
|
438
|
+
throw new MicroFrontendError(
|
|
439
|
+
`Could not find default zone in micro-frontends configuration`,
|
|
440
|
+
{
|
|
441
|
+
type: "zone",
|
|
442
|
+
subtype: "not_found"
|
|
443
|
+
}
|
|
444
|
+
);
|
|
445
|
+
}
|
|
446
|
+
return zone;
|
|
447
|
+
}
|
|
448
|
+
/**
|
|
449
|
+
* Returns the configured port for the local proxy
|
|
450
|
+
*/
|
|
451
|
+
getLocalProxyPort() {
|
|
452
|
+
var _a, _b;
|
|
453
|
+
return ((_b = (_a = this.config.options) == null ? void 0 : _a.localProxy) == null ? void 0 : _b.port) ?? DEFAULT_LOCAL_PROXY_PORT;
|
|
454
|
+
}
|
|
455
|
+
/**
|
|
456
|
+
* Serializes the class back to the Schema type.
|
|
457
|
+
*
|
|
458
|
+
* NOTE: This is used when writing the config to disk and must always match the input Schema
|
|
459
|
+
*/
|
|
460
|
+
toSchemaJson() {
|
|
461
|
+
const applications = {};
|
|
462
|
+
for (const [name, zone] of Object.entries(this.zones)) {
|
|
463
|
+
applications[name] = zone.serialize();
|
|
464
|
+
}
|
|
465
|
+
return {
|
|
466
|
+
$schema: this.$schema,
|
|
467
|
+
name: this.name,
|
|
468
|
+
version: this.version,
|
|
469
|
+
options: this.options,
|
|
470
|
+
applications
|
|
471
|
+
};
|
|
472
|
+
}
|
|
473
|
+
serialize() {
|
|
474
|
+
var _a;
|
|
475
|
+
const applications = {};
|
|
476
|
+
for (const [name, zone] of Object.entries(this.zones)) {
|
|
477
|
+
applications[name] = zone.serialize();
|
|
478
|
+
}
|
|
479
|
+
return {
|
|
480
|
+
config: {
|
|
481
|
+
name: this.name,
|
|
482
|
+
version: this.version,
|
|
483
|
+
applications,
|
|
484
|
+
options: this.options,
|
|
485
|
+
$schema: this.$schema
|
|
486
|
+
},
|
|
487
|
+
overrides: (_a = this.overrides) == null ? void 0 : _a.serialize()
|
|
488
|
+
};
|
|
489
|
+
}
|
|
490
|
+
write(_) {
|
|
491
|
+
throw new MicroFrontendError(
|
|
492
|
+
`Writing to file to disk requires using an instance of "MicroFrontendConfig".`,
|
|
493
|
+
{ type: "config", subtype: "unsupported_operation" }
|
|
494
|
+
);
|
|
495
|
+
}
|
|
496
|
+
};
|
|
497
|
+
|
|
498
|
+
// src/config/edge/micro-frontend-config.ts
|
|
499
|
+
var MicroFrontendConfigEdge = class extends MicroFrontendConfigCommon {
|
|
500
|
+
static fromEnv({
|
|
501
|
+
cookies
|
|
502
|
+
}) {
|
|
503
|
+
return new MicroFrontendConfigCommon({
|
|
504
|
+
config: (0, import_jsonc_parser.parse)(MicroFrontendConfigCommon.getConfigFromEnv()),
|
|
505
|
+
overrides: Overrides.parseOverrides(cookies)
|
|
506
|
+
});
|
|
507
|
+
}
|
|
508
|
+
};
|
|
509
|
+
|
|
510
|
+
// src/routing/get-domain-for-current-environment.ts
|
|
511
|
+
function debugDomains(zone, env, domain) {
|
|
512
|
+
if (process.env.MFE_DEBUG === "true") {
|
|
513
|
+
const indent = " ".repeat(4);
|
|
514
|
+
const header = "domains (zone (env) -> domain)";
|
|
515
|
+
const separator = "\u23AF".repeat(header.length);
|
|
516
|
+
const line = `${indent} 1. ${zone} (${env}) -> ${domain}`;
|
|
517
|
+
console.log(`${indent}${header}
|
|
518
|
+
${indent}${separator}
|
|
519
|
+
${line}
|
|
520
|
+
`);
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
function getDomainForCurrentEnvironment(zone) {
|
|
524
|
+
var _a;
|
|
525
|
+
if ((_a = zone.overrides) == null ? void 0 : _a.environment) {
|
|
526
|
+
return zone.overrides.environment.toString();
|
|
527
|
+
}
|
|
528
|
+
const zoneName = zone.name;
|
|
529
|
+
if (!process.env.VERCEL_ENV || process.env.VERCEL_ENV === "development") {
|
|
530
|
+
const domain = process.env.NODE_ENV === "test" ? zone.development.local.toString() : zone.production.toString();
|
|
531
|
+
debugDomains(zoneName, "development", domain);
|
|
532
|
+
return domain;
|
|
533
|
+
} else if (process.env.VERCEL_ENV === "production") {
|
|
534
|
+
const domain = zone.production.toString();
|
|
535
|
+
debugDomains(zoneName, "production", domain);
|
|
536
|
+
return domain;
|
|
537
|
+
} else if (process.env.VERCEL_ENV === "preview") {
|
|
538
|
+
const MFE_PREVIEW_DOMAINS = JSON.parse(
|
|
539
|
+
process.env.MFE_PREVIEW_DOMAINS ?? "{}"
|
|
540
|
+
);
|
|
541
|
+
if (MFE_PREVIEW_DOMAINS[zoneName]) {
|
|
542
|
+
debugDomains(zoneName, "preview", MFE_PREVIEW_DOMAINS[zoneName]);
|
|
543
|
+
return MFE_PREVIEW_DOMAINS[zoneName];
|
|
544
|
+
}
|
|
545
|
+
throw new Error(
|
|
546
|
+
`Could not find preview domain for application "${zoneName}"`
|
|
547
|
+
);
|
|
548
|
+
}
|
|
549
|
+
debugDomains(zoneName, process.env.VERCEL_ENV, zone.production.toString());
|
|
550
|
+
return zone.production.toString();
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
// src/next/middleware/middleware.ts
|
|
554
|
+
async function verifyPreviewDomain(req, rewriteDomain) {
|
|
555
|
+
var _a;
|
|
556
|
+
const jwtCookie = (_a = req.cookies.get("_vercel_jwt")) == null ? void 0 : _a.value;
|
|
557
|
+
const deploymentProtectionBypassHeader = req.headers.get(
|
|
558
|
+
"x-vercel-protection-bypass"
|
|
559
|
+
);
|
|
560
|
+
const previewResponse = await fetch(rewriteDomain, {
|
|
561
|
+
method: "HEAD",
|
|
562
|
+
headers: {
|
|
563
|
+
"x-vercel-micro-frontends-check": "1",
|
|
564
|
+
...jwtCookie ? { cookie: `_vercel_jwt=${jwtCookie}` } : {},
|
|
565
|
+
...deploymentProtectionBypassHeader ? {
|
|
566
|
+
"x-vercel-protection-bypass": deploymentProtectionBypassHeader
|
|
567
|
+
} : {}
|
|
568
|
+
}
|
|
569
|
+
});
|
|
570
|
+
const vercelError = previewResponse.headers.get("x-vercel-error");
|
|
571
|
+
return !(previewResponse.status === 404 && (vercelError === "DEPLOYMENT_NOT_FOUND" || vercelError === "NOT_FOUND"));
|
|
572
|
+
}
|
|
573
|
+
function microFrontendInternalRoutingHandler({
|
|
574
|
+
config
|
|
575
|
+
}) {
|
|
576
|
+
return async (req) => {
|
|
577
|
+
const payload = Object.fromEntries(
|
|
578
|
+
await Promise.all(
|
|
579
|
+
config.getAllApplications().map(async (application) => {
|
|
580
|
+
let rewriteDomain = getDomainForCurrentEnvironment(application);
|
|
581
|
+
const isPreviewDomainAvailable = await verifyPreviewDomain(
|
|
582
|
+
req,
|
|
583
|
+
rewriteDomain
|
|
584
|
+
);
|
|
585
|
+
if (!isPreviewDomainAvailable) {
|
|
586
|
+
rewriteDomain = application.production.toString();
|
|
587
|
+
}
|
|
588
|
+
return [
|
|
589
|
+
application.name,
|
|
590
|
+
{ routing: { host: rewriteDomain } }
|
|
591
|
+
];
|
|
592
|
+
})
|
|
593
|
+
)
|
|
594
|
+
);
|
|
595
|
+
return import_server.NextResponse.json(payload);
|
|
596
|
+
};
|
|
597
|
+
}
|
|
598
|
+
function getHandler({
|
|
599
|
+
application,
|
|
600
|
+
flagFn,
|
|
601
|
+
pattern
|
|
602
|
+
}) {
|
|
603
|
+
return async (req) => {
|
|
604
|
+
var _a, _b, _c;
|
|
605
|
+
const pathname = req.nextUrl.pathname;
|
|
606
|
+
const search = req.nextUrl.search;
|
|
607
|
+
let rewriteDomain = getDomainForCurrentEnvironment(application);
|
|
608
|
+
const patchedHeaders = new Headers(req.headers);
|
|
609
|
+
const responseCallbacks = [];
|
|
610
|
+
const onRewrite = (response) => {
|
|
611
|
+
for (const callback of responseCallbacks) {
|
|
612
|
+
callback(response);
|
|
613
|
+
}
|
|
614
|
+
return response;
|
|
615
|
+
};
|
|
616
|
+
if (process.env.VERCEL_ENV === "preview" && rewriteDomain) {
|
|
617
|
+
const zoneFallbackCookieName = `__zone_${application.name}_production_fallback`;
|
|
618
|
+
const assetPrefix = (_a = application.routing) == null ? void 0 : _a.assetPrefix;
|
|
619
|
+
if (assetPrefix && pathname.startsWith(`/${assetPrefix}`) && ((_b = req.cookies.get(zoneFallbackCookieName)) == null ? void 0 : _b.value) === "1") {
|
|
620
|
+
rewriteDomain = application.production.toString();
|
|
621
|
+
} else {
|
|
622
|
+
try {
|
|
623
|
+
let deploymentFound;
|
|
624
|
+
if (req.headers.get("x-vercel-skip-deployment-existence-check")) {
|
|
625
|
+
deploymentFound = true;
|
|
626
|
+
} else {
|
|
627
|
+
deploymentFound = await verifyPreviewDomain(req, rewriteDomain);
|
|
628
|
+
}
|
|
629
|
+
if (!deploymentFound) {
|
|
630
|
+
rewriteDomain = application.production.toString();
|
|
631
|
+
responseCallbacks.push((response) => {
|
|
632
|
+
response.cookies.set(zoneFallbackCookieName, "1", {
|
|
633
|
+
httpOnly: true,
|
|
634
|
+
sameSite: "lax",
|
|
635
|
+
secure: true,
|
|
636
|
+
maxAge: 60
|
|
637
|
+
// 1 minute
|
|
638
|
+
});
|
|
639
|
+
});
|
|
640
|
+
try {
|
|
641
|
+
const existingCookie = patchedHeaders.get("cookie");
|
|
642
|
+
if (!(existingCookie == null ? void 0 : existingCookie.includes("__vercel_toolbar"))) {
|
|
643
|
+
patchedHeaders.set(
|
|
644
|
+
"cookie",
|
|
645
|
+
[`__vercel_toolbar=1`, existingCookie].join("; ")
|
|
646
|
+
);
|
|
647
|
+
responseCallbacks.push((response) => {
|
|
648
|
+
response.cookies.set("__vercel_toolbar", "1", {
|
|
649
|
+
httpOnly: false,
|
|
650
|
+
sameSite: "lax",
|
|
651
|
+
secure: true,
|
|
652
|
+
maxAge: 29030400
|
|
653
|
+
});
|
|
654
|
+
});
|
|
655
|
+
}
|
|
656
|
+
} catch (error) {
|
|
657
|
+
}
|
|
658
|
+
} else if (((_c = req.cookies.get(zoneFallbackCookieName)) == null ? void 0 : _c.value) === "1") {
|
|
659
|
+
responseCallbacks.push((response) => {
|
|
660
|
+
response.cookies.set(zoneFallbackCookieName, "", {
|
|
661
|
+
httpOnly: true,
|
|
662
|
+
sameSite: "lax",
|
|
663
|
+
secure: true,
|
|
664
|
+
maxAge: 0
|
|
665
|
+
});
|
|
666
|
+
});
|
|
667
|
+
}
|
|
668
|
+
} catch {
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
const isFlagEnabled = async () => flagFn ? flagFn() : true;
|
|
673
|
+
if (pattern.test(pathname) && await isFlagEnabled()) {
|
|
674
|
+
return onRewrite(
|
|
675
|
+
import_server.NextResponse.rewrite(`${rewriteDomain}${pathname}${search}`, {
|
|
676
|
+
request: {
|
|
677
|
+
headers: patchedHeaders
|
|
678
|
+
}
|
|
679
|
+
})
|
|
680
|
+
);
|
|
681
|
+
}
|
|
682
|
+
};
|
|
683
|
+
}
|
|
684
|
+
function getMicroFrontendsMiddleware({
|
|
685
|
+
request,
|
|
686
|
+
flagValues
|
|
687
|
+
}) {
|
|
688
|
+
var _a, _b, _c;
|
|
689
|
+
const config = MicroFrontendConfigEdge.fromEnv({
|
|
690
|
+
cookies: request.cookies.getAll()
|
|
691
|
+
});
|
|
692
|
+
const middlewares = [];
|
|
693
|
+
middlewares.push({
|
|
694
|
+
src: "/.well-known/vercel/microfrontend-routing",
|
|
695
|
+
fn: microFrontendInternalRoutingHandler({ config })
|
|
696
|
+
});
|
|
697
|
+
for (const application of config.getAllApplications()) {
|
|
698
|
+
if (application.name === process.env.NEXT_PUBLIC_MFE_CURRENT_APPLICATION) {
|
|
699
|
+
continue;
|
|
700
|
+
}
|
|
701
|
+
if (application.routing) {
|
|
702
|
+
if (process.env.VERCEL_ENV === "preview" && application.routing.assetPrefix) {
|
|
703
|
+
const pattern = (0, import_path_to_regexp.pathToRegexp)(
|
|
704
|
+
`/${application.routing.assetPrefix}/:path+`
|
|
705
|
+
);
|
|
706
|
+
middlewares.push({
|
|
707
|
+
src: pattern,
|
|
708
|
+
fn: getHandler({
|
|
709
|
+
application,
|
|
710
|
+
pattern
|
|
711
|
+
})
|
|
712
|
+
});
|
|
713
|
+
}
|
|
714
|
+
for (const pathGroup of application.routing.matches) {
|
|
715
|
+
if (process.env.VERCEL_ENV === "preview" || ((_a = pathGroup.options) == null ? void 0 : _a.flag) || ((_b = pathGroup.options) == null ? void 0 : _b.routeToDefaultApplication)) {
|
|
716
|
+
const flagName = (_c = pathGroup.options) == null ? void 0 : _c.flag;
|
|
717
|
+
let flagFn;
|
|
718
|
+
if (flagName) {
|
|
719
|
+
flagFn = flagValues == null ? void 0 : flagValues[flagName];
|
|
720
|
+
if (!flagFn) {
|
|
721
|
+
throw new Error(
|
|
722
|
+
`Flag "${flagName}" was specified to control routing for path group "${pathGroup.group}" in application ${application.name} but not found in provided flag values.`
|
|
723
|
+
);
|
|
724
|
+
}
|
|
725
|
+
}
|
|
726
|
+
for (const path of pathGroup.paths) {
|
|
727
|
+
const pattern = (0, import_path_to_regexp.pathToRegexp)(path);
|
|
728
|
+
middlewares.push({
|
|
729
|
+
src: pattern,
|
|
730
|
+
fn: getHandler({
|
|
731
|
+
application,
|
|
732
|
+
flagFn,
|
|
733
|
+
pattern
|
|
734
|
+
})
|
|
735
|
+
});
|
|
736
|
+
}
|
|
737
|
+
}
|
|
738
|
+
}
|
|
739
|
+
}
|
|
740
|
+
}
|
|
741
|
+
return middlewares;
|
|
742
|
+
}
|
|
743
|
+
async function runMicroFrontendsMiddleware(request, flagValues) {
|
|
744
|
+
const pathname = request.nextUrl.pathname;
|
|
745
|
+
const middlewares = getMicroFrontendsMiddleware({
|
|
746
|
+
request,
|
|
747
|
+
flagValues
|
|
748
|
+
});
|
|
749
|
+
for (const mware of middlewares) {
|
|
750
|
+
if (typeof mware.src === "string" ? pathname === mware.src : mware.src.test(pathname)) {
|
|
751
|
+
const response = await mware.fn(request);
|
|
752
|
+
if (response) {
|
|
753
|
+
return response;
|
|
754
|
+
}
|
|
755
|
+
}
|
|
756
|
+
}
|
|
757
|
+
}
|
|
758
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
759
|
+
0 && (module.exports = {
|
|
760
|
+
getMicroFrontendsMiddleware,
|
|
761
|
+
runMicroFrontendsMiddleware
|
|
762
|
+
});
|
|
763
|
+
//# sourceMappingURL=middleware.cjs.map
|