Skip to content

Commit

Permalink
2.9.0 release! Stream support!
Browse files Browse the repository at this point in the history
  • Loading branch information
ethan7g committed Jan 6, 2018
1 parent 9dd9eae commit 62e502b
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 50 deletions.
45 changes: 41 additions & 4 deletions docs/global.html
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ <h5>Parameters:</h5>

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="phin.js.html">phin.js</a>, <a href="phin.js.html#line37">line 37</a>
<a href="phin.js.html">phin.js</a>, <a href="phin.js.html#line38">line 38</a>
</li></ul></dd>


Expand Down Expand Up @@ -566,6 +566,43 @@ <h5 class="subsection-title">Properties:</h5>



<tr>

<td class="name"><code>stream</code></td>


<td class="type">


<span class="param-type">boolean</span>



</td>


<td class="attributes">

&lt;optional><br>



</td>



<td class="default">

false

</td>


<td class="description last">Enable streaming of response. (Removes )</td>
</tr>



<tr>

<td class="name"><code>compression</code></td>
Expand Down Expand Up @@ -954,7 +991,7 @@ <h5>Parameters:</h5>



<td class="description last">phin response object. Like <a href='https://nodejs.org/api/http.html#http_class_http_serverresponse'>http.ServerResponse</a> but has a body property containing response body</td>
<td class="description last">phin response object. Like <a href='https://nodejs.org/api/http.html#http_class_http_serverresponse'>http.ServerResponse</a> but has a body property containing response body, unless stream. If stream option is enabled, a stream property will be provided to callback with a readable stream.</td>
</tr>


Expand Down Expand Up @@ -995,7 +1032,7 @@ <h5>Parameters:</h5>

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="phin.js.html">phin.js</a>, <a href="phin.js.html#line25">line 25</a>
<a href="phin.js.html">phin.js</a>, <a href="phin.js.html#line26">line 26</a>
</li></ul></dd>


Expand Down Expand Up @@ -1043,7 +1080,7 @@ <h2><a href="index.html">Home</a></h2><h3>Global</h3><ul><li><a href="global.htm
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Fri Dec 29 2017 20:35:06 GMT-0800 (PST)
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Fri Jan 05 2018 16:21:31 GMT-0800 (PST)
</footer>

<script> prettyPrint(); </script>
Expand Down
2 changes: 1 addition & 1 deletion docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ <h2><a href="index.html">Home</a></h2><h3>Global</h3><ul><li><a href="global.htm
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Fri Dec 29 2017 20:35:06 GMT-0800 (PST)
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Fri Jan 05 2018 16:21:31 GMT-0800 (PST)
</footer>

<script> prettyPrint(); </script>
Expand Down
46 changes: 27 additions & 19 deletions docs/phin.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ <h1 class="page-title">Source: phin.js</h1>
* @property {Object} [form] - Object to send as form data (sets 'Content-Type' and 'Content-Length' headers, as well as request body) (overwrites 'data' option if present)
* @property {Object} [headers={}] - Request headers
* @property {string} [parse=none] - Response parsing. Errors will be given if the repsonse can't be parsed. ('none', 'json')
* @property {boolean} [stream=false] - Enable streaming of response. (Removes )
* @property {boolean} [compression=false] - Enable compression for request
* @property {?number} [timeout=null] - Request timeout in milliseconds
* @property {string} [hostname=autodetect] - URL hostname
Expand All @@ -54,7 +55,7 @@ <h1 class="page-title">Source: phin.js</h1>
* Response data callback
* @callback phinResponseCallback
* @param {?(Error|string)} error - Error if any occurred in request, otherwise null.
* @param {?http.serverResponse} phinResponse - phin response object. Like &lt;a href='https://nodejs.org/api/http.html#http_class_http_serverresponse'>http.ServerResponse&lt;/a> but has a body property containing response body
* @param {?http.serverResponse} phinResponse - phin response object. Like &lt;a href='https://nodejs.org/api/http.html#http_class_http_serverresponse'>http.ServerResponse&lt;/a> but has a body property containing response body, unless stream. If stream option is enabled, a stream property will be provided to callback with a readable stream.
*/

