All files / ua Bucket.js

100% Statements 133/133
96.55% Branches 28/29
100% Functions 37/37
100% Lines 133/133

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227  56x 56x   56x 56x 56x 56x     1942x 1942x     1735x 1735x 1735x 1735x     5x     5x     204x 204x 204x     3x 3x 3x     3x 3x 3x 3x     233x 233x 233x     233x 233x 233x 233x     3x 3x 3x     3x 3x 3x 3x     33x 33x 33x     33x 33x 33x 33x     314x 314x 314x     271x 271x 271x 271x     172x 172x 172x     216x 216x 216x 216x     3x 3x 3x     3x 3x 3x 3x     3x 3x 3x     3x 3x 3x 3x     5x 5x 5x     5x 5x 5x 5x     8x 8x 8x     8x 8x 8x 8x     201x 201x 27x   174x 174x 174x       201x 201x 201x 27x 27x   174x 174x     38x 38x 16x   22x     38x 16x 16x   22x 22x     5x 5x 5x       5x 5x 5x     86x 84x     2x                 86x 84x     2x   86x     43x 43x 43x 43x     43x 43x 43x 43x 43x     1735x     56x  
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const guards_1 = require("./guards");
const encode_1 = require("./encode");
const decode_1 = __importDefault(require("./decode"));
class Bucket {
    constructor(b, position) {
        this.bytes = (b !== null && b !== void 0 ? b : new ArrayBuffer(0));
        this.position = (position !== null && position !== void 0 ? position : 0);
    }
    append(slice, elem) {
        const result = new Uint8Array(slice.byteLength + elem.byteLength);
        result.set(new Uint8Array(slice));
        result.set(new Uint8Array(elem), slice.byteLength);
        return result.buffer;
    }
    readBoolean() {
        return this.readUint8() > 0;
    }
    writeBoolean(v) {
        this.writeUint8(v ? 1 : 0);
    }
    readN(n) {
        const result = new Uint8Array(this.bytes, this.position, n);
        this.position += n;
        return result;
    }
    readInt8() {
        const dv = new DataView(this.bytes, this.position);
        this.position += 1;
        return dv.getInt8(0);
    }
    writeInt8(n) {
        const d = new Uint8Array(1);
        const dv = new DataView(d.buffer);
        dv.setInt8(0, n);
        this.write(d);
    }
    readUint8() {
        const dv = new DataView(this.bytes, this.position);
        this.position += 1;
        return dv.getUint8(0);
    }
    writeUint8(n) {
        const d = new Uint8Array(1);
        const dv = new DataView(d.buffer);
        dv.setUint8(0, n);
        this.write(d);
    }
    readInt16() {
        const dv = new DataView(this.bytes, this.position);
        this.position += 2;
        return dv.getInt16(0, true);
    }
    writeInt16(n) {
        const d = new Uint8Array(2);
        const dv = new DataView(d.buffer);
        dv.setInt16(0, n, true);
        this.write(d);
    }
    readUint16() {
        const dv = new DataView(this.bytes, this.position);
        this.position += 2;
        return dv.getUint16(0, true);
    }
    writeUint16(n) {
        const d = new Uint8Array(2);
        const dv = new DataView(d.buffer);
        dv.setUint16(0, n, true);
        this.write(d);
    }
    readInt32() {
        const dv = new DataView(this.bytes, this.position);
        this.position += 4;
        return dv.getInt32(0, true);
    }
    writeInt32(n) {
        const d = new Uint8Array(4);
        const dv = new DataView(d.buffer);
        dv.setInt32(0, n, true);
        this.write(d);
    }
    readUint32() {
        const dv = new DataView(this.bytes, this.position);
        this.position += 4;
        return dv.getUint32(0, true);
    }
    writeUint32(n) {
        const d = new Uint8Array(4);
        const dv = new DataView(d.buffer);
        dv.setUint32(0, n, true);
        this.write(d);
    }
    readInt64() {
        const dv = new DataView(this.bytes, this.position);
        this.position += 8;
        return dv.getBigInt64(0, true);
    }
    writeInt64(n) {
        const d = new Uint8Array(8);
        const dv = new DataView(d.buffer);
        dv.setBigInt64(0, n, true);
        this.write(d);
    }
    readUint64() {
        const dv = new DataView(this.bytes, this.position);
        this.position += 8;
        return dv.getBigUint64(0, true);
    }
    writeUint64(n) {
        const d = new Uint8Array(8);
        const dv = new DataView(d.buffer);
        dv.setBigUint64(0, n, true);
        this.write(d);
    }
    readFloat32() {
        const dv = new DataView(this.bytes, this.position);
        this.position += 4;
        return dv.getFloat32(0, true);
    }
    writeFloat32(n) {
        const d = new Uint8Array(4);
        const dv = new DataView(d.buffer);
        dv.setFloat32(0, n, true);
        this.write(d);
    }
    readFloat64() {
        const dv = new DataView(this.bytes, this.position);
        this.position += 8;
        return dv.getFloat64(0, true);
    }
    writeFloat64(n) {
        const d = new Uint8Array(8);
        const dv = new DataView(d.buffer);
        dv.setFloat64(0, n, true);
        this.write(d);
    }
    readString() {
        const n = this.readInt32();
        if (n === -1) {
            return '';
        }
        const d = this.readN(n);
        const decoder = new TextDecoder();
        return decoder.decode(d);
    }
    // write string with length
    writeString(s) {
        const encoder = new TextEncoder();
        const data = encoder.encode(s);
        if (data.byteLength === 0) {
            this.writeInt32(-1);
            return;
        }
        this.writeInt32(data.byteLength);
        this.write(data);
    }
    readByteString() {
        const n = this.readInt32();
        if (n === -1) {
            return new Uint8Array();
        }
        return this.readN(n);
    }
    writeByteString(b) {
        if (b.byteLength === 0) {
            this.writeInt32(-1);
            return;
        }
        this.writeInt32(b.byteLength);
        this.write(b);
    }
    readStringBytes(n) {
        const d = this.readN(n);
        const decoder = new TextDecoder();
        return decoder.decode(d);
    }
    // write string without length
    writeStringBytes(s) {
        const encoder = new TextEncoder();
        const data = encoder.encode(s);
        this.write(data);
    }
    readStruct(w) {
        if (guards_1.isDecoder(w)) {
            this.position = w.decode(this.bytes, this.position);
        }
        else {
            this.position = decode_1.default({
                bytes: this.bytes,
                instance: w,
                position: this.position
            });
        }
    }
    writeStruct(instance) {
        let d;
        if (guards_1.isEncoder(instance)) {
            d = instance.encode();
        }
        else {
            d = encode_1.encode({ instance });
        }
        this.write(d);
    }
    readDate() {
        const dv = new DataView(this.bytes, this.position);
        const value = dv.getBigInt64(0, true);
        this.position += 8;
        return new Date(Number(value - BigInt(116444736000000000)) / 1e4);
    }
    writeDate(date) {
        const b = new Uint8Array(8);
        const dv = new DataView(b.buffer);
        const value = BigInt(date.getTime()) * BigInt(1e4) + BigInt(116444736000000000);
        dv.setBigInt64(0, value, true);
        this.write(b);
    }
    write(d) {
        this.bytes = this.append(this.bytes, d);
    }
}
exports.default = Bucket;