|  | 
|  | 1 | +import { cbor, dateToTag } from "@smithy/core/cbor"; | 
|  | 2 | +import { HttpResponse } from "@smithy/protocol-http"; | 
|  | 3 | +import { requireRequestsFrom } from "@smithy/util-test/src"; | 
|  | 4 | +import { Readable } from "node:stream"; | 
|  | 5 | +import { describe, expect, test as it } from "vitest"; | 
|  | 6 | +import { XYZService } from "xyz"; | 
|  | 7 | + | 
|  | 8 | +describe("local model integration test for cbor eventstreams", () => { | 
|  | 9 | +  it("should read and write cbor event streams", async () => { | 
|  | 10 | +    const client = new XYZService({ | 
|  | 11 | +      endpoint: "https://localhost", | 
|  | 12 | +    }); | 
|  | 13 | + | 
|  | 14 | +    const body = cbor.serialize({ | 
|  | 15 | +      id: "alpha", | 
|  | 16 | +      timestamp: dateToTag(new Date(0)), | 
|  | 17 | +    }); | 
|  | 18 | + | 
|  | 19 | +    function toInt32(n: number): number[] { | 
|  | 20 | +      const uint32 = new Uint8Array(4); | 
|  | 21 | +      const dv = new DataView(uint32.buffer, 0, 4); | 
|  | 22 | +      dv.setUint32(0, n); | 
|  | 23 | +      return [...uint32]; | 
|  | 24 | +    } | 
|  | 25 | + | 
|  | 26 | +    requireRequestsFrom(client) | 
|  | 27 | +      .toMatch({ | 
|  | 28 | +        hostname: /localhost/, | 
|  | 29 | +        async body(body) { | 
|  | 30 | +          const outgoing = []; | 
|  | 31 | +          for await (const chunk of body) { | 
|  | 32 | +            outgoing.push(chunk); | 
|  | 33 | +          } | 
|  | 34 | +          expect(outgoing).toEqual([ | 
|  | 35 | +            new Uint8Array([ | 
|  | 36 | +              0, 0, 0, 101, 0, 0, 0, 75, 213, 254, 191, 76, 11, 58, 101, 118, 101, 110, 116, 45, 116, 121, 112, 101, 7, | 
|  | 37 | +              0, 5, 97, 108, 112, 104, 97, 13, 58, 109, 101, 115, 115, 97, 103, 101, 45, 116, 121, 112, 101, 7, 0, 5, | 
|  | 38 | +              101, 118, 101, 110, 116, 13, 58, 99, 111, 110, 116, 101, 110, 116, 45, 116, 121, 112, 101, 7, 0, 16, 97, | 
|  | 39 | +              112, 112, 108, 105, 99, 97, 116, 105, 111, 110, 47, 99, 98, 111, 114, 161, 98, 105, 100, 101, 97, 108, | 
|  | 40 | +              112, 104, 97, 32, 93, 69, 236, | 
|  | 41 | +            ]), | 
|  | 42 | +            new Uint8Array([ | 
|  | 43 | +              0, 0, 0, 91, 0, 0, 0, 74, 188, 232, 137, 61, 11, 58, 101, 118, 101, 110, 116, 45, 116, 121, 112, 101, 7, | 
|  | 44 | +              0, 4, 98, 101, 116, 97, 13, 58, 109, 101, 115, 115, 97, 103, 101, 45, 116, 121, 112, 101, 7, 0, 5, 101, | 
|  | 45 | +              118, 101, 110, 116, 13, 58, 99, 111, 110, 116, 101, 110, 116, 45, 116, 121, 112, 101, 7, 0, 16, 97, 112, | 
|  | 46 | +              112, 108, 105, 99, 97, 116, 105, 111, 110, 47, 99, 98, 111, 114, 160, 195, 209, 62, 47, | 
|  | 47 | +            ]), | 
|  | 48 | +            new Uint8Array([ | 
|  | 49 | +              0, 0, 0, 91, 0, 0, 0, 74, 188, 232, 137, 61, 11, 58, 101, 118, 101, 110, 116, 45, 116, 121, 112, 101, 7, | 
|  | 50 | +              0, 4, 98, 101, 116, 97, 13, 58, 109, 101, 115, 115, 97, 103, 101, 45, 116, 121, 112, 101, 7, 0, 5, 101, | 
|  | 51 | +              118, 101, 110, 116, 13, 58, 99, 111, 110, 116, 101, 110, 116, 45, 116, 121, 112, 101, 7, 0, 16, 97, 112, | 
|  | 52 | +              112, 108, 105, 99, 97, 116, 105, 111, 110, 47, 99, 98, 111, 114, 160, 195, 209, 62, 47, | 
|  | 53 | +            ]), | 
|  | 54 | +            new Uint8Array(), | 
|  | 55 | +          ]); | 
|  | 56 | +        }, | 
|  | 57 | +      }) | 
|  | 58 | +      .respondWith( | 
|  | 59 | +        new HttpResponse({ | 
|  | 60 | +          statusCode: 200, | 
|  | 61 | +          headers: { | 
|  | 62 | +            "smithy-protocol": "rpc-v2-cbor", | 
|  | 63 | +          }, | 
|  | 64 | +          body: Readable.from({ | 
|  | 65 | +            async *[Symbol.asyncIterator]() { | 
|  | 66 | +              yield new Uint8Array([ | 
|  | 67 | +                /* message size */ ...toInt32(91 + body.byteLength), | 
|  | 68 | +                /* header size */ ...toInt32(75), | 
|  | 69 | +                /* prelude crc */ ...toInt32(1084132878), | 
|  | 70 | +                /* headers */ | 
|  | 71 | +                /* :event-type */ | 
|  | 72 | +                11, | 
|  | 73 | +                ...[58, 101, 118, 101, 110, 116, 45, 116, 121, 112, 101], | 
|  | 74 | +                7, | 
|  | 75 | +                /* alpha */ | 
|  | 76 | +                0, | 
|  | 77 | +                5, | 
|  | 78 | +                ...[97, 108, 112, 104, 97], | 
|  | 79 | +                /* :content-type */ | 
|  | 80 | +                13, | 
|  | 81 | +                ...[58, 99, 111, 110, 116, 101, 110, 116, 45, 116, 121, 112, 101], | 
|  | 82 | +                7, | 
|  | 83 | +                /* application/cbor */ | 
|  | 84 | +                0, | 
|  | 85 | +                16, | 
|  | 86 | +                ...[97, 112, 112, 108, 105, 99, 97, 116, 105, 111, 110, 47, 99, 98, 111, 114], | 
|  | 87 | +                /* :message-type */ | 
|  | 88 | +                13, | 
|  | 89 | +                ...[58, 109, 101, 115, 115, 97, 103, 101, 45, 116, 121, 112, 101], | 
|  | 90 | +                7, | 
|  | 91 | +                /* event */ | 
|  | 92 | +                0, | 
|  | 93 | +                5, | 
|  | 94 | +                ...[101, 118, 101, 110, 116], | 
|  | 95 | + | 
|  | 96 | +                /* body */ | 
|  | 97 | +                ...body, | 
|  | 98 | + | 
|  | 99 | +                /* message crc */ | 
|  | 100 | +                ...toInt32(1938836882), | 
|  | 101 | +              ]); | 
|  | 102 | +            }, | 
|  | 103 | +          }), | 
|  | 104 | +        }) | 
|  | 105 | +      ); | 
|  | 106 | + | 
|  | 107 | +    const response = await client.tradeEventStream({ | 
|  | 108 | +      eventStream: { | 
|  | 109 | +        async *[Symbol.asyncIterator]() { | 
|  | 110 | +          yield { | 
|  | 111 | +            alpha: { | 
|  | 112 | +              id: "alpha", | 
|  | 113 | +            }, | 
|  | 114 | +          }; | 
|  | 115 | +          yield { | 
|  | 116 | +            beta: {}, | 
|  | 117 | +          }; | 
|  | 118 | +          yield { | 
|  | 119 | +            gamma: {}, | 
|  | 120 | +          }; | 
|  | 121 | +        }, | 
|  | 122 | +      }, | 
|  | 123 | +    }); | 
|  | 124 | + | 
|  | 125 | +    const responses = [] as any[]; | 
|  | 126 | +    for await (const event of response.eventStream ?? []) { | 
|  | 127 | +      responses.push(event); | 
|  | 128 | +    } | 
|  | 129 | + | 
|  | 130 | +    expect(responses).toEqual([ | 
|  | 131 | +      { | 
|  | 132 | +        alpha: { | 
|  | 133 | +          id: "alpha", | 
|  | 134 | +          timestamp: new Date(0), | 
|  | 135 | +        }, | 
|  | 136 | +      }, | 
|  | 137 | +    ]); | 
|  | 138 | +  }); | 
|  | 139 | +}); | 
0 commit comments