@cio/transformer
Easily use a Transform pipeline to handle communication.
Install
npm install @cio/transformer --save
Usage
Uses specified transforms in a pipeline from socket input back to socket output.
Accepts a single transform or an array of transforms.
Accepts the actual transform instance, a function to build the transform, or a string it can provide to require()
to get a builder function.
Note: A server should (very likely) use builder functions to make a new transform for each connection.
What to do:
- add the listener to the
cio
instance - provide the
transformer
option property to cio.client()
or cio.server()
- specify one of the allowed types as its value, or, an array of values
- for options to builder functions put the above info in the
transform
sub-property and builder options as sibling keys to it.
var buildCio = require('cio')
, cio = buildCio();
cio.onClient('@cio/transformer');
cio.onServerClient('@cio/transformer');
var fn = require('@cio/transformer');
cio.onClient(fn);
cio.onServerClient(fn);
var transformModule = 'some-module'
, buildTransform = require(transformModule)
, someTransform = buildTransform()
, optionsAsInstance = { transformer: someTransform }
, optionsAsBuilder = { transformer: buildTransform }
, optionsAsString = { transformer: transformModule }
, optionsWithMultiple = {
transformer: [
someTransform
buildTransform,
transformModule,
]
};
var client = cio.client(optionsAsInstance)
, client = cio.client(optionsAsBuilder)
, client = cio.client(optionsAsString);
var client = cio.client(optionsWithMultiple);
Usage: Provide Options for Builder Functions
It's possible to put options in the call to cio.client()
or cio.server()
which will be provided to the builder functions creating the transforms for you.
To do so, move the usual options into a sub-property transform
and the top transformer
object will be provided to the builder functions.
var usualOptions = { transformer: someBuilderFunction }
, newOptions = {
transformer: {
transform: someBuilderFunction,
some: 'other option values'
}
};
Build Transform
There are multiple ways to build a Transform.
- Use the standard methods described in Node's
stream
documentation (Note, this link is for the latest Node, be sure to check for the version you're using). - Use a helper module such as through or through2
- Use a builder module transforming (Note, I made this module...)
var Transform = require('stream').Transform;
function builder1(options) {
return new Transform({
transform: function (data, encoding, next) {
var string = data.toString('utf8')
, object = null;
try {
object = JSON.parse(string);
this.push(object);
next();
} catch(error) {
next({
error: 'Unable to parse string with JSON.parse()'
Error: error
});
}
}
});
}
var thru = require('through2')
function builder2(options) {
return thru(function (data, encoding, next) {
});
}
var transform = require('transforming')()
var builder3 = function(options) {
return transform.splitStringToObject(JSON.parse);
return transform.split('\n').string('utf8').toObject(JSON.parse);
}
MIT License