adnl
TypeScript icon, indicating that this package has built-in type declarations

1.0.3 • Public • Published

adnl

npm GitHub top language

ADNL TypeScript implementation.

How to install

npm i adnl

Simple usage

import {
    ADNLClientTCP, // Not available for browser context
    ADNLClientWS
} from 'adnl'

const ADNL_HOST = '127.0.0.1'
const ADNL_PORT = 1337
const ADNL_PUB_KEY = 'hex/base64/buffer'
const URL = `tcp://${ADNL_HOST}:${ADNL_PORT}`
const URL_PROXY = `wss://proxy.example.com/?dest_host=${ADNL_HOST}:${ADNL_PORT}`

const TL_GETTIME = '7af98bb435263e6c95d6fecb497dfd0aa5f031e7d412986b5ce720496db512052e8f2d100cdf068c7904345aad16000000000000'
const TL_PARSE_GETTIME = (data: Buffer) => {
    const unix = data.slice(data.byteLength - 7, data.byteLength - 3).readUint32LE(0)

    return new Date(unix * 1000).toString()
}

const clientTCP = new ADNLClientTCP(URL, ADNL_PUB_KEY)
    .on('connect', () => console.log('on connect'))
    .on('close', () => console.log('on close'))
    .on('data', (data: Buffer) => console.log('on data:', TL_PARSE_GETTIME(data)))
    .on('error', (error: Error) => console.log('on error:', error))
    .on('ready', () => {
        console.log('on ready')

        let counter = 0
        let interval = setInterval(() => {
            client.write(Buffer.from(TL_GETTIME, 'hex'))

            if (++counter === 5) {
                clearInterval(interval)
                client.end()
            }
        }, 3000)
    })

await clientTCP.connect()

// OR

const clientWSProxy = new ADNLClientWS(URL_PROXY, ADNL_PUB_KEY)
    .on('connect', () => console.log('on connect'))
    .on('close', () => console.log('on close'))
    .on('data', (data: Buffer) => console.log('on data:', TL_PARSE_GETTIME(data)))
    .on('error', (error: Error) => console.log('on error:', error))
    .on('ready', () => {
        console.log('on ready')

        let counter = 0
        let interval = setInterval(() => {
            client.write(Buffer.from(TL_GETTIME, 'hex'))

            if (++counter === 5) {
                clearInterval(interval)
                client.end()
            }
        }, 3000)
    })

await clientWSProxy.connect()

Donations

You can support library author by sending any amount of TON to address below

EQDwxdFSZfY-wI0Udq-YQjVgTYJmyACIhNczwQuAWhf0dkUH

License

MIT License

Readme

Keywords

Package Sidebar

Install

npm i adnl

Weekly Downloads

83,403

Version

1.0.3

License

MIT

Unpacked Size

40.7 kB

Total Files

42

Last publish

Collaborators

  • tjifyodor