UNPKG

tdlib-native

Version:

🚀 Telegram TDLib native nodejs wrapper

29 lines (28 loc) • 1 kB
"use strict"; Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); const base64 = require("./base64.js"); const types = require("./generated/types.js"); const tdTypename = "@type"; const mainKey = new RegExp(`"${types.typename}":`, "g"); const tdKey = new RegExp(`"${tdTypename}":`, "g"); function replacer(_key, value) { if (typeof value === "bigint") { return value.toString(); } if (typeof value === "object" && value instanceof Uint8Array) { return base64.encode(value); } if (typeof value === "object" && value && "type" in value && value.type === "Buffer" && "data" in value && Array.isArray(value.data)) { return base64.encode(value.data); } return value; } function serialize(data) { const json = JSON.stringify(data, replacer); return json.replace(mainKey, `"${tdTypename}":`); } function deserialize(json) { return JSON.parse(json.replaceAll(tdKey, `"${types.typename}":`)); } exports.deserialize = deserialize; exports.serialize = serialize;