@mtcute/web 0.17.0 → 0.18.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/client.cjs +39 -0
- package/client.d.cts +23 -0
- package/client.d.ts +1 -8
- package/client.js +34 -0
- package/common-internals-web/logging.cjs +48 -0
- package/common-internals-web/logging.d.cts +2 -0
- package/common-internals-web/logging.js +43 -0
- package/crypto.cjs +84 -0
- package/crypto.d.cts +21 -0
- package/crypto.js +79 -0
- package/exit-hook.cjs +28 -0
- package/exit-hook.d.cts +1 -0
- package/exit-hook.js +23 -0
- package/idb/driver.cjs +123 -0
- package/idb/driver.d.cts +18 -0
- package/idb/driver.js +118 -0
- package/idb/index.cjs +29 -0
- package/idb/index.d.cts +22 -0
- package/idb/index.js +24 -0
- package/idb/repository/auth-keys.cjs +70 -0
- package/idb/repository/auth-keys.d.cts +14 -0
- package/idb/repository/auth-keys.js +65 -0
- package/idb/repository/kv.cjs +36 -0
- package/idb/repository/kv.d.cts +11 -0
- package/idb/repository/kv.js +31 -0
- package/idb/repository/peers.cjs +47 -0
- package/idb/repository/peers.d.cts +12 -0
- package/idb/repository/peers.js +42 -0
- package/idb/repository/ref-messages.cjs +62 -0
- package/idb/repository/ref-messages.d.cts +12 -0
- package/idb/repository/ref-messages.js +57 -0
- package/idb/utils.cjs +21 -0
- package/idb/utils.d.cts +2 -0
- package/idb/utils.js +16 -0
- package/index.cjs +18 -953
- package/index.js +7 -943
- package/methods.cjs +2 -2
- package/package.json +12 -11
- package/platform.cjs +41 -0
- package/platform.d.cts +11 -0
- package/platform.d.ts +1 -11
- package/platform.js +36 -0
- package/utils.cjs +2 -2
- package/wasm.cjs +38 -0
- package/wasm.d.cts +2 -0
- package/wasm.js +33 -0
- package/websocket.cjs +50 -0
- package/websocket.d.cts +18 -0
- package/websocket.d.ts +6 -34
- package/websocket.js +45 -0
- package/worker.cjs +116 -0
- package/worker.d.cts +12 -0
- package/worker.d.ts +5 -3
- package/worker.js +111 -0
- package/common-internals-web/base64.d.ts +0 -2
- package/common-internals-web/hex.d.ts +0 -2
- package/common-internals-web/utf8.d.ts +0 -3
- /package/{websocket.test.d.ts → idb/idb.test.d.cts} +0 -0
package/methods.cjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
if (typeof globalThis !== "undefined" && !globalThis._MTCUTE_CJS_DEPRECATION_WARNED) {
|
|
2
2
|
globalThis._MTCUTE_CJS_DEPRECATION_WARNED = true;
|
|
3
|
-
console.warn("[
|
|
4
|
-
console.warn("[
|
|
3
|
+
console.warn("[mtcute-workspace] CommonJS support is deprecated and will be removed in 0.20.0. Please consider switching to ESM, it's " + (/* @__PURE__ */ new Date()).getFullYear() + " already.");
|
|
4
|
+
console.warn("[mtcute-workspace] Learn more about switching to ESM: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c");
|
|
5
5
|
}
|
|
6
6
|
"use strict";
|
|
7
7
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
package/package.json
CHANGED
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mtcute/web",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.18.0",
|
|
5
5
|
"description": "Meta-package for the web platform",
|
|
6
|
-
"author": "alina sireneva <alina@tei.su>",
|
|
7
6
|
"license": "MIT",
|
|
8
|
-
"
|
|
7
|
+
"dependencies": {
|
|
8
|
+
"@mtcute/core": "^0.18.0",
|
|
9
|
+
"@mtcute/wasm": "^0.18.0",
|
|
10
|
+
"@fuman/net": "0.0.4",
|
|
11
|
+
"events": "3.2.0"
|
|
12
|
+
},
|
|
9
13
|
"exports": {
|
|
10
14
|
".": {
|
|
11
15
|
"import": {
|
|
@@ -38,12 +42,8 @@
|
|
|
38
42
|
}
|
|
39
43
|
}
|
|
40
44
|
},
|
|
41
|
-
"
|
|
42
|
-
"
|
|
43
|
-
"@mtcute/core": "^0.17.0",
|
|
44
|
-
"@mtcute/wasm": "^0.17.0",
|
|
45
|
-
"events": "3.2.0"
|
|
46
|
-
},
|
|
45
|
+
"author": "alina sireneva <alina@tei.su>",
|
|
46
|
+
"sideEffects": false,
|
|
47
47
|
"denoJson": {
|
|
48
48
|
"compilerOptions": {
|
|
49
49
|
"lib": [
|
|
@@ -56,6 +56,7 @@
|
|
|
56
56
|
"homepage": "https://mtcute.dev",
|
|
57
57
|
"repository": {
|
|
58
58
|
"type": "git",
|
|
59
|
-
"url": "https://github.com/mtcute/mtcute"
|
|
60
|
-
}
|
|
59
|
+
"url": "git+https://github.com/mtcute/mtcute.git"
|
|
60
|
+
},
|
|
61
|
+
"scripts": {}
|
|
61
62
|
}
|
package/platform.cjs
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
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 logging = require("./common-internals-web/logging.cjs");
|
|
9
|
+
const exitHook = require("./exit-hook.cjs");
|
|
10
|
+
class WebPlatform {
|
|
11
|
+
getDeviceModel() {
|
|
12
|
+
if (typeof navigator === "undefined") return "Browser";
|
|
13
|
+
return navigator.userAgent;
|
|
14
|
+
}
|
|
15
|
+
getDefaultLogLevel() {
|
|
16
|
+
if (typeof localStorage !== "undefined") {
|
|
17
|
+
const localLogLevel = Number.parseInt(localStorage.MTCUTE_LOG_LEVEL);
|
|
18
|
+
if (!Number.isNaN(localLogLevel)) {
|
|
19
|
+
return localLogLevel;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
24
|
+
onNetworkChanged(fn) {
|
|
25
|
+
if (!("onLine" in navigator)) return () => {
|
|
26
|
+
};
|
|
27
|
+
const onlineHandler = () => fn(navigator.onLine);
|
|
28
|
+
window.addEventListener("online", onlineHandler);
|
|
29
|
+
window.addEventListener("offline", onlineHandler);
|
|
30
|
+
return () => {
|
|
31
|
+
window.removeEventListener("online", onlineHandler);
|
|
32
|
+
window.removeEventListener("offline", onlineHandler);
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
isOnline() {
|
|
36
|
+
return navigator.onLine;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
WebPlatform.prototype.log = logging.defaultLoggingHandler;
|
|
40
|
+
WebPlatform.prototype.beforeExit = exitHook.beforeExit;
|
|
41
|
+
exports.WebPlatform = WebPlatform;
|
package/platform.d.cts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { ICorePlatform } from '@mtcute/core';
|
|
2
|
+
import { defaultLoggingHandler } from './common-internals-web/logging.js';
|
|
3
|
+
import { beforeExit } from './exit-hook.js';
|
|
4
|
+
export declare class WebPlatform implements ICorePlatform {
|
|
5
|
+
log: typeof defaultLoggingHandler;
|
|
6
|
+
beforeExit: typeof beforeExit;
|
|
7
|
+
getDeviceModel(): string;
|
|
8
|
+
getDefaultLogLevel(): number | null;
|
|
9
|
+
onNetworkChanged(fn: (connected: boolean) => void): () => void;
|
|
10
|
+
isOnline(): boolean;
|
|
11
|
+
}
|
package/platform.d.ts
CHANGED
|
@@ -1,8 +1,5 @@
|
|
|
1
|
-
import { ICorePlatform } from '@mtcute/core
|
|
2
|
-
import { base64Decode, base64Encode } from './common-internals-web/base64.js';
|
|
3
|
-
import { hexDecode, hexEncode } from './common-internals-web/hex.js';
|
|
1
|
+
import { ICorePlatform } from '@mtcute/core';
|
|
4
2
|
import { defaultLoggingHandler } from './common-internals-web/logging.js';
|
|
5
|
-
import { utf8ByteLength, utf8Decode, utf8Encode } from './common-internals-web/utf8.js';
|
|
6
3
|
import { beforeExit } from './exit-hook.js';
|
|
7
4
|
export declare class WebPlatform implements ICorePlatform {
|
|
8
5
|
log: typeof defaultLoggingHandler;
|
|
@@ -11,11 +8,4 @@ export declare class WebPlatform implements ICorePlatform {
|
|
|
11
8
|
getDefaultLogLevel(): number | null;
|
|
12
9
|
onNetworkChanged(fn: (connected: boolean) => void): () => void;
|
|
13
10
|
isOnline(): boolean;
|
|
14
|
-
utf8ByteLength: typeof utf8ByteLength;
|
|
15
|
-
utf8Encode: typeof utf8Encode;
|
|
16
|
-
utf8Decode: typeof utf8Decode;
|
|
17
|
-
hexEncode: typeof hexEncode;
|
|
18
|
-
hexDecode: typeof hexDecode;
|
|
19
|
-
base64Encode: typeof base64Encode;
|
|
20
|
-
base64Decode: typeof base64Decode;
|
|
21
11
|
}
|
package/platform.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { defaultLoggingHandler } from "./common-internals-web/logging.js";
|
|
2
|
+
import { beforeExit } from "./exit-hook.js";
|
|
3
|
+
class WebPlatform {
|
|
4
|
+
getDeviceModel() {
|
|
5
|
+
if (typeof navigator === "undefined") return "Browser";
|
|
6
|
+
return navigator.userAgent;
|
|
7
|
+
}
|
|
8
|
+
getDefaultLogLevel() {
|
|
9
|
+
if (typeof localStorage !== "undefined") {
|
|
10
|
+
const localLogLevel = Number.parseInt(localStorage.MTCUTE_LOG_LEVEL);
|
|
11
|
+
if (!Number.isNaN(localLogLevel)) {
|
|
12
|
+
return localLogLevel;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
onNetworkChanged(fn) {
|
|
18
|
+
if (!("onLine" in navigator)) return () => {
|
|
19
|
+
};
|
|
20
|
+
const onlineHandler = () => fn(navigator.onLine);
|
|
21
|
+
window.addEventListener("online", onlineHandler);
|
|
22
|
+
window.addEventListener("offline", onlineHandler);
|
|
23
|
+
return () => {
|
|
24
|
+
window.removeEventListener("online", onlineHandler);
|
|
25
|
+
window.removeEventListener("offline", onlineHandler);
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
isOnline() {
|
|
29
|
+
return navigator.onLine;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
WebPlatform.prototype.log = defaultLoggingHandler;
|
|
33
|
+
WebPlatform.prototype.beforeExit = beforeExit;
|
|
34
|
+
export {
|
|
35
|
+
WebPlatform
|
|
36
|
+
};
|
package/utils.cjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
if (typeof globalThis !== "undefined" && !globalThis._MTCUTE_CJS_DEPRECATION_WARNED) {
|
|
2
2
|
globalThis._MTCUTE_CJS_DEPRECATION_WARNED = true;
|
|
3
|
-
console.warn("[
|
|
4
|
-
console.warn("[
|
|
3
|
+
console.warn("[mtcute-workspace] CommonJS support is deprecated and will be removed in 0.20.0. Please consider switching to ESM, it's " + (/* @__PURE__ */ new Date()).getFullYear() + " already.");
|
|
4
|
+
console.warn("[mtcute-workspace] Learn more about switching to ESM: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c");
|
|
5
5
|
}
|
|
6
6
|
"use strict";
|
|
7
7
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
package/wasm.cjs
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
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 wasm = require("@mtcute/wasm");
|
|
9
|
+
async function loadWasmBinary(input) {
|
|
10
|
+
if (typeof input === "undefined") {
|
|
11
|
+
input = wasm.getWasmUrl();
|
|
12
|
+
}
|
|
13
|
+
if (typeof input === "string" || typeof Request === "function" && input instanceof Request || typeof URL === "function" && input instanceof URL) {
|
|
14
|
+
input = await fetch(input);
|
|
15
|
+
}
|
|
16
|
+
if (typeof Response === "function" && input instanceof Response) {
|
|
17
|
+
if (typeof WebAssembly.instantiateStreaming === "function") {
|
|
18
|
+
try {
|
|
19
|
+
const { instance: instance2 } = await WebAssembly.instantiateStreaming(input);
|
|
20
|
+
return instance2;
|
|
21
|
+
} catch (e) {
|
|
22
|
+
if (input.headers.get("Content-Type") !== "application/wasm") {
|
|
23
|
+
console.warn(
|
|
24
|
+
"`WebAssembly.instantiateStreaming` failed because your server does not serve wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n",
|
|
25
|
+
e
|
|
26
|
+
);
|
|
27
|
+
} else {
|
|
28
|
+
throw e;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
const bytes = await input.arrayBuffer();
|
|
33
|
+
const { instance } = await WebAssembly.instantiate(bytes);
|
|
34
|
+
return instance;
|
|
35
|
+
}
|
|
36
|
+
return WebAssembly.instantiate(input);
|
|
37
|
+
}
|
|
38
|
+
exports.loadWasmBinary = loadWasmBinary;
|
package/wasm.d.cts
ADDED
package/wasm.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { getWasmUrl } from "@mtcute/wasm";
|
|
2
|
+
async function loadWasmBinary(input) {
|
|
3
|
+
if (typeof input === "undefined") {
|
|
4
|
+
input = getWasmUrl();
|
|
5
|
+
}
|
|
6
|
+
if (typeof input === "string" || typeof Request === "function" && input instanceof Request || typeof URL === "function" && input instanceof URL) {
|
|
7
|
+
input = await fetch(input);
|
|
8
|
+
}
|
|
9
|
+
if (typeof Response === "function" && input instanceof Response) {
|
|
10
|
+
if (typeof WebAssembly.instantiateStreaming === "function") {
|
|
11
|
+
try {
|
|
12
|
+
const { instance: instance2 } = await WebAssembly.instantiateStreaming(input);
|
|
13
|
+
return instance2;
|
|
14
|
+
} catch (e) {
|
|
15
|
+
if (input.headers.get("Content-Type") !== "application/wasm") {
|
|
16
|
+
console.warn(
|
|
17
|
+
"`WebAssembly.instantiateStreaming` failed because your server does not serve wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n",
|
|
18
|
+
e
|
|
19
|
+
);
|
|
20
|
+
} else {
|
|
21
|
+
throw e;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
const bytes = await input.arrayBuffer();
|
|
26
|
+
const { instance } = await WebAssembly.instantiate(bytes);
|
|
27
|
+
return instance;
|
|
28
|
+
}
|
|
29
|
+
return WebAssembly.instantiate(input);
|
|
30
|
+
}
|
|
31
|
+
export {
|
|
32
|
+
loadWasmBinary
|
|
33
|
+
};
|
package/websocket.cjs
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
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 core = require("@mtcute/core");
|
|
10
|
+
const subdomainsMap = {
|
|
11
|
+
1: "pluto",
|
|
12
|
+
2: "venus",
|
|
13
|
+
3: "aurora",
|
|
14
|
+
4: "vesta",
|
|
15
|
+
5: "flora"
|
|
16
|
+
};
|
|
17
|
+
class WebSocketTransport {
|
|
18
|
+
_baseDomain;
|
|
19
|
+
_subdomains;
|
|
20
|
+
_WebSocket;
|
|
21
|
+
constructor({
|
|
22
|
+
ws = WebSocket,
|
|
23
|
+
baseDomain = "web.telegram.org",
|
|
24
|
+
subdomains = subdomainsMap
|
|
25
|
+
} = {}) {
|
|
26
|
+
if (!ws) {
|
|
27
|
+
throw new core.MtUnsupportedError(
|
|
28
|
+
"To use WebSocket transport with NodeJS, install `ws` package and pass it to constructor"
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
if ("default" in ws) {
|
|
32
|
+
ws = ws.default;
|
|
33
|
+
}
|
|
34
|
+
this._baseDomain = baseDomain;
|
|
35
|
+
this._subdomains = subdomains;
|
|
36
|
+
this._WebSocket = ws;
|
|
37
|
+
}
|
|
38
|
+
async connect(dc) {
|
|
39
|
+
const url = `wss://${this._subdomains[dc.id]}.${this._baseDomain}/apiws${dc.testMode ? "_test" : ""}`;
|
|
40
|
+
return net.connectWs({
|
|
41
|
+
url,
|
|
42
|
+
implementation: this._WebSocket,
|
|
43
|
+
protocols: "binary"
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
packetCodec() {
|
|
47
|
+
return new core.ObfuscatedPacketCodec(new core.IntermediatePacketCodec());
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
exports.WebSocketTransport = WebSocketTransport;
|
package/websocket.d.cts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { WebSocketConstructor } from '@fuman/net';
|
|
2
|
+
import { IPacketCodec, ITelegramConnection, TelegramTransport } from '@mtcute/core';
|
|
3
|
+
import { BasicDcOption } from './utils.js';
|
|
4
|
+
export declare class WebSocketTransport implements TelegramTransport {
|
|
5
|
+
private _baseDomain;
|
|
6
|
+
private _subdomains;
|
|
7
|
+
private _WebSocket;
|
|
8
|
+
constructor({ ws, baseDomain, subdomains, }?: {
|
|
9
|
+
/** Custom implementation of WebSocket (e.g. https://npm.im/ws) */
|
|
10
|
+
ws?: WebSocketConstructor;
|
|
11
|
+
/** Base WebSocket domain */
|
|
12
|
+
baseDomain?: string;
|
|
13
|
+
/** Map of sub-domains (key is DC ID, value is string) */
|
|
14
|
+
subdomains?: Record<string, string>;
|
|
15
|
+
});
|
|
16
|
+
connect(dc: BasicDcOption): Promise<ITelegramConnection>;
|
|
17
|
+
packetCodec(): IPacketCodec;
|
|
18
|
+
}
|
package/websocket.d.ts
CHANGED
|
@@ -1,21 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { IPacketCodec,
|
|
3
|
-
import { BasicDcOption
|
|
4
|
-
export
|
|
5
|
-
new (address: string, protocol?: string): WebSocket;
|
|
6
|
-
}
|
|
7
|
-
/**
|
|
8
|
-
* Base for WebSocket transports.
|
|
9
|
-
* Subclasses must provide packet codec in `_packetCodec` property
|
|
10
|
-
*/
|
|
11
|
-
export declare abstract class BaseWebSocketTransport extends EventEmitter implements ITelegramTransport {
|
|
12
|
-
private _currentDc;
|
|
13
|
-
private _state;
|
|
14
|
-
private _socket;
|
|
15
|
-
private _crypto;
|
|
16
|
-
protected log: Logger;
|
|
17
|
-
abstract _packetCodec: IPacketCodec;
|
|
18
|
-
packetCodecInitialized: boolean;
|
|
1
|
+
import { WebSocketConstructor } from '@fuman/net';
|
|
2
|
+
import { IPacketCodec, ITelegramConnection, TelegramTransport } from '@mtcute/core';
|
|
3
|
+
import { BasicDcOption } from './utils.js';
|
|
4
|
+
export declare class WebSocketTransport implements TelegramTransport {
|
|
19
5
|
private _baseDomain;
|
|
20
6
|
private _subdomains;
|
|
21
7
|
private _WebSocket;
|
|
@@ -27,20 +13,6 @@ export declare abstract class BaseWebSocketTransport extends EventEmitter implem
|
|
|
27
13
|
/** Map of sub-domains (key is DC ID, value is string) */
|
|
28
14
|
subdomains?: Record<string, string>;
|
|
29
15
|
});
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
state(): TransportState;
|
|
33
|
-
currentDc(): BasicDcOption | null;
|
|
34
|
-
connect(dc: BasicDcOption, testMode: boolean): void;
|
|
35
|
-
private _closeWaiters;
|
|
36
|
-
close(): Promise<void>;
|
|
37
|
-
handleClosed(): void;
|
|
38
|
-
handleError(event: Event | {
|
|
39
|
-
error: Error;
|
|
40
|
-
}): void;
|
|
41
|
-
handleConnect(): void;
|
|
42
|
-
send(bytes: Uint8Array): Promise<void>;
|
|
43
|
-
}
|
|
44
|
-
export declare class WebSocketTransport extends BaseWebSocketTransport {
|
|
45
|
-
_packetCodec: ObfuscatedPacketCodec;
|
|
16
|
+
connect(dc: BasicDcOption): Promise<ITelegramConnection>;
|
|
17
|
+
packetCodec(): IPacketCodec;
|
|
46
18
|
}
|
package/websocket.js
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { connectWs } from "@fuman/net";
|
|
2
|
+
import { MtUnsupportedError, ObfuscatedPacketCodec, IntermediatePacketCodec } from "@mtcute/core";
|
|
3
|
+
const subdomainsMap = {
|
|
4
|
+
1: "pluto",
|
|
5
|
+
2: "venus",
|
|
6
|
+
3: "aurora",
|
|
7
|
+
4: "vesta",
|
|
8
|
+
5: "flora"
|
|
9
|
+
};
|
|
10
|
+
class WebSocketTransport {
|
|
11
|
+
_baseDomain;
|
|
12
|
+
_subdomains;
|
|
13
|
+
_WebSocket;
|
|
14
|
+
constructor({
|
|
15
|
+
ws = WebSocket,
|
|
16
|
+
baseDomain = "web.telegram.org",
|
|
17
|
+
subdomains = subdomainsMap
|
|
18
|
+
} = {}) {
|
|
19
|
+
if (!ws) {
|
|
20
|
+
throw new MtUnsupportedError(
|
|
21
|
+
"To use WebSocket transport with NodeJS, install `ws` package and pass it to constructor"
|
|
22
|
+
);
|
|
23
|
+
}
|
|
24
|
+
if ("default" in ws) {
|
|
25
|
+
ws = ws.default;
|
|
26
|
+
}
|
|
27
|
+
this._baseDomain = baseDomain;
|
|
28
|
+
this._subdomains = subdomains;
|
|
29
|
+
this._WebSocket = ws;
|
|
30
|
+
}
|
|
31
|
+
async connect(dc) {
|
|
32
|
+
const url = `wss://${this._subdomains[dc.id]}.${this._baseDomain}/apiws${dc.testMode ? "_test" : ""}`;
|
|
33
|
+
return connectWs({
|
|
34
|
+
url,
|
|
35
|
+
implementation: this._WebSocket,
|
|
36
|
+
protocols: "binary"
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
packetCodec() {
|
|
40
|
+
return new ObfuscatedPacketCodec(new IntermediatePacketCodec());
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
export {
|
|
44
|
+
WebSocketTransport
|
|
45
|
+
};
|
package/worker.cjs
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
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 worker_js = require("@mtcute/core/worker.js");
|
|
9
|
+
const platform$1 = require("./platform.cjs");
|
|
10
|
+
let _registered = false;
|
|
11
|
+
class TelegramWorker extends worker_js.TelegramWorker {
|
|
12
|
+
registerWorker(handler) {
|
|
13
|
+
if (_registered) {
|
|
14
|
+
throw new Error("TelegramWorker must be created only once");
|
|
15
|
+
}
|
|
16
|
+
_registered = true;
|
|
17
|
+
if (typeof SharedWorkerGlobalScope !== "undefined" && self instanceof SharedWorkerGlobalScope) {
|
|
18
|
+
const connections = [];
|
|
19
|
+
const broadcast = (message) => {
|
|
20
|
+
for (const port of connections) {
|
|
21
|
+
port.postMessage(message);
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
self.onconnect = (event) => {
|
|
25
|
+
const port = event.ports[0];
|
|
26
|
+
connections.push(port);
|
|
27
|
+
const respond = port.postMessage.bind(port);
|
|
28
|
+
const onClose = () => {
|
|
29
|
+
port.close();
|
|
30
|
+
const idx = connections.indexOf(port);
|
|
31
|
+
if (idx >= 0) {
|
|
32
|
+
connections.splice(connections.indexOf(port), 1);
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
const onTimeout = () => {
|
|
36
|
+
console.warn("some connection timed out!");
|
|
37
|
+
respond({ __type__: "timeout" });
|
|
38
|
+
onClose();
|
|
39
|
+
};
|
|
40
|
+
let timeout = setTimeout(onTimeout, 6e4);
|
|
41
|
+
port.addEventListener("message", (message) => {
|
|
42
|
+
if (message.data.__type__ === "close") {
|
|
43
|
+
onClose();
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
if (message.data.__type__ === "ping") {
|
|
47
|
+
clearTimeout(timeout);
|
|
48
|
+
timeout = setTimeout(onTimeout, 6e4);
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
handler(message.data, respond);
|
|
52
|
+
});
|
|
53
|
+
port.start();
|
|
54
|
+
};
|
|
55
|
+
return broadcast;
|
|
56
|
+
}
|
|
57
|
+
if (typeof WorkerGlobalScope !== "undefined" && self instanceof WorkerGlobalScope) {
|
|
58
|
+
const respond = self.postMessage.bind(self);
|
|
59
|
+
self.addEventListener("message", (message) => handler(message.data, respond));
|
|
60
|
+
return respond;
|
|
61
|
+
}
|
|
62
|
+
throw new Error("TelegramWorker must be created from a worker");
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
const platform = /* @__PURE__ */ new platform$1.WebPlatform();
|
|
66
|
+
class TelegramWorkerPort extends worker_js.TelegramWorkerPort {
|
|
67
|
+
constructor(options) {
|
|
68
|
+
super({
|
|
69
|
+
worker: options.worker,
|
|
70
|
+
platform
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
connectToWorker(worker, handler) {
|
|
74
|
+
if (worker instanceof Worker) {
|
|
75
|
+
const send = worker.postMessage.bind(worker);
|
|
76
|
+
const messageHandler = (ev) => {
|
|
77
|
+
handler(ev.data);
|
|
78
|
+
};
|
|
79
|
+
worker.addEventListener("message", messageHandler);
|
|
80
|
+
return [
|
|
81
|
+
send,
|
|
82
|
+
() => {
|
|
83
|
+
worker.removeEventListener("message", messageHandler);
|
|
84
|
+
}
|
|
85
|
+
];
|
|
86
|
+
}
|
|
87
|
+
if (worker instanceof SharedWorker) {
|
|
88
|
+
const send = worker.port.postMessage.bind(worker.port);
|
|
89
|
+
const pingInterval = setInterval(() => {
|
|
90
|
+
worker.port.postMessage({ __type__: "ping" });
|
|
91
|
+
}, 1e4);
|
|
92
|
+
const messageHandler = (ev) => {
|
|
93
|
+
if (ev.data.__type__ === "timeout") {
|
|
94
|
+
location.reload();
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
handler(ev.data);
|
|
98
|
+
};
|
|
99
|
+
worker.port.addEventListener("message", messageHandler);
|
|
100
|
+
worker.port.start();
|
|
101
|
+
let cancelBeforeExit;
|
|
102
|
+
const close = () => {
|
|
103
|
+
clearInterval(pingInterval);
|
|
104
|
+
worker.port.postMessage({ __type__: "close" });
|
|
105
|
+
worker.port.removeEventListener("message", messageHandler);
|
|
106
|
+
worker.port.close();
|
|
107
|
+
cancelBeforeExit();
|
|
108
|
+
};
|
|
109
|
+
cancelBeforeExit = platform.beforeExit(close);
|
|
110
|
+
return [send, close];
|
|
111
|
+
}
|
|
112
|
+
throw new Error("Only workers and shared workers are supported");
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
exports.TelegramWorker = TelegramWorker;
|
|
116
|
+
exports.TelegramWorkerPort = TelegramWorkerPort;
|
package/worker.d.cts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { ClientMessageHandler, RespondFn, SendFn, SomeWorker, TelegramWorkerOptions, WorkerCustomMethods, WorkerMessageHandler, TelegramWorker as TelegramWorkerBase, TelegramWorkerPort as TelegramWorkerPortBase } from '@mtcute/core/worker.js';
|
|
2
|
+
export type { TelegramWorkerOptions, WorkerCustomMethods };
|
|
3
|
+
export interface TelegramWorkerPortOptions {
|
|
4
|
+
worker: SomeWorker;
|
|
5
|
+
}
|
|
6
|
+
export declare class TelegramWorker<T extends WorkerCustomMethods> extends TelegramWorkerBase<T> {
|
|
7
|
+
registerWorker(handler: WorkerMessageHandler): RespondFn;
|
|
8
|
+
}
|
|
9
|
+
export declare class TelegramWorkerPort<T extends WorkerCustomMethods> extends TelegramWorkerPortBase<T> {
|
|
10
|
+
constructor(options: TelegramWorkerPortOptions);
|
|
11
|
+
connectToWorker(worker: SomeWorker, handler: ClientMessageHandler): [SendFn, () => void];
|
|
12
|
+
}
|
package/worker.d.ts
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
|
-
import { ClientMessageHandler, RespondFn, SendFn, SomeWorker, TelegramWorkerOptions,
|
|
2
|
-
export type { TelegramWorkerOptions,
|
|
1
|
+
import { ClientMessageHandler, RespondFn, SendFn, SomeWorker, TelegramWorkerOptions, WorkerCustomMethods, WorkerMessageHandler, TelegramWorker as TelegramWorkerBase, TelegramWorkerPort as TelegramWorkerPortBase } from '@mtcute/core/worker.js';
|
|
2
|
+
export type { TelegramWorkerOptions, WorkerCustomMethods };
|
|
3
|
+
export interface TelegramWorkerPortOptions {
|
|
4
|
+
worker: SomeWorker;
|
|
5
|
+
}
|
|
3
6
|
export declare class TelegramWorker<T extends WorkerCustomMethods> extends TelegramWorkerBase<T> {
|
|
4
7
|
registerWorker(handler: WorkerMessageHandler): RespondFn;
|
|
5
8
|
}
|
|
6
9
|
export declare class TelegramWorkerPort<T extends WorkerCustomMethods> extends TelegramWorkerPortBase<T> {
|
|
7
|
-
readonly options: TelegramWorkerPortOptions;
|
|
8
10
|
constructor(options: TelegramWorkerPortOptions);
|
|
9
11
|
connectToWorker(worker: SomeWorker, handler: ClientMessageHandler): [SendFn, () => void];
|
|
10
12
|
}
|