Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

tail.js 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // Generated by CoffeeScript 1.6.2
  2. var Tail, environment, events, fs,
  3. __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
  4. __hasProp = {}.hasOwnProperty,
  5. __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
  6. events = require("events");
  7. fs = require('fs');
  8. environment = process.env['NODE_ENV'] || 'development';
  9. Tail = (function(_super) {
  10. __extends(Tail, _super);
  11. Tail.prototype.readBlock = function() {
  12. var block, stream,
  13. _this = this;
  14. if (this.queue.length >= 1) {
  15. block = this.queue.shift();
  16. if (block.end > block.start) {
  17. stream = fs.createReadStream(this.filename, {
  18. start: block.start,
  19. end: block.end - 1,
  20. encoding: "utf-8"
  21. });
  22. stream.on('error', function(error) {
  23. console.log("Tail error:" + error);
  24. return _this.emit('error', error);
  25. });
  26. stream.on('end', function() {
  27. if (_this.queue.length >= 1) {
  28. return _this.internalDispatcher.emit("next");
  29. }
  30. });
  31. return stream.on('data', function(data) {
  32. var chunk, parts, _i, _len, _results;
  33. _this.buffer += data;
  34. parts = _this.buffer.split(_this.separator);
  35. _this.buffer = parts.pop();
  36. _results = [];
  37. for (_i = 0, _len = parts.length; _i < _len; _i++) {
  38. chunk = parts[_i];
  39. _results.push(_this.emit("line", chunk));
  40. }
  41. return _results;
  42. });
  43. }
  44. }
  45. };
  46. function Tail(filename, separator, fsWatchOptions, frombeginning) {
  47. var stats,
  48. _this = this;
  49. this.filename = filename;
  50. this.separator = separator != null ? separator : '\n';
  51. this.fsWatchOptions = fsWatchOptions != null ? fsWatchOptions : {};
  52. this.frombeginning = frombeginning != null ? frombeginning : false;
  53. this.readBlock = __bind(this.readBlock, this);
  54. this.buffer = '';
  55. this.internalDispatcher = new events.EventEmitter();
  56. this.queue = [];
  57. this.isWatching = false;
  58. stats = fs.statSync(this.filename);
  59. this.internalDispatcher.on('next', function() {
  60. return _this.readBlock();
  61. });
  62. this.pos = this.frombeginning ? 0 : stats.size;
  63. this.watch();
  64. }
  65. Tail.prototype.watch = function() {
  66. var _this = this;
  67. if (this.isWatching) {
  68. return;
  69. }
  70. this.isWatching = true;
  71. if (fs.watch) {
  72. return this.watcher = fs.watch(this.filename, this.fsWatchOptions, function(e) {
  73. return _this.watchEvent(e);
  74. });
  75. } else {
  76. return fs.watchFile(this.filename, this.fsWatchOptions, function(curr, prev) {
  77. return _this.watchFileEvent(curr, prev);
  78. });
  79. }
  80. };
  81. Tail.prototype.watchEvent = function(e) {
  82. var stats,
  83. _this = this;
  84. if (e === 'change') {
  85. stats = fs.statSync(this.filename);
  86. if (stats.size < this.pos) {
  87. this.pos = stats.size;
  88. }
  89. if (stats.size > this.pos) {
  90. this.queue.push({
  91. start: this.pos,
  92. end: stats.size
  93. });
  94. this.pos = stats.size;
  95. if (this.queue.length === 1) {
  96. return this.internalDispatcher.emit("next");
  97. }
  98. }
  99. } else if (e === 'rename') {
  100. this.unwatch();
  101. return setTimeout((function() {
  102. return _this.watch();
  103. }), 1000);
  104. }
  105. };
  106. Tail.prototype.watchFileEvent = function(curr, prev) {
  107. if (curr.size > prev.size) {
  108. this.queue.push({
  109. start: prev.size,
  110. end: curr.size
  111. });
  112. if (this.queue.length === 1) {
  113. return this.internalDispatcher.emit("next");
  114. }
  115. }
  116. };
  117. Tail.prototype.unwatch = function() {
  118. if (fs.watch && this.watcher) {
  119. this.watcher.close();
  120. this.pos = 0;
  121. } else {
  122. fs.unwatchFile(this.filename);
  123. }
  124. this.isWatching = false;
  125. return this.queue = [];
  126. };
  127. return Tail;
  128. })(events.EventEmitter);
  129. exports.Tail = Tail;