diff options
author | Stanislav <62983943+stanislavstrelnikov@users.noreply.github.com> | 2020-07-07 04:45:39 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-07-06 21:45:39 -0400 |
commit | 158ae0bfe900d2bac3076390c4fe3d2b54d94fe5 (patch) | |
tree | 209e4b5682e2a899041767c49428e34329e48084 /cli/js/ops | |
parent | ab4c574f5202f607ceb6068f56b3cc8aed1bbbaf (diff) |
clean up code in cli/js (#6611)
Diffstat (limited to 'cli/js/ops')
38 files changed, 166 insertions, 142 deletions
diff --git a/cli/js/ops/dispatch_json.ts b/cli/js/ops/dispatch_json.ts index 6a91f6a4f..cf6f5c095 100644 --- a/cli/js/ops/dispatch_json.ts +++ b/cli/js/ops/dispatch_json.ts @@ -1,4 +1,5 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + import * as util from "../util.ts"; import { core } from "../core.ts"; import { ErrorKind, getErrorClass } from "../errors.ts"; @@ -18,9 +19,10 @@ interface JsonResponse { } // Using an object without a prototype because `Map` was causing GC problems. -const promiseTable: { - [key: number]: util.Resolvable<JsonResponse>; -} = Object.create(null); +const promiseTable: Record< + number, + util.Resolvable<JsonResponse> +> = Object.create(null); let _nextPromiseId = 1; function nextPromiseId(): number { @@ -28,13 +30,11 @@ function nextPromiseId(): number { } function decode(ui8: Uint8Array): JsonResponse { - const s = core.decode(ui8); - return JSON.parse(s) as JsonResponse; + return JSON.parse(core.decode(ui8)); } function encode(args: object): Uint8Array { - const s = JSON.stringify(args); - return core.encode(s); + return core.encode(JSON.stringify(args)); } function unwrapResponse(res: JsonResponse): Ok { @@ -80,7 +80,7 @@ export async function sendAsync( const promise = util.createResolvable<Ok>(); const argsUi8 = encode(args); const buf = core.dispatchByName(opName, argsUi8, ...zeroCopy); - if (buf) { + if (buf != null) { // Sync result. const res = decode(buf); promise.resolve(res); diff --git a/cli/js/ops/dispatch_minimal.ts b/cli/js/ops/dispatch_minimal.ts index 0d1dac44e..ca50b00e9 100644 --- a/cli/js/ops/dispatch_minimal.ts +++ b/cli/js/ops/dispatch_minimal.ts @@ -1,13 +1,15 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + import * as util from "../util.ts"; import { core } from "../core.ts"; import { TextDecoder } from "../web/text_encoding.ts"; import { ErrorKind, errors, getErrorClass } from "../errors.ts"; // Using an object without a prototype because `Map` was causing GC problems. -const promiseTableMin: { - [key: number]: util.Resolvable<RecordMinimal>; -} = Object.create(null); +const promiseTableMin: Record< + number, + util.Resolvable<RecordMinimal> +> = Object.create(null); // Note it's important that promiseId starts at 1 instead of 0, because sync // messages are indicated with promiseId 0. If we ever add wrap around logic for @@ -93,7 +95,7 @@ export async function sendAsyncMinimal( scratch32[2] = 0; // result const promise = util.createResolvable<RecordMinimal>(); const buf = core.dispatchByName(opName, scratchBytes, zeroCopy); - if (buf) { + if (buf != null) { const record = recordFromBufMinimal(buf); // Sync result. promise.resolve(record); diff --git a/cli/js/ops/errors.ts b/cli/js/ops/errors.ts index bd3e6d809..c6c26b8ba 100644 --- a/cli/js/ops/errors.ts +++ b/cli/js/ops/errors.ts @@ -1,4 +1,5 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + import { DiagnosticItem } from "../diagnostics.ts"; import { sendSync } from "./dispatch_json.ts"; @@ -13,12 +14,7 @@ export interface Location { } export function applySourceMap(location: Location): Location { - const { fileName, lineNumber, columnNumber } = location; - const res = sendSync("op_apply_source_map", { - fileName, - lineNumber: lineNumber, - columnNumber: columnNumber, - }); + const res = sendSync("op_apply_source_map", location); return { fileName: res.fileName, lineNumber: res.lineNumber, diff --git a/cli/js/ops/fetch.ts b/cli/js/ops/fetch.ts index 290376c86..2f881cc02 100644 --- a/cli/js/ops/fetch.ts +++ b/cli/js/ops/fetch.ts @@ -17,10 +17,10 @@ export interface FetchResponse { export function fetch( args: FetchRequest, - body: ArrayBufferView | undefined + body?: ArrayBufferView ): Promise<FetchResponse> { - let zeroCopy = undefined; - if (body) { + let zeroCopy; + if (body != null) { zeroCopy = new Uint8Array(body.buffer, body.byteOffset, body.byteLength); } diff --git a/cli/js/ops/fs/chmod.ts b/cli/js/ops/fs/chmod.ts index 76a3c8f49..a2236935b 100644 --- a/cli/js/ops/fs/chmod.ts +++ b/cli/js/ops/fs/chmod.ts @@ -1,13 +1,12 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + import { sendSync, sendAsync } from "../dispatch_json.ts"; import { pathFromURL } from "../../util.ts"; export function chmodSync(path: string | URL, mode: number): void { - path = pathFromURL(path); - sendSync("op_chmod", { path, mode }); + sendSync("op_chmod", { path: pathFromURL(path), mode }); } export async function chmod(path: string | URL, mode: number): Promise<void> { - path = pathFromURL(path); - await sendAsync("op_chmod", { path, mode }); + await sendAsync("op_chmod", { path: pathFromURL(path), mode }); } diff --git a/cli/js/ops/fs/chown.ts b/cli/js/ops/fs/chown.ts index 3afe07f16..52735e097 100644 --- a/cli/js/ops/fs/chown.ts +++ b/cli/js/ops/fs/chown.ts @@ -1,4 +1,5 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + import { sendSync, sendAsync } from "../dispatch_json.ts"; import { pathFromURL } from "../../util.ts"; @@ -7,8 +8,7 @@ export function chownSync( uid: number | null, gid: number | null ): void { - path = pathFromURL(path); - sendSync("op_chown", { path, uid, gid }); + sendSync("op_chown", { path: pathFromURL(path), uid, gid }); } export async function chown( @@ -16,6 +16,5 @@ export async function chown( uid: number | null, gid: number | null ): Promise<void> { - path = pathFromURL(path); - await sendAsync("op_chown", { path, uid, gid }); + await sendAsync("op_chown", { path: pathFromURL(path), uid, gid }); } diff --git a/cli/js/ops/fs/copy_file.ts b/cli/js/ops/fs/copy_file.ts index 6bbb3f599..fcb147bdd 100644 --- a/cli/js/ops/fs/copy_file.ts +++ b/cli/js/ops/fs/copy_file.ts @@ -1,4 +1,5 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + import { sendSync, sendAsync } from "../dispatch_json.ts"; import { pathFromURL } from "../../util.ts"; @@ -6,18 +7,18 @@ export function copyFileSync( fromPath: string | URL, toPath: string | URL ): void { - fromPath = pathFromURL(fromPath); - toPath = pathFromURL(toPath); - - sendSync("op_copy_file", { from: fromPath, to: toPath }); + sendSync("op_copy_file", { + from: pathFromURL(fromPath), + to: pathFromURL(toPath), + }); } export async function copyFile( fromPath: string | URL, toPath: string | URL ): Promise<void> { - fromPath = pathFromURL(fromPath); - toPath = pathFromURL(toPath); - - await sendAsync("op_copy_file", { from: fromPath, to: toPath }); + await sendAsync("op_copy_file", { + from: pathFromURL(fromPath), + to: pathFromURL(toPath), + }); } diff --git a/cli/js/ops/fs/dir.ts b/cli/js/ops/fs/dir.ts index 14b6240ed..dbf468c62 100644 --- a/cli/js/ops/fs/dir.ts +++ b/cli/js/ops/fs/dir.ts @@ -1,4 +1,5 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + import { sendSync } from "../dispatch_json.ts"; export function cwd(): string { diff --git a/cli/js/ops/fs/link.ts b/cli/js/ops/fs/link.ts index 92fb58834..05ff358ef 100644 --- a/cli/js/ops/fs/link.ts +++ b/cli/js/ops/fs/link.ts @@ -1,4 +1,5 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + import { sendSync, sendAsync } from "../dispatch_json.ts"; export function linkSync(oldpath: string, newpath: string): void { diff --git a/cli/js/ops/fs/make_temp.ts b/cli/js/ops/fs/make_temp.ts index 85dea5f20..3996744d1 100644 --- a/cli/js/ops/fs/make_temp.ts +++ b/cli/js/ops/fs/make_temp.ts @@ -1,4 +1,5 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + import { sendSync, sendAsync } from "../dispatch_json.ts"; export interface MakeTempOptions { diff --git a/cli/js/ops/fs/mkdir.ts b/cli/js/ops/fs/mkdir.ts index 374a00d83..61ea1c218 100644 --- a/cli/js/ops/fs/mkdir.ts +++ b/cli/js/ops/fs/mkdir.ts @@ -1,11 +1,21 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + import { sendSync, sendAsync } from "../dispatch_json.ts"; -type MkdirArgs = { path: string; recursive: boolean; mode?: number }; +export interface MkdirOptions { + recursive?: boolean; + mode?: number; +} + +interface MkdirArgs { + path: string; + recursive: boolean; + mode?: number; +} function mkdirArgs(path: string, options?: MkdirOptions): MkdirArgs { const args: MkdirArgs = { path, recursive: false }; - if (options) { + if (options != null) { if (typeof options.recursive == "boolean") { args.recursive = options.recursive; } @@ -16,11 +26,6 @@ function mkdirArgs(path: string, options?: MkdirOptions): MkdirArgs { return args; } -export interface MkdirOptions { - recursive?: boolean; - mode?: number; -} - export function mkdirSync(path: string, options?: MkdirOptions): void { sendSync("op_mkdir", mkdirArgs(path, options)); } diff --git a/cli/js/ops/fs/open.ts b/cli/js/ops/fs/open.ts index 3742d0b52..edd52c376 100644 --- a/cli/js/ops/fs/open.ts +++ b/cli/js/ops/fs/open.ts @@ -1,4 +1,5 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + import { sendSync, sendAsync } from "../dispatch_json.ts"; import { pathFromURL } from "../../util.ts"; @@ -18,8 +19,7 @@ export interface OpenOptions { export function openSync(path: string | URL, options: OpenOptions): number { const mode: number | undefined = options?.mode; - path = pathFromURL(path); - return sendSync("op_open", { path, options, mode }); + return sendSync("op_open", { path: pathFromURL(path), options, mode }); } export function open( @@ -27,10 +27,5 @@ export function open( options: OpenOptions ): Promise<number> { const mode: number | undefined = options?.mode; - path = pathFromURL(path); - return sendAsync("op_open", { - path, - options, - mode, - }); + return sendAsync("op_open", { path: pathFromURL(path), options, mode }); } diff --git a/cli/js/ops/fs/read_dir.ts b/cli/js/ops/fs/read_dir.ts index 09c5d1c12..6ffe6116e 100644 --- a/cli/js/ops/fs/read_dir.ts +++ b/cli/js/ops/fs/read_dir.ts @@ -1,4 +1,5 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + import { sendSync, sendAsync } from "../dispatch_json.ts"; import { pathFromURL } from "../../util.ts"; @@ -18,13 +19,13 @@ function res(response: ReadDirResponse): DirEntry[] { } export function readDirSync(path: string | URL): Iterable<DirEntry> { - path = pathFromURL(path); - return res(sendSync("op_read_dir", { path }))[Symbol.iterator](); + return res(sendSync("op_read_dir", { path: pathFromURL(path) }))[ + Symbol.iterator + ](); } export function readDir(path: string | URL): AsyncIterable<DirEntry> { - path = pathFromURL(path); - const array = sendAsync("op_read_dir", { path }).then(res); + const array = sendAsync("op_read_dir", { path: pathFromURL(path) }).then(res); return { async *[Symbol.asyncIterator](): AsyncIterableIterator<DirEntry> { yield* await array; diff --git a/cli/js/ops/fs/read_link.ts b/cli/js/ops/fs/read_link.ts index bcd27ccb7..33fef7e36 100644 --- a/cli/js/ops/fs/read_link.ts +++ b/cli/js/ops/fs/read_link.ts @@ -1,4 +1,5 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + import { sendSync, sendAsync } from "../dispatch_json.ts"; export function readLinkSync(path: string): string { diff --git a/cli/js/ops/fs/real_path.ts b/cli/js/ops/fs/real_path.ts index ab95b9583..c424d99bc 100644 --- a/cli/js/ops/fs/real_path.ts +++ b/cli/js/ops/fs/real_path.ts @@ -1,4 +1,5 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + import { sendSync, sendAsync } from "../dispatch_json.ts"; export function realPathSync(path: string): string { diff --git a/cli/js/ops/fs/remove.ts b/cli/js/ops/fs/remove.ts index d1a8702f1..52f4cad40 100644 --- a/cli/js/ops/fs/remove.ts +++ b/cli/js/ops/fs/remove.ts @@ -1,4 +1,5 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + import { sendSync, sendAsync } from "../dispatch_json.ts"; import { pathFromURL } from "../../util.ts"; @@ -10,14 +11,18 @@ export function removeSync( path: string | URL, options: RemoveOptions = {} ): void { - path = pathFromURL(path); - sendSync("op_remove", { path, recursive: !!options.recursive }); + sendSync("op_remove", { + path: pathFromURL(path), + recursive: !!options.recursive, + }); } export async function remove( path: string | URL, options: RemoveOptions = {} ): Promise<void> { - path = pathFromURL(path); - await sendAsync("op_remove", { path, recursive: !!options.recursive }); + await sendAsync("op_remove", { + path: pathFromURL(path), + recursive: !!options.recursive, + }); } diff --git a/cli/js/ops/fs/rename.ts b/cli/js/ops/fs/rename.ts index 9f02c8bc0..f0789d3eb 100644 --- a/cli/js/ops/fs/rename.ts +++ b/cli/js/ops/fs/rename.ts @@ -1,4 +1,5 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + import { sendSync, sendAsync } from "../dispatch_json.ts"; export function renameSync(oldpath: string, newpath: string): void { diff --git a/cli/js/ops/fs/seek.ts b/cli/js/ops/fs/seek.ts index c7e4c9172..2e23e084b 100644 --- a/cli/js/ops/fs/seek.ts +++ b/cli/js/ops/fs/seek.ts @@ -1,4 +1,5 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + import { sendSync, sendAsync } from "../dispatch_json.ts"; import { SeekMode } from "../../io.ts"; diff --git a/cli/js/ops/fs/stat.ts b/cli/js/ops/fs/stat.ts index 402adeafc..f444190fd 100644 --- a/cli/js/ops/fs/stat.ts +++ b/cli/js/ops/fs/stat.ts @@ -1,4 +1,5 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + import { sendSync, sendAsync } from "../dispatch_json.ts"; import { build } from "../../build.ts"; import { pathFromURL } from "../../util.ts"; @@ -44,7 +45,7 @@ export interface StatResponse { // @internal export function parseFileInfo(response: StatResponse): FileInfo { - const isUnix = build.os === "darwin" || build.os === "linux"; + const unix = build.os === "darwin" || build.os === "linux"; return { isFile: response.isFile, isDirectory: response.isDirectory, @@ -54,15 +55,15 @@ export function parseFileInfo(response: StatResponse): FileInfo { atime: response.atime != null ? new Date(response.atime) : null, birthtime: response.birthtime != null ? new Date(response.birthtime) : null, // Only non-null if on Unix - dev: isUnix ? response.dev : null, - ino: isUnix ? response.ino : null, - mode: isUnix ? response.mode : null, - nlink: isUnix ? response.nlink : null, - uid: isUnix ? response.uid : null, - gid: isUnix ? response.gid : null, - rdev: isUnix ? response.rdev : null, - blksize: isUnix ? response.blksize : null, - blocks: isUnix ? response.blocks : null, + dev: unix ? response.dev : null, + ino: unix ? response.ino : null, + mode: unix ? response.mode : null, + nlink: unix ? response.nlink : null, + uid: unix ? response.uid : null, + gid: unix ? response.gid : null, + rdev: unix ? response.rdev : null, + blksize: unix ? response.blksize : null, + blocks: unix ? response.blocks : null, }; } @@ -75,37 +76,33 @@ export async function fstat(rid: number): Promise<FileInfo> { } export async function lstat(path: string | URL): Promise<FileInfo> { - path = pathFromURL(path); - const res = (await sendAsync("op_stat", { - path, + const res = await sendAsync("op_stat", { + path: pathFromURL(path), lstat: true, - })) as StatResponse; + }); return parseFileInfo(res); } export function lstatSync(path: string | URL): FileInfo { - path = pathFromURL(path); const res = sendSync("op_stat", { - path, + path: pathFromURL(path), lstat: true, - }) as StatResponse; + }); return parseFileInfo(res); } export async function stat(path: string | URL): Promise<FileInfo> { - path = pathFromURL(path); - const res = (await sendAsync("op_stat", { - path, + const res = await sendAsync("op_stat", { + path: pathFromURL(path), lstat: false, - })) as StatResponse; + }); return parseFileInfo(res); } export function statSync(path: string | URL): FileInfo { - path = pathFromURL(path); const res = sendSync("op_stat", { - path, + path: pathFromURL(path), lstat: false, - }) as StatResponse; + }); return parseFileInfo(res); } diff --git a/cli/js/ops/fs/symlink.ts b/cli/js/ops/fs/symlink.ts index fde611b55..7d4741928 100644 --- a/cli/js/ops/fs/symlink.ts +++ b/cli/js/ops/fs/symlink.ts @@ -1,14 +1,15 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + import { sendSync, sendAsync } from "../dispatch_json.ts"; -export type symlinkOptions = { +export interface SymlinkOptions { type: "file" | "dir"; -}; +} export function symlinkSync( oldpath: string, newpath: string, - options?: symlinkOptions + options?: SymlinkOptions ): void { sendSync("op_symlink", { oldpath, newpath, options }); } @@ -16,7 +17,7 @@ export function symlinkSync( export async function symlink( oldpath: string, newpath: string, - options?: symlinkOptions + options?: SymlinkOptions ): Promise<void> { await sendAsync("op_symlink", { oldpath, newpath, options }); } diff --git a/cli/js/ops/fs/sync.ts b/cli/js/ops/fs/sync.ts index 567aab55b..7f208b8bd 100644 --- a/cli/js/ops/fs/sync.ts +++ b/cli/js/ops/fs/sync.ts @@ -1,4 +1,5 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + import { sendSync, sendAsync } from "../dispatch_json.ts"; export function fdatasyncSync(rid: number): void { diff --git a/cli/js/ops/fs/truncate.ts b/cli/js/ops/fs/truncate.ts index 2b805e5ac..d18e5d9d9 100644 --- a/cli/js/ops/fs/truncate.ts +++ b/cli/js/ops/fs/truncate.ts @@ -1,12 +1,9 @@ // 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) { + if (len == null || len < 0) { return 0; } diff --git a/cli/js/ops/fs/umask.ts b/cli/js/ops/fs/umask.ts index 38bf8ff6c..fbc94091e 100644 --- a/cli/js/ops/fs/umask.ts +++ b/cli/js/ops/fs/umask.ts @@ -1,4 +1,5 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + import { sendSync } from "../dispatch_json.ts"; export function umask(mask?: number): number { diff --git a/cli/js/ops/fs/utime.ts b/cli/js/ops/fs/utime.ts index 2755a7e96..fa86038c6 100644 --- a/cli/js/ops/fs/utime.ts +++ b/cli/js/ops/fs/utime.ts @@ -1,4 +1,5 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + import { sendSync, sendAsync } from "../dispatch_json.ts"; function toSecondsFromEpoch(v: number | Date): number { diff --git a/cli/js/ops/fs_events.ts b/cli/js/ops/fs_events.ts index 9d72cb898..fb78c6196 100644 --- a/cli/js/ops/fs_events.ts +++ b/cli/js/ops/fs_events.ts @@ -1,4 +1,5 @@ -// Copyright 2019 the Deno authors. All rights reserved. MIT license. +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + import { sendSync, sendAsync } from "./dispatch_json.ts"; import { close } from "./resources.ts"; @@ -7,10 +8,14 @@ export interface FsEvent { paths: string[]; } +interface FsWatcherOptions { + recursive: boolean; +} + class FsWatcher implements AsyncIterableIterator<FsEvent> { readonly rid: number; - constructor(paths: string[], options: { recursive: boolean }) { + constructor(paths: string[], options: FsWatcherOptions) { const { recursive } = options; this.rid = sendSync("op_fs_events_open", { recursive, paths }); } @@ -33,7 +38,7 @@ class FsWatcher implements AsyncIterableIterator<FsEvent> { export function watchFs( paths: string | string[], - options = { recursive: true } + options: FsWatcherOptions = { recursive: true } ): AsyncIterableIterator<FsEvent> { return new FsWatcher(Array.isArray(paths) ? paths : [paths], options); } diff --git a/cli/js/ops/get_random_values.ts b/cli/js/ops/get_random_values.ts index 1dc56b698..95e4602e6 100644 --- a/cli/js/ops/get_random_values.ts +++ b/cli/js/ops/get_random_values.ts @@ -1,4 +1,5 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + import { sendSync } from "./dispatch_json.ts"; import { assert } from "../util.ts"; diff --git a/cli/js/ops/io.ts b/cli/js/ops/io.ts index cc3d1d170..ecd1269d5 100644 --- a/cli/js/ops/io.ts +++ b/cli/js/ops/io.ts @@ -3,50 +3,48 @@ import { sendAsyncMinimal, sendSyncMinimal } from "./dispatch_minimal.ts"; export function readSync(rid: number, buffer: Uint8Array): number | null { - if (buffer.length == 0) { + if (buffer.length === 0) { return 0; } + const nread = sendSyncMinimal("op_read", rid, buffer); if (nread < 0) { throw new Error("read error"); - } else if (nread == 0) { - return null; - } else { - return nread; } + + return nread === 0 ? null : nread; } export async function read( rid: number, buffer: Uint8Array ): Promise<number | null> { - if (buffer.length == 0) { + if (buffer.length === 0) { return 0; } + const nread = await sendAsyncMinimal("op_read", rid, buffer); if (nread < 0) { throw new Error("read error"); - } else if (nread == 0) { - return null; - } else { - return nread; } + + return nread === 0 ? null : nread; } export function writeSync(rid: number, data: Uint8Array): number { const result = sendSyncMinimal("op_write", rid, data); if (result < 0) { throw new Error("write error"); - } else { - return result; } + + return result; } export async function write(rid: number, data: Uint8Array): Promise<number> { const result = await sendAsyncMinimal("op_write", rid, data); if (result < 0) { throw new Error("write error"); - } else { - return result; } + + return result; } diff --git a/cli/js/ops/net.ts b/cli/js/ops/net.ts index 98ee71ff8..05b1bc2cd 100644 --- a/cli/js/ops/net.ts +++ b/cli/js/ops/net.ts @@ -1,4 +1,5 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + import { sendSync, sendAsync } from "./dispatch_json.ts"; export interface NetAddr { @@ -18,7 +19,7 @@ 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, + Write = 1, ReadWrite, // unused } @@ -80,10 +81,6 @@ export type SendRequest = { rid: number; } & Addr; -export async function send( - args: SendRequest, - zeroCopy: Uint8Array -): Promise<number> { - const byteLength = await sendAsync("op_datagram_send", args, zeroCopy); - return byteLength; +export function send(args: SendRequest, zeroCopy: Uint8Array): Promise<number> { + return sendAsync("op_datagram_send", args, zeroCopy); } diff --git a/cli/js/ops/os.ts b/cli/js/ops/os.ts index 696c9f10b..50234ee4b 100644 --- a/cli/js/ops/os.ts +++ b/cli/js/ops/os.ts @@ -1,4 +1,5 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + import { sendSync } from "./dispatch_json.ts"; export function loadavg(): number[] { @@ -32,7 +33,7 @@ function deleteEnv(key: string): void { export const env = { get: getEnv, - toObject(): { [key: string]: string } { + toObject(): Record<string, string> { return sendSync("op_env"); }, set: setEnv, diff --git a/cli/js/ops/permissions.ts b/cli/js/ops/permissions.ts index 783b1b297..dfe8c4834 100644 --- a/cli/js/ops/permissions.ts +++ b/cli/js/ops/permissions.ts @@ -1,9 +1,7 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -import { sendSync } from "./dispatch_json.ts"; -// TODO(bartlomieju): duplicated in `cli/js/permissions.ts` as -// `PermissionState -export type PermissionResponse = "granted" | "denied" | "prompt"; +import { sendSync } from "./dispatch_json.ts"; +import { PermissionState } from "../permissions.ts"; interface PermissionRequest { name: string; @@ -11,14 +9,14 @@ interface PermissionRequest { path?: string; } -export function query(desc: PermissionRequest): PermissionResponse { +export function query(desc: PermissionRequest): PermissionState { return sendSync("op_query_permission", desc).state; } -export function revoke(desc: PermissionRequest): PermissionResponse { +export function revoke(desc: PermissionRequest): PermissionState { return sendSync("op_revoke_permission", desc).state; } -export function request(desc: PermissionRequest): PermissionResponse { +export function request(desc: PermissionRequest): PermissionState { return sendSync("op_request_permission", desc).state; } diff --git a/cli/js/ops/plugins.ts b/cli/js/ops/plugins.ts index e4593afbb..787fd799b 100644 --- a/cli/js/ops/plugins.ts +++ b/cli/js/ops/plugins.ts @@ -1,6 +1,7 @@ +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + import { sendSync } from "./dispatch_json.ts"; export function openPlugin(filename: string): number { - const rid = sendSync("op_open_plugin", { filename }); - return rid; + return sendSync("op_open_plugin", { filename }); } diff --git a/cli/js/ops/process.ts b/cli/js/ops/process.ts index 39c6eb8b7..86a0c9a71 100644 --- a/cli/js/ops/process.ts +++ b/cli/js/ops/process.ts @@ -1,4 +1,5 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + import { sendSync, sendAsync } from "./dispatch_json.ts"; import { assert } from "../util.ts"; diff --git a/cli/js/ops/resources.ts b/cli/js/ops/resources.ts index dacdaa659..ffcdb553e 100644 --- a/cli/js/ops/resources.ts +++ b/cli/js/ops/resources.ts @@ -1,9 +1,8 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + import { sendSync } from "./dispatch_json.ts"; -export interface ResourceMap { - [rid: number]: string; -} +export type ResourceMap = Record<number, string>; export function resources(): ResourceMap { const res = sendSync("op_resources") as Array<[number, string]>; diff --git a/cli/js/ops/signal.ts b/cli/js/ops/signal.ts index 09c178105..15093a3c4 100644 --- a/cli/js/ops/signal.ts +++ b/cli/js/ops/signal.ts @@ -1,11 +1,20 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + import { sendSync, sendAsync } from "./dispatch_json.ts"; -export function bindSignal(signo: number): { rid: number } { +interface BindSignalResponse { + rid: number; +} + +interface PollSignalResponse { + done: boolean; +} + +export function bindSignal(signo: number): BindSignalResponse { return sendSync("op_signal_bind", { signo }); } -export function pollSignal(rid: number): Promise<{ done: boolean }> { +export function pollSignal(rid: number): Promise<PollSignalResponse> { return sendAsync("op_signal_poll", { rid }); } diff --git a/cli/js/ops/timers.ts b/cli/js/ops/timers.ts index 1a7081df0..2fdbd6851 100644 --- a/cli/js/ops/timers.ts +++ b/cli/js/ops/timers.ts @@ -1,6 +1,12 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + import { sendSync, sendAsync } from "./dispatch_json.ts"; +interface NowResponse { + seconds: number; + subsecNanos: number; +} + export function stopGlobalTimer(): void { sendSync("op_global_timer_stop"); } @@ -9,11 +15,6 @@ export async function startGlobalTimer(timeout: number): Promise<void> { 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 index 60ec8846c..b278c2d75 100644 --- a/cli/js/ops/tls.ts +++ b/cli/js/ops/tls.ts @@ -1,4 +1,5 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + import { sendAsync, sendSync } from "./dispatch_json.ts"; export interface ConnectTLSRequest { diff --git a/cli/js/ops/tty.ts b/cli/js/ops/tty.ts index f848d2774..8899ca5b8 100644 --- a/cli/js/ops/tty.ts +++ b/cli/js/ops/tty.ts @@ -1,3 +1,5 @@ +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + import { sendSync } from "./dispatch_json.ts"; export function isatty(rid: number): boolean { @@ -5,8 +7,5 @@ export function isatty(rid: number): boolean { } export function setRaw(rid: number, mode: boolean): void { - sendSync("op_set_raw", { - rid, - mode, - }); + sendSync("op_set_raw", { rid, mode }); } diff --git a/cli/js/ops/worker_host.ts b/cli/js/ops/worker_host.ts index 11e268152..24e6b57ba 100644 --- a/cli/js/ops/worker_host.ts +++ b/cli/js/ops/worker_host.ts @@ -1,14 +1,19 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + /* eslint-disable @typescript-eslint/no-explicit-any */ import { sendAsync, sendSync } from "./dispatch_json.ts"; +interface CreateWorkerResponse { + id: number; +} + export function createWorker( specifier: string, hasSourceCode: boolean, sourceCode: string, useDenoNamespace: boolean, name?: string -): { id: number } { +): CreateWorkerResponse { return sendSync("op_create_worker", { specifier, hasSourceCode, |