parse 8.1.1-alpha.5 → 8.2.0-alpha.1

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.
@@ -18,7 +18,7 @@ const config = {
18
18
  SERVER_URL: 'https://api.parse.com/1',
19
19
  LIVEQUERY_SERVER_URL: null,
20
20
  ENCRYPTED_KEY: null,
21
- VERSION: 'js' + "8.1.1-alpha.5",
21
+ VERSION: 'js' + "8.2.0-alpha.1",
22
22
  APPLICATION_ID: null,
23
23
  JAVASCRIPT_KEY: null,
24
24
  MAINTENANCE_KEY: null,
@@ -16,8 +16,8 @@ exports.b64Digit = b64Digit;
16
16
  exports.default = void 0;
17
17
  var _isArray = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/array/is-array"));
18
18
  var _slice = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/slice"));
19
- var _forEach = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/for-each"));
20
19
  var _keys = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/object/keys"));
20
+ var _forEach = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/for-each"));
21
21
  var _promise = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/promise"));
22
22
  var _defineProperty2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/defineProperty"));
23
23
  var _CoreManager = _interopRequireDefault(require("./CoreManager"));
@@ -44,6 +44,7 @@ function _objectSpread(e) {
44
44
  }
45
45
  return e;
46
46
  } /* global Blob */
