From 2b0d4d99aa74d7f3126ed937c2d2d06f871a7a82 Mon Sep 17 00:00:00 2001 From: alina sireneva Date: Tue, 26 Nov 2024 20:21:47 +0300 Subject: [PATCH] chore: some cleanup --- package.json | 4 +- packages/bun/src/common-internals-node | 1 - packages/bun/src/platform.ts | 24 ++++- .../src/utils}/exit-hook.ts | 0 .../src/utils}/logging.ts | 0 packages/node/src/client.ts | 2 +- .../node/src/common-internals-node/readme.md | 2 - packages/node/src/index.ts | 2 +- packages/node/src/utils/exit-hook.ts | 54 +++++++++++ packages/node/src/utils/logging.ts | 38 ++++++++ .../platform.ts | 0 packages/node/src/worker.ts | 2 +- pnpm-lock.yaml | 95 ++++++++++++++++--- tsconfig.json | 2 +- 14 files changed, 199 insertions(+), 27 deletions(-) delete mode 120000 packages/bun/src/common-internals-node rename packages/{node/src/common-internals-node => bun/src/utils}/exit-hook.ts (100%) rename packages/{node/src/common-internals-node => bun/src/utils}/logging.ts (100%) delete mode 100644 packages/node/src/common-internals-node/readme.md create mode 100644 packages/node/src/utils/exit-hook.ts create mode 100644 packages/node/src/utils/logging.ts rename packages/node/src/{common-internals-node => utils}/platform.ts (100%) diff --git a/package.json b/package.json index 42752970..c3daffea 100644 --- a/package.json +++ b/package.json @@ -47,17 +47,17 @@ }, "devDependencies": { "@antfu/eslint-config": "2.26.0", - "@fuman/build": "https://pkg.pr.new/teidesu/fuman/@fuman/build@6017eb4", + "@fuman/build": "https://pkg.pr.new/teidesu/fuman/@fuman/build@1a73dc1", "@fuman/utils": "https://pkg.pr.new/teidesu/fuman/@fuman/utils@6017eb4", "@types/deno": "npm:@teidesu/deno-types@1.46.3", "@types/node": "20.10.0", + "@types/bun": "1.1.14", "@types/ws": "8.5.4", "@vitest/browser": "2.0.5", "@vitest/coverage-v8": "2.0.5", "@vitest/expect": "2.0.5", "@vitest/spy": "2.0.5", "@vitest/ui": "2.0.5", - "bun-types": "^1.1.24", "chai": "5.1.0", "cjs-module-lexer": "1.2.3", "dotenv-flow": "4.1.0", diff --git a/packages/bun/src/common-internals-node b/packages/bun/src/common-internals-node deleted file mode 120000 index d48d33e3..00000000 --- a/packages/bun/src/common-internals-node +++ /dev/null @@ -1 +0,0 @@ -../../node/src/common-internals-node \ No newline at end of file diff --git a/packages/bun/src/platform.ts b/packages/bun/src/platform.ts index 60ff10ce..9febdefa 100644 --- a/packages/bun/src/platform.ts +++ b/packages/bun/src/platform.ts @@ -1,14 +1,32 @@ import * as os from 'node:os' -import { NodePlatform } from './common-internals-node/platform.js' -import { normalizeFile } from './utils/normalize-file.js' +import type { ICorePlatform } from '@mtcute/core' -export class BunPlatform extends NodePlatform { +import { normalizeFile } from './utils/normalize-file.js' +import { defaultLoggingHandler } from './utils/logging.js' +import { beforeExit } from './utils/exit-hook.js' + +export class BunPlatform implements ICorePlatform { + // ICorePlatform + declare log: typeof defaultLoggingHandler + declare beforeExit: typeof beforeExit declare normalizeFile: typeof normalizeFile getDeviceModel(): string { return `Bun/${Bun.version} (${os.type()} ${os.arch()})` } + + getDefaultLogLevel(): number | null { + const envLogLevel = Number.parseInt(process.env.MTCUTE_LOG_LEVEL ?? '') + + if (!Number.isNaN(envLogLevel)) { + return envLogLevel + } + + return null + } } BunPlatform.prototype.normalizeFile = normalizeFile +BunPlatform.prototype.log = defaultLoggingHandler +BunPlatform.prototype.beforeExit = beforeExit diff --git a/packages/node/src/common-internals-node/exit-hook.ts b/packages/bun/src/utils/exit-hook.ts similarity index 100% rename from packages/node/src/common-internals-node/exit-hook.ts rename to packages/bun/src/utils/exit-hook.ts diff --git a/packages/node/src/common-internals-node/logging.ts b/packages/bun/src/utils/logging.ts similarity index 100% rename from packages/node/src/common-internals-node/logging.ts rename to packages/bun/src/utils/logging.ts diff --git a/packages/node/src/client.ts b/packages/node/src/client.ts index 6801faa3..8c442a70 100644 --- a/packages/node/src/client.ts +++ b/packages/node/src/client.ts @@ -18,7 +18,7 @@ import { downloadAsNodeStream } from './methods/download-node-stream.js' import { SqliteStorage } from './sqlite/index.js' import { NodeCryptoProvider } from './utils/crypto.js' import { TcpTransport } from './utils/tcp.js' -import { NodePlatform } from './common-internals-node/platform.js' +import { NodePlatform } from './utils/platform.js' export type { TelegramClientOptions } diff --git a/packages/node/src/common-internals-node/readme.md b/packages/node/src/common-internals-node/readme.md deleted file mode 100644 index 688abefa..00000000 --- a/packages/node/src/common-internals-node/readme.md +++ /dev/null @@ -1,2 +0,0 @@ -this folder is for common code across `@mtcute/node` and `@mtcute/bun`. -it is symlinked into `@mtcute/bun` diff --git a/packages/node/src/index.ts b/packages/node/src/index.ts index d801a59d..8abfead8 100644 --- a/packages/node/src/index.ts +++ b/packages/node/src/index.ts @@ -1,5 +1,5 @@ export * from './client.js' -export * from './common-internals-node/platform.js' +export * from './utils/platform.js' export * from './sqlite/index.js' export * from './utils/tcp.js' export * from './utils/proxies.js' diff --git a/packages/node/src/utils/exit-hook.ts b/packages/node/src/utils/exit-hook.ts new file mode 100644 index 00000000..03466c51 --- /dev/null +++ b/packages/node/src/utils/exit-hook.ts @@ -0,0 +1,54 @@ +// roughly based on https://github.com/sindresorhus/exit-hook/blob/main/index.js, MIT license + +let installed = false +let handled = false + +const callbacks = new Set<() => void>() + +const myHandlers = new Map void>() + +function register(shouldManuallyExit: boolean, signal: number, event: string) { + function eventHandler() { + if (handled) { + return + } + + handled = true + + for (const callback of callbacks) { + callback() + } + + for (const [event, handler] of myHandlers) { + process.off(event, handler) + } + + if (shouldManuallyExit) { + // send the signal again and let node handle it + process.kill(process.pid, signal) + } + } + + process.on(event, eventHandler) + myHandlers.set(event, eventHandler) +} + +export function beforeExit(fn: () => void): () => void { + // unsupported platform + if (typeof process === 'undefined') return () => {} + + if (!installed) { + installed = true + + register(true, 0, 'beforeExit') + register(true, 2, 'SIGINT') + register(true, 15, 'SIGTERM') + register(false, 15, 'exit') + } + + callbacks.add(fn) + + return () => { + callbacks.delete(fn) + } +} diff --git a/packages/node/src/utils/logging.ts b/packages/node/src/utils/logging.ts new file mode 100644 index 00000000..7eb7b4fb --- /dev/null +++ b/packages/node/src/utils/logging.ts @@ -0,0 +1,38 @@ +const isTty = typeof process === 'object' && Boolean(process.stdout?.isTTY) + +const BASE_FORMAT = isTty ? '%s [%s] [%s%s\x1B[0m] ' : '%s [%s] [%s] ' +const LEVEL_NAMES = isTty + ? [ + '', // OFF + '\x1B[31mERR\x1B[0m', + '\x1B[33mWRN\x1B[0m', + '\x1B[34mINF\x1B[0m', + '\x1B[36mDBG\x1B[0m', + '\x1B[35mVRB\x1B[0m', + ] + : [ + '', // OFF + 'ERR', + 'WRN', + 'INF', + 'DBG', + 'VRB', + ] +const TAG_COLORS = [6, 2, 3, 4, 5, 1].map(i => `\x1B[3${i};1m`) + +/** @internal */ +export const defaultLoggingHandler: ( + color: number, + level: number, + tag: string, + fmt: string, + args: unknown[] +) => void = isTty + ? (color: number, level: number, tag: string, fmt: string, args: unknown[]): void => { + // eslint-disable-next-line no-console + console.log(BASE_FORMAT + fmt, new Date().toISOString(), LEVEL_NAMES[level], TAG_COLORS[color], tag, ...args) + } + : (color: number, level: number, tag: string, fmt: string, args: unknown[]): void => { + // eslint-disable-next-line no-console + console.log(BASE_FORMAT + fmt, new Date().toISOString(), LEVEL_NAMES[level], tag, ...args) + } diff --git a/packages/node/src/common-internals-node/platform.ts b/packages/node/src/utils/platform.ts similarity index 100% rename from packages/node/src/common-internals-node/platform.ts rename to packages/node/src/utils/platform.ts diff --git a/packages/node/src/worker.ts b/packages/node/src/worker.ts index f5e9ae5b..4cca621d 100644 --- a/packages/node/src/worker.ts +++ b/packages/node/src/worker.ts @@ -14,7 +14,7 @@ import { TelegramWorkerPort as TelegramWorkerPortBase, } from '@mtcute/core/worker.js' -import { NodePlatform } from './common-internals-node/platform.js' +import { NodePlatform } from './utils/platform.js' export type { TelegramWorkerOptions, WorkerCustomMethods } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 1619f251..3d0ecc22 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -15,11 +15,14 @@ importers: specifier: 2.26.0 version: 2.26.0(@typescript-eslint/utils@8.14.0(eslint@9.9.0)(typescript@5.5.4))(@vue/compiler-sfc@3.5.13)(eslint@9.9.0)(typescript@5.5.4)(vitest@2.0.5(@types/node@20.10.0)(@vitest/browser@2.0.5)(@vitest/ui@2.0.5)) '@fuman/build': - specifier: https://pkg.pr.new/teidesu/fuman/@fuman/build@6017eb4 - version: https://pkg.pr.new/teidesu/fuman/@fuman/build@6017eb4(tough-cookie@4.1.4)(typescript@5.5.4)(vite@5.4.2(@types/node@20.10.0)) + specifier: https://pkg.pr.new/teidesu/fuman/@fuman/build@1a73dc1 + version: https://pkg.pr.new/teidesu/fuman/@fuman/build@1a73dc1(tough-cookie@4.1.4)(typescript@5.5.4)(vite@5.4.2(@types/node@20.10.0)) '@fuman/utils': specifier: https://pkg.pr.new/teidesu/fuman/@fuman/utils@6017eb4 version: https://pkg.pr.new/teidesu/fuman/@fuman/utils@6017eb4 + '@types/bun': + specifier: 1.1.14 + version: 1.1.14 '@types/deno': specifier: npm:@teidesu/deno-types@1.46.3 version: '@teidesu/deno-types@1.46.3' @@ -44,9 +47,6 @@ importers: '@vitest/ui': specifier: 2.0.5 version: 2.0.5(vitest@2.0.5) - bun-types: - specifier: ^1.1.24 - version: 1.1.34 chai: specifier: 5.1.0 version: 5.1.0 @@ -904,8 +904,8 @@ packages: resolution: {integrity: sha512-BsWiH1yFGjXXS2yvrf5LyuoSIIbPrGUWob917o+BTKuZ7qJdxX8aJLRxs1fS9n6r7vESrq1OUqb68dANcFXuQQ==} engines: {node: ^18.18.0 || ^20.9.0 || >=21.1.0} - '@fuman/build@https://pkg.pr.new/teidesu/fuman/@fuman/build@6017eb4': - resolution: {tarball: https://pkg.pr.new/teidesu/fuman/@fuman/build@6017eb4} + '@fuman/build@https://pkg.pr.new/teidesu/fuman/@fuman/build@1a73dc1': + resolution: {tarball: https://pkg.pr.new/teidesu/fuman/@fuman/build@1a73dc1} version: 0.0.1 hasBin: true peerDependencies: @@ -916,6 +916,24 @@ packages: resolution: {tarball: https://pkg.pr.new/teidesu/fuman/@fuman/bun@6017eb4} version: 0.0.1 + '@fuman/fetch@https://pkg.pr.new/teidesu/fuman/@fuman/fetch@1a73dc1': + resolution: {tarball: https://pkg.pr.new/teidesu/fuman/@fuman/fetch@1a73dc1} + version: 0.0.1 + peerDependencies: + tough-cookie: ^5.0.0 || ^4.0.0 + valibot: ^0.42.0 + yup: ^1.0.0 + zod: ^3.0.0 + peerDependenciesMeta: + tough-cookie: + optional: true + valibot: + optional: true + yup: + optional: true + zod: + optional: true + '@fuman/fetch@https://pkg.pr.new/teidesu/fuman/@fuman/fetch@6017eb4': resolution: {tarball: https://pkg.pr.new/teidesu/fuman/@fuman/fetch@6017eb4} version: 0.0.1 @@ -934,18 +952,34 @@ packages: zod: optional: true + '@fuman/io@https://pkg.pr.new/teidesu/fuman/@fuman/io@1a73dc1': + resolution: {tarball: https://pkg.pr.new/teidesu/fuman/@fuman/io@1a73dc1} + version: 0.0.1 + '@fuman/io@https://pkg.pr.new/teidesu/fuman/@fuman/io@6017eb4': resolution: {tarball: https://pkg.pr.new/teidesu/fuman/@fuman/io@6017eb4} version: 0.0.1 + '@fuman/net@https://pkg.pr.new/teidesu/fuman/@fuman/net@1a73dc1': + resolution: {tarball: https://pkg.pr.new/teidesu/fuman/@fuman/net@1a73dc1} + version: 0.0.1 + '@fuman/net@https://pkg.pr.new/teidesu/fuman/@fuman/net@6017eb4': resolution: {tarball: https://pkg.pr.new/teidesu/fuman/@fuman/net@6017eb4} version: 0.0.1 + '@fuman/node@https://pkg.pr.new/teidesu/fuman/@fuman/node@1a73dc1': + resolution: {tarball: https://pkg.pr.new/teidesu/fuman/@fuman/node@1a73dc1} + version: 0.0.1 + '@fuman/node@https://pkg.pr.new/teidesu/fuman/@fuman/node@6017eb4': resolution: {tarball: https://pkg.pr.new/teidesu/fuman/@fuman/node@6017eb4} version: 0.0.1 + '@fuman/utils@https://pkg.pr.new/teidesu/fuman/@fuman/utils@1a73dc1': + resolution: {tarball: https://pkg.pr.new/teidesu/fuman/@fuman/utils@1a73dc1} + version: 0.0.1 + '@fuman/utils@https://pkg.pr.new/teidesu/fuman/@fuman/utils@6017eb4': resolution: {tarball: https://pkg.pr.new/teidesu/fuman/@fuman/utils@6017eb4} version: 0.0.1 @@ -1276,6 +1310,9 @@ packages: '@types/better-sqlite3@7.6.4': resolution: {integrity: sha512-dzrRZCYPXIXfSR1/surNbJ/grU3scTaygS0OMzjlGf71i9sc2fGyHPXXiXmEvNIoE0cGwsanEFMVJxPXmco9Eg==} + '@types/bun@1.1.14': + resolution: {integrity: sha512-opVYiFGtO2af0dnWBdZWlioLBoxSdDO5qokaazLhq8XQtGZbY4pY3/JxY8Zdf/hEwGubbp7ErZXoN1+h2yesxA==} + '@types/cookie@0.6.0': resolution: {integrity: sha512-4Kh9a6B2bQciAhf7FSuMRRkUWecJgJu9nPnx3yzpsfXX/c50REIqpHY4C82bXP90qrLtXtkDxTZosYO3UpOwlA==} @@ -1691,8 +1728,8 @@ packages: builtin-status-codes@3.0.0: resolution: {integrity: sha512-HpGFw18DgFWlncDfjTa2rcQ4W88O1mC8e8yZ2AvQY5KDaktSTwo+KRf6nHK6FRI5FyRyb/5T6+TSxfP7QyGsmQ==} - bun-types@1.1.34: - resolution: {integrity: sha512-br5QygTEL/TwB4uQOb96Ky22j4Gq2WxWH/8Oqv20fk5HagwKXo/akB+LiYgSfzexCt6kkcUaVm+bKiPl71xPvw==} + bun-types@1.1.37: + resolution: {integrity: sha512-C65lv6eBr3LPJWFZ2gswyrGZ82ljnH8flVE03xeXxKhi2ZGtFiO4isRKTKnitbSqtRAcaqYSR6djt1whI66AbA==} cac@6.7.14: resolution: {integrity: sha512-b6Ilus+c3RrdDk+JhLKUAQfzzgLEPy6wcXqS7f/xe1EETvsDP6GORG7SFuOs6cID5YkqchW/LXZbX5bc8j7ZcQ==} @@ -4253,13 +4290,13 @@ snapshots: '@eslint/object-schema@2.1.4': {} - '@fuman/build@https://pkg.pr.new/teidesu/fuman/@fuman/build@6017eb4(tough-cookie@4.1.4)(typescript@5.5.4)(vite@5.4.2(@types/node@20.10.0))': + '@fuman/build@https://pkg.pr.new/teidesu/fuman/@fuman/build@1a73dc1(tough-cookie@4.1.4)(typescript@5.5.4)(vite@5.4.2(@types/node@20.10.0))': dependencies: '@drizzle-team/brocli': 0.10.2 - '@fuman/fetch': https://pkg.pr.new/teidesu/fuman/@fuman/fetch@6017eb4(tough-cookie@4.1.4)(zod@3.23.8) - '@fuman/io': https://pkg.pr.new/teidesu/fuman/@fuman/io@6017eb4 - '@fuman/node': https://pkg.pr.new/teidesu/fuman/@fuman/node@6017eb4 - '@fuman/utils': https://pkg.pr.new/teidesu/fuman/@fuman/utils@6017eb4 + '@fuman/fetch': https://pkg.pr.new/teidesu/fuman/@fuman/fetch@1a73dc1(tough-cookie@4.1.4)(zod@3.23.8) + '@fuman/io': https://pkg.pr.new/teidesu/fuman/@fuman/io@1a73dc1 + '@fuman/node': https://pkg.pr.new/teidesu/fuman/@fuman/node@1a73dc1 + '@fuman/utils': https://pkg.pr.new/teidesu/fuman/@fuman/utils@1a73dc1 cross-spawn: 7.0.3 detect-indent: 7.0.1 js-yaml: 4.1.0 @@ -4280,6 +4317,13 @@ snapshots: '@fuman/net': https://pkg.pr.new/teidesu/fuman/@fuman/net@6017eb4 '@fuman/utils': https://pkg.pr.new/teidesu/fuman/@fuman/utils@6017eb4 + '@fuman/fetch@https://pkg.pr.new/teidesu/fuman/@fuman/fetch@1a73dc1(tough-cookie@4.1.4)(zod@3.23.8)': + dependencies: + '@fuman/utils': https://pkg.pr.new/teidesu/fuman/@fuman/utils@1a73dc1 + optionalDependencies: + tough-cookie: 4.1.4 + zod: 3.23.8 + '@fuman/fetch@https://pkg.pr.new/teidesu/fuman/@fuman/fetch@6017eb4(tough-cookie@4.1.4)(zod@3.23.8)': dependencies: '@fuman/utils': https://pkg.pr.new/teidesu/fuman/@fuman/utils@6017eb4 @@ -4287,21 +4331,38 @@ snapshots: tough-cookie: 4.1.4 zod: 3.23.8 + '@fuman/io@https://pkg.pr.new/teidesu/fuman/@fuman/io@1a73dc1': + dependencies: + '@fuman/utils': https://pkg.pr.new/teidesu/fuman/@fuman/utils@1a73dc1 + '@fuman/io@https://pkg.pr.new/teidesu/fuman/@fuman/io@6017eb4': dependencies: '@fuman/utils': https://pkg.pr.new/teidesu/fuman/@fuman/utils@6017eb4 + '@fuman/net@https://pkg.pr.new/teidesu/fuman/@fuman/net@1a73dc1': + dependencies: + '@fuman/io': https://pkg.pr.new/teidesu/fuman/@fuman/io@1a73dc1 + '@fuman/utils': https://pkg.pr.new/teidesu/fuman/@fuman/utils@1a73dc1 + '@fuman/net@https://pkg.pr.new/teidesu/fuman/@fuman/net@6017eb4': dependencies: '@fuman/io': https://pkg.pr.new/teidesu/fuman/@fuman/io@6017eb4 '@fuman/utils': https://pkg.pr.new/teidesu/fuman/@fuman/utils@6017eb4 + '@fuman/node@https://pkg.pr.new/teidesu/fuman/@fuman/node@1a73dc1': + dependencies: + '@fuman/io': https://pkg.pr.new/teidesu/fuman/@fuman/io@1a73dc1 + '@fuman/net': https://pkg.pr.new/teidesu/fuman/@fuman/net@1a73dc1 + '@fuman/utils': https://pkg.pr.new/teidesu/fuman/@fuman/utils@1a73dc1 + '@fuman/node@https://pkg.pr.new/teidesu/fuman/@fuman/node@6017eb4': dependencies: '@fuman/io': https://pkg.pr.new/teidesu/fuman/@fuman/io@6017eb4 '@fuman/net': https://pkg.pr.new/teidesu/fuman/@fuman/net@6017eb4 '@fuman/utils': https://pkg.pr.new/teidesu/fuman/@fuman/utils@6017eb4 + '@fuman/utils@https://pkg.pr.new/teidesu/fuman/@fuman/utils@1a73dc1': {} + '@fuman/utils@https://pkg.pr.new/teidesu/fuman/@fuman/utils@6017eb4': {} '@humanwhocodes/module-importer@1.0.1': {} @@ -4665,6 +4726,10 @@ snapshots: dependencies: '@types/node': 20.10.0 + '@types/bun@1.1.14': + dependencies: + bun-types: 1.1.37 + '@types/cookie@0.6.0': {} '@types/cross-spawn@6.0.6': @@ -5202,7 +5267,7 @@ snapshots: builtin-status-codes@3.0.0: {} - bun-types@1.1.34: + bun-types@1.1.37: dependencies: '@types/node': 20.12.14 '@types/ws': 8.5.13 diff --git a/tsconfig.json b/tsconfig.json index d30d7318..d46a47c7 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -12,7 +12,7 @@ "moduleResolution": "Bundler", "resolveJsonModule": true, "types": [ - "bun-types", + "bun", "node", "deno/ns", "vite/client"