Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MongoClient import within Worker throws "Cannot find name 'window'" #248

Closed
carlosaviles opened this issue Sep 8, 2021 · 7 comments
Closed

Comments

@carlosaviles
Copy link
Contributor

carlosaviles commented Sep 8, 2021

import { MongoClient } from "https://deno.land/x/mongo@v0.24.0/mod.ts";

This import is causing the following error:

error: Uncaught (in worker "") TS2304 [ERROR]: Cannot find name 'window'.
this.nonce = window.crypto.getRandomValues(new Uint8Array(24));
~~~~~~
at https://deno.land/x/mongo@v0.24.0/src/auth/base.ts:34:18

Not sure whether this is a problem in deno_mongo or Deno itself.

@manyuanrong
Copy link
Member

I think your deno version is too low or does not give enough permissions

@carlosaviles
Copy link
Contributor Author

This happens using Deno 1.13.1

deno 1.13.1 (release, x86_64-apple-darwin)
v8 9.3.345.11
typescript 4.3.5

A simple worker.ts containing the import and a main.ts instantiating the worker causes the error. The command for running I use is:

deno run --allow-read --allow-net --unstable main.ts

@manyuanrong
Copy link
Member

Perhaps the reason is that crypto is temporarily unavailable in deno's webworker

@carlosaviles
Copy link
Contributor Author

If I remove the import and use crypto directly

crypto.getRandomValues(new Uint8Array(24))

then there is no error.

Accessing crypto through the global object is what is causing the problem when executed within a worker.

@carlosaviles
Copy link
Contributor Author

Digging in Deno's discussions in their GitHub, I seem to understand now what the problem is.

denoland/deno#650

They intentionally removed 'window' as a valid global object for Workers. The global object 'window' is only available for code executed in the main thread. Worker's threads can access 'globalThis' or 'self'.

There was much discussion about existing code being broken by that. I think this is what has happened here.

Would be a proper fix for this issue the removal of the reference to 'window' in the code? Or is this module intended to not be used in a worker thread?

@manyuanrong
Copy link
Member

I think we can use globaThis

@carlosaviles
Copy link
Contributor Author

Should I take care of the change and send a pr?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants