diff --git a/cli/js/io.ts b/cli/js/io.ts index 0237181002fcc7..6cd957b763cc6c 100644 --- a/cli/js/io.ts +++ b/cli/js/io.ts @@ -68,8 +68,7 @@ export interface ReadWriteCloser extends Reader, Writer, Closer {} // https://golang.org/pkg/io/#ReadWriteSeeker export interface ReadWriteSeeker extends Reader, Writer, Seeker {} -// https://golang.org/pkg/io/#Copy -export async function copy(dst: Writer, src: Reader): Promise { +export async function copy(src: Reader, dst: Writer): Promise { let n = 0; const b = new Uint8Array(32 * 1024); let gotEOF = false; diff --git a/cli/js/lib.deno.ns.d.ts b/cli/js/lib.deno.ns.d.ts index 9980790b3376a3..fb0f8eefc6fe5b 100644 --- a/cli/js/lib.deno.ns.d.ts +++ b/cli/js/lib.deno.ns.d.ts @@ -565,16 +565,16 @@ declare namespace Deno { * * const source = await Deno.open("my_file.txt"); * const buffer = new Deno.Buffer() - * const bytesCopied1 = await Deno.copy(Deno.stdout, source); - * const bytesCopied2 = await Deno.copy(buffer, source); + * const bytesCopied1 = await Deno.copy(source, Deno.stdout); + * const bytesCopied2 = await Deno.copy(source, buffer); * * Because `copy()` is defined to read from `src` until `EOF`, it does not * treat an `EOF` from `read()` as an error to be reported. * - * @param dst The destination to copy to * @param src The source to copy from + * @param dst The destination to copy to */ - export function copy(dst: Writer, src: Reader): Promise; + export function copy(src: Reader, dst: Writer): Promise; /** **UNSTABLE**: new API, yet to be vetted * Turns a Reader, `r`, into an async iterator. diff --git a/cli/js/tests/files_test.ts b/cli/js/tests/files_test.ts index f68e5def4732c0..a095216b0779a4 100644 --- a/cli/js/tests/files_test.ts +++ b/cli/js/tests/files_test.ts @@ -18,7 +18,7 @@ unitTest({ perms: { read: true } }, async function filesCopyToStdout(): Promise< const filename = "cli/tests/fixture.json"; const file = await Deno.open(filename); assert(file.rid > 2); - const bytesWritten = await Deno.copy(Deno.stdout, file); + const bytesWritten = await Deno.copy(file, Deno.stdout); const fileSize = Deno.statSync(filename).size; assertEquals(bytesWritten, fileSize); console.log("bytes written", bytesWritten); diff --git a/cli/tests/cat.ts b/cli/tests/cat.ts index 756238be61a93f..bd6b5af06c2293 100644 --- a/cli/tests/cat.ts +++ b/cli/tests/cat.ts @@ -4,7 +4,7 @@ async function main(): Promise { for (let i = 1; i < args.length; i++) { const filename = args[i]; const file = await open(filename); - await copy(stdout, file); + await copy(file, stdout); } } diff --git a/std/archive/tar.ts b/std/archive/tar.ts index cafef8723a4c5f..6bc2b92d0bfe27 100644 --- a/std/archive/tar.ts +++ b/std/archive/tar.ts @@ -470,7 +470,7 @@ export class Untar { while (rest > 0) { await this.reader.readFull(this.block); const arr = rest < recordSize ? this.block.subarray(0, rest) : this.block; - await Deno.copy(writer, new Deno.Buffer(arr)); + await Deno.copy(new Deno.Buffer(arr), writer); rest -= recordSize; } diff --git a/std/archive/tar_test.ts b/std/archive/tar_test.ts index ae3ee8138c0b58..cd22addcc80b43 100644 --- a/std/archive/tar_test.ts +++ b/std/archive/tar_test.ts @@ -30,8 +30,8 @@ Deno.test(async function createTarArchive(): Promise { await tar.append("dir/tar.ts", { filePath }); // write tar data to a buffer - const writer = new Deno.Buffer(), - wrote = await Deno.copy(writer, tar.getReader()); + const writer = new Deno.Buffer(); + const wrote = await Deno.copy(tar.getReader(), writer); /** * 3072 = 512 (header) + 512 (content) + 512 (header) + 512 (content) diff --git a/std/examples/cat.ts b/std/examples/cat.ts index 7b7af3145cd89e..cf9a7cb1d5f54a 100644 --- a/std/examples/cat.ts +++ b/std/examples/cat.ts @@ -2,6 +2,6 @@ const filenames = Deno.args; for (const filename of filenames) { const file = await Deno.open(filename); - await Deno.copy(Deno.stdout, file); + await Deno.copy(file, Deno.stdout); file.close(); } diff --git a/std/examples/curl.ts b/std/examples/curl.ts index 44ee66125e0b67..a3e8b73db37ab3 100644 --- a/std/examples/curl.ts +++ b/std/examples/curl.ts @@ -3,7 +3,7 @@ const url_ = Deno.args[0]; const res = await fetch(url_); // TODO(ry) Re-enable streaming in this example. -// Originally we did: await Deno.copy(Deno.stdout, res.body); +// Originally we did: await Deno.copy(res.body, Deno.stdout); // But maybe more JS-y would be: res.pipeTo(Deno.stdout); const body = new Uint8Array(await res.arrayBuffer()); diff --git a/std/http/io.ts b/std/http/io.ts index 37285fdbf87592..2c2fea48a998db 100644 --- a/std/http/io.ts +++ b/std/http/io.ts @@ -273,7 +273,7 @@ export async function writeResponse( const contentLength = headers.get("content-length"); assert(contentLength != null); const bodyLength = parseInt(contentLength); - const n = await Deno.copy(writer, r.body); + const n = await Deno.copy(r.body, writer); assert(n === bodyLength); } else { await writeChunkedBody(writer, r.body); diff --git a/std/io/readers_test.ts b/std/io/readers_test.ts index 22982c92499f24..041fd78a87fa22 100644 --- a/std/io/readers_test.ts +++ b/std/io/readers_test.ts @@ -33,6 +33,6 @@ test(async function ioMultiReader(): Promise { const n = await copyN(w, r, 4); assertEquals(n, 4); assertEquals(w.toString(), "abcd"); - await copy(w, r); + await copy(r, w); assertEquals(w.toString(), "abcdef"); }); diff --git a/std/io/writers_test.ts b/std/io/writers_test.ts index 916ad043e9c3aa..96dc044ae95008 100644 --- a/std/io/writers_test.ts +++ b/std/io/writers_test.ts @@ -9,6 +9,6 @@ test(async function ioStringWriter(): Promise { const r = new StringReader("0123456789"); await copyN(w, r, 4); assertEquals(w.toString(), "base0123"); - await copy(w, r); + await copy(r, w); assertEquals(w.toString(), "base0123456789"); }); diff --git a/std/mime/multipart.ts b/std/mime/multipart.ts index c46948226b3a19..7097522903f103 100644 --- a/std/mime/multipart.ts +++ b/std/mime/multipart.ts @@ -308,7 +308,7 @@ export class MultipartReader { } // file let formFile: FormFile | undefined; - const n = await copy(buf, p); + const n = await copy(p, buf); const contentType = p.headers.get("content-type"); assert(contentType != null, "content-type must be set"); if (n > maxMemory) { @@ -573,7 +573,7 @@ export class MultipartWriter { file: Reader ): Promise { const f = await this.createFormFile(field, filename); - await copy(f, file); + await copy(file, f); } private flush(): Promise { diff --git a/std/mime/multipart_test.ts b/std/mime/multipart_test.ts index 8b721a44144908..aa3b7524eba355 100644 --- a/std/mime/multipart_test.ts +++ b/std/mime/multipart_test.ts @@ -208,7 +208,7 @@ test({ assert(file.tempfile != null); const f = await open(file.tempfile); const w = new StringWriter(); - await copy(w, f); + await copy(f, w); const json = JSON.parse(w.toString()); assertEquals(json["compilerOptions"]["target"], "es2018"); f.close();