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

webstream

v0.0.1

Published

Stream api for node.js websockets

Downloads

5

Readme

webstream - Stream api for node.js websockets

Simply provides a node.js ReadableStream and WritableStream API for websockets.

npm install webstream

Examples

Piping node.js REPL through websockets

// webrepl.js
var webstream = require('webstream');
var repl = require('repl');

webstream.createServer(function(stream) {
  repl.start('> ', stream);
}).listen(5000);

Start the server:

$ node samples/webrepl.js

This will start the node.js REPL and bind it into a websocket. To connect to this server, use the wsclient utility:

$ wsclient http://localhost:5000
> var x = { damn: "i am in the node.js repl" }
undefined
> x
{ damn: 'i am in the node.js repl' }

Piping /bin/bash through websockets

Here's how to bind a webstream to a spawned process:

// webash.js
var spawn = require('child_process').spawn;
var http = require('http');
var webstream = require('webstream');

var server = http.createServer();
server.listen(5000);

webstream.bind(server, function(stream) {
  console.log('Client connected.');

  var proc = spawn('/bin/bash', [ '-i' ]);

  // stdout & stderr
  proc.stdout.on('data', function(data) { stream.write(data); });
  proc.stderr.on('data', function(data) { stream.write(data); });
  
  proc.on('exit', function(status) {
    stream.write('Process exited with status ' + status + '\n');
    stream.end();
  });

  stream.on('data', function(data) {
    proc.stdin.write(data);
  });

  stream.on('close', function() {
    console.log('Client disconnected.');
  });
});

Start the server:

$ node samples/webash.js

Connect via wsclient:

$ bin/wsclient http://localhost:5000
bash: no job control in this shell
bash-3.2$ ls -l
ls -l
total 32
-rw-r--r--  1 eladb  staff  1058 Dec 21 19:07 LICENSE
-rw-r--r--  1 eladb  staff  1764 Dec 21 19:31 README.md
drwxr-xr-x  3 eladb  staff   102 Dec 21 18:33 bin
drwxr-xr-x  4 eladb  staff   136 Dec 21 17:25 lib
-rw-r--r--  1 eladb  staff    45 Dec 21 18:00 main.js
drwxr-xr-x  6 eladb  staff   204 Dec 21 19:12 node_modules
-rw-r--r--  1 eladb  staff   507 Dec 21 19:28 package.json
drwxr-xr-x  4 eladb  staff   136 Dec 21 17:24 samples
drwxr-xr-x  3 eladb  staff   102 Dec 21 19:08 test
bash-3.2$ exit
exit
logout
Process exited with status 0

API

webstream.createStream(connection)

Creates a WebStream object bounds to a WebSocketConnection. The object adheres to the ReadableStream and WritableStream interfaces.

webstream.bind(server, [callback])

Binds a webstream into an HTTP server.

  • Callback is an optional function(stream) which is called for every new connection.
  • Returns an EventEmitter with a 'request' event which is called for every incoming connection. Callbacks are passed a single WebStream argument.

webstream.createServer(callback)

Creates an HTTP server and binds a webstream to it. Returns the HTTP server object.

webstream.connect(url, [callback])

Connects to a webstream server.

  • Callback function(err, stream) is optional.
  • Returns an EventEmitter with the following API:
    • 'connect' event - Called when a connection has been established successfuly. Callbacks are passed a single argument which is a WebStream object.
    • 'error' event - Called if the connection failed. Argument is error.

License

MIT