1 | import { IRawFrameType } from './types.js';
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | const NULL = 0;
|
7 |
|
8 |
|
9 |
|
10 | const LF = 10;
|
11 |
|
12 |
|
13 |
|
14 | const CR = 13;
|
15 |
|
16 |
|
17 |
|
18 | const COLON = 58;
|
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 | export class Parser {
|
63 | private readonly _encoder = new TextEncoder();
|
64 | private readonly _decoder = new TextDecoder();
|
65 |
|
66 |
|
67 | private _results: IRawFrameType;
|
68 |
|
69 | private _token: number[] = [];
|
70 | private _headerKey: string | undefined;
|
71 | private _bodyBytesRemaining: number | undefined;
|
72 |
|
73 |
|
74 | private _onByte: (byte: number) => void;
|
75 |
|
76 | public constructor(
|
77 | public onFrame: (rawFrame: IRawFrameType) => void,
|
78 | public onIncomingPing: () => void
|
79 | ) {
|
80 | this._initState();
|
81 | }
|
82 |
|
83 | public parseChunk(
|
84 | segment: string | ArrayBuffer,
|
85 | appendMissingNULLonIncoming: boolean = false
|
86 | ) {
|
87 | let chunk: Uint8Array;
|
88 |
|
89 | if (typeof segment === 'string') {
|
90 | chunk = this._encoder.encode(segment);
|
91 | } else {
|
92 | chunk = new Uint8Array(segment);
|
93 | }
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 | if (appendMissingNULLonIncoming && chunk[chunk.length - 1] !== 0) {
|
100 | const chunkWithNull = new Uint8Array(chunk.length + 1);
|
101 | chunkWithNull.set(chunk, 0);
|
102 | chunkWithNull[chunk.length] = 0;
|
103 | chunk = chunkWithNull;
|
104 | }
|
105 |
|
106 |
|
107 | for (let i = 0; i < chunk.length; i++) {
|
108 | const byte = chunk[i];
|
109 | this._onByte(byte);
|
110 | }
|
111 | }
|
112 |
|
113 |
|
114 |
|
115 |
|
116 | private _collectFrame(byte: number): void {
|
117 | if (byte === NULL) {
|
118 |
|
119 | return;
|
120 | }
|
121 | if (byte === CR) {
|
122 |
|
123 | return;
|
124 | }
|
125 | if (byte === LF) {
|
126 |
|
127 | this.onIncomingPing();
|
128 | return;
|
129 | }
|
130 |
|
131 | this._onByte = this._collectCommand;
|
132 | this._reinjectByte(byte);
|
133 | }
|
134 |
|
135 | private _collectCommand(byte: number): void {
|
136 | if (byte === CR) {
|
137 |
|
138 | return;
|
139 | }
|
140 | if (byte === LF) {
|
141 | this._results.command = this._consumeTokenAsUTF8();
|
142 | this._onByte = this._collectHeaders;
|
143 | return;
|
144 | }
|
145 |
|
146 | this._consumeByte(byte);
|
147 | }
|
148 |
|
149 | private _collectHeaders(byte: number): void {
|
150 | if (byte === CR) {
|
151 |
|
152 | return;
|
153 | }
|
154 | if (byte === LF) {
|
155 | this._setupCollectBody();
|
156 | return;
|
157 | }
|
158 | this._onByte = this._collectHeaderKey;
|
159 | this._reinjectByte(byte);
|
160 | }
|
161 |
|
162 | private _reinjectByte(byte: number) {
|
163 | this._onByte(byte);
|
164 | }
|
165 |
|
166 | private _collectHeaderKey(byte: number): void {
|
167 | if (byte === COLON) {
|
168 | this._headerKey = this._consumeTokenAsUTF8();
|
169 | this._onByte = this._collectHeaderValue;
|
170 | return;
|
171 | }
|
172 | this._consumeByte(byte);
|
173 | }
|
174 |
|
175 | private _collectHeaderValue(byte: number): void {
|
176 | if (byte === CR) {
|
177 |
|
178 | return;
|
179 | }
|
180 | if (byte === LF) {
|
181 | this._results.headers.push([
|
182 | this._headerKey as string,
|
183 | this._consumeTokenAsUTF8(),
|
184 | ]);
|
185 | this._headerKey = undefined;
|
186 | this._onByte = this._collectHeaders;
|
187 | return;
|
188 | }
|
189 | this._consumeByte(byte);
|
190 | }
|
191 |
|
192 | private _setupCollectBody() {
|
193 | const contentLengthHeader = this._results.headers.filter(
|
194 | (header: [string, string]) => {
|
195 | return header[0] === 'content-length';
|
196 | }
|
197 | )[0];
|
198 |
|
199 | if (contentLengthHeader) {
|
200 | this._bodyBytesRemaining = parseInt(contentLengthHeader[1], 10);
|
201 | this._onByte = this._collectBodyFixedSize;
|
202 | } else {
|
203 | this._onByte = this._collectBodyNullTerminated;
|
204 | }
|
205 | }
|
206 |
|
207 | private _collectBodyNullTerminated(byte: number): void {
|
208 | if (byte === NULL) {
|
209 | this._retrievedBody();
|
210 | return;
|
211 | }
|
212 | this._consumeByte(byte);
|
213 | }
|
214 |
|
215 | private _collectBodyFixedSize(byte: number): void {
|
216 |
|
217 | if ((this._bodyBytesRemaining as number)-- === 0) {
|
218 | this._retrievedBody();
|
219 | return;
|
220 | }
|
221 | this._consumeByte(byte);
|
222 | }
|
223 |
|
224 | private _retrievedBody() {
|
225 | this._results.binaryBody = this._consumeTokenAsRaw();
|
226 |
|
227 | try {
|
228 | this.onFrame(this._results);
|
229 | } catch (e) {
|
230 | console.log(
|
231 | `Ignoring an exception thrown by a frame handler. Original exception: `,
|
232 | e
|
233 | );
|
234 | }
|
235 |
|
236 | this._initState();
|
237 | }
|
238 |
|
239 |
|
240 |
|
241 | private _consumeByte(byte: number) {
|
242 | this._token.push(byte);
|
243 | }
|
244 |
|
245 | private _consumeTokenAsUTF8() {
|
246 | return this._decoder.decode(this._consumeTokenAsRaw());
|
247 | }
|
248 |
|
249 | private _consumeTokenAsRaw() {
|
250 | const rawResult = new Uint8Array(this._token);
|
251 | this._token = [];
|
252 | return rawResult;
|
253 | }
|
254 |
|
255 | private _initState() {
|
256 | this._results = {
|
257 | command: undefined,
|
258 | headers: [],
|
259 | binaryBody: undefined,
|
260 | };
|
261 |
|
262 | this._token = [];
|
263 | this._headerKey = undefined;
|
264 |
|
265 | this._onByte = this._collectFrame;
|
266 | }
|
267 | }
|