async compute()

in source/checksum/lib/algorithm/sha1.js [43:116]


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

    await this.initLib();

    const responseData = await new Promise((resolve, reject) => {
      const rusha = new RUSHA(DEFAULT_HEAPSIZE);
      rusha.resetState();

      if (this.intermediateHash) {
        const buf = Buffer.from(this.intermediateHash.heap, 'base64');
        const state = {
          offset: this.intermediateHash.offset,
          /* convert Base64 Buffer back to ArrayBuffer */
          heap: buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength),
        };
        rusha.setState(state);
      }

      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;
        rusha.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 = rusha.end();
          /* signal base class to record the end time */
          this.setElapsed();
          this.status = 'COMPLETED';
        } else {
          const state = rusha.getState();
          this.intermediateHash = {
            offset: state.offset,
            /* convert ArrayBuffer to Base64 Buffer string */
            heap: Buffer.from(state.heap).toString('base64'),
          };
        }
        resolve(this.responseData());
      });
    });
    return responseData;
  }