@seedprotocol/sdk 0.1.49 → 0.1.50

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.
@@ -1,280 +1,10 @@
1
1
  import { EventObject, fromCallback } from 'xstate'
2
- import { fetchAllFilesMachine } from '@/browser/schema/file/fetchAll/index'
3
- import { ARWEAVE_HOST } from '@/browser/services/internal/constants'
4
- import { GET_FILES_METADATA } from '@/browser/schema/file/queries'
5
- import { getArweave } from '../arweave'
6
- import { fs } from '@zenfs/core'
7
- import { getImageDataType, getMimeType, identifyString } from '@/shared/helpers'
8
- import { appState } from 'src/shared/seedSchema'
9
- import { eq } from 'drizzle-orm'
10
- import { easClient, queryClient } from '@/browser/helpers'
11
- import debug from 'debug'
12
- import { getAppDb } from '@/browser/db/sqlWasmClient'
13
- import { writeAppState } from '@/browser/db/write'
2
+ import { uploadMachine } from '@/browser/schema/file/upload/index'
14
3
 
15
- const logger = debug('app:file:actors:fetchAll')
4
+ export const uploadBinaryData = fromCallback<EventObject, typeof uploadMachine>(
5
+ ({ sendBack, receive, input }) => {},
6
+ )
16
7
 
