From a2924b043a88a26338c96c84f8d11057a166d622 Mon Sep 17 00:00:00 2001 From: Ilia Naryzhny Date: Thu, 14 Mar 2024 21:16:10 -0700 Subject: [PATCH] Add support of scope in CDN. Fix #24 --- .../org/orienteer/jnpm/cdn/CDNRequest.java | 32 +++++++++++++++---- .../java/org/orienteer/jnpm/cdn/CDNTest.java | 9 ++++++ 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/jnpm/src/main/java/org/orienteer/jnpm/cdn/CDNRequest.java b/jnpm/src/main/java/org/orienteer/jnpm/cdn/CDNRequest.java index fbf7a92..8ee0791 100644 --- a/jnpm/src/main/java/org/orienteer/jnpm/cdn/CDNRequest.java +++ b/jnpm/src/main/java/org/orienteer/jnpm/cdn/CDNRequest.java @@ -20,9 +20,10 @@ public class CDNRequest { private static final VersionInfo NULL_VERSION = new VersionInfo(); - private static final String PATH_PATTERN = "/([^/@]*)@?([^/]*)/(.*)"; + private static final String PATH_PATTERN = "/((@[^/]*)/)?([^/@]*)@?([^/]*)/(.*)"; private static final Pattern PATH_REGEXP = Pattern.compile(PATH_PATTERN); + private String scope; private String packageName; private String versionExpression; private String path; @@ -34,7 +35,8 @@ public class CDNRequest { @NonFinal private Boolean forceDownload; - protected CDNRequest(String packageName, String version, String path) { + protected CDNRequest(String scope, String packageName, String version, String path) { + this.scope = scope; this.packageName = packageName; this.versionExpression = version!=null && !version.isEmpty()?version:"latest"; int indx = path.indexOf("?"); @@ -51,7 +53,11 @@ public boolean isExactVersion() { } public String getPackageVersionExpression() { - return packageName+"@"+versionExpression; + if(scope!=null) { + return scope+"/"+packageName+"@"+versionExpression; + } else { + return packageName+"@"+versionExpression; + } } public CDNRequest forceDownload() { @@ -77,16 +83,30 @@ public VersionInfo resolveVersion(Map versionsCache) { } public static CDNRequest valueOf(String packageInfo, String filePath) { - int indx = packageInfo.indexOf("@"); + String scope = null; + if(packageInfo.startsWith("@")) { + int indx = packageInfo.indexOf('/'); + if(indx>0) { + scope = packageInfo.substring(0, indx); + packageInfo = packageInfo.substring(indx+1); + } else { + throw new IllegalArgumentException("Scoped Package Info '"+packageInfo+"' doesn't have package name"); + } + } + return valueOf(scope, packageInfo, filePath); + } + + public static CDNRequest valueOf(String scope, String packageInfo, String filePath) { + int indx = packageInfo.indexOf('@'); String pck = indx<0?packageInfo:packageInfo.substring(0, indx); String version = indx<0?packageInfo.substring(indx+1):null; - return new CDNRequest(pck, version, filePath); + return new CDNRequest(scope, pck, version, filePath); } public static CDNRequest valueOf(String fullPath) { Matcher matcher = PATH_REGEXP.matcher(fullPath); if(matcher.matches()) { - return new CDNRequest(matcher.group(1), matcher.group(2), matcher.group(3)); + return new CDNRequest(matcher.group(2), matcher.group(3), matcher.group(4), matcher.group(5)); } else { throw new IllegalArgumentException("Path '"+fullPath+"' should corresponds pattern '"+PATH_PATTERN+"'"); } diff --git a/jnpm/src/test/java/org/orienteer/jnpm/cdn/CDNTest.java b/jnpm/src/test/java/org/orienteer/jnpm/cdn/CDNTest.java index 820545a..29c9cbd 100644 --- a/jnpm/src/test/java/org/orienteer/jnpm/cdn/CDNTest.java +++ b/jnpm/src/test/java/org/orienteer/jnpm/cdn/CDNTest.java @@ -40,6 +40,15 @@ public void testParsing() { assertEquals("file.js", reqInfo.getFileName()); assertTrue(reqInfo.isExactVersion()); + reqInfo = CDNRequest.valueOf("/@test/vue@2.6.11/path/to/the/file.js"); + assertEquals("@test", reqInfo.getScope()); + assertEquals("vue", reqInfo.getPackageName()); + assertEquals("2.6.11", reqInfo.getVersionExpression()); + assertEquals("@test/vue@2.6.11", reqInfo.getPackageVersionExpression()); + assertEquals("path/to/the/file.js", reqInfo.getPath()); + assertEquals("file.js", reqInfo.getFileName()); + assertTrue(reqInfo.isExactVersion()); + reqInfo = CDNRequest.valueOf("/vue@~2.6.11/path/to/the/file2.js"); assertEquals("vue", reqInfo.getPackageName()); assertEquals("~2.6.11", reqInfo.getVersionExpression());