1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-23 15:16:54 -05:00

Use Uint8Array instead of ArrayBufferView

Original: 0c8ad6eb1a
This commit is contained in:
Ryan Dahl 2018-11-12 14:09:50 -05:00
parent 92455a0b67
commit e931c53a23
4 changed files with 8 additions and 13 deletions

View file

@ -120,10 +120,7 @@ export class Buffer implements Reader, Writer {
* is drained. The return value n is the number of bytes read. If the * 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. * buffer has no data to return, eof in the response will be true.
*/ */
async read(p: ArrayBufferView): Promise<ReadResult> { async read(p: Uint8Array): Promise<ReadResult> {
if (!(p instanceof Uint8Array)) {
throw Error("Only Uint8Array supported");
}
if (this.empty()) { if (this.empty()) {
// Buffer is empty, reset to recover space. // Buffer is empty, reset to recover space.
this.reset(); this.reset();
@ -139,11 +136,8 @@ export class Buffer implements Reader, Writer {
return { nread, eof: false }; return { nread, eof: false };
} }
async write(p: ArrayBufferView): Promise<number> { async write(p: Uint8Array): Promise<number> {
const m = this._grow(p.byteLength); const m = this._grow(p.byteLength);
if (!(p instanceof Uint8Array)) {
throw Error("Only Uint8Array supported");
}
return copyBytes(this.buf, p, m); return copyBytes(this.buf, p, m);
} }

View file

@ -159,7 +159,7 @@ const testOutput = encoder.encode("0123456789abcdefghijklmnopqrstuvwxy");
class TestReader implements Reader { class TestReader implements Reader {
constructor(private data: Uint8Array, private stride: number) {} constructor(private data: Uint8Array, private stride: number) {}
async read(buf: ArrayBufferView): Promise<ReadResult> { async read(buf: Uint8Array): Promise<ReadResult> {
let nread = this.stride; let nread = this.stride;
if (nread > this.data.byteLength) { if (nread > this.data.byteLength) {
nread = this.data.byteLength; nread = this.data.byteLength;

View file

@ -5,7 +5,8 @@ const addr = "0.0.0.0:8000";
const s = serve(addr); const s = serve(addr);
console.log(`listening on http://${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() { async function main() {
for await (const req of s) { for await (const req of s) {

View file

@ -11,7 +11,7 @@ import { Reader, ReadResult } from "deno";
export class OneByteReader implements Reader { export class OneByteReader implements Reader {
constructor(readonly r: Reader) {} constructor(readonly r: Reader) {}
async read(p: ArrayBufferView): Promise<ReadResult> { async read(p: Uint8Array): Promise<ReadResult> {
if (p.byteLength === 0) { if (p.byteLength === 0) {
return { nread: 0, eof: false }; return { nread: 0, eof: false };
} }
@ -28,7 +28,7 @@ export class OneByteReader implements Reader {
export class HalfReader implements Reader { export class HalfReader implements Reader {
constructor(readonly r: Reader) {} constructor(readonly r: Reader) {}
async read(p: ArrayBufferView): Promise<ReadResult> { async read(p: Uint8Array): Promise<ReadResult> {
if (!(p instanceof Uint8Array)) { if (!(p instanceof Uint8Array)) {
throw Error("expected Uint8Array"); throw Error("expected Uint8Array");
} }
@ -51,7 +51,7 @@ export class TimeoutReader implements Reader {
count = 0; count = 0;
constructor(readonly r: Reader) {} constructor(readonly r: Reader) {}
async read(p: ArrayBufferView): Promise<ReadResult> { async read(p: Uint8Array): Promise<ReadResult> {
this.count++; this.count++;
if (this.count === 2) { if (this.count === 2) {
throw new ErrTimeout(); throw new ErrTimeout();