@mingto/version-polling 1.0.31

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 ADDED
@@ -0,0 +1,83 @@
1
+ # @minto-ai/version-polling
2
+
3
+ `@minto-ai/version-polling` 是一个用于实时检测线上环境版本更新的轻量级工具。它支持通过 HTML 文件的 ETag/Last-Modified 或 JSON 文件的版本号字段来检测更新。
4
+
5
+ ## 特性
6
+
7
+ - 🚀 **零依赖**:轻量级设计,不依赖任何第三方库(Worker 内部实现)。
8
+ - 🔄 **自动轮询**:基于 Web Worker 的定时轮询,不阻塞主线程。
9
+ - 👁️ **智能暂停**:当页面不可见时自动暂停轮询,节省资源;页面可见时自动恢复。
10
+ - 📦 **双模式支持**:
11
+ - **HTML 模式**:检测 HTML 文件的 `ETag` 或 `Last-Modified` 响应头变化。
12
+ - **JSON 模式**:检测 `manifest.json` 或其他 JSON 文件中的 `version` 字段变化(需符合 SemVer 规范)。
13
+
14
+ ## 安装
15
+
16
+ ```bash
17
+ pnpm install @minto-ai/version-polling
18
+ ```
19
+
20
+ ## 使用示例
21
+
22
+ ### 基础用法(检测 HTML)
23
+
24
+ 默认情况下,工具会检测当前页面的 HTML 文件。
25
+
26
+ ```typescript
27
+ import { createVersionPolling } from '@minto-ai/version-polling'
28
+
29
+ const versionPolling = createVersionPolling({
30
+ pollingInterval: 60, // 轮询间隔,单位秒
31
+ })
32
+
33
+ versionPolling.on('update', () => {
34
+ // 发现新版本,可以提示用户刷新页面
35
+ if (confirm('发现新版本,是否刷新?')) {
36
+ window.location.reload()
37
+ }
38
+ })
39
+ ```
40
+
41
+ ### 进阶用法(检测 JSON)
42
+
43
+ 如果你使用 `manifest.json` 或专门的版本文件来管理版本号。
44
+
45
+ ```typescript
46
+ import { createVersionPolling } from '@minto-ai/version-polling'
47
+
48
+ const versionPolling = createVersionPolling({
49
+ // 指定检测的 JSON 文件路径
50
+ fileUrl: 'manifest.json',
51
+ pollingInterval: 30,
52
+ })
53
+
54
+ versionPolling.on('update', () => {
55
+ console.log('检测到新版本发布!')
56
+ })
57
+ ```
58
+
59
+ ## API
60
+
61
+ ### `createVersionPolling(options)`
62
+
63
+ 创建并返回一个版本轮询实例。
64
+
65
+ #### 参数 `options`
66
+
67
+ | 属性 | 类型 | 默认值 | 说明 |
68
+ | ----------------- | -------- | -------------------------- | ------------------------------------------------------------------------------------------- |
69
+ | `pollingInterval` | `number` | `60` | 轮询间隔时间,单位为秒。 |
70
+ | `fileUrl` | `string` | `window.location.pathname` | 目标文件地址。可以是 HTML 路径或 JSON 文件路径。工具会自动根据后缀(`.json`)判断检测模式。 |
71
+
72
+ ### 实例方法
73
+
74
+ #### `on(eventName, callback)`
75
+
76
+ 监听事件。
77
+
78
+ - **事件名**: `'update'`
79
+ - **回调参数**: 无
80
+
81
+ #### `emit(eventName, data?)`
82
+
83
+ 手动触发事件(通常不需要手动调用)。
@@ -0,0 +1,12 @@
1
+ import { VersionPollingOptions } from './types';
2
+ import { default as VersionPolling } from './version-polling';
3
+ /**
4
+ * 创建版本轮询实例
5
+ * @param options - 轮询配置项
6
+ * @param options.pollingInterval - 轮询间隔,单位为秒
7
+ * @param options.fileUrl - 目标文件地址(任意主页HTML 或 任意json文件)
8
+ * @returns 版本轮询实例
9
+ */
10
+ export declare function createVersionPolling(options: Partial<Omit<VersionPollingOptions, 'detectionMode'>>): VersionPolling;
11
+ export type { VersionPollingOptions };
12
+ export { VersionPolling };