1/*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16declare function requireNapi(napiModuleName: string): any;
17const stream = requireNapi('util.stream');
18const hash = requireNapi('file.hash');
19
20class HashStream extends stream.Transform {
21    // @ts-ignore
22    hs: hash.HashStream;
23    hashBuf?: ArrayBuffer;
24
25    constructor(algorithm: string) {
26        super();
27        this.hs = new hash.HashStream(algorithm);
28    }
29
30    digest(): string {
31        return this.hs.digest();
32    }
33
34    update(data: ArrayBuffer): void {
35        this.hs.update(data);
36    }
37
38    doTransform(chunk: string, encoding: string, callback: Function): void {
39        const buf = new Uint8Array(chunk.split('').map(x => x.charCodeAt(0))).buffer;
40        this.hs.update(buf);
41        this.push(chunk);
42        callback();
43    }
44
45    doWrite(chunk: string | Uint8Array, encoding: string, callback: Function): void {
46        callback();
47    }
48
49    doFlush(callback: Function): void {
50        callback();
51    }
52}
53
54export default {
55    HashStream: HashStream,
56};
57