Skip to content

Commit

Permalink
feat: add retry option
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaweiss committed Dec 8, 2024
1 parent f50b48e commit 69ae091
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 13 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,5 +86,6 @@ The plugin will upload files of `outDir` path after bundle.
| headers | Request headers setting, more information: https://www.alibabacloud.com/help/en/doc-detail/31978.html | object | {} |
| test | Only test path, no files upload | boolean | false |
| enabled | Enable the ali oss plugin | boolean | true |
| retry | Number of retries when upload fails | number | 0 |
| ... | Other init oss options, more information: https://www.alibabacloud.com/help/en/doc-detail/64097.html | any | |

1 change: 1 addition & 0 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,6 @@ pnpm run build
| headers | 请求头设置,详细信息请见 https://www.alibabacloud.com/help/zh/doc-detail/31978.html | object| {} |
| test | 仅测试路径,不会有文件上传 | boolean | false |
| enabled | 是否启用本插件 | boolean | true |
| retry | 上传失败时,重试次数 | number | 0 |
| ... | 其他初始化 oss 的参数,详细信息请见 https://www.alibabacloud.com/help/zh/doc-detail/64097.html | any | |
1 change: 1 addition & 0 deletions example/vite-6/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const options = {
// overwrite: false,
// enabled: false,
// test: true,
// retry: 3
}

const prod = process.env.NODE_ENV === 'production'
Expand Down
2 changes: 2 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ interface Options {
test?: boolean
/** Enable the ali oss plugin. Default true */
enabled?: boolean
/** Number of retries when upload (default 0) */
retry?: number
/** The temporary Security Token Service (STS) token used to access OSS. */
stsToken?: string
/** The endpoint that is used to access your OSS bucket. */
Expand Down
49 changes: 36 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,28 @@ import { URL } from 'node:url'

import { normalizePath } from 'vite'

const retry = async (fn, time) => {
while (true) {
try {
await fn()
break
} catch (error) {

if (time > 0) {
time -= 1
console.error(color.red(error))
await new Promise(resolve => setTimeout(resolve, 500))
console.log('')
console.log('')
console.log(`[vite-plugin-ali-oss] retry upload after 0.5s, ${time} times left`)
console.log('')
} else {
throw new Error(error)
}
}
}
}

export default function vitePluginAliOss (options) {
let baseConfig = '/'
let buildConfig = {}
Expand Down Expand Up @@ -41,6 +63,7 @@ export default function vitePluginAliOss (options) {
delete createOssOption.headers
delete createOssOption.test
delete createOssOption.enabled
delete createOssOption.retry

const client = new OSS(createOssOption)
const ssrClient = buildConfig.ssrManifest
Expand Down Expand Up @@ -85,22 +108,24 @@ export default function vitePluginAliOss (options) {
}

if (options.overwrite) {
await client.put(
ossFilePath,
fileFullPath,
{
headers: options.headers || {}
}
)
console.log(`upload complete: ${output}`)
await retry(async () => {
await client.put(
ossFilePath,
fileFullPath,
{
headers: options.headers || {}
}
)
console.log(`upload complete: ${output}`)
}, Number(options.retry || 0))

} else {
try {
await client.head(ossFilePath);
console.log(`${color.gray('files exists')}: ${output}`)

} catch (error) {
if (error.code === 'NoSuchKey') {
} catch (error) {
await retry(async () => {
await client.put(
ossFilePath,
fileFullPath,
Expand All @@ -109,9 +134,7 @@ export default function vitePluginAliOss (options) {
}
)
console.log(`upload complete: ${output}`)
} else {
throw new Error(error)
}
}, Number(options.retry || 0))
}
}
}
Expand Down

0 comments on commit 69ae091

Please sign in to comment.