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": {
"test": "mocha -r ts-node/register \"tests/**/*.spec.ts\"",
"docs": "typedoc",
"build": "tsc"
"build": "tsc",
"gen-client": "node ./scripts/generate-client.js"
},
"dependencies": {
"@types/node": "18.16.0",

View file

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

View file

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

View file

@ -2,9 +2,15 @@
import { Readable } from 'stream'
// @copy
import { AsyncLock, MaybeArray, MaybeAsync } from '@mtcute/core'
import { MaybeArray, MaybeAsync } from '@mtcute/core'
// @copy
import { Logger } from '@mtcute/core/src/utils/logger'
import {
AsyncLock,
ConditionVariable,
Deque,
Logger,
SortedLinkedList,
} from '@mtcute/core/utils'
// @copy
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 { 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 { 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 { 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 {
fileIdToInputFileLocation,
fileIdToInputWebFileLocation,

View file

@ -2,7 +2,7 @@ import { fromBuffer as fileTypeFromBuffer } from 'file-type'
import type { ReadStream } from 'fs'
import { Readable } from 'stream'
import { AsyncLock, randomLong } from '@mtcute/core'
import { AsyncLock, randomLong } from '@mtcute/core/utils'
import { tl } from '@mtcute/tl'
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 { 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 { 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 { 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 { 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 { 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 { MtArgumentError } from '../../types'
@ -20,7 +20,9 @@ export async function changeCloudPassword(
): Promise<void> {
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
assertTypeIs(

View file

@ -1,4 +1,4 @@
import { computeNewPasswordHash } from '@mtcute/core'
import { computeNewPasswordHash } from '@mtcute/core/utils'
import { TelegramClient } from '../../client'
import { MtArgumentError } from '../../types'
@ -25,7 +25,9 @@ export async function enableCloudPassword(
): Promise<void> {
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
assertTypeIs(

View file

@ -1,4 +1,4 @@
import { computeSrpParams } from '@mtcute/core'
import { computeSrpParams } from '@mtcute/core/utils'
import { TelegramClient } from '../../client'
import { MtArgumentError } from '../../types'
@ -15,7 +15,9 @@ export async function removeCloudPassword(
): Promise<void> {
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)

View file

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

View file

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

View file

@ -2,7 +2,7 @@ import { CallbackQuery, InlineQuery, Message } from '../..'
import { BotChatJoinRequestUpdate } from './bot-chat-join-request'
import { BotStoppedUpdate } from './bot-stopped'
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 { DeleteMessageUpdate } from './delete-message-update'
import { HistoryReadUpdate } from './history-read-update'
@ -17,6 +17,7 @@ export {
BotStoppedUpdate,
ChatJoinRequestUpdate,
ChatMemberUpdate,
ChatMemberUpdateType,
ChosenInlineResult,
DeleteMessageUpdate,
HistoryReadUpdate,

View file

@ -1,10 +1,5 @@
import {
assertNever,
encodeUrlSafeBase64,
parseUrlSafeBase64,
TlBinaryReader,
TlBinaryWriter,
} from '@mtcute/core'
import { assertNever, TlBinaryReader, TlBinaryWriter } from '@mtcute/core'
import { encodeUrlSafeBase64, parseUrlSafeBase64 } from '@mtcute/core/utils'
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 {
_hits: Deque<bigint>
time: bigint
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.time = BigInt(time) * BigInt(1e6)

View file

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

View file

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

View file

@ -2,7 +2,7 @@ import bigInt from 'big-integer'
import { expect } from 'chai'
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
describe('bigIntToBuffer', () => {

View file

@ -5,7 +5,7 @@ import {
ForgeCryptoProvider,
ICryptoProvider,
NodeCryptoProvider,
} from '../src'
} from '../utils'
export function testCryptoProvider(c: ICryptoProvider): void {
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() {
const tg = new BaseTelegramClient({

View file

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

View file

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

View file

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

View file

@ -5,7 +5,8 @@
},
"include": [
"./src",
"./tests"
"./tests",
"./utils.ts",
],
"typedocOptions": {
"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
import { ige256_decrypt, ige256_encrypt } from './native'

View file

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

View file

@ -1,5 +1,5 @@
import { MtArgumentError, MtClientError } from '@mtcute/client'
import { sleep } from '@mtcute/core'
import { sleep } from '@mtcute/core/utils'
import { IStateStorage } from './storage'
@ -101,7 +101,9 @@ export class UpdateState<State, SceneName extends string = string> {
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
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 { 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 { 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 { tdFileId as td } from './types'

View file

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

View file

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

View file

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

View file

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

View file

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