diff options
Diffstat (limited to 'node_modules/algoliasearch/src/browser/migration-layer')
4 files changed, 124 insertions, 0 deletions
diff --git a/node_modules/algoliasearch/src/browser/migration-layer/is-using-latest.js b/node_modules/algoliasearch/src/browser/migration-layer/is-using-latest.js new file mode 100644 index 00000000..f8e29d4e --- /dev/null +++ b/node_modules/algoliasearch/src/browser/migration-layer/is-using-latest.js @@ -0,0 +1,23 @@ +'use strict'; + +// this module helps finding if the current page is using +// the cdn.jsdelivr.net/algoliasearch/latest/$BUILDNAME.min.js version + +module.exports = isUsingLatest; + +function isUsingLatest(buildName) { + var toFind = new RegExp('cdn\\.jsdelivr\\.net/algoliasearch/latest/' + + buildName.replace('.', '\\.') + // algoliasearch, algoliasearch.angular + '(?:\\.min)?\\.js$'); // [.min].js + + var scripts = document.getElementsByTagName('script'); + var found = false; + for (var currentScript = 0, nbScripts = scripts.length; currentScript < nbScripts; currentScript++) { + if (scripts[currentScript].src && toFind.test(scripts[currentScript].src)) { + found = true; + break; + } + } + + return found; +} diff --git a/node_modules/algoliasearch/src/browser/migration-layer/load-v2.js b/node_modules/algoliasearch/src/browser/migration-layer/load-v2.js new file mode 100644 index 00000000..b3470baa --- /dev/null +++ b/node_modules/algoliasearch/src/browser/migration-layer/load-v2.js @@ -0,0 +1,50 @@ +'use strict'; + +module.exports = loadV2; + +function loadV2(buildName) { + var loadScript = require('load-script'); + var v2ScriptUrl = '//cdn.jsdelivr.net/algoliasearch/2/' + buildName + '.min.js'; + + var message = '-- AlgoliaSearch `latest` warning --\n' + + 'Warning, you are using the `latest` version string from jsDelivr to load the AlgoliaSearch library.\n' + + 'Using `latest` is no more recommended, you should load //cdn.jsdelivr.net/algoliasearch/2/algoliasearch.min.js\n\n' + + 'Also, we updated the AlgoliaSearch JavaScript client to V3. If you want to upgrade,\n' + + 'please read our migration guide at https://github.com/algolia/algoliasearch-client-js/wiki/Migration-guide-from-2.x.x-to-3.x.x\n' + + '-- /AlgoliaSearch `latest` warning --'; + + if (window.console) { + if (window.console.warn) { + window.console.warn(message); + } else if (window.console.log) { + window.console.log(message); + } + } + + // If current script loaded asynchronously, + // it will load the script with DOMElement + // otherwise, it will load the script with document.write + try { + // why \x3c? http://stackoverflow.com/a/236106/147079 + document.write('\x3Cscript>window.ALGOLIA_SUPPORTS_DOCWRITE = true\x3C/script>'); + + if (window.ALGOLIA_SUPPORTS_DOCWRITE === true) { + document.write('\x3Cscript src="' + v2ScriptUrl + '">\x3C/script>'); + scriptLoaded('document.write')(); + } else { + loadScript(v2ScriptUrl, scriptLoaded('DOMElement')); + } + } catch (e) { + loadScript(v2ScriptUrl, scriptLoaded('DOMElement')); + } +} + +function scriptLoaded(method) { + return function log() { + var message = 'AlgoliaSearch: loaded V2 script using ' + method; + + if (window.console && window.console.log) { + window.console.log(message); + } + }; +} diff --git a/node_modules/algoliasearch/src/browser/migration-layer/old-globals.js b/node_modules/algoliasearch/src/browser/migration-layer/old-globals.js new file mode 100644 index 00000000..1f40c0d2 --- /dev/null +++ b/node_modules/algoliasearch/src/browser/migration-layer/old-globals.js @@ -0,0 +1,26 @@ +'use strict'; + +/* eslint no-unused-vars: [2, {"vars": "local"}] */ + +module.exports = oldGlobals; + +// put old window.AlgoliaSearch.. into window. again so that +// users upgrading to V3 without changing their code, will be warned +function oldGlobals() { + var message = '-- AlgoliaSearch V2 => V3 error --\n' + + 'You are trying to use a new version of the AlgoliaSearch JavaScript client with an old notation.\n' + + 'Please read our migration guide at https://github.com/algolia/algoliasearch-client-js/wiki/Migration-guide-from-2.x.x-to-3.x.x\n' + + '-- /AlgoliaSearch V2 => V3 error --'; + + window.AlgoliaSearch = function() { + throw new Error(message); + }; + + window.AlgoliaSearchHelper = function() { + throw new Error(message); + }; + + window.AlgoliaExplainResults = function() { + throw new Error(message); + }; +} diff --git a/node_modules/algoliasearch/src/browser/migration-layer/script.js b/node_modules/algoliasearch/src/browser/migration-layer/script.js new file mode 100644 index 00000000..9d12315f --- /dev/null +++ b/node_modules/algoliasearch/src/browser/migration-layer/script.js @@ -0,0 +1,25 @@ +'use strict'; + +// This script will be browserified and prepended to the normal build +// directly in window, not wrapped in any module definition +// To avoid cases where we are loaded with /latest/ along with +migrationLayer(process.env.ALGOLIA_BUILDNAME); + +// Now onto the V2 related code: +// If the client is using /latest/$BUILDNAME.min.js, load V2 of the library +// +// Otherwise, setup a migration layer that will throw on old constructors like +// new AlgoliaSearch(). +// So that users upgrading from v2 to v3 will have a clear information +// message on what to do if they did not read the migration guide +function migrationLayer(buildName) { + var isUsingLatest = require('./is-using-latest'); + var loadV2 = require('./load-v2'); + var oldGlobals = require('./old-globals'); + + if (isUsingLatest(buildName)) { + loadV2(buildName); + } else { + oldGlobals(); + } +} |
