@xyo-network/schema-cache 7.0.3 → 7.0.4
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/neutral/index.mjs +2 -2
- package/dist/neutral/index.mjs.map +1 -1
- package/package.json +18 -16
package/dist/neutral/index.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// src/Debounce.ts
|
|
2
|
-
import { delay, isDefined } from "@xylabs/sdk
|
|
2
|
+
import { delay, isDefined } from "@xylabs/sdk";
|
|
3
3
|
var Debounce = class {
|
|
4
4
|
map = /* @__PURE__ */ new Map();
|
|
5
5
|
async one(key, closure, timeout = 1e4) {
|
|
@@ -25,7 +25,7 @@ import {
|
|
|
25
25
|
handleError,
|
|
26
26
|
isDefined as isDefined2,
|
|
27
27
|
isString
|
|
28
|
-
} from "@xylabs/sdk
|
|
28
|
+
} from "@xylabs/sdk";
|
|
29
29
|
import { DomainPayloadWrapper, DomainSchemaResolver } from "@xyo-network/domain-payload-plugin";
|
|
30
30
|
import { SchemaSchema } from "@xyo-network/schema-payload-plugin";
|
|
31
31
|
import { Ajv } from "ajv";
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/Debounce.ts", "../../src/SchemaCache.ts", "../../src/TtlLruCache.ts"],
|
|
4
|
-
"sourcesContent": ["import { delay, isDefined } from '@xylabs/sdk
|
|
4
|
+
"sourcesContent": ["import { delay, isDefined } from '@xylabs/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 '@xylabs/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
5
|
"mappings": ";AAAA,SAAS,OAAO,iBAAiB;AAE1B,IAAM,WAAN,MAA8B;AAAA,EAC3B,MAAM,oBAAI,IAAkB;AAAA,EAEpC,MAAM,IAAO,KAAW,SAA2B,UAAU,KAAQ;AACnE,UAAM,YAAY,KAAK,IAAI;AAC3B,WAAO,UAAU,KAAK,IAAI,IAAI,GAAG,CAAC,GAAG;AACnC,YAAM,MAAM,GAAG;AACf,UAAI,KAA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,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
6
|
"names": ["isDefined", "isDefined", "entry", "error"]
|
|
7
7
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xyo-network/schema-cache",
|
|
3
|
-
"version": "7.0.
|
|
3
|
+
"version": "7.0.4",
|
|
4
4
|
"description": "Primary SDK for using XYO Protocol 2.0",
|
|
5
5
|
"homepage": "https://xyo.network",
|
|
6
6
|
"bugs": {
|
|
@@ -34,26 +34,28 @@
|
|
|
34
34
|
"README.md"
|
|
35
35
|
],
|
|
36
36
|
"dependencies": {
|
|
37
|
-
"@xyo-network/
|
|
38
|
-
"@xyo-network/
|
|
39
|
-
"@xyo-network/payload-model": "~7.0.
|
|
40
|
-
"@xyo-network/schema-payload-plugin": "~7.0.
|
|
37
|
+
"@xyo-network/huri": "~7.0.4",
|
|
38
|
+
"@xyo-network/domain-payload-plugin": "~7.0.4",
|
|
39
|
+
"@xyo-network/payload-model": "~7.0.4",
|
|
40
|
+
"@xyo-network/schema-payload-plugin": "~7.0.4"
|
|
41
41
|
},
|
|
42
42
|
"devDependencies": {
|
|
43
43
|
"@bitauth/libauth": "~3.0.0",
|
|
44
44
|
"@opentelemetry/api": "^1.9.1",
|
|
45
45
|
"@opentelemetry/sdk-trace-base": "^2.8.0",
|
|
46
46
|
"@scure/base": "~2.2.0",
|
|
47
|
-
"@xylabs/sdk
|
|
48
|
-
"@xylabs/threads": "
|
|
49
|
-
"@xylabs/toolchain": "~8.3
|
|
50
|
-
"@xylabs/tsconfig": "~8.3
|
|
51
|
-
"@xylabs/tsconfig-dom": "~8.3
|
|
52
|
-
"@xylabs/vitest-extended": "^
|
|
47
|
+
"@xylabs/sdk": "^7.0.1",
|
|
48
|
+
"@xylabs/threads": "~7.0.1",
|
|
49
|
+
"@xylabs/toolchain": "~8.5.3",
|
|
50
|
+
"@xylabs/tsconfig": "~8.5.3",
|
|
51
|
+
"@xylabs/tsconfig-dom": "~8.5.3",
|
|
52
|
+
"@xylabs/vitest-extended": "^7.0.1",
|
|
53
53
|
"ajv": "^8.20.0",
|
|
54
54
|
"async-mutex": "^0.5.0",
|
|
55
|
+
"browserslist": "4.28.4",
|
|
55
56
|
"debug": "^4.4.3",
|
|
56
|
-
"eslint": "^10.
|
|
57
|
+
"eslint": "^10.6.0",
|
|
58
|
+
"eslint-import-resolver-typescript": "^4.4.5",
|
|
57
59
|
"ethers": "^6.17.0",
|
|
58
60
|
"hash-wasm": "~4.12.0",
|
|
59
61
|
"observable-fns": "^0.6.1",
|
|
@@ -61,16 +63,16 @@
|
|
|
61
63
|
"vite": "^8.1.0",
|
|
62
64
|
"vitest": "~4.1.9",
|
|
63
65
|
"zod": "^4.4.3",
|
|
64
|
-
"@xyo-network/network": "~7.0.
|
|
65
|
-
"@xyo-network/payload-builder": "~7.0.
|
|
66
|
+
"@xyo-network/network": "~7.0.4",
|
|
67
|
+
"@xyo-network/payload-builder": "~7.0.4"
|
|
66
68
|
},
|
|
67
69
|
"peerDependencies": {
|
|
68
70
|
"@bitauth/libauth": "~3.0",
|
|
69
71
|
"@opentelemetry/api": "^1.9",
|
|
70
72
|
"@opentelemetry/sdk-trace-base": "^2.7",
|
|
71
73
|
"@scure/base": "~2.2",
|
|
72
|
-
"@xylabs/sdk
|
|
73
|
-
"@xylabs/threads": "^
|
|
74
|
+
"@xylabs/sdk": "^7.0",
|
|
75
|
+
"@xylabs/threads": "^7.0",
|
|
74
76
|
"ajv": "^8.20",
|
|
75
77
|
"async-mutex": "^0.5",
|
|
76
78
|
"debug": "^4.4",
|