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: update docs for client v15 #93

Merged
merged 2 commits into from
Jun 14, 2024
Merged
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
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
13 changes: 6 additions & 7 deletions src/pages/docs/concepts/car.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,13 @@ 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 { CarReader } from '@ipld/car'
import fs from 'node:fs'
import { Readable } from 'node:stream'

async function storeCarFile(filename) {
const inStream = createReadStream(filename)
const car = await CarReader.fromIterable(inStream)
const stream = () => Readable.toWeb(fs.createReadStream(filename))
const client = makeStorageClient()
const cid = await client.uploadCAR(car)
const cid = await client.uploadCAR({ stream })
console.log('Stored CAR file! CID:', cid)
}
```
Expand All @@ -114,7 +113,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 +123,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
Loading