1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-21 21:50:00 -05:00

Add deno.readAll() (#1234)

This commit is contained in:
DanSnow 2018-12-01 01:58:31 +08:00 committed by Ryan Dahl
parent 122ccce89a
commit e749b37b7c
3 changed files with 21 additions and 3 deletions

View file

@ -221,3 +221,11 @@ export class Buffer implements Reader, Writer {
}
}
}
/** Read `r` until EOF and return the content as `Uint8Array`.
*/
export async function readAll(r: Reader): Promise<Uint8Array> {
const buf = new Buffer();
await buf.readFrom(r);
return buf.bytes();
}

View file

@ -1,8 +1,8 @@
import { Buffer, readAll } from "deno";
// This code has been ported almost directly from Go's src/bytes/buffer_test.go
// Copyright 2009 The Go Authors. All rights reserved. BSD license.
// https://github.com/golang/go/blob/master/LICENSE
import { test, assert, assertEqual } from "./test_util.ts";
import { Buffer } from "deno";
import { assert, assertEqual, test } from "./test_util.ts";
// N controls how many iterations of certain checks are performed.
const N = 100;
@ -193,3 +193,13 @@ test(async function bufferTestGrow() {
}
}
});
test(async function testReadAll() {
init();
const reader = new Buffer(testBytes.buffer as ArrayBuffer);
const actualBytes = await readAll(reader);
assertEqual(testBytes.byteLength, actualBytes.byteLength);
for (let i = 0; i < testBytes.length; ++i) {
assertEqual(testBytes[i], actualBytes[i]);
}
});

View file

@ -20,7 +20,7 @@ export {
ReadWriteCloser,
ReadWriteSeeker
} from "./io";
export { Buffer } from "./buffer";
export { Buffer, readAll } from "./buffer";
export { mkdirSync, mkdir } from "./mkdir";
export { makeTempDirSync, makeTempDir } from "./make_temp_dir";
export { chmodSync, chmod } from "./chmod";