Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 | 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 8x 8x 8x 8x 8x 8x 8x 8x 8x 1x 1x 8x 1x 1x 8x 8x 8x 8x 7x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 8x 8x 8x 8x 8x 7x 7x 1x 1x 1x 1x 1x 7x 5x 5x 3x 3x 3x 8x 8x 8x 8x 8x 8x 2x 1x 1x 1x 2x 6x 6x 6x 6x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 5x 5x 5x 5x 5x 5x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 5x 5x 3x 3x 2x | const child_process = require('child_process');
const path = require('path');
const fs = require('fs');
const ora = require('ora-classic');
const CONSTANTS = require('../constant');
const chalk = require('chalk');
const helper = require('../helper');
const replace = require('replace-in-file');
const { AppStaticService } = require('../../services/appstatic.service');
/* For dev, process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';*/
/**
* This function Publish the library to appstatic service
1) zip the build folder.
2) encode the .zip file to base64 and send it in request body.
3) post call to /v100/componentlib endpoint to publish the component
* @param {string} libraryName optional library name, else it will take value from package.json name
* @param {string} libraryVersion optional library version, else it will take value from package.json version
* @param {string} tokenParam optional B2S token for appstatic service, else it will take value from constant.js B2STOKEN
* @param {string} appStaticSVCUrl optional appstatic service url, else it will take value from constant.js APPSTATICURL
*/
const publishLib = async (libraryName, libraryVersion, tokenParam, appStaticSVCUrl) => {
const { buildFolderName, packageVersion } = helper.getBuildFolderName(libraryName, libraryVersion);
let componentRequestData;
const zipFolderPath = path.join(process.cwd(), buildFolderName).replace(/\\/g, '/');
const folderName = path.basename(zipFolderPath).replace(/\\/g, '/');
const srcToZip = path.join(folderName, packageVersion).replace(/\\/g, '/');
const token = helper.getB2SToken(tokenParam);
const appStaticServerUrl = helper.getAppStaticServerUrl(appStaticSVCUrl);
const spinner = ora(chalk.magenta(`Verifying the Cosmos versions in use.\n`));
/*
Step1: zip the file
*/
try{
/* Not needed now multiple version visible in build forlder, console.log(chalk.magenta('Running cleanup on build folder before zipping...'));
helper.cleanUpBuildFolder(parentFolder, buildFolderName, packageVersion); */
if(!token){
console.log(chalk.red('B2STOKEN, Auth token is empty, exiting publish operation.'));
process.exit(1);
}
if(!appStaticServerUrl){
console.log(chalk.red('App static service url not available, exiting publish operation.'));
process.exit(1);
}
spinner.color = 'magenta';
spinner.start();
try {
const npmListOutput = child_process.execSync('npm list --depth=0', { encoding: 'utf-8' });
if(npmListOutput && npmListOutput.indexOf(CONSTANTS.COSMOS_PKG_PATTERN) !== -1 && token){
const cosmosVersion = helper.getVersionFromListOutput(npmListOutput, CONSTANTS.COSMOS_PKG_PATTERN);
const pegaStaticURL = helper.getPayloadFromTkn(token, 'pega_staticurl');
Eif(pegaStaticURL){
const buildInfoResponse = await fetch(pegaStaticURL.endsWith('/')?pegaStaticURL+CONSTANTS.STATIC_BUILDINFO : pegaStaticURL+'/'+CONSTANTS.STATIC_BUILDINFO, {
method: 'GET',
agent: helper.getHttpsAgent()
});
Eif(buildInfoResponse.status === 200){
const buildInfoData = await buildInfoResponse.json();
const diffInCosmos = helper.compareObjects(cosmosVersion, buildInfoData[CONSTANTS.C11N_KEY_PEGA_DEP]);
Eif(diffInCosmos && !(Object.keys(diffInCosmos).length === 0 && diffInCosmos.constructor === Object)){
spinner.stop();
console.log(chalk.red('Error: Please ensure that your package version aligns with the targeted Constellation runtime portal.'));
console.log(chalk.yellow.bold('Prior to publishing, kindly address any version inconsistencies among the Cosmos packages referenced in your library. After resolving these issues, rebuild your project'));
console.log(diffInCosmos);
process.exit(1);
}
}
}
}
} catch (error) {
console.error(`Error executing npm list: ${error}`);
}
//console.log(chalk.magenta(`Zipping content from: ${path.join(process.cwd(), buildFolderName).replace(/\\/g, '/')}`));
spinner.text = chalk.magenta(`Zipping content from: ${path.join(process.cwd(), buildFolderName).replace(/\\/g, '/')}.`);
spinner.color = 'magenta';
spinner.stop();
spinner.start();
if(token){
const orgId = helper.getPayloadFromTkn(token, 'customer_org');
if(orgId && appStaticServerUrl){
let appStaticURL = appStaticServerUrl;
Eif(appStaticURL.endsWith('/'))
appStaticURL = appStaticServerUrl.substring(0, appStaticServerUrl.lastIndexOf('/'));
const options = {
files: srcToZip+'/**',
from: [/{ORG_ID}/g, /{APP_URL}/g],
to: [orgId, appStaticURL],
};
await replace(options);
}
}
const { status } = await helper.zipContent(folderName, srcToZip);
if(status === 'success'){
console.log('\n'+chalk.green('=== Zipped successfully ==='))
}
elseE{
spinner.stop();
return;
}
}
catch(err){
spinner.stop();
console.log(chalk.red(`=== Error occurred while zipping build folder. exiting publish === : ${err}`));
process.exit(1);
}
/*
Step2: encode to 'base64' string
*/
try{
//console.log(chalk.magenta(`encoding zip to base64 ....`));
spinner.text = chalk.magenta(`Encoding zip to base64.\n`);
spinner.color = 'magenta';
spinner.stop();
spinner.start();
componentRequestData = fs.readFileSync(`${zipFolderPath}.zip`, {encoding:'base64'});
if(!componentRequestData){
spinner.stop();
console.log(chalk.red(`zip file encoded to empty string... exiting publish`));
process.exit(1);
}
console.log(chalk.green('=== Encoded successfully ==='))
}
catch(err){
spinner.stop();
console.log(chalk.red("=== Error occurred while encoding zip to base64. exiting publish ==="));
console.error(err);
process.exit(1);
}
// Check for lib and version already exists in server.
const initialLibraryList = await AppStaticService.fetchLibraryNameOrVersions(buildFolderName, tokenParam, appStaticSVCUrl); // Assuming this is an async function
Iif(Array.isArray(initialLibraryList) && initialLibraryList.some(version => version === packageVersion)){
spinner.stop();
console.log(chalk.red(`Library already exists ${chalk.green.underline.bold(buildFolderName+'/'+packageVersion)} in server, Increment the library version, please build and publish new version of library.`));
process.exit(1);
}
//const spinner = ora(chalk.magenta(`Initiating call to appstatic for publishing Library`));
spinner.text = chalk.magenta(`Initiating call to appstatic for publishing Library.\n`);
spinner.color = 'magenta';
spinner.stop();
spinner.start();
/*
Preparing endpoint url, headers, requestbody
*/
//console.log(chalk.magenta(`initiating call to appstatic for publishing component`));
const URL = appStaticServerUrl && appStaticServerUrl.endsWith('/') ? appStaticServerUrl+CONSTANTS.ENDPOINTURL : appStaticServerUrl+'/'+CONSTANTS.ENDPOINTURL;
let status = 500; // defaulting to 500(internal server error)
const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
const maxRetries = 4;
let retries = 0;
const retryInterval = 60000; // 60 seconds in milliseconds
const headers = {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json'
};
const reqbody = {
zip: componentRequestData
}
try {
const response = await fetchWithTimeout (URL, {
method: 'POST',
agent: helper.getHttpsAgent(),
headers,
body: JSON.stringify(reqbody),
});
spinner.text = chalk.magenta(`We are processing your request. This may take a few minutes.\n`);
spinner.color = "magenta";
spinner.stop()
spinner.start();
status = response.status;
data = await response.text();
if (status === 401) {
spinner.stopAndPersist();
throw new Error('Error occurred in authentication. Please regenerate using authenticate');
} else if (status === 200 || status === 201) {
spinner.stopAndPersist();
console.log(chalk.green(`========= Publish Success ✅ : ${chalk.green.underline.bold(buildFolderName+'/'+packageVersion)} =========`));
}
else if(status === 504){
const timeout = setTimeout(() => {
spinner.text = chalk.yellow('Still working on your request. Thank you for your patience!\n');
spinner.color = 'yellow';
spinner.stop();
spinner.start();
}, retryInterval*1.5);
// polling to check lib and version availability
while (retries < maxRetries) {
retries += 1;
//console.log(chalk.yellow(`Gateway timeout, attempt ${retries}. Retrying in ${retryInterval/1000} seconds...`));
await delay(retryInterval);
const libraryList = await AppStaticService.fetchLibraryNameOrVersions(buildFolderName, tokenParam, appStaticSVCUrl); // Assuming this is an async function
if(Array.isArray(libraryList) && libraryList.some(version => version === packageVersion)){
clearTimeout(timeout);
spinner.stop();
console.log(chalk.green(`========= Publish Success ✅ : ${chalk.green.underline.bold(buildFolderName+'/'+packageVersion)} =========`));
break;
} else if(retries == maxRetries){
clearTimeout(timeout);
spinner.stop();
console.log(chalk.yellow(`The request has timed out. Please once verify the server's library availability using the list-library.`));
}
}
} else if(status === 400){
spinner.stopAndPersist();
console.log(chalk.redBright(`Bad Request: ${data}. \nIncrement the library version in package.json to publish new version of library.`));
process.exit(1);
} else {
spinner.stopAndPersist();
console.log(chalk.red(`Error status: ${status}`));
throw new Error(`${response}`);
}
} catch (e) {
spinner.stopAndPersist();
Iif (e && e.code === 'ECONNREFUSED') {
console.log(chalk.red("Error: Connection refused"));
process.exit(1);
} else Iif (status === 403) {
console.log(chalk.red("Error: 403 Forbidden"));
process.exit(1);
} else {
if(e){
console.log(chalk.red(e));
process.exit(1);
} elseE{
console.log(chalk.yellow(`Possible timeout, please check library in server via list-library.`));
process.exit(1);
}
}
}
}
async function fetchWithTimeout(url, options = {}, timeout = 300000) { // Set timeout to 2 minutes (120,000 milliseconds)
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), timeout);
try {
const response = await fetch(url, {
...options,
signal: controller.signal
});
clearTimeout(timeoutId);
return response;
} catch (error) {
Iif (error.name === 'AbortError') {
throw new Error(`Request timed out after ${timeout} ms`);
}
throw error;
}
}
module.exports = { publishLib } |