Skip to content

Commit

Permalink
feat: update docs for client v15
Browse files Browse the repository at this point in the history
  • Loading branch information
Alan Shaw committed Jun 14, 2024
1 parent cf262fb commit 7a96773
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 61 deletions.
21 changes: 5 additions & 16 deletions src/pages/docs/concepts/architecture-options.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,11 @@ A code example that does this can be found below:
**Backend**

```js
import { CarReader } from '@ipld/car'
import * as DID from '@ipld/dag-ucan/did'
import * as Delegation from '@ucanto/core/delegation'
import * as Signer from '@ucanto/principal/ed25519'
import * as Client from '@web3-storage/w3up-client'
import { StoreMemory } from '@web3-storage/w3up-client/stores/memory'
import * as Proof from '@web3-storage/w3up-client/proof'
import { Signer } from '@web3-storage/w3up-client/principal/ed25519'
import * as DID from '@ipld/dag-ucan/did'

async function backend(did) {
// Load client with specific private key
Expand All @@ -92,7 +91,7 @@ async function backend(did) {
const client = await Client.create({ principal, store })

// Add proof that this agent has been delegated capabilities on the space
const proof = await parseProof(process.env.PROOF)
const proof = await Proof.parse(process.env.PROOF)
const space = await client.addSpace(proof)
await client.setCurrentSpace(space.did())

Expand All @@ -106,22 +105,12 @@ async function backend(did) {
const archive = await delegation.archive()
return archive.ok
}

/** @param {string} data Base64 encoded CAR file */
async function parseProof(data) {
const blocks = []
const reader = await CarReader.fromBytes(Buffer.from(data, 'base64'))
for await (const block of reader.blocks()) {
blocks.push(block)
}
return Delegation.importDAG(blocks)
}
```

**Frontend**

```js
import * as Delegation from '@ucanto/core/delegation'
import * as Delegation from '@web3-storage/w3up-client/delegation'
import * as Client from '@web3-storage/w3up-client'

async function frontend() {
Expand Down
8 changes: 4 additions & 4 deletions src/pages/docs/concepts/car.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,11 @@ The [`@ipld/car`](https://github.com/ipld/js-car) package contains the main Java
Here's a simple example of loading a CAR file from a Node.js stream and storing it with web3.storage:

```js
import { createReadStream } from 'fs'
import fs from 'node:fs'
import { CarReader } from '@ipld/car'

