Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[core-util] Create isNodeLike and isNodeRuntime #29086

Merged
merged 4 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/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.
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
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 () {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test names not accurate.

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/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.
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