@mtcute/core 0.25.1 → 0.25.3
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/highlevel/methods/messages/send-common.cjs +2 -1
- package/highlevel/methods/messages/send-common.d.cts +4 -0
- package/highlevel/methods/messages/send-common.d.ts +4 -0
- package/highlevel/methods/messages/send-common.js +2 -1
- package/highlevel/methods/misc/normalize-text.cjs +30 -1
- package/highlevel/methods/misc/normalize-text.js +30 -1
- package/highlevel/methods/misc/normalize-text.test.d.cts +1 -0
- package/highlevel/methods/misc/normalize-text.test.d.ts +1 -0
- package/highlevel/methods/premium/get-star-gift-options.cjs +3 -1
- package/highlevel/methods/premium/get-star-gift-options.js +3 -1
- package/highlevel/types/messages/message-action.cjs +1 -1
- package/highlevel/types/messages/message-action.js +1 -1
- package/highlevel/types/messages/replied-message.cjs +4 -0
- package/highlevel/types/messages/replied-message.d.cts +2 -0
- package/highlevel/types/messages/replied-message.d.ts +2 -0
- package/highlevel/types/messages/replied-message.js +4 -0
- package/highlevel/types/premium/saved-star-gift.cjs +1 -1
- package/highlevel/types/premium/saved-star-gift.js +1 -1
- package/highlevel/types/premium/stars-gift-unique.cjs +7 -0
- package/highlevel/types/premium/stars-gift-unique.d.cts +4 -0
- package/highlevel/types/premium/stars-gift-unique.d.ts +4 -0
- package/highlevel/types/premium/stars-gift-unique.js +7 -0
- package/highlevel/types/premium/stars-gift.cjs +10 -1
- package/highlevel/types/premium/stars-gift.d.cts +8 -2
- package/highlevel/types/premium/stars-gift.d.ts +8 -2
- package/highlevel/types/premium/stars-gift.js +10 -1
- package/highlevel/types/premium/stars-transaction.cjs +1 -1
- package/highlevel/types/premium/stars-transaction.js +1 -1
- package/highlevel/worker/invoker.cjs +4 -1
- package/highlevel/worker/invoker.d.cts +2 -1
- package/highlevel/worker/invoker.d.ts +2 -1
- package/highlevel/worker/invoker.js +4 -1
- package/highlevel/worker/port.cjs +4 -1
- package/highlevel/worker/port.d.cts +2 -0
- package/highlevel/worker/port.d.ts +2 -0
- package/highlevel/worker/port.js +5 -2
- package/highlevel/worker/protocol.cjs +2 -0
- package/highlevel/worker/protocol.d.cts +10 -0
- package/highlevel/worker/protocol.d.ts +10 -0
- package/highlevel/worker/protocol.js +2 -0
- package/highlevel/worker/worker.cjs +23 -2
- package/highlevel/worker/worker.d.cts +5 -1
- package/highlevel/worker/worker.d.ts +5 -1
- package/highlevel/worker/worker.js +24 -3
- package/network/network-manager.cjs +1 -1
- package/network/network-manager.js +1 -1
- package/package.json +2 -2
- package/utils/binary/compat.cjs +22 -2
- package/utils/binary/compat.js +22 -2
- package/worker.cjs +1 -0
- package/worker.js +2 -1
|
@@ -56,7 +56,8 @@ async function _processCommonSendParameters(client, chatId, params) {
|
|
|
56
56
|
quoteText: params.quote?.text,
|
|
57
57
|
quoteEntities: params.quote?.entities,
|
|
58
58
|
quoteOffset: params.quoteOffset,
|
|
59
|
-
monoforumPeerId: params.toMonoforumPeer ? await resolvePeer.resolvePeer(client, params.toMonoforumPeer) : void 0
|
|
59
|
+
monoforumPeerId: params.toMonoforumPeer ? await resolvePeer.resolvePeer(client, params.toMonoforumPeer) : void 0,
|
|
60
|
+
todoItemId: params.replyToTodoItem
|
|
60
61
|
};
|
|
61
62
|
} else if (params.toMonoforumPeer) {
|
|
62
63
|
tlReplyTo = {
|
|
@@ -12,6 +12,10 @@ export interface CommonSendParams {
|
|
|
12
12
|
* Can also be a message from another chat, in which case a quote will be sent.
|
|
13
13
|
*/
|
|
14
14
|
replyTo?: number | Message;
|
|
15
|
+
/**
|
|
16
|
+
* If this is a reply to a specific todo item, ID of that item
|
|
17
|
+
*/
|
|
18
|
+
replyToTodoItem?: number;
|
|
15
19
|
/**
|
|
16
20
|
* When the chat is a monoforum you are an admin of, you **must** pass an
|
|
17
21
|
* ID of a peer you are sending the message to.
|
|
@@ -12,6 +12,10 @@ export interface CommonSendParams {
|
|
|
12
12
|
* Can also be a message from another chat, in which case a quote will be sent.
|
|
13
13
|
*/
|
|
14
14
|
replyTo?: number | Message;
|
|
15
|
+
/**
|
|
16
|
+
* If this is a reply to a specific todo item, ID of that item
|
|
17
|
+
*/
|
|
18
|
+
replyToTodoItem?: number;
|
|
15
19
|
/**
|
|
16
20
|
* When the chat is a monoforum you are an admin of, you **must** pass an
|
|
17
21
|
* ID of a peer you are sending the message to.
|
|
@@ -49,7 +49,8 @@ async function _processCommonSendParameters(client, chatId, params) {
|
|
|
49
49
|
quoteText: params.quote?.text,
|
|
50
50
|
quoteEntities: params.quote?.entities,
|
|
51
51
|
quoteOffset: params.quoteOffset,
|
|
52
|
-
monoforumPeerId: params.toMonoforumPeer ? await resolvePeer(client, params.toMonoforumPeer) : void 0
|
|
52
|
+
monoforumPeerId: params.toMonoforumPeer ? await resolvePeer(client, params.toMonoforumPeer) : void 0,
|
|
53
|
+
todoItemId: params.replyToTodoItem
|
|
53
54
|
};
|
|
54
55
|
} else if (params.toMonoforumPeer) {
|
|
55
56
|
tlReplyTo = {
|
|
@@ -7,6 +7,19 @@ if (typeof globalThis !== "undefined" && !globalThis._MTCUTE_CJS_DEPRECATION_WAR
|
|
|
7
7
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
8
8
|
const resolvePeer = require("../users/resolve-peer.cjs");
|
|
9
9
|
const empty = ["", void 0];
|
|
10
|
+
function adjustOffsets(entities, from, by) {
|
|
11
|
+
for (const ent of entities) {
|
|
12
|
+
if (ent.offset < from) continue;
|
|
13
|
+
if (by >= 0) {
|
|
14
|
+
ent.offset += by;
|
|
15
|
+
continue;
|
|
16
|
+
}
|
|
17
|
+
const adjustTotal = Math.min(ent.offset, Math.abs(by));
|
|
18
|
+
const adjustInternal = Math.max(Math.abs(by) - ent.offset, 0);
|
|
19
|
+
ent.offset -= adjustTotal;
|
|
20
|
+
ent.length -= adjustInternal;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
10
23
|
async function _normalizeInputText(client, input) {
|
|
11
24
|
if (!input) {
|
|
12
25
|
return empty;
|
|
@@ -14,8 +27,10 @@ async function _normalizeInputText(client, input) {
|
|
|
14
27
|
if (typeof input === "string") {
|
|
15
28
|
return [input, void 0];
|
|
16
29
|
}
|
|
17
|
-
|
|
30
|
+
let text = input.text;
|
|
31
|
+
const entities = input.entities;
|
|
18
32
|
if (!entities) return [text, void 0];
|
|
33
|
+
let canTrimStart = true;
|
|
19
34
|
for (const ent of entities) {
|
|
20
35
|
if (ent._ === "messageEntityMentionName") {
|
|
21
36
|
try {
|
|
@@ -27,6 +42,20 @@ async function _normalizeInputText(client, input) {
|
|
|
27
42
|
client.log.warn("Failed to resolve mention entity for %s: %s", ent.userId, e);
|
|
28
43
|
}
|
|
29
44
|
}
|
|
45
|
+
if (ent._ === "messageEntityPre" && ent.offset === 0) {
|
|
46
|
+
canTrimStart = false;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
if (canTrimStart) {
|
|
50
|
+
const resultTrimmed = text.trimStart();
|
|
51
|
+
const numCharsTrimmed = text.length - resultTrimmed.length;
|
|
52
|
+
text = resultTrimmed;
|
|
53
|
+
adjustOffsets(entities, 0, -numCharsTrimmed);
|
|
54
|
+
}
|
|
55
|
+
text = text.trimEnd();
|
|
56
|
+
for (const ent of entities) {
|
|
57
|
+
const end = ent.offset + ent.length;
|
|
58
|
+
if (end > text.length) ent.length = text.length - ent.offset;
|
|
30
59
|
}
|
|
31
60
|
return [text, entities];
|
|
32
61
|
}
|
|
@@ -1,5 +1,18 @@
|
|
|
1
1
|
import { resolveUser } from "../users/resolve-peer.js";
|
|
2
2
|
const empty = ["", void 0];
|
|
3
|
+
function adjustOffsets(entities, from, by) {
|
|
4
|
+
for (const ent of entities) {
|
|
5
|
+
if (ent.offset < from) continue;
|
|
6
|
+
if (by >= 0) {
|
|
7
|
+
ent.offset += by;
|
|
8
|
+
continue;
|
|
9
|
+
}
|
|
10
|
+
const adjustTotal = Math.min(ent.offset, Math.abs(by));
|
|
11
|
+
const adjustInternal = Math.max(Math.abs(by) - ent.offset, 0);
|
|
12
|
+
ent.offset -= adjustTotal;
|
|
13
|
+
ent.length -= adjustInternal;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
3
16
|
async function _normalizeInputText(client, input) {
|
|
4
17
|
if (!input) {
|
|
5
18
|
return empty;
|
|
@@ -7,8 +20,10 @@ async function _normalizeInputText(client, input) {
|
|
|
7
20
|
if (typeof input === "string") {
|
|
8
21
|
return [input, void 0];
|
|
9
22
|
}
|
|
10
|
-
|
|
23
|
+
let text = input.text;
|
|
24
|
+
const entities = input.entities;
|
|
11
25
|
if (!entities) return [text, void 0];
|
|
26
|
+
let canTrimStart = true;
|
|
12
27
|
for (const ent of entities) {
|
|
13
28
|
if (ent._ === "messageEntityMentionName") {
|
|
14
29
|
try {
|
|
@@ -20,6 +35,20 @@ async function _normalizeInputText(client, input) {
|
|
|
20
35
|
client.log.warn("Failed to resolve mention entity for %s: %s", ent.userId, e);
|
|
21
36
|
}
|
|
22
37
|
}
|
|
38
|
+
if (ent._ === "messageEntityPre" && ent.offset === 0) {
|
|
39
|
+
canTrimStart = false;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
if (canTrimStart) {
|
|
43
|
+
const resultTrimmed = text.trimStart();
|
|
44
|
+
const numCharsTrimmed = text.length - resultTrimmed.length;
|
|
45
|
+
text = resultTrimmed;
|
|
46
|
+
adjustOffsets(entities, 0, -numCharsTrimmed);
|
|
47
|
+
}
|
|
48
|
+
text = text.trimEnd();
|
|
49
|
+
for (const ent of entities) {
|
|
50
|
+
const end = ent.offset + ent.length;
|
|
51
|
+
if (end > text.length) ent.length = text.length - ent.offset;
|
|
23
52
|
}
|
|
24
53
|
return [text, entities];
|
|
25
54
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -7,16 +7,18 @@ if (typeof globalThis !== "undefined" && !globalThis._MTCUTE_CJS_DEPRECATION_WAR
|
|
|
7
7
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
8
8
|
const typeAssertions = require("../../../utils/type-assertions.cjs");
|
|
9
9
|
const starsGift = require("../../types/premium/stars-gift.cjs");
|
|
10
|
+
const peersIndex = require("../../types/peers/peers-index.cjs");
|
|
10
11
|
async function getStarGiftOptions(client) {
|
|
11
12
|
const res = await client.call({
|
|
12
13
|
_: "payments.getStarGifts",
|
|
13
14
|
hash: 0
|
|
14
15
|
});
|
|
15
16
|
typeAssertions.assertTypeIsNot("payments.getStarGifts", res, "payments.starGiftsNotModified");
|
|
17
|
+
const peers = peersIndex.PeersIndex.from(res);
|
|
16
18
|
const ret = [];
|
|
17
19
|
for (const gift of res.gifts) {
|
|
18
20
|
if (gift._ === "starGift") {
|
|
19
|
-
ret.push(new starsGift.StarGift(gift));
|
|
21
|
+
ret.push(new starsGift.StarGift(gift, peers));
|
|
20
22
|
}
|
|
21
23
|
}
|
|
22
24
|
return ret;
|
|
@@ -1,15 +1,17 @@
|
|
|
1
1
|
import { assertTypeIsNot } from "../../../utils/type-assertions.js";
|
|
2
2
|
import { StarGift as StarGift$2 } from "../../types/premium/stars-gift.js";
|
|
3
|
+
import { PeersIndex } from "../../types/peers/peers-index.js";
|
|
3
4
|
async function getStarGiftOptions(client) {
|
|
4
5
|
const res = await client.call({
|
|
5
6
|
_: "payments.getStarGifts",
|
|
6
7
|
hash: 0
|
|
7
8
|
});
|
|
8
9
|
assertTypeIsNot("payments.getStarGifts", res, "payments.starGiftsNotModified");
|
|
10
|
+
const peers = PeersIndex.from(res);
|
|
9
11
|
const ret = [];
|
|
10
12
|
for (const gift of res.gifts) {
|
|
11
13
|
if (gift._ === "starGift") {
|
|
12
|
-
ret.push(new StarGift$2(gift));
|
|
14
|
+
ret.push(new StarGift$2(gift, peers));
|
|
13
15
|
}
|
|
14
16
|
}
|
|
15
17
|
return ret;
|
|
@@ -295,7 +295,7 @@ function _messageActionFromTl(act) {
|
|
|
295
295
|
converted: act.converted,
|
|
296
296
|
convertStars: act.convertStars ?? Long.ZERO,
|
|
297
297
|
refunded: act.refunded,
|
|
298
|
-
gift: new starsGift.StarGift(act.gift),
|
|
298
|
+
gift: new starsGift.StarGift(act.gift, this._peers),
|
|
299
299
|
message: act.message ?? null,
|
|
300
300
|
canUpgrade: act.canUpgrade,
|
|
301
301
|
upgraded: act.upgraded,
|
|
@@ -288,7 +288,7 @@ function _messageActionFromTl(act) {
|
|
|
288
288
|
converted: act.converted,
|
|
289
289
|
convertStars: act.convertStars ?? Long.ZERO,
|
|
290
290
|
refunded: act.refunded,
|
|
291
|
-
gift: new StarGift$2(act.gift),
|
|
291
|
+
gift: new StarGift$2(act.gift, this._peers),
|
|
292
292
|
message: act.message ?? null,
|
|
293
293
|
canUpgrade: act.canUpgrade,
|
|
294
294
|
upgraded: act.upgraded,
|
|
@@ -66,6 +66,10 @@ class RepliedMessageInfo {
|
|
|
66
66
|
}
|
|
67
67
|
return this.raw.replyToTopId ?? null;
|
|
68
68
|
}
|
|
69
|
+
/** If this is a reply to a specific todo item, ID of that item */
|
|
70
|
+
get todoItemId() {
|
|
71
|
+
return this.raw.todoItemId ?? null;
|
|
72
|
+
}
|
|
69
73
|
/**
|
|
70
74
|
* If replied-to message is available, chat where the message was sent.
|
|
71
75
|
*
|
|
@@ -56,6 +56,8 @@ export declare class RepliedMessageInfo {
|
|
|
56
56
|
get id(): number | null;
|
|
57
57
|
/** ID of the replies thread where this message belongs to */
|
|
58
58
|
get threadId(): number | null;
|
|
59
|
+
/** If this is a reply to a specific todo item, ID of that item */
|
|
60
|
+
get todoItemId(): number | null;
|
|
59
61
|
/**
|
|
60
62
|
* If replied-to message is available, chat where the message was sent.
|
|
61
63
|
*
|
|
@@ -56,6 +56,8 @@ export declare class RepliedMessageInfo {
|
|
|
56
56
|
get id(): number | null;
|
|
57
57
|
/** ID of the replies thread where this message belongs to */
|
|
58
58
|
get threadId(): number | null;
|
|
59
|
+
/** If this is a reply to a specific todo item, ID of that item */
|
|
60
|
+
get todoItemId(): number | null;
|
|
59
61
|
/**
|
|
60
62
|
* If replied-to message is available, chat where the message was sent.
|
|
61
63
|
*
|
|
@@ -59,6 +59,10 @@ class RepliedMessageInfo {
|
|
|
59
59
|
}
|
|
60
60
|
return this.raw.replyToTopId ?? null;
|
|
61
61
|
}
|
|
62
|
+
/** If this is a reply to a specific todo item, ID of that item */
|
|
63
|
+
get todoItemId() {
|
|
64
|
+
return this.raw.todoItemId ?? null;
|
|
65
|
+
}
|
|
62
66
|
/**
|
|
63
67
|
* If replied-to message is available, chat where the message was sent.
|
|
64
68
|
*
|
|
@@ -46,7 +46,7 @@ class SavedStarGift {
|
|
|
46
46
|
/** The gift itself */
|
|
47
47
|
get gift() {
|
|
48
48
|
if (this.raw.gift._ === "starGift") {
|
|
49
|
-
return new starsGift.StarGift(this.raw.gift);
|
|
49
|
+
return new starsGift.StarGift(this.raw.gift, this._peers);
|
|
50
50
|
}
|
|
51
51
|
return new starsGiftUnique.StarGiftUnique(this.raw.gift, this._peers);
|
|
52
52
|
}
|
|
@@ -39,7 +39,7 @@ class SavedStarGift {
|
|
|
39
39
|
/** The gift itself */
|
|
40
40
|
get gift() {
|
|
41
41
|
if (this.raw.gift._ === "starGift") {
|
|
42
|
-
return new StarGift$2(this.raw.gift);
|
|
42
|
+
return new StarGift$2(this.raw.gift, this._peers);
|
|
43
43
|
}
|
|
44
44
|
return new StarGiftUnique$2(this.raw.gift, this._peers);
|
|
45
45
|
}
|
|
@@ -197,6 +197,13 @@ class StarGiftUnique {
|
|
|
197
197
|
get giftAddress() {
|
|
198
198
|
return this.raw.giftAddress ?? null;
|
|
199
199
|
}
|
|
200
|
+
/**
|
|
201
|
+
* User/channel who released this collection
|
|
202
|
+
*/
|
|
203
|
+
get releasedBy() {
|
|
204
|
+
if (!this.raw.releasedBy) return null;
|
|
205
|
+
return peer.parsePeer(this.raw.releasedBy, this._peers);
|
|
206
|
+
}
|
|
200
207
|
}
|
|
201
208
|
const StarGiftUnique$1 = /* @__PURE__ */ inspectable.makeInspectable(StarGiftUnique);
|
|
202
209
|
const StarGiftUnique$2 = /* @__PURE__ */ memoize.memoizeGetters(StarGiftUnique$1, ["model", "pattern", "backdrop", "originalDetails"]);
|
|
@@ -190,6 +190,13 @@ class StarGiftUnique {
|
|
|
190
190
|
get giftAddress() {
|
|
191
191
|
return this.raw.giftAddress ?? null;
|
|
192
192
|
}
|
|
193
|
+
/**
|
|
194
|
+
* User/channel who released this collection
|
|
195
|
+
*/
|
|
196
|
+
get releasedBy() {
|
|
197
|
+
if (!this.raw.releasedBy) return null;
|
|
198
|
+
return parsePeer(this.raw.releasedBy, this._peers);
|
|
199
|
+
}
|
|
193
200
|
}
|
|
194
201
|
const StarGiftUnique$1 = /* @__PURE__ */ makeInspectable(StarGiftUnique);
|
|
195
202
|
const StarGiftUnique$2 = /* @__PURE__ */ memoizeGetters(StarGiftUnique$1, ["model", "pattern", "backdrop", "originalDetails"]);
|
|
@@ -11,9 +11,11 @@ const typeAssertions = require("../../../utils/type-assertions.cjs");
|
|
|
11
11
|
const inspectable = require("../../utils/inspectable.cjs");
|
|
12
12
|
const memoize = require("../../utils/memoize.cjs");
|
|
13
13
|
const documentUtils = require("../media/document-utils.cjs");
|
|
14
|
+
const peer = require("../peers/peer.cjs");
|
|
14
15
|
class StarGift {
|
|
15
|
-
constructor(raw) {
|
|
16
|
+
constructor(raw, _peers) {
|
|
16
17
|
this.raw = raw;
|
|
18
|
+
this._peers = _peers;
|
|
17
19
|
}
|
|
18
20
|
/** ID of the gift */
|
|
19
21
|
get id() {
|
|
@@ -86,6 +88,13 @@ class StarGift {
|
|
|
86
88
|
get title() {
|
|
87
89
|
return this.raw.title ?? null;
|
|
88
90
|
}
|
|
91
|
+
/**
|
|
92
|
+
* User/channel who released this collection
|
|
93
|
+
*/
|
|
94
|
+
get releasedBy() {
|
|
95
|
+
if (!this.raw.releasedBy) return null;
|
|
96
|
+
return peer.parsePeer(this.raw.releasedBy, this._peers);
|
|
97
|
+
}
|
|
89
98
|
}
|
|
90
99
|
const StarGift$1 = /* @__PURE__ */ inspectable.makeInspectable(StarGift);
|
|
91
100
|
const StarGift$2 = /* @__PURE__ */ memoize.memoizeGetters(StarGift$1, ["sticker"]);
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { tl } from '@mtcute/tl';
|
|
2
2
|
import { Sticker } from '../media/sticker.js';
|
|
3
3
|
import { Message } from '../messages/message.js';
|
|
4
|
-
import {
|
|
4
|
+
import { PeersIndex } from '../peers/peers-index.js';
|
|
5
5
|
import { default as Long } from 'long';
|
|
6
|
+
import { InputPeerLike, Peer } from '../peers/peer.js';
|
|
6
7
|
export type InputStarGift = {
|
|
7
8
|
/** ID of the message containing the gift */
|
|
8
9
|
message: number | Message;
|
|
@@ -17,7 +18,8 @@ export type InputStarGift = {
|
|
|
17
18
|
*/
|
|
18
19
|
export declare class StarGift {
|
|
19
20
|
readonly raw: tl.RawStarGift;
|
|
20
|
-
|
|
21
|
+
readonly _peers: PeersIndex;
|
|
22
|
+
constructor(raw: tl.RawStarGift, _peers: PeersIndex);
|
|
21
23
|
/** ID of the gift */
|
|
22
24
|
get id(): tl.Long;
|
|
23
25
|
/** Whether this gift sold out and cannot be bought anymore */
|
|
@@ -59,4 +61,8 @@ export declare class StarGift {
|
|
|
59
61
|
get resaleFloorPrice(): tl.Long | null;
|
|
60
62
|
/** Title of the gift */
|
|
61
63
|
get title(): string | null;
|
|
64
|
+
/**
|
|
65
|
+
* User/channel who released this collection
|
|
66
|
+
*/
|
|
67
|
+
get releasedBy(): Peer | null;
|
|
62
68
|
}
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { tl } from '@mtcute/tl';
|
|
2
2
|
import { Sticker } from '../media/sticker.js';
|
|
3
3
|
import { Message } from '../messages/message.js';
|
|
4
|
-
import {
|
|
4
|
+
import { PeersIndex } from '../peers/peers-index.js';
|
|
5
5
|
import { default as Long } from 'long';
|
|
6
|
+
import { InputPeerLike, Peer } from '../peers/peer.js';
|
|
6
7
|
export type InputStarGift = {
|
|
7
8
|
/** ID of the message containing the gift */
|
|
8
9
|
message: number | Message;
|
|
@@ -17,7 +18,8 @@ export type InputStarGift = {
|
|
|
17
18
|
*/
|
|
18
19
|
export declare class StarGift {
|
|
19
20
|
readonly raw: tl.RawStarGift;
|
|
20
|
-
|
|
21
|
+
readonly _peers: PeersIndex;
|
|
22
|
+
constructor(raw: tl.RawStarGift, _peers: PeersIndex);
|
|
21
23
|
/** ID of the gift */
|
|
22
24
|
get id(): tl.Long;
|
|
23
25
|
/** Whether this gift sold out and cannot be bought anymore */
|
|
@@ -59,4 +61,8 @@ export declare class StarGift {
|
|
|
59
61
|
get resaleFloorPrice(): tl.Long | null;
|
|
60
62
|
/** Title of the gift */
|
|
61
63
|
get title(): string | null;
|
|
64
|
+
/**
|
|
65
|
+
* User/channel who released this collection
|
|
66
|
+
*/
|
|
67
|
+
get releasedBy(): Peer | null;
|
|
62
68
|
}
|
|
@@ -4,9 +4,11 @@ import { assertTypeIs } from "../../../utils/type-assertions.js";
|
|
|
4
4
|
import { makeInspectable } from "../../utils/inspectable.js";
|
|
5
5
|
import { memoizeGetters } from "../../utils/memoize.js";
|
|
6
6
|
import { parseDocument } from "../media/document-utils.js";
|
|
7
|
+
import { parsePeer } from "../peers/peer.js";
|
|
7
8
|
class StarGift {
|
|
8
|
-
constructor(raw) {
|
|
9
|
+
constructor(raw, _peers) {
|
|
9
10
|
this.raw = raw;
|
|
11
|
+
this._peers = _peers;
|
|
10
12
|
}
|
|
11
13
|
/** ID of the gift */
|
|
12
14
|
get id() {
|
|
@@ -79,6 +81,13 @@ class StarGift {
|
|
|
79
81
|
get title() {
|
|
80
82
|
return this.raw.title ?? null;
|
|
81
83
|
}
|
|
84
|
+
/**
|
|
85
|
+
* User/channel who released this collection
|
|
86
|
+
*/
|
|
87
|
+
get releasedBy() {
|
|
88
|
+
if (!this.raw.releasedBy) return null;
|
|
89
|
+
return parsePeer(this.raw.releasedBy, this._peers);
|
|
90
|
+
}
|
|
82
91
|
}
|
|
83
92
|
const StarGift$1 = /* @__PURE__ */ makeInspectable(StarGift);
|
|
84
93
|
const StarGift$2 = /* @__PURE__ */ memoizeGetters(StarGift$1, ["sticker"]);
|
|
@@ -118,7 +118,7 @@ class StarsTransaction {
|
|
|
118
118
|
return {
|
|
119
119
|
type: "star_gift",
|
|
120
120
|
peer: peer$1,
|
|
121
|
-
gift: new starsGift.StarGift(this.raw.stargift)
|
|
121
|
+
gift: new starsGift.StarGift(this.raw.stargift, this.peers)
|
|
122
122
|
};
|
|
123
123
|
} else if (this.raw.stargiftUpgrade) {
|
|
124
124
|
return {
|
|
@@ -9,14 +9,16 @@ const utils = require("@fuman/utils");
|
|
|
9
9
|
const errors = require("./errors.cjs");
|
|
10
10
|
const protocol = require("./protocol.cjs");
|
|
11
11
|
class WorkerInvoker {
|
|
12
|
-
constructor(send) {
|
|
12
|
+
constructor(send, workerId) {
|
|
13
13
|
this.send = send;
|
|
14
|
+
this.workerId = workerId;
|
|
14
15
|
}
|
|
15
16
|
_nextId = 0;
|
|
16
17
|
_pending = /* @__PURE__ */ new Map();
|
|
17
18
|
_invoke(target, method, args, isVoid, abortSignal) {
|
|
18
19
|
const id = this._nextId++;
|
|
19
20
|
this.send({
|
|
21
|
+
_mtcuteWorkerId: this.workerId,
|
|
20
22
|
type: "invoke",
|
|
21
23
|
id,
|
|
22
24
|
target,
|
|
@@ -27,6 +29,7 @@ class WorkerInvoker {
|
|
|
27
29
|
});
|
|
28
30
|
abortSignal?.addEventListener("abort", () => {
|
|
29
31
|
this.send({
|
|
32
|
+
_mtcuteWorkerId: this.workerId,
|
|
30
33
|
type: "abort",
|
|
31
34
|
id
|
|
32
35
|
});
|
|
@@ -4,7 +4,8 @@ export type InvokeTarget = Extract<WorkerInboundMessage, {
|
|
|
4
4
|
}>['target'];
|
|
5
5
|
export declare class WorkerInvoker {
|
|
6
6
|
private send;
|
|
7
|
-
|
|
7
|
+
private readonly workerId;
|
|
8
|
+
constructor(send: SendFn, workerId: string);
|
|
8
9
|
private _nextId;
|
|
9
10
|
private _pending;
|
|
10
11
|
private _invoke;
|
|
@@ -4,7 +4,8 @@ export type InvokeTarget = Extract<WorkerInboundMessage, {
|
|
|
4
4
|
}>['target'];
|
|
5
5
|
export declare class WorkerInvoker {
|
|
6
6
|
private send;
|
|
7
|
-
|
|
7
|
+
private readonly workerId;
|
|
8
|
+
constructor(send: SendFn, workerId: string);
|
|
8
9
|
private _nextId;
|
|
9
10
|
private _pending;
|
|
10
11
|
private _invoke;
|
|
@@ -2,14 +2,16 @@ import { Deferred } from "@fuman/utils";
|
|
|
2
2
|
import { deserializeError } from "./errors.js";
|
|
3
3
|
import { serializeResult, deserializeResult } from "./protocol.js";
|
|
4
4
|
class WorkerInvoker {
|
|
5
|
-
constructor(send) {
|
|
5
|
+
constructor(send, workerId) {
|
|
6
6
|
this.send = send;
|
|
7
|
+
this.workerId = workerId;
|
|
7
8
|
}
|
|
8
9
|
_nextId = 0;
|
|
9
10
|
_pending = /* @__PURE__ */ new Map();
|
|
10
11
|
_invoke(target, method, args, isVoid, abortSignal) {
|
|
11
12
|
const id = this._nextId++;
|
|
12
13
|
this.send({
|
|
14
|
+
_mtcuteWorkerId: this.workerId,
|
|
13
15
|
type: "invoke",
|
|
14
16
|
id,
|
|
15
17
|
target,
|
|
@@ -20,6 +22,7 @@ class WorkerInvoker {
|
|
|
20
22
|
});
|
|
21
23
|
abortSignal?.addEventListener("abort", () => {
|
|
22
24
|
this.send({
|
|
25
|
+
_mtcuteWorkerId: this.workerId,
|
|
23
26
|
type: "abort",
|
|
24
27
|
id
|
|
25
28
|
});
|
|
@@ -19,8 +19,9 @@ class TelegramWorkerPort {
|
|
|
19
19
|
this.options = options;
|
|
20
20
|
this.log = new logger.LogManager("worker", options.platform);
|
|
21
21
|
this.platform = options.platform;
|
|
22
|
+
this.workerId = options.workerId ?? protocol.DEFAULT_WORKER_ID;
|
|
22
23
|
this._connection = this.connectToWorker(this.options.worker, this._onMessage);
|
|
23
|
-
this._invoker = new invoker.WorkerInvoker(this._connection[0]);
|
|
24
|
+
this._invoker = new invoker.WorkerInvoker(this._connection[0], this.workerId);
|
|
24
25
|
this.storage = new storage.TelegramStorageProxy(this._invoker);
|
|
25
26
|
this.appConfig = new appConfig.AppConfigManagerProxy(this._invoker);
|
|
26
27
|
const bind = this._invoker.makeBinder("client");
|
|
@@ -82,6 +83,7 @@ class TelegramWorkerPort {
|
|
|
82
83
|
recreateDc;
|
|
83
84
|
_abortController = new AbortController();
|
|
84
85
|
stopSignal = this._abortController.signal;
|
|
86
|
+
workerId;
|
|
85
87
|
call(message, params) {
|
|
86
88
|
if (params?.abortSignal) {
|
|
87
89
|
const { abortSignal, ...rest } = params;
|
|
@@ -94,6 +96,7 @@ class TelegramWorkerPort {
|
|
|
94
96
|
onConnectionState = new utils.Emitter();
|
|
95
97
|
onError = new utils.Emitter();
|
|
96
98
|
_onMessage = (message) => {
|
|
99
|
+
if (message._mtcuteWorkerId !== this.workerId) return;
|
|
97
100
|
switch (message.type) {
|
|
98
101
|
case "log":
|
|
99
102
|
this.log.handler(message.color, message.level, message.tag, message.fmt, message.args);
|
|
@@ -12,6 +12,7 @@ import { AppConfigManagerProxy } from './app-config.js';
|
|
|
12
12
|
import { TelegramStorageProxy } from './storage.js';
|
|
13
13
|
export interface TelegramWorkerPortOptions {
|
|
14
14
|
worker: SomeWorker;
|
|
15
|
+
workerId?: string;
|
|
15
16
|
platform: ICorePlatform;
|
|
16
17
|
}
|
|
17
18
|
export declare abstract class TelegramWorkerPort<Custom extends WorkerCustomMethods> implements ITelegramClient {
|
|
@@ -46,6 +47,7 @@ export declare abstract class TelegramWorkerPort<Custom extends WorkerCustomMeth
|
|
|
46
47
|
readonly recreateDc: ITelegramClient['recreateDc'];
|
|
47
48
|
private _abortController;
|
|
48
49
|
readonly stopSignal: AbortSignal;
|
|
50
|
+
readonly workerId: string;
|
|
49
51
|
constructor(options: TelegramWorkerPortOptions);
|
|
50
52
|
call<T extends tl.RpcMethod>(message: MustEqual<T, tl.RpcMethod>, params?: RpcCallOptions): Promise<tl.RpcCallReturn[T['_']]>;
|
|
51
53
|
abstract connectToWorker(worker: SomeWorker, handler: ClientMessageHandler): [SendFn, () => void];
|
|
@@ -12,6 +12,7 @@ import { AppConfigManagerProxy } from './app-config.js';
|
|
|
12
12
|
import { TelegramStorageProxy } from './storage.js';
|
|
13
13
|
export interface TelegramWorkerPortOptions {
|
|
14
14
|
worker: SomeWorker;
|
|
15
|
+
workerId?: string;
|
|
15
16
|
platform: ICorePlatform;
|
|
16
17
|
}
|
|
17
18
|
export declare abstract class TelegramWorkerPort<Custom extends WorkerCustomMethods> implements ITelegramClient {
|
|
@@ -46,6 +47,7 @@ export declare abstract class TelegramWorkerPort<Custom extends WorkerCustomMeth
|
|
|
46
47
|
readonly recreateDc: ITelegramClient['recreateDc'];
|
|
47
48
|
private _abortController;
|
|
48
49
|
readonly stopSignal: AbortSignal;
|
|
50
|
+
readonly workerId: string;
|
|
49
51
|
constructor(options: TelegramWorkerPortOptions);
|
|
50
52
|
call<T extends tl.RpcMethod>(message: MustEqual<T, tl.RpcMethod>, params?: RpcCallOptions): Promise<tl.RpcCallReturn[T['_']]>;
|
|
51
53
|
abstract connectToWorker(worker: SomeWorker, handler: ClientMessageHandler): [SendFn, () => void];
|
package/highlevel/worker/port.js
CHANGED
|
@@ -5,15 +5,16 @@ import { PeersIndex } from "../types/peers/peers-index.js";
|
|
|
5
5
|
import { AppConfigManagerProxy } from "./app-config.js";
|
|
6
6
|
import { deserializeError } from "./errors.js";
|
|
7
7
|
import { WorkerInvoker } from "./invoker.js";
|
|
8
|
-
import { deserializeResult } from "./protocol.js";
|
|
8
|
+
import { DEFAULT_WORKER_ID, deserializeResult } from "./protocol.js";
|
|
9
9
|
import { TelegramStorageProxy } from "./storage.js";
|
|
10
10
|
class TelegramWorkerPort {
|
|
11
11
|
constructor(options) {
|
|
12
12
|
this.options = options;
|
|
13
13
|
this.log = new LogManager("worker", options.platform);
|
|
14
14
|
this.platform = options.platform;
|
|
15
|
+
this.workerId = options.workerId ?? DEFAULT_WORKER_ID;
|
|
15
16
|
this._connection = this.connectToWorker(this.options.worker, this._onMessage);
|
|
16
|
-
this._invoker = new WorkerInvoker(this._connection[0]);
|
|
17
|
+
this._invoker = new WorkerInvoker(this._connection[0], this.workerId);
|
|
17
18
|
this.storage = new TelegramStorageProxy(this._invoker);
|
|
18
19
|
this.appConfig = new AppConfigManagerProxy(this._invoker);
|
|
19
20
|
const bind = this._invoker.makeBinder("client");
|
|
@@ -75,6 +76,7 @@ class TelegramWorkerPort {
|
|
|
75
76
|
recreateDc;
|
|
76
77
|
_abortController = new AbortController();
|
|
77
78
|
stopSignal = this._abortController.signal;
|
|
79
|
+
workerId;
|
|
78
80
|
call(message, params) {
|
|
79
81
|
if (params?.abortSignal) {
|
|
80
82
|
const { abortSignal, ...rest } = params;
|
|
@@ -87,6 +89,7 @@ class TelegramWorkerPort {
|
|
|
87
89
|
onConnectionState = new Emitter();
|
|
88
90
|
onError = new Emitter();
|
|
89
91
|
_onMessage = (message) => {
|
|
92
|
+
if (message._mtcuteWorkerId !== this.workerId) return;
|
|
90
93
|
switch (message.type) {
|
|
91
94
|
case "log":
|
|
92
95
|
this.log.handler(message.color, message.level, message.tag, message.fmt, message.args);
|
|
@@ -6,6 +6,7 @@ if (typeof globalThis !== "undefined" && !globalThis._MTCUTE_CJS_DEPRECATION_WAR
|
|
|
6
6
|
"use strict";
|
|
7
7
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
8
8
|
const Long = require("long");
|
|
9
|
+
const DEFAULT_WORKER_ID = "default";
|
|
9
10
|
function serializeResult(result) {
|
|
10
11
|
if (ArrayBuffer.isView(result)) return result;
|
|
11
12
|
if (Array.isArray(result)) {
|
|
@@ -59,5 +60,6 @@ function deserializeResult(result) {
|
|
|
59
60
|
}
|
|
60
61
|
return result;
|
|
61
62
|
}
|
|
63
|
+
exports.DEFAULT_WORKER_ID = DEFAULT_WORKER_ID;
|
|
62
64
|
exports.deserializeResult = deserializeResult;
|
|
63
65
|
exports.serializeResult = serializeResult;
|
|
@@ -3,6 +3,7 @@ import { Worker as NodeWorker } from 'node:worker_threads';
|
|
|
3
3
|
import { ConnectionState } from '../client.types.js';
|
|
4
4
|
import { SerializedError } from './errors.js';
|
|
5
5
|
export type WorkerInboundMessage = {
|
|
6
|
+
_mtcuteWorkerId: string;
|
|
6
7
|
type: 'invoke';
|
|
7
8
|
id: number;
|
|
8
9
|
target: 'custom' | 'client' | 'storage' | 'storage-self' | 'storage-peers' | 'app-config';
|
|
@@ -11,27 +12,34 @@ export type WorkerInboundMessage = {
|
|
|
11
12
|
void: boolean;
|
|
12
13
|
withAbort: boolean;
|
|
13
14
|
} | {
|
|
15
|
+
_mtcuteWorkerId: string;
|
|
14
16
|
type: 'abort';
|
|
15
17
|
id: number;
|
|
16
18
|
};
|
|
17
19
|
export type WorkerOutboundMessage = {
|
|
20
|
+
_mtcuteWorkerId: string;
|
|
18
21
|
type: 'server_update';
|
|
19
22
|
update: SerializedResult<tl.TypeUpdates>;
|
|
20
23
|
} | {
|
|
24
|
+
_mtcuteWorkerId: string;
|
|
21
25
|
type: 'update';
|
|
22
26
|
update: SerializedResult<tl.TypeUpdate>;
|
|
23
27
|
users: SerializedResult<Map<number, tl.TypeUser>>;
|
|
24
28
|
chats: SerializedResult<Map<number, tl.TypeChat>>;
|
|
25
29
|
hasMin: boolean;
|
|
26
30
|
} | {
|
|
31
|
+
_mtcuteWorkerId: string;
|
|
27
32
|
type: 'error';
|
|
28
33
|
error: SerializedError;
|
|
29
34
|
} | {
|
|
35
|
+
_mtcuteWorkerId: string;
|
|
30
36
|
type: 'stop';
|
|
31
37
|
} | {
|
|
38
|
+
_mtcuteWorkerId: string;
|
|
32
39
|
type: 'conn_state';
|
|
33
40
|
state: ConnectionState;
|
|
34
41
|
} | {
|
|
42
|
+
_mtcuteWorkerId: string;
|
|
35
43
|
type: 'log';
|
|
36
44
|
color: number;
|
|
37
45
|
level: number;
|
|
@@ -39,11 +47,13 @@ export type WorkerOutboundMessage = {
|
|
|
39
47
|
fmt: string;
|
|
40
48
|
args: unknown[];
|
|
41
49
|
} | {
|
|
50
|
+
_mtcuteWorkerId: string;
|
|
42
51
|
type: 'result';
|
|
43
52
|
id: number;
|
|
44
53
|
result?: SerializedResult<unknown>;
|
|
45
54
|
error?: SerializedError;
|
|
46
55
|
};
|
|
56
|
+
export declare const DEFAULT_WORKER_ID = "default";
|
|
47
57
|
export type SomeWorker = NodeWorker | Worker | SharedWorker;
|
|
48
58
|
export type SendFn = (message: WorkerInboundMessage) => void;
|
|
49
59
|
export type ClientMessageHandler = (message: WorkerOutboundMessage) => void;
|
|
@@ -3,6 +3,7 @@ import { Worker as NodeWorker } from 'node:worker_threads';
|
|
|
3
3
|
import { ConnectionState } from '../client.types.js';
|
|
4
4
|
import { SerializedError } from './errors.js';
|
|
5
5
|
export type WorkerInboundMessage = {
|
|
6
|
+
_mtcuteWorkerId: string;
|
|
6
7
|
type: 'invoke';
|
|
7
8
|
id: number;
|
|
8
9
|
target: 'custom' | 'client' | 'storage' | 'storage-self' | 'storage-peers' | 'app-config';
|
|
@@ -11,27 +12,34 @@ export type WorkerInboundMessage = {
|
|
|
11
12
|
void: boolean;
|
|
12
13
|
withAbort: boolean;
|
|
13
14
|
} | {
|
|
15
|
+
_mtcuteWorkerId: string;
|
|
14
16
|
type: 'abort';
|
|
15
17
|
id: number;
|
|
16
18
|
};
|
|
17
19
|
export type WorkerOutboundMessage = {
|
|
20
|
+
_mtcuteWorkerId: string;
|
|
18
21
|
type: 'server_update';
|
|
19
22
|
update: SerializedResult<tl.TypeUpdates>;
|
|
20
23
|
} | {
|
|
24
|
+
_mtcuteWorkerId: string;
|
|
21
25
|
type: 'update';
|
|
22
26
|
update: SerializedResult<tl.TypeUpdate>;
|
|
23
27
|
users: SerializedResult<Map<number, tl.TypeUser>>;
|
|
24
28
|
chats: SerializedResult<Map<number, tl.TypeChat>>;
|
|
25
29
|
hasMin: boolean;
|
|
26
30
|
} | {
|
|
31
|
+
_mtcuteWorkerId: string;
|
|
27
32
|
type: 'error';
|
|
28
33
|
error: SerializedError;
|
|
29
34
|
} | {
|
|
35
|
+
_mtcuteWorkerId: string;
|
|
30
36
|
type: 'stop';
|
|
31
37
|
} | {
|
|
38
|
+
_mtcuteWorkerId: string;
|
|
32
39
|
type: 'conn_state';
|
|
33
40
|
state: ConnectionState;
|
|
34
41
|
} | {
|
|
42
|
+
_mtcuteWorkerId: string;
|
|
35
43
|
type: 'log';
|
|
36
44
|
color: number;
|
|
37
45
|
level: number;
|
|
@@ -39,11 +47,13 @@ export type WorkerOutboundMessage = {
|
|
|
39
47
|
fmt: string;
|
|
40
48
|
args: unknown[];
|
|
41
49
|
} | {
|
|
50
|
+
_mtcuteWorkerId: string;
|
|
42
51
|
type: 'result';
|
|
43
52
|
id: number;
|
|
44
53
|
result?: SerializedResult<unknown>;
|
|
45
54
|
error?: SerializedError;
|
|
46
55
|
};
|
|
56
|
+
export declare const DEFAULT_WORKER_ID = "default";
|
|
47
57
|
export type SomeWorker = NodeWorker | Worker | SharedWorker;
|
|
48
58
|
export type SendFn = (message: WorkerInboundMessage) => void;
|
|
49
59
|
export type ClientMessageHandler = (message: WorkerOutboundMessage) => void;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import Long from "long";
|
|
2
|
+
const DEFAULT_WORKER_ID = "default";
|
|
2
3
|
function serializeResult(result) {
|
|
3
4
|
if (ArrayBuffer.isView(result)) return result;
|
|
4
5
|
if (Array.isArray(result)) {
|
|
@@ -53,6 +54,7 @@ function deserializeResult(result) {
|
|
|
53
54
|
return result;
|
|
54
55
|
}
|
|
55
56
|
export {
|
|
57
|
+
DEFAULT_WORKER_ID,
|
|
56
58
|
deserializeResult,
|
|
57
59
|
serializeResult
|
|
58
60
|
};
|
|
@@ -10,7 +10,9 @@ const protocol = require("./protocol.cjs");
|
|
|
10
10
|
class TelegramWorker {
|
|
11
11
|
constructor(params) {
|
|
12
12
|
this.params = params;
|
|
13
|
-
this.
|
|
13
|
+
this.workerId = params.workerId ?? protocol.DEFAULT_WORKER_ID;
|
|
14
|
+
const [broadcast, cleanup] = this.registerWorker((message, respond) => {
|
|
15
|
+
if (message._mtcuteWorkerId !== this.workerId) return;
|
|
14
16
|
switch (message.type) {
|
|
15
17
|
case "invoke":
|
|
16
18
|
this.onInvoke(message, respond);
|
|
@@ -24,9 +26,12 @@ class TelegramWorker {
|
|
|
24
26
|
}
|
|
25
27
|
}
|
|
26
28
|
});
|
|
29
|
+
this.broadcast = broadcast;
|
|
30
|
+
this._cleanup = cleanup;
|
|
27
31
|
const client = params.client;
|
|
28
32
|
this.client = client;
|
|
29
33
|
client.log.mgr.handler = (color, level, tag, fmt, args) => this.broadcast({
|
|
34
|
+
_mtcuteWorkerId: this.workerId,
|
|
30
35
|
type: "log",
|
|
31
36
|
color,
|
|
32
37
|
level,
|
|
@@ -36,20 +41,26 @@ class TelegramWorker {
|
|
|
36
41
|
});
|
|
37
42
|
client.onError.add(
|
|
38
43
|
(err) => this.broadcast({
|
|
44
|
+
_mtcuteWorkerId: this.workerId,
|
|
39
45
|
type: "error",
|
|
40
46
|
error: errors.serializeError(err)
|
|
41
47
|
})
|
|
42
48
|
);
|
|
43
49
|
client.onConnectionState.add(
|
|
44
50
|
(state) => this.broadcast({
|
|
51
|
+
_mtcuteWorkerId: this.workerId,
|
|
45
52
|
type: "conn_state",
|
|
46
53
|
state
|
|
47
54
|
})
|
|
48
55
|
);
|
|
49
|
-
client.stopSignal.addEventListener("abort", () => this.broadcast({
|
|
56
|
+
client.stopSignal.addEventListener("abort", () => this.broadcast({
|
|
57
|
+
_mtcuteWorkerId: this.workerId,
|
|
58
|
+
type: "stop"
|
|
59
|
+
}));
|
|
50
60
|
if (client.updates) {
|
|
51
61
|
client.onRawUpdate.add(
|
|
52
62
|
({ update, peers }) => this.broadcast({
|
|
63
|
+
_mtcuteWorkerId: this.workerId,
|
|
53
64
|
type: "update",
|
|
54
65
|
update: protocol.serializeResult(update),
|
|
55
66
|
users: protocol.serializeResult(peers.users),
|
|
@@ -60,6 +71,7 @@ class TelegramWorker {
|
|
|
60
71
|
} else {
|
|
61
72
|
client.onServerUpdate.add(
|
|
62
73
|
(update) => this.broadcast({
|
|
74
|
+
_mtcuteWorkerId: this.workerId,
|
|
63
75
|
type: "server_update",
|
|
64
76
|
update: protocol.serializeResult(update)
|
|
65
77
|
})
|
|
@@ -68,7 +80,9 @@ class TelegramWorker {
|
|
|
68
80
|
}
|
|
69
81
|
client;
|
|
70
82
|
broadcast;
|
|
83
|
+
_cleanup;
|
|
71
84
|
pendingAborts = /* @__PURE__ */ new Map();
|
|
85
|
+
workerId;
|
|
72
86
|
onInvoke(msg, respond) {
|
|
73
87
|
let target;
|
|
74
88
|
switch (msg.target) {
|
|
@@ -92,6 +106,7 @@ class TelegramWorker {
|
|
|
92
106
|
break;
|
|
93
107
|
default: {
|
|
94
108
|
respond({
|
|
109
|
+
_mtcuteWorkerId: this.workerId,
|
|
95
110
|
type: "result",
|
|
96
111
|
id: msg.id,
|
|
97
112
|
error: new Error(`Unknown target ${msg.target}`)
|
|
@@ -102,6 +117,7 @@ class TelegramWorker {
|
|
|
102
117
|
const method = target[msg.method];
|
|
103
118
|
if (!method) {
|
|
104
119
|
respond({
|
|
120
|
+
_mtcuteWorkerId: this.workerId,
|
|
105
121
|
type: "result",
|
|
106
122
|
id: msg.id,
|
|
107
123
|
error: new Error(`Method ${msg.method} not found on ${msg.target}`)
|
|
@@ -128,6 +144,7 @@ class TelegramWorker {
|
|
|
128
144
|
}
|
|
129
145
|
if (msg.void) return;
|
|
130
146
|
respond({
|
|
147
|
+
_mtcuteWorkerId: this.workerId,
|
|
131
148
|
type: "result",
|
|
132
149
|
id: msg.id,
|
|
133
150
|
result: protocol.serializeResult(res)
|
|
@@ -137,11 +154,15 @@ class TelegramWorker {
|
|
|
137
154
|
this.pendingAborts.delete(msg.id);
|
|
138
155
|
}
|
|
139
156
|
respond({
|
|
157
|
+
_mtcuteWorkerId: this.workerId,
|
|
140
158
|
type: "result",
|
|
141
159
|
id: msg.id,
|
|
142
160
|
error: errors.serializeError(err)
|
|
143
161
|
});
|
|
144
162
|
});
|
|
145
163
|
}
|
|
164
|
+
destroy() {
|
|
165
|
+
this._cleanup?.();
|
|
166
|
+
}
|
|
146
167
|
}
|
|
147
168
|
exports.TelegramWorker = TelegramWorker;
|
|
@@ -2,14 +2,18 @@ import { BaseTelegramClient } from '../base.js';
|
|
|
2
2
|
import { RespondFn, WorkerCustomMethods, WorkerMessageHandler } from './protocol.js';
|
|
3
3
|
export interface TelegramWorkerOptions<T extends WorkerCustomMethods> {
|
|
4
4
|
client: BaseTelegramClient;
|
|
5
|
+
workerId?: string;
|
|
5
6
|
customMethods?: T;
|
|
6
7
|
}
|
|
7
8
|
export declare abstract class TelegramWorker<T extends WorkerCustomMethods> {
|
|
8
9
|
readonly params: TelegramWorkerOptions<T>;
|
|
9
10
|
readonly client: BaseTelegramClient;
|
|
10
11
|
readonly broadcast: RespondFn;
|
|
11
|
-
|
|
12
|
+
private _cleanup;
|
|
13
|
+
abstract registerWorker(handler: WorkerMessageHandler): [RespondFn, VoidFunction];
|
|
12
14
|
readonly pendingAborts: Map<number, AbortController>;
|
|
15
|
+
readonly workerId: string;
|
|
13
16
|
constructor(params: TelegramWorkerOptions<T>);
|
|
14
17
|
private onInvoke;
|
|
18
|
+
destroy(): void;
|
|
15
19
|
}
|
|
@@ -2,14 +2,18 @@ import { BaseTelegramClient } from '../base.js';
|
|
|
2
2
|
import { RespondFn, WorkerCustomMethods, WorkerMessageHandler } from './protocol.js';
|
|
3
3
|
export interface TelegramWorkerOptions<T extends WorkerCustomMethods> {
|
|
4
4
|
client: BaseTelegramClient;
|
|
5
|
+
workerId?: string;
|
|
5
6
|
customMethods?: T;
|
|
6
7
|
}
|
|
7
8
|
export declare abstract class TelegramWorker<T extends WorkerCustomMethods> {
|
|
8
9
|
readonly params: TelegramWorkerOptions<T>;
|
|
9
10
|
readonly client: BaseTelegramClient;
|
|
10
11
|
readonly broadcast: RespondFn;
|
|
11
|
-
|
|
12
|
+
private _cleanup;
|
|
13
|
+
abstract registerWorker(handler: WorkerMessageHandler): [RespondFn, VoidFunction];
|
|
12
14
|
readonly pendingAborts: Map<number, AbortController>;
|
|
15
|
+
readonly workerId: string;
|
|
13
16
|
constructor(params: TelegramWorkerOptions<T>);
|
|
14
17
|
private onInvoke;
|
|
18
|
+
destroy(): void;
|
|
15
19
|
}
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { serializeError } from "./errors.js";
|
|
2
|
-
import { serializeResult, deserializeResult } from "./protocol.js";
|
|
2
|
+
import { DEFAULT_WORKER_ID, serializeResult, deserializeResult } from "./protocol.js";
|
|
3
3
|
class TelegramWorker {
|
|
4
4
|
constructor(params) {
|
|
5
5
|
this.params = params;
|
|
6
|
-
this.
|
|
6
|
+
this.workerId = params.workerId ?? DEFAULT_WORKER_ID;
|
|
7
|
+
const [broadcast, cleanup] = this.registerWorker((message, respond) => {
|
|
8
|
+
if (message._mtcuteWorkerId !== this.workerId) return;
|
|
7
9
|
switch (message.type) {
|
|
8
10
|
case "invoke":
|
|
9
11
|
this.onInvoke(message, respond);
|
|
@@ -17,9 +19,12 @@ class TelegramWorker {
|
|
|
17
19
|
}
|
|
18
20
|
}
|
|
19
21
|
});
|
|
22
|
+
this.broadcast = broadcast;
|
|
23
|
+
this._cleanup = cleanup;
|
|
20
24
|
const client = params.client;
|
|
21
25
|
this.client = client;
|
|
22
26
|
client.log.mgr.handler = (color, level, tag, fmt, args) => this.broadcast({
|
|
27
|
+
_mtcuteWorkerId: this.workerId,
|
|
23
28
|
type: "log",
|
|
24
29
|
color,
|
|
25
30
|
level,
|
|
@@ -29,20 +34,26 @@ class TelegramWorker {
|
|
|
29
34
|
});
|
|
30
35
|
client.onError.add(
|
|
31
36
|
(err) => this.broadcast({
|
|
37
|
+
_mtcuteWorkerId: this.workerId,
|
|
32
38
|
type: "error",
|
|
33
39
|
error: serializeError(err)
|
|
34
40
|
})
|
|
35
41
|
);
|
|
36
42
|
client.onConnectionState.add(
|
|
37
43
|
(state) => this.broadcast({
|
|
44
|
+
_mtcuteWorkerId: this.workerId,
|
|
38
45
|
type: "conn_state",
|
|
39
46
|
state
|
|
40
47
|
})
|
|
41
48
|
);
|
|
42
|
-
client.stopSignal.addEventListener("abort", () => this.broadcast({
|
|
49
|
+
client.stopSignal.addEventListener("abort", () => this.broadcast({
|
|
50
|
+
_mtcuteWorkerId: this.workerId,
|
|
51
|
+
type: "stop"
|
|
52
|
+
}));
|
|
43
53
|
if (client.updates) {
|
|
44
54
|
client.onRawUpdate.add(
|
|
45
55
|
({ update, peers }) => this.broadcast({
|
|
56
|
+
_mtcuteWorkerId: this.workerId,
|
|
46
57
|
type: "update",
|
|
47
58
|
update: serializeResult(update),
|
|
48
59
|
users: serializeResult(peers.users),
|
|
@@ -53,6 +64,7 @@ class TelegramWorker {
|
|
|
53
64
|
} else {
|
|
54
65
|
client.onServerUpdate.add(
|
|
55
66
|
(update) => this.broadcast({
|
|
67
|
+
_mtcuteWorkerId: this.workerId,
|
|
56
68
|
type: "server_update",
|
|
57
69
|
update: serializeResult(update)
|
|
58
70
|
})
|
|
@@ -61,7 +73,9 @@ class TelegramWorker {
|
|
|
61
73
|
}
|
|
62
74
|
client;
|
|
63
75
|
broadcast;
|
|
76
|
+
_cleanup;
|
|
64
77
|
pendingAborts = /* @__PURE__ */ new Map();
|
|
78
|
+
workerId;
|
|
65
79
|
onInvoke(msg, respond) {
|
|
66
80
|
let target;
|
|
67
81
|
switch (msg.target) {
|
|
@@ -85,6 +99,7 @@ class TelegramWorker {
|
|
|
85
99
|
break;
|
|
86
100
|
default: {
|
|
87
101
|
respond({
|
|
102
|
+
_mtcuteWorkerId: this.workerId,
|
|
88
103
|
type: "result",
|
|
89
104
|
id: msg.id,
|
|
90
105
|
error: new Error(`Unknown target ${msg.target}`)
|
|
@@ -95,6 +110,7 @@ class TelegramWorker {
|
|
|
95
110
|
const method = target[msg.method];
|
|
96
111
|
if (!method) {
|
|
97
112
|
respond({
|
|
113
|
+
_mtcuteWorkerId: this.workerId,
|
|
98
114
|
type: "result",
|
|
99
115
|
id: msg.id,
|
|
100
116
|
error: new Error(`Method ${msg.method} not found on ${msg.target}`)
|
|
@@ -121,6 +137,7 @@ class TelegramWorker {
|
|
|
121
137
|
}
|
|
122
138
|
if (msg.void) return;
|
|
123
139
|
respond({
|
|
140
|
+
_mtcuteWorkerId: this.workerId,
|
|
124
141
|
type: "result",
|
|
125
142
|
id: msg.id,
|
|
126
143
|
result: serializeResult(res)
|
|
@@ -130,12 +147,16 @@ class TelegramWorker {
|
|
|
130
147
|
this.pendingAborts.delete(msg.id);
|
|
131
148
|
}
|
|
132
149
|
respond({
|
|
150
|
+
_mtcuteWorkerId: this.workerId,
|
|
133
151
|
type: "result",
|
|
134
152
|
id: msg.id,
|
|
135
153
|
error: serializeError(err)
|
|
136
154
|
});
|
|
137
155
|
});
|
|
138
156
|
}
|
|
157
|
+
destroy() {
|
|
158
|
+
this._cleanup?.();
|
|
159
|
+
}
|
|
139
160
|
}
|
|
140
161
|
export {
|
|
141
162
|
TelegramWorker
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mtcute/core",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.25.
|
|
4
|
+
"version": "0.25.3",
|
|
5
5
|
"description": "Type-safe library for MTProto (Telegram API)",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"scripts": {},
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
"@fuman/net": "0.0.15",
|
|
11
11
|
"@fuman/utils": "0.0.15",
|
|
12
12
|
"@mtcute/file-id": "^0.24.3",
|
|
13
|
-
"@mtcute/tl": "^
|
|
13
|
+
"@mtcute/tl": "^208.0.0",
|
|
14
14
|
"@mtcute/tl-runtime": "^0.24.3",
|
|
15
15
|
"@types/events": "3.0.0",
|
|
16
16
|
"long": "5.2.3"
|
package/utils/binary/compat.cjs
CHANGED
|
@@ -31,8 +31,10 @@ function mapCompatStarGift(obj) {
|
|
|
31
31
|
};
|
|
32
32
|
case "starGiftUnique_layer198":
|
|
33
33
|
case "starGiftUnique_layer202":
|
|
34
|
+
case "starGiftUnique_layer206":
|
|
34
35
|
return replaceType(obj, "starGiftUnique");
|
|
35
36
|
case "starGift_layer202":
|
|
37
|
+
case "starGift_layer206":
|
|
36
38
|
return replaceType(obj, "starGift");
|
|
37
39
|
default:
|
|
38
40
|
return obj;
|
|
@@ -79,6 +81,18 @@ function mapCompatMessageAction(obj) {
|
|
|
79
81
|
return obj;
|
|
80
82
|
}
|
|
81
83
|
}
|
|
84
|
+
function mapMessageReplyHeader(obj) {
|
|
85
|
+
switch (obj._) {
|
|
86
|
+
case "messageReplyHeader_layer206":
|
|
87
|
+
return {
|
|
88
|
+
...obj,
|
|
89
|
+
_: "messageReplyHeader",
|
|
90
|
+
replyMedia: obj.replyMedia ? mapCompatMessageMedia(obj.replyMedia) : void 0
|
|
91
|
+
};
|
|
92
|
+
default:
|
|
93
|
+
return obj;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
82
96
|
function mapCompatMessage(obj) {
|
|
83
97
|
switch (obj._) {
|
|
84
98
|
case "message_layer199":
|
|
@@ -86,13 +100,15 @@ function mapCompatMessage(obj) {
|
|
|
86
100
|
return {
|
|
87
101
|
...obj,
|
|
88
102
|
_: "message",
|
|
89
|
-
media: obj.media ? mapCompatMessageMedia(obj.media) : void 0
|
|
103
|
+
media: obj.media ? mapCompatMessageMedia(obj.media) : void 0,
|
|
104
|
+
replyTo: obj.replyTo ? mapMessageReplyHeader(obj.replyTo) : void 0
|
|
90
105
|
};
|
|
91
106
|
case "messageService_layer204":
|
|
92
107
|
return {
|
|
93
108
|
...obj,
|
|
94
109
|
_: "messageService",
|
|
95
|
-
action: mapCompatMessageAction(obj.action)
|
|
110
|
+
action: mapCompatMessageAction(obj.action),
|
|
111
|
+
replyTo: obj.replyTo ? mapMessageReplyHeader(obj.replyTo) : void 0
|
|
96
112
|
};
|
|
97
113
|
default:
|
|
98
114
|
return obj;
|
|
@@ -103,7 +119,9 @@ function mapCompatObject(obj) {
|
|
|
103
119
|
case "starGiftUnique_layer197":
|
|
104
120
|
case "starGiftUnique_layer198":
|
|
105
121
|
case "starGiftUnique_layer202":
|
|
122
|
+
case "starGiftUnique_layer206":
|
|
106
123
|
case "starGift_layer202":
|
|
124
|
+
case "starGift_layer206":
|
|
107
125
|
return mapCompatStarGift(obj);
|
|
108
126
|
case "emojiStatus_layer197":
|
|
109
127
|
return mapCompatEmojiStatus(obj);
|
|
@@ -140,6 +158,8 @@ function mapCompatObject(obj) {
|
|
|
140
158
|
case "message_layer204":
|
|
141
159
|
case "messageService_layer204":
|
|
142
160
|
return mapCompatMessage(obj);
|
|
161
|
+
case "messageReplyHeader_layer206":
|
|
162
|
+
return mapMessageReplyHeader(obj);
|
|
143
163
|
default:
|
|
144
164
|
return obj;
|
|
145
165
|
}
|
package/utils/binary/compat.js
CHANGED
|
@@ -24,8 +24,10 @@ function mapCompatStarGift(obj) {
|
|
|
24
24
|
};
|
|
25
25
|
case "starGiftUnique_layer198":
|
|
26
26
|
case "starGiftUnique_layer202":
|
|
27
|
+
case "starGiftUnique_layer206":
|
|
27
28
|
return replaceType(obj, "starGiftUnique");
|
|
28
29
|
case "starGift_layer202":
|
|
30
|
+
case "starGift_layer206":
|
|
29
31
|
return replaceType(obj, "starGift");
|
|
30
32
|
default:
|
|
31
33
|
return obj;
|
|
@@ -72,6 +74,18 @@ function mapCompatMessageAction(obj) {
|
|
|
72
74
|
return obj;
|
|
73
75
|
}
|
|
74
76
|
}
|
|
77
|
+
function mapMessageReplyHeader(obj) {
|
|
78
|
+
switch (obj._) {
|
|
79
|
+
case "messageReplyHeader_layer206":
|
|
80
|
+
return {
|
|
81
|
+
...obj,
|
|
82
|
+
_: "messageReplyHeader",
|
|
83
|
+
replyMedia: obj.replyMedia ? mapCompatMessageMedia(obj.replyMedia) : void 0
|
|
84
|
+
};
|
|
85
|
+
default:
|
|
86
|
+
return obj;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
75
89
|
function mapCompatMessage(obj) {
|
|
76
90
|
switch (obj._) {
|
|
77
91
|
case "message_layer199":
|
|
@@ -79,13 +93,15 @@ function mapCompatMessage(obj) {
|
|
|
79
93
|
return {
|
|
80
94
|
...obj,
|
|
81
95
|
_: "message",
|
|
82
|
-
media: obj.media ? mapCompatMessageMedia(obj.media) : void 0
|
|
96
|
+
media: obj.media ? mapCompatMessageMedia(obj.media) : void 0,
|
|
97
|
+
replyTo: obj.replyTo ? mapMessageReplyHeader(obj.replyTo) : void 0
|
|
83
98
|
};
|
|
84
99
|
case "messageService_layer204":
|
|
85
100
|
return {
|
|
86
101
|
...obj,
|
|
87
102
|
_: "messageService",
|
|
88
|
-
action: mapCompatMessageAction(obj.action)
|
|
103
|
+
action: mapCompatMessageAction(obj.action),
|
|
104
|
+
replyTo: obj.replyTo ? mapMessageReplyHeader(obj.replyTo) : void 0
|
|
89
105
|
};
|
|
90
106
|
default:
|
|
91
107
|
return obj;
|
|
@@ -96,7 +112,9 @@ function mapCompatObject(obj) {
|
|
|
96
112
|
case "starGiftUnique_layer197":
|
|
97
113
|
case "starGiftUnique_layer198":
|
|
98
114
|
case "starGiftUnique_layer202":
|
|
115
|
+
case "starGiftUnique_layer206":
|
|
99
116
|
case "starGift_layer202":
|
|
117
|
+
case "starGift_layer206":
|
|
100
118
|
return mapCompatStarGift(obj);
|
|
101
119
|
case "emojiStatus_layer197":
|
|
102
120
|
return mapCompatEmojiStatus(obj);
|
|
@@ -133,6 +151,8 @@ function mapCompatObject(obj) {
|
|
|
133
151
|
case "message_layer204":
|
|
134
152
|
case "messageService_layer204":
|
|
135
153
|
return mapCompatMessage(obj);
|
|
154
|
+
case "messageReplyHeader_layer206":
|
|
155
|
+
return mapMessageReplyHeader(obj);
|
|
136
156
|
default:
|
|
137
157
|
return obj;
|
|
138
158
|
}
|
package/worker.cjs
CHANGED
|
@@ -9,6 +9,7 @@ const port = require("./highlevel/worker/port.cjs");
|
|
|
9
9
|
const protocol = require("./highlevel/worker/protocol.cjs");
|
|
10
10
|
const worker = require("./highlevel/worker/worker.cjs");
|
|
11
11
|
exports.TelegramWorkerPort = port.TelegramWorkerPort;
|
|
12
|
+
exports.DEFAULT_WORKER_ID = protocol.DEFAULT_WORKER_ID;
|
|
12
13
|
exports.deserializeResult = protocol.deserializeResult;
|
|
13
14
|
exports.serializeResult = protocol.serializeResult;
|
|
14
15
|
exports.TelegramWorker = worker.TelegramWorker;
|
package/worker.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { TelegramWorkerPort } from "./highlevel/worker/port.js";
|
|
2
|
-
import { deserializeResult, serializeResult } from "./highlevel/worker/protocol.js";
|
|
2
|
+
import { DEFAULT_WORKER_ID, deserializeResult, serializeResult } from "./highlevel/worker/protocol.js";
|
|
3
3
|
import { TelegramWorker } from "./highlevel/worker/worker.js";
|
|
4
4
|
export {
|
|
5
|
+
DEFAULT_WORKER_ID,
|
|
5
6
|
TelegramWorker,
|
|
6
7
|
TelegramWorkerPort,
|
|
7
8
|
deserializeResult,
|