@seedprotocol/sdk 0.1.47 → 0.1.49

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 (141) hide show
  1. package/dist/bin.js.map +1 -1
  2. package/dist/constants-BLctWkrn.js.map +1 -1
  3. package/dist/{index-DWf9Ls94.js → index-DMIKRod-.js} +3995 -3995
  4. package/dist/index-DMIKRod-.js.map +1 -0
  5. package/dist/{index-B2WbNudj.js → index-wKss7188.js} +10 -10
  6. package/dist/index-wKss7188.js.map +1 -0
  7. package/dist/main.js +7 -7
  8. package/dist/{seed.schema.config-mBqth17w.js → seed.schema.config-C0M8Rcti.js} +7 -7
  9. package/dist/seed.schema.config-C0M8Rcti.js.map +1 -0
  10. package/dist/src/AppStateSchema.ts +10 -0
  11. package/dist/src/Attestation.ts +21 -0
  12. package/dist/src/ConfigSchema.ts +15 -0
  13. package/dist/src/ItemProperty.ts +383 -0
  14. package/dist/src/MetadataSchema.ts +28 -0
  15. package/dist/src/ModelSchema.ts +46 -0
  16. package/dist/src/ModelUidSchema.ts +16 -0
  17. package/dist/src/PropertyUidSchema.ts +16 -0
  18. package/dist/src/Schema.ts +17 -0
  19. package/dist/src/SeedSchema.ts +29 -0
  20. package/dist/src/VersionSchema.ts +16 -0
  21. package/dist/src/actors.ts +280 -0
  22. package/dist/src/addModelsToDb.ts +150 -0
  23. package/dist/src/allItems.ts +23 -0
  24. package/dist/src/arweave.ts +37 -0
  25. package/dist/src/browser.app.db.config.ts +27 -0
  26. package/dist/src/browser.seed.db.config.ts +33 -0
  27. package/dist/src/browser.ts +30 -0
  28. package/dist/src/checkStatus.ts +50 -0
  29. package/dist/src/client.ts +85 -0
  30. package/dist/src/configureFs.ts +81 -0
  31. package/dist/src/connectToDb.ts +74 -0
  32. package/dist/src/connectionManager.ts +67 -0
  33. package/dist/src/constants.ts +118 -0
  34. package/dist/src/create.ts +39 -0
  35. package/dist/src/createItem.ts +15 -0
  36. package/dist/src/createItemMachine.ts +37 -0
  37. package/dist/src/createPublishAttempt.ts +16 -0
  38. package/dist/src/createSeeds.ts +24 -0
  39. package/dist/src/createVersion.ts +33 -0
  40. package/dist/src/db.ts +247 -0
  41. package/dist/src/dbMachine.ts +181 -0
  42. package/dist/src/deleteItem.ts +19 -0
  43. package/dist/src/download.ts +289 -0
  44. package/dist/src/drizzle.ts +82 -0
  45. package/dist/src/environment.ts +15 -0
  46. package/dist/src/eventBus.ts +5 -0
  47. package/dist/src/events.ts +14 -0
  48. package/dist/src/fetchDataFromEas.ts +90 -0
  49. package/dist/src/fetchDbData.ts +16 -0
  50. package/dist/src/fetchRelatedItems.ts +179 -0
  51. package/dist/src/fetchSeeds.ts +44 -0
  52. package/dist/src/fetchVersions.ts +41 -0
  53. package/dist/src/files.ts +16 -0
  54. package/dist/src/fragment-masking.ts +87 -0
  55. package/dist/src/fsProxy.ts +36 -0
  56. package/dist/src/getItem.ts +189 -0
  57. package/dist/src/getItemProperties.ts +162 -0
  58. package/dist/src/getItems.ts +75 -0
  59. package/dist/src/getMetadata.ts +40 -0
  60. package/dist/src/getModelSchemas.ts +88 -0
  61. package/dist/src/getSchemaForModel.ts +42 -0
  62. package/dist/src/getSeedData.ts +34 -0
  63. package/dist/src/getVersionData.ts +58 -0
  64. package/dist/src/getVersionsForVersionUids.ts +39 -0
  65. package/dist/src/globalMachine.ts +259 -0
  66. package/dist/src/gql.ts +113 -0
  67. package/dist/src/graphql.ts +3201 -0
  68. package/dist/src/helpers.ts +150 -0
  69. package/dist/src/hydrateExistingItem.ts +137 -0
  70. package/dist/src/hydrateFromDb.ts +254 -0
  71. package/dist/src/hydrateNewItem.ts +34 -0
  72. package/dist/src/index.d.ts +5 -0
  73. package/dist/src/index.ts +73 -0
  74. package/dist/src/init.ts +61 -0
  75. package/dist/src/initialize.ts +127 -0
  76. package/dist/src/internalMachine.ts +220 -0
  77. package/dist/src/item.ts +324 -0
  78. package/dist/src/itemMachineAll.ts +158 -0
  79. package/dist/src/itemMachineSingle.ts +175 -0
  80. package/dist/src/loadAppDb.ts +47 -0
  81. package/dist/src/logger.ts +33 -0
  82. package/dist/src/machine.ts +55 -0
  83. package/dist/src/machines.ts +58 -0
  84. package/dist/src/migrate.ts +288 -0
  85. package/dist/src/model.ts +71 -0
  86. package/dist/src/modelClass.ts +19 -0
  87. package/dist/src/node.app.db.config.ts +40 -0
  88. package/dist/src/prepareDb.ts +34 -0
  89. package/dist/src/preparePublishRequestData.ts +81 -0
  90. package/dist/src/processItems.ts +71 -0
  91. package/dist/src/property.ts +161 -0
  92. package/dist/src/propertyMachine.ts +154 -0
  93. package/dist/src/publish.ts +31 -0
  94. package/dist/src/publishMachine.ts +77 -0
  95. package/dist/src/queries.ts +13 -0
  96. package/dist/src/read.ts +174 -0
  97. package/dist/src/recoverDeletedItem.ts +14 -0
  98. package/dist/src/request.ts +54 -0
  99. package/dist/src/requestAll.ts +157 -0
  100. package/dist/src/resolveRelatedValue.ts +348 -0
  101. package/dist/src/resolveRemoteStorage.ts +87 -0
  102. package/dist/src/save.ts +183 -0
  103. package/dist/src/saveConfig.ts +79 -0
  104. package/dist/src/saveDataToDb.ts +145 -0
  105. package/dist/src/saveMetadata.ts +18 -0
  106. package/dist/src/saveValueToDb.ts +94 -0
  107. package/dist/src/seed.schema.config.ts +25 -0
  108. package/dist/src/seed.ts +37 -0
  109. package/dist/src/seedData.ts +0 -0
  110. package/dist/src/seedProtocol.ts +17 -0
  111. package/dist/src/services.ts +359 -0
  112. package/dist/src/sqlWasmClient.ts +88 -0
  113. package/dist/src/syncDbWithEas.ts +686 -0
  114. package/dist/src/trash.ts +29 -0
  115. package/dist/src/ts-to-proto.ts +101 -0
  116. package/dist/src/types.ts +12 -0
  117. package/dist/src/upload.ts +86 -0
  118. package/dist/src/validate.ts +42 -0
  119. package/dist/src/validateInput.ts +33 -0
  120. package/dist/src/validateItemData.ts +20 -0
  121. package/dist/src/waitForDb.ts +23 -0
  122. package/dist/src/wasm.d.ts +8300 -0
  123. package/dist/src/write.ts +366 -0
  124. package/dist/types/src/browser/db/read/getModelSchemas.d.ts.map +1 -1
  125. package/dist/types/src/browser/item/single/actors/hydrateExistingItem.d.ts +3 -1
  126. package/dist/types/src/browser/item/single/actors/hydrateExistingItem.d.ts.map +1 -1
  127. package/dist/types/src/browser/item/single/actors/saveDataToDb.d.ts +3 -3
  128. package/dist/types/src/browser/item/single/itemMachineSingle.d.ts +3 -3
  129. package/dist/types/src/browser/property/ItemProperty.d.ts +7 -7
  130. package/dist/types/src/browser/property/ItemProperty.d.ts.map +1 -1
  131. package/dist/types/src/browser/property/actors/resolveRemoteStorage.d.ts +3 -3
  132. package/dist/types/src/browser/react/trash.d.ts +1 -1
  133. package/dist/types/src/browser/react/trash.d.ts.map +1 -1
  134. package/dist/types/src/browser/services/db/dbMachine.d.ts +6 -6
  135. package/dist/types/src/browser/services/publish/publishMachine.d.ts +17 -17
  136. package/dist/types/src/types/machines.d.ts +4 -0
  137. package/dist/types/src/types/machines.d.ts.map +1 -1
  138. package/package.json +2 -2
  139. package/dist/index-B2WbNudj.js.map +0 -1
  140. package/dist/index-DWf9Ls94.js.map +0 -1
  141. package/dist/seed.schema.config-mBqth17w.js.map +0 -1
