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