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

feat(io): re-introduce IO functions #4128

Merged
merged 9 commits into from
Jan 12, 2024
Merged

feat(io): re-introduce IO functions #4128

merged 9 commits into from
Jan 12, 2024

Conversation

iuioiua
Copy link
Contributor

@iuioiua iuioiua commented Jan 8, 2024

This change re-introduces some of the previously deprecated and I/O-related functions and types. In short, these functions are moved from std/streams to std/io, which makes a lot more sense, in my opinion. These changes also include a few minor non-breaking (hopefully) tweaks.

The refreshed std/io API aims to be as basic and minimal as possible while still covering the basic I/O modalities such as reading (readAll()), writing (writeAll()) and transferring (copy()), as well as conversion utilities to the Streams API's interfaces for more advanced use. E.g. TextLineStream(). There's an argument to be made that storage (Buffer) should be included in these primary modalities, but users may be fine without it.

Closes #4120
Towards denoland/deno#9795

archive/tar_test.ts Outdated Show resolved Hide resolved
* file.close();
* ```
*/
export async function readAll(reader: Reader): Promise<Uint8Array> {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function differs from readAll() from std/streams in that it doesn't depend on Buffer() from std/io. It's simpler, only relying on concat(). This should be a non-breaking change.

*
* @example
* ```ts
* import { readAll } from "https://deno.land/std@$STD_VERSION/io/read_all.ts";
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: Buffer() is not imported here as it's deprecated.

Deno.test("readAll()", async () => {
const testBytes = init();
assert(testBytes);
const reader = new Buffer(testBytes.buffer);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We rely on Buffer() here but should work on removing that dependency in the future (in a PR).

* const fileStream = toReadableStream(file);
* ```
*/
export function toReadableStream(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: renamed from readableStreamfFromReader().

* .pipeTo(toWritableStream(file));
* ```
*/
export function toWritableStream(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: renamed from writableStreamFromWriter().

@iuioiua iuioiua marked this pull request as ready for review January 9, 2024 21:47
@iuioiua iuioiua requested a review from crowlKats as a code owner January 9, 2024 21:47
@iuioiua iuioiua marked this pull request as draft January 9, 2024 23:02
@iuioiua iuioiua marked this pull request as ready for review January 9, 2024 23:28
@iuioiua iuioiua requested a review from kt3k January 9, 2024 23:28
@@ -22,8 +22,7 @@ import type { Reader, Writer } from "../io/types.ts";
* @param dst The destination to copy to
* @param options Can be used to tune size of the buffer. Default size is 32kB
*
* @deprecated (will be removed after 1.0.0) Use
* {@linkcode ReadableStream.pipeTo} instead.
* @deprecated (will be removed in 0.214.0) Import from {@link https://deno.land/std/io/copy.ts} instead.
*/
export async function copy(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: a copySync() could be implemented. But there's never been any demand for it. I also wonder whether there's demand for readAllSync() and writeAllSync().

We could also name this copyAll() to align it with readAll() and writeAll().

Copy link
Member

@kt3k kt3k left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

suggestion: re-consider the deprecation of some IO-related APIs
2 participants