import PakoTypes from '@types/pako' ; import { WebSocketClient } from '@src/gateway/WebSocketClient' ; import WebSocket from 'ws' ; import { WebSocketUtils } from '@src/utils/WebSocketUtils' ; import { WebSocketPacket } from '@src/gateway/interfaces/WebSocketPacket' ; import { CompletedGatewayOptions } from '@src/gateway/interfaces/CompletedGatewayOptions' ; export function message ( client , event , options , inflate ? ) { let { data } = event , packet ; if ( data instanceof ArrayBuffer ) data = new Uint8Array ( data ) ; if ( Array . isArray ( data ) ) data = Buffer . concat ( data ) ; if ( options . compress && data instanceof Uint8Array && inflate ) { const l = data . length ; const flush = l >= 4 && data [ l - 4 ] === 0x00 && data [ l - 3 ] === 0x00 && data [ l - 2 ] === 0xff && data [ l - 1 ] === 0xff ; inflate . push ( data , flush ? WebSocketUtils . pako . Z_SYNC_FLUSH : false ) ; if ( ! flush ) return ; const decompressed = inflate . result ; if ( ! decompressed ) return ; data = Buffer . from ( decompressed ) ; if ( WebSocketUtils . encoding === 'json' ) data = data . toString ( 'utf8' ) ; } try { packet = WebSocketUtils . unpack ( data ) ; } catch ( e ) { } return packet as WebSocketPacket | undefined ; }