@xyo-network/schema-cache 2.110.18 → 2.111.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/index.cjs +1 -1
- package/dist/browser/index.cjs.map +1 -1
- package/dist/browser/{index.js → index.mjs} +2 -2
- package/dist/browser/index.mjs.map +1 -0
- package/dist/neutral/index.cjs +1 -1
- package/dist/neutral/index.cjs.map +1 -1
- package/dist/neutral/{index.js → index.mjs} +2 -2
- package/dist/neutral/index.mjs.map +1 -0
- package/dist/node/index.cjs +1 -1
- package/dist/node/index.cjs.map +1 -1
- package/dist/node/{index.js → index.mjs} +2 -2
- package/dist/node/index.mjs.map +1 -0
- package/package.json +16 -16
- package/src/SchemaCache.ts +6 -6
- package/dist/browser/index.js.map +0 -1
- package/dist/neutral/index.js.map +0 -1
- package/dist/node/index.js.map +0 -1
package/dist/browser/index.cjs
CHANGED
|
@@ -69,7 +69,7 @@ var SchemaCache = class _SchemaCache {
|
|
|
69
69
|
proxy;
|
|
70
70
|
_cache = new import_lru_cache.LRUCache({ max: 500, ttl: 1e3 * 60 * 5 });
|
|
71
71
|
_validators = {};
|
|
72
|
-
//prevents double discovery
|
|
72
|
+
// prevents double discovery
|
|
73
73
|
getDebounce = new Debounce();
|
|
74
74
|
constructor(proxy) {
|
|
75
75
|
this.proxy = proxy;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/index.ts","../../src/Debounce.ts","../../src/SchemaCache.ts"],"sourcesContent":["export * from './Debounce.ts'\nexport * from './SchemaCache.ts'\nexport * from './SchemaNameToValidatorMap.ts'\n","import { delay } from '@xylabs/delay'\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 (this.map.get(key)) {\n await delay(100)\n if (Date.now() - startTime > timeout) {\n throw new Error(`Debounce timed out [${key}]`)\n }\n }\n try {\n this.map.set(key, 1)\n return await closure()\n } finally {\n this.map.set(key, 0)\n }\n }\n}\n","import { isAxiosError } from '@xylabs/axios'\nimport { handleError } from '@xylabs/error'\nimport { DomainPayloadWrapper } from '@xyo-network/domain-payload-plugin'\nimport { FetchedPayload } from '@xyo-network/huri'\nimport { SchemaPayload, SchemaSchema } from '@xyo-network/schema-payload-plugin'\nimport { Ajv, SchemaObject } from 'ajv'\nimport { LRUCache } from 'lru-cache'\n\nimport { Debounce } from './Debounce.ts'\nimport { SchemaNameToValidatorMap } from './SchemaNameToValidatorMap.ts'\n\nconst getSchemaNameFromSchema = (schema: SchemaObject) => {\n if (schema.$id) {\n return schema.$id\n }\n}\n\nexport type SchemaCacheEntry = FetchedPayload<SchemaPayload>\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 onSchemaCached?: (name: string, entry: SchemaCacheEntry) => void\n proxy?: string\n\n private _cache = new LRUCache<string, SchemaCacheEntry>({ max: 500, ttl: 1000 * 60 * 5 })\n private _validators: T = {} as T\n\n //prevents double discovery\n private getDebounce = new Debounce()\n\n private constructor(proxy?: string) {\n this.proxy = proxy\n }\n\n static get instance() {\n if (!this._instance) {\n this._instance = new SchemaCache()\n }\n return this._instance\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 (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 private cacheSchemaIfValid(entry: SchemaCacheEntry) {\n //only store them if they match the schema root\n if (entry.payload.definition) {\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 (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\n private cacheSchemas(aliasEntries?: FetchedPayload[] | null) {\n for (const entry of aliasEntries?.filter(
|
|
1
|
+
{"version":3,"sources":["../../src/index.ts","../../src/Debounce.ts","../../src/SchemaCache.ts"],"sourcesContent":["export * from './Debounce.ts'\nexport * from './SchemaCache.ts'\nexport * from './SchemaNameToValidatorMap.ts'\n","import { delay } from '@xylabs/delay'\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 (this.map.get(key)) {\n await delay(100)\n if (Date.now() - startTime > timeout) {\n throw new Error(`Debounce timed out [${key}]`)\n }\n }\n try {\n this.map.set(key, 1)\n return await closure()\n } finally {\n this.map.set(key, 0)\n }\n }\n}\n","import { isAxiosError } from '@xylabs/axios'\nimport { handleError } from '@xylabs/error'\nimport { DomainPayloadWrapper } from '@xyo-network/domain-payload-plugin'\nimport { FetchedPayload } from '@xyo-network/huri'\nimport { SchemaPayload, SchemaSchema } from '@xyo-network/schema-payload-plugin'\nimport { Ajv, SchemaObject } from 'ajv'\nimport { LRUCache } from 'lru-cache'\n\nimport { Debounce } from './Debounce.ts'\nimport { SchemaNameToValidatorMap } from './SchemaNameToValidatorMap.ts'\n\nconst getSchemaNameFromSchema = (schema: SchemaObject) => {\n if (schema.$id) {\n return schema.$id\n }\n}\n\nexport type SchemaCacheEntry = FetchedPayload<SchemaPayload>\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 onSchemaCached?: (name: string, entry: SchemaCacheEntry) => void\n proxy?: string\n\n private _cache = new LRUCache<string, SchemaCacheEntry>({ max: 500, ttl: 1000 * 60 * 5 })\n private _validators: T = {} as T\n\n // prevents double discovery\n private getDebounce = new Debounce()\n\n private constructor(proxy?: string) {\n this.proxy = proxy\n }\n\n static get instance() {\n if (!this._instance) {\n this._instance = new SchemaCache()\n }\n return this._instance\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 (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 private cacheSchemaIfValid(entry: SchemaCacheEntry) {\n // only store them if they match the schema root\n if (entry.payload.definition) {\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 (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\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 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)\n if (this._cache.get(schema) === undefined) {\n this._cache.set(schema, SchemaCache.NULL)\n }\n } catch (error) {\n // if failed, set it to NULL, TODO: Make an entry for an error to try again in the future?\n this._cache.set(schema, SchemaCache.NULL)\n if (isAxiosError(error)) {\n console.log(`Axios Url: ${error.response?.config.url}`)\n }\n handleError(error, (error) => {\n console.error(`fetchSchema threw: ${error.message}`)\n })\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,mBAAsB;AAEf,IAAM,WAAN,MAA8B;AAAA,EAC3B,MAAM,oBAAI,IAAkB;AAAA,EAEpC,MAAM,IAAO,KAAW,SAA2B,UAAU,KAAQ;AACnE,UAAM,YAAY,KAAK,IAAI;AAC3B,WAAO,KAAK,IAAI,IAAI,GAAG,GAAG;AACxB,gBAAM,oBAAM,GAAG;AACf,UAAI,KAAK,IAAI,IAAI,YAAY,SAAS;AACpC,cAAM,IAAI,MAAM,uBAAuB,GAAG,GAAG;AAAA,MAC/C;AAAA,IACF;AACA,QAAI;AACF,WAAK,IAAI,IAAI,KAAK,CAAC;AACnB,aAAO,MAAM,QAAQ;AAAA,IACvB,UAAE;AACA,WAAK,IAAI,IAAI,KAAK,CAAC;AAAA,IACrB;AAAA,EACF;AACF;;;ACpBA,mBAA6B;AAC7B,mBAA4B;AAC5B,mCAAqC;AAErC,mCAA4C;AAC5C,iBAAkC;AAClC,uBAAyB;AAKzB,IAAM,0BAA0B,CAAC,WAAyB;AACxD,MAAI,OAAO,KAAK;AACd,WAAO,OAAO;AAAA,EAChB;AACF;AAIO,IAAM,cAAN,MAAM,aAA2E;AAAA;AAAA;AAAA;AAAA;AAAA,EAKtF,OAA0B,OAAyB,EAAE,SAAS,EAAE,YAAY,CAAC,GAAG,QAAQ,0CAAa,EAAE;AAAA,EAEvG,OAAe;AAAA,EAEf;AAAA,EACA;AAAA,EAEQ,SAAS,IAAI,0BAAmC,EAA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ACF;AAAA,EAEA,MAAc,YAAY,QAAgB;AACxC,QAAI;AACF,YAAM,SAAS,MAAM,kDAAqB,SAAS,QAAQ,KAAK,KAAK;AACrE,YAAM,QAAQ,MAAM;AACpB,WAAK,aAAa,QAAQ,OAAO;AAGjC,UAAI,KAAK,OAAO,IAAI,MAAM,MAAM,QAAW;AACzC,aAAK,OAAO,IAAI,QAAQ,aAAY,IAAI;AAAA,MAC1C;AAAA,IACF,SAAS,OAAO;AAEd,WAAK,OAAO,IAAI,QAAQ,aAAY,IAAI;AACxC,cAAI,2BAAa,KAAK,GAAG;AACvB,gBAAQ,IAAI,cAAc,MAAM,UAAU,OAAO,GAAG,EAAE;AAAA,MACxD;AACA,oCAAY,OAAO,CAACC,WAAU;AAC5B,gBAAQ,MAAM,sBAAsBA,OAAM,OAAO,EAAE;AAAA,MACrD,CAAC;AAAA,IACH;AAAA,EACF;AACF;","names":["entry","error"]}
|
|
@@ -42,7 +42,7 @@ var SchemaCache = class _SchemaCache {
|
|
|
42
42
|
proxy;
|
|
43
43
|
_cache = new LRUCache({ max: 500, ttl: 1e3 * 60 * 5 });
|
|
44
44
|
_validators = {};
|
|
45
|
-
//prevents double discovery
|
|
45
|
+
// prevents double discovery
|
|
46
46
|
getDebounce = new Debounce();
|
|
47
47
|
constructor(proxy) {
|
|
48
48
|
this.proxy = proxy;
|
|
@@ -114,4 +114,4 @@ export {
|
|
|
114
114
|
Debounce,
|
|
115
115
|
SchemaCache
|
|
116
116
|
};
|
|
117
|
-
//# sourceMappingURL=index.
|
|
117
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/Debounce.ts","../../src/SchemaCache.ts"],"sourcesContent":["import { delay } from '@xylabs/delay'\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 (this.map.get(key)) {\n await delay(100)\n if (Date.now() - startTime > timeout) {\n throw new Error(`Debounce timed out [${key}]`)\n }\n }\n try {\n this.map.set(key, 1)\n return await closure()\n } finally {\n this.map.set(key, 0)\n }\n }\n}\n","import { isAxiosError } from '@xylabs/axios'\nimport { handleError } from '@xylabs/error'\nimport { DomainPayloadWrapper } from '@xyo-network/domain-payload-plugin'\nimport { FetchedPayload } from '@xyo-network/huri'\nimport { SchemaPayload, SchemaSchema } from '@xyo-network/schema-payload-plugin'\nimport { Ajv, SchemaObject } from 'ajv'\nimport { LRUCache } from 'lru-cache'\n\nimport { Debounce } from './Debounce.ts'\nimport { SchemaNameToValidatorMap } from './SchemaNameToValidatorMap.ts'\n\nconst getSchemaNameFromSchema = (schema: SchemaObject) => {\n if (schema.$id) {\n return schema.$id\n }\n}\n\nexport type SchemaCacheEntry = FetchedPayload<SchemaPayload>\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 onSchemaCached?: (name: string, entry: SchemaCacheEntry) => void\n proxy?: string\n\n private _cache = new LRUCache<string, SchemaCacheEntry>({ max: 500, ttl: 1000 * 60 * 5 })\n private _validators: T = {} as T\n\n // prevents double discovery\n private getDebounce = new Debounce()\n\n private constructor(proxy?: string) {\n this.proxy = proxy\n }\n\n static get instance() {\n if (!this._instance) {\n this._instance = new SchemaCache()\n }\n return this._instance\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 (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 private cacheSchemaIfValid(entry: SchemaCacheEntry) {\n // only store them if they match the schema root\n if (entry.payload.definition) {\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 (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\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 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)\n if (this._cache.get(schema) === undefined) {\n this._cache.set(schema, SchemaCache.NULL)\n }\n } catch (error) {\n // if failed, set it to NULL, TODO: Make an entry for an error to try again in the future?\n this._cache.set(schema, SchemaCache.NULL)\n if (isAxiosError(error)) {\n console.log(`Axios Url: ${error.response?.config.url}`)\n }\n handleError(error, (error) => {\n console.error(`fetchSchema threw: ${error.message}`)\n })\n }\n }\n}\n"],"mappings":";AAAA,SAAS,aAAa;AAEf,IAAM,WAAN,MAA8B;AAAA,EAC3B,MAAM,oBAAI,IAAkB;AAAA,EAEpC,MAAM,IAAO,KAAW,SAA2B,UAAU,KAAQ;AACnE,UAAM,YAAY,KAAK,IAAI;AAC3B,WAAO,KAAK,IAAI,IAAI,GAAG,GAAG;AACxB,YAAM,MAAM,GAAG;AACf,UAAI,KAAK,IAAI,IAAI,YAAY,SAAS;AACpC,cAAM,IAAI,MAAM,uBAAuB,GAAG,GAAG;AAAA,MAC/C;AAAA,IACF;AACA,QAAI;AACF,WAAK,IAAI,IAAI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ache = new import_lru_cache.LRUCache({ max: 500, ttl: 1e3 * 60 * 5 });
|
|
71
71
|
_validators = {};
|
|
72
|
-
//prevents double discovery
|
|
72
|
+
// prevents double discovery
|
|
73
73
|
getDebounce = new Debounce();
|
|
74
74
|
constructor(proxy) {
|
|
75
75
|
this.proxy = proxy;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/index.ts","../../src/Debounce.ts","../../src/SchemaCache.ts"],"sourcesContent":["export * from './Debounce.ts'\nexport * from './SchemaCache.ts'\nexport * from './SchemaNameToValidatorMap.ts'\n","import { delay } from '@xylabs/delay'\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 (this.map.get(key)) {\n await delay(100)\n if (Date.now() - startTime > timeout) {\n throw new Error(`Debounce timed out [${key}]`)\n }\n }\n try {\n this.map.set(key, 1)\n return await closure()\n } finally {\n this.map.set(key, 0)\n }\n }\n}\n","import { isAxiosError } from '@xylabs/axios'\nimport { handleError } from '@xylabs/error'\nimport { DomainPayloadWrapper } from '@xyo-network/domain-payload-plugin'\nimport { FetchedPayload } from '@xyo-network/huri'\nimport { SchemaPayload, SchemaSchema } from '@xyo-network/schema-payload-plugin'\nimport { Ajv, SchemaObject } from 'ajv'\nimport { LRUCache } from 'lru-cache'\n\nimport { Debounce } from './Debounce.ts'\nimport { SchemaNameToValidatorMap } from './SchemaNameToValidatorMap.ts'\n\nconst getSchemaNameFromSchema = (schema: SchemaObject) => {\n if (schema.$id) {\n return schema.$id\n }\n}\n\nexport type SchemaCacheEntry = FetchedPayload<SchemaPayload>\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 onSchemaCached?: (name: string, entry: SchemaCacheEntry) => void\n proxy?: string\n\n private _cache = new LRUCache<string, SchemaCacheEntry>({ max: 500, ttl: 1000 * 60 * 5 })\n private _validators: T = {} as T\n\n //prevents double discovery\n private getDebounce = new Debounce()\n\n private constructor(proxy?: string) {\n this.proxy = proxy\n }\n\n static get instance() {\n if (!this._instance) {\n this._instance = new SchemaCache()\n }\n return this._instance\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 (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 private cacheSchemaIfValid(entry: SchemaCacheEntry) {\n //only store them if they match the schema root\n if (entry.payload.definition) {\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 (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\n private cacheSchemas(aliasEntries?: FetchedPayload[] | null) {\n for (const entry of aliasEntries?.filter(
|
|
1
|
+
{"version":3,"sources":["../../src/index.ts","../../src/Debounce.ts","../../src/SchemaCache.ts"],"sourcesContent":["export * from './Debounce.ts'\nexport * from './SchemaCache.ts'\nexport * from './SchemaNameToValidatorMap.ts'\n","import { delay } from '@xylabs/delay'\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 (this.map.get(key)) {\n await delay(100)\n if (Date.now() - startTime > timeout) {\n throw new Error(`Debounce timed out [${key}]`)\n }\n }\n try {\n this.map.set(key, 1)\n return await closure()\n } finally {\n this.map.set(key, 0)\n }\n }\n}\n","import { isAxiosError } from '@xylabs/axios'\nimport { handleError } from '@xylabs/error'\nimport { DomainPayloadWrapper } from '@xyo-network/domain-payload-plugin'\nimport { FetchedPayload } from '@xyo-network/huri'\nimport { SchemaPayload, SchemaSchema } from '@xyo-network/schema-payload-plugin'\nimport { Ajv, SchemaObject } from 'ajv'\nimport { LRUCache } from 'lru-cache'\n\nimport { Debounce } from './Debounce.ts'\nimport { SchemaNameToValidatorMap } from './SchemaNameToValidatorMap.ts'\n\nconst getSchemaNameFromSchema = (schema: SchemaObject) => {\n if (schema.$id) {\n return schema.$id\n }\n}\n\nexport type SchemaCacheEntry = FetchedPayload<SchemaPayload>\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 onSchemaCached?: (name: string, entry: SchemaCacheEntry) => void\n proxy?: string\n\n private _cache = new LRUCache<string, SchemaCacheEntry>({ max: 500, ttl: 1000 * 60 * 5 })\n private _validators: T = {} as T\n\n // prevents double discovery\n private getDebounce = new Debounce()\n\n private constructor(proxy?: string) {\n this.proxy = proxy\n }\n\n static get instance() {\n if (!this._instance) {\n this._instance = new SchemaCache()\n }\n return this._instance\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 (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 private cacheSchemaIfValid(entry: SchemaCacheEntry) {\n // only store them if they match the schema root\n if (entry.payload.definition) {\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 (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\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 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)\n if (this._cache.get(schema) === undefined) {\n this._cache.set(schema, SchemaCache.NULL)\n }\n } catch (error) {\n // if failed, set it to NULL, TODO: Make an entry for an error to try again in the future?\n this._cache.set(schema, SchemaCache.NULL)\n if (isAxiosError(error)) {\n console.log(`Axios Url: ${error.response?.config.url}`)\n }\n handleError(error, (error) => {\n console.error(`fetchSchema threw: ${error.message}`)\n })\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,mBAAsB;AAEf,IAAM,WAAN,MAA8B;AAAA,EAC3B,MAAM,oBAAI,IAAkB;AAAA,EAEpC,MAAM,IAAO,KAAW,SAA2B,UAAU,KAAQ;AACnE,UAAM,YAAY,KAAK,IAAI;AAC3B,WAAO,KAAK,IAAI,IAAI,GAAG,GAAG;AACxB,gBAAM,oBAAM,GAAG;AACf,UAAI,KAAK,IAAI,IAAI,YAAY,SAAS;AACpC,cAAM,IAAI,MAAM,uBAAuB,GAAG,GAAG;AA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new LRUCache({ max: 500, ttl: 1e3 * 60 * 5 });
|
|
44
44
|
_validators = {};
|
|
45
|
-
//prevents double discovery
|
|
45
|
+
// prevents double discovery
|
|
46
46
|
getDebounce = new Debounce();
|
|
47
47
|
constructor(proxy) {
|
|
48
48
|
this.proxy = proxy;
|
|
@@ -114,4 +114,4 @@ export {
|
|
|
114
114
|
Debounce,
|
|
115
115
|
SchemaCache
|
|
116
116
|
};
|
|
117
|
-
//# sourceMappingURL=index.
|
|
117
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/Debounce.ts","../../src/SchemaCache.ts"],"sourcesContent":["import { delay } from '@xylabs/delay'\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 (this.map.get(key)) {\n await delay(100)\n if (Date.now() - startTime > timeout) {\n throw new Error(`Debounce timed out [${key}]`)\n }\n }\n try {\n this.map.set(key, 1)\n return await closure()\n } finally {\n this.map.set(key, 0)\n }\n }\n}\n","import { isAxiosError } from '@xylabs/axios'\nimport { handleError } from '@xylabs/error'\nimport { DomainPayloadWrapper } from '@xyo-network/domain-payload-plugin'\nimport { FetchedPayload } from '@xyo-network/huri'\nimport { SchemaPayload, SchemaSchema } from '@xyo-network/schema-payload-plugin'\nimport { Ajv, SchemaObject } from 'ajv'\nimport { LRUCache } from 'lru-cache'\n\nimport { Debounce } from './Debounce.ts'\nimport { SchemaNameToValidatorMap } from './SchemaNameToValidatorMap.ts'\n\nconst getSchemaNameFromSchema = (schema: SchemaObject) => {\n if (schema.$id) {\n return schema.$id\n }\n}\n\nexport type SchemaCacheEntry = FetchedPayload<SchemaPayload>\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 onSchemaCached?: (name: string, entry: SchemaCacheEntry) => void\n proxy?: string\n\n private _cache = new LRUCache<string, SchemaCacheEntry>({ max: 500, ttl: 1000 * 60 * 5 })\n private _validators: T = {} as T\n\n // prevents double discovery\n private getDebounce = new Debounce()\n\n private constructor(proxy?: string) {\n this.proxy = proxy\n }\n\n static get instance() {\n if (!this._instance) {\n this._instance = new SchemaCache()\n }\n return this._instance\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 (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 private cacheSchemaIfValid(entry: SchemaCacheEntry) {\n // only store them if they match the schema root\n if (entry.payload.definition) {\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 (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\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 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)\n if (this._cache.get(schema) === undefined) {\n this._cache.set(schema, SchemaCache.NULL)\n }\n } catch (error) {\n // if failed, set it to NULL, TODO: Make an entry for an error to try again in the future?\n this._cache.set(schema, SchemaCache.NULL)\n if (isAxiosError(error)) {\n console.log(`Axios Url: ${error.response?.config.url}`)\n }\n handleError(error, (error) => {\n console.error(`fetchSchema threw: ${error.message}`)\n })\n }\n }\n}\n"],"mappings":";AAAA,SAAS,aAAa;AAEf,IAAM,WAAN,MAA8B;AAAA,EAC3B,MAAM,oBAAI,IAAkB;AAAA,EAEpC,MAAM,IAAO,KAAW,SAA2B,UAAU,KAAQ;AACnE,UAAM,YAAY,KAAK,IAAI;AAC3B,WAAO,KAAK,IAAI,IAAI,GAAG,GAAG;AACxB,YAAM,MAAM,GAAG;AACf,UAAI,KAAK,IAAI,IAAI,YAAY,SAAS;AACpC,cAAM,IAAI,MAAM,uBAAuB,GAAG,GAAG;AAAA,MAC/C;AAAA,IACF;AACA,QAAI;AACF,WAAK,IAAI,IAAI,KAAK,CAAC;AACnB,aAAO,MAAM,QAAQ;AAAA,IACvB,UAAE;AACA,WAAK,IAAI,IAAI,KAAK,CAAC;AAAA,IACrB;AAAA,EACF;AACF;;;ACpBA,SAAS,oBAAoB;AAC7B,SAAS,mBAAmB;AAC5B,SAAS,4BAA4B;AAErC,SAAwB,oBAAoB;AAC5C,SAAS,WAAyB;AAClC,SAAS,gBAAgB;AAKzB,IAAM,0BAA0B,CAAC,WAAyB;AACxD,MAAI,OAAO,KAAK;AACd,WAAO,OAAO;AAAA,EAChB;AACF;AAIO,IAAM,cAAN,MAAM,aAA2E;AAAA;AAAA;AAAA;AAAA;AAAA,EAKtF,OAA0B,OAAyB,EAAE,SAAS,EAAE,YAAY,CAAC,GAAG,QAAQ,aAAa,EAAE;AAAA,EAEvG,OAAe;AAAA,EAEf;AAAA,EACA;AAAA,EAEQ,SAAS,IAAI,SAAmC,EAAE,KAAK,KAAK,KAAK,MAAO,KAAK,EAAE,CAAC;AAAA,EAChF,cAAiB,CAAC;AAAA;AAAA,EAGlB,cAAc,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,KAAK;AACrE,YAAM,QAAQ,MAAM;AACpB,WAAK,aAAa,QAAQ,OAAO;AAGjC,UAAI,KAAK,OAAO,IAAI,MAAM,MAAM,QAAW;AACzC,aAAK,OAAO,IAAI,QAAQ,aAAY,IAAI;AAAA,MAC1C;AAAA,IACF,SAAS,OAAO;AAEd,WAAK,OAAO,IAAI,QAAQ,aAAY,IAAI;AACxC,UAAI,aAAa,KAAK,GAAG;AACvB,gBAAQ,IAAI,cAAc,MAAM,UAAU,OAAO,GAAG,EAAE;AAAA,MACxD;AACA,kBAAY,OAAO,CAACC,WAAU;AAC5B,gBAAQ,MAAM,sBAAsBA,OAAM,OAAO,EAAE;AAAA,MACrD,CAAC;AAAA,IACH;AAAA,EACF;AACF;","names":["entry","error"]}
|
package/dist/node/index.cjs
CHANGED
|
@@ -69,7 +69,7 @@ var SchemaCache = class _SchemaCache {
|
|
|
69
69
|
proxy;
|
|
70
70
|
_cache = new import_lru_cache.LRUCache({ max: 500, ttl: 1e3 * 60 * 5 });
|
|
71
71
|
_validators = {};
|
|
72
|
-
//prevents double discovery
|
|
72
|
+
// prevents double discovery
|
|
73
73
|
getDebounce = new Debounce();
|
|
74
74
|
constructor(proxy) {
|
|
75
75
|
this.proxy = proxy;
|
package/dist/node/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/index.ts","../../src/Debounce.ts","../../src/SchemaCache.ts"],"sourcesContent":["export * from './Debounce.ts'\nexport * from './SchemaCache.ts'\nexport * from './SchemaNameToValidatorMap.ts'\n","import { delay } from '@xylabs/delay'\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 (this.map.get(key)) {\n await delay(100)\n if (Date.now() - startTime > timeout) {\n throw new Error(`Debounce timed out [${key}]`)\n }\n }\n try {\n this.map.set(key, 1)\n return await closure()\n } finally {\n this.map.set(key, 0)\n }\n }\n}\n","import { isAxiosError } from '@xylabs/axios'\nimport { handleError } from '@xylabs/error'\nimport { DomainPayloadWrapper } from '@xyo-network/domain-payload-plugin'\nimport { FetchedPayload } from '@xyo-network/huri'\nimport { SchemaPayload, SchemaSchema } from '@xyo-network/schema-payload-plugin'\nimport { Ajv, SchemaObject } from 'ajv'\nimport { LRUCache } from 'lru-cache'\n\nimport { Debounce } from './Debounce.ts'\nimport { SchemaNameToValidatorMap } from './SchemaNameToValidatorMap.ts'\n\nconst getSchemaNameFromSchema = (schema: SchemaObject) => {\n if (schema.$id) {\n return schema.$id\n }\n}\n\nexport type SchemaCacheEntry = FetchedPayload<SchemaPayload>\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 onSchemaCached?: (name: string, entry: SchemaCacheEntry) => void\n proxy?: string\n\n private _cache = new LRUCache<string, SchemaCacheEntry>({ max: 500, ttl: 1000 * 60 * 5 })\n private _validators: T = {} as T\n\n //prevents double discovery\n private getDebounce = new Debounce()\n\n private constructor(proxy?: string) {\n this.proxy = proxy\n }\n\n static get instance() {\n if (!this._instance) {\n this._instance = new SchemaCache()\n }\n return this._instance\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 (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 private cacheSchemaIfValid(entry: SchemaCacheEntry) {\n //only store them if they match the schema root\n if (entry.payload.definition) {\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 (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\n private cacheSchemas(aliasEntries?: FetchedPayload[] | null) {\n for (const entry of aliasEntries?.filter(
|
|
1
|
+
{"version":3,"sources":["../../src/index.ts","../../src/Debounce.ts","../../src/SchemaCache.ts"],"sourcesContent":["export * from './Debounce.ts'\nexport * from './SchemaCache.ts'\nexport * from './SchemaNameToValidatorMap.ts'\n","import { delay } from '@xylabs/delay'\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 (this.map.get(key)) {\n await delay(100)\n if (Date.now() - startTime > timeout) {\n throw new Error(`Debounce timed out [${key}]`)\n }\n }\n try {\n this.map.set(key, 1)\n return await closure()\n } finally {\n this.map.set(key, 0)\n }\n }\n}\n","import { isAxiosError } from '@xylabs/axios'\nimport { handleError } from '@xylabs/error'\nimport { DomainPayloadWrapper } from '@xyo-network/domain-payload-plugin'\nimport { FetchedPayload } from '@xyo-network/huri'\nimport { SchemaPayload, SchemaSchema } from '@xyo-network/schema-payload-plugin'\nimport { Ajv, SchemaObject } from 'ajv'\nimport { LRUCache } from 'lru-cache'\n\nimport { Debounce } from './Debounce.ts'\nimport { SchemaNameToValidatorMap } from './SchemaNameToValidatorMap.ts'\n\nconst getSchemaNameFromSchema = (schema: SchemaObject) => {\n if (schema.$id) {\n return schema.$id\n }\n}\n\nexport type SchemaCacheEntry = FetchedPayload<SchemaPayload>\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 onSchemaCached?: (name: string, entry: SchemaCacheEntry) => void\n proxy?: string\n\n private _cache = new LRUCache<string, SchemaCacheEntry>({ max: 500, ttl: 1000 * 60 * 5 })\n private _validators: T = {} as T\n\n // prevents double discovery\n private getDebounce = new Debounce()\n\n private constructor(proxy?: string) {\n this.proxy = proxy\n }\n\n static get instance() {\n if (!this._instance) {\n this._instance = new SchemaCache()\n }\n return this._instance\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 (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 private cacheSchemaIfValid(entry: SchemaCacheEntry) {\n // only store them if they match the schema root\n if (entry.payload.definition) {\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 (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\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 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)\n if (this._cache.get(schema) === undefined) {\n this._cache.set(schema, SchemaCache.NULL)\n }\n } catch (error) {\n // if failed, set it to NULL, TODO: Make an entry for an error to try again in the future?\n this._cache.set(schema, SchemaCache.NULL)\n if (isAxiosError(error)) {\n console.log(`Axios Url: ${error.response?.config.url}`)\n }\n handleError(error, (error) => {\n console.error(`fetchSchema threw: ${error.message}`)\n })\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,mBAAsB;AAEf,IAAM,WAAN,MAA8B;AAAA,EAC3B,MAAM,oBAAI,IAAkB;AAAA,EAEpC,MAAM,IAAO,KAAW,SAA2B,UAAU,KAAQ;AACnE,UAAM,YAAY,KAAK,IAAI;AAC3B,WAAO,KAAK,IAAI,IAAI,GAAG,GAAG;AACxB,gBAAM,oBAAM,GAAG;AACf,UAAI,KAAK,IAAI,IAAI,YAAY,SAAS;AACpC,cAAM,IAAI,MAAM,uBAAuB,GAAG,GAAG;AAAA,MAC/C;AAAA,IACF;AACA,QAAI;AACF,WAAK,IAAI,IAAI,KAAK,CAAC;AACnB,aAAO,MAAM,QAAQ;AAAA,IACvB,UAAE;AACA,WAAK,IAAI,IAAI,KAAK,CAAC;AAAA,IACrB;AAAA,EACF;AACF;;;ACpBA,mBAA6B;AAC7B,mBAA4B;AAC5B,mCAAqC;AAErC,mCAA4C;AAC5C,iBAAkC;AAClC,uBAAyB;AAKzB,IAAM,0BAA0B,CAAC,WAAyB;AACxD,MAAI,OAAO,KAAK;AACd,WAAO,OAAO;AAAA,EAChB;AACF;AAIO,IAAM,cAAN,MAAM,aAA2E;AAAA;AAAA;AAAA;AAAA;AAAA,EAKtF,OAA0B,OAAyB,EAAE,SAAS,EAAE,YAAY,CAAC,GAAG,QAAQ,0CAAa,EAAE;AAAA,EAEvG,OAAe;AAAA,EAEf;AAAA,EACA;AAAA,EAEQ,SAAS,IAAI,0BAAmC,EAAE,KAAK,KAAK,KAAK,MAAO,KAAK,EAAE,CAAC;AAAA,E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AA8FI,QAAI;AACF,YAAM,SAAS,MAAM,kDAAqB,SAAS,QAAQ,KAAK,KAAK;AACrE,aAAM,iCAAQ;AACd,WAAK,aAAa,iCAAQ,OAAO;AAGjC,UAAI,KAAK,OAAO,IAAI,MAAM,MAAM,QAAW;AACzC,aAAK,OAAO,IAAI,QAAQ,aAAY,IAAI;AAAA,MAC1C;AAAA,IACF,SAAS,OAAO;AAEd,WAAK,OAAO,IAAI,QAAQ,aAAY,IAAI;AACxC,cAAI,2BAAa,KAAK,GAAG;AACvB,gBAAQ,IAAI,eAAc,WAAM,aAAN,mBAAgB,OAAO,GAAG,EAAE;AAAA,MACxD;AACA,oCAAY,OAAO,CAACC,WAAU;AAC5B,gBAAQ,MAAM,sBAAsBA,OAAM,OAAO,EAAE;AAAA,MACrD,CAAC;AAAA,IACH;AAAA,EACF;AACF;","names":["entry","error"]}
|
|
@@ -42,7 +42,7 @@ var SchemaCache = class _SchemaCache {
|
|
|
42
42
|
proxy;
|
|
43
43
|
_cache = new LRUCache({ max: 500, ttl: 1e3 * 60 * 5 });
|
|
44
44
|
_validators = {};
|
|
45
|
-
//prevents double discovery
|
|
45
|
+
// prevents double discovery
|
|
46
46
|
getDebounce = new Debounce();
|
|
47
47
|
constructor(proxy) {
|
|
48
48
|
this.proxy = proxy;
|
|
@@ -116,4 +116,4 @@ export {
|
|
|
116
116
|
Debounce,
|
|
117
117
|
SchemaCache
|
|
118
118
|
};
|
|
119
|
-
//# sourceMappingURL=index.
|
|
119
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/Debounce.ts","../../src/SchemaCache.ts"],"sourcesContent":["import { delay } from '@xylabs/delay'\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 (this.map.get(key)) {\n await delay(100)\n if (Date.now() - startTime > timeout) {\n throw new Error(`Debounce timed out [${key}]`)\n }\n }\n try {\n this.map.set(key, 1)\n return await closure()\n } finally {\n this.map.set(key, 0)\n }\n }\n}\n","import { isAxiosError } from '@xylabs/axios'\nimport { handleError } from '@xylabs/error'\nimport { DomainPayloadWrapper } from '@xyo-network/domain-payload-plugin'\nimport { FetchedPayload } from '@xyo-network/huri'\nimport { SchemaPayload, SchemaSchema } from '@xyo-network/schema-payload-plugin'\nimport { Ajv, SchemaObject } from 'ajv'\nimport { LRUCache } from 'lru-cache'\n\nimport { Debounce } from './Debounce.ts'\nimport { SchemaNameToValidatorMap } from './SchemaNameToValidatorMap.ts'\n\nconst getSchemaNameFromSchema = (schema: SchemaObject) => {\n if (schema.$id) {\n return schema.$id\n }\n}\n\nexport type SchemaCacheEntry = FetchedPayload<SchemaPayload>\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 onSchemaCached?: (name: string, entry: SchemaCacheEntry) => void\n proxy?: string\n\n private _cache = new LRUCache<string, SchemaCacheEntry>({ max: 500, ttl: 1000 * 60 * 5 })\n private _validators: T = {} as T\n\n // prevents double discovery\n private getDebounce = new Debounce()\n\n private constructor(proxy?: string) {\n this.proxy = proxy\n }\n\n static get instance() {\n if (!this._instance) {\n this._instance = new SchemaCache()\n }\n return this._instance\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 (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 private cacheSchemaIfValid(entry: SchemaCacheEntry) {\n // only store them if they match the schema root\n if (entry.payload.definition) {\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 (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\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 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)\n if (this._cache.get(schema) === undefined) {\n this._cache.set(schema, SchemaCache.NULL)\n }\n } catch (error) {\n // if failed, set it to NULL, TODO: Make an entry for an error to try again in the future?\n this._cache.set(schema, SchemaCache.NULL)\n if (isAxiosError(error)) {\n console.log(`Axios Url: ${error.response?.config.url}`)\n }\n handleError(error, (error) => {\n console.error(`fetchSchema threw: ${error.message}`)\n })\n }\n }\n}\n"],"mappings":";AAAA,SAAS,aAAa;AAEf,IAAM,WAAN,MAA8B;AAAA,EAC3B,MAAM,oBAAI,IAAkB;AAAA,EAEpC,MAAM,IAAO,KAAW,SAA2B,UAAU,KAAQ;AACnE,UAAM,YAAY,KAAK,IAAI;AAC3B,WAAO,KAAK,IAAI,IAAI,GAAG,GAAG;AACxB,YAAM,MAAM,GAAG;AACf,UAAI,KAAK,IAAI,IAAI,YAAY,SAAS;AACpC,cAAM,IAAI,MAAM,uBAAuB,GAAG,GAAG;AAAA,MAC/C;AAAA,IACF;AACA,QAAI;AACF,WAAK,IAAI,IAAI,KAAK,CAAC;AACnB,aAAO,MAAM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|
11
11
|
"dependencies": {
|
|
12
|
-
"@xylabs/axios": "^3.6.
|
|
13
|
-
"@xylabs/delay": "^3.6.
|
|
14
|
-
"@xylabs/error": "^3.6.
|
|
15
|
-
"@xyo-network/domain-payload-plugin": "^2.
|
|
16
|
-
"@xyo-network/huri": "^2.
|
|
17
|
-
"@xyo-network/payload-model": "^2.
|
|
18
|
-
"@xyo-network/schema-payload-plugin": "^2.
|
|
12
|
+
"@xylabs/axios": "^3.6.8",
|
|
13
|
+
"@xylabs/delay": "^3.6.8",
|
|
14
|
+
"@xylabs/error": "^3.6.8",
|
|
15
|
+
"@xyo-network/domain-payload-plugin": "^2.111.0",
|
|
16
|
+
"@xyo-network/huri": "^2.111.0",
|
|
17
|
+
"@xyo-network/payload-model": "^2.111.0",
|
|
18
|
+
"@xyo-network/schema-payload-plugin": "^2.111.0",
|
|
19
19
|
"ajv": "^8.17.1",
|
|
20
20
|
"lru-cache": "^11.0.0"
|
|
21
21
|
},
|
|
22
22
|
"description": "Primary SDK for using XYO Protocol 2.0",
|
|
23
23
|
"devDependencies": {
|
|
24
|
-
"@xylabs/assert": "^3.6.
|
|
25
|
-
"@xylabs/ts-scripts-yarn3": "^3.
|
|
26
|
-
"@xylabs/tsconfig-dom": "^3.
|
|
27
|
-
"@xyo-network/network": "^2.
|
|
28
|
-
"@xyo-network/payload-builder": "^2.
|
|
24
|
+
"@xylabs/assert": "^3.6.8",
|
|
25
|
+
"@xylabs/ts-scripts-yarn3": "^3.15.5",
|
|
26
|
+
"@xylabs/tsconfig-dom": "^3.15.5",
|
|
27
|
+
"@xyo-network/network": "^2.111.0",
|
|
28
|
+
"@xyo-network/payload-builder": "^2.111.0",
|
|
29
29
|
"typescript": "^5.5.4"
|
|
30
30
|
},
|
|
31
31
|
"exports": {
|
|
@@ -37,7 +37,7 @@
|
|
|
37
37
|
},
|
|
38
38
|
"import": {
|
|
39
39
|
"types": "./dist/browser/index.d.mts",
|
|
40
|
-
"default": "./dist/browser/index.
|
|
40
|
+
"default": "./dist/browser/index.mjs"
|
|
41
41
|
}
|
|
42
42
|
},
|
|
43
43
|
"node": {
|
|
@@ -47,14 +47,14 @@
|
|
|
47
47
|
},
|
|
48
48
|
"import": {
|
|
49
49
|
"types": "./dist/node/index.d.mts",
|
|
50
|
-
"default": "./dist/node/index.
|
|
50
|
+
"default": "./dist/node/index.mjs"
|
|
51
51
|
}
|
|
52
52
|
}
|
|
53
53
|
},
|
|
54
54
|
"./package.json": "./package.json"
|
|
55
55
|
},
|
|
56
56
|
"main": "dist/node/index.cjs",
|
|
57
|
-
"module": "dist/node/index.
|
|
57
|
+
"module": "dist/node/index.mjs",
|
|
58
58
|
"types": "dist/node/index.d.mts",
|
|
59
59
|
"type": "module",
|
|
60
60
|
"homepage": "https://xyo.network",
|
|
@@ -68,5 +68,5 @@
|
|
|
68
68
|
"url": "https://github.com/XYOracleNetwork/sdk-xyo-client-js.git"
|
|
69
69
|
},
|
|
70
70
|
"sideEffects": false,
|
|
71
|
-
"version": "2.
|
|
71
|
+
"version": "2.111.0"
|
|
72
72
|
}
|
package/src/SchemaCache.ts
CHANGED
|
@@ -32,7 +32,7 @@ export class SchemaCache<T extends SchemaNameToValidatorMap = SchemaNameToValida
|
|
|
32
32
|
private _cache = new LRUCache<string, SchemaCacheEntry>({ max: 500, ttl: 1000 * 60 * 5 })
|
|
33
33
|
private _validators: T = {} as T
|
|
34
34
|
|
|
35
|
-
//prevents double discovery
|
|
35
|
+
// prevents double discovery
|
|
36
36
|
private getDebounce = new Debounce()
|
|
37
37
|
|
|
38
38
|
private constructor(proxy?: string) {
|
|
@@ -70,10 +70,10 @@ export class SchemaCache<T extends SchemaNameToValidatorMap = SchemaNameToValida
|
|
|
70
70
|
}
|
|
71
71
|
|
|
72
72
|
private cacheSchemaIfValid(entry: SchemaCacheEntry) {
|
|
73
|
-
//only store them if they match the schema root
|
|
73
|
+
// only store them if they match the schema root
|
|
74
74
|
if (entry.payload.definition) {
|
|
75
75
|
const ajv = new Ajv({ strict: false })
|
|
76
|
-
//check if it is a valid schema def
|
|
76
|
+
// check if it is a valid schema def
|
|
77
77
|
const validator = ajv.compile(entry.payload.definition)
|
|
78
78
|
const schemaName = getSchemaNameFromSchema(entry.payload.definition)
|
|
79
79
|
if (schemaName) {
|
|
@@ -86,7 +86,7 @@ export class SchemaCache<T extends SchemaNameToValidatorMap = SchemaNameToValida
|
|
|
86
86
|
}
|
|
87
87
|
|
|
88
88
|
private cacheSchemas(aliasEntries?: FetchedPayload[] | null) {
|
|
89
|
-
for (const entry of aliasEntries?.filter(
|
|
89
|
+
for (const entry of aliasEntries?.filter(entry => entry.payload.schema === SchemaSchema) ?? []) {
|
|
90
90
|
this.cacheSchemaIfValid(entry as SchemaCacheEntry)
|
|
91
91
|
}
|
|
92
92
|
}
|
|
@@ -97,12 +97,12 @@ export class SchemaCache<T extends SchemaNameToValidatorMap = SchemaNameToValida
|
|
|
97
97
|
await domain?.fetch()
|
|
98
98
|
this.cacheSchemas(domain?.aliases)
|
|
99
99
|
|
|
100
|
-
//if it is still undefined, mark it as null (not found)
|
|
100
|
+
// if it is still undefined, mark it as null (not found)
|
|
101
101
|
if (this._cache.get(schema) === undefined) {
|
|
102
102
|
this._cache.set(schema, SchemaCache.NULL)
|
|
103
103
|
}
|
|
104
104
|
} catch (error) {
|
|
105
|
-
//if failed, set it to NULL, TODO: Make an entry for an error to try again in the future?
|
|
105
|
+
// if failed, set it to NULL, TODO: Make an entry for an error to try again in the future?
|
|
106
106
|
this._cache.set(schema, SchemaCache.NULL)
|
|
107
107
|
if (isAxiosError(error)) {
|
|
108
108
|
console.log(`Axios Url: ${error.response?.config.url}`)
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/Debounce.ts","../../src/SchemaCache.ts"],"sourcesContent":["import { delay } from '@xylabs/delay'\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 (this.map.get(key)) {\n await delay(100)\n if (Date.now() - startTime > timeout) {\n throw new Error(`Debounce timed out [${key}]`)\n }\n }\n try {\n this.map.set(key, 1)\n return await closure()\n } finally {\n this.map.set(key, 0)\n }\n }\n}\n","import { isAxiosError } from '@xylabs/axios'\nimport { handleError } from '@xylabs/error'\nimport { DomainPayloadWrapper } from '@xyo-network/domain-payload-plugin'\nimport { FetchedPayload } from '@xyo-network/huri'\nimport { SchemaPayload, SchemaSchema } from '@xyo-network/schema-payload-plugin'\nimport { Ajv, SchemaObject } from 'ajv'\nimport { LRUCache } from 'lru-cache'\n\nimport { Debounce } from './Debounce.ts'\nimport { SchemaNameToValidatorMap } from './SchemaNameToValidatorMap.ts'\n\nconst getSchemaNameFromSchema = (schema: SchemaObject) => {\n if (schema.$id) {\n return schema.$id\n }\n}\n\nexport type SchemaCacheEntry = FetchedPayload<SchemaPayload>\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 onSchemaCached?: (name: string, entry: SchemaCacheEntry) => void\n proxy?: string\n\n private _cache = new LRUCache<string, SchemaCacheEntry>({ max: 500, ttl: 1000 * 60 * 5 })\n private _validators: T = {} as T\n\n //prevents double discovery\n private getDebounce = new Debounce()\n\n private constructor(proxy?: string) {\n this.proxy = proxy\n }\n\n static get instance() {\n if (!this._instance) {\n this._instance = new SchemaCache()\n }\n return this._instance\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 (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 private cacheSchemaIfValid(entry: SchemaCacheEntry) {\n //only store them if they match the schema root\n if (entry.payload.definition) {\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 (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\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 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)\n if (this._cache.get(schema) === undefined) {\n this._cache.set(schema, SchemaCache.NULL)\n }\n } catch (error) {\n //if failed, set it to NULL, TODO: Make an entry for an error to try again in the future?\n this._cache.set(schema, SchemaCache.NULL)\n if (isAxiosError(error)) {\n console.log(`Axios Url: ${error.response?.config.url}`)\n }\n handleError(error, (error) => {\n console.error(`fetchSchema threw: ${error.message}`)\n })\n }\n }\n}\n"],"mappings":";AAAA,SAAS,aAAa;AAEf,IAAM,WAAN,MAA8B;AAAA,EAC3B,MAAM,oBAAI,IAAkB;AAAA,EAEpC,MAAM,IAAO,KAAW,SAA2B,UAAU,KAAQ;AACnE,UAAM,YAAY,KAAK,IAAI;AAC3B,WAAO,KAAK,IAAI,IAAI,GAAG,GAAG;AACxB,YAAM,MAAM,GAAG;AACf,UAAI,KAAK,IAAI,IAAI,YAAY,SAAS;AACpC,cAAM,IAAI,MAAM,uBAAuB,GAAG,GAAG;AAAA,MAC/C;AAAA,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chemaCache.ts"],"sourcesContent":["import { delay } from '@xylabs/delay'\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 (this.map.get(key)) {\n await delay(100)\n if (Date.now() - startTime > timeout) {\n throw new Error(`Debounce timed out [${key}]`)\n }\n }\n try {\n this.map.set(key, 1)\n return await closure()\n } finally {\n this.map.set(key, 0)\n }\n }\n}\n","import { isAxiosError } from '@xylabs/axios'\nimport { handleError } from '@xylabs/error'\nimport { DomainPayloadWrapper } from '@xyo-network/domain-payload-plugin'\nimport { FetchedPayload } from '@xyo-network/huri'\nimport { SchemaPayload, SchemaSchema } from '@xyo-network/schema-payload-plugin'\nimport { Ajv, SchemaObject } from 'ajv'\nimport { LRUCache } from 'lru-cache'\n\nimport { Debounce } from './Debounce.ts'\nimport { SchemaNameToValidatorMap } from './SchemaNameToValidatorMap.ts'\n\nconst getSchemaNameFromSchema = (schema: SchemaObject) => {\n if (schema.$id) {\n return schema.$id\n }\n}\n\nexport type SchemaCacheEntry = FetchedPayload<SchemaPayload>\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 onSchemaCached?: (name: string, entry: SchemaCacheEntry) => void\n proxy?: string\n\n private _cache = new LRUCache<string, SchemaCacheEntry>({ max: 500, ttl: 1000 * 60 * 5 })\n private _validators: T = {} as T\n\n //prevents double discovery\n private getDebounce = new Debounce()\n\n private constructor(proxy?: string) {\n this.proxy = proxy\n }\n\n static get instance() {\n if (!this._instance) {\n this._instance = new SchemaCache()\n }\n return this._instance\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 (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 private cacheSchemaIfValid(entry: SchemaCacheEntry) {\n //only store them if they match the schema root\n if (entry.payload.definition) {\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 (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\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 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)\n if (this._cache.get(schema) === undefined) {\n this._cache.set(schema, SchemaCache.NULL)\n }\n } catch (error) {\n //if failed, set it to NULL, TODO: Make an entry for an error to try again in the future?\n this._cache.set(schema, SchemaCache.NULL)\n if (isAxiosError(error)) {\n console.log(`Axios Url: ${error.response?.config.url}`)\n }\n handleError(error, (error) => {\n console.error(`fetchSchema threw: ${error.message}`)\n })\n }\n }\n}\n"],"mappings":";AAAA,SAAS,aAAa;AAEf,IAAM,WAAN,MAA8B;AAAA,EAC3B,MAAM,oBAAI,IAAkB;AAAA,EAEpC,MAAM,IAAO,KAAW,SAA2B,UAAU,KAAQ;AACnE,UAAM,YAAY,KAAK,IAAI;AAC3B,WAAO,KAAK,IAAI,IAA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M,OAAO,EAAE;AAAA,MACrD,CAAC;AAAA,IACH;AAAA,EACF;AACF;","names":["entry","error"]}
|
package/dist/node/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/Debounce.ts","../../src/SchemaCache.ts"],"sourcesContent":["import { delay } from '@xylabs/delay'\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 (this.map.get(key)) {\n await delay(100)\n if (Date.now() - startTime > timeout) {\n throw new Error(`Debounce timed out [${key}]`)\n }\n }\n try {\n this.map.set(key, 1)\n return await closure()\n } finally {\n this.map.set(key, 0)\n }\n }\n}\n","import { isAxiosError } from '@xylabs/axios'\nimport { handleError } from '@xylabs/error'\nimport { DomainPayloadWrapper } from '@xyo-network/domain-payload-plugin'\nimport { FetchedPayload } from '@xyo-network/huri'\nimport { SchemaPayload, SchemaSchema } from '@xyo-network/schema-payload-plugin'\nimport { Ajv, SchemaObject } from 'ajv'\nimport { LRUCache } from 'lru-cache'\n\nimport { Debounce } from './Debounce.ts'\nimport { SchemaNameToValidatorMap } from './SchemaNameToValidatorMap.ts'\n\nconst getSchemaNameFromSchema = (schema: SchemaObject) => {\n if (schema.$id) {\n return schema.$id\n }\n}\n\nexport type SchemaCacheEntry = FetchedPayload<SchemaPayload>\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 onSchemaCached?: (name: string, entry: SchemaCacheEntry) => void\n proxy?: string\n\n private _cache = new LRUCache<string, SchemaCacheEntry>({ max: 500, ttl: 1000 * 60 * 5 })\n private _validators: T = {} as T\n\n //prevents double discovery\n private getDebounce = new Debounce()\n\n private constructor(proxy?: string) {\n this.proxy = proxy\n }\n\n static get instance() {\n if (!this._instance) {\n this._instance = new SchemaCache()\n }\n return this._instance\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 (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 private cacheSchemaIfValid(entry: SchemaCacheEntry) {\n //only store them if they match the schema root\n if (entry.payload.definition) {\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 (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\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 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)\n if (this._cache.get(schema) === undefined) {\n this._cache.set(schema, SchemaCache.NULL)\n }\n } catch (error) {\n //if failed, set it to NULL, TODO: Make an entry for an error to try again in the future?\n this._cache.set(schema, SchemaCache.NULL)\n if (isAxiosError(error)) {\n console.log(`Axios Url: ${error.response?.config.url}`)\n }\n handleError(error, (error) => {\n console.error(`fetchSchema threw: ${error.message}`)\n })\n }\n }\n}\n"],"mappings":";AAAA,SAAS,aAAa;AAEf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