@xyo-network/schema-cache 2.75.0 → 2.75.2
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/Debounce.d.cts +5 -0
- package/dist/browser/Debounce.d.cts.map +1 -0
- package/dist/browser/Debounce.js +2 -3
- package/dist/browser/Debounce.js.map +1 -1
- package/dist/browser/SchemaCache.d.cts +30 -0
- package/dist/browser/SchemaCache.d.cts.map +1 -0
- package/dist/browser/SchemaCache.js +8 -31
- package/dist/browser/SchemaCache.js.map +1 -1
- package/dist/browser/SchemaNameToValidatorMap.d.cts +17 -0
- package/dist/browser/SchemaNameToValidatorMap.d.cts.map +1 -0
- package/dist/browser/index.d.cts +4 -0
- package/dist/browser/index.d.cts.map +1 -0
- package/dist/browser/index.js +3 -116
- package/dist/browser/index.js.map +1 -1
- package/dist/docs.json +2590 -0
- package/dist/node/Debounce.d.cts +5 -0
- package/dist/node/Debounce.d.cts.map +1 -0
- package/dist/node/Debounce.js +4 -2
- package/dist/node/Debounce.js.map +1 -1
- package/dist/node/Debounce.mjs +3 -2
- package/dist/node/Debounce.mjs.map +1 -1
- package/dist/node/SchemaCache.d.cts +30 -0
- package/dist/node/SchemaCache.d.cts.map +1 -0
- package/dist/node/SchemaCache.js +40 -14
- package/dist/node/SchemaCache.js.map +1 -1
- package/dist/node/SchemaCache.mjs +38 -13
- package/dist/node/SchemaCache.mjs.map +1 -1
- package/dist/node/SchemaNameToValidatorMap.d.cts +17 -0
- package/dist/node/SchemaNameToValidatorMap.d.cts.map +1 -0
- package/dist/node/SchemaNameToValidatorMap.js +2 -0
- package/dist/node/SchemaNameToValidatorMap.js.map +1 -1
- package/dist/node/index.d.cts +4 -0
- package/dist/node/index.d.cts.map +1 -0
- package/dist/node/index.js +137 -7
- package/dist/node/index.js.map +1 -1
- package/dist/node/index.mjs +118 -3
- package/dist/node/index.mjs.map +1 -1
- package/package.json +11 -11
|
@@ -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,SAAQ;CAenE"}
|
package/dist/browser/Debounce.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
// src/Debounce.ts
|
|
2
1
|
import { delay } from "@xylabs/delay";
|
|
3
|
-
|
|
2
|
+
class Debounce {
|
|
4
3
|
map = /* @__PURE__ */ new Map();
|
|
5
4
|
async one(key, closure, timeout = 1e4) {
|
|
6
5
|
const startTime = Date.now();
|
|
@@ -17,7 +16,7 @@ var Debounce = class {
|
|
|
17
16
|
this.map.set(key, 0);
|
|
18
17
|
}
|
|
19
18
|
}
|
|
20
|
-
}
|
|
19
|
+
}
|
|
21
20
|
export {
|
|
22
21
|
Debounce
|
|
23
22
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/Debounce.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 = 10000) {\n const startTime = Date.now()\n while (this.map.get(key)) {\n await delay(100)\n if (Date.now() - startTime > timeout) {\n throw 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"],"mappings":"
|
|
1
|
+
{"version":3,"sources":["../../src/Debounce.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 = 10000) {\n const startTime = Date.now()\n while (this.map.get(key)) {\n await delay(100)\n if (Date.now() - startTime > timeout) {\n throw 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"],"mappings":"AAAA,SAAS,aAAa;AAEf,MAAM,SAAwB;AAAA,EAC3B,MAAM,oBAAI,IAAkB;AAAA,EAEpC,MAAM,IAAO,KAAW,SAA2B,UAAU,KAAO;AAClE,UAAM,YAAY,KAAK,IAAI;AAC3B,WAAO,KAAK,IAAI,IAAI,GAAG,GAAG;AACxB,YAAM,MAAM,GAAG;AACf,UAAI,KAAK,IAAI,IAAI,YAAY,SAAS;AACpC,cAAM,MAAM,uBAAuB,GAAG,GAAG;AAAA,MAC3C;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;","names":[]}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { FetchedPayload } from '@xyo-network/huri';
|
|
2
|
+
import { SchemaPayload } from '@xyo-network/schema-payload-plugin';
|
|
3
|
+
import { SchemaNameToValidatorMap } from './SchemaNameToValidatorMap';
|
|
4
|
+
export type SchemaCacheEntry = FetchedPayload<SchemaPayload>;
|
|
5
|
+
export declare class SchemaCache<T extends SchemaNameToValidatorMap = SchemaNameToValidatorMap> {
|
|
6
|
+
/**
|
|
7
|
+
* Object representing `null` since LRU Cache types
|
|
8
|
+
* only allow for types that derive from object
|
|
9
|
+
*/
|
|
10
|
+
protected static readonly NULL: SchemaCacheEntry;
|
|
11
|
+
private static _instance?;
|
|
12
|
+
onSchemaCached?: (name: string, entry: SchemaCacheEntry) => void;
|
|
13
|
+
proxy?: string;
|
|
14
|
+
private _cache;
|
|
15
|
+
private _validators;
|
|
16
|
+
private getDebounce;
|
|
17
|
+
private constructor();
|
|
18
|
+
static get instance(): SchemaCache<SchemaNameToValidatorMap>;
|
|
19
|
+
/**
|
|
20
|
+
* A map of cached schema (by name) to payload validators for the schema. A schema
|
|
21
|
+
* must be cached via `get('schema.name')` before it's validator can be used as
|
|
22
|
+
* they are compiled dynamically at runtime upon retrieval.
|
|
23
|
+
*/
|
|
24
|
+
get validators(): T;
|
|
25
|
+
get(schema?: string): Promise<SchemaCacheEntry | undefined | null>;
|
|
26
|
+
private cacheSchemaIfValid;
|
|
27
|
+
private cacheSchemas;
|
|
28
|
+
private fetchSchema;
|
|
29
|
+
}
|
|
30
|
+
//# sourceMappingURL=SchemaCache.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SchemaCache.d.ts","sourceRoot":"","sources":["../../src/SchemaCache.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAA;AAClD,OAAO,EAAE,aAAa,EAAgB,MAAM,oCAAoC,CAAA;AAKhF,OAAO,EAAE,wBAAwB,EAAE,MAAM,4BAA4B,CAAA;AAQrE,MAAM,MAAM,gBAAgB,GAAG,cAAc,CAAC,aAAa,CAAC,CAAA;AAE5D,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,cAAc,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,gBAAgB,KAAK,IAAI,CAAA;IAChE,KAAK,CAAC,EAAE,MAAM,CAAA;IAEd,OAAO,CAAC,MAAM,CAA2E;IACzF,OAAO,CAAC,WAAW,CAAa;IAGhC,OAAO,CAAC,WAAW,CAAiB;IAEpC,OAAO;IAIP,MAAM,KAAK,QAAQ,0CAKlB;IAED;;;;OAIG;IACH,IAAI,UAAU,IAAI,CAAC,CAElB;IAEK,GAAG,CAAC,MAAM,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,gBAAgB,GAAG,SAAS,GAAG,IAAI,CAAC;IAcxE,OAAO,CAAC,kBAAkB;IAgB1B,OAAO,CAAC,YAAY;YAQN,WAAW;CAqB1B"}
|
|
@@ -1,39 +1,16 @@
|
|
|
1
|
-
// src/SchemaCache.ts
|
|
2
1
|
import { isAxiosError } from "@xyo-network/axios";
|
|
3
2
|
import { DomainPayloadWrapper } from "@xyo-network/domain-payload-plugin";
|
|
4
3
|
import { handleError } from "@xyo-network/error";
|
|
5
4
|
import { SchemaSchema } from "@xyo-network/schema-payload-plugin";
|
|
6
5
|
import Ajv from "ajv";
|
|
7
6
|
import { LRUCache } from "lru-cache";
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
import { delay } from "@xylabs/delay";
|
|
11
|
-
var Debounce = class {
|
|
12
|
-
map = /* @__PURE__ */ new Map();
|
|
13
|
-
async one(key, closure, timeout = 1e4) {
|
|
14
|
-
const startTime = Date.now();
|
|
15
|
-
while (this.map.get(key)) {
|
|
16
|
-
await delay(100);
|
|
17
|
-
if (Date.now() - startTime > timeout) {
|
|
18
|
-
throw Error(`Debounce timed out [${key}]`);
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
try {
|
|
22
|
-
this.map.set(key, 1);
|
|
23
|
-
return await closure();
|
|
24
|
-
} finally {
|
|
25
|
-
this.map.set(key, 0);
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
// src/SchemaCache.ts
|
|
31
|
-
var getSchemaNameFromSchema = (schema) => {
|
|
7
|
+
import { Debounce } from "./Debounce";
|
|
8
|
+
const getSchemaNameFromSchema = (schema) => {
|
|
32
9
|
if (schema.$id) {
|
|
33
10
|
return schema.$id;
|
|
34
11
|
}
|
|
35
12
|
};
|
|
36
|
-
|
|
13
|
+
class SchemaCache {
|
|
37
14
|
/**
|
|
38
15
|
* Object representing `null` since LRU Cache types
|
|
39
16
|
* only allow for types that derive from object
|
|
@@ -51,7 +28,7 @@ var SchemaCache = class _SchemaCache {
|
|
|
51
28
|
}
|
|
52
29
|
static get instance() {
|
|
53
30
|
if (!this._instance) {
|
|
54
|
-
this._instance = new
|
|
31
|
+
this._instance = new SchemaCache();
|
|
55
32
|
}
|
|
56
33
|
return this._instance;
|
|
57
34
|
}
|
|
@@ -71,7 +48,7 @@ var SchemaCache = class _SchemaCache {
|
|
|
71
48
|
}
|
|
72
49
|
});
|
|
73
50
|
const value = this._cache.get(schema);
|
|
74
|
-
return value ===
|
|
51
|
+
return value === SchemaCache.NULL ? null : value;
|
|
75
52
|
}
|
|
76
53
|
return void 0;
|
|
77
54
|
}
|
|
@@ -99,10 +76,10 @@ var SchemaCache = class _SchemaCache {
|
|
|
99
76
|
await domain?.fetch();
|
|
100
77
|
this.cacheSchemas(domain?.aliases);
|
|
101
78
|
if (this._cache.get(schema) === void 0) {
|
|
102
|
-
this._cache.set(schema,
|
|
79
|
+
this._cache.set(schema, SchemaCache.NULL);
|
|
103
80
|
}
|
|
104
81
|
} catch (ex) {
|
|
105
|
-
this._cache.set(schema,
|
|
82
|
+
this._cache.set(schema, SchemaCache.NULL);
|
|
106
83
|
if (isAxiosError(ex)) {
|
|
107
84
|
console.log(`Axios Url: ${ex.response?.config.url}`);
|
|
108
85
|
}
|
|
@@ -111,7 +88,7 @@ var SchemaCache = class _SchemaCache {
|
|
|
111
88
|
});
|
|
112
89
|
}
|
|
113
90
|
}
|
|
114
|
-
}
|
|
91
|
+
}
|
|
115
92
|
export {
|
|
116
93
|
SchemaCache
|
|
117
94
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/SchemaCache.ts"
|
|
1
|
+
{"version":3,"sources":["../../src/SchemaCache.ts"],"sourcesContent":["import { isAxiosError } from '@xyo-network/axios'\nimport { DomainPayloadWrapper } from '@xyo-network/domain-payload-plugin'\nimport { handleError } from '@xyo-network/error'\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'\nimport { SchemaNameToValidatorMap } from './SchemaNameToValidatorMap'\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 aliasEntries\n ?.filter((entry) => entry.payload.schema === SchemaSchema)\n .forEach((entry) => {\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 (ex) {\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(ex)) {\n console.log(`Axios Url: ${ex.response?.config.url}`)\n }\n handleError(ex, (error) => {\n console.error(`fetchSchema threw: ${error.message}`)\n })\n }\n }\n}\n"],"mappings":"AAAA,SAAS,oBAAoB;AAC7B,SAAS,4BAA4B;AACrC,SAAS,mBAAmB;AAE5B,SAAwB,oBAAoB;AAC5C,OAAO,SAA2B;AAClC,SAAS,gBAAgB;AAEzB,SAAS,gBAAgB;AAGzB,MAAM,0BAA0B,CAAC,WAAyB;AACxD,MAAI,OAAO,KAAK;AACd,WAAO,OAAO;AAAA,EAChB;AACF;AAIO,MAAM,YAA2E;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,IAAI,SAAS;AAAA,EAE3B,YAAY,OAAgB;AAClC,SAAK,QAAQ;AAAA,EACf;AAAA,EAEA,WAAW,WAAW;AACpB,QAAI,CAAC,KAAK,WAAW;AACnB,WAAK,YAAY,IAAI,YAAY;AAAA,IACnC;AACA,WAAO,KAAK;AAAA,EACd;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,IAAI,aAAgB;AAClB,WAAO,KAAK;AAAA,EACd;AAAA,EAEA,MAAM,IAAI,QAA+D;AACvE,QAAI,QAAQ;AACV,YAAM,KAAK,YAAY,IAAI,QAAQ,YAAY;AAE7C,YAAI,KAAK,OAAO,IAAI,MAAM,MAAM,QAAW;AACzC,gBAAM,KAAK,YAAY,MAAM;AAAA,QAC/B;AAAA,MACF,CAAC;AACD,YAAM,QAAQ,KAAK,OAAO,IAAI,MAAM;AACpC,aAAO,UAAU,YAAY,OAAO,OAAO;AAAA,IAC7C;AACA,WAAO;AAAA,EACT;AAAA,EAEQ,mBAAmB,OAAyB;AAElD,QAAI,MAAM,QAAQ,YAAY;AAC5B,YAAM,MAAM,IAAI,IAAI,EAAE,QAAQ,MAAM,CAAC;AAErC,YAAM,YAAY,IAAI,QAAQ,MAAM,QAAQ,UAAU;AACtD,YAAM,aAAa,wBAAwB,MAAM,QAAQ,UAAU;AACnE,UAAI,YAAY;AACd,aAAK,OAAO,IAAI,YAAY,KAAK;AACjC,cAAM,MAAM;AACZ,aAAK,YAAY,GAAG,IAAI;AACxB,aAAK,iBAAiB,YAAY,KAAK;AAAA,MACzC;AAAA,IACF;AAAA,EACF;AAAA,EAEQ,aAAa,cAAwC;AAC3D,kBACI,OAAO,CAAC,UAAU,MAAM,QAAQ,WAAW,YAAY,EACxD,QAAQ,CAAC,UAAU;AAClB,WAAK,mBAAmB,KAAyB;AAAA,IACnD,CAAC;AAAA,EACL;AAAA,EAEA,MAAc,YAAY,QAAgB;AACxC,QAAI;AACF,YAAM,SAAS,MAAM,qBAAqB,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,YAAY,IAAI;AAAA,MAC1C;AAAA,IACF,SAAS,IAAI;AAEX,WAAK,OAAO,IAAI,QAAQ,YAAY,IAAI;AACxC,UAAI,aAAa,EAAE,GAAG;AACpB,gBAAQ,IAAI,cAAc,GAAG,UAAU,OAAO,GAAG,EAAE;AAAA,MACrD;AACA,kBAAY,IAAI,CAAC,UAAU;AACzB,gBAAQ,MAAM,sBAAsB,MAAM,OAAO,EAAE;AAAA,MACrD,CAAC;AAAA,IACH;AAAA,EACF;AACF;","names":[]}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { DomainPayload, DomainSchema } from '@xyo-network/domain-payload-plugin';
|
|
2
|
+
import { Payload, PayloadSchema } from '@xyo-network/payload-model';
|
|
3
|
+
import { SchemaPayload, SchemaSchema } from '@xyo-network/schema-payload-plugin';
|
|
4
|
+
/**
|
|
5
|
+
* Used in conjunction with schema validation to support compile time type assertion
|
|
6
|
+
* for known schema types.
|
|
7
|
+
*/
|
|
8
|
+
export type NarrowPayload<T extends Payload = Payload> = ((x: Payload) => x is T) | undefined;
|
|
9
|
+
/**
|
|
10
|
+
* Used to map known schemas (byt their string name) to the validators which assert their types
|
|
11
|
+
*/
|
|
12
|
+
export interface SchemaNameToValidatorMap {
|
|
13
|
+
[DomainSchema]: NarrowPayload<DomainPayload>;
|
|
14
|
+
[PayloadSchema]: NarrowPayload<Payload>;
|
|
15
|
+
[SchemaSchema]: NarrowPayload<SchemaPayload>;
|
|
16
|
+
}
|
|
17
|
+
//# sourceMappingURL=SchemaNameToValidatorMap.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SchemaNameToValidatorMap.d.ts","sourceRoot":"","sources":["../../src/SchemaNameToValidatorMap.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,YAAY,EAAE,MAAM,oCAAoC,CAAA;AAChF,OAAO,EAAE,OAAO,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAA;AACnE,OAAO,EAAE,aAAa,EAAE,YAAY,EAAE,MAAM,oCAAoC,CAAA;AAEhF;;;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;IACvC,CAAC,YAAY,CAAC,EAAE,aAAa,CAAC,aAAa,CAAC,CAAA;IAC5C,CAAC,aAAa,CAAC,EAAE,aAAa,CAAC,OAAO,CAAC,CAAA;IACvC,CAAC,YAAY,CAAC,EAAE,aAAa,CAAC,aAAa,CAAC,CAAA;CAC7C"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAA;AAC1B,cAAc,eAAe,CAAA;AAC7B,cAAc,4BAA4B,CAAA"}
|
package/dist/browser/index.js
CHANGED
|
@@ -1,117 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
map = /* @__PURE__ */ new Map();
|
|
5
|
-
async one(key, closure, timeout = 1e4) {
|
|
6
|
-
const startTime = Date.now();
|
|
7
|
-
while (this.map.get(key)) {
|
|
8
|
-
await delay(100);
|
|
9
|
-
if (Date.now() - startTime > timeout) {
|
|
10
|
-
throw Error(`Debounce timed out [${key}]`);
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
try {
|
|
14
|
-
this.map.set(key, 1);
|
|
15
|
-
return await closure();
|
|
16
|
-
} finally {
|
|
17
|
-
this.map.set(key, 0);
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
// src/SchemaCache.ts
|
|
23
|
-
import { isAxiosError } from "@xyo-network/axios";
|
|
24
|
-
import { DomainPayloadWrapper } from "@xyo-network/domain-payload-plugin";
|
|
25
|
-
import { handleError } from "@xyo-network/error";
|
|
26
|
-
import { SchemaSchema } from "@xyo-network/schema-payload-plugin";
|
|
27
|
-
import Ajv from "ajv";
|
|
28
|
-
import { LRUCache } from "lru-cache";
|
|
29
|
-
var getSchemaNameFromSchema = (schema) => {
|
|
30
|
-
if (schema.$id) {
|
|
31
|
-
return schema.$id;
|
|
32
|
-
}
|
|
33
|
-
};
|
|
34
|
-
var SchemaCache = class _SchemaCache {
|
|
35
|
-
/**
|
|
36
|
-
* Object representing `null` since LRU Cache types
|
|
37
|
-
* only allow for types that derive from object
|
|
38
|
-
*/
|
|
39
|
-
static NULL = { payload: { definition: {}, schema: SchemaSchema } };
|
|
40
|
-
static _instance;
|
|
41
|
-
onSchemaCached;
|
|
42
|
-
proxy;
|
|
43
|
-
_cache = new LRUCache({ max: 500, ttl: 1e3 * 60 * 5 });
|
|
44
|
-
_validators = {};
|
|
45
|
-
//prevents double discovery
|
|
46
|
-
getDebounce = new Debounce();
|
|
47
|
-
constructor(proxy) {
|
|
48
|
-
this.proxy = proxy;
|
|
49
|
-
}
|
|
50
|
-
static get instance() {
|
|
51
|
-
if (!this._instance) {
|
|
52
|
-
this._instance = new _SchemaCache();
|
|
53
|
-
}
|
|
54
|
-
return this._instance;
|
|
55
|
-
}
|
|
56
|
-
/**
|
|
57
|
-
* A map of cached schema (by name) to payload validators for the schema. A schema
|
|
58
|
-
* must be cached via `get('schema.name')` before it's validator can be used as
|
|
59
|
-
* they are compiled dynamically at runtime upon retrieval.
|
|
60
|
-
*/
|
|
61
|
-
get validators() {
|
|
62
|
-
return this._validators;
|
|
63
|
-
}
|
|
64
|
-
async get(schema) {
|
|
65
|
-
if (schema) {
|
|
66
|
-
await this.getDebounce.one(schema, async () => {
|
|
67
|
-
if (this._cache.get(schema) === void 0) {
|
|
68
|
-
await this.fetchSchema(schema);
|
|
69
|
-
}
|
|
70
|
-
});
|
|
71
|
-
const value = this._cache.get(schema);
|
|
72
|
-
return value === _SchemaCache.NULL ? null : value;
|
|
73
|
-
}
|
|
74
|
-
return void 0;
|
|
75
|
-
}
|
|
76
|
-
cacheSchemaIfValid(entry) {
|
|
77
|
-
if (entry.payload.definition) {
|
|
78
|
-
const ajv = new Ajv({ strict: false });
|
|
79
|
-
const validator = ajv.compile(entry.payload.definition);
|
|
80
|
-
const schemaName = getSchemaNameFromSchema(entry.payload.definition);
|
|
81
|
-
if (schemaName) {
|
|
82
|
-
this._cache.set(schemaName, entry);
|
|
83
|
-
const key = schemaName;
|
|
84
|
-
this._validators[key] = validator;
|
|
85
|
-
this.onSchemaCached?.(schemaName, entry);
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
cacheSchemas(aliasEntries) {
|
|
90
|
-
aliasEntries?.filter((entry) => entry.payload.schema === SchemaSchema).forEach((entry) => {
|
|
91
|
-
this.cacheSchemaIfValid(entry);
|
|
92
|
-
});
|
|
93
|
-
}
|
|
94
|
-
async fetchSchema(schema) {
|
|
95
|
-
try {
|
|
96
|
-
const domain = await DomainPayloadWrapper.discover(schema, this.proxy);
|
|
97
|
-
await domain?.fetch();
|
|
98
|
-
this.cacheSchemas(domain?.aliases);
|
|
99
|
-
if (this._cache.get(schema) === void 0) {
|
|
100
|
-
this._cache.set(schema, _SchemaCache.NULL);
|
|
101
|
-
}
|
|
102
|
-
} catch (ex) {
|
|
103
|
-
this._cache.set(schema, _SchemaCache.NULL);
|
|
104
|
-
if (isAxiosError(ex)) {
|
|
105
|
-
console.log(`Axios Url: ${ex.response?.config.url}`);
|
|
106
|
-
}
|
|
107
|
-
handleError(ex, (error) => {
|
|
108
|
-
console.error(`fetchSchema threw: ${error.message}`);
|
|
109
|
-
});
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
};
|
|
113
|
-
export {
|
|
114
|
-
Debounce,
|
|
115
|
-
SchemaCache
|
|
116
|
-
};
|
|
1
|
+
export * from "./Debounce";
|
|
2
|
+
export * from "./SchemaCache";
|
|
3
|
+
export * from "./SchemaNameToValidatorMap";
|
|
117
4
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/
|
|
1
|
+
{"version":3,"sources":["../../src/index.ts"],"sourcesContent":["export * from './Debounce'\nexport * from './SchemaCache'\nexport * from './SchemaNameToValidatorMap'\n"],"mappings":"AAAA,cAAc;AACd,cAAc;AACd,cAAc;","names":[]}
|