@xyo-network/api 2.21.20 → 2.21.21
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/cjs/Archivist/Model/Archivist.d.ts +6 -4
- package/dist/cjs/Archivist/XyoApiArchivist.d.ts +12 -0
- package/dist/cjs/Archivist/XyoApiArchivist.js +35 -0
- package/dist/cjs/Archivist/XyoApiArchivist.js.map +1 -0
- package/dist/cjs/Archivist/XyoArchivist.d.ts +11 -0
- package/dist/cjs/Archivist/XyoArchivist.js +13 -0
- package/dist/cjs/Archivist/XyoArchivist.js.map +1 -0
- package/dist/cjs/Archivist/XyoMemoryArchivist.d.ts +12 -0
- package/dist/cjs/Archivist/XyoMemoryArchivist.js +64 -0
- package/dist/cjs/Archivist/XyoMemoryArchivist.js.map +1 -0
- package/dist/cjs/Archivist/index.d.ts +3 -0
- package/dist/cjs/Archivist/index.js +3 -0
- package/dist/cjs/Archivist/index.js.map +1 -1
- package/dist/esm/Archivist/Model/Archivist.d.ts +6 -4
- package/dist/esm/Archivist/XyoApiArchivist.d.ts +12 -0
- package/dist/esm/Archivist/XyoApiArchivist.js +22 -0
- package/dist/esm/Archivist/XyoApiArchivist.js.map +1 -0
- package/dist/esm/Archivist/XyoArchivist.d.ts +11 -0
- package/dist/esm/Archivist/XyoArchivist.js +9 -0
- package/dist/esm/Archivist/XyoArchivist.js.map +1 -0
- package/dist/esm/Archivist/XyoMemoryArchivist.d.ts +12 -0
- package/dist/esm/Archivist/XyoMemoryArchivist.js +58 -0
- package/dist/esm/Archivist/XyoMemoryArchivist.js.map +1 -0
- package/dist/esm/Archivist/index.d.ts +3 -0
- package/dist/esm/Archivist/index.js +3 -0
- package/dist/esm/Archivist/index.js.map +1 -1
- package/package.json +7 -6
- package/src/Archivist/Model/Archivist.ts +6 -4
- package/src/Archivist/XyoApiArchivist.ts +31 -0
- package/src/Archivist/XyoArchivist.ts +19 -0
- package/src/Archivist/XyoMemoryArchivist.ts +62 -0
- package/src/Archivist/index.ts +3 -0
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
export interface QueryableArchivist<TQueryResponse, TQuery> {
|
|
2
|
-
find(query: TQuery): Promise<TQueryResponse
|
|
2
|
+
find(query: TQuery): Promise<TQueryResponse> | TQueryResponse;
|
|
3
3
|
}
|
|
4
4
|
export interface ReadArchivist<TReadResponse, TId = string> {
|
|
5
|
-
get(id: TId): Promise<TReadResponse
|
|
5
|
+
get(id: TId): Promise<TReadResponse | undefined> | TReadResponse | undefined;
|
|
6
6
|
}
|
|
7
|
-
export interface WriteArchivist<TWriteResponse, TWrite> {
|
|
8
|
-
insert(item: TWrite): Promise<TWriteResponse
|
|
7
|
+
export interface WriteArchivist<TWriteResponse, TWrite, TId = string> {
|
|
8
|
+
insert(item: TWrite): Promise<TWriteResponse> | TWriteResponse;
|
|
9
|
+
delete?(id: TId): Promise<boolean> | boolean;
|
|
10
|
+
clear?(): void;
|
|
9
11
|
}
|
|
10
12
|
export declare type ReadWriteArchivist<TWriteResponse, TWrite, TReadResponse = TWriteResponse, TId = string> = ReadArchivist<TReadResponse, TId> & WriteArchivist<TWriteResponse, TWrite>;
|
|
11
13
|
export declare type Archivist<TWriteResponse, TWrite, TReadResponse = TWriteResponse, TId = string, TQueryResponse = unknown, TQuery = unknown> = ReadWriteArchivist<TWriteResponse, TWrite, TReadResponse, TId> & QueryableArchivist<TQueryResponse, TQuery>;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { XyoBoundWitnessWithPartialMeta } from '@xyo-network/boundwitness';
|
|
2
|
+
import { XyoArchivistApi } from './Api';
|
|
3
|
+
import { XyoPayloadFindFilter } from './Payload';
|
|
4
|
+
import { XyoArchivist } from './XyoArchivist';
|
|
5
|
+
export declare class XyoApiArchivist extends XyoArchivist {
|
|
6
|
+
protected api: XyoArchivistApi;
|
|
7
|
+
protected archive: string;
|
|
8
|
+
constructor(api: XyoArchivistApi, archive: string);
|
|
9
|
+
get(hash: string): Promise<import("@xyo-network/payload").XyoPayload<object> | undefined>;
|
|
10
|
+
insert(payload: XyoBoundWitnessWithPartialMeta): Promise<string[]>;
|
|
11
|
+
find(filter: XyoPayloadFindFilter): Promise<import("@xyo-network/payload").XyoPayloadWithPartialMeta<unknown>[]>;
|
|
12
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.XyoApiArchivist = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const sdk_js_1 = require("@xylabs/sdk-js");
|
|
6
|
+
const XyoArchivist_1 = require("./XyoArchivist");
|
|
7
|
+
class XyoApiArchivist extends XyoArchivist_1.XyoArchivist {
|
|
8
|
+
constructor(api, archive) {
|
|
9
|
+
super();
|
|
10
|
+
this.api = api;
|
|
11
|
+
this.archive = archive;
|
|
12
|
+
}
|
|
13
|
+
get(hash) {
|
|
14
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
15
|
+
const [payloads] = yield this.api.archive(this.archive).payload.hash(hash).get('tuple');
|
|
16
|
+
return payloads === null || payloads === void 0 ? void 0 : payloads.pop();
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
insert(payload) {
|
|
20
|
+
var _a, _b;
|
|
21
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
22
|
+
return (_b = (_a = (yield this.api.archive(this.archive).block.post([payload]))) === null || _a === void 0 ? void 0 : _a.map((value) => (0, sdk_js_1.assertEx)(value._hash))) !== null && _b !== void 0 ? _b : [];
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
find(filter) {
|
|
26
|
+
var _a, _b;
|
|
27
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
28
|
+
const [payloads = []] = (_a = (yield this.api.archive(this.archive).payload.find(filter, 'tuple'))) !== null && _a !== void 0 ? _a : [];
|
|
29
|
+
const [blocks = []] = (_b = (yield this.api.archive(this.archive).block.find(filter, 'tuple'))) !== null && _b !== void 0 ? _b : [];
|
|
30
|
+
return payloads.concat(blocks);
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
exports.XyoApiArchivist = XyoApiArchivist;
|
|
35
|
+
//# sourceMappingURL=XyoApiArchivist.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"XyoApiArchivist.js","sourceRoot":"","sources":["../../../src/Archivist/XyoApiArchivist.ts"],"names":[],"mappings":";;;;AAAA,2CAAyC;AAKzC,iDAA6C;AAE7C,MAAa,eAAgB,SAAQ,2BAAY;IAG/C,YAAY,GAAoB,EAAE,OAAe;QAC/C,KAAK,EAAE,CAAA;QACP,IAAI,CAAC,GAAG,GAAG,GAAG,CAAA;QACd,IAAI,CAAC,OAAO,GAAG,OAAO,CAAA;IACxB,CAAC;IAEY,GAAG,CAAC,IAAY;;YAC3B,MAAM,CAAC,QAAQ,CAAC,GAAG,MAAM,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,CAAA;YACvF,OAAO,QAAQ,aAAR,QAAQ,uBAAR,QAAQ,CAAE,GAAG,EAAE,CAAA;QACxB,CAAC;KAAA;IAEY,MAAM,CAAC,OAAuC;;;YACzD,OAAO,MAAA,MAAA,CAAC,MAAM,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,0CAAE,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,IAAA,iBAAQ,EAAC,KAAK,CAAC,KAAK,CAAC,CAAC,mCAAI,EAAE,CAAA;;KACjH;IAEY,IAAI,CAAC,MAA4B;;;YAC5C,MAAM,CAAC,QAAQ,GAAG,EAAE,CAAC,GAAG,MAAA,CAAC,MAAM,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,OAAO,CAAC,IAAI,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,mCAAI,EAAE,CAAA;YAClG,MAAM,CAAC,MAAM,GAAG,EAAE,CAAC,GAAG,MAAA,CAAC,MAAM,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,mCAAI,EAAE,CAAA;YAC9F,OAAO,QAAQ,CAAC,MAAM,CAAC,MAAM,CAAC,CAAA;;KAC/B;CACF;AAvBD,0CAuBC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { XyoBoundWitness, XyoBoundWitnessWithPartialMeta } from '@xyo-network/boundwitness';
|
|
2
|
+
import { XyoPayload, XyoPayloadWithPartialMeta } from '@xyo-network/payload';
|
|
3
|
+
import { Archivist } from './Model';
|
|
4
|
+
import { XyoPayloadFindFilter } from './Payload';
|
|
5
|
+
export declare abstract class XyoArchivist<TWrite extends XyoBoundWitness = XyoBoundWitnessWithPartialMeta, TRead extends XyoPayload = XyoPayloadWithPartialMeta> implements Archivist<string[], TWrite, TRead | undefined, string, TRead[], XyoPayloadFindFilter> {
|
|
6
|
+
abstract insert(item: TWrite): string[] | Promise<string[]>;
|
|
7
|
+
abstract find(query: XyoPayloadFindFilter): TRead[] | Promise<TRead[]>;
|
|
8
|
+
abstract get(hash: string): TRead | Promise<TRead | undefined> | undefined;
|
|
9
|
+
delete(_hash: string): boolean;
|
|
10
|
+
clear(): void;
|
|
11
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.XyoArchivist = void 0;
|
|
4
|
+
class XyoArchivist {
|
|
5
|
+
delete(_hash) {
|
|
6
|
+
throw Error('delete not supported');
|
|
7
|
+
}
|
|
8
|
+
clear() {
|
|
9
|
+
throw Error('clear not supported');
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
exports.XyoArchivist = XyoArchivist;
|
|
13
|
+
//# sourceMappingURL=XyoArchivist.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"XyoArchivist.js","sourceRoot":"","sources":["../../../src/Archivist/XyoArchivist.ts"],"names":[],"mappings":";;;AAMA,MAAsB,YAAY;IAMzB,MAAM,CAAC,KAAa;QACzB,MAAM,KAAK,CAAC,sBAAsB,CAAC,CAAA;IACrC,CAAC;IACM,KAAK;QACV,MAAM,KAAK,CAAC,qBAAqB,CAAC,CAAA;IACpC,CAAC;CACF;AAZD,oCAYC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { XyoBoundWitnessWithMeta } from '@xyo-network/boundwitness';
|
|
2
|
+
import { XyoPayload, XyoPayloadWithPartialMeta } from '@xyo-network/payload';
|
|
3
|
+
import { XyoPayloadFindFilter } from './Payload';
|
|
4
|
+
import { XyoArchivist } from './XyoArchivist';
|
|
5
|
+
export declare class XyoMemoryArchivist extends XyoArchivist {
|
|
6
|
+
private cache;
|
|
7
|
+
delete(hash: string): boolean;
|
|
8
|
+
clear(): void;
|
|
9
|
+
get(hash: string): XyoPayloadWithPartialMeta<unknown> | undefined;
|
|
10
|
+
insert(payload: XyoBoundWitnessWithMeta): string[];
|
|
11
|
+
find<T extends XyoPayload = XyoPayload>(filter: XyoPayloadFindFilter): T[];
|
|
12
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.XyoMemoryArchivist = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const boundwitness_1 = require("@xyo-network/boundwitness");
|
|
6
|
+
const payload_1 = require("@xyo-network/payload");
|
|
7
|
+
const lru_cache_1 = tslib_1.__importDefault(require("lru-cache"));
|
|
8
|
+
const XyoArchivist_1 = require("./XyoArchivist");
|
|
9
|
+
class XyoMemoryArchivist extends XyoArchivist_1.XyoArchivist {
|
|
10
|
+
constructor() {
|
|
11
|
+
super(...arguments);
|
|
12
|
+
this.cache = new lru_cache_1.default({ max: 10000 });
|
|
13
|
+
}
|
|
14
|
+
delete(hash) {
|
|
15
|
+
return this.cache.delete(hash);
|
|
16
|
+
}
|
|
17
|
+
clear() {
|
|
18
|
+
this.cache.clear();
|
|
19
|
+
}
|
|
20
|
+
get(hash) {
|
|
21
|
+
return this.cache.get(hash);
|
|
22
|
+
}
|
|
23
|
+
insert(payload) {
|
|
24
|
+
var _a;
|
|
25
|
+
const wrapper = new boundwitness_1.XyoBoundWitnessWrapper(payload);
|
|
26
|
+
const payloadWithmeta = Object.assign(Object.assign({}, payload), { _hash: wrapper.hash, _timestamp: Date.now() });
|
|
27
|
+
const hashes = [];
|
|
28
|
+
hashes.push(payloadWithmeta._hash);
|
|
29
|
+
this.cache.set(payloadWithmeta._hash, payloadWithmeta);
|
|
30
|
+
(_a = payload._payloads) === null || _a === void 0 ? void 0 : _a.forEach((payload) => {
|
|
31
|
+
const wrapper = new payload_1.XyoPayloadWrapper(payload);
|
|
32
|
+
const payloadWithmeta = Object.assign(Object.assign({}, payload), { _hash: wrapper.hash, _timestamp: Date.now() });
|
|
33
|
+
hashes.push(payloadWithmeta._hash);
|
|
34
|
+
this.cache.set(payloadWithmeta._hash, payloadWithmeta);
|
|
35
|
+
});
|
|
36
|
+
return hashes;
|
|
37
|
+
}
|
|
38
|
+
find(filter) {
|
|
39
|
+
const result = [];
|
|
40
|
+
if (filter.type === 'schema') {
|
|
41
|
+
const filterSchema = filter.schema !== undefined ? (Array.isArray(filter.schema) ? filter.schema : [filter.schema]) : undefined;
|
|
42
|
+
this.cache.forEach((value) => {
|
|
43
|
+
let match = value;
|
|
44
|
+
match = filterSchema === undefined || filterSchema.includes(value.schema) ? value : undefined;
|
|
45
|
+
if (match) {
|
|
46
|
+
result.push(match);
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
if (filter.type === 'hash') {
|
|
51
|
+
const filterHash = filter.hash !== undefined ? (Array.isArray(filter.hash) ? filter.hash : [filter.hash]) : undefined;
|
|
52
|
+
this.cache.forEach((value) => {
|
|
53
|
+
let match = value;
|
|
54
|
+
match = filterHash === undefined || filterHash.includes(new payload_1.XyoPayloadWrapper(value).hash) ? value : undefined;
|
|
55
|
+
if (match) {
|
|
56
|
+
result.push(match);
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
return result;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
exports.XyoMemoryArchivist = XyoMemoryArchivist;
|
|
64
|
+
//# sourceMappingURL=XyoMemoryArchivist.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"XyoMemoryArchivist.js","sourceRoot":"","sources":["../../../src/Archivist/XyoMemoryArchivist.ts"],"names":[],"mappings":";;;;AAAA,4DAA2F;AAC3F,kDAA+F;AAC/F,kEAAgC;AAGhC,iDAA6C;AAE7C,MAAa,kBAAmB,SAAQ,2BAAY;IAApD;;QACU,UAAK,GAAgD,IAAI,mBAAQ,CAAoC,EAAE,GAAG,EAAE,KAAK,EAAE,CAAC,CAAA;IAqD9H,CAAC;IAnDQ,MAAM,CAAC,IAAY;QACxB,OAAO,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,IAAI,CAAC,CAAA;IAChC,CAAC;IAEM,KAAK;QACV,IAAI,CAAC,KAAK,CAAC,KAAK,EAAE,CAAA;IACpB,CAAC;IAEM,GAAG,CAAC,IAAY;QACrB,OAAO,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,CAAA;IAC7B,CAAC;IAEM,MAAM,CAAC,OAAgC;;QAC5C,MAAM,OAAO,GAAG,IAAI,qCAAsB,CAAC,OAAO,CAAC,CAAA;QACnD,MAAM,eAAe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AAG,YAAY,KAAK,SAAS,IAAI,YAAY,CAAC,QAAQ,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,SAAS,CAAA;gBAC7F,IAAI,KAAK,EAAE;oBACT,MAAM,CAAC,IAAI,CAAC,KAAU,CAAC,CAAA;iBACxB;YACH,CAAC,CAAC,CAAA;SACH;QACD,IAAI,MAAM,CAAC,IAAI,KAAK,MAAM,EAAE;YAC1B,MAAM,UAAU,GAAG,MAAM,CAAC,IAAI,KAAK,SAAS,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,SAAS,CAAA;YACrH,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,KAAK,EAAE,EAAE;gBAC3B,IAAI,KAAK,GAA2B,KAAK,CAAA;gBACzC,KAAK,GAAG,UAAU,KAAK,SAAS,IAAI,UAAU,CAAC,QAAQ,CAAC,IAAI,2BAAiB,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,SAAS,CAAA;gBAC9G,IAAI,KAAK,EAAE;oBACT,MAAM,CAAC,IAAI,CAAC,KAAU,CAAC,CAAA;iBACxB;YACH,CAAC,CAAC,CAAA;SACH;QACD,OAAO,MAAM,CAAA;IACf,CAAC;CACF;AAtDD,gDAsDC"}
|
|
@@ -8,4 +8,7 @@ tslib_1.__exportStar(require("./Archives"), exports);
|
|
|
8
8
|
tslib_1.__exportStar(require("./Block"), exports);
|
|
9
9
|
tslib_1.__exportStar(require("./Model"), exports);
|
|
10
10
|
tslib_1.__exportStar(require("./Payload"), exports);
|
|
11
|
+
tslib_1.__exportStar(require("./XyoApiArchivist"), exports);
|
|
12
|
+
tslib_1.__exportStar(require("./XyoArchivist"), exports);
|
|
13
|
+
tslib_1.__exportStar(require("./XyoMemoryArchivist"), exports);
|
|
11
14
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/Archivist/index.ts"],"names":[],"mappings":";;;AAAA,oDAAyB;AACzB,gDAAqB;AACrB,oDAAyB;AACzB,qDAA0B;AAC1B,kDAAuB;AACvB,kDAAuB;AACvB,oDAAyB"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/Archivist/index.ts"],"names":[],"mappings":";;;AAAA,oDAAyB;AACzB,gDAAqB;AACrB,oDAAyB;AACzB,qDAA0B;AAC1B,kDAAuB;AACvB,kDAAuB;AACvB,oDAAyB;AACzB,4DAAiC;AACjC,yDAA8B;AAC9B,+DAAoC"}
|
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
export interface QueryableArchivist<TQueryResponse, TQuery> {
|
|
2
|
-
find(query: TQuery): Promise<TQueryResponse
|
|
2
|
+
find(query: TQuery): Promise<TQueryResponse> | TQueryResponse;
|
|
3
3
|
}
|
|
4
4
|
export interface ReadArchivist<TReadResponse, TId = string> {
|
|
5
|
-
get(id: TId): Promise<TReadResponse
|
|
5
|
+
get(id: TId): Promise<TReadResponse | undefined> | TReadResponse | undefined;
|
|
6
6
|
}
|
|
7
|
-
export interface WriteArchivist<TWriteResponse, TWrite> {
|
|
8
|
-
insert(item: TWrite): Promise<TWriteResponse
|
|
7
|
+
export interface WriteArchivist<TWriteResponse, TWrite, TId = string> {
|
|
8
|
+
insert(item: TWrite): Promise<TWriteResponse> | TWriteResponse;
|
|
9
|
+
delete?(id: TId): Promise<boolean> | boolean;
|
|
10
|
+
clear?(): void;
|
|
9
11
|
}
|
|
10
12
|
export declare type ReadWriteArchivist<TWriteResponse, TWrite, TReadResponse = TWriteResponse, TId = string> = ReadArchivist<TReadResponse, TId> & WriteArchivist<TWriteResponse, TWrite>;
|
|
11
13
|
export declare type Archivist<TWriteResponse, TWrite, TReadResponse = TWriteResponse, TId = string, TQueryResponse = unknown, TQuery = unknown> = ReadWriteArchivist<TWriteResponse, TWrite, TReadResponse, TId> & QueryableArchivist<TQueryResponse, TQuery>;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { XyoBoundWitnessWithPartialMeta } from '@xyo-network/boundwitness';
|
|
2
|
+
import { XyoArchivistApi } from './Api';
|
|
3
|
+
import { XyoPayloadFindFilter } from './Payload';
|
|
4
|
+
import { XyoArchivist } from './XyoArchivist';
|
|
5
|
+
export declare class XyoApiArchivist extends XyoArchivist {
|
|
6
|
+
protected api: XyoArchivistApi;
|
|
7
|
+
protected archive: string;
|
|
8
|
+
constructor(api: XyoArchivistApi, archive: string);
|
|
9
|
+
get(hash: string): Promise<import("@xyo-network/payload").XyoPayload<object> | undefined>;
|
|
10
|
+
insert(payload: XyoBoundWitnessWithPartialMeta): Promise<string[]>;
|
|
11
|
+
find(filter: XyoPayloadFindFilter): Promise<import("@xyo-network/payload").XyoPayloadWithPartialMeta<unknown>[]>;
|
|
12
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { assertEx } from '@xylabs/sdk-js';
|
|
2
|
+
import { XyoArchivist } from './XyoArchivist';
|
|
3
|
+
export class XyoApiArchivist extends XyoArchivist {
|
|
4
|
+
constructor(api, archive) {
|
|
5
|
+
super();
|
|
6
|
+
this.api = api;
|
|
7
|
+
this.archive = archive;
|
|
8
|
+
}
|
|
9
|
+
async get(hash) {
|
|
10
|
+
const [payloads] = await this.api.archive(this.archive).payload.hash(hash).get('tuple');
|
|
11
|
+
return payloads?.pop();
|
|
12
|
+
}
|
|
13
|
+
async insert(payload) {
|
|
14
|
+
return (await this.api.archive(this.archive).block.post([payload]))?.map((value) => assertEx(value._hash)) ?? [];
|
|
15
|
+
}
|
|
16
|
+
async find(filter) {
|
|
17
|
+
const [payloads = []] = (await this.api.archive(this.archive).payload.find(filter, 'tuple')) ?? [];
|
|
18
|
+
const [blocks = []] = (await this.api.archive(this.archive).block.find(filter, 'tuple')) ?? [];
|
|
19
|
+
return payloads.concat(blocks);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
//# sourceMappingURL=XyoApiArchivist.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"XyoApiArchivist.js","sourceRoot":"","sources":["../../../src/Archivist/XyoApiArchivist.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAA;AAKzC,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAA;AAE7C,MAAM,OAAO,eAAgB,SAAQ,YAAY;IAG/C,YAAY,GAAoB,EAAE,OAAe;QAC/C,KAAK,EAAE,CAAA;QACP,IAAI,CAAC,GAAG,GAAG,GAAG,CAAA;QACd,IAAI,CAAC,OAAO,GAAG,OAAO,CAAA;IACxB,CAAC;IAEM,KAAK,CAAC,GAAG,CAAC,IAAY;QAC3B,MAAM,CAAC,QAAQ,CAAC,GAAG,MAAM,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,CAAA;QACvF,OAAO,QAAQ,EAAE,GAAG,EAAE,CAAA;IACxB,CAAC;IAEM,KAAK,CAAC,MAAM,CAAC,OAAuC;QACzD,OAAO,CAAC,MAAM,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,QAAQ,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,IAAI,EAAE,CAAA;IAClH,CAAC;IAEM,KAAK,CAAC,IAAI,CAAC,MAA4B;QAC5C,MAAM,CAAC,QAAQ,GAAG,EAAE,CAAC,GAAG,CAAC,MAAM,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,OAAO,CAAC,IAAI,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,IAAI,EAAE,CAAA;QAClG,MAAM,CAAC,MAAM,GAAG,EAAE,CAAC,GAAG,CAAC,MAAM,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,IAAI,EAAE,CAAA;QAC9F,OAAO,QAAQ,CAAC,MAAM,CAAC,MAAM,CAAC,CAAA;IAChC,CAAC;CACF"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { XyoBoundWitness, XyoBoundWitnessWithPartialMeta } from '@xyo-network/boundwitness';
|
|
2
|
+
import { XyoPayload, XyoPayloadWithPartialMeta } from '@xyo-network/payload';
|
|
3
|
+
import { Archivist } from './Model';
|
|
4
|
+
import { XyoPayloadFindFilter } from './Payload';
|
|
5
|
+
export declare abstract class XyoArchivist<TWrite extends XyoBoundWitness = XyoBoundWitnessWithPartialMeta, TRead extends XyoPayload = XyoPayloadWithPartialMeta> implements Archivist<string[], TWrite, TRead | undefined, string, TRead[], XyoPayloadFindFilter> {
|
|
6
|
+
abstract insert(item: TWrite): string[] | Promise<string[]>;
|
|
7
|
+
abstract find(query: XyoPayloadFindFilter): TRead[] | Promise<TRead[]>;
|
|
8
|
+
abstract get(hash: string): TRead | Promise<TRead | undefined> | undefined;
|
|
9
|
+
delete(_hash: string): boolean;
|
|
10
|
+
clear(): void;
|
|
11
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"XyoArchivist.js","sourceRoot":"","sources":["../../../src/Archivist/XyoArchivist.ts"],"names":[],"mappings":"AAMA,MAAM,OAAgB,YAAY;IAMzB,MAAM,CAAC,KAAa;QACzB,MAAM,KAAK,CAAC,sBAAsB,CAAC,CAAA;IACrC,CAAC;IACM,KAAK;QACV,MAAM,KAAK,CAAC,qBAAqB,CAAC,CAAA;IACpC,CAAC;CACF"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { XyoBoundWitnessWithMeta } from '@xyo-network/boundwitness';
|
|
2
|
+
import { XyoPayload, XyoPayloadWithPartialMeta } from '@xyo-network/payload';
|
|
3
|
+
import { XyoPayloadFindFilter } from './Payload';
|
|
4
|
+
import { XyoArchivist } from './XyoArchivist';
|
|
5
|
+
export declare class XyoMemoryArchivist extends XyoArchivist {
|
|
6
|
+
private cache;
|
|
7
|
+
delete(hash: string): boolean;
|
|
8
|
+
clear(): void;
|
|
9
|
+
get(hash: string): XyoPayloadWithPartialMeta<unknown> | undefined;
|
|
10
|
+
insert(payload: XyoBoundWitnessWithMeta): string[];
|
|
11
|
+
find<T extends XyoPayload = XyoPayload>(filter: XyoPayloadFindFilter): T[];
|
|
12
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { XyoBoundWitnessWrapper } from '@xyo-network/boundwitness';
|
|
2
|
+
import { XyoPayloadWrapper } from '@xyo-network/payload';
|
|
3
|
+
import LruCache from 'lru-cache';
|
|
4
|
+
import { XyoArchivist } from './XyoArchivist';
|
|
5
|
+
export class XyoMemoryArchivist extends XyoArchivist {
|
|
6
|
+
constructor() {
|
|
7
|
+
super(...arguments);
|
|
8
|
+
this.cache = new LruCache({ max: 10000 });
|
|
9
|
+
}
|
|
10
|
+
delete(hash) {
|
|
11
|
+
return this.cache.delete(hash);
|
|
12
|
+
}
|
|
13
|
+
clear() {
|
|
14
|
+
this.cache.clear();
|
|
15
|
+
}
|
|
16
|
+
get(hash) {
|
|
17
|
+
return this.cache.get(hash);
|
|
18
|
+
}
|
|
19
|
+
insert(payload) {
|
|
20
|
+
const wrapper = new XyoBoundWitnessWrapper(payload);
|
|
21
|
+
const payloadWithmeta = { ...payload, _hash: wrapper.hash, _timestamp: Date.now() };
|
|
22
|
+
const hashes = [];
|
|
23
|
+
hashes.push(payloadWithmeta._hash);
|
|
24
|
+
this.cache.set(payloadWithmeta._hash, payloadWithmeta);
|
|
25
|
+
payload._payloads?.forEach((payload) => {
|
|
26
|
+
const wrapper = new XyoPayloadWrapper(payload);
|
|
27
|
+
const payloadWithmeta = { ...payload, _hash: wrapper.hash, _timestamp: Date.now() };
|
|
28
|
+
hashes.push(payloadWithmeta._hash);
|
|
29
|
+
this.cache.set(payloadWithmeta._hash, payloadWithmeta);
|
|
30
|
+
});
|
|
31
|
+
return hashes;
|
|
32
|
+
}
|
|
33
|
+
find(filter) {
|
|
34
|
+
const result = [];
|
|
35
|
+
if (filter.type === 'schema') {
|
|
36
|
+
const filterSchema = filter.schema !== undefined ? (Array.isArray(filter.schema) ? filter.schema : [filter.schema]) : undefined;
|
|
37
|
+
this.cache.forEach((value) => {
|
|
38
|
+
let match = value;
|
|
39
|
+
match = filterSchema === undefined || filterSchema.includes(value.schema) ? value : undefined;
|
|
40
|
+
if (match) {
|
|
41
|
+
result.push(match);
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
if (filter.type === 'hash') {
|
|
46
|
+
const filterHash = filter.hash !== undefined ? (Array.isArray(filter.hash) ? filter.hash : [filter.hash]) : undefined;
|
|
47
|
+
this.cache.forEach((value) => {
|
|
48
|
+
let match = value;
|
|
49
|
+
match = filterHash === undefined || filterHash.includes(new XyoPayloadWrapper(value).hash) ? value : undefined;
|
|
50
|
+
if (match) {
|
|
51
|
+
result.push(match);
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
return result;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
//# sourceMappingURL=XyoMemoryArchivist.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"XyoMemoryArchivist.js","sourceRoot":"","sources":["../../../src/Archivist/XyoMemoryArchivist.ts"],"names":[],"mappings":"AAAA,OAAO,EAA2B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C,CAAC,CAAC,SAAS,CAAA;YACrH,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,KAAK,EAAE,EAAE;gBAC3B,IAAI,KAAK,GAA2B,KAAK,CAAA;gBACzC,KAAK,GAAG,UAAU,KAAK,SAAS,IAAI,UAAU,CAAC,QAAQ,CAAC,IAAI,iBAAiB,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,SAAS,CAAA;gBAC9G,IAAI,KAAK,EAAE;oBACT,MAAM,CAAC,IAAI,CAAC,KAAU,CAAC,CAAA;iBACxB;YACH,CAAC,CAAC,CAAA;SACH;QACD,OAAO,MAAM,CAAA;IACf,CAAC;CACF"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/Archivist/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAA;AACzB,cAAc,OAAO,CAAA;AACrB,cAAc,WAAW,CAAA;AACzB,cAAc,YAAY,CAAA;AAC1B,cAAc,SAAS,CAAA;AACvB,cAAc,SAAS,CAAA;AACvB,cAAc,WAAW,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/Archivist/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAA;AACzB,cAAc,OAAO,CAAA;AACrB,cAAc,WAAW,CAAA;AACzB,cAAc,YAAY,CAAA;AAC1B,cAAc,SAAS,CAAA;AACvB,cAAc,SAAS,CAAA;AACvB,cAAc,WAAW,CAAA;AACzB,cAAc,mBAAmB,CAAA;AACjC,cAAc,gBAAgB,CAAA;AAC9B,cAAc,sBAAsB,CAAA"}
|
package/package.json
CHANGED
|
@@ -10,12 +10,13 @@
|
|
|
10
10
|
},
|
|
11
11
|
"dependencies": {
|
|
12
12
|
"@xylabs/sdk-js": "^2.6.2",
|
|
13
|
-
"@xyo-network/account": "^2.21.
|
|
14
|
-
"@xyo-network/boundwitness": "^2.21.
|
|
15
|
-
"@xyo-network/core": "^2.21.
|
|
16
|
-
"@xyo-network/payload": "^2.21.
|
|
17
|
-
"@xyo-network/typeof": "^2.21.
|
|
13
|
+
"@xyo-network/account": "^2.21.21",
|
|
14
|
+
"@xyo-network/boundwitness": "^2.21.21",
|
|
15
|
+
"@xyo-network/core": "^2.21.21",
|
|
16
|
+
"@xyo-network/payload": "^2.21.21",
|
|
17
|
+
"@xyo-network/typeof": "^2.21.21",
|
|
18
18
|
"axios": "^0.27.2",
|
|
19
|
+
"lru-cache": "^7.13.1",
|
|
19
20
|
"pako": "^2.0.4",
|
|
20
21
|
"uuid": "^8.3.2"
|
|
21
22
|
},
|
|
@@ -60,6 +61,6 @@
|
|
|
60
61
|
},
|
|
61
62
|
"sideEffects": true,
|
|
62
63
|
"types": "dist/esm/index.d.ts",
|
|
63
|
-
"version": "2.21.
|
|
64
|
+
"version": "2.21.21",
|
|
64
65
|
"packageManager": "yarn@3.1.1"
|
|
65
66
|
}
|
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
export interface QueryableArchivist<TQueryResponse, TQuery> {
|
|
2
|
-
find(query: TQuery): Promise<TQueryResponse>
|
|
2
|
+
find(query: TQuery): Promise<TQueryResponse> | TQueryResponse
|
|
3
3
|
}
|
|
4
4
|
export interface ReadArchivist<TReadResponse, TId = string> {
|
|
5
|
-
get(id: TId): Promise<TReadResponse>
|
|
5
|
+
get(id: TId): Promise<TReadResponse | undefined> | TReadResponse | undefined
|
|
6
6
|
}
|
|
7
7
|
|
|
8
|
-
export interface WriteArchivist<TWriteResponse, TWrite> {
|
|
9
|
-
insert(item: TWrite): Promise<TWriteResponse>
|
|
8
|
+
export interface WriteArchivist<TWriteResponse, TWrite, TId = string> {
|
|
9
|
+
insert(item: TWrite): Promise<TWriteResponse> | TWriteResponse
|
|
10
|
+
delete?(id: TId): Promise<boolean> | boolean
|
|
11
|
+
clear?(): void
|
|
10
12
|
}
|
|
11
13
|
|
|
12
14
|
export type ReadWriteArchivist<TWriteResponse, TWrite, TReadResponse = TWriteResponse, TId = string> = ReadArchivist<TReadResponse, TId> & WriteArchivist<TWriteResponse, TWrite>
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { assertEx } from '@xylabs/sdk-js'
|
|
2
|
+
import { XyoBoundWitnessWithPartialMeta } from '@xyo-network/boundwitness'
|
|
3
|
+
|
|
4
|
+
import { XyoArchivistApi } from './Api'
|
|
5
|
+
import { XyoPayloadFindFilter } from './Payload'
|
|
6
|
+
import { XyoArchivist } from './XyoArchivist'
|
|
7
|
+
|
|
8
|
+
export class XyoApiArchivist extends XyoArchivist {
|
|
9
|
+
protected api: XyoArchivistApi
|
|
10
|
+
protected archive: string
|
|
11
|
+
constructor(api: XyoArchivistApi, archive: string) {
|
|
12
|
+
super()
|
|
13
|
+
this.api = api
|
|
14
|
+
this.archive = archive
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
public async get(hash: string) {
|
|
18
|
+
const [payloads] = await this.api.archive(this.archive).payload.hash(hash).get('tuple')
|
|
19
|
+
return payloads?.pop()
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
public async insert(payload: XyoBoundWitnessWithPartialMeta) {
|
|
23
|
+
return (await this.api.archive(this.archive).block.post([payload]))?.map((value) => assertEx(value._hash)) ?? []
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
public async find(filter: XyoPayloadFindFilter) {
|
|
27
|
+
const [payloads = []] = (await this.api.archive(this.archive).payload.find(filter, 'tuple')) ?? []
|
|
28
|
+
const [blocks = []] = (await this.api.archive(this.archive).block.find(filter, 'tuple')) ?? []
|
|
29
|
+
return payloads.concat(blocks)
|
|
30
|
+
}
|
|
31
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { XyoBoundWitness, XyoBoundWitnessWithPartialMeta } from '@xyo-network/boundwitness'
|
|
2
|
+
import { XyoPayload, XyoPayloadWithPartialMeta } from '@xyo-network/payload'
|
|
3
|
+
|
|
4
|
+
import { Archivist } from './Model'
|
|
5
|
+
import { XyoPayloadFindFilter } from './Payload'
|
|
6
|
+
|
|
7
|
+
export abstract class XyoArchivist<TWrite extends XyoBoundWitness = XyoBoundWitnessWithPartialMeta, TRead extends XyoPayload = XyoPayloadWithPartialMeta>
|
|
8
|
+
implements Archivist<string[], TWrite, TRead | undefined, string, TRead[], XyoPayloadFindFilter>
|
|
9
|
+
{
|
|
10
|
+
abstract insert(item: TWrite): string[] | Promise<string[]>
|
|
11
|
+
abstract find(query: XyoPayloadFindFilter): TRead[] | Promise<TRead[]>
|
|
12
|
+
abstract get(hash: string): TRead | Promise<TRead | undefined> | undefined
|
|
13
|
+
public delete(_hash: string): boolean {
|
|
14
|
+
throw Error('delete not supported')
|
|
15
|
+
}
|
|
16
|
+
public clear() {
|
|
17
|
+
throw Error('clear not supported')
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { XyoBoundWitnessWithMeta, XyoBoundWitnessWrapper } from '@xyo-network/boundwitness'
|
|
2
|
+
import { XyoPayload, XyoPayloadWithPartialMeta, XyoPayloadWrapper } from '@xyo-network/payload'
|
|
3
|
+
import LruCache from 'lru-cache'
|
|
4
|
+
|
|
5
|
+
import { XyoPayloadFindFilter } from './Payload'
|
|
6
|
+
import { XyoArchivist } from './XyoArchivist'
|
|
7
|
+
|
|
8
|
+
export class XyoMemoryArchivist extends XyoArchivist {
|
|
9
|
+
private cache: LruCache<string, XyoPayloadWithPartialMeta> = new LruCache<string, XyoPayloadWithPartialMeta>({ max: 10000 })
|
|
10
|
+
|
|
11
|
+
public delete(hash: string) {
|
|
12
|
+
return this.cache.delete(hash)
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
public clear() {
|
|
16
|
+
this.cache.clear()
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
public get(hash: string) {
|
|
20
|
+
return this.cache.get(hash)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
public insert(payload: XyoBoundWitnessWithMeta) {
|
|
24
|
+
const wrapper = new XyoBoundWitnessWrapper(payload)
|
|
25
|
+
const payloadWithmeta = { ...payload, _hash: wrapper.hash, _timestamp: Date.now() }
|
|
26
|
+
const hashes: string[] = []
|
|
27
|
+
hashes.push(payloadWithmeta._hash)
|
|
28
|
+
this.cache.set(payloadWithmeta._hash, payloadWithmeta)
|
|
29
|
+
payload._payloads?.forEach((payload) => {
|
|
30
|
+
const wrapper = new XyoPayloadWrapper(payload)
|
|
31
|
+
const payloadWithmeta = { ...payload, _hash: wrapper.hash, _timestamp: Date.now() }
|
|
32
|
+
hashes.push(payloadWithmeta._hash)
|
|
33
|
+
this.cache.set(payloadWithmeta._hash, payloadWithmeta)
|
|
34
|
+
})
|
|
35
|
+
return hashes
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
public find<T extends XyoPayload = XyoPayload>(filter: XyoPayloadFindFilter): T[] {
|
|
39
|
+
const result: T[] = []
|
|
40
|
+
if (filter.type === 'schema') {
|
|
41
|
+
const filterSchema = filter.schema !== undefined ? (Array.isArray(filter.schema) ? filter.schema : [filter.schema]) : undefined
|
|
42
|
+
this.cache.forEach((value) => {
|
|
43
|
+
let match: XyoPayload | undefined = value
|
|
44
|
+
match = filterSchema === undefined || filterSchema.includes(value.schema) ? value : undefined
|
|
45
|
+
if (match) {
|
|
46
|
+
result.push(match as T)
|
|
47
|
+
}
|
|
48
|
+
})
|
|
49
|
+
}
|
|
50
|
+
if (filter.type === 'hash') {
|
|
51
|
+
const filterHash = filter.hash !== undefined ? (Array.isArray(filter.hash) ? filter.hash : [filter.hash]) : undefined
|
|
52
|
+
this.cache.forEach((value) => {
|
|
53
|
+
let match: XyoPayload | undefined = value
|
|
54
|
+
match = filterHash === undefined || filterHash.includes(new XyoPayloadWrapper(value).hash) ? value : undefined
|
|
55
|
+
if (match) {
|
|
56
|
+
result.push(match as T)
|
|
57
|
+
}
|
|
58
|
+
})
|
|
59
|
+
}
|
|
60
|
+
return result
|
|
61
|
+
}
|
|
62
|
+
}
|