2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-07-06 21:45:39 -04:00
|
|
|
|
2020-07-08 05:26:39 -04:00
|
|
|
import type {
|
2019-03-27 23:29:36 -04:00
|
|
|
Reader,
|
|
|
|
Writer,
|
|
|
|
Seeker,
|
|
|
|
Closer,
|
|
|
|
SeekMode,
|
2020-04-28 07:23:30 -04:00
|
|
|
ReaderSync,
|
|
|
|
WriterSync,
|
|
|
|
SeekerSync,
|
2019-09-02 17:07:11 -04:00
|
|
|
} from "./io.ts";
|
2020-03-08 08:09:22 -04:00
|
|
|
import { close } from "./ops/resources.ts";
|
2020-03-09 10:18:02 -04:00
|
|
|
import { read, readSync, write, writeSync } from "./ops/io.ts";
|
2020-03-09 19:22:15 -04:00
|
|
|
import { seek, seekSync } from "./ops/fs/seek.ts";
|
|
|
|
export { seek, seekSync } from "./ops/fs/seek.ts";
|
|
|
|
import {
|
|
|
|
open as opOpen,
|
|
|
|
openSync as opOpenSync,
|
|
|
|
OpenOptions,
|
|
|
|
} from "./ops/fs/open.ts";
|
2020-07-08 05:26:39 -04:00
|
|
|
export type { OpenOptions } from "./ops/fs/open.ts";
|
2019-05-03 00:06:43 -04:00
|
|
|
|
2020-01-21 04:49:42 -05:00
|
|
|
export function openSync(
|
2020-06-11 12:36:20 -04:00
|
|
|
path: string | URL,
|
2020-04-24 18:45:55 -04:00
|
|
|
options: OpenOptions = { read: true }
|
2020-01-21 04:49:42 -05:00
|
|
|
): File {
|
2020-04-24 18:45:55 -04:00
|
|
|
checkOpenOptions(options);
|
|
|
|
const rid = opOpenSync(path, options);
|
2019-08-26 08:50:21 -04:00
|
|
|
return new File(rid);
|
2019-03-27 23:29:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function open(
|
2020-06-11 12:36:20 -04:00
|
|
|
path: string | URL,
|
2020-04-24 18:45:55 -04:00
|
|
|
options: OpenOptions = { read: true }
|
2019-03-27 23:29:36 -04:00
|
|
|
): Promise<File> {
|
2020-04-24 18:45:55 -04:00
|
|
|
checkOpenOptions(options);
|
|
|
|
const rid = await opOpen(path, options);
|
2019-08-26 08:50:21 -04:00
|
|
|
return new File(rid);
|
2019-03-27 23:29:36 -04:00
|
|
|
}
|
|
|
|
|
2020-06-11 12:36:20 -04:00
|
|
|
export function createSync(path: string | URL): File {
|
2020-04-24 18:45:55 -04:00
|
|
|
return openSync(path, {
|
|
|
|
read: true,
|
|
|
|
write: true,
|
|
|
|
truncate: true,
|
|
|
|
create: true,
|
|
|
|
});
|
2020-01-08 17:07:03 -05:00
|
|
|
}
|
|
|
|
|
2020-06-11 12:36:20 -04:00
|
|
|
export function create(path: string | URL): Promise<File> {
|
2020-04-24 18:45:55 -04:00
|
|
|
return open(path, {
|
|
|
|
read: true,
|
|
|
|
write: true,
|
|
|
|
truncate: true,
|
|
|
|
create: true,
|
|
|
|
});
|
2020-01-08 17:07:03 -05:00
|
|
|
}
|
|
|
|
|
2019-03-27 23:29:36 -04:00
|
|
|
export class File
|
|
|
|
implements
|
|
|
|
Reader,
|
2020-04-28 07:23:30 -04:00
|
|
|
ReaderSync,
|
2019-03-27 23:29:36 -04:00
|
|
|
Writer,
|
2020-04-28 07:23:30 -04:00
|
|
|
WriterSync,
|
2019-03-27 23:29:36 -04:00
|
|
|
Seeker,
|
2020-04-28 07:23:30 -04:00
|
|
|
SeekerSync,
|
2019-03-27 23:29:36 -04:00
|
|
|
Closer {
|
2019-03-09 12:30:38 -05:00
|
|
|
constructor(readonly rid: number) {}
|
|
|
|
|
|
|
|
write(p: Uint8Array): Promise<number> {
|
|
|
|
return write(this.rid, p);
|
|
|
|
}
|
|
|
|
|
2019-03-27 23:29:36 -04:00
|
|
|
writeSync(p: Uint8Array): number {
|
|
|
|
return writeSync(this.rid, p);
|
|
|
|
}
|
|
|
|
|
2020-04-28 12:40:43 -04:00
|
|
|
read(p: Uint8Array): Promise<number | null> {
|
2019-03-09 12:30:38 -05:00
|
|
|
return read(this.rid, p);
|
|
|
|
}
|
|
|
|
|
2020-04-28 12:40:43 -04:00
|
|
|
readSync(p: Uint8Array): number | null {
|
2019-03-27 23:29:36 -04:00
|
|
|
return readSync(this.rid, p);
|
|
|
|
}
|
|
|
|
|
2020-03-02 11:44:46 -05:00
|
|
|
seek(offset: number, whence: SeekMode): Promise<number> {
|
2019-03-09 12:30:38 -05:00
|
|
|
return seek(this.rid, offset, whence);
|
|
|
|
}
|
|
|
|
|
2020-03-02 11:44:46 -05:00
|
|
|
seekSync(offset: number, whence: SeekMode): number {
|
2019-03-27 23:29:36 -04:00
|
|
|
return seekSync(this.rid, offset, whence);
|
|
|
|
}
|
|
|
|
|
2019-03-09 12:30:38 -05:00
|
|
|
close(): void {
|
|
|
|
close(this.rid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-28 07:23:30 -04:00
|
|
|
class Stdin implements Reader, ReaderSync, Closer {
|
2020-07-06 21:45:39 -04:00
|
|
|
readonly rid = 0;
|
2020-04-24 19:01:25 -04:00
|
|
|
|
2020-04-28 12:40:43 -04:00
|
|
|
read(p: Uint8Array): Promise<number | null> {
|
2020-04-24 19:01:25 -04:00
|
|
|
return read(this.rid, p);
|
|
|
|
}
|
|
|
|
|
2020-04-28 12:40:43 -04:00
|
|
|
readSync(p: Uint8Array): number | null {
|
2020-04-24 19:01:25 -04:00
|
|
|
return readSync(this.rid, p);
|
|
|
|
}
|
|
|
|
|
|
|
|
close(): void {
|
|
|
|
close(this.rid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-28 07:23:30 -04:00
|
|
|
class Stdout implements Writer, WriterSync, Closer {
|
2020-07-06 21:45:39 -04:00
|
|
|
readonly rid = 1;
|
2020-04-24 19:01:25 -04:00
|
|
|
|
|
|
|
write(p: Uint8Array): Promise<number> {
|
|
|
|
return write(this.rid, p);
|
|
|
|
}
|
|
|
|
|
|
|
|
writeSync(p: Uint8Array): number {
|
|
|
|
return writeSync(this.rid, p);
|
|
|
|
}
|
|
|
|
|
|
|
|
close(): void {
|
|
|
|
close(this.rid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-28 07:23:30 -04:00
|
|
|
export class Stderr implements Writer, WriterSync, Closer {
|
2020-07-06 21:45:39 -04:00
|
|
|
readonly rid = 2;
|
2020-04-24 19:01:25 -04:00
|
|
|
|
|
|
|
write(p: Uint8Array): Promise<number> {
|
|
|
|
return write(this.rid, p);
|
|
|
|
}
|
|
|
|
|
|
|
|
writeSync(p: Uint8Array): number {
|
|
|
|
return writeSync(this.rid, p);
|
|
|
|
}
|
|
|
|
|
|
|
|
close(): void {
|
|
|
|
close(this.rid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const stdin = new Stdin();
|
|
|
|
export const stdout = new Stdout();
|
|
|
|
export const stderr = new Stderr();
|
2019-03-09 12:30:38 -05:00
|
|
|
|
2020-01-21 04:49:42 -05:00
|
|
|
function checkOpenOptions(options: OpenOptions): void {
|
2020-03-28 13:03:49 -04:00
|
|
|
if (Object.values(options).filter((val) => val === true).length === 0) {
|
2020-01-21 04:49:42 -05:00
|
|
|
throw new Error("OpenOptions requires at least one option to be true");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.truncate && !options.write) {
|
|
|
|
throw new Error("'truncate' option requires 'write' option");
|
|
|
|
}
|
|
|
|
|
|
|
|
const createOrCreateNewWithoutWriteOrAppend =
|
|
|
|
(options.create || options.createNew) && !(options.write || options.append);
|
|
|
|
|
|
|
|
if (createOrCreateNewWithoutWriteOrAppend) {
|
|
|
|
throw new Error(
|
|
|
|
"'create' or 'createNew' options require 'write' or 'append' option"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|