@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.
Files changed (100) hide show
  1. package/README.md +202 -0
  2. package/cli/index.cjs +21 -0
  3. package/dist/bin/cli.cjs +1604 -0
  4. package/dist/config/client.cjs +54 -0
  5. package/dist/config/client.cjs.map +1 -0
  6. package/dist/config/client.d.ts +23 -0
  7. package/dist/config/client.js +28 -0
  8. package/dist/config/client.js.map +1 -0
  9. package/dist/config/edge.cjs +508 -0
  10. package/dist/config/edge.cjs.map +1 -0
  11. package/dist/config/edge.d.ts +20 -0
  12. package/dist/config/edge.js +481 -0
  13. package/dist/config/edge.js.map +1 -0
  14. package/dist/config.cjs +1254 -0
  15. package/dist/config.cjs.map +1 -0
  16. package/dist/config.d.ts +25 -0
  17. package/dist/config.js +1212 -0
  18. package/dist/config.js.map +1 -0
  19. package/dist/index-eff254d8.d.ts +175 -0
  20. package/dist/micro-frontend-config-42886104.d.ts +154 -0
  21. package/dist/next/client.cjs +3 -0
  22. package/dist/next/client.cjs.map +1 -0
  23. package/dist/next/client.d.ts +45 -0
  24. package/dist/next/client.js +3 -0
  25. package/dist/next/client.js.map +1 -0
  26. package/dist/next/config.cjs +1793 -0
  27. package/dist/next/config.cjs.map +1 -0
  28. package/dist/next/config.d.ts +22 -0
  29. package/dist/next/config.js +1758 -0
  30. package/dist/next/config.js.map +1 -0
  31. package/dist/next/endpoints.cjs +82 -0
  32. package/dist/next/endpoints.cjs.map +1 -0
  33. package/dist/next/endpoints.d.ts +15 -0
  34. package/dist/next/endpoints.js +57 -0
  35. package/dist/next/endpoints.js.map +1 -0
  36. package/dist/next/middleware.cjs +763 -0
  37. package/dist/next/middleware.cjs.map +1 -0
  38. package/dist/next/middleware.d.ts +29 -0
  39. package/dist/next/middleware.js +735 -0
  40. package/dist/next/middleware.js.map +1 -0
  41. package/dist/next/testing.cjs +1430 -0
  42. package/dist/next/testing.cjs.map +1 -0
  43. package/dist/next/testing.d.ts +53 -0
  44. package/dist/next/testing.js +1389 -0
  45. package/dist/next/testing.js.map +1 -0
  46. package/dist/overrides.cjs +143 -0
  47. package/dist/overrides.cjs.map +1 -0
  48. package/dist/overrides.d.ts +2 -0
  49. package/dist/overrides.js +115 -0
  50. package/dist/overrides.js.map +1 -0
  51. package/dist/schema-83a75e61.d.ts +188 -0
  52. package/dist/types-4fd1c7c6.d.ts +157 -0
  53. package/dist/v2/config.cjs +720 -0
  54. package/dist/v2/config.cjs.map +1 -0
  55. package/dist/v2/config.d.ts +2 -0
  56. package/dist/v2/config.js +695 -0
  57. package/dist/v2/config.js.map +1 -0
  58. package/dist/v2/microfrontends/server.cjs +1312 -0
  59. package/dist/v2/microfrontends/server.cjs.map +1 -0
  60. package/dist/v2/microfrontends/server.d.ts +48 -0
  61. package/dist/v2/microfrontends/server.js +1277 -0
  62. package/dist/v2/microfrontends/server.js.map +1 -0
  63. package/dist/v2/microfrontends.cjs +803 -0
  64. package/dist/v2/microfrontends.cjs.map +1 -0
  65. package/dist/v2/microfrontends.d.ts +44 -0
  66. package/dist/v2/microfrontends.js +776 -0
  67. package/dist/v2/microfrontends.js.map +1 -0
  68. package/dist/v2/next/client.cjs +3 -0
  69. package/dist/v2/next/client.cjs.map +1 -0
  70. package/dist/v2/next/client.d.ts +45 -0
  71. package/dist/v2/next/client.js +3 -0
  72. package/dist/v2/next/client.js.map +1 -0
  73. package/dist/v2/next/config.cjs +1829 -0
  74. package/dist/v2/next/config.cjs.map +1 -0
  75. package/dist/v2/next/config.d.ts +22 -0
  76. package/dist/v2/next/config.js +1794 -0
  77. package/dist/v2/next/config.js.map +1 -0
  78. package/dist/v2/next/endpoints.cjs +138 -0
  79. package/dist/v2/next/endpoints.cjs.map +1 -0
  80. package/dist/v2/next/endpoints.d.ts +15 -0
  81. package/dist/v2/next/endpoints.js +113 -0
  82. package/dist/v2/next/endpoints.js.map +1 -0
  83. package/dist/v2/next/middleware.cjs +1110 -0
  84. package/dist/v2/next/middleware.cjs.map +1 -0
  85. package/dist/v2/next/middleware.d.ts +29 -0
  86. package/dist/v2/next/middleware.js +1082 -0
  87. package/dist/v2/next/middleware.js.map +1 -0
  88. package/dist/v2/schema.cjs +19 -0
  89. package/dist/v2/schema.cjs.map +1 -0
  90. package/dist/v2/schema.d.ts +1 -0
  91. package/dist/v2/schema.js +1 -0
  92. package/dist/v2/schema.js.map +1 -0
  93. package/dist/validation.cjs +754 -0
  94. package/dist/validation.cjs.map +1 -0
  95. package/dist/validation.d.ts +163 -0
  96. package/dist/validation.js +726 -0
  97. package/dist/validation.js.map +1 -0
  98. package/package.json +204 -0
  99. package/schema/schema-v2.json +288 -0
  100. package/schema/schema.json +352 -0
