Skip to content

Commit

Permalink
feat(other): Add Storage support (#7888)
Browse files Browse the repository at this point in the history
Co-authored-by: Salakar <mike@invertase.io>
  • Loading branch information
Ehesp and Salakar authored Jul 10, 2024
1 parent ffaac4f commit 9b8dda7
Show file tree
Hide file tree
Showing 15 changed files with 674 additions and 56 deletions.
12 changes: 12 additions & 0 deletions packages/app/lib/internal/RNFBNativeEventEmitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@ class RNFBNativeEventEmitter extends NativeEventEmitter {
if (global.RNFBDebug) {
// eslint-disable-next-line no-console
console.debug(`[RNFB<--Event][📣] ${eventType} <-`, JSON.stringify(args[0]));
// Possible leaking test if events are still being received after the test.
// This is not super accurate but it's better than nothing, e.g. if doing setup/teardown
// logic outside of a test this may cause false positives.
if (global.RNFBTest && !global.RNFBDebugInTestLeakDetection) {
// eslint-disable-next-line no-console
console.debug(
`[TEST--->Leak][💡] Possible leaking test detected! An event (☝️) ` +
`was received outside of any running tests which may indicates that some ` +
`listeners/event subscriptions that have not been unsubscribed from in your ` +
`test code. The last test that ran was: "${global.RNFBDebugLastTest}".`,
);
}
}
return listener(...args);
};
Expand Down
4 changes: 4 additions & 0 deletions packages/app/lib/internal/web/firebaseStorage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// We need to share firebase imports between modules, otherwise
// apps and instances of the firebase modules are not shared.
export * from 'firebase/app';
export * from 'firebase/storage';
27 changes: 27 additions & 0 deletions packages/app/lib/internal/web/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { DeviceEventEmitter } from 'react-native';

// A general purpose guard function to catch errors and return a structured error object.
export function guard(fn) {
return fn().catch(e => Promise.reject(getWebError(e)));
}

// Converts a thrown error to a structured error object
// required by RNFirebase native module internals.
export function getWebError(error) {
const obj = {
code: error.code || 'unknown',
message: error.message,
};
// Split out prefix, since we internally prefix all error codes already.
if (obj.code.includes('/')) {
obj.code = obj.code.split('/')[1];
}
return {
...obj,
userInfo: obj,
};
}

export function emitEvent(eventName, event) {
setImmediate(() => DeviceEventEmitter.emit('rnfb_' + eventName, event));
}
110 changes: 68 additions & 42 deletions packages/storage/e2e/StorageReference.e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,11 @@ describe('storage() -> StorageReference', function () {
return Promise.reject(new Error('Did not throw'));
} catch (error) {
error.code.should.equal('storage/object-not-found');
error.message.should.equal(
'[storage/object-not-found] No object exists at the desired reference.',
);
if (!Platform.other) {
error.message.should.equal(
'[storage/object-not-found] No object exists at the desired reference.',
);
}
return Promise.resolve();
}
});
Expand All @@ -124,9 +126,11 @@ describe('storage() -> StorageReference', function () {
return Promise.reject(new Error('Did not throw'));
} catch (error) {
error.code.should.equal('storage/object-not-found');
error.message.should.equal(
'[storage/object-not-found] No object exists at the desired reference.',
);
if (!Platform.other) {
error.message.should.equal(
'[storage/object-not-found] No object exists at the desired reference.',
);
}
return Promise.resolve();
}
});
Expand All @@ -139,9 +143,11 @@ describe('storage() -> StorageReference', function () {
return Promise.reject(new Error('Did not throw'));
} catch (error) {
error.code.should.equal('storage/unauthorized');
error.message.should.equal(
'[storage/unauthorized] User is not authorized to perform the desired action.',
);
if (!Platform.other) {
error.message.should.equal(
'[storage/unauthorized] User is not authorized to perform the desired action.',
);
}
return Promise.resolve();
}
});
Expand All @@ -168,9 +174,11 @@ describe('storage() -> StorageReference', function () {
return Promise.reject(new Error('Did not throw'));
} catch (error) {
error.code.should.equal('storage/object-not-found');
error.message.should.equal(
'[storage/object-not-found] No object exists at the desired reference.',
);
if (!Platform.other) {
error.message.should.equal(
'[storage/object-not-found] No object exists at the desired reference.',
);
}
return Promise.resolve();
}
});
Expand All @@ -182,9 +190,11 @@ describe('storage() -> StorageReference', function () {
return Promise.reject(new Error('Did not throw'));
} catch (error) {
error.code.should.equal('storage/unauthorized');
error.message.should.equal(
'[storage/unauthorized] User is not authorized to perform the desired action.',
);
if (!Platform.other) {
error.message.should.equal(
'[storage/unauthorized] User is not authorized to perform the desired action.',
);
}
return Promise.resolve();
}
});
Expand All @@ -196,7 +206,7 @@ describe('storage() -> StorageReference', function () {
const metadata = await storageReference.getMetadata();
metadata.generation.should.be.a.String();
metadata.fullPath.should.equal(`${PATH}/list/file1.txt`);
if (Platform.android) {
if (Platform.android || Platform.other) {
metadata.name.should.equal('file1.txt');
} else {
// FIXME on ios file comes through as fully-qualified
Expand Down Expand Up @@ -341,9 +351,11 @@ describe('storage() -> StorageReference', function () {
return Promise.reject(new Error('listAll on a forbidden directory succeeded'));
} catch (error) {
error.code.should.equal('storage/unauthorized');
error.message.should.equal(
'[storage/unauthorized] User is not authorized to perform the desired action.',
);
if (!Platform.other) {
error.message.should.equal(
'[storage/unauthorized] User is not authorized to perform the desired action.',
);
}
return Promise.resolve();
}
});
Expand All @@ -366,7 +378,7 @@ describe('storage() -> StorageReference', function () {
// Things that are set automagically for us
metadata.generation.should.be.a.String();
metadata.fullPath.should.equal(`${PATH}/list/file1.txt`);
if (Platform.android) {
if (Platform.android || Platform.other) {
metadata.name.should.equal('file1.txt');
} else {
// FIXME on ios file comes through as fully-qualified
Expand Down Expand Up @@ -402,7 +414,7 @@ describe('storage() -> StorageReference', function () {
// Things that are set automagically for us and are not updatable
metadata.generation.should.be.a.String();
metadata.fullPath.should.equal(`${PATH}/list/file1.txt`);
if (Platform.android) {
if (Platform.android || Platform.other) {
metadata.name.should.equal('file1.txt');
} else {
// FIXME on ios file comes through as fully-qualified
Expand Down Expand Up @@ -454,6 +466,7 @@ describe('storage() -> StorageReference', function () {
contentType: 'application/octet-stream',
customMetadata: {
keepMe: 'please',
removeMeSecondTime: null,
},
});
Object.keys(metadata.customMetadata).length.should.equal(1);
Expand Down Expand Up @@ -783,9 +796,11 @@ describe('storage() -> StorageReference', function () {
return Promise.reject(new Error('Did not throw'));
} catch (error) {
error.code.should.equal('storage/object-not-found');
error.message.should.equal(
'[storage/object-not-found] No object exists at the desired reference.',
);
if (!Platform.other) {
error.message.should.equal(
'[storage/object-not-found] No object exists at the desired reference.',
);
}
return Promise.resolve();
}
});
Expand All @@ -799,9 +814,11 @@ describe('storage() -> StorageReference', function () {
return Promise.reject(new Error('Did not throw'));
} catch (error) {
error.code.should.equal('storage/object-not-found');
error.message.should.equal(
'[storage/object-not-found] No object exists at the desired reference.',
);
if (!Platform.other) {
error.message.should.equal(
'[storage/object-not-found] No object exists at the desired reference.',
);
}
return Promise.resolve();
}
});
Expand All @@ -815,9 +832,11 @@ describe('storage() -> StorageReference', function () {
return Promise.reject(new Error('Did not throw'));
} catch (error) {
error.code.should.equal('storage/unauthorized');
error.message.should.equal(
'[storage/unauthorized] User is not authorized to perform the desired action.',
);
if (!Platform.other) {
error.message.should.equal(
'[storage/unauthorized] User is not authorized to perform the desired action.',
);
}
return Promise.resolve();
}
});
Expand Down Expand Up @@ -847,9 +866,11 @@ describe('storage() -> StorageReference', function () {
return Promise.reject(new Error('Did not throw'));
} catch (error) {
error.code.should.equal('storage/object-not-found');
error.message.should.equal(
'[storage/object-not-found] No object exists at the desired reference.',
);
if (!Platform.other) {
error.message.should.equal(
'[storage/object-not-found] No object exists at the desired reference.',
);
}
return Promise.resolve();
}
});
Expand All @@ -863,9 +884,11 @@ describe('storage() -> StorageReference', function () {
return Promise.reject(new Error('Did not throw'));
} catch (error) {
error.code.should.equal('storage/unauthorized');
error.message.should.equal(
'[storage/unauthorized] User is not authorized to perform the desired action.',
);
if (!Platform.other) {
error.message.should.equal(
'[storage/unauthorized] User is not authorized to perform the desired action.',
);
}
return Promise.resolve();
}
});
Expand All @@ -878,7 +901,7 @@ describe('storage() -> StorageReference', function () {
const metadata = await getMetadata(storageReference);
metadata.generation.should.be.a.String();
metadata.fullPath.should.equal(`${PATH}/list/file1.txt`);
if (Platform.android) {
if (Platform.android || Platform.other) {
metadata.name.should.equal('file1.txt');
} else {
// FIXME on ios file comes through as fully-qualified
Expand Down Expand Up @@ -1049,9 +1072,11 @@ describe('storage() -> StorageReference', function () {
return Promise.reject(new Error('listAll on a forbidden directory succeeded'));
} catch (error) {
error.code.should.equal('storage/unauthorized');
error.message.should.equal(
'[storage/unauthorized] User is not authorized to perform the desired action.',
);
if (!Platform.other) {
error.message.should.equal(
'[storage/unauthorized] User is not authorized to perform the desired action.',
);
}
return Promise.resolve();
}
});
Expand All @@ -1077,7 +1102,7 @@ describe('storage() -> StorageReference', function () {
// Things that are set automagically for us
metadata.generation.should.be.a.String();
metadata.fullPath.should.equal(`${PATH}/list/file1.txt`);
if (Platform.android) {
if (Platform.android || Platform.other) {
metadata.name.should.equal('file1.txt');
} else {
// FIXME on ios file comes through as fully-qualified
Expand Down Expand Up @@ -1116,7 +1141,7 @@ describe('storage() -> StorageReference', function () {
// Things that are set automagically for us and are not updatable
metadata.generation.should.be.a.String();
metadata.fullPath.should.equal(`${PATH}/list/file1.txt`);
if (Platform.android) {
if (Platform.android || Platform.other) {
metadata.name.should.equal('file1.txt');
} else {
// FIXME on ios file comes through as fully-qualified
Expand Down Expand Up @@ -1175,6 +1200,7 @@ describe('storage() -> StorageReference', function () {
contentType: 'application/octet-stream',
customMetadata: {
keepMe: 'please',
removeMeSecondTime: null,
},
});

Expand Down
Loading

0 comments on commit 9b8dda7

Please sign in to comment.