UNPKG

@fedify/redis

Version:

Redis drivers for Fedify

62 lines (58 loc) 1.34 kB
import { Temporal } from "@js-temporal/polyfill"; import { Buffer } from "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 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 export { CodecError, DecodingError, EncodingError, JsonCodec };