/**
Expand All @@ -79,7 +80,8 @@ <h1 class="page-title">Source: phin.js</h1>
'headers': {},
'auth': (addr.auth || null),
'timeout': null,
'parse': 'none'
'parse': 'none',
'stream': false
}

if (typeof opts === 'object') {
Expand Down Expand Up @@ -114,24 +116,30 @@ <h1 class="page-title">Source: phin.js</h1>
stream = res.pipe(zlib.createInflate())
}
}
res.body = new Buffer([])
stream.on('data', (chunk) => {
res.body = Buffer.concat([res.body, chunk])
})
stream.on('end', () => {
if (cb) {
if (options.parse === 'json') {
try {
res.body = JSON.parse(res.body.toString())
}
catch (err) {
cb('Invalid JSON received.', res)
return
if (options.stream === true) {
res.stream = stream
cb(null, res)
}
else {
res.body = new Buffer([])
stream.on('data', (chunk) => {
res.body = Buffer.concat([res.body, chunk])
})
stream.on('end', () => {
if (cb) {
if (options.parse === 'json') {
try {
res.body = JSON.parse(res.body.toString())
}
catch (err) {
cb('Invalid JSON received.', res)
return
}
}
cb(null, res)
}
cb(null, res)
}
})
})
}
}

switch (addr.protocol.toLowerCase()) {
Expand Down Expand Up @@ -220,7 +228,7 @@ <h2><a href="index.html">Home</a></h2><h3>Global</h3><ul><li><a href="global.htm
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Fri Dec 29 2017 20:35:06 GMT-0800 (PST)
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Fri Jan 05 2018 16:21:31 GMT-0800 (PST)
</footer>

<script> prettyPrint(); </script>
Expand Down
2 changes: 1 addition & 1 deletion lib/phin.compiled.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 26 additions & 18 deletions lib/phin.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const util = require('util')
* @property {Object} [form] - Object to send as form data (sets 'Content-Type' and 'Content-Length' headers, as well as request body) (overwrites 'data' option if present)
* @property {Object} [headers={}] - Request headers
* @property {string} [parse=none] - Response parsing. Errors will be given if the repsonse can't be parsed. ('none', 'json')
* @property {boolean} [stream=false] - Enable streaming of response. (Removes )
* @property {boolean} [compression=false] - Enable compression for request
* @property {?number} [timeout=null] - Request timeout in milliseconds
* @property {string} [hostname=autodetect] - URL hostname
Expand All @@ -26,7 +27,7 @@ const util = require('util')
* Response data callback
* @callback phinResponseCallback
* @param {?(Error|string)} error - Error if any occurred in request, otherwise null.
* @param {?http.serverResponse} phinResponse - phin response object. Like <a href='https://nodejs.org/api/http.html#http_class_http_serverresponse'>http.ServerResponse</a> but has a body property containing response body
* @param {?http.serverResponse} phinResponse - phin response object. Like <a href='https://nodejs.org/api/http.html#http_class_http_serverresponse'>http.ServerResponse</a> but has a body property containing response body, unless stream. If stream option is enabled, a stream property will be provided to callback with a readable stream.
*/

/**
Expand All @@ -51,7 +52,8 @@ const phin = (opts, cb) => {
'headers': {},
'auth': (addr.auth || null),
'timeout': null,
'parse': 'none'
'parse': 'none',
'stream': false
}

if (typeof opts === 'object') {
Expand Down Expand Up @@ -86,24 +88,30 @@ const phin = (opts, cb) => {
stream = res.pipe(zlib.createInflate())
}
}
res.body = new Buffer([])
stream.on('data', (chunk) => {
res.body = Buffer.concat([res.body, chunk])
})
stream.on('end', () => {
if (cb) {
if (options.parse === 'json') {
try {
res.body = JSON.parse(res.body.toString())
}
catch (err) {
cb('Invalid JSON received.', res)
return
if (options.stream === true) {
res.stream = stream
cb(null, res)
}
else {
res.body = new Buffer([])
stream.on('data', (chunk) => {
res.body = Buffer.concat([res.body, chunk])
})
stream.on('end', () => {
if (cb) {
if (options.parse === 'json') {
try {
res.body = JSON.parse(res.body.toString())
}
catch (err) {
cb('Invalid JSON received.', res)
return
}
}
cb(null, res)
}
cb(null, res)
}
})
})
}
}

switch (addr.protocol.toLowerCase()) {
Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "phin",
"version": "2.8.1",
"version": "2.9.0",
"description": "Ultra-simple, lightweight, dependency-free Node.JS HTTP request client",
"main": "lib/phin.compiled.js",
"scripts": {
"test": "echo \"Tested before deployment.\" && exit 0",
"test-dev": "npm install --only=dev && node ./tests/test.compiled.js",
"test-dev": "node ./tests/test.js",
"prepublishOnly": "npm run test-dev",
"gendocs": "rm -r docs || true && jsdoc -R README.md -d ./docs lib/phin.js",
"compile": "npx babel lib/phin.js --out-file lib/phin.compiled.js --presets env --minified --no-comments && npx babel tests/test.js --out-file tests/test.compiled.js --presets env --minified --no-comments"
"compile": "npx babel lib/phin.js --out-file lib/phin.compiled.js --presets env --minified --no-comments"
},
"repository": {
"type": "git",
Expand All @@ -32,6 +32,7 @@
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-preset-env": "^1.6.1",
"poky": "^1.0.1",
"whew": "^1.0.0"
},
"files": [
Expand Down
Loading

0 comments on commit 62e502b

Please sign in to comment.