diff options
Diffstat (limited to 'std/io')
-rw-r--r-- | std/io/bufio.ts | 54 | ||||
-rw-r--r-- | std/io/bufio_test.ts | 59 | ||||
-rw-r--r-- | std/io/iotest.ts | 6 | ||||
-rw-r--r-- | std/io/ioutil.ts | 22 | ||||
-rw-r--r-- | std/io/ioutil_test.ts | 2 | ||||
-rw-r--r-- | std/io/readers.ts | 10 | ||||
-rw-r--r-- | std/io/readers_test.ts | 4 |
7 files changed, 82 insertions, 75 deletions
diff --git a/std/io/bufio.ts b/std/io/bufio.ts index cd08fbefa..9a22c7f3e 100644 --- a/std/io/bufio.ts +++ b/std/io/bufio.ts @@ -83,7 +83,7 @@ export class BufReader implements Reader { // Read new data: try a limited number of times. for (let i = MAX_CONSECUTIVE_EMPTY_READS; i > 0; i--) { const rr = await this.rd.read(this.buf.subarray(this.w)); - if (rr === Deno.EOF) { + if (rr === null) { this.eof = true; return; } @@ -120,8 +120,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<number | Deno.EOF> { - let rr: number | Deno.EOF = p.byteLength; + async read(p: Uint8Array): Promise<number | null> { + let rr: number | null = p.byteLength; if (p.byteLength === 0) return rr; if (this.r === this.w) { @@ -129,7 +129,7 @@ export class BufReader implements Reader { // Large read, empty buffer. // Read directly into p to avoid copy. const rr = await this.rd.read(p); - const nread = rr === Deno.EOF ? 0 : rr; + const nread = rr ?? 0; assert(nread >= 0, "negative read"); // if (rr.nread > 0) { // this.lastByte = p[rr.nread - 1]; @@ -143,7 +143,7 @@ export class BufReader implements Reader { this.r = 0; this.w = 0; rr = await this.rd.read(this.buf); - if (rr === 0 || rr === Deno.EOF) return rr; + if (rr === 0 || rr === null) return rr; assert(rr >= 0, "negative read"); this.w += rr; } @@ -161,7 +161,7 @@ export class BufReader implements Reader { * If successful, `p` is returned. * * If the end of the underlying stream has been reached, and there are no more - * bytes available in the buffer, `readFull()` returns `EOF` instead. + * bytes available in the buffer, `readFull()` returns `null` instead. * * 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 @@ -170,14 +170,14 @@ export class BufReader implements Reader { * * Ported from https://golang.org/pkg/io/#ReadFull */ - async readFull(p: Uint8Array): Promise<Uint8Array | Deno.EOF> { + async readFull(p: Uint8Array): Promise<Uint8Array | null> { let bytesRead = 0; while (bytesRead < p.length) { try { const rr = await this.read(p.subarray(bytesRead)); - if (rr === Deno.EOF) { + if (rr === null) { if (bytesRead === 0) { - return Deno.EOF; + return null; } else { throw new PartialReadError(); } @@ -191,10 +191,10 @@ export class BufReader implements Reader { return p; } - /** Returns the next byte [0, 255] or `EOF`. */ - async readByte(): Promise<number | Deno.EOF> { + /** Returns the next byte [0, 255] or `null`. */ + async readByte(): Promise<number | null> { while (this.r === this.w) { - if (this.eof) return Deno.EOF; + if (this.eof) return null; await this._fill(); // buffer is empty. } const c = this.buf[this.r]; @@ -207,17 +207,17 @@ export class BufReader implements Reader { * returning a string containing the data up to and including the delimiter. * If ReadString encounters an error before finding a delimiter, * it returns the data read before the error and the error itself - * (often io.EOF). + * (often `null`). * ReadString returns err != nil if and only if the returned data does not end * in delim. * For simple uses, a Scanner may be more convenient. */ - async readString(delim: string): Promise<string | Deno.EOF> { + async readString(delim: string): Promise<string | null> { if (delim.length !== 1) { throw new Error("Delimiter should be a single character"); } const buffer = await this.readSlice(delim.charCodeAt(0)); - if (buffer == Deno.EOF) return Deno.EOF; + if (buffer === null) return null; return new TextDecoder().decode(buffer); } @@ -237,14 +237,14 @@ export class BufReader implements Reader { * 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, - * `readLine()` returns the `EOF` symbol. + * `readLine()` returns `null`. * * 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()`. */ - async readLine(): Promise<ReadLineResult | Deno.EOF> { - let line: Uint8Array | Deno.EOF; + async readLine(): Promise<ReadLineResult | null> { + let line: Uint8Array | null; try { line = await this.readSlice(LF); @@ -277,8 +277,8 @@ export class BufReader implements Reader { return { line: partial, more: !this.eof }; } - if (line === Deno.EOF) { - return Deno.EOF; + if (line === null) { + return null; } if (line.byteLength === 0) { @@ -306,12 +306,12 @@ export class BufReader implements Reader { * 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, - * it returns `EOF`. + * it returns `null`. * * 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 | Deno.EOF> { + async readSlice(delim: number): Promise<Uint8Array | null> { let s = 0; // search start index let slice: Uint8Array | undefined; @@ -328,7 +328,7 @@ export class BufReader implements Reader { // EOF? if (this.eof) { if (this.r === this.w) { - return Deno.EOF; + return null; } slice = this.buf.subarray(this.r, this.w); this.r = this.w; @@ -367,13 +367,13 @@ export class BufReader implements Reader { * * 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 - * left in the buffer, it returns `EOF`. + * left in the buffer, it returns `null`. * * 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. */ - async peek(n: number): Promise<Uint8Array | Deno.EOF> { + async peek(n: number): Promise<Uint8Array | null> { if (n < 0) { throw Error("negative count"); } @@ -390,7 +390,7 @@ export class BufReader implements Reader { } if (avail === 0 && this.eof) { - return Deno.EOF; + return null; } else if (avail < n && this.eof) { return this.buf.subarray(this.r, this.r + avail); } else if (avail < n) { @@ -656,7 +656,7 @@ export async function* readDelim( let matchIndex = 0; while (true) { const result = await reader.read(inspectArr); - if (result === Deno.EOF) { + if (result === null) { // Yield last chunk. yield inputBuffer.bytes(); return; diff --git a/std/io/bufio_test.ts b/std/io/bufio_test.ts index 7e737e60f..a4f03338d 100644 --- a/std/io/bufio_test.ts +++ b/std/io/bufio_test.ts @@ -5,12 +5,7 @@ const { Buffer } = Deno; type Reader = Deno.Reader; -import { - assert, - assertEquals, - fail, - assertNotEOF, -} from "../testing/asserts.ts"; +import { assert, assertEquals, fail } from "../testing/asserts.ts"; import { BufReader, BufWriter, @@ -30,7 +25,7 @@ async function readBytes(buf: BufReader): Promise<string> { let nb = 0; while (true) { const c = await buf.readByte(); - if (c === Deno.EOF) { + if (c === null) { break; // EOF } b[nb] = c; @@ -69,7 +64,7 @@ async function reads(buf: BufReader, m: number): Promise<string> { let nb = 0; while (true) { const result = await buf.read(b.subarray(nb, nb + m)); - if (result === Deno.EOF) { + if (result === null) { break; } nb += result; @@ -152,7 +147,8 @@ Deno.test("bufioBufferFull", async function (): Promise<void> { assertEquals(decoder.decode(err.partial), "And now, hello, "); } - const line = assertNotEOF(await buf.readSlice(charCode("!"))); + const line = await buf.readSlice(charCode("!")); + assert(line !== null); const actual = decoder.decode(line); assertEquals(actual, "world!"); }); @@ -161,14 +157,16 @@ Deno.test("bufioReadString", async function (): Promise<void> { const string = "And now, hello world!"; const buf = new BufReader(stringsReader(string), MIN_READ_BUFFER_SIZE); - const line = assertNotEOF(await buf.readString(",")); + const line = await buf.readString(","); + assert(line !== null); assertEquals(line, "And now,"); assertEquals(line.length, 8); - const line2 = assertNotEOF(await buf.readString(",")); + const line2 = await buf.readString(","); + assert(line2 !== null); assertEquals(line2, " hello world!"); - assertEquals(await buf.readString(","), Deno.EOF); + assertEquals(await buf.readString(","), null); try { await buf.readString("deno"); @@ -192,7 +190,7 @@ const testOutput = encoder.encode("0123456789abcdefghijklmnopqrstuvwxy"); class TestReader implements Reader { constructor(private data: Uint8Array, private stride: number) {} - read(buf: Uint8Array): Promise<number | Deno.EOF> { + read(buf: Uint8Array): Promise<number | null> { let nread = this.stride; if (nread > this.data.byteLength) { nread = this.data.byteLength; @@ -201,7 +199,7 @@ class TestReader implements Reader { nread = buf.byteLength; } if (nread === 0) { - return Promise.resolve(Deno.EOF); + return Promise.resolve(null); } copyBytes(buf as Uint8Array, this.data); this.data = this.data.subarray(nread); @@ -216,7 +214,7 @@ async function testReadLine(input: Uint8Array): Promise<void> { const l = new BufReader(reader, input.byteLength + 1); while (true) { const r = await l.readLine(); - if (r === Deno.EOF) { + if (r === null) { break; } const { line, more } = r; @@ -253,10 +251,12 @@ Deno.test("bufioPeek", async function (): Promise<void> { MIN_READ_BUFFER_SIZE ); - let actual = assertNotEOF(await buf.peek(1)); + let actual = await buf.peek(1); + assert(actual !== null); assertEquals(decoder.decode(actual), "a"); - actual = assertNotEOF(await buf.peek(4)); + actual = await buf.peek(4); + assert(actual !== null); assertEquals(decoder.decode(actual), "abcd"); try { @@ -271,33 +271,39 @@ Deno.test("bufioPeek", async function (): Promise<void> { await buf.read(p.subarray(0, 3)); assertEquals(decoder.decode(p.subarray(0, 3)), "abc"); - actual = assertNotEOF(await buf.peek(1)); + actual = await buf.peek(1); + assert(actual !== null); assertEquals(decoder.decode(actual), "d"); - actual = assertNotEOF(await buf.peek(1)); + actual = await buf.peek(1); + assert(actual !== null); assertEquals(decoder.decode(actual), "d"); - actual = assertNotEOF(await buf.peek(1)); + actual = await buf.peek(1); + assert(actual !== null); assertEquals(decoder.decode(actual), "d"); - actual = assertNotEOF(await buf.peek(2)); + actual = await buf.peek(2); + assert(actual !== null); assertEquals(decoder.decode(actual), "de"); const res = await buf.read(p.subarray(0, 3)); assertEquals(decoder.decode(p.subarray(0, 3)), "def"); - assert(res !== Deno.EOF); + assert(res !== null); - actual = assertNotEOF(await buf.peek(4)); + actual = await buf.peek(4); + assert(actual !== null); assertEquals(decoder.decode(actual), "ghij"); await buf.read(p); assertEquals(decoder.decode(p), "ghijklmnop"); - actual = assertNotEOF(await buf.peek(0)); + actual = await buf.peek(0); + assert(actual !== null); assertEquals(decoder.decode(actual), ""); const r = await buf.peek(1); - assert(r === Deno.EOF); + assert(r === null); /* TODO Test for issue 3022, not exposing a reader's error on a successful Peek. buf = NewReaderSize(dataAndEOFReader("abcd"), 32) @@ -396,7 +402,8 @@ Deno.test("bufReaderReadFull", async function (): Promise<void> { const bufr = new BufReader(data, 3); { const buf = new Uint8Array(6); - const r = assertNotEOF(await bufr.readFull(buf)); + const r = await bufr.readFull(buf); + assert(r !== null); assertEquals(r, buf); assertEquals(dec.decode(buf), "Hello "); } diff --git a/std/io/iotest.ts b/std/io/iotest.ts index e2cf315aa..a309fb5e1 100644 --- a/std/io/iotest.ts +++ b/std/io/iotest.ts @@ -10,7 +10,7 @@ type Reader = Deno.Reader; export class OneByteReader implements Reader { constructor(readonly r: Reader) {} - read(p: Uint8Array): Promise<number | Deno.EOF> { + read(p: Uint8Array): Promise<number | null> { if (p.byteLength === 0) { return Promise.resolve(0); } @@ -27,7 +27,7 @@ export class OneByteReader implements Reader { export class HalfReader implements Reader { constructor(readonly r: Reader) {} - read(p: Uint8Array): Promise<number | Deno.EOF> { + read(p: Uint8Array): Promise<number | null> { if (!(p instanceof Uint8Array)) { throw Error("expected Uint8Array"); } @@ -43,7 +43,7 @@ export class TimeoutReader implements Reader { count = 0; constructor(readonly r: Reader) {} - read(p: Uint8Array): Promise<number | Deno.EOF> { + read(p: Uint8Array): Promise<number | null> { this.count++; if (this.count === 2) { throw new Deno.errors.TimedOut(); diff --git a/std/io/ioutil.ts b/std/io/ioutil.ts index 8c30ae566..7b6761708 100644 --- a/std/io/ioutil.ts +++ b/std/io/ioutil.ts @@ -21,13 +21,13 @@ export async function copyN( buf = new Uint8Array(size - bytesRead); } const result = await r.read(buf); - const nread = result === Deno.EOF ? 0 : result; + const nread = result ?? 0; bytesRead += nread; if (nread > 0) { const n = await dest.write(buf.slice(0, nread)); assert(n === nread, "could not write"); } - if (result === Deno.EOF) { + if (result === null) { break; } } @@ -35,31 +35,31 @@ export async function copyN( } /** Read big endian 16bit short from BufReader */ -export async function readShort(buf: BufReader): Promise<number | Deno.EOF> { +export async function readShort(buf: BufReader): Promise<number | null> { const high = await buf.readByte(); - if (high === Deno.EOF) return Deno.EOF; + if (high === null) return null; const low = await buf.readByte(); - if (low === Deno.EOF) throw new Deno.errors.UnexpectedEof(); + if (low === null) throw new Deno.errors.UnexpectedEof(); return (high << 8) | low; } /** Read big endian 32bit integer from BufReader */ -export async function readInt(buf: BufReader): Promise<number | Deno.EOF> { +export async function readInt(buf: BufReader): Promise<number | null> { const high = await readShort(buf); - if (high === Deno.EOF) return Deno.EOF; + if (high === null) return null; const low = await readShort(buf); - if (low === Deno.EOF) throw new Deno.errors.UnexpectedEof(); + if (low === null) throw new Deno.errors.UnexpectedEof(); 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 | Deno.EOF> { +export async function readLong(buf: BufReader): Promise<number | null> { const high = await readInt(buf); - if (high === Deno.EOF) return Deno.EOF; + if (high === null) return null; const low = await readInt(buf); - if (low === Deno.EOF) throw new Deno.errors.UnexpectedEof(); + if (low === null) throw new Deno.errors.UnexpectedEof(); const big = (BigInt(high) << 32n) | BigInt(low); // We probably should provide a similar API that returns BigInt values. if (big > MAX_SAFE_INTEGER) { diff --git a/std/io/ioutil_test.ts b/std/io/ioutil_test.ts index 5e4d3e3d2..4d7d0f4ca 100644 --- a/std/io/ioutil_test.ts +++ b/std/io/ioutil_test.ts @@ -17,7 +17,7 @@ class BinaryReader implements Reader { constructor(private bytes: Uint8Array = new Uint8Array(0)) {} - read(p: Uint8Array): Promise<number | Deno.EOF> { + read(p: Uint8Array): Promise<number | null> { p.set(this.bytes.subarray(this.index, p.byteLength)); this.index += p.byteLength; return Promise.resolve(p.byteLength); diff --git a/std/io/readers.ts b/std/io/readers.ts index 8a567d7d1..10069986c 100644 --- a/std/io/readers.ts +++ b/std/io/readers.ts @@ -9,12 +9,12 @@ export class StringReader implements Reader { constructor(private readonly s: string) {} - read(p: Uint8Array): Promise<number | Deno.EOF> { + read(p: Uint8Array): Promise<number | null> { const n = Math.min(p.byteLength, this.buf.byteLength - this.offs); p.set(this.buf.slice(this.offs, this.offs + n)); this.offs += n; if (n === 0) { - return Promise.resolve(Deno.EOF); + return Promise.resolve(null); } return Promise.resolve(n); } @@ -29,11 +29,11 @@ export class MultiReader implements Reader { this.readers = readers; } - async read(p: Uint8Array): Promise<number | Deno.EOF> { + async read(p: Uint8Array): Promise<number | null> { const r = this.readers[this.currentIndex]; - if (!r) return Deno.EOF; + if (!r) return null; const result = await r.read(p); - if (result === Deno.EOF) { + if (result === null) { this.currentIndex++; return 0; } diff --git a/std/io/readers_test.ts b/std/io/readers_test.ts index c94285581..b0810f9e0 100644 --- a/std/io/readers_test.ts +++ b/std/io/readers_test.ts @@ -10,7 +10,7 @@ test("ioStringReader", async function (): Promise<void> { const res0 = await r.read(new Uint8Array(6)); assertEquals(res0, 6); const res1 = await r.read(new Uint8Array(6)); - assertEquals(res1, Deno.EOF); + assertEquals(res1, null); }); test("ioStringReader", async function (): Promise<void> { @@ -23,7 +23,7 @@ test("ioStringReader", async function (): Promise<void> { assertEquals(res2, 3); assertEquals(decode(buf), "def"); const res3 = await r.read(buf); - assertEquals(res3, Deno.EOF); + assertEquals(res3, null); assertEquals(decode(buf), "def"); }); |