@xyo-network/schema-cache 2.106.0 → 2.107.1

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.
Files changed (50) hide show
  1. package/dist/browser/SchemaCache.d.cts +0 -9
  2. package/dist/browser/SchemaCache.d.cts.map +1 -1
  3. package/dist/browser/SchemaCache.d.mts +0 -9
  4. package/dist/browser/SchemaCache.d.mts.map +1 -1
  5. package/dist/browser/SchemaCache.d.ts +0 -9
  6. package/dist/browser/SchemaCache.d.ts.map +1 -1
  7. package/dist/browser/SchemaNameToValidatorMap.d.cts +0 -7
  8. package/dist/browser/SchemaNameToValidatorMap.d.cts.map +1 -1
  9. package/dist/browser/SchemaNameToValidatorMap.d.mts +0 -7
  10. package/dist/browser/SchemaNameToValidatorMap.d.mts.map +1 -1
  11. package/dist/browser/SchemaNameToValidatorMap.d.ts +0 -7
  12. package/dist/browser/SchemaNameToValidatorMap.d.ts.map +1 -1
  13. package/dist/browser/index.cjs +1 -166
  14. package/dist/browser/index.cjs.map +1 -1
  15. package/dist/browser/index.js +1 -135
  16. package/dist/browser/index.js.map +1 -1
  17. package/dist/neutral/SchemaCache.d.cts +0 -9
  18. package/dist/neutral/SchemaCache.d.cts.map +1 -1
  19. package/dist/neutral/SchemaCache.d.mts +0 -9
  20. package/dist/neutral/SchemaCache.d.mts.map +1 -1
  21. package/dist/neutral/SchemaCache.d.ts +0 -9
  22. package/dist/neutral/SchemaCache.d.ts.map +1 -1
  23. package/dist/neutral/SchemaNameToValidatorMap.d.cts +0 -7
  24. package/dist/neutral/SchemaNameToValidatorMap.d.cts.map +1 -1
  25. package/dist/neutral/SchemaNameToValidatorMap.d.mts +0 -7
  26. package/dist/neutral/SchemaNameToValidatorMap.d.mts.map +1 -1
  27. package/dist/neutral/SchemaNameToValidatorMap.d.ts +0 -7
  28. package/dist/neutral/SchemaNameToValidatorMap.d.ts.map +1 -1
  29. package/dist/neutral/index.cjs +1 -166
  30. package/dist/neutral/index.cjs.map +1 -1
  31. package/dist/neutral/index.js +1 -135
  32. package/dist/neutral/index.js.map +1 -1
  33. package/dist/node/SchemaCache.d.cts +0 -9
  34. package/dist/node/SchemaCache.d.cts.map +1 -1
  35. package/dist/node/SchemaCache.d.mts +0 -9
  36. package/dist/node/SchemaCache.d.mts.map +1 -1
  37. package/dist/node/SchemaCache.d.ts +0 -9
  38. package/dist/node/SchemaCache.d.ts.map +1 -1
  39. package/dist/node/SchemaNameToValidatorMap.d.cts +0 -7
  40. package/dist/node/SchemaNameToValidatorMap.d.cts.map +1 -1
  41. package/dist/node/SchemaNameToValidatorMap.d.mts +0 -7
  42. package/dist/node/SchemaNameToValidatorMap.d.mts.map +1 -1
  43. package/dist/node/SchemaNameToValidatorMap.d.ts +0 -7
  44. package/dist/node/SchemaNameToValidatorMap.d.ts.map +1 -1
  45. package/dist/node/index.cjs +1 -173
  46. package/dist/node/index.cjs.map +1 -1
  47. package/dist/node/index.js +1 -137
  48. package/dist/node/index.js.map +1 -1
  49. package/package.json +10 -10
  50. package/src/SchemaCache.ts +0 -1
@@ -3,10 +3,6 @@ import { SchemaPayload } from '@xyo-network/schema-payload-plugin';
3
3
  import { SchemaNameToValidatorMap } from './SchemaNameToValidatorMap';
4
4
  export type SchemaCacheEntry = FetchedPayload<SchemaPayload>;
5
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
6
  protected static readonly NULL: SchemaCacheEntry;
11
7
  private static _instance?;
12
8
  onSchemaCached?: (name: string, entry: SchemaCacheEntry) => void;
@@ -16,11 +12,6 @@ export declare class SchemaCache<T extends SchemaNameToValidatorMap = SchemaName
16
12
  private getDebounce;
17
13
  private constructor();
18
14
  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
15
  get validators(): T;
25
16
  get(schema?: string): Promise<SchemaCacheEntry | undefined | null>;
26
17
  private cacheSchemaIfValid;
@@ -1 +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;AAMhF,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;YAMN,WAAW;CAqB1B"}
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;IAKpF,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;IAOD,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;YAMN,WAAW;CAqB1B"}
@@ -3,10 +3,6 @@ import { SchemaPayload } from '@xyo-network/schema-payload-plugin';
3
3
  import { SchemaNameToValidatorMap } from './SchemaNameToValidatorMap';
