@mtcute/convert 0.16.0 → 0.16.9

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.
Files changed (173) hide show
  1. package/README.md +1 -1
  2. package/gramjs/__fixtures__/session.d.ts +1 -0
  3. package/gramjs/serialize.test.d.ts +1 -0
  4. package/index.cjs +493 -0
  5. package/index.d.ts +5 -1
  6. package/index.js +488 -1
  7. package/mtkruto/__fixtures__/session.d.ts +1 -0
  8. package/mtkruto/convert.test.d.ts +1 -0
  9. package/mtkruto/parse.test.d.ts +1 -0
  10. package/mtkruto/serialize.test.d.ts +1 -0
  11. package/package.json +27 -23
  12. package/pyrogram/__fixtures__/session.d.ts +1 -0
  13. package/pyrogram/__fixtures__/session_old.d.ts +1 -0
  14. package/pyrogram/convert.test.d.ts +1 -0
  15. package/pyrogram/parse.test.d.ts +1 -0
  16. package/pyrogram/serialize.test.d.ts +1 -0
  17. package/telethon/__fixtures__/session.d.ts +1 -0
  18. package/telethon/__fixtures__/session_v6.d.ts +1 -0
  19. package/telethon/convert.test.d.ts +1 -0
  20. package/telethon/parse.test.d.ts +1 -0
  21. package/telethon/serialize.test.d.ts +1 -0
  22. package/types.d.ts +0 -0
  23. package/cjs/dcs.js +0 -113
  24. package/cjs/dcs.js.map +0 -1
  25. package/cjs/gramjs/convert.js +0 -28
  26. package/cjs/gramjs/convert.js.map +0 -1
  27. package/cjs/gramjs/index.js +0 -21
  28. package/cjs/gramjs/index.js.map +0 -1
  29. package/cjs/gramjs/parse.js +0 -31
  30. package/cjs/gramjs/parse.js.map +0 -1
  31. package/cjs/gramjs/serialize.js +0 -24
  32. package/cjs/gramjs/serialize.js.map +0 -1
  33. package/cjs/gramjs/types.js +0 -3
  34. package/cjs/gramjs/types.js.map +0 -1
  35. package/cjs/index.js +0 -28
  36. package/cjs/index.js.map +0 -1
  37. package/cjs/mtkruto/convert.js +0 -31
  38. package/cjs/mtkruto/convert.js.map +0 -1
  39. package/cjs/mtkruto/index.js +0 -21
  40. package/cjs/mtkruto/index.js.map +0 -1
  41. package/cjs/mtkruto/parse.js +0 -28
  42. package/cjs/mtkruto/parse.js.map +0 -1
  43. package/cjs/mtkruto/serialize.js +0 -15
  44. package/cjs/mtkruto/serialize.js.map +0 -1
  45. package/cjs/mtkruto/types.js +0 -3
  46. package/cjs/mtkruto/types.js.map +0 -1
  47. package/cjs/package.json +0 -3
  48. package/cjs/pyrogram/convert.js +0 -40
  49. package/cjs/pyrogram/convert.js.map +0 -1
  50. package/cjs/pyrogram/index.js +0 -21
  51. package/cjs/pyrogram/index.js.map +0 -1
  52. package/cjs/pyrogram/parse.js +0 -56
  53. package/cjs/pyrogram/parse.js.map +0 -1
  54. package/cjs/pyrogram/serialize.js +0 -40
  55. package/cjs/pyrogram/serialize.js.map +0 -1
  56. package/cjs/pyrogram/types.js +0 -3
  57. package/cjs/pyrogram/types.js.map +0 -1
  58. package/cjs/telethon/convert.js +0 -44
  59. package/cjs/telethon/convert.js.map +0 -1
  60. package/cjs/telethon/index.js +0 -21
  61. package/cjs/telethon/index.js.map +0 -1
  62. package/cjs/telethon/parse.js +0 -33
  63. package/cjs/telethon/parse.js.map +0 -1
  64. package/cjs/telethon/serialize.js +0 -34
  65. package/cjs/telethon/serialize.js.map +0 -1
  66. package/cjs/telethon/types.js +0 -3
  67. package/cjs/telethon/types.js.map +0 -1
  68. package/cjs/types.js +0 -3
  69. package/cjs/types.js.map +0 -1
  70. package/cjs/utils/ip.js +0 -45
  71. package/cjs/utils/ip.js.map +0 -1
  72. package/cjs/utils/rle.js +0 -55
  73. package/cjs/utils/rle.js.map +0 -1
  74. package/esm/dcs.d.ts +0 -4
  75. package/esm/dcs.js +0 -109
  76. package/esm/dcs.js.map +0 -1
  77. package/esm/gramjs/convert.d.ts +0 -4
  78. package/esm/gramjs/convert.js +0 -23
  79. package/esm/gramjs/convert.js.map +0 -1
  80. package/esm/gramjs/index.d.ts +0 -4
  81. package/esm/gramjs/index.js +0 -5
  82. package/esm/gramjs/index.js.map +0 -1
  83. package/esm/gramjs/parse.d.ts +0 -2
  84. package/esm/gramjs/parse.js +0 -27
  85. package/esm/gramjs/parse.js.map +0 -1
  86. package/esm/gramjs/serialize.d.ts +0 -2
  87. package/esm/gramjs/serialize.js +0 -20
  88. package/esm/gramjs/serialize.js.map +0 -1
  89. package/esm/gramjs/types.d.ts +0 -7
  90. package/esm/gramjs/types.js +0 -2
  91. package/esm/gramjs/types.js.map +0 -1
  92. package/esm/index.d.ts +0 -5
  93. package/esm/index.js +0 -6
  94. package/esm/index.js.map +0 -1
  95. package/esm/mtkruto/convert.d.ts +0 -4
  96. package/esm/mtkruto/convert.js +0 -26
  97. package/esm/mtkruto/convert.js.map +0 -1
  98. package/esm/mtkruto/index.d.ts +0 -4
  99. package/esm/mtkruto/index.js +0 -5
  100. package/esm/mtkruto/index.js.map +0 -1
  101. package/esm/mtkruto/parse.d.ts +0 -2
  102. package/esm/mtkruto/parse.js +0 -24
  103. package/esm/mtkruto/parse.js.map +0 -1
  104. package/esm/mtkruto/serialize.d.ts +0 -2
  105. package/esm/mtkruto/serialize.js +0 -11
  106. package/esm/mtkruto/serialize.js.map +0 -1
  107. package/esm/mtkruto/types.d.ts +0 -5
  108. package/esm/mtkruto/types.js +0 -2
  109. package/esm/mtkruto/types.js.map +0 -1
  110. package/esm/pyrogram/convert.d.ts +0 -6
  111. package/esm/pyrogram/convert.js +0 -35
  112. package/esm/pyrogram/convert.js.map +0 -1
  113. package/esm/pyrogram/index.d.ts +0 -4
  114. package/esm/pyrogram/index.js +0 -5
  115. package/esm/pyrogram/index.js.map +0 -1
  116. package/esm/pyrogram/parse.d.ts +0 -2
  117. package/esm/pyrogram/parse.js +0 -52
  118. package/esm/pyrogram/parse.js.map +0 -1
  119. package/esm/pyrogram/serialize.d.ts +0 -2
  120. package/esm/pyrogram/serialize.js +0 -36
  121. package/esm/pyrogram/serialize.js.map +0 -1
  122. package/esm/pyrogram/types.d.ts +0 -8
  123. package/esm/pyrogram/types.js +0 -2
  124. package/esm/pyrogram/types.js.map +0 -1
  125. package/esm/telethon/convert.d.ts +0 -4
  126. package/esm/telethon/convert.js +0 -39
  127. package/esm/telethon/convert.js.map +0 -1
  128. package/esm/telethon/index.d.ts +0 -4
  129. package/esm/telethon/index.js +0 -5
  130. package/esm/telethon/index.js.map +0 -1
  131. package/esm/telethon/parse.d.ts +0 -2
  132. package/esm/telethon/parse.js +0 -29
  133. package/esm/telethon/parse.js.map +0 -1
  134. package/esm/telethon/serialize.d.ts +0 -2
  135. package/esm/telethon/serialize.js +0 -30
  136. package/esm/telethon/serialize.js.map +0 -1
  137. package/esm/telethon/types.d.ts +0 -7
  138. package/esm/telethon/types.js +0 -2
  139. package/esm/telethon/types.js.map +0 -1
  140. package/esm/types.js +0 -2
  141. package/esm/types.js.map +0 -1
  142. package/esm/utils/ip.d.ts +0 -3
  143. package/esm/utils/ip.js +0 -39
  144. package/esm/utils/ip.js.map +0 -1
  145. package/esm/utils/rle.d.ts +0 -3
  146. package/esm/utils/rle.js +0 -49
  147. package/esm/utils/rle.js.map +0 -1
  148. /package/{cjs/dcs.d.ts → dcs.d.ts} +0 -0
  149. /package/{cjs/gramjs → gramjs}/convert.d.ts +0 -0
  150. /package/{cjs/types.d.ts → gramjs/convert.test.d.ts} +0 -0
  151. /package/{cjs/gramjs → gramjs}/index.d.ts +0 -0
  152. /package/{cjs/gramjs → gramjs}/parse.d.ts +0 -0
  153. /package/{esm/types.d.ts → gramjs/parse.test.d.ts} +0 -0
  154. /package/{cjs/gramjs → gramjs}/serialize.d.ts +0 -0
  155. /package/{cjs/gramjs → gramjs}/types.d.ts +0 -0
  156. /package/{cjs/index.d.ts → index.d.cts} +0 -0
  157. /package/{cjs/mtkruto → mtkruto}/convert.d.ts +0 -0
  158. /package/{cjs/mtkruto → mtkruto}/index.d.ts +0 -0
  159. /package/{cjs/mtkruto → mtkruto}/parse.d.ts +0 -0
  160. /package/{cjs/mtkruto → mtkruto}/serialize.d.ts +0 -0
  161. /package/{cjs/mtkruto → mtkruto}/types.d.ts +0 -0
  162. /package/{cjs/pyrogram → pyrogram}/convert.d.ts +0 -0
  163. /package/{cjs/pyrogram → pyrogram}/index.d.ts +0 -0
  164. /package/{cjs/pyrogram → pyrogram}/parse.d.ts +0 -0
  165. /package/{cjs/pyrogram → pyrogram}/serialize.d.ts +0 -0
  166. /package/{cjs/pyrogram → pyrogram}/types.d.ts +0 -0
  167. /package/{cjs/telethon → telethon}/convert.d.ts +0 -0
  168. /package/{cjs/telethon → telethon}/index.d.ts +0 -0
  169. /package/{cjs/telethon → telethon}/parse.d.ts +0 -0
  170. /package/{cjs/telethon → telethon}/serialize.d.ts +0 -0
  171. /package/{cjs/telethon → telethon}/types.d.ts +0 -0
  172. /package/{cjs/utils → utils}/ip.d.ts +0 -0
  173. /package/{cjs/utils → utils}/rle.d.ts +0 -0
