@mtcute/web 0.16.7 → 0.16.13
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/{cjs/client.d.ts → client.d.ts} +2 -3
- package/{cjs/crypto.d.ts → crypto.d.ts} +2 -3
- package/idb/idb.test.d.ts +1 -0
- package/{esm/idb → idb}/index.d.ts +1 -1
- package/{esm/idb → idb}/repository/auth-keys.d.ts +2 -2
- package/{cjs/idb → idb}/repository/kv.d.ts +2 -2
- package/{esm/idb → idb}/repository/peers.d.ts +3 -3
- package/{cjs/idb → idb}/repository/ref-messages.d.ts +2 -2
- package/index.cjs +965 -0
- package/index.d.ts +7 -1
- package/index.js +955 -1
- package/methods.cjs +14 -0
- package/methods.d.ts +1 -1
- package/methods.js +1 -1
- package/package.json +56 -40
- package/{cjs/platform.d.ts → platform.d.ts} +1 -1
- package/utils.cjs +14 -0
- package/utils.d.ts +1 -1
- package/utils.js +1 -1
- package/{esm/websocket.d.ts → websocket.d.ts} +3 -4
- package/websocket.test.d.ts +1 -0
- package/{cjs/worker.d.ts → worker.d.ts} +1 -2
- package/cjs/client.js +0 -43
- package/cjs/client.js.map +0 -1
- package/cjs/common-internals-web/base64.js +0 -111
- package/cjs/common-internals-web/base64.js.map +0 -1
- package/cjs/common-internals-web/hex.js +0 -66
- package/cjs/common-internals-web/hex.js.map +0 -1
- package/cjs/common-internals-web/logging.js +0 -35
- package/cjs/common-internals-web/logging.js.map +0 -1
- package/cjs/common-internals-web/utf8.js +0 -28
- package/cjs/common-internals-web/utf8.js.map +0 -1
- package/cjs/crypto.js +0 -73
- package/cjs/crypto.js.map +0 -1
- package/cjs/exit-hook.js +0 -23
- package/cjs/exit-hook.js.map +0 -1
- package/cjs/idb/driver.js +0 -139
- package/cjs/idb/driver.js.map +0 -1
- package/cjs/idb/index.d.ts +0 -22
- package/cjs/idb/index.js +0 -34
- package/cjs/idb/index.js.map +0 -1
- package/cjs/idb/repository/auth-keys.d.ts +0 -14
- package/cjs/idb/repository/auth-keys.js +0 -71
- package/cjs/idb/repository/auth-keys.js.map +0 -1
- package/cjs/idb/repository/kv.js +0 -35
- package/cjs/idb/repository/kv.js.map +0 -1
- package/cjs/idb/repository/peers.d.ts +0 -12
- package/cjs/idb/repository/peers.js +0 -39
- package/cjs/idb/repository/peers.js.map +0 -1
- package/cjs/idb/repository/ref-messages.js +0 -62
- package/cjs/idb/repository/ref-messages.js.map +0 -1
- package/cjs/idb/utils.js +0 -17
- package/cjs/idb/utils.js.map +0 -1
- package/cjs/index.js +0 -30
- package/cjs/index.js.map +0 -1
- package/cjs/methods.js +0 -24
- package/cjs/methods.js.map +0 -1
- package/cjs/package.json +0 -3
- package/cjs/platform.js +0 -49
- package/cjs/platform.js.map +0 -1
- package/cjs/utils.js +0 -24
- package/cjs/utils.js.map +0 -1
- package/cjs/wasm.js +0 -35
- package/cjs/wasm.js.map +0 -1
- package/cjs/websocket.d.ts +0 -47
- package/cjs/websocket.js +0 -132
- package/cjs/websocket.js.map +0 -1
- package/cjs/worker.js +0 -128
- package/cjs/worker.js.map +0 -1
- package/esm/client.d.ts +0 -31
- package/esm/client.js +0 -38
- package/esm/client.js.map +0 -1
- package/esm/common-internals-web/base64.d.ts +0 -2
- package/esm/common-internals-web/base64.js +0 -107
- package/esm/common-internals-web/base64.js.map +0 -1
- package/esm/common-internals-web/hex.d.ts +0 -2
- package/esm/common-internals-web/hex.js +0 -62
- package/esm/common-internals-web/hex.js.map +0 -1
- package/esm/common-internals-web/logging.d.ts +0 -2
- package/esm/common-internals-web/logging.js +0 -31
- package/esm/common-internals-web/logging.js.map +0 -1
- package/esm/common-internals-web/utf8.d.ts +0 -3
- package/esm/common-internals-web/utf8.js +0 -23
- package/esm/common-internals-web/utf8.js.map +0 -1
- package/esm/crypto.d.ts +0 -22
- package/esm/crypto.js +0 -69
- package/esm/crypto.js.map +0 -1
- package/esm/exit-hook.d.ts +0 -1
- package/esm/exit-hook.js +0 -20
- package/esm/exit-hook.js.map +0 -1
- package/esm/idb/driver.d.ts +0 -18
- package/esm/idb/driver.js +0 -135
- package/esm/idb/driver.js.map +0 -1
- package/esm/idb/index.js +0 -29
- package/esm/idb/index.js.map +0 -1
- package/esm/idb/repository/auth-keys.js +0 -67
- package/esm/idb/repository/auth-keys.js.map +0 -1
- package/esm/idb/repository/kv.d.ts +0 -11
- package/esm/idb/repository/kv.js +0 -31
- package/esm/idb/repository/kv.js.map +0 -1
- package/esm/idb/repository/peers.js +0 -35
- package/esm/idb/repository/peers.js.map +0 -1
- package/esm/idb/repository/ref-messages.d.ts +0 -12
- package/esm/idb/repository/ref-messages.js +0 -58
- package/esm/idb/repository/ref-messages.js.map +0 -1
- package/esm/idb/utils.d.ts +0 -2
- package/esm/idb/utils.js +0 -13
- package/esm/idb/utils.js.map +0 -1
- package/esm/index.d.ts +0 -7
- package/esm/index.js +0 -8
- package/esm/index.js.map +0 -1
- package/esm/methods.d.ts +0 -1
- package/esm/methods.js +0 -2
- package/esm/methods.js.map +0 -1
- package/esm/platform.d.ts +0 -21
- package/esm/platform.js +0 -45
- package/esm/platform.js.map +0 -1
- package/esm/utils.d.ts +0 -1
- package/esm/utils.js +0 -2
- package/esm/utils.js.map +0 -1
- package/esm/wasm.d.ts +0 -2
- package/esm/wasm.js +0 -32
- package/esm/wasm.js.map +0 -1
- package/esm/websocket.js +0 -124
- package/esm/websocket.js.map +0 -1
- package/esm/worker.d.ts +0 -11
- package/esm/worker.js +0 -123
- package/esm/worker.js.map +0 -1
- /package/{cjs/common-internals-web → common-internals-web}/base64.d.ts +0 -0
- /package/{cjs/common-internals-web → common-internals-web}/hex.d.ts +0 -0
- /package/{cjs/common-internals-web → common-internals-web}/logging.d.ts +0 -0
- /package/{cjs/common-internals-web → common-internals-web}/utf8.d.ts +0 -0
- /package/{cjs/exit-hook.d.ts → exit-hook.d.ts} +0 -0
- /package/{cjs/idb → idb}/driver.d.ts +0 -0
- /package/{cjs/idb → idb}/utils.d.ts +0 -0
- /package/{cjs/index.d.ts → index.d.cts} +0 -0
- /package/{cjs/methods.d.ts → methods.d.cts} +0 -0
- /package/{cjs/utils.d.ts → utils.d.cts} +0 -0
- /package/{cjs/wasm.d.ts → wasm.d.ts} +0 -0
package/esm/worker.js
DELETED
|
@@ -1,123 +0,0 @@
|
|
|
1
|
-
import { setPlatform } from '@mtcute/core/platform.js';
|
|
2
|
-
import { TelegramWorker as TelegramWorkerBase, TelegramWorkerPort as TelegramWorkerPortBase, } from '@mtcute/core/worker.js';
|
|
3
|
-
import { WebPlatform } from './platform.js';
|
|
4
|
-
let _registered = false;
|
|
5
|
-
export class TelegramWorker extends TelegramWorkerBase {
|
|
6
|
-
registerWorker(handler) {
|
|
7
|
-
if (_registered) {
|
|
8
|
-
throw new Error('TelegramWorker must be created only once');
|
|
9
|
-
}
|
|
10
|
-
_registered = true;
|
|
11
|
-
if (typeof SharedWorkerGlobalScope !== 'undefined' && self instanceof SharedWorkerGlobalScope) {
|
|
12
|
-
const connections = [];
|
|
13
|
-
const broadcast = (message) => {
|
|
14
|
-
for (const port of connections) {
|
|
15
|
-
port.postMessage(message);
|
|
16
|
-
}
|
|
17
|
-
};
|
|
18
|
-
self.onconnect = (event) => {
|
|
19
|
-
const port = event.ports[0];
|
|
20
|
-
connections.push(port);
|
|
21
|
-
const respond = port.postMessage.bind(port);
|
|
22
|
-
// not very reliable, but better than nothing
|
|
23
|
-
// SharedWorker API doesn't provide a way to detect when the client closes the connection
|
|
24
|
-
// so we just assume that the client is done when it sends a 'close' message
|
|
25
|
-
// and keep a timeout for the case when the client closes without sending a 'close' message
|
|
26
|
-
const onClose = () => {
|
|
27
|
-
port.close();
|
|
28
|
-
const idx = connections.indexOf(port);
|
|
29
|
-
if (idx >= 0) {
|
|
30
|
-
connections.splice(connections.indexOf(port), 1);
|
|
31
|
-
}
|
|
32
|
-
};
|
|
33
|
-
const onTimeout = () => {
|
|
34
|
-
console.warn('some connection timed out!');
|
|
35
|
-
respond({ __type__: 'timeout' });
|
|
36
|
-
onClose();
|
|
37
|
-
};
|
|
38
|
-
// 60s should be a reasonable timeout considering that the client should send a ping every 10s
|
|
39
|
-
// so even if the browser has suspended the timers, we should still get a ping within a minute
|
|
40
|
-
let timeout = setTimeout(onTimeout, 60000);
|
|
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, 60000);
|
|
49
|
-
return;
|
|
50
|
-
}
|
|
51
|
-
// eslint-disable-next-line ts/no-unsafe-argument
|
|
52
|
-
handler(message.data, respond);
|
|
53
|
-
});
|
|
54
|
-
port.start();
|
|
55
|
-
};
|
|
56
|
-
return broadcast;
|
|
57
|
-
}
|
|
58
|
-
if (typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope) {
|
|
59
|
-
const respond = self.postMessage.bind(self);
|
|
60
|
-
// eslint-disable-next-line ts/no-unsafe-argument
|
|
61
|
-
self.addEventListener('message', (message) => handler(message.data, respond));
|
|
62
|
-
return respond;
|
|
63
|
-
}
|
|
64
|
-
throw new Error('TelegramWorker must be created from a worker');
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
const platform = new WebPlatform();
|
|
68
|
-
export class TelegramWorkerPort extends TelegramWorkerPortBase {
|
|
69
|
-
options;
|
|
70
|
-
constructor(options) {
|
|
71
|
-
setPlatform(platform);
|
|
72
|
-
super(options);
|
|
73
|
-
this.options = options;
|
|
74
|
-
}
|
|
75
|
-
connectToWorker(worker, handler) {
|
|
76
|
-
if (worker instanceof Worker) {
|
|
77
|
-
const send = worker.postMessage.bind(worker);
|
|
78
|
-
const messageHandler = (ev) => {
|
|
79
|
-
// eslint-disable-next-line ts/no-unsafe-argument
|
|
80
|
-
handler(ev.data);
|
|
81
|
-
};
|
|
82
|
-
worker.addEventListener('message', messageHandler);
|
|
83
|
-
return [
|
|
84
|
-
send,
|
|
85
|
-
() => {
|
|
86
|
-
worker.removeEventListener('message', messageHandler);
|
|
87
|
-
},
|
|
88
|
-
];
|
|
89
|
-
}
|
|
90
|
-
if (worker instanceof SharedWorker) {
|
|
91
|
-
const send = worker.port.postMessage.bind(worker.port);
|
|
92
|
-
const pingInterval = setInterval(() => {
|
|
93
|
-
worker.port.postMessage({ __type__: 'ping' });
|
|
94
|
-
}, 10000);
|
|
95
|
-
const messageHandler = (ev) => {
|
|
96
|
-
if (ev.data.__type__ === 'timeout') {
|
|
97
|
-
// we got disconnected from the worker due to timeout
|
|
98
|
-
// if the page is still alive (which is unlikely), we should reconnect
|
|
99
|
-
// however it's not really possible with SharedWorker API without re-creating the worker
|
|
100
|
-
// so we just reload the page for now
|
|
101
|
-
location.reload();
|
|
102
|
-
return;
|
|
103
|
-
}
|
|
104
|
-
// eslint-disable-next-line ts/no-unsafe-argument
|
|
105
|
-
handler(ev.data);
|
|
106
|
-
};
|
|
107
|
-
worker.port.addEventListener('message', messageHandler);
|
|
108
|
-
worker.port.start();
|
|
109
|
-
let cancelBeforeExit;
|
|
110
|
-
const close = () => {
|
|
111
|
-
clearInterval(pingInterval);
|
|
112
|
-
worker.port.postMessage({ __type__: 'close' });
|
|
113
|
-
worker.port.removeEventListener('message', messageHandler);
|
|
114
|
-
worker.port.close();
|
|
115
|
-
cancelBeforeExit();
|
|
116
|
-
};
|
|
117
|
-
cancelBeforeExit = platform.beforeExit(close);
|
|
118
|
-
return [send, close];
|
|
119
|
-
}
|
|
120
|
-
throw new Error('Only workers and shared workers are supported');
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
//# sourceMappingURL=worker.js.map
|
package/esm/worker.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"worker.js","sourceRoot":"","sources":["../../src/worker.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,0BAA0B,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IAAI,CAAC,QAAQ,KAAK,SAAS,EAAE,CAAC;oBACjC,qDAAqD;oBACrD,sEAAsE;oBACtE,wFAAwF;oBACxF,qCAAqC;oBACrC,QAAQ,CAAC,MAAM,EAAE,CAAA;oBAEjB,OAAM;gBACV,CAAC;gBAED,iDAAiD;gBACjD,OAAO,CAAC,EAAE,CAAC,IAAI,CAAC,CAAA;YACpB,CAAC,CAAA;YAED,MAAM,CAAC,IAAI,CAAC,gBAAgB,CAAC,SAAS,EAAE,cAAc,CAAC,CAAA;YACvD,MAAM,CAAC,IAAI,CAAC,KAAK,EAAE,CAAA;YAEnB,IAAI,gBAA4B,CAAA;YAEhC,MAAM,KAAK,GAAG,GAAG,EAAE;gBACf,aAAa,CAAC,YAAY,CAAC,CAAA;gBAC3B,MAAM,CAAC,IAAI,CAAC,WAAW,CAAC,EAAE,QAAQ,EAAE,OAAO,EAAE,CAAC,CAAA;gBAC9C,MAAM,CAAC,IAAI,CAAC,mBAAmB,CAAC,SAAS,EAAE,cAAc,CAAC,CAAA;gBAC1D,MAAM,CAAC,IAAI,CAAC,KAAK,EAAE,CAAA;gBACnB,gBAAgB,EAAE,CAAA;YACtB,CAAC,CAAA;YAED,gBAAgB,GAAG,QAAQ,CAAC,UAAU,CAAC,KAAK,CAAC,CAAA;YAE7C,OAAO,CAAC,IAAI,EAAE,KAAK,CAAC,CAAA;QACxB,CAAC;QAED,MAAM,IAAI,KAAK,CAAC,+CAA+C,CAAC,CAAA;IACpE,CAAC;CACJ","sourcesContent":["import { setPlatform } from '@mtcute/core/platform.js'\nimport type {\n ClientMessageHandler,\n RespondFn,\n SendFn,\n SomeWorker,\n TelegramWorkerOptions,\n TelegramWorkerPortOptions,\n WorkerCustomMethods,\n WorkerMessageHandler,\n} from '@mtcute/core/worker.js'\nimport {\n TelegramWorker as TelegramWorkerBase,\n TelegramWorkerPort as TelegramWorkerPortBase,\n} from '@mtcute/core/worker.js'\n\nimport { WebPlatform } from './platform.js'\n\nexport type { TelegramWorkerOptions, TelegramWorkerPortOptions, WorkerCustomMethods }\n\nlet _registered = false\n\nexport class TelegramWorker<T extends WorkerCustomMethods> extends TelegramWorkerBase<T> {\n registerWorker(handler: WorkerMessageHandler): RespondFn {\n if (_registered) {\n throw new Error('TelegramWorker must be created only once')\n }\n\n _registered = true\n\n if (typeof SharedWorkerGlobalScope !== 'undefined' && self instanceof SharedWorkerGlobalScope) {\n const connections: MessagePort[] = []\n\n const broadcast = (message: unknown) => {\n for (const port of connections) {\n port.postMessage(message)\n }\n }\n\n self.onconnect = (event: MessageEvent) => {\n const port = event.ports[0]\n connections.push(port)\n\n const respond = port.postMessage.bind(port)\n\n // not very reliable, but better than nothing\n // SharedWorker API doesn't provide a way to detect when the client closes the connection\n // so we just assume that the client is done when it sends a 'close' message\n // and keep a timeout for the case when the client closes without sending a 'close' message\n const onClose = () => {\n port.close()\n const idx = connections.indexOf(port)\n\n if (idx >= 0) {\n connections.splice(connections.indexOf(port), 1)\n }\n }\n\n const onTimeout = () => {\n console.warn('some connection timed out!')\n respond({ __type__: 'timeout' })\n onClose()\n }\n\n // 60s should be a reasonable timeout considering that the client should send a ping every 10s\n // so even if the browser has suspended the timers, we should still get a ping within a minute\n let timeout = setTimeout(onTimeout, 60000)\n\n port.addEventListener('message', (message) => {\n if (message.data.__type__ === 'close') {\n onClose()\n\n return\n }\n\n if (message.data.__type__ === 'ping') {\n clearTimeout(timeout)\n timeout = setTimeout(onTimeout, 60000)\n\n return\n }\n\n // eslint-disable-next-line ts/no-unsafe-argument\n handler(message.data, respond)\n })\n port.start()\n }\n\n return broadcast\n }\n\n if (typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope) {\n const respond: RespondFn = self.postMessage.bind(self)\n\n // eslint-disable-next-line ts/no-unsafe-argument\n self.addEventListener('message', (message: MessageEvent) => handler(message.data, respond))\n\n return respond\n }\n\n throw new Error('TelegramWorker must be created from a worker')\n }\n}\n\nconst platform = new WebPlatform()\n\nexport class TelegramWorkerPort<T extends WorkerCustomMethods> extends TelegramWorkerPortBase<T> {\n constructor(readonly options: TelegramWorkerPortOptions) {\n setPlatform(platform)\n super(options)\n }\n\n connectToWorker(worker: SomeWorker, handler: ClientMessageHandler): [SendFn, () => void] {\n if (worker instanceof Worker) {\n const send: SendFn = worker.postMessage.bind(worker)\n\n const messageHandler = (ev: MessageEvent) => {\n // eslint-disable-next-line ts/no-unsafe-argument\n handler(ev.data)\n }\n\n worker.addEventListener('message', messageHandler)\n\n return [\n send,\n () => {\n worker.removeEventListener('message', messageHandler)\n },\n ]\n }\n\n if (worker instanceof SharedWorker) {\n const send: SendFn = worker.port.postMessage.bind(worker.port)\n\n const pingInterval = setInterval(() => {\n worker.port.postMessage({ __type__: 'ping' })\n }, 10000)\n\n const messageHandler = (ev: MessageEvent) => {\n if (ev.data.__type__ === 'timeout') {\n // we got disconnected from the worker due to timeout\n // if the page is still alive (which is unlikely), we should reconnect\n // however it's not really possible with SharedWorker API without re-creating the worker\n // so we just reload the page for now\n location.reload()\n\n return\n }\n\n // eslint-disable-next-line ts/no-unsafe-argument\n handler(ev.data)\n }\n\n worker.port.addEventListener('message', messageHandler)\n worker.port.start()\n\n let cancelBeforeExit: () => void\n\n const close = () => {\n clearInterval(pingInterval)\n worker.port.postMessage({ __type__: 'close' })\n worker.port.removeEventListener('message', messageHandler)\n worker.port.close()\n cancelBeforeExit()\n }\n\n cancelBeforeExit = platform.beforeExit(close)\n\n return [send, close]\n }\n\n throw new Error('Only workers and shared workers are supported')\n }\n}\n"]}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|