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 { 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).
|
||||
* Promise will resolve once the download is complete.
|
||||
|
@ -27,29 +20,18 @@ export async function downloadToFile(
|
|||
location: FileDownloadLocation,
|
||||
params?: FileDownloadParameters,
|
||||
): Promise<void> {
|
||||
if (!fs) {
|
||||
throw new MtUnsupportedError('Downloading to file is only supported in NodeJS')
|
||||
}
|
||||
|
||||
if (location instanceof FileLocation && ArrayBuffer.isView(location.location)) {
|
||||
// early return for inline files
|
||||
const buf = location.location
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
fs!.writeFile(filename, buf, (err) => {
|
||||
if (err) reject(err)
|
||||
else resolve()
|
||||
})
|
||||
})
|
||||
await writeFile(filename, location.location)
|
||||
}
|
||||
|
||||
const output = fs.createWriteStream(filename)
|
||||
const output = createWriteStream(filename)
|
||||
|
||||
if (params?.abortSignal) {
|
||||
params.abortSignal.addEventListener('abort', () => {
|
||||
client.log.debug('aborting file download %s - cleaning up', filename)
|
||||
output.destroy()
|
||||
fs!.rmSync(filename)
|
||||
rmSync(filename)
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
@ -54,7 +54,6 @@
|
|||
"@types/ws": "8.5.4",
|
||||
"node-forge": "1.3.1",
|
||||
"@mtcute/test": "workspace:^",
|
||||
"@mtcute/dispatcher": "workspace:^",
|
||||
"ws": "8.13.0"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
import { IStateStorage } from '@mtcute/dispatcher'
|
||||
import { tl } from '@mtcute/tl'
|
||||
|
||||
import { LruMap, toggleChannelIdMark } from '../utils/index.js'
|
||||
|
@ -56,7 +55,7 @@ export interface MemorySessionState {
|
|||
|
||||
const USERNAME_TTL = 86400000 // 24 hours
|
||||
|
||||
export class MemoryStorage implements ITelegramStorage, IStateStorage {
|
||||
export class MemoryStorage implements ITelegramStorage {
|
||||
protected _state!: MemorySessionState
|
||||
private _cachedInputPeers: LruMap<number, tl.TypeInputPeer> = new LruMap(100)
|
||||
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
import Long from 'long'
|
||||
import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
|
||||
import type { IStateStorage } from '@mtcute/dispatcher'
|
||||
import { createStub } from '@mtcute/test'
|
||||
import { tl } from '@mtcute/tl'
|
||||
import { __tlReaderMap } from '@mtcute/tl/binary/reader.js'
|
||||
import { __tlWriterMap } from '@mtcute/tl/binary/writer.js'
|
||||
|
||||
import { MaybeAsync } from '../types/index.js'
|
||||
import { defaultProductionDc } from '../utils/default-dcs.js'
|
||||
import { LogManager } from '../utils/index.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) {
|
||||
describe('key-value state', () => {
|
||||
beforeAll(() => void vi.useFakeTimers())
|
||||
|
|
|
@ -1,5 +1,8 @@
|
|||
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.
|
||||
*
|
||||
|
|
|
@ -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 connectTls, SecureContextOptions } from 'tls'
|
||||
|
||||
|
|
|
@ -130,9 +130,6 @@ importers:
|
|||
specifier: 5.2.3
|
||||
version: 5.2.3
|
||||
devDependencies:
|
||||
'@mtcute/dispatcher':
|
||||
specifier: workspace:^
|
||||
version: link:../dispatcher
|
||||
'@mtcute/test':
|
||||
specifier: workspace:^
|
||||
version: link:../test
|
||||
|
|
|
@ -130,16 +130,17 @@ if (buildConfig.buildTs) {
|
|||
|
||||
let tsconfig = fs.readFileSync(path.join(packageDir, 'tsconfig.backup.json'), 'utf-8')
|
||||
// what the fuck
|
||||
tsconfig = tsconfig.replace(
|
||||
/("extends": "\.\.\/\.\.\/tsconfig\.json",)/,
|
||||
'$1"exclude": ["**/*.{test,test-utils}.ts"],',
|
||||
)
|
||||
tsconfig = tsconfig.replace(/(?<="extends": "\.\.\/\.\.\/)tsconfig\.json(?=",)/, 'tsconfig.build.json')
|
||||
fs.writeFileSync(path.join(packageDir, 'tsconfig.json'), tsconfig)
|
||||
|
||||
const restoreTsconfig = () => {
|
||||
fs.renameSync(path.join(packageDir, 'tsconfig.backup.json'), path.join(packageDir, 'tsconfig.json'))
|
||||
}
|
||||
|
||||
try {
|
||||
exec('pnpm exec tsc --build', { cwd: packageDir, stdio: 'inherit' })
|
||||
} catch (e) {
|
||||
fs.renameSync(path.join(packageDir, 'tsconfig.backup.json'), path.join(packageDir, 'tsconfig.json'))
|
||||
restoreTsconfig()
|
||||
throw e
|
||||
}
|
||||
|
||||
|
@ -169,10 +170,13 @@ if (buildConfig.buildTs) {
|
|||
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...')
|
||||
|
||||
|
|
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