diff options
author | Ryan Dahl <ry@tinyclouds.org> | 2020-07-23 10:27:26 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-07-23 16:27:26 +0200 |
commit | ca4dcb36dd5be0b14a2fafa059ea02ee7e0a0262 (patch) | |
tree | 393fc85f19e97bb66402676f67e63690d7a29e00 /cli/rt/30_files.js | |
parent | 090455936c892b6f2dfa425be9b1cdfb4c63af4a (diff) |
Rename cli/js2 to cli/rt (#6857)
Diffstat (limited to 'cli/rt/30_files.js')
-rw-r--r-- | cli/rt/30_files.js | 204 |
1 files changed, 204 insertions, 0 deletions
diff --git a/cli/rt/30_files.js b/cli/rt/30_files.js new file mode 100644 index 000000000..0b6d9c67f --- /dev/null +++ b/cli/rt/30_files.js @@ -0,0 +1,204 @@ +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + +((window) => { + const { close } = window.__bootstrap.resources; + const { read, readSync, write, writeSync } = window.__bootstrap.io; + const { sendSync, sendAsync } = window.__bootstrap.dispatchJson; + const { pathFromURL } = window.__bootstrap.util; + + function seekSync( + rid, + offset, + whence, + ) { + return sendSync("op_seek", { rid, offset, whence }); + } + + function seek( + rid, + offset, + whence, + ) { + return sendAsync("op_seek", { rid, offset, whence }); + } + + function opOpenSync(path, options) { + const mode = options?.mode; + return sendSync("op_open", { path: pathFromURL(path), options, mode }); + } + + function opOpen( + path, + options, + ) { + const mode = options?.mode; + return sendAsync("op_open", { path: pathFromURL(path), options, mode }); + } + + function openSync( + path, + options = { read: true }, + ) { + checkOpenOptions(options); + const rid = opOpenSync(path, options); + return new File(rid); + } + + async function open( + path, + options = { read: true }, + ) { + checkOpenOptions(options); + const rid = await opOpen(path, options); + return new File(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 File { + #rid = 0; + + constructor(rid) { + this.#rid = rid; + } + + get rid() { + return this.#rid; + } + + write(p) { + return write(this.rid, p); + } + + writeSync(p) { + return writeSync(this.rid, p); + } + + 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); + } + + close() { + close(this.rid); + } + } + + class Stdin { + constructor() { + this.rid = 0; + } + + read(p) { + return read(this.rid, p); + } + + readSync(p) { + return readSync(this.rid, p); + } + + close() { + close(this.rid); + } + } + + class Stdout { + constructor() { + this.rid = 1; + } + + write(p) { + return write(this.rid, p); + } + + writeSync(p) { + return writeSync(this.rid, p); + } + + close() { + close(this.rid); + } + } + + class Stderr { + constructor() { + this.rid = 2; + } + + write(p) { + return write(this.rid, p); + } + + writeSync(p) { + return writeSync(this.rid, p); + } + + close() { + close(this.rid); + } + } + + const stdin = new Stdin(); + const stdout = new Stdout(); + const stderr = new Stderr(); + + function checkOpenOptions(options) { + if (Object.values(options).filter((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", + ); + } + } + + window.__bootstrap.files = { + stdin, + stdout, + stderr, + File, + create, + createSync, + open, + openSync, + seek, + seekSync, + }; +})(this); |