UNPKG

@fedify/redis

Version:

Redis drivers for Fedify

66 lines (62 loc) 1.56 kB
const { Temporal } = require("@js-temporal/polyfill"); const require_rolldown_runtime = require('./_virtual/rolldown_runtime.cjs'); const node_buffer = require_rolldown_runtime.__toESM(require("node:buffer")); //#region src/codec.ts /** * An error that occurs when encoding or decoding data. */ var CodecError = class extends Error { constructor(message) { super(message); this.name = "CodecError"; } }; /** * An error that occurs when encoding data. */ var EncodingError = class extends CodecError { constructor(message) { super(message); this.name = "EncodingError"; } }; /** * An error that occurs when decoding data. */ var DecodingError = class extends CodecError { constructor(message) { super(message); this.name = "DecodingError"; } }; /** * A codec that encodes and decodes JavaScript objects to and from JSON. */ var JsonCodec = class { #textEncoder = new TextEncoder(); #textDecoder = new TextDecoder(); encode(value) { let json; try { json = JSON.stringify(value); } catch (e) { if (e instanceof TypeError) throw new EncodingError(e.message); throw e; } return node_buffer.Buffer.from(this.#textEncoder.encode(json)); } decode(encoded) { const json = this.#textDecoder.decode(encoded); try { return JSON.parse(json); } catch (e) { if (e instanceof SyntaxError) throw new DecodingError(e.message); throw e; } } }; //#endregion exports.CodecError = CodecError; exports.DecodingError = DecodingError; exports.EncodingError = EncodingError; exports.JsonCodec = JsonCodec;