async compute()

in source/checksum/lib/algorithm/md5.js [41:101]


  async compute() {
    /* shouldn't be here */
    if (this.computed) {
      throw new ComputedChecksumExistError();
    }

    await this.initLib();

    const responseData = await new Promise((resolve, reject) => {
      const spark = new SPARK.ArrayBuffer();
      if (this.intermediateHash) {
        spark.setState(this.intermediateHash);
      }

      const [start, end] = this.calculateByteRange();
      const range = (this.byteStart === 0 && this.fileSize === 0)
        ? undefined
        : `bytes=${start}-${end}`;

      const s3 = new AWS.S3({
        apiVersion: '2006-03-01',
        signatureVersion: 'v4',
        customUserAgent: process.env.ENV_CUSTOM_USER_AGENT,
      });

      const stream = s3.getObject({
        Bucket: this.bucket,
        Key: this.key,
        Range: range,
        IfMatch: this.etag,
      }).createReadStream();

      stream.on('error', e =>
        reject(e));

      stream.on('data', async (data) => {
        this.bytesRead += data.length;
        spark.append(data);
      });

      stream.on('end', async () => {
        this.t1 = new Date();
        const byteProcessed = this.byteStart + this.bytesRead;
        if (byteProcessed > this.fileSize) {
          reject(new MismatchFileSizeError(`byte processed (${byteProcessed}) larger than the actual file size (${this.fileSize})`));
          return;
        }
        if (byteProcessed === this.fileSize) {
          this.computed = spark.end();
          /* signal base class to record the end time */
          this.setElapsed();
          this.status = 'COMPLETED';
        } else {
          this.intermediateHash = spark.getState();
        }
        resolve(this.responseData());
      });
    });

    return responseData;
  }