forked from Azure/azure-sdk-for-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[core-util] Add environment checks (Azure#26018)
### 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
1 parent
bbeef36
commit fe18e3b
Showing
10 changed files
with
181 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
43 changes: 43 additions & 0 deletions
43
sdk/core/core-util/test/public/browser/checkEnvironment.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.
43 changes: 43 additions & 0 deletions
43
sdk/core/core-util/test/public/node/checkEnvironment.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.