facebook-node-sdk
Advanced tools
Comparing version 0.1.14 to 0.2.0
@@ -1183,3 +1183,3 @@ var assert = require('assert'); | ||
BaseFacebook.prototype.makeRequest = function makeRequest(host, path, params, callback) { | ||
requestUtil.requestFacebookApi(https, host, 443, path, params, callback); | ||
requestUtil.requestFacebookApi(https, host, 443, path, params, this.fileUploadSupport, callback); | ||
}; | ||
@@ -1186,0 +1186,0 @@ |
var assert = require('assert'); | ||
var querystring = require('querystring'); | ||
var Multipart = require('./multipart'); | ||
exports.requestFacebookApi = function(http, host, port, path, params, callback) { | ||
exports.requestFacebookApi = function(http, host, port, path, params, withMultipart, callback) { | ||
var req = new FacebookApiRequest(http, host, port, path, params); | ||
req.start(callback); | ||
req.start(withMultipart, callback); | ||
}; | ||
@@ -20,28 +21,16 @@ | ||
function FacebookApiRequest(http, host, port, path, params) { | ||
assert.equal(this.postData, null); | ||
assert.equal(this.options, null); | ||
assert.equal(this.http, null); | ||
assert.equal(this.host, null); | ||
assert.equal(this.port, null); | ||
assert.equal(this.path, null); | ||
assert.equal(this.params, null); | ||
// TODO request timeout setting | ||
// TODO user agent setting | ||
// TODO support multipart/form-data | ||
// Querystring is encoding multibyte as utf-8. | ||
this.postData = querystring.stringify(params); | ||
// PostData must not have multibyte data. | ||
var postDataLength = this.postData.length; | ||
this.options = { | ||
host: host, | ||
path: path, | ||
port: port, | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/x-www-form-urlencoded', | ||
'Content-Length': postDataLength | ||
} | ||
}; | ||
this.http = http; | ||
this.host = host; | ||
this.port = port; | ||
this.path = path; | ||
this.params = params; | ||
@@ -55,5 +44,7 @@ this.selfBoundResponseErrorHandler = bindSelf(this, this.handleResponseError); | ||
FacebookApiRequest.prototype.postData = null; | ||
FacebookApiRequest.prototype.options = null; | ||
FacebookApiRequest.prototype.http = null; | ||
FacebookApiRequest.prototype.host = null; | ||
FacebookApiRequest.prototype.port = null; | ||
FacebookApiRequest.prototype.path = null; | ||
FacebookApiRequest.prototype.params = null; | ||
FacebookApiRequest.prototype.callback = null; | ||
@@ -66,6 +57,4 @@ FacebookApiRequest.prototype.selfBoundResponseErrorHandler = null; | ||
FacebookApiRequest.prototype.start = function(callback) { | ||
FacebookApiRequest.prototype.start = function(withMultipart, callback) { | ||
assert.equal(this.req, null); | ||
assert.notEqual(this.options, null); | ||
assert.notEqual(this.postData, null); | ||
assert.equal(this.callback, null); | ||
@@ -75,6 +64,88 @@ | ||
this.req = this.http.request(this.options); | ||
this.req.on('error', this.selfBoundResponseErrorHandler); | ||
this.req.on('response', this.selfBoundResponseHandler); | ||
this.req.end(this.postData); | ||
if (withMultipart) { | ||
var multipart = new Multipart(); | ||
var keys = Object.keys(this.params); | ||
var self = this; | ||
(function loop() { | ||
try { | ||
var key = keys.shift(); | ||
if (key === undefined) { | ||
afterParams(); | ||
return; | ||
} | ||
if (self.params[key].charAt(0) === '@') { | ||
multipart.addFile(key, self.params[key].substr(1), function(err) { | ||
if (err) { | ||
callback(err, null); | ||
} | ||
else { | ||
loop(); | ||
} | ||
}); | ||
} | ||
else { | ||
multipart.addText(key, self.params[key]); | ||
loop(); | ||
} | ||
} | ||
catch (err) { | ||
callback(err, null); | ||
} | ||
})(); | ||
function afterParams() { | ||
try { | ||
var options = { | ||
host: self.host, | ||
path: self.path, | ||
port: self.port, | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': multipart.getContentType(), | ||
'Content-Length': multipart.getContentLength() | ||
} | ||
}; | ||
self.req = self.http.request(options); | ||
self.req.on('error', self.selfBoundResponseErrorHandler); | ||
self.req.on('response', self.selfBoundResponseHandler); | ||
multipart.writeToStream(self.req, function(err) { | ||
if (err) { | ||
onerror(err); | ||
} | ||
else { | ||
self.req.end(); | ||
} | ||
}); | ||
} | ||
catch (err) { | ||
onerror(err); | ||
} | ||
function onerror(err) { | ||
if (self.req) { | ||
self.callQuietly(self.detachResponseAndErrorHandlers); | ||
self.callQuietly(self.abortRequest); | ||
} | ||
callback(err, null); | ||
} | ||
} | ||
} | ||
else { | ||
// Querystring is encoding multibyte as utf-8. | ||
var postData = querystring.stringify(this.params); | ||
var options = { | ||
host: this.host, | ||
path: this.path, | ||
port: this.port, | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/x-www-form-urlencoded', | ||
'Content-Length': postData.length | ||
} | ||
}; | ||
this.req = this.http.request(options); | ||
this.req.on('error', this.selfBoundResponseErrorHandler); | ||
this.req.on('response', this.selfBoundResponseHandler); | ||
this.req.end(postData); | ||
} | ||
}; | ||
@@ -81,0 +152,0 @@ |
{ | ||
"name": "facebook-node-sdk", | ||
"version": "0.1.14", | ||
"version": "0.2.0", | ||
"description": "Node.js SDK for the Facebook API", | ||
@@ -5,0 +5,0 @@ "tags": ["facebook"], |
@@ -19,3 +19,3 @@ var path = require('path'); | ||
var req = new requestUtil.requestFacebookApi(https, 'graph.facebook.com', 443, '/amachang', { method: 'GET' }, function(err, data) { | ||
var req = new requestUtil.requestFacebookApi(https, 'graph.facebook.com', 443, '/amachang', { method: 'GET' }, false, function(err, data) { | ||
e = err; | ||
@@ -27,2 +27,20 @@ d = data; | ||
simpleMultipartRequest: function(beforeExit, assert) { | ||
var e = null; | ||
var d = null; | ||
var done = false; | ||
beforeExit(function() { | ||
assert.equal(e, null); | ||
assert.notEqual(d, null); | ||
assert.equal(JSON.parse(d).id, '1055572299'); | ||
assert.ok(done); | ||
}); | ||
var req = new requestUtil.requestFacebookApi(https, 'graph.facebook.com', 443, '/amachang', { method: 'GET' }, true, function(err, data) { | ||
e = err; | ||
d = data; | ||
done = true; | ||
}); | ||
}, | ||
constructorAndStart: function(beforeExit, assert) { | ||
@@ -40,3 +58,3 @@ var e = null; | ||
var req = new requestUtil.FacebookApiRequest(https, 'graph.facebook.com', 443, '/amachang', { method: 'GET' }); | ||
req.start(function(err, data) { | ||
req.start(false, function(err, data) { | ||
e = err; | ||
@@ -61,3 +79,3 @@ d = data; | ||
req.start(function(err, data) { | ||
req.start(false, function(err, data) { | ||
e = err; | ||
@@ -84,3 +102,3 @@ d = data; | ||
req.selfBoundDataHandler = null; | ||
req.start(function(err, data) { | ||
req.start(false, function(err, data) { | ||
e = err; | ||
@@ -113,3 +131,3 @@ d = data; | ||
req.start(function(err, data) { | ||
req.start(false, function(err, data) { | ||
e = err; | ||
@@ -134,3 +152,3 @@ d = data; | ||
req.detachDataAndEndAndErrorHandlers = null; | ||
req.start(function(err, data) { | ||
req.start(false, function(err, data) { | ||
e = err; | ||
@@ -137,0 +155,0 @@ d = data; |
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
126363
18
3627
15