Skip to content

Type error for the upload example in the docsΒ #82

@2color

Description

@2color

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions