Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add arfs-js to handle read write ops #139

Open
wants to merge 3 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"@uiw/react-codemirror": "^4.21.11",
"@uiw/react-md-editor": "^3.23.5",
"ardb": "^1.1.10",
"arfs-js": "^1.4.2",
"arweave": "^1.14.4",
"clsx": "^2.0.0",
"date-fns": "^2.30.0",
Expand All @@ -38,7 +39,7 @@
"framer-motion": "^10.15.0",
"headless-stepper": "^1.9.1",
"immer": "^10.0.2",
"isomorphic-git": "^1.24.5",
"@protocol.land/isomorphic-git": "^1.1.0",
"js-yaml": "^4.1.0",
"jszip": "^3.10.1",
"lang-exts-map": "^0.4.0",
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const AOS_PROCESS_ID = 'yJZ3_Yrc-qYRt1zHmY7YeNvpmQwuqyK3dT0-gxWftew'
export const AOS_PROCESS_ID = 'gJ3c9ivxv8iwSXJQRI3ZINkq2DYqIIjhwINTNcKRxT0'
export const VITE_GA_TRACKING_ID = 'G-L433HSR0D0'
export const AMPLITUDE_TRACKING_ID = '92a463755ed8c8b96f0f2353a37b7b2'
export const PL_REPO_ID = '6ace6247-d267-463d-b5bd-7e50d98c3693'
Expand Down
12 changes: 10 additions & 2 deletions src/helpers/getArrayBufSize.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
export function getArrayBufSize(arrayBuffer: ArrayBuffer): GetArrayBufSizeReturnType {
const byteSize = arrayBuffer.byteLength
export function getRepoSize(arrayBufferOrSize: ArrayBuffer | number): GetArrayBufSizeReturnType {
let byteSize = 0

if (arrayBufferOrSize instanceof ArrayBuffer) {
byteSize = arrayBufferOrSize.byteLength
}

if (typeof arrayBufferOrSize === 'number') {
byteSize = arrayBufferOrSize
}

if (byteSize >= 1073741824) {
return {
Expand Down
37 changes: 37 additions & 0 deletions src/lib/arfs/arfsSingleton.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { ArFS, BiFrost, Drive } from 'arfs-js'

export class ArFSSingleton {
driveInstance: Drive | null = null
bifrostInstance: BiFrost | null = null
arfsInstance: ArFS | null = null

constructor() {}

getInstance() {
return this
}

getBifrostInstance() {
return this.bifrostInstance
}

getArfsInstance() {
return this.arfsInstance
}

getDriveInstance() {
return this.driveInstance
}

setDrive(drive: Drive) {
this.driveInstance = drive
}

setBifrost(bifrost: BiFrost) {
this.bifrostInstance = bifrost
}

setArFS(arfs: ArFS) {
this.arfsInstance = arfs
}
}
38 changes: 38 additions & 0 deletions src/lib/arfs/arfsSingletonMap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { ArFSSingleton } from './arfsSingleton'

let instance: ArFSSingletonMap
const map: Map<string, ArFSSingleton> = new Map()

export class ArFSSingletonMap {
constructor() {
if (instance) {
throw new Error('You can only create one instance!')
}
// eslint-disable-next-line @typescript-eslint/no-this-alias
instance = this
}

getInstance() {
return this
}

getArFSSingleton(key: string) {
if (!map.has(key)) {
throw new Error('Singleton Instance not found.')
}

return map.get(key)
}

setArFSSingleton(key: string, arfsSingleton: ArFSSingleton) {
map.set(key, arfsSingleton)
}

getAllArFSSingletons() {
return map
}
}

const arfsSingletonMap = Object.freeze(new ArFSSingletonMap())

export default arfsSingletonMap
24 changes: 24 additions & 0 deletions src/lib/arfs/arfsTxSubmissionOverride.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import Transaction from 'arweave/web/lib/transaction'
import { v4 as uuidv4 } from 'uuid'

import { createSignedQueuePayload } from '../queue/helpers'
import taskQueueSingleton from '../queue/TaskQueue'

export async function arfsTxSubmissionOverride(txList: Transaction[]) {
const queueStatus = taskQueueSingleton.getTaskQueueStatus()
const txIds: string[] = []

if (queueStatus === 'Busy') throw new Error('Task Queue is busy. Try again later.')

for (const tx of txList) {
const dataItem = await createSignedQueuePayload(tx)

const token = uuidv4()
taskQueueSingleton.sendToPending(token, dataItem)

const txid = await dataItem.id
txIds.push(txid)
}

return { successTxIds: txIds, failedTxIndex: [] }
}
10 changes: 10 additions & 0 deletions src/lib/arfs/getArFS.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { ArFS } from 'arfs-js'

import { arfsTxSubmissionOverride } from './arfsTxSubmissionOverride'

export function getArFS() {
const arfs = new ArFS({ wallet: 'use_wallet', appName: 'Protocol.Land' })
arfs.api.signAndSendAllTransactions = arfsTxSubmissionOverride

return arfs
}
7 changes: 7 additions & 0 deletions src/lib/arfs/getBifrost.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { ArFS, BiFrost, Drive } from 'arfs-js'

export function getBifrost(drive: Drive, arfs: ArFS) {
const bifrost = new BiFrost(drive, arfs)

return bifrost
}
2 changes: 1 addition & 1 deletion src/lib/dragondeploy/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import ArDB from 'ardb'
import Arweave from 'arweave'
import { Tag } from 'arweave/web/lib/transaction'
import git, { WORKDIR } from 'isomorphic-git'
import git, { WORKDIR } from '@protocol.land/isomorphic-git'
import mime from 'mime'
import type { Dispatch, SetStateAction } from 'react'

Expand Down
5 changes: 3 additions & 2 deletions src/lib/git/branch.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import git from 'isomorphic-git'
import git from '@protocol.land/isomorphic-git'

import { withAsync } from '@/helpers/withAsync'

Expand Down Expand Up @@ -47,7 +47,8 @@ export async function checkoutBranch({ fs, dir, name }: CommonBranchOptions & {
dir,
ref: name,
force: true,
track: false
track: false,
noUpdateHead: true
})
)
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/git/commit.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import git from 'isomorphic-git'
import git from '@protocol.land/isomorphic-git'
import { FileWithPath } from 'react-dropzone'

import { toArrayBuffer } from '@/helpers/toArrayBuffer'
Expand Down
14 changes: 11 additions & 3 deletions src/lib/git/helpers/fsWithName.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import LightningFS from '@isomorphic-git/lightning-fs'
import arfsSingletonMap from '@/lib/arfs/arfsSingletonMap'

export function fsWithName(name: string) {
return new LightningFS(name)
const arfsSingleton = arfsSingletonMap.getArFSSingleton(name)

if (!arfsSingleton) throw new Error('ArFS uninitialized.')

const bifrost = arfsSingleton.getBifrostInstance()

if (!bifrost) throw new Error('Bifrost uninitialized.')

return bifrost.fs
}

export type FSType = ReturnType<typeof fsWithName>
export type FSType = ReturnType<typeof fsWithName>
2 changes: 1 addition & 1 deletion src/lib/git/helpers/oid.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import git, { TreeEntry } from 'isomorphic-git'
import git, { TreeEntry } from '@protocol.land/isomorphic-git'

import { FSType } from './fsWithName'

Expand Down
36 changes: 36 additions & 0 deletions src/lib/git/helpers/zipUtils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import JSZip from 'jszip'

import { waitFor } from '@/helpers/waitFor'
import { withAsync } from '@/helpers/withAsync'

import { FSType } from './fsWithName'

Expand Down Expand Up @@ -33,6 +34,41 @@ export async function unpackGitRepo({ fs, blob }: UnpackGitRepoOptions) {
return true
}

export async function copyFilesToTargetRepo(
sourceDirPath: string,
sourceFS: FSType,
targetFS: FSType,
targetDir: string
) {
// ensure targetFS is initialized with dir
const { error } = await withAsync(() => targetFS.promises.readdir(targetDir))
if (error) {
await targetFS.promises.mkdir(targetDir)
}

const dirItems = await sourceFS.promises.readdir(sourceDirPath)

dirItems.forEach(async (item) => {
const srcPath = `${sourceDirPath}/${item}`
const destPath = `${targetDir}/${item}`

const stats = await sourceFS.promises.stat(srcPath)

if (stats.isDirectory()) {
try {
await targetFS.promises.mkdir(destPath)
} catch (error) {
// ignore
}

await copyFilesToTargetRepo(srcPath, sourceFS, targetFS, destPath)
} else {
const fileContent = await sourceFS.promises.readFile(srcPath)
await targetFS.promises.writeFile(destPath, fileContent)
}
})
}

async function addFilesToZip(zip: JSZip, path: string, fs: FSType) {
const dirItems = await fs.promises.readdir(path)

Expand Down
2 changes: 1 addition & 1 deletion src/lib/git/pull-request.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import git, { Errors } from 'isomorphic-git'
import git, { Errors } from '@protocol.land/isomorphic-git'

import { getTags } from '@/helpers/getTags'
import { trackGoogleAnalyticsEvent } from '@/helpers/google-analytics'
Expand Down
44 changes: 8 additions & 36 deletions src/lib/git/repo.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import git from '@protocol.land/isomorphic-git'
import Arweave from 'arweave'
import { Tag } from 'arweave/web/lib/transaction'
import Dexie from 'dexie'
import git from 'isomorphic-git'
import { v4 as uuidv4 } from 'uuid'

import { getTags } from '@/helpers/getTags'
import { toArrayBuffer } from '@/helpers/toArrayBuffer'
Expand Down Expand Up @@ -33,43 +32,20 @@ const arweave = new Arweave({
protocol: 'https'
})

export async function postNewRepo({ id, title, description, file, owner, visibility }: any) {
const userSigner = await getSigner()

const data = (await toArrayBuffer(file)) as ArrayBuffer

const inputTags = [
{ name: 'App-Name', value: 'Protocol.Land' },
{ name: 'Content-Type', value: file.type },
{ name: 'Creator', value: owner },
{ name: 'Title', value: title },
{ name: 'Description', value: description },
{ name: 'Repo-Id', value: id },
{ name: 'Type', value: 'repo-create' },
{ name: 'Visibility', value: visibility }
] as Tag[]

await waitFor(500)

const dataTxResponse = await signAndSendTx(data, inputTags, userSigner, true)

if (!dataTxResponse) {
throw new Error('Failed to post Git repository')
}

export async function postNewRepo({ id, dataTxId, title, description }: any) {
await sendMessage({
tags: getTags({
Action: 'Initialize-Repo',
Id: id,
Name: title,
Description: description,
'Data-TxId': dataTxResponse,
Visibility: visibility,
'Data-TxId': dataTxId,
Visibility: 'public',
'Private-State-TxId': ''
})
})

return { txResponse: dataTxResponse }
return { txResponse: dataTxId }
}

export async function updateGithubSync({ id, currentGithubSync, githubSync }: any) {
Expand Down Expand Up @@ -147,20 +123,18 @@ export async function updateGithubSync({ id, currentGithubSync, githubSync }: an
}

export async function createNewFork(data: ForkRepositoryOptions) {
const uuid = uuidv4()

await sendMessage({
tags: getTags({
Action: 'Fork-Repo',
Id: uuid,
Id: data.id,
Name: data.name,
Description: data.description,
'Data-TxId': data.dataTxId,
Parent: data.parent
})
})

return uuid
return data.id
}

export async function postUpdatedRepo({ fs, dir, owner, id }: PostUpdatedRepoOptions) {
Expand Down Expand Up @@ -325,9 +299,7 @@ export async function createNewRepo(title: string, fs: FSType, owner: string, id

await waitFor(1000)

const repoBlob = await packGitRepo({ fs, dir })

return { repoBlob, commit: sha }
return { commit: sha }
} catch (error) {
console.error('failed to create repo')
}
Expand Down
Loading