@mtcute/convert 0.16.7 → 0.17.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.
Files changed (172) hide show
  1. package/{cjs/dcs.d.ts → dcs.d.ts} +1 -1
  2. package/gramjs/__fixtures__/session.d.ts +1 -0
  3. package/{esm/gramjs → gramjs}/convert.d.ts +2 -2
  4. package/{cjs/gramjs → gramjs}/parse.d.ts +1 -1
  5. package/{cjs/gramjs → gramjs}/serialize.d.ts +1 -1
  6. package/gramjs/serialize.test.d.ts +1 -0
  7. package/index.cjs +493 -0
  8. package/index.d.ts +5 -1
  9. package/index.js +488 -1
  10. package/mtkruto/__fixtures__/session.d.ts +1 -0
  11. package/{cjs/mtkruto → mtkruto}/convert.d.ts +2 -2
  12. package/mtkruto/convert.test.d.ts +1 -0
  13. package/{esm/mtkruto → mtkruto}/parse.d.ts +1 -1
  14. package/mtkruto/parse.test.d.ts +1 -0
  15. package/{cjs/mtkruto → mtkruto}/serialize.d.ts +1 -1
  16. package/mtkruto/serialize.test.d.ts +1 -0
  17. package/package.json +27 -23
  18. package/pyrogram/__fixtures__/session.d.ts +1 -0
  19. package/pyrogram/__fixtures__/session_old.d.ts +1 -0
  20. package/{esm/pyrogram → pyrogram}/convert.d.ts +2 -2
  21. package/pyrogram/convert.test.d.ts +1 -0
  22. package/{esm/pyrogram → pyrogram}/parse.d.ts +1 -1
  23. package/pyrogram/parse.test.d.ts +1 -0
  24. package/{esm/pyrogram → pyrogram}/serialize.d.ts +1 -1
  25. package/pyrogram/serialize.test.d.ts +1 -0
  26. package/telethon/__fixtures__/session.d.ts +1 -0
  27. package/telethon/__fixtures__/session_v6.d.ts +1 -0
  28. package/{esm/telethon → telethon}/convert.d.ts +2 -2
  29. package/telethon/convert.test.d.ts +1 -0
  30. package/{esm/telethon → telethon}/parse.d.ts +1 -1
  31. package/telethon/parse.test.d.ts +1 -0
  32. package/{esm/telethon → telethon}/serialize.d.ts +1 -1
  33. package/telethon/serialize.test.d.ts +1 -0
  34. package/types.d.ts +0 -0
  35. package/cjs/dcs.js +0 -113
  36. package/cjs/dcs.js.map +0 -1
  37. package/cjs/gramjs/convert.d.ts +0 -4
  38. package/cjs/gramjs/convert.js +0 -27
  39. package/cjs/gramjs/convert.js.map +0 -1
  40. package/cjs/gramjs/index.js +0 -21
  41. package/cjs/gramjs/index.js.map +0 -1
  42. package/cjs/gramjs/parse.js +0 -30
  43. package/cjs/gramjs/parse.js.map +0 -1
  44. package/cjs/gramjs/serialize.js +0 -23
  45. package/cjs/gramjs/serialize.js.map +0 -1
  46. package/cjs/gramjs/types.js +0 -3
  47. package/cjs/gramjs/types.js.map +0 -1
  48. package/cjs/index.js +0 -28
  49. package/cjs/index.js.map +0 -1
  50. package/cjs/mtkruto/convert.js +0 -30
  51. package/cjs/mtkruto/convert.js.map +0 -1
  52. package/cjs/mtkruto/index.js +0 -21
  53. package/cjs/mtkruto/index.js.map +0 -1
  54. package/cjs/mtkruto/parse.d.ts +0 -2
  55. package/cjs/mtkruto/parse.js +0 -27
  56. package/cjs/mtkruto/parse.js.map +0 -1
  57. package/cjs/mtkruto/serialize.js +0 -14
  58. package/cjs/mtkruto/serialize.js.map +0 -1
  59. package/cjs/mtkruto/types.js +0 -3
  60. package/cjs/mtkruto/types.js.map +0 -1
  61. package/cjs/package.json +0 -3
  62. package/cjs/pyrogram/convert.d.ts +0 -6
  63. package/cjs/pyrogram/convert.js +0 -39
  64. package/cjs/pyrogram/convert.js.map +0 -1
  65. package/cjs/pyrogram/index.js +0 -21
  66. package/cjs/pyrogram/index.js.map +0 -1
  67. package/cjs/pyrogram/parse.d.ts +0 -2
  68. package/cjs/pyrogram/parse.js +0 -55
  69. package/cjs/pyrogram/parse.js.map +0 -1
  70. package/cjs/pyrogram/serialize.d.ts +0 -2
  71. package/cjs/pyrogram/serialize.js +0 -39
  72. package/cjs/pyrogram/serialize.js.map +0 -1
  73. package/cjs/pyrogram/types.js +0 -3
  74. package/cjs/pyrogram/types.js.map +0 -1
  75. package/cjs/telethon/convert.d.ts +0 -4
  76. package/cjs/telethon/convert.js +0 -43
  77. package/cjs/telethon/convert.js.map +0 -1
  78. package/cjs/telethon/index.js +0 -21
  79. package/cjs/telethon/index.js.map +0 -1
  80. package/cjs/telethon/parse.d.ts +0 -2
  81. package/cjs/telethon/parse.js +0 -32
  82. package/cjs/telethon/parse.js.map +0 -1
  83. package/cjs/telethon/serialize.d.ts +0 -2
  84. package/cjs/telethon/serialize.js +0 -33
  85. package/cjs/telethon/serialize.js.map +0 -1
  86. package/cjs/telethon/types.js +0 -3
  87. package/cjs/telethon/types.js.map +0 -1
  88. package/cjs/types.js +0 -3
  89. package/cjs/types.js.map +0 -1
  90. package/cjs/utils/ip.js +0 -44
  91. package/cjs/utils/ip.js.map +0 -1
  92. package/cjs/utils/rle.js +0 -54
  93. package/cjs/utils/rle.js.map +0 -1
  94. package/esm/dcs.d.ts +0 -4
  95. package/esm/dcs.js +0 -109
  96. package/esm/dcs.js.map +0 -1
  97. package/esm/gramjs/convert.js +0 -23
  98. package/esm/gramjs/convert.js.map +0 -1
  99. package/esm/gramjs/index.d.ts +0 -4
  100. package/esm/gramjs/index.js +0 -5
  101. package/esm/gramjs/index.js.map +0 -1
  102. package/esm/gramjs/parse.d.ts +0 -2
  103. package/esm/gramjs/parse.js +0 -27
  104. package/esm/gramjs/parse.js.map +0 -1
  105. package/esm/gramjs/serialize.d.ts +0 -2
  106. package/esm/gramjs/serialize.js +0 -20
  107. package/esm/gramjs/serialize.js.map +0 -1
  108. package/esm/gramjs/types.d.ts +0 -7
  109. package/esm/gramjs/types.js +0 -2
  110. package/esm/gramjs/types.js.map +0 -1
  111. package/esm/index.d.ts +0 -5
  112. package/esm/index.js +0 -6
  113. package/esm/index.js.map +0 -1
  114. package/esm/mtkruto/convert.d.ts +0 -4
  115. package/esm/mtkruto/convert.js +0 -26
  116. package/esm/mtkruto/convert.js.map +0 -1
  117. package/esm/mtkruto/index.d.ts +0 -4
  118. package/esm/mtkruto/index.js +0 -5
  119. package/esm/mtkruto/index.js.map +0 -1
  120. package/esm/mtkruto/parse.js +0 -24
  121. package/esm/mtkruto/parse.js.map +0 -1
  122. package/esm/mtkruto/serialize.d.ts +0 -2
  123. package/esm/mtkruto/serialize.js +0 -11
  124. package/esm/mtkruto/serialize.js.map +0 -1
  125. package/esm/mtkruto/types.d.ts +0 -5
  126. package/esm/mtkruto/types.js +0 -2
  127. package/esm/mtkruto/types.js.map +0 -1
  128. package/esm/pyrogram/convert.js +0 -35
  129. package/esm/pyrogram/convert.js.map +0 -1
  130. package/esm/pyrogram/index.d.ts +0 -4
  131. package/esm/pyrogram/index.js +0 -5
  132. package/esm/pyrogram/index.js.map +0 -1
  133. package/esm/pyrogram/parse.js +0 -52
  134. package/esm/pyrogram/parse.js.map +0 -1
  135. package/esm/pyrogram/serialize.js +0 -36
  136. package/esm/pyrogram/serialize.js.map +0 -1
  137. package/esm/pyrogram/types.d.ts +0 -8
  138. package/esm/pyrogram/types.js +0 -2
  139. package/esm/pyrogram/types.js.map +0 -1
  140. package/esm/telethon/convert.js +0 -39
  141. package/esm/telethon/convert.js.map +0 -1
  142. package/esm/telethon/index.d.ts +0 -4
  143. package/esm/telethon/index.js +0 -5
  144. package/esm/telethon/index.js.map +0 -1
  145. package/esm/telethon/parse.js +0 -29
  146. package/esm/telethon/parse.js.map +0 -1
  147. package/esm/telethon/serialize.js +0 -30
  148. package/esm/telethon/serialize.js.map +0 -1
  149. package/esm/telethon/types.d.ts +0 -7
  150. package/esm/telethon/types.js +0 -2
  151. package/esm/telethon/types.js.map +0 -1
  152. package/esm/types.js +0 -2
  153. package/esm/types.js.map +0 -1
  154. package/esm/utils/ip.d.ts +0 -3
  155. package/esm/utils/ip.js +0 -39
  156. package/esm/utils/ip.js.map +0 -1
  157. package/esm/utils/rle.d.ts +0 -3
  158. package/esm/utils/rle.js +0 -49
  159. package/esm/utils/rle.js.map +0 -1
  160. /package/{cjs/types.d.ts → gramjs/convert.test.d.ts} +0 -0
  161. /package/{cjs/gramjs → gramjs}/index.d.ts +0 -0
  162. /package/{esm/types.d.ts → gramjs/parse.test.d.ts} +0 -0
  163. /package/{cjs/gramjs → gramjs}/types.d.ts +0 -0
  164. /package/{cjs/index.d.ts → index.d.cts} +0 -0
  165. /package/{cjs/mtkruto → mtkruto}/index.d.ts +0 -0
  166. /package/{cjs/mtkruto → mtkruto}/types.d.ts +0 -0
  167. /package/{cjs/pyrogram → pyrogram}/index.d.ts +0 -0
  168. /package/{cjs/pyrogram → pyrogram}/types.d.ts +0 -0
  169. /package/{cjs/telethon → telethon}/index.d.ts +0 -0
  170. /package/{cjs/telethon → telethon}/types.d.ts +0 -0
  171. /package/{cjs/utils → utils}/ip.d.ts +0 -0
  172. /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";
