split-frames 2.4.4 → 2.4.5

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/test/2_endOnly.js DELETED
@@ -1,297 +0,0 @@
1
- /*
2
- eslint no-new: 0
3
- */
4
-
5
- "use strict";
6
-
7
- // deps
8
-
9
- const assert = require("assert");
10
- const SplitFrames = require(require("path").join(__dirname, "..", "lib", "main.js"));
11
-
12
- // consts
13
-
14
- const ETX = 0x03;
15
- const DLE = 0x10;
16
-
17
- // module
18
-
19
- describe("end only", () => {
20
-
21
- it("should split frame with no end", () => {
22
-
23
- return new Promise((resolve, reject) => {
24
-
25
- const splitter = new SplitFrames({
26
- "endWith": ETX
27
- }).once("error", reject).once("data", (chunk) => {
28
-
29
- assert.strictEqual(typeof chunk, "object", "The chunk is not an object");
30
- assert.strictEqual(chunk instanceof Buffer, true, "The chunk is not a Buffer");
31
- assert.deepStrictEqual(chunk, Buffer.from([ 0x24, 0x25, 0x26, ETX ]), "The chunk is not as expected");
32
-
33
- resolve();
34
-
35
- });
36
-
37
- // tested frame
38
- splitter.write(Buffer.from([ 0x24, 0x25, 0x26 ]));
39
- // close tested frame
40
- splitter.write(Buffer.from([ ETX ]));
41
-
42
- });
43
-
44
- });
45
-
46
- it("should split frame with one end", () => {
47
-
48
- return new Promise((resolve, reject) => {
49
-
50
- new SplitFrames({
51
- "endWith": ETX
52
- }).once("error", reject).once("data", (chunk) => {
53
-
54
- assert.strictEqual(typeof chunk, "object", "The chunk is not an object");
55
- assert.strictEqual(chunk instanceof Buffer, true, "The chunk is not a Buffer");
56
- assert.deepStrictEqual(chunk, Buffer.from([ 0x24, 0x25, 0x26, ETX ]), "The chunk is not as expected");
57
-
58
- resolve();
59
-
60
- // tested frame
61
- }).write(Buffer.from([ 0x24, 0x25, 0x26, ETX, 0x24, 0x25, 0x26 ]));
62
-
63
- });
64
-
65
- });
66
-
67
- it("should split frame with two end", () => {
68
-
69
- return new Promise((resolve, reject) => {
70
-
71
- let dataCount = 0;
72
-
73
- new SplitFrames({
74
- "endWith": ETX
75
- }).once("error", reject).on("data", (chunk) => {
76
-
77
- ++dataCount;
78
-
79
- assert.strictEqual(typeof chunk, "object", "The chunk is not an object");
80
- assert.strictEqual(chunk instanceof Buffer, true, "The chunk is not a Buffer");
81
-
82
- if (1 === dataCount) {
83
- assert.deepStrictEqual(chunk, Buffer.from([ 0x24, 0x25, 0x26, ETX ]), "The chunk is not as expected");
84
- }
85
- else if (2 === dataCount) {
86
- assert.deepStrictEqual(chunk, Buffer.from([ 0x24, 0x25, ETX ]), "The chunk is not as expected");
87
- resolve();
88
- }
89
-
90
- // tested frame
91
- }).write(Buffer.from([ 0x24, 0x25, 0x26, ETX, 0x24, 0x25, ETX, 0x24, 0x25, 0x26 ]));
92
-
93
- });
94
-
95
- });
96
-
97
- it("should split frame with three end", () => {
98
-
99
- return new Promise((resolve, reject) => {
100
-
101
- let dataCount = 0;
102
-
103
- new SplitFrames({
104
- "endWith": ETX
105
- }).once("error", reject).on("data", (chunk) => {
106
-
107
- ++dataCount;
108
-
109
- assert.strictEqual(typeof chunk, "object", "The chunk is not an object");
110
- assert.strictEqual(chunk instanceof Buffer, true, "The chunk is not a Buffer");
111
-
112
- if (1 === dataCount) {
113
- assert.deepStrictEqual(chunk, Buffer.from([ 0x24, 0x25, 0x26, ETX ]), "The chunk is not as expected");
114
- }
115
- else if (2 === dataCount) {
116
- assert.deepStrictEqual(chunk, Buffer.from([ 0x24, 0x25, ETX ]), "The chunk is not as expected");
117
- }
118
- else if (3 === dataCount) {
119
- assert.deepStrictEqual(chunk, Buffer.from([ 0x24, 0x25, 0x26, ETX ]), "The chunk is not as expected");
120
- resolve();
121
- }
122
-
123
- // tested frame
124
- }).write(Buffer.from([ 0x24, 0x25, 0x26, ETX, 0x24, 0x25, ETX, 0x24, 0x25, 0x26, ETX ]));
125
-
126
- });
127
-
128
- });
129
-
130
- it("should split frame with two bits end", () => {
131
-
132
- return new Promise((resolve, reject) => {
133
-
134
- let dataCount = 0;
135
-
136
- new SplitFrames({
137
- "endWith": Buffer.from([ DLE, ETX ])
138
- }).once("error", reject).on("data", (chunk) => {
139
-
140
- ++dataCount;
141
-
142
- assert.strictEqual(typeof chunk, "object", "The chunk is not an object");
143
- assert.strictEqual(chunk instanceof Buffer, true, "The chunk is not a Buffer");
144
-
145
- if (1 === dataCount) {
146
- assert.deepStrictEqual(chunk, Buffer.from([ 0x24, 0x25, 0x26, DLE, ETX ]), "The chunk is not as expected");
147
- }
148
- else if (2 === dataCount) {
149
- assert.deepStrictEqual(chunk, Buffer.from([ 0x24, 0x25, DLE, ETX ]), "The chunk is not as expected");
150
- }
151
- else if (3 === dataCount) {
152
- assert.deepStrictEqual(chunk, Buffer.from([ 0x24, 0x25, 0x26, DLE, ETX ]), "The chunk is not as expected");
153
- resolve();
154
- }
155
-
156
- // tested frame
157
- }).write(Buffer.from([ 0x24, 0x25, 0x26, DLE, ETX, 0x24, 0x25, DLE, ETX, 0x24, 0x25, 0x26, DLE, ETX ]));
158
-
159
- });
160
-
161
- });
162
-
163
- it("should split frame with array of two bits end", () => {
164
-
165
- return new Promise((resolve, reject) => {
166
-
167
- let dataCount = 0;
168
-
169
- new SplitFrames({
170
- "endWith": [ Buffer.from([ DLE, ETX ]) ]
171
- }).once("error", reject).on("data", (chunk) => {
172
-
173
- ++dataCount;
174
-
175
- assert.strictEqual(typeof chunk, "object", "The chunk is not an object");
176
- assert.strictEqual(chunk instanceof Buffer, true, "The chunk is not a Buffer");
177
-
178
- if (1 === dataCount) {
179
- assert.deepStrictEqual(chunk, Buffer.from([ 0x24, 0x25, 0x26, DLE, ETX ]), "The chunk is not as expected");
180
- }
181
- else if (2 === dataCount) {
182
- assert.deepStrictEqual(chunk, Buffer.from([ 0x24, 0x25, DLE, ETX ]), "The chunk is not as expected");
183
- }
184
- else if (3 === dataCount) {
185
- assert.deepStrictEqual(chunk, Buffer.from([ 0x24, 0x25, 0x26, DLE, ETX ]), "The chunk is not as expected");
186
- resolve();
187
- }
188
-
189
- // tested frame
190
- }).write(Buffer.from([ 0x24, 0x25, 0x26, DLE, ETX, 0x24, 0x25, DLE, ETX, 0x24, 0x25, 0x26, DLE, ETX ]));
191
-
192
- });
193
-
194
- });
195
-
196
- it("should split frame with escaped data", () => {
197
-
198
- return new Promise((resolve, reject) => {
199
-
200
- new SplitFrames({
201
- "endWith": ETX,
202
- "escapeWith": DLE,
203
- "escaped": [ DLE, ETX ]
204
- }).once("error", reject).once("data", (chunk) => {
205
-
206
- assert.strictEqual(typeof chunk, "object", "The chunk is not an object");
207
- assert.strictEqual(chunk instanceof Buffer, true, "The chunk is not a Buffer");
208
- assert.deepStrictEqual(chunk,
209
- Buffer.from([ 0x24, DLE, ETX, 0x04, 0x05, DLE, DLE, 0x06, 0x07, 0x08, DLE, DLE, ETX ]),
210
- "The chunk is not as expected"
211
- );
212
-
213
- resolve();
214
-
215
-
216
- // tested frame
217
- }).write(Buffer.from([ 0x24, DLE, ETX, 0x04, 0x05, DLE, DLE, 0x06, 0x07, 0x08, DLE, DLE, ETX ]));
218
-
219
- });
220
-
221
- });
222
-
223
- describe("control bits", () => {
224
-
225
- it("should end with no control bit", () => {
226
-
227
- return new Promise((resolve) => {
228
-
229
- new SplitFrames({
230
- "endWith": ETX,
231
- "controlBits": "none"
232
- }).on("data", (chunk) => {
233
-
234
- assert.strictEqual(typeof chunk, "object", "The chunk is not an object");
235
- assert.strictEqual(chunk instanceof Buffer, true, "The chunk is not a Buffer");
236
- assert.deepStrictEqual(chunk, Buffer.from([ 0x20, 0x21, 0x22, 0x24, ETX ]), "The chunk is not as expected");
237
-
238
- resolve();
239
-
240
- // tested frame
241
- }).write(Buffer.from([ 0x20, 0x21, 0x22, 0x24, ETX, 0x07, 0x24 ]));
242
-
243
- });
244
-
245
- });
246
-
247
- it("should end with one control bit", () => {
248
-
249
- return new Promise((resolve) => {
250
-
251
- new SplitFrames({
252
- "endWith": ETX,
253
- "controlBits": "end+1"
254
- }).on("data", (chunk) => {
255
-
256
- assert.strictEqual(typeof chunk, "object", "The chunk is not an object");
257
- assert.strictEqual(chunk instanceof Buffer, true, "The chunk is not a Buffer");
258
- assert.deepStrictEqual(chunk, Buffer.from([ 0x20, 0x21, 0x22, 0x24, ETX, 0x07 ]), "The chunk is not as expected");
259
-
260
- resolve();
261
-
262
- // tested frame
263
- }).write(Buffer.from([ 0x20, 0x21, 0x22, 0x24, ETX, 0x07, 0x24 ]));
264
-
265
- });
266
-
267
- });
268
-
269
- it("should end with two control bits", () => {
270
-
271
- return new Promise((resolve) => {
272
-
273
- const splitter = new SplitFrames({
274
- "endWith": ETX,
275
- "controlBits": "end+2"
276
- }).on("data", (chunk) => {
277
-
278
- assert.strictEqual(typeof chunk, "object", "The chunk is not an object");
279
- assert.strictEqual(chunk instanceof Buffer, true, "The chunk is not a Buffer");
280
- assert.deepStrictEqual(chunk, Buffer.from([ 0x20, 0x21, 0x22, 0x24, ETX, 0x07, 0x24 ]), "The chunk is not as expected");
281
-
282
- resolve();
283
-
284
- });
285
-
286
- // tested frame
287
- splitter.write(Buffer.from([ 0x20, 0x21, 0x22, 0x24, ETX, 0x07 ]));
288
- // close tested frame
289
- splitter.write(Buffer.from([ 0x24 ]));
290
-
291
- });
292
-
293
- });
294
-
295
- });
296
-
297
- });
@@ -1,345 +0,0 @@
1
- /*
2
- eslint no-new: 0
3
- */
4
-
5
- "use strict";
6
-
7
- // deps
8
-
9
- const assert = require("assert");
10
- const SplitFrames = require(require("path").join(__dirname, "..", "lib", "main.js"));
11
-
12
- // consts
13
-
14
- const STX = 0x02;
15
- const STX2 = 0x82;
16
- const DLE = 0x10;
17
-
18
- // module
19
-
20
- describe("start only", () => {
21
-
22
- it("should split frame with no start", () => {
23
-
24
- return new Promise((resolve, reject) => {
25
-
26
- const splitter = new SplitFrames({
27
- "startWith": STX
28
- }).once("error", reject).once("data", (chunk) => {
29
-
30
- assert.strictEqual(typeof chunk, "object", "The chunk is not an object");
31
- assert.strictEqual(chunk instanceof Buffer, true, "The chunk is not a Buffer");
32
- assert.deepStrictEqual(chunk, Buffer.from([ STX, 0x24 ]), "The chunk is not as expected");
33
-
34
- resolve();
35
-
36
- });
37
-
38
- // tested frame
39
- splitter.write(Buffer.from([ 0x24, 0x25, 0x26 ]));
40
- // valid frame to close tested frame
41
- splitter.write(Buffer.from([ STX, 0x24 ]));
42
- splitter.write(Buffer.from([ STX ]));
43
-
44
- });
45
-
46
- });
47
-
48
- it("should split frame with no second start bit", () => {
49
-
50
- return new Promise((resolve, reject) => {
51
-
52
- const splitter = new SplitFrames({
53
- "startWith": STX,
54
- "startTimeout": 200
55
- }).once("error", reject).once("data", (chunk) => {
56
-
57
- assert.strictEqual(typeof chunk, "object", "The chunk is not an object");
58
- assert.strictEqual(chunk instanceof Buffer, true, "The chunk is not a Buffer");
59
- assert.deepStrictEqual(chunk, Buffer.from([ STX, 0x24 ]), "The chunk is not as expected");
60
-
61
- resolve();
62
-
63
- });
64
-
65
- // tested frame
66
- splitter.write(Buffer.from([ STX, 0x24 ]));
67
-
68
- });
69
-
70
- });
71
-
72
- it("should split frame with one start", () => {
73
-
74
- return new Promise((resolve, reject) => {
75
-
76
- const splitter = new SplitFrames({
77
- "startWith": STX
78
- }).once("error", reject).once("data", (chunk) => {
79
-
80
- assert.strictEqual(typeof chunk, "object", "The chunk is not an object");
81
- assert.strictEqual(chunk instanceof Buffer, true, "The chunk is not a Buffer");
82
- assert.deepStrictEqual(chunk, Buffer.from([ STX, 0x24, 0x25, 0x26 ]), "The chunk is not as expected");
83
-
84
- resolve();
85
-
86
- });
87
-
88
- // tested frame
89
- splitter.write(Buffer.from([ 0x24, STX, 0x24, 0x25, 0x26 ]));
90
- // close tested frame
91
- splitter.write(Buffer.from([ STX ]));
92
-
93
- });
94
-
95
- });
96
-
97
- it("should split frame with two start", () => {
98
-
99
- return new Promise((resolve, reject) => {
100
-
101
- let dataCount = 0;
102
-
103
- const splitter = new SplitFrames({
104
- "startWith": STX
105
- }).once("error", reject).on("data", (chunk) => {
106
-
107
- ++dataCount;
108
-
109
- assert.strictEqual(typeof chunk, "object", "The chunk is not an object");
110
- assert.strictEqual(chunk instanceof Buffer, true, "The chunk is not a Buffer");
111
-
112
- if (1 === dataCount) {
113
- assert.deepStrictEqual(chunk, Buffer.from([ STX, 0x24, 0x25, 0x26 ]), "The chunk is not as expected");
114
- }
115
- else if (2 === dataCount) {
116
- assert.deepStrictEqual(chunk, Buffer.from([ STX, 0x24, 0x25 ]), "The chunk is not as expected");
117
- resolve();
118
- }
119
-
120
- });
121
-
122
- // tested frame
123
- splitter.write(Buffer.from([ 0x24, STX, 0x24, 0x25, 0x26, STX, 0x24, 0x25 ]));
124
- // close tested frame
125
- splitter.write(Buffer.from([ STX ]));
126
-
127
- });
128
-
129
- });
130
-
131
- it("should split frame with three start", () => {
132
-
133
- return new Promise((resolve, reject) => {
134
-
135
- let dataCount = 0;
136
-
137
- new SplitFrames({
138
- "startWith": STX
139
- }).once("error", reject).on("data", (chunk) => {
140
-
141
- ++dataCount;
142
-
143
- assert.strictEqual(typeof chunk, "object", "The chunk is not an object");
144
- assert.strictEqual(chunk instanceof Buffer, true, "The chunk is not a Buffer");
145
-
146
- if (1 === dataCount) {
147
- assert.deepStrictEqual(chunk, Buffer.from([ STX, 0x24, 0x25, 0x26 ]), "The chunk is not as expected");
148
- }
149
- else if (2 === dataCount) {
150
- assert.deepStrictEqual(chunk, Buffer.from([ STX, 0x24, 0x25 ]), "The chunk is not as expected");
151
- resolve();
152
- }
153
-
154
- // tested frame
155
- }).write(Buffer.from([ 0x24, STX, 0x24, 0x25, 0x26, STX, 0x24, 0x25, STX ]));
156
-
157
- });
158
-
159
- });
160
-
161
- it("should split frame with two bits start", () => {
162
-
163
- return new Promise((resolve, reject) => {
164
-
165
- let dataCount = 0;
166
-
167
- new SplitFrames({
168
- "startWith": Buffer.from([ DLE, STX ])
169
- }).once("error", reject).on("data", (chunk) => {
170
-
171
- ++dataCount;
172
-
173
- assert.strictEqual(typeof chunk, "object", "The chunk is not an object");
174
- assert.strictEqual(chunk instanceof Buffer, true, "The chunk is not a Buffer");
175
-
176
- if (1 === dataCount) {
177
- assert.deepStrictEqual(chunk, Buffer.from([ DLE, STX, 0x24, 0x25, 0x26 ]), "The chunk is not as expected");
178
- }
179
- else if (2 === dataCount) {
180
- assert.deepStrictEqual(chunk, Buffer.from([ DLE, STX, 0x24, 0x25 ]), "The chunk is not as expected");
181
- resolve();
182
- }
183
-
184
- // tested frame
185
- }).write(Buffer.from([ 0x24, DLE, STX, 0x24, 0x25, 0x26, DLE, STX, 0x24, 0x25, DLE, STX ]));
186
-
187
- });
188
-
189
- });
190
-
191
- it("should split frame with array of two bits start", () => {
192
-
193
- return new Promise((resolve, reject) => {
194
-
195
- let dataCount = 0;
196
-
197
- new SplitFrames({
198
- "startWith": [ Buffer.from([ DLE, STX ]) ]
199
- }).once("error", reject).on("data", (chunk) => {
200
-
201
- ++dataCount;
202
-
203
- assert.strictEqual(typeof chunk, "object", "The chunk is not an object");
204
- assert.strictEqual(chunk instanceof Buffer, true, "The chunk is not a Buffer");
205
-
206
- if (1 === dataCount) {
207
- assert.deepStrictEqual(chunk, Buffer.from([ DLE, STX, 0x24, 0x25, 0x26 ]), "The chunk is not as expected");
208
- }
209
- else if (2 === dataCount) {
210
- assert.deepStrictEqual(chunk, Buffer.from([ DLE, STX, 0x24, 0x25 ]), "The chunk is not as expected");
211
- resolve();
212
- }
213
-
214
- // tested frame
215
- }).write(Buffer.from([ 0x24, DLE, STX, 0x24, 0x25, 0x26, DLE, STX, 0x24, 0x25, DLE, STX ]));
216
-
217
- });
218
-
219
- });
220
-
221
- it("should split frame with escaped data", () => {
222
-
223
- return new Promise((resolve, reject) => {
224
-
225
- new SplitFrames({
226
- "startWith": STX,
227
- "escapeWith": DLE,
228
- "escaped": [ DLE, STX ]
229
- }).once("error", reject).once("data", (chunk) => {
230
-
231
- assert.strictEqual(typeof chunk, "object", "The chunk is not an object");
232
- assert.strictEqual(chunk instanceof Buffer, true, "The chunk is not a Buffer");
233
- assert.deepStrictEqual(chunk,
234
- Buffer.from([ STX, 0x24, DLE, STX, 0x24, 0x25, DLE, DLE, 0x24, 0x25, 0x26, DLE, DLE ]),
235
- "The chunk is not as expected"
236
- );
237
-
238
- resolve();
239
-
240
- // tested frame
241
- }).write(Buffer.from([ 0x24, STX, 0x24, DLE, STX, 0x24, 0x25, DLE, DLE, 0x24, 0x25, 0x26, DLE, DLE, STX ]));
242
-
243
- });
244
-
245
- });
246
-
247
- it("should split frame with two start and different starters", () => {
248
-
249
- return new Promise((resolve, reject) => {
250
-
251
- let dataCount = 0;
252
-
253
- new SplitFrames({
254
- "startWith": [ STX, STX2 ]
255
- }).once("error", reject).on("data", (chunk) => {
256
-
257
- ++dataCount;
258
-
259
- assert.strictEqual(typeof chunk, "object", "The chunk is not an object");
260
- assert.strictEqual(chunk instanceof Buffer, true, "The chunk is not a Buffer");
261
-
262
- if (1 === dataCount) {
263
- assert.deepStrictEqual(chunk, Buffer.from([ STX, 0x24, 0x25, 0x26 ]), "The chunk is not as expected");
264
- }
265
- else if (2 === dataCount) {
266
- assert.deepStrictEqual(chunk, Buffer.from([ STX2, 0x24, 0x25 ]), "The chunk is not as expected");
267
- resolve();
268
- }
269
-
270
- // tested frame
271
- }).write(Buffer.from([ 0x24, STX, 0x24, 0x25, 0x26, STX2, 0x24, 0x25, STX ]));
272
-
273
- });
274
-
275
- });
276
-
277
- it("should split frame with two start and different starters and escaped data", () => {
278
-
279
- return new Promise((resolve, reject) => {
280
-
281
- let dataCount = 0;
282
-
283
- new SplitFrames({
284
- "startWith": [ STX, STX2 ],
285
- "escapeWith": DLE,
286
- "escaped": [ DLE, STX, STX2 ]
287
- }).once("error", reject).on("data", (chunk) => {
288
-
289
- ++dataCount;
290
-
291
- assert.strictEqual(typeof chunk, "object", "The chunk is not an object");
292
- assert.strictEqual(chunk instanceof Buffer, true, "The chunk is not a Buffer");
293
-
294
- if (1 === dataCount) {
295
-
296
- assert.deepStrictEqual(chunk,
297
- Buffer.from([ STX, 0x24, DLE, STX, 0x25, 0x26, DLE, DLE, 0x24, 0x25, 0x26 ]),
298
- "The chunk is not as expected"
299
- );
300
-
301
- }
302
- else if (2 === dataCount) {
303
-
304
- assert.deepStrictEqual(chunk,
305
- Buffer.from([ STX2, 0x24, DLE, STX, 0x25, 0x26, DLE, DLE, 0x24, 0x25, 0x26 ]),
306
- "The chunk is not as expected"
307
- );
308
-
309
- resolve();
310
-
311
- }
312
-
313
- // tested frame
314
- }).write(Buffer.from([
315
- 0x24,
316
- STX,
317
- 0x24,
318
- DLE,
319
- STX,
320
- 0x25,
321
- 0x26,
322
- DLE,
323
- DLE,
324
- 0x24,
325
- 0x25,
326
- 0x26,
327
- STX2,
328
- 0x24,
329
- DLE,
330
- STX,
331
- 0x25,
332
- 0x26,
333
- DLE,
334
- DLE,
335
- 0x24,
336
- 0x25,
337
- 0x26,
338
- STX
339
- ]));
340
-
341
- });
342
-
343
- });
344
-
345
- });