Parcourir la source

Allow Use of CDN to be Configurable for Fetching Package Metadata (#10125)

* Configure whether or not to use the CDN to get full package data

* add warning that default npm registry does not have cors enabled

Co-authored-by: Josh Hamet <jhamet@twitter.com>
Josh Hamet il y a 4 ans
Parent
commit
6f0c351e43

+ 6 - 0
packages/extensionmanager-extension/schema/plugin.json

@@ -27,6 +27,12 @@
       "description": "The URI of the CDN to use for fetching full package data",
       "default": "https://unpkg.com",
       "type": "string"
+    },
+    "enableCdn": {
+      "title": "Enable CDN",
+      "description": "Enables using the CDN to fetch full package data.  Otherwise, the configured NPM registry will be used. Due to a lack of CORS support by NPM registry, only disable if supplying a custom registry",
+      "default": true,
+      "type": "boolean"
     }
   },
   "additionalProperties": false,

+ 4 - 2
packages/extensionmanager/src/model.ts

@@ -234,14 +234,16 @@ export class ListModel extends VDomModel {
     this.lister.listingsLoaded.connect(this._listingIsLoaded, this);
     this.searcher = new Searcher(
       settings.composite['npmRegistry'] as string,
-      settings.composite['npmCdn'] as string
+      settings.composite['npmCdn'] as string,
+      settings.composite['enableCdn'] as boolean
     );
     _isDisclaimed = settings.composite['disclaimed'] === true;
     settings.changed.connect(() => {
       _isDisclaimed = settings.composite['disclaimed'] === true;
       this.searcher = new Searcher(
         settings.composite['npmRegistry'] as string,
-        settings.composite['npmCdn'] as string
+        settings.composite['npmCdn'] as string,
+        settings.composite['enableCdn'] as boolean
       );
       void this.update();
     });

+ 11 - 2
packages/extensionmanager/src/npm.ts

@@ -221,10 +221,12 @@ export class Searcher {
    */
   constructor(
     repoUri = 'https://registry.npmjs.org/-/v1/',
-    cdnUri = 'https://unpkg.com'
+    cdnUri = 'https://unpkg.com',
+    enableCdn = true
   ) {
     this.repoUri = repoUri;
     this.cdnUri = cdnUri;
+    this.enableCdn = enableCdn;
   }
 
   /**
@@ -263,7 +265,9 @@ export class Searcher {
     name: string,
     version: string
   ): Promise<IJupyterLabPackageData | null> {
-    const uri = new URL(`/${name}@${version}/package.json`, this.cdnUri);
+    const uri = this.enableCdn
+      ? new URL(`/${name}@${version}/package.json`, this.cdnUri)
+      : new URL(`/${name}/${version}`, this.repoUri);
     return fetch(uri.toString()).then((response: Response) => {
       if (response.ok) {
         return response.json();
@@ -281,6 +285,11 @@ export class Searcher {
    * The URI of the CDN to use for fetching full package data.
    */
   cdnUri: string;
+
+  /**
+   * Whether to use the CDN for fetching full package data. Otherwise, the NPM registry is used.
+   */
+  enableCdn: boolean;
 }
 
 /**