@xyo-network/bridge-abstract 5.3.22 → 5.3.24

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/bridge-abstract",
3
- "version": "5.3.22",
3
+ "version": "5.3.24",
4
4
  "description": "Primary SDK for using XYO Protocol 2.0",
5
5
  "homepage": "https://xyo.network",
6
6
  "bugs": {
@@ -30,79 +30,75 @@
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/account": "~5.3.22",
41
- "@xyo-network/account-model": "~5.3.22",
42
- "@xyo-network/archivist-model": "~5.3.22",
43
- "@xyo-network/archivist-wrapper": "~5.3.22",
44
- "@xyo-network/boundwitness-model": "~5.3.22",
45
- "@xyo-network/boundwitness-wrapper": "~5.3.22",
46
- "@xyo-network/bridge-model": "~5.3.22",
47
- "@xyo-network/diviner-model": "~5.3.22",
48
- "@xyo-network/diviner-wrapper": "~5.3.22",
49
- "@xyo-network/manifest-model": "~5.3.22",
50
- "@xyo-network/module-abstract": "~5.3.22",
51
- "@xyo-network/module-model": "~5.3.22",
52
- "@xyo-network/module-resolver": "~5.3.22",
53
- "@xyo-network/module-wrapper": "~5.3.22",
54
- "@xyo-network/node-model": "~5.3.22",
55
- "@xyo-network/node-wrapper": "~5.3.22",
56
- "@xyo-network/payload-builder": "~5.3.22",
57
- "@xyo-network/payload-model": "~5.3.22",
58
- "@xyo-network/query-payload-plugin": "~5.3.22",
59
- "@xyo-network/sentinel-model": "~5.3.22",
60
- "@xyo-network/sentinel-wrapper": "~5.3.22",
61
- "@xyo-network/witness-model": "~5.3.22",
62
- "@xyo-network/witness-wrapper": "~5.3.22",
63
- "lru-cache": "~11.2.7"
39
+ "lru-cache": "~11.2.7",
40
+ "@xyo-network/account-model": "~5.3.24",
41
+ "@xyo-network/archivist-model": "~5.3.24",
42
+ "@xyo-network/boundwitness-model": "~5.3.24",
43
+ "@xyo-network/archivist-wrapper": "~5.3.24",
44
+ "@xyo-network/boundwitness-wrapper": "~5.3.24",
45
+ "@xyo-network/bridge-model": "~5.3.24",
46
+ "@xyo-network/manifest-model": "~5.3.24",
47
+ "@xyo-network/module-resolver": "~5.3.24",
48
+ "@xyo-network/diviner-model": "~5.3.24",
49
+ "@xyo-network/module-model": "~5.3.24",
50
+ "@xyo-network/account": "~5.3.24",
51
+ "@xyo-network/module-wrapper": "~5.3.24",
52
+ "@xyo-network/node-model": "~5.3.24",
53
+ "@xyo-network/diviner-wrapper": "~5.3.24",
54
+ "@xyo-network/payload-model": "~5.3.24",
55
+ "@xyo-network/module-abstract": "~5.3.24",
56
+ "@xyo-network/payload-builder": "~5.3.24",
57
+ "@xyo-network/node-wrapper": "~5.3.24",
58
+ "@xyo-network/query-payload-plugin": "~5.3.24",
59
+ "@xyo-network/sentinel-model": "~5.3.24",
60
+ "@xyo-network/witness-wrapper": "~5.3.24",
61
+ "@xyo-network/sentinel-wrapper": "~5.3.24",
62
+ "@xyo-network/witness-model": "~5.3.24"
64
63
  },
