@mtcute/convert 0.19.2 → 0.19.4

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.
@@ -0,0 +1,88 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __copyProps = (to, from, except, desc) => {
8
+ if (from && typeof from === "object" || typeof from === "function") {
9
+ for (let key of __getOwnPropNames(from))
10
+ if (!__hasOwnProp.call(to, key) && key !== except)
11
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
12
+ }
13
+ return to;
14
+ };
15
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
16
+ // If the importer is in node compatibility mode or this is not an ESM
17
+ // file that has been converted to a CommonJS file using a Babel-
18
+ // compatible transform (i.e. "__esModule" has not been set), then set
19
+ // "default" to the CommonJS "module.exports" for node compatibility.
20
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
21
+ mod
22
+ ));
23
+ if (typeof globalThis !== "undefined" && !globalThis._MTCUTE_CJS_DEPRECATION_WARNED) {
24
+ globalThis._MTCUTE_CJS_DEPRECATION_WARNED = true;
25
+ console.warn("[@mtcute/convert] CommonJS support is deprecated and will be removed in 0.25.0. Please consider switching to ESM, it's " + (/* @__PURE__ */ new Date()).getFullYear() + " already.");
26
+ console.warn("[@mtcute/convert] Learn more about switching to ESM: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c");
27
+ }
28
+ "use strict";
29
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
30
+ const utils = require("@fuman/utils");
31
+ const fs = require("../utils/fs.cjs");
32
+ async function readGramjsStoreSession(path, params) {
33
+ const fs$1 = params?.fs ?? await import("node:fs/promises");
34
+ const sessionName = fs.basename(path);
35
+ const [fAuthKey, fDcId, fIpAddress, fPort] = await Promise.all([
36
+ fs$1.readFile(fs.joinPaths(path, `${sessionName}%3AauthKey`)),
37
+ fs$1.readFile(fs.joinPaths(path, `${sessionName}%3AdcId`)),
38
+ fs$1.readFile(fs.joinPaths(path, `${sessionName}%3AserverAddress`)),
39
+ fs$1.readFile(fs.joinPaths(path, `${sessionName}%3Aport`))
40
+ ]);
41
+ const authKeyParsed = JSON.parse(utils.utf8.decoder.decode(fAuthKey));
42
+ if (typeof authKeyParsed !== "object" || authKeyParsed === null || !("type" in authKeyParsed) || authKeyParsed.type !== "Buffer" || !("data" in authKeyParsed) || !Array.isArray(authKeyParsed.data)) {
43
+ throw new Error("Invalid auth key file");
44
+ }
45
+ const authKey = new Uint8Array(authKeyParsed.data);
46
+ if (authKey.length !== 256) {
47
+ throw new Error("Invalid auth key length");
48
+ }
49
+ const dcId = Number.parseInt(utils.utf8.decoder.decode(fDcId));
50
+ if (Number.isNaN(dcId)) {
51
+ throw new TypeError("Invalid dc id");
52
+ }
53
+ const port = Number.parseInt(utils.utf8.decoder.decode(fPort));
54
+ if (Number.isNaN(port)) {
55
+ throw new TypeError("Invalid port");
56
+ }
57
+ const ip = JSON.parse(utils.utf8.decoder.decode(fIpAddress));
58
+ if (typeof ip !== "string") {
59
+ throw new TypeError("Invalid server address");
60
+ }
61
+ return {
62
+ dcId,
63
+ ipAddress: ip,
64
+ ipv6: ip.includes(":"),
65
+ // dumb check but gramjs does this
66
+ port,
67
+ authKey
68
+ };
69
+ }
70
+ async function writeGramjsStoreSession(path, session, params) {
71
+ const fs$1 = params?.fs ?? await import("node:fs/promises");
72
+ const sessionName = fs.basename(path);
73
+ await fs$1.mkdir(sessionName, { recursive: true });
74
+ await Promise.all([
75
+ fs$1.writeFile(
76
+ fs.joinPaths(path, `${sessionName}%3AauthKey`),
77
+ utils.utf8.encoder.encode(JSON.stringify({
78
+ type: "Buffer",
79
+ data: Array.from(session.authKey)
80
+ }))
81
+ ),
82
+ fs$1.writeFile(fs.joinPaths(path, `${sessionName}%3AdcId`), utils.utf8.encoder.encode(session.dcId.toString())),
83
+ fs$1.writeFile(fs.joinPaths(path, `${sessionName}%3AserverAddress`), utils.utf8.encoder.encode(JSON.stringify(session.ipAddress))),
84
+ fs$1.writeFile(fs.joinPaths(path, `${sessionName}%3Aport`), utils.utf8.encoder.encode(session.port.toString()))
85
+ ]);
86
+ }
87
+ exports.readGramjsStoreSession = readGramjsStoreSession;
88
+ exports.writeGramjsStoreSession = writeGramjsStoreSession;
@@ -0,0 +1,61 @@
1
+ import { utf8 } from "@fuman/utils";
2
+ import { basename, joinPaths } from "../utils/fs.js";
3
+ async function readGramjsStoreSession(path, params) {
4
+ const fs = params?.fs ?? await import("node:fs/promises");
5
+ const sessionName = basename(path);
6
+ const [fAuthKey, fDcId, fIpAddress, fPort] = await Promise.all([
7
+ fs.readFile(joinPaths(path, `${sessionName}%3AauthKey`)),
8
+ fs.readFile(joinPaths(path, `${sessionName}%3AdcId`)),
9
+ fs.readFile(joinPaths(path, `${sessionName}%3AserverAddress`)),
10
+ fs.readFile(joinPaths(path, `${sessionName}%3Aport`))
11
+ ]);
12
+ const authKeyParsed = JSON.parse(utf8.decoder.decode(fAuthKey));
13
+ if (typeof authKeyParsed !== "object" || authKeyParsed === null || !("type" in authKeyParsed) || authKeyParsed.type !== "Buffer" || !("data" in authKeyParsed) || !Array.isArray(authKeyParsed.data)) {
14
+ throw new Error("Invalid auth key file");
15
+ }
16
+ const authKey = new Uint8Array(authKeyParsed.data);
17
+ if (authKey.length !== 256) {
18
+ throw new Error("Invalid auth key length");
19
+ }
20
+ const dcId = Number.parseInt(utf8.decoder.decode(fDcId));
21
+ if (Number.isNaN(dcId)) {
22
+ throw new TypeError("Invalid dc id");
23
+ }
24
+ const port = Number.parseInt(utf8.decoder.decode(fPort));
25
+ if (Number.isNaN(port)) {
26
+ throw new TypeError("Invalid port");
27
+ }
28
+ const ip = JSON.parse(utf8.decoder.decode(fIpAddress));
29
+ if (typeof ip !== "string") {
30
+ throw new TypeError("Invalid server address");
31
+ }
32
+ return {
33
+ dcId,
34
+ ipAddress: ip,
35
+ ipv6: ip.includes(":"),
36
+ // dumb check but gramjs does this
37
+ port,
38
+ authKey
39
+ };
40
+ }
41
+ async function writeGramjsStoreSession(path, session, params) {
42
+ const fs = params?.fs ?? await import("node:fs/promises");
43
+ const sessionName = basename(path);
44
+ await fs.mkdir(sessionName, { recursive: true });
45
+ await Promise.all([
46
+ fs.writeFile(
47
+ joinPaths(path, `${sessionName}%3AauthKey`),
48
+ utf8.encoder.encode(JSON.stringify({
49
+ type: "Buffer",
50
+ data: Array.from(session.authKey)
51
+ }))
52
+ ),
53
+ fs.writeFile(joinPaths(path, `${sessionName}%3AdcId`), utf8.encoder.encode(session.dcId.toString())),
54
+ fs.writeFile(joinPaths(path, `${sessionName}%3AserverAddress`), utf8.encoder.encode(JSON.stringify(session.ipAddress))),
55
+ fs.writeFile(joinPaths(path, `${sessionName}%3Aport`), utf8.encoder.encode(session.port.toString()))
56
+ ]);
57
+ }
58
+ export {
59
+ readGramjsStoreSession,
60
+ writeGramjsStoreSession
61
+ };
@@ -8,6 +8,9 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
8
8
  function dirname(path) {
9
9
  return path.replace(/[/\\][^/\\]*$/, "");
10
10
  }
