diff --git a/example/redis_client.js b/example/redis_client.js index eb2e58e..68219c4 100644 --- a/example/redis_client.js +++ b/example/redis_client.js @@ -14,7 +14,7 @@ class Client extends EventEmitter { this._resp = new Resp() .on('error', (error) => this.emit('error', error)) .on('data', (data) => { - let cb = this._queue.shift() + const cb = this._queue.shift() if (!cb) return this.emit('error', new Error('Unexpected reply: ' + data)) if (data instanceof Error) cb[1](data) else cb[0](data) @@ -42,31 +42,31 @@ class Client extends EventEmitter { // tman example/redis_client.js tman('simple redis client', function () { - let client = new Client(6379) + const client = new Client(6379) tman.it('info', function * () { - let res = yield client.cmd('info') + const res = yield client.cmd('info') console.log('INFO:', res) assert.ok(res.indexOf('redis_version') > 0) }) tman.it('set', function * () { - let res = yield client.cmd('set', 'resp', 'hello') + const res = yield client.cmd('set', 'resp', 'hello') console.log('SET:', res) assert.strictEqual(res, 'OK') }) tman.it('get', function * () { - let res = yield client.cmd('get', 'resp') + const res = yield client.cmd('get', 'resp') console.log('GET:', res) assert.strictEqual(res, 'hello') }) tman.it('ping', function * () { let count = 10000 - let time = Date.now() + const time = Date.now() while (count--) { - let res = yield client.cmd('ping') + const res = yield client.cmd('ping') assert.strictEqual(res, 'PONG') } console.log('PING:', (10000 * 1000 / (Date.now() - time)).toFixed(2), 'ops/sec') diff --git a/index.js b/index.js index fbee570..22cc976 100644 --- a/index.js +++ b/index.js @@ -42,8 +42,8 @@ class Resp extends EventEmitter { static encodeBufBulk (buf) { if (!Buffer.isBuffer(buf)) throw new TypeError(String(buf) + ' must be Buffer object') - let prefix = '$' + buf.length + CRLF - let buffer = Buffer.allocUnsafe(prefix.length + buf.length + 2) + const prefix = '$' + buf.length + CRLF + const buffer = Buffer.allocUnsafe(prefix.length + buf.length + 2) buffer.write(prefix) buf.copy(buffer, prefix.length) buffer.write(CRLF, prefix.length + buf.length) @@ -52,9 +52,9 @@ class Resp extends EventEmitter { static encodeArray (arr) { if (!Array.isArray(arr)) throw new Error(String(arr) + ' must be Array object') - let prefix = '*' + arr.length + CRLF + const prefix = '*' + arr.length + CRLF let length = prefix.length - let bufs = [Buffer.from(prefix)] + const bufs = [Buffer.from(prefix)] for (let buf, i = 0, len = arr.length; i < len; i++) { buf = arr[i] @@ -71,7 +71,7 @@ class Resp extends EventEmitter { if (!Array.isArray(arr) || arr.length === 0) { throw new Error(String(arr) + ' must be array of value') } - let bulks = Array(arr.length) + const bulks = Array(arr.length) for (let i = 0, len = arr.length; i < len; i++) { bulks[i] = Buffer.isBuffer(arr[i]) ? Resp.encodeBufBulk(arr[i]) : Resp.encodeBulk(arr[i]) } @@ -80,7 +80,7 @@ class Resp extends EventEmitter { // Decode a RESP buffer to RESP value static decode (buf, bufBulk) { - let res = parseBuffer(buf, 0, bufBulk) + const res = parseBuffer(buf, 0, bufBulk) if (!res || res.index < buf.length) throw new Error('Parse "' + buf + '" failed') if (res instanceof Error) throw res return res.content @@ -105,8 +105,8 @@ class Resp extends EventEmitter { if (!this._buf) this._buf = buf else { - let ret = this._buf.length - this._pos - let _buf = Buffer.allocUnsafe(buf.length + ret) + const ret = this._buf.length - this._pos + const _buf = Buffer.allocUnsafe(buf.length + ret) this._buf.copy(_buf, 0, this._pos) buf.copy(_buf, ret) @@ -115,7 +115,7 @@ class Resp extends EventEmitter { } while (this._pos < this._buf.length) { - let result = parseBuffer(this._buf, this._pos, this._bufBulk) + const result = parseBuffer(this._buf, this._pos, this._bufBulk) if (result == null) { this.emit('drain') return true @@ -153,8 +153,8 @@ class ReadRes { } function readBuffer (buf, i) { - let start = i - let len = buf.length + const start = i + const len = buf.length while (i < len && !isCRLF(buf, i)) i++ return i >= len ? null : new ReadRes(buf.utf8Slice(start, i), i + 2) } @@ -190,7 +190,7 @@ function parseBuffer (buf, index, bufBulk) { if (result == null) return result num = parseInteger(result.content) if (num == null || num < -1) return new Error('Parse "$" failed, invalid length') - let endIndex = result.index + num + const endIndex = result.index + num if (num === -1) { // Null Bulk @@ -231,7 +231,7 @@ function parseBuffer (buf, index, bufBulk) { } function parseInteger (str) { - let num = +str + const num = +str return (str && Number.isInteger(num)) ? num : null } diff --git a/test/index.js b/test/index.js index 8d64f61..f1f1605 100644 --- a/test/index.js +++ b/test/index.js @@ -95,7 +95,7 @@ function test (Resp) { assert.strictEqual(Resp.decode(Resp.encodeError(new Error('err'))) instanceof Error, true) assert.strictEqual(Resp.decode(Resp.encodeError(new Error('err')), true) instanceof Error, true) - let err = Resp.decode(Resp.encodeError(new TypeError('err'))) + const err = Resp.decode(Resp.encodeError(new TypeError('err'))) assert.strictEqual(err.name, 'TypeError') assert.strictEqual(err.code, 'TypeError') assert.strictEqual(err.message, 'err') @@ -126,19 +126,19 @@ function test (Resp) { assert.throws(function () { Resp.decode(1) }) assert.throws(function () { Resp.decode(Buffer.from('123')) }) assert.throws(function () { - let buf = Resp.encodeBulk('123') + const buf = Resp.encodeBulk('123') buf[buf.length - 1] = 0 Resp.decode(buf) }) assert.throws(function () { - let buf = Buffer.concat([Resp.encodeBulk('123'), Buffer.from('1')]) + const buf = Buffer.concat([Resp.encodeBulk('123'), Buffer.from('1')]) Resp.decode(buf) }) }) tman.it('new Resp()', function (done) { - let result = [] - let reply = new Resp() + const result = [] + const reply = new Resp() reply .on('data', function (data) { @@ -160,7 +160,7 @@ function test (Resp) { }) tman.it('new Resp({bufBulk: true})', function (done) { - let reply = new Resp({bufBulk: true}) + const reply = new Resp({bufBulk: true}) reply .on('data', function (data) { @@ -174,8 +174,8 @@ function test (Resp) { }) tman.it('new Resp(): Pipelining data', function (done) { - let result = [] - let reply = new Resp() + const result = [] + const reply = new Resp() reply .on('data', function (data) { @@ -193,7 +193,7 @@ function test (Resp) { }) tman.it('new Resp(): with non resp buffer', function (done) { - let reply = new Resp() + const reply = new Resp() reply .on('error', function (error) { @@ -206,8 +206,8 @@ function test (Resp) { }) tman.it('new Resp(): with error data', function (done) { - let result = [] - let reply = new Resp() + const result = [] + const reply = new Resp() reply .on('data', function (data) { @@ -231,9 +231,9 @@ function test (Resp) { tman.it('new Resp(): chaos', function (done) { this.timeout(100000) - let result = [] - let reply = new Resp() - let buf = Resp.encodeArray([ + const result = [] + const reply = new Resp() + const buf = Resp.encodeArray([ Resp.encodeNull(), Resp.encodeString('OKOKOKOK'), Resp.encodeInteger(123456789), @@ -263,7 +263,7 @@ function test (Resp) { }) let start = 0 - let length = bufs.length + const length = bufs.length consumer() function consumer () { @@ -279,10 +279,10 @@ function test (Resp) { tman.it('new Resp(): bench', function (done) { this.timeout(100000) - let result = [] - let reply = new Resp() + const result = [] + const reply = new Resp() - let buf = Resp.encodeArray([ + const buf = Resp.encodeArray([ Resp.encodeString('OK'), Resp.encodeString('QUEUED'), Resp.encodeString('QUEUED'),