@tgsnake/core
Version:
Pure Telegram MTProto library for nodejs
83 lines (82 loc) • 2.59 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.File = void 0;
const platform_node_js_1 = require("../platform.node.js");
const index_js_1 = require("../raw/index.js");
class File extends platform_node_js_1.Duplex {
_bytes;
constructor() {
super({
readableHighWaterMark: 512 * 1024,
writableHighWaterMark: 512 * 1024,
});
this._bytes = new index_js_1.BytesIO();
}
_write(chunk, encoding, next) {
this._bytes.write(platform_node_js_1.Buffer.from(chunk, encoding));
return next();
}
_read(length) {
if (length) {
if (this._bytes.length > 0) {
const bytes = this._bytes.read(length);
if (platform_node_js_1.Buffer.byteLength(bytes) > 0) {
return bytes;
}
}
}
else {
if (this._bytes.length > 0) {
return this._bytes.buffer;
}
}
return null;
}
push(chunk, encoding) {
return super.push(chunk, encoding);
}
on(event, callback) {
return super.on(event, callback);
}
pipe(destination, options) {
return super.pipe(destination, options);
}
get bytes() {
return this._bytes;
}
[Symbol.for('nodejs.util.inspect.custom')]() {
const toPrint = {
_: this.constructor.name,
};
for (const key in this) {
if (Object.prototype.hasOwnProperty.call(this, key)) {
const value = this[key];
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.constructor.name,
};
for (const key in this) {
if (Object.prototype.hasOwnProperty.call(this, key)) {
const value = this[key];
if (!key.startsWith('_') && value !== undefined && value !== null) {
toPrint[key] = typeof value === 'bigint' ? String(value) : value;
}
}
}
return toPrint;
}
toString() {
return `[constructor of ${this.constructor.name}] ${JSON.stringify(this, null, 2)}`;
}
}
exports.File = File;