From e931c53a234a55f29169415cac016e4d792f7f97 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Mon, 12 Nov 2018 14:09:50 -0500 Subject: [PATCH] Use Uint8Array instead of ArrayBufferView Original: https://github.com/denoland/deno_std/commit/0c8ad6eb1af05b259cc7c622e87e27b402886c96 --- buffer.ts | 10 ++-------- bufio_test.ts | 2 +- http_test.ts | 3 ++- iotest.ts | 6 +++--- 4 files changed, 8 insertions(+), 13 deletions(-) diff --git a/buffer.ts b/buffer.ts index ba2715cef7..d177bc18dc 100644 --- a/buffer.ts +++ b/buffer.ts @@ -120,10 +120,7 @@ export class Buffer implements Reader, Writer { * is drained. The return value n is the number of bytes read. If the * buffer has no data to return, eof in the response will be true. */ - async read(p: ArrayBufferView): Promise { - if (!(p instanceof Uint8Array)) { - throw Error("Only Uint8Array supported"); - } + async read(p: Uint8Array): Promise { if (this.empty()) { // Buffer is empty, reset to recover space. this.reset(); @@ -139,11 +136,8 @@ export class Buffer implements Reader, Writer { return { nread, eof: false }; } - async write(p: ArrayBufferView): Promise { + async write(p: Uint8Array): Promise { const m = this._grow(p.byteLength); - if (!(p instanceof Uint8Array)) { - throw Error("Only Uint8Array supported"); - } return copyBytes(this.buf, p, m); } diff --git a/bufio_test.ts b/bufio_test.ts index f3430a755b..5f32500a7a 100644 --- a/bufio_test.ts +++ b/bufio_test.ts @@ -159,7 +159,7 @@ const testOutput = encoder.encode("0123456789abcdefghijklmnopqrstuvwxy"); class TestReader implements Reader { constructor(private data: Uint8Array, private stride: number) {} - async read(buf: ArrayBufferView): Promise { + async read(buf: Uint8Array): Promise { let nread = this.stride; if (nread > this.data.byteLength) { nread = this.data.byteLength; diff --git a/http_test.ts b/http_test.ts index b0007a8927..e6cb87f01e 100644 --- a/http_test.ts +++ b/http_test.ts @@ -5,7 +5,8 @@ const addr = "0.0.0.0:8000"; const s = serve(addr); console.log(`listening on http://${addr}/`); -const body = new TextEncoder().encode("Hello World\n"); +const body = (new TextEncoder()).encode("Hello World\n"); + async function main() { for await (const req of s) { diff --git a/iotest.ts b/iotest.ts index e2498e1557..e3a42f58a2 100644 --- a/iotest.ts +++ b/iotest.ts @@ -11,7 +11,7 @@ import { Reader, ReadResult } from "deno"; export class OneByteReader implements Reader { constructor(readonly r: Reader) {} - async read(p: ArrayBufferView): Promise { + async read(p: Uint8Array): Promise { if (p.byteLength === 0) { return { nread: 0, eof: false }; } @@ -28,7 +28,7 @@ export class OneByteReader implements Reader { export class HalfReader implements Reader { constructor(readonly r: Reader) {} - async read(p: ArrayBufferView): Promise { + async read(p: Uint8Array): Promise { if (!(p instanceof Uint8Array)) { throw Error("expected Uint8Array"); } @@ -51,7 +51,7 @@ export class TimeoutReader implements Reader { count = 0; constructor(readonly r: Reader) {} - async read(p: ArrayBufferView): Promise { + async read(p: Uint8Array): Promise { this.count++; if (this.count === 2) { throw new ErrTimeout();