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