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

ReadableStream instead of FileDirLike? #6

Closed
Manouchehri opened this issue Dec 1, 2022 · 3 comments
Closed

ReadableStream instead of FileDirLike? #6

Manouchehri opened this issue Dec 1, 2022 · 3 comments
Labels
enhancement New feature or request

Comments

@Manouchehri
Copy link

For large files, buffering the entire thing into an ArrayBuffer or Blob for new File(...) isn't always doable. Would it be possible to have create([my_ReadableStream]) to avoid needing to buffer everything at once?

@Sec-ant Sec-ant added the enhancement New feature or request label Dec 1, 2022
@Sec-ant
Copy link
Owner

Sec-ant commented Dec 1, 2022

Thanks for the suggestion.

If you know the size of the file, a simple solution is possible. We can define a new interface that structurally compatible with the File API (partially):

interface MyFile {
  name: string;
  size: number;
  webkitRelativePath: string;
  stream: () => ReableStream<Uint8Array>;
}

And you can put the your ReadableStream in the corresponding location.

Yet this new interface won't work in the current version.

A runnable patch in the current version would be something like this (not tested though):

const myFile = Object.create(File.prototype, {
  name: {
    value: "myFile.txt",
  },
  size: {
    value: 65536,
  },
  webkitRelativePath: {
    value: "",
  },
  stream: {
    value: () => myReadableStream,
  },
}) as File;

If you don't know the size in advance, it should still be possible, but I'll need to change several places in my code. This feature will be supported in the next major version, along with the async-iteration refactoring.

@Manouchehri
Copy link
Author

I should always know the file size in advanced. =)

@Manouchehri
Copy link
Author

Sweet, I can confirm your method works already. Looks like my torrents are functional!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants