Skip to content
This repository has been archived by the owner on Mar 29, 2023. It is now read-only.

Consume upload-client and access-client #56

Closed
alanshaw opened this issue Nov 29, 2022 · 4 comments · Fixed by #58
Closed

Consume upload-client and access-client #56

alanshaw opened this issue Nov 29, 2022 · 4 comments · Fixed by #58
Assignees
Milestone

Comments

@alanshaw
Copy link
Member

alanshaw commented Nov 29, 2022

This module will become a thin wrapper around access-client and upload-client, with the following proposed API:

export class Client {
  /* passthroughs to `upload-client` */
  uploadFile(file: Blob, options: UploadOptions): Promise<CID>
  uploadDirectory(dir: File[], options: UploadOptions): Promise<CID>

  /* passthroughs to `access-client` */
  agent(): Signer
  // space management
  currentSpace(): Space|undefined
  setCurrentSpace(did: DID): Promise<void>
  spaces(): Space[]
  createSpace(name?: string): Promise<DID>
  registerSpace(email: string): Promise<void>
  addSpace(proof: Delegation): Promise<void>
  // capability management
  proofs(): Delegation[]
  addProof(proof: Delegation): Promise<void>
  delegations(): Delegation[]
  createDelegation(audience: Principal, abilities: Abilities[], options: DelegationOptions): Delegation

  /* Advanced API - grouped by capability namespace: *************************/

  capability: {
    /* passthroughs to `upload-client` */
    store: {
      add(carBytes: Blob, options: RequestOptions): Promise<CID>
      remove(carCID: CID, options: RequestOptions): Promise<void>
      list(options: ListRequestOptions): Promise<ListResponse<StoreListResult>>
    }
  
    /* passthroughs to `upload-client` */
    upload: {
      add(root: CID, shards: CID[], options: RequestOptions): Promise<void>
      remove(root: CID, options: RequestOptions): Promise<void>
      list(options: ListRequestOptions): Promise<ListResponse<UploadListResult>>
    }
  
    /* passthroughs to `access-client` */
    space {
      info(space: DID): Promise<SpaceInfo>
      recover(space: DID, email: string): Promise<void>
    }
  }
}

export function create(options?: ClientFactoryOptions): Promise<Client>
@alanshaw alanshaw added this to the w3up phase 1 milestone Nov 29, 2022
@alanshaw alanshaw self-assigned this Nov 29, 2022
@jchris
Copy link

jchris commented Nov 29, 2022

Will uploadFile automatically fast-path when the file is small? Eg no chunking, inline CAR if possible, etc?

@ice-breaker-tg
Copy link
Contributor

ice-breaker-tg commented Nov 29, 2022

This looks good to me, the wrapping of stuff is similar to what we had hoped to do before so I think this is a great api.
The namespacing is also very nice IMO.