package/index.js CHANGED
@@ -1 +1,488 @@
1
- export * from './esm/index.js'
1
+ import { dataViewFromBuffer, readStringSession, TlBinaryReader, TlBinaryWriter, longFromBuffer } from "@mtcute/core/utils.js";
2
+ import { MtArgumentError, Long } from "@mtcute/core";
3
+ import { getPlatform } from "@mtcute/core/platform.js";
4
+ const DC_MAPPING_PROD = {
5
+ 1: {
6
+ main: {
7
+ id: 1,
8
+ ipAddress: "149.154.175.56",
9
+ port: 443
10
+ },
11
+ media: {
12
+ id: 1,
13
+ ipAddress: "149.154.175.211",
14
+ port: 443
15
+ }
16
+ },
17
+ 2: {
18
+ main: {
19
+ id: 2,
20
+ ipAddress: "149.154.167.41",
21
+ port: 443
22
+ },
23
+ media: {
24
+ id: 2,
25
+ ipAddress: "149.154.167.35",
26
+ port: 443
27
+ }
28
+ },
29
+ 3: {
30
+ main: {
31
+ id: 3,
32
+ ipAddress: "149.154.175.100",
33
+ port: 443
34
+ },
35
+ media: {
36
+ id: 3,
37
+ ipAddress: "149.154.175.100",
38
+ port: 443
39
+ }
40
+ },
41
+ 4: {
42
+ main: {
43
+ id: 4,
44
+ ipAddress: "149.154.167.91",
45
+ port: 443
46
+ },
47
+ media: {
48
+ id: 4,
49
+ ipAddress: "149.154.167.255",
50
+ port: 443
51
+ }
52
+ },
53
+ 5: {
54
+ main: {
55
+ id: 5,
56
+ ipAddress: "91.108.56.179",
57
+ port: 443
58
+ },
59
+ media: {
60
+ id: 5,
61
+ ipAddress: "149.154.171.255",
62
+ port: 443
63
+ }
64
+ }
65
+ };
66
+ const DC_MAPPING_TEST = {
67
+ 1: {
68
+ main: {
69
+ id: 1,
70
+ ipAddress: "149.154.175.10",
71
+ port: 80
72
+ },
73
+ media: {
74
+ id: 1,
75
+ ipAddress: "149.154.175.10",
76
+ port: 80
77
+ }
78
+ },
79
+ 2: {
80
+ main: {
81
+ id: 2,
82
+ ipAddress: "149.154.167.40",
83
+ port: 443
84
+ },
85
+ media: {
86
+ id: 2,
87
+ ipAddress: "149.154.167.40",
88
+ port: 443
89
+ }
90
+ },
91
+ 3: {
92
+ main: {
93
+ id: 3,
94
+ ipAddress: "149.154.175.117",
95
+ port: 443
96
+ },
97
+ media: {
98
+ id: 3,
99
+ ipAddress: "149.154.175.117",
100
+ port: 443
101
+ }
102
+ }
103
+ };
104
+ function isTestDc(ip) {
105
+ return Object.values(DC_MAPPING_TEST).some((dc) => dc.main.ipAddress === ip || dc.media.ipAddress === ip);
106
+ }
107
+ function parseIpFromBytes(data) {
108
+ if (data.length === 4) {
109
+ return `${data[0]}.${data[1]}.${data[2]}.${data[3]}`;
110
+ }
111
+ if (data.length === 16) {
112
+ let res = "";
113
+ for (let i = 0; i < 16; i += 2) {
114
+ res += data[i].toString(16).padStart(2, "0");
115
+ res += data[i + 1].toString(16).padStart(2, "0");
116
+ if (i < 14) res += ":";
117
+ }
118
+ return res;
119
+ }
120
+ throw new MtArgumentError("Invalid IP address length");
121
+ }
122
+ function serializeIpv4ToBytes(ip, buf) {
123
+ const parts = ip.split(".");
124
+ if (parts.length !== 4) {
125
+ throw new MtArgumentError("Invalid IPv4 address");
126
+ }
127
+ buf[0] = Number(parts[0]);
128
+ buf[1] = Number(parts[1]);
129
+ buf[2] = Number(parts[2]);
130
+ buf[3] = Number(parts[3]);
131
+ }
132
+ function serializeIpv6ToBytes(ip, buf) {
133
+ const parts = ip.split(":");
134
+ if (parts.length !== 8) {
135
+ throw new MtArgumentError("Invalid IPv6 address");
136
+ }
137
+ for (let i = 0; i < 8; i++) {
138
+ const val = Number.parseInt(parts[i], 16);
139
+ buf[i * 2] = val >> 8;
140
+ buf[i * 2 + 1] = val & 255;
141
+ }
142
+ }
143
+ function parseTelethonSession(session) {
144
+ if (session[0] !== "1") {
145
+ throw new MtArgumentError(`Invalid session string (version = ${session[0]})`);
146
+ }
147
+ session = session.slice(1);
148
+ const data = getPlatform().base64Decode(session, true);
149
+ const dv = dataViewFromBuffer(data);
150
+ const dcId = dv.getUint8(0);
151
+ const ipSize = session.length === 352 ? 4 : 16;
152
+ let pos = 1 + ipSize;
153
+ const ipBytes = data.subarray(1, pos);
154
+ const port = dv.getUint16(pos);
155
+ pos += 2;
156
+ const authKey = data.subarray(pos, pos + 256);
157
+ const ip = parseIpFromBytes(ipBytes);
158
+ return {
159
+ dcId,
160
+ ipAddress: ip,
161
+ ipv6: ipSize === 16,
162
+ port,
163
+ authKey
164
+ };
165
+ }
166
+ function serializeTelethonSession(session) {
167
+ if (session.authKey.length !== 256) {
168
+ throw new MtArgumentError("authKey must be 256 bytes long");
169
+ }
170
+ const ipSize = session.ipv6 ? 16 : 4;
171
+ const u8 = new Uint8Array(259 + ipSize);
172
+ const dv = dataViewFromBuffer(u8);
173
+ dv.setUint8(0, session.dcId);
174
+ let pos;
175
+ if (session.ipv6) {
176
+ serializeIpv6ToBytes(session.ipAddress, u8.subarray(1, 17));
177
+ pos = 17;
178
+ } else {
179
+ serializeIpv4ToBytes(session.ipAddress, u8.subarray(1, 5));
180
+ pos = 5;
181
+ }
182
+ dv.setUint16(pos, session.port);
183
+ pos += 2;
184
+ u8.set(session.authKey, pos);
185
+ let b64 = getPlatform().base64Encode(u8, true);
186
+ while (b64.length % 4 !== 0) b64 += "=";
187
+ return `1${b64}`;
188
+ }
189
+ function convertFromTelethonSession(session) {
190
+ if (typeof session === "string") {
191
+ session = parseTelethonSession(session);
192
+ }
193
+ const dc = {
194
+ id: session.dcId,
195
+ ipAddress: session.ipAddress,
196
+ port: session.port,
197
+ ipv6: session.ipv6
198
+ };
199
+ return {
200
+ version: 3,
201
+ // we don't exactly have that information. try to deduce it from DC_MAPPING_TEST
202
+ // todo: we should maybe check this at connect?
203
+ testMode: isTestDc(session.ipAddress),
204
+ primaryDcs: {
205
+ main: dc,
206
+ media: dc
207
+ },
208
+ authKey: session.authKey
209
+ };
210
+ }
211
+ function convertToTelethonSession(session) {
212
+ if (typeof session === "string") {
213
+ session = readStringSession(session);
214
+ }
215
+ return serializeTelethonSession({
216
+ dcId: session.primaryDcs.main.id,
217
+ ipAddress: session.primaryDcs.main.ipAddress,
218
+ port: session.primaryDcs.main.port,
219
+ ipv6: session.primaryDcs.main.ipv6 ?? false,
220
+ authKey: session.authKey
221
+ });
222
+ }
223
+ function parseGramjsSession(session) {
224
+ if (session[0] !== "1") {
225
+ throw new MtArgumentError(`Invalid session string (version = ${session[0]})`);
226
+ }
227
+ session = session.slice(1);
228
+ const data = getPlatform().base64Decode(session);
229
+ const dv = dataViewFromBuffer(data);
230
+ const dcId = dv.getUint8(0);
231
+ const ipSize = dv.getUint16(1);
232
+ let pos = 3 + ipSize;
233
+ const ip = getPlatform().utf8Decode(data.subarray(3, pos));
234
+ const port = dv.getUint16(pos);
235
+ pos += 2;
236
+ const authKey = data.subarray(pos, pos + 256);
237
+ return {
238
+ dcId,
239
+ ipAddress: ip,
240
+ ipv6: ip.includes(":"),
241
+ // dumb check but gramjs does this
242
+ port,
243
+ authKey
244
+ };
245
+ }
246
+ function serializeGramjsSession(session) {
247
+ if (session.authKey.length !== 256) {
248
+ throw new MtArgumentError("authKey must be 256 bytes long");
249
+ }
250
+ const ipEncoded = getPlatform().utf8Encode(session.ipAddress);
251
+ const u8 = new Uint8Array(261 + ipEncoded.length);
252
+ const dv = dataViewFromBuffer(u8);
253
+ dv.setUint8(0, session.dcId);
254
+ dv.setUint16(1, ipEncoded.length);
255
+ u8.set(ipEncoded, 3);
256
+ let pos = 3 + ipEncoded.length;
257
+ dv.setUint16(pos, session.port);
258
+ pos += 2;
259
+ u8.set(session.authKey, pos);
260
+ return `1${getPlatform().base64Encode(u8)}`;
261
+ }
262
+ function convertFromGramjsSession(session) {
263
+ if (typeof session === "string") {
264
+ session = parseGramjsSession(session);
265
+ }
266
+ return convertFromTelethonSession(session);
267
+ }
268
+ function convertToGramjsSession(session) {
269
+ if (typeof session === "string") {
270
+ session = readStringSession(session);
271
+ }
272
+ return serializeGramjsSession({
273
+ dcId: session.primaryDcs.main.id,
274
+ ipAddress: session.primaryDcs.main.ipAddress,
275
+ port: session.primaryDcs.main.port,
276
+ ipv6: session.primaryDcs.main.ipv6 ?? false,
277
+ authKey: session.authKey
278
+ });
279
+ }
280
+ function telegramRleEncode(buf) {
281
+ const len = buf.length;
282
+ const ret = [];
283
+ let count = 0;
284
+ for (let i = 0; i < len; i++) {
285
+ const cur = buf[i];
286
+ if (cur === 0) {
287
+ count += 1;
288
+ } else {
289
+ if (count > 0) {
290
+ ret.push(0, count);
291
+ count = 0;
292
+ }
293
+ ret.push(cur);
294
+ }
295
+ }
296
+ if (count > 0) {
297
+ ret.push(0, count);
298
+ }
299
+ return new Uint8Array(ret);
300
+ }
301
+ function telegramRleDecode(buf) {
302
+ const len = buf.length;
303
+ const ret = [];
304
+ let prev = -1;
305
+ for (let i = 0; i < len; i++) {
306
+ const cur = buf[i];
307
+ if (prev === 0) {
308
+ for (let j = 0; j < cur; j++) {
309
+ ret.push(prev);
310
+ }
311
+ prev = -1;
312
+ } else {
313
+ if (prev !== -1) ret.push(prev);
314
+ prev = cur;
315
+ }
316
+ }
317
+ if (prev !== -1) ret.push(prev);
318
+ return new Uint8Array(ret);
319
+ }
320
+ function parseMtkrutoSession(session) {
321
+ const data = telegramRleDecode(getPlatform().base64Decode(session, true));
322
+ const reader = TlBinaryReader.manual(data);
323
+ let dcIdStr = reader.string();
324
+ const authKey = reader.bytes();
325
+ const isTest = dcIdStr.endsWith("-test");
326
+ if (isTest) {
327
+ dcIdStr = dcIdStr.slice(0, -5);
328
+ }
329
+ const dcId = Number(dcIdStr);
330
+ if (Number.isNaN(dcId)) {
331
+ throw new MtArgumentError(`Invalid DC ID: ${dcIdStr}`);
332
+ }
333
+ return {
334
+ dcId,
335
+ isTest,
336
+ authKey
337
+ };
338
+ }
339
+ function serializeMtkrutoSession(session) {
340
+ const dcIdStr = `${session.dcId}${session.isTest ? "-test" : ""}`;
341
+ const writer = TlBinaryWriter.manual(session.authKey.length + dcIdStr.length + 8);
342
+ writer.string(dcIdStr);
343
+ writer.bytes(session.authKey);
344
+ return getPlatform().base64Encode(telegramRleEncode(writer.result()), true);
345
+ }
346
+ function convertFromMtkrutoSession(session) {
347
+ if (typeof session === "string") {
348
+ session = parseMtkrutoSession(session);
349
+ }
350
+ return {
351
+ version: 3,
352
+ testMode: session.isTest,
353
+ primaryDcs: (session.isTest ? DC_MAPPING_TEST : DC_MAPPING_PROD)[session.dcId],
354
+ authKey: session.authKey
355
+ };
356
+ }
357
+ function convertToMtkrutoSession(session) {
358
+ if (typeof session === "string") {
359
+ session = readStringSession(session);
360
+ }
361
+ return serializeMtkrutoSession({
362
+ dcId: session.primaryDcs.main.id,
363
+ isTest: session.testMode,
364
+ authKey: session.authKey
365
+ });
366
+ }
367
+ const SESSION_STRING_SIZE$1 = 351;
368
+ const SESSION_STRING_SIZE_64 = 356;
369
+ function parsePyrogramSession(session) {
370
+ const data = getPlatform().base64Decode(session, true);
371
+ const dv = dataViewFromBuffer(data);
372
+ if (session.length === SESSION_STRING_SIZE$1 || session.length === SESSION_STRING_SIZE_64) {
373
+ const dcId2 = dv.getUint8(0);
374
+ const isTest2 = dv.getUint8(1) !== 0;
375
+ const authKey2 = data.subarray(2, 258);
376
+ let userId2;
377
+ if (data.length === SESSION_STRING_SIZE$1) {
378
+ userId2 = dv.getUint32(258);
379
+ } else {
380
+ const high = dv.getUint32(258);
381
+ const low = dv.getUint32(262);
382
+ userId2 = Long.fromBits(low, high).toNumber();
383
+ }
384
+ const isBot2 = dv.getUint8(data.length - 1) !== 0;
385
+ return {
386
+ dcId: dcId2,
387
+ isTest: isTest2,
388
+ authKey: authKey2,
389
+ userId: userId2,
390
+ isBot: isBot2
391
+ };
392
+ }
393
+ const dcId = dv.getUint8(0);
394
+ const apiId = dv.getUint32(1);
395
+ const isTest = dv.getUint8(5) !== 0;
396
+ const authKey = data.subarray(6, 262);
397
+ const userId = longFromBuffer(data.subarray(262, 270), true, false).toNumber();
398
+ const isBot = dv.getUint8(270) !== 0;
399
+ return {
400
+ dcId,
401
+ apiId,
402
+ isTest,
403
+ authKey,
404
+ userId,
405
+ isBot
406
+ };
407
+ }
408
+ const SESSION_STRING_SIZE_OLD = 267;
409
+ const SESSION_STRING_SIZE = 271;
410
+ function serializePyrogramSession(session) {
411
+ if (session.authKey.length !== 256) {
412
+ throw new MtArgumentError("authKey must be 256 bytes long");
413
+ }
414
+ const userIdLong = Long.fromNumber(session.userId, true);
415
+ let u8;
416
+ if (session.apiId === void 0) {
417
+ u8 = new Uint8Array(SESSION_STRING_SIZE_OLD);
418
+ const dv = dataViewFromBuffer(u8);
419
+ dv.setUint8(0, session.dcId);
420
+ dv.setUint8(1, session.isTest ? 1 : 0);
421
+ u8.set(session.authKey, 2);
422
+ dv.setUint32(258, userIdLong.high);
423
+ dv.setUint32(262, userIdLong.low);
424
+ dv.setUint8(266, session.isBot ? 1 : 0);
425
+ } else {
426
+ u8 = new Uint8Array(SESSION_STRING_SIZE);
427
+ const dv = dataViewFromBuffer(u8);
428
+ dv.setUint8(0, session.dcId);
429
+ dv.setUint32(1, session.apiId);
430
+ dv.setUint8(5, session.isTest ? 1 : 0);
431
+ u8.set(session.authKey, 6);
432
+ dv.setUint32(262, userIdLong.high);
433
+ dv.setUint32(266, userIdLong.low);
434
+ dv.setUint8(270, session.isBot ? 1 : 0);
435
+ }
436
+ return getPlatform().base64Encode(u8, true);
437
+ }
438
+ function convertFromPyrogramSession(session) {
439
+ if (typeof session === "string") {
440
+ session = parsePyrogramSession(session);
441
+ }
442
+ return {
443
+ version: 3,
444
+ testMode: session.isTest,
445
+ primaryDcs: (session.isTest ? DC_MAPPING_TEST : DC_MAPPING_PROD)[session.dcId],
446
+ authKey: session.authKey,
447
+ self: {
448
+ userId: session.userId,
449
+ isBot: session.isBot,
450
+ isPremium: false,
451
+ usernames: []
452
+ }
453
+ };
454
+ }
455
+ function convertToPyrogramSession(session, params) {
456
+ if (typeof session === "string") {
457
+ session = readStringSession(session);
458
+ }
459
+ return serializePyrogramSession({
460
+ apiId: params?.apiId,
461
+ isBot: session.self?.isBot ?? false,
462
+ isTest: session.testMode,
463
+ userId: session.self?.userId ?? 0,
464
+ dcId: session.primaryDcs.main.id,
465
+ authKey: session.authKey
466
+ });
467
+ }
468
+ export {
469
+ DC_MAPPING_PROD,
470
+ DC_MAPPING_TEST,
471
+ convertFromGramjsSession,
472
+ convertFromMtkrutoSession,
473
+ convertFromPyrogramSession,
474
+ convertFromTelethonSession,
475
+ convertToGramjsSession,
476
+ convertToMtkrutoSession,
477
+ convertToPyrogramSession,
478
+ convertToTelethonSession,
479
+ isTestDc,
480
+ parseGramjsSession,
481
+ parseMtkrutoSession,
482
+ parsePyrogramSession,
483
+ parseTelethonSession,
484
+ serializeGramjsSession,
485
+ serializeMtkrutoSession,
486
+ serializePyrogramSession,
487
+ serializeTelethonSession
488
+ };
@@ -0,0 +1 @@
1
+ export declare const MTKRUTO_SESSION = "BjItdGVzdAAB_gABAQABWEIKa07Ch-9zoA024mDOpsv20TW4YwuoRRROqSi41YQCbD3c4nKnz7BcFIu1mfn6f6Xm3OTVqoT0zib4p_AuZD9H-t8j5AagecRg-oSpQlmjoiUazKQSxnxWotGWf1mPNntAeOvDNa5t1NjXUxmqdB3e2AjYLF_E2jDESVgUuDBQUMBHIDc_xFBAlz6kVxCZ6iINJHbnyJ2F19tbEPFJvSM999RKaFj5lUUVs0qKNXEUmsFYUuIdPBzjWilY8Uvf9nYU_xXd9CUAAXS5_i4aaWlHoTIf3zn8ZEINhDIU1DMauh5vhSWt7F0fkxODjtou-7PdIunuDtqyQm4steuNJc8";
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
package/package.json CHANGED
@@ -1,26 +1,30 @@
1
1
  {
2
- "name": "@mtcute/convert",
3
- "version": "0.16.0",
4
- "description": "Cross-library session conversion utilities",
5
- "author": "alina sireneva <alina@tei.su>",
6
- "license": "MIT",
7
- "type": "module",
8
- "sideEffects": false,
9
- "scripts": {},
10
- "exports": {
11
- ".": {
12
- "import": "./esm/index.js",
13
- "require": "./cjs/index.js"
2
+ "name": "@mtcute/convert",
3
+ "type": "module",
4
+ "version": "0.16.9",
5
+ "description": "Cross-library session conversion utilities",
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
+ }
20
+ },
21
+ "scripts": {},
22
+ "dependencies": {
23
+ "@mtcute/core": "^0.16.9"
24
+ },
25
+ "homepage": "https://mtcute.dev",
26
+ "repository": {
27
+ "type": "git",
28
+ "url": "https://github.com/mtcute/mtcute"
14
29
  }
15
- },
16
- "dependencies": {
17
- "@mtcute/core": "^0.16.0"
18
- },
19
- "main": "cjs/index.js",
20
- "module": "esm/index.js",
21
- "homepage": "https://mtcute.dev",
22
- "repository": {
23
- "type": "git",
24
- "url": "https://github.com/mtcute/mtcute"
25
- }
26
30
  }
