2021-11-23 00:03:59 +03:00
|
|
|
// Downloads latest MTProto .tl schema
|
|
|
|
|
2024-08-13 04:53:07 +03:00
|
|
|
import { writeFile } from 'node:fs/promises'
|
2023-06-05 03:30:48 +03:00
|
|
|
|
2024-12-02 08:31:24 +03:00
|
|
|
import { ffetchBase as ffetch } from '@fuman/fetch'
|
2024-12-03 09:55:37 +03:00
|
|
|
import { parseTlToEntries } from '@mtcute/tl-utils'
|
|
|
|
import * as cheerio from 'cheerio'
|
2023-06-05 03:30:48 +03:00
|
|
|
|
2023-10-16 19:23:53 +03:00
|
|
|
import { CORE_DOMAIN, MTP_SCHEMA_JSON_FILE } from './constants.js'
|
2021-11-23 00:03:59 +03:00
|
|
|
|
|
|
|
async function fetchMtprotoSchema(): Promise<string> {
|
2024-11-19 18:35:02 +03:00
|
|
|
const html = await ffetch(`${CORE_DOMAIN}/schema/mtproto`).text()
|
2021-11-23 00:03:59 +03:00
|
|
|
const $ = cheerio.load(html)
|
2023-06-05 03:30:48 +03:00
|
|
|
|
2021-11-23 00:03:59 +03:00
|
|
|
// cheerio doesn't always unescape them
|
2023-09-24 01:32:22 +03:00
|
|
|
return $('#dev_page_content pre code').text().replace(/</g, '<').replace(/>/g, '>')
|
2021-11-23 00:03:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
async function main() {
|
|
|
|
console.log('Downloading MTProto schema...')
|
|
|
|
const schema = await fetchMtprotoSchema()
|
|
|
|
|
|
|
|
console.log('Parsing...')
|
2023-06-25 03:09:04 +03:00
|
|
|
let entries = parseTlToEntries(schema, { prefix: 'mt_' })
|
2021-11-23 00:03:59 +03:00
|
|
|
|
|
|
|
// remove manually parsed types
|
|
|
|
entries = entries.filter(
|
2024-08-13 04:53:07 +03:00
|
|
|
it => !['mt_msg_container', 'mt_message', 'mt_msg_copy', 'mt_gzip_packed', 'mt_rpc_result'].includes(it.name),
|
2021-11-23 00:03:59 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// mtproto is handled internally, for simplicity we make them all classes
|
|
|
|
entries.forEach((entry) => {
|
|
|
|
entry.kind = 'class'
|
|
|
|
})
|
|
|
|
|
|
|
|
console.log('Writing to file...')
|
|
|
|
await writeFile(MTP_SCHEMA_JSON_FILE, JSON.stringify(entries))
|
|
|
|
|
|
|
|
console.log('Done!')
|
|
|
|
}
|
|
|
|
|
|
|
|
main().catch(console.error)
|