@xyo-network/schema-cache 5.3.22 → 5.3.25

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/schema-cache",
3
- "version": "5.3.22",
3
+ "version": "5.3.25",
4
4
  "description": "Primary SDK for using XYO Protocol 2.0",
5
5
  "homepage": "https://xyo.network",
6
6
  "bugs": {
@@ -30,43 +30,43 @@
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/domain-payload-plugin": "~5.3.22",
41
- "@xyo-network/huri": "~5.3.22",
42
- "@xyo-network/payload-model": "~5.3.22",
43
- "@xyo-network/schema-payload-plugin": "~5.3.22"
39
+ "@xyo-network/payload-model": "~5.3.25",
40
+ "@xyo-network/schema-payload-plugin": "~5.3.25",
41
+ "@xyo-network/domain-payload-plugin": "~5.3.25",
42
+ "@xyo-network/huri": "~5.3.25"
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
- "@xylabs/tsconfig-dom": "~7.6.8",
53
- "@xylabs/vitest-extended": "~5.0.91",
54
- "@xyo-network/network": "~5.3.22",
55
- "@xyo-network/payload-builder": "~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",
51
+ "@xylabs/tsconfig-dom": "~7.6.16",
52
+ "@xylabs/vitest-extended": "~5.0.93",
56
53
  "acorn": "^8.16.0",
57
54
  "ajv": "^8.18.0",
58
55
  "axios": "^1.14.0",
59
- "cosmiconfig": "^9.0.1",
60
- "esbuild": "^0.27.4",
61
- "eslint": "^10.1.0",
56
+ "esbuild": "^0.28.0",
62
57
  "ethers": "^6.16.0",
63
58
  "lru-cache": "^11.2.7",
64
- "rollup": "^4.60.1",
65
59
  "tslib": "^2.8.1",
66
60
  "typescript": "~5.9.3",
67
61
  "vite": "^8.0.3",
68
62
  "vitest": "~4.1.2",
69
- "zod": "^4.3.6"
63
+ "zod": "^4.3.6",
64
+ "@xyo-network/domain-payload-plugin": "~5.3.25",
65
+ "@xyo-network/huri": "~5.3.25",
66
+ "@xyo-network/network": "~5.3.25",
67
+ "@xyo-network/schema-payload-plugin": "~5.3.25",
68
+ "@xyo-network/payload-model": "~5.3.25",
69
+ "@xyo-network/payload-builder": "~5.3.25"
70
70
  },
