Skip to content

feat: Add Uint8Array support for Parse.File data #2548

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions integration/test/ParseDistTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ for (const fileName of ['parse.js', 'parse.min.js']) {
beforeEach(async () => {
browser = await puppeteer.launch({
args: ['--disable-web-security', '--incognito', '--no-sandbox'],
devtools: false,
});
const context = await browser.createBrowserContext();
page = await context.newPage();
Expand Down Expand Up @@ -42,7 +43,7 @@ for (const fileName of ['parse.js', 'parse.min.js']) {
expect(obj.id).toEqual(response);
});

it('can cancel save file with uri', async () => {
it('can cancel save file', async () => {
let requestsCount = 0;
let abortedCount = 0;
const promise = resolvingPromise();
Expand All @@ -63,11 +64,11 @@ for (const fileName of ['parse.js', 'parse.min.js']) {
}
});
await page.evaluate(async () => {
const parseLogo =
'https://raw.githubusercontent.com/parse-community/parse-server/master/.github/parse-server-logo.png';
const file = new Parse.File('parse-server-logo', { uri: parseLogo });
file.save().then(() => {});

const SIZE_10_MB = 10 * 1024 * 1024;
const file = new Parse.File('test_file.txt', new Uint8Array(SIZE_10_MB));
file.save().then(() => {
fail('should not save');
});
return new Promise(resolve => {
const intervalId = setInterval(() => {
if (file._requestTask && typeof file._requestTask.abort === 'function') {
Expand Down
4 changes: 2 additions & 2 deletions src/ParseFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class ParseFile {
* an alphanumeric character, and consist of alphanumeric characters,
* periods, spaces, underscores, or dashes.
* @param data {Array} The data for the file, as either:
* 1. an Array of byte value Numbers, or
* 1. an Array of byte value Numbers or Uint8Array.
* 2. an Object like { base64: "..." } with a base64-encoded String.
* 3. an Object like { uri: "..." } with a uri String.
* 4. a File object selected with a file upload control. (3) only works
Expand Down Expand Up @@ -113,7 +113,7 @@ class ParseFile {
this._tags = tags || {};

if (data !== undefined) {
if (Array.isArray(data)) {
if (Array.isArray(data) || data instanceof Uint8Array) {
this._data = ParseFile.encodeBase64(data);
this._source = {
format: 'base64',
Expand Down
7 changes: 7 additions & 0 deletions src/__tests__/ParseFile-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,13 @@ describe('ParseFile', () => {
expect(file._data).toBe('ParseA==');
});

it('can create files with Uint8Arrays', () => {
const file = new ParseFile('parse.txt', new Uint8Array([61, 170, 236, 120]));
expect(file._source.base64).toBe('ParseA==');
expect(file._source.type).toBe('');
expect(file._data).toBe('ParseA==');
});

it('can create files with all types of characters', () => {
const file = new ParseFile('parse.txt', [11, 239, 191, 215, 80, 52]);
expect(file._source.base64).toBe('C++/11A0');
Expand Down
2 changes: 1 addition & 1 deletion types/ParseFile.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ declare class ParseFile {
* an alphanumeric character, and consist of alphanumeric characters,
* periods, spaces, underscores, or dashes.
* @param data {Array} The data for the file, as either:
* 1. an Array of byte value Numbers, or
* 1. an Array of byte value Numbers or Uint8Array.
* 2. an Object like { base64: "..." } with a base64-encoded String.
* 3. an Object like { uri: "..." } with a uri String.
* 4. a File object selected with a file upload control. (3) only works
Expand Down