@sybz-components/utils 0.0.5 → 0.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/test.cjs CHANGED
@@ -1,16 +1,7 @@
1
- const { createJiti } = require("../../../node_modules/.pnpm/jiti@2.5.1/node_modules/jiti/lib/jiti.cjs")
1
+ 'use strict';
2
2
 
3
- const jiti = createJiti(__filename, {
4
- "interopDefault": true,
5
- "alias": {
6
- "@sybz-components/utils": "/Users/liulihao/sybz/sybz-components/packages/utils"
7
- },
8
- "transformOptions": {
9
- "babel": {
10
- "plugins": []
11
- }
12
- }
13
- })
3
+ function onlyTest() {
4
+ return "test";
5
+ }
14
6
 
15
- /** @type {import("/Users/liulihao/sybz/sybz-components/packages/utils/src/test")} */
16
- module.exports = jiti("/Users/liulihao/sybz/sybz-components/packages/utils/src/test.ts")
7
+ exports.onlyTest = onlyTest;
package/dist/test.d.cts CHANGED
@@ -1 +1,14 @@
1
- export * from "/Users/liulihao/sybz/sybz-components/packages/utils/src/test";
1
+ /**
2
+ * 返回当前测试占位字符串。
3
+ *
4
+ * 这个方法主要用于确认工具函数导出链路是否正常。
5
+ *
6
+ * @returns 固定字符串 `test`。
7
+ *
8
+ * @example
9
+ * onlyTest()
10
+ * // => 'test'
11
+ */
12
+ declare function onlyTest(): string;
13
+
14
+ export { onlyTest };
package/dist/test.d.mts CHANGED
@@ -1 +1,14 @@
1
- export * from "/Users/liulihao/sybz/sybz-components/packages/utils/src/test";
1
+ /**
2
+ * 返回当前测试占位字符串。
3
+ *
4
+ * 这个方法主要用于确认工具函数导出链路是否正常。
5
+ *
6
+ * @returns 固定字符串 `test`。
7
+ *
8
+ * @example
9
+ * onlyTest()
10
+ * // => 'test'
11
+ */
12
+ declare function onlyTest(): string;
13
+
14
+ export { onlyTest };
package/dist/test.d.ts CHANGED
@@ -1 +1,14 @@
1
- export * from "/Users/liulihao/sybz/sybz-components/packages/utils/src/test";
1
+ /**
2
+ * 返回当前测试占位字符串。
3
+ *
4
+ * 这个方法主要用于确认工具函数导出链路是否正常。
5
+ *
6
+ * @returns 固定字符串 `test`。
7
+ *
8
+ * @example
9
+ * onlyTest()
10
+ * // => 'test'
11
+ */
12
+ declare function onlyTest(): string;
13
+
14
+ export { onlyTest };
package/dist/test.mjs CHANGED
@@ -1,18 +1,5 @@
1
- import { createJiti } from "../../../node_modules/.pnpm/jiti@2.5.1/node_modules/jiti/lib/jiti.mjs";
1
+ function onlyTest() {
2
+ return "test";
3
+ }
2
4
 
3
- const jiti = createJiti(import.meta.url, {
4
- "interopDefault": true,
5
- "alias": {
6
- "@sybz-components/utils": "/Users/liulihao/sybz/sybz-components/packages/utils"
7
- },
8
- "transformOptions": {
9
- "babel": {
10
- "plugins": []
11
- }
12
- }
13
- })
14
-
15
- /** @type {import("/Users/liulihao/sybz/sybz-components/packages/utils/src/test")} */
16
- const _module = await jiti.import("/Users/liulihao/sybz/sybz-components/packages/utils/src/test.ts");
17
-
18
- export const onlyTest = _module.onlyTest;
5
+ export { onlyTest };
package/dist/ws.cjs CHANGED
@@ -1,16 +1,175 @@
1
- const { createJiti } = require("../../../node_modules/.pnpm/jiti@2.5.1/node_modules/jiti/lib/jiti.cjs")
1
+ 'use strict';
2
2
 
