@smithy/eventstream-codec 1.0.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.
Files changed (39) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +4 -0
  3. package/dist-cjs/EventStreamCodec.js +66 -0
  4. package/dist-cjs/HeaderMarshaller.js +186 -0
  5. package/dist-cjs/Int64.js +47 -0
  6. package/dist-cjs/Message.js +2 -0
  7. package/dist-cjs/MessageDecoderStream.js +18 -0
  8. package/dist-cjs/MessageEncoderStream.js +21 -0
  9. package/dist-cjs/SmithyMessageDecoderStream.js +20 -0
  10. package/dist-cjs/SmithyMessageEncoderStream.js +18 -0
  11. package/dist-cjs/TestVectors.fixture.js +693 -0
  12. package/dist-cjs/index.js +11 -0
  13. package/dist-cjs/splitMessage.js +34 -0
  14. package/dist-cjs/vectorTypes.fixture.js +2 -0
  15. package/dist-es/EventStreamCodec.js +62 -0
  16. package/dist-es/HeaderMarshaller.js +182 -0
  17. package/dist-es/Int64.js +43 -0
  18. package/dist-es/Message.js +1 -0
  19. package/dist-es/MessageDecoderStream.js +14 -0
  20. package/dist-es/MessageEncoderStream.js +17 -0
  21. package/dist-es/SmithyMessageDecoderStream.js +16 -0
  22. package/dist-es/SmithyMessageEncoderStream.js +14 -0
  23. package/dist-es/TestVectors.fixture.js +690 -0
  24. package/dist-es/index.js +8 -0
  25. package/dist-es/splitMessage.js +30 -0
  26. package/dist-es/vectorTypes.fixture.js +1 -0
  27. package/dist-types/EventStreamCodec.d.ts +31 -0
  28. package/dist-types/HeaderMarshaller.d.ts +12 -0
  29. package/dist-types/Int64.d.ts +20 -0
  30. package/dist-types/Message.d.ts +26 -0
  31. package/dist-types/MessageDecoderStream.d.ts +17 -0
  32. package/dist-types/MessageEncoderStream.d.ts +18 -0
  33. package/dist-types/SmithyMessageDecoderStream.d.ts +17 -0
  34. package/dist-types/SmithyMessageEncoderStream.d.ts +17 -0
  35. package/dist-types/TestVectors.fixture.d.ts +2 -0
  36. package/dist-types/index.d.ts +8 -0
  37. package/dist-types/splitMessage.d.ts +11 -0
  38. package/dist-types/vectorTypes.fixture.d.ts +12 -0
  39. package/package.json +60 -0
