@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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yidun/cdn-upload-webpack-plugin",
3
- "version": "1.1.8",
3
+ "version": "1.1.10",
4
4
  "description": "A webpack plugin for upload dist to cdn.",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -0,0 +1,7 @@
1
+ {
2
+ "permissions": {
3
+ "allow": [
4
+ "Bash(node test/index.js)"
5
+ ]
6
+ }
7
+ }
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(`CdnUploadVitePlugin: uploading ${filesToUpload.length} files ...`)
98
+ console.log('CdnUploadVitePlugin: assets uploading ...')
99
99
  return new Promise((resolve, reject) => {
100
100
  const taskDispatcher = new TaskDispatcher(tasks, {
101
- onSuccess: () => {
102
- if (cleanupAfterUpload) cleanupFiles(dirs, cleanupKeepRules)
103
- const buildCost = buildStartAt > 0 ? `, build+upload=${Date.now() - buildStartAt}ms` : ''
104
- console.log(`CdnUploadVitePlugin: done — upload=${Date.now() - uploadStart}ms, total=${Date.now() - uploadStageStart}ms${buildCost}${cleanupAfterUpload ? ' [cleaned]' : ''}`)
105
- resolve()
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
  }