@seedprotocol/sdk 0.2.54 → 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.
- package/dist/{ArweaveClient-D5iJisYr.js → ArweaveClient-C0g0es2v.js} +2 -2
- package/dist/{ArweaveClient-D5iJisYr.js.map → ArweaveClient-C0g0es2v.js.map} +1 -1
- package/dist/{ArweaveClient-HEIIuOfB.js → ArweaveClient-CzkBgiZY.js} +2 -2
- package/dist/{ArweaveClient-HEIIuOfB.js.map → ArweaveClient-CzkBgiZY.js.map} +1 -1
- package/dist/{Db-jMth8Y2g.js → Db-C4oog-0n.js} +4 -3
- package/dist/{Db-jMth8Y2g.js.map → Db-C4oog-0n.js.map} +1 -1
- package/dist/Db-VPouegkr.js +82 -0
- package/dist/Db-VPouegkr.js.map +1 -0
- package/dist/EasClient-B9a3NsTi.js +10 -0
- package/dist/EasClient-B9a3NsTi.js.map +1 -0
- package/dist/{EasClient-D3wKgt0o.js → EasClient-DQ93i2ci.js} +2 -2
- package/dist/{EasClient-D3wKgt0o.js.map → EasClient-DQ93i2ci.js.map} +1 -1
- package/dist/{FileManager-4mWYO3Mt.js → FileManager-_ErxVuwb.js} +9 -5
- package/dist/FileManager-_ErxVuwb.js.map +1 -0
- package/dist/{Item-CbgjLSUx.js → Item-D9HFPxsK.js} +4 -3
- package/dist/{Item-CbgjLSUx.js.map → Item-D9HFPxsK.js.map} +1 -1
- package/dist/{ItemProperty-DduurIcl.js → ItemProperty-BdtvKrEy.js} +4 -3
- package/dist/{ItemProperty-DduurIcl.js.map → ItemProperty-BdtvKrEy.js.map} +1 -1
- package/dist/QueryClient-Bmnv_moN.js +22 -0
- package/dist/QueryClient-Bmnv_moN.js.map +1 -0
- package/dist/{QueryClient-CBOP3OoU.js → QueryClient-hzOtFe99.js} +3 -2
- package/dist/{QueryClient-CBOP3OoU.js.map → QueryClient-hzOtFe99.js.map} +1 -1
- package/dist/bin.js +30 -34
- package/dist/bin.js.map +1 -1
- package/dist/{constants-CIDbABDz.js → constants-Bhl5dZpN.js} +9 -4
- package/dist/constants-Bhl5dZpN.js.map +1 -0
- package/dist/db/configs/node.app.db.config.ts +27 -0
- package/dist/{index-DQNTvzqi.js → index-BAtjd9W-.js} +4 -3
- package/dist/index-BAtjd9W-.js.map +1 -0
- package/dist/{index-CTZqN6WI.js → index-BSR5hv7Q.js} +305 -268
- package/dist/index-BSR5hv7Q.js.map +1 -0
- package/dist/main.js +3 -2
- package/dist/main.js.map +1 -1
- package/dist/node/db/node.app.db.config.ts +1 -1
- package/dist/{seed.schema.config-B-4U2RRq.js → seed.schema.config-BxJYabim.js} +10 -9
- package/dist/seed.schema.config-BxJYabim.js.map +1 -0
- package/dist/src/AppStateSchema.ts +10 -0
- package/dist/src/ArweaveClient.ts +13 -0
- package/dist/src/Attestation.ts +21 -0
- package/dist/src/BaseArweaveClient.ts +13 -0
- package/dist/src/BaseClientManager.ts +35 -0
- package/dist/src/BaseDb.ts +35 -0
- package/dist/src/BaseEasClient.ts +14 -0
- package/dist/src/BaseFileManager.ts +60 -0
- package/dist/src/BaseItem.ts +413 -0
- package/dist/src/BaseItemProperty.ts +416 -0
- package/dist/src/BaseQueryClient.ts +13 -0
- package/dist/src/ClientManager.ts +63 -0
- package/dist/src/ConfigSchema.ts +15 -0
- package/dist/src/Db.ts +82 -0
- package/dist/src/EasClient.ts +13 -0
- package/dist/src/FileDownloader.ts +63 -0
- package/dist/src/FileManager.ts +59 -0
- package/dist/src/IDb.ts +7 -0
- package/dist/src/IEasClient.ts +2 -0
- package/dist/src/IItem.ts +26 -0
- package/dist/src/IItemProperty.ts +28 -0
- package/dist/src/IQueryClient.ts +11 -0
- package/dist/src/ImageResizer.ts +81 -0
- package/dist/src/Item.ts +12 -0
- package/dist/src/ItemProperty.ts +9 -0
- package/dist/src/MetadataSchema.ts +28 -0
- package/dist/src/ModelSchema.ts +46 -0
- package/dist/src/ModelUidSchema.ts +16 -0
- package/dist/src/PropertyUidSchema.ts +16 -0
- package/dist/src/QueryClient.ts +26 -0
- package/dist/src/Schema.ts +17 -0
- package/dist/src/SeedProtocolSdk.d.ts +19 -0
- package/dist/src/SeedSchema.ts +29 -0
- package/dist/src/VersionSchema.ts +16 -0
- package/dist/src/actors.ts +295 -0
- package/dist/src/addModelsToDb.ts +152 -0
- package/dist/src/allItems.ts +31 -0
- package/dist/src/analyzeInput.ts +144 -0
- package/dist/src/browser.app.db.config.ts +27 -0
- package/dist/src/browser.ts +30 -0
- package/dist/src/checkStatus.ts +57 -0
- package/dist/src/clientManagerMachine.ts +59 -0
- package/dist/src/configureFs.ts +98 -0
- package/dist/src/connectToDb.ts +41 -0
- package/dist/src/connectionManager.ts +67 -0
- package/dist/src/constants.ts +125 -0
- package/dist/src/content-hash.ts +30 -0
- package/dist/src/create.ts +39 -0
- package/dist/src/createMetadata.ts +77 -0
- package/dist/src/createNewItem.ts +61 -0
- package/dist/src/createPublishAttempt.ts +16 -0
- package/dist/src/createSeed.ts +31 -0
- package/dist/src/createSeeds.ts +24 -0
- package/dist/src/createVersion.ts +33 -0
- package/dist/src/dbMachine.ts +182 -0
- package/dist/src/deleteItem.ts +19 -0
- package/dist/src/download.ts +208 -0
- package/dist/src/drizzle.ts +78 -0
- package/dist/src/eas.ts +40 -0
- package/dist/src/environment.ts +35 -0
- package/dist/src/eventBus.ts +5 -0
- package/dist/src/events.ts +14 -0
- package/dist/src/fetchDataFromEas.ts +94 -0
- package/dist/src/fetchDbData.ts +19 -0
- package/dist/src/fetchRelatedItems.ts +184 -0
- package/dist/src/fetchSeeds.ts +45 -0
- package/dist/src/fetchVersions.ts +49 -0
- package/dist/src/files.ts +81 -0
- package/dist/src/filesDownload.ts +326 -0
- package/dist/src/fragment-masking.ts +87 -0
- package/dist/src/getExistingItem.ts +60 -0
- package/dist/src/getItem.ts +32 -0
- package/dist/src/getItemData.ts +99 -0
- package/dist/src/getItemProperties.ts +56 -0
- package/dist/src/getItemProperty.ts +34 -0
- package/dist/src/getItems.ts +70 -0
- package/dist/src/getMetadata.ts +40 -0
- package/dist/src/getModelSchemas.ts +89 -0
- package/dist/src/getPropertyData.ts +50 -0
- package/dist/src/getPublishPayload.ts +355 -0
- package/dist/src/getPublishUploads.ts +207 -0
- package/dist/src/getRelationValueData.ts +27 -0
- package/dist/src/getSchemaForItemProperty.ts +68 -0
- package/dist/src/getSchemaForModel.ts +42 -0
- package/dist/src/getSchemaUidForModel.ts +32 -0
- package/dist/src/getSeedData.ts +34 -0
- package/dist/src/getSegmentedItemProperties.ts +65 -0
- package/dist/src/getStorageTransactionIdForSeedUid.ts +38 -0
- package/dist/src/getVersionData.ts +46 -0
- package/dist/src/getVersionsForVersionUids.ts +39 -0
- package/dist/src/globalMachine.ts +280 -0
- package/dist/src/gql.ts +118 -0
- package/dist/src/graphql.ts +3209 -0
- package/dist/src/helpers.ts +273 -0
- package/dist/src/hydrateExistingItem.ts +137 -0
- package/dist/src/hydrateFromDb.ts +270 -0
- package/dist/src/hydrateNewItem.ts +34 -0
- package/dist/src/imageResize.ts +507 -0
- package/dist/src/index.d.ts +5 -0
- package/dist/src/index.ts +4 -0
- package/dist/src/initialize.ts +124 -0
- package/dist/src/internalMachine.ts +241 -0
- package/dist/src/itemMachineAll.ts +157 -0
- package/dist/src/itemMachineSingle.ts +158 -0
- package/dist/src/loadAppDb.ts +44 -0
- package/dist/src/machines.ts +66 -0
- package/dist/src/metadataLatest.ts +34 -0
- package/dist/src/migrate.ts +57 -0
- package/dist/src/model.ts +72 -0
- package/dist/src/modelClass.ts +19 -0
- package/dist/src/node.app.db.config.ts +41 -0
- package/dist/src/prepareDb.ts +29 -0
- package/dist/src/preparePublishRequestData.ts +81 -0
- package/dist/src/processItems.ts +74 -0
- package/dist/src/property.ts +156 -0
- package/dist/src/propertyMachine.ts +202 -0
- package/dist/src/publish.ts +28 -0
- package/dist/src/publishMachine.ts +77 -0
- package/dist/src/queries.ts +13 -0
- package/dist/src/recoverDeletedItem.ts +14 -0
- package/dist/src/reload.ts +33 -0
- package/dist/src/request.ts +45 -0
- package/dist/src/requestAll.ts +157 -0
- package/dist/src/resolveRelatedValue.ts +367 -0
- package/dist/src/resolveRemoteStorage.ts +88 -0
- package/dist/src/saveAppState.ts +50 -0
- package/dist/src/saveConfig.ts +106 -0
- package/dist/src/saveDataToDb.ts +145 -0
- package/dist/src/saveImageSrc.ts +242 -0
- package/dist/src/saveItemStorage.ts +156 -0
- package/dist/src/saveMetadata.ts +18 -0
- package/dist/src/savePublishService.ts +30 -0
- package/dist/src/saveRelation.ts +112 -0
- package/dist/src/seed.schema.config.ts +25 -0
- package/dist/src/seed.ts +10 -0
- package/dist/src/seedProtocol.ts +17 -0
- package/dist/src/services.ts +365 -0
- package/dist/src/setAddresses.ts +16 -0
- package/dist/src/sqlWasmClient.ts +88 -0
- package/dist/src/syncDbWithEas.ts +628 -0
- package/dist/src/trash.ts +29 -0
- package/dist/src/ts-to-proto.ts +101 -0
- package/dist/src/types.ts +12 -0
- package/dist/src/updateItemPropertyValue.ts +243 -0
- package/dist/src/updateMachineContext.ts +21 -0
- package/dist/src/updateMetadata.ts +59 -0
- package/dist/src/upload.ts +86 -0
- package/dist/src/validate.ts +45 -0
- package/dist/src/validateInput.ts +43 -0
- package/dist/src/validateItemData.ts +20 -0
- package/dist/src/versionData.ts +24 -0
- package/dist/src/waitForDb.ts +25 -0
- package/dist/src/waitForFiles.ts +41 -0
- package/dist/src/wasm.d.ts +8300 -0
- package/dist/types/scripts/bin.d.ts.map +1 -1
- package/dist/types/src/browser/helpers/EasClient.d.ts +1 -3
- package/dist/types/src/browser/helpers/EasClient.d.ts.map +1 -1
- package/dist/types/src/browser/helpers/FileManager.d.ts +2 -1
- package/dist/types/src/browser/helpers/FileManager.d.ts.map +1 -1
- package/dist/types/src/client/ClientManager.d.ts +44 -0
- package/dist/types/src/client/ClientManager.d.ts.map +1 -0
- package/dist/types/src/client/actions/setAddresses.d.ts.map +1 -1
- package/dist/types/src/client/actors/initialize.d.ts.map +1 -1
- package/dist/types/src/client/actors/saveAppState.d.ts.map +1 -1
- package/dist/types/src/client/clientManagerMachine.d.ts.map +1 -1
- package/dist/types/src/client/index.d.ts +2 -40
- package/dist/types/src/client/index.d.ts.map +1 -1
- package/dist/types/src/db/configs/node.app.db.config.d.ts +3 -0
- package/dist/types/src/db/configs/node.app.db.config.d.ts.map +1 -0
- package/dist/types/src/events/files/download.d.ts.map +1 -1
- package/dist/types/src/helpers/EasClient/BaseEasClient.d.ts.map +1 -1
- package/dist/types/src/helpers/FileManager/BaseFileManager.d.ts +1 -0
- package/dist/types/src/helpers/FileManager/BaseFileManager.d.ts.map +1 -1
- package/dist/types/src/helpers/constants.d.ts +1 -0
- package/dist/types/src/helpers/constants.d.ts.map +1 -1
- package/dist/types/src/interfaces/IQueryClient.d.ts +7 -1
- package/dist/types/src/interfaces/IQueryClient.d.ts.map +1 -1
- package/dist/types/src/node/db/Db.d.ts +5 -10
- package/dist/types/src/node/db/Db.d.ts.map +1 -1
- package/dist/types/src/node/helpers/FileManager.d.ts +2 -1
- package/dist/types/src/node/helpers/FileManager.d.ts.map +1 -1
- package/dist/types/src/node/helpers/QueryClient.d.ts +2 -4
- package/dist/types/src/node/helpers/QueryClient.d.ts.map +1 -1
- package/dist/types/src/services/allItems/actors/fetchSeeds.d.ts.map +1 -1
- package/dist/types/src/services/db/actors/checkStatus.d.ts.map +1 -1
- package/dist/types/src/services/db/actors/migrate.d.ts.map +1 -1
- package/dist/types/src/services/db/actors/waitForFiles.d.ts.map +1 -1
- package/dist/types/src/services/internal/actors/configureFs.d.ts.map +1 -1
- package/dist/types/src/services/internal/actors/loadAppDb.d.ts.map +1 -1
- package/dist/types/src/services/internal/actors/saveConfig.d.ts.map +1 -1
- package/dist/types/src/services/internal/actors/waitForFiles.d.ts.map +1 -1
- package/dist/types/src/services/internal/internalMachine.d.ts.map +1 -1
- package/package.json +10 -10
- package/dist/Db-DK9wSNIH.js +0 -120
- package/dist/Db-DK9wSNIH.js.map +0 -1
- package/dist/EasClient-jqkT2ZBt.js +0 -13
- package/dist/EasClient-jqkT2ZBt.js.map +0 -1
- package/dist/FileManager-4mWYO3Mt.js.map +0 -1
- package/dist/QueryClient-bKb3SB0W.js +0 -23
- package/dist/QueryClient-bKb3SB0W.js.map +0 -1
- package/dist/constants-CIDbABDz.js.map +0 -1
- package/dist/index-CTZqN6WI.js.map +0 -1
- package/dist/index-DQNTvzqi.js.map +0 -1
- package/dist/package.json +0 -189
- package/dist/seed.schema.config-B-4U2RRq.js.map +0 -1
- package/dist/types/src/client.d.ts +0 -18
- package/dist/types/src/client.d.ts.map +0 -1
|
@@ -0,0 +1,295 @@
|
|
|
1
|
+
import { EventObject, fromCallback } from 'xstate'
|
|
2
|
+
import { fetchAllFilesMachine } from '@/schema/file/fetchAll/index'
|
|
3
|
+
import { ARWEAVE_HOST } from '@/services/internal/constants'
|
|
4
|
+
import { GET_FILES_METADATA } from '@/schema/file/queries'
|
|
5
|
+
import { getArweave } from '@/helpers/ArweaveClient'
|
|
6
|
+
import fs from '@zenfs/core'
|
|
7
|
+
import {
|
|
8
|
+
getDataTypeFromString,
|
|
9
|
+
getMimeType,
|
|
10
|
+
identifyString,
|
|
11
|
+
} from '@/helpers'
|
|
12
|
+
import { appState } from '@/seedSchema'
|
|
13
|
+
import { eq } from 'drizzle-orm'
|
|
14
|
+
import { BaseEasClient } from '@/helpers/EasClient/BaseEasClient'
|
|
15
|
+
import { BaseQueryClient } from '@/helpers/QueryClient/BaseQueryClient'
|
|
16
|
+
import debug from 'debug'
|
|
17
|
+
import { BaseDb } from '@/db/Db/BaseDb'
|
|
18
|
+
import { saveAppState } from '@/db/write/saveAppState'
|
|
19
|
+
|
|
20
|
+
const logger = debug('app:file:actors:fetchAll')
|
|
21
|
+
|
|
22
|
+
type FileType = {
|
|
23
|
+
mimeType: string
|
|
24
|
+
extension: string
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// Map of common MIME types to file extensions
|
|
28
|
+
const fileTypeMap: Record<string, FileType> = {
|
|
29
|
+
'image/jpeg': { mimeType: 'image/jpeg', extension: '.jpg' },
|
|
30
|
+
'image/png': { mimeType: 'image/png', extension: '.png' },
|
|
31
|
+
'application/json': { mimeType: 'application/json', extension: '.json' },
|
|
32
|
+
'text/plain': { mimeType: 'text/plain', extension: '.txt' },
|
|
33
|
+
// Add more MIME types and file extensions as needed
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export const fetchAllMetadataRecords = fromCallback<
|
|
37
|
+
EventObject,
|
|
38
|
+
typeof fetchAllFilesMachine
|
|
39
|
+
>(({ sendBack, receive, input: { context, event } }) => {
|
|
40
|
+
const { addresses } = context
|
|
41
|
+
|
|
42
|
+
const _fetchAllMetadataRecords = async () => {
|
|
43
|
+
const queryClient = BaseQueryClient.getQueryClient()
|
|
44
|
+
const easClient = BaseEasClient.getEasClient()
|
|
45
|
+
|
|
46
|
+
const { filesMetadata } = await queryClient.fetchQuery({
|
|
47
|
+
queryKey: ['getFilesMetadata', ...addresses],
|
|
48
|
+
queryFn: async () =>
|
|
49
|
+
easClient.request(GET_FILES_METADATA, {
|
|
50
|
+
where: {
|
|
51
|
+
attester: {
|
|
52
|
+
in: addresses,
|
|
53
|
+
},
|
|
54
|
+
schema: {
|
|
55
|
+
is: {
|
|
56
|
+
id: {
|
|
57
|
+
equals:
|
|
58
|
+
'0x55fdefb36fcbbaebeb7d6b41dc3a1a9666e4e42154267c889de064faa7ede517',
|
|
59
|
+
},
|
|
60
|
+
},
|
|
61
|
+
},
|
|
62
|
+
},
|
|
63
|
+
}),
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
return filesMetadata
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
_fetchAllMetadataRecords().then((filesMetadata) => {
|
|
70
|
+
sendBack({ type: 'fetchingAllMetadataRecordsSuccess', filesMetadata })
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
return () => { }
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
export const fetchAllBinaryData = fromCallback<
|
|
77
|
+
EventObject,
|
|
78
|
+
typeof fetchAllFilesMachine
|
|
79
|
+
>(({ sendBack, input: { context } }) => {
|
|
80
|
+
const { filesMetadata, addresses } = context
|
|
81
|
+
|
|
82
|
+
const _fetchAllBinaryData = async () => {
|
|
83
|
+
if (!(await fs.promises.exists('/files'))) {
|
|
84
|
+
await fs.promises.mkdir('/files', { recursive: true })
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
if (!(await fs.promises.exists('/files/html'))) {
|
|
88
|
+
await fs.promises.mkdir('/files/html', { recursive: true })
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if (!(await fs.promises.exists('/files/json'))) {
|
|
92
|
+
await fs.promises.mkdir('/files/json', { recursive: true })
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (!(await fs.promises.exists('/files/images'))) {
|
|
96
|
+
await fs.promises.mkdir('/files/images', { recursive: true })
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const appDb = BaseDb.getAppDb()
|
|
100
|
+
|
|
101
|
+
if (!appDb) {
|
|
102
|
+
logger('[fetchAll/actors] [fetchAllBinaryData] seedDb not available')
|
|
103
|
+
return []
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
for (const fileMetadata of filesMetadata) {
|
|
107
|
+
const json = JSON.parse(fileMetadata.decodedDataJson)
|
|
108
|
+
const transactionId = json[0].value.value
|
|
109
|
+
|
|
110
|
+
const excludedTransactionsQuery = await appDb
|
|
111
|
+
.select()
|
|
112
|
+
.from(appState)
|
|
113
|
+
.where(eq(appState.key, 'excludedTransactions'))
|
|
114
|
+
|
|
115
|
+
let excludedTransactions = new Set<string>()
|
|
116
|
+
|
|
117
|
+
if (excludedTransactionsQuery && excludedTransactionsQuery.length === 1) {
|
|
118
|
+
const valueString = excludedTransactionsQuery[0].value
|
|
119
|
+
if (valueString) {
|
|
120
|
+
const excludedTransactionsArray = JSON.parse(valueString)
|
|
121
|
+
excludedTransactions = new Set(excludedTransactionsArray)
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
if (excludedTransactions.has(transactionId)) {
|
|
126
|
+
continue
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const arweave = getArweave()
|
|
130
|
+
|
|
131
|
+
if (!arweave) {
|
|
132
|
+
logger('[fetchAll/actors] [fetchAllBinaryData] arweave not available')
|
|
133
|
+
return []
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
try {
|
|
137
|
+
const res = await fetch(
|
|
138
|
+
`https://${ARWEAVE_HOST}/tx/${transactionId}/status`,
|
|
139
|
+
)
|
|
140
|
+
|
|
141
|
+
if (res.status !== 200) {
|
|
142
|
+
logger(
|
|
143
|
+
`[fetchAll/actors] [fetchAllBinaryData] error fetching transaction data for ${transactionId}`,
|
|
144
|
+
)
|
|
145
|
+
|
|
146
|
+
excludedTransactions.add(transactionId)
|
|
147
|
+
|
|
148
|
+
await saveAppState(
|
|
149
|
+
'excludedTransactions',
|
|
150
|
+
JSON.stringify(Array.from(excludedTransactions)),
|
|
151
|
+
)
|
|
152
|
+
|
|
153
|
+
logger(
|
|
154
|
+
'[fetchAll/actors] [fetchAllBinaryData] updated excludedTransactions:',
|
|
155
|
+
excludedTransactions,
|
|
156
|
+
)
|
|
157
|
+
|
|
158
|
+
continue
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
const dataString = await arweave.transactions
|
|
162
|
+
.getData(transactionId, {
|
|
163
|
+
decode: true,
|
|
164
|
+
string: true,
|
|
165
|
+
})
|
|
166
|
+
.catch((error) => {
|
|
167
|
+
logger(
|
|
168
|
+
`[fetchAll/actors] [fetchAllBinaryData] error fetching transaction data for ${transactionId}`,
|
|
169
|
+
error,
|
|
170
|
+
)
|
|
171
|
+
})
|
|
172
|
+
|
|
173
|
+
const dataUint8Array = await arweave.transactions.getData(transactionId)
|
|
174
|
+
// let buffer
|
|
175
|
+
//
|
|
176
|
+
// if (dataUint8Array && dataUint8Array instanceof Uint8Array) {
|
|
177
|
+
// }
|
|
178
|
+
|
|
179
|
+
let contentType = identifyString(dataString)
|
|
180
|
+
if (
|
|
181
|
+
contentType !== 'json' &&
|
|
182
|
+
contentType !== 'base64' &&
|
|
183
|
+
contentType !== 'html'
|
|
184
|
+
) {
|
|
185
|
+
const possibleImageType = getDataTypeFromString(dataString)
|
|
186
|
+
if (!possibleImageType) {
|
|
187
|
+
logger(
|
|
188
|
+
`[fetchAll/actors] [fetchAllBinaryData] transaction ${transactionId} data not in expected format: ${possibleImageType}`,
|
|
189
|
+
)
|
|
190
|
+
continue
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
contentType = possibleImageType
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
if (contentType === 'url') {
|
|
197
|
+
const url = dataString as string
|
|
198
|
+
const response = await fetch(url)
|
|
199
|
+
if (!response.ok) {
|
|
200
|
+
throw new Error(`Failed to fetch image: ${response.statusText}`)
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
// Get the image as a Blob
|
|
204
|
+
const blob = await response.blob()
|
|
205
|
+
const buffer = await blob.arrayBuffer()
|
|
206
|
+
const bufferUint8Array = new Uint8Array(buffer)
|
|
207
|
+
|
|
208
|
+
// Extract the file extension from the URL
|
|
209
|
+
const extensionMatch = url.match(
|
|
210
|
+
/\.(jpg|jpeg|png|gif|bmp|webp|svg)$/i,
|
|
211
|
+
)
|
|
212
|
+
if (!extensionMatch) {
|
|
213
|
+
throw new Error(
|
|
214
|
+
'Unable to determine the file extension from the URL.',
|
|
215
|
+
)
|
|
216
|
+
}
|
|
217
|
+
const fileExtension = extensionMatch[0] // e.g., ".jpg"
|
|
218
|
+
|
|
219
|
+
// Set the file name (you can customize this)
|
|
220
|
+
// const fileNameFromUrl = `${transactionId}${fileExtension}`
|
|
221
|
+
|
|
222
|
+
await fs.promises.writeFile(
|
|
223
|
+
`/files/images/${transactionId}`,
|
|
224
|
+
bufferUint8Array,
|
|
225
|
+
{
|
|
226
|
+
encoding: 'binary',
|
|
227
|
+
},
|
|
228
|
+
)
|
|
229
|
+
|
|
230
|
+
continue
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
let mimeType = getMimeType(dataString as string)
|
|
234
|
+
let fileExtension = mimeType
|
|
235
|
+
|
|
236
|
+
if (fileExtension && fileExtension?.startsWith('image')) {
|
|
237
|
+
fileExtension = fileExtension.replace('image/', '')
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
let fileName = transactionId
|
|
241
|
+
|
|
242
|
+
if (contentType === 'base64') {
|
|
243
|
+
if (mimeType) {
|
|
244
|
+
fileName += `.${fileExtension}`
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
// Remove the Base64 header if it exists (e.g., "data:image/png;base64,")
|
|
248
|
+
const base64Data = dataString.split(',').pop() || ''
|
|
249
|
+
|
|
250
|
+
// Decode the Base64 string to binary
|
|
251
|
+
const binaryString = atob(base64Data)
|
|
252
|
+
const length = binaryString.length
|
|
253
|
+
const binaryData = new Uint8Array(length)
|
|
254
|
+
|
|
255
|
+
for (let i = 0; i < length; i++) {
|
|
256
|
+
binaryData[i] = binaryString.charCodeAt(i)
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
console.log(`attempting to writeFile to /files/images/${fileName}`)
|
|
260
|
+
|
|
261
|
+
await fs.promises.writeFile(`/files/images/${fileName}`, binaryData, {
|
|
262
|
+
encoding: 'binary',
|
|
263
|
+
})
|
|
264
|
+
|
|
265
|
+
// if (dataUint8Array && dataUint8Array instanceof Uint8Array) {
|
|
266
|
+
// await fs.promises.writeFile(
|
|
267
|
+
// `/files/images/${fileName}`,
|
|
268
|
+
// dataUint8Array,
|
|
269
|
+
// )
|
|
270
|
+
// }
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
if (contentType === 'html') {
|
|
274
|
+
fileName += '.html'
|
|
275
|
+
await fs.promises.writeFile(`/files/html/${fileName}`, dataString)
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
if (contentType === 'json') {
|
|
279
|
+
fileName += '.json'
|
|
280
|
+
await fs.promises.writeFile(`/files/json/${fileName}`, dataString)
|
|
281
|
+
}
|
|
282
|
+
} catch (error) {
|
|
283
|
+
logger(error)
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
return []
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
_fetchAllBinaryData().then((binaryData) => {
|
|
291
|
+
sendBack({ type: 'fetchingAllBinaryDataSuccess', binaryData })
|
|
292
|
+
})
|
|
293
|
+
|
|
294
|
+
return () => { }
|
|
295
|
+
})
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { EventObject, fromCallback } from 'xstate'
|
|
2
|
+
import { models as modelsTable, modelUids } from '@/seedSchema'
|
|
3
|
+
import { eq } from 'drizzle-orm'
|
|
4
|
+
import { toSnakeCase, BaseEasClient, BaseQueryClient } from '@/helpers'
|
|
5
|
+
import { BaseDb } from '@/db/Db/BaseDb'
|
|
6
|
+
import { GLOBAL_ADDING_MODELS_TO_DB_SUCCESS } from '@/services/internal/constants'
|
|
7
|
+
import { eventEmitter } from '@/eventBus'
|
|
8
|
+
import { FromCallbackInput, GlobalMachineContext } from '@/types'
|
|
9
|
+
import debug from 'debug'
|
|
10
|
+
import { GET_SCHEMAS } from '@/Item/queries'
|
|
11
|
+
|
|
12
|
+
const logger = debug('app:services:global:actors:addModelsToDb')
|
|
13
|
+
|
|
14
|
+
export const addModelsToDb = fromCallback<
|
|
15
|
+
EventObject,
|
|
16
|
+
FromCallbackInput<GlobalMachineContext>
|
|
17
|
+
>(({ sendBack, input: { context } }) => {
|
|
18
|
+
const { models } = context
|
|
19
|
+
|
|
20
|
+
const _addModelsToDb = async () => {
|
|
21
|
+
const appDb = BaseDb.getAppDb()
|
|
22
|
+
|
|
23
|
+
if (!models) {
|
|
24
|
+
return
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const { models: SeedModels } = await import(
|
|
28
|
+
'@/db/configs/seed.schema.config'
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
const allModels = {
|
|
32
|
+
...SeedModels,
|
|
33
|
+
...models,
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
let hasModelsInDb = false
|
|
37
|
+
const schemaDefsByModelName = new Map<
|
|
38
|
+
string,
|
|
39
|
+
{
|
|
40
|
+
dbId: number
|
|
41
|
+
schemaDef: string
|
|
42
|
+
}
|
|
43
|
+
>()
|
|
44
|
+
|
|
45
|
+
for (const [modelName, _] of Object.entries(allModels)) {
|
|
46
|
+
logger(
|
|
47
|
+
'[helpers/db] [addModelsToInternalDb] starting modelName:',
|
|
48
|
+
modelName,
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
let foundModel
|
|
52
|
+
|
|
53
|
+
const foundModelsQuery = await appDb
|
|
54
|
+
.select()
|
|
55
|
+
.from(modelsTable)
|
|
56
|
+
.where(eq(modelsTable.name, modelName))
|
|
57
|
+
|
|
58
|
+
if (!foundModelsQuery || foundModelsQuery.length === 0) {
|
|
59
|
+
await appDb.insert(modelsTable).values({
|
|
60
|
+
name: modelName,
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
logger('[global/actors] [addModelsToDb] inserted model:', modelName)
|
|
64
|
+
const foundModels = await appDb
|
|
65
|
+
.select({
|
|
66
|
+
id: modelsTable.id,
|
|
67
|
+
name: modelsTable.name,
|
|
68
|
+
uid: modelUids.uid,
|
|
69
|
+
})
|
|
70
|
+
.from(modelsTable)
|
|
71
|
+
.leftJoin(modelUids, eq(modelsTable.id, modelUids.modelId))
|
|
72
|
+
.where(eq(modelsTable.name, modelName))
|
|
73
|
+
.limit(1)
|
|
74
|
+
|
|
75
|
+
foundModel = foundModels[0]
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
if (foundModelsQuery && foundModelsQuery.length > 0) {
|
|
79
|
+
foundModel = foundModelsQuery[0]
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (!foundModel) {
|
|
83
|
+
hasModelsInDb = false
|
|
84
|
+
break
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
schemaDefsByModelName.set(modelName, {
|
|
88
|
+
dbId: foundModel.id,
|
|
89
|
+
schemaDef: `bytes32 ${toSnakeCase(modelName)}`,
|
|
90
|
+
})
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
if (!hasModelsInDb) {
|
|
94
|
+
return false
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const schemaDefs = Array.from(schemaDefsByModelName.values()).map(
|
|
98
|
+
({ schemaDef }) => schemaDef,
|
|
99
|
+
)
|
|
100
|
+
|
|
101
|
+
const queryClient = BaseQueryClient.getQueryClient()
|
|
102
|
+
const easClient = BaseEasClient.getEasClient()
|
|
103
|
+
|
|
104
|
+
const { schemas } = await queryClient.fetchQuery({
|
|
105
|
+
queryKey: [`getSchemasVersion`],
|
|
106
|
+
queryFn: async () =>
|
|
107
|
+
easClient.request(GET_SCHEMAS, {
|
|
108
|
+
where: {
|
|
109
|
+
schema: {
|
|
110
|
+
in: schemaDefs,
|
|
111
|
+
},
|
|
112
|
+
},
|
|
113
|
+
}),
|
|
114
|
+
})
|
|
115
|
+
|
|
116
|
+
if (!schemas || schemas.length === 0) {
|
|
117
|
+
throw new Error(`No schemas found`)
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
for (const schema of schemas) {
|
|
121
|
+
const modelId = Array.from(schemaDefsByModelName.values()).find(
|
|
122
|
+
({ schemaDef }) => schemaDef === schema.schema,
|
|
123
|
+
)?.dbId
|
|
124
|
+
|
|
125
|
+
if (!modelId) {
|
|
126
|
+
throw new Error(`No modelId found for schema ${schema.schema}`)
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
await appDb
|
|
130
|
+
.insert(modelUids)
|
|
131
|
+
.values({
|
|
132
|
+
modelId,
|
|
133
|
+
uid: schema.id,
|
|
134
|
+
})
|
|
135
|
+
.onConflictDoNothing()
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
_addModelsToDb().then((hasModelsInDb) => {
|
|
140
|
+
sendBack({ type: GLOBAL_ADDING_MODELS_TO_DB_SUCCESS })
|
|
141
|
+
if (hasModelsInDb) {
|
|
142
|
+
}
|
|
143
|
+
for (const [modelName, model] of Object.entries(models)) {
|
|
144
|
+
const service = context[`${modelName}Service`]
|
|
145
|
+
service.send({ type: 'modelsFound' })
|
|
146
|
+
}
|
|
147
|
+
eventEmitter.emit('syncDbWithEas')
|
|
148
|
+
return
|
|
149
|
+
})
|
|
150
|
+
|
|
151
|
+
return () => { }
|
|
152
|
+
})
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { getGlobalService } from '@/services/global'
|
|
2
|
+
|
|
3
|
+
import { saveAppState } from '@/db/write/saveAppState'
|
|
4
|
+
|
|
5
|
+
type SaveServiceEvent = {
|
|
6
|
+
modelName: string
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export const saveServiceHandler = async (event: SaveServiceEvent) => {
|
|
10
|
+
const globalService = getGlobalService()
|
|
11
|
+
|
|
12
|
+
if (!globalService || !globalService.getSnapshot().context) {
|
|
13
|
+
return
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const { modelName } = event
|
|
17
|
+
|
|
18
|
+
const nameOfService: string = `${modelName}Service`
|
|
19
|
+
|
|
20
|
+
const service = globalService.getSnapshot().context[nameOfService]
|
|
21
|
+
|
|
22
|
+
if (!service) {
|
|
23
|
+
console.log(`[saveServiceHandler] service not found: ${nameOfService}`)
|
|
24
|
+
return
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
await saveAppState(
|
|
28
|
+
`snapshot__${modelName}`,
|
|
29
|
+
JSON.stringify(service.getPersistedSnapshot()),
|
|
30
|
+
)
|
|
31
|
+
}
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import { EventObject, fromCallback } from 'xstate'
|
|
2
|
+
import { FromCallbackInput } from '@/types/machines'
|
|
3
|
+
import {
|
|
4
|
+
ItemPropertyValueType,
|
|
5
|
+
PropertyMachineContext,
|
|
6
|
+
SaveValueToDbEvent,
|
|
7
|
+
} from '@/types/property'
|
|
8
|
+
import { updateItemPropertyValue } from '@/db/write/updateItemPropertyValue'
|
|
9
|
+
|
|
10
|
+
import { getSchemaForItemProperty } from '@/helpers/getSchemaForItemProperty'
|
|
11
|
+
import { INTERNAL_DATA_TYPES } from '@/helpers/constants'
|
|
12
|
+
import { TypedData } from '@ethereum-attestation-service/eas-sdk/dist/offchain/typed-data-handler'
|
|
13
|
+
|
|
14
|
+
export const analyzeInput = fromCallback<
|
|
15
|
+
EventObject,
|
|
16
|
+
FromCallbackInput<PropertyMachineContext, SaveValueToDbEvent>
|
|
17
|
+
>(({ sendBack, input: { context, event } }) => {
|
|
18
|
+
const {
|
|
19
|
+
localId,
|
|
20
|
+
propertyName: propertyNameRaw,
|
|
21
|
+
seedLocalId,
|
|
22
|
+
versionLocalId,
|
|
23
|
+
versionUid,
|
|
24
|
+
propertyValue: existingValue,
|
|
25
|
+
propertyRecordSchema,
|
|
26
|
+
modelName,
|
|
27
|
+
} = context
|
|
28
|
+
|
|
29
|
+
let { schemaUid } = context
|
|
30
|
+
|
|
31
|
+
let newValue: ItemPropertyValueType
|
|
32
|
+
|
|
33
|
+
if (event) {
|
|
34
|
+
newValue = event.newValue
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
if (existingValue === newValue) {
|
|
38
|
+
sendBack({ type: 'saveValueToDbSuccess' })
|
|
39
|
+
return
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (!propertyRecordSchema) {
|
|
43
|
+
throw new Error('Missing propertyRecordSchema')
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const _analyzeInput = async (): Promise<boolean> => {
|
|
47
|
+
let propertyName = propertyNameRaw
|
|
48
|
+
|
|
49
|
+
if (
|
|
50
|
+
propertyRecordSchema.refValueType &&
|
|
51
|
+
propertyRecordSchema.refValueType !== 'ImageSrc' &&
|
|
52
|
+
propertyRecordSchema.dataType === 'Relation'
|
|
53
|
+
) {
|
|
54
|
+
sendBack({
|
|
55
|
+
type: 'saveRelation',
|
|
56
|
+
newValue,
|
|
57
|
+
})
|
|
58
|
+
return false
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (
|
|
62
|
+
propertyRecordSchema.refValueType === 'ImageSrc' ||
|
|
63
|
+
propertyRecordSchema.dataType === 'ImageSrc'
|
|
64
|
+
) {
|
|
65
|
+
sendBack({
|
|
66
|
+
type: 'saveImageSrc',
|
|
67
|
+
newValue,
|
|
68
|
+
})
|
|
69
|
+
return false
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (
|
|
73
|
+
propertyRecordSchema.storageType &&
|
|
74
|
+
propertyRecordSchema.storageType === 'ItemStorage'
|
|
75
|
+
) {
|
|
76
|
+
sendBack({
|
|
77
|
+
type: 'saveItemStorage',
|
|
78
|
+
newValue,
|
|
79
|
+
})
|
|
80
|
+
return false
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
if (!schemaUid) {
|
|
84
|
+
let easDataType
|
|
85
|
+
|
|
86
|
+
if (propertyRecordSchema.dataType) {
|
|
87
|
+
easDataType = INTERNAL_DATA_TYPES[propertyRecordSchema.dataType]
|
|
88
|
+
.eas as TypedData['type']
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const schemaFromEas = await getSchemaForItemProperty({
|
|
92
|
+
propertyName,
|
|
93
|
+
easDataType,
|
|
94
|
+
})
|
|
95
|
+
if (schemaFromEas) {
|
|
96
|
+
schemaUid = schemaFromEas.id
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const result = await updateItemPropertyValue({
|
|
101
|
+
localId,
|
|
102
|
+
propertyName,
|
|
103
|
+
newValue,
|
|
104
|
+
seedLocalId,
|
|
105
|
+
versionLocalId,
|
|
106
|
+
versionUid,
|
|
107
|
+
modelName,
|
|
108
|
+
schemaUid,
|
|
109
|
+
})
|
|
110
|
+
|
|
111
|
+
let updatedContext: Partial<PropertyMachineContext> = {
|
|
112
|
+
propertyValue: newValue,
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
if (localId) {
|
|
116
|
+
updatedContext.localId = localId
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
if (schemaUid) {
|
|
120
|
+
updatedContext.schemaUid = schemaUid
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
if (!localId && result?.localId) {
|
|
124
|
+
updatedContext.localId = result.localId
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
if (!schemaUid && result?.schemaUid) {
|
|
128
|
+
updatedContext.schemaUid = result.schemaUid
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
sendBack({
|
|
132
|
+
type: 'updateContext',
|
|
133
|
+
...updatedContext,
|
|
134
|
+
})
|
|
135
|
+
|
|
136
|
+
return true
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
_analyzeInput().then((isDone) => {
|
|
140
|
+
if (isDone) {
|
|
141
|
+
sendBack({ type: 'saveValueToDbSuccess' })
|
|
142
|
+
}
|
|
143
|
+
})
|
|
144
|
+
})
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { defineConfig } from 'drizzle-kit'
|
|
2
|
+
import dotenv from 'dotenv'
|
|
3
|
+
import process from 'node:process'
|
|
4
|
+
import path from 'path'
|
|
5
|
+
|
|
6
|
+
dotenv.config()
|
|
7
|
+
|
|
8
|
+
let dotSeedDir = path.join(process.cwd(), '.seed')
|
|
9
|
+
|
|
10
|
+
if (process.env.IS_SEED_DEV) {
|
|
11
|
+
dotSeedDir = path.join(
|
|
12
|
+
process.cwd(),
|
|
13
|
+
'__tests__',
|
|
14
|
+
'__mocks__',
|
|
15
|
+
'project',
|
|
16
|
+
'.seed',
|
|
17
|
+
)
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export default defineConfig({
|
|
21
|
+
schema: [`${dotSeedDir}/app/schema/*Schema.ts`],
|
|
22
|
+
dialect: 'sqlite',
|
|
23
|
+
out: `${dotSeedDir}/app/db`,
|
|
24
|
+
dbCredentials: {
|
|
25
|
+
url: `${dotSeedDir}/app/db/app_db.sqlite3`,
|
|
26
|
+
},
|
|
27
|
+
})
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { Endpoints } from './index'
|
|
2
|
+
import { SqliteRemoteResult } from 'drizzle-orm/sqlite-proxy'
|
|
3
|
+
|
|
4
|
+
export type SeedInitBrowserProps = {
|
|
5
|
+
endpoints: Endpoints
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export interface SeedInitBrowser {
|
|
9
|
+
(props: SeedInitBrowserProps): Promise<void>
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export type DbQueryResult = SqliteRemoteResult<SqliteWasmResult>
|
|
13
|
+
|
|
14
|
+
export type ResultObject = {
|
|
15
|
+
[key: string]: string
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export type SqliteWasmResult = {
|
|
19
|
+
type: string | null
|
|
20
|
+
row: string[] | null
|
|
21
|
+
rowNumber: number | null
|
|
22
|
+
columnNames: string[]
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export type SqliteWasmCallback = (result: SqliteWasmResult) => void
|
|
26
|
+
|
|
27
|
+
export type ReturnObj = {
|
|
28
|
+
database: string
|
|
29
|
+
[key: string]: string | number | null | undefined | string[]
|
|
30
|
+
}
|