File size: 2,090 Bytes
5641073
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/**
 * The maximum number of bytes that can be read from the hash.
 *
 * Calculated out 2^64-1, since `Xn` syntax (for `Xn ** Yn`) requires TS
 * targeting esnext/es2020 which includes features that Node 10 doesn't
 * yet supported.
 */
exports.maxHashBytes = BigInt('18446744073709551615');
/**
 * Base hash reader implementation.
 */
class BaseHashReader {
    constructor(reader) {
        this.pos = BigInt(0);
        this.reader = reader;
    }
    get position() {
        return this.pos;
    }
    set position(value) {
        var _a;
        // to avoid footguns of people using numbers:
        if (typeof value !== 'bigint') {
            throw new Error(`Got a ${typeof value} set in to reader.position, expected a bigint`);
        }
        this.boundsCheck(value);
        this.pos = value;
        (_a = this.reader) === null || _a === void 0 ? void 0 : _a.set_position(value);
    }
    /**
     * @inheritdoc
     */
    readInto(target) {
        if (!this.reader) {
            throw new Error(`Cannot read from a hash after it was disposed`);
        }
        const next = this.pos + BigInt(target.length);
        this.boundsCheck(next);
        this.reader.fill(target);
        this.position = next;
    }
    /**
     * @inheritdoc
     */
    read(bytes) {
        const data = this.alloc(bytes);
        this.readInto(data);
        return data;
    }
    /**
     * @inheritdoc
     */
    dispose() {
        var _a, _b;
        (_b = (_a = this.reader) === null || _a === void 0 ? void 0 : _a.free) === null || _b === void 0 ? void 0 : _b.call(_a);
        this.reader = undefined;
    }
    boundsCheck(position) {
        if (position > exports.maxHashBytes) {
            throw new RangeError(`Cannot read past ${exports.maxHashBytes} bytes in BLAKE3 hashes`);
        }
        if (position < BigInt(0)) {
            throw new RangeError(`Cannot read to a negative position`);
        }
    }
}
exports.BaseHashReader = BaseHashReader;
//# sourceMappingURL=hash-reader.js.map