@mtcute/test 0.1.1
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/LICENSE +8 -0
- package/README.md +5 -0
- package/cjs/client.d.ts +43 -0
- package/cjs/client.js +217 -0
- package/cjs/client.js.map +1 -0
- package/cjs/index.d.ts +5 -0
- package/cjs/index.js +22 -0
- package/cjs/index.js.map +1 -0
- package/cjs/package.json +3 -0
- package/cjs/schema.d.ts +1 -0
- package/cjs/schema.js +32 -0
- package/cjs/schema.js.map +1 -0
- package/cjs/storage.d.ts +50 -0
- package/cjs/storage.js +58 -0
- package/cjs/storage.js.map +1 -0
- package/cjs/stub.d.ts +2 -0
- package/cjs/stub.js +71 -0
- package/cjs/stub.js.map +1 -0
- package/cjs/transport.d.ts +30 -0
- package/cjs/transport.js +52 -0
- package/cjs/transport.js.map +1 -0
- package/cjs/types.d.ts +8 -0
- package/cjs/types.js +3 -0
- package/cjs/types.js.map +1 -0
- package/cjs/utils.d.ts +2 -0
- package/cjs/utils.js +18 -0
- package/cjs/utils.js.map +1 -0
- package/esm/client.d.ts +43 -0
- package/esm/client.js +213 -0
- package/esm/client.js.map +1 -0
- package/esm/index.d.ts +5 -0
- package/esm/index.js +6 -0
- package/esm/index.js.map +1 -0
- package/esm/schema.d.ts +1 -0
- package/esm/schema.js +28 -0
- package/esm/schema.js.map +1 -0
- package/esm/storage.d.ts +50 -0
- package/esm/storage.js +54 -0
- package/esm/storage.js.map +1 -0
- package/esm/stub.d.ts +2 -0
- package/esm/stub.js +64 -0
- package/esm/stub.js.map +1 -0
- package/esm/transport.d.ts +30 -0
- package/esm/transport.js +45 -0
- package/esm/transport.js.map +1 -0
- package/esm/types.d.ts +8 -0
- package/esm/types.js +2 -0
- package/esm/types.js.map +1 -0
- package/esm/utils.d.ts +2 -0
- package/esm/utils.js +14 -0
- package/esm/utils.js.map +1 -0
- package/package.json +24 -0
package/cjs/transport.js
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.StubTelegramTransport = void 0;
|
|
7
|
+
const events_1 = __importDefault(require("events"));
|
|
8
|
+
const core_1 = require("@mtcute/core");
|
|
9
|
+
class StubTelegramTransport extends events_1.default {
|
|
10
|
+
constructor(params) {
|
|
11
|
+
super();
|
|
12
|
+
this.params = params;
|
|
13
|
+
this._state = core_1.TransportState.Idle;
|
|
14
|
+
this._currentDc = null;
|
|
15
|
+
if (params.getMtproxyInfo) {
|
|
16
|
+
this.getMtproxyInfo = params.getMtproxyInfo;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
write(data) {
|
|
20
|
+
this.emit('message', data);
|
|
21
|
+
}
|
|
22
|
+
setup(crypto, log) {
|
|
23
|
+
this._crypto = crypto;
|
|
24
|
+
this._log = log;
|
|
25
|
+
}
|
|
26
|
+
state() {
|
|
27
|
+
return this._state;
|
|
28
|
+
}
|
|
29
|
+
currentDc() {
|
|
30
|
+
return this._currentDc;
|
|
31
|
+
}
|
|
32
|
+
connect(dc, testMode) {
|
|
33
|
+
this._currentDc = dc;
|
|
34
|
+
this._state = core_1.TransportState.Ready;
|
|
35
|
+
this.emit('ready');
|
|
36
|
+
this._log.debug('stubbing connection to %s:%d', dc.ipAddress, dc.port);
|
|
37
|
+
this.params.onConnect?.(dc, testMode);
|
|
38
|
+
}
|
|
39
|
+
close() {
|
|
40
|
+
this._currentDc = null;
|
|
41
|
+
this._state = core_1.TransportState.Idle;
|
|
42
|
+
this.emit('close');
|
|
43
|
+
this._log.debug('stub connection closed');
|
|
44
|
+
this.params.onClose?.();
|
|
45
|
+
}
|
|
46
|
+
// eslint-disable-next-line @typescript-eslint/require-await
|
|
47
|
+
async send(data) {
|
|
48
|
+
this.params.onMessage?.(data);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
exports.StubTelegramTransport = StubTelegramTransport;
|
|
52
|
+
//# sourceMappingURL=transport.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"transport.js","sourceRoot":"","sources":["../../src/transport.ts"],"names":[],"mappings":";;;;;;AAAA,oDAAiC;AAEjC,uCAAiE;AAIjE,MAAa,qBAAsB,SAAQ,gBAAY;IACnD,YACa,MAKR;QAED,KAAK,EAAE,CAAA;QAPE,WAAM,GAAN,MAAM,CAKd;QASL,WAAM,GAAG,qBAAc,CAAC,IAAI,CAAA;QAC5B,eAAU,GAA0B,IAAI,CAAA;QANpC,IAAI,MAAM,CAAC,cAAc,EAAE;YACtB,IAA2B,CAAC,cAAc,GAAG,MAAM,CAAC,cAAc,CAAA;SACtE;IACL,CAAC;IAOD,KAAK,CAAC,IAAgB;QAClB,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,IAAI,CAAC,CAAA;IAC9B,CAAC;IAED,KAAK,CAAC,MAAuB,EAAE,GAAW;QACtC,IAAI,CAAC,OAAO,GAAG,MAAM,CAAA;QACrB,IAAI,CAAC,IAAI,GAAG,GAAG,CAAA;IACnB,CAAC;IAED,KAAK;QACD,OAAO,IAAI,CAAC,MAAM,CAAA;IACtB,CAAC;IAED,SAAS;QACL,OAAO,IAAI,CAAC,UAAU,CAAA;IAC1B,CAAC;IAED,OAAO,CAAC,EAAkB,EAAE,QAAiB;QACzC,IAAI,CAAC,UAAU,GAAG,EAAE,CAAA;QACpB,IAAI,CAAC,MAAM,GAAG,qBAAc,CAAC,KAAK,CAAA;QAClC,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,CAAA;QAClB,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,8BAA8B,EAAE,EAAE,CAAC,SAAS,EAAE,EAAE,CAAC,IAAI,CAAC,CAAA;QAEtE,IAAI,CAAC,MAAM,CAAC,SAAS,EAAE,CAAC,EAAE,EAAE,QAAQ,CAAC,CAAA;IACzC,CAAC;IAED,KAAK;QACD,IAAI,CAAC,UAAU,GAAG,IAAI,CAAA;QACtB,IAAI,CAAC,MAAM,GAAG,qBAAc,CAAC,IAAI,CAAA;QACjC,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,CAAA;QAClB,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,wBAAwB,CAAC,CAAA;QAEzC,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,EAAE,CAAA;IAC3B,CAAC;IAED,4DAA4D;IAC5D,KAAK,CAAC,IAAI,CAAC,IAAgB;QACvB,IAAI,CAAC,MAAM,CAAC,SAAS,EAAE,CAAC,IAAI,CAAC,CAAA;IACjC,CAAC;CACJ;AA5DD,sDA4DC","sourcesContent":["import EventEmitter from 'events'\n\nimport { ITelegramTransport, TransportState } from '@mtcute/core'\nimport { ICryptoProvider, Logger } from '@mtcute/core/utils.js'\nimport { tl } from '@mtcute/tl'\n\nexport class StubTelegramTransport extends EventEmitter implements ITelegramTransport {\n constructor(\n readonly params: {\n getMtproxyInfo?: () => tl.RawInputClientProxy\n onConnect?: (dc: tl.RawDcOption, testMode: boolean) => void\n onClose?: () => void\n onMessage?: (msg: Uint8Array) => void\n },\n ) {\n super()\n\n if (params.getMtproxyInfo) {\n (this as ITelegramTransport).getMtproxyInfo = params.getMtproxyInfo\n }\n }\n\n _state = TransportState.Idle\n _currentDc: tl.RawDcOption | null = null\n _crypto!: ICryptoProvider\n _log!: Logger\n\n write(data: Uint8Array): void {\n this.emit('message', data)\n }\n\n setup(crypto: ICryptoProvider, log: Logger): void {\n this._crypto = crypto\n this._log = log\n }\n\n state(): TransportState {\n return this._state\n }\n\n currentDc(): tl.RawDcOption | null {\n return this._currentDc\n }\n\n connect(dc: tl.RawDcOption, testMode: boolean): void {\n this._currentDc = dc\n this._state = TransportState.Ready\n this.emit('ready')\n this._log.debug('stubbing connection to %s:%d', dc.ipAddress, dc.port)\n\n this.params.onConnect?.(dc, testMode)\n }\n\n close(): void {\n this._currentDc = null\n this._state = TransportState.Idle\n this.emit('close')\n this._log.debug('stub connection closed')\n\n this.params.onClose?.()\n }\n\n // eslint-disable-next-line @typescript-eslint/require-await\n async send(data: Uint8Array): Promise<void> {\n this.params.onMessage?.(data)\n }\n}\n"]}
|
package/cjs/types.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { tl } from '@mtcute/tl';
|
|
2
|
+
import type { StubTelegramClient } from './client.js';
|
|
3
|
+
export type Responder<Method extends tl.RpcMethod['_']> = [
|
|
4
|
+
Method,
|
|
5
|
+
(req: tl.FindByName<tl.RpcMethod, Method>) => tl.RpcCallReturn[Method]
|
|
6
|
+
];
|
|
7
|
+
export type ResponderFactory<Method extends tl.RpcMethod['_']> = (client: StubTelegramClient) => Responder<Method>;
|
|
8
|
+
export type InputResponder<Method extends tl.RpcMethod['_']> = Responder<Method> | ResponderFactory<Method> | InputResponder<Method>[];
|
package/cjs/types.js
ADDED
package/cjs/types.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"","sourcesContent":["import { tl } from '@mtcute/tl'\n\nimport type { StubTelegramClient } from './client.js'\n\nexport type Responder<Method extends tl.RpcMethod['_']> = [\n Method,\n (req: tl.FindByName<tl.RpcMethod, Method>) => tl.RpcCallReturn[Method],\n]\nexport type ResponderFactory<Method extends tl.RpcMethod['_']> = (client: StubTelegramClient) => Responder<Method>\nexport type InputResponder<Method extends tl.RpcMethod['_']> =\n | Responder<Method>\n | ResponderFactory<Method>\n | InputResponder<Method>[]\n"]}
|
package/cjs/utils.d.ts
ADDED
package/cjs/utils.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.markedIdToPeer = void 0;
|
|
4
|
+
const core_1 = require("@mtcute/core");
|
|
5
|
+
function markedIdToPeer(id) {
|
|
6
|
+
const type = (0, core_1.getBasicPeerType)(id);
|
|
7
|
+
const bareId = (0, core_1.markedPeerIdToBare)(id);
|
|
8
|
+
switch (type) {
|
|
9
|
+
case 'user':
|
|
10
|
+
return { _: 'peerUser', userId: bareId };
|
|
11
|
+
case 'chat':
|
|
12
|
+
return { _: 'peerChat', chatId: bareId };
|
|
13
|
+
case 'channel':
|
|
14
|
+
return { _: 'peerChannel', channelId: bareId };
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
exports.markedIdToPeer = markedIdToPeer;
|
|
18
|
+
//# sourceMappingURL=utils.js.map
|
package/cjs/utils.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":";;;AAAA,uCAAuE;AAEvE,SAAgB,cAAc,CAAC,EAAU;IACrC,MAAM,IAAI,GAAG,IAAA,uBAAgB,EAAC,EAAE,CAAC,CAAA;IACjC,MAAM,MAAM,GAAG,IAAA,yBAAkB,EAAC,EAAE,CAAC,CAAA;IAErC,QAAQ,IAAI,EAAE;QACV,KAAK,MAAM;YACP,OAAO,EAAE,CAAC,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,EAAE,CAAA;QAC5C,KAAK,MAAM;YACP,OAAO,EAAE,CAAC,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,EAAE,CAAA;QAC5C,KAAK,SAAS;YACV,OAAO,EAAE,CAAC,EAAE,aAAa,EAAE,SAAS,EAAE,MAAM,EAAE,CAAA;KACrD;AACL,CAAC;AAZD,wCAYC","sourcesContent":["import { getBasicPeerType, markedPeerIdToBare, tl } from '@mtcute/core'\n\nexport function markedIdToPeer(id: number): tl.TypePeer {\n const type = getBasicPeerType(id)\n const bareId = markedPeerIdToBare(id)\n\n switch (type) {\n case 'user':\n return { _: 'peerUser', userId: bareId }\n case 'chat':\n return { _: 'peerChat', chatId: bareId }\n case 'channel':\n return { _: 'peerChannel', channelId: bareId }\n }\n}\n"]}
|
package/esm/client.d.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { BaseTelegramClient, BaseTelegramClientOptions, MaybeAsync, MustEqual, RpcCallOptions, tl } from '@mtcute/core';
|
|
2
|
+
import { InputResponder } from './types.js';
|
|
3
|
+
interface MessageBox {
|
|
4
|
+
pts: number;
|
|
5
|
+
lastMessageId: number;
|
|
6
|
+
}
|
|
7
|
+
type InputPeerId = number | tl.TypePeer | false | undefined;
|
|
8
|
+
export declare class StubTelegramClient extends BaseTelegramClient {
|
|
9
|
+
constructor(params?: Partial<BaseTelegramClientOptions>);
|
|
10
|
+
readonly _knownChats: Map<number, tl.TypeChat>;
|
|
11
|
+
readonly _knownUsers: Map<number, tl.TypeUser>;
|
|
12
|
+
_selfId: number;
|
|
13
|
+
registerChat(chat: tl.TypeChat | tl.TypeChat[]): void;
|
|
14
|
+
registerUser(user: tl.TypeUser | tl.TypeUser[]): void;
|
|
15
|
+
getPeers(ids: InputPeerId[]): {
|
|
16
|
+
users: tl.TypeUser[];
|
|
17
|
+
chats: tl.TypeChat[];
|
|
18
|
+
};
|
|
19
|
+
private _onRawMessage?;
|
|
20
|
+
onRawMessage(fn: (data: Uint8Array) => void): void;
|
|
21
|
+
private _responders;
|
|
22
|
+
addResponder<T extends tl.RpcMethod['_']>(responders: InputResponder<T>): void;
|
|
23
|
+
respondWith<T extends tl.RpcMethod['_']>(method: T, response: tl.RpcCallReturn[T] | ((data: tl.FindByName<tl.RpcMethod, T>) => tl.RpcCallReturn[T])): void;
|
|
24
|
+
call<T extends tl.RpcMethod>(message: MustEqual<T, tl.RpcMethod>, params?: RpcCallOptions): Promise<tl.RpcCallReturn[T['_']]>;
|
|
25
|
+
private _fakeMessageBoxes;
|
|
26
|
+
private _lastQts;
|
|
27
|
+
private _lastDate;
|
|
28
|
+
private _lastSeq;
|
|
29
|
+
getMessageBox(chatId?: number): MessageBox;
|
|
30
|
+
getNextMessageId(chatId?: number): number;
|
|
31
|
+
getNextPts(chatId?: number, count?: number): number;
|
|
32
|
+
getNextQts(): number;
|
|
33
|
+
getNextDate(): number;
|
|
34
|
+
createStubUpdates(params: {
|
|
35
|
+
updates: tl.TypeUpdate[];
|
|
36
|
+
peers?: (number | tl.TypePeer)[];
|
|
37
|
+
seq?: number;
|
|
38
|
+
seqCount?: number;
|
|
39
|
+
}): tl.TypeUpdates;
|
|
40
|
+
connectAndWait(): Promise<void>;
|
|
41
|
+
with(fn: () => MaybeAsync<void>): Promise<void>;
|
|
42
|
+
}
|
|
43
|
+
export {};
|
package/esm/client.js
ADDED
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
import { BaseTelegramClient } from '@mtcute/core';
|
|
2
|
+
import { StubMemoryTelegramStorage } from './storage.js';
|
|
3
|
+
import { StubTelegramTransport } from './transport.js';
|
|
4
|
+
import { markedIdToPeer } from './utils.js';
|
|
5
|
+
export class StubTelegramClient extends BaseTelegramClient {
|
|
6
|
+
constructor(params) {
|
|
7
|
+
const storage = new StubMemoryTelegramStorage({
|
|
8
|
+
hasKeys: true,
|
|
9
|
+
hasTempKeys: true,
|
|
10
|
+
});
|
|
11
|
+
super({
|
|
12
|
+
apiId: 0,
|
|
13
|
+
apiHash: '',
|
|
14
|
+
logLevel: 0,
|
|
15
|
+
storage,
|
|
16
|
+
transport: () => {
|
|
17
|
+
const transport = new StubTelegramTransport({
|
|
18
|
+
onMessage: (data) => {
|
|
19
|
+
if (!this._onRawMessage) {
|
|
20
|
+
if (this._responders.size) {
|
|
21
|
+
this._emitError(new Error('Unexpected outgoing message'));
|
|
22
|
+
}
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
const dcId = transport._currentDc.id;
|
|
26
|
+
const key = storage.getAuthKeyFor(dcId);
|
|
27
|
+
if (key) {
|
|
28
|
+
this._onRawMessage(storage.decryptOutgoingMessage(transport._crypto, data, dcId));
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
});
|
|
32
|
+
return transport;
|
|
33
|
+
},
|
|
34
|
+
...params,
|
|
35
|
+
});
|
|
36
|
+
// some fake peers handling //
|
|
37
|
+
this._knownChats = new Map();
|
|
38
|
+
this._knownUsers = new Map();
|
|
39
|
+
this._selfId = 0;
|
|
40
|
+
// eslint-disable-next-line func-call-spacing
|
|
41
|
+
this._responders = new Map();
|
|
42
|
+
// some fake updates mechanism //
|
|
43
|
+
this._fakeMessageBoxes = new Map();
|
|
44
|
+
this._lastQts = 0;
|
|
45
|
+
this._lastDate = Math.floor(Date.now() / 1000);
|
|
46
|
+
this._lastSeq = 0;
|
|
47
|
+
}
|
|
48
|
+
registerChat(chat) {
|
|
49
|
+
if (Array.isArray(chat)) {
|
|
50
|
+
for (const c of chat) {
|
|
51
|
+
this.registerChat(c);
|
|
52
|
+
}
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
this._knownChats.set(chat.id, chat);
|
|
56
|
+
}
|
|
57
|
+
registerUser(user) {
|
|
58
|
+
if (Array.isArray(user)) {
|
|
59
|
+
for (const u of user) {
|
|
60
|
+
this.registerUser(u);
|
|
61
|
+
}
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
this._knownUsers.set(user.id, user);
|
|
65
|
+
if (user._ === 'user' && user.self) {
|
|
66
|
+
this._selfId = user.id;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
getPeers(ids) {
|
|
70
|
+
const users = [];
|
|
71
|
+
const chats = [];
|
|
72
|
+
for (let id of ids) {
|
|
73
|
+
if (!id)
|
|
74
|
+
continue;
|
|
75
|
+
if (typeof id === 'number') {
|
|
76
|
+
id = markedIdToPeer(id);
|
|
77
|
+
}
|
|
78
|
+
switch (id._) {
|
|
79
|
+
case 'peerUser': {
|
|
80
|
+
const user = this._knownUsers.get(id.userId);
|
|
81
|
+
if (!user)
|
|
82
|
+
throw new Error(`Unknown user with ID ${id.userId}`);
|
|
83
|
+
users.push(user);
|
|
84
|
+
break;
|
|
85
|
+
}
|
|
86
|
+
case 'peerChat': {
|
|
87
|
+
const chat = this._knownChats.get(id.chatId);
|
|
88
|
+
if (!chat)
|
|
89
|
+
throw new Error(`Unknown chat with ID ${id.chatId}`);
|
|
90
|
+
chats.push(chat);
|
|
91
|
+
break;
|
|
92
|
+
}
|
|
93
|
+
case 'peerChannel': {
|
|
94
|
+
const chat = this._knownChats.get(id.channelId);
|
|
95
|
+
if (!chat)
|
|
96
|
+
throw new Error(`Unknown channel with ID ${id.channelId}`);
|
|
97
|
+
chats.push(chat);
|
|
98
|
+
break;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return { users, chats };
|
|
103
|
+
}
|
|
104
|
+
onRawMessage(fn) {
|
|
105
|
+
this._onRawMessage = fn;
|
|
106
|
+
}
|
|
107
|
+
addResponder(responders) {
|
|
108
|
+
if (Array.isArray(responders)) {
|
|
109
|
+
for (const responder of responders) {
|
|
110
|
+
this.addResponder(responder);
|
|
111
|
+
}
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
if (typeof responders === 'function') {
|
|
115
|
+
responders = responders(this);
|
|
116
|
+
}
|
|
117
|
+
const [method, responder] = responders;
|
|
118
|
+
this.respondWith(method, responder);
|
|
119
|
+
}
|
|
120
|
+
respondWith(method, response) {
|
|
121
|
+
if (typeof response !== 'function') {
|
|
122
|
+
const res = response;
|
|
123
|
+
response = () => res;
|
|
124
|
+
}
|
|
125
|
+
// eslint-disable-next-line
|
|
126
|
+
this._responders.set(method, response);
|
|
127
|
+
}
|
|
128
|
+
async call(message, params) {
|
|
129
|
+
if (this._responders.has(message._)) {
|
|
130
|
+
// eslint-disable-next-line
|
|
131
|
+
return Promise.resolve(this._responders.get(message._)(message));
|
|
132
|
+
}
|
|
133
|
+
return super.call(message, params);
|
|
134
|
+
}
|
|
135
|
+
getMessageBox(chatId = 0) {
|
|
136
|
+
const state = this._fakeMessageBoxes.get(chatId);
|
|
137
|
+
if (!state) {
|
|
138
|
+
const newState = { pts: 0, lastMessageId: 0 };
|
|
139
|
+
this._fakeMessageBoxes.set(chatId, newState);
|
|
140
|
+
return newState;
|
|
141
|
+
}
|
|
142
|
+
return state;
|
|
143
|
+
}
|
|
144
|
+
getNextMessageId(chatId = 0) {
|
|
145
|
+
const state = this.getMessageBox(chatId);
|
|
146
|
+
const nextId = state.lastMessageId + 1;
|
|
147
|
+
state.lastMessageId = nextId;
|
|
148
|
+
return nextId;
|
|
149
|
+
}
|
|
150
|
+
getNextPts(chatId = 0, count = 1) {
|
|
151
|
+
const state = this.getMessageBox(chatId);
|
|
152
|
+
const nextPts = state.pts + count;
|
|
153
|
+
state.pts = nextPts;
|
|
154
|
+
return nextPts;
|
|
155
|
+
}
|
|
156
|
+
getNextQts() {
|
|
157
|
+
return this._lastQts++;
|
|
158
|
+
}
|
|
159
|
+
getNextDate() {
|
|
160
|
+
return (this._lastDate = Math.floor(Date.now() / 1000));
|
|
161
|
+
}
|
|
162
|
+
createStubUpdates(params) {
|
|
163
|
+
const { peers, updates, seq = 0, seqCount = 1 } = params;
|
|
164
|
+
const { users, chats } = this.getPeers(peers ?? []);
|
|
165
|
+
const seqStart = seq - seqCount + 1;
|
|
166
|
+
if (seq !== 0) {
|
|
167
|
+
this._lastSeq = seq + seqCount;
|
|
168
|
+
}
|
|
169
|
+
if (seqStart !== seq) {
|
|
170
|
+
return {
|
|
171
|
+
_: 'updatesCombined',
|
|
172
|
+
updates,
|
|
173
|
+
users,
|
|
174
|
+
chats,
|
|
175
|
+
date: this.getNextDate(),
|
|
176
|
+
seq,
|
|
177
|
+
seqStart,
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
return {
|
|
181
|
+
_: 'updates',
|
|
182
|
+
updates,
|
|
183
|
+
users,
|
|
184
|
+
chats,
|
|
185
|
+
date: this.getNextDate(),
|
|
186
|
+
seq,
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
// helpers //
|
|
190
|
+
async connectAndWait() {
|
|
191
|
+
await this.connect();
|
|
192
|
+
await new Promise((resolve) => this.once('usable', resolve));
|
|
193
|
+
}
|
|
194
|
+
async with(fn) {
|
|
195
|
+
await this.connectAndWait();
|
|
196
|
+
let error;
|
|
197
|
+
this.onError((err) => {
|
|
198
|
+
error = err;
|
|
199
|
+
});
|
|
200
|
+
try {
|
|
201
|
+
await fn();
|
|
202
|
+
}
|
|
203
|
+
catch (e) {
|
|
204
|
+
error = e;
|
|
205
|
+
}
|
|
206
|
+
await this.close();
|
|
207
|
+
if (error) {
|
|
208
|
+
// eslint-disable-next-line @typescript-eslint/no-throw-literal
|
|
209
|
+
throw error;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
//# sourceMappingURL=client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.js","sourceRoot":"","sources":["../../src/client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAwE,MAAM,cAAc,CAAA;AAEvH,OAAO,EAAE,yBAAyB,EAAE,MAAM,cAAc,CAAA;AACxD,OAAO,EAAE,qBAAqB,EAAE,MAAM,gBAAgB,CAAA;AAEtD,OAAO,EAAE,cAAc,EAAE,MAAM,YAAY,CAAA;AAS3C,MAAM,OAAO,kBAAmB,SAAQ,kBAAkB;IACtD,YAAY,MAA2C;QACnD,MAAM,OAAO,GAAG,IAAI,yBAAyB,CAAC;YAC1C,OAAO,EAAE,IAAI;YACb,WAAW,EAAE,IAAI;SACpB,CAAC,CAAA;QAEF,KAAK,CAAC;YACF,KAAK,EAAE,CAAC;YACR,OAAO,EAAE,EAAE;YACX,QAAQ,EAAE,CAAC;YACX,OAAO;YACP,SAAS,EAAE,GAAG,EAAE;gBACZ,MAAM,SAAS,GAAG,IAAI,qBAAqB,CAAC;oBACxC,SAAS,EAAE,CAAC,IAAI,EAAE,EAAE;wBAChB,IAAI,CAAC,IAAI,CAAC,aAAa,EAAE;4BACrB,IAAI,IAAI,CAAC,WAAW,CAAC,IAAI,E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CAAA;QAElB,IAAI,CAAC,OAAO,CAAC,CAAC,GAAG,EAAE,EAAE;YACjB,KAAK,GAAG,GAAG,CAAA;QACf,CAAC,CAAC,CAAA;QAEF,IAAI;YACA,MAAM,EAAE,EAAE,CAAA;SACb;QAAC,OAAO,CAAC,EAAE;YACR,KAAK,GAAG,CAAC,CAAA;SACZ;QAED,MAAM,IAAI,CAAC,KAAK,EAAE,CAAA;QAElB,IAAI,KAAK,EAAE;YACP,+DAA+D;YAC/D,MAAM,KAAK,CAAA;SACd;IACL,CAAC;CACJ","sourcesContent":["import { BaseTelegramClient, BaseTelegramClientOptions, MaybeAsync, MustEqual, RpcCallOptions, tl } from '@mtcute/core'\n\nimport { StubMemoryTelegramStorage } from './storage.js'\nimport { StubTelegramTransport } from './transport.js'\nimport { InputResponder } from './types.js'\nimport { markedIdToPeer } from './utils.js'\n\ninterface MessageBox {\n pts: number\n lastMessageId: number\n}\n\ntype InputPeerId = number | tl.TypePeer | false | undefined\n\nexport class StubTelegramClient extends BaseTelegramClient {\n constructor(params?: Partial<BaseTelegramClientOptions>) {\n const storage = new StubMemoryTelegramStorage({\n hasKeys: true,\n hasTempKeys: true,\n })\n\n super({\n apiId: 0,\n apiHash: '',\n logLevel: 0,\n storage,\n transport: () => {\n const transport = new StubTelegramTransport({\n onMessage: (data) => {\n if (!this._onRawMessage) {\n if (this._responders.size) {\n this._emitError(new Error('Unexpected outgoing message'))\n }\n\n return\n }\n\n const dcId = transport._currentDc!.id\n const key = storage.getAuthKeyFor(dcId)\n\n if (key) {\n this._onRawMessage(storage.decryptOutgoingMessage(transport._crypto, data, dcId))\n }\n },\n })\n\n return transport\n },\n ...params,\n })\n }\n\n // some fake peers handling //\n\n readonly _knownChats = new Map<number, tl.TypeChat>()\n readonly _knownUsers = new Map<number, tl.TypeUser>()\n _selfId = 0\n\n registerChat(chat: tl.TypeChat | tl.TypeChat[]): void {\n if (Array.isArray(chat)) {\n for (const c of chat) {\n this.registerChat(c)\n }\n\n return\n }\n\n this._knownChats.set(chat.id, chat)\n }\n\n registerUser(user: tl.TypeUser | tl.TypeUser[]): void {\n if (Array.isArray(user)) {\n for (const u of user) {\n this.registerUser(u)\n }\n\n return\n }\n\n this._knownUsers.set(user.id, user)\n\n if (user._ === 'user' && user.self) {\n this._selfId = user.id\n }\n }\n\n getPeers(ids: InputPeerId[]) {\n const users: tl.TypeUser[] = []\n const chats: tl.TypeChat[] = []\n\n for (let id of ids) {\n if (!id) continue\n\n if (typeof id === 'number') {\n id = markedIdToPeer(id)\n }\n\n switch (id._) {\n case 'peerUser': {\n const user = this._knownUsers.get(id.userId)\n if (!user) throw new Error(`Unknown user with ID ${id.userId}`)\n\n users.push(user)\n break\n }\n case 'peerChat': {\n const chat = this._knownChats.get(id.chatId)\n if (!chat) throw new Error(`Unknown chat with ID ${id.chatId}`)\n\n chats.push(chat)\n break\n }\n case 'peerChannel': {\n const chat = this._knownChats.get(id.channelId)\n if (!chat) throw new Error(`Unknown channel with ID ${id.channelId}`)\n\n chats.push(chat)\n break\n }\n }\n }\n\n return { users, chats }\n }\n\n // method calls intercepting //\n\n private _onRawMessage?: (data: Uint8Array) => void\n onRawMessage(fn: (data: Uint8Array) => void): void {\n this._onRawMessage = fn\n }\n\n // eslint-disable-next-line func-call-spacing\n private _responders = new Map<string, (data: unknown) => unknown>()\n\n addResponder<T extends tl.RpcMethod['_']>(responders: InputResponder<T>): void {\n if (Array.isArray(responders)) {\n for (const responder of responders) {\n this.addResponder(responder as InputResponder<tl.RpcMethod['_']>)\n }\n\n return\n }\n\n if (typeof responders === 'function') {\n responders = responders(this)\n }\n\n const [method, responder] = responders\n this.respondWith(method, responder)\n }\n\n respondWith<T extends tl.RpcMethod['_']>(\n method: T,\n response: tl.RpcCallReturn[T] | ((data: tl.FindByName<tl.RpcMethod, T>) => tl.RpcCallReturn[T]),\n ) {\n if (typeof response !== 'function') {\n const res = response\n response = () => res\n }\n\n // eslint-disable-next-line\n this._responders.set(method, response as any)\n }\n\n async call<T extends tl.RpcMethod>(\n message: MustEqual<T, tl.RpcMethod>,\n params?: RpcCallOptions,\n ): Promise<tl.RpcCallReturn[T['_']]> {\n if (this._responders.has(message._)) {\n // eslint-disable-next-line\n return Promise.resolve(this._responders.get(message._)!(message)) as any\n }\n\n return super.call(message, params)\n }\n\n // some fake updates mechanism //\n\n private _fakeMessageBoxes = new Map<number, MessageBox>()\n private _lastQts = 0\n private _lastDate = Math.floor(Date.now() / 1000)\n private _lastSeq = 0\n\n getMessageBox(chatId = 0): MessageBox {\n const state = this._fakeMessageBoxes.get(chatId)\n\n if (!state) {\n const newState = { pts: 0, lastMessageId: 0 }\n this._fakeMessageBoxes.set(chatId, newState)\n\n return newState\n }\n\n return state\n }\n\n getNextMessageId(chatId = 0) {\n const state = this.getMessageBox(chatId)\n\n const nextId = state.lastMessageId + 1\n state.lastMessageId = nextId\n\n return nextId\n }\n\n getNextPts(chatId = 0, count = 1) {\n const state = this.getMessageBox(chatId)\n\n const nextPts = state.pts + count\n state.pts = nextPts\n\n return nextPts\n }\n\n getNextQts() {\n return this._lastQts++\n }\n\n getNextDate() {\n return (this._lastDate = Math.floor(Date.now() / 1000))\n }\n\n createStubUpdates(params: {\n updates: tl.TypeUpdate[]\n peers?: (number | tl.TypePeer)[]\n seq?: number\n seqCount?: number\n }): tl.TypeUpdates {\n const { peers, updates, seq = 0, seqCount = 1 } = params\n\n const { users, chats } = this.getPeers(peers ?? [])\n\n const seqStart = seq - seqCount + 1\n\n if (seq !== 0) {\n this._lastSeq = seq + seqCount\n }\n\n if (seqStart !== seq) {\n return {\n _: 'updatesCombined',\n updates,\n users,\n chats,\n date: this.getNextDate(),\n seq,\n seqStart,\n }\n }\n\n return {\n _: 'updates',\n updates,\n users,\n chats,\n date: this.getNextDate(),\n seq,\n }\n }\n\n // helpers //\n\n async connectAndWait() {\n await this.connect()\n await new Promise((resolve) => this.once('usable', resolve))\n }\n\n async with(fn: () => MaybeAsync<void>): Promise<void> {\n await this.connectAndWait()\n\n let error: unknown\n\n this.onError((err) => {\n error = err\n })\n\n try {\n await fn()\n } catch (e) {\n error = e\n }\n\n await this.close()\n\n if (error) {\n // eslint-disable-next-line @typescript-eslint/no-throw-literal\n throw error\n }\n }\n}\n"]}
|
package/esm/index.d.ts
ADDED
package/esm/index.js
ADDED
package/esm/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAA;AAC3B,cAAc,cAAc,CAAA;AAC5B,cAAc,WAAW,CAAA;AACzB,cAAc,gBAAgB,CAAA;AAC9B,cAAc,YAAY,CAAA","sourcesContent":["export * from './client.js'\nexport * from './storage.js'\nexport * from './stub.js'\nexport * from './transport.js'\nexport * from './types.js'\n"]}
|
package/esm/schema.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/esm/schema.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { createRequire } from 'module';
|
|
2
|
+
let _cachedEntriesMap = null;
|
|
3
|
+
let _cachedUnionsMap = null;
|
|
4
|
+
/** @internal */
|
|
5
|
+
export function getEntriesMap() {
|
|
6
|
+
if (_cachedEntriesMap) {
|
|
7
|
+
return {
|
|
8
|
+
entries: _cachedEntriesMap,
|
|
9
|
+
unions: _cachedUnionsMap,
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
const schema = createRequire(import.meta.url)('@mtcute/tl/api-schema.json');
|
|
13
|
+
_cachedEntriesMap = new Map();
|
|
14
|
+
_cachedUnionsMap = new Map();
|
|
15
|
+
let entry;
|
|
16
|
+
for (entry of schema.e) {
|
|
17
|
+
_cachedEntriesMap.set(entry.name, entry);
|
|
18
|
+
if (!_cachedUnionsMap.has(entry.type)) {
|
|
19
|
+
_cachedUnionsMap.set(entry.type, []);
|
|
20
|
+
}
|
|
21
|
+
_cachedUnionsMap.get(entry.type).push(entry);
|
|
22
|
+
}
|
|
23
|
+
return {
|
|
24
|
+
entries: _cachedEntriesMap,
|
|
25
|
+
unions: _cachedUnionsMap,
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
//# sourceMappingURL=schema.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schema.js","sourceRoot":"","sources":["../../src/schema.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,QAAQ,CAAA;AAItC,IAAI,iBAAiB,GAAgC,IAAI,CAAA;AACzD,IAAI,gBAAgB,GAAkC,IAAI,CAAA;AAE1D,gBAAgB;AAChB,MAAM,UAAU,aAAa;IACzB,IAAI,iBAAiB,EAAE;QACnB,OAAO;YACH,OAAO,EAAE,iBAAiB;YAC1B,MAAM,EAAE,gBAAiB;SAC5B,CAAA;KACJ;IAED,MAAM,MAAM,GAAG,aAAa,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,4BAA4B,CAEzE,CAAA;IAED,iBAAiB,GAAG,IAAI,GAAG,EAAE,CAAA;IAC7B,gBAAgB,GAAG,IAAI,GAAG,EAAE,CAAA;IAE5B,IAAI,KAAc,CAAA;IAElB,KAAK,KAAK,IAAI,MAAM,CAAC,CAAC,EAAE;QACpB,iBAAiB,CAAC,GAAG,CAAC,KAAK,CAAC,IAAI,EAAE,KAAK,CAAC,CAAA;QAExC,IAAI,CAAC,gBAAgB,CAAC,GAAG,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE;YACnC,gBAAgB,CAAC,GAAG,CAAC,KAAK,CAAC,IAAI,EAAE,EAAE,CAAC,CAAA;SACvC;QACD,gBAAgB,CAAC,GAAG,CAAC,KAAK,CAAC,IAAI,CAAE,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;KAChD;IAED,OAAO;QACH,OAAO,EAAE,iBAAiB;QAC1B,MAAM,EAAE,gBAAgB;KAC3B,CAAA;AACL,CAAC","sourcesContent":["import { createRequire } from 'module'\n\nimport type { TlEntry } from '@mtcute/tl-utils'\n\nlet _cachedEntriesMap: Map<string, TlEntry> | null = null\nlet _cachedUnionsMap: Map<string, TlEntry[]> | null = null\n\n/** @internal */\nexport function getEntriesMap() {\n if (_cachedEntriesMap) {\n return {\n entries: _cachedEntriesMap,\n unions: _cachedUnionsMap!,\n }\n }\n\n const schema = createRequire(import.meta.url)('@mtcute/tl/api-schema.json') as {\n e: TlEntry[]\n }\n\n _cachedEntriesMap = new Map()\n _cachedUnionsMap = new Map()\n\n let entry: TlEntry\n\n for (entry of schema.e) {\n _cachedEntriesMap.set(entry.name, entry)\n\n if (!_cachedUnionsMap.has(entry.type)) {\n _cachedUnionsMap.set(entry.type, [])\n }\n _cachedUnionsMap.get(entry.type)!.push(entry)\n }\n\n return {\n entries: _cachedEntriesMap,\n unions: _cachedUnionsMap,\n }\n}\n"]}
|
package/esm/storage.d.ts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { ITelegramStorage, MemoryStorage } from '@mtcute/core';
|
|
2
|
+
import { ICryptoProvider } from '@mtcute/core/utils.js';
|
|
3
|
+
export declare class StubMemoryTelegramStorage extends MemoryStorage implements ITelegramStorage {
|
|
4
|
+
readonly params: {
|
|
5
|
+
/**
|
|
6
|
+
* IDs of the DCs for which the storage should have auth keys,
|
|
7
|
+
* or `true` to have keys for all DCs
|
|
8
|
+
*
|
|
9
|
+
* @default true
|
|
10
|
+
*/
|
|
11
|
+
hasKeys?: boolean | number[];
|
|
12
|
+
/**
|
|
13
|
+
* IDs of the DCs for which the storage should have temp auth keys,
|
|
14
|
+
* or `true` to have keys for all DCs
|
|
15
|
+
*
|
|
16
|
+
* @default true
|
|
17
|
+
*/
|
|
18
|
+
hasTempKeys?: boolean | number[];
|
|
19
|
+
onLoad?: () => void;
|
|
20
|
+
onSave?: () => void;
|
|
21
|
+
onDestroy?: () => void;
|
|
22
|
+
onReset?: () => void;
|
|
23
|
+
};
|
|
24
|
+
constructor(params?: {
|
|
25
|
+
/**
|
|
26
|
+
* IDs of the DCs for which the storage should have auth keys,
|
|
27
|
+
* or `true` to have keys for all DCs
|
|
28
|
+
*
|
|
29
|
+
* @default true
|
|
30
|
+
*/
|
|
31
|
+
hasKeys?: boolean | number[];
|
|
32
|
+
/**
|
|
33
|
+
* IDs of the DCs for which the storage should have temp auth keys,
|
|
34
|
+
* or `true` to have keys for all DCs
|
|
35
|
+
*
|
|
36
|
+
* @default true
|
|
37
|
+
*/
|
|
38
|
+
hasTempKeys?: boolean | number[];
|
|
39
|
+
onLoad?: () => void;
|
|
40
|
+
onSave?: () => void;
|
|
41
|
+
onDestroy?: () => void;
|
|
42
|
+
onReset?: () => void;
|
|
43
|
+
});
|
|
44
|
+
getAuthKeyFor(dcId: number, tempIndex?: number | undefined): Uint8Array | null;
|
|
45
|
+
load(): void;
|
|
46
|
+
save(): void;
|
|
47
|
+
destroy(): void;
|
|
48
|
+
reset(withKeys?: boolean): void;
|
|
49
|
+
decryptOutgoingMessage(crypto: ICryptoProvider, data: Uint8Array, dcId: number, tempIndex?: number | undefined): Uint8Array;
|
|
50
|
+
}
|
package/esm/storage.js
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { MemoryStorage, MtArgumentError } from '@mtcute/core';
|
|
2
|
+
import { createAesIgeForMessage } from '@mtcute/core/utils.js';
|
|
3
|
+
export class StubMemoryTelegramStorage extends MemoryStorage {
|
|
4
|
+
constructor(params = {
|
|
5
|
+
hasKeys: true,
|
|
6
|
+
hasTempKeys: true,
|
|
7
|
+
}) {
|
|
8
|
+
super();
|
|
9
|
+
this.params = params;
|
|
10
|
+
}
|
|
11
|
+
getAuthKeyFor(dcId, tempIndex) {
|
|
12
|
+
if (tempIndex === undefined && this.params.hasKeys) {
|
|
13
|
+
if (this.params.hasKeys === true || this.params.hasKeys.includes(dcId)) {
|
|
14
|
+
return new Uint8Array(256);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
if (tempIndex === undefined && this.params.hasTempKeys) {
|
|
18
|
+
if (this.params.hasTempKeys === true || this.params.hasTempKeys.includes(dcId)) {
|
|
19
|
+
return new Uint8Array(256);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
return super.getAuthKeyFor(dcId, tempIndex);
|
|
23
|
+
}
|
|
24
|
+
load() {
|
|
25
|
+
this.params.onLoad?.();
|
|
26
|
+
super.load();
|
|
27
|
+
}
|
|
28
|
+
save() {
|
|
29
|
+
this.params.onSave?.();
|
|
30
|
+
}
|
|
31
|
+
destroy() {
|
|
32
|
+
this.params.onDestroy?.();
|
|
33
|
+
super.destroy();
|
|
34
|
+
}
|
|
35
|
+
reset(withKeys = false) {
|
|
36
|
+
this.params?.onReset?.();
|
|
37
|
+
super.reset(withKeys);
|
|
38
|
+
}
|
|
39
|
+
decryptOutgoingMessage(crypto, data, dcId, tempIndex) {
|
|
40
|
+
const key = this.getAuthKeyFor(dcId, tempIndex);
|
|
41
|
+
if (!key) {
|
|
42
|
+
throw new MtArgumentError(`No auth key for DC ${dcId}`);
|
|
43
|
+
}
|
|
44
|
+
const messageKey = data.subarray(8, 24);
|
|
45
|
+
const encryptedData = data.subarray(24);
|
|
46
|
+
const ige = createAesIgeForMessage(crypto, key, messageKey, true);
|
|
47
|
+
const innerData = ige.decrypt(encryptedData);
|
|
48
|
+
// skipping all checks because who cares
|
|
49
|
+
const dv = new DataView(innerData.buffer, innerData.byteOffset, innerData.byteLength);
|
|
50
|
+
const length = dv.getUint32(28, true);
|
|
51
|
+
return innerData.subarray(32, 32 + length);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
//# sourceMappingURL=storage.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"storage.js","sourceRoot":"","sources":["../../src/storage.ts"],"names":[],"mappings":"AAAA,OAAO,EAAoB,aAAa,EAAE,eAAe,EAAE,MAAM,cAAc,CAAA;AAC/E,OAAO,EAAE,sBAAsB,EAAmB,MAAM,uBAAuB,CAAA;AAE/E,MAAM,OAAO,yBAA0B,SAA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CAAC;IAED,sBAAsB,CAAC,MAAuB,EAAE,IAAgB,EAAE,IAAY,EAAE,SAA8B;QAC1G,MAAM,GAAG,GAAG,IAAI,CAAC,aAAa,CAAC,IAAI,EAAE,SAAS,CAAC,CAAA;QAE/C,IAAI,CAAC,GAAG,EAAE;YACN,MAAM,IAAI,eAAe,CAAC,sBAAsB,IAAI,EAAE,CAAC,CAAA;SAC1D;QAED,MAAM,UAAU,GAAG,IAAI,CAAC,QAAQ,CAAC,CAAC,EAAE,EAAE,CAAC,CAAA;QACvC,MAAM,aAAa,GAAG,IAAI,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAA;QAEvC,MAAM,GAAG,GAAG,sBAAsB,CAAC,MAAM,EAAE,GAAG,EAAE,UAAU,EAAE,IAAI,CAAC,CAAA;QACjE,MAAM,SAAS,GAAG,GAAG,CAAC,OAAO,CAAC,aAAa,CAAC,CAAA;QAE5C,wCAAwC;QACxC,MAAM,EAAE,GAAG,IAAI,QAAQ,CAAC,SAAS,CAAC,MAAM,EAAE,SAAS,CAAC,UAAU,EAAE,SAAS,CAAC,UAAU,CAAC,CAAA;QACrF,MAAM,MAAM,GAAG,EAAE,CAAC,SAAS,CAAC,EAAE,EAAE,IAAI,CAAC,CAAA;QAErC,OAAO,SAAS,CAAC,QAAQ,CAAC,EAAE,EAAE,EAAE,GAAG,MAAM,CAAC,CAAA;IAC9C,CAAC;CACJ","sourcesContent":["import { ITelegramStorage, MemoryStorage, MtArgumentError } from '@mtcute/core'\nimport { createAesIgeForMessage, ICryptoProvider } from '@mtcute/core/utils.js'\n\nexport class StubMemoryTelegramStorage extends MemoryStorage implements ITelegramStorage {\n constructor(\n readonly params: {\n /**\n * IDs of the DCs for which the storage should have auth keys,\n * or `true` to have keys for all DCs\n *\n * @default true\n */\n hasKeys?: boolean | number[]\n\n /**\n * IDs of the DCs for which the storage should have temp auth keys,\n * or `true` to have keys for all DCs\n *\n * @default true\n */\n hasTempKeys?: boolean | number[]\n\n onLoad?: () => void\n onSave?: () => void\n onDestroy?: () => void\n onReset?: () => void\n } = {\n hasKeys: true,\n hasTempKeys: true,\n },\n ) {\n super()\n }\n\n getAuthKeyFor(dcId: number, tempIndex?: number | undefined): Uint8Array | null {\n if (tempIndex === undefined && this.params.hasKeys) {\n if (this.params.hasKeys === true || this.params.hasKeys.includes(dcId)) {\n return new Uint8Array(256)\n }\n }\n\n if (tempIndex === undefined && this.params.hasTempKeys) {\n if (this.params.hasTempKeys === true || this.params.hasTempKeys.includes(dcId)) {\n return new Uint8Array(256)\n }\n }\n\n return super.getAuthKeyFor(dcId, tempIndex)\n }\n\n load(): void {\n this.params.onLoad?.()\n super.load()\n }\n\n save(): void {\n this.params.onSave?.()\n }\n\n destroy(): void {\n this.params.onDestroy?.()\n super.destroy()\n }\n\n reset(withKeys = false): void {\n this.params?.onReset?.()\n super.reset(withKeys)\n }\n\n decryptOutgoingMessage(crypto: ICryptoProvider, data: Uint8Array, dcId: number, tempIndex?: number | undefined) {\n const key = this.getAuthKeyFor(dcId, tempIndex)\n\n if (!key) {\n throw new MtArgumentError(`No auth key for DC ${dcId}`)\n }\n\n const messageKey = data.subarray(8, 24)\n const encryptedData = data.subarray(24)\n\n const ige = createAesIgeForMessage(crypto, key, messageKey, true)\n const innerData = ige.decrypt(encryptedData)\n\n // skipping all checks because who cares\n const dv = new DataView(innerData.buffer, innerData.byteOffset, innerData.byteLength)\n const length = dv.getUint32(28, true)\n\n return innerData.subarray(32, 32 + length)\n }\n}\n"]}
|
package/esm/stub.d.ts
ADDED
package/esm/stub.js
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import Long from 'long';
|
|
2
|
+
import { getEntriesMap } from './schema.js';
|
|
3
|
+
function getDefaultFor(arg) {
|
|
4
|
+
if (arg.typeModifiers?.isVector || arg.typeModifiers?.isBareVector) {
|
|
5
|
+
return [];
|
|
6
|
+
}
|
|
7
|
+
if (arg.typeModifiers?.predicate) {
|
|
8
|
+
return arg.type === 'true' ? false : undefined;
|
|
9
|
+
}
|
|
10
|
+
switch (arg.type) {
|
|
11
|
+
case 'int':
|
|
12
|
+
case 'int53':
|
|
13
|
+
case 'double':
|
|
14
|
+
return 0;
|
|
15
|
+
case 'long':
|
|
16
|
+
return Long.ZERO;
|
|
17
|
+
case 'int128':
|
|
18
|
+
return new Uint8Array(16);
|
|
19
|
+
case 'int256':
|
|
20
|
+
return new Uint8Array(32);
|
|
21
|
+
case 'string':
|
|
22
|
+
return '';
|
|
23
|
+
case 'bytes':
|
|
24
|
+
return new Uint8Array(0);
|
|
25
|
+
case 'Bool':
|
|
26
|
+
case 'bool':
|
|
27
|
+
return false;
|
|
28
|
+
default: {
|
|
29
|
+
const union = getEntriesMap().unions.get(arg.type);
|
|
30
|
+
if (!union)
|
|
31
|
+
throw new Error(`Unknown type ${arg.type}`);
|
|
32
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
33
|
+
return createStub(union[0].name);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
const snakeToCamel = (s) => {
|
|
38
|
+
return s.replace(/(?<!^|_)(_[a-z0-9])/gi, ($1) => {
|
|
39
|
+
return $1.substring(1).toUpperCase();
|
|
40
|
+
});
|
|
41
|
+
};
|
|
42
|
+
export function createStub(name, partial = {}) {
|
|
43
|
+
const { entries } = getEntriesMap();
|
|
44
|
+
const entry = entries.get(name);
|
|
45
|
+
if (!entry)
|
|
46
|
+
throw new Error(`Entry ${name} is unknown`);
|
|
47
|
+
const ret = {
|
|
48
|
+
_: name,
|
|
49
|
+
};
|
|
50
|
+
for (const arg of entry.arguments) {
|
|
51
|
+
if (arg.type === '#')
|
|
52
|
+
continue;
|
|
53
|
+
if (arg.name in partial)
|
|
54
|
+
continue;
|
|
55
|
+
ret[snakeToCamel(arg.name)] = getDefaultFor(arg);
|
|
56
|
+
}
|
|
57
|
+
for (const key in partial) {
|
|
58
|
+
// @ts-expect-error partial is not a full object
|
|
59
|
+
ret[key] = partial[key];
|
|
60
|
+
}
|
|
61
|
+
// eslint-disable-next-line
|
|
62
|
+
return ret;
|
|
63
|
+
}
|
|
64
|
+
//# sourceMappingURL=stub.js.map
|
package/esm/stub.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stub.js","sourceRoot":"","sources":["../../src/stub.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,MAAM,CAAA;AAKvB,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAA;AAE3C,SAAS,aAAa,CAAC,GAAe;IAClC,IAAI,GAAG,CAAC,aAAa,EAAE,QAAQ,IAAI,GAAG,CAAC,aAAa,EAAE,YAAY,EAAE;QAChE,OAAO,EAAE,CAAA;KACZ;IAED,IAAI,GAAG,CAAC,aAAa,EAAE,SAAS,EAAE;QAC9B,OAAO,GAAG,CAAC,IAAI,KAAK,MAAM,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,SAAS,CAAA;KACjD;IAED,QAAQ,GAAG,CAAC,IAAI,EAAE;QACd,KAAK,KAAK,CAAC;QACX,KAAK,OAAO,CAAC;QACb,KAAK,QAAQ;YACT,OAAO,CAAC,CAAA;QACZ,KAAK,MAAM;YACP,OAAO,IAAI,CAAC,IAAI,CAAA;QACpB,KAAK,QAAQ;YACT,OAAO,IAAI,UAAU,CAAC,EAAE,CAAC,CAAA;QAC7B,KAAK,QAAQ;YACT,OAAO,IAAI,UAAU,CAAC,EAAE,CAAC,CAAA;QAC7B,KAAK,QAAQ;YACT,OAAO,EAAE,CAAA;QACb,KAAK,OAAO;YACR,OAAO,IAAI,UAAU,CAAC,CAAC,CAAC,CAAA;QAC5B,KAAK,MAAM,CAAC;QACZ,KAAK,MAAM;YACP,OAAO,KAAK,CAAA;QAEhB,OAAO,CAAC,CAAC;YACL,MAAM,KAAK,GAAG,aAAa,EAAE,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC,CAAA;YAClD,IAAI,CAAC,KAAK;gBA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l'\nimport type { TlArgument } from '@mtcute/tl-utils'\n\nimport { getEntriesMap } from './schema.js'\n\nfunction getDefaultFor(arg: TlArgument): unknown {\n if (arg.typeModifiers?.isVector || arg.typeModifiers?.isBareVector) {\n return []\n }\n\n if (arg.typeModifiers?.predicate) {\n return arg.type === 'true' ? false : undefined\n }\n\n switch (arg.type) {\n case 'int':\n case 'int53':\n case 'double':\n return 0\n case 'long':\n return Long.ZERO\n case 'int128':\n return new Uint8Array(16)\n case 'int256':\n return new Uint8Array(32)\n case 'string':\n return ''\n case 'bytes':\n return new Uint8Array(0)\n case 'Bool':\n case 'bool':\n return false\n\n default: {\n const union = getEntriesMap().unions.get(arg.type)\n if (!union) throw new Error(`Unknown type ${arg.type}`)\n\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n return createStub<any>(union[0].name)\n }\n }\n}\n\nconst snakeToCamel = (s: string): string => {\n return s.replace(/(?<!^|_)(_[a-z0-9])/gi, ($1) => {\n return $1.substring(1).toUpperCase()\n })\n}\n\nexport function createStub<T extends tl.TlObject['_']>(\n name: T,\n partial: Partial<tl.FindByName<tl.TlObject, T>> = {},\n): tl.FindByName<tl.TlObject, T> {\n const { entries } = getEntriesMap()\n const entry = entries.get(name)\n\n if (!entry) throw new Error(`Entry ${name} is unknown`)\n\n const ret: Record<string, unknown> = {\n _: name,\n }\n\n for (const arg of entry.arguments) {\n if (arg.type === '#') continue\n if (arg.name in partial) continue\n\n ret[snakeToCamel(arg.name)] = getDefaultFor(arg)\n }\n\n for (const key in partial) {\n // @ts-expect-error partial is not a full object\n ret[key] = partial[key]\n }\n\n // eslint-disable-next-line\n return ret as any\n}\n"]}
|