chore: some cleanup
This commit is contained in:
parent
2cbed9b883
commit
2b0d4d99aa
14 changed files with 199 additions and 27 deletions
|
@ -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",
|
||||
|
|
|
@ -1 +0,0 @@
|
|||
../../node/src/common-internals-node
|
|
@ -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
|
||||
|
|
|
@ -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 }
|
||||
|
||||
|
|
|
@ -1,2 +0,0 @@
|
|||
this folder is for common code across `@mtcute/node` and `@mtcute/bun`.
|
||||
it is symlinked into `@mtcute/bun`
|
|
@ -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'
|
||||
|
|
54
packages/node/src/utils/exit-hook.ts
Normal file
54
packages/node/src/utils/exit-hook.ts
Normal file
|
@ -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<string, () => 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)
|
||||
}
|
||||
}
|
38
packages/node/src/utils/logging.ts
Normal file
38
packages/node/src/utils/logging.ts
Normal file
|
@ -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)
|
||||
}
|
|
@ -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 }
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -12,7 +12,7 @@
|
|||
"moduleResolution": "Bundler",
|
||||
"resolveJsonModule": true,
|
||||
"types": [
|
||||
"bun-types",
|
||||
"bun",
|
||||
"node",
|
||||
"deno/ns",
|
||||
"vite/client"
|
||||
|
|
Loading…
Reference in a new issue