split-frames 2.5.0 → 2.5.1

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