You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Erik Hollensbe 8fbde8714c Ditch the lock since I overengineered it 2 years ago
testdata jsonbuffer is a package to implement buffer envelopes as JSON. 2 years ago
LICENSE jsonbuffer is a package to implement buffer envelopes as JSON. 2 years ago
README.md jsonbuffer is a package to implement buffer envelopes as JSON. 2 years ago
go.mod jsonbuffer is a package to implement buffer envelopes as JSON. 2 years ago
go.sum jsonbuffer is a package to implement buffer envelopes as JSON. 2 years ago
jsonbuffer.go Ditch the lock since I overengineered it 2 years ago
jsonbuffer_test.go jsonbuffer is a package to implement buffer envelopes as JSON. 2 years ago
task.yml jsonbuffer is a package to implement buffer envelopes as JSON. 2 years ago
tinyci.yml jsonbuffer is a package to implement buffer envelopes as JSON. 2 years ago

README.md

jsonbuffer: an implementation of byte buffers with JSON envelopes

This is a pattern frequently used when presenting a terminal to a user through some sort of HTTP interface, such as a websocket. The JSON envelope contains a type as well as the message sent, allowing you to act on interrupting/erroneous behavior or other signals the system may want to send you.

The value in this system:

  • Presentable, hackable buffer measurement and management
  • Easy to use interface for delivering and receiving messages over any stream
  • (eventually) will come with javascript client -- see below for examples for now

Usage & Installation

Typically, one would embed this library into their buffering system and then talk to it either through the go or javascript client code.

go modules is likely the best way to install this library. To get it or update it, simply run this in your project directory:

go get -u github.com/erikh/jsonbuffer@latest

Golang

To use check out our godoc or try this example:

package main

import (
	"io"
	"os"

	"github.com/erikh/jsonbuffer"
)

func main() {
	f, err := os.Open("/proc/cpuinfo")
	if err != nil {
		panic(err) // not on linux? use another file
	}

	// NewWrapper requires an io.ReadWriter, which io.Pipe doesn't provide.
	// this operation would normally occur over a TCP socket.
	pr, pw, err := os.Pipe()
	if err != nil {
		panic(err) // evaluate your system health
	}

	go func() {
		io.Copy(jsonbuffer.NewWrapper(pw), f)
		f.Close()
		pw.Close()
	}()

	if _, err := io.Copy(os.Stdout, jsonbuffer.NewWrapper(pr)); err != nil {
		panic(err)
	}
}

Javascript

The client for javascript is so simple, it's hard to find the urge make one; here's some code you can use with an appropriate websocket setup, no account for security of implementation here:

var ws = new WebSocket('https://example.org/terminal');

ws.onmessage = event => {
  var parsed = JSON.parse(event.data);
  if (parsed.type === 'error') {
    alert(parsed.payload);
    ws.close();
  }
  document.write(parsed.payload);
};

ws.onerror = event => {
  alert(event);
};

ws.onclose = event => {
  if (!event.wasClean && event.reason !== '') {
    alert(event.reason);
  }
};

Author

Erik Hollensbe github+jsonbuffer@hollensbe.org

License

Mozilla Public License 2.0