@module-federation/dts-plugin 2.0.0 → 2.1.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/dist/Action-CzhPMw2i.js +153 -0
- package/dist/Broker-DRlzScTT.js +800 -0
- package/dist/CHANGELOG.md +26 -0
- package/dist/DtsWorker-Dtem3-FM.d.ts +166 -0
- package/dist/constant-BwEkyidO.d.ts +42 -0
- package/dist/consumeTypes-AD2ig87l.js +237 -0
- package/dist/core.d.ts +3 -68
- package/dist/core.js +26 -2380
- package/dist/dynamic-remote-type-hints-plugin.d.ts +622 -3
- package/dist/dynamic-remote-type-hints-plugin.js +65 -187
- package/dist/esm/Action-DNNg2YDh.mjs +47 -0
- package/dist/esm/Broker-BU4gToNr.mjs +736 -0
- package/dist/esm/consumeTypes-D51rVbSt.mjs +204 -0
- package/dist/esm/core.mjs +5 -0
- package/dist/esm/dynamic-remote-type-hints-plugin.mjs +73 -0
- package/dist/esm/expose-rpc-DMhY1i8A.mjs +1301 -0
- package/dist/esm/fork-dev-worker.mjs +103 -0
- package/dist/esm/fork-generate-dts.mjs +14 -0
- package/dist/esm/index.mjs +465 -0
- package/dist/esm/start-broker.mjs +22 -0
- package/dist/esm/utils-CkPvDGOy.mjs +13 -0
- package/dist/expose-rpc-BLAH20uj.js +1415 -0
- package/dist/fork-dev-worker.d.ts +10 -9
- package/dist/fork-dev-worker.js +100 -2869
- package/dist/fork-generate-dts.d.ts +4 -8
- package/dist/fork-generate-dts.js +11 -2040
- package/dist/iife/launch-web-client.iife.js +117 -0
- package/dist/index.d.ts +57 -46
- package/dist/index.js +449 -2980
- package/dist/package.json +27 -12
- package/dist/start-broker.d.ts +41 -39
- package/dist/start-broker.js +17 -952
- package/dist/utils-7KqCZHbb.js +19 -0
- package/package.json +32 -17
- package/dist/DTSManager-b15Gfat3.d.ts +0 -53
- package/dist/DTSManagerOptions-QVchWb0x.d.ts +0 -32
- package/dist/DtsWorker-BrHsGz8C.d.ts +0 -56
- package/dist/core.d.mts +0 -68
- package/dist/dynamic-remote-type-hints-plugin.d.mts +0 -5
- package/dist/esm/chunk-647HGGGS.js +0 -241
- package/dist/esm/chunk-G65LOFTY.js +0 -24
- package/dist/esm/chunk-N7GTIQUA.js +0 -282
- package/dist/esm/chunk-RWXNVNFM.js +0 -1579
- package/dist/esm/chunk-WWV5RWOP.js +0 -902
- package/dist/esm/core.js +0 -44
- package/dist/esm/dynamic-remote-type-hints-plugin.js +0 -73
- package/dist/esm/fork-dev-worker.js +0 -145
- package/dist/esm/fork-generate-dts.js +0 -27
- package/dist/esm/index.js +0 -646
- package/dist/esm/start-broker.js +0 -36
- package/dist/fork-dev-worker.d.mts +0 -15
- package/dist/fork-generate-dts.d.mts +0 -10
- package/dist/iife/launch-web-client.js +0 -152
- package/dist/index.d.mts +0 -56
- package/dist/start-broker.d.mts +0 -42
- package/dist/utils-C4sQemLR.d.ts +0 -15
package/dist/fork-dev-worker.js
CHANGED
|
@@ -1,2873 +1,104 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
var __spreadValues = (a, b) => {
|
|
13
|
-
for (var prop in b || (b = {}))
|
|
14
|
-
if (__hasOwnProp.call(b, prop))
|
|
15
|
-
__defNormalProp(a, prop, b[prop]);
|
|
16
|
-
if (__getOwnPropSymbols)
|
|
17
|
-
for (var prop of __getOwnPropSymbols(b)) {
|
|
18
|
-
if (__propIsEnum.call(b, prop))
|
|
19
|
-
__defNormalProp(a, prop, b[prop]);
|
|
20
|
-
}
|
|
21
|
-
return a;
|
|
22
|
-
};
|
|
23
|
-
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
24
|
-
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
25
|
-
var __objRest = (source, exclude) => {
|
|
26
|
-
var target = {};
|
|
27
|
-
for (var prop in source)
|
|
28
|
-
if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
|
|
29
|
-
target[prop] = source[prop];
|
|
30
|
-
if (source != null && __getOwnPropSymbols)
|
|
31
|
-
for (var prop of __getOwnPropSymbols(source)) {
|
|
32
|
-
if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
|
|
33
|
-
target[prop] = source[prop];
|
|
34
|
-
}
|
|
35
|
-
return target;
|
|
36
|
-
};
|
|
37
|
-
var __export = (target, all) => {
|
|
38
|
-
for (var name in all)
|
|
39
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
40
|
-
};
|
|
41
|
-
var __copyProps = (to, from, except, desc) => {
|
|
42
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
43
|
-
for (let key of __getOwnPropNames(from))
|
|
44
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
45
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
46
|
-
}
|
|
47
|
-
return to;
|
|
48
|
-
};
|
|
49
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
50
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
51
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
52
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
53
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
54
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
55
|
-
mod
|
|
56
|
-
));
|
|
57
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
58
|
-
var __publicField = (obj, key, value) => {
|
|
59
|
-
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
60
|
-
return value;
|
|
61
|
-
};
|
|
62
|
-
var __async = (__this, __arguments, generator) => {
|
|
63
|
-
return new Promise((resolve4, reject) => {
|
|
64
|
-
var fulfilled = (value) => {
|
|
65
|
-
try {
|
|
66
|
-
step(generator.next(value));
|
|
67
|
-
} catch (e) {
|
|
68
|
-
reject(e);
|
|
69
|
-
}
|
|
70
|
-
};
|
|
71
|
-
var rejected = (value) => {
|
|
72
|
-
try {
|
|
73
|
-
step(generator.throw(value));
|
|
74
|
-
} catch (e) {
|
|
75
|
-
reject(e);
|
|
76
|
-
}
|
|
77
|
-
};
|
|
78
|
-
var step = (x) => x.done ? resolve4(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
79
|
-
step((generator = generator.apply(__this, __arguments)).next());
|
|
80
|
-
});
|
|
81
|
-
};
|
|
82
|
-
|
|
83
|
-
// src/dev-worker/forkDevWorker.ts
|
|
84
|
-
var forkDevWorker_exports = {};
|
|
85
|
-
__export(forkDevWorker_exports, {
|
|
86
|
-
forkDevWorker: () => forkDevWorker
|
|
87
|
-
});
|
|
88
|
-
module.exports = __toCommonJS(forkDevWorker_exports);
|
|
89
|
-
|
|
90
|
-
// src/core/configurations/remotePlugin.ts
|
|
91
|
-
var import_fs2 = require("fs");
|
|
92
|
-
var import_path5 = require("path");
|
|
93
|
-
var import_managers2 = require("@module-federation/managers");
|
|
94
|
-
var import_typescript = __toESM(require("typescript"));
|
|
95
|
-
|
|
96
|
-
// src/core/lib/utils.ts
|
|
97
|
-
var import_axios = __toESM(require("axios"));
|
|
98
|
-
var import_http2 = __toESM(require("http"));
|
|
99
|
-
var import_https = __toESM(require("https"));
|
|
100
|
-
var import_sdk6 = require("@module-federation/sdk");
|
|
101
|
-
var import_ansi_colors = __toESM(require("ansi-colors"));
|
|
102
|
-
|
|
103
|
-
// src/core/lib/DTSManager.ts
|
|
104
|
-
var import_path4 = __toESM(require("path"));
|
|
105
|
-
var import_promises3 = require("fs/promises");
|
|
106
|
-
var import_fs = __toESM(require("fs"));
|
|
107
|
-
var import_fs_extra3 = __toESM(require("fs-extra"));
|
|
108
|
-
var import_sdk5 = require("@module-federation/sdk");
|
|
109
|
-
var import_third_party_dts_extractor2 = require("@module-federation/third-party-dts-extractor");
|
|
110
|
-
|
|
111
|
-
// src/core/lib/archiveHandler.ts
|
|
112
|
-
var import_adm_zip = __toESM(require("adm-zip"));
|
|
113
|
-
var import_path3 = require("path");
|
|
114
|
-
var import_promises2 = require("fs/promises");
|
|
115
|
-
|
|
116
|
-
// src/core/lib/typeScriptCompiler.ts
|
|
117
|
-
var import_fs_extra2 = require("fs-extra");
|
|
118
|
-
var import_crypto = __toESM(require("crypto"));
|
|
119
|
-
var import_promises = require("fs/promises");
|
|
120
|
-
var import_path2 = require("path");
|
|
121
|
-
var import_error_codes = require("@module-federation/error-codes");
|
|
122
|
-
var import_third_party_dts_extractor = require("@module-federation/third-party-dts-extractor");
|
|
123
|
-
var import_child_process2 = require("child_process");
|
|
124
|
-
var import_util = __toESM(require("util"));
|
|
125
|
-
var import_sdk3 = require("@module-federation/sdk");
|
|
126
|
-
|
|
127
|
-
// src/server/message/Message.ts
|
|
128
|
-
var _Message = class _Message {
|
|
129
|
-
constructor(type, kind) {
|
|
130
|
-
__publicField(this, "type");
|
|
131
|
-
__publicField(this, "kind");
|
|
132
|
-
__publicField(this, "time");
|
|
133
|
-
this.type = type;
|
|
134
|
-
this.kind = kind;
|
|
135
|
-
this.time = Date.now();
|
|
136
|
-
}
|
|
137
|
-
};
|
|
138
|
-
__name(_Message, "Message");
|
|
139
|
-
var Message = _Message;
|
|
140
|
-
|
|
141
|
-
// src/server/message/API/API.ts
|
|
142
|
-
var APIKind;
|
|
143
|
-
(function(APIKind2) {
|
|
144
|
-
APIKind2["UPDATE_SUBSCRIBER"] = "UPDATE_SUBSCRIBER";
|
|
145
|
-
APIKind2["RELOAD_WEB_CLIENT"] = "RELOAD_WEB_CLIENT";
|
|
146
|
-
APIKind2["FETCH_TYPES"] = "FETCH_TYPES";
|
|
147
|
-
})(APIKind || (APIKind = {}));
|
|
148
|
-
var _API = class _API extends Message {
|
|
149
|
-
constructor(content, kind) {
|
|
150
|
-
super("API", kind);
|
|
151
|
-
__publicField(this, "code");
|
|
152
|
-
__publicField(this, "payload");
|
|
153
|
-
const { code, payload } = content;
|
|
154
|
-
this.code = code;
|
|
155
|
-
this.payload = payload;
|
|
156
|
-
}
|
|
157
|
-
};
|
|
158
|
-
__name(_API, "API");
|
|
159
|
-
var API = _API;
|
|
160
|
-
|
|
161
|
-
// src/server/message/API/UpdateSubscriber.ts
|
|
162
|
-
var _UpdateSubscriberAPI = class _UpdateSubscriberAPI extends API {
|
|
163
|
-
constructor(payload) {
|
|
164
|
-
super({
|
|
165
|
-
code: 0,
|
|
166
|
-
payload
|
|
167
|
-
}, APIKind.UPDATE_SUBSCRIBER);
|
|
168
|
-
}
|
|
169
|
-
};
|
|
170
|
-
__name(_UpdateSubscriberAPI, "UpdateSubscriberAPI");
|
|
171
|
-
var UpdateSubscriberAPI = _UpdateSubscriberAPI;
|
|
172
|
-
|
|
173
|
-
// src/server/message/API/ReloadWebClient.ts
|
|
174
|
-
var _ReloadWebClientAPI = class _ReloadWebClientAPI extends API {
|
|
175
|
-
constructor(payload) {
|
|
176
|
-
super({
|
|
177
|
-
code: 0,
|
|
178
|
-
payload
|
|
179
|
-
}, APIKind.RELOAD_WEB_CLIENT);
|
|
180
|
-
}
|
|
181
|
-
};
|
|
182
|
-
__name(_ReloadWebClientAPI, "ReloadWebClientAPI");
|
|
183
|
-
var ReloadWebClientAPI = _ReloadWebClientAPI;
|
|
184
|
-
|
|
185
|
-
// src/server/message/API/FetchTypes.ts
|
|
186
|
-
var _FetchTypesAPI = class _FetchTypesAPI extends API {
|
|
187
|
-
constructor(payload) {
|
|
188
|
-
super({
|
|
189
|
-
code: 0,
|
|
190
|
-
payload
|
|
191
|
-
}, APIKind.FETCH_TYPES);
|
|
192
|
-
}
|
|
193
|
-
};
|
|
194
|
-
__name(_FetchTypesAPI, "FetchTypesAPI");
|
|
195
|
-
var FetchTypesAPI = _FetchTypesAPI;
|
|
196
|
-
|
|
197
|
-
// src/server/utils/index.ts
|
|
198
|
-
var import_net = __toESM(require("net"));
|
|
199
|
-
var import_sdk2 = require("@module-federation/sdk");
|
|
200
|
-
|
|
201
|
-
// src/server/utils/logTransform.ts
|
|
202
|
-
var import_chalk = __toESM(require("chalk"));
|
|
203
|
-
|
|
204
|
-
// src/server/message/Log/Log.ts
|
|
205
|
-
var LogLevel;
|
|
206
|
-
(function(LogLevel2) {
|
|
207
|
-
LogLevel2["LOG"] = "LOG";
|
|
208
|
-
LogLevel2["WARN"] = "WARN";
|
|
209
|
-
LogLevel2["ERROR"] = "ERROR";
|
|
210
|
-
})(LogLevel || (LogLevel = {}));
|
|
211
|
-
var LogKind;
|
|
212
|
-
(function(LogKind2) {
|
|
213
|
-
LogKind2["BrokerExitLog"] = "BrokerExitLog";
|
|
214
|
-
LogKind2["PublisherRegisteredLog"] = "PublisherRegisteredLog";
|
|
215
|
-
})(LogKind || (LogKind = {}));
|
|
216
|
-
var _Log = class _Log extends Message {
|
|
217
|
-
constructor(level, kind, ignoreVerbose = false) {
|
|
218
|
-
super("Log", kind);
|
|
219
|
-
__publicField(this, "level");
|
|
220
|
-
__publicField(this, "ignoreVerbose", false);
|
|
221
|
-
this.level = level;
|
|
222
|
-
this.ignoreVerbose = ignoreVerbose;
|
|
223
|
-
}
|
|
224
|
-
};
|
|
225
|
-
__name(_Log, "Log");
|
|
226
|
-
var Log = _Log;
|
|
227
|
-
|
|
228
|
-
// src/server/message/Log/BrokerExitLog.ts
|
|
229
|
-
var _BrokerExitLog = class _BrokerExitLog extends Log {
|
|
230
|
-
constructor() {
|
|
231
|
-
super(LogLevel.LOG, LogKind.BrokerExitLog);
|
|
232
|
-
}
|
|
233
|
-
};
|
|
234
|
-
__name(_BrokerExitLog, "BrokerExitLog");
|
|
235
|
-
var BrokerExitLog = _BrokerExitLog;
|
|
236
|
-
|
|
237
|
-
// src/server/utils/log.ts
|
|
238
|
-
var import_sdk = require("@module-federation/sdk");
|
|
239
|
-
var log4js = __toESM(require("log4js"));
|
|
240
|
-
|
|
241
|
-
// src/server/constant.ts
|
|
242
|
-
var DEFAULT_WEB_SOCKET_PORT = 16322;
|
|
243
|
-
var WEB_SOCKET_CONNECT_MAGIC_ID = "1hpzW-zo2z-o8io-gfmV1-2cb1d82";
|
|
244
|
-
var MF_SERVER_IDENTIFIER = "Module Federation DTS";
|
|
245
|
-
var DEFAULT_TAR_NAME = "@mf-types.zip";
|
|
246
|
-
var UpdateMode;
|
|
247
|
-
(function(UpdateMode2) {
|
|
248
|
-
UpdateMode2["POSITIVE"] = "POSITIVE";
|
|
249
|
-
UpdateMode2["PASSIVE"] = "PASSIVE";
|
|
250
|
-
})(UpdateMode || (UpdateMode = {}));
|
|
251
|
-
|
|
252
|
-
// src/server/utils/log.ts
|
|
253
|
-
var logger = (0, import_sdk.createLogger)(`[ ${MF_SERVER_IDENTIFIER} ]`);
|
|
254
|
-
function fileLog(msg, module2, level) {
|
|
255
|
-
var _a3, _b;
|
|
256
|
-
if (!((_a3 = process == null ? void 0 : process.env) == null ? void 0 : _a3["FEDERATION_DEBUG"])) {
|
|
257
|
-
return;
|
|
258
|
-
}
|
|
259
|
-
log4js.configure({
|
|
260
|
-
appenders: {
|
|
261
|
-
[module2]: {
|
|
262
|
-
type: "file",
|
|
263
|
-
filename: ".mf/typesGenerate.log"
|
|
264
|
-
},
|
|
265
|
-
default: {
|
|
266
|
-
type: "file",
|
|
267
|
-
filename: ".mf/typesGenerate.log"
|
|
268
|
-
}
|
|
269
|
-
},
|
|
270
|
-
categories: {
|
|
271
|
-
[module2]: {
|
|
272
|
-
appenders: [
|
|
273
|
-
module2
|
|
274
|
-
],
|
|
275
|
-
level: "error"
|
|
276
|
-
},
|
|
277
|
-
default: {
|
|
278
|
-
appenders: [
|
|
279
|
-
"default"
|
|
280
|
-
],
|
|
281
|
-
level: "trace"
|
|
282
|
-
}
|
|
283
|
-
}
|
|
284
|
-
});
|
|
285
|
-
const logger4 = log4js.getLogger(module2);
|
|
286
|
-
logger4.level = "debug";
|
|
287
|
-
(_b = logger4[level]) == null ? void 0 : _b.call(logger4, msg);
|
|
288
|
-
}
|
|
289
|
-
__name(fileLog, "fileLog");
|
|
290
|
-
function error(error2, action, from) {
|
|
291
|
-
const err = error2 instanceof Error ? error2 : new Error(`${action} error`);
|
|
292
|
-
fileLog(`[${action}] error: ${err}`, from, "fatal");
|
|
293
|
-
return err.toString();
|
|
294
|
-
}
|
|
295
|
-
__name(error, "error");
|
|
296
|
-
|
|
297
|
-
// src/server/utils/getIPV4.ts
|
|
298
|
-
var import_os = __toESM(require("os"));
|
|
299
|
-
var localIpv4 = "127.0.0.1";
|
|
300
|
-
var getIpv4Interfaces = /* @__PURE__ */ __name(() => {
|
|
301
|
-
try {
|
|
302
|
-
const interfaces = import_os.default.networkInterfaces();
|
|
303
|
-
const ipv4Interfaces = [];
|
|
304
|
-
Object.values(interfaces).forEach((detail) => {
|
|
305
|
-
detail == null ? void 0 : detail.forEach((detail2) => {
|
|
306
|
-
const familyV4Value = typeof detail2.family === "string" ? "IPv4" : 4;
|
|
307
|
-
if (detail2.family === familyV4Value && detail2.address !== localIpv4) {
|
|
308
|
-
ipv4Interfaces.push(detail2);
|
|
309
|
-
}
|
|
310
|
-
});
|
|
311
|
-
});
|
|
312
|
-
return ipv4Interfaces;
|
|
313
|
-
} catch (_err) {
|
|
314
|
-
return [];
|
|
315
|
-
}
|
|
316
|
-
}, "getIpv4Interfaces");
|
|
317
|
-
var getIPV4 = /* @__PURE__ */ __name(() => {
|
|
318
|
-
const ipv4Interfaces = getIpv4Interfaces();
|
|
319
|
-
const ipv4Interface = ipv4Interfaces[0] || {
|
|
320
|
-
address: localIpv4
|
|
321
|
-
};
|
|
322
|
-
return ipv4Interface.address;
|
|
323
|
-
}, "getIPV4");
|
|
324
|
-
|
|
325
|
-
// src/server/utils/index.ts
|
|
326
|
-
function getIdentifier(options) {
|
|
327
|
-
const { ip, name } = options;
|
|
328
|
-
return `mf ${import_sdk2.SEPARATOR}${name}${ip ? `${import_sdk2.SEPARATOR}${ip}` : ""}`;
|
|
329
|
-
}
|
|
330
|
-
__name(getIdentifier, "getIdentifier");
|
|
331
|
-
function fib(n) {
|
|
332
|
-
let i = 2;
|
|
333
|
-
const res = [
|
|
334
|
-
0,
|
|
335
|
-
1,
|
|
336
|
-
1
|
|
337
|
-
];
|
|
338
|
-
while (i <= n) {
|
|
339
|
-
res[i] = res[i - 1] + res[i - 2];
|
|
340
|
-
i++;
|
|
341
|
-
}
|
|
342
|
-
return res[n];
|
|
343
|
-
}
|
|
344
|
-
__name(fib, "fib");
|
|
345
|
-
function getFreePort() {
|
|
346
|
-
return new Promise((resolve4, reject) => {
|
|
347
|
-
const server = import_net.default.createServer();
|
|
348
|
-
server.unref();
|
|
349
|
-
server.on("error", reject);
|
|
350
|
-
server.listen(0, () => {
|
|
351
|
-
const { port } = server.address();
|
|
352
|
-
server.close(() => {
|
|
353
|
-
resolve4(port);
|
|
354
|
-
});
|
|
355
|
-
});
|
|
356
|
-
});
|
|
357
|
-
}
|
|
358
|
-
__name(getFreePort, "getFreePort");
|
|
359
|
-
|
|
360
|
-
// src/server/Publisher.ts
|
|
361
|
-
var _Publisher = class _Publisher {
|
|
362
|
-
constructor(ctx) {
|
|
363
|
-
__publicField(this, "_ip");
|
|
364
|
-
__publicField(this, "_name");
|
|
365
|
-
__publicField(this, "_remoteTypeTarPath");
|
|
366
|
-
__publicField(this, "_subscribers");
|
|
367
|
-
__publicField(this, "_ws");
|
|
368
|
-
__publicField(this, "dynamicRemoteMap");
|
|
369
|
-
this._name = ctx.name;
|
|
370
|
-
this._ip = ctx.ip;
|
|
371
|
-
this._remoteTypeTarPath = ctx.remoteTypeTarPath;
|
|
372
|
-
this._subscribers = /* @__PURE__ */ new Map();
|
|
373
|
-
this._ws = ctx.ws;
|
|
374
|
-
this.dynamicRemoteMap = /* @__PURE__ */ new Map();
|
|
375
|
-
}
|
|
376
|
-
get identifier() {
|
|
377
|
-
return getIdentifier({
|
|
378
|
-
name: this._name,
|
|
379
|
-
ip: this._ip
|
|
380
|
-
});
|
|
381
|
-
}
|
|
382
|
-
get name() {
|
|
383
|
-
return this._name;
|
|
384
|
-
}
|
|
385
|
-
get ip() {
|
|
386
|
-
return this._ip;
|
|
387
|
-
}
|
|
388
|
-
get remoteTypeTarPath() {
|
|
389
|
-
return this._remoteTypeTarPath;
|
|
390
|
-
}
|
|
391
|
-
get hasSubscribes() {
|
|
392
|
-
return Boolean(this._subscribers.size);
|
|
393
|
-
}
|
|
394
|
-
get subscribers() {
|
|
395
|
-
return this._subscribers;
|
|
396
|
-
}
|
|
397
|
-
addSubscriber(identifier, subscriber) {
|
|
398
|
-
fileLog(`${this.name} set subscriber: ${identifier}`, "Publisher", "info");
|
|
399
|
-
this._subscribers.set(identifier, subscriber);
|
|
400
|
-
}
|
|
401
|
-
removeSubscriber(identifier) {
|
|
402
|
-
if (this._subscribers.has(identifier)) {
|
|
403
|
-
fileLog(`${this.name} removeSubscriber: ${identifier}`, "Publisher", "warn");
|
|
404
|
-
this._subscribers.delete(identifier);
|
|
405
|
-
}
|
|
406
|
-
}
|
|
407
|
-
notifySubscriber(subscriberIdentifier, options) {
|
|
408
|
-
const subscriber = this._subscribers.get(subscriberIdentifier);
|
|
409
|
-
if (!subscriber) {
|
|
410
|
-
fileLog(`[notifySubscriber] ${this.name} notifySubscriber: ${subscriberIdentifier}, does not exits`, "Publisher", "error");
|
|
411
|
-
return;
|
|
412
|
-
}
|
|
413
|
-
const api = new UpdateSubscriberAPI(options);
|
|
414
|
-
subscriber.send(JSON.stringify(api));
|
|
415
|
-
fileLog(`[notifySubscriber] ${this.name} notifySubscriber: ${JSON.stringify(subscriberIdentifier)}, message: ${JSON.stringify(api)}`, "Publisher", "info");
|
|
416
|
-
}
|
|
417
|
-
fetchRemoteTypes(options) {
|
|
418
|
-
fileLog(`[fetchRemoteTypes] ${this.name} fetchRemoteTypes, options: ${JSON.stringify(options)}, ws: ${Boolean(this._ws)}`, "Publisher", "info");
|
|
419
|
-
if (!this._ws) {
|
|
420
|
-
return;
|
|
421
|
-
}
|
|
422
|
-
const api = new FetchTypesAPI(options);
|
|
423
|
-
this._ws.send(JSON.stringify(api));
|
|
424
|
-
}
|
|
425
|
-
notifySubscribers(options) {
|
|
426
|
-
const api = new UpdateSubscriberAPI(options);
|
|
427
|
-
this.broadcast(api);
|
|
428
|
-
}
|
|
429
|
-
broadcast(message) {
|
|
430
|
-
if (this.hasSubscribes) {
|
|
431
|
-
this._subscribers.forEach((subscriber, key) => {
|
|
432
|
-
fileLog(`[BroadCast] ${this.name} notifySubscriber: ${key}, PID: ${process.pid}, message: ${JSON.stringify(message)}`, "Publisher", "info");
|
|
433
|
-
subscriber.send(JSON.stringify(message));
|
|
434
|
-
});
|
|
435
|
-
} else {
|
|
436
|
-
fileLog(`[BroadCast] ${this.name}'s subscribe is empty`, "Publisher", "warn");
|
|
437
|
-
}
|
|
438
|
-
}
|
|
439
|
-
close() {
|
|
440
|
-
this._ws = void 0;
|
|
441
|
-
this._subscribers.forEach((_subscriber, identifier) => {
|
|
442
|
-
fileLog(`[BroadCast] close ${this.name} remove: ${identifier}`, "Publisher", "warn");
|
|
443
|
-
this.removeSubscriber(identifier);
|
|
444
|
-
});
|
|
445
|
-
}
|
|
446
|
-
};
|
|
447
|
-
__name(_Publisher, "Publisher");
|
|
448
|
-
var Publisher = _Publisher;
|
|
449
|
-
|
|
450
|
-
// src/server/DevServer.ts
|
|
451
|
-
var import_isomorphic_ws2 = __toESM(require("isomorphic-ws"));
|
|
452
|
-
|
|
453
|
-
// src/server/broker/Broker.ts
|
|
454
|
-
var import_http = require("http");
|
|
455
|
-
var import_isomorphic_ws = __toESM(require("isomorphic-ws"));
|
|
456
|
-
var import_node_schedule = __toESM(require("node-schedule"));
|
|
457
|
-
var import_url = require("url");
|
|
458
|
-
|
|
459
|
-
// src/server/message/Action/Action.ts
|
|
460
|
-
var ActionKind;
|
|
461
|
-
(function(ActionKind2) {
|
|
462
|
-
ActionKind2["ADD_SUBSCRIBER"] = "ADD_SUBSCRIBER";
|
|
463
|
-
ActionKind2["EXIT_SUBSCRIBER"] = "EXIT_SUBSCRIBER";
|
|
464
|
-
ActionKind2["ADD_PUBLISHER"] = "ADD_PUBLISHER";
|
|
465
|
-
ActionKind2["UPDATE_PUBLISHER"] = "UPDATE_PUBLISHER";
|
|
466
|
-
ActionKind2["NOTIFY_SUBSCRIBER"] = "NOTIFY_SUBSCRIBER";
|
|
467
|
-
ActionKind2["EXIT_PUBLISHER"] = "EXIT_PUBLISHER";
|
|
468
|
-
ActionKind2["ADD_WEB_CLIENT"] = "ADD_WEB_CLIENT";
|
|
469
|
-
ActionKind2["NOTIFY_WEB_CLIENT"] = "NOTIFY_WEB_CLIENT";
|
|
470
|
-
ActionKind2["FETCH_TYPES"] = "FETCH_TYPES";
|
|
471
|
-
ActionKind2["ADD_DYNAMIC_REMOTE"] = "ADD_DYNAMIC_REMOTE";
|
|
472
|
-
})(ActionKind || (ActionKind = {}));
|
|
473
|
-
var _Action = class _Action extends Message {
|
|
474
|
-
constructor(content, kind) {
|
|
475
|
-
super("Action", kind);
|
|
476
|
-
__publicField(this, "payload");
|
|
477
|
-
const { payload } = content;
|
|
478
|
-
this.payload = payload;
|
|
479
|
-
}
|
|
480
|
-
};
|
|
481
|
-
__name(_Action, "Action");
|
|
482
|
-
var Action = _Action;
|
|
483
|
-
|
|
484
|
-
// src/server/message/Action/Update.ts
|
|
485
|
-
var UpdateKind;
|
|
486
|
-
(function(UpdateKind2) {
|
|
487
|
-
UpdateKind2["UPDATE_TYPE"] = "UPDATE_TYPE";
|
|
488
|
-
UpdateKind2["RELOAD_PAGE"] = "RELOAD_PAGE";
|
|
489
|
-
})(UpdateKind || (UpdateKind = {}));
|
|
490
|
-
|
|
491
|
-
// src/server/message/Action/AddPublisher.ts
|
|
492
|
-
var _AddPublisherAction = class _AddPublisherAction extends Action {
|
|
493
|
-
constructor(payload) {
|
|
494
|
-
super({
|
|
495
|
-
payload
|
|
496
|
-
}, ActionKind.ADD_PUBLISHER);
|
|
497
|
-
}
|
|
498
|
-
};
|
|
499
|
-
__name(_AddPublisherAction, "AddPublisherAction");
|
|
500
|
-
var AddPublisherAction = _AddPublisherAction;
|
|
501
|
-
|
|
502
|
-
// src/server/message/Action/AddSubscriber.ts
|
|
503
|
-
var _AddSubscriberAction = class _AddSubscriberAction extends Action {
|
|
504
|
-
constructor(payload) {
|
|
505
|
-
super({
|
|
506
|
-
payload
|
|
507
|
-
}, ActionKind.ADD_SUBSCRIBER);
|
|
508
|
-
}
|
|
509
|
-
};
|
|
510
|
-
__name(_AddSubscriberAction, "AddSubscriberAction");
|
|
511
|
-
var AddSubscriberAction = _AddSubscriberAction;
|
|
512
|
-
|
|
513
|
-
// src/server/message/Action/ExitSubscriber.ts
|
|
514
|
-
var _ExitSubscriberAction = class _ExitSubscriberAction extends Action {
|
|
515
|
-
constructor(payload) {
|
|
516
|
-
super({
|
|
517
|
-
payload
|
|
518
|
-
}, ActionKind.EXIT_SUBSCRIBER);
|
|
519
|
-
}
|
|
520
|
-
};
|
|
521
|
-
__name(_ExitSubscriberAction, "ExitSubscriberAction");
|
|
522
|
-
var ExitSubscriberAction = _ExitSubscriberAction;
|
|
523
|
-
|
|
524
|
-
// src/server/message/Action/ExitPublisher.ts
|
|
525
|
-
var _ExitPublisherAction = class _ExitPublisherAction extends Action {
|
|
526
|
-
constructor(payload) {
|
|
527
|
-
super({
|
|
528
|
-
payload
|
|
529
|
-
}, ActionKind.EXIT_PUBLISHER);
|
|
530
|
-
}
|
|
531
|
-
};
|
|
532
|
-
__name(_ExitPublisherAction, "ExitPublisherAction");
|
|
533
|
-
var ExitPublisherAction = _ExitPublisherAction;
|
|
534
|
-
|
|
535
|
-
// src/server/message/Action/NotifyWebClient.ts
|
|
536
|
-
var _NotifyWebClientAction = class _NotifyWebClientAction extends Action {
|
|
537
|
-
constructor(payload) {
|
|
538
|
-
super({
|
|
539
|
-
payload
|
|
540
|
-
}, ActionKind.NOTIFY_WEB_CLIENT);
|
|
541
|
-
}
|
|
542
|
-
};
|
|
543
|
-
__name(_NotifyWebClientAction, "NotifyWebClientAction");
|
|
544
|
-
var NotifyWebClientAction = _NotifyWebClientAction;
|
|
545
|
-
|
|
546
|
-
// src/server/message/Action/UpdatePublisher.ts
|
|
547
|
-
var _UpdatePublisherAction = class _UpdatePublisherAction extends Action {
|
|
548
|
-
constructor(payload) {
|
|
549
|
-
super({
|
|
550
|
-
payload
|
|
551
|
-
}, ActionKind.UPDATE_PUBLISHER);
|
|
552
|
-
}
|
|
553
|
-
};
|
|
554
|
-
__name(_UpdatePublisherAction, "UpdatePublisherAction");
|
|
555
|
-
var UpdatePublisherAction = _UpdatePublisherAction;
|
|
556
|
-
|
|
557
|
-
// src/server/broker/Broker.ts
|
|
558
|
-
var _Broker = class _Broker {
|
|
559
|
-
constructor() {
|
|
560
|
-
__publicField(this, "_publisherMap", /* @__PURE__ */ new Map());
|
|
561
|
-
__publicField(this, "_webClientMap", /* @__PURE__ */ new Map());
|
|
562
|
-
__publicField(this, "_webSocketServer");
|
|
563
|
-
__publicField(this, "_secureWebSocketServer");
|
|
564
|
-
__publicField(this, "_tmpSubscriberShelter", /* @__PURE__ */ new Map());
|
|
565
|
-
__publicField(this, "_scheduleJob", null);
|
|
566
|
-
this._setSchedule();
|
|
567
|
-
this._startWsServer();
|
|
568
|
-
this._stopWhenSIGTERMOrSIGINT();
|
|
569
|
-
this._handleUnexpectedExit();
|
|
570
|
-
}
|
|
571
|
-
get hasPublishers() {
|
|
572
|
-
return Boolean(this._publisherMap.size);
|
|
573
|
-
}
|
|
574
|
-
_startWsServer() {
|
|
575
|
-
return __async(this, null, function* () {
|
|
576
|
-
const wsHandler = /* @__PURE__ */ __name((ws, req) => {
|
|
577
|
-
const { url: reqUrl = "" } = req;
|
|
578
|
-
const { query } = (0, import_url.parse)(reqUrl, true);
|
|
579
|
-
const { WEB_SOCKET_CONNECT_MAGIC_ID: WEB_SOCKET_CONNECT_MAGIC_ID2 } = query;
|
|
580
|
-
if (WEB_SOCKET_CONNECT_MAGIC_ID2 === _Broker.WEB_SOCKET_CONNECT_MAGIC_ID) {
|
|
581
|
-
ws.on("message", (message) => {
|
|
582
|
-
try {
|
|
583
|
-
const text = message.toString();
|
|
584
|
-
const action = JSON.parse(text);
|
|
585
|
-
fileLog(`${action == null ? void 0 : action.kind} action received `, "Broker", "info");
|
|
586
|
-
this._takeAction(action, ws);
|
|
587
|
-
} catch (error2) {
|
|
588
|
-
fileLog(`parse action message error: ${error2}`, "Broker", "error");
|
|
589
|
-
}
|
|
590
|
-
});
|
|
591
|
-
ws.on("error", (e) => {
|
|
592
|
-
fileLog(`parse action message error: ${e}`, "Broker", "error");
|
|
593
|
-
});
|
|
594
|
-
} else {
|
|
595
|
-
ws.send("Invalid CONNECT ID.");
|
|
596
|
-
fileLog("Invalid CONNECT ID.", "Broker", "warn");
|
|
597
|
-
ws.close();
|
|
598
|
-
}
|
|
599
|
-
}, "wsHandler");
|
|
600
|
-
const server = (0, import_http.createServer)();
|
|
601
|
-
this._webSocketServer = new import_isomorphic_ws.default.Server({
|
|
602
|
-
noServer: true
|
|
603
|
-
});
|
|
604
|
-
this._webSocketServer.on("error", (err) => {
|
|
605
|
-
fileLog(`ws error:
|
|
606
|
-
${err.message}
|
|
607
|
-
${err.stack}`, "Broker", "error");
|
|
608
|
-
});
|
|
609
|
-
this._webSocketServer.on("listening", () => {
|
|
610
|
-
fileLog(`WebSocket server is listening on port ${_Broker.DEFAULT_WEB_SOCKET_PORT}`, "Broker", "info");
|
|
611
|
-
});
|
|
612
|
-
this._webSocketServer.on("connection", wsHandler);
|
|
613
|
-
this._webSocketServer.on("close", (code) => {
|
|
614
|
-
fileLog(`WebSocket Server Close with Code ${code}`, "Broker", "warn");
|
|
615
|
-
this._webSocketServer && this._webSocketServer.close();
|
|
616
|
-
this._webSocketServer = void 0;
|
|
617
|
-
});
|
|
618
|
-
server.on("upgrade", (req, socket, head) => {
|
|
619
|
-
var _a3;
|
|
620
|
-
if (req.url) {
|
|
621
|
-
const { pathname } = (0, import_url.parse)(req.url);
|
|
622
|
-
if (pathname === "/") {
|
|
623
|
-
(_a3 = this._webSocketServer) == null ? void 0 : _a3.handleUpgrade(req, socket, head, (ws) => {
|
|
624
|
-
var _a4;
|
|
625
|
-
(_a4 = this._webSocketServer) == null ? void 0 : _a4.emit("connection", ws, req);
|
|
626
|
-
});
|
|
627
|
-
}
|
|
628
|
-
}
|
|
629
|
-
});
|
|
630
|
-
server.listen(_Broker.DEFAULT_WEB_SOCKET_PORT);
|
|
631
|
-
});
|
|
632
|
-
}
|
|
633
|
-
_takeAction(action, client) {
|
|
634
|
-
return __async(this, null, function* () {
|
|
635
|
-
const { kind, payload } = action;
|
|
636
|
-
if (kind === ActionKind.ADD_PUBLISHER) {
|
|
637
|
-
yield this._addPublisher(payload, client);
|
|
638
|
-
}
|
|
639
|
-
if (kind === ActionKind.UPDATE_PUBLISHER) {
|
|
640
|
-
yield this._updatePublisher(payload, client);
|
|
641
|
-
}
|
|
642
|
-
if (kind === ActionKind.ADD_SUBSCRIBER) {
|
|
643
|
-
yield this._addSubscriber(payload, client);
|
|
644
|
-
}
|
|
645
|
-
if (kind === ActionKind.EXIT_SUBSCRIBER) {
|
|
646
|
-
yield this._removeSubscriber(payload, client);
|
|
647
|
-
}
|
|
648
|
-
if (kind === ActionKind.EXIT_PUBLISHER) {
|
|
649
|
-
yield this._removePublisher(payload, client);
|
|
650
|
-
}
|
|
651
|
-
if (kind === ActionKind.ADD_WEB_CLIENT) {
|
|
652
|
-
yield this._addWebClient(payload, client);
|
|
653
|
-
}
|
|
654
|
-
if (kind === ActionKind.NOTIFY_WEB_CLIENT) {
|
|
655
|
-
yield this._notifyWebClient(payload, client);
|
|
656
|
-
}
|
|
657
|
-
if (kind === ActionKind.FETCH_TYPES) {
|
|
658
|
-
yield this._fetchTypes(payload, client);
|
|
659
|
-
}
|
|
660
|
-
if (kind === ActionKind.ADD_DYNAMIC_REMOTE) {
|
|
661
|
-
this._addDynamicRemote(payload);
|
|
662
|
-
}
|
|
663
|
-
});
|
|
664
|
-
}
|
|
665
|
-
_addPublisher(context, client) {
|
|
666
|
-
return __async(this, null, function* () {
|
|
667
|
-
const { name, ip, remoteTypeTarPath } = context != null ? context : {};
|
|
668
|
-
const identifier = getIdentifier({
|
|
669
|
-
name,
|
|
670
|
-
ip
|
|
671
|
-
});
|
|
672
|
-
if (this._publisherMap.has(identifier)) {
|
|
673
|
-
fileLog(`[${ActionKind.ADD_PUBLISHER}] ${identifier} has been added, this action will be ignored`, "Broker", "warn");
|
|
674
|
-
return;
|
|
675
|
-
}
|
|
676
|
-
try {
|
|
677
|
-
const publisher = new Publisher({
|
|
678
|
-
name,
|
|
679
|
-
ip,
|
|
680
|
-
remoteTypeTarPath,
|
|
681
|
-
ws: client
|
|
682
|
-
});
|
|
683
|
-
this._publisherMap.set(identifier, publisher);
|
|
684
|
-
fileLog(`[${ActionKind.ADD_PUBLISHER}] ${identifier} Adding Publisher Succeed`, "Broker", "info");
|
|
685
|
-
const tmpSubScribers = this._getTmpSubScribers(identifier);
|
|
686
|
-
if (tmpSubScribers) {
|
|
687
|
-
fileLog(`[${ActionKind.ADD_PUBLISHER}] consumeTmpSubscriber set ${publisher.name}\u2019s subscribers `, "Broker", "info");
|
|
688
|
-
this._consumeTmpSubScribers(publisher, tmpSubScribers);
|
|
689
|
-
this._clearTmpSubScriberRelation(identifier);
|
|
690
|
-
}
|
|
691
|
-
} catch (err) {
|
|
692
|
-
const msg = error(err, ActionKind.ADD_PUBLISHER, "Broker");
|
|
693
|
-
client.send(msg);
|
|
694
|
-
client.close();
|
|
695
|
-
}
|
|
696
|
-
});
|
|
697
|
-
}
|
|
698
|
-
_updatePublisher(context, client) {
|
|
699
|
-
return __async(this, null, function* () {
|
|
700
|
-
const { name, updateMode, updateKind, updateSourcePaths, remoteTypeTarPath, ip } = context != null ? context : {};
|
|
701
|
-
const identifier = getIdentifier({
|
|
702
|
-
name,
|
|
703
|
-
ip
|
|
704
|
-
});
|
|
705
|
-
if (!this._publisherMap.has(identifier)) {
|
|
706
|
-
fileLog(`[${ActionKind.UPDATE_PUBLISHER}] ${identifier} has not been started, this action will be ignored
|
|
707
|
-
this._publisherMap: ${JSON.stringify(this._publisherMap.entries())}
|
|
708
|
-
`, "Broker", "warn");
|
|
709
|
-
return;
|
|
710
|
-
}
|
|
711
|
-
try {
|
|
712
|
-
const publisher = this._publisherMap.get(identifier);
|
|
713
|
-
fileLog(
|
|
714
|
-
// eslint-disable-next-line max-len
|
|
715
|
-
`[${ActionKind.UPDATE_PUBLISHER}] ${identifier} update, and notify subscribers to update`,
|
|
716
|
-
"Broker",
|
|
717
|
-
"info"
|
|
718
|
-
);
|
|
719
|
-
if (publisher) {
|
|
720
|
-
publisher.notifySubscribers({
|
|
721
|
-
remoteTypeTarPath,
|
|
722
|
-
name,
|
|
723
|
-
updateMode,
|
|
724
|
-
updateKind,
|
|
725
|
-
updateSourcePaths: updateSourcePaths || []
|
|
726
|
-
});
|
|
727
|
-
this._publisherMap.forEach((p) => {
|
|
728
|
-
if (p.name === publisher.name) {
|
|
729
|
-
return;
|
|
730
|
-
}
|
|
731
|
-
const dynamicRemoteInfo = p.dynamicRemoteMap.get(identifier);
|
|
732
|
-
if (dynamicRemoteInfo) {
|
|
733
|
-
fileLog(
|
|
734
|
-
// eslint-disable-next-line max-len
|
|
735
|
-
`dynamicRemoteInfo: ${JSON.stringify(dynamicRemoteInfo)}, identifier:${identifier} publish: ${p.name}`,
|
|
736
|
-
"Broker",
|
|
737
|
-
"info"
|
|
738
|
-
);
|
|
739
|
-
p.fetchRemoteTypes({
|
|
740
|
-
remoteInfo: dynamicRemoteInfo,
|
|
741
|
-
once: false
|
|
742
|
-
});
|
|
743
|
-
}
|
|
744
|
-
});
|
|
745
|
-
}
|
|
746
|
-
} catch (err) {
|
|
747
|
-
const msg = error(err, ActionKind.UPDATE_PUBLISHER, "Broker");
|
|
748
|
-
client.send(msg);
|
|
749
|
-
client.close();
|
|
750
|
-
}
|
|
751
|
-
});
|
|
752
|
-
}
|
|
753
|
-
_fetchTypes(context, _client) {
|
|
754
|
-
return __async(this, null, function* () {
|
|
755
|
-
const { name, ip, remoteInfo } = context != null ? context : {};
|
|
756
|
-
const identifier = getIdentifier({
|
|
757
|
-
name,
|
|
758
|
-
ip
|
|
759
|
-
});
|
|
760
|
-
try {
|
|
761
|
-
const publisher = this._publisherMap.get(identifier);
|
|
762
|
-
fileLog(`[${ActionKind.FETCH_TYPES}] ${identifier} fetch types`, "Broker", "info");
|
|
763
|
-
if (publisher) {
|
|
764
|
-
publisher.fetchRemoteTypes({
|
|
765
|
-
remoteInfo,
|
|
766
|
-
once: true
|
|
767
|
-
});
|
|
768
|
-
}
|
|
769
|
-
} catch (err) {
|
|
770
|
-
fileLog(`[${ActionKind.FETCH_TYPES}] ${identifier} fetch types fail , error info: ${err}`, "Broker", "error");
|
|
771
|
-
}
|
|
772
|
-
});
|
|
773
|
-
}
|
|
774
|
-
_addDynamicRemote(context) {
|
|
775
|
-
const { name, ip, remoteInfo, remoteIp } = context != null ? context : {};
|
|
776
|
-
const identifier = getIdentifier({
|
|
777
|
-
name,
|
|
778
|
-
ip
|
|
779
|
-
});
|
|
780
|
-
const publisher = this._publisherMap.get(identifier);
|
|
781
|
-
const remoteId = getIdentifier({
|
|
782
|
-
name: remoteInfo.name,
|
|
783
|
-
ip: remoteIp
|
|
784
|
-
});
|
|
785
|
-
fileLog(`[${ActionKind.ADD_DYNAMIC_REMOTE}] identifier:${identifier},publisher: ${publisher.name}, remoteId:${remoteId}`, "Broker", "error");
|
|
786
|
-
if (!publisher || publisher.dynamicRemoteMap.has(remoteId)) {
|
|
787
|
-
return;
|
|
788
|
-
}
|
|
789
|
-
publisher.dynamicRemoteMap.set(remoteId, remoteInfo);
|
|
790
|
-
}
|
|
791
|
-
// app1 consumes provider1,provider2. Dependencies at this time: publishers: [provider1, provider2], subscriberName: app1
|
|
792
|
-
// provider1 is app1's remote
|
|
793
|
-
_addSubscriber(context, client) {
|
|
794
|
-
return __async(this, null, function* () {
|
|
795
|
-
const { publishers, name: subscriberName } = context != null ? context : {};
|
|
796
|
-
publishers.forEach((publisher) => {
|
|
797
|
-
const { name, ip } = publisher;
|
|
798
|
-
const identifier = getIdentifier({
|
|
799
|
-
name,
|
|
800
|
-
ip
|
|
801
|
-
});
|
|
802
|
-
if (!this._publisherMap.has(identifier)) {
|
|
803
|
-
fileLog(`[${ActionKind.ADD_SUBSCRIBER}]: ${identifier} has not been started, ${subscriberName} will add the relation to tmp shelter`, "Broker", "warn");
|
|
804
|
-
this._addTmpSubScriberRelation({
|
|
805
|
-
name: getIdentifier({
|
|
806
|
-
name: context.name,
|
|
807
|
-
ip: context.ip
|
|
808
|
-
}),
|
|
809
|
-
client
|
|
810
|
-
}, publisher);
|
|
811
|
-
return;
|
|
812
|
-
}
|
|
813
|
-
try {
|
|
814
|
-
const registeredPublisher = this._publisherMap.get(identifier);
|
|
815
|
-
if (registeredPublisher) {
|
|
816
|
-
registeredPublisher.addSubscriber(getIdentifier({
|
|
817
|
-
name: subscriberName,
|
|
818
|
-
ip: context.ip
|
|
819
|
-
}), client);
|
|
820
|
-
fileLog(
|
|
821
|
-
// eslint-disable-next-line @ies/eden/max-calls-in-template
|
|
822
|
-
`[${ActionKind.ADD_SUBSCRIBER}]: ${identifier} has been started, Adding Subscriber ${subscriberName} Succeed, this.__publisherMap are: ${JSON.stringify(Array.from(this._publisherMap.entries()))}`,
|
|
823
|
-
"Broker",
|
|
824
|
-
"info"
|
|
825
|
-
);
|
|
826
|
-
registeredPublisher.notifySubscriber(getIdentifier({
|
|
827
|
-
name: subscriberName,
|
|
828
|
-
ip: context.ip
|
|
829
|
-
}), {
|
|
830
|
-
updateKind: UpdateKind.UPDATE_TYPE,
|
|
831
|
-
updateMode: UpdateMode.PASSIVE,
|
|
832
|
-
updateSourcePaths: [
|
|
833
|
-
registeredPublisher.name
|
|
834
|
-
],
|
|
835
|
-
remoteTypeTarPath: registeredPublisher.remoteTypeTarPath,
|
|
836
|
-
name: registeredPublisher.name
|
|
837
|
-
});
|
|
838
|
-
fileLog(
|
|
839
|
-
// eslint-disable-next-line @ies/eden/max-calls-in-template
|
|
840
|
-
`[${ActionKind.ADD_SUBSCRIBER}]: notifySubscriber Subscriber ${subscriberName}, updateMode: "PASSIVE", updateSourcePaths: ${registeredPublisher.name}`,
|
|
841
|
-
"Broker",
|
|
842
|
-
"info"
|
|
843
|
-
);
|
|
844
|
-
}
|
|
845
|
-
} catch (err) {
|
|
846
|
-
const msg = error(err, ActionKind.ADD_SUBSCRIBER, "Broker");
|
|
847
|
-
client.send(msg);
|
|
848
|
-
client.close();
|
|
849
|
-
}
|
|
850
|
-
});
|
|
851
|
-
});
|
|
852
|
-
}
|
|
853
|
-
// Trigger while consumer exit
|
|
854
|
-
_removeSubscriber(context, client) {
|
|
855
|
-
return __async(this, null, function* () {
|
|
856
|
-
const { publishers } = context != null ? context : {};
|
|
857
|
-
const subscriberIdentifier = getIdentifier({
|
|
858
|
-
name: context == null ? void 0 : context.name,
|
|
859
|
-
ip: context == null ? void 0 : context.ip
|
|
860
|
-
});
|
|
861
|
-
publishers.forEach((publisher) => {
|
|
862
|
-
const { name, ip } = publisher;
|
|
863
|
-
const identifier = getIdentifier({
|
|
864
|
-
name,
|
|
865
|
-
ip
|
|
866
|
-
});
|
|
867
|
-
const registeredPublisher = this._publisherMap.get(identifier);
|
|
868
|
-
if (!registeredPublisher) {
|
|
869
|
-
fileLog(`[${ActionKind.EXIT_SUBSCRIBER}], ${identifier} does not exit `, "Broker", "warn");
|
|
870
|
-
return;
|
|
871
|
-
}
|
|
872
|
-
try {
|
|
873
|
-
fileLog(`[${ActionKind.EXIT_SUBSCRIBER}], ${identifier} will exit `, "Broker", "INFO");
|
|
874
|
-
registeredPublisher.removeSubscriber(subscriberIdentifier);
|
|
875
|
-
this._clearTmpSubScriberRelation(identifier);
|
|
876
|
-
if (!registeredPublisher.hasSubscribes) {
|
|
877
|
-
this._publisherMap.delete(identifier);
|
|
878
|
-
}
|
|
879
|
-
if (!this.hasPublishers) {
|
|
880
|
-
this.exit();
|
|
881
|
-
}
|
|
882
|
-
} catch (err) {
|
|
883
|
-
const msg = error(err, ActionKind.EXIT_SUBSCRIBER, "Broker");
|
|
884
|
-
client.send(msg);
|
|
885
|
-
client.close();
|
|
886
|
-
}
|
|
887
|
-
});
|
|
888
|
-
});
|
|
889
|
-
}
|
|
890
|
-
_removePublisher(context, client) {
|
|
891
|
-
return __async(this, null, function* () {
|
|
892
|
-
const { name, ip } = context != null ? context : {};
|
|
893
|
-
const identifier = getIdentifier({
|
|
894
|
-
name,
|
|
895
|
-
ip
|
|
896
|
-
});
|
|
897
|
-
const publisher = this._publisherMap.get(identifier);
|
|
898
|
-
if (!publisher) {
|
|
899
|
-
fileLog(`[${ActionKind.EXIT_PUBLISHER}]: ${identifier}} has not been added, this action will be ingored`, "Broker", "warn");
|
|
900
|
-
return;
|
|
901
|
-
}
|
|
902
|
-
try {
|
|
903
|
-
const { subscribers } = publisher;
|
|
904
|
-
subscribers.forEach((subscriber, subscriberIdentifier) => {
|
|
905
|
-
this._addTmpSubScriberRelation({
|
|
906
|
-
name: subscriberIdentifier,
|
|
907
|
-
client: subscriber
|
|
908
|
-
}, {
|
|
909
|
-
name: publisher.name,
|
|
910
|
-
ip: publisher.ip
|
|
911
|
-
});
|
|
912
|
-
fileLog(
|
|
913
|
-
// eslint-disable-next-line max-len
|
|
914
|
-
`[${ActionKind.EXIT_PUBLISHER}]: ${identifier} is removing , subscriber: ${subscriberIdentifier} will be add tmpSubScriberRelation`,
|
|
915
|
-
"Broker",
|
|
916
|
-
"info"
|
|
917
|
-
);
|
|
918
|
-
});
|
|
919
|
-
this._publisherMap.delete(identifier);
|
|
920
|
-
fileLog(`[${ActionKind.EXIT_PUBLISHER}]: ${identifier} is removed `, "Broker", "info");
|
|
921
|
-
if (!this.hasPublishers) {
|
|
922
|
-
fileLog(`[${ActionKind.EXIT_PUBLISHER}]: _publisherMap is empty, all server will exit `, "Broker", "warn");
|
|
923
|
-
this.exit();
|
|
924
|
-
}
|
|
925
|
-
} catch (err) {
|
|
926
|
-
const msg = error(err, ActionKind.EXIT_PUBLISHER, "Broker");
|
|
927
|
-
client.send(msg);
|
|
928
|
-
client.close();
|
|
929
|
-
}
|
|
930
|
-
});
|
|
931
|
-
}
|
|
932
|
-
_addWebClient(context, client) {
|
|
933
|
-
return __async(this, null, function* () {
|
|
934
|
-
const { name } = context != null ? context : {};
|
|
935
|
-
const identifier = getIdentifier({
|
|
936
|
-
name
|
|
937
|
-
});
|
|
938
|
-
if (this._webClientMap.has(identifier)) {
|
|
939
|
-
fileLog(`${identifier}} has been added, this action will override prev WebClient`, "Broker", "warn");
|
|
940
|
-
}
|
|
941
|
-
try {
|
|
942
|
-
this._webClientMap.set(identifier, client);
|
|
943
|
-
fileLog(`${identifier} adding WebClient Succeed`, "Broker", "info");
|
|
944
|
-
} catch (err) {
|
|
945
|
-
const msg = error(err, ActionKind.ADD_WEB_CLIENT, "Broker");
|
|
946
|
-
client.send(msg);
|
|
947
|
-
client.close();
|
|
948
|
-
}
|
|
949
|
-
});
|
|
950
|
-
}
|
|
951
|
-
_notifyWebClient(context, client) {
|
|
952
|
-
return __async(this, null, function* () {
|
|
953
|
-
const { name, updateMode } = context != null ? context : {};
|
|
954
|
-
const identifier = getIdentifier({
|
|
955
|
-
name
|
|
956
|
-
});
|
|
957
|
-
const webClient = this._webClientMap.get(identifier);
|
|
958
|
-
if (!webClient) {
|
|
959
|
-
fileLog(`[${ActionKind.NOTIFY_WEB_CLIENT}] ${identifier} has not been added, this action will be ignored`, "Broker", "warn");
|
|
960
|
-
return;
|
|
961
|
-
}
|
|
962
|
-
try {
|
|
963
|
-
const api = new ReloadWebClientAPI({
|
|
964
|
-
name,
|
|
965
|
-
updateMode
|
|
966
|
-
});
|
|
967
|
-
webClient.send(JSON.stringify(api));
|
|
968
|
-
fileLog(`[${ActionKind.NOTIFY_WEB_CLIENT}] Notify ${name} WebClient Succeed`, "Broker", "info");
|
|
969
|
-
} catch (err) {
|
|
970
|
-
const msg = error(err, ActionKind.NOTIFY_WEB_CLIENT, "Broker");
|
|
971
|
-
client.send(msg);
|
|
972
|
-
client.close();
|
|
973
|
-
}
|
|
974
|
-
});
|
|
975
|
-
}
|
|
976
|
-
// app1 consumes provider1, and provider1 not launch. this._tmpSubscriberShelter at this time: {provider1: Map{subscribers: Map{app1: app1+ip+client'}, timestamp: 'xx'} }
|
|
977
|
-
_addTmpSubScriberRelation(subscriber, publisher) {
|
|
978
|
-
const publisherIdentifier = getIdentifier({
|
|
979
|
-
name: publisher.name,
|
|
980
|
-
ip: publisher.ip
|
|
981
|
-
});
|
|
982
|
-
const subscriberIdentifier = subscriber.name;
|
|
983
|
-
const shelter = this._tmpSubscriberShelter.get(publisherIdentifier);
|
|
984
|
-
if (!shelter) {
|
|
985
|
-
const map = /* @__PURE__ */ new Map();
|
|
986
|
-
map.set(subscriberIdentifier, subscriber);
|
|
987
|
-
this._tmpSubscriberShelter.set(publisherIdentifier, {
|
|
988
|
-
subscribers: map,
|
|
989
|
-
timestamp: Date.now()
|
|
990
|
-
});
|
|
991
|
-
fileLog(`[AddTmpSubscriberRelation] ${publisherIdentifier}'s subscriber has ${subscriberIdentifier} `, "Broker", "info");
|
|
992
|
-
return;
|
|
993
|
-
}
|
|
994
|
-
const tmpSubScriberShelterSubscriber = shelter.subscribers.get(subscriberIdentifier);
|
|
995
|
-
if (tmpSubScriberShelterSubscriber) {
|
|
996
|
-
fileLog(`[AddTmpSubscriberRelation] ${publisherIdentifier} and ${subscriberIdentifier} relation has been added`, "Broker", "warn");
|
|
997
|
-
shelter.subscribers.set(subscriberIdentifier, subscriber);
|
|
998
|
-
shelter.timestamp = Date.now();
|
|
999
|
-
} else {
|
|
1000
|
-
fileLog(
|
|
1001
|
-
// eslint-disable-next-line max-len
|
|
1002
|
-
`AddTmpSubscriberLog ${publisherIdentifier}'s shelter has been added, update shelter.subscribers ${subscriberIdentifier}`,
|
|
1003
|
-
"Broker",
|
|
1004
|
-
"warn"
|
|
1005
|
-
);
|
|
1006
|
-
shelter.subscribers.set(subscriberIdentifier, subscriber);
|
|
1007
|
-
}
|
|
1008
|
-
}
|
|
1009
|
-
_getTmpSubScribers(publisherIdentifier) {
|
|
1010
|
-
var _a3;
|
|
1011
|
-
return (_a3 = this._tmpSubscriberShelter.get(publisherIdentifier)) == null ? void 0 : _a3.subscribers;
|
|
1012
|
-
}
|
|
1013
|
-
// after adding publisher, it will change the temp subscriber to regular subscriber
|
|
1014
|
-
_consumeTmpSubScribers(publisher, tmpSubScribers) {
|
|
1015
|
-
tmpSubScribers.forEach((tmpSubScriber, identifier) => {
|
|
1016
|
-
fileLog(`notifyTmpSubScribers ${publisher.name} will be add a subscriber: ${identifier} `, "Broker", "warn");
|
|
1017
|
-
publisher.addSubscriber(identifier, tmpSubScriber.client);
|
|
1018
|
-
publisher.notifySubscriber(identifier, {
|
|
1019
|
-
updateKind: UpdateKind.UPDATE_TYPE,
|
|
1020
|
-
updateMode: UpdateMode.PASSIVE,
|
|
1021
|
-
updateSourcePaths: [
|
|
1022
|
-
publisher.name
|
|
1023
|
-
],
|
|
1024
|
-
remoteTypeTarPath: publisher.remoteTypeTarPath,
|
|
1025
|
-
name: publisher.name
|
|
1026
|
-
});
|
|
1027
|
-
});
|
|
1028
|
-
}
|
|
1029
|
-
_clearTmpSubScriberRelation(identifier) {
|
|
1030
|
-
this._tmpSubscriberShelter.delete(identifier);
|
|
1031
|
-
}
|
|
1032
|
-
_clearTmpSubScriberRelations() {
|
|
1033
|
-
this._tmpSubscriberShelter.clear();
|
|
1034
|
-
}
|
|
1035
|
-
_disconnect() {
|
|
1036
|
-
this._publisherMap.forEach((publisher) => {
|
|
1037
|
-
publisher.close();
|
|
1038
|
-
});
|
|
1039
|
-
}
|
|
1040
|
-
// Every day on 0/6/9/12/15//18, Publishers that have not been connected within 1.5 hours will be cleared regularly.
|
|
1041
|
-
// If process.env.FEDERATION_SERVER_TEST is set, it will be read at a specified time.
|
|
1042
|
-
_setSchedule() {
|
|
1043
|
-
const rule = new import_node_schedule.default.RecurrenceRule();
|
|
1044
|
-
if (Number(process.env["FEDERATION_SERVER_TEST"])) {
|
|
1045
|
-
const interval = Number(process.env["FEDERATION_SERVER_TEST"]) / 1e3;
|
|
1046
|
-
const second = [];
|
|
1047
|
-
for (let i = 0; i < 60; i = i + interval) {
|
|
1048
|
-
second.push(i);
|
|
1049
|
-
}
|
|
1050
|
-
rule.second = second;
|
|
1051
|
-
} else {
|
|
1052
|
-
rule.second = 0;
|
|
1053
|
-
rule.hour = [
|
|
1054
|
-
0,
|
|
1055
|
-
3,
|
|
1056
|
-
6,
|
|
1057
|
-
9,
|
|
1058
|
-
12,
|
|
1059
|
-
15,
|
|
1060
|
-
18
|
|
1061
|
-
];
|
|
1062
|
-
rule.minute = 0;
|
|
1063
|
-
}
|
|
1064
|
-
const serverTest = Number(process.env["FEDERATION_SERVER_TEST"]);
|
|
1065
|
-
this._scheduleJob = import_node_schedule.default.scheduleJob(rule, () => {
|
|
1066
|
-
this._tmpSubscriberShelter.forEach((tmpSubscriber, identifier) => {
|
|
1067
|
-
fileLog(` _clearTmpSubScriberRelation ${identifier}, ${Date.now() - tmpSubscriber.timestamp >= (process.env["GARFISH_MODULE_SERVER_TEST"] ? serverTest : _Broker.DEFAULT_WAITING_TIME)}`, "Broker", "info");
|
|
1068
|
-
if (Date.now() - tmpSubscriber.timestamp >= (process.env["FEDERATION_SERVER_TEST"] ? serverTest : _Broker.DEFAULT_WAITING_TIME)) {
|
|
1069
|
-
this._clearTmpSubScriberRelation(identifier);
|
|
1070
|
-
}
|
|
1071
|
-
});
|
|
1072
|
-
});
|
|
1073
|
-
}
|
|
1074
|
-
_clearSchedule() {
|
|
1075
|
-
if (!this._scheduleJob) {
|
|
1076
|
-
return;
|
|
1077
|
-
}
|
|
1078
|
-
this._scheduleJob.cancel();
|
|
1079
|
-
this._scheduleJob = null;
|
|
1080
|
-
}
|
|
1081
|
-
_stopWhenSIGTERMOrSIGINT() {
|
|
1082
|
-
process.on("SIGTERM", () => {
|
|
1083
|
-
this.exit();
|
|
1084
|
-
});
|
|
1085
|
-
process.on("SIGINT", () => {
|
|
1086
|
-
this.exit();
|
|
1087
|
-
});
|
|
1088
|
-
}
|
|
1089
|
-
_handleUnexpectedExit() {
|
|
1090
|
-
process.on("unhandledRejection", (error2) => {
|
|
1091
|
-
console.error("Unhandled Rejection Error: ", error2);
|
|
1092
|
-
fileLog(`Unhandled Rejection Error: ${error2}`, "Broker", "fatal");
|
|
1093
|
-
process.exit(1);
|
|
1094
|
-
});
|
|
1095
|
-
process.on("uncaughtException", (error2) => {
|
|
1096
|
-
console.error("Unhandled Exception Error: ", error2);
|
|
1097
|
-
fileLog(`Unhandled Rejection Error: ${error2}`, "Broker", "fatal");
|
|
1098
|
-
process.exit(1);
|
|
1099
|
-
});
|
|
1100
|
-
}
|
|
1101
|
-
start() {
|
|
1102
|
-
return __async(this, null, function* () {
|
|
1103
|
-
});
|
|
1104
|
-
}
|
|
1105
|
-
exit() {
|
|
1106
|
-
const brokerExitLog = new BrokerExitLog();
|
|
1107
|
-
this.broadcast(JSON.stringify(brokerExitLog));
|
|
1108
|
-
this._disconnect();
|
|
1109
|
-
this._clearSchedule();
|
|
1110
|
-
this._clearTmpSubScriberRelations();
|
|
1111
|
-
this._webSocketServer && this._webSocketServer.close();
|
|
1112
|
-
this._secureWebSocketServer && this._secureWebSocketServer.close();
|
|
1113
|
-
process.exit(0);
|
|
1114
|
-
}
|
|
1115
|
-
broadcast(message) {
|
|
1116
|
-
var _a3, _b;
|
|
1117
|
-
fileLog(`[broadcast] exit info : ${JSON.stringify(message)}`, "Broker", "warn");
|
|
1118
|
-
(_a3 = this._webSocketServer) == null ? void 0 : _a3.clients.forEach((client) => {
|
|
1119
|
-
client.send(JSON.stringify(message));
|
|
1120
|
-
});
|
|
1121
|
-
(_b = this._secureWebSocketServer) == null ? void 0 : _b.clients.forEach((client) => {
|
|
1122
|
-
client.send(JSON.stringify(message));
|
|
1123
|
-
});
|
|
1124
|
-
}
|
|
1125
|
-
};
|
|
1126
|
-
__name(_Broker, "Broker");
|
|
1127
|
-
__publicField(_Broker, "WEB_SOCKET_CONNECT_MAGIC_ID", WEB_SOCKET_CONNECT_MAGIC_ID);
|
|
1128
|
-
__publicField(_Broker, "DEFAULT_WEB_SOCKET_PORT", DEFAULT_WEB_SOCKET_PORT);
|
|
1129
|
-
__publicField(_Broker, "DEFAULT_SECURE_WEB_SOCKET_PORT", 16324);
|
|
1130
|
-
__publicField(_Broker, "DEFAULT_WAITING_TIME", 1.5 * 60 * 60 * 1e3);
|
|
1131
|
-
var Broker = _Broker;
|
|
1132
|
-
|
|
1133
|
-
// src/server/broker/createBroker.ts
|
|
1134
|
-
var import_child_process = require("child_process");
|
|
1135
|
-
var import_path = __toESM(require("path"));
|
|
1136
|
-
function createBroker() {
|
|
1137
|
-
const startBrokerPath = import_path.default.resolve(__dirname, "./start-broker.js");
|
|
1138
|
-
const sub = (0, import_child_process.fork)(startBrokerPath, [], {
|
|
1139
|
-
detached: true,
|
|
1140
|
-
stdio: "ignore",
|
|
1141
|
-
env: process.env
|
|
1142
|
-
});
|
|
1143
|
-
sub.send("start");
|
|
1144
|
-
sub.unref();
|
|
1145
|
-
return sub;
|
|
1146
|
-
}
|
|
1147
|
-
__name(createBroker, "createBroker");
|
|
1148
|
-
|
|
1149
|
-
// src/server/DevServer.ts
|
|
1150
|
-
var _ModuleFederationDevServer = class _ModuleFederationDevServer {
|
|
1151
|
-
constructor(ctx) {
|
|
1152
|
-
__publicField(this, "_remotes");
|
|
1153
|
-
__publicField(this, "_ip");
|
|
1154
|
-
__publicField(this, "_name");
|
|
1155
|
-
__publicField(this, "_remoteTypeTarPath");
|
|
1156
|
-
__publicField(this, "_publishWebSocket", null);
|
|
1157
|
-
__publicField(this, "_subscriberWebsocketMap", {});
|
|
1158
|
-
__publicField(this, "_reconnect", true);
|
|
1159
|
-
__publicField(this, "_reconnectTimes", 0);
|
|
1160
|
-
__publicField(this, "_isConnected", false);
|
|
1161
|
-
__publicField(this, "_isReconnecting", false);
|
|
1162
|
-
__publicField(this, "_updateCallback", /* @__PURE__ */ __name(() => Promise.resolve(void 0), "_updateCallback"));
|
|
1163
|
-
const { name, remotes, remoteTypeTarPath, updateCallback: updateCallback2 } = ctx;
|
|
1164
|
-
this._ip = getIPV4();
|
|
1165
|
-
this._name = name;
|
|
1166
|
-
this._remotes = remotes;
|
|
1167
|
-
this._remoteTypeTarPath = remoteTypeTarPath;
|
|
1168
|
-
this._updateCallback = updateCallback2;
|
|
1169
|
-
this._stopWhenSIGTERMOrSIGINT();
|
|
1170
|
-
this._handleUnexpectedExit();
|
|
1171
|
-
this._connectPublishToServer();
|
|
1172
|
-
}
|
|
1173
|
-
_connectPublishToServer() {
|
|
1174
|
-
if (!this._reconnect) {
|
|
1175
|
-
return;
|
|
1176
|
-
}
|
|
1177
|
-
fileLog(`Publisher:${this._name} Trying to connect to ws://${this._ip}:${Broker.DEFAULT_WEB_SOCKET_PORT}...`, MF_SERVER_IDENTIFIER, "info");
|
|
1178
|
-
this._publishWebSocket = new import_isomorphic_ws2.default(`ws://${this._ip}:${Broker.DEFAULT_WEB_SOCKET_PORT}?WEB_SOCKET_CONNECT_MAGIC_ID=${Broker.WEB_SOCKET_CONNECT_MAGIC_ID}`);
|
|
1179
|
-
this._publishWebSocket.on("open", () => {
|
|
1180
|
-
var _a3;
|
|
1181
|
-
fileLog(`Current pid: ${process.pid}, publisher:${this._name} connected to ws://${this._ip}:${Broker.DEFAULT_WEB_SOCKET_PORT}, starting service...`, MF_SERVER_IDENTIFIER, "info");
|
|
1182
|
-
this._isConnected = true;
|
|
1183
|
-
const addPublisherAction = new AddPublisherAction({
|
|
1184
|
-
name: this._name,
|
|
1185
|
-
ip: this._ip,
|
|
1186
|
-
remoteTypeTarPath: this._remoteTypeTarPath
|
|
1187
|
-
});
|
|
1188
|
-
(_a3 = this._publishWebSocket) == null ? void 0 : _a3.send(JSON.stringify(addPublisherAction));
|
|
1189
|
-
this._connectSubscribers();
|
|
1190
|
-
});
|
|
1191
|
-
this._publishWebSocket.on("message", (message) => __async(this, null, function* () {
|
|
1192
|
-
var _a3, _b;
|
|
1193
|
-
try {
|
|
1194
|
-
const parsedMessage = JSON.parse(message.toString());
|
|
1195
|
-
if (parsedMessage.type === "Log") {
|
|
1196
|
-
if (parsedMessage.kind === LogKind.BrokerExitLog) {
|
|
1197
|
-
fileLog(`Receive broker exit signal, ${this._name} service will exit...`, MF_SERVER_IDENTIFIER, "warn");
|
|
1198
|
-
this._exit();
|
|
1199
|
-
}
|
|
1200
|
-
}
|
|
1201
|
-
if (parsedMessage.type === "API") {
|
|
1202
|
-
if (parsedMessage.kind === APIKind.FETCH_TYPES) {
|
|
1203
|
-
const { payload: { remoteInfo } } = parsedMessage;
|
|
1204
|
-
fileLog(`${this._name} Receive broker FETCH_TYPES, payload as follows: ${JSON.stringify(remoteInfo, null, 2)}.`, MF_SERVER_IDENTIFIER, "info");
|
|
1205
|
-
yield this.fetchDynamicRemoteTypes({
|
|
1206
|
-
remoteInfo
|
|
1207
|
-
});
|
|
1208
|
-
}
|
|
1209
|
-
}
|
|
1210
|
-
} catch (err) {
|
|
1211
|
-
console.error(err);
|
|
1212
|
-
const exitPublisher = new ExitPublisherAction({
|
|
1213
|
-
name: this._name,
|
|
1214
|
-
ip: this._ip
|
|
1215
|
-
});
|
|
1216
|
-
const exitSubscriber = new ExitSubscriberAction({
|
|
1217
|
-
name: this._name,
|
|
1218
|
-
ip: this._ip,
|
|
1219
|
-
publishers: this._remotes.map((remote) => ({
|
|
1220
|
-
name: remote.name,
|
|
1221
|
-
ip: remote.ip
|
|
1222
|
-
}))
|
|
1223
|
-
});
|
|
1224
|
-
(_a3 = this._publishWebSocket) == null ? void 0 : _a3.send(JSON.stringify(exitPublisher));
|
|
1225
|
-
(_b = this._publishWebSocket) == null ? void 0 : _b.send(JSON.stringify(exitSubscriber));
|
|
1226
|
-
fileLog("Parse messages error, ModuleFederationDevServer will exit...", MF_SERVER_IDENTIFIER, "fatal");
|
|
1227
|
-
this._exit();
|
|
1228
|
-
}
|
|
1229
|
-
}));
|
|
1230
|
-
this._publishWebSocket.on("close", (code) => {
|
|
1231
|
-
fileLog(`Connection closed with code ${code}.`, MF_SERVER_IDENTIFIER, "warn");
|
|
1232
|
-
this._publishWebSocket && this._publishWebSocket.close();
|
|
1233
|
-
this._publishWebSocket = null;
|
|
1234
|
-
if (!this._reconnect) {
|
|
1235
|
-
return;
|
|
1236
|
-
}
|
|
1237
|
-
const reconnectTime = fib(++this._reconnectTimes);
|
|
1238
|
-
fileLog(`start reconnecting to server after ${reconnectTime}s.`, MF_SERVER_IDENTIFIER, "info");
|
|
1239
|
-
setTimeout(() => this._connectPublishToServer(), reconnectTime * 1e3);
|
|
1240
|
-
});
|
|
1241
|
-
this._publishWebSocket.on("error", this._tryCreateBackgroundBroker.bind(this));
|
|
1242
|
-
}
|
|
1243
|
-
// Associate the remotes(Subscriber) to the Broker
|
|
1244
|
-
_connectSubscriberToServer(remote) {
|
|
1245
|
-
const { name, ip } = remote;
|
|
1246
|
-
fileLog(`remote module:${name} trying to connect to ws://${ip}:${Broker.DEFAULT_WEB_SOCKET_PORT}...`, MF_SERVER_IDENTIFIER, "info");
|
|
1247
|
-
const identifier = getIdentifier({
|
|
1248
|
-
name,
|
|
1249
|
-
ip
|
|
1250
|
-
});
|
|
1251
|
-
this._subscriberWebsocketMap[identifier] = new import_isomorphic_ws2.default(`ws://${ip}:${Broker.DEFAULT_WEB_SOCKET_PORT}?WEB_SOCKET_CONNECT_MAGIC_ID=${Broker.WEB_SOCKET_CONNECT_MAGIC_ID}`);
|
|
1252
|
-
this._subscriberWebsocketMap[identifier].on("open", () => {
|
|
1253
|
-
fileLog(`Current pid: ${process.pid} remote module: ${name} connected to ws://${ip}:${Broker.DEFAULT_WEB_SOCKET_PORT}, starting service...`, MF_SERVER_IDENTIFIER, "info");
|
|
1254
|
-
const addSubscriber = new AddSubscriberAction({
|
|
1255
|
-
name: this._name,
|
|
1256
|
-
ip: this._ip,
|
|
1257
|
-
publishers: [
|
|
1258
|
-
{
|
|
1259
|
-
name,
|
|
1260
|
-
ip
|
|
1261
|
-
}
|
|
1262
|
-
]
|
|
1263
|
-
});
|
|
1264
|
-
this._subscriberWebsocketMap[identifier].send(JSON.stringify(addSubscriber));
|
|
1265
|
-
});
|
|
1266
|
-
this._subscriberWebsocketMap[identifier].on("message", (message) => __async(this, null, function* () {
|
|
1267
|
-
try {
|
|
1268
|
-
const parsedMessage = JSON.parse(message.toString());
|
|
1269
|
-
if (parsedMessage.type === "Log") {
|
|
1270
|
-
if (parsedMessage.kind === LogKind.BrokerExitLog) {
|
|
1271
|
-
fileLog(`${identifier}'s Server exit, thus ${identifier} will no longer has reload ability.`, MF_SERVER_IDENTIFIER, "warn");
|
|
1272
|
-
this._exit();
|
|
1273
|
-
}
|
|
1274
|
-
}
|
|
1275
|
-
if (parsedMessage.type === "API") {
|
|
1276
|
-
if (parsedMessage.kind === APIKind.UPDATE_SUBSCRIBER) {
|
|
1277
|
-
const { payload: { updateKind, updateSourcePaths, name: subscribeName, remoteTypeTarPath, updateMode } } = parsedMessage;
|
|
1278
|
-
yield this._updateSubscriber({
|
|
1279
|
-
remoteTypeTarPath,
|
|
1280
|
-
name: subscribeName,
|
|
1281
|
-
updateKind,
|
|
1282
|
-
updateMode,
|
|
1283
|
-
updateSourcePaths
|
|
1284
|
-
});
|
|
1285
|
-
}
|
|
1286
|
-
}
|
|
1287
|
-
} catch (err) {
|
|
1288
|
-
console.error(err);
|
|
1289
|
-
const exitSubscriber = new ExitSubscriberAction({
|
|
1290
|
-
name: this._name,
|
|
1291
|
-
ip: this._ip,
|
|
1292
|
-
publishers: [
|
|
1293
|
-
{
|
|
1294
|
-
name,
|
|
1295
|
-
ip
|
|
1296
|
-
}
|
|
1297
|
-
]
|
|
1298
|
-
});
|
|
1299
|
-
this._subscriberWebsocketMap[identifier].send(JSON.stringify(exitSubscriber));
|
|
1300
|
-
fileLog(`${identifier} exit,
|
|
1301
|
-
error: ${err instanceof Error ? err.toString() : JSON.stringify(err)}
|
|
1302
|
-
`, MF_SERVER_IDENTIFIER, "warn");
|
|
1303
|
-
}
|
|
1304
|
-
}));
|
|
1305
|
-
this._subscriberWebsocketMap[identifier].on("close", (code) => {
|
|
1306
|
-
var _a3;
|
|
1307
|
-
fileLog(`Connection closed with code ${code}.`, MF_SERVER_IDENTIFIER, "warn");
|
|
1308
|
-
(_a3 = this._subscriberWebsocketMap[identifier]) == null ? void 0 : _a3.close();
|
|
1309
|
-
delete this._subscriberWebsocketMap[identifier];
|
|
1310
|
-
});
|
|
1311
|
-
this._subscriberWebsocketMap[identifier].on("error", (err) => {
|
|
1312
|
-
var _a3;
|
|
1313
|
-
if ("code" in err && err.code === "ETIMEDOUT") {
|
|
1314
|
-
fileLog(`Can not connect ${JSON.stringify(remote)}, please make sure this remote is started locally.`, MF_SERVER_IDENTIFIER, "warn");
|
|
1315
|
-
} else {
|
|
1316
|
-
console.error(err);
|
|
1317
|
-
}
|
|
1318
|
-
(_a3 = this._subscriberWebsocketMap[identifier]) == null ? void 0 : _a3.close();
|
|
1319
|
-
delete this._subscriberWebsocketMap[identifier];
|
|
1320
|
-
});
|
|
1321
|
-
}
|
|
1322
|
-
_connectSubscribers() {
|
|
1323
|
-
this._remotes.forEach((remote) => {
|
|
1324
|
-
this._connectSubscriberToServer(remote);
|
|
1325
|
-
});
|
|
1326
|
-
}
|
|
1327
|
-
// app1 consumes provider1. And the function will be triggered when provider1 code change.
|
|
1328
|
-
_updateSubscriber(options) {
|
|
1329
|
-
return __async(this, null, function* () {
|
|
1330
|
-
var _a3;
|
|
1331
|
-
const { updateMode, updateKind, updateSourcePaths, name, remoteTypeTarPath, remoteInfo } = options;
|
|
1332
|
-
fileLog(
|
|
1333
|
-
// eslint-disable-next-line max-len
|
|
1334
|
-
`[_updateSubscriber] run, options: ${JSON.stringify(options, null, 2)}`,
|
|
1335
|
-
MF_SERVER_IDENTIFIER,
|
|
1336
|
-
"warn"
|
|
1337
|
-
);
|
|
1338
|
-
if (updateMode === UpdateMode.PASSIVE && updateSourcePaths.includes(this._name)) {
|
|
1339
|
-
fileLog(
|
|
1340
|
-
// eslint-disable-next-line max-len
|
|
1341
|
-
`[_updateSubscriber] run, updateSourcePaths:${updateSourcePaths} includes ${this._name}, update ignore!`,
|
|
1342
|
-
MF_SERVER_IDENTIFIER,
|
|
1343
|
-
"warn"
|
|
1344
|
-
);
|
|
1345
|
-
return;
|
|
1346
|
-
}
|
|
1347
|
-
if (updateSourcePaths.slice(-1)[0] === this._name) {
|
|
1348
|
-
fileLog(`[_updateSubscriber] run, updateSourcePaths:${updateSourcePaths} ends is ${this._name}, update ignore!`, MF_SERVER_IDENTIFIER, "warn");
|
|
1349
|
-
return;
|
|
1350
|
-
}
|
|
1351
|
-
fileLog(
|
|
1352
|
-
// eslint-disable-next-line max-len
|
|
1353
|
-
`[_updateSubscriber] run, updateSourcePaths:${updateSourcePaths}, current module:${this._name}, update start...`,
|
|
1354
|
-
MF_SERVER_IDENTIFIER,
|
|
1355
|
-
"info"
|
|
1356
|
-
);
|
|
1357
|
-
yield this._updateCallback({
|
|
1358
|
-
name,
|
|
1359
|
-
updateMode,
|
|
1360
|
-
updateKind,
|
|
1361
|
-
updateSourcePaths,
|
|
1362
|
-
remoteTypeTarPath,
|
|
1363
|
-
remoteInfo
|
|
1364
|
-
});
|
|
1365
|
-
const newUpdateSourcePaths = updateSourcePaths.concat(this._name);
|
|
1366
|
-
const updatePublisher = new UpdatePublisherAction({
|
|
1367
|
-
name: this._name,
|
|
1368
|
-
ip: this._ip,
|
|
1369
|
-
updateMode: UpdateMode.PASSIVE,
|
|
1370
|
-
updateKind,
|
|
1371
|
-
updateSourcePaths: newUpdateSourcePaths,
|
|
1372
|
-
remoteTypeTarPath: this._remoteTypeTarPath
|
|
1373
|
-
});
|
|
1374
|
-
fileLog(
|
|
1375
|
-
// eslint-disable-next-line max-len
|
|
1376
|
-
`[_updateSubscriber] run, updateSourcePaths:${newUpdateSourcePaths}, update publisher ${this._name} start...`,
|
|
1377
|
-
MF_SERVER_IDENTIFIER,
|
|
1378
|
-
"info"
|
|
1379
|
-
);
|
|
1380
|
-
(_a3 = this._publishWebSocket) == null ? void 0 : _a3.send(JSON.stringify(updatePublisher));
|
|
1381
|
-
});
|
|
1382
|
-
}
|
|
1383
|
-
_tryCreateBackgroundBroker(err) {
|
|
1384
|
-
if (!(((err == null ? void 0 : err.code) === "ECONNREFUSED" || (err == null ? void 0 : err.code) === "ETIMEDOUT") && err.port === Broker.DEFAULT_WEB_SOCKET_PORT)) {
|
|
1385
|
-
fileLog(`websocket error: ${err.stack}`, MF_SERVER_IDENTIFIER, "fatal");
|
|
1386
|
-
return;
|
|
1387
|
-
}
|
|
1388
|
-
fileLog(`Failed to connect to ws://${this._ip}:${Broker.DEFAULT_WEB_SOCKET_PORT}...`, MF_SERVER_IDENTIFIER, "fatal");
|
|
1389
|
-
this._isReconnecting = true;
|
|
1390
|
-
setTimeout(() => {
|
|
1391
|
-
this._isReconnecting = false;
|
|
1392
|
-
if (this._reconnect === false) {
|
|
1393
|
-
return;
|
|
1394
|
-
}
|
|
1395
|
-
fileLog("Creating new background broker...", MF_SERVER_IDENTIFIER, "warn");
|
|
1396
|
-
const broker = createBroker();
|
|
1397
|
-
broker.on("message", (message) => {
|
|
1398
|
-
if (message === "ready") {
|
|
1399
|
-
fileLog("background broker started.", MF_SERVER_IDENTIFIER, "info");
|
|
1400
|
-
this._reconnectTimes = 1;
|
|
1401
|
-
if (process.send) {
|
|
1402
|
-
process.send("ready");
|
|
1403
|
-
}
|
|
1404
|
-
}
|
|
1405
|
-
});
|
|
1406
|
-
}, Math.ceil(100 * Math.random()));
|
|
1407
|
-
}
|
|
1408
|
-
_stopWhenSIGTERMOrSIGINT() {
|
|
1409
|
-
process.on("SIGTERM", () => {
|
|
1410
|
-
fileLog(`Process(${process.pid}) SIGTERM, ModuleFederationDevServer will exit...`, MF_SERVER_IDENTIFIER, "warn");
|
|
1411
|
-
this._exit();
|
|
1412
|
-
});
|
|
1413
|
-
process.on("SIGINT", () => {
|
|
1414
|
-
fileLog(`Process(${process.pid}) SIGINT, ModuleFederationDevServer will exit...`, MF_SERVER_IDENTIFIER, "warn");
|
|
1415
|
-
this._exit();
|
|
1416
|
-
});
|
|
1417
|
-
}
|
|
1418
|
-
_handleUnexpectedExit() {
|
|
1419
|
-
process.on("unhandledRejection", (error2) => {
|
|
1420
|
-
if (this._isReconnecting) {
|
|
1421
|
-
return;
|
|
1422
|
-
}
|
|
1423
|
-
console.error("Unhandled Rejection Error: ", error2);
|
|
1424
|
-
fileLog(`Process(${process.pid}) unhandledRejection, garfishModuleServer will exit...`, MF_SERVER_IDENTIFIER, "error");
|
|
1425
|
-
this._exit();
|
|
1426
|
-
});
|
|
1427
|
-
process.on("uncaughtException", (error2) => {
|
|
1428
|
-
if (this._isReconnecting) {
|
|
1429
|
-
return;
|
|
1430
|
-
}
|
|
1431
|
-
console.error("Unhandled Exception Error: ", error2);
|
|
1432
|
-
fileLog(`Process(${process.pid}) uncaughtException, garfishModuleServer will exit...`, MF_SERVER_IDENTIFIER, "error");
|
|
1433
|
-
this._exit();
|
|
1434
|
-
});
|
|
1435
|
-
}
|
|
1436
|
-
_exit() {
|
|
1437
|
-
this._reconnect = false;
|
|
1438
|
-
if (this._publishWebSocket) {
|
|
1439
|
-
const exitPublisher = new ExitPublisherAction({
|
|
1440
|
-
name: this._name,
|
|
1441
|
-
ip: this._ip
|
|
1442
|
-
});
|
|
1443
|
-
this._publishWebSocket.send(JSON.stringify(exitPublisher));
|
|
1444
|
-
this._publishWebSocket.on("message", (message) => {
|
|
1445
|
-
const parsedMessage = JSON.parse(message.toString());
|
|
1446
|
-
fileLog(`[${parsedMessage.kind}]: ${JSON.stringify(parsedMessage)}`, MF_SERVER_IDENTIFIER, "info");
|
|
1447
|
-
});
|
|
1448
|
-
}
|
|
1449
|
-
if (this._publishWebSocket) {
|
|
1450
|
-
this._publishWebSocket.close();
|
|
1451
|
-
this._publishWebSocket = null;
|
|
1452
|
-
}
|
|
1453
|
-
process.exit(0);
|
|
1454
|
-
}
|
|
1455
|
-
exit() {
|
|
1456
|
-
this._exit();
|
|
1457
|
-
}
|
|
1458
|
-
update(options) {
|
|
1459
|
-
if (!this._publishWebSocket || !this._isConnected) {
|
|
1460
|
-
return;
|
|
1461
|
-
}
|
|
1462
|
-
const { updateKind, updateMode, updateSourcePaths, clientName } = options;
|
|
1463
|
-
fileLog(`update run, ${this._name} module update, updateKind: ${updateKind}, updateMode: ${updateMode}, updateSourcePaths: ${updateSourcePaths}`, MF_SERVER_IDENTIFIER, "info");
|
|
1464
|
-
if (updateKind === UpdateKind.RELOAD_PAGE) {
|
|
1465
|
-
const notifyWebClient = new NotifyWebClientAction({
|
|
1466
|
-
name: clientName || this._name,
|
|
1467
|
-
updateMode
|
|
1468
|
-
});
|
|
1469
|
-
this._publishWebSocket.send(JSON.stringify(notifyWebClient));
|
|
1470
|
-
return;
|
|
1471
|
-
}
|
|
1472
|
-
const updatePublisher = new UpdatePublisherAction({
|
|
1473
|
-
name: this._name,
|
|
1474
|
-
ip: this._ip,
|
|
1475
|
-
updateMode,
|
|
1476
|
-
updateKind,
|
|
1477
|
-
updateSourcePaths: [
|
|
1478
|
-
this._name
|
|
1479
|
-
],
|
|
1480
|
-
remoteTypeTarPath: this._remoteTypeTarPath
|
|
1481
|
-
});
|
|
1482
|
-
this._publishWebSocket.send(JSON.stringify(updatePublisher));
|
|
1483
|
-
}
|
|
1484
|
-
fetchDynamicRemoteTypes(options) {
|
|
1485
|
-
return __async(this, null, function* () {
|
|
1486
|
-
const { remoteInfo, once } = options;
|
|
1487
|
-
const updateMode = UpdateMode.PASSIVE;
|
|
1488
|
-
const updateKind = UpdateKind.UPDATE_TYPE;
|
|
1489
|
-
fileLog(`fetchDynamicRemoteTypes: remoteInfo: ${JSON.stringify(remoteInfo)}`, MF_SERVER_IDENTIFIER, "info");
|
|
1490
|
-
yield this._updateCallback({
|
|
1491
|
-
name: this._name,
|
|
1492
|
-
updateMode,
|
|
1493
|
-
updateKind,
|
|
1494
|
-
updateSourcePaths: [],
|
|
1495
|
-
remoteTypeTarPath: "",
|
|
1496
|
-
remoteInfo,
|
|
1497
|
-
once
|
|
1498
|
-
});
|
|
1499
|
-
const updatePublisher = new UpdatePublisherAction({
|
|
1500
|
-
name: this._name,
|
|
1501
|
-
ip: this._ip,
|
|
1502
|
-
updateMode,
|
|
1503
|
-
updateKind,
|
|
1504
|
-
updateSourcePaths: [
|
|
1505
|
-
this._name
|
|
1506
|
-
],
|
|
1507
|
-
remoteTypeTarPath: this._remoteTypeTarPath
|
|
1508
|
-
});
|
|
1509
|
-
this._publishWebSocket.send(JSON.stringify(updatePublisher));
|
|
1510
|
-
});
|
|
1511
|
-
}
|
|
1512
|
-
};
|
|
1513
|
-
__name(_ModuleFederationDevServer, "ModuleFederationDevServer");
|
|
1514
|
-
var ModuleFederationDevServer = _ModuleFederationDevServer;
|
|
1515
|
-
|
|
1516
|
-
// src/server/createKoaServer.ts
|
|
1517
|
-
var import_fs_extra = __toESM(require("fs-extra"));
|
|
1518
|
-
var import_koa = __toESM(require("koa"));
|
|
1519
|
-
function createKoaServer(options) {
|
|
1520
|
-
return __async(this, null, function* () {
|
|
1521
|
-
const { typeTarPath } = options;
|
|
1522
|
-
const freeport = yield getFreePort();
|
|
1523
|
-
const app = new import_koa.default();
|
|
1524
|
-
app.use((ctx, next) => __async(this, null, function* () {
|
|
1525
|
-
if (ctx.path === `/${DEFAULT_TAR_NAME}`) {
|
|
1526
|
-
ctx.status = 200;
|
|
1527
|
-
ctx.body = import_fs_extra.default.createReadStream(typeTarPath);
|
|
1528
|
-
ctx.response.type = "application/x-gzip";
|
|
1529
|
-
} else {
|
|
1530
|
-
yield next();
|
|
1531
|
-
}
|
|
1532
|
-
}));
|
|
1533
|
-
app.listen(freeport);
|
|
1534
|
-
return {
|
|
1535
|
-
server: app,
|
|
1536
|
-
serverAddress: `http://${getIPV4()}:${freeport}`
|
|
1537
|
-
};
|
|
1538
|
-
});
|
|
1539
|
-
}
|
|
1540
|
-
__name(createKoaServer, "createKoaServer");
|
|
1541
|
-
|
|
1542
|
-
// src/core/lib/typeScriptCompiler.ts
|
|
1543
|
-
var STARTS_WITH_SLASH = /^\//;
|
|
1544
|
-
var DEFINITION_FILE_EXTENSION = ".d.ts";
|
|
1545
|
-
var retrieveMfTypesPath = /* @__PURE__ */ __name((tsConfig, remoteOptions) => (0, import_path2.normalize)(tsConfig.compilerOptions.outDir.replace(remoteOptions.compiledTypesFolder, "")), "retrieveMfTypesPath");
|
|
1546
|
-
var retrieveOriginalOutDir = /* @__PURE__ */ __name((tsConfig, remoteOptions) => (0, import_path2.normalize)(tsConfig.compilerOptions.outDir.replace(remoteOptions.compiledTypesFolder, "").replace(remoteOptions.typesFolder, "")), "retrieveOriginalOutDir");
|
|
1547
|
-
var retrieveMfAPITypesPath = /* @__PURE__ */ __name((tsConfig, remoteOptions) => (0, import_path2.join)(retrieveOriginalOutDir(tsConfig, remoteOptions), `${remoteOptions.typesFolder}.d.ts`), "retrieveMfAPITypesPath");
|
|
1548
|
-
function writeTempTsConfig(tsConfig, context, name, cwd) {
|
|
1549
|
-
const createHash = /* @__PURE__ */ __name((contents) => {
|
|
1550
|
-
return import_crypto.default.createHash("md5").update(contents).digest("hex");
|
|
1551
|
-
}, "createHash");
|
|
1552
|
-
const hash = createHash(`${JSON.stringify(tsConfig)}${name}${Date.now()}`);
|
|
1553
|
-
const tempTsConfigJsonPath = (0, import_path2.resolve)(cwd != null ? cwd : context, "node_modules", import_sdk3.TEMP_DIR, `tsconfig.${hash}.json`);
|
|
1554
|
-
(0, import_fs_extra2.ensureDirSync)((0, import_path2.dirname)(tempTsConfigJsonPath));
|
|
1555
|
-
(0, import_fs_extra2.writeFileSync)(tempTsConfigJsonPath, JSON.stringify(tsConfig, null, 2));
|
|
1556
|
-
return tempTsConfigJsonPath;
|
|
1557
|
-
}
|
|
1558
|
-
__name(writeTempTsConfig, "writeTempTsConfig");
|
|
1559
|
-
var removeExt = /* @__PURE__ */ __name((f) => {
|
|
1560
|
-
const vueExt = ".vue";
|
|
1561
|
-
const ext = (0, import_path2.extname)(f);
|
|
1562
|
-
if (ext === vueExt) {
|
|
1563
|
-
return f;
|
|
1564
|
-
}
|
|
1565
|
-
const regexPattern = new RegExp(`\\${ext}$`);
|
|
1566
|
-
return f.replace(regexPattern, "");
|
|
1567
|
-
}, "removeExt");
|
|
1568
|
-
function getExposeKey(options) {
|
|
1569
|
-
const { filePath, rootDir, outDir, mapExposeToEntry } = options;
|
|
1570
|
-
const relativeFilePath = (0, import_path2.relative)(outDir, filePath.replace(new RegExp(`\\.d.ts$`), ""));
|
|
1571
|
-
return mapExposeToEntry[relativeFilePath];
|
|
1572
|
-
}
|
|
1573
|
-
__name(getExposeKey, "getExposeKey");
|
|
1574
|
-
var processTypesFile = /* @__PURE__ */ __name((options) => __async(void 0, null, function* () {
|
|
1575
|
-
const { outDir, filePath, rootDir, cb, mapExposeToEntry, mfTypePath } = options;
|
|
1576
|
-
if (!(0, import_fs_extra2.existsSync)(filePath)) {
|
|
1577
|
-
return;
|
|
1578
|
-
}
|
|
1579
|
-
const stats = yield (0, import_promises.stat)(filePath);
|
|
1580
|
-
if (stats.isDirectory()) {
|
|
1581
|
-
const files = yield (0, import_promises.readdir)(filePath);
|
|
1582
|
-
yield Promise.all(files.map((file) => processTypesFile(__spreadProps(__spreadValues({}, options), {
|
|
1583
|
-
filePath: (0, import_path2.join)(filePath, file)
|
|
1584
|
-
}))));
|
|
1585
|
-
} else if (filePath.endsWith(".d.ts")) {
|
|
1586
|
-
const exposeKey = getExposeKey({
|
|
1587
|
-
filePath,
|
|
1588
|
-
rootDir,
|
|
1589
|
-
outDir,
|
|
1590
|
-
mapExposeToEntry
|
|
1591
|
-
});
|
|
1592
|
-
if (exposeKey) {
|
|
1593
|
-
const sourceEntry = exposeKey === "." ? "index" : exposeKey;
|
|
1594
|
-
const mfeTypeEntry = (0, import_path2.join)(mfTypePath, `${sourceEntry}${DEFINITION_FILE_EXTENSION}`);
|
|
1595
|
-
const mfeTypeEntryDirectory = (0, import_path2.dirname)(mfeTypeEntry);
|
|
1596
|
-
const relativePathToOutput = (0, import_path2.relative)(mfeTypeEntryDirectory, filePath).replace(DEFINITION_FILE_EXTENSION, "").replace(STARTS_WITH_SLASH, "").split(import_path2.sep).join("/");
|
|
1597
|
-
(0, import_fs_extra2.ensureDirSync)(mfeTypeEntryDirectory);
|
|
1598
|
-
yield (0, import_promises.writeFile)(mfeTypeEntry, `export * from './${relativePathToOutput}';
|
|
1599
|
-
export { default } from './${relativePathToOutput}';`);
|
|
1600
|
-
}
|
|
1601
|
-
const content = yield (0, import_promises.readFile)(filePath, "utf8");
|
|
1602
|
-
cb(content);
|
|
1603
|
-
}
|
|
1604
|
-
}), "processTypesFile");
|
|
1605
|
-
var getPMFromUserAgent = /* @__PURE__ */ __name(() => {
|
|
1606
|
-
const userAgent = process.env["npm_config_user_agent"];
|
|
1607
|
-
if (userAgent == null) {
|
|
1608
|
-
return "null";
|
|
1609
|
-
}
|
|
1610
|
-
const name = userAgent.split("/")[0];
|
|
1611
|
-
return name;
|
|
1612
|
-
}, "getPMFromUserAgent");
|
|
1613
|
-
var resolvePackageManagerExecutable = /* @__PURE__ */ __name(() => {
|
|
1614
|
-
const pm = getPMFromUserAgent();
|
|
1615
|
-
switch (pm) {
|
|
1616
|
-
case "yarn":
|
|
1617
|
-
return "yarn";
|
|
1618
|
-
case "npm":
|
|
1619
|
-
case "pnpm":
|
|
1620
|
-
default:
|
|
1621
|
-
return "npx";
|
|
1622
|
-
}
|
|
1623
|
-
}, "resolvePackageManagerExecutable");
|
|
1624
|
-
var compileTs = /* @__PURE__ */ __name((mapComponentsToExpose, tsConfig, remoteOptions) => __async(void 0, null, function* () {
|
|
1625
|
-
var _a3, _b, _c, _d;
|
|
1626
|
-
if (!Object.keys(mapComponentsToExpose).length) {
|
|
1627
|
-
return;
|
|
1628
|
-
}
|
|
1629
|
-
const { compilerOptions } = tsConfig;
|
|
1630
|
-
const tempTsConfigJsonPath = writeTempTsConfig(tsConfig, remoteOptions.context, remoteOptions.moduleFederationConfig.name || "mf", typeof remoteOptions.moduleFederationConfig.dts !== "boolean" ? (_b = (_a3 = remoteOptions.moduleFederationConfig.dts) == null ? void 0 : _a3.cwd) != null ? _b : void 0 : void 0);
|
|
1631
|
-
logger.debug(`tempTsConfigJsonPath: ${tempTsConfigJsonPath}`);
|
|
1632
|
-
try {
|
|
1633
|
-
const mfTypePath = retrieveMfTypesPath(tsConfig, remoteOptions);
|
|
1634
|
-
const thirdPartyExtractor = new import_third_party_dts_extractor.ThirdPartyExtractor({
|
|
1635
|
-
destDir: (0, import_path2.resolve)(mfTypePath, "node_modules"),
|
|
1636
|
-
context: remoteOptions.context,
|
|
1637
|
-
exclude: typeof remoteOptions.extractThirdParty === "object" ? remoteOptions.extractThirdParty.exclude : void 0
|
|
1638
|
-
});
|
|
1639
|
-
const execPromise = import_util.default.promisify(import_child_process2.exec);
|
|
1640
|
-
const pmExecutable = resolvePackageManagerExecutable();
|
|
1641
|
-
const cmd = `${pmExecutable} ${remoteOptions.compilerInstance} --project '${tempTsConfigJsonPath}'`;
|
|
1642
|
-
try {
|
|
1643
|
-
yield execPromise(cmd, {
|
|
1644
|
-
cwd: typeof remoteOptions.moduleFederationConfig.dts !== "boolean" ? (_d = (_c = remoteOptions.moduleFederationConfig.dts) == null ? void 0 : _c.cwd) != null ? _d : void 0 : void 0
|
|
1645
|
-
});
|
|
1646
|
-
} catch (err) {
|
|
1647
|
-
if (compilerOptions.tsBuildInfoFile) {
|
|
1648
|
-
try {
|
|
1649
|
-
yield (0, import_promises.rm)(compilerOptions.tsBuildInfoFile);
|
|
1650
|
-
} catch (e) {
|
|
1651
|
-
}
|
|
1652
|
-
}
|
|
1653
|
-
throw new Error((0, import_error_codes.getShortErrorMsg)(import_error_codes.TYPE_001, import_error_codes.typeDescMap, {
|
|
1654
|
-
cmd
|
|
1655
|
-
}));
|
|
1656
|
-
}
|
|
1657
|
-
const mapExposeToEntry = Object.fromEntries(Object.entries(mapComponentsToExpose).map(([exposed, filename]) => {
|
|
1658
|
-
const normalizedFileName = (0, import_path2.normalize)(filename);
|
|
1659
|
-
let relativeFileName = "";
|
|
1660
|
-
if ((0, import_path2.isAbsolute)(normalizedFileName)) {
|
|
1661
|
-
relativeFileName = (0, import_path2.relative)(tsConfig.compilerOptions.rootDir, normalizedFileName);
|
|
1662
|
-
} else {
|
|
1663
|
-
relativeFileName = (0, import_path2.relative)(tsConfig.compilerOptions.rootDir, (0, import_path2.resolve)(remoteOptions.context, normalizedFileName));
|
|
1664
|
-
}
|
|
1665
|
-
return [
|
|
1666
|
-
removeExt(relativeFileName),
|
|
1667
|
-
exposed
|
|
1668
|
-
];
|
|
1669
|
-
}));
|
|
1670
|
-
const cb = remoteOptions.extractThirdParty ? thirdPartyExtractor.collectPkgs.bind(thirdPartyExtractor) : () => void 0;
|
|
1671
|
-
yield processTypesFile({
|
|
1672
|
-
outDir: compilerOptions.outDir,
|
|
1673
|
-
filePath: compilerOptions.outDir,
|
|
1674
|
-
rootDir: compilerOptions.rootDir,
|
|
1675
|
-
mfTypePath,
|
|
1676
|
-
cb,
|
|
1677
|
-
mapExposeToEntry
|
|
1678
|
-
});
|
|
1679
|
-
if (remoteOptions.extractThirdParty) {
|
|
1680
|
-
yield thirdPartyExtractor.copyDts();
|
|
1681
|
-
}
|
|
1682
|
-
if (remoteOptions.deleteTsConfig) {
|
|
1683
|
-
yield (0, import_promises.rm)(tempTsConfigJsonPath);
|
|
1684
|
-
}
|
|
1685
|
-
} catch (err) {
|
|
1686
|
-
throw err;
|
|
1687
|
-
}
|
|
1688
|
-
}), "compileTs");
|
|
1689
|
-
|
|
1690
|
-
// src/core/lib/archiveHandler.ts
|
|
1691
|
-
var retrieveTypesZipPath = /* @__PURE__ */ __name((mfTypesPath, remoteOptions) => (0, import_path3.join)(mfTypesPath.replace(remoteOptions.typesFolder, ""), `${remoteOptions.typesFolder}.zip`), "retrieveTypesZipPath");
|
|
1692
|
-
var createTypesArchive = /* @__PURE__ */ __name((tsConfig, remoteOptions) => __async(void 0, null, function* () {
|
|
1693
|
-
const mfTypesPath = retrieveMfTypesPath(tsConfig, remoteOptions);
|
|
1694
|
-
const zip = new import_adm_zip.default();
|
|
1695
|
-
zip.addLocalFolder(mfTypesPath);
|
|
1696
|
-
return zip.writeZipPromise(retrieveTypesZipPath(mfTypesPath, remoteOptions));
|
|
1697
|
-
}), "createTypesArchive");
|
|
1698
|
-
var downloadErrorLogger = /* @__PURE__ */ __name((destinationFolder, fileToDownload) => (reason) => {
|
|
1699
|
-
throw __spreadProps(__spreadValues({}, reason), {
|
|
1700
|
-
message: `Network error: Unable to download federated mocks for '${destinationFolder}' from '${fileToDownload}' because '${reason.message}'`
|
|
1701
|
-
});
|
|
1702
|
-
}, "downloadErrorLogger");
|
|
1703
|
-
var retrieveTypesArchiveDestinationPath = /* @__PURE__ */ __name((hostOptions, destinationFolder) => {
|
|
1704
|
-
return (0, import_path3.resolve)(hostOptions.context, hostOptions.typesFolder, destinationFolder);
|
|
1705
|
-
}, "retrieveTypesArchiveDestinationPath");
|
|
1706
|
-
var downloadTypesArchive = /* @__PURE__ */ __name((hostOptions) => {
|
|
1707
|
-
let retries = 0;
|
|
1708
|
-
return (_0) => __async(void 0, [_0], function* ([destinationFolder, fileToDownload]) {
|
|
1709
|
-
var _a3;
|
|
1710
|
-
const destinationPath = retrieveTypesArchiveDestinationPath(hostOptions, destinationFolder);
|
|
1711
|
-
while (retries++ < hostOptions.maxRetries) {
|
|
1712
|
-
try {
|
|
1713
|
-
const url = new URL(fileToDownload).href;
|
|
1714
|
-
const response = yield axiosGet(url, {
|
|
1715
|
-
responseType: "arraybuffer",
|
|
1716
|
-
timeout: hostOptions.timeout,
|
|
1717
|
-
family: hostOptions.family
|
|
1718
|
-
}).catch(downloadErrorLogger(destinationFolder, url));
|
|
1719
|
-
if (typeof ((_a3 = response.headers) == null ? void 0 : _a3["content-type"]) === "string" && response.headers["content-type"].includes("text/html")) {
|
|
1720
|
-
throw new Error(`${url} receives invalid content-type: ${response.headers["content-type"]}`);
|
|
1721
|
-
}
|
|
1722
|
-
try {
|
|
1723
|
-
if (hostOptions.deleteTypesFolder) {
|
|
1724
|
-
yield (0, import_promises2.rm)(destinationPath, {
|
|
1725
|
-
recursive: true,
|
|
1726
|
-
force: true
|
|
1727
|
-
});
|
|
1728
|
-
}
|
|
1729
|
-
} catch (error2) {
|
|
1730
|
-
fileLog(`Unable to remove types folder, ${error2}`, "downloadTypesArchive", "error");
|
|
1731
|
-
}
|
|
1732
|
-
const zip = new import_adm_zip.default(Buffer.from(response.data));
|
|
1733
|
-
zip.extractAllTo(destinationPath, true);
|
|
1734
|
-
fileLog(`zip.extractAllTo success destinationPath: ${destinationPath}; url: ${url}`, "downloadTypesArchive", "info");
|
|
1735
|
-
return [
|
|
1736
|
-
destinationFolder,
|
|
1737
|
-
destinationPath
|
|
1738
|
-
];
|
|
1739
|
-
} catch (error2) {
|
|
1740
|
-
fileLog(`Error during types archive download: ${(error2 == null ? void 0 : error2.message) || "unknown error"}`, "downloadTypesArchive", "error");
|
|
1741
|
-
if (retries >= hostOptions.maxRetries) {
|
|
1742
|
-
logger.error(`Failed to download types archive from "${fileToDownload}". Set FEDERATION_DEBUG=true for details.`);
|
|
1743
|
-
if (hostOptions.abortOnError !== false) {
|
|
1744
|
-
throw error2;
|
|
1745
|
-
}
|
|
1746
|
-
return void 0;
|
|
1747
|
-
}
|
|
1748
|
-
}
|
|
1749
|
-
}
|
|
1750
|
-
});
|
|
1751
|
-
}, "downloadTypesArchive");
|
|
1752
|
-
|
|
1753
|
-
// src/core/configurations/hostPlugin.ts
|
|
1754
|
-
var import_sdk4 = require("@module-federation/sdk");
|
|
1755
|
-
var import_managers = require("@module-federation/managers");
|
|
1756
|
-
var defaultOptions = {
|
|
1757
|
-
typesFolder: "@mf-types",
|
|
1758
|
-
remoteTypesFolder: "@mf-types",
|
|
1759
|
-
deleteTypesFolder: true,
|
|
1760
|
-
maxRetries: 3,
|
|
1761
|
-
implementation: "",
|
|
1762
|
-
context: process.cwd(),
|
|
1763
|
-
abortOnError: true,
|
|
1764
|
-
consumeAPITypes: false,
|
|
1765
|
-
runtimePkgs: [],
|
|
1766
|
-
remoteTypeUrls: {},
|
|
1767
|
-
timeout: 6e4,
|
|
1768
|
-
typesOnBuild: false,
|
|
1769
|
-
family: 4
|
|
1770
|
-
};
|
|
1771
|
-
var buildZipUrl = /* @__PURE__ */ __name((hostOptions, url) => {
|
|
1772
|
-
const remoteUrl = new URL(url, "file:");
|
|
1773
|
-
const pathnameWithoutEntry = remoteUrl.pathname.split("/").slice(0, -1).join("/");
|
|
1774
|
-
remoteUrl.pathname = `${pathnameWithoutEntry}/${hostOptions.remoteTypesFolder}.zip`;
|
|
1775
|
-
return remoteUrl.protocol === "file:" ? remoteUrl.pathname : remoteUrl.href;
|
|
1776
|
-
}, "buildZipUrl");
|
|
1777
|
-
var buildApiTypeUrl = /* @__PURE__ */ __name((zipUrl) => {
|
|
1778
|
-
if (!zipUrl) {
|
|
1779
|
-
return void 0;
|
|
1780
|
-
}
|
|
1781
|
-
return zipUrl.replace(".zip", ".d.ts");
|
|
1782
|
-
}, "buildApiTypeUrl");
|
|
1783
|
-
var retrieveRemoteInfo = /* @__PURE__ */ __name((options) => {
|
|
1784
|
-
const { hostOptions, remoteAlias, remote } = options;
|
|
1785
|
-
const { remoteTypeUrls } = hostOptions;
|
|
1786
|
-
let decodedRemote = remote;
|
|
1787
|
-
if (decodedRemote.startsWith(import_sdk4.ENCODE_NAME_PREFIX)) {
|
|
1788
|
-
decodedRemote = (0, import_sdk4.decodeName)(decodedRemote, import_sdk4.ENCODE_NAME_PREFIX);
|
|
1789
|
-
}
|
|
1790
|
-
const parsedInfo = (0, import_sdk4.parseEntry)(decodedRemote, void 0, "@");
|
|
1791
|
-
const url = "entry" in parsedInfo ? parsedInfo.entry : parsedInfo.name === decodedRemote ? decodedRemote : "";
|
|
1792
|
-
let zipUrl = "";
|
|
1793
|
-
let apiTypeUrl = "";
|
|
1794
|
-
const name = parsedInfo.name || remoteAlias;
|
|
1795
|
-
if (typeof remoteTypeUrls === "object" && remoteTypeUrls[name]) {
|
|
1796
|
-
zipUrl = remoteTypeUrls[name].zip;
|
|
1797
|
-
apiTypeUrl = remoteTypeUrls[name].api;
|
|
1798
|
-
}
|
|
1799
|
-
if (!zipUrl && url) {
|
|
1800
|
-
zipUrl = buildZipUrl(hostOptions, url);
|
|
1801
|
-
}
|
|
1802
|
-
if (!apiTypeUrl && zipUrl) {
|
|
1803
|
-
apiTypeUrl = buildApiTypeUrl(zipUrl);
|
|
1804
|
-
}
|
|
1805
|
-
return {
|
|
1806
|
-
name,
|
|
1807
|
-
url,
|
|
1808
|
-
zipUrl,
|
|
1809
|
-
apiTypeUrl,
|
|
1810
|
-
alias: remoteAlias
|
|
1811
|
-
};
|
|
1812
|
-
}, "retrieveRemoteInfo");
|
|
1813
|
-
var resolveRemotes = /* @__PURE__ */ __name((hostOptions) => {
|
|
1814
|
-
var _a3;
|
|
1815
|
-
const parsedOptions = import_managers.utils.parseOptions(hostOptions.moduleFederationConfig.remotes || {}, (item, key) => ({
|
|
1816
|
-
remote: Array.isArray(item) ? item[0] : item,
|
|
1817
|
-
key
|
|
1818
|
-
}), (item, key) => ({
|
|
1819
|
-
remote: Array.isArray(item.external) ? item.external[0] : item.external,
|
|
1820
|
-
key
|
|
1821
|
-
}));
|
|
1822
|
-
const remoteTypeUrls = (_a3 = hostOptions.remoteTypeUrls) != null ? _a3 : {};
|
|
1823
|
-
if (typeof remoteTypeUrls !== "object") {
|
|
1824
|
-
throw new Error("remoteTypeUrls must be consumed before resolveRemotes");
|
|
1825
|
-
}
|
|
1826
|
-
const remoteInfos = Object.keys(remoteTypeUrls).reduce((sum, remoteName) => {
|
|
1827
|
-
const { zip, api, alias } = remoteTypeUrls[remoteName];
|
|
1828
|
-
sum[alias] = {
|
|
1829
|
-
name: remoteName,
|
|
1830
|
-
url: "",
|
|
1831
|
-
zipUrl: zip,
|
|
1832
|
-
apiTypeUrl: api,
|
|
1833
|
-
alias: alias || remoteName
|
|
1834
|
-
};
|
|
1835
|
-
return sum;
|
|
1836
|
-
}, {});
|
|
1837
|
-
return parsedOptions.reduce((accumulator, item) => {
|
|
1838
|
-
const { key, remote } = item[1];
|
|
1839
|
-
const res = retrieveRemoteInfo({
|
|
1840
|
-
hostOptions,
|
|
1841
|
-
remoteAlias: key,
|
|
1842
|
-
remote
|
|
1843
|
-
});
|
|
1844
|
-
if (accumulator[key]) {
|
|
1845
|
-
accumulator[key] = __spreadProps(__spreadValues({}, accumulator[key]), {
|
|
1846
|
-
url: res.url,
|
|
1847
|
-
apiTypeUrl: accumulator[key].apiTypeUrl || res.apiTypeUrl
|
|
1848
|
-
});
|
|
1849
|
-
return accumulator;
|
|
1850
|
-
}
|
|
1851
|
-
accumulator[key] = res;
|
|
1852
|
-
return accumulator;
|
|
1853
|
-
}, remoteInfos);
|
|
1854
|
-
}, "resolveRemotes");
|
|
1855
|
-
var retrieveHostConfig = /* @__PURE__ */ __name((options) => {
|
|
1856
|
-
validateOptions(options);
|
|
1857
|
-
const hostOptions = __spreadValues(__spreadValues({}, defaultOptions), options);
|
|
1858
|
-
const mapRemotesToDownload = resolveRemotes(hostOptions);
|
|
1859
|
-
return {
|
|
1860
|
-
hostOptions,
|
|
1861
|
-
mapRemotesToDownload
|
|
1862
|
-
};
|
|
1863
|
-
}, "retrieveHostConfig");
|
|
1864
|
-
|
|
1865
|
-
// src/core/constant.ts
|
|
1866
|
-
var REMOTE_ALIAS_IDENTIFIER = "REMOTE_ALIAS_IDENTIFIER";
|
|
1867
|
-
var REMOTE_API_TYPES_FILE_NAME = "apis.d.ts";
|
|
1868
|
-
var HOST_API_TYPES_FILE_NAME = "index.d.ts";
|
|
1869
|
-
|
|
1870
|
-
// src/core/lib/DTSManager.ts
|
|
1871
|
-
var _a;
|
|
1872
|
-
var DTSManager = (_a = class {
|
|
1873
|
-
constructor(options) {
|
|
1874
|
-
__publicField(this, "options");
|
|
1875
|
-
__publicField(this, "runtimePkgs");
|
|
1876
|
-
__publicField(this, "remoteAliasMap");
|
|
1877
|
-
__publicField(this, "loadedRemoteAPIAlias");
|
|
1878
|
-
__publicField(this, "extraOptions");
|
|
1879
|
-
__publicField(this, "updatedRemoteInfos");
|
|
1880
|
-
this.options = cloneDeepOptions(options);
|
|
1881
|
-
this.runtimePkgs = [
|
|
1882
|
-
"@module-federation/runtime",
|
|
1883
|
-
"@module-federation/enhanced/runtime",
|
|
1884
|
-
"@module-federation/runtime-tools"
|
|
1885
|
-
];
|
|
1886
|
-
this.loadedRemoteAPIAlias = /* @__PURE__ */ new Set();
|
|
1887
|
-
this.remoteAliasMap = {};
|
|
1888
|
-
this.extraOptions = (options == null ? void 0 : options.extraOptions) || {};
|
|
1889
|
-
this.updatedRemoteInfos = {};
|
|
1890
|
-
}
|
|
1891
|
-
generateAPITypes(mapComponentsToExpose) {
|
|
1892
|
-
const exposePaths = /* @__PURE__ */ new Set();
|
|
1893
|
-
const packageType = Object.keys(mapComponentsToExpose).reduce((sum, exposeKey) => {
|
|
1894
|
-
const exposePath = import_path4.default.join(REMOTE_ALIAS_IDENTIFIER, exposeKey).split(import_path4.default.sep).join("/");
|
|
1895
|
-
exposePaths.add(`'${exposePath}'`);
|
|
1896
|
-
const curType = `T extends '${exposePath}' ? typeof import('${exposePath}') :`;
|
|
1897
|
-
sum = curType + sum;
|
|
1898
|
-
return sum;
|
|
1899
|
-
}, "any;");
|
|
1900
|
-
const exposePathKeys = [
|
|
1901
|
-
...exposePaths
|
|
1902
|
-
].join(" | ");
|
|
1903
|
-
return `
|
|
1904
|
-
export type RemoteKeys = ${exposePathKeys};
|
|
1905
|
-
type PackageType<T> = ${packageType}`;
|
|
1906
|
-
}
|
|
1907
|
-
extractRemoteTypes(options) {
|
|
1908
|
-
return __async(this, null, function* () {
|
|
1909
|
-
var _a3;
|
|
1910
|
-
const { remoteOptions, tsConfig } = options;
|
|
1911
|
-
if (!remoteOptions.extractRemoteTypes) {
|
|
1912
|
-
return;
|
|
1913
|
-
}
|
|
1914
|
-
let hasRemotes = false;
|
|
1915
|
-
const remotes = remoteOptions.moduleFederationConfig.remotes;
|
|
1916
|
-
if (remotes) {
|
|
1917
|
-
if (Array.isArray(remotes)) {
|
|
1918
|
-
hasRemotes = Boolean(remotes.length);
|
|
1919
|
-
} else if (typeof remotes === "object") {
|
|
1920
|
-
hasRemotes = Boolean(Object.keys(remotes).length);
|
|
1921
|
-
}
|
|
1922
|
-
}
|
|
1923
|
-
const mfTypesPath = retrieveMfTypesPath(tsConfig, remoteOptions);
|
|
1924
|
-
if (hasRemotes && this.options.host) {
|
|
1925
|
-
try {
|
|
1926
|
-
const { hostOptions } = retrieveHostConfig(this.options.host);
|
|
1927
|
-
const remoteTypesFolder = import_path4.default.resolve(hostOptions.context, hostOptions.typesFolder);
|
|
1928
|
-
const targetDir = import_path4.default.join(mfTypesPath, "node_modules");
|
|
1929
|
-
if (import_fs.default.existsSync(remoteTypesFolder)) {
|
|
1930
|
-
const targetFolder = import_path4.default.resolve(remoteOptions.context, targetDir);
|
|
1931
|
-
yield import_fs_extra3.default.ensureDir(targetFolder);
|
|
1932
|
-
yield import_fs_extra3.default.copy(remoteTypesFolder, targetFolder, {
|
|
1933
|
-
overwrite: true
|
|
1934
|
-
});
|
|
1935
|
-
}
|
|
1936
|
-
} catch (err) {
|
|
1937
|
-
if (((_a3 = this.options.host) == null ? void 0 : _a3.abortOnError) === false) {
|
|
1938
|
-
fileLog(`Unable to copy remote types, ${err}`, "extractRemoteTypes", "error");
|
|
1939
|
-
} else {
|
|
1940
|
-
throw err;
|
|
1941
|
-
}
|
|
1942
|
-
}
|
|
1943
|
-
}
|
|
1944
|
-
});
|
|
1945
|
-
}
|
|
1946
|
-
// it must execute after consumeTypes
|
|
1947
|
-
generateTypes() {
|
|
1948
|
-
return __async(this, null, function* () {
|
|
1949
|
-
var _a3, _b;
|
|
1950
|
-
try {
|
|
1951
|
-
const { options } = this;
|
|
1952
|
-
if (!options.remote) {
|
|
1953
|
-
throw new Error("options.remote is required if you want to generateTypes");
|
|
1954
|
-
}
|
|
1955
|
-
const { remoteOptions, tsConfig, mapComponentsToExpose } = retrieveRemoteConfig(options.remote);
|
|
1956
|
-
if (!Object.keys(mapComponentsToExpose).length) {
|
|
1957
|
-
return;
|
|
1958
|
-
}
|
|
1959
|
-
if (!((_a3 = tsConfig.files) == null ? void 0 : _a3.length)) {
|
|
1960
|
-
logger.info("No type files to compile, skip");
|
|
1961
|
-
return;
|
|
1962
|
-
}
|
|
1963
|
-
if (tsConfig.compilerOptions.tsBuildInfoFile) {
|
|
1964
|
-
try {
|
|
1965
|
-
const tsBuildInfoFile = import_path4.default.resolve(remoteOptions.context, tsConfig.compilerOptions.tsBuildInfoFile);
|
|
1966
|
-
const mfTypesPath = retrieveMfTypesPath(tsConfig, remoteOptions);
|
|
1967
|
-
if (!import_fs.default.existsSync(mfTypesPath)) {
|
|
1968
|
-
import_fs.default.rmSync(tsBuildInfoFile, {
|
|
1969
|
-
force: true
|
|
1970
|
-
});
|
|
1971
|
-
}
|
|
1972
|
-
} catch (e) {
|
|
1973
|
-
}
|
|
1974
|
-
}
|
|
1975
|
-
yield this.extractRemoteTypes({
|
|
1976
|
-
remoteOptions,
|
|
1977
|
-
tsConfig,
|
|
1978
|
-
mapComponentsToExpose
|
|
1979
|
-
});
|
|
1980
|
-
yield compileTs(mapComponentsToExpose, tsConfig, remoteOptions);
|
|
1981
|
-
yield createTypesArchive(tsConfig, remoteOptions);
|
|
1982
|
-
let apiTypesPath = "";
|
|
1983
|
-
if (remoteOptions.generateAPITypes) {
|
|
1984
|
-
const apiTypes = this.generateAPITypes(mapComponentsToExpose);
|
|
1985
|
-
apiTypesPath = retrieveMfAPITypesPath(tsConfig, remoteOptions);
|
|
1986
|
-
import_fs.default.writeFileSync(apiTypesPath, apiTypes);
|
|
1987
|
-
}
|
|
1988
|
-
try {
|
|
1989
|
-
if (remoteOptions.deleteTypesFolder) {
|
|
1990
|
-
yield (0, import_promises3.rm)(retrieveMfTypesPath(tsConfig, remoteOptions), {
|
|
1991
|
-
recursive: true,
|
|
1992
|
-
force: true
|
|
1993
|
-
});
|
|
1994
|
-
}
|
|
1995
|
-
} catch (err) {
|
|
1996
|
-
if (isDebugMode()) {
|
|
1997
|
-
console.error(err);
|
|
1998
|
-
}
|
|
1999
|
-
}
|
|
2000
|
-
logger.success("Federated types created correctly");
|
|
2001
|
-
} catch (error2) {
|
|
2002
|
-
if (((_b = this.options.remote) == null ? void 0 : _b.abortOnError) === false) {
|
|
2003
|
-
if (this.options.displayErrorInTerminal) {
|
|
2004
|
-
logger.error(error2);
|
|
2005
|
-
}
|
|
2006
|
-
} else {
|
|
2007
|
-
throw error2;
|
|
2008
|
-
}
|
|
2009
|
-
}
|
|
2010
|
-
});
|
|
2011
|
-
}
|
|
2012
|
-
requestRemoteManifest(remoteInfo, hostOptions) {
|
|
2013
|
-
return __async(this, null, function* () {
|
|
2014
|
-
try {
|
|
2015
|
-
if (!remoteInfo.url.includes(import_sdk5.MANIFEST_EXT)) {
|
|
2016
|
-
return remoteInfo;
|
|
2017
|
-
}
|
|
2018
|
-
if (remoteInfo.zipUrl) {
|
|
2019
|
-
return remoteInfo;
|
|
2020
|
-
}
|
|
2021
|
-
const url = remoteInfo.url;
|
|
2022
|
-
const res = yield axiosGet(url, {
|
|
2023
|
-
timeout: hostOptions.timeout,
|
|
2024
|
-
family: hostOptions.family
|
|
2025
|
-
});
|
|
2026
|
-
const manifestJson = res.data;
|
|
2027
|
-
if (!manifestJson.metaData.types.zip) {
|
|
2028
|
-
throw new Error(`Can not get ${remoteInfo.name}'s types archive url!`);
|
|
2029
|
-
}
|
|
2030
|
-
const addProtocol = /* @__PURE__ */ __name((u) => {
|
|
2031
|
-
if (u.startsWith("//")) {
|
|
2032
|
-
return `https:${u}`;
|
|
2033
|
-
}
|
|
2034
|
-
return u;
|
|
2035
|
-
}, "addProtocol");
|
|
2036
|
-
let publicPath;
|
|
2037
|
-
if ("publicPath" in manifestJson.metaData) {
|
|
2038
|
-
publicPath = manifestJson.metaData.publicPath;
|
|
2039
|
-
} else {
|
|
2040
|
-
const getPublicPath = new Function(manifestJson.metaData.getPublicPath);
|
|
2041
|
-
if (manifestJson.metaData.getPublicPath.startsWith("function")) {
|
|
2042
|
-
publicPath = getPublicPath()();
|
|
2043
|
-
} else {
|
|
2044
|
-
publicPath = getPublicPath();
|
|
2045
|
-
}
|
|
2046
|
-
}
|
|
2047
|
-
if (publicPath === "auto") {
|
|
2048
|
-
publicPath = (0, import_sdk5.inferAutoPublicPath)(remoteInfo.url);
|
|
2049
|
-
}
|
|
2050
|
-
remoteInfo.zipUrl = new URL(import_path4.default.join(addProtocol(publicPath), manifestJson.metaData.types.zip)).href;
|
|
2051
|
-
if (!manifestJson.metaData.types.api) {
|
|
2052
|
-
console.warn(`Can not get ${remoteInfo.name}'s api types url!`);
|
|
2053
|
-
remoteInfo.apiTypeUrl = "";
|
|
2054
|
-
return remoteInfo;
|
|
2055
|
-
}
|
|
2056
|
-
remoteInfo.apiTypeUrl = new URL(import_path4.default.join(addProtocol(publicPath), manifestJson.metaData.types.api)).href;
|
|
2057
|
-
return remoteInfo;
|
|
2058
|
-
} catch (_err) {
|
|
2059
|
-
fileLog(`fetch manifest failed, ${_err}, ${remoteInfo.name} will be ignored`, "requestRemoteManifest", "error");
|
|
2060
|
-
return remoteInfo;
|
|
2061
|
-
}
|
|
2062
|
-
});
|
|
2063
|
-
}
|
|
2064
|
-
consumeTargetRemotes(hostOptions, remoteInfo) {
|
|
2065
|
-
return __async(this, null, function* () {
|
|
2066
|
-
if (!remoteInfo.zipUrl) {
|
|
2067
|
-
throw new Error(`Can not get ${remoteInfo.name}'s types archive url!`);
|
|
2068
|
-
}
|
|
2069
|
-
const typesDownloader = downloadTypesArchive(hostOptions);
|
|
2070
|
-
return typesDownloader([
|
|
2071
|
-
remoteInfo.alias,
|
|
2072
|
-
remoteInfo.zipUrl
|
|
2073
|
-
]);
|
|
2074
|
-
});
|
|
2075
|
-
}
|
|
2076
|
-
downloadAPITypes(remoteInfo, destinationPath, hostOptions) {
|
|
2077
|
-
return __async(this, null, function* () {
|
|
2078
|
-
const { apiTypeUrl } = remoteInfo;
|
|
2079
|
-
if (!apiTypeUrl) {
|
|
2080
|
-
return;
|
|
2081
|
-
}
|
|
2082
|
-
try {
|
|
2083
|
-
const url = apiTypeUrl;
|
|
2084
|
-
const res = yield axiosGet(url, {
|
|
2085
|
-
timeout: hostOptions.timeout,
|
|
2086
|
-
family: hostOptions.family
|
|
2087
|
-
});
|
|
2088
|
-
let apiTypeFile = res.data;
|
|
2089
|
-
apiTypeFile = apiTypeFile.replaceAll(REMOTE_ALIAS_IDENTIFIER, remoteInfo.alias);
|
|
2090
|
-
const filePath = import_path4.default.join(destinationPath, REMOTE_API_TYPES_FILE_NAME);
|
|
2091
|
-
import_fs.default.writeFileSync(filePath, apiTypeFile);
|
|
2092
|
-
const existed = this.loadedRemoteAPIAlias.has(remoteInfo.alias);
|
|
2093
|
-
this.loadedRemoteAPIAlias.add(remoteInfo.alias);
|
|
2094
|
-
fileLog(`success`, "downloadAPITypes", "info");
|
|
2095
|
-
return existed;
|
|
2096
|
-
} catch (err) {
|
|
2097
|
-
fileLog(`Unable to download "${remoteInfo.name}" api types, ${err}`, "downloadAPITypes", "error");
|
|
2098
|
-
}
|
|
2099
|
-
});
|
|
2100
|
-
}
|
|
2101
|
-
consumeAPITypes(hostOptions) {
|
|
2102
|
-
const apiTypeFileName = import_path4.default.join(hostOptions.context, hostOptions.typesFolder, HOST_API_TYPES_FILE_NAME);
|
|
2103
|
-
try {
|
|
2104
|
-
const existedFile = import_fs.default.readFileSync(apiTypeFileName, "utf-8");
|
|
2105
|
-
const existedImports = new import_third_party_dts_extractor2.ThirdPartyExtractor({
|
|
2106
|
-
destDir: ""
|
|
2107
|
-
}).collectTypeImports(existedFile);
|
|
2108
|
-
existedImports.forEach((existedImport) => {
|
|
2109
|
-
const alias = existedImport.split("./").slice(1).join("./").replace("/apis.d.ts", "");
|
|
2110
|
-
this.loadedRemoteAPIAlias.add(alias);
|
|
2111
|
-
});
|
|
2112
|
-
} catch (err) {
|
|
2113
|
-
}
|
|
2114
|
-
if (!this.loadedRemoteAPIAlias.size) {
|
|
2115
|
-
return;
|
|
2116
|
-
}
|
|
2117
|
-
const packageTypes = [];
|
|
2118
|
-
const remoteKeys = [];
|
|
2119
|
-
const importTypeStr = [
|
|
2120
|
-
...this.loadedRemoteAPIAlias
|
|
2121
|
-
].sort().map((alias, index) => {
|
|
2122
|
-
const remoteKey = `RemoteKeys_${index}`;
|
|
2123
|
-
const packageType = `PackageType_${index}`;
|
|
2124
|
-
packageTypes.push(`T extends ${remoteKey} ? ${packageType}<T>`);
|
|
2125
|
-
remoteKeys.push(remoteKey);
|
|
2126
|
-
return `import type { PackageType as ${packageType},RemoteKeys as ${remoteKey} } from './${alias}/apis.d.ts';`;
|
|
2127
|
-
}).join("\n");
|
|
2128
|
-
const remoteKeysStr = `type RemoteKeys = ${remoteKeys.join(" | ")};`;
|
|
2129
|
-
const packageTypesStr = `type PackageType<T, Y=any> = ${[
|
|
2130
|
-
...packageTypes,
|
|
2131
|
-
"Y"
|
|
2132
|
-
].join(" :\n")} ;`;
|
|
2133
|
-
const runtimePkgs = /* @__PURE__ */ new Set();
|
|
2134
|
-
[
|
|
2135
|
-
...this.runtimePkgs,
|
|
2136
|
-
...hostOptions.runtimePkgs
|
|
2137
|
-
].forEach((pkg) => {
|
|
2138
|
-
runtimePkgs.add(pkg);
|
|
2139
|
-
});
|
|
2140
|
-
const pkgsDeclareStr = [
|
|
2141
|
-
...runtimePkgs
|
|
2142
|
-
].map((pkg) => {
|
|
2143
|
-
return `declare module "${pkg}" {
|
|
2144
|
-
${remoteKeysStr}
|
|
2145
|
-
${packageTypesStr}
|
|
2146
|
-
export function loadRemote<T extends RemoteKeys,Y>(packageName: T): Promise<PackageType<T, Y>>;
|
|
2147
|
-
export function loadRemote<T extends string,Y>(packageName: T): Promise<PackageType<T, Y>>;
|
|
2148
|
-
}`;
|
|
2149
|
-
}).join("\n");
|
|
2150
|
-
const fileStr = `${importTypeStr}
|
|
2151
|
-
${pkgsDeclareStr}
|
|
2152
|
-
`;
|
|
2153
|
-
import_fs.default.writeFileSync(import_path4.default.join(hostOptions.context, hostOptions.typesFolder, HOST_API_TYPES_FILE_NAME), fileStr);
|
|
2154
|
-
}
|
|
2155
|
-
consumeArchiveTypes(options) {
|
|
2156
|
-
return __async(this, null, function* () {
|
|
2157
|
-
const { hostOptions, mapRemotesToDownload } = retrieveHostConfig(options);
|
|
2158
|
-
const downloadPromises = Object.entries(mapRemotesToDownload).map((item) => __async(this, null, function* () {
|
|
2159
|
-
const remoteInfo = item[1];
|
|
2160
|
-
if (!this.remoteAliasMap[remoteInfo.alias]) {
|
|
2161
|
-
const requiredRemoteInfo = yield this.requestRemoteManifest(remoteInfo, hostOptions);
|
|
2162
|
-
this.remoteAliasMap[remoteInfo.alias] = requiredRemoteInfo;
|
|
2163
|
-
}
|
|
2164
|
-
return this.consumeTargetRemotes(hostOptions, this.remoteAliasMap[remoteInfo.alias]);
|
|
2165
|
-
}));
|
|
2166
|
-
const downloadPromisesResult = yield Promise.allSettled(downloadPromises);
|
|
2167
|
-
return {
|
|
2168
|
-
hostOptions,
|
|
2169
|
-
downloadPromisesResult
|
|
2170
|
-
};
|
|
2171
|
-
});
|
|
2172
|
-
}
|
|
2173
|
-
consumeTypes() {
|
|
2174
|
-
return __async(this, null, function* () {
|
|
2175
|
-
var _a3;
|
|
2176
|
-
try {
|
|
2177
|
-
const { options } = this;
|
|
2178
|
-
if (!options.host) {
|
|
2179
|
-
throw new Error("options.host is required if you want to consumeTypes");
|
|
2180
|
-
}
|
|
2181
|
-
const { mapRemotesToDownload } = retrieveHostConfig(options.host);
|
|
2182
|
-
if (!Object.keys(mapRemotesToDownload).length) {
|
|
2183
|
-
return;
|
|
2184
|
-
}
|
|
2185
|
-
const { downloadPromisesResult, hostOptions } = yield this.consumeArchiveTypes(options.host);
|
|
2186
|
-
if (hostOptions.consumeAPITypes) {
|
|
2187
|
-
yield Promise.all(downloadPromisesResult.map((item) => __async(this, null, function* () {
|
|
2188
|
-
if (item.status === "rejected" || !item.value) {
|
|
2189
|
-
return;
|
|
2190
|
-
}
|
|
2191
|
-
const [alias, destinationPath] = item.value;
|
|
2192
|
-
const remoteInfo = this.remoteAliasMap[alias];
|
|
2193
|
-
if (!remoteInfo) {
|
|
2194
|
-
return;
|
|
2195
|
-
}
|
|
2196
|
-
yield this.downloadAPITypes(remoteInfo, destinationPath, hostOptions);
|
|
2197
|
-
})));
|
|
2198
|
-
this.consumeAPITypes(hostOptions);
|
|
2199
|
-
}
|
|
2200
|
-
logger.success("Federated types extraction completed");
|
|
2201
|
-
} catch (err) {
|
|
2202
|
-
if (((_a3 = this.options.host) == null ? void 0 : _a3.abortOnError) === false) {
|
|
2203
|
-
fileLog(`Unable to consume federated types, ${err}`, "consumeTypes", "error");
|
|
2204
|
-
} else {
|
|
2205
|
-
throw err;
|
|
2206
|
-
}
|
|
2207
|
-
}
|
|
2208
|
-
});
|
|
2209
|
-
}
|
|
2210
|
-
updateTypes(options) {
|
|
2211
|
-
return __async(this, null, function* () {
|
|
2212
|
-
var _a3, _b, _c;
|
|
2213
|
-
try {
|
|
2214
|
-
const { remoteName, updateMode, remoteTarPath, remoteInfo: updatedRemoteInfo, once } = options;
|
|
2215
|
-
const hostName = (_c = (_b = (_a3 = this.options) == null ? void 0 : _a3.host) == null ? void 0 : _b.moduleFederationConfig) == null ? void 0 : _c.name;
|
|
2216
|
-
fileLog(`options: ${JSON.stringify(options, null, 2)};
|
|
2217
|
-
hostName: ${hostName}`, "updateTypes", "info");
|
|
2218
|
-
if (updateMode === UpdateMode.POSITIVE && remoteName === hostName) {
|
|
2219
|
-
if (!this.options.remote) {
|
|
2220
|
-
return;
|
|
2221
|
-
}
|
|
2222
|
-
yield this.generateTypes();
|
|
2223
|
-
} else {
|
|
2224
|
-
const { remoteAliasMap } = this;
|
|
2225
|
-
if (!this.options.host) {
|
|
2226
|
-
return;
|
|
2227
|
-
}
|
|
2228
|
-
const { hostOptions, mapRemotesToDownload } = retrieveHostConfig(this.options.host);
|
|
2229
|
-
const loadedRemoteInfo = Object.values(remoteAliasMap).find((i) => i.name === remoteName);
|
|
2230
|
-
const consumeTypes2 = /* @__PURE__ */ __name((requiredRemoteInfo) => __async(this, null, function* () {
|
|
2231
|
-
fileLog(`consumeTypes start`, "updateTypes", "info");
|
|
2232
|
-
if (!requiredRemoteInfo.zipUrl) {
|
|
2233
|
-
throw new Error(`Can not get ${requiredRemoteInfo.name}'s types archive url!`);
|
|
2234
|
-
}
|
|
2235
|
-
const [_alias, destinationPath] = yield this.consumeTargetRemotes(hostOptions, __spreadProps(__spreadValues({}, requiredRemoteInfo), {
|
|
2236
|
-
// use remoteTarPath first
|
|
2237
|
-
zipUrl: remoteTarPath || requiredRemoteInfo.zipUrl
|
|
2238
|
-
}));
|
|
2239
|
-
const addNew = yield this.downloadAPITypes(requiredRemoteInfo, destinationPath, hostOptions);
|
|
2240
|
-
if (addNew) {
|
|
2241
|
-
this.consumeAPITypes(hostOptions);
|
|
2242
|
-
}
|
|
2243
|
-
fileLog(`consumeTypes end`, "updateTypes", "info");
|
|
2244
|
-
}), "consumeTypes");
|
|
2245
|
-
fileLog(`loadedRemoteInfo: ${JSON.stringify(loadedRemoteInfo, null, 2)}`, "updateTypes", "info");
|
|
2246
|
-
if (!loadedRemoteInfo) {
|
|
2247
|
-
const remoteInfo = Object.values(mapRemotesToDownload).find((item) => {
|
|
2248
|
-
return item.name === remoteName;
|
|
2249
|
-
});
|
|
2250
|
-
fileLog(`remoteInfo: ${JSON.stringify(remoteInfo, null, 2)}`, "updateTypes", "info");
|
|
2251
|
-
if (remoteInfo) {
|
|
2252
|
-
if (!this.remoteAliasMap[remoteInfo.alias]) {
|
|
2253
|
-
const requiredRemoteInfo = yield this.requestRemoteManifest(remoteInfo, hostOptions);
|
|
2254
|
-
this.remoteAliasMap[remoteInfo.alias] = requiredRemoteInfo;
|
|
2255
|
-
}
|
|
2256
|
-
yield consumeTypes2(this.remoteAliasMap[remoteInfo.alias]);
|
|
2257
|
-
} else if (updatedRemoteInfo) {
|
|
2258
|
-
const consumeDynamicRemoteTypes = /* @__PURE__ */ __name(() => __async(this, null, function* () {
|
|
2259
|
-
yield consumeTypes2(this.updatedRemoteInfos[updatedRemoteInfo.name]);
|
|
2260
|
-
}), "consumeDynamicRemoteTypes");
|
|
2261
|
-
if (!this.updatedRemoteInfos[updatedRemoteInfo.name]) {
|
|
2262
|
-
const parsedRemoteInfo = retrieveRemoteInfo({
|
|
2263
|
-
hostOptions,
|
|
2264
|
-
remoteAlias: updatedRemoteInfo.alias || updatedRemoteInfo.name,
|
|
2265
|
-
remote: updatedRemoteInfo.url
|
|
2266
|
-
});
|
|
2267
|
-
fileLog(`start request manifest`, "consumeTypes", "info");
|
|
2268
|
-
this.updatedRemoteInfos[updatedRemoteInfo.name] = yield this.requestRemoteManifest(parsedRemoteInfo, hostOptions);
|
|
2269
|
-
fileLog(`end request manifest, this.updatedRemoteInfos[updatedRemoteInfo.name]: ${JSON.stringify(this.updatedRemoteInfos[updatedRemoteInfo.name], null, 2)}`, "updateTypes", "info");
|
|
2270
|
-
yield consumeDynamicRemoteTypes();
|
|
2271
|
-
}
|
|
2272
|
-
if (!once && this.updatedRemoteInfos[updatedRemoteInfo.name]) {
|
|
2273
|
-
yield consumeDynamicRemoteTypes();
|
|
2274
|
-
}
|
|
2275
|
-
}
|
|
2276
|
-
} else {
|
|
2277
|
-
yield consumeTypes2(loadedRemoteInfo);
|
|
2278
|
-
}
|
|
2279
|
-
}
|
|
2280
|
-
} catch (err) {
|
|
2281
|
-
fileLog(`updateTypes fail, ${err}`, "updateTypes", "error");
|
|
2282
|
-
}
|
|
2283
|
-
});
|
|
2284
|
-
}
|
|
2285
|
-
}, __name(_a, "DTSManager"), _a);
|
|
2286
|
-
|
|
2287
|
-
// src/core/lib/utils.ts
|
|
2288
|
-
var import_lodash = __toESM(require("lodash.clonedeepwith"));
|
|
2289
|
-
function getDTSManagerConstructor(implementation) {
|
|
2290
|
-
if (implementation) {
|
|
2291
|
-
const NewConstructor = require(implementation);
|
|
2292
|
-
return NewConstructor.default ? NewConstructor.default : NewConstructor;
|
|
2293
|
-
}
|
|
2294
|
-
return DTSManager;
|
|
2295
|
-
}
|
|
2296
|
-
__name(getDTSManagerConstructor, "getDTSManagerConstructor");
|
|
2297
|
-
var validateOptions = /* @__PURE__ */ __name((options) => {
|
|
2298
|
-
if (!options.moduleFederationConfig) {
|
|
2299
|
-
throw new Error("moduleFederationConfig is required");
|
|
2300
|
-
}
|
|
2301
|
-
}, "validateOptions");
|
|
2302
|
-
function isDebugMode() {
|
|
2303
|
-
return Boolean(process.env["FEDERATION_DEBUG"]) || process.env["NODE_ENV"] === "test";
|
|
2304
|
-
}
|
|
2305
|
-
__name(isDebugMode, "isDebugMode");
|
|
2306
|
-
function cloneDeepOptions(options) {
|
|
2307
|
-
const excludeKeys = [
|
|
2308
|
-
"manifest",
|
|
2309
|
-
"async"
|
|
2310
|
-
];
|
|
2311
|
-
return (0, import_lodash.default)(options, (value, key) => {
|
|
2312
|
-
if (typeof key === "string" && excludeKeys.includes(key)) {
|
|
2313
|
-
return false;
|
|
2314
|
-
}
|
|
2315
|
-
if (typeof value === "function") {
|
|
2316
|
-
return false;
|
|
2317
|
-
}
|
|
2318
|
-
if (key === "extractThirdParty" && Array.isArray(value)) {
|
|
2319
|
-
return value.map((item) => {
|
|
2320
|
-
return item.toString();
|
|
2321
|
-
});
|
|
2322
|
-
}
|
|
2323
|
-
});
|
|
2324
|
-
}
|
|
2325
|
-
__name(cloneDeepOptions, "cloneDeepOptions");
|
|
2326
|
-
var getEnvHeaders = /* @__PURE__ */ __name(() => {
|
|
2327
|
-
const headersStr = (0, import_sdk6.getProcessEnv)()["MF_ENV_HEADERS"] || "{}";
|
|
2328
|
-
return __spreadValues({}, JSON.parse(headersStr));
|
|
2329
|
-
}, "getEnvHeaders");
|
|
2330
|
-
function axiosGet(url, config) {
|
|
2331
|
-
return __async(this, null, function* () {
|
|
2332
|
-
var _a3, _b;
|
|
2333
|
-
const httpAgent = new import_http2.default.Agent({
|
|
2334
|
-
family: (_a3 = config == null ? void 0 : config.family) != null ? _a3 : 4
|
|
2335
|
-
});
|
|
2336
|
-
const httpsAgent = new import_https.default.Agent({
|
|
2337
|
-
family: (_b = config == null ? void 0 : config.family) != null ? _b : 4
|
|
2338
|
-
});
|
|
2339
|
-
return import_axios.default.get(url, __spreadProps(__spreadValues(__spreadValues({
|
|
2340
|
-
httpAgent,
|
|
2341
|
-
httpsAgent
|
|
2342
|
-
}, {
|
|
2343
|
-
headers: getEnvHeaders()
|
|
2344
|
-
}), config), {
|
|
2345
|
-
timeout: (config == null ? void 0 : config.timeout) || 6e4
|
|
2346
|
-
}));
|
|
2347
|
-
});
|
|
2348
|
-
}
|
|
2349
|
-
__name(axiosGet, "axiosGet");
|
|
2350
|
-
|
|
2351
|
-
// src/core/configurations/remotePlugin.ts
|
|
2352
|
-
var defaultOptions2 = {
|
|
2353
|
-
tsConfigPath: "./tsconfig.json",
|
|
2354
|
-
typesFolder: "@mf-types",
|
|
2355
|
-
compiledTypesFolder: "compiled-types",
|
|
2356
|
-
hostRemoteTypesFolder: "@mf-types",
|
|
2357
|
-
deleteTypesFolder: true,
|
|
2358
|
-
additionalFilesToCompile: [],
|
|
2359
|
-
compilerInstance: "tsc",
|
|
2360
|
-
compileInChildProcess: false,
|
|
2361
|
-
implementation: "",
|
|
2362
|
-
generateAPITypes: false,
|
|
2363
|
-
context: process.cwd(),
|
|
2364
|
-
abortOnError: true,
|
|
2365
|
-
extractRemoteTypes: false,
|
|
2366
|
-
extractThirdParty: false,
|
|
2367
|
-
outputDir: "",
|
|
2368
|
-
deleteTsConfig: true
|
|
2369
|
-
};
|
|
2370
|
-
function getEffectiveRootDir(parsedCommandLine) {
|
|
2371
|
-
const compilerOptions = parsedCommandLine.options;
|
|
2372
|
-
if (compilerOptions.rootDir) {
|
|
2373
|
-
return compilerOptions.rootDir;
|
|
2374
|
-
}
|
|
2375
|
-
const files = parsedCommandLine.fileNames;
|
|
2376
|
-
if (files.length > 0) {
|
|
2377
|
-
const commonRoot = files.map((file) => (0, import_path5.dirname)(file)).reduce((commonPath, fileDir) => {
|
|
2378
|
-
while (!fileDir.startsWith(commonPath)) {
|
|
2379
|
-
commonPath = (0, import_path5.dirname)(commonPath);
|
|
2380
|
-
}
|
|
2381
|
-
return commonPath;
|
|
2382
|
-
}, files[0]);
|
|
2383
|
-
return commonRoot;
|
|
2384
|
-
}
|
|
2385
|
-
throw new Error("Can not get effective rootDir, please set compilerOptions.rootDir !");
|
|
2386
|
-
}
|
|
2387
|
-
__name(getEffectiveRootDir, "getEffectiveRootDir");
|
|
2388
|
-
var getDependentFiles = /* @__PURE__ */ __name((rootFiles, configContent, rootDir) => {
|
|
2389
|
-
const program = import_typescript.default.createProgram(rootFiles, configContent.options);
|
|
2390
|
-
const sourceFiles = program.getSourceFiles();
|
|
2391
|
-
const dependentFiles = sourceFiles.map((file) => file.fileName).filter((file) => !file.endsWith(".d.ts") && file.startsWith(rootDir));
|
|
2392
|
-
return dependentFiles.length ? dependentFiles : rootFiles;
|
|
2393
|
-
}, "getDependentFiles");
|
|
2394
|
-
var readTsConfig = /* @__PURE__ */ __name(({ tsConfigPath, typesFolder, compiledTypesFolder, context, additionalFilesToCompile, outputDir }, mapComponentsToExpose) => {
|
|
2395
|
-
const resolvedTsConfigPath = (0, import_path5.resolve)(context, tsConfigPath);
|
|
2396
|
-
const readResult = import_typescript.default.readConfigFile(resolvedTsConfigPath, import_typescript.default.sys.readFile);
|
|
2397
|
-
if (readResult.error) {
|
|
2398
|
-
throw new Error(readResult.error.messageText.toString());
|
|
2399
|
-
}
|
|
2400
|
-
const rawTsConfigJson = readResult.config;
|
|
2401
|
-
const configContent = import_typescript.default.parseJsonConfigFileContent(rawTsConfigJson, import_typescript.default.sys, (0, import_path5.dirname)(resolvedTsConfigPath));
|
|
2402
|
-
const rootDir = getEffectiveRootDir(configContent);
|
|
2403
|
-
const outDir = (0, import_path5.resolve)(context, outputDir || configContent.options.outDir || "dist", typesFolder, compiledTypesFolder);
|
|
2404
|
-
const defaultCompilerOptions = {
|
|
2405
|
-
rootDir,
|
|
2406
|
-
emitDeclarationOnly: true,
|
|
2407
|
-
noEmit: false,
|
|
2408
|
-
declaration: true,
|
|
2409
|
-
outDir
|
|
2410
|
-
};
|
|
2411
|
-
rawTsConfigJson.compilerOptions = rawTsConfigJson.compilerOptions || {};
|
|
2412
|
-
rawTsConfigJson.compilerOptions = __spreadValues(__spreadValues({
|
|
2413
|
-
incremental: true,
|
|
2414
|
-
tsBuildInfoFile: (0, import_path5.resolve)(context, "node_modules/.cache/mf-types/.tsbuildinfo")
|
|
2415
|
-
}, rawTsConfigJson.compilerOptions), defaultCompilerOptions);
|
|
2416
|
-
const _a3 = rawTsConfigJson.compilerOptions || {}, { paths, baseUrl } = _a3, restCompilerOptions = __objRest(_a3, ["paths", "baseUrl"]);
|
|
2417
|
-
rawTsConfigJson.compilerOptions = restCompilerOptions;
|
|
2418
|
-
const outDirWithoutTypesFolder = (0, import_path5.resolve)(context, outputDir || configContent.options.outDir || "dist");
|
|
2419
|
-
const excludeExtensions = [
|
|
2420
|
-
".mdx",
|
|
2421
|
-
".md"
|
|
2422
|
-
];
|
|
2423
|
-
const rootFiles = [
|
|
2424
|
-
...Object.values(mapComponentsToExpose),
|
|
2425
|
-
...additionalFilesToCompile
|
|
2426
|
-
].filter((filename) => !excludeExtensions.some((ext) => filename.endsWith(ext)));
|
|
2427
|
-
const filesToCompile = [
|
|
2428
|
-
...getDependentFiles(rootFiles, configContent, rootDir),
|
|
2429
|
-
...configContent.fileNames.filter((filename) => filename.endsWith(".d.ts") && !filename.startsWith(outDirWithoutTypesFolder))
|
|
2430
|
-
];
|
|
2431
|
-
rawTsConfigJson.include = [];
|
|
2432
|
-
rawTsConfigJson.files = [
|
|
2433
|
-
...new Set(filesToCompile)
|
|
2434
|
-
];
|
|
2435
|
-
rawTsConfigJson.exclude = [];
|
|
2436
|
-
"references" in rawTsConfigJson && delete rawTsConfigJson.references;
|
|
2437
|
-
rawTsConfigJson.extends = resolvedTsConfigPath;
|
|
2438
|
-
if (rawTsConfigJson.compilerOptions.declarationDir) {
|
|
2439
|
-
delete rawTsConfigJson.compilerOptions.declarationDir;
|
|
2440
|
-
}
|
|
2441
|
-
return rawTsConfigJson;
|
|
2442
|
-
}, "readTsConfig");
|
|
2443
|
-
var TS_EXTENSIONS = [
|
|
2444
|
-
"ts",
|
|
2445
|
-
"tsx",
|
|
2446
|
-
"vue",
|
|
2447
|
-
"svelte"
|
|
2448
|
-
];
|
|
2449
|
-
var resolveWithExtension = /* @__PURE__ */ __name((exposedPath, context) => {
|
|
2450
|
-
if ((0, import_path5.extname)(exposedPath)) {
|
|
2451
|
-
return (0, import_path5.resolve)(context, exposedPath);
|
|
2452
|
-
}
|
|
2453
|
-
for (const extension of TS_EXTENSIONS) {
|
|
2454
|
-
const exposedPathWithExtension = (0, import_path5.resolve)(context, `${exposedPath}.${extension}`);
|
|
2455
|
-
if ((0, import_fs2.existsSync)(exposedPathWithExtension)) {
|
|
2456
|
-
return exposedPathWithExtension;
|
|
2457
|
-
}
|
|
2458
|
-
}
|
|
2459
|
-
return void 0;
|
|
2460
|
-
}, "resolveWithExtension");
|
|
2461
|
-
var resolveExposes = /* @__PURE__ */ __name((remoteOptions) => {
|
|
2462
|
-
const parsedOptions = import_managers2.utils.parseOptions(remoteOptions.moduleFederationConfig.exposes || {}, (item, key) => ({
|
|
2463
|
-
exposePath: Array.isArray(item) ? item[0] : item,
|
|
2464
|
-
key
|
|
2465
|
-
}), (item, key) => ({
|
|
2466
|
-
exposePath: Array.isArray(item.import) ? item.import[0] : item.import[0],
|
|
2467
|
-
key
|
|
2468
|
-
}));
|
|
2469
|
-
return parsedOptions.reduce((accumulator, item) => {
|
|
2470
|
-
const { exposePath, key } = item[1];
|
|
2471
|
-
accumulator[key] = resolveWithExtension(exposePath, remoteOptions.context) || resolveWithExtension((0, import_path5.join)(exposePath, "index"), remoteOptions.context) || exposePath;
|
|
2472
|
-
return accumulator;
|
|
2473
|
-
}, {});
|
|
2474
|
-
}, "resolveExposes");
|
|
2475
|
-
var retrieveRemoteConfig = /* @__PURE__ */ __name((options) => {
|
|
2476
|
-
validateOptions(options);
|
|
2477
|
-
const remoteOptions = __spreadValues(__spreadValues({}, defaultOptions2), options);
|
|
2478
|
-
const mapComponentsToExpose = resolveExposes(remoteOptions);
|
|
2479
|
-
const tsConfig = readTsConfig(remoteOptions, mapComponentsToExpose);
|
|
2480
|
-
if (tsConfig.compilerOptions.incremental && tsConfig.compilerOptions.tsBuildInfoFile && options.deleteTypesFolder !== true) {
|
|
2481
|
-
remoteOptions.deleteTypesFolder = false;
|
|
2482
|
-
}
|
|
2483
|
-
return {
|
|
2484
|
-
tsConfig,
|
|
2485
|
-
mapComponentsToExpose,
|
|
2486
|
-
remoteOptions
|
|
2487
|
-
};
|
|
2488
|
-
}, "retrieveRemoteConfig");
|
|
2489
|
-
|
|
2490
|
-
// src/core/rpc/index.ts
|
|
2491
|
-
var rpc_exports = {};
|
|
2492
|
-
__export(rpc_exports, {
|
|
2493
|
-
RpcExitError: () => RpcExitError,
|
|
2494
|
-
RpcGMCallTypes: () => RpcGMCallTypes,
|
|
2495
|
-
createRpcWorker: () => createRpcWorker,
|
|
2496
|
-
exposeRpc: () => exposeRpc,
|
|
2497
|
-
getRpcWorkerData: () => getRpcWorkerData,
|
|
2498
|
-
wrapRpc: () => wrapRpc
|
|
2499
|
-
});
|
|
2500
|
-
|
|
2501
|
-
// src/core/rpc/expose-rpc.ts
|
|
2502
|
-
var import_process = __toESM(require("process"));
|
|
2503
|
-
|
|
2504
|
-
// src/core/rpc/types.ts
|
|
2505
|
-
var RpcGMCallTypes;
|
|
2506
|
-
(function(RpcGMCallTypes2) {
|
|
2507
|
-
RpcGMCallTypes2["CALL"] = "mf_call";
|
|
2508
|
-
RpcGMCallTypes2["RESOLVE"] = "mf_resolve";
|
|
2509
|
-
RpcGMCallTypes2["REJECT"] = "mf_reject";
|
|
2510
|
-
RpcGMCallTypes2["EXIT"] = "mf_exit";
|
|
2511
|
-
})(RpcGMCallTypes || (RpcGMCallTypes = {}));
|
|
2512
|
-
|
|
2513
|
-
// src/core/rpc/expose-rpc.ts
|
|
2514
|
-
function exposeRpc(fn) {
|
|
2515
|
-
const sendMessage = /* @__PURE__ */ __name((message) => new Promise((resolve4, reject) => {
|
|
2516
|
-
if (!import_process.default.send) {
|
|
2517
|
-
reject(new Error(`Process ${import_process.default.pid} doesn't have IPC channels`));
|
|
2518
|
-
} else if (!import_process.default.connected) {
|
|
2519
|
-
reject(new Error(`Process ${import_process.default.pid} doesn't have open IPC channels`));
|
|
2520
|
-
} else {
|
|
2521
|
-
import_process.default.send(message, void 0, void 0, (error2) => {
|
|
2522
|
-
if (error2) {
|
|
2523
|
-
reject(error2);
|
|
2524
|
-
} else {
|
|
2525
|
-
resolve4(void 0);
|
|
2526
|
-
}
|
|
2527
|
-
});
|
|
2528
|
-
}
|
|
2529
|
-
}), "sendMessage");
|
|
2530
|
-
const handleMessage = /* @__PURE__ */ __name((message) => __async(this, null, function* () {
|
|
2531
|
-
if (message.type === RpcGMCallTypes.CALL) {
|
|
2532
|
-
if (!import_process.default.send) {
|
|
2533
|
-
return;
|
|
2534
|
-
}
|
|
2535
|
-
let value, error2;
|
|
2536
|
-
try {
|
|
2537
|
-
value = yield fn(...message.args);
|
|
2538
|
-
} catch (fnError) {
|
|
2539
|
-
error2 = fnError;
|
|
2540
|
-
}
|
|
2541
|
-
try {
|
|
2542
|
-
if (error2) {
|
|
2543
|
-
yield sendMessage({
|
|
2544
|
-
type: RpcGMCallTypes.REJECT,
|
|
2545
|
-
id: message.id,
|
|
2546
|
-
error: error2
|
|
2547
|
-
});
|
|
2548
|
-
} else {
|
|
2549
|
-
yield sendMessage({
|
|
2550
|
-
type: RpcGMCallTypes.RESOLVE,
|
|
2551
|
-
id: message.id,
|
|
2552
|
-
value
|
|
2553
|
-
});
|
|
2554
|
-
}
|
|
2555
|
-
} catch (sendError) {
|
|
2556
|
-
if (error2) {
|
|
2557
|
-
if (error2 instanceof Error) {
|
|
2558
|
-
console.error(error2);
|
|
2559
|
-
}
|
|
2560
|
-
}
|
|
2561
|
-
console.error(sendError);
|
|
2562
|
-
}
|
|
2563
|
-
}
|
|
2564
|
-
}), "handleMessage");
|
|
2565
|
-
import_process.default.on("message", handleMessage);
|
|
2566
|
-
}
|
|
2567
|
-
__name(exposeRpc, "exposeRpc");
|
|
2568
|
-
|
|
2569
|
-
// src/core/rpc/rpc-error.ts
|
|
2570
|
-
var _a2;
|
|
2571
|
-
var RpcExitError = (_a2 = class extends Error {
|
|
2572
|
-
constructor(message, code, signal) {
|
|
2573
|
-
super(message);
|
|
2574
|
-
__publicField(this, "code");
|
|
2575
|
-
__publicField(this, "signal");
|
|
2576
|
-
this.code = code, this.signal = signal;
|
|
2577
|
-
this.name = "RpcExitError";
|
|
2578
|
-
}
|
|
2579
|
-
}, __name(_a2, "RpcExitError"), _a2);
|
|
2580
|
-
|
|
2581
|
-
// src/core/rpc/wrap-rpc.ts
|
|
2582
|
-
function createControlledPromise() {
|
|
2583
|
-
let resolve4 = /* @__PURE__ */ __name(() => void 0, "resolve");
|
|
2584
|
-
let reject = /* @__PURE__ */ __name(() => void 0, "reject");
|
|
2585
|
-
const promise = new Promise((aResolve, aReject) => {
|
|
2586
|
-
resolve4 = aResolve;
|
|
2587
|
-
reject = aReject;
|
|
2588
|
-
});
|
|
2589
|
-
return {
|
|
2590
|
-
promise,
|
|
2591
|
-
resolve: resolve4,
|
|
2592
|
-
reject
|
|
2593
|
-
};
|
|
2594
|
-
}
|
|
2595
|
-
__name(createControlledPromise, "createControlledPromise");
|
|
2596
|
-
function wrapRpc(childProcess, options) {
|
|
2597
|
-
return (...args) => __async(this, null, function* () {
|
|
2598
|
-
if (!childProcess.send) {
|
|
2599
|
-
throw new Error(`Process ${childProcess.pid} doesn't have IPC channels`);
|
|
2600
|
-
} else if (!childProcess.connected) {
|
|
2601
|
-
throw new Error(`Process ${childProcess.pid} doesn't have open IPC channels`);
|
|
2602
|
-
}
|
|
2603
|
-
const { id, once } = options;
|
|
2604
|
-
const { promise: resultPromise, resolve: resolveResult, reject: rejectResult } = createControlledPromise();
|
|
2605
|
-
const { promise: sendPromise, resolve: resolveSend, reject: rejectSend } = createControlledPromise();
|
|
2606
|
-
const handleMessage = /* @__PURE__ */ __name((message) => {
|
|
2607
|
-
if ((message == null ? void 0 : message.id) === id) {
|
|
2608
|
-
if (message.type === RpcGMCallTypes.RESOLVE) {
|
|
2609
|
-
resolveResult(message.value);
|
|
2610
|
-
} else if (message.type === RpcGMCallTypes.REJECT) {
|
|
2611
|
-
rejectResult(message.error);
|
|
2612
|
-
}
|
|
2613
|
-
}
|
|
2614
|
-
if (once && (childProcess == null ? void 0 : childProcess.kill)) {
|
|
2615
|
-
childProcess.kill("SIGTERM");
|
|
2616
|
-
}
|
|
2617
|
-
}, "handleMessage");
|
|
2618
|
-
const handleClose = /* @__PURE__ */ __name((code, signal) => {
|
|
2619
|
-
rejectResult(new RpcExitError(code ? `Process ${childProcess.pid} exited with code ${code}${signal ? ` [${signal}]` : ""}` : `Process ${childProcess.pid} exited${signal ? ` [${signal}]` : ""}`, code, signal));
|
|
2620
|
-
removeHandlers();
|
|
2621
|
-
}, "handleClose");
|
|
2622
|
-
const removeHandlers = /* @__PURE__ */ __name(() => {
|
|
2623
|
-
childProcess.off("message", handleMessage);
|
|
2624
|
-
childProcess.off("close", handleClose);
|
|
2625
|
-
}, "removeHandlers");
|
|
2626
|
-
if (once) {
|
|
2627
|
-
childProcess.once("message", handleMessage);
|
|
2628
|
-
} else {
|
|
2629
|
-
childProcess.on("message", handleMessage);
|
|
2630
|
-
}
|
|
2631
|
-
childProcess.on("close", handleClose);
|
|
2632
|
-
childProcess.send({
|
|
2633
|
-
type: RpcGMCallTypes.CALL,
|
|
2634
|
-
id,
|
|
2635
|
-
args
|
|
2636
|
-
}, (error2) => {
|
|
2637
|
-
if (error2) {
|
|
2638
|
-
rejectSend(error2);
|
|
2639
|
-
removeHandlers();
|
|
2640
|
-
} else {
|
|
2641
|
-
resolveSend(void 0);
|
|
2642
|
-
}
|
|
2643
|
-
});
|
|
2644
|
-
return sendPromise.then(() => resultPromise);
|
|
2645
|
-
});
|
|
2646
|
-
}
|
|
2647
|
-
__name(wrapRpc, "wrapRpc");
|
|
2648
|
-
|
|
2649
|
-
// src/core/rpc/rpc-worker.ts
|
|
2650
|
-
var child_process = __toESM(require("child_process"));
|
|
2651
|
-
var process3 = __toESM(require("process"));
|
|
2652
|
-
var import_crypto2 = require("crypto");
|
|
2653
|
-
var FEDERATION_WORKER_DATA_ENV_KEY = "VMOK_WORKER_DATA_ENV";
|
|
2654
|
-
function createRpcWorker(modulePath, data, memoryLimit, once) {
|
|
2655
|
-
const options = {
|
|
2656
|
-
env: __spreadProps(__spreadValues({}, process3.env), {
|
|
2657
|
-
[FEDERATION_WORKER_DATA_ENV_KEY]: JSON.stringify(data || {})
|
|
2658
|
-
}),
|
|
2659
|
-
stdio: [
|
|
2660
|
-
"inherit",
|
|
2661
|
-
"inherit",
|
|
2662
|
-
"inherit",
|
|
2663
|
-
"ipc"
|
|
2664
|
-
],
|
|
2665
|
-
serialization: "advanced"
|
|
2666
|
-
};
|
|
2667
|
-
if (memoryLimit) {
|
|
2668
|
-
options.execArgv = [
|
|
2669
|
-
`--max-old-space-size=${memoryLimit}`
|
|
2670
|
-
];
|
|
2671
|
-
}
|
|
2672
|
-
let childProcess, remoteMethod;
|
|
2673
|
-
const id = (0, import_crypto2.randomUUID)();
|
|
2674
|
-
const worker = {
|
|
2675
|
-
connect(...args) {
|
|
2676
|
-
if (childProcess && !childProcess.connected) {
|
|
2677
|
-
childProcess.send({
|
|
2678
|
-
type: RpcGMCallTypes.EXIT,
|
|
2679
|
-
id
|
|
2680
|
-
});
|
|
2681
|
-
childProcess = void 0;
|
|
2682
|
-
remoteMethod = void 0;
|
|
2683
|
-
}
|
|
2684
|
-
if (!(childProcess == null ? void 0 : childProcess.connected)) {
|
|
2685
|
-
childProcess = child_process.fork(modulePath, options);
|
|
2686
|
-
remoteMethod = wrapRpc(childProcess, {
|
|
2687
|
-
id,
|
|
2688
|
-
once
|
|
2689
|
-
});
|
|
2690
|
-
}
|
|
2691
|
-
if (!remoteMethod) {
|
|
2692
|
-
return Promise.reject(new Error("Worker is not connected - cannot perform RPC."));
|
|
2693
|
-
}
|
|
2694
|
-
return remoteMethod(...args);
|
|
2695
|
-
},
|
|
2696
|
-
terminate() {
|
|
2697
|
-
try {
|
|
2698
|
-
if (childProcess.connected) {
|
|
2699
|
-
childProcess.send({
|
|
2700
|
-
type: RpcGMCallTypes.EXIT,
|
|
2701
|
-
id
|
|
2702
|
-
}, (err) => {
|
|
2703
|
-
if (err) {
|
|
2704
|
-
console.error("Error sending message:", err);
|
|
2705
|
-
}
|
|
2706
|
-
});
|
|
2707
|
-
}
|
|
2708
|
-
} catch (error2) {
|
|
2709
|
-
if (error2.code === "EPIPE") {
|
|
2710
|
-
console.error("Pipe closed before message could be sent:", error2);
|
|
2711
|
-
} else {
|
|
2712
|
-
console.error("Unexpected error:", error2);
|
|
2713
|
-
}
|
|
2714
|
-
}
|
|
2715
|
-
childProcess = void 0;
|
|
2716
|
-
remoteMethod = void 0;
|
|
2717
|
-
},
|
|
2718
|
-
get connected() {
|
|
2719
|
-
return Boolean(childProcess == null ? void 0 : childProcess.connected);
|
|
2720
|
-
},
|
|
2721
|
-
get process() {
|
|
2722
|
-
return childProcess;
|
|
2723
|
-
},
|
|
2724
|
-
get id() {
|
|
2725
|
-
return id;
|
|
2726
|
-
}
|
|
2727
|
-
};
|
|
2728
|
-
return worker;
|
|
2729
|
-
}
|
|
2730
|
-
__name(createRpcWorker, "createRpcWorker");
|
|
2731
|
-
function getRpcWorkerData() {
|
|
2732
|
-
return JSON.parse(process3.env[FEDERATION_WORKER_DATA_ENV_KEY] || "{}");
|
|
2733
|
-
}
|
|
2734
|
-
__name(getRpcWorkerData, "getRpcWorkerData");
|
|
2735
|
-
|
|
2736
|
-
// src/dev-worker/forkDevWorker.ts
|
|
2737
|
-
var import_sdk7 = require("@module-federation/sdk");
|
|
2738
|
-
|
|
2739
|
-
// src/dev-worker/utils.ts
|
|
2740
|
-
var DEFAULT_LOCAL_IPS = [
|
|
2741
|
-
"localhost",
|
|
2742
|
-
"127.0.0.1"
|
|
2743
|
-
];
|
|
2744
|
-
function getIpFromEntry(entry, ipv4) {
|
|
2745
|
-
let ip;
|
|
2746
|
-
entry.replace(/https?:\/\/([0-9|.]+|localhost):/, (str, matched) => {
|
|
2747
|
-
ip = matched;
|
|
2748
|
-
return str;
|
|
2749
|
-
});
|
|
2750
|
-
if (ip) {
|
|
2751
|
-
return DEFAULT_LOCAL_IPS.includes(ip) ? ipv4 : ip;
|
|
2752
|
-
}
|
|
2753
|
-
}
|
|
2754
|
-
__name(getIpFromEntry, "getIpFromEntry");
|
|
2755
|
-
|
|
2756
|
-
// src/dev-worker/forkDevWorker.ts
|
|
2757
|
-
var typesManager;
|
|
2758
|
-
var serverAddress;
|
|
2759
|
-
var moduleServer;
|
|
2760
|
-
var cacheOptions;
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
+
const require_Action = require('./Action-CzhPMw2i.js');
|
|
3
|
+
const require_Broker = require('./Broker-DRlzScTT.js');
|
|
4
|
+
const require_expose_rpc = require('./expose-rpc-BLAH20uj.js');
|
|
5
|
+
require('./consumeTypes-AD2ig87l.js');
|
|
6
|
+
require('./core.js');
|
|
7
|
+
const require_utils = require('./utils-7KqCZHbb.js');
|
|
8
|
+
let _module_federation_sdk = require("@module-federation/sdk");
|
|
9
|
+
|
|
10
|
+
//#region src/dev-worker/forkDevWorker.ts
|
|
11
|
+
let typesManager, serverAddress, moduleServer, cacheOptions;
|
|
2761
12
|
function getLocalRemoteNames(options, encodeNameIdentifier) {
|
|
2762
|
-
|
|
2763
|
-
|
|
2764
|
-
|
|
2765
|
-
|
|
2766
|
-
|
|
2767
|
-
|
|
2768
|
-
|
|
2769
|
-
|
|
2770
|
-
|
|
2771
|
-
|
|
2772
|
-
|
|
2773
|
-
|
|
2774
|
-
|
|
2775
|
-
|
|
2776
|
-
|
|
2777
|
-
|
|
2778
|
-
|
|
2779
|
-
|
|
13
|
+
if (!options) return [];
|
|
14
|
+
let hostConfig;
|
|
15
|
+
try {
|
|
16
|
+
hostConfig = require_expose_rpc.retrieveHostConfig(options);
|
|
17
|
+
} catch (e) {
|
|
18
|
+
require_Broker.fileLog(`getLocalRemoteNames: retrieveHostConfig failed: ${e.message}`, "forkDevWorker", "warn");
|
|
19
|
+
return [];
|
|
20
|
+
}
|
|
21
|
+
const { mapRemotesToDownload } = hostConfig;
|
|
22
|
+
return Object.keys(mapRemotesToDownload).reduce((sum, remoteModuleName) => {
|
|
23
|
+
const remoteInfo = mapRemotesToDownload[remoteModuleName];
|
|
24
|
+
const name = encodeNameIdentifier ? (0, _module_federation_sdk.decodeName)(remoteInfo.name, encodeNameIdentifier) : remoteInfo.name;
|
|
25
|
+
const ip = require_utils.getIpFromEntry(remoteInfo.url, require_Broker.getIPV4());
|
|
26
|
+
if (!ip) return sum;
|
|
27
|
+
sum.push({
|
|
28
|
+
name,
|
|
29
|
+
entry: remoteInfo.url,
|
|
30
|
+
ip
|
|
31
|
+
});
|
|
32
|
+
return sum;
|
|
33
|
+
}, []);
|
|
34
|
+
}
|
|
35
|
+
async function updateCallback({ updateMode, name, remoteTypeTarPath, remoteInfo, once }) {
|
|
36
|
+
const { disableHotTypesReload, disableLiveReload } = cacheOptions || {};
|
|
37
|
+
require_Broker.fileLog(`sync remote module ${name}, types to ${cacheOptions?.name},typesManager.updateTypes run`, "forkDevWorker", "info");
|
|
38
|
+
if (!disableLiveReload && moduleServer) moduleServer.update({
|
|
39
|
+
updateKind: require_Broker.UpdateKind.RELOAD_PAGE,
|
|
40
|
+
updateMode: require_Action.UpdateMode.PASSIVE
|
|
41
|
+
});
|
|
42
|
+
if (!disableHotTypesReload && typesManager) await typesManager.updateTypes({
|
|
43
|
+
updateMode,
|
|
44
|
+
remoteName: name,
|
|
45
|
+
remoteTarPath: remoteTypeTarPath,
|
|
46
|
+
remoteInfo,
|
|
47
|
+
once
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
async function forkDevWorker(options, action) {
|
|
51
|
+
if (!typesManager) {
|
|
52
|
+
const { name, remote, host, extraOptions } = options;
|
|
53
|
+
typesManager = new (require_expose_rpc.getDTSManagerConstructor(remote?.implementation))({
|
|
54
|
+
remote,
|
|
55
|
+
host,
|
|
56
|
+
extraOptions
|
|
57
|
+
});
|
|
58
|
+
if (!options.disableHotTypesReload && remote) {
|
|
59
|
+
const { remoteOptions, tsConfig } = require_expose_rpc.retrieveRemoteConfig(remote);
|
|
60
|
+
const mfTypesZipPath = require_expose_rpc.retrieveTypesZipPath(require_expose_rpc.retrieveMfTypesPath(tsConfig, remoteOptions), remoteOptions);
|
|
61
|
+
await Promise.all([require_expose_rpc.createHttpServer({ typeTarPath: mfTypesZipPath }).then((res) => {
|
|
62
|
+
serverAddress = res.serverAddress;
|
|
63
|
+
}), typesManager.generateTypes()]).catch((err) => {
|
|
64
|
+
require_Broker.fileLog(`${name} module generateTypes done, localServerAddress: ${JSON.stringify(err)}`, "forkDevWorker", "error");
|
|
65
|
+
});
|
|
66
|
+
require_Broker.fileLog(`${name} module generateTypes done, localServerAddress: ${serverAddress}`, "forkDevWorker", "info");
|
|
67
|
+
}
|
|
68
|
+
moduleServer = new require_expose_rpc.ModuleFederationDevServer({
|
|
69
|
+
name,
|
|
70
|
+
remotes: getLocalRemoteNames(host, extraOptions?.["encodeNameIdentifier"]),
|
|
71
|
+
updateCallback,
|
|
72
|
+
remoteTypeTarPath: `${serverAddress}/${require_Action.DEFAULT_TAR_NAME}`
|
|
73
|
+
});
|
|
74
|
+
cacheOptions = options;
|
|
75
|
+
}
|
|
76
|
+
if (action === "update" && cacheOptions) {
|
|
77
|
+
require_Broker.fileLog(`remoteModule ${cacheOptions.name} receive devWorker update, start typesManager.updateTypes `, "forkDevWorker", "info");
|
|
78
|
+
if (!cacheOptions.disableLiveReload) moduleServer?.update({
|
|
79
|
+
updateKind: require_Broker.UpdateKind.RELOAD_PAGE,
|
|
80
|
+
updateMode: require_Action.UpdateMode.POSITIVE
|
|
81
|
+
});
|
|
82
|
+
if (!cacheOptions.disableHotTypesReload) typesManager?.updateTypes({
|
|
83
|
+
updateMode: require_Action.UpdateMode.POSITIVE,
|
|
84
|
+
remoteName: cacheOptions.name
|
|
85
|
+
}).then(() => {
|
|
86
|
+
moduleServer?.update({
|
|
87
|
+
updateKind: require_Broker.UpdateKind.UPDATE_TYPE,
|
|
88
|
+
updateMode: require_Action.UpdateMode.POSITIVE
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
}
|
|
2780
92
|
}
|
|
2781
|
-
__name(getLocalRemoteNames, "getLocalRemoteNames");
|
|
2782
|
-
function updateCallback(_0) {
|
|
2783
|
-
return __async(this, arguments, function* ({ updateMode, name, remoteTypeTarPath, remoteInfo, once }) {
|
|
2784
|
-
const { disableHotTypesReload, disableLiveReload } = cacheOptions || {};
|
|
2785
|
-
fileLog(`sync remote module ${name}, types to ${cacheOptions == null ? void 0 : cacheOptions.name},typesManager.updateTypes run`, "forkDevWorker", "info");
|
|
2786
|
-
if (!disableLiveReload && moduleServer) {
|
|
2787
|
-
moduleServer.update({
|
|
2788
|
-
updateKind: UpdateKind.RELOAD_PAGE,
|
|
2789
|
-
updateMode: UpdateMode.PASSIVE
|
|
2790
|
-
});
|
|
2791
|
-
}
|
|
2792
|
-
if (!disableHotTypesReload && typesManager) {
|
|
2793
|
-
yield typesManager.updateTypes({
|
|
2794
|
-
updateMode,
|
|
2795
|
-
remoteName: name,
|
|
2796
|
-
remoteTarPath: remoteTypeTarPath,
|
|
2797
|
-
remoteInfo,
|
|
2798
|
-
once
|
|
2799
|
-
});
|
|
2800
|
-
}
|
|
2801
|
-
});
|
|
2802
|
-
}
|
|
2803
|
-
__name(updateCallback, "updateCallback");
|
|
2804
|
-
function forkDevWorker(options, action) {
|
|
2805
|
-
return __async(this, null, function* () {
|
|
2806
|
-
if (!typesManager) {
|
|
2807
|
-
const { name, remote, host, extraOptions } = options;
|
|
2808
|
-
const DTSManagerConstructor = getDTSManagerConstructor(remote == null ? void 0 : remote.implementation);
|
|
2809
|
-
typesManager = new DTSManagerConstructor({
|
|
2810
|
-
remote,
|
|
2811
|
-
host,
|
|
2812
|
-
extraOptions
|
|
2813
|
-
});
|
|
2814
|
-
if (!options.disableHotTypesReload && remote) {
|
|
2815
|
-
const { remoteOptions, tsConfig } = retrieveRemoteConfig(remote);
|
|
2816
|
-
const mfTypesPath = retrieveMfTypesPath(tsConfig, remoteOptions);
|
|
2817
|
-
const mfTypesZipPath = retrieveTypesZipPath(mfTypesPath, remoteOptions);
|
|
2818
|
-
yield Promise.all([
|
|
2819
|
-
createKoaServer({
|
|
2820
|
-
typeTarPath: mfTypesZipPath
|
|
2821
|
-
}).then((res) => {
|
|
2822
|
-
serverAddress = res.serverAddress;
|
|
2823
|
-
}),
|
|
2824
|
-
typesManager.generateTypes()
|
|
2825
|
-
]).catch((err) => {
|
|
2826
|
-
fileLog(`${name} module generateTypes done, localServerAddress: ${JSON.stringify(err)}`, "forkDevWorker", "error");
|
|
2827
|
-
});
|
|
2828
|
-
fileLog(`${name} module generateTypes done, localServerAddress: ${serverAddress}`, "forkDevWorker", "info");
|
|
2829
|
-
}
|
|
2830
|
-
moduleServer = new ModuleFederationDevServer({
|
|
2831
|
-
name,
|
|
2832
|
-
remotes: getLocalRemoteNames(host, extraOptions == null ? void 0 : extraOptions["encodeNameIdentifier"]),
|
|
2833
|
-
updateCallback,
|
|
2834
|
-
remoteTypeTarPath: `${serverAddress}/${DEFAULT_TAR_NAME}`
|
|
2835
|
-
});
|
|
2836
|
-
cacheOptions = options;
|
|
2837
|
-
}
|
|
2838
|
-
if (action === "update" && cacheOptions) {
|
|
2839
|
-
fileLog(`remoteModule ${cacheOptions.name} receive devWorker update, start typesManager.updateTypes `, "forkDevWorker", "info");
|
|
2840
|
-
if (!cacheOptions.disableLiveReload) {
|
|
2841
|
-
moduleServer == null ? void 0 : moduleServer.update({
|
|
2842
|
-
updateKind: UpdateKind.RELOAD_PAGE,
|
|
2843
|
-
updateMode: UpdateMode.POSITIVE
|
|
2844
|
-
});
|
|
2845
|
-
}
|
|
2846
|
-
if (!cacheOptions.disableHotTypesReload) {
|
|
2847
|
-
typesManager == null ? void 0 : typesManager.updateTypes({
|
|
2848
|
-
updateMode: UpdateMode.POSITIVE,
|
|
2849
|
-
remoteName: cacheOptions.name
|
|
2850
|
-
}).then(() => {
|
|
2851
|
-
moduleServer == null ? void 0 : moduleServer.update({
|
|
2852
|
-
updateKind: UpdateKind.UPDATE_TYPE,
|
|
2853
|
-
updateMode: UpdateMode.POSITIVE
|
|
2854
|
-
});
|
|
2855
|
-
});
|
|
2856
|
-
}
|
|
2857
|
-
}
|
|
2858
|
-
});
|
|
2859
|
-
}
|
|
2860
|
-
__name(forkDevWorker, "forkDevWorker");
|
|
2861
93
|
process.on("message", (message) => {
|
|
2862
|
-
|
|
2863
|
-
|
|
2864
|
-
|
|
2865
|
-
|
|
2866
|
-
|
|
2867
|
-
|
|
2868
|
-
});
|
|
2869
|
-
rpc_exports.exposeRpc(forkDevWorker);
|
|
2870
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
2871
|
-
0 && (module.exports = {
|
|
2872
|
-
forkDevWorker
|
|
94
|
+
require_Broker.fileLog(`ChildProcess(${process.pid}), message: ${JSON.stringify(message)} `, "forkDevWorker", "info");
|
|
95
|
+
if (message.type === require_expose_rpc.RpcGMCallTypes.EXIT) {
|
|
96
|
+
require_Broker.fileLog(`ChildProcess(${process.pid}) SIGTERM, Federation DevServer will exit...`, "forkDevWorker", "error");
|
|
97
|
+
moduleServer?.exit();
|
|
98
|
+
process.exit(0);
|
|
99
|
+
}
|
|
2873
100
|
});
|
|
101
|
+
require_expose_rpc.exposeRpc(forkDevWorker);
|
|
102
|
+
|
|
103
|
+
//#endregion
|
|
104
|
+
exports.forkDevWorker = forkDevWorker;
|