@@ -0,0 +1 @@
1
+ export declare const PYROGRAM_TEST_SESSION = "AgAyyvcBTk6KssqikKPxEhxfXJpkoFIgQ_o8VpCk_4g0tcHe0rVCXx34AaDKvaNOlbkJOZ4jA3AI8iDYkI2opuifbM_7S2u9MMdnrjfg5jpfkXfI9-wF8DK_UBGIe1zk_Ibn0IHLRz-lkb-QqZNhh8O8Ggb8cieamatEYwLrkjkZR7JG53q76F0ktUd22L6_bUlp9p_qgXqBg8vZdkIIs9T1OiShw2X6TNO0lYqfJVaczMVQcT9Zt0FiyrAMpovFuT7-96OFKWcQ9gzrs_SHfz9HrQgBwvNSdkVziXTtxLJXsaNz3smGeyh-CEuEgdF3enIECnzftlvvUClLN_ylcPir1bi4_wAAAAEqEi1JAA";
@@ -0,0 +1 @@
1
+ export declare const PYROGRAM_TEST_SESSION_OLD = "AgEWdHMtuA1pC01YkNiHpL1bC0yBC3wzGZCwSRWKlA_a69RhePUN3M51NpnwSXrW3pZV9FS8WjAwUkA23uT_49t8c7Umw3ihhKD6-hTpZ5wXC2MuC0EsF0-Z6WshYhT3gmN6QhEt0jlXo5cW1BJ3MYmXtsTWNf_hJfd3_wF_ZFa58ntVV-3qd08wQRhiL_IxM7L5YazjPw0dg2z92CqRARku_oq5D29V6W6bo8T-SLzF_ujj5ZcAQL25mJtCcXfhhjp9atxcrqnKzEs05xyrehnlJZKoGmnX0mF2P_6wUHqZC9tcTBUV4AmFcbuy7m_4SYLnJ8MbftNs7aWHHNcB1R4fAAAAASoSLUkA";
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export declare const TELETHON_TEST_SESSION = "1ApWapygAUChJS1_xwUK01Is4cOvQa1JKTn1POabdMUCfLmXNYFUyvG3v9Z_qbFNFp3zYP--3aVpTYI2DpB2Ib46p_bwSC0j1QEjvdQxJj26cVj8NfslrCkYrdV3glOhdczSq08kp31eqBGXMPhA7wy7DOcSLLAoy-Jf3Q_V_Q3y2a8_64ArFJe8PFfSqkdO56VQutajNLscFUtTQXUQFLJ7ft6vIl__UOc9tpQZEiFW7jWmID79WkfYLHFjuChTVKGMLDa8YcZj6z5Sq-pXPE9VbAbJ5L1JRqXOey3QGtZgJeIEww_WWD5nMMUfhLIydD2i7eDmVoUE5EIZPpsevJmjiGLw4vJk=";
@@ -0,0 +1 @@
1
+ export declare const TELETHON_TEST_SESSION_V6 = "1ASABCyjyPfABAAAAAAAAAA4Bu4pveAFWSE51_trKsrRQeMvGXMl8fI6NsGaWqdrXXeqyaXne9qNthqnrBmH56kHfOhFUCPSoVzNNrGgnQr67AYQbkhpP_Yml2EDd8epdc6Gywh4q2NBgYyW6VBT8UKg89-FebYTO6n47I1cJMGsSZ1ddxEOpIpHXsSmPdGBSTz6uaHbLYo0jnxd59PQn4H4dKb8FxuOQsUVa3vY_o79HMVMQRVT1IksUKFg5gAe5ZJ0yx6W4pMviVbC-TYZC0HInmv2fFMv-S3rQyg1C7qpU-Gbo1P6UZC4KZGmu2pMJooFNyfRbFgl3BI5Z-FNx9TKu4UFrF9G6Q0l8PjPXOZm4j-c=";
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
package/types.d.ts ADDED
File without changes
package/cjs/dcs.js DELETED
@@ -1,113 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isTestDc = exports.DC_MAPPING_TEST = exports.DC_MAPPING_PROD = void 0;
4
- // some libraries only store the DCs in the source code, so we need to map them to the correct DCs
5
- // this may not be very accurate, but it's better than nothing
6
- // we *could* always map those to the primary dc (the client should handle that gracefully),
7
- // but imo it's better to be as accurate as possible
8
- // we'll also only map to ipv4 since that's more portable
9
- exports.DC_MAPPING_PROD = {
10
- '1': {
11
- main: {
12
- id: 1,
13
- ipAddress: '149.154.175.56',
14
- port: 443,
15
- },
16
- media: {
17
- id: 1,
18
- ipAddress: '149.154.175.211',
19
- port: 443,
20
- },
21
- },
22
- '2': {
23
- main: {
24
- id: 2,
25
- ipAddress: '149.154.167.41',
26
- port: 443,
27
- },
28
- media: {
29
- id: 2,
30
- ipAddress: '149.154.167.35',
31
- port: 443,
32
- },
33
- },
34
- '3': {
35
- main: {
36
- id: 3,
37
- ipAddress: '149.154.175.100',
38
- port: 443,
39
- },
40
- media: {
41
- id: 3,
42
- ipAddress: '149.154.175.100',
43
- port: 443,
44
- },
45
- },
46
- '4': {
47
- main: {
48
- id: 4,
49
- ipAddress: '149.154.167.91',
50
- port: 443,
51
- },
52
- media: {
53
- id: 4,
54
- ipAddress: '149.154.167.255',
55
- port: 443,
56
- },
57
- },
58
- '5': {
59
- main: {
60
- id: 5,
61
- ipAddress: '91.108.56.179',
62
- port: 443,
63
- },
64
- media: {
65
- id: 5,
66
- ipAddress: '149.154.171.255',
67
- port: 443,
68
- },
69
- },
70
- };
71
- exports.DC_MAPPING_TEST = {
72
- '1': {
73
- main: {
74
- id: 1,
75
- ipAddress: '149.154.175.10',
76
- port: 80,
77
- },
78
- media: {
79
- id: 1,
80
- ipAddress: '149.154.175.10',
81
- port: 80,
82
- },
83
- },
84
- '2': {
85
- main: {
86
- id: 2,
87
- ipAddress: '149.154.167.40',
88
- port: 443,
89
- },
90
- media: {
91
- id: 2,
92
- ipAddress: '149.154.167.40',
93
- port: 443,
94
- },
95
- },
96
- '3': {
97
- main: {
98
- id: 3,
99
- ipAddress: '149.154.175.117',
100
- port: 443,
101
- },
102
- media: {
103
- id: 3,
104
- ipAddress: '149.154.175.117',
105
- port: 443,
106
- },
107
- },
108
- };
109
- function isTestDc(ip) {
110
- return Object.values(exports.DC_MAPPING_TEST).some((dc) => dc.main.ipAddress === ip || dc.media.ipAddress === ip);
111
- }
112
- exports.isTestDc = isTestDc;
113
- //# sourceMappingURL=dcs.js.map