├── .gitignore ├── .travis.yml ├── package.json ├── test.js ├── index.js ├── LICENSE └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | coverage/ 3 | tmp/ 4 | dist/ 5 | npm-debug.log* 6 | .DS_Store 7 | .nyc_output 8 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | node_js: 2 | - "4" 3 | - "5" 4 | - "6" 5 | - "7" 6 | sudo: false 7 | language: node_js 8 | script: "npm run test" 9 | # after_success: "npm i -g codecov && npm run coverage && codecov" 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "json-stream-to-object", 3 | "description": "Parse a JSON stream into an object", 4 | "repository": "yoshuawuyts/json-stream-to-object", 5 | "version": "1.1.0", 6 | "scripts": { 7 | "deps": "dependency-check . && dependency-check . --extra --no-dev", 8 | "start": "node .", 9 | "test": "standard && npm run deps && node test" 10 | }, 11 | "dependencies": { 12 | "end-of-stream": "^1.4.0", 13 | "fast-json-parse": "^1.0.2", 14 | "is-stream": "^1.1.0", 15 | "request-payload": "^1.0.1" 16 | }, 17 | "devDependencies": { 18 | "dependency-check": "^2.8.0", 19 | "get-server-port": "^1.0.0", 20 | "spok": "^0.8.1", 21 | "standard": "^10.0.2", 22 | "tape": "^4.6.3" 23 | }, 24 | "keywords": [ 25 | "stream", 26 | "parse", 27 | "json" 28 | ], 29 | "license": "MIT" 30 | } 31 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | var getPort = require('get-server-port') 2 | var http = require('http') 3 | var spok = require('spok') 4 | var tape = require('tape') 5 | var url = require('url') 6 | 7 | var toObject = require('./') 8 | 9 | tape('parse json', function (assert) { 10 | assert.plan(3) 11 | var server = http.createServer(function (req, res) { 12 | toObject(req, function (err, obj) { 13 | assert.ifError(err, 'no err parsing') 14 | spok(assert, obj, { 15 | foo: 'bar' 16 | }) 17 | res.end() 18 | }) 19 | }) 20 | 21 | server.listen(function () { 22 | var port = getPort(server) 23 | var opts = url.parse('http://localhost:' + port) 24 | opts.method = 'POST' 25 | var req = http.request(opts, function (res) { 26 | assert.equal(res.statusCode, 200, 'status OK') 27 | server.close() 28 | }) 29 | req.end(JSON.stringify({ foo: 'bar' })) 30 | }) 31 | }) 32 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var requestPayload = require('request-payload') 2 | var parse = require('fast-json-parse') 3 | var isStream = require('is-stream') 4 | var eos = require('end-of-stream') 5 | var assert = require('assert') 6 | 7 | module.exports = jsonStreamToObject 8 | 9 | function jsonStreamToObject (source, opts, cb) { 10 | if (!cb) { 11 | cb = opts 12 | opts = {} 13 | } 14 | 15 | assert.ok(isStream(source), 'json-stream-to-object: source should be an instance of Stream') 16 | assert.equal(typeof opts, 'object', 'json-stream-to-object: opts should be type object') 17 | assert.equal(typeof cb, 'function', 'json-stream-to-object: cb should be type function') 18 | 19 | eos(source, function (err) { 20 | if (err) return cb(err) // size limit was probably reached 21 | }) 22 | 23 | requestPayload(source, opts, function (buf) { 24 | var res = parse(buf) 25 | if (res.err) return cb(res.err) 26 | cb(null, res.value) 27 | }) 28 | } 29 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Yoshua Wuyts 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 all 13 | 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 THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # json-stream-to-object [![stability][0]][1] 2 | [![npm version][2]][3] [![build status][4]][5] 3 | [![downloads][8]][9] [![js-standard-style][10]][11] 4 | 5 | Parse a JSON stream into an object. 6 | 7 | ## Usage 8 | ```js 9 | var toObject = require('json-stream-to-object') 10 | var http = require('http') 11 | 12 | http.createServer(function (req, res) { 13 | toObject(req, function (err, obj) { 14 | if (err) throw err 15 | console.log('json parsed', obj) 16 | }) 17 | }).listen(8080) 18 | ``` 19 | 20 | ## API 21 | ### `toObject(stream, [opts], cb(err, object))` 22 | Parse a stream of JSON to an object. Opts can be: 23 | - __opts.limit:__ set a max size limit for the incoming data. Useful to prevent 24 | services getting DDoSed 25 | 26 | ## License 27 | [MIT](https://tldrlegal.com/license/mit-license) 28 | 29 | [0]: https://img.shields.io/badge/stability-experimental-orange.svg?style=flat-square 30 | [1]: https://nodejs.org/api/documentation.html#documentation_stability_index 31 | [2]: https://img.shields.io/npm/v/json-stream-to-object.svg?style=flat-square 32 | [3]: https://npmjs.org/package/json-stream-to-object 33 | [4]: https://img.shields.io/travis/yoshuawuyts/json-stream-to-object/master.svg?style=flat-square 34 | [5]: https://travis-ci.org/yoshuawuyts/json-stream-to-object 35 | [6]: https://img.shields.io/codecov/c/github/yoshuawuyts/json-stream-to-object/master.svg?style=flat-square 36 | [7]: https://codecov.io/github/yoshuawuyts/json-stream-to-object 37 | [8]: http://img.shields.io/npm/dm/json-stream-to-object.svg?style=flat-square 38 | [9]: https://npmjs.org/package/json-stream-to-object 39 | [10]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square 40 | [11]: https://github.com/feross/standard 41 | --------------------------------------------------------------------------------