@xyo-network/schema-cache 2.84.18 → 2.85.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/browser/index.cjs +29 -12
- package/dist/browser/index.cjs.map +1 -1
- package/dist/browser/index.js +31 -12
- package/dist/browser/index.js.map +1 -1
- package/dist/node/index.cjs +36 -16
- package/dist/node/index.cjs.map +1 -1
- package/dist/node/index.js +38 -16
- package/dist/node/index.js.map +1 -1
- package/package.json +7 -7
package/dist/browser/index.cjs
CHANGED
|
@@ -5,6 +5,7 @@ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
|
5
5
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
6
|
var __getProtoOf = Object.getPrototypeOf;
|
|
7
7
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
8
9
|
var __export = (target, all) => {
|
|
9
10
|
for (var name in all)
|
|
10
11
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
@@ -38,6 +39,9 @@ module.exports = __toCommonJS(src_exports);
|
|
|
38
39
|
// src/Debounce.ts
|
|
39
40
|
var import_delay = require("@xylabs/delay");
|
|
40
41
|
var Debounce = class {
|
|
42
|
+
static {
|
|
43
|
+
__name(this, "Debounce");
|
|
44
|
+
}
|
|
41
45
|
map = /* @__PURE__ */ new Map();
|
|
42
46
|
async one(key, closure, timeout = 1e4) {
|
|
43
47
|
const startTime = Date.now();
|
|
@@ -63,21 +67,32 @@ var import_domain_payload_plugin = require("@xyo-network/domain-payload-plugin")
|
|
|
63
67
|
var import_schema_payload_plugin = require("@xyo-network/schema-payload-plugin");
|
|
64
68
|
var import_ajv = __toESM(require("ajv"), 1);
|
|
65
69
|
var import_lru_cache = require("lru-cache");
|
|
66
|
-
var getSchemaNameFromSchema = (schema) => {
|
|
70
|
+
var getSchemaNameFromSchema = /* @__PURE__ */ __name((schema) => {
|
|
67
71
|
if (schema.$id) {
|
|
68
72
|
return schema.$id;
|
|
69
73
|
}
|
|
70
|
-
};
|
|
74
|
+
}, "getSchemaNameFromSchema");
|
|
71
75
|
var SchemaCache = class _SchemaCache {
|
|
76
|
+
static {
|
|
77
|
+
__name(this, "SchemaCache");
|
|
78
|
+
}
|
|
72
79
|
/**
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
static NULL = {
|
|
80
|
+
* Object representing `null` since LRU Cache types
|
|
81
|
+
* only allow for types that derive from object
|
|
82
|
+
*/
|
|
83
|
+
static NULL = {
|
|
84
|
+
payload: {
|
|
85
|
+
definition: {},
|
|
86
|
+
schema: import_schema_payload_plugin.SchemaSchema
|
|
87
|
+
}
|
|
88
|
+
};
|
|
77
89
|
static _instance;
|
|
78
90
|
onSchemaCached;
|
|
79
91
|
proxy;
|
|
80
|
-
_cache = new import_lru_cache.LRUCache({
|
|
92
|
+
_cache = new import_lru_cache.LRUCache({
|
|
93
|
+
max: 500,
|
|
94
|
+
ttl: 1e3 * 60 * 5
|
|
95
|
+
});
|
|
81
96
|
_validators = {};
|
|
82
97
|
//prevents double discovery
|
|
83
98
|
getDebounce = new Debounce();
|
|
@@ -91,10 +106,10 @@ var SchemaCache = class _SchemaCache {
|
|
|
91
106
|
return this._instance;
|
|
92
107
|
}
|
|
93
108
|
/**
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
109
|
+
* A map of cached schema (by name) to payload validators for the schema. A schema
|
|
110
|
+
* must be cached via `get('schema.name')` before it's validator can be used as
|
|
111
|
+
* they are compiled dynamically at runtime upon retrieval.
|
|
112
|
+
*/
|
|
98
113
|
get validators() {
|
|
99
114
|
return this._validators;
|
|
100
115
|
}
|
|
@@ -112,7 +127,9 @@ var SchemaCache = class _SchemaCache {
|
|
|
112
127
|
}
|
|
113
128
|
cacheSchemaIfValid(entry) {
|
|
114
129
|
if (entry.payload.definition) {
|
|
115
|
-
const ajv = new import_ajv.default({
|
|
130
|
+
const ajv = new import_ajv.default({
|
|
131
|
+
strict: false
|
|
132
|
+
});
|
|
116
133
|
const validator = ajv.compile(entry.payload.definition);
|
|
117
134
|
const schemaName = getSchemaNameFromSchema(entry.payload.definition);
|
|
118
135
|
if (schemaName) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/index.ts","../../src/Debounce.ts","../../src/SchemaCache.ts"],"sourcesContent":["export * from './Debounce'\nexport * from './SchemaCache'\nexport * from './SchemaNameToValidatorMap'\n","import { delay } from '@xylabs/delay'\n\nexport class Debounce<TKey = string> {\n private map = new Map<TKey, number>()\n\n async one<T>(key: TKey, closure: () => Promise<T>, timeout = 10_000) {\n const startTime = Date.now()\n while (this.map.get(key)) {\n await delay(100)\n if (Date.now() - startTime > timeout) {\n throw new Error(`Debounce timed out [${key}]`)\n }\n }\n try {\n this.map.set(key, 1)\n return await closure()\n } finally {\n this.map.set(key, 0)\n }\n }\n}\n","import { isAxiosError } from '@xylabs/axios'\nimport { handleError } from '@xylabs/error'\nimport { DomainPayloadWrapper } from '@xyo-network/domain-payload-plugin'\nimport { FetchedPayload } from '@xyo-network/huri'\nimport { SchemaPayload, SchemaSchema } from '@xyo-network/schema-payload-plugin'\nimport Ajv, { SchemaObject } from 'ajv'\nimport { LRUCache } from 'lru-cache'\n\nimport { Debounce } from './Debounce'\nimport { SchemaNameToValidatorMap } from './SchemaNameToValidatorMap'\n\nconst getSchemaNameFromSchema = (schema: SchemaObject) => {\n if (schema.$id) {\n return schema.$id\n }\n}\n\nexport type SchemaCacheEntry = FetchedPayload<SchemaPayload>\n\nexport class SchemaCache<T extends SchemaNameToValidatorMap = SchemaNameToValidatorMap> {\n /**\n * Object representing `null` since LRU Cache types\n * only allow for types that derive from object\n */\n protected static readonly NULL: SchemaCacheEntry = { payload: { definition: {}, schema: SchemaSchema } }\n\n private static _instance?: SchemaCache\n\n onSchemaCached?: (name: string, entry: SchemaCacheEntry) => void\n proxy?: string\n\n private _cache = new LRUCache<string, SchemaCacheEntry>({ max: 500, ttl: 1000 * 60 * 5 })\n private _validators: T = {} as T\n\n //prevents double discovery\n private getDebounce = new Debounce()\n\n private constructor(proxy?: string) {\n this.proxy = proxy\n }\n\n static get instance() {\n if (!this._instance) {\n this._instance = new SchemaCache()\n }\n return this._instance\n }\n\n /**\n * A map of cached schema (by name) to payload validators for the schema. A schema\n * must be cached via `get('schema.name')` before it's validator can be used as\n * they are compiled dynamically at runtime upon retrieval.\n */\n get validators(): T {\n return this._validators\n }\n\n async get(schema?: string): Promise<SchemaCacheEntry | undefined | null> {\n if (schema) {\n await this.getDebounce.one(schema, async () => {\n // If we've never looked for it before, it will be undefined\n if (this._cache.get(schema) === undefined) {\n await this.fetchSchema(schema)\n }\n })\n const value = this._cache.get(schema)\n return value === SchemaCache.NULL ? null : value\n }\n return undefined\n }\n\n private cacheSchemaIfValid(entry: SchemaCacheEntry) {\n //only store them if they match the schema root\n if (entry.payload.definition) {\n const ajv = new Ajv({ strict: false })\n //check if it is a valid schema def\n const validator = ajv.compile(entry.payload.definition)\n const schemaName = getSchemaNameFromSchema(entry.payload.definition)\n if (schemaName) {\n this._cache.set(schemaName, entry)\n const key = schemaName as keyof T\n this._validators[key] = validator as unknown as T[keyof T]\n this.onSchemaCached?.(schemaName, entry)\n }\n }\n }\n\n private cacheSchemas(aliasEntries?: FetchedPayload[] | null) {\n for (const entry of aliasEntries?.filter((entry) => entry.payload.schema === SchemaSchema) ?? []) {\n this.cacheSchemaIfValid(entry as SchemaCacheEntry)\n }\n }\n\n private async fetchSchema(schema: string) {\n try {\n const domain = await DomainPayloadWrapper.discover(schema, this.proxy)\n await domain?.fetch()\n this.cacheSchemas(domain?.aliases)\n\n //if it is still undefined, mark it as null (not found)\n if (this._cache.get(schema) === undefined) {\n this._cache.set(schema, SchemaCache.NULL)\n }\n } catch (error) {\n //if failed, set it to NULL, TODO: Make an entry for an error to try again in the future?\n this._cache.set(schema, SchemaCache.NULL)\n if (isAxiosError(error)) {\n console.log(`Axios Url: ${error.response?.config.url}`)\n }\n handleError(error, (error) => {\n console.error(`fetchSchema threw: ${error.message}`)\n })\n }\n }\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"sources":["../../src/index.ts","../../src/Debounce.ts","../../src/SchemaCache.ts"],"sourcesContent":["export * from './Debounce'\nexport * from './SchemaCache'\nexport * from './SchemaNameToValidatorMap'\n","import { delay } from '@xylabs/delay'\n\nexport class Debounce<TKey = string> {\n private map = new Map<TKey, number>()\n\n async one<T>(key: TKey, closure: () => Promise<T>, timeout = 10_000) {\n const startTime = Date.now()\n while (this.map.get(key)) {\n await delay(100)\n if (Date.now() - startTime > timeout) {\n throw new Error(`Debounce timed out [${key}]`)\n }\n }\n try {\n this.map.set(key, 1)\n return await closure()\n } finally {\n this.map.set(key, 0)\n }\n }\n}\n","import { isAxiosError } from '@xylabs/axios'\nimport { handleError } from '@xylabs/error'\nimport { DomainPayloadWrapper } from '@xyo-network/domain-payload-plugin'\nimport { FetchedPayload } from '@xyo-network/huri'\nimport { SchemaPayload, SchemaSchema } from '@xyo-network/schema-payload-plugin'\nimport Ajv, { SchemaObject } from 'ajv'\nimport { LRUCache } from 'lru-cache'\n\nimport { Debounce } from './Debounce'\nimport { SchemaNameToValidatorMap } from './SchemaNameToValidatorMap'\n\nconst getSchemaNameFromSchema = (schema: SchemaObject) => {\n if (schema.$id) {\n return schema.$id\n }\n}\n\nexport type SchemaCacheEntry = FetchedPayload<SchemaPayload>\n\nexport class SchemaCache<T extends SchemaNameToValidatorMap = SchemaNameToValidatorMap> {\n /**\n * Object representing `null` since LRU Cache types\n * only allow for types that derive from object\n */\n protected static readonly NULL: SchemaCacheEntry = { payload: { definition: {}, schema: SchemaSchema } }\n\n private static _instance?: SchemaCache\n\n onSchemaCached?: (name: string, entry: SchemaCacheEntry) => void\n proxy?: string\n\n private _cache = new LRUCache<string, SchemaCacheEntry>({ max: 500, ttl: 1000 * 60 * 5 })\n private _validators: T = {} as T\n\n //prevents double discovery\n private getDebounce = new Debounce()\n\n private constructor(proxy?: string) {\n this.proxy = proxy\n }\n\n static get instance() {\n if (!this._instance) {\n this._instance = new SchemaCache()\n }\n return this._instance\n }\n\n /**\n * A map of cached schema (by name) to payload validators for the schema. A schema\n * must be cached via `get('schema.name')` before it's validator can be used as\n * they are compiled dynamically at runtime upon retrieval.\n */\n get validators(): T {\n return this._validators\n }\n\n async get(schema?: string): Promise<SchemaCacheEntry | undefined | null> {\n if (schema) {\n await this.getDebounce.one(schema, async () => {\n // If we've never looked for it before, it will be undefined\n if (this._cache.get(schema) === undefined) {\n await this.fetchSchema(schema)\n }\n })\n const value = this._cache.get(schema)\n return value === SchemaCache.NULL ? null : value\n }\n return undefined\n }\n\n private cacheSchemaIfValid(entry: SchemaCacheEntry) {\n //only store them if they match the schema root\n if (entry.payload.definition) {\n const ajv = new Ajv({ strict: false })\n //check if it is a valid schema def\n const validator = ajv.compile(entry.payload.definition)\n const schemaName = getSchemaNameFromSchema(entry.payload.definition)\n if (schemaName) {\n this._cache.set(schemaName, entry)\n const key = schemaName as keyof T\n this._validators[key] = validator as unknown as T[keyof T]\n this.onSchemaCached?.(schemaName, entry)\n }\n }\n }\n\n private cacheSchemas(aliasEntries?: FetchedPayload[] | null) {\n for (const entry of aliasEntries?.filter((entry) => entry.payload.schema === SchemaSchema) ?? []) {\n this.cacheSchemaIfValid(entry as SchemaCacheEntry)\n }\n }\n\n private async fetchSchema(schema: string) {\n try {\n const domain = await DomainPayloadWrapper.discover(schema, this.proxy)\n await domain?.fetch()\n this.cacheSchemas(domain?.aliases)\n\n //if it is still undefined, mark it as null (not found)\n if (this._cache.get(schema) === undefined) {\n this._cache.set(schema, SchemaCache.NULL)\n }\n } catch (error) {\n //if failed, set it to NULL, TODO: Make an entry for an error to try again in the future?\n this._cache.set(schema, SchemaCache.NULL)\n if (isAxiosError(error)) {\n console.log(`Axios Url: ${error.response?.config.url}`)\n }\n handleError(error, (error) => {\n console.error(`fetchSchema threw: ${error.message}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C;IACF,SAASuC,OAAO;AAEd,WAAKhC,OAAOsB,IAAIhC,QAAQE,aAAYC,IAAI;AACxC,cAAIwC,2BAAaD,KAAAA,GAAQ;AACvBE,gBAAQC,IAAI,cAAcH,MAAMI,UAAUC,OAAOC,GAAAA,EAAK;MACxD;AACAC,oCAAYP,OAAO,CAACA,WAAAA;AAClBE,gBAAQF,MAAM,sBAAsBA,OAAMQ,OAAO,EAAE;MACrD,CAAA;IACF;EACF;AACF;","names":["Debounce","map","Map","one","key","closure","timeout","startTime","Date","now","get","delay","Error","set","getSchemaNameFromSchema","schema","$id","SchemaCache","NULL","payload","definition","SchemaSchema","_instance","onSchemaCached","proxy","_cache","LRUCache","max","ttl","_validators","getDebounce","Debounce","instance","validators","get","one","undefined","fetchSchema","value","cacheSchemaIfValid","entry","ajv","Ajv","strict","validator","compile","schemaName","set","key","cacheSchemas","aliasEntries","filter","domain","DomainPayloadWrapper","discover","fetch","aliases","error","isAxiosError","console","log","response","config","url","handleError","message"]}
|
package/dist/browser/index.js
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
|
+
|
|
1
4
|
// src/Debounce.ts
|
|
2
5
|
import { delay } from "@xylabs/delay";
|
|
3
6
|
var Debounce = class {
|
|
7
|
+
static {
|
|
8
|
+
__name(this, "Debounce");
|
|
9
|
+
}
|
|
4
10
|
map = /* @__PURE__ */ new Map();
|
|
5
11
|
async one(key, closure, timeout = 1e4) {
|
|
6
12
|
const startTime = Date.now();
|
|
@@ -26,21 +32,32 @@ import { DomainPayloadWrapper } from "@xyo-network/domain-payload-plugin";
|
|
|
26
32
|
import { SchemaSchema } from "@xyo-network/schema-payload-plugin";
|
|
27
33
|
import Ajv from "ajv";
|
|
28
34
|
import { LRUCache } from "lru-cache";
|
|
29
|
-
var getSchemaNameFromSchema = (schema) => {
|
|
35
|
+
var getSchemaNameFromSchema = /* @__PURE__ */ __name((schema) => {
|
|
30
36
|
if (schema.$id) {
|
|
31
37
|
return schema.$id;
|
|
32
38
|
}
|
|
33
|
-
};
|
|
39
|
+
}, "getSchemaNameFromSchema");
|
|
34
40
|
var SchemaCache = class _SchemaCache {
|
|
41
|
+
static {
|
|
42
|
+
__name(this, "SchemaCache");
|
|
43
|
+
}
|
|
35
44
|
/**
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
static NULL = {
|
|
45
|
+
* Object representing `null` since LRU Cache types
|
|
46
|
+
* only allow for types that derive from object
|
|
47
|
+
*/
|
|
48
|
+
static NULL = {
|
|
49
|
+
payload: {
|
|
50
|
+
definition: {},
|
|
51
|
+
schema: SchemaSchema
|
|
52
|
+
}
|
|
53
|
+
};
|
|
40
54
|
static _instance;
|
|
41
55
|
onSchemaCached;
|
|
42
56
|
proxy;
|
|
43
|
-
_cache = new LRUCache({
|
|
57
|
+
_cache = new LRUCache({
|
|
58
|
+
max: 500,
|
|
59
|
+
ttl: 1e3 * 60 * 5
|
|
60
|
+
});
|
|
44
61
|
_validators = {};
|
|
45
62
|
//prevents double discovery
|
|
46
63
|
getDebounce = new Debounce();
|
|
@@ -54,10 +71,10 @@ var SchemaCache = class _SchemaCache {
|
|
|
54
71
|
return this._instance;
|
|
55
72
|
}
|
|
56
73
|
/**
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
74
|
+
* A map of cached schema (by name) to payload validators for the schema. A schema
|
|
75
|
+
* must be cached via `get('schema.name')` before it's validator can be used as
|
|
76
|
+
* they are compiled dynamically at runtime upon retrieval.
|
|
77
|
+
*/
|
|
61
78
|
get validators() {
|
|
62
79
|
return this._validators;
|
|
63
80
|
}
|
|
@@ -75,7 +92,9 @@ var SchemaCache = class _SchemaCache {
|
|
|
75
92
|
}
|
|
76
93
|
cacheSchemaIfValid(entry) {
|
|
77
94
|
if (entry.payload.definition) {
|
|
78
|
-
const ajv = new Ajv({
|
|
95
|
+
const ajv = new Ajv({
|
|
96
|
+
strict: false
|
|
97
|
+
});
|
|
79
98
|
const validator = ajv.compile(entry.payload.definition);
|
|
80
99
|
const schemaName = getSchemaNameFromSchema(entry.payload.definition);
|
|
81
100
|
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'\nimport { SchemaNameToValidatorMap } from './SchemaNameToValidatorMap'\n\nconst getSchemaNameFromSchema = (schema: SchemaObject) => {\n if (schema.$id) {\n return schema.$id\n }\n}\n\nexport type SchemaCacheEntry = FetchedPayload<SchemaPayload>\n\nexport class SchemaCache<T extends SchemaNameToValidatorMap = SchemaNameToValidatorMap> {\n /**\n * Object representing `null` since LRU Cache types\n * only allow for types that derive from object\n */\n protected static readonly NULL: SchemaCacheEntry = { payload: { definition: {}, schema: SchemaSchema } }\n\n private static _instance?: SchemaCache\n\n onSchemaCached?: (name: string, entry: SchemaCacheEntry) => void\n proxy?: string\n\n private _cache = new LRUCache<string, SchemaCacheEntry>({ max: 500, ttl: 1000 * 60 * 5 })\n private _validators: T = {} as T\n\n //prevents double discovery\n private getDebounce = new Debounce()\n\n private constructor(proxy?: string) {\n this.proxy = proxy\n }\n\n static get instance() {\n if (!this._instance) {\n this._instance = new SchemaCache()\n }\n return this._instance\n }\n\n /**\n * A map of cached schema (by name) to payload validators for the schema. A schema\n * must be cached via `get('schema.name')` before it's validator can be used as\n * they are compiled dynamically at runtime upon retrieval.\n */\n get validators(): T {\n return this._validators\n }\n\n async get(schema?: string): Promise<SchemaCacheEntry | undefined | null> {\n if (schema) {\n await this.getDebounce.one(schema, async () => {\n // If we've never looked for it before, it will be undefined\n if (this._cache.get(schema) === undefined) {\n await this.fetchSchema(schema)\n }\n })\n const value = this._cache.get(schema)\n return value === SchemaCache.NULL ? null : value\n }\n return undefined\n }\n\n private cacheSchemaIfValid(entry: SchemaCacheEntry) {\n //only store them if they match the schema root\n if (entry.payload.definition) {\n const ajv = new Ajv({ strict: false })\n //check if it is a valid schema def\n const validator = ajv.compile(entry.payload.definition)\n const schemaName = getSchemaNameFromSchema(entry.payload.definition)\n if (schemaName) {\n this._cache.set(schemaName, entry)\n const key = schemaName as keyof T\n this._validators[key] = validator as unknown as T[keyof T]\n this.onSchemaCached?.(schemaName, entry)\n }\n }\n }\n\n private cacheSchemas(aliasEntries?: FetchedPayload[] | null) {\n for (const entry of aliasEntries?.filter((entry) => entry.payload.schema === SchemaSchema) ?? []) {\n this.cacheSchemaIfValid(entry as SchemaCacheEntry)\n }\n }\n\n private async fetchSchema(schema: string) {\n try {\n const domain = await DomainPayloadWrapper.discover(schema, this.proxy)\n await domain?.fetch()\n this.cacheSchemas(domain?.aliases)\n\n //if it is still undefined, mark it as null (not found)\n if (this._cache.get(schema) === undefined) {\n this._cache.set(schema, SchemaCache.NULL)\n }\n } catch (error) {\n //if failed, set it to NULL, TODO: Make an entry for an error to try again in the future?\n this._cache.set(schema, SchemaCache.NULL)\n if (isAxiosError(error)) {\n console.log(`Axios Url: ${error.response?.config.url}`)\n }\n handleError(error, (error) => {\n console.error(`fetchSchema threw: ${error.message}`)\n })\n }\n }\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"sources":["../../src/Debounce.ts","../../src/SchemaCache.ts"],"sourcesContent":["import { delay } from '@xylabs/delay'\n\nexport class Debounce<TKey = string> {\n private map = new Map<TKey, number>()\n\n async one<T>(key: TKey, closure: () => Promise<T>, timeout = 10_000) {\n const startTime = Date.now()\n while (this.map.get(key)) {\n await delay(100)\n if (Date.now() - startTime > timeout) {\n throw new Error(`Debounce timed out [${key}]`)\n }\n }\n try {\n this.map.set(key, 1)\n return await closure()\n } finally {\n this.map.set(key, 0)\n }\n }\n}\n","import { isAxiosError } from '@xylabs/axios'\nimport { handleError } from '@xylabs/error'\nimport { DomainPayloadWrapper } from '@xyo-network/domain-payload-plugin'\nimport { FetchedPayload } from '@xyo-network/huri'\nimport { SchemaPayload, SchemaSchema } from '@xyo-network/schema-payload-plugin'\nimport Ajv, { SchemaObject } from 'ajv'\nimport { LRUCache } from 'lru-cache'\n\nimport { Debounce } from './Debounce'\nimport { SchemaNameToValidatorMap } from './SchemaNameToValidatorMap'\n\nconst getSchemaNameFromSchema = (schema: SchemaObject) => {\n if (schema.$id) {\n return schema.$id\n }\n}\n\nexport type SchemaCacheEntry = FetchedPayload<SchemaPayload>\n\nexport class SchemaCache<T extends SchemaNameToValidatorMap = SchemaNameToValidatorMap> {\n /**\n * Object representing `null` since LRU Cache types\n * only allow for types that derive from object\n */\n protected static readonly NULL: SchemaCacheEntry = { payload: { definition: {}, schema: SchemaSchema } }\n\n private static _instance?: SchemaCache\n\n onSchemaCached?: (name: string, entry: SchemaCacheEntry) => void\n proxy?: string\n\n private _cache = new LRUCache<string, SchemaCacheEntry>({ max: 500, ttl: 1000 * 60 * 5 })\n private _validators: T = {} as T\n\n //prevents double discovery\n private getDebounce = new Debounce()\n\n private constructor(proxy?: string) {\n this.proxy = proxy\n }\n\n static get instance() {\n if (!this._instance) {\n this._instance = new SchemaCache()\n }\n return this._instance\n }\n\n /**\n * A map of cached schema (by name) to payload validators for the schema. A schema\n * must be cached via `get('schema.name')` before it's validator can be used as\n * they are compiled dynamically at runtime upon retrieval.\n */\n get validators(): T {\n return this._validators\n }\n\n async get(schema?: string): Promise<SchemaCacheEntry | undefined | null> {\n if (schema) {\n await this.getDebounce.one(schema, async () => {\n // If we've never looked for it before, it will be undefined\n if (this._cache.get(schema) === undefined) {\n await this.fetchSchema(schema)\n }\n })\n const value = this._cache.get(schema)\n return value === SchemaCache.NULL ? null : value\n }\n return undefined\n }\n\n private cacheSchemaIfValid(entry: SchemaCacheEntry) {\n //only store them if they match the schema root\n if (entry.payload.definition) {\n const ajv = new Ajv({ strict: false })\n //check if it is a valid schema def\n const validator = ajv.compile(entry.payload.definition)\n const schemaName = getSchemaNameFromSchema(entry.payload.definition)\n if (schemaName) {\n this._cache.set(schemaName, entry)\n const key = schemaName as keyof T\n this._validators[key] = validator as unknown as T[keyof T]\n this.onSchemaCached?.(schemaName, entry)\n }\n }\n }\n\n private cacheSchemas(aliasEntries?: FetchedPayload[] | null) {\n for (const entry of aliasEntries?.filter((entry) => entry.payload.schema === SchemaSchema) ?? []) {\n this.cacheSchemaIfValid(entry as SchemaCacheEntry)\n }\n }\n\n private async fetchSchema(schema: string) {\n try {\n const domain = await DomainPayloadWrapper.discover(schema, this.proxy)\n await domain?.fetch()\n this.cacheSchemas(domain?.aliases)\n\n //if it is still undefined, mark it as null (not found)\n if (this._cache.get(schema) === undefined) {\n this._cache.set(schema, SchemaCache.NULL)\n }\n } catch (error) {\n //if failed, set it to NULL, TODO: Make an entry for an error to try again in the future?\n this._cache.set(schema, SchemaCache.NULL)\n if (isAxiosError(error)) {\n console.log(`Axios Url: ${error.response?.config.url}`)\n }\n handleError(error, (error) => {\n console.error(`fetchSchema threw: ${error.message}`)\n })\n }\n }\n}\n"],"mappings":";;;;AAAA,SAASA,aAAa;AAEf,IAAMC,WAAN,MAAMA;EAFb,OAEaA;;;EACHC,MAAM,oBAAIC,IAAAA;EAElB,MAAMC,IAAOC,KAAWC,SA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AAK;MACxD;AACAC,kBAAYP,OAAO,CAACA,WAAAA;AAClBE,gBAAQF,MAAM,sBAAsBA,OAAMQ,OAAO,EAAE;MACrD,CAAA;IACF;EACF;AACF;","names":["delay","Debounce","map","Map","one","key","closure","timeout","startTime","Date","now","get","delay","Error","set","isAxiosError","handleError","DomainPayloadWrapper","SchemaSchema","Ajv","LRUCache","getSchemaNameFromSchema","schema","$id","SchemaCache","NULL","payload","definition","SchemaSchema","_instance","onSchemaCached","proxy","_cache","LRUCache","max","ttl","_validators","getDebounce","Debounce","instance","validators","get","one","undefined","fetchSchema","value","cacheSchemaIfValid","entry","ajv","Ajv","strict","validator","compile","schemaName","set","key","cacheSchemas","aliasEntries","filter","domain","DomainPayloadWrapper","discover","fetch","aliases","error","isAxiosError","console","log","response","config","url","handleError","message"]}
|
package/dist/node/index.cjs
CHANGED
|
@@ -5,6 +5,8 @@ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
|
5
5
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
6
|
var __getProtoOf = Object.getPrototypeOf;
|
|
7
7
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
9
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
8
10
|
var __export = (target, all) => {
|
|
9
11
|
for (var name in all)
|
|
10
12
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
@@ -26,6 +28,10 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
26
28
|
mod
|
|
27
29
|
));
|
|
28
30
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
31
|
+
var __publicField = (obj, key, value) => {
|
|
32
|
+
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
33
|
+
return value;
|
|
34
|
+
};
|
|
29
35
|
|
|
30
36
|
// src/index.ts
|
|
31
37
|
var src_exports = {};
|
|
@@ -37,7 +43,7 @@ module.exports = __toCommonJS(src_exports);
|
|
|
37
43
|
|
|
38
44
|
// src/Debounce.ts
|
|
39
45
|
var import_delay = require("@xylabs/delay");
|
|
40
|
-
var
|
|
46
|
+
var _Debounce = class _Debounce {
|
|
41
47
|
map = /* @__PURE__ */ new Map();
|
|
42
48
|
async one(key, closure, timeout = 1e4) {
|
|
43
49
|
const startTime = Date.now();
|
|
@@ -55,6 +61,8 @@ var Debounce = class {
|
|
|
55
61
|
}
|
|
56
62
|
}
|
|
57
63
|
};
|
|
64
|
+
__name(_Debounce, "Debounce");
|
|
65
|
+
var Debounce = _Debounce;
|
|
58
66
|
|
|
59
67
|
// src/SchemaCache.ts
|
|
60
68
|
var import_axios = require("@xylabs/axios");
|
|
@@ -63,21 +71,18 @@ var import_domain_payload_plugin = require("@xyo-network/domain-payload-plugin")
|
|
|
63
71
|
var import_schema_payload_plugin = require("@xyo-network/schema-payload-plugin");
|
|
64
72
|
var import_ajv = __toESM(require("ajv"), 1);
|
|
65
73
|
var import_lru_cache = require("lru-cache");
|
|
66
|
-
var getSchemaNameFromSchema = (schema) => {
|
|
74
|
+
var getSchemaNameFromSchema = /* @__PURE__ */ __name((schema) => {
|
|
67
75
|
if (schema.$id) {
|
|
68
76
|
return schema.$id;
|
|
69
77
|
}
|
|
70
|
-
};
|
|
71
|
-
var
|
|
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
|
+
}, "getSchemaNameFromSchema");
|
|
79
|
+
var _SchemaCache = class _SchemaCache {
|
|
78
80
|
onSchemaCached;
|
|
79
81
|
proxy;
|
|
80
|
-
_cache = new import_lru_cache.LRUCache({
|
|
82
|
+
_cache = new import_lru_cache.LRUCache({
|
|
83
|
+
max: 500,
|
|
84
|
+
ttl: 1e3 * 60 * 5
|
|
85
|
+
});
|
|
81
86
|
_validators = {};
|
|
82
87
|
//prevents double discovery
|
|
83
88
|
getDebounce = new Debounce();
|
|
@@ -91,10 +96,10 @@ var SchemaCache = class _SchemaCache {
|
|
|
91
96
|
return this._instance;
|
|
92
97
|
}
|
|
93
98
|
/**
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
99
|
+
* A map of cached schema (by name) to payload validators for the schema. A schema
|
|
100
|
+
* must be cached via `get('schema.name')` before it's validator can be used as
|
|
101
|
+
* they are compiled dynamically at runtime upon retrieval.
|
|
102
|
+
*/
|
|
98
103
|
get validators() {
|
|
99
104
|
return this._validators;
|
|
100
105
|
}
|
|
@@ -113,7 +118,9 @@ var SchemaCache = class _SchemaCache {
|
|
|
113
118
|
cacheSchemaIfValid(entry) {
|
|
114
119
|
var _a;
|
|
115
120
|
if (entry.payload.definition) {
|
|
116
|
-
const ajv = new import_ajv.default({
|
|
121
|
+
const ajv = new import_ajv.default({
|
|
122
|
+
strict: false
|
|
123
|
+
});
|
|
117
124
|
const validator = ajv.compile(entry.payload.definition);
|
|
118
125
|
const schemaName = getSchemaNameFromSchema(entry.payload.definition);
|
|
119
126
|
if (schemaName) {
|
|
@@ -149,6 +156,19 @@ var SchemaCache = class _SchemaCache {
|
|
|
149
156
|
}
|
|
150
157
|
}
|
|
151
158
|
};
|
|
159
|
+
__name(_SchemaCache, "SchemaCache");
|
|
160
|
+
/**
|
|
161
|
+
* Object representing `null` since LRU Cache types
|
|
162
|
+
* only allow for types that derive from object
|
|
163
|
+
*/
|
|
164
|
+
__publicField(_SchemaCache, "NULL", {
|
|
165
|
+
payload: {
|
|
166
|
+
definition: {},
|
|
167
|
+
schema: import_schema_payload_plugin.SchemaSchema
|
|
168
|
+
}
|
|
169
|
+
});
|
|
170
|
+
__publicField(_SchemaCache, "_instance");
|
|
171
|
+
var SchemaCache = _SchemaCache;
|
|
152
172
|
// Annotate the CommonJS export names for ESM import in node:
|
|
153
173
|
0 && (module.exports = {
|
|
154
174
|
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'\nexport * from './SchemaCache'\nexport * from './SchemaNameToValidatorMap'\n","import { delay } from '@xylabs/delay'\n\nexport class Debounce<TKey = string> {\n private map = new Map<TKey, number>()\n\n async one<T>(key: TKey, closure: () => Promise<T>, timeout = 10_000) {\n const startTime = Date.now()\n while (this.map.get(key)) {\n await delay(100)\n if (Date.now() - startTime > timeout) {\n throw new Error(`Debounce timed out [${key}]`)\n }\n }\n try {\n this.map.set(key, 1)\n return await closure()\n } finally {\n this.map.set(key, 0)\n }\n }\n}\n","import { isAxiosError } from '@xylabs/axios'\nimport { handleError } from '@xylabs/error'\nimport { DomainPayloadWrapper } from '@xyo-network/domain-payload-plugin'\nimport { FetchedPayload } from '@xyo-network/huri'\nimport { SchemaPayload, SchemaSchema } from '@xyo-network/schema-payload-plugin'\nimport Ajv, { SchemaObject } from 'ajv'\nimport { LRUCache } from 'lru-cache'\n\nimport { Debounce } from './Debounce'\nimport { SchemaNameToValidatorMap } from './SchemaNameToValidatorMap'\n\nconst getSchemaNameFromSchema = (schema: SchemaObject) => {\n if (schema.$id) {\n return schema.$id\n }\n}\n\nexport type SchemaCacheEntry = FetchedPayload<SchemaPayload>\n\nexport class SchemaCache<T extends SchemaNameToValidatorMap = SchemaNameToValidatorMap> {\n /**\n * Object representing `null` since LRU Cache types\n * only allow for types that derive from object\n */\n protected static readonly NULL: SchemaCacheEntry = { payload: { definition: {}, schema: SchemaSchema } }\n\n private static _instance?: SchemaCache\n\n onSchemaCached?: (name: string, entry: SchemaCacheEntry) => void\n proxy?: string\n\n private _cache = new LRUCache<string, SchemaCacheEntry>({ max: 500, ttl: 1000 * 60 * 5 })\n private _validators: T = {} as T\n\n //prevents double discovery\n private getDebounce = new Debounce()\n\n private constructor(proxy?: string) {\n this.proxy = proxy\n }\n\n static get instance() {\n if (!this._instance) {\n this._instance = new SchemaCache()\n }\n return this._instance\n }\n\n /**\n * A map of cached schema (by name) to payload validators for the schema. A schema\n * must be cached via `get('schema.name')` before it's validator can be used as\n * they are compiled dynamically at runtime upon retrieval.\n */\n get validators(): T {\n return this._validators\n }\n\n async get(schema?: string): Promise<SchemaCacheEntry | undefined | null> {\n if (schema) {\n await this.getDebounce.one(schema, async () => {\n // If we've never looked for it before, it will be undefined\n if (this._cache.get(schema) === undefined) {\n await this.fetchSchema(schema)\n }\n })\n const value = this._cache.get(schema)\n return value === SchemaCache.NULL ? null : value\n }\n return undefined\n }\n\n private cacheSchemaIfValid(entry: SchemaCacheEntry) {\n //only store them if they match the schema root\n if (entry.payload.definition) {\n const ajv = new Ajv({ strict: false })\n //check if it is a valid schema def\n const validator = ajv.compile(entry.payload.definition)\n const schemaName = getSchemaNameFromSchema(entry.payload.definition)\n if (schemaName) {\n this._cache.set(schemaName, entry)\n const key = schemaName as keyof T\n this._validators[key] = validator as unknown as T[keyof T]\n this.onSchemaCached?.(schemaName, entry)\n }\n }\n }\n\n private cacheSchemas(aliasEntries?: FetchedPayload[] | null) {\n for (const entry of aliasEntries?.filter((entry) => entry.payload.schema === SchemaSchema) ?? []) {\n this.cacheSchemaIfValid(entry as SchemaCacheEntry)\n }\n }\n\n private async fetchSchema(schema: string) {\n try {\n const domain = await DomainPayloadWrapper.discover(schema, this.proxy)\n await domain?.fetch()\n this.cacheSchemas(domain?.aliases)\n\n //if it is still undefined, mark it as null (not found)\n if (this._cache.get(schema) === undefined) {\n this._cache.set(schema, SchemaCache.NULL)\n }\n } catch (error) {\n //if failed, set it to NULL, TODO: Make an entry for an error to try again in the future?\n this._cache.set(schema, SchemaCache.NULL)\n if (isAxiosError(error)) {\n console.log(`Axios Url: ${error.response?.config.url}`)\n }\n handleError(error, (error) => {\n console.error(`fetchSchema threw: ${error.message}`)\n })\n }\n }\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"sources":["../../src/index.ts","../../src/Debounce.ts","../../src/SchemaCache.ts"],"sourcesContent":["export * from './Debounce'\nexport * from './SchemaCache'\nexport * from './SchemaNameToValidatorMap'\n","import { delay } from '@xylabs/delay'\n\nexport class Debounce<TKey = string> {\n private map = new Map<TKey, number>()\n\n async one<T>(key: TKey, closure: () => Promise<T>, timeout = 10_000) {\n const startTime = Date.now()\n while (this.map.get(key)) {\n await delay(100)\n if (Date.now() - startTime > timeout) {\n throw new Error(`Debounce timed out [${key}]`)\n }\n }\n try {\n this.map.set(key, 1)\n return await closure()\n } finally {\n this.map.set(key, 0)\n }\n }\n}\n","import { isAxiosError } from '@xylabs/axios'\nimport { handleError } from '@xylabs/error'\nimport { DomainPayloadWrapper } from '@xyo-network/domain-payload-plugin'\nimport { FetchedPayload } from '@xyo-network/huri'\nimport { SchemaPayload, SchemaSchema } from '@xyo-network/schema-payload-plugin'\nimport Ajv, { SchemaObject } from 'ajv'\nimport { LRUCache } from 'lru-cache'\n\nimport { Debounce } from './Debounce'\nimport { SchemaNameToValidatorMap } from './SchemaNameToValidatorMap'\n\nconst getSchemaNameFromSchema = (schema: SchemaObject) => {\n if (schema.$id) {\n return schema.$id\n }\n}\n\nexport type SchemaCacheEntry = FetchedPayload<SchemaPayload>\n\nexport class SchemaCache<T extends SchemaNameToValidatorMap = SchemaNameToValidatorMap> {\n /**\n * Object representing `null` since LRU Cache types\n * only allow for types that derive from object\n */\n protected static readonly NULL: SchemaCacheEntry = { payload: { definition: {}, schema: SchemaSchema } }\n\n private static _instance?: SchemaCache\n\n onSchemaCached?: (name: string, entry: SchemaCacheEntry) => void\n proxy?: string\n\n private _cache = new LRUCache<string, SchemaCacheEntry>({ max: 500, ttl: 1000 * 60 * 5 })\n private _validators: T = {} as T\n\n //prevents double discovery\n private getDebounce = new Debounce()\n\n private constructor(proxy?: string) {\n this.proxy = proxy\n }\n\n static get instance() {\n if (!this._instance) {\n this._instance = new SchemaCache()\n }\n return this._instance\n }\n\n /**\n * A map of cached schema (by name) to payload validators for the schema. A schema\n * must be cached via `get('schema.name')` before it's validator can be used as\n * they are compiled dynamically at runtime upon retrieval.\n */\n get validators(): T {\n return this._validators\n }\n\n async get(schema?: string): Promise<SchemaCacheEntry | undefined | null> {\n if (schema) {\n await this.getDebounce.one(schema, async () => {\n // If we've never looked for it before, it will be undefined\n if (this._cache.get(schema) === undefined) {\n await this.fetchSchema(schema)\n }\n })\n const value = this._cache.get(schema)\n return value === SchemaCache.NULL ? null : value\n }\n return undefined\n }\n\n private cacheSchemaIfValid(entry: SchemaCacheEntry) {\n //only store them if they match the schema root\n if (entry.payload.definition) {\n const ajv = new Ajv({ strict: false })\n //check if it is a valid schema def\n const validator = ajv.compile(entry.payload.definition)\n const schemaName = getSchemaNameFromSchema(entry.payload.definition)\n if (schemaName) {\n this._cache.set(schemaName, entry)\n const key = schemaName as keyof T\n this._validators[key] = validator as unknown as T[keyof T]\n this.onSchemaCached?.(schemaName, entry)\n }\n }\n }\n\n private cacheSchemas(aliasEntries?: FetchedPayload[] | null) {\n for (const entry of aliasEntries?.filter((entry) => entry.payload.schema === SchemaSchema) ?? []) {\n this.cacheSchemaIfValid(entry as SchemaCacheEntry)\n }\n }\n\n private async fetchSchema(schema: string) {\n try {\n const domain = await DomainPayloadWrapper.discover(schema, this.proxy)\n await domain?.fetch()\n this.cacheSchemas(domain?.aliases)\n\n //if it is still undefined, mark it as null (not found)\n if (this._cache.get(schema) === undefined) {\n this._cache.set(schema, SchemaCache.NULL)\n }\n } catch (error) {\n //if failed, set it to NULL, TODO: Make an entry for an error to try again in the future?\n this._cache.set(schema, SchemaCache.NULL)\n if (isAxiosError(error)) {\n console.log(`Axios Url: ${error.response?.config.url}`)\n }\n handleError(error, (error) => {\n console.error(`fetchSchema threw: ${error.message}`)\n })\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;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AAMI,aAANJ,mBAAgBK,OAAOC,GAAAA,EAAK;MACxD;AACAC,oCAAYP,OAAO,CAACA,WAAAA;AAClBE,gBAAQF,MAAM,sBAAsBA,OAAMQ,OAAO,EAAE;MACrD,CAAA;IACF;EACF;AACF;AA/FahD;;;;;AAKX,cALWA,cAKekB,QAAyB;EAAEG,SAAS;IAAEC,YAAY,CAAC;IAAGxB,QAAQoC;EAAa;AAAE;AAEvG,cAPWlC,cAOIW;AAPV,IAAMX,cAAN;","names":["Debounce","map","Map","one","key","closure","timeout","startTime","Date","now","get","delay","Error","set","getSchemaNameFromSchema","schema","$id","SchemaCache","onSchemaCached","proxy","_cache","LRUCache","max","ttl","_validators","getDebounce","Debounce","instance","_instance","validators","get","one","undefined","fetchSchema","value","NULL","cacheSchemaIfValid","entry","payload","definition","ajv","Ajv","strict","validator","compile","schemaName","set","key","cacheSchemas","aliasEntries","filter","SchemaSchema","domain","DomainPayloadWrapper","discover","fetch","aliases","error","isAxiosError","console","log","response","config","url","handleError","message"]}
|
package/dist/node/index.js
CHANGED
|
@@ -1,6 +1,14 @@
|
|
|
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) => {
|
|
5
|
+
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
6
|
+
return value;
|
|
7
|
+
};
|
|
8
|
+
|
|
1
9
|
// src/Debounce.ts
|
|
2
10
|
import { delay } from "@xylabs/delay";
|
|
3
|
-
var
|
|
11
|
+
var _Debounce = class _Debounce {
|
|
4
12
|
map = /* @__PURE__ */ new Map();
|
|
5
13
|
async one(key, closure, timeout = 1e4) {
|
|
6
14
|
const startTime = Date.now();
|
|
@@ -18,6 +26,8 @@ var Debounce = class {
|
|
|
18
26
|
}
|
|
19
27
|
}
|
|
20
28
|
};
|
|
29
|
+
__name(_Debounce, "Debounce");
|
|
30
|
+
var Debounce = _Debounce;
|
|
21
31
|
|
|
22
32
|
// src/SchemaCache.ts
|
|
23
33
|
import { isAxiosError } from "@xylabs/axios";
|
|
@@ -26,21 +36,18 @@ import { DomainPayloadWrapper } from "@xyo-network/domain-payload-plugin";
|
|
|
26
36
|
import { SchemaSchema } from "@xyo-network/schema-payload-plugin";
|
|
27
37
|
import Ajv from "ajv";
|
|
28
38
|
import { LRUCache } from "lru-cache";
|
|
29
|
-
var getSchemaNameFromSchema = (schema) => {
|
|
39
|
+
var getSchemaNameFromSchema = /* @__PURE__ */ __name((schema) => {
|
|
30
40
|
if (schema.$id) {
|
|
31
41
|
return schema.$id;
|
|
32
42
|
}
|
|
33
|
-
};
|
|
34
|
-
var
|
|
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;
|
|
43
|
+
}, "getSchemaNameFromSchema");
|
|
44
|
+
var _SchemaCache = class _SchemaCache {
|
|
41
45
|
onSchemaCached;
|
|
42
46
|
proxy;
|
|
43
|
-
_cache = new LRUCache({
|
|
47
|
+
_cache = new LRUCache({
|
|
48
|
+
max: 500,
|
|
49
|
+
ttl: 1e3 * 60 * 5
|
|
50
|
+
});
|
|
44
51
|
_validators = {};
|
|
45
52
|
//prevents double discovery
|
|
46
53
|
getDebounce = new Debounce();
|
|
@@ -54,10 +61,10 @@ var SchemaCache = class _SchemaCache {
|
|
|
54
61
|
return this._instance;
|
|
55
62
|
}
|
|
56
63
|
/**
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
64
|
+
* A map of cached schema (by name) to payload validators for the schema. A schema
|
|
65
|
+
* must be cached via `get('schema.name')` before it's validator can be used as
|
|
66
|
+
* they are compiled dynamically at runtime upon retrieval.
|
|
67
|
+
*/
|
|
61
68
|
get validators() {
|
|
62
69
|
return this._validators;
|
|
63
70
|
}
|
|
@@ -76,7 +83,9 @@ var SchemaCache = class _SchemaCache {
|
|
|
76
83
|
cacheSchemaIfValid(entry) {
|
|
77
84
|
var _a;
|
|
78
85
|
if (entry.payload.definition) {
|
|
79
|
-
const ajv = new Ajv({
|
|
86
|
+
const ajv = new Ajv({
|
|
87
|
+
strict: false
|
|
88
|
+
});
|
|
80
89
|
const validator = ajv.compile(entry.payload.definition);
|
|
81
90
|
const schemaName = getSchemaNameFromSchema(entry.payload.definition);
|
|
82
91
|
if (schemaName) {
|
|
@@ -112,6 +121,19 @@ var SchemaCache = class _SchemaCache {
|
|
|
112
121
|
}
|
|
113
122
|
}
|
|
114
123
|
};
|
|
124
|
+
__name(_SchemaCache, "SchemaCache");
|
|
125
|
+
/**
|
|
126
|
+
* Object representing `null` since LRU Cache types
|
|
127
|
+
* only allow for types that derive from object
|
|
128
|
+
*/
|
|
129
|
+
__publicField(_SchemaCache, "NULL", {
|
|
130
|
+
payload: {
|
|
131
|
+
definition: {},
|
|
132
|
+
schema: SchemaSchema
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
__publicField(_SchemaCache, "_instance");
|
|
136
|
+
var SchemaCache = _SchemaCache;
|
|
115
137
|
export {
|
|
116
138
|
Debounce,
|
|
117
139
|
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'\nimport { SchemaNameToValidatorMap } from './SchemaNameToValidatorMap'\n\nconst getSchemaNameFromSchema = (schema: SchemaObject) => {\n if (schema.$id) {\n return schema.$id\n }\n}\n\nexport type SchemaCacheEntry = FetchedPayload<SchemaPayload>\n\nexport class SchemaCache<T extends SchemaNameToValidatorMap = SchemaNameToValidatorMap> {\n /**\n * Object representing `null` since LRU Cache types\n * only allow for types that derive from object\n */\n protected static readonly NULL: SchemaCacheEntry = { payload: { definition: {}, schema: SchemaSchema } }\n\n private static _instance?: SchemaCache\n\n onSchemaCached?: (name: string, entry: SchemaCacheEntry) => void\n proxy?: string\n\n private _cache = new LRUCache<string, SchemaCacheEntry>({ max: 500, ttl: 1000 * 60 * 5 })\n private _validators: T = {} as T\n\n //prevents double discovery\n private getDebounce = new Debounce()\n\n private constructor(proxy?: string) {\n this.proxy = proxy\n }\n\n static get instance() {\n if (!this._instance) {\n this._instance = new SchemaCache()\n }\n return this._instance\n }\n\n /**\n * A map of cached schema (by name) to payload validators for the schema. A schema\n * must be cached via `get('schema.name')` before it's validator can be used as\n * they are compiled dynamically at runtime upon retrieval.\n */\n get validators(): T {\n return this._validators\n }\n\n async get(schema?: string): Promise<SchemaCacheEntry | undefined | null> {\n if (schema) {\n await this.getDebounce.one(schema, async () => {\n // If we've never looked for it before, it will be undefined\n if (this._cache.get(schema) === undefined) {\n await this.fetchSchema(schema)\n }\n })\n const value = this._cache.get(schema)\n return value === SchemaCache.NULL ? null : value\n }\n return undefined\n }\n\n private cacheSchemaIfValid(entry: SchemaCacheEntry) {\n //only store them if they match the schema root\n if (entry.payload.definition) {\n const ajv = new Ajv({ strict: false })\n //check if it is a valid schema def\n const validator = ajv.compile(entry.payload.definition)\n const schemaName = getSchemaNameFromSchema(entry.payload.definition)\n if (schemaName) {\n this._cache.set(schemaName, entry)\n const key = schemaName as keyof T\n this._validators[key] = validator as unknown as T[keyof T]\n this.onSchemaCached?.(schemaName, entry)\n }\n }\n }\n\n private cacheSchemas(aliasEntries?: FetchedPayload[] | null) {\n for (const entry of aliasEntries?.filter((entry) => entry.payload.schema === SchemaSchema) ?? []) {\n this.cacheSchemaIfValid(entry as SchemaCacheEntry)\n }\n }\n\n private async fetchSchema(schema: string) {\n try {\n const domain = await DomainPayloadWrapper.discover(schema, this.proxy)\n await domain?.fetch()\n this.cacheSchemas(domain?.aliases)\n\n //if it is still undefined, mark it as null (not found)\n if (this._cache.get(schema) === undefined) {\n this._cache.set(schema, SchemaCache.NULL)\n }\n } catch (error) {\n //if failed, set it to NULL, TODO: Make an entry for an error to try again in the future?\n this._cache.set(schema, SchemaCache.NULL)\n if (isAxiosError(error)) {\n console.log(`Axios Url: ${error.response?.config.url}`)\n }\n handleError(error, (error) => {\n console.error(`fetchSchema threw: ${error.message}`)\n })\n }\n }\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"sources":["../../src/Debounce.ts","../../src/SchemaCache.ts"],"sourcesContent":["import { delay } from '@xylabs/delay'\n\nexport class Debounce<TKey = string> {\n private map = new Map<TKey, number>()\n\n async one<T>(key: TKey, closure: () => Promise<T>, timeout = 10_000) {\n const startTime = Date.now()\n while (this.map.get(key)) {\n await delay(100)\n if (Date.now() - startTime > timeout) {\n throw new Error(`Debounce timed out [${key}]`)\n }\n }\n try {\n this.map.set(key, 1)\n return await closure()\n } finally {\n this.map.set(key, 0)\n }\n }\n}\n","import { isAxiosError } from '@xylabs/axios'\nimport { handleError } from '@xylabs/error'\nimport { DomainPayloadWrapper } from '@xyo-network/domain-payload-plugin'\nimport { FetchedPayload } from '@xyo-network/huri'\nimport { SchemaPayload, SchemaSchema } from '@xyo-network/schema-payload-plugin'\nimport Ajv, { SchemaObject } from 'ajv'\nimport { LRUCache } from 'lru-cache'\n\nimport { Debounce } from './Debounce'\nimport { SchemaNameToValidatorMap } from './SchemaNameToValidatorMap'\n\nconst getSchemaNameFromSchema = (schema: SchemaObject) => {\n if (schema.$id) {\n return schema.$id\n }\n}\n\nexport type SchemaCacheEntry = FetchedPayload<SchemaPayload>\n\nexport class SchemaCache<T extends SchemaNameToValidatorMap = SchemaNameToValidatorMap> {\n /**\n * Object representing `null` since LRU Cache types\n * only allow for types that derive from object\n */\n protected static readonly NULL: SchemaCacheEntry = { payload: { definition: {}, schema: SchemaSchema } }\n\n private static _instance?: SchemaCache\n\n onSchemaCached?: (name: string, entry: SchemaCacheEntry) => void\n proxy?: string\n\n private _cache = new LRUCache<string, SchemaCacheEntry>({ max: 500, ttl: 1000 * 60 * 5 })\n private _validators: T = {} as T\n\n //prevents double discovery\n private getDebounce = new Debounce()\n\n private constructor(proxy?: string) {\n this.proxy = proxy\n }\n\n static get instance() {\n if (!this._instance) {\n this._instance = new SchemaCache()\n }\n return this._instance\n }\n\n /**\n * A map of cached schema (by name) to payload validators for the schema. A schema\n * must be cached via `get('schema.name')` before it's validator can be used as\n * they are compiled dynamically at runtime upon retrieval.\n */\n get validators(): T {\n return this._validators\n }\n\n async get(schema?: string): Promise<SchemaCacheEntry | undefined | null> {\n if (schema) {\n await this.getDebounce.one(schema, async () => {\n // If we've never looked for it before, it will be undefined\n if (this._cache.get(schema) === undefined) {\n await this.fetchSchema(schema)\n }\n })\n const value = this._cache.get(schema)\n return value === SchemaCache.NULL ? null : value\n }\n return undefined\n }\n\n private cacheSchemaIfValid(entry: SchemaCacheEntry) {\n //only store them if they match the schema root\n if (entry.payload.definition) {\n const ajv = new Ajv({ strict: false })\n //check if it is a valid schema def\n const validator = ajv.compile(entry.payload.definition)\n const schemaName = getSchemaNameFromSchema(entry.payload.definition)\n if (schemaName) {\n this._cache.set(schemaName, entry)\n const key = schemaName as keyof T\n this._validators[key] = validator as unknown as T[keyof T]\n this.onSchemaCached?.(schemaName, entry)\n }\n }\n }\n\n private cacheSchemas(aliasEntries?: FetchedPayload[] | null) {\n for (const entry of aliasEntries?.filter((entry) => entry.payload.schema === SchemaSchema) ?? []) {\n this.cacheSchemaIfValid(entry as SchemaCacheEntry)\n }\n }\n\n private async fetchSchema(schema: string) {\n try {\n const domain = await DomainPayloadWrapper.discover(schema, this.proxy)\n await domain?.fetch()\n this.cacheSchemas(domain?.aliases)\n\n //if it is still undefined, mark it as null (not found)\n if (this._cache.get(schema) === undefined) {\n this._cache.set(schema, SchemaCache.NULL)\n }\n } catch (error) {\n //if failed, set it to NULL, TODO: Make an entry for an error to try again in the future?\n this._cache.set(schema, SchemaCache.NULL)\n if (isAxiosError(error)) {\n console.log(`Axios Url: ${error.response?.config.url}`)\n }\n handleError(error, (error) => {\n console.error(`fetchSchema threw: ${error.message}`)\n })\n }\n }\n}\n"],"mappings":";;;;;;;;;AAAA,SAASA,aAAa;AAEf,IAAMC,YAAN,MAAMA,UAAAA;EACHC,MAAM,oBAAIC,IAAAA;EAElB,MAAMC,IAAOC,KAAWC,SAA2BC,UAAU,KAAQ;AACnE,UAAMC,YAAYC,KAAKC,IAAG;AAC1B,WAAO,KAAKR,IAAIS,IAAIN,GAAAA,GAAM;AACxB,YAAMO,MAAM,GAAA;AACZ,UAAIH,KAAKC,IAAG,IAAKF,YAAYD,SAAS;AACpC,cAAM,IAAIM,MAAM,uBAAuBR,GAAAA,GAAM;MAC/C;IACF;AACA,QAAI;AACF,WAAKH,IAAIY,IAAIT,KAAK,CAAA;AAClB,aAAO,MAAMC,QAAAA;IACf,UAAA;AACE,WAAKJ,IAAIY,IAAIT,KAAK,CAAA;IACpB;EACF;AACF;AAlBaJ;AAAN,IAAMA,WAAN;;;ACFP,SAASc,oBAAoB;AAC7B,SAASC,mBAAmB;AAC5B,SAASC,4BAA4B;AAErC,SAAwBC,oBAAoB;AAC5C,OA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AsB2B,YAAYR;MACpC;IACF;EACF;EAEQW,aAAaC,cAAwC;AAC3D,eAAWZ,UAASY,6CAAcC,OAAO,CAACb,WAAUA,OAAMC,QAAQvB,WAAWoC,kBAAiB,CAAA,GAAI;AAChG,WAAKf,mBAAmBC,KAAAA;IAC1B;EACF;EAEA,MAAcJ,YAAYlB,QAAgB;AA7F5C;AA8FI,QAAI;AACF,YAAMqC,SAAS,MAAMC,qBAAqBC,SAASvC,QAAQ,KAAKI,KAAK;AACrE,aAAMiC,iCAAQG;AACd,WAAKP,aAAaI,iCAAQI,OAAAA;AAG1B,UAAI,KAAKpC,OAAOU,IAAIf,MAAAA,MAAYiB,QAAW;AACzC,aAAKZ,OAAO0B,IAAI/B,QAAQE,aAAYkB,IAAI;MAC1C;IACF,SAASsB,OAAO;AAEd,WAAKrC,OAAO0B,IAAI/B,QAAQE,aAAYkB,IAAI;AACxC,UAAIuB,aAAaD,KAAAA,GAAQ;AACvBE,gBAAQC,IAAI,eAAcH,WAAMI,aAANJ,mBAAgBK,OAAOC,GAAAA,EAAK;MACxD;AACAC,kBAAYP,OAAO,CAACA,WAAAA;AAClBE,gBAAQF,MAAM,sBAAsBA,OAAMQ,OAAO,EAAE;MACrD,CAAA;IACF;EACF;AACF;AA/FahD;;;;;AAKX,cALWA,cAKekB,QAAyB;EAAEG,SAAS;IAAEC,YAAY,CAAC;IAAGxB,QAAQoC;EAAa;AAAE;AAEvG,cAPWlC,cAOIW;AAPV,IAAMX,cAAN;","names":["delay","Debounce","map","Map","one","key","closure","timeout","startTime","Date","now","get","delay","Error","set","isAxiosError","handleError","DomainPayloadWrapper","SchemaSchema","Ajv","LRUCache","getSchemaNameFromSchema","schema","$id","SchemaCache","onSchemaCached","proxy","_cache","LRUCache","max","ttl","_validators","getDebounce","Debounce","instance","_instance","validators","get","one","undefined","fetchSchema","value","NULL","cacheSchemaIfValid","entry","payload","definition","ajv","Ajv","strict","validator","compile","schemaName","set","key","cacheSchemas","aliasEntries","filter","SchemaSchema","domain","DomainPayloadWrapper","discover","fetch","aliases","error","isAxiosError","console","log","response","config","url","handleError","message"]}
|
package/package.json
CHANGED
|
@@ -12,10 +12,10 @@
|
|
|
12
12
|
"@xylabs/axios": "^2.13.20",
|
|
13
13
|
"@xylabs/delay": "^2.13.20",
|
|
14
14
|
"@xylabs/error": "^2.13.20",
|
|
15
|
-
"@xyo-network/domain-payload-plugin": "^2.
|
|
16
|
-
"@xyo-network/huri": "~2.
|
|
17
|
-
"@xyo-network/payload-model": "~2.
|
|
18
|
-
"@xyo-network/schema-payload-plugin": "^2.
|
|
15
|
+
"@xyo-network/domain-payload-plugin": "^2.85.0",
|
|
16
|
+
"@xyo-network/huri": "~2.85.0",
|
|
17
|
+
"@xyo-network/payload-model": "~2.85.0",
|
|
18
|
+
"@xyo-network/schema-payload-plugin": "^2.85.0",
|
|
19
19
|
"ajv": "^8.12.0",
|
|
20
20
|
"lru-cache": "^10.1.0"
|
|
21
21
|
},
|
|
@@ -24,8 +24,8 @@
|
|
|
24
24
|
"@xylabs/assert": "^2.13.20",
|
|
25
25
|
"@xylabs/ts-scripts-yarn3": "^3.2.25",
|
|
26
26
|
"@xylabs/tsconfig-dom": "^3.2.25",
|
|
27
|
-
"@xyo-network/network": "~2.
|
|
28
|
-
"@xyo-network/payload-builder": "~2.
|
|
27
|
+
"@xyo-network/network": "~2.85.0",
|
|
28
|
+
"@xyo-network/payload-builder": "~2.85.0",
|
|
29
29
|
"typescript": "^5.3.3"
|
|
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.
|
|
71
|
+
"version": "2.85.0"
|
|
72
72
|
}
|