diff options
Diffstat (limited to 'js/buffer.ts')
-rw-r--r-- | js/buffer.ts | 49 |
1 files changed, 44 insertions, 5 deletions
diff --git a/js/buffer.ts b/js/buffer.ts index c5be762f5..551e8c4d4 100644 --- a/js/buffer.ts +++ b/js/buffer.ts @@ -5,7 +5,7 @@ // https://github.com/golang/go/blob/master/LICENSE //import * as io from "./io"; -import { Reader, Writer, ReadResult } from "./io"; +import { Reader, Writer, ReadResult, SyncReader, SyncWriter } from "./io"; import { assert } from "./util"; import { TextDecoder } from "./text_encoding"; import { DenoError, ErrorKind } from "./errors"; @@ -32,7 +32,7 @@ function copyBytes(dst: Uint8Array, src: Uint8Array, off = 0): number { /** A Buffer is a variable-sized buffer of bytes with read() and write() * methods. Based on https://golang.org/pkg/bytes/#Buffer */ -export class Buffer implements Reader, Writer { +export class Buffer implements Reader, SyncReader, Writer, SyncWriter { private buf: Uint8Array; // contents are the bytes buf[off : len(buf)] private off = 0; // read at buf[off], write at buf[buf.byteLength] @@ -126,11 +126,11 @@ export class Buffer implements Reader, Writer { this.buf = new Uint8Array(this.buf.buffer, 0, len); } - /** read() reads the next len(p) bytes from the buffer or until the buffer + /** readSync() reads the next len(p) bytes from the buffer or until the buffer * 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: Uint8Array): Promise<ReadResult> { + readSync(p: Uint8Array): ReadResult { if (this.empty()) { // Buffer is empty, reset to recover space. this.reset(); @@ -145,11 +145,21 @@ export class Buffer implements Reader, Writer { return { nread, eof: false }; } - async write(p: Uint8Array): Promise<number> { + async read(p: Uint8Array): Promise<ReadResult> { + const rr = this.readSync(p); + return Promise.resolve(rr); + } + + writeSync(p: Uint8Array): number { const m = this._grow(p.byteLength); return copyBytes(this.buf, p, m); } + async write(p: Uint8Array): Promise<number> { + const n = this.writeSync(p); + return Promise.resolve(n); + } + /** _grow() grows the buffer to guarantee space for n more bytes. * It returns the index where bytes should be written. * If the buffer can't grow it will throw with ErrTooLarge. @@ -226,6 +236,27 @@ export class Buffer implements Reader, Writer { } } } + + /** Sync version of `readFrom` + */ + readFromSync(r: SyncReader): number { + let n = 0; + while (true) { + try { + const i = this._grow(MIN_READ); + this._reslice(i); + const fub = new Uint8Array(this.buf.buffer, i); + const { nread, eof } = r.readSync(fub); + this._reslice(i + nread); + n += nread; + if (eof) { + return n; + } + } catch (e) { + return n; + } + } + } } /** Read `r` until EOF and return the content as `Uint8Array`. @@ -235,3 +266,11 @@ export async function readAll(r: Reader): Promise<Uint8Array> { await buf.readFrom(r); return buf.bytes(); } + +/** Read synchronously `r` until EOF and return the content as `Uint8Array`. + */ +export function readAllSync(r: SyncReader): Uint8Array { + const buf = new Buffer(); + buf.readFromSync(r); + return buf.bytes(); +} |