@mtcute/test 0.17.0 → 0.18.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/client.js +223 -0
- package/crypto.d.ts +0 -1
- package/crypto.js +191 -0
- package/index.d.ts +0 -1
- package/index.js +9 -843
- package/package.json +19 -17
- package/platform.web.js +7 -0
- package/schema.js +29 -0
- package/storage/auth-keys.js +91 -0
- package/storage/key-value.js +37 -0
- package/storage/peers.js +83 -0
- package/storage/ref-messages.js +67 -0
- package/storage.d.ts +1 -1
- package/storage.js +45 -0
- package/stub.js +60 -0
- package/transport.d.ts +12 -22
- package/transport.js +28 -0
- package/utils.js +15 -0
- package/index.d.cts +0 -8
- package/platform.test.d.ts +0 -1
- package/transport.test.d.ts +0 -1
package/package.json
CHANGED
|
@@ -1,11 +1,21 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mtcute/test",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.18.0",
|
|
5
5
|
"description": "Test utilities for mtcute",
|
|
6
|
-
"author": "alina sireneva <alina@tei.su>",
|
|
7
6
|
"license": "MIT",
|
|
8
|
-
"
|
|
7
|
+
"dependencies": {
|
|
8
|
+
"long": "5.2.3",
|
|
9
|
+
"@fuman/utils": "0.0.4",
|
|
10
|
+
"@fuman/net": "0.0.4"
|
|
11
|
+
},
|
|
12
|
+
"peerDependencies": {
|
|
13
|
+
"@mtcute/core": "^0.18.0",
|
|
14
|
+
"@mtcute/node": "^0.18.0",
|
|
15
|
+
"@mtcute/tl": "195.0.0",
|
|
16
|
+
"@mtcute/web": "^0.18.0",
|
|
17
|
+
"vitest": "*"
|
|
18
|
+
},
|
|
9
19
|
"exports": {
|
|
10
20
|
".": {
|
|
11
21
|
"import": {
|
|
@@ -18,14 +28,8 @@
|
|
|
18
28
|
}
|
|
19
29
|
}
|
|
20
30
|
},
|
|
21
|
-
"
|
|
22
|
-
"
|
|
23
|
-
"@mtcute/core": "^0.17.0",
|
|
24
|
-
"@mtcute/node": "^0.17.0",
|
|
25
|
-
"@mtcute/tl": "*",
|
|
26
|
-
"@mtcute/web": "^0.17.0",
|
|
27
|
-
"vitest": "*"
|
|
28
|
-
},
|
|
31
|
+
"author": "alina sireneva <alina@tei.su>",
|
|
32
|
+
"sideEffects": false,
|
|
29
33
|
"peerDependenciesMeta": {
|
|
30
34
|
"@mtcute/node": {
|
|
31
35
|
"optional": true
|
|
@@ -34,15 +38,13 @@
|
|
|
34
38
|
"optional": true
|
|
35
39
|
}
|
|
36
40
|
},
|
|
37
|
-
"dependencies": {
|
|
38
|
-
"long": "5.2.3"
|
|
39
|
-
},
|
|
40
41
|
"browser": {
|
|
41
|
-
"./
|
|
42
|
+
"./src/platform.js": "./src/platform.web.js"
|
|
42
43
|
},
|
|
43
44
|
"homepage": "https://mtcute.dev",
|
|
44
45
|
"repository": {
|
|
45
46
|
"type": "git",
|
|
46
|
-
"url": "https://github.com/mtcute/mtcute"
|
|
47
|
-
}
|
|
47
|
+
"url": "git+https://github.com/mtcute/mtcute.git"
|
|
48
|
+
},
|
|
49
|
+
"scripts": {}
|
|
48
50
|
}
|
package/platform.web.js
ADDED
package/schema.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import * as schema_ from "@mtcute/tl/api-schema.json";
|
|
2
|
+
const schema = "default" in schema_ ? schema_.default : schema_;
|
|
3
|
+
let _cachedEntriesMap = null;
|
|
4
|
+
let _cachedUnionsMap = null;
|
|
5
|
+
function getEntriesMap() {
|
|
6
|
+
if (_cachedEntriesMap) {
|
|
7
|
+
return {
|
|
8
|
+
entries: _cachedEntriesMap,
|
|
9
|
+
unions: _cachedUnionsMap
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
_cachedEntriesMap = /* @__PURE__ */ new Map();
|
|
13
|
+
_cachedUnionsMap = /* @__PURE__ */ new Map();
|
|
14
|
+
let entry;
|
|
15
|
+
for (entry of schema.e) {
|
|
16
|
+
_cachedEntriesMap.set(entry.name, entry);
|
|
17
|
+
if (!_cachedUnionsMap.has(entry.type)) {
|
|
18
|
+
_cachedUnionsMap.set(entry.type, []);
|
|
19
|
+
}
|
|
20
|
+
_cachedUnionsMap.get(entry.type).push(entry);
|
|
21
|
+
}
|
|
22
|
+
return {
|
|
23
|
+
entries: _cachedEntriesMap,
|
|
24
|
+
unions: _cachedUnionsMap
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
export {
|
|
28
|
+
getEntriesMap
|
|
29
|
+
};
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { vi, describe, afterEach, it, expect } from "vitest";
|
|
2
|
+
function fakeAuthKeysRepository() {
|
|
3
|
+
return {
|
|
4
|
+
get: vi.fn(),
|
|
5
|
+
set: vi.fn(),
|
|
6
|
+
getTemp: vi.fn(),
|
|
7
|
+
setTemp: vi.fn(),
|
|
8
|
+
deleteByDc: vi.fn(),
|
|
9
|
+
deleteAll: vi.fn()
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
function fixBuffer(buf) {
|
|
13
|
+
if (!buf) return buf;
|
|
14
|
+
return typeof Buffer !== "undefined" && buf instanceof Buffer ? new Uint8Array(buf) : buf;
|
|
15
|
+
}
|
|
16
|
+
function testAuthKeysRepository(repo) {
|
|
17
|
+
const key2 = new Uint8Array(256).fill(66);
|
|
18
|
+
const key3 = new Uint8Array(256).fill(67);
|
|
19
|
+
const key2i0 = new Uint8Array(256).fill(68);
|
|
20
|
+
const key2i1 = new Uint8Array(256).fill(69);
|
|
21
|
+
const key3i0 = new Uint8Array(256).fill(70);
|
|
22
|
+
const key3i1 = new Uint8Array(256).fill(71);
|
|
23
|
+
describe("auth keys", () => {
|
|
24
|
+
afterEach(() => repo.deleteAll());
|
|
25
|
+
it("should be empty by default", async () => {
|
|
26
|
+
expect(fixBuffer(await repo.get(2))).toEqual(null);
|
|
27
|
+
expect(fixBuffer(await repo.get(3))).toEqual(null);
|
|
28
|
+
});
|
|
29
|
+
it("should store and retrieve auth keys", async () => {
|
|
30
|
+
await repo.set(2, key2);
|
|
31
|
+
await repo.set(3, key3);
|
|
32
|
+
expect(fixBuffer(await repo.get(2))).toEqual(key2);
|
|
33
|
+
expect(fixBuffer(await repo.get(3))).toEqual(key3);
|
|
34
|
+
});
|
|
35
|
+
it("should delete auth keys", async () => {
|
|
36
|
+
await repo.set(2, key2);
|
|
37
|
+
await repo.set(3, key3);
|
|
38
|
+
await repo.set(2, null);
|
|
39
|
+
await repo.set(3, null);
|
|
40
|
+
expect(fixBuffer(await repo.get(2))).toEqual(null);
|
|
41
|
+
expect(fixBuffer(await repo.get(3))).toEqual(null);
|
|
42
|
+
});
|
|
43
|
+
it("should store and retrieve temp auth keys", async () => {
|
|
44
|
+
await repo.setTemp(2, 0, key2i0, 1);
|
|
45
|
+
await repo.setTemp(2, 1, key2i1, 1);
|
|
46
|
+
await repo.setTemp(3, 0, key3i0, 1);
|
|
47
|
+
await repo.setTemp(3, 1, key3i1, 1);
|
|
48
|
+
expect(fixBuffer(await repo.getTemp(2, 0, 0))).toEqual(key2i0);
|
|
49
|
+
expect(fixBuffer(await repo.getTemp(2, 1, 0))).toEqual(key2i1);
|
|
50
|
+
expect(fixBuffer(await repo.getTemp(3, 0, 0))).toEqual(key3i0);
|
|
51
|
+
expect(fixBuffer(await repo.getTemp(3, 1, 0))).toEqual(key3i1);
|
|
52
|
+
expect(fixBuffer(await repo.getTemp(2, 0, 100))).toEqual(null);
|
|
53
|
+
expect(fixBuffer(await repo.getTemp(2, 1, 100))).toEqual(null);
|
|
54
|
+
expect(fixBuffer(await repo.getTemp(3, 0, 100))).toEqual(null);
|
|
55
|
+
expect(fixBuffer(await repo.getTemp(3, 1, 100))).toEqual(null);
|
|
56
|
+
});
|
|
57
|
+
it("should delete temp auth keys", async () => {
|
|
58
|
+
await repo.setTemp(2, 0, key2i0, 1);
|
|
59
|
+
await repo.setTemp(2, 1, key2i1, 1);
|
|
60
|
+
await repo.setTemp(3, 0, key3i0, 1);
|
|
61
|
+
await repo.setTemp(3, 1, key3i1, 1);
|
|
62
|
+
await repo.setTemp(2, 0, null, 1);
|
|
63
|
+
await repo.setTemp(2, 1, null, 1);
|
|
64
|
+
await repo.setTemp(3, 0, null, 1);
|
|
65
|
+
await repo.setTemp(3, 1, null, 1);
|
|
66
|
+
expect(fixBuffer(await repo.getTemp(2, 0, 0))).toEqual(null);
|
|
67
|
+
expect(fixBuffer(await repo.getTemp(2, 1, 0))).toEqual(null);
|
|
68
|
+
expect(fixBuffer(await repo.getTemp(3, 0, 0))).toEqual(null);
|
|
69
|
+
expect(fixBuffer(await repo.getTemp(3, 1, 0))).toEqual(null);
|
|
70
|
+
});
|
|
71
|
+
it("should delete all auth keys by DC", async () => {
|
|
72
|
+
await repo.set(2, key2);
|
|
73
|
+
await repo.set(3, key3);
|
|
74
|
+
await repo.setTemp(2, 0, key2i0, 1);
|
|
75
|
+
await repo.setTemp(2, 1, key2i1, 1);
|
|
76
|
+
await repo.setTemp(3, 0, key3i0, 1);
|
|
77
|
+
await repo.setTemp(3, 1, key3i1, 1);
|
|
78
|
+
await repo.deleteByDc(2);
|
|
79
|
+
expect(fixBuffer(await repo.get(2))).toEqual(null);
|
|
80
|
+
expect(fixBuffer(await repo.get(3))).toEqual(key3);
|
|
81
|
+
expect(fixBuffer(await repo.getTemp(2, 0, 0))).toEqual(null);
|
|
82
|
+
expect(fixBuffer(await repo.getTemp(2, 1, 0))).toEqual(null);
|
|
83
|
+
expect(fixBuffer(await repo.getTemp(3, 0, 0))).toEqual(key3i0);
|
|
84
|
+
expect(fixBuffer(await repo.getTemp(3, 1, 0))).toEqual(key3i1);
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
export {
|
|
89
|
+
fakeAuthKeysRepository,
|
|
90
|
+
testAuthKeysRepository
|
|
91
|
+
};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { vi, describe, afterEach, it, expect } from "vitest";
|
|
2
|
+
function fakeKeyValueRepository() {
|
|
3
|
+
return {
|
|
4
|
+
get: vi.fn(),
|
|
5
|
+
set: vi.fn(),
|
|
6
|
+
delete: vi.fn(),
|
|
7
|
+
deleteAll: vi.fn()
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
function fixBuffer(buf) {
|
|
11
|
+
if (!buf) return buf;
|
|
12
|
+
return typeof Buffer !== "undefined" && buf instanceof Buffer ? new Uint8Array(buf) : buf;
|
|
13
|
+
}
|
|
14
|
+
function testKeyValueRepository(repo, driver) {
|
|
15
|
+
describe("key-value", () => {
|
|
16
|
+
afterEach(() => repo.deleteAll());
|
|
17
|
+
it("should be empty by default", async () => {
|
|
18
|
+
expect(fixBuffer(await repo.get("key"))).toEqual(null);
|
|
19
|
+
});
|
|
20
|
+
it("should store and retrieve values", async () => {
|
|
21
|
+
await repo.set("key", new Uint8Array([1, 2, 3]));
|
|
22
|
+
await driver.save?.();
|
|
23
|
+
expect(fixBuffer(await repo.get("key"))).toEqual(new Uint8Array([1, 2, 3]));
|
|
24
|
+
});
|
|
25
|
+
it("should delete values", async () => {
|
|
26
|
+
await repo.set("key", new Uint8Array([1, 2, 3]));
|
|
27
|
+
await driver.save?.();
|
|
28
|
+
await repo.delete("key");
|
|
29
|
+
await driver.save?.();
|
|
30
|
+
expect(fixBuffer(await repo.get("key"))).toEqual(null);
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
export {
|
|
35
|
+
fakeKeyValueRepository,
|
|
36
|
+
testKeyValueRepository
|
|
37
|
+
};
|
package/storage/peers.js
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { TlBinaryWriter } from "@mtcute/core/utils.js";
|
|
2
|
+
import { __tlWriterMap } from "@mtcute/tl/binary/writer.js";
|
|
3
|
+
import { vi, describe, it, expect } from "vitest";
|
|
4
|
+
import { createStub } from "../stub.js";
|
|
5
|
+
function fakePeersRepository() {
|
|
6
|
+
return {
|
|
7
|
+
getById: vi.fn(),
|
|
8
|
+
getByUsername: vi.fn(),
|
|
9
|
+
getByPhone: vi.fn(),
|
|
10
|
+
store: vi.fn(),
|
|
11
|
+
deleteAll: vi.fn()
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
function fixPeerInfo(peer) {
|
|
15
|
+
if (!peer) return peer;
|
|
16
|
+
return {
|
|
17
|
+
...peer,
|
|
18
|
+
complete: (
|
|
19
|
+
// eslint-disable-next-line no-restricted-globals
|
|
20
|
+
typeof Buffer !== "undefined" && peer.complete instanceof Buffer ? new Uint8Array(peer.complete) : peer.complete
|
|
21
|
+
)
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
function testPeersRepository(repo, driver) {
|
|
25
|
+
const stubPeerUser = {
|
|
26
|
+
id: 123123,
|
|
27
|
+
accessHash: "123|456",
|
|
28
|
+
isMin: false,
|
|
29
|
+
usernames: ["some_user"],
|
|
30
|
+
phone: "78005553535",
|
|
31
|
+
updated: 666,
|
|
32
|
+
complete: TlBinaryWriter.serializeObject(__tlWriterMap, createStub("user", { id: 123123 }))
|
|
33
|
+
};
|
|
34
|
+
const stubPeerChannel = {
|
|
35
|
+
id: -1001183945448,
|
|
36
|
+
accessHash: "666|555",
|
|
37
|
+
isMin: false,
|
|
38
|
+
usernames: ["some_channel"],
|
|
39
|
+
updated: 777,
|
|
40
|
+
complete: TlBinaryWriter.serializeObject(__tlWriterMap, createStub("channel", { id: 123123 }))
|
|
41
|
+
};
|
|
42
|
+
const stupPeerMinUser = { ...stubPeerUser, isMin: true };
|
|
43
|
+
describe("peers", () => {
|
|
44
|
+
it("should be empty by default", async () => {
|
|
45
|
+
expect(await repo.getById(123123, false)).toEqual(null);
|
|
46
|
+
expect(await repo.getByUsername("some_user")).toEqual(null);
|
|
47
|
+
expect(await repo.getByPhone("phone")).toEqual(null);
|
|
48
|
+
});
|
|
49
|
+
it("should store and retrieve peers", async () => {
|
|
50
|
+
await repo.store(stubPeerUser);
|
|
51
|
+
await repo.store(stubPeerChannel);
|
|
52
|
+
await driver.save?.();
|
|
53
|
+
expect(fixPeerInfo(await repo.getById(123123, false))).toEqual(stubPeerUser);
|
|
54
|
+
expect(fixPeerInfo(await repo.getByUsername("some_user"))).toEqual(stubPeerUser);
|
|
55
|
+
expect(fixPeerInfo(await repo.getByPhone("78005553535"))).toEqual(stubPeerUser);
|
|
56
|
+
expect(fixPeerInfo(await repo.getById(-1001183945448, false))).toEqual(stubPeerChannel);
|
|
57
|
+
expect(fixPeerInfo(await repo.getByUsername("some_channel"))).toEqual(stubPeerChannel);
|
|
58
|
+
});
|
|
59
|
+
it("should update peers usernames", async () => {
|
|
60
|
+
await repo.store(stubPeerUser);
|
|
61
|
+
await driver.save?.();
|
|
62
|
+
const modUser = { ...stubPeerUser, usernames: ["some_user2"] };
|
|
63
|
+
await repo.store(modUser);
|
|
64
|
+
await driver.save?.();
|
|
65
|
+
expect(fixPeerInfo(await repo.getById(123123, false))).toEqual(modUser);
|
|
66
|
+
expect(await repo.getByUsername("some_user")).toEqual(null);
|
|
67
|
+
expect(fixPeerInfo(await repo.getByUsername("some_user2"))).toEqual(modUser);
|
|
68
|
+
});
|
|
69
|
+
it("should not return min peers by default", async () => {
|
|
70
|
+
await repo.deleteAll();
|
|
71
|
+
await repo.store(stupPeerMinUser);
|
|
72
|
+
await driver.save?.();
|
|
73
|
+
expect(await repo.getById(123123, false)).toEqual(null);
|
|
74
|
+
expect(await repo.getByUsername("some_user")).toEqual(null);
|
|
75
|
+
expect(await repo.getByPhone("78005553535")).toEqual(null);
|
|
76
|
+
expect(fixPeerInfo(await repo.getById(123123, true))).toEqual(stupPeerMinUser);
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
export {
|
|
81
|
+
fakePeersRepository,
|
|
82
|
+
testPeersRepository
|
|
83
|
+
};
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { vi, describe, afterEach, it, expect } from "vitest";
|
|
2
|
+
function fakeRefMessagesRepository() {
|
|
3
|
+
return {
|
|
4
|
+
store: vi.fn(),
|
|
5
|
+
getByPeer: vi.fn(),
|
|
6
|
+
delete: vi.fn(),
|
|
7
|
+
deleteByPeer: vi.fn(),
|
|
8
|
+
deleteAll: vi.fn()
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
function testRefMessagesRepository(repo, driver) {
|
|
12
|
+
describe("IReferenceMessagesRepository", () => {
|
|
13
|
+
afterEach(() => repo.deleteAll());
|
|
14
|
+
it("should be empty by default", async () => {
|
|
15
|
+
expect(await repo.getByPeer(1)).toEqual(null);
|
|
16
|
+
});
|
|
17
|
+
it("should store and retrieve reference messages", async () => {
|
|
18
|
+
await repo.store(1, 2, 3);
|
|
19
|
+
await repo.store(1, 4, 5);
|
|
20
|
+
await repo.store(2, 6, 7);
|
|
21
|
+
await driver.save?.();
|
|
22
|
+
expect(await repo.getByPeer(1)).deep.oneOf([
|
|
23
|
+
[2, 3],
|
|
24
|
+
[4, 5]
|
|
25
|
+
]);
|
|
26
|
+
expect(await repo.getByPeer(2)).toEqual([6, 7]);
|
|
27
|
+
expect(await repo.getByPeer(3)).toEqual(null);
|
|
28
|
+
expect(await repo.getByPeer(4)).toEqual(null);
|
|
29
|
+
expect(await repo.getByPeer(5)).toEqual(null);
|
|
30
|
+
expect(await repo.getByPeer(6)).toEqual(null);
|
|
31
|
+
expect(await repo.getByPeer(7)).toEqual(null);
|
|
32
|
+
});
|
|
33
|
+
it("should delete reference messages", async () => {
|
|
34
|
+
await repo.store(1, 2, 3);
|
|
35
|
+
await repo.store(1, 4, 5);
|
|
36
|
+
await repo.store(2, 6, 7);
|
|
37
|
+
await driver.save?.();
|
|
38
|
+
await repo.delete(4, [5]);
|
|
39
|
+
await driver.save?.();
|
|
40
|
+
expect(await repo.getByPeer(1)).toEqual([2, 3]);
|
|
41
|
+
await repo.delete(2, [2, 3, 4]);
|
|
42
|
+
await driver.save?.();
|
|
43
|
+
expect(await repo.getByPeer(1)).toEqual(null);
|
|
44
|
+
});
|
|
45
|
+
it("should delete all reference messages for a peer", async () => {
|
|
46
|
+
await repo.store(1, 2, 3);
|
|
47
|
+
await repo.store(1, 4, 5);
|
|
48
|
+
await repo.store(1, 6, 7);
|
|
49
|
+
await repo.store(2, 20, 30);
|
|
50
|
+
await repo.store(2, 40, 50);
|
|
51
|
+
await repo.store(2, 60, 70);
|
|
52
|
+
await driver.save?.();
|
|
53
|
+
await repo.deleteByPeer(1);
|
|
54
|
+
await driver.save?.();
|
|
55
|
+
expect(await repo.getByPeer(1)).toEqual(null);
|
|
56
|
+
expect(await repo.getByPeer(2)).deep.oneOf([
|
|
57
|
+
[20, 30],
|
|
58
|
+
[40, 50],
|
|
59
|
+
[60, 70]
|
|
60
|
+
]);
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
export {
|
|
65
|
+
fakeRefMessagesRepository,
|
|
66
|
+
testRefMessagesRepository
|
|
67
|
+
};
|
package/storage.d.ts
CHANGED
package/storage.js
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { MemoryStorage, MtArgumentError } from "@mtcute/core";
|
|
2
|
+
import { createAesIgeForMessage } from "@mtcute/core/utils.js";
|
|
3
|
+
class StubMemoryTelegramStorage extends MemoryStorage {
|
|
4
|
+
constructor(params = {
|
|
5
|
+
hasKeys: true,
|
|
6
|
+
hasTempKeys: true
|
|
7
|
+
}) {
|
|
8
|
+
super();
|
|
9
|
+
this.params = params;
|
|
10
|
+
const _origGet = this.authKeys.get;
|
|
11
|
+
this.authKeys.get = (dcId) => {
|
|
12
|
+
if (this.params.hasKeys) {
|
|
13
|
+
if (this.params.hasKeys === true || this.params.hasKeys.includes(dcId)) {
|
|
14
|
+
return new Uint8Array(256);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
return _origGet.call(this.authKeys, dcId);
|
|
18
|
+
};
|
|
19
|
+
const _origGetTemp = this.authKeys.getTemp;
|
|
20
|
+
this.authKeys.getTemp = (dcId, idx, now) => {
|
|
21
|
+
if (this.params.hasTempKeys) {
|
|
22
|
+
if (this.params.hasTempKeys === true || this.params.hasTempKeys.includes(dcId)) {
|
|
23
|
+
return new Uint8Array(256);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return _origGetTemp.call(this.authKeys, dcId, idx, now);
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
decryptOutgoingMessage(crypto, data, dcId, tempIndex) {
|
|
30
|
+
const key = tempIndex ? this.authKeys.getTemp(dcId, tempIndex, Date.now()) : this.authKeys.get(dcId);
|
|
31
|
+
if (!key) {
|
|
32
|
+
throw new MtArgumentError(`No auth key for DC ${dcId}`);
|
|
33
|
+
}
|
|
34
|
+
const messageKey = data.subarray(8, 24);
|
|
35
|
+
const encryptedData = data.subarray(24);
|
|
36
|
+
const ige = createAesIgeForMessage(crypto, key, messageKey, true);
|
|
37
|
+
const innerData = ige.decrypt(encryptedData);
|
|
38
|
+
const dv = new DataView(innerData.buffer, innerData.byteOffset, innerData.byteLength);
|
|
39
|
+
const length = dv.getUint32(28, true);
|
|
40
|
+
return innerData.subarray(32, 32 + length);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
export {
|
|
44
|
+
StubMemoryTelegramStorage
|
|
45
|
+
};
|
package/stub.js
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { u8 } from "@fuman/utils";
|
|
2
|
+
import Long from "long";
|
|
3
|
+
import { getEntriesMap } from "./schema.js";
|
|
4
|
+
function getDefaultFor(arg) {
|
|
5
|
+
if (arg.typeModifiers?.isVector || arg.typeModifiers?.isBareVector) {
|
|
6
|
+
return [];
|
|
7
|
+
}
|
|
8
|
+
if (arg.typeModifiers?.predicate) {
|
|
9
|
+
return arg.type === "true" ? false : void 0;
|
|
10
|
+
}
|
|
11
|
+
switch (arg.type) {
|
|
12
|
+
case "int":
|
|
13
|
+
case "int53":
|
|
14
|
+
case "double":
|
|
15
|
+
return 0;
|
|
16
|
+
case "long":
|
|
17
|
+
return Long.ZERO;
|
|
18
|
+
case "int128":
|
|
19
|
+
return u8.alloc(16);
|
|
20
|
+
case "int256":
|
|
21
|
+
return u8.alloc(32);
|
|
22
|
+
case "string":
|
|
23
|
+
return "";
|
|
24
|
+
case "bytes":
|
|
25
|
+
return u8.alloc(0);
|
|
26
|
+
case "Bool":
|
|
27
|
+
case "bool":
|
|
28
|
+
return false;
|
|
29
|
+
default: {
|
|
30
|
+
const union = getEntriesMap().unions.get(arg.type);
|
|
31
|
+
if (!union) throw new Error(`Unknown type ${arg.type}`);
|
|
32
|
+
return createStub(union[0].name);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
function snakeToCamel(s) {
|
|
37
|
+
return s.replace(/(?<!^|_)_[a-z0-9]/gi, ($1) => {
|
|
38
|
+
return $1.substring(1).toUpperCase();
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
function createStub(name, partial = {}) {
|
|
42
|
+
const { entries } = getEntriesMap();
|
|
43
|
+
const entry = entries.get(name);
|
|
44
|
+
if (!entry) throw new Error(`Entry ${name} is unknown`);
|
|
45
|
+
const ret = {
|
|
46
|
+
_: name
|
|
47
|
+
};
|
|
48
|
+
for (const arg of entry.arguments) {
|
|
49
|
+
if (arg.type === "#") continue;
|
|
50
|
+
if (arg.name in partial) continue;
|
|
51
|
+
ret[snakeToCamel(arg.name)] = getDefaultFor(arg);
|
|
52
|
+
}
|
|
53
|
+
for (const key in partial) {
|
|
54
|
+
ret[key] = partial[key];
|
|
55
|
+
}
|
|
56
|
+
return ret;
|
|
57
|
+
}
|
|
58
|
+
export {
|
|
59
|
+
createStub
|
|
60
|
+
};
|
package/transport.d.ts
CHANGED
|
@@ -1,29 +1,19 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
import { tl } from '@mtcute/tl';
|
|
5
|
-
export declare class StubTelegramTransport extends EventEmitter implements ITelegramTransport {
|
|
1
|
+
import { BasicDcOption } from '@mtcute/core/utils.js';
|
|
2
|
+
import { IPacketCodec, ITelegramConnection, TelegramTransport } from '@mtcute/core';
|
|
3
|
+
export declare class StubTelegramTransport implements TelegramTransport {
|
|
6
4
|
readonly params: {
|
|
7
|
-
|
|
8
|
-
onConnect?: (dc:
|
|
5
|
+
packetCodec?: () => IPacketCodec;
|
|
6
|
+
onConnect?: (dc: BasicDcOption) => void;
|
|
9
7
|
onClose?: () => void;
|
|
10
|
-
onMessage?: (msg: Uint8Array) => void;
|
|
8
|
+
onMessage?: (msg: Uint8Array, dcId: number) => void;
|
|
11
9
|
};
|
|
12
10
|
constructor(params: {
|
|
13
|
-
|
|
14
|
-
onConnect?: (dc:
|
|
11
|
+
packetCodec?: () => IPacketCodec;
|
|
12
|
+
onConnect?: (dc: BasicDcOption) => void;
|
|
15
13
|
onClose?: () => void;
|
|
16
|
-
onMessage?: (msg: Uint8Array) => void;
|
|
14
|
+
onMessage?: (msg: Uint8Array, dcId: number) => void;
|
|
17
15
|
});
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
_log: Logger;
|
|
22
|
-
write(data: Uint8Array): void;
|
|
23
|
-
setup(crypto: ICryptoProvider, log: Logger): void;
|
|
24
|
-
state(): TransportState;
|
|
25
|
-
currentDc(): tl.RawDcOption | null;
|
|
26
|
-
connect(dc: tl.RawDcOption, testMode: boolean): void;
|
|
27
|
-
close(): void;
|
|
28
|
-
send(data: Uint8Array): Promise<void>;
|
|
16
|
+
private _dcId;
|
|
17
|
+
connect(dc: BasicDcOption): Promise<ITelegramConnection>;
|
|
18
|
+
packetCodec(): IPacketCodec;
|
|
29
19
|
}
|
package/transport.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { FakeConnection } from "@fuman/net";
|
|
2
|
+
import { IntermediatePacketCodec } from "@mtcute/core";
|
|
3
|
+
class StubTelegramTransport {
|
|
4
|
+
constructor(params) {
|
|
5
|
+
this.params = params;
|
|
6
|
+
}
|
|
7
|
+
_dcId = 0;
|
|
8
|
+
async connect(dc) {
|
|
9
|
+
this.params.onConnect?.(dc);
|
|
10
|
+
this._dcId = dc.id;
|
|
11
|
+
return new FakeConnection(dc);
|
|
12
|
+
}
|
|
13
|
+
packetCodec() {
|
|
14
|
+
const inner = this.params.packetCodec?.() ?? new IntermediatePacketCodec();
|
|
15
|
+
return {
|
|
16
|
+
decode: (reader, eof) => inner.decode(reader, eof),
|
|
17
|
+
reset: () => inner.reset(),
|
|
18
|
+
tag: () => inner.tag(),
|
|
19
|
+
encode: (message, into) => {
|
|
20
|
+
this.params.onMessage?.(message, this._dcId);
|
|
21
|
+
return inner.encode(message, into);
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
export {
|
|
27
|
+
StubTelegramTransport
|
|
28
|
+
};
|
package/utils.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { parseMarkedPeerId } from "@mtcute/core";
|
|
2
|
+
function markedIdToPeer(id) {
|
|
3
|
+
const [type, bareId] = parseMarkedPeerId(id);
|
|
4
|
+
switch (type) {
|
|
5
|
+
case "user":
|
|
6
|
+
return { _: "peerUser", userId: bareId };
|
|
7
|
+
case "chat":
|
|
8
|
+
return { _: "peerChat", chatId: bareId };
|
|
9
|
+
case "channel":
|
|
10
|
+
return { _: "peerChannel", channelId: bareId };
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
export {
|
|
14
|
+
markedIdToPeer
|
|
15
|
+
};
|
package/index.d.cts
DELETED
package/platform.test.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/transport.test.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|