Skip to content

Commit

Permalink
Add support of scope in CDN. Fix #24
Browse files Browse the repository at this point in the history
  • Loading branch information
PhantomYdn committed Mar 15, 2024
1 parent 665651d commit a2924b0
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
32 changes: 26 additions & 6 deletions jnpm/src/main/java/org/orienteer/jnpm/cdn/CDNRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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("?");
Expand All @@ -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() {
Expand All @@ -77,16 +83,30 @@ public VersionInfo resolveVersion(Map<String, VersionInfo> 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+"'");
}
Expand Down
9 changes: 9 additions & 0 deletions jnpm/src/test/java/org/orienteer/jnpm/cdn/CDNTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ public void testParsing() {
assertEquals("file.js", reqInfo.getFileName());
assertTrue(reqInfo.isExactVersion());

reqInfo = CDNRequest.valueOf("/@test/[email protected]/path/to/the/file.js");
assertEquals("@test", reqInfo.getScope());
assertEquals("vue", reqInfo.getPackageName());
assertEquals("2.6.11", reqInfo.getVersionExpression());
assertEquals("@test/[email protected]", 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());
Expand Down

0 comments on commit a2924b0

Please sign in to comment.