@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.
Files changed (55) hide show
  1. package/client.cjs +101 -0
  2. package/client.d.ts +2 -9
  3. package/client.js +96 -0
  4. package/index.cjs +29 -423
  5. package/index.d.ts +2 -1
  6. package/index.js +13 -397
  7. package/{chunks/cjs/BpvQ752Q.js → methods/download-file.cjs} +3 -7
  8. package/{chunks/es/CnOjjhdK.js → methods/download-file.js} +1 -6
  9. package/methods/download-node-stream.cjs +13 -0
  10. package/methods/download-node-stream.d.ts +1 -1
  11. package/methods/download-node-stream.js +8 -0
  12. package/methods.cjs +5 -4
  13. package/methods.js +2 -1
  14. package/package.json +19 -16
  15. package/sqlite/driver.cjs +27 -0
  16. package/sqlite/driver.d.ts +1 -1
  17. package/sqlite/driver.js +22 -0
  18. package/sqlite/index.cjs +18 -0
  19. package/sqlite/index.d.ts +1 -1
  20. package/sqlite/index.js +13 -0
  21. package/{chunks/cjs/D7i-4e0W.js → utils/crypto.cjs} +4 -3
  22. package/{chunks/es/HZgHrOPU.js → utils/crypto.js} +1 -1
  23. package/utils/exit-hook.cjs +46 -0
  24. package/utils/exit-hook.js +41 -0
  25. package/utils/logging.cjs +33 -0
  26. package/utils/logging.js +28 -0
  27. package/utils/normalize-file.cjs +33 -0
  28. package/utils/normalize-file.js +28 -0
  29. package/utils/platform.cjs +44 -0
  30. package/{common-internals-node → utils}/platform.d.ts +1 -8
  31. package/utils/platform.js +22 -0
  32. package/utils/proxies.cjs +73 -0
  33. package/utils/proxies.d.ts +34 -0
  34. package/utils/proxies.js +63 -0
  35. package/utils/tcp.cjs +18 -0
  36. package/utils/tcp.d.ts +6 -28
  37. package/utils/tcp.js +13 -0
  38. package/utils.cjs +3 -6
  39. package/utils.d.ts +0 -1
  40. package/utils.js +2 -5
  41. package/worker.cjs +49 -0
  42. package/worker.d.ts +5 -3
  43. package/worker.js +44 -0
  44. package/chunks/cjs/HP2yqAk_.js +0 -79
  45. package/chunks/cjs/package.json +0 -3
  46. package/chunks/es/CKso6cAV.js +0 -75
  47. package/index.d.cts +0 -8
  48. package/methods.d.cts +0 -3
  49. package/utils/stream-utils.d.ts +0 -3
  50. package/utils/stream-utils.test.d.ts +0 -1
  51. package/utils/tcp.test.d.ts +0 -1
  52. package/utils/version.d.ts +0 -3
  53. package/utils.d.cts +0 -3
  54. /package/{common-internals-node → utils}/exit-hook.d.ts +0 -0
  55. /package/{common-internals-node → utils}/logging.d.ts +0 -0
