zlib#deflate TypeScript Examples

The following examples show how to use zlib#deflate. 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: clientRequestManager.ts    From flood with GNU General Public License v3.0 6 votes vote down vote up
private async methodCall(request: [string, RencodableArray, RencodableObject], auth = true): Promise<RencodableData> {
    const rpc = await (auth ? this.rpcWithAuth : this.rpc);
    if (rpc == undefined) {
      throw new Error('RPC is not connected.');
    }

    const requestId = this.requestId++;

    return await new Promise<RencodableData>((resolve, reject) => {
      deflate(encode([[requestId, ...request]]), (err, payloadBuf) => {
        if (err) {
          reject(err);
          return;
        }

        const {length} = payloadBuf;
        if (length > 0xff_ff_ff_ff) {
          reject(new Error('Payload is too large.'));
          return;
        }

        const lengthBuf = Buffer.alloc(4);
        lengthBuf.writeUInt32BE(length, 0);

        this.requestQueue[requestId] = [resolve, reject];
        rpc.write(Buffer.concat([protocolVerBuf, lengthBuf, payloadBuf]));
      });
    });
  }