Skip to content

Doesn't work when using ESModule #8

Open
@fiatjaf

Description

@fiatjaf

How to reproduce

Given the following files:

// package.json
{
  "type": "module"
}
// build.sbt
enablePlugins(ScalaJSPlugin)

scalaVersion := "3.1.3"

libraryDependencies ++= Seq(
  ("org.scala-js" %%% "scalajs-java-securerandom" % "1.0.0").cross(CrossVersion.for3Use2_13)
)

scalaJSLinkerConfig ~= { _.withModuleKind(ModuleKind.ESModule) }

scalaJSUseMainModuleInitializer := tru
// src/main/scala/Main.scala

import java.security.SecureRandom
  
object Main {  
  def main(args: Array[String]): Unit = {  
    println(randomBytes(16).map(_.toInt).mkString("")) 
  }                                                    
                                         
  private val secureRandom = new SecureRandom
                                             
  def randomBytes(length: Int): Array[Byte] = { 
    val buffer = new Array[Byte](length)       
    secureRandom.nextBytes(buffer)             
    buffer                              
  }                               
}

Do

sbt fastLinkJS
node target/scala-3.1.3/*-fastopt/main.js

The result will be

file:///home/fiatjaf/comp/misc/target/scala-3.1.3/misc-fastopt/main.js:2716
  throw new $c_jl_UnsupportedOperationException("java.security.SecureRandom is not supported on this platform because it provides neither `crypto.getRandomValues` nor Node.js' \"crypto\" module.")
        ^

<ref *1> java.lang.UnsupportedOperationException: java.security.SecureRandom is not supported on this platform because it provides neither `crypto.getRandomValues` nor Node.js' "crypto" module.
    at $p_Ljava_security_SecureRandom$__notSupported__E (file:///home/fiatjaf/comp/misc/target/scala-3.1.3/misc-fastopt/main.js:2716:9)
    at $p_Ljava_security_SecureRandom$__getRandomValuesFun$lzycompute__sjs_js_Function1 (file:///home/fiatjaf/comp/misc/target/scala-3.1.3/misc-fastopt/main.js:2710:128)
    at $c_Ljava_security_SecureRandom$.java$security$SecureRandom$$getRandomValuesFun__sjs_js_Function1 (file:///home/fiatjaf/comp/misc/target/scala-3.1.3/misc-fastopt/main.js:2746:62)
    at new $c_Ljava_security_SecureRandom (file:///home/fiatjaf/comp/misc/target/scala-3.1.3/misc-fastopt/main.js:3323:94)
    at new $c_LMain$ (file:///home/fiatjaf/comp/misc/target/scala-3.1.3/misc-fastopt/main.js:904:33)
    at $m_LMain$ (file:///home/fiatjaf/comp/misc/target/scala-3.1.3/misc-fastopt/main.js:1024:17)
    at $s_LMain__main__AT__V (file:///home/fiatjaf/comp/misc/target/scala-3.1.3/misc-fastopt/main.js:898:3)
    at file:///home/fiatjaf/comp/misc/target/scala-3.1.3/misc-fastopt/main.js:5965:1
    at ModuleJob.run (node:internal/modules/esm/module_job:198:25)
    at async Promise.all (index 0) {
  jl_Throwable__f_s: 'java.security.SecureRandom is not supported on this platform because it provides neither `crypto.getRandomValues` nor Node.js\' "crypto" module.',
  jl_Throwable__f_e: null,
  jl_Throwable__f_enableSuppression: true,
  jl_Throwable__f_writableStackTrace: true,
  jl_Throwable__f_stackTraceStateInternal: [Circular *1],
  jl_Throwable__f_stackTrace: null,
  jl_Throwable__f_suppressed: null
}

Node.js v18.4.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions