@xyo-network/node-memory 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/node-memory",
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,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/module-model": "~5.3.22",
41
- "@xyo-network/node-abstract": "~5.3.22",
42
- "@xyo-network/node-model": "~5.3.22",
43
- "async-mutex": "~0.5.0"
39
+ "async-mutex": "~0.5.0",
40
+ "@xyo-network/module-model": "~5.3.24",
41
+ "@xyo-network/node-abstract": "~5.3.24",
42
+ "@xyo-network/node-model": "~5.3.24"
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
- "@xyo-network/module-model": "~5.3.22",
53
- "@xyo-network/module-resolver": "~5.3.22",
54
- "@xyo-network/node-abstract": "~5.3.22",
55
- "@xyo-network/node-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",
56
51
  "acorn": "^8.16.0",
57
52
  "async-mutex": "~0.5.0",
58
53
  "axios": "^1.14.0",
59
- "cosmiconfig": "^9.0.1",
60
- "esbuild": "^0.27.4",
61
- "eslint": "^10.1.0",
54
+ "esbuild": "^0.28.0",
62
55
  "ethers": "^6.16.0",
63
- "rollup": "^4.60.1",
64
56
  "tslib": "^2.8.1",
65
57
  "typescript": "~5.9.3",
66
- "zod": "^4.3.6"
58
+ "zod": "^4.3.6",
59
+ "@xyo-network/module-model": "~5.3.24",
60
+ "@xyo-network/module-resolver": "~5.3.24",
61
+ "@xyo-network/node-abstract": "~5.3.24",
62
+ "@xyo-network/node-model": "~5.3.24"
67
63
  },
