@seedprotocol/sdk 0.1.46 → 0.1.48

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 (147) hide show
  1. package/dist/bin.js.map +1 -1
  2. package/dist/constants-BLctWkrn.js.map +1 -1
  3. package/dist/{index-BEzB8REh.js → index-DMIKRod-.js} +3684 -3660
  4. package/dist/index-DMIKRod-.js.map +1 -0
  5. package/dist/{index-ChGsdGPJ.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-jKpK-lR6.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 +10 -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 +21 -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/events/item/create.d.ts.map +1 -1
  126. package/dist/types/src/browser/events/item/publish.d.ts.map +1 -1
  127. package/dist/types/src/browser/events/item/requestAll.d.ts.map +1 -1
  128. package/dist/types/src/browser/events/item/syncDbWithEas.d.ts.map +1 -1
  129. package/dist/types/src/browser/item/single/actors/hydrateExistingItem.d.ts +3 -1
  130. package/dist/types/src/browser/item/single/actors/hydrateExistingItem.d.ts.map +1 -1
  131. package/dist/types/src/browser/item/single/actors/saveDataToDb.d.ts +3 -3
  132. package/dist/types/src/browser/item/single/itemMachineSingle.d.ts +3 -3
  133. package/dist/types/src/browser/property/ItemProperty.d.ts +7 -7
  134. package/dist/types/src/browser/property/ItemProperty.d.ts.map +1 -1
  135. package/dist/types/src/browser/property/actors/hydrateFromDb.d.ts.map +1 -1
  136. package/dist/types/src/browser/property/actors/initialize.d.ts.map +1 -1
  137. package/dist/types/src/browser/property/actors/resolveRemoteStorage.d.ts +3 -3
  138. package/dist/types/src/browser/react/trash.d.ts +1 -1
  139. package/dist/types/src/browser/react/trash.d.ts.map +1 -1
  140. package/dist/types/src/browser/services/db/dbMachine.d.ts +6 -6
  141. package/dist/types/src/browser/services/publish/publishMachine.d.ts +17 -17
  142. package/dist/types/src/types/machines.d.ts +4 -0
  143. package/dist/types/src/types/machines.d.ts.map +1 -1
  144. package/package.json +1 -1
  145. package/dist/index-BEzB8REh.js.map +0 -1
  146. package/dist/index-ChGsdGPJ.js.map +0 -1
  147. package/dist/seed.schema.config-jKpK-lR6.js.map +0 -1
@@ -0,0 +1,359 @@
1
+ import { useCallback, useEffect, useState } from 'react'
2
+ import { ActorRef } from 'xstate'
3
+ import { orderBy } from 'lodash-es'
4
+ import { produce } from 'immer'
5
+ import { eventEmitter } from '@/eventBus'
6
+ import pluralize from 'pluralize'
7
+ import { getGlobalService } from '@/browser/services/global'
8
+ import { useSelector } from '@xstate/react'
9
+ import debug from 'debug'
10
+ import { appState } from '@/shared/seedSchema'
11
+ import { like } from 'drizzle-orm'
12
+ import { getAppDb } from '@/browser/db/sqlWasmClient'
13
+ import { MachineIds } from '@/browser/services/internal/constants'
14
+
15
+ const logger = debug('app:react:services')
16
+
17
+ const finalStrings = ['idle', 'ready', 'done', 'success']
18
+
19
+ export const getServiceName = (service: ActorRef<any, any>) => {
20
+ let name = 'actor'
21
+ if (service && service.uniqueKey) {
22
+ name = service.uniqueKey
23
+ }
24
+ if (service && !service.uniqueKey && service.logic && service.logic.config) {
25
+ name = getServiceUniqueKey(service)
26
+ }
27
+ return name
28
+ }
29
+
30
+ export const getServiceValue = (
31
+ service: ActorRef<any, any>,
32
+ ): string | undefined => {
33
+ let value
34
+ if (service && service.getSnapshot() && service.getSnapshot().value) {
35
+ value = service.getSnapshot().value
36
+ }
37
+ if (getServiceName(service) === 'global') {
38
+ if (
39
+ value &&
40
+ typeof value === 'object' &&
41
+ Object.keys(value).length > 0 &&
42
+ Object.keys(value)[0] === 'initialized'
43
+ ) {
44
+ value = 'ready'
45
+ }
46
+ }
47
+ if (value && typeof value === 'object') {
48
+ value = JSON.stringify(value)
49
+ }
50
+ return value
51
+ }
52
+
53
+ export const getServiceUniqueKey = (service: ActorRef<any, any>) => {
54
+ if (!service || !service.logic || !service.logic.config) {
55
+ return
56
+ }
57
+ const config = service.logic.config
58
+ if (!config.id) {
59
+ return
60
+ }
61
+ let uniqueKey = config.id
62
+ if (config.id.includes('@seedSdk/')) {
63
+ uniqueKey = config.id.match(/^.*@seedSdk\/(\w+)[\.\w]*/)[1]
64
+ }
65
+ let snapshot
66
+ try {
67
+ snapshot = service.getSnapshot()
68
+ } catch (error) {
69
+ logger('Error:', error)
70
+ return uniqueKey
71
+ }
72
+ if (snapshot) {
73
+ const context = snapshot.context
74
+ if (context && context.dbName) {
75
+ uniqueKey = context.dbName
76
+ }
77
+ if (context && context.modelNamePlural) {
78
+ uniqueKey = context.modelNamePlural
79
+ }
80
+ if (context && context.modelName) {
81
+ uniqueKey = pluralize(context.modelName.toLowerCase())
82
+ }
83
+ }
84
+ return uniqueKey
85
+ }
86
+
87
+ export const useService = (service: ActorRef<any, any>) => {
88
+ const [timeElapsed, setTimeElapsed] = useState(0)
89
+
90
+ const getPercentComplete = (service: ActorRef<any, any>) => {
91
+ let percentComplete = 0
92
+ if (service.logic.states) {
93
+ const stateNames = Object.keys(service.logic.states)
94
+ const totalStates = stateNames.length
95
+ const value = getServiceValue(service)
96
+ if (finalStrings.includes(value)) {
97
+ return 0
98
+ }
99
+ const stateIndex = stateNames.indexOf(value)
100
+ percentComplete = (stateIndex / totalStates) * 100
101
+ }
102
+ return percentComplete
103
+ }
104
+
105
+ const updateTime = useCallback(
106
+ (interval) => {
107
+ const context = service.getSnapshot().context
108
+ if (context && context.times) {
109
+ console.log('[ActorItem] [useEffect] context.times', context.times)
110
+ }
111
+ const status = service.getSnapshot().value
112
+ if (
113
+ status === 'done' ||
114
+ status === 'success' ||
115
+ status === 'idle' ||
116
+ status === 'ready'
117
+ ) {
118
+ clearInterval(interval)
119
+ return
120
+ }
121
+ setTimeElapsed((timeElapsed) => timeElapsed + 1)
122
+ },
123
+ [service],
124
+ )
125
+
126
+ const startInterval = useCallback(() => {
127
+ const interval = setInterval(() => {
128
+ updateTime(interval)
129
+ }, 1000)
130
+ return interval
131
+ }, [updateTime, service])
132
+
133
+ useEffect(() => {
134
+ const interval = startInterval()
135
+ return () => clearInterval(interval)
136
+ }, [])
137
+
138
+ return {
139
+ name: getServiceName(service),
140
+ timeElapsed,
141
+ value: getServiceValue(service),
142
+ percentComplete: getPercentComplete(service),
143
+ uniqueKey: getServiceUniqueKey(service),
144
+ }
145
+ }
146
+
147
+ export const useIsDbReady = () => {
148
+ const [isDbReady, setIsDbReady] = useState(false)
149
+
150
+ const { internalStatus } = useGlobalServiceStatus()
151
+
152
+ useEffect(() => {
153
+ if (internalStatus === 'ready') {
154
+ setIsDbReady(true)
155
+ }
156
+ }, [internalStatus])
157
+
158
+ return isDbReady
159
+ }
160
+
161
+ export const usePersistedSnapshots = () => {
162
+ const [initialized, setInitialized] = useState(false)
163
+
164
+ const hasSavedSnapshots = useHasSavedSnapshots()
165
+
166
+ const { services, percentComplete } = useServices()
167
+
168
+ // Helper function to save all actor snapshots to the database
169
+ const save = useCallback(async () => {
170
+ for (const actor of services) {
171
+ const uniqueKey = getServiceUniqueKey(actor)
172
+ console.log(
173
+ `would save to db with snapshot__${uniqueKey}:`,
174
+ JSON.stringify(actor.getPersistedSnapshot()),
175
+ )
176
+ // await writeAppState(
177
+ // `snapshot__${uniqueKey}`,
178
+ // JSON.stringify(actor.getPersistedSnapshot()),
179
+ // )
180
+ }
181
+ }, [services])
182
+
183
+ // Helper function to load persisted snapshots from the database
184
+ const load = useCallback(async () => {
185
+ const appDb = getAppDb()
186
+
187
+ if (!appDb) {
188
+ return []
189
+ }
190
+
191
+ const persistedSnapshots = await appDb
192
+ .select()
193
+ .from(appState)
194
+ .where(like(appState.key, 'snapshot__%'))
195
+ return persistedSnapshots
196
+ }, [])
197
+
198
+ useEffect(() => {
199
+ if (!hasSavedSnapshots || initialized) {
200
+ return
201
+ }
202
+ const initialize = async () => {
203
+ const persistedSnapshots = await load()
204
+ console.log('persistedSnapshots:', persistedSnapshots)
205
+ setInitialized(true)
206
+ }
207
+
208
+ initialize()
209
+
210
+ return () => {
211
+ save() // Save snapshots on unmount
212
+ }
213
+ }, [hasSavedSnapshots, initialized])
214
+ }
215
+
216
+ export const useHasSavedSnapshots = () => {
217
+ const [hasSavedSnapshots, setHasSavedSnapshots] = useState(false)
218
+
219
+ const isDbReady = useIsDbReady()
220
+
221
+ useEffect(() => {
222
+ if (isDbReady) {
223
+ const _checkForSnapshots = async (): Promise<void> => {
224
+ const appDb = getAppDb()
225
+
226
+ const rows = await appDb
227
+ .select()
228
+ .from(appState)
229
+ .where(like(appState.key, 'snapshot__%'))
230
+
231
+ if (rows && rows.length > 0) {
232
+ setHasSavedSnapshots(true)
233
+ }
234
+ }
235
+
236
+ _checkForSnapshots()
237
+ }
238
+ }, [isDbReady])
239
+
240
+ return hasSavedSnapshots
241
+ }
242
+
243
+ export const useServices = () => {
244
+ const [actors, setActors] = useState<ActorRef<any, any>[]>([])
245
+ const [percentComplete, setPercentComplete] = useState(5)
246
+
247
+ const actorsMap = new Map<string, ActorRef<any, any>>()
248
+
249
+ useEffect(() => {
250
+ const globalServiceListener = (event) => {
251
+ if (event && event.type === 'init') {
252
+ return
253
+ }
254
+ if (
255
+ event.actorRef &&
256
+ event.actorRef.logic &&
257
+ event.actorRef.logic.config
258
+ ) {
259
+ const service = event.actorRef
260
+ const services = [service]
261
+
262
+ if (service.logic.config.id === MachineIds.GLOBAL) {
263
+ const context = service.getSnapshot().context
264
+ const keys = Object.keys(context)
265
+ for (const key of keys) {
266
+ if (!key.startsWith('internal') && key.endsWith('Service')) {
267
+ const allItemsService = context[key]
268
+ services.push(allItemsService)
269
+ }
270
+ }
271
+ }
272
+
273
+ services.forEach((innerService) => {
274
+ const uniqueKey = getServiceUniqueKey(innerService)
275
+ if (!uniqueKey) {
276
+ return
277
+ }
278
+ innerService.uniqueKey = uniqueKey
279
+ actorsMap.set(uniqueKey, innerService)
280
+ })
281
+
282
+ let actorsArray = Array.from(actorsMap.values())
283
+ actorsArray = orderBy(actorsArray, (a) => a.logic.config.id, ['asc'])
284
+
285
+ setActors(
286
+ produce(actors, (draft) => {
287
+ return actorsArray
288
+ }),
289
+ )
290
+ }
291
+ }
292
+
293
+ eventEmitter.addListener('inspect.globalService', globalServiceListener)
294
+
295
+ return () => {
296
+ eventEmitter.removeListener(
297
+ 'inspect.globalService',
298
+ globalServiceListener,
299
+ )
300
+ }
301
+ }, [])
302
+
303
+ useEffect(() => {
304
+ const globalService = actors.find(
305
+ (actor) => getServiceName(actor) === 'global',
306
+ )
307
+ const internalService = actors.find(
308
+ (actor) => getServiceName(actor) === 'internal',
309
+ )
310
+ if (!globalService || !internalService) {
311
+ return
312
+ }
313
+ if (
314
+ getServiceValue(globalService) === 'ready' &&
315
+ getServiceValue(internalService) === 'ready'
316
+ ) {
317
+ const denominator = actors.length
318
+ const finishedActors = actors.filter((actor) => {
319
+ const value = getServiceValue(actor)
320
+ return finalStrings.includes(value)
321
+ })
322
+ const numerator = finishedActors.length
323
+ const percentComplete = (numerator / denominator) * 100
324
+ setPercentComplete(percentComplete)
325
+ }
326
+ }, [actors])
327
+
328
+ return {
329
+ services: actors,
330
+ percentComplete,
331
+ }
332
+ }
333
+
334
+ export const useGlobalServiceStatus = () => {
335
+ const globalService = getGlobalService()
336
+
337
+ const status = useSelector(globalService, (snapshot) => {
338
+ return snapshot.value
339
+ })
340
+
341
+ const internalStatus = useSelector(
342
+ globalService.getSnapshot().context.internalService,
343
+ (snapshot) => {
344
+ if (!snapshot) {
345
+ return
346
+ }
347
+ return snapshot.value
348
+ },
349
+ )
350
+
351
+ const internalService = useSelector(globalService, (snapshot) => {
352
+ return snapshot.context.internalService
353
+ })
354
+
355
+ return {
356
+ status,
357
+ internalStatus,
358
+ }
359
+ }
@@ -0,0 +1,88 @@
1
+ import { SqliteConnectionManager } from '@/browser/services/db'
2
+ import {
3
+ SqliteRemoteDatabase,
4
+ SqliteRemoteResult,
5
+ } from 'drizzle-orm/sqlite-proxy'
6
+ import { sql } from 'drizzle-orm'
7
+
8
+ let sqlite3InitModule
9
+ let sqliteWasmClient
10
+ let manager: SqliteConnectionManager | undefined
11
+ let isPreparing = false
12
+
13
+ export const setSqliteWasmClient = (client) => {
14
+ sqliteWasmClient = client
15
+ }
16
+
17
+ export const getSqliteWasmClient = async () => {
18
+ if (sqliteWasmClient) {
19
+ return sqliteWasmClient
20
+ }
21
+ if (typeof window === 'undefined') {
22
+ throw new Error('validateInput called from non-browser context')
23
+ }
24
+
25
+ if (isPreparing) {
26
+ return
27
+ }
28
+
29
+ isPreparing = true
30
+
31
+ if (!sqlite3InitModule) {
32
+ sqlite3InitModule = await import('@sqlite.org/sqlite-wasm')
33
+ }
34
+
35
+ if (!window.sqlite3Worker1Promiser) {
36
+ await sqlite3InitModule()
37
+ }
38
+
39
+ if (!window.sqlite3Worker1Promiser) {
40
+ console.error('window.sqlite3Worker1Promiser not found')
41
+ isPreparing = false
42
+ return
43
+ }
44
+
45
+ try {
46
+ sqliteWasmClient = await window.sqlite3Worker1Promiser.v2().catch((err) => {
47
+ console.error('Error initializing sqliteWasmClient:', err)
48
+ isPreparing = false
49
+ })
50
+ } catch (err) {
51
+ console.error('Error initializing sqliteWasmClient:', err)
52
+ isPreparing = false
53
+ }
54
+ return sqliteWasmClient
55
+ }
56
+
57
+ export const getManager = () => {
58
+ return manager
59
+ }
60
+
61
+ export const setManager = (m) => {
62
+ manager = m
63
+ }
64
+ let appDb: SqliteRemoteDatabase<Record<string, unknown>> | undefined
65
+ export const setAppDb = (db: SqliteRemoteDatabase<Record<string, unknown>>) => {
66
+ appDb = db
67
+ }
68
+ export const getAppDb = () => {
69
+ if (!appDb) {
70
+ throw new Error('getAppDb: appDb is undefined')
71
+ }
72
+
73
+ return appDb
74
+ }
75
+ export const isAppDbReady = () => {
76
+ return !!appDb
77
+ }
78
+ type RunQueryForStatement = (
79
+ statement: string,
80
+ ) => Promise<SqliteRemoteResult<unknown>>
81
+
82
+ export const runQueryForStatement: RunQueryForStatement = async (
83
+ statement: string,
84
+ ) => {
85
+ const appDb = getAppDb()
86
+
87
+ return appDb.run(sql.raw(statement))
88
+ }