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.
@@ -1,363 +0,0 @@
1
- /*
2
- eslint no-implicit-globals: 0, no-bitwise: 0
3
- */
4
-
5
- "use strict";
6
-
7
- // deps
8
-
9
- const assert = require("assert");
10
- const { Readable } = require("stream");
11
-
12
- const Splitter = require(require("path").join(__dirname, "..", "lib", "main.js"));
13
-
14
- // consts
15
-
16
- const STX = 0x02;
17
- const ETX = 0x03;
18
- const DLE = 0x10;
19
- const ACK = 0x06;
20
- const WAK = 0x13;
21
- const NAK = 0x15;
22
-
23
- // private
24
-
25
- // methods
26
-
27
- /**
28
- * Compute LRC
29
- * @param {Buffer} chunk : frame to compute
30
- * @returns {number} LRC
31
- */
32
- function _computeLRC (chunk) {
33
-
34
- let lrc = 0x00;
35
-
36
- for (let i = 0; i < chunk.length; ++i) {
37
- lrc ^= chunk[i];
38
- }
39
-
40
- return lrc;
41
-
42
- }
43
-
44
- // module
45
-
46
- describe("documentation", () => {
47
-
48
- /**
49
- * Create Readable stream
50
- * @returns {Readable} stream
51
- */
52
- function createReadStream () {
53
-
54
- return new Readable({
55
- read () {
56
- // nothing to do here
57
- }
58
- });
59
-
60
- }
61
-
62
- it("should test start", () => {
63
-
64
- return new Promise((resolve, reject) => {
65
-
66
- let dataCount = 0;
67
-
68
- const stream = createReadStream();
69
-
70
- stream.pipe(new Splitter({
71
- "startWith": STX
72
- })).once("error", reject).on("data", (chunk) => {
73
-
74
- ++dataCount;
75
-
76
- assert.strictEqual(typeof chunk, "object", "The chunk is not an object");
77
- assert.strictEqual(chunk instanceof Buffer, true, "The chunk is not a Buffer");
78
-
79
- if (1 === dataCount) {
80
- assert.deepStrictEqual(chunk, Buffer.from([ STX, 0x24, 0x25, 0x26 ]), "The chunk is not as expected");
81
- }
82
- else if (2 === dataCount) {
83
- assert.deepStrictEqual(chunk, Buffer.from([ STX, 0x24, 0x25 ]), "The chunk is not as expected");
84
- resolve();
85
- }
86
-
87
- });
88
-
89
- stream.push(Buffer.from([ 0x20, STX, 0x24, 0x25, 0x26, STX, 0x24 ]));
90
- stream.push(Buffer.from([ 0x25, STX ]));
91
-
92
- });
93
-
94
- });
95
-
96
- it("should test end", () => {
97
-
98
- return new Promise((resolve, reject) => {
99
-
100
- let dataCount = 0;
101
-
102
- const stream = createReadStream();
103
-
104
- stream.pipe(new Splitter({
105
- "endWith": ETX
106
- })).once("error", reject).on("data", (chunk) => {
107
-
108
- ++dataCount;
109
-
110
- assert.strictEqual(typeof chunk, "object", "The chunk is not an object");
111
- assert.strictEqual(chunk instanceof Buffer, true, "The chunk is not a Buffer");
112
-
113
- if (1 === dataCount) {
114
- assert.deepStrictEqual(chunk, Buffer.from([ 0x24, 0x25, 0x26, ETX ]), "The chunk is not as expected");
115
- }
116
- else if (2 === dataCount) {
117
- assert.deepStrictEqual(chunk, Buffer.from([ 0x24, 0x25, ETX ]), "The chunk is not as expected");
118
- resolve();
119
- }
120
-
121
- });
122
-
123
- stream.push(Buffer.from([ 0x24, 0x25, 0x26, ETX, 0x24, 0x25 ]));
124
- stream.push(Buffer.from([ ETX ]));
125
-
126
- });
127
-
128
- });
129
-
130
- it("should test start & end", () => {
131
-
132
- return new Promise((resolve, reject) => {
133
-
134
- let dataCount = 0;
135
-
136
- const stream = createReadStream();
137
-
138
- stream.pipe(new Splitter({
139
- "startWith": STX,
140
- "endWith": ETX
141
- })).once("error", reject).on("data", (chunk) => {
142
-
143
- ++dataCount;
144
-
145
- assert.strictEqual(typeof chunk, "object", "The chunk is not an object");
146
- assert.strictEqual(chunk instanceof Buffer, true, "The chunk is not a Buffer");
147
-
148
- if (1 === dataCount) {
149
- assert.deepStrictEqual(chunk, Buffer.from([ STX, 0x24, 0x25, 0x26, ETX ]), "The chunk is not as expected");
150
- }
151
- else if (2 === dataCount) {
152
- assert.deepStrictEqual(chunk, Buffer.from([ STX, 0x24, 0x25, ETX ]), "The chunk is not as expected");
153
- resolve();
154
- }
155
-
156
- });
157
-
158
- stream.push(Buffer.from([ 0x20, STX, 0x24, 0x25, 0x26, ETX, 0x24, 0x25, STX ]));
159
- stream.push(Buffer.from([ 0x24, 0x25, ETX ]));
160
-
161
- });
162
-
163
- });
164
-
165
- it("should test start & escaped end", () => {
166
-
167
- return new Promise((resolve) => {
168
-
169
- const stream = createReadStream();
170
-
171
- stream.pipe(new Splitter({
172
- "startWith": STX,
173
- "endWith": Buffer.from([ DLE, ETX ])
174
- })).on("data", (chunk) => {
175
-
176
- assert.strictEqual(typeof chunk, "object", "The chunk is not an object");
177
- assert.strictEqual(chunk instanceof Buffer, true, "The chunk is not a Buffer");
178
- assert.deepStrictEqual(chunk, Buffer.from([ STX, 0x24, 0x25, 0x26, DLE, ETX ]), "The chunk is not as expected");
179
-
180
- resolve();
181
-
182
- });
183
-
184
- stream.push(Buffer.from([ 0x24, STX, 0x24, 0x25, 0x26, DLE, ETX, STX, 0x24, 0x25 ]));
185
-
186
- });
187
-
188
- });
189
-
190
- it("should test escaped start & end", () => {
191
-
192
- return new Promise((resolve) => {
193
-
194
- const stream = createReadStream();
195
-
196
- stream.pipe(new Splitter({
197
- "startWith": STX,
198
- "endWith": ETX,
199
- "escapeWith": DLE,
200
- "escaped": [ DLE, STX, ETX ]
201
- })).on("data", (chunk) => {
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
- assert.deepStrictEqual(chunk,
207
- Buffer.from([ STX, 0x24, DLE, STX, 0x25, 0x26, DLE, DLE, 0x27, DLE, ETX, 0x28, ETX ]),
208
- "The chunk is not as expected"
209
- );
210
-
211
- resolve();
212
-
213
- });
214
-
215
- stream.push(Buffer.from([ 0x20, STX, 0x24, DLE, STX, 0x25, 0x26 ]));
216
- stream.push(Buffer.from([ DLE, DLE, 0x27, DLE, ETX, 0x28, ETX, STX, 0x24, 0x25 ]));
217
-
218
- });
219
-
220
- });
221
-
222
- it("should test escaped start & end with multiples start", () => {
223
-
224
- return new Promise((resolve) => {
225
-
226
- let dataCount = 0;
227
-
228
- const STX2 = 0x82;
229
- const stream = createReadStream();
230
-
231
- stream.pipe(new Splitter({
232
- "startWith": [ STX, STX2 ],
233
- "endWith": ETX,
234
- "escapeWith": DLE,
235
- "escaped": [ DLE, STX, ETX ]
236
- })).on("data", (chunk) => {
237
-
238
- ++dataCount;
239
-
240
- assert.strictEqual(typeof chunk, "object", "The chunk is not an object");
241
- assert.strictEqual(chunk instanceof Buffer, true, "The chunk is not a Buffer");
242
-
243
- if (1 === dataCount) {
244
-
245
- assert.deepStrictEqual(chunk,
246
- Buffer.from([ STX, 0x24, DLE, STX, 0x25, 0x26, DLE, DLE, 0x27, DLE, ETX, 0x28, ETX ]),
247
- "The chunk is not as expected"
248
- );
249
-
250
- }
251
- else if (2 === dataCount) {
252
-
253
- assert.deepStrictEqual(chunk,
254
- Buffer.from([ STX2, 0x24, DLE, STX, 0x25, 0x26, DLE, DLE, 0x27, DLE, ETX, 0x28, ETX ]),
255
- "The chunk is not as expected"
256
- );
257
-
258
- resolve();
259
-
260
- }
261
-
262
- });
263
-
264
- stream.push(Buffer.from([ 0x20, STX, 0x24, DLE, STX, 0x25, 0x26 ]));
265
- stream.push(Buffer.from([ DLE, DLE, 0x27, DLE, ETX, 0x28, ETX, 0x26, 0x24, 0x25 ]));
266
- stream.push(Buffer.from([ STX2, 0x24, DLE, STX, 0x25, 0x26 ]));
267
- stream.push(Buffer.from([ DLE, DLE, 0x27, DLE, ETX, 0x28, ETX, 0x26, 0x24, 0x25 ]));
268
-
269
- });
270
-
271
- });
272
-
273
- it("should test ack & nak", () => {
274
-
275
- return new Promise((resolve) => {
276
-
277
- let ackFound = false;
278
- let nakFound = false;
279
- let whateverFound = false;
280
-
281
- const stream = createReadStream();
282
-
283
- stream.pipe(new Splitter({
284
- "startWith": STX,
285
- "endWith": ETX,
286
- "specifics": {
287
- "ack": ACK,
288
- "nak": NAK,
289
- "wak": WAK,
290
- "whatever": 0x51
291
- },
292
- "escapeWith": DLE,
293
- "escaped": [ DLE, ACK, NAK ]
294
- })).on("data", (chunk) => {
295
-
296
- assert.strictEqual(typeof chunk, "object", "The chunk is not an object");
297
- assert.strictEqual(chunk instanceof Buffer, true, "The chunk is not a Buffer");
298
- assert.deepStrictEqual(chunk, Buffer.from([ STX, 0x20, 0x21, 0x22, ACK, NAK, WAK, 0x23, ETX ]), "The chunk is not as expected");
299
-
300
- }).on("ack", () => {
301
- ackFound = true;
302
- }).on("nak", () => {
303
- nakFound = true;
304
- }).on("whatever", () => {
305
- whateverFound = true;
306
- }).on("wak", () => {
307
-
308
- assert.strictEqual(ackFound, true, "There is no \"ack\" found");
309
- assert.strictEqual(nakFound, true, "There is no \"nak\" found");
310
- assert.strictEqual(whateverFound, true, "There is no \"whatever\" found");
311
-
312
- resolve();
313
-
314
- });
315
-
316
- stream.push(Buffer.from([ 0x51, 0x24, ACK, DLE, ACK, STX, 0x20, 0x21, 0x22, ACK, NAK, WAK ]));
317
- stream.push(Buffer.from([ 0x23, ETX, NAK, DLE, NAK, WAK, DLE, WAK, 0x20, 0x21 ]));
318
-
319
- });
320
-
321
- });
322
-
323
- it("should start & end with LRC", () => {
324
-
325
- return new Promise((resolve, reject) => {
326
-
327
- const stream = createReadStream();
328
-
329
- stream.pipe(new Splitter({
330
- "startWith": STX,
331
- "endWith": ETX,
332
- "controlBits": "end+1"
333
- })).on("data", (chunk) => {
334
-
335
- assert.strictEqual(typeof chunk, "object", "The chunk is not an object");
336
- assert.strictEqual(chunk instanceof Buffer, true, "The chunk is not a Buffer");
337
- assert.deepStrictEqual(chunk, Buffer.from([ STX, 0x20, 0x21, 0x22, 0x24, ETX, 0x07 ]), "The chunk is not as expected");
338
-
339
- const data = chunk.slice(1, chunk.length - 2);
340
- const LRC = chunk[chunk.length - 1];
341
-
342
- if (_computeLRC(data) === LRC) {
343
- resolve();
344
- }
345
- else {
346
-
347
- reject(
348
- new Error(
349
- "not well-computed data :" + data.toString("hex") + "|" + Buffer.from([ LRC ]).toString("hex")
350
- )
351
- );
352
-
353
- }
354
-
355
- });
356
-
357
- stream.push(Buffer.from([ 0x51, 0x24, STX, 0x20, 0x21, 0x22, 0x24, ETX, 0x07, 0x24 ]));
358
-
359
- });
360
-
361
- });
362
-
363
- });
@@ -1,38 +0,0 @@
1
- /// <reference path="../../lib/index.d.ts" />
2
-
3
- import Splitter = require("../../lib/main.js");
4
- import { Readable } from "stream";
5
-
6
- const STX = 0x02, ETX = 0x03, DLE = 0x10, ACK = 0x06, NAK = 0x15, WAK = 0x13;
7
-
8
- const stream = new Readable({
9
- read () { }
10
- });
11
-
12
- stream.pipe(new Splitter({
13
- "startWith": STX,
14
- "endWith": ETX,
15
- "specifics": {
16
- "ack": ACK,
17
- "nak": NAK,
18
- "wak": WAK,
19
- "whatever": 0x51
20
- },
21
- "escapeWith": DLE,
22
- "escaped": [ DLE, ACK, NAK, WAK ],
23
- "controlBits": "end+1"
24
- })).on("ack", () => {
25
- console.log("ack received"); // (only 1x) -> good, escaped, in data
26
- }).on("nak", () => {
27
- console.log("nak received"); // (only 1x) -> in data, good, escaped
28
- }).on("wak", () => {
29
- console.log("wak received"); // (only 1x) -> in data, good, escaped
30
- }).on("whatever", () => {
31
- console.log("whatever received"); // (only 1x)
32
- }).on("data", (chunk) => {
33
- console.log(chunk);
34
- // Buffer([ STX, 0x20, 0x21, 0x22, ACK, NAK, WAK, 0x23, ETX, 0x01 ]) (x1)
35
- });
36
-
37
- stream.push(Buffer.from([ 0x51, 0x01, ACK, DLE, ACK, STX, 0x20, 0x21, 0x22, ACK, NAK, WAK ]));
38
- stream.push(Buffer.from([ 0x23, ETX, 0x01, NAK, DLE, NAK, WAK, DLE, WAK, 0x20, 0x21 ]));