webserial-core 1.0.5 → 1.0.6
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/dist/Core.d.ts +183 -0
- package/dist/Core.d.ts.map +1 -0
- package/dist/Devices.d.ts +24 -0
- package/dist/Devices.d.ts.map +1 -0
- package/dist/Dispatcher.d.ts +30 -0
- package/dist/Dispatcher.d.ts.map +1 -0
- package/dist/SerialEvent.d.ts +4 -0
- package/dist/SerialEvent.d.ts.map +1 -0
- package/dist/main.d.ts +13 -0
- package/dist/main.d.ts.map +1 -0
- package/dist/utils.d.ts +3 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/webserial-core.js +182 -209
- package/dist/webserial-core.umd.cjs +3 -3
- package/package.json +12 -9
package/dist/Core.d.ts
ADDED
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import { Dispatcher } from "./Dispatcher.ts";
|
|
2
|
+
interface LastError {
|
|
3
|
+
message: string | null;
|
|
4
|
+
action: string | null;
|
|
5
|
+
code: number | Array<Uint8Array> | Array<string> | null;
|
|
6
|
+
no_code: number;
|
|
7
|
+
}
|
|
8
|
+
interface DeviceData {
|
|
9
|
+
type: string;
|
|
10
|
+
id: string;
|
|
11
|
+
listen_on_port: number | null;
|
|
12
|
+
}
|
|
13
|
+
type SerialResponseAs = "hex" | "uint8" | "string" | "arraybuffer";
|
|
14
|
+
interface SerialResponse {
|
|
15
|
+
length: number | null;
|
|
16
|
+
buffer: Uint8Array;
|
|
17
|
+
as: SerialResponseAs;
|
|
18
|
+
replacer: RegExp | string;
|
|
19
|
+
limiter: null | string | RegExp;
|
|
20
|
+
}
|
|
21
|
+
interface QueueData {
|
|
22
|
+
bytes: string[];
|
|
23
|
+
action: string;
|
|
24
|
+
}
|
|
25
|
+
type SerialData = {
|
|
26
|
+
connected: boolean;
|
|
27
|
+
port: SerialPort | null;
|
|
28
|
+
last_action: string | null;
|
|
29
|
+
response: SerialResponse;
|
|
30
|
+
reader: ReadableStreamDefaultReader<Uint8Array> | null;
|
|
31
|
+
input_done: Promise<void> | null;
|
|
32
|
+
output_done: Promise<void> | null;
|
|
33
|
+
input_stream: ReadableStream<Uint8Array> | null;
|
|
34
|
+
output_stream: WritableStream<Uint8Array> | null;
|
|
35
|
+
keep_reading: boolean;
|
|
36
|
+
time_until_send_bytes: number | undefined | ReturnType<typeof setTimeout>;
|
|
37
|
+
delay_first_connection: number;
|
|
38
|
+
bytes_connection: Array<string> | null;
|
|
39
|
+
filters: SerialPortFilter[];
|
|
40
|
+
config_port: SerialOptions;
|
|
41
|
+
queue: QueueData[];
|
|
42
|
+
auto_response: any;
|
|
43
|
+
};
|
|
44
|
+
interface TimeResponse {
|
|
45
|
+
response_connection: number;
|
|
46
|
+
response_general: number;
|
|
47
|
+
}
|
|
48
|
+
interface Timeout {
|
|
49
|
+
until_response: number | ReturnType<typeof setTimeout>;
|
|
50
|
+
}
|
|
51
|
+
interface InternalIntervals {
|
|
52
|
+
reconnection: number;
|
|
53
|
+
}
|
|
54
|
+
export type Internal = {
|
|
55
|
+
auto_response: boolean;
|
|
56
|
+
device_number: number;
|
|
57
|
+
aux_port_connector: number;
|
|
58
|
+
last_error: LastError;
|
|
59
|
+
serial: SerialData;
|
|
60
|
+
device: DeviceData;
|
|
61
|
+
time: TimeResponse;
|
|
62
|
+
timeout: Timeout;
|
|
63
|
+
interval: InternalIntervals;
|
|
64
|
+
};
|
|
65
|
+
interface CoreConstructorParams {
|
|
66
|
+
filters?: SerialPortFilter[] | null;
|
|
67
|
+
config_port?: SerialOptions;
|
|
68
|
+
no_device?: number;
|
|
69
|
+
device_listen_on_channel?: number | string;
|
|
70
|
+
}
|
|
71
|
+
interface CustomCode {
|
|
72
|
+
code: Array<string>;
|
|
73
|
+
}
|
|
74
|
+
interface ICore {
|
|
75
|
+
lastAction: string | null;
|
|
76
|
+
set listenOnChannel(channel: string | number);
|
|
77
|
+
set serialFilters(filters: SerialPortFilter[]);
|
|
78
|
+
get serialFilters(): SerialPortFilter[];
|
|
79
|
+
set serialConfigPort(config_port: SerialOptions);
|
|
80
|
+
get serialConfigPort(): SerialOptions;
|
|
81
|
+
get isConnected(): boolean;
|
|
82
|
+
get isDisconnected(): boolean;
|
|
83
|
+
get deviceNumber(): number;
|
|
84
|
+
get uuid(): string;
|
|
85
|
+
get typeDevice(): string;
|
|
86
|
+
get queue(): QueueData[];
|
|
87
|
+
timeout(bytes: string[], event: string): Promise<void>;
|
|
88
|
+
disconnect(detail?: null): Promise<void>;
|
|
89
|
+
connect(): Promise<string>;
|
|
90
|
+
serialDisconnect(): Promise<void>;
|
|
91
|
+
serialPortsSaved(ports: SerialPort[]): Promise<void>;
|
|
92
|
+
serialErrors(error: unknown | Error | DOMException): void;
|
|
93
|
+
serialConnect(): Promise<void>;
|
|
94
|
+
serialForget(): Promise<boolean>;
|
|
95
|
+
decToHex(dec: number | string): string;
|
|
96
|
+
hexToDec(hex: string): number;
|
|
97
|
+
hexMaker(val?: string, min?: number): string;
|
|
98
|
+
add0x(bytes: string[]): string[];
|
|
99
|
+
bytesToHex(bytes: string[]): string[];
|
|
100
|
+
appendToQueue(arr: string[], action: string): Promise<void>;
|
|
101
|
+
serialSetConnectionConstant(listen_on_port?: number): never[] | string[];
|
|
102
|
+
serialMessage(hex: string[]): void;
|
|
103
|
+
serialCorruptMessage(code: string[], data: never | null): void;
|
|
104
|
+
clearSerialQueue(): void;
|
|
105
|
+
sumHex(arr: string[]): string;
|
|
106
|
+
softReload(): void;
|
|
107
|
+
sendConnect(): Promise<void>;
|
|
108
|
+
sendCustomCode({ code }: {
|
|
109
|
+
code: CustomCode;
|
|
110
|
+
}): Promise<void>;
|
|
111
|
+
stringToArrayHex(string: string): string[];
|
|
112
|
+
stringToArrayBuffer(string: string, end: string): ArrayBufferLike;
|
|
113
|
+
parseStringToBytes(string: string, end: string): string[];
|
|
114
|
+
parseUint8ToHex(array: Uint8Array): string[];
|
|
115
|
+
parseHexToUint8(array: string[]): Uint8Array;
|
|
116
|
+
stringArrayToUint8Array(strings: string[]): Uint8Array;
|
|
117
|
+
parseUint8ArrayToString(array: string[]): string;
|
|
118
|
+
parseStringToTextEncoder(string: string, end: string): Uint8Array;
|
|
119
|
+
hexToAscii(hex: string | number): string;
|
|
120
|
+
asciiToHex(asciiString: string): string;
|
|
121
|
+
getResponseAsArrayBuffer(): void;
|
|
122
|
+
getResponseAsArrayHex(): void;
|
|
123
|
+
getResponseAsUint8Array(): void;
|
|
124
|
+
getResponseAsString(): void;
|
|
125
|
+
}
|
|
126
|
+
export declare class Core extends Dispatcher implements ICore {
|
|
127
|
+
#private;
|
|
128
|
+
protected __internal__: Internal;
|
|
129
|
+
constructor({ filters, config_port, no_device, device_listen_on_channel, }?: CoreConstructorParams);
|
|
130
|
+
set listenOnChannel(channel: string | number);
|
|
131
|
+
get lastAction(): string | null;
|
|
132
|
+
get listenOnChannel(): number;
|
|
133
|
+
set serialFilters(filters: SerialPortFilter[]);
|
|
134
|
+
get serialFilters(): SerialPortFilter[];
|
|
135
|
+
set serialConfigPort(config_port: SerialOptions);
|
|
136
|
+
get serialConfigPort(): SerialOptions;
|
|
137
|
+
get isConnected(): boolean;
|
|
138
|
+
get isDisconnected(): boolean;
|
|
139
|
+
get deviceNumber(): number;
|
|
140
|
+
get uuid(): string;
|
|
141
|
+
get typeDevice(): string;
|
|
142
|
+
get queue(): QueueData[];
|
|
143
|
+
timeout(bytes: string[], event: string): Promise<void>;
|
|
144
|
+
disconnect(detail?: null): Promise<void>;
|
|
145
|
+
connect(): Promise<string>;
|
|
146
|
+
serialDisconnect(): Promise<void>;
|
|
147
|
+
getResponseAsArrayBuffer(): void;
|
|
148
|
+
getResponseAsArrayHex(): void;
|
|
149
|
+
getResponseAsUint8Array(): void;
|
|
150
|
+
getResponseAsString(): void;
|
|
151
|
+
serialPortsSaved(ports: SerialPort[]): Promise<void>;
|
|
152
|
+
serialErrors(error: any): void;
|
|
153
|
+
serialConnect(): Promise<void>;
|
|
154
|
+
serialForget(): Promise<boolean>;
|
|
155
|
+
decToHex(dec: number | string): string;
|
|
156
|
+
hexToDec(hex: string): number;
|
|
157
|
+
hexMaker(val?: string, min?: number): string;
|
|
158
|
+
add0x(bytes: string[]): string[];
|
|
159
|
+
bytesToHex(bytes: string[]): string[];
|
|
160
|
+
appendToQueue(arr: string[], action: string): Promise<void>;
|
|
161
|
+
serialSetConnectionConstant(listen_on_port?: number): never[] | string[];
|
|
162
|
+
serialMessage(hex: string[] | Uint8Array<ArrayBufferLike> | string | ArrayBuffer): void;
|
|
163
|
+
serialCorruptMessage(code: string[], data: never | null): void;
|
|
164
|
+
clearSerialQueue(): void;
|
|
165
|
+
sumHex(arr: string[]): string;
|
|
166
|
+
toString(): string;
|
|
167
|
+
softReload(): void;
|
|
168
|
+
sendConnect(): Promise<void>;
|
|
169
|
+
sendCustomCode({ code }?: CustomCode): Promise<void>;
|
|
170
|
+
stringToArrayHex(string: string): string[];
|
|
171
|
+
stringToArrayBuffer(string: string, end?: string): ArrayBufferLike;
|
|
172
|
+
parseStringToTextEncoder(string?: string, end?: string): Uint8Array;
|
|
173
|
+
parseStringToBytes(string?: string, end?: string): string[];
|
|
174
|
+
parseUint8ToHex(array: Uint8Array): string[];
|
|
175
|
+
parseHexToUint8(array: string[]): Uint8Array;
|
|
176
|
+
stringArrayToUint8Array(strings: string[]): Uint8Array;
|
|
177
|
+
parseUint8ArrayToString(array: string[]): string;
|
|
178
|
+
hexToAscii(hex: string | number): string;
|
|
179
|
+
asciiToHex(asciiString: string): string;
|
|
180
|
+
$checkAndDispatchConnection(): boolean;
|
|
181
|
+
}
|
|
182
|
+
export {};
|
|
183
|
+
//# sourceMappingURL=Core.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Core.d.ts","sourceRoot":"","sources":["../lib/Core.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAI7C,UAAU,SAAS;IACjB,OAAO,EAAE,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,IAAI,IAAI;IAKZ,WAAW,IAAI,OAAO,CAAC,IAAI,CAAC;IAQ5B,cAAc,CAAC,EAAE,IAAS,EAAE,GAAE,UAAyB,GAAG,OAAO,CAAC,IAAI,CAAC;IAO7E,gBAAgB,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE;IAI1C,mBAAmB,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,GAAE,MAAa,GAAG,eAAe;IAIxE,wBAAwB,CAAC,MAAM,GAAE,MAAW,EAAE,GAAG,GAAE,MAAa,GAAG,UAAU;IAM7E,kBAAkB,CAAC,MAAM,GAAE,MAAW,EAAE,GAAG,GAAE,MAAa,GAAG,MAAM,EAAE;IAKrE,eAAe,CAAC,KAAK,EAAE,UAAU,GAAG,MAAM,EAAE;IAI5C,eAAe,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,UAAU;IAI5C,uBAAuB,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,UAAU;IAUtD,uBAAuB,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,MAAM;IAUhD,UAAU,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM;IASxC,UAAU,CAAC,WAAW,EAAE,MAAM,GAAG,MAAM;IASvC,2BAA2B,IAAI,OAAO;CAGvC"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { Core } from "./Core.ts";
|
|
2
|
+
import { Dispatcher } from "./Dispatcher.ts";
|
|
3
|
+
interface IDevice {
|
|
4
|
+
[key: string]: Core;
|
|
5
|
+
}
|
|
6
|
+
interface IDevices {
|
|
7
|
+
[key: string]: IDevice;
|
|
8
|
+
}
|
|
9
|
+
export declare class Devices extends Dispatcher {
|
|
10
|
+
static instance: Devices;
|
|
11
|
+
static devices: IDevices;
|
|
12
|
+
constructor();
|
|
13
|
+
static $dispatchChange(device?: Core | null): void;
|
|
14
|
+
static typeError(type: string): void;
|
|
15
|
+
static registerType(type: string): void;
|
|
16
|
+
static add(device: Core): number;
|
|
17
|
+
static get(type: string, id: string): Core;
|
|
18
|
+
static getAll(type?: string | null): IDevice | IDevices;
|
|
19
|
+
static getList(): Core[];
|
|
20
|
+
static getByNumber(type: string, device_number: number): Core | null;
|
|
21
|
+
static getCustom(type: string, device_number?: number): Core | null;
|
|
22
|
+
}
|
|
23
|
+
export {};
|
|
24
|
+
//# sourceMappingURL=Devices.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Devices.d.ts","sourceRoot":"","sources":["../lib/Devices.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAE7C,UAAU,OAAO;IACf,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI,CAAC;CACrB;AAED,UAAU,QAAQ;IAChB,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;CACxB;AAED,qBAAa,OAAQ,SAAQ,UAAU;IACrC,MAAM,CAAC,QAAQ,EAAE,OAAO,CAAC;IACzB,MAAM,CAAC,OAAO,EAAE,QAAQ,CAAM;;IAY9B,MAAM,CAAC,eAAe,CAAC,MAAM,GAAE,IAAI,GAAG,IAAW,GAAG,IAAI;IAOxD,MAAM,CAAC,SAAS,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI;IAOpC,MAAM,CAAC,YAAY,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI;IAMvC,MAAM,CAAC,GAAG,CAAC,MAAM,EAAE,IAAI,GAAG,MAAM;IAoBhC,MAAM,CAAC,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,GAAG,IAAI;IAU1C,MAAM,CAAC,MAAM,CAAC,IAAI,GAAE,MAAM,GAAG,IAAW,GAAG,OAAO,GAAG,QAAQ;IAO7D,MAAM,CAAC,OAAO,IAAI,IAAI,EAAE;IAWxB,MAAM,CAAC,WAAW,CAAC,IAAI,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,GAAG,IAAI,GAAG,IAAI;IAOpE,MAAM,CAAC,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,aAAa,GAAE,MAAU,GAAG,IAAI,GAAG,IAAI;CAMvE"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
type AvailableListener = {
|
|
2
|
+
type: string;
|
|
3
|
+
listening: boolean;
|
|
4
|
+
};
|
|
5
|
+
type AvailableListeners = AvailableListener[];
|
|
6
|
+
type DataType = string | number | boolean | object | null;
|
|
7
|
+
interface IDispatcher {
|
|
8
|
+
dispatch(type: string, data?: DataType): void;
|
|
9
|
+
dispatchAsync(type: string, data?: DataType, ms?: number): void;
|
|
10
|
+
on(type: string, callback: EventListener): void;
|
|
11
|
+
off(type: string, callback: EventListener): void;
|
|
12
|
+
serialRegisterAvailableListener(type: string): void;
|
|
13
|
+
availableListeners: AvailableListeners;
|
|
14
|
+
}
|
|
15
|
+
interface Listeners {
|
|
16
|
+
[key: string]: boolean;
|
|
17
|
+
debug: boolean;
|
|
18
|
+
}
|
|
19
|
+
export declare class Dispatcher extends EventTarget implements IDispatcher {
|
|
20
|
+
__listeners__: Listeners;
|
|
21
|
+
__debug__: boolean;
|
|
22
|
+
dispatch(type: string, data?: DataType): void;
|
|
23
|
+
dispatchAsync(type: string, data?: null, ms?: number): void;
|
|
24
|
+
on(type: string, callback: EventListenerOrEventListenerObject): void;
|
|
25
|
+
off(type: string, callback: EventListenerOrEventListenerObject): void;
|
|
26
|
+
serialRegisterAvailableListener(type: string): void;
|
|
27
|
+
get availableListeners(): AvailableListeners;
|
|
28
|
+
}
|
|
29
|
+
export {};
|
|
30
|
+
//# sourceMappingURL=Dispatcher.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Dispatcher.d.ts","sourceRoot":"","sources":["../lib/Dispatcher.ts"],"names":[],"mappings":"AAEA,KAAK,iBAAiB,GAAG;IAAE,IAAI,EAAE,MAAM,CAAC;I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 -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SerialEvent.d.ts","sourceRoot":"","sources":["../lib/SerialEvent.ts"],"names":[],"mappings":"AAAA,qBAAa,WAAY,SAAQ,WAAW,CAAC,WAAW,CAAE,YAAW,WAAW;gBAClE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,eAAe;CAGnD"}
|
package/dist/main.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license webserial-core
|
|
3
|
+
* webserial-core
|
|
4
|
+
*
|
|
5
|
+
* Created by (c) Danidoble.
|
|
6
|
+
*
|
|
7
|
+
* This source code is licensed under the MIT license found in the
|
|
8
|
+
* LICENSE file in the root directory of this source tree.
|
|
9
|
+
*/
|
|
10
|
+
export { Core } from "./Core";
|
|
11
|
+
export { Devices } from "./Devices";
|
|
12
|
+
export { Dispatcher } from "./Dispatcher";
|
|
13
|
+
//# sourceMappingURL=main.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"main.d.ts","sourceRoot":"","sources":["../lib/main.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AAC9B,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC"}
|
package/dist/utils.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../lib/utils.ts"],"names":[],"mappings":"AAEA,wBAAgB,IAAI,CAAC,EAAE,GAAE,MAAY,GAAG,OAAO,CAAC,IAAI,CAAC,CAIpD;AAED,wBAAgB,gBAAgB,IAAI,OAAO,CAE1C"}
|
package/dist/webserial-core.js
CHANGED
|
@@ -1,60 +1,33 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
throw TypeError(
|
|
1
|
+
var M = Object.defineProperty;
|
|
2
|
+
var w = (l) => {
|
|
3
|
+
throw TypeError(l);
|
|
4
4
|
};
|
|
5
|
-
var
|
|
6
|
-
var
|
|
7
|
-
var
|
|
8
|
-
var
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
function B(s, i = 0) {
|
|
13
|
-
return (_[s[i + 0]] + _[s[i + 1]] + _[s[i + 2]] + _[s[i + 3]] + "-" + _[s[i + 4]] + _[s[i + 5]] + "-" + _[s[i + 6]] + _[s[i + 7]] + "-" + _[s[i + 8]] + _[s[i + 9]] + "-" + _[s[i + 10]] + _[s[i + 11]] + _[s[i + 12]] + _[s[i + 13]] + _[s[i + 14]] + _[s[i + 15]]).toLowerCase();
|
|
14
|
-
}
|
|
15
|
-
let b;
|
|
16
|
-
const F = new Uint8Array(16);
|
|
17
|
-
function V() {
|
|
18
|
-
if (!b) {
|
|
19
|
-
if (typeof crypto > "u" || !crypto.getRandomValues)
|
|
20
|
-
throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");
|
|
21
|
-
b = crypto.getRandomValues.bind(crypto);
|
|
22
|
-
}
|
|
23
|
-
return b(F);
|
|
24
|
-
}
|
|
25
|
-
const W = typeof crypto < "u" && crypto.randomUUID && crypto.randomUUID.bind(crypto), x = { randomUUID: W };
|
|
26
|
-
function Q(s, i, e) {
|
|
27
|
-
var n;
|
|
28
|
-
if (x.randomUUID && !s)
|
|
29
|
-
return x.randomUUID();
|
|
30
|
-
s = s || {};
|
|
31
|
-
const t = s.random ?? ((n = s.rng) == null ? void 0 : n.call(s)) ?? V();
|
|
32
|
-
if (t.length < 16)
|
|
33
|
-
throw new Error("Random bytes length must be >= 16");
|
|
34
|
-
return t[6] = t[6] & 15 | 64, t[8] = t[8] & 63 | 128, B(t);
|
|
35
|
-
}
|
|
36
|
-
class S extends CustomEvent {
|
|
37
|
-
constructor(i, e) {
|
|
38
|
-
super(i, e);
|
|
5
|
+
var N = (l, n, e) => n in l ? M(l, n, { enumerable: !0, configurable: !0, writable: !0, value: e }) : l[n] = e;
|
|
6
|
+
var d = (l, n, e) => N(l, typeof n != "symbol" ? n + "" : n, e), O = (l, n, e) => n.has(l) || w("Cannot " + e);
|
|
7
|
+
var m = (l, n, e) => n.has(l) ? w("Cannot add the same private member more than once") : n instanceof WeakSet ? n.add(l) : n.set(l, e);
|
|
8
|
+
var o = (l, n, e) => (O(l, n, "access private method"), e);
|
|
9
|
+
class v extends CustomEvent {
|
|
10
|
+
constructor(n, e) {
|
|
11
|
+
super(n, e);
|
|
39
12
|
}
|
|
40
13
|
}
|
|
41
|
-
class
|
|
14
|
+
class x extends EventTarget {
|
|
42
15
|
constructor() {
|
|
43
16
|
super(...arguments);
|
|
44
|
-
|
|
17
|
+
d(this, "__listeners__", {
|
|
45
18
|
debug: !1
|
|
46
19
|
});
|
|
47
|
-
|
|
20
|
+
d(this, "__debug__", !1);
|
|
48
21
|
}
|
|
49
22
|
dispatch(e, t = null) {
|
|
50
|
-
const
|
|
51
|
-
this.dispatchEvent(
|
|
23
|
+
const i = new v(e, { detail: t });
|
|
24
|
+
this.dispatchEvent(i), this.__debug__ && this.dispatchEvent(new v("debug", { detail: { type: e, data: t } }));
|
|
52
25
|
}
|
|
53
|
-
dispatchAsync(e, t = null,
|
|
54
|
-
const
|
|
26
|
+
dispatchAsync(e, t = null, i = 100) {
|
|
27
|
+
const r = this;
|
|
55
28
|
setTimeout(() => {
|
|
56
|
-
|
|
57
|
-
},
|
|
29
|
+
r.dispatch(e, t);
|
|
30
|
+
}, i);
|
|
58
31
|
}
|
|
59
32
|
on(e, t) {
|
|
60
33
|
typeof this.__listeners__[e] < "u" && !this.__listeners__[e] && (this.__listeners__[e] = !0), this.addEventListener(e, t);
|
|
@@ -72,58 +45,58 @@ class E extends EventTarget {
|
|
|
72
45
|
}));
|
|
73
46
|
}
|
|
74
47
|
}
|
|
75
|
-
const
|
|
48
|
+
const a = class a extends x {
|
|
76
49
|
constructor() {
|
|
77
50
|
super(), ["change"].forEach((e) => {
|
|
78
51
|
this.serialRegisterAvailableListener(e);
|
|
79
52
|
});
|
|
80
53
|
}
|
|
81
|
-
static $dispatchChange(
|
|
82
|
-
|
|
54
|
+
static $dispatchChange(n = null) {
|
|
55
|
+
n && n.$checkAndDispatchConnection(), a.instance.dispatch("change", { devices: a.devices, dispatcher: n });
|
|
83
56
|
}
|
|
84
|
-
static typeError(
|
|
57
|
+
static typeError(n) {
|
|
85
58
|
const e = new Error();
|
|
86
|
-
throw e.message = `Type ${
|
|
59
|
+
throw e.message = `Type ${n} is not supported`, e.name = "DeviceTypeError", e;
|
|
87
60
|
}
|
|
88
|
-
static registerType(
|
|
89
|
-
typeof
|
|
61
|
+
static registerType(n) {
|
|
62
|
+
typeof a.devices[n] > "u" && (a.devices[n] = {});
|
|
90
63
|
}
|
|
91
|
-
static add(
|
|
92
|
-
const e =
|
|
93
|
-
typeof
|
|
94
|
-
const t =
|
|
95
|
-
if (typeof
|
|
64
|
+
static add(n) {
|
|
65
|
+
const e = n.typeDevice;
|
|
66
|
+
typeof a.devices[e] > "u" && (a.devices[e] = {});
|
|
67
|
+
const t = n.uuid;
|
|
68
|
+
if (typeof a.devices[e] > "u" && a.typeError(e), a.devices[e][t])
|
|
96
69
|
throw new Error(`Device with id ${t} already exists`);
|
|
97
|
-
return
|
|
70
|
+
return a.devices[e][t] = n, a.$dispatchChange(n), Object.keys(a.devices[e]).indexOf(t);
|
|
98
71
|
}
|
|
99
|
-
static get(
|
|
100
|
-
return typeof
|
|
72
|
+
static get(n, e) {
|
|
73
|
+
return typeof a.devices[n] > "u" && (a.devices[n] = {}), typeof a.devices[n] > "u" && a.typeError(n), a.devices[n][e];
|
|
101
74
|
}
|
|
102
|
-
static getAll(
|
|
103
|
-
return
|
|
75
|
+
static getAll(n = null) {
|
|
76
|
+
return n === null ? a.devices : (typeof a.devices[n] > "u" && a.typeError(n), a.devices[n]);
|
|
104
77
|
}
|
|
105
78
|
static getList() {
|
|
106
|
-
return Object.values(
|
|
79
|
+
return Object.values(a.devices).map((e) => Object.values(e)).flat();
|
|
107
80
|
}
|
|
108
|
-
static getByNumber(
|
|
109
|
-
return typeof
|
|
81
|
+
static getByNumber(n, e) {
|
|
82
|
+
return typeof a.devices[n] > "u" && a.typeError(n), Object.values(a.devices[n]).find((i) => i.deviceNumber === e) ?? null;
|
|
110
83
|
}
|
|
111
|
-
static getCustom(
|
|
112
|
-
return typeof
|
|
84
|
+
static getCustom(n, e = 1) {
|
|
85
|
+
return typeof a.devices[n] > "u" && a.typeError(n), Object.values(a.devices[n]).find((i) => i.deviceNumber === e) ?? null;
|
|
113
86
|
}
|
|
114
87
|
};
|
|
115
|
-
|
|
116
|
-
let
|
|
117
|
-
|
|
118
|
-
function
|
|
88
|
+
d(a, "instance"), d(a, "devices", {});
|
|
89
|
+
let _ = a;
|
|
90
|
+
_.instance || (_.instance = new _());
|
|
91
|
+
function C(l = 100) {
|
|
119
92
|
return new Promise(
|
|
120
|
-
(
|
|
93
|
+
(n) => setTimeout(() => n(), l)
|
|
121
94
|
);
|
|
122
95
|
}
|
|
123
|
-
function
|
|
96
|
+
function H() {
|
|
124
97
|
return "serial" in navigator;
|
|
125
98
|
}
|
|
126
|
-
const
|
|
99
|
+
const g = {
|
|
127
100
|
baudRate: 9600,
|
|
128
101
|
dataBits: 8,
|
|
129
102
|
stopBits: 1,
|
|
@@ -131,22 +104,22 @@ const y = {
|
|
|
131
104
|
bufferSize: 32768,
|
|
132
105
|
flowControl: "none"
|
|
133
106
|
};
|
|
134
|
-
var
|
|
135
|
-
class
|
|
107
|
+
var s, p, f, b, h, S, A, E, T, k, U, L, P, D, $, q, I;
|
|
108
|
+
class B extends x {
|
|
136
109
|
constructor({
|
|
137
110
|
filters: e = null,
|
|
138
|
-
config_port: t =
|
|
139
|
-
no_device:
|
|
140
|
-
device_listen_on_channel:
|
|
111
|
+
config_port: t = g,
|
|
112
|
+
no_device: i = 1,
|
|
113
|
+
device_listen_on_channel: r = 1
|
|
141
114
|
} = {
|
|
142
115
|
filters: null,
|
|
143
|
-
config_port:
|
|
116
|
+
config_port: g,
|
|
144
117
|
no_device: 1,
|
|
145
118
|
device_listen_on_channel: 1
|
|
146
119
|
}) {
|
|
147
120
|
super();
|
|
148
|
-
|
|
149
|
-
|
|
121
|
+
m(this, s);
|
|
122
|
+
d(this, "__internal__", {
|
|
150
123
|
auto_response: !1,
|
|
151
124
|
device_number: 1,
|
|
152
125
|
aux_port_connector: 0,
|
|
@@ -177,13 +150,13 @@ class J extends E {
|
|
|
177
150
|
delay_first_connection: 200,
|
|
178
151
|
bytes_connection: null,
|
|
179
152
|
filters: [],
|
|
180
|
-
config_port:
|
|
153
|
+
config_port: g,
|
|
181
154
|
queue: [],
|
|
182
155
|
auto_response: ["DD", "DD"]
|
|
183
156
|
},
|
|
184
157
|
device: {
|
|
185
158
|
type: "unknown",
|
|
186
|
-
id:
|
|
159
|
+
id: window.crypto.randomUUID(),
|
|
187
160
|
listen_on_port: null
|
|
188
161
|
},
|
|
189
162
|
time: {
|
|
@@ -199,7 +172,7 @@ class J extends E {
|
|
|
199
172
|
});
|
|
200
173
|
if (!("serial" in navigator))
|
|
201
174
|
throw new Error("Web Serial not supported");
|
|
202
|
-
e && (this.serialFilters = e), t && (this.serialConfigPort = t),
|
|
175
|
+
e && (this.serialFilters = e), t && (this.serialConfigPort = t), i && o(this, s, q).call(this, i), r && ["number", "string"].includes(typeof r) && (this.listenOnChannel = r), o(this, s, L).call(this), o(this, s, P).call(this);
|
|
203
176
|
}
|
|
204
177
|
set listenOnChannel(e) {
|
|
205
178
|
if (typeof e == "string" && (e = parseInt(e)), isNaN(e) || e < 1 || e > 255)
|
|
@@ -225,12 +198,12 @@ class J extends E {
|
|
|
225
198
|
return this.__internal__.serial.config_port;
|
|
226
199
|
}
|
|
227
200
|
get isConnected() {
|
|
228
|
-
const e = this.__internal__.serial.connected, t =
|
|
229
|
-
return e && !t &&
|
|
201
|
+
const e = this.__internal__.serial.connected, t = o(this, s, p).call(this, this.__internal__.serial.port);
|
|
202
|
+
return e && !t && o(this, s, f).call(this, { error: "Port is closed, not readable or writable." }), this.__internal__.serial.connected = t, this.__internal__.serial.connected;
|
|
230
203
|
}
|
|
231
204
|
get isDisconnected() {
|
|
232
|
-
const e = this.__internal__.serial.connected, t =
|
|
233
|
-
return !e && t && (this.dispatch("serial:connected"),
|
|
205
|
+
const e = this.__internal__.serial.connected, t = o(this, s, p).call(this, this.__internal__.serial.port);
|
|
206
|
+
return !e && t && (this.dispatch("serial:connected"), _.$dispatchChange(this)), this.__internal__.serial.connected = t, !this.__internal__.serial.connected;
|
|
234
207
|
}
|
|
235
208
|
get deviceNumber() {
|
|
236
209
|
return this.__internal__.device_number;
|
|
@@ -245,30 +218,30 @@ class J extends E {
|
|
|
245
218
|
return this.__internal__.serial.queue;
|
|
246
219
|
}
|
|
247
220
|
async timeout(e, t) {
|
|
248
|
-
this.__internal__.last_error.message = "Operation response timed out.", this.__internal__.last_error.action = t, this.__internal__.last_error.code = e, this.__internal__.timeout.until_response && (clearTimeout(this.__internal__.timeout.until_response), this.__internal__.timeout.until_response = 0), t === "connect" ? (this.__internal__.serial.connected = !1, this.dispatch("serial:reconnect", {}),
|
|
221
|
+
this.__internal__.last_error.message = "Operation response timed out.", this.__internal__.last_error.action = t, this.__internal__.last_error.code = e, this.__internal__.timeout.until_response && (clearTimeout(this.__internal__.timeout.until_response), this.__internal__.timeout.until_response = 0), t === "connect" ? (this.__internal__.serial.connected = !1, this.dispatch("serial:reconnect", {}), _.$dispatchChange(this)) : t === "connection:start" && (await this.serialDisconnect(), this.__internal__.serial.connected = !1, this.__internal__.aux_port_connector += 1, _.$dispatchChange(this), await this.serialConnect()), this.dispatch("serial:timeout", {
|
|
249
222
|
...this.__internal__.last_error,
|
|
250
223
|
bytes: e,
|
|
251
224
|
action: t
|
|
252
225
|
});
|
|
253
226
|
}
|
|
254
227
|
async disconnect(e = null) {
|
|
255
|
-
await this.serialDisconnect(),
|
|
228
|
+
await this.serialDisconnect(), o(this, s, f).call(this, e);
|
|
256
229
|
}
|
|
257
230
|
async connect() {
|
|
258
231
|
return new Promise((e, t) => {
|
|
259
|
-
|
|
260
|
-
await
|
|
232
|
+
H() || t("Web Serial not supported"), setTimeout(async () => {
|
|
233
|
+
await C(499), await this.serialConnect(), this.isConnected ? e(`${this.typeDevice} device ${this.deviceNumber} connected`) : t(`${this.typeDevice} device ${this.deviceNumber} not connected`);
|
|
261
234
|
}, 1);
|
|
262
235
|
});
|
|
263
236
|
}
|
|
264
237
|
async serialDisconnect() {
|
|
265
238
|
try {
|
|
266
239
|
const e = this.__internal__.serial.reader, t = this.__internal__.serial.output_stream;
|
|
267
|
-
e && (await e.cancel().catch((
|
|
240
|
+
e && (await e.cancel().catch((r) => this.serialErrors(r)), await this.__internal__.serial.input_done), t && (await t.getWriter().close(), await this.__internal__.serial.output_done), this.__internal__.serial.connected && this.__internal__.serial && this.__internal__.serial.port && await this.__internal__.serial.port.close();
|
|
268
241
|
} catch (e) {
|
|
269
242
|
this.serialErrors(e);
|
|
270
243
|
} finally {
|
|
271
|
-
this.__internal__.serial.reader = null, this.__internal__.serial.input_done = null, this.__internal__.serial.output_stream = null, this.__internal__.serial.output_done = null, this.__internal__.serial.connected = !1, this.__internal__.serial.port = null,
|
|
244
|
+
this.__internal__.serial.reader = null, this.__internal__.serial.input_done = null, this.__internal__.serial.output_stream = null, this.__internal__.serial.output_done = null, this.__internal__.serial.connected = !1, this.__internal__.serial.port = null, _.$dispatchChange(this);
|
|
272
245
|
}
|
|
273
246
|
}
|
|
274
247
|
getResponseAsArrayBuffer() {
|
|
@@ -286,8 +259,8 @@ class J extends E {
|
|
|
286
259
|
async serialPortsSaved(e) {
|
|
287
260
|
const t = this.serialFilters;
|
|
288
261
|
if (this.__internal__.aux_port_connector < e.length) {
|
|
289
|
-
const
|
|
290
|
-
this.__internal__.serial.port = e[
|
|
262
|
+
const i = this.__internal__.aux_port_connector;
|
|
263
|
+
this.__internal__.serial.port = e[i];
|
|
291
264
|
} else
|
|
292
265
|
this.__internal__.aux_port_connector = 0, this.__internal__.serial.port = await navigator.serial.requestPort({
|
|
293
266
|
filters: t
|
|
@@ -308,7 +281,7 @@ class J extends E {
|
|
|
308
281
|
case t.includes(
|
|
309
282
|
"this readable stream reader has been released and cannot be used to cancel its previous owner stream"
|
|
310
283
|
):
|
|
311
|
-
this.dispatch("serial:need-permission", {}),
|
|
284
|
+
this.dispatch("serial:need-permission", {}), _.$dispatchChange(this);
|
|
312
285
|
break;
|
|
313
286
|
case t.includes("the port is already open."):
|
|
314
287
|
case t.includes("failed to open serial port"):
|
|
@@ -331,7 +304,7 @@ class J extends E {
|
|
|
331
304
|
case t.includes("the port is already closed."):
|
|
332
305
|
break;
|
|
333
306
|
case t.includes("the device has been lost"):
|
|
334
|
-
this.dispatch("serial:lost", {}),
|
|
307
|
+
this.dispatch("serial:lost", {}), _.$dispatchChange(this);
|
|
335
308
|
break;
|
|
336
309
|
case t.includes("navigator.serial is undefined"):
|
|
337
310
|
this.dispatch("serial:unsupported", {});
|
|
@@ -345,36 +318,36 @@ class J extends E {
|
|
|
345
318
|
async serialConnect() {
|
|
346
319
|
try {
|
|
347
320
|
this.dispatch("serial:connecting", {});
|
|
348
|
-
const e = await
|
|
321
|
+
const e = await o(this, s, S).call(this);
|
|
349
322
|
if (e.length > 0)
|
|
350
323
|
await this.serialPortsSaved(e);
|
|
351
324
|
else {
|
|
352
|
-
const
|
|
325
|
+
const r = this.serialFilters;
|
|
353
326
|
this.__internal__.serial.port = await navigator.serial.requestPort({
|
|
354
|
-
filters:
|
|
327
|
+
filters: r
|
|
355
328
|
});
|
|
356
329
|
}
|
|
357
330
|
const t = this.__internal__.serial.port;
|
|
358
331
|
if (!t)
|
|
359
332
|
throw new Error("No port selected by the user");
|
|
360
333
|
await t.open(this.serialConfigPort);
|
|
361
|
-
const
|
|
362
|
-
t.onconnect = (
|
|
363
|
-
console.log(
|
|
334
|
+
const i = this;
|
|
335
|
+
t.onconnect = (r) => {
|
|
336
|
+
console.log(r), i.dispatch("serial:connected", r), _.$dispatchChange(this), i.__internal__.serial.queue.length > 0 && i.dispatch("internal:queue", {});
|
|
364
337
|
}, t.ondisconnect = async () => {
|
|
365
|
-
await
|
|
366
|
-
}, await
|
|
367
|
-
await
|
|
368
|
-
}, this.__internal__.time.response_connection), this.__internal__.serial.last_action = "connect", await
|
|
338
|
+
await i.disconnect();
|
|
339
|
+
}, await C(this.__internal__.serial.delay_first_connection), this.__internal__.timeout.until_response = setTimeout(async () => {
|
|
340
|
+
await i.timeout(i.__internal__.serial.bytes_connection ?? [], "connection:start");
|
|
341
|
+
}, this.__internal__.time.response_connection), this.__internal__.serial.last_action = "connect", await o(this, s, b).call(this, this.__internal__.serial.bytes_connection ?? []), this.dispatch("serial:sent", {
|
|
369
342
|
action: "connect",
|
|
370
343
|
bytes: this.__internal__.serial.bytes_connection
|
|
371
|
-
}), this.__internal__.auto_response &&
|
|
344
|
+
}), this.__internal__.auto_response && o(this, s, h).call(this, this.__internal__.serial.auto_response, null), await o(this, s, k).call(this);
|
|
372
345
|
} catch (e) {
|
|
373
346
|
this.serialErrors(e);
|
|
374
347
|
}
|
|
375
348
|
}
|
|
376
349
|
async serialForget() {
|
|
377
|
-
return await
|
|
350
|
+
return await o(this, s, U).call(this);
|
|
378
351
|
}
|
|
379
352
|
decToHex(e) {
|
|
380
353
|
return typeof e == "string" && (e = parseInt(e, 10)), e.toString(16);
|
|
@@ -387,21 +360,21 @@ class J extends E {
|
|
|
387
360
|
}
|
|
388
361
|
add0x(e) {
|
|
389
362
|
const t = [];
|
|
390
|
-
return e.forEach((
|
|
391
|
-
t[
|
|
363
|
+
return e.forEach((i, r) => {
|
|
364
|
+
t[r] = "0x" + i;
|
|
392
365
|
}), t;
|
|
393
366
|
}
|
|
394
367
|
bytesToHex(e) {
|
|
395
368
|
return this.add0x(Array.from(e, (t) => this.hexMaker(t)));
|
|
396
369
|
}
|
|
397
370
|
async appendToQueue(e, t) {
|
|
398
|
-
const
|
|
371
|
+
const i = this.bytesToHex(e);
|
|
399
372
|
if (["connect", "connection:start"].includes(t)) {
|
|
400
373
|
if (this.__internal__.serial.connected) return;
|
|
401
374
|
await this.serialConnect();
|
|
402
375
|
return;
|
|
403
376
|
}
|
|
404
|
-
this.__internal__.serial.queue.push({ bytes:
|
|
377
|
+
this.__internal__.serial.queue.push({ bytes: i, action: t }), this.dispatch("internal:queue", {});
|
|
405
378
|
}
|
|
406
379
|
serialSetConnectionConstant(e = 1) {
|
|
407
380
|
throw new Error(`Method not implemented 'serialSetConnectionConstant' to listen on channel ${e}`);
|
|
@@ -417,8 +390,8 @@ class J extends E {
|
|
|
417
390
|
}
|
|
418
391
|
sumHex(e) {
|
|
419
392
|
let t = 0;
|
|
420
|
-
return e.forEach((
|
|
421
|
-
t += parseInt(
|
|
393
|
+
return e.forEach((i) => {
|
|
394
|
+
t += parseInt(i, 16);
|
|
422
395
|
}), t.toString(16);
|
|
423
396
|
}
|
|
424
397
|
toString() {
|
|
@@ -431,7 +404,7 @@ class J extends E {
|
|
|
431
404
|
});
|
|
432
405
|
}
|
|
433
406
|
softReload() {
|
|
434
|
-
|
|
407
|
+
o(this, s, I).call(this), this.dispatch("serial:soft-reload", {});
|
|
435
408
|
}
|
|
436
409
|
async sendConnect() {
|
|
437
410
|
if (!this.__internal__.serial.bytes_connection)
|
|
@@ -453,13 +426,13 @@ class J extends E {
|
|
|
453
426
|
}
|
|
454
427
|
parseStringToTextEncoder(e = "", t = `
|
|
455
428
|
`) {
|
|
456
|
-
const
|
|
457
|
-
return e += t,
|
|
429
|
+
const i = new TextEncoder();
|
|
430
|
+
return e += t, i.encode(e);
|
|
458
431
|
}
|
|
459
432
|
parseStringToBytes(e = "", t = `
|
|
460
433
|
`) {
|
|
461
|
-
const
|
|
462
|
-
return Array.from(
|
|
434
|
+
const i = this.parseStringToTextEncoder(e, t);
|
|
435
|
+
return Array.from(i).map((r) => r.toString(16));
|
|
463
436
|
}
|
|
464
437
|
parseUint8ToHex(e) {
|
|
465
438
|
return Array.from(e).map((t) => t.toString(16));
|
|
@@ -469,29 +442,29 @@ class J extends E {
|
|
|
469
442
|
}
|
|
470
443
|
stringArrayToUint8Array(e) {
|
|
471
444
|
const t = [];
|
|
472
|
-
return e.forEach((
|
|
473
|
-
const
|
|
474
|
-
t.push(parseInt(
|
|
445
|
+
return e.forEach((i) => {
|
|
446
|
+
const r = i.replace("0x", "");
|
|
447
|
+
t.push(parseInt(r, 16));
|
|
475
448
|
}), new Uint8Array(t);
|
|
476
449
|
}
|
|
477
450
|
parseUint8ArrayToString(e) {
|
|
478
451
|
const t = this.stringArrayToUint8Array(e);
|
|
479
452
|
e = this.parseUint8ToHex(t);
|
|
480
|
-
const
|
|
481
|
-
return this.__internal__.serial.response.replacer ? String.fromCharCode(...
|
|
453
|
+
const i = e.map((r) => parseInt(r, 16));
|
|
454
|
+
return this.__internal__.serial.response.replacer ? String.fromCharCode(...i).replace(this.__internal__.serial.response.replacer, "") : String.fromCharCode(...i);
|
|
482
455
|
}
|
|
483
456
|
hexToAscii(e) {
|
|
484
457
|
const t = e.toString();
|
|
485
|
-
let
|
|
486
|
-
for (let
|
|
487
|
-
|
|
488
|
-
return
|
|
458
|
+
let i = "";
|
|
459
|
+
for (let r = 0; r < t.length; r += 2)
|
|
460
|
+
i += String.fromCharCode(parseInt(t.substring(r, 2), 16));
|
|
461
|
+
return i;
|
|
489
462
|
}
|
|
490
463
|
asciiToHex(e) {
|
|
491
464
|
const t = [];
|
|
492
|
-
for (let
|
|
493
|
-
const
|
|
494
|
-
t.push(
|
|
465
|
+
for (let i = 0, r = e.length; i < r; i++) {
|
|
466
|
+
const c = Number(e.charCodeAt(i)).toString(16);
|
|
467
|
+
t.push(c);
|
|
495
468
|
}
|
|
496
469
|
return t.join("");
|
|
497
470
|
}
|
|
@@ -499,121 +472,121 @@ class J extends E {
|
|
|
499
472
|
return this.isConnected;
|
|
500
473
|
}
|
|
501
474
|
}
|
|
502
|
-
|
|
475
|
+
s = new WeakSet(), p = function(e) {
|
|
503
476
|
return !!(e && e.readable && e.writable);
|
|
504
|
-
},
|
|
505
|
-
this.__internal__.serial.connected = !1, this.__internal__.aux_port_connector = 0, this.dispatch("serial:disconnected", e),
|
|
506
|
-
},
|
|
477
|
+
}, f = function(e = null) {
|
|
478
|
+
this.__internal__.serial.connected = !1, this.__internal__.aux_port_connector = 0, this.dispatch("serial:disconnected", e), _.$dispatchChange(this);
|
|
479
|
+
}, b = async function(e) {
|
|
507
480
|
const t = this.__internal__.serial.port;
|
|
508
481
|
if (!t || t && (!t.readable || !t.writable))
|
|
509
|
-
throw
|
|
510
|
-
const
|
|
482
|
+
throw o(this, s, f).call(this, { error: "Port is closed, not readable or writable." }), new Error("The port is closed or is not readable/writable");
|
|
483
|
+
const i = this.stringArrayToUint8Array(e);
|
|
511
484
|
if (t.writable === null) return;
|
|
512
|
-
const
|
|
513
|
-
await
|
|
514
|
-
},
|
|
485
|
+
const r = t.writable.getWriter();
|
|
486
|
+
await r.write(i), r.releaseLock();
|
|
487
|
+
}, h = function(e = [], t = null) {
|
|
515
488
|
if (e && e.length > 0) {
|
|
516
|
-
const
|
|
517
|
-
this.__internal__.serial.connected =
|
|
518
|
-
const
|
|
519
|
-
for (const
|
|
520
|
-
|
|
489
|
+
const i = this.__internal__.serial.connected;
|
|
490
|
+
this.__internal__.serial.connected = o(this, s, p).call(this, this.__internal__.serial.port), _.$dispatchChange(this), !i && this.__internal__.serial.connected && this.dispatch("serial:connected"), this.__internal__.interval.reconnection && (clearInterval(this.__internal__.interval.reconnection), this.__internal__.interval.reconnection = 0), this.__internal__.timeout.until_response && (clearTimeout(this.__internal__.timeout.until_response), this.__internal__.timeout.until_response = 0);
|
|
491
|
+
const r = [];
|
|
492
|
+
for (const c in e)
|
|
493
|
+
r.push(e[c].toString().padStart(2, "0").toLowerCase());
|
|
521
494
|
if (this.__internal__.serial.response.as === "hex")
|
|
522
|
-
this.serialMessage(
|
|
495
|
+
this.serialMessage(r);
|
|
523
496
|
else if (this.__internal__.serial.response.as === "uint8")
|
|
524
|
-
this.serialMessage(this.parseHexToUint8(this.add0x(
|
|
497
|
+
this.serialMessage(this.parseHexToUint8(this.add0x(r)));
|
|
525
498
|
else if (this.__internal__.serial.response.as === "string")
|
|
526
499
|
if (this.__internal__.serial.response.limiter !== null) {
|
|
527
|
-
const
|
|
528
|
-
for (const
|
|
529
|
-
|
|
500
|
+
const u = this.parseUint8ArrayToString(this.add0x(r)).split(this.__internal__.serial.response.limiter);
|
|
501
|
+
for (const y in u)
|
|
502
|
+
u[y] && this.serialMessage(u[y]);
|
|
530
503
|
} else
|
|
531
|
-
this.serialMessage(this.parseUint8ArrayToString(this.add0x(
|
|
504
|
+
this.serialMessage(this.parseUint8ArrayToString(this.add0x(r)));
|
|
532
505
|
else {
|
|
533
|
-
const
|
|
534
|
-
this.parseUint8ArrayToString(this.add0x(
|
|
506
|
+
const c = this.stringToArrayBuffer(
|
|
507
|
+
this.parseUint8ArrayToString(this.add0x(r))
|
|
535
508
|
);
|
|
536
|
-
this.serialMessage(
|
|
509
|
+
this.serialMessage(c);
|
|
537
510
|
}
|
|
538
511
|
} else
|
|
539
512
|
this.serialCorruptMessage(e, t);
|
|
540
513
|
this.__internal__.serial.queue.length !== 0 && this.dispatch("internal:queue", {});
|
|
541
|
-
},
|
|
514
|
+
}, S = async function() {
|
|
542
515
|
const e = this.serialFilters, t = await navigator.serial.getPorts({ filters: e });
|
|
543
|
-
return e.length === 0 ? t : t.filter((
|
|
544
|
-
const
|
|
545
|
-
return e.some((
|
|
546
|
-
}).filter((
|
|
547
|
-
},
|
|
516
|
+
return e.length === 0 ? t : t.filter((r) => {
|
|
517
|
+
const c = r.getInfo();
|
|
518
|
+
return e.some((u) => c.usbProductId === u.usbProductId && c.usbVendorId === u.usbVendorId);
|
|
519
|
+
}).filter((r) => !o(this, s, p).call(this, r));
|
|
520
|
+
}, A = function(e) {
|
|
548
521
|
if (e) {
|
|
549
|
-
const t = this.__internal__.serial.response.buffer,
|
|
550
|
-
|
|
522
|
+
const t = this.__internal__.serial.response.buffer, i = new Uint8Array(t.length + e.byteLength);
|
|
523
|
+
i.set(t, 0), i.set(new Uint8Array(e), t.length), this.__internal__.serial.response.buffer = i;
|
|
551
524
|
}
|
|
552
|
-
},
|
|
525
|
+
}, E = async function() {
|
|
553
526
|
this.__internal__.serial.time_until_send_bytes && (clearTimeout(this.__internal__.serial.time_until_send_bytes), this.__internal__.serial.time_until_send_bytes = 0), this.__internal__.serial.time_until_send_bytes = setTimeout(() => {
|
|
554
527
|
const e = [];
|
|
555
528
|
for (const t in this.__internal__.serial.response.buffer)
|
|
556
529
|
e.push(this.__internal__.serial.response.buffer[t].toString(16));
|
|
557
|
-
this.__internal__.serial.response.buffer &&
|
|
530
|
+
this.__internal__.serial.response.buffer && o(this, s, h).call(this, e), this.__internal__.serial.response.buffer = new Uint8Array(0);
|
|
558
531
|
}, 400);
|
|
559
|
-
},
|
|
532
|
+
}, T = async function() {
|
|
560
533
|
if (this.__internal__.serial.response.length !== null) {
|
|
561
534
|
if (this.__internal__.serial.response.length === this.__internal__.serial.response.buffer.length) {
|
|
562
535
|
const e = [];
|
|
563
536
|
for (const t in this.__internal__.serial.response.buffer)
|
|
564
537
|
e.push(this.__internal__.serial.response.buffer[t].toString(16));
|
|
565
|
-
|
|
538
|
+
o(this, s, h).call(this, e), this.__internal__.serial.response.buffer = new Uint8Array(0);
|
|
566
539
|
} else if (this.__internal__.serial.response.length < this.__internal__.serial.response.buffer.length) {
|
|
567
540
|
let e = new Uint8Array(0);
|
|
568
|
-
for (let
|
|
569
|
-
e[
|
|
541
|
+
for (let i = 0; i < this.__internal__.serial.response.length; i++)
|
|
542
|
+
e[i] = this.__internal__.serial.response.buffer[i];
|
|
570
543
|
if (e.length === this.__internal__.serial.response.length) {
|
|
571
|
-
const
|
|
572
|
-
for (const
|
|
573
|
-
|
|
574
|
-
|
|
544
|
+
const i = [];
|
|
545
|
+
for (const r in e)
|
|
546
|
+
i.push(e[r].toString(16));
|
|
547
|
+
o(this, s, h).call(this, i), this.__internal__.serial.response.buffer = new Uint8Array(0);
|
|
575
548
|
return;
|
|
576
549
|
}
|
|
577
550
|
e = new Uint8Array(0);
|
|
578
551
|
const t = this.__internal__.serial.response.length * 2;
|
|
579
552
|
if (this.__internal__.serial.response.buffer.length === t) {
|
|
580
|
-
for (let
|
|
581
|
-
e[
|
|
553
|
+
for (let i = 14; i < t; i++)
|
|
554
|
+
e[i - this.__internal__.serial.response.length] = this.__internal__.serial.response.buffer[i];
|
|
582
555
|
if (e.length === this.__internal__.serial.response.length) {
|
|
583
|
-
const
|
|
584
|
-
for (const
|
|
585
|
-
|
|
586
|
-
|
|
556
|
+
const i = [];
|
|
557
|
+
for (const r in e)
|
|
558
|
+
i.push(e[r].toString(16));
|
|
559
|
+
o(this, s, h).call(this, i), this.__internal__.serial.response.buffer = new Uint8Array(0);
|
|
587
560
|
}
|
|
588
561
|
}
|
|
589
562
|
}
|
|
590
563
|
}
|
|
591
|
-
},
|
|
564
|
+
}, k = async function() {
|
|
592
565
|
const e = this.__internal__.serial.port;
|
|
593
566
|
if (!e || !e.readable) throw new Error("Port is not readable");
|
|
594
567
|
for (; e.readable && this.__internal__.serial.keep_reading; ) {
|
|
595
568
|
const t = e.readable.getReader();
|
|
596
569
|
this.__internal__.serial.reader = t;
|
|
597
570
|
try {
|
|
598
|
-
let
|
|
599
|
-
for (;
|
|
600
|
-
const { value:
|
|
601
|
-
if (
|
|
602
|
-
t.releaseLock(), this.__internal__.serial.keep_reading = !1,
|
|
571
|
+
let i = !0;
|
|
572
|
+
for (; i; ) {
|
|
573
|
+
const { value: r, done: c } = await t.read();
|
|
574
|
+
if (c) {
|
|
575
|
+
t.releaseLock(), this.__internal__.serial.keep_reading = !1, i = !1;
|
|
603
576
|
break;
|
|
604
577
|
}
|
|
605
|
-
|
|
578
|
+
o(this, s, A).call(this, r), this.__internal__.serial.response.length === null ? await o(this, s, E).call(this) : await o(this, s, T).call(this);
|
|
606
579
|
}
|
|
607
|
-
} catch (
|
|
608
|
-
this.serialErrors(
|
|
580
|
+
} catch (i) {
|
|
581
|
+
this.serialErrors(i);
|
|
609
582
|
} finally {
|
|
610
583
|
t.releaseLock();
|
|
611
584
|
}
|
|
612
585
|
}
|
|
613
586
|
this.__internal__.serial.keep_reading = !0, this.__internal__.serial.port && await this.__internal__.serial.port.close();
|
|
614
|
-
},
|
|
587
|
+
}, U = async function() {
|
|
615
588
|
return typeof window > "u" ? !1 : "serial" in navigator && "forget" in SerialPort.prototype && this.__internal__.serial.port ? (await this.__internal__.serial.port.forget(), !0) : !1;
|
|
616
|
-
},
|
|
589
|
+
}, L = function() {
|
|
617
590
|
[
|
|
618
591
|
"serial:connected",
|
|
619
592
|
"serial:connecting",
|
|
@@ -632,21 +605,21 @@ r = new WeakSet(), f = function(e) {
|
|
|
632
605
|
].forEach((t) => {
|
|
633
606
|
this.serialRegisterAvailableListener(t);
|
|
634
607
|
});
|
|
635
|
-
},
|
|
608
|
+
}, P = function() {
|
|
636
609
|
const e = this;
|
|
637
610
|
this.on("internal:queue", async () => {
|
|
638
611
|
var t;
|
|
639
|
-
await
|
|
640
|
-
}),
|
|
641
|
-
},
|
|
612
|
+
await o(t = e, s, $).call(t);
|
|
613
|
+
}), o(this, s, D).call(this);
|
|
614
|
+
}, D = function() {
|
|
642
615
|
const e = this;
|
|
643
616
|
navigator.serial.addEventListener("connect", async () => {
|
|
644
617
|
e.isDisconnected && await e.serialConnect().catch(() => {
|
|
645
618
|
});
|
|
646
619
|
});
|
|
647
|
-
},
|
|
648
|
-
if (!
|
|
649
|
-
|
|
620
|
+
}, $ = async function() {
|
|
621
|
+
if (!o(this, s, p).call(this, this.__internal__.serial.port)) {
|
|
622
|
+
o(this, s, f).call(this, { error: "Port is closed, not readable or writable." }), await this.serialConnect();
|
|
650
623
|
return;
|
|
651
624
|
}
|
|
652
625
|
if (this.__internal__.timeout.until_response || this.__internal__.serial.queue.length === 0) return;
|
|
@@ -654,15 +627,15 @@ r = new WeakSet(), f = function(e) {
|
|
|
654
627
|
let t = this.__internal__.time.response_general;
|
|
655
628
|
e.action === "connect" && (t = this.__internal__.time.response_connection), this.__internal__.timeout.until_response = setTimeout(async () => {
|
|
656
629
|
await this.timeout(e.bytes, e.action);
|
|
657
|
-
}, t), this.__internal__.serial.last_action = e.action ?? "unknown", await
|
|
630
|
+
}, t), this.__internal__.serial.last_action = e.action ?? "unknown", await o(this, s, b).call(this, e.bytes), this.dispatch("serial:sent", {
|
|
658
631
|
action: e.action,
|
|
659
632
|
bytes: e.bytes
|
|
660
|
-
}), this.__internal__.auto_response &&
|
|
661
|
-
const
|
|
662
|
-
this.__internal__.serial.queue =
|
|
663
|
-
},
|
|
633
|
+
}), this.__internal__.auto_response && o(this, s, h).call(this, this.__internal__.serial.auto_response, null);
|
|
634
|
+
const i = [...this.__internal__.serial.queue];
|
|
635
|
+
this.__internal__.serial.queue = i.splice(1);
|
|
636
|
+
}, q = function(e = 1) {
|
|
664
637
|
this.__internal__.device_number = e, this.__internal__.serial.bytes_connection = this.serialSetConnectionConstant(e);
|
|
665
|
-
},
|
|
638
|
+
}, I = function() {
|
|
666
639
|
this.__internal__.last_error = {
|
|
667
640
|
message: null,
|
|
668
641
|
action: null,
|
|
@@ -671,7 +644,7 @@ r = new WeakSet(), f = function(e) {
|
|
|
671
644
|
};
|
|
672
645
|
};
|
|
673
646
|
export {
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
647
|
+
B as Core,
|
|
648
|
+
_ as Devices,
|
|
649
|
+
x as Dispatcher
|
|
677
650
|
};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
(function(c,a){typeof exports=="object"&&typeof module<"u"?a(exports):typeof define=="function"&&define.amd?define(["exports"],a):(c=typeof globalThis<"u"?globalThis:c||self,a(c.WebSerialCore={}))})(this,function(c){"use strict";var Q=Object.defineProperty;var E=c=>{throw TypeError(c)};var z=(c,a,u)=>a in c?Q(c,a,{enumerable:!0,configurable:!0,writable:!0,value:u}):c[a]=u;var g=(c,a,u)=>z(c,typeof a!="symbol"?a+"":a,u),G=(c,a,u)=>a.has(c)||E("Cannot "+u);var U=(c,a,u)=>a.has(c)?E("Cannot add the same private member more than once"):a instanceof WeakSet?a.add(c):a.set(c,u);var _=(c,a,u)=>(G(c,a,"access private method"),u);var s,b,y,C,p,k,D,L,P,I,$,q,M,R,N,O,H;const a=[];for(let o=0;o<256;++o)a.push((o+256).toString(16).slice(1));function u(o,i=0){return(a[o[i+0]]+a[o[i+1]]+a[o[i+2]]+a[o[i+3]]+"-"+a[o[i+4]]+a[o[i+5]]+"-"+a[o[i+6]]+a[o[i+7]]+"-"+a[o[i+8]]+a[o[i+9]]+"-"+a[o[i+10]]+a[o[i+11]]+a[o[i+12]]+a[o[i+13]]+a[o[i+14]]+a[o[i+15]]).toLowerCase()}let m;const j=new Uint8Array(16);function B(){if(!m){if(typeof crypto>"u"||!crypto.getRandomValues)throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");m=crypto.getRandomValues.bind(crypto)}return m(j)}const x={randomUUID:typeof crypto<"u"&&crypto.randomUUID&&crypto.randomUUID.bind(crypto)};function F(o,i,e){var n;if(x.randomUUID&&!o)return x.randomUUID();o=o||{};const t=o.random??((n=o.rng)==null?void 0:n.call(o))??B();if(t.length<16)throw new Error("Random bytes length must be >= 16");return t[6]=t[6]&15|64,t[8]=t[8]&63|128,u(t)}class S extends CustomEvent{constructor(i,e){super(i,e)}}class w extends EventTarget{constructor(){super(...arguments);g(this,"__listeners__",{debug:!1});g(this,"__debug__",!1)}dispatch(e,t=null){const n=new S(e,{detail:t});this.dispatchEvent(n),this.__debug__&&this.dispatchEvent(new S("debug",{detail:{type:e,data:t}}))}dispatchAsync(e,t=null,n=100){const r=this;setTimeout(()=>{r.dispatch(e,t)},n)}on(e,t){typeof this.__listeners__[e]<"u"&&!this.__listeners__[e]&&(this.__listeners__[e]=!0),this.addEventListener(e,t)}off(e,t){this.removeEventListener(e,t)}serialRegisterAvailableListener(e){this.__listeners__[e]||(this.__listeners__[e]=!1)}get availableListeners(){return Object.keys(this.__listeners__).sort().map(t=>({type:t,listening:this.__listeners__[t]}))}}const l=class l extends w{constructor(){super(),["change"].forEach(e=>{this.serialRegisterAvailableListener(e)})}static $dispatchChange(i=null){i&&i.$checkAndDispatchConnection(),l.instance.dispatch("change",{devices:l.devices,dispatcher:i})}static typeError(i){const e=new Error;throw e.message=`Type ${i} is not supported`,e.name="DeviceTypeError",e}static registerType(i){typeof l.devices[i]>"u"&&(l.devices[i]={})}static add(i){const e=i.typeDevice;typeof l.devices[e]>"u"&&(l.devices[e]={});const t=i.uuid;if(typeof l.devices[e]>"u"&&l.typeError(e),l.devices[e][t])throw new Error(`Device with id ${t} already exists`);return l.devices[e][t]=i,l.$dispatchChange(i),Object.keys(l.devices[e]).indexOf(t)}static get(i,e){return typeof l.devices[i]>"u"&&(l.devices[i]={}),typeof l.devices[i]>"u"&&l.typeError(i),l.devices[i][e]}static getAll(i=null){return i===null?l.devices:(typeof l.devices[i]>"u"&&l.typeError(i),l.devices[i])}static getList(){return Object.values(l.devices).map(e=>Object.values(e)).flat()}static getByNumber(i,e){return typeof l.devices[i]>"u"&&l.typeError(i),Object.values(l.devices[i]).find(n=>n.deviceNumber===e)??null}static getCustom(i,e=1){return typeof l.devices[i]>"u"&&l.typeError(i),Object.values(l.devices[i]).find(n=>n.deviceNumber===e)??null}};g(l,"instance"),g(l,"devices",{});let h=l;h.instance||(h.instance=new h);function A(o=100){return new Promise(i=>setTimeout(()=>i(),o))}function W(){return"serial"in navigator}const v={baudRate:9600,dataBits:8,stopBits:1,parity:"none",bufferSize:32768,flowControl:"none"};class V extends w{constructor({filters:e=null,config_port:t=v,no_device:n=1,device_listen_on_channel:r=1}={filters:null,config_port:v,no_device:1,device_listen_on_channel:1}){super();U(this,s);g(this,"__internal__",{auto_response:!1,device_number:1,aux_port_connector:0,last_error:{message:null,action:null,code:null,no_code:0},serial:{connected:!1,port:null,last_action:null,response:{length:null,buffer:new Uint8Array([]),as:"hex",replacer:/[\n\r]+/g,limiter:null},reader:null,input_done:null,output_done:null,input_stream:null,output_stream:null,keep_reading:!0,time_until_send_bytes:void 0,delay_first_connection:200,bytes_connection:null,filters:[],config_port:v,queue:[],auto_response:["DD","DD"]},device:{type:"unknown",id:F(),listen_on_port:null},time:{response_connection:500,response_general:2e3},timeout:{until_response:0},interval:{reconnection:0}});if(!("serial"in navigator))throw new Error("Web Serial not supported");e&&(this.serialFilters=e),t&&(this.serialConfigPort=t),n&&_(this,s,O).call(this,n),r&&["number","string"].includes(typeof r)&&(this.listenOnChannel=r),_(this,s,q).call(this),_(this,s,M).call(this)}set listenOnChannel(e){if(typeof e=="string"&&(e=parseInt(e)),isNaN(e)||e<1||e>255)throw new Error("Invalid port number");this.__internal__.device.listen_on_port=e,this.__internal__.serial.bytes_connection=this.serialSetConnectionConstant(e)}get lastAction(){return this.__internal__.serial.last_action}get listenOnChannel(){return this.__internal__.device.listen_on_port??1}set serialFilters(e){this.__internal__.serial.filters=e}get serialFilters(){return this.__internal__.serial.filters}set serialConfigPort(e){this.__internal__.serial.config_port=e}get serialConfigPort(){return this.__internal__.serial.config_port}get isConnected(){const e=this.__internal__.serial.connected,t=_(this,s,b).call(this,this.__internal__.serial.port);return e&&!t&&_(this,s,y).call(this,{error:"Port is closed, not readable or writable."}),this.__internal__.serial.connected=t,this.__internal__.serial.connected}get isDisconnected(){const e=this.__internal__.serial.connected,t=_(this,s,b).call(this,this.__internal__.serial.port);return!e&&t&&(this.dispatch("serial:connected"),h.$dispatchChange(this)),this.__internal__.serial.connected=t,!this.__internal__.serial.connected}get deviceNumber(){return this.__internal__.device_number}get uuid(){return this.__internal__.device.id}get typeDevice(){return this.__internal__.device.type}get queue(){return this.__internal__.serial.queue}async timeout(e,t){this.__internal__.last_error.message="Operation response timed out.",this.__internal__.last_error.action=t,this.__internal__.last_error.code=e,this.__internal__.timeout.until_response&&(clearTimeout(this.__internal__.timeout.until_response),this.__internal__.timeout.until_response=0),t==="connect"?(this.__internal__.serial.connected=!1,this.dispatch("serial:reconnect",{}),h.$dispatchChange(this)):t==="connection:start"&&(await this.serialDisconnect(),this.__internal__.serial.connected=!1,this.__internal__.aux_port_connector+=1,h.$dispatchChange(this),await this.serialConnect()),this.dispatch("serial:timeout",{...this.__internal__.last_error,bytes:e,action:t})}async disconnect(e=null){await this.serialDisconnect(),_(this,s,y).call(this,e)}async connect(){return new Promise((e,t)=>{W()||t("Web Serial not supported"),setTimeout(async()=>{await A(499),await this.serialConnect(),this.isConnected?e(`${this.typeDevice} device ${this.deviceNumber} connected`):t(`${this.typeDevice} device ${this.deviceNumber} not connected`)},1)})}async serialDisconnect(){try{const e=this.__internal__.serial.reader,t=this.__internal__.serial.output_stream;e&&(await e.cancel().catch(r=>this.serialErrors(r)),await this.__internal__.serial.input_done),t&&(await t.getWriter().close(),await this.__internal__.serial.output_done),this.__internal__.serial.connected&&this.__internal__.serial&&this.__internal__.serial.port&&await this.__internal__.serial.port.close()}catch(e){this.serialErrors(e)}finally{this.__internal__.serial.reader=null,this.__internal__.serial.input_done=null,this.__internal__.serial.output_stream=null,this.__internal__.serial.output_done=null,this.__internal__.serial.connected=!1,this.__internal__.serial.port=null,h.$dispatchChange(this)}}getResponseAsArrayBuffer(){this.__internal__.serial.response.as="arraybuffer"}getResponseAsArrayHex(){this.__internal__.serial.response.as="hex"}getResponseAsUint8Array(){this.__internal__.serial.response.as="uint8"}getResponseAsString(){this.__internal__.serial.response.as="string"}async serialPortsSaved(e){const t=this.serialFilters;if(this.__internal__.aux_port_connector<e.length){const n=this.__internal__.aux_port_connector;this.__internal__.serial.port=e[n]}else this.__internal__.aux_port_connector=0,this.__internal__.serial.port=await navigator.serial.requestPort({filters:t});if(!this.__internal__.serial.port)throw new Error("Select another port please")}serialErrors(e){const t=e.toString().toLowerCase();switch(!0){case t.includes("must be handling a user gesture to show a permission request"):case t.includes("the port is closed."):case t.includes("the port is closed or is not writable"):case t.includes("the port is closed or is not readable"):case t.includes("the port is closed or is not readable/writable"):case t.includes("select another port please"):case t.includes("no port selected by the user"):case t.includes("this readable stream reader has been released and cannot be used to cancel its previous owner stream"):this.dispatch("serial:need-permission",{}),h.$dispatchChange(this);break;case t.includes("the port is already open."):case t.includes("failed to open serial port"):this.serialDisconnect().then(async()=>{this.__internal__.aux_port_connector+=1,await this.serialConnect()});break;case t.includes("cannot read properties of undefined (reading 'writable')"):case t.includes("cannot read properties of null (reading 'writable')"):case t.includes("cannot read property 'writable' of null"):case t.includes("cannot read property 'writable' of undefined"):this.serialDisconnect().then(async()=>{await this.serialConnect()});break;case t.includes("'close' on 'serialport': a call to close() is already in progress."):break;case t.includes("failed to execute 'open' on 'serialport': a call to open() is already in progress."):break;case t.includes("the port is already closed."):break;case t.includes("the device has been lost"):this.dispatch("serial:lost",{}),h.$dispatchChange(this);break;case t.includes("navigator.serial is undefined"):this.dispatch("serial:unsupported",{});break;default:console.error(e);break}this.dispatch("serial:error",e)}async serialConnect(){try{this.dispatch("serial:connecting",{});const e=await _(this,s,k).call(this);if(e.length>0)await this.serialPortsSaved(e);else{const r=this.serialFilters;this.__internal__.serial.port=await navigator.serial.requestPort({filters:r})}const t=this.__internal__.serial.port;if(!t)throw new Error("No port selected by the user");await t.open(this.serialConfigPort);const n=this;t.onconnect=r=>{console.log(r),n.dispatch("serial:connected",r),h.$dispatchChange(this),n.__internal__.serial.queue.length>0&&n.dispatch("internal:queue",{})},t.ondisconnect=async()=>{await n.disconnect()},await A(this.__internal__.serial.delay_first_connection),this.__internal__.timeout.until_response=setTimeout(async()=>{await n.timeout(n.__internal__.serial.bytes_connection??[],"connection:start")},this.__internal__.time.response_connection),this.__internal__.serial.last_action="connect",await _(this,s,C).call(this,this.__internal__.serial.bytes_connection??[]),this.dispatch("serial:sent",{action:"connect",bytes:this.__internal__.serial.bytes_connection}),this.__internal__.auto_response&&_(this,s,p).call(this,this.__internal__.serial.auto_response,null),await _(this,s,I).call(this)}catch(e){this.serialErrors(e)}}async serialForget(){return await _(this,s,$).call(this)}decToHex(e){return typeof e=="string"&&(e=parseInt(e,10)),e.toString(16)}hexToDec(e){return parseInt(e,16)}hexMaker(e="00",t=2){return e.toString().padStart(t,"0").toLowerCase()}add0x(e){const t=[];return e.forEach((n,r)=>{t[r]="0x"+n}),t}bytesToHex(e){return this.add0x(Array.from(e,t=>this.hexMaker(t)))}async appendToQueue(e,t){const n=this.bytesToHex(e);if(["connect","connection:start"].includes(t)){if(this.__internal__.serial.connected)return;await this.serialConnect();return}this.__internal__.serial.queue.push({bytes:n,action:t}),this.dispatch("internal:queue",{})}serialSetConnectionConstant(e=1){throw new Error(`Method not implemented 'serialSetConnectionConstant' to listen on channel ${e}`)}serialMessage(e){throw console.log(e),new Error("Method not implemented 'serialMessage'")}serialCorruptMessage(e,t){throw console.log(e,t),new Error("Method not implemented 'serialCorruptMessage'")}clearSerialQueue(){this.__internal__.serial.queue=[]}sumHex(e){let t=0;return e.forEach(n=>{t+=parseInt(n,16)}),t.toString(16)}toString(){return JSON.stringify({__class:this.typeDevice,device_number:this.deviceNumber,uuid:this.uuid,connected:this.isConnected,connection:this.__internal__.serial.bytes_connection})}softReload(){_(this,s,H).call(this),this.dispatch("serial:soft-reload",{})}async sendConnect(){if(!this.__internal__.serial.bytes_connection)throw new Error("No connection bytes defined");await this.appendToQueue(this.__internal__.serial.bytes_connection,"connect")}async sendCustomCode({code:e=[]}={code:[]}){if(e===null||e.length===0)throw new Error("No data to send");await this.appendToQueue(e,"custom")}stringToArrayHex(e){return Array.from(e).map(t=>t.charCodeAt(0).toString(16))}stringToArrayBuffer(e,t=`
|
|
1
|
+
(function(l,_){typeof exports=="object"&&typeof module<"u"?_(exports):typeof define=="function"&&define.amd?define(["exports"],_):(l=typeof globalThis<"u"?globalThis:l||self,_(l.WebSerialCore={}))})(this,function(l){"use strict";var H=Object.defineProperty;var x=l=>{throw TypeError(l)};var R=(l,_,h)=>_ in l?H(l,_,{enumerable:!0,configurable:!0,writable:!0,value:h}):l[_]=h;var f=(l,_,h)=>R(l,typeof _!="symbol"?_+"":_,h),j=(l,_,h)=>_.has(l)||x("Cannot "+h);var S=(l,_,h)=>_.has(l)?x("Cannot add the same private member more than once"):_ instanceof WeakSet?_.add(l):_.set(l,h);var o=(l,_,h)=>(j(l,_,"access private method"),h);var n,g,b,m,d,A,T,E,k,U,P,L,D,$,q,I,M;class _ extends CustomEvent{constructor(r,e){super(r,e)}}class h extends EventTarget{constructor(){super(...arguments);f(this,"__listeners__",{debug:!1});f(this,"__debug__",!1)}dispatch(e,t=null){const i=new _(e,{detail:t});this.dispatchEvent(i),this.__debug__&&this.dispatchEvent(new _("debug",{detail:{type:e,data:t}}))}dispatchAsync(e,t=null,i=100){const s=this;setTimeout(()=>{s.dispatch(e,t)},i)}on(e,t){typeof this.__listeners__[e]<"u"&&!this.__listeners__[e]&&(this.__listeners__[e]=!0),this.addEventListener(e,t)}off(e,t){this.removeEventListener(e,t)}serialRegisterAvailableListener(e){this.__listeners__[e]||(this.__listeners__[e]=!1)}get availableListeners(){return Object.keys(this.__listeners__).sort().map(t=>({type:t,listening:this.__listeners__[t]}))}}const a=class a extends h{constructor(){super(),["change"].forEach(e=>{this.serialRegisterAvailableListener(e)})}static $dispatchChange(r=null){r&&r.$checkAndDispatchConnection(),a.instance.dispatch("change",{devices:a.devices,dispatcher:r})}static typeError(r){const e=new Error;throw e.message=`Type ${r} is not supported`,e.name="DeviceTypeError",e}static registerType(r){typeof a.devices[r]>"u"&&(a.devices[r]={})}static add(r){const e=r.typeDevice;typeof a.devices[e]>"u"&&(a.devices[e]={});const t=r.uuid;if(typeof a.devices[e]>"u"&&a.typeError(e),a.devices[e][t])throw new Error(`Device with id ${t} already exists`);return a.devices[e][t]=r,a.$dispatchChange(r),Object.keys(a.devices[e]).indexOf(t)}static get(r,e){return typeof a.devices[r]>"u"&&(a.devices[r]={}),typeof a.devices[r]>"u"&&a.typeError(r),a.devices[r][e]}static getAll(r=null){return r===null?a.devices:(typeof a.devices[r]>"u"&&a.typeError(r),a.devices[r])}static getList(){return Object.values(a.devices).map(e=>Object.values(e)).flat()}static getByNumber(r,e){return typeof a.devices[r]>"u"&&a.typeError(r),Object.values(a.devices[r]).find(i=>i.deviceNumber===e)??null}static getCustom(r,e=1){return typeof a.devices[r]>"u"&&a.typeError(r),Object.values(a.devices[r]).find(i=>i.deviceNumber===e)??null}};f(a,"instance"),f(a,"devices",{});let c=a;c.instance||(c.instance=new c);function v(y=100){return new Promise(r=>setTimeout(()=>r(),y))}function N(){return"serial"in navigator}const w={baudRate:9600,dataBits:8,stopBits:1,parity:"none",bufferSize:32768,flowControl:"none"};class O extends h{constructor({filters:e=null,config_port:t=w,no_device:i=1,device_listen_on_channel:s=1}={filters:null,config_port:w,no_device:1,device_listen_on_channel:1}){super();S(this,n);f(this,"__internal__",{auto_response:!1,device_number:1,aux_port_connector:0,last_error:{message:null,action:null,code:null,no_code:0},serial:{connected:!1,port:null,last_action:null,response:{length:null,buffer:new Uint8Array([]),as:"hex",replacer:/[\n\r]+/g,limiter:null},reader:null,input_done:null,output_done:null,input_stream:null,output_stream:null,keep_reading:!0,time_until_send_bytes:void 0,delay_first_connection:200,bytes_connection:null,filters:[],config_port:w,queue:[],auto_response:["DD","DD"]},device:{type:"unknown",id:window.crypto.randomUUID(),listen_on_port:null},time:{response_connection:500,response_general:2e3},timeout:{until_response:0},interval:{reconnection:0}});if(!("serial"in navigator))throw new Error("Web Serial not supported");e&&(this.serialFilters=e),t&&(this.serialConfigPort=t),i&&o(this,n,I).call(this,i),s&&["number","string"].includes(typeof s)&&(this.listenOnChannel=s),o(this,n,L).call(this),o(this,n,D).call(this)}set listenOnChannel(e){if(typeof e=="string"&&(e=parseInt(e)),isNaN(e)||e<1||e>255)throw new Error("Invalid port number");this.__internal__.device.listen_on_port=e,this.__internal__.serial.bytes_connection=this.serialSetConnectionConstant(e)}get lastAction(){return this.__internal__.serial.last_action}get listenOnChannel(){return this.__internal__.device.listen_on_port??1}set serialFilters(e){this.__internal__.serial.filters=e}get serialFilters(){return this.__internal__.serial.filters}set serialConfigPort(e){this.__internal__.serial.config_port=e}get serialConfigPort(){return this.__internal__.serial.config_port}get isConnected(){const e=this.__internal__.serial.connected,t=o(this,n,g).call(this,this.__internal__.serial.port);return e&&!t&&o(this,n,b).call(this,{error:"Port is closed, not readable or writable."}),this.__internal__.serial.connected=t,this.__internal__.serial.connected}get isDisconnected(){const e=this.__internal__.serial.connected,t=o(this,n,g).call(this,this.__internal__.serial.port);return!e&&t&&(this.dispatch("serial:connected"),c.$dispatchChange(this)),this.__internal__.serial.connected=t,!this.__internal__.serial.connected}get deviceNumber(){return this.__internal__.device_number}get uuid(){return this.__internal__.device.id}get typeDevice(){return this.__internal__.device.type}get queue(){return this.__internal__.serial.queue}async timeout(e,t){this.__internal__.last_error.message="Operation response timed out.",this.__internal__.last_error.action=t,this.__internal__.last_error.code=e,this.__internal__.timeout.until_response&&(clearTimeout(this.__internal__.timeout.until_response),this.__internal__.timeout.until_response=0),t==="connect"?(this.__internal__.serial.connected=!1,this.dispatch("serial:reconnect",{}),c.$dispatchChange(this)):t==="connection:start"&&(await this.serialDisconnect(),this.__internal__.serial.connected=!1,this.__internal__.aux_port_connector+=1,c.$dispatchChange(this),await this.serialConnect()),this.dispatch("serial:timeout",{...this.__internal__.last_error,bytes:e,action:t})}async disconnect(e=null){await this.serialDisconnect(),o(this,n,b).call(this,e)}async connect(){return new Promise((e,t)=>{N()||t("Web Serial not supported"),setTimeout(async()=>{await v(499),await this.serialConnect(),this.isConnected?e(`${this.typeDevice} device ${this.deviceNumber} connected`):t(`${this.typeDevice} device ${this.deviceNumber} not connected`)},1)})}async serialDisconnect(){try{const e=this.__internal__.serial.reader,t=this.__internal__.serial.output_stream;e&&(await e.cancel().catch(s=>this.serialErrors(s)),await this.__internal__.serial.input_done),t&&(await t.getWriter().close(),await this.__internal__.serial.output_done),this.__internal__.serial.connected&&this.__internal__.serial&&this.__internal__.serial.port&&await this.__internal__.serial.port.close()}catch(e){this.serialErrors(e)}finally{this.__internal__.serial.reader=null,this.__internal__.serial.input_done=null,this.__internal__.serial.output_stream=null,this.__internal__.serial.output_done=null,this.__internal__.serial.connected=!1,this.__internal__.serial.port=null,c.$dispatchChange(this)}}getResponseAsArrayBuffer(){this.__internal__.serial.response.as="arraybuffer"}getResponseAsArrayHex(){this.__internal__.serial.response.as="hex"}getResponseAsUint8Array(){this.__internal__.serial.response.as="uint8"}getResponseAsString(){this.__internal__.serial.response.as="string"}async serialPortsSaved(e){const t=this.serialFilters;if(this.__internal__.aux_port_connector<e.length){const i=this.__internal__.aux_port_connector;this.__internal__.serial.port=e[i]}else this.__internal__.aux_port_connector=0,this.__internal__.serial.port=await navigator.serial.requestPort({filters:t});if(!this.__internal__.serial.port)throw new Error("Select another port please")}serialErrors(e){const t=e.toString().toLowerCase();switch(!0){case t.includes("must be handling a user gesture to show a permission request"):case t.includes("the port is closed."):case t.includes("the port is closed or is not writable"):case t.includes("the port is closed or is not readable"):case t.includes("the port is closed or is not readable/writable"):case t.includes("select another port please"):case t.includes("no port selected by the user"):case t.includes("this readable stream reader has been released and cannot be used to cancel its previous owner stream"):this.dispatch("serial:need-permission",{}),c.$dispatchChange(this);break;case t.includes("the port is already open."):case t.includes("failed to open serial port"):this.serialDisconnect().then(async()=>{this.__internal__.aux_port_connector+=1,await this.serialConnect()});break;case t.includes("cannot read properties of undefined (reading 'writable')"):case t.includes("cannot read properties of null (reading 'writable')"):case t.includes("cannot read property 'writable' of null"):case t.includes("cannot read property 'writable' of undefined"):this.serialDisconnect().then(async()=>{await this.serialConnect()});break;case t.includes("'close' on 'serialport': a call to close() is already in progress."):break;case t.includes("failed to execute 'open' on 'serialport': a call to open() is already in progress."):break;case t.includes("the port is already closed."):break;case t.includes("the device has been lost"):this.dispatch("serial:lost",{}),c.$dispatchChange(this);break;case t.includes("navigator.serial is undefined"):this.dispatch("serial:unsupported",{});break;default:console.error(e);break}this.dispatch("serial:error",e)}async serialConnect(){try{this.dispatch("serial:connecting",{});const e=await o(this,n,A).call(this);if(e.length>0)await this.serialPortsSaved(e);else{const s=this.serialFilters;this.__internal__.serial.port=await navigator.serial.requestPort({filters:s})}const t=this.__internal__.serial.port;if(!t)throw new Error("No port selected by the user");await t.open(this.serialConfigPort);const i=this;t.onconnect=s=>{console.log(s),i.dispatch("serial:connected",s),c.$dispatchChange(this),i.__internal__.serial.queue.length>0&&i.dispatch("internal:queue",{})},t.ondisconnect=async()=>{await i.disconnect()},await v(this.__internal__.serial.delay_first_connection),this.__internal__.timeout.until_response=setTimeout(async()=>{await i.timeout(i.__internal__.serial.bytes_connection??[],"connection:start")},this.__internal__.time.response_connection),this.__internal__.serial.last_action="connect",await o(this,n,m).call(this,this.__internal__.serial.bytes_connection??[]),this.dispatch("serial:sent",{action:"connect",bytes:this.__internal__.serial.bytes_connection}),this.__internal__.auto_response&&o(this,n,d).call(this,this.__internal__.serial.auto_response,null),await o(this,n,U).call(this)}catch(e){this.serialErrors(e)}}async serialForget(){return await o(this,n,P).call(this)}decToHex(e){return typeof e=="string"&&(e=parseInt(e,10)),e.toString(16)}hexToDec(e){return parseInt(e,16)}hexMaker(e="00",t=2){return e.toString().padStart(t,"0").toLowerCase()}add0x(e){const t=[];return e.forEach((i,s)=>{t[s]="0x"+i}),t}bytesToHex(e){return this.add0x(Array.from(e,t=>this.hexMaker(t)))}async appendToQueue(e,t){const i=this.bytesToHex(e);if(["connect","connection:start"].includes(t)){if(this.__internal__.serial.connected)return;await this.serialConnect();return}this.__internal__.serial.queue.push({bytes:i,action:t}),this.dispatch("internal:queue",{})}serialSetConnectionConstant(e=1){throw new Error(`Method not implemented 'serialSetConnectionConstant' to listen on channel ${e}`)}serialMessage(e){throw console.log(e),new Error("Method not implemented 'serialMessage'")}serialCorruptMessage(e,t){throw console.log(e,t),new Error("Method not implemented 'serialCorruptMessage'")}clearSerialQueue(){this.__internal__.serial.queue=[]}sumHex(e){let t=0;return e.forEach(i=>{t+=parseInt(i,16)}),t.toString(16)}toString(){return JSON.stringify({__class:this.typeDevice,device_number:this.deviceNumber,uuid:this.uuid,connected:this.isConnected,connection:this.__internal__.serial.bytes_connection})}softReload(){o(this,n,M).call(this),this.dispatch("serial:soft-reload",{})}async sendConnect(){if(!this.__internal__.serial.bytes_connection)throw new Error("No connection bytes defined");await this.appendToQueue(this.__internal__.serial.bytes_connection,"connect")}async sendCustomCode({code:e=[]}={code:[]}){if(e===null||e.length===0)throw new Error("No data to send");await this.appendToQueue(e,"custom")}stringToArrayHex(e){return Array.from(e).map(t=>t.charCodeAt(0).toString(16))}stringToArrayBuffer(e,t=`
|
|
2
2
|
`){return this.parseStringToTextEncoder(e,t).buffer}parseStringToTextEncoder(e="",t=`
|
|
3
|
-
`){const
|
|
4
|
-
`){const
|
|
3
|
+
`){const i=new TextEncoder;return e+=t,i.encode(e)}parseStringToBytes(e="",t=`
|
|
4
|
+
`){const i=this.parseStringToTextEncoder(e,t);return Array.from(i).map(s=>s.toString(16))}parseUint8ToHex(e){return Array.from(e).map(t=>t.toString(16))}parseHexToUint8(e){return new Uint8Array(e.map(t=>parseInt(t,16)))}stringArrayToUint8Array(e){const t=[];return e.forEach(i=>{const s=i.replace("0x","");t.push(parseInt(s,16))}),new Uint8Array(t)}parseUint8ArrayToString(e){const t=this.stringArrayToUint8Array(e);e=this.parseUint8ToHex(t);const i=e.map(s=>parseInt(s,16));return this.__internal__.serial.response.replacer?String.fromCharCode(...i).replace(this.__internal__.serial.response.replacer,""):String.fromCharCode(...i)}hexToAscii(e){const t=e.toString();let i="";for(let s=0;s<t.length;s+=2)i+=String.fromCharCode(parseInt(t.substring(s,2),16));return i}asciiToHex(e){const t=[];for(let i=0,s=e.length;i<s;i++){const u=Number(e.charCodeAt(i)).toString(16);t.push(u)}return t.join("")}$checkAndDispatchConnection(){return this.isConnected}}n=new WeakSet,g=function(e){return!!(e&&e.readable&&e.writable)},b=function(e=null){this.__internal__.serial.connected=!1,this.__internal__.aux_port_connector=0,this.dispatch("serial:disconnected",e),c.$dispatchChange(this)},m=async function(e){const t=this.__internal__.serial.port;if(!t||t&&(!t.readable||!t.writable))throw o(this,n,b).call(this,{error:"Port is closed, not readable or writable."}),new Error("The port is closed or is not readable/writable");const i=this.stringArrayToUint8Array(e);if(t.writable===null)return;const s=t.writable.getWriter();await s.write(i),s.releaseLock()},d=function(e=[],t=null){if(e&&e.length>0){const i=this.__internal__.serial.connected;this.__internal__.serial.connected=o(this,n,g).call(this,this.__internal__.serial.port),c.$dispatchChange(this),!i&&this.__internal__.serial.connected&&this.dispatch("serial:connected"),this.__internal__.interval.reconnection&&(clearInterval(this.__internal__.interval.reconnection),this.__internal__.interval.reconnection=0),this.__internal__.timeout.until_response&&(clearTimeout(this.__internal__.timeout.until_response),this.__internal__.timeout.until_response=0);const s=[];for(const u in e)s.push(e[u].toString().padStart(2,"0").toLowerCase());if(this.__internal__.serial.response.as==="hex")this.serialMessage(s);else if(this.__internal__.serial.response.as==="uint8")this.serialMessage(this.parseHexToUint8(this.add0x(s)));else if(this.__internal__.serial.response.as==="string")if(this.__internal__.serial.response.limiter!==null){const p=this.parseUint8ArrayToString(this.add0x(s)).split(this.__internal__.serial.response.limiter);for(const C in p)p[C]&&this.serialMessage(p[C])}else this.serialMessage(this.parseUint8ArrayToString(this.add0x(s)));else{const u=this.stringToArrayBuffer(this.parseUint8ArrayToString(this.add0x(s)));this.serialMessage(u)}}else this.serialCorruptMessage(e,t);this.__internal__.serial.queue.length!==0&&this.dispatch("internal:queue",{})},A=async function(){const e=this.serialFilters,t=await navigator.serial.getPorts({filters:e});return e.length===0?t:t.filter(s=>{const u=s.getInfo();return e.some(p=>u.usbProductId===p.usbProductId&&u.usbVendorId===p.usbVendorId)}).filter(s=>!o(this,n,g).call(this,s))},T=function(e){if(e){const t=this.__internal__.serial.response.buffer,i=new Uint8Array(t.length+e.byteLength);i.set(t,0),i.set(new Uint8Array(e),t.length),this.__internal__.serial.response.buffer=i}},E=async function(){this.__internal__.serial.time_until_send_bytes&&(clearTimeout(this.__internal__.serial.time_until_send_bytes),this.__internal__.serial.time_until_send_bytes=0),this.__internal__.serial.time_until_send_bytes=setTimeout(()=>{const e=[];for(const t in this.__internal__.serial.response.buffer)e.push(this.__internal__.serial.response.buffer[t].toString(16));this.__internal__.serial.response.buffer&&o(this,n,d).call(this,e),this.__internal__.serial.response.buffer=new Uint8Array(0)},400)},k=async function(){if(this.__internal__.serial.response.length!==null){if(this.__internal__.serial.response.length===this.__internal__.serial.response.buffer.length){const e=[];for(const t in this.__internal__.serial.response.buffer)e.push(this.__internal__.serial.response.buffer[t].toString(16));o(this,n,d).call(this,e),this.__internal__.serial.response.buffer=new Uint8Array(0)}else if(this.__internal__.serial.response.length<this.__internal__.serial.response.buffer.length){let e=new Uint8Array(0);for(let i=0;i<this.__internal__.serial.response.length;i++)e[i]=this.__internal__.serial.response.buffer[i];if(e.length===this.__internal__.serial.response.length){const i=[];for(const s in e)i.push(e[s].toString(16));o(this,n,d).call(this,i),this.__internal__.serial.response.buffer=new Uint8Array(0);return}e=new Uint8Array(0);const t=this.__internal__.serial.response.length*2;if(this.__internal__.serial.response.buffer.length===t){for(let i=14;i<t;i++)e[i-this.__internal__.serial.response.length]=this.__internal__.serial.response.buffer[i];if(e.length===this.__internal__.serial.response.length){const i=[];for(const s in e)i.push(e[s].toString(16));o(this,n,d).call(this,i),this.__internal__.serial.response.buffer=new Uint8Array(0)}}}}},U=async function(){const e=this.__internal__.serial.port;if(!e||!e.readable)throw new Error("Port is not readable");for(;e.readable&&this.__internal__.serial.keep_reading;){const t=e.readable.getReader();this.__internal__.serial.reader=t;try{let i=!0;for(;i;){const{value:s,done:u}=await t.read();if(u){t.releaseLock(),this.__internal__.serial.keep_reading=!1,i=!1;break}o(this,n,T).call(this,s),this.__internal__.serial.response.length===null?await o(this,n,E).call(this):await o(this,n,k).call(this)}}catch(i){this.serialErrors(i)}finally{t.releaseLock()}}this.__internal__.serial.keep_reading=!0,this.__internal__.serial.port&&await this.__internal__.serial.port.close()},P=async function(){return typeof window>"u"?!1:"serial"in navigator&&"forget"in SerialPort.prototype&&this.__internal__.serial.port?(await this.__internal__.serial.port.forget(),!0):!1},L=function(){["serial:connected","serial:connecting","serial:reconnect","serial:timeout","serial:disconnected","serial:sent","serial:soft-reload","serial:message","unknown","serial:need-permission","serial:lost","serial:unsupported","serial:error","debug"].forEach(t=>{this.serialRegisterAvailableListener(t)})},D=function(){const e=this;this.on("internal:queue",async()=>{var t;await o(t=e,n,q).call(t)}),o(this,n,$).call(this)},$=function(){const e=this;navigator.serial.addEventListener("connect",async()=>{e.isDisconnected&&await e.serialConnect().catch(()=>{})})},q=async function(){if(!o(this,n,g).call(this,this.__internal__.serial.port)){o(this,n,b).call(this,{error:"Port is closed, not readable or writable."}),await this.serialConnect();return}if(this.__internal__.timeout.until_response||this.__internal__.serial.queue.length===0)return;const e=this.__internal__.serial.queue[0];let t=this.__internal__.time.response_general;e.action==="connect"&&(t=this.__internal__.time.response_connection),this.__internal__.timeout.until_response=setTimeout(async()=>{await this.timeout(e.bytes,e.action)},t),this.__internal__.serial.last_action=e.action??"unknown",await o(this,n,m).call(this,e.bytes),this.dispatch("serial:sent",{action:e.action,bytes:e.bytes}),this.__internal__.auto_response&&o(this,n,d).call(this,this.__internal__.serial.auto_response,null);const i=[...this.__internal__.serial.queue];this.__internal__.serial.queue=i.splice(1)},I=function(e=1){this.__internal__.device_number=e,this.__internal__.serial.bytes_connection=this.serialSetConnectionConstant(e)},M=function(){this.__internal__.last_error={message:null,action:null,code:null,no_code:0}},l.Core=O,l.Devices=c,l.Dispatcher=h,Object.defineProperty(l,Symbol.toStringTag,{value:"Module"})});
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "webserial-core",
|
|
3
3
|
"description": "Webserial Core to easy connections with serial devices",
|
|
4
|
-
"version": "1.0.
|
|
4
|
+
"version": "1.0.6",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"author": "danidoble",
|
|
@@ -17,7 +17,11 @@
|
|
|
17
17
|
"webserial",
|
|
18
18
|
"serial",
|
|
19
19
|
"core",
|
|
20
|
-
"web"
|
|
20
|
+
"web",
|
|
21
|
+
"iot",
|
|
22
|
+
"device",
|
|
23
|
+
"communication",
|
|
24
|
+
"usb"
|
|
21
25
|
],
|
|
22
26
|
"files": [
|
|
23
27
|
"dist",
|
|
@@ -25,8 +29,10 @@
|
|
|
25
29
|
],
|
|
26
30
|
"main": "./dist/webserial-core.umd.cjs",
|
|
27
31
|
"module": "./dist/webserial-core.js",
|
|
32
|
+
"types": "./dist/main.d.ts",
|
|
28
33
|
"exports": {
|
|
29
34
|
".": {
|
|
35
|
+
"types": "./dist/main.d.ts",
|
|
30
36
|
"import": "./dist/webserial-core.js",
|
|
31
37
|
"require": "./dist/webserial-core.umd.cjs"
|
|
32
38
|
}
|
|
@@ -39,17 +45,14 @@
|
|
|
39
45
|
"format": "prettier --write ./lib/**/*.ts"
|
|
40
46
|
},
|
|
41
47
|
"devDependencies": {
|
|
42
|
-
"@eslint/js": "^9.
|
|
48
|
+
"@eslint/js": "^9.26.0",
|
|
43
49
|
"@types/w3c-web-serial": "^1.0.8",
|
|
44
|
-
"eslint": "^9.
|
|
50
|
+
"eslint": "^9.26.0",
|
|
45
51
|
"globals": "^15.15.0",
|
|
46
52
|
"prettier": "3.4.2",
|
|
47
|
-
"puppeteer": "^24.4.0",
|
|
48
|
-
"semantic-release": "^24.2.3",
|
|
49
53
|
"typescript": "~5.7.3",
|
|
50
|
-
"typescript-eslint": "^8.
|
|
51
|
-
"
|
|
52
|
-
"vite": "^6.2.2"
|
|
54
|
+
"typescript-eslint": "^8.32.0",
|
|
55
|
+
"vite": "^6.3.5"
|
|
53
56
|
},
|
|
54
57
|
"publishConfig": {
|
|
55
58
|
"access": "public"
|