17
- type FileType = {
18
- mimeType: string
19
- extension: string
20
- }
21
-
22
- // Map of common MIME types to file extensions
23
- const fileTypeMap: Record<string, FileType> = {
24
- 'image/jpeg': { mimeType: 'image/jpeg', extension: '.jpg' },
25
- 'image/png': { mimeType: 'image/png', extension: '.png' },
26
- 'application/json': { mimeType: 'application/json', extension: '.json' },
27
- 'text/plain': { mimeType: 'text/plain', extension: '.txt' },
28
- // Add more MIME types and file extensions as needed
29
- }
30
-
31
- export const fetchAllMetadataRecords = fromCallback<
32
- EventObject,
33
- typeof fetchAllFilesMachine
34
- >(({ sendBack, receive, input: { context, event } }) => {
35
- const { addresses } = context
36
-
37
- const _fetchAllMetadataRecords = async () => {
38
- const { filesMetadata } = await queryClient.fetchQuery({
39
- queryKey: ['getFilesMetadata', ...addresses],
40
- queryFn: async () =>
41
- easClient.request(GET_FILES_METADATA, {
42
- where: {
43
- attester: {
44
- in: addresses,
45
- },
46
- schema: {
47
- is: {
48
- id: {
49
- equals:
50
- '0x55fdefb36fcbbaebeb7d6b41dc3a1a9666e4e42154267c889de064faa7ede517',
51
- },
52
- },
53
- },
54
- },
55
- }),
56
- })
57
-
58
- return filesMetadata
59
- }
60
-
61
- _fetchAllMetadataRecords().then((filesMetadata) => {
62
- sendBack({ type: 'fetchingAllMetadataRecordsSuccess', filesMetadata })
63
- })
64
-
65
- return () => {}
66
- })
67
-
68
- export const fetchAllBinaryData = fromCallback<
69
- EventObject,
70
- typeof fetchAllFilesMachine
71
- >(({ sendBack, input: { context } }) => {
72
- const { filesMetadata, addresses } = context
73
-
74
- const _fetchAllBinaryData = async () => {
75
- if (!(await fs.promises.exists('/files'))) {
76
- await fs.promises.mkdir('/files', { recursive: true })
77
- }
78
-
79
- if (!(await fs.promises.exists('/files/html'))) {
80
- await fs.promises.mkdir('/files/html', { recursive: true })
81
- }
82
-
83
- if (!(await fs.promises.exists('/files/json'))) {
84
- await fs.promises.mkdir('/files/json', { recursive: true })
85
- }
86
-
87
- if (!(await fs.promises.exists('/files/images'))) {
88
- await fs.promises.mkdir('/files/images', { recursive: true })
89
- }
90
-
91
- const appDb = getAppDb()
92
-
93
- if (!appDb) {
94
- logger('[fetchAll/actors] [fetchAllBinaryData] seedDb not available')
95
- return []
96
- }
97
-
98
- for (const fileMetadata of filesMetadata) {
99
- const json = JSON.parse(fileMetadata.decodedDataJson)
100
- const transactionId = json[0].value.value
101
-
102
- const excludedTransactionsQuery = await appDb
103
- .select()
104
- .from(appState)
105
- .where(eq(appState.key, 'excludedTransactions'))
106
-
107
- let excludedTransactions = new Set<string>()
108
-
109
- if (excludedTransactionsQuery && excludedTransactionsQuery.length === 1) {
110
- const valueString = excludedTransactionsQuery[0].value
111
- if (valueString) {
112
- const excludedTransactionsArray = JSON.parse(valueString)
113
- excludedTransactions = new Set(excludedTransactionsArray)
114
- }
115
- }
116
-
117
- if (excludedTransactions.has(transactionId)) {
118
- continue
119
- }
120
-
121
- const arweave = getArweave()
122
-
123
- if (!arweave) {
124
- logger('[fetchAll/actors] [fetchAllBinaryData] arweave not available')
125
- return []
126
- }
127
-
128
- try {
129
- const res = await fetch(
130
- `https://${ARWEAVE_HOST}/tx/${transactionId}/status`,
131
- )
132
-
133
- if (res.status !== 200) {
134
- logger(
135
- `[fetchAll/actors] [fetchAllBinaryData] error fetching transaction data for ${transactionId}`,
136
- )
137
-
138
- excludedTransactions.add(transactionId)
139
-
140
- await writeAppState(
141
- 'excludedTransactions',
142
- JSON.stringify(Array.from(excludedTransactions)),
143
- )
144
-
145
- logger(
146
- '[fetchAll/actors] [fetchAllBinaryData] updated excludedTransactions:',
147
- excludedTransactions,
148
- )
149
-
150
- continue
151
- }
152
-
153
- const dataString = await arweave.transactions
154
- .getData(transactionId, {
155
- decode: true,
156
- string: true,
157
- })
158
- .catch((error) => {
159
- logger(
160
- `[fetchAll/actors] [fetchAllBinaryData] error fetching transaction data for ${transactionId}`,
161
- error,
162
- )
163
- })
164
-
165
- const dataUint8Array = await arweave.transactions.getData(transactionId)
166
- // let buffer
167
- //
168
- // if (dataUint8Array && dataUint8Array instanceof Uint8Array) {
169
- // }
170
-
171
- let contentType = identifyString(dataString)
172
- if (
173
- contentType !== 'json' &&
174
- contentType !== 'base64' &&
175
- contentType !== 'html'
176
- ) {
177
- const possibleImageType = getImageDataType(dataString)
178
- if (!possibleImageType) {
179
- logger(
180
- `[fetchAll/actors] [fetchAllBinaryData] transaction ${transactionId} data not in expected format: ${possibleImageType}`,
181
- )
182
- continue
183
- }
184
-
185
- contentType = possibleImageType
186
- }
187
-
188
- if (contentType === 'url') {
189
- const url = dataString as string
190
- const response = await fetch(url)
191
- if (!response.ok) {
192
- throw new Error(`Failed to fetch image: ${response.statusText}`)
193
- }
194
-
195
- // Get the image as a Blob
196
- const blob = await response.blob()
197
- const buffer = await blob.arrayBuffer()
198
- const bufferUint8Array = new Uint8Array(buffer)
199
-
200
- // Extract the file extension from the URL
201
- const extensionMatch = url.match(
202
- /\.(jpg|jpeg|png|gif|bmp|webp|svg)$/i,
203
- )
204
- if (!extensionMatch) {
205
- throw new Error(
206
- 'Unable to determine the file extension from the URL.',
207
- )
208
- }
209
- const fileExtension = extensionMatch[0] // e.g., ".jpg"
210
-
211
- // Set the file name (you can customize this)
212
- // const fileNameFromUrl = `${transactionId}${fileExtension}`
213
-
214
- await fs.promises.writeFile(
215
- `/files/images/${transactionId}`,
216
- bufferUint8Array,
217
- {
218
- encoding: 'binary',
219
- },
220
- )
221
-
222
- continue
223
- }
224
-
225
- const mimeType = getMimeType(dataString as string)
226
-
227
- let fileName = transactionId
228
-
229
- if (contentType === 'base64') {
230
- if (mimeType) {
231
- fileName += `.${mimeType}`
232
- }
233
-
234
- // Remove the Base64 header if it exists (e.g., "data:image/png;base64,")
235
- const base64Data = dataString.split(',').pop() || ''
236
-
237
- // Decode the Base64 string to binary
238
- const binaryString = atob(base64Data)
239
- const length = binaryString.length
240
- const binaryData = new Uint8Array(length)
241
-
242
- for (let i = 0; i < length; i++) {
243
- binaryData[i] = binaryString.charCodeAt(i)
244
- }
245
-
246
- await fs.promises.writeFile(`/files/images/${fileName}`, binaryData, {
247
- encoding: 'binary',
248
- })
249
-
250
- // if (dataUint8Array && dataUint8Array instanceof Uint8Array) {
251
- // await fs.promises.writeFile(
252
- // `/files/images/${fileName}`,
253
- // dataUint8Array,
254
- // )
255
- // }
256
- }
257
-
258
- if (contentType === 'html') {
259
- fileName += '.html'
260
- await fs.promises.writeFile(`/files/html/${fileName}`, dataString)
261
- }
262
-
263
- if (contentType === 'json') {
264
- fileName += '.json'
265
- await fs.promises.writeFile(`/files/json/${fileName}`, dataString)
266
- }
267
- } catch (error) {
268
- logger(error)
269
- }
270
- }
271
-
272
- return []
273
- }
274
-
275
- _fetchAllBinaryData().then((binaryData) => {
276
- sendBack({ type: 'fetchingAllBinaryDataSuccess', binaryData })
277
- })
278
-
279
- return () => {}
280
- })
8
+ export const uploadMetadata = fromCallback<EventObject, typeof uploadMachine>(
9
+ ({ sendBack, receive, input }) => {},
10
+ )
@@ -1,5 +1,5 @@
1
1
  export * from './wasm.d.ts'
