diff --git a/lib/command.ts b/lib/command.ts index 8f52c8f7..9819abd1 100644 --- a/lib/command.ts +++ b/lib/command.ts @@ -1,4 +1,3 @@ -import * as fbuffer from 'flexbuffer' import * as commands from 'redis-commands' import * as calculateSlot from 'cluster-key-slot' import asCallback from 'standard-as-callback' @@ -223,22 +222,22 @@ export default class Command { let result let commandStr = '*' + (this.args.length + 1) + '\r\n$' + this.name.length + '\r\n' + this.name + '\r\n' if (bufferMode) { - const resultBuffer = new fbuffer.FlexBuffer(0) - resultBuffer.write(commandStr) + const buffers = new MixedBuffers(); + buffers.push(commandStr); for (const arg of this.args) { if (arg instanceof Buffer) { if (arg.length === 0) { - resultBuffer.write('$0\r\n\r\n') + buffers.push('$0\r\n\r\n') } else { - resultBuffer.write('$' + arg.length + '\r\n') - resultBuffer.write(arg) - resultBuffer.write('\r\n') + buffers.push('$' + arg.length + '\r\n') + buffers.push(arg) + buffers.push('\r\n') } } else { - resultBuffer.write('$' + Buffer.byteLength(arg as string | Buffer) + '\r\n' + arg + '\r\n') + buffers.push('$' + Buffer.byteLength(arg as string | Buffer) + '\r\n' + arg + '\r\n') } } - result = resultBuffer.getBuffer() + result = buffers.toBuffer(); } else { result = commandStr for (const arg of this.args) { @@ -332,3 +331,26 @@ Command.setReplyTransformer('hgetall', function (result) { } return result }) + +class MixedBuffers { + length = 0 + items = [] + + public push(x: string | Buffer) { + this.length += Buffer.byteLength(x); + this.items.push(x) + } + + public toBuffer(): Buffer { + const result = Buffer.allocUnsafe(this.length); + let offset = 0; + for (const item of this.items) { + const length = Buffer.byteLength(item); + Buffer.isBuffer(item) + ? (item as Buffer).copy(result, offset) + : result.write(item, offset, length) + offset += length; + } + return result; + } +} diff --git a/lib/pipeline.ts b/lib/pipeline.ts index 1bffe2bb..82c9294b 100644 --- a/lib/pipeline.ts +++ b/lib/pipeline.ts @@ -1,5 +1,4 @@ import Command from './command' -import {FlexBuffer} from 'flexbuffer' import {deprecate} from 'util' import asCallback from 'standard-as-callback' import {exists, hasFlag} from 'redis-commands' @@ -278,7 +277,7 @@ Pipeline.prototype.exec = function (callback: CallbackFunction) { }).then(execPipeline) function execPipeline() { - let data: FlexBuffer | string = '' + let data: Buffer | string = '' let writePending: number = _this.replyPending = _this._queue.length let node @@ -292,19 +291,14 @@ Pipeline.prototype.exec = function (callback: CallbackFunction) { bufferMode = true } if (bufferMode) { - if (typeof data === 'string') { - var flexBuffer = new FlexBuffer(0) - flexBuffer.write(data) - data = flexBuffer - } - (data as FlexBuffer).write(writable) + data = Buffer.concat([ + typeof data === 'string' ? Buffer.from(data, 'utf8') : data, + typeof writable === 'string' ? Buffer.from(writable, 'utf8') : writable + ]) } else { data += writable } if (!--writePending) { - if (bufferMode) { - data = data.getBuffer() - } if (_this.isCluster) { node.redis.stream.write(data) } else { diff --git a/package-lock.json b/package-lock.json index 9f0a781c..c5e73cfb 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1032,10 +1032,6 @@ "test-value": "^3.0.0" } }, - "flexbuffer": { - "version": "github:mercadolibre/flexbuffer-node#1487df393a30872e3e81b246711a4cf6b0b23314", - "from": "github:mercadolibre/flexbuffer-node#1487df393a30872e3e81b246711a4cf6b0b23314" - }, "for-in": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/for-in/-/for-in-1.0.2.tgz", diff --git a/package.json b/package.json index 0fb57228..ccddc83c 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,6 @@ "cluster-key-slot": "^1.0.6", "debug": "^3.1.0", "denque": "^1.1.0", - "flexbuffer": "github:mercadolibre/flexbuffer-node#1487df393a30872e3e81b246711a4cf6b0b23314", "lodash.defaults": "^4.2.0", "lodash.flatten": "^4.4.0", "redis-commands": "1.4.0",