-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
130 lines (107 loc) · 2.97 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
'use strict'
var list = []
exports.list = list
var map = Object.create(null)
function nameIsAvailable(name) {
return map[name] === undefined
}
var native
if (typeof Promise === 'function') {
native = Promise
map.native = list[0] = {
name: 'native',
aliases: [],
Promise: Promise,
version: process.versions.v8,
}
}
exports.has = function has(name) {
return !nameIsAvailable(name)
}
exports.get = function get(name) {
return map[name]
}
exports.getPromiseConstructor = function getPromiseConstructor(name) {
return map[name] && map[name].Promise
}
exports.register = function register(name, alias, exportPromise) {
if (!name) throw new TypeError('name')
if (!nameIsAvailable(name)) throw new Error('Duplicated name: ' + name)
var aliases = alias ? [].concat(alias).filter(nameIsAvailable) : []
var promise, version
var impl = {
name: name,
aliases: aliases,
get Promise() {
if (promise !== undefined) return promise
try {
promise = getPromise(name, exportPromise)
} catch (e) {
console.error(e)
impl.error = e
promise = null
}
return promise
},
get version() {
if (version !== undefined) return version
try {
version = require(name + '/package.json').version
} catch (e) {
console.error(e)
}
return version
}
}
list.push(impl)
map[name] = impl
impl.aliases.forEach(function (alias) { map[alias] = impl })
}
exports.unregister = function unregister(name) {
if (nameIsAvailable(name)) return false
var impl = map[name]
list.splice(list.indexOf(impl), 1)
delete map[name]
return true
}
Object.defineProperty(exports, 'default', {
get: function () {
var P = process.env.P
if (P) {
var impl = exports.get(P)
|| exports.get(P.toLowerCase())
|| exports.get(P.toUpperCase())
if (!impl) console.warn('Unknown Promise implementaion:', P)
if (impl.Promise) return impl.Promise
}
if (native) console.info('Use V8 native implementaion instead')
return native
}
})
function getPromise(packageName, exportPromise) {
var P
if (typeof exportPromise === 'function') {
P = exportPromise(function () { return require(packageName) })
} else {
var M = require(packageName)
P = M[exportPromise || 'Promise'] || M
}
if (typeof P !== 'function') throw new Error(
'Failed to export Promise constructor from ' + packageName
+ ', please recheck its API docs')
else new P(function (r) { r() }).then(function () {})
if (P === native) throw new Error(
packageName + ' may be a polyfill which exports native Promise'
+ ', please recheck its API docs')
if (typeof P.resolve !== 'function') throw new Error(
'Missing Promise.resolve() method from ' + packageName)
else P.resolve().then(function () {})
if (typeof P.reject !== 'function') throw new Error(
'Missing Promise.reject() method from ' + packageName)
else P.reject()['catch'](function () {})
return P
}
var implementations = require('./implementations')
implementations.forEach(function (args) {
exports.register.apply(undefined, args)
})