68
64
  "peerDependencies": {
69
65
  "@xylabs/sdk-js": "^5",
@@ -74,4 +70,4 @@
74
70
  "publishConfig": {
75
71
  "access": "public"
76
72
  }
77
- }
73
+ }
package/src/MemoryNode.ts DELETED
@@ -1,265 +0,0 @@
1
- import type {
2
- Address, EventListener, Promisable,
3
- } from '@xylabs/sdk-js'
4
- import {
5
- assertEx,
6
- exists, isAddress, isDefined,
7
- } from '@xylabs/sdk-js'
8
- import {
9
- type AnyConfigSchema,
10
- type AttachableModuleInstance,
11
- creatableModule,
12
- type ModuleIdentifier,
13
- type ModuleInstance,
14
- type ModuleResolverInstance,
15
- type QueryableModule,
16
- } from '@xyo-network/module-model'
17
- import type { CompositeModuleResolver } from '@xyo-network/module-resolver'
18
- import { AbstractNode } from '@xyo-network/node-abstract'
19
- import type {
20
- ChildCertificationFields,
21
- NodeConfig, NodeInstanceParams,
22
- NodeModuleEventData,
23
- } from '@xyo-network/node-model'
24
- import { isNodeModule } from '@xyo-network/node-model'
25
- import { Mutex } from 'async-mutex'
26
-
27
- export type MemoryNodeParams = NodeInstanceParams<AnyConfigSchema<NodeConfig>>
28
-
29
- @creatableModule()
30
- export class MemoryNode<TParams extends MemoryNodeParams = MemoryNodeParams, TEventData extends NodeModuleEventData = NodeModuleEventData>
31
- extends AbstractNode<TParams, TEventData> {
32
- protected registeredModuleMap: Partial<Record<Address, AttachableModuleInstance>> = {}
33
-
34
- private _attachMutex = new Mutex()
35
-
36
- private _attachedPrivateModules = new Set<Address>()
37
- private _attachedPublicModules = new Set<Address>()
38
-
39
- async attachHandler(id: ModuleIdentifier, external?: boolean) {
40
- await this.startedAsync('warn', true)
41
- return assertEx(
42
- isAddress(id) ? await this.attachUsingAddress(id as Address, external) : await this.attachUsingName(id, external),
43
- () => `Unable to locate module [${id}]`,
44
- )
45
- }
46
-
47
- async certifyHandler(id: ModuleIdentifier): Promise<ChildCertificationFields> {
48
- const child = (await this.publicChildren()).find(child => child.modName === id || child.address === id)
49
- if (child) {
50
- return { address: child.address, expiration: Date.now() + 1000 * 60 * 10 /* 10 minutes */ }
51
- }
52
- throw new Error(`Unable to certify child module [${id}]`)
53
- }
54
-
55
- async detachHandler(id: ModuleIdentifier) {
56
- return isAddress(id) ? await this.detachUsingAddress(id as Address) : await this.detachUsingName(id)
57
- }
58
-
59
- override async privateChildren(): Promise<ModuleInstance[]> {
60
- return [...(await this.attachedPrivateModules()), ...this.params.privateChildren ?? []]
61
- }
62
-
63
- override async publicChildren(): Promise<ModuleInstance[]> {
64
- return [...(await this.attachedPublicModules()), ...this.params.publicChildren ?? []]
65
- }
66
-
67
- async register(mod: AttachableModuleInstance) {
68
- this.started('warn')
69
- if (this.registeredModuleMap[mod.address]) {
70
- if (this.registeredModuleMap[mod.address] === mod) {
71
- this.logger?.warn(`Module already registered at that address[${mod.address}]|${mod.id}|[${mod.config.schema}]`)
72
- } else {
73
- throw new Error(`Other module already registered at that address[${mod.address}]|${mod.id}|[${mod.config.schema}]`)
74
- }
75
- }
76
- this.registeredModuleMap[mod.address] = mod
77
- const args = { mod, name: mod.modName }
78
- await this.emit('moduleRegistered', args)
79
- }
80
-
81
- registeredHandler(): Promisable<Address[]> {
82
- return Promise.resolve(
83
- (Object.keys(this.registeredModuleMap) as Address[]).map((key) => {
84
- return key
85
- }),
86
- )
87
- }
88
-
89
- registeredModules(): AttachableModuleInstance[] {
90
- return Object.values(this.registeredModuleMap).map((value) => {
91
- return value
92
- }).filter(exists)
93
- }
94
-
95
- async unregister(mod: ModuleInstance): Promise<ModuleInstance> {
96
- // try to detach if it is attached
97
- try {
98
- await this.detach(mod.address)
99
- } catch {}
100
- delete this.registeredModuleMap[mod.address]
101
- const args = { mod, name: mod.modName }
102
- await this.emit('moduleUnregistered', args)
103
- return this
104
- }
105
-
106
- protected async attachUsingAddress(address: Address, external?: boolean) {
107
- return await this._attachMutex.runExclusive(async () => {
108
- const existingModule = await this.resolve(address)
109
- if (existingModule) {
110
- this.logger?.warn(`MemoryNode: Module [${existingModule?.modName ?? existingModule?.address}] already attached at address [${address}]`)
111
- }
112
- const mod = assertEx(this.registeredModuleMap[address], () => `No Module Registered at address [${address}]`)
113
-
114
- if (this._attachedPublicModules.has(mod.address)) {
115
- this.logger?.warn(`Module [${mod.modName}] already attached at [${address}] (public)`)
116
- }
117
- if (this._attachedPrivateModules.has(mod.address)) {
118
- this.logger?.warn(`Module [${mod.modName}] already attached at [${address}] (private)`)
119
- }
120
-
121
- const notificationList = await this.getModulesToNotifyAbout(mod)
122
-
123
- // give it private access
124
- mod.upResolver.addResolver?.(this.privateResolver)
125
-
126
- // give it public access
127
- mod.upResolver.addResolver?.(this.downResolver as CompositeModuleResolver)
128
-
129
- // give it outside access
130
- mod.upResolver.addResolver?.(this.upResolver)
131
-
132
- if (external) {
133
- // expose it externally
134
- this._attachedPublicModules.add(mod.address)
135
- this.downResolver.addResolver(mod.downResolver as ModuleResolverInstance)
136
- } else {
137
- this._attachedPrivateModules.add(mod.address)
138
- this.privateResolver.addResolver(mod.downResolver as ModuleResolverInstance)
139
- }
140
-
141
- mod.addParent(this)
142
-
143
- const args = { mod, name: mod.modName }
144
- await this.emit('moduleAttached', args)
145
-
146
- if (isNodeModule(mod) && external) {
147
- const attachedListener: EventListener<TEventData['moduleAttached']> = async (args: TEventData['moduleAttached']) =>
148
- await this.emit('moduleAttached', args)
149
-
150
- const detachedListener: EventListener<TEventData['moduleDetached']> = async (args: TEventData['moduleDetached']) =>
151
- await this.emit('moduleDetached', args)
152
-
153
- mod.on('moduleAttached', attachedListener as EventListener)
154
- mod.on('moduleDetached', detachedListener as EventListener)
155
- }
156
-
157
- await this.notifyOfExistingModulesAttached(notificationList)
158
-
159
- return address
160
- })
161
- }
162
-
163
- protected async detachUsingAddress(address: Address) {
164
- const mod = assertEx(this.registeredModuleMap[address], () => `No Module Registered at address [${address}]`)
165
-
166
- const isAttachedPublic = this._attachedPublicModules.has(mod.address)
167
- const isAttachedPrivate = this._attachedPrivateModules.has(mod.address)
168
-
169
- assertEx(isAttachedPublic || isAttachedPrivate, () => `Module [${mod.modName}] not attached at [${address}]`)
170
-
171
- // remove inside access
172
- mod.upResolver?.removeResolver?.(this.privateResolver)
173
-
174
- // remove outside access
175
- mod.upResolver?.removeResolver?.(this.upResolver)
176
-
177
- // remove external exposure
178
- this.downResolver.removeResolver(mod.downResolver as ModuleResolverInstance)
179
-
180
- mod.removeParent(this.address)
181
-
182
- if (isAttachedPublic) {
183
- this._attachedPublicModules.delete(mod.address)
184
- }
185
-
186
- if (isAttachedPrivate) {
187
- this._attachedPrivateModules.delete(mod.address)
188
- }
189
-
190
- const args = { mod, name: mod.modName }
191
- await this.emit('moduleDetached', args)
192
-
193
- // notify of all sub node children detach
194
- if (isNodeModule(mod)) {
195
- const notificationList = await this.getModulesToNotifyAbout(mod)
196
- await this.notifyOfExistingModulesDetached(notificationList)
197
- }
198
- return address
199
- }
200
-
201
- protected override startHandler() {
202
- return super.startHandler()
203
- }
204
-
205
- private async attachUsingName(name: string, external?: boolean) {
206
- const address = this.registeredModuleAddressFromName(name)
207
- if (isDefined(address)) {
208
- return await this.attachUsingAddress(address, external)
209
- }
210
- }
211
-
212
- private async detachUsingName(name: string) {
213
- const address = this.registeredModuleAddressFromName(name)
214
- if (isDefined(address)) {
215
- return await this.detachUsingAddress(address)
216
- }
217
- }
218
-
219
- private async getModulesToNotifyAbout(node: ModuleInstance) {
220
- const notifiedAddresses: string[] = []
221
- // send attach events for all existing attached modules
222
- const childModules = await node.resolve('*', { direction: 'down' })
223
- return (
224
- childModules.map((child) => {
225
- // don't report self
226
- if (node.address === child.address) {
227
- return
228
- }
229
-
230
- // prevent loop
231
- if (notifiedAddresses.includes(child.address)) {
232
- return
233
- }
234
-
235
- notifiedAddresses.push(child.address)
236
-
237
- return child
238
- })
239
- ).filter(exists)
240
- }
241
-
242
- private async notifyOfExistingModulesAttached(childModules: QueryableModule[]) {
243
- await Promise.all(
244
- childModules.map(async (child) => {
245
- const args = { mod: child, name: child.modName }
246
- await this.emit('moduleAttached', args)
247
- }),
248
- )
249
- }
250
-
251
- private async notifyOfExistingModulesDetached(childModules: QueryableModule[]) {
252
- await Promise.all(
253
- childModules.map(async (child) => {
254
- const args = { mod: child, name: child.modName }
255
- await this.emit('moduleDetached', args)
256
- }),
257
- )
258
- }
259
-
260
- private registeredModuleAddressFromName(name: string) {
261
- return Object.values(this.registeredModuleMap).find((value) => {
262
- return value?.modName === name
263
- })?.address
264
- }
265
- }
@@ -1,25 +0,0 @@
1
- import { assertEx } from '@xylabs/sdk-js'
2
- import type { ModuleIdentifier } from '@xyo-network/module-model'
3
- import type { NodeInstance } from '@xyo-network/node-model'
4
- import { asNodeInstance } from '@xyo-network/node-model'
5
-
6
- import { MemoryNode } from '../MemoryNode.ts'
7
- import { flatAttachToExistingNode } from './flatAttachToExistingNode.ts'
8
-
9
- export const attachToExistingNode = async (source: NodeInstance, id: ModuleIdentifier, destination: NodeInstance): Promise<NodeInstance> => {
10
- const parts = id.split(':')
11
- const first = assertEx(parts.shift(), () => 'missing first part')
12
- if (parts.length === 0) {
13
- // this is an immediate child/children
14
- return await flatAttachToExistingNode(source, first, destination)
15
- } else {
16
- // all parts that are not the last should be nodes, build them and nest
17
- const firstModule = await source.resolve(first)
18
- const firstNode = asNodeInstance(firstModule, () => 'first part is not a node', { required: true })
19
- const newNode = await MemoryNode.create({ config: firstNode.config })
20
- await destination.register?.(newNode)
21
- await destination.attach(newNode.address, true)
22
- await attachToExistingNode(firstNode, parts.join(':'), newNode)
23
- return destination
24
- }
25
- }
@@ -1,19 +0,0 @@
1
- import type { CreatableName } from '@xylabs/sdk-js'
2
- import type { ModuleIdentifier } from '@xyo-network/module-model'
3
- import type { NodeInstance } from '@xyo-network/node-model'
4
- import { NodeConfigSchema } from '@xyo-network/node-model'
5
-
6
- import type { MemoryNodeParams } from '../MemoryNode.ts'
7
- import { MemoryNode } from '../MemoryNode.ts'
8
- import { attachToExistingNode } from './attachToExistingNode.ts'
9
-
10
- const DEFAULT_NODE_PARAMS = { config: { schema: NodeConfigSchema }, name: 'MemoryNode' as CreatableName }
11
-
12
- export const attachToNewNode = async (
13
- source: NodeInstance,
14
- id: ModuleIdentifier,
15
- destinationParams: MemoryNodeParams = DEFAULT_NODE_PARAMS,
16
- ): Promise<NodeInstance> => {
17
- const destination = await MemoryNode.create(destinationParams)
18
- return await attachToExistingNode(source, id, destination)
19
- }
@@ -1,28 +0,0 @@
1
- import { assertEx, exists } from '@xylabs/sdk-js'
2
- import type { ModuleIdentifier } from '@xyo-network/module-model'
3
- import { asAttachableModuleInstance } from '@xyo-network/module-model'
4
- import type { NodeInstance } from '@xyo-network/node-model'
5
-
6
- export const flatAttachAllToExistingNode = async (source: NodeInstance, destination: NodeInstance): Promise<NodeInstance> => {
7
- const children = (await source.publicChildren()).map(child => asAttachableModuleInstance(child)).filter(exists)
8
- await Promise.all(
9
- children.map(async (child) => {
10
- await destination.register?.(child)
11
- await destination.attach(child.address, true)
12
- }),
13
- )
14
- return destination
15
- }
16
-
17
- export const flatAttachChildToExistingNode = async (source: NodeInstance, id: ModuleIdentifier, destination: NodeInstance): Promise<NodeInstance> => {
18
- assertEx(id !== '*', () => '* is not a single child')
19
- const child = assertEx(await source.resolve(id), () => `Module ${id} not found`)
20
- const attachableChild = asAttachableModuleInstance(child, () => `Module ${id} is not attachable`, { required: true })
21
- await destination.register?.(attachableChild)
22
- await destination.attach(child.address, true)
23
- return destination
24
- }
25
-
26
- export const flatAttachToExistingNode = async (source: NodeInstance, id: ModuleIdentifier, destination: NodeInstance): Promise<NodeInstance> => {
27
- return id === '*' ? await flatAttachAllToExistingNode(source, destination) : flatAttachChildToExistingNode(source, id, destination)
28
- }
@@ -1,15 +0,0 @@
1
- import type { ModuleIdentifier } from '@xyo-network/module-model'
2
- import type { NodeInstance } from '@xyo-network/node-model'
3
-
4
- import type { MemoryNodeParams } from '../MemoryNode.ts'
5
- import { MemoryNode } from '../MemoryNode.ts'
6
- import { flatAttachToExistingNode } from './flatAttachToExistingNode.ts'
7
-
8
- export const flatAttachToNewNode = async (
9
- source: NodeInstance,
10
- id: ModuleIdentifier,
11
- destinationParams: MemoryNodeParams,
12
- ): Promise<NodeInstance> => {
13
- const destination = await MemoryNode.create(destinationParams)
14
- return await flatAttachToExistingNode(source, id, destination)
15
- }
@@ -1,18 +0,0 @@
1
- import { NodeHelper } from '@xyo-network/node-abstract'
2
-
3
- import { attachToExistingNode } from './attachToExistingNode.ts'
4
- import { attachToNewNode } from './attachToNewNode.ts'
5
- import {
6
- flatAttachAllToExistingNode, flatAttachChildToExistingNode, flatAttachToExistingNode,
7
- } from './flatAttachToExistingNode.ts'
8
- import { flatAttachToNewNode } from './flatAttachToNewNode.ts'
9
-
10
- export const MemoryNodeHelper = {
11
- ...NodeHelper,
12
- attachToExistingNode,
13
- attachToNewNode,
14
- flatAttachAllToExistingNode,
15
- flatAttachChildToExistingNode,
16
- flatAttachToExistingNode,
17
- flatAttachToNewNode,
18
- }
package/src/index.ts DELETED
@@ -1,2 +0,0 @@
1
- export * from './MemoryNode.ts'
2
- export * from './NodeHelper/index.ts'