mirror of
https://github.com/denoland/deno.git
synced 2025-07-24 05:35:33 +00:00
refactor: rename sync io interfaces (#4945)
This commit renames sync io interfaces: * SyncReader -> ReaderSync * SyncWriter -> WriterSync * SyncSeeker -> SeekerSync
This commit is contained in:
parent
b980b26d85
commit
2cb875bcfb
7 changed files with 46 additions and 46 deletions
|
@ -4,7 +4,7 @@
|
||||||
// Copyright 2009 The Go Authors. All rights reserved. BSD license.
|
// Copyright 2009 The Go Authors. All rights reserved. BSD license.
|
||||||
// https://github.com/golang/go/blob/master/LICENSE
|
// https://github.com/golang/go/blob/master/LICENSE
|
||||||
|
|
||||||
import { Reader, Writer, EOF, SyncReader, SyncWriter } from "./io.ts";
|
import { Reader, Writer, EOF, ReaderSync, WriterSync } from "./io.ts";
|
||||||
import { assert } from "./util.ts";
|
import { assert } from "./util.ts";
|
||||||
import { TextDecoder } from "./web/text_encoding.ts";
|
import { TextDecoder } from "./web/text_encoding.ts";
|
||||||
|
|
||||||
|
@ -27,7 +27,7 @@ function copyBytes(dst: Uint8Array, src: Uint8Array, off = 0): number {
|
||||||
return src.byteLength;
|
return src.byteLength;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Buffer implements Reader, SyncReader, Writer, SyncWriter {
|
export class Buffer implements Reader, ReaderSync, Writer, WriterSync {
|
||||||
#buf: Uint8Array; // contents are the bytes buf[off : len(buf)]
|
#buf: Uint8Array; // contents are the bytes buf[off : len(buf)]
|
||||||
#off = 0; // read at buf[off], write at buf[buf.byteLength]
|
#off = 0; // read at buf[off], write at buf[buf.byteLength]
|
||||||
|
|
||||||
|
@ -180,7 +180,7 @@ export class Buffer implements Reader, SyncReader, Writer, SyncWriter {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
readFromSync(r: SyncReader): number {
|
readFromSync(r: ReaderSync): number {
|
||||||
let n = 0;
|
let n = 0;
|
||||||
while (true) {
|
while (true) {
|
||||||
try {
|
try {
|
||||||
|
@ -206,7 +206,7 @@ export async function readAll(r: Reader): Promise<Uint8Array> {
|
||||||
return buf.bytes();
|
return buf.bytes();
|
||||||
}
|
}
|
||||||
|
|
||||||
export function readAllSync(r: SyncReader): Uint8Array {
|
export function readAllSync(r: ReaderSync): Uint8Array {
|
||||||
const buf = new Buffer();
|
const buf = new Buffer();
|
||||||
buf.readFromSync(r);
|
buf.readFromSync(r);
|
||||||
return buf.bytes();
|
return buf.bytes();
|
||||||
|
@ -219,7 +219,7 @@ export async function writeAll(w: Writer, arr: Uint8Array): Promise<void> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function writeAllSync(w: SyncWriter, arr: Uint8Array): void {
|
export function writeAllSync(w: WriterSync, arr: Uint8Array): void {
|
||||||
let nwritten = 0;
|
let nwritten = 0;
|
||||||
while (nwritten < arr.length) {
|
while (nwritten < arr.length) {
|
||||||
nwritten += w.writeSync(arr.subarray(nwritten));
|
nwritten += w.writeSync(arr.subarray(nwritten));
|
||||||
|
|
|
@ -46,9 +46,9 @@ export {
|
||||||
iterSync,
|
iterSync,
|
||||||
SeekMode,
|
SeekMode,
|
||||||
Reader,
|
Reader,
|
||||||
SyncReader,
|
ReaderSync,
|
||||||
Writer,
|
Writer,
|
||||||
SyncWriter,
|
WriterSync,
|
||||||
Closer,
|
Closer,
|
||||||
Seeker,
|
Seeker,
|
||||||
} from "./io.ts";
|
} from "./io.ts";
|
||||||
|
|
|
@ -6,9 +6,9 @@ import {
|
||||||
Seeker,
|
Seeker,
|
||||||
Closer,
|
Closer,
|
||||||
SeekMode,
|
SeekMode,
|
||||||
SyncReader,
|
ReaderSync,
|
||||||
SyncWriter,
|
WriterSync,
|
||||||
SyncSeeker,
|
SeekerSync,
|
||||||
} from "./io.ts";
|
} from "./io.ts";
|
||||||
import { close } from "./ops/resources.ts";
|
import { close } from "./ops/resources.ts";
|
||||||
import { read, readSync, write, writeSync } from "./ops/io.ts";
|
import { read, readSync, write, writeSync } from "./ops/io.ts";
|
||||||
|
@ -60,11 +60,11 @@ export function create(path: string): Promise<File> {
|
||||||
export class File
|
export class File
|
||||||
implements
|
implements
|
||||||
Reader,
|
Reader,
|
||||||
SyncReader,
|
ReaderSync,
|
||||||
Writer,
|
Writer,
|
||||||
SyncWriter,
|
WriterSync,
|
||||||
Seeker,
|
Seeker,
|
||||||
SyncSeeker,
|
SeekerSync,
|
||||||
Closer {
|
Closer {
|
||||||
constructor(readonly rid: number) {}
|
constructor(readonly rid: number) {}
|
||||||
|
|
||||||
|
@ -97,7 +97,7 @@ export class File
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class Stdin implements Reader, SyncReader, Closer {
|
class Stdin implements Reader, ReaderSync, Closer {
|
||||||
readonly rid: number;
|
readonly rid: number;
|
||||||
constructor() {
|
constructor() {
|
||||||
this.rid = 0;
|
this.rid = 0;
|
||||||
|
@ -116,7 +116,7 @@ class Stdin implements Reader, SyncReader, Closer {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class Stdout implements Writer, SyncWriter, Closer {
|
class Stdout implements Writer, WriterSync, Closer {
|
||||||
readonly rid: number;
|
readonly rid: number;
|
||||||
constructor() {
|
constructor() {
|
||||||
this.rid = 1;
|
this.rid = 1;
|
||||||
|
@ -135,7 +135,7 @@ class Stdout implements Writer, SyncWriter, Closer {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Stderr implements Writer, SyncWriter, Closer {
|
export class Stderr implements Writer, WriterSync, Closer {
|
||||||
readonly rid: number;
|
readonly rid: number;
|
||||||
constructor() {
|
constructor() {
|
||||||
this.rid = 2;
|
this.rid = 2;
|
||||||
|
|
|
@ -22,7 +22,7 @@ export interface Reader {
|
||||||
read(p: Uint8Array): Promise<number | EOF>;
|
read(p: Uint8Array): Promise<number | EOF>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface SyncReader {
|
export interface ReaderSync {
|
||||||
readSync(p: Uint8Array): number | EOF;
|
readSync(p: Uint8Array): number | EOF;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -32,7 +32,7 @@ export interface Writer {
|
||||||
write(p: Uint8Array): Promise<number>;
|
write(p: Uint8Array): Promise<number>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface SyncWriter {
|
export interface WriterSync {
|
||||||
writeSync(p: Uint8Array): number;
|
writeSync(p: Uint8Array): number;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -48,7 +48,7 @@ export interface Seeker {
|
||||||
seek(offset: number, whence: SeekMode): Promise<number>;
|
seek(offset: number, whence: SeekMode): Promise<number>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface SyncSeeker {
|
export interface SeekerSync {
|
||||||
seekSync(offset: number, whence: SeekMode): number;
|
seekSync(offset: number, whence: SeekMode): number;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -93,7 +93,7 @@ export async function* iter(
|
||||||
}
|
}
|
||||||
|
|
||||||
export function* iterSync(
|
export function* iterSync(
|
||||||
r: SyncReader,
|
r: ReaderSync,
|
||||||
options?: {
|
options?: {
|
||||||
bufSize?: number;
|
bufSize?: number;
|
||||||
}
|
}
|
||||||
|
|
32
cli/js/lib.deno.ns.d.ts
vendored
32
cli/js/lib.deno.ns.d.ts
vendored
|
@ -395,7 +395,7 @@ declare namespace Deno {
|
||||||
read(p: Uint8Array): Promise<number | EOF>;
|
read(p: Uint8Array): Promise<number | EOF>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface SyncReader {
|
export interface ReaderSync {
|
||||||
/** Reads up to `p.byteLength` bytes into `p`. It resolves to the number
|
/** Reads up to `p.byteLength` bytes into `p`. It resolves to the number
|
||||||
* of bytes read (`0` < `n` <= `p.byteLength`) and rejects if any error
|
* of bytes read (`0` < `n` <= `p.byteLength`) and rejects if any error
|
||||||
* encountered. Even if `read()` returns `n` < `p.byteLength`, it may use
|
* encountered. Even if `read()` returns `n` < `p.byteLength`, it may use
|
||||||
|
@ -414,7 +414,7 @@ declare namespace Deno {
|
||||||
*
|
*
|
||||||
* Implementations should not retain a reference to `p`.
|
* Implementations should not retain a reference to `p`.
|
||||||
*
|
*
|
||||||
* Use Deno.iterSync() to turn a SyncReader into an Iterator.
|
* Use Deno.iterSync() to turn a ReaderSync into an Iterator.
|
||||||
*/
|
*/
|
||||||
readSync(p: Uint8Array): number | EOF;
|
readSync(p: Uint8Array): number | EOF;
|
||||||
}
|
}
|
||||||
|
@ -432,7 +432,7 @@ declare namespace Deno {
|
||||||
write(p: Uint8Array): Promise<number>;
|
write(p: Uint8Array): Promise<number>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface SyncWriter {
|
export interface WriterSync {
|
||||||
/** Writes `p.byteLength` bytes from `p` to the underlying data
|
/** Writes `p.byteLength` bytes from `p` to the underlying data
|
||||||
* stream. It returns the number of bytes written from `p` (`0` <= `n`
|
* stream. It returns the number of bytes written from `p` (`0` <= `n`
|
||||||
* <= `p.byteLength`) and any error encountered that caused the write to
|
* <= `p.byteLength`) and any error encountered that caused the write to
|
||||||
|
@ -464,7 +464,7 @@ declare namespace Deno {
|
||||||
seek(offset: number, whence: SeekMode): Promise<number>;
|
seek(offset: number, whence: SeekMode): Promise<number>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface SyncSeeker {
|
export interface SeekerSync {
|
||||||
/** Seek sets the offset for the next `readSync()` or `writeSync()` to
|
/** Seek sets the offset for the next `readSync()` or `writeSync()` to
|
||||||
* offset, interpreted according to `whence`: `Start` means relative
|
* offset, interpreted according to `whence`: `Start` means relative
|
||||||
* to the start of the file, `Current` means relative to the current
|
* to the start of the file, `Current` means relative to the current
|
||||||
|
@ -533,7 +533,7 @@ declare namespace Deno {
|
||||||
}
|
}
|
||||||
): AsyncIterableIterator<Uint8Array>;
|
): AsyncIterableIterator<Uint8Array>;
|
||||||
|
|
||||||
/** Turns a SyncReader, `r`, into an iterator.
|
/** Turns a ReaderSync, `r`, into an iterator.
|
||||||
*
|
*
|
||||||
* let f = Deno.openSync("/etc/passwd");
|
* let f = Deno.openSync("/etc/passwd");
|
||||||
* for (const chunk of Deno.iterSync(reader)) {
|
* for (const chunk of Deno.iterSync(reader)) {
|
||||||
|
@ -559,7 +559,7 @@ declare namespace Deno {
|
||||||
* next iteration will overwrite contents of previously returned chunk.
|
* next iteration will overwrite contents of previously returned chunk.
|
||||||
*/
|
*/
|
||||||
export function iterSync(
|
export function iterSync(
|
||||||
r: SyncReader,
|
r: ReaderSync,
|
||||||
options?: {
|
options?: {
|
||||||
bufSize?: number;
|
bufSize?: number;
|
||||||
}
|
}
|
||||||
|
@ -733,11 +733,11 @@ declare namespace Deno {
|
||||||
export class File
|
export class File
|
||||||
implements
|
implements
|
||||||
Reader,
|
Reader,
|
||||||
SyncReader,
|
ReaderSync,
|
||||||
Writer,
|
Writer,
|
||||||
SyncWriter,
|
WriterSync,
|
||||||
Seeker,
|
Seeker,
|
||||||
SyncSeeker,
|
SeekerSync,
|
||||||
Closer {
|
Closer {
|
||||||
readonly rid: number;
|
readonly rid: number;
|
||||||
constructor(rid: number);
|
constructor(rid: number);
|
||||||
|
@ -751,11 +751,11 @@ declare namespace Deno {
|
||||||
}
|
}
|
||||||
|
|
||||||
/** A handle for `stdin`. */
|
/** A handle for `stdin`. */
|
||||||
export const stdin: Reader & SyncReader & Closer & { rid: number };
|
export const stdin: Reader & ReaderSync & Closer & { rid: number };
|
||||||
/** A handle for `stdout`. */
|
/** A handle for `stdout`. */
|
||||||
export const stdout: Writer & SyncWriter & Closer & { rid: number };
|
export const stdout: Writer & WriterSync & Closer & { rid: number };
|
||||||
/** A handle for `stderr`. */
|
/** A handle for `stderr`. */
|
||||||
export const stderr: Writer & SyncWriter & Closer & { rid: number };
|
export const stderr: Writer & WriterSync & Closer & { rid: number };
|
||||||
|
|
||||||
export interface OpenOptions {
|
export interface OpenOptions {
|
||||||
/** Sets the option for read access. This option, when `true`, means that the
|
/** Sets the option for read access. This option, when `true`, means that the
|
||||||
|
@ -831,7 +831,7 @@ declare namespace Deno {
|
||||||
* of ArrayBuffer.
|
* of ArrayBuffer.
|
||||||
*
|
*
|
||||||
* Based on [Go Buffer](https://golang.org/pkg/bytes/#Buffer). */
|
* Based on [Go Buffer](https://golang.org/pkg/bytes/#Buffer). */
|
||||||
export class Buffer implements Reader, SyncReader, Writer, SyncWriter {
|
export class Buffer implements Reader, ReaderSync, Writer, WriterSync {
|
||||||
constructor(ab?: ArrayBuffer);
|
constructor(ab?: ArrayBuffer);
|
||||||
/** Returns a slice holding the unread portion of the buffer.
|
/** Returns a slice holding the unread portion of the buffer.
|
||||||
*
|
*
|
||||||
|
@ -892,7 +892,7 @@ declare namespace Deno {
|
||||||
*
|
*
|
||||||
* Based on Go Lang's
|
* Based on Go Lang's
|
||||||
* [Buffer.ReadFrom](https://golang.org/pkg/bytes/#Buffer.ReadFrom). */
|
* [Buffer.ReadFrom](https://golang.org/pkg/bytes/#Buffer.ReadFrom). */
|
||||||
readFromSync(r: SyncReader): number;
|
readFromSync(r: ReaderSync): number;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Read Reader `r` until end of file (`Deno.EOF`) and resolve to the content
|
/** Read Reader `r` until end of file (`Deno.EOF`) and resolve to the content
|
||||||
|
@ -931,7 +931,7 @@ declare namespace Deno {
|
||||||
* const reader = new Deno.Buffer(myData.buffer as ArrayBuffer);
|
* const reader = new Deno.Buffer(myData.buffer as ArrayBuffer);
|
||||||
* const bufferContent = Deno.readAllSync(reader);
|
* const bufferContent = Deno.readAllSync(reader);
|
||||||
*/
|
*/
|
||||||
export function readAllSync(r: SyncReader): Uint8Array;
|
export function readAllSync(r: ReaderSync): Uint8Array;
|
||||||
|
|
||||||
/** Write all the content of the array buffer (`arr`) to the writer (`w`).
|
/** Write all the content of the array buffer (`arr`) to the writer (`w`).
|
||||||
*
|
*
|
||||||
|
@ -972,7 +972,7 @@ declare namespace Deno {
|
||||||
* Deno.writeAllSync(writer, contentBytes);
|
* Deno.writeAllSync(writer, contentBytes);
|
||||||
* console.log(writer.bytes().length); // 11
|
* console.log(writer.bytes().length); // 11
|
||||||
*/
|
*/
|
||||||
export function writeAllSync(w: SyncWriter, arr: Uint8Array): void;
|
export function writeAllSync(w: WriterSync, arr: Uint8Array): void;
|
||||||
|
|
||||||
export interface MkdirOptions {
|
export interface MkdirOptions {
|
||||||
/** Defaults to `false`. If set to `true`, means that any intermediate
|
/** Defaults to `false`. If set to `true`, means that any intermediate
|
||||||
|
|
|
@ -128,7 +128,7 @@ unitTest(async function readerIterSync(): Promise<void> {
|
||||||
// ref: https://github.com/denoland/deno/issues/2330
|
// ref: https://github.com/denoland/deno/issues/2330
|
||||||
const encoder = new TextEncoder();
|
const encoder = new TextEncoder();
|
||||||
|
|
||||||
class TestReader implements Deno.SyncReader {
|
class TestReader implements Deno.ReaderSync {
|
||||||
#offset = 0;
|
#offset = 0;
|
||||||
#buf: Uint8Array;
|
#buf: Uint8Array;
|
||||||
|
|
||||||
|
|
|
@ -5,7 +5,7 @@
|
||||||
|
|
||||||
type Reader = Deno.Reader;
|
type Reader = Deno.Reader;
|
||||||
type Writer = Deno.Writer;
|
type Writer = Deno.Writer;
|
||||||
type SyncWriter = Deno.SyncWriter;
|
type WriterSync = Deno.WriterSync;
|
||||||
import { charCode, copyBytes } from "./util.ts";
|
import { charCode, copyBytes } from "./util.ts";
|
||||||
import { assert } from "../testing/asserts.ts";
|
import { assert } from "../testing/asserts.ts";
|
||||||
|
|
||||||
|
@ -524,17 +524,17 @@ export class BufWriter extends AbstractBufBase implements Writer {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** BufWriterSync implements buffering for a deno.SyncWriter object.
|
/** BufWriterSync implements buffering for a deno.WriterSync object.
|
||||||
* If an error occurs writing to a SyncWriter, no more data will be
|
* If an error occurs writing to a WriterSync, no more data will be
|
||||||
* accepted and all subsequent writes, and flush(), will return the error.
|
* accepted and all subsequent writes, and flush(), will return the error.
|
||||||
* After all data has been written, the client should call the
|
* After all data has been written, the client should call the
|
||||||
* flush() method to guarantee all data has been forwarded to
|
* flush() method to guarantee all data has been forwarded to
|
||||||
* the underlying deno.SyncWriter.
|
* the underlying deno.WriterSync.
|
||||||
*/
|
*/
|
||||||
export class BufWriterSync extends AbstractBufBase implements SyncWriter {
|
export class BufWriterSync extends AbstractBufBase implements WriterSync {
|
||||||
/** return new BufWriterSync unless writer is BufWriterSync */
|
/** return new BufWriterSync unless writer is BufWriterSync */
|
||||||
static create(
|
static create(
|
||||||
writer: SyncWriter,
|
writer: WriterSync,
|
||||||
size: number = DEFAULT_BUF_SIZE
|
size: number = DEFAULT_BUF_SIZE
|
||||||
): BufWriterSync {
|
): BufWriterSync {
|
||||||
return writer instanceof BufWriterSync
|
return writer instanceof BufWriterSync
|
||||||
|
@ -542,7 +542,7 @@ export class BufWriterSync extends AbstractBufBase implements SyncWriter {
|
||||||
: new BufWriterSync(writer, size);
|
: new BufWriterSync(writer, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
constructor(private writer: SyncWriter, size: number = DEFAULT_BUF_SIZE) {
|
constructor(private writer: WriterSync, size: number = DEFAULT_BUF_SIZE) {
|
||||||
super();
|
super();
|
||||||
if (size <= 0) {
|
if (size <= 0) {
|
||||||
size = DEFAULT_BUF_SIZE;
|
size = DEFAULT_BUF_SIZE;
|
||||||
|
@ -553,13 +553,13 @@ export class BufWriterSync extends AbstractBufBase implements SyncWriter {
|
||||||
/** Discards any unflushed buffered data, clears any error, and
|
/** Discards any unflushed buffered data, clears any error, and
|
||||||
* resets buffer to write its output to w.
|
* resets buffer to write its output to w.
|
||||||
*/
|
*/
|
||||||
reset(w: SyncWriter): void {
|
reset(w: WriterSync): void {
|
||||||
this.err = null;
|
this.err = null;
|
||||||
this.usedBufferBytes = 0;
|
this.usedBufferBytes = 0;
|
||||||
this.writer = w;
|
this.writer = w;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Flush writes any buffered data to the underlying io.SyncWriter. */
|
/** Flush writes any buffered data to the underlying io.WriterSync. */
|
||||||
flush(): void {
|
flush(): void {
|
||||||
if (this.err !== null) throw this.err;
|
if (this.err !== null) throw this.err;
|
||||||
if (this.usedBufferBytes === 0) return;
|
if (this.usedBufferBytes === 0) return;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue