2020-09-21 08:26:41 -04:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2018-11-08 12:26:20 -05:00
|
|
|
// Based on https://github.com/golang/go/blob/891682/src/bufio/bufio.go
|
2018-11-07 14:17:36 -05:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2019-03-07 19:25:16 -05:00
|
|
|
type Reader = Deno.Reader;
|
|
|
|
type Writer = Deno.Writer;
|
2020-04-28 07:23:30 -04:00
|
|
|
type WriterSync = Deno.WriterSync;
|
2020-06-25 06:40:51 -04:00
|
|
|
import { copyBytes } from "../bytes/mod.ts";
|
2020-06-07 09:20:33 -04:00
|
|
|
import { assert } from "../_util/assert.ts";
|
2018-11-07 13:16:07 -05:00
|
|
|
|
|
|
|
const DEFAULT_BUF_SIZE = 4096;
|
|
|
|
const MIN_BUF_SIZE = 16;
|
|
|
|
const MAX_CONSECUTIVE_EMPTY_READS = 100;
|
2020-06-25 06:40:51 -04:00
|
|
|
const CR = "\r".charCodeAt(0);
|
|
|
|
const LF = "\n".charCodeAt(0);
|
2018-11-07 13:16:07 -05:00
|
|
|
|
2019-05-23 22:04:06 -04:00
|
|
|
export class BufferFullError extends Error {
|
|
|
|
name = "BufferFullError";
|
|
|
|
constructor(public partial: Uint8Array) {
|
|
|
|
super("Buffer full");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-13 21:40:13 -04:00
|
|
|
export class PartialReadError extends Deno.errors.UnexpectedEof {
|
|
|
|
name = "PartialReadError";
|
2019-11-13 13:42:34 -05:00
|
|
|
partial?: Uint8Array;
|
2019-05-23 22:04:06 -04:00
|
|
|
constructor() {
|
2020-03-13 21:40:13 -04:00
|
|
|
super("Encountered UnexpectedEof, data only partially read");
|
2019-05-23 22:04:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Result type returned by of BufReader.readLine(). */
|
|
|
|
export interface ReadLineResult {
|
|
|
|
line: Uint8Array;
|
|
|
|
more: boolean;
|
|
|
|
}
|
2018-11-07 14:17:36 -05:00
|
|
|
|
2018-11-07 21:01:32 -05:00
|
|
|
/** BufReader implements buffering for a Reader object. */
|
|
|
|
export class BufReader implements Reader {
|
2019-05-30 08:59:30 -04:00
|
|
|
private buf!: Uint8Array;
|
|
|
|
private rd!: Reader; // Reader provided by caller.
|
2018-11-07 13:16:07 -05:00
|
|
|
private r = 0; // buf read position.
|
|
|
|
private w = 0; // buf write position.
|
2019-05-23 22:04:06 -04:00
|
|
|
private eof = false;
|
|
|
|
// private lastByte: number;
|
|
|
|
// private lastCharSize: number;
|
2018-11-07 13:16:07 -05:00
|
|
|
|
2019-05-14 15:19:12 -04:00
|
|
|
/** return new BufReader unless r is BufReader */
|
2019-09-29 10:11:18 -04:00
|
|
|
static create(r: Reader, size: number = DEFAULT_BUF_SIZE): BufReader {
|
2019-05-14 15:19:12 -04:00
|
|
|
return r instanceof BufReader ? r : new BufReader(r, size);
|
|
|
|
}
|
|
|
|
|
2019-09-29 10:11:18 -04:00
|
|
|
constructor(rd: Reader, size: number = DEFAULT_BUF_SIZE) {
|
2018-11-07 13:16:07 -05:00
|
|
|
if (size < MIN_BUF_SIZE) {
|
|
|
|
size = MIN_BUF_SIZE;
|
|
|
|
}
|
2018-11-07 14:17:36 -05:00
|
|
|
this._reset(new Uint8Array(size), rd);
|
2018-11-07 13:16:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Returns the size of the underlying buffer in bytes. */
|
2018-11-09 13:25:30 -05:00
|
|
|
size(): number {
|
2018-11-07 13:16:07 -05:00
|
|
|
return this.buf.byteLength;
|
|
|
|
}
|
|
|
|
|
2018-11-07 21:01:32 -05:00
|
|
|
buffered(): number {
|
|
|
|
return this.w - this.r;
|
|
|
|
}
|
|
|
|
|
2018-11-07 13:16:07 -05:00
|
|
|
// Reads a new chunk into the buffer.
|
2018-11-08 02:05:06 -05:00
|
|
|
private async _fill(): Promise<void> {
|
2018-11-07 13:16:07 -05:00
|
|
|
// Slide existing data to beginning.
|
|
|
|
if (this.r > 0) {
|
|
|
|
this.buf.copyWithin(0, this.r, this.w);
|
|
|
|
this.w -= this.r;
|
|
|
|
this.r = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.w >= this.buf.byteLength) {
|
|
|
|
throw Error("bufio: tried to fill full buffer");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read new data: try a limited number of times.
|
|
|
|
for (let i = MAX_CONSECUTIVE_EMPTY_READS; i > 0; i--) {
|
2019-07-07 15:20:41 -04:00
|
|
|
const rr = await this.rd.read(this.buf.subarray(this.w));
|
2020-04-28 12:40:43 -04:00
|
|
|
if (rr === null) {
|
2019-05-23 22:04:06 -04:00
|
|
|
this.eof = true;
|
2018-11-08 02:05:06 -05:00
|
|
|
return;
|
2018-11-07 13:16:07 -05:00
|
|
|
}
|
2019-07-07 15:20:41 -04:00
|
|
|
assert(rr >= 0, "negative read");
|
|
|
|
this.w += rr;
|
|
|
|
if (rr > 0) {
|
2018-11-08 02:05:06 -05:00
|
|
|
return;
|
2018-11-07 13:16:07 -05:00
|
|
|
}
|
|
|
|
}
|
2019-05-23 22:04:06 -04:00
|
|
|
|
|
|
|
throw new Error(
|
2020-07-14 15:24:17 -04:00
|
|
|
`No progress after ${MAX_CONSECUTIVE_EMPTY_READS} read() calls`,
|
2019-05-23 22:04:06 -04:00
|
|
|
);
|
2018-11-07 13:16:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Discards any buffered data, resets all state, and switches
|
|
|
|
* the buffered reader to read from r.
|
|
|
|
*/
|
2018-11-07 21:01:32 -05:00
|
|
|
reset(r: Reader): void {
|
2018-11-07 13:16:07 -05:00
|
|
|
this._reset(this.buf, r);
|
|
|
|
}
|
|
|
|
|
2018-11-07 21:01:32 -05:00
|
|
|
private _reset(buf: Uint8Array, rd: Reader): void {
|
2018-11-07 13:16:07 -05:00
|
|
|
this.buf = buf;
|
|
|
|
this.rd = rd;
|
2019-05-23 22:04:06 -04:00
|
|
|
this.eof = false;
|
|
|
|
// this.lastByte = -1;
|
|
|
|
// this.lastCharSize = -1;
|
2018-11-07 13:16:07 -05:00
|
|
|
}
|
|
|
|
|
2018-11-07 14:17:36 -05:00
|
|
|
/** reads data into p.
|
|
|
|
* It returns the number of bytes read into p.
|
|
|
|
* The bytes are taken from at most one Read on the underlying Reader,
|
|
|
|
* hence n may be less than len(p).
|
|
|
|
* To read exactly len(p) bytes, use io.ReadFull(b, p).
|
|
|
|
*/
|
2020-04-28 12:40:43 -04:00
|
|
|
async read(p: Uint8Array): Promise<number | null> {
|
|
|
|
let rr: number | null = p.byteLength;
|
2019-05-23 22:04:06 -04:00
|
|
|
if (p.byteLength === 0) return rr;
|
2018-11-07 14:17:36 -05:00
|
|
|
|
|
|
|
if (this.r === this.w) {
|
|
|
|
if (p.byteLength >= this.buf.byteLength) {
|
|
|
|
// Large read, empty buffer.
|
|
|
|
// Read directly into p to avoid copy.
|
2019-05-23 22:04:06 -04:00
|
|
|
const rr = await this.rd.read(p);
|
2020-04-28 12:40:43 -04:00
|
|
|
const nread = rr ?? 0;
|
2019-07-07 15:20:41 -04:00
|
|
|
assert(nread >= 0, "negative read");
|
2019-05-23 22:04:06 -04:00
|
|
|
// if (rr.nread > 0) {
|
|
|
|
// this.lastByte = p[rr.nread - 1];
|
|
|
|
// this.lastCharSize = -1;
|
|
|
|
// }
|
2018-11-07 14:17:36 -05:00
|
|
|
return rr;
|
|
|
|
}
|
2019-05-23 22:04:06 -04:00
|
|
|
|
2018-11-07 14:17:36 -05:00
|
|
|
// One read.
|
|
|
|
// Do not use this.fill, which will loop.
|
|
|
|
this.r = 0;
|
|
|
|
this.w = 0;
|
2019-05-23 22:04:06 -04:00
|
|
|
rr = await this.rd.read(this.buf);
|
2020-04-28 12:40:43 -04:00
|
|
|
if (rr === 0 || rr === null) return rr;
|
2019-07-07 15:20:41 -04:00
|
|
|
assert(rr >= 0, "negative read");
|
|
|
|
this.w += rr;
|
2018-11-07 14:17:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// copy as much as we can
|
2020-04-30 16:39:25 -04:00
|
|
|
const copied = copyBytes(this.buf.subarray(this.r, this.w), p, 0);
|
2019-07-07 15:20:41 -04:00
|
|
|
this.r += copied;
|
2019-05-23 22:04:06 -04:00
|
|
|
// this.lastByte = this.buf[this.r - 1];
|
|
|
|
// this.lastCharSize = -1;
|
2019-07-07 15:20:41 -04:00
|
|
|
return copied;
|
2018-11-07 13:16:07 -05:00
|
|
|
}
|
|
|
|
|
2019-05-23 22:04:06 -04:00
|
|
|
/** reads exactly `p.length` bytes into `p`.
|
|
|
|
*
|
|
|
|
* If successful, `p` is returned.
|
|
|
|
*
|
|
|
|
* If the end of the underlying stream has been reached, and there are no more
|
2020-04-28 12:40:43 -04:00
|
|
|
* bytes available in the buffer, `readFull()` returns `null` instead.
|
2019-05-23 22:04:06 -04:00
|
|
|
*
|
|
|
|
* An error is thrown if some bytes could be read, but not enough to fill `p`
|
|
|
|
* entirely before the underlying stream reported an error or EOF. Any error
|
|
|
|
* thrown will have a `partial` property that indicates the slice of the
|
|
|
|
* buffer that has been successfully filled with data.
|
|
|
|
*
|
2018-12-17 22:57:45 -05:00
|
|
|
* Ported from https://golang.org/pkg/io/#ReadFull
|
|
|
|
*/
|
2020-04-28 12:40:43 -04:00
|
|
|
async readFull(p: Uint8Array): Promise<Uint8Array | null> {
|
2019-05-23 22:04:06 -04:00
|
|
|
let bytesRead = 0;
|
|
|
|
while (bytesRead < p.length) {
|
|
|
|
try {
|
|
|
|
const rr = await this.read(p.subarray(bytesRead));
|
2020-04-28 12:40:43 -04:00
|
|
|
if (rr === null) {
|
2019-05-23 22:04:06 -04:00
|
|
|
if (bytesRead === 0) {
|
2020-04-28 12:40:43 -04:00
|
|
|
return null;
|
2019-05-23 22:04:06 -04:00
|
|
|
} else {
|
2020-03-13 21:40:13 -04:00
|
|
|
throw new PartialReadError();
|
2019-05-23 22:04:06 -04:00
|
|
|
}
|
|
|
|
}
|
2019-07-07 15:20:41 -04:00
|
|
|
bytesRead += rr;
|
2019-05-23 22:04:06 -04:00
|
|
|
} catch (err) {
|
|
|
|
err.partial = p.subarray(0, bytesRead);
|
|
|
|
throw err;
|
|
|
|
}
|
2018-12-17 22:57:45 -05:00
|
|
|
}
|
2019-05-23 22:04:06 -04:00
|
|
|
return p;
|
2018-12-17 22:57:45 -05:00
|
|
|
}
|
|
|
|
|
2020-04-28 12:40:43 -04:00
|
|
|
/** Returns the next byte [0, 255] or `null`. */
|
|
|
|
async readByte(): Promise<number | null> {
|
2018-11-07 13:16:07 -05:00
|
|
|
while (this.r === this.w) {
|
2020-04-28 12:40:43 -04:00
|
|
|
if (this.eof) return null;
|
2018-11-08 02:05:06 -05:00
|
|
|
await this._fill(); // buffer is empty.
|
2018-11-07 13:16:07 -05:00
|
|
|
}
|
|
|
|
const c = this.buf[this.r];
|
|
|
|
this.r++;
|
2019-05-23 22:04:06 -04:00
|
|
|
// this.lastByte = c;
|
2018-11-07 13:16:07 -05:00
|
|
|
return c;
|
|
|
|
}
|
2018-11-07 20:28:01 -05:00
|
|
|
|
|
|
|
/** readString() reads until the first occurrence of delim in the input,
|
|
|
|
* returning a string containing the data up to and including the delimiter.
|
|
|
|
* If ReadString encounters an error before finding a delimiter,
|
2019-06-19 00:22:01 -04:00
|
|
|
* it returns the data read before the error and the error itself
|
2020-04-28 12:40:43 -04:00
|
|
|
* (often `null`).
|
2019-06-19 00:22:01 -04:00
|
|
|
* ReadString returns err != nil if and only if the returned data does not end
|
2019-10-28 13:28:29 -04:00
|
|
|
* in delim.
|
2018-11-07 20:28:01 -05:00
|
|
|
* For simple uses, a Scanner may be more convenient.
|
|
|
|
*/
|
2020-04-28 12:40:43 -04:00
|
|
|
async readString(delim: string): Promise<string | null> {
|
2020-03-28 13:03:49 -04:00
|
|
|
if (delim.length !== 1) {
|
2019-09-22 13:04:46 -04:00
|
|
|
throw new Error("Delimiter should be a single character");
|
2020-03-28 13:03:49 -04:00
|
|
|
}
|
2019-09-22 13:04:46 -04:00
|
|
|
const buffer = await this.readSlice(delim.charCodeAt(0));
|
2020-04-28 12:40:43 -04:00
|
|
|
if (buffer === null) return null;
|
2019-10-28 13:28:29 -04:00
|
|
|
return new TextDecoder().decode(buffer);
|
2018-11-07 20:28:01 -05:00
|
|
|
}
|
2018-11-07 23:19:08 -05:00
|
|
|
|
2019-05-23 22:04:06 -04:00
|
|
|
/** `readLine()` is a low-level line-reading primitive. Most callers should
|
|
|
|
* use `readString('\n')` instead or use a Scanner.
|
2018-11-07 23:19:08 -05:00
|
|
|
*
|
2019-05-23 22:04:06 -04:00
|
|
|
* `readLine()` tries to return a single line, not including the end-of-line
|
|
|
|
* bytes. If the line was too long for the buffer then `more` is set and the
|
2018-11-07 23:19:08 -05:00
|
|
|
* beginning of the line is returned. The rest of the line will be returned
|
2019-05-23 22:04:06 -04:00
|
|
|
* from future calls. `more` will be false when returning the last fragment
|
2018-11-07 23:19:08 -05:00
|
|
|
* of the line. The returned buffer is only valid until the next call to
|
2019-05-23 22:04:06 -04:00
|
|
|
* `readLine()`.
|
2018-11-07 23:19:08 -05:00
|
|
|
*
|
2019-05-23 22:04:06 -04:00
|
|
|
* The text returned from ReadLine does not include the line end ("\r\n" or
|
|
|
|
* "\n").
|
|
|
|
*
|
|
|
|
* When the end of the underlying stream is reached, the final bytes in the
|
|
|
|
* stream are returned. No indication or error is given if the input ends
|
|
|
|
* without a final line end. When there are no more trailing bytes to read,
|
2020-04-28 12:40:43 -04:00
|
|
|
* `readLine()` returns `null`.
|
2019-05-23 22:04:06 -04:00
|
|
|
*
|
|
|
|
* Calling `unreadByte()` after `readLine()` will always unread the last byte
|
|
|
|
* read (possibly a character belonging to the line end) even if that byte is
|
|
|
|
* not part of the line returned by `readLine()`.
|
2018-11-07 23:19:08 -05:00
|
|
|
*/
|
2020-04-28 12:40:43 -04:00
|
|
|
async readLine(): Promise<ReadLineResult | null> {
|
|
|
|
let line: Uint8Array | null;
|
2019-05-23 22:04:06 -04:00
|
|
|
|
|
|
|
try {
|
|
|
|
line = await this.readSlice(LF);
|
|
|
|
} catch (err) {
|
|
|
|
let { partial } = err;
|
|
|
|
assert(
|
|
|
|
partial instanceof Uint8Array,
|
2020-07-14 15:24:17 -04:00
|
|
|
"bufio: caught error from `readSlice()` without `partial` property",
|
2019-05-23 22:04:06 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
// Don't throw if `readSlice()` failed with `BufferFullError`, instead we
|
|
|
|
// just return whatever is available and set the `more` flag.
|
|
|
|
if (!(err instanceof BufferFullError)) {
|
|
|
|
throw err;
|
|
|
|
}
|
2018-11-07 23:19:08 -05:00
|
|
|
|
|
|
|
// Handle the case where "\r\n" straddles the buffer.
|
2019-05-23 22:04:06 -04:00
|
|
|
if (
|
|
|
|
!this.eof &&
|
|
|
|
partial.byteLength > 0 &&
|
|
|
|
partial[partial.byteLength - 1] === CR
|
|
|
|
) {
|
2018-11-07 23:19:08 -05:00
|
|
|
// Put the '\r' back on buf and drop it from line.
|
|
|
|
// Let the next call to ReadLine check for "\r\n".
|
|
|
|
assert(this.r > 0, "bufio: tried to rewind past start of buffer");
|
|
|
|
this.r--;
|
2019-05-23 22:04:06 -04:00
|
|
|
partial = partial.subarray(0, partial.byteLength - 1);
|
2018-11-07 23:19:08 -05:00
|
|
|
}
|
2019-05-23 22:04:06 -04:00
|
|
|
|
|
|
|
return { line: partial, more: !this.eof };
|
|
|
|
}
|
|
|
|
|
2020-04-28 12:40:43 -04:00
|
|
|
if (line === null) {
|
|
|
|
return null;
|
2018-11-07 23:19:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (line.byteLength === 0) {
|
2019-05-23 22:04:06 -04:00
|
|
|
return { line, more: false };
|
2018-11-07 23:19:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (line[line.byteLength - 1] == LF) {
|
|
|
|
let drop = 1;
|
|
|
|
if (line.byteLength > 1 && line[line.byteLength - 2] === CR) {
|
|
|
|
drop = 2;
|
|
|
|
}
|
|
|
|
line = line.subarray(0, line.byteLength - drop);
|
|
|
|
}
|
2019-05-23 22:04:06 -04:00
|
|
|
return { line, more: false };
|
2018-11-07 23:19:08 -05:00
|
|
|
}
|
|
|
|
|
2019-05-23 22:04:06 -04:00
|
|
|
/** `readSlice()` reads until the first occurrence of `delim` in the input,
|
2018-11-07 23:19:08 -05:00
|
|
|
* returning a slice pointing at the bytes in the buffer. The bytes stop
|
2019-05-23 22:04:06 -04:00
|
|
|
* being valid at the next read.
|
|
|
|
*
|
|
|
|
* If `readSlice()` encounters an error before finding a delimiter, or the
|
|
|
|
* buffer fills without finding a delimiter, it throws an error with a
|
|
|
|
* `partial` property that contains the entire buffer.
|
|
|
|
*
|
|
|
|
* If `readSlice()` encounters the end of the underlying stream and there are
|
|
|
|
* any bytes left in the buffer, the rest of the buffer is returned. In other
|
|
|
|
* words, EOF is always treated as a delimiter. Once the buffer is empty,
|
2020-04-28 12:40:43 -04:00
|
|
|
* it returns `null`.
|
2019-05-23 22:04:06 -04:00
|
|
|
*
|
|
|
|
* Because the data returned from `readSlice()` will be overwritten by the
|
|
|
|
* next I/O operation, most clients should use `readString()` instead.
|
2018-11-07 23:19:08 -05:00
|
|
|
*/
|
2020-04-28 12:40:43 -04:00
|
|
|
async readSlice(delim: number): Promise<Uint8Array | null> {
|
2018-11-07 23:19:08 -05:00
|
|
|
let s = 0; // search start index
|
2020-02-19 15:36:18 -05:00
|
|
|
let slice: Uint8Array | undefined;
|
2019-05-23 22:04:06 -04:00
|
|
|
|
2018-11-07 23:19:08 -05:00
|
|
|
while (true) {
|
|
|
|
// Search buffer.
|
|
|
|
let i = this.buf.subarray(this.r + s, this.w).indexOf(delim);
|
|
|
|
if (i >= 0) {
|
|
|
|
i += s;
|
2019-05-23 22:04:06 -04:00
|
|
|
slice = this.buf.subarray(this.r, this.r + i + 1);
|
2018-11-07 23:19:08 -05:00
|
|
|
this.r += i + 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-05-23 22:04:06 -04:00
|
|
|
// EOF?
|
|
|
|
if (this.eof) {
|
|
|
|
if (this.r === this.w) {
|
2020-04-28 12:40:43 -04:00
|
|
|
return null;
|
2019-05-23 22:04:06 -04:00
|
|
|
}
|
|
|
|
slice = this.buf.subarray(this.r, this.w);
|
2018-11-07 23:19:08 -05:00
|
|
|
this.r = this.w;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Buffer full?
|
|
|
|
if (this.buffered() >= this.buf.byteLength) {
|
|
|
|
this.r = this.w;
|
2020-05-20 10:34:20 -04:00
|
|
|
// #4521 The internal buffer should not be reused across reads because it causes corruption of data.
|
|
|
|
const oldbuf = this.buf;
|
|
|
|
const newbuf = this.buf.slice(0);
|
|
|
|
this.buf = newbuf;
|
|
|
|
throw new BufferFullError(oldbuf);
|
2018-11-07 23:19:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
s = this.w - this.r; // do not rescan area we scanned before
|
|
|
|
|
2019-05-23 22:04:06 -04:00
|
|
|
// Buffer is not full.
|
|
|
|
try {
|
|
|
|
await this._fill();
|
|
|
|
} catch (err) {
|
2020-02-08 15:15:59 -05:00
|
|
|
err.partial = slice;
|
2019-05-23 22:04:06 -04:00
|
|
|
throw err;
|
|
|
|
}
|
2018-11-07 23:19:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Handle last byte, if any.
|
2019-05-23 22:04:06 -04:00
|
|
|
// const i = slice.byteLength - 1;
|
|
|
|
// if (i >= 0) {
|
|
|
|
// this.lastByte = slice[i];
|
|
|
|
// this.lastCharSize = -1
|
|
|
|
// }
|
2018-11-07 23:19:08 -05:00
|
|
|
|
2019-05-23 22:04:06 -04:00
|
|
|
return slice;
|
2018-11-07 23:19:08 -05:00
|
|
|
}
|
2018-11-08 04:01:20 -05:00
|
|
|
|
2019-05-23 22:04:06 -04:00
|
|
|
/** `peek()` returns the next `n` bytes without advancing the reader. The
|
|
|
|
* bytes stop being valid at the next read call.
|
|
|
|
*
|
|
|
|
* When the end of the underlying stream is reached, but there are unread
|
|
|
|
* bytes left in the buffer, those bytes are returned. If there are no bytes
|
2020-04-28 12:40:43 -04:00
|
|
|
* left in the buffer, it returns `null`.
|
2019-05-23 22:04:06 -04:00
|
|
|
*
|
|
|
|
* If an error is encountered before `n` bytes are available, `peek()` throws
|
|
|
|
* an error with the `partial` property set to a slice of the buffer that
|
|
|
|
* contains the bytes that were available before the error occurred.
|
2018-11-08 04:01:20 -05:00
|
|
|
*/
|
2020-04-28 12:40:43 -04:00
|
|
|
async peek(n: number): Promise<Uint8Array | null> {
|
2018-11-08 04:01:20 -05:00
|
|
|
if (n < 0) {
|
|
|
|
throw Error("negative count");
|
|
|
|
}
|
|
|
|
|
2019-05-23 22:04:06 -04:00
|
|
|
let avail = this.w - this.r;
|
|
|
|
while (avail < n && avail < this.buf.byteLength && !this.eof) {
|
|
|
|
try {
|
|
|
|
await this._fill();
|
|
|
|
} catch (err) {
|
|
|
|
err.partial = this.buf.subarray(this.r, this.w);
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
avail = this.w - this.r;
|
2018-11-08 04:01:20 -05:00
|
|
|
}
|
|
|
|
|
2019-05-23 22:04:06 -04:00
|
|
|
if (avail === 0 && this.eof) {
|
2020-04-28 12:40:43 -04:00
|
|
|
return null;
|
2019-05-23 22:04:06 -04:00
|
|
|
} else if (avail < n && this.eof) {
|
|
|
|
return this.buf.subarray(this.r, this.r + avail);
|
|
|
|
} else if (avail < n) {
|
|
|
|
throw new BufferFullError(this.buf.subarray(this.r, this.w));
|
2018-11-08 04:01:20 -05:00
|
|
|
}
|
|
|
|
|
2019-05-23 22:04:06 -04:00
|
|
|
return this.buf.subarray(this.r, this.r + n);
|
2018-11-08 04:01:20 -05:00
|
|
|
}
|
2018-11-07 13:16:07 -05:00
|
|
|
}
|
2018-11-08 18:15:26 -05:00
|
|
|
|
2020-04-17 16:12:14 -04:00
|
|
|
abstract class AbstractBufBase {
|
|
|
|
buf!: Uint8Array;
|
|
|
|
usedBufferBytes = 0;
|
|
|
|
err: Error | null = null;
|
|
|
|
|
|
|
|
/** Size returns the size of the underlying buffer in bytes. */
|
|
|
|
size(): number {
|
|
|
|
return this.buf.byteLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Returns how many bytes are unused in the buffer. */
|
|
|
|
available(): number {
|
|
|
|
return this.buf.byteLength - this.usedBufferBytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** buffered returns the number of bytes that have been written into the
|
|
|
|
* current buffer.
|
|
|
|
*/
|
|
|
|
buffered(): number {
|
|
|
|
return this.usedBufferBytes;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-08 18:15:26 -05:00
|
|
|
/** BufWriter implements buffering for an deno.Writer object.
|
|
|
|
* If an error occurs writing to a Writer, no more data will be
|
|
|
|
* accepted and all subsequent writes, and flush(), will return the error.
|
|
|
|
* After all data has been written, the client should call the
|
|
|
|
* flush() method to guarantee all data has been forwarded to
|
|
|
|
* the underlying deno.Writer.
|
|
|
|
*/
|
2020-04-17 16:12:14 -04:00
|
|
|
export class BufWriter extends AbstractBufBase implements Writer {
|
|
|
|
/** return new BufWriter unless writer is BufWriter */
|
|
|
|
static create(writer: Writer, size: number = DEFAULT_BUF_SIZE): BufWriter {
|
|
|
|
return writer instanceof BufWriter ? writer : new BufWriter(writer, size);
|
2019-05-14 15:19:12 -04:00
|
|
|
}
|
|
|
|
|
2020-04-17 16:12:14 -04:00
|
|
|
constructor(private writer: Writer, size: number = DEFAULT_BUF_SIZE) {
|
|
|
|
super();
|
2018-11-09 13:25:30 -05:00
|
|
|
if (size <= 0) {
|
|
|
|
size = DEFAULT_BUF_SIZE;
|
|
|
|
}
|
|
|
|
this.buf = new Uint8Array(size);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Discards any unflushed buffered data, clears any error, and
|
2020-04-17 16:12:14 -04:00
|
|
|
* resets buffer to write its output to w.
|
2018-11-09 13:25:30 -05:00
|
|
|
*/
|
|
|
|
reset(w: Writer): void {
|
|
|
|
this.err = null;
|
2020-04-17 16:12:14 -04:00
|
|
|
this.usedBufferBytes = 0;
|
|
|
|
this.writer = w;
|
2018-11-09 13:25:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Flush writes any buffered data to the underlying io.Writer. */
|
2019-05-23 22:04:06 -04:00
|
|
|
async flush(): Promise<void> {
|
|
|
|
if (this.err !== null) throw this.err;
|
2020-04-17 16:12:14 -04:00
|
|
|
if (this.usedBufferBytes === 0) return;
|
2018-11-09 13:25:30 -05:00
|
|
|
|
|
|
|
try {
|
2020-06-25 08:17:33 -04:00
|
|
|
await Deno.writeAll(
|
|
|
|
this.writer,
|
2020-07-14 15:24:17 -04:00
|
|
|
this.buf.subarray(0, this.usedBufferBytes),
|
2020-04-17 16:12:14 -04:00
|
|
|
);
|
2018-11-09 13:25:30 -05:00
|
|
|
} catch (e) {
|
2019-05-23 22:04:06 -04:00
|
|
|
this.err = e;
|
|
|
|
throw e;
|
2018-11-09 13:25:30 -05:00
|
|
|
}
|
|
|
|
|
2020-06-12 15:15:29 -04:00
|
|
|
this.buf = new Uint8Array(this.buf.length);
|
2020-04-17 16:12:14 -04:00
|
|
|
this.usedBufferBytes = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Writes the contents of `data` into the buffer. If the contents won't fully
|
|
|
|
* fit into the buffer, those bytes that can are copied into the buffer, the
|
|
|
|
* buffer is the flushed to the writer and the remaining bytes are copied into
|
|
|
|
* the now empty buffer.
|
|
|
|
*
|
|
|
|
* @return the number of bytes written to the buffer.
|
|
|
|
*/
|
|
|
|
async write(data: Uint8Array): Promise<number> {
|
|
|
|
if (this.err !== null) throw this.err;
|
|
|
|
if (data.length === 0) return 0;
|
|
|
|
|
|
|
|
let totalBytesWritten = 0;
|
|
|
|
let numBytesWritten = 0;
|
|
|
|
while (data.byteLength > this.available()) {
|
|
|
|
if (this.buffered() === 0) {
|
|
|
|
// Large write, empty buffer.
|
|
|
|
// Write directly from data to avoid copy.
|
|
|
|
try {
|
|
|
|
numBytesWritten = await this.writer.write(data);
|
|
|
|
} catch (e) {
|
|
|
|
this.err = e;
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
} else {
|
2020-04-30 16:39:25 -04:00
|
|
|
numBytesWritten = copyBytes(data, this.buf, this.usedBufferBytes);
|
2020-04-17 16:12:14 -04:00
|
|
|
this.usedBufferBytes += numBytesWritten;
|
|
|
|
await this.flush();
|
2018-11-09 13:25:30 -05:00
|
|
|
}
|
2020-04-17 16:12:14 -04:00
|
|
|
totalBytesWritten += numBytesWritten;
|
|
|
|
data = data.subarray(numBytesWritten);
|
2018-11-09 13:25:30 -05:00
|
|
|
}
|
2019-05-23 22:04:06 -04:00
|
|
|
|
2020-04-30 16:39:25 -04:00
|
|
|
numBytesWritten = copyBytes(data, this.buf, this.usedBufferBytes);
|
2020-04-17 16:12:14 -04:00
|
|
|
this.usedBufferBytes += numBytesWritten;
|
|
|
|
totalBytesWritten += numBytesWritten;
|
|
|
|
return totalBytesWritten;
|
2018-11-09 13:25:30 -05:00
|
|
|
}
|
2020-04-17 16:12:14 -04:00
|
|
|
}
|
2018-11-09 13:25:30 -05:00
|
|
|
|
2020-04-28 07:23:30 -04:00
|
|
|
/** BufWriterSync implements buffering for a deno.WriterSync object.
|
|
|
|
* If an error occurs writing to a WriterSync, no more data will be
|
2020-04-17 16:12:14 -04:00
|
|
|
* accepted and all subsequent writes, and flush(), will return the error.
|
|
|
|
* After all data has been written, the client should call the
|
|
|
|
* flush() method to guarantee all data has been forwarded to
|
2020-04-28 07:23:30 -04:00
|
|
|
* the underlying deno.WriterSync.
|
2020-04-17 16:12:14 -04:00
|
|
|
*/
|
2020-04-28 07:23:30 -04:00
|
|
|
export class BufWriterSync extends AbstractBufBase implements WriterSync {
|
2020-04-17 16:12:14 -04:00
|
|
|
/** return new BufWriterSync unless writer is BufWriterSync */
|
|
|
|
static create(
|
2020-04-28 07:23:30 -04:00
|
|
|
writer: WriterSync,
|
2020-07-14 15:24:17 -04:00
|
|
|
size: number = DEFAULT_BUF_SIZE,
|
2020-04-17 16:12:14 -04:00
|
|
|
): BufWriterSync {
|
|
|
|
return writer instanceof BufWriterSync
|
|
|
|
? writer
|
|
|
|
: new BufWriterSync(writer, size);
|
2018-11-09 13:25:30 -05:00
|
|
|
}
|
|
|
|
|
2020-04-28 07:23:30 -04:00
|
|
|
constructor(private writer: WriterSync, size: number = DEFAULT_BUF_SIZE) {
|
2020-04-17 16:12:14 -04:00
|
|
|
super();
|
|
|
|
if (size <= 0) {
|
|
|
|
size = DEFAULT_BUF_SIZE;
|
|
|
|
}
|
|
|
|
this.buf = new Uint8Array(size);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Discards any unflushed buffered data, clears any error, and
|
|
|
|
* resets buffer to write its output to w.
|
2018-11-09 13:25:30 -05:00
|
|
|
*/
|
2020-04-28 07:23:30 -04:00
|
|
|
reset(w: WriterSync): void {
|
2020-04-17 16:12:14 -04:00
|
|
|
this.err = null;
|
|
|
|
this.usedBufferBytes = 0;
|
|
|
|
this.writer = w;
|
|
|
|
}
|
|
|
|
|
2020-04-28 07:23:30 -04:00
|
|
|
/** Flush writes any buffered data to the underlying io.WriterSync. */
|
2020-04-17 16:12:14 -04:00
|
|
|
flush(): void {
|
|
|
|
if (this.err !== null) throw this.err;
|
|
|
|
if (this.usedBufferBytes === 0) return;
|
|
|
|
|
|
|
|
try {
|
2020-06-25 08:17:33 -04:00
|
|
|
Deno.writeAllSync(
|
|
|
|
this.writer,
|
2020-07-14 15:24:17 -04:00
|
|
|
this.buf.subarray(0, this.usedBufferBytes),
|
2020-04-17 16:12:14 -04:00
|
|
|
);
|
|
|
|
} catch (e) {
|
|
|
|
this.err = e;
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
|
2020-06-12 15:15:29 -04:00
|
|
|
this.buf = new Uint8Array(this.buf.length);
|
2020-04-17 16:12:14 -04:00
|
|
|
this.usedBufferBytes = 0;
|
2018-11-09 13:25:30 -05:00
|
|
|
}
|
|
|
|
|
2020-04-17 16:12:14 -04:00
|
|
|
/** Writes the contents of `data` into the buffer. If the contents won't fully
|
|
|
|
* fit into the buffer, those bytes that can are copied into the buffer, the
|
|
|
|
* buffer is the flushed to the writer and the remaining bytes are copied into
|
|
|
|
* the now empty buffer.
|
|
|
|
*
|
|
|
|
* @return the number of bytes written to the buffer.
|
2018-11-09 13:25:30 -05:00
|
|
|
*/
|
2020-04-17 16:12:14 -04:00
|
|
|
writeSync(data: Uint8Array): number {
|
2019-05-23 22:04:06 -04:00
|
|
|
if (this.err !== null) throw this.err;
|
2020-04-17 16:12:14 -04:00
|
|
|
if (data.length === 0) return 0;
|
2019-05-23 22:04:06 -04:00
|
|
|
|
2020-04-17 16:12:14 -04:00
|
|
|
let totalBytesWritten = 0;
|
|
|
|
let numBytesWritten = 0;
|
|
|
|
while (data.byteLength > this.available()) {
|
2019-05-23 22:04:06 -04:00
|
|
|
if (this.buffered() === 0) {
|
2018-11-09 13:25:30 -05:00
|
|
|
// Large write, empty buffer.
|
2020-04-17 16:12:14 -04:00
|
|
|
// Write directly from data to avoid copy.
|
2018-11-09 13:25:30 -05:00
|
|
|
try {
|
2020-04-17 16:12:14 -04:00
|
|
|
numBytesWritten = this.writer.writeSync(data);
|
2018-11-09 13:25:30 -05:00
|
|
|
} catch (e) {
|
|
|
|
this.err = e;
|
2019-05-23 22:04:06 -04:00
|
|
|
throw e;
|
2018-11-09 13:25:30 -05:00
|
|
|
}
|
|
|
|
} else {
|
2020-04-30 16:39:25 -04:00
|
|
|
numBytesWritten = copyBytes(data, this.buf, this.usedBufferBytes);
|
2020-04-17 16:12:14 -04:00
|
|
|
this.usedBufferBytes += numBytesWritten;
|
|
|
|
this.flush();
|
2018-11-09 13:25:30 -05:00
|
|
|
}
|
2020-04-17 16:12:14 -04:00
|
|
|
totalBytesWritten += numBytesWritten;
|
|
|
|
data = data.subarray(numBytesWritten);
|
2018-11-09 13:25:30 -05:00
|
|
|
}
|
2019-05-23 22:04:06 -04:00
|
|
|
|
2020-04-30 16:39:25 -04:00
|
|
|
numBytesWritten = copyBytes(data, this.buf, this.usedBufferBytes);
|
2020-04-17 16:12:14 -04:00
|
|
|
this.usedBufferBytes += numBytesWritten;
|
|
|
|
totalBytesWritten += numBytesWritten;
|
|
|
|
return totalBytesWritten;
|
2018-11-09 13:25:30 -05:00
|
|
|
}
|
2018-11-08 18:15:26 -05:00
|
|
|
}
|
2020-02-17 19:51:13 -05:00
|
|
|
|
|
|
|
/** Generate longest proper prefix which is also suffix array. */
|
|
|
|
function createLPS(pat: Uint8Array): Uint8Array {
|
|
|
|
const lps = new Uint8Array(pat.length);
|
|
|
|
lps[0] = 0;
|
|
|
|
let prefixEnd = 0;
|
|
|
|
let i = 1;
|
|
|
|
while (i < lps.length) {
|
|
|
|
if (pat[i] == pat[prefixEnd]) {
|
|
|
|
prefixEnd++;
|
|
|
|
lps[i] = prefixEnd;
|
|
|
|
i++;
|
|
|
|
} else if (prefixEnd === 0) {
|
|
|
|
lps[i] = 0;
|
|
|
|
i++;
|
|
|
|
} else {
|
|
|
|
prefixEnd = pat[prefixEnd - 1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return lps;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Read delimited bytes from a Reader. */
|
|
|
|
export async function* readDelim(
|
|
|
|
reader: Reader,
|
2020-07-14 15:24:17 -04:00
|
|
|
delim: Uint8Array,
|
2020-02-17 19:51:13 -05:00
|
|
|
): AsyncIterableIterator<Uint8Array> {
|
|
|
|
// Avoid unicode problems
|
|
|
|
const delimLen = delim.length;
|
|
|
|
const delimLPS = createLPS(delim);
|
|
|
|
|
|
|
|
let inputBuffer = new Deno.Buffer();
|
|
|
|
const inspectArr = new Uint8Array(Math.max(1024, delimLen + 1));
|
|
|
|
|
|
|
|
// Modified KMP
|
|
|
|
let inspectIndex = 0;
|
|
|
|
let matchIndex = 0;
|
|
|
|
while (true) {
|
|
|
|
const result = await reader.read(inspectArr);
|
2020-04-28 12:40:43 -04:00
|
|
|
if (result === null) {
|
2020-02-17 19:51:13 -05:00
|
|
|
// Yield last chunk.
|
|
|
|
yield inputBuffer.bytes();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ((result as number) < 0) {
|
|
|
|
// Discard all remaining and silently fail.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const sliceRead = inspectArr.subarray(0, result as number);
|
|
|
|
await Deno.writeAll(inputBuffer, sliceRead);
|
|
|
|
|
|
|
|
let sliceToProcess = inputBuffer.bytes();
|
|
|
|
while (inspectIndex < sliceToProcess.length) {
|
|
|
|
if (sliceToProcess[inspectIndex] === delim[matchIndex]) {
|
|
|
|
inspectIndex++;
|
|
|
|
matchIndex++;
|
|
|
|
if (matchIndex === delimLen) {
|
|
|
|
// Full match
|
|
|
|
const matchEnd = inspectIndex - delimLen;
|
|
|
|
const readyBytes = sliceToProcess.subarray(0, matchEnd);
|
|
|
|
// Copy
|
|
|
|
const pendingBytes = sliceToProcess.slice(inspectIndex);
|
|
|
|
yield readyBytes;
|
|
|
|
// Reset match, different from KMP.
|
|
|
|
sliceToProcess = pendingBytes;
|
|
|
|
inspectIndex = 0;
|
|
|
|
matchIndex = 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (matchIndex === 0) {
|
|
|
|
inspectIndex++;
|
|
|
|
} else {
|
|
|
|
matchIndex = delimLPS[matchIndex - 1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Keep inspectIndex and matchIndex.
|
|
|
|
inputBuffer = new Deno.Buffer(sliceToProcess);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Read delimited strings from a Reader. */
|
|
|
|
export async function* readStringDelim(
|
|
|
|
reader: Reader,
|
2020-07-14 15:24:17 -04:00
|
|
|
delim: string,
|
2020-02-17 19:51:13 -05:00
|
|
|
): AsyncIterableIterator<string> {
|
|
|
|
const encoder = new TextEncoder();
|
|
|
|
const decoder = new TextDecoder();
|
|
|
|
for await (const chunk of readDelim(reader, encoder.encode(delim))) {
|
|
|
|
yield decoder.decode(chunk);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Read strings line-by-line from a Reader. */
|
|
|
|
export async function* readLines(
|
2020-07-14 15:24:17 -04:00
|
|
|
reader: Reader,
|
2020-02-17 19:51:13 -05:00
|
|
|
): AsyncIterableIterator<string> {
|
|
|
|
yield* readStringDelim(reader, "\n");
|
|
|
|
}
|