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