npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ws-stream-tunnel

v0.4.29

Published

use websocket as a data tunnel

Downloads

86

Readme

ws-stream-tunnel

For node.js

use node.js stream over websocket.

Install

npm i ws-stream-tunnel --save

Usage

Create a server,then use client to connect it. The client will negotiate with server about the tunnel type.(Both sides can create streams in the tunnel) Then the tunnel will be used to transfer stream.

websocket connection
|_one of the tunnel types
   |_stream(s)
      |_data
const {server,client}=require('ws-stream-tunnel');

//server
const tServer=new server({
	port:80,
	perMessageDeflate:false,
});
tServer.on('tunnel_open',t=>{
	console.log('server','new tunnel');

	t.on('stream_open',stream=>{
		console.log('server','stream opened');
		
		//do sth with the stream
		stream.on('data',chunk=>{
			console.log('server','recevied data',chunk)
		});

	}).once('close',()=>{
		console.log('server','tunnel closed')
	})
});


//client
const tClient=new client({
	addr:'ws://127.0.0.1:80',
	mode:'subStream',
});
tClient.on('tunnel_open',tunnel=>{
	console.log('client','tunnel opened');

	tunnel.on('stream_open',stream=>{
		console.log('client','stream opened')
		
		//do sth with the stream
		stream.write('poi');

	}).on('stream_close',stream=>{
		console.log('client','stream closed')
	}).on('error',e=>{
		console.error('client:error',e)
	}).once('close',()=>{
		console.log('client','tunnel closed')
	});

	tunnel.createStream();//create a sub stream
});

Tunnel types

  • subStream : mix multi-streams in one tunnel
  • stream : extends from subStream,but only allows one stream in the tunnel

server

Class : tunnelServer(options)

The server class extends from ws.server,options is the same with here.

Two new event

  • 'tunnel_open' : (new tunnel)
  • 'tunnel_close' : (the closed tunnel)

One more property

  • tunnelHelper : see tunnelHelper below

client

The server class extends from events.

Class : tunnelClient(options)

options

  • mode : tunnel type
  • addr : target ws address
  • ws : options object for ws connection,see here
  • idleTimeout : millisecond before closing the connection,0 presents always keep the connection,defaults to 0
  • keepBrokenTunnel : millisecond before closing the broken tunnel. The tunnel will immediately close if this option is not set

events

  • 'tunnel_open':(tunnel)
  • 'tunnel_close':(tunnel)

properties

  • connectionManager : connectionManager instance
  • tunnelHelper : tunnelHelper instance
  • closed : boolean

getters

  • tunnelID
  • tunnelCreated : boolean
  • tunnel : tunnel of the mode options

methods

close()

close the client

closeTunnel(reason)

close the tunnel,this method will destroy all alive streams,the reason will be emit as the error event to each stream.

requestTunnel(options)

request a tunnell for the connection

options:

  • mode : see Tunnel types on the top
  • keepBrokenTunnel : see tunnelClient options

If tunnelID exists, the client will attempt to use the tunnel ID to resume the broken tunnel transfer.

Class: tunnelHelper

this class is used in client and server to manage tunnels

properties

  • tunnels : a Map stores all tunnels (tunnelID=>tunnel)

extra

Tunnel types extends from tunnelManager class in lib/tunnel.js,tunnels are defined in tunnels directory.See README.md in each dirs.