@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/README.md
CHANGED
|
@@ -2,16 +2,20 @@
|
|
|
2
2
|
|
|
3
3
|
📖 [API Reference](https://ref.mtcute.dev/modules/_mtcute_node.html)
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
(must be installed separately, `@mtcute/crypto-node`)
|
|
7
|
-
|
|
5
|
+
Node.js support package for mtcute. Includes:
|
|
6
|
+
- Support for native crypto addon (must be installed separately, `@mtcute/crypto-node`)
|
|
7
|
+
- Terminal I/O via `readline`
|
|
8
|
+
- SQLite storage (`@mtcute/sqlite`)
|
|
9
|
+
- TCP transport
|
|
10
|
+
- `TelegramClient` implementation using the above
|
|
11
|
+
- HTML and Markdown parsers
|
|
8
12
|
|
|
9
13
|
## Usage
|
|
10
14
|
|
|
11
15
|
```typescript
|
|
12
|
-
import {
|
|
16
|
+
import { TelegramClient } from '@mtcute/node'
|
|
13
17
|
|
|
14
|
-
const tg = new
|
|
18
|
+
const tg = new TelegramClient({
|
|
15
19
|
apiId: 12345,
|
|
16
20
|
apiHash: 'abcdef',
|
|
17
21
|
storage: 'my-account'
|
package/cjs/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/cjs/client.js
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TelegramClient = exports.BaseTelegramClient = void 0;
|
|
4
|
+
const readline_1 = require("readline");
|
|
5
|
+
const client_js_1 = require("@mtcute/core/client.js");
|
|
6
|
+
const platform_js_1 = require("@mtcute/core/platform.js");
|
|
7
|
+
const sqlite_1 = require("@mtcute/sqlite");
|
|
8
|
+
const download_file_js_1 = require("./methods/download-file.js");
|
|
9
|
+
const download_node_stream_js_1 = require("./methods/download-node-stream.js");
|
|
10
|
+
const platform_js_2 = require("./platform.js");
|
|
11
|
+
const crypto_js_1 = require("./utils/crypto.js");
|
|
12
|
+
const tcp_js_1 = require("./utils/tcp.js");
|
|
13
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
14
|
+
let nativeCrypto;
|
|
15
|
+
try {
|
|
16
|
+
//
|
|
17
|
+
// eslint-disable-next-line
|
|
18
|
+
nativeCrypto = require('@mtcute/crypto-node').NodeNativeCryptoProvider;
|
|
19
|
+
}
|
|
20
|
+
catch (e) { }
|
|
21
|
+
class BaseTelegramClient extends client_js_1.BaseTelegramClient {
|
|
22
|
+
constructor(opts) {
|
|
23
|
+
if (!opts.platformless)
|
|
24
|
+
(0, platform_js_1.setPlatform)(new platform_js_2.NodePlatform());
|
|
25
|
+
super({
|
|
26
|
+
// eslint-disable-next-line
|
|
27
|
+
crypto: nativeCrypto ? new nativeCrypto() : new crypto_js_1.NodeCryptoProvider(),
|
|
28
|
+
transport: () => new tcp_js_1.TcpTransport(),
|
|
29
|
+
...opts,
|
|
30
|
+
storage: typeof opts.storage === 'string' ?
|
|
31
|
+
new sqlite_1.SqliteStorage(opts.storage) :
|
|
32
|
+
opts.storage ?? new sqlite_1.SqliteStorage('client.session'),
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
exports.BaseTelegramClient = BaseTelegramClient;
|
|
37
|
+
/**
|
|
38
|
+
* Telegram client for use in Node.js
|
|
39
|
+
*/
|
|
40
|
+
class TelegramClient extends client_js_1.TelegramClient {
|
|
41
|
+
constructor(opts) {
|
|
42
|
+
if ('client' in opts) {
|
|
43
|
+
super(opts);
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
super({
|
|
47
|
+
client: new BaseTelegramClient(opts),
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Tiny wrapper over Node `readline` package
|
|
52
|
+
* for simpler user input for `.run()` method.
|
|
53
|
+
*
|
|
54
|
+
* Associated `readline` interface is closed
|
|
55
|
+
* after `run()` returns, or with the client.
|
|
56
|
+
*
|
|
57
|
+
* @param text Text of the question
|
|
58
|
+
*/
|
|
59
|
+
input(text) {
|
|
60
|
+
if (!this._rl) {
|
|
61
|
+
this._rl = (0, readline_1.createInterface)({
|
|
62
|
+
input: process.stdin,
|
|
63
|
+
output: process.stdout,
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
return new Promise((res) => this._rl?.question(text, res));
|
|
67
|
+
}
|
|
68
|
+
close() {
|
|
69
|
+
this._rl?.close();
|
|
70
|
+
return super.close();
|
|
71
|
+
}
|
|
72
|
+
start(params = {}) {
|
|
73
|
+
if (!params.botToken) {
|
|
74
|
+
if (!params.phone)
|
|
75
|
+
params.phone = () => this.input('phone > ');
|
|
76
|
+
if (!params.code)
|
|
77
|
+
params.code = () => this.input('code > ');
|
|
78
|
+
if (!params.password) {
|
|
79
|
+
params.password = () => this.input('2fa password > ');
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return super.start(params).then((user) => {
|
|
83
|
+
if (this._rl) {
|
|
84
|
+
this._rl.close();
|
|
85
|
+
delete this._rl;
|
|
86
|
+
}
|
|
87
|
+
return user;
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
run(params, then) {
|
|
91
|
+
if (typeof params === 'function') {
|
|
92
|
+
then = params;
|
|
93
|
+
params = {};
|
|
94
|
+
}
|
|
95
|
+
this.start(params)
|
|
96
|
+
.then(then)
|
|
97
|
+
.catch((err) => this.emitError(err));
|
|
98
|
+
}
|
|
99
|
+
downloadToFile(filename, location, params) {
|
|
100
|
+
return (0, download_file_js_1.downloadToFile)(this, filename, location, params);
|
|
101
|
+
}
|
|
102
|
+
downloadAsNodeStream(location, params) {
|
|
103
|
+
return (0, download_node_stream_js_1.downloadAsNodeStream)(this, location, params);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
exports.TelegramClient = TelegramClient;
|
|
107
|
+
//# sourceMappingURL=client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.js","sourceRoot":"","sources":["../../src/client.ts"],"names":[],"mappings":";;;AACA,uCAAoE;AAGpE,sDAK+B;AAC/B,0DAAsD;AACtD,2CAA8C;AAE9C,iEAA2D;AAC3D,+EAAwE;AACxE,+CAA4C;AAC5C,iDAAsD;AACtD,2CAA6C;AAI7C,8DAA8D;AAC9D,IAAI,YAAiB,CAAA;AAErB,IAAI;IACA,GAAG;IACH,2BAA2B;IAC3B,YAAY,GAAG,OAAO,CAAC,qBAAqB,CAAC,CAAC,wBAAwB,CAAA;CACzE;AAAC,OAAO,CAAC,EAAE,GAAE;AAuBd,MAAa,kBAAmB,SAAQ,8BAAsB;IAC1D,YAAY,IAA+B;QACvC,IAAI,CAAC,IAAI,CAAC,YAAY;YAAE,IAAA,yBAAW,EAAC,IAAI,0BAAY,EAAE,CAAC,CAAA;QAEvD,KAAK,CAAC;YACF,2BAA2B;YAC3B,MAAM,EAAE,YAAY,CAAC,CAAC,CAAC,IAAI,YAAY,EAAE,CAAC,CAAC,CAAC,IAAI,8BAAkB,EAAE;YACpE,SAAS,EAAE,GAAG,EAAE,CAAC,IAAI,qBAAY,EAAE;YACnC,GAAG,IAAI;YACP,OAAO,EACH,OAAO,IAAI,CAAC,OAAO,KAAK,QAAQ,CAAC,CAAC;gBAC9B,IAAI,sBAAa,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC;gBACjC,IAAI,CAAC,OAAO,IAAI,IAAI,sBAAa,CAAC,gBAAgB,CAAC;SAC9D,CAAC,CAAA;IACN,CAAC;CACJ;AAfD,gDAeC;AAED;;GAEG;AACH,MAAa,cAAe,SAAQ,0BAAkB;IAClD,YAAY,IAA2B;QACnC,IAAI,QAAQ,IAAI,IAAI,EAAE;YAClB,KAAK,CAAC,IAAI,CAAC,CAAA;YAEX,OAAM;SACT;QAED,KAAK,CAAC;YACF,MAAM,EAAE,IAAI,kBAAkB,CAAC,IAAI,CAAC;SACvC,CAAC,CAAA;IACN,CAAC;IAID;;;;;;;;OAQG;IACH,KAAK,CAAC,IAAY;QACd,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE;YACX,IAAI,CAAC,GAAG,GAAG,IAAA,0BAAe,EAAC;gBACvB,KAAK,EAAE,OAAO,CAAC,KAAK;gBACpB,MAAM,EAAE,OAAO,CAAC,MAAM;aACzB,CAAC,CAAA;SACL;QAED,OAAO,IAAI,OAAO,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,IAAI,CAAC,GAAG,EAAE,QAAQ,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC,CAAA;IAC9D,CAAC;IAED,KAAK;QACD,IAAI,CAAC,GAAG,EAAE,KAAK,EAAE,CAAA;QAEjB,OAAO,KAAK,CAAC,KAAK,EAAE,CAAA;IACxB,CAAC;IAED,KAAK,CAAC,SAAqD,EAAE;QACzD,IAAI,CAAC,MAAM,CAAC,QAAQ,EAAE;YAClB,IAAI,CAAC,MAAM,CAAC,KAAK;gBAAE,MAAM,CAAC,KAAK,GAAG,GAAG,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,UAAU,CAAC,CAAA;YAC9D,IAAI,CAAC,MAAM,CAAC,IAAI;gBAAE,MAAM,CAAC,IAAI,GAAG,GAAG,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC,CAAA;YAE3D,IAAI,CAAC,MAAM,CAAC,QAAQ,EAAE;gBAClB,MAAM,CAAC,QAAQ,GAAG,GAAG,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,iBAAiB,CAAC,CAAA;aACxD;SACJ;QAED,OAAO,KAAK,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE;YACrC,IAAI,IAAI,CAAC,GAAG,EAAE;gBACV,IAAI,CAAC,GAAG,CAAC,KAAK,EAAE,CAAA;gBAChB,OAAO,IAAI,CAAC,GAAG,CAAA;aAClB;YAED,OAAO,IAAI,CAAA;QACf,CAAC,CAAC,CAAA;IACN,CAAC;IAED,GAAG,CACC,MAAuF,EACvF,IAA2C;QAE3C,IAAI,OAAO,MAAM,KAAK,UAAU,EAAE;YAC9B,IAAI,GAAG,MAAM,CAAA;YACb,MAAM,GAAG,EAAE,CAAA;SACd;QAED,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC;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,IAAA,iCAAc,EAAC,IAAI,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,CAAC,CAAA;IAC3D,CAAC;IAED,oBAAoB,CAAC,QAA8B,EAAE,MAA2C;QAC5F,OAAO,IAAA,8CAAoB,EAAC,IAAI,EAAE,QAAQ,EAAE,MAAM,CAAC,CAAA;IACvD,CAAC;CACJ;AAtFD,wCAsFC","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 // \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/cjs/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/cjs/index.js
CHANGED
|
@@ -14,89 +14,13 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
__exportStar(require("@mtcute/
|
|
17
|
+
__exportStar(require("./client.js"), exports);
|
|
18
|
+
__exportStar(require("./platform.js"), exports);
|
|
19
|
+
__exportStar(require("./utils/crypto.js"), exports);
|
|
20
|
+
__exportStar(require("./utils/tcp.js"), exports);
|
|
21
|
+
__exportStar(require("./worker.js"), exports);
|
|
22
|
+
__exportStar(require("@mtcute/core"), exports);
|
|
23
23
|
__exportStar(require("@mtcute/html-parser"), exports);
|
|
24
24
|
__exportStar(require("@mtcute/markdown-parser"), exports);
|
|
25
|
-
|
|
26
|
-
let nativeCrypto;
|
|
27
|
-
try {
|
|
28
|
-
//
|
|
29
|
-
// eslint-disable-next-line
|
|
30
|
-
nativeCrypto = require('@mtcute/crypto-node').NodeNativeCryptoProvider;
|
|
31
|
-
}
|
|
32
|
-
catch (e) { }
|
|
33
|
-
/**
|
|
34
|
-
* Tiny wrapper over {@link TelegramClient} for usage inside Node JS.
|
|
35
|
-
*
|
|
36
|
-
* This class automatically manages native
|
|
37
|
-
* crypto addon and defaults to SQLite session (unlike `TelegarmClient`,
|
|
38
|
-
* which defaults to a JSON file on Node).
|
|
39
|
-
*/
|
|
40
|
-
class NodeTelegramClient extends client_1.TelegramClient {
|
|
41
|
-
constructor(opts) {
|
|
42
|
-
super({
|
|
43
|
-
// eslint-disable-next-line
|
|
44
|
-
crypto: nativeCrypto ? () => new nativeCrypto() : undefined,
|
|
45
|
-
...opts,
|
|
46
|
-
storage: typeof opts.storage === 'string' ?
|
|
47
|
-
new sqlite_1.SqliteStorage(opts.storage) :
|
|
48
|
-
opts.storage ?? new sqlite_1.SqliteStorage('client.session'),
|
|
49
|
-
});
|
|
50
|
-
}
|
|
51
|
-
/**
|
|
52
|
-
* Tiny wrapper over Node `readline` package
|
|
53
|
-
* for simpler user input for `.run()` method.
|
|
54
|
-
*
|
|
55
|
-
* Associated `readline` interface is closed
|
|
56
|
-
* after `run()` returns, or with the client.
|
|
57
|
-
*
|
|
58
|
-
* @param text Text of the question
|
|
59
|
-
*/
|
|
60
|
-
input(text) {
|
|
61
|
-
if (!this._rl) {
|
|
62
|
-
this._rl = (0, readline_1.createInterface)({
|
|
63
|
-
input: process.stdin,
|
|
64
|
-
output: process.stdout,
|
|
65
|
-
});
|
|
66
|
-
}
|
|
67
|
-
return new Promise((res) => this._rl?.question(text, res));
|
|
68
|
-
}
|
|
69
|
-
close() {
|
|
70
|
-
this._rl?.close();
|
|
71
|
-
return super.close();
|
|
72
|
-
}
|
|
73
|
-
start(params = {}) {
|
|
74
|
-
if (!params.botToken) {
|
|
75
|
-
if (!params.phone)
|
|
76
|
-
params.phone = () => this.input('phone > ');
|
|
77
|
-
if (!params.code)
|
|
78
|
-
params.code = () => this.input('code > ');
|
|
79
|
-
if (!params.password) {
|
|
80
|
-
params.password = () => this.input('2fa password > ');
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
return super.start(params).then((user) => {
|
|
84
|
-
if (this._rl) {
|
|
85
|
-
this._rl.close();
|
|
86
|
-
delete this._rl;
|
|
87
|
-
}
|
|
88
|
-
return user;
|
|
89
|
-
});
|
|
90
|
-
}
|
|
91
|
-
run(params, then) {
|
|
92
|
-
if (typeof params === 'function') {
|
|
93
|
-
then = params;
|
|
94
|
-
params = {};
|
|
95
|
-
}
|
|
96
|
-
this.start(params)
|
|
97
|
-
.then(then)
|
|
98
|
-
.catch((err) => this._emitError(err));
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
exports.NodeTelegramClient = NodeTelegramClient;
|
|
25
|
+
__exportStar(require("@mtcute/sqlite"), exports);
|
|
102
26
|
//# sourceMappingURL=index.js.map
|
package/cjs/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,8CAA2B;AAC3B,gDAA6B;AAC7B,oDAAiC;AACjC,iDAA8B;AAC9B,8CAA2B;AAC3B,+CAA4B;AAC5B,sDAAmC;AACnC,0DAAuC;AACvC,iDAA8B","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,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.downloadToFile = void 0;
|
|
4
|
+
const fs_1 = require("fs");
|
|
5
|
+
const promises_1 = require("fs/promises");
|
|
6
|
+
const core_1 = require("@mtcute/core");
|
|
7
|
+
const methods_js_1 = require("@mtcute/core/methods.js");
|
|
8
|
+
/**
|
|
9
|
+
* Download a remote file to a local file (only for NodeJS).
|
|
10
|
+
* Promise will resolve once the download is complete.
|
|
11
|
+
*
|
|
12
|
+
* @param filename Local file name to which the remote file will be downloaded
|
|
13
|
+
* @param params File download parameters
|
|
14
|
+
*/
|
|
15
|
+
async function downloadToFile(client, filename, location, params) {
|
|
16
|
+
if (location instanceof core_1.FileLocation && ArrayBuffer.isView(location.location)) {
|
|
17
|
+
// early return for inline files
|
|
18
|
+
await (0, promises_1.writeFile)(filename, location.location);
|
|
19
|
+
}
|
|
20
|
+
const output = (0, fs_1.createWriteStream)(filename);
|
|
21
|
+
if (params?.abortSignal) {
|
|
22
|
+
params.abortSignal.addEventListener('abort', () => {
|
|
23
|
+
client.log.debug('aborting file download %s - cleaning up', filename);
|
|
24
|
+
output.destroy();
|
|
25
|
+
(0, fs_1.rmSync)(filename);
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
for await (const chunk of (0, methods_js_1.downloadAsIterable)(client, location, params)) {
|
|
29
|
+
output.write(chunk);
|
|
30
|
+
}
|
|
31
|
+
output.end();
|
|
32
|
+
}
|
|
33
|
+
exports.downloadToFile = downloadToFile;
|
|
34
|
+
//# 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,2BAA8C;AAC9C,0CAAuC;AAEvC,uCAA0G;AAC1G,wDAA4D;AAE5D;;;;;;GAMG;AACI,KAAK,UAAU,cAAc,CAChC,MAAuB,EACvB,QAAgB,EAChB,QAA8B,EAC9B,MAA+B;IAE/B,IAAI,QAAQ,YAAY,mBAAY,IAAI,WAAW,CAAC,MAAM,CAAC,QAAQ,CAAC,QAAQ,CAAC,EAAE;QAC3E,gCAAgC;QAChC,MAAM,IAAA,oBAAS,EAAC,QAAQ,EAAE,QAAQ,CAAC,QAAQ,CAAC,CAAA;KAC/C;IAED,MAAM,MAAM,GAAG,IAAA,sBAAiB,EAAC,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,IAAA,WAAM,EAAC,QAAQ,CAAC,CAAA;QACpB,CAAC,CAAC,CAAA;KACL;IAED,IAAI,KAAK,EAAE,MAAM,KAAK,IAAI,IAAA,+BAAkB,EAAC,MAAM,EAAE,QAAQ,EAAE,MAAM,CAAC,EAAE;QACpE,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,CAAA;KACtB;IAED,MAAM,CAAC,GAAG,EAAE,CAAA;AAChB,CAAC;AA1BD,wCA0BC","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,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.downloadAsNodeStream = void 0;
|
|
4
|
+
const methods_js_1 = require("@mtcute/core/methods.js");
|
|
5
|
+
const stream_utils_js_1 = require("../utils/stream-utils.js");
|
|
6
|
+
/**
|
|
7
|
+
* Download a remote file as a Node.js Readable stream.
|
|
8
|
+
*
|
|
9
|
+
* @param params File download parameters
|
|
10
|
+
*/
|
|
11
|
+
function downloadAsNodeStream(client, location, params) {
|
|
12
|
+
return (0, stream_utils_js_1.webStreamToNode)((0, methods_js_1.downloadAsStream)(client, location, params));
|
|
13
|
+
}
|
|
14
|
+
exports.downloadAsNodeStream = downloadAsNodeStream;
|
|
15
|
+
//# 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,wDAA0D;AAE1D,8DAA0D;AAE1D;;;;GAIG;AACH,SAAgB,oBAAoB,CAChC,MAAuB,EACvB,QAA8B,EAC9B,MAA+B;IAE/B,OAAO,IAAA,iCAAe,EAAC,IAAA,6BAAgB,EAAC,MAAM,EAAE,QAAQ,EAAE,MAAM,CAAC,CAAC,CAAA;AACtE,CAAC;AAND,oDAMC","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/cjs/methods.d.ts
ADDED
package/cjs/methods.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.downloadAsNodeStream = exports.downloadToFile = void 0;
|
|
18
|
+
/* eslint-disable import/export, simple-import-sort/exports */
|
|
19
|
+
__exportStar(require("@mtcute/core/methods.js"), exports);
|
|
20
|
+
var download_file_js_1 = require("./methods/download-file.js");
|
|
21
|
+
Object.defineProperty(exports, "downloadToFile", { enumerable: true, get: function () { return download_file_js_1.downloadToFile; } });
|
|
22
|
+
var download_node_stream_js_1 = require("./methods/download-node-stream.js");
|
|
23
|
+
Object.defineProperty(exports, "downloadAsNodeStream", { enumerable: true, get: function () { return download_node_stream_js_1.downloadAsNodeStream; } });
|
|
24
|
+
//# sourceMappingURL=methods.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"methods.js","sourceRoot":"","sources":["../../src/methods.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,8DAA8D;AAC9D,0DAAuC;AAEvC,+DAA2D;AAAlD,kHAAA,cAAc,OAAA;AACvB,6EAAwE;AAA/D,+HAAA,oBAAoB,OAAA","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/cjs/platform.js
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.NodePlatform = void 0;
|
|
27
|
+
const os = __importStar(require("os"));
|
|
28
|
+
const exit_hook_js_1 = require("./utils/exit-hook.js");
|
|
29
|
+
const logging_js_1 = require("./utils/logging.js");
|
|
30
|
+
const normalize_file_js_1 = require("./utils/normalize-file.js");
|
|
31
|
+
const BUFFER_BASE64_URL_AVAILABLE = typeof Buffer.isEncoding === 'function' && Buffer.isEncoding('base64url');
|
|
32
|
+
const toBuffer = (buf) => Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength);
|
|
33
|
+
class NodePlatform {
|
|
34
|
+
getDeviceModel() {
|
|
35
|
+
return `${os.type()} ${os.arch()} ${os.release()}`;
|
|
36
|
+
}
|
|
37
|
+
getDefaultLogLevel() {
|
|
38
|
+
const envLogLevel = parseInt(process.env.MTCUTE_LOG_LEVEL ?? '');
|
|
39
|
+
if (!isNaN(envLogLevel)) {
|
|
40
|
+
return envLogLevel;
|
|
41
|
+
}
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
// ITlPlatform
|
|
45
|
+
utf8ByteLength(str) {
|
|
46
|
+
return Buffer.byteLength(str, 'utf8');
|
|
47
|
+
}
|
|
48
|
+
utf8Encode(str) {
|
|
49
|
+
return Buffer.from(str, 'utf8');
|
|
50
|
+
}
|
|
51
|
+
utf8Decode(buf) {
|
|
52
|
+
return toBuffer(buf).toString('utf8');
|
|
53
|
+
}
|
|
54
|
+
hexEncode(buf) {
|
|
55
|
+
return toBuffer(buf).toString('hex');
|
|
56
|
+
}
|
|
57
|
+
hexDecode(str) {
|
|
58
|
+
return Buffer.from(str, 'hex');
|
|
59
|
+
}
|
|
60
|
+
base64Encode(buf, url = false) {
|
|
61
|
+
const nodeBuffer = toBuffer(buf);
|
|
62
|
+
if (url && BUFFER_BASE64_URL_AVAILABLE)
|
|
63
|
+
return nodeBuffer.toString('base64url');
|
|
64
|
+
const str = nodeBuffer.toString('base64');
|
|
65
|
+
if (url)
|
|
66
|
+
return str.replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, '');
|
|
67
|
+
return str;
|
|
68
|
+
}
|
|
69
|
+
base64Decode(string, url = false) {
|
|
70
|
+
if (url && BUFFER_BASE64_URL_AVAILABLE) {
|
|
71
|
+
return Buffer.from(string, 'base64url');
|
|
72
|
+
}
|
|
73
|
+
if (url) {
|
|
74
|
+
string = string.replace(/-/g, '+').replace(/_/g, '/');
|
|
75
|
+
while (string.length % 4)
|
|
76
|
+
string += '=';
|
|
77
|
+
}
|
|
78
|
+
return Buffer.from(string, 'base64');
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
exports.NodePlatform = NodePlatform;
|
|
82
|
+
NodePlatform.prototype.log = logging_js_1.defaultLoggingHandler;
|
|
83
|
+
NodePlatform.prototype.beforeExit = exit_hook_js_1.beforeExit;
|
|
84
|
+
NodePlatform.prototype.normalizeFile = normalize_file_js_1.normalizeFile;
|
|
85
|
+
//# sourceMappingURL=platform.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"platform.js","sourceRoot":"","sources":["../../src/platform.ts"],"names":[],"mappings":"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","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
|
+
}
|