2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2019-03-27 23:29:36 -04:00
|
|
|
import {
|
2019-07-06 10:16:03 -04:00
|
|
|
EOF,
|
2019-03-27 23:29:36 -04:00
|
|
|
Reader,
|
|
|
|
Writer,
|
|
|
|
Seeker,
|
|
|
|
Closer,
|
|
|
|
SeekMode,
|
|
|
|
SyncReader,
|
|
|
|
SyncWriter,
|
2020-03-28 13:03:49 -04:00
|
|
|
SyncSeeker,
|
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,
|
2020-03-28 13:03:49 -04:00
|
|
|
OpenMode,
|
2020-03-09 19:22:15 -04:00
|
|
|
} from "./ops/fs/open.ts";
|
|
|
|
export { OpenOptions, OpenMode } from "./ops/fs/open.ts";
|
2019-05-03 00:06:43 -04:00
|
|
|
|
2020-03-16 15:02:41 -04:00
|
|
|
export function openSync(path: string, options?: OpenOptions): File;
|
|
|
|
export function openSync(path: string, openMode?: OpenMode): File;
|
|
|
|
|
|
|
|
/**@internal*/
|
2020-01-21 04:49:42 -05:00
|
|
|
export function openSync(
|
2020-03-06 11:29:23 -05:00
|
|
|
path: string,
|
2020-01-21 04:49:42 -05:00
|
|
|
modeOrOptions: OpenOptions | OpenMode = "r"
|
|
|
|
): File {
|
2020-03-16 15:02:41 -04:00
|
|
|
let openMode = undefined;
|
2020-03-09 19:22:15 -04:00
|
|
|
let options = undefined;
|
2020-01-21 04:49:42 -05:00
|
|
|
|
|
|
|
if (typeof modeOrOptions === "string") {
|
2020-03-16 15:02:41 -04:00
|
|
|
openMode = modeOrOptions;
|
2020-01-21 04:49:42 -05:00
|
|
|
} else {
|
|
|
|
checkOpenOptions(modeOrOptions);
|
2020-03-09 19:22:15 -04:00
|
|
|
options = modeOrOptions as OpenOptions;
|
2020-01-21 04:49:42 -05:00
|
|
|
}
|
|
|
|
|
2020-03-16 15:02:41 -04:00
|
|
|
const rid = opOpenSync(path, openMode as OpenMode, options);
|
2019-08-26 08:50:21 -04:00
|
|
|
return new File(rid);
|
2019-03-27 23:29:36 -04:00
|
|
|
}
|
|
|
|
|
2020-03-06 11:29:23 -05:00
|
|
|
export async function open(path: string, options?: OpenOptions): Promise<File>;
|
2020-03-16 15:02:41 -04:00
|
|
|
export async function open(path: string, openMode?: OpenMode): Promise<File>;
|
|
|
|
|
|
|
|
/**@internal*/
|
2019-03-27 23:29:36 -04:00
|
|
|
export async function open(
|
2020-03-06 11:29:23 -05:00
|
|
|
path: string,
|
2020-01-21 04:49:42 -05:00
|
|
|
modeOrOptions: OpenOptions | OpenMode = "r"
|
2019-03-27 23:29:36 -04:00
|
|
|
): Promise<File> {
|
2020-03-16 15:02:41 -04:00
|
|
|
let openMode = undefined;
|
2020-03-09 19:22:15 -04:00
|
|
|
let options = undefined;
|
2020-01-21 04:49:42 -05:00
|
|
|
|
|
|
|
if (typeof modeOrOptions === "string") {
|
2020-03-16 15:02:41 -04:00
|
|
|
openMode = modeOrOptions;
|
2020-01-21 04:49:42 -05:00
|
|
|
} else {
|
|
|
|
checkOpenOptions(modeOrOptions);
|
2020-03-09 19:22:15 -04:00
|
|
|
options = modeOrOptions as OpenOptions;
|
2020-01-21 04:49:42 -05:00
|
|
|
}
|
|
|
|
|
2020-03-16 15:02:41 -04:00
|
|
|
const rid = await opOpen(path, openMode as OpenMode, options);
|
2019-08-26 08:50:21 -04:00
|
|
|
return new File(rid);
|
2019-03-27 23:29:36 -04:00
|
|
|
}
|
|
|
|
|
2020-03-06 11:29:23 -05:00
|
|
|
export function createSync(path: string): File {
|
|
|
|
return openSync(path, "w+");
|
2020-01-08 17:07:03 -05:00
|
|
|
}
|
|
|
|
|
2020-03-06 11:29:23 -05:00
|
|
|
export function create(path: string): Promise<File> {
|
|
|
|
return open(path, "w+");
|
2020-01-08 17:07:03 -05:00
|
|
|
}
|
|
|
|
|
2019-03-27 23:29:36 -04:00
|
|
|
export class File
|
|
|
|
implements
|
|
|
|
Reader,
|
|
|
|
SyncReader,
|
|
|
|
Writer,
|
|
|
|
SyncWriter,
|
|
|
|
Seeker,
|
|
|
|
SyncSeeker,
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2019-07-06 10:16:03 -04:00
|
|
|
read(p: Uint8Array): Promise<number | EOF> {
|
2019-03-09 12:30:38 -05:00
|
|
|
return read(this.rid, p);
|
|
|
|
}
|
|
|
|
|
2019-07-06 10:16:03 -04:00
|
|
|
readSync(p: Uint8Array): number | EOF {
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const stdin = new File(0);
|
|
|
|
export const stdout = new File(1);
|
|
|
|
export const stderr = new File(2);
|
|
|
|
|
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"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|