tuna-for-node
v0.2.4
Published
Nodejs wrapper around NKN Tuna
Downloads
9
Readme
Nodejs wrapper around NKN Tuna
Allows using NKN tuna directly from a nodejs script.
Example:
import { startTuna, waitConnected } from 'tuna-for-node'
//starts the tuna process
await startTuna('entry', new URL('./config/', import.meta.url))
//awaits tuna is connected
const ip = await waitConnected()
console.log('IP:', ip)
Installation
requires node 20 or greater
npm
npm install tuna-for-node
pnpm
pnpm add tuna-for-node
What it does
It downloads the correct tuna release binary (based on the platform) from Tuna releases. And uses a simple script to start/stop the process (using spawn
).
API
startTuna ⇒ Promise<void>
Starts the tuna client.
| Param | Type | Description | | ------------- | --------------------------------------------------------------- | -------------------------------------------- | | command | 'entry' | 'exit' | | | configDir | string | URL | | | ipChangeCB | function | | | validatePorts | boolean | if it waits for the proxy port to be open | | restart | boolean | if it should restart tuna if already running |
waitConnected ⇒ Promise<string>
Awaits tuna client being connected.
Returns: Promise<string> - the current ip
| Param | Type | Description | | ------------- | -------------------- | -------------------------------------------------------------------------------------------- | | validatePorts | boolean | if it waits for the proxy port to be open | | start | boolean | if tuna should be started if not running (throws error if false and tuna is already running) |
stopTuna ⇒ Promise<void>
Stops the tuna client.
startTunaProxy ⇒ Promise<TunaHandler>
Starts a tuna proxy (http or socks5), this mode allows starting multiple proxies and there is no interference between each process. Port is assigned randomly by OS, and one can retrieve it from the return value listenPort
Known issues:
- This package relies on reading the log output stream from tuna to determine if connected and to what ip. Due to the limitation of the output, there is no way to determine which serivce disconnects and connects belong to. So in the configuration you must only have 1 service defined, it will throw error otherwise
- Tuna does not fail or report an error if the defined service binding port is already in use, so is up to you to make sure is not.
Improvments for the future
- Ability to start multiple (currently the process is global, so only one at the time can be started). One of the limitations is config directory, currently at best I could do a global map per config directory. However looking at more general solution options
- Handle all errors from the tuna output
- Better docs for startTunaProxy
- implement similar solution for startTuna