-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPhantomJSEnv.scala
335 lines (286 loc) · 11.2 KB
/
PhantomJSEnv.scala
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/* __ *\
** ________ ___ / / ___ __ ____ PhantomJS support for Scala.js **
** / __/ __// _ | / / / _ | __ / // __/ (c) 2013-2017, LAMP/EPFL **
** __\ \/ /__/ __ |/ /__/ __ |/_// /_\ \ https://www.scala-js.org/ **
** /____/\___/_/ |_/____/_/ | |__/ /____/ **
** |/____/ **
\* */
package org.scalajs.jsenv.phantomjs
import scala.util.control.NonFatal
import java.io._
import java.net._
import java.nio.charset.StandardCharsets.UTF_8
import java.nio.file.{Files, Path, StandardCopyOption}
import com.google.common.jimfs.Jimfs
import org.scalajs.jsenv._
import org.scalajs.jsenv.JSUtils.escapeJS
final class PhantomJSEnv(config: PhantomJSEnv.Config) extends JSEnv {
import PhantomJSEnv._
def this() = this(PhantomJSEnv.Config())
val name: String = "PhantomJS"
def start(input: Seq[Input], runConfig: RunConfig): JSRun = {
PhantomJSEnv.validator.validate(runConfig)
val scripts = validateInput(input)
internalStart(initFiles ::: scripts, runConfig)
}
def startWithCom(input: Seq[Input], runConfig: RunConfig,
onMessage: String => Unit): JSComRun = {
PhantomJSEnv.validator.validate(runConfig)
val scripts = validateInput(input)
ComRun.start(config.jettyClassLoader, runConfig, onMessage) { comSetup =>
internalStart(comSetup :: initFiles ::: scripts, runConfig)
}
}
private def validateInput(input: Seq[Input]): List[Path] = {
input.map {
case Input.Script(script) =>
script
case _ =>
throw new UnsupportedInputException(input)
}.toList
}
private def internalStart(files: List[Path],
runConfig: RunConfig): JSRun = {
try {
val launcherFile = createTmpLauncherFile(files, runConfig)
val command =
config.executable :: config.args ::: launcherFile.getAbsolutePath :: Nil
val externalConfig = ExternalJSRun.Config()
.withEnv(config.env)
.withRunConfig(runConfig)
ExternalJSRun.start(command, externalConfig)(_.close())
} catch {
case NonFatal(t) =>
JSRun.failed(t)
}
}
/**
* PhantomJS doesn't support Function.prototype.bind. We polyfill it.
* https://github.com/ariya/phantomjs/issues/10522
*/
private def initFiles: List[Path] = List(
// scalastyle:off line.size.limit
Utils.createMemFile(
"bindPolyfill.js",
"""
|// Polyfill for Function.bind from Facebook react:
|// https://github.com/facebook/react/blob/3dc10749080a460e48bee46d769763ec7191ac76/src/test/phantomjs-shims.js
|// Originally licensed under Apache 2.0
|(function() {
|
| var Ap = Array.prototype;
| var slice = Ap.slice;
| var Fp = Function.prototype;
|
| if (!Fp.bind) {
| // PhantomJS doesn't support Function.prototype.bind natively, so
| // polyfill it whenever this module is required.
| Fp.bind = function(context) {
| var func = this;
| var args = slice.call(arguments, 1);
|
| function bound() {
| var invokedAsConstructor = func.prototype && (this instanceof func);
| return func.apply(
| // Ignore the context parameter when invoking the bound function
| // as a constructor. Note that this includes not only constructor
| // invocations using the new keyword but also calls to base class
| // constructors such as BaseClass.call(this, ...) or super(...).
| !invokedAsConstructor && context || this,
| args.concat(slice.call(arguments))
| );
| }
|
| // The bound function must share the .prototype of the unbound
| // function so that any object created by one constructor will count
| // as an instance of both constructors.
| bound.prototype = func.prototype;
|
| return bound;
| };
| }
|
|})();
|""".stripMargin
)
// scalastyle:on line.size.limit
)
protected def createTmpLauncherFile(scripts: List[Path],
runConfig: RunConfig): File = {
val webF = createTmpWebpage(scripts, runConfig)
val launcherTmpF = File.createTempFile("phantomjs-launcher", ".js")
launcherTmpF.deleteOnExit()
val out = new FileWriter(launcherTmpF)
try {
out.write(
s"""// Scala.js Phantom.js launcher
|if (!console.error)
| console.error = console.log;
|var page = require('webpage').create();
|var url = "${escapeJS(fixFileURI(webF.toURI).toASCIIString)}";
|page.onConsoleMessage = function(msg) {
| console.log(msg);
|};
|page.onError = function(msg, trace) {
| console.error(msg);
| if (trace && trace.length) {
| console.error('');
| trace.forEach(function(t) {
| console.error(' ' + t.file + ':' + t.line +
| (t.function ? ' (in function "' + t.function +'")' : ''));
| });
| }
|
| phantom.exit(2);
|};
|page.onCallback = function(data) {
| if (!data.action) {
| console.error('Called callback without action');
| phantom.exit(3);
| } else if (data.action === 'exit') {
| phantom.exit(data.returnValue || 0);
| } else {
| console.error('Unknown callback action ' + data.action);
| phantom.exit(4);
| }
|};
|page.open(url, function (status) {
| if (status !== 'success')
| phantom.exit(1);
|});
|""".stripMargin)
} finally {
out.close()
}
runConfig.logger.debug(
"PhantomJS using launcher at: " + launcherTmpF.getAbsolutePath())
launcherTmpF
}
protected def createTmpWebpage(scripts: List[Path],
runConfig: RunConfig): File = {
val webTmpF = File.createTempFile("phantomjs-launcher-webpage", ".html")
webTmpF.deleteOnExit()
val out = new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(webTmpF), "UTF-8"))
try {
writeWebpageLauncher(out, scripts)
} finally {
out.close()
}
runConfig.logger.debug(
"PhantomJS using webpage launcher at: " + webTmpF.getAbsolutePath())
webTmpF
}
protected def writeWebpageLauncher(out: Writer,
scripts: List[Path]): Unit = {
out.write(s"""<html><head>
<title>Phantom.js Launcher</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />""")
val hackedScriptsFS = Jimfs.newFileSystem()
// Enhance scripts with an awesome hack to detect syntax errors
val scriptsAndSyntaxErrorHacks = scripts.zipWithIndex.flatMap {
case (script, index) =>
val hackedScriptOutputStream = new ByteArrayOutputStream()
Utils.pipeInputStreamToOutputStream(Files.newInputStream(script),
hackedScriptOutputStream)
val hackedScriptCode =
s"\n;\nvar SCALAJS_PHANTOMJS_SYNTAXERROR_HACK_$index = true;\n"
hackedScriptOutputStream.write(hackedScriptCode.getBytes(UTF_8))
val hackedScript = hackedScriptsFS.getPath(script.toString())
if (hackedScript.getParent() != null)
Files.createDirectories(hackedScript.getParent())
Files.write(hackedScript, hackedScriptOutputStream.toByteArray())
val checkScript = Utils.createMemFile(
s"checkSyntaxError$index.js",
s"""
|if (typeof SCALAJS_PHANTOMJS_SYNTAXERROR_HACK_$index === 'undefined')
| throw new SyntaxError("Syntax error in ${escapeJS(script.toString())}");
""".stripMargin)
List(hackedScript, checkScript)
}
for (script <- scriptsAndSyntaxErrorHacks) {
val scriptURI = tmpFile(script.toString(), Files.newInputStream(script))
val fname = htmlEscape(fixFileURI(scriptURI).toASCIIString)
out.write(
s"""<script type="text/javascript" src="$fname"></script>""" + "\n")
}
out.write(s"</head>\n<body></body>\n</html>\n")
}
protected def htmlEscape(str: String): String = str.flatMap {
case '<' => "<"
case '>' => ">"
case '"' => """
case '&' => "&"
case c => c.toString
}
/** Adds an empty authority to URIs with the "file" scheme without authority.
* Some browsers don't fetch URIs without authority correctly.
*/
private def fixFileURI(uri: URI): URI =
if (uri.getScheme() != "file" || uri.getAuthority() != null) uri
else new URI("file", "", uri.getPath(), uri.getQuery(), uri.getFragment())
}
object PhantomJSEnv {
private final val launcherName = "scalaJSPhantomJSEnvLauncher"
private lazy val validator = ExternalJSRun.supports(RunConfig.Validator())
// tmpSuffixRE and tmpFile copied from HTMLRunnerBuilder.scala in Scala.js
private val tmpSuffixRE = """[a-zA-Z0-9-_.]*$""".r
private def tmpFile(path: String, in: InputStream): URI = {
try {
/* - createTempFile requires a prefix of at least 3 chars
* - we use a safe part of the path as suffix so the extension stays (some
* browsers need that) and there is a clue which file it came from.
*/
val suffix = tmpSuffixRE.findFirstIn(path).orNull
val f = File.createTempFile("tmp-", suffix)
f.deleteOnExit()
Files.copy(in, f.toPath(), StandardCopyOption.REPLACE_EXISTING)
f.toURI()
} finally {
in.close()
}
}
final class Config private (
val executable: String,
val args: List[String],
val env: Map[String, String],
val jettyClassLoader: ClassLoader
) {
private def this() = {
this(
executable = "phantomjs",
args = Nil,
env = Map.empty,
jettyClassLoader = null
)
}
def withExecutable(executable: String): Config =
copy(executable = executable)
def withArgs(args: List[String]): Config =
copy(args = args)
def withEnv(env: Map[String, String]): Config =
copy(env = env)
def withJettyClassLoader(jettyClassLoader: ClassLoader): Config =
copy(jettyClassLoader = jettyClassLoader)
private def copy(
executable: String = executable,
args: List[String] = args,
env: Map[String, String] = env,
jettyClassLoader: ClassLoader = jettyClassLoader
): Config = {
new Config(executable, args, env, jettyClassLoader)
}
}
object Config {
/** Returns a default configuration for a [[PhantomJSEnv]].
*
* The defaults are:
*
* - `executable`: `"phantomjs"`
* - `args`: `Nil`
* - `env`: `Map.empty`
* - `jettyClassLoader`: `null` (will use the current class loader)
*/
def apply(): Config = new Config()
}
}