-
Notifications
You must be signed in to change notification settings - Fork 21
/
index.js
367 lines (327 loc) · 10.8 KB
/
index.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
var h54sError = require('../error.js');
var logs = require('../logs.js');
var Tables = require('../tables');
var SasData = require('../sasData.js');
var Files = require('../files');
/*
* Call Sas program
*
* @param {string} sasProgram - Path of the sas program
* @param {function} callback - Callback function called when ajax call is finished
*
*/
module.exports.call = function(sasProgram, dataObj, callback, params) {
var self = this;
var retryCount = 0;
var dbg = this.debug;
if (!callback || typeof callback !== 'function'){
throw new h54sError('argumentError', 'You must provide callback');
}
if(!sasProgram) {
throw new h54sError('argumentError', 'You must provide Sas program file path');
}
if(typeof sasProgram !== 'string') {
throw new h54sError('argumentError', 'First parameter should be string');
}
if(this.useMultipartFormData === false && !(dataObj instanceof Tables)) {
throw new h54sError('argumentError', 'Cannot send files using application/x-www-form-urlencoded. Please use Tables or default value for useMultipartFormData');
}
if(!params) {
params = {
_program: this._utils.getFullProgramPath(this.metadataRoot, sasProgram),
_debug: this.debug ? 131 : 0,
_service: 'default',
};
}
if(dataObj) {
var key, dataProvider;
if(dataObj instanceof Tables) {
dataProvider = dataObj._tables;
} else if(dataObj instanceof Files || dataObj instanceof SasData){
dataProvider = dataObj._files;
} else {
throw new h54sError('argumentError', 'Wrong type of tables object');
}
for(key in dataProvider) {
if(dataProvider.hasOwnProperty(key)) {
params[key] = dataProvider[key];
}
}
}
if(this._disableCalls) {
this._pendingCalls.push({
sasProgram: sasProgram,
callback: callback,
params: params
});
return;
}
this._ajax.post(this.url, params, this.useMultipartFormData).success(function(res) {
if(self._utils.needToLogin.call(self, res)) {
//remember the call for latter use
self._pendingCalls.push({
sasProgram: sasProgram,
callback: callback,
params: params
});
//there's no need to continue if previous call returned login error
if(self._disableCalls) {
return;
} else {
self._disableCalls = true;
}
callback(new h54sError('notLoggedinError', 'You are not logged in'));
} else {
var resObj, unescapedResObj, err;
if(!dbg) {
var done = false;
try {
resObj = self._utils.parseRes(res.responseText, sasProgram, params);
logs.addApplicationLog(resObj.logmessage, sasProgram);
if(dataObj instanceof Tables) {
unescapedResObj = self._utils.unescapeValues(resObj);
} else {
unescapedResObj = resObj;
}
if(resObj.status !== 'success') {
err = new h54sError('programError', resObj.errormessage, resObj.status);
}
done = true;
} catch(e) {
if(e instanceof SyntaxError) {
if(retryCount < self.maxXhrRetries) {
done = false;
self._ajax.post(self.url, params, self.useMultipartFormData).success(this.success).error(this.error);
retryCount++;
logs.addApplicationLog("Retrying #" + retryCount, sasProgram);
} else {
self._utils.parseErrorResponse(res.responseText, sasProgram);
self._utils.addFailedResponse(res.responseText, sasProgram);
err = new h54sError('parseError', 'Unable to parse response json');
done = true;
}
} else if(e instanceof h54sError) {
self._utils.parseErrorResponse(res.responseText, sasProgram);
self._utils.addFailedResponse(res.responseText, sasProgram);
err = e;
done = true;
} else {
self._utils.parseErrorResponse(res.responseText, sasProgram);
self._utils.addFailedResponse(res.responseText, sasProgram);
err = new h54sError('unknownError', e.message);
err.stack = e.stack;
done = true;
}
} finally {
if(done) {
callback(err, unescapedResObj);
}
}
} else {
try {
resObj = self._utils.parseDebugRes(res.responseText, sasProgram, params);
logs.addApplicationLog(resObj.logmessage, sasProgram);
if(dataObj instanceof Tables) {
unescapedResObj = self._utils.unescapeValues(resObj);
} else {
unescapedResObj = resObj;
}
if(resObj.status !== 'success') {
err = new h54sError('programError', resObj.errormessage, resObj.status);
}
} catch(e) {
if(e instanceof SyntaxError) {
err = new h54sError('parseError', e.message);
} else if(e instanceof h54sError) {
err = e;
} else {
err = new h54sError('unknownError', e.message);
err.stack = e.stack;
}
} finally {
callback(err, unescapedResObj);
}
}
}
}).error(function(res) {
if(res.status === 401) {
callback(new h54sError('notLoggedinError', 'You are not logged in'));
} else {
logs.addApplicationLog('Request failed with status: ' + res.status, sasProgram);
callback(new h54sError('httpError', res.statusText));
}
});
};
/*
* Login method
*
* @param {string} user - Login username
* @param {string} pass - Login password
* @param {function} callback - Callback function called when ajax call is finished
*
* OR
*
* @param {function} callback - Callback function called when ajax call is finished
*
*/
module.exports.login = function(user, pass, callback) {
if(!user || !pass) {
throw new h54sError('argumentError', 'Credentials not set');
}
if(typeof user !== 'string' || typeof pass !== 'string') {
throw new h54sError('argumentError', 'User and pass parameters must be strings');
}
//NOTE: callback optional?
if(!callback || typeof callback !== 'function') {
throw new h54sError('argumentError', 'You must provide callback');
}
if(!this.RESTauth) {
handleSasLogon.call(this, user, pass, callback);
} else {
handleRestLogon.call(this, user, pass, callback);
}
};
function handleSasLogon(user, pass, callback) {
var self = this;
var loginParams = {
_service: 'default',
ux: user,
px: pass,
//for SAS 9.4,
username: user,
password: pass
};
for (var key in this._aditionalLoginParams) {
loginParams[key] = this._aditionalLoginParams[key];
}
this._loginAttempts = 0;
this._ajax.post(this.loginUrl, loginParams).success(function(res) {
if(++self._loginAttempts === 3) {
return callback(-2);
}
if(self._utils.needToLogin.call(self, res)) {
//we are getting form again after redirect
//and need to login again using the new url
//_loginChanged is set in needToLogin function
//but if login url is not different, we are checking if there are aditional parameters
if(self._loginChanged || (self._isNewLoginPage && !self._aditionalLoginParams)) {
delete self._loginChanged;
var inputs = res.responseText.match(/<input.*"hidden"[^>]*>/g);
if(inputs) {
inputs.forEach(function(inputStr) {
var valueMatch = inputStr.match(/name="([^"]*)"\svalue="([^"]*)/);
loginParams[valueMatch[1]] = valueMatch[2];
});
}
var success = this.success, error = this.error;
self._ajax.post(self.loginUrl, loginParams).success(function() {
//we need this get request because of the sas 9.4 security checks
self._ajax.get(self.url).success(success).error(error);
}).error(this.error);
} else {
//getting form again, but it wasn't a redirect
logs.addApplicationLog('Wrong username or password');
callback(-1);
}
} else {
callback(res.status);
self._disableCalls = false;
while(self._pendingCalls.length > 0) {
var pendingCall = self._pendingCalls.shift();
var sasProgram = pendingCall.sasProgram;
var callbackPending = pendingCall.callback;
var params = pendingCall.params;
//update debug because it may change in the meantime
params._debug = self.debug ? 131 : 0;
if(self.retryAfterLogin) {
self.call(sasProgram, null, callbackPending, params);
}
}
}
}).error(function(res) {
logs.addApplicationLog('Login failed with status code: ' + res.status);
callback(res.status);
});
}
function handleRestLogon(user, pass, callback) {
var self = this;
var loginParams = {
username: user,
password: pass
};
this._ajax.post(this.RESTauthLoginUrl, loginParams).success(function(res) {
var location = res.getResponseHeader('Location');
self._ajax.post(location, {
service: self.url
}).success(function(res) {
if(self.url.indexOf('?') === -1) {
self.url += '?ticket=' + res.responseText;
} else {
if(self.url.indexOf('ticket') !== -1) {
self.url = self.url.replace(/ticket=[^&]+/, 'ticket=' + res.responseText);
} else {
self.url += '&ticket=' + res.responseText;
}
}
callback(res.status);
}).error(function(res) {
logs.addApplicationLog('Login failed with status code: ' + res.status);
callback(res.status);
});
}).error(function(res) {
if(res.responseText === 'error.authentication.credentials.bad') {
callback(-1);
} else {
logs.addApplicationLog('Login failed with status code: ' + res.status);
callback(res.status);
}
});
}
/*
* Logout method
*
* @param {function} callback - Callback function called when ajax call is finished
*
*/
module.exports.logout = function(callback) {
this._ajax.get(this.url, {_action: 'logoff'}).success(function(res) {
callback();
}).error(function(res) {
logs.addApplicationLog('Logout failed with status code: ' + res.status);
callback(res.status);
});
};
/*
* Enter debug mode
*
*/
module.exports.setDebugMode = function() {
this.debug = true;
};
/*
* Exit debug mode
*
*/
module.exports.unsetDebugMode = function() {
this.debug = false;
};
for(var key in logs.get) {
if(logs.get.hasOwnProperty(key)) {
module.exports[key] = logs.get[key];
}
}
for(var key in logs.clear) {
if(logs.clear.hasOwnProperty(key)) {
module.exports[key] = logs.clear[key];
}
}
/*
* Add callback functions executed when properties are updated with remote config
*
*@callback - callback pushed to array
*
*/
module.exports.onRemoteConfigUpdate = function(callback) {
this.remoteConfigUpdateCallbacks.push(callback);
};
module.exports._utils = require('./utils.js');