Skip to content

Commit 71f06ed

Browse files
heavykisaacs
authored andcommitted
fixup 0.8
1 parent c1b3777 commit 71f06ed

File tree

2 files changed

+121
-1
lines changed

2 files changed

+121
-1
lines changed

graceful-fs.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ module.exports = require('./fs.js')
44
// fix up some busted stuff, mostly on windows and old nodes
55
require('./polyfills.js')
66

7+
module.exports.open = open
8+
9+
if (process.version.substr(0, 4) === 'v0.8') {
10+
var legacy = require('./legacy-streams.js')
11+
ReadStream = legacy.ReadStream
12+
WriteStream = legacy.WriteStream
13+
}
14+
715
module.exports.FileReadStream = ReadStream; // Legacy name.
816
module.exports.FileWriteStream = WriteStream; // Legacy name.
917
module.exports.ReadStream = ReadStream
@@ -12,7 +20,6 @@ module.exports.close = close
1220
module.exports.closeSync = closeSync
1321
module.exports.createReadStream = createReadStream
1422
module.exports.createWriteStream = createWriteStream
15-
module.exports.open = open
1623
module.exports.readFile = readFile
1724
module.exports.readdir = readdir
1825

legacy-streams.js

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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

Comments
 (0)