@xyo-network/archivist 2.38.15 → 2.38.17
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.d.ts +3 -3
- package/dist/cjs/Archivist.d.ts.map +1 -1
- package/dist/cjs/Config.d.ts +2 -2
- package/dist/cjs/Config.d.ts.map +1 -1
- package/dist/cjs/Queries/Delete.d.ts +1 -1
- package/dist/cjs/Queries/Delete.d.ts.map +1 -1
- package/dist/cjs/Queries/Find.d.ts +1 -1
- package/dist/cjs/Queries/Find.d.ts.map +1 -1
- package/dist/cjs/Queries/Get.d.ts +1 -1
- package/dist/cjs/Queries/Get.d.ts.map +1 -1
- package/dist/cjs/Queries/Insert.d.ts +1 -1
- package/dist/cjs/Queries/Insert.d.ts.map +1 -1
- package/dist/cjs/XyoArchivist.d.ts +7 -7
- package/dist/cjs/XyoArchivist.d.ts.map +1 -1
- package/dist/cjs/XyoArchivist.js +26 -26
- package/dist/cjs/XyoArchivist.js.map +1 -1
- package/dist/cjs/XyoArchivistWrapper.d.ts +4 -4
- package/dist/cjs/XyoArchivistWrapper.d.ts.map +1 -1
- package/dist/cjs/XyoArchivistWrapper.js +24 -24
- package/dist/cjs/XyoArchivistWrapper.js.map +1 -1
- package/dist/cjs/XyoCookieArchivist.d.ts +9 -9
- package/dist/cjs/XyoCookieArchivist.d.ts.map +1 -1
- package/dist/cjs/XyoCookieArchivist.js +63 -63
- package/dist/cjs/XyoCookieArchivist.js.map +1 -1
- package/dist/cjs/XyoMemoryArchivist.d.ts +8 -8
- package/dist/cjs/XyoMemoryArchivist.d.ts.map +1 -1
- package/dist/cjs/XyoMemoryArchivist.js +51 -51
- package/dist/cjs/XyoMemoryArchivist.js.map +1 -1
- package/dist/cjs/XyoStorageArchivist.d.ts +18 -18
- package/dist/cjs/XyoStorageArchivist.d.ts.map +1 -1
- package/dist/cjs/XyoStorageArchivist.js +101 -99
- package/dist/cjs/XyoStorageArchivist.js.map +1 -1
- package/dist/docs.json +2388 -2388
- package/dist/esm/Archivist.d.ts +3 -3
- package/dist/esm/Archivist.d.ts.map +1 -1
- package/dist/esm/Config.d.ts +2 -2
- package/dist/esm/Config.d.ts.map +1 -1
- package/dist/esm/Queries/Delete.d.ts +1 -1
- package/dist/esm/Queries/Delete.d.ts.map +1 -1
- package/dist/esm/Queries/Find.d.ts +1 -1
- package/dist/esm/Queries/Find.d.ts.map +1 -1
- package/dist/esm/Queries/Get.d.ts +1 -1
- package/dist/esm/Queries/Get.d.ts.map +1 -1
- package/dist/esm/Queries/Insert.d.ts +1 -1
- package/dist/esm/Queries/Insert.d.ts.map +1 -1
- package/dist/esm/XyoArchivist.d.ts +7 -7
- package/dist/esm/XyoArchivist.d.ts.map +1 -1
- package/dist/esm/XyoArchivist.js +25 -25
- package/dist/esm/XyoArchivist.js.map +1 -1
- package/dist/esm/XyoArchivistWrapper.d.ts +4 -4
- package/dist/esm/XyoArchivistWrapper.d.ts.map +1 -1
- package/dist/esm/XyoArchivistWrapper.js +23 -23
- package/dist/esm/XyoArchivistWrapper.js.map +1 -1
- package/dist/esm/XyoCookieArchivist.d.ts +9 -9
- package/dist/esm/XyoCookieArchivist.d.ts.map +1 -1
- package/dist/esm/XyoCookieArchivist.js +54 -54
- package/dist/esm/XyoCookieArchivist.js.map +1 -1
- package/dist/esm/XyoMemoryArchivist.d.ts +8 -8
- package/dist/esm/XyoMemoryArchivist.d.ts.map +1 -1
- package/dist/esm/XyoMemoryArchivist.js +50 -50
- package/dist/esm/XyoMemoryArchivist.js.map +1 -1
- package/dist/esm/XyoStorageArchivist.d.ts +18 -18
- package/dist/esm/XyoStorageArchivist.d.ts.map +1 -1
- package/dist/esm/XyoStorageArchivist.js +89 -89
- package/dist/esm/XyoStorageArchivist.js.map +1 -1
- package/package.json +6 -6
- package/src/Archivist.ts +3 -3
- package/src/Config.ts +3 -2
- package/src/Queries/Delete.ts +1 -1
- package/src/Queries/Find.ts +1 -1
- package/src/Queries/Get.ts +1 -1
- package/src/Queries/Insert.ts +1 -1
- package/src/XyoArchivist.ts +30 -29
- package/src/XyoArchivistWrapper.ts +24 -24
- package/src/XyoCookieArchivist.ts +61 -61
- package/src/XyoMemoryArchivist.ts +58 -57
- package/src/XyoStorageArchivist.ts +98 -96
package/src/Config.ts
CHANGED
|
@@ -2,16 +2,17 @@ import { AddressString, XyoModuleConfig } from '@xyo-network/module'
|
|
|
2
2
|
import { XyoPayload } from '@xyo-network/payload'
|
|
3
3
|
|
|
4
4
|
export interface XyoArchivistParents {
|
|
5
|
+
commit?: AddressString[]
|
|
5
6
|
read?: AddressString[]
|
|
6
7
|
write?: AddressString[]
|
|
7
|
-
commit?: AddressString[]
|
|
8
8
|
}
|
|
9
9
|
|
|
10
10
|
export type XyoArchivistConfig<TConfig extends XyoPayload = XyoPayload> = XyoModuleConfig<
|
|
11
11
|
{
|
|
12
|
+
cacheParentReads?: boolean
|
|
12
13
|
/** @field address of one or more parent archivists to read from */
|
|
13
14
|
parents?: XyoArchivistParents
|
|
14
|
-
|
|
15
|
+
|
|
15
16
|
/** @field address of archivist to write through to */
|
|
16
17
|
writeThrough?: string
|
|
17
18
|
} & TConfig
|
package/src/Queries/Delete.ts
CHANGED
|
@@ -4,6 +4,6 @@ export type XyoArchivistDeleteQuerySchema = 'network.xyo.query.archivist.delete'
|
|
|
4
4
|
export const XyoArchivistDeleteQuerySchema: XyoArchivistDeleteQuerySchema = 'network.xyo.query.archivist.delete'
|
|
5
5
|
|
|
6
6
|
export type XyoArchivistDeleteQuery = XyoQuery<{
|
|
7
|
-
schema: XyoArchivistDeleteQuerySchema
|
|
8
7
|
hashes: string[]
|
|
8
|
+
schema: XyoArchivistDeleteQuerySchema
|
|
9
9
|
}>
|
package/src/Queries/Find.ts
CHANGED
|
@@ -5,6 +5,6 @@ export type XyoArchivistFindQuerySchema = 'network.xyo.query.archivist.find'
|
|
|
5
5
|
export const XyoArchivistFindQuerySchema: XyoArchivistFindQuerySchema = 'network.xyo.query.archivist.find'
|
|
6
6
|
|
|
7
7
|
export type XyoArchivistFindQuery = XyoQuery<{
|
|
8
|
-
schema: XyoArchivistFindQuerySchema
|
|
9
8
|
filter?: XyoPayloadFindFilter
|
|
9
|
+
schema: XyoArchivistFindQuerySchema
|
|
10
10
|
}>
|
package/src/Queries/Get.ts
CHANGED
|
@@ -4,6 +4,6 @@ export type XyoArchivistGetQuerySchema = 'network.xyo.query.archivist.get'
|
|
|
4
4
|
export const XyoArchivistGetQuerySchema: XyoArchivistGetQuerySchema = 'network.xyo.query.archivist.get'
|
|
5
5
|
|
|
6
6
|
export type XyoArchivistGetQuery = XyoQuery<{
|
|
7
|
-
schema: XyoArchivistGetQuerySchema
|
|
8
7
|
hashes: string[]
|
|
8
|
+
schema: XyoArchivistGetQuerySchema
|
|
9
9
|
}>
|
package/src/Queries/Insert.ts
CHANGED
|
@@ -4,6 +4,6 @@ export type XyoArchivistInsertQuerySchema = 'network.xyo.query.archivist.insert'
|
|
|
4
4
|
export const XyoArchivistInsertQuerySchema: XyoArchivistInsertQuerySchema = 'network.xyo.query.archivist.insert'
|
|
5
5
|
|
|
6
6
|
export type XyoArchivistInsertQuery = XyoQuery<{
|
|
7
|
-
schema: XyoArchivistInsertQuerySchema
|
|
8
7
|
payloads: string[]
|
|
8
|
+
schema: XyoArchivistInsertQuerySchema
|
|
9
9
|
}>
|
package/src/XyoArchivist.ts
CHANGED
|
@@ -22,20 +22,27 @@ import {
|
|
|
22
22
|
import { XyoArchivistWrapper } from './XyoArchivistWrapper'
|
|
23
23
|
|
|
24
24
|
export interface XyoArchivistParentWrappers {
|
|
25
|
+
commit?: Record<string, XyoArchivistWrapper>
|
|
25
26
|
read?: Record<string, XyoArchivistWrapper>
|
|
26
27
|
write?: Record<string, XyoArchivistWrapper>
|
|
27
|
-
commit?: Record<string, XyoArchivistWrapper>
|
|
28
28
|
}
|
|
29
29
|
|
|
30
30
|
export abstract class XyoArchivist<TConfig extends XyoArchivistConfig = XyoArchivistConfig> extends XyoModule<TConfig> implements PayloadArchivist {
|
|
31
|
-
|
|
32
|
-
return [XyoArchivistGetQuerySchema, XyoArchivistInsertQuerySchema, ...super.queries()]
|
|
33
|
-
}
|
|
31
|
+
private _parents?: XyoArchivistParentWrappers
|
|
34
32
|
|
|
35
33
|
protected get cacheParentReads() {
|
|
36
34
|
return !!this.config?.cacheParentReads
|
|
37
35
|
}
|
|
38
36
|
|
|
37
|
+
protected get parents() {
|
|
38
|
+
this._parents = this._parents ?? {
|
|
39
|
+
commit: this.resolveArchivists(this.config?.parents?.commit),
|
|
40
|
+
read: this.resolveArchivists(this.config?.parents?.read),
|
|
41
|
+
write: this.resolveArchivists(this.config?.parents?.write),
|
|
42
|
+
}
|
|
43
|
+
return assertEx(this._parents)
|
|
44
|
+
}
|
|
45
|
+
|
|
39
46
|
protected get writeThrough() {
|
|
40
47
|
return !!this.config?.writeThrough
|
|
41
48
|
}
|
|
@@ -66,9 +73,9 @@ export abstract class XyoArchivist<TConfig extends XyoArchivistConfig = XyoArchi
|
|
|
66
73
|
}
|
|
67
74
|
}
|
|
68
75
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
76
|
+
public override queries() {
|
|
77
|
+
return [XyoArchivistGetQuerySchema, XyoArchivistInsertQuerySchema, ...super.queries()]
|
|
78
|
+
}
|
|
72
79
|
|
|
73
80
|
override async query<T extends XyoQueryBoundWitness = XyoQueryBoundWitness>(
|
|
74
81
|
query: T,
|
|
@@ -119,21 +126,6 @@ export abstract class XyoArchivist<TConfig extends XyoArchivistConfig = XyoArchi
|
|
|
119
126
|
return this.bindResult(resultPayloads, queryAccount)
|
|
120
127
|
}
|
|
121
128
|
|
|
122
|
-
private resolveArchivists(archivists?: string[]) {
|
|
123
|
-
const resolvedWrappers: Record<string, XyoArchivistWrapper> = {}
|
|
124
|
-
if (archivists) {
|
|
125
|
-
archivists.map((archivist) => {
|
|
126
|
-
if (resolvedWrappers[archivist] === undefined) {
|
|
127
|
-
const module = this.resolver?.fromAddress([archivist]).shift()
|
|
128
|
-
if (module) {
|
|
129
|
-
resolvedWrappers[archivist] = new XyoArchivistWrapper(module)
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
})
|
|
133
|
-
}
|
|
134
|
-
return resolvedWrappers
|
|
135
|
-
}
|
|
136
|
-
|
|
137
129
|
protected async getFromParents(hash: string) {
|
|
138
130
|
return compact(
|
|
139
131
|
await Promise.all(
|
|
@@ -168,13 +160,22 @@ export abstract class XyoArchivist<TConfig extends XyoArchivistConfig = XyoArchi
|
|
|
168
160
|
).flat()
|
|
169
161
|
}
|
|
170
162
|
|
|
171
|
-
private
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
163
|
+
private resolveArchivists(archivists?: string[]) {
|
|
164
|
+
const resolvedWrappers: Record<string, XyoArchivistWrapper> = {}
|
|
165
|
+
if (archivists) {
|
|
166
|
+
archivists.map((archivist) => {
|
|
167
|
+
if (resolvedWrappers[archivist] === undefined) {
|
|
168
|
+
const module = this.resolver?.fromAddress([archivist]).shift()
|
|
169
|
+
if (module) {
|
|
170
|
+
resolvedWrappers[archivist] = new XyoArchivistWrapper(module)
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
})
|
|
177
174
|
}
|
|
178
|
-
return
|
|
175
|
+
return resolvedWrappers
|
|
179
176
|
}
|
|
177
|
+
|
|
178
|
+
abstract get(hashes: string[]): PromisableArray<XyoPayload>
|
|
179
|
+
|
|
180
|
+
abstract insert(item: XyoPayload[]): PromisableArray<XyoBoundWitness>
|
|
180
181
|
}
|
|
@@ -22,12 +22,12 @@ import {
|
|
|
22
22
|
} from './Queries'
|
|
23
23
|
|
|
24
24
|
export class XyoArchivistWrapper extends XyoModuleWrapper implements PayloadArchivist {
|
|
25
|
-
public async
|
|
26
|
-
const queryPayload = PayloadWrapper.parse<
|
|
25
|
+
public async all(): Promise<XyoPayload[]> {
|
|
26
|
+
const queryPayload = PayloadWrapper.parse<XyoArchivistAllQuery>({ schema: XyoArchivistAllQuerySchema })
|
|
27
27
|
const query = await this.bindQuery(queryPayload)
|
|
28
28
|
const result = await this.module.query(query[0], query[1])
|
|
29
29
|
this.throwErrors(query, result)
|
|
30
|
-
return result[
|
|
30
|
+
return compact(result[1])
|
|
31
31
|
}
|
|
32
32
|
|
|
33
33
|
public async clear(): Promise<void> {
|
|
@@ -37,25 +37,20 @@ export class XyoArchivistWrapper extends XyoModuleWrapper implements PayloadArch
|
|
|
37
37
|
this.throwErrors(query, result)
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
-
public async
|
|
41
|
-
const queryPayload = PayloadWrapper.parse<
|
|
40
|
+
public async commit(): Promise<XyoBoundWitness[]> {
|
|
41
|
+
const queryPayload = PayloadWrapper.parse<XyoArchivistCommitQuery>({ schema: XyoArchivistCommitQuerySchema })
|
|
42
42
|
const query = await this.bindQuery(queryPayload)
|
|
43
43
|
const result = await this.module.query(query[0], query[1])
|
|
44
44
|
this.throwErrors(query, result)
|
|
45
|
-
return result[1]
|
|
45
|
+
return result[1].filter(isXyoBoundWitnessPayload)
|
|
46
46
|
}
|
|
47
47
|
|
|
48
|
-
public async
|
|
49
|
-
const queryPayload = PayloadWrapper.parse<
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
})
|
|
53
|
-
const query = await this.bindQuery(queryPayload, payloads)
|
|
54
|
-
const result = await this.module.query(query[0], [queryPayload.payload, ...payloads])
|
|
55
|
-
const innerBoundWitnesses =
|
|
56
|
-
result[1]?.filter<XyoBoundWitness>((payload): payload is XyoBoundWitness => payload?.schema === XyoBoundWitnessSchema) ?? []
|
|
48
|
+
public async delete(hashes: string[]) {
|
|
49
|
+
const queryPayload = PayloadWrapper.parse<XyoArchivistDeleteQuery>({ hashes, schema: XyoArchivistDeleteQuerySchema })
|
|
50
|
+
const query = await this.bindQuery(queryPayload)
|
|
51
|
+
const result = await this.module.query(query[0], query[1])
|
|
57
52
|
this.throwErrors(query, result)
|
|
58
|
-
return
|
|
53
|
+
return result[0].payload_hashes.map(() => true)
|
|
59
54
|
}
|
|
60
55
|
|
|
61
56
|
public async find(filter?: XyoPayloadFindFilter): Promise<XyoPayload[]> {
|
|
@@ -66,19 +61,24 @@ export class XyoArchivistWrapper extends XyoModuleWrapper implements PayloadArch
|
|
|
66
61
|
return compact(result[1])
|
|
67
62
|
}
|
|
68
63
|
|
|
69
|
-
public async
|
|
70
|
-
const queryPayload = PayloadWrapper.parse<
|
|
64
|
+
public async get(hashes: string[]): Promise<XyoPayload[]> {
|
|
65
|
+
const queryPayload = PayloadWrapper.parse<XyoArchivistGetQuery>({ hashes, schema: XyoArchivistGetQuerySchema })
|
|
71
66
|
const query = await this.bindQuery(queryPayload)
|
|
72
67
|
const result = await this.module.query(query[0], query[1])
|
|
73
68
|
this.throwErrors(query, result)
|
|
74
|
-
return
|
|
69
|
+
return result[1]
|
|
75
70
|
}
|
|
76
71
|
|
|
77
|
-
public async
|
|
78
|
-
const queryPayload = PayloadWrapper.parse<
|
|
79
|
-
|
|
80
|
-
|
|
72
|
+
public async insert(payloads: XyoPayload[]): Promise<XyoBoundWitness[]> {
|
|
73
|
+
const queryPayload = PayloadWrapper.parse<XyoArchivistInsertQuery>({
|
|
74
|
+
payloads: payloads.map((payload) => PayloadWrapper.hash(payload)),
|
|
75
|
+
schema: XyoArchivistInsertQuerySchema,
|
|
76
|
+
})
|
|
77
|
+
const query = await this.bindQuery(queryPayload, payloads)
|
|
78
|
+
const result = await this.module.query(query[0], [queryPayload.payload, ...payloads])
|
|
79
|
+
const innerBoundWitnesses =
|
|
80
|
+
result[1]?.filter<XyoBoundWitness>((payload): payload is XyoBoundWitness => payload?.schema === XyoBoundWitnessSchema) ?? []
|
|
81
81
|
this.throwErrors(query, result)
|
|
82
|
-
return result[
|
|
82
|
+
return [result[0], ...innerBoundWitnesses]
|
|
83
83
|
}
|
|
84
84
|
}
|
|
@@ -22,26 +22,20 @@ export type XyoCookieArchivistConfigSchema = 'network.xyo.module.config.archivis
|
|
|
22
22
|
export const XyoCookieArchivistConfigSchema: XyoCookieArchivistConfigSchema = 'network.xyo.module.config.archivist.cookie'
|
|
23
23
|
|
|
24
24
|
export type XyoCookieArchivistConfig = XyoArchivistConfig<{
|
|
25
|
-
schema: XyoCookieArchivistConfigSchema
|
|
26
25
|
domain?: string
|
|
27
26
|
maxEntries?: number
|
|
28
27
|
maxEntrySize?: number
|
|
29
28
|
namespace?: string
|
|
29
|
+
schema: XyoCookieArchivistConfigSchema
|
|
30
30
|
}>
|
|
31
31
|
|
|
32
32
|
export class XyoCookieArchivist extends XyoArchivist<XyoCookieArchivistConfig> {
|
|
33
|
-
static override
|
|
34
|
-
return (await super.create(params)) as XyoCookieArchivist
|
|
35
|
-
}
|
|
33
|
+
static override configSchema = XyoCookieArchivistConfigSchema
|
|
36
34
|
|
|
37
35
|
public get domain() {
|
|
38
36
|
return this.config?.domain
|
|
39
37
|
}
|
|
40
38
|
|
|
41
|
-
public get namespace() {
|
|
42
|
-
return this.config?.namespace ?? 'xyoarch'
|
|
43
|
-
}
|
|
44
|
-
|
|
45
39
|
public get maxEntries() {
|
|
46
40
|
//all browsers support at least 60 cookies
|
|
47
41
|
return this.config?.maxEntries ?? 60
|
|
@@ -52,27 +46,19 @@ export class XyoCookieArchivist extends XyoArchivist<XyoCookieArchivistConfig> {
|
|
|
52
46
|
return this.config?.maxEntrySize ?? 4000
|
|
53
47
|
}
|
|
54
48
|
|
|
55
|
-
public
|
|
56
|
-
return
|
|
57
|
-
XyoArchivistAllQuerySchema,
|
|
58
|
-
XyoArchivistDeleteQuerySchema,
|
|
59
|
-
XyoArchivistClearQuerySchema,
|
|
60
|
-
XyoArchivistFindQuerySchema,
|
|
61
|
-
XyoArchivistCommitQuerySchema,
|
|
62
|
-
...super.queries(),
|
|
63
|
-
]
|
|
49
|
+
public get namespace() {
|
|
50
|
+
return this.config?.namespace ?? 'xyoarch'
|
|
64
51
|
}
|
|
65
52
|
|
|
66
|
-
|
|
67
|
-
return
|
|
53
|
+
static override async create(params?: XyoModuleParams<XyoCookieArchivistConfig>): Promise<XyoCookieArchivist> {
|
|
54
|
+
return (await super.create(params)) as XyoCookieArchivist
|
|
68
55
|
}
|
|
69
56
|
|
|
70
|
-
public override
|
|
57
|
+
public override all(): PromisableArray<XyoPayload> {
|
|
71
58
|
try {
|
|
72
|
-
return
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
})
|
|
59
|
+
return Object.entries(Cookies.get())
|
|
60
|
+
.filter(([key]) => key.startsWith(`${this.namespace}-`))
|
|
61
|
+
.map(([, value]) => JSON.parse(value))
|
|
76
62
|
} catch (ex) {
|
|
77
63
|
console.error(`Error: ${JSON.stringify(ex, null, 2)}`)
|
|
78
64
|
throw ex
|
|
@@ -92,6 +78,46 @@ export class XyoCookieArchivist extends XyoArchivist<XyoCookieArchivistConfig> {
|
|
|
92
78
|
}
|
|
93
79
|
}
|
|
94
80
|
|
|
81
|
+
public override async commit(): Promise<XyoBoundWitness[]> {
|
|
82
|
+
try {
|
|
83
|
+
const payloads = await this.all()
|
|
84
|
+
assertEx(payloads.length > 0, 'Nothing to commit')
|
|
85
|
+
const settled = await Promise.allSettled(
|
|
86
|
+
compact(
|
|
87
|
+
Object.values(this.parents?.commit ?? [])?.map(async (parent) => {
|
|
88
|
+
const queryPayload = PayloadWrapper.parse<XyoArchivistInsertQuery>({
|
|
89
|
+
payloads: payloads.map((payload) => PayloadWrapper.hash(payload)),
|
|
90
|
+
schema: XyoArchivistInsertQuerySchema,
|
|
91
|
+
})
|
|
92
|
+
const query = await this.bindQuery(queryPayload)
|
|
93
|
+
return (await parent?.query(query[0], query[1]))?.[0]
|
|
94
|
+
}),
|
|
95
|
+
),
|
|
96
|
+
)
|
|
97
|
+
await this.clear()
|
|
98
|
+
return compact(
|
|
99
|
+
settled.map((result) => {
|
|
100
|
+
return result.status === 'fulfilled' ? result.value : null
|
|
101
|
+
}),
|
|
102
|
+
)
|
|
103
|
+
} catch (ex) {
|
|
104
|
+
console.error(`Error: ${JSON.stringify(ex, null, 2)}`)
|
|
105
|
+
throw ex
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
public override delete(hashes: string[]): PromisableArray<boolean> {
|
|
110
|
+
try {
|
|
111
|
+
return hashes.map((hash) => {
|
|
112
|
+
Cookies.remove(this.keyFromHash(hash))
|
|
113
|
+
return true
|
|
114
|
+
})
|
|
115
|
+
} catch (ex) {
|
|
116
|
+
console.error(`Error: ${JSON.stringify(ex, null, 2)}`)
|
|
117
|
+
throw ex
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
95
121
|
public async get(hashes: string[]): Promise<XyoPayload[]> {
|
|
96
122
|
try {
|
|
97
123
|
return await Promise.all(
|
|
@@ -129,44 +155,18 @@ export class XyoCookieArchivist extends XyoArchivist<XyoCookieArchivistConfig> {
|
|
|
129
155
|
}
|
|
130
156
|
}
|
|
131
157
|
|
|
132
|
-
public override
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
158
|
+
public override queries() {
|
|
159
|
+
return [
|
|
160
|
+
XyoArchivistAllQuerySchema,
|
|
161
|
+
XyoArchivistDeleteQuerySchema,
|
|
162
|
+
XyoArchivistClearQuerySchema,
|
|
163
|
+
XyoArchivistFindQuerySchema,
|
|
164
|
+
XyoArchivistCommitQuerySchema,
|
|
165
|
+
...super.queries(),
|
|
166
|
+
]
|
|
141
167
|
}
|
|
142
168
|
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
const payloads = await this.all()
|
|
146
|
-
assertEx(payloads.length > 0, 'Nothing to commit')
|
|
147
|
-
const settled = await Promise.allSettled(
|
|
148
|
-
compact(
|
|
149
|
-
Object.values(this.parents?.commit ?? [])?.map(async (parent) => {
|
|
150
|
-
const queryPayload = PayloadWrapper.parse<XyoArchivistInsertQuery>({
|
|
151
|
-
payloads: payloads.map((payload) => PayloadWrapper.hash(payload)),
|
|
152
|
-
schema: XyoArchivistInsertQuerySchema,
|
|
153
|
-
})
|
|
154
|
-
const query = await this.bindQuery(queryPayload)
|
|
155
|
-
return (await parent?.query(query[0], query[1]))?.[0]
|
|
156
|
-
}),
|
|
157
|
-
),
|
|
158
|
-
)
|
|
159
|
-
await this.clear()
|
|
160
|
-
return compact(
|
|
161
|
-
settled.map((result) => {
|
|
162
|
-
return result.status === 'fulfilled' ? result.value : null
|
|
163
|
-
}),
|
|
164
|
-
)
|
|
165
|
-
} catch (ex) {
|
|
166
|
-
console.error(`Error: ${JSON.stringify(ex, null, 2)}`)
|
|
167
|
-
throw ex
|
|
168
|
-
}
|
|
169
|
+
private keyFromHash(hash: string) {
|
|
170
|
+
return `${this.namespace}-${hash}`
|
|
169
171
|
}
|
|
170
|
-
|
|
171
|
-
static override configSchema = XyoCookieArchivistConfigSchema
|
|
172
172
|
}
|
|
@@ -22,51 +22,78 @@ export type XyoMemoryArchivistConfigSchema = 'network.xyo.module.config.archivis
|
|
|
22
22
|
export const XyoMemoryArchivistConfigSchema: XyoMemoryArchivistConfigSchema = 'network.xyo.module.config.archivist.memory'
|
|
23
23
|
|
|
24
24
|
export type XyoMemoryArchivistConfig = XyoArchivistConfig<{
|
|
25
|
-
schema: XyoMemoryArchivistConfigSchema
|
|
26
25
|
max?: number
|
|
26
|
+
schema: XyoMemoryArchivistConfigSchema
|
|
27
27
|
}>
|
|
28
28
|
|
|
29
29
|
export class XyoMemoryArchivist<TConfig extends XyoMemoryArchivistConfig = XyoMemoryArchivistConfig> extends XyoArchivist<TConfig> {
|
|
30
|
-
static override
|
|
31
|
-
|
|
30
|
+
static override configSchema = XyoMemoryArchivistConfigSchema
|
|
31
|
+
|
|
32
|
+
private cache: LruCache<string, XyoPayload>
|
|
33
|
+
|
|
34
|
+
protected constructor(params?: XyoModuleParams<TConfig>) {
|
|
35
|
+
super(params)
|
|
36
|
+
this.cache = new LruCache<string, XyoPayload>({ max: this.max })
|
|
32
37
|
}
|
|
33
38
|
|
|
34
39
|
public get max() {
|
|
35
40
|
return this.config?.max ?? 10000
|
|
36
41
|
}
|
|
37
42
|
|
|
38
|
-
|
|
43
|
+
static override async create(params?: XyoModuleParams<XyoMemoryArchivistConfig>): Promise<XyoMemoryArchivist> {
|
|
44
|
+
return (await super.create(params)) as XyoMemoryArchivist
|
|
45
|
+
}
|
|
39
46
|
|
|
40
|
-
public override
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
...super.queries(),
|
|
48
|
-
]
|
|
47
|
+
public override all(): PromisableArray<XyoPayload> {
|
|
48
|
+
try {
|
|
49
|
+
return this.cache.dump().map((value) => value[1].value)
|
|
50
|
+
} catch (ex) {
|
|
51
|
+
console.error(`Error: ${JSON.stringify(ex, null, 2)}`)
|
|
52
|
+
throw ex
|
|
53
|
+
}
|
|
49
54
|
}
|
|
50
55
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
56
|
+
public override clear(): void | Promise<void> {
|
|
57
|
+
try {
|
|
58
|
+
this.cache.clear()
|
|
59
|
+
} catch (ex) {
|
|
60
|
+
console.error(`Error: ${JSON.stringify(ex, null, 2)}`)
|
|
61
|
+
throw ex
|
|
62
|
+
}
|
|
54
63
|
}
|
|
55
64
|
|
|
56
|
-
public override
|
|
65
|
+
public override async commit(): Promise<XyoBoundWitness[]> {
|
|
57
66
|
try {
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
67
|
+
const payloads = assertEx(await this.all(), 'Nothing to commit')
|
|
68
|
+
const settled = await Promise.allSettled(
|
|
69
|
+
compact(
|
|
70
|
+
Object.values(this.parents?.commit ?? [])?.map(async (parent) => {
|
|
71
|
+
const queryPayload = PayloadWrapper.parse<XyoArchivistInsertQuery>({
|
|
72
|
+
payloads: payloads.map((payload) => PayloadWrapper.hash(payload)),
|
|
73
|
+
schema: XyoArchivistInsertQuerySchema,
|
|
74
|
+
})
|
|
75
|
+
const query = await this.bindQuery(queryPayload)
|
|
76
|
+
return (await parent?.query(query[0], query[1]))?.[0]
|
|
77
|
+
}),
|
|
78
|
+
),
|
|
79
|
+
)
|
|
80
|
+
await this.clear()
|
|
81
|
+
return compact(
|
|
82
|
+
settled.map((result) => {
|
|
83
|
+
return result.status === 'fulfilled' ? result.value : null
|
|
84
|
+
}),
|
|
85
|
+
)
|
|
61
86
|
} catch (ex) {
|
|
62
87
|
console.error(`Error: ${JSON.stringify(ex, null, 2)}`)
|
|
63
88
|
throw ex
|
|
64
89
|
}
|
|
65
90
|
}
|
|
66
91
|
|
|
67
|
-
public override
|
|
92
|
+
public override delete(hashes: string[]): PromisableArray<boolean> {
|
|
68
93
|
try {
|
|
69
|
-
|
|
94
|
+
return hashes.map((hash) => {
|
|
95
|
+
return this.cache.delete(hash)
|
|
96
|
+
})
|
|
70
97
|
} catch (ex) {
|
|
71
98
|
console.error(`Error: ${JSON.stringify(ex, null, 2)}`)
|
|
72
99
|
throw ex
|
|
@@ -112,40 +139,14 @@ export class XyoMemoryArchivist<TConfig extends XyoMemoryArchivistConfig = XyoMe
|
|
|
112
139
|
}
|
|
113
140
|
}
|
|
114
141
|
|
|
115
|
-
public override
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
public override async commit(): Promise<XyoBoundWitness[]> {
|
|
125
|
-
try {
|
|
126
|
-
const payloads = assertEx(await this.all(), 'Nothing to commit')
|
|
127
|
-
const settled = await Promise.allSettled(
|
|
128
|
-
compact(
|
|
129
|
-
Object.values(this.parents?.commit ?? [])?.map(async (parent) => {
|
|
130
|
-
const queryPayload = PayloadWrapper.parse<XyoArchivistInsertQuery>({
|
|
131
|
-
payloads: payloads.map((payload) => PayloadWrapper.hash(payload)),
|
|
132
|
-
schema: XyoArchivistInsertQuerySchema,
|
|
133
|
-
})
|
|
134
|
-
const query = await this.bindQuery(queryPayload)
|
|
135
|
-
return (await parent?.query(query[0], query[1]))?.[0]
|
|
136
|
-
}),
|
|
137
|
-
),
|
|
138
|
-
)
|
|
139
|
-
await this.clear()
|
|
140
|
-
return compact(
|
|
141
|
-
settled.map((result) => {
|
|
142
|
-
return result.status === 'fulfilled' ? result.value : null
|
|
143
|
-
}),
|
|
144
|
-
)
|
|
145
|
-
} catch (ex) {
|
|
146
|
-
console.error(`Error: ${JSON.stringify(ex, null, 2)}`)
|
|
147
|
-
throw ex
|
|
148
|
-
}
|
|
142
|
+
public override queries() {
|
|
143
|
+
return [
|
|
144
|
+
XyoArchivistAllQuerySchema,
|
|
145
|
+
XyoArchivistDeleteQuerySchema,
|
|
146
|
+
XyoArchivistClearQuerySchema,
|
|
147
|
+
XyoArchivistFindQuerySchema,
|
|
148
|
+
XyoArchivistCommitQuerySchema,
|
|
149
|
+
...super.queries(),
|
|
150
|
+
]
|
|
149
151
|
}
|
|
150
|
-
static override configSchema = XyoMemoryArchivistConfigSchema
|
|
151
152
|
}
|