説明なし

legacy-streams.js 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. var Stream = require('stream').Stream
  2. module.exports = legacy
  3. function legacy (fs) {
  4. return {
  5. ReadStream: ReadStream,
  6. WriteStream: WriteStream
  7. }
  8. function ReadStream (path, options) {
  9. if (!(this instanceof ReadStream)) return new ReadStream(path, options);
  10. Stream.call(this);
  11. var self = this;
  12. this.path = path;
  13. this.fd = null;
  14. this.readable = true;
  15. this.paused = false;
  16. this.flags = 'r';
  17. this.mode = 438; /*=0666*/
  18. this.bufferSize = 64 * 1024;
  19. options = options || {};
  20. // Mixin options into this
  21. var keys = Object.keys(options);
  22. for (var index = 0, length = keys.length; index < length; index++) {
  23. var key = keys[index];
  24. this[key] = options[key];
  25. }
  26. if (this.encoding) this.setEncoding(this.encoding);
  27. if (this.start !== undefined) {
  28. if ('number' !== typeof this.start) {
  29. throw TypeError('start must be a Number');
  30. }
  31. if (this.end === undefined) {
  32. this.end = Infinity;
  33. } else if ('number' !== typeof this.end) {
  34. throw TypeError('end must be a Number');
  35. }
  36. if (this.start > this.end) {
  37. throw new Error('start must be <= end');
  38. }
  39. this.pos = this.start;
  40. }
  41. if (this.fd !== null) {
  42. process.nextTick(function() {
  43. self._read();
  44. });
  45. return;
  46. }
  47. fs.open(this.path, this.flags, this.mode, function (err, fd) {
  48. if (err) {
  49. self.emit('error', err);
  50. self.readable = false;
  51. return;
  52. }
  53. self.fd = fd;
  54. self.emit('open', fd);
  55. self._read();
  56. })
  57. }
  58. function WriteStream (path, options) {
  59. if (!(this instanceof WriteStream)) return new WriteStream(path, options);
  60. Stream.call(this);
  61. this.path = path;
  62. this.fd = null;
  63. this.writable = true;
  64. this.flags = 'w';
  65. this.encoding = 'binary';
  66. this.mode = 438; /*=0666*/
  67. this.bytesWritten = 0;
  68. options = options || {};
  69. // Mixin options into this
  70. var keys = Object.keys(options);
  71. for (var index = 0, length = keys.length; index < length; index++) {
  72. var key = keys[index];
  73. this[key] = options[key];
  74. }
  75. if (this.start !== undefined) {
  76. if ('number' !== typeof this.start) {
  77. throw TypeError('start must be a Number');
  78. }
  79. if (this.start < 0) {
  80. throw new Error('start must be >= zero');
  81. }
  82. this.pos = this.start;
  83. }
  84. this.busy = false;
  85. this._queue = [];
  86. if (this.fd === null) {
  87. this._open = fs.open;
  88. this._queue.push([this._open, this.path, this.flags, this.mode, undefined]);
  89. this.flush();
  90. }
  91. }
  92. }