birdpack 1.0.7 → 1.0.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/core.js +58 -32
- package/lib/server.js +30 -3
- package/package.json +1 -1
package/lib/core.js
CHANGED
|
@@ -387,46 +387,72 @@ module.exports = class{
|
|
|
387
387
|
this.end();
|
|
388
388
|
}
|
|
389
389
|
}else if(fileRange == 'file'){
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
390
|
+
if(options.parameter){
|
|
391
|
+
let data = fs.readFileSync(options.file, { encoding: 'utf8', flag: 'r' });
|
|
392
|
+
|
|
393
|
+
data = data.replace(/\{\{[a-zA-Z0-9\_]*\}\}/g, (a)=>{
|
|
394
|
+
let key = a.slice(2,-2);
|
|
395
|
+
if(key != ''){
|
|
396
|
+
if(options.parameter.hasOwnProperty(key)){
|
|
397
|
+
return options.parameter[key]
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
return '';
|
|
401
|
+
});
|
|
394
402
|
|
|
395
|
-
|
|
396
|
-
const fileDescriptor = fs.openSync(options.file, "r");
|
|
403
|
+
data = Buffer.from(data, "utf8");
|
|
397
404
|
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
fs.closeSync(fileDescriptor);
|
|
401
|
-
return;
|
|
405
|
+
if(options.download === true){
|
|
406
|
+
this.set('content-disposition', `attachment; filename="${options.filename}"`);
|
|
402
407
|
}
|
|
403
408
|
|
|
404
|
-
|
|
405
|
-
|
|
409
|
+
this.code(200)
|
|
410
|
+
.length(data.length)
|
|
411
|
+
.type(options.type);
|
|
412
|
+
|
|
413
|
+
this.send(data);
|
|
414
|
+
}else{
|
|
415
|
+
const totalSize = fileStat.size;
|
|
406
416
|
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
if (err) {
|
|
410
|
-
fs.closeSync(fileDescriptor);
|
|
411
|
-
} else {
|
|
412
|
-
sendChunk(position + bytesRead);
|
|
413
|
-
}
|
|
414
|
-
});
|
|
415
|
-
}else{
|
|
416
|
-
fs.closeSync(fileDescriptor);
|
|
417
|
-
}
|
|
418
|
-
};
|
|
417
|
+
const start = 0;
|
|
418
|
+
const end = totalSize - 1;
|
|
419
419
|
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
420
|
+
const chunkSize = end - start + 1;
|
|
421
|
+
const fileDescriptor = fs.openSync(options.file, "r");
|
|
422
|
+
|
|
423
|
+
const sendChunk = (position) => {
|
|
424
|
+
if(position > end){
|
|
425
|
+
fs.closeSync(fileDescriptor);
|
|
426
|
+
return;
|
|
427
|
+
}
|
|
423
428
|
|
|
424
|
-
|
|
425
|
-
|
|
429
|
+
const bytesToRead = Math.min(bufferSize, end - position + 1);
|
|
430
|
+
const bytesRead = fs.readSync(fileDescriptor, buffer, 0, bytesToRead, position);
|
|
431
|
+
|
|
432
|
+
if(bytesRead > 0 && this.res.writable){
|
|
433
|
+
this[position + bytesRead > end ? 'end' : 'write'](buffer.slice(0, bytesRead), (err) => {
|
|
434
|
+
if (err) {
|
|
435
|
+
fs.closeSync(fileDescriptor);
|
|
436
|
+
} else {
|
|
437
|
+
sendChunk(position + bytesRead);
|
|
438
|
+
}
|
|
439
|
+
});
|
|
440
|
+
}else{
|
|
441
|
+
fs.closeSync(fileDescriptor);
|
|
442
|
+
}
|
|
443
|
+
};
|
|
444
|
+
|
|
445
|
+
this.code(200)
|
|
446
|
+
.length(chunkSize)
|
|
447
|
+
.type(options.type);
|
|
448
|
+
|
|
449
|
+
if(options.download === true){
|
|
450
|
+
this.set('content-disposition', `attachment; filename="${options.filename}"`);
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
this.writeHead();
|
|
454
|
+
sendChunk(start);
|
|
426
455
|
}
|
|
427
|
-
|
|
428
|
-
this.writeHead();
|
|
429
|
-
sendChunk(start);
|
|
430
456
|
}else{
|
|
431
457
|
options.error(400, 'This file not supported.');
|
|
432
458
|
}
|
package/lib/server.js
CHANGED
|
@@ -37,6 +37,35 @@ module.exports = class{
|
|
|
37
37
|
}else{
|
|
38
38
|
this.log = ()=>{};
|
|
39
39
|
}
|
|
40
|
+
|
|
41
|
+
let traffic = this.opt.traffic;
|
|
42
|
+
if(typeof traffic !== 'string'){
|
|
43
|
+
traffic = '$time $status $method $path $ip $head(user-agent)';
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
let useTraffic = traffic.replace(/\$[a-zA-Z0-9\(\)\-]*/g, (a)=>{
|
|
47
|
+
if(a == '$time'){
|
|
48
|
+
return '${time}';
|
|
49
|
+
}else if(a == '$status'){
|
|
50
|
+
return '${c.status}';
|
|
51
|
+
}else if(a == '$method'){
|
|
52
|
+
return '${c.method}';
|
|
53
|
+
}else if(a == '$path'){
|
|
54
|
+
return '${c.path}';
|
|
55
|
+
}else if(a == '$ip'){
|
|
56
|
+
return '${c.ip}';
|
|
57
|
+
}else if(a == '$body'){
|
|
58
|
+
return '${typeof c.body == "object" ? JSON.stringify(c.body) : (typeof c.body == "string" ? c.body : "NULL")}';
|
|
59
|
+
}else if(a.slice(0,5) == '$head'){
|
|
60
|
+
let myhead = a.slice(6,-1);
|
|
61
|
+
return '${c.get("' + myhead + '") || "NULL"}';
|
|
62
|
+
}
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
let callTraffic = new Function("time", "c", `return \`${useTraffic}\``);
|
|
66
|
+
this.traffic = (c)=>{
|
|
67
|
+
this.log('client', callTraffic(this.getFormattedDateTime(), c));
|
|
68
|
+
}
|
|
40
69
|
}
|
|
41
70
|
argv(){
|
|
42
71
|
let cmd = {
|
|
@@ -246,9 +275,6 @@ module.exports = class{
|
|
|
246
275
|
let out = `[${status}] - ${text}`;
|
|
247
276
|
console.log(out);
|
|
248
277
|
}
|
|
249
|
-
traffic(c){
|
|
250
|
-
this.log('client',`${this.getFormattedDateTime()} ${c.status} ${c.method} ${c.path} ${c.ip} ${c.get('user-agent')}`);
|
|
251
|
-
}
|
|
252
278
|
getFormattedDateTime(){
|
|
253
279
|
let now = new Date();
|
|
254
280
|
|
|
@@ -383,3 +409,4 @@ module.exports = class{
|
|
|
383
409
|
return ()=>{};
|
|
384
410
|
}
|
|
385
411
|
}
|
|
412
|
+
|