@@ -0,0 +1,54 @@
1
+ import { getItem } from '@/browser/db/read/getItem'
2
+ import { eventEmitter } from '@/eventBus'
3
+
4
+ const activeItemRequests = new Set<string>()
5
+
6
+ const queue = new Map<string, Record<string, unknown>>()
7
+
8
+ export const itemRequestHandler = async (event) => {
9
+ const { seedLocalId, seedUid, modelName, eventId } = event
10
+
11
+ if (activeItemRequests.has(seedLocalId)) {
12
+ console.log(
13
+ '[item/events] [itemRequestHandler] already requesting item with queue size',
14
+ queue.size,
15
+ )
16
+ queue.set(seedLocalId, event)
17
+ return
18
+ }
19
+
20
+ const item = await getItem({
21
+ modelName,
22
+ seedLocalId,
23
+ seedUid,
24
+ })
25
+
26
+ if (!item) {
27
+ console.warn(
28
+ `[item/events] [itemRequestHandler] no item for seedLocalId ${seedLocalId} or versionUid ${seedUid}`,
29
+ )
30
+ if (eventId) {
31
+ eventEmitter.emit(`item.${modelName}.${seedLocalId}.response`, {
32
+ item,
33
+ eventId,
34
+ })
35
+ }
36
+ return
37
+ }
38
+
39
+ eventEmitter.emit(`item.${modelName}.${seedLocalId}.response`, {
40
+ item,
41
+ eventId,
42
+ })
43
+
44
+ queue.delete(seedLocalId)
45
+ if (queue.size > 0 && queue.has(seedLocalId)) {
46
+ const nextEvent = queue.get(seedLocalId)
47
+ console.log(
48
+ '[item/events] [itemRequestHandler] taking event from queue with queue size',
49
+ queue.size,
50
+ nextEvent,
51
+ )
52
+ await itemRequestHandler(nextEvent)
53
+ }
54
+ }
@@ -0,0 +1,157 @@
1
+ import { eventEmitter } from '@/eventBus'
2
+ import { Item } from '@/browser/item'
3
+ import { getItemsData } from '@/browser/db/read/getItems'
4
+ import debug from 'debug'
5
+ import { getModel } from '@/browser/stores/modelClass'
6
+
7
+ const logger = debug('app:events:requestAll')
8
+
9
+ const cache = new Map<string, Map<string, Item<any>>>()
10
+
11
+ let modelCount = 0
12
+
13
+ const handleRequestAll = async (event) => {
14
+ const { modelName, eventId } = event
15
+
16
+ let modelItemsCache
17
+
18
+ if (cache.has(modelName)) {
19
+ modelItemsCache = cache.get(modelName)
20
+ }
21
+
22
+ if (modelItemsCache && modelName === 'Identity' && modelCount <= 1) {
23
+ // Send what we have while we update the cache
24
+ eventEmitter.emit(`item.${modelName}.requestAll.response`, {
25
+ items: Array.from(modelItemsCache.values()),
26
+ eventId,
27
+ })
28
+ modelCount++
29
+ }
30
+
31
+ if (!modelItemsCache) {
32
+ modelItemsCache = new Map()
33
+ }
34
+
35
+ const itemsData = await getItemsData({ modelName })
36
+
37
+ const cacheKeys = Array.from(modelItemsCache.keys())
38
+
39
+ const keysInDb = []
40
+
41
+ const returnItems = []
42
+
43
+ const ModelClass = getModel(modelName)
44
+
45
+ for (const itemData of itemsData) {
46
+ returnItems.push(
47
+ await Item.create({
48
+ ...itemData,
49
+ modelName,
50
+ }),
51
+ )
52
+ }
53
+
54
+ if (modelName === 'Identity' && modelCount <= 1) {
55
+ logger(
56
+ `[XXXXXX] [handleRequestAll] ${ModelClass?.originalConstructor.name}:`,
57
+ returnItems.length,
58
+ )
59
+ eventEmitter.emit(`item.${modelName}.requestAll.response`, {
60
+ items: returnItems,
61
+ eventId,
62
+ })
63
+ }
64
+
65
+ for (const returnItem of returnItems) {
66
+ const itemCacheKey = `${returnItem.seedLocalId}_${returnItem.seedUid || ''}`
67
+ keysInDb.push(itemCacheKey)
68
+ const existingCacheKey = cacheKeys.find((ck) => ck.includes(itemCacheKey))
69
+
70
+ if (!existingCacheKey) {
71
+ modelItemsCache.set(itemCacheKey, returnItem)
72
+ continue
73
+ }
74
+
75
+ if (existingCacheKey) {
76
+ const existingItem = modelItemsCache.get(existingCacheKey)
77
+ if (!existingItem) {
78
+ console.error(
79
+ '[events/requestAll] [getItemsDataFromDb] no existingItem for cache key',
80
+ existingCacheKey,
81
+ modelItemsCache,
82
+ )
83
+ continue
84
+ }
85
+ for (const [key, value] of Object.entries(returnItem)) {
86
+ if (key === 'seedLocalId' || key === 'seedUid') {
87
+ continue
88
+ }
89
+ existingItem[key] = value
90
+ }
91
+ modelItemsCache.set(existingCacheKey, existingItem)
92
+ }
93
+ }
94
+
95
+ // Remove any stale items from the cache that were deleted from the DB
96
+ if (keysInDb.length !== cacheKeys.length) {
97
+ for (const cacheKey of cacheKeys) {
98
+ if (!keysInDb.includes(cacheKey)) {
99
+ modelItemsCache.delete(cacheKey)
100
+ }
101
+ }
102
+ }
103
+
104
+ cache.set(modelName, modelItemsCache)
105
+
106
+ if (modelName === 'Identity' && modelCount <= 1) {
107
+ logger(
108
+ `[XXXXXX] [getItemsDataFromDb] ${modelName} responding with`,
109
+ Array.from(modelItemsCache.values()),
110
+ )
111
+ eventEmitter.emit(`item.${modelName}.requestAll.response`, {
112
+ items: Array.from(modelItemsCache.values()),
113
+ eventId,
114
+ })
115
+ }
116
+ }
117
+
118
+ export const itemRequestAllHandler = async (event) => {
119
+ logger('[events/requestAll] Request all items', event)
120
+ await handleRequestAll(event)
121
+ // eventQueue.push(event, (err) => {
122
+ // if (err) {
123
+ // console.error(err)
124
+ // }
125
+ // if (!err) {
126
+ // logger('[events/requestAll] Request all items done without error')
127
+ // }
128
+ // })
129
+ }
130
+
131
+ // export const getItemCache = () => {
132
+ // return itemCache
133
+ // }
134
+
135
+ type CreateItemCacheKeyParams = {
136
+ seedLocalId?: string
137
+ seedUid?: string
138
+ }
139
+
140
+ type CreateItemCacheKey = (
141
+ itemData: CreateItemCacheKeyParams,
142
+ ) => string | undefined
143
+
144
+ export const createItemCacheKey: CreateItemCacheKey = (itemData) => {
145
+ if (!itemData || (!itemData.seedLocalId && !itemData.seedUid)) {
146
+ return
147
+ }
148
+ return `${itemData.seedLocalId}_${itemData.seedUid || ''}`
149
+ }
150
+
151
+ // export const updateItemCache = (itemCacheKey: string, item: Item<any>) => {
152
+ // itemCache.set(itemCacheKey, item)
153
+ // }
154
+ //
155
+ // export const deleteItemFromCache = (itemCacheKey: string) => {
156
+ // itemCache.delete(itemCacheKey)
157
+ // }
@@ -0,0 +1,348 @@
1
+ import { EventObject, fromCallback } from 'xstate'
2
+ import { convertTxIdToImageSrc } from '@/shared/helpers'
3
+ import { propertyMachine } from '../propertyMachine'
4
+ import { fs } from '@zenfs/core'
5
+ import {
6
+ getRelationValueData,
7
+ getStorageTransactionIdForSeedUid,
8
+ } from '@/browser/db/read'
9
+
10
+ const storageTransactionIdToContentUrl = new Map<string, string>()
11
+ const refResolvedValueToContentUrl = new Map<string, string>()
12
+ const seedUidToContentUrl = new Map<string, string>()
13
+
14
+ export const resolveRelatedValue = fromCallback<
15
+ EventObject,
16
+ typeof propertyMachine
17
+ >(({ sendBack, input: { context } }) => {
18
+ const {
19
+ isRelation,
20
+ propertyValue,
21
+ propertyName,
22
+ seedUid,
23
+ propertyRecordSchema,
24
+ seedLocalId,
25
+ schemaUid,
26
+ } = context
27
+
28
+ const _resolveRelatedValue = async () => {
29
+ if (!propertyValue || !isRelation) {
30
+ return
31
+ }
32
+
33
+ if (seedUidToContentUrl.has(propertyValue)) {
34
+ const contentUrl = seedUidToContentUrl.get(propertyValue)
35
+ sendBack({
36
+ type: 'updateRenderValue',
37
+ renderValue: contentUrl,
38
+ })
39
+ sendBack({
40
+ type: 'resolvingRelatedValueSuccess',
41
+ resolvedDisplayValue: contentUrl,
42
+ })
43
+ return true
44
+ }
45
+
46
+ if (Array.isArray(propertyValue)) {
47
+ // TODO: Handle array of seedUids
48
+ return
49
+ }
50
+
51
+ const storageTransactionId =
52
+ await getStorageTransactionIdForSeedUid(propertyValue)
53
+
54
+ if (storageTransactionId) {
55
+ if (storageTransactionIdToContentUrl.has(storageTransactionId)) {
56
+ const contentUrl =
57
+ storageTransactionIdToContentUrl.get(storageTransactionId)
58
+ sendBack({
59
+ type: 'updateRenderValue',
60
+ renderValue: contentUrl,
61
+ })
62
+ sendBack({
63
+ type: 'resolvingRelatedValueSuccess',
64
+ resolvedDisplayValue: contentUrl,
65
+ resolvedValue: storageTransactionId,
66
+ })
67
+ return true
68
+ }
69
+
70
+ const contentUrl = await convertTxIdToImageSrc(storageTransactionId)
71
+ if (contentUrl) {
72
+ seedUidToContentUrl.set(propertyValue, contentUrl)
73
+ }
74
+ sendBack({
75
+ type: 'updateRenderValue',
76
+ renderValue: contentUrl,
77
+ })
78
+ sendBack({
79
+ type: 'resolvingRelatedValueSuccess',
80
+ resolvedDisplayValue: contentUrl,
81
+ resolvedValue: storageTransactionId,
82
+ })
83
+ return true
84
+ }
85
+
86
+ const relationValueData = await getRelationValueData(propertyValue)
87
+
88
+ if (relationValueData) {
89
+ const { refResolvedValue } = relationValueData
90
+ const propertyValueFromDb = relationValueData.propertyValue
91
+
92
+ // This handles a local-only relation value and resolves from the filesystem
93
+ if (
94
+ refResolvedValue &&
95
+ propertyRecordSchema.dataType === 'Relation' &&
96
+ propertyValueFromDb.length === 10 &&
97
+ propertyRecordSchema.ref
98
+ ) {
99
+ if (refResolvedValueToContentUrl.has(refResolvedValue)) {
100
+ const contentUrl = refResolvedValueToContentUrl.get(refResolvedValue)
101
+ sendBack({
102
+ type: 'updateRenderValue',
103
+ renderValue: contentUrl,
104
+ })
105
+ sendBack({
106
+ type: 'resolvingRelatedValueSuccess',
107
+ resolvedDisplayValue: contentUrl,
108
+ })
109
+ return true
110
+ }
111
+
112
+ const fileExists = await fs.promises.exists(
113
+ '/files/images/' + refResolvedValue,
114
+ )
115
+ if (fileExists) {
116
+ const fileContents = await fs.promises.readFile(
117
+ '/files/images/' + refResolvedValue,
118
+ )
119
+ const fileHandler = new File([fileContents], refResolvedValue)
120
+ const contentUrl = URL.createObjectURL(fileHandler)
121
+ refResolvedValueToContentUrl.set(refResolvedValue, contentUrl)
122
+ sendBack({
123
+ type: 'updateRenderValue',
124
+ renderValue: contentUrl,
125
+ })
126
+ sendBack({
127
+ type: 'resolvingRelatedValueSuccess',
128
+ resolvedDisplayValue: contentUrl,
129
+ })
130
+ return true
131
+ }
132
+ }
133
+
134
+ if (typeof propertyValueFromDb === 'string') {
135
+ // Check files for a filename that matches the propertyValue
136
+ if (propertyRecordSchema.dataType === 'ImageSrc') {
137
+ let contentUrl
138
+
139
+ if (storageTransactionIdToContentUrl.has(propertyValueFromDb)) {
140
+ contentUrl =
141
+ storageTransactionIdToContentUrl.get(propertyValueFromDb)
142
+ }
143
+
144
+ if (!contentUrl) {
145
+ const imageFileExists = await fs.promises.exists(
146
+ `/images/${propertyValue}`,
147
+ )
148
+ if (imageFileExists) {
149
+ const fileContents = await fs.promises.readFile(
150
+ `/images/${propertyValue}`,
151
+ )
152
+ const fileHandler = new File([fileContents], propertyValue)
153
+ contentUrl = URL.createObjectURL(fileHandler)
154
+ storageTransactionIdToContentUrl.set(
155
+ propertyValueFromDb,
156
+ contentUrl,
157
+ )
158
+ }
159
+ }
160
+
161
+ if (contentUrl) {
162
+ sendBack({
163
+ type: 'updateRenderValue',
164
+ renderValue: contentUrl,
165
+ })
166
+ sendBack({
167
+ type: 'resolvingRelatedValueSuccess',
168
+ resolvedDisplayValue: contentUrl,
169
+ })
170
+ return true
171
+ }
172
+ }
173
+ }
174
+ }
175
+ }
176
+
177
+ _resolveRelatedValue().then((success) => {
178
+ // if (success) {
179
+ // sendBack({
180
+ // type: 'resolvingRelatedValueDone',
181
+ // })
182
+ // }
183
+ // return
184
+ sendBack({
185
+ type: 'resolvingRelatedValueDone',
186
+ })
187
+ })
188
+ })
189
+
190
+ // const eventKey = `storage.transaction.${initialValue}.contentUrl.response`
191
+ //
192
+ // const contentUrlListener = async (event) => {
193
+ // console.log('[itemProperty] [constructor] contentUrlListener', event)
194
+ // }
195
+ //
196
+ // eventEmitter.once(eventKey, contentUrlListener)
197
+ //
198
+ // const ready = getArePropertyEventHandlersReady()
199
+ //
200
+ // console.log('[itemProperty] [constructor] ready', ready)
201
+ //
202
+ // eventEmitter.emit('storage.transaction.contentUrl.request', {
203
+ // storageTransactionId: initialValue,
204
+ // })
205
+
206
+ // Related property values can either be seedUid or seedUid[]
207
+ // let relatedSeedUid: string
208
+ // let relatedSeedUids: string[]
209
+ //
210
+ // if (Array.isArray(propertyValue)) {
211
+ // relatedSeedUids = propertyValue
212
+ // } else {
213
+ // relatedSeedUid = propertyValue
214
+ // }
215
+ //
216
+ // if (relatedSeedUid) {
217
+ // console.log(
218
+ // `[property/actors] [resolveRelatedValue] seedUid: ${relatedSeedUid}`,
219
+ // )
220
+ // const latestVersionOfRelatedSeedQuery = await appDb.run(
221
+ // sql.raw(
222
+ // `
223
+ // SELECT local_id, uid, MAX(attestation_created_at)
224
+ // FROM versions
225
+ // WHERE seed_uid = '${relatedSeedUid}';
226
+ // `,
227
+ // ),
228
+ // )
229
+ // if (
230
+ // latestVersionOfRelatedSeedQuery &&
231
+ // latestVersionOfRelatedSeedQuery.rows &&
232
+ // latestVersionOfRelatedSeedQuery.rows.length > 0
233
+ // ) {
234
+ // const latestVersionOfRelatedSeed =
235
+ // latestVersionOfRelatedSeedQuery.rows[0]
236
+ // console.log(
237
+ // '[property/actors] [resolveRelatedValue] latestVersionOfRelatedSeed',
238
+ // latestVersionOfRelatedSeed,
239
+ // )
240
+ // const storageIdQuery = await appDb.run(
241
+ // sql.raw(
242
+ // `
243
+ // SELECT property_value, MAX(attestation_created_at), ref_resolved_display_value, ref_resolved_value
244
+ // FROM metadata
245
+ // WHERE seed_uid = '${relatedSeedUid}'
246
+ // AND property_name = 'storageTransactionId';
247
+ // `,
248
+ // ),
249
+ // )
250
+ //
251
+ // if (
252
+ // storageIdQuery &&
253
+ // storageIdQuery.rows &&
254
+ // storageIdQuery.rows.length > 0
255
+ // ) {
256
+ // const storageId = storageIdQuery.rows[0][0]
257
+ //
258
+ // if (!storageId) {
259
+ // console.error(
260
+ // `storageId not found for ${propertyName} with relatedSeedUid ${relatedSeedUid}`,
261
+ // )
262
+ // }
263
+ //
264
+ // const resolvedDisplayValue = storageIdQuery.rows[0][2]
265
+ // let resolvedValue = storageIdQuery.rows[0][3]
266
+ //
267
+ // if (resolvedDisplayValue && resolvedValue) {
268
+ // sendBack({
269
+ // type: 'resolvingRelatedValueSuccess',
270
+ // resolvedDisplayValue,
271
+ // resolvedValue,
272
+ // })
273
+ // return
274
+ // }
275
+ //
276
+ // console.log(
277
+ // '[property/actors] [resolveRelatedValue] storageId',
278
+ // storageId,
279
+ // )
280
+ //
281
+ // const contentUrl = await convertTxIdToImageSrc(storageId)
282
+ //
283
+ // if (!contentUrl) {
284
+ // throw new Error(
285
+ // `contentUrl not found for ${propertyName} with relatedSeedUid ${relatedSeedUid}`,
286
+ // )
287
+ // }
288
+ //
289
+ // await appDb.run(
290
+ // sql.raw(
291
+ // `UPDATE metadata
292
+ // SET ref_resolved_display_value = '${contentUrl}',
293
+ // ref_resolved_value = '${storageId}'
294
+ // WHERE seed_uid = '${relatedSeedUid}'
295
+ // AND property_name = 'storageTransactionId';
296
+ // `,
297
+ // ),
298
+ // )
299
+ //
300
+ // sendBack({
301
+ // type: 'resolvingRelatedValueSuccess',
302
+ // resolvedDisplayValue: contentUrl,
303
+ // resolvedValue: storageId,
304
+ // })
305
+ // }
306
+ // }
307
+ //
308
+ // const versionUidQuery = await appDb.run(
309
+ // sql.raw(
310
+ // `
311
+ // SELECT uid
312
+ // FROM versions
313
+ // WHERE seed_uid = '${relatedSeedUid}';
314
+ // `,
315
+ // ),
316
+ // )
317
+ //
318
+ // if (
319
+ // versionUidQuery &&
320
+ // versionUidQuery.rows &&
321
+ // versionUidQuery.rows.length > 0
322
+ // ) {
323
+ // const versionUids = versionUidQuery.rows.map((row) => row[0])
324
+ // console.log(
325
+ // '[property/actors] [resolveRelatedValue] versionUids',
326
+ // versionUids,
327
+ // )
328
+ // const { itemProperties } = await easClient.request(GET_PROPERTIES, {
329
+ // where: {
330
+ // refUID: {
331
+ // in: versionUids,
332
+ // },
333
+ // decodedDataJson: {
334
+ // contains: 'storage_transaction_id',
335
+ // },
336
+ // },
337
+ // })
338
+ //
339
+ // console.log(
340
+ // '[property/actors] [resolveRelatedValue] itemProperties',
341
+ // itemProperties,
342
+ // )
343
+ //
344
+ // if (itemProperties && itemProperties.length > 0) {
345
+ // await savePropertiesToDb(itemProperties)
346
+ // }
347
+ // }
348
+ // }
@@ -0,0 +1,87 @@
1
+ import { EventObject, fromCallback } from 'xstate'
2
+ import { itemMachineSingle } from '@/browser/item/single/itemMachineSingle'
3
+
4
+ export const resolveRemoteStorage = fromCallback<
5
+ EventObject,
6
+ typeof itemMachineSingle
7
+ >(({ sendBack, input: { context } }) => {
8
+ const { propertyInstances } = context
9
+
10
+ if (!propertyInstances) {
11
+ throw new Error(`propertyInstances not found for ${context.seedLocalId}`)
12
+ }
13
+
14
+ if (!propertyInstances.has('storageTransactionId')) {
15
+ return
16
+ }
17
+
18
+ const storageTransactionId = propertyInstances.get('storageTransactionId')
19
+
20
+ const _resolveRemoteStorage = async (): Promise<void> => {
21
+ const path = await import('path-browserify')
22
+ const { fs } = await import('@zenfs/core')
23
+
24
+ const filesDirExists = await fs.promises.exists('/files')
25
+
26
+ if (!filesDirExists) {
27
+ await fs.promises.mkdir('/files')
28
+ }
29
+
30
+ const htmlDir = path.join('/files', 'html')
31
+
32
+ const htmlExists = await fs.promises.exists(htmlDir)
33
+
34
+ if (htmlExists) {
35
+ const htmlFiles = await fs.promises.readdir(htmlDir)
36
+ const matchingHtmlFile = htmlFiles.find(
37
+ (file) => file === `${storageTransactionId}.html`,
38
+ )
39
+ if (matchingHtmlFile) {
40
+ const htmlString = await fs.promises.readFile(
41
+ path.join(htmlDir, matchingHtmlFile),
42
+ 'utf8',
43
+ )
44
+ sendBack({
45
+ type: 'updateValue',
46
+ propertyName: 'html',
47
+ propertyValue: htmlString,
48
+ })
49
+ }
50
+ }
51
+
52
+ if (!htmlExists) {
53
+ await fs.promises.mkdir(htmlDir)
54
+ }
55
+
56
+ const jsonDir = path.join('/files', 'json')
57
+
58
+ const jsonExists = await fs.promises.exists(jsonDir)
59
+
60
+ if (jsonExists) {
61
+ const jsonFiles = await fs.promises.readdir(jsonDir)
62
+ const matchingJsonFile = jsonFiles.find(
63
+ (file) => file === `${storageTransactionId}.json`,
64
+ )
65
+ if (matchingJsonFile) {
66
+ const jsonString = await fs.promises.readFile(
67
+ path.join(jsonDir, matchingJsonFile),
68
+ 'utf8',
69
+ )
70
+ sendBack({
71
+ type: 'updateValue',
72
+ propertyName: 'json',
73
+ propertyValue: jsonString,
74
+ })
75
+ }
76
+ }
77
+
78
+ if (!jsonExists) {
79
+ await fs.promises.mkdir(jsonDir)
80
+ }
81
+ }
82
+
83
+ _resolveRemoteStorage().then(() => {
84
+ sendBack({ type: 'resolveRemoteStorageSuccess' })
85
+ return
86
+ })
87
+ })