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
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.createDeviceFixture = createDeviceFixture;
|
|
7
|
+
var _UsbSerial = require("../UsbSerial");
|
|
8
|
+
var _WebSerial = require("../WebSerial");
|
|
9
|
+
var _inMemorySerialTransport = require("./in-memory-serial-transport");
|
|
10
|
+
var _serialClient = require("./serial-client");
|
|
11
|
+
/**
|
|
12
|
+
* `createDeviceFixture` — the one-call fixture for testing serial code. It wires a
|
|
13
|
+
* {@link SimulatedDevice} simulator to an {@link InMemorySerialTransport} + `Serial`,
|
|
14
|
+
* and hands back everything a test needs to drive *both* sides:
|
|
15
|
+
*
|
|
16
|
+
* - `serial`/`port` — what the app-under-test consumes,
|
|
17
|
+
* - `simulatedDevice` (typed) + `device` handle — drive the device (inject data,
|
|
18
|
+
* move the GPS, inject faults),
|
|
19
|
+
* - `client` — a host-side {@link SerialClient} (for protocol tests), and
|
|
20
|
+
* - `whenOpened()/whenClosed()` — `await` the app connecting.
|
|
21
|
+
*
|
|
22
|
+
* @example Test how your app reacts to a device event
|
|
23
|
+
* const {simulatedDevice, device, whenOpened} = await createDeviceFixture(new MyGps());
|
|
24
|
+
* renderMyApp(); // opens the port itself
|
|
25
|
+
* await whenOpened();
|
|
26
|
+
* simulatedDevice.update({latitude: 51.48, longitude: 0});
|
|
27
|
+
* // …assert your app's UI updated
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
async function createDeviceFixture(deviceOrDevices, options = {}) {
|
|
31
|
+
const list = Array.isArray(deviceOrDevices) ? deviceOrDevices : [deviceOrDevices];
|
|
32
|
+
const hasPermission = options.hasPermission ?? true;
|
|
33
|
+
const transport = new _inMemorySerialTransport.InMemorySerialTransport(options.transport);
|
|
34
|
+
const handles = list.map((dev, i) => transport.addDevice(dev, {
|
|
35
|
+
hasPermission,
|
|
36
|
+
...(Array.isArray(deviceOrDevices) ? options.devices?.[i] : options.device)
|
|
37
|
+
}));
|
|
38
|
+
const serial = new _WebSerial.Serial(transport);
|
|
39
|
+
if (options.installGlobally) (0, _UsbSerial.setUsbSerial)(transport);
|
|
40
|
+
const ports = await serial.getPorts();
|
|
41
|
+
const clients = ports.map(p => new _serialClient.SerialClient(p, {
|
|
42
|
+
defaultTimeoutMs: options.client?.defaultTimeoutMs
|
|
43
|
+
}));
|
|
44
|
+
if (!Array.isArray(deviceOrDevices)) {
|
|
45
|
+
const handle = handles[0];
|
|
46
|
+
const port = ports[0];
|
|
47
|
+
if (!port) throw new Error('createDeviceFixture: device did not enumerate');
|
|
48
|
+
return {
|
|
49
|
+
transport,
|
|
50
|
+
serial,
|
|
51
|
+
port,
|
|
52
|
+
device: handle,
|
|
53
|
+
simulatedDevice: deviceOrDevices,
|
|
54
|
+
client: clients[0],
|
|
55
|
+
whenOpened: () => handle.whenOpened(),
|
|
56
|
+
whenClosed: () => handle.whenClosed()
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
return {
|
|
60
|
+
transport,
|
|
61
|
+
serial,
|
|
62
|
+
ports,
|
|
63
|
+
devices: handles,
|
|
64
|
+
simulatedDevices: list,
|
|
65
|
+
clients,
|
|
66
|
+
whenOpened: index => index === undefined ? Promise.race(handles.map(h => h.whenOpened())) : handles[index].whenOpened(),
|
|
67
|
+
whenClosed: index => index === undefined ? Promise.race(handles.map(h => h.whenClosed())) : handles[index].whenClosed()
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
//# sourceMappingURL=device-fixture.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["_UsbSerial","require","_WebSerial","_inMemorySerialTransport","_serialClient","createDeviceFixture","deviceOrDevices","options","list","Array","isArray","hasPermission","transport","InMemorySerialTransport","handles","map","dev","i","addDevice","devices","device","serial","Serial","installGlobally","setUsbSerial","ports","getPorts","clients","p","SerialClient","defaultTimeoutMs","client","handle","port","Error","simulatedDevice","whenOpened","whenClosed","simulatedDevices","index","undefined","Promise","race","h"],"sourceRoot":"../../../src","sources":["testing/device-fixture.ts"],"mappings":";;;;;;AAmBA,IAAAA,UAAA,GAAAC,OAAA;AAEA,IAAAC,UAAA,GAAAD,OAAA;AAKA,IAAAE,wBAAA,GAAAF,OAAA;AAIA,IAAAG,aAAA,GAAAH,OAAA;AA9BA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAwEO,eAAeI,mBAAmBA,CACvCC,eAAoD,EACpDC,OAA6B,GAAG,CAAC,CAAC,EACqB;EACvD,MAAM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AC,CAAC;MAClBS,UAAU,EAAEA,CAAA,KAAMJ,MAAM,CAACI,UAAU,CAAC,CAAC;MACrCC,UAAU,EAAEA,CAAA,KAAML,MAAM,CAACK,UAAU,CAAC;IACtC,CAAC;EACH;EAEA,OAAO;IACLzB,SAAS;IACTS,MAAM;IACNI,KAAK;IACLN,OAAO,EAAEL,OAAO;IAChBwB,gBAAgB,EAAE9B,IAAI;IACtBmB,OAAO;IACPS,UAAU,EAAGG,KAAc,IACzBA,KAAK,KAAKC,SAAS,GACfC,OAAO,CAACC,IAAI,CAAC5B,OAAO,CAACC,GAAG,CAAC4B,CAAC,IAAIA,CAAC,CAACP,UAAU,CAAC,CAAC,CAAC,CAAC,GAC9CtB,OAAO,CAACyB,KAAK,CAAC,CAACH,UAAU,CAAC,CAAC;IACjCC,UAAU,EAAGE,KAAc,IACzBA,KAAK,KAAKC,SAAS,GACfC,OAAO,CAACC,IAAI,CAAC5B,OAAO,CAACC,GAAG,CAAC4B,CAAC,IAAIA,CAAC,CAACN,UAAU,CAAC,CAAC,CAAC,CAAC,GAC9CvB,OAAO,CAACyB,KAAK,CAAC,CAACF,UAAU,CAAC;EAClC,CAAC;AACH","ignoreList":[]}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.exposeSimulatedDevice = exposeSimulatedDevice;
|
|
7
|
+
var _websocket = require("../websocket");
|
|
8
|
+
var _inMemorySerialTransport = require("./in-memory-serial-transport");
|
|
9
|
+
/**
|
|
10
|
+
* `exposeSimulatedDevice` — run a {@link SimulatedDevice} simulator behind a WebSocket
|
|
11
|
+
* server so a real app (on a device/emulator) can connect to it with
|
|
12
|
+
* `new Serial(new WebSocketSerialTransport(url))` and drive the *same* simulated
|
|
13
|
+
* peripheral your Jest tests drive. The test process keeps the
|
|
14
|
+
* {@link DeviceHandle} handle, so it can inject frames / move the GPS and
|
|
15
|
+
* `await whenOpened()` for the app to connect — turning an in-memory device
|
|
16
|
+
* suite into an on-device E2E without changing the device code.
|
|
17
|
+
*
|
|
18
|
+
* `ws` is an *optional* dependency: it is loaded lazily (and only in Node), via
|
|
19
|
+
* an indirect require so app bundlers never pull it into a mobile bundle. Pass
|
|
20
|
+
* `options.WebSocketServer` to inject your own (e.g. `import {WebSocketServer}
|
|
21
|
+
* from 'ws'`) and skip the lazy load entirely.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* import {WebSocketServer} from 'ws';
|
|
25
|
+
* const ex = exposeSimulatedDevice(new WMBusGateway('iU891A-XL'), {
|
|
26
|
+
* port: 8090,
|
|
27
|
+
* WebSocketServer,
|
|
28
|
+
* });
|
|
29
|
+
* // …app connects to ex.url…
|
|
30
|
+
* await ex.whenOpened();
|
|
31
|
+
* ex.simulatedDevice.addMeter(meter);
|
|
32
|
+
* meter.sendTelegram(); // the app receives the 0x20 telegram event
|
|
33
|
+
* await ex.close();
|
|
34
|
+
*/
|
|
35
|
+
|
|
36
|
+
/** The `ws` WebSocketServer surface this helper uses. */
|
|
37
|
+
|
|
38
|
+
/** A `ws`-compatible `WebSocketServer` constructor. */
|
|
39
|
+
|
|
40
|
+
/** Resolve a `ws` WebSocketServer lazily, in Node only, invisibly to bundlers. */
|
|
41
|
+
function loadWebSocketServer() {
|
|
42
|
+
const specifier = 'ws';
|
|
43
|
+
const nodeRequire =
|
|
44
|
+
// @ts-ignore
|
|
45
|
+
typeof module !== 'undefined' &&
|
|
46
|
+
// @ts-ignore
|
|
47
|
+
typeof module.require === 'function' ?
|
|
48
|
+
// @ts-ignore
|
|
49
|
+
module.require.bind(module) : /* istanbul ignore next */undefined;
|
|
50
|
+
/* istanbul ignore next — only reachable in non-Node bundled environments */
|
|
51
|
+
if (!nodeRequire) {
|
|
52
|
+
throw new Error("exposeSimulatedDevice could not load 'ws'. Pass options.WebSocketServer, " + 'or run it in a Node process with the optional `ws` package installed.');
|
|
53
|
+
}
|
|
54
|
+
const ws = nodeRequire(specifier);
|
|
55
|
+
const Ctor = ws.WebSocketServer ?? ws.Server;
|
|
56
|
+
if (!Ctor) {
|
|
57
|
+
throw new Error("the 'ws' package did not export a WebSocketServer.");
|
|
58
|
+
}
|
|
59
|
+
return Ctor;
|
|
60
|
+
}
|
|
61
|
+
function exposeSimulatedDevice(simulatedDevice, options) {
|
|
62
|
+
const transport = new _inMemorySerialTransport.InMemorySerialTransport();
|
|
63
|
+
const device = transport.addDevice(simulatedDevice, {
|
|
64
|
+
hasPermission: true,
|
|
65
|
+
...options.device
|
|
66
|
+
});
|
|
67
|
+
const Ctor = options.WebSocketServer ?? loadWebSocketServer();
|
|
68
|
+
const server = new Ctor({
|
|
69
|
+
port: options.port,
|
|
70
|
+
host: options.host
|
|
71
|
+
});
|
|
72
|
+
server.on('connection', socket => {
|
|
73
|
+
const serial = (0, _websocket.SimulatedDeviceToSerialLike)(transport, device);
|
|
74
|
+
(0, _websocket.attachBridge)(serial, socket, {
|
|
75
|
+
portInfo: (0, _websocket.portInfoFromDevice)(device),
|
|
76
|
+
readingByDefault: options.readingByDefault,
|
|
77
|
+
log: options.log
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
const host = options.host ?? 'localhost';
|
|
81
|
+
return {
|
|
82
|
+
url: `ws://${host}:${options.port}`,
|
|
83
|
+
transport,
|
|
84
|
+
device,
|
|
85
|
+
simulatedDevice,
|
|
86
|
+
whenOpened: () => device.whenOpened(),
|
|
87
|
+
whenClosed: () => device.whenClosed(),
|
|
88
|
+
close: () => new Promise(resolve => server.close(() => resolve()))
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
//# sourceMappingURL=expose.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["_websocket","require","_inMemorySerialTransport","loadWebSocketServer","specifier","nodeRequire","module","bind","undefined","Error","ws","Ctor","WebSocketServer","Server","exposeSimulatedDevice","simulatedDevice","options","transport","InMemorySerialTransport","device","addDevice","hasPermission","server","port","host","on","socket","serial","SimulatedDeviceToSerialLike","attachBridge","portInfo","portInfoFromDevice","readingByDefault","log","url","whenOpened","whenClosed","close","Promise","resolve"],"sourceRoot":"../../../src","sources":["testing/expose.ts"],"mappings":";;;;;;AA2BA,IAAAA,UAAA,GAAAC,OAAA;AAOA,IAAAC,wBAAA,GAAAD,OAAA;AAlCA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAkBA;;AAMA;;AAqCA;AACA,SAASE,mBAAmBA,CAAA,EAAwB;EAClD,MAAMC,SAAS,GAAG,IAAI;EACtB,MAAMC,WAAW;EACf;EACA,OAAOC,MAAM,KAAK,WAAW;EAC7B;EACA,OAAQA,MAAM,CAAyBL,OAAO,KAAK,UAAU;EACzD;EACCK,MAAM,CAAwCL,OAAO,CAACM,IAAI,CAACD,MAAM,CAAC,GACnE,0BAA2BE,SAAS;EAC1C;EACA,IAAI,CAACH,WAAW,EAAE;IAChB,MAAM,IAAII,KAAK,CACb,2EAA2E,GACzE,uEACJ,CAAC;EACH;EACA,MAAMC,EAAE,GAAGL,WAAW,CAACD,SAAS,CAG/B;EACD,MAAMO,IAAI,GAAGD,EAAE,CAACE,eAAe,IAAIF,EAAE,CAACG,MAAM;EAC5C,IAAI,CAACF,IAAI,EAAE;IACT,MAAM,IAAIF,KAAK,CAAC,oDAAoD,CAAC;EACvE;EACA,OAAOE,IAAI;AACb;AAEO,SAASG,qBAAqBA,CACnCC,eAAkB,EAClBC,OAAqC,EACnB;EAClB,MAAMC,SAAS,GAAG,IAAIC,gDAAuB,CAAC,CAAC;EAC/C,MAAMC,MAAM,GAAGF,SAAS,CAACG,SAAS,CAACL,eAAe,EAAE;IAClDM,aAAa,EAAE,IAAI;IACnB,GAAGL,OAAO,CAACG;EACb,CAAC,CAAC;EAEF,MAAMR,IAAI,GAAGK,OAAO,CAACJ,eAAe,IAAIT,mBAAmB,CAAC,CAAC;EAC7D,MAAMmB,MAAM,GAAG,IAAIX,IAAI,CAAC;IAACY,IAAI,EAAEP,OAAO,CAACO,IAAI;IAAEC,IAAI,EAAER,OAAO,CAACQ;EAAI,CAAC,CAAC;EACjEF,MAAM,CAACG,EAAE,CAAC,YAAY,EAAEC,MAAM,IAAI;IAChC,MAAMC,MAAM,GAAG,IAAAC,sCAA2B,EAACX,SAAS,EAAEE,MAAM,CAAC;IAC7D,IAAAU,uBAAY,EAACF,MAAM,EAAED,MAAM,EAAE;MAC3BI,QAAQ,EAAE,IAAAC,6BAAkB,EAACZ,MAAM,CAAC;MACpCa,gBAAgB,EAAEhB,OAAO,CAACgB,gBAAgB;MAC1CC,GAAG,EAAEjB,OAAO,CAACiB;IACf,CAAC,CAAC;EACJ,CAAC,CAAC;EAEF,MAAMT,IAAI,GAAGR,OAAO,CAACQ,IAAI,IAAI,WAAW;EACxC,OAAO;IACLU,GAAG,EAAE,QAAQV,IAAI,IAAIR,OAAO,CAACO,IAAI,EAAE;IACnCN,SAAS;IACTE,MAAM;IACNJ,eAAe;IACfoB,UAAU,EAAEA,CAAA,KAAMhB,MAAM,CAACgB,UAAU,CAAC,CAAC;IACrCC,UAAU,EAAEA,CAAA,KAAMjB,MAAM,CAACiB,UAAU,CAAC,CAAC;IACrCC,KAAK,EAAEA,CAAA,KAAM,IAAIC,OAAO,CAAOC,OAAO,IAAIjB,MAAM,CAACe,KAAK,CAAC,MAAME,OAAO,CAAC,CAAC,CAAC;EACzE,CAAC;AACH","ignoreList":[]}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.assert = assert;
|
|
7
|
+
exports.assertEqual = assertEqual;
|
|
8
|
+
exports.assertRejects = assertRejects;
|
|
9
|
+
exports.bytesEqual = bytesEqual;
|
|
10
|
+
exports.errorMessage = errorMessage;
|
|
11
|
+
exports.readBytes = readBytes;
|
|
12
|
+
exports.withTimeout = withTimeout;
|
|
13
|
+
/**
|
|
14
|
+
* Tiny, dependency-free test helpers for driving serial devices — shared by the
|
|
15
|
+
* library's own conformance suite, the {@link SerialClient}, and consumers' app
|
|
16
|
+
* tests. Free of `jest` and `react-native`, so they run under any test runner
|
|
17
|
+
* and on a real device (e.g. an on-device Self-Test screen).
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
/** Throw `message` if `condition` is falsy. */
|
|
21
|
+
function assert(condition, message) {
|
|
22
|
+
if (!condition) throw new Error(message);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Throw if `actual !== expected`, including both values in the message. */
|
|
26
|
+
function assertEqual(actual, expected, message) {
|
|
27
|
+
if (actual !== expected) {
|
|
28
|
+
throw new Error(`${message} (expected ${String(expected)}, got ${String(actual)})`);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** Byte-for-byte equality of two sequences. */
|
|
33
|
+
function bytesEqual(a, b) {
|
|
34
|
+
if (a.length !== b.length) return false;
|
|
35
|
+
for (let i = 0; i < a.length; i++) if (a[i] !== b[i]) return false;
|
|
36
|
+
return true;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** What {@link assertRejects} may additionally check about the thrown error. */
|
|
40
|
+
|
|
41
|
+
/** Assert `fn()` rejects (optionally matching the error name/type/message). */
|
|
42
|
+
async function assertRejects(fn, message, expected) {
|
|
43
|
+
try {
|
|
44
|
+
await fn();
|
|
45
|
+
} catch (e) {
|
|
46
|
+
const err = e;
|
|
47
|
+
if (expected?.name && err.name !== expected.name) {
|
|
48
|
+
throw new Error(`${message}: expected error "${expected.name}" but got "${err.name}"`);
|
|
49
|
+
}
|
|
50
|
+
if (expected?.type && !(err instanceof expected.type)) {
|
|
51
|
+
throw new Error(`${message}: expected a ${expected.type.name}`);
|
|
52
|
+
}
|
|
53
|
+
if (expected?.message !== undefined) {
|
|
54
|
+
const ok = expected.message instanceof RegExp ? expected.message.test(err.message) : err.message.includes(expected.message);
|
|
55
|
+
if (!ok) {
|
|
56
|
+
throw new Error(`${message}: expected the error message to match ${String(expected.message)}`);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
throw new Error(`${message}: expected a rejection but none occurred`);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Normalise any thrown value into a `"Name: message"` string. */
|
|
65
|
+
function errorMessage(e) {
|
|
66
|
+
return e instanceof Error ? `${e.name}: ${e.message}` : String(e);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Reject if `promise` doesn't settle within `ms`; `label` names it on timeout. */
|
|
70
|
+
function withTimeout(promise, ms, label) {
|
|
71
|
+
return new Promise((resolve, reject) => {
|
|
72
|
+
const timer = setTimeout(() => reject(new Error(`timed out: ${label}`)), ms);
|
|
73
|
+
promise.then(v => {
|
|
74
|
+
clearTimeout(timer);
|
|
75
|
+
resolve(v);
|
|
76
|
+
}, e => {
|
|
77
|
+
clearTimeout(timer);
|
|
78
|
+
reject(e);
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** The slice of `ReadableStreamDefaultReader` the helpers need. */
|
|
84
|
+
|
|
85
|
+
/** Read exactly `count` bytes from a reader (accumulating chunks), with a timeout. */
|
|
86
|
+
async function readBytes(reader, count, timeoutMs = 2000) {
|
|
87
|
+
const out = [];
|
|
88
|
+
while (out.length < count) {
|
|
89
|
+
const {
|
|
90
|
+
done,
|
|
91
|
+
value
|
|
92
|
+
} = await withTimeout(reader.read(), timeoutMs, `reading ${count} bytes (got ${out.length})`);
|
|
93
|
+
if (done) break;
|
|
94
|
+
if (value) out.push(...value);
|
|
95
|
+
}
|
|
96
|
+
return out;
|
|
97
|
+
}
|
|
98
|
+
//# sourceMappingURL=harness.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["assert","condition","message","Error","assertEqual","actual","expected","String","bytesEqual","a","b","length","i","assertRejects","fn","e","err","name","type","undefined","ok","RegExp","test","includes","errorMessage","withTimeout","promise","ms","label","Promise","resolve","reject","timer","setTimeout","then","v","clearTimeout","readBytes","reader","count","timeoutMs","out","done","value","read","push"],"sourceRoot":"../../../src","sources":["testing/harness.ts"],"mappings":";;;;;;;;;;;;AAAA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACO,SAASA,MAAMA,CAACC,SAAkB,EAAEC,OAAe,EAAqB;EAC7E,IAAI,CAACD,SAAS,EAAE,MAAM,IAAIE,KAAK,CAACD,OAAO,CAAC;AAC1C;;AAEA;AACO,SAASE,WAAWA,CAAIC,MAAS,EAAEC,QAAW,EAAEJ,OAAe,EAAQ;EAC5E,IAAIG,MAAM,KAAKC,QAAQ,EAAE;IACvB,MAAM,IAAIH,KAAK,CACb,GAAGD,OAAO,cAAcK,MAAM,CAACD,QAAQ,CAAC,SAASC,MAAM,CAACF,MAAM,CAAC,GACjE,CAAC;EACH;AACF;;AAEA;AACO,SAASG,UAAUA,CACxBC,CAAoB,EACpBC,CAAoB,EACX;EACT,IAAID,CAAC,CAACE,MAAM,KAAKD,CAAC,CAACC,MAAM,EAAE,OAAO,KAAK;EACvC,KAAK,IAAIC,CAAC,GAAG,CAAC,EAAEA,CAAC,GAAGH,CAAC,CAACE,MAAM,EAAEC,CAAC,EAAE,EAAE,IAAIH,CAAC,CAACG,CAAC,CAAC,KAAKF,CAAC,CAACE,CAAC,CAAC,EAAE,OAAO,KAAK;EAClE,OAAO,IAAI;AACb;;AAEA;;AAOA;AACO,eAAeC,aAAaA,CACjCC,EAA0B,EAC1BZ,OAAe,EACfI,QAA+B,EAChB;EACf,IAAI;IACF,MAAMQ,EAAE,CAAC,CAAC;EACZ,CAAC,CAAC,OAAOC,CAAC,EAAE;IACV,MAAMC,GAAG,GAAGD,CAAU;IACtB,IA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