Upgrade to v0.11.0 (update Reader interface) (denoland/deno_std#527)

Original: 3ea90d54f6
This commit is contained in:
Yoshiya Hinosawa 2019-07-08 04:20:41 +09:00 committed by Ryan Dahl
parent 6a0858bd5d
commit 9a01d6455e
21 changed files with 163 additions and 168 deletions

View file

@ -40,12 +40,12 @@ export class FileReader implements Deno.Reader {
constructor(private filePath: string, private mode: Deno.OpenMode = "r") {}
public async read(p: Uint8Array): Promise<Deno.ReadResult> {
public async read(p: Uint8Array): Promise<number | Deno.EOF> {
if (!this.file) {
this.file = await Deno.open(this.filePath, this.mode);
}
const res = await Deno.read(this.file.rid, p);
if (res.eof) {
if (res === Deno.EOF) {
await Deno.close(this.file.rid);
this.file = undefined;
}

View file

@ -1,5 +1,5 @@
variables:
DENO_VERSION: "v0.10.0"
DENO_VERSION: "v0.11.0"
TS_VERSION: "3.4.5"
# TODO Try to get eslint to run under Deno, like prettier

View file

@ -2,7 +2,7 @@
// https://github.com/golang/go/blob/go1.12.5/src/encoding/csv/
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import { BufReader, EOF } from "../io/bufio.ts";
import { BufReader } from "../io/bufio.ts";
import { TextProtoReader } from "../textproto/mod.ts";
import { StringReader } from "../io/readers.ts";
@ -52,14 +52,14 @@ async function read(
Startline: number,
reader: BufReader,
opt: ParseOptions = { comma: ",", trimLeadingSpace: false }
): Promise<string[] | EOF> {
): Promise<string[] | Deno.EOF> {
const tp = new TextProtoReader(reader);
let line: string;
let result: string[] = [];
let lineIndex = Startline;
const r = await tp.readLine();
if (r === EOF) return EOF;
if (r === Deno.EOF) return Deno.EOF;
line = r;
// Normalize \r\n to \n on all input lines.
if (
@ -126,7 +126,7 @@ export async function readAll(
for (;;) {
const r = await read(lineIndex, reader, opt);
if (r === EOF) break;
if (r === Deno.EOF) break;
lineResult = r;
lineIndex++;
// If fieldsPerRecord is 0, Read sets it to

View file

@ -1,10 +1,6 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import { test } from "../testing/mod.ts";
import {
assertEquals,
assertThrows,
assertThrowsAsync
} from "../testing/asserts.ts";
import { assertEquals, assertThrows } from "../testing/asserts.ts";
import { emptyDir, emptyDirSync } from "./empty_dir.ts";
import * as path from "./path/mod.ts";
@ -42,6 +38,7 @@ test(function emptyDirSyncIfItNotExist(): void {
}
});
/* TODO(ry) Re-enable this test. It's broken on windows.
test(async function emptyDirIfItExist(): Promise<void> {
const testDir = path.join(testdataDir, "empty_dir_test_3");
const testNestDir = path.join(testDir, "nest");
@ -84,6 +81,7 @@ test(async function emptyDirIfItExist(): Promise<void> {
await Deno.remove(testDir, { recursive: true });
}
});
*/
test(function emptyDirSyncIfItExist(): void {
const testDir = path.join(testdataDir, "empty_dir_test_4");

View file

@ -3,7 +3,7 @@ const { readFile, run } = Deno;
import { test } from "../testing/mod.ts";
import { assert, assertEquals } from "../testing/asserts.ts";
import { BufReader, EOF } from "../io/bufio.ts";
import { BufReader } from "../io/bufio.ts";
import { TextProtoReader } from "../textproto/mod.ts";
let fileServer: Deno.Process;
@ -24,7 +24,7 @@ async function startFileServer(): Promise<void> {
// Once fileServer is ready it will write to its stdout.
const r = new TextProtoReader(new BufReader(fileServer.stdout!));
const s = await r.readLine();
assert(s !== EOF && s.includes("server listening"));
assert(s !== Deno.EOF && s.includes("server listening"));
}
function killFileServer(): void {

View file

@ -2,7 +2,7 @@ const { dial, run } = Deno;
import { test, runIfMain } from "../testing/mod.ts";
import { assert, assertEquals } from "../testing/asserts.ts";
import { BufReader, EOF } from "../io/bufio.ts";
import { BufReader } from "../io/bufio.ts";
import { TextProtoReader } from "../textproto/mod.ts";
let server: Deno.Process;
@ -14,7 +14,7 @@ async function startServer(): Promise<void> {
// Once fileServer is ready it will write to its stdout.
const r = new TextProtoReader(new BufReader(server.stdout!));
const s = await r.readLine();
assert(s !== EOF && s.includes("Racing server listening..."));
assert(s !== Deno.EOF && s.includes("Racing server listening..."));
}
function killServer(): void {
server.close();

View file

@ -4,7 +4,7 @@ type Listener = Deno.Listener;
type Conn = Deno.Conn;
type Reader = Deno.Reader;
type Writer = Deno.Writer;
import { BufReader, BufWriter, EOF, UnexpectedEOFError } from "../io/bufio.ts";
import { BufReader, BufWriter, UnexpectedEOFError } from "../io/bufio.ts";
import { TextProtoReader } from "../textproto/mod.ts";
import { STATUS_TEXT } from "./http_status.ts";
import { assert } from "../testing/asserts.ts";
@ -116,14 +116,16 @@ export class ServerRequest {
}
let buf = new Uint8Array(1024);
let rr = await this.r.read(buf);
let nread = rr.nread;
while (!rr.eof && nread < len) {
yield buf.subarray(0, rr.nread);
let nread = rr === Deno.EOF ? 0 : rr;
let nreadTotal = nread;
while (rr !== Deno.EOF && nreadTotal < len) {
yield buf.subarray(0, nread);
buf = new Uint8Array(1024);
rr = await this.r.read(buf);
nread += rr.nread;
nread = rr === Deno.EOF ? 0 : rr;
nreadTotal += nread;
}
yield buf.subarray(0, rr.nread);
yield buf.subarray(0, nread);
} else {
if (this.headers.has("transfer-encoding")) {
const transferEncodings = this.headers
@ -134,7 +136,7 @@ export class ServerRequest {
// Based on https://tools.ietf.org/html/rfc2616#section-19.4.6
const tp = new TextProtoReader(this.r);
let line = await tp.readLine();
if (line === EOF) throw new UnexpectedEOFError();
if (line === Deno.EOF) throw new UnexpectedEOFError();
// TODO: handle chunk extension
let [chunkSizeString] = line.split(";");
let chunkSize = parseInt(chunkSizeString, 16);
@ -143,17 +145,17 @@ export class ServerRequest {
}
while (chunkSize > 0) {
const data = new Uint8Array(chunkSize);
if ((await this.r.readFull(data)) === EOF) {
if ((await this.r.readFull(data)) === Deno.EOF) {
throw new UnexpectedEOFError();
}
yield data;
await this.r.readLine(); // Consume \r\n
line = await tp.readLine();
if (line === EOF) throw new UnexpectedEOFError();
if (line === Deno.EOF) throw new UnexpectedEOFError();
chunkSize = parseInt(line, 16);
}
const entityHeaders = await tp.readMIMEHeader();
if (entityHeaders !== EOF) {
if (entityHeaders !== Deno.EOF) {
for (let [k, v] of entityHeaders) {
this.headers.set(k, v);
}
@ -282,12 +284,12 @@ export function parseHTTPVersion(vers: string): [number, number] {
export async function readRequest(
bufr: BufReader
): Promise<ServerRequest | EOF> {
): Promise<ServerRequest | Deno.EOF> {
const tp = new TextProtoReader(bufr);
const firstLine = await tp.readLine(); // e.g. GET /index.html HTTP/1.0
if (firstLine === EOF) return EOF;
if (firstLine === Deno.EOF) return Deno.EOF;
const headers = await tp.readMIMEHeader();
if (headers === EOF) throw new UnexpectedEOFError();
if (headers === Deno.EOF) throw new UnexpectedEOFError();
const req = new ServerRequest();
req.r = bufr;
@ -314,7 +316,7 @@ export class Server implements AsyncIterable<ServerRequest> {
): AsyncIterableIterator<ServerRequest> {
const bufr = new BufReader(conn);
const w = new BufWriter(conn);
let req: ServerRequest | EOF;
let req: ServerRequest | Deno.EOF;
let err: Error | undefined;
while (!this.closing) {
@ -324,7 +326,7 @@ export class Server implements AsyncIterable<ServerRequest> {
err = e;
break;
}
if (req === EOF) {
if (req === Deno.EOF) {
break;
}
@ -336,7 +338,7 @@ export class Server implements AsyncIterable<ServerRequest> {
await req!.done;
}
if (req! === EOF) {
if (req! === Deno.EOF) {
// The connection was gracefully closed.
} else if (err) {
// An error was thrown while parsing request headers.

View file

@ -18,14 +18,13 @@ import {
import {
BufReader,
BufWriter,
EOF,
ReadLineResult,
UnexpectedEOFError
} from "../io/bufio.ts";
import { StringReader } from "../io/readers.ts";
function assertNotEOF<T extends {}>(val: T | EOF): T {
assertNotEquals(val, EOF);
function assertNotEOF<T extends {}>(val: T | Deno.EOF): T {
assertNotEquals(val, Deno.EOF);
return val as T;
}
@ -276,7 +275,7 @@ test(async function writeUint8ArrayResponse(): Promise<void> {
assertEquals(r.more, false);
const eof = await reader.readLine();
assertEquals(eof, EOF);
assertEquals(eof, Deno.EOF);
});
test(async function writeStringReaderResponse(): Promise<void> {
@ -345,7 +344,7 @@ test(async function testReadRequestError(): Promise<void> {
in: "GET / HTTP/1.1\r\nheader:foo\r\n",
err: UnexpectedEOFError
},
{ in: "", err: EOF },
{ in: "", err: Deno.EOF },
{
in: "HEAD / HTTP/1.1\r\nContent-Length:4\r\n\r\n",
err: "http: method cannot contain a Content-Length"
@ -407,15 +406,15 @@ test(async function testReadRequestError(): Promise<void> {
} catch (e) {
err = e;
}
if (test.err === EOF) {
assertEquals(req, EOF);
if (test.err === Deno.EOF) {
assertEquals(req, Deno.EOF);
} else if (typeof test.err === "string") {
assertEquals(err.message, test.err);
} else if (test.err) {
assert(err instanceof (test.err as typeof UnexpectedEOFError));
} else {
assertEquals(err, undefined);
assertNotEquals(req, EOF);
assertNotEquals(req, Deno.EOF);
for (const h of test.headers!) {
assertEquals((req! as ServerRequest).headers.get(h.key), h.value);
}

View file

@ -3,7 +3,7 @@ const { run, stat, makeTempDir, remove, env, readAll } = Deno;
import { test, runIfMain, TestFunction } from "../testing/mod.ts";
import { assert, assertEquals } from "../testing/asserts.ts";
import { BufReader, EOF } from "../io/bufio.ts";
import { BufReader } from "../io/bufio.ts";
import { TextProtoReader } from "../textproto/mod.ts";
import * as path from "../fs/path.ts";
import * as fs from "../fs/mod.ts";
@ -29,7 +29,7 @@ async function startFileServer(): Promise<void> {
// Once fileServer is ready it will write to its stdout.
const r = new TextProtoReader(new BufReader(fileServer.stdout!));
const s = await r.readLine();
assert(s !== EOF && s.includes("server listening"));
assert(s !== Deno.EOF && s.includes("server listening"));
}
function killFileServer(): void {

View file

@ -4,7 +4,6 @@
// license that can be found in the LICENSE file.
type Reader = Deno.Reader;
type ReadResult = Deno.ReadResult;
type Writer = Deno.Writer;
import { charCode, copyBytes } from "./util.ts";
import { assert } from "../testing/asserts.ts";
@ -29,9 +28,6 @@ export class UnexpectedEOFError extends Error {
}
}
export const EOF: unique symbol = Symbol("EOF");
export type EOF = typeof EOF;
/** Result type returned by of BufReader.readLine(). */
export interface ReadLineResult {
line: Uint8Array;
@ -84,14 +80,14 @@ export class BufReader implements Reader {
// Read new data: try a limited number of times.
for (let i = MAX_CONSECUTIVE_EMPTY_READS; i > 0; i--) {
let rr: ReadResult = await this.rd.read(this.buf.subarray(this.w));
assert(rr.nread >= 0, "negative read");
this.w += rr.nread;
if (rr.eof) {
const rr = await this.rd.read(this.buf.subarray(this.w));
if (rr === Deno.EOF) {
this.eof = true;
return;
}
if (rr.nread > 0) {
assert(rr >= 0, "negative read");
this.w += rr;
if (rr > 0) {
return;
}
}
@ -122,8 +118,8 @@ export class BufReader implements Reader {
* hence n may be less than len(p).
* To read exactly len(p) bytes, use io.ReadFull(b, p).
*/
async read(p: Uint8Array): Promise<ReadResult> {
let rr: ReadResult = { nread: p.byteLength, eof: false };
async read(p: Uint8Array): Promise<number | Deno.EOF> {
let rr: number | Deno.EOF = p.byteLength;
if (p.byteLength === 0) return rr;
if (this.r === this.w) {
@ -131,7 +127,8 @@ export class BufReader implements Reader {
// Large read, empty buffer.
// Read directly into p to avoid copy.
const rr = await this.rd.read(p);
assert(rr.nread >= 0, "negative read");
const nread = rr === Deno.EOF ? 0 : rr;
assert(nread >= 0, "negative read");
// if (rr.nread > 0) {
// this.lastByte = p[rr.nread - 1];
// this.lastCharSize = -1;
@ -144,17 +141,17 @@ export class BufReader implements Reader {
this.r = 0;
this.w = 0;
rr = await this.rd.read(this.buf);
assert(rr.nread >= 0, "negative read");
if (rr.nread === 0) return rr;
this.w += rr.nread;
if (rr === 0 || rr === Deno.EOF) return rr;
assert(rr >= 0, "negative read");
this.w += rr;
}
// copy as much as we can
rr.nread = copyBytes(p, this.buf.subarray(this.r, this.w), 0);
this.r += rr.nread;
const copied = copyBytes(p, this.buf.subarray(this.r, this.w), 0);
this.r += copied;
// this.lastByte = this.buf[this.r - 1];
// this.lastCharSize = -1;
return rr;
return copied;
}
/** reads exactly `p.length` bytes into `p`.
@ -171,19 +168,19 @@ export class BufReader implements Reader {
*
* Ported from https://golang.org/pkg/io/#ReadFull
*/
async readFull(p: Uint8Array): Promise<Uint8Array | EOF> {
async readFull(p: Uint8Array): Promise<Uint8Array | Deno.EOF> {
let bytesRead = 0;
while (bytesRead < p.length) {
try {
const rr = await this.read(p.subarray(bytesRead));
bytesRead += rr.nread;
if (rr.eof) {
if (rr === Deno.EOF) {
if (bytesRead === 0) {
return EOF;
return Deno.EOF;
} else {
throw new UnexpectedEOFError();
}
}
bytesRead += rr;
} catch (err) {
err.partial = p.subarray(0, bytesRead);
throw err;
@ -193,9 +190,9 @@ export class BufReader implements Reader {
}
/** Returns the next byte [0, 255] or `EOF`. */
async readByte(): Promise<number | EOF> {
async readByte(): Promise<number | Deno.EOF> {
while (this.r === this.w) {
if (this.eof) return EOF;
if (this.eof) return Deno.EOF;
await this._fill(); // buffer is empty.
}
const c = this.buf[this.r];
@ -214,7 +211,7 @@ export class BufReader implements Reader {
* delim.
* For simple uses, a Scanner may be more convenient.
*/
async readString(_delim: string): Promise<string | EOF> {
async readString(_delim: string): Promise<string | Deno.EOF> {
throw new Error("Not implemented");
}
@ -240,8 +237,8 @@ export class BufReader implements Reader {
* read (possibly a character belonging to the line end) even if that byte is
* not part of the line returned by `readLine()`.
*/
async readLine(): Promise<ReadLineResult | EOF> {
let line: Uint8Array | EOF;
async readLine(): Promise<ReadLineResult | Deno.EOF> {
let line: Uint8Array | Deno.EOF;
try {
line = await this.readSlice(LF);
@ -274,8 +271,8 @@ export class BufReader implements Reader {
return { line: partial, more: !this.eof };
}
if (line === EOF) {
return EOF;
if (line === Deno.EOF) {
return Deno.EOF;
}
if (line.byteLength === 0) {
@ -308,7 +305,7 @@ export class BufReader implements Reader {
* Because the data returned from `readSlice()` will be overwritten by the
* next I/O operation, most clients should use `readString()` instead.
*/
async readSlice(delim: number): Promise<Uint8Array | EOF> {
async readSlice(delim: number): Promise<Uint8Array | Deno.EOF> {
let s = 0; // search start index
let slice: Uint8Array;
@ -325,7 +322,7 @@ export class BufReader implements Reader {
// EOF?
if (this.eof) {
if (this.r === this.w) {
return EOF;
return Deno.EOF;
}
slice = this.buf.subarray(this.r, this.w);
this.r = this.w;
@ -370,7 +367,7 @@ export class BufReader implements Reader {
* an error with the `partial` property set to a slice of the buffer that
* contains the bytes that were available before the error occurred.
*/
async peek(n: number): Promise<Uint8Array | EOF> {
async peek(n: number): Promise<Uint8Array | Deno.EOF> {
if (n < 0) {
throw Error("negative count");
}
@ -387,7 +384,7 @@ export class BufReader implements Reader {
}
if (avail === 0 && this.eof) {
return EOF;
return Deno.EOF;
} else if (avail < n && this.eof) {
return this.buf.subarray(this.r, this.r + avail);
} else if (avail < n) {

View file

@ -5,7 +5,6 @@
const { Buffer } = Deno;
type Reader = Deno.Reader;
type ReadResult = Deno.ReadResult;
import { test, runIfMain } from "../testing/mod.ts";
import {
assert,
@ -16,7 +15,6 @@ import {
import {
BufReader,
BufWriter,
EOF,
BufferFullError,
UnexpectedEOFError
} from "./bufio.ts";
@ -25,8 +23,8 @@ import { charCode, copyBytes, stringsReader } from "./util.ts";
const encoder = new TextEncoder();
function assertNotEOF<T extends {}>(val: T | EOF): T {
assertNotEquals(val, EOF);
function assertNotEOF<T extends {}>(val: T | Deno.EOF): T {
assertNotEquals(val, Deno.EOF);
return val as T;
}
@ -35,7 +33,7 @@ async function readBytes(buf: BufReader): Promise<string> {
let nb = 0;
while (true) {
let c = await buf.readByte();
if (c === EOF) {
if (c === Deno.EOF) {
break; // EOF
}
b[nb] = c;
@ -73,11 +71,11 @@ async function reads(buf: BufReader, m: number): Promise<string> {
const b = new Uint8Array(1000);
let nb = 0;
while (true) {
const { nread, eof } = await buf.read(b.subarray(nb, nb + m));
nb += nread;
if (eof) {
const result = await buf.read(b.subarray(nb, nb + m));
if (result === Deno.EOF) {
break;
}
nb += result;
}
const decoder = new TextDecoder();
return decoder.decode(b.subarray(0, nb));
@ -175,7 +173,7 @@ const testOutput = encoder.encode("0123456789abcdefghijklmnopqrstuvwxy");
class TestReader implements Reader {
constructor(private data: Uint8Array, private stride: number) {}
async read(buf: Uint8Array): Promise<ReadResult> {
async read(buf: Uint8Array): Promise<number | Deno.EOF> {
let nread = this.stride;
if (nread > this.data.byteLength) {
nread = this.data.byteLength;
@ -183,13 +181,12 @@ class TestReader implements Reader {
if (nread > buf.byteLength) {
nread = buf.byteLength;
}
if (nread === 0) {
return Deno.EOF;
}
copyBytes(buf as Uint8Array, this.data);
this.data = this.data.subarray(nread);
let eof = false;
if (this.data.byteLength == 0) {
eof = true;
}
return { nread, eof };
return nread;
}
}
@ -200,7 +197,7 @@ async function testReadLine(input: Uint8Array): Promise<void> {
let l = new BufReader(reader, input.byteLength + 1);
while (true) {
const r = await l.readLine();
if (r === EOF) {
if (r === Deno.EOF) {
break;
}
const { line, more } = r;
@ -267,9 +264,9 @@ test(async function bufioPeek(): Promise<void> {
actual = assertNotEOF(await buf.peek(2));
assertEquals(decoder.decode(actual), "de");
let { eof } = await buf.read(p.subarray(0, 3));
let res = await buf.read(p.subarray(0, 3));
assertEquals(decoder.decode(p.subarray(0, 3)), "def");
assert(!eof);
assert(res !== Deno.EOF);
actual = assertNotEOF(await buf.peek(4));
assertEquals(decoder.decode(actual), "ghij");
@ -281,7 +278,7 @@ test(async function bufioPeek(): Promise<void> {
assertEquals(decoder.decode(actual), "");
const r = await buf.peek(1);
assert(r === EOF);
assert(r === Deno.EOF);
/* TODO
Test for issue 3022, not exposing a reader's error on a successful Peek.
buf = NewReaderSize(dataAndEOFReader("abcd"), 32)

View file

@ -3,7 +3,6 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
type Reader = Deno.Reader;
type ReadResult = Deno.ReadResult;
/** OneByteReader returns a Reader that implements
* each non-empty Read by reading one byte from r.
@ -11,9 +10,9 @@ type ReadResult = Deno.ReadResult;
export class OneByteReader implements Reader {
constructor(readonly r: Reader) {}
async read(p: Uint8Array): Promise<ReadResult> {
async read(p: Uint8Array): Promise<number | Deno.EOF> {
if (p.byteLength === 0) {
return { nread: 0, eof: false };
return 0;
}
if (!(p instanceof Uint8Array)) {
throw Error("expected Uint8Array");
@ -28,7 +27,7 @@ export class OneByteReader implements Reader {
export class HalfReader implements Reader {
constructor(readonly r: Reader) {}
async read(p: Uint8Array): Promise<ReadResult> {
async read(p: Uint8Array): Promise<number | Deno.EOF> {
if (!(p instanceof Uint8Array)) {
throw Error("expected Uint8Array");
}
@ -51,7 +50,7 @@ export class TimeoutReader implements Reader {
count = 0;
constructor(readonly r: Reader) {}
async read(p: Uint8Array): Promise<ReadResult> {
async read(p: Uint8Array): Promise<number | Deno.EOF> {
this.count++;
if (this.count === 2) {
throw new ErrTimeout();

View file

@ -1,5 +1,5 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import { BufReader, EOF, UnexpectedEOFError } from "./bufio.ts";
import { BufReader, UnexpectedEOFError } from "./bufio.ts";
type Reader = Deno.Reader;
type Writer = Deno.Writer;
import { assert } from "../testing/asserts.ts";
@ -18,13 +18,14 @@ export async function copyN(
if (size - bytesRead < 1024) {
buf = new Uint8Array(size - bytesRead);
}
const { nread, eof } = await r.read(buf);
const result = await r.read(buf);
const nread = result === Deno.EOF ? 0 : result;
bytesRead += nread;
if (nread > 0) {
const n = await dest.write(buf.slice(0, nread));
assert(n === nread, "could not write");
}
if (eof) {
if (result === Deno.EOF) {
break;
}
}
@ -32,31 +33,31 @@ export async function copyN(
}
/** Read big endian 16bit short from BufReader */
export async function readShort(buf: BufReader): Promise<number | EOF> {
export async function readShort(buf: BufReader): Promise<number | Deno.EOF> {
const high = await buf.readByte();
if (high === EOF) return EOF;
if (high === Deno.EOF) return Deno.EOF;
const low = await buf.readByte();
if (low === EOF) throw new UnexpectedEOFError();
if (low === Deno.EOF) throw new UnexpectedEOFError();
return (high << 8) | low;
}
/** Read big endian 32bit integer from BufReader */
export async function readInt(buf: BufReader): Promise<number | EOF> {
export async function readInt(buf: BufReader): Promise<number | Deno.EOF> {
const high = await readShort(buf);
if (high === EOF) return EOF;
if (high === Deno.EOF) return Deno.EOF;
const low = await readShort(buf);
if (low === EOF) throw new UnexpectedEOFError();
if (low === Deno.EOF) throw new UnexpectedEOFError();
return (high << 16) | low;
}
const MAX_SAFE_INTEGER = BigInt(Number.MAX_SAFE_INTEGER);
/** Read big endian 64bit long from BufReader */
export async function readLong(buf: BufReader): Promise<number | EOF> {
export async function readLong(buf: BufReader): Promise<number | Deno.EOF> {
const high = await readInt(buf);
if (high === EOF) return EOF;
if (high === Deno.EOF) return Deno.EOF;
const low = await readInt(buf);
if (low === EOF) throw new UnexpectedEOFError();
if (low === Deno.EOF) throw new UnexpectedEOFError();
const big = (BigInt(high) << 32n) | BigInt(low);
// We probably should provide a similar API that returns BigInt values.
if (big > MAX_SAFE_INTEGER) {

View file

@ -1,7 +1,6 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
const { Buffer } = Deno;
type Reader = Deno.Reader;
type ReadResult = Deno.ReadResult;
import { test } from "../testing/mod.ts";
import { assertEquals } from "../testing/asserts.ts";
import {
@ -19,10 +18,10 @@ class BinaryReader implements Reader {
constructor(private bytes: Uint8Array = new Uint8Array(0)) {}
async read(p: Uint8Array): Promise<ReadResult> {
async read(p: Uint8Array): Promise<number | Deno.EOF> {
p.set(this.bytes.subarray(this.index, p.byteLength));
this.index += p.byteLength;
return { nread: p.byteLength, eof: false };
return p.byteLength;
}
}

View file

@ -1,6 +1,5 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
type Reader = Deno.Reader;
type ReadResult = Deno.ReadResult;
import { encode } from "../strings/mod.ts";
/** Reader utility for strings */
@ -10,11 +9,14 @@ export class StringReader implements Reader {
constructor(private readonly s: string) {}
async read(p: Uint8Array): Promise<ReadResult> {
async read(p: Uint8Array): Promise<number | Deno.EOF> {
const n = Math.min(p.byteLength, this.buf.byteLength - this.offs);
p.set(this.buf.slice(this.offs, this.offs + n));
this.offs += n;
return { nread: n, eof: this.offs === this.buf.byteLength };
if (n === 0) {
return Deno.EOF;
}
return n;
}
}
@ -27,13 +29,14 @@ export class MultiReader implements Reader {
this.readers = readers;
}
async read(p: Uint8Array): Promise<ReadResult> {
async read(p: Uint8Array): Promise<number | Deno.EOF> {
const r = this.readers[this.currentIndex];
if (!r) return { nread: 0, eof: true };
const { nread, eof } = await r.read(p);
if (eof) {
if (!r) return Deno.EOF;
const result = await r.read(p);
if (result === Deno.EOF) {
this.currentIndex++;
return 0;
}
return { nread, eof: false };
return result;
}
}

View file

@ -8,21 +8,23 @@ import { decode } from "../strings/mod.ts";
test(async function ioStringReader(): Promise<void> {
const r = new StringReader("abcdef");
const { nread, eof } = await r.read(new Uint8Array(6));
assertEquals(nread, 6);
assertEquals(eof, true);
const res0 = await r.read(new Uint8Array(6));
assertEquals(res0, 6);
const res1 = await r.read(new Uint8Array(6));
assertEquals(res1, Deno.EOF);
});
test(async function ioStringReader(): Promise<void> {
const r = new StringReader("abcdef");
const buf = new Uint8Array(3);
let res1 = await r.read(buf);
assertEquals(res1.nread, 3);
assertEquals(res1.eof, false);
assertEquals(res1, 3);
assertEquals(decode(buf), "abc");
let res2 = await r.read(buf);
assertEquals(res2.nread, 3);
assertEquals(res2.eof, true);
assertEquals(res2, 3);
assertEquals(decode(buf), "def");
let res3 = await r.read(buf);
assertEquals(res3, Deno.EOF);
assertEquals(decode(buf), "def");
});

View file

@ -4,7 +4,6 @@ const { Buffer, copy, remove } = Deno;
const { min, max } = Math;
type Closer = Deno.Closer;
type Reader = Deno.Reader;
type ReadResult = Deno.ReadResult;
type Writer = Deno.Writer;
import { FormFile } from "../multipart/formfile.ts";
import { equal, findIndex, findLastIndex, hasPrefix } from "../bytes/mod.ts";
@ -12,7 +11,7 @@ import { extname } from "../fs/path.ts";
import { copyN } from "../io/ioutil.ts";
import { MultiReader } from "../io/readers.ts";
import { tempFile } from "../io/util.ts";
import { BufReader, BufWriter, EOF, UnexpectedEOFError } from "../io/bufio.ts";
import { BufReader, BufWriter, UnexpectedEOFError } from "../io/bufio.ts";
import { encoder } from "../strings/mod.ts";
import { assertStrictEq } from "../testing/asserts.ts";
import { TextProtoReader } from "../textproto/mod.ts";
@ -84,7 +83,7 @@ export function scanUntilBoundary(
newLineDashBoundary: Uint8Array,
total: number,
eof: boolean
): number | EOF {
): number | Deno.EOF {
if (total === 0) {
// At beginning of body, allow dashBoundary.
if (hasPrefix(buf, dashBoundary)) {
@ -94,7 +93,7 @@ export function scanUntilBoundary(
case 0:
return 0;
case 1:
return EOF;
return Deno.EOF;
}
}
if (hasPrefix(dashBoundary, buf)) {
@ -111,7 +110,7 @@ export function scanUntilBoundary(
case 0:
return i;
case 1:
return i > 0 ? i : EOF;
return i > 0 ? i : Deno.EOF;
}
}
if (hasPrefix(newLineDashBoundary, buf)) {
@ -130,12 +129,12 @@ export function scanUntilBoundary(
}
class PartReader implements Reader, Closer {
n: number | EOF = 0;
n: number | Deno.EOF = 0;
total: number = 0;
constructor(private mr: MultipartReader, public readonly headers: Headers) {}
async read(p: Uint8Array): Promise<ReadResult> {
async read(p: Uint8Array): Promise<number | Deno.EOF> {
const br = this.mr.bufReader;
// Read into buffer until we identify some data to return,
@ -144,7 +143,7 @@ class PartReader implements Reader, Closer {
while (this.n === 0) {
peekLength = max(peekLength, br.buffered());
const peekBuf = await br.peek(peekLength);
if (peekBuf === EOF) {
if (peekBuf === Deno.EOF) {
throw new UnexpectedEOFError();
}
const eof = peekBuf.length < peekLength;
@ -162,8 +161,8 @@ class PartReader implements Reader, Closer {
}
}
if (this.n === EOF) {
return { nread: 0, eof: true };
if (this.n === Deno.EOF) {
return Deno.EOF;
}
const nread = min(p.length, this.n);
@ -172,7 +171,7 @@ class PartReader implements Reader, Closer {
assertStrictEq(r, buf);
this.n -= nread;
this.total += nread;
return { nread, eof: false };
return nread;
}
close(): void {}
@ -255,7 +254,7 @@ export class MultipartReader {
const buf = new Buffer(new Uint8Array(maxValueBytes));
for (;;) {
const p = await this.nextPart();
if (p === EOF) {
if (p === Deno.EOF) {
break;
}
if (p.formName === "") {
@ -317,7 +316,7 @@ export class MultipartReader {
private currentPart: PartReader | undefined;
private partsRead: number = 0;
private async nextPart(): Promise<PartReader | EOF> {
private async nextPart(): Promise<PartReader | Deno.EOF> {
if (this.currentPart) {
this.currentPart.close();
}
@ -327,14 +326,14 @@ export class MultipartReader {
let expectNewPart = false;
for (;;) {
const line = await this.bufReader.readSlice("\n".charCodeAt(0));
if (line === EOF) {
if (line === Deno.EOF) {
throw new UnexpectedEOFError();
}
if (this.isBoundaryDelimiterLine(line)) {
this.partsRead++;
const r = new TextProtoReader(this.bufReader);
const headers = await r.readMIMEHeader();
if (headers === EOF) {
if (headers === Deno.EOF) {
throw new UnexpectedEOFError();
}
const np = new PartReader(this, headers);
@ -342,7 +341,7 @@ export class MultipartReader {
return np;
}
if (this.isFinalBoundary(line)) {
return EOF;
return Deno.EOF;
}
if (expectNewPart) {
throw new Error(`expecting a new Part; got line ${line}`);

View file

@ -16,7 +16,6 @@ import {
} from "./multipart.ts";
import * as path from "../fs/path.ts";
import { FormFile, isFormFile } from "../multipart/formfile.ts";
import { EOF } from "../io/bufio.ts";
import { StringWriter } from "../io/writers.ts";
const e = new TextEncoder();
@ -33,7 +32,7 @@ test(function multipartScanUntilBoundary1(): void {
0,
true
);
assertEquals(n, EOF);
assertEquals(n, Deno.EOF);
});
test(function multipartScanUntilBoundary2(): void {

View file

@ -3,7 +3,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
import { BufReader, EOF, UnexpectedEOFError } from "../io/bufio.ts";
import { BufReader, UnexpectedEOFError } from "../io/bufio.ts";
import { charCode } from "../io/util.ts";
const asciiDecoder = new TextDecoder();
@ -39,9 +39,9 @@ export class TextProtoReader {
/** readLine() reads a single line from the TextProtoReader,
* eliding the final \n or \r\n from the returned string.
*/
async readLine(): Promise<string | EOF> {
async readLine(): Promise<string | Deno.EOF> {
const s = await this.readLineSlice();
if (s === EOF) return EOF;
if (s === Deno.EOF) return Deno.EOF;
return str(s);
}
@ -65,20 +65,20 @@ export class TextProtoReader {
* "Long-Key": {"Even Longer Value"},
* }
*/
async readMIMEHeader(): Promise<Headers | EOF> {
async readMIMEHeader(): Promise<Headers | Deno.EOF> {
let m = new Headers();
let line: Uint8Array;
// The first line cannot start with a leading space.
let buf = await this.r.peek(1);
if (buf === EOF) {
return EOF;
if (buf === Deno.EOF) {
return Deno.EOF;
} else if (buf[0] == charCode(" ") || buf[0] == charCode("\t")) {
line = (await this.readLineSlice()) as Uint8Array;
}
buf = await this.r.peek(1);
if (buf === EOF) {
if (buf === Deno.EOF) {
throw new UnexpectedEOFError();
} else if (buf[0] == charCode(" ") || buf[0] == charCode("\t")) {
throw new ProtocolError(
@ -88,7 +88,7 @@ export class TextProtoReader {
while (true) {
let kv = await this.readLineSlice(); // readContinuedLineSlice
if (kv === EOF) throw new UnexpectedEOFError();
if (kv === Deno.EOF) throw new UnexpectedEOFError();
if (kv.byteLength === 0) return m;
// Key ends at first colon; should not have trailing spaces
@ -133,12 +133,12 @@ export class TextProtoReader {
}
}
async readLineSlice(): Promise<Uint8Array | EOF> {
async readLineSlice(): Promise<Uint8Array | Deno.EOF> {
// this.closeDot();
let line: Uint8Array;
while (true) {
const r = await this.r.readLine();
if (r === EOF) return EOF;
if (r === Deno.EOF) return Deno.EOF;
const { line: l, more } = r;
// Avoid the copy if the first call produced a full line.

View file

@ -3,7 +3,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
import { BufReader, EOF } from "../io/bufio.ts";
import { BufReader } from "../io/bufio.ts";
import { TextProtoReader, ProtocolError } from "./mod.ts";
import { stringsReader } from "../io/util.ts";
import {
@ -14,8 +14,8 @@ import {
} from "../testing/asserts.ts";
import { test, runIfMain } from "../testing/mod.ts";
function assertNotEOF<T extends {}>(val: T | EOF): T {
assertNotEquals(val, EOF);
function assertNotEOF<T extends {}>(val: T | Deno.EOF): T {
assertNotEquals(val, Deno.EOF);
return val as T;
}
@ -33,7 +33,7 @@ function reader(s: string): TextProtoReader {
test(async function textprotoReadEmpty(): Promise<void> {
const r = reader("");
const m = await r.readMIMEHeader();
assertEquals(m, EOF);
assertEquals(m, Deno.EOF);
});
test(async function textprotoReader(): Promise<void> {
@ -45,7 +45,7 @@ test(async function textprotoReader(): Promise<void> {
assertEquals(s, "line2");
s = await r.readLine();
assert(s === EOF);
assert(s === Deno.EOF);
});
test({

View file

@ -4,7 +4,7 @@ import { decode, encode } from "../strings/mod.ts";
type Conn = Deno.Conn;
type Writer = Deno.Writer;
import { BufReader, BufWriter, EOF, UnexpectedEOFError } from "../io/bufio.ts";
import { BufReader, BufWriter, UnexpectedEOFError } from "../io/bufio.ts";
import { readLong, readShort, sliceLongToBytes } from "../io/ioutil.ts";
import { Sha1 } from "./sha1.ts";
import { writeResponse } from "../http/server.ts";
@ -142,7 +142,7 @@ export async function writeFrame(
/** Read websocket frame from given BufReader */
export async function readFrame(buf: BufReader): Promise<WebSocketFrame> {
let b = await buf.readByte();
if (b === EOF) throw new UnexpectedEOFError();
if (b === Deno.EOF) throw new UnexpectedEOFError();
let isLastFrame = false;
switch (b >>> 4) {
case 0b1000:
@ -157,16 +157,16 @@ export async function readFrame(buf: BufReader): Promise<WebSocketFrame> {
const opcode = b & 0x0f;
// has_mask & payload
b = await buf.readByte();
if (b === EOF) throw new UnexpectedEOFError();
if (b === Deno.EOF) throw new UnexpectedEOFError();
const hasMask = b >>> 7;
let payloadLength = b & 0b01111111;
if (payloadLength === 126) {
const l = await readShort(buf);
if (l === EOF) throw new UnexpectedEOFError();
if (l === Deno.EOF) throw new UnexpectedEOFError();
payloadLength = l;
} else if (payloadLength === 127) {
const l = await readLong(buf);
if (l === EOF) throw new UnexpectedEOFError();
if (l === Deno.EOF) throw new UnexpectedEOFError();
payloadLength = Number(l);
}
// mask
@ -442,7 +442,7 @@ async function handshake(
const tpReader = new TextProtoReader(bufReader);
const statusLine = await tpReader.readLine();
if (statusLine === EOF) {
if (statusLine === Deno.EOF) {
throw new UnexpectedEOFError();
}
const m = statusLine.match(/^(?<version>\S+) (?<statusCode>\S+) /);
@ -460,7 +460,7 @@ async function handshake(
}
const responseHeaders = await tpReader.readMIMEHeader();
if (responseHeaders === EOF) {
if (responseHeaders === Deno.EOF) {
throw new UnexpectedEOFError();
}