@psdk/device-webusb 0.3.3 → 0.4.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/build/index.d.ts +4 -2
- package/build/index.d.ts.map +1 -1
- package/build/index.js +102 -100
- package/package.json +7 -9
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,CA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1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DeviceFilter = exports.WebusbReadOptions = exports.Webusb = void 0;
|
|
4
|
+
const frame_father_1 = require("@psdk/frame-father");
|
|
5
|
+
class Webusb {
|
|
6
|
+
filters;
|
|
7
|
+
configuration;
|
|
8
|
+
interface;
|
|
9
|
+
readEndpoint;
|
|
10
|
+
writeEndpoint;
|
|
11
|
+
inputReadEndpoint;
|
|
12
|
+
inputWriteEndpoint;
|
|
13
|
+
state;
|
|
14
|
+
device;
|
|
12
15
|
constructor(options) {
|
|
13
|
-
var _a, _b, _c, _d;
|
|
14
16
|
if (options.filters.length == 0) {
|
|
15
17
|
throw new Error('Missing filters');
|
|
16
18
|
}
|
|
17
19
|
this.filters = options.filters;
|
|
18
|
-
this.configuration =
|
|
19
|
-
this.interface =
|
|
20
|
-
this.state = ConnectionState.DISCONNECTED;
|
|
21
|
-
this.
|
|
22
|
-
this.
|
|
20
|
+
this.configuration = options.configuration ?? 1;
|
|
21
|
+
this.interface = options.interface ?? 0;
|
|
22
|
+
this.state = frame_father_1.ConnectionState.DISCONNECTED;
|
|
23
|
+
this.inputReadEndpoint = options.readEndpoint ?? 2;
|
|
24
|
+
this.inputWriteEndpoint = options.writeEndpoint ?? 2;
|
|
23
25
|
}
|
|
24
26
|
static withFilter(filter) {
|
|
25
27
|
return this.withFilters([filter]);
|
|
@@ -27,97 +29,93 @@ export class Webusb {
|
|
|
27
29
|
static withFilters(filters) {
|
|
28
30
|
return new Webusb({ filters });
|
|
29
31
|
}
|
|
30
|
-
connect() {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
});
|
|
54
|
-
this.device = device;
|
|
55
|
-
return yield this.connectedDevice();
|
|
32
|
+
async connect() {
|
|
33
|
+
// @ts-ignore
|
|
34
|
+
if (!navigator.usb) {
|
|
35
|
+
throw new Error('Your browser not support webusb');
|
|
36
|
+
}
|
|
37
|
+
// @ts-ignore
|
|
38
|
+
const device = await navigator.usb.requestDevice({
|
|
39
|
+
filters: this.filters,
|
|
40
|
+
});
|
|
41
|
+
if (!device)
|
|
42
|
+
throw new Error('Failed to get device');
|
|
43
|
+
await device.open();
|
|
44
|
+
await device.selectConfiguration(this.configuration);
|
|
45
|
+
await device.claimInterface(device.configuration.interfaces[this.interface].interfaceNumber);
|
|
46
|
+
const alternates = device.configuration.interfaces[0].alternates[0];
|
|
47
|
+
const inEndpoints = alternates.endpoints.filter(endpoint => endpoint.direction === 'in');
|
|
48
|
+
const outEndpoints = alternates.endpoints.filter(endpoint => endpoint.direction === 'out');
|
|
49
|
+
this.readEndpoint = inEndpoints && inEndpoints.length ? inEndpoints[0].endpointNumber : this.inputReadEndpoint;
|
|
50
|
+
this.writeEndpoint = outEndpoints && outEndpoints.length ? outEndpoints[0].endpointNumber : this.inputWriteEndpoint;
|
|
51
|
+
this.state = frame_father_1.ConnectionState.CONNECTED;
|
|
52
|
+
// @ts-ignore
|
|
53
|
+
navigator.usb.addEventListener('connect', e => {
|
|
54
|
+
this.state = frame_father_1.ConnectionState.CONNECTED;
|
|
56
55
|
});
|
|
56
|
+
// @ts-ignore
|
|
57
|
+
navigator.usb.addEventListener('disconnect', e => {
|
|
58
|
+
this.state = frame_father_1.ConnectionState.DISCONNECTED;
|
|
59
|
+
});
|
|
60
|
+
this.device = device;
|
|
61
|
+
return await this.connectedDevice();
|
|
57
62
|
}
|
|
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
|
-
});
|
|
63
|
+
async connectedDevice() {
|
|
64
|
+
const _device = () => this.device;
|
|
65
|
+
const _connectionState = () => this.state;
|
|
66
|
+
const _readEndpoint = () => this.readEndpoint;
|
|
67
|
+
const _writeEndpoint = () => this.writeEndpoint;
|
|
68
|
+
return new class {
|
|
69
|
+
origin() {
|
|
70
|
+
return _device();
|
|
71
|
+
}
|
|
72
|
+
connectionState() {
|
|
73
|
+
const device = _device();
|
|
74
|
+
if (!device.opened) {
|
|
75
|
+
return frame_father_1.ConnectionState.DISCONNECTED;
|
|
103
76
|
}
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
77
|
+
return _connectionState();
|
|
78
|
+
}
|
|
79
|
+
deviceName() {
|
|
80
|
+
const device = _device();
|
|
81
|
+
return device.productName;
|
|
82
|
+
}
|
|
83
|
+
async disconnect() {
|
|
84
|
+
// todo: disconnect webusb
|
|
85
|
+
}
|
|
86
|
+
canRead() {
|
|
87
|
+
return true;
|
|
88
|
+
}
|
|
89
|
+
async read(options) {
|
|
90
|
+
const device = _device();
|
|
91
|
+
const _options = options ?? WebusbReadOptions.def();
|
|
92
|
+
const result = await device.transferIn(_readEndpoint(), _options.readLength);
|
|
93
|
+
const { status, data } = result;
|
|
94
|
+
if (status != 'ok') {
|
|
95
|
+
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
96
|
}
|
|
110
|
-
|
|
97
|
+
const arrs = [];
|
|
98
|
+
const len = data.byteLength;
|
|
99
|
+
for (let i = 0; i < len; i++) {
|
|
100
|
+
arrs.push(data.getUint8(i));
|
|
111
101
|
}
|
|
112
|
-
|
|
113
|
-
|
|
102
|
+
return new Uint8Array(arrs);
|
|
103
|
+
}
|
|
104
|
+
async write(data) {
|
|
105
|
+
const device = _device();
|
|
106
|
+
await device.transferOut(_writeEndpoint(), data);
|
|
107
|
+
}
|
|
108
|
+
flush() {
|
|
109
|
+
}
|
|
110
|
+
};
|
|
114
111
|
}
|
|
115
112
|
}
|
|
116
|
-
|
|
113
|
+
exports.Webusb = Webusb;
|
|
114
|
+
class WebusbReadOptions extends frame_father_1.ReadOptions {
|
|
115
|
+
_length;
|
|
117
116
|
constructor(options) {
|
|
118
|
-
var _a;
|
|
119
117
|
super(options);
|
|
120
|
-
this._length =
|
|
118
|
+
this._length = options?.length ?? 1024;
|
|
121
119
|
}
|
|
122
120
|
static def() {
|
|
123
121
|
return new WebusbReadOptions();
|
|
@@ -126,9 +124,13 @@ export class WebusbReadOptions extends ReadOptions {
|
|
|
126
124
|
return this._length;
|
|
127
125
|
}
|
|
128
126
|
}
|
|
129
|
-
|
|
127
|
+
exports.WebusbReadOptions = WebusbReadOptions;
|
|
128
|
+
class DeviceFilter {
|
|
129
|
+
vendorId;
|
|
130
|
+
productId;
|
|
130
131
|
constructor(options) {
|
|
131
132
|
this.vendorId = options.vendorId;
|
|
132
133
|
this.productId = options.productId;
|
|
133
134
|
}
|
|
134
135
|
}
|
|
136
|
+
exports.DeviceFilter = DeviceFilter;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@psdk/device-webusb",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "psdk",
|
|
5
5
|
"main": "build/index.js",
|
|
6
6
|
"types": "build/index.d.ts",
|
|
@@ -12,22 +12,20 @@
|
|
|
12
12
|
},
|
|
13
13
|
"author": "",
|
|
14
14
|
"devDependencies": {
|
|
15
|
-
"@babel/core": "^7.24.5",
|
|
16
|
-
"@babel/preset-env": "^7.24.5",
|
|
17
|
-
"@babel/preset-typescript": "^7.24.1",
|
|
18
15
|
"@types/jest": "^29.5.12",
|
|
19
16
|
"@vercel/ncc": "^0.38.1",
|
|
20
|
-
"babel-jest": "^29.7.0",
|
|
21
17
|
"jest": "^29.7.0",
|
|
22
|
-
"
|
|
23
|
-
"
|
|
18
|
+
"ts-jest": "^29.1.5",
|
|
19
|
+
"ts-node": "^10.9.2",
|
|
20
|
+
"typedoc": "^0.26.4",
|
|
21
|
+
"typescript": "^5.5.3"
|
|
24
22
|
},
|
|
25
23
|
"dependencies": {
|
|
26
|
-
"@psdk/frame-father": "0.
|
|
24
|
+
"@psdk/frame-father": "0.4.0"
|
|
27
25
|
},
|
|
28
26
|
"files": [
|
|
29
27
|
"build",
|
|
30
28
|
"!build/browser/psdk*"
|
|
31
29
|
],
|
|
32
|
-
"gitHead": "
|
|
30
|
+
"gitHead": "13fa8cc6aacbf8f6bacb5f485da11a7610001f61"
|
|
33
31
|
}
|