@@ -0,0 +1,1312 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/config-v2/microfrontends/server/index.ts
31
+ var server_exports = {};
32
+ __export(server_exports, {
33
+ MicrofrontendsServer: () => MicrofrontendsServer
34
+ });
35
+ module.exports = __toCommonJS(server_exports);
36
+ var import_node_fs = __toESM(require("fs"), 1);
37
+ var import_node_path2 = require("path");
38
+
39
+ // src/config-v2/overrides/constants.ts
40
+ var OVERRIDES_COOKIE_PREFIX = "vercel-microfrontends-override";
41
+ var OVERRIDES_ENV_COOKIE_PREFIX = `${OVERRIDES_COOKIE_PREFIX}:env:`;
42
+
43
+ // src/config-v2/overrides/is-override-cookie.ts
44
+ function isOverrideCookie(cookie) {
45
+ var _a;
46
+ return Boolean((_a = cookie.name) == null ? void 0 : _a.startsWith(OVERRIDES_COOKIE_PREFIX));
47
+ }
48
+
49
+ // src/config-v2/overrides/get-override-from-cookie.ts
50
+ function getOverrideFromCookie(cookie) {
51
+ if (!isOverrideCookie(cookie) || !cookie.value)
52
+ return;
53
+ return {
54
+ application: cookie.name.replace(OVERRIDES_ENV_COOKIE_PREFIX, ""),
55
+ host: cookie.value
56
+ };
57
+ }
58
+
59
+ // src/config-v2/overrides/parse-overrides.ts
60
+ function parseOverrides(cookies) {
61
+ const overridesConfig = { applications: {} };
62
+ cookies.forEach((cookie) => {
63
+ const override = getOverrideFromCookie(cookie);
64
+ if (!override)
65
+ return;
66
+ overridesConfig.applications[override.application] = {
67
+ environment: { host: override.host }
68
+ };
69
+ });
70
+ return overridesConfig;
71
+ }
72
+
73
+ // src/config-v2/errors.ts
74
+ var MicroFrontendError = class extends Error {
75
+ constructor(message, opts) {
76
+ super(message);
77
+ this.name = "MicroFrontendsError";
78
+ this.source = (opts == null ? void 0 : opts.source) ?? "@vercel/micro-frontends";
79
+ this.type = (opts == null ? void 0 : opts.type) ?? "unknown";
80
+ this.subtype = opts == null ? void 0 : opts.subtype;
81
+ Error.captureStackTrace(this, MicroFrontendError);
82
+ }
83
+ isKnown() {
84
+ return this.type !== "unknown";
85
+ }
86
+ isUnknown() {
87
+ return !this.isKnown();
88
+ }
89
+ /**
90
+ * Converts an error to a MicroFrontendsError.
91
+ * @param original - The original error to convert.
92
+ * @returns The converted MicroFrontendsError.
93
+ */
94
+ static convert(original, opts) {
95
+ if (opts == null ? void 0 : opts.fileName) {
96
+ const err = MicroFrontendError.convertFSError(original, opts.fileName);
97
+ if (err) {
98
+ return err;
99
+ }
100
+ }
101
+ if (original.message.includes(
102
+ "Code generation from strings disallowed for this context"
103
+ )) {
104
+ return new MicroFrontendError(original.message, {
105
+ type: "config",
106
+ subtype: "unsupported_validation_env",
107
+ source: "ajv"
108
+ });
109
+ }
110
+ return new MicroFrontendError(original.message);
111
+ }
112
+ static convertFSError(original, fileName) {
113
+ if (original instanceof Error && "code" in original) {
114
+ if (original.code === "ENOENT") {
115
+ return new MicroFrontendError(`Could not find "${fileName}"`, {
116
+ type: "config",
117
+ subtype: "unable_to_read_file",
118
+ source: "fs"
119
+ });
120
+ }
121
+ if (original.code === "EACCES") {
122
+ return new MicroFrontendError(
123
+ `Permission denied while accessing "${fileName}"`,
124
+ {
125
+ type: "config",
126
+ subtype: "invalid_permissions",
127
+ source: "fs"
128
+ }
129
+ );
130
+ }
131
+ }
132
+ if (original instanceof SyntaxError) {
133
+ return new MicroFrontendError(
134
+ `Failed to parse "${fileName}": Invalid JSON format.`,
135
+ {
136
+ type: "config",
137
+ subtype: "invalid_syntax",
138
+ source: "fs"
139
+ }
140
+ );
141
+ }
142
+ return null;
143
+ }
144
+ /**
145
+ * Handles an unknown error and returns a MicroFrontendsError instance.
146
+ * @param err - The error to handle.
147
+ * @returns A MicroFrontendsError instance.
148
+ */
149
+ static handle(err, opts) {
150
+ if (err instanceof MicroFrontendError) {
151
+ return err;
152
+ }
153
+ if (err instanceof Error) {
154
+ return MicroFrontendError.convert(err, opts);
155
+ }
156
+ if (typeof err === "object" && err !== null) {
157
+ if ("message" in err && typeof err.message === "string") {
158
+ return MicroFrontendError.convert(new Error(err.message), opts);
159
+ }
160
+ }
161
+ return new MicroFrontendError("An unknown error occurred");
162
+ }
163
+ };
164
+
165
+ // src/config-v2/microfrontends-config/utils/get-config-from-env.ts
166
+ function getConfigStringFromEnv() {
167
+ const config = process.env.MFE_CONFIG;
168
+ if (!config) {
169
+ throw new MicroFrontendError(`Missing "MFE_CONFIG" in environment.`, {
170
+ type: "config",
171
+ subtype: "not_found_in_env"
172
+ });
173
+ }
174
+ return config;
175
+ }
176
+
177
+ // src/config-v2/microfrontends-config/isomorphic/index.ts
178
+ var import_jsonc_parser = require("jsonc-parser");
179
+
180
+ // src/config-v2/schema/utils/is-main-config.ts
181
+ function isMainConfig(c) {
182
+ return !("partOf" in c);
183
+ }
184
+
185
+ // src/config-v2/schema/utils/is-default-app.ts
186
+ function isDefaultApp(a) {
187
+ return !("routing" in a);
188
+ }
189
+
190
+ // src/config-v2/microfrontends-config/client/index.ts
191
+ var import_path_to_regexp = require("path-to-regexp");
192
+ var MicroFrontendConfigClient = class {
193
+ constructor(config, opts) {
194
+ this.pathCache = {};
195
+ this.serialized = config;
196
+ if (opts == null ? void 0 : opts.removeFlaggedPaths) {
197
+ for (const app of Object.values(config.applications)) {
198
+ if (app.routing) {
199
+ app.routing = app.routing.filter((match) => !match.flag);
200
+ }
201
+ }
202
+ }
203
+ this.applications = config.applications;
204
+ }
205
+ /**
206
+ * Create a new `MicroFrontendConfigClient` from a JSON string.
207
+ * Config must be passed in to remain framework agnostic
208
+ */
209
+ static fromEnv(config, opts) {
210
+ if (!config) {
211
+ throw new Error("No micro-frontends configuration found");
212
+ }
213
+ return new MicroFrontendConfigClient(
214
+ JSON.parse(config),
215
+ opts
216
+ );
217
+ }
218
+ isEqual(other) {
219
+ return JSON.stringify(this.applications) === JSON.stringify(other.applications);
220
+ }
221
+ getApplicationNameForPath(path2) {
222
+ if (!path2.startsWith("/")) {
223
+ throw new Error(`Path must start with a /`);
224
+ }
225
+ if (this.pathCache[path2]) {
226
+ return this.pathCache[path2];
227
+ }
228
+ const pathname = new URL(path2, "https://example.com").pathname;
229
+ for (const [name, application] of Object.entries(this.applications)) {
230
+ if (application.routing) {
231
+ for (const group of application.routing) {
232
+ for (const childPath of group.paths) {
233
+ const regexp = (0, import_path_to_regexp.pathToRegexp)(childPath);
234
+ if (regexp.test(pathname)) {
235
+ this.pathCache[path2] = name;
236
+ return name;
237
+ }
238
+ }
239
+ }
240
+ }
241
+ }
242
+ const defaultApplication = Object.entries(this.applications).find(
243
+ ([, application]) => application.default
244
+ );
245
+ if (!defaultApplication) {
246
+ return null;
247
+ }
248
+ this.pathCache[path2] = defaultApplication[0];
249
+ return defaultApplication[0];
250
+ }
251
+ serialize() {
252
+ return this.serialized;
253
+ }
254
+ };
255
+
256
+ // src/config-v2/microfrontends-config/isomorphic/validation.ts
257
+ var import_path_to_regexp2 = require("path-to-regexp");
258
+ var SUPPORTED_VERSIONS = ["2"];
259
+ var validateConfigVersion = (version) => {
260
+ if (!SUPPORTED_VERSIONS.includes(version)) {
261
+ throw new MicroFrontendError(
262
+ `Unsupported version: ${version}. Supported versions are: ${SUPPORTED_VERSIONS.join(
263
+ ", "
264
+ )}`,
265
+ { type: "config", subtype: "unsupported_version" }
266
+ );
267
+ }
268
+ };
269
+ var validateConfigPaths = (applicationConfigsById) => {
270
+ if (!applicationConfigsById) {
271
+ return;
272
+ }
273
+ const pathsByApplicationId = /* @__PURE__ */ new Map();
274
+ const errors = [];
275
+ for (const [id, app] of Object.entries(applicationConfigsById)) {
276
+ if (isDefaultApp(app)) {
277
+ continue;
278
+ }
279
+ for (const pathMatch of app.routing) {
280
+ for (const path2 of pathMatch.paths) {
281
+ const tokens = (0, import_path_to_regexp2.parse)(path2);
282
+ for (const token of tokens.slice(0, -1)) {
283
+ if (typeof token !== "string") {
284
+ errors.push(
285
+ `Path ${path2} may only have a :wildcard in the last path component`
286
+ );
287
+ }
288
+ }
289
+ const existing = pathsByApplicationId.get(path2);
290
+ if (existing) {
291
+ existing.applications.push(id);
292
+ } else {
293
+ pathsByApplicationId.set(path2, {
294
+ applications: [id],
295
+ matcher: (0, import_path_to_regexp2.pathToRegexp)(path2),
296
+ applicationId: id
297
+ });
298
+ }
299
+ }
300
+ }
301
+ }
302
+ const entries = Array.from(pathsByApplicationId.entries());
303
+ entries.forEach(([path2, { applications: ids, matcher, applicationId }]) => {
304
+ if (ids.length > 1) {
305
+ errors.push(
306
+ `Duplicate path "${path2}" for applications "${ids.join(", ")}"`
307
+ );
308
+ }
309
+ entries.forEach(
310
+ ([
311
+ matchPath,
312
+ { applications: matchIds, applicationId: matchApplicationId }
313
+ ]) => {
314
+ if (path2 === matchPath) {
315
+ return;
316
+ }
317
+ if (applicationId === matchApplicationId) {
318
+ return;
319
+ }
320
+ if (matcher.test(matchPath)) {
321
+ const source = `"${path2}" of application${ids.length > 0 ? "s" : ""} ${ids.join(", ")}`;
322
+ const destination = `"${matchPath}" of application${matchIds.length > 0 ? "s" : ""} ${matchIds.join(", ")}`;
323
+ errors.push(
324
+ `Overlapping path detected between ${source} and ${destination}`
325
+ );
326
+ }
327
+ }
328
+ );
329
+ });
330
+ if (errors.length) {
331
+ throw new MicroFrontendError(`Invalid paths: ${errors.join(", ")}`, {
332
+ type: "config",
333
+ subtype: "conflicting_paths"
334
+ });
335
+ }
336
+ };
337
+ var validateAppPaths = (name, app) => {
338
+ for (const group of app.routing) {
339
+ for (const p of group.paths) {
340
+ if (p === "/") {
341
+ continue;
342
+ }
343
+ if (p.endsWith("/")) {
344
+ throw new MicroFrontendError(
345
+ `Invalid path for application "${name}". ${p} must not end with a slash.`,
346
+ { type: "application", subtype: "invalid_path" }
347
+ );
348
+ }
349
+ if (!p.startsWith("/")) {
350
+ throw new MicroFrontendError(
351
+ `Invalid path for application "${name}". ${p} must start with a slash.`,
352
+ { type: "application", subtype: "invalid_path" }
353
+ );
354
+ }
355
+ }
356
+ }
357
+ };
358
+ var validateConfigDefaultApplication = (applicationConfigsById) => {
359
+ if (!applicationConfigsById) {
360
+ return;
361
+ }
362
+ const applicationsWithRouting = Object.entries(applicationConfigsById).filter(
363
+ ([, app]) => !isDefaultApp(app)
364
+ );
365
+ const applicationsWithRoutingNames = applicationsWithRouting.map(
366
+ ([key]) => key
367
+ );
368
+ const numApplications = Object.keys(applicationConfigsById).length;
369
+ const numApplicationsWithRouting = applicationsWithRoutingNames.length;
370
+ const numApplicationsWithoutRouting = numApplications - numApplicationsWithRouting;
371
+ if (numApplicationsWithoutRouting === 0) {
372
+ throw new MicroFrontendError(
373
+ `No default application found. At least one application needs to be the default by omitting routing.`,
374
+ { type: "config", subtype: "no_default_application" }
375
+ );
376
+ }
377
+ if (numApplicationsWithoutRouting > 1) {
378
+ throw new MicroFrontendError(
379
+ `Only one application can omit "routing". Found ${applicationsWithRoutingNames.length - Object.keys(applicationConfigsById).length > 1}.`,
380
+ { type: "config", subtype: "multiple_default_applications" }
381
+ );
382
+ }
383
+ };
384
+ var validateConfigOptions = (options) => {
385
+ var _a;
386
+ if ((_a = options == null ? void 0 : options.vercel) == null ? void 0 : _a.previewDeploymentSuffix) {
387
+ if (!/^[a-zA-Z]{2,}\.[a-zA-Z]{2,}$/.test(
388
+ options.vercel.previewDeploymentSuffix
389
+ )) {
390
+ throw new MicroFrontendError(
391
+ `Invalid preview deployment suffix: ${options.vercel.previewDeploymentSuffix}. Should have be formatted like "vercel.app".`,
392
+ { type: "config", subtype: "invalid_preview_deployment_suffix" }
393
+ );
394
+ }
395
+ }
396
+ };
397
+
398
+ // src/config-v2/microfrontends-config/isomorphic/utils/generate-asset-prefix.ts
399
+ var PREFIX = "vc-ap";
400
+ function generateAssetPrefixFromName({
401
+ name
402
+ }) {
403
+ if (!name) {
404
+ throw new Error("Name is required to generate an asset prefix");
405
+ }
406
+ return `${PREFIX}-${name}`;
407
+ }
408
+
409
+ // src/config-v2/microfrontends-config/isomorphic/utils/generate-port.ts
410
+ function generatePortFromName({
411
+ name,
412
+ minPort = 3e3,
413
+ maxPort = 8e3
414
+ }) {
415
+ if (!name) {
416
+ throw new Error("Name is required to generate a port");
417
+ }
418
+ let hash = 0;
419
+ for (let i = 0; i < name.length; i++) {
420
+ hash = (hash << 5) - hash + name.charCodeAt(i);
421
+ hash |= 0;
422
+ }
423
+ hash = Math.abs(hash);
424
+ const range = maxPort - minPort;
425
+ const port = minPort + hash % range;
426
+ return port;
427
+ }
428
+
429
+ // src/config-v2/microfrontends-config/isomorphic/host.ts
430
+ var Host = class {
431
+ constructor(hostConfig, options) {
432
+ const { protocol = "https", host, port } = hostConfig;
433
+ this.protocol = protocol;
434
+ this.host = host;
435
+ this.port = Host.getPort({ port, protocol: this.protocol });
436
+ this.local = options == null ? void 0 : options.isLocal;
437
+ }
438
+ isLocal() {
439
+ return this.local || this.host === "localhost" || this.host === "127.0.0.1";
440
+ }
441
+ static getPort({
442
+ protocol,
443
+ port
444
+ }) {
445
+ if (!port) {
446
+ if (protocol === "http") {
447
+ return 80;
448
+ }
449
+ return 443;
450
+ }
451
+ return port;
452
+ }
453
+ isDefaultPort() {
454
+ return this.port === Host.getPort({ protocol: this.protocol });
455
+ }
456
+ toString(opts = {}) {
457
+ const url = this.toUrl(opts);
458
+ return url.toString().replace(/\/$/, "");
459
+ }
460
+ toUrl(opts = {}) {
461
+ const { includeDefaultPort } = opts;
462
+ const url = `${this.protocol}://${this.host}${this.isDefaultPort() && !includeDefaultPort ? "" : `:${this.port}`}`;
463
+ return new URL(url);
464
+ }
465
+ };
466
+ var LocalHost = class extends Host {
467
+ constructor({
468
+ appName,
469
+ ...hostConfig
470
+ }) {
471
+ const host = hostConfig.host ?? "localhost";
472
+ const port = hostConfig.port ?? generatePortFromName({ name: appName });
473
+ const protocol = hostConfig.protocol ?? "http";
474
+ super({ protocol, host, port });
475
+ }
476
+ };
477
+
478
+ // src/config-v2/microfrontends-config/isomorphic/application.ts
479
+ var Application = class {
480
+ constructor(name, {
481
+ app,
482
+ overrides,
483
+ isDefault
484
+ }) {
485
+ var _a, _b;
486
+ this.name = name;
487
+ this.development = {
488
+ local: new LocalHost({
489
+ appName: name,
490
+ ...(_a = app.development) == null ? void 0 : _a.local
491
+ }),
492
+ fallback: ((_b = app.development) == null ? void 0 : _b.fallback) ? new Host(app.development.fallback) : void 0
493
+ };
494
+ this.production = app.production ? new Host(app.production) : void 0;
495
+ this.vercel = app.vercel;
496
+ this.overrides = (overrides == null ? void 0 : overrides.environment) ? {
497
+ environment: new Host(overrides.environment)
498
+ } : void 0;
499
+ this.default = isDefault ?? false;
500
+ this.serialized = app;
501
+ }
502
+ isDefault() {
503
+ return this.default;
504
+ }
505
+ getAssetPrefix() {
506
+ return generateAssetPrefixFromName({ name: this.name });
507
+ }
508
+ serialize() {
509
+ return this.serialized;
510
+ }
511
+ };
512
+ var DefaultApplication = class extends Application {
513
+ constructor(name, {
514
+ app,
515
+ overrides
516
+ }) {
517
+ super(name, {
518
+ app,
519
+ overrides,
520
+ isDefault: true
521
+ });
522
+ this.default = true;
523
+ this.production = new Host(app.production);
524
+ }
525
+ };
526
+ var ChildApplication = class extends Application {
527
+ constructor(name, {
528
+ app,
529
+ overrides
530
+ }) {
531
+ ChildApplication.validate(name, app);
532
+ super(name, {
533
+ app,
534
+ overrides,
535
+ isDefault: false
536
+ });
537
+ this.default = false;
538
+ this.routing = app.routing;
539
+ }
540
+ static validate(name, app) {
541
+ validateAppPaths(name, app);
542
+ }
543
+ };
544
+
545
+ // src/config-v2/microfrontends-config/isomorphic/constants.ts
546
+ var DEFAULT_LOCAL_PROXY_PORT = 3024;
547
+
548
+ // src/config-v2/microfrontends-config/isomorphic/index.ts
549
+ var MicroFrontendConfigIsomorphic = class {
550
+ constructor({
551
+ config,
552
+ overrides,
553
+ meta
554
+ }) {
555
+ this.childApplications = {};
556
+ var _a, _b, _c, _d;
557
+ MicroFrontendConfigIsomorphic.validate(config);
558
+ const disableOverrides = ((_b = (_a = config.options) == null ? void 0 : _a.vercel) == null ? void 0 : _b.disableOverrides) ?? false;
559
+ this.overrides = overrides && !disableOverrides ? overrides : void 0;
560
+ this.isMainConfig = isMainConfig(config);
561
+ if (isMainConfig(config)) {
562
+ for (const [appId, appConfig] of Object.entries(config.applications)) {
563
+ const appOverrides = !disableOverrides ? (_c = this.overrides) == null ? void 0 : _c.applications[appId] : void 0;
564
+ if (isDefaultApp(appConfig)) {
565
+ this.defaultApplication = new DefaultApplication(appId, {
566
+ app: appConfig,
567
+ overrides: appOverrides
568
+ });
569
+ } else {
570
+ this.childApplications[appId] = new ChildApplication(appId, {
571
+ app: appConfig,
572
+ overrides: appOverrides
573
+ });
574
+ }
575
+ }
576
+ } else {
577
+ this.partOf = config.partOf;
578
+ const appOverrides = !disableOverrides ? (_d = this.overrides) == null ? void 0 : _d.applications[meta.fromApp] : void 0;
579
+ this.childApplications[meta.fromApp] = new ChildApplication(
580
+ meta.fromApp,
581
+ {
582
+ // we don't know routing because we're not in the main config
583
+ app: { routing: [] },
584
+ overrides: appOverrides
585
+ }
586
+ );
587
+ }
588
+ if (isMainConfig(config) && !this.defaultApplication) {
589
+ throw new MicroFrontendError(
590
+ `Could not find default application in microfrontends configuration`,
591
+ {
592
+ type: "application",
593
+ subtype: "not_found"
594
+ }
595
+ );
596
+ }
597
+ this.config = config;
598
+ this.options = config.options;
599
+ this.serialized = {
600
+ config,
601
+ overrides,
602
+ meta
603
+ };
604
+ }
605
+ static validate(config) {
606
+ const c = typeof config === "string" ? (0, import_jsonc_parser.parse)(config) : config;
607
+ if (isMainConfig(c)) {
608
+ validateConfigVersion(c.version);
609
+ validateConfigPaths(c.applications);
610
+ validateConfigDefaultApplication(c.applications);
611
+ }
612
+ validateConfigOptions(c.options);
613
+ return c;
614
+ }
615
+ static fromEnv({
616
+ meta,
617
+ cookies
618
+ }) {
619
+ return new MicroFrontendConfigIsomorphic({
620
+ config: (0, import_jsonc_parser.parse)(getConfigStringFromEnv()),
621
+ overrides: parseOverrides(cookies ?? []),
622
+ meta
623
+ });
624
+ }
625
+ isOverridesDisabled() {
626
+ var _a, _b;
627
+ return ((_b = (_a = this.options) == null ? void 0 : _a.vercel) == null ? void 0 : _b.disableOverrides) ?? false;
628
+ }
629
+ getConfig() {
630
+ return this.config;
631
+ }
632
+ getApplicationsByType() {
633
+ return {
634
+ defaultApplication: this.defaultApplication,
635
+ applications: Object.values(this.childApplications)
636
+ };
637
+ }
638
+ getChildApplications() {
639
+ return Object.values(this.childApplications);
640
+ }
641
+ getAllApplications() {
642
+ return [
643
+ this.defaultApplication,
644
+ ...Object.values(this.childApplications)
645
+ ].filter(Boolean);
646
+ }
647
+ getApplication(name) {
648
+ var _a;
649
+ if (((_a = this.defaultApplication) == null ? void 0 : _a.name) === name) {
650
+ return this.defaultApplication;
651
+ }
652
+ const app = this.childApplications[name];
653
+ if (!app) {
654
+ throw new MicroFrontendError(
655
+ `Could not find microfrontends configuration for application "${name}"`,
656
+ {
657
+ type: "application",
658
+ subtype: "not_found"
659
+ }
660
+ );
661
+ }
662
+ return app;
663
+ }
664
+ getApplicationByProjectId(projectId) {
665
+ var _a, _b;
666
+ if (((_b = (_a = this.defaultApplication) == null ? void 0 : _a.vercel) == null ? void 0 : _b.projectId) === projectId) {
667
+ return this.defaultApplication;
668
+ }
669
+ return Object.values(this.childApplications).find(
670
+ (app) => {
671
+ var _a2;
672
+ return ((_a2 = app.vercel) == null ? void 0 : _a2.projectId) === projectId;
673
+ }
674
+ );
675
+ }
676
+ /**
677
+ * Returns the default application. This can throw if the default application
678
+ * is undefined ( )
679
+ */
680
+ getDefaultApplication() {
681
+ if (!this.defaultApplication) {
682
+ throw new MicroFrontendError(
683
+ `Could not find default application in microfrontends configuration`,
684
+ {
685
+ type: "application",
686
+ subtype: "not_found"
687
+ }
688
+ );
689
+ }
690
+ return this.defaultApplication;
691
+ }
692
+ /**
693
+ * Returns the configured port for the local proxy
694
+ */
695
+ getLocalProxyPort() {
696
+ var _a, _b;
697
+ return ((_b = (_a = this.config.options) == null ? void 0 : _a.localProxy) == null ? void 0 : _b.port) ?? DEFAULT_LOCAL_PROXY_PORT;
698
+ }
699
+ /**
700
+ * Serializes the class back to the Schema type.
701
+ *
702
+ * NOTE: This is used when writing the config to disk and must always match the input Schema
703
+ */
704
+ toSchemaJson() {
705
+ return this.serialized.config;
706
+ }
707
+ toClientConfig() {
708
+ const applications = Object.fromEntries(
709
+ Object.entries(this.childApplications).map(([name, application]) => [
710
+ name,
711
+ {
712
+ default: false,
713
+ routing: application.routing
714
+ }
715
+ ])
716
+ );
717
+ if (this.defaultApplication) {
718
+ applications[this.defaultApplication.name] = {
719
+ default: true
720
+ };
721
+ }
722
+ return new MicroFrontendConfigClient({
723
+ applications
724
+ });
725
+ }
726
+ serialize() {
727
+ return this.serialized;
728
+ }
729
+ };
730
+
731
+ // src/config-v2/microfrontends-config/isomorphic/child.ts
732
+ var MicroFrontendChildConfig = class extends MicroFrontendConfigIsomorphic {
733
+ constructor({
734
+ config,
735
+ overrides,
736
+ meta
737
+ }) {
738
+ super({ config, overrides, meta });
739
+ this.isMainConfig = false;
740
+ this.partOf = config.partOf;
741
+ }
742
+ };
743
+
744
+ // src/config-v2/microfrontends-config/isomorphic/main.ts
745
+ var MicroFrontendMainConfig = class extends MicroFrontendConfigIsomorphic {
746
+ constructor({
747
+ config,
748
+ overrides,
749
+ meta
750
+ }) {
751
+ var _a, _b, _c;
752
+ super({ config, overrides, meta });
753
+ this.isMainConfig = true;
754
+ this.provider = config.provider;
755
+ const disableOverrides = ((_b = (_a = config.options) == null ? void 0 : _a.vercel) == null ? void 0 : _b.disableOverrides) ?? false;
756
+ let defaultApplication;
757
+ for (const [appId, appConfig] of Object.entries(config.applications)) {
758
+ const appOverrides = !disableOverrides ? (_c = this.overrides) == null ? void 0 : _c.applications[appId] : void 0;
759
+ if (isDefaultApp(appConfig)) {
760
+ defaultApplication = new DefaultApplication(appId, {
761
+ app: appConfig,
762
+ overrides: appOverrides
763
+ });
764
+ } else {
765
+ this.childApplications[appId] = new ChildApplication(appId, {
766
+ app: appConfig,
767
+ overrides: appOverrides
768
+ });
769
+ }
770
+ }
771
+ if (!defaultApplication) {
772
+ throw new MicroFrontendError(
773
+ `Could not find default application in microfrontends configuration`,
774
+ {
775
+ type: "application",
776
+ subtype: "not_found"
777
+ }
778
+ );
779
+ }
780
+ this.defaultApplication = defaultApplication;
781
+ }
782
+ };
783
+
784
+ // src/config-v2/microfrontends/isomorphic/index.ts
785
+ var Microfrontends = class {
786
+ constructor({
787
+ config,
788
+ overrides,
789
+ meta
790
+ }) {
791
+ if (isMainConfig(config)) {
792
+ this.config = new MicroFrontendMainConfig({ config, overrides, meta });
793
+ } else {
794
+ this.config = new MicroFrontendChildConfig({ config, overrides, meta });
795
+ }
796
+ }
797
+ isChildConfig() {
798
+ return this.config instanceof MicroFrontendChildConfig;
799
+ }
800
+ static fromEnv({
801
+ cookies,
802
+ meta
803
+ }) {
804
+ const config = MicroFrontendConfigIsomorphic.fromEnv({
805
+ cookies,
806
+ meta
807
+ });
808
+ return new Microfrontends(config.serialize());
809
+ }
810
+ };
811
+
812
+ // src/config-v2/microfrontends/server/utils/get-output-file-path.ts
813
+ var import_node_path = __toESM(require("path"), 1);
814
+
815
+ // src/config-v2/microfrontends/server/constants.ts
816
+ var MFE_CONFIG_DEFAULT_FILE_PATH = "microfrontends";
817
+ var MFE_CONFIG_DEFAULT_FILE_NAME = "microfrontends.json";
818
+
819
+ // src/utils/is-vercel.ts
820
+ function isVercel() {
821
+ return process.env.VERCEL === "1";
822
+ }
823
+
824
+ // src/config-v2/microfrontends/server/utils/get-output-file-path.ts
825
+ function getOutputFilePath() {
826
+ if (isVercel()) {
827
+ return import_node_path.default.join(
828
+ ".vercel",
829
+ MFE_CONFIG_DEFAULT_FILE_PATH,
830
+ MFE_CONFIG_DEFAULT_FILE_NAME
831
+ );
832
+ }
833
+ return import_node_path.default.join(MFE_CONFIG_DEFAULT_FILE_PATH, MFE_CONFIG_DEFAULT_FILE_NAME);
834
+ }
835
+
836
+ // src/config-v2/microfrontends/server/validation.ts
837
+ var import_jsonc_parser2 = require("jsonc-parser");
838
+ var import_ajv = require("ajv");
839
+
840
+ // src/config/errors.ts
841
+ var MicroFrontendError2 = class extends Error {
842
+ constructor(message, opts) {
843
+ super(message);
844
+ this.name = "MicroFrontendsError";
845
+ this.source = (opts == null ? void 0 : opts.source) ?? "@vercel/micro-frontends";
846
+ this.type = (opts == null ? void 0 : opts.type) ?? "unknown";
847
+ this.subtype = opts == null ? void 0 : opts.subtype;
848
+ Error.captureStackTrace(this, MicroFrontendError2);
849
+ }
850
+ isKnown() {
851
+ return this.type !== "unknown";
852
+ }
853
+ isUnknown() {
854
+ return !this.isKnown();
855
+ }
856
+ /**
857
+ * Converts an error to a MicroFrontendsError.
858
+ * @param original - The original error to convert.
859
+ * @returns The converted MicroFrontendsError.
860
+ */
861
+ static convert(original, opts) {
862
+ if (opts == null ? void 0 : opts.fileName) {
863
+ const err = MicroFrontendError2.convertFSError(original, opts.fileName);
864
+ if (err) {
865
+ return err;
866
+ }
867
+ }
868
+ if (original.message.includes(
869
+ "Code generation from strings disallowed for this context"
870
+ )) {
871
+ return new MicroFrontendError2(original.message, {
872
+ type: "config",
873
+ subtype: "unsupported_validation_env",
874
+ source: "ajv"
875
+ });
876
+ }
877
+ return new MicroFrontendError2(original.message);
878
+ }
879
+ static convertFSError(original, fileName) {
880
+ if (original instanceof Error && "code" in original) {
881
+ if (original.code === "ENOENT") {
882
+ return new MicroFrontendError2(`Could not find "${fileName}"`, {
883
+ type: "config",
884
+ subtype: "unable_to_read_file",
885
+ source: "fs"
886
+ });
887
+ }
888
+ if (original.code === "EACCES") {
889
+ return new MicroFrontendError2(
890
+ `Permission denied while accessing "${fileName}"`,
891
+ {
892
+ type: "config",
893
+ subtype: "invalid_permissions",
894
+ source: "fs"
895
+ }
896
+ );
897
+ }
898
+ }
899
+ if (original instanceof SyntaxError) {
900
+ return new MicroFrontendError2(
901
+ `Failed to parse "${fileName}": Invalid JSON format.`,
902
+ {
903
+ type: "config",
904
+ subtype: "invalid_syntax",
905
+ source: "fs"
906
+ }
907
+ );
908
+ }
909
+ return null;
910
+ }
911
+ /**
912
+ * Handles an unknown error and returns a MicroFrontendsError instance.
913
+ * @param err - The error to handle.
914
+ * @returns A MicroFrontendsError instance.
915
+ */
916
+ static handle(err, opts) {
917
+ if (err instanceof MicroFrontendError2) {
918
+ return err;
919
+ }
920
+ if (err instanceof Error) {
921
+ return MicroFrontendError2.convert(err, opts);
922
+ }
923
+ if (typeof err === "object" && err !== null) {
924
+ if ("message" in err && typeof err.message === "string") {
925
+ return MicroFrontendError2.convert(new Error(err.message), opts);
926
+ }
927
+ }
928
+ return new MicroFrontendError2("An unknown error occurred");
929
+ }
930
+ };
931
+
932
+ // schema/schema-v2.json
933
+ var schema_v2_default = {
934
+ $schema: "http://json-schema.org/draft-07/schema#",
935
+ $ref: "#/definitions/Config",
936
+ definitions: {
937
+ Config: {
938
+ anyOf: [
939
+ {
940
+ $ref: "#/definitions/MainConfig"
941
+ },
942
+ {
943
+ $ref: "#/definitions/ChildConfig"
944
+ }
945
+ ]
946
+ },
947
+ MainConfig: {
948
+ type: "object",
949
+ properties: {
950
+ $schema: {
951
+ type: "string"
952
+ },
953
+ version: {
954
+ type: "string",
955
+ const: "2"
956
+ },
957
+ options: {
958
+ $ref: "#/definitions/Options"
959
+ },
960
+ remotes: {
961
+ type: "object",
962
+ additionalProperties: {
963
+ $ref: "#/definitions/Application"
964
+ },
965
+ description: "Applications that only serve a subset of the microfrontend routes only need to reference the name of the primary application that owns the full microfrontends configuration."
966
+ },
967
+ provider: {
968
+ $ref: "#/definitions/Provider"
969
+ },
970
+ applications: {
971
+ $ref: "#/definitions/ApplicationRouting",
972
+ description: "Mapping of application names to the routes that they host. Only needs to be defined in the application that owns the primary microfrontend domain"
973
+ }
974
+ },
975
+ required: ["applications", "provider", "version"]
976
+ },
977
+ Options: {
978
+ type: "object",
979
+ properties: {
980
+ vercel: {
981
+ $ref: "#/definitions/VercelOptions",
982
+ description: "Micro-Frontends wide options for Vercel."
983
+ },
984
+ localProxy: {
985
+ $ref: "#/definitions/LocalProxyOptions",
986
+ description: "Options for local proxy."
987
+ }
988
+ }
989
+ },
990
+ VercelOptions: {
991
+ type: "object",
992
+ properties: {
993
+ previewDeploymentSuffix: {
994
+ type: "string",
995
+ description: "If your team uses a custom Preview Deployment Suffix, please specify it here. See https://vercel.com/docs/deployments/preview-deployment-suffix. The default is `vercel.app`."
996
+ },
997
+ teamSlug: {
998
+ type: "string",
999
+ description: "Team slug for the Vercel team"
1000
+ },
1001
+ disableOverrides: {
1002
+ type: "boolean",
1003
+ description: "If you want to disable the overrides for the site. For example, if you are managing rewrites between applications externally, you may wish to disable the overrides on the toolbar as they will have no effect."
1004
+ }
1005
+ }
1006
+ },
1007
+ LocalProxyOptions: {
1008
+ type: "object",
1009
+ properties: {
1010
+ port: {
1011
+ type: "number",
1012
+ description: "The port number used by the local proxy server.\n\nThe default is `3024`."
1013
+ }
1014
+ }
1015
+ },
1016
+ Application: {
1017
+ anyOf: [
1018
+ {
1019
+ $ref: "#/definitions/DefaultApplication"
1020
+ },
1021
+ {
1022
+ $ref: "#/definitions/ChildApplication"
1023
+ }
1024
+ ]
1025
+ },
1026
+ DefaultApplication: {
1027
+ type: "object",
1028
+ properties: {
1029
+ vercel: {
1030
+ $ref: "#/definitions/Vercel"
1031
+ },
1032
+ development: {
1033
+ $ref: "#/definitions/Development"
1034
+ },
1035
+ production: {
1036
+ $ref: "#/definitions/HostConfig"
1037
+ }
1038
+ },
1039
+ required: ["production"]
1040
+ },
1041
+ Vercel: {
1042
+ type: "object",
1043
+ properties: {
1044
+ projectId: {
1045
+ type: "string",
1046
+ description: "Vercel project ID"
1047
+ },
1048
+ routeSpeedInsightsToDefaultZone: {
1049
+ type: "boolean"
1050
+ }
1051
+ },
1052
+ required: ["projectId"]
1053
+ },
1054
+ Development: {
1055
+ type: "object",
1056
+ properties: {
1057
+ local: {
1058
+ $ref: "#/definitions/LocalHostConfig"
1059
+ },
1060
+ fallback: {
1061
+ $ref: "#/definitions/HostConfig",
1062
+ description: "Fallback for local development, could be a host config that points to any environment. If this is not provided, or the application is not running - requests to the application in local development will error."
1063
+ },
1064
+ task: {
1065
+ type: "string",
1066
+ description: "Optional task to run when starting the development server. Should reference a script in the package.json of the application."
1067
+ }
1068
+ }
1069
+ },
1070
+ LocalHostConfig: {
1071
+ type: "object",
1072
+ properties: {
1073
+ host: {
1074
+ type: "string",
1075
+ description: "The hostname or IP address of the server. This can be a domain name (e.g., `example.com`) or an IP address (e.g., `192.168.1.1`)."
1076
+ },
1077
+ protocol: {
1078
+ type: "string",
1079
+ enum: ["http", "https"],
1080
+ description: 'The protocol to be used for the connection.\n- `http`: Hypertext Transfer Protocol (HTTP).\n- `https`: Secure Hypertext Transfer Protocol (HTTPS).\n\n* @defaultValue "http" for local development, "https" for otherwise'
1081
+ },
1082
+ port: {
1083
+ type: "number",
1084
+ description: "The port number to be used for the connection. Common values include `80` for HTTP and `443` for HTTPS."
1085
+ }
1086
+ }
1087
+ },
1088
+ HostConfig: {
1089
+ type: "object",
1090
+ properties: {
1091
+ protocol: {
1092
+ type: "string",
1093
+ enum: ["http", "https"],
1094
+ description: 'The protocol to be used for the connection.\n- `http`: Hypertext Transfer Protocol (HTTP).\n- `https`: Secure Hypertext Transfer Protocol (HTTPS).\n\n* @defaultValue "http" for local development, "https" for otherwise'
1095
+ },
1096
+ host: {
1097
+ type: "string",
1098
+ description: "The hostname or IP address of the server. This can be a domain name (e.g., `example.com`) or an IP address (e.g., `192.168.1.1`)."
1099
+ },
1100
+ port: {
1101
+ type: "number",
1102
+ description: "The port number to be used for the connection. Common values include `80` for HTTP and `443` for HTTPS."
1103
+ }
1104
+ },
1105
+ required: ["host"]
1106
+ },
1107
+ ChildApplication: {
1108
+ type: "object",
1109
+ properties: {
1110
+ vercel: {
1111
+ $ref: "#/definitions/Vercel"
1112
+ },
1113
+ development: {
1114
+ $ref: "#/definitions/Development"
1115
+ },
1116
+ routing: {
1117
+ $ref: "#/definitions/Routing",
1118
+ description: "Groups of path expressions that are routed to this application."
1119
+ },
1120
+ production: {
1121
+ $ref: "#/definitions/HostConfig"
1122
+ }
1123
+ },
1124
+ required: ["routing"]
1125
+ },
1126
+ Routing: {
1127
+ type: "array",
1128
+ items: {
1129
+ $ref: "#/definitions/PathGroup"
1130
+ }
1131
+ },
1132
+ PathGroup: {
1133
+ type: "object",
1134
+ properties: {
1135
+ group: {
1136
+ type: "string",
1137
+ description: "Optional group name for the paths"
1138
+ },
1139
+ flag: {
1140
+ type: "string",
1141
+ description: "flag name that can be used to enable/disable all paths in the group"
1142
+ },
1143
+ routeToDefaultApplication: {
1144
+ type: "boolean",
1145
+ description: "True to route the request to the default application for this micro-frontends set-up. This must be `true` when using `flag` or when you want to use custom logic to make the routing decision for this group of paths."
1146
+ },
1147
+ paths: {
1148
+ type: "array",
1149
+ items: {
1150
+ type: "string"
1151
+ }
1152
+ }
1153
+ },
1154
+ required: ["paths"]
1155
+ },
1156
+ Provider: {
1157
+ type: "string",
1158
+ enum: ["vercel", "other"]
1159
+ },
1160
+ ApplicationRouting: {
1161
+ type: "object",
1162
+ additionalProperties: {
1163
+ $ref: "#/definitions/Application"
1164
+ }
1165
+ },
1166
+ ChildConfig: {
1167
+ type: "object",
1168
+ properties: {
1169
+ $schema: {
1170
+ type: "string"
1171
+ },
1172
+ version: {
1173
+ type: "string",
1174
+ const: "2"
1175
+ },
1176
+ options: {
1177
+ $ref: "#/definitions/Options"
1178
+ },
1179
+ remotes: {
1180
+ type: "object",
1181
+ additionalProperties: {
1182
+ $ref: "#/definitions/Application"
1183
+ },
1184
+ description: "Applications that only serve a subset of the microfrontend routes only need to reference the name of the primary application that owns the full microfrontends configuration."
1185
+ },
1186
+ partOf: {
1187
+ type: "string",
1188
+ description: "Applications that only serve a subset of the microfrontend routes only need to reference the name of the primary application that owns the full microfrontends configuration."
1189
+ }
1190
+ },
1191
+ required: ["partOf", "version"]
1192
+ }
1193
+ }
1194
+ };
1195
+
1196
+ // src/config-v2/schema/utils/load.ts
1197
+ var SCHEMA = schema_v2_default;
1198
+
1199
+ // src/config-v2/microfrontends/server/validation.ts
1200
+ function validateSchema(configString) {
1201
+ const parsedConfig = (0, import_jsonc_parser2.parse)(configString);
1202
+ const ajv = new import_ajv.Ajv();
1203
+ const validate = ajv.compile(SCHEMA);
1204
+ const isValid = validate(parsedConfig);
1205
+ if (!isValid) {
1206
+ throw new MicroFrontendError2(
1207
+ `Invalid config: ${ajv.errorsText(validate.errors)}`,
1208
+ { type: "config", subtype: "does_not_match_schema" }
1209
+ );
1210
+ }
1211
+ return parsedConfig;
1212
+ }
1213
+
1214
+ // src/config-v2/microfrontends/server/index.ts
1215
+ var MicrofrontendsServer = class extends Microfrontends {
1216
+ /**
1217
+ * Writes the configuration to a file.
1218
+ */
1219
+ writeConfig(opts = {
1220
+ pretty: true
1221
+ }) {
1222
+ const outputPath = getOutputFilePath();
1223
+ import_node_fs.default.mkdirSync((0, import_node_path2.dirname)(outputPath), { recursive: true });
1224
+ import_node_fs.default.writeFileSync(
1225
+ outputPath,
1226
+ JSON.stringify(
1227
+ this.config.toSchemaJson(),
1228
+ null,
1229
+ opts.pretty ?? true ? 2 : void 0
1230
+ )
1231
+ );
1232
+ }
1233
+ // --------- Static Methods ---------
1234
+ /**
1235
+ * Generates a MicrofrontendsServer instance from an unknown object.
1236
+ */
1237
+ static fromUnknown({
1238
+ config,
1239
+ cookies,
1240
+ meta
1241
+ }) {
1242
+ const overrides = cookies ? parseOverrides(cookies) : void 0;
1243
+ if (typeof config === "string") {
1244
+ return new MicrofrontendsServer({
1245
+ config: MicrofrontendsServer.validate(config),
1246
+ overrides,
1247
+ meta
1248
+ });
1249
+ }
1250
+ if (typeof config === "object") {
1251
+ return new MicrofrontendsServer({
1252
+ config,
1253
+ overrides,
1254
+ meta
1255
+ });
1256
+ }
1257
+ throw new MicroFrontendError(
1258
+ "Invalid config: must be a string or an object",
1259
+ { type: "config", subtype: "does_not_match_schema" }
1260
+ );
1261
+ }
1262
+ /**
1263
+ * Generates a MicrofrontendsServer instance from the environment.
1264
+ * Uses additional validation that is only available when in a node runtime
1265
+ */
1266
+ static fromEnv({
1267
+ cookies,
1268
+ meta
1269
+ }) {
1270
+ return new MicrofrontendsServer({
1271
+ config: MicrofrontendsServer.validate(getConfigStringFromEnv()),
1272
+ overrides: parseOverrides(cookies),
1273
+ meta
1274
+ });
1275
+ }
1276
+ /**
1277
+ * Validates the configuration against the JSON schema
1278
+ */
1279
+ static validate(config) {
1280
+ if (typeof config === "string") {
1281
+ const c = validateSchema(config);
1282
+ return c;
1283
+ }
1284
+ return config;
1285
+ }
1286
+ /*
1287
+ * Generates a MicrofrontendsServer instance from a file.
1288
+ */
1289
+ static fromFile({
1290
+ filePath,
1291
+ cookies,
1292
+ meta
1293
+ }) {
1294
+ try {
1295
+ const config = import_node_fs.default.readFileSync(filePath, "utf-8");
1296
+ return new MicrofrontendsServer({
1297
+ config: MicrofrontendsServer.validate(config),
1298
+ overrides: cookies ? parseOverrides(cookies) : void 0,
1299
+ meta
1300
+ });
1301
+ } catch (e) {
1302
+ throw MicroFrontendError.handle(e, {
1303
+ fileName: filePath
1304
+ });
1305
+ }
1306
+ }
1307
+ };
1308
+ // Annotate the CommonJS export names for ESM import in node:
1309
+ 0 && (module.exports = {
1310
+ MicrofrontendsServer
1311
+ });
1312
+ //# sourceMappingURL=server.cjs.map