2019-01-21 19:03:30 +00:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2018-09-27 04:56:39 +00:00
|
|
|
// Interfaces 100% copied from Go.
|
|
|
|
// Documentation liberally lifted from them too.
|
|
|
|
// Thank you! We love Go!
|
|
|
|
|
2019-10-31 14:57:09 +00:00
|
|
|
export const EOF: unique symbol = Symbol("EOF");
|
|
|
|
export type EOF = typeof EOF;
|
2018-09-27 04:56:39 +00:00
|
|
|
|
2019-02-18 23:26:41 +00:00
|
|
|
// Seek whence values.
|
|
|
|
// https://golang.org/pkg/io/#pkg-constants
|
|
|
|
export enum SeekMode {
|
|
|
|
SEEK_START = 0,
|
|
|
|
SEEK_CURRENT = 1,
|
|
|
|
SEEK_END = 2
|
|
|
|
}
|
|
|
|
|
2018-09-27 04:56:39 +00:00
|
|
|
// Reader is the interface that wraps the basic read() method.
|
|
|
|
// https://golang.org/pkg/io/#Reader
|
|
|
|
export interface Reader {
|
2018-10-14 20:29:50 +00:00
|
|
|
/** Reads up to p.byteLength bytes into `p`. It resolves to the number
|
2019-07-06 14:16:03 +00:00
|
|
|
* of bytes read (`0` < `n` <= `p.byteLength`) and rejects if any error encountered.
|
2018-10-14 20:29:50 +00:00
|
|
|
* Even if `read()` returns `n` < `p.byteLength`, it may use all of `p` as
|
|
|
|
* scratch space during the call. If some data is available but not
|
|
|
|
* `p.byteLength` bytes, `read()` conventionally returns what is available
|
|
|
|
* instead of waiting for more.
|
|
|
|
*
|
2019-07-06 14:16:03 +00:00
|
|
|
* When `read()` encounters end-of-file condition, it returns EOF symbol.
|
2018-10-14 20:29:50 +00:00
|
|
|
*
|
2019-07-06 14:16:03 +00:00
|
|
|
* When `read()` encounters an error, it rejects with an error.
|
2018-10-14 20:29:50 +00:00
|
|
|
*
|
2019-07-06 14:16:03 +00:00
|
|
|
* Callers should always process the `n` > `0` bytes returned before
|
|
|
|
* considering the EOF. Doing so correctly handles I/O errors that happen
|
|
|
|
* after reading some bytes and also both of the allowed EOF behaviors.
|
2018-10-14 20:29:50 +00:00
|
|
|
*
|
|
|
|
* Implementations must not retain `p`.
|
|
|
|
*/
|
2019-07-06 14:16:03 +00:00
|
|
|
read(p: Uint8Array): Promise<number | EOF>;
|
2018-09-27 04:56:39 +00:00
|
|
|
}
|
|
|
|
|
2019-03-28 03:29:36 +00:00
|
|
|
export interface SyncReader {
|
2019-07-06 14:16:03 +00:00
|
|
|
readSync(p: Uint8Array): number | EOF;
|
2019-03-28 03:29:36 +00:00
|
|
|
}
|
|
|
|
|
2018-09-27 04:56:39 +00:00
|
|
|
// Writer is the interface that wraps the basic write() method.
|
|
|
|
// https://golang.org/pkg/io/#Writer
|
|
|
|
export interface Writer {
|
2018-10-14 20:29:50 +00:00
|
|
|
/** Writes `p.byteLength` bytes from `p` to the underlying data
|
|
|
|
* stream. It resolves to the number of bytes written from `p` (`0` <= `n` <=
|
|
|
|
* `p.byteLength`) and any error encountered that caused the write to stop
|
|
|
|
* early. `write()` must return a non-null error if it returns `n` <
|
|
|
|
* `p.byteLength`. write() must not modify the slice data, even temporarily.
|
|
|
|
*
|
|
|
|
* Implementations must not retain `p`.
|
|
|
|
*/
|
2018-11-08 06:18:21 +00:00
|
|
|
write(p: Uint8Array): Promise<number>;
|
2018-09-27 04:56:39 +00:00
|
|
|
}
|
|
|
|
|
2019-03-28 03:29:36 +00:00
|
|
|
export interface SyncWriter {
|
|
|
|
writeSync(p: Uint8Array): number;
|
|
|
|
}
|
2018-09-27 04:56:39 +00: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 {
|
2018-10-14 20:29:50 +00:00
|
|
|
/** Seek sets the offset for the next `read()` or `write()` to offset,
|
|
|
|
* interpreted according to `whence`: `SeekStart` means relative to the start
|
|
|
|
* of the file, `SeekCurrent` means relative to the current offset, and
|
|
|
|
* `SeekEnd` means relative to the end. Seek returns the new offset relative
|
|
|
|
* to the start of the file and an error, if any.
|
|
|
|
*
|
|
|
|
* Seeking to an offset before the start of the file is an error. Seeking to
|
|
|
|
* any positive offset is legal, but the behavior of subsequent I/O operations
|
|
|
|
* on the underlying object is implementation-dependent.
|
|
|
|
*/
|
2019-02-18 23:26:41 +00:00
|
|
|
seek(offset: number, whence: SeekMode): Promise<void>;
|
2018-09-27 04:56:39 +00:00
|
|
|
}
|
|
|
|
|
2019-03-28 03:29:36 +00:00
|
|
|
export interface SyncSeeker {
|
|
|
|
seekSync(offset: number, whence: SeekMode): void;
|
|
|
|
}
|
|
|
|
|
2018-09-27 04:56:39 +00:00
|
|
|
// https://golang.org/pkg/io/#ReadCloser
|
2018-10-26 16:14:06 +00:00
|
|
|
export interface ReadCloser extends Reader, Closer {}
|
2018-09-27 04:56:39 +00: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 {}
|
|
|
|
|
2018-10-14 20:29:50 +00:00
|
|
|
/** Copies from `src` to `dst` until either `EOF` is reached on `src`
|
|
|
|
* or an error occurs. It returns the number of bytes copied and the first
|
|
|
|
* error encountered while copying, if any.
|
|
|
|
*
|
|
|
|
* Because `copy()` is defined to read from `src` until `EOF`, it does not
|
|
|
|
* treat an `EOF` from `read()` as an error to be reported.
|
|
|
|
*/
|
2018-09-27 04:56:39 +00:00
|
|
|
// https://golang.org/pkg/io/#Copy
|
|
|
|
export async function copy(dst: Writer, src: Reader): Promise<number> {
|
|
|
|
let n = 0;
|
2018-10-14 20:29:50 +00:00
|
|
|
const b = new Uint8Array(32 * 1024);
|
2018-09-27 04:56:39 +00:00
|
|
|
let gotEOF = false;
|
|
|
|
while (gotEOF === false) {
|
|
|
|
const result = await src.read(b);
|
2019-07-06 14:16:03 +00:00
|
|
|
if (result === EOF) {
|
2018-09-27 04:56:39 +00:00
|
|
|
gotEOF = true;
|
2019-07-06 14:16:03 +00:00
|
|
|
} else {
|
|
|
|
n += await dst.write(b.subarray(0, result));
|
2018-09-27 04:56:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
2018-10-31 14:29:13 +00:00
|
|
|
|
2018-11-09 04:07:48 +00:00
|
|
|
/** Turns `r` into async iterator.
|
2018-10-31 14:29:13 +00:00
|
|
|
*
|
2019-01-29 18:24:40 +00:00
|
|
|
* for await (const chunk of toAsyncIterator(reader)) {
|
2018-11-09 04:07:48 +00:00
|
|
|
* console.log(chunk)
|
|
|
|
* }
|
2018-10-31 14:29:13 +00:00
|
|
|
*/
|
2018-11-08 06:18:21 +00:00
|
|
|
export function toAsyncIterator(r: Reader): AsyncIterableIterator<Uint8Array> {
|
2018-10-31 14:29:13 +00:00
|
|
|
const b = new Uint8Array(1024);
|
2019-05-11 14:05:56 +00:00
|
|
|
// Keep track if end-of-file has been reached, then
|
|
|
|
// signal that iterator is done during subsequent next()
|
2019-07-06 14:16:03 +00:00
|
|
|
// call. This is required because `r` can return a `number | EOF`
|
2019-05-11 14:05:56 +00:00
|
|
|
// with data read and EOF reached. But if iterator returns
|
|
|
|
// `done` then `value` is discarded.
|
|
|
|
//
|
|
|
|
// See https://github.com/denoland/deno/issues/2330 for reference.
|
|
|
|
let sawEof = false;
|
2018-10-31 14:29:13 +00:00
|
|
|
|
|
|
|
return {
|
2019-04-21 20:40:10 +00:00
|
|
|
[Symbol.asyncIterator](): AsyncIterableIterator<Uint8Array> {
|
2018-10-31 14:29:13 +00:00
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2018-11-08 06:18:21 +00:00
|
|
|
async next(): Promise<IteratorResult<Uint8Array>> {
|
2019-05-11 14:05:56 +00:00
|
|
|
if (sawEof) {
|
|
|
|
return { value: new Uint8Array(), done: true };
|
|
|
|
}
|
|
|
|
|
2018-10-31 14:29:13 +00:00
|
|
|
const result = await r.read(b);
|
2019-07-06 14:16:03 +00:00
|
|
|
if (result === EOF) {
|
|
|
|
sawEof = true;
|
|
|
|
return { value: new Uint8Array(), done: true };
|
|
|
|
}
|
2019-05-11 14:05:56 +00:00
|
|
|
|
2018-10-31 14:29:13 +00:00
|
|
|
return {
|
2019-07-06 14:16:03 +00:00
|
|
|
value: b.subarray(0, result),
|
2019-05-11 14:05:56 +00:00
|
|
|
done: false
|
2018-10-31 14:29:13 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|