gifsockets-middleware

Set of HTTP middlewares for gifsockets

npm install gifsockets-middleware
3 downloads in the last week
6 downloads in the last month

gifsockets-middleware Build status

Set of HTTP middlewares for gifsockets

This is part of the gifsockets project; it acts as a plug and play middleware that is used in the demo but can be re-used anywhere.

Getting Started

Install the module with: npm install gifsockets-middleware

var GifsocketMiddleware = require('gifsockets-middleware');
var middlewares = GifsocketMiddleware({width: 200, height: 200});
var express = require('express');
var app = express();

// middlewares returns an object containing 4 middlewares
// `openImage` writes the beginning of a .gif and leaves `res` open
app.get('/image.gif', middlewares.openImage);

// `writePixelsToImages` writes a new frame to all open `res` from openImage
var bodyParser = express.bodyParser();
app.post('/image/pixels', bodyParser, middlewares.writePixelsToImages);

// `writeTextToImages` accepts a string of text and writes a new frame
// This requires running `phantomjs-pixel-server`
app.post('/image/text', bodyParser, middlewares.writeTextToImages);

// `closeOpenImages` closes all active images opened by `openImage`
app.post('/image/close', middlewares.closeOpenImages);

// If you want to load a specific middleware, you can do so
var openImageMiddleware = require('gifsockets-middleware/lib/middlewares/open-image');
var openImage = openImageMiddleware(gifsocket);
// `openImage` has the same behavior as that returned from `GifsocketMiddleware`

Documentation

gifsockets-middleware returns GifsocketMiddleware as its module.exports

GifsocketMiddleware(options)

Function that generates an object of middlewares for gifsockets

  • options Object
    • width Number Width of the output GIF/gifsocket
    • height Number Height of the output GIF/gifsocket
  • Returns an object containing
    • openImage middleware
    • writePixelsToImages middleware
    • writeTextToImages middleware
    • closeOpenImages middleware

openImage middleware

Middleware that will maintain an open connection such that it can write additional GIF frames.

Function signature is function (req, res, next) {}

This does not expect any information on req/res and will not callback to next.

writePixelsToImages middleware

Middleware that will write a new GIF frame with the provided pixels.

Function signature is function (req, res, next) {}

If req.rgbPixels exists, we will draw a GIF frame with the pixel values.

req.rgbPixels is expected to be an stringified array of rgb pixels; [0, 1, 2, 3, 4, 5] is 2 pixels with r: 0, g: 1, b: 2 and r: 3, g: 4, b: 5

If req.rgbPixels is not found, we look for req.rgbaPixels or req.body. If either of these is found, we will draw a GIF frame with the pixel values.

req.rgbaPixels/req.body is expected to be an stringified array of rgba pixels; [0, 1, 2, 3, 4, 5, 6, 7] is 2 pixels with r: 0, g: 1, b: 2, a: 3 and r: 4, g: 5, b: 6, a: 7

This will reply with a 204 when it is complete.

writeTextToImages middleware

This depends on phantomjs-pixel-server running as another process.

Please read the documentation: https://github.com/twolfson/phantomjs-pixel-server

Middleware that will create and write a new GIF frame with the provided text parameters.

Function signature is function (req, res, next) {}

If req.body is provided, we will parse it via querystring.parse and use the following parameters:

  • text String - Text to write to the canvas
  • background String - CSS color background for the canvas (default "#000000")
  • foreground String - CSS color for text on the canvas (default "#BADA55")
  • font-size Number - Size of the font to draw (default: 30)
  • font-family String - Font family to use on the canvas (default: "Impact")

This will reply with a 204 when it is complete.

closeOpenImages middleware

Middleware that iterates over open connections, writes GIF footer, and closes connection.

Function signature is function (req, res, next) {}

This will reply with a 204 when it is complete.

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint via grunt and test via npm test.

Donating

Support this project and others by twolfson via gittip.

Support via Gittip

Unlicense

As of Nov 20 2013, Todd Wolfson has released this repository and its contents to the public domain.

It has been released under the UNLICENSE.

npm loves you