@@ -1,9 +1,10 @@
1
1
  if (typeof globalThis !== "undefined" && !globalThis._MTCUTE_CJS_DEPRECATION_WARNED) {
2
2
  globalThis._MTCUTE_CJS_DEPRECATION_WARNED = true;
3
- console.warn("[@mtcute/node] CommonJS support is deprecated and will be removed soon. Please consider switching to ESM, it's " + (/* @__PURE__ */ new Date()).getFullYear() + " already.");
4
- console.warn("[@mtcute/node] Learn more about switching to ESM: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c");
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
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
7
8
  const node_crypto = require("node:crypto");
8
9
  const promises = require("node:fs/promises");
9
10
  const node_module = require("node:module");
@@ -54,7 +55,7 @@ class BaseNodeCryptoProvider extends utils_js.BaseCryptoProvider {
54
55
  }
55
56
  class NodeCryptoProvider extends BaseNodeCryptoProvider {
56
57
  async initialize() {
57
- const require$1 = node_module.createRequire(typeof document === "undefined" ? require("url").pathToFileURL(__filename).href : _documentCurrentScript && _documentCurrentScript.src || new URL("chunks/cjs/D7i-4e0W.js", document.baseURI).href);
58
+ const require$1 = node_module.createRequire(typeof document === "undefined" ? require("url").pathToFileURL(__filename).href : _documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === "SCRIPT" && _documentCurrentScript.src || new URL("utils/crypto.cjs", document.baseURI).href);
58
59
  const wasmFile = require$1.resolve("@mtcute/wasm/mtcute.wasm");
59
60
  const wasm$1 = await promises.readFile(wasmFile);
60
61
  wasm.initSync(wasm$1);
@@ -3,7 +3,7 @@ import { readFile } from "node:fs/promises";
3
3
  import { createRequire } from "node:module";
4
4
  import { deflateSync, gunzipSync } from "node:zlib";
5
5
  import { BaseCryptoProvider } from "@mtcute/core/utils.js";
6
- import { initSync, ige256Encrypt, ige256Decrypt } from "@mtcute/wasm";
6
+ import { initSync, ige256Decrypt, ige256Encrypt } from "@mtcute/wasm";
7
7
  class BaseNodeCryptoProvider extends BaseCryptoProvider {
8
8
  createAesCtr(key, iv) {
9
9
  const cipher = createCipheriv(`aes-${key.length * 8}-ctr`, key, iv);
@@ -0,0 +1,46 @@
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
+ let installed = false;
9
+ let handled = false;
10
+ const callbacks = /* @__PURE__ */ new Set();
11
+ const myHandlers = /* @__PURE__ */ new Map();
12
+ function register(shouldManuallyExit, signal, event) {
13
+ function eventHandler() {
14
+ if (handled) {
15
+ return;
16
+ }
17
+ handled = true;
18
+ for (const callback of callbacks) {
19
+ callback();
20
+ }
21
+ for (const [event2, handler] of myHandlers) {
22
+ process.off(event2, handler);
23
+ }
24
+ if (shouldManuallyExit) {
25
+ process.kill(process.pid, signal);
26
+ }
27
+ }
28
+ process.on(event, eventHandler);
29
+ myHandlers.set(event, eventHandler);
30
+ }
31
+ function beforeExit(fn) {
32
+ if (typeof process === "undefined") return () => {
33
+ };
34
+ if (!installed) {
35
+ installed = true;
36
+ register(true, 0, "beforeExit");
37
+ register(true, 2, "SIGINT");
38
+ register(true, 15, "SIGTERM");
39
+ register(false, 15, "exit");
40
+ }
41
+ callbacks.add(fn);
42
+ return () => {
43
+ callbacks.delete(fn);
44
+ };
45
+ }
46
+ exports.beforeExit = beforeExit;
@@ -0,0 +1,41 @@
1
+ let installed = false;
2
+ let handled = false;
3
+ const callbacks = /* @__PURE__ */ new Set();
4
+ const myHandlers = /* @__PURE__ */ new Map();
5
+ function register(shouldManuallyExit, signal, event) {
6
+ function eventHandler() {
7
+ if (handled) {
8
+ return;
9
+ }
10
+ handled = true;
11
+ for (const callback of callbacks) {
12
+ callback();
13
+ }
14
+ for (const [event2, handler] of myHandlers) {
15
+ process.off(event2, handler);
16
+ }
17
+ if (shouldManuallyExit) {
18
+ process.kill(process.pid, signal);
19
+ }
20
+ }
21
+ process.on(event, eventHandler);
22
+ myHandlers.set(event, eventHandler);
23
+ }
24
+ function beforeExit(fn) {
25
+ if (typeof process === "undefined") return () => {
26
+ };
27
+ if (!installed) {
28
+ installed = true;
29
+ register(true, 0, "beforeExit");
30
+ register(true, 2, "SIGINT");
31
+ register(true, 15, "SIGTERM");
32
+ register(false, 15, "exit");
33
+ }
34
+ callbacks.add(fn);
35
+ return () => {
36
+ callbacks.delete(fn);
37
+ };
38
+ }
39
+ export {
40
+ beforeExit
41
+ };
@@ -0,0 +1,33 @@
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 isTty = typeof process === "object" && Boolean(process.stdout?.isTTY);
9
+ const BASE_FORMAT = isTty ? "%s [%s] [%s%s\x1B[0m] " : "%s [%s] [%s] ";
10
+ const LEVEL_NAMES = isTty ? [
11
+ "",
12
+ // OFF
13
+ "\x1B[31mERR\x1B[0m",
14
+ "\x1B[33mWRN\x1B[0m",
15
+ "\x1B[34mINF\x1B[0m",
16
+ "\x1B[36mDBG\x1B[0m",
17
+ "\x1B[35mVRB\x1B[0m"
18
+ ] : [
19
+ "",
20
+ // OFF
21
+ "ERR",
22
+ "WRN",
23
+ "INF",
24
+ "DBG",
25
+ "VRB"
26
+ ];
27
+ const TAG_COLORS = [6, 2, 3, 4, 5, 1].map((i) => `\x1B[3${i};1m`);
28
+ const defaultLoggingHandler = isTty ? (color, level, tag, fmt, args) => {
29
+ console.log(BASE_FORMAT + fmt, (/* @__PURE__ */ new Date()).toISOString(), LEVEL_NAMES[level], TAG_COLORS[color], tag, ...args);
30
+ } : (color, level, tag, fmt, args) => {
31
+ console.log(BASE_FORMAT + fmt, (/* @__PURE__ */ new Date()).toISOString(), LEVEL_NAMES[level], tag, ...args);
32
+ };
33
+ exports.defaultLoggingHandler = defaultLoggingHandler;
@@ -0,0 +1,28 @@
1
+ const isTty = typeof process === "object" && Boolean(process.stdout?.isTTY);
2
+ const BASE_FORMAT = isTty ? "%s [%s] [%s%s\x1B[0m] " : "%s [%s] [%s] ";
3
+ const LEVEL_NAMES = isTty ? [
4
+ "",
5
+ // OFF
6
+ "\x1B[31mERR\x1B[0m",
7
+ "\x1B[33mWRN\x1B[0m",
8
+ "\x1B[34mINF\x1B[0m",
9
+ "\x1B[36mDBG\x1B[0m",
10
+ "\x1B[35mVRB\x1B[0m"
11
+ ] : [
12
+ "",
13
+ // OFF
14
+ "ERR",
15
+ "WRN",
16
+ "INF",
17
+ "DBG",
18
+ "VRB"
19
+ ];
20
+ const TAG_COLORS = [6, 2, 3, 4, 5, 1].map((i) => `\x1B[3${i};1m`);
21
+ const defaultLoggingHandler = isTty ? (color, level, tag, fmt, args) => {
22
+ console.log(BASE_FORMAT + fmt, (/* @__PURE__ */ new Date()).toISOString(), LEVEL_NAMES[level], TAG_COLORS[color], tag, ...args);
23
+ } : (color, level, tag, fmt, args) => {
24
+ console.log(BASE_FORMAT + fmt, (/* @__PURE__ */ new Date()).toISOString(), LEVEL_NAMES[level], tag, ...args);
25
+ };
26
+ export {
27
+ defaultLoggingHandler
28
+ };
@@ -0,0 +1,33 @@
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_fs = require("node:fs");
9
+ const promises = require("node:fs/promises");
10
+ const node_path = require("node:path");
11
+ const node_stream = require("node:stream");
12
+ const node = require("@fuman/node");
13
+ async function normalizeFile(file) {
14
+ if (typeof file === "string") {
15
+ file = node_fs.createReadStream(file);
16
+ }
17
+ if (file instanceof node_fs.ReadStream) {
18
+ const fileName = node_path.basename(file.path.toString());
19
+ const fileSize = await promises.stat(file.path.toString()).then((stat2) => stat2.size);
20
+ return {
21
+ file: node.nodeReadableToFuman(file),
22
+ fileName,
23
+ fileSize
24
+ };
25
+ }
26
+ if (file instanceof node_stream.Readable) {
27
+ return {
28
+ file: node.nodeReadableToFuman(file)
29
+ };
30
+ }
31
+ return null;
32
+ }
33
+ exports.normalizeFile = normalizeFile;
@@ -0,0 +1,28 @@
1
+ import { createReadStream, ReadStream } from "node:fs";
2
+ import { stat } from "node:fs/promises";
3
+ import { basename } from "node:path";
4
+ import { Readable } from "node:stream";
5
+ import { nodeReadableToFuman } from "@fuman/node";
6
+ async function normalizeFile(file) {
7
+ if (typeof file === "string") {
8
+ file = createReadStream(file);
9
+ }
10
+ if (file instanceof ReadStream) {
11
+ const fileName = basename(file.path.toString());
12
+ const fileSize = await stat(file.path.toString()).then((stat2) => stat2.size);
13
+ return {
14
+ file: nodeReadableToFuman(file),
15
+ fileName,
16
+ fileSize
17
+ };
18
+ }
19
+ if (file instanceof Readable) {
20
+ return {
21
+ file: nodeReadableToFuman(file)
22
+ };
23
+ }
24
+ return null;
25
+ }
26
+ export {
27
+ normalizeFile
28
+ };
@@ -0,0 +1,44 @@
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 os = require("node:os");
9
+ const normalizeFile = require("./normalize-file.cjs");
10
+ const exitHook = require("./exit-hook.cjs");
11
+ const logging = require("./logging.cjs");
12
+ function _interopNamespaceDefault(e) {
13
+ const n = Object.create(null, { [Symbol.toStringTag]: { value: "Module" } });
14
+ if (e) {
15
+ for (const k in e) {
16
+ if (k !== "default") {
17
+ const d = Object.getOwnPropertyDescriptor(e, k);
18
+ Object.defineProperty(n, k, d.get ? d : {
19
+ enumerable: true,
20
+ get: () => e[k]
21
+ });
22
+ }
23
+ }
24
+ }
25
+ n.default = e;
26
+ return Object.freeze(n);
27
+ }
28
+ const os__namespace = /* @__PURE__ */ _interopNamespaceDefault(os);
29
+ class NodePlatform {
30
+ getDeviceModel() {
31
+ return `Node.js/${process.version} (${os__namespace.type()} ${os__namespace.arch()})`;
32
+ }
33
+ getDefaultLogLevel() {
34
+ const envLogLevel = Number.parseInt(process.env.MTCUTE_LOG_LEVEL ?? "");
35
+ if (!Number.isNaN(envLogLevel)) {
36
+ return envLogLevel;
37
+ }
38
+ return null;
39
+ }
40
+ }
41
+ NodePlatform.prototype.log = logging.defaultLoggingHandler;
42
+ NodePlatform.prototype.beforeExit = exitHook.beforeExit;
43
+ NodePlatform.prototype.normalizeFile = normalizeFile.normalizeFile;
44
+ exports.NodePlatform = NodePlatform;
@@ -1,4 +1,4 @@
1
- import { ICorePlatform } from '@mtcute/core/platform.js';
1
+ import { ICorePlatform } from '@mtcute/core';
2
2
  import { normalizeFile } from '../utils/normalize-file.js';
3
3
  import { beforeExit } from './exit-hook.js';
4
4
  import { defaultLoggingHandler } from './logging.js';
@@ -8,11 +8,4 @@ export declare class NodePlatform implements ICorePlatform {
8
8
  normalizeFile: typeof normalizeFile;
9
9
  getDeviceModel(): string;
10
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
11
  }
@@ -0,0 +1,22 @@
1
+ import * as os from "node:os";
2
+ import { normalizeFile } from "./normalize-file.js";
3
+ import { beforeExit } from "./exit-hook.js";
4
+ import { defaultLoggingHandler } from "./logging.js";
5
+ class NodePlatform {
6
+ getDeviceModel() {
7
+ return `Node.js/${process.version} (${os.type()} ${os.arch()})`;
8
+ }
9
+ getDefaultLogLevel() {
10
+ const envLogLevel = Number.parseInt(process.env.MTCUTE_LOG_LEVEL ?? "");
11
+ if (!Number.isNaN(envLogLevel)) {
12
+ return envLogLevel;
13
+ }
14
+ return null;
15
+ }
16
+ }
17
+ NodePlatform.prototype.log = defaultLoggingHandler;
18
+ NodePlatform.prototype.beforeExit = beforeExit;
19
+ NodePlatform.prototype.normalizeFile = normalizeFile;
20
+ export {
21
+ NodePlatform
22
+ };
@@ -0,0 +1,73 @@
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 net = require("@fuman/net");
9
+ const node = require("@fuman/node");
10
+ const core = require("@mtcute/core");
11
+ class HttpProxyTcpTransport {
12
+ constructor(proxy) {
13
+ this.proxy = proxy;
14
+ }
15
+ async connect(dc) {
16
+ let conn;
17
+ if (this.proxy.tls) {
18
+ conn = await node.connectTls({
19
+ address: this.proxy.host,
20
+ port: this.proxy.port,
21
+ extraOptions: this.proxy.tlsOptions
22
+ });
23
+ } else {
24
+ conn = await node.connectTcp({
25
+ address: this.proxy.host,
26
+ port: this.proxy.port
27
+ });
28
+ }
29
+ await net.performHttpProxyHandshake(conn, conn, this.proxy, {
30
+ address: dc.ipAddress,
31
+ port: dc.port
32
+ });
33
+ return conn;
34
+ }
35
+ packetCodec() {
36
+ return new core.IntermediatePacketCodec();
37
+ }
38
+ }
39
+ class SocksProxyTcpTransport {
40
+ constructor(proxy) {
41
+ this.proxy = proxy;
42
+ }
43
+ async connect(dc) {
44
+ const conn = await node.connectTcp({
45
+ address: this.proxy.host,
46
+ port: this.proxy.port
47
+ });
48
+ await net.performSocksHandshake(conn, conn, this.proxy, {
49
+ address: dc.ipAddress,
50
+ port: dc.port
51
+ });
52
+ return conn;
53
+ }
54
+ packetCodec() {
55
+ return new core.IntermediatePacketCodec();
56
+ }
57
+ }
58
+ class MtProxyTcpTransport extends core.BaseMtProxyTransport {
59
+ async _connectTcp(endpoint) {
60
+ return node.connectTcp(endpoint);
61
+ }
62
+ }
63
+ Object.defineProperty(exports, "HttpProxyConnectionError", {
64
+ enumerable: true,
65
+ get: () => net.HttpProxyConnectionError
66
+ });
67
+ Object.defineProperty(exports, "SocksProxyConnectionError", {
68
+ enumerable: true,
69
+ get: () => net.SocksProxyConnectionError
70
+ });
71
+ exports.HttpProxyTcpTransport = HttpProxyTcpTransport;
72
+ exports.MtProxyTcpTransport = MtProxyTcpTransport;
73
+ exports.SocksProxyTcpTransport = SocksProxyTcpTransport;
@@ -0,0 +1,34 @@
1
+ import { HttpProxySettings as FumanHttpProxySettings, ITcpConnection, SocksProxySettings, TcpEndpoint } from '@fuman/net';
2
+ import { BasicDcOption } from '@mtcute/core/utils.js';
3
+ import { SecureContextOptions } from 'node:tls';
4
+ import { BaseMtProxyTransport, IntermediatePacketCodec, ITelegramConnection, TelegramTransport } from '@mtcute/core';
5
+ export type { SocksProxySettings } from '@fuman/net';
6
+ export { HttpProxyConnectionError, SocksProxyConnectionError } from '@fuman/net';
7
+ export type { MtProxySettings } from '@mtcute/core';
8
+ export interface HttpProxySettings extends FumanHttpProxySettings {
9
+ /**
10
+ * Whether this is a HTTPS proxy (by default it is regular HTTP).
11
+ */
12
+ tls?: boolean;
13
+ /**
14
+ * Additional TLS options, used if `tls = true`.
15
+ * Can contain stuff like custom certificate, host,
16
+ * or whatever.
17
+ */
18
+ tlsOptions?: SecureContextOptions;
19
+ }
20
+ export declare class HttpProxyTcpTransport implements TelegramTransport {
21
+ readonly proxy: HttpProxySettings;
22
+ constructor(proxy: HttpProxySettings);
23
+ connect(dc: BasicDcOption): Promise<ITelegramConnection>;
24
+ packetCodec(): IntermediatePacketCodec;
25
+ }
26
+ export declare class SocksProxyTcpTransport implements TelegramTransport {
27
+ readonly proxy: SocksProxySettings;
28
+ constructor(proxy: SocksProxySettings);
29
+ connect(dc: BasicDcOption): Promise<ITelegramConnection>;
30
+ packetCodec(): IntermediatePacketCodec;
31
+ }
32
+ export declare class MtProxyTcpTransport extends BaseMtProxyTransport {
33
+ _connectTcp(endpoint: TcpEndpoint): Promise<ITcpConnection>;
34
+ }
@@ -0,0 +1,63 @@
1
+ import { performHttpProxyHandshake, performSocksHandshake } from "@fuman/net";
2
+ import { HttpProxyConnectionError, SocksProxyConnectionError } from "@fuman/net";
3
+ import { connectTls, connectTcp } from "@fuman/node";
4
+ import { IntermediatePacketCodec, BaseMtProxyTransport } from "@mtcute/core";
5
+ class HttpProxyTcpTransport {
6
+ constructor(proxy) {
7
+ this.proxy = proxy;
8
+ }
9
+ async connect(dc) {
10
+ let conn;
11
+ if (this.proxy.tls) {
12
+ conn = await connectTls({
13
+ address: this.proxy.host,
14
+ port: this.proxy.port,
15
+ extraOptions: this.proxy.tlsOptions
16
+ });
17
+ } else {
18
+ conn = await connectTcp({
19
+ address: this.proxy.host,
20
+ port: this.proxy.port
21
+ });
22
+ }
23
+ await performHttpProxyHandshake(conn, conn, this.proxy, {
24
+ address: dc.ipAddress,
25
+ port: dc.port
26
+ });
27
+ return conn;
28
+ }
29
+ packetCodec() {
30
+ return new IntermediatePacketCodec();
31
+ }
32
+ }
33
+ class SocksProxyTcpTransport {
34
+ constructor(proxy) {
35
+ this.proxy = proxy;
36
+ }
37
+ async connect(dc) {
38
+ const conn = await connectTcp({
39
+ address: this.proxy.host,
40
+ port: this.proxy.port
41
+ });
42
+ await performSocksHandshake(conn, conn, this.proxy, {
43
+ address: dc.ipAddress,
44
+ port: dc.port
45
+ });
46
+ return conn;
47
+ }
48
+ packetCodec() {
49
+ return new IntermediatePacketCodec();
50
+ }
51
+ }
52
+ class MtProxyTcpTransport extends BaseMtProxyTransport {
53
+ async _connectTcp(endpoint) {
54
+ return connectTcp(endpoint);
55
+ }
56
+ }
57
+ export {
58
+ HttpProxyConnectionError,
59
+ HttpProxyTcpTransport,
60
+ MtProxyTcpTransport,
61
+ SocksProxyConnectionError,
62
+ SocksProxyTcpTransport
63
+ };
package/utils/tcp.cjs ADDED
@@ -0,0 +1,18 @@
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 = require("@fuman/node");
9
+ const core = require("@mtcute/core");
10
+ class TcpTransport {
11
+ connect(dc) {
12
+ return node.connectTcp({ address: dc.ipAddress, port: dc.port });
13
+ }
14
+ packetCodec() {
15
+ return new core.IntermediatePacketCodec();
16
+ }
17
+ }
18
+ exports.TcpTransport = TcpTransport;
package/utils/tcp.d.ts CHANGED
@@ -1,29 +1,7 @@
1
- import { default as EventEmitter } from 'node:events';
2
- import { Socket } from 'node:net';
3
- import { IPacketCodec, ITelegramTransport, IntermediatePacketCodec, TransportState } from '@mtcute/core';
4
- import { BasicDcOption, ICryptoProvider, Logger } from '@mtcute/core/utils.js';
5
- /**
6
- * Base for TCP transports.
7
- * Subclasses must provide packet codec in `_packetCodec` property
8
- */
9
- export declare abstract class BaseTcpTransport extends EventEmitter implements ITelegramTransport {
10
- protected _currentDc: BasicDcOption | null;
11
- protected _state: TransportState;
12
- protected _socket: Socket | null;
13
- abstract _packetCodec: IPacketCodec;
14
- protected _crypto: ICryptoProvider;
15
- protected log: Logger;
16
- packetCodecInitialized: boolean;
17
- private _updateLogPrefix;
18
- setup(crypto: ICryptoProvider, log: Logger): void;
19
- state(): TransportState;
20
- currentDc(): BasicDcOption | null;
21
- connect(dc: BasicDcOption, testMode: boolean): void;
22
- close(): void;
23
- handleError(error: Error): void;
24
- handleConnect(): void;
25
- send(bytes: Uint8Array): Promise<void>;
26
- }
27
- export declare class TcpTransport extends BaseTcpTransport {
28
- _packetCodec: IntermediatePacketCodec;
1
+ import { ITcpConnection } from '@fuman/net';
2
+ import { TelegramTransport, IntermediatePacketCodec } from '@mtcute/core';
3
+ import { BasicDcOption } from '@mtcute/core/utils.js';
4
+ export declare class TcpTransport implements TelegramTransport {
5
+ connect(dc: BasicDcOption): Promise<ITcpConnection>;
6
+ packetCodec(): IntermediatePacketCodec;
29
7
  }
package/utils/tcp.js ADDED
@@ -0,0 +1,13 @@
1
+ import { connectTcp } from "@fuman/node";
2
+ import { IntermediatePacketCodec } from "@mtcute/core";
3
+ class TcpTransport {
4
+ connect(dc) {
5
+ return connectTcp({ address: dc.ipAddress, port: dc.port });
6
+ }
7
+ packetCodec() {
8
+ return new IntermediatePacketCodec();
9
+ }
10
+ }
11
+ export {
12
+ TcpTransport
13
+ };
package/utils.cjs CHANGED
@@ -1,17 +1,14 @@
1
1
  if (typeof globalThis !== "undefined" && !globalThis._MTCUTE_CJS_DEPRECATION_WARNED) {
2
2
  globalThis._MTCUTE_CJS_DEPRECATION_WARNED = true;
3
- console.warn("[@mtcute/node] CommonJS support is deprecated and will be removed soon. Please consider switching to ESM, it's " + (/* @__PURE__ */ new Date()).getFullYear() + " already.");
4
- console.warn("[@mtcute/node] Learn more about switching to ESM: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c");
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 crypto = require("./chunks/cjs/D7i-4e0W.js");
9
- const streamUtils = require("./chunks/cjs/HP2yqAk_.js");
8
+ const crypto = require("./utils/crypto.cjs");
10
9
  const utils_js = require("@mtcute/core/utils.js");
11
10
  exports.BaseNodeCryptoProvider = crypto.BaseNodeCryptoProvider;
12
11
  exports.NodeCryptoProvider = crypto.NodeCryptoProvider;
13
- exports.nodeStreamToWeb = streamUtils.nodeStreamToWeb;
14
- exports.webStreamToNode = streamUtils.webStreamToNode;
15
12
  Object.keys(utils_js).forEach((k) => {
16
13
  if (k !== "default" && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
17
14
  enumerable: true,
package/utils.d.ts CHANGED
@@ -1,3 +1,2 @@
1
1
  export * from './utils/crypto.js';
2
- export * from './utils/stream-utils.js';
3
2
  export * from '@mtcute/core/utils.js';
package/utils.js CHANGED
@@ -1,9 +1,6 @@
1
- import { BaseNodeCryptoProvider, NodeCryptoProvider } from "./chunks/es/HZgHrOPU.js";
2
- import { nodeStreamToWeb, webStreamToNode } from "./chunks/es/CKso6cAV.js";
1
+ import { BaseNodeCryptoProvider, NodeCryptoProvider } from "./utils/crypto.js";
3
2
  export * from "@mtcute/core/utils.js";
4
3
  export {
5
4
  BaseNodeCryptoProvider,
6
- NodeCryptoProvider,
7
- nodeStreamToWeb,
8
- webStreamToNode
5
+ NodeCryptoProvider
9
6
  };
package/worker.cjs ADDED
@@ -0,0 +1,49 @@
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_worker_threads = require("node:worker_threads");
9
+ const worker_js = require("@mtcute/core/worker.js");
10
+ const platform = require("./utils/platform.cjs");
11
+ let _registered = false;
12
+ class TelegramWorker extends worker_js.TelegramWorker {
13
+ registerWorker(handler) {
14
+ if (!node_worker_threads.parentPort) {
15
+ throw new Error("TelegramWorker must be created from a worker thread");
16
+ }
17
+ if (_registered) {
18
+ throw new Error("TelegramWorker must be created only once");
19
+ }
20
+ _registered = true;
21
+ const port = node_worker_threads.parentPort;
22
+ const respond = port.postMessage.bind(port);
23
+ node_worker_threads.parentPort.on("message", (message) => handler(message, respond));
24
+ return respond;
25
+ }
26
+ }
27
+ class TelegramWorkerPort extends worker_js.TelegramWorkerPort {
28
+ constructor(options) {
29
+ super({
30
+ worker: options.worker,
31
+ platform: new platform.NodePlatform()
32
+ });
33
+ }
34
+ connectToWorker(worker, handler) {
35
+ if (!(worker instanceof node_worker_threads.Worker)) {
36
+ throw new TypeError("Only worker_threads are supported");
37
+ }
38
+ const send = worker.postMessage.bind(worker);
39
+ worker.on("message", handler);
40
+ return [
41
+ send,
42
+ () => {
43
+ worker.off("message", handler);
44
+ }
45
+ ];
46
+ }
47
+ }
48
+ exports.TelegramWorker = TelegramWorker;
49
+ exports.TelegramWorkerPort = TelegramWorkerPort;