Skip to content

Commit

Permalink
fix: removed flexbuffer dependency (#856)
Browse files Browse the repository at this point in the history
  • Loading branch information
kroleg authored and luin committed May 3, 2019
1 parent 899486b commit 35e0c5e
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 25 deletions.
40 changes: 31 additions & 9 deletions lib/command.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}
}
16 changes: 5 additions & 11 deletions lib/pipeline.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down
4 changes: 0 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 35e0c5e

Please sign in to comment.