tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

browser_worker-01.js (2438B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 const { DevToolsWorker, workerify } = ChromeUtils.importESModule(
      7  "resource://devtools/shared/worker/worker.sys.mjs"
      8 );
      9 
     10 const BUFFER_SIZE = 8;
     11 
     12 registerCleanupFunction(function () {
     13  Services.prefs.clearUserPref("security.allow_parent_unrestricted_js_loads");
     14 });
     15 
     16 add_task(async function () {
     17  // Test both CJS and JSM versions
     18 
     19  await testWorker();
     20  await testTransfer();
     21 });
     22 
     23 async function testWorker() {
     24  // Needed for blob:null
     25  Services.prefs.setBoolPref(
     26    "security.allow_parent_unrestricted_js_loads",
     27    true
     28  );
     29 
     30  const blob = new Blob(
     31    [
     32      `
     33 importScripts("resource://gre/modules/workers/require.js");
     34 const { createTask } = require("resource://devtools/shared/worker/helper.js");
     35 
     36 createTask(self, "groupByField", function({
     37  items,
     38  groupField
     39 }) {
     40  const groups = {};
     41  for (const item of items) {
     42    if (!groups[item[groupField]]) {
     43      groups[item[groupField]] = [];
     44    }
     45    groups[item[groupField]].push(item);
     46  }
     47  return { groups };
     48 });
     49    `,
     50    ],
     51    { type: "application/javascript" }
     52  );
     53 
     54  const WORKER_URL = URL.createObjectURL(blob);
     55  const worker = new DevToolsWorker(WORKER_URL);
     56 
     57  const results = await worker.performTask("groupByField", {
     58    items: [
     59      { name: "Paris", country: "France" },
     60      { name: "Lagos", country: "Nigeria" },
     61      { name: "Lyon", country: "France" },
     62    ],
     63    groupField: "country",
     64  });
     65 
     66  is(
     67    Object.keys(results.groups).join(","),
     68    "France,Nigeria",
     69    `worker should have returned the expected result`
     70  );
     71 
     72  URL.revokeObjectURL(WORKER_URL);
     73 
     74  const fn = workerify(x => x * x);
     75  is(await fn(5), 25, `workerify works`);
     76  fn.destroy();
     77 
     78  worker.destroy();
     79 }
     80 
     81 async function testTransfer() {
     82  Services.prefs.setBoolPref(
     83    "security.allow_parent_unrestricted_js_loads",
     84    true
     85  );
     86  const workerFn = workerify(({ buf }) => buf.byteLength);
     87  const buf = new ArrayBuffer(BUFFER_SIZE);
     88 
     89  is(
     90    buf.byteLength,
     91    BUFFER_SIZE,
     92    "Size of the buffer before transfer is correct."
     93  );
     94 
     95  is(await workerFn({ buf }), 8, "Sent array buffer to worker");
     96  is(buf.byteLength, 8, "Array buffer was copied, not transferred.");
     97 
     98  is(await workerFn({ buf }, [buf]), 8, "Sent array buffer to worker");
     99  is(buf.byteLength, 0, "Array buffer was transferred, not copied.");
    100 
    101  workerFn.destroy();
    102 }