@psdk/device-webusb 0.3.2 → 0.3.4
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/build/index.d.ts +4 -2
- package/build/index.d.ts.map +1 -1
- package/build/index.js +75 -89
- package/package.json +3 -3
package/build/index.d.ts
CHANGED
|
@@ -3,8 +3,10 @@ export declare class Webusb {
|
|
|
3
3
|
private readonly filters;
|
|
4
4
|
private readonly configuration;
|
|
5
5
|
private readonly interface;
|
|
6
|
-
private
|
|
7
|
-
private
|
|
6
|
+
private readEndpoint;
|
|
7
|
+
private writeEndpoint;
|
|
8
|
+
private inputReadEndpoint?;
|
|
9
|
+
private inputWriteEndpoint?;
|
|
8
10
|
private state;
|
|
9
11
|
private device?;
|
|
10
12
|
constructor(options: {
|
package/build/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,eAAe,EAAmB,WAAW,EAAC,MAAM,oBAAoB,CAAC;AAEjF,qBAAa,MAAM;IACjB,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAsB;IAC9C,OAAO,CAAC,QAAQ,CAAC,aAAa,CAAS;IACvC,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAS;IACnC,OAAO,CAAC,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,eAAe,EAAmB,WAAW,EAAC,MAAM,oBAAoB,CAAC;AAEjF,qBAAa,MAAM;IACjB,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAsB;IAC9C,OAAO,CAAC,QAAQ,CAAC,aAAa,CAAS;IACvC,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAS;IACnC,OAAO,CAAC,YAAY,CAAS;IAC7B,OAAO,CAAC,aAAa,CAAS;IAC9B,OAAO,CAAC,iBAAiB,CAAC,CAAS;IACnC,OAAO,CAAC,kBAAkB,CAAC,CAAS;IAEpC,OAAO,CAAC,KAAK,CAAkB;IAC/B,OAAO,CAAC,MAAM,CAAC,CAAM;gBAEF,OAAO,EAAE;QAC1B;;WAEG;QACH,OAAO,EAAE,KAAK,CAAC,YAAY,CAAC,CAAC;QAC7B;;WAEG;QACH,aAAa,CAAC,EAAE,MAAM,CAAC;QACvB;;WAEG;QACH,SAAS,CAAC,EAAE,MAAM,CAAC;QACnB;;WAEG;QACH,aAAa,CAAC,EAAE,MAAM,CAAC;QACvB;;WAEG;QACH,YAAY,CAAC,EAAE,MAAM,CAAC;KACvB;WAYa,UAAU,CAAC,MAAM,EAAE,YAAY,GAAG,MAAM;WAIxC,WAAW,CAAC,OAAO,EAAE,KAAK,CAAC,YAAY,CAAC,GAAG,MAAM;IAIlD,OAAO,IAAI,OAAO,CAAC,eAAe,CAAC;YAkClC,eAAe;CAwD9B;AAED,qBAAa,iBAAkB,SAAQ,WAAW;IAChD,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAS;gBAErB,OAAO,CAAC,EAAE;QACpB,OAAO,CAAC,EAAE,MAAM,CAAC;QACjB;;WAEG;QACH,MAAM,CAAC,EAAE,MAAM,CAAC;KACjB;WAKa,GAAG,IAAI,iBAAiB;IAItC,IAAI,UAAU,IAAI,MAAM,CAEvB;CACF;AAED,qBAAa,YAAY;IAChB,QAAQ,EAAE,MAAM,CAAC;IACjB,SAAS,CAAC,EAAE,MAAM,CAAC;gBAEP,OAAO,EAAE;QAC1B;;WAEG;QACH,QAAQ,EAAE,MAAM,CAAC;QACjB;;WAEG;QACH,SAAS,CAAC,EAAE,MAAM,CAAC;KACpB;CAIF"}
|
package/build/index.js
CHANGED
|
@@ -1,12 +1,3 @@
|
|
|
1
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
1
|
import { ConnectionState, ReadOptions } from "@psdk/frame-father";
|
|
11
2
|
export class Webusb {
|
|
12
3
|
constructor(options) {
|
|
@@ -18,8 +9,8 @@ export class Webusb {
|
|
|
18
9
|
this.configuration = (_a = options.configuration) !== null && _a !== void 0 ? _a : 1;
|
|
19
10
|
this.interface = (_b = options.interface) !== null && _b !== void 0 ? _b : 0;
|
|
20
11
|
this.state = ConnectionState.DISCONNECTED;
|
|
21
|
-
this.
|
|
22
|
-
this.
|
|
12
|
+
this.inputReadEndpoint = (_c = options.readEndpoint) !== null && _c !== void 0 ? _c : 2;
|
|
13
|
+
this.inputWriteEndpoint = (_d = options.writeEndpoint) !== null && _d !== void 0 ? _d : 2;
|
|
23
14
|
}
|
|
24
15
|
static withFilter(filter) {
|
|
25
16
|
return this.withFilters([filter]);
|
|
@@ -27,90 +18,85 @@ export class Webusb {
|
|
|
27
18
|
static withFilters(filters) {
|
|
28
19
|
return new Webusb({ filters });
|
|
29
20
|
}
|
|
30
|
-
connect() {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
21
|
+
async connect() {
|
|
22
|
+
// @ts-ignore
|
|
23
|
+
if (!navigator.usb) {
|
|
24
|
+
throw new Error('Your browser not support webusb');
|
|
25
|
+
}
|
|
26
|
+
// @ts-ignore
|
|
27
|
+
const device = await navigator.usb.requestDevice({
|
|
28
|
+
filters: this.filters,
|
|
29
|
+
});
|
|
30
|
+
if (!device)
|
|
31
|
+
throw new Error('Failed to get device');
|
|
32
|
+
await device.open();
|
|
33
|
+
await device.selectConfiguration(this.configuration);
|
|
34
|
+
await device.claimInterface(device.configuration.interfaces[this.interface].interfaceNumber);
|
|
35
|
+
const alternates = device.configuration.interfaces[0].alternates[0];
|
|
36
|
+
const inEndpoints = alternates.endpoints.filter(endpoint => endpoint.direction === 'in');
|
|
37
|
+
const outEndpoints = alternates.endpoints.filter(endpoint => endpoint.direction === 'out');
|
|
38
|
+
this.readEndpoint = inEndpoints && inEndpoints.length ? inEndpoints[0].endpointNumber : this.inputReadEndpoint;
|
|
39
|
+
this.writeEndpoint = outEndpoints && outEndpoints.length ? outEndpoints[0].endpointNumber : this.inputWriteEndpoint;
|
|
40
|
+
this.state = ConnectionState.CONNECTED;
|
|
41
|
+
// @ts-ignore
|
|
42
|
+
navigator.usb.addEventListener('connect', e => {
|
|
45
43
|
this.state = ConnectionState.CONNECTED;
|
|
46
|
-
// @ts-ignore
|
|
47
|
-
navigator.usb.addEventListener('connect', e => {
|
|
48
|
-
this.state = ConnectionState.CONNECTED;
|
|
49
|
-
});
|
|
50
|
-
// @ts-ignore
|
|
51
|
-
navigator.usb.addEventListener('disconnect', e => {
|
|
52
|
-
this.state = ConnectionState.DISCONNECTED;
|
|
53
|
-
});
|
|
54
|
-
this.device = device;
|
|
55
|
-
return yield this.connectedDevice();
|
|
56
44
|
});
|
|
45
|
+
// @ts-ignore
|
|
46
|
+
navigator.usb.addEventListener('disconnect', e => {
|
|
47
|
+
this.state = ConnectionState.DISCONNECTED;
|
|
48
|
+
});
|
|
49
|
+
this.device = device;
|
|
50
|
+
return await this.connectedDevice();
|
|
57
51
|
}
|
|
58
|
-
connectedDevice() {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
return ConnectionState.DISCONNECTED;
|
|
72
|
-
}
|
|
73
|
-
return _connectionState();
|
|
74
|
-
}
|
|
75
|
-
deviceName() {
|
|
76
|
-
const device = _device();
|
|
77
|
-
return device.productName;
|
|
78
|
-
}
|
|
79
|
-
disconnect() {
|
|
80
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
81
|
-
// todo: disconnect webusb
|
|
82
|
-
});
|
|
83
|
-
}
|
|
84
|
-
canRead() {
|
|
85
|
-
return true;
|
|
86
|
-
}
|
|
87
|
-
read(options) {
|
|
88
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
89
|
-
const device = _device();
|
|
90
|
-
const _options = options !== null && options !== void 0 ? options : WebusbReadOptions.def();
|
|
91
|
-
const result = yield device.transferIn(_readEndpoint(), _options.readLength);
|
|
92
|
-
const { status, data } = result;
|
|
93
|
-
if (status != 'ok') {
|
|
94
|
-
throw new Error(`[${status}]: Failed to read data. about status code you can read this https://developer.mozilla.org/en-US/docs/Web/API/USBInTransferResult`);
|
|
95
|
-
}
|
|
96
|
-
const arrs = [];
|
|
97
|
-
const len = data.byteLength;
|
|
98
|
-
for (let i = 0; i < len; i++) {
|
|
99
|
-
arrs.push(data.getUint8(i));
|
|
100
|
-
}
|
|
101
|
-
return new Uint8Array(arrs);
|
|
102
|
-
});
|
|
52
|
+
async connectedDevice() {
|
|
53
|
+
const _device = () => this.device;
|
|
54
|
+
const _connectionState = () => this.state;
|
|
55
|
+
const _readEndpoint = () => this.readEndpoint;
|
|
56
|
+
const _writeEndpoint = () => this.writeEndpoint;
|
|
57
|
+
return new class {
|
|
58
|
+
origin() {
|
|
59
|
+
return _device();
|
|
60
|
+
}
|
|
61
|
+
connectionState() {
|
|
62
|
+
const device = _device();
|
|
63
|
+
if (!device.opened) {
|
|
64
|
+
return ConnectionState.DISCONNECTED;
|
|
103
65
|
}
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
66
|
+
return _connectionState();
|
|
67
|
+
}
|
|
68
|
+
deviceName() {
|
|
69
|
+
const device = _device();
|
|
70
|
+
return device.productName;
|
|
71
|
+
}
|
|
72
|
+
async disconnect() {
|
|
73
|
+
// todo: disconnect webusb
|
|
74
|
+
}
|
|
75
|
+
canRead() {
|
|
76
|
+
return true;
|
|
77
|
+
}
|
|
78
|
+
async read(options) {
|
|
79
|
+
const device = _device();
|
|
80
|
+
const _options = options !== null && options !== void 0 ? options : WebusbReadOptions.def();
|
|
81
|
+
const result = await device.transferIn(_readEndpoint(), _options.readLength);
|
|
82
|
+
const { status, data } = result;
|
|
83
|
+
if (status != 'ok') {
|
|
84
|
+
throw new Error(`[${status}]: Failed to read data. about status code you can read this https://developer.mozilla.org/en-US/docs/Web/API/USBInTransferResult`);
|
|
109
85
|
}
|
|
110
|
-
|
|
86
|
+
const arrs = [];
|
|
87
|
+
const len = data.byteLength;
|
|
88
|
+
for (let i = 0; i < len; i++) {
|
|
89
|
+
arrs.push(data.getUint8(i));
|
|
111
90
|
}
|
|
112
|
-
|
|
113
|
-
|
|
91
|
+
return new Uint8Array(arrs);
|
|
92
|
+
}
|
|
93
|
+
async write(data) {
|
|
94
|
+
const device = _device();
|
|
95
|
+
await device.transferOut(_writeEndpoint(), data);
|
|
96
|
+
}
|
|
97
|
+
flush() {
|
|
98
|
+
}
|
|
99
|
+
};
|
|
114
100
|
}
|
|
115
101
|
}
|
|
116
102
|
export class WebusbReadOptions extends ReadOptions {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@psdk/device-webusb",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.4",
|
|
4
4
|
"description": "psdk",
|
|
5
5
|
"main": "build/index.js",
|
|
6
6
|
"types": "build/index.d.ts",
|
|
@@ -23,11 +23,11 @@
|
|
|
23
23
|
"typescript": "^5.4.5"
|
|
24
24
|
},
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"@psdk/frame-father": "0.3.
|
|
26
|
+
"@psdk/frame-father": "0.3.4"
|
|
27
27
|
},
|
|
28
28
|
"files": [
|
|
29
29
|
"build",
|
|
30
30
|
"!build/browser/psdk*"
|
|
31
31
|
],
|
|
32
|
-
"gitHead": "
|
|
32
|
+
"gitHead": "b8c2fdcb52f1bd3e9d75d004d37d717bd465b4c9"
|
|
33
33
|
}
|