Skip to content

Commit

Permalink
fix: missing responses
Browse files Browse the repository at this point in the history
some response messages were lost due to faulty concatenation, resulting
in "Parallel promise to send message [n] did not resolve in time" error;
move message processing to a separate method, so that leftovers are
concatenated with the beginning of the next chunk, not the first
message in queue for processing
  • Loading branch information
ianshade committed Apr 21, 2021
1 parent ec1f85e commit d86e571
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
40 changes: 39 additions & 1 deletion src/__tests__/peptalk.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,20 @@ describe('PepTalk happy', () => {
ws.send(`${value.slice(13, 14)}\r\n`)
return ws.send(`${value.slice(14)}\r\n`)
}
return ws.send(`${index} ok <entry depth="${depth}" name="${name}"/>`)
if (bits[2] === '4' || bits[2] === '5' || bits[2] === '6') {
return
}
if (bits[2] === '7') {
const value = (id: number) => `<entry depth="${depth}" name="${name}">${id}</entry>`
ws.send(
`${+index - 3} ok {${value(4).length}}${value(4)}\r\n${+index - 2} ok {${value(5).length}}${value(
5
)}\r\n${+index - 1} ok {${value(6).length}}${value(6).slice(0, 13)}`
)
ws.send(`${value(6).slice(13)}\r\n`)
return ws.send(`${index} ok {${value(7).length}}${value(7)}\r\n`)
}
return ws.send(`${index} ok <entry depth="${depth}" name="${name}"/>\r\n`)
}
if (message.indexOf('copy') >= 0) {
let destination = message.slice(message.lastIndexOf('/') + 1)
Expand Down Expand Up @@ -171,6 +184,31 @@ describe('PepTalk happy', () => {
})
})

test('Multi chunk', async () => {
await Promise.all([
expect(pep.get('/multithree/with/lines/4', 7)).resolves.toMatchObject({
body: '<entry depth="7" name="multithree">4</entry>',
sent: 'get {24}/multithree/with/lines/4 7',
status: 'ok',
}),
expect(pep.get('/multithree/with/lines/5', 7)).resolves.toMatchObject({
body: '<entry depth="7" name="multithree">5</entry>',
sent: 'get {24}/multithree/with/lines/5 7',
status: 'ok',
}),
expect(pep.get('/multithree/with/lines/6', 7)).resolves.toMatchObject({
body: '<entry depth="7" name="multithree">6</entry>',
sent: 'get {24}/multithree/with/lines/6 7',
status: 'ok',
}),
expect(pep.get('/multithree/with/lines/7', 7)).resolves.toMatchObject({
body: '<entry depth="7" name="multithree">7</entry>',
sent: 'get {24}/multithree/with/lines/7 7',
status: 'ok',
}),
])
})

test('Copy', async () => {
await expect(pep.copy('/copy/source', '/copy/destination', LocationType.First)).resolves.toMatchObject({
body: 'destination',
Expand Down
10 changes: 7 additions & 3 deletions src/peptalk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ class PepTalk extends EventEmitter implements PepTalkClient, PepTalkJS {
this.port = port ? port : 8595
}

private processMessage(m: string): void {
private processChunk(m: string): void {
let split = m.trim().split('\r\n')
if (split.length === 0) return
const re = /\{(\d+)\}/g
Expand Down Expand Up @@ -406,16 +406,20 @@ class PepTalk extends EventEmitter implements PepTalkClient, PepTalkJS {
return
}
}
this.leftovers = leftovers ? leftovers : this.leftovers
if (split.length > 1) {
for (const sm of split) {
// console.log('smsm >>>', sm)
if (sm.length > 0) this.processMessage(sm)
}
return
}
this.leftovers = leftovers ? leftovers : this.leftovers
if (split.length === 0) return
m = split[0]
this.processMessage(m)
}

private processMessage(m: string): void {
const firstSpace = m.indexOf(' ')
if (firstSpace <= 0) return
const c = +m.slice(0, firstSpace)
Expand Down Expand Up @@ -538,7 +542,7 @@ class PepTalk extends EventEmitter implements PepTalkClient, PepTalkJS {
// console.log('<<<', `ws://${this.hostname}:${this.port}/`)
const ws = new websocket(`ws://${this.hostname}:${this.port}/`)
ws.once('open', () => {
ws.on('message', this.processMessage.bind(this))
ws.on('message', this.processChunk.bind(this))
resolve(ws)
})
ws.once('error', (err) => {
Expand Down

0 comments on commit d86e571

Please sign in to comment.