0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-03-03 17:34:47 -05:00

Reader/Writer should use Uint8Array not ArrayBufferView

Because many Reader/Writer implementations (e.g. bufio) assume their
able to use subarray() with byte indexes and often ask for byte values,
it makes sense to simply restrict all implementations to Uint8Array.
This commit is contained in:
Ryan Dahl 2018-11-08 01:18:21 -05:00
parent 98e6366cb5
commit 1f2c92c7c8
4 changed files with 12 additions and 23 deletions

View file

@ -127,10 +127,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();
@ -146,11 +143,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

@ -9,11 +9,11 @@ import * as flatbuffers from "./flatbuffers";
export class File implements Reader, Writer, Closer { export class File implements Reader, Writer, Closer {
constructor(readonly rid: number) {} constructor(readonly rid: number) {}
write(p: ArrayBufferView): Promise<number> { write(p: Uint8Array): Promise<number> {
return write(this.rid, p); return write(this.rid, p);
} }
read(p: ArrayBufferView): Promise<ReadResult> { read(p: Uint8Array): Promise<ReadResult> {
return read(this.rid, p); return read(this.rid, p);
} }
@ -68,10 +68,7 @@ export async function open(
* *
* Resolves with the `ReadResult` for the operation. * Resolves with the `ReadResult` for the operation.
*/ */
export async function read( export async function read(rid: number, p: Uint8Array): Promise<ReadResult> {
rid: number,
p: ArrayBufferView
): Promise<ReadResult> {
const builder = flatbuffers.createBuilder(); const builder = flatbuffers.createBuilder();
msg.Read.startRead(builder); msg.Read.startRead(builder);
msg.Read.addRid(builder, rid); msg.Read.addRid(builder, rid);
@ -88,7 +85,7 @@ export async function read(
* *
* Resolves with the number of bytes written. * Resolves with the number of bytes written.
*/ */
export async function write(rid: number, p: ArrayBufferView): Promise<number> { export async function write(rid: number, p: Uint8Array): Promise<number> {
const builder = flatbuffers.createBuilder(); const builder = flatbuffers.createBuilder();
msg.Write.startWrite(builder); msg.Write.startWrite(builder);
msg.Write.addRid(builder, rid); msg.Write.addRid(builder, rid);

View file

@ -37,7 +37,7 @@ export interface Reader {
* *
* Implementations must not retain `p`. * Implementations must not retain `p`.
*/ */
read(p: ArrayBufferView): Promise<ReadResult>; read(p: Uint8Array): Promise<ReadResult>;
} }
// Writer is the interface that wraps the basic write() method. // Writer is the interface that wraps the basic write() method.
@ -51,7 +51,7 @@ export interface Writer {
* *
* Implementations must not retain `p`. * Implementations must not retain `p`.
*/ */
write(p: ArrayBufferView): Promise<number>; write(p: Uint8Array): Promise<number>;
} }
// https://golang.org/pkg/io/#Closer // https://golang.org/pkg/io/#Closer
@ -123,9 +123,7 @@ export async function copy(dst: Writer, src: Reader): Promise<number> {
* console.log(chunk) * console.log(chunk)
* } * }
*/ */
export function toAsyncIterator( export function toAsyncIterator(r: Reader): AsyncIterableIterator<Uint8Array> {
r: Reader
): AsyncIterableIterator<ArrayBufferView> {
const b = new Uint8Array(1024); const b = new Uint8Array(1024);
return { return {
@ -133,7 +131,7 @@ export function toAsyncIterator(
return this; return this;
}, },
async next(): Promise<IteratorResult<ArrayBufferView>> { async next(): Promise<IteratorResult<Uint8Array>> {
const result = await r.read(b); const result = await r.read(b);
return { return {
value: b.subarray(0, result.nread), value: b.subarray(0, result.nread),

View file

@ -74,11 +74,11 @@ class ConnImpl implements Conn {
readonly localAddr: string readonly localAddr: string
) {} ) {}
write(p: ArrayBufferView): Promise<number> { write(p: Uint8Array): Promise<number> {
return write(this.rid, p); return write(this.rid, p);
} }
read(p: ArrayBufferView): Promise<ReadResult> { read(p: Uint8Array): Promise<ReadResult> {
return read(this.rid, p); return read(this.rid, p);
} }