Skip to content

Commit

Permalink
fix: Using globalThis instead of window
Browse files Browse the repository at this point in the history
* fix: Using globalThis instead of window.

* Add test for worker thread importing deno_mongo.

Co-authored-by: silence <silence_zhpf@aliyun.com>
Co-authored-by: ERFANIUM <erfanshield@outlook.com>
  • Loading branch information
3 people authored Oct 4, 2021
1 parent eda9e9c commit a152fb5
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/auth/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ export class AuthContext {
this.protocol = protocol;
this.credentials = credentials;
this.options = options;
this.nonce = window.crypto.getRandomValues(new Uint8Array(24));
this.nonce = globalThis.crypto.getRandomValues(new Uint8Array(24));
}
}
29 changes: 29 additions & 0 deletions tests/cases/07_worker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { assert } from "../test.deps.ts";

export default function workerTests() {
Deno.test({
name: "WORKER: Deno does not throw when deno_mongo is imported in worker",
fn: async function () {
let workerFinished: (p: void | PromiseLike<void>) => void;

const p = new Promise<void>((resolve, _reject) => {
workerFinished = resolve;
});

const importWorker = new Worker(
new URL("import_worker.ts", import.meta.url).href,
{ type: "module" },
);

importWorker.onmessage = (_e) => {
workerFinished();
};

importWorker.postMessage("startWorker");

await p;

assert(true);
},
});
}
6 changes: 6 additions & 0 deletions tests/cases/import_worker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import {} from "../../mod.ts";

onmessage = (_e) => {
self.postMessage("done");
self.close();
};
3 changes: 2 additions & 1 deletion tests/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import connectTests from "./cases/02_connect.ts";
import curdTests from "./cases/03_curd.ts";
import indexesTests from "./cases/04_indexes.ts";
import srvTests from "./cases/05_srv.ts";
import workerTests from "./cases/07_worker.ts";
import gridfsTests from "./cases/06_gridfs.ts";

import cleanup from "./cases/99_cleanup.ts";

uriTests();
Expand All @@ -15,5 +15,6 @@ curdTests();
gridfsTests();
indexesTests();
srvTests();
workerTests();

cleanup();

0 comments on commit a152fb5

Please sign in to comment.