4
4
  export type SchemaCacheEntry = FetchedPayload<SchemaPayload>;
5
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
6
  protected static readonly NULL: SchemaCacheEntry;
11
7
  private static _instance?;
12
8
  onSchemaCached?: (name: string, entry: SchemaCacheEntry) => void;
@@ -16,11 +12,6 @@ export declare class SchemaCache<T extends SchemaNameToValidatorMap = SchemaName
16
12
  private getDebounce;
17
13
  private constructor();
18
14
  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
15
  get validators(): T;
25
16
  get(schema?: string): Promise<SchemaCacheEntry | undefined | null>;
26
17
  private cacheSchemaIfValid;
@@ -1 +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;AAMhF,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;YAMN,WAAW;CAqB1B"}
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;IAKpF,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;IAOD,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;YAMN,WAAW;CAqB1B"}
@@ -3,10 +3,6 @@ import { SchemaPayload } from '@xyo-network/schema-payload-plugin';
3
3
  import { SchemaNameToValidatorMap } from './SchemaNameToValidatorMap';
4
4
  export type SchemaCacheEntry = FetchedPayload<SchemaPayload>;
5
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
6
  protected static readonly NULL: SchemaCacheEntry;
11
7
  private static _instance?;
12
8
  onSchemaCached?: (name: string, entry: SchemaCacheEntry) => void;
@@ -16,11 +12,6 @@ export declare class SchemaCache<T extends SchemaNameToValidatorMap = SchemaName
16
12
  private getDebounce;
17
13
  private constructor();
18
14
  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
15
  get validators(): T;
25
16
  get(schema?: string): Promise<SchemaCacheEntry | undefined | null>;
26
17
  private cacheSchemaIfValid;
@@ -1 +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;AAMhF,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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} from '@xyo-network/domain-payload-plugin';
2
2
  import { Payload, PayloadSchema } from '@xyo-network/payload-model';
3
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
4
  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
