Skip to content

Commit b77e7b1

Browse files
feat: updated injector to receive a simpler format to load the remote (#416)
* feat: updated injector to receive a simpler format to load the remote * feat: updated /api/get-remote to remove graphql and serve the new simple format * fix read write token Co-authored-by: ScriptedAlchemy <[email protected]>
1 parent 778b7be commit b77e7b1

File tree

8 files changed

+251
-265
lines changed

8 files changed

+251
-265
lines changed

dashboard-example/dsl/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"@webpack-cli/serve": "1.5.2",
1010
"babel-loader": "8.2.2",
1111
"css-loader": "5.2.7",
12-
"esbuild-loader": "2.15.1",
12+
"esbuild-loader": "2.19.0",
1313
"html-webpack-plugin": "5.3.2",
1414
"less": "4.1.2",
1515
"less-loader": "8.1.1",

dashboard-example/home/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"dependencies": {
2828
"@ant-design/icons": "^4.1.0",
2929
"antd": "^4.2.5",
30-
"esbuild-loader": "^2.11.1",
30+
"esbuild-loader": "^2.19.0",
3131
"lodash": "^4.17.15",
3232
"react": "17.0.2",
3333
"react-dom": "17.0.2"

dashboard-example/home/webpack.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ module.exports = {
7474
process.env.VERCEL_URL
7575
? "https://federation-dashboard-alpha.vercel.app"
7676
: "http://localhost:3000"
77-
}/api/get-remote?token=${process.env.DASHBOARD_WRITE_TOKEN}`,
77+
}/api/get-remote?token=${process.env.DASHBOARD_READ_TOKEN}`,
7878
}),
7979
search: clientVersion({
8080
currentHost: "home",

dashboard-fe/pages/api/get-remote.js

Lines changed: 0 additions & 64 deletions
This file was deleted.

dashboard-fe/pages/api/get-remote.ts

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import { MongoClient } from "mongodb";
2+
import { createHash, randomUUID } from "crypto";
3+
4+
export default async function handler(req, res) {
5+
res.setHeader("Access-Control-Allow-Credentials", true);
6+
res.setHeader("Access-Control-Allow-Origin", "*");
7+
res.setHeader("Access-Control-Allow-Methods", "GET,OPTIONS,POST");
8+
res.setHeader(
9+
"Access-Control-Allow-Headers",
10+
"X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version"
11+
);
12+
res.setHeader("Cache-Control", "no-cache");
13+
14+
if (req.method === "OPTIONS") {
15+
res.status(200).end();
16+
return;
17+
}
18+
const { currentHost, remoteName, token } = req.query;
19+
const id = randomUUID();
20+
const log = (msg) =>
21+
console.log(`REQ(${id}): ${currentHost}; ${remoteName}; ${msg}`);
22+
23+
const remoteFindLabel = "find-remote-" + id;
24+
console.time(remoteFindLabel);
25+
26+
try {
27+
const client = new MongoClient(process.env.MONGO_URL, {
28+
useNewUrlParser: true,
29+
useUnifiedTopology: true,
30+
});
31+
32+
await client.connect();
33+
34+
log("Connected to mongo db");
35+
36+
const mainDb = client.db("fmdashboard");
37+
const siteSettings = mainDb.collection("siteSettings");
38+
39+
const foundSettings = await siteSettings.findOne({
40+
tokens: { key: "readOnlyToken", value: token },
41+
});
42+
43+
if (!foundSettings) {
44+
log("User key not found");
45+
res.status(401).end();
46+
return;
47+
}
48+
49+
const userDbId = createHash("md5").update(foundSettings.id).digest("hex");
50+
51+
const userDb = client.db(userDbId);
52+
53+
if (!userDb) {
54+
log("User db not found");
55+
res.status(404).end();
56+
return;
57+
}
58+
59+
const appVersionCollection = userDb.collection("applicationVersions");
60+
const hostVersion = await appVersionCollection.findOne({
61+
applicationId: currentHost,
62+
"consumes.applicationID": remoteName,
63+
});
64+
65+
if (!hostVersion) {
66+
log("host not found");
67+
res.status(404).end();
68+
return;
69+
}
70+
71+
const app = await appVersionCollection.findOne({
72+
applicationId: remoteName,
73+
latest: true,
74+
});
75+
76+
if (!app) {
77+
log("remote version not found");
78+
res.status(404).end();
79+
return;
80+
}
81+
82+
const name = app.applicationId;
83+
const version = app.version;
84+
const remoteURL = app.metadata.find(
85+
({ name }) => name === "baseUrl"
86+
)?.value;
87+
88+
if (!remoteURL) {
89+
log("remote url not found");
90+
res.status(404).end();
91+
return;
92+
}
93+
94+
res.json({ name, version, remoteURL });
95+
console.timeEnd(remoteFindLabel);
96+
} catch (err) {
97+
res.status(500).json(err);
98+
}
99+
}

dashboard-plugin/FederationDashboardPlugin.js

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -519,28 +519,27 @@ class FederationDashboardPlugin {
519519
);
520520
}
521521

522-
postDashboardData(dashData) {
523-
return new Promise((resolve) => {
524-
console.log("this._options.dashboardURL", this._options.dashboardURL);
525-
fetch(this._options.dashboardURL, {
522+
async postDashboardData(dashData) {
523+
try {
524+
const res = await fetch(this._options.dashboardURL, {
526525
method: "POST",
527526
body: dashData,
528527
headers: {
529528
Accept: "application/json",
530529
"Content-type": "application/json",
531530
},
532-
})
533-
.then((resp) => resp.json())
534-
.then(resolve)
535-
.catch((e) => {
536-
console.warn(
537-
`Error posting data to dashboard URL: ${this._options.dashboardURL}`
538-
);
539-
console.error(e);
540-
resolve();
541-
});
542-
});
531+
});
532+
533+
if (!res.ok) throw new Error(msg);
534+
} catch (err) {
535+
console.warn(
536+
`Error posting data to dashboard URL: ${this._options.dashboardURL}`
537+
);
538+
console.error(e);
539+
}
543540
}
544541
}
545542

543+
FederationDashboardPlugin.clientVersion = require("./client-version");
544+
546545
module.exports = FederationDashboardPlugin;

dashboard-plugin/client-version.js

Lines changed: 6 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -60,39 +60,14 @@ module.exports = ({ currentHost, remoteName, dashboardURL }) => {
6060
Accept: "application/json",
6161
},
6262
})
63-
.then(function(res){
64-
return res.json()
65-
})
63+
.then((res) => res.json())
6664
.then(function(data){
67-
var metadata;
65+
// Here we have data as { name: string, remoteURL: string, version: string }
66+
var metadata = [{name: 'baseUrl', value: data.remoteURL}];
6867
${injectScript.toString()}
69-
if(data && data.groups && data.groups[0] && data.groups[0].applications && data.groups[0].applications[0]) {
70-
var currentApp = data.groups[0].applications[0];
71-
if (!data.groups[0].applications[0].overrides.length) {
72-
metadata = data.groups[0].applications[0].metadata
73-
injectScript(document, "script", "federation-dynamic-remote-${remoteName}",data.groups[0].applications[0].metadata).then(function() {
74-
resolve(window.${remoteName});
75-
metadata = null
76-
});
77-
return;
78-
}
79-
currentApp.overrides.map((override) => {
80-
var objVersion = override && override.version ? override.version.split('.').join('_') : "";
81-
82-
metadata = override.application.metadata
83-
return injectScript(
84-
document,
85-
"script",
86-
"federation-dynamic-remote-${remoteName}-" + objVersion,
87-
override,
88-
).then(function(){
89-
var versionedModule = "${remoteName}_" + objVersion;
90-
resolve(window[versionedModule])
91-
})
92-
});
93-
}
68+
return injectScript(document, "script", "federation-dynamic-remote-${remoteName}").then(function() {
69+
resolve(window.${remoteName});
70+
});
9471
})
95-
}).then((res)=>{
96-
return res;
9772
}).catch(console.error)`;
9873
};

0 commit comments

Comments
 (0)