react-native-web-serial-api 0.0.2 → 0.1.0
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.
- package/README.md +23 -0
- package/TESTING.md +301 -0
- package/android/build.gradle +2 -2
- package/android/src/main/java/dev/webserialapi/NativeUsbSerialModule.java +7 -1
- package/lib/commonjs/UsbSerial.js +58 -26
- package/lib/commonjs/UsbSerial.js.map +1 -1
- package/lib/commonjs/WebSerial.js +169 -57
- package/lib/commonjs/WebSerial.js.map +1 -1
- package/lib/commonjs/index.js +13 -1
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/lib/dom-exception.js +176 -0
- package/lib/commonjs/lib/dom-exception.js.map +1 -0
- package/lib/commonjs/lib/event-target.js +138 -0
- package/lib/commonjs/lib/event-target.js.map +1 -0
- package/lib/commonjs/lib/promise.js +23 -0
- package/lib/commonjs/lib/promise.js.map +1 -0
- package/lib/commonjs/testing/index.js +70 -0
- package/lib/commonjs/testing/index.js.map +1 -0
- package/lib/commonjs/testing/install.js +54 -0
- package/lib/commonjs/testing/install.js.map +1 -0
- package/lib/commonjs/testing/serial-device.js +164 -0
- package/lib/commonjs/testing/serial-device.js.map +1 -0
- package/lib/commonjs/testing/virtual-serial.js +615 -0
- package/lib/commonjs/testing/virtual-serial.js.map +1 -0
- package/lib/commonjs/transport.js +61 -0
- package/lib/commonjs/transport.js.map +1 -0
- package/lib/typescript/src/UsbSerial.d.ts +24 -67
- package/lib/typescript/src/UsbSerial.d.ts.map +1 -1
- package/lib/typescript/src/WebSerial.d.ts +11 -2
- package/lib/typescript/src/WebSerial.d.ts.map +1 -1
- package/lib/typescript/src/index.d.ts +2 -0
- package/lib/typescript/src/index.d.ts.map +1 -1
- package/lib/typescript/src/lib/dom-exception.d.ts +100 -0
- package/lib/typescript/src/lib/dom-exception.d.ts.map +1 -0
- package/lib/typescript/src/lib/event-target.d.ts +53 -0
- package/lib/typescript/src/lib/event-target.d.ts.map +1 -0
- package/lib/typescript/src/lib/promise.d.ts +11 -0
- package/lib/typescript/src/lib/promise.d.ts.map +1 -0
- package/lib/typescript/src/testing/index.d.ts +23 -0
- package/lib/typescript/src/testing/index.d.ts.map +1 -0
- package/lib/typescript/src/testing/install.d.ts +25 -0
- package/lib/typescript/src/testing/install.d.ts.map +1 -0
- package/lib/typescript/src/testing/serial-device.d.ts +127 -0
- package/lib/typescript/src/testing/serial-device.d.ts.map +1 -0
- package/lib/typescript/src/testing/virtual-serial.d.ts +205 -0
- package/lib/typescript/src/testing/virtual-serial.d.ts.map +1 -0
- package/lib/typescript/src/transport.d.ts +131 -0
- package/lib/typescript/src/transport.d.ts.map +1 -0
- package/package.json +38 -2
- package/src/UsbSerial.ts +65 -90
- package/src/WebSerial.ts +227 -88
- package/src/index.ts +2 -7
- package/src/lib/dom-exception.ts +129 -60
- package/src/lib/event-target.ts +46 -21
- package/src/lib/promise.ts +7 -7
- package/src/testing/index.ts +42 -0
- package/src/testing/install.ts +65 -0
- package/src/testing/serial-device.ts +193 -0
- package/src/testing/virtual-serial.ts +801 -0
- package/src/transport.ts +200 -0
- package/babel.config.js +0 -3
- package/biome.json +0 -35
- package/example/.watchmanconfig +0 -1
- package/example/App.tsx +0 -71
- package/example/__tests__/App.test.tsx +0 -16
- package/example/__tests__/connectEvents.test.tsx +0 -81
- package/example/__tests__/getPorts.test.tsx +0 -140
- package/example/android/app/build.gradle +0 -120
- package/example/android/app/debug.keystore +0 -0
- package/example/android/app/proguard-rules.pro +0 -10
- package/example/android/app/src/debug/AndroidManifest.xml +0 -9
- package/example/android/app/src/main/AndroidManifest.xml +0 -38
- package/example/android/app/src/main/java/dev/uzlopak/MainActivity.kt +0 -22
- package/example/android/app/src/main/java/dev/uzlopak/MainApplication.kt +0 -41
- package/example/android/app/src/main/res/drawable/rn_edit_text_material.xml +0 -37
- package/example/android/app/src/main/res/mipmap-hdpi/ic_launcher.png +0 -0
- package/example/android/app/src/main/res/mipmap-hdpi/ic_launcher_round.png +0 -0
- package/example/android/app/src/main/res/mipmap-mdpi/ic_launcher.png +0 -0
- package/example/android/app/src/main/res/mipmap-mdpi/ic_launcher_round.png +0 -0
- package/example/android/app/src/main/res/mipmap-xhdpi/ic_launcher.png +0 -0
- package/example/android/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png +0 -0
- package/example/android/app/src/main/res/mipmap-xxhdpi/ic_launcher.png +0 -0
- package/example/android/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png +0 -0
- package/example/android/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png +0 -0
- package/example/android/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png +0 -0
- package/example/android/app/src/main/res/values/strings.xml +0 -3
- package/example/android/app/src/main/res/values/styles.xml +0 -9
- package/example/android/build.gradle +0 -22
- package/example/android/gradle/wrapper/gradle-wrapper.jar +0 -0
- package/example/android/gradle/wrapper/gradle-wrapper.properties +0 -7
- package/example/android/gradle.properties +0 -47
- package/example/android/gradlew +0 -252
- package/example/android/gradlew.bat +0 -94
- package/example/android/settings.gradle +0 -6
- package/example/app.json +0 -4
- package/example/babel.config.js +0 -21
- package/example/biome.json +0 -47
- package/example/deploy.sh +0 -11
- package/example/index.html +0 -26
- package/example/index.js +0 -9
- package/example/index.web.js +0 -8
- package/example/jest.config.js +0 -12
- package/example/metro.config.js +0 -58
- package/example/package-lock.json +0 -14510
- package/example/package.json +0 -48
- package/example/react-native.config.js +0 -17
- package/example/src/components/AppBar.tsx +0 -73
- package/example/src/components/Menu.tsx +0 -90
- package/example/src/components/SingleChoiceDialog.tsx +0 -120
- package/example/src/screens/ConnectScreen.tsx +0 -195
- package/example/src/screens/DevicesScreen.tsx +0 -248
- package/example/src/screens/TerminalScreen.tsx +0 -564
- package/example/src/settings.ts +0 -43
- package/example/src/theme.ts +0 -19
- package/example/src/util/TextUtil.ts +0 -129
- package/example/tsconfig.json +0 -10
- package/example/vite.config.mjs +0 -55
- package/scripts/deploy-release.sh +0 -127
- package/tsconfig.build.json +0 -7
- package/tsconfig.json +0 -20
package/src/transport.ts
ADDED
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hardware-transport seam for the Web Serial polyfill.
|
|
3
|
+
*
|
|
4
|
+
* `SerialTransport` is the single interface that the `Serial`/`SerialPort`
|
|
5
|
+
* classes depend on to talk to "the device". The production implementation
|
|
6
|
+
* (`UsbSerialModule`, backed by the `NativeUsbSerial` TurboModule — see
|
|
7
|
+
* {@link ./UsbSerial}) and the in-memory test/dev double
|
|
8
|
+
* (`VirtualSerialTransport` — see {@link ./testing/virtual-serial}) both
|
|
9
|
+
* implement it.
|
|
10
|
+
*
|
|
11
|
+
* This module is intentionally free of any `react-native` import. That is what
|
|
12
|
+
* lets the virtual transport — and therefore the conformance suite built on top
|
|
13
|
+
* of it — run unchanged under Node/Jest, in a browser, and on a device.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
// UsbSerialPort.ControlLine enum
|
|
17
|
+
export type ControlLine = 'RTS' | 'CTS' | 'DTR' | 'DSR' | 'CD' | 'RI';
|
|
18
|
+
|
|
19
|
+
// UsbSerialPort.FlowControl enum
|
|
20
|
+
export type FlowControl =
|
|
21
|
+
| 'NONE'
|
|
22
|
+
| 'RTS_CTS'
|
|
23
|
+
| 'DTR_DSR'
|
|
24
|
+
| 'XON_XOFF'
|
|
25
|
+
| 'XON_XOFF_INLINE';
|
|
26
|
+
|
|
27
|
+
export type PortFilter = {
|
|
28
|
+
usbVendorId?: number;
|
|
29
|
+
usbProductId?: number;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export type PortPickerLabels = {
|
|
33
|
+
titleSelectPort?: string;
|
|
34
|
+
titleNoPortsAvailable?: string;
|
|
35
|
+
messageNoPortsAvailable?: string;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
export type PortId = {
|
|
39
|
+
deviceId: number;
|
|
40
|
+
portNumber: number;
|
|
41
|
+
usbVendorId: number;
|
|
42
|
+
usbProductId: number;
|
|
43
|
+
/**
|
|
44
|
+
* Whether the app currently holds Android USB permission to access this
|
|
45
|
+
* device (via the system attach dialog or a prior permission request).
|
|
46
|
+
*/
|
|
47
|
+
hasPermission: boolean;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
export type DataEvent = {
|
|
51
|
+
deviceId: number;
|
|
52
|
+
portNumber: number;
|
|
53
|
+
data: number[];
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
export type ErrorEvent = {
|
|
57
|
+
deviceId: number;
|
|
58
|
+
portNumber: number;
|
|
59
|
+
error: string;
|
|
60
|
+
/**
|
|
61
|
+
* Optional spec error name (e.g. "BreakError", "BufferOverrunError",
|
|
62
|
+
* "FramingError", "ParityError") for a typed read error. When present the
|
|
63
|
+
* polyfill surfaces a DOMException of that name on the readable stream
|
|
64
|
+
* (otherwise it defaults to "NetworkError"); the WPT-derived spec tests in
|
|
65
|
+
* src/__tests__/conformance-suite.ts exercise this (BreakError,
|
|
66
|
+
* BufferOverrunError).
|
|
67
|
+
*/
|
|
68
|
+
errorName?: string;
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
export type ConnectEvent = {
|
|
72
|
+
deviceId: number;
|
|
73
|
+
usbVendorId: number;
|
|
74
|
+
usbProductId: number;
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
export const Parity = {
|
|
78
|
+
NONE: 0,
|
|
79
|
+
ODD: 1,
|
|
80
|
+
EVEN: 2,
|
|
81
|
+
MARK: 3,
|
|
82
|
+
SPACE: 4,
|
|
83
|
+
} as const;
|
|
84
|
+
|
|
85
|
+
export const DataBits = {
|
|
86
|
+
FIVE: 5,
|
|
87
|
+
SIX: 6,
|
|
88
|
+
SEVEN: 7,
|
|
89
|
+
EIGHT: 8,
|
|
90
|
+
} as const;
|
|
91
|
+
|
|
92
|
+
export const StopBits = {
|
|
93
|
+
ONE: 1,
|
|
94
|
+
ONE_FIVE: 3,
|
|
95
|
+
TWO: 2,
|
|
96
|
+
} as const;
|
|
97
|
+
|
|
98
|
+
export type OpenOptions = {
|
|
99
|
+
baudRate: number;
|
|
100
|
+
dataBits?: number;
|
|
101
|
+
stopBits?: number;
|
|
102
|
+
parity?: number;
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
export const DEFAULT_OPEN_OPTIONS: Required<Omit<OpenOptions, 'baudRate'>> = {
|
|
106
|
+
dataBits: DataBits.EIGHT,
|
|
107
|
+
stopBits: StopBits.ONE,
|
|
108
|
+
parity: Parity.NONE,
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
/** Handle returned by the `on*` subscription methods. */
|
|
112
|
+
export type Subscription = {remove: () => void};
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* The contract every serial transport must satisfy. It mirrors the JS-friendly
|
|
116
|
+
* surface of `UsbSerialModule` exactly, so `UsbSerialModule implements
|
|
117
|
+
* SerialTransport` is a faithful 1:1 and any conforming double (e.g.
|
|
118
|
+
* `VirtualSerialTransport`) is a drop-in replacement.
|
|
119
|
+
*
|
|
120
|
+
* Ports are addressed by the pair `(deviceId, portNumber)`. Inbound bytes,
|
|
121
|
+
* read errors and device attach/detach arrive through the `on*` subscriptions.
|
|
122
|
+
*/
|
|
123
|
+
export interface SerialTransport {
|
|
124
|
+
// Discovery & permission
|
|
125
|
+
findAllDrivers(): Promise<ReadonlyArray<PortId>>;
|
|
126
|
+
showPortPicker(
|
|
127
|
+
filter: ReadonlyArray<PortFilter>,
|
|
128
|
+
labels?: PortPickerLabels,
|
|
129
|
+
): Promise<PortId>;
|
|
130
|
+
requestPermission(deviceId: number): Promise<boolean>;
|
|
131
|
+
|
|
132
|
+
// Lifecycle
|
|
133
|
+
open(
|
|
134
|
+
deviceId: number,
|
|
135
|
+
portNumber: number,
|
|
136
|
+
options: OpenOptions,
|
|
137
|
+
): Promise<void>;
|
|
138
|
+
close(deviceId: number, portNumber: number): Promise<void>;
|
|
139
|
+
isOpen(deviceId: number, portNumber: number): boolean;
|
|
140
|
+
|
|
141
|
+
// I/O
|
|
142
|
+
write(
|
|
143
|
+
deviceId: number,
|
|
144
|
+
portNumber: number,
|
|
145
|
+
data: number[],
|
|
146
|
+
timeout?: number,
|
|
147
|
+
): Promise<void>;
|
|
148
|
+
startReading(deviceId: number, portNumber: number): Promise<void>;
|
|
149
|
+
stopReading(deviceId: number, portNumber: number): Promise<void>;
|
|
150
|
+
|
|
151
|
+
// Parameters
|
|
152
|
+
setParameters(
|
|
153
|
+
deviceId: number,
|
|
154
|
+
portNumber: number,
|
|
155
|
+
options: OpenOptions,
|
|
156
|
+
): Promise<void>;
|
|
157
|
+
|
|
158
|
+
// Control signals
|
|
159
|
+
setDTR(deviceId: number, portNumber: number, value: boolean): Promise<void>;
|
|
160
|
+
setRTS(deviceId: number, portNumber: number, value: boolean): Promise<void>;
|
|
161
|
+
getDTR(deviceId: number, portNumber: number): Promise<boolean>;
|
|
162
|
+
getRTS(deviceId: number, portNumber: number): Promise<boolean>;
|
|
163
|
+
getCD(deviceId: number, portNumber: number): Promise<boolean>;
|
|
164
|
+
getCTS(deviceId: number, portNumber: number): Promise<boolean>;
|
|
165
|
+
getDSR(deviceId: number, portNumber: number): Promise<boolean>;
|
|
166
|
+
getRI(deviceId: number, portNumber: number): Promise<boolean>;
|
|
167
|
+
getControlLines(deviceId: number, portNumber: number): Promise<ControlLine[]>;
|
|
168
|
+
getSupportedControlLines(
|
|
169
|
+
deviceId: number,
|
|
170
|
+
portNumber: number,
|
|
171
|
+
): Promise<ControlLine[]>;
|
|
172
|
+
|
|
173
|
+
// Flow control
|
|
174
|
+
setFlowControl(
|
|
175
|
+
deviceId: number,
|
|
176
|
+
portNumber: number,
|
|
177
|
+
flowControl: FlowControl,
|
|
178
|
+
): Promise<void>;
|
|
179
|
+
getFlowControl(deviceId: number, portNumber: number): Promise<FlowControl>;
|
|
180
|
+
getSupportedFlowControl(
|
|
181
|
+
deviceId: number,
|
|
182
|
+
portNumber: number,
|
|
183
|
+
): Promise<FlowControl[]>;
|
|
184
|
+
|
|
185
|
+
// Misc
|
|
186
|
+
setBreak(deviceId: number, portNumber: number, value: boolean): Promise<void>;
|
|
187
|
+
purgeHwBuffers(
|
|
188
|
+
deviceId: number,
|
|
189
|
+
portNumber: number,
|
|
190
|
+
purgeWriteBuffers: boolean,
|
|
191
|
+
purgeReadBuffers: boolean,
|
|
192
|
+
): Promise<void>;
|
|
193
|
+
getSerial(deviceId: number, portNumber: number): Promise<string>;
|
|
194
|
+
|
|
195
|
+
// Event subscriptions
|
|
196
|
+
onData(listener: (event: DataEvent) => void): Subscription;
|
|
197
|
+
onError(listener: (event: ErrorEvent) => void): Subscription;
|
|
198
|
+
onConnect(listener: (event: ConnectEvent) => void): Subscription;
|
|
199
|
+
onDisconnect(listener: (event: ConnectEvent) => void): Subscription;
|
|
200
|
+
}
|
package/babel.config.js
DELETED
package/biome.json
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"$schema": "https://biomejs.dev/schemas/2.4.16/schema.json",
|
|
3
|
-
"vcs": {
|
|
4
|
-
"enabled": true,
|
|
5
|
-
"clientKind": "git",
|
|
6
|
-
"useIgnoreFile": true
|
|
7
|
-
},
|
|
8
|
-
"files": {
|
|
9
|
-
"includes": ["src/**/*.{ts,tsx}"]
|
|
10
|
-
},
|
|
11
|
-
"formatter": {
|
|
12
|
-
"enabled": true,
|
|
13
|
-
"indentStyle": "space",
|
|
14
|
-
"indentWidth": 2,
|
|
15
|
-
"lineWidth": 80
|
|
16
|
-
},
|
|
17
|
-
"javascript": {
|
|
18
|
-
"formatter": {
|
|
19
|
-
"quoteStyle": "single",
|
|
20
|
-
"trailingCommas": "all",
|
|
21
|
-
"bracketSpacing": false,
|
|
22
|
-
"bracketSameLine": true,
|
|
23
|
-
"arrowParentheses": "asNeeded"
|
|
24
|
-
}
|
|
25
|
-
},
|
|
26
|
-
"linter": {
|
|
27
|
-
"enabled": true,
|
|
28
|
-
"rules": {
|
|
29
|
-
"recommended": true,
|
|
30
|
-
"style": {
|
|
31
|
-
"noNonNullAssertion": "off"
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
}
|
package/example/.watchmanconfig
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{}
|
package/example/App.tsx
DELETED
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
import React from 'react';
|
|
2
|
-
import {StatusBar, StyleSheet, View} from 'react-native';
|
|
3
|
-
import type {SerialPort} from 'react-native-web-serial-api';
|
|
4
|
-
import {ConnectScreen} from './src/screens/ConnectScreen';
|
|
5
|
-
import {DevicesScreen} from './src/screens/DevicesScreen';
|
|
6
|
-
import {TerminalScreen} from './src/screens/TerminalScreen';
|
|
7
|
-
import {type ConnectionSettings, DEFAULT_SETTINGS} from './src/settings';
|
|
8
|
-
import {colors} from './src/theme';
|
|
9
|
-
|
|
10
|
-
type Screen = 'devices' | 'connect' | 'terminal';
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* A React Native clone of Kai Morich's SimpleUsbTerminal, built on the
|
|
14
|
-
* react-native-web-serial-api Web Serial API. Runs on Android and (Chromium) web.
|
|
15
|
-
*/
|
|
16
|
-
function App(): React.JSX.Element {
|
|
17
|
-
const [screen, setScreen] = React.useState<Screen>('devices');
|
|
18
|
-
const [port, setPort] = React.useState<SerialPort | null>(null);
|
|
19
|
-
const [settings, setSettings] =
|
|
20
|
-
React.useState<ConnectionSettings>(DEFAULT_SETTINGS);
|
|
21
|
-
|
|
22
|
-
// Devices -> Connect form
|
|
23
|
-
const selectPort = (p: SerialPort) => {
|
|
24
|
-
setPort(p);
|
|
25
|
-
setScreen('connect');
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
// Connect form -> Terminal
|
|
29
|
-
const startTerminal = (s: ConnectionSettings) => {
|
|
30
|
-
setSettings(s);
|
|
31
|
-
setScreen('terminal');
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
const backToDevices = () => {
|
|
35
|
-
setScreen('devices');
|
|
36
|
-
setPort(null);
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
const backToConnect = () => setScreen('connect');
|
|
40
|
-
|
|
41
|
-
return (
|
|
42
|
-
<View style={styles.root}>
|
|
43
|
-
<StatusBar
|
|
44
|
-
barStyle="light-content"
|
|
45
|
-
backgroundColor={colors.primaryDark}
|
|
46
|
-
/>
|
|
47
|
-
{screen === 'terminal' && port ? (
|
|
48
|
-
<TerminalScreen
|
|
49
|
-
port={port}
|
|
50
|
-
settings={settings}
|
|
51
|
-
onBack={backToConnect}
|
|
52
|
-
/>
|
|
53
|
-
) : screen === 'connect' && port ? (
|
|
54
|
-
<ConnectScreen
|
|
55
|
-
port={port}
|
|
56
|
-
initial={settings}
|
|
57
|
-
onBack={backToDevices}
|
|
58
|
-
onConnect={startTerminal}
|
|
59
|
-
/>
|
|
60
|
-
) : (
|
|
61
|
-
<DevicesScreen onSelect={selectPort} />
|
|
62
|
-
)}
|
|
63
|
-
</View>
|
|
64
|
-
);
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
const styles = StyleSheet.create({
|
|
68
|
-
root: {flex: 1, backgroundColor: colors.background},
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
export default App;
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @format
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
// Note: import explicitly to use the types shipped with jest.
|
|
6
|
-
import {expect, it} from '@jest/globals';
|
|
7
|
-
import {render, screen, waitFor} from '@testing-library/react-native';
|
|
8
|
-
import App from '../App';
|
|
9
|
-
|
|
10
|
-
it('renders the devices screen', async () => {
|
|
11
|
-
render(<App />);
|
|
12
|
-
expect(screen.getByText('USB Devices')).toBeTruthy();
|
|
13
|
-
// DevicesScreen kicks off an async serial.getPorts() refresh on mount; let it
|
|
14
|
-
// settle so the state update is wrapped in act() (avoids a console warning).
|
|
15
|
-
await waitFor(() => expect(screen.queryByText('USB Devices')).toBeTruthy());
|
|
16
|
-
});
|
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @format
|
|
3
|
-
*
|
|
4
|
-
* Verifies that subscribing to Serial "connect"/"disconnect" wires up the
|
|
5
|
-
* native USB state listeners WITHOUT first calling getPorts()/requestPort(),
|
|
6
|
-
* and that a native event (e.g. the "connect" emitted when USB permission is
|
|
7
|
-
* granted) reaches the JS listener. This is what lets the example's device
|
|
8
|
-
* list auto-refresh on attach / detach / permission-grant.
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
import {afterEach, beforeEach, expect, it, jest} from '@jest/globals';
|
|
12
|
-
import {NativeModules} from 'react-native';
|
|
13
|
-
|
|
14
|
-
// Capture the JS listeners registered against the native event emitter so the
|
|
15
|
-
// test can drive them like the native side would.
|
|
16
|
-
const nativeListeners: Record<string, ((event: unknown) => void)[]> = {};
|
|
17
|
-
|
|
18
|
-
beforeEach(() => {
|
|
19
|
-
for (const k of Object.keys(nativeListeners)) {
|
|
20
|
-
delete nativeListeners[k];
|
|
21
|
-
}
|
|
22
|
-
(NativeModules as any).NativeUsbSerial = {
|
|
23
|
-
findAllDrivers: jest.fn(async () => []),
|
|
24
|
-
addListener: jest.fn(),
|
|
25
|
-
removeListeners: jest.fn(),
|
|
26
|
-
};
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
afterEach(() => {
|
|
30
|
-
// RCTDeviceEventEmitter is a global singleton across the RN copy; clear it so
|
|
31
|
-
// listeners from one test don't leak into the next.
|
|
32
|
-
const {DeviceEventEmitter} = require('react-native');
|
|
33
|
-
DeviceEventEmitter.removeAllListeners('connect');
|
|
34
|
-
DeviceEventEmitter.removeAllListeners('disconnect');
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
function freshSerial() {
|
|
38
|
-
let api: any;
|
|
39
|
-
jest.isolateModules(() => {
|
|
40
|
-
api = require('react-native-web-serial-api');
|
|
41
|
-
});
|
|
42
|
-
return new api.Serial();
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
// Emit a native device event the way the TurboModule does (via
|
|
46
|
-
// RCTDeviceEventEmitter, which NativeEventEmitter listens on).
|
|
47
|
-
function emitNative(event: 'connect' | 'disconnect', payload: unknown) {
|
|
48
|
-
const {DeviceEventEmitter} = require('react-native');
|
|
49
|
-
DeviceEventEmitter.emit(event, payload);
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
it('addEventListener("connect") receives native events without calling getPorts() first', () => {
|
|
53
|
-
const serial = freshSerial();
|
|
54
|
-
const onConnect = jest.fn();
|
|
55
|
-
|
|
56
|
-
// Subscribe only — no getPorts()/requestPort() beforehand.
|
|
57
|
-
serial.addEventListener('connect', onConnect);
|
|
58
|
-
|
|
59
|
-
// Native fires "connect" (e.g. attach or permission-grant).
|
|
60
|
-
emitNative('connect', {
|
|
61
|
-
deviceId: 7,
|
|
62
|
-
usbVendorId: 0x0403,
|
|
63
|
-
usbProductId: 0x6001,
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
expect(onConnect).toHaveBeenCalledTimes(1);
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
it('forwards native disconnect to Serial listeners', () => {
|
|
70
|
-
const serial = freshSerial();
|
|
71
|
-
const onDisconnect = jest.fn();
|
|
72
|
-
serial.addEventListener('disconnect', onDisconnect);
|
|
73
|
-
|
|
74
|
-
emitNative('disconnect', {
|
|
75
|
-
deviceId: 7,
|
|
76
|
-
usbVendorId: 0x0403,
|
|
77
|
-
usbProductId: 0x6001,
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
expect(onDisconnect).toHaveBeenCalledTimes(1);
|
|
81
|
-
});
|
|
@@ -1,140 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @format
|
|
3
|
-
*
|
|
4
|
-
* Android/native permission-model contract for the Web Serial polyfill.
|
|
5
|
-
*
|
|
6
|
-
* getPorts() enumerates every probed USB-serial port the app can currently
|
|
7
|
-
* access through Android USB permission (PortId.hasPermission === true) —
|
|
8
|
-
* whether that permission came from requestPort() or was granted natively
|
|
9
|
-
* (e.g. the system "use by default for this device" attach dialog). Ports the
|
|
10
|
-
* app cannot access yet are excluded.
|
|
11
|
-
*
|
|
12
|
-
* requestPort() is unchanged: it shows the native picker and resolves with the
|
|
13
|
-
* chosen port; it does not consult getPorts()/hasPermission.
|
|
14
|
-
*/
|
|
15
|
-
|
|
16
|
-
import {afterEach, beforeEach, describe, expect, it, jest} from '@jest/globals';
|
|
17
|
-
import {NativeModules} from 'react-native';
|
|
18
|
-
|
|
19
|
-
type FakePort = {
|
|
20
|
-
deviceId: number;
|
|
21
|
-
portNumber: number;
|
|
22
|
-
usbVendorId: number;
|
|
23
|
-
usbProductId: number;
|
|
24
|
-
hasPermission: boolean;
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
const findAllDrivers = jest.fn<() => Promise<FakePort[]>>();
|
|
28
|
-
const showPortPicker = jest.fn<() => Promise<FakePort>>();
|
|
29
|
-
|
|
30
|
-
// In jest, TurboModuleRegistry.get() falls back to NativeModules[name], so
|
|
31
|
-
// registering a fake here lets the real library (Serial -> UsbSerialModule)
|
|
32
|
-
// run against controlled data. addListener/removeListeners are required by
|
|
33
|
-
// NativeEventEmitter.
|
|
34
|
-
beforeEach(() => {
|
|
35
|
-
(NativeModules as any).NativeUsbSerial = {
|
|
36
|
-
findAllDrivers,
|
|
37
|
-
showPortPicker,
|
|
38
|
-
addListener: jest.fn(),
|
|
39
|
-
removeListeners: jest.fn(),
|
|
40
|
-
};
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
afterEach(() => {
|
|
44
|
-
findAllDrivers.mockReset();
|
|
45
|
-
showPortPicker.mockReset();
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
// Fresh module graph each time so the Serial singleton / known-port cache does
|
|
49
|
-
// not leak between tests.
|
|
50
|
-
function freshSerial() {
|
|
51
|
-
let api: any;
|
|
52
|
-
jest.isolateModules(() => {
|
|
53
|
-
api = require('react-native-web-serial-api');
|
|
54
|
-
});
|
|
55
|
-
return new api.Serial();
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
const ftdi: FakePort = {
|
|
59
|
-
deviceId: 1,
|
|
60
|
-
portNumber: 0,
|
|
61
|
-
usbVendorId: 0x0403,
|
|
62
|
-
usbProductId: 0x6001,
|
|
63
|
-
hasPermission: true,
|
|
64
|
-
};
|
|
65
|
-
const cp210xUnpermitted: FakePort = {
|
|
66
|
-
deviceId: 2,
|
|
67
|
-
portNumber: 0,
|
|
68
|
-
usbVendorId: 0x10c4,
|
|
69
|
-
usbProductId: 0xea60,
|
|
70
|
-
hasPermission: false,
|
|
71
|
-
};
|
|
72
|
-
|
|
73
|
-
describe('getPorts() Android permission filtering', () => {
|
|
74
|
-
it('includes a device granted natively (never via requestPort)', async () => {
|
|
75
|
-
findAllDrivers.mockResolvedValueOnce([ftdi]);
|
|
76
|
-
const ports = await freshSerial().getPorts();
|
|
77
|
-
expect(ports).toHaveLength(1);
|
|
78
|
-
expect(ports[0].getInfo()).toEqual({
|
|
79
|
-
usbVendorId: 0x0403,
|
|
80
|
-
usbProductId: 0x6001,
|
|
81
|
-
});
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
it('excludes a probed device the app has no permission for', async () => {
|
|
85
|
-
findAllDrivers.mockResolvedValueOnce([cp210xUnpermitted]);
|
|
86
|
-
const ports = await freshSerial().getPorts();
|
|
87
|
-
expect(ports).toHaveLength(0);
|
|
88
|
-
});
|
|
89
|
-
|
|
90
|
-
it('returns only the permitted subset when mixed', async () => {
|
|
91
|
-
findAllDrivers.mockResolvedValueOnce([ftdi, cp210xUnpermitted]);
|
|
92
|
-
const ports = await freshSerial().getPorts();
|
|
93
|
-
expect(ports).toHaveLength(1);
|
|
94
|
-
expect(ports[0].getInfo().usbVendorId).toBe(0x0403);
|
|
95
|
-
});
|
|
96
|
-
|
|
97
|
-
it('returns an empty list when nothing is permitted', async () => {
|
|
98
|
-
findAllDrivers.mockResolvedValueOnce([
|
|
99
|
-
cp210xUnpermitted,
|
|
100
|
-
{...cp210xUnpermitted, deviceId: 3},
|
|
101
|
-
]);
|
|
102
|
-
expect(await freshSerial().getPorts()).toHaveLength(0);
|
|
103
|
-
});
|
|
104
|
-
|
|
105
|
-
it('lists each port of a multi-port permitted device', async () => {
|
|
106
|
-
findAllDrivers.mockResolvedValueOnce([
|
|
107
|
-
{...ftdi, portNumber: 0},
|
|
108
|
-
{...ftdi, portNumber: 1},
|
|
109
|
-
]);
|
|
110
|
-
const ports = await freshSerial().getPorts();
|
|
111
|
-
expect(ports).toHaveLength(2);
|
|
112
|
-
});
|
|
113
|
-
|
|
114
|
-
it('returns the SAME SerialPort instance across repeated calls', async () => {
|
|
115
|
-
const serial = freshSerial();
|
|
116
|
-
findAllDrivers.mockResolvedValue([ftdi]);
|
|
117
|
-
const first = await serial.getPorts();
|
|
118
|
-
const second = await serial.getPorts();
|
|
119
|
-
expect(first[0]).toBe(second[0]); // cached, not re-created
|
|
120
|
-
});
|
|
121
|
-
});
|
|
122
|
-
|
|
123
|
-
describe('requestPort() is unaffected by the permission filter', () => {
|
|
124
|
-
it('resolves with the picked port even if getPorts would exclude it', async () => {
|
|
125
|
-
// getPorts sees nothing permitted...
|
|
126
|
-
findAllDrivers.mockResolvedValue([]);
|
|
127
|
-
// ...but the user picks a device through the native picker.
|
|
128
|
-
showPortPicker.mockResolvedValueOnce(ftdi);
|
|
129
|
-
|
|
130
|
-
const serial = freshSerial();
|
|
131
|
-
expect(await serial.getPorts()).toHaveLength(0);
|
|
132
|
-
|
|
133
|
-
const port = await serial.requestPort();
|
|
134
|
-
expect(port.getInfo()).toEqual({
|
|
135
|
-
usbVendorId: 0x0403,
|
|
136
|
-
usbProductId: 0x6001,
|
|
137
|
-
});
|
|
138
|
-
expect(showPortPicker).toHaveBeenCalledTimes(1);
|
|
139
|
-
});
|
|
140
|
-
});
|
|
@@ -1,120 +0,0 @@
|
|
|
1
|
-
apply plugin: "com.android.application"
|
|
2
|
-
apply plugin: "org.jetbrains.kotlin.android"
|
|
3
|
-
apply plugin: "com.facebook.react"
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* This is the configuration block to customize your React Native Android app.
|
|
7
|
-
* By default you don't need to apply any configuration, just uncomment the lines you need.
|
|
8
|
-
*/
|
|
9
|
-
react {
|
|
10
|
-
/* Folders */
|
|
11
|
-
// The root of your project, i.e. where "package.json" lives. Default is '../..'
|
|
12
|
-
// root = file("../../")
|
|
13
|
-
// The folder where the react-native NPM package is. Default is ../../node_modules/react-native
|
|
14
|
-
// reactNativeDir = file("../../node_modules/react-native")
|
|
15
|
-
// The folder where the react-native Codegen package is. Default is ../../node_modules/@react-native/codegen
|
|
16
|
-
// codegenDir = file("../../node_modules/@react-native/codegen")
|
|
17
|
-
// The cli.js file which is the React Native CLI entrypoint. Default is ../../node_modules/react-native/cli.js
|
|
18
|
-
// cliFile = file("../../node_modules/react-native/cli.js")
|
|
19
|
-
|
|
20
|
-
/* Variants */
|
|
21
|
-
// The list of variants to that are debuggable. For those we're going to
|
|
22
|
-
// skip the bundling of the JS bundle and the assets. By default is just 'debug'.
|
|
23
|
-
// If you add flavors like lite, prod, etc. you'll have to list your debuggableVariants.
|
|
24
|
-
// debuggableVariants = ["liteDebug", "prodDebug"]
|
|
25
|
-
|
|
26
|
-
/* Bundling */
|
|
27
|
-
// A list containing the node command and its flags. Default is just 'node'.
|
|
28
|
-
// nodeExecutableAndArgs = ["node"]
|
|
29
|
-
//
|
|
30
|
-
// The command to run when bundling. By default is 'bundle'
|
|
31
|
-
// bundleCommand = "ram-bundle"
|
|
32
|
-
//
|
|
33
|
-
// The path to the CLI configuration file. Default is empty.
|
|
34
|
-
// bundleConfig = file(../rn-cli.config.js)
|
|
35
|
-
//
|
|
36
|
-
// The name of the generated asset file containing your JS bundle
|
|
37
|
-
// bundleAssetName = "MyApplication.android.bundle"
|
|
38
|
-
//
|
|
39
|
-
// The entry file for bundle generation. Default is 'index.android.js' or 'index.js'
|
|
40
|
-
// entryFile = file("../js/MyApplication.android.js")
|
|
41
|
-
//
|
|
42
|
-
// A list of extra flags to pass to the 'bundle' commands.
|
|
43
|
-
// See https://github.com/react-native-community/cli/blob/main/docs/commands.md#bundle
|
|
44
|
-
// extraPackagerArgs = []
|
|
45
|
-
|
|
46
|
-
/* Hermes Commands */
|
|
47
|
-
// The hermes compiler command to run. By default it is 'hermesc'
|
|
48
|
-
// hermesCommand = "$rootDir/my-custom-hermesc/bin/hermesc"
|
|
49
|
-
//
|
|
50
|
-
// The list of flags to pass to the Hermes compiler. By default is "-O", "-output-source-map"
|
|
51
|
-
// hermesFlags = ["-O", "-output-source-map"]
|
|
52
|
-
|
|
53
|
-
/* Autolinking */
|
|
54
|
-
autolinkLibrariesWithApp()
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* Set this to true to Run Proguard on Release builds to minify the Java bytecode.
|
|
59
|
-
*/
|
|
60
|
-
def enableProguardInReleaseBuilds = false
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* The preferred build flavor of JavaScriptCore (JSC)
|
|
64
|
-
*
|
|
65
|
-
* For example, to use the international variant, you can use:
|
|
66
|
-
* `def jscFlavor = 'org.webkit:android-jsc-intl:+'`
|
|
67
|
-
*
|
|
68
|
-
* The international variant includes ICU i18n library and necessary data
|
|
69
|
-
* allowing to use e.g. `Date.toLocaleString` and `String.localeCompare` that
|
|
70
|
-
* give correct results when using with locales other than en-US. Note that
|
|
71
|
-
* this variant is about 6MiB larger per architecture than default.
|
|
72
|
-
*/
|
|
73
|
-
def jscFlavor = 'org.webkit:android-jsc:+'
|
|
74
|
-
|
|
75
|
-
android {
|
|
76
|
-
ndkVersion rootProject.ext.ndkVersion
|
|
77
|
-
buildToolsVersion rootProject.ext.buildToolsVersion
|
|
78
|
-
compileSdk rootProject.ext.compileSdkVersion
|
|
79
|
-
|
|
80
|
-
namespace "dev.react_native_web_serial_api.example"
|
|
81
|
-
defaultConfig {
|
|
82
|
-
applicationId "dev.react_native_web_serial_api.example"
|
|
83
|
-
minSdkVersion rootProject.ext.minSdkVersion
|
|
84
|
-
targetSdkVersion rootProject.ext.targetSdkVersion
|
|
85
|
-
versionCode 1
|
|
86
|
-
versionName "1.0"
|
|
87
|
-
}
|
|
88
|
-
signingConfigs {
|
|
89
|
-
debug {
|
|
90
|
-
storeFile file('debug.keystore')
|
|
91
|
-
storePassword 'android'
|
|
92
|
-
keyAlias 'androiddebugkey'
|
|
93
|
-
keyPassword 'android'
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
buildTypes {
|
|
97
|
-
debug {
|
|
98
|
-
signingConfig signingConfigs.debug
|
|
99
|
-
}
|
|
100
|
-
release {
|
|
101
|
-
// Caution! In production, you need to generate your own keystore file.
|
|
102
|
-
// see https://reactnative.dev/docs/signed-apk-android.
|
|
103
|
-
signingConfig signingConfigs.debug
|
|
104
|
-
minifyEnabled enableProguardInReleaseBuilds
|
|
105
|
-
proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
dependencies {
|
|
111
|
-
// usb-serial-for-android is provided transitively by react-native-web-serial-api
|
|
112
|
-
// The version of react-native is set by the React Native Gradle Plugin
|
|
113
|
-
implementation("com.facebook.react:react-android")
|
|
114
|
-
|
|
115
|
-
if (hermesEnabled.toBoolean()) {
|
|
116
|
-
implementation("com.facebook.react:hermes-android")
|
|
117
|
-
} else {
|
|
118
|
-
implementation jscFlavor
|
|
119
|
-
}
|
|
120
|
-
}
|
|
Binary file
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
# Add project specific ProGuard rules here.
|
|
2
|
-
# By default, the flags in this file are appended to flags specified
|
|
3
|
-
# in /usr/local/Cellar/android-sdk/24.3.3/tools/proguard/proguard-android.txt
|
|
4
|
-
# You can edit the include path and order by changing the proguardFiles
|
|
5
|
-
# directive in build.gradle.
|
|
6
|
-
#
|
|
7
|
-
# For more details, see
|
|
8
|
-
# http://developer.android.com/guide/developing/tools/proguard.html
|
|
9
|
-
|
|
10
|
-
# Add any project specific keep options here:
|