Service Adapter
Service Adapter - stream transform protocol with queue control
$ npm install service-adapter
Run tests
Browse module (e.g. node_modules/service-adapter
) install directory, and run tests:
$ npm test# or $ node test.js
Compare test results with travis run tests.
Include in your script
const adapter = ;
Socket adapter type
AaS
- Adapter as ServerAaC
- Adapter as Client
Diagram example
HTTP Server DB Server
----------- ---------
AaC | AaC <----------> AaS
^ ^
| Log Server |
| ---------- |
--------> AaS | AaC <---
Network router
A socket connection can only
be made between two different socket adapter type, see the diagram above. You can use the same set of functions
for different adapters.
const functions = { console; // call `AaC.func2` thisnext'func2'; } { console; // call `AaS.func3` thisnext'func3'; // optional, end client connection if thisname === 'AaC' thisclient; } { console; // optional, close the server if thisname === 'AaS' thisserver; } // ... and so on; ;// the `AaS` is listening on unix socket `/tmp/AaS.sock` and `AaC` is connecting to it/*Output:------AaS func1 callAaC func2 callAaS func3 call*/
Basic router
Adapters can be use to create routers between internal app functions, without connecting to any socket. This way, you can split the code between multiple micro services (adapters) in the same application for individual maintenance and debugging.
// object functions for adapter1const fc1 = console;// object functions for adapter2const fc2 = { console; // `adapter1` is next on the pipe, after `adapter2` // call function `test1` from `adapter1` thisnext'test1' head 'back'; };// adaptersconst adapter1 = fc1;const adapter2 = fc2; // create a router, data flow logicadapter1; // call function `test2` from `adapter2`adapter2; // `adapter2` is next on the pipe, after `adapter1`// call function `test2` from `adapter2`adapter1next'test2' 'welcome_'; // `adapter2.exec` has the same result as `adapter1.next` for the router created/*Output:------test2 call _welcometest1 call _welcome backtest2 call welcome_test1 call welcome_ back*/
new adapter (functions[, options])
Adapter constructor functions
- Object functions listoptions
- Object {queue
:Boolean,error
:String,limit
:Number,qumax
:Number}queue
- enable queue, default:false
(disabled)error
- error event name, default:err
limit
- limit parser bytes, default:Infinity
qumax
- max queue jobs, default:Infinity
Adapter prototype function
exec (func[, head[, body]])
- call functionfunc
from this adapternext (func[, head[, body]])
- call functionfunc
from next adapter on the pipehead
- Value, can be any type (not function)body
- Buffer or Stringexeq ()
- execute all queue jobsdelq ()
- delete all queue jobsjobs ()
- return all queue jobs
([head,[ body]])
Function adapter head
- Value, can be any type (not function)body
- Buffer
Enable queue
The adapter has an internal queue system, that is turned off by default. When queue is enabled, each adapter function execution 'adapter.exec
' (and pipe(adapter) exec) is considered as a job. The queue can be enabled on adapter init new adapter(functions, {queue:true})
or later adapter.queue=true
.
Attention, call
this.done()
at the end of each job (adapter function), in order to execute the next queue job, otherwise, if more jobs are added in queue, the queue list will increase until app is out of memory.
const functions = { console; // set a task for 1s const t = this; ; } { console; // add a job in the queue this; // exec the next job on the queue this; }; const adapter1 = functions queue: false ;// add two jobsadapter1;/*Output:------job1 calljob2 calljob1 calljob1 donejob1 done*/ // {queue:true} - enable queueconst adapter2 = functions queue: true ;// add jobsadapter2;/*Output:------job1 calljob1 donejob2 calljob1 calljob1 done*/
Disable queue
If queue is enabled on the adapter, there are two methods to disable it:
- Safe. Async execute all queue jobs
adapter.exeq()
(this may take a while, depending on how many jobs are in the queue), then disable the queueadapter.queue=false
. - Unsafe . Delete all queue jobs
adapter.delq()
(this is unsafe), then disable the queueadapter.queue=false
.
Read queue
If queue is enabled, function adapter.jobs()
, will return an array with all queue jobs. Use adapter.jobs().length
to see how many jobs are in the queue, at a given time.
Error handling
The adapter emit, by default, the event named err
for errors, to ensure the data flow (non-blocking state). For blocking state (no data flow), name it error
on constructor options {error:'error'}
, or later adapter.error='error'
.
// default adapter `err` error event namefunctions error:'err'// adapter error `err` event, non-blocking mode// stream standard `error` event, blocking mode;
For more informations, consult or run the test.js file.
Service Adapter is licensed under the MIT license. See the included LICENSE
file for more details.