Skip to content

Commit c9124dc

Browse files
committed
Add support for npm packages in PackageHub
Previously, PackageHub only supported Maven and JDK packages. Now, PackageHub can additionally serve sources of npm packages. This makes it possible to LSIF index the sources of npm packages similar to how VS Code and IntelliJ index the `node_modules/` directory.
1 parent 42069f4 commit c9124dc

File tree

6 files changed

+273
-62
lines changed

6 files changed

+273
-62
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ RUN git config --global user.name "Your Name"
66
RUN git config --global http.postBuffer 1048576000
77
RUN curl -L https://sourcegraph.com/.api/src-cli/src_linux_amd64 -o /src
88
RUN chmod +x /src
9-
RUN /coursier bootstrap -r sonatype:snapshots com.sourcegraph:packagehub_2.13:0.5.1-26-2d4609cc-SNAPSHOT -o /packagehub
9+
RUN /coursier bootstrap -r sonatype:snapshots com.sourcegraph:packagehub_2.13:0.5.1-43-da43fdbb-SNAPSHOT -o /packagehub
1010
ENV COURSIER_REPOSITORIES=central|https://maven.google.com/|jitpack
1111
CMD ["/packagehub.sh"]

auto-indexing/Dockerfile

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
FROM gradle:7.0.0-jdk8@sha256:25ecb23e3516b697219f5a780bf68598318cf13b35ca616f4521c7e9e115a11e
2-
RUN apt-get update && \
3-
apt-get install --yes maven && \
4-
curl -fLo /coursier https://git.io/coursier-cli && \
5-
chmod +x /coursier && \
6-
/coursier launch --contrib lsif-java -- --help
2+
RUN apt-get update
3+
RUN apt-get install --yes maven npm
4+
RUN curl -fLo /coursier https://git.io/coursier-cli
5+
RUN chmod +x /coursier
6+
RUN apt-get install --yes yarn
7+
RUN /coursier launch --contrib lsif-java -- --help

build.sbt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ lazy val packagehub = project
229229
"com.zaxxer" % "HikariCP" % "4.0.3",
230230
"org.flywaydb" % "flyway-core" % "7.7.1",
231231
"org.postgresql" % "postgresql" % "42.2.14",
232+
"org.rauschig" % "jarchivelib" % "1.1.0",
232233
"org.scalameta" %% "scalameta" % V.scalameta,
233234
"com.lihaoyi" %% "cask" % "0.7.8"
234235
)

packagehub/src/main/scala/com/sourcegraph/packagehub/Package.scala

Lines changed: 64 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ sealed abstract class Package(
3131
def relativePath: Path = Paths.get(path)
3232
}
3333
object Package {
34+
def npm(name: String, version: String): NpmPackage = {
35+
NpmPackage(name, version)
36+
}
3437
def jdk(version: String): JdkPackage = {
3538
JdkPackage(version)
3639
}
@@ -49,6 +52,8 @@ object Package {
4952
case s"maven:$library" =>
5053
val Right(dep) = Dependencies.parseDependencyEither(library)
5154
MavenPackage(dep)
55+
case s"npm:$name:$version" =>
56+
NpmPackage(name, version)
5257
}
5358
}
5459
def fromPath(path: List[String]): Option[(Package, List[String])] =
@@ -57,19 +62,37 @@ object Package {
5762
Some(Package.maven(org, name, version) -> requestPath)
5863
case "jdk" :: version :: requestPath =>
5964
Some(Package.jdk(version) -> requestPath)
65+
case "npm" :: GitRequestPrefix(parts, requestPath) =>
66+
val name = parts.init.mkString("/")
67+
val version = parts.last
68+
val actualName =
69+
if (name.startsWith("-"))
70+
"@" + name.stripPrefix("-")
71+
else
72+
name
73+
Some(Package.npm(actualName, version) -> requestPath)
6074
case _ =>
6175
None
6276
}
77+
78+
object GitRequestPrefix {
79+
private val suffixes = List(
80+
".git" :: "info" :: "refs" :: Nil,
81+
"info" :: "refs" :: Nil,
82+
".git" :: "git-upload-pack" :: Nil,
83+
"git-upload-pack" :: Nil
84+
)
85+
def unapply(path: List[String]): Option[(List[String], List[String])] = {
86+
suffixes.find(path.endsWith) match {
87+
case Some(suffix) =>
88+
Some(path.dropRight(suffix.length) -> suffix)
89+
case None =>
90+
None
91+
}
92+
}
93+
}
6394
def fromString(value: String, coursier: String): Either[String, Package] = {
6495
value match {
65-
case s"jdk:$version" =>
66-
val exit = os
67-
.proc(coursier, "java-home", "--jvm", version)
68-
.call(check = false)
69-
if (exit.exitCode == 0)
70-
Right(JdkPackage(version))
71-
else
72-
Left(exit.out.trim())
7396
case s"maven:$library" =>
7497
try {
7598
val deps = Dependencies
@@ -85,6 +108,29 @@ object Package {
85108
case NonFatal(e) =>
86109
Left(e.getMessage())
87110
}
111+
case s"jdk:$version" =>
112+
val exit = os
113+
.proc(coursier, "java-home", "--jvm", version)
114+
.call(check = false)
115+
if (exit.exitCode == 0)
116+
Right(JdkPackage(version))
117+
else
118+
Left(exit.out.trim())
119+
case s"npm:$name:$version" =>
120+
try {
121+
val out = os
122+
.proc("npm", "info", s"$name@$version")
123+
.call(check = false)
124+
.out
125+
.trim()
126+
if (out.nonEmpty)
127+
Right(NpmPackage(name, version))
128+
else
129+
Left(s"no such npm package: $name@$version")
130+
} catch {
131+
case NonFatal(e) =>
132+
Left(e.getMessage)
133+
}
88134
case other =>
89135
Left(
90136
s"unsupported package '$other'. To fix this problem, use a valid syntax " +
@@ -120,3 +166,13 @@ case class JdkPackage(override val version: String)
120166
extends Package(s"jdk:${version}", s"jdk/${version}", version) {
121167
def repr = id.stripPrefix("jdk:")
122168
}
169+
170+
case class NpmPackage(packageName: String, override val version: String)
171+
extends Package(
172+
s"npm:$packageName:$version",
173+
s"npm/$packageName/$version".replace("@", "-"),
174+
version
175+
) {
176+
def npmName = s"$packageName@$version"
177+
def tarballFilename = s"$packageName-$version.tgz".replace('/', '-')
178+
}

0 commit comments

Comments
 (0)