@mearl/cloud-types 2.1.0 → 2.2.1
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/README.md +18 -5
- package/dist/index.d.ts +36 -0
- package/dist/index.js +41 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -15,6 +15,7 @@ interface CloudMessage {
|
|
|
15
15
|
id: string;
|
|
16
16
|
action: string;
|
|
17
17
|
data: Record<string, any>;
|
|
18
|
+
connector?: string;
|
|
18
19
|
browser?: string;
|
|
19
20
|
timeoutSec?: number;
|
|
20
21
|
}
|
|
@@ -31,16 +32,32 @@ interface HeartbeatMessage {
|
|
|
31
32
|
type: 'ping' | 'pong';
|
|
32
33
|
timestamp: number;
|
|
33
34
|
}
|
|
35
|
+
|
|
36
|
+
interface AgentHelloMessage {
|
|
37
|
+
type: 'agent_hello';
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
interface ConnectorHelloMessage {
|
|
41
|
+
type: 'connector_hello';
|
|
42
|
+
connectorId: string;
|
|
43
|
+
name: string;
|
|
44
|
+
version?: string;
|
|
45
|
+
}
|
|
34
46
|
```
|
|
35
47
|
|
|
36
48
|
### 类型守卫
|
|
37
49
|
|
|
38
50
|
```typescript
|
|
51
|
+
isAgentHelloMessage(msg): msg is AgentHelloMessage
|
|
52
|
+
isConnectorHelloMessage(msg): msg is ConnectorHelloMessage
|
|
39
53
|
isHeartbeatMessage(msg): msg is HeartbeatMessage
|
|
40
54
|
isCloudMessage(msg): msg is CloudMessage
|
|
41
55
|
isCloudResponse(msg): msg is CloudResponse
|
|
42
56
|
```
|
|
43
57
|
|
|
58
|
+
`resolveCloudConnectorSelector(connectors, selector)` 按精确 ID、精确名称、唯一模糊匹配的
|
|
59
|
+
顺序解析 connector 目标,并区分匹配、歧义和未找到。
|
|
60
|
+
|
|
44
61
|
### 常量
|
|
45
62
|
|
|
46
63
|
```typescript
|
|
@@ -54,11 +71,7 @@ DEFAULT_HEARTBEAT_TIMEOUT; // 90s
|
|
|
54
71
|
## 使用
|
|
55
72
|
|
|
56
73
|
```typescript
|
|
57
|
-
import {
|
|
58
|
-
type CloudMessage,
|
|
59
|
-
isCloudResponse,
|
|
60
|
-
DEFAULT_REQUEST_TIMEOUT,
|
|
61
|
-
} from '@mearl/cloud-types';
|
|
74
|
+
import { type CloudMessage, isCloudResponse, DEFAULT_REQUEST_TIMEOUT } from '@mearl/cloud-types';
|
|
62
75
|
```
|
|
63
76
|
|
|
64
77
|
## 构建
|
package/dist/index.d.ts
CHANGED
|
@@ -2,6 +2,8 @@ export interface CloudMessage {
|
|
|
2
2
|
id: string;
|
|
3
3
|
action: string;
|
|
4
4
|
data: Record<string, any>;
|
|
5
|
+
/** Target a browser-side cloud connector by id / name. */
|
|
6
|
+
connector?: string;
|
|
5
7
|
/**
|
|
6
8
|
* Target a specific browser on the connector's machine by id / name. Omitted →
|
|
7
9
|
* the connector's default (most-recently-focused) browser.
|
|
@@ -21,11 +23,45 @@ export interface HeartbeatMessage {
|
|
|
21
23
|
type: 'ping' | 'pong';
|
|
22
24
|
timestamp: number;
|
|
23
25
|
}
|
|
26
|
+
export interface AgentHelloMessage {
|
|
27
|
+
type: 'agent_hello';
|
|
28
|
+
}
|
|
29
|
+
export interface ConnectorHelloMessage {
|
|
30
|
+
type: 'connector_hello';
|
|
31
|
+
connectorId: string;
|
|
32
|
+
name: string;
|
|
33
|
+
version?: string;
|
|
34
|
+
}
|
|
35
|
+
export interface CloudConnectorInfo {
|
|
36
|
+
connectorId: string;
|
|
37
|
+
name: string;
|
|
38
|
+
version?: string;
|
|
39
|
+
connectedAt: string;
|
|
40
|
+
lastHeartbeatAt: string;
|
|
41
|
+
pendingRequests: number;
|
|
42
|
+
legacy?: boolean;
|
|
43
|
+
}
|
|
44
|
+
export interface CloudConnectorListResult {
|
|
45
|
+
count: number;
|
|
46
|
+
connectors: CloudConnectorInfo[];
|
|
47
|
+
}
|
|
48
|
+
export type CloudConnectorSelectorResult<T> = {
|
|
49
|
+
status: 'matched';
|
|
50
|
+
connector: T;
|
|
51
|
+
} | {
|
|
52
|
+
status: 'ambiguous';
|
|
53
|
+
} | {
|
|
54
|
+
status: 'not_found';
|
|
55
|
+
};
|
|
24
56
|
export declare const MAX_BUFFER_SIZE: number;
|
|
25
57
|
export declare const DEFAULT_REQUEST_TIMEOUT = 60;
|
|
26
58
|
export declare const DEFAULT_HEARTBEAT_INTERVAL = 30;
|
|
27
59
|
export declare const DEFAULT_CONNECT_TIMEOUT = 30;
|
|
28
60
|
export declare const DEFAULT_HEARTBEAT_TIMEOUT = 90;
|
|
61
|
+
/** Resolve connector targets with one precedence order shared by routing and diagnostics. */
|
|
62
|
+
export declare function resolveCloudConnectorSelector<T extends Pick<ConnectorHelloMessage, 'connectorId' | 'name'>>(connectors: readonly T[], selector: string): CloudConnectorSelectorResult<T>;
|
|
29
63
|
export declare function isHeartbeatMessage(msg: any): msg is HeartbeatMessage;
|
|
64
|
+
export declare function isAgentHelloMessage(msg: any): msg is AgentHelloMessage;
|
|
65
|
+
export declare function isConnectorHelloMessage(msg: any): msg is ConnectorHelloMessage;
|
|
30
66
|
export declare function isCloudMessage(msg: any): msg is CloudMessage;
|
|
31
67
|
export declare function isCloudResponse(msg: any): msg is CloudResponse;
|
package/dist/index.js
CHANGED
|
@@ -3,11 +3,51 @@ export const DEFAULT_REQUEST_TIMEOUT = 60;
|
|
|
3
3
|
export const DEFAULT_HEARTBEAT_INTERVAL = 30;
|
|
4
4
|
export const DEFAULT_CONNECT_TIMEOUT = 30;
|
|
5
5
|
export const DEFAULT_HEARTBEAT_TIMEOUT = 90;
|
|
6
|
+
/** Resolve connector targets with one precedence order shared by routing and diagnostics. */
|
|
7
|
+
export function resolveCloudConnectorSelector(connectors, selector) {
|
|
8
|
+
const normalized = selector.trim().toLowerCase();
|
|
9
|
+
if (!normalized)
|
|
10
|
+
return { status: 'not_found' };
|
|
11
|
+
const resolveMatches = (matches) => {
|
|
12
|
+
if (matches.length === 1)
|
|
13
|
+
return { status: 'matched', connector: matches[0] };
|
|
14
|
+
if (matches.length > 1)
|
|
15
|
+
return { status: 'ambiguous' };
|
|
16
|
+
return null;
|
|
17
|
+
};
|
|
18
|
+
const exactId = resolveMatches(connectors.filter(connector => connector.connectorId.toLowerCase() === normalized));
|
|
19
|
+
if (exactId)
|
|
20
|
+
return exactId;
|
|
21
|
+
const exactName = resolveMatches(connectors.filter(connector => connector.name.toLowerCase() === normalized));
|
|
22
|
+
if (exactName)
|
|
23
|
+
return exactName;
|
|
24
|
+
return (resolveMatches(connectors.filter(connector => connector.connectorId.toLowerCase().includes(normalized) ||
|
|
25
|
+
connector.name.toLowerCase().includes(normalized))) ?? { status: 'not_found' });
|
|
26
|
+
}
|
|
6
27
|
export function isHeartbeatMessage(msg) {
|
|
7
28
|
return msg && (msg.type === 'ping' || msg.type === 'pong');
|
|
8
29
|
}
|
|
30
|
+
export function isAgentHelloMessage(msg) {
|
|
31
|
+
return msg?.type === 'agent_hello';
|
|
32
|
+
}
|
|
33
|
+
export function isConnectorHelloMessage(msg) {
|
|
34
|
+
return (msg?.type === 'connector_hello' &&
|
|
35
|
+
typeof msg.connectorId === 'string' &&
|
|
36
|
+
msg.connectorId.length > 0 &&
|
|
37
|
+
msg.connectorId.length <= 128 &&
|
|
38
|
+
/^[A-Za-z0-9._:-]+$/.test(msg.connectorId) &&
|
|
39
|
+
typeof msg.name === 'string' &&
|
|
40
|
+
msg.name.length > 0 &&
|
|
41
|
+
msg.name.length <= 128 &&
|
|
42
|
+
!/\p{Cc}/u.test(msg.name) &&
|
|
43
|
+
(msg.version === undefined || typeof msg.version === 'string'));
|
|
44
|
+
}
|
|
9
45
|
export function isCloudMessage(msg) {
|
|
10
|
-
return msg &&
|
|
46
|
+
return (msg &&
|
|
47
|
+
typeof msg.id === 'string' &&
|
|
48
|
+
typeof msg.action === 'string' &&
|
|
49
|
+
(msg.connector === undefined || typeof msg.connector === 'string') &&
|
|
50
|
+
(msg.browser === undefined || typeof msg.browser === 'string'));
|
|
11
51
|
}
|
|
12
52
|
export function isCloudResponse(msg) {
|
|
13
53
|
return msg && typeof msg.id === 'string' && typeof msg.success === 'boolean';
|