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

Abstraction of CID to provide several returning types #209

Closed
wants to merge 2 commits into from
Closed
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 packages/auto-drive/src/api/models/uploads.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { z } from 'zod'
import { AutoCID } from '../../utils/autohash'
import { FolderTreeFolderSchema } from './folderTree'

export enum UploadType {
Expand Down Expand Up @@ -79,7 +80,7 @@ type ObjectUploadStatus<Type extends 'file' | 'folder'> =
completed: true
type: Type
progress: number
cid: string
cid: AutoCID
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cid should remain a string, if you want to add an optional hash parameter that get return only if some optional input parameter is present, I'm fine with that.

But I don't want cid to become an object or to return the hash by default, I think this will be a step backward for the devEx,

}
| {
completed: false
Expand Down
7 changes: 4 additions & 3 deletions packages/auto-drive/src/api/wrappers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import mime from 'mime-types'
import { asyncByChunk, asyncFromStream, fileToIterable } from '../utils/async'
import { AutoCID } from '../utils/autohash'
import { progressToPercentage } from '../utils/misc'
import { PromisedObservable } from '../utils/observable'
import { apiCalls } from './calls/index'
Expand Down Expand Up @@ -110,7 +111,7 @@ export const uploadFileFromInput = (
type: 'file',
progress: 100,
completed: true,
cid: result.cid,
cid: await AutoCID.create(result.cid),
})
subscriber.complete()
})
Expand Down Expand Up @@ -193,7 +194,7 @@ export const uploadFile = (
type: 'file',
progress: 100,
completed: true,
cid: result.cid,
cid: await AutoCID.create(result.cid),
})
subscriber.complete()
})
Expand Down Expand Up @@ -285,7 +286,7 @@ export const uploadFolderFromInput = async (
type: 'folder',
progress: 100,
completed: true,
cid: result.cid,
cid: await AutoCID.create(result.cid),
})
subscriber.complete()
})
Expand Down
3 changes: 2 additions & 1 deletion packages/auto-drive/src/fs/wrappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { GenericFileWithinFolder } from '../api/models/file.js'
import { constructFromFileSystemEntries } from '../api/models/folderTree.js'
import { UploadFileStatus, UploadFolderStatus } from '../api/models/uploads.js'
import { uploadFile, UploadFileOptions, uploadFileWithinFolderUpload } from '../api/wrappers.js'
import { AutoCID } from '../utils/autohash.js'
import { fileToIterable } from '../utils/index.js'
import { progressToPercentage } from '../utils/misc.js'
import { PromisedObservable } from '../utils/observable.js'
Expand Down Expand Up @@ -139,7 +140,7 @@ export const uploadFolderFromFolderPath = async (
type: 'folder',
progress: 100,
completed: true,
cid: result.cid,
cid: await AutoCID.create(result.cid),
})
subscriber.complete()
})
Expand Down
39 changes: 39 additions & 0 deletions packages/auto-drive/src/utils/autohash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import type { CID } from 'multiformats/cid'

interface AutoCIDTransformers {
stringToCid: (cid: string) => CID
blake3HashFromCid: (cid: CID) => Uint8Array
}

export class AutoCID {
constructor(
private readonly cid: string,
private readonly tools: AutoCIDTransformers,
) {}

static async create(cid: string): Promise<AutoCID> {
const tools = await getToolsFromAutoDagData
return new AutoCID(cid, tools)
}

get asString(): string {
return this.cid
}

get asCID(): CID {
return this.tools.stringToCid(this.cid)
}

get asBlake3Hash(): Buffer {
return Buffer.from(this.tools.blake3HashFromCid(this.asCID))
}
}

export const getToolsFromAutoDagData: Promise<AutoCIDTransformers> = import(
'@autonomys/auto-dag-data'
).then(({ stringToCid, blake3HashFromCid }) => {
return {
stringToCid,
blake3HashFromCid,
}
})
Loading