aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <jarred@jarredsumner.com> 2022-03-01 00:50:30 -0800
committerGravatar Jarred Sumner <jarred@jarredsumner.com> 2022-03-01 00:50:30 -0800
commitea80cd1cd8a0ecea511914707189ebe130d3b0ba (patch)
treee0a48b75cf057fdb5f77f33c98ddc0e05d9a4eb6
parent114c0e8ed2a0eea835139ece3677efce09a8b702 (diff)
downloadbun-ea80cd1cd8a0ecea511914707189ebe130d3b0ba.tar.gz
bun-ea80cd1cd8a0ecea511914707189ebe130d3b0ba.tar.zst
bun-ea80cd1cd8a0ecea511914707189ebe130d3b0ba.zip
[bun.js] shim async fs
-rw-r--r--integration/bunjs-only-snippets/fs.test.js34
-rw-r--r--src/javascript/jsc/fs.exports.js228
2 files changed, 226 insertions, 36 deletions
diff --git a/integration/bunjs-only-snippets/fs.test.js b/integration/bunjs-only-snippets/fs.test.js
index c4d937ca3..4c048d199 100644
--- a/integration/bunjs-only-snippets/fs.test.js
+++ b/integration/bunjs-only-snippets/fs.test.js
@@ -1,5 +1,11 @@
import { describe, it, expect } from "vitest";
-import { mkdirSync, existsSync, readFileSync, writeFileSync } from "node:fs";
+import {
+ mkdirSync,
+ existsSync,
+ readFileSync,
+ writeFileSync,
+ readFile,
+} from "node:fs";
const Buffer = globalThis.Buffer || Uint8Array;
@@ -36,6 +42,32 @@ describe("readFileSync", () => {
});
});
+describe("readFile", () => {
+ it("works", async () => {
+ await new Promise((resolve, reject) => {
+ readFile(import.meta.dir + "/readFileSync.txt", "utf8", (err, text) => {
+ expect(text).toBe("File read successfully");
+ resolve(true);
+ });
+ });
+ });
+
+ it("returning Buffer works", async () => {
+ await new Promise((resolve, reject) => {
+ readFile(import.meta.dir + "/readFileSync.txt", (err, text) => {
+ const encoded = [
+ 70, 105, 108, 101, 32, 114, 101, 97, 100, 32, 115, 117, 99, 99, 101,
+ 115, 115, 102, 117, 108, 108, 121,
+ ];
+ for (let i = 0; i < encoded.length; i++) {
+ expect(text[i]).toBe(encoded[i]);
+ }
+ resolve(true);
+ });
+ });
+ });
+});
+
describe("writeFileSync", () => {
it("works", () => {
const path = `/tmp/${Date.now()}.writeFileSync.txt`;
diff --git a/src/javascript/jsc/fs.exports.js b/src/javascript/jsc/fs.exports.js
index 923ef9801..e2f182137 100644
--- a/src/javascript/jsc/fs.exports.js
+++ b/src/javascript/jsc/fs.exports.js
@@ -1,39 +1,124 @@
var fs = Bun.fs();
-export default fs;
-export var access = fs.access.bind(fs);
-export var appendFile = fs.appendFile.bind(fs);
-export var close = fs.close.bind(fs);
-export var copyFile = fs.copyFile.bind(fs);
-export var exists = fs.exists.bind(fs);
-export var chown = fs.chown.bind(fs);
-export var chmod = fs.chmod.bind(fs);
-export var fchmod = fs.fchmod.bind(fs);
-export var fchown = fs.fchown.bind(fs);
-export var fstat = fs.fstat.bind(fs);
-export var fsync = fs.fsync.bind(fs);
-export var ftruncate = fs.ftruncate.bind(fs);
-export var futimes = fs.futimes.bind(fs);
-export var lchmod = fs.lchmod.bind(fs);
-export var lchown = fs.lchown.bind(fs);
-export var link = fs.link.bind(fs);
-export var lstat = fs.lstat.bind(fs);
-export var mkdir = fs.mkdir.bind(fs);
-export var mkdtemp = fs.mkdtemp.bind(fs);
-export var open = fs.open.bind(fs);
-export var read = fs.read.bind(fs);
-export var write = fs.write.bind(fs);
-export var readdir = fs.readdir.bind(fs);
-export var readFile = fs.readFile.bind(fs);
-export var writeFile = fs.writeFile.bind(fs);
-export var readlink = fs.readlink.bind(fs);
-export var realpath = fs.realpath.bind(fs);
-export var rename = fs.rename.bind(fs);
-export var stat = fs.stat.bind(fs);
-export var symlink = fs.symlink.bind(fs);
-export var truncate = fs.truncate.bind(fs);
-export var unlink = fs.unlink.bind(fs);
-export var utimes = fs.utimes.bind(fs);
-export var lutimes = fs.lutimes.bind(fs);
+
+export function access(...args) {
+ callbackify(fs.accessSync, args);
+}
+export function appendFile(...args) {
+ callbackify(fs.appendFileSync, args);
+}
+export function close(...args) {
+ callbackify(fs.closeSync, args);
+}
+export function copyFile(...args) {
+ callbackify(fs.copyFileSync, args);
+}
+export function exists(...args) {
+ callbackify(fs.existsSync, args);
+}
+export function chown(...args) {
+ callbackify(fs.chownSync, args);
+}
+export function chmod(...args) {
+ callbackify(fs.chmodSync, args);
+}
+export function fchmod(...args) {
+ callbackify(fs.fchmodSync, args);
+}
+export function fchown(...args) {
+ callbackify(fs.fchownSync, args);
+}
+export function fstat(...args) {
+ callbackify(fs.fstatSync, args);
+}
+export function fsync(...args) {
+ callbackify(fs.fsyncSync, args);
+}
+export function ftruncate(...args) {
+ callbackify(fs.ftruncateSync, args);
+}
+export function futimes(...args) {
+ callbackify(fs.futimesSync, args);
+}
+export function lchmod(...args) {
+ callbackify(fs.lchmodSync, args);
+}
+export function lchown(...args) {
+ callbackify(fs.lchownSync, args);
+}
+export function link(...args) {
+ callbackify(fs.linkSync, args);
+}
+export function lstat(...args) {
+ callbackify(fs.lstatSync, args);
+}
+export function mkdir(...args) {
+ callbackify(fs.mkdirSync, args);
+}
+export function mkdtemp(...args) {
+ callbackify(fs.mkdtempSync, args);
+}
+export function open(...args) {
+ callbackify(fs.openSync, args);
+}
+export function read(...args) {
+ callbackify(fs.readSync, args);
+}
+export function write(...args) {
+ callbackify(fs.writeSync, args);
+}
+export function readdir(...args) {
+ callbackify(fs.readdirSync, args);
+}
+export function readFile(...args) {
+ callbackify(fs.readFileSync, args);
+}
+export function writeFile(...args) {
+ callbackify(fs.writeFileSync, args);
+}
+export function readlink(...args) {
+ callbackify(fs.readlinkSync, args);
+}
+export function realpath(...args) {
+ callbackify(fs.realpathSync, args);
+}
+export function rename(...args) {
+ callbackify(fs.renameSync, args);
+}
+export function stat(...args) {
+ callbackify(fs.statSync, args);
+}
+export function symlink(...args) {
+ callbackify(fs.symlinkSync, args);
+}
+export function truncate(...args) {
+ callbackify(fs.truncateSync, args);
+}
+export function unlink(...args) {
+ callbackify(fs.unlinkSync, args);
+}
+export function utimes(...args) {
+ callbackify(fs.utimesSync, args);
+}
+export function lutimes(...args) {
+ callbackify(fs.lutimesSync, args);
+}
+
+function callbackify(fsFunction, args) {
+ queueMicrotask(function () {
+ try {
+ args[args.length - 1](
+ null,
+ fsFunction.apply(fs, args.slice(0, args.length - 1))
+ );
+ } catch (err) {
+ args[args.length - 1](err);
+ } finally {
+ // ensure we don't leak it
+ args = null;
+ }
+ });
+}
+
export var accessSync = fs.accessSync.bind(fs);
export var appendFileSync = fs.appendFileSync.bind(fs);
export var closeSync = fs.closeSync.bind(fs);
@@ -75,3 +160,76 @@ export var createWriteStream = fs.createWriteStream.bind(fs);
// lol
realpath.native = realpath;
realpathSync.native = realpathSync;
+
+export default {
+ access,
+ appendFile,
+ close,
+ copyFile,
+ exists,
+ chown,
+ chmod,
+ fchmod,
+ fchown,
+ fstat,
+ fsync,
+ ftruncate,
+ futimes,
+ lchmod,
+ lchown,
+ link,
+ lstat,
+ mkdir,
+ mkdtemp,
+ open,
+ read,
+ write,
+ readdir,
+ readFile,
+ writeFile,
+ readlink,
+ realpath,
+ rename,
+ stat,
+ symlink,
+ truncate,
+ unlink,
+ utimes,
+ lutimes,
+ accessSync,
+ appendFileSync,
+ closeSync,
+ copyFileSync,
+ existsSync,
+ chownSync,
+ chmodSync,
+ fchmodSync,
+ fchownSync,
+ fstatSync,
+ fsyncSync,
+ ftruncateSync,
+ futimesSync,
+ lchmodSync,
+ lchownSync,
+ linkSync,
+ lstatSync,
+ mkdirSync,
+ mkdtempSync,
+ openSync,
+ readSync,
+ writeSync,
+ readdirSync,
+ readFileSync,
+ writeFileSync,
+ readlinkSync,
+ realpathSync,
+ renameSync,
+ statSync,
+ symlinkSync,
+ truncateSync,
+ unlinkSync,
+ utimesSync,
+ lutimesSync,
+ createReadStream,
+ createWriteStream,
+};