71
71
  "peerDependencies": {
72
72
  "@xylabs/sdk-js": "^5",
@@ -80,4 +80,4 @@
80
80
  "publishConfig": {
81
81
  "access": "public"
82
82
  }
83
- }
83
+ }
package/src/Debounce.ts DELETED
@@ -1,21 +0,0 @@
1
- import { delay, isDefined } from '@xylabs/sdk-js'
2
-
3
- export class Debounce<TKey = string> {
4
- private map = new Map<TKey, number>()
5
-
6
- async one<T>(key: TKey, closure: () => Promise<T>, timeout = 10_000) {
7
- const startTime = Date.now()
8
- while (isDefined(this.map.get(key))) {
9
- await delay(100)
10
- if (Date.now() - startTime > timeout) {
11
- throw new Error(`Debounce timed out [${key}]`)
12
- }
13
- }
14
- try {
15
- this.map.set(key, 1)
16
- return await closure()
17
- } finally {
18
- this.map.set(key, 0)
19
- }
20
- }
21
- }
@@ -1,119 +0,0 @@
1
- import {
2
- handleError, isDefined, isString,
3
- } from '@xylabs/sdk-js'
4
- import { DomainPayloadWrapper } from '@xyo-network/domain-payload-plugin'
5
- import type { FetchedPayload } from '@xyo-network/huri'
6
- import type { SchemaPayload } from '@xyo-network/schema-payload-plugin'
7
- import { SchemaSchema } from '@xyo-network/schema-payload-plugin'
8
- import type { SchemaObject } from 'ajv'
9
- import { Ajv } from 'ajv'
10
- import { isAxiosError } from 'axios'
11
- import { LRUCache } from 'lru-cache'
12
-
13
- import { Debounce } from './Debounce.ts'
14
- import type { SchemaNameToValidatorMap } from './SchemaNameToValidatorMap.ts'
15
-
16
- const getSchemaNameFromSchema = (schema: SchemaObject) => {
17
- if (isString(schema.$id)) {
18
- return schema.$id
19
- }
20
- }
21
-
22
- export type SchemaCacheEntry = FetchedPayload<SchemaPayload>
23
-
24
- export class SchemaCache<T extends SchemaNameToValidatorMap = SchemaNameToValidatorMap> {
25
- /**
26
- * Object representing `null` since LRU Cache types
27
- * only allow for types that derive from object
28
- */
29
- protected static readonly NULL: SchemaCacheEntry = { payload: { definition: {}, schema: SchemaSchema } }
30
-
31
- private static _instance?: SchemaCache
32
-
33
- onSchemaCached?: (name: string, entry: SchemaCacheEntry) => void
34
- proxy?: string
35
-
36
- private _cache = new LRUCache<string, SchemaCacheEntry>({ max: 500, ttl: 1000 * 60 * 5 })
37
- private _validators: T = {} as T
38
-
39
- // prevents double discovery
40
- private getDebounce = new Debounce()
41
-
42
- private constructor(proxy?: string) {
43
- this.proxy = proxy
44
- }
45
-
46
- static get instance() {
47
- if (!this._instance) {
48
- this._instance = new SchemaCache()
49
- }
50
- return this._instance
51
- }
52
-
53
- /**
54
- * A map of cached schema (by name) to payload validators for the schema. A schema
55
- * must be cached via `get('schema.name')` before it's validator can be used as
56
- * they are compiled dynamically at runtime upon retrieval.
57
- */
58
- get validators(): T {
59
- return this._validators
60
- }
61
-
62
- async get(schema?: string): Promise<SchemaCacheEntry | undefined | null> {
63
- if (isString(schema)) {
64
- await this.getDebounce.one(schema, async () => {
65
- // If we've never looked for it before, it will be undefined
66
- if (this._cache.get(schema) === undefined) {
67
- await this.fetchSchema(schema)
68
- }
69
- })
70
- const value = this._cache.get(schema)
71
- return value === SchemaCache.NULL ? null : value
72
- }
73
- return undefined
74
- }
75
-
76
- private cacheSchemaIfValid(entry: SchemaCacheEntry) {
77
- // only store them if they match the schema root
78
- if (isDefined(entry?.payload?.definition)) {
79
- const ajv = new Ajv({ strict: false })
80
- // check if it is a valid schema def
81
- const validator = ajv.compile(entry.payload.definition)
82
- const schemaName = getSchemaNameFromSchema(entry.payload.definition)
83
- if (isString(schemaName)) {
84
- this._cache.set(schemaName, entry)
85
- const key = schemaName as keyof T
86
- this._validators[key] = validator as unknown as T[keyof T]
87
- this.onSchemaCached?.(schemaName, entry)
88
- }
89
- }
90
- }
91
-
92
- private cacheSchemas(aliasEntries?: FetchedPayload[] | null) {
93
- for (const entry of aliasEntries?.filter(entry => entry.payload.schema === SchemaSchema) ?? []) {
94
- this.cacheSchemaIfValid(entry as SchemaCacheEntry)
95
- }
96
- }
97
-
98
- private async fetchSchema(schema: string) {
99
- try {
100
- const domain = await DomainPayloadWrapper.discover(schema, this.proxy)
101
- await domain?.fetch()
102
- this.cacheSchemas(domain?.aliases)
103
-
104
- // if it is still undefined, mark it as null (not found)
105
- if (this._cache.get(schema) === undefined) {
106
- this._cache.set(schema, SchemaCache.NULL)
107
- }
108
- } catch (error) {
109
- // if failed, set it to NULL, TODO: Make an entry for an error to try again in the future?
110
- this._cache.set(schema, SchemaCache.NULL)
111
- if (isAxiosError(error)) {
112
- console.log(`Axios Url: ${error.response?.config.url}`)
113
- }
114
- handleError(error, (error) => {
115
- console.error(`fetchSchema threw: ${error.message}`)
116
- })
117
- }
118
- }
119
- }
@@ -1,18 +0,0 @@
1
- import type { DomainPayload, DomainSchema } from '@xyo-network/domain-payload-plugin'
2
- import type { Payload, PayloadSchema } from '@xyo-network/payload-model'
3
- import type { SchemaPayload, SchemaSchema } from '@xyo-network/schema-payload-plugin'
4
-
5
- /**
6
- * Used in conjunction with schema validation to support compile time type assertion
7
- * for known schema types.
8
- */
9
- export type NarrowPayload<T extends Payload = Payload> = ((x: Payload) => x is T) | undefined
10
-
11
- /**
12
- * Used to map known schemas (byt their string name) to the validators which assert their types
13
- */
14
- export interface SchemaNameToValidatorMap {
15
- [DomainSchema]: NarrowPayload<DomainPayload>
16
- [PayloadSchema]: NarrowPayload<Payload>
17
- [SchemaSchema]: NarrowPayload<SchemaPayload>
18
- }
package/src/index.ts DELETED
@@ -1,3 +0,0 @@
1
- export * from './Debounce.ts'
2
- export * from './SchemaCache.ts'
3
- export * from './SchemaNameToValidatorMap.ts'