Skip to content

Commit 6f0b5f5

Browse files
author
Paul van Brenk
committed
PR Feedback
1 parent dfdd6d2 commit 6f0b5f5

File tree

3 files changed

+142
-124
lines changed

3 files changed

+142
-124
lines changed

Nodejs/Product/Nodejs/NodejsConstants.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
1+
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
22

33
using System;
44
using System.IO;
@@ -25,6 +25,7 @@ internal static class NodejsConstants
2525
internal const string ProjectFileFilter = "Node.js Project File (*.njsproj)\n*.njsproj\nAll Files (*.*)\n*.*\n";
2626

2727
internal const string NodeModulesFolder = "node_modules";
28+
internal const string NodeModulesFolderWithSeparators = "\\" + NodeModulesFolder + "\\";
2829
internal const string NodeModulesStagingFolder = "node_modules\\.staging\\";
2930
internal const string BowerComponentsFolder = "bower_components";
3031

Nodejs/Product/Nodejs/NpmUI/NpmWorker.cs

Lines changed: 128 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -106,145 +106,158 @@ public async Task<IEnumerable<IPackage>> GetCatalogPackagesAsync(string filterTe
106106

107107
TelemetryHelper.LogSearchNpm();
108108

109-
string relativeUri;
110109
if (filterText.Length == 1)
111110
{
112-
// Special case since the search API won't retur results for
113-
// single chararcter queries.
114-
relativeUri = $"/{WebUtility.UrlEncode(filterText)}/latest";
111+
return await QueryNpmForSingleCharAsync(filterText);
112+
}
113+
else
114+
{
115+
return await QueryNpmAsync(filterText);
116+
}
117+
}
118+
119+
private async Task<IEnumerable<IPackage>> QueryNpmAsync(string filterText)
120+
{
121+
Debug.Assert(filterText.Length > 1, $"Use {nameof(QueryNpmForSingleCharAsync)} for single character queries.");
122+
123+
var relativeUri = $"/-/v1/search?text={WebUtility.UrlEncode(filterText)}";
124+
125+
using (var response = await QueryNpmRegistryAsync(relativeUri))
126+
{
127+
/* We expect the following response:
128+
{
129+
"objects": [
130+
{
131+
"package": {
132+
"name": "express",
133+
"scope": "unscoped",
134+
"version": "4.15.2",
135+
"description": "Fast, unopinionated, minimalist web framework",
136+
"keywords": [ "express", "framework", "sinatra", "web", "rest", "restful", "router", "app", "api" ],
137+
"date": "2017-03-06T13:42:44.853Z",
138+
"links": {
139+
"npm": "https://www.npmjs.com/package/express",
140+
"homepage": "http://expressjs.com/",
141+
"repository": "https://github.com/expressjs/express",
142+
"bugs": "https://github.com/expressjs/express/issues"
143+
},
144+
"author": {
145+
"name": "TJ Holowaychuk",
146+
"email": "[email protected]"
147+
},
148+
"publisher": {
149+
"username": "dougwilson",
150+
"email": "[email protected]"
151+
},
152+
"maintainers": [
153+
{
154+
"username": "dougwilson",
155+
"email": "[email protected]"
156+
}
157+
]
158+
},
159+
"score": {
160+
"final": 0.9549640105248649,
161+
"detail": {
162+
"quality": 0.9427473299991661,
163+
"popularity": 0.9496544159654299,
164+
"maintenance": 0.9707450455348992
165+
}
166+
},
167+
"searchScore": 100000.95
168+
}
169+
],
170+
"total": 10991,
171+
"time": "Tue May 09 2017 22:41:07 GMT+0000 (UTC)"
172+
}
173+
*/
115174

116-
using (var response = await SearchNpmRegistry(relativeUri))
175+
using (var reader = new StreamReader(response.GetResponseStream()))
176+
using (var jsonReader = new JsonTextReader(reader))
117177
{
118-
/* We expect the following response
119-
{
120-
"name": "express",
121-
"scope": "unscoped",
122-
"version": "4.15.2",
123-
"description": "Fast, unopinionated, minimalist web framework",
124-
"keywords": [ "express", "framework", "sinatra", "web", "rest", "restful", "router", "app", "api" ],
125-
"date": "2017-03-06T13:42:44.853Z",
126-
"links": {
127-
"npm": "https://www.npmjs.com/package/express",
128-
"homepage": "http://expressjs.com/",
129-
"repository": "https://github.com/expressjs/express",
130-
"bugs": "https://github.com/expressjs/express/issues"
131-
},
132-
"author": {
133-
"name": "TJ Holowaychuk",
134-
"email": "[email protected]"
135-
},
136-
"publisher": {
137-
"username": "dougwilson",
138-
"email": "[email protected]"
139-
},
140-
"maintainers": [
141-
{
142-
"username": "dougwilson",
143-
"email": "[email protected]"
144-
}
145-
]
146-
}*/
147-
var reader = new StreamReader(response.GetResponseStream());
148-
using (var jsonReader = new JsonTextReader(reader))
178+
while (jsonReader.Read())
149179
{
150-
while (jsonReader.Read())
180+
switch (jsonReader.TokenType)
151181
{
152-
switch (jsonReader.TokenType)
153-
{
154-
case JsonToken.StartObject:
155-
var token = JToken.ReadFrom(jsonReader);
156-
var package = ReadPackage(token, new NodeModuleBuilder());
157-
if (package != null)
158-
{
159-
return new[] { package };
160-
}
161-
break;
162-
default:
163-
throw new InvalidOperationException("Unexpected json token.");
164-
}
182+
case JsonToken.StartObject:
183+
case JsonToken.PropertyName:
184+
continue;
185+
case JsonToken.StartArray:
186+
return ReadPackagesFromArray(jsonReader);
187+
default:
188+
throw new InvalidOperationException("Unexpected json token.");
165189
}
166190
}
167191
}
168192
}
169-
else
170-
{
171-
relativeUri = $"/-/v1/search?text={WebUtility.UrlEncode(filterText)}";
172193

173-
using (var response = await SearchNpmRegistry(relativeUri))
174-
{
175-
/* We expect the following response:
176-
{
177-
"objects": [
178-
{
179-
"package": {
180-
"name": "express",
181-
"scope": "unscoped",
182-
"version": "4.15.2",
183-
"description": "Fast, unopinionated, minimalist web framework",
184-
"keywords": [ "express", "framework", "sinatra", "web", "rest", "restful", "router", "app", "api" ],
185-
"date": "2017-03-06T13:42:44.853Z",
186-
"links": {
187-
"npm": "https://www.npmjs.com/package/express",
188-
"homepage": "http://expressjs.com/",
189-
"repository": "https://github.com/expressjs/express",
190-
"bugs": "https://github.com/expressjs/express/issues"
191-
},
192-
"author": {
193-
"name": "TJ Holowaychuk",
194-
"email": "[email protected]"
195-
},
196-
"publisher": {
197-
"username": "dougwilson",
198-
"email": "[email protected]"
199-
},
200-
"maintainers": [
201-
{
202-
"username": "dougwilson",
203-
"email": "[email protected]"
204-
}
205-
]
206-
},
207-
"score": {
208-
"final": 0.9549640105248649,
209-
"detail": {
210-
"quality": 0.9427473299991661,
211-
"popularity": 0.9496544159654299,
212-
"maintenance": 0.9707450455348992
213-
}
214-
},
215-
"searchScore": 100000.95
216-
}
217-
],
218-
"total": 10991,
219-
"time": "Tue May 09 2017 22:41:07 GMT+0000 (UTC)"
220-
}
221-
*/
194+
// should never get here
195+
throw new InvalidOperationException("Unexpected json token.");
196+
}
222197

223-
var reader = new StreamReader(response.GetResponseStream());
224-
using (var jsonReader = new JsonTextReader(reader))
198+
private async Task<IEnumerable<IPackage>> QueryNpmForSingleCharAsync(string filterText)
199+
{
200+
Debug.Assert(filterText.Length == 1, $"Use {nameof(QueryNpmAsync)} for general queries when the search query has more than 1 character.");
201+
202+
// Special case since the search API won't return results for
203+
// single chararacter queries.
204+
var relativeUri = $"/{WebUtility.UrlEncode(filterText)}/latest";
205+
206+
using (var response = await QueryNpmRegistryAsync(relativeUri))
207+
{
208+
/* We expect the following response
209+
{
210+
"name": "express",
211+
"scope": "unscoped",
212+
"version": "4.15.2",
213+
"description": "Fast, unopinionated, minimalist web framework",
214+
"keywords": [ "express", "framework", "sinatra", "web", "rest", "restful", "router", "app", "api" ],
215+
"date": "2017-03-06T13:42:44.853Z",
216+
"links": {
217+
"npm": "https://www.npmjs.com/package/express",
218+
"homepage": "http://expressjs.com/",
219+
"repository": "https://github.com/expressjs/express",
220+
"bugs": "https://github.com/expressjs/express/issues"
221+
},
222+
"author": {
223+
"name": "TJ Holowaychuk",
224+
"email": "[email protected]"
225+
},
226+
"publisher": {
227+
"username": "dougwilson",
228+
"email": "[email protected]"
229+
},
230+
"maintainers": [
231+
{
232+
"username": "dougwilson",
233+
"email": "[email protected]"
234+
}
235+
]
236+
}*/
237+
using (var reader = new StreamReader(response.GetResponseStream()))
238+
using (var jsonReader = new JsonTextReader(reader))
239+
{
240+
while (jsonReader.Read())
225241
{
226-
while (jsonReader.Read())
242+
if (jsonReader.TokenType == JsonToken.StartObject)
227243
{
228-
switch (jsonReader.TokenType)
244+
var token = JToken.ReadFrom(jsonReader);
245+
var package = ReadPackage(token, new NodeModuleBuilder());
246+
if (package != null)
229247
{
230-
case JsonToken.StartObject:
231-
case JsonToken.PropertyName:
232-
continue;
233-
case JsonToken.StartArray:
234-
return ReadPackagesFromArray(jsonReader);
235-
default:
236-
throw new InvalidOperationException("Unexpected json token.");
248+
return new[] { package };
237249
}
238250
}
251+
252+
throw new InvalidOperationException($"Unexpected json token. '{jsonReader.TokenType}'");
239253
}
240254
}
241255
}
242256

243-
// should never get here
244257
throw new InvalidOperationException("Unexpected json token.");
245258
}
246259

247-
private static Task<WebResponse> SearchNpmRegistry(string relativeUri)
260+
private static Task<WebResponse> QueryNpmRegistryAsync(string relativeUri)
248261
{
249262
var searchUri = new Uri(defaultRegistryUri, relativeUri);
250263

Nodejs/Product/Npm/SPI/RootPackage.cs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
using System;
44
using System.Collections.Generic;
5+
using System.Diagnostics;
56
using System.Globalization;
67
using System.IO;
78
using System.Linq;
@@ -12,11 +13,11 @@ namespace Microsoft.NodejsTools.Npm.SPI
1213
internal class RootPackage : IRootPackage
1314
{
1415
public RootPackage(
15-
string fullPathToRootDirectory,
16-
bool showMissingDevOptionalSubPackages,
17-
Dictionary<string, ModuleInfo> allModules = null,
18-
int depth = 0,
19-
int maxDepth = 1)
16+
string fullPathToRootDirectory,
17+
bool showMissingDevOptionalSubPackages,
18+
Dictionary<string, ModuleInfo> allModules = null,
19+
int depth = 0,
20+
int maxDepth = 1)
2021
{
2122
this.Path = fullPathToRootDirectory;
2223
var packageJsonFile = System.IO.Path.Combine(fullPathToRootDirectory, "package.json");
@@ -57,10 +58,13 @@ public RootPackage(
5758
{
5859
// this is the root package so the full folder name after node_nodules is the name
5960
// of the package
60-
var index = this.Path.IndexOf(NodejsConstants.NodeModulesFolder, StringComparison.OrdinalIgnoreCase);
61-
var name = this.Path.Substring(index + NodejsConstants.NodeModulesFolder.Length);
61+
var index = this.Path.IndexOf(NodejsConstants.NodeModulesFolderWithSeparators, StringComparison.OrdinalIgnoreCase);
6262

63-
this.Name = name.TrimStart('\\', '/');
63+
Debug.Assert(index > -1, "Failed to find the node_modules folder.");
64+
65+
var name = this.Path.Substring(index + NodejsConstants.NodeModulesFolderWithSeparators.Length);
66+
67+
this.Name = name;
6468
}
6569
}
6670

0 commit comments

Comments
 (0)