split-frames 2.5.0 → 2.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +13 -13
- package/README.md +268 -263
- package/lib/cjs/SplitFrames.d.ts +52 -57
- package/lib/cjs/SplitFrames.js +400 -384
- package/lib/cjs/checkers/checkTagsValidity.d.ts +2 -3
- package/lib/cjs/checkers/checkTagsValidity.js +22 -18
- package/lib/cjs/checkers/initData.d.ts +2 -2
- package/lib/cjs/checkers/initData.js +94 -85
- package/lib/cjs/main.cjs +8 -8
- package/lib/cjs/main.d.cts +2 -2
- package/package.json +111 -96
package/lib/cjs/SplitFrames.js
CHANGED
|
@@ -1,384 +1,400 @@
|
|
|
1
|
-
"use strict";
|
|
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
|
-
if (
|
|
64
|
-
this.
|
|
65
|
-
}
|
|
66
|
-
else {
|
|
67
|
-
this.
|
|
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
|
-
this.
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
this.
|
|
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
|
-
if (
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
this.
|
|
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
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
//
|
|
246
|
-
if (
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
}
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
return
|
|
325
|
-
}
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
if (
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
//
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
eslint-disable no-unmodified-loop-condition
|
|
4
|
+
*/
|
|
5
|
+
// => no-unmodified-loop-condition is disabled to allow data generation in a loop (_digestStartAndEnd)
|
|
6
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
7
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
8
|
+
};
|
|
9
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
+
// deps
|
|
11
|
+
// natives
|
|
12
|
+
const node_stream_1 = require("node:stream");
|
|
13
|
+
// locals
|
|
14
|
+
const initData_1 = __importDefault(require("./checkers/initData"));
|
|
15
|
+
// module
|
|
16
|
+
class SplitFrames extends node_stream_1.Transform {
|
|
17
|
+
// attributes
|
|
18
|
+
// protected
|
|
19
|
+
_frame;
|
|
20
|
+
_digesting;
|
|
21
|
+
_timeoutDigest;
|
|
22
|
+
// from params
|
|
23
|
+
_startWith;
|
|
24
|
+
_startTimeout;
|
|
25
|
+
_endWith;
|
|
26
|
+
_escapeWith;
|
|
27
|
+
_escaped;
|
|
28
|
+
_specifics;
|
|
29
|
+
_controlBits;
|
|
30
|
+
// dynamic digester
|
|
31
|
+
_digester;
|
|
32
|
+
// constructor
|
|
33
|
+
constructor(options) {
|
|
34
|
+
super();
|
|
35
|
+
this._frame = Buffer.from([]);
|
|
36
|
+
this._digesting = false;
|
|
37
|
+
this._timeoutDigest = null;
|
|
38
|
+
// from params
|
|
39
|
+
const validatedOptions = (0, initData_1.default)(options);
|
|
40
|
+
this._startWith = validatedOptions.startWith;
|
|
41
|
+
this._startTimeout = validatedOptions.startTimeout;
|
|
42
|
+
this._endWith = validatedOptions.endWith;
|
|
43
|
+
this._escapeWith = validatedOptions.escapeWith;
|
|
44
|
+
this._escaped = validatedOptions.escaped;
|
|
45
|
+
this._specifics = validatedOptions.specifics;
|
|
46
|
+
this._controlBits = validatedOptions.controlBits;
|
|
47
|
+
// dynamic digester
|
|
48
|
+
this._digester = this._chooseDigester();
|
|
49
|
+
}
|
|
50
|
+
// methods
|
|
51
|
+
// public
|
|
52
|
+
_transform(chunk, enc, cb) {
|
|
53
|
+
if (chunk.length) {
|
|
54
|
+
this._frame = Buffer.concat([this._frame, chunk]);
|
|
55
|
+
if (!this._digesting) {
|
|
56
|
+
this._digest(enc);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return cb();
|
|
60
|
+
}
|
|
61
|
+
// protected
|
|
62
|
+
_chooseDigester() {
|
|
63
|
+
if (undefined === this._startWith && undefined === this._endWith) {
|
|
64
|
+
return this._digestNoStartNoEnd.bind(this);
|
|
65
|
+
}
|
|
66
|
+
else if (undefined !== this._startWith && undefined === this._endWith) {
|
|
67
|
+
return this._digestStartOnly.bind(this);
|
|
68
|
+
}
|
|
69
|
+
else if (undefined === this._startWith && undefined !== this._endWith) {
|
|
70
|
+
return this._digestEndOnly.bind(this);
|
|
71
|
+
}
|
|
72
|
+
return this._digestStartAndEnd.bind(this);
|
|
73
|
+
}
|
|
74
|
+
_digest(enc) {
|
|
75
|
+
if (!this._frame.length) {
|
|
76
|
+
this._digesting = false;
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
this._digesting = true;
|
|
80
|
+
try {
|
|
81
|
+
this._digester(enc);
|
|
82
|
+
}
|
|
83
|
+
catch (e) {
|
|
84
|
+
this.emit("error", e);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
// private
|
|
89
|
+
// digesters
|
|
90
|
+
_digestNoStartNoEnd() {
|
|
91
|
+
const KEYS = Object.keys(this._specifics);
|
|
92
|
+
if (KEYS.length) {
|
|
93
|
+
KEYS.forEach((key) => {
|
|
94
|
+
const size = "object" === typeof this._specifics[key] && this._specifics[key] instanceof Buffer ? this._specifics[key].length : 1;
|
|
95
|
+
for (let foundAt = this._searchTags(this._specifics[key]); -1 < foundAt; foundAt = this._searchTags(this._specifics[key])) {
|
|
96
|
+
this.emit(key);
|
|
97
|
+
this._frame = Buffer.from(Buffer.concat([
|
|
98
|
+
this._frame.slice(0, foundAt),
|
|
99
|
+
this._frame.slice(foundAt + size, this._frame.length)
|
|
100
|
+
]));
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
this.push(Buffer.from(this._frame));
|
|
105
|
+
this._frame = Buffer.from([]);
|
|
106
|
+
this._digesting = false;
|
|
107
|
+
}
|
|
108
|
+
_digestStartOnly(enc) {
|
|
109
|
+
if (this._timeoutDigest) {
|
|
110
|
+
clearTimeout(this._timeoutDigest);
|
|
111
|
+
this._timeoutDigest = null;
|
|
112
|
+
}
|
|
113
|
+
const firstStart = this._searchFirstStart();
|
|
114
|
+
// no start tag detected
|
|
115
|
+
if (!firstStart) {
|
|
116
|
+
this._digesting = false;
|
|
117
|
+
}
|
|
118
|
+
// first start tag detected
|
|
119
|
+
else {
|
|
120
|
+
const nextStartAt = this._searchTags(this._startWith, firstStart.end);
|
|
121
|
+
// second start tag not detected
|
|
122
|
+
if (-1 >= nextStartAt) {
|
|
123
|
+
// remove useless bits
|
|
124
|
+
this._frame = 0 < firstStart.start ? Buffer.from(this._frame.slice(firstStart.start, this._frame.length)) : this._frame;
|
|
125
|
+
this._digesting = false;
|
|
126
|
+
this._timeoutDigest = setTimeout(() => {
|
|
127
|
+
clearTimeout(this._timeoutDigest);
|
|
128
|
+
this._timeoutDigest = null;
|
|
129
|
+
this.push(Buffer.from(this._frame.slice(firstStart.start, this._frame.length)));
|
|
130
|
+
this._frame = Buffer.from([]);
|
|
131
|
+
this._digesting = false;
|
|
132
|
+
}, this._startTimeout);
|
|
133
|
+
}
|
|
134
|
+
// second start tag detected
|
|
135
|
+
else {
|
|
136
|
+
// extract first frame
|
|
137
|
+
this.push(Buffer.from(this._frame.slice(firstStart.start, nextStartAt)));
|
|
138
|
+
// remove extracted frame from main one
|
|
139
|
+
this._frame = Buffer.from(this._frame.slice(nextStartAt, this._frame.length));
|
|
140
|
+
// second full frame detected (force minimal data length)
|
|
141
|
+
if (-1 < this._searchTags(this._startWith, 1)) {
|
|
142
|
+
this._digest(enc);
|
|
143
|
+
}
|
|
144
|
+
else {
|
|
145
|
+
this._digesting = false;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
_digestEndOnly(enc) {
|
|
151
|
+
this._extractSpecifics();
|
|
152
|
+
const firstEnd = this._searchFirstEnd(0);
|
|
153
|
+
// no end tag detected
|
|
154
|
+
if (!firstEnd) {
|
|
155
|
+
this._digesting = false;
|
|
156
|
+
}
|
|
157
|
+
// first end tag detected
|
|
158
|
+
else {
|
|
159
|
+
// extract first frame
|
|
160
|
+
this.push(Buffer.from(this._frame.slice(0, firstEnd.end)));
|
|
161
|
+
// remove extracted frame from main one
|
|
162
|
+
this._frame = Buffer.from(this._frame.slice(firstEnd.end, this._frame.length));
|
|
163
|
+
// second full frame detected
|
|
164
|
+
if (-1 < this._searchTags(this._endWith)) {
|
|
165
|
+
this._digest(enc);
|
|
166
|
+
}
|
|
167
|
+
else {
|
|
168
|
+
this._digesting = false;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
_extractSpecifics() {
|
|
173
|
+
let found = false;
|
|
174
|
+
Object.keys(this._specifics).forEach((key) => {
|
|
175
|
+
if (0 === this._frame.indexOf(this._specifics[key])) {
|
|
176
|
+
found = true;
|
|
177
|
+
const size = "object" === typeof this._specifics[key] && this._specifics[key] instanceof Buffer
|
|
178
|
+
? this._specifics[key].length
|
|
179
|
+
: 1;
|
|
180
|
+
this.emit(key);
|
|
181
|
+
this._frame = Buffer.from(this._frame.slice(size, this._frame.length));
|
|
182
|
+
}
|
|
183
|
+
});
|
|
184
|
+
// disabled due to malfunction
|
|
185
|
+
if (found) { // eslint-disable-line @typescript-eslint/no-unnecessary-condition
|
|
186
|
+
this._extractSpecifics();
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
_digestStartAndEnd(enc) {
|
|
190
|
+
const KEYS = Object.keys(this._specifics);
|
|
191
|
+
if (KEYS.length) {
|
|
192
|
+
KEYS.forEach((key) => {
|
|
193
|
+
const firstStart = this._searchFirstStart();
|
|
194
|
+
const size = "object" === typeof this._specifics[key] && this._specifics[key] instanceof Buffer ? this._specifics[key].length : 1;
|
|
195
|
+
for (let foundAt = this._searchTags(this._specifics[key]), i = 0; -1 < foundAt && (!firstStart || foundAt < firstStart.start - i); foundAt = this._searchTags(this._specifics[key]), i += size) {
|
|
196
|
+
this.emit(key);
|
|
197
|
+
this._frame = Buffer.from(Buffer.concat([
|
|
198
|
+
this._frame.slice(0, foundAt),
|
|
199
|
+
this._frame.slice(foundAt + size, this._frame.length)
|
|
200
|
+
]));
|
|
201
|
+
}
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
const firstStart = this._searchFirstStart();
|
|
205
|
+
// no start tag detected
|
|
206
|
+
if (!firstStart) {
|
|
207
|
+
this._digesting = false;
|
|
208
|
+
}
|
|
209
|
+
// start tag detected
|
|
210
|
+
else {
|
|
211
|
+
const firstEnd = this._searchFirstEnd(firstStart.end);
|
|
212
|
+
// end tag not detected
|
|
213
|
+
if (!firstEnd) {
|
|
214
|
+
// remove useless bits
|
|
215
|
+
this._frame = 0 < firstStart.start ? Buffer.from(this._frame.slice(firstStart.start, this._frame.length)) : this._frame;
|
|
216
|
+
this._digesting = false;
|
|
217
|
+
}
|
|
218
|
+
// end tag detected
|
|
219
|
+
else {
|
|
220
|
+
this.push(Buffer.from(this._frame.slice(firstStart.start, firstEnd.end)));
|
|
221
|
+
// remove extracted frame from main one
|
|
222
|
+
this._frame = Buffer.from(this._frame.slice(firstEnd.end, this._frame.length));
|
|
223
|
+
// search specifics
|
|
224
|
+
let foundSpecific = false;
|
|
225
|
+
for (let i = 0; i < KEYS.length; ++i) {
|
|
226
|
+
if (-1 < this._searchTags(this._specifics[KEYS[i]])) {
|
|
227
|
+
foundSpecific = true;
|
|
228
|
+
break;
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
// specific detected or second start detected (force minimal data length)
|
|
232
|
+
if (foundSpecific || -1 < this._searchTags(this._startWith, 1)) {
|
|
233
|
+
this._digest(enc);
|
|
234
|
+
}
|
|
235
|
+
else {
|
|
236
|
+
this._digesting = false;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
// search tag
|
|
242
|
+
_searchFirstTag(tag, beginAt = 0) {
|
|
243
|
+
let startAt = -1;
|
|
244
|
+
let endAt = -1;
|
|
245
|
+
// number
|
|
246
|
+
if ("number" === typeof tag) {
|
|
247
|
+
const find = this._searchFirstNumberTag(tag, beginAt);
|
|
248
|
+
startAt = find.start;
|
|
249
|
+
endAt = find.end;
|
|
250
|
+
}
|
|
251
|
+
// Buffer | Array
|
|
252
|
+
else if ("object" === typeof tag) {
|
|
253
|
+
const find = this._searchFirstBufferOrArrayTag(tag, beginAt);
|
|
254
|
+
startAt = find.start;
|
|
255
|
+
endAt = find.end;
|
|
256
|
+
}
|
|
257
|
+
// not found
|
|
258
|
+
if (-1 >= startAt || -1 >= endAt) {
|
|
259
|
+
return undefined;
|
|
260
|
+
}
|
|
261
|
+
// escaped
|
|
262
|
+
else if (0 < startAt
|
|
263
|
+
&& 1 === endAt - startAt // must be a number to be escaped
|
|
264
|
+
&& this._escaped.includes(this._frame[startAt])
|
|
265
|
+
&& this._frame[startAt - 1] === this._escapeWith) {
|
|
266
|
+
// escaper not escaped
|
|
267
|
+
if (!this._escaped.includes(this._escapeWith) || 1 >= startAt || this._frame[startAt - 2] !== this._escapeWith) {
|
|
268
|
+
return this._searchFirstTag(tag, endAt + 1);
|
|
269
|
+
}
|
|
270
|
+
else {
|
|
271
|
+
return {
|
|
272
|
+
"start": startAt,
|
|
273
|
+
"end": endAt
|
|
274
|
+
};
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
// not escaped
|
|
278
|
+
else {
|
|
279
|
+
return {
|
|
280
|
+
"start": startAt,
|
|
281
|
+
"end": endAt
|
|
282
|
+
};
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
_searchFirstNumberTag(tag, beginAt) {
|
|
286
|
+
const startAt = this._frame.indexOf(tag, beginAt);
|
|
287
|
+
return {
|
|
288
|
+
"start": startAt,
|
|
289
|
+
"end": startAt + 1
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
_searchFirstBufferOrArrayTag(tag, beginAt) {
|
|
293
|
+
let startAt = -1;
|
|
294
|
+
let endAt = -1;
|
|
295
|
+
// Buffer
|
|
296
|
+
if (tag instanceof Buffer) {
|
|
297
|
+
startAt = this._frame.indexOf(tag, beginAt);
|
|
298
|
+
if (-1 < startAt) {
|
|
299
|
+
endAt = startAt + tag.length;
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
// Array
|
|
303
|
+
else if (tag instanceof Array) {
|
|
304
|
+
for (let i = 0; i < tag.length; ++i) {
|
|
305
|
+
// number | Buffer
|
|
306
|
+
if ("number" === typeof tag[i]
|
|
307
|
+
|| ("object" === typeof tag[i] && tag[i] instanceof Buffer)) {
|
|
308
|
+
const _startAt = this._frame.indexOf(tag[i], beginAt);
|
|
309
|
+
if (-1 < _startAt && (-1 >= startAt || _startAt < startAt)) {
|
|
310
|
+
startAt = _startAt;
|
|
311
|
+
if (-1 < startAt) {
|
|
312
|
+
endAt = startAt + ("number" === typeof tag[i] ? 1 : tag[i].length);
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
return {
|
|
319
|
+
"start": startAt,
|
|
320
|
+
"end": endAt
|
|
321
|
+
};
|
|
322
|
+
}
|
|
323
|
+
_searchFirstStart() {
|
|
324
|
+
return this._searchFirstTag(this._startWith);
|
|
325
|
+
}
|
|
326
|
+
_searchFirstEnd(startEndAt) {
|
|
327
|
+
const firstTag = this._searchFirstTag(this._endWith, startEndAt);
|
|
328
|
+
if (firstTag) {
|
|
329
|
+
if ("end+1" === this._controlBits) {
|
|
330
|
+
firstTag.end += 1;
|
|
331
|
+
}
|
|
332
|
+
else if ("end+2" === this._controlBits) {
|
|
333
|
+
firstTag.end += 2;
|
|
334
|
+
}
|
|
335
|
+
// end tag detected but without valid control bits length
|
|
336
|
+
if (firstTag.end > this._frame.length) {
|
|
337
|
+
return undefined;
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
return firstTag;
|
|
341
|
+
}
|
|
342
|
+
_searchTags(tags, beginAt = 0) {
|
|
343
|
+
// number
|
|
344
|
+
if ("number" === typeof tags) {
|
|
345
|
+
return this._searchNumberTags(tags, beginAt);
|
|
346
|
+
}
|
|
347
|
+
// Buffer | Array
|
|
348
|
+
else if ("object" === typeof tags) {
|
|
349
|
+
return this._searchBufferOrArrayTags(tags, beginAt);
|
|
350
|
+
}
|
|
351
|
+
return -1;
|
|
352
|
+
}
|
|
353
|
+
_searchNumberTags(tags, beginAt) {
|
|
354
|
+
const foundAt = this._frame.indexOf(tags, beginAt);
|
|
355
|
+
// escaped
|
|
356
|
+
if (0 < foundAt
|
|
357
|
+
&& this._escaped.includes(this._frame[foundAt])
|
|
358
|
+
&& this._frame[foundAt - 1] === this._escapeWith) {
|
|
359
|
+
// escaper not escaped
|
|
360
|
+
if (!this._escaped.includes(this._escapeWith) || 1 >= foundAt || this._frame[foundAt - 2] !== this._escapeWith) {
|
|
361
|
+
return this._searchTags(tags, foundAt + 1);
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
return foundAt;
|
|
365
|
+
}
|
|
366
|
+
_searchBufferOrArrayTags(tags, beginAt) {
|
|
367
|
+
// Buffer
|
|
368
|
+
if (tags instanceof Buffer) {
|
|
369
|
+
return this._frame.indexOf(tags, beginAt);
|
|
370
|
+
}
|
|
371
|
+
// Array
|
|
372
|
+
else if (tags instanceof Array) {
|
|
373
|
+
let foundAt = -1;
|
|
374
|
+
let type = "undefined";
|
|
375
|
+
for (let i = 0; i < tags.length; ++i) {
|
|
376
|
+
// number | Buffer
|
|
377
|
+
if ("number" === typeof tags[i]
|
|
378
|
+
|| ("object" === typeof tags[i] && tags[i] instanceof Buffer)) {
|
|
379
|
+
const _foundAt = this._frame.indexOf(tags[i], beginAt);
|
|
380
|
+
if (-1 < _foundAt && (-1 >= foundAt || _foundAt < foundAt)) {
|
|
381
|
+
foundAt = _foundAt;
|
|
382
|
+
type = typeof tags[i];
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
// escaped
|
|
387
|
+
if ("number" === type && 0 < foundAt
|
|
388
|
+
&& this._escaped.includes(this._frame[foundAt])
|
|
389
|
+
&& this._frame[foundAt - 1] === this._escapeWith) {
|
|
390
|
+
// escaper not escaped
|
|
391
|
+
if (!this._escaped.includes(this._escapeWith) || 1 >= foundAt || this._frame[foundAt - 2] !== this._escapeWith) {
|
|
392
|
+
foundAt = this._searchTags(tags, foundAt + 1);
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
return foundAt;
|
|
396
|
+
}
|
|
397
|
+
return -1;
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
exports.default = SplitFrames;
|