2020-01-02 20:13:47 +00:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2019-03-28 03:29:36 +00:00
|
|
|
import {
|
|
|
|
Reader,
|
|
|
|
Writer,
|
|
|
|
Seeker,
|
|
|
|
Closer,
|
|
|
|
SeekMode,
|
2020-04-28 11:23:30 +00:00
|
|
|
ReaderSync,
|
|
|
|
WriterSync,
|
|
|
|
SeekerSync,
|
2019-09-02 21:07:11 +00:00
|
|
|
} from "./io.ts";
|
2020-03-08 12:09:22 +00:00
|
|
|
import { close } from "./ops/resources.ts";
|
2020-03-09 14:18:02 +00:00
|
|
|
import { read, readSync, write, writeSync } from "./ops/io.ts";
|
2020-03-09 23:22:15 +00: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-04-24 22:45:55 +00:00
|
|
|
export { OpenOptions } from "./ops/fs/open.ts";
|
2019-05-03 04:06:43 +00:00
|
|
|
|
2020-01-21 09:49:42 +00:00
|
|
|
export function openSync(
|
2020-03-06 16:29:23 +00:00
|
|
|
path: string,
|
2020-04-24 22:45:55 +00:00
|
|
|
options: OpenOptions = { read: true }
|
2020-01-21 09:49:42 +00:00
|
|
|
): File {
|
2020-04-24 22:45:55 +00:00
|
|
|
checkOpenOptions(options);
|
|
|
|
const rid = opOpenSync(path, options);
|
2019-08-26 12:50:21 +00:00
|
|
|
return new File(rid);
|
2019-03-28 03:29:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function open(
|
2020-03-06 16:29:23 +00:00
|
|
|
path: string,
|
2020-04-24 22:45:55 +00:00
|
|
|
options: OpenOptions = { read: true }
|
2019-03-28 03:29:36 +00:00
|
|
|
): Promise<File> {
|
2020-04-24 22:45:55 +00:00
|
|
|
checkOpenOptions(options);
|
|
|
|
const rid = await opOpen(path, options);
|
2019-08-26 12:50:21 +00:00
|
|
|
return new File(rid);
|
2019-03-28 03:29:36 +00:00
|
|
|
}
|
|
|
|
|
2020-03-06 16:29:23 +00:00
|
|
|
export function createSync(path: string): File {
|
2020-04-24 22:45:55 +00:00
|
|
|
return openSync(path, {
|
|
|
|
read: true,
|
|
|
|
write: true,
|
|
|
|
truncate: true,
|
|
|
|
create: true,
|
|
|
|
});
|
2020-01-08 22:07:03 +00:00
|
|
|
}
|
|
|
|
|
2020-03-06 16:29:23 +00:00
|
|
|
export function create(path: string): Promise<File> {
|
2020-04-24 22:45:55 +00:00
|
|
|
return open(path, {
|
|
|
|
read: true,
|
|
|
|
write: true,
|
|
|
|
truncate: true,
|
|
|
|
create: true,
|
|
|
|
});
|
2020-01-08 22:07:03 +00:00
|
|
|
}
|
|
|
|
|
2019-03-28 03:29:36 +00:00
|
|
|
export class File
|
|
|
|
implements
|
|
|
|
Reader,
|
2020-04-28 11:23:30 +00:00
|
|
|
ReaderSync,
|
2019-03-28 03:29:36 +00:00
|
|
|
Writer,
|
2020-04-28 11:23:30 +00:00
|
|
|
WriterSync,
|
2019-03-28 03:29:36 +00:00
|
|
|
Seeker,
|
2020-04-28 11:23:30 +00:00
|
|
|
SeekerSync,
|
2019-03-28 03:29:36 +00:00
|
|
|
Closer {
|
2019-03-09 17:30:38 +00:00
|
|
|
constructor(readonly rid: number) {}
|
|
|
|
|
|
|
|
write(p: Uint8Array): Promise<number> {
|
|
|
|
return write(this.rid, p);
|
|
|
|
}
|
|
|
|
|
2019-03-28 03:29:36 +00:00
|
|
|
writeSync(p: Uint8Array): number {
|
|
|
|
return writeSync(this.rid, p);
|
|
|
|
}
|
|
|
|
|
2020-04-28 16:40:43 +00:00
|
|
|
read(p: Uint8Array): Promise<number | null> {
|
2019-03-09 17:30:38 +00:00
|
|
|
return read(this.rid, p);
|
|
|
|
}
|
|
|
|
|
2020-04-28 16:40:43 +00:00
|
|
|
readSync(p: Uint8Array): number | null {
|
2019-03-28 03:29:36 +00:00
|
|
|
return readSync(this.rid, p);
|
|
|
|
}
|
|
|
|
|
2020-03-02 16:44:46 +00:00
|
|
|
seek(offset: number, whence: SeekMode): Promise<number> {
|
2019-03-09 17:30:38 +00:00
|
|
|
return seek(this.rid, offset, whence);
|
|
|
|
}
|
|
|
|
|
2020-03-02 16:44:46 +00:00
|
|
|
seekSync(offset: number, whence: SeekMode): number {
|
2019-03-28 03:29:36 +00:00
|
|
|
return seekSync(this.rid, offset, whence);
|
|
|
|
}
|
|
|
|
|
2019-03-09 17:30:38 +00:00
|
|
|
close(): void {
|
|
|
|
close(this.rid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-28 11:23:30 +00:00
|
|
|
class Stdin implements Reader, ReaderSync, Closer {
|
2020-04-24 23:01:25 +00:00
|
|
|
readonly rid: number;
|
|
|
|
constructor() {
|
|
|
|
this.rid = 0;
|
|
|
|
}
|
|
|
|
|
2020-04-28 16:40:43 +00:00
|
|
|
read(p: Uint8Array): Promise<number | null> {
|
2020-04-24 23:01:25 +00:00
|
|
|
return read(this.rid, p);
|
|
|
|
}
|
|
|
|
|
2020-04-28 16:40:43 +00:00
|
|
|
readSync(p: Uint8Array): number | null {
|
2020-04-24 23:01:25 +00:00
|
|
|
return readSync(this.rid, p);
|
|
|
|
}
|
|
|
|
|
|
|
|
close(): void {
|
|
|
|
close(this.rid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-28 11:23:30 +00:00
|
|
|
class Stdout implements Writer, WriterSync, Closer {
|
2020-04-24 23:01:25 +00:00
|
|
|
readonly rid: number;
|
|
|
|
constructor() {
|
|
|
|
this.rid = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
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 11:23:30 +00:00
|
|
|
export class Stderr implements Writer, WriterSync, Closer {
|
2020-04-24 23:01:25 +00:00
|
|
|
readonly rid: number;
|
|
|
|
constructor() {
|
|
|
|
this.rid = 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
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 17:30:38 +00:00
|
|
|
|
2020-01-21 09:49:42 +00:00
|
|
|
function checkOpenOptions(options: OpenOptions): void {
|
2020-03-28 17:03:49 +00:00
|
|
|
if (Object.values(options).filter((val) => val === true).length === 0) {
|
2020-01-21 09:49:42 +00: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"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|