Skip to content

Commit fd7d03c

Browse files
authored
Merge pull request #1366 from ligangty/npm
Handle scopedPath case for npm tarball replacement
2 parents d8b0ec0 + 5975c6e commit fd7d03c

3 files changed

Lines changed: 134 additions & 10 deletions

File tree

addons/pkg-npm/common/src/main/java/org/commonjava/indy/pkg/npm/content/DecoratorUtils.java

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ private static String getPath( String url ) throws IOException
6767
throw new IOException( "Failed to parse URL " + url, e ); // should not happen
6868
}
6969

70-
String[] pathParts = url1.getPath().split( "\\/" );
70+
String[] pathParts = url1.getPath().split( "/" );
7171
if ( pathParts.length < 1 )
7272
{
7373
return "";
@@ -80,12 +80,36 @@ else if ( pathParts.length == 1 )
8080
String lastPart = pathParts[pathParts.length - 1];
8181
if ( ( "package.json".equals( lastPart ) || lastPart.endsWith( "tgz" ) ) && pathParts.length > 2 )
8282
{
83-
return String.format( "%s/%s/%s", pathParts[pathParts.length - 3], pathParts[pathParts.length - 2],
83+
final String firstPath;
84+
//Handle if scopedPath like "@types/jquery/***" or singlePath like "jquery/***"
85+
if ( pathParts.length > 3 && pathParts[pathParts.length - 4].startsWith( "@" ) )
86+
{
87+
// scoped path
88+
firstPath = String.format( "%s/%s", pathParts[pathParts.length - 4], pathParts[pathParts.length - 3] );
89+
}
90+
else
91+
{
92+
// single path
93+
firstPath = pathParts[pathParts.length - 3];
94+
}
95+
return String.format( "%s/%s/%s", firstPath, pathParts[pathParts.length - 2],
8496
pathParts[pathParts.length - 1] );
8597
}
8698
else if ( "-".equals( lastPart ) )
8799
{
88-
return String.format( "%s/%s", pathParts[pathParts.length - 2], pathParts[pathParts.length - 1] );
100+
final String firstPath;
101+
//Handle if scopedPath like "@types/jquery/***" or singlePath like "jquery/***"
102+
if ( pathParts.length > 2 && pathParts[pathParts.length - 3].startsWith( "@" ) )
103+
{
104+
// scoped path
105+
firstPath = String.format( "%s/%s", pathParts[pathParts.length - 3], pathParts[pathParts.length - 2] );
106+
}
107+
else
108+
{
109+
// single path
110+
firstPath = pathParts[pathParts.length - 2];
111+
}
112+
return String.format( "%s/%s", firstPath, pathParts[pathParts.length - 1] );
89113
}
90114

91115
return lastPart;

addons/pkg-npm/ftests/src/main/java/org/commonjava/indy/pkg/npm/content/NPMRemoteMetadataContentDecorator_ScopedPathTest.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,24 +50,26 @@
5050
* </ul>
5151
*/
5252
public class NPMRemoteMetadataContentDecorator_ScopedPathTest
53-
extends AbstractContentManagementTest
53+
extends AbstractContentManagementTest
5454
{
5555
protected static final String GROUP = "G";
5656

5757
private static final String SUBPATH = "subpath";
5858

5959
@Test
60-
public void test() throws Exception
60+
public void test()
61+
throws Exception
6162
{
6263

63-
final String packagePath = "@type/jquery";
64+
final String packagePath = "@types/jquery";
65+
final String tarball = "jquery-1.10.8-alpha.tgz";
6466

6567
final String baseUrl = server.formatUrl( STORE, SUBPATH );
6668

67-
final String tarballUrl = baseUrl + "/jquery/-/jquery-1.5.1.tgz";
69+
final String tarballUrl = String.format( "%s/%s/-/%s", baseUrl, packagePath, tarball );
6870

6971
final String packageContent = IOUtils.toString(
70-
Thread.currentThread().getContextClassLoader().getResourceAsStream( "subpath-package-1.5.1.json" ) )
72+
Thread.currentThread().getContextClassLoader().getResourceAsStream( "subpath-scoped-package-1.10.8-alpha.json" ) )
7173
.replace( "@@REGISTRY@@", baseUrl );
7274

7375
server.expect( server.formatUrl( STORE, SUBPATH, packagePath ), 200,
@@ -86,7 +88,7 @@ public void test() throws Exception
8688

8789
String contextUrl =
8890
UrlUtils.buildUrl( fixture.getUrl(), "content", NPM_PKG_KEY, storeKey.getType().name(), STORE );
89-
String decoratedContent = packageContent.replaceAll( tarballUrl, contextUrl + "/jquery/-/jquery-1.5.1.tgz" );
91+
String decoratedContent = packageContent.replaceAll( tarballUrl, String.format( "%s/%s/-/%s", contextUrl, packagePath, tarball ) );
9092
assertThat( IOUtils.toString( stream ), equalTo( decoratedContent ) );
9193
stream.close();
9294

@@ -95,7 +97,7 @@ public void test() throws Exception
9597
stream = client.content().get( groupKey, packagePath );
9698
assertThat( stream, notNullValue() );
9799
contextUrl = UrlUtils.buildUrl( fixture.getUrl(), "content", NPM_PKG_KEY, groupKey.getType().name(), GROUP );
98-
String maskedUrl = contextUrl + "/jquery/-/jquery-1.5.1.tgz";
100+
String maskedUrl = String.format( "%s/%s/-/%s", contextUrl, packagePath, tarball );
99101
// group metadata is not a simple copy of the remote repo so we only check if the decorated tarball url exists
100102
assertThat( IOUtils.toString( stream ), containsString( maskedUrl ) );
101103
stream.close();
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
{
2+
"_id": "@types/jquery",
3+
"_rev": "804-f7e6bcad91344f46c1bc2a6448331767",
4+
"name": "@types/jquery",
5+
"description": "TypeScript definitions for jQuery",
6+
"dist-tags": {
7+
"beta": "2.2.1",
8+
"latest": "2.2.1"
9+
},
10+
"versions": {
11+
"1.10.8-alpha": {
12+
"name": "@types/jquery",
13+
"version": "1.10.8-alpha",
14+
"description": "Type definitions for jQuery from https://www.github.com/DefinitelyTyped/DefinitelyTyped",
15+
"main": "",
16+
"scripts": {},
17+
"author": {
18+
"name": "Boris Yankov",
19+
"email": "https://github.com/borisyankov/"
20+
},
21+
"license": "MIT",
22+
"typings": "jquery.d.ts",
23+
"dependencies": {},
24+
"_id": "@types/jquery@1.10.8-alpha",
25+
"_shasum": "2e0196ac0d0da999a874c57a17691eeb07b1ca2a",
26+
"_from": ".",
27+
"_npmVersion": "3.8.2",
28+
"_nodeVersion": "5.5.0",
29+
"_npmUser": {
30+
"name": "types",
31+
"email": "ryan.cavanaugh@microsoft.com"
32+
},
33+
"dist": {
34+
"shasum": "2e0196ac0d0da999a874c57a17691eeb07b1ca2a",
35+
"tarball": "@@REGISTRY@@/@types/jquery/-/jquery-1.10.8-alpha.tgz"
36+
},
37+
"maintainers": [
38+
{
39+
"name": "types",
40+
"email": "ryan.cavanaugh@microsoft.com"
41+
}
42+
],
43+
"_npmOperationalInternal": {
44+
"host": "packages-12-west.internal.npmjs.com",
45+
"tmp": "tmp/jquery-1.10.8-alpha.tgz_1459201471822_0.1997919890563935"
46+
},
47+
"directories": {}
48+
}
49+
},
50+
"maintainers": [
51+
{
52+
"name": "dmethvin",
53+
"email": "dave.methvin@gmail.com"
54+
},
55+
{
56+
"name": "mgol",
57+
"email": "m.goleb@gmail.com"
58+
}
59+
],
60+
"time": {
61+
"modified": "2017-04-23T10:57:14.309Z",
62+
"created": "2011-03-19T07:19:56.392Z",
63+
"1.6.2": "2011-07-06T16:13:21.519Z",
64+
"1.5.1": "2011-03-19T07:19:56.956Z",
65+
"1.6.3": "2011-09-12T19:05:34.373Z"
66+
},
67+
"author": {
68+
"name": "JS Foundation and other contributors",
69+
"url": "https://github.com/jquery/jquery/blob/3.2.1/AUTHORS.txt"
70+
},
71+
"users": {
72+
"dodo": true,
73+
"fgribreau": false,
74+
"parroit": true
75+
},
76+
"repository": {
77+
"type": "git",
78+
"url": "git+https://github.com/jquery1/jquery1.git"
79+
},
80+
"readme": "# jQuery\n\n> jQuery is a fast, small, and feature-rich JavaScript library.\n\nFor information on how to get started and how to use jQuery, please see [jQuery's documentation](http://api.jquery.com/).\nFor source files and issues, please visit the [jQuery repo](https://github.com/jquery/jquery).\n\nIf upgrading, please see the [blog post for 3.2.1](https://blog.jquery.com/2017/03/20/jquery-3-2-1-now-available/). This includes notable differences from the previous version and a more readable changelog.\n\n## Including jQuery\n\nBelow are some of the most common ways to include jQuery.\n\n### Browser\n\n#### Script tag\n\n```html\n<script src=\"https://code.jquery.com/jquery-3.2.1.min.js\"></script>\n```\n\n#### Babel\n\n[Babel](http://babeljs.io/) is a next generation JavaScript compiler. One of the features is the ability to use ES6/ES2015 modules now, even though browsers do not yet support this feature natively.\n\n```js\nimport $ from \"jquery\";\n```\n\n#### Browserify/Webpack\n\nThere are several ways to use [Browserify](http://browserify.org/) and [Webpack](https://webpack.github.io/). For more information on using these tools, please refer to the corresponding project's documention. In the script, including jQuery will usually look like this...\n\n```js\nvar $ = require(\"jquery\");\n```\n\n#### AMD (Asynchronous Module Definition)\n\nAMD is a module format built for the browser. For more information, we recommend [require.js' documentation](http://requirejs.org/docs/whyamd.html).\n\n```js\ndefine([\"jquery\"], function($) {\n\n});\n```\n\n### Node\n\nTo include jQuery in [Node](nodejs.org), first install with npm.\n\n```sh\nnpm install jquery\n```\n\nFor jQuery to work in Node, a window with a document is required. Since no such window exists natively in Node, one can be mocked by tools such as [jsdom](https://github.com/tmpvar/jsdom). This can be useful for testing purposes.\n\n```js\nrequire(\"jsdom\").env(\"\", function(err, window) {\n\tif (err) {\n\t\tconsole.error(err);\n\t\treturn;\n\t}\n\n\tvar $ = require(\"jquery\")(window);\n});\n```\n",
81+
"readmeFilename": "README1.md",
82+
"homepage": "https://jquery1.com",
83+
"keywords": [
84+
"jquery",
85+
"javascript"
86+
],
87+
"bugs": {
88+
"url": "https://github.com/jquery1/jquery1/issues"
89+
},
90+
"license": "MIT1",
91+
"_attachments": {
92+
"jquery-1.5.1.tgz": {
93+
"content_type": "application/octet-stream",
94+
"data": "H4sIAAAAAAAAA+1Uz0/CMBgdIMYQDh703vTASaFdt0GIB43xoFwgxkQTLs1WoMp+pB2KGOJfzNG77YrEA8aDBEOytyzf9r3vfW3XviXUf6JD1khMrD/KOLI2DISQ5zhAx6bnZhHZ5l2DEAIsTLDruDbymk0LYYJsT/Gbnsg6TGRKhZrKX/uYtYBV3BG8VQCAEQ0ZbAP4GtJWC57o1DMTkseRztp1XCcmGzDpC56kSyYMAZdAsEkyjmlgSkLKM45HAZueRuxFHSnDGKlUpB5UJVImU13K/FEM+vBKiFi0QRQDTQCZMJ8POAv6ENRqgE15CjBUynnWjU7SUSy03HQfc59FMlvG9e0lrMwr//1pdwJL3zeyTV79BdLhbINj/OJ/G9mZ/wl2XOQa/xOMc/9vA91Oac84pVde3HxnitahZTfu7qvWe2/wMFP3R+FI1ReKx6WfFNWvx/PFhQ5af7DSdzvlfZ0tqOtMxa3sb44cOXLkWItPNfw2cQAMAAA=",
95+
"length": 380
96+
}
97+
}
98+
}

0 commit comments

Comments
 (0)