@rwx-research/abq
Advanced tools
Comparing version 0.1.0-alpha.7 to 0.1.0-alpha.8
@@ -15,3 +15,5 @@ /// <reference types="node" /> | ||
*/ | ||
export declare function protocolRead(stream: Readable): Promise<AbqTypes.InitMessage | AbqTypes.TestCaseMessage | null>; | ||
export declare function protocolRead(stream: Readable, { debug }?: { | ||
debug: boolean; | ||
}): Promise<AbqTypes.InitMessage | AbqTypes.TestCaseMessage | null>; | ||
/** | ||
@@ -18,0 +20,0 @@ * Reads messages using the standard 4-byte header protocol. |
@@ -48,3 +48,3 @@ "use strict"; | ||
*/ | ||
function protocolRead(stream) { | ||
function protocolRead(stream, { debug } = { debug: false }) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
@@ -61,11 +61,25 @@ return yield new Promise(resolve => { | ||
if (messageSizeBuffer === null) { | ||
if (debug) { | ||
console.log('protocolRead:', 'received no data trying to read message size'); | ||
} | ||
return; | ||
} | ||
messageSize = messageSizeBuffer.readUInt32BE(0); | ||
if (debug) { | ||
console.log('protocolRead:', `read message size ${messageSize}`); | ||
} | ||
} | ||
const messageBuffer = stream.read(messageSize); | ||
if (messageBuffer === null) { | ||
if (debug) { | ||
console.log('protocolRead:', 'received no data trying to read message'); | ||
} | ||
return; | ||
} | ||
resolve(JSON.parse(messageBuffer.toString('utf8'))); | ||
const message = messageBuffer.toString('utf8'); | ||
if (debug) { | ||
console.log('protocolRead:', `read message ${message}`); | ||
} | ||
removeListeners(); | ||
resolve(JSON.parse(message)); | ||
}; | ||
@@ -112,10 +126,10 @@ const closeHandler = () => { | ||
messageSize = buffer.readUInt32BE(0); | ||
buffer = buffer.subarray(4); | ||
} | ||
if (messageSize && buffer.length >= messageSize + 4) { | ||
// We now know the whole message is available; get it. | ||
const currentMessage = buffer.toString('utf8', 4, 4 + messageSize); | ||
const currentMessage = buffer.toString('utf8'); | ||
// There might be an additional message waiting for us behind the one we | ||
// just parsed. Reset the buffer to this new message. | ||
const newBuffer = buffer.subarray(4 + messageSize); | ||
buffer = newBuffer; | ||
buffer = buffer.subarray(messageSize); | ||
yield handler(JSON.parse(currentMessage)); | ||
@@ -122,0 +136,0 @@ } |
{ | ||
"name": "@rwx-research/abq", | ||
"version": "0.1.0-alpha.7", | ||
"version": "0.1.0-alpha.8", | ||
"repository": { | ||
@@ -5,0 +5,0 @@ "type": "git", |
17847
441