zlib#inflate TypeScript Examples

The following examples show how to use zlib#inflate. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: gameService.ts    From project-tauntaun with GNU Lesser General Public License v3.0 6 votes vote down vote up
async function receiveUpdateMessage(event: any) {
  const time_end = performance.now();
  if (time_start !== null) {
    console.log(`Roundtrip: ${time_end - time_start}ms`);
    time_start = null;
  }

  const data = Buffer.from(Buffer.from(event.data, 'base64'), 4);
  const do_unzip = promisify(inflate);
  const unzipped_data = await do_unzip(data);

  const message = JSON.parse(unzipped_data as unknown as string);
  if (message.key === 'mission_updated') {
    Object.keys(missionUpdateListeners).forEach(key => missionUpdateListeners[key](message.value));
  } else if (message.key === 'sessionid') {
    Object.keys(sessionIdUpdateListeners).forEach(key => sessionIdUpdateListeners[key](message.value));
  } else if (message.key === 'sessions_updated') {
    Object.keys(sessionsUpdateListeners).forEach(key => sessionsUpdateListeners[key](message.value));
  } else {
    Object.keys(genericUpdateListeners).forEach(key => genericUpdateListeners[key](message));
  }
}
Example #2
Source File: clientRequestManager.ts    From flood with GNU General Public License v3.0 5 votes vote down vote up
private connect(): Promise<tls.TLSSocket> {
    return new Promise<tls.TLSSocket>((resolve, reject) => {
      Object.keys(this.requestQueue).forEach((id) => {
        const idAsNumber = Number(id);
        const [, rejectRequest] = this.requestQueue[idAsNumber];
        rejectRequest(new Error('Session is no longer active.'));
      });

      this.requestId = 0;
      this.requestQueue = {};

      let rpcBufferSize = 0;
      let rpcBuffer: Buffer | null = null;

      const tlsSocket = tls.connect({
        host: this.connectionSettings.host,
        port: this.connectionSettings.port,
        timeout: 30,
        rejectUnauthorized: false,
      });

      const handleError = (e: Error) => {
        tlsSocket.destroy();
        this.rpcWithAuth = this.rpc = Promise.reject();
        reject(e);
      };

      tlsSocket.once('error', handleError);
      tlsSocket.once('close', handleError);

      tlsSocket.on('secureConnect', () => {
        tlsSocket.on('data', (chunk: Buffer) => {
          if (rpcBuffer != null) {
            rpcBuffer = Buffer.concat([rpcBuffer, chunk], rpcBufferSize);
          } else {
            if (chunk[0] !== DELUGE_RPC_PROTOCOL_VERSION) {
              handleError(new Error('Unexpected Deluge RPC version.'));
              return;
            }

            rpcBufferSize = chunk.slice(1, 5).readUInt32BE(0);
            rpcBuffer = chunk.slice(5);
          }

          if (rpcBuffer.length >= rpcBufferSize) {
            const buf = rpcBuffer;
            rpcBuffer = null;
            rpcBufferSize = 0;
            inflate(buf, (err, data) => {
              if (err) {
                handleError(err);
                return;
              }

              this.receive(data);
            });
          }
        });

        resolve(tlsSocket);
      });
    });
  }