forked from JuanIrache/eloquent-javascript-exercises
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path20_2_directory_creation.js
110 lines (92 loc) · 2.7 KB
/
20_2_directory_creation.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
/////////// Provided code
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.`
};
}
const { parse } = require('url');
const { resolve, sep } = require('path');
const 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 };
};
/////////// Exercise
const { mkdir } = require('fs').promises;
const { existsSync } = require('fs');
methods.MKCOL = async function(request) {
let path = urlPath(request.url);
if (existsSync(path)) return { status: 409, body: `${path} already exists.` };
try {
await mkdir(path);
return { status: 204 };
} catch (error) {
throw error;
}
};