|
1 |
| -import { Crc32 } from "@aws-crypto/crc32"; |
| 1 | +import { EventStreamCodec } from "@aws-sdk/eventstream-codec"; |
2 | 2 | import { Message, MessageHeaders } from "@aws-sdk/types";
|
3 | 3 | import { Decoder, Encoder } from "@aws-sdk/types";
|
4 | 4 |
|
5 |
| -import { HeaderMarshaller } from "./HeaderMarshaller"; |
6 |
| -import { splitMessage } from "./splitMessage"; |
7 |
| - |
8 | 5 | /**
|
9 | 6 | * A marshaller that can convert binary-packed event stream messages into
|
10 | 7 | * JavaScript objects and back again into their binary format.
|
| 8 | + * |
| 9 | + * @deprecated Use EventStreamCodec from @aws-sdk/eventstream-codec instead. |
11 | 10 | */
|
12 | 11 | export class EventStreamMarshaller {
|
13 |
| - private readonly headerMarshaller: HeaderMarshaller; |
| 12 | + private readonly codec: EventStreamCodec; |
14 | 13 |
|
15 | 14 | constructor(toUtf8: Encoder, fromUtf8: Decoder) {
|
16 |
| - this.headerMarshaller = new HeaderMarshaller(toUtf8, fromUtf8); |
| 15 | + this.codec = new EventStreamCodec(toUtf8, fromUtf8); |
17 | 16 | }
|
18 | 17 |
|
19 |
| - /** |
20 |
| - * Convert a structured JavaScript object with tagged headers into a binary |
21 |
| - * event stream message. |
22 |
| - */ |
23 |
| - marshall({ headers: rawHeaders, body }: Message): Uint8Array { |
24 |
| - const headers = this.headerMarshaller.format(rawHeaders); |
25 |
| - const length = headers.byteLength + body.byteLength + 16; |
26 |
| - |
27 |
| - const out = new Uint8Array(length); |
28 |
| - const view = new DataView(out.buffer, out.byteOffset, out.byteLength); |
29 |
| - const checksum = new Crc32(); |
30 |
| - |
31 |
| - // Format message |
32 |
| - view.setUint32(0, length, false); |
33 |
| - view.setUint32(4, headers.byteLength, false); |
34 |
| - view.setUint32(8, checksum.update(out.subarray(0, 8)).digest(), false); |
35 |
| - out.set(headers, 12); |
36 |
| - out.set(body, headers.byteLength + 12); |
37 |
| - |
38 |
| - // Write trailing message checksum |
39 |
| - view.setUint32(length - 4, checksum.update(out.subarray(8, length - 4)).digest(), false); |
40 |
| - |
41 |
| - return out; |
| 18 | + marshall(message: Message): Uint8Array { |
| 19 | + return this.codec.encode(message); |
42 | 20 | }
|
43 | 21 |
|
44 |
| - /** |
45 |
| - * Convert a binary event stream message into a JavaScript object with an |
46 |
| - * opaque, binary body and tagged, parsed headers. |
47 |
| - */ |
48 | 22 | unmarshall(message: ArrayBufferView): Message {
|
49 |
| - const { headers, body } = splitMessage(message); |
50 |
| - |
51 |
| - return { headers: this.headerMarshaller.parse(headers), body }; |
| 23 | + return this.codec.decode(message); |
52 | 24 | }
|
53 | 25 |
|
54 |
| - /** |
55 |
| - * Convert a structured JavaScript object with tagged headers into a binary |
56 |
| - * event stream message header. |
57 |
| - */ |
58 | 26 | formatHeaders(rawHeaders: MessageHeaders): Uint8Array {
|
59 |
| - return this.headerMarshaller.format(rawHeaders); |
| 27 | + return this.codec.formatHeaders(rawHeaders); |
60 | 28 | }
|
61 | 29 | }
|
0 commit comments