@open-pioneer/runtime 2.4.0-dev.20241120092632 → 3.0.0-dev.20250221111321

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- {"version":3,"file":"ServiceLookup.js","sources":["ServiceLookup.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2023 Open Pioneer project (https://github.com/open-pioneer)\n// SPDX-License-Identifier: Apache-2.0\nimport { Error } from \"@open-pioneer/core\";\nimport { ErrorId } from \"../errors\";\nimport { InterfaceSpec, isSingleImplementationSpec, ReferenceSpec } from \"./InterfaceSpec\";\nimport { ServiceRepr } from \"./ServiceRepr\";\n\ninterface Services {\n // The unqualified service implementations (if any).\n unqualified: ServiceRepr[];\n\n // Services indexed by their qualifier (if any).\n byQualifier: Map<string, ServiceRepr>;\n}\n\nexport type ReadonlyServiceLookup = Pick<\n ServiceLookup,\n \"lookup\" | \"lookupOne\" | \"lookupAll\" | \"serviceCount\"\n>;\n\nexport interface Unimplemented {\n type: \"unimplemented\";\n}\n\nexport interface Ambiguous {\n type: \"ambiguous\";\n choices: AmbiguousChoice[];\n}\n\nexport type AmbiguousChoice = [serviceId: string, qualifier: string | undefined];\n\nexport interface Found<T> {\n type: \"found\";\n value: T;\n}\n\nexport type ServiceLookupResult = Unimplemented | Ambiguous | Found<ServiceRepr>;\n\nexport type ServicesLookupResult = Found<ServiceRepr[]>;\n\nexport class ServiceLookup {\n // Service implementations indexed by interface name.\n private services = new Map<string, Services>();\n\n // Total service count\n private _count = 0;\n\n /**\n * Returns the total number of registered services.\n */\n get serviceCount() {\n return this._count;\n }\n\n /**\n * Attempts to register the given service as an implementation of `interfaceName`.\n * Throws if the registration did not complete successfully.\n *\n * All services should be registered before any lookups are attempted.\n */\n register(service: ServiceRepr, { interfaceName, qualifier }: InterfaceSpec): void {\n if (!interfaceName) {\n throw new Error(\n ErrorId.INVALID_METADATA,\n `Service '${service.id}' provides invalid interface '${interfaceName}'.`\n );\n }\n if (qualifier != null && !qualifier) {\n throw new Error(\n ErrorId.INVALID_METADATA,\n `Service '${service.id}' uses an invalid qualifier for interface '${interfaceName}': '${qualifier}'.`\n );\n }\n\n const services = this.ensureInterfaceEntry(interfaceName);\n if (!qualifier) {\n services.unqualified.push(service);\n ++this._count;\n return;\n }\n\n const existingQualified = services.byQualifier.get(qualifier);\n if (existingQualified) {\n throw new Error(\n ErrorId.DUPLICATE_INTERFACE,\n `Cannot register '${service.id}' as interface '${interfaceName}' with qualifier '${qualifier}'.` +\n ` The interface is already provided by service '${existingQualified.id}'.` +\n ` You can choose a different qualifier or remove the service to resolve the collision.`\n );\n }\n services.byQualifier.set(qualifier, service);\n ++this._count;\n }\n\n /**\n * Attempts to find the service(s) required by the given reference spec.\n */\n lookup(ref: ReferenceSpec): ServiceLookupResult | ServicesLookupResult {\n if (isSingleImplementationSpec(ref)) {\n return this.lookupOne(ref);\n } else {\n return this.lookupAll(ref.interfaceName);\n }\n }\n\n /**\n * Attempts to find a service implementing the given interface.\n */\n lookupOne({ interfaceName, qualifier }: InterfaceSpec): ServiceLookupResult {\n if (!interfaceName) {\n throw new Error(\n ErrorId.INVALID_METADATA,\n `Invalid interface name during service lookup: '${interfaceName}'.`\n );\n }\n if (qualifier != null && !qualifier) {\n throw new Error(\n ErrorId.INVALID_METADATA,\n `Invalid qualifier during service lookup of interface '${interfaceName}': '${qualifier}'.`\n );\n }\n\n const services = this.services.get(interfaceName);\n if (!services) {\n return { type: \"unimplemented\" };\n }\n\n if (!qualifier) {\n // Just pick one, but the choice must be unambiguous.\n const count = services.unqualified.length + services.byQualifier.size;\n if (count > 1) {\n // [serviceId, qualifier] tuples\n const choices: AmbiguousChoice[] = [];\n choices.push(\n ...Array.from(services.byQualifier.entries()).map<AmbiguousChoice>(\n ([qualifier, service]) => [service.id, qualifier]\n )\n );\n choices.push(\n ...services.unqualified.map<AmbiguousChoice>((s) => [s.id, undefined])\n );\n return {\n type: \"ambiguous\",\n choices: choices\n };\n }\n\n const service = services.unqualified[0] ?? first(services.byQualifier);\n if (!service) {\n return { type: \"unimplemented\" };\n }\n return { type: \"found\", value: service };\n }\n\n const service = services.byQualifier.get(qualifier);\n if (!service) {\n return { type: \"unimplemented\" };\n }\n return { type: \"found\", value: service };\n }\n\n /**\n * Returns all services implementing the given interface.\n */\n lookupAll(interfaceName: string): ServicesLookupResult {\n if (!interfaceName) {\n throw new Error(\n ErrorId.INVALID_METADATA,\n `Invalid interface name during service lookup: '${interfaceName}'.`\n );\n }\n\n const services = this.services.get(interfaceName);\n if (!services) {\n return { type: \"found\", value: [] };\n }\n\n const deduped = new Set([...services.unqualified, ...services.byQualifier.values()]);\n return {\n type: \"found\",\n value: Array.from(deduped)\n };\n }\n\n private ensureInterfaceEntry(interfaceName: string): Services {\n const services = this.services;\n let entry = services.get(interfaceName);\n if (!entry) {\n entry = {\n unqualified: [],\n byQualifier: new Map()\n };\n services.set(interfaceName, entry);\n }\n return entry;\n }\n}\n\nexport function renderAmbiguousServiceChoices(choices: AmbiguousChoice[], max = 3): string {\n let message = \"\";\n let count = 0;\n for (const [serviceId, qualifier] of choices) {\n if (count) {\n message += \", \";\n }\n\n message += `'${serviceId}'`;\n if (qualifier) {\n message += ` (with qualifier '${qualifier}')`;\n }\n\n if (++count >= max) {\n break;\n }\n }\n\n const remaining = choices.length - count;\n if (remaining > 0) {\n message += ` and ${remaining} more`;\n }\n return message;\n}\n\nfunction first<K, V>(map: Map<K, V>): V | undefined {\n return map.values().next().value;\n}\n"],"names":["qualifier","service"],"mappings":";;;;AAwCO,MAAM,aAAc,CAAA;AAAA;AAAA,EAEf,QAAA,uBAAe,GAAsB,EAAA,CAAA;AAAA;AAAA,EAGrC,MAAS,GAAA,CAAA,CAAA;AAAA;AAAA;AAAA;AAAA,EAKjB,IAAI,YAAe,GAAA;AACf,IAAA,OAAO,IAAK,CAAA,MAAA,CAAA;AAAA,GAChB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,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IAAI,SAAS,CAAA,CAAA,CAAA,CAAA;AACxB,IAAA,IAAI,SAAW,EAAA;AACX,MAAA,OAAA,IAAW,qBAAqB,SAAS,CAAA,EAAA,CAAA,CAAA;AAAA,KAC7C;AAEA,IAAI,IAAA,EAAE,SAAS,GAAK,EAAA;AAChB,MAAA,MAAA;AAAA,KACJ;AAAA,GACJ;AAEA,EAAM,MAAA,SAAA,GAAY,QAAQ,MAAS,GAAA,KAAA,CAAA;AACnC,EAAA,IAAI,YAAY,CAAG,EAAA;AACf,IAAA,OAAA,IAAW,QAAQ,SAAS,CAAA,KAAA,CAAA,CAAA;AAAA,GAChC;AACA,EAAO,OAAA,OAAA,CAAA;AACX,CAAA;AAEA,SAAS,MAAY,GAA+B,EAAA;AAChD,EAAA,OAAO,GAAI,CAAA,MAAA,EAAS,CAAA,IAAA,EAAO,CAAA,KAAA,CAAA;AAC/B;;;;"}
1
+ {"version":3,"file":"ServiceLookup.js","sources":["ServiceLookup.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2023-2025 Open Pioneer project (https://github.com/open-pioneer)\n// SPDX-License-Identifier: Apache-2.0\nimport { Error } from \"@open-pioneer/core\";\nimport { ErrorId } from \"../errors\";\nimport { InterfaceSpec, isSingleImplementationSpec, ReferenceSpec } from \"./InterfaceSpec\";\nimport { ServiceRepr } from \"./ServiceRepr\";\n\ninterface Services {\n // The unqualified service implementations (if any).\n unqualified: ServiceRepr[];\n\n // Services indexed by their qualifier (if any).\n byQualifier: Map<string, ServiceRepr>;\n}\n\nexport type ReadonlyServiceLookup = Pick<\n ServiceLookup,\n \"lookup\" | \"lookupOne\" | \"lookupAll\" | \"serviceCount\"\n>;\n\nexport interface Unimplemented {\n type: \"unimplemented\";\n}\n\nexport interface Ambiguous {\n type: \"ambiguous\";\n choices: AmbiguousChoice[];\n}\n\nexport type AmbiguousChoice = [serviceId: string, qualifier: string | undefined];\n\nexport interface Found<T> {\n type: \"found\";\n value: T;\n}\n\nexport type ServiceLookupResult = Unimplemented | Ambiguous | Found<ServiceRepr>;\n\nexport type ServicesLookupResult = Found<ServiceRepr[]>;\n\nexport class ServiceLookup {\n // Service implementations indexed by interface name.\n private services = new Map<string, Services>();\n\n // Total service count\n private _count = 0;\n\n /**\n * Returns the total number of registered services.\n */\n get serviceCount() {\n return this._count;\n }\n\n /**\n * Attempts to register the given service as an implementation of `interfaceName`.\n * Throws if the registration did not complete successfully.\n *\n * All services should be registered before any lookups are attempted.\n */\n register(service: ServiceRepr, { interfaceName, qualifier }: InterfaceSpec): void {\n if (!interfaceName) {\n throw new Error(\n ErrorId.INVALID_METADATA,\n `Service '${service.id}' provides invalid interface '${interfaceName}'.`\n );\n }\n if (qualifier != null && !qualifier) {\n throw new Error(\n ErrorId.INVALID_METADATA,\n `Service '${service.id}' uses an invalid qualifier for interface '${interfaceName}': '${qualifier}'.`\n );\n }\n\n const services = this.ensureInterfaceEntry(interfaceName);\n if (!qualifier) {\n services.unqualified.push(service);\n ++this._count;\n return;\n }\n\n const existingQualified = services.byQualifier.get(qualifier);\n if (existingQualified) {\n throw new Error(\n ErrorId.DUPLICATE_INTERFACE,\n `Cannot register '${service.id}' as interface '${interfaceName}' with qualifier '${qualifier}'.` +\n ` The interface is already provided by service '${existingQualified.id}'.` +\n ` You can choose a different qualifier or remove the service to resolve the collision.`\n );\n }\n services.byQualifier.set(qualifier, service);\n ++this._count;\n }\n\n /**\n * Attempts to find the service(s) required by the given reference spec.\n */\n lookup(ref: ReferenceSpec): ServiceLookupResult | ServicesLookupResult {\n if (isSingleImplementationSpec(ref)) {\n return this.lookupOne(ref);\n } else {\n return this.lookupAll(ref.interfaceName);\n }\n }\n\n /**\n * Attempts to find a service implementing the given interface.\n */\n lookupOne({ interfaceName, qualifier }: InterfaceSpec): ServiceLookupResult {\n if (!interfaceName) {\n throw new Error(\n ErrorId.INVALID_METADATA,\n `Invalid interface name during service lookup: '${interfaceName}'.`\n );\n }\n if (qualifier != null && !qualifier) {\n throw new Error(\n ErrorId.INVALID_METADATA,\n `Invalid qualifier during service lookup of interface '${interfaceName}': '${qualifier}'.`\n );\n }\n\n const services = this.services.get(interfaceName);\n if (!services) {\n return { type: \"unimplemented\" };\n }\n\n if (!qualifier) {\n // Just pick one, but the choice must be unambiguous.\n const count = services.unqualified.length + services.byQualifier.size;\n if (count > 1) {\n // [serviceId, qualifier] tuples\n const choices: AmbiguousChoice[] = [];\n choices.push(\n ...Array.from(services.byQualifier.entries()).map<AmbiguousChoice>(\n ([qualifier, service]) => [service.id, qualifier]\n )\n );\n choices.push(\n ...services.unqualified.map<AmbiguousChoice>((s) => [s.id, undefined])\n );\n return {\n type: \"ambiguous\",\n choices: choices\n };\n }\n\n const service = services.unqualified[0] ?? first(services.byQualifier);\n if (!service) {\n return { type: \"unimplemented\" };\n }\n return { type: \"found\", value: service };\n }\n\n const service = services.byQualifier.get(qualifier);\n if (!service) {\n return { type: \"unimplemented\" };\n }\n return { type: \"found\", value: service };\n }\n\n /**\n * Returns all services implementing the given interface.\n */\n lookupAll(interfaceName: string): ServicesLookupResult {\n if (!interfaceName) {\n throw new Error(\n ErrorId.INVALID_METADATA,\n `Invalid interface name during service lookup: '${interfaceName}'.`\n );\n }\n\n const services = this.services.get(interfaceName);\n if (!services) {\n return { type: \"found\", value: [] };\n }\n\n const deduped = new Set([...services.unqualified, ...services.byQualifier.values()]);\n return {\n type: \"found\",\n value: Array.from(deduped)\n };\n }\n\n private ensureInterfaceEntry(interfaceName: string): Services {\n const services = this.services;\n let entry = services.get(interfaceName);\n if (!entry) {\n entry = {\n unqualified: [],\n byQualifier: new Map()\n };\n services.set(interfaceName, entry);\n }\n return entry;\n }\n}\n\nexport function renderAmbiguousServiceChoices(choices: AmbiguousChoice[], max = 3): string {\n let message = \"\";\n let count = 0;\n for (const [serviceId, qualifier] of choices) {\n if (count) {\n message += \", \";\n }\n\n message += `'${serviceId}'`;\n if (qualifier) {\n message += ` (with qualifier '${qualifier}')`;\n }\n\n if (++count >= max) {\n break;\n }\n }\n\n const remaining = choices.length - count;\n if (remaining > 0) {\n message += ` and ${remaining} more`;\n }\n return message;\n}\n\nfunction first<K, V>(map: Map<K, V>): V | undefined {\n return map.values().next().value;\n}\n"],"names":["qualifier","service"],"mappings":";;;;AAwCO,MAAM,aAAc,CAAA;AAAA;AAAA,EAEf,QAAA,uBAAe,GAAsB,EAAA;AAAA;AAAA,EAGrC,MAAS,GAAA,CAAA;AAAA;AAAA;AAAA;AAAA,EAKjB,IAAI,YAAe,GAAA;AACf,IAAA,OAAO,IAAK,CAAA,MAAA;AAAA;AAChB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,QAAS,CAAA,OAAA,EAAsB,EAAE,aAAA,EAAe,WAAkC,EAAA;AAC9E,IAAA,IAAI,CAAC,aAAe,EAAA;AAChB,MAAA,MAAM,IAAI,KAAA;AAAA,QACN,OAAQ,CAAA,gBAAA;AAAA,QACR,CAAY,SAAA,EAAA,OAAA,CAAQ,EAAE,CAAA,8BAAA,EAAiC,aAAa,CAAA,EAAA;AAAA,OACxE;AAAA;AAEJ,IAAI,IAAA,SAAA,IAAa,IAAQ,IAAA,CAAC,SAAW,EAAA;AACjC,MAAA,MAAM,IAAI,KAAA;AAAA,QACN,OAAQ,CAAA,gBAAA;AAAA,QACR,YAAY,OAAQ,CAAA,EAAE,CAA8C,2CAAA,EAAA,aAAa,OAAO,SAAS,CAAA,EAAA;AAAA,OACrG;AAAA;AAGJ,IAAM,MAAA,QAAA,GAAW,IAAK,CAAA,oBAAA,CAAqB,aAAa,CAAA;AACxD,IAAA,IAAI,CAAC,SAAW,EAAA;AACZ,MAAS,QAAA,CAAA,WAAA,CAAY,KAAK,OAAO,CAAA;AACjC,MAAA,EAAE,IAAK,CAAA,MAAA;AACP,MAAA;AAAA;AAGJ,IAAA,MAAM,iBAAoB,GAAA,QAAA,CAAS,WAAY,CAAA,GAAA,CAAI,SAAS,CAAA;AAC5D,IAAA,IAAI,iBAAmB,EAAA;AACnB,MAAA,MAAM,IAAI,KA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AA,IAAI,SAAW,EAAA;AACX,MAAA,OAAA,IAAW,qBAAqB,SAAS,CAAA,EAAA,CAAA;AAAA;AAG7C,IAAI,IAAA,EAAE,SAAS,GAAK,EAAA;AAChB,MAAA;AAAA;AACJ;AAGJ,EAAM,MAAA,SAAA,GAAY,QAAQ,MAAS,GAAA,KAAA;AACnC,EAAA,IAAI,YAAY,CAAG,EAAA;AACf,IAAA,OAAA,IAAW,QAAQ,SAAS,CAAA,KAAA,CAAA;AAAA;AAEhC,EAAO,OAAA,OAAA;AACX;AAEA,SAAS,MAAY,GAA+B,EAAA;AAChD,EAAA,OAAO,GAAI,CAAA,MAAA,EAAS,CAAA,IAAA,EAAO,CAAA,KAAA;AAC/B;;;;"}
@@ -1 +1 @@
1
- {"version":3,"file":"ServiceRepr.js","sources":["ServiceRepr.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2023 Open Pioneer project (https://github.com/open-pioneer)\n// SPDX-License-Identifier: Apache-2.0\nimport { ErrorId } from \"../errors\";\nimport { ServiceMetadata } from \"../metadata\";\nimport { Service, ServiceConstructor, ServiceOptions } from \"../Service\";\nimport { Error } from \"@open-pioneer/core\";\nimport { InterfaceSpec, parseReferenceSpec, ReferenceSpec } from \"./InterfaceSpec\";\nimport { PackageIntl } from \"../i18n\";\n\nexport type ServiceState = \"not-constructed\" | \"constructing\" | \"constructed\" | \"destroyed\";\n\nexport type ServiceDependency = ReferenceSpec & { referenceName: string };\n\nexport interface ConstructorFactory {\n type: \"class\";\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n clazz: ServiceConstructor<any>;\n}\n\nexport interface FunctionFactory {\n type: \"function\";\n create: (options: ServiceOptions) => Service;\n}\n\nexport type ServiceFactory = ConstructorFactory | FunctionFactory;\n\nexport interface ServiceReprOptions {\n name: string;\n packageName: string;\n factory: ServiceFactory;\n intl: PackageIntl;\n dependencies?: ServiceDependency[];\n interfaces?: InterfaceSpec[];\n properties?: Record<string, unknown>;\n}\n\n/**\n * Represents metadata and state of a service in the runtime.\n * `this.instance` is the actual service instance (when constructed).\n */\nexport class ServiceRepr {\n static create(\n packageName: string,\n data: ServiceMetadata,\n intl: PackageIntl,\n properties?: Record<string, unknown>\n ): ServiceRepr {\n const clazz = data.clazz;\n const name = data.name;\n const dependencies = Object.entries(data.references ?? {}).map(\n ([name, referenceMetadata]) => {\n return {\n referenceName: name,\n ...parseReferenceSpec(referenceMetadata)\n };\n }\n );\n const interfaces = data.provides?.map<InterfaceSpec>((i) => {\n return {\n interfaceName: i.name,\n qualifier: i.qualifier\n };\n });\n const factory: ConstructorFactory = {\n type: \"class\",\n clazz\n };\n return new ServiceRepr({\n name,\n packageName,\n factory,\n intl,\n dependencies,\n interfaces,\n properties\n });\n }\n\n /** Unique id of this service. Contains the package name and the service name. */\n readonly id: string;\n\n /** Name of this service in it's package. */\n readonly name: string;\n\n /** Name of the parent package. */\n readonly packageName: string;\n\n /** Locale-dependant i18n messages. */\n readonly intl: PackageIntl;\n\n /** Service properties made available via the service's constructor. */\n readonly properties: Readonly<Record<string, unknown>>;\n\n /** Dependencies required by the service constructor. */\n readonly dependencies: readonly ServiceDependency[];\n\n /** Interfaces provided by the service. */\n readonly interfaces: readonly Readonly<InterfaceSpec>[];\n\n /** Number of references to this service. */\n private _useCount = 0;\n\n /** Service factory to construct an instance. */\n private factory: ServiceFactory;\n\n /** Current state of this service. \"constructed\" -> instance is available. */\n private _state: ServiceState = \"not-constructed\";\n\n /** Service instance, once constructed. */\n private _instance: Service | undefined = undefined;\n\n constructor(options: ServiceReprOptions) {\n const {\n name,\n packageName,\n factory,\n intl,\n dependencies = [],\n interfaces = [],\n properties = {}\n } = options;\n if (!isValidServiceName(name)) {\n throw new Error(ErrorId.INTERNAL, `Invalid service name: '${name}'.`);\n }\n\n this.id = `${packageName}::${name}`;\n this.name = name;\n this.packageName = packageName;\n this.factory = factory;\n this.intl = intl;\n this.dependencies = dependencies;\n this.interfaces = interfaces;\n this.properties = properties;\n }\n\n /** Returns the current service instance or undefined if the service has not been constructed. */\n get instance(): Service | undefined {\n return this._instance;\n }\n\n /** Returns the current state of the service. */\n get state() {\n return this._state;\n }\n\n /** Returns the current reference count. */\n get useCount() {\n return this._useCount;\n }\n\n /**\n * Same as `instance`, but throws when the instance has not been constructed.\n */\n getInstanceOrThrow() {\n const instance = this._instance;\n if (!instance) {\n throw new Error(ErrorId.INTERNAL, \"Expected service instance to be present.\");\n }\n return instance;\n }\n\n /**\n * Called before `create()` to place the service into the `constructing` state,\n * which is currently used to detect cycles.\n */\n beforeCreate() {\n if (this._state === \"not-constructed\") {\n this._state = \"constructing\";\n } else {\n throw new Error(\n ErrorId.INTERNAL,\n \"Inconsistent state: service is already under construction.\"\n );\n }\n }\n\n /**\n * Instantiates the service by invoking the service constructor\n * with the given `options`.\n *\n * The service's use count is initialized to `1`, so every `create()`\n * should be paired with a `removeRef()`.\n *\n * `destroy()` can be invoked once the final `removeRef()` has returned zero.\n */\n create(options: Pick<ServiceOptions, \"references\" | \"referencesMeta\">) {\n if (this._state !== \"constructing\" || this.instance !== undefined) {\n throw new Error(\n ErrorId.INTERNAL,\n \"Inconsistent state: service is not being constructed.\"\n );\n }\n try {\n this._instance = createService(this.factory, {\n ...options,\n properties: this.properties,\n intl: this.intl\n });\n this._state = \"constructed\";\n this._useCount = 1;\n return this._instance;\n } catch (e) {\n throw new Error(\n ErrorId.SERVICE_CONSTRUCTION_FAILED,\n `Failed to create service instance '${this.id}'.`,\n { cause: e }\n );\n }\n }\n\n destroy() {\n if (this._instance) {\n try {\n this._instance.destroy?.();\n } catch (e) {\n throw new Error(\n ErrorId.SERVICE_DESTRUCTION_FAILED,\n `Failed to destroy service instance '${this.id}'.`,\n { cause: e }\n );\n }\n }\n this._instance = undefined;\n this._state = \"destroyed\";\n }\n\n /**\n * Adds a use to the service's use count.\n * References to a service are tracked: it should only be destroyed when it is no longer being used.\n */\n addRef() {\n return (this._useCount += 1);\n }\n\n /**\n * Removes a use from the service's use count.\n * Returns the new use count.\n */\n removeRef() {\n return (this._useCount -= 1);\n }\n}\n\nexport function createConstructorFactory<T extends {}>(\n clazz: ServiceConstructor<T>\n): ConstructorFactory {\n return { type: \"class\", clazz };\n}\n\nexport function createFunctionFactory(\n create: (options: ServiceOptions) => Service\n): FunctionFactory {\n return { type: \"function\", create };\n}\n\nconst SERVICE_NAME_REGEX = /^[a-z0-9_-]+$/i;\n\nfunction isValidServiceName(name: string) {\n return SERVICE_NAME_REGEX.test(name);\n}\n\nfunction createService(factory: ServiceFactory, options: ServiceOptions) {\n switch (factory.type) {\n case \"class\":\n return new factory.clazz(options);\n case \"function\":\n return factory.create(options);\n }\n}\n"],"names":["name"],"mappings":";;;;AAwCO,MAAM,WAAY,CAAA;AAAA,EACrB,OAAO,MAAA,CACH,WACA,EAAA,IAAA,EACA,MACA,UACW,EAAA;AACX,IAAA,MAAM,QAAQ,IAAK,CAAA,KAAA,CAAA;AACnB,IAAA,MAAM,OAAO,IAAK,CAAA,IAAA,CAAA;AAClB,IAAA,MAAM,eAAe,MAAO,CAAA,OAAA,CAAQ,KAAK,UAAc,IAAA,EAAE,CAAE,CAAA,GAAA;AAAA,MACvD,CAAC,CAACA,KAAM,EAAA,iBAAiB,CAAM,KAAA;AAC3B,QAAO,OAAA;AAAA,UACH,aAAeA,EAAAA,KAAAA;AAAA,UACf,GAAG,mBAAmB,iBAAiB,CAAA;AAAA,SAC3C,CAAA;AAAA,OACJ;AAAA,KACJ,CAAA;AACA,IAAA,MAAM,UAAa,GAAA,IAAA,CAAK,QAAU,EAAA,GAAA,CAAmB,CAAC,CAAM,KAAA;AACxD,MAAO,OAAA;AAAA,QACH,eAAe,CAAE,CAAA,IAAA;AAAA,QACjB,WAAW,CAAE,CAAA,SAAA;AAAA,OACjB,CAAA;AAAA,KACH,CAAA,CAAA;AACD,IAAA,MAAM,OAA8B,GAAA;AAAA,MAChC,IAAM,EAAA,OAAA;AAAA,MACN,KAAA;AAAA,KACJ,CAAA;AACA,IAAA,OAAO,IAAI,WAAY,C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,IAAA,CAAA,CAAA;AAAA,GAC9B;AACJ,CAAA;AAEO,SAAS,yBACZ,KACkB,EAAA;AAClB,EAAO,OAAA,EAAE,IAAM,EAAA,OAAA,EAAS,KAAM,EAAA,CAAA;AAClC,CAAA;AAEO,SAAS,sBACZ,MACe,EAAA;AACf,EAAO,OAAA,EAAE,IAAM,EAAA,UAAA,EAAY,MAAO,EAAA,CAAA;AACtC,CAAA;AAEA,MAAM,kBAAqB,GAAA,gBAAA,CAAA;AAE3B,SAAS,mBAAmB,IAAc,EAAA;AACtC,EAAO,OAAA,kBAAA,CAAmB,KAAK,IAAI,CAAA,CAAA;AACvC,CAAA;AAEA,SAAS,aAAA,CAAc,SAAyB,OAAyB,EAAA;AACrE,EAAA,QAAQ,QAAQ,IAAM;AAAA,IAClB,KAAK,OAAA;AACD,MAAO,OAAA,IAAI,OAAQ,CAAA,KAAA,CAAM,OAAO,CAAA,CAAA;AAAA,IACpC,KAAK,UAAA;AACD,MAAO,OAAA,OAAA,CAAQ,OAAO,OAAO,CAAA,CAAA;AAAA,GACrC;AACJ;;;;"}
1
+ {"version":3,"file":"ServiceRepr.js","sources":["ServiceRepr.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2023-2025 Open Pioneer project (https://github.com/open-pioneer)\n// SPDX-License-Identifier: Apache-2.0\nimport { ErrorId } from \"../errors\";\nimport { ServiceMetadata } from \"../metadata\";\nimport { Service, ServiceConstructor, ServiceOptions } from \"../Service\";\nimport { Error } from \"@open-pioneer/core\";\nimport { InterfaceSpec, parseReferenceSpec, ReferenceSpec } from \"./InterfaceSpec\";\nimport { PackageIntl } from \"../i18n\";\n\nexport type ServiceState = \"not-constructed\" | \"constructing\" | \"constructed\" | \"destroyed\";\n\nexport type ServiceDependency = ReferenceSpec & { referenceName: string };\n\nexport interface ConstructorFactory {\n type: \"class\";\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n clazz: ServiceConstructor<any>;\n}\n\nexport interface FunctionFactory {\n type: \"function\";\n create: (options: ServiceOptions) => Service;\n}\n\nexport type ServiceFactory = ConstructorFactory | FunctionFactory;\n\nexport interface ServiceReprOptions {\n name: string;\n packageName: string;\n factory: ServiceFactory;\n intl: PackageIntl;\n dependencies?: ServiceDependency[];\n interfaces?: InterfaceSpec[];\n properties?: Record<string, unknown>;\n}\n\n/**\n * Represents metadata and state of a service in the runtime.\n * `this.instance` is the actual service instance (when constructed).\n */\nexport class ServiceRepr {\n static create(\n packageName: string,\n data: ServiceMetadata,\n intl: PackageIntl,\n properties?: Record<string, unknown>\n ): ServiceRepr {\n const clazz = data.clazz;\n const name = data.name;\n const dependencies = Object.entries(data.references ?? {}).map(\n ([name, referenceMetadata]) => {\n return {\n referenceName: name,\n ...parseReferenceSpec(referenceMetadata)\n };\n }\n );\n const interfaces = data.provides?.map<InterfaceSpec>((i) => {\n return {\n interfaceName: i.name,\n qualifier: i.qualifier\n };\n });\n const factory: ConstructorFactory = {\n type: \"class\",\n clazz\n };\n return new ServiceRepr({\n name,\n packageName,\n factory,\n intl,\n dependencies,\n interfaces,\n properties\n });\n }\n\n /** Unique id of this service. Contains the package name and the service name. */\n readonly id: string;\n\n /** Name of this service in it's package. */\n readonly name: string;\n\n /** Name of the parent package. */\n readonly packageName: string;\n\n /** Locale-dependant i18n messages. */\n readonly intl: PackageIntl;\n\n /** Service properties made available via the service's constructor. */\n readonly properties: Readonly<Record<string, unknown>>;\n\n /** Dependencies required by the service constructor. */\n readonly dependencies: readonly ServiceDependency[];\n\n /** Interfaces provided by the service. */\n readonly interfaces: readonly Readonly<InterfaceSpec>[];\n\n /** Number of references to this service. */\n private _useCount = 0;\n\n /** Service factory to construct an instance. */\n private factory: ServiceFactory;\n\n /** Current state of this service. \"constructed\" -> instance is available. */\n private _state: ServiceState = \"not-constructed\";\n\n /** Service instance, once constructed. */\n private _instance: Service | undefined = undefined;\n\n constructor(options: ServiceReprOptions) {\n const {\n name,\n packageName,\n factory,\n intl,\n dependencies = [],\n interfaces = [],\n properties = {}\n } = options;\n if (!isValidServiceName(name)) {\n throw new Error(ErrorId.INTERNAL, `Invalid service name: '${name}'.`);\n }\n\n this.id = `${packageName}::${name}`;\n this.name = name;\n this.packageName = packageName;\n this.factory = factory;\n this.intl = intl;\n this.dependencies = dependencies;\n this.interfaces = interfaces;\n this.properties = properties;\n }\n\n /** Returns the current service instance or undefined if the service has not been constructed. */\n get instance(): Service | undefined {\n return this._instance;\n }\n\n /** Returns the current state of the service. */\n get state() {\n return this._state;\n }\n\n /** Returns the current reference count. */\n get useCount() {\n return this._useCount;\n }\n\n /**\n * Same as `instance`, but throws when the instance has not been constructed.\n */\n getInstanceOrThrow() {\n const instance = this._instance;\n if (!instance) {\n throw new Error(ErrorId.INTERNAL, \"Expected service instance to be present.\");\n }\n return instance;\n }\n\n /**\n * Called before `create()` to place the service into the `constructing` state,\n * which is currently used to detect cycles.\n */\n beforeCreate() {\n if (this._state === \"not-constructed\") {\n this._state = \"constructing\";\n } else {\n throw new Error(\n ErrorId.INTERNAL,\n \"Inconsistent state: service is already under construction.\"\n );\n }\n }\n\n /**\n * Instantiates the service by invoking the service constructor\n * with the given `options`.\n *\n * The service's use count is initialized to `1`, so every `create()`\n * should be paired with a `removeRef()`.\n *\n * `destroy()` can be invoked once the final `removeRef()` has returned zero.\n */\n create(options: Pick<ServiceOptions, \"references\" | \"referencesMeta\">) {\n if (this._state !== \"constructing\" || this.instance !== undefined) {\n throw new Error(\n ErrorId.INTERNAL,\n \"Inconsistent state: service is not being constructed.\"\n );\n }\n try {\n this._instance = createService(this.factory, {\n ...options,\n properties: this.properties,\n intl: this.intl\n });\n this._state = \"constructed\";\n this._useCount = 1;\n return this._instance;\n } catch (e) {\n throw new Error(\n ErrorId.SERVICE_CONSTRUCTION_FAILED,\n `Failed to create service instance '${this.id}'.`,\n { cause: e }\n );\n }\n }\n\n destroy() {\n if (this._instance) {\n try {\n this._instance.destroy?.();\n } catch (e) {\n throw new Error(\n ErrorId.SERVICE_DESTRUCTION_FAILED,\n `Failed to destroy service instance '${this.id}'.`,\n { cause: e }\n );\n }\n }\n this._instance = undefined;\n this._state = \"destroyed\";\n }\n\n /**\n * Adds a use to the service's use count.\n * References to a service are tracked: it should only be destroyed when it is no longer being used.\n */\n addRef() {\n return (this._useCount += 1);\n }\n\n /**\n * Removes a use from the service's use count.\n * Returns the new use count.\n */\n removeRef() {\n return (this._useCount -= 1);\n }\n}\n\nexport function createConstructorFactory<T extends {}>(\n clazz: ServiceConstructor<T>\n): ConstructorFactory {\n return { type: \"class\", clazz };\n}\n\nexport function createFunctionFactory(\n create: (options: ServiceOptions) => Service\n): FunctionFactory {\n return { type: \"function\", create };\n}\n\nconst SERVICE_NAME_REGEX = /^[a-z0-9_-]+$/i;\n\nfunction isValidServiceName(name: string) {\n return SERVICE_NAME_REGEX.test(name);\n}\n\nfunction createService(factory: ServiceFactory, options: ServiceOptions) {\n switch (factory.type) {\n case \"class\":\n return new factory.clazz(options);\n case \"function\":\n return factory.create(options);\n }\n}\n"],"names":["name"],"mappings":";;;;AAwCO,MAAM,WAAY,CAAA;AAAA,EACrB,OAAO,MAAA,CACH,WACA,EAAA,IAAA,EACA,MACA,UACW,EAAA;AACX,IAAA,MAAM,QAAQ,IAAK,CAAA,KAAA;AACnB,IAAA,MAAM,OAAO,IAAK,CAAA,IAAA;AAClB,IAAA,MAAM,eAAe,MAAO,CAAA,OAAA,CAAQ,KAAK,UAAc,IAAA,EAAE,CAAE,CAAA,GAAA;AAAA,MACvD,CAAC,CAACA,KAAM,EAAA,iBAAiB,CAAM,KAAA;AAC3B,QAAO,OAAA;AAAA,UACH,aAAeA,EAAAA,KAAAA;AAAA,UACf,GAAG,mBAAmB,iBAAiB;AAAA,SAC3C;AAAA;AACJ,KACJ;AACA,IAAA,MAAM,UAAa,GAAA,IAAA,CAAK,QAAU,EAAA,GAAA,CAAmB,CAAC,CAAM,KAAA;AACxD,MAAO,OAAA;AAAA,QACH,eAAe,CAAE,CAAA,IAAA;AAAA,QACjB,WAAW,CAAE,C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B,SAAS,mBAAmB,IAAc,EAAA;AACtC,EAAO,OAAA,kBAAA,CAAmB,KAAK,IAAI,CAAA;AACvC;AAEA,SAAS,aAAA,CAAc,SAAyB,OAAyB,EAAA;AACrE,EAAA,QAAQ,QAAQ,IAAM;AAAA,IAClB,KAAK,OAAA;AACD,MAAO,OAAA,IAAI,OAAQ,CAAA,KAAA,CAAM,OAAO,CAAA;AAAA,IACpC,KAAK,UAAA;AACD,MAAO,OAAA,OAAA,CAAQ,OAAO,OAAO,CAAA;AAAA;AAEzC;;;;"}
@@ -1 +1 @@
1
- {"version":3,"file":"verifyDependencies.js","sources":["verifyDependencies.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2023 Open Pioneer project (https://github.com/open-pioneer)\n// SPDX-License-Identifier: Apache-2.0\nimport { Error } from \"@open-pioneer/core\";\nimport { ErrorId } from \"../errors\";\nimport { InterfaceSpec, ReferenceSpec, renderInterfaceSpec } from \"./InterfaceSpec\";\nimport {\n AmbiguousChoice,\n ReadonlyServiceLookup,\n renderAmbiguousServiceChoices,\n ServiceLookup\n} from \"./ServiceLookup\";\nimport { ServiceRepr } from \"./ServiceRepr\";\n\nexport interface DependencyOptions {\n /**\n * All services known to the service layer.\n */\n services?: readonly ServiceRepr[];\n\n /**\n * References that must be satisfied by the services.\n */\n requiredReferences?: readonly RequiredReference[];\n}\n\nexport type RequiredReference = UIDependency | FrameworkDependency;\n\nexport type UIDependency = { type: \"ui\"; packageName: string } & ReferenceSpec;\n\nexport type FrameworkDependency = { type: \"framework\" } & ReferenceSpec;\n\nexport type ComputedServiceDependencies = Record<string, ServiceRepr | ServiceRepr[]>;\n\nexport interface VerifyDependenciesResult {\n /**\n * Precomputed dependencies for every service.\n *\n * Key: service id.\n */\n serviceDependencies: Map<string, ComputedServiceDependencies>;\n\n /**\n * Index structure mapping interface names to service instances.\n */\n serviceLookup: ReadonlyServiceLookup;\n}\n\n/**\n * Visits all services and ensures that their dependencies can be satisfied by each other without a cycle.\n *\n * Throws an error if a cycle is detected or if a required service is never implemented.\n *\n * @returns an object containing the service index and precomputed dependency information\n */\nexport function verifyDependencies(options: DependencyOptions): VerifyDependenciesResult {\n const verifier = new Verifier(options);\n verifier.verify();\n return {\n serviceLookup: verifier.getServiceLookup(),\n serviceDependencies: verifier.getComputedDependencies()\n };\n}\n\ninterface GraphItem {\n state: ItemState;\n service: ServiceRepr;\n dependencies: ComputedServiceDependencies;\n}\n\ntype ItemState = \"not-visited\" | \"pending\" | \"done\";\n\n/** The reason why a graph node is being visited. */\ntype ItemVisitReason = \"root\" | InterfaceReference;\n\ntype InterfaceReference =\n | {\n type: \"framework-reference\";\n value: ReferenceSpec;\n }\n | {\n type: \"ui-reference\";\n packageName: string;\n value: ReferenceSpec;\n }\n | {\n type: \"service-reference\";\n service: ServiceRepr;\n referenceName: string;\n value: ReferenceSpec;\n };\n\nclass Verifier {\n private requiredReferences: readonly RequiredReference[];\n private items: GraphItem[];\n private serviceLookup = new ServiceLookup();\n private serviceToGraphItem = new Map<ServiceRepr, GraphItem>();\n\n // Stack of visited nodes\n private stack: [item: GraphItem, reason: ItemVisitReason][] = [];\n\n constructor(options: DependencyOptions) {\n this.requiredReferences = options.requiredReferences ?? [];\n const services = options.services ?? [];\n const items = (this.items = services.map<GraphItem>((service) => {\n return {\n service: service,\n state: \"not-visited\",\n dependencies: {}\n };\n }));\n\n for (const item of items) {\n for (const spec of item.service.interfaces) {\n this.registerService(item, spec);\n }\n }\n }\n\n verify() {\n for (const ref of this.requiredReferences) {\n let interfaceRef: InterfaceReference;\n switch (ref.type) {\n case \"framework\":\n interfaceRef = { type: \"framework-reference\", value: ref };\n break;\n case \"ui\":\n interfaceRef = {\n type: \"ui-reference\",\n packageName: ref.packageName,\n value: ref\n };\n break;\n }\n this.visitReference(interfaceRef);\n }\n for (const item of this.items) {\n this.visitItem(item, \"root\");\n }\n }\n\n getServiceLookup() {\n return this.serviceLookup;\n }\n\n getComputedDependencies() {\n return new Map(\n this.items.map<[string, ComputedServiceDependencies]>((item) => {\n return [item.service.id, item.dependencies];\n })\n );\n }\n\n private visitItem(item: GraphItem, reason: ItemVisitReason) {\n const stack = this.stack;\n const state = item.state;\n if (state === \"done\") {\n return; // Item is initialized, cycle impossible\n }\n if (state === \"pending\") {\n this.throwCycleError(item, reason);\n }\n\n stack.push([item, reason]);\n item.state = \"pending\";\n for (const dep of item.service.dependencies) {\n item.dependencies[dep.referenceName] = this.visitReference({\n type: \"service-reference\",\n service: item.service,\n referenceName: dep.referenceName,\n value: dep\n });\n }\n item.state = \"done\";\n stack.pop();\n }\n\n private visitReference(ref: InterfaceReference) {\n const itemOrItems = this.findServices(ref);\n if (Array.isArray(itemOrItems)) {\n for (const child of itemOrItems) {\n this.visitItem(child, ref);\n }\n return itemOrItems.map((item) => item.service);\n } else {\n this.visitItem(itemOrItems, ref);\n return itemOrItems.service;\n }\n }\n\n private registerService(item: GraphItem, spec: InterfaceSpec) {\n this.serviceLookup.register(item.service, spec);\n this.serviceToGraphItem.set(item.service, item);\n }\n\n private findServices(ref: InterfaceReference): GraphItem | GraphItem[] {\n const spec = ref.value;\n const lookupResult = this.serviceLookup.lookup(spec);\n switch (lookupResult.type) {\n case \"unimplemented\": {\n const message = unimplementedInterfaceMessage(ref);\n throw new Error(ErrorId.INTERFACE_NOT_FOUND, message);\n }\n case \"ambiguous\": {\n const message = ambiguousInterfaceMessage(ref, lookupResult.choices);\n throw new Error(ErrorId.AMBIGUOUS_DEPENDENCY, message);\n }\n }\n\n const value = lookupResult.value;\n if (Array.isArray(value)) {\n return value.map((service) => this.getGraphItem(service));\n } else {\n return this.getGraphItem(value);\n }\n }\n\n private getGraphItem(service: ServiceRepr): GraphItem {\n const graphItem = this.serviceToGraphItem.get(service);\n if (!graphItem || graphItem.service !== service) {\n throw new Error(\n ErrorId.INTERNAL,\n `Failed to find matching graph item for service '${service.id}'.`\n );\n }\n return graphItem;\n }\n\n private throwCycleError(item: GraphItem, reason: ItemVisitReason) {\n // Find the item on the stack (it must be there because we detected a cycle).\n const stack = this.stack;\n const ownIndex = stack.findIndex((entry) => entry[0] === item);\n if (ownIndex === -1) {\n throw new Error(ErrorId.INTERNAL, \"Failed to find cycle participant on the stack.\");\n }\n\n const cycleInfo = stack\n .slice(ownIndex)\n .concat([[item, reason]])\n .map<string>((entry, index) => {\n const item = entry[0];\n const reason = index === 0 ? \"root\" : entry[1]; // info before the cycle does not matter\n\n let message = `'${item.service.id}'`;\n if (typeof reason === \"object\") {\n message += ` (${referenceDetail(reason)})`;\n }\n return message;\n });\n throw new Error(\n ErrorId.DEPENDENCY_CYCLE,\n `Detected dependency cycle: ${cycleInfo.join(\" => \")}.`\n );\n }\n}\n\nfunction unimplementedInterfaceMessage(ref: InterfaceReference): string {\n const interfaceText = renderInterfaceSpec(ref.value);\n switch (ref.type) {\n case \"service-reference\":\n return `Service '${ref.service.id}' requires an unimplemented interface ${interfaceText} as dependency '${ref.referenceName}'.`;\n case \"ui-reference\":\n return `The UI of package '${ref.packageName}' requires an unimplemented interface ${interfaceText}.`;\n case \"framework-reference\":\n return `The framework requires an unimplemented interface ${interfaceText}.`;\n }\n}\n\nfunction ambiguousInterfaceMessage(ref: InterfaceReference, choices: AmbiguousChoice[]): string {\n const interfaceText = renderInterfaceSpec(ref.value);\n const serviceChoices = renderAmbiguousServiceChoices(choices);\n\n let message = \"\";\n switch (ref.type) {\n case \"service-reference\":\n message = `Service '${ref.service.id}' requires the ambiguous interface ${interfaceText} as dependency '${ref.referenceName}'.`;\n break;\n case \"ui-reference\":\n message = `The UI of package '${ref.packageName}' requires the ambiguous interface ${interfaceText}.`;\n break;\n case \"framework-reference\":\n message = `The framework requires the ambiguous interface ${interfaceText}.`;\n break;\n }\n message += ` Possible choices are ${serviceChoices}.`;\n return message;\n}\n\nfunction referenceDetail(reason: InterfaceReference) {\n switch (reason.type) {\n case \"service-reference\":\n return `'${reason.referenceName}' providing '${reason.value.interfaceName}'`;\n case \"ui-reference\":\n return `UI of package '${reason.packageName}' requiring '${reason.value.interfaceName}'`;\n case \"framework-reference\":\n return `framework requiring '${reason.value.interfaceName}'`;\n }\n}\n"],"names":["item","reason"],"mappings":";;;;;AAsDO,SAAS,mBAAmB,OAAsD,EAAA;AACrF,EAAM,MAAA,QAAA,GAAW,IAAI,QAAA,CAAS,OAAO,CAAA,CAAA;AACrC,EAAA,QAAA,CAAS,MAAO,EAAA,CAAA;AAChB,EAAO,OAAA;AAAA,IACH,aAAA,EAAe,SAAS,gBAAiB,EAAA;AAAA,IACzC,mBAAA,EAAqB,SAAS,uBAAwB,EAAA;AAAA,GAC1D,CAAA;AACJ,CAAA;AA8BA,MAAM,QAAS,CAAA;AAAA,EACH,kBAAA,CAAA;AAAA,EACA,KAAA,CAAA;AAAA,EACA,aAAA,GAAgB,IAAI,aAAc,EAAA,CAAA;AAAA,EAClC,kBAAA,uBAAyB,GAA4B,EAAA,CAAA;AAAA;AAAA,EAGrD,QAAsD,EAAC,CAAA;AAAA,EAE/D,YAAY,OAA4B,EAAA;AACpC,IAAK,IAAA,CAAA,kBAAA,GAAqB,OAAQ,CAAA,kBAAA,IAAsB,EAAC,CAAA;AACzD,IAAM,MAAA,QAAA,GAAW,OAAQ,CAAA,QAAA,IAAY,EAAC,CAAA;AACtC,IAAA,MAAM,QAAS,IAAK,CAAA,KAAA,GAAQ,QAAS,CAAA,GAAA,CAAe,CAAC,OAAY,KAAA;AAC7D,MAAO,OAAA;AAAA,QACH,OAAA;AAAA,QACA,KAAO,EAAA,aAAA;AAAA,QACP,cAAc,EAAC;AAAA,OACnB,CAAA;AAAA,KACH,CAAA,CAAA;AAED,IAAA,KAAA,MAAW,QAAQ,KAAO,EAAA;AACtB,MAAW,KAAA,MAAA,IAAA,IAAQ,IAAK,CAAA,OAAA,CAAQ,UAAY,EAAA;AACxC,QAAK,IAAA,CAAA,eAAA,CAAgB,MAAM,IAAI,CAAA,CAAA;AAAA,OACnC;AAAA,KACJ;AAAA,GACJ;AAAA,EAEA,MAAS,GAAA;AACL,IAAW,KAA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AAA,OAAA,GAAU,kDAAkD,aAAa,CAAA,CAAA,CAAA,CAAA;AACzE,MAAA,MAAA;AAAA,GACR;AACA,EAAA,OAAA,IAAW,yBAAyB,cAAc,CAAA,CAAA,CAAA,CAAA;AAClD,EAAO,OAAA,OAAA,CAAA;AACX,CAAA;AAEA,SAAS,gBAAgB,MAA4B,EAAA;AACjD,EAAA,QAAQ,OAAO,IAAM;AAAA,IACjB,KAAK,mBAAA;AACD,MAAA,OAAO,IAAI,MAAO,CAAA,aAAa,CAAgB,aAAA,EAAA,MAAA,CAAO,MAAM,aAAa,CAAA,CAAA,CAAA,CAAA;AAAA,IAC7E,KAAK,cAAA;AACD,MAAA,OAAO,kBAAkB,MAAO,CAAA,WAAW,CAAgB,aAAA,EAAA,MAAA,CAAO,MAAM,aAAa,CAAA,CAAA,CAAA,CAAA;AAAA,IACzF,KAAK,qBAAA;AACD,MAAO,OAAA,CAAA,qBAAA,EAAwB,MAAO,CAAA,KAAA,CAAM,aAAa,CAAA,CAAA,CAAA,CAAA;AAAA,GACjE;AACJ;;;;"}
1
+ {"version":3,"file":"verifyDependencies.js","sources":["verifyDependencies.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2023-2025 Open Pioneer project (https://github.com/open-pioneer)\n// SPDX-License-Identifier: Apache-2.0\nimport { Error } from \"@open-pioneer/core\";\nimport { ErrorId } from \"../errors\";\nimport { InterfaceSpec, ReferenceSpec, renderInterfaceSpec } from \"./InterfaceSpec\";\nimport {\n AmbiguousChoice,\n ReadonlyServiceLookup,\n renderAmbiguousServiceChoices,\n ServiceLookup\n} from \"./ServiceLookup\";\nimport { ServiceRepr } from \"./ServiceRepr\";\n\nexport interface DependencyOptions {\n /**\n * All services known to the service layer.\n */\n services?: readonly ServiceRepr[];\n\n /**\n * References that must be satisfied by the services.\n */\n requiredReferences?: readonly RequiredReference[];\n}\n\nexport type RequiredReference = UIDependency | FrameworkDependency;\n\nexport type UIDependency = { type: \"ui\"; packageName: string } & ReferenceSpec;\n\nexport type FrameworkDependency = { type: \"framework\" } & ReferenceSpec;\n\nexport type ComputedServiceDependencies = Record<string, ServiceRepr | ServiceRepr[]>;\n\nexport interface VerifyDependenciesResult {\n /**\n * Precomputed dependencies for every service.\n *\n * Key: service id.\n */\n serviceDependencies: Map<string, ComputedServiceDependencies>;\n\n /**\n * Index structure mapping interface names to service instances.\n */\n serviceLookup: ReadonlyServiceLookup;\n}\n\n/**\n * Visits all services and ensures that their dependencies can be satisfied by each other without a cycle.\n *\n * Throws an error if a cycle is detected or if a required service is never implemented.\n *\n * @returns an object containing the service index and precomputed dependency information\n */\nexport function verifyDependencies(options: DependencyOptions): VerifyDependenciesResult {\n const verifier = new Verifier(options);\n verifier.verify();\n return {\n serviceLookup: verifier.getServiceLookup(),\n serviceDependencies: verifier.getComputedDependencies()\n };\n}\n\ninterface GraphItem {\n state: ItemState;\n service: ServiceRepr;\n dependencies: ComputedServiceDependencies;\n}\n\ntype ItemState = \"not-visited\" | \"pending\" | \"done\";\n\n/** The reason why a graph node is being visited. */\ntype ItemVisitReason = \"root\" | InterfaceReference;\n\ntype InterfaceReference =\n | {\n type: \"framework-reference\";\n value: ReferenceSpec;\n }\n | {\n type: \"ui-reference\";\n packageName: string;\n value: ReferenceSpec;\n }\n | {\n type: \"service-reference\";\n service: ServiceRepr;\n referenceName: string;\n value: ReferenceSpec;\n };\n\nclass Verifier {\n private requiredReferences: readonly RequiredReference[];\n private items: GraphItem[];\n private serviceLookup = new ServiceLookup();\n private serviceToGraphItem = new Map<ServiceRepr, GraphItem>();\n\n // Stack of visited nodes\n private stack: [item: GraphItem, reason: ItemVisitReason][] = [];\n\n constructor(options: DependencyOptions) {\n this.requiredReferences = options.requiredReferences ?? [];\n const services = options.services ?? [];\n const items = (this.items = services.map<GraphItem>((service) => {\n return {\n service: service,\n state: \"not-visited\",\n dependencies: {}\n };\n }));\n\n for (const item of items) {\n for (const spec of item.service.interfaces) {\n this.registerService(item, spec);\n }\n }\n }\n\n verify() {\n for (const ref of this.requiredReferences) {\n let interfaceRef: InterfaceReference;\n switch (ref.type) {\n case \"framework\":\n interfaceRef = { type: \"framework-reference\", value: ref };\n break;\n case \"ui\":\n interfaceRef = {\n type: \"ui-reference\",\n packageName: ref.packageName,\n value: ref\n };\n break;\n }\n this.visitReference(interfaceRef);\n }\n for (const item of this.items) {\n this.visitItem(item, \"root\");\n }\n }\n\n getServiceLookup() {\n return this.serviceLookup;\n }\n\n getComputedDependencies() {\n return new Map(\n this.items.map<[string, ComputedServiceDependencies]>((item) => {\n return [item.service.id, item.dependencies];\n })\n );\n }\n\n private visitItem(item: GraphItem, reason: ItemVisitReason) {\n const stack = this.stack;\n const state = item.state;\n if (state === \"done\") {\n return; // Item is initialized, cycle impossible\n }\n if (state === \"pending\") {\n this.throwCycleError(item, reason);\n }\n\n stack.push([item, reason]);\n item.state = \"pending\";\n for (const dep of item.service.dependencies) {\n item.dependencies[dep.referenceName] = this.visitReference({\n type: \"service-reference\",\n service: item.service,\n referenceName: dep.referenceName,\n value: dep\n });\n }\n item.state = \"done\";\n stack.pop();\n }\n\n private visitReference(ref: InterfaceReference) {\n const itemOrItems = this.findServices(ref);\n if (Array.isArray(itemOrItems)) {\n for (const child of itemOrItems) {\n this.visitItem(child, ref);\n }\n return itemOrItems.map((item) => item.service);\n } else {\n this.visitItem(itemOrItems, ref);\n return itemOrItems.service;\n }\n }\n\n private registerService(item: GraphItem, spec: InterfaceSpec) {\n this.serviceLookup.register(item.service, spec);\n this.serviceToGraphItem.set(item.service, item);\n }\n\n private findServices(ref: InterfaceReference): GraphItem | GraphItem[] {\n const spec = ref.value;\n const lookupResult = this.serviceLookup.lookup(spec);\n switch (lookupResult.type) {\n case \"unimplemented\": {\n const message = unimplementedInterfaceMessage(ref);\n throw new Error(ErrorId.INTERFACE_NOT_FOUND, message);\n }\n case \"ambiguous\": {\n const message = ambiguousInterfaceMessage(ref, lookupResult.choices);\n throw new Error(ErrorId.AMBIGUOUS_DEPENDENCY, message);\n }\n }\n\n const value = lookupResult.value;\n if (Array.isArray(value)) {\n return value.map((service) => this.getGraphItem(service));\n } else {\n return this.getGraphItem(value);\n }\n }\n\n private getGraphItem(service: ServiceRepr): GraphItem {\n const graphItem = this.serviceToGraphItem.get(service);\n if (!graphItem || graphItem.service !== service) {\n throw new Error(\n ErrorId.INTERNAL,\n `Failed to find matching graph item for service '${service.id}'.`\n );\n }\n return graphItem;\n }\n\n private throwCycleError(item: GraphItem, reason: ItemVisitReason) {\n // Find the item on the stack (it must be there because we detected a cycle).\n const stack = this.stack;\n const ownIndex = stack.findIndex((entry) => entry[0] === item);\n if (ownIndex === -1) {\n throw new Error(ErrorId.INTERNAL, \"Failed to find cycle participant on the stack.\");\n }\n\n const cycleInfo = stack\n .slice(ownIndex)\n .concat([[item, reason]])\n .map<string>((entry, index) => {\n const item = entry[0];\n const reason = index === 0 ? \"root\" : entry[1]; // info before the cycle does not matter\n\n let message = `'${item.service.id}'`;\n if (typeof reason === \"object\") {\n message += ` (${referenceDetail(reason)})`;\n }\n return message;\n });\n throw new Error(\n ErrorId.DEPENDENCY_CYCLE,\n `Detected dependency cycle: ${cycleInfo.join(\" => \")}.`\n );\n }\n}\n\nfunction unimplementedInterfaceMessage(ref: InterfaceReference): string {\n const interfaceText = renderInterfaceSpec(ref.value);\n switch (ref.type) {\n case \"service-reference\":\n return `Service '${ref.service.id}' requires an unimplemented interface ${interfaceText} as dependency '${ref.referenceName}'.`;\n case \"ui-reference\":\n return `The UI of package '${ref.packageName}' requires an unimplemented interface ${interfaceText}.`;\n case \"framework-reference\":\n return `The framework requires an unimplemented interface ${interfaceText}.`;\n }\n}\n\nfunction ambiguousInterfaceMessage(ref: InterfaceReference, choices: AmbiguousChoice[]): string {\n const interfaceText = renderInterfaceSpec(ref.value);\n const serviceChoices = renderAmbiguousServiceChoices(choices);\n\n let message = \"\";\n switch (ref.type) {\n case \"service-reference\":\n message = `Service '${ref.service.id}' requires the ambiguous interface ${interfaceText} as dependency '${ref.referenceName}'.`;\n break;\n case \"ui-reference\":\n message = `The UI of package '${ref.packageName}' requires the ambiguous interface ${interfaceText}.`;\n break;\n case \"framework-reference\":\n message = `The framework requires the ambiguous interface ${interfaceText}.`;\n break;\n }\n message += ` Possible choices are ${serviceChoices}.`;\n return message;\n}\n\nfunction referenceDetail(reason: InterfaceReference) {\n switch (reason.type) {\n case \"service-reference\":\n return `'${reason.referenceName}' providing '${reason.value.interfaceName}'`;\n case \"ui-reference\":\n return `UI of package '${reason.packageName}' requiring '${reason.value.interfaceName}'`;\n case \"framework-reference\":\n return `framework requiring '${reason.value.interfaceName}'`;\n }\n}\n"],"names":["item","reason"],"mappings":";;;;;AAsDO,SAAS,mBAAmB,OAAsD,EAAA;AACrF,EAAM,MAAA,QAAA,GAAW,IAAI,QAAA,CAAS,OAAO,CAAA;AACrC,EAAA,QAAA,CAAS,MAAO,EAAA;AAChB,EAAO,OAAA;AAAA,IACH,aAAA,EAAe,SAAS,gBAAiB,EAAA;AAAA,IACzC,mBAAA,EAAqB,SAAS,uBAAwB;AAAA,GAC1D;AACJ;AA8BA,MAAM,QAAS,CAAA;AAAA,EACH,kBAAA;AAAA,EACA,KAAA;AAAA,EACA,aAAA,GAAgB,IAAI,aAAc,EAAA;AAAA,EAClC,kBAAA,uBAAyB,GAA4B,EAAA;AAAA;AAAA,EAGrD,QAAsD,EAAC;AAAA,EAE/D,YAAY,OAA4B,EAAA;AACpC,IAAK,IAAA,CAAA,kBAAA,GAAqB,OAAQ,CAAA,kBAAA,IAAsB,EAAC;AACzD,IAAM,MAAA,QAAA,GAAW,OAAQ,CAAA,QAAA,IAAY,EAAC;AACtC,IAAA,MAAM,QAAS,IAAK,CAAA,KAAA,GAAQ,QAAS,CAAA,GAAA,CAAe,CAAC,OAAY,KAAA;AAC7D,MAAO,OAAA;AAAA,QACH,OAAA;AAAA,QACA,KAAO,EAAA,aAAA;AAAA,QACP,cAAc;AAAC,OACnB;AAAA,KACH,CAAA;AAED,IAAA,KAAA,MAAW,QAAQ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