Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] io: update Reader interface #527

Merged
merged 6 commits into from
Jul 7, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions archive/tar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
8 changes: 4 additions & 4 deletions encoding/csv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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 (
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions http/file_server_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions http/racing_server_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down
34 changes: 18 additions & 16 deletions http/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -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);
}
Expand Down Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -324,7 +326,7 @@ export class Server implements AsyncIterable<ServerRequest> {
err = e;
break;
}
if (req === EOF) {
if (req === Deno.EOF) {
break;
}

Expand All @@ -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.
Expand Down
15 changes: 7 additions & 8 deletions http/server_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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> {
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions installer/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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 {
Expand Down
Loading