2021-01-11 12:13:41 -05:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
2020-03-03 12:22:53 -05:00
|
|
|
|
|
|
|
// Requires to be run with `--allow-net` flag
|
|
|
|
|
2021-01-06 15:31:16 -05:00
|
|
|
import {
|
|
|
|
assert,
|
|
|
|
assertEquals,
|
|
|
|
assertThrows,
|
2021-02-15 08:48:47 -05:00
|
|
|
} from "../../../test_util/std/testing/asserts.ts";
|
|
|
|
import { deferred } from "../../../test_util/std/async/deferred.ts";
|
2020-03-03 12:22:53 -05:00
|
|
|
|
2020-03-18 19:25:55 -04:00
|
|
|
Deno.test({
|
2020-04-09 18:15:17 -04:00
|
|
|
name: "worker terminate",
|
2020-03-28 13:03:49 -04:00
|
|
|
fn: async function (): Promise<void> {
|
2020-11-26 11:22:36 -05:00
|
|
|
const promise = deferred();
|
2020-04-09 18:15:17 -04:00
|
|
|
|
2020-06-09 08:33:52 -04:00
|
|
|
const jsWorker = new Worker(
|
2021-02-15 08:48:47 -05:00
|
|
|
new URL("test_worker.js", import.meta.url).href,
|
2020-07-14 15:24:17 -04:00
|
|
|
{ type: "module" },
|
2020-06-09 08:33:52 -04:00
|
|
|
);
|
|
|
|
const tsWorker = new Worker(
|
2021-02-15 08:48:47 -05:00
|
|
|
new URL("test_worker.ts", import.meta.url).href,
|
2020-07-14 15:24:17 -04:00
|
|
|
{ type: "module", name: "tsWorker" },
|
2020-06-09 08:33:52 -04:00
|
|
|
);
|
2020-03-18 19:25:55 -04:00
|
|
|
|
|
|
|
tsWorker.onmessage = (e): void => {
|
|
|
|
assertEquals(e.data, "Hello World");
|
|
|
|
promise.resolve();
|
|
|
|
};
|
|
|
|
|
|
|
|
jsWorker.onmessage = (e): void => {
|
|
|
|
assertEquals(e.data, "Hello World");
|
|
|
|
tsWorker.postMessage("Hello World");
|
|
|
|
};
|
|
|
|
|
|
|
|
jsWorker.onerror = (e: Event): void => {
|
|
|
|
e.preventDefault();
|
|
|
|
jsWorker.postMessage("Hello World");
|
|
|
|
};
|
2020-02-11 04:04:59 -05:00
|
|
|
|
|
|
|
jsWorker.postMessage("Hello World");
|
2020-03-18 19:25:55 -04:00
|
|
|
await promise;
|
2020-04-09 18:15:17 -04:00
|
|
|
tsWorker.terminate();
|
|
|
|
jsWorker.terminate();
|
2020-03-28 13:03:49 -04:00
|
|
|
},
|
2020-02-11 04:04:59 -05:00
|
|
|
});
|
|
|
|
|
2021-01-05 21:22:38 -05:00
|
|
|
Deno.test({
|
|
|
|
name: "worker from data url",
|
|
|
|
async fn() {
|
|
|
|
const promise = deferred();
|
|
|
|
const tsWorker = new Worker(
|
|
|
|
"data:application/typescript;base64,aWYgKHNlbGYubmFtZSAhPT0gInRzV29ya2VyIikgewogIHRocm93IEVycm9yKGBJbnZhbGlkIHdvcmtlciBuYW1lOiAke3NlbGYubmFtZX0sIGV4cGVjdGVkIHRzV29ya2VyYCk7Cn0KCm9ubWVzc2FnZSA9IGZ1bmN0aW9uIChlKTogdm9pZCB7CiAgcG9zdE1lc3NhZ2UoZS5kYXRhKTsKICBjbG9zZSgpOwp9Owo=",
|
|
|
|
{ type: "module", name: "tsWorker" },
|
|
|
|
);
|
|
|
|
|
|
|
|
tsWorker.onmessage = (e): void => {
|
|
|
|
assertEquals(e.data, "Hello World");
|
|
|
|
promise.resolve();
|
|
|
|
};
|
|
|
|
|
|
|
|
tsWorker.postMessage("Hello World");
|
|
|
|
|
|
|
|
await promise;
|
|
|
|
tsWorker.terminate();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2020-03-18 19:25:55 -04:00
|
|
|
Deno.test({
|
2020-04-09 18:15:17 -04:00
|
|
|
name: "worker nested",
|
2020-03-28 13:03:49 -04:00
|
|
|
fn: async function (): Promise<void> {
|
2020-11-26 11:22:36 -05:00
|
|
|
const promise = deferred();
|
2020-03-18 19:25:55 -04:00
|
|
|
|
2020-06-09 08:33:52 -04:00
|
|
|
const nestedWorker = new Worker(
|
2021-02-15 08:48:47 -05:00
|
|
|
new URL("nested_worker.js", import.meta.url).href,
|
2020-07-14 15:24:17 -04:00
|
|
|
{ type: "module", name: "nested" },
|
2020-06-09 08:33:52 -04:00
|
|
|
);
|
2020-03-18 19:25:55 -04:00
|
|
|
|
|
|
|
nestedWorker.onmessage = (e): void => {
|
|
|
|
assert(e.data.type !== "error");
|
|
|
|
promise.resolve();
|
|
|
|
};
|
|
|
|
|
|
|
|
nestedWorker.postMessage("Hello World");
|
|
|
|
await promise;
|
2020-04-09 18:15:17 -04:00
|
|
|
nestedWorker.terminate();
|
2020-03-28 13:03:49 -04:00
|
|
|
},
|
2020-02-11 04:04:59 -05:00
|
|
|
});
|
|
|
|
|
2020-03-18 19:25:55 -04:00
|
|
|
Deno.test({
|
2020-04-09 18:15:17 -04:00
|
|
|
name: "worker throws when executing",
|
2020-03-28 13:03:49 -04:00
|
|
|
fn: async function (): Promise<void> {
|
2020-11-26 11:22:36 -05:00
|
|
|
const promise = deferred();
|
2020-06-09 08:33:52 -04:00
|
|
|
const throwingWorker = new Worker(
|
2021-02-15 08:48:47 -05:00
|
|
|
new URL("throwing_worker.js", import.meta.url).href,
|
2020-07-14 15:24:17 -04:00
|
|
|
{ type: "module" },
|
2020-06-09 08:33:52 -04:00
|
|
|
);
|
2020-03-18 19:25:55 -04:00
|
|
|
|
2020-11-03 10:19:29 -05:00
|
|
|
// deno-lint-ignore no-explicit-any
|
2020-03-18 19:25:55 -04:00
|
|
|
throwingWorker.onerror = (e: any): void => {
|
|
|
|
e.preventDefault();
|
|
|
|
assert(/Uncaught Error: Thrown error/.test(e.message));
|
|
|
|
promise.resolve();
|
|
|
|
};
|
|
|
|
|
|
|
|
await promise;
|
2020-04-09 18:15:17 -04:00
|
|
|
throwingWorker.terminate();
|
2020-03-28 13:03:49 -04:00
|
|
|
},
|
2020-02-11 04:04:59 -05:00
|
|
|
});
|
2020-02-21 10:35:41 -05:00
|
|
|
|
2020-10-11 18:04:43 -04:00
|
|
|
Deno.test({
|
|
|
|
name: "worker globals",
|
|
|
|
fn: async function (): Promise<void> {
|
2020-11-26 11:22:36 -05:00
|
|
|
const promise = deferred();
|
2021-01-18 21:26:39 -05:00
|
|
|
const workerOptions: WorkerOptions = { type: "module" };
|
2020-10-11 18:04:43 -04:00
|
|
|
const w = new Worker(
|
2021-02-15 08:48:47 -05:00
|
|
|
new URL("worker_globals.ts", import.meta.url).href,
|
2021-01-18 21:26:39 -05:00
|
|
|
workerOptions,
|
2020-10-11 18:04:43 -04:00
|
|
|
);
|
|
|
|
w.onmessage = (e): void => {
|
|
|
|
assertEquals(e.data, "true, true, true");
|
|
|
|
promise.resolve();
|
|
|
|
};
|
|
|
|
w.postMessage("Hello, world!");
|
|
|
|
await promise;
|
|
|
|
w.terminate();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2020-03-18 19:25:55 -04:00
|
|
|
Deno.test({
|
2020-04-09 18:15:17 -04:00
|
|
|
name: "worker fetch API",
|
2020-03-28 13:03:49 -04:00
|
|
|
fn: async function (): Promise<void> {
|
2020-11-26 11:22:36 -05:00
|
|
|
const promise = deferred();
|
2020-03-18 19:25:55 -04:00
|
|
|
|
2020-06-09 08:33:52 -04:00
|
|
|
const fetchingWorker = new Worker(
|
2021-02-15 08:48:47 -05:00
|
|
|
new URL("fetching_worker.js", import.meta.url).href,
|
2020-07-14 15:24:17 -04:00
|
|
|
{ type: "module" },
|
2020-06-09 08:33:52 -04:00
|
|
|
);
|
2020-03-18 19:25:55 -04:00
|
|
|
|
2020-11-03 10:19:29 -05:00
|
|
|
// deno-lint-ignore no-explicit-any
|
2020-03-18 19:25:55 -04:00
|
|
|
fetchingWorker.onerror = (e: any): void => {
|
|
|
|
e.preventDefault();
|
|
|
|
promise.reject(e.message);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Defer promise.resolve() to allow worker to shut down
|
|
|
|
fetchingWorker.onmessage = (e): void => {
|
|
|
|
assert(e.data === "Done!");
|
|
|
|
promise.resolve();
|
|
|
|
};
|
|
|
|
|
2020-04-09 18:15:17 -04:00
|
|
|
await promise;
|
|
|
|
fetchingWorker.terminate();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "worker terminate busy loop",
|
|
|
|
fn: async function (): Promise<void> {
|
2020-11-26 11:22:36 -05:00
|
|
|
const promise = deferred();
|
2020-04-09 18:15:17 -04:00
|
|
|
|
2020-06-09 08:33:52 -04:00
|
|
|
const busyWorker = new Worker(
|
2021-02-15 08:48:47 -05:00
|
|
|
new URL("busy_worker.js", import.meta.url).href,
|
2020-07-14 15:24:17 -04:00
|
|
|
{ type: "module" },
|
2020-06-09 08:33:52 -04:00
|
|
|
);
|
2020-04-09 18:15:17 -04:00
|
|
|
|
|
|
|
let testResult = 0;
|
|
|
|
|
|
|
|
busyWorker.onmessage = (e): void => {
|
|
|
|
testResult = e.data;
|
|
|
|
if (testResult >= 10000) {
|
|
|
|
busyWorker.terminate();
|
|
|
|
busyWorker.onmessage = (_e): void => {
|
|
|
|
throw new Error("unreachable");
|
|
|
|
};
|
|
|
|
setTimeout(() => {
|
|
|
|
assertEquals(testResult, 10000);
|
|
|
|
promise.resolve();
|
|
|
|
}, 100);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
busyWorker.postMessage("ping");
|
|
|
|
await promise;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "worker race condition",
|
|
|
|
fn: async function (): Promise<void> {
|
|
|
|
// See issue for details
|
|
|
|
// https://github.com/denoland/deno/issues/4080
|
2020-11-26 11:22:36 -05:00
|
|
|
const promise = deferred();
|
2020-04-09 18:15:17 -04:00
|
|
|
|
2020-06-09 08:33:52 -04:00
|
|
|
const racyWorker = new Worker(
|
2021-02-15 08:48:47 -05:00
|
|
|
new URL("racy_worker.js", import.meta.url).href,
|
2020-07-14 15:24:17 -04:00
|
|
|
{ type: "module" },
|
2020-06-09 08:33:52 -04:00
|
|
|
);
|
2020-04-09 18:15:17 -04:00
|
|
|
|
|
|
|
racyWorker.onmessage = (e): void => {
|
|
|
|
assertEquals(e.data.buf.length, 999999);
|
|
|
|
racyWorker.onmessage = (_e): void => {
|
|
|
|
throw new Error("unreachable");
|
|
|
|
};
|
|
|
|
setTimeout(() => {
|
|
|
|
promise.resolve();
|
|
|
|
}, 100);
|
|
|
|
};
|
|
|
|
|
2020-03-18 19:25:55 -04:00
|
|
|
await promise;
|
2020-03-28 13:03:49 -04:00
|
|
|
},
|
2020-02-21 10:35:41 -05:00
|
|
|
});
|
2020-04-13 12:34:32 -04:00
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "worker is event listener",
|
|
|
|
fn: async function (): Promise<void> {
|
|
|
|
let messageHandlersCalled = 0;
|
|
|
|
let errorHandlersCalled = 0;
|
|
|
|
|
2020-11-26 11:22:36 -05:00
|
|
|
const promise1 = deferred();
|
|
|
|
const promise2 = deferred();
|
2020-04-13 12:34:32 -04:00
|
|
|
|
2020-06-09 08:33:52 -04:00
|
|
|
const worker = new Worker(
|
2021-02-15 08:48:47 -05:00
|
|
|
new URL("event_worker.js", import.meta.url).href,
|
2020-07-14 15:24:17 -04:00
|
|
|
{ type: "module" },
|
2020-06-09 08:33:52 -04:00
|
|
|
);
|
2020-04-13 12:34:32 -04:00
|
|
|
|
|
|
|
worker.onmessage = (_e: Event): void => {
|
|
|
|
messageHandlersCalled++;
|
|
|
|
};
|
|
|
|
worker.addEventListener("message", (_e: Event) => {
|
|
|
|
messageHandlersCalled++;
|
|
|
|
});
|
|
|
|
worker.addEventListener("message", (_e: Event) => {
|
|
|
|
messageHandlersCalled++;
|
|
|
|
promise1.resolve();
|
|
|
|
});
|
|
|
|
|
|
|
|
worker.onerror = (e): void => {
|
|
|
|
errorHandlersCalled++;
|
|
|
|
e.preventDefault();
|
|
|
|
};
|
|
|
|
worker.addEventListener("error", (_e: Event) => {
|
|
|
|
errorHandlersCalled++;
|
|
|
|
});
|
|
|
|
worker.addEventListener("error", (_e: Event) => {
|
|
|
|
errorHandlersCalled++;
|
|
|
|
promise2.resolve();
|
|
|
|
});
|
|
|
|
|
|
|
|
worker.postMessage("ping");
|
|
|
|
await promise1;
|
|
|
|
assertEquals(messageHandlersCalled, 3);
|
|
|
|
|
|
|
|
worker.postMessage("boom");
|
|
|
|
await promise2;
|
|
|
|
assertEquals(errorHandlersCalled, 3);
|
|
|
|
worker.terminate();
|
|
|
|
},
|
|
|
|
});
|
2020-04-13 16:18:31 -04:00
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "worker scope is event listener",
|
|
|
|
fn: async function (): Promise<void> {
|
2020-11-26 11:22:36 -05:00
|
|
|
const promise1 = deferred();
|
2020-04-13 16:18:31 -04:00
|
|
|
|
2020-06-09 08:33:52 -04:00
|
|
|
const worker = new Worker(
|
2021-02-15 08:48:47 -05:00
|
|
|
new URL("event_worker_scope.js", import.meta.url).href,
|
2020-07-14 15:24:17 -04:00
|
|
|
{ type: "module" },
|
2020-06-09 08:33:52 -04:00
|
|
|
);
|
2020-04-13 16:18:31 -04:00
|
|
|
|
|
|
|
worker.onmessage = (e: MessageEvent): void => {
|
|
|
|
const { messageHandlersCalled, errorHandlersCalled } = e.data;
|
|
|
|
assertEquals(messageHandlersCalled, 4);
|
|
|
|
assertEquals(errorHandlersCalled, 4);
|
|
|
|
promise1.resolve();
|
|
|
|
};
|
|
|
|
|
|
|
|
worker.onerror = (_e): void => {
|
|
|
|
throw new Error("unreachable");
|
|
|
|
};
|
|
|
|
|
|
|
|
worker.postMessage("boom");
|
|
|
|
worker.postMessage("ping");
|
|
|
|
await promise1;
|
|
|
|
worker.terminate();
|
|
|
|
},
|
|
|
|
});
|
2020-04-16 17:40:29 -04:00
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "worker with Deno namespace",
|
|
|
|
fn: async function (): Promise<void> {
|
2020-11-26 11:22:36 -05:00
|
|
|
const promise = deferred();
|
|
|
|
const promise2 = deferred();
|
2020-04-16 17:40:29 -04:00
|
|
|
|
2020-06-09 08:33:52 -04:00
|
|
|
const regularWorker = new Worker(
|
2021-02-15 08:48:47 -05:00
|
|
|
new URL("non_deno_worker.js", import.meta.url).href,
|
2020-07-14 15:24:17 -04:00
|
|
|
{ type: "module" },
|
2020-06-09 08:33:52 -04:00
|
|
|
);
|
|
|
|
const denoWorker = new Worker(
|
2021-02-15 08:48:47 -05:00
|
|
|
new URL("deno_worker.ts", import.meta.url).href,
|
2021-01-06 15:31:16 -05:00
|
|
|
{
|
|
|
|
type: "module",
|
|
|
|
deno: {
|
|
|
|
namespace: true,
|
|
|
|
permissions: "inherit",
|
|
|
|
},
|
|
|
|
},
|
2020-06-09 08:33:52 -04:00
|
|
|
);
|
2020-04-16 17:40:29 -04:00
|
|
|
|
|
|
|
regularWorker.onmessage = (e): void => {
|
|
|
|
assertEquals(e.data, "Hello World");
|
|
|
|
regularWorker.terminate();
|
|
|
|
promise.resolve();
|
|
|
|
};
|
|
|
|
|
|
|
|
denoWorker.onmessage = (e): void => {
|
|
|
|
assertEquals(e.data, "Hello World");
|
|
|
|
denoWorker.terminate();
|
|
|
|
promise2.resolve();
|
|
|
|
};
|
|
|
|
|
|
|
|
regularWorker.postMessage("Hello World");
|
|
|
|
await promise;
|
|
|
|
denoWorker.postMessage("Hello World");
|
|
|
|
await promise2;
|
|
|
|
},
|
|
|
|
});
|
2020-05-08 08:30:53 -04:00
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "worker with crypto in scope",
|
|
|
|
fn: async function (): Promise<void> {
|
2020-11-26 11:22:36 -05:00
|
|
|
const promise = deferred();
|
2020-06-09 08:33:52 -04:00
|
|
|
const w = new Worker(
|
2021-02-15 08:48:47 -05:00
|
|
|
new URL("worker_crypto.js", import.meta.url).href,
|
2020-07-14 15:24:17 -04:00
|
|
|
{ type: "module" },
|
2020-06-09 08:33:52 -04:00
|
|
|
);
|
2020-05-08 08:30:53 -04:00
|
|
|
w.onmessage = (e): void => {
|
|
|
|
assertEquals(e.data, true);
|
|
|
|
promise.resolve();
|
|
|
|
};
|
|
|
|
w.postMessage(null);
|
|
|
|
await promise;
|
|
|
|
w.terminate();
|
|
|
|
},
|
|
|
|
});
|
2020-11-10 07:15:42 -05:00
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "Worker event handler order",
|
|
|
|
fn: async function (): Promise<void> {
|
2020-11-26 11:22:36 -05:00
|
|
|
const promise = deferred();
|
2020-11-10 07:15:42 -05:00
|
|
|
const w = new Worker(
|
2021-02-15 08:48:47 -05:00
|
|
|
new URL("test_worker.ts", import.meta.url).href,
|
2020-11-10 07:15:42 -05:00
|
|
|
{ type: "module", name: "tsWorker" },
|
|
|
|
);
|
|
|
|
const arr: number[] = [];
|
|
|
|
w.addEventListener("message", () => arr.push(1));
|
|
|
|
w.onmessage = (e): void => {
|
|
|
|
arr.push(2);
|
|
|
|
};
|
|
|
|
w.addEventListener("message", () => arr.push(3));
|
|
|
|
w.addEventListener("message", () => {
|
|
|
|
assertEquals(arr, [1, 2, 3]);
|
|
|
|
promise.resolve();
|
|
|
|
});
|
|
|
|
w.postMessage("Hello World");
|
|
|
|
await promise;
|
|
|
|
w.terminate();
|
|
|
|
},
|
|
|
|
});
|
2020-11-27 08:19:24 -05:00
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "Worker immediate close",
|
|
|
|
fn: async function (): Promise<void> {
|
|
|
|
const promise = deferred();
|
|
|
|
const w = new Worker(
|
2021-02-15 08:48:47 -05:00
|
|
|
new URL("./immediately_close_worker.js", import.meta.url).href,
|
2020-11-27 08:19:24 -05:00
|
|
|
{ type: "module" },
|
|
|
|
);
|
|
|
|
setTimeout(() => {
|
|
|
|
promise.resolve();
|
|
|
|
}, 1000);
|
|
|
|
await promise;
|
|
|
|
w.terminate();
|
|
|
|
},
|
|
|
|
});
|
2021-01-06 15:31:16 -05:00
|
|
|
|
2021-01-08 09:44:24 -05:00
|
|
|
Deno.test({
|
|
|
|
name: "Worker post undefined",
|
|
|
|
fn: async function (): Promise<void> {
|
|
|
|
const promise = deferred();
|
|
|
|
const worker = new Worker(
|
2021-02-15 08:48:47 -05:00
|
|
|
new URL("./post_undefined.ts", import.meta.url).href,
|
2021-01-08 09:44:24 -05:00
|
|
|
{ type: "module" },
|
|
|
|
);
|
|
|
|
|
|
|
|
const handleWorkerMessage = (e: MessageEvent): void => {
|
|
|
|
console.log("main <- worker:", e.data);
|
|
|
|
worker.terminate();
|
|
|
|
promise.resolve();
|
|
|
|
};
|
|
|
|
|
|
|
|
worker.addEventListener("messageerror", () => console.log("message error"));
|
|
|
|
worker.addEventListener("error", () => console.log("error"));
|
|
|
|
worker.addEventListener("message", handleWorkerMessage);
|
|
|
|
|
|
|
|
console.log("\npost from parent");
|
|
|
|
worker.postMessage(undefined);
|
|
|
|
await promise;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2021-01-06 15:31:16 -05:00
|
|
|
Deno.test("Worker inherits permissions", async function () {
|
|
|
|
const promise = deferred();
|
|
|
|
const worker = new Worker(
|
2021-02-15 08:48:47 -05:00
|
|
|
new URL("./read_check_worker.js", import.meta.url).href,
|
2021-01-06 15:31:16 -05:00
|
|
|
{
|
|
|
|
type: "module",
|
|
|
|
deno: {
|
|
|
|
namespace: true,
|
|
|
|
permissions: "inherit",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
worker.onmessage = ({ data: hasPermission }) => {
|
|
|
|
assert(hasPermission);
|
|
|
|
promise.resolve();
|
|
|
|
};
|
|
|
|
|
|
|
|
worker.postMessage(null);
|
|
|
|
|
|
|
|
await promise;
|
|
|
|
worker.terminate();
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("Worker limit children permissions", async function () {
|
|
|
|
const promise = deferred();
|
|
|
|
const worker = new Worker(
|
2021-02-15 08:48:47 -05:00
|
|
|
new URL("./read_check_worker.js", import.meta.url).href,
|
2021-01-06 15:31:16 -05:00
|
|
|
{
|
|
|
|
type: "module",
|
|
|
|
deno: {
|
|
|
|
namespace: true,
|
|
|
|
permissions: {
|
|
|
|
read: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
worker.onmessage = ({ data: hasPermission }) => {
|
|
|
|
assert(!hasPermission);
|
|
|
|
promise.resolve();
|
|
|
|
};
|
|
|
|
|
|
|
|
worker.postMessage(null);
|
|
|
|
|
|
|
|
await promise;
|
|
|
|
worker.terminate();
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("Worker limit children permissions granularly", async function () {
|
|
|
|
const promise = deferred();
|
|
|
|
const worker = new Worker(
|
2021-02-15 08:48:47 -05:00
|
|
|
new URL("./read_check_granular_worker.js", import.meta.url).href,
|
2021-01-06 15:31:16 -05:00
|
|
|
{
|
|
|
|
type: "module",
|
|
|
|
deno: {
|
|
|
|
namespace: true,
|
|
|
|
permissions: {
|
|
|
|
read: [
|
2021-02-15 08:48:47 -05:00
|
|
|
new URL("./read_check_worker.js", import.meta.url),
|
2021-01-06 15:31:16 -05:00
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
//Routes are relative to the spawned worker location
|
|
|
|
const routes = [
|
|
|
|
{ permission: false, route: "read_check_granular_worker.js" },
|
|
|
|
{ permission: true, route: "read_check_worker.js" },
|
|
|
|
];
|
|
|
|
|
|
|
|
let checked = 0;
|
|
|
|
worker.onmessage = ({ data }) => {
|
|
|
|
checked++;
|
|
|
|
assertEquals(data.hasPermission, routes[data.index].permission);
|
|
|
|
routes.shift();
|
|
|
|
if (checked === routes.length) {
|
|
|
|
promise.resolve();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
routes.forEach(({ route }, index) =>
|
|
|
|
worker.postMessage({
|
|
|
|
index,
|
|
|
|
route,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
await promise;
|
|
|
|
worker.terminate();
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("Nested worker limit children permissions", async function () {
|
|
|
|
const promise = deferred();
|
|
|
|
|
|
|
|
/** This worker has read permissions but doesn't grant them to its children */
|
|
|
|
const worker = new Worker(
|
2021-02-15 08:48:47 -05:00
|
|
|
new URL("./parent_read_check_worker.js", import.meta.url).href,
|
2021-01-06 15:31:16 -05:00
|
|
|
{
|
|
|
|
type: "module",
|
|
|
|
deno: {
|
|
|
|
namespace: true,
|
|
|
|
permissions: "inherit",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
worker.onmessage = ({ data }) => {
|
|
|
|
assert(data.parentHasPermission);
|
|
|
|
assert(!data.childHasPermission);
|
|
|
|
promise.resolve();
|
|
|
|
};
|
|
|
|
|
|
|
|
worker.postMessage(null);
|
|
|
|
|
|
|
|
await promise;
|
|
|
|
worker.terminate();
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("Nested worker limit children permissions granularly", async function () {
|
|
|
|
const promise = deferred();
|
|
|
|
|
|
|
|
/** This worker has read permissions but doesn't grant them to its children */
|
|
|
|
const worker = new Worker(
|
2021-02-15 08:48:47 -05:00
|
|
|
new URL("./parent_read_check_granular_worker.js", import.meta.url)
|
2021-01-06 15:31:16 -05:00
|
|
|
.href,
|
|
|
|
{
|
|
|
|
type: "module",
|
|
|
|
deno: {
|
|
|
|
namespace: true,
|
|
|
|
permissions: {
|
|
|
|
read: [
|
2021-02-15 08:48:47 -05:00
|
|
|
new URL("./read_check_granular_worker.js", import.meta.url),
|
2021-01-06 15:31:16 -05:00
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
//Routes are relative to the spawned worker location
|
|
|
|
const routes = [
|
|
|
|
{
|
|
|
|
childHasPermission: false,
|
|
|
|
parentHasPermission: true,
|
|
|
|
route: "read_check_granular_worker.js",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
childHasPermission: false,
|
|
|
|
parentHasPermission: false,
|
|
|
|
route: "read_check_worker.js",
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
let checked = 0;
|
|
|
|
worker.onmessage = ({ data }) => {
|
|
|
|
checked++;
|
|
|
|
assertEquals(
|
|
|
|
data.childHasPermission,
|
|
|
|
routes[data.index].childHasPermission,
|
|
|
|
);
|
|
|
|
assertEquals(
|
|
|
|
data.parentHasPermission,
|
|
|
|
routes[data.index].parentHasPermission,
|
|
|
|
);
|
|
|
|
if (checked === routes.length) {
|
|
|
|
promise.resolve();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Index needed cause requests will be handled asynchronously
|
|
|
|
routes.forEach(({ route }, index) =>
|
|
|
|
worker.postMessage({
|
|
|
|
index,
|
|
|
|
route,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
await promise;
|
|
|
|
worker.terminate();
|
|
|
|
});
|
|
|
|
|
|
|
|
// This test relies on env permissions not being granted on main thread
|
|
|
|
Deno.test("Worker initialization throws on worker permissions greater than parent thread permissions", function () {
|
|
|
|
assertThrows(
|
|
|
|
() => {
|
|
|
|
const worker = new Worker(
|
2021-02-15 08:48:47 -05:00
|
|
|
new URL("./deno_worker.ts", import.meta.url).href,
|
2021-01-06 15:31:16 -05:00
|
|
|
{
|
|
|
|
type: "module",
|
|
|
|
deno: {
|
|
|
|
namespace: true,
|
|
|
|
permissions: {
|
|
|
|
env: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
);
|
|
|
|
worker.terminate();
|
|
|
|
},
|
|
|
|
Deno.errors.PermissionDenied,
|
|
|
|
"Can't escalate parent thread permissions",
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("Worker with disabled permissions", async function () {
|
|
|
|
const promise = deferred();
|
|
|
|
|
|
|
|
const worker = new Worker(
|
2021-02-15 08:48:47 -05:00
|
|
|
new URL("./no_permissions_worker.js", import.meta.url).href,
|
2021-01-06 15:31:16 -05:00
|
|
|
{
|
|
|
|
type: "module",
|
|
|
|
deno: {
|
|
|
|
namespace: true,
|
2021-01-07 05:52:30 -05:00
|
|
|
permissions: "none",
|
2021-01-06 15:31:16 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
worker.onmessage = ({ data: sandboxed }) => {
|
|
|
|
assert(sandboxed);
|
|
|
|
promise.resolve();
|
|
|
|
};
|
|
|
|
|
|
|
|
worker.postMessage(null);
|
|
|
|
await promise;
|
|
|
|
worker.terminate();
|
|
|
|
});
|
2021-01-07 13:06:08 -05:00
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "worker location",
|
|
|
|
fn: async function (): Promise<void> {
|
|
|
|
const promise = deferred();
|
|
|
|
const workerModuleHref =
|
2021-02-15 08:48:47 -05:00
|
|
|
new URL("worker_location.ts", import.meta.url).href;
|
2021-01-07 13:06:08 -05:00
|
|
|
const w = new Worker(workerModuleHref, { type: "module" });
|
|
|
|
w.onmessage = (e): void => {
|
2021-01-17 10:28:54 -05:00
|
|
|
assertEquals(e.data, `${workerModuleHref}, true`);
|
2021-01-07 13:06:08 -05:00
|
|
|
promise.resolve();
|
|
|
|
};
|
|
|
|
w.postMessage("Hello, world!");
|
|
|
|
await promise;
|
|
|
|
w.terminate();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "worker with relative specifier",
|
|
|
|
fn: async function (): Promise<void> {
|
|
|
|
assertEquals(location.href, "http://127.0.0.1:4545/cli/tests/");
|
|
|
|
const promise = deferred();
|
|
|
|
const w = new Worker(
|
|
|
|
"./workers/test_worker.ts",
|
|
|
|
{ type: "module", name: "tsWorker" },
|
|
|
|
);
|
|
|
|
w.onmessage = (e): void => {
|
|
|
|
assertEquals(e.data, "Hello, world!");
|
|
|
|
promise.resolve();
|
|
|
|
};
|
|
|
|
w.postMessage("Hello, world!");
|
|
|
|
await promise;
|
|
|
|
w.terminate();
|
|
|
|
},
|
|
|
|
});
|