summaryrefslogtreecommitdiff
path: root/runtime/js/40_files.js
diff options
context:
space:
mode:
Diffstat (limited to 'runtime/js/40_files.js')
-rw-r--r--runtime/js/40_files.js480
1 files changed, 243 insertions, 237 deletions
diff --git a/runtime/js/40_files.js b/runtime/js/40_files.js
index 4471610cc..98c569a09 100644
--- a/runtime/js/40_files.js
+++ b/runtime/js/40_files.js
@@ -1,293 +1,299 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
-"use strict";
-
-((window) => {
- const core = window.Deno.core;
- const ops = core.ops;
- const { read, readSync, write, writeSync } = window.__bootstrap.io;
- const { ftruncate, ftruncateSync, fstat, fstatSync } = window.__bootstrap.fs;
- const { pathFromURL } = window.__bootstrap.util;
- const { readableStreamForRid, writableStreamForRid } =
- window.__bootstrap.streams;
- const {
- ArrayPrototypeFilter,
- Error,
- ObjectValues,
- } = window.__bootstrap.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,
+const core = globalThis.Deno.core;
+const ops = core.ops;
+import { read, readSync, write, writeSync } from "internal:runtime/js/12_io.js";
+import {
+ fstat,
+ fstatSync,
+ ftruncate,
+ ftruncateSync,
+} from "internal:runtime/js/30_fs.js";
+import { pathFromURL } from "internal:runtime/js/06_util.js";
+import {
+ readableStreamForRid,
+ writableStreamForRid,
+} from "internal:ext/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,
- ) {
- 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,
+ 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,
- ) {
- if (options) checkOpenOptions(options);
- const mode = options?.mode;
- const rid = await core.opAsync(
- "op_open_async",
- pathFromURL(path),
- options,
- mode,
- );
-
- return new FsFile(rid);
+ 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;
}
- function createSync(path) {
- return openSync(path, {
- read: true,
- write: true,
- truncate: true,
- create: true,
- });
+ get rid() {
+ return this.#rid;
}
- function create(path) {
- return open(path, {
- read: true,
- write: true,
- truncate: true,
- create: true,
- });
+ write(p) {
+ return write(this.rid, p);
}
- 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);
- }
+ writeSync(p) {
+ return writeSync(this.rid, p);
+ }
- truncate(len) {
- return ftruncate(this.rid, len);
- }
+ truncate(len) {
+ return ftruncate(this.rid, len);
+ }
- truncateSync(len) {
- return ftruncateSync(this.rid, len);
- }
+ truncateSync(len) {
+ return ftruncateSync(this.rid, len);
+ }
- read(p) {
- return read(this.rid, p);
- }
+ read(p) {
+ return read(this.rid, p);
+ }
- readSync(p) {
- return readSync(this.rid, p);
- }
+ readSync(p) {
+ return readSync(this.rid, p);
+ }
- seek(offset, whence) {
- return seek(this.rid, offset, whence);
- }
+ seek(offset, whence) {
+ return seek(this.rid, offset, whence);
+ }
- seekSync(offset, whence) {
- return seekSync(this.rid, offset, whence);
- }
+ seekSync(offset, whence) {
+ return seekSync(this.rid, offset, whence);
+ }
- stat() {
- return fstat(this.rid);
- }
+ stat() {
+ return fstat(this.rid);
+ }
- statSync() {
- return fstatSync(this.rid);
- }
+ statSync() {
+ return fstatSync(this.rid);
+ }
- close() {
- core.close(this.rid);
- }
+ close() {
+ core.close(this.rid);
+ }
- get readable() {
- if (this.#readable === undefined) {
- this.#readable = readableStreamForRid(this.rid);
- }
- return this.#readable;
+ 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;
+ get writable() {
+ if (this.#writable === undefined) {
+ this.#writable = writableStreamForRid(this.rid);
}
+ return this.#writable;
}
+}
- class Stdin {
- #readable;
+class Stdin {
+ #readable;
- constructor() {
- }
-
- get rid() {
- return 0;
- }
+ constructor() {
+ }
- read(p) {
- return read(this.rid, p);
- }
+ get rid() {
+ return 0;
+ }
- readSync(p) {
- return readSync(this.rid, p);
- }
+ read(p) {
+ return read(this.rid, p);
+ }
- close() {
- core.close(this.rid);
- }
+ readSync(p) {
+ return readSync(this.rid, p);
+ }
- get readable() {
- if (this.#readable === undefined) {
- this.#readable = readableStreamForRid(this.rid);
- }
- return this.#readable;
- }
+ close() {
+ core.close(this.rid);
+ }
- setRaw(mode, options = {}) {
- const cbreak = !!(options.cbreak ?? false);
- ops.op_stdin_set_raw(mode, cbreak);
+ get readable() {
+ if (this.#readable === undefined) {
+ this.#readable = readableStreamForRid(this.rid);
}
+ return this.#readable;
}
- class Stdout {
- #writable;
-
- constructor() {
- }
+ setRaw(mode, options = {}) {
+ const cbreak = !!(options.cbreak ?? false);
+ ops.op_stdin_set_raw(mode, cbreak);
+ }
+}
- get rid() {
- return 1;
- }
+class Stdout {
+ #writable;
- write(p) {
- return write(this.rid, p);
- }
+ constructor() {
+ }
- writeSync(p) {
- return writeSync(this.rid, p);
- }
+ get rid() {
+ return 1;
+ }
- close() {
- core.close(this.rid);
- }
+ write(p) {
+ return write(this.rid, p);
+ }
- get writable() {
- if (this.#writable === undefined) {
- this.#writable = writableStreamForRid(this.rid);
- }
- return this.#writable;
- }
+ writeSync(p) {
+ return writeSync(this.rid, p);
}
- class Stderr {
- #writable;
+ close() {
+ core.close(this.rid);
+ }
- constructor() {
+ get writable() {
+ if (this.#writable === undefined) {
+ this.#writable = writableStreamForRid(this.rid);
}
+ return this.#writable;
+ }
+}
- get rid() {
- return 2;
- }
+class Stderr {
+ #writable;
- write(p) {
- return write(this.rid, p);
- }
+ constructor() {
+ }
- writeSync(p) {
- return writeSync(this.rid, p);
- }
+ get rid() {
+ return 2;
+ }
- close() {
- core.close(this.rid);
- }
+ write(p) {
+ return write(this.rid, p);
+ }
- get writable() {
- if (this.#writable === undefined) {
- this.#writable = writableStreamForRid(this.rid);
- }
- return this.#writable;
- }
+ writeSync(p) {
+ return writeSync(this.rid, p);
}
- const stdin = new Stdin();
- const stdout = new Stdout();
- const stderr = new Stderr();
+ close() {
+ core.close(this.rid);
+ }
- function checkOpenOptions(options) {
- if (
- ArrayPrototypeFilter(
- ObjectValues(options),
- (val) => val === true,
- ).length === 0
- ) {
- throw new Error("OpenOptions requires at least one option to be true");
+ 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");
- }
+ if (options.truncate && !options.write) {
+ throw new Error("'truncate' option requires 'write' option");
+ }
- const createOrCreateNewWithoutWriteOrAppend =
- (options.create || options.createNew) &&
- !(options.write || options.append);
+ const createOrCreateNewWithoutWriteOrAppend =
+ (options.create || options.createNew) &&
+ !(options.write || options.append);
- if (createOrCreateNewWithoutWriteOrAppend) {
- throw new Error(
- "'create' or 'createNew' options require 'write' or 'append' option",
- );
- }
+ if (createOrCreateNewWithoutWriteOrAppend) {
+ throw new Error(
+ "'create' or 'createNew' options require 'write' or 'append' option",
+ );
}
-
- window.__bootstrap.files = {
- stdin,
- stdout,
- stderr,
- File: FsFile,
- FsFile,
- create,
- createSync,
- open,
- openSync,
- seek,
- seekSync,
- };
-})(this);
+}
+
+const File = FsFile;
+export {
+ create,
+ createSync,
+ File,
+ FsFile,
+ open,
+ openSync,
+ seek,
+ seekSync,
+ stderr,
+ stdin,
+ stdout,
+};