| 1 | "use strict"
|
|---|
| 2 | module.exports = require('./loader')(global, loadImplementation);
|
|---|
| 3 |
|
|---|
| 4 | /**
|
|---|
| 5 | * Node.js version of loadImplementation.
|
|---|
| 6 | *
|
|---|
| 7 | * Requires the given implementation and returns the registration
|
|---|
| 8 | * containing {Promise, implementation}
|
|---|
| 9 | *
|
|---|
| 10 | * If implementation is undefined or global.Promise, loads it
|
|---|
| 11 | * Otherwise uses require
|
|---|
| 12 | */
|
|---|
| 13 | function loadImplementation(implementation){
|
|---|
| 14 | var impl = null
|
|---|
| 15 |
|
|---|
| 16 | if(shouldPreferGlobalPromise(implementation)){
|
|---|
| 17 | // if no implementation or env specified use global.Promise
|
|---|
| 18 | impl = {
|
|---|
| 19 | Promise: global.Promise,
|
|---|
| 20 | implementation: 'global.Promise'
|
|---|
| 21 | }
|
|---|
| 22 | } else if(implementation){
|
|---|
| 23 | // if implementation specified, require it
|
|---|
| 24 | var lib = require(implementation)
|
|---|
| 25 | impl = {
|
|---|
| 26 | Promise: lib.Promise || lib,
|
|---|
| 27 | implementation: implementation
|
|---|
| 28 | }
|
|---|
| 29 | } else {
|
|---|
| 30 | // try to auto detect implementation. This is non-deterministic
|
|---|
| 31 | // and should prefer other branches, but this is our last chance
|
|---|
| 32 | // to load something without throwing error
|
|---|
| 33 | 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 impl
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| 47 | /**
|
|---|
| 48 | * Determines if the global.Promise should be preferred if an implementation
|
|---|
| 49 | * has not been registered.
|
|---|
| 50 | */
|
|---|
| 51 | function shouldPreferGlobalPromise(implementation){
|
|---|
| 52 | if(implementation){
|
|---|
| 53 | return implementation === 'global.Promise'
|
|---|
| 54 | } else if(typeof global.Promise !== 'undefined'){
|
|---|
| 55 | // Load global promise if implementation not specified
|
|---|
| 56 | // Versions < 0.11 did not have global Promise
|
|---|
| 57 | // Do not use for version < 0.12 as version 0.11 contained buggy versions
|
|---|
| 58 | 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 specified
|
|---|
| 63 | return false
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | /**
|
|---|
| 67 | * Look for common libs as last resort there is no guarantee that
|
|---|
| 68 | * this will return a desired implementation or even be deterministic.
|
|---|
| 69 | * The priority is also nearly arbitrary. We are only doing this
|
|---|
| 70 | * for older versions of Node.js <0.12 that do not have a reasonable
|
|---|
| 71 | * global.Promise implementation and we the user has not registered
|
|---|
| 72 | * the preference. This preserves the behavior of any-promise <= 0.1
|
|---|
| 73 | * and may be deprecated or removed in the future
|
|---|
| 74 | */
|
|---|
| 75 | function 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.length
|
|---|
| 88 | for(; i < len; i++){
|
|---|
| 89 | try {
|
|---|
| 90 | return loadImplementation(libs[i])
|
|---|
| 91 | } catch(e){}
|
|---|
| 92 | }
|
|---|
| 93 | return null
|
|---|
| 94 | }
|
|---|