1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-24 16:19:12 -05:00
denoland-deno/runtime/js/40_read_file.js
Luca Casonato c73ef5fa14
refactor(web): use encoding_rs for text encoding (#10844)
This commit removes all JS based text encoding / text decoding. Instead
encoding now happens in Rust via encoding_rs (already in tree). This
implementation retains stream support, but adds the last missing
encodings. We are incredibly close to 100% WPT on text encoding now.

This should reduce our baseline heap by quite a bit.
2021-06-05 23:10:07 +02:00

55 lines
1.1 KiB
JavaScript

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