Skip to content

Commit 4d0329e

Browse files
jonathanongvkurchatkin
authored andcommitted
fs: remove unnecessary usage of .hasOwnProperty()
PR-URL: #635 Reviewed-By: Vladimir Kurchatkin <[email protected]>
1 parent b72fa03 commit 4d0329e

File tree

2 files changed

+195
-19
lines changed

2 files changed

+195
-19
lines changed

lib/fs.js

+15-19
Original file line numberDiff line numberDiff line change
@@ -1608,21 +1608,20 @@ function ReadStream(path, options) {
16081608
return new ReadStream(path, options);
16091609

16101610
// a little bit bigger buffer and water marks by default
1611-
options = util._extend({
1612-
highWaterMark: 64 * 1024
1613-
}, options || {});
1611+
options = Object.create(options || {});
1612+
if (options.highWaterMark === undefined)
1613+
options.highWaterMark = 64 * 1024;
16141614

16151615
Readable.call(this, options);
16161616

16171617
this.path = path;
1618-
this.fd = options.hasOwnProperty('fd') ? options.fd : null;
1619-
this.flags = options.hasOwnProperty('flags') ? options.flags : 'r';
1620-
this.mode = options.hasOwnProperty('mode') ? options.mode : 0o666;
1621-
1622-
this.start = options.hasOwnProperty('start') ? options.start : undefined;
1623-
this.end = options.hasOwnProperty('end') ? options.end : undefined;
1624-
this.autoClose = options.hasOwnProperty('autoClose') ?
1625-
options.autoClose : true;
1618+
this.fd = options.fd === undefined ? null : options.fd;
1619+
this.flags = options.flags === undefined ? 'r' : options.flags;
1620+
this.mode = options.mode === undefined ? 0o666 : options.mode;
1621+
1622+
this.start = options.start === undefined ? undefined : options.start;
1623+
this.end = options.end === undefined ? undefined : options.end;
1624+
this.autoClose = options.autoClose === undefined ? true : options.autoClose;
16261625
this.pos = undefined;
16271626

16281627
if (this.start !== undefined) {
@@ -1779,13 +1778,11 @@ function WriteStream(path, options) {
17791778
Writable.call(this, options);
17801779

17811780
this.path = path;
1782-
this.fd = null;
1783-
1784-
this.fd = options.hasOwnProperty('fd') ? options.fd : null;
1785-
this.flags = options.hasOwnProperty('flags') ? options.flags : 'w';
1786-
this.mode = options.hasOwnProperty('mode') ? options.mode : 0o666;
1781+
this.fd = options.fd === undefined ? null : options.fd;
1782+
this.flags = options.flags === undefined ? 'w' : options.flags;
1783+
this.mode = options.mode === undefined ? 0o666 : options.mode;
17871784

1788-
this.start = options.hasOwnProperty('start') ? options.start : undefined;
1785+
this.start = options.start === undefined ? undefined : options.start;
17891786
this.pos = undefined;
17901787
this.bytesWritten = 0;
17911788

@@ -1865,8 +1862,7 @@ function SyncWriteStream(fd, options) {
18651862
this.fd = fd;
18661863
this.writable = true;
18671864
this.readable = false;
1868-
this.autoClose = options.hasOwnProperty('autoClose') ?
1869-
options.autoClose : true;
1865+
this.autoClose = options.autoClose === undefined ? true : options.autoClose;
18701866
}
18711867

18721868
util.inherits(SyncWriteStream, Stream);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
var common = require('../common');
2+
var assert = require('assert');
3+
4+
// TODO Improved this test. test_ca.pem is too small. A proper test would
5+
// great a large utf8 (with multibyte chars) file and stream it in,
6+
// performing sanity checks throughout.
7+
8+
var path = require('path');
9+
var fs = require('fs');
10+
var fn = path.join(common.fixturesDir, 'elipses.txt');
11+
var rangeFile = path.join(common.fixturesDir, 'x.txt');
12+
13+
var callbacks = { open: 0, end: 0, close: 0 };
14+
15+
var paused = false;
16+
17+
var file = fs.ReadStream(fn);
18+
19+
file.on('open', function(fd) {
20+
file.length = 0;
21+
callbacks.open++;
22+
assert.equal('number', typeof fd);
23+
assert.ok(file.readable);
24+
25+
// GH-535
26+
file.pause();
27+
file.resume();
28+
file.pause();
29+
file.resume();
30+
});
31+
32+
file.on('data', function(data) {
33+
assert.ok(data instanceof Buffer);
34+
assert.ok(!paused);
35+
file.length += data.length;
36+
37+
paused = true;
38+
file.pause();
39+
40+
setTimeout(function() {
41+
paused = false;
42+
file.resume();
43+
}, 10);
44+
});
45+
46+
47+
file.on('end', function(chunk) {
48+
callbacks.end++;
49+
});
50+
51+
52+
file.on('close', function() {
53+
callbacks.close++;
54+
55+
//assert.equal(fs.readFileSync(fn), fileContent);
56+
});
57+
58+
var file3 = fs.createReadStream(fn, Object.create({encoding: 'utf8'}));
59+
file3.length = 0;
60+
file3.on('data', function(data) {
61+
assert.equal('string', typeof(data));
62+
file3.length += data.length;
63+
64+
for (var i = 0; i < data.length; i++) {
65+
// http://www.fileformat.info/info/unicode/char/2026/index.htm
66+
assert.equal('\u2026', data[i]);
67+
}
68+
});
69+
70+
file3.on('close', function() {
71+
callbacks.close++;
72+
});
73+
74+
process.on('exit', function() {
75+
assert.equal(1, callbacks.open);
76+
assert.equal(1, callbacks.end);
77+
assert.equal(2, callbacks.close);
78+
assert.equal(30000, file.length);
79+
assert.equal(10000, file3.length);
80+
console.error('ok');
81+
});
82+
83+
var file4 = fs.createReadStream(rangeFile, Object.create({bufferSize: 1, start: 1, end: 2}));
84+
assert.equal(file4.start, 1);
85+
assert.equal(file4.end, 2);
86+
var contentRead = '';
87+
file4.on('data', function(data) {
88+
contentRead += data.toString('utf-8');
89+
});
90+
file4.on('end', function(data) {
91+
assert.equal(contentRead, 'yz');
92+
});
93+
94+
var file5 = fs.createReadStream(rangeFile, Object.create({bufferSize: 1, start: 1}));
95+
assert.equal(file5.start, 1);
96+
file5.data = '';
97+
file5.on('data', function(data) {
98+
file5.data += data.toString('utf-8');
99+
});
100+
file5.on('end', function() {
101+
assert.equal(file5.data, 'yz\n');
102+
});
103+
104+
// https://github.com/joyent/node/issues/2320
105+
var file6 = fs.createReadStream(rangeFile, Object.create({bufferSize: 1.23, start: 1}));
106+
assert.equal(file6.start, 1);
107+
file6.data = '';
108+
file6.on('data', function(data) {
109+
file6.data += data.toString('utf-8');
110+
});
111+
file6.on('end', function() {
112+
assert.equal(file6.data, 'yz\n');
113+
});
114+
115+
assert.throws(function() {
116+
fs.createReadStream(rangeFile, Object.create({start: 10, end: 2}));
117+
}, /start must be <= end/);
118+
119+
var stream = fs.createReadStream(rangeFile, Object.create({ start: 0, end: 0 }));
120+
assert.equal(stream.start, 0);
121+
assert.equal(stream.end, 0);
122+
stream.data = '';
123+
124+
stream.on('data', function(chunk) {
125+
stream.data += chunk;
126+
});
127+
128+
stream.on('end', function() {
129+
assert.equal('x', stream.data);
130+
});
131+
132+
// pause and then resume immediately.
133+
var pauseRes = fs.createReadStream(rangeFile);
134+
pauseRes.pause();
135+
pauseRes.resume();
136+
137+
var file7 = fs.createReadStream(rangeFile, Object.create({autoClose: false }));
138+
assert.equal(file7.autoClose, false);
139+
file7.on('data', function() {});
140+
file7.on('end', function() {
141+
process.nextTick(function() {
142+
assert(!file7.closed);
143+
assert(!file7.destroyed);
144+
file7Next();
145+
});
146+
});
147+
148+
function file7Next(){
149+
// This will tell us if the fd is usable again or not.
150+
file7 = fs.createReadStream(null, Object.create({fd: file7.fd, start: 0 }));
151+
file7.data = '';
152+
file7.on('data', function(data) {
153+
file7.data += data;
154+
});
155+
file7.on('end', function(err) {
156+
assert.equal(file7.data, 'xyz\n');
157+
});
158+
}
159+
160+
// Just to make sure autoClose won't close the stream because of error.
161+
var file8 = fs.createReadStream(null, Object.create({fd: 13337, autoClose: false }));
162+
file8.on('data', function() {});
163+
file8.on('error', common.mustCall(function() {}));
164+
165+
// Make sure stream is destroyed when file does not exist.
166+
var file9 = fs.createReadStream('/path/to/file/that/does/not/exist');
167+
file9.on('data', function() {});
168+
file9.on('error', common.mustCall(function() {}));
169+
170+
process.on('exit', function() {
171+
assert(file7.closed);
172+
assert(file7.destroyed);
173+
174+
assert(!file8.closed);
175+
assert(!file8.destroyed);
176+
assert(file8.fd);
177+
178+
assert(!file9.closed);
179+
assert(file9.destroyed);
180+
});

0 commit comments

Comments
 (0)