forked from jsforce/jsforce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transport.js
142 lines (132 loc) · 3.7 KB
/
transport.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*global process, Sfdc */
var inherits = require('inherits'),
stream = require('stream'),
Promise = require('./promise');
/* */
var nodeRequest = require('request'),
xhrRequest = require('./browser/request'),
canvas = require('./browser/canvas'),
jsonp = require('./browser/jsonp');
var request, baseUrl;
if (typeof window === 'undefined' || typeof global !== 'undefined') {
var defaults = {
followAllRedirects: true
};
if (process.env.HTTP_PROXY) {
defaults.proxy = process.env.HTTP_PROXY;
}
if (process.env.HTTP_TIMEOUT) {
defaults.timeout = process.env.HTTP_TIMEOUT;
}
request = nodeRequest.defaults(defaults);
baseUrl = process.env.LOCATION_BASE_URL || "";
} else {
request = xhrRequest;
var apiHost = normalizeApiHost(window.location.host);
baseUrl = apiHost ? "https://" + apiHost : "";
}
/**
* Add stream() method to promise (and following promise chain), to access original request stream.
* @private
*/
function streamify(promise, factory) {
var _then = promise.then;
promise.then = function() {
factory();
var newPromise = _then.apply(promise, arguments);
return streamify(newPromise, factory);
};
promise.stream = factory;
return promise;
}
/**
* Normarize Salesforce API host name
* @private
*/
function normalizeApiHost(apiHost) {
var m = /(\w+)\.(visual\.force|salesforce)\.com$/.exec(apiHost)
if (m) {
apiHost = m[1] + ".salesforce.com";
}
return apiHost;
}
/**
* Class for HTTP request transport
*
* @class
* @protected
*/
var Transport = module.exports = function() {};
/**
* Make HTTP request, returns promise instead of stream
*
* @param {Object} params - HTTP request
* @param {Callback.<Object>} [callback] - Calback Function
* @param {Callback.<Object>} [options] - Options
* @returns {Promise.<Object>}
*/
Transport.prototype.httpRequest = function(params, callback, options) {
var deferred = Promise.defer();
var req;
var httpRequest = request;
if (options && options.jsonp && jsonp.supported) {
httpRequest = jsonp.createRequest(options.jsonp);
} else if (options && options.signedRequest && canvas.supported) {
httpRequest = canvas.createRequest(options.signedRequest);
}
var createRequest = function() {
if (!req) {
req = httpRequest(params, function(err, response) {
if (err) {
deferred.reject(err);
} else {
deferred.resolve(response);
}
});
}
return req;
};
return streamify(deferred.promise, createRequest).thenCall(callback);
};
/**
* Class for HTTP request transport using AJAX proxy service
*
* @class Transport~ProxyTransport
* @protected
* @extends Transport
* @param {String} proxyUrl - AJAX Proxy server URL
*/
var ProxyTransport = Transport.ProxyTransport = function(proxyUrl) {
this._proxyUrl = proxyUrl;
};
inherits(ProxyTransport, Transport);
/**
* Make HTTP request via AJAX proxy
*
* @method Transport~ProxyTransport#httpRequest
* @param {Object} params - HTTP request
* @param {Callback.<Object>} [callback] - Calback Function
* @returns {Promise.<Object>}
*/
ProxyTransport.prototype.httpRequest = function(params, callback) {
var url = params.url;
if (url.indexOf("/") === 0) {
url = baseUrl + url;
}
var proxyParams = {
method: params.method,
url: this._proxyUrl + '?' + Date.now() + "." + ("" + Math.random()).substring(2),
headers: {
'salesforceproxy-endpoint': url
}
};
if (params.body || params.body === "") {
proxyParams.body = params.body;
}
if (params.headers) {
for (var name in params.headers) {
proxyParams.headers[name] = params.headers[name];
}
}
return ProxyTransport.super_.prototype.httpRequest.call(this, proxyParams, callback);
};