chore: export core utils from @mtcute/core/utils

This commit is contained in:
alina 🌸 2023-09-21 13:18:09 +03:00
parent 67487e14c9
commit 7a6a30ea5c
Signed by: teidesu
SSH key fingerprint: SHA256:uNeCpw6aTSU4aIObXLvHfLkDa82HWH9EiOj9AXOIRpI
45 changed files with 105 additions and 74 deletions

View file

@ -9,7 +9,8 @@
"scripts": { "scripts": {
"test": "mocha -r ts-node/register \"tests/**/*.spec.ts\"", "test": "mocha -r ts-node/register \"tests/**/*.spec.ts\"",
"docs": "typedoc", "docs": "typedoc",
"build": "tsc" "build": "tsc",
"gen-client": "node ./scripts/generate-client.js"
}, },
"dependencies": { "dependencies": {
"@types/node": "18.16.0", "@types/node": "18.16.0",

View file

@ -3,16 +3,18 @@
import { Readable } from 'stream' import { Readable } from 'stream'
import { import {
AsyncLock,
BaseTelegramClient, BaseTelegramClient,
BaseTelegramClientOptions, BaseTelegramClientOptions,
Deque,
MaybeArray, MaybeArray,
MaybeAsync, MaybeAsync,
SortedLinkedList,
} from '@mtcute/core' } from '@mtcute/core'
import { ConditionVariable } from '@mtcute/core/src/utils/condition-variable' import {
import { Logger } from '@mtcute/core/src/utils/logger' AsyncLock,
ConditionVariable,
Deque,
Logger,
SortedLinkedList,
} from '@mtcute/core/utils'
import { tdFileId } from '@mtcute/file-id' import { tdFileId } from '@mtcute/file-id'
import { tl } from '@mtcute/tl' import { tl } from '@mtcute/tl'

View file

@ -5,7 +5,6 @@ export * from './utils/peer-utils'
export { createDummyUpdate } from './utils/updates-utils' export { createDummyUpdate } from './utils/updates-utils'
export { export {
assertNever, assertNever,
defaultDcs,
JsonFileStorage, JsonFileStorage,
LocalstorageStorage, LocalstorageStorage,
MemoryStorage, MemoryStorage,

View file

@ -2,9 +2,15 @@
import { Readable } from 'stream' import { Readable } from 'stream'
// @copy // @copy
import { AsyncLock, MaybeArray, MaybeAsync } from '@mtcute/core' import { MaybeArray, MaybeAsync } from '@mtcute/core'
// @copy // @copy
import { Logger } from '@mtcute/core/src/utils/logger' import {
AsyncLock,
ConditionVariable,
Deque,
Logger,
SortedLinkedList,
} from '@mtcute/core/utils'
// @copy // @copy
import { tdFileId } from '@mtcute/file-id' import { tdFileId } from '@mtcute/file-id'

View file

@ -1,4 +1,4 @@
import { computeSrpParams } from '@mtcute/core' import { computeSrpParams } from '@mtcute/core/utils'
import { TelegramClient } from '../../client' import { TelegramClient } from '../../client'
import { User } from '../../types' import { User } from '../../types'

View file

@ -1,4 +1,4 @@
import { computeSrpParams } from '@mtcute/core' import { computeSrpParams } from '@mtcute/core/utils'
import { tl } from '@mtcute/tl' import { tl } from '@mtcute/tl'
import { TelegramClient } from '../../client' import { TelegramClient } from '../../client'

View file

@ -1,4 +1,4 @@
import { sleep } from '@mtcute/core' import { sleep } from '@mtcute/core/utils'
import { TelegramClient } from '../../client' import { TelegramClient } from '../../client'
import { InputPeerLike } from '../../types' import { InputPeerLike } from '../../types'

View file

@ -1,4 +1,5 @@
import { ConditionVariable, ConnectionKind } from '@mtcute/core' import { ConnectionKind } from '@mtcute/core'
import { ConditionVariable } from '@mtcute/core/utils'
import { import {
fileIdToInputFileLocation, fileIdToInputFileLocation,
fileIdToInputWebFileLocation, fileIdToInputWebFileLocation,

View file

@ -2,7 +2,7 @@ import { fromBuffer as fileTypeFromBuffer } from 'file-type'
import type { ReadStream } from 'fs' import type { ReadStream } from 'fs'
import { Readable } from 'stream' import { Readable } from 'stream'
import { AsyncLock, randomLong } from '@mtcute/core' import { AsyncLock, randomLong } from '@mtcute/core/utils'
import { tl } from '@mtcute/tl' import { tl } from '@mtcute/tl'
import { TelegramClient } from '../../client' import { TelegramClient } from '../../client'

View file

@ -1,4 +1,5 @@
import { MaybeArray, randomLong } from '@mtcute/core' import { MaybeArray } from '@mtcute/core'
import { randomLong } from '@mtcute/core/utils'
import { tl } from '@mtcute/tl' import { tl } from '@mtcute/tl'
import { TelegramClient } from '../../client' import { TelegramClient } from '../../client'

View file

@ -1,4 +1,5 @@
import { getMarkedPeerId, randomLong } from '@mtcute/core' import { getMarkedPeerId } from '@mtcute/core'
import { randomLong } from '@mtcute/core/utils'
import { tl } from '@mtcute/tl' import { tl } from '@mtcute/tl'
import { TelegramClient } from '../../client' import { TelegramClient } from '../../client'

View file

@ -1,4 +1,5 @@
import { getMarkedPeerId, randomLong } from '@mtcute/core' import { getMarkedPeerId } from '@mtcute/core'
import { randomLong } from '@mtcute/core/utils'
import { tl } from '@mtcute/tl' import { tl } from '@mtcute/tl'
import { TelegramClient } from '../../client' import { TelegramClient } from '../../client'

View file

@ -1,4 +1,5 @@
import { getMarkedPeerId, randomLong } from '@mtcute/core' import { getMarkedPeerId } from '@mtcute/core'
import { randomLong } from '@mtcute/core/utils'
import { tl } from '@mtcute/tl' import { tl } from '@mtcute/tl'
import { TelegramClient } from '../../client' import { TelegramClient } from '../../client'

View file

@ -1,4 +1,4 @@
import { isPresent } from '@mtcute/core' import { isPresent } from '@mtcute/core/utils'
import { TelegramClient } from '../../client' import { TelegramClient } from '../../client'
import { InputPeerLike, MessageEntity } from '../../types' import { InputPeerLike, MessageEntity } from '../../types'

View file

@ -1,4 +1,4 @@
import { computeNewPasswordHash, computeSrpParams } from '@mtcute/core' import { computeNewPasswordHash, computeSrpParams } from '@mtcute/core/utils'
import { TelegramClient } from '../../client' import { TelegramClient } from '../../client'
import { MtArgumentError } from '../../types' import { MtArgumentError } from '../../types'
@ -20,7 +20,9 @@ export async function changeCloudPassword(
): Promise<void> { ): Promise<void> {
const pwd = await this.call({ _: 'account.getPassword' }) const pwd = await this.call({ _: 'account.getPassword' })
if (!pwd.hasPassword) { throw new MtArgumentError('Cloud password is not enabled') } if (!pwd.hasPassword) {
throw new MtArgumentError('Cloud password is not enabled')
}
const algo = pwd.newAlgo const algo = pwd.newAlgo
assertTypeIs( assertTypeIs(

View file

@ -1,4 +1,4 @@
import { computeNewPasswordHash } from '@mtcute/core' import { computeNewPasswordHash } from '@mtcute/core/utils'
import { TelegramClient } from '../../client' import { TelegramClient } from '../../client'
import { MtArgumentError } from '../../types' import { MtArgumentError } from '../../types'
@ -25,7 +25,9 @@ export async function enableCloudPassword(
): Promise<void> { ): Promise<void> {
const pwd = await this.call({ _: 'account.getPassword' }) const pwd = await this.call({ _: 'account.getPassword' })
if (pwd.hasPassword) { throw new MtArgumentError('Cloud password is already enabled') } if (pwd.hasPassword) {
throw new MtArgumentError('Cloud password is already enabled')
}
const algo = pwd.newAlgo const algo = pwd.newAlgo
assertTypeIs( assertTypeIs(

View file

@ -1,4 +1,4 @@
import { computeSrpParams } from '@mtcute/core' import { computeSrpParams } from '@mtcute/core/utils'
import { TelegramClient } from '../../client' import { TelegramClient } from '../../client'
import { MtArgumentError } from '../../types' import { MtArgumentError } from '../../types'
@ -15,7 +15,9 @@ export async function removeCloudPassword(
): Promise<void> { ): Promise<void> {
const pwd = await this.call({ _: 'account.getPassword' }) const pwd = await this.call({ _: 'account.getPassword' })
if (!pwd.hasPassword) { throw new MtArgumentError('Cloud password is not enabled') } if (!pwd.hasPassword) {
throw new MtArgumentError('Cloud password is not enabled')
}
const oldSrp = await computeSrpParams(this._crypto, pwd, password) const oldSrp = await computeSrpParams(this._crypto, pwd, password)

View file

@ -1,17 +1,16 @@
/* eslint-disable max-depth */ /* eslint-disable max-depth */
import { assertNever } from '@mtcute/core'
import { import {
assertNever,
AsyncLock, AsyncLock,
ConditionVariable,
Deque,
getBarePeerId, getBarePeerId,
getMarkedPeerId, getMarkedPeerId,
Logger, Logger,
markedPeerIdToBare, markedPeerIdToBare,
SortedLinkedList,
toggleChannelIdMark, toggleChannelIdMark,
} from '@mtcute/core' } from '@mtcute/core/utils'
// @copy
import { Deque, SortedLinkedList } from '@mtcute/core'
// @copy
import { ConditionVariable } from '@mtcute/core/src/utils/condition-variable'
import { tl } from '@mtcute/tl' import { tl } from '@mtcute/tl'
import { TelegramClient } from '../client' import { TelegramClient } from '../client'

View file

@ -1,6 +1,11 @@
/* eslint-disable dot-notation */ /* eslint-disable dot-notation */
import { AsyncLock, Deque, getMarkedPeerId, MaybeAsync } from '@mtcute/core' import { getMarkedPeerId, MaybeAsync } from '@mtcute/core'
import { ControllablePromise, createControllablePromise } from '@mtcute/core' import {
AsyncLock,
ControllablePromise,
createControllablePromise,
Deque,
} from '@mtcute/core/utils'
import { tl } from '@mtcute/tl' import { tl } from '@mtcute/tl'
import { TelegramClient } from '../client' import { TelegramClient } from '../client'

View file

@ -1,4 +1,4 @@
import { LongMap } from '@mtcute/core' import { LongMap } from '@mtcute/core/utils'
import { tl } from '@mtcute/tl' import { tl } from '@mtcute/tl'
import { TelegramClient } from '../../client' import { TelegramClient } from '../../client'

View file

@ -2,7 +2,7 @@ import { CallbackQuery, InlineQuery, Message } from '../..'
import { BotChatJoinRequestUpdate } from './bot-chat-join-request' import { BotChatJoinRequestUpdate } from './bot-chat-join-request'
import { BotStoppedUpdate } from './bot-stopped' import { BotStoppedUpdate } from './bot-stopped'
import { ChatJoinRequestUpdate } from './chat-join-request' import { ChatJoinRequestUpdate } from './chat-join-request'
import { ChatMemberUpdate } from './chat-member-update' import { ChatMemberUpdate, ChatMemberUpdateType } from './chat-member-update'
import { ChosenInlineResult } from './chosen-inline-result' import { ChosenInlineResult } from './chosen-inline-result'
import { DeleteMessageUpdate } from './delete-message-update' import { DeleteMessageUpdate } from './delete-message-update'
import { HistoryReadUpdate } from './history-read-update' import { HistoryReadUpdate } from './history-read-update'
@ -17,6 +17,7 @@ export {
BotStoppedUpdate, BotStoppedUpdate,
ChatJoinRequestUpdate, ChatJoinRequestUpdate,
ChatMemberUpdate, ChatMemberUpdate,
ChatMemberUpdateType,
ChosenInlineResult, ChosenInlineResult,
DeleteMessageUpdate, DeleteMessageUpdate,
HistoryReadUpdate, HistoryReadUpdate,

View file

@ -1,10 +1,5 @@
import { import { assertNever, TlBinaryReader, TlBinaryWriter } from '@mtcute/core'
assertNever, import { encodeUrlSafeBase64, parseUrlSafeBase64 } from '@mtcute/core/utils'
encodeUrlSafeBase64,
parseUrlSafeBase64,
TlBinaryReader,
TlBinaryWriter,
} from '@mtcute/core'
import { tl } from '@mtcute/tl' import { tl } from '@mtcute/tl'
/** /**

View file

@ -1,11 +1,13 @@
import { Deque } from '@mtcute/core' import { Deque } from '@mtcute/core/utils'
export class RpsMeter { export class RpsMeter {
_hits: Deque<bigint> _hits: Deque<bigint>
time: bigint time: bigint
constructor(readonly size = 500, time = 5000) { constructor(readonly size = 500, time = 5000) {
if (typeof process === 'undefined' || !process.hrtime.bigint) { throw new Error('RPS meter is not supported on this platform') } if (typeof process === 'undefined' || !process.hrtime.bigint) {
throw new Error('RPS meter is not supported on this platform')
}
this._hits = new Deque<bigint>(size) this._hits = new Deque<bigint>(size)
this.time = BigInt(time) * BigInt(1e6) this.time = BigInt(time) * BigInt(1e6)

View file

@ -2,7 +2,7 @@ export * from './base-client'
export * from './network' export * from './network'
export * from './storage' export * from './storage'
export * from './types' export * from './types'
export * from './utils' export * from './utils/peer-utils'
/** @hidden */ /** @hidden */
export * from '@mtcute/tl' export * from '@mtcute/tl'

View file

@ -2,6 +2,7 @@ import { ForgeCryptoProvider } from './forge-crypto'
import { NodeCryptoProvider } from './node-crypto' import { NodeCryptoProvider } from './node-crypto'
export * from './abstract' export * from './abstract'
export * from './keys'
export * from './password' export * from './password'
export { ForgeCryptoProvider, NodeCryptoProvider } export { ForgeCryptoProvider, NodeCryptoProvider }

View file

@ -2,7 +2,7 @@ import bigInt from 'big-integer'
import { expect } from 'chai' import { expect } from 'chai'
import { describe, it } from 'mocha' import { describe, it } from 'mocha'
import { bigIntToBuffer, bufferToBigInt } from '../src' import { bigIntToBuffer, bufferToBigInt } from '../utils'
// since bigIntToBuffer is a tiny wrapper over writeBigInt, no need to test it individually // since bigIntToBuffer is a tiny wrapper over writeBigInt, no need to test it individually
describe('bigIntToBuffer', () => { describe('bigIntToBuffer', () => {

View file

@ -5,7 +5,7 @@ import {
ForgeCryptoProvider, ForgeCryptoProvider,
ICryptoProvider, ICryptoProvider,
NodeCryptoProvider, NodeCryptoProvider,
} from '../src' } from '../utils'
export function testCryptoProvider(c: ICryptoProvider): void { export function testCryptoProvider(c: ICryptoProvider): void {
it('should calculate sha1', async () => { it('should calculate sha1', async () => {

View file

@ -1,4 +1,5 @@
import { BaseTelegramClient, NodeCryptoProvider, TcpTransport } from '../../src' import { BaseTelegramClient, TcpTransport } from '../../src'
import { NodeCryptoProvider } from '../../utils'
export function createTestTelegramClient() { export function createTestTelegramClient() {
const tg = new BaseTelegramClient({ const tg = new BaseTelegramClient({

View file

@ -1,8 +1,7 @@
import { expect } from 'chai' import { expect } from 'chai'
import { describe, it } from 'mocha' import { describe, it } from 'mocha'
import { NodeCryptoProvider } from '../src' import { NodeCryptoProvider, parsePublicKey } from '../utils'
import { parsePublicKey } from '../src/utils/crypto/keys'
const crypto = new NodeCryptoProvider() const crypto = new NodeCryptoProvider()

View file

@ -1,7 +1,7 @@
import { expect } from 'chai' import { expect } from 'chai'
import { describe, it } from 'mocha' import { describe, it } from 'mocha'
import { LruMap } from '../src' import { LruMap } from '../utils'
describe('LruMap', () => { describe('LruMap', () => {
it('Map backend', () => { it('Map backend', () => {

View file

@ -2,7 +2,7 @@ import { expect } from 'chai'
import Long from 'long' import Long from 'long'
import { describe, it } from 'mocha' import { describe, it } from 'mocha'
import { LruSet } from '../src' import { LruSet } from '../utils'
describe('LruSet', () => { describe('LruSet', () => {
describe('for strings', () => { describe('for strings', () => {

View file

@ -5,7 +5,8 @@
}, },
"include": [ "include": [
"./src", "./src",
"./tests" "./tests",
"./utils.ts",
], ],
"typedocOptions": { "typedocOptions": {
"name": "@mtcute/core", "name": "@mtcute/core",

4
packages/core/utils.ts Normal file
View file

@ -0,0 +1,4 @@
// this file only exists as a hint to IDEs that we can use @mtcute/core/utils instead of @mtcute/core/src/utils.
// it is not present in the built package, just a DX improvement
export * from './src/utils'

View file

@ -1,4 +1,4 @@
import { IEncryptionScheme, NodeCryptoProvider } from '@mtcute/core' import { IEncryptionScheme, NodeCryptoProvider } from '@mtcute/core/utils'
// eslint-disable-next-line camelcase // eslint-disable-next-line camelcase
import { ige256_decrypt, ige256_encrypt } from './native' import { ige256_decrypt, ige256_encrypt } from './native'

View file

@ -6,6 +6,7 @@ import {
CallbackQuery, CallbackQuery,
Chat, Chat,
ChatMemberUpdate, ChatMemberUpdate,
ChatMemberUpdateType,
ChatType, ChatType,
ChosenInlineResult, ChosenInlineResult,
Contact, Contact,
@ -36,7 +37,6 @@ import {
Voice, Voice,
WebPage, WebPage,
} from '@mtcute/client' } from '@mtcute/client'
import { ChatMemberUpdateType } from '@mtcute/client/src/types/updates/chat-member-update'
import { MaybeArray } from '@mtcute/core' import { MaybeArray } from '@mtcute/core'
import { UpdateState } from './state' import { UpdateState } from './state'

View file

@ -1,5 +1,5 @@
import { MtArgumentError, MtClientError } from '@mtcute/client' import { MtArgumentError, MtClientError } from '@mtcute/client'
import { sleep } from '@mtcute/core' import { sleep } from '@mtcute/core/utils'
import { IStateStorage } from './storage' import { IStateStorage } from './storage'
@ -101,7 +101,9 @@ export class UpdateState<State, SceneName extends string = string> {
return this._cached return this._cached
} }
let res = await this._localStorage.getState(this._localKey) as State | null let res = (await this._localStorage.getState(
this._localKey,
)) as State | null
if (!res && fallback) res = fallback if (!res && fallback) res = fallback
this._cached = res this._cached = res

View file

@ -1,4 +1,4 @@
import { parseUrlSafeBase64 } from '@mtcute/core' import { parseUrlSafeBase64 } from '@mtcute/core/utils'
import { TlBinaryReader } from '@mtcute/tl-runtime' import { TlBinaryReader } from '@mtcute/tl-runtime'
import { tdFileId as td } from './types' import { tdFileId as td } from './types'

View file

@ -1,4 +1,5 @@
import { assertNever, encodeUrlSafeBase64 } from '@mtcute/core' import { assertNever } from '@mtcute/core'
import { encodeUrlSafeBase64 } from '@mtcute/core/utils'
import { TlBinaryWriter } from '@mtcute/tl-runtime' import { TlBinaryWriter } from '@mtcute/tl-runtime'
import { tdFileId as td } from './types' import { tdFileId as td } from './types'

View file

@ -1,4 +1,5 @@
import { assertNever, encodeUrlSafeBase64 } from '@mtcute/core' import { assertNever } from '@mtcute/core'
import { encodeUrlSafeBase64 } from '@mtcute/core/utils'
import { TlBinaryWriter } from '@mtcute/tl-runtime' import { TlBinaryWriter } from '@mtcute/tl-runtime'
import { tdFileId as td } from './types' import { tdFileId as td } from './types'

View file

@ -1,13 +1,12 @@
import bigInt, { BigInteger } from 'big-integer' import bigInt, { BigInteger } from 'big-integer'
import { IPacketCodec, WrappedCodec } from '@mtcute/core'
import { import {
bigIntToBuffer, bigIntToBuffer,
bufferToBigInt, bufferToBigInt,
ICryptoProvider, ICryptoProvider,
IPacketCodec,
randomBytes, randomBytes,
WrappedCodec, } from '@mtcute/core/utils'
} from '@mtcute/core'
const MAX_TLS_PACKET_LENGTH = 2878 const MAX_TLS_PACKET_LENGTH = 2878
const TLS_FIRST_PREFIX = Buffer.from('140303000101', 'hex') const TLS_FIRST_PREFIX = Buffer.from('140303000101', 'hex')

View file

@ -8,10 +8,10 @@ import {
IPacketCodec, IPacketCodec,
ObfuscatedPacketCodec, ObfuscatedPacketCodec,
PaddedIntermediatePacketCodec, PaddedIntermediatePacketCodec,
parseUrlSafeBase64,
tl, tl,
TransportState, TransportState,
} from '@mtcute/core' } from '@mtcute/core'
import { parseUrlSafeBase64 } from '@mtcute/core/utils'
import { FakeTlsPacketCodec, generateFakeTlsHeader } from './fake-tls' import { FakeTlsPacketCodec, generateFakeTlsHeader } from './fake-tls'

View file

@ -4,11 +4,6 @@ import sqlite3, { Options } from 'better-sqlite3'
import { import {
ITelegramStorage, ITelegramStorage,
Logger,
longFromFastString,
longToFastString,
LruMap,
throttle,
tl, tl,
TlBinaryReader, TlBinaryReader,
TlBinaryWriter, TlBinaryWriter,
@ -16,6 +11,13 @@ import {
TlWriterMap, TlWriterMap,
toggleChannelIdMark, toggleChannelIdMark,
} from '@mtcute/core' } from '@mtcute/core'
import {
Logger,
longFromFastString,
longToFastString,
LruMap,
throttle,
} from '@mtcute/core/utils'
import { IStateStorage } from '@mtcute/dispatcher' import { IStateStorage } from '@mtcute/dispatcher'
// todo: add testMode to "self" // todo: add testMode to "self"

View file

@ -9,7 +9,7 @@ import { readFile, writeFile } from 'fs/promises'
import { join } from 'path' import { join } from 'path'
import * as readline from 'readline' import * as readline from 'readline'
import { hasPresentKey, isPresent } from '@mtcute/core' import { hasPresentKey, isPresent } from '@mtcute/core/utils'
import { import {
generateTlSchemasDifference, generateTlSchemasDifference,
mergeTlEntries, mergeTlEntries,

View file

@ -3,8 +3,7 @@ import { writeFile } from 'fs/promises'
import { join } from 'path' import { join } from 'path'
import readline from 'readline' import readline from 'readline'
import { NodeCryptoProvider } from '@mtcute/core/src/utils/crypto' import { NodeCryptoProvider, parsePublicKey } from '@mtcute/core/utils'
import { parsePublicKey } from '@mtcute/core/src/utils/crypto/keys'
import { TlPublicKey } from '../binary/rsa-keys' import { TlPublicKey } from '../binary/rsa-keys'
import { ESM_PRELUDE } from './constants' import { ESM_PRELUDE } from './constants'