Add plugin that can remove all data for a URL. (#3173)

This commit is contained in:
Peter Hedenskog 2020-11-01 17:18:44 +01:00 committed by GitHub
parent 99190352de
commit ab57bcdab1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 54 additions and 2 deletions

View File

@ -17,7 +17,8 @@ const defaultPlugins = new Set([
'harstorer',
'budget',
'thirdparty',
'tracestorer'
'tracestorer',
'remove'
]);
const pluginsDir = path.join(__dirname, '..', 'plugins');

View File

@ -2,9 +2,14 @@
const fs = require('fs-extra');
const path = require('path');
const pathToFolder = require('./pathToFolder');
const log = require('intel').getLogger('sitespeedio.storageManager');
const { promisify } = require('util');
const mkdir = promisify(fs.mkdir);
const readdir = promisify(fs.readdir);
const lstat = promisify(fs.lstat);
const unlink = promisify(fs.unlink);
const rmdir = promisify(fs.rmdir);
const pathToFolder = require('./pathToFolder');
function write(dirPath, filename, data) {
return fs.writeFile(path.join(dirPath, filename), data);
@ -51,6 +56,33 @@ module.exports = function storageManager(baseDir, storagePathPrefix, options) {
copyToResultDir(filename) {
return this.createDirectory().then(dir => fs.copy(filename, dir));
},
removeDataForUrl(url) {
const dirName = path.join(baseDir, pathToFolder(url, useHash));
const removeDir = async dir => {
try {
const files = await readdir(dir);
await Promise.all(
files.map(async file => {
try {
const p = path.join(dir, file);
const stat = await lstat(p);
if (stat.isDirectory()) {
await removeDir(p);
} else {
await unlink(p);
}
} catch (err) {
log.error('Could not remove file:' + file, err);
}
})
);
await rmdir(dir);
} catch (err) {
log.error('Could not remove dir:' + dir, err);
}
};
return removeDir(dirName);
},
createDirForUrl(url, subDir) {
return this.createDirectory(pathToFolder(url, useHash), subDir);
},

View File

@ -0,0 +1,19 @@
'use strict';
const log = require('intel').getLogger('sitespeedio.plugin.remove');
module.exports = {
open(context, options) {
this.storageManager = context.storageManager;
this.options = options;
},
async processMessage(message) {
switch (message.type) {
case 'remove.url': {
log.info('Remove data for URL %s', message.url);
await this.storageManager.removeDataForUrl(message.url);
break;
}
}
}
};