Skip to content

Commit

Permalink
[core-util] Add environment checks (Azure#26018)
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#26003

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

Adds extra environment checks for browser or other environments such as
Deno, Bun, React-Native, Web Workers, etc

### 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?


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


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


### 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)
  • Loading branch information
mpodwysocki authored and minhanh-phan committed Jun 12, 2023
1 parent bbeef36 commit fe18e3b
Show file tree
Hide file tree
Showing 10 changed files with 181 additions and 39 deletions.
1 change: 0 additions & 1 deletion sdk/core/core-util/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"main": "dist/index.js",
"module": "dist-esm/src/index.js",
"browser": {
"./dist-esm/src/isNode.js": "./dist-esm/src/isNode.browser.js",
"./dist-esm/src/sha256.js": "./dist-esm/src/sha256.browser.js",
"./dist-esm/src/uuidUtils.js": "./dist-esm/src/uuidUtils.browser.js"
},
Expand Down
15 changes: 15 additions & 0 deletions sdk/core/core-util/review/core-util.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,18 @@ export function getErrorMessage(e: unknown): string;
// @public
export function getRandomIntegerInclusive(min: number, max: number): number;

// @public
export const isBrowser: boolean;

// @public
export const isBun: boolean;

// @public
export function isDefined<T>(thing: T | undefined | null): thing is T;

// @public
export const isDeno: boolean;

// @public
export function isError(e: unknown): e is Error;

Expand All @@ -52,6 +61,12 @@ export function isObject(input: unknown): input is UnknownObject;
// @public
export function isObjectWithProperties<Thing, PropertyName extends string>(thing: Thing, properties: PropertyName[]): thing is Thing & Record<PropertyName, unknown>;

// @public
export const isReactNative: boolean;

// @public
export const isWebWorker: boolean;

// @public
export function objectHasProperty<Thing, PropertyName extends string>(thing: Thing, property: PropertyName): thing is Thing & Record<PropertyName, unknown>;

Expand Down
79 changes: 79 additions & 0 deletions sdk/core/core-util/src/checkEnvironment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

declare global {
interface Window {
document: unknown;
}

interface DedicatedWorkerGlobalScope {
constructor: {
name: string;
};

importScripts: (...paths: string[]) => void;
}

interface Navigator {
product: string;
}

interface DenoGlobal {
version: {
deno: string;
};
}

interface BunGlobal {
version: string;
}

// eslint-disable-next-line @azure/azure-sdk/ts-no-window
const window: Window;
const self: DedicatedWorkerGlobalScope;
const Deno: DenoGlobal;
const Bun: BunGlobal;
const navigator: Navigator;
}

/**
* A constant that indicates whether the environment the code is running is a Web Browser.
*/
// eslint-disable-next-line @azure/azure-sdk/ts-no-window
export const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined";

/**
* A constant that indicates whether the environment the code is running is a Web Worker.
*/
export const isWebWorker =
typeof self === "object" &&
typeof self?.importScripts === "function" &&
(self.constructor?.name === "DedicatedWorkerGlobalScope" ||
self.constructor?.name === "ServiceWorkerGlobalScope" ||
self.constructor?.name === "SharedWorkerGlobalScope");

/**
* 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);

/**
* A constant that indicates whether the environment the code is running is Deno.
*/
export const isDeno =
typeof Deno !== "undefined" &&
typeof Deno.version !== "undefined" &&
typeof Deno.version.deno !== "undefined";

/**
* A constant that indicates whether the environment the code is running is Bun.sh.
*/
export const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined";

/**
* A constant that indicates whether the environment the code is running is in React-Native.
*/
// https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js
export const isReactNative =
typeof navigator !== "undefined" && navigator?.product === "ReactNative";
2 changes: 1 addition & 1 deletion sdk/core/core-util/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

export { isNode } from "./isNode";
export { delay, DelayOptions } from "./delay";
export { createAbortablePromise, CreateAbortablePromiseOptions } from "./createAbortablePromise";
export { getRandomIntegerInclusive } from "./random";
Expand All @@ -10,3 +9,4 @@ export { isError, getErrorMessage } from "./error";
export { computeSha256Hash, computeSha256Hmac } from "./sha256";
export { isDefined, isObjectWithProperties, objectHasProperty } from "./typeGuards";
export { randomUUID } from "./uuidUtils";
export { isBrowser, isBun, isNode, isDeno, isReactNative, isWebWorker } from "./checkEnvironment";
7 changes: 0 additions & 7 deletions sdk/core/core-util/src/isNode.browser.ts

This file was deleted.

8 changes: 0 additions & 8 deletions sdk/core/core-util/src/isNode.ts

This file was deleted.

43 changes: 43 additions & 0 deletions sdk/core/core-util/test/public/browser/checkEnvironment.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { isBrowser, isBun, isDeno, isNode, isReactNative, isWebWorker } from "../../../src";
import { assert } from "chai";

describe("checkEnvironment (browser)", function () {
describe("isBun (browser)", function () {
it("should return false", async function () {
assert.isFalse(isBun);
});
});

describe("isBrowser (browser)", function () {
it("should return true", async function () {
assert.isTrue(isBrowser);
});
});

describe("isDeno (browser)", function () {
it("should return false", async function () {
assert.isFalse(isDeno);
});
});

describe("isNode (browser)", function () {
it("should return true", async function () {
assert.isFalse(isNode);
});
});

describe("isReactNative (browser)", function () {
it("should return false", async function () {
assert.isFalse(isReactNative);
});
});

describe("isWebWorker (browser)", function () {
it("should return false", async function () {
assert.isFalse(isWebWorker);
});
});
});
11 changes: 0 additions & 11 deletions sdk/core/core-util/test/public/browser/isNode.spec.ts

This file was deleted.

43 changes: 43 additions & 0 deletions sdk/core/core-util/test/public/node/checkEnvironment.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { isBrowser, isBun, isDeno, isNode, isReactNative, isWebWorker } from "../../../src";
import { assert } from "chai";

describe("checkEnvironment (node)", function () {
describe("isBun (node)", function () {
it("should return false", async function () {
assert.isFalse(isBun);
});
});

describe("isBrowser (node)", function () {
it("should return false", async function () {
assert.isFalse(isBrowser);
});
});

describe("isDeno (node)", function () {
it("should return false", async function () {
assert.isFalse(isDeno);
});
});

describe("isNode (node)", function () {
it("should return true", async function () {
assert.isTrue(isNode);
});
});

describe("isReactNative (node)", function () {
it("should return false", async function () {
assert.isFalse(isReactNative);
});
});

describe("isWebWorker (node)", function () {
it("should return false", async function () {
assert.isFalse(isWebWorker);
});
});
});
11 changes: 0 additions & 11 deletions sdk/core/core-util/test/public/node/isNode.spec.ts

This file was deleted.

0 comments on commit fe18e3b

Please sign in to comment.