2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2018-09-27 00:56:39 -04:00
|
|
|
// Interfaces 100% copied from Go.
|
|
|
|
// Documentation liberally lifted from them too.
|
|
|
|
// Thank you! We love Go!
|
|
|
|
|
2019-10-31 10:57:09 -04:00
|
|
|
export const EOF: unique symbol = Symbol("EOF");
|
|
|
|
export type EOF = typeof EOF;
|
2018-09-27 00:56:39 -04:00
|
|
|
|
2020-04-24 18:05:48 -04:00
|
|
|
const DEFAULT_BUFFER_SIZE = 32 * 1024;
|
|
|
|
|
2019-02-18 18:26:41 -05:00
|
|
|
// Seek whence values.
|
|
|
|
// https://golang.org/pkg/io/#pkg-constants
|
|
|
|
export enum SeekMode {
|
2020-04-28 06:30:59 -04:00
|
|
|
Start = 0,
|
|
|
|
Current = 1,
|
|
|
|
End = 2,
|
2019-02-18 18:26:41 -05:00
|
|
|
}
|
|
|
|
|
2018-09-27 00:56:39 -04:00
|
|
|
// Reader is the interface that wraps the basic read() method.
|
|
|
|
// https://golang.org/pkg/io/#Reader
|
|
|
|
export interface Reader {
|
2019-07-06 10:16:03 -04:00
|
|
|
read(p: Uint8Array): Promise<number | EOF>;
|
2018-09-27 00:56:39 -04:00
|
|
|
}
|
|
|
|
|
2019-03-27 23:29:36 -04:00
|
|
|
export interface SyncReader {
|
2019-07-06 10:16:03 -04:00
|
|
|
readSync(p: Uint8Array): number | EOF;
|
2019-03-27 23:29:36 -04:00
|
|
|
}
|
|
|
|
|
2018-09-27 00:56:39 -04:00
|
|
|
// Writer is the interface that wraps the basic write() method.
|
|
|
|
// https://golang.org/pkg/io/#Writer
|
|
|
|
export interface Writer {
|
2018-11-08 01:18:21 -05:00
|
|
|
write(p: Uint8Array): Promise<number>;
|
2018-09-27 00:56:39 -04:00
|
|
|
}
|
|
|
|
|
2019-03-27 23:29:36 -04:00
|
|
|
export interface SyncWriter {
|
|
|
|
writeSync(p: Uint8Array): number;
|
|
|
|
}
|
2020-03-02 10:19:42 -05:00
|
|
|
|
2018-09-27 00:56:39 -04:00
|
|
|
// https://golang.org/pkg/io/#Closer
|
|
|
|
export interface Closer {
|
|
|
|
// The behavior of Close after the first call is undefined. Specific
|
|
|
|
// implementations may document their own behavior.
|
|
|
|
close(): void;
|
|
|
|
}
|
|
|
|
|
|
|
|
// https://golang.org/pkg/io/#Seeker
|
|
|
|
export interface Seeker {
|
2020-03-02 11:44:46 -05:00
|
|
|
seek(offset: number, whence: SeekMode): Promise<number>;
|
2018-09-27 00:56:39 -04:00
|
|
|
}
|
|
|
|
|
2019-03-27 23:29:36 -04:00
|
|
|
export interface SyncSeeker {
|
2020-03-02 11:44:46 -05:00
|
|
|
seekSync(offset: number, whence: SeekMode): number;
|
2019-03-27 23:29:36 -04:00
|
|
|
}
|
|
|
|
|
2018-09-27 00:56:39 -04:00
|
|
|
// https://golang.org/pkg/io/#ReadCloser
|
2018-10-26 12:14:06 -04:00
|
|
|
export interface ReadCloser extends Reader, Closer {}
|
2018-09-27 00:56:39 -04:00
|
|
|
|
|
|
|
// https://golang.org/pkg/io/#WriteCloser
|
|
|
|
export interface WriteCloser extends Writer, Closer {}
|
|
|
|
|
|
|
|
// https://golang.org/pkg/io/#ReadSeeker
|
|
|
|
export interface ReadSeeker extends Reader, Seeker {}
|
|
|
|
|
|
|
|
// https://golang.org/pkg/io/#WriteSeeker
|
|
|
|
export interface WriteSeeker extends Writer, Seeker {}
|
|
|
|
|
|
|
|
// https://golang.org/pkg/io/#ReadWriteCloser
|
|
|
|
export interface ReadWriteCloser extends Reader, Writer, Closer {}
|
|
|
|
|
|
|
|
// https://golang.org/pkg/io/#ReadWriteSeeker
|
|
|
|
export interface ReadWriteSeeker extends Reader, Writer, Seeker {}
|
|
|
|
|
2020-04-26 16:25:24 -04:00
|
|
|
export async function copy(
|
|
|
|
src: Reader,
|
|
|
|
dst: Writer,
|
|
|
|
options?: {
|
|
|
|
bufSize?: number;
|
|
|
|
}
|
|
|
|
): Promise<number> {
|
2018-09-27 00:56:39 -04:00
|
|
|
let n = 0;
|
2020-04-26 16:25:24 -04:00
|
|
|
const bufSize = options?.bufSize ?? DEFAULT_BUFFER_SIZE;
|
|
|
|
const b = new Uint8Array(bufSize);
|
2018-09-27 00:56:39 -04:00
|
|
|
let gotEOF = false;
|
|
|
|
while (gotEOF === false) {
|
|
|
|
const result = await src.read(b);
|
2019-07-06 10:16:03 -04:00
|
|
|
if (result === EOF) {
|
2018-09-27 00:56:39 -04:00
|
|
|
gotEOF = true;
|
2019-07-06 10:16:03 -04:00
|
|
|
} else {
|
|
|
|
n += await dst.write(b.subarray(0, result));
|
2018-09-27 00:56:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
2018-10-31 10:29:13 -04:00
|
|
|
|
2020-04-22 15:30:45 -04:00
|
|
|
export async function* iter(
|
|
|
|
r: Reader,
|
2020-04-24 18:05:48 -04:00
|
|
|
options?: {
|
|
|
|
bufSize?: number;
|
|
|
|
}
|
2020-04-22 15:30:45 -04:00
|
|
|
): AsyncIterableIterator<Uint8Array> {
|
2020-04-24 18:05:48 -04:00
|
|
|
const bufSize = options?.bufSize ?? DEFAULT_BUFFER_SIZE;
|
|
|
|
const b = new Uint8Array(bufSize);
|
2020-04-22 15:30:45 -04:00
|
|
|
while (true) {
|
|
|
|
const result = await r.read(b);
|
|
|
|
if (result === EOF) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
yield b.subarray(0, result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function* iterSync(
|
|
|
|
r: SyncReader,
|
2020-04-24 18:05:48 -04:00
|
|
|
options?: {
|
|
|
|
bufSize?: number;
|
|
|
|
}
|
2020-04-22 15:30:45 -04:00
|
|
|
): IterableIterator<Uint8Array> {
|
2020-04-24 18:05:48 -04:00
|
|
|
const bufSize = options?.bufSize ?? DEFAULT_BUFFER_SIZE;
|
|
|
|
const b = new Uint8Array(bufSize);
|
2020-04-22 15:30:45 -04:00
|
|
|
while (true) {
|
|
|
|
const result = r.readSync(b);
|
|
|
|
if (result === EOF) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
yield b.subarray(0, result);
|
|
|
|
}
|
2018-10-31 10:29:13 -04:00
|
|
|
}
|