@@ -1,4 +1,4 @@
1
- import type { StringSessionData } from '@mtcute/core/utils.js';
2
- import type { MtkrutoSession } from './types.js';
1
+ import { StringSessionData } from '@mtcute/core/utils.js';
2
+ import { MtkrutoSession } from './types.js';
3
3
  export declare function convertFromMtkrutoSession(session: MtkrutoSession | string): StringSessionData;
4
4
  export declare function convertToMtkrutoSession(session: StringSessionData | string): string;
@@ -0,0 +1 @@
1
+ export {};
@@ -1,2 +1,2 @@
1
- import type { MtkrutoSession } from './types.js';
1
+ import { MtkrutoSession } from './types.js';
2
2
  export declare function parseMtkrutoSession(session: string): MtkrutoSession;
@@ -0,0 +1 @@
1
+ export {};
@@ -1,2 +1,2 @@
1
- import type { MtkrutoSession } from './types.js';
1
+ import { MtkrutoSession } from './types.js';
2
2
  export declare function serializeMtkrutoSession(session: MtkrutoSession): string;
@@ -0,0 +1 @@
1
+ export {};
package/package.json CHANGED
@@ -1,26 +1,30 @@
1
1
  {
2
- "name": "@mtcute/convert",
3
- "type": "module",
4
- "version": "0.16.7",
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": "./esm/index.js",
12
- "require": "./cjs/index.js"
2
+ "name": "@mtcute/convert",
3
+ "type": "module",
4
+ "version": "0.17.0",
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.17.0"
24
+ },
25
+ "homepage": "https://mtcute.dev",
26
+ "repository": {
27
+ "type": "git",
28
+ "url": "https://github.com/mtcute/mtcute"
13
29
  }
