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

stream on sliced Bunfile doesn't work #7057

Open
bogeychan opened this issue Nov 11, 2023 · 4 comments
Open

stream on sliced Bunfile doesn't work #7057

bogeychan opened this issue Nov 11, 2023 · 4 comments
Labels
bug Something isn't working bun.js Something to do with a Bun-specific API good first issue Something that would be good for new contributors

Comments

@bogeychan
Copy link

What version of Bun is running?

1.0.11

What platform is your computer?

No response

What steps can reproduce the bug?

Create the following files

./file.txt

12345

./index.ts

const file = Bun.file('./file.txt').slice(0, 2);

const content = await(new Response(file.stream())).text();

console.log(content);
console.log(file.size);

Run

bun run index.ts

What is the expected behavior?

Console output:

12
2

What do you see instead?

Console output:

12345
2

Additional information

No response

@bogeychan bogeychan added the bug Something isn't working label Nov 11, 2023
@Electroid Electroid added bun.js Something to do with a Bun-specific API good first issue Something that would be good for new contributors labels Nov 13, 2023
@veera-sivarajan
Copy link

Hi, I'd like to work on this.

@botxboom
Copy link

Hi @bogeychan , I was going through the read-file docs(https://bun.sh/guides/read-file/string) and found Bun.File() returns a file instance as :
FileRef ("./file.txt") { type: "text/plain;charset=utf-8" }

and you are performing slice on this instance. which won't slice the text inside the file.

To achieve that you can do:

const content = await(new Response(file.stream())).text();
console.log(content.slice(0,2))

You can also do:
const file = Bun.file(path);
const content = await file.text();

@veera-sivarajan
Copy link

From the docs (https://bun.sh/guides/read-file/string):

The Bun.file() function accepts a path and returns a BunFile instance. The BunFile class extends Blob and allows you to lazily read the file in a variety of formats.

And slice() is a method on Blob (https://developer.mozilla.org/en-US/docs/Web/API/Blob). So I think Bun.file('./file.txt').slice(0, 2); should work as expected?

I'm not sure though.

@anderspitman
Copy link

anderspitman commented Feb 6, 2024

Given this bug, and the lack of node fs.createReadStream, is there currently a way to seek and stream chunks from a large file in Bun? It's not time critical for me, but my code supports Node and Deno, and would like it to support Bun eventually.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working bun.js Something to do with a Bun-specific API good first issue Something that would be good for new contributors
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants