diff options
Diffstat (limited to 'cli/js')
-rw-r--r-- | cli/js/files.ts | 8 | ||||
-rw-r--r-- | cli/js/lib.deno.ns.d.ts | 78 | ||||
-rw-r--r-- | cli/js/ops/fs/chmod.ts | 7 | ||||
-rw-r--r-- | cli/js/ops/fs/chown.ts | 7 | ||||
-rw-r--r-- | cli/js/ops/fs/copy_file.ts | 16 | ||||
-rw-r--r-- | cli/js/ops/fs/open.ts | 10 | ||||
-rw-r--r-- | cli/js/ops/fs/read_dir.ts | 7 | ||||
-rw-r--r-- | cli/js/ops/fs/remove.ts | 10 | ||||
-rw-r--r-- | cli/js/ops/fs/stat.ts | 13 | ||||
-rw-r--r-- | cli/js/read_file.ts | 4 | ||||
-rw-r--r-- | cli/js/read_text_file.ts | 4 | ||||
-rw-r--r-- | cli/js/util.ts | 48 | ||||
-rw-r--r-- | cli/js/write_file.ts | 4 | ||||
-rw-r--r-- | cli/js/write_text_file.ts | 7 |
14 files changed, 166 insertions, 57 deletions
diff --git a/cli/js/files.ts b/cli/js/files.ts index d6df8bad0..83922627a 100644 --- a/cli/js/files.ts +++ b/cli/js/files.ts @@ -21,7 +21,7 @@ import { export { OpenOptions } from "./ops/fs/open.ts"; export function openSync( - path: string, + path: string | URL, options: OpenOptions = { read: true } ): File { checkOpenOptions(options); @@ -30,7 +30,7 @@ export function openSync( } export async function open( - path: string, + path: string | URL, options: OpenOptions = { read: true } ): Promise<File> { checkOpenOptions(options); @@ -38,7 +38,7 @@ export async function open( return new File(rid); } -export function createSync(path: string): File { +export function createSync(path: string | URL): File { return openSync(path, { read: true, write: true, @@ -47,7 +47,7 @@ export function createSync(path: string): File { }); } -export function create(path: string): Promise<File> { +export function create(path: string | URL): Promise<File> { return open(path, { read: true, write: true, diff --git a/cli/js/lib.deno.ns.d.ts b/cli/js/lib.deno.ns.d.ts index 972405680..c50c71d46 100644 --- a/cli/js/lib.deno.ns.d.ts +++ b/cli/js/lib.deno.ns.d.ts @@ -419,7 +419,7 @@ declare namespace Deno { * * Requires `allow-read` and/or `allow-write` permissions depending on options. */ - export function openSync(path: string, options?: OpenOptions): File; + export function openSync(path: string | URL, options?: OpenOptions): File; /** Open a file and resolve to an instance of `Deno.File`. The * file does not need to previously exist if using the `create` or `createNew` @@ -434,7 +434,10 @@ declare namespace Deno { * * Requires `allow-read` and/or `allow-write` permissions depending on options. */ - export function open(path: string, options?: OpenOptions): Promise<File>; + export function open( + path: string | URL, + options?: OpenOptions + ): Promise<File>; /** Creates a file if none exists or truncates an existing file and returns * an instance of `Deno.File`. @@ -445,7 +448,7 @@ declare namespace Deno { * * Requires `allow-read` and `allow-write` permissions. */ - export function createSync(path: string): File; + export function createSync(path: string | URL): File; /** Creates a file if none exists or truncates an existing file and resolves to * an instance of `Deno.File`. @@ -456,7 +459,7 @@ declare namespace Deno { * * Requires `allow-read` and `allow-write` permissions. */ - export function create(path: string): Promise<File>; + export function create(path: string | URL): Promise<File>; /** Synchronously read from a resource ID (`rid`) into an array buffer (`buffer`). * @@ -890,7 +893,7 @@ declare namespace Deno { * Defaults to throwing error if the directory already exists. * * Requires `allow-write` permission. */ - export function mkdirSync(path: string, options?: MkdirOptions): void; + export function mkdirSync(path: string | URL, options?: MkdirOptions): void; /** Creates a new directory with the specified path. * @@ -903,7 +906,10 @@ declare namespace Deno { * Defaults to throwing error if the directory already exists. * * Requires `allow-write` permission. */ - export function mkdir(path: string, options?: MkdirOptions): Promise<void>; + export function mkdir( + path: string | URL, + options?: MkdirOptions + ): Promise<void>; export interface MakeTempOptions { /** Directory where the temporary directory should be created (defaults to @@ -1011,7 +1017,7 @@ declare namespace Deno { * NOTE: This API currently throws on Windows * * Requires `allow-write` permission. */ - export function chmodSync(path: string, mode: number): void; + export function chmodSync(path: string | URL, mode: number): void; /** Changes the permission of a specific file/directory of specified path. * Ignores the process's umask. @@ -1041,7 +1047,7 @@ declare namespace Deno { * NOTE: This API currently throws on Windows * * Requires `allow-write` permission. */ - export function chmod(path: string, mode: number): Promise<void>; + export function chmod(path: string | URL, mode: number): Promise<void>; /** Synchronously change owner of a regular file or directory. This functionality * is not available on Windows. @@ -1058,7 +1064,7 @@ declare namespace Deno { * @param uid user id (UID) of the new owner * @param gid group id (GID) of the new owner */ - export function chownSync(path: string, uid: number, gid: number): void; + export function chownSync(path: string | URL, uid: number, gid: number): void; /** Change owner of a regular file or directory. This functionality * is not available on Windows. @@ -1075,7 +1081,11 @@ declare namespace Deno { * @param uid user id (UID) of the new owner * @param gid group id (GID) of the new owner */ - export function chown(path: string, uid: number, gid: number): Promise<void>; + export function chown( + path: string | URL, + uid: number, + gid: number + ): Promise<void>; export interface RemoveOptions { /** Defaults to `false`. If set to `true`, path will be removed even if @@ -1094,7 +1104,7 @@ declare namespace Deno { * directory and the `recursive` option isn't set to `true`. * * Requires `allow-write` permission. */ - export function removeSync(path: string, options?: RemoveOptions): void; + export function removeSync(path: string | URL, options?: RemoveOptions): void; /** Removes the named file or directory. * @@ -1107,7 +1117,10 @@ declare namespace Deno { * directory and the `recursive` option isn't set to `true`. * * Requires `allow-write` permission. */ - export function remove(path: string, options?: RemoveOptions): Promise<void>; + export function remove( + path: string | URL, + options?: RemoveOptions + ): Promise<void>; /** Synchronously renames (moves) `oldpath` to `newpath`. Paths may be files or * directories. If `newpath` already exists and is not a directory, @@ -1152,7 +1165,7 @@ declare namespace Deno { * ``` * * Requires `allow-read` permission. */ - export function readTextFileSync(path: string): string; + export function readTextFileSync(path: string | URL): string; /** Asynchronously reads and returns the entire contents of a file as a utf8 * encoded string. Reading a directory returns an empty data array. @@ -1163,7 +1176,7 @@ declare namespace Deno { * ``` * * Requires `allow-read` permission. */ - export function readTextFile(path: string): Promise<string>; + export function readTextFile(path: string | URL): Promise<string>; /** Synchronously reads and returns the entire contents of a file as an array * of bytes. `TextDecoder` can be used to transform the bytes to string if @@ -1176,7 +1189,7 @@ declare namespace Deno { * ``` * * Requires `allow-read` permission. */ - export function readFileSync(path: string): Uint8Array; + export function readFileSync(path: string | URL): Uint8Array; /** Reads and resolves to the entire contents of a file as an array of bytes. * `TextDecoder` can be used to transform the bytes to string if required. @@ -1189,7 +1202,7 @@ declare namespace Deno { * ``` * * Requires `allow-read` permission. */ - export function readFile(path: string): Promise<Uint8Array>; + export function readFile(path: string | URL): Promise<Uint8Array>; /** A FileInfo describes a file and is returned by `stat`, `lstat`, * `statSync`, `lstatSync`. */ @@ -1307,7 +1320,7 @@ declare namespace Deno { * Throws error if `path` is not a directory. * * Requires `allow-read` permission. */ - export function readDirSync(path: string): Iterable<DirEntry>; + export function readDirSync(path: string | URL): Iterable<DirEntry>; /** Reads the directory given by `path` and returns an async iterable of * `Deno.DirEntry`. @@ -1321,7 +1334,7 @@ declare namespace Deno { * Throws error if `path` is not a directory. * * Requires `allow-read` permission. */ - export function readDir(path: string): AsyncIterable<DirEntry>; + export function readDir(path: string | URL): AsyncIterable<DirEntry>; /** Synchronously copies the contents and permissions of one file to another * specified path, by default creating a new file if needed, else overwriting. @@ -1333,7 +1346,10 @@ declare namespace Deno { * * Requires `allow-read` permission on fromPath. * Requires `allow-write` permission on toPath. */ - export function copyFileSync(fromPath: string, toPath: string): void; + export function copyFileSync( + fromPath: string | URL, + toPath: string | URL + ): void; /** 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 @@ -1345,7 +1361,10 @@ declare namespace Deno { * * Requires `allow-read` permission on fromPath. * Requires `allow-write` permission on toPath. */ - export function copyFile(fromPath: string, toPath: string): Promise<void>; + export function copyFile( + fromPath: string | URL, + toPath: string | URL + ): Promise<void>; /** Returns the full path destination of the named symbolic link. * @@ -1382,7 +1401,7 @@ declare namespace Deno { * ``` * * Requires `allow-read` permission. */ - export function lstat(path: string): Promise<FileInfo>; + export function lstat(path: string | URL): Promise<FileInfo>; /** Synchronously returns a `Deno.FileInfo` for the specified `path`. If * `path` is a symlink, information for the symlink will be returned instead of @@ -1394,7 +1413,7 @@ declare namespace Deno { * ``` * * Requires `allow-read` permission. */ - export function lstatSync(path: string): FileInfo; + export function lstatSync(path: string | URL): FileInfo; /** Resolves to a `Deno.FileInfo` for the specified `path`. Will always * follow symlinks. @@ -1406,7 +1425,7 @@ declare namespace Deno { * ``` * * Requires `allow-read` permission. */ - export function stat(path: string): Promise<FileInfo>; + export function stat(path: string | URL): Promise<FileInfo>; /** Synchronously returns a `Deno.FileInfo` for the specified `path`. Will * always follow symlinks. @@ -1418,7 +1437,7 @@ declare namespace Deno { * ``` * * Requires `allow-read` permission. */ - export function statSync(path: string): FileInfo; + export function statSync(path: string | URL): FileInfo; /** Options for writing to a file. */ export interface WriteFileOptions { @@ -1448,7 +1467,7 @@ declare namespace Deno { * `false`. */ export function writeFileSync( - path: string, + path: string | URL, data: Uint8Array, options?: WriteFileOptions ): void; @@ -1468,7 +1487,7 @@ declare namespace Deno { * Requires `allow-write` permission, and `allow-read` if `options.create` is `false`. */ export function writeFile( - path: string, + path: string | URL, data: Uint8Array, options?: WriteFileOptions ): Promise<void>; @@ -1482,7 +1501,7 @@ declare namespace Deno { * * Requires `allow-write` permission, and `allow-read` if `options.create` is `false`. */ - export function writeTextFileSync(path: string, data: string): void; + export function writeTextFileSync(path: string | URL, data: string): void; /** Asynchronously write string `data` to the given `path`, by default creating a new file if needed, * else overwriting. @@ -1493,7 +1512,10 @@ declare namespace Deno { * * Requires `allow-write` permission, and `allow-read` if `options.create` is `false`. */ - export function writeTextFile(path: string, data: string): Promise<void>; + export function writeTextFile( + path: string | URL, + data: string + ): Promise<void>; /** Synchronously truncates or extends the specified file, to reach the * specified `len`. If `len` is not specified then the entire file contents diff --git a/cli/js/ops/fs/chmod.ts b/cli/js/ops/fs/chmod.ts index 91e898360..76a3c8f49 100644 --- a/cli/js/ops/fs/chmod.ts +++ b/cli/js/ops/fs/chmod.ts @@ -1,10 +1,13 @@ // 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, mode: number): void { +export function chmodSync(path: string | URL, mode: number): void { + path = pathFromURL(path); sendSync("op_chmod", { path, mode }); } -export async function chmod(path: string, mode: number): Promise<void> { +export async function chmod(path: string | URL, mode: number): Promise<void> { + path = pathFromURL(path); await sendAsync("op_chmod", { path, mode }); } diff --git a/cli/js/ops/fs/chown.ts b/cli/js/ops/fs/chown.ts index d6e3702c6..f24ab5e55 100644 --- a/cli/js/ops/fs/chown.ts +++ b/cli/js/ops/fs/chown.ts @@ -1,14 +1,17 @@ // 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 chownSync(path: string, uid: number, gid: number): void { +export function chownSync(path: string | URL, uid: number, gid: number): void { + path = pathFromURL(path); sendSync("op_chown", { path, uid, gid }); } export async function chown( - path: string, + path: string | URL, uid: number, gid: number ): Promise<void> { + path = pathFromURL(path); 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 index 4c8c74667..6bbb3f599 100644 --- a/cli/js/ops/fs/copy_file.ts +++ b/cli/js/ops/fs/copy_file.ts @@ -1,13 +1,23 @@ // 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 copyFileSync( + fromPath: string | URL, + toPath: string | URL +): void { + fromPath = pathFromURL(fromPath); + toPath = pathFromURL(toPath); -export function copyFileSync(fromPath: string, toPath: string): void { sendSync("op_copy_file", { from: fromPath, to: toPath }); } export async function copyFile( - fromPath: string, - toPath: string + fromPath: string | URL, + toPath: string | URL ): Promise<void> { + fromPath = pathFromURL(fromPath); + toPath = pathFromURL(toPath); + await sendAsync("op_copy_file", { from: fromPath, to: toPath }); } diff --git a/cli/js/ops/fs/open.ts b/cli/js/ops/fs/open.ts index afe713db8..3742d0b52 100644 --- a/cli/js/ops/fs/open.ts +++ b/cli/js/ops/fs/open.ts @@ -1,5 +1,6 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. import { sendSync, sendAsync } from "../dispatch_json.ts"; +import { pathFromURL } from "../../util.ts"; export interface OpenOptions { read?: boolean; @@ -15,13 +16,18 @@ export interface OpenOptions { mode?: number; } -export function openSync(path: string, options: OpenOptions): number { +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 }); } -export function open(path: string, options: OpenOptions): Promise<number> { +export function open( + path: string | URL, + options: OpenOptions +): Promise<number> { const mode: number | undefined = options?.mode; + path = pathFromURL(path); return sendAsync("op_open", { path, options, diff --git a/cli/js/ops/fs/read_dir.ts b/cli/js/ops/fs/read_dir.ts index 1e8d79edc..09c5d1c12 100644 --- a/cli/js/ops/fs/read_dir.ts +++ b/cli/js/ops/fs/read_dir.ts @@ -1,5 +1,6 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. import { sendSync, sendAsync } from "../dispatch_json.ts"; +import { pathFromURL } from "../../util.ts"; export interface DirEntry { name: string; @@ -16,11 +17,13 @@ function res(response: ReadDirResponse): DirEntry[] { return response.entries; } -export function readDirSync(path: string): Iterable<DirEntry> { +export function readDirSync(path: string | URL): Iterable<DirEntry> { + path = pathFromURL(path); return res(sendSync("op_read_dir", { path }))[Symbol.iterator](); } -export function readDir(path: string): AsyncIterable<DirEntry> { +export function readDir(path: string | URL): AsyncIterable<DirEntry> { + path = pathFromURL(path); const array = sendAsync("op_read_dir", { path }).then(res); return { async *[Symbol.asyncIterator](): AsyncIterableIterator<DirEntry> { diff --git a/cli/js/ops/fs/remove.ts b/cli/js/ops/fs/remove.ts index d5af82f9b..d1a8702f1 100644 --- a/cli/js/ops/fs/remove.ts +++ b/cli/js/ops/fs/remove.ts @@ -1,17 +1,23 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. import { sendSync, sendAsync } from "../dispatch_json.ts"; +import { pathFromURL } from "../../util.ts"; export interface RemoveOptions { recursive?: boolean; } -export function removeSync(path: string, options: RemoveOptions = {}): void { +export function removeSync( + path: string | URL, + options: RemoveOptions = {} +): void { + path = pathFromURL(path); sendSync("op_remove", { path, recursive: !!options.recursive }); } export async function remove( - path: string, + path: string | URL, options: RemoveOptions = {} ): Promise<void> { + path = pathFromURL(path); await sendAsync("op_remove", { path, recursive: !!options.recursive }); } diff --git a/cli/js/ops/fs/stat.ts b/cli/js/ops/fs/stat.ts index e8fd28218..93d31fc3f 100644 --- a/cli/js/ops/fs/stat.ts +++ b/cli/js/ops/fs/stat.ts @@ -1,6 +1,7 @@ // 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"; export interface FileInfo { size: number; @@ -65,7 +66,8 @@ export function parseFileInfo(response: StatResponse): FileInfo { }; } -export async function lstat(path: string): Promise<FileInfo> { +export async function lstat(path: string | URL): Promise<FileInfo> { + path = pathFromURL(path); const res = (await sendAsync("op_stat", { path, lstat: true, @@ -73,7 +75,8 @@ export async function lstat(path: string): Promise<FileInfo> { return parseFileInfo(res); } -export function lstatSync(path: string): FileInfo { +export function lstatSync(path: string | URL): FileInfo { + path = pathFromURL(path); const res = sendSync("op_stat", { path, lstat: true, @@ -81,7 +84,8 @@ export function lstatSync(path: string): FileInfo { return parseFileInfo(res); } -export async function stat(path: string): Promise<FileInfo> { +export async function stat(path: string | URL): Promise<FileInfo> { + path = pathFromURL(path); const res = (await sendAsync("op_stat", { path, lstat: false, @@ -89,7 +93,8 @@ export async function stat(path: string): Promise<FileInfo> { return parseFileInfo(res); } -export function statSync(path: string): FileInfo { +export function statSync(path: string | URL): FileInfo { + path = pathFromURL(path); const res = sendSync("op_stat", { path, lstat: false, diff --git a/cli/js/read_file.ts b/cli/js/read_file.ts index 317401af5..b8d428b8c 100644 --- a/cli/js/read_file.ts +++ b/cli/js/read_file.ts @@ -2,14 +2,14 @@ import { open, openSync } from "./files.ts"; import { readAll, readAllSync } from "./buffer.ts"; -export function readFileSync(path: string): Uint8Array { +export function readFileSync(path: string | URL): Uint8Array { const file = openSync(path); const contents = readAllSync(file); file.close(); return contents; } -export async function readFile(path: string): Promise<Uint8Array> { +export async function readFile(path: string | URL): Promise<Uint8Array> { const file = await open(path); const contents = await readAll(file); file.close(); diff --git a/cli/js/read_text_file.ts b/cli/js/read_text_file.ts index 3423b26b8..154b01b0e 100644 --- a/cli/js/read_text_file.ts +++ b/cli/js/read_text_file.ts @@ -1,7 +1,7 @@ import { open, openSync } from "./files.ts"; import { readAll, readAllSync } from "./buffer.ts"; -export function readTextFileSync(path: string): string { +export function readTextFileSync(path: string | URL): string { const decoder = new TextDecoder(); const file = openSync(path); const content = readAllSync(file); @@ -9,7 +9,7 @@ export function readTextFileSync(path: string): string { return decoder.decode(content); } -export async function readTextFile(path: string): Promise<string> { +export async function readTextFile(path: string | URL): Promise<string> { const decoder = new TextDecoder(); const file = await open(path); const content = await readAll(file); diff --git a/cli/js/util.ts b/cli/js/util.ts index 309bfcd0c..c50a4cdcb 100644 --- a/cli/js/util.ts +++ b/cli/js/util.ts @@ -1,4 +1,6 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. +import { build } from "./build.ts"; +import { exposeForTest } from "./internals.ts"; let logDebug = false; let logSource = "JS"; @@ -78,3 +80,49 @@ export function immutableDefine( writable: false, }); } + +function pathFromURLWin32(url: URL): string { + if (url.hostname !== "") { + //TODO(actual-size) Node adds a punycode decoding step, we should consider adding this + return `\\\\${url.hostname}${url.pathname}`; + } + + const validPath = /^\/(?<driveLetter>[A-Za-z]):\//; + const matches = validPath.exec(url.pathname); + + if (!matches?.groups?.driveLetter) { + throw new TypeError("A URL with the file schema must be absolute."); + } + + const pathname = url.pathname.replace(/\//g, "\\"); + // we don't want a leading slash on an absolute path in Windows + return pathname.slice(1); +} + +function pathFromURLPosix(url: URL): string { + if (url.hostname !== "") { + throw new TypeError(`Host must be empty.`); + } + + return decodeURIComponent(url.pathname); +} + +export function pathFromURL(pathOrUrl: string | URL): string { + if (typeof pathOrUrl == "string") { + try { + pathOrUrl = new URL(pathOrUrl); + } catch {} + } + if (pathOrUrl instanceof URL) { + if (pathOrUrl.protocol != "file:") { + throw new TypeError("Must be a path string or file URL."); + } + + return build.os == "windows" + ? pathFromURLWin32(pathOrUrl) + : pathFromURLPosix(pathOrUrl); + } + return pathOrUrl; +} + +exposeForTest("pathFromURL", pathFromURL); diff --git a/cli/js/write_file.ts b/cli/js/write_file.ts index 6961b78ea..3106c48ef 100644 --- a/cli/js/write_file.ts +++ b/cli/js/write_file.ts @@ -12,7 +12,7 @@ export interface WriteFileOptions { } export function writeFileSync( - path: string, + path: string | URL, data: Uint8Array, options: WriteFileOptions = {} ): void { @@ -42,7 +42,7 @@ export function writeFileSync( } export async function writeFile( - path: string, + path: string | URL, data: Uint8Array, options: WriteFileOptions = {} ): Promise<void> { diff --git a/cli/js/write_text_file.ts b/cli/js/write_text_file.ts index d2a6575d7..4f111edb4 100644 --- a/cli/js/write_text_file.ts +++ b/cli/js/write_text_file.ts @@ -1,7 +1,7 @@ import { open, openSync } from "./files.ts"; import { writeAll, writeAllSync } from "./buffer.ts"; -export function writeTextFileSync(path: string, data: string): void { +export function writeTextFileSync(path: string | URL, data: string): void { const file = openSync(path, { write: true, create: true, truncate: true }); const enc = new TextEncoder(); const contents = enc.encode(data); @@ -9,7 +9,10 @@ export function writeTextFileSync(path: string, data: string): void { file.close(); } -export async function writeTextFile(path: string, data: string): Promise<void> { +export async function writeTextFile( + path: string | URL, + data: string +): Promise<void> { const file = await open(path, { write: true, create: true, truncate: true }); const enc = new TextEncoder(); const contents = enc.encode(data); |