summaryrefslogtreecommitdiff
path: root/runtime/js
diff options
context:
space:
mode:
Diffstat (limited to 'runtime/js')
-rw-r--r--runtime/js/30_fs.js346
-rw-r--r--runtime/js/40_files.js299
-rw-r--r--runtime/js/40_process.js2
-rw-r--r--runtime/js/40_read_file.js70
-rw-r--r--runtime/js/40_write_file.js100
-rw-r--r--runtime/js/41_prompt.js2
-rw-r--r--runtime/js/90_deno_ns.js41
7 files changed, 365 insertions, 495 deletions
diff --git a/runtime/js/30_fs.js b/runtime/js/30_fs.js
index eaf83b543..a7936085e 100644
--- a/runtime/js/30_fs.js
+++ b/runtime/js/30_fs.js
@@ -4,16 +4,26 @@ const core = globalThis.Deno.core;
const ops = core.ops;
const primordials = globalThis.__bootstrap.primordials;
const {
+ ArrayPrototypeFilter,
Date,
DatePrototype,
+ Error,
+ Function,
MathTrunc,
+ ObjectEntries,
ObjectPrototypeIsPrototypeOf,
+ ObjectValues,
SymbolAsyncIterator,
SymbolIterator,
- Function,
- ObjectEntries,
Uint32Array,
} = primordials;
+import { read, readSync, write, writeSync } from "internal:deno_io/12_io.js";
+import * as abortSignal from "internal:deno_web/03_abort_signal.js";
+import {
+ readableStreamForRid,
+ ReadableStreamPrototype,
+ writableStreamForRid,
+} from "internal:deno_web/06_streams.js";
import { pathFromURL } from "internal:runtime/06_util.js";
import { build } from "internal:runtime/01_build.js";
@@ -503,6 +513,322 @@ async function funlock(rid) {
await core.opAsync("op_funlock_async", rid);
}
+function seekSync(
+ rid,
+ offset,
+ whence,
+) {
+ return ops.op_seek_sync({ rid, offset, whence });
+}
+
+function seek(
+ rid,
+ offset,
+ whence,
+) {
+ return core.opAsync("op_seek_async", { rid, offset, whence });
+}
+
+function openSync(
+ path,
+ options,
+) {
+ if (options) checkOpenOptions(options);
+ const mode = options?.mode;
+ const rid = ops.op_open_sync(
+ pathFromURL(path),
+ options,
+ mode,
+ );
+
+ return new FsFile(rid);
+}
+
+async function open(
+ path,
+ options,
+) {
+ if (options) checkOpenOptions(options);
+ const mode = options?.mode;
+ const rid = await core.opAsync(
+ "op_open_async",
+ pathFromURL(path),
+ options,
+ mode,
+ );
+
+ return new FsFile(rid);
+}
+
+function createSync(path) {
+ return openSync(path, {
+ read: true,
+ write: true,
+ truncate: true,
+ create: true,
+ });
+}
+
+function create(path) {
+ return open(path, {
+ read: true,
+ write: true,
+ truncate: true,
+ create: true,
+ });
+}
+
+class FsFile {
+ #rid = 0;
+
+ #readable;
+ #writable;
+
+ constructor(rid) {
+ this.#rid = rid;
+ }
+
+ get rid() {
+ return this.#rid;
+ }
+
+ write(p) {
+ return write(this.rid, p);
+ }
+
+ writeSync(p) {
+ return writeSync(this.rid, p);
+ }
+
+ truncate(len) {
+ return ftruncate(this.rid, len);
+ }
+
+ truncateSync(len) {
+ return ftruncateSync(this.rid, len);
+ }
+
+ read(p) {
+ return read(this.rid, p);
+ }
+
+ readSync(p) {
+ return readSync(this.rid, p);
+ }
+
+ seek(offset, whence) {
+ return seek(this.rid, offset, whence);
+ }
+
+ seekSync(offset, whence) {
+ return seekSync(this.rid, offset, whence);
+ }
+
+ stat() {
+ return fstat(this.rid);
+ }
+
+ statSync() {
+ return fstatSync(this.rid);
+ }
+
+ close() {
+ core.close(this.rid);
+ }
+
+ get readable() {
+ if (this.#readable === undefined) {
+ this.#readable = readableStreamForRid(this.rid);
+ }
+ return this.#readable;
+ }
+
+ get writable() {
+ if (this.#writable === undefined) {
+ this.#writable = writableStreamForRid(this.rid);
+ }
+ return this.#writable;
+ }
+}
+
+function checkOpenOptions(options) {
+ if (
+ ArrayPrototypeFilter(
+ ObjectValues(options),
+ (val) => val === true,
+ ).length === 0
+ ) {
+ throw new Error("OpenOptions requires at least one option to be true");
+ }
+
+ if (options.truncate && !options.write) {
+ throw new Error("'truncate' option requires 'write' option");
+ }
+
+ const createOrCreateNewWithoutWriteOrAppend =
+ (options.create || options.createNew) &&
+ !(options.write || options.append);
+
+ if (createOrCreateNewWithoutWriteOrAppend) {
+ throw new Error(
+ "'create' or 'createNew' options require 'write' or 'append' option",
+ );
+ }
+}
+
+const File = FsFile;
+
+function readFileSync(path) {
+ return ops.op_readfile_sync(pathFromURL(path));
+}
+
+async function readFile(path, options) {
+ let cancelRid;
+ let abortHandler;
+ if (options?.signal) {
+ options.signal.throwIfAborted();
+ cancelRid = ops.op_cancel_handle();
+ abortHandler = () => core.tryClose(cancelRid);
+ options.signal[abortSignal.add](abortHandler);
+ }
+
+ try {
+ const read = await core.opAsync(
+ "op_readfile_async",
+ pathFromURL(path),
+ cancelRid,
+ );
+ return read;
+ } finally {
+ if (options?.signal) {
+ options.signal[abortSignal.remove](abortHandler);
+
+ // always throw the abort error when aborted
+ options.signal.throwIfAborted();
+ }
+ }
+}
+
+function readTextFileSync(path) {
+ return ops.op_readfile_text_sync(pathFromURL(path));
+}
+
+async function readTextFile(path, options) {
+ let cancelRid;
+ let abortHandler;
+ if (options?.signal) {
+ options.signal.throwIfAborted();
+ cancelRid = ops.op_cancel_handle();
+ abortHandler = () => core.tryClose(cancelRid);
+ options.signal[abortSignal.add](abortHandler);
+ }
+
+ try {
+ const read = await core.opAsync(
+ "op_readfile_text_async",
+ pathFromURL(path),
+ cancelRid,
+ );
+ return read;
+ } finally {
+ if (options?.signal) {
+ options.signal[abortSignal.remove](abortHandler);
+
+ // always throw the abort error when aborted
+ options.signal.throwIfAborted();
+ }
+ }
+}
+
+function writeFileSync(
+ path,
+ data,
+ options = {},
+) {
+ options.signal?.throwIfAborted();
+ ops.op_write_file_sync(
+ pathFromURL(path),
+ options.mode,
+ options.append ?? false,
+ options.create ?? true,
+ options.createNew ?? false,
+ data,
+ );
+}
+
+async function writeFile(
+ path,
+ data,
+ options = {},
+) {
+ let cancelRid;
+ let abortHandler;
+ if (options.signal) {
+ options.signal.throwIfAborted();
+ cancelRid = ops.op_cancel_handle();
+ abortHandler = () => core.tryClose(cancelRid);
+ options.signal[abortSignal.add](abortHandler);
+ }
+ try {
+ if (ObjectPrototypeIsPrototypeOf(ReadableStreamPrototype, data)) {
+ const file = await open(path, {
+ mode: options.mode,
+ append: options.append ?? false,
+ create: options.create ?? true,
+ createNew: options.createNew ?? false,
+ write: true,
+ });
+ await data.pipeTo(file.writable, {
+ signal: options.signal,
+ });
+ } else {
+ await core.opAsync(
+ "op_write_file_async",
+ pathFromURL(path),
+ options.mode,
+ options.append ?? false,
+ options.create ?? true,
+ options.createNew ?? false,
+ data,
+ cancelRid,
+ );
+ }
+ } finally {
+ if (options.signal) {
+ options.signal[abortSignal.remove](abortHandler);
+
+ // always throw the abort error when aborted
+ options.signal.throwIfAborted();
+ }
+ }
+}
+
+function writeTextFileSync(
+ path,
+ data,
+ options = {},
+) {
+ const encoder = new TextEncoder();
+ return writeFileSync(path, encoder.encode(data), options);
+}
+
+function writeTextFile(
+ path,
+ data,
+ options = {},
+) {
+ if (ObjectPrototypeIsPrototypeOf(ReadableStreamPrototype, data)) {
+ return writeFile(
+ path,
+ data.pipeThrough(new TextEncoderStream()),
+ options,
+ );
+ } else {
+ const encoder = new TextEncoder();
+ return writeFile(path, encoder.encode(data), options);
+ }
+}
+
export {
chdir,
chmod,
@@ -511,11 +837,15 @@ export {
chownSync,
copyFile,
copyFileSync,
+ create,
+ createSync,
cwd,
fdatasync,
fdatasyncSync,
+ File,
flock,
flockSync,
+ FsFile,
fstat,
fstatSync,
fsync,
@@ -536,16 +866,24 @@ export {
makeTempFileSync,
mkdir,
mkdirSync,
+ open,
+ openSync,
readDir,
readDirSync,
+ readFile,
+ readFileSync,
readLink,
readLinkSync,
+ readTextFile,
+ readTextFileSync,
realPath,
realPathSync,
remove,
removeSync,
rename,
renameSync,
+ seek,
+ seekSync,
stat,
statSync,
symlink,
@@ -555,4 +893,8 @@ export {
umask,
utime,
utimeSync,
+ writeFile,
+ writeFileSync,
+ writeTextFile,
+ writeTextFileSync,
};
diff --git a/runtime/js/40_files.js b/runtime/js/40_files.js
deleted file mode 100644
index 5fc5e6c62..000000000
--- a/runtime/js/40_files.js
+++ /dev/null
@@ -1,299 +0,0 @@
-// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
-
-const core = globalThis.Deno.core;
-const ops = core.ops;
-import { read, readSync, write, writeSync } from "internal:deno_io/12_io.js";
-import {
- fstat,
- fstatSync,
- ftruncate,
- ftruncateSync,
-} from "internal:runtime/30_fs.js";
-import { pathFromURL } from "internal:runtime/06_util.js";
-import {
- readableStreamForRid,
- writableStreamForRid,
-} from "internal:deno_web/06_streams.js";
-const primordials = globalThis.__bootstrap.primordials;
-const {
- ArrayPrototypeFilter,
- Error,
- ObjectValues,
-} = primordials;
-
-function seekSync(
- rid,
- offset,
- whence,
-) {
- return ops.op_seek_sync({ rid, offset, whence });
-}
-
-function seek(
- rid,
- offset,
- whence,
-) {
- return core.opAsync("op_seek_async", { rid, offset, whence });
-}
-
-function openSync(
- path,
- options,
-) {
- if (options) checkOpenOptions(options);
- const mode = options?.mode;
- const rid = ops.op_open_sync(
- pathFromURL(path),
- options,
- mode,
- );
-
- return new FsFile(rid);
-}
-
-async function open(
- path,
- options,
-) {
- if (options) checkOpenOptions(options);
- const mode = options?.mode;
- const rid = await core.opAsync(
- "op_open_async",
- pathFromURL(path),
- options,
- mode,
- );
-
- return new FsFile(rid);
-}
-
-function createSync(path) {
- return openSync(path, {
- read: true,
- write: true,
- truncate: true,
- create: true,
- });
-}
-
-function create(path) {
- return open(path, {
- read: true,
- write: true,
- truncate: true,
- create: true,
- });
-}
-
-class FsFile {
- #rid = 0;
-
- #readable;
- #writable;
-
- constructor(rid) {
- this.#rid = rid;
- }
-
- get rid() {
- return this.#rid;
- }
-
- write(p) {
- return write(this.rid, p);
- }
-
- writeSync(p) {
- return writeSync(this.rid, p);
- }
-
- truncate(len) {
- return ftruncate(this.rid, len);
- }
-
- truncateSync(len) {
- return ftruncateSync(this.rid, len);
- }
-
- read(p) {
- return read(this.rid, p);
- }
-
- readSync(p) {
- return readSync(this.rid, p);
- }
-
- seek(offset, whence) {
- return seek(this.rid, offset, whence);
- }
-
- seekSync(offset, whence) {
- return seekSync(this.rid, offset, whence);
- }
-
- stat() {
- return fstat(this.rid);
- }
-
- statSync() {
- return fstatSync(this.rid);
- }
-
- close() {
- core.close(this.rid);
- }
-
- get readable() {
- if (this.#readable === undefined) {
- this.#readable = readableStreamForRid(this.rid);
- }
- return this.#readable;
- }
-
- get writable() {
- if (this.#writable === undefined) {
- this.#writable = writableStreamForRid(this.rid);
- }
- return this.#writable;
- }
-}
-
-class Stdin {
- #readable;
-
- constructor() {
- }
-
- get rid() {
- return 0;
- }
-
- read(p) {
- return read(this.rid, p);
- }
-
- readSync(p) {
- return readSync(this.rid, p);
- }
-
- close() {
- core.close(this.rid);
- }
-
- get readable() {
- if (this.#readable === undefined) {
- this.#readable = readableStreamForRid(this.rid);
- }
- return this.#readable;
- }
-
- setRaw(mode, options = {}) {
- const cbreak = !!(options.cbreak ?? false);
- ops.op_stdin_set_raw(mode, cbreak);
- }
-}
-
-class Stdout {
- #writable;
-
- constructor() {
- }
-
- get rid() {
- return 1;
- }
-
- write(p) {
- return write(this.rid, p);
- }
-
- writeSync(p) {
- return writeSync(this.rid, p);
- }
-
- close() {
- core.close(this.rid);
- }
-
- get writable() {
- if (this.#writable === undefined) {
- this.#writable = writableStreamForRid(this.rid);
- }
- return this.#writable;
- }
-}
-
-class Stderr {
- #writable;
-
- constructor() {
- }
-
- get rid() {
- return 2;
- }
-
- write(p) {
- return write(this.rid, p);
- }
-
- writeSync(p) {
- return writeSync(this.rid, p);
- }
-
- close() {
- core.close(this.rid);
- }
-
- get writable() {
- if (this.#writable === undefined) {
- this.#writable = writableStreamForRid(this.rid);
- }
- return this.#writable;
- }
-}
-
-const stdin = new Stdin();
-const stdout = new Stdout();
-const stderr = new Stderr();
-
-function checkOpenOptions(options) {
- if (
- ArrayPrototypeFilter(
- ObjectValues(options),
- (val) => val === true,
- ).length === 0
- ) {
- throw new Error("OpenOptions requires at least one option to be true");
- }
-
- if (options.truncate && !options.write) {
- throw new Error("'truncate' option requires 'write' option");
- }
-
- const createOrCreateNewWithoutWriteOrAppend =
- (options.create || options.createNew) &&
- !(options.write || options.append);
-
- if (createOrCreateNewWithoutWriteOrAppend) {
- throw new Error(
- "'create' or 'createNew' options require 'write' or 'append' option",
- );
- }
-}
-
-const File = FsFile;
-export {
- create,
- createSync,
- File,
- FsFile,
- open,
- openSync,
- seek,
- seekSync,
- stderr,
- stdin,
- stdout,
-};
diff --git a/runtime/js/40_process.js b/runtime/js/40_process.js
index 0854f402a..bb224ae9c 100644
--- a/runtime/js/40_process.js
+++ b/runtime/js/40_process.js
@@ -2,7 +2,7 @@
const core = globalThis.Deno.core;
const ops = core.ops;
-import { FsFile } from "internal:runtime/40_files.js";
+import { FsFile } from "internal:runtime/30_fs.js";
import { readAll } from "internal:deno_io/12_io.js";
import { pathFromURL } from "internal:runtime/06_util.js";
import { assert } from "internal:deno_web/00_infra.js";
diff --git a/runtime/js/40_read_file.js b/runtime/js/40_read_file.js
deleted file mode 100644
index dd4f6e67a..000000000
--- a/runtime/js/40_read_file.js
+++ /dev/null
@@ -1,70 +0,0 @@
-// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
-
-const core = globalThis.Deno.core;
-const ops = core.ops;
-import { pathFromURL } from "internal:runtime/06_util.js";
-import * as abortSignal from "internal:deno_web/03_abort_signal.js";
-
-function readFileSync(path) {
- return ops.op_readfile_sync(pathFromURL(path));
-}
-
-async function readFile(path, options) {
- let cancelRid;
- let abortHandler;
- if (options?.signal) {
- options.signal.throwIfAborted();
- cancelRid = ops.op_cancel_handle();
- abortHandler = () => core.tryClose(cancelRid);
- options.signal[abortSignal.add](abortHandler);
- }
-
- try {
- const read = await core.opAsync(
- "op_readfile_async",
- pathFromURL(path),
- cancelRid,
- );
- return read;
- } finally {
- if (options?.signal) {
- options.signal[abortSignal.remove](abortHandler);
-
- // always throw the abort error when aborted
- options.signal.throwIfAborted();
- }
- }
-}
-
-function readTextFileSync(path) {
- return ops.op_readfile_text_sync(pathFromURL(path));
-}
-
-async function readTextFile(path, options) {
- let cancelRid;
- let abortHandler;
- if (options?.signal) {
- options.signal.throwIfAborted();
- cancelRid = ops.op_cancel_handle();
- abortHandler = () => core.tryClose(cancelRid);
- options.signal[abortSignal.add](abortHandler);
- }
-
- try {
- const read = await core.opAsync(
- "op_readfile_text_async",
- pathFromURL(path),
- cancelRid,
- );
- return read;
- } finally {
- if (options?.signal) {
- options.signal[abortSignal.remove](abortHandler);
-
- // always throw the abort error when aborted
- options.signal.throwIfAborted();
- }
- }
-}
-
-export { readFile, readFileSync, readTextFile, readTextFileSync };
diff --git a/runtime/js/40_write_file.js b/runtime/js/40_write_file.js
deleted file mode 100644
index 89d670a65..000000000
--- a/runtime/js/40_write_file.js
+++ /dev/null
@@ -1,100 +0,0 @@
-// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
-const core = globalThis.Deno.core;
-const ops = core.ops;
-const primordials = globalThis.__bootstrap.primordials;
-import * as abortSignal from "internal:deno_web/03_abort_signal.js";
-import { pathFromURL } from "internal:runtime/06_util.js";
-import { open } from "internal:runtime/40_files.js";
-import { ReadableStreamPrototype } from "internal:deno_web/06_streams.js";
-const { ObjectPrototypeIsPrototypeOf } = primordials;
-
-function writeFileSync(
- path,
- data,
- options = {},
-) {
- options.signal?.throwIfAborted();
- ops.op_write_file_sync(
- pathFromURL(path),
- options.mode,
- options.append ?? false,
- options.create ?? true,
- options.createNew ?? false,
- data,
- );
-}
-
-async function writeFile(
- path,
- data,
- options = {},
-) {
- let cancelRid;
- let abortHandler;
- if (options.signal) {
- options.signal.throwIfAborted();
- cancelRid = ops.op_cancel_handle();
- abortHandler = () => core.tryClose(cancelRid);
- options.signal[abortSignal.add](abortHandler);
- }
- try {
- if (ObjectPrototypeIsPrototypeOf(ReadableStreamPrototype, data)) {
- const file = await open(path, {
- mode: options.mode,
- append: options.append ?? false,
- create: options.create ?? true,
- createNew: options.createNew ?? false,
- write: true,
- });
- await data.pipeTo(file.writable, {
- signal: options.signal,
- });
- } else {
- await core.opAsync(
- "op_write_file_async",
- pathFromURL(path),
- options.mode,
- options.append ?? false,
- options.create ?? true,
- options.createNew ?? false,
- data,
- cancelRid,
- );
- }
- } finally {
- if (options.signal) {
- options.signal[abortSignal.remove](abortHandler);
-
- // always throw the abort error when aborted
- options.signal.throwIfAborted();
- }
- }
-}
-
-function writeTextFileSync(
- path,
- data,
- options = {},
-) {
- const encoder = new TextEncoder();
- return writeFileSync(path, encoder.encode(data), options);
-}
-
-function writeTextFile(
- path,
- data,
- options = {},
-) {
- if (ObjectPrototypeIsPrototypeOf(ReadableStreamPrototype, data)) {
- return writeFile(
- path,
- data.pipeThrough(new TextEncoderStream()),
- options,
- );
- } else {
- const encoder = new TextEncoder();
- return writeFile(path, encoder.encode(data), options);
- }
-}
-
-export { writeFile, writeFileSync, writeTextFile, writeTextFileSync };
diff --git a/runtime/js/41_prompt.js b/runtime/js/41_prompt.js
index 9fca3bc8f..66598f755 100644
--- a/runtime/js/41_prompt.js
+++ b/runtime/js/41_prompt.js
@@ -2,7 +2,7 @@
const core = globalThis.Deno.core;
const primordials = globalThis.__bootstrap.primordials;
import { isatty } from "internal:runtime/40_tty.js";
-import { stdin } from "internal:runtime/40_files.js";
+import { stdin } from "internal:deno_io/12_io.js";
const { ArrayPrototypePush, StringPrototypeCharCodeAt, Uint8Array } =
primordials;
const LF = StringPrototypeCharCodeAt("\n", 0);
diff --git a/runtime/js/90_deno_ns.js b/runtime/js/90_deno_ns.js
index a76eb5bde..39a1def90 100644
--- a/runtime/js/90_deno_ns.js
+++ b/runtime/js/90_deno_ns.js
@@ -18,13 +18,10 @@ import * as io from "internal:deno_io/12_io.js";
import * as buffer from "internal:runtime/13_buffer.js";
import * as fs from "internal:runtime/30_fs.js";
import * as os from "internal:runtime/30_os.js";
-import * as files from "internal:runtime/40_files.js";
import * as fsEvents from "internal:runtime/40_fs_events.js";
import * as process from "internal:runtime/40_process.js";
-import * as readFile from "internal:runtime/40_read_file.js";
import * as signals from "internal:runtime/40_signals.js";
import * as tty from "internal:runtime/40_tty.js";
-import * as writeFile from "internal:runtime/40_write_file.js";
import * as spawn from "internal:runtime/40_spawn.js";
// TODO(bartlomieju): this is funky we have two `http` imports
import * as httpRuntime from "internal:runtime/40_http.js";
@@ -34,14 +31,14 @@ const denoNs = {
Process: process.Process,
run: process.run,
isatty: tty.isatty,
- writeFileSync: writeFile.writeFileSync,
- writeFile: writeFile.writeFile,
- writeTextFileSync: writeFile.writeTextFileSync,
- writeTextFile: writeFile.writeTextFile,
- readTextFile: readFile.readTextFile,
- readTextFileSync: readFile.readTextFileSync,
- readFile: readFile.readFile,
- readFileSync: readFile.readFileSync,
+ writeFileSync: fs.writeFileSync,
+ writeFile: fs.writeFile,
+ writeTextFileSync: fs.writeTextFileSync,
+ writeTextFile: fs.writeTextFile,
+ readTextFile: fs.readTextFile,
+ readTextFileSync: fs.readTextFileSync,
+ readFile: fs.readFile,
+ readFileSync: fs.readFileSync,
watchFs: fsEvents.watchFs,
chmodSync: fs.chmodSync,
chmod: fs.chmod,
@@ -101,17 +98,17 @@ const denoNs = {
readSync: io.readSync,
write: io.write,
writeSync: io.writeSync,
- File: files.File,
- FsFile: files.FsFile,
- open: files.open,
- openSync: files.openSync,
- create: files.create,
- createSync: files.createSync,
- stdin: files.stdin,
- stdout: files.stdout,
- stderr: files.stderr,
- seek: files.seek,
- seekSync: files.seekSync,
+ File: fs.File,
+ FsFile: fs.FsFile,
+ open: fs.open,
+ openSync: fs.openSync,
+ create: fs.create,
+ createSync: fs.createSync,
+ stdin: io.stdin,
+ stdout: io.stdout,
+ stderr: io.stderr,
+ seek: fs.seek,
+ seekSync: fs.seekSync,
connect: net.connect,
listen: net.listen,
loadavg: os.loadavg,