Forráskód Böngészése

Add support for browsers NPM package.

painor 3 éve
szülő
commit
34ad655b43
51 módosított fájl, 507 hozzáadás és 230 törlés
  1. 3 0
      gramjs/CryptoFile-BROWSER.ts
  2. 5 0
      gramjs/CryptoFile.ts
  3. 1 4
      gramjs/Helpers.ts
  4. 1 2
      gramjs/Password.ts
  5. 5 5
      gramjs/Utils.ts
  6. 1 1
      gramjs/Version.ts
  7. 4 3
      gramjs/client/TelegramClient.ts
  8. 1 1
      gramjs/client/chats.ts
  9. 16 0
      gramjs/client/fs-BROWSER.ts
  10. 1 0
      gramjs/client/fs.ts
  11. 1 1
      gramjs/client/index.ts
  12. 1 1
      gramjs/client/messageParse.ts
  13. 9 0
      gramjs/client/os-BROWSER.ts
  14. 3 0
      gramjs/client/os.ts
  15. 6 0
      gramjs/client/path-BROWSER.ts
  16. 3 0
      gramjs/client/path.ts
  17. 5 5
      gramjs/client/telegramBaseClient.ts
  18. 7 7
      gramjs/client/uploads.ts
  19. 1 1
      gramjs/events/CallbackQuery.ts
  20. 3 3
      gramjs/extensions/Logger.ts
  21. 1 0
      gramjs/extensions/PromisedNetSockets-BROWSER.ts
  22. 5 5
      gramjs/extensions/PromisedNetSockets.ts
  23. 12 0
      gramjs/extensions/net-BROWSER.ts
  24. 1 0
      gramjs/extensions/net.ts
  25. 3 0
      gramjs/extensions/socks-BROWSER.ts
  26. 1 0
      gramjs/extensions/socks.ts
  27. 3 0
      gramjs/inspect-BROWSER.ts
  28. 1 0
      gramjs/inspect.ts
  29. 2 2
      gramjs/network/connection/Connection.ts
  30. 1 1
      gramjs/network/connection/TCPFull.ts
  31. 1 1
      gramjs/network/connection/TCPMTProxy.ts
  32. 1 1
      gramjs/sessions/StoreSession.ts
  33. 5 0
      gramjs/sessions/localStorage-BROWSER.ts
  34. 1 0
      gramjs/sessions/localStorage.ts
  35. 1 1
      gramjs/tl/api.js
  36. 1 1
      gramjs/tl/custom/button.ts
  37. 1 1
      gramjs/tl/custom/chatGetter.ts
  38. 1 1
      gramjs/tl/custom/dialog.ts
  39. 1 1
      gramjs/tl/custom/draft.ts
  40. 1 1
      gramjs/tl/custom/file.ts
  41. 1 1
      gramjs/tl/custom/forward.ts
  42. 1 1
      gramjs/tl/custom/inlineResult.ts
  43. 1 1
      gramjs/tl/custom/inlineResults.ts
  44. 1 1
      gramjs/tl/custom/message.ts
  45. 1 1
      gramjs/tl/custom/messageButton.ts
  46. 1 1
      gramjs/tl/custom/senderGetter.ts
  47. 216 147
      package-lock.json
  48. 10 11
      package.json
  49. 152 15
      publish_npm.js
  50. 1 1
      tsconfig.json
  51. 1 0
      webpack.config.js

+ 3 - 0
gramjs/CryptoFile-BROWSER.ts

@@ -0,0 +1,3 @@
+import * as crypto from "./crypto/crypto";
+
+export default crypto;

+ 5 - 0
gramjs/CryptoFile.ts

@@ -0,0 +1,5 @@
+import { isNode } from "./platform";
+
+const crypto = require(isNode ? "crypto" : "./crypto/crypto");
+
+export default crypto;

+ 1 - 4
gramjs/Helpers.ts

@@ -1,10 +1,7 @@
 import bigInt from "big-integer";
-import { isNode } from "./platform";
 import type { EntityLike } from "./define";
 import type { Api } from "./tl";
