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

getFilesFromPath works in Javascript but not in Typescript #830

Closed
partment opened this issue Dec 29, 2021 · 3 comments · Fixed by storacha/files-from-path#13
Closed

getFilesFromPath works in Javascript but not in Typescript #830

partment opened this issue Dec 29, 2021 · 3 comments · Fixed by storacha/files-from-path#13
Labels
kind/bug A bug in existing code (including security flaws) need/triage Needs initial labeling and prioritization

Comments

@partment
Copy link

partment commented Dec 29, 2021

My code looks like:

import { Web3Storage, getFilesFromPath } from 'web3.storage'

const files = await getFilesFromPath('path/to/a/folder')
const onRootCidReady = rootCid => console.log('root cid:', rootCid)
const rootCid = await client.put(files, {
  wrapWithDirectory: false,
  onRootCidReady
})

This code works in Node.js but when I migrated to Typescript
Following error comes out when I compile Typescript into CommonJS

src/index.ts:83:36 - error TS2345: Argument of type 'FileObject[]' is not assignable to parameter of type 'Iterable<Filelike>'.
  The types returned by '[Symbol.iterator]().next(...)' are incompatible between these types.
    Type 'IteratorResult<FileObject, any>' is not assignable to type 'IteratorResult<Filelike, any>'.
      Type 'IteratorYieldResult<FileObject>' is not assignable to type 'IteratorResult<Filelike, any>'.
        Type 'IteratorYieldResult<FileObject>' is not assignable to type 'IteratorYieldResult<Filelike>'.
          Type 'FileObject' is not assignable to type 'Filelike'.
            Types of property 'stream' are incompatible.
              Type 'AsyncIterator<Buffer, any, undefined>' is not assignable to type '() => ReadableStream<any>'.
                Type 'AsyncIterator<Buffer, any, undefined>' provides no match for the signature '(): ReadableStream<any>'.

83   const rootCid = await client.put(files, {
                                      ~~~~~

I've done some researches and it's apparently not helping me at all:
whatwg/streams#778
microsoft/TypeScript#39051

I don't even know whether this is a Typescript bug or not.

My tsconfig.json

{
  "compilerOptions": {
    "module": "commonjs",
    "target": "esnext",
    "outDir": "dist",
    "moduleResolution": "node",
    "esModuleInterop": true,
    "removeComments": true,
    "preserveConstEnums": true,
    "sourceMap": false
  }
}

I really need help, thks.

Update 1

I mirgrated the code from
https://github.com/web3-storage/files-from-path/blob/main/src/index.js#L32
like this

import { Web3Storage, filesFromPath } from 'web3.storage'

const files = []
for await (const file of filesFromPath('path/to/a/folder', { hidden: false })) {
  files.push(file)
}
const onRootCidReady = rootCid => console.log('root cid:', rootCid)
const rootCid = await client.put(files, {
  wrapWithDirectory: false,
  onRootCidReady
})

Surprisingly it works, still don't know why.

@partment partment added kind/bug A bug in existing code (including security flaws) need/triage Needs initial labeling and prioritization labels Dec 29, 2021
@vasco-santos
Copy link
Contributor

Hey @partment

Created storacha/files-from-path#13 which should fix this issue. It should return a function that returns a fs.readStream

I think the second solution works because files type will be Array<any>

@partment
Copy link
Author

@vasco-santos Thank you for fixing the problem.

@vasco-santos
Copy link
Contributor

No problem @partment It is already released now :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/bug A bug in existing code (including security flaws) need/triage Needs initial labeling and prioritization
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants