react-native-web-serial-api 0.0.3 → 0.2.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 +198 -104
- package/TESTING.md +542 -0
- package/android/build.gradle +16 -2
- package/android/src/main/java/dev/webserialapi/NativeUsbSerialModule.java +74 -11
- package/android/src/main/java/dev/webserialapi/PortPickerActivity.java +61 -59
- package/bin/expose-serial.js +205 -0
- package/lib/commonjs/UsbSerial.js +58 -26
- package/lib/commonjs/UsbSerial.js.map +1 -1
- package/lib/commonjs/WebSerial.js +273 -77
- package/lib/commonjs/WebSerial.js.map +1 -1
- package/lib/commonjs/index.js +15 -3
- 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 +140 -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/lib/web-streams.js +42 -0
- package/lib/commonjs/lib/web-streams.js.map +1 -0
- package/lib/commonjs/testing/device-fixture.js +70 -0
- package/lib/commonjs/testing/device-fixture.js.map +1 -0
- package/lib/commonjs/testing/expose.js +91 -0
- package/lib/commonjs/testing/expose.js.map +1 -0
- package/lib/commonjs/testing/harness.js +98 -0
- package/lib/commonjs/testing/harness.js.map +1 -0
- package/lib/commonjs/testing/in-memory-serial-transport.js +653 -0
- package/lib/commonjs/testing/in-memory-serial-transport.js.map +1 -0
- package/lib/commonjs/testing/index.js +153 -0
- package/lib/commonjs/testing/index.js.map +1 -0
- package/lib/commonjs/testing/install-in-memory-serial-transport.js +54 -0
- package/lib/commonjs/testing/install-in-memory-serial-transport.js.map +1 -0
- package/lib/commonjs/testing/serial-client.js +277 -0
- package/lib/commonjs/testing/serial-client.js.map +1 -0
- package/lib/commonjs/testing/simulated-device.js +164 -0
- package/lib/commonjs/testing/simulated-device.js.map +1 -0
- package/lib/commonjs/testing/test-suite.js +142 -0
- package/lib/commonjs/testing/test-suite.js.map +1 -0
- package/lib/commonjs/transport.js +61 -0
- package/lib/commonjs/transport.js.map +1 -0
- package/lib/commonjs/websocket/WebSocketSerialTransport.js +659 -0
- package/lib/commonjs/websocket/WebSocketSerialTransport.js.map +1 -0
- package/lib/commonjs/websocket/bridge.js +234 -0
- package/lib/commonjs/websocket/bridge.js.map +1 -0
- package/lib/commonjs/websocket/index.js +33 -0
- package/lib/commonjs/websocket/index.js.map +1 -0
- package/lib/commonjs/websocket/protocol.js +55 -0
- package/lib/commonjs/websocket/protocol.js.map +1 -0
- package/lib/commonjs/websocket/serial-device-bridge.js +130 -0
- package/lib/commonjs/websocket/serial-device-bridge.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 +16 -7
- package/lib/typescript/src/WebSerial.d.ts.map +1 -1
- package/lib/typescript/src/index.d.ts +3 -1
- 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 +55 -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/lib/web-streams.d.ts +9 -0
- package/lib/typescript/src/lib/web-streams.d.ts.map +1 -0
- package/lib/typescript/src/testing/device-fixture.d.ts +70 -0
- package/lib/typescript/src/testing/device-fixture.d.ts.map +1 -0
- package/lib/typescript/src/testing/expose.d.ts +71 -0
- package/lib/typescript/src/testing/expose.d.ts.map +1 -0
- package/lib/typescript/src/testing/harness.d.ts +34 -0
- package/lib/typescript/src/testing/harness.d.ts.map +1 -0
- package/lib/typescript/src/testing/in-memory-serial-transport.d.ts +216 -0
- package/lib/typescript/src/testing/in-memory-serial-transport.d.ts.map +1 -0
- package/lib/typescript/src/testing/index.d.ts +33 -0
- package/lib/typescript/src/testing/index.d.ts.map +1 -0
- package/lib/typescript/src/testing/install-in-memory-serial-transport.d.ts +25 -0
- package/lib/typescript/src/testing/install-in-memory-serial-transport.d.ts.map +1 -0
- package/lib/typescript/src/testing/serial-client.d.ts +62 -0
- package/lib/typescript/src/testing/serial-client.d.ts.map +1 -0
- package/lib/typescript/src/testing/simulated-device.d.ts +127 -0
- package/lib/typescript/src/testing/simulated-device.d.ts.map +1 -0
- package/lib/typescript/src/testing/test-suite.d.ts +75 -0
- package/lib/typescript/src/testing/test-suite.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/lib/typescript/src/websocket/WebSocketSerialTransport.d.ts +111 -0
- package/lib/typescript/src/websocket/WebSocketSerialTransport.d.ts.map +1 -0
- package/lib/typescript/src/websocket/bridge.d.ts +66 -0
- package/lib/typescript/src/websocket/bridge.d.ts.map +1 -0
- package/lib/typescript/src/websocket/index.d.ts +19 -0
- package/lib/typescript/src/websocket/index.d.ts.map +1 -0
- package/lib/typescript/src/websocket/protocol.d.ts +64 -0
- package/lib/typescript/src/websocket/protocol.d.ts.map +1 -0
- package/lib/typescript/src/websocket/serial-device-bridge.d.ts +32 -0
- package/lib/typescript/src/websocket/serial-device-bridge.d.ts.map +1 -0
- package/package.json +57 -3
- package/src/UsbSerial.ts +65 -90
- package/src/WebSerial.ts +351 -113
- package/src/index.ts +6 -8
- package/src/lib/dom-exception.ts +129 -60
- package/src/lib/event-target.ts +58 -21
- package/src/lib/promise.ts +7 -7
- package/src/lib/web-streams.ts +43 -0
- package/src/testing/device-fixture.ts +150 -0
- package/src/testing/expose.ts +147 -0
- package/src/testing/harness.ts +124 -0
- package/src/testing/in-memory-serial-transport.ts +840 -0
- package/src/testing/index.ts +90 -0
- package/src/testing/install-in-memory-serial-transport.ts +65 -0
- package/src/testing/serial-client.ts +313 -0
- package/src/testing/simulated-device.ts +193 -0
- package/src/testing/test-suite.ts +186 -0
- package/src/transport.ts +200 -0
- package/src/websocket/WebSocketSerialTransport.ts +796 -0
- package/src/websocket/bridge.ts +299 -0
- package/src/websocket/index.ts +38 -0
- package/src/websocket/protocol.ts +101 -0
- package/src/websocket/serial-device-bridge.ts +160 -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 -252
- package/example/src/screens/TerminalScreen.tsx +0 -572
- 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/UsbSerial.ts
CHANGED
|
@@ -1,92 +1,39 @@
|
|
|
1
1
|
import {NativeEventEmitter, NativeModules} from 'react-native';
|
|
2
2
|
import NativeUsbSerial from './NativeUsbSerial';
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
portNumber: number;
|
|
38
|
-
data: number[];
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
export type ErrorEvent = {
|
|
42
|
-
deviceId: number;
|
|
43
|
-
portNumber: number;
|
|
44
|
-
error: string;
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
export type ConnectEvent = {
|
|
48
|
-
deviceId: number;
|
|
49
|
-
usbVendorId: number;
|
|
50
|
-
usbProductId: number;
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
export const Parity = {
|
|
54
|
-
NONE: 0,
|
|
55
|
-
ODD: 1,
|
|
56
|
-
EVEN: 2,
|
|
57
|
-
MARK: 3,
|
|
58
|
-
SPACE: 4,
|
|
59
|
-
} as const;
|
|
60
|
-
|
|
61
|
-
export const DataBits = {
|
|
62
|
-
FIVE: 5,
|
|
63
|
-
SIX: 6,
|
|
64
|
-
SEVEN: 7,
|
|
65
|
-
EIGHT: 8,
|
|
66
|
-
} as const;
|
|
67
|
-
|
|
68
|
-
export const StopBits = {
|
|
69
|
-
ONE: 1,
|
|
70
|
-
ONE_FIVE: 3,
|
|
71
|
-
TWO: 2,
|
|
72
|
-
} as const;
|
|
73
|
-
|
|
74
|
-
export type OpenOptions = {
|
|
75
|
-
baudRate: number;
|
|
76
|
-
dataBits?: number;
|
|
77
|
-
stopBits?: number;
|
|
78
|
-
parity?: number;
|
|
79
|
-
};
|
|
80
|
-
|
|
81
|
-
const DEFAULT_OPEN_OPTIONS: Required<Omit<OpenOptions, 'baudRate'>> = {
|
|
82
|
-
dataBits: DataBits.EIGHT,
|
|
83
|
-
stopBits: StopBits.ONE,
|
|
84
|
-
parity: Parity.NONE,
|
|
85
|
-
};
|
|
86
|
-
|
|
87
|
-
type Subscription = {remove: () => void};
|
|
88
|
-
|
|
89
|
-
export class UsbSerialModule {
|
|
3
|
+
import type {
|
|
4
|
+
ConnectEvent,
|
|
5
|
+
ControlLine,
|
|
6
|
+
DataEvent,
|
|
7
|
+
ErrorEvent,
|
|
8
|
+
FlowControl,
|
|
9
|
+
OpenOptions,
|
|
10
|
+
PortFilter,
|
|
11
|
+
PortId,
|
|
12
|
+
PortPickerLabels,
|
|
13
|
+
SerialTransport,
|
|
14
|
+
Subscription,
|
|
15
|
+
} from './transport';
|
|
16
|
+
import {DEFAULT_OPEN_OPTIONS} from './transport';
|
|
17
|
+
|
|
18
|
+
// These types and enums were originally declared in this module and form part
|
|
19
|
+
// of the public `UsbSerial` namespace. They now live in ./transport (a
|
|
20
|
+
// react-native-free module the virtual transport also builds on) and are
|
|
21
|
+
// re-exported here unchanged for backwards compatibility.
|
|
22
|
+
export type {
|
|
23
|
+
ConnectEvent,
|
|
24
|
+
ControlLine,
|
|
25
|
+
DataEvent,
|
|
26
|
+
ErrorEvent,
|
|
27
|
+
FlowControl,
|
|
28
|
+
OpenOptions,
|
|
29
|
+
PortFilter,
|
|
30
|
+
PortId,
|
|
31
|
+
PortPickerLabels,
|
|
32
|
+
SerialTransport,
|
|
33
|
+
} from './transport';
|
|
34
|
+
export {DataBits, Parity, StopBits} from './transport';
|
|
35
|
+
|
|
36
|
+
export class UsbSerialModule implements SerialTransport {
|
|
90
37
|
private readonly native: NonNullable<typeof NativeUsbSerial>;
|
|
91
38
|
private readonly emitter: NativeEventEmitter;
|
|
92
39
|
|
|
@@ -300,11 +247,39 @@ export class UsbSerialModule {
|
|
|
300
247
|
}
|
|
301
248
|
}
|
|
302
249
|
|
|
303
|
-
let instance:
|
|
304
|
-
|
|
305
|
-
|
|
250
|
+
let instance: SerialTransport | null = null;
|
|
251
|
+
let override: SerialTransport | null = null;
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* Resolve the active serial transport. Returns the override set via
|
|
255
|
+
* {@link setUsbSerial} if present (used by tests and by the example's on-device
|
|
256
|
+
* "virtual device" mode); otherwise lazily constructs the real native-backed
|
|
257
|
+
* {@link UsbSerialModule}.
|
|
258
|
+
*/
|
|
259
|
+
export function getUsbSerial(): SerialTransport {
|
|
260
|
+
if (override) return override;
|
|
306
261
|
if (!instance) {
|
|
307
262
|
instance = new UsbSerialModule();
|
|
308
263
|
}
|
|
309
264
|
return instance;
|
|
310
265
|
}
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* Override the transport returned by {@link getUsbSerial}. Pass a
|
|
269
|
+
* {@link SerialTransport} (e.g. an `InMemorySerialTransport`) to make the
|
|
270
|
+
* singleton `serial` instance — and any `new Serial()` created without an
|
|
271
|
+
* explicit transport — talk to it instead of real hardware. Pass `null` to
|
|
272
|
+
* clear the override.
|
|
273
|
+
*
|
|
274
|
+
* Inject before the first `getPorts()` / `requestPort()` / `addEventListener()`
|
|
275
|
+
* call so the lazy initialisation in `Serial` picks it up.
|
|
276
|
+
*/
|
|
277
|
+
export function setUsbSerial(transport: SerialTransport | null): void {
|
|
278
|
+
override = transport;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/** Clear any override and drop the cached native instance (test teardown). */
|
|
282
|
+
export function resetUsbSerial(): void {
|
|
283
|
+
override = null;
|
|
284
|
+
instance = null;
|
|
285
|
+
}
|