jsonpipe
jsonpipe is a lightweight AJAX client for chunked JSON responses. The API is similar to jQuery ajax, but for JSON responses transmitted through chunked encoding. It is a standalone utility with no dependencies.
Browser Support
IE8 and above. All versions of Chrome, Safari, Firefox, Opera and Android browsers.
Prerequisites
To use jsonpipe, the server should
- Emit the Transfer-Encoding: chunked HTTP header (only if HTTP/1.* is used)
- Every valid JSON object should be separated by the delimiter
\n\n
(double new line character, it is also configurable). Instead of processing the JSON on every chunk, jsonpipe waits for the delimiter and then processes. The server should always ensure there is a valid JSON object between the delimiter. The reasoning behind this is, even when a chunk has an invalid JSON (which is very likely), the JSON processing would not break and wait for the next delimiter. A sample JSON response shown below
{
"id": 12345,
"title": "Bruce Wayne",
"price": "$199.99"
}
\n\n
{
"id": 67890,
"title": "Bane",
"price": "$299.99"
}
Usage
jsonpipe.js is bundled as a browserify CommonJS module, so it can be used in the same node.js require
style. It has only one API named flow
exposed
<script src="jsonpipe.js"></script>
var jsonpipe = require('jsonpipe');
jsonpipe.flow('http://api.com/items?q=batman', {
"delimiter": "",
"onUploadProgress", function(progressEvent) {
},
"onHeaders": function(statusText, headers) {
}
"success": function(data) {
},
"error": function(errorMsg) {
},
"complete": function(statusText) {
},
"timeout": 3000,
"method": "GET",
"headers": {
"X-Requested-With": "XMLHttpRequest"
},
"data": "",
"disableContentType": false,
"withCredentials": true
});
options
delimiter
Type: String
The delimiter separating valid JSON objects in the chunked response; default is \n\n
onUploadProgress
Type: Function
The callback function to be called when browser's native upload progress event triggered.
Type: Function
The callback function to be called when headers are received. The function gets passed the the XMLHttpRequest
object's statusText
and the headers.
success
Type: Function
The callback function to be called on every valid JSON chunk. The function gets passed the parsed JSON object.
error
Type: Function
The callback function to be called on error scenarios. The function gets passed with an error message, reasoning the failure. There can be many reasons for errors, the most common one being the JSON parse error. It that case the error message would be parsererror
. For errors associated with the HTTP request the message would be XMLHttpRequest
object's statusText
.
complete
Type: Function
The callback function to be called when the request finishes (after success and error callbacks are executed). The function gets passed the XMLHttpRequest
object's statusText
.
timeout
Type: Number
Timeout in milliseconds for the HTTP request. If a call exceeds the timeout, the call is aborted and error function is called.
method
Type: String
The HTTP method/type of request to make (e.g. POST
, DELETE
, PUT
); default is GET
.
Type: Object
An object of additional header key/value pairs to send along with request.
data
Type: String
| FormData
| File
| Blob
What to be sent in the request body. Please note if you would like to send anything rather than String
, the option disableContentType
must set to true
.
disableContentType
Type Boolean
By default jsonpipe will set Content-Type
to "application/x-www-form-urlencoded", you can set disableContentType
to true
to skip this behavior. Must set true
if your data
is not a string.
Testing
The entire test suite for the jsonpipe API is available in the main test file jsonpipe.js. The mocha-phantomjs wrapper is used as the testing framework and chai for assertion. To run the tests - clone/fork the repo,
install the package $ npm install
and run
$ npm test
Issues
Have a bug or a feature request? Please open a new issue
Author(s)
Senthil Padmanabhan
License
Copyright (c) 2015 eBay Inc.
Released under the MIT License
http://www.opensource.org/licenses/MIT