basant307/AI_Governance_Project
045
1"use strict"2module.exports = require('./loader')(global, loadImplementation);3 4/**5 * Node.js version of loadImplementation.6 *7 * Requires the given implementation and returns the registration8 * containing {Promise, implementation}9 *10 * If implementation is undefined or global.Promise, loads it11 * Otherwise uses require12 */13function loadImplementation(implementation){14 var impl = null15 16 if(shouldPreferGlobalPromise(implementation)){17 // if no implementation or env specified use global.Promise18 impl = {19 Promise: global.Promise,20 implementation: 'global.Promise'21 }22 } else if(implementation){23 // if implementation specified, require it24 var lib = require(implementation)25 impl = {26 Promise: lib.Promise || lib,27 implementation: implementation28 }29 } else {30 // try to auto detect implementation. This is non-deterministic31 // and should prefer other branches, but this is our last chance32 // to load something without throwing error33 impl = tryAutoDetect()34 }35 36 if(impl === null){37 throw new Error('Cannot find any-promise implementation nor'+38 ' global.Promise. You must install polyfill or call'+39 ' require("any-promise/register") with your preferred'+40 ' implementation, e.g. require("any-promise/register/bluebird")'+41 ' on application load prior to any require("any-promise").')42 }43 44 return impl45}46 47/**48 * Determines if the global.Promise should be preferred if an implementation49 * has not been registered.50 */51function shouldPreferGlobalPromise(implementation){52 if(implementation){53 return implementation === 'global.Promise'54 } else if(typeof global.Promise !== 'undefined'){55 // Load global promise if implementation not specified56 // Versions < 0.11 did not have global Promise57 // Do not use for version < 0.12 as version 0.11 contained buggy versions58 var version = (/v(\d+)\.(\d+)\.(\d+)/).exec(process.version)59 return !(version && +version[1] == 0 && +version[2] < 12)60 }61 62 // do not have global.Promise or another implementation was specified63 return false64}65 66/**67 * Look for common libs as last resort there is no guarantee that68 * this will return a desired implementation or even be deterministic.69 * The priority is also nearly arbitrary. We are only doing this70 * for older versions of Node.js <0.12 that do not have a reasonable71 * global.Promise implementation and we the user has not registered72 * the preference. This preserves the behavior of any-promise <= 0.173 * and may be deprecated or removed in the future74 */75function tryAutoDetect(){76 var libs = [77 "es6-promise",78 "promise",79 "native-promise-only",80 "bluebird",81 "rsvp",82 "when",83 "q",84 "pinkie",85 "lie",86 "vow"]87 var i = 0, len = libs.length88 for(; i < len; i++){89 try {90 return loadImplementation(libs[i])91 } catch(e){}92 }93 return null94}95 