From e0239d280504bedc0e710acdb5d571a910b19112 Mon Sep 17 00:00:00 2001 From: Benjamin Wilson Date: Thu, 4 Jul 2024 17:19:58 -0400 Subject: [PATCH] handle partial socket data --- src/models/StratumV1Client.ts | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/src/models/StratumV1Client.ts b/src/models/StratumV1Client.ts index 9592614..9f83fe4 100644 --- a/src/models/StratumV1Client.ts +++ b/src/models/StratumV1Client.ts @@ -52,7 +52,8 @@ export class StratumV1Client { public noFee: boolean; public hashRate: number; - + private buffer: string = ''; + constructor( public readonly socket: Socket, private readonly stratumV1JobsService: StratumV1JobsService, @@ -65,19 +66,22 @@ export class StratumV1Client { private readonly addressSettingsService: AddressSettingsService ) { - const rl = createInterface({ - input: this.socket - }) + this.socket.on('data', (data: Buffer) => { + this.buffer += data.toString(); + let lines = this.buffer.split('\n'); + this.buffer = lines.pop() || ''; // Save the last part of the data (incomplete line) to the buffer - rl.on("line", async (line) => { - try { - await this.handleMessage(line); - } catch (e) { - await this.socket.end(); - console.error(e); - } - }) - rl.on('error', async (error: Error) => { }); + lines + .filter(m => m.length > 0) + .forEach(async (m) => { + try { + await this.handleMessage(m); + } catch (e) { + await this.socket.end(); + console.error(e); + } + }); + }); }