@gurezo/web-serial-rxjs 0.1.0 → 0.1.3
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/browser/browser-detection.d.ts +104 -0
- package/dist/browser/browser-detection.d.ts.map +1 -0
- package/dist/browser/browser-support.d.ts +57 -0
- package/dist/browser/browser-support.d.ts.map +1 -0
- package/dist/client/index.d.ts +250 -0
- package/dist/client/index.d.ts.map +1 -0
- package/dist/client/serial-client.d.ts +98 -0
- package/dist/client/serial-client.d.ts.map +1 -0
- package/dist/errors/serial-error-code.d.ts +132 -0
- package/dist/errors/serial-error-code.d.ts.map +1 -0
- package/dist/errors/serial-error.d.ts +72 -0
- package/dist/errors/serial-error.d.ts.map +1 -0
- package/dist/filters/build-request-options.d.ts +42 -0
- package/dist/filters/build-request-options.d.ts.map +1 -0
- package/dist/index.d.ts +54 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +595 -0
- package/dist/index.mjs +596 -0
- package/dist/index.mjs.map +7 -0
- package/dist/io/observable-to-writable.d.ts +65 -0
- package/dist/io/observable-to-writable.d.ts.map +1 -0
- package/dist/io/readable-to-observable.d.ts +44 -0
- package/dist/io/readable-to-observable.d.ts.map +1 -0
- package/dist/lib/web-serial-rxjs.d.ts +7 -0
- package/dist/lib/web-serial-rxjs.d.ts.map +1 -0
- package/dist/types/options.d.ts +107 -0
- package/dist/types/options.d.ts.map +1 -0
- package/package.json +11 -9
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Browser type enumeration for identifying the browser environment.
|
|
3
|
+
*
|
|
4
|
+
* This enum is used to identify the specific browser type, which is useful for
|
|
5
|
+
* browser-specific behavior or error messages.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```typescript
|
|
9
|
+
* const browserType = detectBrowserType();
|
|
10
|
+
* if (browserType === BrowserType.CHROME) {
|
|
11
|
+
* console.log('Running in Chrome');
|
|
12
|
+
* }
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
export declare enum BrowserType {
|
|
16
|
+
/** Google Chrome browser */
|
|
17
|
+
CHROME = "chrome",
|
|
18
|
+
/** Microsoft Edge browser */
|
|
19
|
+
EDGE = "edge",
|
|
20
|
+
/** Opera browser */
|
|
21
|
+
OPERA = "opera",
|
|
22
|
+
/** Unknown or unsupported browser */
|
|
23
|
+
UNKNOWN = "unknown"
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Feature detection for Web Serial API.
|
|
27
|
+
*
|
|
28
|
+
* Checks if the browser supports the Web Serial API by verifying the presence
|
|
29
|
+
* of `navigator.serial`. This is a non-throwing check that returns `false` if
|
|
30
|
+
* the API is not available.
|
|
31
|
+
*
|
|
32
|
+
* Note: This function only checks for API availability, not whether the browser
|
|
33
|
+
* type is supported. For a throwing check that provides better error messages,
|
|
34
|
+
* use {@link checkBrowserSupport}.
|
|
35
|
+
*
|
|
36
|
+
* @returns `true` if the Web Serial API is available, `false` otherwise
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* ```typescript
|
|
40
|
+
* if (hasWebSerialSupport()) {
|
|
41
|
+
* // Safe to use serial port functionality
|
|
42
|
+
* const client = createSerialClient();
|
|
43
|
+
* } else {
|
|
44
|
+
* console.error('Web Serial API is not supported');
|
|
45
|
+
* }
|
|
46
|
+
* ```
|
|
47
|
+
*
|
|
48
|
+
* @see {@link checkBrowserSupport} for a throwing version with better error messages
|
|
49
|
+
* @see {@link isBrowserSupported} for an alias to this function
|
|
50
|
+
*/
|
|
51
|
+
export declare function hasWebSerialSupport(): boolean;
|
|
52
|
+
/**
|
|
53
|
+
* Detect browser type from the user agent string.
|
|
54
|
+
*
|
|
55
|
+
* Analyzes the browser's user agent string to identify the browser type.
|
|
56
|
+
* This function is useful for providing browser-specific functionality or
|
|
57
|
+
* error messages.
|
|
58
|
+
*
|
|
59
|
+
* @returns The detected {@link BrowserType}, or {@link BrowserType.UNKNOWN} if the browser cannot be identified
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```typescript
|
|
63
|
+
* const browserType = detectBrowserType();
|
|
64
|
+
* switch (browserType) {
|
|
65
|
+
* case BrowserType.CHROME:
|
|
66
|
+
* console.log('Running in Chrome');
|
|
67
|
+
* break;
|
|
68
|
+
* case BrowserType.EDGE:
|
|
69
|
+
* console.log('Running in Edge');
|
|
70
|
+
* break;
|
|
71
|
+
* case BrowserType.OPERA:
|
|
72
|
+
* console.log('Running in Opera');
|
|
73
|
+
* break;
|
|
74
|
+
* default:
|
|
75
|
+
* console.log('Unknown browser');
|
|
76
|
+
* }
|
|
77
|
+
* ```
|
|
78
|
+
*
|
|
79
|
+
* @see {@link isChromiumBased} for checking if the browser is Chromium-based
|
|
80
|
+
*/
|
|
81
|
+
export declare function detectBrowserType(): BrowserType;
|
|
82
|
+
/**
|
|
83
|
+
* Check if the browser is Chromium-based.
|
|
84
|
+
*
|
|
85
|
+
* Determines if the current browser is based on Chromium, which includes
|
|
86
|
+
* Chrome, Edge, and Opera. These browsers support the Web Serial API.
|
|
87
|
+
*
|
|
88
|
+
* @returns `true` if the browser is Chromium-based (Chrome, Edge, or Opera), `false` otherwise
|
|
89
|
+
*
|
|
90
|
+
* @example
|
|
91
|
+
* ```typescript
|
|
92
|
+
* if (isChromiumBased()) {
|
|
93
|
+
* // Browser supports Web Serial API
|
|
94
|
+
* const client = createSerialClient();
|
|
95
|
+
* } else {
|
|
96
|
+
* console.error('Please use a Chromium-based browser (Chrome, Edge, or Opera)');
|
|
97
|
+
* }
|
|
98
|
+
* ```
|
|
99
|
+
*
|
|
100
|
+
* @see {@link detectBrowserType} for identifying the specific browser type
|
|
101
|
+
* @see {@link hasWebSerialSupport} for checking Web Serial API availability
|
|
102
|
+
*/
|
|
103
|
+
export declare function isChromiumBased(): boolean;
|
|
104
|
+
//# sourceMappingURL=browser-detection.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"browser-detection.d.ts","sourceRoot":"","sources":["../../src/browser/browser-detection.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AACH,oBAAY,WAAW;IACrB,4BAA4B;IAC5B,MAAM,WAAW;IACjB,6BAA6B;IAC7B,IAAI,SAAS;IACb,oBAAoB;IACpB,KAAK,UAAU;IACf,qCAAqC;IACrC,OAAO,YAAY;CACpB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,wBAAgB,mBAAmB,IAAI,OAAO,CAO7C;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,wBAAgB,iBAAiB,IAAI,WAAW,CAoB/C;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,eAAe,IAAI,OAAO,CAOzC"}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Check if the browser supports the Web Serial API, throwing an error if not supported.
|
|
3
|
+
*
|
|
4
|
+
* This function performs a feature detection check and throws a {@link SerialError}
|
|
5
|
+
* with code {@link SerialErrorCode.BROWSER_NOT_SUPPORTED} if the Web Serial API
|
|
6
|
+
* is not available. The error message includes the detected browser type for better
|
|
7
|
+
* user feedback.
|
|
8
|
+
*
|
|
9
|
+
* This is the recommended way to check browser support before using serial port
|
|
10
|
+
* functionality, as it provides clear error messages.
|
|
11
|
+
*
|
|
12
|
+
* @throws {@link SerialError} with code {@link SerialErrorCode.BROWSER_NOT_SUPPORTED}
|
|
13
|
+
* if the browser doesn't support the Web Serial API
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```typescript
|
|
17
|
+
* try {
|
|
18
|
+
* checkBrowserSupport();
|
|
19
|
+
* // Safe to use serial port functionality
|
|
20
|
+
* const client = createSerialClient();
|
|
21
|
+
* } catch (error) {
|
|
22
|
+
* if (error instanceof SerialError && error.code === SerialErrorCode.BROWSER_NOT_SUPPORTED) {
|
|
23
|
+
* console.error(error.message);
|
|
24
|
+
* // Show user-friendly message: "Please use a Chromium-based browser..."
|
|
25
|
+
* }
|
|
26
|
+
* }
|
|
27
|
+
* ```
|
|
28
|
+
*
|
|
29
|
+
* @see {@link isBrowserSupported} for a non-throwing version that returns a boolean
|
|
30
|
+
* @see {@link hasWebSerialSupport} for the underlying feature detection function
|
|
31
|
+
*/
|
|
32
|
+
export declare function checkBrowserSupport(): void;
|
|
33
|
+
/**
|
|
34
|
+
* Check if the browser supports the Web Serial API (non-throwing version).
|
|
35
|
+
*
|
|
36
|
+
* This is a convenience function that returns a boolean indicating whether
|
|
37
|
+
* the Web Serial API is available. Unlike {@link checkBrowserSupport}, this
|
|
38
|
+
* function does not throw an error if the API is not available.
|
|
39
|
+
*
|
|
40
|
+
* @returns `true` if the Web Serial API is supported, `false` otherwise
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```typescript
|
|
44
|
+
* if (isBrowserSupported()) {
|
|
45
|
+
* const client = createSerialClient();
|
|
46
|
+
* // Use serial port functionality
|
|
47
|
+
* } else {
|
|
48
|
+
* console.error('Web Serial API is not supported in this browser');
|
|
49
|
+
* // Show fallback UI or message
|
|
50
|
+
* }
|
|
51
|
+
* ```
|
|
52
|
+
*
|
|
53
|
+
* @see {@link checkBrowserSupport} for a throwing version with better error messages
|
|
54
|
+
* @see {@link hasWebSerialSupport} which this function calls internally
|
|
55
|
+
*/
|
|
56
|
+
export declare function isBrowserSupported(): boolean;
|
|
57
|
+
//# sourceMappingURL=browser-support.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"browser-support.d.ts","sourceRoot":"","sources":["../../src/browser/browser-support.ts"],"names":[],"mappings":"AAOA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,wBAAgB,mBAAmB,IAAI,IAAI,CAa1C;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,kBAAkB,IAAI,OAAO,CAE5C"}
|
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
import { Observable } from 'rxjs';
|
|
2
|
+
import { SerialClientOptions } from '../types/options';
|
|
3
|
+
/**
|
|
4
|
+
* SerialClient interface for interacting with serial ports using RxJS Observables.
|
|
5
|
+
*
|
|
6
|
+
* This interface provides a reactive API for serial port communication, allowing you to
|
|
7
|
+
* connect to serial devices, read and write data using RxJS Observables.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```typescript
|
|
11
|
+
* const client = createSerialClient({ baudRate: 9600 });
|
|
12
|
+
*
|
|
13
|
+
* // Connect to a port
|
|
14
|
+
* client.connect().subscribe({
|
|
15
|
+
* next: () => {
|
|
16
|
+
* console.log('Connected!');
|
|
17
|
+
*
|
|
18
|
+
* // Read data
|
|
19
|
+
* client.getReadStream().subscribe({
|
|
20
|
+
* next: (data) => console.log('Received:', data),
|
|
21
|
+
* });
|
|
22
|
+
*
|
|
23
|
+
* // Write data
|
|
24
|
+
* const encoder = new TextEncoder();
|
|
25
|
+
* client.write(encoder.encode('Hello')).subscribe();
|
|
26
|
+
* },
|
|
27
|
+
* error: (error) => console.error('Connection error:', error),
|
|
28
|
+
* });
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
export interface SerialClient {
|
|
32
|
+
/**
|
|
33
|
+
* Request a serial port from the user.
|
|
34
|
+
*
|
|
35
|
+
* This method opens the browser's port selection dialog and returns an Observable
|
|
36
|
+
* that emits the selected SerialPort when the user chooses a port.
|
|
37
|
+
*
|
|
38
|
+
* @returns An Observable that emits the selected {@link SerialPort} when the user selects a port
|
|
39
|
+
* @throws {@link SerialError} with code {@link SerialErrorCode.OPERATION_CANCELLED} if the user cancels the selection
|
|
40
|
+
* @throws {@link SerialError} with code {@link SerialErrorCode.PORT_NOT_AVAILABLE} if the port request fails
|
|
41
|
+
* @throws {@link SerialError} with code {@link SerialErrorCode.BROWSER_NOT_SUPPORTED} if the browser doesn't support Web Serial API
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* ```typescript
|
|
45
|
+
* client.requestPort().subscribe({
|
|
46
|
+
* next: (port) => console.log('Selected port:', port),
|
|
47
|
+
* error: (error) => console.error('Port selection failed:', error),
|
|
48
|
+
* });
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
requestPort(): Observable<SerialPort>;
|
|
52
|
+
/**
|
|
53
|
+
* Get available serial ports that have been previously granted access.
|
|
54
|
+
*
|
|
55
|
+
* This method returns an Observable that emits an array of SerialPort instances
|
|
56
|
+
* that the user has previously granted access to in this browser session.
|
|
57
|
+
*
|
|
58
|
+
* @returns An Observable that emits an array of available {@link SerialPort} instances
|
|
59
|
+
* @throws {@link SerialError} with code {@link SerialErrorCode.PORT_NOT_AVAILABLE} if getting ports fails
|
|
60
|
+
* @throws {@link SerialError} with code {@link SerialErrorCode.BROWSER_NOT_SUPPORTED} if the browser doesn't support Web Serial API
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* ```typescript
|
|
64
|
+
* client.getPorts().subscribe({
|
|
65
|
+
* next: (ports) => {
|
|
66
|
+
* console.log(`Found ${ports.length} available ports`);
|
|
67
|
+
* if (ports.length > 0) {
|
|
68
|
+
* client.connect(ports[0]).subscribe();
|
|
69
|
+
* }
|
|
70
|
+
* },
|
|
71
|
+
* });
|
|
72
|
+
* ```
|
|
73
|
+
*/
|
|
74
|
+
getPorts(): Observable<SerialPort[]>;
|
|
75
|
+
/**
|
|
76
|
+
* Connect to a serial port.
|
|
77
|
+
*
|
|
78
|
+
* Opens the specified port (or requests one if not provided) and configures it
|
|
79
|
+
* with the options passed to {@link createSerialClient}. The port must be connected
|
|
80
|
+
* before reading or writing data.
|
|
81
|
+
*
|
|
82
|
+
* @param port - Optional {@link SerialPort} to connect to. If not provided, will call {@link requestPort} to prompt the user.
|
|
83
|
+
* @returns An Observable that completes when the port is successfully opened
|
|
84
|
+
* @throws {@link SerialError} with code {@link SerialErrorCode.PORT_ALREADY_OPEN} if a port is already open
|
|
85
|
+
* @throws {@link SerialError} with code {@link SerialErrorCode.PORT_OPEN_FAILED} if opening the port fails
|
|
86
|
+
* @throws {@link SerialError} with code {@link SerialErrorCode.OPERATION_CANCELLED} if the user cancels port selection
|
|
87
|
+
* @throws {@link SerialError} with code {@link SerialErrorCode.BROWSER_NOT_SUPPORTED} if the browser doesn't support Web Serial API
|
|
88
|
+
*
|
|
89
|
+
* @example
|
|
90
|
+
* ```typescript
|
|
91
|
+
* // Connect by requesting a port
|
|
92
|
+
* client.connect().subscribe({
|
|
93
|
+
* next: () => console.log('Connected!'),
|
|
94
|
+
* error: (error) => console.error('Connection failed:', error),
|
|
95
|
+
* });
|
|
96
|
+
*
|
|
97
|
+
* // Connect to a specific port
|
|
98
|
+
* client.getPorts().subscribe({
|
|
99
|
+
* next: (ports) => {
|
|
100
|
+
* if (ports.length > 0) {
|
|
101
|
+
* client.connect(ports[0]).subscribe();
|
|
102
|
+
* }
|
|
103
|
+
* },
|
|
104
|
+
* });
|
|
105
|
+
* ```
|
|
106
|
+
*/
|
|
107
|
+
connect(port?: SerialPort): Observable<void>;
|
|
108
|
+
/**
|
|
109
|
+
* Disconnect from the serial port.
|
|
110
|
+
*
|
|
111
|
+
* Closes the currently open port and stops all active read/write streams.
|
|
112
|
+
* This method is safe to call even if no port is currently open.
|
|
113
|
+
*
|
|
114
|
+
* @returns An Observable that completes when the port is successfully closed
|
|
115
|
+
* @throws {@link SerialError} with code {@link SerialErrorCode.CONNECTION_LOST} if closing the port fails
|
|
116
|
+
*
|
|
117
|
+
* @example
|
|
118
|
+
* ```typescript
|
|
119
|
+
* client.disconnect().subscribe({
|
|
120
|
+
* next: () => console.log('Disconnected'),
|
|
121
|
+
* error: (error) => console.error('Disconnect failed:', error),
|
|
122
|
+
* });
|
|
123
|
+
* ```
|
|
124
|
+
*/
|
|
125
|
+
disconnect(): Observable<void>;
|
|
126
|
+
/**
|
|
127
|
+
* Get an Observable that emits data read from the serial port.
|
|
128
|
+
*
|
|
129
|
+
* Returns an Observable stream that emits Uint8Array chunks as data is received
|
|
130
|
+
* from the serial port. The stream will continue until the port is disconnected
|
|
131
|
+
* or an error occurs.
|
|
132
|
+
*
|
|
133
|
+
* @returns An Observable that emits Uint8Array chunks containing data read from the serial port
|
|
134
|
+
* @throws {@link SerialError} with code {@link SerialErrorCode.PORT_NOT_OPEN} if the port is not open
|
|
135
|
+
*
|
|
136
|
+
* @example
|
|
137
|
+
* ```typescript
|
|
138
|
+
* client.getReadStream().subscribe({
|
|
139
|
+
* next: (data) => {
|
|
140
|
+
* const text = new TextDecoder().decode(data);
|
|
141
|
+
* console.log('Received:', text);
|
|
142
|
+
* },
|
|
143
|
+
* error: (error) => console.error('Read error:', error),
|
|
144
|
+
* });
|
|
145
|
+
* ```
|
|
146
|
+
*/
|
|
147
|
+
getReadStream(): Observable<Uint8Array>;
|
|
148
|
+
/**
|
|
149
|
+
* Write data to the serial port from an Observable.
|
|
150
|
+
*
|
|
151
|
+
* Writes data from an Observable stream to the serial port. The Observable should
|
|
152
|
+
* emit Uint8Array chunks that will be written sequentially to the port. If a previous
|
|
153
|
+
* write stream is active, it will be cancelled before starting the new one.
|
|
154
|
+
*
|
|
155
|
+
* @param data$ - Observable that emits Uint8Array chunks to write to the serial port
|
|
156
|
+
* @returns An Observable that completes when all data has been written and the stream completes
|
|
157
|
+
* @throws {@link SerialError} with code {@link SerialErrorCode.PORT_NOT_OPEN} if the port is not open
|
|
158
|
+
* @throws {@link SerialError} with code {@link SerialErrorCode.WRITE_FAILED} if writing fails
|
|
159
|
+
*
|
|
160
|
+
* @example
|
|
161
|
+
* ```typescript
|
|
162
|
+
* const data$ = from([
|
|
163
|
+
* new TextEncoder().encode('Hello'),
|
|
164
|
+
* new TextEncoder().encode('World'),
|
|
165
|
+
* ]);
|
|
166
|
+
*
|
|
167
|
+
* client.writeStream(data$).subscribe({
|
|
168
|
+
* next: () => console.log('Writing...'),
|
|
169
|
+
* complete: () => console.log('All data written'),
|
|
170
|
+
* error: (error) => console.error('Write error:', error),
|
|
171
|
+
* });
|
|
172
|
+
* ```
|
|
173
|
+
*/
|
|
174
|
+
writeStream(data$: Observable<Uint8Array>): Observable<void>;
|
|
175
|
+
/**
|
|
176
|
+
* Write a single chunk of data to the serial port.
|
|
177
|
+
*
|
|
178
|
+
* Writes a single Uint8Array chunk to the serial port. For writing multiple chunks,
|
|
179
|
+
* consider using {@link writeStream} with an Observable instead.
|
|
180
|
+
*
|
|
181
|
+
* @param data - Uint8Array data to write to the serial port
|
|
182
|
+
* @returns An Observable that completes when the data has been written
|
|
183
|
+
* @throws {@link SerialError} with code {@link SerialErrorCode.PORT_NOT_OPEN} if the port is not open
|
|
184
|
+
* @throws {@link SerialError} with code {@link SerialErrorCode.WRITE_FAILED} if writing fails
|
|
185
|
+
*
|
|
186
|
+
* @example
|
|
187
|
+
* ```typescript
|
|
188
|
+
* const encoder = new TextEncoder();
|
|
189
|
+
* const data = encoder.encode('Hello, Serial!');
|
|
190
|
+
*
|
|
191
|
+
* client.write(data).subscribe({
|
|
192
|
+
* next: () => console.log('Data written'),
|
|
193
|
+
* error: (error) => console.error('Write error:', error),
|
|
194
|
+
* });
|
|
195
|
+
* ```
|
|
196
|
+
*/
|
|
197
|
+
write(data: Uint8Array): Observable<void>;
|
|
198
|
+
/**
|
|
199
|
+
* Check if the port is currently open and connected.
|
|
200
|
+
*
|
|
201
|
+
* @returns `true` if a port is currently open, `false` otherwise
|
|
202
|
+
*/
|
|
203
|
+
readonly connected: boolean;
|
|
204
|
+
/**
|
|
205
|
+
* Get the current SerialPort instance.
|
|
206
|
+
*
|
|
207
|
+
* Returns the currently connected SerialPort instance, or `null` if no port is open.
|
|
208
|
+
* This allows direct access to the underlying Web Serial API SerialPort object if needed.
|
|
209
|
+
*
|
|
210
|
+
* @returns The current {@link SerialPort} instance, or `null` if no port is open
|
|
211
|
+
*/
|
|
212
|
+
readonly currentPort: SerialPort | null;
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Create a new SerialClient instance for interacting with serial ports.
|
|
216
|
+
*
|
|
217
|
+
* This is the main entry point for creating a serial client. The client provides
|
|
218
|
+
* a reactive RxJS-based API for connecting to serial ports and reading/writing data.
|
|
219
|
+
*
|
|
220
|
+
* @param options - Optional configuration options for the serial port connection.
|
|
221
|
+
* If not provided, default values will be used (9600 baud, 8 data bits, etc.)
|
|
222
|
+
* @returns A new {@link SerialClient} instance
|
|
223
|
+
* @throws {@link SerialError} with code {@link SerialErrorCode.BROWSER_NOT_SUPPORTED} if the browser doesn't support Web Serial API
|
|
224
|
+
*
|
|
225
|
+
* @example
|
|
226
|
+
* ```typescript
|
|
227
|
+
* // Create a client with default settings (9600 baud)
|
|
228
|
+
* const client = createSerialClient();
|
|
229
|
+
*
|
|
230
|
+
* // Create a client with custom settings
|
|
231
|
+
* const client = createSerialClient({
|
|
232
|
+
* baudRate: 115200,
|
|
233
|
+
* dataBits: 8,
|
|
234
|
+
* stopBits: 1,
|
|
235
|
+
* parity: 'none',
|
|
236
|
+
* filters: [{ usbVendorId: 0x1234 }],
|
|
237
|
+
* });
|
|
238
|
+
*
|
|
239
|
+
* // Check browser support before creating a client
|
|
240
|
+
* import { isBrowserSupported } from '@gurezo/web-serial-rxjs';
|
|
241
|
+
*
|
|
242
|
+
* if (!isBrowserSupported()) {
|
|
243
|
+
* console.error('Web Serial API is not supported');
|
|
244
|
+
* } else {
|
|
245
|
+
* const client = createSerialClient();
|
|
246
|
+
* }
|
|
247
|
+
* ```
|
|
248
|
+
*/
|
|
249
|
+
export declare function createSerialClient(options?: SerialClientOptions): SerialClient;
|
|
250
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/client/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,MAAM,CAAC;AAClC,OAAO,EAAE,mBAAmB,EAAE,MAAM,kBAAkB,CAAC;AAGvD;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,MAAM,WAAW,YAAY;IAC3B;;;;;;;;;;;;;;;;;;OAkBG;IACH,WAAW,IAAI,UAAU,CAAC,UAAU,CAAC,CAAC;IAEtC;;;;;;;;;;;;;;;;;;;;;OAqBG;IACH,QAAQ,IAAI,UAAU,CAAC,UAAU,EAAE,CAAC,CAAC;IAErC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA+BG;IACH,OAAO,CAAC,IAAI,CAAC,EAAE,UAAU,GAAG,UAAU,CAAC,IAAI,CAAC,CAAC;IAE7C;;;;;;;;;;;;;;;;OAgBG;IACH,UAAU,IAAI,UAAU,CAAC,IAAI,CAAC,CAAC;IAE/B;;;;;;;;;;;;;;;;;;;;OAoBG;IACH,aAAa,IAAI,UAAU,CAAC,UAAU,CAAC,CAAC;IAExC;;;;;;;;;;;;;;;;;;;;;;;;;OAyBG;IACH,WAAW,CAAC,KAAK,EAAE,UAAU,CAAC,UAAU,CAAC,GAAG,UAAU,CAAC,IAAI,CAAC,CAAC;IAE7D;;;;;;;;;;;;;;;;;;;;;OAqBG;IACH,KAAK,CAAC,IAAI,EAAE,UAAU,GAAG,UAAU,CAAC,IAAI,CAAC,CAAC;IAE1C;;;;OAIG;IACH,QAAQ,CAAC,SAAS,EAAE,OAAO,CAAC;IAE5B;;;;;;;OAOG;IACH,QAAQ,CAAC,WAAW,EAAE,UAAU,GAAG,IAAI,CAAC;CACzC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,wBAAgB,kBAAkB,CAChC,OAAO,CAAC,EAAE,mBAAmB,GAC5B,YAAY,CAEd"}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { Observable } from 'rxjs';
|
|
2
|
+
import { SerialClientOptions } from '../types/options';
|
|
3
|
+
/**
|
|
4
|
+
* Internal implementation of SerialClient interface.
|
|
5
|
+
*
|
|
6
|
+
* This class implements the {@link SerialClient} interface and provides the actual
|
|
7
|
+
* functionality for serial port communication. Users should not instantiate this class
|
|
8
|
+
* directly; instead, use {@link createSerialClient} to create a SerialClient instance.
|
|
9
|
+
*
|
|
10
|
+
* @internal
|
|
11
|
+
*/
|
|
12
|
+
export declare class SerialClientImpl {
|
|
13
|
+
/** @internal */
|
|
14
|
+
private port;
|
|
15
|
+
/** @internal */
|
|
16
|
+
private isOpen;
|
|
17
|
+
/** @internal */
|
|
18
|
+
private readSubscription;
|
|
19
|
+
/** @internal */
|
|
20
|
+
private writeSubscription;
|
|
21
|
+
/** @internal */
|
|
22
|
+
private readonly options;
|
|
23
|
+
/**
|
|
24
|
+
* Creates a new SerialClientImpl instance.
|
|
25
|
+
*
|
|
26
|
+
* @param options - Optional configuration options for the serial port connection
|
|
27
|
+
* @throws {@link SerialError} with code {@link SerialErrorCode.BROWSER_NOT_SUPPORTED} if the browser doesn't support Web Serial API
|
|
28
|
+
* @internal
|
|
29
|
+
*/
|
|
30
|
+
constructor(options?: SerialClientOptions);
|
|
31
|
+
/**
|
|
32
|
+
* Request a serial port from the user.
|
|
33
|
+
*
|
|
34
|
+
* @returns Observable that emits the selected SerialPort
|
|
35
|
+
* @internal
|
|
36
|
+
*/
|
|
37
|
+
requestPort(): Observable<SerialPort>;
|
|
38
|
+
/**
|
|
39
|
+
* Get available serial ports.
|
|
40
|
+
*
|
|
41
|
+
* @returns Observable that emits an array of available SerialPorts
|
|
42
|
+
* @internal
|
|
43
|
+
*/
|
|
44
|
+
getPorts(): Observable<SerialPort[]>;
|
|
45
|
+
/**
|
|
46
|
+
* Connect to a serial port.
|
|
47
|
+
*
|
|
48
|
+
* @param port - Optional SerialPort to connect to. If not provided, will request one.
|
|
49
|
+
* @returns Observable that completes when the port is opened
|
|
50
|
+
* @internal
|
|
51
|
+
*/
|
|
52
|
+
connect(port?: SerialPort): Observable<void>;
|
|
53
|
+
/**
|
|
54
|
+
* Disconnect from the serial port.
|
|
55
|
+
*
|
|
56
|
+
* @returns Observable that completes when the port is closed
|
|
57
|
+
* @internal
|
|
58
|
+
*/
|
|
59
|
+
disconnect(): Observable<void>;
|
|
60
|
+
/**
|
|
61
|
+
* Get an Observable that emits data read from the serial port.
|
|
62
|
+
*
|
|
63
|
+
* @returns Observable that emits Uint8Array chunks
|
|
64
|
+
* @internal
|
|
65
|
+
*/
|
|
66
|
+
getReadStream(): Observable<Uint8Array>;
|
|
67
|
+
/**
|
|
68
|
+
* Write data to the serial port from an Observable.
|
|
69
|
+
*
|
|
70
|
+
* @param data$ - Observable that emits Uint8Array chunks to write
|
|
71
|
+
* @returns Observable that completes when writing is finished
|
|
72
|
+
* @internal
|
|
73
|
+
*/
|
|
74
|
+
writeStream(data$: Observable<Uint8Array>): Observable<void>;
|
|
75
|
+
/**
|
|
76
|
+
* Write a single chunk of data to the serial port.
|
|
77
|
+
*
|
|
78
|
+
* @param data - Data to write
|
|
79
|
+
* @returns Observable that completes when the data is written
|
|
80
|
+
* @internal
|
|
81
|
+
*/
|
|
82
|
+
write(data: Uint8Array): Observable<void>;
|
|
83
|
+
/**
|
|
84
|
+
* Check if the port is currently open.
|
|
85
|
+
*
|
|
86
|
+
* @returns `true` if a port is currently open, `false` otherwise
|
|
87
|
+
* @internal
|
|
88
|
+
*/
|
|
89
|
+
get connected(): boolean;
|
|
90
|
+
/**
|
|
91
|
+
* Get the current SerialPort instance.
|
|
92
|
+
*
|
|
93
|
+
* @returns The current SerialPort instance, or `null` if no port is open
|
|
94
|
+
* @internal
|
|
95
|
+
*/
|
|
96
|
+
get currentPort(): SerialPort | null;
|
|
97
|
+
}
|
|
98
|
+
//# sourceMappingURL=serial-client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"serial-client.d.ts","sourceRoot":"","sources":["../../src/client/serial-client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAoB,MAAM,MAAM,CAAC;AAMpD,OAAO,EAEL,mBAAmB,EACpB,MAAM,kBAAkB,CAAC;AAE1B;;;;;;;;GAQG;AACH,qBAAa,gBAAgB;IAC3B,gBAAgB;IAChB,OAAO,CAAC,IAAI,CAA2B;IACvC,gBAAgB;IAChB,OAAO,CAAC,MAAM,CAAS;IACvB,gBAAgB;IAChB,OAAO,CAAC,gBAAgB,CAA4C;IACpE,gBAAgB;IAChB,OAAO,CAAC,iBAAiB,CAA4C;IACrE,gBAAgB;IAChB,OAAO,CAAC,QAAQ,CAAC,OAAO,CAEtB;IAEF;;;;;;OAMG;gBACS,OAAO,CAAC,EAAE,mBAAmB;IASzC;;;;;OAKG;IACH,WAAW,IAAI,UAAU,CAAC,UAAU,CAAC;IAuBrC;;;;;OAKG;IACH,QAAQ,IAAI,UAAU,CAAC,UAAU,EAAE,CAAC;IAcpC;;;;;;OAMG;IACH,OAAO,CAAC,IAAI,CAAC,EAAE,UAAU,GAAG,UAAU,CAAC,IAAI,CAAC;IAyD5C;;;;;OAKG;IACH,UAAU,IAAI,UAAU,CAAC,IAAI,CAAC;IAqC9B;;;;;OAKG;IACH,aAAa,IAAI,UAAU,CAAC,UAAU,CAAC;IAWvC;;;;;;OAMG;IACH,WAAW,CAAC,KAAK,EAAE,UAAU,CAAC,UAAU,CAAC,GAAG,UAAU,CAAC,IAAI,CAAC;IAuD5D;;;;;;OAMG;IACH,KAAK,CAAC,IAAI,EAAE,UAAU,GAAG,UAAU,CAAC,IAAI,CAAC;IA0BzC;;;;;OAKG;IACH,IAAI,SAAS,IAAI,OAAO,CAEvB;IAED;;;;;OAKG;IACH,IAAI,WAAW,IAAI,UAAU,GAAG,IAAI,CAEnC;CACF"}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Error codes for serial port operations.
|
|
3
|
+
*
|
|
4
|
+
* These codes identify specific error conditions that can occur when working with
|
|
5
|
+
* serial ports. Each error code corresponds to a specific failure scenario, making
|
|
6
|
+
* it easier to handle errors programmatically.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* try {
|
|
11
|
+
* await client.connect().toPromise();
|
|
12
|
+
* } catch (error) {
|
|
13
|
+
* if (error instanceof SerialError) {
|
|
14
|
+
* switch (error.code) {
|
|
15
|
+
* case SerialErrorCode.BROWSER_NOT_SUPPORTED:
|
|
16
|
+
* console.error('Please use a Chromium-based browser');
|
|
17
|
+
* break;
|
|
18
|
+
* case SerialErrorCode.OPERATION_CANCELLED:
|
|
19
|
+
* console.log('User cancelled port selection');
|
|
20
|
+
* break;
|
|
21
|
+
* // ... handle other error codes
|
|
22
|
+
* }
|
|
23
|
+
* }
|
|
24
|
+
* }
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
export declare enum SerialErrorCode {
|
|
28
|
+
/**
|
|
29
|
+
* Browser does not support the Web Serial API.
|
|
30
|
+
*
|
|
31
|
+
* This error occurs when attempting to use serial port functionality in a browser
|
|
32
|
+
* that doesn't support the Web Serial API. Only Chromium-based browsers (Chrome,
|
|
33
|
+
* Edge, Opera) support this API.
|
|
34
|
+
*
|
|
35
|
+
* **Suggested action**: Inform the user to use a supported browser.
|
|
36
|
+
*/
|
|
37
|
+
BROWSER_NOT_SUPPORTED = "BROWSER_NOT_SUPPORTED",
|
|
38
|
+
/**
|
|
39
|
+
* Serial port is not available.
|
|
40
|
+
*
|
|
41
|
+
* This error occurs when a requested port cannot be accessed, such as when
|
|
42
|
+
* getting previously granted ports fails or when the port is already in use
|
|
43
|
+
* by another application.
|
|
44
|
+
*
|
|
45
|
+
* **Suggested action**: Check if the port is available or being used by another application.
|
|
46
|
+
*/
|
|
47
|
+
PORT_NOT_AVAILABLE = "PORT_NOT_AVAILABLE",
|
|
48
|
+
/**
|
|
49
|
+
* Failed to open the serial port.
|
|
50
|
+
*
|
|
51
|
+
* This error occurs when the port cannot be opened, typically due to incorrect
|
|
52
|
+
* connection parameters, hardware issues, or permission problems.
|
|
53
|
+
*
|
|
54
|
+
* **Suggested action**: Verify connection parameters and check hardware connections.
|
|
55
|
+
*/
|
|
56
|
+
PORT_OPEN_FAILED = "PORT_OPEN_FAILED",
|
|
57
|
+
/**
|
|
58
|
+
* Serial port is already open.
|
|
59
|
+
*
|
|
60
|
+
* This error occurs when attempting to open a port that is already connected.
|
|
61
|
+
* Only one connection can be active at a time per SerialClient instance.
|
|
62
|
+
*
|
|
63
|
+
* **Suggested action**: Disconnect the current port before connecting a new one.
|
|
64
|
+
*/
|
|
65
|
+
PORT_ALREADY_OPEN = "PORT_ALREADY_OPEN",
|
|
66
|
+
/**
|
|
67
|
+
* Serial port is not open.
|
|
68
|
+
*
|
|
69
|
+
* This error occurs when attempting to read from or write to a port that hasn't
|
|
70
|
+
* been opened yet. The port must be connected before performing I/O operations.
|
|
71
|
+
*
|
|
72
|
+
* **Suggested action**: Call {@link SerialClient.connect} before reading or writing.
|
|
73
|
+
*/
|
|
74
|
+
PORT_NOT_OPEN = "PORT_NOT_OPEN",
|
|
75
|
+
/**
|
|
76
|
+
* Failed to read from the serial port.
|
|
77
|
+
*
|
|
78
|
+
* This error occurs when reading data from the port fails, typically due to
|
|
79
|
+
* connection loss, hardware issues, or stream errors.
|
|
80
|
+
*
|
|
81
|
+
* **Suggested action**: Check the connection and hardware, then retry the read operation.
|
|
82
|
+
*/
|
|
83
|
+
READ_FAILED = "READ_FAILED",
|
|
84
|
+
/**
|
|
85
|
+
* Failed to write to the serial port.
|
|
86
|
+
*
|
|
87
|
+
* This error occurs when writing data to the port fails, typically due to
|
|
88
|
+
* connection loss, hardware issues, or stream errors.
|
|
89
|
+
*
|
|
90
|
+
* **Suggested action**: Check the connection and hardware, then retry the write operation.
|
|
91
|
+
*/
|
|
92
|
+
WRITE_FAILED = "WRITE_FAILED",
|
|
93
|
+
/**
|
|
94
|
+
* Serial port connection was lost.
|
|
95
|
+
*
|
|
96
|
+
* This error occurs when the connection to the serial port is unexpectedly
|
|
97
|
+
* terminated, such as when the device is disconnected or the port is closed
|
|
98
|
+
* by another process.
|
|
99
|
+
*
|
|
100
|
+
* **Suggested action**: Check the physical connection and reconnect if needed.
|
|
101
|
+
*/
|
|
102
|
+
CONNECTION_LOST = "CONNECTION_LOST",
|
|
103
|
+
/**
|
|
104
|
+
* Invalid filter options provided.
|
|
105
|
+
*
|
|
106
|
+
* This error occurs when port filter options are invalid, such as when
|
|
107
|
+
* filter values are out of range or missing required fields.
|
|
108
|
+
*
|
|
109
|
+
* **Suggested action**: Verify filter options match the expected format and value ranges.
|
|
110
|
+
*/
|
|
111
|
+
INVALID_FILTER_OPTIONS = "INVALID_FILTER_OPTIONS",
|
|
112
|
+
/**
|
|
113
|
+
* Operation was cancelled by the user.
|
|
114
|
+
*
|
|
115
|
+
* This error occurs when the user cancels a port selection dialog or aborts
|
|
116
|
+
* an operation before it completes.
|
|
117
|
+
*
|
|
118
|
+
* **Suggested action**: This is a normal condition - no action required, but you may want
|
|
119
|
+
* to inform the user that the operation was cancelled.
|
|
120
|
+
*/
|
|
121
|
+
OPERATION_CANCELLED = "OPERATION_CANCELLED",
|
|
122
|
+
/**
|
|
123
|
+
* Unknown error occurred.
|
|
124
|
+
*
|
|
125
|
+
* This error code is used for errors that don't fit into any other category.
|
|
126
|
+
* The original error details may be available in the error's message or originalError property.
|
|
127
|
+
*
|
|
128
|
+
* **Suggested action**: Check the error message and originalError for more details.
|
|
129
|
+
*/
|
|
130
|
+
UNKNOWN = "UNKNOWN"
|
|
131
|
+
}
|
|
132
|
+
//# sourceMappingURL=serial-error-code.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"serial-error-code.d.ts","sourceRoot":"","sources":["../../src/errors/serial-error-code.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,oBAAY,eAAe;IACzB;;;;;;;;OAQG;IACH,qBAAqB,0BAA0B;IAE/C;;;;;;;;OAQG;IACH,kBAAkB,uBAAuB;IAEzC;;;;;;;OAOG;IACH,gBAAgB,qBAAqB;IAErC;;;;;;;OAOG;IACH,iBAAiB,sBAAsB;IAEvC;;;;;;;OAOG;IACH,aAAa,kBAAkB;IAE/B;;;;;;;OAOG;IACH,WAAW,gBAAgB;IAE3B;;;;;;;OAOG;IACH,YAAY,iBAAiB;IAE7B;;;;;;;;OAQG;IACH,eAAe,oBAAoB;IAEnC;;;;;;;OAOG;IACH,sBAAsB,2BAA2B;IAEjD;;;;;;;;OAQG;IACH,mBAAmB,wBAAwB;IAE3C;;;;;;;OAOG;IACH,OAAO,YAAY;CACpB"}
|