├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── index.js ├── package.json └── test.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - '0.10' 4 | - '0.12' 5 | - 'stable' 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Mathias Buus 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # duplex-json-stream 2 | 3 | Turn a transport stream into an duplex stream that parses from / serializes to json 4 | 5 | ``` 6 | npm install duplex-json-stream 7 | ``` 8 | 9 | [![build status](http://img.shields.io/travis/mafintosh/duplex-json-stream.svg?style=flat)](http://travis-ci.org/mafintosh/duplex-json-stream) 10 | 11 | ## Usage 12 | 13 | ``` js 14 | var jsonStream = require('duplex-json-stream') 15 | var net = require('net') 16 | 17 | var server = net.createServer(function (socket) { 18 | socket = jsonStream(socket) // turn the transport stream into an object stream 19 | socket.on('data', function (data) { 20 | socket.write({echo: data}) // echo back the messages 21 | }) 22 | }) 23 | 24 | server.listen(10000) 25 | 26 | var client = jsonStream(net.connect(10000)) 27 | 28 | client.write({hello: 'world'}) 29 | client.on('data', function (data) { 30 | console.log(data) // will print {echo: {hello: 'world'}} 31 | }) 32 | ``` 33 | 34 | ## License 35 | 36 | MIT 37 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var pumpify = require('pumpify') 2 | var ndjson = require('ndjson') 3 | 4 | module.exports = function (stream) { 5 | return pumpify.obj(ndjson.serialize(), stream, ndjson.parse()) 6 | } 7 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "duplex-json-stream", 3 | "version": "1.0.1", 4 | "description": "Turn a transport stream into an object stream that parses from / serializes to json", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "tape test.js" 8 | }, 9 | "dependencies": { 10 | "ndjson": "^1.4.2", 11 | "pumpify": "^1.3.3" 12 | }, 13 | "devDependencies": { 14 | "tape": "^4.2.1" 15 | }, 16 | "repository": { 17 | "type": "git", 18 | "url": "https://github.com/mafintosh/duplex-json-stream.git" 19 | }, 20 | "author": "Mathias Buus (@mafintosh)", 21 | "license": "MIT", 22 | "bugs": { 23 | "url": "https://github.com/mafintosh/duplex-json-stream/issues" 24 | }, 25 | "homepage": "https://github.com/mafintosh/duplex-json-stream" 26 | } 27 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | var tape = require('tape') 2 | var net = require('net') 3 | var jsonStream = require('./') 4 | 5 | tape('serializes and parses', function (t) { 6 | var server = net.createServer(function (socket) { 7 | socket = jsonStream(socket) 8 | socket.on('data', function (data) { 9 | t.same(data, {hello: 'world'}) 10 | socket.write({echo: data}) 11 | socket.end() 12 | }) 13 | }) 14 | 15 | server.listen(0, function () { 16 | var client = jsonStream(net.connect(server.address().port)) 17 | 18 | client.write({hello: 'world'}) 19 | client.end() 20 | 21 | client.on('data', function (data) { 22 | server.close() 23 | t.same(data, {echo: {hello: 'world'}}) 24 | t.end() 25 | }) 26 | }) 27 | }) --------------------------------------------------------------------------------