Skip to content

Commit 6f6bb66

Browse files
authored
feat: Add Uint8Array support for Parse.File data (#2548)
1 parent 616eabd commit 6f6bb66

File tree

4 files changed

+17
-9
lines changed

4 files changed

+17
-9
lines changed

integration/test/ParseDistTest.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ for (const fileName of ['parse.js', 'parse.min.js']) {
88
beforeEach(async () => {
99
browser = await puppeteer.launch({
1010
args: ['--disable-web-security', '--incognito', '--no-sandbox'],
11+
devtools: false,
1112
});
1213
const context = await browser.createBrowserContext();
1314
page = await context.newPage();
@@ -42,7 +43,7 @@ for (const fileName of ['parse.js', 'parse.min.js']) {
4243
expect(obj.id).toEqual(response);
4344
});
4445

45-
it('can cancel save file with uri', async () => {
46+
it('can cancel save file', async () => {
4647
let requestsCount = 0;
4748
let abortedCount = 0;
4849
const promise = resolvingPromise();
@@ -63,11 +64,11 @@ for (const fileName of ['parse.js', 'parse.min.js']) {
6364
}
6465
});
6566
await page.evaluate(async () => {
66-
const parseLogo =
67-
'https://raw.githubusercontent.com/parse-community/parse-server/master/.github/parse-server-logo.png';
68-
const file = new Parse.File('parse-server-logo', { uri: parseLogo });
69-
file.save().then(() => {});
70-
67+
const SIZE_10_MB = 10 * 1024 * 1024;
68+
const file = new Parse.File('test_file.txt', new Uint8Array(SIZE_10_MB));
69+
file.save().then(() => {
70+
fail('should not save');
71+
});
7172
return new Promise(resolve => {
7273
const intervalId = setInterval(() => {
7374
if (file._requestTask && typeof file._requestTask.abort === 'function') {

src/ParseFile.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class ParseFile {
8181
* an alphanumeric character, and consist of alphanumeric characters,
8282
* periods, spaces, underscores, or dashes.
8383
* @param data {Array} The data for the file, as either:
84-
* 1. an Array of byte value Numbers, or
84+
* 1. an Array of byte value Numbers or Uint8Array.
8585
* 2. an Object like { base64: "..." } with a base64-encoded String.
8686
* 3. an Object like { uri: "..." } with a uri String.
8787
* 4. a File object selected with a file upload control. (3) only works
@@ -113,7 +113,7 @@ class ParseFile {
113113
this._tags = tags || {};
114114

115115
if (data !== undefined) {
116-
if (Array.isArray(data)) {
116+
if (Array.isArray(data) || data instanceof Uint8Array) {
117117
this._data = ParseFile.encodeBase64(data);
118118
this._source = {
119119
format: 'base64',

src/__tests__/ParseFile-test.js

+7
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,13 @@ describe('ParseFile', () => {
156156
expect(file._data).toBe('ParseA==');
157157
});
158158

159+
it('can create files with Uint8Arrays', () => {
160+
const file = new ParseFile('parse.txt', new Uint8Array([61, 170, 236, 120]));
161+
expect(file._source.base64).toBe('ParseA==');
162+
expect(file._source.type).toBe('');
163+
expect(file._data).toBe('ParseA==');
164+
});
165+
159166
it('can create files with all types of characters', () => {
160167
const file = new ParseFile('parse.txt', [11, 239, 191, 215, 80, 52]);
161168
expect(file._source.base64).toBe('C++/11A0');

types/ParseFile.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ declare class ParseFile {
4444
* an alphanumeric character, and consist of alphanumeric characters,
4545
* periods, spaces, underscores, or dashes.
4646
* @param data {Array} The data for the file, as either:
47-
* 1. an Array of byte value Numbers, or
47+
* 1. an Array of byte value Numbers or Uint8Array.
4848
* 2. an Object like { base64: "..." } with a base64-encoded String.
4949
* 3. an Object like { uri: "..." } with a uri String.
5050
* 4. a File object selected with a file upload control. (3) only works

0 commit comments

Comments
 (0)