UNPKG

2.61 kBJavaScriptView Raw
1'use strict';
2var global = require('../internals/global');
3var NativePromiseConstructor = require('../internals/promise-native-constructor');
4var isCallable = require('../internals/is-callable');
5var isForced = require('../internals/is-forced');
6var inspectSource = require('../internals/inspect-source');
7var wellKnownSymbol = require('../internals/well-known-symbol');
8var IS_BROWSER = require('../internals/engine-is-browser');
9var IS_DENO = require('../internals/engine-is-deno');
10var IS_PURE = require('../internals/is-pure');
11var V8_VERSION = require('../internals/engine-v8-version');
12
13var NativePromisePrototype = NativePromiseConstructor && NativePromiseConstructor.prototype;
14var SPECIES = wellKnownSymbol('species');
15var SUBCLASSING = false;
16var NATIVE_PROMISE_REJECTION_EVENT = isCallable(global.PromiseRejectionEvent);
17
18var FORCED_PROMISE_CONSTRUCTOR = isForced('Promise', function () {
19 var PROMISE_CONSTRUCTOR_SOURCE = inspectSource(NativePromiseConstructor);
20 var GLOBAL_CORE_JS_PROMISE = PROMISE_CONSTRUCTOR_SOURCE !== String(NativePromiseConstructor);
21 // V8 6.6 (Node 10 and Chrome 66) have a bug with resolving custom thenables
22 // https://bugs.chromium.org/p/chromium/issues/detail?id=830565
23 // We can't detect it synchronously, so just check versions
24 if (!GLOBAL_CORE_JS_PROMISE && V8_VERSION === 66) return true;
25 // We need Promise#{ catch, finally } in the pure version for preventing prototype pollution
26 if (IS_PURE && !(NativePromisePrototype['catch'] && NativePromisePrototype['finally'])) return true;
27 // We can't use @@species feature detection in V8 since it causes
28 // deoptimization and performance degradation
29 // https://github.com/zloirock/core-js/issues/679
30 if (!V8_VERSION || V8_VERSION < 51 || !/native code/.test(PROMISE_CONSTRUCTOR_SOURCE)) {
31 // Detect correctness of subclassing with @@species support
32 var promise = new NativePromiseConstructor(function (resolve) { resolve(1); });
33 var FakePromise = function (exec) {
34 exec(function () { /* empty */ }, function () { /* empty */ });
35 };
36 var constructor = promise.constructor = {};
37 constructor[SPECIES] = FakePromise;
38 SUBCLASSING = promise.then(function () { /* empty */ }) instanceof FakePromise;
39 if (!SUBCLASSING) return true;
40 // Unhandled rejections tracking support, NodeJS Promise without it fails @@species test
41 } return !GLOBAL_CORE_JS_PROMISE && (IS_BROWSER || IS_DENO) && !NATIVE_PROMISE_REJECTION_EVENT;
42});
43
44module.exports = {
45 CONSTRUCTOR: FORCED_PROMISE_CONSTRUCTOR,
46 REJECTION_EVENT: NATIVE_PROMISE_REJECTION_EVENT,
47 SUBCLASSING: SUBCLASSING
48};