14
- },
15
- "scripts": {},
16
- "dependencies": {
17
- "@mtcute/core": "^0.16.7"
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";
@@ -1,5 +1,5 @@
1
- import type { StringSessionData } from '@mtcute/core/utils.js';
2
- import type { PyrogramSession } from './types.js';
1
+ import { StringSessionData } from '@mtcute/core/utils.js';
2
+ import { PyrogramSession } from './types.js';
3
3
  export declare function convertFromPyrogramSession(session: PyrogramSession | string): StringSessionData;
4
4
  export declare function convertToPyrogramSession(session: StringSessionData | string, params?: {
5
5
  apiId?: number;
@@ -0,0 +1 @@
1
+ export {};
@@ -1,2 +1,2 @@
1
- import type { PyrogramSession } from './types.js';
1
+ import { PyrogramSession } from './types.js';
2
2
  export declare function parsePyrogramSession(session: string): PyrogramSession;
@@ -0,0 +1 @@
1
+ export {};
@@ -1,2 +1,2 @@
1
- import type { PyrogramSession } from './types.js';
1
+ import { PyrogramSession } from './types.js';
2
2
  export declare function serializePyrogramSession(session: PyrogramSession): string;
@@ -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=";
@@ -1,4 +1,4 @@
1
- import type { StringSessionData } from '@mtcute/core/utils.js';
2
- import type { TelethonSession } from './types.js';
1
+ import { StringSessionData } from '@mtcute/core/utils.js';
2
+ import { TelethonSession } from './types.js';
3
3
  export declare function convertFromTelethonSession(session: TelethonSession | string): StringSessionData;
4
4
  export declare function convertToTelethonSession(session: StringSessionData | string): string;
@@ -0,0 +1 @@
1
+ export {};
@@ -1,2 +1,2 @@
1
- import type { TelethonSession } from './types.js';
1
+ import { TelethonSession } from './types.js';
2
2
  export declare function parseTelethonSession(session: string): TelethonSession;
@@ -0,0 +1 @@
1
+ export {};
@@ -1,2 +1,2 @@
1
- import type { TelethonSession } from './types.js';
1
+ import { TelethonSession } from './types.js';
2
2
  export declare function serializeTelethonSession(session: TelethonSession): string;
@@ -0,0 +1 @@
1
+ export {};
package/types.d.ts ADDED
File without changes