Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
private _feed_messageChunk(chunk: Buffer) {
assert(chunk);
const messageHeader = readMessageHeader(new BinaryStream(chunk));
/**
* notify the observers that new message chunk has been received
* @event chunk
* @param messageChunk the raw message chunk
*/
this.emit("chunk", chunk);
if (messageHeader.isFinal === "F") {
// last message
this._append(chunk);
if (this._hasReceivedError) {
return false;
}
const fullMessageBody: Buffer = this.blocks.length === 1 ? this.blocks[0] : Buffer.concat(this.blocks);
private _send_chunk(requestId: number, chunk: Buffer | null) {
const requestData = this._requests[requestId];
if (chunk) {
/**
* notify the observer that a message chunk is about to be sent to the server
* @event send_chunk
* @param message_chunk {Object} the message chunk
*/
this.emit("send_chunk", chunk);
/* istanbul ignore next */
if (doDebug && checkChunks) {
verify_message_chunk(chunk);
debugLog(chalk.yellow("CLIENT SEND chunk "));
debugLog(chalk.yellow(messageHeaderToString(chunk)));
debugLog(chalk.red(hexDump(chunk)));
}
assert(this._transport);
this._transport.write(chunk);
requestData.chunk_count += 1;
} else {
// last chunk ....
/* istanbul ignore next */
if (doDebug && checkChunks) {
debugLog(chalk.yellow("CLIENT SEND done."));
}
if (requestData) {
const self = this;
const request_data = self._request_data[requestId];
if (messageChunk) {
/**
* notify the observer that a message chunk is about to be sent to the server
* @event send_chunk
* @param message_chunk {Object} the message chunk
*/
self.emit("send_chunk", messageChunk);
/* istanbul ignore next */
if (doDebug && false) {
verify_message_chunk(messageChunk);
debugLog("CLIENT SEND chunk ".yellow);
debugLog(messageHeaderToString(messageChunk).yellow);
debugLog(hexDump(messageChunk).red);
}
assert(self._transport);
self._transport.write(messageChunk);
request_data.chunk_count += 1;
} else {
// last chunk ....
/* istanbul ignore next */
if (doDebug) {
debugLog("CLIENT SEND done.".yellow.bold);
}
if (request_data) {
maxChunkCount: self.maxChunkCount
});
//xx acknowledgeMessage.receiveBufferSize = 8192;
//xx acknowledgeMessage.sendBufferSize = 8192;
//xx console.log("xxx receiveBufferSize = ",acknowledgeMessage.receiveBufferSize , helloMessage.receiveBufferSize) ;
//xx console.log("xxx sendBufferSize = ",acknowledgeMessage.sendBufferSize , helloMessage.sendBufferSize);
//xx console.log("xxx maxMessageSize = ",acknowledgeMessage.maxMessageSize , helloMessage.maxMessageSize);
//xx console.log("xxx maxChunkCount = ",acknowledgeMessage.maxChunkCount , helloMessage.maxChunkCount);
const messageChunk = packTcpMessage("ACK", acknowledgeMessage);
/* istanbul ignore next*/
if (doDebug) {
verify_message_chunk(messageChunk);
debugLog("server send: " + "ACK".yellow);
debugLog("server send: " + hexDump(messageChunk));
debugLog("acknowledgeMessage=", acknowledgeMessage);
}
// send the ACK reply
self.write(messageChunk);
};
writeSequenceHeaderFunc: (buffer: Buffer) => {
// assert(buffer.length === this.sequenceHeaderSize);
this.writeSequenceHeader(buffer);
},
// ---------------------------------------- Signing stuff
signBufferFunc: options.signBufferFunc,
signatureLength: options.signatureLength,
// ---------------------------------------- Encrypting stuff
cipherBlockSize: options.cipherBlockSize,
encryptBufferFunc: options.encryptBufferFunc,
plainBlockSize: options.plainBlockSize,
};
this.chunkManager = new ChunkManager(params);
this.chunkManager.on("chunk", (chunk: Buffer, isLast: boolean) => {
/**
* @event chunk
*/
this.emit("chunk", chunk, isLast || this.aborted);
});
}
writeSequenceHeaderFunc: function(block) {
assert(block.length === this.sequenceHeaderSize);
self.writeSequenceHeader(block);
},
// ---------------------------------------- Signing stuff
signatureLength: options.signatureLength,
compute_signature: options.signingFunc,
// ---------------------------------------- Encrypting stuff
plainBlockSize: options.plainBlockSize,
cipherBlockSize: options.cipherBlockSize,
encrypt_buffer: options.encrypt_buffer
};
self.chunkManager = new ChunkManager(params);
self.chunkManager.on("chunk", function(chunk, is_last) {
/**
* @event chunk
* @param chunk {Buffer}
*/
self.emit("chunk", chunk, is_last || self.aborted);
});
};
util.inherits(SecureMessageChunkManager, EventEmitter);
private _handle_ACK_response(messageChunk: Buffer, callback: ErrorCallback) {
const _stream = new BinaryStream(messageChunk);
const messageHeader = readMessageHeader(_stream);
let err;
/* istanbul ignore next */
if (messageHeader.isFinal !== "F") {
err = new Error(" invalid ACK message");
return callback(err);
}
let responseClass;
let response;
if (messageHeader.msgType === "ERR") {
responseClass = TCPErrorMessage;
_stream.rewind();
response = decodeMessage(_stream, responseClass) as TCPErrorMessage;
err = new Error("ACK: ERR received " + response.statusCode.toString() + " : " + response.reason);
ClientSecureChannelLayer.prototype._on_receive_message_chunk = function (message_chunk) {
const self = this;
/* istanbul ignore next */
if (doDebug) {
const _stream = new BinaryStream(message_chunk);
const messageHeader = readMessageHeader(_stream);
debugLog("CLIENT RECEIVED " + (JSON.stringify(messageHeader) + "").yellow);
debugLog("\n" + hexDump(message_chunk).blue);
debugLog(messageHeaderToString(message_chunk));
}
self.messageBuilder.feed(message_chunk);
};
private _on_receive_message_chunk(messageChunk: Buffer) {
/* istanbul ignore next */
if (doDebug1) {
const _stream = new BinaryStream(messageChunk);
const messageHeader = readMessageHeader(_stream);
debugLog("CLIENT RECEIVED " + chalk.yellow(JSON.stringify(messageHeader) + ""));
debugLog("\n" + hexDump(messageChunk));
debugLog(messageHeaderToString(messageChunk));
}
this.messageBuilder.feed(messageChunk);
}
protected _read_headers(binaryStream: BinaryStream): boolean {
this.messageHeader = readMessageHeader(binaryStream);
assert(binaryStream.length === 8, "expecting message header to be 8 bytes");
this.channelId = binaryStream.readUInt32();
assert(binaryStream.length === 12);
// verifying secure ChannelId
if (this._expectedChannelId && this.channelId !== this._expectedChannelId) {
return this._report_error("Invalid secure channel Id");
}
return true;
}