@hazae41/kdbx
Version:
Rust-like KeePass (KDBX 4) file format for TypeScript
64 lines (61 loc) • 2.02 kB
JavaScript
import { Opaque, Readable, Writable } from '@hazae41/binary';
import { Cursor } from '@hazae41/cursor';
import { TLV } from '../../../libs/tlv/index.mjs';
class Vector {
bytes;
value;
constructor(bytes, value) {
this.bytes = bytes;
this.value = value;
}
sizeOrThrow() {
return this.bytes.sizeOrThrow();
}
writeOrThrow(cursor) {
this.bytes.writeOrThrow(cursor);
}
cloneOrThrow() {
return Readable.readFromBytesOrThrow(Vector, Writable.writeToBytesOrThrow(this));
}
}
(function (Vector) {
function initOrThrow(indexed) {
const entries = new Array();
for (const key of Object.keys(indexed)) {
const index = Number(key);
const array = indexed[index];
if (array == null)
continue;
for (const value of array)
entries.push(new TLV(index, value));
continue;
}
const sized = entries.reduce((x, r) => x + r.sizeOrThrow(), 0) + TLV.Empty.sizeOrThrow();
const bytes = new Opaque(new Uint8Array(sized));
const cursor = new Cursor(bytes.bytes);
for (const entry of entries)
entry.writeOrThrow(cursor);
TLV.Empty.writeOrThrow(cursor);
return new Vector(bytes, indexed);
}
Vector.initOrThrow = initOrThrow;
function readOrThrow(cursor) {
const start = cursor.offset;
const entries = new Array();
const indexed = {};
while (true) {
const tlv = TLV.readOrThrow(cursor);
if (tlv.type === 0)
break;
entries.push(tlv);
indexed[tlv.type] ??= [];
indexed[tlv.type].push(tlv.value);
continue;
}
const bytes = new Opaque(cursor.bytes.subarray(start, cursor.offset));
return new Vector(bytes, indexed);
}
Vector.readOrThrow = readOrThrow;
})(Vector || (Vector = {}));
export { Vector };
//# sourceMappingURL=index.mjs.map