@xyo-network/schema-cache 2.72.9 → 2.73.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.
- package/dist/docs.json +133 -133
- package/dist/index.d.mts +52 -0
- package/dist/index.d.ts +52 -0
- package/dist/index.js +155 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +117 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +35 -22
- package/tsup.config.ts +16 -0
- package/dist/cjs/Debounce.js +0 -30
- package/dist/cjs/Debounce.js.map +0 -1
- package/dist/cjs/SchemaCache.js +0 -106
- package/dist/cjs/SchemaCache.js.map +0 -1
- package/dist/cjs/SchemaNameToValidatorMap.js +0 -6
- package/dist/cjs/SchemaNameToValidatorMap.js.map +0 -1
- package/dist/cjs/index.js +0 -7
- package/dist/cjs/index.js.map +0 -1
- package/dist/esm/Debounce.js +0 -21
- package/dist/esm/Debounce.js.map +0 -1
- package/dist/esm/SchemaCache.js +0 -100
- package/dist/esm/SchemaCache.js.map +0 -1
- package/dist/esm/SchemaNameToValidatorMap.js +0 -4
- package/dist/esm/SchemaNameToValidatorMap.js.map +0 -1
- package/dist/esm/index.js +0 -4
- package/dist/esm/index.js.map +0 -1
- package/dist/types/Debounce.d.ts +0 -5
- package/dist/types/Debounce.d.ts.map +0 -1
- package/dist/types/SchemaCache.d.ts +0 -30
- package/dist/types/SchemaCache.d.ts.map +0 -1
- package/dist/types/SchemaNameToValidatorMap.d.ts +0 -17
- package/dist/types/SchemaNameToValidatorMap.d.ts.map +0 -1
- package/dist/types/index.d.ts +0 -4
- package/dist/types/index.d.ts.map +0 -1
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { FetchedPayload } from '@xyo-network/huri';
|
|
2
|
+
import { SchemaSchema, SchemaPayload } from '@xyo-network/schema-payload-plugin';
|
|
3
|
+
import { DomainSchema, DomainPayload } from '@xyo-network/domain-payload-plugin';
|
|
4
|
+
import { Payload, PayloadSchema } from '@xyo-network/payload-model';
|
|
5
|
+
|
|
6
|
+
declare class Debounce<TKey = string> {
|
|
7
|
+
private map;
|
|
8
|
+
one<T>(key: TKey, closure: () => Promise<T>, timeout?: number): Promise<T>;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Used in conjunction with schema validation to support compile time type assertion
|
|
13
|
+
* for known schema types.
|
|
14
|
+
*/
|
|
15
|
+
type NarrowPayload<T extends Payload = Payload> = ((x: Payload) => x is T) | undefined;
|
|
16
|
+
/**
|
|
17
|
+
* Used to map known schemas (byt their string name) to the validators which assert their types
|
|
18
|
+
*/
|
|
19
|
+
interface SchemaNameToValidatorMap {
|
|
20
|
+
[DomainSchema]: NarrowPayload<DomainPayload>;
|
|
21
|
+
[PayloadSchema]: NarrowPayload<Payload>;
|
|
22
|
+
[SchemaSchema]: NarrowPayload<SchemaPayload>;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
type SchemaCacheEntry = FetchedPayload<SchemaPayload>;
|
|
26
|
+
declare class SchemaCache<T extends SchemaNameToValidatorMap = SchemaNameToValidatorMap> {
|
|
27
|
+
/**
|
|
28
|
+
* Object representing `null` since LRU Cache types
|
|
29
|
+
* only allow for types that derive from object
|
|
30
|
+
*/
|
|
31
|
+
protected static readonly NULL: SchemaCacheEntry;
|
|
32
|
+
private static _instance?;
|
|
33
|
+
onSchemaCached?: (name: string, entry: SchemaCacheEntry) => void;
|
|
34
|
+
proxy?: string;
|
|
35
|
+
private _cache;
|
|
36
|
+
private _validators;
|
|
37
|
+
private getDebounce;
|
|
38
|
+
private constructor();
|
|
39
|
+
static get instance(): SchemaCache<SchemaNameToValidatorMap>;
|
|
40
|
+
/**
|
|
41
|
+
* A map of cached schema (by name) to payload validators for the schema. A schema
|
|
42
|
+
* must be cached via `get('schema.name')` before it's validator can be used as
|
|
43
|
+
* they are compiled dynamically at runtime upon retrieval.
|
|
44
|
+
*/
|
|
45
|
+
get validators(): T;
|
|
46
|
+
get(schema?: string): Promise<SchemaCacheEntry | undefined | null>;
|
|
47
|
+
private cacheSchemaIfValid;
|
|
48
|
+
private cacheSchemas;
|
|
49
|
+
private fetchSchema;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export { Debounce, NarrowPayload, SchemaCache, SchemaCacheEntry, SchemaNameToValidatorMap };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { FetchedPayload } from '@xyo-network/huri';
|
|
2
|
+
import { SchemaSchema, SchemaPayload } from '@xyo-network/schema-payload-plugin';
|
|
3
|
+
import { DomainSchema, DomainPayload } from '@xyo-network/domain-payload-plugin';
|
|
4
|
+
import { Payload, PayloadSchema } from '@xyo-network/payload-model';
|
|
5
|
+
|
|
6
|
+
declare class Debounce<TKey = string> {
|
|
7
|
+
private map;
|
|
8
|
+
one<T>(key: TKey, closure: () => Promise<T>, timeout?: number): Promise<T>;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Used in conjunction with schema validation to support compile time type assertion
|
|
13
|
+
* for known schema types.
|
|
14
|
+
*/
|
|
15
|
+
type NarrowPayload<T extends Payload = Payload> = ((x: Payload) => x is T) | undefined;
|
|
16
|
+
/**
|
|
17
|
+
* Used to map known schemas (byt their string name) to the validators which assert their types
|
|
18
|
+
*/
|
|
19
|
+
interface SchemaNameToValidatorMap {
|
|
20
|
+
[DomainSchema]: NarrowPayload<DomainPayload>;
|
|
21
|
+
[PayloadSchema]: NarrowPayload<Payload>;
|
|
22
|
+
[SchemaSchema]: NarrowPayload<SchemaPayload>;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
type SchemaCacheEntry = FetchedPayload<SchemaPayload>;
|
|
26
|
+
declare class SchemaCache<T extends SchemaNameToValidatorMap = SchemaNameToValidatorMap> {
|
|
27
|
+
/**
|
|
28
|
+
* Object representing `null` since LRU Cache types
|
|
29
|
+
* only allow for types that derive from object
|
|
30
|
+
*/
|
|
31
|
+
protected static readonly NULL: SchemaCacheEntry;
|
|
32
|
+
private static _instance?;
|
|
33
|
+
onSchemaCached?: (name: string, entry: SchemaCacheEntry) => void;
|
|
34
|
+
proxy?: string;
|
|
35
|
+
private _cache;
|
|
36
|
+
private _validators;
|
|
37
|
+
private getDebounce;
|
|
38
|
+
private constructor();
|
|
39
|
+
static get instance(): SchemaCache<SchemaNameToValidatorMap>;
|
|
40
|
+
/**
|
|
41
|
+
* A map of cached schema (by name) to payload validators for the schema. A schema
|
|
42
|
+
* must be cached via `get('schema.name')` before it's validator can be used as
|
|
43
|
+
* they are compiled dynamically at runtime upon retrieval.
|
|
44
|
+
*/
|
|
45
|
+
get validators(): T;
|
|
46
|
+
get(schema?: string): Promise<SchemaCacheEntry | undefined | null>;
|
|
47
|
+
private cacheSchemaIfValid;
|
|
48
|
+
private cacheSchemas;
|
|
49
|
+
private fetchSchema;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export { Debounce, NarrowPayload, SchemaCache, SchemaCacheEntry, SchemaNameToValidatorMap };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
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 __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var src_exports = {};
|
|
32
|
+
__export(src_exports, {
|
|
33
|
+
Debounce: () => Debounce,
|
|
34
|
+
SchemaCache: () => SchemaCache
|
|
35
|
+
});
|
|
36
|
+
module.exports = __toCommonJS(src_exports);
|
|
37
|
+
|
|
38
|
+
// src/Debounce.ts
|
|
39
|
+
var import_delay = require("@xylabs/delay");
|
|
40
|
+
var Debounce = class {
|
|
41
|
+
map = /* @__PURE__ */ new Map();
|
|
42
|
+
async one(key, closure, timeout = 1e4) {
|
|
43
|
+
const startTime = Date.now();
|
|
44
|
+
while (this.map.get(key)) {
|
|
45
|
+
await (0, import_delay.delay)(100);
|
|
46
|
+
if (Date.now() - startTime > timeout) {
|
|
47
|
+
throw Error(`Debounce timed out [${key}]`);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
try {
|
|
51
|
+
this.map.set(key, 1);
|
|
52
|
+
return await closure();
|
|
53
|
+
} finally {
|
|
54
|
+
this.map.set(key, 0);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
// src/SchemaCache.ts
|
|
60
|
+
var import_axios = require("@xyo-network/axios");
|
|
61
|
+
var import_domain_payload_plugin = require("@xyo-network/domain-payload-plugin");
|
|
62
|
+
var import_error = require("@xyo-network/error");
|
|
63
|
+
var import_schema_payload_plugin = require("@xyo-network/schema-payload-plugin");
|
|
64
|
+
var import_ajv = __toESM(require("ajv"));
|
|
65
|
+
var import_lru_cache = require("lru-cache");
|
|
66
|
+
var getSchemaNameFromSchema = (schema) => {
|
|
67
|
+
if (schema.$id) {
|
|
68
|
+
return schema.$id;
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
var SchemaCache = class _SchemaCache {
|
|
72
|
+
/**
|
|
73
|
+
* Object representing `null` since LRU Cache types
|
|
74
|
+
* only allow for types that derive from object
|
|
75
|
+
*/
|
|
76
|
+
static NULL = { payload: { definition: {}, schema: import_schema_payload_plugin.SchemaSchema } };
|
|
77
|
+
static _instance;
|
|
78
|
+
onSchemaCached;
|
|
79
|
+
proxy;
|
|
80
|
+
_cache = new import_lru_cache.LRUCache({ max: 500, ttl: 1e3 * 60 * 5 });
|
|
81
|
+
_validators = {};
|
|
82
|
+
//prevents double discovery
|
|
83
|
+
getDebounce = new Debounce();
|
|
84
|
+
constructor(proxy) {
|
|
85
|
+
this.proxy = proxy;
|
|
86
|
+
}
|
|
87
|
+
static get instance() {
|
|
88
|
+
if (!this._instance) {
|
|
89
|
+
this._instance = new _SchemaCache();
|
|
90
|
+
}
|
|
91
|
+
return this._instance;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* A map of cached schema (by name) to payload validators for the schema. A schema
|
|
95
|
+
* must be cached via `get('schema.name')` before it's validator can be used as
|
|
96
|
+
* they are compiled dynamically at runtime upon retrieval.
|
|
97
|
+
*/
|
|
98
|
+
get validators() {
|
|
99
|
+
return this._validators;
|
|
100
|
+
}
|
|
101
|
+
async get(schema) {
|
|
102
|
+
if (schema) {
|
|
103
|
+
await this.getDebounce.one(schema, async () => {
|
|
104
|
+
if (this._cache.get(schema) === void 0) {
|
|
105
|
+
await this.fetchSchema(schema);
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
const value = this._cache.get(schema);
|
|
109
|
+
return value === _SchemaCache.NULL ? null : value;
|
|
110
|
+
}
|
|
111
|
+
return void 0;
|
|
112
|
+
}
|
|
113
|
+
cacheSchemaIfValid(entry) {
|
|
114
|
+
if (entry.payload.definition) {
|
|
115
|
+
const ajv = new import_ajv.default({ strict: false });
|
|
116
|
+
const validator = ajv.compile(entry.payload.definition);
|
|
117
|
+
const schemaName = getSchemaNameFromSchema(entry.payload.definition);
|
|
118
|
+
if (schemaName) {
|
|
119
|
+
this._cache.set(schemaName, entry);
|
|
120
|
+
const key = schemaName;
|
|
121
|
+
this._validators[key] = validator;
|
|
122
|
+
this.onSchemaCached?.(schemaName, entry);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
cacheSchemas(aliasEntries) {
|
|
127
|
+
aliasEntries?.filter((entry) => entry.payload.schema === import_schema_payload_plugin.SchemaSchema).forEach((entry) => {
|
|
128
|
+
this.cacheSchemaIfValid(entry);
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
async fetchSchema(schema) {
|
|
132
|
+
try {
|
|
133
|
+
const domain = await import_domain_payload_plugin.DomainPayloadWrapper.discover(schema, this.proxy);
|
|
134
|
+
await domain?.fetch();
|
|
135
|
+
this.cacheSchemas(domain?.aliases);
|
|
136
|
+
if (this._cache.get(schema) === void 0) {
|
|
137
|
+
this._cache.set(schema, _SchemaCache.NULL);
|
|
138
|
+
}
|
|
139
|
+
} catch (ex) {
|
|
140
|
+
this._cache.set(schema, _SchemaCache.NULL);
|
|
141
|
+
if ((0, import_axios.isAxiosError)(ex)) {
|
|
142
|
+
console.log(`Axios Url: ${ex.response?.config.url}`);
|
|
143
|
+
}
|
|
144
|
+
(0, import_error.handleError)(ex, (error) => {
|
|
145
|
+
console.error(`fetchSchema threw: ${error.message}`);
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
151
|
+
0 && (module.exports = {
|
|
152
|
+
Debounce,
|
|
153
|
+
SchemaCache
|
|
154
|
+
});
|
|
155
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +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 = 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","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;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,mBAAsB;AAEf,IAAM,WAAN,MAA8B;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,gBAAM,oBAAM,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;;;ACpBA,mBAA6B;AAC7B,mCAAqC;AACrC,mBAA4B;AAE5B,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,EAChF,cAAiB,CAAC;AAAA;AAAA,EAGlB,cAAc,IAAI,SAAS;AAAA,EAE3B,YAAY,OAAgB;AAClC,SAAK,QAAQ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,QAAQ,OAAO;AAGjC,UAAI,KAAK,OAAO,IAAI,MAAM,MAAM,QAAW;AACzC,aAAK,OAAO,IAAI,QAAQ,aAAY,IAAI;AAAA,MAC1C;AAAA,IACF,SAAS,IAAI;AAEX,WAAK,OAAO,IAAI,QAAQ,aAAY,IAAI;AACxC,cAAI,2BAAa,EAAE,GAAG;AACpB,gBAAQ,IAAI,cAAc,GAAG,UAAU,OAAO,GAAG,EAAE;AAAA,MACrD;AACA,oCAAY,IAAI,CAAC,UAAU;AACzB,gBAAQ,MAAM,sBAAsB,MAAM,OAAO,EAAE;AAAA,MACrD,CAAC;AAAA,IACH;AAAA,EACF;AACF;","names":["Ajv"]}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
// src/Debounce.ts
|
|
2
|
+
import { delay } from "@xylabs/delay";
|
|
3
|
+
var Debounce = class {
|
|
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
|
+
};
|
|
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 = 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","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,aAAa;AAEf,IAAM,WAAN,MAA8B;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,MAC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racleNetwork/sdk-xyo-client-js/issues"
|
|
10
10
|
},
|
|
11
11
|
"dependencies": {
|
|
12
|
-
"@xylabs/delay": "^2.
|
|
13
|
-
"@xyo-network/axios": "~2.
|
|
14
|
-
"@xyo-network/domain-payload-plugin": "~2.
|
|
15
|
-
"@xyo-network/error": "~2.
|
|
16
|
-
"@xyo-network/huri": "~2.
|
|
17
|
-
"@xyo-network/payload-model": "~2.
|
|
18
|
-
"@xyo-network/schema-payload-plugin": "~2.
|
|
12
|
+
"@xylabs/delay": "^2.10.7",
|
|
13
|
+
"@xyo-network/axios": "~2.73.1",
|
|
14
|
+
"@xyo-network/domain-payload-plugin": "~2.73.1",
|
|
15
|
+
"@xyo-network/error": "~2.73.1",
|
|
16
|
+
"@xyo-network/huri": "~2.73.1",
|
|
17
|
+
"@xyo-network/payload-model": "~2.73.1",
|
|
18
|
+
"@xyo-network/schema-payload-plugin": "~2.73.1",
|
|
19
19
|
"ajv": "^8.12.0",
|
|
20
20
|
"lru-cache": "^10.0.1"
|
|
21
21
|
},
|
|
22
22
|
"description": "Primary SDK for using XYO Protocol 2.0",
|
|
23
23
|
"devDependencies": {
|
|
24
|
-
"@xylabs/ts-scripts-yarn3": "^2.19.
|
|
25
|
-
"@xylabs/tsconfig-dom": "^2.19.
|
|
24
|
+
"@xylabs/ts-scripts-yarn3": "^2.19.5",
|
|
25
|
+
"@xylabs/tsconfig-dom": "^2.19.5",
|
|
26
|
+
"publint": "^0.2.2",
|
|
27
|
+
"tsup": "^7.2.0",
|
|
26
28
|
"typescript": "^5.2.2"
|
|
27
29
|
},
|
|
28
|
-
"browser": "dist/esm/index.js",
|
|
29
30
|
"docs": "dist/docs.json",
|
|
30
31
|
"exports": {
|
|
31
32
|
".": {
|
|
32
|
-
"
|
|
33
|
-
"
|
|
34
|
-
"
|
|
33
|
+
"require": {
|
|
34
|
+
"types": "./dist/index.d.ts",
|
|
35
|
+
"default": "./dist/index.js"
|
|
35
36
|
},
|
|
36
|
-
"
|
|
37
|
-
"
|
|
38
|
-
"
|
|
39
|
-
}
|
|
40
|
-
"default": "./dist/esm/index.js"
|
|
37
|
+
"import": {
|
|
38
|
+
"types": "./dist/index.d.mts",
|
|
39
|
+
"default": "./dist/index.mjs"
|
|
40
|
+
}
|
|
41
41
|
},
|
|
42
42
|
"./dist/docs.json": {
|
|
43
43
|
"default": "./dist/docs.json"
|
|
44
44
|
},
|
|
45
|
+
"./cjs": {
|
|
46
|
+
"default": "./dist/index.js"
|
|
47
|
+
},
|
|
48
|
+
"./docs": {
|
|
49
|
+
"default": "./dist/docs.json"
|
|
50
|
+
},
|
|
51
|
+
"./esm": {
|
|
52
|
+
"default": "./dist/index.mjs"
|
|
53
|
+
},
|
|
45
54
|
"./package.json": "./package.json"
|
|
46
55
|
},
|
|
47
|
-
"main": "dist/
|
|
48
|
-
"module": "dist/
|
|
56
|
+
"main": "dist/index.js",
|
|
57
|
+
"module": "dist/index.mjs",
|
|
49
58
|
"homepage": "https://xyo.network",
|
|
50
59
|
"license": "LGPL-3.0",
|
|
51
60
|
"name": "@xyo-network/schema-cache",
|
|
@@ -56,7 +65,11 @@
|
|
|
56
65
|
"type": "git",
|
|
57
66
|
"url": "https://github.com/XYOracleNetwork/sdk-xyo-client-js.git"
|
|
58
67
|
},
|
|
68
|
+
"scripts": {
|
|
69
|
+
"package-compile": "tsup && publint",
|
|
70
|
+
"package-recompile": "tsup && publint"
|
|
71
|
+
},
|
|
59
72
|
"sideEffects": false,
|
|
60
|
-
"types": "dist/
|
|
61
|
-
"version": "2.
|
|
73
|
+
"types": "dist/index.d.ts",
|
|
74
|
+
"version": "2.73.1"
|
|
62
75
|
}
|
package/tsup.config.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { defineConfig } from 'tsup'
|
|
2
|
+
|
|
3
|
+
// eslint-disable-next-line import/no-default-export
|
|
4
|
+
export default defineConfig({
|
|
5
|
+
bundle: true,
|
|
6
|
+
cjsInterop: true,
|
|
7
|
+
clean: true,
|
|
8
|
+
dts: {
|
|
9
|
+
entry: ['src/index.ts'],
|
|
10
|
+
},
|
|
11
|
+
entry: ['src/index.ts'],
|
|
12
|
+
format: ['cjs', 'esm'],
|
|
13
|
+
sourcemap: true,
|
|
14
|
+
splitting: false,
|
|
15
|
+
tsconfig: 'tsconfig.json',
|
|
16
|
+
})
|
package/dist/cjs/Debounce.js
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Debounce = void 0;
|
|
4
|
-
const tslib_1 = require("tslib");
|
|
5
|
-
const delay_1 = require("@xylabs/delay");
|
|
6
|
-
class Debounce {
|
|
7
|
-
constructor() {
|
|
8
|
-
this.map = new Map();
|
|
9
|
-
}
|
|
10
|
-
one(key, closure, timeout = 10000) {
|
|
11
|
-
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
12
|
-
const startTime = Date.now();
|
|
13
|
-
while (this.map.get(key)) {
|
|
14
|
-
yield (0, delay_1.delay)(100);
|
|
15
|
-
if (Date.now() - startTime > timeout) {
|
|
16
|
-
throw Error(`Debounce timed out [${key}]`);
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
try {
|
|
20
|
-
this.map.set(key, 1);
|
|
21
|
-
return yield closure();
|
|
22
|
-
}
|
|
23
|
-
finally {
|
|
24
|
-
this.map.set(key, 0);
|
|
25
|
-
}
|
|
26
|
-
});
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
exports.Debounce = Debounce;
|
|
30
|
-
//# sourceMappingURL=Debounce.js.map
|
package/dist/cjs/Debounce.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"Debounce.js","sourceRoot":"","sources":["../../src/Debounce.ts"],"names":[],"mappings":";;;;AAAA,yCAAqC;AAErC,MAAa,QAAQ;IAArB;QACU,QAAG,GAAG,IAAI,GAAG,EAAgB,CAAA;IAiBvC,CAAC;IAfO,GAAG,CAAI,GAAS,EAAE,OAAyB,EAAE,OAAO,GAAG,KAAK;;YAChE,MAAM,SAAS,GAAG,IAAI,CAAC,GAAG,EAAE,CAAA;YAC5B,OAAO,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE;gBACxB,MAAM,IAAA,aAAK,EAAC,GAAG,CAAC,CAAA;gBAChB,IAAI,IAAI,CAAC,GAAG,EAAE,GAAG,SAAS,GAAG,OAAO,EAAE;oBACpC,MAAM,KAAK,CAAC,uBAAuB,GAAG,GAAG,CAAC,CAAA;iBAC3C;aACF;YACD,IAAI;gBACF,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,EAAE,CAAC,CAAC,CAAA;gBACpB,OAAO,MAAM,OAAO,EAAE,CAAA;aACvB;oBAAS;gBACR,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,EAAE,CAAC,CAAC,CAAA;aACrB;QACH,CAAC;KAAA;CACF;AAlBD,4BAkBC"}
|
package/dist/cjs/SchemaCache.js
DELETED
|
@@ -1,106 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.SchemaCache = void 0;
|
|
4
|
-
const tslib_1 = require("tslib");
|
|
5
|
-
const axios_1 = require("@xyo-network/axios");
|
|
6
|
-
const domain_payload_plugin_1 = require("@xyo-network/domain-payload-plugin");
|
|
7
|
-
const error_1 = require("@xyo-network/error");
|
|
8
|
-
const schema_payload_plugin_1 = require("@xyo-network/schema-payload-plugin");
|
|
9
|
-
const ajv_1 = tslib_1.__importDefault(require("ajv"));
|
|
10
|
-
const lru_cache_1 = require("lru-cache");
|
|
11
|
-
const Debounce_1 = require("./Debounce");
|
|
12
|
-
const getSchemaNameFromSchema = (schema) => {
|
|
13
|
-
if (schema.$id) {
|
|
14
|
-
return schema.$id;
|
|
15
|
-
}
|
|
16
|
-
};
|
|
17
|
-
class SchemaCache {
|
|
18
|
-
constructor(proxy) {
|
|
19
|
-
this._cache = new lru_cache_1.LRUCache({ max: 500, ttl: 1000 * 60 * 5 });
|
|
20
|
-
this._validators = {};
|
|
21
|
-
//prevents double discovery
|
|
22
|
-
this.getDebounce = new Debounce_1.Debounce();
|
|
23
|
-
this.proxy = proxy;
|
|
24
|
-
}
|
|
25
|
-
static get instance() {
|
|
26
|
-
if (!this._instance) {
|
|
27
|
-
this._instance = new SchemaCache();
|
|
28
|
-
}
|
|
29
|
-
return this._instance;
|
|
30
|
-
}
|
|
31
|
-
/**
|
|
32
|
-
* A map of cached schema (by name) to payload validators for the schema. A schema
|
|
33
|
-
* must be cached via `get('schema.name')` before it's validator can be used as
|
|
34
|
-
* they are compiled dynamically at runtime upon retrieval.
|
|
35
|
-
*/
|
|
36
|
-
get validators() {
|
|
37
|
-
return this._validators;
|
|
38
|
-
}
|
|
39
|
-
get(schema) {
|
|
40
|
-
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
41
|
-
if (schema) {
|
|
42
|
-
yield this.getDebounce.one(schema, () => tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
43
|
-
// If we've never looked for it before, it will be undefined
|
|
44
|
-
if (this._cache.get(schema) === undefined) {
|
|
45
|
-
yield this.fetchSchema(schema);
|
|
46
|
-
}
|
|
47
|
-
}));
|
|
48
|
-
const value = this._cache.get(schema);
|
|
49
|
-
return value === SchemaCache.NULL ? null : value;
|
|
50
|
-
}
|
|
51
|
-
return undefined;
|
|
52
|
-
});
|
|
53
|
-
}
|
|
54
|
-
cacheSchemaIfValid(entry) {
|
|
55
|
-
var _a;
|
|
56
|
-
//only store them if they match the schema root
|
|
57
|
-
if (entry.payload.definition) {
|
|
58
|
-
const ajv = new ajv_1.default({ strict: false });
|
|
59
|
-
//check if it is a valid schema def
|
|
60
|
-
const validator = ajv.compile(entry.payload.definition);
|
|
61
|
-
const schemaName = getSchemaNameFromSchema(entry.payload.definition);
|
|
62
|
-
if (schemaName) {
|
|
63
|
-
this._cache.set(schemaName, entry);
|
|
64
|
-
const key = schemaName;
|
|
65
|
-
this._validators[key] = validator;
|
|
66
|
-
(_a = this.onSchemaCached) === null || _a === void 0 ? void 0 : _a.call(this, schemaName, entry);
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
cacheSchemas(aliasEntries) {
|
|
71
|
-
aliasEntries === null || aliasEntries === void 0 ? void 0 : aliasEntries.filter((entry) => entry.payload.schema === schema_payload_plugin_1.SchemaSchema).forEach((entry) => {
|
|
72
|
-
this.cacheSchemaIfValid(entry);
|
|
73
|
-
});
|
|
74
|
-
}
|
|
75
|
-
fetchSchema(schema) {
|
|
76
|
-
var _a;
|
|
77
|
-
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
78
|
-
try {
|
|
79
|
-
const domain = yield domain_payload_plugin_1.DomainPayloadWrapper.discover(schema, this.proxy);
|
|
80
|
-
yield (domain === null || domain === void 0 ? void 0 : domain.fetch());
|
|
81
|
-
this.cacheSchemas(domain === null || domain === void 0 ? void 0 : domain.aliases);
|
|
82
|
-
//if it is still undefined, mark it as null (not found)
|
|
83
|
-
if (this._cache.get(schema) === undefined) {
|
|
84
|
-
this._cache.set(schema, SchemaCache.NULL);
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
catch (ex) {
|
|
88
|
-
//if failed, set it to NULL, TODO: Make an entry for an error to try again in the future?
|
|
89
|
-
this._cache.set(schema, SchemaCache.NULL);
|
|
90
|
-
if ((0, axios_1.isAxiosError)(ex)) {
|
|
91
|
-
console.log(`Axios Url: ${(_a = ex.response) === null || _a === void 0 ? void 0 : _a.config.url}`);
|
|
92
|
-
}
|
|
93
|
-
(0, error_1.handleError)(ex, (error) => {
|
|
94
|
-
console.error(`fetchSchema threw: ${error.message}`);
|
|
95
|
-
});
|
|
96
|
-
}
|
|
97
|
-
});
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
exports.SchemaCache = SchemaCache;
|
|
101
|
-
/**
|
|
102
|
-
* Object representing `null` since LRU Cache types
|
|
103
|
-
* only allow for types that derive from object
|
|
104
|
-
*/
|
|
105
|
-
SchemaCache.NULL = { payload: { definition: {}, schema: schema_payload_plugin_1.SchemaSchema } };
|
|
106
|
-
//# sourceMappingURL=SchemaCache.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"SchemaCache.js","sourceRoot":"","sources":["../../src/SchemaCache.ts"],"names":[],"mappings":";;;;AAAA,8CAAiD;AACjD,8EAAyE;AACzE,8CAAgD;AAEhD,8EAAgF;AAChF,sDAAuC;AACvC,yCAAoC;AAEpC,yCAAqC;AAGrC,MAAM,uBAAuB,GAAG,CAAC,MAAoB,EAAE,EAAE;IACvD,IAAI,MAAM,CAAC,GAAG,EAAE;QACd,OAAO,MAAM,CAAC,GAAG,CAAA;KAClB;AACH,CAAC,CAAA;AAID,MAAa,WAAW;IAkBtB,YAAoB,KAAc;QAN1B,WAAM,GAAG,IAAI,oBAAQ,CAA2B,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,IAAI,GAAG,EAAE,GAAG,CAAC,EAAE,CAAC,CAAA;QACjF,gBAAW,GAAM,EAAO,CAAA;QAEhC,2BAA2B;QACnB,gBAAW,GAAG,IAAI,mBAAQ,EAAE,CAAA;QAGlC,IAAI,CAAC,KAAK,GAAG,KAAK,CAAA;IACpB,CAAC;IAED,MAAM,KAAK,QAAQ;QACjB,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE;YACnB,IAAI,CAAC,SAAS,GAAG,IAAI,WAAW,EAAE,CAAA;SACnC;QACD,OAAO,IAAI,CAAC,SAAS,CAAA;IACvB,CAAC;IAED;;;;OAIG;IACH,IAAI,UAAU;QACZ,OAAO,IAAI,CAAC,WAAW,CAAA;IACzB,CAAC;IAEK,GAAG,CAAC,MAAe;;YACvB,IAAI,MAAM,EAAE;gBACV,MAAM,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,MAAM,EAAE,GAAS,EAAE;oBAC5C,4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