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