Deno write large file
How can I convert this node code to Deno
how can I write large file in Deno and write only when the highWaterMark reached so I can apply draining?
6 Replies
In Deno, you can achieve similar functionality to Node's streams by using Deno's built-in
writable
streams. The documentation provides an example of using writable streams for file operations, which can be adapted for your use case.
Here's how you can convert your Node.js code to Deno:
1. Use a generator function to create your large array.
2. Open a file for writing using Deno.open
with the create
and write
options.
3. Get a writer from the file's writable
property.
4. Write to the file using the writer's write
method, which returns a promise that resolves when the write operation is complete.
5. Use the ready
property of the writer to wait until it's ready to write again, which is analogous to Node's drain
event.
Here's an example based on the documentation:
This code does not explicitly check for a highWaterMark or implement a drain mechanism like in Node.js. However, the await writer.ready
line ensures that the writer is ready to handle more data, which serves a similar purpose to waiting for a drain event in Node.js.
Remember to run your Deno script with the--allow-write
flag to grant permission for file writing operations:
š¦ If you need more granular control over the backpressure mechanism, you might need to implement additional logic to handle the case when the internal buffer of the writable stream is full. However, the provided example should suffice for most use cases where you simply want to write to a file and wait as needed for the system to be ready to accept more data.2 ways i'd do it:
or if you have a open file already:
I tried both of the methods you recommended, but they are significantly slower compared to the Node version. Is there any way to make them as fast as the Node implementation?
Sorry for my ignorance, but why is the TransformStream needed in this case?
the generator generates numbers, so gotta turn it into strings, and since we want to add newlines, we add those as well
hm, i am not entirely sure