Skip to content

Commit b9b4313

Browse files
authored
implement server mode, same as joern frontends (#368)
1 parent d7a106f commit b9b4313

File tree

6 files changed

+67
-7
lines changed

6 files changed

+67
-7
lines changed

build.sbt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
val cpgVersion = "1.7.37"
2-
val joernVersion = "4.0.384"
1+
val cpgVersion = "1.7.49"
2+
val joernVersion = "4.0.444"
33

44
val gitCommitString = SettingKey[String]("gitSha")
55

js2cpg.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env sh
22

33
SCRIPT_ABS_PATH=$(readlink -f "$0")
4-
SCRIPT_ABS_DIR=$(dirname $SCRIPT_ABS_PATH)
4+
SCRIPT_ABS_DIR=$(dirname "$SCRIPT_ABS_PATH")
55

6-
$SCRIPT_ABS_DIR/target/universal/stage/bin/js2cpg -Dlog4j.configurationFile=$SCRIPT_ABS_DIR/src/main/resources/log4j2.xml $@
6+
exec "$SCRIPT_ABS_DIR/target/universal/stage/bin/js2cpg" "-Dlog4j.configurationFile=$SCRIPT_ABS_DIR/src/main/resources/log4j2.xml" "$@"

src/main/scala/io/shiftleft/js2cpg/core/Config.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ object Config {
3434
val DEFAULT_WITH_NODE_MODULES_FOLDER: Boolean = false
3535
val DEFAULT_OPTIMIZE_DEPENDENCIES: Boolean = true
3636
val DEFAULT_FIXED_TRANSPILATION_DEPENDENCIES: Boolean = false
37+
val DEFAULT_SERVER_MODE: Boolean = false
3738

3839
}
3940

@@ -59,7 +60,9 @@ case class Config(
5960
moduleMode: Option[String] = Config.DEFAULT_MODULE_MODE,
6061
withNodeModuleFolder: Boolean = Config.DEFAULT_WITH_NODE_MODULES_FOLDER,
6162
optimizeDependencies: Boolean = Config.DEFAULT_OPTIMIZE_DEPENDENCIES,
62-
fixedTranspilationDependencies: Boolean = Config.DEFAULT_FIXED_TRANSPILATION_DEPENDENCIES
63+
fixedTranspilationDependencies: Boolean = Config.DEFAULT_FIXED_TRANSPILATION_DEPENDENCIES,
64+
serverMode: Boolean = Config.DEFAULT_SERVER_MODE,
65+
serverTimeoutSeconds: Option[Int] = None
6366
) {
6467

6568
def createPathForPackageJson(): Path = Paths.get(packageJsonLocation) match {

src/main/scala/io/shiftleft/js2cpg/core/Js2CpgMain.scala

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,30 @@
11
package io.shiftleft.js2cpg.core
22

3+
import io.joern.x2cpg.utils.server.FrontendHTTPServer
4+
35
object Js2CpgMain {
46
def main(args: Array[String]): Unit = {
57
val argumentsParser = new Js2cpgArgumentsParser()
68
argumentsParser.parse(args) match {
9+
case Some(config) if config.serverMode =>
10+
val server: FrontendHTTPServer = new FrontendHTTPServer(
11+
FrontendHTTPServer.defaultExecutor(),
12+
arguments => {
13+
val config = argumentsParser.parse(arguments)
14+
config.foreach(Js2Cpg().run(_))
15+
}
16+
)
17+
18+
val port = server.startup()
19+
println(s"FrontendHTTPServer started on port $port")
20+
try {
21+
config.serverTimeoutSeconds match {
22+
case Some(value) => server.stopServerAfterTimeout(value)
23+
case None => Thread.sleep(Long.MaxValue)
24+
}
25+
} finally {
26+
server.stop()
27+
}
728
case Some(config) =>
829
new Js2Cpg().run(config)
930
case None =>

src/main/scala/io/shiftleft/js2cpg/core/Js2cpgArgumentsParser.scala

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ object Js2cpgArgumentsParser {
4141
val OPTIMIZE_DEPENDENCIES: String = "optimize-dependencies"
4242
val ALL_DEPENDENCIES: String = "all-dependencies"
4343
val FIXED_TRANSPILATION_DEPENDENCIES: String = "fixed-transpilation-dependencies"
44+
val SERVER_MODE: String = "server"
45+
val SERVER_MODE_TIMEOUT_MINUTES: String = "server-timeout-minutes"
46+
val ENABLE_EARLY_SCHEMA_CHECKING: String = "enable-early-schema-checking"
4447
}
4548

4649
class Js2cpgArgumentsParser {
@@ -203,6 +206,15 @@ class Js2cpgArgumentsParser {
203206
)
204207
.action((module, c) => c.copy(moduleMode = Some(module)))
205208
.hidden()
209+
opt[Unit](SERVER_MODE)
210+
.action((module, c) => c.copy(serverMode = true))
211+
.hidden()
212+
.text("runs this frontend in server mode (disabled by default)")
213+
opt[Int](SERVER_MODE_TIMEOUT_MINUTES)
214+
.action((minutes, c) => c.copy(serverTimeoutSeconds = Some(minutes * 60)))
215+
.hidden()
216+
.text("timeout after which the server should terminate (use with --server)")
217+
opt[Unit](ENABLE_EARLY_SCHEMA_CHECKING).text("does nothing, just here for compat with joern frontends").hidden()
206218
}
207219

208220
def parse(args: Array[String]): Option[Config] = parser.parse(args, Config())

updateDependencies.sh

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,28 @@ check_installed() {
1111

1212
check_installed curl
1313

14+
# macOS is known to ship 'bash' version < 4
15+
if [[ "$OSTYPE" == "darwin"* ]]; then
16+
BASH_VERSION=`bash --version | grep "GNU bash, version " | awk '{print $4}' | cut -d. -f1`
17+
if [[ $BASH_VERSION -lt 4 ]]; then
18+
echo "error: 'bash' version detected is less than 4"
19+
if [ "$NON_INTERACTIVE_OPTION" == "--non-interactive" ]
20+
then
21+
echo "update 'bash' using 'brew install bash'? [Y/n]"
22+
read ANSWER
23+
if [ -z $ANSWER ] || [ "y" == $ANSWER ] || [ "Y" == $ANSWER ]
24+
then
25+
brew install bash
26+
else
27+
exit 1
28+
fi
29+
else
30+
echo "error: Please upgrade bash version and re-run"
31+
exit 1
32+
fi
33+
fi
34+
fi
35+
1436
# check if xmllint is installed
1537
if type xmllint > /dev/null; then
1638
USE_XMLLINT=1 #true
@@ -58,13 +80,13 @@ function update {
5880
if [ "$NON_INTERACTIVE_OPTION" == "--non-interactive" ]
5981
then
6082
echo "non-interactive mode, auto-updating $NAME: $OLD_VERSION -> $VERSION"
61-
sed -i "s/$SEARCH/$REPLACE/" build.sbt
83+
sed -i'.bak' "s/$SEARCH/$REPLACE/" build.sbt
6284
else
6385
echo "update $NAME: $OLD_VERSION -> $VERSION? [Y/n]"
6486
read ANSWER
6587
if [ -z $ANSWER ] || [ "y" == $ANSWER ] || [ "Y" == $ANSWER ]
6688
then
67-
sed -i "s/$SEARCH/$REPLACE/" build.sbt
89+
sed -i'.bak' "s/$SEARCH/$REPLACE/" build.sbt
6890
fi
6991
fi
7092
fi
@@ -77,3 +99,5 @@ else
7799
DEPENDENCY="${DEPENDENCY#--only=}"
78100
update $DEPENDENCY
79101
fi
102+
103+
rm -f build.sbt.bak # Remove back-up files generated by 'sed'

0 commit comments

Comments
 (0)