# Queue ## Purpose The Queue module provides the FIFO processing queue behind each pipeline stage, and the two specialised queues built on it: the seal queue (plaintext packets in, sealed frames out) and the open queue (frames in, plaintext packets out). Strict one-at-a-time processing is what lets a protocol assign a monotonically increasing counter to every frame. --- ## Key Interfaces ### `Queue` ```typescript interface Queue { readonly addMessage: (message: T) => void // Enqueue; starts processing when autoStart is on readonly isRunning: () => boolean // Whether the queue is processing readonly stop: () => void // Pause processing (messages accumulate) readonly resume: () => void // Resume processing accumulated messages readonly size: () => number // Number of messages waiting readonly currentMessage: () => T | null // The message being processed } ``` ### `MessageHandler` ```typescript type MessageHandler = (message: T) => Promise | void ``` ### `QueueFailureHandler` Called with the rejected input, why it was rejected, and the error the operation threw when it threw one. ```typescript type QueueFailureHandler = (raw: unknown, reason: string, cause?: unknown) => void ``` ### `QueueOperation`, `QueueCreatorArguments`, `QueueCreatorValidity` ```typescript type QueueOperation = PacketSealer | PacketOpener interface QueueCreatorArguments { label: string operation: QueueOperation logger: Logger onSuccess: (packet: T) => void onFail: QueueFailureHandler } interface QueueCreatorValidity { label: boolean operation: boolean logger: boolean onSuccess: boolean onFail: boolean } ``` ### `SealQueueCreater` and `OpenQueueCreater` ```typescript type SealQueueCreater = ( label: string, seal: PacketSealer, logger: Logger, onSuccess: (packet: WirePacket) => void, onFail: QueueFailureHandler ) => Queue type OpenQueueCreater = ( label: string, open: PacketOpener, logger: Logger, onSuccess: (packet: UnencryptedPacket) => void, onFail: QueueFailureHandler ) => Queue ``` --- ## Queue Types | Queue Creator | Input | Output | Used by | | ------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------- | ------------------------------------ | | [`createSealQueue`](https://www.hyperfrontend.dev/docs/libraries/network-protocol/queue/#api-createSealQueue) | [`UnencryptedPacket`](https://www.hyperfrontend.dev/docs/libraries/network-protocol/#api-UnencryptedPacket) | [`WirePacket`](https://www.hyperfrontend.dev/docs/libraries/network-protocol/#api-WirePacket) | [`sender/`](https://www.hyperfrontend.dev/docs/libraries/network-protocol/sender/) | | [`createOpenQueue`](https://www.hyperfrontend.dev/docs/libraries/network-protocol/queue/#api-createOpenQueue) | [`WirePacket`](https://www.hyperfrontend.dev/docs/libraries/network-protocol/#api-WirePacket) | [`UnencryptedPacket`](https://www.hyperfrontend.dev/docs/libraries/network-protocol/#api-UnencryptedPacket) | [`receiver/`](https://www.hyperfrontend.dev/docs/libraries/network-protocol/receiver/) | --- ## Factory Functions ### `createQueue` **Location**: [`@hyperfrontend/network-protocol/queue`](https://www.hyperfrontend.dev/docs/libraries/network-protocol/queue.md) ```typescript function createQueue>(processMessage: MessageHandler, autoStart = true): Queue ``` Messages are processed strictly one at a time in arrival order; the next one starts only after the handler's promise settles. The backing store is an array with a moving head, so a pull is constant time; once 1024 pulled slots sit at the front the array is compacted. ```typescript import { createQueue } from '@hyperfrontend/network-protocol/queue' const queue = createQueue<{ id: string }>(async (message) => { await handle(message) }) queue.addMessage({ id: '1' }) // starts processing at once ``` Throws `processMessage must be a function` and `autoStart must be a boolean` at creation; [`addMessage`](https://www.hyperfrontend.dev/docs/libraries/network-protocol/queue/#api-Queue-prop-addMessage) throws a `TypeError` (`Message must be a non-null object`) for anything that is not an object. ### `createSealQueue` ```typescript import { createSealQueue } from '@hyperfrontend/network-protocol/queue' const sealing = createSealQueue( 'comms sender', protocol.seal, logger, (frame) => transport.post(frame), (packet, reason, cause) => report(reason, cause) ) sealing.addMessage(unencryptedPacket) ``` Each packet is checked with [`isValidUnencryptedPacket`](https://www.hyperfrontend.dev/docs/libraries/network-protocol/#api-isValidUnencryptedPacket), sealed, and the result checked with [`isValidWirePacket`](https://www.hyperfrontend.dev/docs/libraries/network-protocol/#api-isValidWirePacket) before [`onSuccess`](https://www.hyperfrontend.dev/docs/libraries/network-protocol/queue/#api-QueueCreatorArguments-prop-onSuccess). ### `createOpenQueue` ```typescript import { createOpenQueue } from '@hyperfrontend/network-protocol/queue' const opening = createOpenQueue( 'comms receiver', protocol.open, logger, (packet) => deliver(packet), (frame, reason, cause) => report(reason, cause) ) opening.addMessage(frame) ``` Each frame is checked with [`isValidWirePacket`](https://www.hyperfrontend.dev/docs/libraries/network-protocol/#api-isValidWirePacket), opened, and the result checked with [`isValidUnencryptedPacket`](https://www.hyperfrontend.dev/docs/libraries/network-protocol/#api-isValidUnencryptedPacket) before [`onSuccess`](https://www.hyperfrontend.dev/docs/libraries/network-protocol/queue/#api-QueueCreatorArguments-prop-onSuccess). --- ## Lifecycle Management ```typescript queue.stop() queue.isRunning() // false once the current message settles queue.addMessage(a) // accumulates while stopped queue.addMessage(b) queue.size() // 2 queue.resume() // processes a, then b ``` `currentMessage()` returns the message in flight, or `null` between messages. --- ## Error Handling A rejected input never blocks the queue: the stage logs it, calls [`onFail`](https://www.hyperfrontend.dev/docs/libraries/network-protocol/queue/#api-QueueCreatorArguments-prop-onFail), and moves on to the next message. | Queue | [`reason`](https://www.hyperfrontend.dev/docs/libraries/network-protocol/#api-PacketDrop-prop-reason) | [`cause`](https://www.hyperfrontend.dev/docs/libraries/network-protocol/#api-PacketDrop-prop-cause) | | ----- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------- | | seal | `Invalid packet ignored` | none | | seal | the message of the error [`seal`](https://www.hyperfrontend.dev/docs/libraries/network-protocol/#api-Protocol-prop-seal) threw (a [`ProtocolError`](https://www.hyperfrontend.dev/docs/libraries/network-protocol/security/#api-ProtocolError)) | the thrown error | | seal | `Sealed packet is not valid` | none | | open | `Invalid frame ignored` | none | | open | the message of the error [`open`](https://www.hyperfrontend.dev/docs/libraries/network-protocol/#api-Protocol-prop-open) threw (replay, forgery, malformed frame) | the thrown [`ProtocolError`](https://www.hyperfrontend.dev/docs/libraries/network-protocol/security/#api-ProtocolError) | | open | `Opened packet is not valid` | none | | both | `An unexpected error occurred. ` | the thrown error | The sender and receiver translate these calls into [`PacketDrop`](https://www.hyperfrontend.dev/docs/libraries/network-protocol/#api-PacketDrop) reports for the channel's [`onDrop`](https://www.hyperfrontend.dev/docs/libraries/network-protocol/#api-ChannelOptions-prop-onDrop). ### Validation Errors The specialised creators validate their arguments and throw `Cannot create seal queue without ...` or `Cannot create open queue without ...` followed by `a label`, `seal function` / `open function`, `a logger`, `a success callback function`, or `a failed callback function`. --- ## Relationship to Other Modules - **Depends on**: [`packet/`](https://www.hyperfrontend.dev/docs/libraries/network-protocol/packet/) (packet types and validations), [`@hyperfrontend/logging`](https://www.hyperfrontend.dev/docs/libraries/logging.md) - **Used by**: [`sender/`](https://www.hyperfrontend.dev/docs/libraries/network-protocol/sender/), [`receiver/`](https://www.hyperfrontend.dev/docs/libraries/network-protocol/receiver/) --- ## See Also - **[Library Index](../README.md)** - All modules - **[Architecture Guide](../../../ARCHITECTURE.md#queue)** - Queue architecture - **[Queue Entry](../../queue/README.md)** - The [`@hyperfrontend/network-protocol/queue`](https://www.hyperfrontend.dev/docs/libraries/network-protocol/queue.md) entry ### Related Modules | Module | Relationship | | ---------------------------------- | ------------------------------------- | | [sender/](https://www.hyperfrontend.dev/docs/libraries/network-protocol/sender/) | Wraps the seal queue | | [receiver/](https://www.hyperfrontend.dev/docs/libraries/network-protocol/receiver/) | Wraps the open queue | | [packet/](https://www.hyperfrontend.dev/docs/libraries/network-protocol/packet/) | Packet types processed in queues | | [protocol/](https://www.hyperfrontend.dev/docs/libraries/network-protocol/protocol/) | Supplies the seal and open operations | ## API Reference ### Functions #### createQueue ```typescript function createQueue(processMessage: MessageHandler, autoStart: boolean): Queue ``` Creates a message processing queue with FIFO ordering. Messages are processed strictly one at a time in arrival order; the next one starts only after the handler's promise settles. That ordering is what lets a protocol assign a monotonically increasing counter to each frame it seals or opens. A handler that rejects loses only its own message: the rejection is contained, the next message starts, and the queue stays resumable. Reporting the failure is the handler's job. **Parameters** | Name | Type | Description | | --- | --- | --- | | `processMessage` | `MessageHandler` | The handler function to process each message | | `autoStart` | `boolean` | Whether to automatically start processing messages (default: true) (default: `true`) | **Returns** `Queue`: A Queue instance with methods to manage message processing **Example: Creating a message processing queue** ```typescript const queue = createQueue(async (message) => { await processMessage(message) }) queue.addMessage({ type: 'ping', data: {} }) ``` ### Interfaces #### Queue Message processing queue interface **Properties** | Name | Type | Description | | --- | --- | --- | | `readonly addMessage` | `(message: T) => void` | Adds a message to the queue | | `readonly isRunning` | `() => boolean` | Returns whether the queue is processing messages | | `readonly stop` | `() => void` | Stops queue processing | | `readonly resume` | `() => void` | Resumes queue processing | | `readonly size` | `() => number` | Returns the number of messages in the queue | | `readonly currentMessage` | `() => T` | Returns the message currently being processed | #### QueueCreatorArguments Arguments for creating a queue instance **Properties** | Name | Type | Description | | --- | --- | --- | | `label` | `string` | Queue label for logging | | `operation` | `QueueOperation` | Packet operation function | | `logger` | `Logger` | Logger instance | | `onSuccess` | `(packet: T) => void` | Callback on successful packet processing | | `onFail` | `QueueFailureHandler` | Callback on packet processing failure | #### QueueCreatorValidity Validation result for queue creator arguments **Properties** | Name | Type | Description | | --- | --- | --- | | `label` | `boolean` | Whether label is valid | | `operation` | `boolean` | Whether operation is valid | | `logger` | `boolean` | Whether logger is valid | | `onSuccess` | `boolean` | Whether onSuccess callback is valid | | `onFail` | `boolean` | Whether onFail callback is valid | ### Types #### MessageHandler Function that handles messages from a queue ```typescript type MessageHandler = (message: T) => Promise | void ``` #### OpenQueueCreater Factory function for creating open queues ```typescript type OpenQueueCreater = (label: string, open: PacketOpener, logger: Logger, onSuccess: (packet: UnencryptedPacket) => void, onFail: QueueFailureHandler) => Queue ``` #### QueueFailureHandler Called with the rejected input, why it was rejected, and the error the operation threw when it threw one ```typescript type QueueFailureHandler = (raw: unknown, reason: string, cause?: unknown) => void ``` #### QueueOperation The packet operation a specialised queue runs ```typescript type QueueOperation = PacketSealer | PacketOpener ``` #### SealQueueCreater Factory function for creating seal queues ```typescript type SealQueueCreater = (label: string, seal: PacketSealer, logger: Logger, onSuccess: (packet: WirePacket) => void, onFail: QueueFailureHandler) => Queue ``` ### Variables #### createOpenQueue Creates the inbound queue: wire bytes in, plaintext packets out, one at a time. Bytes that are not a wire packet, an open that throws (a forged, replayed, or foreign frame), or an open that yields an invalid packet are reported through `onFail` and the queue moves on. Processing one frame at a time is what keeps a session's replay counter exact. ```typescript const createOpenQueue: OpenQueueCreater ``` **Value** `...` #### createSealQueue Creates the outbound queue: plaintext packets in, sealed wire bytes out, one at a time. A packet that is not a valid plaintext packet, a seal that throws, or a seal that yields something other than wire bytes is reported through `onFail` and the queue moves on. ```typescript const createSealQueue: SealQueueCreater ``` **Value** `...` --- Canonical page: https://www.hyperfrontend.dev/docs/libraries/network-protocol/queue/ This file: https://www.hyperfrontend.dev/docs/libraries/network-protocol/queue.md Documentation index: https://www.hyperfrontend.dev/llms.txt