async function storeCarFile(filename) {
const inStream = createReadStream(filename)
const inStream = fs.createReadStream(filename)
const car = await CarReader.fromIterable(inStream)
const client = makeStorageClient()
const cid = await client.uploadCAR(car)
Expand All @@ -114,7 +114,7 @@ CarReader.fromIterable accepts any iterable of Uint8Array data, including Node.j

The CarReader type shown above will read the entire contents of the CAR into memory, which may cause issues with large files. On Node.js, you can use [CarIndexedReader](https://github.com/ipld/js-car#carindexedreader), which reads CAR data from disk directly and uses less memory than CarReader.

## [Advanced IPLD formats](https://web3.storage/docs/how-tos/work-with-car-files/#advanced-ipld-formats)
## Advanced IPLD formats

IPLD can also be used as a general purpose data format like JSON. In fact, you can use JSON directly as IPLD just by using a special convention for linking to other IPLD objects. This convention is defined in the [dag-json](https://ipld.io/docs/codecs/known/dag-json/)["codec"](https://ipld.io/docs/codecs/known/dag-json/).

Expand All @@ -124,7 +124,7 @@ Here's an example of a `dag-json` object:
{
"name": "Have you seen this dog?",
"description": "I have now...",
"image":{"/":"bafybeihkqv2ukwgpgzkwsuz7whmvneztvxglkljbs3zosewgku2cfluvba"}
"image": { "/": "bafybeihkqv2ukwgpgzkwsuz7whmvneztvxglkljbs3zosewgku2cfluvba" }
}
```

Expand Down
4 changes: 2 additions & 2 deletions src/pages/docs/how-to/remove.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,5 @@ To remove shard CIDs and upload CIDs separately, you'll generally do this by:
- Client: `client.capability.upload.remove(contentCID)`
- CLI: `w3 can upload rm <contentCID>`
1. Remove each of the shards (ensure first that no other content is using that shard!):
- Client: `client.capability.store.remove(shardCID)`
- CLI: `w3 can store rm <shardCID>`
- Client: `client.capability.blob.remove(shardMultihash)`
- CLI: `w3 can blob rm <shardMultihash>`
49 changes: 10 additions & 39 deletions src/pages/docs/how-to/upload.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -131,48 +131,30 @@ w3 key create
# If you want to limit permissions being passed to the Agent, you can specify
# permissions to give, e.g., `--can space/blob/add --can space/index/add --can
# filecoin/offer --can upload/add` limits to just being able to upload.
w3 delegation create <did_from_ucan-key_command_above> | base64
w3 delegation create <did_from_ucan-key_command_above> --base64

# ❗️ Store the output in environment variable PROOF
```

Then, when you initialize and configure the client, you can pass in this Agent and UCAN.
Then, when you initialize and configure the client, you can pass in this private key and UCAN delegation.

```javascript
import * as Client from '@web3-storage/w3up-client'
import { StoreMemory } from '@web3-storage/w3up-client/stores/memory'
import { importDAG } from '@ucanto/core/delegation'
import { CarReader } from '@ipld/car'
import * as Signer from '@ucanto/principal/ed25519'
import * as Proof from '@web3-storage/w3up-client/proof'
import { Signer } from '@web3-storage/w3up-client/principal/ed25519'

async function main () {
// Load client with specific private key
const principal = Signer.parse(process.env.KEY)
const store = new StoreMemory()
const client = await Client.create({ principal, store })
// Add proof that this agent has been delegated capabilities on the space
const proof = await parseProof(process.env.PROOF)
const proof = await Proof.parse(process.env.PROOF)
const space = await client.addSpace(proof)
await client.setCurrentSpace(space.did())
// READY to go!
}

/** @param {string} data Base64 encoded CAR file */
async function parseProof (data) {
const blocks = []
const reader = await CarReader.fromBytes(Buffer.from(data, 'base64'))
for await (const block of reader.blocks()) {
blocks.push(block)
}
return importDAG(blocks)
}
```

If you're doing this in a non-persistent or serverless backend, you might consider using an in-memory [Store](https://github.com/web3-storage/w3up/tree/main/packages/access-client) for your Agent information rather than the default on-disk:

```javascript
import { StoreMemory } from '@web3-storage/access/stores/store-memory'
const client = await Client.create({ principal, store: new StoreMemory() })
```

### Uploading to web3.storage
Expand Down Expand Up @@ -226,12 +208,11 @@ Your backend instance can also be used to delegate upload permissions directly t
**Backend**

```js
import { CarReader } from '@ipld/car'
import * as DID from '@ipld/dag-ucan/did'
import * as Delegation from '@ucanto/core/delegation'
import * as Signer from '@ucanto/principal/ed25519'
import * as Client from '@web3-storage/w3up-client'
import { StoreMemory } from '@web3-storage/w3up-client/stores/memory'
import * as Proof from '@web3-storage/w3up-client/proof'
import { Signer } from '@web3-storage/w3up-client/principal/ed25519'
import * as DID from '@ipld/dag-ucan/did'

async function backend(did) {
// Load client with specific private key
Expand All @@ -240,7 +221,7 @@ async function backend(did) {
const client = await Client.create({ principal, store })

// Add proof that this agent has been delegated capabilities on the space
const proof = await parseProof(process.env.PROOF)
const proof = await Proof.parse(process.env.PROOF)
const space = await client.addSpace(proof)
await client.setCurrentSpace(space.did())

Expand All @@ -254,22 +235,12 @@ async function backend(did) {
const archive = await delegation.archive()
return archive.ok
}

/** @param {string} data Base64 encoded CAR file */
async function parseProof(data) {
const blocks = []
const reader = await CarReader.fromBytes(Buffer.from(data, 'base64'))
for await (const block of reader.blocks()) {
blocks.push(block)
}
return Delegation.importDAG(blocks)
}
```

**Frontend**

```js
import * as Delegation from '@ucanto/core/delegation'
import * as Delegation from '@web3-storage/w3up-client/delegation'
import * as Client from '@web3-storage/w3up-client'

async function frontend() {
Expand Down

0 comments on commit 7a96773

Please sign in to comment.