@myshkouski/web-serial-polyfill 2.0.3 → 2.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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/serial.ts +29 -17
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@myshkouski/web-serial-polyfill",
3
- "version": "2.0.3",
3
+ "version": "2.1.0",
4
4
  "description": "An implementation of the [Serial API](https://wicg.github.io/serial) on top of the [WebUSB API](https://wicg.github.io/webusb) for use with USB-to-serial adapters. Use of this library is limited to hardware and platforms where the device is accessible via the WebUSB API because it has not been claimed by a built-in device driver. This project will be used to prototype the design of the Serial API.",
5
5
  "type": "module",
6
6
  "main": "./dist/serial.js",
package/serial.ts CHANGED
@@ -198,8 +198,10 @@ class UsbEndpointUnderlyingSink implements UnderlyingSink<Uint8Array> {
198
198
  }
199
199
  }
200
200
 
201
+ export type BaseSerialPort = Omit<SerialPort, "getSignals" | "onconnect" | "ondisconnect" | keyof EventTarget>
202
+
201
203
  /** a class used to control serial devices over WebUSB */
202
- export class SerialPort {
204
+ class SerialPortPolyfill implements BaseSerialPort {
203
205
  private polyfillOptions_: SerialPolyfillOptions;
204
206
  private device_: USBDevice;
205
207
  private controlInterface_: USBInterface;
@@ -239,6 +241,17 @@ export class SerialPort {
239
241
  this.outEndpoint_ = findEndpoint(this.transferInterface_, 'out');
240
242
  }
241
243
 
244
+ /**
245
+ * Getter for checking whether device is connected.
246
+ * Since there is no equivalent getter on USBDevice instance,
247
+ * this getter returns "true" when device is opened, not connected.
248
+ *
249
+ * @returns {boolean} "true" when the underlying USB device is opened.
250
+ */
251
+ public get connected(): boolean {
252
+ return this.device_.opened
253
+ }
254
+
242
255
  /**
243
256
  * Getter for the readable attribute. Constructs a new ReadableStream as
244
257
  * necessary.
@@ -282,15 +295,12 @@ export class SerialPort {
282
295
  * Used before closing device.
283
296
  */
284
297
  private async releaseInterfaces_(): Promise<void> {
285
- if (this.transferInterface_.claimed) {
286
- await this.device_.releaseInterface(
287
- this.transferInterface_.interfaceNumber
288
- );
289
- }
290
- if (this.controlInterface_.claimed) {
291
- await this.device_.releaseInterface(
292
- this.controlInterface_.interfaceNumber);
293
- }
298
+ await this.device_.releaseInterface(
299
+ this.transferInterface_.interfaceNumber
300
+ );
301
+ await this.device_.releaseInterface(
302
+ this.controlInterface_.interfaceNumber
303
+ );
294
304
  }
295
305
 
296
306
  /**
@@ -528,8 +538,10 @@ export class SerialPort {
528
538
  }
529
539
  }
530
540
 
541
+ export { SerialPortPolyfill as SerialPort }
542
+
531
543
  /** generic implementation of navigator.serial object */
532
- export abstract class BaseSerial<T extends SerialPort> {
544
+ export abstract class BaseSerial<T extends BaseSerialPort> {
533
545
  /**
534
546
  * @param {USB} usb Instance of navigator.usb object
535
547
  */
@@ -545,7 +557,7 @@ export abstract class BaseSerial<T extends SerialPort> {
545
557
  *
546
558
  * @param {SerialPortRequestOptions} options
547
559
  * @param {SerialPolyfillOptions} polyfillOptions
548
- * @return {Promise<SerialPort>}
560
+ * @return {Promise<T>}
549
561
  */
550
562
  async requestPort(
551
563
  options?: SerialPortRequestOptions,
@@ -584,7 +596,7 @@ export abstract class BaseSerial<T extends SerialPort> {
584
596
  *
585
597
  * @param {SerialPolyfillOptions} polyfillOptions Polyfill configuration that
586
598
  * should be applied to these ports.
587
- * @return {Promise<SerialPort[]>} a promise that is resolved with a list of
599
+ * @return {Promise<T[]>} a promise that is resolved with a list of
588
600
  * ports.
589
601
  */
590
602
  async getPorts(polyfillOptions?: SerialPolyfillOptions): Promise<T[]> {
@@ -605,15 +617,15 @@ export abstract class BaseSerial<T extends SerialPort> {
605
617
  }
606
618
 
607
619
  /** default implementation of the global navigator.serial object */
608
- export class Serial extends BaseSerial<SerialPort> {
620
+ export class Serial extends BaseSerial<SerialPortPolyfill> {
609
621
  /**
610
622
  * @param {USBDevice} device
611
623
  * @param {SerialPolyfillOptions} options
612
- * @return {SerialPort} Default serial port implementation
624
+ * @return {SerialPortPolyfill} Default serial port implementation
613
625
  */
614
626
  protected createPort(device: USBDevice,
615
- options?: SerialPolyfillOptions): SerialPort {
616
- return new SerialPort(device, options);
627
+ options?: SerialPolyfillOptions): SerialPortPolyfill {
628
+ return new SerialPortPolyfill(device, options);
617
629
  }
618
630
  }
619
631