├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── example.js ├── index.js ├── package.json └── test.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "0.10" 4 | -------------------------------------------------------------------------------- /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 | # json-format-stream 2 | 3 | Streaming JSON serializer that allows you to add metadata and will forward an error message if the stream is destroyed prematurely 4 | 5 | ``` 6 | npm install json-format-stream 7 | ``` 8 | 9 | [![build status](http://img.shields.io/travis/mafintosh/json-format-stream.svg?style=flat)](http://travis-ci.org/mafintosh/json-format-stream) 10 | 11 | ## Usage 12 | 13 | ``` js 14 | var format = require('json-format-stream') 15 | 16 | var stream = format({some: 'metadata'}) 17 | 18 | stream.write({some: 'data'}) 19 | stream.write({more: 'data'}) 20 | stream.destroy(new Error('an error occurred')) 21 | 22 | stream.pipe(process.stdout) 23 | ``` 24 | 25 | Running the above will print out 26 | 27 | ``` 28 | { 29 | "some": "metadata", 30 | "result": [ 31 | { 32 | "some": "data" 33 | }, 34 | { 35 | "more": "data" 36 | } 37 | ], 38 | "error": "an error occurred" 39 | } 40 | ``` 41 | 42 | If you don't call `destroy` the error property in the result will be set to `null` when the stream finishes. 43 | The main result is streamed using [JSONStream](https://github.com/dominictarr/JSONStream.git) which makes this memory efficient 44 | 45 | ## API 46 | 47 | #### `stream = format(metadata, options)` 48 | 49 | Creates a new JSON formatter. Any metadata properties you provide in the constructor will be set in the beginning of the JSON response. 50 | 51 | Pass `options.outputKey` to specify which key data is added to. Defaults to `result`. 52 | 53 | ``` js 54 | var stream = format(null, {outputKey: 'data'}) 55 | ``` 56 | 57 | ## License 58 | 59 | MIT 60 | -------------------------------------------------------------------------------- /example.js: -------------------------------------------------------------------------------- 1 | var format = require('./') 2 | 3 | var stream = format({some: 'metadata'}) 4 | 5 | stream.write({some: 'data'}) 6 | stream.write({more: 'data'}) 7 | stream.destroy(new Error('an error occurred')) 8 | 9 | stream.pipe(process.stdout) 10 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var through = require('through2') 2 | var JSONStream = require('JSONStream') 3 | var duplexify = require('duplexify') 4 | 5 | module.exports = function (metadata, opts) { 6 | var outputKey = (opts && opts.outputKey) || 'result' 7 | var err = null 8 | 9 | var inp = JSONStream.stringify() 10 | var out = through.obj(function (data, enc, cb) { 11 | this.push(data) 12 | cb() 13 | }, function (cb) { 14 | this.push(',"error": ' + JSON.stringify(err) + '}') 15 | cb() 16 | }) 17 | 18 | out.push('{') 19 | 20 | if (metadata) { 21 | Object.keys(metadata).forEach(function (key) { 22 | out.push(JSON.stringify(key) + ':' + JSON.stringify(metadata[key]) + ',') 23 | }) 24 | } 25 | 26 | out.push('"' + outputKey + '":') 27 | 28 | var dup = duplexify.obj(inp, out, {destroy: false}) 29 | 30 | dup.on('close', function () { 31 | err = 'Unexpected close of stream' 32 | inp.end() 33 | }) 34 | 35 | dup.on('error', function (e) { 36 | err = e.message 37 | inp.end() 38 | }) 39 | 40 | inp.pipe(out) 41 | return dup 42 | } 43 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "json-format-stream", 3 | "version": "1.1.0", 4 | "description": "Streaming JSON serializer that allows you to add metadata and will forward an error message if the stream is destroyed prematurely", 5 | "main": "index.js", 6 | "dependencies": { 7 | "JSONStream": "^0.10.0", 8 | "duplexify": "^3.2.0", 9 | "through2": "^0.6.3" 10 | }, 11 | "devDependencies": { 12 | "concat-stream": "^1.4.7", 13 | "standard": "^2.3.2", 14 | "tape": "^3.5.0" 15 | }, 16 | "scripts": { 17 | "test": "standard && tape test.js" 18 | }, 19 | "repository": { 20 | "type": "git", 21 | "url": "https://github.com/mafintosh/json-format-stream.git" 22 | }, 23 | "author": "Mathias Buus (@mafintosh)", 24 | "license": "MIT", 25 | "bugs": { 26 | "url": "https://github.com/mafintosh/json-format-stream/issues" 27 | }, 28 | "homepage": "https://github.com/mafintosh/json-format-stream" 29 | } 30 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | var tape = require('tape') 2 | var concat = require('concat-stream') 3 | var format = require('./') 4 | 5 | tape('success response', function (t) { 6 | var stream = format() 7 | 8 | stream.write({hello: 'world'}) 9 | stream.write({hello: 'new world'}) 10 | stream.end() 11 | 12 | stream.pipe(concat(function (data) { 13 | t.same(JSON.parse(data), {result: [{hello: 'world'}, {hello: 'new world'}], error: null}) 14 | t.end() 15 | })) 16 | }) 17 | 18 | tape('error response', function (t) { 19 | var stream = format() 20 | 21 | stream.write({hello: 'world'}) 22 | stream.write({hello: 'new world'}) 23 | stream.destroy(new Error(':(')) 24 | 25 | stream.pipe(concat(function (data) { 26 | t.same(JSON.parse(data), {result: [{hello: 'world'}, {hello: 'new world'}], error: ':('}) 27 | t.end() 28 | })) 29 | }) 30 | 31 | tape('close error response', function (t) { 32 | var stream = format() 33 | 34 | stream.write({hello: 'world'}) 35 | stream.write({hello: 'new world'}) 36 | stream.destroy() 37 | 38 | stream.pipe(concat(function (data) { 39 | t.same(JSON.parse(data), {result: [{hello: 'world'}, {hello: 'new world'}], error: 'Unexpected close of stream'}) 40 | t.end() 41 | })) 42 | }) 43 | 44 | tape('metadata', function (t) { 45 | var stream = format({metadata: 42, test: 10}) 46 | 47 | stream.write({hello: 'world'}) 48 | stream.end() 49 | 50 | stream.pipe(concat(function (data) { 51 | t.same(JSON.parse(data), {metadata: 42, test: 10, result: [{hello: 'world'}], error: null}) 52 | t.end() 53 | })) 54 | }) 55 | 56 | tape('opts.outputKey', function (t) { 57 | var stream = format(null, {outputKey: 'data'}) 58 | 59 | stream.write({hello: 'world'}) 60 | stream.end() 61 | 62 | stream.pipe(concat(function (data) { 63 | t.same(JSON.parse(data), {data: [{hello: 'world'}], error: null}) 64 | t.end() 65 | })) 66 | }) 67 | --------------------------------------------------------------------------------