@xyo-network/archivist 2.32.0-rc.1 → 2.32.0-rc.2
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 +5 -4
- package/dist/cjs/Archivist.d.ts.map +1 -1
- package/dist/cjs/XyoArchivist.d.ts +24 -3
- package/dist/cjs/XyoArchivist.d.ts.map +1 -1
- package/dist/cjs/XyoArchivist.js +44 -2
- package/dist/cjs/XyoArchivist.js.map +1 -1
- package/dist/cjs/XyoArchivistWrapper.d.ts +3 -2
- package/dist/cjs/XyoArchivistWrapper.d.ts.map +1 -1
- package/dist/cjs/XyoArchivistWrapper.js +2 -2
- package/dist/cjs/XyoArchivistWrapper.js.map +1 -1
- package/dist/cjs/XyoCookieArchivist.d.ts +33 -0
- package/dist/cjs/XyoCookieArchivist.d.ts.map +1 -0
- package/dist/cjs/XyoCookieArchivist.js +158 -0
- package/dist/cjs/XyoCookieArchivist.js.map +1 -0
- package/dist/cjs/XyoMemoryArchivist.d.ts +4 -9
- package/dist/cjs/XyoMemoryArchivist.d.ts.map +1 -1
- package/dist/cjs/XyoMemoryArchivist.js +25 -28
- package/dist/cjs/XyoMemoryArchivist.js.map +1 -1
- package/dist/cjs/XyoStorageArchivist.d.ts +33 -0
- package/dist/cjs/XyoStorageArchivist.d.ts.map +1 -0
- package/dist/cjs/XyoStorageArchivist.js +150 -0
- package/dist/cjs/XyoStorageArchivist.js.map +1 -0
- package/dist/cjs/index.d.ts +3 -0
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +3 -0
- package/dist/cjs/index.js.map +1 -1
- package/dist/docs.json +12661 -4393
- package/dist/esm/Archivist.d.ts +5 -4
- package/dist/esm/Archivist.d.ts.map +1 -1
- package/dist/esm/XyoArchivist.d.ts +24 -3
- package/dist/esm/XyoArchivist.d.ts.map +1 -1
- package/dist/esm/XyoArchivist.js +32 -2
- package/dist/esm/XyoArchivist.js.map +1 -1
- package/dist/esm/XyoArchivistWrapper.d.ts +3 -2
- package/dist/esm/XyoArchivistWrapper.d.ts.map +1 -1
- package/dist/esm/XyoArchivistWrapper.js +2 -2
- package/dist/esm/XyoArchivistWrapper.js.map +1 -1
- package/dist/esm/XyoCookieArchivist.d.ts +33 -0
- package/dist/esm/XyoCookieArchivist.d.ts.map +1 -0
- package/dist/esm/XyoCookieArchivist.js +139 -0
- package/dist/esm/XyoCookieArchivist.js.map +1 -0
- package/dist/esm/XyoMemoryArchivist.d.ts +4 -9
- package/dist/esm/XyoMemoryArchivist.d.ts.map +1 -1
- package/dist/esm/XyoMemoryArchivist.js +15 -16
- package/dist/esm/XyoMemoryArchivist.js.map +1 -1
- package/dist/esm/XyoStorageArchivist.d.ts +33 -0
- package/dist/esm/XyoStorageArchivist.d.ts.map +1 -0
- package/dist/esm/XyoStorageArchivist.js +132 -0
- package/dist/esm/XyoStorageArchivist.js.map +1 -0
- package/dist/esm/index.d.ts +3 -0
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +3 -0
- package/dist/esm/index.js.map +1 -1
- package/package.json +10 -5
- package/src/Archivist.ts +6 -5
- package/src/XyoArchivist.ts +48 -5
- package/src/XyoArchivistWrapper.ts +5 -4
- package/src/XyoCookieArchivist.ts +176 -0
- package/src/XyoMemoryArchivist.ts +17 -26
- package/src/XyoStorageArchivist.ts +169 -0
- package/src/index.ts +3 -0
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import { assertEx } from '@xylabs/sdk-js'
|
|
2
|
+
import { XyoBoundWitness } from '@xyo-network/boundwitness'
|
|
3
|
+
import { XyoPayload, XyoPayloadWrapper } from '@xyo-network/payload'
|
|
4
|
+
import { PromisableArray } from '@xyo-network/promisable'
|
|
5
|
+
import compact from 'lodash/compact'
|
|
6
|
+
import store, { StoreBase } from 'store2'
|
|
7
|
+
|
|
8
|
+
import { XyoArchivistConfig } from './Config'
|
|
9
|
+
import { PartialArchivistConfig } from './PartialArchivistConfig'
|
|
10
|
+
import {
|
|
11
|
+
XyoArchivistAllQuerySchema,
|
|
12
|
+
XyoArchivistClearQuerySchema,
|
|
13
|
+
XyoArchivistCommitQuerySchema,
|
|
14
|
+
XyoArchivistDeleteQuerySchema,
|
|
15
|
+
XyoArchivistFindQuerySchema,
|
|
16
|
+
XyoArchivistInsertQuery,
|
|
17
|
+
XyoArchivistInsertQuerySchema,
|
|
18
|
+
} from './Queries'
|
|
19
|
+
import { XyoArchivist } from './XyoArchivist'
|
|
20
|
+
import { XyoPayloadFindFilter } from './XyoPayloadFindFilter'
|
|
21
|
+
|
|
22
|
+
export type XyoStorageArchivistConfigSchema = 'network.xyo.module.config.archivist.storage'
|
|
23
|
+
export const XyoStorageArchivistConfigSchema: XyoStorageArchivistConfigSchema = 'network.xyo.module.config.archivist.storage'
|
|
24
|
+
|
|
25
|
+
export type XyoStorageArchivistConfig = XyoArchivistConfig<{
|
|
26
|
+
schema: XyoStorageArchivistConfigSchema
|
|
27
|
+
type?: 'local' | 'session' | 'page'
|
|
28
|
+
namespace?: string
|
|
29
|
+
maxEntries?: number
|
|
30
|
+
maxEntrySize?: number
|
|
31
|
+
}>
|
|
32
|
+
|
|
33
|
+
class StorageArchivistError extends Error {
|
|
34
|
+
constructor(action: string, error: Error['cause'], message?: string) {
|
|
35
|
+
super(`Storage Archivist [${action}] failed${message ? ` (${message})` : ''}`, { cause: error })
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export class XyoStorageArchivist extends XyoArchivist<XyoStorageArchivistConfig> {
|
|
40
|
+
public get type() {
|
|
41
|
+
return this.config?.type ?? 'local'
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
public get namespace() {
|
|
45
|
+
return this.config?.namespace ?? 'xyoarch'
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
public get maxEntries() {
|
|
49
|
+
return this.config?.maxEntries ?? 1000
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
public get maxEntrySize() {
|
|
53
|
+
return this.config?.maxEntries ?? 16000
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
public override get queries() {
|
|
57
|
+
return [
|
|
58
|
+
...super.queries,
|
|
59
|
+
XyoArchivistAllQuerySchema,
|
|
60
|
+
XyoArchivistDeleteQuerySchema,
|
|
61
|
+
XyoArchivistClearQuerySchema,
|
|
62
|
+
XyoArchivistFindQuerySchema,
|
|
63
|
+
XyoArchivistCommitQuerySchema,
|
|
64
|
+
]
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
private storage: StoreBase
|
|
68
|
+
|
|
69
|
+
constructor(config?: PartialArchivistConfig<XyoStorageArchivistConfig>) {
|
|
70
|
+
super({ ...config, schema: XyoStorageArchivistConfigSchema })
|
|
71
|
+
this.storage = store[this.type].namespace(this.namespace)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
public delete(hashes: string[]): PromisableArray<boolean> {
|
|
75
|
+
try {
|
|
76
|
+
return hashes.map((hash) => {
|
|
77
|
+
this.storage.remove(hash)
|
|
78
|
+
return true
|
|
79
|
+
})
|
|
80
|
+
} catch (ex) {
|
|
81
|
+
throw new StorageArchivistError('delete', ex, 'unexpected')
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
public override clear(): void | Promise<void> {
|
|
86
|
+
try {
|
|
87
|
+
this.storage.clear()
|
|
88
|
+
} catch (ex) {
|
|
89
|
+
throw new StorageArchivistError('clear', ex, 'unexpected')
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
public async get(hashes: string[]): Promise<(XyoPayload | null)[]> {
|
|
94
|
+
try {
|
|
95
|
+
return await Promise.all(
|
|
96
|
+
hashes.map(async (hash) => {
|
|
97
|
+
const cookieString = this.storage.get(hash)
|
|
98
|
+
return cookieString ? JSON.parse(cookieString) : (await this.getFromParents(hash)) ?? null
|
|
99
|
+
}),
|
|
100
|
+
)
|
|
101
|
+
} catch (ex) {
|
|
102
|
+
throw new StorageArchivistError('get', ex, 'unexpected')
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
public async insert(payloads: XyoPayload[]): Promise<XyoBoundWitness> {
|
|
107
|
+
try {
|
|
108
|
+
const storedPayloads = payloads.map((payload) => {
|
|
109
|
+
const wrapper = new XyoPayloadWrapper(payload)
|
|
110
|
+
const hash = wrapper.hash
|
|
111
|
+
const value = JSON.stringify(wrapper.payload)
|
|
112
|
+
assertEx(value.length < this.maxEntrySize, `Payload too large [${wrapper.hash}, ${value.length}]`)
|
|
113
|
+
this.storage.set(hash, JSON.stringify(wrapper.payload))
|
|
114
|
+
return wrapper.payload
|
|
115
|
+
})
|
|
116
|
+
const boundwitness = this.bindPayloads(storedPayloads)
|
|
117
|
+
if (this.writeThrough) {
|
|
118
|
+
await this.writeToParents([boundwitness, ...storedPayloads])
|
|
119
|
+
}
|
|
120
|
+
return boundwitness
|
|
121
|
+
} catch (ex) {
|
|
122
|
+
throw new StorageArchivistError('insert', ex, 'unexpected')
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
public async find(filter: XyoPayloadFindFilter): Promise<XyoPayload[]> {
|
|
127
|
+
try {
|
|
128
|
+
const x = (await this.all()).filter((payload) => {
|
|
129
|
+
if (filter.schema && filter.schema !== payload.schema) {
|
|
130
|
+
return false
|
|
131
|
+
}
|
|
132
|
+
return true
|
|
133
|
+
})
|
|
134
|
+
return x
|
|
135
|
+
} catch (ex) {
|
|
136
|
+
throw new StorageArchivistError('find', ex, 'unexpected')
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
public all(): PromisableArray<XyoPayload> {
|
|
141
|
+
try {
|
|
142
|
+
return Object.entries(this.storage.getAll())
|
|
143
|
+
.filter(([key]) => key.startsWith(`${this.namespace}-`))
|
|
144
|
+
.map(([, value]) => JSON.parse(value))
|
|
145
|
+
} catch (ex) {
|
|
146
|
+
throw new StorageArchivistError('all', ex, 'unexpected')
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
public async commit(): Promise<XyoBoundWitness> {
|
|
151
|
+
try {
|
|
152
|
+
const payloads = await this.all()
|
|
153
|
+
assertEx(payloads.length > 0, 'Nothing to commit')
|
|
154
|
+
const block = this.bindPayloads(payloads)
|
|
155
|
+
await Promise.allSettled(
|
|
156
|
+
compact(
|
|
157
|
+
Object.values(this.parents?.commit ?? [])?.map(async (parent) => {
|
|
158
|
+
const query: XyoArchivistInsertQuery = { payloads: [block, ...payloads], schema: XyoArchivistInsertQuerySchema }
|
|
159
|
+
return await parent?.query(query)
|
|
160
|
+
}),
|
|
161
|
+
),
|
|
162
|
+
)
|
|
163
|
+
await this.clear()
|
|
164
|
+
return block
|
|
165
|
+
} catch (ex) {
|
|
166
|
+
throw new StorageArchivistError('commit', ex, 'unexpected')
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
export * from './Archivist'
|
|
2
2
|
export * from './Config'
|
|
3
|
+
export * from './PartialArchivistConfig'
|
|
3
4
|
export * from './Queries'
|
|
4
5
|
export * from './XyoArchivist'
|
|
5
6
|
export * from './XyoArchivistWrapper'
|
|
7
|
+
export * from './XyoCookieArchivist'
|
|
6
8
|
export * from './XyoMemoryArchivist'
|
|
7
9
|
export * from './XyoPayloadFindFilter'
|
|
10
|
+
export * from './XyoStorageArchivist'
|