birdpack 1.0.13 → 1.0.15
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 +31 -44
- package/lib/method.js +3 -1
- package/lib/server.js +12 -2
- package/lib/websocket.js +11 -1
- package/package.json +1 -1
package/lib/core.js
CHANGED
|
@@ -340,54 +340,41 @@ module.exports = class{
|
|
|
340
340
|
const totalSize = fileStat.size;
|
|
341
341
|
let match = (this.get('range') || '').slice(6).split('-');
|
|
342
342
|
|
|
343
|
-
|
|
344
|
-
|
|
343
|
+
const start = parseInt(match[0], 10) || 0;
|
|
344
|
+
const end = parseInt(match[1], 10) || totalSize - 1;
|
|
345
345
|
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
const end = parseInt(match[1], 10) || totalSize - 1;
|
|
346
|
+
const chunkSize = end - start + 1;
|
|
347
|
+
const fileDescriptor = fs.openSync(options.file, "r");
|
|
349
348
|
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
fs.closeSync(fileDescriptor);
|
|
356
|
-
return;
|
|
357
|
-
}
|
|
358
|
-
|
|
359
|
-
const bytesToRead = Math.min(bufferSize, end - position + 1);
|
|
360
|
-
const bytesRead = fs.readSync(fileDescriptor, buffer, 0, bytesToRead, position);
|
|
349
|
+
const sendChunk = (position) => {
|
|
350
|
+
if(position > end){
|
|
351
|
+
fs.closeSync(fileDescriptor);
|
|
352
|
+
return;
|
|
353
|
+
}
|
|
361
354
|
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
if(err){
|
|
365
|
-
fs.closeSync(fileDescriptor);
|
|
366
|
-
}else{
|
|
367
|
-
sendChunk(position + bytesRead);
|
|
368
|
-
}
|
|
369
|
-
});
|
|
370
|
-
}else{
|
|
371
|
-
fs.closeSync(fileDescriptor);
|
|
372
|
-
}
|
|
373
|
-
};
|
|
374
|
-
|
|
375
|
-
this.code(206)
|
|
376
|
-
.length(chunkSize)
|
|
377
|
-
.set('content-range', `bytes ${start}-${end}/${totalSize}`)
|
|
378
|
-
.writeHead();
|
|
379
|
-
sendChunk(start);
|
|
380
|
-
}else{
|
|
381
|
-
const start = 0;
|
|
382
|
-
const end = totalSize - 1;
|
|
383
|
-
const chunkSize = end - start + 1;
|
|
355
|
+
const bytesToRead = Math.min(bufferSize, end - position + 1);
|
|
356
|
+
const bytesRead = fs.readSync(fileDescriptor, buffer, 0, bytesToRead, position);
|
|
384
357
|
|
|
385
|
-
this.
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
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);
|
|
391
378
|
}else if(fileRange == 'file'){
|
|
392
379
|
if(options.parameter){
|
|
393
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
|
|
package/lib/websocket.js
CHANGED
|
@@ -28,6 +28,8 @@ module.exports = class{
|
|
|
28
28
|
})
|
|
29
29
|
.on('close', () => {
|
|
30
30
|
this.userOut();
|
|
31
|
+
this.callOut();
|
|
32
|
+
this.callOut = ()=>{};
|
|
31
33
|
this.callback(this, 'close');
|
|
32
34
|
})
|
|
33
35
|
.on('error', (e) => {
|
|
@@ -226,7 +228,10 @@ module.exports = class{
|
|
|
226
228
|
}
|
|
227
229
|
});
|
|
228
230
|
}
|
|
229
|
-
|
|
231
|
+
callOut(){
|
|
232
|
+
|
|
233
|
+
}
|
|
234
|
+
async start(uid){
|
|
230
235
|
let seckey = this.get('sec-websocket-key');
|
|
231
236
|
let protocol = this.get('sec-websocket-protocol');
|
|
232
237
|
let version = parseInt(this.get('sec-websocket-version') || 0);
|
|
@@ -240,6 +245,11 @@ module.exports = class{
|
|
|
240
245
|
if(checkUser !== false){
|
|
241
246
|
checkUser.userOut();
|
|
242
247
|
checkUser.stop();
|
|
248
|
+
await new Promise((resolve)=>{
|
|
249
|
+
checkUser.callOut = ()=>{
|
|
250
|
+
resolve();
|
|
251
|
+
};
|
|
252
|
+
});
|
|
243
253
|
}
|
|
244
254
|
|
|
245
255
|
this.uid = uid;
|