Skip to content

Commit

Permalink
chore(mobile): ⬆️ upgrade usb lib, jest, sentry
Browse files Browse the repository at this point in the history
  • Loading branch information
Nodonisko committed Nov 14, 2024
1 parent b45127d commit 4dddd40
Show file tree
Hide file tree
Showing 11 changed files with 251 additions and 241 deletions.
3 changes: 1 addition & 2 deletions jest.config.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,11 @@ module.exports = {
'\\.(js|jsx|ts|tsx)$': ['babel-jest', babelConfig],
},
transformIgnorePatterns: [
'node_modules/(?!((jest-)?react-native|@react-native(-community)?)|expo(nent)?|@expo(nent)?/.*|@expo-google-fonts/.*|react-navigation|@react-navigation/.*|@unimodules/.*|unimodules|sentry-expo|native-base|react-native-svg)',
'node_modules/(?!((jest-)?react-native|@react-native(-community)?)|expo(nent)?|@expo(nent)?/.*|@expo-google-fonts/.*|react-navigation|@react-navigation/.*|@sentry/react-native|native-base|react-native-svg)',
],
setupFiles: [
'<rootDir>/../../node_modules/@shopify/react-native-skia/jestSetup.js',
'<rootDir>/../../node_modules/react-native-gesture-handler/jestSetup.js',
'<rootDir>/../../suite-native/test-utils/src/setupReactReanimatedMock.js',
'<rootDir>/../../suite-native/test-utils/src/atomsMock.js',
'<rootDir>/../../suite-native/test-utils/src/expoMock.js',
'<rootDir>/../../suite-native/test-utils/src/walletSdkMock.js',
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@
"globals": "^15.11.0",
"jest": "29.7.0",
"jest-environment-jsdom": "29.7.0",
"jest-expo": "^50.0.2",
"jest-expo": "^52.0.1",
"metro-react-native-babel-preset": "0.77.0",
"node-fetch": "^2.6.4",
"nx": "^18.0.3",
Expand Down
21 changes: 20 additions & 1 deletion packages/react-native-usb/src/ReactNativeUsbModule.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
import { requireNativeModule } from 'expo-modules-core';
import { NativeModule } from 'expo';

import { NativeDevice } from './ReactNativeUsb.types';

type DeviceEvents = {
onDeviceConnect: (device: NativeDevice | null) => void;
onDeviceDisconnect: (device: NativeDevice | null) => void;
};

declare class ReactNativeUsbModuleDeclaration extends NativeModule<DeviceEvents> {
open: (deviceName: string) => Promise<void>;
close: (deviceName: string) => Promise<void>;
claimInterface: (deviceName: string, interfaceNumber: number) => Promise<void>;
releaseInterface: (deviceName: string, interfaceNumber: number) => Promise<void>;
selectConfiguration: (deviceName: string, configurationValue: number) => Promise<void>;
transferIn: (deviceName: string, endpointNumber: number, length: number) => Promise<number[]>;
transferOut: (deviceName: string, endpointNumber: number, data: string) => Promise<void>;
}

// It loads the native module object from the JSI or falls back to
// the bridge module (from NativeModulesProxy) if the remote debugger is on.
export const ReactNativeUsbModule = requireNativeModule('ReactNativeUsb');
export const ReactNativeUsbModule =
requireNativeModule<ReactNativeUsbModuleDeclaration>('ReactNativeUsb');
58 changes: 23 additions & 35 deletions packages/react-native-usb/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
/* eslint-disable no-console */
import { NativeModulesProxy, EventEmitter, Subscription } from 'expo-modules-core';

import { ReactNativeUsbModule } from './ReactNativeUsbModule';
import { NativeDevice, OnConnectEvent, WebUSBDevice } from './ReactNativeUsb.types';

const DEBUG_LOGS = false;

const debugLog = (...args: any[]) => {
if (DEBUG_LOGS) {
// eslint-disable-next-line no-console
console.log(...args);
}
};

const emitter = new EventEmitter(ReactNativeUsbModule ?? NativeModulesProxy.ReactNativeUsb);

const open = (deviceName: string) => ReactNativeUsbModule.open(deviceName);

const close = (deviceName: string) => ReactNativeUsbModule.close(deviceName);
Expand Down Expand Up @@ -105,53 +101,45 @@ const createWebUSBDevice = (device: NativeDevice): WebUSBDevice => ({
// and not send onConnect event if device is already connected.
const connectedDevices = new Map<string, WebUSBDevice>();

export function onDeviceConnected(listener: (event: OnConnectEvent) => void): Subscription {
return emitter.addListener<NativeDevice>('onDeviceConnect', event => {
if (!event) {
console.error('JS: USB onDeviceConnect: event is null');
// just for debugging purposes now
alert('JS: USB onDeviceConnect: event is null');
export function onDeviceConnected(listener: (event: OnConnectEvent) => void) {
return ReactNativeUsbModule.addListener('onDeviceConnect', (device: NativeDevice | null) => {
if (!device) {
debugLog('JS: USB onDeviceConnect: device is null');
console.error('JS: USB onDeviceConnect: device is null');
alert('JS: USB onDeviceConnect: device is null');

return;
}

if (connectedDevices.has(event.deviceName)) {
if (connectedDevices.has(device.deviceName)) {
console.warn('JS: USB onDeviceConnect: device already connected');
debugLog('JS: USB onDeviceConnect: device already connected');

return;
}

const eventPayload = {
device: createWebUSBDevice(event as NativeDevice),
};

debugLog('JS: USB onDeviceConnect', eventPayload);

connectedDevices.set(event.deviceName, eventPayload.device);
const webUSBDevice = createWebUSBDevice(device);
connectedDevices.set(device.deviceName, webUSBDevice);

return listener(eventPayload as any);
const event = { device: webUSBDevice, ...new Event('onconnect') } as OnConnectEvent;
listener(event);
});
}

export function onDeviceDisconnect(listener: (event: OnConnectEvent) => void): Subscription {
return emitter.addListener<NativeDevice>('onDeviceDisconnect', event => {
if (!event) {
console.error('JS: USB onDeviceConnect: event is null');
// just for debugging purposes now
alert('JS: USB onDeviceConnect: event is null');
export function onDeviceDisconnect(listener: (event: OnConnectEvent) => void) {
return ReactNativeUsbModule.addListener('onDeviceDisconnect', (device: NativeDevice | null) => {
if (!device) {
debugLog('JS: USB onDeviceDisconnect: device is null');
console.error('JS: USB onDeviceDisconnect: device is null');
alert('JS: USB onDeviceDisconnect: device is null');

return;
}

const eventPayload = {
device: createWebUSBDevice(event as NativeDevice),
};

debugLog('JS: USB onDeviceDisconnect', eventPayload);

connectedDevices.delete(event.deviceName);

return listener(eventPayload as any);
const webUSBDevice = createWebUSBDevice(device);
connectedDevices.delete(device.deviceName);
const event = { device: webUSBDevice, ...new Event('ondisconnect') } as OnConnectEvent;
listener(event);
});
}

Expand Down
4 changes: 2 additions & 2 deletions suite-common/sentry/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
"type-check": "yarn g:tsc --build"
},
"dependencies": {
"@sentry/integrations": "^7.100.1",
"@sentry/types": "^7.100.1",
"@sentry/integrations": "7.114.0",
"@sentry/types": "^8.38.0",
"@suite-common/suite-utils": "workspace:*",
"@trezor/utils": "workspace:*"
}
Expand Down
2 changes: 1 addition & 1 deletion suite-native/app/app.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ export default ({ config }: ConfigContext): ExpoConfig => {
slug: appSlugs[buildType],
owner: appOwners[buildType],
version: suiteNativeVersion,
runtimeVersion: '10',
runtimeVersion: '11',
...(buildType === 'production'
? {}
: {
Expand Down
4 changes: 2 additions & 2 deletions suite-native/app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"@react-navigation/native": "6.1.18",
"@react-navigation/native-stack": "6.11.0",
"@reduxjs/toolkit": "1.9.5",
"@sentry/integrations": "^7.114.0",
"@sentry/core": "8.34.0",
"@sentry/react-native": "6.1.0",
"@shopify/flash-list": "^1.7.2",
"@shopify/react-native-skia": "^1.5.3",
Expand Down Expand Up @@ -130,7 +130,7 @@
"@types/node": "^20.11.24",
"babel-plugin-transform-inline-environment-variables": "^0.4.4",
"babel-plugin-transform-remove-console": "^6.9.4",
"detox": "^20.25.6",
"detox": "^20.28.0",
"expo-atlas": "0.3.27",
"jest": "^29.7.0",
"metro": "0.81.0",
Expand Down
2 changes: 1 addition & 1 deletion suite-native/app/src/SentryProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useEffect, ReactNode } from 'react';
import { useSelector } from 'react-redux';

import * as Sentry from '@sentry/react-native';
import { captureConsoleIntegration } from '@sentry/integrations';
import { captureConsoleIntegration } from '@sentry/core';

import { selectIsAnalyticsEnabled } from '@suite-common/analytics';
import { getEnv, isDebugEnv, isDevelopEnv } from '@suite-native/config';
Expand Down
3 changes: 1 addition & 2 deletions suite-native/test-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@
"type-check": "yarn g:tsc --build"
},
"dependencies": {
"@testing-library/react-native": "^11.0.0",
"@testing-library/react-native": "^12.8.1",
"@trezor/styles": "workspace:*",
"@trezor/theme": "workspace:*",
"react": "18.2.0",
"react-native-gesture-handler": "^2.20.2",
"react-native-reanimated": "3.16.1",
"react-native-safe-area-context": "^4.14.0"
}
}
16 changes: 0 additions & 16 deletions suite-native/test-utils/src/setupReactReanimatedMock.js

This file was deleted.

Loading

0 comments on commit 4dddd40

Please sign in to comment.