| 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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416 |
1
1
1
1
1
1
753
75
73
2
2
2
2
3
81
81
81
81
63
81
81
45
81
22
22
22
22
22
22
22
22
4
4
22
22
15
15
15
15
15
7
7
7
14
14
7
7
67
1
66
66
66
66
66
66
66
66
66
3
66
1
66
59
59
66
66
66
66
66
66
66
66
66
66
66
1
1
1
1
59
59
59
59
59
59
22
53
53
53
53
53
53
53
53
11
1
1
1
1
1
1
1
1
1
| /* globals window, XDomainRequest, XMLHttpRequest, FormData, FieldDB, document */
"use strict";
var Q;
try {
Q = document.Q || FieldDB.Q;
} catch (exception) {
Eif (!Q) {
Q = require("q");
}
}
var CORS = {
OFFLINE: {
CODE: 600,
MESSAGE: "Unable to contact URL, you appear to be offline."
},
CLIENT_TIMEOUT: {
CODE: 500,
MESSAGE: "The request to URL timed out, please try again. If this happens again, please report this."
},
CONNECTION_ABORTED: {
CODE: 500,
MESSAGE: "The request to URL was aborted by the server, please report this."
},
CONNECTION_ERRORED: {
CODE: 500,
MESSAGE: "Server is not responding for URL, please report this."
},
INTERNAL_SERVER_ERROR: {
CODE: 500
},
CLIENT_ERROR: {
CODE: 400
},
fieldDBtype: "CORS",
timeout: 30 * 1000,
debugMode: false,
debug: function(a, b, c) {
Iif (this.debugMode) {
if (a) {
console.log("CORS-DEBUG: " + a);
}
if (b) {
console.log(b);
}
if (c) {
console.log(c);
}
}
},
warn: function(message, b, c) {
if (this.warnMessage && this.warnMessage.indexOf("message") > -1) {
return;
} else {
this.warnMessage = this.warnMessage + message;
}
console.warn("CORS-WARN: " + message);
Iif (b) {
console.log(b);
}
Iif (c) {
console.log(c);
}
// throw message;
},
bug: function(message) {
console.warn("CORS-BUG: " + message);
},
render: function() {
this.debug("Render requested for CORS, but it doesnt know how to render itself.");
},
clearCookies: function(hostname) {
this.warn("TODO clear cookies requested for " + hostname);
},
preprocess: function(options) {
this.debug("\n\n\npreprocess " + options.url + " " + options.complete);
options.complete = false;
options.percentComplete = 0;
// this.debugMode = true;
if (!options.method) {
options.method = options.type || "GET";
}
Iif (!options.url) {
this.bug("There was an error. Please report this.");
}
if (!options.data) {
options.data = "";
}
Iif (options.method === "GET" && options.data) {
options.dataToSend = JSON.stringify(options.data).replace(/,/g, "&").replace(/:/g, "=").replace(/"/g, "").replace(/[}{]/g, "");
options.url = options.url + "?" + options.dataToSend;
}
},
handleResponse: function(options, deferred) {
Iif (options.complete) {
// options = null;
return;
}
this.debug(options.url + " " + options.complete + ": handleResponse " + options.url);
var err,
status,
attr;
// this.debug("handleResponse", options);
var response = options.xhr.responseJSON || options.xhr.responseText || options.xhr.response;
// this.debug("Response from CORS request to " + options.url + ": " + response);
try {
response = JSON.parse(response);
} catch (e) {}
Iif (!response) {
response = {};
}
if (response.reason) {
response.message = response.reason;
delete response.reason;
}
status = response.status || options.xhr.status;
if (status < 400) {
this.debug("done " + options.url + " " + options.complete + "\n\n\n");
options.complete = true;
options.percentComplete = 100;
deferred.resolve(response);
return;
}
Iif (typeof response !== "object") {
response = {
message: response
};
}
err = new Error(response.userFriendlyErrors ? response.userFriendlyErrors[0] : response.message);
for (attr in response) {
Eif (response.hasOwnProperty(attr)) {
err[attr] = response[attr];
}
}
err.status = status;
this.handleError(options, err, deferred);
},
handleError: function(options, err, deferred) {
if (options.complete) {
// options = null;
return;
}
this.debug(options.url + " " + options.complete + ": handleError " + options.url);
var response;
// this.warn(options.url + " " + options.complete + ": was unsuccesful ");
this.debug(err.stack);
var referrer = "node";
try {
referrer = window.location.href;
} catch(err){
// not running in a browser
}
response = {
userFriendlyErrors: err.userFriendlyErrors || [err.message || "There was an error contacting URL from " + referrer + " the app will not function normally. Please report this."],
status: err.status || 610,
error: err
};
Iif (response.userFriendlyErrors[0] === "missing") {
response.userFriendlyErrors.unshift("The server replied that " + options.url.replace(/\/[^:]+:.+@/i, "/_:_@") + " is missing, please report this.");
} else if (response.userFriendlyErrors[0] === "no_db_file") {
response.userFriendlyErrors.unshift("That database doesn't exist. Are you sure this is the database you wanted to open: " + options.url.replace(/\/[^:]+:.+@/i, "/_:_@"));
}
if (response.status === 401) {
Iif (CORS.application && CORS.application.authentication && CORS.application.authentication.dispatchEvent) {
CORS.application.authentication.dispatchEvent("unauthorized");
}
}
// Tell the user if they are offline
if (response.status >= 500) {
try {
if (window && window.navigator && !window.navigator.onLine) {
response.userFriendlyErrors.unshift(CORS.OFFLINE.MESSAGE);
response.status = CORS.OFFLINE.CODE;
} else if (err && err.srcElement && err.srcElement.status !== undefined) {
response.status = err.srcElement.status;
}
} catch (e) {}
}
response.userFriendlyErrors[0] = response.userFriendlyErrors[0].replace("URL", options.url.replace(/\/[^:]+:.+@/i, "/_:_@"));
this.bug(response.userFriendlyErrors[0]);
response.details = options;
response.details.url = options.url.replace(/\/[^:]+:.+@/i, "/_:_@");
Eif (response.details && response.details.xhr) {
this.debug(options.url + " " + options.complete + ": cleaning up ", options.url);
options.status = options.status || options.xhr.status;
delete options.xhr;
}
this.debug(options.url + " " + options.complete + ": done " + options.url + "\n\n\n");
options.complete = true;
deferred.reject(response);
},
setHeader: function(xhr, key, value) {
xhr.setRequestHeader(key, value);
},
ontimeout: function(options, evt, deferred) {
var err = new Error(this.CLIENT_TIMEOUT.MESSAGE);
this.debug(options.url + " " + options.complete + ": timed out.");
err.status = this.CLIENT_TIMEOUT.CODE;
this.handleError(options, err, deferred);
},
onabort: function(options, evt, deferred) {
var err = new Error(this.CONNECTION_ABORTED.MESSAGE);
this.debug(options.url + " " + options.complete + ": was aborted.");
err.status = this.CONNECTION_ABORTED.CODE;
this.handleError(options, err, deferred);
},
onerror: function(options, evt, deferred) {
var err = new Error(this.CONNECTION_ERRORED.MESSAGE);
this.debug(options.url + " " + options.complete + ": errored.");
Iif (options.xhr && options.xhr.status) {
err.status = options.xhr.status;
} else Iif (options.status) {
err.status = options.status;
} else {
err.status = this.CONNECTION_ERRORED.CODE;
}
this.handleError(options, err, deferred);
},
onload: function(options, evt, deferred) {
this.handleResponse(options, deferred);
},
onprogress: function(options, evt) {
this.debug(options.url + " " + options.complete + ": onprogress " + options.url + " " + options.complete, evt);
Eif (evt.lengthComputable) {
var percentComplete = (evt.loaded / evt.total) * 100;
this.debug(options.url + " " + options.complete + ": percentComplete " + options.url, percentComplete);
console.log("Percent complete " + options.url.replace(/\/[^:]+:.+@/i, "/_:_@") + " : " + percentComplete);
Eif (!options.complete) {
options.percentComplete = percentComplete;
}
}
if (options.onprogress && typeof options.onprogress === "function") {
options.onprogress(evt);
}
}
};
/*
* Helper function which handles IE
*/
CORS.buildXhr = function(options) {
var xhr;
try {
xhr = new XMLHttpRequest();
} catch (e) {
this.warn("XMLHttpRequest is not defined, nothing will happen.", e);
return null;
}
if ("withCredentials" in xhr) {
// XHR for Chrome/Firefox/Opera/Safari.
try {
xhr.open(options.method, options.url, true);
} catch (exception) {
console.log("CORS exception", exception);
this.error = exception.message;
return null;
}
// https://mathiasbynens.be/notes/xhr-responsetype-json
// xhr.responseType = "json";
} else if (typeof XDomainRequest !== "undefined") {
// XDomainRequest for IE.
xhr = new XDomainRequest();
xhr.open(options.method, options.url);
} else {
// CORS not supported.
xhr = null;
}
if (!xhr) {
return;
}
this.setHeader(xhr, "Content-type", "application/json");
if (options.withCredentials !== false) {
xhr.withCredentials = true;
// Dont use CORS if its a file
if (options.url.lastIndexOf(".") > options.url.lastIndexOf("/") && options.url.substring(options.url.lastIndexOf(".")).indexOf("htm") !== 0) {
xhr.withCredentials = false;
}
}
if (options.timeout || this.timeout) {
xhr.timeout = options.timeout || this.timeout;
}
return xhr;
};
/*
* Functions for well formed CORS requests
*/
CORS.makeCORSRequest = function(options) {
var self = this,
deferred = Q.defer(),
data,
xhr;
if (!options || !options.url) {
Q.nextTick(function() {
deferred.reject({
status: 400,
details: options,
userFriendlyErrors: ["Url must be defined"]
});
});
return deferred.promise;
}
this.preprocess(options, deferred);
//forcing production server
options.url = options.url.replace("corpusdev", "corpus");
options.url = options.url.replace("authdev", "auth");
options.url = options.url.replace("localhost:6984", "corpus.lingsync.org");
options.url = options.url.replace("localhost:5984", "corpus.lingsync.org");
xhr = this.buildXhr(options);
if (!xhr) {
var message = this.error || "CORS not supported, your device will be unable to contact " + options.url;
this.bug(message);
Q.nextTick(function() {
deferred.reject({
status: 620,
details: options,
userFriendlyErrors: [message]
});
});
return deferred.promise;
}
// If it contains files, make it into a mulitpart upload
if (options && options.data && options.data.files) {
console.log("converting to formdata ", options.data);
data = new FormData();
for (var part in options.data) {
if (options.data.hasOwnProperty(part)) {
data.append(part, options.data[part]);
}
}
data = data;
this.setHeader(xhr, "Content-Type", "multipart/form-data");
} else {
if (options.data) {
data = JSON.stringify(options.data);
}
}
xhr.onprogress = function(evt) {
self.onprogress(options, evt, deferred);
};
xhr.onload = function(evt) {
self.onload(options, evt, deferred);
};
xhr.onerror = function(evt) {
self.onerror(options, evt, deferred);
};
xhr.onabort = function(evt) {
self.onabort(options, evt, deferred);
};
xhr.ontimeout = function(evt) {
self.ontimeout(options, evt, deferred);
};
options.xhr = xhr;
try {
if (data) {
self.debug(options.url + " " + options.complete + ": sending ", data);
xhr.send(data);
} else {
xhr.send();
}
} catch (e) {
self.warn("Caught an exception when calling send on xhr", e);
e.details = options;
deferred.reject(e);
}
return deferred.promise;
};
try {
exports.CORS = CORS;
} catch (e) {}
try {
if (!document) {}
// console.log(document);
} catch (e) {
var CORSNode = require("./CORSNode").CORS;
console.warn("REST requests: enabled");
exports.CORS = CORSNode || CORS;
}
|