UNPKG

amqplib

Version:

An AMQP 0-9-1 (e.g., RabbitMQ) library and client.

235 lines (191 loc) • 6.58 kB
const { constants: bufferConstants } = require('node:buffer'); const defs = require('./defs'); const constants = defs.constants; const decode = defs.decode; module.exports.PROTOCOL_HEADER = `AMQP${String.fromCharCode(0, 0, 9, 1)}`; /* Frame format: 0 1 3 7 size+7 size+8 +------+---------+-------------+ +------------+ +-----------+ | type | channel | size | | payload | | frame-end | +------+---------+-------------+ +------------+ +-----------+ octet short long size octets octet In general I want to know those first three things straight away, so I can discard frames early. */ // framing constants const FRAME_METHOD = constants.FRAME_METHOD; const FRAME_HEARTBEAT = constants.FRAME_HEARTBEAT; const FRAME_HEADER = constants.FRAME_HEADER; const FRAME_BODY = constants.FRAME_BODY; const FRAME_END = constants.FRAME_END; // expected byte sizes for frame parts const TYPE_BYTES = 1; const CHANNEL_BYTES = 2; const SIZE_BYTES = 4; const FRAME_HEADER_BYTES = TYPE_BYTES + CHANNEL_BYTES + SIZE_BYTES; const FRAME_END_BYTES = 1; const LARGEST_BUFFER_BYTES = bufferConstants.MAX_LENGTH; const NO_BYTES = Buffer.alloc(0); /** * @typedef {{ * type: number, * channel: number, * size: number, * payload: Buffer, * rest: Buffer * }} FrameStructure */ function readInt64BE(buffer, offset) { return Number(buffer.readBigInt64BE(offset)); } // %%% TESTME possibly better to cons the first bit and write the // second directly, in the absence of IO lists /** * Make a frame header * @arg { number } channel * @arg { Buffer } payload */ module.exports.makeBodyFrame = (channel, payload) => { const frameSize = FRAME_HEADER_BYTES + payload.length + FRAME_END_BYTES; const frame = Buffer.alloc(frameSize); let offset = 0; offset = frame.writeUInt8(FRAME_BODY, offset); offset = frame.writeUInt16BE(channel, offset); offset = frame.writeInt32BE(payload.length, offset); payload.copy(frame, offset); offset += payload.length; frame.writeUInt8(FRAME_END, offset); return frame; }; /** * Parse an AMQP frame * @arg { Buffer } bin * @arg { number } max * @returns { FrameStructure | boolean } */ function parseFrame(bin, max) { if (bin.length < FRAME_HEADER_BYTES) { return false; } const type = bin.readUInt8(0); const channel = bin.readUInt16BE(1); const size = bin.readUInt32BE(3); if (max > 0 && size > max) { throw new Error(`Frame size ${size} exceeds maxFrameSize ${max}`); } const totalSize = FRAME_HEADER_BYTES + size + FRAME_END_BYTES; if (bin.length < totalSize) { return false; } const frameEnd = bin.readUInt8(FRAME_HEADER_BYTES + size); if (frameEnd !== FRAME_END) { throw new Error('Invalid frame'); } return { type, channel, size, payload: bin.subarray(FRAME_HEADER_BYTES, FRAME_HEADER_BYTES + size), rest: bin.subarray(totalSize), }; } module.exports.parseFrame = parseFrame; // See https://github.com/amqp-node/amqplib/pull/875 // // Bug: each read from the socket was appended to the unparsed bytes with Buffer.concat, which // copies everything received so far. A frame that arrived in many reads was copied many times. // // Simpler fix, rejected: allocate one buffer of the size the frame header declares, and fill it. // The header is only 7 bytes, so the client would reserve up to maxFrameSize (4 GiB when there // is no limit) before receiving any of the frame. Node 18 and 20 also cannot allocate a buffer // over 4 GiB, which the largest frame is. // // Actual fix: copy each read into a buffer that doubles when it is full, so each byte is copied // fewer than three times. Caveat: the buffer can be twice the size of what has arrived, and a // message body the application keeps holds on to the whole buffer it was read from. // // Better fix, being considered: https://github.com/amqp-node/amqplib/pull/860 replaces this // code and does not copy a message body at all until the message is assembled. // // From Node 22 a buffer can be larger than 4 GiB, so storageBytesFor can drop // largestBufferBytes and return twice unparsedBytes. class UnparsedBytes { constructor() { this.bytes = NO_BYTES; this.storage = NO_BYTES; this.storageUsed = 0; } append(incoming) { const unparsedBytes = this.bytes.length + incoming.length; const room = this.storage.length - this.storageUsed; if (incoming.length > room) this.#moveToLargerStorage(unparsedBytes); incoming.copy(this.storage, this.storageUsed); this.storageUsed += incoming.length; this.bytes = this.storage.subarray(this.storageUsed - unparsedBytes, this.storageUsed); } keepOnly(rest) { this.bytes = rest; if (rest.length === 0) this.#releaseStorage(); } #moveToLargerStorage(unparsedBytes) { const storage = Buffer.allocUnsafe(storageBytesFor(unparsedBytes, LARGEST_BUFFER_BYTES)); this.bytes.copy(storage); this.storage = storage; this.storageUsed = this.bytes.length; } #releaseStorage() { this.storage = NO_BYTES; this.storageUsed = 0; } } function storageBytesFor(unparsedBytes, largestBufferBytes) { const doubled = 2 * unparsedBytes; return Math.max(unparsedBytes, Math.min(doubled, largestBufferBytes)); } module.exports.UnparsedBytes = UnparsedBytes; module.exports.storageBytesFor = storageBytesFor; const HEARTBEAT = { channel: 0 }; /** * Decode AMQP frame into JS object * @param { FrameStructure } frame * @returns */ module.exports.decodeFrame = (frame) => { const payload = frame.payload; const channel = frame.channel; switch (frame.type) { case FRAME_METHOD: { const id = payload.readUInt32BE(0); const args = payload.subarray(4); const fields = decode(id, args); return { id, channel, fields }; } case FRAME_HEADER: { const id = payload.readUInt16BE(0); // const weight = payload.readUInt16BE(2) const size = readInt64BE(payload, 4); const flagsAndfields = payload.subarray(12); const fields = decode(id, flagsAndfields); return { id, channel, size, fields }; } case FRAME_BODY: return { channel, content: payload }; case FRAME_HEARTBEAT: return HEARTBEAT; default: throw new Error(`Unknown frame type ${frame.type}`); } }; // encoded heartbeat module.exports.HEARTBEAT_BUF = Buffer.from([ constants.FRAME_HEARTBEAT, 0, 0, 0, 0, // size = 0 0, 0, // channel = 0 constants.FRAME_END, ]); module.exports.HEARTBEAT = HEARTBEAT;