2020-03-15 11:48:46 -04:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-06-23 22:32:43 -04:00
|
|
|
import {
|
|
|
|
Encodings,
|
|
|
|
getEncoding,
|
|
|
|
FileOptionsArgument,
|
|
|
|
TextOptionsArgument,
|
|
|
|
BinaryOptionsArgument,
|
|
|
|
TextEncodings,
|
|
|
|
BinaryEncodings,
|
|
|
|
} from "./_fs_common.ts";
|
2020-06-06 15:56:49 -04:00
|
|
|
import { Buffer } from "../buffer.ts";
|
2020-05-03 15:14:38 -04:00
|
|
|
import { fromFileUrl } from "../path.ts";
|
2020-03-15 11:48:46 -04:00
|
|
|
|
2020-06-23 22:32:43 -04:00
|
|
|
function maybeDecode(data: Uint8Array, encoding: TextEncodings): string;
|
|
|
|
function maybeDecode(
|
|
|
|
data: Uint8Array,
|
2020-07-14 15:24:17 -04:00
|
|
|
encoding: BinaryEncodings | null,
|
2020-06-23 22:32:43 -04:00
|
|
|
): Buffer;
|
2020-03-15 11:48:46 -04:00
|
|
|
function maybeDecode(
|
|
|
|
data: Uint8Array,
|
2020-07-14 15:24:17 -04:00
|
|
|
encoding: Encodings | null,
|
2020-06-06 15:56:49 -04:00
|
|
|
): string | Buffer {
|
|
|
|
const buffer = new Buffer(data.buffer, data.byteOffset, data.byteLength);
|
2020-06-23 22:32:43 -04:00
|
|
|
if (encoding && encoding !== "binary") return buffer.toString(encoding);
|
2020-06-06 15:56:49 -04:00
|
|
|
return buffer;
|
2020-03-15 11:48:46 -04:00
|
|
|
}
|
|
|
|
|
2020-06-23 22:32:43 -04:00
|
|
|
type TextCallback = (err: Error | null, data?: string) => void;
|
|
|
|
type BinaryCallback = (err: Error | null, data?: Buffer) => void;
|
|
|
|
type GenericCallback = (err: Error | null, data?: string | Buffer) => void;
|
|
|
|
type Callback = TextCallback | BinaryCallback | GenericCallback;
|
|
|
|
|
|
|
|
export function readFile(
|
|
|
|
path: string | URL,
|
|
|
|
options: TextOptionsArgument,
|
2020-07-14 15:24:17 -04:00
|
|
|
callback: TextCallback,
|
2020-06-23 22:32:43 -04:00
|
|
|
): void;
|
|
|
|
export function readFile(
|
|
|
|
path: string | URL,
|
|
|
|
options: BinaryOptionsArgument,
|
2020-07-14 15:24:17 -04:00
|
|
|
callback: BinaryCallback,
|
2020-06-23 22:32:43 -04:00
|
|
|
): void;
|
|
|
|
export function readFile(
|
|
|
|
path: string | URL,
|
|
|
|
options: null | undefined | FileOptionsArgument,
|
2020-07-14 15:24:17 -04:00
|
|
|
callback: BinaryCallback,
|
2020-06-23 22:32:43 -04:00
|
|
|
): void;
|
|
|
|
export function readFile(path: string | URL, callback: BinaryCallback): void;
|
2020-03-15 11:48:46 -04:00
|
|
|
export function readFile(
|
2020-05-03 15:14:38 -04:00
|
|
|
path: string | URL,
|
2020-06-23 22:32:43 -04:00
|
|
|
optOrCallback?: FileOptionsArgument | Callback | null | undefined,
|
2020-07-14 15:24:17 -04:00
|
|
|
callback?: Callback,
|
2020-03-15 11:48:46 -04:00
|
|
|
): void {
|
2020-05-03 15:14:38 -04:00
|
|
|
path = path instanceof URL ? fromFileUrl(path) : path;
|
2020-06-23 22:32:43 -04:00
|
|
|
let cb: Callback | undefined;
|
2020-03-15 11:48:46 -04:00
|
|
|
if (typeof optOrCallback === "function") {
|
|
|
|
cb = optOrCallback;
|
|
|
|
} else {
|
|
|
|
cb = callback;
|
|
|
|
}
|
|
|
|
|
|
|
|
const encoding = getEncoding(optOrCallback);
|
|
|
|
|
2020-06-23 22:32:43 -04:00
|
|
|
const p = Deno.readFile(path);
|
|
|
|
|
|
|
|
if (cb) {
|
|
|
|
p.then((data: Uint8Array) => {
|
|
|
|
if (encoding && encoding !== "binary") {
|
|
|
|
const text = maybeDecode(data, encoding);
|
|
|
|
return (cb as TextCallback)(null, text);
|
|
|
|
}
|
|
|
|
const buffer = maybeDecode(data, encoding);
|
|
|
|
(cb as BinaryCallback)(null, buffer);
|
|
|
|
}).catch((err) => cb && cb(err));
|
|
|
|
}
|
2020-03-15 11:48:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export function readFileSync(
|
2020-05-03 15:14:38 -04:00
|
|
|
path: string | URL,
|
2020-07-14 15:24:17 -04:00
|
|
|
opt: TextOptionsArgument,
|
2020-06-23 22:32:43 -04:00
|
|
|
): string;
|
|
|
|
export function readFileSync(
|
|
|
|
path: string | URL,
|
2020-07-14 15:24:17 -04:00
|
|
|
opt?: BinaryOptionsArgument,
|
2020-06-23 22:32:43 -04:00
|
|
|
): Buffer;
|
|
|
|
export function readFileSync(
|
|
|
|
path: string | URL,
|
2020-07-14 15:24:17 -04:00
|
|
|
opt?: FileOptionsArgument,
|
2020-06-06 15:56:49 -04:00
|
|
|
): string | Buffer {
|
2020-05-03 15:14:38 -04:00
|
|
|
path = path instanceof URL ? fromFileUrl(path) : path;
|
2020-06-23 22:32:43 -04:00
|
|
|
const data = Deno.readFileSync(path);
|
|
|
|
const encoding = getEncoding(opt);
|
|
|
|
if (encoding && encoding !== "binary") {
|
|
|
|
const text = maybeDecode(data, encoding);
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
const buffer = maybeDecode(data, encoding);
|
|
|
|
return buffer;
|
2020-03-15 11:48:46 -04:00
|
|
|
}
|