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

Type error for the upload example in the docs #82

Open
2color opened this issue Feb 21, 2024 · 0 comments
Open

Type error for the upload example in the docs #82

2color opened this issue Feb 21, 2024 · 0 comments

Comments

@2color
Copy link
Contributor

2color commented Feb 21, 2024

The docs give the following code for uploading:

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'
 
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 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)
  }
  // 👇 type error for this call
  return importDAG(blocks) 

}

When trying this, I get the following type error

Argument of type 'Block[]' is not assignable to parameter of type 'Iterable<Block<unknown, number, number, 1>>'.
  The types returned by '[Symbol.iterator]().next(...)' are incompatible between these types.
    Type 'IteratorResult<Block, any>' is not assignable to type 'IteratorResult<Block<unknown, number, number, 1>, any>'.
      Type 'IteratorYieldResult<Block>' is not assignable to type 'IteratorResult<Block<unknown, number, number, 1>, any>'.
        Type 'IteratorYieldResult<Block>' is not assignable to type 'IteratorYieldResult<Block<unknown, number, number, 1>>'.
          Type 'Block' is not assignable to type 'Block<unknown, number, number, 1>'.
            The types of 'cid.version' are incompatible between these types.
              Type 'Version' is not assignable to type '1'.
                Type '0' is not assignable to type '1'.ts(2345)

Another thing is that the example is for Node.js and doesn't work out of the box in modern runtimes like cloudflare workers. It's probably worth mentioning or even providing an alternate example.

Errors/warnings in Cloudflare workers:

  • ▲ [WARNING] Import "Readable" will always be undefined because the file "(disabled):stream" has no exports [import-is-undefined]
  • Buffers don't exist, so the base64 encoded proof has to be converted into a Uint8Array which is rather cumbersome.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant