@xyo-network/module-resolver 2.106.0-rc.5 → 2.107.0
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/dist/browser/AbstractModuleResolver.d.cts +6 -7
- package/dist/browser/AbstractModuleResolver.d.cts.map +1 -1
- package/dist/browser/AbstractModuleResolver.d.mts +6 -7
- package/dist/browser/AbstractModuleResolver.d.mts.map +1 -1
- package/dist/browser/AbstractModuleResolver.d.ts +6 -7
- package/dist/browser/AbstractModuleResolver.d.ts.map +1 -1
- package/dist/browser/ResolverEventEmitter.d.cts +7 -19
- package/dist/browser/ResolverEventEmitter.d.cts.map +1 -1
- package/dist/browser/ResolverEventEmitter.d.mts +7 -19
- package/dist/browser/ResolverEventEmitter.d.mts.map +1 -1
- package/dist/browser/ResolverEventEmitter.d.ts +7 -19
- package/dist/browser/ResolverEventEmitter.d.ts.map +1 -1
- package/dist/browser/index.cjs +1 -510
- package/dist/browser/index.cjs.map +1 -1
- package/dist/browser/index.js +1 -489
- package/dist/browser/index.js.map +1 -1
- package/dist/neutral/AbstractModuleResolver.d.cts +6 -7
- package/dist/neutral/AbstractModuleResolver.d.cts.map +1 -1
- package/dist/neutral/AbstractModuleResolver.d.mts +6 -7
- package/dist/neutral/AbstractModuleResolver.d.mts.map +1 -1
- package/dist/neutral/AbstractModuleResolver.d.ts +6 -7
- package/dist/neutral/AbstractModuleResolver.d.ts.map +1 -1
- package/dist/neutral/ResolverEventEmitter.d.cts +7 -19
- package/dist/neutral/ResolverEventEmitter.d.cts.map +1 -1
- package/dist/neutral/ResolverEventEmitter.d.mts +7 -19
- package/dist/neutral/ResolverEventEmitter.d.mts.map +1 -1
- package/dist/neutral/ResolverEventEmitter.d.ts +7 -19
- package/dist/neutral/ResolverEventEmitter.d.ts.map +1 -1
- package/dist/neutral/index.cjs +1 -510
- package/dist/neutral/index.cjs.map +1 -1
- package/dist/neutral/index.js +1 -489
- package/dist/neutral/index.js.map +1 -1
- package/dist/node/AbstractModuleResolver.d.cts +6 -7
- package/dist/node/AbstractModuleResolver.d.cts.map +1 -1
- package/dist/node/AbstractModuleResolver.d.mts +6 -7
- package/dist/node/AbstractModuleResolver.d.mts.map +1 -1
- package/dist/node/AbstractModuleResolver.d.ts +6 -7
- package/dist/node/AbstractModuleResolver.d.ts.map +1 -1
- package/dist/node/ResolverEventEmitter.d.cts +7 -19
- package/dist/node/ResolverEventEmitter.d.cts.map +1 -1
- package/dist/node/ResolverEventEmitter.d.mts +7 -19
- package/dist/node/ResolverEventEmitter.d.mts.map +1 -1
- package/dist/node/ResolverEventEmitter.d.ts +7 -19
- package/dist/node/ResolverEventEmitter.d.ts.map +1 -1
- package/dist/node/index.cjs +1 -522
- package/dist/node/index.cjs.map +1 -1
- package/dist/node/index.js +1 -493
- package/dist/node/index.js.map +1 -1
- package/package.json +8 -9
- package/src/CompositeModuleResolver.ts +1 -1
- package/src/ResolverEventEmitter.ts +1 -1
package/dist/node/index.js
CHANGED
|
@@ -1,494 +1,2 @@
|
|
|
1
|
-
var
|
|
2
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
3
|
-
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
4
|
-
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
5
|
-
|
|
6
|
-
// src/AbstractModuleResolver.ts
|
|
7
|
-
import { assertEx } from "@xylabs/assert";
|
|
8
|
-
import { Base, toJsonString } from "@xylabs/object";
|
|
9
|
-
import { asModuleInstance, ObjectResolverPriority } from "@xyo-network/module-model";
|
|
10
|
-
var _AbstractModuleResolver = class _AbstractModuleResolver extends Base {
|
|
11
|
-
get priority() {
|
|
12
|
-
return this.params.priority ?? ObjectResolverPriority.Normal;
|
|
13
|
-
}
|
|
14
|
-
set priority(value) {
|
|
15
|
-
this.params.priority = value;
|
|
16
|
-
}
|
|
17
|
-
get root() {
|
|
18
|
-
return assertEx(this.params.root, () => "root is not set");
|
|
19
|
-
}
|
|
20
|
-
async resolve(idOrFilter = "*", options) {
|
|
21
|
-
if (idOrFilter === "*") {
|
|
22
|
-
const values = await this.resolveHandler(idOrFilter, options);
|
|
23
|
-
assertEx(Array.isArray(values), () => "resolveHandler returned a non-array");
|
|
24
|
-
return values.map((value) => asModuleInstance(value, () => {
|
|
25
|
-
var _a;
|
|
26
|
-
return `resolveHandler returned invalid result (*) [${(_a = value == null ? void 0 : value.constructor) == null ? void 0 : _a.name}][${toJsonString(value)}]`;
|
|
27
|
-
}));
|
|
28
|
-
}
|
|
29
|
-
switch (typeof idOrFilter) {
|
|
30
|
-
case "string": {
|
|
31
|
-
const [value] = await this.resolveHandler(idOrFilter, options);
|
|
32
|
-
return asModuleInstance(value, () => {
|
|
33
|
-
var _a;
|
|
34
|
-
return (
|
|
35
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
36
|
-
`resolveHandler returned invalid result (string) [${(_a = value == null ? void 0 : value.constructor) == null ? void 0 : _a.name}][${toJsonString(value)}]`
|
|
37
|
-
);
|
|
38
|
-
});
|
|
39
|
-
}
|
|
40
|
-
default: {
|
|
41
|
-
const values = await this.resolveHandler(idOrFilter, options);
|
|
42
|
-
assertEx(Array.isArray(values), () => "resolveHandler returned a non-array");
|
|
43
|
-
return values.map((value) => asModuleInstance(value, () => {
|
|
44
|
-
var _a;
|
|
45
|
-
return `resolveHandler returned invalid result (filter) [${(_a = value == null ? void 0 : value.constructor) == null ? void 0 : _a.name}][${toJsonString(value)}]`;
|
|
46
|
-
}));
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
async resolvePrivate(id, _options) {
|
|
51
|
-
if (id === "*") return await Promise.resolve([]);
|
|
52
|
-
}
|
|
53
|
-
};
|
|
54
|
-
__name(_AbstractModuleResolver, "AbstractModuleResolver");
|
|
55
|
-
var AbstractModuleResolver = _AbstractModuleResolver;
|
|
56
|
-
|
|
57
|
-
// src/CompositeModuleResolver.ts
|
|
58
|
-
import { assertEx as assertEx3 } from "@xylabs/assert";
|
|
59
|
-
import { exists } from "@xylabs/exists";
|
|
60
|
-
import { duplicateModules, ObjectResolverPriority as ObjectResolverPriority2, ResolveHelper } from "@xyo-network/module-model";
|
|
61
|
-
import { LRUCache } from "lru-cache";
|
|
62
|
-
|
|
63
|
-
// src/SimpleModuleResolver.ts
|
|
64
|
-
import { assertEx as assertEx2 } from "@xylabs/assert";
|
|
65
|
-
import { isAddress } from "@xylabs/hex";
|
|
66
|
-
import { compact } from "@xylabs/lodash";
|
|
67
|
-
import { isAddressModuleFilter, isModuleName, isNameModuleFilter, isQueryModuleFilter } from "@xyo-network/module-model";
|
|
68
|
-
var _SimpleModuleResolver = class _SimpleModuleResolver extends AbstractModuleResolver {
|
|
69
|
-
modules = {};
|
|
70
|
-
nameToModule = {};
|
|
71
|
-
constructor(params) {
|
|
72
|
-
super(params);
|
|
73
|
-
}
|
|
74
|
-
get allowNameResolution() {
|
|
75
|
-
return this.params.allowNameResolution ?? true;
|
|
76
|
-
}
|
|
77
|
-
add(mods) {
|
|
78
|
-
if (Array.isArray(mods)) {
|
|
79
|
-
for (const mod of mods) this.addSingleModule(mod);
|
|
80
|
-
} else {
|
|
81
|
-
this.addSingleModule(mods);
|
|
82
|
-
}
|
|
83
|
-
return this;
|
|
84
|
-
}
|
|
85
|
-
addResolver(_resolver) {
|
|
86
|
-
throw "Adding resolvers not supported";
|
|
87
|
-
}
|
|
88
|
-
remove(address) {
|
|
89
|
-
if (Array.isArray(address)) {
|
|
90
|
-
for (const addr of address) this.removeSingleModule(addr);
|
|
91
|
-
} else {
|
|
92
|
-
this.removeSingleModule(address);
|
|
93
|
-
}
|
|
94
|
-
return this;
|
|
95
|
-
}
|
|
96
|
-
removeResolver(_resolver) {
|
|
97
|
-
throw "Removing resolvers not supported";
|
|
98
|
-
}
|
|
99
|
-
resolveHandler(idOrFilter = "*", options) {
|
|
100
|
-
const unfiltered = (() => {
|
|
101
|
-
if (idOrFilter) {
|
|
102
|
-
if (typeof idOrFilter === "string") {
|
|
103
|
-
if (idOrFilter === "*") {
|
|
104
|
-
return Object.values(this.modules);
|
|
105
|
-
}
|
|
106
|
-
const id = idOrFilter;
|
|
107
|
-
const name = isModuleName(id) ? id : void 0;
|
|
108
|
-
const address = isAddress(id) ? id : void 0;
|
|
109
|
-
assertEx2(name || address, () => "module identifier must be a ModuleName or Address");
|
|
110
|
-
return (name ? this.resolveByName(Object.values(this.modules), [
|
|
111
|
-
name
|
|
112
|
-
]).pop() : void 0) ?? (address ? this.resolveByAddress(this.modules, [
|
|
113
|
-
address
|
|
114
|
-
]).pop() : void 0);
|
|
115
|
-
} else {
|
|
116
|
-
const filter = idOrFilter;
|
|
117
|
-
if (isAddressModuleFilter(filter)) {
|
|
118
|
-
return this.resolveByAddress(this.modules, filter.address);
|
|
119
|
-
} else if (isNameModuleFilter(filter)) {
|
|
120
|
-
return this.resolveByName(Object.values(this.modules), filter.name);
|
|
121
|
-
} else if (isQueryModuleFilter(filter)) {
|
|
122
|
-
return this.resolveByQuery(Object.values(this.modules), filter.query);
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
} else {
|
|
126
|
-
return Object.values(this.modules);
|
|
127
|
-
}
|
|
128
|
-
})();
|
|
129
|
-
const identity = options == null ? void 0 : options.identity;
|
|
130
|
-
if (identity) {
|
|
131
|
-
return Array.isArray(unfiltered) ? unfiltered == null ? void 0 : unfiltered.filter((module) => identity(module)) : identity(unfiltered) ? [
|
|
132
|
-
unfiltered
|
|
133
|
-
] : [];
|
|
134
|
-
} else {
|
|
135
|
-
return unfiltered ? Array.isArray(unfiltered) ? unfiltered : [
|
|
136
|
-
unfiltered
|
|
137
|
-
] : [];
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
resolveIdentifier(id, _options) {
|
|
141
|
-
const moduleFromName = this.nameToModule[id];
|
|
142
|
-
if (moduleFromName) {
|
|
143
|
-
return moduleFromName.address;
|
|
144
|
-
}
|
|
145
|
-
for (const module of Object.values(this.modules)) {
|
|
146
|
-
if (module.address === id) {
|
|
147
|
-
return module.address;
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
addSingleModule(mod) {
|
|
152
|
-
if (mod) {
|
|
153
|
-
const modName = mod.modName;
|
|
154
|
-
if (modName && this.allowNameResolution) {
|
|
155
|
-
assertEx2(this.nameToModule[modName] === void 0, () => `Module with name ${modName} already added`);
|
|
156
|
-
this.nameToModule[modName] = mod;
|
|
157
|
-
}
|
|
158
|
-
this.modules[mod.address] = mod;
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
removeSingleModule(address) {
|
|
162
|
-
assertEx2(isAddress(address), () => "Invalid address");
|
|
163
|
-
const mod = assertEx2(this.modules[address], () => "Address not found in modules");
|
|
164
|
-
delete this.modules[address];
|
|
165
|
-
const modName = mod.modName;
|
|
166
|
-
if (modName) {
|
|
167
|
-
delete this.nameToModule[modName];
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
resolveByAddress(modules, address) {
|
|
171
|
-
return compact(address.map((address2) => {
|
|
172
|
-
return modules[address2];
|
|
173
|
-
}));
|
|
174
|
-
}
|
|
175
|
-
resolveByName(modules, name) {
|
|
176
|
-
return compact(name.map((name2) => {
|
|
177
|
-
return modules.find((module) => module.modName === name2);
|
|
178
|
-
}));
|
|
179
|
-
}
|
|
180
|
-
resolveByQuery(modules, query) {
|
|
181
|
-
return compact(modules.filter((module) => query == null ? void 0 : query.reduce((supported, queryList) => {
|
|
182
|
-
return (
|
|
183
|
-
// eslint-disable-next-line unicorn/no-array-reduce
|
|
184
|
-
queryList.reduce((supported2, query2) => {
|
|
185
|
-
const queryable = module.queries.includes(query2);
|
|
186
|
-
return supported2 && queryable;
|
|
187
|
-
}, true) || supported
|
|
188
|
-
);
|
|
189
|
-
}, false)));
|
|
190
|
-
}
|
|
191
|
-
};
|
|
192
|
-
__name(_SimpleModuleResolver, "SimpleModuleResolver");
|
|
193
|
-
var SimpleModuleResolver = _SimpleModuleResolver;
|
|
194
|
-
|
|
195
|
-
// src/CompositeModuleResolver.ts
|
|
196
|
-
var moduleIdentifierParts = /* @__PURE__ */ __name((moduleIdentifier) => {
|
|
197
|
-
return moduleIdentifier == null ? void 0 : moduleIdentifier.split(":");
|
|
198
|
-
}, "moduleIdentifierParts");
|
|
199
|
-
var _CompositeModuleResolver = class _CompositeModuleResolver extends AbstractModuleResolver {
|
|
200
|
-
_cache;
|
|
201
|
-
resolvers = [];
|
|
202
|
-
_allowAddResolver = true;
|
|
203
|
-
_localResolver;
|
|
204
|
-
constructor(params) {
|
|
205
|
-
super(params);
|
|
206
|
-
const localResolver = new SimpleModuleResolver({
|
|
207
|
-
allowNameResolution: params.allowNameResolution,
|
|
208
|
-
root: params.root
|
|
209
|
-
});
|
|
210
|
-
this.addResolver(localResolver);
|
|
211
|
-
const {
|
|
212
|
-
max = 100,
|
|
213
|
-
ttl = 1e3 * 5
|
|
214
|
-
/* five seconds */
|
|
215
|
-
} = params.cache ?? {};
|
|
216
|
-
this._cache = new LRUCache({
|
|
217
|
-
max,
|
|
218
|
-
ttl,
|
|
219
|
-
...params.cache
|
|
220
|
-
});
|
|
221
|
-
this._localResolver = localResolver;
|
|
222
|
-
}
|
|
223
|
-
get allowAddResolver() {
|
|
224
|
-
return this._allowAddResolver;
|
|
225
|
-
}
|
|
226
|
-
set allowAddResolver(value) {
|
|
227
|
-
this.resolvers = [
|
|
228
|
-
this._localResolver
|
|
229
|
-
];
|
|
230
|
-
this._allowAddResolver = value;
|
|
231
|
-
}
|
|
232
|
-
get allowNameResolution() {
|
|
233
|
-
return this.params.allowNameResolution ?? true;
|
|
234
|
-
}
|
|
235
|
-
get moduleIdentifierTransformers() {
|
|
236
|
-
return this.params.moduleIdentifierTransformers ?? ResolveHelper.transformers;
|
|
237
|
-
}
|
|
238
|
-
add(module) {
|
|
239
|
-
if (Array.isArray(module)) {
|
|
240
|
-
for (const mod of module) this.addSingleModule(mod);
|
|
241
|
-
} else {
|
|
242
|
-
this.addSingleModule(module);
|
|
243
|
-
}
|
|
244
|
-
return this;
|
|
245
|
-
}
|
|
246
|
-
addResolver(resolver) {
|
|
247
|
-
if (this.allowAddResolver) {
|
|
248
|
-
this.resolvers.push(resolver);
|
|
249
|
-
}
|
|
250
|
-
return this;
|
|
251
|
-
}
|
|
252
|
-
remove(addresses) {
|
|
253
|
-
if (Array.isArray(addresses)) {
|
|
254
|
-
for (const address of addresses) this.removeSingleModule(address);
|
|
255
|
-
} else {
|
|
256
|
-
this.removeSingleModule(addresses);
|
|
257
|
-
}
|
|
258
|
-
return this;
|
|
259
|
-
}
|
|
260
|
-
removeResolver(resolver) {
|
|
261
|
-
this.resolvers = this.resolvers.filter((item) => item !== resolver);
|
|
262
|
-
return this;
|
|
263
|
-
}
|
|
264
|
-
// eslint-disable-next-line complexity
|
|
265
|
-
async resolveHandler(idOrFilter = "*", options = {}) {
|
|
266
|
-
const mutatedOptions = {
|
|
267
|
-
...options,
|
|
268
|
-
maxDepth: (options == null ? void 0 : options.maxDepth) ?? _CompositeModuleResolver.defaultMaxDepth
|
|
269
|
-
};
|
|
270
|
-
if (idOrFilter === "*") {
|
|
271
|
-
const all = idOrFilter;
|
|
272
|
-
if (mutatedOptions.maxDepth < 0) {
|
|
273
|
-
return [];
|
|
274
|
-
}
|
|
275
|
-
if (mutatedOptions.maxDepth === 0) {
|
|
276
|
-
return await this._localResolver.resolve(all, mutatedOptions) ?? [];
|
|
277
|
-
}
|
|
278
|
-
const childOptions = {
|
|
279
|
-
...mutatedOptions,
|
|
280
|
-
maxDepth: (mutatedOptions == null ? void 0 : mutatedOptions.maxDepth) - 1
|
|
281
|
-
};
|
|
282
|
-
const result = await Promise.all(this.resolvers.map(async (resolver) => {
|
|
283
|
-
const result2 = await resolver.resolve(all, childOptions);
|
|
284
|
-
return result2;
|
|
285
|
-
}));
|
|
286
|
-
const flatResult = result.flat().filter(exists);
|
|
287
|
-
return flatResult.filter(duplicateModules);
|
|
288
|
-
}
|
|
289
|
-
if (typeof idOrFilter === "string") {
|
|
290
|
-
if (mutatedOptions.maxDepth < 0) {
|
|
291
|
-
return [];
|
|
292
|
-
}
|
|
293
|
-
const idParts = moduleIdentifierParts(idOrFilter);
|
|
294
|
-
if (idParts.length > 1) {
|
|
295
|
-
const mod = await this.resolveMultipartIdentifier(idOrFilter);
|
|
296
|
-
return mod ? Array.isArray(mod) ? mod : [
|
|
297
|
-
mod
|
|
298
|
-
] : [];
|
|
299
|
-
}
|
|
300
|
-
const id = await ResolveHelper.transformModuleIdentifier(idOrFilter, this.moduleIdentifierTransformers);
|
|
301
|
-
if (id) {
|
|
302
|
-
if (mutatedOptions.maxDepth < 0) {
|
|
303
|
-
return [];
|
|
304
|
-
}
|
|
305
|
-
const cachedResult = this._cache.get(id);
|
|
306
|
-
if (cachedResult) {
|
|
307
|
-
if (cachedResult.status === "dead") {
|
|
308
|
-
this._cache.delete(id);
|
|
309
|
-
} else {
|
|
310
|
-
return [
|
|
311
|
-
cachedResult
|
|
312
|
-
];
|
|
313
|
-
}
|
|
314
|
-
}
|
|
315
|
-
if (mutatedOptions.maxDepth === 0) {
|
|
316
|
-
const mod2 = await this._localResolver.resolve(idOrFilter, mutatedOptions);
|
|
317
|
-
return mod2 ? Array.isArray(mod2) ? mod2 : [
|
|
318
|
-
mod2
|
|
319
|
-
] : [];
|
|
320
|
-
}
|
|
321
|
-
const resolvePriority = /* @__PURE__ */ __name(async (priority) => {
|
|
322
|
-
const resolvers = this.resolvers.filter((resolver) => resolver.priority === priority);
|
|
323
|
-
const results = (await Promise.all(resolvers.map(async (resolver) => {
|
|
324
|
-
const result2 = await resolver.resolve(id, mutatedOptions);
|
|
325
|
-
return result2;
|
|
326
|
-
}))).filter(exists);
|
|
327
|
-
const result = results.filter(exists).filter(duplicateModules).pop();
|
|
328
|
-
if (result) {
|
|
329
|
-
this._cache.set(id, result);
|
|
330
|
-
return result;
|
|
331
|
-
}
|
|
332
|
-
return priority === ObjectResolverPriority2.VeryLow ? void 0 : await resolvePriority(priority - 1);
|
|
333
|
-
}, "resolvePriority");
|
|
334
|
-
const mod = await resolvePriority(ObjectResolverPriority2.VeryHigh);
|
|
335
|
-
return mod ? Array.isArray(mod) ? mod : [
|
|
336
|
-
mod
|
|
337
|
-
] : [];
|
|
338
|
-
}
|
|
339
|
-
} else if (typeof idOrFilter === "object") {
|
|
340
|
-
if (mutatedOptions.maxDepth < 0) {
|
|
341
|
-
return [];
|
|
342
|
-
}
|
|
343
|
-
const filter = idOrFilter;
|
|
344
|
-
if (mutatedOptions.maxDepth === 0) {
|
|
345
|
-
return await this._localResolver.resolve(filter, mutatedOptions);
|
|
346
|
-
}
|
|
347
|
-
const childOptions = {
|
|
348
|
-
...mutatedOptions,
|
|
349
|
-
maxDepth: (mutatedOptions == null ? void 0 : mutatedOptions.maxDepth) - 1
|
|
350
|
-
};
|
|
351
|
-
const result = await Promise.all(this.resolvers.map(async (resolver) => {
|
|
352
|
-
const result2 = await resolver.resolve(filter, childOptions);
|
|
353
|
-
return result2;
|
|
354
|
-
}));
|
|
355
|
-
const flatResult = result.flat().filter(exists);
|
|
356
|
-
return flatResult.filter(duplicateModules);
|
|
357
|
-
}
|
|
358
|
-
return [];
|
|
359
|
-
}
|
|
360
|
-
async resolveIdentifier(id, _options) {
|
|
361
|
-
const idParts = id.split(":");
|
|
362
|
-
if (idParts.length > 1) {
|
|
363
|
-
return this.resolveComplexIdentifier(id);
|
|
364
|
-
}
|
|
365
|
-
const results = (await Promise.all(this.resolvers.map(async (resolver) => {
|
|
366
|
-
const result2 = await resolver.resolveIdentifier(id);
|
|
367
|
-
return result2;
|
|
368
|
-
}))).filter(exists);
|
|
369
|
-
const result = results.shift();
|
|
370
|
-
if (results.length > 0) {
|
|
371
|
-
for (const altResult of results) {
|
|
372
|
-
assertEx3(altResult === result, () => `Inconsistent results for ${id} [${result}][${altResult}]`);
|
|
373
|
-
}
|
|
374
|
-
}
|
|
375
|
-
return result;
|
|
376
|
-
}
|
|
377
|
-
resolveComplexIdentifier(_id, _options) {
|
|
378
|
-
throw new Error("Method not implemented.");
|
|
379
|
-
}
|
|
380
|
-
addSingleModule(module) {
|
|
381
|
-
if (module) {
|
|
382
|
-
this._localResolver.add(module);
|
|
383
|
-
}
|
|
384
|
-
}
|
|
385
|
-
removeSingleModule(address) {
|
|
386
|
-
this._localResolver.remove(address);
|
|
387
|
-
}
|
|
388
|
-
async resolveMultipartIdentifier(moduleIdentifier) {
|
|
389
|
-
const idParts = moduleIdentifierParts(moduleIdentifier);
|
|
390
|
-
assertEx3(idParts.length >= 2, () => "Not a valid multipart identifier");
|
|
391
|
-
const id = assertEx3(idParts.shift());
|
|
392
|
-
const module = await this.resolve(id) ?? await this.resolvePrivate(id);
|
|
393
|
-
return await (module == null ? void 0 : module.resolve(idParts.join(":"))) ?? await (module == null ? void 0 : module.resolvePrivate(idParts.join(":")));
|
|
394
|
-
}
|
|
395
|
-
};
|
|
396
|
-
__name(_CompositeModuleResolver, "CompositeModuleResolver");
|
|
397
|
-
__publicField(_CompositeModuleResolver, "defaultMaxDepth", 3);
|
|
398
|
-
var CompositeModuleResolver = _CompositeModuleResolver;
|
|
399
|
-
|
|
400
|
-
// src/NameRegistrarTransformer.ts
|
|
401
|
-
import { PayloadDivinerQuerySchema } from "@xyo-network/diviner-payload-model";
|
|
402
|
-
import { LRUCache as LRUCache2 } from "lru-cache";
|
|
403
|
-
var _NameRegistrarTransformer = class _NameRegistrarTransformer {
|
|
404
|
-
registrarDiviner;
|
|
405
|
-
root;
|
|
406
|
-
_cache;
|
|
407
|
-
constructor(registrarDiviner, root) {
|
|
408
|
-
this.registrarDiviner = registrarDiviner;
|
|
409
|
-
this.root = root;
|
|
410
|
-
this._cache = new LRUCache2({
|
|
411
|
-
max: 1e3,
|
|
412
|
-
ttl: 1e3 * 5
|
|
413
|
-
});
|
|
414
|
-
}
|
|
415
|
-
async transform(identifier) {
|
|
416
|
-
var _a, _b;
|
|
417
|
-
const parts = identifier.split(":");
|
|
418
|
-
const first = parts.shift();
|
|
419
|
-
const nameParts = first == null ? void 0 : first.split(".");
|
|
420
|
-
if ((nameParts == null ? void 0 : nameParts.length) === 2 && nameParts[1] === this.root) {
|
|
421
|
-
const cachedResult = this._cache.get(identifier);
|
|
422
|
-
if (cachedResult) return cachedResult;
|
|
423
|
-
const query = {
|
|
424
|
-
domain: nameParts[0],
|
|
425
|
-
order: "desc",
|
|
426
|
-
schema: PayloadDivinerQuerySchema,
|
|
427
|
-
tld: nameParts[1]
|
|
428
|
-
};
|
|
429
|
-
const result = await ((_a = this.registrarDiviner) == null ? void 0 : _a.divine([
|
|
430
|
-
query
|
|
431
|
-
]));
|
|
432
|
-
const resultPayload = result == null ? void 0 : result.shift();
|
|
433
|
-
if (!resultPayload) {
|
|
434
|
-
throw new Error(`Unable to resolve registrar name (failed) [${first}]`);
|
|
435
|
-
}
|
|
436
|
-
if (resultPayload) {
|
|
437
|
-
const address = (_b = resultPayload.address) == null ? void 0 : _b.shift();
|
|
438
|
-
if (address) {
|
|
439
|
-
this._cache.set(identifier, address);
|
|
440
|
-
return address;
|
|
441
|
-
}
|
|
442
|
-
}
|
|
443
|
-
throw new Error(`Unable to resolve registrar name (not found) [${first}]`);
|
|
444
|
-
}
|
|
445
|
-
return identifier;
|
|
446
|
-
}
|
|
447
|
-
};
|
|
448
|
-
__name(_NameRegistrarTransformer, "NameRegistrarTransformer");
|
|
449
|
-
var NameRegistrarTransformer = _NameRegistrarTransformer;
|
|
450
|
-
|
|
451
|
-
// src/ResolverEventEmitter.ts
|
|
452
|
-
var getMixin = /* @__PURE__ */ __name((resolver) => {
|
|
453
|
-
const listeners = [];
|
|
454
|
-
const emit = /* @__PURE__ */ __name((event, args) => {
|
|
455
|
-
if (listeners.length === 0) return false;
|
|
456
|
-
listeners.map((listener) => listener(args));
|
|
457
|
-
return true;
|
|
458
|
-
}, "emit");
|
|
459
|
-
const onModuleResolved = /* @__PURE__ */ __name((module, filter) => {
|
|
460
|
-
const args = {
|
|
461
|
-
filter,
|
|
462
|
-
module
|
|
463
|
-
};
|
|
464
|
-
emit("moduleResolved", args);
|
|
465
|
-
}, "onModuleResolved");
|
|
466
|
-
const { resolve } = resolver;
|
|
467
|
-
function originalResolve(filter) {
|
|
468
|
-
return resolve.bind(resolver)(filter);
|
|
469
|
-
}
|
|
470
|
-
__name(originalResolve, "originalResolve");
|
|
471
|
-
return {
|
|
472
|
-
on: /* @__PURE__ */ __name((event, listener) => {
|
|
473
|
-
listeners.push(listener);
|
|
474
|
-
}, "on"),
|
|
475
|
-
resolve: /* @__PURE__ */ __name(async (filter) => {
|
|
476
|
-
const modules = await originalResolve(filter);
|
|
477
|
-
await Promise.allSettled(modules.map((mod) => onModuleResolved(mod, filter)));
|
|
478
|
-
return modules;
|
|
479
|
-
}, "resolve")
|
|
480
|
-
};
|
|
481
|
-
}, "getMixin");
|
|
482
|
-
var mixinResolverEventEmitter = /* @__PURE__ */ __name((resolver) => {
|
|
483
|
-
const mixin = getMixin(resolver);
|
|
484
|
-
const ret = Object.assign(resolver, mixin);
|
|
485
|
-
return ret;
|
|
486
|
-
}, "mixinResolverEventEmitter");
|
|
487
|
-
export {
|
|
488
|
-
AbstractModuleResolver,
|
|
489
|
-
CompositeModuleResolver,
|
|
490
|
-
NameRegistrarTransformer,
|
|
491
|
-
SimpleModuleResolver,
|
|
492
|
-
mixinResolverEventEmitter
|
|
493
|
-
};
|
|
1
|
+
var S=Object.defineProperty;var L=(l,e,t)=>e in l?S(l,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):l[e]=t;var a=(l,e)=>S(l,"name",{value:e,configurable:!0});var j=(l,e,t)=>L(l,typeof e!="symbol"?e+"":e,t);import{assertEx as x}from"@xylabs/assert";import{Base as Q,toJsonString as R}from"@xylabs/object";import{asModuleInstance as A,ObjectResolverPriority as U}from"@xyo-network/module-model";var M=class M extends Q{get priority(){return this.params.priority??U.Normal}set priority(e){this.params.priority=e}get root(){return x(this.params.root,()=>"root is not set")}async resolve(e="*",t){if(e==="*"){let r=await this.resolveHandler(e,t);return x(Array.isArray(r),()=>"resolveHandler returned a non-array"),r.map(s=>A(s,()=>{var o;return`resolveHandler returned invalid result (*) [${(o=s==null?void 0:s.constructor)==null?void 0:o.name}][${R(s)}]`}))}switch(typeof e){case"string":{let[r]=await this.resolveHandler(e,t);return A(r,()=>{var s;return`resolveHandler returned invalid result (string) [${(s=r==null?void 0:r.constructor)==null?void 0:s.name}][${R(r)}]`})}default:{let r=await this.resolveHandler(e,t);return x(Array.isArray(r),()=>"resolveHandler returned a non-array"),r.map(s=>A(s,()=>{var o;return`resolveHandler returned invalid result (filter) [${(o=s==null?void 0:s.constructor)==null?void 0:o.name}][${R(s)}]`}))}}}async resolvePrivate(e,t){if(e==="*")return await Promise.resolve([])}};a(M,"AbstractModuleResolver");var c=M;import{assertEx as N}from"@xylabs/assert";import{exists as h}from"@xylabs/exists";import{duplicateModules as P,ObjectResolverPriority as $,ResolveHelper as I}from"@xyo-network/module-model";import{LRUCache as q}from"lru-cache";import{assertEx as p}from"@xylabs/assert";import{isAddress as H}from"@xylabs/hex";import{compact as _}from"@xylabs/lodash";import{isAddressModuleFilter as V,isModuleName as C,isNameModuleFilter as J,isQueryModuleFilter as k}from"@xyo-network/module-model";var D=class D extends c{modules={};nameToModule={};constructor(e){super(e)}get allowNameResolution(){return this.params.allowNameResolution??!0}add(e){if(Array.isArray(e))for(let t of e)this.addSingleModule(t);else this.addSingleModule(e);return this}addResolver(e){throw"Adding resolvers not supported"}remove(e){if(Array.isArray(e))for(let t of e)this.removeSingleModule(t);else this.removeSingleModule(e);return this}removeResolver(e){throw"Removing resolvers not supported"}resolveHandler(e="*",t){let r=(()=>{if(e)if(typeof e=="string"){if(e==="*")return Object.values(this.modules);let o=e,i=C(o)?o:void 0,n=H(o)?o:void 0;return p(i||n,()=>"module identifier must be a ModuleName or Address"),(i?this.resolveByName(Object.values(this.modules),[i]).pop():void 0)??(n?this.resolveByAddress(this.modules,[n]).pop():void 0)}else{let o=e;if(V(o))return this.resolveByAddress(this.modules,o.address);if(J(o))return this.resolveByName(Object.values(this.modules),o.name);if(k(o))return this.resolveByQuery(Object.values(this.modules),o.query)}else return Object.values(this.modules)})(),s=t==null?void 0:t.identity;return s?Array.isArray(r)?r==null?void 0:r.filter(o=>s(o)):s(r)?[r]:[]:r?Array.isArray(r)?r:[r]:[]}resolveIdentifier(e,t){let r=this.nameToModule[e];if(r)return r.address;for(let s of Object.values(this.modules))if(s.address===e)return s.address}addSingleModule(e){if(e){let t=e.modName;t&&this.allowNameResolution&&(p(this.nameToModule[t]===void 0,()=>`Module with name ${t} already added`),this.nameToModule[t]=e),this.modules[e.address]=e}}removeSingleModule(e){p(H(e),()=>"Invalid address");let t=p(this.modules[e],()=>"Address not found in modules");delete this.modules[e];let r=t.modName;r&&delete this.nameToModule[r]}resolveByAddress(e,t){return _(t.map(r=>e[r]))}resolveByName(e,t){return _(t.map(r=>e.find(s=>s.modName===r)))}resolveByQuery(e,t){return _(e.filter(r=>t==null?void 0:t.reduce((s,o)=>o.reduce((i,n)=>{let u=r.queries.includes(n);return i&&u},!0)||s,!1)))}};a(D,"SimpleModuleResolver");var y=D;var B=a(l=>l==null?void 0:l.split(":"),"moduleIdentifierParts"),f=class f extends c{_cache;resolvers=[];_allowAddResolver=!0;_localResolver;constructor(e){super(e);let t=new y({allowNameResolution:e.allowNameResolution,root:e.root});this.addResolver(t);let{max:r=100,ttl:s=1e3*5}=e.cache??{};this._cache=new q({max:r,ttl:s,...e.cache}),this._localResolver=t}get allowAddResolver(){return this._allowAddResolver}set allowAddResolver(e){this.resolvers=[this._localResolver],this._allowAddResolver=e}get allowNameResolution(){return this.params.allowNameResolution??!0}get moduleIdentifierTransformers(){return this.params.moduleIdentifierTransformers??I.transformers}add(e){if(Array.isArray(e))for(let t of e)this.addSingleModule(t);else this.addSingleModule(e);return this}addResolver(e){return this.allowAddResolver&&this.resolvers.push(e),this}remove(e){if(Array.isArray(e))for(let t of e)this.removeSingleModule(t);else this.removeSingleModule(e);return this}removeResolver(e){return this.resolvers=this.resolvers.filter(t=>t!==e),this}async resolveHandler(e="*",t={}){let r={...t,maxDepth:(t==null?void 0:t.maxDepth)??f.defaultMaxDepth};if(e==="*"){let s=e;if(r.maxDepth<0)return[];if(r.maxDepth===0)return await this._localResolver.resolve(s,r)??[];let o={...r,maxDepth:(r==null?void 0:r.maxDepth)-1};return(await Promise.all(this.resolvers.map(async u=>await u.resolve(s,o)))).flat().filter(h).filter(P)}if(typeof e=="string"){if(r.maxDepth<0)return[];if(B(e).length>1){let i=await this.resolveMultipartIdentifier(e);return i?Array.isArray(i)?i:[i]:[]}let o=await I.transformModuleIdentifier(e,this.moduleIdentifierTransformers);if(o){if(r.maxDepth<0)return[];let i=this._cache.get(o);if(i)if(i.status==="dead")this._cache.delete(o);else return[i];if(r.maxDepth===0){let d=await this._localResolver.resolve(e,r);return d?Array.isArray(d)?d:[d]:[]}let n=a(async d=>{let m=this.resolvers.filter(g=>g.priority===d),w=(await Promise.all(m.map(async g=>await g.resolve(o,r)))).filter(h).filter(h).findLast(P);return w?(this._cache.set(o,w),w):d===$.VeryLow?void 0:await n(d-1)},"resolvePriority"),u=await n($.VeryHigh);return u?Array.isArray(u)?u:[u]:[]}}else if(typeof e=="object"){if(r.maxDepth<0)return[];let s=e;if(r.maxDepth===0)return await this._localResolver.resolve(s,r);let o={...r,maxDepth:(r==null?void 0:r.maxDepth)-1};return(await Promise.all(this.resolvers.map(async u=>await u.resolve(s,o)))).flat().filter(h).filter(P)}return[]}async resolveIdentifier(e,t){if(e.split(":").length>1)return this.resolveComplexIdentifier(e);let s=(await Promise.all(this.resolvers.map(async i=>await i.resolveIdentifier(e)))).filter(h),o=s.shift();if(s.length>0)for(let i of s)N(i===o,()=>`Inconsistent results for ${e} [${o}][${i}]`);return o}resolveComplexIdentifier(e,t){throw new Error("Method not implemented.")}addSingleModule(e){e&&this._localResolver.add(e)}removeSingleModule(e){this._localResolver.remove(e)}async resolveMultipartIdentifier(e){let t=B(e);N(t.length>=2,()=>"Not a valid multipart identifier");let r=N(t.shift()),s=await this.resolve(r)??await this.resolvePrivate(r);return await(s==null?void 0:s.resolve(t.join(":")))??await(s==null?void 0:s.resolvePrivate(t.join(":")))}};a(f,"CompositeModuleResolver"),j(f,"defaultMaxDepth",3);var E=f;import{PayloadDivinerQuerySchema as z}from"@xyo-network/diviner-payload-model";import{LRUCache as G}from"lru-cache";var b=class b{registrarDiviner;root;_cache;constructor(e,t){this.registrarDiviner=e,this.root=t,this._cache=new G({max:1e3,ttl:1e3*5})}async transform(e){var o,i;let r=e.split(":").shift(),s=r==null?void 0:r.split(".");if((s==null?void 0:s.length)===2&&s[1]===this.root){let n=this._cache.get(e);if(n)return n;let u={domain:s[0],order:"desc",schema:z,tld:s[1]},d=await((o=this.registrarDiviner)==null?void 0:o.divine([u])),m=d==null?void 0:d.shift();if(!m)throw new Error(`Unable to resolve registrar name (failed) [${r}]`);if(m){let v=(i=m.address)==null?void 0:i.shift();if(v)return this._cache.set(e,v),v}throw new Error(`Unable to resolve registrar name (not found) [${r}]`)}return e}};a(b,"NameRegistrarTransformer");var T=b;var K=a(l=>{let e=[],t=a((i,n)=>e.length===0?!1:(e.map(u=>u(n)),!0),"emit"),r=a((i,n)=>{t("moduleResolved",{filter:n,module:i})},"onModuleResolved"),{resolve:s}=l;function o(i){return s.bind(l)(i)}return a(o,"originalResolve"),{on:a((i,n)=>{e.push(n)},"on"),resolve:a(async i=>{let n=await o(i);return await Promise.allSettled(n.map(u=>r(u,i))),n},"resolve")}},"getMixin"),xe=a(l=>{let e=K(l);return Object.assign(l,e)},"mixinResolverEventEmitter");export{c as AbstractModuleResolver,E as CompositeModuleResolver,T as NameRegistrarTransformer,y as SimpleModuleResolver,xe as mixinResolverEventEmitter};
|
|
494
2
|
//# sourceMappingURL=index.js.map
|