forked from stomp-js/stompjs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.js
More file actions
222 lines (222 loc) · 7 KB
/
parser.js
File metadata and controls
222 lines (222 loc) · 7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/**
* @internal
*/
const NULL = 0;
/**
* @internal
*/
const LF = 10;
/**
* @internal
*/
const CR = 13;
/**
* @internal
*/
const COLON = 58;
/**
* This is an evented, rec descent parser.
* A stream of Octets can be passed and whenever it recognizes
* a complete Frame or an incoming ping it will invoke the registered callbacks.
*
* All incoming Octets are fed into _onByte function.
* Depending on current state the _onByte function keeps changing.
* Depending on the state it keeps accumulating into _token and _results.
* State is indicated by current value of _onByte, all states are named as _collect.
*
* STOMP standards https://stomp.github.io/stomp-specification-1.2.html
* imply that all lengths are considered in bytes (instead of string lengths).
* So, before actual parsing, if the incoming data is String it is converted to Octets.
* This allows faithful implementation of the protocol and allows NULL Octets to be present in the body.
*
* There is no peek function on the incoming data.
* When a state change occurs based on an Octet without consuming the Octet,
* the Octet, after state change, is fed again (_reinjectByte).
* This became possible as the state change can be determined by inspecting just one Octet.
*
* There are two modes to collect the body, if content-length header is there then it by counting Octets
* otherwise it is determined by NULL terminator.
*
* Following the standards, the command and headers are converted to Strings
* and the body is returned as Octets.
* Headers are returned as an array and not as Hash - to allow multiple occurrence of an header.
*
* This parser does not use Regular Expressions as that can only operate on Strings.
*
* It handles if multiple STOMP frames are given as one chunk, a frame is split into multiple chunks, or
* any combination there of. The parser remembers its state (any partial frame) and continues when a new chunk
* is pushed.
*
* Typically the higher level function will convert headers to Hash, handle unescaping of header values
* (which is protocol version specific), and convert body to text.
*
* Check the parser.spec.js to understand cases that this parser is supposed to handle.
*
* Part of `@stomp/stompjs`.
*
* @internal
*/
export class Parser {
constructor(onFrame, onIncomingPing) {
this.onFrame = onFrame;
this.onIncomingPing = onIncomingPing;
this._encoder = new TextEncoder();
this._decoder = new TextDecoder();
this._token = [];
this._initState();
}
parseChunk(segment, appendMissingNULLonIncoming = false) {
let chunk;
if (typeof segment === 'string') {
chunk = this._encoder.encode(segment);
}
else {
chunk = new Uint8Array(segment);
}
// See https://github.com/stomp-js/stompjs/issues/89
// Remove when underlying issue is fixed.
//
// Send a NULL byte, if the last byte of a Text frame was not NULL.F
if (appendMissingNULLonIncoming && chunk[chunk.length - 1] !== 0) {
const chunkWithNull = new Uint8Array(chunk.length + 1);
chunkWithNull.set(chunk, 0);
chunkWithNull[chunk.length] = 0;
chunk = chunkWithNull;
}
// tslint:disable-next-line:prefer-for-of
for (let i = 0; i < chunk.length; i++) {
const byte = chunk[i];
this._onByte(byte);
}
}
// The following implements a simple Rec Descent Parser.
// The grammar is simple and just one byte tells what should be the next state
_collectFrame(byte) {
if (byte === NULL) {
// Ignore
return;
}
if (byte === CR) {
// Ignore CR
return;
}
if (byte === LF) {
// Incoming Ping
this.onIncomingPing();
return;
}
this._onByte = this._collectCommand;
this._reinjectByte(byte);
}
_collectCommand(byte) {
if (byte === CR) {
// Ignore CR
return;
}
if (byte === LF) {
this._results.command = this._consumeTokenAsUTF8();
this._onByte = this._collectHeaders;
return;
}
this._consumeByte(byte);
}
_collectHeaders(byte) {
if (byte === CR) {
// Ignore CR
return;
}
if (byte === LF) {
this._setupCollectBody();
return;
}
this._onByte = this._collectHeaderKey;
this._reinjectByte(byte);
}
_reinjectByte(byte) {
this._onByte(byte);
}
_collectHeaderKey(byte) {
if (byte === COLON) {
this._headerKey = this._consumeTokenAsUTF8();
this._onByte = this._collectHeaderValue;
return;
}
this._consumeByte(byte);
}
_collectHeaderValue(byte) {
if (byte === CR) {
// Ignore CR
return;
}
if (byte === LF) {
this._results.headers.push([
this._headerKey,
this._consumeTokenAsUTF8(),
]);
this._headerKey = undefined;
this._onByte = this._collectHeaders;
return;
}
this._consumeByte(byte);
}
_setupCollectBody() {
const contentLengthHeader = this._results.headers.filter((header) => {
return header[0] === 'content-length';
})[0];
if (contentLengthHeader) {
this._bodyBytesRemaining = parseInt(contentLengthHeader[1], 10);
this._onByte = this._collectBodyFixedSize;
}
else {
this._onByte = this._collectBodyNullTerminated;
}
}
_collectBodyNullTerminated(byte) {
if (byte === NULL) {
this._retrievedBody();
return;
}
this._consumeByte(byte);
}
_collectBodyFixedSize(byte) {
// It is post decrement, so that we discard the trailing NULL octet
if (this._bodyBytesRemaining-- === 0) {
this._retrievedBody();
return;
}
this._consumeByte(byte);
}
_retrievedBody() {
this._results.binaryBody = this._consumeTokenAsRaw();
try {
this.onFrame(this._results);
}
catch (e) {
console.log(`Ignoring an exception thrown by a frame handler. Original exception: `, e);
}
this._initState();
}
// Rec Descent Parser helpers
_consumeByte(byte) {
this._token.push(byte);
}
_consumeTokenAsUTF8() {
return this._decoder.decode(this._consumeTokenAsRaw());
}
_consumeTokenAsRaw() {
const rawResult = new Uint8Array(this._token);
this._token = [];
return rawResult;
}
_initState() {
this._results = {
command: undefined,
headers: [],
binaryBody: undefined,
};
this._token = [];
this._headerKey = undefined;
this._onByte = this._collectFrame;
}
}
//# sourceMappingURL=parser.js.map