47
+ let NodeReadable;
47
48
  function b64Digit(number) {
48
49
  if (number < 26) {
49
50
  return String.fromCharCode(65 + number);
@@ -79,8 +80,13 @@ class ParseFile {
79
80
  * 1. an Array of byte value Numbers or Uint8Array.
80
81
  * 2. an Object like { base64: "..." } with a base64-encoded String.
81
82
  * 3. an Object like { uri: "..." } with a uri String.
82
- * 4. a File object selected with a file upload control. (3) only works
83
- * in Firefox 3.6+, Safari 6.0.2+, Chrome 7+, and IE 10+.
83
+ * 4. a File object selected with a file upload control.
84
+ * 5. (Node.js only) a Buffer. Uploaded as raw binary data instead of
85
+ * base64-encoding, reducing memory usage. Falls back to base64
86
+ * JSON encoding if metadata or tags are set.
87
+ * 6. (Node.js only) a Readable stream, or a Web ReadableStream.
88
+ * Streamed as raw binary data directly into the upload request.
89
+ * Throws if metadata or tags are set.
84
90
  * For example:
85
91
  * <pre>
86
92
  * var fileUploadControl = $("#profilePhotoFileUpload")[0];
@@ -114,7 +120,25 @@ class ParseFile {
114
120
  this._metadata = metadata || {};
115
121
  this._tags = tags || {};
116
122
  if (data !== undefined) {
117
- if ((0, _isArray.default)(data) || data instanceof Uint8Array) {
123
+ if (typeof Buffer !== 'undefined' && Buffer.isBuffer(data)) {
124
+ this._source = {
125
+ format: 'buffer',
126
+ buffer: data,
127
+ type: specifiedType
128
+ };
129
+ } else if (data !== null && typeof data === 'object' && typeof data.pipe === 'function' && typeof data.read === 'function') {
130
+ this._source = {
131
+ format: 'stream',
132
+ stream: data,
133
+ type: specifiedType
134
+ };
135
+ } else if (typeof ReadableStream !== 'undefined' && data instanceof ReadableStream) {
136
+ this._source = {
137
+ format: 'stream',
138
+ stream: data,
139
+ type: specifiedType
140
+ };
141
+ } else if ((0, _isArray.default)(data) || data instanceof Uint8Array) {
118
142
  this._data = ParseFile.encodeBase64(data);
119
143
  this._source = {
120
144
  format: 'base64',
@@ -169,10 +193,15 @@ class ParseFile {
169
193
  * @returns {Promise} Promise that is resolve with base64 data
170
194
  */
171
195
  async getData(options) {
196
+ var _this$_source;
172
197
  options = options || {};
173
198
  if (this._data) {
174
199
  return this._data;
175
200
  }
201
+ if (((_this$_source = this._source) === null || _this$_source === void 0 ? void 0 : _this$_source.format) === 'buffer') {
202
+ this._data = this._source.buffer.toString('base64');
203
+ return this._data;
204
+ }
176
205
  if (!this._url) {
177
206
  throw new Error('Cannot retrieve data for unsaved ParseFile.');
178
207
  }
@@ -235,6 +264,12 @@ class ParseFile {
235
264
  /**
236
265
  * Saves the file to the Parse cloud.
237
266
  *
267
+ * In Node.js, files created with Buffer or ReadableStream are uploaded as
268
+ * raw binary data, avoiding base64 encoding overhead. If metadata
269
+ * or tags are set on a Buffer-backed file, the upload falls back to base64
270
+ * JSON encoding (since the binary endpoint does not support metadata).
271
+ * Stream-backed files with metadata or tags will throw an error.
272
+ *
238
273
  * @param {object} options
239
274
  * Valid options are:<ul>
240
275
  * <li>useMasterKey: In Cloud Code and Node only, causes the Master Key to
@@ -262,7 +297,38 @@ class ParseFile {
262
297
  options.tags = this._tags;
263
298
  const controller = _CoreManager.default.getFileController();
264
299
  if (!this._previousSave) {
265
- if (this._source.format === 'file') {
300
+ if (this._source.format === 'buffer' || this._source.format === 'stream') {
301
+ const hasMetadataOrTags = this._metadata && (0, _keys.default)(this._metadata).length > 0 || this._tags && (0, _keys.default)(this._tags).length > 0;
302
+ if (this._source.format === 'stream' && hasMetadataOrTags) {
303
+ throw new Error('Cannot save a stream-based file with metadata or tags. Use a Buffer instead.');
304
+ }
305
+ if (this._source.format === 'stream' && !controller.saveBinary) {
306
+ throw new Error('Cannot save a stream-based file without saveBinary support on the FileController.');
307
+ }
308
+ if (!hasMetadataOrTags && controller.saveBinary) {
309
+ // Binary upload via ajax
310
+ this._previousSave = controller.saveBinary(this._name, this._source, options).then(res => {
311
+ this._name = res.name;
312
+ this._url = res.url;
313
+ this._data = null;
314
+ this._requestTask = null;
315
+ return this;
316
+ });
317
+ } else if (this._source.format === 'buffer') {
318
+ // Buffer: fall back to base64 JSON encoding (metadata/tags or no saveBinary)
319
+ const base64Source = {
320
+ format: 'base64',
321
+ base64: this._source.buffer.toString('base64'),
322
+ type: this._source.type
323
+ };
324
+ this._previousSave = controller.saveBase64(this._name, base64Source, options).then(res => {
325
+ this._name = res.name;
326
+ this._url = res.url;
327
+ this._requestTask = null;
328
+ return this;
329
+ });
330
+ }
331
+ } else if (this._source.format === 'file') {
266
332
  this._previousSave = controller.saveFile(this._name, this._source, options).then(res => {
267
333
  this._name = res.name;
268
334
  this._url = res.url;
@@ -474,6 +540,68 @@ const DefaultController = {
474
540
  }
475
541
  return _CoreManager.default.getRESTController().request('POST', 'files/' + name, data, options);
476
542
  },
543
+ saveBinary: async function (name, source) {
544
+ let options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
545
+ if (source.format !== 'buffer' && source.format !== 'stream') {
546
+ throw new Error('saveBinary can only be used with Buffer or Stream sources.');
547
+ }
548
+ const headers = {
549
+ 'X-Parse-Application-ID': _CoreManager.default.get('APPLICATION_ID')
550
+ };
551
+ headers['Content-Type'] = (source.type || 'application/octet-stream').replace(/[\r\n]/g, '');
552
+ const jsKey = _CoreManager.default.get('JAVASCRIPT_KEY');
553
+ if (jsKey) {
554
+ headers['X-Parse-JavaScript-Key'] = jsKey;
555
+ }
556
+ let useMasterKey = options.useMasterKey;
557
+ if (typeof useMasterKey === 'undefined') {
558
+ useMasterKey = _CoreManager.default.get('USE_MASTER_KEY');
559
+ }
560
+ if (useMasterKey) {
561
+ if (_CoreManager.default.get('MASTER_KEY')) {
562
+ delete headers['X-Parse-JavaScript-Key'];
563
+ headers['X-Parse-Master-Key'] = _CoreManager.default.get('MASTER_KEY');
564
+ } else {
565
+ throw new Error('Cannot use the Master Key, it has not been provided.');
566
+ }
567
+ }
568
+ if (options.sessionToken) {
569
+ headers['X-Parse-Session-Token'] = options.sessionToken;
570
+ } else {
571
+ const userController = _CoreManager.default.getUserController();
572
+ if (userController) {
573
+ const user = await userController.currentUserAsync();
574
+ if (user) {
575
+ const token = user.getSessionToken();
576
+ if (token) {
577
+ headers['X-Parse-Session-Token'] = token;
578
+ }
579
+ }
580
+ }
581
+ }
582
+ let body;
583
+ if (source.format === 'buffer') {
584
+ body = source.buffer;
585
+ } else if (source.format === 'stream') {
586
+ const stream = source.stream;
587
+ if (typeof stream.pipe === 'function' && typeof stream.read === 'function') {
588
+ body = NodeReadable.toWeb(stream);
589
+ } else {
590
+ body = stream;
591
+ }
592
+ }
593
+ let url = _CoreManager.default.get('SERVER_URL');
594
+ if (url[url.length - 1] !== '/') {
595
+ url += '/';
596
+ }
597
+ url += 'files/' + encodeURIComponent(name);
598
+ return _CoreManager.default.getRESTController().ajax('POST', url, body, headers, options).then(_ref => {
599
+ let {
600
+ response
601
+ } = _ref;
602
+ return response;
603
+ });
604
+ },
477
605
  download: async function (uri, options) {
478
606
  const controller = new AbortController();
479
607
  options.requestTask(controller);
@@ -1735,6 +1735,11 @@ class ParseQuery {
1735
1735
  * provided keys. If this is called multiple times, then all of the keys
1736
1736
  * specified in each of the calls will be included.
1737
1737
  *
1738
+ * When selecting `authData` on `Parse.User`, only auth data of currently
1739
+ * configured auth providers is returned. Auth data of providers that are no
1740
+ * longer configured is not included. To return all auth data regardless of
1741
+ * the provider configuration, do not select `authData`.
1742
+ *
1738
1743
  * @param {...string|Array<string>} keys The name(s) of the key(s) to include.
1739
1744
  * @returns {Parse.Query} Returns the query, so you can chain this call.
1740
1745
  */
@@ -153,6 +153,9 @@ const RESTController = {
153
153
  };
154
154
  if (data) {
155
155
  fetchOptions.body = data;
156
+ if (typeof ReadableStream !== 'undefined' && data instanceof ReadableStream) {
157
+ fetchOptions.duplex = 'half';
158
+ }
156
159
  }
157
160
  const response = await fetch(url, fetchOptions);
158
161
  const {
@@ -219,7 +222,9 @@ const RESTController = {
219
222
  });
220
223
  } else if (status >= 500 || status === 0) {
221
224
  // retry on 5XX or library error
222
- if (++attempts < _CoreManager.default.get('REQUEST_ATTEMPT_LIMIT')) {
225
+ // ReadableStream bodies cannot be retried because they are consumed by the first fetch
226
+ const isStream = typeof ReadableStream !== 'undefined' && data instanceof ReadableStream;
227
+ if (!isStream && ++attempts < _CoreManager.default.get('REQUEST_ATTEMPT_LIMIT')) {
223
228
  // Exponentially-growing random delay
224
229
  const delay = Math.round(Math.random() * 125 * Math.pow(2, attempts));
225
230
  (0, _setTimeout2.default)(dispatch, delay);
@@ -14,7 +14,7 @@ const config = {
14
14
  SERVER_URL: 'https://api.parse.com/1',
15
15
  LIVEQUERY_SERVER_URL: null,
16
16
  ENCRYPTED_KEY: null,
17
- VERSION: 'js' + "8.1.1-alpha.5",
17
+ VERSION: 'js' + "8.2.0-alpha.1",
18
18
  APPLICATION_ID: null,
19
19
  JAVASCRIPT_KEY: null,
20
20
  MAINTENANCE_KEY: null,
@@ -14,6 +14,7 @@ function _interopRequireDefault(e) {
14
14
  }
15
15
  /* global Blob */
16
16
 
17
+ let NodeReadable = require('stream').Readable;
17
18
  function b64Digit(number) {
18
19
  if (number < 26) {
19
20
  return String.fromCharCode(65 + number);
@@ -49,8 +50,13 @@ class ParseFile {
49
50
  * 1. an Array of byte value Numbers or Uint8Array.
50
51
  * 2. an Object like { base64: "..." } with a base64-encoded String.
51
52
  * 3. an Object like { uri: "..." } with a uri String.
52
- * 4. a File object selected with a file upload control. (3) only works
53
- * in Firefox 3.6+, Safari 6.0.2+, Chrome 7+, and IE 10+.
53
+ * 4. a File object selected with a file upload control.
54
+ * 5. (Node.js only) a Buffer. Uploaded as raw binary data instead of
55
+ * base64-encoding, reducing memory usage. Falls back to base64
56
+ * JSON encoding if metadata or tags are set.
57
+ * 6. (Node.js only) a Readable stream, or a Web ReadableStream.
58
+ * Streamed as raw binary data directly into the upload request.
59
+ * Throws if metadata or tags are set.
54
60
  * For example:
55
61
  * <pre>
56
62
  * var fileUploadControl = $("#profilePhotoFileUpload")[0];
@@ -76,7 +82,25 @@ class ParseFile {
76
82
  this._metadata = metadata || {};
77
83
  this._tags = tags || {};
78
84
  if (data !== undefined) {
79
- if (Array.isArray(data) || data instanceof Uint8Array) {
85
+ if (typeof Buffer !== 'undefined' && Buffer.isBuffer(data)) {
86
+ this._source = {
87
+ format: 'buffer',
88
+ buffer: data,
89
+ type: specifiedType
90
+ };
91
+ } else if (data !== null && typeof data === 'object' && typeof data.pipe === 'function' && typeof data.read === 'function') {
92
+ this._source = {
93
+ format: 'stream',
94
+ stream: data,
95
+ type: specifiedType
96
+ };
97
+ } else if (typeof ReadableStream !== 'undefined' && data instanceof ReadableStream) {
98
+ this._source = {
99
+ format: 'stream',
100
+ stream: data,
101
+ type: specifiedType
102
+ };
103
+ } else if (Array.isArray(data) || data instanceof Uint8Array) {
80
104
  this._data = ParseFile.encodeBase64(data);
81
105
  this._source = {
82
106
  format: 'base64',
@@ -130,10 +154,15 @@ class ParseFile {
130
154
  * @returns {Promise} Promise that is resolve with base64 data
131
155
  */
132
156
  async getData(options) {
157
+ var _this$_source;
133
158
  options = options || {};
134
159
  if (this._data) {
135
160
  return this._data;
136
161
  }
162
+ if (((_this$_source = this._source) === null || _this$_source === void 0 ? void 0 : _this$_source.format) === 'buffer') {
163
+ this._data = this._source.buffer.toString('base64');
164
+ return this._data;
165
+ }
137
166
  if (!this._url) {
138
167
  throw new Error('Cannot retrieve data for unsaved ParseFile.');
139
168
  }
@@ -196,6 +225,12 @@ class ParseFile {
196
225
  /**
197
226
  * Saves the file to the Parse cloud.
198
227
  *
228
+ * In Node.js, files created with Buffer or ReadableStream are uploaded as
229
+ * raw binary data, avoiding base64 encoding overhead. If metadata
230
+ * or tags are set on a Buffer-backed file, the upload falls back to base64
231
+ * JSON encoding (since the binary endpoint does not support metadata).
232
+ * Stream-backed files with metadata or tags will throw an error.
233
+ *
199
234
  * @param {object} options
200
235
  * Valid options are:<ul>
201
236
  * <li>useMasterKey: In Cloud Code and Node only, causes the Master Key to
@@ -223,7 +258,38 @@ class ParseFile {
223
258
  options.tags = this._tags;
224
259
  const controller = _CoreManager.default.getFileController();
225
260
  if (!this._previousSave) {
226
- if (this._source.format === 'file') {
261
+ if (this._source.format === 'buffer' || this._source.format === 'stream') {
262
+ const hasMetadataOrTags = this._metadata && Object.keys(this._metadata).length > 0 || this._tags && Object.keys(this._tags).length > 0;
263
+ if (this._source.format === 'stream' && hasMetadataOrTags) {
264
+ throw new Error('Cannot save a stream-based file with metadata or tags. Use a Buffer instead.');
265
+ }
266
+ if (this._source.format === 'stream' && !controller.saveBinary) {
267
+ throw new Error('Cannot save a stream-based file without saveBinary support on the FileController.');
268
+ }
269
+ if (!hasMetadataOrTags && controller.saveBinary) {
270
+ // Binary upload via ajax
271
+ this._previousSave = controller.saveBinary(this._name, this._source, options).then(res => {
272
+ this._name = res.name;
273
+ this._url = res.url;
274
+ this._data = null;
275
+ this._requestTask = null;
276
+ return this;
277
+ });
278
+ } else if (this._source.format === 'buffer') {
279
+ // Buffer: fall back to base64 JSON encoding (metadata/tags or no saveBinary)
280
+ const base64Source = {
281
+ format: 'base64',
282
+ base64: this._source.buffer.toString('base64'),
283
+ type: this._source.type
284
+ };
285
+ this._previousSave = controller.saveBase64(this._name, base64Source, options).then(res => {
286
+ this._name = res.name;
287
+ this._url = res.url;
288
+ this._requestTask = null;
289
+ return this;
290
+ });
291
+ }
292
+ } else if (this._source.format === 'file') {
227
293
  this._previousSave = controller.saveFile(this._name, this._source, options).then(res => {
228
294
  this._name = res.name;
229
295
  this._url = res.url;
@@ -435,6 +501,64 @@ const DefaultController = {
435
501
  }
436
502
  return _CoreManager.default.getRESTController().request('POST', 'files/' + name, data, options);
437
503
  },
504
+ saveBinary: async function (name, source, options = {}) {
505
+ if (source.format !== 'buffer' && source.format !== 'stream') {
506
+ throw new Error('saveBinary can only be used with Buffer or Stream sources.');
507
+ }
508
+ const headers = {
509
+ 'X-Parse-Application-ID': _CoreManager.default.get('APPLICATION_ID')
510
+ };
511
+ headers['Content-Type'] = (source.type || 'application/octet-stream').replace(/[\r\n]/g, '');
512
+ const jsKey = _CoreManager.default.get('JAVASCRIPT_KEY');
513
+ if (jsKey) {
514
+ headers['X-Parse-JavaScript-Key'] = jsKey;
515
+ }
516
+ let useMasterKey = options.useMasterKey;
517
+ if (typeof useMasterKey === 'undefined') {
518
+ useMasterKey = _CoreManager.default.get('USE_MASTER_KEY');
519
+ }
520
+ if (useMasterKey) {
521
+ if (_CoreManager.default.get('MASTER_KEY')) {
522
+ delete headers['X-Parse-JavaScript-Key'];
523
+ headers['X-Parse-Master-Key'] = _CoreManager.default.get('MASTER_KEY');
524
+ } else {
525
+ throw new Error('Cannot use the Master Key, it has not been provided.');
526
+ }
527
+ }
528
+ if (options.sessionToken) {
529
+ headers['X-Parse-Session-Token'] = options.sessionToken;
530
+ } else {
531
+ const userController = _CoreManager.default.getUserController();
532
+ if (userController) {
533
+ const user = await userController.currentUserAsync();
534
+ if (user) {
535
+ const token = user.getSessionToken();
536
+ if (token) {
537
+ headers['X-Parse-Session-Token'] = token;
538
+ }
539
+ }
540
+ }
541
+ }
542
+ let body;
543
+ if (source.format === 'buffer') {
544
+ body = source.buffer;
545
+ } else if (source.format === 'stream') {
546
+ const stream = source.stream;
547
+ if (typeof stream.pipe === 'function' && typeof stream.read === 'function') {
548
+ body = NodeReadable.toWeb(stream);
549
+ } else {
550
+ body = stream;
551
+ }
552
+ }
553
+ let url = _CoreManager.default.get('SERVER_URL');
554
+ if (url[url.length - 1] !== '/') {
555
+ url += '/';
556
+ }
557
+ url += 'files/' + encodeURIComponent(name);
558
+ return _CoreManager.default.getRESTController().ajax('POST', url, body, headers, options).then(({
559
+ response
560
+ }) => response);
561
+ },
438
562
  download: async function (uri, options) {
439
563
  const controller = new AbortController();
440
564
  options.requestTask(controller);
@@ -1671,6 +1671,11 @@ class ParseQuery {
1671
1671
  * provided keys. If this is called multiple times, then all of the keys
1672
1672
  * specified in each of the calls will be included.
1673
1673
  *
1674
+ * When selecting `authData` on `Parse.User`, only auth data of currently
1675
+ * configured auth providers is returned. Auth data of providers that are no
1676
+ * longer configured is not included. To return all auth data regardless of
1677
+ * the provider configuration, do not select `authData`.
1678
+ *
1674
1679
  * @param {...string|Array<string>} keys The name(s) of the key(s) to include.
1675
1680
  * @returns {Parse.Query} Returns the query, so you can chain this call.
1676
1681
  */
@@ -118,6 +118,9 @@ const RESTController = {
118
118
  };
119
119
  if (data) {
120
120
  fetchOptions.body = data;
121
+ if (typeof ReadableStream !== 'undefined' && data instanceof ReadableStream) {
122
+ fetchOptions.duplex = 'half';
123
+ }
121
124
  }
122
125
  const response = await fetch(url, fetchOptions);
123
126
  const {
@@ -183,7 +186,9 @@ const RESTController = {
183
186
  });
184
187
  } else if (status >= 500 || status === 0) {
185
188
  // retry on 5XX or library error
186
- if (++attempts < _CoreManager.default.get('REQUEST_ATTEMPT_LIMIT')) {
189
+ // ReadableStream bodies cannot be retried because they are consumed by the first fetch
190
+ const isStream = typeof ReadableStream !== 'undefined' && data instanceof ReadableStream;
191
+ if (!isStream && ++attempts < _CoreManager.default.get('REQUEST_ATTEMPT_LIMIT')) {
187
192
  // Exponentially-growing random delay
188
193
  const delay = Math.round(Math.random() * 125 * Math.pow(2, attempts));
189
194
  setTimeout(dispatch, delay);
@@ -10,7 +10,7 @@ var config = {
10
10
  SERVER_URL: 'https://api.parse.com/1',
11
11
  LIVEQUERY_SERVER_URL: null,
12
12
  ENCRYPTED_KEY: null,
13
- VERSION: 'js' + "8.1.1-alpha.5",
13
+ VERSION: 'js' + "8.2.0-alpha.1",
14
14
  APPLICATION_ID: null,
15
15
  JAVASCRIPT_KEY: null,
16
16
  MAINTENANCE_KEY: null,
@@ -10,6 +10,7 @@ var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/cl
10
10
  var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
11
11
  var _CoreManager = _interopRequireDefault(require("./CoreManager"));
12
12
  var _ParseError = _interopRequireDefault(require("./ParseError"));
13
+ var NodeReadable;
13
14
  function b64Digit(number) {
14
15
  if (number < 26) {
15
16
  return String.fromCharCode(65 + number);
@@ -36,7 +37,25 @@ var ParseFile = function () {
36
37
  this._metadata = metadata || {};
37
38
  this._tags = tags || {};
38
39
  if (data !== undefined) {
39
- if (Array.isArray(data) || data instanceof Uint8Array) {
40
+ if (typeof Buffer !== 'undefined' && Buffer.isBuffer(data)) {
41
+ this._source = {
42
+ format: 'buffer',
43
+ buffer: data,
44
+ type: specifiedType
45
+ };
46
+ } else if (data !== null && typeof data === 'object' && typeof data.pipe === 'function' && typeof data.read === 'function') {
47
+ this._source = {
48
+ format: 'stream',
49
+ stream: data,
50
+ type: specifiedType
51
+ };
52
+ } else if (typeof ReadableStream !== 'undefined' && data instanceof ReadableStream) {
53
+ this._source = {
54
+ format: 'stream',
55
+ stream: data,
56
+ type: specifiedType
57
+ };
58
+ } else if (Array.isArray(data) || data instanceof Uint8Array) {
40
59
  this._data = ParseFile.encodeBase64(data);
41
60
  this._source = {
42
61
  format: 'base64',
@@ -73,11 +92,16 @@ var ParseFile = function () {
73
92
  key: "getData",
74
93
  value: function () {
75
94
  var _getData = (0, _asyncToGenerator2.default)(function* (options) {
76
- var _this = this;
95
+ var _this$_source,
96
+ _this = this;
77
97
  options = options || {};
78
98
  if (this._data) {
79
99
  return this._data;
80
100
  }
101
+ if (((_this$_source = this._source) == null ? void 0 : _this$_source.format) === 'buffer') {
102
+ this._data = this._source.buffer.toString('base64');
103
+ return this._data;
104
+ }
81
105
  if (!this._url) {
82
106
  throw new Error('Cannot retrieve data for unsaved ParseFile.');
83
107
  }
@@ -134,7 +158,36 @@ var ParseFile = function () {
134
158
  options.tags = this._tags;
135
159
  var controller = _CoreManager.default.getFileController();
136
160
  if (!this._previousSave) {
137
- if (this._source.format === 'file') {
161
+ if (this._source.format === 'buffer' || this._source.format === 'stream') {
162
+ var hasMetadataOrTags = this._metadata && Object.keys(this._metadata).length > 0 || this._tags && Object.keys(this._tags).length > 0;
163
+ if (this._source.format === 'stream' && hasMetadataOrTags) {
164
+ throw new Error('Cannot save a stream-based file with metadata or tags. Use a Buffer instead.');
165
+ }
166
+ if (this._source.format === 'stream' && !controller.saveBinary) {
167
+ throw new Error('Cannot save a stream-based file without saveBinary support on the FileController.');
168
+ }
169
+ if (!hasMetadataOrTags && controller.saveBinary) {
170
+ this._previousSave = controller.saveBinary(this._name, this._source, options).then(function (res) {
171
+ _this2._name = res.name;
172
+ _this2._url = res.url;
173
+ _this2._data = null;
174
+ _this2._requestTask = null;
175
+ return _this2;
176
+ });
177
+ } else if (this._source.format === 'buffer') {
178
+ var base64Source = {
179
+ format: 'base64',
180
+ base64: this._source.buffer.toString('base64'),
181
+ type: this._source.type
182
+ };
183
+ this._previousSave = controller.saveBase64(this._name, base64Source, options).then(function (res) {
184
+ _this2._name = res.name;
185
+ _this2._url = res.url;
186
+ _this2._requestTask = null;
187
+ return _this2;
188
+ });
189
+ }
190
+ } else if (this._source.format === 'file') {
138
191
  this._previousSave = controller.saveFile(this._name, this._source, options).then(function (res) {
139
192
  _this2._name = res.name;
140
193
  _this2._url = res.url;
@@ -334,6 +387,72 @@ var DefaultController = {
334
387
  }
335
388
  return _CoreManager.default.getRESTController().request('POST', 'files/' + name, data, options);
336
389
  },
390
+ saveBinary: function () {
391
+ var _saveBinary = (0, _asyncToGenerator2.default)(function* (name, source) {
392
+ var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
393
+ if (source.format !== 'buffer' && source.format !== 'stream') {
394
+ throw new Error('saveBinary can only be used with Buffer or Stream sources.');
395
+ }
396
+ var headers = {
397
+ 'X-Parse-Application-ID': _CoreManager.default.get('APPLICATION_ID')
398
+ };
399
+ headers['Content-Type'] = (source.type || 'application/octet-stream').replace(/[\r\n]/g, '');
400
+ var jsKey = _CoreManager.default.get('JAVASCRIPT_KEY');
401
+ if (jsKey) {
402
+ headers['X-Parse-JavaScript-Key'] = jsKey;
403
+ }
404
+ var useMasterKey = options.useMasterKey;
405
+ if (typeof useMasterKey === 'undefined') {
406
+ useMasterKey = _CoreManager.default.get('USE_MASTER_KEY');
407
+ }
408
+ if (useMasterKey) {
409
+ if (_CoreManager.default.get('MASTER_KEY')) {
410
+ delete headers['X-Parse-JavaScript-Key'];
411
+ headers['X-Parse-Master-Key'] = _CoreManager.default.get('MASTER_KEY');
412
+ } else {
413
+ throw new Error('Cannot use the Master Key, it has not been provided.');
414
+ }
415
+ }
416
+ if (options.sessionToken) {
417
+ headers['X-Parse-Session-Token'] = options.sessionToken;
418
+ } else {
419
+ var userController = _CoreManager.default.getUserController();
420
+ if (userController) {
421
+ var user = yield userController.currentUserAsync();
422
+ if (user) {
423
+ var token = user.getSessionToken();
424
+ if (token) {
425
+ headers['X-Parse-Session-Token'] = token;
426
+ }
427
+ }
428
+ }
429
+ }
430
+ var body;
431
+ if (source.format === 'buffer') {
432
+ body = source.buffer;
433
+ } else if (source.format === 'stream') {
434
+ var stream = source.stream;
435
+ if (typeof stream.pipe === 'function' && typeof stream.read === 'function') {
436
+ body = NodeReadable.toWeb(stream);
437
+ } else {
438
+ body = stream;
439
+ }
440
+ }
441
+ var url = _CoreManager.default.get('SERVER_URL');
442
+ if (url[url.length - 1] !== '/') {
443
+ url += '/';
444
+ }
445
+ url += 'files/' + encodeURIComponent(name);
446
+ return _CoreManager.default.getRESTController().ajax('POST', url, body, headers, options).then(function (_ref) {
447
+ var response = _ref.response;
448
+ return response;
449
+ });
450
+ });
451
+ function saveBinary() {
452
+ return _saveBinary.apply(this, arguments);
453
+ }
454
+ return saveBinary;
455
+ }(),
337
456
  download: function () {
338
457
  var _download = (0, _asyncToGenerator2.default)(function* (uri, options) {
339
458
  var controller = new AbortController();
@@ -102,6 +102,9 @@ var RESTController = {
102
102
  };
103
103
  if (data) {
104
104
  fetchOptions.body = data;
105
+ if (typeof ReadableStream !== 'undefined' && data instanceof ReadableStream) {
106
+ fetchOptions.duplex = 'half';
107
+ }
105
108
  }
106
109
  var response = yield fetch(url, fetchOptions);
107
110
  var status = response.status;
@@ -163,7 +166,8 @@ var RESTController = {
163
166
  dropBody: status === 303
164
167
  });
165
168
  } else if (status >= 500 || status === 0) {
166
- if (++attempts < _CoreManager.default.get('REQUEST_ATTEMPT_LIMIT')) {
169
+ var isStream = typeof ReadableStream !== 'undefined' && data instanceof ReadableStream;
170
+ if (!isStream && ++attempts < _CoreManager.default.get('REQUEST_ATTEMPT_LIMIT')) {
167
171
  var delay = Math.round(Math.random() * 125 * Math.pow(2, attempts));
168
172
  setTimeout(_dispatch, delay);
169
173
  } else if (status === 0) {
@@ -18,7 +18,7 @@ const config = {
18
18
  SERVER_URL: 'https://api.parse.com/1',
19
19
  LIVEQUERY_SERVER_URL: null,
20
20
  ENCRYPTED_KEY: null,
21
- VERSION: 'js' + "8.1.1-alpha.5",
21
+ VERSION: 'js' + "8.2.0-alpha.1",
22
22
  APPLICATION_ID: null,
23
23
  JAVASCRIPT_KEY: null,
24
24
  MAINTENANCE_KEY: null,