@seedprotocol/sdk 0.3.0 → 0.3.1

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 (200) hide show
  1. package/dist/{ArweaveClient-CmJYeuq1.js → ArweaveClient-C0g0es2v.js} +2 -2
  2. package/dist/{ArweaveClient-EXXrXUzz.js.map → ArweaveClient-C0g0es2v.js.map} +1 -1
  3. package/dist/{ArweaveClient-EXXrXUzz.js → ArweaveClient-CzkBgiZY.js} +2 -2
  4. package/dist/{ArweaveClient-CmJYeuq1.js.map → ArweaveClient-CzkBgiZY.js.map} +1 -1
  5. package/dist/{Db-CESKJH-G.js → Db-C4oog-0n.js} +3 -3
  6. package/dist/{Db-CESKJH-G.js.map → Db-C4oog-0n.js.map} +1 -1
  7. package/dist/{Db-DiTPSbQ8.js → Db-VPouegkr.js} +3 -3
  8. package/dist/{Db-DiTPSbQ8.js.map → Db-VPouegkr.js.map} +1 -1
  9. package/dist/{EasClient-BesHK2bO.js → EasClient-B9a3NsTi.js} +2 -2
  10. package/dist/{EasClient-BesHK2bO.js.map → EasClient-B9a3NsTi.js.map} +1 -1
  11. package/dist/{EasClient-Cop6WtWl.js → EasClient-DQ93i2ci.js} +2 -2
  12. package/dist/{EasClient-Cop6WtWl.js.map → EasClient-DQ93i2ci.js.map} +1 -1
  13. package/dist/{FileManager-DTzkncLh.js → FileManager-_ErxVuwb.js} +8 -5
  14. package/dist/FileManager-_ErxVuwb.js.map +1 -0
  15. package/dist/{Item-B5Tz-Y7t.js → Item-D9HFPxsK.js} +3 -3
  16. package/dist/{Item-B5Tz-Y7t.js.map → Item-D9HFPxsK.js.map} +1 -1
  17. package/dist/{ItemProperty-DBV0Iib_.js → ItemProperty-BdtvKrEy.js} +3 -3
  18. package/dist/{ItemProperty-DBV0Iib_.js.map → ItemProperty-BdtvKrEy.js.map} +1 -1
  19. package/dist/{QueryClient-C7a0NHCt.js → QueryClient-Bmnv_moN.js} +2 -2
  20. package/dist/{QueryClient-C7a0NHCt.js.map → QueryClient-Bmnv_moN.js.map} +1 -1
  21. package/dist/{QueryClient-BnwbHtqe.js → QueryClient-hzOtFe99.js} +2 -2
  22. package/dist/{QueryClient-BnwbHtqe.js.map → QueryClient-hzOtFe99.js.map} +1 -1
  23. package/dist/bin.js +5 -5
  24. package/dist/bin.js.map +1 -1
  25. package/dist/{constants-CWg2B3Au.js → constants-Bhl5dZpN.js} +4 -1
  26. package/dist/constants-Bhl5dZpN.js.map +1 -0
  27. package/dist/{index-CQ9FS10O.js → index-BAtjd9W-.js} +3 -3
  28. package/dist/index-BAtjd9W-.js.map +1 -0
  29. package/dist/{index-BHGJ-Es9.js → index-BSR5hv7Q.js} +59 -27
  30. package/dist/index-BSR5hv7Q.js.map +1 -0
  31. package/dist/main.js +2 -2
  32. package/dist/{seed.schema.config-DDzfQguf.js → seed.schema.config-BxJYabim.js} +3 -3
  33. package/dist/{seed.schema.config-DDzfQguf.js.map → seed.schema.config-BxJYabim.js.map} +1 -1
  34. package/dist/src/AppStateSchema.ts +10 -0
  35. package/dist/src/ArweaveClient.ts +13 -0
  36. package/dist/src/Attestation.ts +21 -0
  37. package/dist/src/BaseArweaveClient.ts +13 -0
  38. package/dist/src/BaseClientManager.ts +35 -0
  39. package/dist/src/BaseDb.ts +35 -0
  40. package/dist/src/BaseEasClient.ts +14 -0
  41. package/dist/src/BaseFileManager.ts +60 -0
  42. package/dist/src/BaseItem.ts +413 -0
  43. package/dist/src/BaseItemProperty.ts +416 -0
  44. package/dist/src/BaseQueryClient.ts +13 -0
  45. package/dist/src/ClientManager.ts +63 -0
  46. package/dist/src/ConfigSchema.ts +15 -0
  47. package/dist/src/Db.ts +82 -0
  48. package/dist/src/EasClient.ts +13 -0
  49. package/dist/src/FileDownloader.ts +63 -0
  50. package/dist/src/FileManager.ts +59 -0
  51. package/dist/src/IDb.ts +7 -0
  52. package/dist/src/IEasClient.ts +2 -0
  53. package/dist/src/IItem.ts +26 -0
  54. package/dist/src/IItemProperty.ts +28 -0
  55. package/dist/src/IQueryClient.ts +11 -0
  56. package/dist/src/ImageResizer.ts +81 -0
  57. package/dist/src/Item.ts +12 -0
  58. package/dist/src/ItemProperty.ts +9 -0
  59. package/dist/src/MetadataSchema.ts +28 -0
  60. package/dist/src/ModelSchema.ts +46 -0
  61. package/dist/src/ModelUidSchema.ts +16 -0
  62. package/dist/src/PropertyUidSchema.ts +16 -0
  63. package/dist/src/QueryClient.ts +26 -0
  64. package/dist/src/Schema.ts +17 -0
  65. package/dist/src/SeedProtocolSdk.d.ts +19 -0
  66. package/dist/src/SeedSchema.ts +29 -0
  67. package/dist/src/VersionSchema.ts +16 -0
  68. package/dist/src/actors.ts +295 -0
  69. package/dist/src/addModelsToDb.ts +152 -0
  70. package/dist/src/allItems.ts +31 -0
  71. package/dist/src/analyzeInput.ts +144 -0
  72. package/dist/src/browser.app.db.config.ts +27 -0
  73. package/dist/src/browser.ts +30 -0
  74. package/dist/src/checkStatus.ts +57 -0
  75. package/dist/src/clientManagerMachine.ts +59 -0
  76. package/dist/src/configureFs.ts +98 -0
  77. package/dist/src/connectToDb.ts +41 -0
  78. package/dist/src/connectionManager.ts +67 -0
  79. package/dist/src/constants.ts +125 -0
  80. package/dist/src/content-hash.ts +30 -0
  81. package/dist/src/create.ts +39 -0
  82. package/dist/src/createMetadata.ts +77 -0
  83. package/dist/src/createNewItem.ts +61 -0
  84. package/dist/src/createPublishAttempt.ts +16 -0
  85. package/dist/src/createSeed.ts +31 -0
  86. package/dist/src/createSeeds.ts +24 -0
  87. package/dist/src/createVersion.ts +33 -0
  88. package/dist/src/dbMachine.ts +182 -0
  89. package/dist/src/deleteItem.ts +19 -0
  90. package/dist/src/download.ts +208 -0
  91. package/dist/src/drizzle.ts +78 -0
  92. package/dist/src/eas.ts +40 -0
  93. package/dist/src/environment.ts +35 -0
  94. package/dist/src/eventBus.ts +5 -0
  95. package/dist/src/events.ts +14 -0
  96. package/dist/src/fetchDataFromEas.ts +94 -0
  97. package/dist/src/fetchDbData.ts +19 -0
  98. package/dist/src/fetchRelatedItems.ts +184 -0
  99. package/dist/src/fetchSeeds.ts +45 -0
  100. package/dist/src/fetchVersions.ts +49 -0
  101. package/dist/src/files.ts +81 -0
  102. package/dist/src/filesDownload.ts +326 -0
  103. package/dist/src/fragment-masking.ts +87 -0
  104. package/dist/src/getExistingItem.ts +60 -0
  105. package/dist/src/getItem.ts +32 -0
  106. package/dist/src/getItemData.ts +99 -0
  107. package/dist/src/getItemProperties.ts +56 -0
  108. package/dist/src/getItemProperty.ts +34 -0
  109. package/dist/src/getItems.ts +70 -0
  110. package/dist/src/getMetadata.ts +40 -0
  111. package/dist/src/getModelSchemas.ts +89 -0
  112. package/dist/src/getPropertyData.ts +50 -0
  113. package/dist/src/getPublishPayload.ts +355 -0
  114. package/dist/src/getPublishUploads.ts +207 -0
  115. package/dist/src/getRelationValueData.ts +27 -0
  116. package/dist/src/getSchemaForItemProperty.ts +68 -0
  117. package/dist/src/getSchemaForModel.ts +42 -0
  118. package/dist/src/getSchemaUidForModel.ts +32 -0
  119. package/dist/src/getSeedData.ts +34 -0
  120. package/dist/src/getSegmentedItemProperties.ts +65 -0
  121. package/dist/src/getStorageTransactionIdForSeedUid.ts +38 -0
  122. package/dist/src/getVersionData.ts +46 -0
  123. package/dist/src/getVersionsForVersionUids.ts +39 -0
  124. package/dist/src/globalMachine.ts +280 -0
  125. package/dist/src/gql.ts +118 -0
  126. package/dist/src/graphql.ts +3209 -0
  127. package/dist/src/helpers.ts +273 -0
  128. package/dist/src/hydrateExistingItem.ts +137 -0
  129. package/dist/src/hydrateFromDb.ts +270 -0
  130. package/dist/src/hydrateNewItem.ts +34 -0
  131. package/dist/src/imageResize.ts +507 -0
  132. package/dist/src/index.d.ts +5 -0
  133. package/dist/src/index.ts +4 -0
  134. package/dist/src/initialize.ts +124 -0
  135. package/dist/src/internalMachine.ts +241 -0
  136. package/dist/src/itemMachineAll.ts +157 -0
  137. package/dist/src/itemMachineSingle.ts +158 -0
  138. package/dist/src/loadAppDb.ts +44 -0
  139. package/dist/src/machines.ts +66 -0
  140. package/dist/src/metadataLatest.ts +34 -0
  141. package/dist/src/migrate.ts +57 -0
  142. package/dist/src/model.ts +72 -0
  143. package/dist/src/modelClass.ts +19 -0
  144. package/dist/src/node.app.db.config.ts +41 -0
  145. package/dist/src/prepareDb.ts +29 -0
  146. package/dist/src/preparePublishRequestData.ts +81 -0
  147. package/dist/src/processItems.ts +74 -0
  148. package/dist/src/property.ts +156 -0
  149. package/dist/src/propertyMachine.ts +202 -0
  150. package/dist/src/publish.ts +28 -0
  151. package/dist/src/publishMachine.ts +77 -0
  152. package/dist/src/queries.ts +13 -0
  153. package/dist/src/recoverDeletedItem.ts +14 -0
  154. package/dist/src/reload.ts +33 -0
  155. package/dist/src/request.ts +45 -0
  156. package/dist/src/requestAll.ts +157 -0
  157. package/dist/src/resolveRelatedValue.ts +367 -0
  158. package/dist/src/resolveRemoteStorage.ts +88 -0
  159. package/dist/src/saveAppState.ts +50 -0
  160. package/dist/src/saveConfig.ts +106 -0
  161. package/dist/src/saveDataToDb.ts +145 -0
  162. package/dist/src/saveImageSrc.ts +242 -0
  163. package/dist/src/saveItemStorage.ts +156 -0
  164. package/dist/src/saveMetadata.ts +18 -0
  165. package/dist/src/savePublishService.ts +30 -0
  166. package/dist/src/saveRelation.ts +112 -0
  167. package/dist/src/seed.schema.config.ts +25 -0
  168. package/dist/src/seed.ts +10 -0
  169. package/dist/src/seedProtocol.ts +17 -0
  170. package/dist/src/services.ts +365 -0
  171. package/dist/src/setAddresses.ts +16 -0
  172. package/dist/src/sqlWasmClient.ts +88 -0
  173. package/dist/src/syncDbWithEas.ts +628 -0
  174. package/dist/src/trash.ts +29 -0
  175. package/dist/src/ts-to-proto.ts +101 -0
  176. package/dist/src/types.ts +12 -0
  177. package/dist/src/updateItemPropertyValue.ts +243 -0
  178. package/dist/src/updateMachineContext.ts +21 -0
  179. package/dist/src/updateMetadata.ts +59 -0
  180. package/dist/src/upload.ts +86 -0
  181. package/dist/src/validate.ts +45 -0
  182. package/dist/src/validateInput.ts +43 -0
  183. package/dist/src/validateItemData.ts +20 -0
  184. package/dist/src/versionData.ts +24 -0
  185. package/dist/src/waitForDb.ts +25 -0
  186. package/dist/src/waitForFiles.ts +41 -0
  187. package/dist/src/wasm.d.ts +8300 -0
  188. package/dist/types/scripts/bin.d.ts.map +1 -1
  189. package/dist/types/src/browser/helpers/FileManager.d.ts +2 -1
  190. package/dist/types/src/browser/helpers/FileManager.d.ts.map +1 -1
  191. package/dist/types/src/helpers/FileManager/BaseFileManager.d.ts +1 -0
  192. package/dist/types/src/helpers/FileManager/BaseFileManager.d.ts.map +1 -1
  193. package/dist/types/src/node/helpers/FileManager.d.ts +2 -1
  194. package/dist/types/src/node/helpers/FileManager.d.ts.map +1 -1
  195. package/package.json +3 -3
  196. package/dist/FileManager-DTzkncLh.js.map +0 -1
  197. package/dist/constants-CWg2B3Au.js.map +0 -1
  198. package/dist/index-BHGJ-Es9.js.map +0 -1
  199. package/dist/index-CQ9FS10O.js.map +0 -1
  200. package/dist/package.json +0 -189
