vue2server7 7.0.101 → 7.0.103

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.
Files changed (3) hide show
  1. package/package.json +1 -1
  2. package/test/1112 +69 -0
  3. package/test/1312312 +164 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vue2server7",
3
- "version": "7.0.101",
3
+ "version": "7.0.103",
4
4
  "description": "",
5
5
  "scripts": {
6
6
  "dev": "nodemon --watch src --ext ts --exec \"ts-node src/app.ts\"",
package/test/1112 ADDED
@@ -0,0 +1,69 @@
1
+ // public/config-worker.js
2
+
3
+ let timer = null
4
+ const KEY = 'APP_CONFIG_VERSION'
5
+
6
+ function getVersion(text) {
7
+ const match = text.match(/VERSION:\s*['"](.+?)['"]/)
8
+ return match?.[1]
9
+ }
10
+
11
+ async function checkConfig() {
12
+ const res = await fetch(`/config.js?t=${Date.now()}`, {
13
+ cache: 'no-store'
14
+ })
15
+
16
+ const text = await res.text()
17
+ const latestVersion = getVersion(text)
18
+
19
+ if (!latestVersion) return
20
+
21
+ postMessage({
22
+ type: 'CONFIG_VERSION',
23
+ version: latestVersion
24
+ })
25
+ }
26
+
27
+ self.onmessage = (event) => {
28
+ if (event.data?.type === 'START') {
29
+ if (timer) return
30
+
31
+ checkConfig()
32
+
33
+ timer = setInterval(() => {
34
+ checkConfig()
35
+ }, event.data.interval || 10000)
36
+ }
37
+ }
38
+
39
+
40
+ // src/utils/configWatcher.js
41
+
42
+ const KEY = 'APP_CONFIG_VERSION'
43
+
44
+ export function startConfigWatcher() {
45
+ const worker = new Worker('/config-worker.js')
46
+
47
+ worker.postMessage({
48
+ type: 'START',
49
+ interval: 10000
50
+ })
51
+
52
+ worker.onmessage = (event) => {
53
+ const { type, version } = event.data || {}
54
+
55
+ if (type !== 'CONFIG_VERSION' || !version) return
56
+
57
+ const localVersion = localStorage.getItem(KEY)
58
+
59
+ if (!localVersion) {
60
+ localStorage.setItem(KEY, version)
61
+ return
62
+ }
63
+
64
+ if (version !== localVersion) {
65
+ localStorage.setItem(KEY, version)
66
+ window.location.reload()
67
+ }
68
+ }
69
+ }
package/test/1312312 ADDED
@@ -0,0 +1,164 @@
1
+ type WorkerMessage =
2
+ | {
3
+ type: 'START'
4
+ interval?: number
5
+ }
6
+ | {
7
+ type: 'CHECK_NOW'
8
+ }
9
+ | {
10
+ type: 'STOP'
11
+ }
12
+
13
+ type WorkerResponse =
14
+ | {
15
+ type: 'CONFIG_VERSION'
16
+ version: string
17
+ }
18
+ | {
19
+ type: 'CONFIG_ERROR'
20
+ message: string
21
+ }
22
+
23
+ let timer: ReturnType<typeof setInterval> | null = null
24
+
25
+ function getVersion(text: string): string | null {
26
+ const match = text.match(/VERSION:\s*['"](.+?)['"]/)
27
+ return match?.[1] || null
28
+ }
29
+
30
+ async function checkConfig(): Promise<void> {
31
+ try {
32
+ const res = await fetch(`/config.js?t=${Date.now()}`, {
33
+ cache: 'no-store'
34
+ })
35
+
36
+ const text = await res.text()
37
+ const latestVersion = getVersion(text)
38
+
39
+ if (!latestVersion) return
40
+
41
+ const message: WorkerResponse = {
42
+ type: 'CONFIG_VERSION',
43
+ version: latestVersion
44
+ }
45
+
46
+ self.postMessage(message)
47
+ } catch (err) {
48
+ const message: WorkerResponse = {
49
+ type: 'CONFIG_ERROR',
50
+ message: err instanceof Error ? err.message : '配置检查失败'
51
+ }
52
+
53
+ self.postMessage(message)
54
+ }
55
+ }
56
+
57
+ self.onmessage = (event: MessageEvent<WorkerMessage>) => {
58
+ const data = event.data
59
+
60
+ if (data.type === 'START') {
61
+ if (timer) return
62
+
63
+ checkConfig()
64
+
65
+ timer = setInterval(() => {
66
+ checkConfig()
67
+ }, data.interval || 10000)
68
+ }
69
+
70
+ if (data.type === 'CHECK_NOW') {
71
+ checkConfig()
72
+ }
73
+
74
+ if (data.type === 'STOP') {
75
+ if (timer) {
76
+ clearInterval(timer)
77
+ timer = null
78
+ }
79
+ }
80
+ }
81
+
82
+ export {}
83
+
84
+
85
+
86
+ type WorkerResponse =
87
+ | {
88
+ type: 'CONFIG_VERSION'
89
+ version: string
90
+ }
91
+ | {
92
+ type: 'CONFIG_ERROR'
93
+ message: string
94
+ }
95
+
96
+ const CONFIG_VERSION_KEY = 'APP_CONFIG_VERSION'
97
+
98
+ let worker: Worker | null = null
99
+
100
+ function handleVersionChange(version: string): void {
101
+ const localVersion = localStorage.getItem(CONFIG_VERSION_KEY)
102
+
103
+ if (!localVersion) {
104
+ localStorage.setItem(CONFIG_VERSION_KEY, version)
105
+ return
106
+ }
107
+
108
+ if (version !== localVersion) {
109
+ localStorage.setItem(CONFIG_VERSION_KEY, version)
110
+ window.location.reload()
111
+ }
112
+ }
113
+
114
+ export function startConfigWatcher(interval = 10000): void {
115
+ if (worker) return
116
+
117
+ worker = new Worker(new URL('../workers/config.worker.ts', import.meta.url), {
118
+ type: 'module'
119
+ })
120
+
121
+ worker.postMessage({
122
+ type: 'START',
123
+ interval
124
+ })
125
+
126
+ worker.onmessage = (event: MessageEvent<WorkerResponse>) => {
127
+ const data = event.data
128
+
129
+ if (data.type === 'CONFIG_VERSION') {
130
+ handleVersionChange(data.version)
131
+ }
132
+
133
+ if (data.type === 'CONFIG_ERROR') {
134
+ console.error('配置版本检查失败:', data.message)
135
+ }
136
+ }
137
+
138
+ document.addEventListener('visibilitychange', () => {
139
+ if (!document.hidden) {
140
+ worker?.postMessage({
141
+ type: 'CHECK_NOW'
142
+ })
143
+ }
144
+ })
145
+
146
+ window.addEventListener('focus', () => {
147
+ worker?.postMessage({
148
+ type: 'CHECK_NOW'
149
+ })
150
+ })
151
+ }
152
+
153
+ export function stopConfigWatcher(): void {
154
+ if (!worker) return
155
+
156
+ worker.postMessage({
157
+ type: 'STOP'
158
+ })
159
+
160
+ worker.terminate()
161
+ worker = null
162
+ }
163
+
164
+