@@ -0,0 +1,693 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.vectors = void 0;
4
+ const Int64_1 = require("./Int64");
5
+ exports.vectors = {
6
+ all_headers: {
7
+ expectation: "success",
8
+ encoded: Uint8Array.from([
9
+ 0,
10
+ 0,
11
+ 0,
12
+ 204,
13
+ 0,
14
+ 0,
15
+ 0,
16
+ 175,
17
+ 15,
18
+ 174,
19
+ 100,
20
+ 202,
21
+ 10,
22
+ 101,
23
+ 118,
24
+ 101,
25
+ 110,
26
+ 116,
27
+ 45,
28
+ 116,
29
+ 121,
30
+ 112,
31
+ 101,
32
+ 4,
33
+ 0,
34
+ 0,
35
+ 160,
36
+ 12,
37
+ 12,
38
+ 99,
39
+ 111,
40
+ 110,
41
+ 116,
42
+ 101,
43
+ 110,
44
+ 116,
45
+ 45,
46
+ 116,
47
+ 121,
48
+ 112,
49
+ 101,
50
+ 7,
51
+ 0,
52
+ 16,
53
+ 97,
54
+ 112,
55
+ 112,
56
+ 108,
57
+ 105,
58
+ 99,
59
+ 97,
60
+ 116,
61
+ 105,
62
+ 111,
63
+ 110,
64
+ 47,
65
+ 106,
66
+ 115,
67
+ 111,
68
+ 110,
69
+ 10,
70
+ 98,
71
+ 111,
72
+ 111,
73
+ 108,
74
+ 32,
75
+ 102,
76
+ 97,
77
+ 108,
78
+ 115,
79
+ 101,
80
+ 1,
81
+ 9,
82
+ 98,
83
+ 111,
84
+ 111,
85
+ 108,
86
+ 32,
87
+ 116,
88
+ 114,
89
+ 117,
90
+ 101,
91
+ 0,
92
+ 4,
93
+ 98,
94
+ 121,
95
+ 116,
96
+ 101,
97
+ 2,
98
+ 207,
99
+ 8,
100
+ 98,
101
+ 121,
102
+ 116,
103
+ 101,
104
+ 32,
105
+ 98,
106
+ 117,
107
+ 102,
108
+ 6,
109
+ 0,
110
+ 20,
111
+ 73,
112
+ 39,
113
+ 109,
114
+ 32,
115
+ 97,
116
+ 32,
117
+ 108,
118
+ 105,
119
+ 116,
120
+ 116,
121
+ 108,
122
+ 101,
123
+ 32,
124
+ 116,
125
+ 101,
126
+ 97,
127
+ 112,
128
+ 111,
129
+ 116,
130
+ 33,
131
+ 9,
132
+ 116,
133
+ 105,
134
+ 109,
135
+ 101,
136
+ 115,
137
+ 116,
138
+ 97,
139
+ 109,
140
+ 112,
141
+ 8,
142
+ 0,
143
+ 0,
144
+ 0,
145
+ 0,
146
+ 0,
147
+ 132,
148
+ 95,
149
+ 237,
150
+ 5,
151
+ 105,
152
+ 110,
153
+ 116,
154
+ 49,
155
+ 54,
156
+ 3,
157
+ 0,
158
+ 42,
159
+ 5,
160
+ 105,
161
+ 110,
162
+ 116,
163
+ 54,
164
+ 52,
165
+ 5,
166
+ 0,
167
+ 0,
168
+ 0,
169
+ 0,
170
+ 2,
171
+ 135,
172
+ 87,
173
+ 178,
174
+ 4,
175
+ 117,
176
+ 117,
177
+ 105,
178
+ 100,
179
+ 9,
180
+ 1,
181
+ 2,
182
+ 3,
183
+ 4,
184
+ 5,
185
+ 6,
186
+ 7,
187
+ 8,
188
+ 9,
189
+ 10,
190
+ 11,
191
+ 12,
192
+ 13,
193
+ 14,
194
+ 15,
195
+ 16,
196
+ 123,
197
+ 39,
198
+ 102,
199
+ 111,
200
+ 111,
201
+ 39,
202
+ 58,
203
+ 39,
204
+ 98,
205
+ 97,
206
+ 114,
207
+ 39,
208
+ 125,
209
+ 171,
210
+ 165,
211
+ 241,
212
+ 12,
213
+ ]),
214
+ decoded: {
215
+ headers: {
216
+ "event-type": {
217
+ type: "integer",
218
+ value: 40972,
219
+ },
220
+ "content-type": {
221
+ type: "string",
222
+ value: "application/json",
223
+ },
224
+ "bool false": {
225
+ type: "boolean",
226
+ value: false,
227
+ },
228
+ "bool true": {
229
+ type: "boolean",
230
+ value: true,
231
+ },
232
+ byte: {
233
+ type: "byte",
234
+ value: -49,
235
+ },
236
+ "byte buf": {
237
+ type: "binary",
238
+ value: Uint8Array.from([
239
+ 73,
240
+ 39,
241
+ 109,
242
+ 32,
243
+ 97,
244
+ 32,
245
+ 108,
246
+ 105,
247
+ 116,
248
+ 116,
249
+ 108,
250
+ 101,
251
+ 32,
252
+ 116,
253
+ 101,
254
+ 97,
255
+ 112,
256
+ 111,
257
+ 116,
258
+ 33,
259
+ ]),
260
+ },
261
+ timestamp: {
262
+ type: "timestamp",
263
+ value: new Date(8675309),
264
+ },
265
+ int16: {
266
+ type: "short",
267
+ value: 42,
268
+ },
269
+ int64: {
270
+ type: "long",
271
+ value: Int64_1.Int64.fromNumber(42424242),
272
+ },
273
+ uuid: {
274
+ type: "uuid",
275
+ value: "01020304-0506-0708-090a-0b0c0d0e0f10",
276
+ },
277
+ },
278
+ body: Uint8Array.from([123, 39, 102, 111, 111, 39, 58, 39, 98, 97, 114, 39, 125]),
279
+ },
280
+ },
281
+ empty_message: {
282
+ expectation: "success",
283
+ encoded: Uint8Array.from([0, 0, 0, 16, 0, 0, 0, 0, 5, 194, 72, 235, 125, 152, 200, 255]),
284
+ decoded: {
285
+ headers: {},
286
+ body: Uint8Array.from([]),
287
+ },
288
+ },
289
+ int32_header: {
290
+ expectation: "success",
291
+ encoded: Uint8Array.from([
292
+ 0,
293
+ 0,
294
+ 0,
295
+ 45,
296
+ 0,
297
+ 0,
298
+ 0,
299
+ 16,
300
+ 65,
301
+ 196,
302
+ 36,
303
+ 184,
304
+ 10,
305
+ 101,
306
+ 118,
307
+ 101,
308
+ 110,
309
+ 116,
310
+ 45,
311
+ 116,
312
+ 121,
313
+ 112,
314
+ 101,
315
+ 4,
316
+ 0,
317
+ 0,
318
+ 160,
319
+ 12,
320
+ 123,
321
+ 39,
322
+ 102,
323
+ 111,
324
+ 111,
325
+ 39,
326
+ 58,
327
+ 39,
328
+ 98,
329
+ 97,
330
+ 114,
331
+ 39,
332
+ 125,
333
+ 54,
334
+ 244,
335
+ 128,
336
+ 160,
337
+ ]),
338
+ decoded: {
339
+ headers: {
340
+ "event-type": {
341
+ type: "integer",
342
+ value: 40972,
343
+ },
344
+ },
345
+ body: Uint8Array.from([123, 39, 102, 111, 111, 39, 58, 39, 98, 97, 114, 39, 125]),
346
+ },
347
+ },
348
+ payload_no_headers: {
349
+ expectation: "success",
350
+ encoded: Uint8Array.from([
351
+ 0,
352
+ 0,
353
+ 0,
354
+ 29,
355
+ 0,
356
+ 0,
357
+ 0,
358
+ 0,
359
+ 253,
360
+ 82,
361
+ 140,
362
+ 90,
363
+ 123,
364
+ 39,
365
+ 102,
366
+ 111,
367
+ 111,
368
+ 39,
369
+ 58,
370
+ 39,
371
+ 98,
372
+ 97,
373
+ 114,
374
+ 39,
375
+ 125,
376
+ 195,
377
+ 101,
378
+ 57,
379
+ 54,
380
+ ]),
381
+ decoded: {
382
+ headers: {},
383
+ body: Uint8Array.from([123, 39, 102, 111, 111, 39, 58, 39, 98, 97, 114, 39, 125]),
384
+ },
385
+ },
386
+ payload_one_str_header: {
387
+ expectation: "success",
388
+ encoded: Uint8Array.from([
389
+ 0,
390
+ 0,
391
+ 0,
392
+ 61,
393
+ 0,
394
+ 0,
395
+ 0,
396
+ 32,
397
+ 7,
398
+ 253,
399
+ 131,
400
+ 150,
401
+ 12,
402
+ 99,
403
+ 111,
404
+ 110,
405
+ 116,
406
+ 101,
407
+ 110,
408
+ 116,
409
+ 45,
410
+ 116,
411
+ 121,
412
+ 112,
413
+ 101,
414
+ 7,
415
+ 0,
416
+ 16,
417
+ 97,
418
+ 112,
419
+ 112,
420
+ 108,
421
+ 105,
422
+ 99,
423
+ 97,
424
+ 116,
425
+ 105,
426
+ 111,
427
+ 110,
428
+ 47,
429
+ 106,
430
+ 115,
431
+ 111,
432
+ 110,
433
+ 123,
434
+ 39,
435
+ 102,
436
+ 111,
437
+ 111,
438
+ 39,
439
+ 58,
440
+ 39,
441
+ 98,
442
+ 97,
443
+ 114,
444
+ 39,
445
+ 125,
446
+ 141,
447
+ 156,
448
+ 8,
449
+ 177,
450
+ ]),
451
+ decoded: {
452
+ headers: {
453
+ "content-type": {
454
+ type: "string",
455
+ value: "application/json",
456
+ },
457
+ },
458
+ body: Uint8Array.from([123, 39, 102, 111, 111, 39, 58, 39, 98, 97, 114, 39, 125]),
459
+ },
460
+ },
461
+ corrupted_headers: {
462
+ expectation: "failure",
463
+ encoded: Uint8Array.from([
464
+ 0,
465
+ 0,
466
+ 0,
467
+ 61,
468
+ 0,
469
+ 0,
470
+ 0,
471
+ 32,
472
+ 7,
473
+ 253,
474
+ 131,
475
+ 150,
476
+ 12,
477
+ 99,
478
+ 111,
479
+ 110,
480
+ 116,
481
+ 101,
482
+ 110,
483
+ 116,
484
+ 45,
485
+ 116,
486
+ 121,
487
+ 112,
488
+ 101,
489
+ 7,
490
+ 0,
491
+ 16,
492
+ 97,
493
+ 112,
494
+ 112,
495
+ 108,
496
+ 105,
497
+ 99,
498
+ 97,
499
+ 116,
500
+ 105,
501
+ 111,
502
+ 110,
503
+ 47,
504
+ 106,
505
+ 115,
506
+ 111,
507
+ 110,
508
+ 123,
509
+ 97,
510
+ 102,
511
+ 111,
512
+ 111,
513
+ 39,
514
+ 58,
515
+ 39,
516
+ 98,
517
+ 97,
518
+ 114,
519
+ 39,
520
+ 125,
521
+ 141,
522
+ 156,
523
+ 8,
524
+ 177,
525
+ ]),
526
+ },
527
+ corrupted_header_len: {
528
+ expectation: "failure",
529
+ encoded: Uint8Array.from([
530
+ 0,
531
+ 0,
532
+ 0,
533
+ 61,
534
+ 0,
535
+ 0,
536
+ 0,
537
+ 33,
538
+ 7,
539
+ 253,
540
+ 131,
541
+ 150,
542
+ 12,
543
+ 99,
544
+ 111,
545
+ 110,
546
+ 116,
547
+ 101,
548
+ 110,
549
+ 116,
550
+ 45,
551
+ 116,
552
+ 121,
553
+ 112,
554
+ 101,
555
+ 7,
556
+ 0,
557
+ 16,
558
+ 97,
559
+ 112,
560
+ 112,
561
+ 108,
562
+ 105,
563
+ 99,
564
+ 97,
565
+ 116,
566
+ 105,
567
+ 111,
568
+ 110,
569
+ 47,
570
+ 106,
571
+ 115,
572
+ 111,
573
+ 110,
574
+ 123,
575
+ 39,
576
+ 102,
577
+ 111,
578
+ 111,
579
+ 39,
580
+ 58,
581
+ 39,
582
+ 98,
583
+ 97,
584
+ 114,
585
+ 39,
586
+ 125,
587
+ 141,
588
+ 156,
589
+ 8,
590
+ 177,
591
+ ]),
592
+ },
593
+ corrupted_length: {
594
+ expectation: "failure",
595
+ encoded: Uint8Array.from([
596
+ 0,
597
+ 0,
598
+ 0,
599
+ 62,
600
+ 0,
601
+ 0,
602
+ 0,
603
+ 32,
604
+ 7,
605
+ 253,
606
+ 131,
607
+ 150,
608
+ 12,
609
+ 99,
610
+ 111,
611
+ 110,
612
+ 116,
613
+ 101,
614
+ 110,
615
+ 116,
616
+ 45,
617
+ 116,
618
+ 121,
619
+ 112,
620
+ 101,
621
+ 7,
622
+ 0,
623
+ 16,
624
+ 97,
625
+ 112,
626
+ 112,
627
+ 108,
628
+ 105,
629
+ 99,
630
+ 97,
631
+ 116,
632
+ 105,
633
+ 111,
634
+ 110,
635
+ 47,
636
+ 106,
637
+ 115,
638
+ 111,
639
+ 110,
640
+ 123,
641
+ 39,
642
+ 102,
643
+ 111,
644
+ 111,
645
+ 39,
646
+ 58,
647
+ 39,
648
+ 98,
649
+ 97,
650
+ 114,
651
+ 39,
652
+ 125,
653
+ 141,
654
+ 156,
655
+ 8,
656
+ 177,
657
+ ]),
658
+ },
659
+ corrupted_payload: {
660
+ expectation: "failure",
661
+ encoded: Uint8Array.from([
662
+ 0,
663
+ 0,
664
+ 0,
665
+ 29,
666
+ 0,
667
+ 0,
668
+ 0,
669
+ 0,
670
+ 253,
671
+ 82,
672
+ 140,
673
+ 90,
674
+ 91,
675
+ 39,
676
+ 102,
677
+ 111,
678
+ 111,
679
+ 39,
680
+ 58,
681
+ 39,
682
+ 98,
683
+ 97,
684
+ 114,
685
+ 39,
686
+ 125,
687
+ 195,
688
+ 101,
689
+ 57,
690
+ 54,
691
+ ]),
692
+ },
693
+ };
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("tslib");
4
+ tslib_1.__exportStar(require("./EventStreamCodec"), exports);
5
+ tslib_1.__exportStar(require("./HeaderMarshaller"), exports);
6
+ tslib_1.__exportStar(require("./Int64"), exports);
7
+ tslib_1.__exportStar(require("./Message"), exports);
8
+ tslib_1.__exportStar(require("./MessageDecoderStream"), exports);
9
+ tslib_1.__exportStar(require("./MessageEncoderStream"), exports);
10
+ tslib_1.__exportStar(require("./SmithyMessageDecoderStream"), exports);
11
+ tslib_1.__exportStar(require("./SmithyMessageEncoderStream"), exports);
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.splitMessage = void 0;
4
+ const crc32_1 = require("@aws-crypto/crc32");
5
+ const PRELUDE_MEMBER_LENGTH = 4;
6
+ const PRELUDE_LENGTH = PRELUDE_MEMBER_LENGTH * 2;
7
+ const CHECKSUM_LENGTH = 4;
8
+ const MINIMUM_MESSAGE_LENGTH = PRELUDE_LENGTH + CHECKSUM_LENGTH * 2;
9
+ function splitMessage({ byteLength, byteOffset, buffer }) {
10
+ if (byteLength < MINIMUM_MESSAGE_LENGTH) {
11
+ throw new Error("Provided message too short to accommodate event stream message overhead");
12
+ }
13
+ const view = new DataView(buffer, byteOffset, byteLength);
14
+ const messageLength = view.getUint32(0, false);
15
+ if (byteLength !== messageLength) {
16
+ throw new Error("Reported message length does not match received message length");
17
+ }
18
+ const headerLength = view.getUint32(PRELUDE_MEMBER_LENGTH, false);
19
+ const expectedPreludeChecksum = view.getUint32(PRELUDE_LENGTH, false);
20
+ const expectedMessageChecksum = view.getUint32(byteLength - CHECKSUM_LENGTH, false);
21
+ const checksummer = new crc32_1.Crc32().update(new Uint8Array(buffer, byteOffset, PRELUDE_LENGTH));
22
+ if (expectedPreludeChecksum !== checksummer.digest()) {
23
+ throw new Error(`The prelude checksum specified in the message (${expectedPreludeChecksum}) does not match the calculated CRC32 checksum (${checksummer.digest()})`);
24
+ }
25
+ checksummer.update(new Uint8Array(buffer, byteOffset + PRELUDE_LENGTH, byteLength - (PRELUDE_LENGTH + CHECKSUM_LENGTH)));
26
+ if (expectedMessageChecksum !== checksummer.digest()) {
27
+ throw new Error(`The message checksum (${checksummer.digest()}) did not match the expected value of ${expectedMessageChecksum}`);
28
+ }
29
+ return {
30
+ headers: new DataView(buffer, byteOffset + PRELUDE_LENGTH + CHECKSUM_LENGTH, headerLength),
31
+ body: new Uint8Array(buffer, byteOffset + PRELUDE_LENGTH + CHECKSUM_LENGTH + headerLength, messageLength - headerLength - (PRELUDE_LENGTH + CHECKSUM_LENGTH + CHECKSUM_LENGTH)),
32
+ };
33
+ }
34
+ exports.splitMessage = splitMessage;