-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New RESP2 parser #1899
Merged
Merged
New RESP2 parser #1899
Changes from all commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
ec1b7a0
parser
leibale 7670d17
Merge branch 'master' of github.com:redis/node-redis into parser
leibale b0b2464
a new RESP parser :)
leibale 9c7087e
clean code
leibale 35466e7
fix simple string and bulk string cursor
leibale 48fb5ae
performance improvements
leibale 1d09acb
change typescript compiler target
leibale 165bee5
do not use stream.Transform
leibale ae9fed0
Update decoder.ts
leibale 9953e7e
fix for 1d09acb
leibale 89415cc
improve integer performance
leibale 8097431
revert 1d09acb
leibale 04e9eca
improve RESP2 decoder performance
leibale f8dacb7
improve performance
leibale 1331698
improve encode performance
leibale dcdceb1
remove unused import
leibale 6ff6d08
Merge branch 'master' of github.com:redis/node-redis into parser
leibale 53ac472
Merge branch 'master' of github.com:redis/node-redis into parser
leibale 7688d56
Merge branch 'master' of github.com:redis/node-redis into parser
leibale 0cd0a60
Merge branch 'master' of github.com:redis/node-redis into parser
leibale 783f4e7
upgrade benchmark deps
leibale e141eb0
clean code
leibale aaaa15c
fix socket error handlers, reset parser on error
leibale cfbfa2e
fix #2080 - reset pubSubState on socket error
leibale ab1a50b
Merge branch 'master' of github.com:redis/node-redis into parser
leibale aea1258
Merge branch 'error-handlers' of github.com:leibale/node-redis into p…
leibale ce37282
reset decoder on socket error
leibale 8ad0473
fix pubsub
leibale 46c2c5c
Merge branch 'error-handlers' of github.com:leibale/node-redis into p…
leibale 2c23787
fix "RedisSocketInitiator"
leibale 57bd86e
Merge branch 'error-handlers' of github.com:leibale/node-redis into p…
leibale e307d1f
fix returnStringsAsBuffers
leibale 8ec4855
fix merge
leibale 6b2de37
Merge branch 'master' into parser
leibale File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { strict as assert } from 'assert'; | ||
import BufferComposer from './buffer'; | ||
|
||
describe('Buffer Composer', () => { | ||
const composer = new BufferComposer(); | ||
|
||
it('should compose two buffers', () => { | ||
composer.write(Buffer.from([0])); | ||
assert.deepEqual( | ||
composer.end(Buffer.from([1])), | ||
Buffer.from([0, 1]) | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Composer } from './interface'; | ||
|
||
export default class BufferComposer implements Composer<Buffer> { | ||
private chunks: Array<Buffer> = []; | ||
|
||
write(buffer: Buffer): void { | ||
this.chunks.push(buffer); | ||
} | ||
|
||
end(buffer: Buffer): Buffer { | ||
this.write(buffer); | ||
return Buffer.concat(this.chunks.splice(0)); | ||
} | ||
|
||
reset() { | ||
this.chunks = []; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export interface Composer<T> { | ||
write(buffer: Buffer): void; | ||
|
||
end(buffer: Buffer): T; | ||
|
||
reset(): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { strict as assert } from 'assert'; | ||
import StringComposer from './string'; | ||
|
||
describe('String Composer', () => { | ||
const composer = new StringComposer(); | ||
|
||
it('should compose two strings', () => { | ||
composer.write(Buffer.from([0])); | ||
assert.deepEqual( | ||
composer.end(Buffer.from([1])), | ||
Buffer.from([0, 1]).toString() | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { StringDecoder } from 'string_decoder'; | ||
import { Composer } from './interface'; | ||
|
||
export default class StringComposer implements Composer<string> { | ||
private decoder = new StringDecoder(); | ||
|
||
private string = ''; | ||
|
||
write(buffer: Buffer): void { | ||
this.string += this.decoder.write(buffer); | ||
} | ||
|
||
end(buffer: Buffer): string { | ||
const string = this.string + this.decoder.end(buffer); | ||
this.string = ''; | ||
return string; | ||
} | ||
|
||
reset() { | ||
this.string = ''; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,195 @@ | ||
import { strict as assert } from 'assert'; | ||
import { SinonSpy, spy } from 'sinon'; | ||
import RESP2Decoder from './decoder'; | ||
import { ErrorReply } from '../../errors'; | ||
|
||
interface DecoderAndSpies { | ||
decoder: RESP2Decoder; | ||
returnStringsAsBuffersSpy: SinonSpy; | ||
onReplySpy: SinonSpy; | ||
} | ||
|
||
function createDecoderAndSpies(returnStringsAsBuffers: boolean): DecoderAndSpies { | ||
const returnStringsAsBuffersSpy = spy(() => returnStringsAsBuffers), | ||
onReplySpy = spy(); | ||
|
||
return { | ||
decoder: new RESP2Decoder({ | ||
returnStringsAsBuffers: returnStringsAsBuffersSpy, | ||
onReply: onReplySpy | ||
}), | ||
returnStringsAsBuffersSpy, | ||
onReplySpy | ||
}; | ||
} | ||
|
||
function writeChunks(stream: RESP2Decoder, buffer: Buffer) { | ||
let i = 0; | ||
while (i < buffer.length) { | ||
stream.write(buffer.slice(i, ++i)); | ||
} | ||
} | ||
|
||
type Replies = Array<Array<unknown>>; | ||
|
||
interface TestsOptions { | ||
toWrite: Buffer; | ||
returnStringsAsBuffers: boolean; | ||
replies: Replies; | ||
} | ||
|
||
function generateTests({ | ||
toWrite, | ||
returnStringsAsBuffers, | ||
replies | ||
}: TestsOptions): void { | ||
Comment on lines
+41
to
+45
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No commenting |
||
it('single chunk', () => { | ||
const { decoder, returnStringsAsBuffersSpy, onReplySpy } = | ||
createDecoderAndSpies(returnStringsAsBuffers); | ||
decoder.write(toWrite); | ||
assert.equal(returnStringsAsBuffersSpy.callCount, replies.length); | ||
testReplies(onReplySpy, replies); | ||
}); | ||
|
||
it('multiple chunks', () => { | ||
const { decoder, returnStringsAsBuffersSpy, onReplySpy } = | ||
createDecoderAndSpies(returnStringsAsBuffers); | ||
writeChunks(decoder, toWrite); | ||
assert.equal(returnStringsAsBuffersSpy.callCount, replies.length); | ||
testReplies(onReplySpy, replies); | ||
}); | ||
} | ||
|
||
function testReplies(spy: SinonSpy, replies: Replies): void { | ||
if (!replies) { | ||
assert.equal(spy.callCount, 0); | ||
return; | ||
} | ||
|
||
assert.equal(spy.callCount, replies.length); | ||
for (const [i, reply] of replies.entries()) { | ||
assert.deepEqual( | ||
spy.getCall(i).args, | ||
reply | ||
); | ||
} | ||
} | ||
|
||
describe('RESP2Parser', () => { | ||
describe('Simple String', () => { | ||
describe('as strings', () => { | ||
generateTests({ | ||
toWrite: Buffer.from('+OK\r\n'), | ||
returnStringsAsBuffers: false, | ||
replies: [['OK']] | ||
}); | ||
}); | ||
|
||
describe('as buffers', () => { | ||
generateTests({ | ||
toWrite: Buffer.from('+OK\r\n'), | ||
returnStringsAsBuffers: true, | ||
replies: [[Buffer.from('OK')]] | ||
}); | ||
}); | ||
}); | ||
|
||
describe('Error', () => { | ||
generateTests({ | ||
toWrite: Buffer.from('-ERR\r\n'), | ||
returnStringsAsBuffers: false, | ||
replies: [[new ErrorReply('ERR')]] | ||
}); | ||
}); | ||
|
||
describe('Integer', () => { | ||
describe('-1', () => { | ||
generateTests({ | ||
toWrite: Buffer.from(':-1\r\n'), | ||
returnStringsAsBuffers: false, | ||
replies: [[-1]] | ||
}); | ||
}); | ||
|
||
describe('0', () => { | ||
generateTests({ | ||
toWrite: Buffer.from(':0\r\n'), | ||
returnStringsAsBuffers: false, | ||
replies: [[0]] | ||
}); | ||
}); | ||
}); | ||
|
||
describe('Bulk String', () => { | ||
describe('null', () => { | ||
generateTests({ | ||
toWrite: Buffer.from('$-1\r\n'), | ||
returnStringsAsBuffers: false, | ||
replies: [[null]] | ||
}); | ||
}); | ||
|
||
describe('as strings', () => { | ||
generateTests({ | ||
toWrite: Buffer.from('$2\r\naa\r\n'), | ||
returnStringsAsBuffers: false, | ||
replies: [['aa']] | ||
}); | ||
}); | ||
|
||
describe('as buffers', () => { | ||
generateTests({ | ||
toWrite: Buffer.from('$2\r\naa\r\n'), | ||
returnStringsAsBuffers: true, | ||
replies: [[Buffer.from('aa')]] | ||
}); | ||
}); | ||
}); | ||
|
||
describe('Array', () => { | ||
describe('null', () => { | ||
generateTests({ | ||
toWrite: Buffer.from('*-1\r\n'), | ||
returnStringsAsBuffers: false, | ||
replies: [[null]] | ||
}); | ||
}); | ||
|
||
const arrayBuffer = Buffer.from( | ||
'*5\r\n' + | ||
'+OK\r\n' + | ||
'-ERR\r\n' + | ||
':0\r\n' + | ||
'$1\r\na\r\n' + | ||
'*0\r\n' | ||
); | ||
|
||
describe('as strings', () => { | ||
generateTests({ | ||
toWrite: arrayBuffer, | ||
returnStringsAsBuffers: false, | ||
replies: [[[ | ||
'OK', | ||
new ErrorReply('ERR'), | ||
0, | ||
'a', | ||
[] | ||
]]] | ||
}); | ||
}); | ||
|
||
describe('as buffers', () => { | ||
generateTests({ | ||
toWrite: arrayBuffer, | ||
returnStringsAsBuffers: true, | ||
replies: [[[ | ||
Buffer.from('OK'), | ||
new ErrorReply('ERR'), | ||
0, | ||
Buffer.from('a'), | ||
[] | ||
]]] | ||
}); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Question: Are deepequal comparisons expensive in node? They are in Go.