UNPKG

oca_package

Version:

The wrapper of OCA bundle to generate OCA Package at ADC

177 lines (176 loc) 8.3 kB
"use strict"; // TODO: use canonicalize to consistently order how properties of overlays should appear // Do validation for the generation exentension chunck, and parts of oca_package. var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Extension = exports.Overlay = exports.ExtensionState = void 0; const ordering_js_1 = __importDefault(require("./state/overlays/ordering.js")); const unit_framing_js_1 = __importDefault(require("./state/overlays/framing/unit_framing.js")); const example_js_1 = __importDefault(require("./state/overlays/example.js")); const range_js_1 = __importDefault(require("./state/overlays/range.js")); const sensitive_js_1 = __importDefault(require("./state/overlays/sensitive.js")); const attribute_framing_js_1 = __importDefault(require("./state/overlays/framing/attribute_framing.js")); const helpers_js_1 = require("../utils/helpers.js"); const saidify_1 = require("saidify"); const ADC_COMMUNITY = 'adc'; const EXTENSION_VERSION = '1.0'; class ExtensionState { constructor(ExtensionInputJson) { this._extension_input_json = ExtensionInputJson; this._extensionState = this.BuildExtensionState(); } BuildExtensionState() { let state = {}; for (const community in this._extension_input_json.extensions) { const community_extensions = this._extension_input_json.extensions[community]; state[community] = {}; for (const bundle_digest in community_extensions) { state[community][bundle_digest] = community_extensions[bundle_digest]; } } return state; } // TODO: list the overlays get communities() { return this._extensionState; } } exports.ExtensionState = ExtensionState; class Overlay { constructor(community_overlay) { this._overlay = community_overlay; } GenerateOverlay() { const overlay = {}; for (const ov_type in this._overlay) { if (ov_type === 'ordering_overlay') { const ordering_instance = new ordering_js_1.default(this._overlay.ordering_overlay); const ordering_ov = ordering_instance.GenerateOverlay(); overlay['ordering'] = JSON.parse(ordering_ov); } else if (ov_type === 'unit_framing_overlay') { const unit_framing_instance = new unit_framing_js_1.default(this._overlay.unit_framing_overlay); const unit_framing_ov = unit_framing_instance.GenerateOverlay(); overlay['unit_framing'] = JSON.parse(unit_framing_ov); } else if (ov_type === 'range_overlay') { const range_instance = new range_js_1.default(this._overlay.range_overlay); const range_ov = range_instance.GenerateOverlay(); overlay['range'] = JSON.parse(range_ov); } else if (ov_type === 'example_overlay') { const example_ov = example_js_1.default.GenerateOverlay(this._overlay.example_overlay); overlay['example'] = JSON.parse(example_ov); } else if (ov_type === 'sensitive_overlay') { const sensitive_instance = new sensitive_js_1.default(this._overlay.sensitive_overlay); const sensitive_ov = sensitive_instance.GenerateOverlay(); overlay['sensitive'] = JSON.parse(sensitive_ov); } else if (ov_type === 'attribute_framing_overlay') { const attribute_framing_instance = new attribute_framing_js_1.default(this._overlay.attribute_framing_overlay); const attribute_framing_ov = attribute_framing_instance.GenerateOverlay(); overlay['attribute_framing'] = JSON.parse(attribute_framing_ov); } else { throw new Error(`Unsupported overaly type ${ov_type}. Supported extension overlays at ADC are [ ordering_overlay, unit_framing_overlay, range_overlay, example_overlay, sensitive_overlay ]`); } } const sortedOverlays = Object.keys(overlay) .sort() .reduce((acc, key) => { acc[key] = overlay[key]; return acc; }, {}); return sortedOverlays; } } exports.Overlay = Overlay; class ADCOverlayStrategy { GenerateOverlay(extensions) { const overlays = {}; for (const extKey in extensions) { const ext = extensions[extKey]; const overlay_instance = new Overlay(ext); const generated_overlay = overlay_instance.GenerateOverlay(); for (const overlay_type in generated_overlay) { overlays[overlay_type] = generated_overlay[overlay_type]; } } return overlays; } } // To implement overlays for external communities, create a new class that implements the OverlayStrategy interface. // For example, if you have a community called "external_community", you can create a class like this: class DefaultOverlayStrategy { GenerateOverlay(_extensions) { throw new Error('Unsupported community type'); } } class Extension { constructor(_extensions_input, community) { this.d = ''; this.overlays = {}; if (!_extensions_input || !community) { throw new Error('extension array is required from extension state and community is required'); } this._community = community; this._exensions = _extensions_input; this.type = `community/${this._community}/extension/${EXTENSION_VERSION}`; } GenerateOverlays() { const strategy = this._community === ADC_COMMUNITY ? new ADCOverlayStrategy() : new DefaultOverlayStrategy(); return strategy.GenerateOverlay(this._exensions); } toJSON() { return { d: '', type: `community/${this._community}/extension/1.0`, overlays: this.GenerateOverlays(), }; } Saidifying() { const [, sad] = (0, saidify_1.saidify)(this.toJSON()); return sad; } GenerateExtension() { return this.Saidifying(); } } exports.Extension = Extension; class ExtensionBox { constructor(extension_input_json, oca_bundle) { this._extensions_box = {}; this._oca_bundle = oca_bundle; this._extensionState = new ExtensionState(extension_input_json); } get buildExtensionsBox() { const extensionsBox = {}; const extensionState_communities = this._extensionState.communities; for (const community in extensionState_communities) { extensionsBox[community] = {}; const current_community = extensionState_communities[community]; for (const bundle_digest in current_community) { if (bundle_digest === (0, helpers_js_1.ocabundleDigest)(this._oca_bundle)) { const capture_base_digest = (0, helpers_js_1.getDigest)(this._oca_bundle); const community_extension_input = extensionState_communities[community][bundle_digest]; const extension = new Extension(community_extension_input, community); extensionsBox[community][capture_base_digest] = extension.GenerateExtension(); } else if (bundle_digest !== (0, helpers_js_1.ocabundleDigest)(this._oca_bundle)) { if ((0, helpers_js_1.isOcaBundleWithDeps)(this._oca_bundle)) { const current_bundle = (0, helpers_js_1.getOcaBundleFromDeps)(this._oca_bundle, bundle_digest); const capture_base_digest = (0, helpers_js_1.getDigest)(current_bundle); const community_extension_input = extensionState_communities[community][bundle_digest]; const extension = new Extension(community_extension_input, community); extensionsBox[community][capture_base_digest] = extension.GenerateExtension(); } } } } return extensionsBox; } } exports.default = ExtensionBox;