diff --git a/packages/krpc-client/src/client.ts b/packages/krpc-client/src/client.ts index 3a8525d..1d7f6fd 100644 --- a/packages/krpc-client/src/client.ts +++ b/packages/krpc-client/src/client.ts @@ -106,11 +106,19 @@ export class KRPCClient { }); let resp: { status: number | string; message: string; clientIdentifier: Uint8Array }; try { + const rpcRaw = await recvRawMessage(this.rpcSocket); + if (process.env.KRPC_DEBUG) { + // eslint-disable-next-line no-console + console.log( + '[krpc-client] rpc handshake raw response (' + rpcRaw.length + ' bytes):', + Buffer.from(rpcRaw).toString('hex'), + ); + } resp = decodeMessage<{ status: number | string; message: string; clientIdentifier: Uint8Array; - }>(KRPC.ConnectionResponse, await recvRawMessage(this.rpcSocket)); + }>(KRPC.ConnectionResponse, rpcRaw); } catch (e) { throw new Error(`kRPC RPC handshake (response decode) failed: ${formatErr(e)}`); } @@ -136,10 +144,26 @@ export class KRPCClient { type: 1, // STREAM clientIdentifier: this.clientIdentifier, }); - const streamResp = decodeMessage<{ status: number | string; message: string }>( - KRPC.ConnectionResponse, - await recvRawMessage(this.streamSocket), - ); + let streamResp: { status: number | string; message: string }; + try { + const streamRaw = await recvRawMessage(this.streamSocket); + // Diagnostic: log the raw bytes for the stream handshake response + // so we can see what the kRPC server actually sent. Useful when + // debugging "Cannot read properties of null" type errors. + if (process.env.KRPC_DEBUG) { + // eslint-disable-next-line no-console + console.log( + '[krpc-client] stream handshake raw response (' + streamRaw.length + ' bytes):', + Buffer.from(streamRaw).toString('hex'), + ); + } + streamResp = decodeMessage<{ status: number | string; message: string }>( + KRPC.ConnectionResponse, + streamRaw, + ); + } catch (e) { + throw new Error(`kRPC Stream handshake (response decode) failed: ${formatErr(e)}`); + } if (streamResp.status !== 'OK' && streamResp.status !== 0) { throw new Error(`Stream handshake failed: ${streamResp.status} ${streamResp.message}`); }