Skip to content

Commit 9dc2365

Browse files
committed
Allow POST to PURGE for Cloudflare
1 parent 8fa6e71 commit 9dc2365

2 files changed

Lines changed: 26 additions & 11 deletions

File tree

src/api.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ export async function getVTStats(hash) {
199199
stats = data?.data?.attributes?.last_analysis_stats
200200
}
201201
if (!stats) await cacheError(key, 'VT Stats Not Found')
202-
await cacheSet(key, stats, 60 * 60 * 48)
202+
await cacheSet(key, stats, 60 * 60 * 24 * 5)
203203
return stats
204204
}
205205

@@ -279,6 +279,16 @@ export async function incrKey(key) {
279279
await client.incr(key)
280280
}
281281

282+
// export async function incrPurge(result) {
283+
// const multi = client.multi().incr('purge_count')
284+
// // multi.incr('purge_count')
285+
// if (result) {
286+
// multi.incr('purge_hit')
287+
// } else {
288+
// multi.incr('purge_miss')
289+
// }
290+
// }
291+
282292
export async function sendInflux() {
283293
if (!influxClient) return debug('InfluxDB Not Configured.')
284294
debug(`Processing Influx: ${new Date().toLocaleString()}`)

src/app.js

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,8 @@ app.get('/colors{/:index}', async (req, res) => {
133133
// })
134134

135135
app.all('/vt/:type/:hash', async (req, res, next) => {
136-
if (req.method === 'PURGE') {
137-
debug('PURGE:', req.originalUrl)
136+
if (['PURGE', 'POST'].includes(req.method)) {
137+
debug(`PURGE: ${req.method}`, req.originalUrl)
138138
if (!['id', 'sha'].includes(req.params.type)) return next()
139139
const hash = req.params.hash.includes(':')
140140
? req.params.hash.split(':')[1]
@@ -167,8 +167,8 @@ app.get('/vt/:type/:hash', async (req, res) => {
167167
})
168168

169169
app.all('/vt/:owner/:repo/:asset{/:tag}', async (req, res, next) => {
170-
if (req.method === 'PURGE') {
171-
debug('PURGE:', req.originalUrl)
170+
if (['PURGE', 'POST'].includes(req.method)) {
171+
debug(`PURGE: ${req.method}`, req.originalUrl)
172172
const tag = req.params.tag || 'latest'
173173
const key = `${req.params.owner}/${req.params.repo}/${req.params.asset}/${tag}`
174174
return purgeKey(res, key)
@@ -189,8 +189,8 @@ app.get('/vt/:owner/:repo/:asset{/:tag}', async (req, res) => {
189189
})
190190

191191
app.all('/ghcr/tags/:owner/:package{/:latest}', async (req, res, next) => {
192-
if (req.method === 'PURGE') {
193-
debug('PURGE:', req.originalUrl)
192+
if (['PURGE', 'POST'].includes(req.method)) {
193+
debug(`PURGE: ${req.method}`, req.originalUrl)
194194
const key = `ghcr/tags/${req.params.owner}/${req.params.package}/tags/list`
195195
return purgeKey(res, key)
196196
}
@@ -233,8 +233,8 @@ app.get('/ghcr/tags/:owner/:package{/:latest}', async (req, res) => {
233233
})
234234

235235
app.all('/ghcr/size/:owner/:package{/:tag}', async (req, res, next) => {
236-
if (req.method === 'PURGE') {
237-
debug('PURGE:', req.originalUrl)
236+
if (['PURGE', 'POST'].includes(req.method)) {
237+
debug(`PURGE: ${req.method}`, req.originalUrl)
238238
const tag = req.params.tag ? req.params.tag : 'latest'
239239
const key = `ghcr/size/${req.params.owner}/${req.params.package}/${tag}`
240240
return purgeKey(res, key)
@@ -270,8 +270,8 @@ app.get('/static/:message{/:label}', async (req, res) => {
270270

271271
app.all('/:type/:url/:path', async (req, res, next) => {
272272
if (!['yaml', 'json'].includes(req.params.type)) return next()
273-
if (req.method === 'PURGE') {
274-
debug('PURGE:', req.originalUrl)
273+
if (['PURGE', 'POST'].includes(req.method)) {
274+
debug(`PURGE: ${req.method}`, req.originalUrl)
275275
return purgeKey(res, req.path)
276276
}
277277
next()
@@ -431,6 +431,11 @@ async function purgeKey(res, key) {
431431
const result = await cacheDelete(key)
432432
debug('result:', result)
433433
res.send(result.toString())
434+
if (result) {
435+
incrKey('purge_hit').catch(console.error)
436+
} else {
437+
incrKey('purge_miss').catch(console.error)
438+
}
434439
}
435440

436441
/**

0 commit comments

Comments
 (0)