Skip to content

Commit a9fe160

Browse files
authored
feat(io): re-introduce IO functions (#4128)
1 parent 15a97b2 commit a9fe160

26 files changed

+639
-106
lines changed

archive/tar_test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { Tar } from "./tar.ts";
1515
import { Untar } from "./untar.ts";
1616
import { Buffer } from "../io/buffer.ts";
1717
import { copy } from "../streams/copy.ts";
18-
import { readAll } from "../streams/read_all.ts";
18+
import { readAll } from "../io/read_all.ts";
1919
import { filePath, testdataDir } from "./_test_common.ts";
2020

2121
Deno.test("createTarArchive", async function () {

archive/untar.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import {
3737
UstarFields,
3838
ustarStructure,
3939
} from "./_common.ts";
40-
import { readAll } from "../streams/read_all.ts";
40+
import { readAll } from "../io/read_all.ts";
4141
import type { Reader } from "../io/types.ts";
4242

4343
/**

archive/untar_test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
} from "./untar.ts";
1111
import { Buffer } from "../io/buffer.ts";
1212
import { copy } from "../streams/copy.ts";
13-
import { readAll } from "../streams/read_all.ts";
13+
import { readAll } from "../io/read_all.ts";
1414
import { filePath, testdataDir } from "./_test_common.ts";
1515

1616
interface TestEntry {

http/server_test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
import { ConnInfo, serve, serveListener, Server, serveTls } from "./server.ts";
33
import { mockConn as createMockConn } from "./_mock_conn.ts";
44
import { dirname, fromFileUrl, join, resolve } from "../path/mod.ts";
5-
import { writeAll } from "../streams/write_all.ts";
6-
import { readAll } from "../streams/read_all.ts";
5+
import { writeAll } from "../io/write_all.ts";
6+
import { readAll } from "../io/read_all.ts";
77
import { delay } from "../async/mod.ts";
88
import {
99
assert,

io/_common.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
2+
// This module is browser compatible.
3+
4+
import type { Closer } from "./types.ts";
5+
6+
export function isCloser(value: unknown): value is Closer {
7+
return typeof value === "object" && value !== null && value !== undefined &&
8+
"close" in value &&
9+
// deno-lint-ignore no-explicit-any
10+
typeof (value as Record<string, any>)["close"] === "function";
11+
}

io/_constants.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
2+
// This module is browser compatible.
3+
4+
export const DEFAULT_CHUNK_SIZE = 16_640;
5+
export const DEFAULT_BUFFER_SIZE = 32 * 1024;

io/_test_common.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,14 @@ export class BinaryReader implements Reader {
2727
return Promise.resolve(p.byteLength);
2828
}
2929
}
30+
31+
// N controls how many iterations of certain checks are performed.
32+
const N = 100;
33+
34+
export function init(): Uint8Array {
35+
const testBytes = new Uint8Array(N);
36+
for (let i = 0; i < N; i++) {
37+
testBytes[i] = "a".charCodeAt(0) + (i % 26);
38+
}
39+
return testBytes;
40+
}

io/buffer_test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
assertThrows,
1111
} from "../assert/mod.ts";
1212
import { Buffer } from "./buffer.ts";
13-
import { writeAllSync } from "../streams/write_all.ts";
13+
import { writeAllSync } from "./write_all.ts";
1414

1515
const MAX_SIZE = 2 ** 32 - 2;
1616
// N controls how many iterations of certain checks are performed.

io/copy.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
2+
// This module is browser compatible.
3+
4+
import { DEFAULT_BUFFER_SIZE } from "./_constants.ts";
5+
import type { Reader, Writer } from "./types.ts";
6+
7+
/**
8+
* Copies from `src` to `dst` until either EOF (`null`) is read from `src` or
9+
* an error occurs. It resolves to the number of bytes copied or rejects with
10+
* the first error encountered while copying.
11+
*
12+
* @example
13+
* ```ts
14+
* import { copy } from "https://deno.land/std@$STD_VERSION/io/copy.ts";
15+
*
16+
* const source = await Deno.open("my_file.txt");
17+
* const bytesCopied1 = await copy(source, Deno.stdout);
18+
* const destination = await Deno.create("my_file_2.txt");
19+
* const bytesCopied2 = await copy(source, destination);
20+
* ```
21+
*
22+
* @param src The source to copy from
23+
* @param dst The destination to copy to
24+
* @param options Can be used to tune size of the buffer. Default size is 32kB
25+
*/
26+
export async function copy(
27+
src: Reader,
28+
dst: Writer,
29+
options?: {
30+
bufSize?: number;
31+
},
32+
): Promise<number> {
33+
let n = 0;
34+
const bufSize = options?.bufSize ?? DEFAULT_BUFFER_SIZE;
35+
const b = new Uint8Array(bufSize);
36+
let gotEOF = false;
37+
while (gotEOF === false) {
38+
const result = await src.read(b);
39+
if (result === null) {
40+
gotEOF = true;
41+
} else {
42+
let nwritten = 0;
43+
while (nwritten < result) {
44+
nwritten += await dst.write(b.subarray(nwritten, result));
45+
}
46+
n += nwritten;
47+
}
48+
}
49+
return n;
50+
}

io/copy_test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
2+
import { copy } from "./copy.ts";
3+
import { assertEquals } from "../assert/assert_equals.ts";
4+
5+
const SRC_PATH = "./io/testdata/copy-src.txt";
6+
const DST_PATH = "./io/testdata/copy-dst.txt";
7+
8+
Deno.test("copy()", async () => {
9+
try {
10+
using srcFile = await Deno.open(SRC_PATH);
11+
using dstFile = await Deno.open(DST_PATH, {
12+
create: true,
13+
write: true,
14+
});
15+
await copy(srcFile, dstFile);
16+
const srcOutput = await Deno.readFile(SRC_PATH);
17+
const dstOutput = await Deno.readFile(DST_PATH);
18+
assertEquals(srcOutput, dstOutput);
19+
} finally {
20+
await Deno.remove(DST_PATH);
21+
}
22+
});

0 commit comments

Comments
 (0)