total5 0.0.9-3 → 0.0.9-5

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/changelog.txt CHANGED
@@ -11,6 +11,12 @@
11
11
  - replaced `$.hostname` property with a method in the Schema/Auth Options
12
12
  - __critital__ fixed cloning buffers in the `FlowStream`
13
13
  - improved user-agent parser
14
+ - added `FILESTORAGE().clone(id, newid, [callback])` method
15
+ - fixed `FILESTORAGE().copy(id, newid, [callback])` method
16
+ - extended `PROXY(endpoint, stream_function(ctrl))` by adding support for a custom handler
17
+ - extended `REQUEST()` by adding `opt.writer {function(res)}` option
18
+ - added missing `TotalAPI()` method
19
+ - fixed `CONF.totalapi` key when initializing the framework
14
20
 
15
21
  ========================
16
22
  0.0.8
package/filestorage.js CHANGED
@@ -373,6 +373,106 @@ FP._read = function(id, callback, nostream) {
373
373
  return self;
374
374
  };
375
375
 
376
+ FP.clone = function(id, newid, callback) {
377
+ var self = this;
378
+
379
+ if (typeof(newid) === 'function') {
380
+ callback = newid;
381
+ newid = UID();
382
+ }
383
+
384
+ if (callback)
385
+ return self._clone(id, newid, callback);
386
+ else
387
+ return new Promise((resolve, reject) => self._clone(id, newid, (err, res) => err ? reject(err) : resolve(res)));
388
+ };
389
+
390
+ FP._clone = function(id, newid, callback) {
391
+
392
+ var self = this;
393
+
394
+ if (self.pause) {
395
+ setTimeout(self._clone, 500, id, newid, callback);
396
+ return self;
397
+ }
398
+
399
+ var filename = F.Path.join(self.makedirectory(id), id + '.file');
400
+
401
+ F.stats.performance.open++;
402
+ F.Fs.open(filename, 'r', function(err, fd) {
403
+
404
+ if (err) {
405
+ callback(err);
406
+ return;
407
+ }
408
+
409
+ var buffer = Buffer.alloc(HEADERSIZE);
410
+ F.Fs.read(fd, buffer, 0, HEADERSIZE, 0, function(err) {
411
+
412
+ if (err) {
413
+ F.Fs.close(fd, NOOP);
414
+ callback(err);
415
+ return;
416
+ }
417
+
418
+ F.stats.performance.open++;
419
+
420
+ var str = buffer.toString('utf8').replace(REG_CLEAN, '');
421
+ if (!str) {
422
+ // Invalid file
423
+ F.Fs.close(fd, function() {
424
+ if (buffer.length === HEADERSIZE)
425
+ F.Fs.unlink(filename, NOOP);
426
+ });
427
+ callback('File not found');
428
+ return;
429
+ }
430
+
431
+ var meta = str.parseJSON(true);
432
+ if (!meta) {
433
+ F.Fs.close(fd, NOOP);
434
+ callback('Invalid file');
435
+ return;
436
+ }
437
+
438
+ F.Fs.close(fd, NOOP);
439
+ meta.id = newid;
440
+
441
+ if (meta.expire && meta.expire < NOW) {
442
+ callback('File is expired');
443
+ return;
444
+ }
445
+
446
+ var directory = self.makedirectory(newid);
447
+ var filenamenew = F.Path.join(directory, newid + '.file');
448
+
449
+ if (self.cache[directory]) {
450
+ F.Fs.copyFile(filename, filenamenew, function(err) {
451
+ if (!err)
452
+ F.Fs.appendFile(self.logger, JSON.stringify(meta) + '\n', NOOP);
453
+ callback && callback(err, meta);
454
+ });
455
+ } else {
456
+ F.Fs.mkdir(directory, MKDIR, function(err) {
457
+
458
+ if (err) {
459
+ callback(err);
460
+ return;
461
+ }
462
+
463
+ self.cache[directory] = 1;
464
+ F.Fs.copyFile(filename, filenamenew, function(err) {
465
+ if (!err)
466
+ F.Fs.appendFile(self.logger, JSON.stringify(meta) + '\n', NOOP);
467
+ callback && callback(err, meta);
468
+ });
469
+ });
470
+ }
471
+
472
+ });
473
+ });
474
+ };
475
+
376
476
  FP.copy = function(id, path, callback) {
377
477
  var self = this;
378
478
  if (callback)
@@ -390,10 +490,10 @@ FP._copy = function(id, path, callback) {
390
490
  return self;
391
491
  }
392
492
 
393
- var filename = Path.join(self.makedirectory(id), id + '.file');
493
+ var filename = F.Path.join(self.makedirectory(id), id + '.file');
394
494
 
395
495
  F.stats.performance.open++;
396
- Fs.open(filename, 'r', function(err, fd) {
496
+ F.Fs.open(filename, 'r', function(err, fd) {
397
497
 
398
498
  if (err) {
399
499
  callback(err);
@@ -401,20 +501,20 @@ FP._copy = function(id, path, callback) {
401
501
  }
402
502
 
403
503
  var buffer = Buffer.alloc(HEADERSIZE);
404
- Fs.read(fd, buffer, 0, HEADERSIZE, 0, function(err) {
504
+ F.Fs.read(fd, buffer, 0, HEADERSIZE, 0, function(err) {
405
505
 
406
506
  if (err) {
407
- Fs.close(fd, NOOP);
507
+ F.Fs.close(fd, NOOP);
408
508
  callback(err);
409
509
  return;
410
510
  }
411
511
 
412
- var str = buffer.toString('utf8').replace(REGCLEAN, '');
512
+ var str = buffer.toString('utf8').replace(REG_CLEAN, '');
413
513
  if (!str) {
414
514
  // Invalid file
415
- Fs.close(fd, function() {
515
+ F.Fs.close(fd, function() {
416
516
  if (buffer.length === HEADERSIZE)
417
- Fs.unlink(filename, NOOP);
517
+ F.Fs.unlink(filename, NOOP);
418
518
  });
419
519
  callback('File not found');
420
520
  return;
@@ -422,7 +522,7 @@ FP._copy = function(id, path, callback) {
422
522
 
423
523
  var meta = str.parseJSON(true);
424
524
  if (!meta) {
425
- Fs.close(fd, NOOP);
525
+ F.Fs.close(fd, NOOP);
426
526
  callback('Invalid file');
427
527
  return;
428
528
  }
@@ -430,21 +530,21 @@ FP._copy = function(id, path, callback) {
430
530
  meta.id = id;
431
531
 
432
532
  if (meta.expire && meta.expire < NOW) {
433
- Fs.close(fd, NOOP);
533
+ F.Fs.close(fd, NOOP);
434
534
  callback('File is expired');
435
535
  return;
436
536
  }
437
537
 
438
538
  F.stats.performance.open++;
439
539
 
440
- var reader = Fs.createReadStream(filename, { fd: fd, start: HEADERSIZE });
441
- var writer = Fs.createWriteStream(path.includes('.') ? path : Path.join(path, meta.name));
540
+ var reader = F.Fs.createReadStream(filename, { fd: fd, start: HEADERSIZE });
541
+ var writer = F.Fs.createWriteStream(path.includes('.') ? path : F.Path.join(path, meta.name));
442
542
 
443
543
  reader.pipe(writer);
444
544
 
445
545
  CLEANUP(reader, function() {
446
546
  callback(err, meta);
447
- Fs.close(fd, NOOP);
547
+ F.Fs.close(fd, NOOP);
448
548
  });
449
549
 
450
550
  });
package/global.js CHANGED
@@ -299,3 +299,9 @@ global.clearTimeout2 = function(id) {
299
299
 
300
300
  return !!tmp;
301
301
  };
302
+
303
+ global.TotalAPI = function(name, model, callback) {
304
+ let obj = API('TAPI', name, model);
305
+ callback && obj.callback(callback);
306
+ return obj;
307
+ };
package/index.js CHANGED
@@ -545,7 +545,10 @@ F.loadconfig = function(value) {
545
545
 
546
546
  switch (key) {
547
547
  case 'totalapi':
548
- key = '$tapi';
548
+ if (typeof(val) === 'string' && val.length > 5)
549
+ key = 'secret_totalapi';
550
+ else
551
+ key = '$tapi';
549
552
  break;
550
553
  case '$tms':
551
554
  break;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "total5",
3
- "version": "0.0.9-3",
3
+ "version": "0.0.9-5",
4
4
  "description": "Total.js framework v5",
5
5
  "main": "index.js",
6
6
  "directories": {
package/routing.js CHANGED
@@ -761,19 +761,23 @@ function Proxy(url, target) {
761
761
  t.url = url.toLowerCase();
762
762
  t.copypath = 'none'; // replace|extend|none
763
763
 
764
- if ((/^(https|http):\/\//).test(target))
764
+ if (typeof(target) === 'function')
765
+ t.stream = target;
766
+ else if ((/^(https|http):\/\//).test(target))
765
767
  t.target = F.Url.parse(target);
766
768
  else
767
769
  t.target = { socketPath: target };
768
770
 
769
- if (t.target.href) {
770
- let index = t.target.href.indexOf('?');
771
- if (index !== -1)
772
- t.query = t.target.href.substring(index + 1);
773
- t.path = t.target.pathname[t.target.pathname.length - 1] === '/' ? t.target.pathname.substring(0, t.target.pathname.length - 1) : t.target.pathname;
774
- }
771
+ if (t.target) {
772
+ if (t.target.href) {
773
+ let index = t.target.href.indexOf('?');
774
+ if (index !== -1)
775
+ t.query = t.target.href.substring(index + 1);
776
+ t.path = t.target.pathname[t.target.pathname.length - 1] === '/' ? t.target.pathname.substring(0, t.target.pathname.length - 1) : t.target.pathname;
777
+ }
775
778
 
776
- t.uri = t.target;
779
+ t.uri = t.target;
780
+ }
777
781
  }
778
782
 
779
783
  Proxy.prototype.copy = function(type) {
@@ -883,6 +887,13 @@ function proxyheadersws(header, headers) {
883
887
 
884
888
  function proxycreate(proxy, ctrl) {
885
889
 
890
+ if (proxy.stream) {
891
+ F.stats.performance.external++;
892
+ F.stats.request.external++;
893
+ proxy.stream(ctrl);
894
+ return;
895
+ }
896
+
886
897
  var secured = proxy.uri.protocol === 'https:';
887
898
  var uri = {};
888
899
 
package/utils.js CHANGED
@@ -893,7 +893,9 @@ function request_call(uri, options) {
893
893
 
894
894
  req.on('response', request_assign_res);
895
895
 
896
- if (options.upload) {
896
+ if (options.writer) {
897
+ options.writer(req);
898
+ } else if (options.upload) {
897
899
  options.first = true;
898
900
  options.files.wait(function(file, next) {
899
901
  request_writefile(req, options, file, next);
@@ -921,9 +923,9 @@ function request_call(uri, options) {
921
923
  req.end(NEWLINE + '--' + options.boundary + '--');
922
924
  });
923
925
  } else {
924
- if (options.opt.compress) {
926
+ if (options.opt.compress)
925
927
  F.Zlib[options.opt.compress](options.body, (err, buffer) => req.end(buffer));
926
- } else
928
+ else
927
929
  req.end(options.body);
928
930
  }
929
931
  }