{"_id":"service-adapter","_rev":"30-a64dd6b7e35948d3d93acbc06fcec02e","name":"service-adapter","time":{"modified":"2022-06-26T18:11:29.200Z","created":"2016-02-15T08:27:06.706Z","0.0.1":"2016-02-15T08:27:06.706Z","0.0.2":"2016-02-15T13:43:55.478Z","0.0.3":"2016-02-15T15:01:21.300Z","0.0.4":"2016-02-15T17:21:48.775Z","0.0.5":"2016-02-16T08:09:32.967Z","0.0.6":"2016-02-16T08:11:18.821Z","0.1.0":"2016-02-16T10:09:09.954Z","0.1.1":"2016-02-16T10:24:15.499Z","0.1.2":"2016-02-16T11:16:26.344Z","0.1.3":"2016-02-16T12:23:16.770Z","0.2.0":"2016-02-17T23:06:07.792Z","1.0.0":"2016-02-18T01:30:16.702Z","1.0.2":"2016-02-18T08:33:15.013Z","1.1.0":"2016-02-18T10:08:07.957Z","2.0.0":"2016-02-18T15:05:54.923Z","2.0.1":"2016-02-18T16:45:42.332Z","2.1.0":"2016-02-19T04:06:35.600Z","2.2.0":"2016-02-19T05:01:50.565Z","2.2.1":"2016-02-19T17:41:47.702Z","2.2.2":"2016-02-19T18:41:43.502Z","2.2.3":"2016-02-20T10:05:23.999Z","2.2.4":"2016-02-20T11:01:03.890Z","2.2.5":"2016-02-20T11:37:46.384Z","3.0.0":"2017-01-21T21:21:01.764Z","3.1.0":"2017-01-21T21:55:54.085Z","3.1.1":"2017-01-21T22:26:45.353Z","3.1.2":"2017-01-22T08:32:27.550Z","3.1.3":"2017-01-22T10:16:24.815Z"},"maintainers":[{"name":"realtimecom","email":"expertphp@gmail.com"}],"dist-tags":{"latest":"3.1.3"},"description":"Service Adapter - stream transform protocol with queue control","readme":"## Service Adapter\r\n[![NPM](https://nodei.co/npm/service-adapter.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/service-adapter/)\r\n\r\n[![Build Status](https://travis-ci.org/RealTimeCom/service-adapter.svg?branch=master)](http://travis-ci.org/RealTimeCom/service-adapter)\r\n[![dependencies](https://david-dm.org/RealTimeCom/service-adapter.svg)](https://david-dm.org/RealTimeCom/service-adapter)\r\n\r\n**Service Adapter - stream transform protocol with queue control**\r\n\r\n```sh\r\n$ npm install service-adapter\r\n```\r\n#### Run tests\r\nBrowse module (e.g. `node_modules/service-adapter`) install directory, and run tests:\r\n```sh\r\n$ npm test\r\n# or\r\n$ node test.js\r\n```\r\nCompare test results with <a href=\"https://travis-ci.org/RealTimeCom/service-adapter\">travis run tests</a>.\r\n\r\n#### Include in your script\r\n```js\r\nconst adapter = require('service-adapter');\r\n```\r\n#### Socket adapter type\r\n* `AaS` - Adapter as Server\r\n* `AaC` - Adapter as Client\r\n\r\n#### Diagram example\r\n```\r\nHTTP Server          DB Server\r\n-----------          ---------\r\n AaC | AaC <----------> AaS\r\n  ^                      ^\r\n  |        Log Server    |\r\n  |        ----------    |\r\n  --------> AaS | AaC <---\r\n```\r\n#### Network router\r\nA 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.\r\n```js\r\nconst functions = {\r\n    func1: function(head, body) {\r\n        console.log(this.name, 'func1 call');\r\n        // call `AaC.func2`\r\n        this.next('func2');\r\n    },\r\n    func2: function(head, body) {\r\n        console.log(this.name, 'func2 call');\r\n        // call `AaS.func3`\r\n        this.next('func3');\r\n        // optional, end client connection\r\n        if (this.name === 'AaC') { this.client.end(); }\r\n    },\r\n    func3: function(head, body) {\r\n        console.log(this.name, 'func3 call');\r\n        // optional, close the server\r\n        if (this.name === 'AaS') { this.server.close(); }\r\n    }\r\n    // ... and so on\r\n};\r\n\r\nrequire('net').createServer(function(socket) {\r\n\r\n    // create the `AaS` adapter\r\n    const AaS = new adapter(functions);\r\n    // optional, adapter name\r\n    AaS.name = 'AaS';\r\n    // optional, create `this.server`, see `func3`\r\n    AaS.server = this;\r\n    // pipe `AaS` into `socket` stream\r\n    socket.pipe(AaS).pipe(socket);\r\n\r\n}).listen('/tmp/AaS.sock', () => {\r\n\r\n    require('net').connect('/tmp/AaS.sock', function() {\r\n\r\n        // create the `AaC` adapter\r\n        const AaC = new adapter(functions);\r\n        // optional, adapter name\r\n        AaC.name = 'AaC';\r\n        // optional, create `this.client`, see `func2`\r\n        AaC.client = this;\r\n        // pipe `AaC` into `this` client stream\r\n        this.pipe(AaC).pipe(this);\r\n        // start data flow\r\n        AaC.next('func1'); // call `AaS.func1`\r\n\r\n    });\r\n\r\n});\r\n// the `AaS` is listening on unix socket `/tmp/AaS.sock` and `AaC` is connecting to it\r\n/*\r\nOutput:\r\n------\r\nAaS func1 call\r\nAaC func2 call\r\nAaS func3 call\r\n*/\r\n```\r\n#### Basic router\r\nAdapters 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.\r\n```js\r\n// object functions for adapter1\r\nconst fc1 = {\r\n    test1: (head, body) => console.log('test1 call', head, body.toString())\r\n};\r\n// object functions for adapter2\r\nconst fc2 = {\r\n    test2: function(head, body) {\r\n        console.log('test2 call', head);\r\n        // `adapter1` is next on the pipe, after `adapter2`\r\n        // call function `test1` from `adapter1`\r\n        this.next('test1', head, 'back');\r\n    }\r\n};\r\n// adapters\r\nconst adapter1 = new adapter(fc1);\r\nconst adapter2 = new adapter(fc2);\r\n\r\n// create a router, data flow logic\r\nadapter1.pipe(adapter2).pipe(adapter1);\r\n\r\n// call function `test2` from `adapter2`\r\nadapter2.exec('test2', '_welcome');\r\n\r\n// `adapter2` is next on the pipe, after `adapter1`\r\n// call function `test2` from `adapter2`\r\nadapter1.next('test2', 'welcome_');\r\n\r\n// `adapter2.exec` has the same result as `adapter1.next` for the router created\r\n/*\r\nOutput:\r\n------\r\ntest2 call _welcome\r\ntest1 call _welcome back\r\ntest2 call welcome_\r\ntest1 call welcome_ back\r\n*/\r\n```\r\n##### Adapter constructor `new adapter (functions[, options])`\r\n* <b><code>functions</code></b> - Object functions list\r\n* <b><code>options</code></b> - Object {`queue`:Boolean, `error`:String, `limit`:Number, `qumax`:Number}\r\n* `queue` - enable queue, default: `false` (disabled)\r\n* `error` - error event name, default: `err`\r\n* `limit` - limit parser bytes, default: `Infinity`\r\n* `qumax` - max queue jobs, default: `Infinity`\r\n\r\n##### Adapter prototype function\r\n* <b><code>exec (func[, head[, body]])</code></b> - call function `func` from this adapter\r\n* <b><code>next (func[, head[, body]])</code></b> - call function `func` from next adapter on the pipe\r\n* `head` - Value, can be any type (not function)\r\n* `body` - Buffer or String\r\n* <b><code>exeq ()</code></b> - execute all queue jobs\r\n* <b><code>delq ()</code></b> - delete all queue jobs\r\n* <b><code>jobs ()</code></b> - return all queue jobs\r\n\r\n##### Function adapter `([head,[ body]])`\r\n* `head` - Value, can be any type (not function)\r\n* `body` - Buffer\r\n\r\n#### Enable queue\r\nThe 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`.\r\n> <b>Attention</b>, 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.\r\n\r\n```js\r\nconst functions = {\r\n    job1: function(head, body) {\r\n        console.log('job1 call');\r\n        // set a task for 1s\r\n        const t = this;\r\n        setTimeout(() => {\r\n            // the task is done\r\n            console.log('job1 done');\r\n            // exec the next job on the queue\r\n            t.done();\r\n        }, 1000);\r\n    },\r\n    job2: function(head, body) {\r\n        console.log('job2 call');\r\n        // add a job in the queue\r\n        this.exec('job1');\r\n        // exec the next job on the queue\r\n        this.done();\r\n    }\r\n};\r\n\r\nconst adapter1 = new adapter(functions, { queue: false });\r\n// add two jobs\r\nadapter1.exec('job1').exec('job2');\r\n/*\r\nOutput:\r\n------\r\njob1 call\r\njob2 call\r\njob1 call\r\njob1 done\r\njob1 done\r\n*/\r\n\r\n// {queue:true} - enable queue\r\nconst adapter2 = new adapter(functions, { queue: true });\r\n// add jobs\r\nadapter2.exec('job1').exec('job1').exec('job1');\r\n/*\r\nOutput:\r\n------\r\njob1 call\r\njob1 done\r\njob2 call\r\njob1 call\r\njob1 done\r\n*/\r\n```\r\n#### Disable queue\r\nIf queue is enabled on the adapter, there are two methods to disable it:\r\n\r\n1. **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 queue `adapter.queue=false`.\r\n2. **Unsafe** . Delete all queue jobs `adapter.delq()` (this is unsafe), then disable the queue `adapter.queue=false`.\r\n\r\n#### Read queue\r\nIf 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.\r\n\r\n#### Error handling\r\nThe 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'`.\r\n```js\r\n// default adapter `err` error event name\r\nnew adapter(functions, {error:'err'}).\r\n// adapter error `err` event, non-blocking mode\r\non('err', e => console.log('adapter onErr', e)).\r\n// stream standard `error` event, blocking mode\r\non('error', e => console.log('adapter onError', e));\r\n```\r\n\r\n**For more informations, consult or run the <a href=\"https://github.com/RealTimeCom/service-adapter/blob/master/test.js\"><b>test.js</b></a> file.**\r\n\r\n--------------------------------------------------------\r\n**Service Adapter** is licensed under the MIT license. See the included `LICENSE` file for more details.\r\n","versions":{"3.0.0":{"name":"service-adapter","version":"3.0.0","description":"Service Adapter - stream transform protocol with queue control","main":"index.js","scripts":{"test":"node test.js"},"repository":{"type":"git","url":"git+https://github.com/RealTimeCom/service-adapter.git"},"keywords":["service","microservice","adapter","adaptor","server","client","proxy","protocol","transform","stream"],"author":{"name":"Tanase Laurentiu Iulian","email":"expertphp@gmail.com","url":"https://github.com/RealTimeCom"},"license":"MIT","bugs":{"url":"https://github.com/RealTimeCom/service-adapter/issues"},"homepage":"https://github.com/RealTimeCom/service-adapter#readme","_id":"service-adapter@3.0.0","_shasum":"2ffcdb799896132c969b8dff0379e0e60b3547d3","_from":".","_npmVersion":"4.1.2","_nodeVersion":"7.4.0","_npmUser":{"name":"realtimecom","email":"expertphp@gmail.com"},"dist":{"shasum":"2ffcdb799896132c969b8dff0379e0e60b3547d3","tarball":"https://registry.npmjs.org/service-adapter/-/service-adapter-3.0.0.tgz","integrity":"sha512-k2ZQ2pTZDE8Vvly6legYO+DnicPgG98I5ScWRW0W3ylSX06W2C8A9LvZNzQMG+19n7wyU4nj3EAdjSiEQcIXjg==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQDR5mwc9cRXJe+rwmzgcuzqudSI6xf+OftMy6LzW4GT1gIhANgVHaS4QGQTTaVtkrdNkDlCYQILbSlFjnC59x6Hl5M8"}]},"maintainers":[{"name":"realtimecom","email":"expertphp@gmail.com"}],"_npmOperationalInternal":{"host":"packages-12-west.internal.npmjs.com","tmp":"tmp/service-adapter-3.0.0.tgz_1485033659727_0.2634325036779046"}},"3.1.0":{"name":"service-adapter","version":"3.1.0","description":"Service Adapter - stream transform protocol with queue control","main":"index.js","scripts":{"test":"node test.js"},"repository":{"type":"git","url":"git+https://github.com/RealTimeCom/service-adapter.git"},"keywords":["service","microservice","adapter","adaptor","server","client","proxy","protocol","transform","stream"],"author":{"name":"Tanase Laurentiu Iulian","email":"expertphp@gmail.com","url":"https://github.com/RealTimeCom"},"license":"MIT","bugs":{"url":"https://github.com/RealTimeCom/service-adapter/issues"},"homepage":"https://github.com/RealTimeCom/service-adapter#readme","_id":"service-adapter@3.1.0","_shasum":"0ac1bb2965aa10f54b3c8add68c86442cae67ccd","_from":".","_npmVersion":"4.1.2","_nodeVersion":"7.4.0","_npmUser":{"name":"realtimecom","email":"expertphp@gmail.com"},"dist":{"shasum":"0ac1bb2965aa10f54b3c8add68c86442cae67ccd","tarball":"https://registry.npmjs.org/service-adapter/-/service-adapter-3.1.0.tgz","integrity":"sha512-oGgd/XYBQJMWQrLTlZ6GT04sDcTlVF4Jvl9f49Xz7iPw4PDllygjH7rjvM/3a1d840AfnxWRuUWl2qEdl7W0yA==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIGE3hxKR/3UKxkwTcPpGPZ7ptGYBw7c+OeD3sEpTl5V7AiApQdsbiFgPsKKo41+CCCRCl/LQVdM1Aloyu2z/pkFWdw=="}]},"maintainers":[{"name":"realtimecom","email":"expertphp@gmail.com"}],"_npmOperationalInternal":{"host":"packages-18-east.internal.npmjs.com","tmp":"tmp/service-adapter-3.1.0.tgz_1485035753432_0.2589996256865561"}},"3.1.1":{"name":"service-adapter","version":"3.1.1","description":"Service Adapter - stream transform protocol with queue control","main":"index.js","scripts":{"test":"node test.js"},"repository":{"type":"git","url":"git+https://github.com/RealTimeCom/service-adapter.git"},"keywords":["service","microservice","adapter","adaptor","server","client","proxy","protocol","transform","stream"],"author":{"name":"Tanase Laurentiu Iulian","email":"expertphp@gmail.com","url":"https://github.com/RealTimeCom"},"license":"MIT","bugs":{"url":"https://github.com/RealTimeCom/service-adapter/issues"},"homepage":"https://github.com/RealTimeCom/service-adapter#readme","_id":"service-adapter@3.1.1","_shasum":"07e9a7ae32b553020e06b2aff163a0d2edbe71ee","_from":".","_npmVersion":"4.1.2","_nodeVersion":"7.4.0","_npmUser":{"name":"realtimecom","email":"expertphp@gmail.com"},"dist":{"shasum":"07e9a7ae32b553020e06b2aff163a0d2edbe71ee","tarball":"https://registry.npmjs.org/service-adapter/-/service-adapter-3.1.1.tgz","integrity":"sha512-mDKEdIRvJz0z5R8iR2cOvXXHWKUQS/yXEL3CKYi3DWvf6e4HdRrVb4HGATmlDyz4+qbMPtxewG8nc7IsqZQ+cw==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIAkpofcVS/rQb7kgeVgUZ/wACS9wsPDb86pE0BzWlR/VAiBmTb4xdnchhoC7YMfXQtzM37gVYoUwpS01vLZzX9VjFg=="}]},"maintainers":[{"name":"realtimecom","email":"expertphp@gmail.com"}],"_npmOperationalInternal":{"host":"packages-18-east.internal.npmjs.com","tmp":"tmp/service-adapter-3.1.1.tgz_1485037604649_0.8958133163396269"}},"3.1.2":{"name":"service-adapter","version":"3.1.2","description":"Service Adapter - stream transform protocol with queue control","main":"index.js","scripts":{"test":"node test.js"},"repository":{"type":"git","url":"git+https://github.com/RealTimeCom/service-adapter.git"},"keywords":["service","microservice","adapter","adaptor","server","client","proxy","protocol","transform","stream"],"author":{"name":"Tanase Laurentiu Iulian","email":"expertphp@gmail.com","url":"https://github.com/RealTimeCom"},"license":"MIT","bugs":{"url":"https://github.com/RealTimeCom/service-adapter/issues"},"homepage":"https://github.com/RealTimeCom/service-adapter#readme","_id":"service-adapter@3.1.2","_shasum":"bd9c4b870afccf06a1e2d8919e7872466e856e6b","_from":".","_npmVersion":"4.1.2","_nodeVersion":"7.4.0","_npmUser":{"name":"realtimecom","email":"expertphp@gmail.com"},"dist":{"shasum":"bd9c4b870afccf06a1e2d8919e7872466e856e6b","tarball":"https://registry.npmjs.org/service-adapter/-/service-adapter-3.1.2.tgz","integrity":"sha512-D9kZ5lGrw2XherPnVPGxqmQD53rqTu/8KjPuMdtczA6l8tPi13MTRQ33JNmaVFq4dnVAdwLdFJVEGUHkdk5gQA==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQCn9/7zF9QUH3xyF5zu4OjI3qwRVGEhuv0bVQi2KME8vAIgZoAzaxO8Vw5AaHE9RZKEAUwDL4CoBuXsMIFy1hnavoc="}]},"maintainers":[{"name":"realtimecom","email":"expertphp@gmail.com"}],"_npmOperationalInternal":{"host":"packages-12-west.internal.npmjs.com","tmp":"tmp/service-adapter-3.1.2.tgz_1485073945654_0.7509415750391781"}},"3.1.3":{"name":"service-adapter","version":"3.1.3","description":"Service Adapter - stream transform protocol with queue control","main":"index.js","scripts":{"test":"node test.js"},"repository":{"type":"git","url":"git+https://github.com/RealTimeCom/service-adapter.git"},"keywords":["service","microservice","adapter","adaptor","server","client","proxy","protocol","transform","stream"],"author":{"name":"Tanase Laurentiu Iulian","email":"expertphp@gmail.com","url":"https://github.com/RealTimeCom"},"license":"MIT","bugs":{"url":"https://github.com/RealTimeCom/service-adapter/issues"},"homepage":"https://github.com/RealTimeCom/service-adapter#readme","_id":"service-adapter@3.1.3","_shasum":"349ecf23bcfd401859815b2a128fc8478df2f29c","_from":".","_npmVersion":"4.1.2","_nodeVersion":"7.4.0","_npmUser":{"name":"realtimecom","email":"expertphp@gmail.com"},"dist":{"shasum":"349ecf23bcfd401859815b2a128fc8478df2f29c","tarball":"https://registry.npmjs.org/service-adapter/-/service-adapter-3.1.3.tgz","integrity":"sha512-u9VpPT3rJQkzaMyBC642MA6ZjuQpwbbuecj4FTASPg2gsDl4fSX+WHDdhphd5u4MnIf16UDPS1lW1qm88XzTug==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIBabEgZtyMXS0ZE5Vb35KqT+Y8PgmupinXDc5VfMh4IHAiBbk+XeMA/EpXUxH5RU44Od8A2wLlftKnqogACUFMWXsA=="}]},"maintainers":[{"name":"realtimecom","email":"expertphp@gmail.com"}],"_npmOperationalInternal":{"host":"packages-18-east.internal.npmjs.com","tmp":"tmp/service-adapter-3.1.3.tgz_1485080184165_0.44910828582942486"}}},"homepage":"https://github.com/RealTimeCom/service-adapter#readme","keywords":["service","microservice","adapter","adaptor","server","client","proxy","protocol","transform","stream"],"repository":{"type":"git","url":"git+https://github.com/RealTimeCom/service-adapter.git"},"author":{"name":"Tanase Laurentiu Iulian","email":"expertphp@gmail.com","url":"https://github.com/RealTimeCom"},"bugs":{"url":"https://github.com/RealTimeCom/service-adapter/issues"},"license":"MIT","readmeFilename":"README.md"}