2022-01-07 22:09:52 -05:00
|
|
|
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
2021-02-04 17:18:32 -05:00
|
|
|
"use strict";
|
2020-07-19 13:49:44 -04:00
|
|
|
|
|
|
|
((window) => {
|
2020-09-16 16:22:43 -04:00
|
|
|
const core = window.Deno.core;
|
2022-08-11 09:56:56 -04:00
|
|
|
const ops = core.ops;
|
2020-07-19 13:49:44 -04:00
|
|
|
const { read, readSync, write, writeSync } = window.__bootstrap.io;
|
2021-04-12 08:32:58 -04:00
|
|
|
const { ftruncate, ftruncateSync, fstat, fstatSync } = window.__bootstrap.fs;
|
2020-07-19 13:49:44 -04:00
|
|
|
const { pathFromURL } = window.__bootstrap.util;
|
2022-04-22 06:49:08 -04:00
|
|
|
const { writableStreamForRid } = window.__bootstrap.streamUtils;
|
|
|
|
const { readableStreamForRid } = window.__bootstrap.streams;
|
2021-07-03 18:17:52 -04:00
|
|
|
const {
|
2022-02-15 07:35:22 -05:00
|
|
|
ArrayPrototypeFilter,
|
2021-07-03 18:17:52 -04:00
|
|
|
Error,
|
|
|
|
ObjectValues,
|
|
|
|
} = window.__bootstrap.primordials;
|
2020-07-19 13:49:44 -04:00
|
|
|
|
|
|
|
function seekSync(
|
|
|
|
rid,
|
|
|
|
offset,
|
|
|
|
whence,
|
|
|
|
) {
|
2022-08-11 09:56:56 -04:00
|
|
|
return ops.op_seek_sync({ rid, offset, whence });
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function seek(
|
|
|
|
rid,
|
|
|
|
offset,
|
|
|
|
whence,
|
|
|
|
) {
|
2021-04-12 15:55:05 -04:00
|
|
|
return core.opAsync("op_seek_async", { rid, offset, whence });
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function openSync(
|
|
|
|
path,
|
2022-08-19 06:24:40 -04:00
|
|
|
options,
|
2020-07-19 13:49:44 -04:00
|
|
|
) {
|
2022-08-19 06:24:40 -04:00
|
|
|
if (options) checkOpenOptions(options);
|
2020-08-14 13:43:11 -04:00
|
|
|
const mode = options?.mode;
|
2022-08-11 09:56:56 -04:00
|
|
|
const rid = ops.op_open_sync(
|
2022-08-19 06:24:40 -04:00
|
|
|
pathFromURL(path),
|
|
|
|
options,
|
|
|
|
mode,
|
2020-08-18 08:07:57 -04:00
|
|
|
);
|
2020-08-14 13:43:11 -04:00
|
|
|
|
2022-02-15 07:59:04 -05:00
|
|
|
return new FsFile(rid);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async function open(
|
|
|
|
path,
|
2022-08-19 06:24:40 -04:00
|
|
|
options,
|
2020-07-19 13:49:44 -04:00
|
|
|
) {
|
2022-08-19 06:24:40 -04:00
|
|
|
if (options) checkOpenOptions(options);
|
2020-08-14 13:43:11 -04:00
|
|
|
const mode = options?.mode;
|
2021-04-12 15:55:05 -04:00
|
|
|
const rid = await core.opAsync(
|
2020-08-18 08:07:57 -04:00
|
|
|
"op_open_async",
|
2022-08-19 06:24:40 -04:00
|
|
|
pathFromURL(path),
|
|
|
|
options,
|
|
|
|
mode,
|
2020-08-14 13:43:11 -04:00
|
|
|
);
|
|
|
|
|
2022-02-15 07:59:04 -05:00
|
|
|
return new FsFile(rid);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-02-15 07:59:04 -05:00
|
|
|
class FsFile {
|
2020-07-19 13:49:44 -04:00
|
|
|
#rid = 0;
|
|
|
|
|
2022-02-15 07:35:22 -05:00
|
|
|
#readable;
|
|
|
|
#writable;
|
|
|
|
|
2020-07-19 13:49:44 -04:00
|
|
|
constructor(rid) {
|
|
|
|
this.#rid = rid;
|
|
|
|
}
|
|
|
|
|
|
|
|
get rid() {
|
|
|
|
return this.#rid;
|
|
|
|
}
|
|
|
|
|
|
|
|
write(p) {
|
|
|
|
return write(this.rid, p);
|
|
|
|
}
|
|
|
|
|
|
|
|
writeSync(p) {
|
|
|
|
return writeSync(this.rid, p);
|
|
|
|
}
|
|
|
|
|
2021-04-12 08:32:58 -04:00
|
|
|
truncate(len) {
|
|
|
|
return ftruncate(this.rid, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
truncateSync(len) {
|
|
|
|
return ftruncateSync(this.rid, len);
|
|
|
|
}
|
|
|
|
|
2020-07-19 13:49:44 -04:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2021-04-12 07:33:05 -04:00
|
|
|
stat() {
|
|
|
|
return fstat(this.rid);
|
|
|
|
}
|
|
|
|
|
|
|
|
statSync() {
|
|
|
|
return fstatSync(this.rid);
|
|
|
|
}
|
|
|
|
|
2020-07-19 13:49:44 -04:00
|
|
|
close() {
|
2020-09-17 12:09:50 -04:00
|
|
|
core.close(this.rid);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2022-02-15 07:35:22 -05:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
class Stdin {
|
2022-02-15 07:35:22 -05:00
|
|
|
#readable;
|
|
|
|
|
2020-07-19 13:49:44 -04:00
|
|
|
constructor() {
|
2020-10-20 07:20:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
get rid() {
|
|
|
|
return 0;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
read(p) {
|
|
|
|
return read(this.rid, p);
|
|
|
|
}
|
|
|
|
|
|
|
|
readSync(p) {
|
|
|
|
return readSync(this.rid, p);
|
|
|
|
}
|
|
|
|
|
|
|
|
close() {
|
2020-09-17 12:09:50 -04:00
|
|
|
core.close(this.rid);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2022-02-15 07:35:22 -05:00
|
|
|
|
|
|
|
get readable() {
|
|
|
|
if (this.#readable === undefined) {
|
|
|
|
this.#readable = readableStreamForRid(this.rid);
|
|
|
|
}
|
|
|
|
return this.#readable;
|
|
|
|
}
|
2022-09-28 09:03:56 -04:00
|
|
|
|
|
|
|
setRaw(mode, options = {}) {
|
|
|
|
const cbreak = !!(options.cbreak ?? false);
|
|
|
|
ops.op_stdin_set_raw(mode, cbreak);
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
class Stdout {
|
2022-02-15 07:35:22 -05:00
|
|
|
#writable;
|
|
|
|
|
2020-07-19 13:49:44 -04:00
|
|
|
constructor() {
|
2020-10-20 07:20:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
get rid() {
|
|
|
|
return 1;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
write(p) {
|
|
|
|
return write(this.rid, p);
|
|
|
|
}
|
|
|
|
|
|
|
|
writeSync(p) {
|
|
|
|
return writeSync(this.rid, p);
|
|
|
|
}
|
|
|
|
|
|
|
|
close() {
|
2020-09-17 12:09:50 -04:00
|
|
|
core.close(this.rid);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2022-02-15 07:35:22 -05:00
|
|
|
|
|
|
|
get writable() {
|
|
|
|
if (this.#writable === undefined) {
|
|
|
|
this.#writable = writableStreamForRid(this.rid);
|
|
|
|
}
|
|
|
|
return this.#writable;
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
class Stderr {
|
2022-02-15 07:35:22 -05:00
|
|
|
#writable;
|
|
|
|
|
2020-07-19 13:49:44 -04:00
|
|
|
constructor() {
|
2020-10-20 07:20:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
get rid() {
|
|
|
|
return 2;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
write(p) {
|
|
|
|
return write(this.rid, p);
|
|
|
|
}
|
|
|
|
|
|
|
|
writeSync(p) {
|
|
|
|
return writeSync(this.rid, p);
|
|
|
|
}
|
|
|
|
|
|
|
|
close() {
|
2020-09-17 12:09:50 -04:00
|
|
|
core.close(this.rid);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2022-02-15 07:35:22 -05:00
|
|
|
|
|
|
|
get writable() {
|
|
|
|
if (this.#writable === undefined) {
|
|
|
|
this.#writable = writableStreamForRid(this.rid);
|
|
|
|
}
|
|
|
|
return this.#writable;
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const stdin = new Stdin();
|
|
|
|
const stdout = new Stdout();
|
|
|
|
const stderr = new Stderr();
|
|
|
|
|
|
|
|
function checkOpenOptions(options) {
|
2021-07-03 18:17:52 -04:00
|
|
|
if (
|
|
|
|
ArrayPrototypeFilter(
|
|
|
|
ObjectValues(options),
|
|
|
|
(val) => val === true,
|
|
|
|
).length === 0
|
|
|
|
) {
|
2020-07-19 13:49:44 -04:00
|
|
|
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",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
window.__bootstrap.files = {
|
|
|
|
stdin,
|
|
|
|
stdout,
|
|
|
|
stderr,
|
2022-02-15 07:59:04 -05:00
|
|
|
File: FsFile,
|
|
|
|
FsFile,
|
2020-07-19 13:49:44 -04:00
|
|
|
create,
|
|
|
|
createSync,
|
|
|
|
open,
|
|
|
|
openSync,
|
|
|
|
seek,
|
|
|
|
seekSync,
|
|
|
|
};
|
|
|
|
})(this);
|