import { fromHex, toHex } from "@smithy/util-hex-encoding"; import { fromUtf8 } from "@smithy/util-utf8"; export class HeaderFormatter { format(headers) { const chunks = []; for (const headerName of Object.keys(headers)) { const bytes = fromUtf8(headerName); chunks.push(Uint8Array.from([bytes.byteLength]), bytes, this.formatHeaderValue(headers[headerName])); } const out = new Uint8Array(chunks.reduce((carry, bytes) => carry + bytes.byteLength, 0)); let position = 0; for (const chunk of chunks) { out.set(chunk, position); position += chunk.byteLength; } return out; } formatHeaderValue(header) { switch (header.type) { case "boolean": return Uint8Array.from([header.value ? 0 : 1]); case "byte": return Uint8Array.from([2, header.value]); case "short": const shortView = new DataView(new ArrayBuffer(3)); shortView.setUint8(0, 3); shortView.setInt16(1, header.value, false); return new Uint8Array(shortView.buffer); case "integer": const intView = new DataView(new ArrayBuffer(5)); intView.setUint8(0, 4); intView.setInt32(1, header.value, false); return new Uint8Array(intView.buffer); case "long": const longBytes = new Uint8Array(9); longBytes[0] = 5; longBytes.set(header.value.bytes, 1); return longBytes; case "binary": const binView = new DataView(new ArrayBuffer(3 + header.value.byteLength)); binView.setUint8(0, 6); binView.setUint16(1, header.value.byteLength, false); const binBytes = new Uint8Array(binView.buffer); binBytes.set(header.value, 3); return binBytes; case "string": const utf8Bytes = fromUtf8(header.value); const strView = new DataView(new ArrayBuffer(3 + utf8Bytes.byteLength)); strView.setUint8(0, 7); strView.setUint16(1, utf8Bytes.byteLength, false); const strBytes = new Uint8Array(strView.buffer); strBytes.set(utf8Bytes, 3); return strBytes; case "timestamp": const tsBytes = new Uint8Array(9); tsBytes[0] = 8; tsBytes.set(Int64.fromNumber(header.value.valueOf()).bytes, 1); return tsBytes; case "uuid": if (!UUID_PATTERN.test(header.value)) { throw new Error(`Invalid UUID received: ${header.value}`); } const uuidBytes = new Uint8Array(17); uuidBytes[0] = 9; uuidBytes.set(fromHex(header.value.replace(/\-/g, "")), 1); return uuidBytes; } } } var HEADER_VALUE_TYPE; (function (HEADER_VALUE_TYPE) { HEADER_VALUE_TYPE[HEADER_VALUE_TYPE["boolTrue"] = 0] = "boolTrue"; HEADER_VALUE_TYPE[HEADER_VALUE_TYPE["boolFalse"] = 1] = "boolFalse"; HEADER_VALUE_TYPE[HEADER_VALUE_TYPE["byte"] = 2] = "byte"; HEADER_VALUE_TYPE[HEADER_VALUE_TYPE["short"] = 3] = "short"; HEADER_VALUE_TYPE[HEADER_VALUE_TYPE["integer"] = 4] = "integer"; HEADER_VALUE_TYPE[HEADER_VALUE_TYPE["long"] = 5] = "long"; HEADER_VALUE_TYPE[HEADER_VALUE_TYPE["byteArray"] = 6] = "byteArray"; HEADER_VALUE_TYPE[HEADER_VALUE_TYPE["string"] = 7] = "string"; HEADER_VALUE_TYPE[HEADER_VALUE_TYPE["timestamp"] = 8] = "timestamp"; HEADER_VALUE_TYPE[HEADER_VALUE_TYPE["uuid"] = 9] = "uuid"; })(HEADER_VALUE_TYPE || (HEADER_VALUE_TYPE = {})); const UUID_PATTERN = /^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/; export class Int64 { constructor(bytes) { this.bytes = bytes; if (bytes.byteLength !== 8) { throw new Error("Int64 buffers must be exactly 8 bytes"); } } static fromNumber(number) { if (number > 9223372036854776000 || number < -9223372036854776000) { throw new Error(`${number} is too large (or, if negative, too small) to represent as an Int64`); } const bytes = new Uint8Array(8); for (let i = 7, remaining = Math.abs(Math.round(number)); i > -1 && remaining > 0; i--, remaining /= 256) { bytes[i] = remaining; } if (number < 0) { negate(bytes); } return new Int64(bytes); } valueOf() { const bytes = this.bytes.slice(0); const negative = bytes[0] & 0b10000000; if (negative) { negate(bytes); } return parseInt(toHex(bytes), 16) * (negative ? -1 : 1); } toString() { return String(this.valueOf()); } } function negate(bytes) { for (let i = 0; i < 8; i++) { bytes[i] ^= 0xff; } for (let i = 7; i > -1; i--) { bytes[i]++; if (bytes[i] !== 0) break; } }