@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,85 @@
1
+ import { areFsListenersReady, setupFsListeners } from '@/browser/events/files'
2
+ import { setupAllItemsEventHandlers } from '@/browser/events'
3
+ import { setupServicesEventHandlers } from '@/browser/services/events'
4
+ import { eventEmitter } from '@/eventBus'
5
+ import { globalService } from '@/browser/services'
6
+ import { ModelClassType, SeedConstructorOptions } from '@/types'
7
+ import {
8
+ getModel,
9
+ getModelNames,
10
+ getModels,
11
+ setModel,
12
+ } from '@/browser/stores/modelClass'
13
+ import { setupServiceHandlers } from '@/browser/events/services'
14
+
15
+ const client = {
16
+ init: ({ config, addresses }: SeedConstructorOptions) => {
17
+ const { endpoints, models } = config
18
+
19
+ for (const [key, value] of Object.entries(models)) {
20
+ setModel(key, value)
21
+ }
22
+ setupFsListeners()
23
+ setupAllItemsEventHandlers()
24
+ setupServicesEventHandlers()
25
+ setupServiceHandlers()
26
+ if (areFsListenersReady()) {
27
+ eventEmitter.emit('fs.init')
28
+ }
29
+ if (!areFsListenersReady()) {
30
+ console.error('fs listeners not ready during init')
31
+ }
32
+ globalService.send({ type: 'init', endpoints, models, addresses })
33
+ import('@/shared/configs/seed.schema.config').then(({ models }) => {
34
+ for (const [key, value] of Object.entries(models)) {
35
+ setModel(key, value)
36
+ }
37
+ })
38
+ },
39
+ subscribe: (callback: any) => {
40
+ callback({
41
+ type: '@xstate.snapshot',
42
+ actorRef: globalService,
43
+ snapshot: globalService.getSnapshot(),
44
+ })
45
+ eventEmitter.addListener('globalService', callback)
46
+
47
+ return {
48
+ unsubscribe: () => {
49
+ eventEmitter.removeListener('globalService', callback)
50
+ },
51
+ }
52
+ },
53
+ on: (outerEvent: string, callback: any) => {
54
+ eventEmitter.addListener(outerEvent, callback)
55
+
56
+ return {
57
+ unsubscribe: () => {
58
+ eventEmitter.removeListener(outerEvent, callback)
59
+ },
60
+ }
61
+ },
62
+ getSeedClass: async () => {
63
+ return new Promise((resolve) => {
64
+ const subscription = globalService.subscribe((snapshot) => {
65
+ if (snapshot.status === 'done') {
66
+ resolve(snapshot.output)
67
+ }
68
+ })
69
+
70
+ globalService.send({ type: 'getSeed' })
71
+ subscription.unsubscribe()
72
+ })
73
+ },
74
+ getModel: (modelName: string) => {
75
+ return getModel(modelName)
76
+ },
77
+ getModels: (): Record<string, ModelClassType> => {
78
+ return getModels()
79
+ },
80
+ getModelNames: (): string[] => {
81
+ return getModelNames()
82
+ },
83
+ }
84
+
85
+ export { client }
@@ -0,0 +1,81 @@
1
+ import { EventObject, fromCallback } from 'xstate'
2
+ import { areFsListenersReady, isFsInitialized } from '@/browser/events/files'
3
+ import { waitForEvent } from '@/browser/events'
4
+ import {
5
+ BROWSER_FS_TOP_DIR,
6
+ DB_WAITING_FOR_FILES_RECEIVED,
7
+ INTERNAL_CONFIGURING_FS_SUCCESS,
8
+ } from '@/browser/services/internal/constants'
9
+ import { fs } from '@zenfs/core'
10
+ import debug from 'debug'
11
+ import { FromCallbackInput, InternalMachineContext } from '@/types'
12
+
13
+ const logger = debug('app:internal:actors:configureFs')
14
+
15
+ export const configureFs = fromCallback<
16
+ EventObject,
17
+ FromCallbackInput<InternalMachineContext>
18
+ >(({ sendBack, input: { context } }) => {
19
+ const { endpoints, appDbService } = context
20
+
21
+ logger('[internal/actors] [configureFs] Configuring FS')
22
+
23
+ const _configureFs = async (): Promise<void> => {
24
+ logger('[internal/actors] [configureFs] calling _configureFs')
25
+
26
+ logger(
27
+ '[internal/actors] [configureFs] areFsListenersReady:',
28
+ areFsListenersReady(),
29
+ )
30
+ logger(
31
+ '[internal/actors] [configureFs] isFsInitialized:',
32
+ isFsInitialized(),
33
+ )
34
+
35
+ await waitForEvent({
36
+ req: {
37
+ eventLabel: 'fs.downloadAll.request',
38
+ data: { endpoints },
39
+ },
40
+ res: {
41
+ eventLabel: 'fs.downloadAll.success',
42
+ },
43
+ })
44
+
45
+ const journalPath = `${BROWSER_FS_TOP_DIR}/db/meta/_journal.json`
46
+
47
+ const journalExists = await fs.promises.exists(journalPath)
48
+
49
+ if (journalExists) {
50
+ appDbService.send({ type: DB_WAITING_FOR_FILES_RECEIVED })
51
+ }
52
+
53
+ // return new Promise<void>((resolve) => {
54
+ // const interval = setInterval(() => {
55
+ // journalExistsSync = fs.existsSync(journalPath)
56
+ // logger(
57
+ // '[internal/actors] [configureFs] journalExistsSync:',
58
+ // journalExistsSync,
59
+ // )
60
+ // if (journalExistsSync) {
61
+ // service.send({ type: DB_WAITING_FOR_FILES_RECEIVED })
62
+ // clearInterval(interval)
63
+ // resolve()
64
+ // }
65
+ // }, 200)
66
+ // })
67
+
68
+ logger('[internal/actors] [configureFs] fs configured!')
69
+ }
70
+
71
+ // Some of our dependencies use fs sync functions, which don't work with
72
+ // OPFS. ZenFS creates an async cache of all files so that the sync functions
73
+ // work, but we have to wait for it to be built. Otherwise things like
74
+ // drizzleMigrate will fail since they can't see the migration files yet.
75
+ _configureFs().then(() => {
76
+ sendBack({ type: INTERNAL_CONFIGURING_FS_SUCCESS })
77
+ return
78
+ })
79
+
80
+ return () => {}
81
+ })
@@ -0,0 +1,74 @@
1
+ import { EventObject, fromCallback } from 'xstate'
2
+ import { DbServiceContext, FromCallbackInput } from '@/types'
3
+ import { getSqliteWasmClient } from '@/browser/db/sqlWasmClient'
4
+ import { DB_CREATING_SUCCESS } from '@/browser/services/internal/constants'
5
+ import debug from 'debug'
6
+
7
+ const logger = debug('app:services:db:actors:connectToDb')
8
+
9
+ export const connectToDb = fromCallback<
10
+ EventObject,
11
+ FromCallbackInput<DbServiceContext>
12
+ >(({ sendBack, input: { context } }) => {
13
+ logger('[db/actors] connectToDb context', context)
14
+
15
+ const { dbName, pathToDir } = context
16
+
17
+ let isConnecting = false
18
+ let dbId: string | undefined
19
+
20
+ const _create = async (): Promise<void> => {
21
+ if (isConnecting) {
22
+ return
23
+ }
24
+ isConnecting = true
25
+ let response
26
+
27
+ const sqliteWasmClient = await getSqliteWasmClient()
28
+
29
+ //@ts-ignore
30
+ response = await sqliteWasmClient('config-get', {})
31
+ logger(response)
32
+ logger('Running SQLite3 version', response.result.version.libVersion)
33
+
34
+ //@ts-ignore
35
+ response = await sqliteWasmClient('open', {
36
+ filename: `file:${pathToDir}/db/${dbName}.sqlite3?vfs=opfs`,
37
+ })
38
+
39
+ logger(response)
40
+ dbId = response.dbId
41
+ // logger(`dbId: ${dbId}`)
42
+ logger(
43
+ 'OPFS is available, created persisted database at',
44
+ response.result.filename.replace(/^file:(.*?)\?vfs=opfs$/, '$1'),
45
+ )
46
+ }
47
+
48
+ const interval = setInterval(() => {
49
+ // TODO: Add a timeout
50
+ // TODO: Add a cancel token to the promise so we can prevent more loops starting while we're checking the successful outcome
51
+ if (dbId) {
52
+ // logger(
53
+ // '[db/actors] opening sqliteWasm connection with dbId:',
54
+ // dbId,
55
+ // )
56
+ clearInterval(interval)
57
+ sendBack({ type: DB_CREATING_SUCCESS, dbId })
58
+ return
59
+ }
60
+ _create()
61
+ .then(() => {
62
+ return
63
+ })
64
+ .catch((e) => {
65
+ isConnecting = false
66
+ })
67
+ }, 500)
68
+
69
+ return () => {
70
+ if (interval) {
71
+ clearInterval(interval)
72
+ }
73
+ }
74
+ })
@@ -0,0 +1,67 @@
1
+ type SqliteDatabase = {
2
+ open: (filename: string) => Promise<void>
3
+ exec: (sql: string, params?: any[]) => any
4
+ close: () => void
5
+ }
6
+
7
+ class SqliteConnectionManager {
8
+ private sqliteModule: SqliteDatabase
9
+ private idleTimeout: number
10
+ private databases: { [key: string]: SqliteDatabase }
11
+ private idleTimers: { [key: string]: NodeJS.Timeout }
12
+
13
+ constructor(sqliteModule: SqliteDatabase, idleTimeout: number = 300000) {
14
+ // Default idle timeout: 5 minutes
15
+ this.sqliteModule = sqliteModule
16
+ this.idleTimeout = idleTimeout
17
+ this.databases = {}
18
+ this.idleTimers = {}
19
+ }
20
+
21
+ private resetIdleTimer(dbName: string): void {
22
+ if (this.idleTimers[dbName]) {
23
+ clearTimeout(this.idleTimers[dbName])
24
+ }
25
+
26
+ this.idleTimers[dbName] = setTimeout(() => {
27
+ this.closeConnection(dbName)
28
+ }, this.idleTimeout)
29
+ }
30
+
31
+ private async getConnection(dbName: string): Promise<SqliteDatabase> {
32
+ if (this.databases[dbName]) {
33
+ this.resetIdleTimer(dbName)
34
+ return this.databases[dbName]
35
+ }
36
+
37
+ const db = new this.sqliteModule()
38
+ await db.open(dbName)
39
+ this.databases[dbName] = db
40
+ this.resetIdleTimer(dbName)
41
+ return db
42
+ }
43
+
44
+ public async execute(
45
+ dbName: string,
46
+ sql: string,
47
+ params: any[] = [],
48
+ ): Promise<any> {
49
+ const db = await this.getConnection(dbName)
50
+ const result = db.exec(sql, params)
51
+ this.resetIdleTimer(dbName)
52
+ return result
53
+ }
54
+
55
+ public closeConnection(dbName: string): void {
56
+ if (this.databases[dbName]) {
57
+ this.databases[dbName].close()
58
+ delete this.databases[dbName]
59
+ if (this.idleTimers[dbName]) {
60
+ clearTimeout(this.idleTimers[dbName])
61
+ delete this.idleTimers[dbName]
62
+ }
63
+ }
64
+ }
65
+ }
66
+
67
+ export { SqliteConnectionManager }
@@ -0,0 +1,118 @@
1
+ const MACHINE_ID_SCOPE = '@seedSdk'
2
+
3
+ export enum MachineIds {
4
+ GLOBAL = `${MACHINE_ID_SCOPE}/global`,
5
+ INTERNAL = `${MACHINE_ID_SCOPE}/internal`,
6
+ DB = `${MACHINE_ID_SCOPE}/db`,
7
+ EAS = `${MACHINE_ID_SCOPE}/eas`,
8
+ ITEM = `${MACHINE_ID_SCOPE}/item`,
9
+ ALL_ITEMS = `${MACHINE_ID_SCOPE}/allItems`,
10
+ MODEL = `${MACHINE_ID_SCOPE}/model`,
11
+ FILE_SYSTEM = `${MACHINE_ID_SCOPE}/fileSystem`,
12
+ }
13
+
14
+ const { INTERNAL, DB, GLOBAL, EAS, MODEL } = MachineIds
15
+
16
+ export enum GlobalState {
17
+ UNINITIALIZED = 'uninitialized',
18
+ INITIALIZING = 'initializing',
19
+ INITIALIZED = 'initialized',
20
+ PUBLISHING_ITEM = 'publishingItem',
21
+ ADDING_MODELS_TO_DB = 'addingModelsToDb',
22
+ }
23
+
24
+ const {
25
+ UNINITIALIZED,
26
+ INITIALIZING,
27
+ INITIALIZED,
28
+ PUBLISHING_ITEM,
29
+ ADDING_MODELS_TO_DB,
30
+ } = GlobalState
31
+
32
+ export enum InternalState {
33
+ IDLE = 'idle',
34
+ INITIALIZING = 'initializing',
35
+ VALIDATING_INPUT = 'validatingInput',
36
+ CONFIGURING_FS = 'configuringFs',
37
+ LOADING_SEED_DB = 'loadingSeedDb',
38
+ SAVING_CONFIG = 'savingConfig',
39
+ LOADING_APP_DB = 'loadingAppDb',
40
+ LOADING_SDK_DB = 'loadingSdkDb',
41
+ }
42
+
43
+ const { VALIDATING_INPUT, CONFIGURING_FS, LOADING_SEED_DB, LOADING_APP_DB } =
44
+ InternalState
45
+
46
+ export enum DbState {
47
+ CHECKING_STATUS = 'checkingStatus',
48
+ WAITING_FOR_FILES = 'waitingForFiles',
49
+ VALIDATING = 'validating',
50
+ CONNECTING_TO_DB = 'connectingToDb',
51
+ // FETCHING_MIGRATIONS = 'fetchingMigrations',
52
+ MIGRATING = 'migrating',
53
+ }
54
+
55
+ const {
56
+ CHECKING_STATUS,
57
+ VALIDATING,
58
+ CONNECTING_TO_DB,
59
+ WAITING_FOR_FILES,
60
+ MIGRATING,
61
+ } = DbState
62
+
63
+ export enum PublishMachineStates {
64
+ VALIDATING_ITEM_DATA = 'validatingItemData',
65
+ CREATING_PUBLISH_ATTEMPT = 'creatingPublishAttempt',
66
+ UPLOADING = 'uploading',
67
+ PREPARING_PUBLISH_REQUEST_DATA = 'preparingPublishRequestData',
68
+ PUBLISHING = 'publishing',
69
+ IDLE = 'idle',
70
+ }
71
+
72
+ export const INTERNAL_VALIDATING_INPUT_SUCCESS = `${INTERNAL}.${VALIDATING_INPUT}.success`
73
+
74
+ export const INTERNAL_CONFIGURING_FS_SUCCESS = `${INTERNAL}.${CONFIGURING_FS}.success`
75
+ export const INTERNAL_CONFIGURING_FS_FAILURE = `${INTERNAL}.${CONFIGURING_FS}.failure`
76
+
77
+ export const INTERNAL_LOADING_SEED_DB_SUCCESS = `${INTERNAL}.${LOADING_SEED_DB}.success`
78
+ export const INTERNAL_LOADING_SEED_DB_FAILURE = `${INTERNAL}.${LOADING_SEED_DB}.failure`
79
+
80
+ export const INTERNAL_LOADING_APP_DB_SUCCESS = `${INTERNAL}.${LOADING_APP_DB}.success`
81
+ export const INTERNAL_LOADING_APP_DB_FAILURE = `${INTERNAL}.${LOADING_APP_DB}.failure`
82
+
83
+ export const INTERNAL_SAVING_CONFIG_SUCCESS = `${INTERNAL}.savingConfig.success`
84
+ export const INTERNAL_SAVING_CONFIG_FAILURE = `${INTERNAL}.savingConfig.failure`
85
+
86
+ export const GLOBAL_GETTING_SEED_CLASS_SUCCESS = `${GLOBAL}.${PUBLISHING_ITEM}.success`
87
+ export const GLOBAL_INITIALIZING_SEND_CONFIG = `${GLOBAL}.${INITIALIZING}.sendConfig`
88
+ export const GLOBAL_INITIALIZING_INTERNAL_SERVICE_READY = `${GLOBAL}.${INITIALIZING}.internalServiceReady`
89
+ export const GLOBAL_INITIALIZING_CREATE_ALL_ITEMS_SERVICES = `${GLOBAL}.${INITIALIZING}.createAllItemsServices`
90
+ export const GLOBAL_ADDING_MODELS_TO_DB_SUCCESS = `${GLOBAL}.${ADDING_MODELS_TO_DB}.success`
91
+
92
+ export const DB_CHECK_STATUS_UPDATE_PATHS = `${DB}.${CHECKING_STATUS}.updatePaths`
93
+ export const DB_CHECK_STATUS_EXISTS = `${DB}.${CHECKING_STATUS}.exists`
94
+ export const DB_CHECK_STATUS_DOES_NOT_EXIST = `${DB}.${CHECKING_STATUS}.doesNotExist`
95
+ export const DB_CHECK_STATUS_FAILURE = `${DB}.${CHECKING_STATUS}.failure`
96
+
97
+ export const DB_VALIDATING_SUCCESS = `${DB}.${VALIDATING}.success`
98
+ export const DB_VALIDATING_WAIT = `${DB}.${VALIDATING}.wait`
99
+ export const DB_MIGRATING_WAIT = `${DB}.${MIGRATING}.wait`
100
+ export const DB_MIGRATING_SUCCESS = `${DB}.${MIGRATING}.success`
101
+ export const DB_CREATING_SUCCESS = `${DB}.${CONNECTING_TO_DB}.success`
102
+ export const DB_WAITING_FOR_FILES_RECEIVED = `${DB}.${WAITING_FOR_FILES}.filesReceived`
103
+ export const DB_ON_SNAPSHOT = `${DB}.onSnapshot`
104
+ export const CHILD_SNAPSHOT = 'childSnapshot'
105
+
106
+ export const INTERNAL_SERVICE_SNAPSHOT = 'internalServiceSnapshot'
107
+
108
+ export const DB_NAME_APP = 'app_db'
109
+ export const BROWSER_FS_TOP_DIR = 'app-files'
110
+
111
+ export const EAS_ENDPOINT =
112
+ process.env.NEXT_PUBLIC_EAS_ENDPOINT ||
113
+ process.env.EAS_ENDPOINT ||
114
+ 'https://optimism-sepolia.easscan.org/graphql'
115
+
116
+ export const ARWEAVE_HOST =
117
+ process.env.NEXT_PUBLIC_ARWEAVE_HOST || 'permagate.io'
118
+ export const ARWEAVE_ENDPOINT = `https://${ARWEAVE_HOST}/graphql`
@@ -0,0 +1,39 @@
1
+ import { Item } from 'src/browser/item'
2
+ import { eventEmitter } from '@/eventBus'
3
+ import { createItemCacheKey, getItemCache, updateItemCache } from './requestAll'
4
+
5
+ export const createItemRequestHandler = async (event) => {
6
+ const { ModelClass, itemData } = event
7
+
8
+ const itemCache = getItemCache()
9
+
10
+ const itemCacheKey = createItemCacheKey(itemData)
11
+
12
+ if (!itemCacheKey) {
13
+ console.warn('itemCacheKey not found for itemData', itemData)
14
+ return
15
+ }
16
+
17
+ if (itemCache.has(itemCacheKey)) {
18
+ return
19
+ }
20
+
21
+ const newItem = Item.create({
22
+ modelName: ModelClass.originalConstructor.name,
23
+ ...itemData,
24
+ })
25
+
26
+ updateItemCache(itemCacheKey, newItem)
27
+
28
+ return new Promise<void>((resolve) => {
29
+ const subscription = newItem.subscribe((context) => {
30
+ if (context && context.versionLocalId && context.seedLocalId) {
31
+ subscription.unsubscribe()
32
+ // allItemsService.send({ type: 'addItemToContext', item: newItem })
33
+ eventEmitter.emit('item.create.response', { item: newItem })
34
+ // eventEmitter.emit('item.requestAll', { modelName })
35
+ resolve()
36
+ }
37
+ })
38
+ })
39
+ }
@@ -0,0 +1,15 @@
1
+ import { EventObject, fromCallback } from 'xstate'
2
+ import { createItemMachine } from '../createItemMachine'
3
+
4
+ export const createItem = fromCallback<EventObject, typeof createItemMachine>(
5
+ ({ sendBack, input: { context, event } }) => {
6
+ const { item } = context
7
+
8
+ if (!item) {
9
+ console.warn('No item found')
10
+ return
11
+ }
12
+
13
+ sendBack({ type: 'itemCreated', item })
14
+ },
15
+ )
@@ -0,0 +1,37 @@
1
+ import { setup } from 'xstate'
2
+
3
+ const createItemMachine = setup({
4
+ types: {
5
+ context: {},
6
+ input: {},
7
+ },
8
+ }).createMachine({
9
+ id: 'createItem',
10
+ initial: 'creating',
11
+ context: ({ input }) => input as any,
12
+ states: {
13
+ idle: {
14
+ on: {
15
+ create: 'creating',
16
+ },
17
+ },
18
+ creating: {
19
+ invoke: {
20
+ src: 'createItem',
21
+ onDone: {
22
+ target: 'created',
23
+ actions: 'onSuccess',
24
+ },
25
+ onError: {
26
+ target: 'idle',
27
+ actions: 'onError',
28
+ },
29
+ },
30
+ },
31
+ created: {
32
+ type: 'final',
33
+ },
34
+ },
35
+ })
36
+
37
+ export { createItemMachine }
@@ -0,0 +1,16 @@
1
+ import { EventObject, fromCallback } from 'xstate'
2
+ import { FromCallbackInput, PublishMachineContext } from '@/types'
3
+
4
+ export const createPublishAttempt = fromCallback<
5
+ EventObject,
6
+ FromCallbackInput<PublishMachineContext>
7
+ >(({ sendBack, input: { context } }) => {
8
+ const _createPublishAttempt = async () => {
9
+ // Do some stuff
10
+ return true
11
+ }
12
+
13
+ _createPublishAttempt().then(() => {
14
+ sendBack({ type: 'createPublishAttemptSuccess' })
15
+ })
16
+ })
@@ -0,0 +1,24 @@
1
+ import { seeds, SeedType } from '@/shared/seedSchema'
2
+ import { getAppDb } from '@/browser/db/sqlWasmClient'
3
+
4
+ type CreateSeeds = (newSeeds: Partial<SeedType>[]) => Promise<string[]>
5
+
6
+ export const createSeeds: CreateSeeds = async (
7
+ newSeeds: Partial<SeedType>[],
8
+ ) => {
9
+ const appDb = getAppDb()
10
+
11
+ const results = await appDb
12
+ .insert(seeds)
13
+ .values(newSeeds)
14
+ .returning({ uid: seeds.uid })
15
+
16
+ const newUids = results.reduce((acc, result) => {
17
+ if (result.uid) {
18
+ acc.push(result.uid)
19
+ }
20
+ return acc
21
+ }, [] as string[])
22
+
23
+ return newUids
24
+ }
@@ -0,0 +1,33 @@
1
+ import { getAppDb } from '../sqlWasmClient'
2
+ import { generateId } from '@/shared/helpers'
3
+ import { versions } from '@/shared/seedSchema'
4
+
5
+ type CreateVersionProps = {
6
+ seedLocalId?: string
7
+ seedUid?: string
8
+ seedType?: string
9
+ uid?: string
10
+ }
11
+ type CreateVersion = (props: CreateVersionProps) => Promise<string>
12
+
13
+ export const createVersion: CreateVersion = async ({
14
+ seedLocalId,
15
+ seedUid,
16
+ seedType,
17
+ uid,
18
+ }) => {
19
+ const appDb = getAppDb()
20
+
21
+ const newVersionLocalId = generateId()
22
+
23
+ await appDb.insert(versions).values({
24
+ localId: newVersionLocalId,
25
+ createdAt: Date.now(),
26
+ seedLocalId,
27
+ seedUid: seedUid ?? 'NULL',
28
+ seedType,
29
+ uid: uid || 'NULL',
30
+ })
31
+
32
+ return newVersionLocalId
33
+ }