birdpack 1.0.12 → 1.0.14
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 +33 -44
- package/lib/method.js +3 -1
- package/lib/server.js +12 -2
- package/package.json +1 -1
package/lib/core.js
CHANGED
|
@@ -328,6 +328,8 @@ module.exports = class{
|
|
|
328
328
|
fileRange = 'stream';
|
|
329
329
|
}
|
|
330
330
|
options.type = fileType[fileExt];
|
|
331
|
+
}else{
|
|
332
|
+
options.type = 'application';
|
|
331
333
|
}
|
|
332
334
|
|
|
333
335
|
if(options.stream === true){
|
|
@@ -338,54 +340,41 @@ module.exports = class{
|
|
|
338
340
|
const totalSize = fileStat.size;
|
|
339
341
|
let match = (this.get('range') || '').slice(6).split('-');
|
|
340
342
|
|
|
341
|
-
|
|
342
|
-
|
|
343
|
+
const start = parseInt(match[0], 10) || 0;
|
|
344
|
+
const end = parseInt(match[1], 10) || totalSize - 1;
|
|
343
345
|
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
const end = parseInt(match[1], 10) || totalSize - 1;
|
|
346
|
+
const chunkSize = end - start + 1;
|
|
347
|
+
const fileDescriptor = fs.openSync(options.file, "r");
|
|
347
348
|
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
fs.closeSync(fileDescriptor);
|
|
354
|
-
return;
|
|
355
|
-
}
|
|
349
|
+
const sendChunk = (position) => {
|
|
350
|
+
if(position > end){
|
|
351
|
+
fs.closeSync(fileDescriptor);
|
|
352
|
+
return;
|
|
353
|
+
}
|
|
356
354
|
|
|
357
|
-
|
|
358
|
-
|
|
355
|
+
const bytesToRead = Math.min(bufferSize, end - position + 1);
|
|
356
|
+
const bytesRead = fs.readSync(fileDescriptor, buffer, 0, bytesToRead, position);
|
|
359
357
|
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
const end = totalSize - 1;
|
|
381
|
-
const chunkSize = end - start + 1;
|
|
382
|
-
|
|
383
|
-
this.code(200)
|
|
384
|
-
.length(chunkSize)
|
|
385
|
-
.set('content-range', `bytes ${start}-${end}/${totalSize}`)
|
|
386
|
-
.writeHead();
|
|
387
|
-
this.end();
|
|
388
|
-
}
|
|
358
|
+
if(bytesRead > 0 && this.res.writable){
|
|
359
|
+
this[position + bytesRead > end ? 'end' : 'write'](buffer.slice(0, bytesRead), (err) => {
|
|
360
|
+
if(err){
|
|
361
|
+
fs.closeSync(fileDescriptor);
|
|
362
|
+
}else{
|
|
363
|
+
sendChunk(position + bytesRead);
|
|
364
|
+
}
|
|
365
|
+
});
|
|
366
|
+
}else{
|
|
367
|
+
fs.closeSync(fileDescriptor);
|
|
368
|
+
}
|
|
369
|
+
};
|
|
370
|
+
|
|
371
|
+
this.code(206)
|
|
372
|
+
.length(chunkSize)
|
|
373
|
+
.type(options.type)
|
|
374
|
+
.set('accept-ranges', 'bytes')
|
|
375
|
+
.set('content-range', `bytes ${start}-${end}/${totalSize}`)
|
|
376
|
+
.writeHead();
|
|
377
|
+
sendChunk(start);
|
|
389
378
|
}else if(fileRange == 'file'){
|
|
390
379
|
if(options.parameter){
|
|
391
380
|
let data = fs.readFileSync(options.file, { encoding: 'utf8', flag: 'r' });
|
package/lib/method.js
CHANGED
|
@@ -28,7 +28,9 @@ const serviceMethod = {
|
|
|
28
28
|
}
|
|
29
29
|
},
|
|
30
30
|
directory:(use)=>{
|
|
31
|
-
return (dir,
|
|
31
|
+
return (dir, option)=>{
|
|
32
|
+
let {path, callback, error} = option || {};
|
|
33
|
+
|
|
32
34
|
if(typeof path !== 'string'){
|
|
33
35
|
path = '/';
|
|
34
36
|
}else if(path[path.length-1] != '/'){
|
package/lib/server.js
CHANGED
|
@@ -334,7 +334,12 @@ module.exports = class{
|
|
|
334
334
|
return config;
|
|
335
335
|
}
|
|
336
336
|
set({domain, method, url, next, callback}){
|
|
337
|
-
if(
|
|
337
|
+
if(Array.isArray(domain)){
|
|
338
|
+
for(let x of domain){
|
|
339
|
+
this.set({domain:x, method, url, next, callback});
|
|
340
|
+
}
|
|
341
|
+
return true;
|
|
342
|
+
}else if(typeof domain != 'string'){
|
|
338
343
|
domain = '*';
|
|
339
344
|
}
|
|
340
345
|
|
|
@@ -383,7 +388,12 @@ module.exports = class{
|
|
|
383
388
|
}
|
|
384
389
|
}
|
|
385
390
|
setUpgrade({domain, next, url, callback}){
|
|
386
|
-
if(
|
|
391
|
+
if(Array.isArray(domain)){
|
|
392
|
+
for(let x of domain){
|
|
393
|
+
this.set({domain:x, next, url, callback});
|
|
394
|
+
}
|
|
395
|
+
return true;
|
|
396
|
+
}else if(typeof domain != 'string'){
|
|
387
397
|
domain = '*';
|
|
388
398
|
}
|
|
389
399
|
|