diff --git a/src-bindings/node/node.ml b/src-bindings/node/node.ml index 5690298b8..c1dadf116 100644 --- a/src-bindings/node/node.ml +++ b/src-bindings/node/node.ml @@ -148,23 +148,47 @@ module Path = struct ;; end +module Constants = struct + include [%js: val r_ok : Ojs.t [@@js.global "constants.R_OK"]] + + type t = R_OK + + let t_to_js = function + | R_OK -> r_ok + ;; +end + module Fs = struct + module ReadFileOptions = struct + include Interface.Make () + include [%js: val create : encoding:string -> t [@@js.builder]] + end + include [%js: - val readDir : string -> string list Promise.t [@@js.global "fs.readDir"] + val access : string -> mode:Constants.t -> unit Promise.t [@@js.global "fs.access"] + val readdir : string -> string list Promise.t [@@js.global "fs.readdir"] - val readFile : string -> encoding:string -> string Promise.t - [@@js.global "fs.readFile"] + val readFile : string -> options:ReadFileOptions.t -> string Promise.t + [@@js.global "fs.readFile"]] - val exists : string -> bool Promise.t [@@js.global "fs.exists"]] + let exists path = + access path ~mode:Constants.R_OK + |> Promise.then_ + ~fulfilled:(fun _ -> Promise.return true) + ~rejected:(fun _ -> Promise.return false) + ;; let readDir path = - readDir path + readdir path |> Promise.then_ ~fulfilled:Promise.Result.return ~rejected:(fun error -> Promise.return (Error (JsError.message error))) ;; - let readFile = readFile ~encoding:"utf8" + let readFile = + let options = ReadFileOptions.create ~encoding:"utf8" in + readFile ~options + ;; end module Net = struct diff --git a/src-bindings/node/node.mli b/src-bindings/node/node.mli index dcb99ef57..78bcdc22c 100644 --- a/src-bindings/node/node.mli +++ b/src-bindings/node/node.mli @@ -105,6 +105,12 @@ module Os : sig val homedir : unit -> string end +module Constants : sig + type t = R_OK + + val t_to_js : t -> Ojs.t +end + module Fs : sig val readDir : string -> (string list, string) result Promise.t val readFile : string -> string Promise.t diff --git a/src-bindings/node/node_stub.js b/src-bindings/node/node_stub.js index b90ed98c9..ba19d8382 100644 --- a/src-bindings/node/node_stub.js +++ b/src-bindings/node/node_stub.js @@ -1,11 +1,6 @@ -const fs = require("node:fs"); -const { promisify } = require("node:util"); +joo_global_object.constants = require("node:constants"); -joo_global_object.fs = { - readDir: promisify(fs.readdir), - readFile: promisify(fs.readFile), - exists: promisify(fs.exists), -}; +joo_global_object.fs = require("node:fs/promises"); joo_global_object.child_process = require("node:child_process");