65
64
  "devDependencies": {
66
65
  "@opentelemetry/api": "^1.9.1",
67
66
  "@types/node": "^25.5.0",
68
- "@xylabs/sdk-js": "^5.0.91",
69
- "@xylabs/ts-scripts-common": "~7.6.8",
70
- "@xylabs/ts-scripts-yarn3": "~7.6.8",
71
- "@xylabs/tsconfig": "~7.6.8",
72
- "@xyo-network/account": "~5.3.22",
73
- "@xyo-network/account-model": "~5.3.22",
74
- "@xyo-network/archivist-model": "~5.3.22",
75
- "@xyo-network/archivist-wrapper": "~5.3.22",
76
- "@xyo-network/boundwitness-model": "~5.3.22",
77
- "@xyo-network/boundwitness-wrapper": "~5.3.22",
78
- "@xyo-network/bridge-model": "~5.3.22",
79
- "@xyo-network/diviner-model": "~5.3.22",
80
- "@xyo-network/diviner-wrapper": "~5.3.22",
81
- "@xyo-network/manifest-model": "~5.3.22",
82
- "@xyo-network/module-abstract": "~5.3.22",
83
- "@xyo-network/module-model": "~5.3.22",
84
- "@xyo-network/module-resolver": "~5.3.22",
85
- "@xyo-network/module-wrapper": "~5.3.22",
86
- "@xyo-network/node-model": "~5.3.22",
87
- "@xyo-network/node-wrapper": "~5.3.22",
88
- "@xyo-network/payload-builder": "~5.3.22",
89
- "@xyo-network/payload-model": "~5.3.22",
90
- "@xyo-network/query-payload-plugin": "~5.3.22",
91
- "@xyo-network/sentinel-model": "~5.3.22",
92
- "@xyo-network/sentinel-wrapper": "~5.3.22",
93
- "@xyo-network/witness-model": "~5.3.22",
94
- "@xyo-network/witness-wrapper": "~5.3.22",
67
+ "@xylabs/sdk-js": "^5.0.93",
68
+ "@xylabs/ts-scripts-common": "~7.6.16",
69
+ "@xylabs/ts-scripts-pnpm": "~7.6.16",
70
+ "@xylabs/tsconfig": "~7.6.16",
95
71
  "acorn": "^8.16.0",
96
72
  "axios": "^1.14.0",
97
- "cosmiconfig": "^9.0.1",
98
- "esbuild": "^0.27.4",
99
- "eslint": "^10.1.0",
73
+ "esbuild": "^0.28.0",
100
74
  "ethers": "^6.16.0",
101
- "lru-cache": "^11.2.7",
102
- "rollup": "^4.60.1",
75
+ "lru-cache": "~11.2.7",
103
76
  "tslib": "^2.8.1",
104
77
  "typescript": "~5.9.3",
105
- "zod": "^4.3.6"
78
+ "zod": "^4.3.6",
79
+ "@xyo-network/account": "~5.3.24",
80
+ "@xyo-network/account-model": "~5.3.24",
81
+ "@xyo-network/archivist-wrapper": "~5.3.24",
82
+ "@xyo-network/boundwitness-model": "~5.3.24",
83
+ "@xyo-network/archivist-model": "~5.3.24",
84
+ "@xyo-network/boundwitness-wrapper": "~5.3.24",
85
+ "@xyo-network/bridge-model": "~5.3.24",
86
+ "@xyo-network/diviner-model": "~5.3.24",
87
+ "@xyo-network/manifest-model": "~5.3.24",
88
+ "@xyo-network/module-resolver": "~5.3.24",
89
+ "@xyo-network/node-model": "~5.3.24",
90
+ "@xyo-network/node-wrapper": "~5.3.24",
91
+ "@xyo-network/module-wrapper": "~5.3.24",
92
+ "@xyo-network/payload-builder": "~5.3.24",
93
+ "@xyo-network/module-abstract": "~5.3.24",
94
+ "@xyo-network/diviner-wrapper": "~5.3.24",
95
+ "@xyo-network/payload-model": "~5.3.24",
96
+ "@xyo-network/query-payload-plugin": "~5.3.24",
97
+ "@xyo-network/sentinel-model": "~5.3.24",
98
+ "@xyo-network/sentinel-wrapper": "~5.3.24",
99
+ "@xyo-network/witness-model": "~5.3.24",
100
+ "@xyo-network/module-model": "~5.3.24",
101
+ "@xyo-network/witness-wrapper": "~5.3.24"
106
102
  },
