@@ -333,6 +333,31 @@ const IGNORED_SPEC_IDS = [
333
333
'github.com/api.github.com.2022-11-28'
334
334
] ;
335
335
336
+ // For some specs, we do want to include the spec in the collection, but the server URLs shouldn't
337
+ // be indexed, because they're not valid public URLs. For example, relative URLs, localhost/local
338
+ // network URLs, etc. The specs can still be required by id, where required, it's just that they're
339
+ // ambiguous enough that looking up these addresses in the index shouldn't return these APIs.
340
+ function shouldIndexUrl ( url : string ) {
341
+ if ( ! url ) return false ; // Bizarrely, yes, some specs list an empty server URL
342
+ if ( url . startsWith ( '/' ) ) return false ; // Purely relative URLs aren't indexable
343
+
344
+ // Make protocol-less URLs (common from Swagger?) parseable:
345
+ if ( ! url . match ( / ^ h t t p ( s ) ? : \/ \/ / ) ) url = `https://${ url } ` ;
346
+
347
+ try {
348
+ const parsedUrl = new URL ( url ) ;
349
+
350
+ return parsedUrl . hostname !== 'localhost' && // Localhost URLs
351
+ ! parsedUrl . hostname . endsWith ( '.localhost' ) &&
352
+ ! parsedUrl . hostname . endsWith ( '.local' ) && // mDNS local addresses
353
+ ! parsedUrl . hostname . match ( / ^ ( 1 2 7 | 1 0 | 1 9 2 | 0 ) \. \d + \. \d + \. \d + $ / ) && // Local network ips
354
+ parsedUrl . hostname . includes ( '.' ) ; // Local-only hostnames
355
+ } catch ( e ) {
356
+ console . log ( 'Failed to parse' , url ) ;
357
+ return false ; // If it's not a parseable URL, it's definitely not indexable
358
+ }
359
+ }
360
+
336
361
export async function generateApis ( globs : string [ ] , options : ApiGenerationOptions = { } ) {
337
362
const [ specs ] = await Promise . all ( [
338
363
globby ( globs ) ,
@@ -405,10 +430,7 @@ export async function generateApis(globs: string[], options: ApiGenerationOption
405
430
specIds [ specId . toLowerCase ( ) ] = specSource ;
406
431
407
432
serverUrls . forEach ( ( url ) => {
408
- // The index stores full URLs, so we skip indexing all server URLS that are relative - it's not
409
- // really practical or helpful to include or match against these. We want a domain etc. The
410
- // specs are still built & available, they're just not discoverable from the index.
411
- if ( url . startsWith ( '/' ) ) return ;
433
+ if ( ! shouldIndexUrl ( url ) ) return ; // Skip adding this spec to the index
412
434
413
435
if ( index [ url ] ) {
414
436
index [ url ] = [ specId ] . concat ( index [ url ] ) ;
0 commit comments