11
+ function basename(path) {
12
+ return path.replace(/^.*[/\\]/, "");
13
+ }
11
14
  function joinPaths(...paths) {
12
15
  const parts = [];
13
16
  let guessedSep;
@@ -29,5 +32,6 @@ function joinPaths(...paths) {
29
32
  }
30
33
  return parts.join(guessedSep ?? "/");
31
34
  }
35
+ exports.basename = basename;
32
36
  exports.dirname = dirname;
33
37
  exports.joinPaths = joinPaths;
@@ -1,6 +1,9 @@
1
1
  function dirname(path) {
2
2
  return path.replace(/[/\\][^/\\]*$/, "");
3
3
  }
4
+ function basename(path) {
5
+ return path.replace(/^.*[/\\]/, "");
6
+ }
4
7
  function joinPaths(...paths) {
5
8
  const parts = [];
6
9
  let guessedSep;
@@ -23,6 +26,7 @@ function joinPaths(...paths) {
23
26
  return parts.join(guessedSep ?? "/");
24
27
  }
25
28
  export {
29
+ basename,
26
30
  dirname,
27
31
  joinPaths
28
32
  };
@@ -1,4 +1,5 @@
1
1
  export * from './convert.js';
2
2
  export * from './parse.js';
3
3
  export * from './serialize.js';
4
+ export * from './store-session.js';
4
5
  export * from './types.js';
package/gramjs/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from './convert.js';
2
2
  export * from './parse.js';
3
3
  export * from './serialize.js';
4
+ export * from './store-session.js';
4
5
  export * from './types.js';
package/index.cjs CHANGED
@@ -9,6 +9,7 @@ const dcs = require("./convert/src/dcs.cjs");
9
9
  const convert = require("./convert/src/gramjs/convert.cjs");
10
10
  const parse = require("./convert/src/gramjs/parse.cjs");
11
11
  const serialize = require("./convert/src/gramjs/serialize.cjs");
12
+ const storeSession = require("./convert/src/gramjs/store-session.cjs");
12
13
  const convert$1 = require("./convert/src/mtkruto/convert.cjs");
13
14
  const parse$1 = require("./convert/src/mtkruto/parse.cjs");
14
15
  const serialize$1 = require("./convert/src/mtkruto/serialize.cjs");
@@ -28,6 +29,8 @@ exports.convertFromGramjsSession = convert.convertFromGramjsSession;
28
29
  exports.convertToGramjsSession = convert.convertToGramjsSession;
29
30
  exports.parseGramjsSession = parse.parseGramjsSession;
30
31
  exports.serializeGramjsSession = serialize.serializeGramjsSession;
32
+ exports.readGramjsStoreSession = storeSession.readGramjsStoreSession;
33
+ exports.writeGramjsStoreSession = storeSession.writeGramjsStoreSession;
31
34
  exports.convertFromMtkrutoSession = convert$1.convertFromMtkrutoSession;
32
35
  exports.convertToMtkrutoSession = convert$1.convertToMtkrutoSession;
33
36
  exports.parseMtkrutoSession = parse$1.parseMtkrutoSession;
package/index.js CHANGED
@@ -2,6 +2,7 @@ import { DC_MAPPING_PROD, DC_MAPPING_TEST, isTestDc } from "./convert/src/dcs.js
2
2
  import { convertFromGramjsSession, convertToGramjsSession } from "./convert/src/gramjs/convert.js";
3
3
  import { parseGramjsSession } from "./convert/src/gramjs/parse.js";
4
4
  import { serializeGramjsSession } from "./convert/src/gramjs/serialize.js";
5
+ import { readGramjsStoreSession, writeGramjsStoreSession } from "./convert/src/gramjs/store-session.js";
5
6
  import { convertFromMtkrutoSession, convertToMtkrutoSession } from "./convert/src/mtkruto/convert.js";
6
7
  import { parseMtkrutoSession } from "./convert/src/mtkruto/parse.js";
7
8
  import { serializeMtkrutoSession } from "./convert/src/mtkruto/serialize.js";
@@ -34,8 +35,10 @@ export {
34
35
  parsePyrogramSession,
35
36
  parseTelethonSession,
36
37
  qtBundle as qt,
38
+ readGramjsStoreSession,
37
39
  serializeGramjsSession,
38
40
  serializeMtkrutoSession,
39
41
  serializePyrogramSession,
40
- serializeTelethonSession
42
+ serializeTelethonSession,
43
+ writeGramjsStoreSession
41
44
  };
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@mtcute/convert",
3
3
  "type": "module",
4
- "version": "0.19.2",
4
+ "version": "0.19.4",
5
5
  "description": "Cross-library session conversion utilities",
6
6
  "license": "MIT",
7
7
  "dependencies": {
8
- "@mtcute/core": "^0.19.2",
8
+ "@mtcute/core": "^0.19.3",
9
9
  "@fuman/utils": "0.0.4",
10
- "@fuman/net": "0.0.8",
10
+ "@fuman/net": "0.0.9",
11
11
  "@fuman/io": "0.0.8"
12
12
  },
13
13
  "exports": {