Comparing version 1.1.0 to 1.2.0
@@ -0,1 +1,12 @@ | ||
## 1.2.0 | ||
#### Chore | ||
* minor changelog script fix ([a17a035](https://github.com/DonutEspresso/big-json/commit/a17a035e62d6d449a7ed69f59758dfa7425063d5)) | ||
* update changelog script ([73980ad](https://github.com/DonutEspresso/big-json/commit/73980adb898a8de4fc5a9d4ba5e5aff84420b3c7)) | ||
#### New | ||
* add callback style APIs (#4) ([a12598f](https://github.com/DonutEspresso/big-json/commit/a12598fbfc70307427c9b34e3e05ccce59553919)) | ||
## 1.1.0 | ||
@@ -2,0 +13,0 @@ |
'use strict'; | ||
// core modules | ||
const stream = require('stream'); | ||
// external modules | ||
@@ -39,3 +42,4 @@ const Assembler = require('stream-json/utils/Assembler'); | ||
* @param {Object} [options] an options object | ||
* @param {Object} [options.multibyte] when true, support multibyte | ||
* @param {Object} [options.multibyte] when true, support multibyte. defaults | ||
* to true. | ||
* @function createParseStream | ||
@@ -130,5 +134,81 @@ * @return {Stream} | ||
/** | ||
* stream based JSON.parse. async function signature to abstract over streams. | ||
* @public | ||
* @param {Object} opts options to pass to parse stream | ||
* @param {String} opts.body string to parse | ||
* @param {Function} callback a callback function | ||
* @return {Object} the parsed JSON object | ||
*/ | ||
function parse(opts, callback) { | ||
assert.object(opts, 'opts'); | ||
assert.string(opts.body, 'opts.body'); | ||
assert.func(callback, 'callback'); | ||
const writeStream = through2.obj(function(chunk, enc, cb) { | ||
this.push(chunk); | ||
return cb(); | ||
}); | ||
const parseStream = createParseStream(opts); | ||
parseStream.on('data', function(pojo) { | ||
return callback(null, pojo); | ||
}); | ||
parseStream.on('error', function(err) { | ||
return callback(err); | ||
}); | ||
writeStream.pipe(parseStream); | ||
writeStream.end(opts.body); | ||
} | ||
/** | ||
* stream based JSON.stringify. async function signature to abstract over | ||
* streams. | ||
* @public | ||
* @param {Object} opts options to pass to stringify stream | ||
* @param {Function} callback a callback function | ||
* @function parse | ||
* @return {Object} the parsed JSON object | ||
*/ | ||
function stringify(opts, callback) { | ||
assert.object(opts, 'opts'); | ||
assert.func(callback, 'callback'); | ||
let done = false; | ||
let stringified = ''; | ||
const stringifyStream = createStringifyStream(opts); | ||
const passthroughStream = new stream.PassThrough(); | ||
// setup the passthrough stream as a sink | ||
passthroughStream.on('data', function(chunk) { | ||
stringified += chunk; | ||
}); | ||
passthroughStream.on('end', function() { | ||
// if we didn't already error and exit | ||
if (done === false) { | ||
return callback(null, stringified); | ||
} | ||
return null; | ||
}); | ||
// don't know what errors stringify stream may emit, but pass them back | ||
// up. | ||
stringifyStream.on('error', function(err) { | ||
done = true; | ||
return callback(err); | ||
}); | ||
stringifyStream.pipe(passthroughStream); | ||
} | ||
module.exports = { | ||
createParseStream, | ||
createStringifyStream | ||
createStringifyStream, | ||
parse, | ||
stringify | ||
}; |
{ | ||
"name": "big-json", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"main": "lib/index.js", | ||
@@ -5,0 +5,0 @@ "description": "A stream based implementation of JSON.parse and JSON.stringify for big POJOs", |
@@ -21,3 +21,3 @@ # big-json | ||
possible to run up against the V8 string length limitation, which is currently | ||
(as of 9/2017) 268435440 characters. Thus, if your large object has enough keys | ||
(as of 9/2017) limited to 512MB. Thus, if your large object has enough keys | ||
or values, it is possible to exceed the string length limit when calling | ||
@@ -102,2 +102,22 @@ [JSON.stringify](https://github.com/nodejs/node/issues/10738). | ||
### parse(opts, callback) | ||
An async JSON.parse using the same underlying stream implementation, but with | ||
a callback interface. | ||
* `opts` {Object} an options object passed to `createParseStream` | ||
* `opts.body` {Object} the string to be parsed | ||
* `callback` {Function} a callback object | ||
__Returns__: {Object} the parsed object | ||
### stringify(opts, callback) | ||
An async JSON.stringify using the same underlying stream implementation, but | ||
with a callback interface. | ||
* `opts` {Object} an options object passed to `createStringifyStream` | ||
* `opts.body` {Object} the object to be stringified | ||
* `callback` {Function} a callback object | ||
__Returns__: {Object} the stringified object | ||
## Contributing | ||
@@ -104,0 +124,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
15726
180
148