summaryrefslogtreecommitdiff
path: root/examples/scala-js/sbt-plugin/src/main/scala/scala/scalajs/sbtplugin/env/nodejs/NodeJSEnv.scala
blob: dfabe23b8952257736e2430b3c62d40c4783f231 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*                     __                                               *\
**     ________ ___   / /  ___      __ ____  Scala.js sbt plugin        **
**    / __/ __// _ | / /  / _ | __ / // __/  (c) 2013, LAMP/EPFL        **
**  __\ \/ /__/ __ |/ /__/ __ |/_// /_\ \    http://scala-js.org/       **
** /____/\___/_/ |_/____/_/ | |__/ /____/                               **
**                          |/____/                                     **
\*                                                                      */


package scala.scalajs.sbtplugin.env.nodejs

import scala.scalajs.sbtplugin.env._
import scala.scalajs.sbtplugin.JSUtils.toJSstr

import scala.scalajs.tools.io._
import scala.scalajs.tools.classpath._
import scala.scalajs.tools.env._
import scala.scalajs.tools.jsdep._
import scala.scalajs.tools.logging._

import scala.scalajs.sbtplugin.JSUtils._

import java.io.{ Console => _, _ }
import java.net._

import scala.io.Source

class NodeJSEnv(
  nodejsPath: String = "node",
  addArgs:    Seq[String] = Seq.empty,
  addEnv:     Map[String, String] = Map.empty
) extends ExternalJSEnv(addArgs, addEnv) with ComJSEnv {

  protected def vmName: String = "node.js"
  protected def executable: String = nodejsPath

  override def jsRunner(classpath: CompleteClasspath, code: VirtualJSFile,
      logger: Logger, console: JSConsole): JSRunner = {
    new NodeRunner(classpath, code, logger, console)
  }

  override def asyncRunner(classpath: CompleteClasspath, code: VirtualJSFile,
      logger: Logger, console: JSConsole): AsyncJSRunner = {
    new AsyncNodeRunner(classpath, code, logger, console)
  }

  override def comRunner(classpath: CompleteClasspath, code: VirtualJSFile,
      logger: Logger, console: JSConsole): ComJSRunner = {
    new ComNodeRunner(classpath, code, logger, console)
  }

  protected class NodeRunner(classpath: CompleteClasspath,
      code: VirtualJSFile, logger: Logger, console: JSConsole
  ) extends ExtRunner(classpath, code, logger, console)
       with AbstractNodeRunner

  protected class AsyncNodeRunner(classpath: CompleteClasspath,
      code: VirtualJSFile, logger: Logger, console: JSConsole
  ) extends AsyncExtRunner(classpath, code, logger, console)
       with AbstractNodeRunner

  protected class ComNodeRunner(classpath: CompleteClasspath,
      code: VirtualJSFile, logger: Logger, console: JSConsole
  ) extends AsyncNodeRunner(classpath, code, logger, console)
       with ComJSRunner {

    /** Retry-timeout to wait for the JS VM to connect */
    private final val acceptTimeout = 1000

    private[this] val serverSocket =
      new ServerSocket(0, 0, InetAddress.getByName(null)) // Loopback address
    private[this] var comSocket: Socket = _
    private[this] var jvm2js: DataOutputStream = _
    private[this] var js2jvm: DataInputStream = _

    private def comSetup = new MemVirtualJSFile("comSetup.js").withContent(
      s"""
      (function() {
        // The socket for communication
        var socket = null;
        // The callback where received messages go
        var recvCallback = null;

        // Buffers received data
        var inBuffer = new Buffer(0);

        function onData(data) {
          inBuffer = Buffer.concat([inBuffer, data]);
          tryReadMsg();
        }

        function tryReadMsg() {
          if (inBuffer.length < 4) return;
          var msgLen = inBuffer.readInt32BE(0);
          var byteLen = 4 + msgLen * 2;

          if (inBuffer.length < byteLen) return;
          var res = "";

          for (var i = 0; i < msgLen; ++i)
            res += String.fromCharCode(inBuffer.readInt16BE(4 + i * 2));

          inBuffer = inBuffer.slice(byteLen);

          recvCallback(res);
        }

        global.scalajsCom = {
          init: function(recvCB) {
            if (socket !== null) throw new Error("Com already open");

            var net = require('net');
            recvCallback = recvCB;
            socket = net.connect(${serverSocket.getLocalPort});
            socket.on('data', onData);
          },
          send: function(msg) {
            if (socket === null) throw new Error("Com not open");

            var len = msg.length;
            var buf = new Buffer(4 + len * 2);
            buf.writeInt32BE(len, 0);
            for (var i = 0; i < len; ++i)
              buf.writeInt16BE(msg.charCodeAt(i), 4 + i * 2);
            socket.write(buf);
          },
          close: function() {
            if (socket === null) throw new Error("Com not open");
            socket.end();
          }
        }
      }).call(this);
      """
    )

    def send(msg: String): Unit = {
      if (awaitConnection()) {
        jvm2js.writeInt(msg.length)
        jvm2js.writeChars(msg)
        jvm2js.flush()
      }
    }

    def receive(): String = {
      if (!awaitConnection())
        throw new ComJSEnv.ComClosedException
      try {
        val len = js2jvm.readInt()
        val carr = Array.fill(len)(js2jvm.readChar())
        String.valueOf(carr)
      } catch {
        case e: EOFException =>
          throw new ComJSEnv.ComClosedException
      }
    }

    def close(): Unit = {
      serverSocket.close()
      if (jvm2js != null)
        jvm2js.close()
      if (js2jvm != null)
        js2jvm.close()
      if (comSocket != null)
        comSocket.close()
    }

    override def stop(): Unit = {
      close()
      super.stop()
    }

    /** Waits until the JS VM has established a connection or terminates
     *  @return true if the connection was established
     */
    private def awaitConnection(): Boolean = {
      serverSocket.setSoTimeout(acceptTimeout)
      while (comSocket == null && isRunning) {
        try {
          comSocket = serverSocket.accept()
          jvm2js = new DataOutputStream(
              new BufferedOutputStream(comSocket.getOutputStream()))
          js2jvm = new DataInputStream(
              new BufferedInputStream(comSocket.getInputStream()))
        } catch {
          case to: SocketTimeoutException =>
        }
      }

      comSocket != null
    }

    override protected def initFiles(): Seq[VirtualJSFile] =
      super.initFiles :+ comSetup

    override protected def finalize(): Unit = close()
  }

  protected trait AbstractNodeRunner extends AbstractExtRunner {

    protected[this] val libCache = new VirtualFileMaterializer(true)

    /** File(s) to automatically install source-map-support.
     *  Is used by [[initFiles]], override to change/disable.
     */
    protected def installSourceMap(): Seq[VirtualJSFile] = Seq(
        new MemVirtualJSFile("sourceMapSupport.js").withContent(
          """
          try {
            require('source-map-support').install();
          } catch (e) {}
          """
        )
    )

    /** File(s) to hack console.log to prevent if from changing `%%` to `%`.
     *  Is used by [[initFiles]], override to change/disable.
     */
    protected def fixPercentConsole(): Seq[VirtualJSFile] = Seq(
        new MemVirtualJSFile("nodeConsoleHack.js").withContent(
          """
          // Hack console log to duplicate double % signs
          (function() {
            var oldLog = console.log;
            var newLog = function() {
              var args = arguments;
              if (args.length >= 1 && args[0] !== void 0 && args[0] !== null) {
                args[0] = args[0].toString().replace(/%/g, "%%");
              }
              oldLog.apply(console, args);
            };
            console.log = newLog;
          })();
          """
        )
    )

    /** File(s) to define `__ScalaJSEnv`. Defines `exitFunction`.
     *  Is used by [[initFiles]], override to change/disable.
     */
    protected def runtimeEnv(): Seq[VirtualJSFile] = Seq(
        new MemVirtualJSFile("scalaJSEnvInfo.js").withContent(
          """
          __ScalaJSEnv = {
            exitFunction: function(status) { process.exit(status); }
          };
          """
        )
    )

    /** Concatenates results from [[installSourceMap]], [[fixPercentConsole]] and
     *  [[runtimeEnv]] (in this order).
     */
    override protected def initFiles(): Seq[VirtualJSFile] =
      installSourceMap() ++ fixPercentConsole() ++ runtimeEnv()

    /** Libraries are loaded via require in Node.js */
    override protected def getLibJSFiles(): Seq[VirtualJSFile] = {
      initFiles() ++
      classpath.jsLibs.map(requireLibrary) :+
      classpath.scalaJSCode
    }

    /** Rewrites a library virtual file to a require statement if possible */
    protected def requireLibrary(dep: ResolvedJSDependency): VirtualJSFile = {
      dep.info.commonJSName.fold(dep.lib) { varname =>
        val fname = dep.lib.name
        libCache.materialize(dep.lib)
        new MemVirtualJSFile(s"require-$fname").withContent(
          s"""$varname = require(${toJSstr(fname)});"""
        )
      }
    }

    // Send code to Stdin
    override protected def sendVMStdin(out: OutputStream): Unit = {
      sendJS(getJSFiles(), out)
    }

    /** write a single JS file to a writer using an include fct if appropriate
     *  uses `require` if the file exists on the filesystem
     */
    override protected def writeJSFile(file: VirtualJSFile,
        writer: Writer): Unit = {
      file match {
        case file: FileVirtualJSFile =>
          val fname = toJSstr(file.file.getAbsolutePath)
          writer.write(s"require($fname);\n")
        case _ =>
          super.writeJSFile(file, writer)
      }
    }

    // Node.js specific (system) environment
    override protected def getVMEnv(): Map[String, String] = {
      val baseNodePath = sys.env.get("NODE_PATH").filter(_.nonEmpty)
      val nodePath = libCache.cacheDir.getAbsolutePath +
        baseNodePath.fold("")(p => File.pathSeparator + p)

      sys.env ++ Seq(
          "NODE_MODULE_CONTEXTS" -> "0",
          "NODE_PATH" -> nodePath
      ) ++ additionalEnv
    }
  }

}