@mtcute/node 0.6.0 → 0.8.0
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/README.md +9 -5
- package/cjs/client.d.ts +47 -0
- package/cjs/client.js +107 -0
- package/cjs/client.js.map +1 -0
- package/cjs/index.d.ts +7 -28
- package/cjs/index.js +7 -83
- package/cjs/index.js.map +1 -1
- package/cjs/methods/download-file.d.ts +9 -0
- package/cjs/methods/download-file.js +34 -0
- package/cjs/methods/download-file.js.map +1 -0
- package/cjs/methods/download-node-stream.d.ts +9 -0
- package/cjs/methods/download-node-stream.js +15 -0
- package/cjs/methods/download-node-stream.js.map +1 -0
- package/cjs/methods.d.ts +3 -0
- package/cjs/methods.js +24 -0
- package/cjs/methods.js.map +1 -0
- package/cjs/platform.d.ts +18 -0
- package/cjs/platform.js +85 -0
- package/cjs/platform.js.map +1 -0
- package/cjs/utils/crypto.d.ts +16 -0
- package/cjs/utils/crypto.js +73 -0
- package/cjs/utils/crypto.js.map +1 -0
- package/cjs/utils/exit-hook.d.ts +1 -0
- package/cjs/utils/exit-hook.js +45 -0
- package/cjs/utils/exit-hook.js.map +1 -0
- package/cjs/utils/logging.d.ts +1 -0
- package/cjs/utils/logging.js +34 -0
- package/cjs/utils/logging.js.map +1 -0
- package/cjs/utils/normalize-file.d.ts +10 -0
- package/cjs/utils/normalize-file.js +31 -0
- package/cjs/utils/normalize-file.js.map +1 -0
- package/cjs/utils/stream-utils.d.ts +4 -0
- package/cjs/utils/stream-utils.js +72 -0
- package/cjs/utils/stream-utils.js.map +1 -0
- package/cjs/utils/tcp.d.ts +31 -0
- package/cjs/utils/tcp.js +128 -0
- package/cjs/utils/tcp.js.map +1 -0
- package/cjs/utils/version.d.ts +3 -0
- package/cjs/utils/version.js +21 -0
- package/cjs/utils/version.js.map +1 -0
- package/cjs/utils.d.ts +2 -1
- package/cjs/utils.js +2 -1
- package/cjs/utils.js.map +1 -1
- package/cjs/worker.d.ts +10 -0
- package/cjs/worker.js +47 -0
- package/cjs/worker.js.map +1 -0
- package/esm/client.d.ts +47 -0
- package/esm/client.js +105 -0
- package/esm/client.js.map +1 -0
- package/esm/index.d.ts +7 -28
- package/esm/index.js +7 -84
- package/esm/index.js.map +1 -1
- package/esm/methods/download-file.d.ts +9 -0
- package/esm/methods/download-file.js +30 -0
- package/esm/methods/download-file.js.map +1 -0
- package/esm/methods/download-node-stream.d.ts +9 -0
- package/esm/methods/download-node-stream.js +11 -0
- package/esm/methods/download-node-stream.js.map +1 -0
- package/esm/methods.d.ts +3 -0
- package/esm/methods.js +5 -0
- package/esm/methods.js.map +1 -0
- package/esm/platform.d.ts +18 -0
- package/esm/platform.js +58 -0
- package/esm/platform.js.map +1 -0
- package/esm/utils/crypto.d.ts +16 -0
- package/esm/utils/crypto.js +71 -0
- package/esm/utils/crypto.js.map +1 -0
- package/esm/utils/exit-hook.d.ts +1 -0
- package/esm/utils/exit-hook.js +41 -0
- package/esm/utils/exit-hook.js.map +1 -0
- package/esm/utils/logging.d.ts +1 -0
- package/esm/utils/logging.js +31 -0
- package/esm/utils/logging.js.map +1 -0
- package/esm/utils/normalize-file.d.ts +10 -0
- package/esm/utils/normalize-file.js +27 -0
- package/esm/utils/normalize-file.js.map +1 -0
- package/esm/utils/stream-utils.d.ts +4 -0
- package/esm/utils/stream-utils.js +67 -0
- package/esm/utils/stream-utils.js.map +1 -0
- package/esm/utils/tcp.d.ts +31 -0
- package/esm/utils/tcp.js +120 -0
- package/esm/utils/tcp.js.map +1 -0
- package/esm/utils/version.d.ts +3 -0
- package/esm/utils/version.js +17 -0
- package/esm/utils/version.js.map +1 -0
- package/esm/utils.d.ts +2 -1
- package/esm/utils.js +2 -1
- package/esm/utils.js.map +1 -1
- package/esm/worker.d.ts +10 -0
- package/esm/worker.js +42 -0
- package/esm/worker.js.map +1 -0
- package/package.json +11 -9
package/esm/client.d.ts
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/// <reference types="node" resolution-mode="require"/>
|
|
2
|
+
import { FileDownloadLocation, FileDownloadParameters, ITelegramStorageProvider, PartialOnly, User } from '@mtcute/core';
|
|
3
|
+
import { BaseTelegramClient as BaseTelegramClientBase, BaseTelegramClientOptions as BaseTelegramClientOptionsBase, TelegramClient as TelegramClientBase, TelegramClientOptions } from '@mtcute/core/client.js';
|
|
4
|
+
export type { TelegramClientOptions };
|
|
5
|
+
export interface BaseTelegramClientOptions extends PartialOnly<Omit<BaseTelegramClientOptionsBase, 'storage'>, 'transport' | 'crypto'> {
|
|
6
|
+
/**
|
|
7
|
+
* Storage to use for this client.
|
|
8
|
+
*
|
|
9
|
+
* If a string is passed, it will be used as
|
|
10
|
+
* a name for an SQLite database file.
|
|
11
|
+
*
|
|
12
|
+
* @default `"client.session"`
|
|
13
|
+
*/
|
|
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
|
+
}
|
|
23
|
+
export declare class BaseTelegramClient extends BaseTelegramClientBase {
|
|
24
|
+
constructor(opts: BaseTelegramClientOptions);
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Telegram client for use in Node.js
|
|
28
|
+
*/
|
|
29
|
+
export declare class TelegramClient extends TelegramClientBase {
|
|
30
|
+
constructor(opts: TelegramClientOptions);
|
|
31
|
+
private _rl?;
|
|
32
|
+
/**
|
|
33
|
+
* Tiny wrapper over Node `readline` package
|
|
34
|
+
* for simpler user input for `.run()` method.
|
|
35
|
+
*
|
|
36
|
+
* Associated `readline` interface is closed
|
|
37
|
+
* after `run()` returns, or with the client.
|
|
38
|
+
*
|
|
39
|
+
* @param text Text of the question
|
|
40
|
+
*/
|
|
41
|
+
input(text: string): Promise<string>;
|
|
42
|
+
close(): Promise<void>;
|
|
43
|
+
start(params?: Parameters<TelegramClientBase['start']>[0]): Promise<User>;
|
|
44
|
+
run(params: Parameters<TelegramClient['start']>[0] | ((user: User) => void | Promise<void>), then?: (user: User) => void | Promise<void>): void;
|
|
45
|
+
downloadToFile(filename: string, location: FileDownloadLocation, params?: FileDownloadParameters | undefined): Promise<void>;
|
|
46
|
+
downloadAsNodeStream(location: FileDownloadLocation, params?: FileDownloadParameters | undefined): import("stream").Readable;
|
|
47
|
+
}
|
package/esm/client.js
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { createRequire } from 'module';
|
|
2
|
+
import { createInterface } from 'readline';
|
|
3
|
+
import { BaseTelegramClient as BaseTelegramClientBase, TelegramClient as TelegramClientBase, } from '@mtcute/core/client.js';
|
|
4
|
+
import { setPlatform } from '@mtcute/core/platform.js';
|
|
5
|
+
import { SqliteStorage } from '@mtcute/sqlite';
|
|
6
|
+
import { downloadToFile } from './methods/download-file.js';
|
|
7
|
+
import { downloadAsNodeStream } from './methods/download-node-stream.js';
|
|
8
|
+
import { NodePlatform } from './platform.js';
|
|
9
|
+
import { NodeCryptoProvider } from './utils/crypto.js';
|
|
10
|
+
import { TcpTransport } from './utils/tcp.js';
|
|
11
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
12
|
+
let nativeCrypto;
|
|
13
|
+
try {
|
|
14
|
+
// @only-if-esm
|
|
15
|
+
const require = createRequire(import.meta.url);
|
|
16
|
+
// @/only-if-esm
|
|
17
|
+
// eslint-disable-next-line
|
|
18
|
+
nativeCrypto = require('@mtcute/crypto-node').NodeNativeCryptoProvider;
|
|
19
|
+
}
|
|
20
|
+
catch (e) { }
|
|
21
|
+
export class BaseTelegramClient extends BaseTelegramClientBase {
|
|
22
|
+
constructor(opts) {
|
|
23
|
+
if (!opts.platformless)
|
|
24
|
+
setPlatform(new NodePlatform());
|
|
25
|
+
super({
|
|
26
|
+
// eslint-disable-next-line
|
|
27
|
+
crypto: nativeCrypto ? new nativeCrypto() : new NodeCryptoProvider(),
|
|
28
|
+
transport: () => new TcpTransport(),
|
|
29
|
+
...opts,
|
|
30
|
+
storage: typeof opts.storage === 'string' ?
|
|
31
|
+
new SqliteStorage(opts.storage) :
|
|
32
|
+
opts.storage ?? new SqliteStorage('client.session'),
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Telegram client for use in Node.js
|
|
38
|
+
*/
|
|
39
|
+
export class TelegramClient extends TelegramClientBase {
|
|
40
|
+
constructor(opts) {
|
|
41
|
+
if ('client' in opts) {
|
|
42
|
+
super(opts);
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
super({
|
|
46
|
+
client: new BaseTelegramClient(opts),
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Tiny wrapper over Node `readline` package
|
|
51
|
+
* for simpler user input for `.run()` method.
|
|
52
|
+
*
|
|
53
|
+
* Associated `readline` interface is closed
|
|
54
|
+
* after `run()` returns, or with the client.
|
|
55
|
+
*
|
|
56
|
+
* @param text Text of the question
|
|
57
|
+
*/
|
|
58
|
+
input(text) {
|
|
59
|
+
if (!this._rl) {
|
|
60
|
+
this._rl = createInterface({
|
|
61
|
+
input: process.stdin,
|
|
62
|
+
output: process.stdout,
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
return new Promise((res) => this._rl?.question(text, res));
|
|
66
|
+
}
|
|
67
|
+
close() {
|
|
68
|
+
this._rl?.close();
|
|
69
|
+
return super.close();
|
|
70
|
+
}
|
|
71
|
+
start(params = {}) {
|
|
72
|
+
if (!params.botToken) {
|
|
73
|
+
if (!params.phone)
|
|
74
|
+
params.phone = () => this.input('phone > ');
|
|
75
|
+
if (!params.code)
|
|
76
|
+
params.code = () => this.input('code > ');
|
|
77
|
+
if (!params.password) {
|
|
78
|
+
params.password = () => this.input('2fa password > ');
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return super.start(params).then((user) => {
|
|
82
|
+
if (this._rl) {
|
|
83
|
+
this._rl.close();
|
|
84
|
+
delete this._rl;
|
|
85
|
+
}
|
|
86
|
+
return user;
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
run(params, then) {
|
|
90
|
+
if (typeof params === 'function') {
|
|
91
|
+
then = params;
|
|
92
|
+
params = {};
|
|
93
|
+
}
|
|
94
|
+
this.start(params)
|
|
95
|
+
.then(then)
|
|
96
|
+
.catch((err) => this.emitError(err));
|
|
97
|
+
}
|
|
98
|
+
downloadToFile(filename, location, params) {
|
|
99
|
+
return downloadToFile(this, filename, location, params);
|
|
100
|
+
}
|
|
101
|
+
downloadAsNodeStream(location, params) {
|
|
102
|
+
return downloadAsNodeStream(this, location, params);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
//# sourceMappingURL=client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.js","sourceRoot":"","sources":["../../src/client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,QAAQ,CAAA;AACtC,OAAO,EAAE,eAAe,EAA4B,MAAM,UAAU,CAAA;AAGpE,OAAO,EACH,kBAAkB,IAAI,sBAAsB,EAE5C,cAAc,IAAI,kBAAkB,GAEvC,MAAM,wBAAwB,CAAA;AAC/B,OAAO,EAAE,WAAW,EAAE,MAAM,0BAA0B,CAAA;AACtD,OAAO,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAA;AAE9C,OAAO,EAAE,cAAc,EAAE,MAAM,4BAA4B,CAAA;AAC3D,OAAO,EAAE,oBAAoB,EAAE,MAAM,mCAAmC,CAAA;AACxE,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAA;AAC5C,OAAO,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAA;AACtD,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAA;AAI7C,8DAA8D;AAC9D,IAAI,YAAiB,CAAA;AAErB,IAAI;IACA,e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AAC;aACb,IAAI,CAAC,IAAI,CAAC;aACV,KAAK,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC,CAAA;IAC5C,CAAC;IAED,cAAc,CACV,QAAgB,EAChB,QAA8B,EAC9B,MAA2C;QAE3C,OAAO,cAAc,CAAC,IAAI,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,CAAC,CAAA;IAC3D,CAAC;IAED,oBAAoB,CAAC,QAA8B,EAAE,MAA2C;QAC5F,OAAO,oBAAoB,CAAC,IAAI,EAAE,QAAQ,EAAE,MAAM,CAAC,CAAA;IACvD,CAAC;CACJ","sourcesContent":["import { createRequire } from 'module'\nimport { createInterface, Interface as RlInterface } from 'readline'\n\nimport { FileDownloadLocation, FileDownloadParameters, ITelegramStorageProvider, PartialOnly, User } from '@mtcute/core'\nimport {\n BaseTelegramClient as BaseTelegramClientBase,\n BaseTelegramClientOptions as BaseTelegramClientOptionsBase,\n TelegramClient as TelegramClientBase,\n TelegramClientOptions,\n} from '@mtcute/core/client.js'\nimport { setPlatform } from '@mtcute/core/platform.js'\nimport { SqliteStorage } from '@mtcute/sqlite'\n\nimport { downloadToFile } from './methods/download-file.js'\nimport { downloadAsNodeStream } from './methods/download-node-stream.js'\nimport { NodePlatform } from './platform.js'\nimport { NodeCryptoProvider } from './utils/crypto.js'\nimport { TcpTransport } from './utils/tcp.js'\n\nexport type { TelegramClientOptions }\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nlet nativeCrypto: any\n\ntry {\n // @only-if-esm\n const require = createRequire(import.meta.url)\n // @/only-if-esm\n // eslint-disable-next-line\n nativeCrypto = require('@mtcute/crypto-node').NodeNativeCryptoProvider\n} catch (e) {}\n\nexport interface BaseTelegramClientOptions\n extends PartialOnly<Omit<BaseTelegramClientOptionsBase, 'storage'>, 'transport' | 'crypto'> {\n /**\n * Storage to use for this client.\n *\n * If a string is passed, it will be used as\n * a name for an SQLite database file.\n *\n * @default `\"client.session\"`\n */\n storage?: string | ITelegramStorageProvider\n\n /**\n * **ADVANCED USE ONLY**\n *\n * Whether to not set up the platform.\n * This is useful if you call `setPlatform` yourself.\n */\n platformless?: boolean\n}\n\nexport class BaseTelegramClient extends BaseTelegramClientBase {\n constructor(opts: BaseTelegramClientOptions) {\n if (!opts.platformless) setPlatform(new NodePlatform())\n\n super({\n // eslint-disable-next-line\n crypto: nativeCrypto ? new nativeCrypto() : new NodeCryptoProvider(),\n transport: () => new TcpTransport(),\n ...opts,\n storage:\n typeof opts.storage === 'string' ?\n new SqliteStorage(opts.storage) :\n opts.storage ?? new SqliteStorage('client.session'),\n })\n }\n}\n\n/**\n * Telegram client for use in Node.js\n */\nexport class TelegramClient extends TelegramClientBase {\n constructor(opts: TelegramClientOptions) {\n if ('client' in opts) {\n super(opts)\n\n return\n }\n\n super({\n client: new BaseTelegramClient(opts),\n })\n }\n\n private _rl?: RlInterface\n\n /**\n * Tiny wrapper over Node `readline` package\n * for simpler user input for `.run()` method.\n *\n * Associated `readline` interface is closed\n * after `run()` returns, or with the client.\n *\n * @param text Text of the question\n */\n input(text: string): Promise<string> {\n if (!this._rl) {\n this._rl = createInterface({\n input: process.stdin,\n output: process.stdout,\n })\n }\n\n return new Promise((res) => this._rl?.question(text, res))\n }\n\n close(): Promise<void> {\n this._rl?.close()\n\n return super.close()\n }\n\n start(params: Parameters<TelegramClientBase['start']>[0] = {}): Promise<User> {\n if (!params.botToken) {\n if (!params.phone) params.phone = () => this.input('phone > ')\n if (!params.code) params.code = () => this.input('code > ')\n\n if (!params.password) {\n params.password = () => this.input('2fa password > ')\n }\n }\n\n return super.start(params).then((user) => {\n if (this._rl) {\n this._rl.close()\n delete this._rl\n }\n\n return user\n })\n }\n\n run(\n params: Parameters<TelegramClient['start']>[0] | ((user: User) => void | Promise<void>),\n then?: (user: User) => void | Promise<void>,\n ): void {\n if (typeof params === 'function') {\n then = params\n params = {}\n }\n\n this.start(params)\n .then(then)\n .catch((err) => this.emitError(err))\n }\n\n downloadToFile(\n filename: string,\n location: FileDownloadLocation,\n params?: FileDownloadParameters | undefined,\n ): Promise<void> {\n return downloadToFile(this, filename, location, params)\n }\n\n downloadAsNodeStream(location: FileDownloadLocation, params?: FileDownloadParameters | undefined) {\n return downloadAsNodeStream(this, location, params)\n }\n}\n"]}
|
package/esm/index.d.ts
CHANGED
|
@@ -1,30 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
export * from '
|
|
1
|
+
export * from './client.js';
|
|
2
|
+
export * from './platform.js';
|
|
3
|
+
export * from './utils/crypto.js';
|
|
4
|
+
export * from './utils/tcp.js';
|
|
5
|
+
export * from './worker.js';
|
|
6
|
+
export * from '@mtcute/core';
|
|
4
7
|
export * from '@mtcute/html-parser';
|
|
5
8
|
export * from '@mtcute/markdown-parser';
|
|
6
|
-
export
|
|
7
|
-
/**
|
|
8
|
-
* Tiny wrapper over {@link TelegramClient} for usage inside Node JS.
|
|
9
|
-
*
|
|
10
|
-
* This class automatically manages native
|
|
11
|
-
* crypto addon and defaults to SQLite session (unlike `TelegarmClient`,
|
|
12
|
-
* which defaults to a JSON file on Node).
|
|
13
|
-
*/
|
|
14
|
-
export declare class NodeTelegramClient extends TelegramClient {
|
|
15
|
-
constructor(opts: TelegramClientOptions);
|
|
16
|
-
private _rl?;
|
|
17
|
-
/**
|
|
18
|
-
* Tiny wrapper over Node `readline` package
|
|
19
|
-
* for simpler user input for `.run()` method.
|
|
20
|
-
*
|
|
21
|
-
* Associated `readline` interface is closed
|
|
22
|
-
* after `run()` returns, or with the client.
|
|
23
|
-
*
|
|
24
|
-
* @param text Text of the question
|
|
25
|
-
*/
|
|
26
|
-
input(text: string): Promise<string>;
|
|
27
|
-
close(): Promise<void>;
|
|
28
|
-
start(params?: Parameters<TelegramClient['start']>[0]): Promise<User>;
|
|
29
|
-
run(params: Parameters<TelegramClient['start']>[0] | ((user: User) => void | Promise<void>), then?: (user: User) => void | Promise<void>): void;
|
|
30
|
-
}
|
|
9
|
+
export * from '@mtcute/sqlite';
|
package/esm/index.js
CHANGED
|
@@ -1,87 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
export * from '
|
|
1
|
+
export * from './client.js';
|
|
2
|
+
export * from './platform.js';
|
|
3
|
+
export * from './utils/crypto.js';
|
|
4
|
+
export * from './utils/tcp.js';
|
|
5
|
+
export * from './worker.js';
|
|
6
|
+
export * from '@mtcute/core';
|
|
6
7
|
export * from '@mtcute/html-parser';
|
|
7
8
|
export * from '@mtcute/markdown-parser';
|
|
8
|
-
export
|
|
9
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
10
|
-
let nativeCrypto;
|
|
11
|
-
try {
|
|
12
|
-
// @only-if-esm
|
|
13
|
-
const require = createRequire(import.meta.url);
|
|
14
|
-
// @/only-if-esm
|
|
15
|
-
// eslint-disable-next-line
|
|
16
|
-
nativeCrypto = require('@mtcute/crypto-node').NodeNativeCryptoProvider;
|
|
17
|
-
}
|
|
18
|
-
catch (e) { }
|
|
19
|
-
/**
|
|
20
|
-
* Tiny wrapper over {@link TelegramClient} for usage inside Node JS.
|
|
21
|
-
*
|
|
22
|
-
* This class automatically manages native
|
|
23
|
-
* crypto addon and defaults to SQLite session (unlike `TelegarmClient`,
|
|
24
|
-
* which defaults to a JSON file on Node).
|
|
25
|
-
*/
|
|
26
|
-
export class NodeTelegramClient extends TelegramClient {
|
|
27
|
-
constructor(opts) {
|
|
28
|
-
super({
|
|
29
|
-
// eslint-disable-next-line
|
|
30
|
-
crypto: nativeCrypto ? () => new nativeCrypto() : undefined,
|
|
31
|
-
...opts,
|
|
32
|
-
storage: typeof opts.storage === 'string' ?
|
|
33
|
-
new SqliteStorage(opts.storage) :
|
|
34
|
-
opts.storage ?? new SqliteStorage('client.session'),
|
|
35
|
-
});
|
|
36
|
-
}
|
|
37
|
-
/**
|
|
38
|
-
* Tiny wrapper over Node `readline` package
|
|
39
|
-
* for simpler user input for `.run()` method.
|
|
40
|
-
*
|
|
41
|
-
* Associated `readline` interface is closed
|
|
42
|
-
* after `run()` returns, or with the client.
|
|
43
|
-
*
|
|
44
|
-
* @param text Text of the question
|
|
45
|
-
*/
|
|
46
|
-
input(text) {
|
|
47
|
-
if (!this._rl) {
|
|
48
|
-
this._rl = createInterface({
|
|
49
|
-
input: process.stdin,
|
|
50
|
-
output: process.stdout,
|
|
51
|
-
});
|
|
52
|
-
}
|
|
53
|
-
return new Promise((res) => this._rl?.question(text, res));
|
|
54
|
-
}
|
|
55
|
-
close() {
|
|
56
|
-
this._rl?.close();
|
|
57
|
-
return super.close();
|
|
58
|
-
}
|
|
59
|
-
start(params = {}) {
|
|
60
|
-
if (!params.botToken) {
|
|
61
|
-
if (!params.phone)
|
|
62
|
-
params.phone = () => this.input('phone > ');
|
|
63
|
-
if (!params.code)
|
|
64
|
-
params.code = () => this.input('code > ');
|
|
65
|
-
if (!params.password) {
|
|
66
|
-
params.password = () => this.input('2fa password > ');
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
return super.start(params).then((user) => {
|
|
70
|
-
if (this._rl) {
|
|
71
|
-
this._rl.close();
|
|
72
|
-
delete this._rl;
|
|
73
|
-
}
|
|
74
|
-
return user;
|
|
75
|
-
});
|
|
76
|
-
}
|
|
77
|
-
run(params, then) {
|
|
78
|
-
if (typeof params === 'function') {
|
|
79
|
-
then = params;
|
|
80
|
-
params = {};
|
|
81
|
-
}
|
|
82
|
-
this.start(params)
|
|
83
|
-
.then(then)
|
|
84
|
-
.catch((err) => this._emitError(err));
|
|
85
|
-
}
|
|
86
|
-
}
|
|
9
|
+
export * from '@mtcute/sqlite';
|
|
87
10
|
//# sourceMappingURL=index.js.map
|
package/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../index.ts"],"names":[],"mappings":"AAAA,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAA;AAC3B,cAAc,eAAe,CAAA;AAC7B,cAAc,mBAAmB,CAAA;AACjC,cAAc,gBAAgB,CAAA;AAC9B,cAAc,aAAa,CAAA;AAC3B,cAAc,cAAc,CAAA;AAC5B,cAAc,qBAAqB,CAAA;AACnC,cAAc,yBAAyB,CAAA;AACvC,cAAc,gBAAgB,CAAA","sourcesContent":["export * from './client.js'\nexport * from './platform.js'\nexport * from './utils/crypto.js'\nexport * from './utils/tcp.js'\nexport * from './worker.js'\nexport * from '@mtcute/core'\nexport * from '@mtcute/html-parser'\nexport * from '@mtcute/markdown-parser'\nexport * from '@mtcute/sqlite'\n"]}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { FileDownloadLocation, FileDownloadParameters, ITelegramClient } from '@mtcute/core';
|
|
2
|
+
/**
|
|
3
|
+
* Download a remote file to a local file (only for NodeJS).
|
|
4
|
+
* Promise will resolve once the download is complete.
|
|
5
|
+
*
|
|
6
|
+
* @param filename Local file name to which the remote file will be downloaded
|
|
7
|
+
* @param params File download parameters
|
|
8
|
+
*/
|
|
9
|
+
export declare function downloadToFile(client: ITelegramClient, filename: string, location: FileDownloadLocation, params?: FileDownloadParameters): Promise<void>;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { createWriteStream, rmSync } from 'fs';
|
|
2
|
+
import { writeFile } from 'fs/promises';
|
|
3
|
+
import { FileLocation } from '@mtcute/core';
|
|
4
|
+
import { downloadAsIterable } from '@mtcute/core/methods.js';
|
|
5
|
+
/**
|
|
6
|
+
* Download a remote file to a local file (only for NodeJS).
|
|
7
|
+
* Promise will resolve once the download is complete.
|
|
8
|
+
*
|
|
9
|
+
* @param filename Local file name to which the remote file will be downloaded
|
|
10
|
+
* @param params File download parameters
|
|
11
|
+
*/
|
|
12
|
+
export async function downloadToFile(client, filename, location, params) {
|
|
13
|
+
if (location instanceof FileLocation && ArrayBuffer.isView(location.location)) {
|
|
14
|
+
// early return for inline files
|
|
15
|
+
await writeFile(filename, location.location);
|
|
16
|
+
}
|
|
17
|
+
const output = createWriteStream(filename);
|
|
18
|
+
if (params?.abortSignal) {
|
|
19
|
+
params.abortSignal.addEventListener('abort', () => {
|
|
20
|
+
client.log.debug('aborting file download %s - cleaning up', filename);
|
|
21
|
+
output.destroy();
|
|
22
|
+
rmSync(filename);
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
for await (const chunk of downloadAsIterable(client, location, params)) {
|
|
26
|
+
output.write(chunk);
|
|
27
|
+
}
|
|
28
|
+
output.end();
|
|
29
|
+
}
|
|
30
|
+
//# sourceMappingURL=download-file.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"download-file.js","sourceRoot":"","sources":["../../../src/methods/download-file.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,EAAE,MAAM,IAAI,CAAA;AAC9C,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;AAEvC,OAAO,EAAgD,YAAY,EAAmB,MAAM,cAAc,CAAA;AAC1G,OAAO,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAA;AAE5D;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,cAAc,CAChC,MAAuB,EACvB,QAAgB,EAChB,QAA8B,EAC9B,MAA+B;IAE/B,IAAI,QAAQ,YAAY,YAAY,IAAI,WAAW,CAAC,MAAM,CAAC,QAAQ,CAAC,QAAQ,CAAC,EAAE;QAC3E,gCAAgC;QAChC,MAAM,SAAS,CAAC,QAAQ,EAAE,QAAQ,CAAC,QAAQ,CAAC,CAAA;KAC/C;IAED,MAAM,MAAM,GAAG,iBAAiB,CAAC,QAAQ,CAAC,CAAA;IAE1C,IAAI,MAAM,EAAE,WAAW,EAAE;QACrB,MAAM,CAAC,WAAW,CAAC,gBAAgB,CAAC,OAAO,EAAE,GAAG,EAAE;YAC9C,MAAM,CAAC,GAAG,CAAC,KAAK,CAAC,yCAAyC,EAAE,QAAQ,CAAC,CAAA;YACrE,MAAM,CAAC,OAAO,EAAE,CAAA;YAChB,MAAM,CAAC,QAAQ,CAAC,CAAA;QACpB,CAAC,CAAC,CAAA;KACL;IAED,IAAI,KAAK,EAAE,MAAM,KAAK,IAAI,kBAAkB,CAAC,MAAM,EAAE,QAAQ,EAAE,MAAM,CAAC,EAAE;QACpE,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,CAAA;KACtB;IAED,MAAM,CAAC,GAAG,EAAE,CAAA;AAChB,CAAC","sourcesContent":["import { createWriteStream, rmSync } from 'fs'\nimport { writeFile } from 'fs/promises'\n\nimport { FileDownloadLocation, FileDownloadParameters, FileLocation, ITelegramClient } from '@mtcute/core'\nimport { downloadAsIterable } from '@mtcute/core/methods.js'\n\n/**\n * Download a remote file to a local file (only for NodeJS).\n * Promise will resolve once the download is complete.\n *\n * @param filename Local file name to which the remote file will be downloaded\n * @param params File download parameters\n */\nexport async function downloadToFile(\n client: ITelegramClient,\n filename: string,\n location: FileDownloadLocation,\n params?: FileDownloadParameters,\n): Promise<void> {\n if (location instanceof FileLocation && ArrayBuffer.isView(location.location)) {\n // early return for inline files\n await writeFile(filename, location.location)\n }\n\n const output = createWriteStream(filename)\n\n if (params?.abortSignal) {\n params.abortSignal.addEventListener('abort', () => {\n client.log.debug('aborting file download %s - cleaning up', filename)\n output.destroy()\n rmSync(filename)\n })\n }\n\n for await (const chunk of downloadAsIterable(client, location, params)) {\n output.write(chunk)\n }\n\n output.end()\n}\n"]}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/// <reference types="node" resolution-mode="require"/>
|
|
2
|
+
import { Readable } from 'stream';
|
|
3
|
+
import { FileDownloadLocation, FileDownloadParameters, ITelegramClient } from '@mtcute/core';
|
|
4
|
+
/**
|
|
5
|
+
* Download a remote file as a Node.js Readable stream.
|
|
6
|
+
*
|
|
7
|
+
* @param params File download parameters
|
|
8
|
+
*/
|
|
9
|
+
export declare function downloadAsNodeStream(client: ITelegramClient, location: FileDownloadLocation, params?: FileDownloadParameters): Readable;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { downloadAsStream } from '@mtcute/core/methods.js';
|
|
2
|
+
import { webStreamToNode } from '../utils/stream-utils.js';
|
|
3
|
+
/**
|
|
4
|
+
* Download a remote file as a Node.js Readable stream.
|
|
5
|
+
*
|
|
6
|
+
* @param params File download parameters
|
|
7
|
+
*/
|
|
8
|
+
export function downloadAsNodeStream(client, location, params) {
|
|
9
|
+
return webStreamToNode(downloadAsStream(client, location, params));
|
|
10
|
+
}
|
|
11
|
+
//# sourceMappingURL=download-node-stream.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"download-node-stream.js","sourceRoot":"","sources":["../../../src/methods/download-node-stream.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,gBAAgB,EAAE,MAAM,yBAAyB,CAAA;AAE1D,OAAO,EAAE,eAAe,EAAE,MAAM,0BAA0B,CAAA;AAE1D;;;;GAIG;AACH,MAAM,UAAU,oBAAoB,CAChC,MAAuB,EACvB,QAA8B,EAC9B,MAA+B;IAE/B,OAAO,eAAe,CAAC,gBAAgB,CAAC,MAAM,EAAE,QAAQ,EAAE,MAAM,CAAC,CAAC,CAAA;AACtE,CAAC","sourcesContent":["import { Readable } from 'stream'\n\nimport { FileDownloadLocation, FileDownloadParameters, ITelegramClient } from '@mtcute/core'\nimport { downloadAsStream } from '@mtcute/core/methods.js'\n\nimport { webStreamToNode } from '../utils/stream-utils.js'\n\n/**\n * Download a remote file as a Node.js Readable stream.\n *\n * @param params File download parameters\n */\nexport function downloadAsNodeStream(\n client: ITelegramClient,\n location: FileDownloadLocation,\n params?: FileDownloadParameters,\n): Readable {\n return webStreamToNode(downloadAsStream(client, location, params))\n}\n"]}
|
package/esm/methods.d.ts
ADDED
package/esm/methods.js
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
/* eslint-disable import/export, simple-import-sort/exports */
|
|
2
|
+
export * from '@mtcute/core/methods.js';
|
|
3
|
+
export { downloadToFile } from './methods/download-file.js';
|
|
4
|
+
export { downloadAsNodeStream } from './methods/download-node-stream.js';
|
|
5
|
+
//# sourceMappingURL=methods.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"methods.js","sourceRoot":"","sources":["../../src/methods.ts"],"names":[],"mappings":"AAAA,8DAA8D;AAC9D,cAAc,yBAAyB,CAAA;AAEvC,OAAO,EAAE,cAAc,EAAE,MAAM,4BAA4B,CAAA;AAC3D,OAAO,EAAE,oBAAoB,EAAE,MAAM,mCAAmC,CAAA","sourcesContent":["/* eslint-disable import/export, simple-import-sort/exports */\nexport * from '@mtcute/core/methods.js'\n\nexport { downloadToFile } from './methods/download-file.js'\nexport { downloadAsNodeStream } from './methods/download-node-stream.js'\n"]}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { ICorePlatform } from '@mtcute/core/platform.js';
|
|
2
|
+
import { beforeExit } from './utils/exit-hook.js';
|
|
3
|
+
import { defaultLoggingHandler } from './utils/logging.js';
|
|
4
|
+
import { normalizeFile } from './utils/normalize-file.js';
|
|
5
|
+
export declare class NodePlatform implements ICorePlatform {
|
|
6
|
+
log: typeof defaultLoggingHandler;
|
|
7
|
+
beforeExit: typeof beforeExit;
|
|
8
|
+
normalizeFile: typeof normalizeFile;
|
|
9
|
+
getDeviceModel(): string;
|
|
10
|
+
getDefaultLogLevel(): number | null;
|
|
11
|
+
utf8ByteLength(str: string): number;
|
|
12
|
+
utf8Encode(str: string): Uint8Array;
|
|
13
|
+
utf8Decode(buf: Uint8Array): string;
|
|
14
|
+
hexEncode(buf: Uint8Array): string;
|
|
15
|
+
hexDecode(str: string): Uint8Array;
|
|
16
|
+
base64Encode(buf: Uint8Array, url?: boolean): string;
|
|
17
|
+
base64Decode(string: string, url?: boolean): Uint8Array;
|
|
18
|
+
}
|
package/esm/platform.js
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import * as os from 'os';
|
|
2
|
+
import { beforeExit } from './utils/exit-hook.js';
|
|
3
|
+
import { defaultLoggingHandler } from './utils/logging.js';
|
|
4
|
+
import { normalizeFile } from './utils/normalize-file.js';
|
|
5
|
+
const BUFFER_BASE64_URL_AVAILABLE = typeof Buffer.isEncoding === 'function' && Buffer.isEncoding('base64url');
|
|
6
|
+
const toBuffer = (buf) => Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength);
|
|
7
|
+
export class NodePlatform {
|
|
8
|
+
getDeviceModel() {
|
|
9
|
+
return `${os.type()} ${os.arch()} ${os.release()}`;
|
|
10
|
+
}
|
|
11
|
+
getDefaultLogLevel() {
|
|
12
|
+
const envLogLevel = parseInt(process.env.MTCUTE_LOG_LEVEL ?? '');
|
|
13
|
+
if (!isNaN(envLogLevel)) {
|
|
14
|
+
return envLogLevel;
|
|
15
|
+
}
|
|
16
|
+
return null;
|
|
17
|
+
}
|
|
18
|
+
// ITlPlatform
|
|
19
|
+
utf8ByteLength(str) {
|
|
20
|
+
return Buffer.byteLength(str, 'utf8');
|
|
21
|
+
}
|
|
22
|
+
utf8Encode(str) {
|
|
23
|
+
return Buffer.from(str, 'utf8');
|
|
24
|
+
}
|
|
25
|
+
utf8Decode(buf) {
|
|
26
|
+
return toBuffer(buf).toString('utf8');
|
|
27
|
+
}
|
|
28
|
+
hexEncode(buf) {
|
|
29
|
+
return toBuffer(buf).toString('hex');
|
|
30
|
+
}
|
|
31
|
+
hexDecode(str) {
|
|
32
|
+
return Buffer.from(str, 'hex');
|
|
33
|
+
}
|
|
34
|
+
base64Encode(buf, url = false) {
|
|
35
|
+
const nodeBuffer = toBuffer(buf);
|
|
36
|
+
if (url && BUFFER_BASE64_URL_AVAILABLE)
|
|
37
|
+
return nodeBuffer.toString('base64url');
|
|
38
|
+
const str = nodeBuffer.toString('base64');
|
|
39
|
+
if (url)
|
|
40
|
+
return str.replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, '');
|
|
41
|
+
return str;
|
|
42
|
+
}
|
|
43
|
+
base64Decode(string, url = false) {
|
|
44
|
+
if (url && BUFFER_BASE64_URL_AVAILABLE) {
|
|
45
|
+
return Buffer.from(string, 'base64url');
|
|
46
|
+
}
|
|
47
|
+
if (url) {
|
|
48
|
+
string = string.replace(/-/g, '+').replace(/_/g, '/');
|
|
49
|
+
while (string.length % 4)
|
|
50
|
+
string += '=';
|
|
51
|
+
}
|
|
52
|
+
return Buffer.from(string, 'base64');
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
NodePlatform.prototype.log = defaultLoggingHandler;
|
|
56
|
+
NodePlatform.prototype.beforeExit = beforeExit;
|
|
57
|
+
NodePlatform.prototype.normalizeFile = normalizeFile;
|
|
58
|
+
//# sourceMappingURL=platform.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"platform.js","sourceRoot":"","sources":["../../src/platform.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,IAAI,CAAA;AAIxB,OAAO,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAA;AACjD,OAAO,EAAE,qBAAqB,EAAE,MAAM,oBAAoB,CAAA;AAC1D,OAAO,EAAE,aAAa,EAAE,MAAM,2BAA2B,CAAA;AAEzD,MAAM,2BAA2B,GAAG,OAAO,MAAM,CAAC,UAAU,KAAK,UAAU,IAAI,MAAM,CAAC,UAAU,CAAC,WAAW,CAAC,CAAA;AAE7G,MAAM,QAAQ,GAAG,CAAC,GAAe,EAAU,EAAE,CAAC,MAAM,CAAC,IAAI,CACrD,GAAG,CAAC,MAAM,EACV,GAAG,CAAC,UAAU,EACd,GAAG,CAAC,UAAU,CACjB,CAAA;AAED,MAAM,OAAO,YAAY;IAMrB,cAAc;QACV,OAAO,GAAG,EAAE,CAAC,IAAI,EAAE,IAAI,EAAE,CAAC,IAAI,EAAE,IAAI,EAAE,CAAC,OAAO,EAAE,EAAE,CAAA;IACtD,CAAC;IAED,kBAAkB;QACd,MAA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AAG,KAAK;QACpC,IAAI,GAAG,IAAI,2BAA2B,EAAE;YACpC,OAAO,MAAM,CAAC,IAAI,CAAC,MAAM,EAAE,WAAW,CAAC,CAAA;SAC1C;QAED,IAAI,GAAG,EAAE;YACL,MAAM,GAAG,MAAM,CAAC,OAAO,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC,OAAO,CAAC,IAAI,EAAE,GAAG,CAAC,CAAA;YACrD,OAAO,MAAM,CAAC,MAAM,GAAG,CAAC;gBAAE,MAAM,IAAI,GAAG,CAAA;SAC1C;QAED,OAAO,MAAM,CAAC,IAAI,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAA;IACxC,CAAC;CACJ;AAED,YAAY,CAAC,SAAS,CAAC,GAAG,GAAG,qBAAqB,CAAA;AAClD,YAAY,CAAC,SAAS,CAAC,UAAU,GAAG,UAAU,CAAA;AAC9C,YAAY,CAAC,SAAS,CAAC,aAAa,GAAG,aAAa,CAAA","sourcesContent":["import * as os from 'os'\n\nimport { ICorePlatform } from '@mtcute/core/platform.js'\n\nimport { beforeExit } from './utils/exit-hook.js'\nimport { defaultLoggingHandler } from './utils/logging.js'\nimport { normalizeFile } from './utils/normalize-file.js'\n\nconst BUFFER_BASE64_URL_AVAILABLE = typeof Buffer.isEncoding === 'function' && Buffer.isEncoding('base64url')\n\nconst toBuffer = (buf: Uint8Array): Buffer => Buffer.from(\n buf.buffer,\n buf.byteOffset,\n buf.byteLength,\n)\n\nexport class NodePlatform implements ICorePlatform {\n // ICorePlatform\n log!: typeof defaultLoggingHandler\n beforeExit!: typeof beforeExit\n normalizeFile!: typeof normalizeFile\n\n getDeviceModel(): string {\n return `${os.type()} ${os.arch()} ${os.release()}`\n }\n\n getDefaultLogLevel(): number | null {\n const envLogLevel = parseInt(process.env.MTCUTE_LOG_LEVEL ?? '')\n\n if (!isNaN(envLogLevel)) {\n return envLogLevel\n }\n\n return null\n }\n\n // ITlPlatform\n utf8ByteLength(str: string): number {\n return Buffer.byteLength(str, 'utf8')\n }\n utf8Encode(str: string): Uint8Array {\n return Buffer.from(str, 'utf8')\n }\n utf8Decode(buf: Uint8Array): string {\n return toBuffer(buf).toString('utf8')\n }\n\n hexEncode(buf: Uint8Array): string {\n return toBuffer(buf).toString('hex')\n }\n hexDecode(str: string): Uint8Array {\n return Buffer.from(str, 'hex')\n }\n\n base64Encode(buf: Uint8Array, url = false): string {\n const nodeBuffer = toBuffer(buf)\n\n if (url && BUFFER_BASE64_URL_AVAILABLE) return nodeBuffer.toString('base64url')\n\n const str = nodeBuffer.toString('base64')\n if (url) return str.replace(/\\+/g, '-').replace(/\\//g, '_').replace(/=/g, '')\n\n return str\n }\n base64Decode(string: string, url = false): Uint8Array {\n if (url && BUFFER_BASE64_URL_AVAILABLE) {\n return Buffer.from(string, 'base64url')\n }\n\n if (url) {\n string = string.replace(/-/g, '+').replace(/_/g, '/')\n while (string.length % 4) string += '='\n }\n\n return Buffer.from(string, 'base64')\n }\n}\n\nNodePlatform.prototype.log = defaultLoggingHandler\nNodePlatform.prototype.beforeExit = beforeExit\nNodePlatform.prototype.normalizeFile = normalizeFile\n"]}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { MaybePromise } from '@mtcute/core';
|
|
2
|
+
import { BaseCryptoProvider, IAesCtr, ICryptoProvider, IEncryptionScheme } from '@mtcute/core/utils.js';
|
|
3
|
+
export declare abstract class BaseNodeCryptoProvider extends BaseCryptoProvider {
|
|
4
|
+
createAesCtr(key: Uint8Array, iv: Uint8Array): IAesCtr;
|
|
5
|
+
pbkdf2(password: Uint8Array, salt: Uint8Array, iterations: number, keylen?: number, algo?: string): MaybePromise<Uint8Array>;
|
|
6
|
+
sha1(data: Uint8Array): Uint8Array;
|
|
7
|
+
sha256(data: Uint8Array): Uint8Array;
|
|
8
|
+
hmacSha256(data: Uint8Array, key: Uint8Array): Uint8Array;
|
|
9
|
+
gzip(data: Uint8Array, maxSize: number): Uint8Array | null;
|
|
10
|
+
gunzip(data: Uint8Array): Uint8Array;
|
|
11
|
+
randomFill(buf: Uint8Array): void;
|
|
12
|
+
}
|
|
13
|
+
export declare class NodeCryptoProvider extends BaseNodeCryptoProvider implements ICryptoProvider {
|
|
14
|
+
initialize(): Promise<void>;
|
|
15
|
+
createAesIge(key: Uint8Array, iv: Uint8Array): IEncryptionScheme;
|
|
16
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
// eslint-disable-next-line no-restricted-imports
|
|
2
|
+
import { createCipheriv, createHash, createHmac, pbkdf2, randomFillSync } from 'crypto';
|
|
3
|
+
import { readFile } from 'fs/promises';
|
|
4
|
+
import { createRequire } from 'module';
|
|
5
|
+
import { deflateSync, gunzipSync } from 'zlib';
|
|
6
|
+
import { BaseCryptoProvider } from '@mtcute/core/utils.js';
|
|
7
|
+
import { ige256Decrypt, ige256Encrypt, initSync } from '@mtcute/wasm';
|
|
8
|
+
export class BaseNodeCryptoProvider extends BaseCryptoProvider {
|
|
9
|
+
createAesCtr(key, iv) {
|
|
10
|
+
const cipher = createCipheriv(`aes-${key.length * 8}-ctr`, key, iv);
|
|
11
|
+
const update = (data) => cipher.update(data);
|
|
12
|
+
return {
|
|
13
|
+
process: update,
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
pbkdf2(password, salt, iterations, keylen = 64, algo = 'sha512') {
|
|
17
|
+
return new Promise((resolve, reject) => pbkdf2(password, salt, iterations, keylen, algo, (err, buf) => err !== null ? reject(err) : resolve(buf)));
|
|
18
|
+
}
|
|
19
|
+
sha1(data) {
|
|
20
|
+
return createHash('sha1').update(data).digest();
|
|
21
|
+
}
|
|
22
|
+
sha256(data) {
|
|
23
|
+
return createHash('sha256').update(data).digest();
|
|
24
|
+
}
|
|
25
|
+
hmacSha256(data, key) {
|
|
26
|
+
return createHmac('sha256', key).update(data).digest();
|
|
27
|
+
}
|
|
28
|
+
gzip(data, maxSize) {
|
|
29
|
+
try {
|
|
30
|
+
// telegram accepts both zlib and gzip, but zlib is faster and has less overhead, so we use it here
|
|
31
|
+
return deflateSync(data, {
|
|
32
|
+
maxOutputLength: maxSize,
|
|
33
|
+
});
|
|
34
|
+
// hot path, avoid additional runtime checks
|
|
35
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
36
|
+
}
|
|
37
|
+
catch (e) {
|
|
38
|
+
if (e.code === 'ERR_BUFFER_TOO_LARGE') {
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
throw e;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
gunzip(data) {
|
|
45
|
+
return gunzipSync(data);
|
|
46
|
+
}
|
|
47
|
+
randomFill(buf) {
|
|
48
|
+
randomFillSync(buf);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
export class NodeCryptoProvider extends BaseNodeCryptoProvider {
|
|
52
|
+
async initialize() {
|
|
53
|
+
// @only-if-esm
|
|
54
|
+
const require = createRequire(import.meta.url);
|
|
55
|
+
// @/only-if-esm
|
|
56
|
+
const wasmFile = require.resolve('@mtcute/wasm/mtcute.wasm');
|
|
57
|
+
const wasm = await readFile(wasmFile);
|
|
58
|
+
initSync(wasm);
|
|
59
|
+
}
|
|
60
|
+
createAesIge(key, iv) {
|
|
61
|
+
return {
|
|
62
|
+
encrypt(data) {
|
|
63
|
+
return ige256Encrypt(data, key, iv);
|
|
64
|
+
},
|
|
65
|
+
decrypt(data) {
|
|
66
|
+
return ige256Decrypt(data, key, iv);
|
|
67
|
+
},
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
//# sourceMappingURL=crypto.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"crypto.js","sourceRoot":"","sources":["../../../src/utils/crypto.ts"],"names":[],"mappings":"AAAA,iDAAiD;AACjD,OAAO,EAAE,cAAc,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,EAAE,cAAc,EAAE,MAAM,QAAQ,CAAA;AACvF,OAAO,EAAE,QAAQ,EAAE,MAAM,aAA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AAG,EAAE,EAAE,CAAC,CAAA;YACvC,CAAC;SACJ,CAAA;IACL,CAAC;CACJ","sourcesContent":["// eslint-disable-next-line no-restricted-imports\nimport { createCipheriv, createHash, createHmac, pbkdf2, randomFillSync } from 'crypto'\nimport { readFile } from 'fs/promises'\nimport { createRequire } from 'module'\nimport { deflateSync, gunzipSync } from 'zlib'\n\nimport { MaybePromise } from '@mtcute/core'\nimport { BaseCryptoProvider, IAesCtr, ICryptoProvider, IEncryptionScheme } from '@mtcute/core/utils.js'\nimport { ige256Decrypt, ige256Encrypt, initSync } from '@mtcute/wasm'\n\nexport abstract class BaseNodeCryptoProvider extends BaseCryptoProvider {\n createAesCtr(key: Uint8Array, iv: Uint8Array): IAesCtr {\n const cipher = createCipheriv(`aes-${key.length * 8}-ctr`, key, iv)\n\n const update = (data: Uint8Array) => cipher.update(data)\n\n return {\n process: update,\n }\n }\n\n pbkdf2(\n password: Uint8Array,\n salt: Uint8Array,\n iterations: number,\n keylen = 64,\n algo = 'sha512',\n ): MaybePromise<Uint8Array> {\n return new Promise((resolve, reject) =>\n pbkdf2(password, salt, iterations, keylen, algo, (err: Error | null, buf: Uint8Array) =>\n err !== null ? reject(err) : resolve(buf),\n ),\n )\n }\n\n sha1(data: Uint8Array): Uint8Array {\n return createHash('sha1').update(data).digest()\n }\n\n sha256(data: Uint8Array): Uint8Array {\n return createHash('sha256').update(data).digest()\n }\n\n hmacSha256(data: Uint8Array, key: Uint8Array): Uint8Array {\n return createHmac('sha256', key).update(data).digest()\n }\n\n gzip(data: Uint8Array, maxSize: number): Uint8Array | null {\n try {\n // telegram accepts both zlib and gzip, but zlib is faster and has less overhead, so we use it here\n return deflateSync(data, {\n maxOutputLength: maxSize,\n })\n // hot path, avoid additional runtime checks\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n } catch (e: any) {\n if (e.code === 'ERR_BUFFER_TOO_LARGE') {\n return null\n }\n\n throw e\n }\n }\n\n gunzip(data: Uint8Array): Uint8Array {\n return gunzipSync(data)\n }\n\n randomFill(buf: Uint8Array) {\n randomFillSync(buf)\n }\n}\n\nexport class NodeCryptoProvider extends BaseNodeCryptoProvider implements ICryptoProvider {\n async initialize(): Promise<void> {\n // @only-if-esm\n const require = createRequire(import.meta.url)\n // @/only-if-esm\n const wasmFile = require.resolve('@mtcute/wasm/mtcute.wasm')\n const wasm = await readFile(wasmFile)\n initSync(wasm)\n }\n\n createAesIge(key: Uint8Array, iv: Uint8Array): IEncryptionScheme {\n return {\n encrypt(data: Uint8Array): Uint8Array {\n return ige256Encrypt(data, key, iv)\n },\n decrypt(data: Uint8Array): Uint8Array {\n return ige256Decrypt(data, key, iv)\n },\n }\n }\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function beforeExit(fn: () => void): () => void;
|