My only concern is the "overlap" of uploadFile, and upload.* namespaces (I think that internally uploadFile will call the other and handle it, but that is exposed as a lower level api? If so, I think that's fine but we should clarify in docs or etc).

@alanshaw
Copy link
Member Author

Will uploadFile automatically fast-path when the file is small? Eg no chunking, inline CAR if possible, etc?

If the file is smaller then the minimum shard size then it won't be sharded. It can definitely auto inline but that's not something that we support yet!

My only concern is the "overlap" of uploadFile, and upload.* namespaces

Yes, root API uploadFile and uploadDirectory are both sugar for unixfs encode + store/add + upload/add. I'm open to suggestion for a better name?

@alanshaw
Copy link
Member Author

alanshaw commented Dec 6, 2022

My only concern is the "overlap" of uploadFile, and upload.* namespaces (I think that internally uploadFile will call the other and handle it, but that is exposed as a lower level api? If so, I think that's fine but we should clarify in docs or etc).

Totally valid, my current thinking is to put these under capability in the client, so you'd call something like client.capability.upload.add(root, shards), which is...verbose, but maybe a better separation.

alanshaw pushed a commit that referenced this issue Dec 14, 2022
This PR updates `w3up-client` to consume the [access-client](https://www.npmjs.com/package/@web3-storage/access) and [upload-client](https://www.npmjs.com/package/@web3-storage/upload-client) modules. It brings:

* Convenience methods for storing files/directories and registering uploads
* Automatic CAR sharding
* Support for store/upload list pagination
* Automatic data store selection (IndexedDB in browser, Conf in Node.js)
* Automatic key selection (RSA in browser, Ed25519 in Node.js)
* 100% test coverage

It exports the following API:

```ts
export class Client {
  /* passthroughs to `upload-client` */
  uploadFile(file: Blob, options: UploadOptions): Promise<CID>
  uploadDirectory(dir: File[], options: UploadOptions): Promise<CID>

  /* passthroughs to `access-client` */
  agent(): Signer
  // space management
  currentSpace(): Space|undefined
  setCurrentSpace(did: DID): Promise<void>
  spaces(): Space[]
  createSpace(name?: string): Promise<DID>
  registerSpace(email: string): Promise<void>
  addSpace(proof: Delegation): Promise<void>
  // capability management
  proofs(): Delegation[]
  addProof(proof: Delegation): Promise<void>
  delegations(): Delegation[]
  createDelegation(audience: Principal, abilities: Abilities[], options: DelegationOptions): Delegation

  /* Advanced API - grouped by capability namespace: *************************/

  capability: {
    /* passthroughs to `upload-client` */
    store: {
      add(carBytes: Blob, options: RequestOptions): Promise<CID>
      remove(carCID: CID, options: RequestOptions): Promise<void>
      list(options: ListRequestOptions): Promise<ListResponse<StoreListResult>>
    }
  
    /* passthroughs to `upload-client` */
    upload: {
      add(root: CID, shards: CID[], options: RequestOptions): Promise<void>
      remove(root: CID, options: RequestOptions): Promise<void>
      list(options: ListRequestOptions): Promise<ListResponse<UploadListResult>>
    }
  
    /* passthroughs to `access-client` */
    space {
      info(space: DID): Promise<SpaceInfo>
      recover(space: DID, email: string): Promise<void>
    }
  }
}

export function create(options?: ClientFactoryOptions): Promise<Client>
```

resolves #56

BREAKING CHANGE: The client has been re-written as a wrapper around [access-client](https://www.npmjs.com/package/@web3-storage/access) and [upload-client](https://www.npmjs.com/package/@web3-storage/upload-client) and the API has changed. Migration notes:

* `client.account()` has been removed, use `client.currentSpace()`
* `client.exportDelegation()` has been removed, use `client.createDelegation()` and then call `export()` on the returned value and encode the returned blocks as a CAR file using the [`@ipld/car`](https://www.npmjs.com/package/@ipld/car) library.
* `client.identity()` has been removed, use `client.agent()` + `client.currentSpace()` + `client.delegations()`
* `client.importDelegation()` has been removed, use `client.addProof()` (for general delegations to your agent) or `client.addSpace()` (to add a proof and _also_ add the space to your list of spaces).
* `client.insights()` has been removed - this was never working
* `client.invoke()` has been removed
* `client.list()` has been removed, use `client.capability.upload.list()`
* `client.makeDelegation()` has been renamed and signature has changed, use `client.createDelegation()`
* `client.register()` has been removed, use `client.registerSpace()`
* `client.remove()` has been removed, use `client.capability.store.remove()`
* `client.removeUpload()` has been removed, use `client.capability.upload.remove()`
* `client.stat()` has been removed, use `client.capability.store.list()`
* `client.upload()` has been removed, use `client.capability.store.add()`
* `client.uploadAdd()` has been removed, use `client.capability.upload.add()`
* `client.whoami()` has been removed, use `client.capability.space.info()`
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants