@mtcute/test 0.16.7 → 0.16.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/client.d.ts +3 -5
- package/client.test.d.ts +1 -0
- package/crypto.d.ts +1 -1
- package/index.d.cts +8 -0
- package/index.js +862 -9
- package/package.json +44 -35
- package/platform.test.d.ts +1 -0
- package/schema.d.ts +1 -1
- package/storage/auth-keys.d.ts +1 -1
- package/storage/key-value.d.ts +1 -1
- package/storage/peers.d.ts +1 -1
- package/storage/ref-messages.d.ts +1 -1
- package/storage.d.ts +1 -1
- package/stub.d.ts +1 -1
- package/stub.test.d.ts +1 -0
- package/transport.d.ts +4 -5
- package/transport.test.d.ts +1 -0
- package/types.d.ts +2 -2
- package/utils.d.ts +1 -1
- package/utils.test.d.ts +1 -0
- package/client.js +0 -230
- package/client.js.map +0 -1
- package/crypto.js +0 -179
- package/crypto.js.map +0 -1
- package/index.js.map +0 -1
- package/platform.js +0 -5
- package/platform.js.map +0 -1
- package/platform.web.js +0 -4
- package/platform.web.js.map +0 -1
- package/schema.js +0 -28
- package/schema.js.map +0 -1
- package/storage/auth-keys.js +0 -90
- package/storage/auth-keys.js.map +0 -1
- package/storage/index.js +0 -5
- package/storage/index.js.map +0 -1
- package/storage/key-value.js +0 -36
- package/storage/key-value.js.map +0 -1
- package/storage/peers.js +0 -70
- package/storage/peers.js.map +0 -1
- package/storage/ref-messages.js +0 -64
- package/storage/ref-messages.js.map +0 -1
- package/storage.js +0 -45
- package/storage.js.map +0 -1
- package/stub.js +0 -63
- package/stub.js.map +0 -1
- package/transport.js +0 -47
- package/transport.js.map +0 -1
- package/types.js +0 -2
- package/types.js.map +0 -1
- package/utils.js +0 -13
- package/utils.js.map +0 -1
package/package.json
CHANGED
|
@@ -1,39 +1,48 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
"peerDependenciesMeta": {
|
|
21
|
-
"@mtcute/node": {
|
|
22
|
-
"optional": true
|
|
2
|
+
"name": "@mtcute/test",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.16.13",
|
|
5
|
+
"description": "Test utilities for mtcute",
|
|
6
|
+
"author": "alina sireneva <alina@tei.su>",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"sideEffects": false,
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"import": {
|
|
12
|
+
"types": "./index.d.ts",
|
|
13
|
+
"default": "./index.js"
|
|
14
|
+
},
|
|
15
|
+
"require": {
|
|
16
|
+
"types": "./index.d.cts",
|
|
17
|
+
"default": "./index.cjs"
|
|
18
|
+
}
|
|
19
|
+
}
|
|
23
20
|
},
|
|
24
|
-
"
|
|
25
|
-
|
|
21
|
+
"scripts": {},
|
|
22
|
+
"peerDependencies": {
|
|
23
|
+
"@mtcute/core": "^0.16.13",
|
|
24
|
+
"@mtcute/node": "^0.16.13",
|
|
25
|
+
"@mtcute/tl": "*",
|
|
26
|
+
"@mtcute/web": "^0.16.13",
|
|
27
|
+
"vitest": "*"
|
|
28
|
+
},
|
|
29
|
+
"peerDependenciesMeta": {
|
|
30
|
+
"@mtcute/node": {
|
|
31
|
+
"optional": true
|
|
32
|
+
},
|
|
33
|
+
"@mtcute/web": {
|
|
34
|
+
"optional": true
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
"dependencies": {
|
|
38
|
+
"long": "5.2.3"
|
|
39
|
+
},
|
|
40
|
+
"browser": {
|
|
41
|
+
"./esm/platform.js": "./esm/platform.web.js"
|
|
42
|
+
},
|
|
43
|
+
"homepage": "https://mtcute.dev",
|
|
44
|
+
"repository": {
|
|
45
|
+
"type": "git",
|
|
46
|
+
"url": "https://github.com/mtcute/mtcute"
|
|
26
47
|
}
|
|
27
|
-
},
|
|
28
|
-
"dependencies": {
|
|
29
|
-
"long": "5.2.3"
|
|
30
|
-
},
|
|
31
|
-
"browser": {
|
|
32
|
-
"./esm/platform.js": "./esm/platform.web.js"
|
|
33
|
-
},
|
|
34
|
-
"homepage": "https://mtcute.dev",
|
|
35
|
-
"repository": {
|
|
36
|
-
"type": "git",
|
|
37
|
-
"url": "https://github.com/mtcute/mtcute"
|
|
38
|
-
}
|
|
39
48
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/schema.d.ts
CHANGED
package/storage/auth-keys.d.ts
CHANGED
package/storage/key-value.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { IKeyValueRepository, IStorageDriver } from '@mtcute/core';
|
|
2
2
|
export declare function fakeKeyValueRepository(): IKeyValueRepository;
|
|
3
3
|
export declare function testKeyValueRepository(repo: IKeyValueRepository, driver: IStorageDriver): void;
|
package/storage/peers.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { IPeersRepository, IStorageDriver } from '@mtcute/core';
|
|
2
2
|
export declare function fakePeersRepository(): IPeersRepository;
|
|
3
3
|
export declare function testPeersRepository(repo: IPeersRepository, driver: IStorageDriver): void;
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { IReferenceMessagesRepository, IStorageDriver } from '@mtcute/core';
|
|
2
2
|
export declare function fakeRefMessagesRepository(): IReferenceMessagesRepository;
|
|
3
3
|
export declare function testRefMessagesRepository(repo: IReferenceMessagesRepository, driver: IStorageDriver): void;
|
package/storage.d.ts
CHANGED
package/stub.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { tl } from '@mtcute/tl';
|
|
2
2
|
export declare function createStub<T extends tl.TlObject['_']>(name: T, partial?: Partial<tl.FindByName<tl.TlObject, T>>): tl.FindByName<tl.TlObject, T>;
|
package/stub.test.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/transport.d.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import EventEmitter from 'node:events';
|
|
2
|
-
import
|
|
3
|
-
import {
|
|
4
|
-
import
|
|
5
|
-
import type { tl } from '@mtcute/tl';
|
|
1
|
+
import { default as EventEmitter } from 'node:events';
|
|
2
|
+
import { ITelegramTransport, TransportState } from '@mtcute/core';
|
|
3
|
+
import { ICryptoProvider, Logger } from '@mtcute/core/utils.js';
|
|
4
|
+
import { tl } from '@mtcute/tl';
|
|
6
5
|
export declare class StubTelegramTransport extends EventEmitter implements ITelegramTransport {
|
|
7
6
|
readonly params: {
|
|
8
7
|
getMtproxyInfo?: () => tl.RawInputClientProxy;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/types.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
1
|
+
import { tl } from '@mtcute/tl';
|
|
2
|
+
import { StubTelegramClient } from './client.js';
|
|
3
3
|
export type Responder<Method extends tl.RpcMethod['_']> = [
|
|
4
4
|
Method,
|
|
5
5
|
(req: tl.FindByName<tl.RpcMethod, Method>) => tl.RpcCallReturn[Method]
|
package/utils.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { tl } from '@mtcute/core';
|
|
2
2
|
export declare function markedIdToPeer(id: number): tl.TypePeer;
|
package/utils.test.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/client.js
DELETED
|
@@ -1,230 +0,0 @@
|
|
|
1
|
-
import { tl } from '@mtcute/core';
|
|
2
|
-
import { BaseTelegramClient } from '@mtcute/core/client.js';
|
|
3
|
-
import { defaultCryptoProvider } from './platform.js';
|
|
4
|
-
import { StubMemoryTelegramStorage } from './storage.js';
|
|
5
|
-
import { StubTelegramTransport } from './transport.js';
|
|
6
|
-
import { markedIdToPeer } from './utils.js';
|
|
7
|
-
export class StubTelegramClient extends BaseTelegramClient {
|
|
8
|
-
constructor(params) {
|
|
9
|
-
const storage = new StubMemoryTelegramStorage({
|
|
10
|
-
hasKeys: true,
|
|
11
|
-
hasTempKeys: true,
|
|
12
|
-
});
|
|
13
|
-
super({
|
|
14
|
-
apiId: 0,
|
|
15
|
-
apiHash: '',
|
|
16
|
-
logLevel: 0,
|
|
17
|
-
storage,
|
|
18
|
-
disableUpdates: true,
|
|
19
|
-
transport: () => {
|
|
20
|
-
const transport = new StubTelegramTransport({
|
|
21
|
-
onMessage: (data) => {
|
|
22
|
-
if (!this._onRawMessage) {
|
|
23
|
-
if (this._responders.size) {
|
|
24
|
-
this.emitError(new Error('Unexpected outgoing message'));
|
|
25
|
-
}
|
|
26
|
-
return;
|
|
27
|
-
}
|
|
28
|
-
const dcId = transport._currentDc.id;
|
|
29
|
-
const key = storage.authKeys.get(dcId);
|
|
30
|
-
if (key) {
|
|
31
|
-
this._onRawMessage(storage.decryptOutgoingMessage(transport._crypto, data, dcId));
|
|
32
|
-
}
|
|
33
|
-
},
|
|
34
|
-
});
|
|
35
|
-
return transport;
|
|
36
|
-
},
|
|
37
|
-
crypto: defaultCryptoProvider,
|
|
38
|
-
...params,
|
|
39
|
-
});
|
|
40
|
-
}
|
|
41
|
-
/**
|
|
42
|
-
* Create a fake client that may not actually be used for API calls
|
|
43
|
-
*
|
|
44
|
-
* Useful for testing "offline" methods
|
|
45
|
-
*/
|
|
46
|
-
static offline() {
|
|
47
|
-
const client = new StubTelegramClient();
|
|
48
|
-
client.call = (obj) => {
|
|
49
|
-
throw new Error(`Expected offline client to not make any API calls (method called: ${obj._})`);
|
|
50
|
-
};
|
|
51
|
-
return client;
|
|
52
|
-
}
|
|
53
|
-
/**
|
|
54
|
-
* Create a fake "full" client (i.e. TelegramClient)
|
|
55
|
-
*
|
|
56
|
-
* Basically a proxy that returns an empty function for every unknown method
|
|
57
|
-
*/
|
|
58
|
-
static full() {
|
|
59
|
-
const client = new StubTelegramClient();
|
|
60
|
-
// eslint-disable-next-line ts/no-unsafe-return
|
|
61
|
-
return new Proxy(client, {
|
|
62
|
-
get(target, prop) {
|
|
63
|
-
if (typeof prop === 'string' && !(prop in target)) {
|
|
64
|
-
return () => { };
|
|
65
|
-
}
|
|
66
|
-
return target[prop];
|
|
67
|
-
},
|
|
68
|
-
});
|
|
69
|
-
// i don't want to type this properly since it would require depending test utils on client
|
|
70
|
-
}
|
|
71
|
-
// some fake peers handling //
|
|
72
|
-
_knownChats = new Map();
|
|
73
|
-
_knownUsers = new Map();
|
|
74
|
-
_selfId = 0;
|
|
75
|
-
async registerPeers(...peers) {
|
|
76
|
-
for (const peer of peers) {
|
|
77
|
-
if (tl.isAnyUser(peer)) {
|
|
78
|
-
this._knownUsers.set(peer.id, peer);
|
|
79
|
-
}
|
|
80
|
-
else {
|
|
81
|
-
this._knownChats.set(peer.id, peer);
|
|
82
|
-
}
|
|
83
|
-
await this.storage.peers.updatePeersFrom(peer);
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
getPeers(ids) {
|
|
87
|
-
const users = [];
|
|
88
|
-
const chats = [];
|
|
89
|
-
for (let id of ids) {
|
|
90
|
-
if (!id)
|
|
91
|
-
continue;
|
|
92
|
-
if (typeof id === 'number') {
|
|
93
|
-
id = markedIdToPeer(id);
|
|
94
|
-
}
|
|
95
|
-
switch (id._) {
|
|
96
|
-
case 'peerUser': {
|
|
97
|
-
const user = this._knownUsers.get(id.userId);
|
|
98
|
-
if (!user)
|
|
99
|
-
throw new Error(`Unknown user with ID ${id.userId}`);
|
|
100
|
-
users.push(user);
|
|
101
|
-
break;
|
|
102
|
-
}
|
|
103
|
-
case 'peerChat': {
|
|
104
|
-
const chat = this._knownChats.get(id.chatId);
|
|
105
|
-
if (!chat)
|
|
106
|
-
throw new Error(`Unknown chat with ID ${id.chatId}`);
|
|
107
|
-
chats.push(chat);
|
|
108
|
-
break;
|
|
109
|
-
}
|
|
110
|
-
case 'peerChannel': {
|
|
111
|
-
const chat = this._knownChats.get(id.channelId);
|
|
112
|
-
if (!chat)
|
|
113
|
-
throw new Error(`Unknown channel with ID ${id.channelId}`);
|
|
114
|
-
chats.push(chat);
|
|
115
|
-
break;
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
return { users, chats };
|
|
120
|
-
}
|
|
121
|
-
// method calls intercepting //
|
|
122
|
-
_onRawMessage;
|
|
123
|
-
onRawMessage(fn) {
|
|
124
|
-
this._onRawMessage = fn;
|
|
125
|
-
}
|
|
126
|
-
_responders = new Map();
|
|
127
|
-
addResponder(responders) {
|
|
128
|
-
if (Array.isArray(responders)) {
|
|
129
|
-
for (const responder of responders) {
|
|
130
|
-
this.addResponder(responder);
|
|
131
|
-
}
|
|
132
|
-
return;
|
|
133
|
-
}
|
|
134
|
-
if (typeof responders === 'function') {
|
|
135
|
-
responders = responders(this);
|
|
136
|
-
}
|
|
137
|
-
const [method, responder] = responders;
|
|
138
|
-
this.respondWith(method, responder);
|
|
139
|
-
}
|
|
140
|
-
respondWith(method, response) {
|
|
141
|
-
// eslint-disable-next-line
|
|
142
|
-
this._responders.set(method, response);
|
|
143
|
-
return response;
|
|
144
|
-
}
|
|
145
|
-
async call(message, params) {
|
|
146
|
-
if (this._responders.has(message._)) {
|
|
147
|
-
// eslint-disable-next-line
|
|
148
|
-
return Promise.resolve(this._responders.get(message._)(message));
|
|
149
|
-
}
|
|
150
|
-
return super.call(message, params);
|
|
151
|
-
}
|
|
152
|
-
// some fake updates mechanism //
|
|
153
|
-
_fakeMessageBoxes = new Map();
|
|
154
|
-
_lastQts = 0;
|
|
155
|
-
_lastDate = Math.floor(Date.now() / 1000);
|
|
156
|
-
_lastSeq = 0;
|
|
157
|
-
getMessageBox(chatId = 0) {
|
|
158
|
-
const state = this._fakeMessageBoxes.get(chatId);
|
|
159
|
-
if (!state) {
|
|
160
|
-
const newState = { pts: 0, lastMessageId: 0 };
|
|
161
|
-
this._fakeMessageBoxes.set(chatId, newState);
|
|
162
|
-
return newState;
|
|
163
|
-
}
|
|
164
|
-
return state;
|
|
165
|
-
}
|
|
166
|
-
getNextMessageId(chatId = 0) {
|
|
167
|
-
const state = this.getMessageBox(chatId);
|
|
168
|
-
const nextId = state.lastMessageId + 1;
|
|
169
|
-
state.lastMessageId = nextId;
|
|
170
|
-
return nextId;
|
|
171
|
-
}
|
|
172
|
-
getNextPts(chatId = 0, count = 1) {
|
|
173
|
-
const state = this.getMessageBox(chatId);
|
|
174
|
-
const nextPts = state.pts + count;
|
|
175
|
-
state.pts = nextPts;
|
|
176
|
-
return nextPts;
|
|
177
|
-
}
|
|
178
|
-
getNextQts() {
|
|
179
|
-
return this._lastQts++;
|
|
180
|
-
}
|
|
181
|
-
getNextDate() {
|
|
182
|
-
return (this._lastDate = Math.floor(Date.now() / 1000));
|
|
183
|
-
}
|
|
184
|
-
createStubUpdates(params) {
|
|
185
|
-
const { peers, updates, seq = 0, seqCount = 1 } = params;
|
|
186
|
-
const { users, chats } = this.getPeers(peers ?? []);
|
|
187
|
-
const seqStart = seq - seqCount + 1;
|
|
188
|
-
if (seq !== 0) {
|
|
189
|
-
this._lastSeq = seq + seqCount;
|
|
190
|
-
}
|
|
191
|
-
if (seqStart !== seq) {
|
|
192
|
-
return {
|
|
193
|
-
_: 'updatesCombined',
|
|
194
|
-
updates,
|
|
195
|
-
users,
|
|
196
|
-
chats,
|
|
197
|
-
date: this.getNextDate(),
|
|
198
|
-
seq,
|
|
199
|
-
seqStart,
|
|
200
|
-
};
|
|
201
|
-
}
|
|
202
|
-
return {
|
|
203
|
-
_: 'updates',
|
|
204
|
-
updates,
|
|
205
|
-
users,
|
|
206
|
-
chats,
|
|
207
|
-
date: this.getNextDate(),
|
|
208
|
-
seq,
|
|
209
|
-
};
|
|
210
|
-
}
|
|
211
|
-
// helpers //
|
|
212
|
-
async with(fn) {
|
|
213
|
-
await this.connect();
|
|
214
|
-
let error;
|
|
215
|
-
this.onError((err) => {
|
|
216
|
-
error = err;
|
|
217
|
-
});
|
|
218
|
-
try {
|
|
219
|
-
await fn();
|
|
220
|
-
}
|
|
221
|
-
catch (e) {
|
|
222
|
-
error = e;
|
|
223
|
-
}
|
|
224
|
-
await this.close();
|
|
225
|
-
if (error) {
|
|
226
|
-
throw error;
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
//# sourceMappingURL=client.js.map
|
package/client.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"client.js","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,EAAE,EAAE,MAAM,cAAc,CAAA;AAEjC,OAAO,EAAE,kBAAkB,EAAE,MAAM,wBAAwB,CAAA;AAE3D,OAAO,EAAE,qBAAqB,EAAE,MAAM,eAAe,CAAA;AACrD,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,cAAc,EAAE,IAAI;YACpB,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,CAAC;4BACtB,IAAI,IAAI,CAAC,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H,CAAC,EAAE,iBAAiB;gBACpB,OAAO;gBACP,KAAK;gBACL,KAAK;gBACL,IAAI,EAAE,IAAI,CAAC,WAAW,EAAE;gBACxB,GAAG;gBACH,QAAQ;aACX,CAAA;QACL,CAAC;QAED,OAAO;YACH,CAAC,EAAE,SAAS;YACZ,OAAO;YACP,KAAK;YACL,KAAK;YACL,IAAI,EAAE,IAAI,CAAC,WAAW,EAAE;YACxB,GAAG;SACN,CAAA;IACL,CAAC;IAED,aAAa;IAEb,KAAK,CAAC,IAAI,CAAC,EAA4B;QACnC,MAAM,IAAI,CAAC,OAAO,EAAE,CAAA;QAEpB,IAAI,KAAc,CAAA;QAElB,IAAI,CAAC,OAAO,CAAC,CAAC,GAAG,EAAE,EAAE;YACjB,KAAK,GAAG,GAAG,CAAA;QACf,CAAC,CAAC,CAAA;QAEF,IAAI,CAAC;YACD,MAAM,EAAE,EAAE,CAAA;QACd,CAAC;QAAC,OAAO,CAAC,EAAE,CAAC;YACT,KAAK,GAAG,CAAC,CAAA;QACb,CAAC;QAED,MAAM,IAAI,CAAC,KAAK,EAAE,CAAA;QAElB,IAAI,KAAK,EAAE,CAAC;YACR,MAAM,KAAK,CAAA;QACf,CAAC;IACL,CAAC;CACJ","sourcesContent":["import type { MaybePromise, MustEqual, RpcCallOptions } from '@mtcute/core'\nimport { tl } from '@mtcute/core'\nimport type { BaseTelegramClientOptions } from '@mtcute/core/client.js'\nimport { BaseTelegramClient } from '@mtcute/core/client.js'\n\nimport { defaultCryptoProvider } from './platform.js'\nimport { StubMemoryTelegramStorage } from './storage.js'\nimport { StubTelegramTransport } from './transport.js'\nimport type { 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 disableUpdates: true,\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.authKeys.get(dcId)\n\n if (key) {\n this._onRawMessage(storage.decryptOutgoingMessage(transport._crypto, data, dcId))\n }\n },\n })\n\n return transport\n },\n crypto: defaultCryptoProvider,\n ...params,\n })\n }\n\n /**\n * Create a fake client that may not actually be used for API calls\n *\n * Useful for testing \"offline\" methods\n */\n static offline(): StubTelegramClient {\n const client = new StubTelegramClient()\n\n client.call = (obj) => {\n throw new Error(`Expected offline client to not make any API calls (method called: ${obj._})`)\n }\n\n return client\n }\n\n /**\n * Create a fake \"full\" client (i.e. TelegramClient)\n *\n * Basically a proxy that returns an empty function for every unknown method\n */\n static full() {\n const client = new StubTelegramClient()\n\n // eslint-disable-next-line ts/no-unsafe-return\n return new Proxy(client, {\n get(target, prop) {\n if (typeof prop === 'string' && !(prop in target)) {\n return () => {}\n }\n\n return target[prop as keyof typeof target]\n },\n }) as any\n // i don't want to type this properly since it would require depending test utils on client\n }\n\n // some fake peers handling //\n\n readonly _knownChats: Map<number, tl.TypeChat> = new Map()\n readonly _knownUsers: Map<number, tl.TypeUser> = new Map()\n _selfId = 0\n\n async registerPeers(...peers: (tl.TypeUser | tl.TypeChat)[]): Promise<void> {\n for (const peer of peers) {\n if (tl.isAnyUser(peer)) {\n this._knownUsers.set(peer.id, peer)\n } else {\n this._knownChats.set(peer.id, peer)\n }\n\n await this.storage.peers.updatePeersFrom(peer)\n }\n }\n\n getPeers(ids: InputPeerId[]): { users: tl.TypeUser[], chats: tl.TypeChat[] } {\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 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<\n T extends tl.RpcMethod['_'],\n Fn extends(data: tl.FindByName<tl.RpcMethod, T>) => MaybePromise<tl.RpcCallReturn[T]>,\n >(method: T,\n response: Fn,\n ): Fn {\n // eslint-disable-next-line\n this._responders.set(method, response as any)\n\n return response\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): number {\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): number {\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(): number {\n return this._lastQts++\n }\n\n getNextDate(): number {\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 with(fn: () => MaybePromise<void>): Promise<void> {\n await this.connect()\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 throw error\n }\n }\n}\n"]}
|
package/crypto.js
DELETED
|
@@ -1,179 +0,0 @@
|
|
|
1
|
-
import { gzipSync, inflateSync } from 'node:zlib';
|
|
2
|
-
import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
3
|
-
import { getPlatform } from '@mtcute/core/platform.js';
|
|
4
|
-
import { dataViewFromBuffer } from '@mtcute/core/utils.js';
|
|
5
|
-
import { defaultCryptoProvider } from './platform.js';
|
|
6
|
-
// some random 1024 bytes of entropy
|
|
7
|
-
const DEFAULT_ENTROPY = `
|
|
8
|
-
29afd26df40fb8ed10b6b4ad6d56ef5df9453f88e6ee6adb6e0544ba635dc6a8a990c9b8b980c343936b33fa7f97bae025102532233abb269c489920ef99021b
|
|
9
|
-
259ce3a2c964c5c8972b4a84ff96f3375a94b535a9468f2896e2080ac7a32ed58e910474a4b02415e07671cbb5bdd58a5dd26fd137c4c98b8c346571fae6ead3
|
|
10
|
-
9dfd612bd6b480b6723433f5218e9d6e271591153fb3ffefc089f7e848d3f4633459fff66b33cf939e5655813149fa34be8625f9bc4814d1ee6cf40e4d0de229
|
|
11
|
-
1aa22e68c8ad8cc698103734f9aaf79f2bdc052a787a7a9b3629d1ed38750f88cb0481c0ba30a9c611672f9a4d1dc02637abb4e98913ee810a3b152d3d75f25d
|
|
12
|
-
7efdc263c08833569968b1771ebbe843d187e2c917d9ad8e8865e44b69f7b74d72ab86a4ef1891dce196ee11a7c9d7d8074fc0450e745bd3a827d77bb0820b90
|
|
13
|
-
3055dc15f0abd897ea740a99606b64d28968d770b5e43492ddbf07a7c75104d3e522be9b72050c0fdae8412cdf49014be21105b87a06cb7202dd580387adc007
|
|
14
|
-
6280d98b015a1a413819d817f007939d1490467a1ef85a345584c7e594bb729c12a1233f806e515e7088360219dfa109264310ba84777b93eb1ad3c40727a25a
|
|
15
|
-
a5d9cdd6748c6ab2ca0bd4daa2ba8225bce2b066a163bcacf05609fc84055bb86a4742c28addd7d7ab8d87b64cfde0b3f4b3bc8e05f3d0a1a2fadb294860e099
|
|
16
|
-
a10b3721b0d5b28918b8fb49a18a82a0fde6680a64ed915637805e35ffe8b2c1d4177ec10d10eaaf24425e0351b6a89e794944e1aa82eb5c0210a37da66cccac
|
|
17
|
-
895398cf915a8aa141f611521fc258514a99c02721113942c66f2c9a8f9601ff0044a953d17a47b07ad1b5f8725cc020a1a5239be65db0a43d42c206903740f0
|
|
18
|
-
27c3f749ecfff2e646570118cd54db2fec392b44d8eb8377309f3e4d164dbc0530914b117b9d278b06db8359d97442d4dcbcaff93cd9a08a6b06a5ba8725d0d7
|
|
19
|
-
06b313a5d792be254d33e087b7a4fafcdf819941b9bec4c6057d4c050bd01eb243efd4e6b707281b127820a2b734c6d8f6b2131bf0b5b215c7a798ff3fe90ceb
|
|
20
|
-
da91539fcc7b03d2b8b1381bd6023fff20278344ad944d364ba684842db3901c346335f0d455eda414f99c1e794a86aa3a90bcc6e085eecb0b4bf61198d16ed3
|
|
21
|
-
89cfa495f977a37a51502b2f60649f2efd7d89c757b6366776ba4c0612017bf1fbfc682dd62e9960d39cbea854d2dcc708b1db5d268192954d13ee72c0bb1bd8
|
|
22
|
-
558a3cf3b02b1cd795b40f7a57780391bb8724883d3f7764846c3823e165b3f8c025f59d896905f9a955478586ce57f820d958a01aa59a4cace7ecdf125df334
|
|
23
|
-
fa3de8e50aac96c1275591a1221c32a60a1513370a33a228e00894341b10cf44a6ae6ac250d17a364e956ab1a17b068df3fb2d5b5a672d8a409eeb8b6ca1ade6
|
|
24
|
-
`.replace(/\s/g, '');
|
|
25
|
-
export function withFakeRandom(provider, source = DEFAULT_ENTROPY) {
|
|
26
|
-
const sourceBytes = getPlatform().hexDecode(source);
|
|
27
|
-
let offset = 0;
|
|
28
|
-
function getRandomValues(buf) {
|
|
29
|
-
if (offset + buf.length > sourceBytes.length) {
|
|
30
|
-
throw new Error('not enough entropy');
|
|
31
|
-
}
|
|
32
|
-
buf.set(sourceBytes.subarray(offset, offset + buf.length));
|
|
33
|
-
offset += buf.length;
|
|
34
|
-
}
|
|
35
|
-
return new Proxy(provider, {
|
|
36
|
-
get(target, prop, receiver) {
|
|
37
|
-
if (prop === 'randomFill')
|
|
38
|
-
return getRandomValues;
|
|
39
|
-
// eslint-disable-next-line ts/no-unsafe-return
|
|
40
|
-
return Reflect.get(target, prop, receiver);
|
|
41
|
-
},
|
|
42
|
-
});
|
|
43
|
-
}
|
|
44
|
-
export function useFakeMathRandom(source = DEFAULT_ENTROPY) {
|
|
45
|
-
const sourceBytes = getPlatform().hexDecode(source);
|
|
46
|
-
const dv = dataViewFromBuffer(sourceBytes);
|
|
47
|
-
let spy;
|
|
48
|
-
beforeEach(() => {
|
|
49
|
-
let offset = 0;
|
|
50
|
-
spy = vi.spyOn(globalThis.Math, 'random').mockImplementation(() => {
|
|
51
|
-
const ret = dv.getUint32(offset, true) / 0xFFFFFFFF;
|
|
52
|
-
offset += 4;
|
|
53
|
-
return ret;
|
|
54
|
-
});
|
|
55
|
-
});
|
|
56
|
-
afterEach(() => {
|
|
57
|
-
spy.mockRestore();
|
|
58
|
-
});
|
|
59
|
-
}
|
|
60
|
-
export async function defaultTestCryptoProvider(source = DEFAULT_ENTROPY) {
|
|
61
|
-
const prov = withFakeRandom(defaultCryptoProvider, source);
|
|
62
|
-
await prov.initialize?.();
|
|
63
|
-
return prov;
|
|
64
|
-
}
|
|
65
|
-
export function testCryptoProvider(c) {
|
|
66
|
-
beforeAll(() => c.initialize?.());
|
|
67
|
-
const p = getPlatform();
|
|
68
|
-
function gzipSyncWrap(data) {
|
|
69
|
-
if (import.meta.env.TEST_ENV === 'browser') {
|
|
70
|
-
// @ts-expect-error fucking crutch because @jspm/core uses Buffer.isBuffer for some reason
|
|
71
|
-
data._isBuffer = true;
|
|
72
|
-
return new Uint8Array(gzipSync(data));
|
|
73
|
-
}
|
|
74
|
-
return gzipSync(data);
|
|
75
|
-
}
|
|
76
|
-
function inflateSyncWrap(data) {
|
|
77
|
-
if (import.meta.env.TEST_ENV === 'browser') {
|
|
78
|
-
// @ts-expect-error fucking crutch because @jspm/core uses Buffer.isBuffer for some reason
|
|
79
|
-
data._isBuffer = true;
|
|
80
|
-
return new Uint8Array(inflateSync(data));
|
|
81
|
-
}
|
|
82
|
-
return inflateSync(data);
|
|
83
|
-
}
|
|
84
|
-
it('should calculate sha1', () => {
|
|
85
|
-
expect(p.hexEncode(c.sha1(p.utf8Encode('')))).to.eq('da39a3ee5e6b4b0d3255bfef95601890afd80709');
|
|
86
|
-
expect(p.hexEncode(c.sha1(p.utf8Encode('hello')))).to.eq('aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d');
|
|
87
|
-
expect(p.hexEncode(c.sha1(p.hexDecode('aebb1f')))).to.eq('62849d15c5dea495916c5eea8dba5f9551288850');
|
|
88
|
-
});
|
|
89
|
-
it('should calculate sha256', () => {
|
|
90
|
-
expect(p.hexEncode(c.sha256(p.utf8Encode('')))).to.eq('e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855');
|
|
91
|
-
expect(p.hexEncode(c.sha256(p.utf8Encode('hello')))).to.eq('2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824');
|
|
92
|
-
expect(p.hexEncode(c.sha256(p.hexDecode('aebb1f')))).to.eq('2d29658aba48f2b286fe8bbddb931b7ad297e5adb5b9a6fc3aab67ef7fbf4e80');
|
|
93
|
-
});
|
|
94
|
-
it('should calculate hmac-sha256', async () => {
|
|
95
|
-
const key = p.hexDecode('aaeeff');
|
|
96
|
-
expect(p.hexEncode(await c.hmacSha256(p.utf8Encode(''), key))).to.eq('642711307c9e4437df09d6ebaa6bdc1b3a810c7f15c50fd1d0f8d7d5490f44dd');
|
|
97
|
-
expect(p.hexEncode(await c.hmacSha256(p.utf8Encode('hello'), key))).to.eq('39b00bab151f9868e6501655c580b5542954711181243474d46b894703b1c1c2');
|
|
98
|
-
expect(p.hexEncode(await c.hmacSha256(p.hexDecode('aebb1f'), key))).to.eq('a3a7273871808711cab17aba14f58e96f63f3ccfc5097d206f0f00ead2c3dd35');
|
|
99
|
-
});
|
|
100
|
-
it('should derive pbkdf2 key', async () => {
|
|
101
|
-
expect(p.hexEncode(await c.pbkdf2(p.utf8Encode('pbkdf2 test'), p.utf8Encode('some salt'), 10))).to.eq('e43276cfa27f135f261cec8ddcf593fd74ec251038e459c165461f2308f3a7235e0744ee1aed9710b00db28d1a2112e20fea3601c60e770ac57ffe6b33ca8be1');
|
|
102
|
-
});
|
|
103
|
-
it('should encrypt and decrypt aes-ctr', () => {
|
|
104
|
-
let aes = c.createAesCtr(p.hexDecode('d450aae0bf0060a4af1044886b42a13f7c506b35255d134a7e87ab3f23a9493b'), p.hexDecode('0182de2bd789c295c3c6c875c5e9e190'), true);
|
|
105
|
-
const data = p.hexDecode('7baae571e4c2f4cfadb1931d5923aca7');
|
|
106
|
-
expect(p.hexEncode(aes.process(data))).eq('df5647dbb70bc393f2fb05b72f42286f');
|
|
107
|
-
expect(p.hexEncode(aes.process(data))).eq('3917147082672516b3177150129bc579');
|
|
108
|
-
expect(p.hexEncode(aes.process(data))).eq('2a7a9089270a5de45d5e3dd399cac725');
|
|
109
|
-
expect(p.hexEncode(aes.process(data))).eq('56d085217771398ac13583de4d677dd8');
|
|
110
|
-
expect(p.hexEncode(aes.process(data))).eq('cc639b488126cf36e79c4515e8012b92');
|
|
111
|
-
expect(p.hexEncode(aes.process(data))).eq('01384d100646cd562cc5586ec3f8f8c4');
|
|
112
|
-
aes.close?.();
|
|
113
|
-
aes = c.createAesCtr(p.hexDecode('d450aae0bf0060a4af1044886b42a13f7c506b35255d134a7e87ab3f23a9493b'), p.hexDecode('0182de2bd789c295c3c6c875c5e9e190'), false);
|
|
114
|
-
expect(p.hexEncode(aes.process(p.hexDecode('df5647dbb70bc393f2fb05b72f42286f')))).eq(p.hexEncode(data));
|
|
115
|
-
expect(p.hexEncode(aes.process(p.hexDecode('3917147082672516b3177150129bc579')))).eq(p.hexEncode(data));
|
|
116
|
-
expect(p.hexEncode(aes.process(p.hexDecode('2a7a9089270a5de45d5e3dd399cac725')))).eq(p.hexEncode(data));
|
|
117
|
-
expect(p.hexEncode(aes.process(p.hexDecode('56d085217771398ac13583de4d677dd8')))).eq(p.hexEncode(data));
|
|
118
|
-
expect(p.hexEncode(aes.process(p.hexDecode('cc639b488126cf36e79c4515e8012b92')))).eq(p.hexEncode(data));
|
|
119
|
-
expect(p.hexEncode(aes.process(p.hexDecode('01384d100646cd562cc5586ec3f8f8c4')))).eq(p.hexEncode(data));
|
|
120
|
-
aes.close?.();
|
|
121
|
-
});
|
|
122
|
-
it('should encrypt and decrypt aes-ige', () => {
|
|
123
|
-
const aes = c.createAesIge(p.hexDecode('5468697320697320616E20696D706C655468697320697320616E20696D706C65'), p.hexDecode('6D656E746174696F6E206F6620494745206D6F646520666F72204F70656E5353'));
|
|
124
|
-
expect(p.hexEncode(aes.encrypt(p.hexDecode('99706487a1cde613bc6de0b6f24b1c7aa448c8b9c3403e3467a8cad89340f53b')))).to.eq('792ea8ae577b1a66cb3bd92679b8030ca54ee631976bd3a04547fdcb4639fa69');
|
|
125
|
-
expect(p.hexEncode(aes.decrypt(p.hexDecode('792ea8ae577b1a66cb3bd92679b8030ca54ee631976bd3a04547fdcb4639fa69')))).to.eq('99706487a1cde613bc6de0b6f24b1c7aa448c8b9c3403e3467a8cad89340f53b');
|
|
126
|
-
});
|
|
127
|
-
it('should decompose PQ to prime factors P and Q', async () => {
|
|
128
|
-
const testFactorization = async (pq, p_, q) => {
|
|
129
|
-
const [p1, q1] = await c.factorizePQ(p.hexDecode(pq));
|
|
130
|
-
expect(p.hexEncode(p1)).eq(p_.toLowerCase());
|
|
131
|
-
expect(p.hexEncode(q1)).eq(q.toLowerCase());
|
|
132
|
-
};
|
|
133
|
-
// from samples at https://core.telegram.org/mtproto/samples-auth_key
|
|
134
|
-
await testFactorization('17ED48941A08F981', '494C553B', '53911073');
|
|
135
|
-
// random example
|
|
136
|
-
await testFactorization('14fcab4dfc861f45', '494c5c99', '494c778d');
|
|
137
|
-
},
|
|
138
|
-
// since PQ factorization relies on RNG, it may take a while (or may not!)
|
|
139
|
-
{ timeout: 10000 });
|
|
140
|
-
it('should correctly gzip', () => {
|
|
141
|
-
const data = new Uint8Array(1000).fill(0x42);
|
|
142
|
-
const compressed = c.gzip(data, 100);
|
|
143
|
-
expect(compressed).not.toBeNull();
|
|
144
|
-
const decompressed = inflateSyncWrap(compressed);
|
|
145
|
-
expect(compressed.length).toBeLessThan(data.length);
|
|
146
|
-
expect(p.hexEncode(decompressed)).toEqual(p.hexEncode(data));
|
|
147
|
-
});
|
|
148
|
-
it('should correctly gunzip', () => {
|
|
149
|
-
const data = new Uint8Array(1000).fill(0x42);
|
|
150
|
-
const compressed = gzipSyncWrap(data);
|
|
151
|
-
const decompressed = c.gunzip(compressed);
|
|
152
|
-
expect(p.hexEncode(decompressed)).toEqual(p.hexEncode(data));
|
|
153
|
-
});
|
|
154
|
-
describe('randomBytes', () => {
|
|
155
|
-
it('should return exactly N bytes', () => {
|
|
156
|
-
expect(c.randomBytes(0).length).eq(0);
|
|
157
|
-
expect(c.randomBytes(5).length).eq(5);
|
|
158
|
-
expect(c.randomBytes(10).length).eq(10);
|
|
159
|
-
expect(c.randomBytes(256).length).eq(256);
|
|
160
|
-
});
|
|
161
|
-
it('should not be deterministic', () => {
|
|
162
|
-
expect([...c.randomBytes(8)]).not.eql([...c.randomBytes(8)]);
|
|
163
|
-
});
|
|
164
|
-
it('should use randomFill', () => {
|
|
165
|
-
const spy = vi.spyOn(c, 'randomFill');
|
|
166
|
-
c.randomBytes(8);
|
|
167
|
-
expect(spy).toHaveBeenCalled();
|
|
168
|
-
});
|
|
169
|
-
});
|
|
170
|
-
}
|
|
171
|
-
export function u8HexDecode(hex) {
|
|
172
|
-
const buf = getPlatform().hexDecode(hex);
|
|
173
|
-
// eslint-disable-next-line no-restricted-globals
|
|
174
|
-
if ((import.meta.env.TEST_ENV === 'node' || import.meta.env.TEST_ENV === 'bun') && Buffer.isBuffer(buf)) {
|
|
175
|
-
return new Uint8Array(buf);
|
|
176
|
-
}
|
|
177
|
-
return buf;
|
|
178
|
-
}
|
|
179
|
-
//# sourceMappingURL=crypto.js.map
|