@xyo-network/schema-cache 2.110.9 → 2.110.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/browser/index.cjs +12 -29
- package/dist/browser/index.cjs.map +1 -1
- package/dist/browser/index.js +12 -31
- package/dist/browser/index.js.map +1 -1
- package/dist/neutral/index.cjs +12 -29
- package/dist/neutral/index.cjs.map +1 -1
- package/dist/neutral/index.js +12 -31
- package/dist/neutral/index.js.map +1 -1
- package/dist/node/index.cjs +16 -33
- package/dist/node/index.cjs.map +1 -1
- package/dist/node/index.js +16 -35
- package/dist/node/index.js.map +1 -1
- package/package.json +13 -13
package/dist/browser/index.cjs
CHANGED
|
@@ -3,7 +3,6 @@ var __defProp = Object.defineProperty;
|
|
|
3
3
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
5
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
7
6
|
var __export = (target, all) => {
|
|
8
7
|
for (var name in all)
|
|
9
8
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
@@ -29,9 +28,6 @@ module.exports = __toCommonJS(src_exports);
|
|
|
29
28
|
// src/Debounce.ts
|
|
30
29
|
var import_delay = require("@xylabs/delay");
|
|
31
30
|
var Debounce = class {
|
|
32
|
-
static {
|
|
33
|
-
__name(this, "Debounce");
|
|
34
|
-
}
|
|
35
31
|
map = /* @__PURE__ */ new Map();
|
|
36
32
|
async one(key, closure, timeout = 1e4) {
|
|
37
33
|
const startTime = Date.now();
|
|
@@ -57,32 +53,21 @@ var import_domain_payload_plugin = require("@xyo-network/domain-payload-plugin")
|
|
|
57
53
|
var import_schema_payload_plugin = require("@xyo-network/schema-payload-plugin");
|
|
58
54
|
var import_ajv = require("ajv");
|
|
59
55
|
var import_lru_cache = require("lru-cache");
|
|
60
|
-
var getSchemaNameFromSchema =
|
|
56
|
+
var getSchemaNameFromSchema = (schema) => {
|
|
61
57
|
if (schema.$id) {
|
|
62
58
|
return schema.$id;
|
|
63
59
|
}
|
|
64
|
-
}
|
|
60
|
+
};
|
|
65
61
|
var SchemaCache = class _SchemaCache {
|
|
66
|
-
static {
|
|
67
|
-
__name(this, "SchemaCache");
|
|
68
|
-
}
|
|
69
62
|
/**
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
static NULL = {
|
|
74
|
-
payload: {
|
|
75
|
-
definition: {},
|
|
76
|
-
schema: import_schema_payload_plugin.SchemaSchema
|
|
77
|
-
}
|
|
78
|
-
};
|
|
63
|
+
* Object representing `null` since LRU Cache types
|
|
64
|
+
* only allow for types that derive from object
|
|
65
|
+
*/
|
|
66
|
+
static NULL = { payload: { definition: {}, schema: import_schema_payload_plugin.SchemaSchema } };
|
|
79
67
|
static _instance;
|
|
80
68
|
onSchemaCached;
|
|
81
69
|
proxy;
|
|
82
|
-
_cache = new import_lru_cache.LRUCache({
|
|
83
|
-
max: 500,
|
|
84
|
-
ttl: 1e3 * 60 * 5
|
|
85
|
-
});
|
|
70
|
+
_cache = new import_lru_cache.LRUCache({ max: 500, ttl: 1e3 * 60 * 5 });
|
|
86
71
|
_validators = {};
|
|
87
72
|
//prevents double discovery
|
|
88
73
|
getDebounce = new Debounce();
|
|
@@ -96,10 +81,10 @@ var SchemaCache = class _SchemaCache {
|
|
|
96
81
|
return this._instance;
|
|
97
82
|
}
|
|
98
83
|
/**
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
84
|
+
* A map of cached schema (by name) to payload validators for the schema. A schema
|
|
85
|
+
* must be cached via `get('schema.name')` before it's validator can be used as
|
|
86
|
+
* they are compiled dynamically at runtime upon retrieval.
|
|
87
|
+
*/
|
|
103
88
|
get validators() {
|
|
104
89
|
return this._validators;
|
|
105
90
|
}
|
|
@@ -117,9 +102,7 @@ var SchemaCache = class _SchemaCache {
|
|
|
117
102
|
}
|
|
118
103
|
cacheSchemaIfValid(entry) {
|
|
119
104
|
if (entry.payload.definition) {
|
|
120
|
-
const ajv = new import_ajv.Ajv({
|
|
121
|
-
strict: false
|
|
122
|
-
});
|
|
105
|
+
const ajv = new import_ajv.Ajv({ strict: false });
|
|
123
106
|
const validator = ajv.compile(entry.payload.definition);
|
|
124
107
|
const schemaName = getSchemaNameFromSchema(entry.payload.definition);
|
|
125
108
|
if (schemaName) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/index.ts","../../src/Debounce.ts","../../src/SchemaCache.ts"],"sourcesContent":["export * from './Debounce.js'\nexport * from './SchemaCache.js'\nexport * from './SchemaNameToValidatorMap.js'\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.js'\nimport { SchemaNameToValidatorMap } from './SchemaNameToValidatorMap.js'\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":"
|
|
1
|
+
{"version":3,"sources":["../../src/index.ts","../../src/Debounce.ts","../../src/SchemaCache.ts"],"sourcesContent":["export * from './Debounce.js'\nexport * from './SchemaCache.js'\nexport * from './SchemaNameToValidatorMap.js'\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.js'\nimport { SchemaNameToValidatorMap } from './SchemaNameToValidatorMap.js'\n\nconst getSchemaNameFromSchema = (schema: SchemaObject) => {\n if (schema.$id) {\n return schema.$id\n }\n}\n\nexport type SchemaCacheEntry = FetchedPayload<SchemaPayload>\n\nexport class SchemaCache<T extends SchemaNameToValidatorMap = SchemaNameToValidatorMap> {\n /**\n * Object representing `null` since LRU Cache types\n * only allow for types that derive from object\n */\n protected static readonly NULL: SchemaCacheEntry = { payload: { definition: {}, schema: SchemaSchema } }\n\n private static _instance?: SchemaCache\n\n onSchemaCached?: (name: string, entry: SchemaCacheEntry) => void\n proxy?: string\n\n private _cache = new LRUCache<string, SchemaCacheEntry>({ max: 500, ttl: 1000 * 60 * 5 })\n private _validators: T = {} as T\n\n //prevents double discovery\n private getDebounce = new Debounce()\n\n private constructor(proxy?: string) {\n this.proxy = proxy\n }\n\n static get instance() {\n if (!this._instance) {\n this._instance = new SchemaCache()\n }\n return this._instance\n }\n\n /**\n * A map of cached schema (by name) to payload validators for the schema. A schema\n * must be cached via `get('schema.name')` before it's validator can be used as\n * they are compiled dynamically at runtime upon retrieval.\n */\n get validators(): T {\n return this._validators\n }\n\n async get(schema?: string): Promise<SchemaCacheEntry | undefined | null> {\n if (schema) {\n await this.getDebounce.one(schema, async () => {\n // If we've never looked for it before, it will be undefined\n if (this._cache.get(schema) === undefined) {\n await this.fetchSchema(schema)\n }\n })\n const value = this._cache.get(schema)\n return value === SchemaCache.NULL ? null : value\n }\n return undefined\n }\n\n private cacheSchemaIfValid(entry: SchemaCacheEntry) {\n //only store them if they match the schema root\n if (entry.payload.definition) {\n const ajv = new Ajv({ strict: false })\n //check if it is a valid schema def\n const validator = ajv.compile(entry.payload.definition)\n const schemaName = getSchemaNameFromSchema(entry.payload.definition)\n if (schemaName) {\n this._cache.set(schemaName, entry)\n const key = schemaName as keyof T\n this._validators[key] = validator as unknown as T[keyof T]\n this.onSchemaCached?.(schemaName, entry)\n }\n }\n }\n\n private cacheSchemas(aliasEntries?: FetchedPayload[] | null) {\n for (const entry of aliasEntries?.filter((entry) => entry.payload.schema === SchemaSchema) ?? []) {\n this.cacheSchemaIfValid(entry as SchemaCacheEntry)\n }\n }\n\n private async fetchSchema(schema: string) {\n try {\n const domain = await DomainPayloadWrapper.discover(schema, this.proxy)\n await domain?.fetch()\n this.cacheSchemas(domain?.aliases)\n\n //if it is still undefined, mark it as null (not found)\n if (this._cache.get(schema) === undefined) {\n this._cache.set(schema, SchemaCache.NULL)\n }\n } catch (error) {\n //if failed, set it to NULL, TODO: Make an entry for an error to try again in the future?\n this._cache.set(schema, SchemaCache.NULL)\n if (isAxiosError(error)) {\n console.log(`Axios Url: ${error.response?.config.url}`)\n }\n handleError(error, (error) => {\n console.error(`fetchSchema threw: ${error.message}`)\n })\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,mBAAsB;AAEf,IAAM,WAAN,MAA8B;AAAA,EAC3B,MAAM,oBAAI,IAAkB;AAAA,EAEpC,MAAM,IAAO,KAAW,SAA2B,UAAU,KAAQ;AACnE,UAAM,YAAY,KAAK,IAAI;AAC3B,WAAO,KAAK,IAAI,IAAI,GAAG,GAAG;AACxB,gBAAM,oBAAM,GAAG;AACf,UAAI,KAAK,IAAI,IAAI,YAAY,SAAS;AACpC,cAAM,IAAI,MAAM,uBAAuB,GAAG,GAAG;AAAA,MAC/C;AAAA,IACF;AACA,QAAI;AACF,WAAK,IAAI,IAAI,KAAK,CAAC;AACnB,aAAO,MAAM,QAAQ;AAAA,IACvB,UAAE;AACA,WAAK,IAAI,IAAI,KAAK,CAAC;AAAA,IACrB;AAAA,EACF;AACF;;;ACpBA,mBAA6B;AAC7B,mBAA4B;AAC5B,mCAAqC;AAErC,mCAA4C;AAC5C,iBAAkC;AAClC,uBAAyB;AAKzB,IAAM,0BAA0B,CAAC,WAAyB;AACxD,MAAI,OAAO,KAAK;AACd,WAAO,OAAO;AAAA,EAChB;AACF;AAIO,IAAM,cAAN,MAAM,aAA2E;AAAA;AAAA;AAAA;AAAA;AAAA,EAKtF,OAA0B,OAAyB,EAAE,SAAS,EAAE,YAAY,CAAC,GAAG,QAAQ,0CAAa,EAAE;AAAA,EAEvG,OAAe;AAAA,EAEf;AAAA,EACA;AAAA,EAEQ,SAAS,IAAI,0BAAmC,EAAE,KAAK,KAAK,KAAK,MAAO,KAAK,EAAE,CAAC;AAAA,EAChF,cAAiB,CAAC;AAAA;AAAA,EAGlB,cAAc,IAAI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K;AACrE,YAAM,QAAQ,MAAM;AACpB,WAAK,aAAa,QAAQ,OAAO;AAGjC,UAAI,KAAK,OAAO,IAAI,MAAM,MAAM,QAAW;AACzC,aAAK,OAAO,IAAI,QAAQ,aAAY,IAAI;AAAA,MAC1C;AAAA,IACF,SAAS,OAAO;AAEd,WAAK,OAAO,IAAI,QAAQ,aAAY,IAAI;AACxC,cAAI,2BAAa,KAAK,GAAG;AACvB,gBAAQ,IAAI,cAAc,MAAM,UAAU,OAAO,GAAG,EAAE;AAAA,MACxD;AACA,oCAAY,OAAO,CAACC,WAAU;AAC5B,gBAAQ,MAAM,sBAAsBA,OAAM,OAAO,EAAE;AAAA,MACrD,CAAC;AAAA,IACH;AAAA,EACF;AACF;","names":["entry","error"]}
|
package/dist/browser/index.js
CHANGED
|
@@ -1,12 +1,6 @@
|
|
|
1
|
-
var __defProp = Object.defineProperty;
|
|
2
|
-
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
|
-
|
|
4
1
|
// src/Debounce.ts
|
|
5
2
|
import { delay } from "@xylabs/delay";
|
|
6
3
|
var Debounce = class {
|
|
7
|
-
static {
|
|
8
|
-
__name(this, "Debounce");
|
|
9
|
-
}
|
|
10
4
|
map = /* @__PURE__ */ new Map();
|
|
11
5
|
async one(key, closure, timeout = 1e4) {
|
|
12
6
|
const startTime = Date.now();
|
|
@@ -32,32 +26,21 @@ import { DomainPayloadWrapper } from "@xyo-network/domain-payload-plugin";
|
|
|
32
26
|
import { SchemaSchema } from "@xyo-network/schema-payload-plugin";
|
|
33
27
|
import { Ajv } from "ajv";
|
|
34
28
|
import { LRUCache } from "lru-cache";
|
|
35
|
-
var getSchemaNameFromSchema =
|
|
29
|
+
var getSchemaNameFromSchema = (schema) => {
|
|
36
30
|
if (schema.$id) {
|
|
37
31
|
return schema.$id;
|
|
38
32
|
}
|
|
39
|
-
}
|
|
33
|
+
};
|
|
40
34
|
var SchemaCache = class _SchemaCache {
|
|
41
|
-
static {
|
|
42
|
-
__name(this, "SchemaCache");
|
|
43
|
-
}
|
|
44
35
|
/**
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
static NULL = {
|
|
49
|
-
payload: {
|
|
50
|
-
definition: {},
|
|
51
|
-
schema: SchemaSchema
|
|
52
|
-
}
|
|
53
|
-
};
|
|
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 } };
|
|
54
40
|
static _instance;
|
|
55
41
|
onSchemaCached;
|
|
56
42
|
proxy;
|
|
57
|
-
_cache = new LRUCache({
|
|
58
|
-
max: 500,
|
|
59
|
-
ttl: 1e3 * 60 * 5
|
|
60
|
-
});
|
|
43
|
+
_cache = new LRUCache({ max: 500, ttl: 1e3 * 60 * 5 });
|
|
61
44
|
_validators = {};
|
|
62
45
|
//prevents double discovery
|
|
63
46
|
getDebounce = new Debounce();
|
|
@@ -71,10 +54,10 @@ var SchemaCache = class _SchemaCache {
|
|
|
71
54
|
return this._instance;
|
|
72
55
|
}
|
|
73
56
|
/**
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
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
|
+
*/
|
|
78
61
|
get validators() {
|
|
79
62
|
return this._validators;
|
|
80
63
|
}
|
|
@@ -92,9 +75,7 @@ var SchemaCache = class _SchemaCache {
|
|
|
92
75
|
}
|
|
93
76
|
cacheSchemaIfValid(entry) {
|
|
94
77
|
if (entry.payload.definition) {
|
|
95
|
-
const ajv = new Ajv({
|
|
96
|
-
strict: false
|
|
97
|
-
});
|
|
78
|
+
const ajv = new Ajv({ strict: false });
|
|
98
79
|
const validator = ajv.compile(entry.payload.definition);
|
|
99
80
|
const schemaName = getSchemaNameFromSchema(entry.payload.definition);
|
|
100
81
|
if (schemaName) {
|
|
@@ -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'\nimport { Ajv, SchemaObject } from 'ajv'\nimport { LRUCache } from 'lru-cache'\n\nimport { Debounce } from './Debounce.js'\nimport { SchemaNameToValidatorMap } from './SchemaNameToValidatorMap.js'\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":"
|
|
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.js'\nimport { SchemaNameToValidatorMap } from './SchemaNameToValidatorMap.js'\n\nconst getSchemaNameFromSchema = (schema: SchemaObject) => {\n if (schema.$id) {\n return schema.$id\n }\n}\n\nexport type SchemaCacheEntry = FetchedPayload<SchemaPayload>\n\nexport class SchemaCache<T extends SchemaNameToValidatorMap = SchemaNameToValidatorMap> {\n /**\n * Object representing `null` since LRU Cache types\n * only allow for types that derive from object\n */\n protected static readonly NULL: SchemaCacheEntry = { payload: { definition: {}, schema: SchemaSchema } }\n\n private static _instance?: SchemaCache\n\n onSchemaCached?: (name: string, entry: SchemaCacheEntry) => void\n proxy?: string\n\n private _cache = new LRUCache<string, SchemaCacheEntry>({ max: 500, ttl: 1000 * 60 * 5 })\n private _validators: T = {} as T\n\n //prevents double discovery\n private getDebounce = new Debounce()\n\n private constructor(proxy?: string) {\n this.proxy = proxy\n }\n\n static get instance() {\n if (!this._instance) {\n this._instance = new SchemaCache()\n }\n return this._instance\n }\n\n /**\n * A map of cached schema (by name) to payload validators for the schema. A schema\n * must be cached via `get('schema.name')` before it's validator can be used as\n * they are compiled dynamically at runtime upon retrieval.\n */\n get validators(): T {\n return this._validators\n }\n\n async get(schema?: string): Promise<SchemaCacheEntry | undefined | null> {\n if (schema) {\n await this.getDebounce.one(schema, async () => {\n // If we've never looked for it before, it will be undefined\n if (this._cache.get(schema) === undefined) {\n await this.fetchSchema(schema)\n }\n })\n const value = this._cache.get(schema)\n return value === SchemaCache.NULL ? null : value\n }\n return undefined\n }\n\n private cacheSchemaIfValid(entry: SchemaCacheEntry) {\n //only store them if they match the schema root\n if (entry.payload.definition) {\n const ajv = new Ajv({ strict: false })\n //check if it is a valid schema def\n const validator = ajv.compile(entry.payload.definition)\n const schemaName = getSchemaNameFromSchema(entry.payload.definition)\n if (schemaName) {\n this._cache.set(schemaName, entry)\n const key = schemaName as keyof T\n this._validators[key] = validator as unknown as T[keyof T]\n this.onSchemaCached?.(schemaName, entry)\n }\n }\n }\n\n private cacheSchemas(aliasEntries?: FetchedPayload[] | null) {\n for (const entry of aliasEntries?.filter((entry) => entry.payload.schema === SchemaSchema) ?? []) {\n this.cacheSchemaIfValid(entry as SchemaCacheEntry)\n }\n }\n\n private async fetchSchema(schema: string) {\n try {\n const domain = await DomainPayloadWrapper.discover(schema, this.proxy)\n await domain?.fetch()\n this.cacheSchemas(domain?.aliases)\n\n //if it is still undefined, mark it as null (not found)\n if (this._cache.get(schema) === undefined) {\n this._cache.set(schema, SchemaCache.NULL)\n }\n } catch (error) {\n //if failed, set it to NULL, TODO: Make an entry for an error to try again in the future?\n this._cache.set(schema, SchemaCache.NULL)\n if (isAxiosError(error)) {\n console.log(`Axios Url: ${error.response?.config.url}`)\n }\n handleError(error, (error) => {\n console.error(`fetchSchema threw: ${error.message}`)\n })\n }\n }\n}\n"],"mappings":";AAAA,SAAS,aAAa;AAEf,IAAM,WAAN,MAA8B;AAAA,EAC3B,MAAM,oBAAI,IAAkB;AAAA,EAEpC,MAAM,IAAO,KAAW,SAA2B,UAAU,KAAQ;AACnE,UAAM,YAAY,KAAK,IAAI;AAC3B,WAAO,KAAK,IAAI,IAAI,GAAG,GAAG;AACxB,YAAM,MAAM,GAAG;AACf,UAAI,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EACF;AACF;","names":["entry","error"]}
|
package/dist/neutral/index.cjs
CHANGED
|
@@ -3,7 +3,6 @@ var __defProp = Object.defineProperty;
|
|
|
3
3
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
5
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
7
6
|
var __export = (target, all) => {
|
|
8
7
|
for (var name in all)
|
|
9
8
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
@@ -29,9 +28,6 @@ module.exports = __toCommonJS(src_exports);
|
|
|
29
28
|
// src/Debounce.ts
|
|
30
29
|
var import_delay = require("@xylabs/delay");
|
|
31
30
|
var Debounce = class {
|
|
32
|
-
static {
|
|
33
|
-
__name(this, "Debounce");
|
|
34
|
-
}
|
|
35
31
|
map = /* @__PURE__ */ new Map();
|
|
36
32
|
async one(key, closure, timeout = 1e4) {
|
|
37
33
|
const startTime = Date.now();
|
|
@@ -57,32 +53,21 @@ var import_domain_payload_plugin = require("@xyo-network/domain-payload-plugin")
|
|
|
57
53
|
var import_schema_payload_plugin = require("@xyo-network/schema-payload-plugin");
|
|
58
54
|
var import_ajv = require("ajv");
|
|
59
55
|
var import_lru_cache = require("lru-cache");
|
|
60
|
-
var getSchemaNameFromSchema =
|
|
56
|
+
var getSchemaNameFromSchema = (schema) => {
|
|
61
57
|
if (schema.$id) {
|
|
62
58
|
return schema.$id;
|
|
63
59
|
}
|
|
64
|
-
}
|
|
60
|
+
};
|
|
65
61
|
var SchemaCache = class _SchemaCache {
|
|
66
|
-
static {
|
|
67
|
-
__name(this, "SchemaCache");
|
|
68
|
-
}
|
|
69
62
|
/**
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
static NULL = {
|
|
74
|
-
payload: {
|
|
75
|
-
definition: {},
|
|
76
|
-
schema: import_schema_payload_plugin.SchemaSchema
|
|
77
|
-
}
|
|
78
|
-
};
|
|
63
|
+
* Object representing `null` since LRU Cache types
|
|
64
|
+
* only allow for types that derive from object
|
|
65
|
+
*/
|
|
66
|
+
static NULL = { payload: { definition: {}, schema: import_schema_payload_plugin.SchemaSchema } };
|
|
79
67
|
static _instance;
|
|
80
68
|
onSchemaCached;
|
|
81
69
|
proxy;
|
|
82
|
-
_cache = new import_lru_cache.LRUCache({
|
|
83
|
-
max: 500,
|
|
84
|
-
ttl: 1e3 * 60 * 5
|
|
85
|
-
});
|
|
70
|
+
_cache = new import_lru_cache.LRUCache({ max: 500, ttl: 1e3 * 60 * 5 });
|
|
86
71
|
_validators = {};
|
|
87
72
|
//prevents double discovery
|
|
88
73
|
getDebounce = new Debounce();
|
|
@@ -96,10 +81,10 @@ var SchemaCache = class _SchemaCache {
|
|
|
96
81
|
return this._instance;
|
|
97
82
|
}
|
|
98
83
|
/**
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
84
|
+
* A map of cached schema (by name) to payload validators for the schema. A schema
|
|
85
|
+
* must be cached via `get('schema.name')` before it's validator can be used as
|
|
86
|
+
* they are compiled dynamically at runtime upon retrieval.
|
|
87
|
+
*/
|
|
103
88
|
get validators() {
|
|
104
89
|
return this._validators;
|
|
105
90
|
}
|
|
@@ -117,9 +102,7 @@ var SchemaCache = class _SchemaCache {
|
|
|
117
102
|
}
|
|
118
103
|
cacheSchemaIfValid(entry) {
|
|
119
104
|
if (entry.payload.definition) {
|
|
120
|
-
const ajv = new import_ajv.Ajv({
|
|
121
|
-
strict: false
|
|
122
|
-
});
|
|
105
|
+
const ajv = new import_ajv.Ajv({ strict: false });
|
|
123
106
|
const validator = ajv.compile(entry.payload.definition);
|
|
124
107
|
const schemaName = getSchemaNameFromSchema(entry.payload.definition);
|
|
125
108
|
if (schemaName) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/index.ts","../../src/Debounce.ts","../../src/SchemaCache.ts"],"sourcesContent":["export * from './Debounce.js'\nexport * from './SchemaCache.js'\nexport * from './SchemaNameToValidatorMap.js'\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.js'\nimport { SchemaNameToValidatorMap } from './SchemaNameToValidatorMap.js'\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":"
|
|
1
|
+
{"version":3,"sources":["../../src/index.ts","../../src/Debounce.ts","../../src/SchemaCache.ts"],"sourcesContent":["export * from './Debounce.js'\nexport * from './SchemaCache.js'\nexport * from './SchemaNameToValidatorMap.js'\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.js'\nimport { SchemaNameToValidatorMap } from './SchemaNameToValidatorMap.js'\n\nconst getSchemaNameFromSchema = (schema: SchemaObject) => {\n if (schema.$id) {\n return schema.$id\n }\n}\n\nexport type SchemaCacheEntry = FetchedPayload<SchemaPayload>\n\nexport class SchemaCache<T extends SchemaNameToValidatorMap = SchemaNameToValidatorMap> {\n /**\n * Object representing `null` since LRU Cache types\n * only allow for types that derive from object\n */\n protected static readonly NULL: SchemaCacheEntry = { payload: { definition: {}, schema: SchemaSchema } }\n\n private static _instance?: SchemaCache\n\n onSchemaCached?: (name: string, entry: SchemaCacheEntry) => void\n proxy?: string\n\n private _cache = new LRUCache<string, SchemaCacheEntry>({ max: 500, ttl: 1000 * 60 * 5 })\n private _validators: T = {} as T\n\n //prevents double discovery\n private getDebounce = new Debounce()\n\n private constructor(proxy?: string) {\n this.proxy = proxy\n }\n\n static get instance() {\n if (!this._instance) {\n this._instance = new SchemaCache()\n }\n return this._instance\n }\n\n /**\n * A map of cached schema (by name) to payload validators for the schema. A schema\n * must be cached via `get('schema.name')` before it's validator can be used as\n * they are compiled dynamically at runtime upon retrieval.\n */\n get validators(): T {\n return this._validators\n }\n\n async get(schema?: string): Promise<SchemaCacheEntry | undefined | null> {\n if (schema) {\n await this.getDebounce.one(schema, async () => {\n // If we've never looked for it before, it will be undefined\n if (this._cache.get(schema) === undefined) {\n await this.fetchSchema(schema)\n }\n })\n const value = this._cache.get(schema)\n return value === SchemaCache.NULL ? null : value\n }\n return undefined\n }\n\n private cacheSchemaIfValid(entry: SchemaCacheEntry) {\n //only store them if they match the schema root\n if (entry.payload.definition) {\n const ajv = new Ajv({ strict: false })\n //check if it is a valid schema def\n const validator = ajv.compile(entry.payload.definition)\n const schemaName = getSchemaNameFromSchema(entry.payload.definition)\n if (schemaName) {\n this._cache.set(schemaName, entry)\n const key = schemaName as keyof T\n this._validators[key] = validator as unknown as T[keyof T]\n this.onSchemaCached?.(schemaName, entry)\n }\n }\n }\n\n private cacheSchemas(aliasEntries?: FetchedPayload[] | null) {\n for (const entry of aliasEntries?.filter((entry) => entry.payload.schema === SchemaSchema) ?? []) {\n this.cacheSchemaIfValid(entry as SchemaCacheEntry)\n }\n }\n\n private async fetchSchema(schema: string) {\n try {\n const domain = await DomainPayloadWrapper.discover(schema, this.proxy)\n await domain?.fetch()\n this.cacheSchemas(domain?.aliases)\n\n //if it is still undefined, mark it as null (not found)\n if (this._cache.get(schema) === undefined) {\n this._cache.set(schema, SchemaCache.NULL)\n }\n } catch (error) {\n //if failed, set it to NULL, TODO: Make an entry for an error to try again in the future?\n this._cache.set(schema, SchemaCache.NULL)\n if (isAxiosError(error)) {\n console.log(`Axios Url: ${error.response?.config.url}`)\n }\n handleError(error, (error) => {\n console.error(`fetchSchema threw: ${error.message}`)\n })\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,mBAAsB;AAEf,IAAM,WAAN,MAA8B;AAAA,EAC3B,MAAM,oBAAI,IAAkB;AAAA,EAEpC,MAAM,IAAO,KAAW,SAA2B,UAAU,KAAQ;AACnE,UAAM,YAAY,KAAK,I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,OAAM,OAAO,EAAE;AAAA,MACrD,CAAC;AAAA,IACH;AAAA,EACF;AACF;","names":["entry","error"]}
|
package/dist/neutral/index.js
CHANGED
|
@@ -1,12 +1,6 @@
|
|
|
1
|
-
var __defProp = Object.defineProperty;
|
|
2
|
-
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
|
-
|
|
4
1
|
// src/Debounce.ts
|
|
5
2
|
import { delay } from "@xylabs/delay";
|
|
6
3
|
var Debounce = class {
|
|
7
|
-
static {
|
|
8
|
-
__name(this, "Debounce");
|
|
9
|
-
}
|
|
10
4
|
map = /* @__PURE__ */ new Map();
|
|
11
5
|
async one(key, closure, timeout = 1e4) {
|
|
12
6
|
const startTime = Date.now();
|
|
@@ -32,32 +26,21 @@ import { DomainPayloadWrapper } from "@xyo-network/domain-payload-plugin";
|
|
|
32
26
|
import { SchemaSchema } from "@xyo-network/schema-payload-plugin";
|
|
33
27
|
import { Ajv } from "ajv";
|
|
34
28
|
import { LRUCache } from "lru-cache";
|
|
35
|
-
var getSchemaNameFromSchema =
|
|
29
|
+
var getSchemaNameFromSchema = (schema) => {
|
|
36
30
|
if (schema.$id) {
|
|
37
31
|
return schema.$id;
|
|
38
32
|
}
|
|
39
|
-
}
|
|
33
|
+
};
|
|
40
34
|
var SchemaCache = class _SchemaCache {
|
|
41
|
-
static {
|
|
42
|
-
__name(this, "SchemaCache");
|
|
43
|
-
}
|
|
44
35
|
/**
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
static NULL = {
|
|
49
|
-
payload: {
|
|
50
|
-
definition: {},
|
|
51
|
-
schema: SchemaSchema
|
|
52
|
-
}
|
|
53
|
-
};
|
|
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 } };
|
|
54
40
|
static _instance;
|
|
55
41
|
onSchemaCached;
|
|
56
42
|
proxy;
|
|
57
|
-
_cache = new LRUCache({
|
|
58
|
-
max: 500,
|
|
59
|
-
ttl: 1e3 * 60 * 5
|
|
60
|
-
});
|
|
43
|
+
_cache = new LRUCache({ max: 500, ttl: 1e3 * 60 * 5 });
|
|
61
44
|
_validators = {};
|
|
62
45
|
//prevents double discovery
|
|
63
46
|
getDebounce = new Debounce();
|
|
@@ -71,10 +54,10 @@ var SchemaCache = class _SchemaCache {
|
|
|
71
54
|
return this._instance;
|
|
72
55
|
}
|
|
73
56
|
/**
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
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
|
+
*/
|
|
78
61
|
get validators() {
|
|
79
62
|
return this._validators;
|
|
80
63
|
}
|
|
@@ -92,9 +75,7 @@ var SchemaCache = class _SchemaCache {
|
|
|
92
75
|
}
|
|
93
76
|
cacheSchemaIfValid(entry) {
|
|
94
77
|
if (entry.payload.definition) {
|
|
95
|
-
const ajv = new Ajv({
|
|
96
|
-
strict: false
|
|
97
|
-
});
|
|
78
|
+
const ajv = new Ajv({ strict: false });
|
|
98
79
|
const validator = ajv.compile(entry.payload.definition);
|
|
99
80
|
const schemaName = getSchemaNameFromSchema(entry.payload.definition);
|
|
100
81
|
if (schemaName) {
|
|
@@ -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'\nimport { Ajv, SchemaObject } from 'ajv'\nimport { LRUCache } from 'lru-cache'\n\nimport { Debounce } from './Debounce.js'\nimport { SchemaNameToValidatorMap } from './SchemaNameToValidatorMap.js'\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":"
|
|
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.js'\nimport { SchemaNameToValidatorMap } from './SchemaNameToValidatorMap.js'\n\nconst getSchemaNameFromSchema = (schema: SchemaObject) => {\n if (schema.$id) {\n return schema.$id\n }\n}\n\nexport type SchemaCacheEntry = FetchedPayload<SchemaPayload>\n\nexport class SchemaCache<T extends SchemaNameToValidatorMap = SchemaNameToValidatorMap> {\n /**\n * Object representing `null` since LRU Cache types\n * only allow for types that derive from object\n */\n protected static readonly NULL: SchemaCacheEntry = { payload: { definition: {}, schema: SchemaSchema } }\n\n private static _instance?: SchemaCache\n\n onSchemaCached?: (name: string, entry: SchemaCacheEntry) => void\n proxy?: string\n\n private _cache = new LRUCache<string, SchemaCacheEntry>({ max: 500, ttl: 1000 * 60 * 5 })\n private _validators: T = {} as T\n\n //prevents double discovery\n private getDebounce = new Debounce()\n\n private constructor(proxy?: string) {\n this.proxy = proxy\n }\n\n static get instance() {\n if (!this._instance) {\n this._instance = new SchemaCache()\n }\n return this._instance\n }\n\n /**\n * A map of cached schema (by name) to payload validators for the schema. A schema\n * must be cached via `get('schema.name')` before it's validator can be used as\n * they are compiled dynamically at runtime upon retrieval.\n */\n get validators(): T {\n return this._validators\n }\n\n async get(schema?: string): Promise<SchemaCacheEntry | undefined | null> {\n if (schema) {\n await this.getDebounce.one(schema, async () => {\n // If we've never looked for it before, it will be undefined\n if (this._cache.get(schema) === undefined) {\n await this.fetchSchema(schema)\n }\n })\n const value = this._cache.get(schema)\n return value === SchemaCache.NULL ? null : value\n }\n return undefined\n }\n\n private cacheSchemaIfValid(entry: SchemaCacheEntry) {\n //only store them if they match the schema root\n if (entry.payload.definition) {\n const ajv = new Ajv({ strict: false })\n //check if it is a valid schema def\n const validator = ajv.compile(entry.payload.definition)\n const schemaName = getSchemaNameFromSchema(entry.payload.definition)\n if (schemaName) {\n this._cache.set(schemaName, entry)\n const key = schemaName as keyof T\n this._validators[key] = validator as unknown as T[keyof T]\n this.onSchemaCached?.(schemaName, entry)\n }\n }\n }\n\n private cacheSchemas(aliasEntries?: FetchedPayload[] | null) {\n for (const entry of aliasEntries?.filter((entry) => entry.payload.schema === SchemaSchema) ?? []) {\n this.cacheSchemaIfValid(entry as SchemaCacheEntry)\n }\n }\n\n private async fetchSchema(schema: string) {\n try {\n const domain = await DomainPayloadWrapper.discover(schema, this.proxy)\n await domain?.fetch()\n this.cacheSchemas(domain?.aliases)\n\n //if it is still undefined, mark it as null (not found)\n if (this._cache.get(schema) === undefined) {\n this._cache.set(schema, SchemaCache.NULL)\n }\n } catch (error) {\n //if failed, set it to NULL, TODO: Make an entry for an error to try again in the future?\n this._cache.set(schema, SchemaCache.NULL)\n if (isAxiosError(error)) {\n console.log(`Axios Url: ${error.response?.config.url}`)\n }\n handleError(error, (error) => {\n console.error(`fetchSchema threw: ${error.message}`)\n })\n }\n }\n}\n"],"mappings":";AAAA,SAAS,aAAa;AAEf,IAAM,WAAN,MAA8B;AAAA,EAC3B,MAAM,oBAAI,IAAkB;AAAA,EAEpC,MAAM,IAAO,KAAW,SAA2B,UAAU,KAAQ;AACnE,UAAM,YAAY,KAAK,IAAI;AAC3B,WAAO,KAAK,IAAI,IAAI,GAAG,GAAG;AACxB,YAAM,MAAM,GAAG;AACf,UAAI,KAAK,IAAI,IAAI,YAAY,SAAS;AACpC,cAAM,IAAI,MAAM,uBAAuB,GAAG,GAAG;AAAA,MAC/C;AAAA,IACF;AACA,QAAI;AACF,WAAK,IAAI,IAAI,KAAK,CAAC;AACnB,aAAO,MAAM,QAAQ;AAAA,IACvB,UAAE;AACA,WAAK,IAAI,IAAI,KAAK,CAAC;AAAA,IACrB;AAAA,EACF;AACF;;;ACpBA,SAAS,oBAAoB;AAC7B,SAAS,mBAAmB;AAC5B,SAAS,4BAA4B;AAErC,SAAwB,oBAAoB;AAC5C,SAAS,WAAyB;AAClC,SAAS,gBAAgB;AAKzB,IA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AO,IAAI,YAAY,KAAK;AACjC,cAAM,MAAM;AACZ,aAAK,YAAY,GAAG,IAAI;AACxB,aAAK,iBAAiB,YAAY,KAAK;AAAA,MACzC;AAAA,IACF;AAAA,EACF;AAAA,EAEQ,aAAa,cAAwC;AAC3D,eAAW,SAAS,cAAc,OAAO,CAACA,WAAUA,OAAM,QAAQ,WAAW,YAAY,KAAK,CAAC,GAAG;AAChG,WAAK,mBAAmB,KAAyB;AAAA,IACnD;AAAA,EACF;AAAA,EAEA,MAAc,YAAY,QAAgB;AACxC,QAAI;AACF,YAAM,SAAS,MAAM,qBAAqB,SAAS,QAAQ,KAAK,KAAK;AACrE,YAAM,QAAQ,MAAM;AACpB,WAAK,aAAa,QAAQ,OAAO;AAGjC,UAAI,KAAK,OAAO,IAAI,MAAM,MAAM,QAAW;AACzC,aAAK,OAAO,IAAI,QAAQ,aAAY,IAAI;AAAA,MAC1C;AAAA,IACF,SAAS,OAAO;AAEd,WAAK,OAAO,IAAI,QAAQ,aAAY,IAAI;AACxC,UAAI,aAAa,KAAK,GAAG;AACvB,gBAAQ,IAAI,cAAc,MAAM,UAAU,OAAO,GAAG,EAAE;AAAA,MACxD;AACA,kBAAY,OAAO,CAACC,WAAU;AAC5B,gBAAQ,MAAM,sBAAsBA,OAAM,OAAO,EAAE;AAAA,MACrD,CAAC;AAAA,IACH;AAAA,EACF;AACF;","names":["entry","error"]}
|
package/dist/node/index.cjs
CHANGED
|
@@ -3,8 +3,6 @@ var __defProp = Object.defineProperty;
|
|
|
3
3
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
5
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
7
|
-
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
8
6
|
var __export = (target, all) => {
|
|
9
7
|
for (var name in all)
|
|
10
8
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
@@ -18,7 +16,6 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
18
16
|
return to;
|
|
19
17
|
};
|
|
20
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
21
|
-
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
22
19
|
|
|
23
20
|
// src/index.ts
|
|
24
21
|
var src_exports = {};
|
|
@@ -30,7 +27,7 @@ module.exports = __toCommonJS(src_exports);
|
|
|
30
27
|
|
|
31
28
|
// src/Debounce.ts
|
|
32
29
|
var import_delay = require("@xylabs/delay");
|
|
33
|
-
var
|
|
30
|
+
var Debounce = class {
|
|
34
31
|
map = /* @__PURE__ */ new Map();
|
|
35
32
|
async one(key, closure, timeout = 1e4) {
|
|
36
33
|
const startTime = Date.now();
|
|
@@ -48,8 +45,6 @@ var _Debounce = class _Debounce {
|
|
|
48
45
|
}
|
|
49
46
|
}
|
|
50
47
|
};
|
|
51
|
-
__name(_Debounce, "Debounce");
|
|
52
|
-
var Debounce = _Debounce;
|
|
53
48
|
|
|
54
49
|
// src/SchemaCache.ts
|
|
55
50
|
var import_axios = require("@xylabs/axios");
|
|
@@ -58,18 +53,21 @@ var import_domain_payload_plugin = require("@xyo-network/domain-payload-plugin")
|
|
|
58
53
|
var import_schema_payload_plugin = require("@xyo-network/schema-payload-plugin");
|
|
59
54
|
var import_ajv = require("ajv");
|
|
60
55
|
var import_lru_cache = require("lru-cache");
|
|
61
|
-
var getSchemaNameFromSchema =
|
|
56
|
+
var getSchemaNameFromSchema = (schema) => {
|
|
62
57
|
if (schema.$id) {
|
|
63
58
|
return schema.$id;
|
|
64
59
|
}
|
|
65
|
-
}
|
|
66
|
-
var
|
|
60
|
+
};
|
|
61
|
+
var SchemaCache = class _SchemaCache {
|
|
62
|
+
/**
|
|
63
|
+
* Object representing `null` since LRU Cache types
|
|
64
|
+
* only allow for types that derive from object
|
|
65
|
+
*/
|
|
66
|
+
static NULL = { payload: { definition: {}, schema: import_schema_payload_plugin.SchemaSchema } };
|
|
67
|
+
static _instance;
|
|
67
68
|
onSchemaCached;
|
|
68
69
|
proxy;
|
|
69
|
-
_cache = new import_lru_cache.LRUCache({
|
|
70
|
-
max: 500,
|
|
71
|
-
ttl: 1e3 * 60 * 5
|
|
72
|
-
});
|
|
70
|
+
_cache = new import_lru_cache.LRUCache({ max: 500, ttl: 1e3 * 60 * 5 });
|
|
73
71
|
_validators = {};
|
|
74
72
|
//prevents double discovery
|
|
75
73
|
getDebounce = new Debounce();
|
|
@@ -83,10 +81,10 @@ var _SchemaCache = class _SchemaCache {
|
|
|
83
81
|
return this._instance;
|
|
84
82
|
}
|
|
85
83
|
/**
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
84
|
+
* A map of cached schema (by name) to payload validators for the schema. A schema
|
|
85
|
+
* must be cached via `get('schema.name')` before it's validator can be used as
|
|
86
|
+
* they are compiled dynamically at runtime upon retrieval.
|
|
87
|
+
*/
|
|
90
88
|
get validators() {
|
|
91
89
|
return this._validators;
|
|
92
90
|
}
|
|
@@ -105,9 +103,7 @@ var _SchemaCache = class _SchemaCache {
|
|
|
105
103
|
cacheSchemaIfValid(entry) {
|
|
106
104
|
var _a;
|
|
107
105
|
if (entry.payload.definition) {
|
|
108
|
-
const ajv = new import_ajv.Ajv({
|
|
109
|
-
strict: false
|
|
110
|
-
});
|
|
106
|
+
const ajv = new import_ajv.Ajv({ strict: false });
|
|
111
107
|
const validator = ajv.compile(entry.payload.definition);
|
|
112
108
|
const schemaName = getSchemaNameFromSchema(entry.payload.definition);
|
|
113
109
|
if (schemaName) {
|
|
@@ -143,19 +139,6 @@ var _SchemaCache = class _SchemaCache {
|
|
|
143
139
|
}
|
|
144
140
|
}
|
|
145
141
|
};
|
|
146
|
-
__name(_SchemaCache, "SchemaCache");
|
|
147
|
-
/**
|
|
148
|
-
* Object representing `null` since LRU Cache types
|
|
149
|
-
* only allow for types that derive from object
|
|
150
|
-
*/
|
|
151
|
-
__publicField(_SchemaCache, "NULL", {
|
|
152
|
-
payload: {
|
|
153
|
-
definition: {},
|
|
154
|
-
schema: import_schema_payload_plugin.SchemaSchema
|
|
155
|
-
}
|
|
156
|
-
});
|
|
157
|
-
__publicField(_SchemaCache, "_instance");
|
|
158
|
-
var SchemaCache = _SchemaCache;
|
|
159
142
|
// Annotate the CommonJS export names for ESM import in node:
|
|
160
143
|
0 && (module.exports = {
|
|
161
144
|
Debounce,
|
package/dist/node/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/index.ts","../../src/Debounce.ts","../../src/SchemaCache.ts"],"sourcesContent":["export * from './Debounce.js'\nexport * from './SchemaCache.js'\nexport * from './SchemaNameToValidatorMap.js'\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.js'\nimport { SchemaNameToValidatorMap } from './SchemaNameToValidatorMap.js'\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":"
|
|
1
|
+
{"version":3,"sources":["../../src/index.ts","../../src/Debounce.ts","../../src/SchemaCache.ts"],"sourcesContent":["export * from './Debounce.js'\nexport * from './SchemaCache.js'\nexport * from './SchemaNameToValidatorMap.js'\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.js'\nimport { SchemaNameToValidatorMap } from './SchemaNameToValidatorMap.js'\n\nconst getSchemaNameFromSchema = (schema: SchemaObject) => {\n if (schema.$id) {\n return schema.$id\n }\n}\n\nexport type SchemaCacheEntry = FetchedPayload<SchemaPayload>\n\nexport class SchemaCache<T extends SchemaNameToValidatorMap = SchemaNameToValidatorMap> {\n /**\n * Object representing `null` since LRU Cache types\n * only allow for types that derive from object\n */\n protected static readonly NULL: SchemaCacheEntry = { payload: { definition: {}, schema: SchemaSchema } }\n\n private static _instance?: SchemaCache\n\n onSchemaCached?: (name: string, entry: SchemaCacheEntry) => void\n proxy?: string\n\n private _cache = new LRUCache<string, SchemaCacheEntry>({ max: 500, ttl: 1000 * 60 * 5 })\n private _validators: T = {} as T\n\n //prevents double discovery\n private getDebounce = new Debounce()\n\n private constructor(proxy?: string) {\n this.proxy = proxy\n }\n\n static get instance() {\n if (!this._instance) {\n this._instance = new SchemaCache()\n }\n return this._instance\n }\n\n /**\n * A map of cached schema (by name) to payload validators for the schema. A schema\n * must be cached via `get('schema.name')` before it's validator can be used as\n * they are compiled dynamically at runtime upon retrieval.\n */\n get validators(): T {\n return this._validators\n }\n\n async get(schema?: string): Promise<SchemaCacheEntry | undefined | null> {\n if (schema) {\n await this.getDebounce.one(schema, async () => {\n // If we've never looked for it before, it will be undefined\n if (this._cache.get(schema) === undefined) {\n await this.fetchSchema(schema)\n }\n })\n const value = this._cache.get(schema)\n return value === SchemaCache.NULL ? null : value\n }\n return undefined\n }\n\n private cacheSchemaIfValid(entry: SchemaCacheEntry) {\n //only store them if they match the schema root\n if (entry.payload.definition) {\n const ajv = new Ajv({ strict: false })\n //check if it is a valid schema def\n const validator = ajv.compile(entry.payload.definition)\n const schemaName = getSchemaNameFromSchema(entry.payload.definition)\n if (schemaName) {\n this._cache.set(schemaName, entry)\n const key = schemaName as keyof T\n this._validators[key] = validator as unknown as T[keyof T]\n this.onSchemaCached?.(schemaName, entry)\n }\n }\n }\n\n private cacheSchemas(aliasEntries?: FetchedPayload[] | null) {\n for (const entry of aliasEntries?.filter((entry) => entry.payload.schema === SchemaSchema) ?? []) {\n this.cacheSchemaIfValid(entry as SchemaCacheEntry)\n }\n }\n\n private async fetchSchema(schema: string) {\n try {\n const domain = await DomainPayloadWrapper.discover(schema, this.proxy)\n await domain?.fetch()\n this.cacheSchemas(domain?.aliases)\n\n //if it is still undefined, mark it as null (not found)\n if (this._cache.get(schema) === undefined) {\n this._cache.set(schema, SchemaCache.NULL)\n }\n } catch (error) {\n //if failed, set it to NULL, TODO: Make an entry for an error to try again in the future?\n this._cache.set(schema, SchemaCache.NULL)\n if (isAxiosError(error)) {\n console.log(`Axios Url: ${error.response?.config.url}`)\n }\n handleError(error, (error) => {\n console.error(`fetchSchema threw: ${error.message}`)\n })\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,mBAAsB;AAEf,IAAM,WAAN,MAA8B;AAAA,EAC3B,MAAM,oBAAI,IAAkB;AAAA,EAEpC,MAAM,IAAO,KAAW,SAA2B,UAAU,KAAQ;AACnE,UAAM,YAAY,KAAK,IAAI;AAC3B,WAAO,KAAK,IAAI,IAAI,GAAG,GAAG;AACxB,gBAAM,oBAAM,GAAG;AACf,UAAI,KAAK,IAAI,IAAI,YAAY,SAAS;AACpC,cAAM,IAAI,MAAM,uBAAuB,GAAG,GAAG;AAAA,MAC/C;AAAA,IACF;AACA,QAAI;AACF,WAAK,IAAI,IAAI,KAAK,CAAC;AACnB,aAAO,MAAM,QAAQ;AAAA,IACvB,UAAE;AACA,WAAK,IAAI,IAAI,KAAK,CAAC;AAAA,IACrB;AAAA,EACF;AACF;;;ACpBA,mBAA6B;AAC7B,mBAA4B;AAC5B,mCAAqC;AAErC,mCAA4C;AAC5C,iBAAkC;AAClC,uBAAyB;AAKzB,IAAM,0BAA0B,CAAC,WAAyB;AACxD,MAAI,OAAO,KAAK;AACd,WAAO,OAAO;AAAA,EAChB;AACF;AAIO,IAAM,cAAN,MAAM,aAA2E;AAAA;AAAA;AAAA;AAAA;AAAA,EAKtF,OAA0B,OAAyB,EAAE,SAAS,EAAE,YAAY,CAAC,GAAG,QAAQ,0CAAa,EAAE;AAAA,EAEvG,OAAe;AAAA,EAEf;AAAA,EACA;AAAA,EAEQ,SAAS,IAAI,0BAA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,IACnD;AAAA,EACF;AAAA,EAEA,MAAc,YAAY,QAAgB;AA7F5C;AA8FI,QAAI;AACF,YAAM,SAAS,MAAM,kDAAqB,SAAS,QAAQ,KAAK,KAAK;AACrE,aAAM,iCAAQ;AACd,WAAK,aAAa,iCAAQ,OAAO;AAGjC,UAAI,KAAK,OAAO,IAAI,MAAM,MAAM,QAAW;AACzC,aAAK,OAAO,IAAI,QAAQ,aAAY,IAAI;AAAA,MAC1C;AAAA,IACF,SAAS,OAAO;AAEd,WAAK,OAAO,IAAI,QAAQ,aAAY,IAAI;AACxC,cAAI,2BAAa,KAAK,GAAG;AACvB,gBAAQ,IAAI,eAAc,WAAM,aAAN,mBAAgB,OAAO,GAAG,EAAE;AAAA,MACxD;AACA,oCAAY,OAAO,CAACC,WAAU;AAC5B,gBAAQ,MAAM,sBAAsBA,OAAM,OAAO,EAAE;AAAA,MACrD,CAAC;AAAA,IACH;AAAA,EACF;AACF;","names":["entry","error"]}
|
package/dist/node/index.js
CHANGED
|
@@ -1,11 +1,6 @@
|
|
|
1
|
-
var __defProp = Object.defineProperty;
|
|
2
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
3
|
-
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
4
|
-
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
5
|
-
|
|
6
1
|
// src/Debounce.ts
|
|
7
2
|
import { delay } from "@xylabs/delay";
|
|
8
|
-
var
|
|
3
|
+
var Debounce = class {
|
|
9
4
|
map = /* @__PURE__ */ new Map();
|
|
10
5
|
async one(key, closure, timeout = 1e4) {
|
|
11
6
|
const startTime = Date.now();
|
|
@@ -23,8 +18,6 @@ var _Debounce = class _Debounce {
|
|
|
23
18
|
}
|
|
24
19
|
}
|
|
25
20
|
};
|
|
26
|
-
__name(_Debounce, "Debounce");
|
|
27
|
-
var Debounce = _Debounce;
|
|
28
21
|
|
|
29
22
|
// src/SchemaCache.ts
|
|
30
23
|
import { isAxiosError } from "@xylabs/axios";
|
|
@@ -33,18 +26,21 @@ import { DomainPayloadWrapper } from "@xyo-network/domain-payload-plugin";
|
|
|
33
26
|
import { SchemaSchema } from "@xyo-network/schema-payload-plugin";
|
|
34
27
|
import { Ajv } from "ajv";
|
|
35
28
|
import { LRUCache } from "lru-cache";
|
|
36
|
-
var getSchemaNameFromSchema =
|
|
29
|
+
var getSchemaNameFromSchema = (schema) => {
|
|
37
30
|
if (schema.$id) {
|
|
38
31
|
return schema.$id;
|
|
39
32
|
}
|
|
40
|
-
}
|
|
41
|
-
var
|
|
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;
|
|
42
41
|
onSchemaCached;
|
|
43
42
|
proxy;
|
|
44
|
-
_cache = new LRUCache({
|
|
45
|
-
max: 500,
|
|
46
|
-
ttl: 1e3 * 60 * 5
|
|
47
|
-
});
|
|
43
|
+
_cache = new LRUCache({ max: 500, ttl: 1e3 * 60 * 5 });
|
|
48
44
|
_validators = {};
|
|
49
45
|
//prevents double discovery
|
|
50
46
|
getDebounce = new Debounce();
|
|
@@ -58,10 +54,10 @@ var _SchemaCache = class _SchemaCache {
|
|
|
58
54
|
return this._instance;
|
|
59
55
|
}
|
|
60
56
|
/**
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
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
|
+
*/
|
|
65
61
|
get validators() {
|
|
66
62
|
return this._validators;
|
|
67
63
|
}
|
|
@@ -80,9 +76,7 @@ var _SchemaCache = class _SchemaCache {
|
|
|
80
76
|
cacheSchemaIfValid(entry) {
|
|
81
77
|
var _a;
|
|
82
78
|
if (entry.payload.definition) {
|
|
83
|
-
const ajv = new Ajv({
|
|
84
|
-
strict: false
|
|
85
|
-
});
|
|
79
|
+
const ajv = new Ajv({ strict: false });
|
|
86
80
|
const validator = ajv.compile(entry.payload.definition);
|
|
87
81
|
const schemaName = getSchemaNameFromSchema(entry.payload.definition);
|
|
88
82
|
if (schemaName) {
|
|
@@ -118,19 +112,6 @@ var _SchemaCache = class _SchemaCache {
|
|
|
118
112
|
}
|
|
119
113
|
}
|
|
120
114
|
};
|
|
121
|
-
__name(_SchemaCache, "SchemaCache");
|
|
122
|
-
/**
|
|
123
|
-
* Object representing `null` since LRU Cache types
|
|
124
|
-
* only allow for types that derive from object
|
|
125
|
-
*/
|
|
126
|
-
__publicField(_SchemaCache, "NULL", {
|
|
127
|
-
payload: {
|
|
128
|
-
definition: {},
|
|
129
|
-
schema: SchemaSchema
|
|
130
|
-
}
|
|
131
|
-
});
|
|
132
|
-
__publicField(_SchemaCache, "_instance");
|
|
133
|
-
var SchemaCache = _SchemaCache;
|
|
134
115
|
export {
|
|
135
116
|
Debounce,
|
|
136
117
|
SchemaCache
|
package/dist/node/index.js.map
CHANGED
|
@@ -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'\nimport { Ajv, SchemaObject } from 'ajv'\nimport { LRUCache } from 'lru-cache'\n\nimport { Debounce } from './Debounce.js'\nimport { SchemaNameToValidatorMap } from './SchemaNameToValidatorMap.js'\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":"
|
|
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.js'\nimport { SchemaNameToValidatorMap } from './SchemaNameToValidatorMap.js'\n\nconst getSchemaNameFromSchema = (schema: SchemaObject) => {\n if (schema.$id) {\n return schema.$id\n }\n}\n\nexport type SchemaCacheEntry = FetchedPayload<SchemaPayload>\n\nexport class SchemaCache<T extends SchemaNameToValidatorMap = SchemaNameToValidatorMap> {\n /**\n * Object representing `null` since LRU Cache types\n * only allow for types that derive from object\n */\n protected static readonly NULL: SchemaCacheEntry = { payload: { definition: {}, schema: SchemaSchema } }\n\n private static _instance?: SchemaCache\n\n onSchemaCached?: (name: string, entry: SchemaCacheEntry) => void\n proxy?: string\n\n private _cache = new LRUCache<string, SchemaCacheEntry>({ max: 500, ttl: 1000 * 60 * 5 })\n private _validators: T = {} as T\n\n //prevents double discovery\n private getDebounce = new Debounce()\n\n private constructor(proxy?: string) {\n this.proxy = proxy\n }\n\n static get instance() {\n if (!this._instance) {\n this._instance = new SchemaCache()\n }\n return this._instance\n }\n\n /**\n * A map of cached schema (by name) to payload validators for the schema. A schema\n * must be cached via `get('schema.name')` before it's validator can be used as\n * they are compiled dynamically at runtime upon retrieval.\n */\n get validators(): T {\n return this._validators\n }\n\n async get(schema?: string): Promise<SchemaCacheEntry | undefined | null> {\n if (schema) {\n await this.getDebounce.one(schema, async () => {\n // If we've never looked for it before, it will be undefined\n if (this._cache.get(schema) === undefined) {\n await this.fetchSchema(schema)\n }\n })\n const value = this._cache.get(schema)\n return value === SchemaCache.NULL ? null : value\n }\n return undefined\n }\n\n private cacheSchemaIfValid(entry: SchemaCacheEntry) {\n //only store them if they match the schema root\n if (entry.payload.definition) {\n const ajv = new Ajv({ strict: false })\n //check if it is a valid schema def\n const validator = ajv.compile(entry.payload.definition)\n const schemaName = getSchemaNameFromSchema(entry.payload.definition)\n if (schemaName) {\n this._cache.set(schemaName, entry)\n const key = schemaName as keyof T\n this._validators[key] = validator as unknown as T[keyof T]\n this.onSchemaCached?.(schemaName, entry)\n }\n }\n }\n\n private cacheSchemas(aliasEntries?: FetchedPayload[] | null) {\n for (const entry of aliasEntries?.filter((entry) => entry.payload.schema === SchemaSchema) ?? []) {\n this.cacheSchemaIfValid(entry as SchemaCacheEntry)\n }\n }\n\n private async fetchSchema(schema: string) {\n try {\n const domain = await DomainPayloadWrapper.discover(schema, this.proxy)\n await domain?.fetch()\n this.cacheSchemas(domain?.aliases)\n\n //if it is still undefined, mark it as null (not found)\n if (this._cache.get(schema) === undefined) {\n this._cache.set(schema, SchemaCache.NULL)\n }\n } catch (error) {\n //if failed, set it to NULL, TODO: Make an entry for an error to try again in the future?\n this._cache.set(schema, SchemaCache.NULL)\n if (isAxiosError(error)) {\n console.log(`Axios Url: ${error.response?.config.url}`)\n }\n handleError(error, (error) => {\n console.error(`fetchSchema threw: ${error.message}`)\n })\n }\n }\n}\n"],"mappings":";AAAA,SAAS,aAAa;AAEf,IAAM,WAAN,MAA8B;AAAA,EAC3B,MAAM,oBAAI,IAAkB;AAAA,EAEpC,MAAM,IAAO,KAAW,SAA2B,UAAU,KAAQ;AACnE,UAAM,YAAY,KAAK,IAAI;AAC3B,WAAO,KAAK,IAAI,IAAI,GAAG,GAAG;AACxB,YAAM,MAAM,GAAG;AACf,UAAI,KAAK,IAAI,IAAI,YAAY,SAAS;AACpC,cAAM,IAAI,MAAM,uBAAuB,GAAG,GAAG;AAAA,MAC/C;AAAA,IACF;AACA,QAAI;AACF,WAAK,IAAI,IAAI,KAAK,CAAC;AACnB,aAAO,MAAM,QAAQ;AAAA,IACvB,UAAE;AACA,WAAK,IAAI,IAAI,KAAK,CAAC;AAAA,IACrB;AAAA,EACF;AACF;;;ACpBA,SAAS,oBAAoB;AAC7B,SAAS,mBAAmB;AAC5B,SAAS,4BAA4B;AAErC,SAAwB,oBAAoB;AAC5C,SAAS,WAAyB;AAClC,SAAS,gBAAgB;AAKzB,IAAM,0BAA0B,CAAC,WAAyB;AACxD,MAAI,OAAO,KAAK;AACd,WAAO,OAAO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Y,GAAG,IAAI;AACxB,mBAAK,mBAAL,8BAAsB,YAAY;AAAA,MACpC;AAAA,IACF;AAAA,EACF;AAAA,EAEQ,aAAa,cAAwC;AAC3D,eAAW,UAAS,6CAAc,OAAO,CAACA,WAAUA,OAAM,QAAQ,WAAW,kBAAiB,CAAC,GAAG;AAChG,WAAK,mBAAmB,KAAyB;AAAA,IACnD;AAAA,EACF;AAAA,EAEA,MAAc,YAAY,QAAgB;AA7F5C;AA8FI,QAAI;AACF,YAAM,SAAS,MAAM,qBAAqB,SAAS,QAAQ,KAAK,KAAK;AACrE,aAAM,iCAAQ;AACd,WAAK,aAAa,iCAAQ,OAAO;AAGjC,UAAI,KAAK,OAAO,IAAI,MAAM,MAAM,QAAW;AACzC,aAAK,OAAO,IAAI,QAAQ,aAAY,IAAI;AAAA,MAC1C;AAAA,IACF,SAAS,OAAO;AAEd,WAAK,OAAO,IAAI,QAAQ,aAAY,IAAI;AACxC,UAAI,aAAa,KAAK,GAAG;AACvB,gBAAQ,IAAI,eAAc,WAAM,aAAN,mBAAgB,OAAO,GAAG,EAAE;AAAA,MACxD;AACA,kBAAY,OAAO,CAACC,WAAU;AAC5B,gBAAQ,MAAM,sBAAsBA,OAAM,OAAO,EAAE;AAAA,MACrD,CAAC;AAAA,IACH;AAAA,EACF;AACF;","names":["entry","error"]}
|
package/package.json
CHANGED
|
@@ -9,23 +9,23 @@
|
|
|
9
9
|
"url": "https://github.com/XYOracleNetwork/sdk-xyo-client-js/issues"
|
|
10
10
|
},
|
|
11
11
|
"dependencies": {
|
|
12
|
-
"@xylabs/axios": "^3.6.
|
|
13
|
-
"@xylabs/delay": "^3.6.
|
|
14
|
-
"@xylabs/error": "^3.6.
|
|
15
|
-
"@xyo-network/domain-payload-plugin": "^2.110.
|
|
16
|
-
"@xyo-network/huri": "^2.110.
|
|
17
|
-
"@xyo-network/payload-model": "^2.110.
|
|
18
|
-
"@xyo-network/schema-payload-plugin": "^2.110.
|
|
12
|
+
"@xylabs/axios": "^3.6.5",
|
|
13
|
+
"@xylabs/delay": "^3.6.5",
|
|
14
|
+
"@xylabs/error": "^3.6.5",
|
|
15
|
+
"@xyo-network/domain-payload-plugin": "^2.110.11",
|
|
16
|
+
"@xyo-network/huri": "^2.110.11",
|
|
17
|
+
"@xyo-network/payload-model": "^2.110.11",
|
|
18
|
+
"@xyo-network/schema-payload-plugin": "^2.110.11",
|
|
19
19
|
"ajv": "^8.17.1",
|
|
20
20
|
"lru-cache": "^11.0.0"
|
|
21
21
|
},
|
|
22
22
|
"description": "Primary SDK for using XYO Protocol 2.0",
|
|
23
23
|
"devDependencies": {
|
|
24
|
-
"@xylabs/assert": "^3.6.
|
|
25
|
-
"@xylabs/ts-scripts-yarn3": "^3.
|
|
26
|
-
"@xylabs/tsconfig-dom": "^3.
|
|
27
|
-
"@xyo-network/network": "^2.110.
|
|
28
|
-
"@xyo-network/payload-builder": "^2.110.
|
|
24
|
+
"@xylabs/assert": "^3.6.5",
|
|
25
|
+
"@xylabs/ts-scripts-yarn3": "^3.13.3",
|
|
26
|
+
"@xylabs/tsconfig-dom": "^3.13.3",
|
|
27
|
+
"@xyo-network/network": "^2.110.11",
|
|
28
|
+
"@xyo-network/payload-builder": "^2.110.11",
|
|
29
29
|
"typescript": "^5.5.4"
|
|
30
30
|
},
|
|
31
31
|
"exports": {
|
|
@@ -68,5 +68,5 @@
|
|
|
68
68
|
"url": "https://github.com/XYOracleNetwork/sdk-xyo-client-js.git"
|
|
69
69
|
},
|
|
70
70
|
"sideEffects": false,
|
|
71
|
-
"version": "2.110.
|
|
71
|
+
"version": "2.110.11"
|
|
72
72
|
}
|