Skip to content

Commit

Permalink
fix: move EOF to window
Browse files Browse the repository at this point in the history
  • Loading branch information
kt3k committed Jul 4, 2019
1 parent b3d939f commit 828cf3a
Show file tree
Hide file tree
Showing 15 changed files with 60 additions and 46 deletions.
13 changes: 7 additions & 6 deletions js/buffer.ts
Expand Up @@ -4,7 +4,8 @@
// Copyright 2009 The Go Authors. All rights reserved. BSD license.
// https://github.com/golang/go/blob/master/LICENSE

import { Reader, Writer, EOF, SyncReader, SyncWriter } from "./io";
import * as eof from "./eof";
import { Reader, Writer, SyncReader, SyncWriter } from "./io";
import { assert } from "./util";
import { TextDecoder } from "./text_encoding";
import { DenoError, ErrorKind } from "./errors";
Expand Down Expand Up @@ -130,22 +131,22 @@ export class Buffer implements Reader, SyncReader, Writer, SyncWriter {
* 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.
*/
readSync(p: Uint8Array): number | EOF {
readSync(p: Uint8Array): number | eof.EOF {
if (this.empty()) {
// Buffer is empty, reset to recover space.
this.reset();
if (p.byteLength === 0) {
// this edge case is tested in 'bufferReadEmptyAtEOF' test
return 0;
}
return EOF;
return eof.EOF;
}
const nread = copyBytes(p, this.buf.subarray(this.off));
this.off += nread;
return nread;
}

async read(p: Uint8Array): Promise<number | EOF> {
async read(p: Uint8Array): Promise<number | eof.EOF> {
const rr = this.readSync(p);
return Promise.resolve(rr);
}
Expand Down Expand Up @@ -226,7 +227,7 @@ export class Buffer implements Reader, SyncReader, Writer, SyncWriter {
this._reslice(i);
const fub = new Uint8Array(this.buf.buffer, i);
const nread = await r.read(fub);
if (nread === EOF) {
if (nread === eof.EOF) {
return n;
}
this._reslice(i + nread);
Expand All @@ -247,7 +248,7 @@ export class Buffer implements Reader, SyncReader, Writer, SyncWriter {
this._reslice(i);
const fub = new Uint8Array(this.buf.buffer, i);
const nread = r.readSync(fub);
if (nread === EOF) {
if (nread === eof.EOF) {
return n;
}
this._reslice(i + nread);
Expand Down
4 changes: 2 additions & 2 deletions js/buffer_test.ts
Expand Up @@ -60,7 +60,7 @@ async function empty(buf: Buffer, s: string, fub: Uint8Array): Promise<void> {
check(buf, s);
while (true) {
const r = await buf.read(fub);
if (r === Deno.EOF) {
if (r === EOF) {
break;
}
s = s.slice(r);
Expand Down Expand Up @@ -217,7 +217,7 @@ test(async function bufferTestGrow(): Promise<void> {
const buf = new Buffer(xBytes.buffer as ArrayBuffer);
// If we read, this affects buf.off, which is good to test.
const result = await buf.read(tmp);
const nread = result === Deno.EOF ? 0 : result;
const nread = result === EOF ? 0 : result;
buf.grow(growLen);
const yBytes = repeat("y", growLen);
await buf.write(yBytes);
Expand Down
7 changes: 6 additions & 1 deletion js/deno.ts
Expand Up @@ -20,7 +20,6 @@ export {
OpenMode
} from "./files";
export {
EOF,
copy,
toAsyncIterator,
SeekMode,
Expand Down Expand Up @@ -83,6 +82,12 @@ export { build, platform, OperatingSystem, Arch } from "./build";
export { version } from "./version";
export const args: string[] = [];

/*
import * as eof from "./eof";
export const EOF = eof.EOF;
export type EOF = eof.EOF;
*/

// These are internal Deno APIs. We are marking them as internal so they do not
// appear in the runtime type library.
/** @internal */
Expand Down
3 changes: 3 additions & 0 deletions js/eof.ts
@@ -0,0 +1,3 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
export const EOF: unique symbol = Symbol("EOF");
export type EOF = typeof EOF;
3 changes: 2 additions & 1 deletion js/fetch.ts
Expand Up @@ -7,6 +7,7 @@ import * as domTypes from "./dom_types";
import { TextDecoder, TextEncoder } from "./text_encoding";
import { DenoBlob, bytesSymbol as blobBytesSymbol } from "./blob";
import { Headers } from "./headers";
import * as eof from "./eof";
import * as io from "./io";
import { read, close } from "./files";
import { Buffer } from "./buffer";
Expand Down Expand Up @@ -218,7 +219,7 @@ class Body implements domTypes.Body, domTypes.ReadableStream, io.ReadCloser {
return decoder.decode(ab);
}

read(p: Uint8Array): Promise<number | io.EOF> {
read(p: Uint8Array): Promise<number | eof.EOF> {
return read(this.rid, p);
}

Expand Down
19 changes: 11 additions & 8 deletions js/files.ts
@@ -1,6 +1,6 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import * as eof from "./eof";
import {
EOF,
Reader,
Writer,
Seeker,
Expand Down Expand Up @@ -71,13 +71,13 @@ function reqRead(
return [builder, msg.Any.Read, inner, p];
}

function resRead(baseRes: null | msg.Base): number | EOF {
function resRead(baseRes: null | msg.Base): number | eof.EOF {
assert(baseRes != null);
assert(msg.Any.ReadRes === baseRes!.innerType());
const res = new msg.ReadRes();
assert(baseRes!.inner(res) != null);
if (res.eof()) {
return EOF;
return eof.EOF;
}
return res.nread();
}
Expand All @@ -92,7 +92,7 @@ function resRead(baseRes: null | msg.Base): number | EOF {
* const text = new TextDecoder().decode(buf);
*
*/
export function readSync(rid: number, p: Uint8Array): number | EOF {
export function readSync(rid: number, p: Uint8Array): number | eof.EOF {
return resRead(dispatch.sendSync(...reqRead(rid, p)));
}

Expand All @@ -107,12 +107,15 @@ export function readSync(rid: number, p: Uint8Array): number | EOF {
* const text = new TextDecoder().decode(buf);
* })();
*/
export async function read(rid: number, p: Uint8Array): Promise<number | EOF> {
export async function read(
rid: number,
p: Uint8Array
): Promise<number | eof.EOF> {
const nread = await sendAsyncMinimal(OP_READ, rid, p);
if (nread < 0) {
throw new Error("read error");
} else if (nread == 0) {
return EOF;
return eof.EOF;
} else {
return nread;
}
Expand Down Expand Up @@ -230,11 +233,11 @@ export class File
return writeSync(this.rid, p);
}

read(p: Uint8Array): Promise<number | EOF> {
read(p: Uint8Array): Promise<number | eof.EOF> {
return read(this.rid, p);
}

readSync(p: Uint8Array): number | EOF {
readSync(p: Uint8Array): number | eof.EOF {
return readSync(this.rid, p);
}

Expand Down
4 changes: 2 additions & 2 deletions js/files_test.ts
Expand Up @@ -39,13 +39,13 @@ test(async function readerToAsyncIterator(): Promise<void> {

constructor(private readonly s: string) {}

async read(p: Uint8Array): Promise<number | Deno.EOF> {
async read(p: Uint8Array): Promise<number | EOF> {
const n = Math.min(p.byteLength, this.buf.byteLength - this.offset);
p.set(this.buf.slice(this.offset, this.offset + n));
this.offset += n;

if (n === 0) {
return Deno.EOF;
return EOF;
}

return n;
Expand Down
4 changes: 4 additions & 0 deletions js/globals.ts
Expand Up @@ -16,6 +16,7 @@ import * as deno from "./deno";
import * as domTypes from "./dom_types";
import * as domFile from "./dom_file";
import * as event from "./event";
import * as eof from "./eof";
import * as eventTarget from "./event_target";
import * as formData from "./form_data";
import * as fetchTypes from "./fetch";
Expand Down Expand Up @@ -157,3 +158,6 @@ export interface Crypto {
typedArray: T
) => T;
}

window.EOF = eof.EOF;
export type EOF = eof.EOF;
11 changes: 5 additions & 6 deletions js/io.ts
Expand Up @@ -3,8 +3,7 @@
// Documentation liberally lifted from them too.
// Thank you! We love Go!

export const EOF: unique symbol = Symbol("EOF");
export type EOF = typeof EOF;
import * as eof from "./eof";

// Seek whence values.
// https://golang.org/pkg/io/#pkg-constants
Expand Down Expand Up @@ -34,11 +33,11 @@ export interface Reader {
*
* Implementations must not retain `p`.
*/
read(p: Uint8Array): Promise<number | EOF>;
read(p: Uint8Array): Promise<number | eof.EOF>;
}

export interface SyncReader {
readSync(p: Uint8Array): number | EOF;
readSync(p: Uint8Array): number | eof.EOF;
}

// Writer is the interface that wraps the basic write() method.
Expand Down Expand Up @@ -116,7 +115,7 @@ export async function copy(dst: Writer, src: Reader): Promise<number> {
let gotEOF = false;
while (gotEOF === false) {
const result = await src.read(b);
if (result === EOF) {
if (result === eof.EOF) {
gotEOF = true;
} else {
n += await dst.write(b.subarray(0, result));
Expand Down Expand Up @@ -153,7 +152,7 @@ export function toAsyncIterator(r: Reader): AsyncIterableIterator<Uint8Array> {
}

const result = await r.read(b);
if (result === EOF) {
if (result === eof.EOF) {
sawEof = true;
return { value: new Uint8Array(), done: true };
}
Expand Down
5 changes: 3 additions & 2 deletions js/net.ts
@@ -1,5 +1,6 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import { EOF, Reader, Writer, Closer } from "./io";
import * as eof from "./eof";
import { Reader, Writer, Closer } from "./io";
import * as msg from "gen/cli/msg_generated";
import { assert, notImplemented } from "./util";
import * as dispatch from "./dispatch";
Expand Down Expand Up @@ -55,7 +56,7 @@ class ConnImpl implements Conn {
return write(this.rid, p);
}

read(p: Uint8Array): Promise<number | EOF> {
read(p: Uint8Array): Promise<number | eof.EOF> {
return read(this.rid, p);
}

Expand Down
10 changes: 5 additions & 5 deletions js/net_test.ts
Expand Up @@ -59,10 +59,10 @@ testPerm({ net: true }, async function netDialListen(): Promise<void> {
assertEquals(3, buf[2]);
assert(conn.rid > 0);

assert(readResult !== Deno.EOF);
assert(readResult !== EOF);

const readResult2 = await conn.read(buf);
assertEquals(Deno.EOF, readResult2);
assertEquals(EOF, readResult2);

listener.close();
conn.close();
Expand All @@ -87,10 +87,10 @@ testPerm({ net: true }, async function netListenAsyncIterator(): Promise<void> {
assertEquals(3, buf[2]);
assert(conn.rid > 0);
assert(readResult !== Deno.EOF);
assert(readResult !== EOF);
const readResult2 = await conn.read(buf);
assertEquals(Deno.EOF, readResult2);
assertEquals(EOF, readResult2);
listener.close();
conn.close();
Expand Down Expand Up @@ -120,7 +120,7 @@ testPerm({ net: true }, async function netCloseReadSuccess() {
closeReadDeferred.resolve();
const buf = new Uint8Array(1024);
const readResult = await conn.read(buf);
assertEquals(Deno.EOF, readResult); // with immediate EOF
assertEquals(EOF, readResult); // with immediate EOF
// Ensure closeRead does not impact write
await conn.write(new Uint8Array([4, 5, 6]));
await closeDeferred.promise;
Expand Down
10 changes: 5 additions & 5 deletions js/process_test.ts
Expand Up @@ -156,14 +156,14 @@ testPerm({ run: true }, async function runStdoutPiped(): Promise<void> {

const data = new Uint8Array(10);
let r = await p.stdout.read(data);
if (r === Deno.EOF) {
if (r === EOF) {
throw new Error("p.stdout.read(...) should not be EOF");
}
assertEquals(r, 5);
const s = new TextDecoder().decode(data.subarray(0, r));
assertEquals(s, "hello");
r = await p.stdout.read(data);
assertEquals(r, Deno.EOF);
assertEquals(r, EOF);
p.stdout.close();

const status = await p.status();
Expand All @@ -183,14 +183,14 @@ testPerm({ run: true }, async function runStderrPiped(): Promise<void> {

const data = new Uint8Array(10);
let r = await p.stderr.read(data);
if (r === Deno.EOF) {
if (r === EOF) {
throw new Error("p.stderr.read should not return EOF here");
}
assertEquals(r, 5);
const s = new TextDecoder().decode(data.subarray(0, r));
assertEquals(s, "hello");
r = await p.stderr.read(data);
assertEquals(r, Deno.EOF);
assertEquals(r, EOF);
p.stderr.close();

const status = await p.status();
Expand Down Expand Up @@ -308,7 +308,7 @@ testPerm({ run: true }, async function runClose(): Promise<void> {

const data = new Uint8Array(10);
let r = await p.stderr.read(data);
assertEquals(r, Deno.EOF);
assertEquals(r, EOF);
});

test(function signalNumbers(): void {
Expand Down
7 changes: 4 additions & 3 deletions js/xeval.ts
@@ -1,7 +1,8 @@
import { Buffer } from "./buffer";
import { stdin } from "./files";
import { TextEncoder, TextDecoder } from "./text_encoding";
import { Reader, EOF } from "./io";
import { Reader } from "./io";
import * as eof from "./eof";

export type XevalFunc = (v: string) => void;

Expand Down Expand Up @@ -36,7 +37,7 @@ async function* chunks(
let nextMatchIndex = 0;
while (true) {
let result = await reader.read(inspectArr);
let rr = result === EOF ? 0 : result;
let rr = result === eof.EOF ? 0 : result;
if (rr < 0) {
// Silently fail.
break;
Expand Down Expand Up @@ -75,7 +76,7 @@ async function* chunks(
}
// Write all unprocessed chunk to buffer for future inspection.
await writeAll(inputBuffer, sliceRead.subarray(nextSliceStartIndex));
if (result === EOF) {
if (result === eof.EOF) {
// Flush the remainder unprocessed chunk.
const lastChunk = inputBuffer.toString();
yield lastChunk;
Expand Down
4 changes: 0 additions & 4 deletions tools/deno_http_proxy.ts
Expand Up @@ -21,10 +21,6 @@ async function proxyRequest(req: ServerRequest) {
method: req.method,
headers: req.headers
});
// TODO(kt3k): lib.deno_runtime.d.ts has 2 EOF types: Deno.EOF and io.EOF.
// They are identical symbols, and should be compatible. However typescript
// recognizes they are different types and the below call doesn't compile.
// @ts-ignore
req.respond(resp);
}

Expand Down
2 changes: 1 addition & 1 deletion tools/deno_tcp.ts
Expand Up @@ -13,7 +13,7 @@ async function handle(conn: Deno.Conn): Promise<void> {
try {
while (true) {
const r = await conn.read(buffer);
if (r === Deno.EOF) {
if (r === EOF) {
break;
}
await conn.write(response);
Expand Down

0 comments on commit 828cf3a

Please sign in to comment.