@mtcute/node 0.16.13 → 0.18.0-rc.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/client.cjs +101 -0
- package/client.d.ts +2 -9
- package/client.js +96 -0
- package/index.cjs +29 -423
- package/index.d.ts +2 -1
- package/index.js +13 -397
- package/{chunks/cjs/BpvQ752Q.js → methods/download-file.cjs} +3 -7
- package/{chunks/es/CnOjjhdK.js → methods/download-file.js} +1 -6
- package/methods/download-node-stream.cjs +13 -0
- package/methods/download-node-stream.d.ts +1 -1
- package/methods/download-node-stream.js +8 -0
- package/methods.cjs +5 -4
- package/methods.js +2 -1
- package/package.json +19 -16
- package/sqlite/driver.cjs +27 -0
- package/sqlite/driver.d.ts +1 -1
- package/sqlite/driver.js +22 -0
- package/sqlite/index.cjs +18 -0
- package/sqlite/index.d.ts +1 -1
- package/sqlite/index.js +13 -0
- package/{chunks/cjs/D7i-4e0W.js → utils/crypto.cjs} +4 -3
- package/{chunks/es/HZgHrOPU.js → utils/crypto.js} +1 -1
- package/utils/exit-hook.cjs +46 -0
- package/utils/exit-hook.js +41 -0
- package/utils/logging.cjs +33 -0
- package/utils/logging.js +28 -0
- package/utils/normalize-file.cjs +33 -0
- package/utils/normalize-file.js +28 -0
- package/utils/platform.cjs +44 -0
- package/{common-internals-node → utils}/platform.d.ts +1 -8
- package/utils/platform.js +22 -0
- package/utils/proxies.cjs +73 -0
- package/utils/proxies.d.ts +34 -0
- package/utils/proxies.js +63 -0
- package/utils/tcp.cjs +18 -0
- package/utils/tcp.d.ts +6 -28
- package/utils/tcp.js +13 -0
- package/utils.cjs +3 -6
- package/utils.d.ts +0 -1
- package/utils.js +2 -5
- package/worker.cjs +49 -0
- package/worker.d.ts +5 -3
- package/worker.js +44 -0
- package/chunks/cjs/HP2yqAk_.js +0 -79
- package/chunks/cjs/package.json +0 -3
- package/chunks/es/CKso6cAV.js +0 -75
- package/index.d.cts +0 -8
- package/methods.d.cts +0 -3
- package/utils/stream-utils.d.ts +0 -3
- package/utils/stream-utils.test.d.ts +0 -1
- package/utils/tcp.test.d.ts +0 -1
- package/utils/version.d.ts +0 -3
- package/utils.d.cts +0 -3
- /package/{common-internals-node → utils}/exit-hook.d.ts +0 -0
- /package/{common-internals-node → utils}/logging.d.ts +0 -0
package/client.cjs
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
if (typeof globalThis !== "undefined" && !globalThis._MTCUTE_CJS_DEPRECATION_WARNED) {
|
|
2
|
+
globalThis._MTCUTE_CJS_DEPRECATION_WARNED = true;
|
|
3
|
+
console.warn("[mtcute-workspace] CommonJS support is deprecated and will be removed in 0.20.0. Please consider switching to ESM, it's " + (/* @__PURE__ */ new Date()).getFullYear() + " already.");
|
|
4
|
+
console.warn("[mtcute-workspace] Learn more about switching to ESM: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c");
|
|
5
|
+
}
|
|
6
|
+
"use strict";
|
|
7
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
8
|
+
const node_readline = require("node:readline");
|
|
9
|
+
const utils = require("@fuman/utils");
|
|
10
|
+
const client_js = require("@mtcute/core/client.js");
|
|
11
|
+
const downloadFile = require("./methods/download-file.cjs");
|
|
12
|
+
const downloadNodeStream = require("./methods/download-node-stream.cjs");
|
|
13
|
+
const index = require("./sqlite/index.cjs");
|
|
14
|
+
const crypto = require("./utils/crypto.cjs");
|
|
15
|
+
const platform = require("./utils/platform.cjs");
|
|
16
|
+
const tcp = require("./utils/tcp.cjs");
|
|
17
|
+
let nativeCrypto;
|
|
18
|
+
try {
|
|
19
|
+
nativeCrypto = require("@mtcute/crypto-node").NodeNativeCryptoProvider;
|
|
20
|
+
} catch {
|
|
21
|
+
}
|
|
22
|
+
class BaseTelegramClient extends client_js.BaseTelegramClient {
|
|
23
|
+
constructor(opts) {
|
|
24
|
+
super({
|
|
25
|
+
// eslint-disable-next-line
|
|
26
|
+
crypto: nativeCrypto ? new nativeCrypto() : new crypto.NodeCryptoProvider(),
|
|
27
|
+
transport: new tcp.TcpTransport(),
|
|
28
|
+
platform: new platform.NodePlatform(),
|
|
29
|
+
...opts,
|
|
30
|
+
storage: typeof opts.storage === "string" ? new index.SqliteStorage(opts.storage) : opts.storage ?? new index.SqliteStorage("client.session")
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
class TelegramClient extends client_js.TelegramClient {
|
|
35
|
+
constructor(opts) {
|
|
36
|
+
if ("client" in opts) {
|
|
37
|
+
super(opts);
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
super({
|
|
41
|
+
client: new BaseTelegramClient(opts),
|
|
42
|
+
disableUpdates: opts.disableUpdates,
|
|
43
|
+
skipConversationUpdates: opts.skipConversationUpdates,
|
|
44
|
+
updates: opts.updates
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
_rl;
|
|
48
|
+
/**
|
|
49
|
+
* Tiny wrapper over Node `readline` package
|
|
50
|
+
* for simpler user input for `.start()` method.
|
|
51
|
+
*
|
|
52
|
+
* Associated `readline` interface is closed
|
|
53
|
+
* after `start()` returns, or with the client.
|
|
54
|
+
*
|
|
55
|
+
* @param text Text of the question
|
|
56
|
+
*/
|
|
57
|
+
input(text) {
|
|
58
|
+
if (!this._rl) {
|
|
59
|
+
this._rl = node_readline.createInterface({
|
|
60
|
+
input: process.stdin,
|
|
61
|
+
output: process.stdout
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
return new Promise((res) => this._rl?.question(text, res));
|
|
65
|
+
}
|
|
66
|
+
close() {
|
|
67
|
+
this._rl?.close();
|
|
68
|
+
return super.close();
|
|
69
|
+
}
|
|
70
|
+
start(params = {}) {
|
|
71
|
+
if (!params.botToken) {
|
|
72
|
+
if (!params.phone) params.phone = () => this.input("phone > ");
|
|
73
|
+
if (!params.code) params.code = () => this.input("code > ");
|
|
74
|
+
if (!params.password) {
|
|
75
|
+
params.password = () => this.input("2fa password > ");
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return super.start(params).then((user) => {
|
|
79
|
+
if (this._rl) {
|
|
80
|
+
this._rl.close();
|
|
81
|
+
delete this._rl;
|
|
82
|
+
}
|
|
83
|
+
return user;
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
run(params, then) {
|
|
87
|
+
if (typeof params === "function") {
|
|
88
|
+
then = params;
|
|
89
|
+
params = {};
|
|
90
|
+
}
|
|
91
|
+
this.start(params).then(then).catch((err) => this.onError.emit(utils.unknownToError(err)));
|
|
92
|
+
}
|
|
93
|
+
downloadToFile(filename, location, params) {
|
|
94
|
+
return downloadFile.downloadToFile(this, filename, location, params);
|
|
95
|
+
}
|
|
96
|
+
downloadAsNodeStream(location, params) {
|
|
97
|
+
return downloadNodeStream.downloadAsNodeStream(this, location, params);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
exports.BaseTelegramClient = BaseTelegramClient;
|
|
101
|
+
exports.TelegramClient = TelegramClient;
|
package/client.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { Readable } from 'node:stream';
|
|
2
1
|
import { FileDownloadLocation, FileDownloadParameters, ITelegramStorageProvider, PartialOnly, User } from '@mtcute/core';
|
|
3
2
|
import { BaseTelegramClientOptions as BaseTelegramClientOptionsBase, TelegramClientOptions, BaseTelegramClient as BaseTelegramClientBase, TelegramClient as TelegramClientBase } from '@mtcute/core/client.js';
|
|
3
|
+
import { Readable } from 'node:stream';
|
|
4
4
|
export type { TelegramClientOptions };
|
|
5
|
-
export interface BaseTelegramClientOptions extends PartialOnly<Omit<BaseTelegramClientOptionsBase, 'storage'>, 'transport' | 'crypto'> {
|
|
5
|
+
export interface BaseTelegramClientOptions extends PartialOnly<Omit<BaseTelegramClientOptionsBase, 'storage'>, 'transport' | 'crypto' | 'platform'> {
|
|
6
6
|
/**
|
|
7
7
|
* Storage to use for this client.
|
|
8
8
|
*
|
|
@@ -12,13 +12,6 @@ export interface BaseTelegramClientOptions extends PartialOnly<Omit<BaseTelegram
|
|
|
12
12
|
* @default `"client.session"`
|
|
13
13
|
*/
|
|
14
14
|
storage?: string | ITelegramStorageProvider;
|
|
15
|
-
/**
|
|
16
|
-
* **ADVANCED USE ONLY**
|
|
17
|
-
*
|
|
18
|
-
* Whether to not set up the platform.
|
|
19
|
-
* This is useful if you call `setPlatform` yourself.
|
|
20
|
-
*/
|
|
21
|
-
platformless?: boolean;
|
|
22
15
|
}
|
|
23
16
|
export declare class BaseTelegramClient extends BaseTelegramClientBase {
|
|
24
17
|
constructor(opts: BaseTelegramClientOptions);
|
package/client.js
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { createInterface } from "node:readline";
|
|
2
|
+
import { unknownToError } from "@fuman/utils";
|
|
3
|
+
import { BaseTelegramClient as BaseTelegramClient$1, TelegramClient as TelegramClient$1 } from "@mtcute/core/client.js";
|
|
4
|
+
import { downloadToFile } from "./methods/download-file.js";
|
|
5
|
+
import { downloadAsNodeStream } from "./methods/download-node-stream.js";
|
|
6
|
+
import { SqliteStorage } from "./sqlite/index.js";
|
|
7
|
+
import { NodeCryptoProvider } from "./utils/crypto.js";
|
|
8
|
+
import { NodePlatform } from "./utils/platform.js";
|
|
9
|
+
import { TcpTransport } from "./utils/tcp.js";
|
|
10
|
+
let nativeCrypto;
|
|
11
|
+
try {
|
|
12
|
+
nativeCrypto = (await import("@mtcute/crypto-node")).NodeNativeCryptoProvider;
|
|
13
|
+
} catch {
|
|
14
|
+
}
|
|
15
|
+
class BaseTelegramClient extends BaseTelegramClient$1 {
|
|
16
|
+
constructor(opts) {
|
|
17
|
+
super({
|
|
18
|
+
// eslint-disable-next-line
|
|
19
|
+
crypto: nativeCrypto ? new nativeCrypto() : new NodeCryptoProvider(),
|
|
20
|
+
transport: new TcpTransport(),
|
|
21
|
+
platform: new NodePlatform(),
|
|
22
|
+
...opts,
|
|
23
|
+
storage: typeof opts.storage === "string" ? new SqliteStorage(opts.storage) : opts.storage ?? new SqliteStorage("client.session")
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
class TelegramClient extends TelegramClient$1 {
|
|
28
|
+
constructor(opts) {
|
|
29
|
+
if ("client" in opts) {
|
|
30
|
+
super(opts);
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
super({
|
|
34
|
+
client: new BaseTelegramClient(opts),
|
|
35
|
+
disableUpdates: opts.disableUpdates,
|
|
36
|
+
skipConversationUpdates: opts.skipConversationUpdates,
|
|
37
|
+
updates: opts.updates
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
_rl;
|
|
41
|
+
/**
|
|
42
|
+
* Tiny wrapper over Node `readline` package
|
|
43
|
+
* for simpler user input for `.start()` method.
|
|
44
|
+
*
|
|
45
|
+
* Associated `readline` interface is closed
|
|
46
|
+
* after `start()` returns, or with the client.
|
|
47
|
+
*
|
|
48
|
+
* @param text Text of the question
|
|
49
|
+
*/
|
|
50
|
+
input(text) {
|
|
51
|
+
if (!this._rl) {
|
|
52
|
+
this._rl = createInterface({
|
|
53
|
+
input: process.stdin,
|
|
54
|
+
output: process.stdout
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
return new Promise((res) => this._rl?.question(text, res));
|
|
58
|
+
}
|
|
59
|
+
close() {
|
|
60
|
+
this._rl?.close();
|
|
61
|
+
return super.close();
|
|
62
|
+
}
|
|
63
|
+
start(params = {}) {
|
|
64
|
+
if (!params.botToken) {
|
|
65
|
+
if (!params.phone) params.phone = () => this.input("phone > ");
|
|
66
|
+
if (!params.code) params.code = () => this.input("code > ");
|
|
67
|
+
if (!params.password) {
|
|
68
|
+
params.password = () => this.input("2fa password > ");
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return super.start(params).then((user) => {
|
|
72
|
+
if (this._rl) {
|
|
73
|
+
this._rl.close();
|
|
74
|
+
delete this._rl;
|
|
75
|
+
}
|
|
76
|
+
return user;
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
run(params, then) {
|
|
80
|
+
if (typeof params === "function") {
|
|
81
|
+
then = params;
|
|
82
|
+
params = {};
|
|
83
|
+
}
|
|
84
|
+
this.start(params).then(then).catch((err) => this.onError.emit(unknownToError(err)));
|
|
85
|
+
}
|
|
86
|
+
downloadToFile(filename, location, params) {
|
|
87
|
+
return downloadToFile(this, filename, location, params);
|
|
88
|
+
}
|
|
89
|
+
downloadAsNodeStream(location, params) {
|
|
90
|
+
return downloadAsNodeStream(this, location, params);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
export {
|
|
94
|
+
BaseTelegramClient,
|
|
95
|
+
TelegramClient
|
|
96
|
+
};
|
package/index.cjs
CHANGED
|
@@ -1,434 +1,40 @@
|
|
|
1
1
|
if (typeof globalThis !== "undefined" && !globalThis._MTCUTE_CJS_DEPRECATION_WARNED) {
|
|
2
2
|
globalThis._MTCUTE_CJS_DEPRECATION_WARNED = true;
|
|
3
|
-
console.warn("[
|
|
4
|
-
console.warn("[
|
|
3
|
+
console.warn("[mtcute-workspace] CommonJS support is deprecated and will be removed in 0.20.0. Please consider switching to ESM, it's " + (/* @__PURE__ */ new Date()).getFullYear() + " already.");
|
|
4
|
+
console.warn("[mtcute-workspace] Learn more about switching to ESM: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c");
|
|
5
5
|
}
|
|
6
6
|
"use strict";
|
|
7
7
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
8
|
-
const
|
|
9
|
-
const
|
|
10
|
-
const
|
|
11
|
-
const
|
|
12
|
-
const
|
|
13
|
-
const
|
|
14
|
-
const node_path = require("node:path");
|
|
15
|
-
const node_stream = require("node:stream");
|
|
16
|
-
const streamUtils = require("./chunks/cjs/HP2yqAk_.js");
|
|
17
|
-
const downloadNodeStream = require("./chunks/cjs/BpvQ752Q.js");
|
|
8
|
+
const client = require("./client.cjs");
|
|
9
|
+
const index = require("./sqlite/index.cjs");
|
|
10
|
+
const platform = require("./utils/platform.cjs");
|
|
11
|
+
const proxies = require("./utils/proxies.cjs");
|
|
12
|
+
const tcp = require("./utils/tcp.cjs");
|
|
13
|
+
const worker = require("./worker.cjs");
|
|
18
14
|
const core = require("@mtcute/core");
|
|
19
|
-
const sqlite3 = require("better-sqlite3");
|
|
20
|
-
const crypto = require("./chunks/cjs/D7i-4e0W.js");
|
|
21
|
-
const EventEmitter = require("node:events");
|
|
22
|
-
const node_net = require("node:net");
|
|
23
|
-
const node_worker_threads = require("node:worker_threads");
|
|
24
|
-
const worker_js = require("@mtcute/core/worker.js");
|
|
25
15
|
const htmlParser = require("@mtcute/html-parser");
|
|
26
16
|
const markdownParser = require("@mtcute/markdown-parser");
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
if (file instanceof node_fs.ReadStream) {
|
|
49
|
-
const fileName = node_path.basename(file.path.toString());
|
|
50
|
-
const fileSize = await promises.stat(file.path.toString()).then((stat2) => stat2.size);
|
|
51
|
-
return {
|
|
52
|
-
file: streamUtils.nodeStreamToWeb(file),
|
|
53
|
-
fileName,
|
|
54
|
-
fileSize
|
|
55
|
-
};
|
|
56
|
-
}
|
|
57
|
-
if (file instanceof node_stream.Readable) {
|
|
58
|
-
return {
|
|
59
|
-
file: streamUtils.nodeStreamToWeb(file)
|
|
60
|
-
};
|
|
61
|
-
}
|
|
62
|
-
return null;
|
|
63
|
-
}
|
|
64
|
-
let installed = false;
|
|
65
|
-
let handled = false;
|
|
66
|
-
const callbacks = /* @__PURE__ */ new Set();
|
|
67
|
-
const myHandlers = /* @__PURE__ */ new Map();
|
|
68
|
-
function register(shouldManuallyExit, signal, event) {
|
|
69
|
-
function eventHandler() {
|
|
70
|
-
if (handled) {
|
|
71
|
-
return;
|
|
72
|
-
}
|
|
73
|
-
handled = true;
|
|
74
|
-
for (const callback of callbacks) {
|
|
75
|
-
callback();
|
|
76
|
-
}
|
|
77
|
-
for (const [event2, handler] of myHandlers) {
|
|
78
|
-
process.off(event2, handler);
|
|
79
|
-
}
|
|
80
|
-
if (shouldManuallyExit) {
|
|
81
|
-
process.kill(process.pid, signal);
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
process.on(event, eventHandler);
|
|
85
|
-
myHandlers.set(event, eventHandler);
|
|
86
|
-
}
|
|
87
|
-
function beforeExit(fn) {
|
|
88
|
-
if (typeof process === "undefined") return () => {
|
|
89
|
-
};
|
|
90
|
-
if (!installed) {
|
|
91
|
-
installed = true;
|
|
92
|
-
register(true, 0, "beforeExit");
|
|
93
|
-
register(true, 2, "SIGINT");
|
|
94
|
-
register(true, 15, "SIGTERM");
|
|
95
|
-
register(false, 15, "exit");
|
|
96
|
-
}
|
|
97
|
-
callbacks.add(fn);
|
|
98
|
-
return () => {
|
|
99
|
-
callbacks.delete(fn);
|
|
100
|
-
};
|
|
101
|
-
}
|
|
102
|
-
const isTty = typeof process === "object" && Boolean(process.stdout?.isTTY);
|
|
103
|
-
const BASE_FORMAT = isTty ? "%s [%s] [%s%s\x1B[0m] " : "%s [%s] [%s] ";
|
|
104
|
-
const LEVEL_NAMES = isTty ? [
|
|
105
|
-
"",
|
|
106
|
-
// OFF
|
|
107
|
-
"\x1B[31mERR\x1B[0m",
|
|
108
|
-
"\x1B[33mWRN\x1B[0m",
|
|
109
|
-
"\x1B[34mINF\x1B[0m",
|
|
110
|
-
"\x1B[36mDBG\x1B[0m",
|
|
111
|
-
"\x1B[35mVRB\x1B[0m"
|
|
112
|
-
] : [
|
|
113
|
-
"",
|
|
114
|
-
// OFF
|
|
115
|
-
"ERR",
|
|
116
|
-
"WRN",
|
|
117
|
-
"INF",
|
|
118
|
-
"DBG",
|
|
119
|
-
"VRB"
|
|
120
|
-
];
|
|
121
|
-
const TAG_COLORS = [6, 2, 3, 4, 5, 1].map((i) => `\x1B[3${i};1m`);
|
|
122
|
-
const defaultLoggingHandler = isTty ? (color, level, tag, fmt, args) => {
|
|
123
|
-
console.log(BASE_FORMAT + fmt, (/* @__PURE__ */ new Date()).toISOString(), LEVEL_NAMES[level], TAG_COLORS[color], tag, ...args);
|
|
124
|
-
} : (color, level, tag, fmt, args) => {
|
|
125
|
-
console.log(BASE_FORMAT + fmt, (/* @__PURE__ */ new Date()).toISOString(), LEVEL_NAMES[level], tag, ...args);
|
|
126
|
-
};
|
|
127
|
-
const BUFFER_BASE64_URL_AVAILABLE = typeof Buffer.isEncoding === "function" && Buffer.isEncoding("base64url");
|
|
128
|
-
const toBuffer = (buf) => Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength);
|
|
129
|
-
class NodePlatform {
|
|
130
|
-
getDeviceModel() {
|
|
131
|
-
return `Node.js/${process.version} (${os__namespace.type()} ${os__namespace.arch()})`;
|
|
132
|
-
}
|
|
133
|
-
getDefaultLogLevel() {
|
|
134
|
-
const envLogLevel = Number.parseInt(process.env.MTCUTE_LOG_LEVEL ?? "");
|
|
135
|
-
if (!Number.isNaN(envLogLevel)) {
|
|
136
|
-
return envLogLevel;
|
|
137
|
-
}
|
|
138
|
-
return null;
|
|
139
|
-
}
|
|
140
|
-
// ITlPlatform
|
|
141
|
-
utf8ByteLength(str) {
|
|
142
|
-
return Buffer.byteLength(str, "utf8");
|
|
143
|
-
}
|
|
144
|
-
utf8Encode(str) {
|
|
145
|
-
return Buffer.from(str, "utf8");
|
|
146
|
-
}
|
|
147
|
-
utf8Decode(buf) {
|
|
148
|
-
return toBuffer(buf).toString("utf8");
|
|
149
|
-
}
|
|
150
|
-
hexEncode(buf) {
|
|
151
|
-
return toBuffer(buf).toString("hex");
|
|
152
|
-
}
|
|
153
|
-
hexDecode(str) {
|
|
154
|
-
return Buffer.from(str, "hex");
|
|
155
|
-
}
|
|
156
|
-
base64Encode(buf, url = false) {
|
|
157
|
-
const nodeBuffer = toBuffer(buf);
|
|
158
|
-
if (url && BUFFER_BASE64_URL_AVAILABLE) return nodeBuffer.toString("base64url");
|
|
159
|
-
const str = nodeBuffer.toString("base64");
|
|
160
|
-
if (url) return str.replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");
|
|
161
|
-
return str;
|
|
162
|
-
}
|
|
163
|
-
base64Decode(string, url = false) {
|
|
164
|
-
if (url && BUFFER_BASE64_URL_AVAILABLE) {
|
|
165
|
-
return Buffer.from(string, "base64url");
|
|
166
|
-
}
|
|
167
|
-
if (url) {
|
|
168
|
-
string = string.replace(/-/g, "+").replace(/_/g, "/");
|
|
169
|
-
while (string.length % 4) string += "=";
|
|
170
|
-
}
|
|
171
|
-
return Buffer.from(string, "base64");
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
NodePlatform.prototype.log = defaultLoggingHandler;
|
|
175
|
-
NodePlatform.prototype.beforeExit = beforeExit;
|
|
176
|
-
NodePlatform.prototype.normalizeFile = normalizeFile;
|
|
177
|
-
class SqliteStorageDriver extends core.BaseSqliteStorageDriver {
|
|
178
|
-
constructor(filename = ":memory:", params) {
|
|
179
|
-
super();
|
|
180
|
-
this.filename = filename;
|
|
181
|
-
this.params = params;
|
|
182
|
-
}
|
|
183
|
-
_createDatabase() {
|
|
184
|
-
const db = sqlite3(this.filename, {
|
|
185
|
-
...this.params?.options,
|
|
186
|
-
verbose: this._log.mgr.level >= 5 ? this._log.verbose : void 0
|
|
187
|
-
});
|
|
188
|
-
if (!this.params?.disableWal) {
|
|
189
|
-
db.pragma("journal_mode = WAL");
|
|
190
|
-
}
|
|
191
|
-
return db;
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
class SqliteStorage extends core.BaseSqliteStorage {
|
|
195
|
-
constructor(filename = ":memory:", params) {
|
|
196
|
-
super(new SqliteStorageDriver(filename, params));
|
|
197
|
-
this.filename = filename;
|
|
198
|
-
this.params = params;
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
class BaseTcpTransport extends EventEmitter {
|
|
202
|
-
_currentDc = null;
|
|
203
|
-
_state = core.TransportState.Idle;
|
|
204
|
-
_socket = null;
|
|
205
|
-
_crypto;
|
|
206
|
-
log;
|
|
207
|
-
packetCodecInitialized = false;
|
|
208
|
-
_updateLogPrefix() {
|
|
209
|
-
if (this._currentDc) {
|
|
210
|
-
this.log.prefix = `[TCP:${this._currentDc.ipAddress}:${this._currentDc.port}] `;
|
|
211
|
-
} else {
|
|
212
|
-
this.log.prefix = "[TCP:disconnected] ";
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
setup(crypto2, log) {
|
|
216
|
-
this._crypto = crypto2;
|
|
217
|
-
this.log = log.create("tcp");
|
|
218
|
-
this._updateLogPrefix();
|
|
219
|
-
}
|
|
220
|
-
state() {
|
|
221
|
-
return this._state;
|
|
222
|
-
}
|
|
223
|
-
currentDc() {
|
|
224
|
-
return this._currentDc;
|
|
225
|
-
}
|
|
226
|
-
// eslint-disable-next-line unused-imports/no-unused-vars
|
|
227
|
-
connect(dc, testMode) {
|
|
228
|
-
if (this._state !== core.TransportState.Idle) {
|
|
229
|
-
throw new core.MtcuteError("Transport is not IDLE");
|
|
230
|
-
}
|
|
231
|
-
if (!this.packetCodecInitialized) {
|
|
232
|
-
this._packetCodec.setup?.(this._crypto, this.log);
|
|
233
|
-
this._packetCodec.on("error", (err) => this.emit("error", err));
|
|
234
|
-
this._packetCodec.on("packet", (buf) => this.emit("message", buf));
|
|
235
|
-
this.packetCodecInitialized = true;
|
|
236
|
-
}
|
|
237
|
-
this._state = core.TransportState.Connecting;
|
|
238
|
-
this._currentDc = dc;
|
|
239
|
-
this._updateLogPrefix();
|
|
240
|
-
this.log.debug("connecting to %j", dc);
|
|
241
|
-
this._socket = node_net.connect(dc.port, dc.ipAddress, this.handleConnect.bind(this));
|
|
242
|
-
this._socket.on("data", (data) => {
|
|
243
|
-
this._packetCodec.feed(data);
|
|
244
|
-
});
|
|
245
|
-
this._socket.on("error", this.handleError.bind(this));
|
|
246
|
-
this._socket.on("close", this.close.bind(this));
|
|
247
|
-
}
|
|
248
|
-
close() {
|
|
249
|
-
if (this._state === core.TransportState.Idle) return;
|
|
250
|
-
this.log.info("connection closed");
|
|
251
|
-
this._state = core.TransportState.Idle;
|
|
252
|
-
this._socket.removeAllListeners();
|
|
253
|
-
this._socket.destroy();
|
|
254
|
-
this._socket = null;
|
|
255
|
-
this._currentDc = null;
|
|
256
|
-
this._packetCodec.reset();
|
|
257
|
-
this.emit("close");
|
|
258
|
-
}
|
|
259
|
-
handleError(error) {
|
|
260
|
-
this.log.error("error: %s", error.stack);
|
|
261
|
-
if (this.listenerCount("error") > 0) {
|
|
262
|
-
this.emit("error", error);
|
|
263
|
-
}
|
|
264
|
-
}
|
|
265
|
-
handleConnect() {
|
|
266
|
-
this.log.info("connected");
|
|
267
|
-
Promise.resolve(this._packetCodec.tag()).then((initialMessage) => {
|
|
268
|
-
if (initialMessage.length) {
|
|
269
|
-
this._socket.write(initialMessage, (err) => {
|
|
270
|
-
if (err) {
|
|
271
|
-
this.log.error("failed to write initial message: %s", err.stack);
|
|
272
|
-
this.emit("error");
|
|
273
|
-
this.close();
|
|
274
|
-
} else {
|
|
275
|
-
this._state = core.TransportState.Ready;
|
|
276
|
-
this.emit("ready");
|
|
277
|
-
}
|
|
278
|
-
});
|
|
279
|
-
} else {
|
|
280
|
-
this._state = core.TransportState.Ready;
|
|
281
|
-
this.emit("ready");
|
|
282
|
-
}
|
|
283
|
-
}).catch((err) => this.emit("error", err));
|
|
284
|
-
}
|
|
285
|
-
async send(bytes) {
|
|
286
|
-
const framed = await this._packetCodec.encode(bytes);
|
|
287
|
-
if (this._state !== core.TransportState.Ready) {
|
|
288
|
-
throw new core.MtcuteError("Transport is not READY");
|
|
289
|
-
}
|
|
290
|
-
return new Promise((resolve, reject) => {
|
|
291
|
-
this._socket.write(framed, (error) => {
|
|
292
|
-
if (error) {
|
|
293
|
-
reject(error);
|
|
294
|
-
} else {
|
|
295
|
-
resolve();
|
|
296
|
-
}
|
|
297
|
-
});
|
|
298
|
-
});
|
|
299
|
-
}
|
|
300
|
-
}
|
|
301
|
-
class TcpTransport extends BaseTcpTransport {
|
|
302
|
-
_packetCodec = new core.IntermediatePacketCodec();
|
|
303
|
-
}
|
|
304
|
-
let nativeCrypto;
|
|
305
|
-
try {
|
|
306
|
-
nativeCrypto = require("@mtcute/crypto-node").NodeNativeCryptoProvider;
|
|
307
|
-
} catch {
|
|
308
|
-
}
|
|
309
|
-
class BaseTelegramClient extends client_js.BaseTelegramClient {
|
|
310
|
-
constructor(opts) {
|
|
311
|
-
if (!opts.platformless) platform_js.setPlatform(new NodePlatform());
|
|
312
|
-
super({
|
|
313
|
-
// eslint-disable-next-line
|
|
314
|
-
crypto: nativeCrypto ? new nativeCrypto() : new crypto.NodeCryptoProvider(),
|
|
315
|
-
transport: () => new TcpTransport(),
|
|
316
|
-
...opts,
|
|
317
|
-
storage: typeof opts.storage === "string" ? new SqliteStorage(opts.storage) : opts.storage ?? new SqliteStorage("client.session")
|
|
318
|
-
});
|
|
319
|
-
}
|
|
320
|
-
}
|
|
321
|
-
class TelegramClient extends client_js.TelegramClient {
|
|
322
|
-
constructor(opts) {
|
|
323
|
-
if ("client" in opts) {
|
|
324
|
-
super(opts);
|
|
325
|
-
return;
|
|
326
|
-
}
|
|
327
|
-
super({
|
|
328
|
-
client: new BaseTelegramClient(opts),
|
|
329
|
-
disableUpdates: opts.disableUpdates,
|
|
330
|
-
skipConversationUpdates: opts.skipConversationUpdates,
|
|
331
|
-
updates: opts.updates
|
|
332
|
-
});
|
|
333
|
-
}
|
|
334
|
-
_rl;
|
|
335
|
-
/**
|
|
336
|
-
* Tiny wrapper over Node `readline` package
|
|
337
|
-
* for simpler user input for `.start()` method.
|
|
338
|
-
*
|
|
339
|
-
* Associated `readline` interface is closed
|
|
340
|
-
* after `start()` returns, or with the client.
|
|
341
|
-
*
|
|
342
|
-
* @param text Text of the question
|
|
343
|
-
*/
|
|
344
|
-
input(text) {
|
|
345
|
-
if (!this._rl) {
|
|
346
|
-
this._rl = node_readline.createInterface({
|
|
347
|
-
input: process.stdin,
|
|
348
|
-
output: process.stdout
|
|
349
|
-
});
|
|
350
|
-
}
|
|
351
|
-
return new Promise((res) => this._rl?.question(text, res));
|
|
352
|
-
}
|
|
353
|
-
close() {
|
|
354
|
-
this._rl?.close();
|
|
355
|
-
return super.close();
|
|
356
|
-
}
|
|
357
|
-
start(params = {}) {
|
|
358
|
-
if (!params.botToken) {
|
|
359
|
-
if (!params.phone) params.phone = () => this.input("phone > ");
|
|
360
|
-
if (!params.code) params.code = () => this.input("code > ");
|
|
361
|
-
if (!params.password) {
|
|
362
|
-
params.password = () => this.input("2fa password > ");
|
|
363
|
-
}
|
|
364
|
-
}
|
|
365
|
-
return super.start(params).then((user) => {
|
|
366
|
-
if (this._rl) {
|
|
367
|
-
this._rl.close();
|
|
368
|
-
delete this._rl;
|
|
369
|
-
}
|
|
370
|
-
return user;
|
|
371
|
-
});
|
|
372
|
-
}
|
|
373
|
-
run(params, then) {
|
|
374
|
-
if (typeof params === "function") {
|
|
375
|
-
then = params;
|
|
376
|
-
params = {};
|
|
377
|
-
}
|
|
378
|
-
this.start(params).then(then).catch((err) => this.emitError(err));
|
|
379
|
-
}
|
|
380
|
-
downloadToFile(filename, location, params) {
|
|
381
|
-
return downloadNodeStream.downloadToFile(this, filename, location, params);
|
|
382
|
-
}
|
|
383
|
-
downloadAsNodeStream(location, params) {
|
|
384
|
-
return downloadNodeStream.downloadAsNodeStream(this, location, params);
|
|
385
|
-
}
|
|
386
|
-
}
|
|
387
|
-
let _registered = false;
|
|
388
|
-
class TelegramWorker extends worker_js.TelegramWorker {
|
|
389
|
-
registerWorker(handler) {
|
|
390
|
-
if (!node_worker_threads.parentPort) {
|
|
391
|
-
throw new Error("TelegramWorker must be created from a worker thread");
|
|
392
|
-
}
|
|
393
|
-
if (_registered) {
|
|
394
|
-
throw new Error("TelegramWorker must be created only once");
|
|
395
|
-
}
|
|
396
|
-
_registered = true;
|
|
397
|
-
const port = node_worker_threads.parentPort;
|
|
398
|
-
const respond = port.postMessage.bind(port);
|
|
399
|
-
node_worker_threads.parentPort.on("message", (message) => handler(message, respond));
|
|
400
|
-
return respond;
|
|
401
|
-
}
|
|
402
|
-
}
|
|
403
|
-
class TelegramWorkerPort extends worker_js.TelegramWorkerPort {
|
|
404
|
-
constructor(options) {
|
|
405
|
-
platform_js.setPlatform(new NodePlatform());
|
|
406
|
-
super(options);
|
|
407
|
-
this.options = options;
|
|
408
|
-
}
|
|
409
|
-
connectToWorker(worker, handler) {
|
|
410
|
-
if (!(worker instanceof node_worker_threads.Worker)) {
|
|
411
|
-
throw new TypeError("Only worker_threads are supported");
|
|
412
|
-
}
|
|
413
|
-
const send = worker.postMessage.bind(worker);
|
|
414
|
-
worker.on("message", handler);
|
|
415
|
-
return [
|
|
416
|
-
send,
|
|
417
|
-
() => {
|
|
418
|
-
worker.off("message", handler);
|
|
419
|
-
}
|
|
420
|
-
];
|
|
421
|
-
}
|
|
422
|
-
}
|
|
423
|
-
exports.BaseTcpTransport = BaseTcpTransport;
|
|
424
|
-
exports.BaseTelegramClient = BaseTelegramClient;
|
|
425
|
-
exports.NodePlatform = NodePlatform;
|
|
426
|
-
exports.SqliteStorage = SqliteStorage;
|
|
427
|
-
exports.SqliteStorageDriver = SqliteStorageDriver;
|
|
428
|
-
exports.TcpTransport = TcpTransport;
|
|
429
|
-
exports.TelegramClient = TelegramClient;
|
|
430
|
-
exports.TelegramWorker = TelegramWorker;
|
|
431
|
-
exports.TelegramWorkerPort = TelegramWorkerPort;
|
|
17
|
+
const driver = require("./sqlite/driver.cjs");
|
|
18
|
+
const net = require("@fuman/net");
|
|
19
|
+
exports.BaseTelegramClient = client.BaseTelegramClient;
|
|
20
|
+
exports.TelegramClient = client.TelegramClient;
|
|
21
|
+
exports.SqliteStorage = index.SqliteStorage;
|
|
22
|
+
exports.NodePlatform = platform.NodePlatform;
|
|
23
|
+
exports.HttpProxyTcpTransport = proxies.HttpProxyTcpTransport;
|
|
24
|
+
exports.MtProxyTcpTransport = proxies.MtProxyTcpTransport;
|
|
25
|
+
exports.SocksProxyTcpTransport = proxies.SocksProxyTcpTransport;
|
|
26
|
+
exports.TcpTransport = tcp.TcpTransport;
|
|
27
|
+
exports.TelegramWorker = worker.TelegramWorker;
|
|
28
|
+
exports.TelegramWorkerPort = worker.TelegramWorkerPort;
|
|
29
|
+
exports.SqliteStorageDriver = driver.SqliteStorageDriver;
|
|
30
|
+
Object.defineProperty(exports, "HttpProxyConnectionError", {
|
|
31
|
+
enumerable: true,
|
|
32
|
+
get: () => net.HttpProxyConnectionError
|
|
33
|
+
});
|
|
34
|
+
Object.defineProperty(exports, "SocksProxyConnectionError", {
|
|
35
|
+
enumerable: true,
|
|
36
|
+
get: () => net.SocksProxyConnectionError
|
|
37
|
+
});
|
|
432
38
|
Object.keys(core).forEach((k) => {
|
|
433
39
|
if (k !== "default" && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
|
434
40
|
enumerable: true,
|
package/index.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export * from './client.js';
|
|
2
|
-
export * from './common-internals-node/platform.js';
|
|
3
2
|
export * from './sqlite/index.js';
|
|
3
|
+
export * from './utils/platform.js';
|
|
4
|
+
export * from './utils/proxies.js';
|
|
4
5
|
export * from './utils/tcp.js';
|
|
5
6
|
export * from './worker.js';
|
|
6
7
|
export * from '@mtcute/core';
|