@wildberries/service-cdn-uploader
Advanced tools
Comparing version 0.1.0-beta.4 to 0.1.1-beta.0
186
index.js
@@ -0,6 +1,9 @@ | ||
/* eslint-disable import/no-unresolved */ | ||
/* eslint-disable import/no-extraneous-dependencies */ | ||
/* eslint-disable no-console */ | ||
const path = require('path'); | ||
const fs = require('fs'); | ||
const fetch = require('node-fetch'); | ||
const glob = require('glob'); | ||
const FormData = require('form-data'); | ||
const { PureRestRequest } = require('@mihanizm56/fetch-api'); | ||
@@ -21,8 +24,2 @@ const pathToStaticFolder = path.join(process.cwd(), process.argv[2]); | ||
// FOR DEBUGGING | ||
// const WB_STATIC_URL = 'https://mstatic.wbstatic.net'; | ||
// const SERVICE_URL = 'http://api.render-go.svc.k8s.datapro'; | ||
// const CI_COMMIT_TAG = 'v0.0.3'; | ||
// const APP_STATIC_NAMESPACE = 'suppliers-portal-business-card-frontend-test'; | ||
console.log('renderGoAuth', renderGoAuth); | ||
@@ -48,6 +45,15 @@ console.log('CI_COMMIT_TAG', CI_COMMIT_TAG); | ||
const fetchFile = async ({ formData, fileName, staticPath, endpoint, DC }) => { | ||
const fetchFile = async params => { | ||
const { staticPath, DC, renderGoUrl, file, filePath, fileName } = params; | ||
try { | ||
const { error, code } = await new PureRestRequest().postRequest({ | ||
endpoint, | ||
const formData = new FormData(); | ||
formData.append('action', 'file_upload'); | ||
formData.append('type', 'mstatic'); | ||
formData.append('path', staticPath); | ||
formData.append('file', fs.createReadStream(file)); | ||
formData.append('skip', 'true'); | ||
const uploadResponse = await fetch(renderGoUrl, { | ||
method: 'POST', | ||
body: formData, | ||
@@ -57,66 +63,71 @@ headers: { | ||
}, | ||
parseType: 'text', | ||
retry: 3, | ||
}); | ||
if (!error) { | ||
console.log( | ||
`File ${DC} ${fileName} uploaded to ${WB_STATIC_URL}/${staticPath}`, | ||
); | ||
if (uploadResponse.status !== 200) { | ||
throw new Error(`Status code – ${uploadResponse.status}`); | ||
} | ||
return; | ||
const downloadResponse = await fetch(filePath); | ||
if (downloadResponse.status !== 200) { | ||
throw new Error(`Status code – ${downloadResponse.status}`); | ||
} | ||
console.error(`Error status code – ${code}`); | ||
process.exit(1); | ||
console.log(`File ${DC} ${fileName} uploaded to ${filePath}`); | ||
} catch (error) { | ||
console.error( | ||
console.error(error); | ||
throw new Error( | ||
`Error when uploading file ${fileName} ${WB_STATIC_URL}/${staticPath}`, | ||
); | ||
console.error(error); | ||
process.exit(1); | ||
} | ||
}; | ||
const fetchVerifyFile = async ({ endpoint, requestParams }) => { | ||
const fetchRetryer = async ({ request, params, counter = 0 }) => { | ||
try { | ||
const { error, code } = await new PureRestRequest().getRequest({ | ||
endpoint, | ||
parseType: 'text', | ||
retry: 3, | ||
}); | ||
await request(params); | ||
} catch (error) { | ||
console.log('Request error', error); | ||
console.log(`=============== retry counter ${counter + 1} ===============`); | ||
if (!error) { | ||
console.log(`File ${endpoint} is available`); | ||
await fetchRetryer({ request, params, counter: counter + 1 }); | ||
} | ||
}; | ||
return; | ||
} | ||
const pushStatic = async () => { | ||
try { | ||
await glob( | ||
`${pathToStaticFolder}/**/*`, | ||
{ nodir: true }, | ||
async (globError, files) => { | ||
if (globError) { | ||
console.error(globError); | ||
process.exit(1); | ||
} | ||
console.log( | ||
`Error status code – ${code}, DON'T WORRY BRO, I WILL TRY another way`, | ||
); | ||
await Promise.all( | ||
files.map(async file => { | ||
const staticPath = file.replace( | ||
new RegExp(`(${pathToStaticFolder}/umd)|(${pathToStaticFolder})`), | ||
newStaticPath, | ||
); | ||
await fetchFile(requestParams); | ||
await fetchRetryer({ | ||
request: fetchFile, | ||
params: { | ||
file, | ||
staticPath, | ||
DC: 'DP', | ||
renderGoUrl: SERVICE_URL, | ||
fileName: file.replace(`${pathToStaticFolder}`, ''), | ||
filePath: `${WB_STATIC_URL}/${staticPath}`, | ||
}, | ||
}); | ||
}), | ||
); | ||
const { | ||
error: retryError, | ||
code: retryCode, | ||
} = await new PureRestRequest().getRequest({ | ||
endpoint, | ||
parseType: 'text', | ||
retry: 3, | ||
}); | ||
console.log('check'); | ||
if (!retryError) { | ||
console.log(`File ${endpoint} is available`); | ||
return; | ||
} | ||
console.error(`FINALLY Error status code – ${retryCode}`); | ||
process.exit(1); | ||
console.log('UPLOADED SUCCESSFULLY'); | ||
process.exit(0); | ||
}, | ||
); | ||
} catch (error) { | ||
console.error(`FINALLY Error when checking file ${endpoint}`); | ||
console.error(error); | ||
@@ -127,65 +138,2 @@ process.exit(1); | ||
const pushStatic = async () => { | ||
await glob( | ||
`${pathToStaticFolder}/**/*`, | ||
{ nodir: true }, | ||
async (globError, files) => { | ||
if (globError) { | ||
console.error(globError); | ||
process.exit(1); | ||
} | ||
const verifyFiles = []; | ||
await Promise.all( | ||
files.map(async file => { | ||
const staticPath = file.replace( | ||
new RegExp(`(${pathToStaticFolder}/umd)|(${pathToStaticFolder})`), | ||
newStaticPath, | ||
); | ||
const fileName = file.replace(`${pathToStaticFolder}`, ''); | ||
const formData = new FormData(); | ||
formData.append('action', 'file_upload'); | ||
formData.append('type', 'mstatic'); | ||
formData.append('path', staticPath); | ||
formData.append('file', fs.createReadStream(file)); | ||
formData.append('skip', 'true'); | ||
await fetchFile({ | ||
formData, | ||
fileName, | ||
staticPath, | ||
DC: 'DP', | ||
endpoint: SERVICE_URL, | ||
}); | ||
verifyFiles.push({ | ||
endpoint: `${WB_STATIC_URL}/${staticPath}`, | ||
requestParams: { | ||
formData, | ||
fileName, | ||
staticPath, | ||
DC: 'DP', | ||
endpoint: SERVICE_URL, | ||
}, | ||
}); | ||
}), | ||
); | ||
console.log(' '); | ||
console.log('--- START CHECKING FILES ---'); | ||
console.log(' '); | ||
await Promise.all( | ||
verifyFiles.map(params => { | ||
return fetchVerifyFile(params); | ||
}), | ||
); | ||
process.exit(0); | ||
}, | ||
); | ||
}; | ||
pushStatic(); |
{ | ||
"name": "@wildberries/service-cdn-uploader", | ||
"version": "0.1.0-beta.4", | ||
"version": "0.1.1-beta.0", | ||
"main": "./index.js", | ||
@@ -19,4 +19,3 @@ "scripts": { | ||
"node-fetch": "2.6.1", | ||
"util": "0.12.2", | ||
"@mihanizm56/fetch-api": "3.8.12-beta.1" | ||
"util": "0.12.2" | ||
}, | ||
@@ -66,2 +65,2 @@ "devDependencies": { | ||
"bin": "index.js" | ||
} | ||
} |
Sorry, the diff of this file is too big to display
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
5
188078
4967
2
- Removed@mihanizm56/fetch-api@3.8.12-beta.1
- Removed@mihanizm56/fetch-api@3.8.12-beta.1(transitive)
- Removedcolors@1.4.0(transitive)
- Removeddecode-uri-component@0.2.2(transitive)
- Removedfilter-obj@1.1.0(transitive)
- Removedjson-mask@1.0.4(transitive)
- Removedquery-string@7.0.1(transitive)
- Removedsplit-on-first@1.1.0(transitive)
- Removedstrict-uri-encode@2.0.0(transitive)