2020-03-08 19:14:53 -04:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-03-12 10:12:27 -04:00
|
|
|
|
2020-05-04 18:59:37 -04:00
|
|
|
import { notImplemented } from "../_utils.ts";
|
|
|
|
|
|
|
|
export type CallbackWithError = (err?: Error | null) => void;
|
2020-03-12 10:12:27 -04:00
|
|
|
|
2020-06-23 22:32:43 -04:00
|
|
|
export type TextEncodings =
|
|
|
|
| "ascii"
|
|
|
|
| "utf8"
|
|
|
|
| "utf-8"
|
|
|
|
| "utf16le"
|
|
|
|
| "ucs2"
|
|
|
|
| "ucs-2"
|
|
|
|
| "base64"
|
|
|
|
| "latin1"
|
|
|
|
| "hex";
|
|
|
|
export type BinaryEncodings = "binary";
|
|
|
|
export type Encodings = TextEncodings | BinaryEncodings;
|
|
|
|
|
2020-03-08 19:14:53 -04:00
|
|
|
export interface FileOptions {
|
2020-06-23 22:32:43 -04:00
|
|
|
encoding?: Encodings;
|
2020-03-08 19:14:53 -04:00
|
|
|
flag?: string;
|
|
|
|
}
|
|
|
|
|
2020-06-23 22:32:43 -04:00
|
|
|
export type TextOptionsArgument =
|
|
|
|
| TextEncodings
|
|
|
|
| ({ encoding: TextEncodings } & FileOptions);
|
|
|
|
export type BinaryOptionsArgument =
|
|
|
|
| BinaryEncodings
|
|
|
|
| ({ encoding: BinaryEncodings } & FileOptions);
|
|
|
|
export type FileOptionsArgument = Encodings | FileOptions;
|
|
|
|
|
2020-05-04 18:59:37 -04:00
|
|
|
export interface WriteFileOptions extends FileOptions {
|
|
|
|
mode?: number;
|
|
|
|
}
|
|
|
|
|
2020-03-08 19:14:53 -04:00
|
|
|
export function isFileOptions(
|
2020-05-04 18:59:37 -04:00
|
|
|
fileOptions: string | WriteFileOptions | undefined
|
2020-03-08 19:14:53 -04:00
|
|
|
): fileOptions is FileOptions {
|
|
|
|
if (!fileOptions) return false;
|
|
|
|
|
|
|
|
return (
|
|
|
|
(fileOptions as FileOptions).encoding != undefined ||
|
|
|
|
(fileOptions as FileOptions).flag != undefined ||
|
2020-05-04 18:59:37 -04:00
|
|
|
(fileOptions as WriteFileOptions).mode != undefined
|
2020-03-08 19:14:53 -04:00
|
|
|
);
|
|
|
|
}
|
2020-05-04 18:59:37 -04:00
|
|
|
|
|
|
|
export function getEncoding(
|
2020-06-23 22:32:43 -04:00
|
|
|
optOrCallback?: FileOptions | WriteFileOptions | Function | Encodings | null
|
|
|
|
): Encodings | null {
|
2020-05-04 18:59:37 -04:00
|
|
|
if (!optOrCallback || typeof optOrCallback === "function") {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const encoding =
|
|
|
|
typeof optOrCallback === "string" ? optOrCallback : optOrCallback.encoding;
|
|
|
|
if (!encoding) return null;
|
2020-06-06 15:56:49 -04:00
|
|
|
return encoding;
|
|
|
|
}
|
|
|
|
|
2020-06-23 22:32:43 -04:00
|
|
|
export function checkEncoding(encoding: Encodings | null): Encodings | null {
|
2020-06-06 15:56:49 -04:00
|
|
|
if (!encoding) return null;
|
2020-06-27 16:52:49 -04:00
|
|
|
|
|
|
|
encoding = encoding.toLowerCase() as Encodings;
|
|
|
|
if (["utf8", "hex", "base64"].includes(encoding)) return encoding;
|
|
|
|
|
|
|
|
if (encoding === "utf-8") {
|
2020-05-04 18:59:37 -04:00
|
|
|
return "utf8";
|
|
|
|
}
|
2020-06-23 22:32:43 -04:00
|
|
|
if (encoding === "binary") {
|
|
|
|
return "binary";
|
|
|
|
// before this was buffer, however buffer is not used in Node
|
|
|
|
// node -e "require('fs').readFile('../world.txt', 'buffer', console.log)"
|
2020-05-04 18:59:37 -04:00
|
|
|
}
|
|
|
|
|
2020-06-27 16:52:49 -04:00
|
|
|
const notImplementedEncodings = ["utf16le", "latin1", "ascii", "ucs2"];
|
2020-05-04 18:59:37 -04:00
|
|
|
|
2020-06-27 16:52:49 -04:00
|
|
|
if (notImplementedEncodings.includes(encoding as string)) {
|
2020-05-04 18:59:37 -04:00
|
|
|
notImplemented(`"${encoding}" encoding`);
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new Error(`The value "${encoding}" is invalid for option "encoding"`);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getOpenOptions(flag: string | undefined): Deno.OpenOptions {
|
|
|
|
if (!flag) {
|
|
|
|
return { create: true, append: true };
|
|
|
|
}
|
|
|
|
|
|
|
|
let openOptions: Deno.OpenOptions;
|
|
|
|
switch (flag) {
|
|
|
|
case "a": {
|
|
|
|
// 'a': Open file for appending. The file is created if it does not exist.
|
|
|
|
openOptions = { create: true, append: true };
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "ax": {
|
|
|
|
// 'ax': Like 'a' but fails if the path exists.
|
|
|
|
openOptions = { createNew: true, write: true, append: true };
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "a+": {
|
|
|
|
// 'a+': Open file for reading and appending. The file is created if it does not exist.
|
|
|
|
openOptions = { read: true, create: true, append: true };
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "ax+": {
|
|
|
|
// 'ax+': Like 'a+' but fails if the path exists.
|
|
|
|
openOptions = { read: true, createNew: true, append: true };
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "r": {
|
|
|
|
// 'r': Open file for reading. An exception occurs if the file does not exist.
|
|
|
|
openOptions = { read: true };
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "r+": {
|
|
|
|
// 'r+': Open file for reading and writing. An exception occurs if the file does not exist.
|
|
|
|
openOptions = { read: true, write: true };
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "w": {
|
|
|
|
// 'w': Open file for writing. The file is created (if it does not exist) or truncated (if it exists).
|
|
|
|
openOptions = { create: true, write: true, truncate: true };
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "wx": {
|
|
|
|
// 'wx': Like 'w' but fails if the path exists.
|
|
|
|
openOptions = { createNew: true, write: true };
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "w+": {
|
|
|
|
// 'w+': Open file for reading and writing. The file is created (if it does not exist) or truncated (if it exists).
|
|
|
|
openOptions = { create: true, write: true, truncate: true, read: true };
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "wx+": {
|
|
|
|
// 'wx+': Like 'w+' but fails if the path exists.
|
|
|
|
openOptions = { createNew: true, write: true, read: true };
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "as": {
|
|
|
|
// 'as': Open file for appending in synchronous mode. The file is created if it does not exist.
|
|
|
|
openOptions = { create: true, append: true };
|
|
|
|
}
|
|
|
|
case "as+": {
|
|
|
|
// 'as+': Open file for reading and appending in synchronous mode. The file is created if it does not exist.
|
|
|
|
openOptions = { create: true, read: true, append: true };
|
|
|
|
}
|
|
|
|
case "rs+": {
|
|
|
|
// 'rs+': Open file for reading and writing in synchronous mode. Instructs the operating system to bypass the local file system cache.
|
|
|
|
openOptions = { create: true, read: true, write: true };
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
throw new Error(`Unrecognized file system flag: ${flag}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return openOptions;
|
|
|
|
}
|