From 828cf3a730365818224ab53d75285f4e1b3c3e74 Mon Sep 17 00:00:00 2001 From: Yoshiya Hinosawa Date: Thu, 4 Jul 2019 10:33:57 +0900 Subject: [PATCH] fix: move EOF to window --- js/buffer.ts | 13 +++++++------ js/buffer_test.ts | 4 ++-- js/deno.ts | 7 ++++++- js/eof.ts | 3 +++ js/fetch.ts | 3 ++- js/files.ts | 19 +++++++++++-------- js/files_test.ts | 4 ++-- js/globals.ts | 4 ++++ js/io.ts | 11 +++++------ js/net.ts | 5 +++-- js/net_test.ts | 10 +++++----- js/process_test.ts | 10 +++++----- js/xeval.ts | 7 ++++--- tools/deno_http_proxy.ts | 4 ---- tools/deno_tcp.ts | 2 +- 15 files changed, 60 insertions(+), 46 deletions(-) create mode 100644 js/eof.ts diff --git a/js/buffer.ts b/js/buffer.ts index 1f597282d94b65..1cf49abcf1f9bc 100644 --- a/js/buffer.ts +++ b/js/buffer.ts @@ -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"; @@ -130,7 +131,7 @@ 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(); @@ -138,14 +139,14 @@ export class Buffer implements Reader, SyncReader, Writer, SyncWriter { // 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 { + async read(p: Uint8Array): Promise { const rr = this.readSync(p); return Promise.resolve(rr); } @@ -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); @@ -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); diff --git a/js/buffer_test.ts b/js/buffer_test.ts index 3b2f5d31297b62..a5d905d8494e08 100644 --- a/js/buffer_test.ts +++ b/js/buffer_test.ts @@ -60,7 +60,7 @@ async function empty(buf: Buffer, s: string, fub: Uint8Array): Promise { check(buf, s); while (true) { const r = await buf.read(fub); - if (r === Deno.EOF) { + if (r === EOF) { break; } s = s.slice(r); @@ -217,7 +217,7 @@ test(async function bufferTestGrow(): Promise { 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); diff --git a/js/deno.ts b/js/deno.ts index 44d1098a5438f6..a66fb516b37cba 100644 --- a/js/deno.ts +++ b/js/deno.ts @@ -20,7 +20,6 @@ export { OpenMode } from "./files"; export { - EOF, copy, toAsyncIterator, SeekMode, @@ -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 */ diff --git a/js/eof.ts b/js/eof.ts new file mode 100644 index 00000000000000..5697fb789e397c --- /dev/null +++ b/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; diff --git a/js/fetch.ts b/js/fetch.ts index 9853b2194144d8..6cc24852c48525 100644 --- a/js/fetch.ts +++ b/js/fetch.ts @@ -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"; @@ -218,7 +219,7 @@ class Body implements domTypes.Body, domTypes.ReadableStream, io.ReadCloser { return decoder.decode(ab); } - read(p: Uint8Array): Promise { + read(p: Uint8Array): Promise { return read(this.rid, p); } diff --git a/js/files.ts b/js/files.ts index eb899d738d0a00..c58460eb07da1b 100644 --- a/js/files.ts +++ b/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, @@ -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(); } @@ -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))); } @@ -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 { +export async function read( + rid: number, + p: Uint8Array +): Promise { 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; } @@ -230,11 +233,11 @@ export class File return writeSync(this.rid, p); } - read(p: Uint8Array): Promise { + read(p: Uint8Array): Promise { return read(this.rid, p); } - readSync(p: Uint8Array): number | EOF { + readSync(p: Uint8Array): number | eof.EOF { return readSync(this.rid, p); } diff --git a/js/files_test.ts b/js/files_test.ts index babec1fc2bcb68..f31642e2ebbd5e 100644 --- a/js/files_test.ts +++ b/js/files_test.ts @@ -39,13 +39,13 @@ test(async function readerToAsyncIterator(): Promise { constructor(private readonly s: string) {} - async read(p: Uint8Array): Promise { + async read(p: Uint8Array): Promise { 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; diff --git a/js/globals.ts b/js/globals.ts index 1d33f65233dbe0..5c9a2bcbf0d52f 100644 --- a/js/globals.ts +++ b/js/globals.ts @@ -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"; @@ -157,3 +158,6 @@ export interface Crypto { typedArray: T ) => T; } + +window.EOF = eof.EOF; +export type EOF = eof.EOF; diff --git a/js/io.ts b/js/io.ts index 15a4ad09bae392..c1d2f27c95b37b 100644 --- a/js/io.ts +++ b/js/io.ts @@ -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 @@ -34,11 +33,11 @@ export interface Reader { * * Implementations must not retain `p`. */ - read(p: Uint8Array): Promise; + read(p: Uint8Array): Promise; } export interface SyncReader { - readSync(p: Uint8Array): number | EOF; + readSync(p: Uint8Array): number | eof.EOF; } // Writer is the interface that wraps the basic write() method. @@ -116,7 +115,7 @@ export async function copy(dst: Writer, src: Reader): Promise { 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)); @@ -153,7 +152,7 @@ export function toAsyncIterator(r: Reader): AsyncIterableIterator { } const result = await r.read(b); - if (result === EOF) { + if (result === eof.EOF) { sawEof = true; return { value: new Uint8Array(), done: true }; } diff --git a/js/net.ts b/js/net.ts index f1470b86e69097..e211debf1888eb 100644 --- a/js/net.ts +++ b/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"; @@ -55,7 +56,7 @@ class ConnImpl implements Conn { return write(this.rid, p); } - read(p: Uint8Array): Promise { + read(p: Uint8Array): Promise { return read(this.rid, p); } diff --git a/js/net_test.ts b/js/net_test.ts index 10652136c1a6d5..08520d1f6b912c 100644 --- a/js/net_test.ts +++ b/js/net_test.ts @@ -59,10 +59,10 @@ testPerm({ net: true }, async function netDialListen(): Promise { 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(); @@ -87,10 +87,10 @@ testPerm({ net: true }, async function netListenAsyncIterator(): Promise { 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(); @@ -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; diff --git a/js/process_test.ts b/js/process_test.ts index 874f59a8171449..05faeb9a41ccfd 100644 --- a/js/process_test.ts +++ b/js/process_test.ts @@ -156,14 +156,14 @@ testPerm({ run: true }, async function runStdoutPiped(): Promise { 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(); @@ -183,14 +183,14 @@ testPerm({ run: true }, async function runStderrPiped(): Promise { 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(); @@ -308,7 +308,7 @@ testPerm({ run: true }, async function runClose(): Promise { const data = new Uint8Array(10); let r = await p.stderr.read(data); - assertEquals(r, Deno.EOF); + assertEquals(r, EOF); }); test(function signalNumbers(): void { diff --git a/js/xeval.ts b/js/xeval.ts index 81e79f590ab826..94ce24fd6d978e 100644 --- a/js/xeval.ts +++ b/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; @@ -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; @@ -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; diff --git a/tools/deno_http_proxy.ts b/tools/deno_http_proxy.ts index 3259098f353e86..0b775ad075d1ba 100644 --- a/tools/deno_http_proxy.ts +++ b/tools/deno_http_proxy.ts @@ -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); } diff --git a/tools/deno_tcp.ts b/tools/deno_tcp.ts index d744a09c1ccb5e..5b2f58ec16ba4c 100644 --- a/tools/deno_tcp.ts +++ b/tools/deno_tcp.ts @@ -13,7 +13,7 @@ async function handle(conn: Deno.Conn): Promise { try { while (true) { const r = await conn.read(buffer); - if (r === Deno.EOF) { + if (r === EOF) { break; } await conn.write(response);