@cerbos/embedded
Version:
Client library for interacting with embedded Cerbos policy decision points generated by Cerbos Hub from server-side Node.js and browser-based applications
45 lines • 1.37 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Slice = void 0;
const utf8Decoder = new TextDecoder("utf-8", { fatal: true });
const utf8Encoder = new TextEncoder();
class Slice {
offset;
length;
static from(allocator, offsetAndLength) {
return new Slice(allocator, Number(offsetAndLength >> BigInt(32)), Number(offsetAndLength & BigInt(0xffffffff)));
}
static ofJSON(allocator, data) {
return Slice.ofString(allocator, JSON.stringify(data));
}
static ofString(allocator, data) {
const bytes = utf8Encoder.encode(data);
const slice = new Slice(allocator, Number(allocator.allocate(bytes.length)), bytes.length);
try {
slice.copy(bytes);
return slice;
}
catch (error) {
slice.deallocate();
throw error;
}
}
deallocate;
bytes;
constructor({ memory, deallocate }, offset, length) {
this.offset = offset;
this.length = length;
this.deallocate = () => {
deallocate(offset, length);
};
this.bytes = new Uint8Array(memory.buffer, offset, length);
}
text() {
return utf8Decoder.decode(this.bytes);
}
copy(bytes) {
this.bytes.set(bytes);
}
}
exports.Slice = Slice;
//# sourceMappingURL=slice.js.map