@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.
Files changed (61) hide show
  1. package/dist/cjs/Archivist.d.ts +5 -4
  2. package/dist/cjs/Archivist.d.ts.map +1 -1
  3. package/dist/cjs/XyoArchivist.d.ts +24 -3
  4. package/dist/cjs/XyoArchivist.d.ts.map +1 -1
  5. package/dist/cjs/XyoArchivist.js +44 -2
  6. package/dist/cjs/XyoArchivist.js.map +1 -1
  7. package/dist/cjs/XyoArchivistWrapper.d.ts +3 -2
  8. package/dist/cjs/XyoArchivistWrapper.d.ts.map +1 -1
  9. package/dist/cjs/XyoArchivistWrapper.js +2 -2
  10. package/dist/cjs/XyoArchivistWrapper.js.map +1 -1
  11. package/dist/cjs/XyoCookieArchivist.d.ts +33 -0
  12. package/dist/cjs/XyoCookieArchivist.d.ts.map +1 -0
  13. package/dist/cjs/XyoCookieArchivist.js +158 -0
  14. package/dist/cjs/XyoCookieArchivist.js.map +1 -0
  15. package/dist/cjs/XyoMemoryArchivist.d.ts +4 -9
  16. package/dist/cjs/XyoMemoryArchivist.d.ts.map +1 -1
  17. package/dist/cjs/XyoMemoryArchivist.js +25 -28
  18. package/dist/cjs/XyoMemoryArchivist.js.map +1 -1
  19. package/dist/cjs/XyoStorageArchivist.d.ts +33 -0
  20. package/dist/cjs/XyoStorageArchivist.d.ts.map +1 -0
  21. package/dist/cjs/XyoStorageArchivist.js +150 -0
  22. package/dist/cjs/XyoStorageArchivist.js.map +1 -0
  23. package/dist/cjs/index.d.ts +3 -0
  24. package/dist/cjs/index.d.ts.map +1 -1
  25. package/dist/cjs/index.js +3 -0
  26. package/dist/cjs/index.js.map +1 -1
  27. package/dist/docs.json +12661 -4393
  28. package/dist/esm/Archivist.d.ts +5 -4
  29. package/dist/esm/Archivist.d.ts.map +1 -1
  30. package/dist/esm/XyoArchivist.d.ts +24 -3
  31. package/dist/esm/XyoArchivist.d.ts.map +1 -1
  32. package/dist/esm/XyoArchivist.js +32 -2
  33. package/dist/esm/XyoArchivist.js.map +1 -1
  34. package/dist/esm/XyoArchivistWrapper.d.ts +3 -2
  35. package/dist/esm/XyoArchivistWrapper.d.ts.map +1 -1
  36. package/dist/esm/XyoArchivistWrapper.js +2 -2
  37. package/dist/esm/XyoArchivistWrapper.js.map +1 -1
  38. package/dist/esm/XyoCookieArchivist.d.ts +33 -0
  39. package/dist/esm/XyoCookieArchivist.d.ts.map +1 -0
  40. package/dist/esm/XyoCookieArchivist.js +139 -0
  41. package/dist/esm/XyoCookieArchivist.js.map +1 -0
  42. package/dist/esm/XyoMemoryArchivist.d.ts +4 -9
  43. package/dist/esm/XyoMemoryArchivist.d.ts.map +1 -1
  44. package/dist/esm/XyoMemoryArchivist.js +15 -16
  45. package/dist/esm/XyoMemoryArchivist.js.map +1 -1
  46. package/dist/esm/XyoStorageArchivist.d.ts +33 -0
  47. package/dist/esm/XyoStorageArchivist.d.ts.map +1 -0
  48. package/dist/esm/XyoStorageArchivist.js +132 -0
  49. package/dist/esm/XyoStorageArchivist.js.map +1 -0
  50. package/dist/esm/index.d.ts +3 -0
  51. package/dist/esm/index.d.ts.map +1 -1
  52. package/dist/esm/index.js +3 -0
  53. package/dist/esm/index.js.map +1 -1
  54. package/package.json +10 -5
  55. package/src/Archivist.ts +6 -5
  56. package/src/XyoArchivist.ts +48 -5
  57. package/src/XyoArchivistWrapper.ts +5 -4
  58. package/src/XyoCookieArchivist.ts +176 -0
  59. package/src/XyoMemoryArchivist.ts +17 -26
  60. package/src/XyoStorageArchivist.ts +169 -0
  61. 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'