-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathgridsome.server.js
More file actions
210 lines (175 loc) · 6.62 KB
/
Copy pathgridsome.server.js
File metadata and controls
210 lines (175 loc) · 6.62 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
// Server API makes it possible to hook into various parts of Gridsome
// on server-side and add custom data to the GraphQL data layer.
// Learn more: https://gridsome.org/docs/server-api/
// Changes here require a server restart.
// To restart press CTRL + C in terminal and run `gridsome develop`
const axios = require('axios')
const webp = require('webp-converter');
const fs = require('fs');
const path = require('path');
const fetch = require('node-fetch');
const { Feed } = require('feed');
const {
sanitizeForRss,
parseFeedDate,
absoluteUrl,
} = require('./gridsome-rss-utils');
let latestSecurityBlogPosts = [];
webp.grant_permission();
// get remote blog images and convert them to webp
async function convertRemoteBlogImages() {
//get data from jfrog.com
const {data} = await axios.get(`https://jfrog.com/latest-security-posts/`)
parsedPosts = [...data]
const imageUrls = []
for (let i = 0; i < parsedPosts.length; i++) {
const post = parsedPosts[i];
const remoteURL = post.img
//get remote file
const response = await fetch(remoteURL);
const buffer = await response.buffer();
//image extension
const fileStrSplit = remoteURL.split('.')
const ext = fileStrSplit[fileStrSplit.length-1]
//write normal file locally, and add a converted webp version
fs.writeFile(`./static/latest-posts-${i}.${ext}`, buffer, () => {
console.log(`finished downloading ${remoteURL} ! | Saved to ./static/latest-posts-${i}.${ext} `)
const result = webp.cwebp(
`./static/latest-posts-${i}.${ext}`,
`./static/latest-posts-${i}.webp`,
"-q 80",
logging="-v"
);
result.then((response) => {
console.log(response);
});
});
}
}
convertRemoteBlogImages()
module.exports = function(api) {
api.loadSource(
async (store) => {
store.addMetadata("baseURL", "https://research.jfrog.com");
const domain = 'jfrog.local'
//for resting purposes on dev only
// process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
const {data} = await axios.get(`https://jfrog.com/latest-security-posts`)
const Log4shellPost = await axios.get(`https://jfrog.com/latest-log4shell-posts`)
const springShellPost = await axios.get(`https://jfrog.com/latest-springshell-posts`)
const NpmToolsPost = await axios.get(`https://jfrog.com/latest-npmtools-posts`)
const post = data.map((post,imageIndex)=>{
post.img='/latest-posts-'+imageIndex+'.webp';
return post;
})
latestSecurityBlogPosts = post;
store.addMetadata("latestPostsJSON", JSON.stringify(post))
store.addMetadata("latestLog4ShellPostsJSON", JSON.stringify(Log4shellPost.data))
store.addMetadata("latestSpringShellPostsJSON", JSON.stringify(springShellPost.data))
store.addMetadata("latestNpmToolsPostsJSON", JSON.stringify(NpmToolsPost.data))
},
({ addSchemaTypes }) => {
addSchemaTypes(`
type Post implements Node {
id: ID!
title: String
published: Boolean
description: String
date_published: Date
last_updated: Date
xray_id: String
vul_id: String
severity: String
discovered_by: String
type: String
platform: String
downloads_text: String
cvss: String
tag: String
img: String
excerpt: String
minutes: String
date: Date,
schema: String
canonical:String
published_on_hp:Boolean
}
`)
}
);
api.afterBuild(({ config }) => {
const siteUrl = config.siteUrl;
if (!siteUrl) return;
const store = api.store;
const postCollection = store.getCollection('Post');
const realTimeCollection = store.getCollection('realTimePost');
const posts = (postCollection && postCollection.collection && postCollection.collection.data) || [];
const realTimePosts = (realTimeCollection && realTimeCollection.collection && realTimeCollection.collection.data) || [];
const vulnerabilityItems = posts
.filter((node) => node.type === 'vulnerability')
.sort((a, b) => parseFeedDate(b.date_published) - parseFeedDate(a.date_published))
.slice(0, 4)
.map((node) => ({
title: sanitizeForRss(node.title),
date: parseFeedDate(node.date_published),
link: absoluteUrl(siteUrl, node.path),
description: sanitizeForRss(node.description),
content: sanitizeForRss(node.content),
author: [{ name: sanitizeForRss(node.discovered_by || 'JFrog Security Research') }],
}));
const localRealTimePosts = realTimePosts.filter(
(node) => node.type === 'realTimePost' && node.published_on_hp !== false
);
const latestFromBlogItems = [...latestSecurityBlogPosts, ...localRealTimePosts]
.sort((a, b) => parseFeedDate(b.date) - parseFeedDate(a.date))
.slice(0, 5)
.map((node) => {
if (node.path) {
return {
title: sanitizeForRss(node.title),
date: parseFeedDate(node.date),
link: absoluteUrl(siteUrl, node.path),
description: sanitizeForRss(node.excerpt || node.description),
content: sanitizeForRss(node.content),
author: [{ name: sanitizeForRss(node.description || 'JFrog Security Research') }],
};
}
return {
title: sanitizeForRss(node.title),
date: parseFeedDate(node.date),
link: node.href,
description: sanitizeForRss(node.excerpt || node.description),
content: sanitizeForRss(node.excerpt || node.description),
author: [{ name: 'JFrog Security Research' }],
};
});
const feedItems = [...vulnerabilityItems, ...latestFromBlogItems].sort(
(a, b) => b.date - a.date
);
const feed = new Feed({
title: 'JFrog Security Research',
description:
'Homepage feed: latest vulnerabilities and security research posts from JFrog Security Research.',
link: siteUrl,
id: siteUrl,
language: 'en',
generator: 'Gridsome Home RSS',
feedLinks: {
rss: absoluteUrl(siteUrl, '/rss.xml'),
},
});
feedItems.forEach((item) => {
feed.addItem({
...item,
id: item.link,
});
});
const outDir = config.outputDir || config.outDir || './dist';
const outputPath = path.join(outDir, 'rss.xml');
fs.writeFileSync(outputPath, feed.rss2());
console.log('Generate RSS feed at /rss.xml');
});
// api.createPages(({ createPage }) => {
// // Use the Pages API here: https://gridsome.org/docs/pages-api/
// })
};