Skip to content

Commit

Permalink
[core-util] Create isNodeLike and isNodeRuntime (Azure#29086)
Browse files Browse the repository at this point in the history
### Packages impacted by this PR

- @azure/core-util

### Issues associated with this PR

- Azure#28101

### Describe the problem that is addressed by this PR

Reverts the behavior of `isNode` checks to ensure that it can still
return true for Bun and Deno. Introducing `isNodeLike` to also have the
same behavior as `isNode` so we can deprecate the former function. We
have also added `isNodeRuntime` which then checks for an explicit
Node.js runtime.

### What are the possible designs available to address the problem? If
there are more than one possible design, why was the one in this PR
chosen?

The previous attempt was to create a `isNodeLike` and be opt-in, versus
this PR which gives the old behavior and new opt-in for strict Node
runtimes.

### Are there test cases added in this PR? _(If not, why?)_


### Provide a list of related PRs _(if any)_

- Azure#29083

### Command used to generate this PR:**_(Applicable only to SDK release
request PRs)_

### Checklists
- [x] Added impacted package name to the issue description
- [ ] Does this PR needs any fixes in the SDK Generator?** _(If so,
create an Issue in the
[Autorest/typescript](https://github.com/Azure/autorest.typescript)
repository and link it here)_
- [ ] Added a changelog (if necessary)

---------

Co-authored-by: Deyaaeldeen Almahallawi <[email protected]>
  • Loading branch information
mpodwysocki and deyaaeldeen authored Mar 27, 2024
1 parent 0f49259 commit e9f9ff8
Show file tree
Hide file tree
Showing 10 changed files with 133 additions and 19 deletions.
8 changes: 7 additions & 1 deletion sdk/core/core-util/review/core-util.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
20 changes: 14 additions & 6 deletions sdk/core/core-util/src/checkEnvironment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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/[email protected]/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.
Expand Down
2 changes: 2 additions & 0 deletions sdk/core/core-util/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export {
isBrowser,
isBun,
isNode,
isNodeLike,
isNodeRuntime,
isDeno,
isReactNative,
isWebWorker,
Expand Down
22 changes: 21 additions & 1 deletion sdk/core/core-util/test/public/browser/checkEnvironment.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {
isBun,
isDeno,
isNode,
isNodeLike,
isNodeRuntime,
isReactNative,
isWebWorker,
} from "../../../src/index.js";
Expand All @@ -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);
Expand Down
14 changes: 14 additions & 0 deletions sdk/core/core-util/test/public/node/checkEnvironment.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {
isBun,
isDeno,
isNode,
isNodeLike,
isNodeRuntime,
isReactNative,
isWebWorker,
} from "../../../src/index.js";
Expand Down Expand Up @@ -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);
Expand Down
8 changes: 7 additions & 1 deletion sdk/core/ts-http-runtime/review/ts-http-runtime.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 2 additions & 0 deletions sdk/core/ts-http-runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ export {
isBrowser,
isBun,
isNode,
isNodeLike,
isNodeRuntime,
isDeno,
isReactNative,
isWebWorker,
Expand Down
22 changes: 15 additions & 7 deletions sdk/core/ts-http-runtime/src/util/checkEnvironment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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/[email protected]/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.
Expand Down
31 changes: 29 additions & 2 deletions sdk/core/ts-http-runtime/test/browser/checkEnvironment.spec.ts
Original file line number Diff line number Diff line change
@@ -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 () {
Expand All @@ -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);
Expand Down
23 changes: 22 additions & 1 deletion sdk/core/ts-http-runtime/test/node/checkEnvironment.spec.ts
Original file line number Diff line number Diff line change
@@ -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 () {
Expand All @@ -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);
Expand Down

0 comments on commit e9f9ff8

Please sign in to comment.