0.2.4 • Published 10 months ago

tuna-for-node v0.2.4

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

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.

ParamTypeDescription
command'entry' | 'exit'
configDirstring | URL
ipChangeCBfunction
validatePortsbooleanif it waits for the proxy port to be open
restartbooleanif it should restart tuna if already running

waitConnected ⇒ Promise<string>

Awaits tuna client being connected.

Returns: Promise<string> - the current ip

ParamTypeDescription
validatePortsbooleanif it waits for the proxy port to be open
startbooleanif 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:

  1. 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
  2. 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
0.2.3

10 months ago

0.2.2

10 months ago

0.2.4

10 months ago

0.2.1

1 year ago

0.1.1

1 year ago

0.1.0

1 year ago