@xyo-network/schema-cache 7.0.12 → 7.0.13
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/README.md +47 -1
- package/dist/neutral/Debounce.d.ts +5 -0
- package/dist/neutral/Debounce.d.ts.map +1 -0
- package/dist/neutral/SchemaCache.d.ts +43 -0
- package/dist/neutral/SchemaCache.d.ts.map +1 -0
- package/dist/neutral/SchemaNameToValidatorMap.d.ts +12 -0
- package/dist/neutral/SchemaNameToValidatorMap.d.ts.map +1 -0
- package/dist/neutral/TtlLruCache.d.ts +13 -0
- package/dist/neutral/TtlLruCache.d.ts.map +1 -0
- package/dist/neutral/index.d.ts +4 -5
- package/dist/neutral/index.d.ts.map +1 -1
- package/dist/neutral/index.mjs +177 -2
- package/dist/neutral/index.mjs.map +4 -4
- package/package.json +44 -27
package/README.md
CHANGED
|
@@ -1,3 +1,49 @@
|
|
|
1
|
+
[![logo][]](https://xyo.network)
|
|
2
|
+
|
|
1
3
|
# @xyo-network/schema-cache
|
|
2
4
|
|
|
3
|
-
|
|
5
|
+
[![npm][npm-badge]][npm-link]
|
|
6
|
+
[![license][license-badge]][license-link]
|
|
7
|
+
|
|
8
|
+
> Primary SDK for using XYO Protocol 2.0
|
|
9
|
+
|
|
10
|
+
## Install
|
|
11
|
+
|
|
12
|
+
Using npm:
|
|
13
|
+
|
|
14
|
+
```sh
|
|
15
|
+
npm install {{name}}
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
Using yarn:
|
|
19
|
+
|
|
20
|
+
```sh
|
|
21
|
+
yarn add {{name}}
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
Using pnpm:
|
|
25
|
+
|
|
26
|
+
```sh
|
|
27
|
+
pnpm add {{name}}
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Using bun:
|
|
31
|
+
|
|
32
|
+
```sh
|
|
33
|
+
bun add {{name}}
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
## License
|
|
38
|
+
|
|
39
|
+
See the [LICENSE](LICENSE) file for license rights and limitations (LGPL-3.0-only).
|
|
40
|
+
|
|
41
|
+
## Credits
|
|
42
|
+
|
|
43
|
+
[Made with 🔥 and ❄️ by XYO Foundation](https://xyo.network)
|
|
44
|
+
|
|
45
|
+
[npm-badge]: https://img.shields.io/npm/v/@xyo-network/schema-cache.svg
|
|
46
|
+
[npm-link]: https://www.npmjs.com/package/@xyo-network/schema-cache
|
|
47
|
+
[license-badge]: https://img.shields.io/npm/l/@xyo-network/schema-cache.svg
|
|
48
|
+
[license-link]: https://github.com/xylabs/sdk-js/blob/main/LICENSE
|
|
49
|
+
[logo]: https://cdn.xy.company/img/brand/XYO_full_colored.png
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Debounce.d.ts","sourceRoot":"","sources":["../../src/Debounce.ts"],"names":[],"mappings":"AAEA,qBAAa,QAAQ,CAAC,IAAI,GAAG,MAAM;IACjC,OAAO,CAAC,GAAG,CAA0B;IAE/B,GAAG,CAAC,CAAC,EAAE,GAAG,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,EAAE,OAAO,SAAS;CAiBpE"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { SchemaResolution, SchemaResolutionSource } from '@xyo-network/domain-payload-plugin';
|
|
2
|
+
import type { FetchedPayload } from '@xyo-network/huri';
|
|
3
|
+
import type { SchemaPayload } from '@xyo-network/schema-payload-plugin';
|
|
4
|
+
import type { SchemaNameToValidatorMap } from './SchemaNameToValidatorMap.ts';
|
|
5
|
+
export interface SchemaCacheEntry extends FetchedPayload<SchemaPayload> {
|
|
6
|
+
/** Provenance of the schema definition when resolved via the domain schema authority */
|
|
7
|
+
source?: SchemaResolutionSource;
|
|
8
|
+
}
|
|
9
|
+
export declare class SchemaCache<T extends SchemaNameToValidatorMap = SchemaNameToValidatorMap> {
|
|
10
|
+
/**
|
|
11
|
+
* Object representing `null` since LRU Cache types
|
|
12
|
+
* only allow for types that derive from object
|
|
13
|
+
*/
|
|
14
|
+
protected static readonly NULL: SchemaCacheEntry;
|
|
15
|
+
private static _instance?;
|
|
16
|
+
private _cache;
|
|
17
|
+
private _resolutions;
|
|
18
|
+
private _validators;
|
|
19
|
+
private getDebounce;
|
|
20
|
+
onSchemaCached?: (name: string, entry: SchemaCacheEntry) => void;
|
|
21
|
+
proxy?: string;
|
|
22
|
+
private constructor();
|
|
23
|
+
static get instance(): SchemaCache<SchemaNameToValidatorMap>;
|
|
24
|
+
private cacheSchemaIfValid;
|
|
25
|
+
private cacheSchemas;
|
|
26
|
+
private fetchSchema;
|
|
27
|
+
/**
|
|
28
|
+
* A map of cached schema (by name) to payload validators for the schema. A schema
|
|
29
|
+
* must be cached via `get('schema.name')` before it's validator can be used as
|
|
30
|
+
* they are compiled dynamically at runtime upon retrieval.
|
|
31
|
+
*/
|
|
32
|
+
get validators(): T;
|
|
33
|
+
get(schema?: string): Promise<SchemaCacheEntry | undefined | null>;
|
|
34
|
+
/**
|
|
35
|
+
* Resolve a schema name to its domain-published authoritative definition (SPEC.md in
|
|
36
|
+
* @xyo-network/domain-payload-plugin), with provenance and a richer status than get():
|
|
37
|
+
* 'resolved' | 'not-published' | 'unreachable'. Resolved definitions are cached and
|
|
38
|
+
* their validators compiled, exactly as with get(). 'unreachable' results are
|
|
39
|
+
* retryable: they are not cached as permanent negatives.
|
|
40
|
+
*/
|
|
41
|
+
resolve(schema?: string): Promise<SchemaResolution | undefined>;
|
|
42
|
+
}
|
|
43
|
+
//# sourceMappingURL=SchemaCache.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SchemaCache.d.ts","sourceRoot":"","sources":["../../src/SchemaCache.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,gBAAgB,EAAE,sBAAsB,EAAE,MAAM,oCAAoC,CAAA;AAElG,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAA;AACvD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,oCAAoC,CAAA;AAMvE,OAAO,KAAK,EAAE,wBAAwB,EAAE,MAAM,+BAA+B,CAAA;AAS7E,MAAM,WAAW,gBAAiB,SAAQ,cAAc,CAAC,aAAa,CAAC;IACrE,wFAAwF;IACxF,MAAM,CAAC,EAAE,sBAAsB,CAAA;CAChC;AAED,qBAAa,WAAW,CAAC,CAAC,SAAS,wBAAwB,GAAG,wBAAwB;IACpF;;;OAGG;IACH,SAAS,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI,EAAE,gBAAgB,CAAwD;IAExG,OAAO,CAAC,MAAM,CAAC,SAAS,CAAC,CAAa;IAEtC,OAAO,CAAC,MAAM,CAA8E;IAC5F,OAAO,CAAC,YAAY,CAA8E;IAClG,OAAO,CAAC,WAAW,CAAa;IAGhC,OAAO,CAAC,WAAW,CAAiB;IAEpC,cAAc,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,gBAAgB,KAAK,IAAI,CAAA;IAChE,KAAK,CAAC,EAAE,MAAM,CAAA;IAEd,OAAO;IAIP,MAAM,KAAK,QAAQ,0CAGlB;IAED,OAAO,CAAC,kBAAkB;IAiB1B,OAAO,CAAC,YAAY;YAMN,WAAW;IA6BzB;;;;OAIG;IACH,IAAI,UAAU,IAAI,CAAC,CAElB;IAEK,GAAG,CAAC,MAAM,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,gBAAgB,GAAG,SAAS,GAAG,IAAI,CAAC;IAcxE;;;;;;OAMG;IACG,OAAO,CAAC,MAAM,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,gBAAgB,GAAG,SAAS,CAAC;CAetE"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Payload } from '@xyo-network/payload-model';
|
|
2
|
+
/**
|
|
3
|
+
* Used in conjunction with schema validation to support compile time type assertion
|
|
4
|
+
* for known schema types.
|
|
5
|
+
*/
|
|
6
|
+
export type NarrowPayload<T extends Payload = Payload> = ((x: Payload) => x is T) | undefined;
|
|
7
|
+
/**
|
|
8
|
+
* Used to map known schemas (byt their string name) to the validators which assert their types
|
|
9
|
+
*/
|
|
10
|
+
export interface SchemaNameToValidatorMap {
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=SchemaNameToValidatorMap.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SchemaNameToValidatorMap.d.ts","sourceRoot":"","sources":["../../src/SchemaNameToValidatorMap.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,OAAO,EAAiB,MAAM,4BAA4B,CAAA;AAGxE;;;GAGG;AACH,MAAM,MAAM,aAAa,CAAC,CAAC,SAAS,OAAO,GAAG,OAAO,IAAI,CAAC,CAAC,CAAC,EAAE,OAAO,KAAK,CAAC,IAAI,CAAC,CAAC,GAAG,SAAS,CAAA;AAE7F;;GAEG;AACH,MAAM,WAAW,wBAAwB;CAKxC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export interface TtlLruCacheOpts {
|
|
2
|
+
max: number;
|
|
3
|
+
ttl: number;
|
|
4
|
+
}
|
|
5
|
+
export declare class TtlLruCache<K, V> {
|
|
6
|
+
private readonly map;
|
|
7
|
+
private readonly max;
|
|
8
|
+
private readonly ttl;
|
|
9
|
+
constructor(opts: TtlLruCacheOpts);
|
|
10
|
+
get(key: K): V | undefined;
|
|
11
|
+
set(key: K, value: V): void;
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=TtlLruCache.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"TtlLruCache.d.ts","sourceRoot":"","sources":["../../src/TtlLruCache.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,eAAe;IAC9B,GAAG,EAAE,MAAM,CAAA;IACX,GAAG,EAAE,MAAM,CAAA;CACZ;AAED,qBAAa,WAAW,CAAC,CAAC,EAAE,CAAC;IAC3B,OAAO,CAAC,QAAQ,CAAC,GAAG,CAA8C;IAClE,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAQ;IAC5B,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAQ;gBAEhB,IAAI,EAAE,eAAe;IAKjC,GAAG,CAAC,GAAG,EAAE,CAAC,GAAG,CAAC,GAAG,SAAS;IAY1B,GAAG,CAAC,GAAG,EAAE,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,IAAI;CAQ5B"}
|
package/dist/neutral/index.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
|
|
5
|
-
export * from '@xyo-network/sdk-protocol/schema-cache';
|
|
1
|
+
export * from './Debounce.ts';
|
|
2
|
+
export * from './SchemaCache.ts';
|
|
3
|
+
export * from './SchemaNameToValidatorMap.ts';
|
|
4
|
+
export * from './TtlLruCache.ts';
|
|
6
5
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAA;AAC7B,cAAc,kBAAkB,CAAA;AAChC,cAAc,+BAA+B,CAAA;AAC7C,cAAc,kBAAkB,CAAA"}
|
package/dist/neutral/index.mjs
CHANGED
|
@@ -1,3 +1,178 @@
|
|
|
1
|
-
// src/
|
|
2
|
-
|
|
1
|
+
// src/Debounce.ts
|
|
2
|
+
import { delay, isDefined } from "@ariestools/sdk";
|
|
3
|
+
var Debounce = class {
|
|
4
|
+
map = /* @__PURE__ */ new Map();
|
|
5
|
+
async one(key, closure, timeout = 1e4) {
|
|
6
|
+
const startTime = Date.now();
|
|
7
|
+
while (isDefined(this.map.get(key))) {
|
|
8
|
+
await delay(100);
|
|
9
|
+
if (Date.now() - startTime > timeout) {
|
|
10
|
+
throw new Error(`Debounce timed out [${String(key)}]`);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
try {
|
|
14
|
+
this.map.set(key, 1);
|
|
15
|
+
return await closure();
|
|
16
|
+
} finally {
|
|
17
|
+
this.map.delete(key);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
// src/SchemaCache.ts
|
|
23
|
+
import {
|
|
24
|
+
FetchClientError,
|
|
25
|
+
handleError,
|
|
26
|
+
isDefined as isDefined2,
|
|
27
|
+
isString
|
|
28
|
+
} from "@ariestools/sdk";
|
|
29
|
+
import { DomainPayloadWrapper, DomainSchemaResolver } from "@xyo-network/domain-payload-plugin";
|
|
30
|
+
import { SchemaSchema } from "@xyo-network/schema-payload-plugin";
|
|
31
|
+
import { Ajv } from "ajv";
|
|
32
|
+
|
|
33
|
+
// src/TtlLruCache.ts
|
|
34
|
+
var TtlLruCache = class {
|
|
35
|
+
map = /* @__PURE__ */ new Map();
|
|
36
|
+
max;
|
|
37
|
+
ttl;
|
|
38
|
+
constructor(opts) {
|
|
39
|
+
this.max = opts.max;
|
|
40
|
+
this.ttl = opts.ttl;
|
|
41
|
+
}
|
|
42
|
+
get(key) {
|
|
43
|
+
const entry = this.map.get(key);
|
|
44
|
+
if (entry === void 0) return void 0;
|
|
45
|
+
if (entry.expires <= Date.now()) {
|
|
46
|
+
this.map.delete(key);
|
|
47
|
+
return void 0;
|
|
48
|
+
}
|
|
49
|
+
this.map.delete(key);
|
|
50
|
+
this.map.set(key, entry);
|
|
51
|
+
return entry.value;
|
|
52
|
+
}
|
|
53
|
+
set(key, value) {
|
|
54
|
+
if (this.map.has(key)) this.map.delete(key);
|
|
55
|
+
this.map.set(key, { value, expires: Date.now() + this.ttl });
|
|
56
|
+
if (this.map.size > this.max) {
|
|
57
|
+
const oldest = this.map.keys().next().value;
|
|
58
|
+
if (oldest !== void 0) this.map.delete(oldest);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
// src/SchemaCache.ts
|
|
64
|
+
var getSchemaNameFromSchema = (schema) => {
|
|
65
|
+
if (isString(schema.$id)) {
|
|
66
|
+
return schema.$id;
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
var SchemaCache = class _SchemaCache {
|
|
70
|
+
/**
|
|
71
|
+
* Object representing `null` since LRU Cache types
|
|
72
|
+
* only allow for types that derive from object
|
|
73
|
+
*/
|
|
74
|
+
static NULL = { payload: { definition: {}, schema: SchemaSchema } };
|
|
75
|
+
static _instance;
|
|
76
|
+
_cache = new TtlLruCache({ max: 500, ttl: 1e3 * 60 * 5 });
|
|
77
|
+
_resolutions = new TtlLruCache({ max: 500, ttl: 1e3 * 60 * 5 });
|
|
78
|
+
_validators = {};
|
|
79
|
+
// prevents double discovery
|
|
80
|
+
getDebounce = new Debounce();
|
|
81
|
+
onSchemaCached;
|
|
82
|
+
proxy;
|
|
83
|
+
constructor(proxy) {
|
|
84
|
+
this.proxy = proxy;
|
|
85
|
+
}
|
|
86
|
+
static get instance() {
|
|
87
|
+
this._instance ??= new _SchemaCache();
|
|
88
|
+
return this._instance;
|
|
89
|
+
}
|
|
90
|
+
cacheSchemaIfValid(entry) {
|
|
91
|
+
if (!isDefined2(entry?.payload?.definition)) {
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
const ajv = new Ajv({ strict: false });
|
|
95
|
+
const validator = ajv.compile(entry.payload.definition);
|
|
96
|
+
const schemaName = getSchemaNameFromSchema(entry.payload.definition);
|
|
97
|
+
if (isString(schemaName)) {
|
|
98
|
+
this._cache.set(schemaName, entry);
|
|
99
|
+
const key = schemaName;
|
|
100
|
+
this._validators[key] = validator;
|
|
101
|
+
this.onSchemaCached?.(schemaName, entry);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
cacheSchemas(aliasEntries) {
|
|
105
|
+
for (const entry of aliasEntries?.filter((entry2) => entry2.payload.schema === SchemaSchema) ?? []) {
|
|
106
|
+
this.cacheSchemaIfValid(entry);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
async fetchSchema(schema) {
|
|
110
|
+
try {
|
|
111
|
+
const resolution = await this.resolve(schema);
|
|
112
|
+
if (resolution?.status === "resolved") {
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
const domain = await DomainPayloadWrapper.discover(schema, this.proxy);
|
|
116
|
+
await domain?.fetch();
|
|
117
|
+
this.cacheSchemas(domain?.aliases);
|
|
118
|
+
if (this._cache.get(schema) === void 0 && resolution?.status !== "unreachable") {
|
|
119
|
+
this._cache.set(schema, _SchemaCache.NULL);
|
|
120
|
+
}
|
|
121
|
+
} catch (error) {
|
|
122
|
+
if (error instanceof FetchClientError) {
|
|
123
|
+
console.log(`Fetch Url: ${error.config.url}`);
|
|
124
|
+
}
|
|
125
|
+
handleError(error, (error2) => {
|
|
126
|
+
console.error(`fetchSchema threw: ${error2.message}`);
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* A map of cached schema (by name) to payload validators for the schema. A schema
|
|
132
|
+
* must be cached via `get('schema.name')` before it's validator can be used as
|
|
133
|
+
* they are compiled dynamically at runtime upon retrieval.
|
|
134
|
+
*/
|
|
135
|
+
get validators() {
|
|
136
|
+
return this._validators;
|
|
137
|
+
}
|
|
138
|
+
async get(schema) {
|
|
139
|
+
if (isString(schema)) {
|
|
140
|
+
await this.getDebounce.one(schema, async () => {
|
|
141
|
+
if (this._cache.get(schema) === void 0) {
|
|
142
|
+
await this.fetchSchema(schema);
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
const value = this._cache.get(schema);
|
|
146
|
+
return value === _SchemaCache.NULL ? null : value;
|
|
147
|
+
}
|
|
148
|
+
return void 0;
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Resolve a schema name to its domain-published authoritative definition (SPEC.md in
|
|
152
|
+
* @xyo-network/domain-payload-plugin), with provenance and a richer status than get():
|
|
153
|
+
* 'resolved' | 'not-published' | 'unreachable'. Resolved definitions are cached and
|
|
154
|
+
* their validators compiled, exactly as with get(). 'unreachable' results are
|
|
155
|
+
* retryable: they are not cached as permanent negatives.
|
|
156
|
+
*/
|
|
157
|
+
async resolve(schema) {
|
|
158
|
+
if (!isString(schema)) return void 0;
|
|
159
|
+
await this.getDebounce.one(`resolve:${schema}`, async () => {
|
|
160
|
+
const existing = this._resolutions.get(schema);
|
|
161
|
+
if (existing === void 0 || existing.status === "unreachable") {
|
|
162
|
+
const resolver = new DomainSchemaResolver();
|
|
163
|
+
const resolution = await resolver.resolve(schema, { proxy: this.proxy });
|
|
164
|
+
this._resolutions.set(schema, resolution);
|
|
165
|
+
if (resolution.status === "resolved" && isDefined2(resolution.payload)) {
|
|
166
|
+
this.cacheSchemaIfValid({ payload: resolution.payload, source: resolution.source });
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
});
|
|
170
|
+
return this._resolutions.get(schema);
|
|
171
|
+
}
|
|
172
|
+
};
|
|
173
|
+
export {
|
|
174
|
+
Debounce,
|
|
175
|
+
SchemaCache,
|
|
176
|
+
TtlLruCache
|
|
177
|
+
};
|
|
3
178
|
//# sourceMappingURL=index.mjs.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
|
-
"sources": ["../../src/
|
|
4
|
-
"sourcesContent": ["
|
|
5
|
-
"mappings": ";
|
|
6
|
-
"names": []
|
|
3
|
+
"sources": ["../../src/Debounce.ts", "../../src/SchemaCache.ts", "../../src/TtlLruCache.ts"],
|
|
4
|
+
"sourcesContent": ["import { delay, isDefined } from '@ariestools/sdk'\n\nexport class Debounce<TKey = string> {\n private map = new Map<TKey, number>()\n\n async one<T>(key: TKey, closure: () => Promise<T>, timeout = 10_000) {\n const startTime = Date.now()\n while (isDefined(this.map.get(key))) {\n await delay(100)\n if (Date.now() - startTime > timeout) {\n throw new Error(`Debounce timed out [${String(key)}]`)\n }\n }\n try {\n this.map.set(key, 1)\n return await closure()\n } finally {\n // delete (not set 0): isDefined(0) is true, so a leftover 0 would make every\n // subsequent call with this key spin until timeout\n this.map.delete(key)\n }\n }\n}\n", "import {\n FetchClientError,\n handleError, isDefined, isString,\n} from '@ariestools/sdk'\nimport type { SchemaResolution, SchemaResolutionSource } from '@xyo-network/domain-payload-plugin'\nimport { DomainPayloadWrapper, DomainSchemaResolver } from '@xyo-network/domain-payload-plugin'\nimport type { FetchedPayload } from '@xyo-network/huri'\nimport type { SchemaPayload } from '@xyo-network/schema-payload-plugin'\nimport { SchemaSchema } from '@xyo-network/schema-payload-plugin'\nimport type { SchemaObject } from 'ajv'\nimport { Ajv } from 'ajv'\n\nimport { Debounce } from './Debounce.ts'\nimport type { SchemaNameToValidatorMap } from './SchemaNameToValidatorMap.ts'\nimport { TtlLruCache } from './TtlLruCache.ts'\n\nconst getSchemaNameFromSchema = (schema: SchemaObject) => {\n if (isString(schema.$id)) {\n return schema.$id\n }\n}\n\nexport interface SchemaCacheEntry extends FetchedPayload<SchemaPayload> {\n /** Provenance of the schema definition when resolved via the domain schema authority */\n source?: SchemaResolutionSource\n}\n\nexport class SchemaCache<T extends SchemaNameToValidatorMap = SchemaNameToValidatorMap> {\n /**\n * Object representing `null` since LRU Cache types\n * only allow for types that derive from object\n */\n protected static readonly NULL: SchemaCacheEntry = { payload: { definition: {}, schema: SchemaSchema } }\n\n private static _instance?: SchemaCache\n\n private _cache = new TtlLruCache<string, SchemaCacheEntry>({ max: 500, ttl: 1000 * 60 * 5 })\n private _resolutions = new TtlLruCache<string, SchemaResolution>({ max: 500, ttl: 1000 * 60 * 5 })\n private _validators: T = {} as T\n\n // prevents double discovery\n private getDebounce = new Debounce()\n\n onSchemaCached?: (name: string, entry: SchemaCacheEntry) => void\n proxy?: string\n\n private constructor(proxy?: string) {\n this.proxy = proxy\n }\n\n static get instance() {\n this._instance ??= new SchemaCache()\n return this._instance\n }\n\n private cacheSchemaIfValid(entry: SchemaCacheEntry) {\n if (!isDefined(entry?.payload?.definition)) {\n return\n }\n\n const ajv = new Ajv({ strict: false })\n // check if it is a valid schema def\n const validator = ajv.compile(entry.payload.definition)\n const schemaName = getSchemaNameFromSchema(entry.payload.definition)\n if (isString(schemaName)) {\n this._cache.set(schemaName, entry)\n const key = schemaName as keyof T\n this._validators[key] = validator as unknown as T[keyof T]\n this.onSchemaCached?.(schemaName, entry)\n }\n }\n\n private cacheSchemas(aliasEntries?: FetchedPayload[] | null) {\n for (const entry of aliasEntries?.filter(entry => entry.payload.schema === SchemaSchema) ?? []) {\n this.cacheSchemaIfValid(entry as SchemaCacheEntry)\n }\n }\n\n private async fetchSchema(schema: string) {\n try {\n // domain schema authority resolution first (DNS records, xyo.json, xyo. subdomain)\n const resolution = await this.resolve(schema)\n if (resolution?.status === 'resolved') {\n return\n }\n\n // legacy discovery fallback; also opportunistically warms sibling schemas\n const domain = await DomainPayloadWrapper.discover(schema, this.proxy)\n await domain?.fetch()\n this.cacheSchemas(domain?.aliases)\n\n // if it is still undefined, mark it as null (not found) \u2014 but never cache transport\n // failures as permanent negatives; 'unreachable' stays undefined so it is retried\n if (this._cache.get(schema) === undefined && resolution?.status !== 'unreachable') {\n this._cache.set(schema, SchemaCache.NULL)\n }\n } catch (error) {\n // transport failure: leave the entry undefined so a future get() retries\n if (error instanceof FetchClientError) {\n console.log(`Fetch Url: ${error.config.url}`)\n }\n handleError(error, (error) => {\n console.error(`fetchSchema threw: ${error.message}`)\n })\n }\n }\n\n /**\n * A map of cached schema (by name) to payload validators for the schema. A schema\n * must be cached via `get('schema.name')` before it's validator can be used as\n * they are compiled dynamically at runtime upon retrieval.\n */\n get validators(): T {\n return this._validators\n }\n\n async get(schema?: string): Promise<SchemaCacheEntry | undefined | null> {\n if (isString(schema)) {\n await this.getDebounce.one(schema, async () => {\n // If we've never looked for it before, it will be undefined\n if (this._cache.get(schema) === undefined) {\n await this.fetchSchema(schema)\n }\n })\n const value = this._cache.get(schema)\n return value === SchemaCache.NULL ? null : value\n }\n return undefined\n }\n\n /**\n * Resolve a schema name to its domain-published authoritative definition (SPEC.md in\n * @xyo-network/domain-payload-plugin), with provenance and a richer status than get():\n * 'resolved' | 'not-published' | 'unreachable'. Resolved definitions are cached and\n * their validators compiled, exactly as with get(). 'unreachable' results are\n * retryable: they are not cached as permanent negatives.\n */\n async resolve(schema?: string): Promise<SchemaResolution | undefined> {\n if (!isString(schema)) return undefined\n await this.getDebounce.one(`resolve:${schema}`, async () => {\n const existing = this._resolutions.get(schema)\n if (existing === undefined || existing.status === 'unreachable') {\n const resolver = new DomainSchemaResolver()\n const resolution = await resolver.resolve(schema, { proxy: this.proxy })\n this._resolutions.set(schema, resolution)\n if (resolution.status === 'resolved' && isDefined(resolution.payload)) {\n this.cacheSchemaIfValid({ payload: resolution.payload, source: resolution.source })\n }\n }\n })\n return this._resolutions.get(schema)\n }\n}\n", "export interface TtlLruCacheOpts {\n max: number\n ttl: number\n}\n\nexport class TtlLruCache<K, V> {\n private readonly map = new Map<K, { expires: number; value: V }>()\n private readonly max: number\n private readonly ttl: number\n\n constructor(opts: TtlLruCacheOpts) {\n this.max = opts.max\n this.ttl = opts.ttl\n }\n\n get(key: K): V | undefined {\n const entry = this.map.get(key)\n if (entry === undefined) return undefined\n if (entry.expires <= Date.now()) {\n this.map.delete(key)\n return undefined\n }\n this.map.delete(key)\n this.map.set(key, entry)\n return entry.value\n }\n\n set(key: K, value: V): void {\n if (this.map.has(key)) this.map.delete(key)\n this.map.set(key, { value, expires: Date.now() + this.ttl })\n if (this.map.size > this.max) {\n const oldest = this.map.keys().next().value\n if (oldest !== undefined) this.map.delete(oldest)\n }\n }\n}\n"],
|
|
5
|
+
"mappings": ";AAAA,SAAS,OAAO,iBAAiB;AAE1B,IAAM,WAAN,MAA8B;AAAA,EAC3B,MAAM,oBAAI,IAAkB;AAAA,EAEpC,MAAM,IAAO,KAAW,SAA2B,UAAU,K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,MAAM,EAAG,QAAO;AAC9B,UAAM,KAAK,YAAY,IAAI,WAAW,MAAM,IAAI,YAAY;AAC1D,YAAM,WAAW,KAAK,aAAa,IAAI,MAAM;AAC7C,UAAI,aAAa,UAAa,SAAS,WAAW,eAAe;AAC/D,cAAM,WAAW,IAAI,qBAAqB;AAC1C,cAAM,aAAa,MAAM,SAAS,QAAQ,QAAQ,EAAE,OAAO,KAAK,MAAM,CAAC;AACvE,aAAK,aAAa,IAAI,QAAQ,UAAU;AACxC,YAAI,WAAW,WAAW,cAAcF,WAAU,WAAW,OAAO,GAAG;AACrE,eAAK,mBAAmB,EAAE,SAAS,WAAW,SAAS,QAAQ,WAAW,OAAO,CAAC;AAAA,QACpF;AAAA,MACF;AAAA,IACF,CAAC;AACD,WAAO,KAAK,aAAa,IAAI,MAAM;AAAA,EACrC;AACF;",
|
|
6
|
+
"names": ["isDefined", "isDefined", "entry", "error"]
|
|
7
7
|
}
|
package/package.json
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xyo-network/schema-cache",
|
|
3
|
-
"version": "7.0.
|
|
4
|
-
"description": "
|
|
5
|
-
"deprecated": "Use @xyo-network/sdk-protocol/schema-cache instead. @xyo-network/schema-cache is a backward-compatibility re-export stub and will not receive further updates.",
|
|
3
|
+
"version": "7.0.13",
|
|
4
|
+
"description": "Primary SDK for using XYO Protocol 2.0",
|
|
6
5
|
"homepage": "https://xyo.network",
|
|
7
6
|
"bugs": {
|
|
8
7
|
"url": "git+https://github.com/XYOracleNetwork/sdk-xyo-client-js/issues",
|
|
@@ -35,34 +34,52 @@
|
|
|
35
34
|
"README.md"
|
|
36
35
|
],
|
|
37
36
|
"dependencies": {
|
|
38
|
-
"@xyo-network/
|
|
37
|
+
"@xyo-network/domain-payload-plugin": "~7.0.13",
|
|
38
|
+
"@xyo-network/huri": "~7.0.13",
|
|
39
|
+
"@xyo-network/schema-payload-plugin": "~7.0.13",
|
|
40
|
+
"@xyo-network/payload-model": "~7.0.13"
|
|
39
41
|
},
|
|
40
42
|
"devDependencies": {
|
|
41
|
-
"@
|
|
42
|
-
"@
|
|
43
|
-
"
|
|
44
|
-
"
|
|
45
|
-
"
|
|
46
|
-
"
|
|
43
|
+
"@ariestools/sdk": "~7.0.8",
|
|
44
|
+
"@bitauth/libauth": "~3.0.0",
|
|
45
|
+
"@opentelemetry/api": "~1.9.1",
|
|
46
|
+
"@opentelemetry/sdk-trace-base": "~2.9.0",
|
|
47
|
+
"@scure/base": "~2.2.0",
|
|
48
|
+
"@xylabs/threads": "~7.0.8",
|
|
49
|
+
"@xylabs/toolchain": "~8.6.2",
|
|
50
|
+
"@xylabs/tsconfig": "~8.6.2",
|
|
51
|
+
"@xylabs/tsconfig-dom": "~8.6.2",
|
|
52
|
+
"@xylabs/vitest-extended": "~7.0.8",
|
|
53
|
+
"ajv": "~8.20.0",
|
|
54
|
+
"async-mutex": "~0.5.0",
|
|
55
|
+
"browserslist": "~4.28.4",
|
|
56
|
+
"debug": "~4.4.3",
|
|
57
|
+
"eslint": "~10.6.0",
|
|
58
|
+
"eslint-import-resolver-typescript": "~4.4.5",
|
|
59
|
+
"ethers": "~6.17.0",
|
|
60
|
+
"hash-wasm": "~4.12.0",
|
|
61
|
+
"observable-fns": "~0.6.1",
|
|
62
|
+
"typescript": "~6.0.3",
|
|
63
|
+
"vite": "~8.1.3",
|
|
64
|
+
"vitest": "~4.1.9",
|
|
65
|
+
"zod": "~4.4.3",
|
|
66
|
+
"@xyo-network/network": "~7.0.13",
|
|
67
|
+
"@xyo-network/payload-builder": "~7.0.13"
|
|
47
68
|
},
|
|
48
69
|
"peerDependencies": {
|
|
49
|
-
"@ariestools/sdk": "^7.0.
|
|
50
|
-
"@bitauth/libauth": "
|
|
51
|
-
"@
|
|
52
|
-
"@opentelemetry/
|
|
53
|
-
"@
|
|
54
|
-
"@
|
|
55
|
-
"
|
|
56
|
-
"
|
|
57
|
-
"
|
|
58
|
-
"
|
|
59
|
-
"
|
|
60
|
-
"
|
|
61
|
-
"
|
|
62
|
-
"hash-wasm": "~4.12",
|
|
63
|
-
"idb": "^8.0",
|
|
64
|
-
"observable-fns": "^0.6",
|
|
65
|
-
"zod": "^4.4"
|
|
70
|
+
"@ariestools/sdk": "^7.0.8",
|
|
71
|
+
"@bitauth/libauth": "^3.0.0",
|
|
72
|
+
"@opentelemetry/api": "^1.9.1",
|
|
73
|
+
"@opentelemetry/sdk-trace-base": "^2.9.0",
|
|
74
|
+
"@scure/base": "^2.2.0",
|
|
75
|
+
"@xylabs/threads": "^7.0.8",
|
|
76
|
+
"ajv": "^8.20.0",
|
|
77
|
+
"async-mutex": "^0.5.0",
|
|
78
|
+
"debug": "^4.4.3",
|
|
79
|
+
"ethers": "^6.17.0",
|
|
80
|
+
"hash-wasm": "^4.12.0",
|
|
81
|
+
"observable-fns": "^0.6.1",
|
|
82
|
+
"zod": "^4.4.3"
|
|
66
83
|
},
|
|
67
84
|
"engines": {
|
|
68
85
|
"node": "^24"
|