chore: removed dispatcher dep from core (again), improved build script
This commit is contained in:
parent
e2d182519f
commit
8204dc86a4
9 changed files with 41 additions and 41 deletions
|
@ -1,19 +1,12 @@
|
||||||
import { createRequire } from 'module'
|
// eslint-disable-next-line no-restricted-imports
|
||||||
|
import { createWriteStream, rmSync } from 'fs'
|
||||||
|
import { writeFile } from 'fs/promises'
|
||||||
|
|
||||||
import { BaseTelegramClient, MtUnsupportedError } from '@mtcute/core'
|
import { BaseTelegramClient } from '@mtcute/core'
|
||||||
|
|
||||||
import { FileDownloadLocation, FileDownloadParameters, FileLocation } from '../../types/index.js'
|
import { FileDownloadLocation, FileDownloadParameters, FileLocation } from '../../types/index.js'
|
||||||
import { downloadAsIterable } from './download-iterable.js'
|
import { downloadAsIterable } from './download-iterable.js'
|
||||||
|
|
||||||
let fs: typeof import('fs') | null = null
|
|
||||||
|
|
||||||
try {
|
|
||||||
// @only-if-esm
|
|
||||||
const require = createRequire(import.meta.url)
|
|
||||||
// @/only-if-esm
|
|
||||||
fs = require('fs') as typeof import('fs')
|
|
||||||
} catch (e) {}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Download a remote file to a local file (only for NodeJS).
|
* Download a remote file to a local file (only for NodeJS).
|
||||||
* Promise will resolve once the download is complete.
|
* Promise will resolve once the download is complete.
|
||||||
|
@ -27,29 +20,18 @@ export async function downloadToFile(
|
||||||
location: FileDownloadLocation,
|
location: FileDownloadLocation,
|
||||||
params?: FileDownloadParameters,
|
params?: FileDownloadParameters,
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
if (!fs) {
|
|
||||||
throw new MtUnsupportedError('Downloading to file is only supported in NodeJS')
|
|
||||||
}
|
|
||||||
|
|
||||||
if (location instanceof FileLocation && ArrayBuffer.isView(location.location)) {
|
if (location instanceof FileLocation && ArrayBuffer.isView(location.location)) {
|
||||||
// early return for inline files
|
// early return for inline files
|
||||||
const buf = location.location
|
await writeFile(filename, location.location)
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
fs!.writeFile(filename, buf, (err) => {
|
|
||||||
if (err) reject(err)
|
|
||||||
else resolve()
|
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const output = fs.createWriteStream(filename)
|
const output = createWriteStream(filename)
|
||||||
|
|
||||||
if (params?.abortSignal) {
|
if (params?.abortSignal) {
|
||||||
params.abortSignal.addEventListener('abort', () => {
|
params.abortSignal.addEventListener('abort', () => {
|
||||||
client.log.debug('aborting file download %s - cleaning up', filename)
|
client.log.debug('aborting file download %s - cleaning up', filename)
|
||||||
output.destroy()
|
output.destroy()
|
||||||
fs!.rmSync(filename)
|
rmSync(filename)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -54,7 +54,6 @@
|
||||||
"@types/ws": "8.5.4",
|
"@types/ws": "8.5.4",
|
||||||
"node-forge": "1.3.1",
|
"node-forge": "1.3.1",
|
||||||
"@mtcute/test": "workspace:^",
|
"@mtcute/test": "workspace:^",
|
||||||
"@mtcute/dispatcher": "workspace:^",
|
|
||||||
"ws": "8.13.0"
|
"ws": "8.13.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
import { IStateStorage } from '@mtcute/dispatcher'
|
|
||||||
import { tl } from '@mtcute/tl'
|
import { tl } from '@mtcute/tl'
|
||||||
|
|
||||||
import { LruMap, toggleChannelIdMark } from '../utils/index.js'
|
import { LruMap, toggleChannelIdMark } from '../utils/index.js'
|
||||||
|
@ -56,7 +55,7 @@ export interface MemorySessionState {
|
||||||
|
|
||||||
const USERNAME_TTL = 86400000 // 24 hours
|
const USERNAME_TTL = 86400000 // 24 hours
|
||||||
|
|
||||||
export class MemoryStorage implements ITelegramStorage, IStateStorage {
|
export class MemoryStorage implements ITelegramStorage {
|
||||||
protected _state!: MemorySessionState
|
protected _state!: MemorySessionState
|
||||||
private _cachedInputPeers: LruMap<number, tl.TypeInputPeer> = new LruMap(100)
|
private _cachedInputPeers: LruMap<number, tl.TypeInputPeer> = new LruMap(100)
|
||||||
|
|
||||||
|
|
|
@ -1,12 +1,12 @@
|
||||||
import Long from 'long'
|
import Long from 'long'
|
||||||
import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'
|
import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||||
|
|
||||||
import type { IStateStorage } from '@mtcute/dispatcher'
|
|
||||||
import { createStub } from '@mtcute/test'
|
import { createStub } from '@mtcute/test'
|
||||||
import { tl } from '@mtcute/tl'
|
import { tl } from '@mtcute/tl'
|
||||||
import { __tlReaderMap } from '@mtcute/tl/binary/reader.js'
|
import { __tlReaderMap } from '@mtcute/tl/binary/reader.js'
|
||||||
import { __tlWriterMap } from '@mtcute/tl/binary/writer.js'
|
import { __tlWriterMap } from '@mtcute/tl/binary/writer.js'
|
||||||
|
|
||||||
|
import { MaybeAsync } from '../types/index.js'
|
||||||
import { defaultProductionDc } from '../utils/default-dcs.js'
|
import { defaultProductionDc } from '../utils/default-dcs.js'
|
||||||
import { LogManager } from '../utils/index.js'
|
import { LogManager } from '../utils/index.js'
|
||||||
import { ITelegramStorage } from './abstract.js'
|
import { ITelegramStorage } from './abstract.js'
|
||||||
|
@ -232,6 +232,17 @@ export function testStorage(s: ITelegramStorage): void {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface IStateStorage {
|
||||||
|
getState(key: string): MaybeAsync<unknown>
|
||||||
|
setState(key: string, state: unknown, ttl?: number): MaybeAsync<void>
|
||||||
|
deleteState(key: string): MaybeAsync<void>
|
||||||
|
getCurrentScene(key: string): MaybeAsync<string | null>
|
||||||
|
setCurrentScene(key: string, scene: string, ttl?: number): MaybeAsync<void>
|
||||||
|
deleteCurrentScene(key: string): MaybeAsync<void>
|
||||||
|
getRateLimit(key: string, limit: number, window: number): MaybeAsync<[number, number]>
|
||||||
|
resetRateLimit(key: string): MaybeAsync<void>
|
||||||
|
}
|
||||||
|
|
||||||
export function testStateStorage(s: IStateStorage) {
|
export function testStateStorage(s: IStateStorage) {
|
||||||
describe('key-value state', () => {
|
describe('key-value state', () => {
|
||||||
beforeAll(() => void vi.useFakeTimers())
|
beforeAll(() => void vi.useFakeTimers())
|
||||||
|
|
|
@ -1,5 +1,8 @@
|
||||||
import { MaybeAsync } from '@mtcute/client'
|
import { MaybeAsync } from '@mtcute/client'
|
||||||
|
|
||||||
|
// ⚠️ Important: when modifying the below interface, also update it
|
||||||
|
// in packages/core/src/storage/storage.test-utils.ts
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Interface for FSM storage for the dispatcher.
|
* Interface for FSM storage for the dispatcher.
|
||||||
*
|
*
|
||||||
|
|
|
@ -1,5 +1,3 @@
|
||||||
// ^^ because of this._socket. we know it's not null, almost everywhere, but TS doesn't
|
|
||||||
|
|
||||||
import { connect as connectTcp } from 'net'
|
import { connect as connectTcp } from 'net'
|
||||||
import { connect as connectTls, SecureContextOptions } from 'tls'
|
import { connect as connectTls, SecureContextOptions } from 'tls'
|
||||||
|
|
||||||
|
|
|
@ -130,9 +130,6 @@ importers:
|
||||||
specifier: 5.2.3
|
specifier: 5.2.3
|
||||||
version: 5.2.3
|
version: 5.2.3
|
||||||
devDependencies:
|
devDependencies:
|
||||||
'@mtcute/dispatcher':
|
|
||||||
specifier: workspace:^
|
|
||||||
version: link:../dispatcher
|
|
||||||
'@mtcute/test':
|
'@mtcute/test':
|
||||||
specifier: workspace:^
|
specifier: workspace:^
|
||||||
version: link:../test
|
version: link:../test
|
||||||
|
|
|
@ -130,16 +130,17 @@ if (buildConfig.buildTs) {
|
||||||
|
|
||||||
let tsconfig = fs.readFileSync(path.join(packageDir, 'tsconfig.backup.json'), 'utf-8')
|
let tsconfig = fs.readFileSync(path.join(packageDir, 'tsconfig.backup.json'), 'utf-8')
|
||||||
// what the fuck
|
// what the fuck
|
||||||
tsconfig = tsconfig.replace(
|
tsconfig = tsconfig.replace(/(?<="extends": "\.\.\/\.\.\/)tsconfig\.json(?=",)/, 'tsconfig.build.json')
|
||||||
/("extends": "\.\.\/\.\.\/tsconfig\.json",)/,
|
|
||||||
'$1"exclude": ["**/*.{test,test-utils}.ts"],',
|
|
||||||
)
|
|
||||||
fs.writeFileSync(path.join(packageDir, 'tsconfig.json'), tsconfig)
|
fs.writeFileSync(path.join(packageDir, 'tsconfig.json'), tsconfig)
|
||||||
|
|
||||||
|
const restoreTsconfig = () => {
|
||||||
|
fs.renameSync(path.join(packageDir, 'tsconfig.backup.json'), path.join(packageDir, 'tsconfig.json'))
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
exec('pnpm exec tsc --build', { cwd: packageDir, stdio: 'inherit' })
|
exec('pnpm exec tsc --build', { cwd: packageDir, stdio: 'inherit' })
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
fs.renameSync(path.join(packageDir, 'tsconfig.backup.json'), path.join(packageDir, 'tsconfig.json'))
|
restoreTsconfig()
|
||||||
throw e
|
throw e
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -169,10 +170,13 @@ if (buildConfig.buildTs) {
|
||||||
fs.writeFileSync(f, originalFiles[f])
|
fs.writeFileSync(f, originalFiles[f])
|
||||||
}
|
}
|
||||||
|
|
||||||
if (error) throw error
|
if (error) {
|
||||||
|
restoreTsconfig()
|
||||||
|
throw error
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fs.renameSync(path.join(packageDir, 'tsconfig.backup.json'), path.join(packageDir, 'tsconfig.json'))
|
restoreTsconfig()
|
||||||
|
|
||||||
console.log('[i] Post-processing...')
|
console.log('[i] Post-processing...')
|
||||||
|
|
||||||
|
|
7
tsconfig.build.json
Normal file
7
tsconfig.build.json
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
{
|
||||||
|
"extends": "./tsconfig.json",
|
||||||
|
"exclude": [
|
||||||
|
"**/*.test.ts",
|
||||||
|
"**/*.test-utils.ts",
|
||||||
|
]
|
||||||
|
}
|
Loading…
Reference in a new issue