@xyo-network/archivist 2.85.4 → 2.85.6
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 +3 -282
- package/dist/browser/index.cjs.map +1 -1
- package/dist/browser/index.d.cts +3 -3
- package/dist/browser/index.d.cts.map +1 -1
- package/dist/browser/index.d.mts +3 -3
- package/dist/browser/index.d.mts.map +1 -1
- package/dist/browser/index.d.ts +3 -3
- package/dist/browser/index.d.ts.map +1 -1
- package/dist/browser/index.js +3 -268
- package/dist/browser/index.js.map +1 -1
- package/dist/node/index.cjs +7 -307
- package/dist/node/index.cjs.map +1 -1
- package/dist/node/index.d.cts +3 -3
- package/dist/node/index.d.cts.map +1 -1
- package/dist/node/index.d.mts +3 -3
- package/dist/node/index.d.mts.map +1 -1
- package/dist/node/index.d.ts +3 -3
- package/dist/node/index.d.ts.map +1 -1
- package/dist/node/index.js +3 -287
- package/dist/node/index.js.map +1 -1
- package/package.json +7 -21
- package/src/index.ts +3 -3
- package/dist/browser/CookieArchivist.d.cts +0 -32
- package/dist/browser/CookieArchivist.d.cts.map +0 -1
- package/dist/browser/CookieArchivist.d.mts +0 -32
- package/dist/browser/CookieArchivist.d.mts.map +0 -1
- package/dist/browser/CookieArchivist.d.ts +0 -32
- package/dist/browser/CookieArchivist.d.ts.map +0 -1
- package/dist/browser/StorageArchivist.d.cts +0 -39
- package/dist/browser/StorageArchivist.d.cts.map +0 -1
- package/dist/browser/StorageArchivist.d.mts +0 -39
- package/dist/browser/StorageArchivist.d.mts.map +0 -1
- package/dist/browser/StorageArchivist.d.ts +0 -39
- package/dist/browser/StorageArchivist.d.ts.map +0 -1
- package/dist/node/CookieArchivist.d.cts +0 -32
- package/dist/node/CookieArchivist.d.cts.map +0 -1
- package/dist/node/CookieArchivist.d.mts +0 -32
- package/dist/node/CookieArchivist.d.mts.map +0 -1
- package/dist/node/CookieArchivist.d.ts +0 -32
- package/dist/node/CookieArchivist.d.ts.map +0 -1
- package/dist/node/StorageArchivist.d.cts +0 -39
- package/dist/node/StorageArchivist.d.cts.map +0 -1
- package/dist/node/StorageArchivist.d.mts +0 -39
- package/dist/node/StorageArchivist.d.mts.map +0 -1
- package/dist/node/StorageArchivist.d.ts +0 -39
- package/dist/node/StorageArchivist.d.ts.map +0 -1
- package/src/CookieArchivist.ts +0 -164
- package/src/StorageArchivist.ts +0 -191
- package/src/spec/testArchivist.ts +0 -50
package/src/StorageArchivist.ts
DELETED
|
@@ -1,191 +0,0 @@
|
|
|
1
|
-
import { assertEx } from '@xylabs/assert'
|
|
2
|
-
import { compact } from '@xylabs/lodash'
|
|
3
|
-
import { fulfilled, Promisable, PromisableArray } from '@xylabs/promise'
|
|
4
|
-
import { AbstractArchivist } from '@xyo-network/archivist-abstract'
|
|
5
|
-
import {
|
|
6
|
-
ArchivistAllQuerySchema,
|
|
7
|
-
ArchivistClearQuerySchema,
|
|
8
|
-
ArchivistCommitQuerySchema,
|
|
9
|
-
ArchivistConfig,
|
|
10
|
-
ArchivistDeleteQuerySchema,
|
|
11
|
-
ArchivistInsertQuery,
|
|
12
|
-
ArchivistInsertQuerySchema,
|
|
13
|
-
ArchivistInstance,
|
|
14
|
-
ArchivistModuleEventData,
|
|
15
|
-
ArchivistParams,
|
|
16
|
-
} from '@xyo-network/archivist-model'
|
|
17
|
-
import { BoundWitness } from '@xyo-network/boundwitness-model'
|
|
18
|
-
import { PayloadHasher } from '@xyo-network/hash'
|
|
19
|
-
import { AnyConfigSchema } from '@xyo-network/module-model'
|
|
20
|
-
import { Payload } from '@xyo-network/payload-model'
|
|
21
|
-
import { PayloadWrapper } from '@xyo-network/payload-wrapper'
|
|
22
|
-
import store, { StoreBase } from 'store2'
|
|
23
|
-
|
|
24
|
-
export type StorageArchivistConfigSchema = 'network.xyo.archivist.storage.config'
|
|
25
|
-
export const StorageArchivistConfigSchema: StorageArchivistConfigSchema = 'network.xyo.archivist.storage.config'
|
|
26
|
-
|
|
27
|
-
export type StorageArchivistConfig = ArchivistConfig<{
|
|
28
|
-
maxEntries?: number
|
|
29
|
-
maxEntrySize?: number
|
|
30
|
-
namespace?: string
|
|
31
|
-
persistAccount?: boolean
|
|
32
|
-
schema: StorageArchivistConfigSchema
|
|
33
|
-
type?: 'local' | 'session' | 'page'
|
|
34
|
-
}>
|
|
35
|
-
|
|
36
|
-
export type StorageArchivistParams = ArchivistParams<AnyConfigSchema<StorageArchivistConfig>>
|
|
37
|
-
export class StorageArchivist<
|
|
38
|
-
TParams extends StorageArchivistParams = StorageArchivistParams,
|
|
39
|
-
TEventData extends ArchivistModuleEventData = ArchivistModuleEventData,
|
|
40
|
-
>
|
|
41
|
-
extends AbstractArchivist<TParams, TEventData>
|
|
42
|
-
implements ArchivistInstance
|
|
43
|
-
{
|
|
44
|
-
static override configSchemas = [StorageArchivistConfigSchema]
|
|
45
|
-
|
|
46
|
-
private _privateStorage: StoreBase | undefined
|
|
47
|
-
private _storage: StoreBase | undefined
|
|
48
|
-
|
|
49
|
-
get maxEntries() {
|
|
50
|
-
return this.config?.maxEntries ?? 1000
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
get maxEntrySize() {
|
|
54
|
-
return this.config?.maxEntrySize ?? 16_000
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
get namespace() {
|
|
58
|
-
return this.config?.namespace ?? 'xyo-archivist'
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
get persistAccount() {
|
|
62
|
-
return this.config?.persistAccount ?? false
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
override get queries(): string[] {
|
|
66
|
-
return [
|
|
67
|
-
ArchivistAllQuerySchema,
|
|
68
|
-
ArchivistDeleteQuerySchema,
|
|
69
|
-
ArchivistClearQuerySchema,
|
|
70
|
-
ArchivistInsertQuerySchema,
|
|
71
|
-
ArchivistCommitQuerySchema,
|
|
72
|
-
...super.queries,
|
|
73
|
-
]
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
get type() {
|
|
77
|
-
return this.config?.type ?? 'local'
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/* This has to be a getter so that it can access it during construction */
|
|
81
|
-
private get privateStorage(): StoreBase {
|
|
82
|
-
this._privateStorage = this._storage ?? store[this.type].namespace(`${this.namespace}|private`)
|
|
83
|
-
return this._privateStorage
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
/* This has to be a getter so that it can access it during construction */
|
|
87
|
-
private get storage(): StoreBase {
|
|
88
|
-
this._storage = this._storage ?? store[this.type].namespace(this.namespace)
|
|
89
|
-
return this._storage
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
/*override async loadAccount(account?: AccountInstance, persistAccount?: boolean, privateStorage?: StoreBase, _logger?: Logger) {
|
|
93
|
-
if (!this._account) {
|
|
94
|
-
if (persistAccount) {
|
|
95
|
-
const privateKey = privateStorage?.get('privateKey')
|
|
96
|
-
if (privateKey) {
|
|
97
|
-
try {
|
|
98
|
-
this._account = await Account.create({ privateKey })
|
|
99
|
-
return this._account
|
|
100
|
-
} catch (ex) {
|
|
101
|
-
console.error(`Error reading Account from storage [${ex}] - Recreating Account`)
|
|
102
|
-
privateStorage?.remove('privateKey')
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
return await super.loadAccount()
|
|
108
|
-
}*/
|
|
109
|
-
|
|
110
|
-
protected override allHandler(): PromisableArray<Payload> {
|
|
111
|
-
this.logger?.log(`this.storage.length: ${this.storage.length}`)
|
|
112
|
-
return Object.entries(this.storage.getAll()).map(([, value]) => value)
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
protected override clearHandler(): void | Promise<void> {
|
|
116
|
-
this.logger?.log(`this.storage.length: ${this.storage.length}`)
|
|
117
|
-
this.storage.clear()
|
|
118
|
-
return this.emit('cleared', { module: this })
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
protected override async commitHandler(): Promise<BoundWitness[]> {
|
|
122
|
-
this.logger?.log(`this.storage.length: ${this.storage.length}`)
|
|
123
|
-
const payloads = await this.all()
|
|
124
|
-
assertEx(payloads.length > 0, 'Nothing to commit')
|
|
125
|
-
const settled = await Promise.allSettled(
|
|
126
|
-
compact(
|
|
127
|
-
Object.values((await this.parents()).commit ?? [])?.map(async (parent) => {
|
|
128
|
-
const queryPayload: ArchivistInsertQuery = {
|
|
129
|
-
schema: ArchivistInsertQuerySchema,
|
|
130
|
-
}
|
|
131
|
-
const query = await this.bindQuery(queryPayload, payloads)
|
|
132
|
-
return (await parent?.query(query[0], query[1]))?.[0]
|
|
133
|
-
}),
|
|
134
|
-
),
|
|
135
|
-
)
|
|
136
|
-
// TODO - rather than clear, delete the payloads that come back as successfully inserted
|
|
137
|
-
await this.clear()
|
|
138
|
-
return compact(settled.filter(fulfilled).map((result) => result.value))
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
protected override async deleteHandler(hashes: string[]): Promise<string[]> {
|
|
142
|
-
const payloadPairs: [string, Payload][] = await Promise.all(
|
|
143
|
-
(await this.get(hashes)).map<Promise<[string, Payload]>>(async (payload) => [await PayloadHasher.hashAsync(payload), payload]),
|
|
144
|
-
)
|
|
145
|
-
const deletedPairs: [string, Payload][] = compact(
|
|
146
|
-
await Promise.all(
|
|
147
|
-
payloadPairs.map<[string, Payload] | undefined>(([hash, payload]) => {
|
|
148
|
-
this.storage.remove(hash)
|
|
149
|
-
return [hash, payload]
|
|
150
|
-
}),
|
|
151
|
-
),
|
|
152
|
-
)
|
|
153
|
-
return deletedPairs.map(([hash]) => hash)
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
protected override getHandler(hashes: string[]): Promisable<Payload[]> {
|
|
157
|
-
return compact(
|
|
158
|
-
hashes.map((hash) => {
|
|
159
|
-
return this.storage.get(hash)
|
|
160
|
-
}),
|
|
161
|
-
)
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
protected override async insertHandler(payloads: Payload[]): Promise<Payload[]> {
|
|
165
|
-
const resultPayloads = await Promise.all(
|
|
166
|
-
payloads.map(async (payload) => {
|
|
167
|
-
const wrapper = PayloadWrapper.wrap(payload)
|
|
168
|
-
const hash = await wrapper.hashAsync()
|
|
169
|
-
const value = JSON.stringify(wrapper.payload())
|
|
170
|
-
assertEx(value.length < this.maxEntrySize, `Payload too large [${hash}, ${value.length}]`)
|
|
171
|
-
this.storage.set(hash, wrapper.payload())
|
|
172
|
-
return wrapper.payload()
|
|
173
|
-
}),
|
|
174
|
-
)
|
|
175
|
-
return resultPayloads
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
protected saveAccount() {
|
|
179
|
-
if (this.persistAccount) {
|
|
180
|
-
const account = this.account
|
|
181
|
-
this.logger?.log(account.address)
|
|
182
|
-
this.privateStorage.set('privateKey', account.private.hex)
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
protected override async startHandler() {
|
|
187
|
-
await super.startHandler()
|
|
188
|
-
this.saveAccount()
|
|
189
|
-
return true
|
|
190
|
-
}
|
|
191
|
-
}
|
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @jest-environment jsdom
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
import { delay } from '@xylabs/delay'
|
|
6
|
-
import { Promisable } from '@xylabs/promise'
|
|
7
|
-
import { ArchivistInstance } from '@xyo-network/archivist-model'
|
|
8
|
-
import { IdSchema } from '@xyo-network/id-payload-plugin'
|
|
9
|
-
import { Payload } from '@xyo-network/payload-model'
|
|
10
|
-
import { PayloadWrapper } from '@xyo-network/payload-wrapper'
|
|
11
|
-
|
|
12
|
-
export const testArchivistRoundTrip = (archivistPromise: Promisable<ArchivistInstance>, name: string) => {
|
|
13
|
-
test(`Archivist RoundTrip [${name}]`, async () => {
|
|
14
|
-
const idPayload: Payload<{ salt: string }> = {
|
|
15
|
-
salt: Date.now().toString(),
|
|
16
|
-
schema: IdSchema,
|
|
17
|
-
}
|
|
18
|
-
const payloadWrapper = PayloadWrapper.wrap(idPayload)
|
|
19
|
-
|
|
20
|
-
const archivist = await archivistPromise
|
|
21
|
-
const insertResult = await archivist.insert([idPayload])
|
|
22
|
-
expect(insertResult).toBeDefined()
|
|
23
|
-
|
|
24
|
-
const getResult = await archivist.get([await payloadWrapper.hashAsync()])
|
|
25
|
-
expect(getResult).toBeDefined()
|
|
26
|
-
expect(getResult.length).toBe(1)
|
|
27
|
-
const gottenPayload = getResult[0]
|
|
28
|
-
if (gottenPayload) {
|
|
29
|
-
const gottenPayloadWrapper = PayloadWrapper.wrap(gottenPayload)
|
|
30
|
-
expect(await gottenPayloadWrapper.hashAsync()).toBe(await payloadWrapper.hashAsync())
|
|
31
|
-
}
|
|
32
|
-
})
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export const testArchivistAll = (archivist: Promisable<ArchivistInstance>, name: string) => {
|
|
36
|
-
test(`Archivist All [${name}]`, async () => {
|
|
37
|
-
const idPayload = {
|
|
38
|
-
salt: Date.now().toString(),
|
|
39
|
-
schema: IdSchema,
|
|
40
|
-
}
|
|
41
|
-
const archivistModule = await archivist
|
|
42
|
-
for (let x = 0; x < 10; x++) {
|
|
43
|
-
await archivistModule.insert([idPayload])
|
|
44
|
-
await delay(10)
|
|
45
|
-
}
|
|
46
|
-
const getResult = await archivistModule.all?.()
|
|
47
|
-
expect(getResult).toBeDefined()
|
|
48
|
-
expect(getResult?.length).toBe(2)
|
|
49
|
-
})
|
|
50
|
-
}
|