107
103
  "peerDependencies": {
108
104
  "@xylabs/sdk-js": "^5",
@@ -113,4 +109,4 @@
113
109
  "publishConfig": {
114
110
  "access": "public"
115
111
  }
116
- }
112
+ }
@@ -1,213 +0,0 @@
1
- import type { Address, Promisable } from '@xylabs/sdk-js'
2
- import {
3
- assertEx, exists, forget,
4
- globallyUnique,
5
- } from '@xylabs/sdk-js'
6
- import type { ArchivistInstance } from '@xyo-network/archivist-model'
7
- import { asArchivistInstance } from '@xyo-network/archivist-model'
8
- import type { QueryBoundWitness } from '@xyo-network/boundwitness-model'
9
- import { QueryBoundWitnessWrapper } from '@xyo-network/boundwitness-wrapper'
10
- import type {
11
- AttachableBridgeInstance,
12
- BridgeExposeOptions,
13
- BridgeModuleEventData,
14
- BridgeParams,
15
- BridgeQueries,
16
- BridgeUnexposeOptions,
17
- ModuleFilterPayload,
18
- } from '@xyo-network/bridge-model'
19
- import {
20
- BridgeConfigSchema,
21
- BridgeConnectQuerySchema,
22
- BridgeDisconnectQuerySchema,
23
- BridgeExposeQuerySchema,
24
- BridgeUnexposeQuerySchema,
25
- ModuleFilterPayloadSchema,
26
- } from '@xyo-network/bridge-model'
27
- import { AbstractModuleInstance } from '@xyo-network/module-abstract'
28
- import type {
29
- AddressPayload,
30
- ModuleFilterOptions,
31
- ModuleIdentifier,
32
- ModuleInstance,
33
- ModuleQueryHandlerResult,
34
- ModuleResolverInstance,
35
- } from '@xyo-network/module-model'
36
- import {
37
- AddressSchema,
38
- resolveAddressToInstance,
39
- resolvePathToAddress,
40
- transformModuleIdentifier,
41
- } from '@xyo-network/module-model'
42
- import type { Payload, Schema } from '@xyo-network/payload-model'
43
- import { isPayloadOfSchemaType } from '@xyo-network/payload-model'
44
-
45
- export abstract class AbstractBridge<TParams extends BridgeParams = BridgeParams>
46
- extends AbstractModuleInstance<TParams, BridgeModuleEventData>
47
- implements AttachableBridgeInstance<TParams, BridgeModuleEventData> {
48
- static override readonly configSchemas: Schema[] = [...super.configSchemas, BridgeConfigSchema]
49
- static override readonly defaultConfigSchema: Schema = BridgeConfigSchema
50
- static override readonly uniqueName = globallyUnique('AbstractBridge', AbstractBridge, 'xyo')
51
-
52
- protected _roots?: ModuleInstance[]
53
-
54
- override get allowNameResolution() {
55
- // we default to false here to prevent name collisions
56
- return this.params.allowNameResolution ?? true
57
- }
58
-
59
- get discoverRoots() {
60
- return this.config.client?.discoverRoots ?? (this.config.client === undefined ? false : 'start')
61
- }
62
-
63
- override get queries(): Schema[] {
64
- return [BridgeConnectQuerySchema, BridgeDisconnectQuerySchema, BridgeExposeQuerySchema, BridgeUnexposeQuerySchema, ...super.queries]
65
- }
66
-
67
- get resolver(): Promisable<ModuleResolverInstance> {
68
- return assertEx(this.params.resolver, () => 'No resolver provided')
69
- }
70
-
71
- async expose(id: ModuleIdentifier, options?: BridgeExposeOptions | undefined): Promise<ModuleInstance[]> {
72
- this._noOverride('expose')
73
- assertEx(id !== '*', () => "Exposing '*' not supported")
74
- const addressToExpose = assertEx(await resolvePathToAddress(this, id), () => `Module to expose not found [${id}]`)
75
- console.log(`expose: ${addressToExpose}`)
76
- const modules = await this.exposeHandler(addressToExpose, options)
77
- await this.emit('exposed', { mod: this, modules })
78
- return modules
79
- }
80
-
81
- async exposed(): Promise<Address[]> {
82
- this._noOverride('exposed')
83
- return await this.exposedHandler()
84
- }
85
-
86
- /** @deprecated do not pass undefined. If trying to get all, pass '*' */
87
- override async resolve(): Promise<ModuleInstance[]>
88
- override async resolve<T extends ModuleInstance = ModuleInstance>(all: '*', options?: ModuleFilterOptions<T>): Promise<T[]>
89
- override async resolve<T extends ModuleInstance = ModuleInstance>(id: ModuleIdentifier, options?: ModuleFilterOptions<T>): Promise<T | undefined>
90
-
91
- override async resolve<T extends ModuleInstance = ModuleInstance>(
92
- id: ModuleIdentifier = '*',
93
- options: ModuleFilterOptions<T> = {},
94
- ): Promise<T | T[] | undefined> {
95
- const roots = (this._roots ?? []) as T[]
96
- const workingSet = (options.direction === 'up' ? [this as ModuleInstance] : [...roots, this]) as T[]
97
- if (id === '*') {
98
- const remainingDepth = (options.maxDepth ?? 5) - 1
99
- return remainingDepth <= 0
100
- ? workingSet
101
- : (
102
- [...workingSet, ...(await Promise.all(roots.map(mod => mod.resolve('*', { ...options, maxDepth: remainingDepth })))).flat()]
103
- )
104
- }
105
- switch (typeof id) {
106
- case 'string': {
107
- const parts = id.split(':')
108
- const first = assertEx(parts.shift(), () => 'Missing module identifier')
109
- const firstId = await transformModuleIdentifier(first, this.moduleIdentifierTransformers)
110
- const result = workingSet.find((mod) => {
111
- return firstId === mod.address || firstId === mod.modName
112
- })
113
- return parts.length === 0 ? result : result?.resolve(parts.join(':'), options)
114
- }
115
- default: {
116
- return
117
- }
118
- }
119
- }
120
-
121
- override async startHandler() {
122
- if (this.discoverRoots === 'lazy') {
123
- forget(this.getRoots())
124
- } else if (this.discoverRoots === 'start') {
125
- await this.getRoots()
126
- }
127
- }
128
-
129
- async unexpose(id: ModuleIdentifier, options?: BridgeUnexposeOptions | undefined): Promise<Address[]> {
130
- this._noOverride('unexpose')
131
- const addressToUnexpose = assertEx(await resolvePathToAddress(this, id), () => `Module to unexpose not found [${id}]`)
132
- const modules = await this.unexposeHandler(addressToUnexpose, options)
133
- await this.emit('unexposed', { mod: this, modules })
134
- return modules.map(mod => mod.address)
135
- }
136
-
137
- protected override async queryHandler<T extends QueryBoundWitness = QueryBoundWitness>(
138
- query: T,
139
- payloads?: Payload[],
140
- ): Promise<ModuleQueryHandlerResult> {
141
- const wrapper = QueryBoundWitnessWrapper.parseQuery<BridgeQueries>(query, payloads)
142
- const queryPayload = await wrapper.getQuery()
143
- const resultPayloads: Payload[] = []
144
-
145
- switch (queryPayload.schema) {
146
- case BridgeExposeQuerySchema: {
147
- const filterPayloads = (payloads ?? []).filter(isPayloadOfSchemaType<ModuleFilterPayload>(ModuleFilterPayloadSchema))
148
- assertEx(filterPayloads, () => 'At least one filter is required')
149
-
150
- await Promise.all(
151
- filterPayloads.map(async (filter) => {
152
- const { id, ...options } = filter
153
- const modules = await this.expose(id, options)
154
- for (const mod of modules) {
155
- const addressPayload: AddressPayload = {
156
- address: mod.address,
157
- schema: AddressSchema,
158
- }
159
- resultPayloads.push(addressPayload)
160
- }
161
- }),
162
- )
163
- break
164
- }
165
- case BridgeUnexposeQuerySchema: {
166
- const filterPayloads = (payloads ?? []).filter(isPayloadOfSchemaType<ModuleFilterPayload>(ModuleFilterPayloadSchema))
167
- assertEx(filterPayloads, () => 'At least one filter is required')
168
-
169
- await Promise.all(
170
- filterPayloads.map(async (filter) => {
171
- const { id, ...options } = filter
172
- const moduleAddresses = await this.unexpose(id, options)
173
- for (const address of moduleAddresses) {
174
- const addressPayload: AddressPayload = {
175
- address,
176
- schema: AddressSchema,
177
- }
178
- resultPayloads.push(addressPayload)
179
- }
180
- }),
181
- )
182
- break
183
- }
184
- default: {
185
- return await super.queryHandler(query, payloads)
186
- }
187
- }
188
- return resultPayloads
189
- }
190
-
191
- protected override async resolveArchivingArchivists(): Promise<ArchivistInstance[]> {
192
- const archivists = this.archiving?.archivists
193
- if (!archivists) return []
194
- const resolvedAddresses = (
195
- await Promise.all(
196
- archivists.map(async archivist =>
197
- (await Promise.all((await this.parents()).map(parent => resolvePathToAddress(parent, archivist)))).filter(exists)),
198
- )
199
- )
200
- .flat()
201
- .filter(exists)
202
- const resolved = (await Promise.all(resolvedAddresses.map(address => resolveAddressToInstance(this, address)))).filter(exists)
203
- return resolved.map(mod => asArchivistInstance(mod)).filter(exists)
204
- }
205
-
206
- abstract exposeHandler(address: Address, options?: BridgeExposeOptions | undefined): Promisable<ModuleInstance[]>
207
-
208
- abstract exposedHandler(): Promisable<Address[]>
209
-
210
- abstract getRoots(force?: boolean): Promise<ModuleInstance[]>
211
-
212
- abstract unexposeHandler(address: Address, options?: BridgeUnexposeOptions | undefined): Promisable<ModuleInstance[]>
213
- }
@@ -1,47 +0,0 @@
1
- import type { AccountInstance } from '@xyo-network/account-model'
2
- import type { ArchivistInstance } from '@xyo-network/archivist-model'
3
- import type {
4
- BridgeInstance, QuerySendFinishedEventArgs, QuerySendStartedEventArgs,
5
- } from '@xyo-network/bridge-model'
6
- import type {
7
- ArchivingModuleConfig,
8
- CacheConfig,
9
- ModuleFilterOptions,
10
- ModuleIdentifier,
11
- ModuleInstance,
12
- } from '@xyo-network/module-model'
13
- import { ObjectResolverPriority } from '@xyo-network/module-model'
14
- import type { ModuleResolverParams } from '@xyo-network/module-resolver'
15
- import { CompositeModuleResolver } from '@xyo-network/module-resolver'
16
-
17
- import type { ModuleProxyParams } from './AbstractModuleProxy/index.ts'
18
-
19
- export interface BridgeModuleResolverParams extends ModuleResolverParams {
20
- additionalSigners?: AccountInstance[]
21
- archiving?: ArchivingModuleConfig['archiving'] & { resolveArchivists: () => Promise<ArchivistInstance[]> }
22
- bridge: BridgeInstance
23
- cacheConfig?: CacheConfig
24
- onQuerySendFinished?: (args: Omit<QuerySendFinishedEventArgs, 'mod'>) => void
25
- onQuerySendStarted?: (args: Omit<QuerySendStartedEventArgs, 'mod'>) => void
26
- wrapperAccount: AccountInstance
27
- }
28
-
29
- export abstract class AbstractBridgeModuleResolver<
30
- TParams extends BridgeModuleResolverParams = BridgeModuleResolverParams,
31
- TProxyParams extends ModuleProxyParams = ModuleProxyParams,
32
- > extends CompositeModuleResolver<TParams> {
33
- override get priority() {
34
- return ObjectResolverPriority.VeryLow
35
- }
36
-
37
- override async resolveHandler<T extends ModuleInstance = ModuleInstance>(
38
- id: ModuleIdentifier,
39
- options?: ModuleFilterOptions<T>,
40
- _params?: Partial<TProxyParams>,
41
- ): Promise<T[]> {
42
- if (id === '*') {
43
- return []
44
- }
45
- return await super.resolveHandler(id, options)
46
- }
47
- }
@@ -1,309 +0,0 @@
1
- import type { Address } from '@xylabs/sdk-js'
2
- import {
3
- asAddress, assertEx,
4
- exists,
5
- forget,
6
- isAddress, toSafeJsonString,
7
- } from '@xylabs/sdk-js'
8
- import { Account } from '@xyo-network/account'
9
- import type { AccountInstance } from '@xyo-network/account-model'
10
- import type { ArchivistInstance } from '@xyo-network/archivist-model'
11
- import type { QueryBoundWitness } from '@xyo-network/boundwitness-model'
12
- import { BoundWitnessWrapper, QueryBoundWitnessWrapper } from '@xyo-network/boundwitness-wrapper'
13
- import type { QuerySendFinishedEventArgs, QuerySendStartedEventArgs } from '@xyo-network/bridge-model'
14
- import type { ModuleManifestPayload, NodeManifestPayload } from '@xyo-network/manifest-model'
15
- import { ModuleManifestPayloadSchema, NodeManifestPayloadSchema } from '@xyo-network/manifest-model'
16
- import { AbstractModuleInstance } from '@xyo-network/module-abstract'
17
- import type {
18
- AddressPreviousHashPayload,
19
- ArchivingModuleConfig,
20
- AttachableModuleInstance,
21
- ModuleAddressQuery,
22
- ModuleEventData,
23
- ModuleInstance,
24
- ModuleManifestQuery,
25
- ModuleName,
26
- ModuleQueryHandlerResult,
27
- ModuleQueryResult,
28
- ModuleResolver,
29
- QueryableModule,
30
- QueryableModuleParams,
31
- } from '@xyo-network/module-model'
32
- import {
33
- AddressPreviousHashSchema,
34
- DeadModuleError,
35
- ModuleAddressQuerySchema,
36
- ModuleConfigSchema,
37
- ModuleManifestQuerySchema,
38
- ModuleStateQuerySchema,
39
- } from '@xyo-network/module-model'
40
- import { ModuleWrapper } from '@xyo-network/module-wrapper'
41
- import { PayloadBuilder } from '@xyo-network/payload-builder'
42
- import type {
43
- ModuleError, Payload, Schema,
44
- } from '@xyo-network/payload-model'
45
- import {
46
- isPayloadOfSchemaType, isSchema, ModuleErrorSchema,
47
- } from '@xyo-network/payload-model'
48
- import type { QueryPayload } from '@xyo-network/query-payload-plugin'
49
- import { QuerySchema } from '@xyo-network/query-payload-plugin'
50
- import { LRUCache } from 'lru-cache'
51
-
52
- import { ModuleProxyResolver } from './ModuleProxyResolver.ts'
53
-
54
- export interface ModuleProxyParams extends QueryableModuleParams
55
- {
56
- account: AccountInstance
57
- archiving?: ArchivingModuleConfig['archiving'] & { resolveArchivists: () => Promise<ArchivistInstance[]> }
58
- host: ModuleResolver
59
- manifest?: ModuleManifestPayload
60
- moduleAddress: Address
61
- onQuerySendFinished?: (args: Omit<QuerySendFinishedEventArgs, 'mod'>) => void
62
- onQuerySendStarted?: (args: Omit<QuerySendStartedEventArgs, 'mod'>) => void
63
- state?: Payload[]
64
- }
65
-
66
- export abstract class AbstractModuleProxy<
67
- TWrappedModule extends ModuleInstance = ModuleInstance,
68
- TParams extends ModuleProxyParams = ModuleProxyParams,
69
- >
70
- extends AbstractModuleInstance<TParams, TWrappedModule['eventData']>
71
- implements ModuleInstance<TParams, TWrappedModule['eventData']> {
72
- static readonly requiredQueries: string[] = [ModuleStateQuerySchema]
73
-
74
- protected _config?: TWrappedModule['config']
75
- protected _publicChildren?: ModuleInstance[]
76
- protected _state: Payload[] | undefined = undefined
77
- protected _stateInProcess = false
78
-
79
- private _spamTrap = new LRUCache<string, number>({
80
- max: 1000, ttl: 1000 * 60, ttlAutopurge: true,
81
- })
82
-
83
- override get address() {
84
- return this.params.moduleAddress
85
- }
86
-
87
- override get archiving(): ArchivingModuleConfig['archiving'] | undefined {
88
- return this.params?.archiving
89
- }
90
-
91
- override get config(): TWrappedModule['config'] & { schema: Schema } {
92
- return { ...this._config, schema: (this._config?.schema ?? ModuleConfigSchema) }
93
- }
94
-
95
- override get queries(): Schema[] {
96
- const queryPayloads = assertEx(this._state, () => 'Module state not found. Make sure proxy has been started').filter(item =>
97
- isPayloadOfSchemaType<QueryPayload>(QuerySchema)(item)) as QueryPayload[]
98
- return queryPayloads.map(payload => payload.query)
99
- }
100
-
101
- static hasRequiredQueries(mod: QueryableModule) {
102
- return this.missingRequiredQueries(mod).length === 0
103
- }
104
-
105
- static override isAllowedSchema(schema: Schema): boolean {
106
- return isSchema(schema)
107
- }
108
-
109
- static missingRequiredQueries(mod: QueryableModule): string[] {
110
- const moduleQueries = mod.queries
111
- return (
112
- this.requiredQueries.map((query) => {
113
- return moduleQueries.includes(query) ? null : query
114
- })
115
- ).filter(exists)
116
- }
117
-
118
- static override async paramsHandler<T extends AttachableModuleInstance<QueryableModuleParams, ModuleEventData>>(
119
- inParams?: Partial<T['params']>,
120
- ): Promise<T['params']> {
121
- const superParams = await super.paramsHandler(inParams)
122
- return {
123
- ...superParams,
124
- account: (superParams.account === 'random') ? await Account.random() : superParams.account,
125
- addToResolvers: superParams.addToResolvers ?? true,
126
- }
127
- }
128
-
129
- async addressPreviousHash(): Promise<AddressPreviousHashPayload> {
130
- const queryPayload: ModuleAddressQuery = { schema: ModuleAddressQuerySchema }
131
- const result: AddressPreviousHashPayload = assertEx(
132
- (await this.sendQuery(queryPayload, undefined, this.account)).find(
133
- isPayloadOfSchemaType<AddressPreviousHashPayload>(AddressPreviousHashSchema),
134
- ) as AddressPreviousHashPayload,
135
- () => 'Result did not include correct payload',
136
- )
137
- return result
138
- }
139
-
140
- childAddressByName(name: ModuleName): Address | undefined {
141
- const nodeManifests = this._state?.filter(isPayloadOfSchemaType<NodeManifestPayload>(NodeManifestPayloadSchema))
142
- const childPairs = nodeManifests?.flatMap(nodeManifest => Object.entries(nodeManifest.status?.children ?? {}) as [Address, ModuleName | null][])
143
- return asAddress(childPairs?.find(([_, childName]) => childName === name)?.[0])
144
- }
145
-
146
- async childAddressMap(): Promise<Record<Address, ModuleName | null>> {
147
- let nodeManifests: NodeManifestPayload[] | undefined
148
- = isPayloadOfSchemaType<NodeManifestPayload>(NodeManifestPayloadSchema)(this.params.manifest) ? [this.params.manifest] : undefined
149
- const result: Record<Address, ModuleName | null> = {}
150
- if (nodeManifests === undefined) {
151
- const state = await this.state()
152
- nodeManifests = state.filter(isPayloadOfSchemaType<NodeManifestPayload>(NodeManifestPayloadSchema))
153
- }
154
- for (const manifest of nodeManifests ?? []) {
155
- const children = manifest.modules?.public ?? []
156
- for (const child of children) {
157
- if (typeof child === 'object') {
158
- const address = child.status?.address
159
- if (isAddress(address)) {
160
- result[address] = child.config.name ?? null
161
- }
162
- }
163
- }
164
- }
165
- return result
166
- }
167
-
168
- override async createHandler(): Promise<void> {
169
- await super.createHandler()
170
- let manifest: ModuleManifestPayload | NodeManifestPayload | undefined = this.params.manifest
171
- if (!manifest) {
172
- const state = await this.state()
173
- const manifestPayload = state.find(
174
- payload => isPayloadOfSchemaType<NodeManifestPayload>(NodeManifestPayloadSchema)(payload)
175
- || isPayloadOfSchemaType<ModuleManifestPayload>(ModuleManifestPayloadSchema)(payload),
176
- )
177
- manifest = assertEx(manifestPayload, () => "Can't find manifest payload")
178
- }
179
- this.setConfig({ ...manifest.config })
180
- this.downResolver.addResolver(
181
- new ModuleProxyResolver({
182
- childAddressMap: await this.childAddressMap(),
183
- host: this.params.host,
184
- mod: this,
185
- moduleIdentifierTransformers: this.params.moduleIdentifierTransformers,
186
- }),
187
- )
188
- }
189
-
190
- override async manifest(maxDepth?: number): Promise<ModuleManifestPayload> {
191
- const queryPayload: ModuleManifestQuery = { schema: ModuleManifestQuerySchema, ...(maxDepth === undefined ? {} : { maxDepth }) }
192
- return (await this.sendQuery(queryPayload))[0] as ModuleManifestPayload
193
- }
194
-
195
- override async moduleAddress(): Promise<AddressPreviousHashPayload[]> {
196
- const queryPayload: ModuleAddressQuery = { schema: ModuleAddressQuerySchema }
197
- return (await this.sendQuery(queryPayload)) as AddressPreviousHashPayload[]
198
- }
199
-
200
- override async previousHash(): Promise<string | undefined> {
201
- const queryPayload: ModuleAddressQuery = { schema: ModuleAddressQuerySchema }
202
- return ((await this.sendQuery(queryPayload)).pop() as AddressPreviousHashPayload).previousHash
203
- }
204
-
205
- override async publicChildren() {
206
- this._publicChildren = this._publicChildren ?? (await super.publicChildren())
207
- return this._publicChildren
208
- }
209
-
210
- override async query<T extends QueryBoundWitness = QueryBoundWitness>(query: T, payloads?: Payload[]): Promise<ModuleQueryResult> {
211
- this._checkDead()
212
- return await this.busy(async () => {
213
- try {
214
- await this.checkSpam(query)
215
- if (this.archiving && this.isAllowedArchivingQuery(query.schema)) {
216
- forget(this.storeToArchivists([query, ...(payloads ?? [])]))
217
- }
218
- this.params.onQuerySendStarted?.({ payloads, query })
219
- const result = await this.proxyQueryHandler<T>(query, payloads)
220
- this.params.onQuerySendFinished?.({
221
- payloads, query, result, status: 'success',
222
- })
223
- if (this.archiving && this.isAllowedArchivingQuery(query.schema)) {
224
- forget(this.storeToArchivists(result.flat()))
225
- }
226
- forget(this.emit('moduleQueried', {
227
- mod: this, payloads, query, result,
228
- }))
229
- return result
230
- } catch (ex) {
231
- this.params.onQuerySendFinished?.({
232
- payloads, query, status: 'failure',
233
- })
234
- const error = ex as Error
235
- this._lastError = error
236
- // this.status = 'dead'
237
- const deadError = new DeadModuleError(this.address, error)
238
- const errorPayload: ModuleError = {
239
- message: deadError.message,
240
- name: deadError.name,
241
- schema: ModuleErrorSchema,
242
- }
243
- const sourceQuery = assertEx(QueryBoundWitnessWrapper.unwrap(query), () => 'Invalid query')
244
- return await this.bindQueryResult(sourceQuery, [], undefined, [errorPayload])
245
- }
246
- })
247
- }
248
-
249
- override queryHandler<T extends QueryBoundWitness = QueryBoundWitness>(
250
- _query: T,
251
- _payloads?: Payload[],
252
- _queryConfig?: TWrappedModule['params']['config'],
253
- ): Promise<ModuleQueryHandlerResult> {
254
- throw new Error('queryHandler should never be called')
255
- }
256
-
257
- override async queryable<T extends QueryBoundWitness = QueryBoundWitness>(
258
- _query: T,
259
- _payloads?: Payload[],
260
- _queryConfig?: TWrappedModule['params']['config'],
261
- ): Promise<boolean> {
262
- return await Promise.resolve(true)
263
- }
264
-
265
- override async resolveArchivingArchivists(): Promise<ArchivistInstance[]> {
266
- return (await this.params.archiving?.resolveArchivists()) ?? []
267
- }
268
-
269
- setConfig(config: TWrappedModule['params']['config']) {
270
- this._config = config
271
- }
272
-
273
- setState(state: Payload[]) {
274
- this._state = state
275
- }
276
-
277
- override async state(): Promise<Payload[]> {
278
- if (this._state === undefined) {
279
- // temporarily add ModuleStateQuerySchema to the schema list so we can wrap it and get the real query list
280
- const stateQueryPayload: QueryPayload = { query: ModuleStateQuerySchema, schema: QuerySchema }
281
- const manifestQueryPayload: QueryPayload = { query: ModuleManifestQuerySchema, schema: QuerySchema }
282
- this._state = [stateQueryPayload, manifestQueryPayload]
283
- const wrapper = ModuleWrapper.wrap(this, await Account.random())
284
- this._state = await wrapper.state()
285
- }
286
- return this._state
287
- }
288
-
289
- protected filterErrors(result: ModuleQueryResult): ModuleError[] {
290
- const wrapper = BoundWitnessWrapper.wrap(result[0], result[1])
291
- return wrapper.payloadsBySchema<ModuleError>(ModuleErrorSchema)
292
- }
293
-
294
- protected override async startHandler(): Promise<void> {
295
- return await super.startHandler()
296
- }
297
-
298
- // this checks and warns if we are getting spammed by the same query
299
- private async checkSpam(query: QueryBoundWitness) {
300
- const hash = await PayloadBuilder.hash(query)
301
- const previousCount = this._spamTrap.get(hash) ?? 0
302
- if (previousCount > 0) {
303
- this.logger?.warn(`Spam trap triggered for query: ${hash} from ${toSafeJsonString(query.addresses)}`)
304
- }
305
- this._spamTrap.set(hash, previousCount + 1)
306
- }
307
-
308
- abstract proxyQueryHandler<T extends QueryBoundWitness = QueryBoundWitness>(_query: T, _payloads?: Payload[]): Promise<ModuleQueryResult>
309
- }
@@ -1,156 +0,0 @@
1
- import type { Address, Promisable } from '@xylabs/sdk-js'
2
- import { assertEx, exists } from '@xylabs/sdk-js'
3
- import { Account } from '@xyo-network/account'
4
- import type { ModuleManifestPayload } from '@xyo-network/manifest-model'
5
- import type {
6
- ModuleFilterOptions,
7
- ModuleIdentifier,
8
- ModuleIdentifierTransformer,
9
- ModuleInstance,
10
- ModuleName,
11
- ModuleResolver,
12
- ModuleResolverInstance,
13
- ObjectFilterOptions,
14
- } from '@xyo-network/module-model'
15
- import { ObjectResolverPriority } from '@xyo-network/module-model'
16
- import { CompositeModuleResolver } from '@xyo-network/module-resolver'
17
-
18
- import { wrapModuleWithType } from '../wrapModuleWithType.ts'
19
-
20
- export interface ModuleProxyResolverOptions {
21
- childAddressMap: Record<Address, ModuleName | null>
22
- childManifests?: Record<Address, ModuleManifestPayload>
23
- childStates?: Record<Address, ModuleManifestPayload>
24
- host: ModuleResolver
25
- mod: ModuleInstance
26
- moduleIdentifierTransformers?: ModuleIdentifierTransformer[]
27
- }
28
-
29
- export class ModuleProxyResolver<T extends ModuleProxyResolverOptions = ModuleProxyResolverOptions> implements ModuleResolverInstance {
30
- private downResolver: CompositeModuleResolver
31
- private options: T
32
-
33
- constructor(options: T) {
34
- this.options = options
35
- this.downResolver = new CompositeModuleResolver({ moduleIdentifierTransformers: options.moduleIdentifierTransformers, root: this.root })
36
- }
37
-
38
- get priority() {
39
- return ObjectResolverPriority.VeryLow
40
- }
41
-
42
- get root() {
43
- return this.options.mod
44
- }
45
-
46
- protected get childAddressMap() {
47
- return this.options.childAddressMap
48
- }
49
-
50
- protected get host() {
51
- return this.options.host
52
- }
53
-
54
- protected get mod() {
55
- return this.options.mod
56
- }
57
-
58
- addResolver(_resolver: ModuleResolver): this {
59
- throw new Error('Not supported')
60
- }
61
-
62
- childManifest(address: Address) {
63
- return this.options.childManifests?.[address]
64
- }
65
-
66
- childState(address: Address) {
67
- return this.options.childStates?.[address]
68
- }
69
-
70
- removeResolver(_resolver: ModuleResolver): this {
71
- throw new Error('Not supported')
72
- }
73
-
74
- /** @deprecated do not pass undefined. If trying to get all, pass '*' */
75
- async resolve(): Promise<ModuleInstance[]>
76
- async resolve<T extends ModuleInstance = ModuleInstance>(all: '*', options?: ModuleFilterOptions<T>): Promise<T[]>
77
- async resolve<T extends ModuleInstance = ModuleInstance>(id: ModuleIdentifier, options?: ModuleFilterOptions<T>): Promise<T | undefined>
78
- // eslint-disable-next-line complexity
79
- async resolve<T extends ModuleInstance = ModuleInstance>(
80
- id: ModuleIdentifier = '*',
81
- options?: ModuleFilterOptions<T>,
82
- ): Promise<T | T[] | undefined> {
83
- // console.log(`childAddressMap: ${toSafeJsonString(this.childAddressMap, 10)}`)
84
- const direction = options?.direction ?? 'all'
85
- if (id === '*') {
86
- // get all the child addresses. if they have been resolved before, they should be in downResolver
87
- const childAddresses = Object.keys(this.childAddressMap)
88
- const resolvedChildren = await Promise.all(childAddresses.map<Promise<T | undefined>>(address => this.resolve<T>(address, options)))
89
- return resolvedChildren.filter(exists)
90
- } else if (typeof id === 'string') {
91
- const idParts = id.split(':')
92
- const firstPart: ModuleIdentifier = assertEx(idParts.shift(), () => 'Invalid module identifier at first position')
93
- const firstPartAddress = await this.resolveIdentifier(firstPart)
94
- if (firstPartAddress) {
95
- const remainingParts = idParts.length > 0 ? idParts.join(':') : undefined
96
- if (direction === 'down' || direction === 'all') {
97
- const downResolverModule = await this.downResolver.resolve<T>(firstPartAddress)
98
- if (downResolverModule) {
99
- return remainingParts ? downResolverModule.resolve(remainingParts, options) : downResolverModule
100
- }
101
-
102
- console.log(`ModuleProxyResolver: ${firstPartAddress} | ${this.root.address}`)
103
- if (firstPartAddress === this.root.address) {
104
- const wrapped = wrapModuleWithType(this.root, await Account.random()) as unknown as T
105
- return remainingParts ? wrapped?.resolve(remainingParts, options) : wrapped
106
- }
107
-
108
- // if it is a known child, create a proxy
109
- const addressToProxy
110
- = Object.keys(this.childAddressMap).includes(firstPartAddress as Address)
111
- ? (firstPartAddress as Address)
112
- : (Object.entries(this.childAddressMap).find(([_, value]) => value === firstPartAddress)?.[0] as Address | undefined)
113
- if (addressToProxy) {
114
- const proxy = await this.host.resolve(addressToProxy, { ...options, direction: 'down' })
115
- if (proxy) {
116
- const wrapped = wrapModuleWithType(proxy, await Account.random()) as unknown as T
117
- return remainingParts ? wrapped?.resolve(remainingParts, options) : wrapped
118
- }
119
- return
120
- }
121
- }
122
- }
123
- return
124
- }
125
- }
126
-
127
- resolveIdentifier(id: ModuleIdentifier, _options?: ObjectFilterOptions): Promisable<Address | undefined> {
128
- // check if any of the modules have the id as an address
129
- if (this.childAddressMap[id as Address]) {
130
- return id as Address
131
- }
132
-
133
- if (this.root.address === id) {
134
- return this.root.address
135
- }
136
-
137
- if (this.root.modName === id) {
138
- return this.root.address
139
- }
140
-
141
- // check if id is a name of one of modules in the resolver
142
- const addressFromName = Object.entries(this.childAddressMap).find(([, name]) => name === id)?.[0] as Address | undefined
143
- if (addressFromName) {
144
- return addressFromName
145
- }
146
- }
147
-
148
- async resolvePrivate<T extends ModuleInstance = ModuleInstance>(all: '*', options?: ObjectFilterOptions<T>): Promise<T[]>
149
- async resolvePrivate<T extends ModuleInstance = ModuleInstance>(id: ModuleIdentifier, options?: ObjectFilterOptions<T>): Promise<T | undefined>
150
- async resolvePrivate<T extends ModuleInstance = ModuleInstance>(
151
- id: ModuleIdentifier,
152
- _options?: ObjectFilterOptions<T>,
153
- ): Promise<T | T[] | undefined> {
154
- if (id === '*') return await Promise.resolve([])
155
- }
156
- }
@@ -1,2 +0,0 @@
1
- export * from './AbstractModuleProxy.ts'
2
- export * from './ModuleProxyResolver.ts'
package/src/index.ts DELETED
@@ -1,4 +0,0 @@
1
- export * from './AbstractBridge.ts'
2
- export * from './AbstractBridgeModuleResolver.ts'
3
- export * from './AbstractModuleProxy/index.ts'
4
- export * from './wrapModuleWithType.ts'
@@ -1,32 +0,0 @@
1
- import type { AccountInstance } from '@xyo-network/account-model'
2
- import { isArchivistModule } from '@xyo-network/archivist-model'
3
- import { ArchivistWrapper } from '@xyo-network/archivist-wrapper'
4
- import { isDivinerModule } from '@xyo-network/diviner-model'
5
- import { DivinerWrapper } from '@xyo-network/diviner-wrapper'
6
- import type { QueryableModule } from '@xyo-network/module-model'
7
- import type { ModuleWrapper } from '@xyo-network/module-wrapper'
8
- import { isNodeModule } from '@xyo-network/node-model'
9
- import { NodeWrapper } from '@xyo-network/node-wrapper'
10
- import { isSentinelModule } from '@xyo-network/sentinel-model'
11
- import { SentinelWrapper } from '@xyo-network/sentinel-wrapper'
12
- import { isWitnessModule } from '@xyo-network/witness-model'
13
- import { WitnessWrapper } from '@xyo-network/witness-wrapper'
14
-
15
- export const wrapModuleWithType = (mod: QueryableModule, account: AccountInstance): ModuleWrapper => {
16
- if (isArchivistModule(mod)) {
17
- return ArchivistWrapper.wrap(mod, account)
18
- }
19
- if (isDivinerModule(mod)) {
20
- return DivinerWrapper.wrap(mod, account)
21
- }
22
- if (isNodeModule(mod)) {
23
- return NodeWrapper.wrap(mod, account)
24
- }
25
- if (isSentinelModule(mod)) {
26
- return SentinelWrapper.wrap(mod, account)
27
- }
28
- if (isWitnessModule(mod)) {
29
- return WitnessWrapper.wrap(mod, account)
30
- }
31
- throw 'Failed to wrap'
32
- }