-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathintegrations.js
More file actions
254 lines (228 loc) · 12.8 KB
/
integrations.js
File metadata and controls
254 lines (228 loc) · 12.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
const EleventyFetch = require("@11ty/eleventy-fetch");
const certifiedNodes = require("./certifiedNodes");
module.exports = async () => {
console.log("Loading Integrations...");
const api = "https://ff-integrations.flowfuse.cloud/api/nodes";
const cacheDuration = "1h";
const response = await EleventyFetch(api, {
duration: cacheDuration,
type: "json"
});
// Get certified nodes first
const nodes = await certifiedNodes();
const ffNodesMap = nodes.reduce((acc, node) => {
acc[node.id] = node;
return acc;
}, {});
// Sort by weekly downloads and get top 50 nodes
const topNodes = response.catalogue
.sort((a, b) => b.downloads.week - a.downloads.week)
.slice(0, 50); // Limit to top 50 downloaded nodes
// Create a map of top nodes by ID for quick lookup
const topNodesMap = topNodes.reduce((acc, node) => {
acc[node._id] = node;
return acc;
}, {});
// Merge: ensure all certified nodes are included
// Add any certified nodes that aren't in the top 50
const certifiedNodeIds = Object.keys(ffNodesMap);
certifiedNodeIds.forEach(certifiedId => {
if (!topNodesMap[certifiedId]) {
// Find the certified node in the full catalogue
const certifiedNode = response.catalogue.find(n => n._id === certifiedId);
if (certifiedNode) {
topNodes.push(certifiedNode);
}
}
});
const data = Promise.all(
topNodes.map(async (node) => {
// Mark FlowFuse certified nodes
if (ffNodesMap[node._id]) {
node.ffCertified = true;
}
// Ensure categories exist
if (!node.categories) {
node.categories = [];
}
// Ensure unique catalogue-based collection names
node.categories = node.categories.map(category =>
category.includes("catalogue")
? category
: "catalogue_" + category
);
if (!node.categories.includes("catalogue")) {
node.categories.push("catalogue");
}
const stripRelativeTags = (html) => html
.replace(/<link\b[^>]*?\bhref=(?:["']|")(?!https?:\/\/)[^"'>&]*(?:["']|")[^>]*\/?>/gi, '')
.replace(/<script\b[^>]*?\bsrc=(?:["']|")(?!https?:\/\/)[^"'>&]*(?:["']|")[^>]*>(?:[\s\S]*?<\/script>)?/gi, '');
// Fetch full npm node details (readme, etc.)
try {
const nodeDetails = await EleventyFetch(
`https://registry.npmjs.org/${node._id}`,
{
duration: cacheDuration,
type: "json"
}
);
// Extract additional metadata
node.author = nodeDetails.author;
node.maintainers = nodeDetails.maintainers || [];
node.homepage = nodeDetails.homepage;
node.bugs = nodeDetails.bugs;
node.repository = nodeDetails.repository;
node.time = nodeDetails.time;
node.lastUpdated = nodeDetails.time?.modified || nodeDetails.time?.[node.version];
node.created = nodeDetails.time?.created;
// Extract license from npm registry
node.license = nodeDetails.license || nodeDetails.versions?.[node.version]?.license;
// Extract GitHub info if repository is GitHub
if (nodeDetails.repository?.url) {
const repoUrl = nodeDetails.repository.url
.replace('git+', '')
.replace('.git', '')
.replace('git://', 'https://');
const githubMatch = repoUrl.match(/github\.com\/([^\/]+)\/([^\/]+)/);
if (githubMatch) {
node.githubOwner = githubMatch[1];
node.githubRepo = githubMatch[2];
// Try to fetch examples from GitHub
try {
const examplesUrl = `https://api.github.com/repos/${node.githubOwner}/${node.githubRepo}/contents/examples`;
const examplesResponse = await EleventyFetch(examplesUrl, {
duration: cacheDuration,
type: "json",
fetchOptions: {
headers: {
'User-Agent': 'FlowFuse-Website'
}
}
});
// Filter for .json files (Node-RED flows)
if (Array.isArray(examplesResponse)) {
const exampleFiles = examplesResponse
.filter(file => file.name.endsWith('.json') && file.type === 'file');
// Fetch the actual flow content for each example
node.examples = await Promise.all(
exampleFiles.map(async (file) => {
try {
// Fetch the raw flow JSON content
const flowContent = await EleventyFetch(file.download_url, {
duration: cacheDuration,
type: "text",
fetchOptions: {
headers: {
'User-Agent': 'FlowFuse-Website'
}
}
});
const escapeForHtml = (s) => s.replace(/&/g, '\\u0026').replace(/</g, '\\u003c').replace(/>/g, '\\u003e');
let sanitizedFlow = escapeForHtml(flowContent);
try {
const flowJson = JSON.parse(flowContent);
const sanitizeNode = (n) => {
if (n && typeof n === 'object') {
if (typeof n.template === 'string') n.template = stripRelativeTags(n.template);
if (typeof n.html === 'string') n.html = stripRelativeTags(n.html);
}
return n;
};
if (Array.isArray(flowJson)) {
flowJson.forEach(sanitizeNode);
} else {
sanitizeNode(flowJson);
}
sanitizedFlow = JSON.stringify(flowJson)
.replace(/&/g, '\\u0026')
.replace(/</g, '\\u003c')
.replace(/>/g, '\\u003e');
} catch (_) { /* keep original if not valid JSON */ }
return {
name: file.name.replace('.json', ''), // Remove .json extension for display
path: file.path,
url: file.html_url,
downloadUrl: file.download_url,
flow: sanitizedFlow
};
} catch (err) {
console.error(`Failed to fetch flow content for ${file.name}:`, err.message);
// Return without flow content if fetch fails
return {
name: file.name.replace('.json', ''),
path: file.path,
url: file.html_url,
downloadUrl: file.download_url
};
}
})
);
}
} catch (err) {
// Examples folder doesn't exist or API error - this is fine, just skip
node.examples = [];
}
}
}
if (nodeDetails.readme) {
// Fix relative image paths to use GitHub raw content
node.readme = nodeDetails.readme
// Fix relative image paths in markdown style
.replace(
/!\[(.*?)\]\((?!https?:\/\/)([^)]+)\)/g,
(match, alt, imagePath) => {
// If we have GitHub info, construct the raw GitHub URL
if (node.githubOwner && node.githubRepo && imagePath) {
// Clean up the path - remove leading ./ or ../
const cleanPath = imagePath.replace(/^(\.\.\/)+/, '').replace(/^\.\//, '');
// Use the default branch (usually main or master)
const rawUrl = `https://raw.githubusercontent.com/${node.githubOwner}/${node.githubRepo}/master/${cleanPath}`;
return ``;
}
// If no GitHub info, return the match as-is (will be broken, but at least visible)
return match;
}
)
// Fix relative image paths in HTML img tags
.replace(
/<img([^>]*?)src=["']((?!https?:\/\/)(\.\.\/)?(\.\/)?[^"']+)["']([^>]*?)>/gi,
(match, before, src, after) => {
// If we have GitHub info, construct the raw GitHub URL
if (node.githubOwner && node.githubRepo) {
// Clean up the path - remove leading ./ or ../
const cleanPath = src.replace(/^(\.\.\/)+/, '').replace(/^\.\//, '');
// Use the default branch (usually main or master)
const rawUrl = `https://raw.githubusercontent.com/${node.githubOwner}/${node.githubRepo}/master/${cleanPath}`;
return `<img${before}src="${rawUrl}"${after}>`;
}
// If no GitHub info, return the match as-is
return match;
}
);
node.readme = stripRelativeTags(node.readme);
} else {
node.readme = "";
}
// console.log(`Loaded readme for ${node._id}`);
} catch (err) {
// Only log non-404 errors to avoid cluttering console with missing packages
if (!err.message || !err.message.includes('404')) {
console.error(`Failed to load readme for ${node._id}`, err);
}
node.readme = "";
}
return node;
})
).then((nodes) =>
nodes
.sort((a, b) => {
// Certified nodes first
if (a.ffCertified && !b.ffCertified) return -1;
if (!a.ffCertified && b.ffCertified) return 1;
// Then by weekly downloads (descending)
return b.downloads.week - a.downloads.week;
})
);
console.log("Loaded Integrations.");
return data;
};