Repository: opentable/oc
Pull request #813 (2018-01-25 11:55:18)

[DX-366] Allow usage of protocol  in storage.options.path
If no protocol is provided (current legacy usage) default to https.
cc @johnparn

related issue - https://github.com/opencomponents/storage-adapters/issues/34

📝  Update [documentation](https://github.com/opentable/oc/wiki/Registry#registry-configuration) once this get published


Patch:
diff --git a/package.json b/package.json
index 7f6c9f4c..131f38f9 100644
--- a/package.json
+++ b/package.json
@@ -83,7 +83,7 @@
     "oc-client": "3.0.7",
     "oc-client-browser": "1.2.3",
     "oc-get-unix-utc-timestamp": "1.0.1",
-    "oc-s3-storage-adapter": "1.1.0",
+    "oc-s3-storage-adapter": "1.0.4",
     "oc-storage-adapters-utils": "1.0.2",
     "oc-template-handlebars": "6.0.10",
     "oc-template-handlebars-compiler": "6.1.11",
diff --git a/src/registry/domain/options-sanitiser.js b/src/registry/domain/options-sanitiser.js
index 23ac32f5..2aa8fe1b 100644
--- a/src/registry/domain/options-sanitiser.js
+++ b/src/registry/domain/options-sanitiser.js
@@ -77,6 +77,13 @@ module.exports = function(input) {
     options.storage.options.verbosity = options.verbosity;
   }
 
+  if (options.storage && options.storage.path) {
+    options.storage.path =
+      options.storage.path.indexOf('http') === 0
+        ? options.storage.path
+        : `https:${options.storage.path}`;
+  }
+
   if (!options.env) {
     options.env = {};
   }
diff --git a/src/registry/domain/repository.js b/src/registry/domain/repository.js
index a1837e2c..e4d97925 100644
--- a/src/registry/domain/repository.js
+++ b/src/registry/domain/repository.js
@@ -204,7 +204,7 @@ module.exports = function(conf) {
     getComponentPath: (componentName, componentVersion) => {
       const prefix = conf.local
         ? conf.baseUrl
-        : `https:${options.path}${options.componentsDir}/`;
+        : `${options.path}${options.componentsDir}/`;
       return `${prefix}${componentName}/${componentVersion}/`;
     },
     getComponents: callback => {
@@ -248,14 +248,14 @@ module.exports = function(conf) {
       );
     },
     getStaticClientPath: () =>
-      `https:${options.path}${getFilePath(
+      `${options.path}${getFilePath(
         'oc-client',
         packageInfo.version,
         'src/oc-client.min.js'
       )}`,
 
     getStaticClientMapPath: () =>
-      `https:${options.path}${getFilePath(
+      `${options.path}${getFilePath(
         'oc-client',
         packageInfo.version,
         'src/oc-client.min.map'


Test patch:
diff --git a/test/unit/registry-domain-options-sanitiser.js b/test/unit/registry-domain-options-sanitiser.js
index 1c8a687c..2239e588 100644
--- a/test/unit/registry-domain-options-sanitiser.js
+++ b/test/unit/registry-domain-options-sanitiser.js
@@ -136,4 +136,83 @@ describe('registry : domain : options-sanitiser', () => {
       });
     });
   });
+
+  describe('storage adapter configuration', () => {
+    describe('when legacy "s3" param', () => {
+      describe('is provided', () => {
+        const options = { s3: { some: 'data' }, baseUrl: 'dummy' };
+        it('should create a storage.adapter configuration accordingly', () => {
+          expect(sanitise(options).storage.options).to.deep.equal({
+            some: 'data'
+          });
+          expect(sanitise(options).storage.adapter).to.equal(
+            require('oc-s3-storage-adapter')
+          );
+        });
+      });
+    });
+    describe('when no storage adapter', () => {
+      describe('is provided', () => {
+        const options = { storage: {}, baseUrl: 'dummy' };
+        it('should default to the oc-s3-storage-adapter', () => {
+          expect(sanitise(options).storage.adapter).to.equal(
+            require('oc-s3-storage-adapter')
+          );
+        });
+      });
+    });
+
+    describe('when refreshInterval', () => {
+      describe('is provided', () => {
+        const options = {
+          refreshInterval: 666,
+          storage: { options: {} },
+          baseUrl: 'dummy'
+        };
+        it('should pass the refreshInterval to the storage options', () => {
+          expect(sanitise(options).storage.options.refreshInterval).to.equal(
+            666
+          );
+        });
+      });
+    });
+
+    describe('when verbosity', () => {
+      describe('is provided', () => {
+        const options = {
+          verbosity: true,
+          storage: { options: {} },
+          baseUrl: 'dummy'
+        };
+        it('should pass the verbosity to the storage options', () => {
+          expect(sanitise(options).storage.options.verbosity).to.equal(true);
+        });
+      });
+    });
+
+    describe('when storage.path', () => {
+      describe('does not include a protocol', () => {
+        const options = {
+          storage: { path: '//someprovider.com' },
+          baseUrl: 'dummy'
+        };
+        it('should sanitize the path to rely on the https protocol', () => {
+          expect(sanitise(options).storage.path).to.equal(
+            'https://someprovider.com'
+          );
+        });
+      });
+      describe('does include a prefix', () => {
+        const options = {
+          storage: { path: 'http://someprovider.com' },
+          baseUrl: 'dummy'
+        };
+        it('should not modify it', () => {
+          expect(sanitise(options).storage.path).to.equal(
+            'http://someprovider.com'
+          );
+        });
+      });
+    });
+  });
 });
diff --git a/test/unit/registry-domain-repository.js b/test/unit/registry-domain-repository.js
index a60b5ea6..e5224ece 100644
--- a/test/unit/registry-domain-repository.js
+++ b/test/unit/registry-domain-repository.js
@@ -68,7 +68,7 @@ describe('registry : domain : repository', () => {
           bucket: 'walter-test',
           region: 'us-west-2',
           componentsDir: 'components',
-          path: '//s3.amazonaws.com/walter-test/'
+          path: 'https://s3.amazonaws.com/walter-test/'
         }
       }
     };