Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// Note: OPCUA requires that chunkSize is at least 8192
this.chunkSize = options.chunkSize || 1024 * 128;
this.msgType = msgType;
options.channelId = options.channelId || 0;
assert(_.isFinite(options.channelId));
this.channelId = options.channelId;
const requestId = options.requestId;
this.sequenceNumberGenerator = sequenceNumberGenerator;
assert(requestId > 0, "expecting a valid request ID");
this.sequenceHeader = new SequenceHeader({requestId, sequenceNumber: -1});
const securityHeaderSize = this.securityHeader.binaryStoreSize();
const sequenceHeaderSize = this.sequenceHeader.binaryStoreSize();
assert(sequenceHeaderSize === 8);
this.sequenceHeaderSize = sequenceHeaderSize;
this.headerSize = 12 + securityHeaderSize;
const params: IChunkManagerOptions = {
chunkSize: this.chunkSize,
headerSize: this.headerSize,
writeHeaderFunc: (buffer: Buffer, isLast: boolean, totalLength: number) => {
let finalC = isLast ? "F" : "C";
finalC = this.aborted ? "A" : finalC;
this.write_header(finalC, buffer, totalLength);
} else {
this.securityHeader = chooseSecurityHeader(msgType);
this.securityHeader.decode(binaryStream);
if (msgType === "OPN") {
const asymmetricAlgorithmSecurityHeader = this.securityHeader as AsymmetricAlgorithmSecurityHeader;
this.securityPolicy = fromURI(asymmetricAlgorithmSecurityHeader.securityPolicyUri);
this.cryptoFactory = getCryptoFactory(this.securityPolicy);
}
if (!this._decrypt(binaryStream)) {
return false;
}
this.sequenceHeader = new SequenceHeader();
this.sequenceHeader.decode(binaryStream);
/* istanbul ignore next */
if (doDebug) {
debugLog(" Sequence Header", this.sequenceHeader);
}
this._validateSequenceNumber(this.sequenceHeader.sequenceNumber);
}
return true;
}
export function messageHeaderToString(messageChunk: Buffer): string {
const stream = new BinaryStream(messageChunk);
const messageHeader = readMessageHeader(stream);
if (messageHeader.msgType === "ERR" || messageHeader.msgType === "HEL") {
return messageHeader.msgType + " " + messageHeader.isFinal + " length = " + messageHeader.length;
}
const securityHeader = chooseSecurityHeader(messageHeader.msgType);
const sequenceHeader = new SequenceHeader();
assert(stream.length === 8);
const channelId = stream.readUInt32();
securityHeader.decode(stream);
sequenceHeader.decode(stream);
const slice = messageChunk.slice(0, stream.length);
return messageHeader.msgType + " " +
messageHeader.isFinal +
" length = " + messageHeader.length +
" channel = " + channelId +
" seqNum = " + sequenceHeader.sequenceNumber +
" req ID = " + sequenceHeader.requestId +
" security = " + securityHeader.toString() +
"\n\n" + hexDump(slice);