parse 6.1.1 → 7.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -1
- package/dist/parse.js +58626 -26564
- package/dist/parse.min.js +1 -14
- package/dist/parse.weapp.js +58737 -26628
- package/dist/parse.weapp.min.js +1 -14
- package/index.js +1 -1
- package/lib/browser/CoreManager.js +3 -4
- package/lib/browser/ObjectStateMutations.js +51 -9
- package/lib/browser/Parse.js +42 -44
- package/lib/browser/ParseFile.js +74 -54
- package/lib/browser/ParseObject.js +32 -29
- package/lib/browser/ParseOp.js +2 -2
- package/lib/browser/ParseQuery.js +14 -14
- package/lib/browser/RESTController.js +154 -129
- package/lib/browser/SingleInstanceStateController.js +28 -27
- package/lib/browser/UniqueInstanceStateController.js +19 -23
- package/lib/browser/Xhr.weapp.js +62 -102
- package/lib/browser/decode.js +8 -1
- package/lib/browser/encode.js +9 -1
- package/lib/browser/equals.js +1 -1
- package/lib/browser/isDangerousKey.js +29 -0
- package/lib/browser/uuid.js +6 -2
- package/lib/node/CoreManager.js +3 -4
- package/lib/node/ObjectStateMutations.js +50 -9
- package/lib/node/Parse.js +43 -47
- package/lib/node/ParseFile.js +75 -70
- package/lib/node/ParseObject.js +29 -28
- package/lib/node/RESTController.js +152 -128
- package/lib/node/SingleInstanceStateController.js +26 -28
- package/lib/node/UniqueInstanceStateController.js +19 -24
- package/lib/node/Xhr.weapp.js +58 -81
- package/lib/node/decode.js +8 -1
- package/lib/node/encode.js +9 -1
- package/lib/node/equals.js +1 -1
- package/lib/node/isDangerousKey.js +25 -0
- package/lib/node/uuid.js +4 -2
- package/lib/react-native/CoreManager.js +3 -4
- package/lib/react-native/ObjectStateMutations.js +40 -9
- package/lib/react-native/Parse.js +25 -37
- package/lib/react-native/ParseFile.js +62 -50
- package/lib/react-native/ParseObject.js +29 -28
- package/lib/react-native/ParseQuery.js +25 -25
- package/lib/react-native/RESTController.js +186 -150
- package/lib/react-native/SingleInstanceStateController.js +23 -28
- package/lib/react-native/UniqueInstanceStateController.js +19 -24
- package/lib/react-native/Xhr.weapp.js +63 -95
- package/lib/react-native/decode.js +7 -1
- package/lib/react-native/encode.js +7 -1
- package/lib/react-native/equals.js +1 -1
- package/lib/react-native/isDangerousKey.js +17 -0
- package/lib/react-native/uuid.js +1 -2
- package/lib/weapp/CoreManager.js +3 -4
- package/lib/weapp/ObjectStateMutations.js +51 -9
- package/lib/weapp/Parse.js +40 -44
- package/lib/weapp/ParseFile.js +74 -55
- package/lib/weapp/ParseObject.js +32 -29
- package/lib/weapp/ParseOp.js +2 -2
- package/lib/weapp/ParseQuery.js +14 -14
- package/lib/weapp/RESTController.js +155 -130
- package/lib/weapp/SingleInstanceStateController.js +28 -27
- package/lib/weapp/UniqueInstanceStateController.js +19 -23
- package/lib/weapp/Xhr.weapp.js +62 -102
- package/lib/weapp/decode.js +8 -1
- package/lib/weapp/encode.js +9 -1
- package/lib/weapp/equals.js +1 -1
- package/lib/weapp/isDangerousKey.js +29 -0
- package/lib/weapp/uuid.js +2 -1
- package/node.js +1 -1
- package/package.json +38 -55
- package/react-native.js +1 -1
- package/types/Parse.d.ts +16 -10
- package/types/ParseFile.d.ts +20 -5
- package/types/ParseObject.d.ts +1 -1
- package/types/RESTController.d.ts +2 -7
- package/types/SingleInstanceStateController.d.ts +3 -3
- package/types/Xhr.weapp.d.ts +1 -29
- package/types/isDangerousKey.d.ts +7 -0
- package/types/tests.ts +2 -0
- package/weapp.js +1 -1
package/lib/node/ParseFile.js
CHANGED
|
@@ -3,21 +3,17 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
+
exports.b64Digit = b64Digit;
|
|
6
7
|
exports.default = void 0;
|
|
7
8
|
var _CoreManager = _interopRequireDefault(require("./CoreManager"));
|
|
8
9
|
var _ParseError = _interopRequireDefault(require("./ParseError"));
|
|
9
|
-
var _Xhr = _interopRequireDefault(require("./Xhr.weapp"));
|
|
10
10
|
function _interopRequireDefault(e) {
|
|
11
11
|
return e && e.__esModule ? e : {
|
|
12
12
|
default: e
|
|
13
13
|
};
|
|
14
14
|
}
|
|
15
|
-
/* global
|
|
15
|
+
/* global Blob */
|
|
16
16
|
|
|
17
|
-
let XHR = null;
|
|
18
|
-
if (typeof XMLHttpRequest !== 'undefined') {
|
|
19
|
-
XHR = XMLHttpRequest;
|
|
20
|
-
}
|
|
21
17
|
function b64Digit(number) {
|
|
22
18
|
if (number < 26) {
|
|
23
19
|
return String.fromCharCode(65 + number);
|
|
@@ -50,7 +46,7 @@ class ParseFile {
|
|
|
50
46
|
* an alphanumeric character, and consist of alphanumeric characters,
|
|
51
47
|
* periods, spaces, underscores, or dashes.
|
|
52
48
|
* @param data {Array} The data for the file, as either:
|
|
53
|
-
* 1. an Array of byte value Numbers
|
|
49
|
+
* 1. an Array of byte value Numbers or Uint8Array.
|
|
54
50
|
* 2. an Object like { base64: "..." } with a base64-encoded String.
|
|
55
51
|
* 3. an Object like { uri: "..." } with a uri String.
|
|
56
52
|
* 4. a File object selected with a file upload control. (3) only works
|
|
@@ -80,7 +76,7 @@ class ParseFile {
|
|
|
80
76
|
this._metadata = metadata || {};
|
|
81
77
|
this._tags = tags || {};
|
|
82
78
|
if (data !== undefined) {
|
|
83
|
-
if (Array.isArray(data)) {
|
|
79
|
+
if (Array.isArray(data) || data instanceof Uint8Array) {
|
|
84
80
|
this._data = ParseFile.encodeBase64(data);
|
|
85
81
|
this._source = {
|
|
86
82
|
format: 'base64',
|
|
@@ -119,19 +115,31 @@ class ParseFile {
|
|
|
119
115
|
* Data is present if initialized with Byte Array, Base64 or Saved with Uri.
|
|
120
116
|
* Data is cleared if saved with File object selected with a file upload control
|
|
121
117
|
*
|
|
118
|
+
* @param {object} options
|
|
119
|
+
* @param {function} [options.progress] callback for download progress
|
|
120
|
+
* <pre>
|
|
121
|
+
* const parseFile = new Parse.File(name, file);
|
|
122
|
+
* parseFile.getData({
|
|
123
|
+
* progress: (progressValue, loaded, total) => {
|
|
124
|
+
* if (progressValue !== null) {
|
|
125
|
+
* // Update the UI using progressValue
|
|
126
|
+
* }
|
|
127
|
+
* }
|
|
128
|
+
* });
|
|
129
|
+
* </pre>
|
|
122
130
|
* @returns {Promise} Promise that is resolve with base64 data
|
|
123
131
|
*/
|
|
124
|
-
async getData() {
|
|
132
|
+
async getData(options) {
|
|
133
|
+
options = options || {};
|
|
125
134
|
if (this._data) {
|
|
126
135
|
return this._data;
|
|
127
136
|
}
|
|
128
137
|
if (!this._url) {
|
|
129
138
|
throw new Error('Cannot retrieve data for unsaved ParseFile.');
|
|
130
139
|
}
|
|
140
|
+
options.requestTask = task => this._requestTask = task;
|
|
131
141
|
const controller = _CoreManager.default.getFileController();
|
|
132
|
-
const result = await controller.download(this._url,
|
|
133
|
-
requestTask: task => this._requestTask = task
|
|
134
|
-
});
|
|
142
|
+
const result = await controller.download(this._url, options);
|
|
135
143
|
this._data = result.base64;
|
|
136
144
|
return this._data;
|
|
137
145
|
}
|
|
@@ -194,12 +202,12 @@ class ParseFile {
|
|
|
194
202
|
* be used for this request.
|
|
195
203
|
* <li>sessionToken: A valid session token, used for making a request on
|
|
196
204
|
* behalf of a specific user.
|
|
197
|
-
* <li>progress:
|
|
205
|
+
* <li>progress: callback for upload progress. For example:
|
|
198
206
|
* <pre>
|
|
199
207
|
* let parseFile = new Parse.File(name, file);
|
|
200
208
|
* parseFile.save({
|
|
201
|
-
* progress: (progressValue, loaded, total
|
|
202
|
-
* if (
|
|
209
|
+
* progress: (progressValue, loaded, total) => {
|
|
210
|
+
* if (progressValue !== null) {
|
|
203
211
|
* // Update the UI using progressValue
|
|
204
212
|
* }
|
|
205
213
|
* }
|
|
@@ -427,55 +435,59 @@ const DefaultController = {
|
|
|
427
435
|
}
|
|
428
436
|
return _CoreManager.default.getRESTController().request('POST', 'files/' + name, data, options);
|
|
429
437
|
},
|
|
430
|
-
download: function (uri, options) {
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
resp.on('data', data => base64 += data);
|
|
440
|
-
resp.on('end', () => {
|
|
441
|
-
resolve({
|
|
442
|
-
base64,
|
|
443
|
-
contentType: resp.headers['content-type']
|
|
444
|
-
});
|
|
445
|
-
});
|
|
446
|
-
});
|
|
447
|
-
req.on('abort', () => {
|
|
448
|
-
resolve({});
|
|
449
|
-
});
|
|
450
|
-
req.on('error', reject);
|
|
451
|
-
options.requestTask(req);
|
|
438
|
+
download: async function (uri, options) {
|
|
439
|
+
const controller = new AbortController();
|
|
440
|
+
options.requestTask(controller);
|
|
441
|
+
const {
|
|
442
|
+
signal
|
|
443
|
+
} = controller;
|
|
444
|
+
try {
|
|
445
|
+
const response = await fetch(uri, {
|
|
446
|
+
signal
|
|
452
447
|
});
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
448
|
+
const reader = response.body.getReader();
|
|
449
|
+
const length = +response.headers.get('Content-Length') || 0;
|
|
450
|
+
const contentType = response.headers.get('Content-Type');
|
|
451
|
+
if (length === 0) {
|
|
452
|
+
var _options$progress;
|
|
453
|
+
(_options$progress = options.progress) === null || _options$progress === void 0 || _options$progress.call(options, null, null, null);
|
|
454
|
+
return {
|
|
455
|
+
base64: '',
|
|
456
|
+
contentType
|
|
457
|
+
};
|
|
458
|
+
}
|
|
459
|
+
let recieved = 0;
|
|
460
|
+
const chunks = [];
|
|
461
|
+
while (true) {
|
|
462
|
+
var _options$progress2;
|
|
463
|
+
const {
|
|
464
|
+
done,
|
|
465
|
+
value
|
|
466
|
+
} = await reader.read();
|
|
467
|
+
if (done) {
|
|
468
|
+
break;
|
|
469
469
|
}
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
470
|
+
chunks.push(value);
|
|
471
|
+
recieved += (value === null || value === void 0 ? void 0 : value.length) || 0;
|
|
472
|
+
(_options$progress2 = options.progress) === null || _options$progress2 === void 0 || _options$progress2.call(options, recieved / length, recieved, length);
|
|
473
|
+
}
|
|
474
|
+
const body = new Uint8Array(recieved);
|
|
475
|
+
let offset = 0;
|
|
476
|
+
for (const chunk of chunks) {
|
|
477
|
+
body.set(chunk, offset);
|
|
478
|
+
offset += chunk.length;
|
|
479
|
+
}
|
|
480
|
+
return {
|
|
481
|
+
base64: ParseFile.encodeBase64(body),
|
|
482
|
+
contentType
|
|
475
483
|
};
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
484
|
+
} catch (error) {
|
|
485
|
+
if (error.name === 'AbortError') {
|
|
486
|
+
return {};
|
|
487
|
+
} else {
|
|
488
|
+
throw error;
|
|
489
|
+
}
|
|
490
|
+
}
|
|
479
491
|
},
|
|
480
492
|
deleteFile: function (name, options) {
|
|
481
493
|
const headers = {
|
|
@@ -491,20 +503,13 @@ const DefaultController = {
|
|
|
491
503
|
url += 'files/' + name;
|
|
492
504
|
return _CoreManager.default.getRESTController().ajax('DELETE', url, '', headers).catch(response => {
|
|
493
505
|
// TODO: return JSON object in server
|
|
494
|
-
if (!response || response === 'SyntaxError: Unexpected end of JSON input') {
|
|
506
|
+
if (!response || response.toString() === 'SyntaxError: Unexpected end of JSON input') {
|
|
495
507
|
return Promise.resolve();
|
|
496
508
|
} else {
|
|
497
509
|
return _CoreManager.default.getRESTController().handleError(response);
|
|
498
510
|
}
|
|
499
511
|
});
|
|
500
|
-
},
|
|
501
|
-
_setXHR(xhr) {
|
|
502
|
-
XHR = xhr;
|
|
503
|
-
},
|
|
504
|
-
_getXHR() {
|
|
505
|
-
return XHR;
|
|
506
512
|
}
|
|
507
513
|
};
|
|
508
514
|
_CoreManager.default.setFileController(DefaultController);
|
|
509
|
-
var _default = exports.default = ParseFile;
|
|
510
|
-
exports.b64Digit = b64Digit;
|
|
515
|
+
var _default = exports.default = ParseFile;
|
package/lib/node/ParseObject.js
CHANGED
|
@@ -22,30 +22,25 @@ var SingleInstanceStateController = _interopRequireWildcard(require("./SingleIns
|
|
|
22
22
|
var _unique = _interopRequireDefault(require("./unique"));
|
|
23
23
|
var UniqueInstanceStateController = _interopRequireWildcard(require("./UniqueInstanceStateController"));
|
|
24
24
|
var _unsavedChildren = _interopRequireDefault(require("./unsavedChildren"));
|
|
25
|
-
function
|
|
26
|
-
if ("function"
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) {
|
|
45
|
-
var i = a ? Object.getOwnPropertyDescriptor(e, u) : null;
|
|
46
|
-
i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u];
|
|
47
|
-
}
|
|
48
|
-
return n.default = e, t && t.set(e, n), n;
|
|
25
|
+
function _interopRequireWildcard(e, t) {
|
|
26
|
+
if ("function" == typeof WeakMap) var r = new WeakMap(),
|
|
27
|
+
n = new WeakMap();
|
|
28
|
+
return (_interopRequireWildcard = function (e, t) {
|
|
29
|
+
if (!t && e && e.__esModule) return e;
|
|
30
|
+
var o,
|
|
31
|
+
i,
|
|
32
|
+
f = {
|
|
33
|
+
__proto__: null,
|
|
34
|
+
default: e
|
|
35
|
+
};
|
|
36
|
+
if (null === e || "object" != typeof e && "function" != typeof e) return f;
|
|
37
|
+
if (o = t ? n : r) {
|
|
38
|
+
if (o.has(e)) return o.get(e);
|
|
39
|
+
o.set(e, f);
|
|
40
|
+
}
|
|
41
|
+
for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]);
|
|
42
|
+
return f;
|
|
43
|
+
})(e, t);
|
|
49
44
|
}
|
|
50
45
|
function _interopRequireDefault(e) {
|
|
51
46
|
return e && e.__esModule ? e : {
|
|
@@ -54,7 +49,7 @@ function _interopRequireDefault(e) {
|
|
|
54
49
|
}
|
|
55
50
|
// Mapping of class names to constructors, so we can populate objects from the
|
|
56
51
|
// server with appropriate subclasses of ParseObject
|
|
57
|
-
const classMap =
|
|
52
|
+
const classMap = Object.create(null);
|
|
58
53
|
|
|
59
54
|
// Global counter for generating unique Ids for non-single-instance objects
|
|
60
55
|
let objectCount = 0;
|
|
@@ -132,6 +127,11 @@ class ParseObject {
|
|
|
132
127
|
throw new Error("Can't create an invalid Parse Object");
|
|
133
128
|
}
|
|
134
129
|
}
|
|
130
|
+
if (_CoreManager.default.get('NODE_LOGGING')) {
|
|
131
|
+
this[Symbol.for('nodejs.util.inspect.custom')] = function () {
|
|
132
|
+
return `ParseObject: className: ${this.className}, id: ${this.id}\nAttributes: ${JSON.stringify(this.attributes, null, 2)}`;
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
135
|
}
|
|
136
136
|
|
|
137
137
|
/**
|
|
@@ -417,6 +417,9 @@ class ParseObject {
|
|
|
417
417
|
}
|
|
418
418
|
static _getRequestOptions(options = {}) {
|
|
419
419
|
const requestOptions = {};
|
|
420
|
+
if (Object.prototype.toString.call(options) !== '[object Object]') {
|
|
421
|
+
throw new Error('request options must be of type Object');
|
|
422
|
+
}
|
|
420
423
|
const {
|
|
421
424
|
hasOwn
|
|
422
425
|
} = Object;
|
|
@@ -903,7 +906,7 @@ class ParseObject {
|
|
|
903
906
|
*/
|
|
904
907
|
op(attr) {
|
|
905
908
|
const pending = this._getPendingOps();
|
|
906
|
-
for (let i = pending.length; i
|
|
909
|
+
for (let i = 0; i < pending.length; i += 1) {
|
|
907
910
|
if (pending[i][attr]) {
|
|
908
911
|
return pending[i][attr];
|
|
909
912
|
}
|
|
@@ -2337,7 +2340,6 @@ const DefaultController = {
|
|
|
2337
2340
|
const status = responses[index]._status;
|
|
2338
2341
|
delete responses[index]._status;
|
|
2339
2342
|
delete responses[index]._headers;
|
|
2340
|
-
delete responses[index]._xhr;
|
|
2341
2343
|
mapIdForPin[objectId] = obj._localId;
|
|
2342
2344
|
obj._handleSaveResponse(responses[index].success, status);
|
|
2343
2345
|
} else {
|
|
@@ -2399,7 +2401,6 @@ const DefaultController = {
|
|
|
2399
2401
|
const status = response._status;
|
|
2400
2402
|
delete response._status;
|
|
2401
2403
|
delete response._headers;
|
|
2402
|
-
delete response._xhr;
|
|
2403
2404
|
targetCopy._handleSaveResponse(response, status);
|
|
2404
2405
|
}, error => {
|
|
2405
2406
|
targetCopy._handleSaveError();
|
|
@@ -8,7 +8,7 @@ var _uuid = _interopRequireDefault(require("./uuid"));
|
|
|
8
8
|
var _CoreManager = _interopRequireDefault(require("./CoreManager"));
|
|
9
9
|
var _ParseError = _interopRequireDefault(require("./ParseError"));
|
|
10
10
|
var _promiseUtils = require("./promiseUtils");
|
|
11
|
-
var _Xhr =
|
|
11
|
+
var _Xhr = require("./Xhr.weapp");
|
|
12
12
|
function _interopRequireDefault(e) {
|
|
13
13
|
return e && e.__esModule ? e : {
|
|
14
14
|
default: e
|
|
@@ -16,16 +16,20 @@ function _interopRequireDefault(e) {
|
|
|
16
16
|
}
|
|
17
17
|
/* global XMLHttpRequest, XDomainRequest */
|
|
18
18
|
|
|
19
|
-
let XHR = null;
|
|
20
|
-
if (typeof XMLHttpRequest !== 'undefined') {
|
|
21
|
-
XHR = XMLHttpRequest;
|
|
22
|
-
}
|
|
23
|
-
XHR = require('xmlhttprequest').XMLHttpRequest;
|
|
24
19
|
let useXDomainRequest = false;
|
|
25
20
|
// @ts-ignore
|
|
26
21
|
if (typeof XDomainRequest !== 'undefined' && !('withCredentials' in new XMLHttpRequest())) {
|
|
27
22
|
useXDomainRequest = true;
|
|
28
23
|
}
|
|
24
|
+
function getPath(base, pathname) {
|
|
25
|
+
if (base.endsWith('/')) {
|
|
26
|
+
base = base.slice(0, -1);
|
|
27
|
+
}
|
|
28
|
+
if (!pathname.startsWith('/')) {
|
|
29
|
+
pathname = '/' + pathname;
|
|
30
|
+
}
|
|
31
|
+
return base + pathname;
|
|
32
|
+
}
|
|
29
33
|
function ajaxIE9(method, url, data, _headers, options) {
|
|
30
34
|
return new Promise((resolve, reject) => {
|
|
31
35
|
// @ts-ignore
|
|
@@ -68,66 +72,22 @@ function ajaxIE9(method, url, data, _headers, options) {
|
|
|
68
72
|
});
|
|
69
73
|
}
|
|
70
74
|
const RESTController = {
|
|
71
|
-
ajax(method, url, data, headers, options) {
|
|
75
|
+
async ajax(method, url, data, headers, options) {
|
|
72
76
|
if (useXDomainRequest) {
|
|
73
77
|
return ajaxIE9(method, url, data, headers, options);
|
|
74
78
|
}
|
|
79
|
+
if (typeof fetch !== 'function') {
|
|
80
|
+
throw new Error('Cannot make a request: Fetch API not found.');
|
|
81
|
+
}
|
|
75
82
|
const promise = (0, _promiseUtils.resolvingPromise)();
|
|
76
83
|
const isIdempotent = _CoreManager.default.get('IDEMPOTENCY') && ['POST', 'PUT'].includes(method);
|
|
77
84
|
const requestId = isIdempotent ? (0, _uuid.default)() : '';
|
|
78
85
|
let attempts = 0;
|
|
79
|
-
const dispatch = function () {
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
const xhr = new XHR();
|
|
85
|
-
xhr.onreadystatechange = function () {
|
|
86
|
-
if (xhr.readyState !== 4 || handled || xhr._aborted) {
|
|
87
|
-
return;
|
|
88
|
-
}
|
|
89
|
-
handled = true;
|
|
90
|
-
if (xhr.status >= 200 && xhr.status < 300) {
|
|
91
|
-
let response;
|
|
92
|
-
try {
|
|
93
|
-
response = JSON.parse(xhr.responseText);
|
|
94
|
-
const availableHeaders = typeof xhr.getAllResponseHeaders === 'function' ? xhr.getAllResponseHeaders() : '';
|
|
95
|
-
headers = {};
|
|
96
|
-
if (typeof xhr.getResponseHeader === 'function' && (availableHeaders === null || availableHeaders === void 0 ? void 0 : availableHeaders.indexOf('access-control-expose-headers')) >= 0) {
|
|
97
|
-
const responseHeaders = xhr.getResponseHeader('access-control-expose-headers').split(', ');
|
|
98
|
-
responseHeaders.forEach(header => {
|
|
99
|
-
if (availableHeaders.indexOf(header.toLowerCase()) >= 0) {
|
|
100
|
-
headers[header] = xhr.getResponseHeader(header.toLowerCase());
|
|
101
|
-
}
|
|
102
|
-
});
|
|
103
|
-
}
|
|
104
|
-
} catch (e) {
|
|
105
|
-
promise.reject(e.toString());
|
|
106
|
-
}
|
|
107
|
-
if (response) {
|
|
108
|
-
promise.resolve({
|
|
109
|
-
response,
|
|
110
|
-
headers,
|
|
111
|
-
status: xhr.status,
|
|
112
|
-
xhr
|
|
113
|
-
});
|
|
114
|
-
}
|
|
115
|
-
} else if (xhr.status >= 500 || xhr.status === 0) {
|
|
116
|
-
// retry on 5XX or node-xmlhttprequest error
|
|
117
|
-
if (++attempts < _CoreManager.default.get('REQUEST_ATTEMPT_LIMIT')) {
|
|
118
|
-
// Exponentially-growing random delay
|
|
119
|
-
const delay = Math.round(Math.random() * 125 * Math.pow(2, attempts));
|
|
120
|
-
setTimeout(dispatch, delay);
|
|
121
|
-
} else if (xhr.status === 0) {
|
|
122
|
-
promise.reject('Unable to connect to the Parse API');
|
|
123
|
-
} else {
|
|
124
|
-
// After the retry limit is reached, fail
|
|
125
|
-
promise.reject(xhr);
|
|
126
|
-
}
|
|
127
|
-
} else {
|
|
128
|
-
promise.reject(xhr);
|
|
129
|
-
}
|
|
130
|
-
};
|
|
86
|
+
const dispatch = async function () {
|
|
87
|
+
const controller = new AbortController();
|
|
88
|
+
const {
|
|
89
|
+
signal
|
|
90
|
+
} = controller;
|
|
131
91
|
headers = headers || {};
|
|
132
92
|
if (typeof headers['Content-Type'] !== 'string') {
|
|
133
93
|
headers['Content-Type'] = 'text/plain'; // Avoid pre-flight
|
|
@@ -138,52 +98,117 @@ const RESTController = {
|
|
|
138
98
|
if (isIdempotent) {
|
|
139
99
|
headers['X-Parse-Request-Id'] = requestId;
|
|
140
100
|
}
|
|
141
|
-
if (_CoreManager.default.get('SERVER_AUTH_TYPE') && _CoreManager.default.get('SERVER_AUTH_TOKEN')) {
|
|
142
|
-
headers['Authorization'] = _CoreManager.default.get('SERVER_AUTH_TYPE') + ' ' + _CoreManager.default.get('SERVER_AUTH_TOKEN');
|
|
143
|
-
}
|
|
144
101
|
const customHeaders = _CoreManager.default.get('REQUEST_HEADERS');
|
|
145
102
|
for (const key in customHeaders) {
|
|
146
103
|
headers[key] = customHeaders[key];
|
|
147
104
|
}
|
|
148
|
-
if (options && typeof options.progress === 'function') {
|
|
149
|
-
const handleProgress = function (type, event) {
|
|
150
|
-
if (event.lengthComputable) {
|
|
151
|
-
options.progress(event.loaded / event.total, event.loaded, event.total, {
|
|
152
|
-
type
|
|
153
|
-
});
|
|
154
|
-
} else {
|
|
155
|
-
options.progress(null, null, null, {
|
|
156
|
-
type
|
|
157
|
-
});
|
|
158
|
-
}
|
|
159
|
-
};
|
|
160
|
-
xhr.onprogress = event => {
|
|
161
|
-
handleProgress('download', event);
|
|
162
|
-
};
|
|
163
|
-
if (xhr.upload) {
|
|
164
|
-
xhr.upload.onprogress = event => {
|
|
165
|
-
handleProgress('upload', event);
|
|
166
|
-
};
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
xhr.open(method, url, true);
|
|
170
|
-
for (const h in headers) {
|
|
171
|
-
xhr.setRequestHeader(h, headers[h]);
|
|
172
|
-
}
|
|
173
|
-
xhr.onabort = function () {
|
|
174
|
-
promise.resolve({
|
|
175
|
-
response: {
|
|
176
|
-
results: []
|
|
177
|
-
},
|
|
178
|
-
status: 0,
|
|
179
|
-
xhr
|
|
180
|
-
});
|
|
181
|
-
};
|
|
182
|
-
xhr.send(data);
|
|
183
105
|
// @ts-ignore
|
|
184
106
|
if (options && typeof options.requestTask === 'function') {
|
|
185
107
|
// @ts-ignore
|
|
186
|
-
options.requestTask(
|
|
108
|
+
options.requestTask(controller);
|
|
109
|
+
}
|
|
110
|
+
try {
|
|
111
|
+
const fetchOptions = {
|
|
112
|
+
method,
|
|
113
|
+
headers,
|
|
114
|
+
signal,
|
|
115
|
+
redirect: 'manual'
|
|
116
|
+
};
|
|
117
|
+
if (data) {
|
|
118
|
+
fetchOptions.body = data;
|
|
119
|
+
}
|
|
120
|
+
const response = await fetch(url, fetchOptions);
|
|
121
|
+
const {
|
|
122
|
+
status
|
|
123
|
+
} = response;
|
|
124
|
+
if (status >= 200 && status < 300) {
|
|
125
|
+
let result;
|
|
126
|
+
const responseHeaders = {};
|
|
127
|
+
const availableHeaders = response.headers.get('access-control-expose-headers') || '';
|
|
128
|
+
availableHeaders.split(', ').forEach(header => {
|
|
129
|
+
if (header && response.headers.has(header)) {
|
|
130
|
+
responseHeaders[header] = response.headers.get(header);
|
|
131
|
+
}
|
|
132
|
+
});
|
|
133
|
+
if (options && typeof options.progress === 'function' && response.body) {
|
|
134
|
+
const reader = response.body.getReader();
|
|
135
|
+
const length = +response.headers.get('Content-Length') || 0;
|
|
136
|
+
if (length === 0) {
|
|
137
|
+
options.progress(null, null, null);
|
|
138
|
+
result = await response.json();
|
|
139
|
+
} else {
|
|
140
|
+
let recieved = 0;
|
|
141
|
+
const chunks = [];
|
|
142
|
+
while (true) {
|
|
143
|
+
const {
|
|
144
|
+
done,
|
|
145
|
+
value
|
|
146
|
+
} = await reader.read();
|
|
147
|
+
if (done) {
|
|
148
|
+
break;
|
|
149
|
+
}
|
|
150
|
+
chunks.push(value);
|
|
151
|
+
recieved += (value === null || value === void 0 ? void 0 : value.length) || 0;
|
|
152
|
+
options.progress(recieved / length, recieved, length);
|
|
153
|
+
}
|
|
154
|
+
const body = new Uint8Array(recieved);
|
|
155
|
+
let offset = 0;
|
|
156
|
+
for (const chunk of chunks) {
|
|
157
|
+
body.set(chunk, offset);
|
|
158
|
+
offset += chunk.length;
|
|
159
|
+
}
|
|
160
|
+
const jsonString = new TextDecoder().decode(body);
|
|
161
|
+
result = JSON.parse(jsonString);
|
|
162
|
+
}
|
|
163
|
+
} else {
|
|
164
|
+
result = await response.json();
|
|
165
|
+
}
|
|
166
|
+
promise.resolve({
|
|
167
|
+
status,
|
|
168
|
+
response: result,
|
|
169
|
+
headers: responseHeaders
|
|
170
|
+
});
|
|
171
|
+
} else if (status >= 400 && status < 500) {
|
|
172
|
+
const error = await response.json();
|
|
173
|
+
promise.reject(error);
|
|
174
|
+
} else if ([301, 302, 303, 307, 308].includes(status)) {
|
|
175
|
+
const location = response.headers.get('location');
|
|
176
|
+
promise.resolve({
|
|
177
|
+
status,
|
|
178
|
+
location,
|
|
179
|
+
method: status === 303 ? 'GET' : method,
|
|
180
|
+
dropBody: status === 303
|
|
181
|
+
});
|
|
182
|
+
} else if (status >= 500 || status === 0) {
|
|
183
|
+
// retry on 5XX or library error
|
|
184
|
+
if (++attempts < _CoreManager.default.get('REQUEST_ATTEMPT_LIMIT')) {
|
|
185
|
+
// Exponentially-growing random delay
|
|
186
|
+
const delay = Math.round(Math.random() * 125 * Math.pow(2, attempts));
|
|
187
|
+
setTimeout(dispatch, delay);
|
|
188
|
+
} else if (status === 0) {
|
|
189
|
+
promise.reject('Unable to connect to the Parse API');
|
|
190
|
+
} else {
|
|
191
|
+
// After the retry limit is reached, fail
|
|
192
|
+
const error = await response.json();
|
|
193
|
+
promise.reject(error);
|
|
194
|
+
}
|
|
195
|
+
} else {
|
|
196
|
+
promise.reject(response);
|
|
197
|
+
}
|
|
198
|
+
} catch (error) {
|
|
199
|
+
var _error$cause;
|
|
200
|
+
if (error.name === 'AbortError') {
|
|
201
|
+
promise.resolve({
|
|
202
|
+
response: {
|
|
203
|
+
results: []
|
|
204
|
+
},
|
|
205
|
+
status: 0
|
|
206
|
+
});
|
|
207
|
+
} else if (((_error$cause = error.cause) === null || _error$cause === void 0 ? void 0 : _error$cause.code) === 'ECONNREFUSED') {
|
|
208
|
+
promise.reject('Unable to connect to the Parse API');
|
|
209
|
+
} else {
|
|
210
|
+
promise.reject(error);
|
|
211
|
+
}
|
|
187
212
|
}
|
|
188
213
|
};
|
|
189
214
|
dispatch();
|
|
@@ -191,11 +216,7 @@ const RESTController = {
|
|
|
191
216
|
},
|
|
192
217
|
request(method, path, data, options) {
|
|
193
218
|
options = options || {};
|
|
194
|
-
|
|
195
|
-
if (url[url.length - 1] !== '/') {
|
|
196
|
-
url += '/';
|
|
197
|
-
}
|
|
198
|
-
url += path;
|
|
219
|
+
const url = getPath(_CoreManager.default.get('SERVER_URL'), path);
|
|
199
220
|
const payload = {};
|
|
200
221
|
if (data && typeof data === 'object') {
|
|
201
222
|
for (const k in data) {
|
|
@@ -263,18 +284,34 @@ const RESTController = {
|
|
|
263
284
|
payload._SessionToken = token;
|
|
264
285
|
}
|
|
265
286
|
const payloadString = JSON.stringify(payload);
|
|
266
|
-
return RESTController.ajax(method, url, payloadString, {}, options).then(
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
287
|
+
return RESTController.ajax(method, url, payloadString, {}, options).then(async result => {
|
|
288
|
+
if (result.location) {
|
|
289
|
+
let newURL = getPath(result.location, path);
|
|
290
|
+
let newMethod = result.method;
|
|
291
|
+
let newBody = result.dropBody ? undefined : payloadString;
|
|
292
|
+
|
|
293
|
+
// Follow up to 5 redirects to avoid loops
|
|
294
|
+
for (let i = 0; i < 5; i += 1) {
|
|
295
|
+
const r = await RESTController.ajax(newMethod, newURL, newBody, {}, options);
|
|
296
|
+
if (!r.location) {
|
|
297
|
+
result = r;
|
|
298
|
+
break;
|
|
299
|
+
}
|
|
300
|
+
newURL = getPath(r.location, path);
|
|
301
|
+
newMethod = r.method;
|
|
302
|
+
newBody = r.dropBody ? undefined : payloadString;
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
const {
|
|
306
|
+
response,
|
|
307
|
+
status,
|
|
308
|
+
headers
|
|
309
|
+
} = result;
|
|
272
310
|
if (options.returnStatus) {
|
|
273
311
|
return {
|
|
274
312
|
...response,
|
|
275
313
|
_status: status,
|
|
276
|
-
_headers: headers
|
|
277
|
-
_xhr: xhr
|
|
314
|
+
_headers: headers
|
|
278
315
|
};
|
|
279
316
|
} else {
|
|
280
317
|
return response;
|
|
@@ -282,29 +319,16 @@ const RESTController = {
|
|
|
282
319
|
});
|
|
283
320
|
}).catch(RESTController.handleError);
|
|
284
321
|
},
|
|
285
|
-
handleError(
|
|
322
|
+
handleError(errorJSON) {
|
|
286
323
|
// Transform the error into an instance of ParseError by trying to parse
|
|
287
324
|
// the error string as JSON
|
|
288
325
|
let error;
|
|
289
|
-
if (
|
|
290
|
-
|
|
291
|
-
const errorJSON = JSON.parse(response.responseText);
|
|
292
|
-
error = new _ParseError.default(errorJSON.code, errorJSON.error);
|
|
293
|
-
} catch (_) {
|
|
294
|
-
// If we fail to parse the error text, that's okay.
|
|
295
|
-
error = new _ParseError.default(_ParseError.default.INVALID_JSON, 'Received an error with invalid JSON from Parse: ' + response.responseText);
|
|
296
|
-
}
|
|
326
|
+
if (errorJSON.code || errorJSON.error || errorJSON.message) {
|
|
327
|
+
error = new _ParseError.default(errorJSON.code, errorJSON.error || errorJSON.message);
|
|
297
328
|
} else {
|
|
298
|
-
|
|
299
|
-
error = new _ParseError.default(_ParseError.default.CONNECTION_FAILED, 'XMLHttpRequest failed: ' + JSON.stringify(message));
|
|
329
|
+
error = new _ParseError.default(_ParseError.default.CONNECTION_FAILED, 'XMLHttpRequest failed: ' + JSON.stringify(errorJSON));
|
|
300
330
|
}
|
|
301
331
|
return Promise.reject(error);
|
|
302
|
-
},
|
|
303
|
-
_setXHR(xhr) {
|
|
304
|
-
XHR = xhr;
|
|
305
|
-
},
|
|
306
|
-
_getXHR() {
|
|
307
|
-
return XHR;
|
|
308
332
|
}
|
|
309
333
|
};
|
|
310
334
|
var _default = exports.default = RESTController;
|