3
- const jiti = createJiti(__filename, {
4
- "interopDefault": true,
5
- "alias": {
6
- "@sybz-components/utils": "/Users/liulihao/sybz/sybz-components/packages/utils"
7
- },
8
- "transformOptions": {
9
- "babel": {
10
- "plugins": []
3
+ const qs = require('qs');
4
+
5
+ function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
6
+
7
+ const qs__default = /*#__PURE__*/_interopDefaultCompat(qs);
8
+
9
+ const reconnectMaxCount = 3;
10
+ const message = "ping";
11
+ const interval = 3e3;
12
+ const timeout = 1e3;
13
+ class WS {
14
+ url;
15
+ socket = null;
16
+ reconnectCount = 0;
17
+ delay = null;
18
+ timer = null;
19
+ autoReconnect;
20
+ heartbeat;
21
+ query;
22
+ /**
23
+ * 创建并立即连接一个 WebSocket 实例。
24
+ *
25
+ * @param url WebSocket 地址,例如 `wss://example.com/ws`。
26
+ * @param options 连接配置,支持重连、心跳和 query 参数。
27
+ *
28
+ * @example
29
+ * const ws = new WS('wss://example.com/ws', {
30
+ * autoReconnect: { reconnectMaxCount: 5 },
31
+ * heartbeat: { message: 'ping', interval: 5000 },
32
+ * query: { token: 'abc123' },
33
+ * })
34
+ */
35
+ constructor(url, options) {
36
+ const { autoReconnect = true, query = {}, heartbeat = false } = options || {};
37
+ this.autoReconnect = autoReconnect;
38
+ this.heartbeat = heartbeat;
39
+ this.query = query;
40
+ this.url = `${url}` + qs__default.stringify({ ...this.query }, { addQueryPrefix: true });
41
+ this.connect();
42
+ }
43
+ /**
44
+ * 主动发起连接。
45
+ *
46
+ * 调用时会先关闭旧连接,再创建新的 WebSocket 实例。
47
+ *
48
+ * @example
49
+ * const ws = new WS('wss://example.com/ws')
50
+ * ws.connect()
51
+ */
52
+ connect() {
53
+ this.close();
54
+ this.socket = new WebSocket(this.url);
55
+ this.onError();
56
+ this.onOpen();
57
+ }
58
+ /**
59
+ * 注册连接成功后的处理逻辑,并在需要时开启心跳。
60
+ *
61
+ * @example
62
+ * const ws = new WS('wss://example.com/ws')
63
+ * ws.onOpen()
64
+ */
65
+ onOpen() {
66
+ if (this.socket) {
67
+ this.socket.onopen = () => {
68
+ this.send("ping");
69
+ this.heartbeat && this.startHeartbeat();
70
+ };
71
+ }
72
+ }
73
+ /**
74
+ * 开启心跳发送。
75
+ *
76
+ * @example
77
+ * const ws = new WS('wss://example.com/ws', {
78
+ * heartbeat: { message: 'ping', interval: 3000 },
79
+ * })
80
+ * ws.startHeartbeat()
81
+ */
82
+ startHeartbeat() {
83
+ const msg = this.heartbeat?.message || message;
84
+ const int = this.heartbeat?.interval || interval;
85
+ this.timer = setInterval(() => {
86
+ this.send(msg);
87
+ }, int);
88
+ }
89
+ /**
90
+ * 注册错误处理和自动重连逻辑。
91
+ *
92
+ * @example
93
+ * const ws = new WS('wss://example.com/ws', { autoReconnect: true })
94
+ * ws.onError()
95
+ */
96
+ onError() {
97
+ if (this.socket) {
98
+ this.socket.onerror = () => {
99
+ const count = this.autoReconnect?.reconnectMaxCount || reconnectMaxCount;
100
+ if (this.autoReconnect && this.reconnectCount < count) {
101
+ this.reconnectCount++;
102
+ this.connect();
103
+ }
104
+ };
105
+ }
106
+ }
107
+ /**
108
+ * 关闭连接并清理相关定时器。
109
+ *
110
+ * @example
111
+ * ws.close()
112
+ */
113
+ close() {
114
+ this.socket && this.socket.close();
115
+ this.delay && clearTimeout(this.delay);
116
+ this.timer && clearInterval(this.timer);
117
+ this.socket = null;
118
+ }
119
+ /**
120
+ * 监听服务端消息。
121
+ *
122
+ * 内部会优先尝试将消息解析为 JSON;解析失败时会回传原始 `MessageEvent`。
123
+ *
124
+ * @param callback 接收到消息时触发的回调。
125
+ *
126
+ * @example
127
+ * ws.onMessage((message) => {
128
+ * console.log(message)
129
+ * })
130
+ */
131
+ onMessage(callback) {
132
+ if (this.socket) {
133
+ this.socket.onmessage = (data) => {
134
+ try {
135
+ const res = JSON.parse(data.data);
136
+ callback(res);
137
+ } catch (err) {
138
+ callback(data);
139
+ }
140
+ };
141
+ }
142
+ }
143
+ /**
144
+ * 发送消息。
145
+ *
146
+ * - 如果连接已就绪,会立即发送
147
+ * - 如果正在连接,会延迟发送
148
+ * - 如果连接已关闭,会先重新连接再发送
149
+ *
150
+ * @param data 需要发送的消息内容。
151
+ *
152
+ * @example
153
+ * ws.send('hello')
154
+ *
155
+ * @example
156
+ * ws.send(JSON.stringify({ type: 'ping' }))
157
+ */
158
+ send(data) {
159
+ if (!this.socket) return;
160
+ if (this.socket.readyState === this.socket.OPEN) {
161
+ this.socket.send(JSON.stringify(data));
162
+ } else if (this.socket.readyState === this.socket.CONNECTING) {
163
+ this.delay = setTimeout(() => {
164
+ this.socket?.send(data);
165
+ }, timeout);
166
+ } else {
167
+ this.connect();
168
+ this.delay = setTimeout(() => {
169
+ this.socket?.send(data);
170
+ }, timeout);
11
171
  }
12
172
  }
13
- })
173
+ }
14
174
 
15
- /** @type {import("/Users/liulihao/sybz/sybz-components/packages/utils/src/ws")} */
16
- module.exports = jiti("/Users/liulihao/sybz/sybz-components/packages/utils/src/ws.ts")
175
+ exports.WS = WS;
package/dist/ws.d.cts CHANGED
@@ -1 +1,165 @@
1
- export * from "/Users/liulihao/sybz/sybz-components/packages/utils/src/ws";
1
+ type Timeout = ReturnType<typeof setTimeout>;
2
+ type Interval = ReturnType<typeof setInterval>;
3
+ type Nullable<T> = T | null;
4
+ /**
5
+ * WebSocket 自动重连配置。
6
+ */
7
+ interface WSAutoReconnectOptions {
8
+ /**
9
+ * 最大重连次数,默认 `3`。
10
+ */
11
+ reconnectMaxCount?: number;
12
+ }
13
+ /**
14
+ * WebSocket 心跳配置。
15
+ */
16
+ interface WSHeartbeatOptions {
17
+ /**
18
+ * 心跳消息内容,默认 `ping`。
19
+ */
20
+ message: string;
21
+ /**
22
+ * 心跳间隔,单位毫秒,默认 `3000`。
23
+ */
24
+ interval: number;
25
+ }
26
+ /**
27
+ * `WS` 构造函数可选配置。
28
+ */
29
+ interface WSOptions {
30
+ /**
31
+ * 是否自动重连。
32
+ *
33
+ * - `true`: 使用默认重连策略
34
+ * - `false`: 不自动重连
35
+ * - 对象: 自定义重连次数
36
+ */
37
+ autoReconnect: boolean | WSAutoReconnectOptions;
38
+ /**
39
+ * 是否开启心跳。
40
+ *
41
+ * - `false`: 不发送心跳
42
+ * - `true`: 使用默认心跳策略
43
+ * - 对象: 自定义心跳内容和间隔
44
+ */
45
+ heartbeat: boolean | WSHeartbeatOptions;
46
+ /**
47
+ * 需要拼接到 url 上的查询参数。
48
+ */
49
+ query: Record<string, string>;
50
+ }
51
+ /**
52
+ * WebSocket 轻量封装,支持自动连接、自动重连、心跳和 query 参数。
53
+ *
54
+ * @example
55
+ * const ws = new WS('wss://example.com/ws', {
56
+ * autoReconnect: true,
57
+ * heartbeat: { message: 'ping', interval: 3000 },
58
+ * query: { token: 'demo-token' },
59
+ * })
60
+ *
61
+ * ws.onMessage((message) => {
62
+ * console.log(message)
63
+ * })
64
+ *
65
+ * ws.send('hello')
66
+ */
67
+ declare class WS {
68
+ url: string;
69
+ socket: WebSocket | null;
70
+ reconnectCount: number;
71
+ delay: Nullable<Timeout>;
72
+ timer: Nullable<Interval>;
73
+ autoReconnect: WSOptions['autoReconnect'];
74
+ heartbeat: WSOptions['heartbeat'];
75
+ query: WSOptions['query'];
76
+ /**
77
+ * 创建并立即连接一个 WebSocket 实例。
78
+ *
79
+ * @param url WebSocket 地址,例如 `wss://example.com/ws`。
80
+ * @param options 连接配置,支持重连、心跳和 query 参数。
81
+ *
82
+ * @example
83
+ * const ws = new WS('wss://example.com/ws', {
84
+ * autoReconnect: { reconnectMaxCount: 5 },
85
+ * heartbeat: { message: 'ping', interval: 5000 },
86
+ * query: { token: 'abc123' },
87
+ * })
88
+ */
89
+ constructor(url?: string, options?: WSOptions);
90
+ /**
91
+ * 主动发起连接。
92
+ *
93
+ * 调用时会先关闭旧连接,再创建新的 WebSocket 实例。
94
+ *
95
+ * @example
96
+ * const ws = new WS('wss://example.com/ws')
97
+ * ws.connect()
98
+ */
99
+ connect(): void;
100
+ /**
101
+ * 注册连接成功后的处理逻辑,并在需要时开启心跳。
102
+ *
103
+ * @example
104
+ * const ws = new WS('wss://example.com/ws')
105
+ * ws.onOpen()
106
+ */
107
+ onOpen(): void;
108
+ /**
109
+ * 开启心跳发送。
110
+ *
111
+ * @example
112
+ * const ws = new WS('wss://example.com/ws', {
113
+ * heartbeat: { message: 'ping', interval: 3000 },
114
+ * })
115
+ * ws.startHeartbeat()
116
+ */
117
+ startHeartbeat(): void;
118
+ /**
119
+ * 注册错误处理和自动重连逻辑。
120
+ *
121
+ * @example
122
+ * const ws = new WS('wss://example.com/ws', { autoReconnect: true })
123
+ * ws.onError()
124
+ */
125
+ onError(): void;
126
+ /**
127
+ * 关闭连接并清理相关定时器。
128
+ *
129
+ * @example
130
+ * ws.close()
131
+ */
132
+ close(): void;
133
+ /**
134
+ * 监听服务端消息。
135
+ *
136
+ * 内部会优先尝试将消息解析为 JSON;解析失败时会回传原始 `MessageEvent`。
137
+ *
138
+ * @param callback 接收到消息时触发的回调。
139
+ *
140
+ * @example
141
+ * ws.onMessage((message) => {
142
+ * console.log(message)
143
+ * })
144
+ */
145
+ onMessage(callback: (data: unknown) => any): void;
146
+ /**
147
+ * 发送消息。
148
+ *
149
+ * - 如果连接已就绪,会立即发送
150
+ * - 如果正在连接,会延迟发送
151
+ * - 如果连接已关闭,会先重新连接再发送
152
+ *
153
+ * @param data 需要发送的消息内容。
154
+ *
155
+ * @example
156
+ * ws.send('hello')
157
+ *
158
+ * @example
159
+ * ws.send(JSON.stringify({ type: 'ping' }))
160
+ */
161
+ send(data: string | ArrayBufferLike | Blob | ArrayBufferView): void;
162
+ }
163
+
164
+ export { WS };
165
+ export type { WSAutoReconnectOptions, WSHeartbeatOptions, WSOptions };
package/dist/ws.d.mts CHANGED
@@ -1 +1,165 @@
1
- export * from "/Users/liulihao/sybz/sybz-components/packages/utils/src/ws";
1
+ type Timeout = ReturnType<typeof setTimeout>;
2
+ type Interval = ReturnType<typeof setInterval>;
3
+ type Nullable<T> = T | null;
4
+ /**
5
+ * WebSocket 自动重连配置。
6
+ */
7
+ interface WSAutoReconnectOptions {
8
+ /**
9
+ * 最大重连次数,默认 `3`。
10
+ */
11
+ reconnectMaxCount?: number;
12
+ }
13
+ /**
14
+ * WebSocket 心跳配置。
15
+ */
16
+ interface WSHeartbeatOptions {
17
+ /**
18
+ * 心跳消息内容,默认 `ping`。
19
+ */
20
+ message: string;
21
+ /**
22
+ * 心跳间隔,单位毫秒,默认 `3000`。
23
+ */
24
+ interval: number;
25
+ }
26
+ /**
27
+ * `WS` 构造函数可选配置。
28
+ */
29
+ interface WSOptions {
30
+ /**
31
+ * 是否自动重连。
32
+ *
33
+ * - `true`: 使用默认重连策略
34
+ * - `false`: 不自动重连
35
+ * - 对象: 自定义重连次数
36
+ */
37
+ autoReconnect: boolean | WSAutoReconnectOptions;
38
+ /**
39
+ * 是否开启心跳。
40
+ *
41
+ * - `false`: 不发送心跳
42
+ * - `true`: 使用默认心跳策略
43
+ * - 对象: 自定义心跳内容和间隔
44
+ */
45
+ heartbeat: boolean | WSHeartbeatOptions;
46
+ /**
47
+ * 需要拼接到 url 上的查询参数。
48
+ */
49
+ query: Record<string, string>;
50
+ }
51
+ /**
52
+ * WebSocket 轻量封装,支持自动连接、自动重连、心跳和 query 参数。
53
+ *
54
+ * @example
55
+ * const ws = new WS('wss://example.com/ws', {
56
+ * autoReconnect: true,
57
+ * heartbeat: { message: 'ping', interval: 3000 },
58
+ * query: { token: 'demo-token' },
59
+ * })
60
+ *
61
+ * ws.onMessage((message) => {
62
+ * console.log(message)
63
+ * })
64
+ *
65
+ * ws.send('hello')
66
+ */
67
+ declare class WS {
68
+ url: string;
69
+ socket: WebSocket | null;
70
+ reconnectCount: number;
71
+ delay: Nullable<Timeout>;
72
+ timer: Nullable<Interval>;
73
+ autoReconnect: WSOptions['autoReconnect'];
74
+ heartbeat: WSOptions['heartbeat'];
75
+ query: WSOptions['query'];
76
+ /**
77
+ * 创建并立即连接一个 WebSocket 实例。
78
+ *
79
+ * @param url WebSocket 地址,例如 `wss://example.com/ws`。
80
+ * @param options 连接配置,支持重连、心跳和 query 参数。
81
+ *
82
+ * @example
83
+ * const ws = new WS('wss://example.com/ws', {
84
+ * autoReconnect: { reconnectMaxCount: 5 },
85
+ * heartbeat: { message: 'ping', interval: 5000 },
86
+ * query: { token: 'abc123' },
87
+ * })
88
+ */
89
+ constructor(url?: string, options?: WSOptions);
90
+ /**
91
+ * 主动发起连接。
92
+ *
93
+ * 调用时会先关闭旧连接,再创建新的 WebSocket 实例。
94
+ *
95
+ * @example
96
+ * const ws = new WS('wss://example.com/ws')
97
+ * ws.connect()
98
+ */
99
+ connect(): void;
100
+ /**
101
+ * 注册连接成功后的处理逻辑,并在需要时开启心跳。
102
+ *
103
+ * @example
104
+ * const ws = new WS('wss://example.com/ws')
105
+ * ws.onOpen()
106
+ */
107
+ onOpen(): void;
108
+ /**
109
+ * 开启心跳发送。
110
+ *
111
+ * @example
112
+ * const ws = new WS('wss://example.com/ws', {
113
+ * heartbeat: { message: 'ping', interval: 3000 },
114
+ * })
115
+ * ws.startHeartbeat()
116
+ */
117
+ startHeartbeat(): void;
118
+ /**
119
+ * 注册错误处理和自动重连逻辑。
120
+ *
121
+ * @example
122
+ * const ws = new WS('wss://example.com/ws', { autoReconnect: true })
123
+ * ws.onError()
124
+ */
125
+ onError(): void;
126
+ /**
127
+ * 关闭连接并清理相关定时器。
128
+ *
129
+ * @example
130
+ * ws.close()
131
+ */
132
+ close(): void;
133
+ /**
134
+ * 监听服务端消息。
135
+ *
136
+ * 内部会优先尝试将消息解析为 JSON;解析失败时会回传原始 `MessageEvent`。
137
+ *
138
+ * @param callback 接收到消息时触发的回调。
139
+ *
140
+ * @example
141
+ * ws.onMessage((message) => {
142
+ * console.log(message)
143
+ * })
144
+ */
145
+ onMessage(callback: (data: unknown) => any): void;
146
+ /**
147
+ * 发送消息。
148
+ *
149
+ * - 如果连接已就绪,会立即发送
150
+ * - 如果正在连接,会延迟发送
151
+ * - 如果连接已关闭,会先重新连接再发送
152
+ *
153
+ * @param data 需要发送的消息内容。
154
+ *
155
+ * @example
156
+ * ws.send('hello')
157
+ *
158
+ * @example
159
+ * ws.send(JSON.stringify({ type: 'ping' }))
160
+ */
161
+ send(data: string | ArrayBufferLike | Blob | ArrayBufferView): void;
162
+ }
163
+
164
+ export { WS };
165
+ export type { WSAutoReconnectOptions, WSHeartbeatOptions, WSOptions };