Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
marshall({ headers: rawHeaders, body }: Message): Uint8Array {
const headers = this.headerMarshaller.format(rawHeaders);
const length = headers.byteLength + body.byteLength + 16;
const out = new Uint8Array(length);
const view = new DataView(out.buffer, out.byteOffset, out.byteLength);
const checksum = new Crc32();
// Format message
view.setUint32(0, length, false);
view.setUint32(4, headers.byteLength, false);
view.setUint32(8, checksum.update(out.subarray(0, 8)).digest(), false);
out.set(headers, 12);
out.set(body, headers.byteLength + 12);
// Write trailing message checksum
view.setUint32(
length - 4,
checksum.update(out.subarray(8, length - 4)).digest(),
false
);
return out;
const messageLength = view.getUint32(0, false);
if (byteLength !== messageLength) {
throw new Error(
"Reported message length does not match received message length"
);
}
const headerLength = view.getUint32(PRELUDE_MEMBER_LENGTH, false);
const expectedPreludeChecksum = view.getUint32(PRELUDE_LENGTH, false);
const expectedMessageChecksum = view.getUint32(
byteLength - CHECKSUM_LENGTH,
false
);
const checksummer = new Crc32().update(
new Uint8Array(buffer, byteOffset, PRELUDE_LENGTH)
);
if (expectedPreludeChecksum !== checksummer.digest()) {
throw new Error(
`The prelude checksum specified in the message (${expectedPreludeChecksum}) does not match the calculated CRC32 checksum (${checksummer.digest()})`
);
}
checksummer.update(
new Uint8Array(
buffer,
byteOffset + PRELUDE_LENGTH,
byteLength - (PRELUDE_LENGTH + CHECKSUM_LENGTH)
)
);
if (expectedMessageChecksum !== checksummer.digest()) {