@yidun/cdn-upload-webpack-plugin 1.1.8 → 1.1.10
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/package.json +1 -1
- package/src/.claude/settings.local.json +7 -0
- package/src/vite.js +34 -7
package/package.json
CHANGED
package/src/vite.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
const fetch = require('node-fetch')
|
|
2
|
-
const { getFiles, cleanupFiles } = require('./utils')
|
|
2
|
+
const { getFiles, cleanupFiles, normalizeObjectKey } = require('./utils')
|
|
3
3
|
const { TaskDispatcher } = require('./task')
|
|
4
4
|
const NosUploadTask = require('./adaptor/nos')
|
|
5
5
|
|
|
@@ -95,17 +95,44 @@ function CdnUploadVitePlugin (options) {
|
|
|
95
95
|
}
|
|
96
96
|
const tasks = filesToUpload.map((file) => new NosUploadTask(client, file))
|
|
97
97
|
const uploadStart = Date.now()
|
|
98
|
-
console.log(
|
|
98
|
+
console.log('CdnUploadVitePlugin: assets uploading ...')
|
|
99
99
|
return new Promise((resolve, reject) => {
|
|
100
100
|
const taskDispatcher = new TaskDispatcher(tasks, {
|
|
101
|
-
onSuccess: () => {
|
|
102
|
-
|
|
103
|
-
const
|
|
104
|
-
|
|
105
|
-
|
|
101
|
+
onSuccess: async (tasks) => {
|
|
102
|
+
// 健康检查:优先 CDN,失败回退 NOS 源站
|
|
103
|
+
const firstTask = tasks?.[0]
|
|
104
|
+
if (!firstTask) {
|
|
105
|
+
console.log('CdnUploadVitePlugin: assets upload success')
|
|
106
|
+
if (cleanupAfterUpload) cleanupFiles(dirs, cleanupKeepRules)
|
|
107
|
+
resolve()
|
|
108
|
+
return
|
|
109
|
+
}
|
|
110
|
+
const { _file, _namespace, _nosClient } = firstTask
|
|
111
|
+
const cdnUrl = `${domain}/${_namespace}/${_file.relative}`
|
|
112
|
+
|
|
113
|
+
const cdnCheck = await fetch(cdnUrl)
|
|
114
|
+
if (cdnCheck.ok && cdnCheck.status === 200) {
|
|
115
|
+
console.log('CdnUploadVitePlugin: assets upload success')
|
|
116
|
+
if (cleanupAfterUpload) cleanupFiles(dirs, cleanupKeepRules)
|
|
117
|
+
resolve()
|
|
118
|
+
return
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// CDN 缓存未命中,回退检查 NOS 源站
|
|
122
|
+
console.warn(`CdnUploadVitePlugin: CDN returned ${cdnCheck.status}, falling back to NOS source check`)
|
|
123
|
+
try {
|
|
124
|
+
const objectKey = normalizeObjectKey(`${_namespace}/${_file.relative}`)
|
|
125
|
+
await _nosClient.headObject({ objectKey })
|
|
126
|
+
console.log('CdnUploadVitePlugin: NOS source check passed, assets upload success')
|
|
127
|
+
if (cleanupAfterUpload) cleanupFiles(dirs, cleanupKeepRules)
|
|
128
|
+
resolve()
|
|
129
|
+
} catch (nosError) {
|
|
130
|
+
reject(new Error(`CdnUploadVitePlugin Error: CDN url: ${cdnUrl}, status: ${cdnCheck.status}; NOS source check failed: ${nosError.message}`))
|
|
131
|
+
}
|
|
106
132
|
},
|
|
107
133
|
onError: (error) => reject(error)
|
|
108
134
|
})
|
|
135
|
+
console.log('CdnUploadVitePlugin: assets uploading ...')
|
|
109
136
|
taskDispatcher.dispatch(parallelCount)
|
|
110
137
|
})
|
|
111
138
|
}
|