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