@@ -0,0 +1,367 @@
1
+ import { EventObject, fromCallback } from 'xstate'
2
+ import { convertTxIdToImageSrc } from '@/helpers'
3
+ import fs from '@zenfs/core'
4
+ import debug from 'debug'
5
+ import { FromCallbackInput } from '@/types/machines'
6
+ import { PropertyMachineContext } from '@/types/property'
7
+ import { getStorageTransactionIdForSeedUid } from '@/db/read/getStorageTransactionIdForSeedUid'
8
+ import { getRelationValueData } from '@/db/read/getRelationValueData'
9
+
10
+ const logger = debug('app:property:actors:resolveRelatedValue')
11
+
12
+ const storageTransactionIdToContentUrl = new Map<string, string>()
13
+ const refResolvedValueToContentUrl = new Map<string, string>()
14
+ const seedUidToContentUrl = new Map<string, string>()
15
+
16
+ export const resolveRelatedValue = fromCallback<
17
+ EventObject,
18
+ FromCallbackInput<PropertyMachineContext, EventObject>
19
+ >(({ sendBack, input: { context } }) => {
20
+ const {
21
+ isRelation,
22
+ propertyValue,
23
+ propertyName,
24
+ seedUid,
25
+ propertyRecordSchema,
26
+ populatedFromDb,
27
+ schemaUid,
28
+ } = context
29
+
30
+ const _resolveRelatedValue = async () => {
31
+ if (!propertyValue || !isRelation || populatedFromDb) {
32
+ return
33
+ }
34
+
35
+ let parsedValue
36
+
37
+ try {
38
+ parsedValue = JSON.parse(propertyValue)
39
+ } catch (error) {
40
+ logger(`${propertyName} value is not a JSON string.`)
41
+ }
42
+
43
+ if (!parsedValue && seedUidToContentUrl.has(propertyValue)) {
44
+ const contentUrl = seedUidToContentUrl.get(propertyValue)
45
+ sendBack({
46
+ type: 'updateContext',
47
+ renderValue: contentUrl,
48
+ })
49
+ sendBack({
50
+ type: 'resolvingRelatedValueSuccess',
51
+ refResolvedDisplayValue: contentUrl,
52
+ })
53
+ return true
54
+ }
55
+
56
+ if (Array.isArray(propertyValue)) {
57
+ // TODO: Handle array of seedUids
58
+ logger(`${propertyName} value is an array of seedUids`)
59
+ return
60
+ }
61
+
62
+ if (Array.isArray(parsedValue)) {
63
+ logger(`${propertyName} value is a stringified array of seedUids`)
64
+ return
65
+ }
66
+
67
+ const storageTransactionId =
68
+ await getStorageTransactionIdForSeedUid(propertyValue)
69
+
70
+ if (storageTransactionId) {
71
+ if (storageTransactionIdToContentUrl.has(storageTransactionId)) {
72
+ const contentUrl =
73
+ storageTransactionIdToContentUrl.get(storageTransactionId)
74
+ sendBack({
75
+ type: 'updateContext',
76
+ renderValue: contentUrl,
77
+ })
78
+ sendBack({
79
+ type: 'resolvingRelatedValueSuccess',
80
+ refResolvedDisplayValue: contentUrl,
81
+ refResolvedValue: storageTransactionId,
82
+ })
83
+ return true
84
+ }
85
+
86
+ const contentUrl = await convertTxIdToImageSrc(storageTransactionId)
87
+ if (contentUrl) {
88
+ seedUidToContentUrl.set(propertyValue, contentUrl)
89
+ }
90
+ sendBack({
91
+ type: 'updateContext',
92
+ renderValue: contentUrl,
93
+ })
94
+ sendBack({
95
+ type: 'resolvingRelatedValueSuccess',
96
+ refResolvedDisplayValue: contentUrl,
97
+ refResolvedValue: storageTransactionId,
98
+ })
99
+ return true
100
+ }
101
+
102
+ const relationValueData = await getRelationValueData(propertyValue)
103
+
104
+ if (relationValueData) {
105
+ const { refResolvedValue } = relationValueData
106
+ const propertyValueFromDb = relationValueData.propertyValue
107
+
108
+ // This handles a local-only relation value and resolves from the filesystem
109
+ if (
110
+ refResolvedValue &&
111
+ propertyRecordSchema.dataType === 'Relation' &&
112
+ propertyValueFromDb.length === 10 &&
113
+ propertyRecordSchema.ref
114
+ ) {
115
+ if (refResolvedValueToContentUrl.has(refResolvedValue)) {
116
+ const contentUrl = refResolvedValueToContentUrl.get(refResolvedValue)
117
+ sendBack({
118
+ type: 'updateContext',
119
+ renderValue: contentUrl,
120
+ })
121
+ sendBack({
122
+ type: 'resolvingRelatedValueSuccess',
123
+ refResolvedDisplayValue: contentUrl,
124
+ })
125
+ return true
126
+ }
127
+
128
+ const fileExists = await fs.promises.exists(
129
+ '/files/images/' + refResolvedValue,
130
+ )
131
+ if (fileExists) {
132
+ const fileContents = await fs.promises.readFile(
133
+ '/files/images/' + refResolvedValue,
134
+ )
135
+ const fileHandler = new File([fileContents], refResolvedValue)
136
+ const contentUrl = URL.createObjectURL(fileHandler)
137
+ refResolvedValueToContentUrl.set(refResolvedValue, contentUrl)
138
+ sendBack({
139
+ type: 'updateContext',
140
+ renderValue: contentUrl,
141
+ })
142
+ sendBack({
143
+ type: 'resolvingRelatedValueSuccess',
144
+ refResolvedDisplayValue: contentUrl,
145
+ })
146
+ return true
147
+ }
148
+ }
149
+
150
+ if (typeof propertyValueFromDb === 'string') {
151
+ // Check files for a filename that matches the propertyValue
152
+ if (
153
+ propertyRecordSchema &&
154
+ propertyRecordSchema.refValueType === 'ImageSrc'
155
+ ) {
156
+ let contentUrl
157
+
158
+ if (storageTransactionIdToContentUrl.has(propertyValueFromDb)) {
159
+ contentUrl =
160
+ storageTransactionIdToContentUrl.get(propertyValueFromDb)
161
+ }
162
+
163
+ if (!contentUrl) {
164
+ const imageFileExists = await fs.promises.exists(
165
+ `/images/${propertyValue}`,
166
+ )
167
+ if (imageFileExists) {
168
+ const fileContents = await fs.promises.readFile(
169
+ `/images/${propertyValue}`,
170
+ )
171
+ const fileHandler = new File([fileContents], propertyValue)
172
+ contentUrl = URL.createObjectURL(fileHandler)
173
+ storageTransactionIdToContentUrl.set(
174
+ propertyValueFromDb,
175
+ contentUrl,
176
+ )
177
+ }
178
+ }
179
+
180
+ if (contentUrl) {
181
+ sendBack({
182
+ type: 'updateContext',
183
+ renderValue: contentUrl,
184
+ })
185
+ sendBack({
186
+ type: 'resolvingRelatedValueSuccess',
187
+ refResolvedDisplayValue: contentUrl,
188
+ })
189
+ return true
190
+ }
191
+ }
192
+ }
193
+ }
194
+ }
195
+
196
+ _resolveRelatedValue().then((success) => {
197
+ // if (success) {
198
+ // sendBack({
199
+ // type: 'resolvingRelatedValueDone',
200
+ // })
201
+ // }
202
+ // return
203
+ sendBack({
204
+ type: 'resolvingRelatedValueDone',
205
+ })
206
+ })
207
+ })
208
+
209
+ // const eventKey = `storage.transaction.${initialValue}.contentUrl.response`
210
+ //
211
+ // const contentUrlListener = async (event) => {
212
+ // console.log('[itemProperty] [constructor] contentUrlListener', event)
213
+ // }
214
+ //
215
+ // eventEmitter.once(eventKey, contentUrlListener)
216
+ //
217
+ // const ready = getArePropertyEventHandlersReady()
218
+ //
219
+ // console.log('[itemProperty] [constructor] ready', ready)
220
+ //
221
+ // eventEmitter.emit('storage.transaction.contentUrl.request', {
222
+ // storageTransactionId: initialValue,
223
+ // })
224
+
225
+ // Related property values can either be seedUid or seedUid[]
226
+ // let relatedSeedUid: string
227
+ // let relatedSeedUids: string[]
228
+ //
229
+ // if (Array.isArray(propertyValue)) {
230
+ // relatedSeedUids = propertyValue
231
+ // } else {
232
+ // relatedSeedUid = propertyValue
233
+ // }
234
+ //
235
+ // if (relatedSeedUid) {
236
+ // console.log(
237
+ // `[property/actors] [resolveRelatedValue] seedUid: ${relatedSeedUid}`,
238
+ // )
239
+ // const latestVersionOfRelatedSeedQuery = await appDb.run(
240
+ // sql.raw(
241
+ // `
242
+ // SELECT local_id, uid, MAX(attestation_created_at)
243
+ // FROM versions
244
+ // WHERE seed_uid = '${relatedSeedUid}';
245
+ // `,
246
+ // ),
247
+ // )
248
+ // if (
249
+ // latestVersionOfRelatedSeedQuery &&
250
+ // latestVersionOfRelatedSeedQuery.rows &&
251
+ // latestVersionOfRelatedSeedQuery.rows.length > 0
252
+ // ) {
253
+ // const latestVersionOfRelatedSeed =
254
+ // latestVersionOfRelatedSeedQuery.rows[0]
255
+ // console.log(
256
+ // '[property/actors] [resolveRelatedValue] latestVersionOfRelatedSeed',
257
+ // latestVersionOfRelatedSeed,
258
+ // )
259
+ // const storageIdQuery = await appDb.run(
260
+ // sql.raw(
261
+ // `
262
+ // SELECT property_value, MAX(attestation_created_at), ref_resolved_display_value, ref_resolved_value
263
+ // FROM metadata
264
+ // WHERE seed_uid = '${relatedSeedUid}'
265
+ // AND property_name = 'storageTransactionId';
266
+ // `,
267
+ // ),
268
+ // )
269
+ //
270
+ // if (
271
+ // storageIdQuery &&
272
+ // storageIdQuery.rows &&
273
+ // storageIdQuery.rows.length > 0
274
+ // ) {
275
+ // const storageId = storageIdQuery.rows[0][0]
276
+ //
277
+ // if (!storageId) {
278
+ // console.error(
279
+ // `storageId not found for ${propertyName} with relatedSeedUid ${relatedSeedUid}`,
280
+ // )
281
+ // }
282
+ //
283
+ // const refResolvedDisplayValue = storageIdQuery.rows[0][2]
284
+ // let resolvedValue = storageIdQuery.rows[0][3]
285
+ //
286
+ // if (refResolvedDisplayValue && resolvedValue) {
287
+ // sendBack({
288
+ // type: 'resolvingRelatedValueSuccess',
289
+ // refResolvedDisplayValue,
290
+ // resolvedValue,
291
+ // })
292
+ // return
293
+ // }
294
+ //
295
+ // console.log(
296
+ // '[property/actors] [resolveRelatedValue] storageId',
297
+ // storageId,
298
+ // )
299
+ //
300
+ // const contentUrl = await convertTxIdToImageSrc(storageId)
301
+ //
302
+ // if (!contentUrl) {
303
+ // throw new Error(
304
+ // `contentUrl not found for ${propertyName} with relatedSeedUid ${relatedSeedUid}`,
305
+ // )
306
+ // }
307
+ //
308
+ // await appDb.run(
309
+ // sql.raw(
310
+ // `UPDATE metadata
311
+ // SET ref_resolved_display_value = '${contentUrl}',
312
+ // ref_resolved_value = '${storageId}'
313
+ // WHERE seed_uid = '${relatedSeedUid}'
314
+ // AND property_name = 'storageTransactionId';
315
+ // `,
316
+ // ),
317
+ // )
318
+ //
319
+ // sendBack({
320
+ // type: 'resolvingRelatedValueSuccess',
321
+ // refResolvedDisplayValue: contentUrl,
322
+ // resolvedValue: storageId,
323
+ // })
324
+ // }
325
+ // }
326
+ //
327
+ // const versionUidQuery = await appDb.run(
328
+ // sql.raw(
329
+ // `
330
+ // SELECT uid
331
+ // FROM versions
332
+ // WHERE seed_uid = '${relatedSeedUid}';
333
+ // `,
334
+ // ),
335
+ // )
336
+ //
337
+ // if (
338
+ // versionUidQuery &&
339
+ // versionUidQuery.rows &&
340
+ // versionUidQuery.rows.length > 0
341
+ // ) {
342
+ // const versionUids = versionUidQuery.rows.map((row) => row[0])
343
+ // console.log(
344
+ // '[property/actors] [resolveRelatedValue] versionUids',
345
+ // versionUids,
346
+ // )
347
+ // const { itemProperties } = await easClient.request(GET_PROPERTIES, {
348
+ // where: {
349
+ // refUID: {
350
+ // in: versionUids,
351
+ // },
352
+ // decodedDataJson: {
353
+ // contains: 'storage_transaction_id',
354
+ // },
355
+ // },
356
+ // })
357
+ //
358
+ // console.log(
359
+ // '[property/actors] [resolveRelatedValue] itemProperties',
360
+ // itemProperties,
361
+ // )
362
+ //
363
+ // if (itemProperties && itemProperties.length > 0) {
364
+ // await savePropertiesToDb(itemProperties)
365
+ // }
366
+ // }
367
+ // }
@@ -0,0 +1,88 @@
1
+ import { EventObject, fromCallback } from 'xstate'
2
+ import { FromCallbackInput } from '@/types/machines'
3
+ import { PropertyMachineContext } from '@/types/property'
4
+ import fs from '@zenfs/core'
5
+
6
+ export const resolveRemoteStorage = fromCallback<
7
+ EventObject,
8
+ FromCallbackInput<PropertyMachineContext, EventObject>
9
+ >(({ sendBack, input: { context } }) => {
10
+ const { propertyInstances } = context
11
+
12
+ if (!propertyInstances) {
13
+ throw new Error(`propertyInstances not found for ${context.seedLocalId}`)
14
+ }
15
+
16
+ if (!propertyInstances.has('storageTransactionId')) {
17
+ return
18
+ }
19
+
20
+ const storageTransactionId = propertyInstances.get('storageTransactionId')
21
+
22
+ const _resolveRemoteStorage = async (): Promise<void> => {
23
+ const path = await import('path-browserify')
24
+
25
+ const filesDirExists = await fs.promises.exists('/files')
26
+
27
+ if (!filesDirExists) {
28
+ await fs.promises.mkdir('/files')
29
+ }
30
+
31
+ const htmlDir = path.join('/files', 'html')
32
+
33
+ const htmlExists = await fs.promises.exists(htmlDir)
34
+
35
+ if (htmlExists) {
36
+ const htmlFiles = await fs.promises.readdir(htmlDir)
37
+ const matchingHtmlFile = htmlFiles.find(
38
+ (file) => file === `${storageTransactionId}.html`,
39
+ )
40
+ if (matchingHtmlFile) {
41
+ const htmlString = await fs.promises.readFile(
42
+ path.join(htmlDir, matchingHtmlFile),
43
+ 'utf8',
44
+ )
45
+ sendBack({
46
+ type: 'updateValue',
47
+ propertyName: 'html',
48
+ propertyValue: htmlString,
49
+ })
50
+ }
51
+ }
52
+
53
+ if (!htmlExists) {
54
+ await fs.promises.mkdir(htmlDir)
55
+ }
56
+
57
+ const jsonDir = path.join('/files', 'json')
58
+
59
+ const jsonExists = await fs.promises.exists(jsonDir)
60
+
61
+ if (jsonExists) {
62
+ const jsonFiles = await fs.promises.readdir(jsonDir)
63
+ const matchingJsonFile = jsonFiles.find(
64
+ (file) => file === `${storageTransactionId}.json`,
65
+ )
66
+ if (matchingJsonFile) {
67
+ const jsonString = await fs.promises.readFile(
68
+ path.join(jsonDir, matchingJsonFile),
69
+ 'utf8',
70
+ )
71
+ sendBack({
72
+ type: 'updateValue',
73
+ propertyName: 'json',
74
+ propertyValue: jsonString,
75
+ })
76
+ }
77
+ }
78
+
79
+ if (!jsonExists) {
80
+ await fs.promises.mkdir(jsonDir)
81
+ }
82
+ }
83
+
84
+ _resolveRemoteStorage().then(() => {
85
+ sendBack({ type: 'resolveRemoteStorageSuccess' })
86
+ return
87
+ })
88
+ })
@@ -0,0 +1,50 @@
1
+ import { FromCallbackInput } from "@/types";
2
+ import { fromCallback } from "xstate";
3
+ import { ClientManagerContext } from "@/types";
4
+ import { EventObject } from "xstate";
5
+ import debug from "debug";
6
+ import { appState } from "@/seedSchema";
7
+
8
+ const logger = debug('app:client:writeToDb')
9
+
10
+ export const saveAppState = fromCallback<
11
+ EventObject,
12
+ FromCallbackInput<ClientManagerContext>
13
+ >(
14
+ ({sendBack, input: {key, value}}) => {
15
+
16
+ const _saveAppState = async () => {
17
+ const { BaseDb } = await import('@/db/Db/BaseDb')
18
+ if (!BaseDb) {
19
+ throw new Error('BaseDb not found')
20
+ }
21
+ const appDb = BaseDb.getAppDb()
22
+ if (!appDb) {
23
+ throw new Error('App DB not found')
24
+ }
25
+
26
+ console.log(`key: ${key} value: ${value}`)
27
+
28
+ appDb.insert(appState)
29
+ .values({
30
+ key: key,
31
+ value: JSON.stringify(value),
32
+ })
33
+ .onConflictDoUpdate({
34
+ target: appState.key,
35
+ set: {
36
+ value: JSON.stringify(value),
37
+ },
38
+ })
39
+
40
+ }
41
+
42
+ _saveAppState().then(() => {
43
+ sendBack({
44
+ type: 'saveAppStateSuccess',
45
+ key,
46
+ value,
47
+ })
48
+ })
49
+
50
+ })
@@ -0,0 +1,106 @@
1
+ import { EventObject, fromCallback } from 'xstate'
2
+ import { INTERNAL_SAVING_CONFIG_SUCCESS } from '@/services/internal/constants'
3
+ import { BaseDb } from '@/db/Db/BaseDb'
4
+ import { FromCallbackInput, InternalMachineContext } from '@/types'
5
+ import { appState } from '@/seedSchema'
6
+ import debug from 'debug'
7
+ import { getTableColumns, sql } from 'drizzle-orm'
8
+ import { getTableConfig } from 'drizzle-orm/sqlite-core'
9
+
10
+ const logger = debug('app:services:internal:actors:saveConfig')
11
+
12
+ export const saveConfig = fromCallback<
13
+ EventObject,
14
+ FromCallbackInput<InternalMachineContext>
15
+ >(({ sendBack, input: { context } }) => {
16
+
17
+ const { endpoints, addresses, arweaveDomain } = context
18
+
19
+ if (!endpoints) {
20
+ throw new Error('saveConfig called with invalid endpoints')
21
+ }
22
+
23
+ const _saveConfig = async (): Promise<void> => {
24
+ // logger('[sdk] [internal/actors] starting _saveConfig')
25
+ const appDb = BaseDb.getAppDb()
26
+
27
+ if (!appDb) {
28
+ throw new Error('App DB not found')
29
+ }
30
+ const endpointsValueString = JSON.stringify(endpoints)
31
+ const addressesValueString = JSON.stringify(addresses)
32
+
33
+ const tableColumns = getTableColumns(appState)
34
+
35
+ logger('tableColumns', tableColumns)
36
+
37
+ const {
38
+ columns,
39
+ indexes,
40
+ foreignKeys,
41
+ checks,
42
+ primaryKeys,
43
+ name,
44
+ } = getTableConfig(appState);
45
+
46
+ logger('columns', columns)
47
+ logger('indexes', indexes)
48
+ logger('foreignKeys', foreignKeys)
49
+ logger('checks', checks)
50
+ logger('primaryKeys', primaryKeys)
51
+ logger('name', name)
52
+
53
+ logger('calling select on db')
54
+
55
+ const queryResult = await appDb.select().from(appState)
56
+
57
+ logger('queryResult', queryResult)
58
+
59
+ // TODO: Figure out how to define on conflict with multiple rows added
60
+ await appDb
61
+ .insert(appState)
62
+ .values({
63
+ key: 'endpoints',
64
+ value: endpointsValueString,
65
+ })
66
+ .onConflictDoUpdate({
67
+ target: appState.key,
68
+ set: {
69
+ value: endpointsValueString,
70
+ },
71
+ })
72
+ // logger('[sdk] [internal/actors] Saving addresses to db')
73
+ await appDb
74
+ .insert(appState)
75
+ .values({
76
+ key: 'addresses',
77
+ value: addressesValueString,
78
+ })
79
+ .onConflictDoUpdate({
80
+ target: appState.key,
81
+ set: {
82
+ value: addressesValueString,
83
+ },
84
+ })
85
+ await appDb
86
+ .insert(appState)
87
+ .values({
88
+ key: 'arweaveDomain',
89
+ value: arweaveDomain || 'arweave.net',
90
+ })
91
+ .onConflictDoUpdate({
92
+ target: appState.key,
93
+ set: {
94
+ value: arweaveDomain || 'arweave.net',
95
+ },
96
+ })
97
+ logger('[sdk] [internal/actors] Should be done saving')
98
+ }
99
+
100
+ _saveConfig().then(() => {
101
+ logger('[sdk] [internal/actors] Successfully saved config')
102
+ return sendBack({ type: INTERNAL_SAVING_CONFIG_SUCCESS })
103
+ })
104
+
105
+ return () => { }
106
+ })