5
  export interface SchemaNameToValidatorMap {
13
6
  [DomainSchema]: NarrowPayload<DomainPayload>;
14
7
  [PayloadSchema]: NarrowPayload<Payload>;
@@ -1 +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"}
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;AAMhF,MAAM,MAAM,aAAa,CAAC,CAAC,SAAS,OAAO,GAAG,OAAO,IAAI,CAAC,CAAC,CAAC,EAAE,OAAO,KAAK,CAAC,IAAI,CAAC,CAAC,GAAG,SAAS,CAAA;AAK7F,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"}
@@ -1,14 +1,7 @@
1
1
  import { DomainPayload, DomainSchema } from '@xyo-network/domain-payload-plugin';
2
2
  import { Payload, PayloadSchema } from '@xyo-network/payload-model';
3
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
4
  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
5
  export interface SchemaNameToValidatorMap {
13
6
  [DomainSchema]: NarrowPayload<DomainPayload>;
14
7
  [PayloadSchema]: NarrowPayload<Payload>;
@@ -1 +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"}
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;AAMhF,MAAM,MAAM,aAAa,CAAC,CAAC,SAAS,OAAO,GAAG,OAAO,IAAI,CAAC,CAAC,CAAC,EAAE,OAAO,KAAK,CAAC,IAAI,CAAC,CAAC,GAAG,SAAS,CAAA;AAK7F,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"}
@@ -1,14 +1,7 @@
1
1
  import { DomainPayload, DomainSchema } from '@xyo-network/domain-payload-plugin';
2
2
  import { Payload, PayloadSchema } from '@xyo-network/payload-model';
3
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
4
  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
5
  export interface SchemaNameToValidatorMap {
13
6
  [DomainSchema]: NarrowPayload<DomainPayload>;
14
7
  [PayloadSchema]: NarrowPayload<Payload>;
@@ -1 +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"}
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;AAMhF,MAAM,MAAM,aAAa,CAAC,CAAC,SAAS,OAAO,GAAG,OAAO,IAAI,CAAC,CAAC,CAAC,EAAE,OAAO,KAAK,CAAC,IAAI,CAAC,CAAC,GAAG,SAAS,CAAA;AAK7F,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"}
@@ -1,167 +1,2 @@
1
- "use strict";
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
9
- var __export = (target, all) => {
10
- for (var name in all)
11
- __defProp(target, name, { get: all[name], enumerable: true });
12
- };
13
- var __copyProps = (to, from, except, desc) => {
14
- if (from && typeof from === "object" || typeof from === "function") {
15
- for (let key of __getOwnPropNames(from))
16
- if (!__hasOwnProp.call(to, key) && key !== except)
17
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
18
- }
19
- return to;
20
- };
21
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
22
- // If the importer is in node compatibility mode or this is not an ESM
23
- // file that has been converted to a CommonJS file using a Babel-
24
- // compatible transform (i.e. "__esModule" has not been set), then set
25
- // "default" to the CommonJS "module.exports" for node compatibility.
26
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
27
- mod
28
- ));
29
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
30
-
31
- // src/index.ts
32
- var src_exports = {};
33
- __export(src_exports, {
34
- Debounce: () => Debounce,
35
- SchemaCache: () => SchemaCache
36
- });
37
- module.exports = __toCommonJS(src_exports);
38
-
39
- // src/Debounce.ts
40
- var import_delay = require("@xylabs/delay");
41
- var Debounce = class {
42
- static {
43
- __name(this, "Debounce");
44
- }
45
- map = /* @__PURE__ */ new Map();
46
- async one(key, closure, timeout = 1e4) {
47
- const startTime = Date.now();
48
- while (this.map.get(key)) {
49
- await (0, import_delay.delay)(100);
50
- if (Date.now() - startTime > timeout) {
51
- throw new Error(`Debounce timed out [${key}]`);
52
- }
53
- }
54
- try {
55
- this.map.set(key, 1);
56
- return await closure();
57
- } finally {
58
- this.map.set(key, 0);
59
- }
60
- }
61
- };
62
-
63
- // src/SchemaCache.ts
64
- var import_axios = require("@xylabs/axios");
65
- var import_error = require("@xylabs/error");
66
- var import_domain_payload_plugin = require("@xyo-network/domain-payload-plugin");
67
- var import_schema_payload_plugin = require("@xyo-network/schema-payload-plugin");
68
- var import_ajv = __toESM(require("ajv"), 1);
69
- var import_lru_cache = require("lru-cache");
70
- var getSchemaNameFromSchema = /* @__PURE__ */ __name((schema) => {
71
- if (schema.$id) {
72
- return schema.$id;
73
- }
74
- }, "getSchemaNameFromSchema");
75
- var SchemaCache = class _SchemaCache {
76
- static {
77
- __name(this, "SchemaCache");
78
- }
79
- /**
80
- * Object representing `null` since LRU Cache types
81
- * only allow for types that derive from object
82
- */
83
- static NULL = {
84
- payload: {
85
- definition: {},
86
- schema: import_schema_payload_plugin.SchemaSchema
87
- }
88
- };
89
- static _instance;
90
- onSchemaCached;
91
- proxy;
92
- _cache = new import_lru_cache.LRUCache({
93
- max: 500,
94
- ttl: 1e3 * 60 * 5
95
- });
96
- _validators = {};
97
- //prevents double discovery
98
- getDebounce = new Debounce();
99
- constructor(proxy) {
100
- this.proxy = proxy;
101
- }
102
- static get instance() {
103
- if (!this._instance) {
104
- this._instance = new _SchemaCache();
105
- }
106
- return this._instance;
107
- }
108
- /**
109
- * A map of cached schema (by name) to payload validators for the schema. A schema
110
- * must be cached via `get('schema.name')` before it's validator can be used as
111
- * they are compiled dynamically at runtime upon retrieval.
112
- */
113
- get validators() {
114
- return this._validators;
115
- }
116
- async get(schema) {
117
- if (schema) {
118
- await this.getDebounce.one(schema, async () => {
119
- if (this._cache.get(schema) === void 0) {
120
- await this.fetchSchema(schema);
121
- }
122
- });
123
- const value = this._cache.get(schema);
124
- return value === _SchemaCache.NULL ? null : value;
125
- }
126
- return void 0;
127
- }
128
- cacheSchemaIfValid(entry) {
129
- if (entry.payload.definition) {
130
- const ajv = new import_ajv.default({
131
- strict: false
132
- });
133
- const validator = ajv.compile(entry.payload.definition);
134
- const schemaName = getSchemaNameFromSchema(entry.payload.definition);
135
- if (schemaName) {
136
- this._cache.set(schemaName, entry);
137
- const key = schemaName;
138
- this._validators[key] = validator;
139
- this.onSchemaCached?.(schemaName, entry);
140
- }
141
- }
142
- }
143
- cacheSchemas(aliasEntries) {
144
- for (const entry of aliasEntries?.filter((entry2) => entry2.payload.schema === import_schema_payload_plugin.SchemaSchema) ?? []) {
145
- this.cacheSchemaIfValid(entry);
146
- }
147
- }
148
- async fetchSchema(schema) {
149
- try {
150
- const domain = await import_domain_payload_plugin.DomainPayloadWrapper.discover(schema, this.proxy);
151
- await domain?.fetch();
152
- this.cacheSchemas(domain?.aliases);
153
- if (this._cache.get(schema) === void 0) {
154
- this._cache.set(schema, _SchemaCache.NULL);
155
- }
156
- } catch (error) {
157
- this._cache.set(schema, _SchemaCache.NULL);
158
- if ((0, import_axios.isAxiosError)(error)) {
159
- console.log(`Axios Url: ${error.response?.config.url}`);
160
- }
161
- (0, import_error.handleError)(error, (error2) => {
162
- console.error(`fetchSchema threw: ${error2.message}`);
163
- });
164
- }
165
- }
166
- };
1
+ "use strict";var y=Object.create;var r=Object.defineProperty;var S=Object.getOwnPropertyDescriptor;var x=Object.getOwnPropertyNames;var _=Object.getPrototypeOf,v=Object.prototype.hasOwnProperty;var c=(e,t)=>r(e,"name",{value:t,configurable:!0});var L=(e,t)=>{for(var a in t)r(e,a,{get:t[a],enumerable:!0})},m=(e,t,a,o)=>{if(t&&typeof t=="object"||typeof t=="function")for(let i of x(t))!v.call(e,i)&&i!==a&&r(e,i,{get:()=>t[i],enumerable:!(o=S(t,i))||o.enumerable});return e};var D=(e,t,a)=>(a=e!=null?y(_(e)):{},m(t||!e||!e.__esModule?r(a,"default",{value:e,enumerable:!0}):a,e)),N=e=>m(r({},"__esModule",{value:!0}),e);var $={};L($,{Debounce:()=>s,SchemaCache:()=>h});module.exports=N($);var f=require("@xylabs/delay");var s=class{static{c(this,"Debounce")}map=new Map;async one(t,a,o=1e4){let i=Date.now();for(;this.map.get(t);)if(await(0,f.delay)(100),Date.now()-i>o)throw new Error(`Debounce timed out [${t}]`);try{return this.map.set(t,1),await a()}finally{this.map.set(t,0)}}};var l=require("@xylabs/axios"),d=require("@xylabs/error"),p=require("@xyo-network/domain-payload-plugin"),n=require("@xyo-network/schema-payload-plugin"),u=D(require("ajv"),1),w=require("lru-cache");var U=c(e=>{if(e.$id)return e.$id},"getSchemaNameFromSchema"),h=class e{static{c(this,"SchemaCache")}static NULL={payload:{definition:{},schema:n.SchemaSchema}};static _instance;onSchemaCached;proxy;_cache=new w.LRUCache({max:500,ttl:1e3*60*5});_validators={};getDebounce=new s;constructor(t){this.proxy=t}static get instance(){return this._instance||(this._instance=new e),this._instance}get validators(){return this._validators}async get(t){if(t){await this.getDebounce.one(t,async()=>{this._cache.get(t)===void 0&&await this.fetchSchema(t)});let a=this._cache.get(t);return a===e.NULL?null:a}}cacheSchemaIfValid(t){if(t.payload.definition){let o=new u.default({strict:!1}).compile(t.payload.definition),i=U(t.payload.definition);if(i){this._cache.set(i,t);let g=i;this._validators[g]=o,this.onSchemaCached?.(i,t)}}}cacheSchemas(t){for(let a of t?.filter(o=>o.payload.schema===n.SchemaSchema)??[])this.cacheSchemaIfValid(a)}async fetchSchema(t){try{let a=await p.DomainPayloadWrapper.discover(t,this.proxy);await a?.fetch(),this.cacheSchemas(a?.aliases),this._cache.get(t)===void 0&&this._cache.set(t,e.NULL)}catch(a){this._cache.set(t,e.NULL),(0,l.isAxiosError)(a)&&console.log(`Axios Url: ${a.response?.config.url}`),(0,d.handleError)(a,o=>{console.error(`fetchSchema threw: ${o.message}`)})}}};
167
2
  //# sourceMappingURL=index.cjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/index.ts","../../src/Debounce.ts","../../src/SchemaCache.ts"],"sourcesContent":["export * from './Debounce'\nexport * from './SchemaCache'\nexport * from './SchemaNameToValidatorMap'\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'\n// eslint-disable-next-line import/no-named-as-default\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 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;;;;;;;;ACAA,mBAAsB;AAEf,IAAMA,WAAN,MAAMA;EAFb,OAEaA;;;EACHC,MAAM,oBAAIC,IAAAA;EAElB,MAAMC,IAAOC,KAAWC,SAA2BC,UAAU,KAAQ;AACnE,UAAMC,YAAYC,KAAKC,IAAG;AAC1B,WAAO,KAAKR,IAAIS,IAAIN,GAAAA,GAAM;AACxB,gBAAMO,oBAAM,GAAA;AACZ,UAAIH,KAAKC,IAAG,IAAKF,YAAYD,SAAS;AACpC,cAAM,IAAIM,MAAM,uBAAuBR,GAAAA,GAAM;MAC/C;IACF;AACA,QAAI;AACF,WAAKH,IAAIY,IAAIT,KAAK,CAAA;AAClB,aAAO,MAAMC,QAAAA;IACf,UAAA;AACE,WAAKJ,IAAIY,IAAIT,KAAK,CAAA;IACpB;EACF;AACF;;;ACpBA,mBAA6B;AAC7B,mBAA4B;AAC5B,mCAAqC;AAErC,mCAA4C;AAE5C,iBAAkC;AAClC,uBAAyB;AAKzB,IAAMU,0BAA0B,wBAACC,WAAAA;AAC/B,MAAIA,OAAOC,KAAK;AACd,WAAOD,OAAOC;EAChB;AACF,GAJgC;AAQzB,IAAMC,cAAN,MAAMA,aAAAA;EApBb,OAoBaA;;;;;;;EAKX,OAA0BC,OAAyB;IAAEC,SAAS;MAAEC,YAAY,CAAC;MAAGL,QAAQM;IAAa;EAAE;EAEvG,OAAeC;EAEfC;EACAC;EAEQC,SAAS,IAAIC,0BAAmC;IAAEC,KAAK;IAAKC,KAAK,MAAO,KAAK;EAAE,CAAA;EAC/EC,cAAiB,CAAC;;EAGlBC,cAAc,IAAIC,SAAAA;EAE1B,YAAoBP,OAAgB;AAClC,SAAKA,QAAQA;EACf;EAEA,WAAWQ,WAAW;AACp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/B,OAAOS,IAAInB,MAAAA,MAAYqB,QAAW;AACzC,aAAKX,OAAOsB,IAAIhC,QAAQE,aAAYC,IAAI;MAC1C;IACF,SAASuC,OAAO;AAEd,WAAKhC,OAAOsB,IAAIhC,QAAQE,aAAYC,IAAI;AACxC,cAAIwC,2BAAaD,KAAAA,GAAQ;AACvBE,gBAAQC,IAAI,cAAcH,MAAMI,UAAUC,OAAOC,GAAAA,EAAK;MACxD;AACAC,oCAAYP,OAAO,CAACA,WAAAA;AAClBE,gBAAQF,MAAM,sBAAsBA,OAAMQ,OAAO,EAAE;MACrD,CAAA;IACF;EACF;AACF;","names":["Debounce","map","Map","one","key","closure","timeout","startTime","Date","now","get","delay","Error","set","getSchemaNameFromSchema","schema","$id","SchemaCache","NULL","payload","definition","SchemaSchema","_instance","onSchemaCached","proxy","_cache","LRUCache","max","ttl","_validators","getDebounce","Debounce","instance","validators","get","one","undefined","fetchSchema","value","cacheSchemaIfValid","entry","ajv","Ajv","strict","validator","compile","schemaName","set","key","cacheSchemas","aliasEntries","filter","domain","DomainPayloadWrapper","discover","fetch","aliases","error","isAxiosError","console","log","response","config","url","handleError","message"]}
1
+ {"version":3,"sources":["../../src/index.ts","../../src/Debounce.ts","../../src/SchemaCache.ts"],"sourcesContent":["export * from './Debounce'\nexport * from './SchemaCache'\nexport * from './SchemaNameToValidatorMap'\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'\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 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":"6mBAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,cAAAE,EAAA,gBAAAC,IAAA,eAAAC,EAAAJ,GCAA,IAAAK,EAAsB,yBAEf,IAAMC,EAAN,KAAMA,CAFb,MAEaA,CAAAA,EAAAA,iBACHC,IAAM,IAAIC,IAElB,MAAMC,IAAOC,EAAWC,EAA2BC,EAAU,IAAQ,CACnE,IAAMC,EAAYC,KAAKC,IAAG,EAC1B,KAAO,KAAKR,IAAIS,IAAIN,CAAAA,GAElB,GADA,QAAMO,SAAM,GAAA,EACRH,KAAKC,IAAG,EAAKF,EAAYD,EAC3B,MAAM,IAAIM,MAAM,uBAAuBR,CAAAA,GAAM,EAGjD,GAAI,CACF,YAAKH,IAAIY,IAAIT,EAAK,CAAA,EACX,MAAMC,EAAAA,CACf,QAAA,CACE,KAAKJ,IAAIY,IAAIT,EAAK,CAAA,CACpB,CACF,CACF,ECpBA,IAAAU,EAA6B,yBAC7BC,EAA4B,yBAC5BC,EA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AAA,EAAON,EACxB,KAAKlB,iBAAiBsB,EAAYL,CAAAA,CACpC,CACF,CACF,CAEQQ,aAAaC,EAAwC,CAC3D,QAAWT,KAASS,GAAcC,OAAQV,GAAUA,EAAMrB,QAAQJ,SAAWM,cAAAA,GAAiB,CAAA,EAC5F,KAAKkB,mBAAmBC,CAAAA,CAE5B,CAEA,MAAcH,YAAYtB,EAAgB,CACxC,GAAI,CACF,IAAMoC,EAAS,MAAMC,uBAAqBC,SAAStC,EAAQ,KAAKS,KAAK,EACrE,MAAM2B,GAAQG,MAAAA,EACd,KAAKN,aAAaG,GAAQI,OAAAA,EAGtB,KAAK9B,OAAOS,IAAInB,CAAAA,IAAYqB,QAC9B,KAAKX,OAAOqB,IAAI/B,EAAQE,EAAYC,IAAI,CAE5C,OAASsC,EAAO,CAEd,KAAK/B,OAAOqB,IAAI/B,EAAQE,EAAYC,IAAI,KACpCuC,gBAAaD,CAAAA,GACfE,QAAQC,IAAI,cAAcH,EAAMI,UAAUC,OAAOC,GAAAA,EAAK,KAExDC,eAAYP,EAAQA,GAAAA,CAClBE,QAAQF,MAAM,sBAAsBA,EAAMQ,OAAO,EAAE,CACrD,CAAA,CACF,CACF,CACF","names":["src_exports","__export","Debounce","SchemaCache","__toCommonJS","import_delay","Debounce","map","Map","one","key","closure","timeout","startTime","Date","now","get","delay","Error","set","import_axios","import_error","import_domain_payload_plugin","import_schema_payload_plugin","import_ajv","import_lru_cache","getSchemaNameFromSchema","__name","schema","$id","SchemaCache","NULL","payload","definition","SchemaSchema","_instance","onSchemaCached","proxy","_cache","LRUCache","max","ttl","_validators","getDebounce","Debounce","instance","validators","get","one","undefined","fetchSchema","value","cacheSchemaIfValid","entry","validator","Ajv","strict","compile","schemaName","set","key","cacheSchemas","aliasEntries","filter","domain","DomainPayloadWrapper","discover","fetch","aliases","error","isAxiosError","console","log","response","config","url","handleError","message"]}
@@ -1,136 +1,2 @@
1
- var __defProp = Object.defineProperty;
2
- var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
-
4
- // src/Debounce.ts
5
- import { delay } from "@xylabs/delay";
6
- var Debounce = class {
7
- static {
8
- __name(this, "Debounce");
9
- }
10
- map = /* @__PURE__ */ new Map();
11
- async one(key, closure, timeout = 1e4) {
12
- const startTime = Date.now();
13
- while (this.map.get(key)) {
14
- await delay(100);
15
- if (Date.now() - startTime > timeout) {
16
- throw new Error(`Debounce timed out [${key}]`);
17
- }
18
- }
19
- try {
20
- this.map.set(key, 1);
21
- return await closure();
22
- } finally {
23
- this.map.set(key, 0);
24
- }
25
- }
26
- };
27
-
28
- // src/SchemaCache.ts
29
- import { isAxiosError } from "@xylabs/axios";
30
- import { handleError } from "@xylabs/error";
31
- import { DomainPayloadWrapper } from "@xyo-network/domain-payload-plugin";
32
- import { SchemaSchema } from "@xyo-network/schema-payload-plugin";
33
- import Ajv from "ajv";
34
- import { LRUCache } from "lru-cache";
35
- var getSchemaNameFromSchema = /* @__PURE__ */ __name((schema) => {
36
- if (schema.$id) {
37
- return schema.$id;
38
- }
39
- }, "getSchemaNameFromSchema");
40
- var SchemaCache = class _SchemaCache {
41
- static {
42
- __name(this, "SchemaCache");
43
- }
44
- /**
45
- * Object representing `null` since LRU Cache types
46
- * only allow for types that derive from object
47
- */
48
- static NULL = {
49
- payload: {
50
- definition: {},
51
- schema: SchemaSchema
52
- }
53
- };
54
- static _instance;
55
- onSchemaCached;
56
- proxy;
57
- _cache = new LRUCache({
58
- max: 500,
59
- ttl: 1e3 * 60 * 5
60
- });
61
- _validators = {};
62
- //prevents double discovery
63
- getDebounce = new Debounce();
64
- constructor(proxy) {
65
- this.proxy = proxy;
66
- }
67
- static get instance() {
68
- if (!this._instance) {
69
- this._instance = new _SchemaCache();
70
- }
71
- return this._instance;
72
- }
73
- /**
74
- * A map of cached schema (by name) to payload validators for the schema. A schema
75
- * must be cached via `get('schema.name')` before it's validator can be used as
76
- * they are compiled dynamically at runtime upon retrieval.
77
- */
78
- get validators() {
79
- return this._validators;
80
- }
81
- async get(schema) {
82
- if (schema) {
83
- await this.getDebounce.one(schema, async () => {
84
- if (this._cache.get(schema) === void 0) {
85
- await this.fetchSchema(schema);
86
- }
87
- });
88
- const value = this._cache.get(schema);
89
- return value === _SchemaCache.NULL ? null : value;
90
- }
91
- return void 0;
92
- }
93
- cacheSchemaIfValid(entry) {
94
- if (entry.payload.definition) {
95
- const ajv = new Ajv({
96
- strict: false
97
- });
98
- const validator = ajv.compile(entry.payload.definition);
99
- const schemaName = getSchemaNameFromSchema(entry.payload.definition);
100
- if (schemaName) {
101
- this._cache.set(schemaName, entry);
102
- const key = schemaName;
103
- this._validators[key] = validator;
104
- this.onSchemaCached?.(schemaName, entry);
105
- }
106
- }
107
- }
108
- cacheSchemas(aliasEntries) {
109
- for (const entry of aliasEntries?.filter((entry2) => entry2.payload.schema === SchemaSchema) ?? []) {
110
- this.cacheSchemaIfValid(entry);
111
- }
112
- }
113
- async fetchSchema(schema) {
114
- try {
115
- const domain = await DomainPayloadWrapper.discover(schema, this.proxy);
116
- await domain?.fetch();
117
- this.cacheSchemas(domain?.aliases);
118
- if (this._cache.get(schema) === void 0) {
119
- this._cache.set(schema, _SchemaCache.NULL);
120
- }
121
- } catch (error) {
122
- this._cache.set(schema, _SchemaCache.NULL);
123
- if (isAxiosError(error)) {
124
- console.log(`Axios Url: ${error.response?.config.url}`);
125
- }
126
- handleError(error, (error2) => {
127
- console.error(`fetchSchema threw: ${error2.message}`);
128
- });
129
- }
130
- }
131
- };
132
- export {
133
- Debounce,
134
- SchemaCache
135
- };
1
+ var m=Object.defineProperty;var r=(a,t)=>m(a,"name",{value:t,configurable:!0});import{delay as f}from"@xylabs/delay";var c=class{static{r(this,"Debounce")}map=new Map;async one(t,e,i=1e4){let o=Date.now();for(;this.map.get(t);)if(await f(100),Date.now()-o>i)throw new Error(`Debounce timed out [${t}]`);try{return this.map.set(t,1),await e()}finally{this.map.set(t,0)}}};import{isAxiosError as l}from"@xylabs/axios";import{handleError as d}from"@xylabs/error";import{DomainPayloadWrapper as p}from"@xyo-network/domain-payload-plugin";import{SchemaSchema as s}from"@xyo-network/schema-payload-plugin";import u from"ajv";import{LRUCache as w}from"lru-cache";var g=r(a=>{if(a.$id)return a.$id},"getSchemaNameFromSchema"),n=class a{static{r(this,"SchemaCache")}static NULL={payload:{definition:{},schema:s}};static _instance;onSchemaCached;proxy;_cache=new w({max:500,ttl:1e3*60*5});_validators={};getDebounce=new c;constructor(t){this.proxy=t}static get instance(){return this._instance||(this._instance=new a),this._instance}get validators(){return this._validators}async get(t){if(t){await this.getDebounce.one(t,async()=>{this._cache.get(t)===void 0&&await this.fetchSchema(t)});let e=this._cache.get(t);return e===a.NULL?null:e}}cacheSchemaIfValid(t){if(t.payload.definition){let i=new u({strict:!1}).compile(t.payload.definition),o=g(t.payload.definition);if(o){this._cache.set(o,t);let h=o;this._validators[h]=i,this.onSchemaCached?.(o,t)}}}cacheSchemas(t){for(let e of t?.filter(i=>i.payload.schema===s)??[])this.cacheSchemaIfValid(e)}async fetchSchema(t){try{let e=await p.discover(t,this.proxy);await e?.fetch(),this.cacheSchemas(e?.aliases),this._cache.get(t)===void 0&&this._cache.set(t,a.NULL)}catch(e){this._cache.set(t,a.NULL),l(e)&&console.log(`Axios Url: ${e.response?.config.url}`),d(e,i=>{console.error(`fetchSchema threw: ${i.message}`)})}}};export{c as Debounce,n as SchemaCache};
136
2
  //# sourceMappingURL=index.js.map
@@ -1 +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'\n// eslint-disable-next-line import/no-named-as-default\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 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,SAASA,aAAa;AAEf,IAAMC,WAAN,MAAMA;EAFb,OAEaA;;;EACHC,MAAM,oBAAIC,IAAAA;EAElB,MAAMC,IAAOC,KAAWC,SAA2BC,UAAU,KAAQ;AACnE,UAAMC,YAAYC,KAAKC,IAAG;AAC1B,WAAO,KAAKR,IAAIS,IAAIN,GAAAA,GAAM;AACxB,YAAMO,MAAM,GAAA;AACZ,UAAIH,KAAKC,IAAG,IAAKF,YAAYD,SAAS;AACpC,cAAM,IAAIM,MAAM,uBAAuBR,GAAAA,GAAM;MAC/C;IACF;AACA,QAAI;AACF,WAAKH,IAAIY,IAAIT,KAAK,CAAA;AAClB,aAAO,MAAMC,QAAAA;IACf,UAAA;AACE,WAAKJ,IAAIY,IAAIT,KAAK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IAAII,QAAQL,MAAMrB,QAAQC,UAAU;AACtD,YAAM0B,aAAahC,wBAAwB0B,MAAMrB,QAAQC,UAAU;AACnE,UAAI0B,YAAY;AACd,aAAKrB,OAAOsB,IAAID,YAAYN,KAAAA;AAC5B,cAAMQ,MAAMF;AACZ,aAAKjB,YAAYmB,GAAAA,IAAOJ;AACxB,aAAKrB,iBAAiBuB,YAAYN,KAAAA;MACpC;IACF;EACF;EAEQS,aAAaC,cAAwC;AAC3D,eAAWV,SAASU,cAAcC,OAAO,CAACX,WAAUA,OAAMrB,QAAQJ,WAAWM,YAAAA,KAAiB,CAAA,GAAI;AAChG,WAAKkB,mBAAmBC,KAAAA;IAC1B;EACF;EAEA,MAAcH,YAAYtB,QAAgB;AACxC,QAAI;AACF,YAAMqC,SAAS,MAAMC,qBAAqBC,SAASvC,QAAQ,KAAKS,KAAK;AACrE,YAAM4B,QAAQG,MAAAA;AACd,WAAKN,aAAaG,QAAQI,OAAAA;AAG1B,UAAI,KAAK/B,OAAOS,IAAInB,MAAAA,MAAYqB,QAAW;AACzC,aAAKX,OAAOsB,IAAIhC,QAAQE,aAAYC,IAAI;MAC1C;IACF,SAASuC,OAAO;AAEd,WAAKhC,OAAOsB,IAAIhC,QAAQE,aAAYC,IAAI;AACxC,UAAIwC,aAAaD,KAAAA,GAAQ;AACvBE,gBAAQC,IAAI,cAAcH,MAAMI,UAAUC,OAAOC,GAAAA,EAAK;MACxD;AACAC,kBAAYP,OAAO,CAACA,WAAAA;AAClBE,gBAAQF,MAAM,sBAAsBA,OAAMQ,OAAO,EAAE;MACrD,CAAA;IACF;EACF;AACF;","names":["delay","Debounce","map","Map","one","key","closure","timeout","startTime","Date","now","get","delay","Error","set","isAxiosError","handleError","DomainPayloadWrapper","SchemaSchema","Ajv","LRUCache","getSchemaNameFromSchema","schema","$id","SchemaCache","NULL","payload","definition","SchemaSchema","_instance","onSchemaCached","proxy","_cache","LRUCache","max","ttl","_validators","getDebounce","Debounce","instance","validators","get","one","undefined","fetchSchema","value","cacheSchemaIfValid","entry","ajv","Ajv","strict","validator","compile","schemaName","set","key","cacheSchemas","aliasEntries","filter","domain","DomainPayloadWrapper","discover","fetch","aliases","error","isAxiosError","console","log","response","config","url","handleError","message"]}
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'\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 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":"+EAAA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QA,GAAAA,CAClBE,QAAQF,MAAM,sBAAsBA,EAAMQ,OAAO,EAAE,CACrD,CAAA,CACF,CACF,CACF","names":["delay","Debounce","map","Map","one","key","closure","timeout","startTime","Date","now","get","delay","Error","set","isAxiosError","handleError","DomainPayloadWrapper","SchemaSchema","Ajv","LRUCache","getSchemaNameFromSchema","__name","schema","$id","SchemaCache","NULL","payload","definition","SchemaSchema","_instance","onSchemaCached","proxy","_cache","LRUCache","max","ttl","_validators","getDebounce","Debounce","instance","validators","get","one","undefined","fetchSchema","value","cacheSchemaIfValid","entry","validator","Ajv","strict","compile","schemaName","set","key","cacheSchemas","aliasEntries","filter","domain","DomainPayloadWrapper","discover","fetch","aliases","error","isAxiosError","console","log","response","config","url","handleError","message"]}
@@ -3,10 +3,6 @@ import { SchemaPayload } from '@xyo-network/schema-payload-plugin';
3
3
  import { SchemaNameToValidatorMap } from './SchemaNameToValidatorMap';
4
4
  export type SchemaCacheEntry = FetchedPayload<SchemaPayload>;
5
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
6
  protected static readonly NULL: SchemaCacheEntry;
11
7
  private static _instance?;
12
8
  onSchemaCached?: (name: string, entry: SchemaCacheEntry) => void;
@@ -16,11 +12,6 @@ export declare class SchemaCache<T extends SchemaNameToValidatorMap = SchemaName
16
12
  private getDebounce;
17
13
  private constructor();
18
14
  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
15
  get validators(): T;
25
16
  get(schema?: string): Promise<SchemaCacheEntry | undefined | null>;
26
17
  private cacheSchemaIfValid;
@@ -1 +1 @@
1
- {"version":3,"file":"SchemaCache.d.ts","sourceRoot":"","sources":["../../src/SchemaCache.ts"],"names":[],"mappings":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AC,CAAC,SAAS,wBAAwB,GAAG,wBAAwB;IAKpF,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;IAOD,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;YAMN,WAAW;CAqB1B"}
@@ -3,10 +3,6 @@ import { SchemaPayload } from '@xyo-network/schema-payload-plugin';
3
3
  import { SchemaNameToValidatorMap } from './SchemaNameToValidatorMap';
4
4
  export type SchemaCacheEntry = FetchedPayload<SchemaPayload>;
5
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
6
  protected static readonly NULL: SchemaCacheEntry;
11
7
  private static _instance?;
12
8
  onSchemaCached?: (name: string, entry: SchemaCacheEntry) => void;
@@ -16,11 +12,6 @@ export declare class SchemaCache<T extends SchemaNameToValidatorMap = SchemaName
16
12
  private getDebounce;
17
13
  private constructor();
18
14
  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
15
  get validators(): T;
25
16
  get(schema?: string): Promise<SchemaCacheEntry | undefined | null>;
26
17
  private cacheSchemaIfValid;