@xyo-network/module-resolver 5.3.22 → 5.3.25

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xyo-network/module-resolver",
3
- "version": "5.3.22",
3
+ "version": "5.3.25",
4
4
  "description": "Primary SDK for using XYO Protocol 2.0",
5
5
  "homepage": "https://xyo.network",
6
6
  "bugs": {
@@ -30,40 +30,36 @@
30
30
  "types": "dist/neutral/index.d.ts",
31
31
  "files": [
32
32
  "dist",
33
- "src",
34
33
  "!**/*.bench.*",
35
34
  "!**/*.spec.*",
36
35
  "!**/*.test.*",
37
36
  "README.md"
38
37
  ],
39
38
  "dependencies": {
40
- "@xyo-network/diviner-model": "~5.3.22",
41
- "@xyo-network/diviner-payload-model": "~5.3.22",
42
- "@xyo-network/module-model": "~5.3.22",
43
- "lru-cache": "~11.2.7"
39
+ "lru-cache": "~11.2.7",
40
+ "@xyo-network/diviner-model": "~5.3.25",
41
+ "@xyo-network/diviner-payload-model": "~5.3.25",
42
+ "@xyo-network/module-model": "~5.3.25"
44
43
  },
45
44
  "devDependencies": {
46
45
  "@opentelemetry/api": "^1.9.1",
47
46
  "@types/node": "^25.5.0",
48
- "@xylabs/sdk-js": "^5.0.91",
49
- "@xylabs/ts-scripts-common": "~7.6.8",
50
- "@xylabs/ts-scripts-yarn3": "~7.6.8",
51
- "@xylabs/tsconfig": "~7.6.8",
52
- "@xylabs/vitest-extended": "~5.0.91",
53
- "@xyo-network/diviner-model": "~5.3.22",
54
- "@xyo-network/diviner-payload-model": "~5.3.22",
55
- "@xyo-network/module-model": "~5.3.22",
47
+ "@xylabs/sdk-js": "^5.0.93",
48
+ "@xylabs/ts-scripts-common": "~7.6.16",
49
+ "@xylabs/ts-scripts-pnpm": "~7.6.16",
50
+ "@xylabs/tsconfig": "~7.6.16",
51
+ "@xylabs/vitest-extended": "~5.0.93",
56
52
  "acorn": "^8.16.0",
57
53
  "axios": "^1.14.0",
58
- "cosmiconfig": "^9.0.1",
59
- "esbuild": "^0.27.4",
60
- "eslint": "^10.1.0",
61
- "lru-cache": "^11.2.7",
62
- "rollup": "^4.60.1",
54
+ "esbuild": "^0.28.0",
55
+ "lru-cache": "~11.2.7",
63
56
  "typescript": "~5.9.3",
64
57
  "vite": "^8.0.3",
65
58
  "vitest": "~4.1.2",
66
- "zod": "^4.3.6"
59
+ "zod": "^4.3.6",
60
+ "@xyo-network/diviner-payload-model": "~5.3.25",
61
+ "@xyo-network/diviner-model": "~5.3.25",
62
+ "@xyo-network/module-model": "~5.3.25"
67
63
  },
68
64
  "peerDependencies": {
69
65
  "@xylabs/sdk-js": "^5",
@@ -72,4 +68,4 @@
72
68
  "publishConfig": {
73
69
  "access": "public"
74
70
  }
75
- }
71
+ }
@@ -1,89 +0,0 @@
1
- import type {
2
- Address, BaseParams, Promisable,
3
- } from '@xylabs/sdk-js'
4
- import {
5
- assertEx, Base, toSafeJsonString,
6
- } from '@xylabs/sdk-js'
7
- import type {
8
- ModuleFilterOptions,
9
- ModuleIdentifier,
10
- ModuleInstance,
11
- ModuleResolverInstance,
12
- ObjectFilterOptions,
13
- } from '@xyo-network/module-model'
14
- import {
15
- asModuleInstance,
16
- ObjectResolverPriority,
17
- } from '@xyo-network/module-model'
18
-
19
- export interface ModuleResolverParams extends BaseParams {
20
- priority?: ObjectResolverPriority
21
- root: ModuleInstance
22
- }
23
-
24
- export abstract class AbstractModuleResolver<TParams extends ModuleResolverParams = ModuleResolverParams>
25
- extends Base<TParams>
26
- implements ModuleResolverInstance {
27
- get priority() {
28
- return this.params.priority ?? ObjectResolverPriority.Normal
29
- }
30
-
31
- set priority(value: ObjectResolverPriority) {
32
- this.params.priority = value
33
- }
34
-
35
- get root() {
36
- return assertEx(this.params.root, () => 'root is not set')
37
- }
38
-
39
- async resolve<T extends ModuleInstance = ModuleInstance>(): Promise<T[]>
40
- async resolve<T extends ModuleInstance = ModuleInstance>(all: '*', options?: ModuleFilterOptions<T>): Promise<T[]>
41
- async resolve<T extends ModuleInstance = ModuleInstance>(id: ModuleIdentifier, options?: ModuleFilterOptions<T>): Promise<T | undefined>
42
- async resolve<T extends ModuleInstance = ModuleInstance>(
43
- id: ModuleIdentifier = '*',
44
- options?: ModuleFilterOptions<T>,
45
- ): Promise<T[] | T | undefined> {
46
- if (id === '*') {
47
- const values = await this.resolveHandler(id, options)
48
- assertEx(Array.isArray(values), () => 'resolveHandler returned a non-array')
49
- return values.map(value =>
50
- asModuleInstance<T>(value, () => {
51
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
52
- return `resolveHandler returned invalid result (*) [${(value as any)?.constructor?.name}][${toSafeJsonString(value)}]`
53
- }, { required: true }))
54
- }
55
- switch (typeof id) {
56
- case 'string': {
57
- const [value] = await this.resolveHandler(id, options)
58
- return asModuleInstance<T>(
59
- value,
60
- () =>
61
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
62
- `resolveHandler returned invalid result (string) [${(value as any)?.constructor?.name}][${toSafeJsonString(value)}]`,
63
- )
64
- }
65
- default: {
66
- break
67
- }
68
- }
69
- }
70
-
71
- async resolvePrivate<T extends ModuleInstance = ModuleInstance>(all: '*', options?: ObjectFilterOptions<T>): Promise<T[]>
72
- async resolvePrivate<T extends ModuleInstance = ModuleInstance>(id: ModuleIdentifier, options?: ObjectFilterOptions<T>): Promise<T | undefined>
73
- async resolvePrivate<T extends ModuleInstance = ModuleInstance>(
74
- id: ModuleIdentifier,
75
- _options?: ObjectFilterOptions<T>,
76
- ): Promise<T | T[] | undefined> {
77
- if (id === '*') return await Promise.resolve([])
78
- }
79
-
80
- abstract addResolver(resolver: ModuleResolverInstance): this
81
- abstract removeResolver(resolver: ModuleResolverInstance): this
82
-
83
- abstract resolveHandler<T extends ModuleInstance = ModuleInstance>(
84
- id: ModuleIdentifier,
85
- options?: ModuleFilterOptions<T>,
86
- ): Promisable<T[]>
87
-
88
- abstract resolveIdentifier(id: ModuleIdentifier, options?: ObjectFilterOptions): Promisable<Address | undefined>
89
- }
@@ -1,259 +0,0 @@
1
- /* eslint-disable max-statements */
2
- import type { Address, Promisable } from '@xylabs/sdk-js'
3
- import {
4
- assertEx,
5
- exists, isDefined, isString,
6
- } from '@xylabs/sdk-js'
7
- import type {
8
- CacheConfig,
9
- ModuleFilterOptions,
10
- ModuleIdentifier,
11
- ModuleIdentifierPart,
12
- ModuleIdentifierTransformer,
13
- ModuleInstance,
14
- ModuleRepository,
15
- ModuleResolverInstance,
16
- ObjectFilterOptions,
17
- } from '@xyo-network/module-model'
18
- import {
19
- duplicateModules,
20
- ObjectResolverPriority,
21
- ResolveHelper,
22
- } from '@xyo-network/module-model'
23
- import { LRUCache } from 'lru-cache'
24
-
25
- import type { ModuleResolverParams } from './AbstractModuleResolver.ts'
26
- import { AbstractModuleResolver } from './AbstractModuleResolver.ts'
27
- import { SimpleModuleResolver } from './SimpleModuleResolver.ts'
28
-
29
- export interface CompositeModuleResolverParams extends ModuleResolverParams {
30
- allowNameResolution?: boolean
31
- cache?: CacheConfig
32
- moduleIdentifierTransformers?: ModuleIdentifierTransformer[]
33
- }
34
-
35
- const moduleIdentifierParts = (moduleIdentifier: ModuleIdentifier): ModuleIdentifierPart[] => {
36
- return moduleIdentifier?.split(':') as ModuleIdentifierPart[]
37
- }
38
-
39
- export class CompositeModuleResolver<T extends CompositeModuleResolverParams = CompositeModuleResolverParams>
40
- extends AbstractModuleResolver<T>
41
- implements ModuleRepository, ModuleResolverInstance {
42
- static readonly defaultMaxDepth = 3
43
-
44
- protected _cache: LRUCache<ModuleIdentifier, ModuleInstance>
45
- protected resolvers: ModuleResolverInstance[] = []
46
- private _allowAddResolver = true
47
- private _localResolver: SimpleModuleResolver
48
-
49
- constructor(params: T) {
50
- super(params)
51
- const localResolver = new SimpleModuleResolver({ allowNameResolution: params.allowNameResolution, root: params.root })
52
- this.addResolver(localResolver)
53
- const { max = 100, ttl = 1000 * 5 /* five seconds */ } = params.cache ?? {}
54
- this._cache = new LRUCache<ModuleIdentifier, ModuleInstance>({
55
- max, ttl, ...params.cache,
56
- })
57
- this._localResolver = localResolver
58
- }
59
-
60
- get allowAddResolver() {
61
- return this._allowAddResolver
62
- }
63
-
64
- set allowAddResolver(value: boolean) {
65
- this.resolvers = [this._localResolver]
66
- this._allowAddResolver = value
67
- }
68
-
69
- get allowNameResolution() {
70
- return this.params.allowNameResolution ?? true
71
- }
72
-
73
- private get moduleIdentifierTransformers() {
74
- return this.params.moduleIdentifierTransformers ?? ResolveHelper.transformers
75
- }
76
-
77
- add(mod: ModuleInstance): this
78
- add(mod: ModuleInstance[]): this
79
- add(mod: ModuleInstance | ModuleInstance[]): this {
80
- if (Array.isArray(mod)) {
81
- for (const modItem of mod) this.addSingleModule(modItem)
82
- } else {
83
- this.addSingleModule(mod)
84
- }
85
- return this
86
- }
87
-
88
- addResolver(resolver: ModuleResolverInstance): this {
89
- if (this.allowAddResolver) {
90
- this.resolvers.push(resolver)
91
- }
92
- return this
93
- }
94
-
95
- remove(addresses: Address[] | Address): this {
96
- if (Array.isArray(addresses)) {
97
- for (const address of addresses) this.removeSingleModule(address)
98
- } else {
99
- this.removeSingleModule(addresses)
100
- }
101
- return this
102
- }
103
-
104
- removeResolver(resolver: ModuleResolverInstance): this {
105
- this.resolvers = this.resolvers.filter(item => item !== resolver)
106
- return this
107
- }
108
-
109
- // eslint-disable-next-line complexity
110
- async resolveHandler<T extends ModuleInstance = ModuleInstance>(
111
- id: ModuleIdentifier = '*',
112
- options: ModuleFilterOptions<T> = {},
113
- ): Promise<T[]> {
114
- const mutatedOptions = { ...options, maxDepth: options?.maxDepth ?? CompositeModuleResolver.defaultMaxDepth }
115
-
116
- // resolve all
117
- if (id === '*') {
118
- // wen't too far?
119
- if (mutatedOptions.maxDepth < 0) {
120
- return []
121
- }
122
-
123
- // identity resolve?
124
- if (mutatedOptions.maxDepth === 0) {
125
- return (await this._localResolver.resolve('*', mutatedOptions)) ?? []
126
- }
127
-
128
- const childOptions = { ...mutatedOptions, maxDepth: mutatedOptions?.maxDepth - 1 }
129
-
130
- const result = await Promise.all(
131
- this.resolvers.map(async (resolver) => {
132
- const result: T[] = await resolver.resolve<T>('*', childOptions)
133
- return result
134
- }),
135
- )
136
- const flatResult: T[] = result.flat().filter(exists)
137
- return flatResult.filter((v, i, a) => duplicateModules(v, i, a))
138
- }
139
-
140
- if (typeof id === 'string') {
141
- // wen't too far?
142
- if (mutatedOptions.maxDepth < 0) {
143
- return []
144
- }
145
-
146
- // resolve ModuleIdentifier
147
- const idParts = moduleIdentifierParts(id)
148
- if (idParts.length > 1) {
149
- const mod = await this.resolveMultipartIdentifier<T>(id)
150
- return (
151
- mod
152
- ? Array.isArray(mod)
153
- ? mod
154
- : [mod]
155
- : []
156
- )
157
- }
158
- const resolvedId = await ResolveHelper.transformModuleIdentifier(id, this.moduleIdentifierTransformers)
159
- if (isString(resolvedId)) {
160
- if (mutatedOptions.maxDepth < 0) {
161
- return []
162
- }
163
- const cachedResult = this._cache.get(resolvedId)
164
- if (cachedResult) {
165
- if (cachedResult.status === 'error') {
166
- this._cache.delete(id)
167
- } else {
168
- return [cachedResult] as T[]
169
- }
170
- }
171
-
172
- // identity resolve?
173
- if (mutatedOptions.maxDepth === 0) {
174
- const mod = await this._localResolver.resolve(resolvedId, mutatedOptions)
175
- return (
176
- isDefined(mod)
177
- ? Array.isArray(mod)
178
- ? mod
179
- : [mod]
180
- : []
181
- )
182
- }
183
-
184
- // recursive function to resolve by priority
185
- const resolvePriority = async (priority: ObjectResolverPriority): Promise<T | undefined> => {
186
- const resolvers = this.resolvers.filter(resolver => resolver.priority === priority)
187
- const results: T[] = (
188
- await Promise.all(
189
- resolvers.map(async (resolver) => {
190
- const result: T | undefined = await resolver.resolve<T>(resolvedId, mutatedOptions)
191
- return result
192
- }),
193
- )
194
- ).filter(exists)
195
-
196
- const result: T | undefined = results.filter(exists).findLast((v, i, a) => duplicateModules(v, i, a))
197
- if (result) {
198
- this._cache.set(resolvedId, result)
199
- return result
200
- }
201
- // we know that ObjectResolverPriority.VeryLow is the minimum priority, so we can subtract 1 from the rest
202
- return priority === ObjectResolverPriority.VeryLow ? undefined : await resolvePriority((priority - 1) as ObjectResolverPriority)
203
- }
204
- const mod = await resolvePriority(ObjectResolverPriority.VeryHigh)
205
- return (
206
- mod
207
- ? Array.isArray(mod)
208
- ? mod
209
- : [mod]
210
- : []
211
- )
212
- }
213
- }
214
- return []
215
- }
216
-
217
- async resolveIdentifier(id: ModuleIdentifier, _options?: ObjectFilterOptions): Promise<Address | undefined> {
218
- const idParts = id.split(':')
219
- if (idParts.length > 1) {
220
- return this.resolveComplexIdentifier(id)
221
- }
222
- const results = (
223
- await Promise.all(
224
- this.resolvers.map(async (resolver) => {
225
- return await resolver.resolveIdentifier(id)
226
- }),
227
- )
228
- ).filter(exists)
229
- const result = results.shift()
230
- if (results.length > 0) {
231
- for (const altResult of results) {
232
- assertEx(altResult === result, () => `Inconsistent results for ${id} [${result}][${altResult}]`)
233
- }
234
- }
235
- return result
236
- }
237
-
238
- protected resolveComplexIdentifier(_id: ModuleIdentifier, _options?: ObjectFilterOptions): Promisable<Address | undefined> {
239
- throw new Error('Method not implemented.')
240
- }
241
-
242
- private addSingleModule(mod?: ModuleInstance) {
243
- if (mod) {
244
- this._localResolver.add(mod)
245
- }
246
- }
247
-
248
- private removeSingleModule(address: Address) {
249
- this._localResolver.remove(address)
250
- }
251
-
252
- private async resolveMultipartIdentifier<T extends ModuleInstance = ModuleInstance>(moduleIdentifier: ModuleIdentifier): Promise<T | undefined> {
253
- const idParts = moduleIdentifierParts(moduleIdentifier)
254
- assertEx(idParts.length >= 2, () => 'Not a valid multipart identifier')
255
- const id = assertEx(idParts.shift())
256
- const mod = (await this.resolve<T>(id)) ?? (await this.resolvePrivate<T>(id))
257
- return (await mod?.resolve<T>(idParts.join(':'))) ?? (await mod?.resolvePrivate<T>(idParts.join(':')))
258
- }
259
- }
@@ -1,52 +0,0 @@
1
- import type { Address } from '@xylabs/sdk-js'
2
- import { isDefined, isUndefined } from '@xylabs/sdk-js'
3
- import type { DivinerInstance } from '@xyo-network/diviner-model'
4
- import { PayloadDivinerQuerySchema } from '@xyo-network/diviner-payload-model'
5
- import type { ModuleIdentifier, ModuleIdentifierTransformer } from '@xyo-network/module-model'
6
- import { LRUCache } from 'lru-cache'
7
-
8
- export class NameRegistrarTransformer implements ModuleIdentifierTransformer {
9
- private _cache: LRUCache<ModuleIdentifier, ModuleIdentifier> = new LRUCache<ModuleIdentifier, ModuleIdentifier>({ max: 1000, ttl: 1000 * 5 })
10
-
11
- private registrarDiviner: DivinerInstance
12
- private root: string
13
-
14
- constructor(
15
- registrarDiviner: DivinerInstance,
16
- root: string,
17
- ) {
18
- this.registrarDiviner = registrarDiviner
19
- this.root = root
20
- }
21
-
22
- async transform(identifier: ModuleIdentifier): Promise<ModuleIdentifier> {
23
- const parts = identifier.split(':')
24
- const first = parts.shift()
25
- const nameParts = first?.split('.')
26
- if (nameParts?.length === 2 && nameParts[1] === this.root) {
27
- // check cache
28
- const cachedResult = this._cache.get(identifier)
29
- if (isDefined(cachedResult)) return cachedResult
30
-
31
- // not cached, so check registrar
32
- const query = {
33
- domain: nameParts[0], order: 'desc' as const, schema: PayloadDivinerQuerySchema, tld: nameParts[1],
34
- }
35
- const result = await this.registrarDiviner?.divine([query])
36
- const resultPayload = result?.shift()
37
- if (isUndefined(resultPayload)) {
38
- throw new Error(`Unable to resolve registrar name (failed) [${first}]`)
39
- }
40
- // TODO: Use proper types for this check
41
- if (isDefined(resultPayload)) {
42
- const address = (resultPayload as unknown as { address: Address[] }).address?.shift()
43
- if (address) {
44
- this._cache.set(identifier, address)
45
- return address
46
- }
47
- }
48
- throw new Error(`Unable to resolve registrar name (not found) [${first}]`)
49
- }
50
- return identifier
51
- }
52
- }
@@ -1,51 +0,0 @@
1
- import type {
2
- ModuleIdentifier, ModuleResolver,
3
- QueryableModule,
4
- } from '@xyo-network/module-model'
5
-
6
- export interface ModuleResolvedEventArgs {
7
- id?: ModuleIdentifier
8
- mod: QueryableModule
9
- }
10
-
11
- export interface ResolverEventEmitter {
12
- on(event: 'moduleResolved', listener: (args: ModuleResolvedEventArgs) => void): void
13
- }
14
-
15
- type ListenerFunction = (args: ModuleResolvedEventArgs) => void
16
-
17
- const getMixin = <T extends ModuleResolver = ModuleResolver>(resolver: T) => {
18
- const listeners: ListenerFunction[] = []
19
- const emit = (event: 'moduleResolved', args: ModuleResolvedEventArgs): boolean => {
20
- if (listeners.length === 0) return false
21
- for (const listener of listeners) listener(args)
22
- return true
23
- }
24
- const onModuleResolved = (mod: QueryableModule, id?: ModuleIdentifier) => {
25
- const args = { id, mod }
26
- emit('moduleResolved', args)
27
- }
28
-
29
- const { resolve } = resolver
30
- function originalResolve(filter?: ModuleIdentifier) {
31
- return resolve.bind(resolver)(filter)
32
- }
33
-
34
- return {
35
- on: (event: 'moduleResolved', listener: (args: ModuleResolvedEventArgs) => void) => {
36
- listeners.push(listener)
37
- },
38
- resolve: async (id?: ModuleIdentifier): Promise<QueryableModule[]> => {
39
- const modulesResult = await originalResolve(id) ?? []
40
- const modules = Array.isArray(modulesResult) ? modulesResult : [modulesResult]
41
-
42
- await Promise.allSettled(modules.map(mod => onModuleResolved(mod, id)))
43
- return modules
44
- },
45
- }
46
- }
47
-
48
- export const mixinResolverEventEmitter = <T extends ModuleResolver = ModuleResolver>(resolver: T): T & ResolverEventEmitter => {
49
- const mixin = getMixin(resolver)
50
- return Object.assign(resolver, mixin)
51
- }
@@ -1,173 +0,0 @@
1
- import type { Address, Promisable } from '@xylabs/sdk-js'
2
- import {
3
- assertEx, exists, isAddress,
4
- } from '@xylabs/sdk-js'
5
- import type {
6
- ModuleFilterOptions,
7
- ModuleIdentifier,
8
- ModuleInstance,
9
- ModuleName,
10
- ModuleRepository,
11
- ModuleResolverInstance,
12
- ObjectFilterOptions,
13
- } from '@xyo-network/module-model'
14
- import { isModuleName } from '@xyo-network/module-model'
15
-
16
- import type { ModuleResolverParams } from './AbstractModuleResolver.ts'
17
- import { AbstractModuleResolver } from './AbstractModuleResolver.ts'
18
-
19
- export type SimpleModuleResolverParams = ModuleResolverParams & {
20
- allowNameResolution?: boolean
21
- }
22
-
23
- export class SimpleModuleResolver extends AbstractModuleResolver<SimpleModuleResolverParams> implements ModuleRepository {
24
- private modules: Record<Address, ModuleInstance> = {}
25
- private nameToModule: Record<ModuleName, ModuleInstance> = {}
26
-
27
- constructor(params: SimpleModuleResolverParams) {
28
- super(params)
29
- }
30
-
31
- get allowNameResolution() {
32
- return this.params.allowNameResolution ?? true
33
- }
34
-
35
- add(mods: ModuleInstance): this
36
- add(mods: ModuleInstance[]): this
37
- add(mods: ModuleInstance | ModuleInstance[]): this {
38
- if (Array.isArray(mods)) {
39
- for (const mod of mods) this.addSingleModule(mod)
40
- } else {
41
- this.addSingleModule(mods)
42
- }
43
- return this
44
- }
45
-
46
- addResolver(_resolver: ModuleResolverInstance): this {
47
- throw 'Adding resolvers not supported'
48
- }
49
-
50
- remove(address: Address | Address[]): this {
51
- if (Array.isArray(address)) {
52
- for (const addr of address) this.removeSingleModule(addr)
53
- } else {
54
- this.removeSingleModule(address)
55
- }
56
- return this
57
- }
58
-
59
- removeResolver(_resolver: ModuleResolverInstance): this {
60
- throw 'Removing resolvers not supported'
61
- }
62
-
63
- resolveHandler<T extends ModuleInstance = ModuleInstance>(
64
- id: ModuleIdentifier = '*',
65
- options?: ModuleFilterOptions<T>,
66
- ): Promisable<T[]> {
67
- const unfiltered = (() => {
68
- if (id) {
69
- if (typeof id === 'string') {
70
- if (id === '*') {
71
- return Object.values(this.modules) as T[]
72
- }
73
- const name = isModuleName(id) ? id : undefined
74
- const address = isAddress(id) ? id : undefined
75
- assertEx(name || address, () => 'module identifier must be a ModuleName or Address')
76
- return (
77
- (name ? this.resolveByName<T>(Object.values(this.modules), [name]).pop() : undefined)
78
- ?? (address ? this.resolveByAddress<T>(this.modules, [address]).pop() : undefined)
79
- )
80
- }
81
- } else {
82
- return Object.values(this.modules) as T[]
83
- }
84
- })()
85
- const identity = options?.identity
86
- if (identity) {
87
- return (
88
- Array.isArray(unfiltered)
89
- ? unfiltered?.filter(mod => identity(mod))
90
- : identity(unfiltered)
91
- ? [unfiltered]
92
- : []
93
- )
94
- } else {
95
- return (
96
- unfiltered
97
- ? Array.isArray(unfiltered)
98
- ? unfiltered
99
- : [unfiltered]
100
- : []
101
- )
102
- }
103
- }
104
-
105
- resolveIdentifier(id: ModuleIdentifier, _options?: ObjectFilterOptions): Promisable<Address | undefined> {
106
- // check if id is a name of one of modules in the resolver
107
- const moduleFromName = this.nameToModule[id]
108
- if (moduleFromName) {
109
- return moduleFromName.address
110
- }
111
-
112
- // check if any of the modules have the id as an address
113
- for (const mod of Object.values(this.modules)) {
114
- if (mod.address === id) {
115
- return mod.address
116
- }
117
- }
118
- }
119
-
120
- private addSingleModule(mod?: ModuleInstance) {
121
- if (mod) {
122
- const modName = mod.modName
123
- if (modName && this.allowNameResolution) {
124
- // check for collision
125
- assertEx(this.nameToModule[modName] === undefined, () => `Module with name ${modName} already added`)
126
- this.nameToModule[modName] = mod
127
- }
128
- this.modules[mod.address] = mod
129
- }
130
- }
131
-
132
- private removeSingleModule(address: Address) {
133
- assertEx(isAddress(address), () => 'Invalid address')
134
- const mod = assertEx(this.modules[address], () => 'Address not found in modules')
135
- delete this.modules[address]
136
- const modName = mod.modName
137
- if (modName) {
138
- delete this.nameToModule[modName]
139
- }
140
- }
141
-
142
- private resolveByAddress<T extends ModuleInstance = ModuleInstance>(modules: Record<Address, ModuleInstance>, address: Address[]): T[] {
143
- return (
144
- address.map((address) => {
145
- return modules[address]
146
- })
147
- ).filter(exists) as T[]
148
- }
149
-
150
- private resolveByName<T extends ModuleInstance = ModuleInstance>(modules: ModuleInstance[], name: ModuleName[]): T[] {
151
- return (
152
- name.map((name) => {
153
- return modules.find(mod => mod.modName === name)
154
- })
155
- ).filter(exists) as T[]
156
- }
157
-
158
- private resolveByQuery<T extends ModuleInstance = ModuleInstance>(modules: ModuleInstance[], query: string[][]): T[] {
159
- return (
160
- modules.filter(mod =>
161
- // eslint-disable-next-line unicorn/no-array-reduce
162
- query?.reduce((supported, queryList) => {
163
- return (
164
- // eslint-disable-next-line unicorn/no-array-reduce
165
- queryList.reduce((supported, query) => {
166
- const queryable = mod.queries.includes(query)
167
- return supported && queryable
168
- }, true) || supported
169
- )
170
- }, false))
171
- ).filter(exists) as T[]
172
- }
173
- }
package/src/index.ts DELETED
@@ -1,5 +0,0 @@
1
- export * from './AbstractModuleResolver.ts'
2
- export * from './CompositeModuleResolver.ts'
3
- export * from './NameRegistrarTransformer.ts'
4
- export * from './ResolverEventEmitter.ts'
5
- export * from './SimpleModuleResolver.ts'