-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cfCacheCleaner.js
75 lines (62 loc) · 1.92 KB
/
cfCacheCleaner.js
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
import Cloudflare from 'cloudflare';
import fs from 'fs';
import path from 'path';
import klaw from 'klaw';
import {walk} from "./utils.js";
const TOKEN = process.env['CF_CACHE_PURGE_TOKEN'];
const ZONE_ID = process.env['CF_CACHE_ZONE_ID'];
const PURGE_URL = process.env['CF_CACHE_PURGE_URL']; // Should be with "/" in the end
const CFClient = new Cloudflare({
apiToken: TOKEN
});
function purgeFile(cdnPath) {
return PURGE_URL + cdnPath;
}
async function purgeDir(dirPath, cdnPath, version) {
const filesToPurge = [];
dirPath = path.resolve(dirPath);
const files = await walk(dirPath);
for (let i = 0; i < files.length; ++i) {
const file = files[i];
const stats = fs.statSync(file);
if (!stats.isDirectory()) {
const filePath = file.replace(dirPath, '').substring(1).replace(/\\/g, '/');
const key = cdnPath + '/' + filePath;
filesToPurge.push(purgeFile(key));
}
}
if (version) {
filesToPurge.push(purgeFile(cdnPath + '/update.json'));
}
return filesToPurge;
}
async function clearCache(files) {
const CHUNK_SIZE = 30;
const chunks = [];
// Split the files array into smaller arrays of CHUNK_SIZE items
for (let i = 0; i < files.length; i += CHUNK_SIZE) {
chunks.push(files.slice(i, i + CHUNK_SIZE));
}
// Call purgeCache on each subarray using Promise.all()
const promises = chunks.map((chunk) => {
return CFClient.cache.purge({
zone_id: ZONE_ID,
files: chunk
});
});
return Promise.all(promises);
}
export async function purgeCache(filePath, cdnPath, version) {
if (fs.existsSync(filePath)) {
if (fs.lstatSync(filePath).isDirectory()) {
const filesToPurge = await purgeDir(filePath, cdnPath, version);
return clearCache(filesToPurge);
} else {
const toPurge = [ await purgeFile(filePath, cdnPath, version) ];
return clearCache(toPurge);
}
} else {
console.error('File not found');
return false;
}
}