stream#TransformCallback TypeScript Examples

The following examples show how to use stream#TransformCallback. 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: mavlink.ts    From node-mavlink with GNU Lesser General Public License v3.0 6 votes vote down vote up
_transform(chunk: Buffer, encoding, callback: TransformCallback) {
    const protocol = this.getProtocol(chunk)
    const header = protocol.header(chunk)
    const payload = protocol.payload(chunk)
    const crc = protocol.crc(chunk)
    const signature = protocol instanceof MavLinkProtocolV2
      ? protocol.signature(chunk, header)
      : null

    const packet = new MavLinkPacket(chunk, header, payload, crc, protocol, signature)

    callback(null, packet)
  }
Example #2
Source File: DigestTransform.ts    From electron-request with MIT License 6 votes vote down vote up
// noinspection JSUnusedGlobalSymbols
  _flush(callback: TransformCallback): void {
    this._actual = this.digester.digest(this.encoding);

    if (this.isValidateOnEnd) {
      try {
        this.validate();
      } catch (e) {
        callback(e as Error);
        return;
      }
    }

    callback(null);
  }
Example #3
Source File: ProgressCallbackTransform.ts    From electron-request with MIT License 6 votes vote down vote up
_transform(chunk: Buffer, encoding: BufferEncoding, callback: TransformCallback) {
    const chunkLength = chunk.length;
    this.transferred += chunkLength;
    this.delta += chunkLength;

    if (this.total >= this.transferred) {
      const now = Date.now();
      if (now >= this.nextUpdate) {
        this.nextUpdate = now + 1000;
        this.onProgress({
          total: this.total,
          delta: this.delta,
          transferred: this.transferred,
          percent: (this.transferred / this.total) * 100,
          bytesPerSecond: Math.round(this.transferred / ((now - this.start) / 1000)),
        });
        this.delta = 0;
      }
    }

    callback(null, chunk);
  }
Example #4
Source File: ProgressCallbackTransform.ts    From electron-request with MIT License 6 votes vote down vote up
_flush(callback: TransformCallback): void {
    const { total, transferred } = this;
    const totalChunk = transferred > total ? transferred : total;

    this.onProgress({
      total: totalChunk,
      delta: this.delta,
      transferred: totalChunk,
      percent: 100,
      bytesPerSecond: Math.round(this.transferred / ((Date.now() - this.start) / 1000)),
    });
    this.delta = 0;

    callback(null);
  }
Example #5
Source File: mavlink.ts    From node-mavlink with GNU Lesser General Public License v3.0 5 votes vote down vote up
_transform(chunk: Buffer, encoding, callback: TransformCallback) {
    this.buffer = Buffer.concat([ this.buffer, chunk ])

    while (this.buffer.byteLength > 0) {
      const offset = this.findStartOfPacket(this.buffer)
      if (offset === null) {
        // start of the package was not found - need more data
        break
      }

      // fast-forward the buffer to the first start byte
      if (offset > 0) {
        this.buffer = this.buffer.slice(offset)
      }

      this.log.debug('Found potential packet start at', offset)

      // get protocol this buffer is encoded with
      const Protocol = this.getPacketProtocol(this.buffer)

      this.log.debug('Packet protocol is', Protocol.NAME)

      // check if the buffer contains at least the minumum size of data
      if (this.buffer.length < Protocol.PAYLOAD_OFFSET + MavLinkProtocol.CHECKSUM_LENGTH) {
        // current buffer shorter than the shortest message - skipping
        this.log.debug('Current buffer shorter than the shortest message - skipping')
        break
      }

      // check if the current buffer contains the entire message
      const expectedBufferLength = this.readPacketLength(this.buffer, Protocol)
      this.log.debug('Expected buffer length:', expectedBufferLength, `(${hex(expectedBufferLength)})`)
      if (this.buffer.length < expectedBufferLength) {
        // current buffer is not fully retrieved yet - skipping
        this.log.debug('Current buffer is not fully retrieved yet - skipping')
        break
      } else {
        this.log.debug('Current buffer length:', this.buffer.length, `(${hex(this.buffer.length, 4)})`)
      }

      // retrieve the buffer based on payload size
      const buffer = this.buffer.slice(0, expectedBufferLength)
      this.log.debug('Recognized buffer length:', buffer.length, `(${hex(buffer.length, 2)})`)

      switch (this.validatePacket(buffer, Protocol)) {
        case PacketValidationResult.VALID:
          this.log.debug('Found a valid packet')
          this._validPackagesCount++
          this.push(buffer)
          // truncate the buffer to remove the current message
          this.buffer = this.buffer.slice(expectedBufferLength)
          break
        case PacketValidationResult.INVALID:
          this.log.debug('Found an invalid packet - skipping')
          this._invalidPackagesCount++
          // truncate the buffer to remove the wrongly identified STX
          this.buffer = this.buffer.slice(1)
          break
        case PacketValidationResult.UNKNOWN:
          this.log.debug('Found an unknown packet - skipping')
          this._unknownPackagesCount++
          // truncate the buffer to remove the current message
          this.buffer = this.buffer.slice(expectedBufferLength)
          break
      }
    }

    callback(null)
  }
Example #6
Source File: hash-instance.ts    From blake3 with MIT License 5 votes vote down vote up
/**
   * @inheritdoc
   * @hidden
   */
  _transform(chunk: Buffer | string, encoding: string, callback: TransformCallback): void {
    this.update(chunk, encoding as BufferEncoding);
    callback();
  }
Example #7
Source File: hash-instance.ts    From blake3 with MIT License 5 votes vote down vote up
/**
   * @inheritdoc
   * @hidden
   */
  _flush(callback: TransformCallback): void {
    callback(null, this.digest());
  }
Example #8
Source File: DigestTransform.ts    From electron-request with MIT License 5 votes vote down vote up
// noinspection JSUnusedGlobalSymbols
  _transform(chunk: Buffer, encoding: BufferEncoding, callback: TransformCallback) {
    this.digester.update(chunk);
    callback(null, chunk);
  }
Example #9
Source File: bean-converter-stream.ts    From redbean-node with MIT License 5 votes vote down vote up
_transform(chunk: any, encoding: BufferEncoding, callback: TransformCallback) {
        let bean = this.R.convertToBean(this.type, chunk);
        super._transform(bean, encoding, callback);
    }
Example #10
Source File: bean-converter-stream.ts    From redbean-node with MIT License 5 votes vote down vote up
_transform(chunk: any, encoding: BufferEncoding, callback: TransformCallback) {
        let bean = this.R.convertToBean(this.type, chunk);
        super._transform(bean, encoding, callback);
    }