Skip to content

Commit

Permalink
Add unixfs to components.
Browse files Browse the repository at this point in the history
  • Loading branch information
saul-jb committed Apr 3, 2024
1 parent 93a221d commit 61d4a38
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 20 deletions.
6 changes: 5 additions & 1 deletion packages/daemon/src/common/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Path from 'path'
import { bitswap } from '@helia/block-brokers'
import { unixfs as createUnixfs } from '@helia/unixfs'
import HeliaPinManager from '@organicdesign/db-helia-pin-manager'
import { createKeyManager, type KeyManager } from '@organicdesign/db-key-manager'
import { ManualBlockBroker } from '@organicdesign/db-manual-block-broker'
Expand Down Expand Up @@ -73,6 +74,8 @@ export default async (settings: Partial<Settings> = {}): Promise<Components> =>
blockBrokers: [bitswap(), () => manualBlockBroker]
})

const unixfs = createUnixfs(helia)

const welo = await createWelo({
// @ts-expect-error Helia version mismatch here.
ipfs: helia,
Expand Down Expand Up @@ -163,7 +166,8 @@ export default async (settings: Partial<Settings> = {}): Promise<Components> =>
welo,
heliaPinManager,
events,
keyManager
keyManager,
unixfs
}

handleCommands(components)
Expand Down
2 changes: 2 additions & 0 deletions packages/daemon/src/common/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type { Sneakernet } from './sneakernet/index.js'
import type { Tick } from './tick.js'
import type { KeyvalueDB } from '@/interface.js'
import type { Helia } from '@helia/interface'
import type { UnixFS } from '@helia/unixfs'
import type { Libp2p } from '@libp2p/interface'
import type HeliaPinManager from '@organicdesign/db-helia-pin-manager'
import type { KeyManager } from '@organicdesign/db-key-manager'
Expand Down Expand Up @@ -53,4 +54,5 @@ export interface Components {
heliaPinManager: HeliaPinManager
events: EventTarget
keyManager: KeyManager
unixfs: UnixFS
}
7 changes: 2 additions & 5 deletions packages/daemon/src/modules/filesystem/commands/read.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { unixfs } from '@helia/unixfs'
import { Read } from '@organicdesign/db-rpc-interfaces'
import { CID } from 'multiformats/cid'
import { collect } from 'streaming-iterables'
Expand All @@ -7,7 +6,7 @@ import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
import type { Context } from '../index.js'
import type { ModuleMethod } from '@/interface.js'

const command: ModuleMethod<Context> = ({ net, helia }, context) => {
const command: ModuleMethod<Context> = ({ net, unixfs }, context) => {
net.rpc.addMethod(Read.name, async (raw: unknown): Promise<Read.Return> => {
const params = Read.Params.parse(raw)
const fs = context.getFileSystem(CID.parse(params.group))
Expand All @@ -22,9 +21,7 @@ const command: ModuleMethod<Context> = ({ net, helia }, context) => {
throw new Error(`no such item: ${params.path}`)
}

const ufs = unixfs(helia)

return uint8ArrayToString(uint8ArrayConcat(await collect(ufs.cat(entry.cid, { offset: params.position, length: params.length }))))
return uint8ArrayToString(uint8ArrayConcat(await collect(unixfs.cat(entry.cid, { offset: params.position, length: params.length }))))
})
}

Expand Down
8 changes: 3 additions & 5 deletions packages/daemon/src/modules/filesystem/commands/write.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { unixfs } from '@helia/unixfs'
import { CustomEvent } from '@libp2p/interface'
import { Write } from '@organicdesign/db-rpc-interfaces'
import all from 'it-all'
Expand All @@ -9,7 +8,7 @@ import type { Context } from '../index.js'
import type { Entry } from '../interface.js'
import type { ModuleMethod } from '@/interface.js'

const command: ModuleMethod<Context> = ({ net, helia, events }, context) => {
const command: ModuleMethod<Context> = ({ net, unixfs, events }, context) => {
net.rpc.addMethod(Write.name, async (raw: unknown): Promise<Write.Return> => {
const params = Write.Params.parse(raw)
const group = CID.parse(params.group)
Expand All @@ -20,17 +19,16 @@ const command: ModuleMethod<Context> = ({ net, helia, events }, context) => {
}

const entry: Partial<Entry> = await fs.get(params.path) ?? {}
const ufs = unixfs(helia)

const existingData = (entry.cid != null) ? uint8ArrayConcat(await all(ufs.cat(entry.cid))) : new Uint8Array()
const existingData = (entry.cid != null) ? uint8ArrayConcat(await all(unixfs.cat(entry.cid))) : new Uint8Array()

const dataToWrite = uint8ArrayConcat([
existingData.slice(0, params.position),
uint8ArrayFromString(params.data),
existingData.slice(params.position + params.length)
])

const cid = await ufs.addBytes(dataToWrite)
const cid = await unixfs.addBytes(dataToWrite)

const newEntryParams = {
cid,
Expand Down
6 changes: 2 additions & 4 deletions packages/daemon/src/modules/filesystem/upload-operations.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import Path from 'path'
import { unixfs } from '@helia/unixfs'
import { CustomEvent } from '@libp2p/interface'
import { type RevisionStrategies } from '@organicdesign/db-rpc-interfaces/zod'
import all from 'it-all'
Expand All @@ -12,7 +11,7 @@ import type { Pair } from '@/interface.js'
import type { Datastore } from 'interface-datastore'
import { OperationManager } from '@/operation-manager.js'

export default async (context: Pick<Context, 'getFileSystem'>, { events, pinManager, helia }: Components, datastore: Datastore): Promise<OperationManager<{
export default async (context: Pick<Context, 'getFileSystem'>, { events, pinManager, unixfs }: Components, datastore: Datastore): Promise<OperationManager<{
put(groupData: Uint8Array, path: string, encodedEntry: { cid: Uint8Array, encrypted: boolean, revisionStrategy: RevisionStrategies, priority: number }): Promise<void>
delete(groupData: Uint8Array, path: string): Promise<Array<Pair<string, Entry>>>
}>> => {
Expand Down Expand Up @@ -42,8 +41,7 @@ export default async (context: Pick<Context, 'getFileSystem'>, { events, pinMana
}

const parentPath = path.split('/').slice(0, -2).join('/')
const ufs = unixfs(helia)
const cid = await ufs.addBytes(new Uint8Array([]))
const cid = await unixfs.addBytes(new Uint8Array([]))
const pairs = await all(fs.getDir(path))

for await (const p of pairs) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { unixfs } from '@helia/unixfs'
import { ReadRevision } from '@organicdesign/db-rpc-interfaces'
import { CID } from 'multiformats/cid'
import { collect } from 'streaming-iterables'
Expand All @@ -8,7 +7,7 @@ import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
import type { Context } from '../index.js'
import type { ModuleMethod } from '@/interface.js'

const command: ModuleMethod<Context> = ({ net, helia }, context) => {
const command: ModuleMethod<Context> = ({ net, unixfs }, context) => {
net.rpc.addMethod(ReadRevision.name, async (raw: unknown): Promise<ReadRevision.Return> => {
const params = ReadRevision.Params.parse(raw)
const group = CID.parse(params.group)
Expand All @@ -26,9 +25,7 @@ const command: ModuleMethod<Context> = ({ net, helia }, context) => {
throw new Error(`no such revision: ${params.path}, ${author}, ${params.sequence}`)
}

const ufs = unixfs(helia)

return uint8ArrayToString(uint8ArrayConcat(await collect(ufs.cat(entry.cid, { offset: params.position, length: params.length }))))
return uint8ArrayToString(uint8ArrayConcat(await collect(unixfs.cat(entry.cid, { offset: params.position, length: params.length }))))
})
}

Expand Down

0 comments on commit 61d4a38

Please sign in to comment.