|
1 | 1 | import {
|
2 | 2 | decodePacket,
|
3 |
| - decodePacketFromBinary, |
4 | 3 | decodePayload,
|
5 | 4 | encodePacket,
|
6 |
| - encodePacketToBinary, |
7 | 5 | encodePayload,
|
| 6 | + createPacketEncoderStream, |
| 7 | + createPacketDecoderStream, |
8 | 8 | Packet
|
9 | 9 | } from "..";
|
10 | 10 | import * as expect from "expect.js";
|
@@ -114,112 +114,41 @@ describe("engine.io-parser (browser only)", () => {
|
114 | 114 | }
|
115 | 115 | });
|
116 | 116 |
|
117 |
| - describe("single packet (to/from Uint8Array)", function() { |
118 |
| - if (!withNativeArrayBuffer) { |
119 |
| - // @ts-ignore |
120 |
| - return this.skip(); |
121 |
| - } |
122 |
| - |
123 |
| - it("should encode a plaintext packet", done => { |
124 |
| - const packet: Packet = { |
125 |
| - type: "message", |
126 |
| - data: "1€" |
127 |
| - }; |
128 |
| - encodePacketToBinary(packet, encodedPacket => { |
129 |
| - expect(encodedPacket).to.be.an(Uint8Array); |
130 |
| - expect(encodedPacket).to.eql(Uint8Array.from([52, 49, 226, 130, 172])); |
131 |
| - |
132 |
| - const decoded = decodePacketFromBinary( |
133 |
| - encodedPacket, |
134 |
| - false, |
135 |
| - "arraybuffer" |
136 |
| - ); |
137 |
| - expect(decoded).to.eql(packet); |
138 |
| - done(); |
139 |
| - }); |
140 |
| - }); |
| 117 | + if (typeof TextEncoder === "function") { |
| 118 | + describe("createPacketEncoderStream", () => { |
| 119 | + it("should encode a binary packet (Blob)", async () => { |
| 120 | + const stream = createPacketEncoderStream(); |
141 | 121 |
|
142 |
| - it("should encode a binary packet (Uint8Array)", done => { |
143 |
| - const packet: Packet = { |
144 |
| - type: "message", |
145 |
| - data: Uint8Array.from([1, 2, 3]) |
146 |
| - }; |
147 |
| - encodePacketToBinary(packet, encodedPacket => { |
148 |
| - expect(encodedPacket === packet.data).to.be(true); |
149 |
| - done(); |
150 |
| - }); |
151 |
| - }); |
| 122 | + const writer = stream.writable.getWriter(); |
| 123 | + const reader = stream.readable.getReader(); |
152 | 124 |
|
153 |
| - it("should encode a binary packet (Blob)", done => { |
154 |
| - const packet: Packet = { |
155 |
| - type: "message", |
156 |
| - data: new Blob([Uint8Array.from([1, 2, 3])]) |
157 |
| - }; |
158 |
| - encodePacketToBinary(packet, encodedPacket => { |
159 |
| - expect(encodedPacket).to.be.an(Uint8Array); |
160 |
| - expect(encodedPacket).to.eql(Uint8Array.from([1, 2, 3])); |
161 |
| - done(); |
162 |
| - }); |
163 |
| - }); |
| 125 | + writer.write({ |
| 126 | + type: "message", |
| 127 | + data: new Blob([Uint8Array.from([1, 2, 3])]) |
| 128 | + }); |
164 | 129 |
|
165 |
| - it("should encode a binary packet (ArrayBuffer)", done => { |
166 |
| - const packet: Packet = { |
167 |
| - type: "message", |
168 |
| - data: Uint8Array.from([1, 2, 3]).buffer |
169 |
| - }; |
170 |
| - encodePacketToBinary(packet, encodedPacket => { |
171 |
| - expect(encodedPacket).to.be.an(Uint8Array); |
172 |
| - expect(encodedPacket).to.eql(Uint8Array.from([1, 2, 3])); |
173 |
| - done(); |
174 |
| - }); |
175 |
| - }); |
| 130 | + const header = await reader.read(); |
| 131 | + expect(header.value).to.eql(Uint8Array.of(128, 0, 0, 3)); |
176 | 132 |
|
177 |
| - it("should encode a binary packet (Uint16Array)", done => { |
178 |
| - const packet: Packet = { |
179 |
| - type: "message", |
180 |
| - data: Uint16Array.from([1, 2, 257]) |
181 |
| - }; |
182 |
| - encodePacketToBinary(packet, encodedPacket => { |
183 |
| - expect(encodedPacket).to.be.an(Uint8Array); |
184 |
| - expect(encodedPacket).to.eql(Uint8Array.from([1, 0, 2, 0, 1, 1])); |
185 |
| - done(); |
| 133 | + const payload = await reader.read(); |
| 134 | + expect(payload.value).to.eql(Uint8Array.of(1, 2, 3)); |
186 | 135 | });
|
187 | 136 | });
|
188 | 137 |
|
189 |
| - it("should decode a binary packet (Blob)", () => { |
190 |
| - const decoded = decodePacketFromBinary( |
191 |
| - Uint8Array.from([1, 2, 3]), |
192 |
| - false, |
193 |
| - "blob" |
194 |
| - ); |
| 138 | + describe("createPacketDecoderStream", () => { |
| 139 | + it("should decode a binary packet (Blob)", async () => { |
| 140 | + const stream = createPacketDecoderStream(1e6, "blob"); |
195 | 141 |
|
196 |
| - expect(decoded.type).to.eql("message"); |
197 |
| - expect(decoded.data).to.be.a(Blob); |
198 |
| - }); |
| 142 | + const writer = stream.writable.getWriter(); |
| 143 | + const reader = stream.readable.getReader(); |
199 | 144 |
|
200 |
| - it("should decode a binary packet (ArrayBuffer)", () => { |
201 |
| - const decoded = decodePacketFromBinary( |
202 |
| - Uint8Array.from([1, 2, 3]), |
203 |
| - false, |
204 |
| - "arraybuffer" |
205 |
| - ); |
| 145 | + writer.write(Uint8Array.of(128, 0, 0, 3, 1, 2, 3)); |
206 | 146 |
|
207 |
| - expect(decoded.type).to.eql("message"); |
208 |
| - expect(decoded.data).to.be.an(ArrayBuffer); |
209 |
| - expect(areArraysEqual(decoded.data, Uint8Array.from([1, 2, 3]))); |
210 |
| - }); |
| 147 | + const { value } = await reader.read(); |
211 | 148 |
|
212 |
| - it("should decode a binary packet (with binary header)", () => { |
213 |
| - // 52 === "4".charCodeAt(0) |
214 |
| - const decoded = decodePacketFromBinary( |
215 |
| - Uint8Array.from([52]), |
216 |
| - true, |
217 |
| - "arraybuffer" |
218 |
| - ); |
219 |
| - |
220 |
| - expect(decoded.type).to.eql("message"); |
221 |
| - expect(decoded.data).to.be.an(ArrayBuffer); |
222 |
| - expect(areArraysEqual(decoded.data, Uint8Array.from([52]))); |
| 149 | + expect(value.type).to.eql("message"); |
| 150 | + expect(value.data).to.be.a(Blob); |
| 151 | + }); |
223 | 152 | });
|
224 |
| - }); |
| 153 | + } |
225 | 154 | });
|
0 commit comments