-
Notifications
You must be signed in to change notification settings - Fork 3
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
Doesn't work when using ESModule
#8
Comments
It works if I manually put the object Main {
@js.native
@JSImport("crypto", JSImport.Namespace)
val crypto: js.Dynamic = js.native
private val g = scalajs.js.Dynamic.global.globalThis
g.crypto = crypto
... |
Thanks for the report. I don't know how to fix this while retaining compatibility with other scenarios.
The least bad solution I have so far would be use |
Apparently this cannot be reproduced through |
Possibly related to the VM thing used by the Node.js JSEnv? See also scala-js/scala-js-js-envs#20 (comment). |
We don't use the VM thing for ES modules. We import them through a dynamic |
Maybe leave it as it is and tell At least until ES2020 lands on more platforms and that thing you mentioned becomes less prone to breaking? |
If you don't mind expanding on this: hypothetically, what would this solution look like? I understand it's not helpful for Java It won't necessarily help us today, but the Web Crypto API is now available in global scope as of Node.js 19+. Earlier versions can also enable this behavior with https://nodejs.org/en/blog/announcements/v19-release-announce/#stable-webcrypto That would mean that a single API can be used for all platforms and the feature-test would no longer be needed, which I believe would solve this issue. |
I think it even means that the existing release of scalajs-java-securerandom will work out of the box in Node.js 19, even with the ES module setup of this issue. The first thing it tries is the global
Something like import scala.concurrent.ExecutionContext.Implicits.global
import scala.scalajs.js.Thenable.Implicits._
import scala.scalajs.js.JSConverters._
def randomBytes(n: Int): Future[Array[Byte]] = {
for (cryptoMod <- js.`import`("crypto").toFuture) yield {
val ta = new Int8Array(n)
cryptoMod.randomFillSync(ta)
ta.toArray
}
} (not tested nor even compiled; expect changes to be required) |
How to reproduce
Given the following files:
Do
The result will be
The text was updated successfully, but these errors were encountered: