-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsearch_tool.js
126 lines (107 loc) · 3.01 KB
/
search_tool.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
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
const { createServer } = require('http');
const methods = Object.create(null);
createServer((request, response) => {
let handler = methods[request.method] || notAllowed;
handler(request)
.catch((error) => {
if (error.status != null) return error;
return { body: String(error), status: 500 };
})
.then(({ body, status = 200, type = 'text/plain' }) => {
response.writeHead(status, { 'Content-Type': type });
if (body && body.pipe) body.pipe(response);
else response.end(body);
});
}).listen(8000);
async function notAllowed(request) {
return {
status: 405,
body: `Method ${request.method} not allowed.`,
};
}
var { parse } = require('url');
var { resolve, sep } = require('path');
var baseDirectory = process.cwd();
function urlPath(url) {
let { pathname } = parse(url);
let path = resolve(decodeURIComponent(pathname).slice(1));
if (path != baseDirectory && !path.startsWith(baseDirectory + sep)) {
throw { status: 403, body: 'Forbidden' };
}
return path;
}
const { createReadStream } = require('fs');
const { stat, readdir } = require('fs').promises;
const mime = require('mime');
methods.GET = async function (request) {
let path = urlPath(request.url);
let stats;
try {
stats = await stat(path);
} catch (error) {
if (error.code != 'ENOENT') throw error;
else return { status: 404, body: 'File not found' };
}
if (stats.isDirectory()) {
return { body: (await readdir(path)).join('\n') };
} else {
return { body: createReadStream(path), type: mime.getType(path) };
}
};
const { rmdir, unlink } = require('fs').promises;
methods.DELETE = async function (request) {
let path = urlPath(request.url);
let stats;
try {
stats = await stat(path);
} catch (error) {
if (error.code != 'ENOENT') throw error;
else return { status: 204 };
}
if (stats.isDirectory()) await rmdir(path);
else await unlink(path);
return { status: 204 };
};
const { createWriteStream } = require('fs');
function pipeStream(from, to) {
return new Promise((resolve, reject) => {
from.on('error', reject);
to.on('error', reject);
to.on('finish', resolve);
from.pipe(to);
});
}
methods.PUT = async function (request) {
let path = urlPath(request.url);
await pipeStream(request, createWriteStream(path));
return { status: 204 };
};
const { mkdir } = require('fs').promises;
methods.MKCOL = async function (request) {
let path = urlPath(request.url);
let stats;
try {
stats = await stat(path);
} catch (error) {
if (error.code != 'ENOENT') throw error;
await mkdir(path);
return { status: 204 };
}
if (stats.isDirectory()) return { status: 204 };
else return { status: 400, body: 'Not a directory' };
};
const { statSync, readdirSync, readFileSync } = require('fs');
let searchTerm = new RegExp(process.argv[2]);
for (let arg of process.argv.slice(3)) {
search(arg);
}
function search(file) {
let stats = statSync(file);
if (stats.isDirectory()) {
for (let f of readdirSync(file)) {
search(file + '/' + f);
}
} else if (searchTerm.test(readFileSync(file, 'utf8'))) {
console.log(file);
}
}