-
-export const IS_NODE = isNode;
-const crypto = require(isNode ? "crypto" : "./crypto/crypto");
+import crypto from "./CryptoFile";
 
 /**
  * converts a buffer to big int

+ 1 - 2
gramjs/Password.ts

@@ -8,8 +8,7 @@ import {
     sha256,
 } from "./Helpers";
 import bigInt from "big-integer";
-import { isNode } from "./platform";
-const crypto = require(isNode ? "crypto" : "./crypto/crypto");
+import crypto from "./CryptoFile";
 
 const SIZE_FOR_HASH = 256;
 

+ 5 - 5
gramjs/Utils.ts

@@ -3,7 +3,7 @@ import { Api } from "./tl";
 import bigInt from "big-integer";
 import * as markdown from "./extensions/markdown";
 import { EntityCache } from "./entityCache";
-import mime from "mime-types";
+import mime from "mime";
 import type { ParseInterface } from "./client/messageParse";
 import { MarkdownParser } from "./extensions/markdown";
 import { CustomFile } from "./client/uploads";
@@ -583,7 +583,7 @@ export function isAudio(file: any): boolean {
         return (metadata.get("mimeType") || "").startsWith("audio/");
     } else {
         file = "a" + ext;
-        return (mime.lookup(file) || "").startsWith("audio/");
+        return (mime.getType(file) || "").startsWith("audio/");
     }
 }
 
@@ -623,7 +623,7 @@ export function getExtension(media: any): string {
             // Octet stream are just bytes, which have no default extension
             return "";
         } else {
-            return mime.extension(media.mimeType) || "";
+            return mime.getExtension(media.mimeType) || "";
         }
     }
     return "";
@@ -659,7 +659,7 @@ function isVideo(file: any): boolean {
         }
     } else {
         file = "a" + ext;
-        return (mime.lookup(file) || "").startsWith("video/");
+        return (mime.getType(file) || "").startsWith("video/");
     }
 }
 
@@ -682,7 +682,7 @@ export function getAttributes(
     const name: string =
         typeof file == "string" ? file : file.name || "unnamed";
     if (mimeType === undefined) {
-        mimeType = mime.lookup(name) || "application/octet-stream";
+        mimeType = mime.getType(name) || "application/octet-stream";
     }
     const attrObj = new Map();
     attrObj.set(

+ 1 - 1
gramjs/Version.ts

@@ -1 +1 @@
-export const version = "2.5.4";
+export const version = "2.5.20";

+ 4 - 3
gramjs/client/TelegramClient.ts

@@ -19,17 +19,18 @@ import type { EventBuilder } from "../events/common";
 import { MTProtoSender } from "../network";
 
 import { LAYER } from "../tl/AllTLObjects";
-import { betterConsoleLog, IS_NODE } from "../Helpers";
+import { betterConsoleLog } from "../Helpers";
 import { DownloadMediaInterface } from "./downloads";
 import { NewMessage, NewMessageEvent } from "../events";
 import { _handleUpdate, _updateLoop } from "./updates";
 import { Session } from "../sessions";
-import { inspect } from "util";
 import { Album, AlbumEvent } from "../events/Album";
 import { CallbackQuery, CallbackQueryEvent } from "../events/CallbackQuery";
 import { EditedMessage, EditedMessageEvent } from "../events/EditedMessage";
 import { DeletedMessage, DeletedMessageEvent } from "../events/DeletedMessage";
 import { LogLevel } from "../extensions/Logger";
+import { inspect } from "../inspect";
+import { isNode } from "../platform";
 
 /**
  * The TelegramClient uses several methods in different files to provide all the common functionality in a nice interface.</br>
@@ -1422,7 +1423,7 @@ export class TelegramClient extends TelegramBaseClient {
         downloadDC = false
     ): Promise<{ id: number; ipAddress: string; port: number }> {
         this._log.debug(`Getting DC ${dcId}`);
-        if (!IS_NODE) {
+        if (!isNode) {
             switch (dcId) {
                 case 1:
                     return {

+ 1 - 1
gramjs/client/chats.ts

@@ -5,7 +5,7 @@ import { RequestIter } from "../requestIter";
 import { helpers, utils } from "../";
 import { Api } from "../tl";
 import bigInt, { BigInteger } from "big-integer";
-import { inspect } from "util";
+import { inspect } from "../inspect";
 
 const _MAX_PARTICIPANTS_CHUNK_SIZE = 200;
 const _MAX_ADMIN_LOG_CHUNK_SIZE = 100;

+ 16 - 0
gramjs/client/fs-BROWSER.ts

@@ -0,0 +1,16 @@
+export const promises = {
+    lstat: (file: string) => {
+        return {
+            isFile: () => {},
+        };
+    },
+    stat: (file: string) => {
+        return {
+            size: 0,
+        };
+    },
+
+    readFile(path: string) {
+        return Buffer.alloc(0);
+    },
+};

+ 1 - 0
gramjs/client/fs.ts

@@ -0,0 +1 @@
+export * from "fs";

+ 1 - 1
gramjs/client/index.ts

@@ -27,5 +27,5 @@ export {
     tgClient,
     updates,
     uploads,
-    users
+    users,
 };

+ 1 - 1
gramjs/client/messageParse.ts

@@ -1,5 +1,5 @@
 import { getPeerId, sanitizeParseMode } from "../Utils";
-import { Api } from "../tl";
+import { Api } from "../tl/api";
 import type { EntityLike } from "../define";
 import type { TelegramClient } from "./TelegramClient";
 import { utils } from "../index";

+ 9 - 0
gramjs/client/os-BROWSER.ts

@@ -0,0 +1,9 @@
+export default {
+    type: () => {
+        return "Browser";
+    },
+
+    release: () => {
+        return "1.0";
+    },
+};

+ 3 - 0
gramjs/client/os.ts

@@ -0,0 +1,3 @@
+import * as os from "os";
+
+export default os;

+ 6 - 0
gramjs/client/path-BROWSER.ts

@@ -0,0 +1,6 @@
+// No paths in browser so we should throw an error
+export default {
+    basename(file: string) {
+        return "";
+    },
+};

+ 3 - 0
gramjs/client/path.ts

@@ -0,0 +1,3 @@
+import path from "path";
+
+export default path;

+ 5 - 5
gramjs/client/telegramBaseClient.ts

@@ -1,5 +1,5 @@
 import { Connection, TelegramClient, version } from "../";
-import { IS_NODE, sleep } from "../Helpers";
+import { sleep } from "../Helpers";
 import {
     ConnectionTCPFull,
     ConnectionTCPObfuscated,
@@ -8,7 +8,7 @@ import { Session, StoreSession } from "../sessions";
 import { Logger } from "../extensions";
 import { Api } from "../tl";
 
-import os from "os";
+import os from "./os";
 import type { AuthKey } from "../crypto/AuthKey";
 import { EntityCache } from "../entityCache";
 import type { ParseInterface } from "./messageParse";
@@ -23,13 +23,13 @@ import {
 } from "../network/connection/TCPMTProxy";
 import { Semaphore } from "async-mutex";
 import { LogLevel } from "../extensions/Logger";
-import { isBrowser } from "../platform";
+import { isBrowser, isNode } from "../platform";
 
 const EXPORTED_SENDER_RECONNECT_TIMEOUT = 1000; // 1 sec
 const EXPORTED_SENDER_RELEASE_TIMEOUT = 30000; // 30 sec
 
 const DEFAULT_DC_ID = 4;
-const DEFAULT_IPV4_IP = IS_NODE ? "149.154.167.91" : "vesta.web.telegram.org";
+const DEFAULT_IPV4_IP = isNode ? "149.154.167.91" : "vesta.web.telegram.org";
 const DEFAULT_IPV6_IP = "2001:067c:04e8:f004:0000:0000:0000:000a";
 
 /**
@@ -121,7 +121,7 @@ export interface TelegramClientParams {
 }
 
 const clientParamsDefault = {
-    connection: IS_NODE ? ConnectionTCPFull : ConnectionTCPObfuscated,
+    connection: isNode ? ConnectionTCPFull : ConnectionTCPObfuscated,
     useIPV6: false,
     timeout: 10,
     requestRetries: 5,

+ 7 - 7
gramjs/client/uploads.ts

@@ -4,8 +4,8 @@ import { TelegramClient } from "./TelegramClient";
 import { generateRandomBytes, readBigIntFromBuffer, sleep } from "../Helpers";
 import { getAppropriatedPartSize, getInputMedia } from "../Utils";
 import { EntityLike, FileLike, MarkupLike, MessageIDLike } from "../define";
-import path from "path";
-import { promises as fs } from "fs";
+import path from "./path";
+import { promises as fs } from "./fs";
 import { errors, utils } from "../index";
 import { _parseMessageText } from "./messageParse";
 import { getCommentData } from "./messages";
@@ -344,7 +344,7 @@ export async function _fileToMedia(
             } else {
                 name = "unnamed";
             }
-            if (file instanceof Buffer) {
+            if (Buffer.isBuffer(file)) {
                 createdFile = new CustomFile(name, file.length, "", file);
             }
         }
@@ -404,7 +404,7 @@ export async function _fileToMedia(
                 } else {
                     name = "unnamed";
                 }
-                if (thumb instanceof Buffer) {
+                if (Buffer.isBuffer(thumb)) {
                     uploadedThumb = new CustomFile(
                         name,
                         thumb.length,
@@ -651,14 +651,14 @@ export async function sendFile(
     return client._getResponseMessage(request, result, entity) as Api.Message;
 }
 
-function fileToBuffer(file: File | CustomFile) {
+function fileToBuffer(file: File | CustomFile): Promise<Buffer> | Buffer {
     if (typeof File !== "undefined" && file instanceof File) {
-        return new Response(file).arrayBuffer();
+        return new Response(file).arrayBuffer() as Promise<Buffer>;
     } else if (file instanceof CustomFile) {
         if (file.buffer != undefined) {
             return file.buffer;
         } else {
-            return fs.readFile(file.path);
+            return fs.readFile(file.path) as unknown as Buffer;
         }
     } else {
         throw new Error("Could not create buffer from file " + file);

+ 1 - 1
gramjs/events/CallbackQuery.ts

@@ -60,7 +60,7 @@ export class CallbackQuery extends EventBuilder {
             return new CallbackQueryEvent(update, update.peer, update.msgId);
         } else if (update instanceof Api.UpdateInlineBotCallbackQuery) {
             const b = toSignedLittleBuffer(update.msgId.id, 8);
-            const msgId = b.readInt32LE();
+            const msgId = b.readInt32LE(0);
             const peerId = b.readInt32LE(4);
             const peer =
                 peerId < 0

+ 3 - 3
gramjs/extensions/Logger.ts

@@ -1,7 +1,7 @@
-import { IS_NODE } from "../Helpers";
-
 // let _level: string | undefined = undefined;
 
+import { isNode } from "../platform";
+
 export enum LogLevel {
     NONE = "none",
     ERROR = "error",
@@ -29,7 +29,7 @@ export class Logger {
         //     _level = level || "info"; // defaults to info
         // }
         this._logLevel = level || LogLevel.INFO;
-        this.isBrowser = !IS_NODE;
+        this.isBrowser = !isNode;
         if (!this.isBrowser) {
             this.colors = {
                 start: "\x1b[2m",

+ 1 - 0
gramjs/extensions/PromisedNetSockets-BROWSER.ts

@@ -0,0 +1 @@
+export class PromisedNetSockets {}

+ 5 - 5
gramjs/extensions/PromisedNetSockets.ts

@@ -1,5 +1,5 @@
-import { Socket } from "net";
-import { SocksClient } from "socks";
+import * as net from "./net";
+import { SocksClient } from "./socks";
 
 import { Mutex } from "async-mutex";
 import { ProxyInterface } from "../network/connection/TCPMTProxy";
@@ -9,7 +9,7 @@ const mutex = new Mutex();
 const closeError = new Error("NetSocket was closed");
 
 export class PromisedNetSockets {
-    private client?: Socket;
+    private client?: net.Socket;
     private closed: boolean;
     private stream: Buffer;
     private canRead?: boolean | Promise<boolean>;
@@ -108,7 +108,7 @@ export class PromisedNetSockets {
             this.client = info.socket;
             connected = true;
         } else {
-            this.client = new Socket();
+            this.client = new net.Socket();
         }
 
         this.canRead = new Promise((resolve) => {
@@ -158,7 +158,7 @@ export class PromisedNetSockets {
 
     async receive() {
         if (this.client) {
-            this.client.on("data", async (message) => {
+            this.client.on("data", async (message: Buffer) => {
                 const release = await mutex.acquire();
                 try {
                     let data;

+ 12 - 0
gramjs/extensions/net-BROWSER.ts

@@ -0,0 +1,12 @@
+export class Socket {
+    public destroyed?: boolean;
+
+    connect(...args: any): any {}
+
+    on(...args: any): any {}
+
+    write(...args: any): any {}
+
+    destroy(...args: any): any {}
+    unref(...args: any): any {}
+}

+ 1 - 0
gramjs/extensions/net.ts

@@ -0,0 +1 @@
+export * from "net";

+ 3 - 0
gramjs/extensions/socks-BROWSER.ts

@@ -0,0 +1,3 @@
+export const SocksClient = {
+    createConnection: (x: any): any => {},
+};

+ 1 - 0
gramjs/extensions/socks.ts

@@ -0,0 +1 @@
+export * from "socks";

+ 3 - 0
gramjs/inspect-BROWSER.ts

@@ -0,0 +1,3 @@
+export const inspect = {
+    custom: "",
+};

+ 1 - 0
gramjs/inspect.ts

@@ -0,0 +1 @@
+export { inspect } from "util";

+ 2 - 2
gramjs/network/connection/Connection.ts

@@ -4,7 +4,7 @@ import {
     PromisedWebSockets,
 } from "../../extensions";
 import { AsyncQueue } from "../../extensions";
-import { IS_NODE } from "../../Helpers";
+import { isNode } from "../../platform";
 import { AbridgedPacketCodec } from "./TCPAbridged";
 import { FullPacketCodec } from "./TCPFull";
 import { ProxyInterface } from "./TCPMTProxy";
@@ -55,7 +55,7 @@ class Connection {
         this._obfuscation = undefined; // TcpObfuscated and MTProxy
         this._sendArray = new AsyncQueue();
         this._recvArray = new AsyncQueue();
-        this.socket = IS_NODE
+        this.socket = isNode
             ? new PromisedNetSockets(this._proxy)
             : new PromisedWebSockets();
 

+ 1 - 1
gramjs/network/connection/TCPFull.ts

@@ -34,7 +34,7 @@ export class FullPacketCodec extends PacketCodec {
         reader: PromisedNetSockets | PromisedWebSockets
     ): Promise<Buffer> {
         const packetLenSeq = await reader.readExactly(8); // 4 and 4
-        // process.exit(0);
+
         if (packetLenSeq === undefined) {
             // Return empty buffer in case of issue
             return Buffer.alloc(0);

+ 1 - 1
gramjs/network/connection/TCPMTProxy.ts

@@ -94,7 +94,7 @@ class MTProxyIO {
             Buffer.from(random.slice(60)),
         ]);
         const dcIdBytes = Buffer.alloc(2);
-        dcIdBytes.writeInt8(this._dcId);
+        dcIdBytes.writeInt8(this._dcId, 0);
         random = Buffer.concat([
             Buffer.from(random.slice(0, 60)),
             dcIdBytes,

+ 1 - 1
gramjs/sessions/StoreSession.ts

@@ -10,7 +10,7 @@ export class StoreSession extends MemorySession {
     constructor(sessionName: string, divider = ":") {
         super();
         if (typeof localStorage === "undefined" || localStorage === null) {
-            const LocalStorage = require("node-localstorage").LocalStorage;
+            const LocalStorage = require("./localStorage").LocalStorage;
             this.store = store.area("fs", new LocalStorage("./" + sessionName));
         } else {
             this.store = store.area("fs", localStorage);

+ 5 - 0
gramjs/sessions/localStorage-BROWSER.ts

@@ -0,0 +1,5 @@
+export class LocalStorage {
+    constructor(location: string) {
+        throw new Error("Do not call me");
+    }
+}

+ 1 - 0
gramjs/sessions/localStorage.ts

@@ -0,0 +1 @@
+export const LocalStorage = require("node-localstorage").LocalStorage;

+ 1 - 1
gramjs/tl/api.js

@@ -1,4 +1,4 @@
-const { inspect } = require("util");
+const { inspect } = require("../inspect");
 const bigInt = require("big-integer");
 
 const {

+ 1 - 1
gramjs/tl/custom/button.ts

@@ -1,8 +1,8 @@
 import type { ButtonLike, EntityLike } from "../../define";
 import { Api } from "../api";
 import { utils } from "../../";
-import { inspect } from "util";
 import { betterConsoleLog } from "../../Helpers";
+import { inspect } from "../../inspect";
 
 export class Button {
     public button: ButtonLike;

+ 1 - 1
gramjs/tl/custom/chatGetter.ts

@@ -2,8 +2,8 @@ import type { Entity, EntityLike } from "../../define";
 import type { TelegramClient } from "../../client/TelegramClient";
 import { utils } from "../../";
 import { Api } from "../api";
-import { inspect } from "util";
 import { betterConsoleLog, returnBigInt } from "../../Helpers";
+import { inspect } from "../../inspect";
 
 export interface ChatGetterConstructorParams {
     chatPeer?: EntityLike;

+ 1 - 1
gramjs/tl/custom/dialog.ts

@@ -3,9 +3,9 @@ import { Api } from "../api";
 import type { Entity } from "../../define";
 import { getDisplayName, getInputPeer, getPeerId } from "../../Utils";
 import { Draft } from "./draft";
-import { inspect } from "util";
 import { betterConsoleLog, returnBigInt } from "../../Helpers";
 import bigInt from "big-integer";
+import { inspect } from "../../inspect";
 
 export class Dialog {
     _client: TelegramClient;

+ 1 - 1
gramjs/tl/custom/draft.ts

@@ -2,8 +2,8 @@ import type { Entity } from "../../define";
 import type { TelegramClient } from "../..";
 import { getInputPeer, getPeer } from "../../Utils";
 import { Api } from "../api";
-import { inspect } from "util";
 import { betterConsoleLog } from "../../Helpers";
+import { inspect } from "../../inspect";
 
 export class Draft {
     private _client: TelegramClient;

+ 1 - 1
gramjs/tl/custom/file.ts

@@ -1,8 +1,8 @@
 import type { FileLike } from "../../define";
 import { Api } from "../api";
 import { _photoSizeByteCount } from "../../Utils";
-import { inspect } from "util";
 import { betterConsoleLog } from "../../Helpers";
+import { inspect } from "../../inspect";
 
 export class File {
     private readonly media: FileLike;

+ 1 - 1
gramjs/tl/custom/forward.ts

@@ -10,7 +10,7 @@ import {
     returnBigInt,
 } from "../../Helpers";
 import { _getEntityPair, getPeerId } from "../../Utils";
-import { inspect } from "util";
+import { inspect } from "../../inspect";
 
 export class Forward extends SenderGetter {
     private originalFwd: Api.MessageFwdHeader;

+ 1 - 1
gramjs/tl/custom/inlineResult.ts

@@ -2,8 +2,8 @@ import type { TelegramClient } from "../..";
 import type { EntityLike, MessageIDLike } from "../../define";
 import { Api } from "../api";
 import { utils } from "../../";
-import { inspect } from "util";
 import { betterConsoleLog } from "../../Helpers";
+import { inspect } from "../../inspect";
 
 export class InlineResult {
     private _ARTICLE = "article";

+ 1 - 1
gramjs/tl/custom/inlineResults.ts

@@ -2,8 +2,8 @@ import type { TelegramClient } from "../..";
 import type { EntityLike } from "../../define";
 import { Api } from "../api";
 import { InlineResult } from "./inlineResult";
-import { inspect } from "util";
 import { betterConsoleLog } from "../../Helpers";
+import { inspect } from "../../inspect";
 
 export class InlineResults extends Array<InlineResult> {
     private result: Api.messages.TypeBotResults;

+ 1 - 1
gramjs/tl/custom/message.ts

@@ -12,12 +12,12 @@ import {
     UpdatePinMessageParams,
 } from "../../client/messages";
 import { DownloadMediaInterface } from "../../client/downloads";
-import { inspect } from "util";
 import { betterConsoleLog, returnBigInt } from "../../Helpers";
 import { _selfId } from "../../client/users";
 import bigInt, { BigInteger } from "big-integer";
 import { LogLevel } from "../../extensions/Logger";
 import { MessageButton } from "./messageButton";
+import { inspect } from "../../inspect";
 
 interface MessageBaseInterface {
     id: any;

+ 1 - 1
gramjs/tl/custom/messageButton.ts

@@ -2,9 +2,9 @@ import type { TelegramClient } from "../../client/TelegramClient";
 import type { ButtonLike, EntityLike, MessageIDLike } from "../../define";
 import { Api } from "../api";
 import { Button } from "./button";
-import { inspect } from "util";
 import { betterConsoleLog } from "../../Helpers";
 import { computeCheck } from "../../Password";
+import { inspect } from "../../inspect";
 
 export class MessageButton {
     private readonly _client: TelegramClient;

+ 1 - 1
gramjs/tl/custom/senderGetter.ts

@@ -1,10 +1,10 @@
 import type { Entity } from "../../define";
 import type { TelegramClient } from "../..";
 import { Api } from "../api";
-import { inspect } from "util";
 import { betterConsoleLog } from "../../Helpers";
 import { ChatGetter } from "./chatGetter";
 import bigInt from "big-integer";
+import { inspect } from "../../inspect";
 
 interface SenderGetterConstructorInterface {
     senderId?: bigInt.BigInteger;

A különbségek nem kerülnek megjelenítésre, a fájl túl nagy
+ 216 - 147
package-lock.json


+ 10 - 11
package.json

@@ -1,6 +1,6 @@
 {
   "name": "telegram",
-  "version": "2.5.4",
+  "version": "2.5.26",
   "description": "NodeJS/Browser MTProto API Telegram client library,",
   "main": "index.js",
   "types": "index.d.ts",
@@ -33,15 +33,17 @@
     "@babel/core": "^7.12.13",
     "@babel/plugin-proposal-class-properties": "^7.12.13",
     "@babel/preset-env": "^7.12.16",
-    "@types/mime-types": "^2.1.0",
+    "@types/mime": "^2.0.3",
     "@types/node": "^15.12.0",
     "@types/node-localstorage": "^1.3.0",
     "@types/pako": "^1.0.1",
     "@types/websocket": "^1.0.4",
     "babel-loader": "^8.2.2",
     "jest": "^27.2.5",
+    "os-browserify": "^0.3.0",
+    "process": "^0.11.10",
+    "util": "^0.12.4",
     "prettier": "2.3.1",
-    "raw-loader": "^4.0.2",
     "ts-loader": "^8.0.16",
     "ts-node": "^9.1.1",
     "typedoc": "^0.22.11",
@@ -56,16 +58,13 @@
     "big-integer": "^1.6.48",
     "buffer": "^6.0.3",
     "htmlparser2": "^6.1.0",
-    "mime-types": "^2.1.30",
-    "node-localstorage": "^2.1.6",
-    "os-browserify": "^0.3.0",
+    "mime": "^3.0.0",
     "pako": "^2.0.3",
     "path-browserify": "^1.0.1",
-    "process": "^0.11.10",
-    "socks": "^2.6.1",
     "store2": "^2.12.0",
     "ts-custom-error": "^3.2.0",
-    "util": "^0.12.4",
-    "websocket": "^1.0.34"
+    "websocket": "^1.0.34",
+    "node-localstorage": "^2.1.6",
+    "socks": "^2.6.1"
   }
-}
+}

+ 152 - 15
publish_npm.js

@@ -1,16 +1,105 @@
 const { exec } = require("child_process");
 const fs = require("fs");
-fs.rmSync("dist", { recursive: true, force: true });
-
-const tsc = exec("tsc");
-tsc.on("close", (code) => {
-  if (code === 0) {
-    fs.copyFileSync("package.json", "dist/package.json");
-    fs.copyFileSync("README.md", "dist/README.md");
-    fs.copyFileSync("LICENSE", "dist/LICENSE");
-    fs.copyFileSync("gramjs/tl/api.d.ts", "dist/tl/api.d.ts");
-    fs.copyFileSync("gramjs/define.d.ts", "dist/define.d.ts");
-    const npm_publish = exec("npm publish", { cwd: "dist" });
+const path = require("path");
+
+// TODO if someone is brave enough to make all of this readable please do
+
+function addBuffer(dir) {
+  fs.readdirSync(dir).forEach((file) => {
+    let fullPath = path.join(dir, file);
+    if (fs.lstatSync(fullPath).isDirectory()) {
+      addBuffer(fullPath);
+    } else {
+      if (
+        (fullPath.endsWith(".ts") || fullPath.endsWith(".js")) &&
+        (!fullPath.endsWith(".d.ts") || fullPath.endsWith("api.d.ts"))
+      ) {
+        const tsFile = fs.readFileSync(fullPath, "utf8");
+        if (tsFile.includes("Buffer")) {
+          const newTsFile = 'import { Buffer } from "buffer/";\n' + tsFile;
+          fs.writeFileSync(fullPath, newTsFile, "utf8");
+        }
+      }
+    }
+  });
+}
+
+function renameFiles(dir, action) {
+  fs.readdirSync(dir).forEach((file) => {
+    let fullPath = path.join(dir, file);
+    if (fs.lstatSync(fullPath).isDirectory()) {
+      renameFiles(fullPath, action);
+    } else {
+      if (fullPath.includes("example")) {
+        fs.unlinkSync(fullPath);
+      }
+
+      if (fullPath.includes("-BROWSER")) {
+        console.log(action, fullPath);
+
+        if (action === "rename") {
+          fs.renameSync(fullPath, fullPath.replace("-BROWSER", ""));
+        } else if (action === "delete") {
+          fs.unlinkSync(fullPath);
+        }
+      }
+    }
+  });
+}
+
+function copyFolderSync(from, to) {
+  fs.mkdirSync(to);
+  fs.readdirSync(from).forEach((element) => {
+    if (fs.lstatSync(path.join(from, element)).isFile()) {
+      fs.copyFileSync(path.join(from, element), path.join(to, element));
+    } else {
+      copyFolderSync(path.join(from, element), path.join(to, element));
+    }
+  });
+}
+
+fs.rmSync("browser", { recursive: true, force: true });
+fs.rmSync("tempBrowser", { recursive: true, force: true });
+copyFolderSync("gramjs", "tempBrowser");
+addBuffer("tempBrowser");
+// easier that writing two files smh
+const tsconfig = fs.readFileSync("tsconfig.json", "utf8");
+let newTsconfig = tsconfig.replace(/\.\/dist/g, "./browser");
+newTsconfig = newTsconfig.replace(/gramjs/g, "tempBrowser");
+fs.writeFileSync("tsconfig.json", newTsconfig, "utf8");
+const packageJSON = JSON.parse(fs.readFileSync("package.json", "utf8"));
+const oldValueStorage = packageJSON.dependencies["node-localstorage"];
+const oldValueSocks = packageJSON.dependencies["socks"];
+delete packageJSON.dependencies["node-localstorage"];
+delete packageJSON.dependencies["socks"];
+const oldVersion = packageJSON.version.split(".");
++oldVersion[2]++;
+packageJSON.version = oldVersion.join(".");
+fs.writeFileSync(
+  "package.json",
+  JSON.stringify(packageJSON, null, "  "),
+  "utf8"
+);
+const npmi = exec("npm i");
+npmi.on("close", (code) => {
+  if (code !== 0) {
+    throw new Error("Error happened " + code);
+  }
+
+  const tsc = exec("tsc");
+  tsc.on("close", (code) => {
+    if (code !== 0) {
+      throw new Error("Error happened " + code);
+    }
+
+    fs.copyFileSync("package.json", "browser/package.json");
+    fs.copyFileSync("README.md", "browser/README.md");
+    fs.copyFileSync("LICENSE", "browser/LICENSE");
+    fs.copyFileSync("gramjs/tl/api.d.ts", "browser/tl/api.d.ts");
+    fs.copyFileSync("gramjs/define.d.ts", "browser/define.d.ts");
+    renameFiles("browser", "rename");
+
+    const npm_publish = exec("npm publish --tag browser", { cwd: "browser" });
     npm_publish.stdout.on("data", function (data) {
       console.log(data.toString());
     });
@@ -22,13 +111,61 @@ tsc.on("close", (code) => {
     npm_publish.on("close", (code) => {
       if (code === 0) {
         console.log("=====================================");
-        console.log("FINISHED UPLOADING");
+        console.log("FINISHED UPLOADING BROWSER VERSION");
         console.log("=====================================");
       } else {
         throw new Error("something went wrong");
       }
     });
-  } else {
-    throw new Error("Error happened");
-  }
+    fs.rmSync("tempBrowser", { recursive: true, force: true });
+    fs.rmSync("dist", { recursive: true, force: true });
+    // easier that writing two files smh
+    const tsconfig = fs.readFileSync("tsconfig.json", "utf8");
+    let newTsconfig = tsconfig.replace(/\.\/browser/g, "./dist");
+    newTsconfig = newTsconfig.replace(/tempBrowser/g, "gramjs");
+    fs.writeFileSync("tsconfig.json", newTsconfig, "utf8");
+    const packageJSON = JSON.parse(fs.readFileSync("package.json", "utf8"));
+    packageJSON.dependencies["node-localstorage"] = oldValueStorage;
+    packageJSON.dependencies["socks"] = oldValueSocks;
+    const oldVersion = packageJSON.version.split(".");
+    +oldVersion[2]++;
+    packageJSON.version = oldVersion.join(".");
+
+    fs.writeFileSync(
+      "package.json",
+      JSON.stringify(packageJSON, null, "  "),
+      "utf8"
+    );
+    const tsc = exec("tsc");
+    tsc.on("close", (code) => {
+      if (code === 0) {
+        fs.copyFileSync("package.json", "dist/package.json");
+        fs.copyFileSync("README.md", "dist/README.md");
+        fs.copyFileSync("LICENSE", "dist/LICENSE");
+        fs.copyFileSync("gramjs/tl/api.d.ts", "dist/tl/api.d.ts");
+        fs.copyFileSync("gramjs/define.d.ts", "dist/define.d.ts");
+        renameFiles("dist", "delete");
+        /*const npm_publish = exec("npm publish", { cwd: "dist" });
+        npm_publish.stdout.on("data", function (data) {
+          console.log(data.toString());
+        });
+
+        npm_publish.stderr.on("data", function (data) {
+          console.error(data.toString());
+        });
+        npm_publish.on("close", (code) => {
+          if (code === 0) {
+            console.log("=====================================");
+            console.log("FINISHED UPLOADING NODE VERSION");
+            console.log("=====================================");
+          } else {
+            throw new Error("something went wrong");
+          }
+        });*/
+      } else {
+        console.log(code);
+        throw new Error("Error happened");
+      }
+    });
+  });
 });

+ 1 - 1
tsconfig.json

@@ -3,7 +3,7 @@
     "module": "commonjs",
     "target": "es2017",
     "lib": ["dom", "es7"],
-    "inlineSourceMap": true,
+    "sourceMap": false,
     "downlevelIteration": true,
     "allowJs": true,
     "esModuleInterop": true,

+ 1 - 0
webpack.config.js

@@ -30,6 +30,7 @@ module.exports = {
       util: require.resolve("util/"),
       assert: false,
       stream: false,
+      events: false,
       constants: false,
     },
   },

Nem az összes módosított fájl került megjelenítésre, mert túl sok fájl változott