tiny-json-http
Advanced tools
Comparing version 3.0.6 to 3.0.7
28
_read.js
@@ -32,10 +32,7 @@ var http = require('http') | ||
// make a request | ||
method(opts, function __res(res) { | ||
var req = method(opts, function __res(res) { | ||
var raw = [] | ||
var statusCode = res.statusCode | ||
var contentType = res.headers['content-type'] | ||
var isJSON = contentType.startsWith('application/json') | ||
var ok = statusCode >= 200 && statusCode < 300 | ||
var ok = res.statusCode >= 200 && res.statusCode < 300 | ||
if (!ok) { | ||
@@ -47,15 +44,22 @@ callback(Error('GET failed with: ' + statusCode)) | ||
// res.setEncoding('utf8') | ||
res.on('data', function(chunk) {raw.push(chunk)}) | ||
res.on('end', function(x) { | ||
res.on('data', function __data(chunk) { | ||
raw.push(chunk) | ||
}) | ||
res.on('end', function __end() { | ||
var err = null | ||
var result = null | ||
try { | ||
var isJSON = res.headers['content-type'].startsWith('application/json') | ||
var rawData = Buffer.concat(raw).toString() | ||
var parsedData = isJSON? JSON.parse(rawData) : rawData | ||
callback(null, parsedData) | ||
result = isJSON? JSON.parse(rawData) : rawData | ||
} | ||
catch (e) { | ||
callback(e) | ||
err = e | ||
} | ||
callback(err, result) | ||
}) | ||
}).on('error', function(e) { callback(Error(e.message)) } ) | ||
}) | ||
req.on('error', callback) | ||
} |
@@ -31,8 +31,5 @@ var qs = require('querystring') | ||
var raw = [] | ||
var statusCode = res.statusCode | ||
var contentType = res.headers['content-type'] | ||
var isJSON = contentType === 'application/json' | ||
var raw = [] // keep our buffers here | ||
var ok = res.statusCode >= 200 && res.statusCode < 300 | ||
var ok = statusCode >= 200 && statusCode < 300 | ||
if (!ok) { | ||
@@ -44,17 +41,22 @@ callback(Error(httpMethod + ' failed with: ' + statusCode)) | ||
// res.setEncoding('utf8') | ||
res.on('data', function(chunk) { raw.push(chunk) }) | ||
res.on('end', function(x) { | ||
res.on('data', function __data(chunk) { | ||
raw.push(chunk) | ||
}) | ||
res.on('end', function __end() { | ||
var err = null | ||
var result = null | ||
try { | ||
var isJSON = res.headers['content-type'].startsWith('application/json') | ||
var rawData = Buffer.concat(raw).toString() | ||
var parsedData = isJSON? JSON.parse(rawData) : rawData | ||
callback(null, parsedData) | ||
result = isJSON? JSON.parse(rawData) : rawData | ||
} | ||
catch (e) { | ||
callback(e) | ||
err = e | ||
} | ||
callback(err, result) | ||
}) | ||
}) | ||
req.on('error', function(e) { callback(Error(e.message)) }) | ||
req.on('error', callback) | ||
@@ -61,0 +63,0 @@ req.write(postData) |
{ | ||
"name": "tiny-json-http", | ||
"version": "3.0.6", | ||
"version": "3.0.7", | ||
"main": "index.js", | ||
@@ -5,0 +5,0 @@ "scripts": { |
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
111
4812