123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- "use strict";
- Object.defineProperty(exports, "__esModule", { value: true });
- exports.decodePacket = void 0;
- const commons_js_1 = require("./commons.js");
- const base64_arraybuffer_js_1 = require("./contrib/base64-arraybuffer.js");
- const withNativeArrayBuffer = typeof ArrayBuffer === "function";
- const decodePacket = (encodedPacket, binaryType) => {
- if (typeof encodedPacket !== "string") {
- return {
- type: "message",
- data: mapBinary(encodedPacket, binaryType),
- };
- }
- const type = encodedPacket.charAt(0);
- if (type === "b") {
- return {
- type: "message",
- data: decodeBase64Packet(encodedPacket.substring(1), binaryType),
- };
- }
- const packetType = commons_js_1.PACKET_TYPES_REVERSE[type];
- if (!packetType) {
- return commons_js_1.ERROR_PACKET;
- }
- return encodedPacket.length > 1
- ? {
- type: commons_js_1.PACKET_TYPES_REVERSE[type],
- data: encodedPacket.substring(1),
- }
- : {
- type: commons_js_1.PACKET_TYPES_REVERSE[type],
- };
- };
- exports.decodePacket = decodePacket;
- const decodeBase64Packet = (data, binaryType) => {
- if (withNativeArrayBuffer) {
- const decoded = (0, base64_arraybuffer_js_1.decode)(data);
- return mapBinary(decoded, binaryType);
- }
- else {
- return { base64: true, data }; // fallback for old browsers
- }
- };
- const mapBinary = (data, binaryType) => {
- switch (binaryType) {
- case "blob":
- if (data instanceof Blob) {
- // from WebSocket + binaryType "blob"
- return data;
- }
- else {
- // from HTTP long-polling or WebTransport
- return new Blob([data]);
- }
- case "arraybuffer":
- default:
- if (data instanceof ArrayBuffer) {
- // from HTTP long-polling (base64) or WebSocket + binaryType "arraybuffer"
- return data;
- }
- else {
- // from WebTransport (Uint8Array)
- return data.buffer;
- }
- }
- };
|