forked from nodejs/node
/
permission-fs-is-granted.js
50 lines (41 loc) · 1.26 KB
/
permission-fs-is-granted.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
'use strict';
const common = require('../common.js');
const fs = require('fs/promises');
const path = require('path');
const configs = {
n: [1e5],
concurrent: [1, 10],
};
const rootPath = path.resolve(__dirname, '../../..');
const options = {
flags: [
'--experimental-permission',
`--allow-fs-read=${rootPath}`,
],
};
const bench = common.createBenchmark(main, configs, options);
const recursivelyDenyFiles = async (dir) => {
const files = await fs.readdir(dir, { withFileTypes: true });
for (const file of files) {
if (file.isDirectory()) {
await recursivelyDenyFiles(path.join(dir, file.name));
} else if (file.isFile()) {
process.permission.deny('fs.read', [path.join(dir, file.name)]);
}
}
};
async function main(conf) {
const benchmarkDir = path.join(__dirname, '../..');
// Get all the benchmark files and deny access to it
await recursivelyDenyFiles(benchmarkDir);
bench.start();
for (let i = 0; i < conf.n; i++) {
// Valid file in a sequence of denied files
process.permission.has('fs.read', benchmarkDir + '/valid-file');
// Denied file
process.permission.has('fs.read', __filename);
// Valid file a granted directory
process.permission.has('fs.read', '/tmp/example');
}
bench.end(conf.n);
}