-
Notifications
You must be signed in to change notification settings - Fork 0
/
tools.js
132 lines (119 loc) · 3.76 KB
/
tools.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
127
128
129
130
131
132
var fs = require('fs')
, path = require('path')
, http = require('http')
, util = require('util')
;
/**
* Monkey-patch adding "res.respond(...)"
* Usages:
* - res.respond(content as string or object) → 200 OK, with JSON encoded content
* - res.respond(status as number) → given status, with undefined content
* - res.respond(content, status) → ok, you got it :)
*/
http.ServerResponse.prototype.respond = function (content, status) {
if ('undefined' == typeof status) { // only one parameter found
if ('number' == typeof content || !isNaN(parseInt(content))) { // usage "respond(status)"
status = parseInt(content);
content = undefined;
} else { // usage "respond(content)"
status = 200;
}
}
if (status != 200) { // error
content = {
"code": status,
"status": http.STATUS_CODES[status],
"message": content && content.toString() || null
};
}
if ('object' != typeof content) { // wrap content if necessary
content = {"result":content};
}
// respond with JSON data
this.send(content, status);
};
function readDir(start, callback) {
// Use lstat to resolve symlink if we are passed a symlink
fs.lstat(start, function(err, stat) {
if(err) {
return callback(err);
}
var found = [],
total = 0,
processed = 0;
function isDir(abspath) {
fs.stat(abspath, function(err, stat) {
if(stat.isDirectory()) {
found.push(abspath);
// If we found a directory, recurse!
readDir(abspath, function(err, data) {
found = found.concat(data);
if(++processed == total) {
callback(null, found);
}
});
} else {
found.push(abspath);
if(++processed == total) {
callback(null, found);
}
}
});
}
// Read through all the files in this directory
if(stat.isDirectory()) {
fs.readdir(start, function (err, files) {
total = files.length;
for(var x=0, l=files.length; x<l; x++) {
isDir(path.join(start, files[x]));
}
});
} else {
return callback(new Error("path: " + start + " is not a directory"));
}
});
};
// en faire un vrai middleware ???
function serve(file, headers, req, res, next) {
// file, next, header, res
fs.stat(file, function(err, stat) {
// ignore ENOENT
if (err) {
return 'ENOENT' == err.code
? res.respond('File Not Found', 404)
: next(err);
} else if (stat.isDirectory()) {
return next();
}
headers.forEach(function(header) {
res.setHeader(header.name, header.value);
});
res.setHeader('Content-Length', stat.size);
// stream
var stream = fs.createReadStream(file);
req.emit('static', stream);
stream.pipe(res);
});
}
function copy(src, dst, cb) {
function copy(err) {
var is
, os
;
if (!err) {
return cb(new Error("File " + dst + " exists."));
}
fs.stat(src, function (err) {
if (err) {
return cb(err);
}
is = fs.createReadStream(src);
os = fs.createWriteStream(dst);
util.pump(is, os, cb);
});
}
fs.stat(dst, copy);
};
exports.readDir = readDir;
exports.serve = serve;
exports.copy = copy;