1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-21 15:04:11 -05:00

BREAKING(unstable): Enable Deno namespace in workers by default (#14581)

This commit removes "WorkerOptions.deno" option as a boolean,
as well as "WorkerOptions.deno.namespace" settings. Starting
with this commit all workers have access to "Deno" namespace
by default.
This commit is contained in:
Nayeem Rahman 2022-05-17 21:27:17 +01:00 committed by GitHub
parent f57aac77ff
commit 330c820ae8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 50 additions and 198 deletions

View file

@ -1134,9 +1134,6 @@ declare function fetch(
declare interface WorkerOptions {
/** UNSTABLE: New API.
*
* Set deno.namespace to `true` to make `Deno` namespace and all of its
* methods available to the worker environment. Defaults to `false`.
*
* Configure permissions options to change the level of access the worker will
* have. By default it will have no permissions. Note that the permissions
@ -1154,7 +1151,6 @@ declare interface WorkerOptions {
* new URL("deno_worker.ts", import.meta.url).href, {
* type: "module",
* deno: {
* namespace: true,
* permissions: {
* read: true,
* },
@ -1163,11 +1159,7 @@ declare interface WorkerOptions {
* );
* ```
*/
// TODO(Soremwar)
// `deno: boolean` is kept for backwards compatibility with the previous
// worker options implementation. Remove for 2.0.
deno?: boolean | {
namespace?: boolean;
deno?: {
/** Set to `"none"` to disable all the permissions in the worker. */
permissions?: Deno.PermissionOptions;
};

View file

@ -172,7 +172,6 @@ fn create_web_worker_callback(
preload_module_cb,
format_js_error_fn: Some(Arc::new(format_js_error)),
source_map_getter: Some(Box::new(ps.clone())),
use_deno_namespace: args.use_deno_namespace,
worker_type: args.worker_type,
maybe_inspector_server,
get_error_class_fn: Some(&crate::errors::get_error_class_name),

View file

@ -2,7 +2,7 @@ import { deferred } from "../../../../../test_util/std/async/deferred.ts";
const promise = deferred();
const url = new URL("./worker.mjs", import.meta.url);
const worker = new Worker(url.href, { type: "module", deno: true });
const worker = new Worker(url.href, { type: "module" });
worker.onmessage = (e) => {
const pid = e.data.pid;

View file

@ -1,4 +1,4 @@
new Worker(
new URL("event_listener_error_immediate_exit.ts", import.meta.url).href,
{ type: "module", deno: { namespace: true } },
{ type: "module" },
);

View file

@ -1,7 +1,4 @@
new Worker("data:,setTimeout(() => Deno.exit(2), 200)", {
type: "module",
deno: { namespace: true },
});
new Worker("data:,setTimeout(() => Deno.exit(2), 200)", { type: "module" });
try {
await Deno.run({ cmd: ["ps"] });

View file

@ -2,7 +2,7 @@ import { deferred } from "../../../test_util/std/async/deferred.ts";
const worker = new Worker(
new URL("set_exit_code_worker.js", import.meta.url).href,
{ type: "module", deno: { namespace: true } },
{ type: "module" },
);
const promise1 = deferred();

View file

@ -1,12 +1,6 @@
const w = new Worker(
new URL("workers/worker_unstable.ts", import.meta.url).href,
{
type: "module",
deno: {
namespace: true,
},
name: "Unstable Worker",
},
{ type: "module", name: "Unstable Worker" },
);
w.postMessage({});

View file

@ -22,10 +22,7 @@ const WORKER2 = getCodeBlobUrl(`
const WORKER1 = getCodeBlobUrl(`
console.log("Worker 1");
const worker = new Worker(
${JSON.stringify(WORKER2)},
{ type: "module", deno: { namespace: true } }
);
const worker = new Worker(${JSON.stringify(WORKER2)}, { type: "module" });
worker.addEventListener("message", () => {
console.log("Terminating");
@ -34,7 +31,7 @@ const WORKER1 = getCodeBlobUrl(`
});
`);
new Worker(WORKER1, { type: "module", deno: { namespace: true } });
new Worker(WORKER1, { type: "module" });
// Don't kill the process before worker 2 is finished.
setTimeout(() => {}, 3000);

View file

@ -1,12 +1,6 @@
const worker = new Worker(
new URL("./read_check_granular_worker.js", import.meta.url).href,
{
type: "module",
deno: {
namespace: true,
permissions: "none",
},
},
{ type: "module", deno: { permissions: "none" } },
);
onmessage = ({ data }) => {

View file

@ -273,37 +273,19 @@ Deno.test({
Deno.test({
name: "worker with Deno namespace",
fn: async function () {
const regularWorker = new Worker(
new URL("non_deno_worker.js", import.meta.url),
{ type: "module" },
);
const denoWorker = new Worker(
new URL("deno_worker.ts", import.meta.url),
{
type: "module",
deno: {
namespace: true,
permissions: "inherit",
},
},
{ type: "module", deno: { permissions: "inherit" } },
);
const promise1 = deferred();
regularWorker.onmessage = (e) => {
regularWorker.terminate();
promise1.resolve(e.data);
};
const promise2 = deferred();
const promise = deferred();
denoWorker.onmessage = (e) => {
denoWorker.terminate();
promise2.resolve(e.data);
promise.resolve(e.data);
};
regularWorker.postMessage("Hello World");
assertEquals(await promise1, "Hello World");
denoWorker.postMessage("Hello World");
assertEquals(await promise2, "Hello World");
assertEquals(await promise, "Hello World");
},
});
@ -394,13 +376,7 @@ Deno.test({
Deno.test("Worker inherits permissions", async function () {
const worker = new Worker(
new URL("./read_check_worker.js", import.meta.url).href,
{
type: "module",
deno: {
namespace: true,
permissions: "inherit",
},
},
{ type: "module", deno: { permissions: "inherit" } },
);
const promise = deferred();
@ -416,15 +392,7 @@ Deno.test("Worker inherits permissions", async function () {
Deno.test("Worker limit children permissions", async function () {
const worker = new Worker(
new URL("./read_check_worker.js", import.meta.url).href,
{
type: "module",
deno: {
namespace: true,
permissions: {
read: false,
},
},
},
{ type: "module", deno: { permissions: { read: false } } },
);
const promise = deferred();
@ -443,7 +411,6 @@ Deno.test("Worker limit children permissions granularly", async function () {
{
type: "module",
deno: {
namespace: true,
permissions: {
env: ["foo"],
hrtime: true,
@ -493,13 +460,7 @@ Deno.test("Nested worker limit children permissions", async function () {
/** This worker has permissions but doesn't grant them to its children */
const worker = new Worker(
new URL("./parent_read_check_worker.js", import.meta.url).href,
{
type: "module",
deno: {
namespace: true,
permissions: "inherit",
},
},
{ type: "module", deno: { permissions: "inherit" } },
);
const promise = deferred();
worker.onmessage = ({ data }) => promise.resolve(data);
@ -544,15 +505,7 @@ Deno.test({
() => {
const worker = new Worker(
new URL("./deno_worker.ts", import.meta.url).href,
{
type: "module",
deno: {
namespace: true,
permissions: {
env: true,
},
},
},
{ type: "module", deno: { permissions: { env: true } } },
);
worker.terminate();
},
@ -565,13 +518,7 @@ Deno.test({
Deno.test("Worker with disabled permissions", async function () {
const worker = new Worker(
new URL("./no_permissions_worker.js", import.meta.url).href,
{
type: "module",
deno: {
namespace: true,
permissions: "none",
},
},
{ type: "module", deno: { permissions: "none" } },
);
const promise = deferred();
@ -587,13 +534,7 @@ Deno.test("Worker with disabled permissions", async function () {
Deno.test("Worker permissions are not inherited with empty permission object", async function () {
const worker = new Worker(
new URL("./permission_echo.js", import.meta.url).href,
{
type: "module",
deno: {
namespace: true,
permissions: {},
},
},
{ type: "module", deno: { permissions: {} } },
);
const promise = deferred();
@ -617,15 +558,7 @@ Deno.test("Worker permissions are not inherited with empty permission object", a
Deno.test("Worker permissions are not inherited with single specified permission", async function () {
const worker = new Worker(
new URL("./permission_echo.js", import.meta.url).href,
{
type: "module",
deno: {
namespace: true,
permissions: {
net: true,
},
},
},
{ type: "module", deno: { permissions: { net: true } } },
);
const promise = deferred();
@ -720,17 +653,8 @@ Deno.test({
fn: async function () {
const result = deferred();
const worker = new Worker(
new URL(
"./http_worker.js",
import.meta.url,
).href,
{
type: "module",
deno: {
namespace: true,
permissions: "inherit",
},
},
new URL("./http_worker.js", import.meta.url).href,
{ type: "module", deno: { permissions: "inherit" } },
);
worker.onmessage = () => {
result.resolve();
@ -870,7 +794,7 @@ Deno.test({
* self.onmessage = function() {self.postMessage(Deno.memoryUsage())}
*/
"data:application/typescript;base64,c2VsZi5vbm1lc3NhZ2UgPSBmdW5jdGlvbigpIHtzZWxmLnBvc3RNZXNzYWdlKERlbm8ubWVtb3J5VXNhZ2UoKSl9",
{ type: "module", name: "tsWorker", deno: true },
{ type: "module", name: "tsWorker" },
);
w.postMessage(null);

View file

@ -11,15 +11,7 @@ Deno.test(
"../testdata/workers/env_read_check_worker.js",
import.meta.url,
).href,
{
type: "module",
deno: {
namespace: true,
permissions: {
env: ["test", "OTHER"],
},
},
},
{ type: "module", deno: { permissions: { env: ["test", "OTHER"] } } },
);
worker.onmessage = ({ data }) => {

View file

@ -27,7 +27,6 @@
specifier,
hasSourceCode,
sourceCode,
useDenoNamespace,
permissions,
name,
workerType,
@ -38,7 +37,6 @@
permissions: serializePermissions(permissions),
sourceCode,
specifier,
useDenoNamespace,
workerType,
});
}
@ -79,20 +77,6 @@
type = "classic",
} = options;
let namespace;
let permissions;
if (typeof deno == "object") {
namespace = deno.namespace ?? false;
permissions = deno.permissions ?? undefined;
} else {
// Assume `deno: boolean | undefined`.
// TODO(Soremwar)
// `deno: boolean` is kept for backwards compatibility with the previous
// worker options implementation. Remove for 2.0
namespace = !!deno;
permissions = undefined;
}
const workerType = webidl.converters["WorkerType"](type);
if (
@ -120,8 +104,7 @@
specifier,
hasSourceCode,
sourceCode,
namespace,
permissions,
deno?.permissions,
name,
workerType,
);

View file

@ -26,7 +26,6 @@ delete Object.prototype.__proto__;
PromisePrototypeThen,
TypeError,
} = window.__bootstrap.primordials;
const infra = window.__bootstrap.infra;
const util = window.__bootstrap.util;
const eventTarget = window.__bootstrap.eventTarget;
const globalInterfaces = window.__bootstrap.globalInterfaces;
@ -627,7 +626,6 @@ delete Object.prototype.__proto__;
function bootstrapWorkerRuntime(
runtimeOptions,
name,
useDenoNamespace,
internalName,
) {
if (hasBootstrapped) {
@ -697,23 +695,18 @@ delete Object.prototype.__proto__;
close: core.close,
...denoNs,
};
if (useDenoNamespace) {
if (unstableFlag) {
ObjectAssign(finalDenoNs, denoNsUnstable);
}
ObjectDefineProperties(finalDenoNs, {
pid: util.readOnly(pid),
noColor: util.readOnly(noColor),
args: util.readOnly(ObjectFreeze(args)),
});
// Setup `Deno` global - we're actually overriding already
// existing global `Deno` with `Deno` namespace from "./deno.ts".
ObjectDefineProperty(globalThis, "Deno", util.readOnly(finalDenoNs));
ObjectFreeze(globalThis.Deno.core);
} else {
delete globalThis.Deno;
infra.assert(globalThis.Deno === undefined);
if (unstableFlag) {
ObjectAssign(finalDenoNs, denoNsUnstable);
}
ObjectDefineProperties(finalDenoNs, {
pid: util.readOnly(pid),
noColor: util.readOnly(noColor),
args: util.readOnly(ObjectFreeze(args)),
});
// Setup `Deno` global - we're actually overriding already
// existing global `Deno` with `Deno` namespace from "./deno.ts".
ObjectDefineProperty(globalThis, "Deno", util.readOnly(finalDenoNs));
ObjectFreeze(globalThis.Deno.core);
}
ObjectDefineProperties(globalThis, {

View file

@ -36,7 +36,6 @@ pub struct CreateWebWorkerArgs {
pub parent_permissions: Permissions,
pub permissions: Permissions,
pub main_module: ModuleSpecifier,
pub use_deno_namespace: bool,
pub worker_type: WebWorkerType,
pub maybe_exit_code: Option<Arc<AtomicI32>>,
}
@ -133,7 +132,6 @@ pub struct CreateWorkerArgs {
permissions: Option<ChildPermissionsArg>,
source_code: String,
specifier: String,
use_deno_namespace: bool,
worker_type: WebWorkerType,
}
@ -150,10 +148,6 @@ fn op_create_worker(
None
};
let args_name = args.name;
let use_deno_namespace = args.use_deno_namespace;
if use_deno_namespace {
super::check_unstable(state, "Worker.deno.namespace");
}
let worker_type = args.worker_type;
if let WebWorkerType::Classic = worker_type {
if let TestingFeaturesEnabled(false) = state.borrow() {
@ -216,7 +210,6 @@ fn op_create_worker(
parent_permissions,
permissions: worker_permissions,
main_module: module_specifier.clone(),
use_deno_namespace,
worker_type,
maybe_exit_code,
});

View file

@ -312,7 +312,6 @@ pub struct WebWorker {
pub js_runtime: JsRuntime,
pub name: String,
internal_handle: WebWorkerInternalHandle,
pub use_deno_namespace: bool,
pub worker_type: WebWorkerType,
pub main_module: ModuleSpecifier,
poll_for_messages_fn: Option<v8::Global<v8::Value>>,
@ -329,7 +328,6 @@ pub struct WebWorkerOptions {
pub preload_module_cb: Arc<ops::worker_host::PreloadModuleCb>,
pub format_js_error_fn: Option<Arc<FormatJsErrorFn>>,
pub source_map_getter: Option<Box<dyn SourceMapGetter>>,
pub use_deno_namespace: bool,
pub worker_type: WebWorkerType,
pub maybe_inspector_server: Option<Arc<InspectorServer>>,
pub get_error_class_fn: Option<GetErrorClassFn>,
@ -413,30 +411,28 @@ impl WebWorker {
options.format_js_error_fn.clone(),
),
// Extensions providing Deno.* features
ops::fs_events::init().enabled(options.use_deno_namespace),
ops::fs::init().enabled(options.use_deno_namespace),
ops::fs_events::init(),
ops::fs::init(),
ops::io::init(),
ops::io::init_stdio(options.stdio).enabled(options.use_deno_namespace),
deno_tls::init().enabled(options.use_deno_namespace),
ops::io::init_stdio(options.stdio),
deno_tls::init(),
deno_net::init::<Permissions>(
options.root_cert_store.clone(),
unstable,
options.unsafely_ignore_certificate_errors.clone(),
)
.enabled(options.use_deno_namespace),
),
ops::os::init(Some(
options
.maybe_exit_code
.expect("Worker has access to OS ops but exit code was not passed."),
))
.enabled(options.use_deno_namespace),
ops::permissions::init().enabled(options.use_deno_namespace),
ops::process::init().enabled(options.use_deno_namespace),
ops::spawn::init().enabled(options.use_deno_namespace),
ops::signal::init().enabled(options.use_deno_namespace),
ops::tty::init().enabled(options.use_deno_namespace),
deno_http::init().enabled(options.use_deno_namespace),
ops::http::init().enabled(options.use_deno_namespace),
)),
ops::permissions::init(),
ops::process::init(),
ops::spawn::init(),
ops::signal::init(),
ops::tty::init(),
deno_http::init(),
ops::http::init(),
// Permissions ext (worker specific state)
perm_ext,
];
@ -479,7 +475,6 @@ impl WebWorker {
js_runtime,
name,
internal_handle,
use_deno_namespace: options.use_deno_namespace,
worker_type: options.worker_type,
main_module,
poll_for_messages_fn: None,
@ -492,10 +487,9 @@ impl WebWorker {
// Instead of using name for log we use `worker-${id}` because
// WebWorkers can have empty string as name.
let script = format!(
"bootstrap.workerRuntime({}, \"{}\", {}, \"{}\")",
"bootstrap.workerRuntime({}, \"{}\", \"{}\")",
options.as_json(),
self.name,
self.use_deno_namespace,
self.id
);
self