@xyo-network/module-factory-locator 2.98.3
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.
- package/LICENSE +165 -0
- package/README.md +13 -0
- package/dist/browser/ModuleFactoryLocator.d.cts +43 -0
- package/dist/browser/ModuleFactoryLocator.d.cts.map +1 -0
- package/dist/browser/ModuleFactoryLocator.d.mts +43 -0
- package/dist/browser/ModuleFactoryLocator.d.mts.map +1 -0
- package/dist/browser/ModuleFactoryLocator.d.ts +43 -0
- package/dist/browser/ModuleFactoryLocator.d.ts.map +1 -0
- package/dist/browser/index.cjs +150 -0
- package/dist/browser/index.cjs.map +1 -0
- package/dist/browser/index.d.cts +2 -0
- package/dist/browser/index.d.cts.map +1 -0
- package/dist/browser/index.d.mts +2 -0
- package/dist/browser/index.d.mts.map +1 -0
- package/dist/browser/index.d.ts +2 -0
- package/dist/browser/index.d.ts.map +1 -0
- package/dist/browser/index.js +129 -0
- package/dist/browser/index.js.map +1 -0
- package/dist/browser/standardCreatableFactories.d.cts +4 -0
- package/dist/browser/standardCreatableFactories.d.cts.map +1 -0
- package/dist/browser/standardCreatableFactories.d.mts +4 -0
- package/dist/browser/standardCreatableFactories.d.mts.map +1 -0
- package/dist/browser/standardCreatableFactories.d.ts +4 -0
- package/dist/browser/standardCreatableFactories.d.ts.map +1 -0
- package/dist/node/ModuleFactoryLocator.d.cts +43 -0
- package/dist/node/ModuleFactoryLocator.d.cts.map +1 -0
- package/dist/node/ModuleFactoryLocator.d.mts +43 -0
- package/dist/node/ModuleFactoryLocator.d.mts.map +1 -0
- package/dist/node/ModuleFactoryLocator.d.ts +43 -0
- package/dist/node/ModuleFactoryLocator.d.ts.map +1 -0
- package/dist/node/index.cjs +154 -0
- package/dist/node/index.cjs.map +1 -0
- package/dist/node/index.d.cts +2 -0
- package/dist/node/index.d.cts.map +1 -0
- package/dist/node/index.d.mts +2 -0
- package/dist/node/index.d.mts.map +1 -0
- package/dist/node/index.d.ts +2 -0
- package/dist/node/index.d.ts.map +1 -0
- package/dist/node/index.js +129 -0
- package/dist/node/index.js.map +1 -0
- package/dist/node/standardCreatableFactories.d.cts +4 -0
- package/dist/node/standardCreatableFactories.d.cts.map +1 -0
- package/dist/node/standardCreatableFactories.d.mts +4 -0
- package/dist/node/standardCreatableFactories.d.mts.map +1 -0
- package/dist/node/standardCreatableFactories.d.ts +4 -0
- package/dist/node/standardCreatableFactories.d.ts.map +1 -0
- package/package.json +77 -0
- package/src/ModuleFactoryLocator.ts +101 -0
- package/src/index.ts +1 -0
- package/src/standardCreatableFactories.ts +42 -0
- package/typedoc.json +5 -0
package/package.json
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@xyo-network/module-factory-locator",
|
|
3
|
+
"author": {
|
|
4
|
+
"email": "support@xyo.network",
|
|
5
|
+
"name": "XYO Development Team",
|
|
6
|
+
"url": "https://xyo.network"
|
|
7
|
+
},
|
|
8
|
+
"bugs": {
|
|
9
|
+
"email": "support@xyo.network",
|
|
10
|
+
"url": "https://github.com/XYOracleNetwork/sdk-xyo-client-js/issues"
|
|
11
|
+
},
|
|
12
|
+
"dependencies": {
|
|
13
|
+
"@xylabs/assert": "^3.1.10",
|
|
14
|
+
"@xyo-network/archivist-memory": "~2.98.3",
|
|
15
|
+
"@xyo-network/archivist-model": "~2.98.3",
|
|
16
|
+
"@xyo-network/archivist-view": "~2.98.3",
|
|
17
|
+
"@xyo-network/bridge-model": "~2.98.3",
|
|
18
|
+
"@xyo-network/diviner-identity": "~2.98.3",
|
|
19
|
+
"@xyo-network/diviner-model": "~2.98.3",
|
|
20
|
+
"@xyo-network/http-bridge": "~2.98.3",
|
|
21
|
+
"@xyo-network/module-model": "~2.98.3",
|
|
22
|
+
"@xyo-network/node-memory": "~2.98.3",
|
|
23
|
+
"@xyo-network/node-model": "~2.98.3",
|
|
24
|
+
"@xyo-network/node-view": "~2.98.3",
|
|
25
|
+
"@xyo-network/payload-model": "~2.98.3",
|
|
26
|
+
"@xyo-network/sentinel-memory": "~2.98.3",
|
|
27
|
+
"@xyo-network/sentinel-model": "~2.98.3",
|
|
28
|
+
"@xyo-network/witness-adhoc": "~2.98.3",
|
|
29
|
+
"@xyo-network/witness-model": "~2.98.3"
|
|
30
|
+
},
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"@xylabs/ts-scripts-yarn3": "^3.9.1",
|
|
33
|
+
"@xylabs/tsconfig": "^3.9.1",
|
|
34
|
+
"typescript": "^5.4.5"
|
|
35
|
+
},
|
|
36
|
+
"description": "Primary SDK for using XYO Protocol 2.0",
|
|
37
|
+
"types": "dist/node/index.d.ts",
|
|
38
|
+
"exports": {
|
|
39
|
+
".": {
|
|
40
|
+
"browser": {
|
|
41
|
+
"require": {
|
|
42
|
+
"types": "./dist/browser/index.d.cts",
|
|
43
|
+
"default": "./dist/browser/index.cjs"
|
|
44
|
+
},
|
|
45
|
+
"import": {
|
|
46
|
+
"types": "./dist/browser/index.d.mts",
|
|
47
|
+
"default": "./dist/browser/index.js"
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
"node": {
|
|
51
|
+
"require": {
|
|
52
|
+
"types": "./dist/node/index.d.cts",
|
|
53
|
+
"default": "./dist/node/index.cjs"
|
|
54
|
+
},
|
|
55
|
+
"import": {
|
|
56
|
+
"types": "./dist/node/index.d.mts",
|
|
57
|
+
"default": "./dist/node/index.js"
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
},
|
|
61
|
+
"./package.json": "./package.json"
|
|
62
|
+
},
|
|
63
|
+
"main": "dist/node/index.cjs",
|
|
64
|
+
"module": "dist/node/index.js",
|
|
65
|
+
"homepage": "https://xyo.network",
|
|
66
|
+
"license": "LGPL-3.0-only",
|
|
67
|
+
"publishConfig": {
|
|
68
|
+
"access": "public"
|
|
69
|
+
},
|
|
70
|
+
"repository": {
|
|
71
|
+
"type": "git",
|
|
72
|
+
"url": "https://github.com/XYOracleNetwork/sdk-xyo-client-js.git"
|
|
73
|
+
},
|
|
74
|
+
"sideEffects": false,
|
|
75
|
+
"version": "2.98.3",
|
|
76
|
+
"type": "module"
|
|
77
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { assertEx } from '@xylabs/assert'
|
|
2
|
+
import {
|
|
3
|
+
AttachableModuleInstance,
|
|
4
|
+
CreatableModuleFactory,
|
|
5
|
+
CreatableModuleRegistry,
|
|
6
|
+
hasAllLabels,
|
|
7
|
+
hasLabels,
|
|
8
|
+
LabeledCreatableModuleFactory,
|
|
9
|
+
Labels,
|
|
10
|
+
} from '@xyo-network/module-model'
|
|
11
|
+
import { Schema } from '@xyo-network/payload-model'
|
|
12
|
+
|
|
13
|
+
import { standardCreatableFactories } from './standardCreatableFactories'
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* A class which encapsulates the Service Locator Pattern for Module Factories
|
|
17
|
+
*/
|
|
18
|
+
export class ModuleFactoryLocator {
|
|
19
|
+
constructor(protected readonly _registry: CreatableModuleRegistry = standardCreatableFactories()) {}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The current registry for the module factory
|
|
23
|
+
*/
|
|
24
|
+
get registry(): Readonly<CreatableModuleRegistry> {
|
|
25
|
+
return this._registry
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Locates a module factory that matches the supplied schema and labels
|
|
30
|
+
* @param schema The config schema for the module
|
|
31
|
+
* @param labels The labels for the module factory
|
|
32
|
+
* @returns A module factory that matches the supplied schema and labels or throws if one is not found
|
|
33
|
+
*/
|
|
34
|
+
locate(schema: string, labels?: Labels): CreatableModuleFactory | LabeledCreatableModuleFactory {
|
|
35
|
+
return assertEx(
|
|
36
|
+
this.tryLocate(schema, labels),
|
|
37
|
+
() => `No module factory for the supplied ${`config schema [${schema}]`}${labels ? ` & labels [${JSON.stringify(labels)}]` : ''} registered`,
|
|
38
|
+
)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Registers a single module factory (with optional tags) with the locator
|
|
43
|
+
* @param additional Additional module factories to register
|
|
44
|
+
*/
|
|
45
|
+
register(mod: CreatableModuleFactory, labels?: Labels): this {
|
|
46
|
+
this.registerOne(mod, mod.defaultConfigSchema, labels, true)
|
|
47
|
+
mod.configSchemas.map((schema) => {
|
|
48
|
+
this.registerOne(mod, schema, labels, false)
|
|
49
|
+
})
|
|
50
|
+
return this
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Registers multiple module factories with the locator
|
|
55
|
+
* @param additional Additional module factories to register
|
|
56
|
+
*/
|
|
57
|
+
registerMany(mods: CreatableModuleFactory[]): this {
|
|
58
|
+
for (const mod of mods) {
|
|
59
|
+
this.register(mod)
|
|
60
|
+
}
|
|
61
|
+
return this
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Registers a single module factory (with optional tags) with the locator & a specific schema
|
|
66
|
+
* @param additional Additional module factories to register
|
|
67
|
+
*/
|
|
68
|
+
registerOne<TModule extends AttachableModuleInstance>(
|
|
69
|
+
mod: CreatableModuleFactory<TModule>,
|
|
70
|
+
schema: Schema,
|
|
71
|
+
labels?: Labels,
|
|
72
|
+
primary = false,
|
|
73
|
+
): this {
|
|
74
|
+
const existingFactories = this._registry[schema]
|
|
75
|
+
const factory: LabeledCreatableModuleFactory<TModule> = {
|
|
76
|
+
// Destructure instance properties
|
|
77
|
+
...mod,
|
|
78
|
+
// Copy static methods
|
|
79
|
+
create: mod.create.bind(mod) as LabeledCreatableModuleFactory<TModule>['create'],
|
|
80
|
+
// Merge module & supplied labels
|
|
81
|
+
labels: Object.assign({}, (mod as LabeledCreatableModuleFactory).labels ?? {}, labels ?? {}),
|
|
82
|
+
}
|
|
83
|
+
this._registry[schema] = primary ? [factory, ...(existingFactories ?? [])] : [...(existingFactories ?? []), factory]
|
|
84
|
+
return this
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Tries to locate a module factory that matches the supplied schema and labels
|
|
89
|
+
* @param schema The config schema for the module
|
|
90
|
+
* @param labels The labels for the module factory
|
|
91
|
+
* @returns A module factory that matches the supplied schema and labels or undefined
|
|
92
|
+
*/
|
|
93
|
+
tryLocate(schema: string, labels?: Labels): CreatableModuleFactory | LabeledCreatableModuleFactory | undefined {
|
|
94
|
+
// If labels were provided
|
|
95
|
+
return labels ?
|
|
96
|
+
// Find the first factory that has labels and has all the labels provided
|
|
97
|
+
this._registry[schema]?.filter(hasLabels).find((factory) => hasAllLabels(factory?.labels, labels))
|
|
98
|
+
: // Otherwise, return the first factory
|
|
99
|
+
this._registry[schema]?.[0]
|
|
100
|
+
}
|
|
101
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './ModuleFactoryLocator'
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { MemoryArchivist } from '@xyo-network/archivist-memory'
|
|
2
|
+
import { ArchivistConfigSchema } from '@xyo-network/archivist-model'
|
|
3
|
+
import { ViewArchivist } from '@xyo-network/archivist-view'
|
|
4
|
+
import { BridgeConfigSchema } from '@xyo-network/bridge-model'
|
|
5
|
+
import { IdentityDiviner } from '@xyo-network/diviner-identity'
|
|
6
|
+
import { DivinerConfigSchema } from '@xyo-network/diviner-model'
|
|
7
|
+
import { HttpBridge } from '@xyo-network/http-bridge'
|
|
8
|
+
import {
|
|
9
|
+
CreatableModuleFactory,
|
|
10
|
+
LabeledCreatableModuleFactory,
|
|
11
|
+
registerCreatableModuleFactories,
|
|
12
|
+
registerPrimaryCreatableModuleFactory,
|
|
13
|
+
} from '@xyo-network/module-model'
|
|
14
|
+
import { MemoryNode } from '@xyo-network/node-memory'
|
|
15
|
+
import { NodeConfigSchema } from '@xyo-network/node-model'
|
|
16
|
+
import { ViewNode } from '@xyo-network/node-view'
|
|
17
|
+
import { MemorySentinel } from '@xyo-network/sentinel-memory'
|
|
18
|
+
import { SentinelConfigSchema } from '@xyo-network/sentinel-model'
|
|
19
|
+
import { AdhocWitness } from '@xyo-network/witness-adhoc'
|
|
20
|
+
import { WitnessConfigSchema } from '@xyo-network/witness-model'
|
|
21
|
+
|
|
22
|
+
export const standardCreatableFactoriesList: (CreatableModuleFactory | LabeledCreatableModuleFactory)[] = [
|
|
23
|
+
MemoryArchivist,
|
|
24
|
+
HttpBridge,
|
|
25
|
+
MemoryArchivist,
|
|
26
|
+
MemoryNode,
|
|
27
|
+
MemorySentinel,
|
|
28
|
+
ViewArchivist,
|
|
29
|
+
ViewNode,
|
|
30
|
+
AdhocWitness,
|
|
31
|
+
]
|
|
32
|
+
|
|
33
|
+
export const standardCreatableFactories = () => {
|
|
34
|
+
const registry = registerCreatableModuleFactories(standardCreatableFactoriesList)
|
|
35
|
+
registerPrimaryCreatableModuleFactory(registry, MemoryNode, NodeConfigSchema)
|
|
36
|
+
registerPrimaryCreatableModuleFactory(registry, MemoryArchivist, ArchivistConfigSchema)
|
|
37
|
+
registerPrimaryCreatableModuleFactory(registry, MemorySentinel, SentinelConfigSchema)
|
|
38
|
+
registerPrimaryCreatableModuleFactory(registry, AdhocWitness, WitnessConfigSchema)
|
|
39
|
+
registerPrimaryCreatableModuleFactory(registry, IdentityDiviner, DivinerConfigSchema)
|
|
40
|
+
registerPrimaryCreatableModuleFactory(registry, HttpBridge, BridgeConfigSchema)
|
|
41
|
+
return registry
|
|
42
|
+
}
|