@wildberries/service-cdn-uploader
Advanced tools
Comparing version 3.0.5 to 3.0.6
127
index.js
@@ -21,3 +21,3 @@ /* eslint-disable import/no-unresolved */ | ||
const renderGoAuth = process.env.CDN_API_TOKEN_BASE_64; | ||
const renderGoAuth = process.env.CDN_API_TOKEN; | ||
const CI_COMMIT_TAG = process.env.CI_COMMIT_TAG; | ||
@@ -27,3 +27,3 @@ const APP_STATIC_NAMESPACE = process.env.APP_STATIC_NAMESPACE; | ||
const SERVICE_URL = process.env.SERVICE_URL; | ||
const BUCKET = process.env.BUCKET || 'mstatic'; | ||
const BUCKET = process.env.BUCKET || 'basket-seller'; | ||
@@ -58,101 +58,57 @@ console.log('renderGoAuth'.yellow, renderGoAuth); | ||
const timeoutSleep = ms => | ||
new Promise((res, rej) => { | ||
setTimeout(() => { | ||
rej(new Error('Timeout')); | ||
}, ms); | ||
}); | ||
const fetchFile = async params => { | ||
const { staticPath, renderGoUrl, file, filePath, fileName } = params; | ||
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) { | ||
await sleep(5000); | ||
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 ({ | ||
renderGoUrl, | ||
file, | ||
filePath, | ||
requestId, | ||
tryNumber, | ||
fileFolder, | ||
}) => { | ||
try { | ||
console.log( | ||
`File ${filePath} start uploading: fileFolder ${fileFolder} try ${tryNumber}, Request_Id: ${requestId}, timestamp: ${new Date().toISOString()}` | ||
.yellow, | ||
); | ||
const formData = new FormData(); | ||
const task = { | ||
request_id: uuidv4(), | ||
expires: '2050-07-12T14:14:18Z', | ||
export: [{ target: BUCKET, key: staticPath }], | ||
}; | ||
formData.append('file', fs.createReadStream(file)); | ||
formData.append('task', JSON.stringify(task)); | ||
const uploadResponse = await fetch(`${renderGoUrl}/${fileFolder}`, { | ||
const uploadResponse = await fetch(renderGoUrl, { | ||
method: 'POST', | ||
body: formData, | ||
headers: { | ||
Authorization: `Basic ${renderGoAuth}`, | ||
Authorization: `bearer ${renderGoAuth}`, | ||
}, | ||
}).then(data => data.json()); | ||
}); | ||
if (uploadResponse.state !== 0) { | ||
console.log('Error upload response meta: ', uploadResponse); | ||
if (uploadResponse.status !== 200) { | ||
throw new Error(`Status code – ${uploadResponse.status}`.red); | ||
} | ||
// todo refactor | ||
// throw new Error(`Status code – ${uploadResponse.state}`.red); | ||
const downloadResponse = await fetch(filePath); | ||
return; | ||
if (downloadResponse.status !== 200) { | ||
throw new Error(`Status code – ${downloadResponse.status}`.red); | ||
} | ||
console.log( | ||
`File ${filePath} start downloading: try ${tryNumber}, Request_Id: ${requestId}, timestamp: ${new Date().toISOString()}` | ||
.yellow, | ||
console.log(`File ${fileName} uploaded to ${filePath}`.green); | ||
} catch (error) { | ||
console.error(error); | ||
throw new Error( | ||
`Error when uploading file ${fileName} ${WB_STATIC_URL}/${filePath}`, | ||
); | ||
} | ||
}; | ||
await fetchRetryer({ | ||
request: downloadFile, | ||
params: { | ||
filePath, | ||
requestId, | ||
}, | ||
const fetchRetryer = async ({ request, params, counter = 0 }) => { | ||
try { | ||
await request(params); | ||
} catch (error) { | ||
console.log(`Request error ${error}`.red.underline); | ||
console.log('=============== retry, ==============='.yellow.underline, { | ||
counter: counter + 1, | ||
filePath: params.filePath, | ||
}); | ||
console.log( | ||
`File ${filePath} uploaded - try ${tryNumber}, Request_Id: ${requestId}, timestamp: ${new Date().toISOString()}` | ||
.green, | ||
); | ||
} catch (error) { | ||
console.error(error); | ||
await sleep(1000); | ||
throw new Error(`Error when uploading file ${filePath}`); | ||
await fetchRetryer({ request, params, counter: counter + 1 }); | ||
} | ||
@@ -174,4 +130,2 @@ }; | ||
files.map(async file => { | ||
const requestId = uuidv4(); | ||
const staticPath = file.replace( | ||
@@ -182,5 +136,2 @@ new RegExp(`(${pathToStaticFolder}/umd)|(${pathToStaticFolder})`), | ||
const fileName = file.split('/').pop(); | ||
const fileFolder = staticPath.replace(`/${fileName}`, ''); | ||
await fetchRetryer({ | ||
@@ -191,5 +142,5 @@ request: fetchFile, | ||
renderGoUrl: SERVICE_URL, | ||
fileName: file.replace(`${pathToStaticFolder}`, ''), | ||
staticPath, | ||
filePath: `${WB_STATIC_URL}/${staticPath}`, | ||
requestId, | ||
fileFolder, | ||
}, | ||
@@ -196,0 +147,0 @@ }); |
{ | ||
"name": "@wildberries/service-cdn-uploader", | ||
"version": "3.0.5", | ||
"version": "3.0.6", | ||
"main": "./index.js", | ||
@@ -66,2 +66,2 @@ "scripts": { | ||
"bin": "index.js" | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
7126
130
3