Initial
This commit is contained in:
30
resources/app/node_modules/@smithy/eventstream-codec/dist-es/splitMessage.js
generated
vendored
Normal file
30
resources/app/node_modules/@smithy/eventstream-codec/dist-es/splitMessage.js
generated
vendored
Normal file
@@ -0,0 +1,30 @@
|
||||
import { Crc32 } from "@aws-crypto/crc32";
|
||||
const PRELUDE_MEMBER_LENGTH = 4;
|
||||
const PRELUDE_LENGTH = PRELUDE_MEMBER_LENGTH * 2;
|
||||
const CHECKSUM_LENGTH = 4;
|
||||
const MINIMUM_MESSAGE_LENGTH = PRELUDE_LENGTH + CHECKSUM_LENGTH * 2;
|
||||
export function splitMessage({ byteLength, byteOffset, buffer }) {
|
||||
if (byteLength < MINIMUM_MESSAGE_LENGTH) {
|
||||
throw new Error("Provided message too short to accommodate event stream message overhead");
|
||||
}
|
||||
const view = new DataView(buffer, byteOffset, byteLength);
|
||||
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()) {
|
||||
throw new Error(`The message checksum (${checksummer.digest()}) did not match the expected value of ${expectedMessageChecksum}`);
|
||||
}
|
||||
return {
|
||||
headers: new DataView(buffer, byteOffset + PRELUDE_LENGTH + CHECKSUM_LENGTH, headerLength),
|
||||
body: new Uint8Array(buffer, byteOffset + PRELUDE_LENGTH + CHECKSUM_LENGTH + headerLength, messageLength - headerLength - (PRELUDE_LENGTH + CHECKSUM_LENGTH + CHECKSUM_LENGTH)),
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user