UNPKG

tdlib-native

Version:

🚀 Telegram TDLib native nodejs wrapper

29 lines (28 loc) • 868 B
import { encode } from "./base64.mjs"; import { typename } from "./generated/types.mjs"; const tdTypename = "@type"; const mainKey = new RegExp(`"${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 encode(value); } if (typeof value === "object" && value && "type" in value && value.type === "Buffer" && "data" in value && Array.isArray(value.data)) { return 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, `"${typename}":`)); } export { deserialize, serialize };