UNPKG

@deno/kv

Version:

A Deno KV client library optimized for Node.js.

72 lines (71 loc) 2.69 kB
// @ts-nocheck import { encodeJson as encodeJson_1, decodeJson as decodeJson_1, encodeBinary as encodeBinary_1, decodeBinary as decodeBinary_1, } from "./KvEntry.js"; import { tsValueToJsonValueFns, jsonValueToTsValueFns, } from "../../../../../runtime/json/scalar.js"; import { WireType, } from "../../../../../runtime/wire/index.js"; import { default as serialize, } from "../../../../../runtime/wire/serialize.js"; import { tsValueToWireValueFns, wireValueToTsValueFns, } from "../../../../../runtime/wire/scalar.js"; import { default as deserialize, } from "../../../../../runtime/wire/deserialize.js"; export function getDefaultValue() { return { changed: false, entryIfChanged: undefined, }; } export function createValue(partialValue) { return { ...getDefaultValue(), ...partialValue, }; } export function encodeJson(value) { const result = {}; if (value.changed !== undefined) result.changed = tsValueToJsonValueFns.bool(value.changed); if (value.entryIfChanged !== undefined) result.entryIfChanged = encodeJson_1(value.entryIfChanged); return result; } export function decodeJson(value) { const result = getDefaultValue(); if (value.changed !== undefined) result.changed = jsonValueToTsValueFns.bool(value.changed); if (value.entryIfChanged !== undefined) result.entryIfChanged = decodeJson_1(value.entryIfChanged); return result; } export function encodeBinary(value) { const result = []; if (value.changed !== undefined) { const tsValue = value.changed; result.push([1, tsValueToWireValueFns.bool(tsValue)]); } if (value.entryIfChanged !== undefined) { const tsValue = value.entryIfChanged; result.push([2, { type: WireType.LengthDelimited, value: encodeBinary_1(tsValue) }]); } return serialize(result); } export function decodeBinary(binary) { const result = getDefaultValue(); const wireMessage = deserialize(binary); const wireFields = new Map(wireMessage); field: { const wireValue = wireFields.get(1); if (wireValue === undefined) break field; const value = wireValueToTsValueFns.bool(wireValue); if (value === undefined) break field; result.changed = value; } field: { const wireValue = wireFields.get(2); if (wireValue === undefined) break field; const value = wireValue.type === WireType.LengthDelimited ? decodeBinary_1(wireValue.value) : undefined; if (value === undefined) break field; result.entryIfChanged = value; } return result; }