2
2
 
3
3
  declare module '@seedprotocol/sdk' {
4
- export * from '@/'
4
+ export * from '@/types'
5
5
  }
package/dist/src/index.ts CHANGED
@@ -1,73 +1,21 @@
1
- import { assign, setup } from 'xstate'
1
+ import { setup } from 'xstate'
2
2
  import {
3
- fetchAllBinaryData,
4
- fetchAllMetadataRecords,
5
- } from '@/browser/schema/file/fetchAll/actors'
6
- import { Attestation } from '@/browser/gql/graphql'
3
+ uploadBinaryData,
4
+ uploadMetadata,
5
+ } from '@/browser/schema/file/upload/actors'
7
6
 
8
- type FetchAllFilesMachineContext = {
9
- addresses: string[]
10
- dbsLoaded: boolean
11
- filesMetadata?: Attestation[]
12
- filesBlobData?: any[]
13
- }
14
-
15
- export const fetchAllFilesMachine = setup({
16
- types: {
17
- context: {} as FetchAllFilesMachineContext,
18
- },
7
+ export const uploadMachine = setup({
19
8
  actors: {
20
- fetchAllMetadataRecords,
21
- fetchAllBinaryData,
9
+ uploadBinaryData,
10
+ uploadMetadata,
22
11
  },
23
12
  }).createMachine({
24
- id: 'fetchAllFiles',
13
+ id: 'upload',
25
14
  initial: 'idle',
26
- context: ({ input }) =>
27
- ({
28
- ...input,
29
- dbsLoaded: false,
30
- }) as FetchAllFilesMachineContext,
31
- on: {
32
- allDbsLoaded: {
33
- target: '.fetchingAllMetadataRecords',
34
- actions: assign({
35
- dbsLoaded: true,
36
- }),
37
- },
15
+ context: {
16
+ file: '',
38
17
  },
39
18
  states: {
40
19
  idle: {},
41
- fetchingAllMetadataRecords: {
42
- on: {
43
- fetchingAllMetadataRecordsSuccess: {
44
- target: 'fetchingAllBinaryData',
45
- actions: assign({
46
- filesMetadata: ({ event }) => event.filesMetadata,
47
- }),
48
- },
49
- },
50
- invoke: {
51
- src: 'fetchAllMetadataRecords',
52
- input: ({ context, event }) => ({ context, event }),
53
- },
54
- },
55
- fetchingAllBinaryData: {
56
- on: {
57
- fetchingAllBinaryDataSuccess: {
58
- target: 'success',
59
- actions: assign({
60
- filesBlobData: ({ event }) => event.filesBlobData,
61
- }),
62
- },
63
- },
64
- invoke: {
65
- src: 'fetchAllBinaryData',
66
- input: ({ context }) => ({ context }),
67
- },
68
- },
69
- success: {
70
- type: 'final',
71
- },
72
20
  },
73
21
  })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@seedprotocol/sdk",
3
- "version": "0.1.49",
3
+ "version": "0.1.50",
4
4
  "description": "The SDK for Seed Protocol",
5
5
  "type": "module",
6
6
  "types": "dist/types/src/index.d.ts",