1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | var crypto = require('crypto')
|
14 | , Path = require('path')
|
15 | , fs = require('fs');
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 | exports.flatten = function(arr, ret){
|
26 | var ret = ret || []
|
27 | , len = arr.length;
|
28 | for (var i = 0; i < len; ++i) {
|
29 | if (Array.isArray(arr[i])) {
|
30 | exports.flatten(arr[i], ret);
|
31 | } else {
|
32 | ret.push(arr[i]);
|
33 | }
|
34 | }
|
35 | return ret;
|
36 | };
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 | exports.md5 = function(str, encoding){
|
52 | return crypto
|
53 | .createHash('md5')
|
54 | .update(str)
|
55 | .digest(encoding || 'hex');
|
56 | };
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 | exports.merge = function(a, b){
|
74 | if (a && b) {
|
75 | for (var key in b) {
|
76 | a[key] = b[key];
|
77 | }
|
78 | }
|
79 | return a;
|
80 | };
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 | exports.escape = function(html){
|
91 | return String(html)
|
92 | .replace(/&(?!\w+;)/g, '&')
|
93 | .replace(/</g, '<')
|
94 | .replace(/>/g, '>')
|
95 | .replace(/"/g, '"');
|
96 | };
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 | exports.uid = function(len) {
|
111 | var buf = []
|
112 | , chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
|
113 | , charlen = chars.length;
|
114 |
|
115 | for (var i = 0; i < len; ++i) {
|
116 | buf.push(chars[getRandomInt(0, charlen - 1)]);
|
117 | }
|
118 |
|
119 | return buf.join('');
|
120 | };
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 | exports.parseCookie = function(str){
|
131 | var obj = {}
|
132 | , pairs = str.split(/[;,] */);
|
133 | for (var i = 0, len = pairs.length; i < len; ++i) {
|
134 | var pair = pairs[i]
|
135 | , eqlIndex = pair.indexOf('=')
|
136 | , key = pair.substr(0, eqlIndex).trim().toLowerCase()
|
137 | , val = pair.substr(++eqlIndex, pair.length).trim();
|
138 |
|
139 |
|
140 | if ('"' == val[0]) val = val.slice(1, -1);
|
141 |
|
142 |
|
143 | if (undefined == obj[key]) {
|
144 | val = val.replace(/\+/g, ' ');
|
145 | try {
|
146 | obj[key] = decodeURIComponent(val);
|
147 | } catch (err) {
|
148 | if (err instanceof URIError) {
|
149 | obj[key] = val;
|
150 | } else {
|
151 | throw err;
|
152 | }
|
153 | }
|
154 | }
|
155 | }
|
156 | return obj;
|
157 | };
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 | exports.serializeCookie = function(name, val, obj){
|
173 | var pairs = [name + '=' + encodeURIComponent(val)]
|
174 | , obj = obj || {};
|
175 |
|
176 | if (obj.domain) pairs.push('domain=' + obj.domain);
|
177 | if (obj.path) pairs.push('path=' + obj.path);
|
178 | if (obj.expires) pairs.push('expires=' + obj.expires.toUTCString());
|
179 | if (obj.httpOnly) pairs.push('httpOnly');
|
180 | if (obj.secure) pairs.push('secure');
|
181 |
|
182 | return pairs.join('; ');
|
183 | };
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 | exports.pause = function(obj){
|
204 | var onData
|
205 | , onEnd
|
206 | , events = [];
|
207 |
|
208 |
|
209 | obj.on('data', onData = function(data, encoding){
|
210 | events.push(['data', data, encoding]);
|
211 | });
|
212 |
|
213 |
|
214 | obj.on('end', onEnd = function(data, encoding){
|
215 | events.push(['end', data, encoding]);
|
216 | });
|
217 |
|
218 | return {
|
219 | end: function(){
|
220 | obj.removeListener('data', onData);
|
221 | obj.removeListener('end', onEnd);
|
222 | },
|
223 | resume: function(){
|
224 | this.end();
|
225 | for (var i = 0, len = events.length; i < len; ++i) {
|
226 | obj.emit.apply(obj, events[i]);
|
227 | }
|
228 | }
|
229 | };
|
230 | };
|
231 |
|
232 |
|
233 |
|
234 |
|
235 |
|
236 |
|
237 |
|
238 |
|
239 |
|
240 |
|
241 | exports.modified = function(req, res, headers) {
|
242 | var headers = headers || res._headers || {}
|
243 | , modifiedSince = req.headers['if-modified-since']
|
244 | , lastModified = headers['last-modified']
|
245 | , noneMatch = req.headers['if-none-match']
|
246 | , etag = headers['etag'];
|
247 |
|
248 | if (noneMatch) noneMatch = noneMatch.split(/ *, */);
|
249 |
|
250 |
|
251 | if (noneMatch && etag && ~noneMatch.indexOf(etag)) {
|
252 | return false;
|
253 | }
|
254 |
|
255 |
|
256 | if (modifiedSince && lastModified) {
|
257 | modifiedSince = new Date(modifiedSince);
|
258 | lastModified = new Date(lastModified);
|
259 |
|
260 | if (!isNaN(modifiedSince.getTime())) {
|
261 | if (lastModified <= modifiedSince) return false;
|
262 | }
|
263 | }
|
264 |
|
265 | return true;
|
266 | };
|
267 |
|
268 |
|
269 |
|
270 |
|
271 |
|
272 |
|
273 |
|
274 |
|
275 | exports.removeContentHeaders = function(res){
|
276 | Object.keys(res._headers).forEach(function(field){
|
277 | if (0 == field.indexOf('content')) {
|
278 | res.removeHeader(field);
|
279 | }
|
280 | });
|
281 | };
|
282 |
|
283 |
|
284 |
|
285 |
|
286 |
|
287 |
|
288 |
|
289 |
|
290 |
|
291 | exports.conditionalGET = function(req) {
|
292 | return req.headers['if-modified-since']
|
293 | || req.headers['if-none-match'];
|
294 | };
|
295 |
|
296 |
|
297 |
|
298 |
|
299 |
|
300 |
|
301 |
|
302 |
|
303 | exports.forbidden = function(res) {
|
304 | var body = 'Forbidden';
|
305 | res.setHeader('Content-Type', 'text/plain');
|
306 | res.setHeader('Content-Length', body.length);
|
307 | res.statusCode = 403;
|
308 | res.end(body);
|
309 | };
|
310 |
|
311 |
|
312 |
|
313 |
|
314 |
|
315 |
|
316 |
|
317 |
|
318 |
|
319 | exports.unauthorized = function(res, realm) {
|
320 | res.statusCode = 401;
|
321 | res.setHeader('WWW-Authenticate', 'Basic realm="' + realm + '"');
|
322 | res.end('Unauthorized');
|
323 | };
|
324 |
|
325 |
|
326 |
|
327 |
|
328 |
|
329 |
|
330 |
|
331 |
|
332 | exports.badRequest = function(res) {
|
333 | res.statusCode = 400;
|
334 | res.end('Bad Request');
|
335 | };
|
336 |
|
337 |
|
338 |
|
339 |
|
340 |
|
341 |
|
342 |
|
343 |
|
344 |
|
345 | exports.notModified = function(res) {
|
346 | exports.removeContentHeaders(res);
|
347 | res.statusCode = 304;
|
348 | res.end();
|
349 | };
|
350 |
|
351 |
|
352 |
|
353 |
|
354 |
|
355 |
|
356 |
|
357 |
|
358 |
|
359 |
|
360 | exports.etag = function(stat) {
|
361 | return '"' + stat.size + '-' + Number(stat.mtime) + '"';
|
362 | };
|
363 |
|
364 |
|
365 |
|
366 |
|
367 |
|
368 |
|
369 |
|
370 |
|
371 |
|
372 |
|
373 | exports.parseRange = function(size, str){
|
374 | var valid = true;
|
375 | var arr = str.substr(6).split(',').map(function(range){
|
376 | var range = range.split('-')
|
377 | , start = parseInt(range[0], 10)
|
378 | , end = parseInt(range[1], 10);
|
379 |
|
380 |
|
381 | if (isNaN(start)) {
|
382 | start = size - end;
|
383 | end = size - 1;
|
384 |
|
385 | } else if (isNaN(end)) {
|
386 | end = size - 1;
|
387 | }
|
388 |
|
389 |
|
390 | if (isNaN(start) || isNaN(end) || start > end) valid = false;
|
391 |
|
392 | return { start: start, end: end };
|
393 | });
|
394 | return valid ? arr : undefined;
|
395 | };
|
396 |
|
397 |
|
398 |
|
399 |
|
400 |
|
401 |
|
402 |
|
403 |
|
404 |
|
405 | exports.parseCacheControl = function(str){
|
406 | var directives = str.split(',')
|
407 | , obj = {};
|
408 |
|
409 | for(var i = 0, len = directives.length; i < len; i++) {
|
410 | var parts = directives[i].split('=')
|
411 | , key = parts.shift().trim()
|
412 | , val = parseInt(parts.shift(), 10);
|
413 |
|
414 | obj[key] = isNaN(val) ? true : val;
|
415 | }
|
416 |
|
417 | return obj;
|
418 | };
|
419 |
|
420 |
|
421 |
|
422 |
|
423 |
|
424 |
|
425 |
|
426 |
|
427 |
|
428 |
|
429 |
|
430 |
|
431 | var toArray = exports.toArray = function(obj){
|
432 | var len = obj.length
|
433 | , arr = new Array(len);
|
434 | for (var i = 0; i < len; ++i) {
|
435 | arr[i] = obj[i];
|
436 | }
|
437 | return arr;
|
438 | };
|
439 |
|
440 |
|
441 |
|
442 |
|
443 |
|
444 |
|
445 |
|
446 |
|
447 |
|
448 |
|
449 | function getRandomInt(min, max) {
|
450 | return Math.floor(Math.random() * (max - min + 1)) + min;
|
451 | }
|