1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-22 23:34:47 -05:00
denoland-deno/runtime/js/40_read_file.js
Ryan Dahl 1fb5858009
chore: update copyright to 2022 (#13306)
Co-authored-by: Erfan Safari <erfanshield@outlook.com>
2022-01-07 22:09:52 -05:00

52 lines
1.2 KiB
JavaScript

// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
"use strict";
((window) => {
const core = window.Deno.core;
const { open, openSync } = window.__bootstrap.files;
const { readAllSync, readAll, readAllSyncSized, readAllInnerSized } =
window.__bootstrap.io;
function readFileSync(path) {
const file = openSync(path);
try {
const { size } = file.statSync();
if (size === 0) {
return readAllSync(file);
} else {
return readAllSyncSized(file, size);
}
} finally {
file.close();
}
}
async function readFile(path, options) {
const file = await open(path);
try {
const { size } = await file.stat();
if (size === 0) {
return await readAll(file);
} else {
return await readAllInnerSized(file, size, options);
}
} finally {
file.close();
}
}
function readTextFileSync(path) {
return core.decode(readFileSync(path));
}
async function readTextFile(path, options) {
return core.decode(await readFile(path, options));
}
window.__bootstrap.readFile = {
readFile,
readFileSync,
readTextFileSync,
readTextFile,
};
})(this);