oca_package
Version:
The wrapper of OCA bundle to generate OCA Package at ADC
100 lines (99 loc) • 3.81 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getOcaBundleFromDeps = exports.isOcaBundleWithDeps = exports.isSaid = exports.ocabundleDigest = exports.getDigest = exports.isPresent = exports.getCaptureBase = void 0;
// Get capture base from the OCA bundle
const getCaptureBase = (oca_bundle) => {
try {
if (!oca_bundle) {
throw new Error('OCA bundle is undefined or null.');
}
if (!oca_bundle.bundle && oca_bundle.capture_base) {
return oca_bundle.capture_base;
}
return oca_bundle.bundle.capture_base;
}
catch (error) {
console.error('Error in getting capture base:', error);
throw new Error(`Failed to get the capture base from the OCA bundle: ${error.message}`);
}
};
exports.getCaptureBase = getCaptureBase;
// Validate if the attribute exists in the OCA bundle capture base
const isPresent = (attribute, oca_bundle) => {
try {
const capture_base = (0, exports.getCaptureBase)(oca_bundle);
if (!capture_base.attributes) {
throw new Error('OCA bundle capture_base does not contain attributes.');
}
return attribute in capture_base.attributes;
}
catch (error) {
console.error('Error in validation:', error);
throw new Error(`Failed to check if the attribute is present in the capture base: ${error.message}`);
}
};
exports.isPresent = isPresent;
// Get the said (digest) from the OCA bundle capture base
const getDigest = (oca_bundle) => {
try {
const capture_base = (0, exports.getCaptureBase)(oca_bundle);
return capture_base.d;
}
catch (error) {
console.error('Error in getting said:', error);
throw new Error(`Failed to get the said from the capture base: ${error.message}`);
}
};
exports.getDigest = getDigest;
// Get oca bundle digest
const ocabundleDigest = (oca_bundle) => {
try {
const oca_bundle_d = oca_bundle.bundle?.d !== undefined ? oca_bundle.bundle.d : oca_bundle.d;
return oca_bundle_d;
}
catch (error) {
console.error('Error in getting oca bundle digest:', error);
throw new Error(`Failed to get the oca bundle digest: ${error.message}`);
}
};
exports.ocabundleDigest = ocabundleDigest;
const isSaid = (value) => {
return typeof value === 'string' && value.length == 44;
};
exports.isSaid = isSaid;
// Handling oca bundle with dependencies
const isOcaBundleWithDeps = (oca_bundle) => {
try {
if (!oca_bundle) {
throw new Error('OCA bundle is undefined or null.');
}
if (!oca_bundle.dependencies || oca_bundle.dependencies.length === 0) {
return false;
}
return true;
}
catch (error) {
console.error('Error in checking oca bundle with dependencies:', error);
throw new Error(`Failed to check if the OCA bundle has dependencies: ${error.message}`);
}
};
exports.isOcaBundleWithDeps = isOcaBundleWithDeps;
// Get corresponding oca bundle from oca bundle with dependencies
const getOcaBundleFromDeps = (oca_bundle, digest) => {
try {
if (!(0, exports.isOcaBundleWithDeps)(oca_bundle)) {
throw new Error('OCA bundle does not have dependencies.');
}
const dependencies = oca_bundle['dependencies'];
for (const dep in dependencies) {
if ((0, exports.isSaid)(digest) && dependencies[dep].d === digest) {
return dependencies[dep];
}
}
}
catch (error) {
console.error('Error in getting oca bundle from dependencies:', error);
throw new Error(`Failed to get the OCA bundle from dependencies: ${error.message}`);
}
};
exports.getOcaBundleFromDeps = getOcaBundleFromDeps;