From b7eb241c3569ead990d28b4f4889c2c52fc7894c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Tue, 10 Mar 2020 00:22:15 +0100 Subject: reorg: move JS ops implementations to cli/js/ops/, part 3 (#4302) Following JS ops were moved to separate files in cli/js/ops directory: - net - tls - fs --- cli/js/chmod.ts | 22 -------- cli/js/chown.ts | 32 ------------ cli/js/compiler_host.ts | 2 +- cli/js/compiler_imports.ts | 2 +- cli/js/copy_file.ts | 29 ----------- cli/js/deno.ts | 32 ++++++------ cli/js/dir.ts | 27 ---------- cli/js/file_info.ts | 2 +- cli/js/files.ts | 126 ++++++--------------------------------------- cli/js/link.ts | 20 ------- cli/js/make_temp.ts | 90 -------------------------------- cli/js/mkdir.ts | 67 ------------------------ cli/js/net.ts | 42 ++++----------- cli/js/ops/fs/chmod.ts | 22 ++++++++ cli/js/ops/fs/chown.ts | 32 ++++++++++++ cli/js/ops/fs/copy_file.ts | 29 +++++++++++ cli/js/ops/fs/dir.ts | 27 ++++++++++ cli/js/ops/fs/link.ts | 20 +++++++ cli/js/ops/fs/make_temp.ts | 90 ++++++++++++++++++++++++++++++++ cli/js/ops/fs/mkdir.ts | 67 ++++++++++++++++++++++++ cli/js/ops/fs/open.ts | 67 ++++++++++++++++++++++++ cli/js/ops/fs/read_dir.ts | 37 +++++++++++++ cli/js/ops/fs/read_link.ts | 20 +++++++ cli/js/ops/fs/realpath.ts | 18 +++++++ cli/js/ops/fs/remove.ts | 33 ++++++++++++ cli/js/ops/fs/rename.ts | 25 +++++++++ cli/js/ops/fs/seek.ts | 33 ++++++++++++ cli/js/ops/fs/stat.ts | 84 ++++++++++++++++++++++++++++++ cli/js/ops/fs/symlink.ts | 44 ++++++++++++++++ cli/js/ops/fs/truncate.ts | 33 ++++++++++++ cli/js/ops/fs/utime.ts | 50 ++++++++++++++++++ cli/js/ops/net.ts | 117 +++++++++++++++++++++++++++++++++++++++++ cli/js/ops/timers.ts | 9 ++++ cli/js/ops/tls.ts | 69 +++++++++++++++++++++++++ cli/js/performance.ts | 9 +--- cli/js/read_dir.ts | 37 ------------- cli/js/read_link.ts | 20 ------- cli/js/realpath.ts | 18 ------- cli/js/remove.ts | 33 ------------ cli/js/rename.ts | 25 --------- cli/js/stat.ts | 84 ------------------------------ cli/js/symlink.ts | 44 ---------------- cli/js/tls.ts | 8 +-- cli/js/truncate.ts | 33 ------------ cli/js/utime.ts | 50 ------------------ cli/js/write_file.ts | 4 +- 46 files changed, 980 insertions(+), 804 deletions(-) delete mode 100644 cli/js/chmod.ts delete mode 100644 cli/js/chown.ts delete mode 100644 cli/js/copy_file.ts delete mode 100644 cli/js/dir.ts delete mode 100644 cli/js/link.ts delete mode 100644 cli/js/make_temp.ts delete mode 100644 cli/js/mkdir.ts create mode 100644 cli/js/ops/fs/chmod.ts create mode 100644 cli/js/ops/fs/chown.ts create mode 100644 cli/js/ops/fs/copy_file.ts create mode 100644 cli/js/ops/fs/dir.ts create mode 100644 cli/js/ops/fs/link.ts create mode 100644 cli/js/ops/fs/make_temp.ts create mode 100644 cli/js/ops/fs/mkdir.ts create mode 100644 cli/js/ops/fs/open.ts create mode 100644 cli/js/ops/fs/read_dir.ts create mode 100644 cli/js/ops/fs/read_link.ts create mode 100644 cli/js/ops/fs/realpath.ts create mode 100644 cli/js/ops/fs/remove.ts create mode 100644 cli/js/ops/fs/rename.ts create mode 100644 cli/js/ops/fs/seek.ts create mode 100644 cli/js/ops/fs/stat.ts create mode 100644 cli/js/ops/fs/symlink.ts create mode 100644 cli/js/ops/fs/truncate.ts create mode 100644 cli/js/ops/fs/utime.ts create mode 100644 cli/js/ops/net.ts create mode 100644 cli/js/ops/tls.ts delete mode 100644 cli/js/read_dir.ts delete mode 100644 cli/js/read_link.ts delete mode 100644 cli/js/realpath.ts delete mode 100644 cli/js/remove.ts delete mode 100644 cli/js/rename.ts delete mode 100644 cli/js/stat.ts delete mode 100644 cli/js/symlink.ts delete mode 100644 cli/js/truncate.ts delete mode 100644 cli/js/utime.ts (limited to 'cli') diff --git a/cli/js/chmod.ts b/cli/js/chmod.ts deleted file mode 100644 index b6238f02d..000000000 --- a/cli/js/chmod.ts +++ /dev/null @@ -1,22 +0,0 @@ -// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -import { sendSync, sendAsync } from "./ops/dispatch_json.ts"; - -/** Synchronously changes the permission of a specific file/directory of - * specified path. Ignores the process's umask. - * - * Deno.chmodSync("/path/to/file", 0o666); - * - * Requires `allow-write` permission. */ -export function chmodSync(path: string, mode: number): void { - sendSync("op_chmod", { path, mode }); -} - -/** Changes the permission of a specific file/directory of specified path. - * Ignores the process's umask. - * - * await Deno.chmod("/path/to/file", 0o666); - * - * Requires `allow-write` permission. */ -export async function chmod(path: string, mode: number): Promise { - await sendAsync("op_chmod", { path, mode }); -} diff --git a/cli/js/chown.ts b/cli/js/chown.ts deleted file mode 100644 index a89c58f5b..000000000 --- a/cli/js/chown.ts +++ /dev/null @@ -1,32 +0,0 @@ -// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -import { sendSync, sendAsync } from "./ops/dispatch_json.ts"; - -/** Synchronously change owner of a regular file or directory. Linux/Mac OS - * only at the moment. - * - * Requires `allow-write` permission. - * - * @param path path to the file - * @param uid user id of the new owner - * @param gid group id of the new owner - */ -export function chownSync(path: string, uid: number, gid: number): void { - sendSync("op_chown", { path, uid, gid }); -} - -/** Change owner of a regular file or directory. Linux/Mac OS only at the - * moment. - * - * Requires `allow-write` permission. - * - * @param path path to the file - * @param uid user id of the new owner - * @param gid group id of the new owner - */ -export async function chown( - path: string, - uid: number, - gid: number -): Promise { - await sendAsync("op_chown", { path, uid, gid }); -} diff --git a/cli/js/compiler_host.ts b/cli/js/compiler_host.ts index 585a7833e..b89c7163c 100644 --- a/cli/js/compiler_host.ts +++ b/cli/js/compiler_host.ts @@ -2,7 +2,7 @@ import { ASSETS, MediaType, SourceFile } from "./compiler_sourcefile.ts"; import { OUT_DIR, WriteFileCallback, getAsset } from "./compiler_util.ts"; -import { cwd } from "./dir.ts"; +import { cwd } from "./ops/fs/dir.ts"; import { assert, notImplemented } from "./util.ts"; import * as util from "./util.ts"; diff --git a/cli/js/compiler_imports.ts b/cli/js/compiler_imports.ts index 377df4098..a5f3cd17e 100644 --- a/cli/js/compiler_imports.ts +++ b/cli/js/compiler_imports.ts @@ -6,7 +6,7 @@ import { SourceFileJson } from "./compiler_sourcefile.ts"; import { normalizeString, CHAR_FORWARD_SLASH } from "./compiler_util.ts"; -import { cwd } from "./dir.ts"; +import { cwd } from "./ops/fs/dir.ts"; import { assert } from "./util.ts"; import * as util from "./util.ts"; import * as compilerOps from "./ops/compiler.ts"; diff --git a/cli/js/copy_file.ts b/cli/js/copy_file.ts deleted file mode 100644 index 6c9bf6b03..000000000 --- a/cli/js/copy_file.ts +++ /dev/null @@ -1,29 +0,0 @@ -// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -import { sendSync, sendAsync } from "./ops/dispatch_json.ts"; - -/** Synchronously copies the contents and permissions of one file to another - * specified path, by default creating a new file if needed, else overwriting. - * Fails if target path is a directory or is unwritable. - * - * Deno.copyFileSync("from.txt", "to.txt"); - * - * Requires `allow-read` permission on fromPath. - * Requires `allow-write` permission on toPath. */ -export function copyFileSync(fromPath: string, toPath: string): void { - sendSync("op_copy_file", { from: fromPath, to: toPath }); -} - -/** Copies the contents and permissions of one file to another specified path, - * by default creating a new file if needed, else overwriting. Fails if target - * path is a directory or is unwritable. - * - * await Deno.copyFile("from.txt", "to.txt"); - * - * Requires `allow-read` permission on fromPath. - * Requires `allow-write` permission on toPath. */ -export async function copyFile( - fromPath: string, - toPath: string -): Promise { - await sendAsync("op_copy_file", { from: fromPath, to: toPath }); -} diff --git a/cli/js/deno.ts b/cli/js/deno.ts index a4b524f8d..23fcfda5d 100644 --- a/cli/js/deno.ts +++ b/cli/js/deno.ts @@ -9,18 +9,18 @@ export { writeAllSync } from "./buffer.ts"; export { build, OperatingSystem, Arch } from "./build.ts"; -export { chmodSync, chmod } from "./chmod.ts"; -export { chownSync, chown } from "./chown.ts"; +export { chmodSync, chmod } from "./ops/fs/chmod.ts"; +export { chownSync, chown } from "./ops/fs/chown.ts"; export { transpileOnly, compile, bundle } from "./compiler_api.ts"; export { inspect } from "./console.ts"; -export { copyFileSync, copyFile } from "./copy_file.ts"; +export { copyFileSync, copyFile } from "./ops/fs/copy_file.ts"; export { Diagnostic, DiagnosticCategory, DiagnosticItem, DiagnosticMessageChain } from "./diagnostics.ts"; -export { chdir, cwd } from "./dir.ts"; +export { chdir, cwd } from "./ops/fs/dir.ts"; export { applySourceMap, formatDiagnostics } from "./ops/errors.ts"; export { errors } from "./errors.ts"; export { FileInfo } from "./file_info.ts"; @@ -59,16 +59,16 @@ export { ReadWriteCloser, ReadWriteSeeker } from "./io.ts"; -export { linkSync, link } from "./link.ts"; +export { linkSync, link } from "./ops/fs/link.ts"; export { makeTempDirSync, makeTempDir, makeTempFileSync, makeTempFile, MakeTempOptions -} from "./make_temp.ts"; +} from "./ops/fs/make_temp.ts"; export { metrics, Metrics } from "./ops/runtime.ts"; -export { mkdirSync, mkdir, MkdirOptions } from "./mkdir.ts"; +export { mkdirSync, mkdir, MkdirOptions } from "./ops/fs/mkdir.ts"; export { Addr, connect, @@ -100,20 +100,20 @@ export { export { openPlugin } from "./plugins.ts"; export { kill } from "./ops/process.ts"; export { run, RunOptions, Process, ProcessStatus, Signal } from "./process.ts"; -export { readdirSync, readdir } from "./read_dir.ts"; +export { readdirSync, readdir } from "./ops/fs/read_dir.ts"; export { readFileSync, readFile } from "./read_file.ts"; -export { readlinkSync, readlink } from "./read_link.ts"; -export { realpathSync, realpath } from "./realpath.ts"; -export { removeSync, remove, RemoveOptions } from "./remove.ts"; -export { renameSync, rename } from "./rename.ts"; +export { readlinkSync, readlink } from "./ops/fs/read_link.ts"; +export { realpathSync, realpath } from "./ops/fs/realpath.ts"; +export { removeSync, remove, RemoveOptions } from "./ops/fs/remove.ts"; +export { renameSync, rename } from "./ops/fs/rename.ts"; export { resources, close } from "./ops/resources.ts"; export { signal, signals, SignalStream } from "./signals.ts"; -export { statSync, lstatSync, stat, lstat } from "./stat.ts"; -export { symlinkSync, symlink } from "./symlink.ts"; +export { statSync, lstatSync, stat, lstat } from "./ops/fs/stat.ts"; +export { symlinkSync, symlink } from "./ops/fs/symlink.ts"; export { connectTLS, listenTLS } from "./tls.ts"; -export { truncateSync, truncate } from "./truncate.ts"; +export { truncateSync, truncate } from "./ops/fs/truncate.ts"; export { isatty, setRaw } from "./ops/tty.ts"; -export { utimeSync, utime } from "./utime.ts"; +export { utimeSync, utime } from "./ops/fs/utime.ts"; export { version } from "./version.ts"; export { writeFileSync, writeFile, WriteFileOptions } from "./write_file.ts"; export const args: string[] = []; diff --git a/cli/js/dir.ts b/cli/js/dir.ts deleted file mode 100644 index 7a809a664..000000000 --- a/cli/js/dir.ts +++ /dev/null @@ -1,27 +0,0 @@ -// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -import { sendSync } from "./ops/dispatch_json.ts"; - -/** - * **UNSTABLE**: maybe needs permissions. - * - * Return a string representing the current working directory. - * - * If the current directory can be reached via multiple paths (due to symbolic - * links), `cwd()` may return any one of them. - * - * Throws `Deno.errors.NotFound` if directory not available. - */ -export function cwd(): string { - return sendSync("op_cwd"); -} - -/** - * **UNSTABLE**: maybe needs permissions. - * - * Change the current working directory to the specified path. - * - * Throws `Deno.errors.NotFound` if directory not available. - */ -export function chdir(directory: string): void { - sendSync("op_chdir", { directory }); -} diff --git a/cli/js/file_info.ts b/cli/js/file_info.ts index 5e4da2a77..884f850d2 100644 --- a/cli/js/file_info.ts +++ b/cli/js/file_info.ts @@ -1,5 +1,5 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -import { StatResponse } from "./stat.ts"; +import { StatResponse } from "./ops/fs/stat.ts"; import { build } from "./build.ts"; /** A FileInfo describes a file and is returned by `stat`, `lstat`, diff --git a/cli/js/files.ts b/cli/js/files.ts index 9103ab60b..945390ca0 100644 --- a/cli/js/files.ts +++ b/cli/js/files.ts @@ -10,12 +10,17 @@ import { SyncWriter, SyncSeeker } from "./io.ts"; -import { - sendSync as sendSyncJson, - sendAsync as sendAsyncJson -} from "./ops/dispatch_json.ts"; import { close } from "./ops/resources.ts"; import { read, readSync, write, writeSync } from "./ops/io.ts"; +import { seek, seekSync } from "./ops/fs/seek.ts"; +export { seek, seekSync } from "./ops/fs/seek.ts"; +import { + open as opOpen, + openSync as opOpenSync, + OpenOptions, + OpenMode +} from "./ops/fs/open.ts"; +export { OpenOptions, OpenMode } from "./ops/fs/open.ts"; /** Synchronously open a file and return an instance of the `File` object. * @@ -24,31 +29,22 @@ import { read, readSync, write, writeSync } from "./ops/io.ts"; * Requires `allow-read` and `allow-write` permissions depending on mode. */ export function openSync(path: string, mode?: OpenOptions): File; - -/** Synchronously open a file and return an instance of the `File` object. - * - * const file = Deno.openSync("/foo/bar.txt", "r"); - * - * Requires `allow-read` and `allow-write` permissions depending on mode. - */ export function openSync(path: string, mode?: OpenMode): File; - -/**@internal*/ export function openSync( path: string, modeOrOptions: OpenOptions | OpenMode = "r" ): File { - let mode = null; - let options = null; + let mode = undefined; + let options = undefined; if (typeof modeOrOptions === "string") { mode = modeOrOptions; } else { checkOpenOptions(modeOrOptions); - options = modeOrOptions; + options = modeOrOptions as OpenOptions; } - const rid = sendSyncJson("op_open", { path, options, mode }); + const rid = opOpenSync(path, mode as OpenMode, options); return new File(rid); } @@ -59,35 +55,22 @@ export function openSync( * Requires `allow-read` and `allow-write` permissions depending on mode. */ export async function open(path: string, options?: OpenOptions): Promise; - -/** Open a file and resolves to an instance of `Deno.File`. - * - * const file = await Deno.open("/foo/bar.txt, "w+"); - * - * Requires `allow-read` and `allow-write` permissions depending on mode. - */ export async function open(path: string, mode?: OpenMode): Promise; - -/**@internal*/ export async function open( path: string, modeOrOptions: OpenOptions | OpenMode = "r" ): Promise { - let mode = null; - let options = null; + let mode = undefined; + let options = undefined; if (typeof modeOrOptions === "string") { mode = modeOrOptions; } else { checkOpenOptions(modeOrOptions); - options = modeOrOptions; + options = modeOrOptions as OpenOptions; } - const rid = await sendAsyncJson("op_open", { - path, - options, - mode - }); + const rid = await opOpen(path, mode as OpenMode, options); return new File(rid); } @@ -113,36 +96,6 @@ export function create(path: string): Promise { return open(path, "w+"); } -/** Synchronously seek a file ID to the given offset under mode given by `whence`. - * - * Returns the number of cursor position. - * - * const file = Deno.openSync("/foo/bar.txt"); - * const position = Deno.seekSync(file.rid, 0, 0); - */ -export function seekSync( - rid: number, - offset: number, - whence: SeekMode -): number { - return sendSyncJson("op_seek", { rid, offset, whence }); -} - -/** Seek a file ID to the given offset under mode given by `whence`. - * - * Resolves with the number of cursor position. - * - * const file = await Deno.open("/foo/bar.txt"); - * const position = await Deno.seek(file.rid, 0, 0); - */ -export async function seek( - rid: number, - offset: number, - whence: SeekMode -): Promise { - return await sendAsyncJson("op_seek", { rid, offset, whence }); -} - /** The Deno abstraction for reading and writing files. */ export class File implements @@ -191,51 +144,6 @@ export const stdout = new File(1); /** An instance of `Deno.File` for `stderr`. */ export const stderr = new File(2); -export interface OpenOptions { - /** Sets the option for read access. This option, when `true`, means that the - * file should be read-able if opened. */ - read?: boolean; - /** Sets the option for write access. This option, when `true`, means that - * the file should be write-able if opened. If the file already exists, - * any write calls on it will overwrite its contents, by default without - * truncating it. */ - write?: boolean; - /**Sets the option for the append mode. This option, when `true`, means that - * writes will append to a file instead of overwriting previous contents. - * Note that setting `{ write: true, append: true }` has the same effect as - * setting only `{ append: true }`. */ - append?: boolean; - /** Sets the option for truncating a previous file. If a file is - * successfully opened with this option set it will truncate the file to `0` - * length if it already exists. The file must be opened with write access - * for truncate to work. */ - truncate?: boolean; - /** Sets the option to allow creating a new file, if one doesn't already - * exist at the specified path. Requires write or append access to be - * used. */ - create?: boolean; - /** Defaults to `false`. If set to `true`, no file, directory, or symlink is - * allowed to exist at the target location. Requires write or append - * access to be used. When createNew is set to `true`, create and truncate - * are ignored. */ - createNew?: boolean; -} - -/** A set of string literals which specify the open mode of a file. - * - * |Value |Description | - * |------|--------------------------------------------------------------------------------------------------| - * |`"r"` |Read-only. Default. Starts at beginning of file. | - * |`"r+"`|Read-write. Start at beginning of file. | - * |`"w"` |Write-only. Opens and truncates existing file or creates new one for writing only. | - * |`"w+"`|Read-write. Opens and truncates existing file or creates new one for writing and reading. | - * |`"a"` |Write-only. Opens existing file or creates new one. Each write appends content to the end of file.| - * |`"a+"`|Read-write. Behaves like `"a"` and allows to read from file. | - * |`"x"` |Write-only. Exclusive create - creates new file only if one doesn't exist already. | - * |`"x+"`|Read-write. Behaves like `x` and allows reading from file. | - */ -export type OpenMode = "r" | "r+" | "w" | "w+" | "a" | "a+" | "x" | "x+"; - /** Check if OpenOptions is set to valid combination of options. * @returns Tuple representing if openMode is valid and error message if it's not * @internal diff --git a/cli/js/link.ts b/cli/js/link.ts deleted file mode 100644 index a2ce33d9f..000000000 --- a/cli/js/link.ts +++ /dev/null @@ -1,20 +0,0 @@ -// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -import { sendSync, sendAsync } from "./ops/dispatch_json.ts"; - -/** Creates `newname` as a hard link to `oldname`. - * - * Deno.linkSync("old/name", "new/name"); - * - * Requires `allow-read` and `allow-write` permissions. */ -export function linkSync(oldname: string, newname: string): void { - sendSync("op_link", { oldname, newname }); -} - -/** Creates `newname` as a hard link to `oldname`. - * - * await Deno.link("old/name", "new/name"); - * - * Requires `allow-read` and `allow-write` permissions. */ -export async function link(oldname: string, newname: string): Promise { - await sendAsync("op_link", { oldname, newname }); -} diff --git a/cli/js/make_temp.ts b/cli/js/make_temp.ts deleted file mode 100644 index 87c694204..000000000 --- a/cli/js/make_temp.ts +++ /dev/null @@ -1,90 +0,0 @@ -// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -import { sendSync, sendAsync } from "./ops/dispatch_json.ts"; - -export interface MakeTempOptions { - /** Directory where the temporary directory should be created (defaults to - * the env variable TMPDIR, or the system's default, usually /tmp). */ - dir?: string; - /** String that should precede the random portion of the temporary - * directory's name. */ - prefix?: string; - /** String that should follow the random portion of the temporary - * directory's name. */ - suffix?: string; -} - -/** Synchronously creates a new temporary directory in the directory `dir`, - * its name beginning with `prefix` and ending with `suffix`. - * - * It returns the full path to the newly created directory. - * - * If `dir` is unspecified, uses the default directory for temporary files. - * Multiple programs calling this function simultaneously will create different - * directories. It is the caller's responsibility to remove the directory when - * no longer needed. - * - * const tempDirName0 = Deno.makeTempDirSync(); - * const tempDirName1 = Deno.makeTempDirSync({ prefix: 'my_temp' }); - * - * Requires `allow-write` permission. */ -export function makeTempDirSync(options: MakeTempOptions = {}): string { - return sendSync("op_make_temp_dir", options); -} - -/** Creates a new temporary directory in the directory `dir`, its name - * beginning with `prefix` and ending with `suffix`. - * - * It resolves to the full path to the newly created directory. - * - * If `dir` is unspecified, uses the default directory for temporary files. - * Multiple programs calling this function simultaneously will create different - * directories. It is the caller's responsibility to remove the directory when - * no longer needed. - * - * const tempDirName0 = await Deno.makeTempDir(); - * const tempDirName1 = await Deno.makeTempDir({ prefix: 'my_temp' }); - * - * Requires `allow-write` permission. */ -export async function makeTempDir( - options: MakeTempOptions = {} -): Promise { - return await sendAsync("op_make_temp_dir", options); -} - -/** Synchronously creates a new temporary file in the directory `dir`, its name - * beginning with `prefix` and ending with `suffix`. - * - * It returns the full path to the newly created file. - * - * If `dir` is unspecified, uses the default directory for temporary files. - * Multiple programs calling this function simultaneously will create different - * files. It is the caller's responsibility to remove the file when - * no longer needed. - * - * const tempFileName0 = Deno.makeTempFileSync(); - * const tempFileName1 = Deno.makeTempFileSync({ prefix: 'my_temp' }); - * - * Requires `allow-write` permission. */ -export function makeTempFileSync(options: MakeTempOptions = {}): string { - return sendSync("op_make_temp_file", options); -} - -/** Creates a new temporary file in the directory `dir`, its name - * beginning with `prefix` and ending with `suffix`. - * - * It resolves to the full path to the newly created file. - * - * If `dir` is unspecified, uses the default directory for temporary files. - * Multiple programs calling this function simultaneously will create different - * files. It is the caller's responsibility to remove the file when - * no longer needed. - * - * const tempFileName0 = await Deno.makeTempFile(); - * const tempFileName1 = await Deno.makeTempFile({ prefix: 'my_temp' }); - * - * Requires `allow-write` permission. */ -export async function makeTempFile( - options: MakeTempOptions = {} -): Promise { - return await sendAsync("op_make_temp_file", options); -} diff --git a/cli/js/mkdir.ts b/cli/js/mkdir.ts deleted file mode 100644 index 3f4a18b19..000000000 --- a/cli/js/mkdir.ts +++ /dev/null @@ -1,67 +0,0 @@ -// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -import { sendSync, sendAsync } from "./ops/dispatch_json.ts"; - -// TODO(ry) The complexity in argument parsing is to support deprecated forms of -// mkdir and mkdirSync. -function mkdirArgs( - path: string, - optionsOrRecursive?: MkdirOptions | boolean, - mode?: number -): { path: string; recursive: boolean; mode: number } { - const args = { path, recursive: false, mode: 0o777 }; - if (typeof optionsOrRecursive == "boolean") { - args.recursive = optionsOrRecursive; - if (mode) { - args.mode = mode; - } - } else if (optionsOrRecursive) { - if (typeof optionsOrRecursive.recursive == "boolean") { - args.recursive = optionsOrRecursive.recursive; - } - if (optionsOrRecursive.mode) { - args.mode = optionsOrRecursive.mode; - } - } - return args; -} - -export interface MkdirOptions { - /** Defaults to `false`. If set to `true`, means that any intermediate - * directories will also be created (as with the shell command `mkdir -p`). - * Intermediate directories are created with the same permissions. - * When recursive is set to `true`, succeeds silently (without changing any - * permissions) if a directory already exists at the path. */ - recursive?: boolean; - /** Permissions to use when creating the directory (defaults to `0o777`, - * before the process's umask). - * Does nothing/raises on Windows. */ - mode?: number; -} - -/** Synchronously creates a new directory with the specified path. - * - * Deno.mkdirSync("new_dir"); - * Deno.mkdirSync("nested/directories", { recursive: true }); - * - * Requires `allow-write` permission. */ -export function mkdirSync( - path: string, - optionsOrRecursive?: MkdirOptions | boolean, - mode?: number -): void { - sendSync("op_mkdir", mkdirArgs(path, optionsOrRecursive, mode)); -} - -/** Creates a new directory with the specified path. - * - * await Deno.mkdir("new_dir"); - * await Deno.mkdir("nested/directories", { recursive: true }); - * - * Requires `allow-write` permission. */ -export async function mkdir( - path: string, - optionsOrRecursive?: MkdirOptions | boolean, - mode?: number -): Promise { - await sendAsync("op_mkdir", mkdirArgs(path, optionsOrRecursive, mode)); -} diff --git a/cli/js/net.ts b/cli/js/net.ts index 3b0525096..ccc5bf89e 100644 --- a/cli/js/net.ts +++ b/cli/js/net.ts @@ -2,11 +2,9 @@ import { EOF, Reader, Writer, Closer } from "./io.ts"; import { read, write } from "./ops/io.ts"; import { close } from "./ops/resources.ts"; -import { sendSync, sendAsync } from "./ops/dispatch_json.ts"; - -export type Transport = "tcp" | "udp"; -// TODO support other types: -// export type Transport = "tcp" | "tcp4" | "tcp6" | "unix" | "unixpacket"; +import * as netOps from "./ops/net.ts"; +import { Transport } from "./ops/net.ts"; +export { ShutdownMode, shutdown, Transport } from "./ops/net.ts"; export interface Addr { transport: Transport; @@ -55,26 +53,6 @@ export interface Listener extends AsyncIterator { [Symbol.asyncIterator](): AsyncIterator; } -export enum ShutdownMode { - // See http://man7.org/linux/man-pages/man2/shutdown.2.html - // Corresponding to SHUT_RD, SHUT_WR, SHUT_RDWR - Read = 0, - Write, - ReadWrite // unused -} - -/** Shut down socket send and receive operations. - * - * Matches behavior of POSIX shutdown(3). - * - * const listener = Deno.listen({ port: 80 }); - * const conn = await listener.accept(); - * Deno.shutdown(conn.rid, Deno.ShutdownMode.Write); - */ -export function shutdown(rid: number, how: ShutdownMode): void { - sendSync("op_shutdown", { rid, how }); -} - export class ConnImpl implements Conn { constructor( readonly rid: number, @@ -98,14 +76,14 @@ export class ConnImpl implements Conn { * Most callers should just use close(). */ closeRead(): void { - shutdown(this.rid, ShutdownMode.Read); + netOps.shutdown(this.rid, netOps.ShutdownMode.Read); } /** closeWrite shuts down (shutdown(2)) the writing side of the TCP * connection. Most callers should just use close(). */ closeWrite(): void { - shutdown(this.rid, ShutdownMode.Write); + netOps.shutdown(this.rid, netOps.ShutdownMode.Write); } } @@ -117,7 +95,7 @@ export class ListenerImpl implements Listener { ) {} async accept(): Promise { - const res = await sendAsync("op_accept", { rid: this.rid }); + const res = await netOps.accept(this.rid); return new ConnImpl(res.rid, res.remoteAddr, res.localAddr); } @@ -152,7 +130,7 @@ export async function recvfrom( rid: number, p: Uint8Array ): Promise<[number, Addr]> { - const { size, remoteAddr } = await sendAsync("op_receive", { rid }, p); + const { size, remoteAddr } = await netOps.receive(rid, p); return [size, remoteAddr]; } @@ -175,7 +153,7 @@ export class UDPConnImpl implements UDPConn { const remote = { hostname: "127.0.0.1", transport: "udp", ...addr }; if (remote.transport !== "udp") throw Error("Remote transport must be UDP"); const args = { ...remote, rid: this.rid }; - await sendAsync("op_send", args, p); + await netOps.send(args as netOps.SendRequest, p); } close(): void { @@ -253,7 +231,7 @@ export function listen( export function listen(options: ListenOptions & { transport: "udp" }): UDPConn; export function listen(options: ListenOptions): Listener | UDPConn { const args = { ...listenDefaults, ...options }; - const res = sendSync("op_listen", args); + const res = netOps.listen(args as netOps.ListenRequest); if (args.transport === "tcp") { return new ListenerImpl(res.rid, res.localAddr); @@ -289,6 +267,6 @@ const connectDefaults = { hostname: "127.0.0.1", transport: "tcp" }; */ export async function connect(options: ConnectOptions): Promise { options = Object.assign(connectDefaults, options); - const res = await sendAsync("op_connect", options); + const res = await netOps.connect(options as netOps.ConnectRequest); return new ConnImpl(res.rid, res.remoteAddr!, res.localAddr!); } diff --git a/cli/js/ops/fs/chmod.ts b/cli/js/ops/fs/chmod.ts new file mode 100644 index 000000000..9e748672f --- /dev/null +++ b/cli/js/ops/fs/chmod.ts @@ -0,0 +1,22 @@ +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. +import { sendSync, sendAsync } from "../dispatch_json.ts"; + +/** Synchronously changes the permission of a specific file/directory of + * specified path. Ignores the process's umask. + * + * Deno.chmodSync("/path/to/file", 0o666); + * + * Requires `allow-write` permission. */ +export function chmodSync(path: string, mode: number): void { + sendSync("op_chmod", { path, mode }); +} + +/** Changes the permission of a specific file/directory of specified path. + * Ignores the process's umask. + * + * await Deno.chmod("/path/to/file", 0o666); + * + * Requires `allow-write` permission. */ +export async function chmod(path: string, mode: number): Promise { + await sendAsync("op_chmod", { path, mode }); +} diff --git a/cli/js/ops/fs/chown.ts b/cli/js/ops/fs/chown.ts new file mode 100644 index 000000000..6f871f313 --- /dev/null +++ b/cli/js/ops/fs/chown.ts @@ -0,0 +1,32 @@ +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. +import { sendSync, sendAsync } from "../dispatch_json.ts"; + +/** Synchronously change owner of a regular file or directory. Linux/Mac OS + * only at the moment. + * + * Requires `allow-write` permission. + * + * @param path path to the file + * @param uid user id of the new owner + * @param gid group id of the new owner + */ +export function chownSync(path: string, uid: number, gid: number): void { + sendSync("op_chown", { path, uid, gid }); +} + +/** Change owner of a regular file or directory. Linux/Mac OS only at the + * moment. + * + * Requires `allow-write` permission. + * + * @param path path to the file + * @param uid user id of the new owner + * @param gid group id of the new owner + */ +export async function chown( + path: string, + uid: number, + gid: number +): Promise { + await sendAsync("op_chown", { path, uid, gid }); +} diff --git a/cli/js/ops/fs/copy_file.ts b/cli/js/ops/fs/copy_file.ts new file mode 100644 index 000000000..6bd49fe2c --- /dev/null +++ b/cli/js/ops/fs/copy_file.ts @@ -0,0 +1,29 @@ +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. +import { sendSync, sendAsync } from "../dispatch_json.ts"; + +/** Synchronously copies the contents and permissions of one file to another + * specified path, by default creating a new file if needed, else overwriting. + * Fails if target path is a directory or is unwritable. + * + * Deno.copyFileSync("from.txt", "to.txt"); + * + * Requires `allow-read` permission on fromPath. + * Requires `allow-write` permission on toPath. */ +export function copyFileSync(fromPath: string, toPath: string): void { + sendSync("op_copy_file", { from: fromPath, to: toPath }); +} + +/** Copies the contents and permissions of one file to another specified path, + * by default creating a new file if needed, else overwriting. Fails if target + * path is a directory or is unwritable. + * + * await Deno.copyFile("from.txt", "to.txt"); + * + * Requires `allow-read` permission on fromPath. + * Requires `allow-write` permission on toPath. */ +export async function copyFile( + fromPath: string, + toPath: string +): Promise { + await sendAsync("op_copy_file", { from: fromPath, to: toPath }); +} diff --git a/cli/js/ops/fs/dir.ts b/cli/js/ops/fs/dir.ts new file mode 100644 index 000000000..e9e95005b --- /dev/null +++ b/cli/js/ops/fs/dir.ts @@ -0,0 +1,27 @@ +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. +import { sendSync } from "../dispatch_json.ts"; + +/** + * **UNSTABLE**: maybe needs permissions. + * + * Return a string representing the current working directory. + * + * If the current directory can be reached via multiple paths (due to symbolic + * links), `cwd()` may return any one of them. + * + * Throws `Deno.errors.NotFound` if directory not available. + */ +export function cwd(): string { + return sendSync("op_cwd"); +} + +/** + * **UNSTABLE**: maybe needs permissions. + * + * Change the current working directory to the specified path. + * + * Throws `Deno.errors.NotFound` if directory not available. + */ +export function chdir(directory: string): void { + sendSync("op_chdir", { directory }); +} diff --git a/cli/js/ops/fs/link.ts b/cli/js/ops/fs/link.ts new file mode 100644 index 000000000..0f083dd7b --- /dev/null +++ b/cli/js/ops/fs/link.ts @@ -0,0 +1,20 @@ +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. +import { sendSync, sendAsync } from "../dispatch_json.ts"; + +/** Creates `newname` as a hard link to `oldname`. + * + * Deno.linkSync("old/name", "new/name"); + * + * Requires `allow-read` and `allow-write` permissions. */ +export function linkSync(oldname: string, newname: string): void { + sendSync("op_link", { oldname, newname }); +} + +/** Creates `newname` as a hard link to `oldname`. + * + * await Deno.link("old/name", "new/name"); + * + * Requires `allow-read` and `allow-write` permissions. */ +export async function link(oldname: string, newname: string): Promise { + await sendAsync("op_link", { oldname, newname }); +} diff --git a/cli/js/ops/fs/make_temp.ts b/cli/js/ops/fs/make_temp.ts new file mode 100644 index 000000000..1fbff41d0 --- /dev/null +++ b/cli/js/ops/fs/make_temp.ts @@ -0,0 +1,90 @@ +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. +import { sendSync, sendAsync } from "../dispatch_json.ts"; + +export interface MakeTempOptions { + /** Directory where the temporary directory should be created (defaults to + * the env variable TMPDIR, or the system's default, usually /tmp). */ + dir?: string; + /** String that should precede the random portion of the temporary + * directory's name. */ + prefix?: string; + /** String that should follow the random portion of the temporary + * directory's name. */ + suffix?: string; +} + +/** Synchronously creates a new temporary directory in the directory `dir`, + * its name beginning with `prefix` and ending with `suffix`. + * + * It returns the full path to the newly created directory. + * + * If `dir` is unspecified, uses the default directory for temporary files. + * Multiple programs calling this function simultaneously will create different + * directories. It is the caller's responsibility to remove the directory when + * no longer needed. + * + * const tempDirName0 = Deno.makeTempDirSync(); + * const tempDirName1 = Deno.makeTempDirSync({ prefix: 'my_temp' }); + * + * Requires `allow-write` permission. */ +export function makeTempDirSync(options: MakeTempOptions = {}): string { + return sendSync("op_make_temp_dir", options); +} + +/** Creates a new temporary directory in the directory `dir`, its name + * beginning with `prefix` and ending with `suffix`. + * + * It resolves to the full path to the newly created directory. + * + * If `dir` is unspecified, uses the default directory for temporary files. + * Multiple programs calling this function simultaneously will create different + * directories. It is the caller's responsibility to remove the directory when + * no longer needed. + * + * const tempDirName0 = await Deno.makeTempDir(); + * const tempDirName1 = await Deno.makeTempDir({ prefix: 'my_temp' }); + * + * Requires `allow-write` permission. */ +export async function makeTempDir( + options: MakeTempOptions = {} +): Promise { + return await sendAsync("op_make_temp_dir", options); +} + +/** Synchronously creates a new temporary file in the directory `dir`, its name + * beginning with `prefix` and ending with `suffix`. + * + * It returns the full path to the newly created file. + * + * If `dir` is unspecified, uses the default directory for temporary files. + * Multiple programs calling this function simultaneously will create different + * files. It is the caller's responsibility to remove the file when + * no longer needed. + * + * const tempFileName0 = Deno.makeTempFileSync(); + * const tempFileName1 = Deno.makeTempFileSync({ prefix: 'my_temp' }); + * + * Requires `allow-write` permission. */ +export function makeTempFileSync(options: MakeTempOptions = {}): string { + return sendSync("op_make_temp_file", options); +} + +/** Creates a new temporary file in the directory `dir`, its name + * beginning with `prefix` and ending with `suffix`. + * + * It resolves to the full path to the newly created file. + * + * If `dir` is unspecified, uses the default directory for temporary files. + * Multiple programs calling this function simultaneously will create different + * files. It is the caller's responsibility to remove the file when + * no longer needed. + * + * const tempFileName0 = await Deno.makeTempFile(); + * const tempFileName1 = await Deno.makeTempFile({ prefix: 'my_temp' }); + * + * Requires `allow-write` permission. */ +export async function makeTempFile( + options: MakeTempOptions = {} +): Promise { + return await sendAsync("op_make_temp_file", options); +} diff --git a/cli/js/ops/fs/mkdir.ts b/cli/js/ops/fs/mkdir.ts new file mode 100644 index 000000000..4df33a29f --- /dev/null +++ b/cli/js/ops/fs/mkdir.ts @@ -0,0 +1,67 @@ +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. +import { sendSync, sendAsync } from "../dispatch_json.ts"; + +// TODO(ry) The complexity in argument parsing is to support deprecated forms of +// mkdir and mkdirSync. +function mkdirArgs( + path: string, + optionsOrRecursive?: MkdirOptions | boolean, + mode?: number +): { path: string; recursive: boolean; mode: number } { + const args = { path, recursive: false, mode: 0o777 }; + if (typeof optionsOrRecursive == "boolean") { + args.recursive = optionsOrRecursive; + if (mode) { + args.mode = mode; + } + } else if (optionsOrRecursive) { + if (typeof optionsOrRecursive.recursive == "boolean") { + args.recursive = optionsOrRecursive.recursive; + } + if (optionsOrRecursive.mode) { + args.mode = optionsOrRecursive.mode; + } + } + return args; +} + +export interface MkdirOptions { + /** Defaults to `false`. If set to `true`, means that any intermediate + * directories will also be created (as with the shell command `mkdir -p`). + * Intermediate directories are created with the same permissions. + * When recursive is set to `true`, succeeds silently (without changing any + * permissions) if a directory already exists at the path. */ + recursive?: boolean; + /** Permissions to use when creating the directory (defaults to `0o777`, + * before the process's umask). + * Does nothing/raises on Windows. */ + mode?: number; +} + +/** Synchronously creates a new directory with the specified path. + * + * Deno.mkdirSync("new_dir"); + * Deno.mkdirSync("nested/directories", { recursive: true }); + * + * Requires `allow-write` permission. */ +export function mkdirSync( + path: string, + optionsOrRecursive?: MkdirOptions | boolean, + mode?: number +): void { + sendSync("op_mkdir", mkdirArgs(path, optionsOrRecursive, mode)); +} + +/** Creates a new directory with the specified path. + * + * await Deno.mkdir("new_dir"); + * await Deno.mkdir("nested/directories", { recursive: true }); + * + * Requires `allow-write` permission. */ +export async function mkdir( + path: string, + optionsOrRecursive?: MkdirOptions | boolean, + mode?: number +): Promise { + await sendAsync("op_mkdir", mkdirArgs(path, optionsOrRecursive, mode)); +} diff --git a/cli/js/ops/fs/open.ts b/cli/js/ops/fs/open.ts new file mode 100644 index 000000000..e166ef16b --- /dev/null +++ b/cli/js/ops/fs/open.ts @@ -0,0 +1,67 @@ +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. +import { sendSync, sendAsync } from "../dispatch_json.ts"; + +export interface OpenOptions { + /** Sets the option for read access. This option, when `true`, means that the + * file should be read-able if opened. */ + read?: boolean; + /** Sets the option for write access. This option, when `true`, means that + * the file should be write-able if opened. If the file already exists, + * any write calls on it will overwrite its contents, by default without + * truncating it. */ + write?: boolean; + /**Sets the option for the append mode. This option, when `true`, means that + * writes will append to a file instead of overwriting previous contents. + * Note that setting `{ write: true, append: true }` has the same effect as + * setting only `{ append: true }`. */ + append?: boolean; + /** Sets the option for truncating a previous file. If a file is + * successfully opened with this option set it will truncate the file to `0` + * length if it already exists. The file must be opened with write access + * for truncate to work. */ + truncate?: boolean; + /** Sets the option to allow creating a new file, if one doesn't already + * exist at the specified path. Requires write or append access to be + * used. */ + create?: boolean; + /** Defaults to `false`. If set to `true`, no file, directory, or symlink is + * allowed to exist at the target location. Requires write or append + * access to be used. When createNew is set to `true`, create and truncate + * are ignored. */ + createNew?: boolean; +} + +/** A set of string literals which specify the open mode of a file. + * + * |Value |Description | + * |------|--------------------------------------------------------------------------------------------------| + * |`"r"` |Read-only. Default. Starts at beginning of file. | + * |`"r+"`|Read-write. Start at beginning of file. | + * |`"w"` |Write-only. Opens and truncates existing file or creates new one for writing only. | + * |`"w+"`|Read-write. Opens and truncates existing file or creates new one for writing and reading. | + * |`"a"` |Write-only. Opens existing file or creates new one. Each write appends content to the end of file.| + * |`"a+"`|Read-write. Behaves like `"a"` and allows to read from file. | + * |`"x"` |Write-only. Exclusive create - creates new file only if one doesn't exist already. | + * |`"x+"`|Read-write. Behaves like `x` and allows reading from file. | + */ +export type OpenMode = "r" | "r+" | "w" | "w+" | "a" | "a+" | "x" | "x+"; + +export function openSync( + path: string, + mode: OpenMode | undefined, + options: OpenOptions | undefined +): number { + return sendSync("op_open", { path, options, mode }); +} + +export async function open( + path: string, + mode: OpenMode | undefined, + options: OpenOptions | undefined +): Promise { + return await sendAsync("op_open", { + path, + options, + mode + }); +} diff --git a/cli/js/ops/fs/read_dir.ts b/cli/js/ops/fs/read_dir.ts new file mode 100644 index 000000000..75c821c33 --- /dev/null +++ b/cli/js/ops/fs/read_dir.ts @@ -0,0 +1,37 @@ +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. +import { sendSync, sendAsync } from "../dispatch_json.ts"; +import { FileInfo, FileInfoImpl } from "../../file_info.ts"; +import { StatResponse } from "./stat.ts"; + +interface ReadDirResponse { + entries: StatResponse[]; +} + +function res(response: ReadDirResponse): FileInfo[] { + return response.entries.map( + (statRes: StatResponse): FileInfo => { + return new FileInfoImpl(statRes); + } + ); +} + +/** Synchronously reads the directory given by `path` and returns an array of + * `Deno.FileInfo`. + * + * const files = Deno.readdirSync("/"); + * + * Requires `allow-read` permission. */ +export function readdirSync(path: string): FileInfo[] { + return res(sendSync("op_read_dir", { path })); +} + +/** UNSTABLE: Maybe need to return an `AsyncIterable`. + * + * Reads the directory given by `path` and resolves to an array of `Deno.FileInfo`. + * + * const files = await Deno.readdir("/"); + * + * Requires `allow-read` permission. */ +export async function readdir(path: string): Promise { + return res(await sendAsync("op_read_dir", { path })); +} diff --git a/cli/js/ops/fs/read_link.ts b/cli/js/ops/fs/read_link.ts new file mode 100644 index 000000000..b5ac82cd7 --- /dev/null +++ b/cli/js/ops/fs/read_link.ts @@ -0,0 +1,20 @@ +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. +import { sendSync, sendAsync } from "../dispatch_json.ts"; + +/** Returns the destination of the named symbolic link. + * + * const targetPath = Deno.readlinkSync("symlink/path"); + * + * Requires `allow-read` permission. */ +export function readlinkSync(path: string): string { + return sendSync("op_read_link", { path }); +} + +/** Resolves to the destination of the named symbolic link. + * + * const targetPath = await Deno.readlink("symlink/path"); + * + * Requires `allow-read` permission. */ +export async function readlink(path: string): Promise { + return await sendAsync("op_read_link", { path }); +} diff --git a/cli/js/ops/fs/realpath.ts b/cli/js/ops/fs/realpath.ts new file mode 100644 index 000000000..c8070edea --- /dev/null +++ b/cli/js/ops/fs/realpath.ts @@ -0,0 +1,18 @@ +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. +import { sendSync, sendAsync } from "../dispatch_json.ts"; + +/** Returns absolute normalized path with symbolic links resolved synchronously. + * + * const realPath = Deno.realpathSync("./some/path"); + */ +export function realpathSync(path: string): string { + return sendSync("op_realpath", { path }); +} + +/** Returns absolute normalized path with symbolic links resolved. + * + * const realPath = await Deno.realpath("./some/path"); + */ +export async function realpath(path: string): Promise { + return await sendAsync("op_realpath", { path }); +} diff --git a/cli/js/ops/fs/remove.ts b/cli/js/ops/fs/remove.ts new file mode 100644 index 000000000..565035793 --- /dev/null +++ b/cli/js/ops/fs/remove.ts @@ -0,0 +1,33 @@ +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. +import { sendSync, sendAsync } from "../dispatch_json.ts"; + +export interface RemoveOptions { + /** Defaults to `false`. If set to `true`, path will be removed even if + * it's a non-empty directory. */ + recursive?: boolean; +} + +/** Synchronously removes the named file or directory. Throws error if + * permission denied, path not found, or path is a non-empty directory and + * the `recursive` option isn't set to `true`. + * + * Deno.removeSync("/path/to/dir/or/file", { recursive: false }); + * + * Requires `allow-write` permission. */ +export function removeSync(path: string, options: RemoveOptions = {}): void { + sendSync("op_remove", { path, recursive: !!options.recursive }); +} + +/** Removes the named file or directory. Throws error if permission denied, + * path not found, or path is a non-empty directory and the `recursive` + * option isn't set to `true`. + * + * await Deno.remove("/path/to/dir/or/file", { recursive: false }); + * + * Requires `allow-write` permission. */ +export async function remove( + path: string, + options: RemoveOptions = {} +): Promise { + await sendAsync("op_remove", { path, recursive: !!options.recursive }); +} diff --git a/cli/js/ops/fs/rename.ts b/cli/js/ops/fs/rename.ts new file mode 100644 index 000000000..016ebc2d7 --- /dev/null +++ b/cli/js/ops/fs/rename.ts @@ -0,0 +1,25 @@ +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. +import { sendSync, sendAsync } from "../dispatch_json.ts"; + +/** Synchronously renames (moves) `oldpath` to `newpath`. If `newpath` already + * exists and is not a directory, `renameSync()` replaces it. OS-specific + * restrictions may apply when `oldpath` and `newpath` are in different + * directories. + * + * Deno.renameSync("old/path", "new/path"); + * + * Requires `allow-read` and `allow-write` permissions. */ +export function renameSync(oldpath: string, newpath: string): void { + sendSync("op_rename", { oldpath, newpath }); +} + +/** Renames (moves) `oldpath` to `newpath`. If `newpath` already exists and is + * not a directory, `rename()` replaces it. OS-specific restrictions may apply + * when `oldpath` and `newpath` are in different directories. + * + * await Deno.rename("old/path", "new/path"); + * + * Requires `allow-read` and `allow-write`. */ +export async function rename(oldpath: string, newpath: string): Promise { + await sendAsync("op_rename", { oldpath, newpath }); +} diff --git a/cli/js/ops/fs/seek.ts b/cli/js/ops/fs/seek.ts new file mode 100644 index 000000000..313d365c6 --- /dev/null +++ b/cli/js/ops/fs/seek.ts @@ -0,0 +1,33 @@ +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. +import { sendSync, sendAsync } from "../dispatch_json.ts"; +import { SeekMode } from "../../io.ts"; + +/** Synchronously seek a file ID to the given offset under mode given by `whence`. + * + * Returns the number of cursor position. + * + * const file = Deno.openSync("/foo/bar.txt"); + * const position = Deno.seekSync(file.rid, 0, 0); + */ +export function seekSync( + rid: number, + offset: number, + whence: SeekMode +): number { + return sendSync("op_seek", { rid, offset, whence }); +} + +/** Seek a file ID to the given offset under mode given by `whence`. + * + * Resolves with the number of cursor position. + * + * const file = await Deno.open("/foo/bar.txt"); + * const position = await Deno.seek(file.rid, 0, 0); + */ +export async function seek( + rid: number, + offset: number, + whence: SeekMode +): Promise { + return await sendAsync("op_seek", { rid, offset, whence }); +} diff --git a/cli/js/ops/fs/stat.ts b/cli/js/ops/fs/stat.ts new file mode 100644 index 000000000..6a764a8bb --- /dev/null +++ b/cli/js/ops/fs/stat.ts @@ -0,0 +1,84 @@ +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. +import { sendSync, sendAsync } from "../dispatch_json.ts"; +import { FileInfo, FileInfoImpl } from "../../file_info.ts"; + +/** @internal */ +export interface StatResponse { + isFile: boolean; + isSymlink: boolean; + len: number; + modified: number; + accessed: number; + created: number; + name: string | null; + // Unix only members + dev: number; + ino: number; + mode: number; + nlink: number; + uid: number; + gid: number; + rdev: number; + blksize: number; + blocks: number; +} + +/** Resolves to a `Deno.FileInfo` for the specified `path`. If `path` is a + * symlink, information for the symlink will be returned. + * + * const fileInfo = await Deno.lstat("hello.txt"); + * assert(fileInfo.isFile()); + * + * Requires `allow-read` permission. */ +export async function lstat(path: string): Promise { + const res = (await sendAsync("op_stat", { + path, + lstat: true + })) as StatResponse; + return new FileInfoImpl(res); +} + +/** Synchronously returns a `Deno.FileInfo` for the specified `path`. If + * `path` is a symlink, information for the symlink will be returned. + * + * const fileInfo = Deno.lstatSync("hello.txt"); + * assert(fileInfo.isFile()); + * + * Requires `allow-read` permission. */ +export function lstatSync(path: string): FileInfo { + const res = sendSync("op_stat", { + path, + lstat: true + }) as StatResponse; + return new FileInfoImpl(res); +} + +/** Resolves to a `Deno.FileInfo` for the specified `path`. Will always + * follow symlinks. + * + * const fileInfo = await Deno.stat("hello.txt"); + * assert(fileInfo.isFile()); + * + * Requires `allow-read` permission. */ +export async function stat(path: string): Promise { + const res = (await sendAsync("op_stat", { + path, + lstat: false + })) as StatResponse; + return new FileInfoImpl(res); +} + +/** Synchronously returns a `Deno.FileInfo` for the specified `path`. Will + * always follow symlinks. + * + * const fileInfo = Deno.statSync("hello.txt"); + * assert(fileInfo.isFile()); + * + * Requires `allow-read` permission. */ +export function statSync(path: string): FileInfo { + const res = sendSync("op_stat", { + path, + lstat: false + }) as StatResponse; + return new FileInfoImpl(res); +} diff --git a/cli/js/ops/fs/symlink.ts b/cli/js/ops/fs/symlink.ts new file mode 100644 index 000000000..3bb7c3335 --- /dev/null +++ b/cli/js/ops/fs/symlink.ts @@ -0,0 +1,44 @@ +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. +import { sendSync, sendAsync } from "../dispatch_json.ts"; +import * as util from "../../util.ts"; +import { build } from "../../build.ts"; + +/** **UNSTABLE**: `type` argument type may be changed to `"dir" | "file"`. + * + * Creates `newname` as a symbolic link to `oldname`. The type argument can be + * set to `dir` or `file`. Is only available on Windows and ignored on other + * platforms. + * + * Deno.symlinkSync("old/name", "new/name"); + * + * Requires `allow-read` and `allow-write` permissions. */ +export function symlinkSync( + oldname: string, + newname: string, + type?: string +): void { + if (build.os === "win" && type) { + return util.notImplemented(); + } + sendSync("op_symlink", { oldname, newname }); +} + +/** **UNSTABLE**: `type` argument may be changed to "dir" | "file" + * + * Creates `newname` as a symbolic link to `oldname`. The type argument can be + * set to `dir` or `file`. Is only available on Windows and ignored on other + * platforms. + * + * await Deno.symlink("old/name", "new/name"); + * + * Requires `allow-read` and `allow-write` permissions. */ +export async function symlink( + oldname: string, + newname: string, + type?: string +): Promise { + if (build.os === "win" && type) { + return util.notImplemented(); + } + await sendAsync("op_symlink", { oldname, newname }); +} diff --git a/cli/js/ops/fs/truncate.ts b/cli/js/ops/fs/truncate.ts new file mode 100644 index 000000000..578e37aa3 --- /dev/null +++ b/cli/js/ops/fs/truncate.ts @@ -0,0 +1,33 @@ +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. +import { sendSync, sendAsync } from "../dispatch_json.ts"; + +function coerceLen(len?: number): number { + if (!len) { + return 0; + } + + if (len < 0) { + return 0; + } + + return len; +} + +/** Synchronously truncates or extends the specified file, to reach the + * specified `len`. + * + * Deno.truncateSync("hello.txt", 10); + * + * Requires `allow-write` permission. */ +export function truncateSync(path: string, len?: number): void { + sendSync("op_truncate", { path, len: coerceLen(len) }); +} + +/** Truncates or extends the specified file, to reach the specified `len`. + * + * await Deno.truncate("hello.txt", 10); + * + * Requires `allow-write` permission. */ +export async function truncate(path: string, len?: number): Promise { + await sendAsync("op_truncate", { path, len: coerceLen(len) }); +} diff --git a/cli/js/ops/fs/utime.ts b/cli/js/ops/fs/utime.ts new file mode 100644 index 000000000..f684ac80a --- /dev/null +++ b/cli/js/ops/fs/utime.ts @@ -0,0 +1,50 @@ +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. +import { sendSync, sendAsync } from "../dispatch_json.ts"; + +function toSecondsFromEpoch(v: number | Date): number { + return v instanceof Date ? v.valueOf() / 1000 : v; +} + +/** **UNSTABLE**: needs investigation into high precision time. + * + * Synchronously changes the access and modification times of a file system + * object referenced by `path`. Given times are either in seconds (UNIX epoch + * time) or as `Date` objects. + * + * Deno.utimeSync("myfile.txt", 1556495550, new Date()); + * + * Requires `allow-write` permission. */ +export function utimeSync( + path: string, + atime: number | Date, + mtime: number | Date +): void { + sendSync("op_utime", { + path, + // TODO(ry) split atime, mtime into [seconds, nanoseconds] tuple + atime: toSecondsFromEpoch(atime), + mtime: toSecondsFromEpoch(mtime) + }); +} + +/** **UNSTABLE**: needs investigation into high precision time. + * + * Changes the access and modification times of a file system object + * referenced by `path`. Given times are either in seconds (UNIX epoch time) + * or as `Date` objects. + * + * await Deno.utime("myfile.txt", 1556495550, new Date()); + * + * Requires `allow-write` permission. */ +export async function utime( + path: string, + atime: number | Date, + mtime: number | Date +): Promise { + await sendAsync("op_utime", { + path, + // TODO(ry) split atime, mtime into [seconds, nanoseconds] tuple + atime: toSecondsFromEpoch(atime), + mtime: toSecondsFromEpoch(mtime) + }); +} diff --git a/cli/js/ops/net.ts b/cli/js/ops/net.ts new file mode 100644 index 000000000..a026189d2 --- /dev/null +++ b/cli/js/ops/net.ts @@ -0,0 +1,117 @@ +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. +import { sendSync, sendAsync } from "./dispatch_json.ts"; + +export type Transport = "tcp" | "udp"; +// TODO support other types: +// export type Transport = "tcp" | "tcp4" | "tcp6" | "unix" | "unixpacket"; + +export enum ShutdownMode { + // See http://man7.org/linux/man-pages/man2/shutdown.2.html + // Corresponding to SHUT_RD, SHUT_WR, SHUT_RDWR + Read = 0, + Write, + ReadWrite // unused +} + +/** Shut down socket send and receive operations. + * + * Matches behavior of POSIX shutdown(3). + * + * const listener = Deno.listen({ port: 80 }); + * const conn = await listener.accept(); + * Deno.shutdown(conn.rid, Deno.ShutdownMode.Write); + */ +export function shutdown(rid: number, how: ShutdownMode): void { + sendSync("op_shutdown", { rid, how }); +} + +interface AcceptResponse { + rid: number; + localAddr: { + hostname: string; + port: number; + transport: Transport; + }; + remoteAddr: { + hostname: string; + port: number; + transport: Transport; + }; +} + +export async function accept(rid: number): Promise { + return await sendAsync("op_accept", { rid }); +} + +export interface ListenRequest { + transport: Transport; + hostname: string; + port: number; +} + +interface ListenResponse { + rid: number; + localAddr: { + hostname: string; + port: number; + transport: Transport; + }; +} + +export function listen(args: ListenRequest): ListenResponse { + return sendSync("op_listen", args); +} + +interface ConnectResponse { + rid: number; + localAddr: { + hostname: string; + port: number; + transport: Transport; + }; + remoteAddr: { + hostname: string; + port: number; + transport: Transport; + }; +} + +export interface ConnectRequest { + transport: Transport; + hostname: string; + port: number; +} + +export async function connect(args: ConnectRequest): Promise { + return await sendAsync("op_connect", args); +} + +interface ReceiveResponse { + size: number; + remoteAddr: { + hostname: string; + port: number; + transport: Transport; + }; +} + +export async function receive( + rid: number, + zeroCopy: Uint8Array +): Promise { + return await sendAsync("op_receive", { rid }, zeroCopy); +} + +export interface SendRequest { + rid: number; + hostname: string; + port: number; + transport: Transport; +} + +export async function send( + args: SendRequest, + zeroCopy: Uint8Array +): Promise { + await sendAsync("op_send", args, zeroCopy); +} diff --git a/cli/js/ops/timers.ts b/cli/js/ops/timers.ts index bc7fe6453..1a7081df0 100644 --- a/cli/js/ops/timers.ts +++ b/cli/js/ops/timers.ts @@ -8,3 +8,12 @@ export function stopGlobalTimer(): void { export async function startGlobalTimer(timeout: number): Promise { await sendAsync("op_global_timer", { timeout }); } + +interface NowResponse { + seconds: number; + subsecNanos: number; +} + +export function now(): NowResponse { + return sendSync("op_now"); +} diff --git a/cli/js/ops/tls.ts b/cli/js/ops/tls.ts new file mode 100644 index 000000000..3a9d70385 --- /dev/null +++ b/cli/js/ops/tls.ts @@ -0,0 +1,69 @@ +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. +import { sendAsync, sendSync } from "./dispatch_json.ts"; +import { Transport } from "./net.ts"; + +export interface ConnectTLSRequest { + transport: Transport; + hostname: string; + port: number; + cert_file?: string; +} + +interface ConnectTLSResponse { + rid: number; + localAddr: { + hostname: string; + port: number; + transport: Transport; + }; + remoteAddr: { + hostname: string; + port: number; + transport: Transport; + }; +} + +export async function connectTLS( + args: ConnectTLSRequest +): Promise { + return await sendAsync("op_connect_tls", args); +} + +interface AcceptTLSResponse { + rid: number; + localAddr: { + hostname: string; + port: number; + transport: Transport; + }; + remoteAddr: { + hostname: string; + port: number; + transport: Transport; + }; +} + +export async function acceptTLS(rid: number): Promise { + return await sendAsync("op_accept_tls", { rid }); +} + +export interface ListenTLSRequest { + port: number; + hostname: string; + transport: Transport; + certFile: string; + keyFile: string; +} + +interface ListenTLSResponse { + rid: number; + localAddr: { + hostname: string; + port: number; + transport: Transport; + }; +} + +export function listenTLS(args: ListenTLSRequest): ListenTLSResponse { + return sendSync("op_listen_tls", args); +} diff --git a/cli/js/performance.ts b/cli/js/performance.ts index 9d49e6a4c..7aaa35952 100644 --- a/cli/js/performance.ts +++ b/cli/js/performance.ts @@ -1,10 +1,5 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -import { sendSync } from "./ops/dispatch_json.ts"; - -interface NowResponse { - seconds: number; - subsecNanos: number; -} +import { now as opNow } from "./ops/timers.ts"; export class Performance { /** Returns a current time from Deno's start in milliseconds. @@ -15,7 +10,7 @@ export class Performance { * console.log(`${t} ms since start!`); */ now(): number { - const res = sendSync("op_now") as NowResponse; + const res = opNow(); return res.seconds * 1e3 + res.subsecNanos / 1e6; } } diff --git a/cli/js/read_dir.ts b/cli/js/read_dir.ts deleted file mode 100644 index 975f5d8b4..000000000 --- a/cli/js/read_dir.ts +++ /dev/null @@ -1,37 +0,0 @@ -// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -import { sendSync, sendAsync } from "./ops/dispatch_json.ts"; -import { FileInfo, FileInfoImpl } from "./file_info.ts"; -import { StatResponse } from "./stat.ts"; - -interface ReadDirResponse { - entries: StatResponse[]; -} - -function res(response: ReadDirResponse): FileInfo[] { - return response.entries.map( - (statRes: StatResponse): FileInfo => { - return new FileInfoImpl(statRes); - } - ); -} - -/** Synchronously reads the directory given by `path` and returns an array of - * `Deno.FileInfo`. - * - * const files = Deno.readdirSync("/"); - * - * Requires `allow-read` permission. */ -export function readdirSync(path: string): FileInfo[] { - return res(sendSync("op_read_dir", { path })); -} - -/** UNSTABLE: Maybe need to return an `AsyncIterable`. - * - * Reads the directory given by `path` and resolves to an array of `Deno.FileInfo`. - * - * const files = await Deno.readdir("/"); - * - * Requires `allow-read` permission. */ -export async function readdir(path: string): Promise { - return res(await sendAsync("op_read_dir", { path })); -} diff --git a/cli/js/read_link.ts b/cli/js/read_link.ts deleted file mode 100644 index 3ff56b990..000000000 --- a/cli/js/read_link.ts +++ /dev/null @@ -1,20 +0,0 @@ -// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -import { sendSync, sendAsync } from "./ops/dispatch_json.ts"; - -/** Returns the destination of the named symbolic link. - * - * const targetPath = Deno.readlinkSync("symlink/path"); - * - * Requires `allow-read` permission. */ -export function readlinkSync(path: string): string { - return sendSync("op_read_link", { path }); -} - -/** Resolves to the destination of the named symbolic link. - * - * const targetPath = await Deno.readlink("symlink/path"); - * - * Requires `allow-read` permission. */ -export async function readlink(path: string): Promise { - return await sendAsync("op_read_link", { path }); -} diff --git a/cli/js/realpath.ts b/cli/js/realpath.ts deleted file mode 100644 index 5a0c1cbf3..000000000 --- a/cli/js/realpath.ts +++ /dev/null @@ -1,18 +0,0 @@ -// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -import { sendSync, sendAsync } from "./ops/dispatch_json.ts"; - -/** Returns absolute normalized path with symbolic links resolved synchronously. - * - * const realPath = Deno.realpathSync("./some/path"); - */ -export function realpathSync(path: string): string { - return sendSync("op_realpath", { path }); -} - -/** Returns absolute normalized path with symbolic links resolved. - * - * const realPath = await Deno.realpath("./some/path"); - */ -export async function realpath(path: string): Promise { - return await sendAsync("op_realpath", { path }); -} diff --git a/cli/js/remove.ts b/cli/js/remove.ts deleted file mode 100644 index 37603bd0c..000000000 --- a/cli/js/remove.ts +++ /dev/null @@ -1,33 +0,0 @@ -// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -import { sendSync, sendAsync } from "./ops/dispatch_json.ts"; - -export interface RemoveOptions { - /** Defaults to `false`. If set to `true`, path will be removed even if - * it's a non-empty directory. */ - recursive?: boolean; -} - -/** Synchronously removes the named file or directory. Throws error if - * permission denied, path not found, or path is a non-empty directory and - * the `recursive` option isn't set to `true`. - * - * Deno.removeSync("/path/to/dir/or/file", { recursive: false }); - * - * Requires `allow-write` permission. */ -export function removeSync(path: string, options: RemoveOptions = {}): void { - sendSync("op_remove", { path, recursive: !!options.recursive }); -} - -/** Removes the named file or directory. Throws error if permission denied, - * path not found, or path is a non-empty directory and the `recursive` - * option isn't set to `true`. - * - * await Deno.remove("/path/to/dir/or/file", { recursive: false }); - * - * Requires `allow-write` permission. */ -export async function remove( - path: string, - options: RemoveOptions = {} -): Promise { - await sendAsync("op_remove", { path, recursive: !!options.recursive }); -} diff --git a/cli/js/rename.ts b/cli/js/rename.ts deleted file mode 100644 index eaa236336..000000000 --- a/cli/js/rename.ts +++ /dev/null @@ -1,25 +0,0 @@ -// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -import { sendSync, sendAsync } from "./ops/dispatch_json.ts"; - -/** Synchronously renames (moves) `oldpath` to `newpath`. If `newpath` already - * exists and is not a directory, `renameSync()` replaces it. OS-specific - * restrictions may apply when `oldpath` and `newpath` are in different - * directories. - * - * Deno.renameSync("old/path", "new/path"); - * - * Requires `allow-read` and `allow-write` permissions. */ -export function renameSync(oldpath: string, newpath: string): void { - sendSync("op_rename", { oldpath, newpath }); -} - -/** Renames (moves) `oldpath` to `newpath`. If `newpath` already exists and is - * not a directory, `rename()` replaces it. OS-specific restrictions may apply - * when `oldpath` and `newpath` are in different directories. - * - * await Deno.rename("old/path", "new/path"); - * - * Requires `allow-read` and `allow-write`. */ -export async function rename(oldpath: string, newpath: string): Promise { - await sendAsync("op_rename", { oldpath, newpath }); -} diff --git a/cli/js/stat.ts b/cli/js/stat.ts deleted file mode 100644 index a00a62d0c..000000000 --- a/cli/js/stat.ts +++ /dev/null @@ -1,84 +0,0 @@ -// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -import { sendSync, sendAsync } from "./ops/dispatch_json.ts"; -import { FileInfo, FileInfoImpl } from "./file_info.ts"; - -/** @internal */ -export interface StatResponse { - isFile: boolean; - isSymlink: boolean; - len: number; - modified: number; - accessed: number; - created: number; - name: string | null; - // Unix only members - dev: number; - ino: number; - mode: number; - nlink: number; - uid: number; - gid: number; - rdev: number; - blksize: number; - blocks: number; -} - -/** Resolves to a `Deno.FileInfo` for the specified `path`. If `path` is a - * symlink, information for the symlink will be returned. - * - * const fileInfo = await Deno.lstat("hello.txt"); - * assert(fileInfo.isFile()); - * - * Requires `allow-read` permission. */ -export async function lstat(path: string): Promise { - const res = (await sendAsync("op_stat", { - path, - lstat: true - })) as StatResponse; - return new FileInfoImpl(res); -} - -/** Synchronously returns a `Deno.FileInfo` for the specified `path`. If - * `path` is a symlink, information for the symlink will be returned. - * - * const fileInfo = Deno.lstatSync("hello.txt"); - * assert(fileInfo.isFile()); - * - * Requires `allow-read` permission. */ -export function lstatSync(path: string): FileInfo { - const res = sendSync("op_stat", { - path, - lstat: true - }) as StatResponse; - return new FileInfoImpl(res); -} - -/** Resolves to a `Deno.FileInfo` for the specified `path`. Will always - * follow symlinks. - * - * const fileInfo = await Deno.stat("hello.txt"); - * assert(fileInfo.isFile()); - * - * Requires `allow-read` permission. */ -export async function stat(path: string): Promise { - const res = (await sendAsync("op_stat", { - path, - lstat: false - })) as StatResponse; - return new FileInfoImpl(res); -} - -/** Synchronously returns a `Deno.FileInfo` for the specified `path`. Will - * always follow symlinks. - * - * const fileInfo = Deno.statSync("hello.txt"); - * assert(fileInfo.isFile()); - * - * Requires `allow-read` permission. */ -export function statSync(path: string): FileInfo { - const res = sendSync("op_stat", { - path, - lstat: false - }) as StatResponse; - return new FileInfoImpl(res); -} diff --git a/cli/js/symlink.ts b/cli/js/symlink.ts deleted file mode 100644 index 9a63fca9f..000000000 --- a/cli/js/symlink.ts +++ /dev/null @@ -1,44 +0,0 @@ -// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -import { sendSync, sendAsync } from "./ops/dispatch_json.ts"; -import * as util from "./util.ts"; -import { build } from "./build.ts"; - -/** **UNSTABLE**: `type` argument type may be changed to `"dir" | "file"`. - * - * Creates `newname` as a symbolic link to `oldname`. The type argument can be - * set to `dir` or `file`. Is only available on Windows and ignored on other - * platforms. - * - * Deno.symlinkSync("old/name", "new/name"); - * - * Requires `allow-read` and `allow-write` permissions. */ -export function symlinkSync( - oldname: string, - newname: string, - type?: string -): void { - if (build.os === "win" && type) { - return util.notImplemented(); - } - sendSync("op_symlink", { oldname, newname }); -} - -/** **UNSTABLE**: `type` argument may be changed to "dir" | "file" - * - * Creates `newname` as a symbolic link to `oldname`. The type argument can be - * set to `dir` or `file`. Is only available on Windows and ignored on other - * platforms. - * - * await Deno.symlink("old/name", "new/name"); - * - * Requires `allow-read` and `allow-write` permissions. */ -export async function symlink( - oldname: string, - newname: string, - type?: string -): Promise { - if (build.os === "win" && type) { - return util.notImplemented(); - } - await sendAsync("op_symlink", { oldname, newname }); -} diff --git a/cli/js/tls.ts b/cli/js/tls.ts index 98a6586bb..8fff562d2 100644 --- a/cli/js/tls.ts +++ b/cli/js/tls.ts @@ -1,5 +1,5 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -import { sendAsync, sendSync } from "./ops/dispatch_json.ts"; +import * as tlsOps from "./ops/tls.ts"; import { Listener, Transport, Conn, ConnImpl, ListenerImpl } from "./net.ts"; // TODO(ry) There are many configuration options to add... @@ -17,13 +17,13 @@ const connectTLSDefaults = { hostname: "127.0.0.1", transport: "tcp" }; */ export async function connectTLS(options: ConnectTLSOptions): Promise { options = Object.assign(connectTLSDefaults, options); - const res = await sendAsync("op_connect_tls", options); + const res = await tlsOps.connectTLS(options as tlsOps.ConnectTLSRequest); return new ConnImpl(res.rid, res.remoteAddr!, res.localAddr!); } class TLSListenerImpl extends ListenerImpl { async accept(): Promise { - const res = await sendAsync("op_accept_tls", { rid: this.rid }); + const res = await tlsOps.acceptTLS(this.rid); return new ConnImpl(res.rid, res.remoteAddr, res.localAddr); } } @@ -52,7 +52,7 @@ export interface ListenTLSOptions { export function listenTLS(options: ListenTLSOptions): Listener { const hostname = options.hostname || "0.0.0.0"; const transport = options.transport || "tcp"; - const res = sendSync("op_listen_tls", { + const res = tlsOps.listenTLS({ hostname, port: options.port, transport, diff --git a/cli/js/truncate.ts b/cli/js/truncate.ts deleted file mode 100644 index b43c88f60..000000000 --- a/cli/js/truncate.ts +++ /dev/null @@ -1,33 +0,0 @@ -// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -import { sendSync, sendAsync } from "./ops/dispatch_json.ts"; - -function coerceLen(len?: number): number { - if (!len) { - return 0; - } - - if (len < 0) { - return 0; - } - - return len; -} - -/** Synchronously truncates or extends the specified file, to reach the - * specified `len`. - * - * Deno.truncateSync("hello.txt", 10); - * - * Requires `allow-write` permission. */ -export function truncateSync(path: string, len?: number): void { - sendSync("op_truncate", { path, len: coerceLen(len) }); -} - -/** Truncates or extends the specified file, to reach the specified `len`. - * - * await Deno.truncate("hello.txt", 10); - * - * Requires `allow-write` permission. */ -export async function truncate(path: string, len?: number): Promise { - await sendAsync("op_truncate", { path, len: coerceLen(len) }); -} diff --git a/cli/js/utime.ts b/cli/js/utime.ts deleted file mode 100644 index 9224a3ffa..000000000 --- a/cli/js/utime.ts +++ /dev/null @@ -1,50 +0,0 @@ -// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -import { sendSync, sendAsync } from "./ops/dispatch_json.ts"; - -function toSecondsFromEpoch(v: number | Date): number { - return v instanceof Date ? v.valueOf() / 1000 : v; -} - -/** **UNSTABLE**: needs investigation into high precision time. - * - * Synchronously changes the access and modification times of a file system - * object referenced by `path`. Given times are either in seconds (UNIX epoch - * time) or as `Date` objects. - * - * Deno.utimeSync("myfile.txt", 1556495550, new Date()); - * - * Requires `allow-write` permission. */ -export function utimeSync( - path: string, - atime: number | Date, - mtime: number | Date -): void { - sendSync("op_utime", { - path, - // TODO(ry) split atime, mtime into [seconds, nanoseconds] tuple - atime: toSecondsFromEpoch(atime), - mtime: toSecondsFromEpoch(mtime) - }); -} - -/** **UNSTABLE**: needs investigation into high precision time. - * - * Changes the access and modification times of a file system object - * referenced by `path`. Given times are either in seconds (UNIX epoch time) - * or as `Date` objects. - * - * await Deno.utime("myfile.txt", 1556495550, new Date()); - * - * Requires `allow-write` permission. */ -export async function utime( - path: string, - atime: number | Date, - mtime: number | Date -): Promise { - await sendAsync("op_utime", { - path, - // TODO(ry) split atime, mtime into [seconds, nanoseconds] tuple - atime: toSecondsFromEpoch(atime), - mtime: toSecondsFromEpoch(mtime) - }); -} diff --git a/cli/js/write_file.ts b/cli/js/write_file.ts index d57274228..995335f57 100644 --- a/cli/js/write_file.ts +++ b/cli/js/write_file.ts @@ -1,7 +1,7 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -import { stat, statSync } from "./stat.ts"; +import { stat, statSync } from "./ops/fs/stat.ts"; import { open, openSync } from "./files.ts"; -import { chmod, chmodSync } from "./chmod.ts"; +import { chmod, chmodSync } from "./ops/fs/chmod.ts"; import { writeAll, writeAllSync } from "./buffer.ts"; /** Options for writing to a file. */ -- cgit v1.2.3