UNPKG

3.06 kBTypeScriptView Raw
1import { IRawFrameType } from './types.js';
2/**
3 * This is an evented, rec descent parser.
4 * A stream of Octets can be passed and whenever it recognizes
5 * a complete Frame or an incoming ping it will invoke the registered callbacks.
6 *
7 * All incoming Octets are fed into _onByte function.
8 * Depending on current state the _onByte function keeps changing.
9 * Depending on the state it keeps accumulating into _token and _results.
10 * State is indicated by current value of _onByte, all states are named as _collect.
11 *
12 * STOMP standards https://stomp.github.io/stomp-specification-1.2.html
13 * imply that all lengths are considered in bytes (instead of string lengths).
14 * So, before actual parsing, if the incoming data is String it is converted to Octets.
15 * This allows faithful implementation of the protocol and allows NULL Octets to be present in the body.
16 *
17 * There is no peek function on the incoming data.
18 * When a state change occurs based on an Octet without consuming the Octet,
19 * the Octet, after state change, is fed again (_reinjectByte).
20 * This became possible as the state change can be determined by inspecting just one Octet.
21 *
22 * There are two modes to collect the body, if content-length header is there then it by counting Octets
23 * otherwise it is determined by NULL terminator.
24 *
25 * Following the standards, the command and headers are converted to Strings
26 * and the body is returned as Octets.
27 * Headers are returned as an array and not as Hash - to allow multiple occurrence of an header.
28 *
29 * This parser does not use Regular Expressions as that can only operate on Strings.
30 *
31 * It handles if multiple STOMP frames are given as one chunk, a frame is split into multiple chunks, or
32 * any combination there of. The parser remembers its state (any partial frame) and continues when a new chunk
33 * is pushed.
34 *
35 * Typically the higher level function will convert headers to Hash, handle unescaping of header values
36 * (which is protocol version specific), and convert body to text.
37 *
38 * Check the parser.spec.js to understand cases that this parser is supposed to handle.
39 *
40 * Part of `@stomp/stompjs`.
41 *
42 * @internal
43 */
44export declare class Parser {
45 onFrame: (rawFrame: IRawFrameType) => void;
46 onIncomingPing: () => void;
47 private readonly _encoder;
48 private readonly _decoder;
49 private _results;
50 private _token;
51 private _headerKey;
52 private _bodyBytesRemaining;
53 private _onByte;
54 constructor(onFrame: (rawFrame: IRawFrameType) => void, onIncomingPing: () => void);
55 parseChunk(segment: string | ArrayBuffer, appendMissingNULLonIncoming?: boolean): void;
56 private _collectFrame;
57 private _collectCommand;
58 private _collectHeaders;
59 private _reinjectByte;
60 private _collectHeaderKey;
61 private _collectHeaderValue;
62 private _setupCollectBody;
63 private _collectBodyNullTerminated;
64 private _collectBodyFixedSize;
65 private _retrievedBody;
66 private _consumeByte;
67 private _consumeTokenAsUTF8;
68 private _consumeTokenAsRaw;
69 private _initState;
70}