From d86e57199298db61e86e869a8ac17d4fc58e4e36 Mon Sep 17 00:00:00 2001 From: ianshade Date: Wed, 21 Apr 2021 21:30:55 +0200 Subject: [PATCH] fix: missing responses 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 --- src/__tests__/peptalk.spec.ts | 40 ++++++++++++++++++++++++++++++++++- src/peptalk.ts | 10 ++++++--- 2 files changed, 46 insertions(+), 4 deletions(-) diff --git a/src/__tests__/peptalk.spec.ts b/src/__tests__/peptalk.spec.ts index 533d715..685aad3 100644 --- a/src/__tests__/peptalk.spec.ts +++ b/src/__tests__/peptalk.spec.ts @@ -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 `) + if (bits[2] === '4' || bits[2] === '5' || bits[2] === '6') { + return + } + if (bits[2] === '7') { + const value = (id: number) => `${id}` + 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 \r\n`) } if (message.indexOf('copy') >= 0) { let destination = message.slice(message.lastIndexOf('/') + 1) @@ -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: '4', + sent: 'get {24}/multithree/with/lines/4 7', + status: 'ok', + }), + expect(pep.get('/multithree/with/lines/5', 7)).resolves.toMatchObject({ + body: '5', + sent: 'get {24}/multithree/with/lines/5 7', + status: 'ok', + }), + expect(pep.get('/multithree/with/lines/6', 7)).resolves.toMatchObject({ + body: '6', + sent: 'get {24}/multithree/with/lines/6 7', + status: 'ok', + }), + expect(pep.get('/multithree/with/lines/7', 7)).resolves.toMatchObject({ + body: '7', + 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', diff --git a/src/peptalk.ts b/src/peptalk.ts index 92336e1..3bc95e5 100644 --- a/src/peptalk.ts +++ b/src/peptalk.ts @@ -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 @@ -406,6 +406,7 @@ 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) @@ -413,9 +414,12 @@ class PepTalk extends EventEmitter implements PepTalkClient, PepTalkJS { } 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) @@ -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) => {