From d3f3e0d717fdbbf531fde0c9e7259f6bb887fa10 Mon Sep 17 00:00:00 2001 From: Asher Gomez Date: Sun, 7 Apr 2024 15:42:53 +1000 Subject: [PATCH] FUTURE(ext/fs): make `Deno.FsFile` constructor illegal (#23235) I'm unsure whether we're planning to make the `Deno.FsFile` constructor illegal or remove `FsFile` from the `Deno.*` namspace in Deno 2. Either way, this PR works towards the former. I'll create a superceding PR if the latter is planned instead. Towards #23089 --- ext/fs/30_fs.js | 6 ++++++ runtime/js/99_main.js | 10 ++++++++++ tests/specs/future/runtime_api/main.js | 12 ++++++++++++ tests/specs/future/runtime_api/main.out | 1 + 4 files changed, 29 insertions(+) diff --git a/ext/fs/30_fs.js b/ext/fs/30_fs.js index c2fb665288..b6cd2d1f13 100644 --- a/ext/fs/30_fs.js +++ b/ext/fs/30_fs.js @@ -90,6 +90,7 @@ const { SymbolAsyncIterator, SymbolIterator, SymbolFor, + TypeError, Uint32Array, } = primordials; @@ -673,6 +674,11 @@ class FsFile { new Error().stack, "Use `Deno.open` or `Deno.openSync` instead.", ); + if (internals.future) { + throw new TypeError( + "`Deno.FsFile` cannot be constructed, use `Deno.open()` or `Deno.openSync()` instead.", + ); + } } } diff --git a/runtime/js/99_main.js b/runtime/js/99_main.js index c66732331f..a66a1660ec 100644 --- a/runtime/js/99_main.js +++ b/runtime/js/99_main.js @@ -678,6 +678,11 @@ function bootstrapMainRuntime(runtimeOptions, warmup = false) { 9: future, } = runtimeOptions; + // TODO(iuioiua): remove in Deno v2. This allows us to dynamically delete + // class properties within constructors for classes that are not defined + // within the Deno namespace. + internals.future = future; + removeImportedOps(); deprecatedApiWarningDisabled = shouldDisableDeprecatedApiWarning; @@ -840,6 +845,11 @@ function bootstrapWorkerRuntime( 9: future, } = runtimeOptions; + // TODO(iuioiua): remove in Deno v2. This allows us to dynamically delete + // class properties within constructors for classes that are not defined + // within the Deno namespace. + internals.future = future; + deprecatedApiWarningDisabled = shouldDisableDeprecatedApiWarning; verboseDeprecatedApiWarning = shouldUseVerboseDeprecatedApiWarning; performance.setTimeOrigin(DateNow()); diff --git a/tests/specs/future/runtime_api/main.js b/tests/specs/future/runtime_api/main.js index ed60a7b7c1..9d3b3ed6ce 100644 --- a/tests/specs/future/runtime_api/main.js +++ b/tests/specs/future/runtime_api/main.js @@ -31,4 +31,16 @@ console.log("Deno.writeAllSync is", Deno.writeAllSync); console.log("Deno.write is", Deno.write); console.log("Deno.writeSync is", Deno.writeSync); +try { + new Deno.FsFile(0); +} catch (error) { + if ( + error instanceof TypeError && + error.message === + "`Deno.FsFile` cannot be constructed, use `Deno.open()` or `Deno.openSync()` instead." + ) { + console.log("Deno.FsFile constructor is illegal"); + } +} + self.close(); diff --git a/tests/specs/future/runtime_api/main.out b/tests/specs/future/runtime_api/main.out index c8ef8c62a0..595ffd4a4a 100644 --- a/tests/specs/future/runtime_api/main.out +++ b/tests/specs/future/runtime_api/main.out @@ -27,3 +27,4 @@ Deno.writeAll is undefined Deno.writeAllSync is undefined Deno.write is undefined Deno.writeSync is undefined +Deno.FsFile constructor is illegal