diff --git a/sdk/core/core-util/review/core-util.api.md b/sdk/core/core-util/review/core-util.api.md index 8199faa59f1a..3db25b8de4e2 100644 --- a/sdk/core/core-util/review/core-util.api.md +++ b/sdk/core/core-util/review/core-util.api.md @@ -67,9 +67,15 @@ export const isDeno: boolean; // @public export function isError(e: unknown): e is Error; -// @public +// @public @deprecated export const isNode: boolean; +// @public +export const isNodeLike: boolean; + +// @public +export const isNodeRuntime: boolean; + // @public export function isObject(input: unknown): input is UnknownObject; diff --git a/sdk/core/core-util/src/checkEnvironment.ts b/sdk/core/core-util/src/checkEnvironment.ts index 15f2920a14a0..a15ae9643719 100644 --- a/sdk/core/core-util/src/checkEnvironment.ts +++ b/sdk/core/core-util/src/checkEnvironment.ts @@ -64,15 +64,23 @@ export const isDeno = export const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; /** - * A constant that indicates whether the environment the code is running is Node.JS. + * A constant that indicates whether the environment the code is running is a Node.js compatible environment. */ -export const isNode = +export const isNodeLike = typeof globalThis.process !== "undefined" && Boolean(globalThis.process.version) && - Boolean(globalThis.process.versions?.node) && - // Deno thought it was a good idea to spoof process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; + Boolean(globalThis.process.versions?.node); + +/** + * A constant that indicates whether the environment the code is running is a Node.js compatible environment. + * @deprecated Use `isNodeLike` instead. + */ +export const isNode = isNodeLike; + +/** + * A constant that indicates whether the environment the code is running is Node.JS. + */ +export const isNodeRuntime = isNodeLike && !isBun && !isDeno; /** * A constant that indicates whether the environment the code is running is in React-Native. diff --git a/sdk/core/core-util/src/index.ts b/sdk/core/core-util/src/index.ts index e3a1b862622c..ad0d8a3390b5 100644 --- a/sdk/core/core-util/src/index.ts +++ b/sdk/core/core-util/src/index.ts @@ -21,6 +21,8 @@ export { isBrowser, isBun, isNode, + isNodeLike, + isNodeRuntime, isDeno, isReactNative, isWebWorker, diff --git a/sdk/core/core-util/test/public/browser/checkEnvironment.spec.ts b/sdk/core/core-util/test/public/browser/checkEnvironment.spec.ts index 1949a41813cf..6a4a785b0edd 100644 --- a/sdk/core/core-util/test/public/browser/checkEnvironment.spec.ts +++ b/sdk/core/core-util/test/public/browser/checkEnvironment.spec.ts @@ -6,6 +6,8 @@ import { isBun, isDeno, isNode, + isNodeLike, + isNodeRuntime, isReactNative, isWebWorker, } from "../../../src/index.js"; @@ -30,12 +32,30 @@ describe("checkEnvironment (browser)", function () { }); }); - describe("isNode (browser)", function () { + describe("isNode(browser)", function () { it("should return true", async function () { assert.isFalse(isNode); }); }); + describe("isNodeRuntime (browser)", function () { + it("should return true", async function () { + assert.isFalse(isNodeRuntime); + }); + }); + + describe("isNodeLike (browser)", function () { + it("should return false", async function () { + assert.isFalse(isNodeLike); + }); + }); + + describe("isNodeRuntime (browser)", function () { + it("should return false", async function () { + assert.isFalse(isNodeRuntime); + }); + }); + describe("isReactNative (browser)", function () { it("should return false", async function () { assert.isFalse(isReactNative); diff --git a/sdk/core/core-util/test/public/node/checkEnvironment.spec.ts b/sdk/core/core-util/test/public/node/checkEnvironment.spec.ts index d5d767bb8ae0..48dfd7d94c4b 100644 --- a/sdk/core/core-util/test/public/node/checkEnvironment.spec.ts +++ b/sdk/core/core-util/test/public/node/checkEnvironment.spec.ts @@ -6,6 +6,8 @@ import { isBun, isDeno, isNode, + isNodeLike, + isNodeRuntime, isReactNative, isWebWorker, } from "../../../src/index.js"; @@ -36,6 +38,18 @@ describe("checkEnvironment (node)", function () { }); }); + describe("isNodeLike (node)", function () { + it("should return true", async function () { + assert.isTrue(isNodeLike); + }); + }); + + describe("isNodeRuntime (node)", function () { + it("should return true", async function () { + assert.isTrue(isNodeRuntime); + }); + }); + describe("isReactNative (node)", function () { it("should return false", async function () { assert.isFalse(isReactNative); diff --git a/sdk/core/ts-http-runtime/review/ts-http-runtime.api.md b/sdk/core/ts-http-runtime/review/ts-http-runtime.api.md index 6e02bc22c417..6a648761514e 100644 --- a/sdk/core/ts-http-runtime/review/ts-http-runtime.api.md +++ b/sdk/core/ts-http-runtime/review/ts-http-runtime.api.md @@ -375,9 +375,15 @@ export function isError(e: unknown): e is Error; // @public export function isKeyCredential(credential: unknown): credential is KeyCredential; -// @public +// @public @deprecated export const isNode: boolean; +// @public +export const isNodeLike: boolean; + +// @public +export const isNodeRuntime: boolean; + // @public export function isObject(input: unknown): input is UnknownObject; diff --git a/sdk/core/ts-http-runtime/src/index.ts b/sdk/core/ts-http-runtime/src/index.ts index 956a5552536e..e47e002e668e 100644 --- a/sdk/core/ts-http-runtime/src/index.ts +++ b/sdk/core/ts-http-runtime/src/index.ts @@ -133,6 +133,8 @@ export { isBrowser, isBun, isNode, + isNodeLike, + isNodeRuntime, isDeno, isReactNative, isWebWorker, diff --git a/sdk/core/ts-http-runtime/src/util/checkEnvironment.ts b/sdk/core/ts-http-runtime/src/util/checkEnvironment.ts index b88affe4698f..ba911ae085f1 100644 --- a/sdk/core/ts-http-runtime/src/util/checkEnvironment.ts +++ b/sdk/core/ts-http-runtime/src/util/checkEnvironment.ts @@ -45,16 +45,24 @@ export const isDeno = typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; +/** + * A constant that indicates whether the environment the code is running is a Node.js compatible environment. + */ +export const isNodeLike = + typeof globalThis.process !== "undefined" && + Boolean(globalThis.process.version) && + Boolean(globalThis.process.versions?.node); + +/** + * A constant that indicates whether the environment the code is running is a Node.js compatible environment. + * @deprecated Use `isNodeLike` instead. + */ +export const isNode = isNodeLike; + /** * A constant that indicates whether the environment the code is running is Node.JS. */ -export const isNode = - typeof process !== "undefined" && - Boolean(process.version) && - Boolean(process.versions?.node) && - // Deno thought it was a good idea to spoof process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions - !isDeno && - !isBun; +export const isNodeRuntime = isNodeLike && !isBun && !isDeno; /** * A constant that indicates whether the environment the code is running is in React-Native. diff --git a/sdk/core/ts-http-runtime/test/browser/checkEnvironment.spec.ts b/sdk/core/ts-http-runtime/test/browser/checkEnvironment.spec.ts index 41fa19aeae24..f3f10f89ea07 100644 --- a/sdk/core/ts-http-runtime/test/browser/checkEnvironment.spec.ts +++ b/sdk/core/ts-http-runtime/test/browser/checkEnvironment.spec.ts @@ -1,8 +1,17 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT license. +import { + isBrowser, + isBun, + isDeno, + isNode, + isNodeLike, + isNodeRuntime, + isReactNative, + isWebWorker, +} from "../../src/index.js"; import { describe, it, assert } from "vitest"; -import { isBrowser, isBun, isDeno, isNode, isReactNative, isWebWorker } from "../../src/index.js"; describe("checkEnvironment (browser)", function () { describe("isBun (browser)", function () { @@ -23,12 +32,30 @@ describe("checkEnvironment (browser)", function () { }); }); - describe("isNode (browser)", function () { + describe("isNode(browser)", function () { it("should return true", async function () { assert.isFalse(isNode); }); }); + describe("isNodeRuntime (browser)", function () { + it("should return true", async function () { + assert.isFalse(isNodeRuntime); + }); + }); + + describe("isNodeLike (browser)", function () { + it("should return false", async function () { + assert.isFalse(isNodeLike); + }); + }); + + describe("isNodeRuntime (browser)", function () { + it("should return false", async function () { + assert.isFalse(isNodeRuntime); + }); + }); + describe("isReactNative (browser)", function () { it("should return false", async function () { assert.isFalse(isReactNative); diff --git a/sdk/core/ts-http-runtime/test/node/checkEnvironment.spec.ts b/sdk/core/ts-http-runtime/test/node/checkEnvironment.spec.ts index 91c251822eb3..0d356ba31547 100644 --- a/sdk/core/ts-http-runtime/test/node/checkEnvironment.spec.ts +++ b/sdk/core/ts-http-runtime/test/node/checkEnvironment.spec.ts @@ -1,8 +1,17 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT license. +import { + isBrowser, + isBun, + isDeno, + isNode, + isNodeLike, + isNodeRuntime, + isReactNative, + isWebWorker, +} from "../../src/index.js"; import { describe, it, assert } from "vitest"; -import { isBrowser, isBun, isDeno, isNode, isReactNative, isWebWorker } from "../../src/index.js"; describe("checkEnvironment (node)", function () { describe("isBun (node)", function () { @@ -29,6 +38,18 @@ describe("checkEnvironment (node)", function () { }); }); + describe("isNodeLike (node)", function () { + it("should return true", async function () { + assert.isTrue(isNodeLike); + }); + }); + + describe("isNodeRuntime (node)", function () { + it("should return true", async function () { + assert.isTrue(isNodeRuntime); + }); + }); + describe("isReactNative (node)", function () { it("should return false", async function () { assert.isFalse(isReactNative);