From 10ba94f4ad8bf5e99b014787498fee390781cdc3 Mon Sep 17 00:00:00 2001 From: Benjamin Gruenbaum Date: Sun, 24 Jan 2016 11:02:01 +0200 Subject: [PATCH 1/5] Refactor self=this Refactor a self=this pattern to use arrow functions. --- lib/https.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/https.js b/lib/https.js index 7008a79131c663..36f158f34ae371 100644 --- a/lib/https.js +++ b/lib/https.js @@ -77,12 +77,12 @@ function createConnection(port, host, options) { } } - const self = this; - const socket = tls.connect(options, function() { + + const socket = tls.connect(options, () => { if (!options._agentKey) return; - self._cacheSession(options._agentKey, socket.getSession()); + this._cacheSession(options._agentKey, socket.getSession()); }); return socket; } From 60e4ad385ba015e3126d433d2d0c496832a1827e Mon Sep 17 00:00:00 2001 From: Benjamin Gruenbaum Date: Sun, 24 Jan 2016 11:03:39 +0200 Subject: [PATCH 2/5] Update self=this in http.js --- lib/http.js | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/lib/http.js b/lib/http.js index 64788dd5c07f9f..8f27111c07a9f6 100644 --- a/lib/http.js +++ b/lib/http.js @@ -59,10 +59,10 @@ function Client(port, host) { } util.inherits(Client, EventEmitter); Client.prototype.request = function(method, path, headers) { - var self = this; + var options = {}; - options.host = self.host; - options.port = self.port; + options.host = this.host; + options.port = this.port; if (method[0] === '/') { headers = path; path = method; @@ -71,22 +71,22 @@ Client.prototype.request = function(method, path, headers) { options.method = method; options.path = path; options.headers = headers; - options.agent = self.agent; + options.agent = this.agent; var c = new ClientRequest(options); - c.on('error', function(e) { - self.emit('error', e); + c.on('error', (e) => { + this.emit('error', e); }); // The old Client interface emitted 'end' on socket end. // This doesn't map to how we want things to operate in the future // but it will get removed when we remove this legacy interface. - c.on('socket', function(s) { - s.on('end', function() { - if (self._decoder) { - var ret = self._decoder.end(); + c.on('socket', (s) => { + s.on('end', () => { + if (this._decoder) { + var ret = this._decoder.end(); if (ret) - self.emit('data', ret); + this.emit('data', ret); } - self.emit('end'); + this.emit('end'); }); }); return c; From 1ebb5b2ff6f091d42881e6e3ee987127c537011e Mon Sep 17 00:00:00 2001 From: Benjamin Gruenbaum Date: Sun, 24 Jan 2016 11:07:05 +0200 Subject: [PATCH 3/5] Refactor self=this to arrows --- lib/fs.js | 74 +++++++++++++++++++++++++++---------------------------- 1 file changed, 36 insertions(+), 38 deletions(-) diff --git a/lib/fs.js b/lib/fs.js index af8001bb181698..32b78e611a4ed8 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -1300,18 +1300,17 @@ fs.appendFileSync = function(path, data, options) { function FSWatcher() { EventEmitter.call(this); - var self = this; this._handle = new FSEvent(); this._handle.owner = this; - this._handle.onchange = function(status, event, filename) { + this._handle.onchange = (status, event, filename) => { if (status < 0) { - self._handle.close(); + this._handle.close(); const error = errnoException(status, `watch ${filename}`); error.filename = filename; - self.emit('error', error); + this.emit('error', error); } else { - self.emit('change', event, filename); + this.emit('change', event, filename); } }; } @@ -1367,24 +1366,23 @@ fs.watch = function(filename) { function StatWatcher() { EventEmitter.call(this); - var self = this; this._handle = new binding.StatWatcher(); // uv_fs_poll is a little more powerful than ev_stat but we curb it for // the sake of backwards compatibility var oldStatus = -1; - this._handle.onchange = function(current, previous, newStatus) { + this._handle.onchange = (current, previous, newStatus) => { if (oldStatus === -1 && newStatus === -1 && current.nlink === previous.nlink) return; oldStatus = newStatus; - self.emit('change', current, previous); + this.emit('change', current, previous); }; - this._handle.onstop = function() { - self.emit('stop'); + this._handle.onstop = () => { + this.emit('stop'); }; } util.inherits(StatWatcher, EventEmitter); @@ -1766,20 +1764,20 @@ function ReadStream(path, options) { fs.FileReadStream = fs.ReadStream; // support the legacy name ReadStream.prototype.open = function() { - var self = this; - fs.open(this.path, this.flags, this.mode, function(er, fd) { + + fs.open(this.path, this.flags, this.mode, (er, fd) => { if (er) { - if (self.autoClose) { - self.destroy(); + if (this.autoClose) { + this.destroy(); } - self.emit('error', er); + this.emit('error', er); return; } - self.fd = fd; - self.emit('open', fd); + this.fd = fd; + this.emit('open', fd); // start the flow of data. - self.read(); + this.read(); }); }; @@ -1814,7 +1812,6 @@ ReadStream.prototype._read = function(n) { return this.push(null); // the actual read. - var self = this; fs.read(this.fd, pool, pool.used, toRead, this.pos, onread); // move the pool positions, and internal position for reading. @@ -1822,6 +1819,7 @@ ReadStream.prototype._read = function(n) { this.pos += toRead; pool.used += toRead; + var self = this; function onread(er, bytesRead) { if (er) { if (self.autoClose) { @@ -1848,7 +1846,17 @@ ReadStream.prototype.destroy = function() { ReadStream.prototype.close = function(cb) { - var self = this; + + const close = (fd) => { + fs.close(fd || this.fd, function(er) { + if (er) + this.emit('error', er); + else + this.emit('close'); + }); + this.fd = null; + }; + if (cb) this.once('close', cb); if (this.closed || typeof this.fd !== 'number') { @@ -1861,15 +1869,7 @@ ReadStream.prototype.close = function(cb) { this.closed = true; close(); - function close(fd) { - fs.close(fd || self.fd, function(er) { - if (er) - self.emit('error', er); - else - self.emit('close'); - }); - self.fd = null; - } + }; @@ -1957,15 +1957,14 @@ WriteStream.prototype._write = function(data, encoding, cb) { this._write(data, encoding, cb); }); - var self = this; - fs.write(this.fd, data, 0, data.length, this.pos, function(er, bytes) { + fs.write(this.fd, data, 0, data.length, this.pos, (er, bytes) => { if (er) { - if (self.autoClose) { - self.destroy(); + if (this.autoClose) { + this.destroy(); } return cb(er); } - self.bytesWritten += bytes; + this.bytesWritten += bytes; cb(); }); @@ -1992,7 +1991,6 @@ WriteStream.prototype._writev = function(data, cb) { this._writev(data, cb); }); - const self = this; const len = data.length; const chunks = new Array(len); var size = 0; @@ -2004,12 +2002,12 @@ WriteStream.prototype._writev = function(data, cb) { size += chunk.length; } - writev(this.fd, chunks, this.pos, function(er, bytes) { + writev(this.fd, chunks, this.pos, (er, bytes) => { if (er) { - self.destroy(); + this.destroy(); return cb(er); } - self.bytesWritten += bytes; + this.bytesWritten += bytes; cb(); }); From dd96a00b7a4d5a0dba6c4cafefa107e5f78aa615 Mon Sep 17 00:00:00 2001 From: Benjamin Gruenbaum Date: Sun, 24 Jan 2016 16:30:21 +0200 Subject: [PATCH 4/5] fix leading whitespace linter issue --- lib/fs.js | 3 +-- lib/http.js | 2 +- lib/https.js | 1 - 3 files changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/fs.js b/lib/fs.js index 32b78e611a4ed8..ae3ed4ed1da7ff 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -1764,7 +1764,7 @@ function ReadStream(path, options) { fs.FileReadStream = fs.ReadStream; // support the legacy name ReadStream.prototype.open = function() { - + fs.open(this.path, this.flags, this.mode, (er, fd) => { if (er) { if (this.autoClose) { @@ -1868,7 +1868,6 @@ ReadStream.prototype.close = function(cb) { } this.closed = true; close(); - }; diff --git a/lib/http.js b/lib/http.js index 8f27111c07a9f6..203883676ce4ae 100644 --- a/lib/http.js +++ b/lib/http.js @@ -59,7 +59,7 @@ function Client(port, host) { } util.inherits(Client, EventEmitter); Client.prototype.request = function(method, path, headers) { - + var options = {}; options.host = this.host; options.port = this.port; diff --git a/lib/https.js b/lib/https.js index 36f158f34ae371..9f32b2f6e96582 100644 --- a/lib/https.js +++ b/lib/https.js @@ -77,7 +77,6 @@ function createConnection(port, host, options) { } } - const socket = tls.connect(options, () => { if (!options._agentKey) return; From 889f46764aafffbaf2be8569bbc8412f79426d03 Mon Sep 17 00:00:00 2001 From: Benjamin Gruenbaum Date: Mon, 25 Jan 2016 22:35:20 +0200 Subject: [PATCH 5/5] fix emberrasing mistake --- lib/fs.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/fs.js b/lib/fs.js index ae3ed4ed1da7ff..8fd0d35dc47489 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -1848,7 +1848,7 @@ ReadStream.prototype.destroy = function() { ReadStream.prototype.close = function(cb) { const close = (fd) => { - fs.close(fd || this.fd, function(er) { + fs.close(fd || this.fd, (er) => { if (er) this.emit('error', er); else