@tgsnake/core
Version:
Pure Telegram MTProto library for nodejs
109 lines (108 loc) • 3.65 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.TLObject = void 0;
const All_js_1 = require("../All.js");
const Logger_js_1 = require("../../Logger.js");
const platform_node_js_1 = require("../../platform.node.js");
const index_js_1 = require("../index.js");
function getModule(name) {
if (!name) {
throw new Error("name of module can't be undefined");
}
if (name === 'Message') {
return index_js_1.Message;
}
else if (name === 'GzipPacked') {
return index_js_1.GzipPacked;
}
else if (name === 'MsgContainer') {
return index_js_1.MsgContainer;
}
else if (name.startsWith('Primitive')) {
return index_js_1.Primitive[name.split('.')[1]];
}
else {
const split = name.split('.');
if (split.length == 3) {
return index_js_1.Raw[split[1]][split[2]];
}
return index_js_1.Raw[split[1]];
}
}
class TLObject {
_slots;
cls = this.constructor;
constructorId;
subclassOfId;
className;
classType;
constructor() {
this._slots = [];
this.constructorId = this.cls.ID ?? 0;
this.className = 'TLObject';
}
static async read(data, ...args) {
const id = data.readUInt32LE(4);
Logger_js_1.Logger.debug(`[10] Reading TLObject with id: ${id.toString(16)} (${All_js_1.AllTLObject[id]})`);
const _class = getModule(All_js_1.AllTLObject[id]);
return await _class.read(data, ...args);
}
static write(..._args) {
return platform_node_js_1.Buffer.alloc(0);
}
read(data, ...args) {
return this.cls.read(data, ...args);
}
write(...args) {
return this.cls.write(...args);
}
[Symbol.for('nodejs.util.inspect.custom')]() {
const toPrint = {
_: this.className,
};
const ignore = ['slots', 'className', 'constructorId', 'subclassOfId', 'classType', 'cls'];
for (const key in this) {
if (Object.prototype.hasOwnProperty.call(this, key)) {
const value = this[key];
if (ignore.includes(key))
continue;
if (!key.startsWith('_') && value !== undefined && value !== null) {
toPrint[key] = value;
}
}
}
return toPrint;
}
[Symbol.for('Deno.customInspect')]() {
return String((0, platform_node_js_1.inspect)(this[Symbol.for('nodejs.util.inspect.custom')](), { colors: true }));
}
toJSON() {
const toPrint = {
_: this.className,
};
const ignore = ['slots', 'className', 'constructorId', 'subclassOfId', 'classType', 'cls'];
for (const key in this) {
if (Object.prototype.hasOwnProperty.call(this, key)) {
const value = this[key];
if (ignore.includes(key))
continue;
if (!key.startsWith('_') && value !== undefined && value !== null) {
if (typeof value === 'bigint') {
toPrint[key] = String(value);
}
else if (Array.isArray(value)) {
toPrint[key] = value.map((v) => (typeof v === 'bigint' ? String(v) : v));
}
else {
toPrint[key] = value;
}
}
}
}
return toPrint;
}
toString() {
return `[constructor of ${this.className}] ${JSON.stringify(this, null, 2)}`;
}
}
exports.TLObject = TLObject;