@wildberries/service-cdn-uploader
Advanced tools
Comparing version 3.0.2 to 3.0.3
78
index.js
@@ -49,9 +49,2 @@ /* eslint-disable import/no-unresolved */ | ||
const sleep = ms => | ||
new Promise(res => { | ||
setTimeout(() => { | ||
res(); | ||
}, ms); | ||
}); | ||
const timeoutSleep = ms => | ||
@@ -64,2 +57,36 @@ new Promise((res, rej) => { | ||
const fetchRetryer = async ({ request, params, counter = 0 }) => { | ||
try { | ||
await Promise.race([ | ||
request({ ...params, tryNumber: counter }), | ||
timeoutSleep(20000), | ||
]); | ||
} catch (error) { | ||
console.log( | ||
'=============== Request error ==============='.yellow.underline, | ||
{ | ||
counter: counter + 1, | ||
filePath: params.filePath, | ||
RequestId: params.requestId, | ||
error, | ||
}, | ||
); | ||
await fetchRetryer({ request, params, counter: counter + 1 }); | ||
} | ||
}; | ||
const downloadFile = async ({ filePath, tryNumber, requestId }) => { | ||
const downloadResponse = await fetch(filePath); | ||
if (downloadResponse.status !== 200) { | ||
throw new Error(`Status code – ${downloadResponse.status}`.red); | ||
} | ||
console.log( | ||
`File ${filePath} uploaded - try ${tryNumber}, Request_Id: ${requestId}, timestamp: ${new Date().toISOString()}` | ||
.green, | ||
); | ||
}; | ||
const fetchFile = async ({ | ||
@@ -102,10 +129,10 @@ renderGoUrl, | ||
await sleep(5000); | ||
await fetchRetryer({ | ||
request: downloadFile, | ||
params: { | ||
filePath, | ||
requestId, | ||
}, | ||
}); | ||
const downloadResponse = await fetch(filePath); | ||
if (downloadResponse.status !== 200) { | ||
throw new Error(`Status code – ${downloadResponse.status}`.red); | ||
} | ||
console.log( | ||
@@ -122,25 +149,2 @@ `File ${filePath} uploaded - try ${tryNumber}, Request_Id: ${requestId}, timestamp: ${new Date().toISOString()}` | ||
const fetchRetryer = async ({ request, params, counter = 0 }) => { | ||
try { | ||
await Promise.race([ | ||
request({ ...params, tryNumber: counter }), | ||
timeoutSleep(20000), | ||
]); | ||
} catch (error) { | ||
console.log( | ||
'=============== Request error ==============='.yellow.underline, | ||
{ | ||
counter: counter + 1, | ||
filePath: params.filePath, | ||
RequestId: params.requestId, | ||
error, | ||
}, | ||
); | ||
await sleep(5000); | ||
await fetchRetryer({ request, params, counter: counter + 1 }); | ||
} | ||
}; | ||
const pushStatic = async () => { | ||
@@ -147,0 +151,0 @@ try { |
{ | ||
"name": "@wildberries/service-cdn-uploader", | ||
"version": "3.0.2", | ||
"version": "3.0.3", | ||
"main": "./index.js", | ||
@@ -5,0 +5,0 @@ "scripts": { |
8034
162