Skip to content

Commit

Permalink
improve some cases of environment detection
Browse files Browse the repository at this point in the history
  • Loading branch information
zloirock committed Jul 18, 2024
1 parent 11ecdfd commit 25b5716
Show file tree
Hide file tree
Showing 31 changed files with 67 additions and 63 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
- Updated the way of escaping, [regex-escaping/77](https://github.com/tc39/proposal-regex-escaping/pull/77)
- Throw an error on non-strings, [regex-escaping/58](https://github.com/tc39/proposal-regex-escaping/pull/58)
- Fixed `RegExp` named capture groups polyfill in combination with non-capturing groups, [#1352](https://github.com/zloirock/core-js/pull/1352), thanks [**@Ulop**](https://github.com/Ulop)
- Improved some cases of engine detection
- Improved some cases of environment detection
- Uses [`process.getBuiltinModule`](https://nodejs.org/docs/latest/api/process.html#processgetbuiltinmoduleid) for getting built-in NodeJS modules where it's available
- Uses `https` instead of `http` in `URL` constructor feature detection to avoid extra notifications from some overly vigilant security scanners, [#1345](https://github.com/zloirock/core-js/issues/1345)
- Some minor optimizations
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';
var fails = require('../internals/fails');
var wellKnownSymbol = require('../internals/well-known-symbol');
var V8_VERSION = require('../internals/engine-v8-version');
var V8_VERSION = require('../internals/environment-v8-version');

var SPECIES = wellKnownSymbol('species');

Expand Down
7 changes: 0 additions & 7 deletions packages/core-js/internals/engine-is-browser.js

This file was deleted.

3 changes: 0 additions & 3 deletions packages/core-js/internals/engine-is-bun.js

This file was deleted.

3 changes: 0 additions & 3 deletions packages/core-js/internals/engine-is-deno.js

This file was deleted.

5 changes: 0 additions & 5 deletions packages/core-js/internals/engine-is-node.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';
var userAgent = require('../internals/engine-user-agent');
var userAgent = require('../internals/environment-user-agent');

var firefox = userAgent.match(/firefox\/(\d+)/i);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
'use strict';
var UA = require('../internals/engine-user-agent');
var UA = require('../internals/environment-user-agent');

module.exports = /MSIE|Trident/.test(UA);
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
'use strict';
var userAgent = require('../internals/engine-user-agent');
var userAgent = require('../internals/environment-user-agent');

module.exports = /ipad|iphone|ipod/i.test(userAgent) && typeof Pebble != 'undefined';
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';
var userAgent = require('../internals/engine-user-agent');
var userAgent = require('../internals/environment-user-agent');

// eslint-disable-next-line redos/no-vulnerable -- safe
module.exports = /(?:ipad|iphone|ipod).*applewebkit/i.test(userAgent);
4 changes: 4 additions & 0 deletions packages/core-js/internals/environment-is-node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
'use strict';
var ENVIRONMENT = require('../internals/environment');

module.exports = ENVIRONMENT === 'NODE';
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
'use strict';
var userAgent = require('../internals/engine-user-agent');
var userAgent = require('../internals/environment-user-agent');

module.exports = /web0s(?!.*chrome)/i.test(userAgent);
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';
var global = require('../internals/global');
var userAgent = require('../internals/engine-user-agent');
var userAgent = require('../internals/environment-user-agent');

var process = global.process;
var Deno = global.Deno;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';
var userAgent = require('../internals/engine-user-agent');
var userAgent = require('../internals/environment-user-agent');

var webkit = userAgent.match(/AppleWebKit\/(\d+)\./);

Expand Down
21 changes: 21 additions & 0 deletions packages/core-js/internals/environment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';
/* global Bun, Deno -- detection */
var globalThis = require('../internals/global');
var userAgent = require('../internals/environment-user-agent');
var classof = require('../internals/classof-raw');

var userAgentStartsWith = function (string) {
return userAgent.slice(0, string.length) === string;
};

module.exports = (function () {
if (userAgentStartsWith('Bun/')) return 'BUN';
if (userAgentStartsWith('Cloudflare-Workers')) return 'CLOUDFLARE';
if (userAgentStartsWith('Deno/')) return 'DENO';
if (userAgentStartsWith('Node.js/')) return 'NODE';
if (globalThis.Bun && typeof Bun.version == 'string') return 'BUN';
if (globalThis.Deno && typeof Deno.version == 'object') return 'DENO';
if (classof(globalThis.process) === 'process') return 'NODE';
if (globalThis.window && globalThis.document) return 'BROWSER';
return 'REST';
})();
2 changes: 1 addition & 1 deletion packages/core-js/internals/get-built-in-node-module.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';
var IS_NODE = require('../internals/engine-is-node');
var globalThis = require('../internals/global');
var IS_NODE = require('../internals/environment-is-node');

module.exports = function (name) {
if (IS_NODE) {
Expand Down
8 changes: 4 additions & 4 deletions packages/core-js/internals/microtask.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ var safeGetBuiltIn = require('../internals/safe-get-built-in');
var bind = require('../internals/function-bind-context');
var macrotask = require('../internals/task').set;
var Queue = require('../internals/queue');
var IS_IOS = require('../internals/engine-is-ios');
var IS_IOS_PEBBLE = require('../internals/engine-is-ios-pebble');
var IS_WEBOS_WEBKIT = require('../internals/engine-is-webos-webkit');
var IS_NODE = require('../internals/engine-is-node');
var IS_IOS = require('../internals/environment-is-ios');
var IS_IOS_PEBBLE = require('../internals/environment-is-ios-pebble');
var IS_WEBOS_WEBKIT = require('../internals/environment-is-webos-webkit');
var IS_NODE = require('../internals/environment-is-node');

var MutationObserver = global.MutationObserver || global.WebKitMutationObserver;
var document = global.document;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
var IS_PURE = require('../internals/is-pure');
var global = require('../internals/global');
var fails = require('../internals/fails');
var WEBKIT = require('../internals/engine-webkit-version');
var WEBKIT = require('../internals/environment-webkit-version');

// Forced replacement object prototype accessors methods
module.exports = IS_PURE || !fails(function () {
Expand Down
7 changes: 3 additions & 4 deletions packages/core-js/internals/promise-constructor-detection.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ var isCallable = require('../internals/is-callable');
var isForced = require('../internals/is-forced');
var inspectSource = require('../internals/inspect-source');
var wellKnownSymbol = require('../internals/well-known-symbol');
var IS_BROWSER = require('../internals/engine-is-browser');
var IS_DENO = require('../internals/engine-is-deno');
var ENVIRONMENT = require('../internals/environment');
var IS_PURE = require('../internals/is-pure');
var V8_VERSION = require('../internals/engine-v8-version');
var V8_VERSION = require('../internals/environment-v8-version');

var NativePromisePrototype = NativePromiseConstructor && NativePromiseConstructor.prototype;
var SPECIES = wellKnownSymbol('species');
Expand Down Expand Up @@ -38,7 +37,7 @@ var FORCED_PROMISE_CONSTRUCTOR = isForced('Promise', function () {
SUBCLASSING = promise.then(function () { /* empty */ }) instanceof FakePromise;
if (!SUBCLASSING) return true;
// Unhandled rejections tracking support, NodeJS Promise without it fails @@species test
} return !GLOBAL_CORE_JS_PROMISE && (IS_BROWSER || IS_DENO) && !NATIVE_PROMISE_REJECTION_EVENT;
} return !GLOBAL_CORE_JS_PROMISE && (ENVIRONMENT === 'BROWSER' || ENVIRONMENT === 'DENO') && !NATIVE_PROMISE_REJECTION_EVENT;
});

module.exports = {
Expand Down
6 changes: 3 additions & 3 deletions packages/core-js/internals/schedulers-fix.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
var global = require('../internals/global');
var apply = require('../internals/function-apply');
var isCallable = require('../internals/is-callable');
var ENGINE_IS_BUN = require('../internals/engine-is-bun');
var USER_AGENT = require('../internals/engine-user-agent');
var ENVIRONMENT = require('../internals/environment');
var USER_AGENT = require('../internals/environment-user-agent');
var arraySlice = require('../internals/array-slice');
var validateArgumentsLength = require('../internals/validate-arguments-length');

var Function = global.Function;
// dirty IE9- and Bun 0.3.0- checks
var WRAP = /MSIE .\./.test(USER_AGENT) || ENGINE_IS_BUN && (function () {
var WRAP = /MSIE .\./.test(USER_AGENT) || ENVIRONMENT === 'BUN' && (function () {
var version = global.Bun.version.split('.');
return version.length < 3 || version[0] === '0' && (version[1] < 3 || version[1] === '3' && version[2] === '0');
})();
Expand Down
2 changes: 1 addition & 1 deletion packages/core-js/internals/string-pad-webkit-bug.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';
// https://github.com/zloirock/core-js/issues/280
var userAgent = require('../internals/engine-user-agent');
var userAgent = require('../internals/environment-user-agent');

module.exports = /Version\/10(?:\.\d+){1,2}(?: [\w./]+)?(?: Mobile\/\w+)? Safari\//.test(userAgent);
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
'use strict';
var global = require('../internals/global');
var fails = require('../internals/fails');
var V8 = require('../internals/engine-v8-version');
var IS_BROWSER = require('../internals/engine-is-browser');
var IS_DENO = require('../internals/engine-is-deno');
var IS_NODE = require('../internals/engine-is-node');
var V8 = require('../internals/environment-v8-version');
var ENVIRONMENT = require('../internals/environment');

var structuredClone = global.structuredClone;

module.exports = !!structuredClone && !fails(function () {
// prevent V8 ArrayBufferDetaching protector cell invalidation and performance degradation
// https://github.com/zloirock/core-js/issues/679
if ((IS_DENO && V8 > 92) || (IS_NODE && V8 > 94) || (IS_BROWSER && V8 > 97)) return false;
if ((ENVIRONMENT === 'DENO' && V8 > 92) || (ENVIRONMENT === 'NODE' && V8 > 94) || (ENVIRONMENT === 'BROWSER' && V8 > 97)) return false;
var buffer = new ArrayBuffer(8);
var clone = structuredClone(buffer, { transfer: [buffer] });
return buffer.byteLength !== 0 || clone.byteLength !== 8;
Expand Down
2 changes: 1 addition & 1 deletion packages/core-js/internals/symbol-constructor-detection.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';
/* eslint-disable es/no-symbol -- required for testing */
var V8_VERSION = require('../internals/engine-v8-version');
var V8_VERSION = require('../internals/environment-v8-version');
var fails = require('../internals/fails');
var global = require('../internals/global');

Expand Down
4 changes: 2 additions & 2 deletions packages/core-js/internals/task.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ var html = require('../internals/html');
var arraySlice = require('../internals/array-slice');
var createElement = require('../internals/document-create-element');
var validateArgumentsLength = require('../internals/validate-arguments-length');
var IS_IOS = require('../internals/engine-is-ios');
var IS_NODE = require('../internals/engine-is-node');
var IS_IOS = require('../internals/environment-is-ios');
var IS_NODE = require('../internals/environment-is-node');

var set = global.setImmediate;
var clear = global.clearImmediate;
Expand Down
2 changes: 1 addition & 1 deletion packages/core-js/modules/es.array.concat.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var createProperty = require('../internals/create-property');
var arraySpeciesCreate = require('../internals/array-species-create');
var arrayMethodHasSpeciesSupport = require('../internals/array-method-has-species-support');
var wellKnownSymbol = require('../internals/well-known-symbol');
var V8_VERSION = require('../internals/engine-v8-version');
var V8_VERSION = require('../internals/environment-v8-version');

var IS_CONCAT_SPREADABLE = wellKnownSymbol('isConcatSpreadable');

Expand Down
4 changes: 2 additions & 2 deletions packages/core-js/modules/es.array.reduce-right.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
var $ = require('../internals/export');
var $reduceRight = require('../internals/array-reduce').right;
var arrayMethodIsStrict = require('../internals/array-method-is-strict');
var CHROME_VERSION = require('../internals/engine-v8-version');
var IS_NODE = require('../internals/engine-is-node');
var CHROME_VERSION = require('../internals/environment-v8-version');
var IS_NODE = require('../internals/environment-is-node');

// Chrome 80-82 has a critical bug
// https://bugs.chromium.org/p/chromium/issues/detail?id=1049982
Expand Down
4 changes: 2 additions & 2 deletions packages/core-js/modules/es.array.reduce.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
var $ = require('../internals/export');
var $reduce = require('../internals/array-reduce').left;
var arrayMethodIsStrict = require('../internals/array-method-is-strict');
var CHROME_VERSION = require('../internals/engine-v8-version');
var IS_NODE = require('../internals/engine-is-node');
var CHROME_VERSION = require('../internals/environment-v8-version');
var IS_NODE = require('../internals/environment-is-node');

// Chrome 80-82 has a critical bug
// https://bugs.chromium.org/p/chromium/issues/detail?id=1049982
Expand Down
8 changes: 4 additions & 4 deletions packages/core-js/modules/es.array.sort.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ var toString = require('../internals/to-string');
var fails = require('../internals/fails');
var internalSort = require('../internals/array-sort');
var arrayMethodIsStrict = require('../internals/array-method-is-strict');
var FF = require('../internals/engine-ff-version');
var IE_OR_EDGE = require('../internals/engine-is-ie-or-edge');
var V8 = require('../internals/engine-v8-version');
var WEBKIT = require('../internals/engine-webkit-version');
var FF = require('../internals/environment-ff-version');
var IE_OR_EDGE = require('../internals/environment-is-ie-or-edge');
var V8 = require('../internals/environment-v8-version');
var WEBKIT = require('../internals/environment-webkit-version');

var test = [];
var nativeSort = uncurryThis(test.sort);
Expand Down
2 changes: 1 addition & 1 deletion packages/core-js/modules/es.promise.constructor.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';
var $ = require('../internals/export');
var IS_PURE = require('../internals/is-pure');
var IS_NODE = require('../internals/engine-is-node');
var IS_NODE = require('../internals/environment-is-node');
var global = require('../internals/global');
var call = require('../internals/function-call');
var defineBuiltIn = require('../internals/define-built-in');
Expand Down
8 changes: 4 additions & 4 deletions packages/core-js/modules/es.typed-array.sort.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ var fails = require('../internals/fails');
var aCallable = require('../internals/a-callable');
var internalSort = require('../internals/array-sort');
var ArrayBufferViewCore = require('../internals/array-buffer-view-core');
var FF = require('../internals/engine-ff-version');
var IE_OR_EDGE = require('../internals/engine-is-ie-or-edge');
var V8 = require('../internals/engine-v8-version');
var WEBKIT = require('../internals/engine-webkit-version');
var FF = require('../internals/environment-ff-version');
var IE_OR_EDGE = require('../internals/environment-is-ie-or-edge');
var V8 = require('../internals/environment-v8-version');
var WEBKIT = require('../internals/environment-webkit-version');

var aTypedArray = ArrayBufferViewCore.aTypedArray;
var exportTypedArrayMethod = ArrayBufferViewCore.exportTypedArrayMethod;
Expand Down

0 comments on commit 25b5716

Please sign in to comment.