2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
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,
|
2023-02-07 16:09:50 -05:00
|
|
|
} from "internal:deno_web/06_streams.js";
|
2023-02-07 14:22:46 -05:00
|
|
|
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),
|
2022-08-19 06:24:40 -04:00
|
|
|
options,
|
2023-02-07 14:22:46 -05:00
|
|
|
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),
|
2022-08-19 06:24:40 -04:00
|
|
|
options,
|
2023-02-07 14:22:46 -05:00
|
|
|
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;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
get rid() {
|
|
|
|
return this.#rid;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
write(p) {
|
|
|
|
return write(this.rid, p);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
writeSync(p) {
|
|
|
|
return writeSync(this.rid, p);
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
truncate(len) {
|
|
|
|
return ftruncate(this.rid, len);
|
|
|
|
}
|
2021-04-12 08:32:58 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
truncateSync(len) {
|
|
|
|
return ftruncateSync(this.rid, len);
|
|
|
|
}
|
2021-04-12 08:32:58 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
read(p) {
|
|
|
|
return read(this.rid, p);
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
readSync(p) {
|
|
|
|
return readSync(this.rid, p);
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
seek(offset, whence) {
|
|
|
|
return seek(this.rid, offset, whence);
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
seekSync(offset, whence) {
|
|
|
|
return seekSync(this.rid, offset, whence);
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
stat() {
|
|
|
|
return fstat(this.rid);
|
|
|
|
}
|
2021-04-12 07:33:05 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
statSync() {
|
|
|
|
return fstatSync(this.rid);
|
|
|
|
}
|
2021-04-12 07:33:05 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
close() {
|
|
|
|
core.close(this.rid);
|
|
|
|
}
|
2022-02-15 07:35:22 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
get readable() {
|
|
|
|
if (this.#readable === undefined) {
|
|
|
|
this.#readable = readableStreamForRid(this.rid);
|
2022-02-15 07:35:22 -05:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
return this.#readable;
|
|
|
|
}
|
2022-02-15 07:35:22 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
get writable() {
|
|
|
|
if (this.#writable === undefined) {
|
|
|
|
this.#writable = writableStreamForRid(this.rid);
|
2022-02-15 07:35:22 -05:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
return this.#writable;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
class Stdin {
|
|
|
|
#readable;
|
2022-02-15 07:35:22 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
constructor() {
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
get rid() {
|
|
|
|
return 0;
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
read(p) {
|
|
|
|
return read(this.rid, p);
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
readSync(p) {
|
|
|
|
return readSync(this.rid, p);
|
|
|
|
}
|
2022-02-15 07:35:22 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
close() {
|
|
|
|
core.close(this.rid);
|
|
|
|
}
|
2022-09-28 09:03:56 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
get readable() {
|
|
|
|
if (this.#readable === undefined) {
|
|
|
|
this.#readable = readableStreamForRid(this.rid);
|
2022-09-28 09:03:56 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
return this.#readable;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
setRaw(mode, options = {}) {
|
|
|
|
const cbreak = !!(options.cbreak ?? false);
|
|
|
|
ops.op_stdin_set_raw(mode, cbreak);
|
|
|
|
}
|
|
|
|
}
|
2020-10-20 07:20:17 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
class Stdout {
|
|
|
|
#writable;
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
constructor() {
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
get rid() {
|
|
|
|
return 1;
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
write(p) {
|
|
|
|
return write(this.rid, p);
|
|
|
|
}
|
2022-02-15 07:35:22 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
writeSync(p) {
|
|
|
|
return writeSync(this.rid, p);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
close() {
|
|
|
|
core.close(this.rid);
|
|
|
|
}
|
2022-02-15 07:35:22 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
get writable() {
|
|
|
|
if (this.#writable === undefined) {
|
|
|
|
this.#writable = writableStreamForRid(this.rid);
|
2020-10-20 07:20:17 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
return this.#writable;
|
|
|
|
}
|
|
|
|
}
|
2020-10-20 07:20:17 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
class Stderr {
|
|
|
|
#writable;
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
constructor() {
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
get rid() {
|
|
|
|
return 2;
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
write(p) {
|
|
|
|
return write(this.rid, p);
|
|
|
|
}
|
2022-02-15 07:35:22 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
writeSync(p) {
|
|
|
|
return writeSync(this.rid, p);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
close() {
|
|
|
|
core.close(this.rid);
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
get writable() {
|
|
|
|
if (this.#writable === undefined) {
|
|
|
|
this.#writable = writableStreamForRid(this.rid);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
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");
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (options.truncate && !options.write) {
|
|
|
|
throw new Error("'truncate' option requires 'write' option");
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const createOrCreateNewWithoutWriteOrAppend =
|
|
|
|
(options.create || options.createNew) &&
|
|
|
|
!(options.write || options.append);
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (createOrCreateNewWithoutWriteOrAppend) {
|
|
|
|
throw new Error(
|
|
|
|
"'create' or 'createNew' options require 'write' or 'append' option",
|
|
|
|
);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
const File = FsFile;
|
|
|
|
export {
|
|
|
|
create,
|
|
|
|
createSync,
|
|
|
|
File,
|
|
|
|
FsFile,
|
|
|
|
open,
|
|
|
|
openSync,
|
|
|
|
seek,
|
|
|
|
seekSync,
|
|
|
|
stderr,
|
|
|
|
stdin,
|
|
|
|
stdout,
|
|
|
|
};
|