summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cli/bench/deno_common.js5
-rw-r--r--cli/bench/http/deno_flash_send_file.js2
-rw-r--r--ext/web/06_streams.js15
-rw-r--r--runtime/js/40_files.js16
-rw-r--r--runtime/ops/fs.rs77
5 files changed, 81 insertions, 34 deletions
diff --git a/cli/bench/deno_common.js b/cli/bench/deno_common.js
index abafdc519..074346d28 100644
--- a/cli/bench/deno_common.js
+++ b/cli/bench/deno_common.js
@@ -23,6 +23,11 @@ Deno.bench("perf_now", { n: 5e5 }, () => {
performance.now();
});
+Deno.bench("open_file_sync", () => {
+ const file = Deno.openSync("./cli/bench/testdata/128k.bin");
+ file.close();
+});
+
// A common "language feature", that should be fast
// also a decent representation of a non-trivial JSON-op
{
diff --git a/cli/bench/http/deno_flash_send_file.js b/cli/bench/http/deno_flash_send_file.js
index 261f5a207..db2ad7a82 100644
--- a/cli/bench/http/deno_flash_send_file.js
+++ b/cli/bench/http/deno_flash_send_file.js
@@ -7,7 +7,7 @@ const { serve } = Deno;
const path = new URL("../testdata/128k.bin", import.meta.url).pathname;
function handler() {
- const file = Deno.openSync(path, { read: true });
+ const file = Deno.openSync(path);
return new Response(file.readable);
}
diff --git a/ext/web/06_streams.js b/ext/web/06_streams.js
index 1b753572b..bd1714964 100644
--- a/ext/web/06_streams.js
+++ b/ext/web/06_streams.js
@@ -658,7 +658,9 @@
* @returns {ReadableStream<Uint8Array>}
*/
function readableStreamForRid(rid, unrefCallback) {
- const stream = new ReadableStream({
+ const stream = webidl.createBranded(ReadableStream);
+ stream[_maybeRid] = rid;
+ const underlyingSource = {
type: "bytes",
async pull(controller) {
const v = controller.byobRequest.view;
@@ -685,9 +687,15 @@
core.tryClose(rid);
},
autoAllocateChunkSize: DEFAULT_CHUNK_SIZE,
- });
+ };
+ initializeReadableStream(stream);
+ setUpReadableByteStreamControllerFromUnderlyingSource(
+ stream,
+ underlyingSource,
+ underlyingSource,
+ 0,
+ );
- stream[_maybeRid] = rid;
return stream;
}
@@ -714,7 +722,6 @@
}
return true;
}
-
/**
* @template T
* @param {{ [_queue]: Array<ValueWithSize<T | _close>>, [_queueTotalSize]: number }} container
diff --git a/runtime/js/40_files.js b/runtime/js/40_files.js
index 12e406aba..a2afdb09d 100644
--- a/runtime/js/40_files.js
+++ b/runtime/js/40_files.js
@@ -33,12 +33,14 @@
function openSync(
path,
- options = { read: true },
+ options,
) {
- checkOpenOptions(options);
+ if (options) checkOpenOptions(options);
const mode = options?.mode;
const rid = ops.op_open_sync(
- { path: pathFromURL(path), options, mode },
+ pathFromURL(path),
+ options,
+ mode,
);
return new FsFile(rid);
@@ -46,13 +48,15 @@
async function open(
path,
- options = { read: true },
+ options,
) {
- checkOpenOptions(options);
+ if (options) checkOpenOptions(options);
const mode = options?.mode;
const rid = await core.opAsync(
"op_open_async",
- { path: pathFromURL(path), options, mode },
+ pathFromURL(path),
+ options,
+ mode,
);
return new FsFile(rid);
diff --git a/runtime/ops/fs.rs b/runtime/ops/fs.rs
index 5c9c5c99b..1637c20c0 100644
--- a/runtime/ops/fs.rs
+++ b/runtime/ops/fs.rs
@@ -128,15 +128,18 @@ pub struct OpenOptions {
create_new: bool,
}
+#[inline]
fn open_helper(
state: &mut OpState,
- args: &OpenArgs,
+ path: &str,
+ mode: Option<u32>,
+ options: Option<&OpenOptions>,
) -> Result<(PathBuf, std::fs::OpenOptions), AnyError> {
- let path = Path::new(&args.path).to_path_buf();
+ let path = Path::new(path).to_path_buf();
let mut open_options = std::fs::OpenOptions::new();
- if let Some(mode) = args.mode {
+ if let Some(mode) = mode {
// mode only used if creating the file on Unix
// if not specified, defaults to 0o666
#[cfg(unix)]
@@ -149,23 +152,36 @@ fn open_helper(
}
let permissions = state.borrow_mut::<Permissions>();
- let options = &args.options;
- if options.read {
- permissions.read.check(&path)?;
- }
+ match options {
+ None => {
+ permissions.read.check(&path)?;
+ open_options
+ .read(true)
+ .create(false)
+ .write(false)
+ .truncate(false)
+ .append(false)
+ .create_new(false);
+ }
+ Some(options) => {
+ if options.read {
+ permissions.read.check(&path)?;
+ }
- if options.write || options.append {
- permissions.write.check(&path)?;
- }
+ if options.write || options.append {
+ permissions.write.check(&path)?;
+ }
- open_options
- .read(options.read)
- .create(options.create)
- .write(options.write)
- .truncate(options.truncate)
- .append(options.append)
- .create_new(options.create_new);
+ open_options
+ .read(options.read)
+ .create(options.create)
+ .write(options.write)
+ .truncate(options.truncate)
+ .append(options.append)
+ .create_new(options.create_new);
+ }
+ }
Ok((path, open_options))
}
@@ -173,9 +189,11 @@ fn open_helper(
#[op]
fn op_open_sync(
state: &mut OpState,
- args: OpenArgs,
+ path: String,
+ options: Option<OpenOptions>,
+ mode: Option<u32>,
) -> Result<ResourceId, AnyError> {
- let (path, open_options) = open_helper(state, &args)?;
+ let (path, open_options) = open_helper(state, &path, mode, options.as_ref())?;
let std_file = open_options.open(&path).map_err(|err| {
Error::new(err.kind(), format!("{}, open '{}'", err, path.display()))
})?;
@@ -187,9 +205,12 @@ fn op_open_sync(
#[op]
async fn op_open_async(
state: Rc<RefCell<OpState>>,
- args: OpenArgs,
+ path: String,
+ options: Option<OpenOptions>,
+ mode: Option<u32>,
) -> Result<ResourceId, AnyError> {
- let (path, open_options) = open_helper(&mut state.borrow_mut(), &args)?;
+ let (path, open_options) =
+ open_helper(&mut state.borrow_mut(), &path, mode, options.as_ref())?;
let std_file = tokio::task::spawn_blocking(move || {
open_options.open(path.clone()).map_err(|err| {
Error::new(err.kind(), format!("{}, open '{}'", err, path.display()))
@@ -238,7 +259,12 @@ fn op_write_file_sync(
args: WriteFileArgs,
) -> Result<(), AnyError> {
let (open_args, data) = args.into_open_args_and_data();
- let (path, open_options) = open_helper(state, &open_args)?;
+ let (path, open_options) = open_helper(
+ state,
+ &open_args.path,
+ open_args.mode,
+ Some(&open_args.options),
+ )?;
write_file(&path, open_options, &open_args, data)
}
@@ -256,7 +282,12 @@ async fn op_write_file_async(
None => None,
};
let (open_args, data) = args.into_open_args_and_data();
- let (path, open_options) = open_helper(&mut *state.borrow_mut(), &open_args)?;
+ let (path, open_options) = open_helper(
+ &mut *state.borrow_mut(),
+ &open_args.path,
+ open_args.mode,
+ Some(&open_args.options),
+ )?;
let write_future = tokio::task::spawn_blocking(move || {
write_file(&path, open_options, &open_args, data)
});