@aptos-labs/ts-sdk
Version:
Aptos TypeScript SDK
71 lines • 2.22 kB
JavaScript
// Copyright © Aptos Foundation
// SPDX-License-Identifier: Apache-2.0
/**
* Detects the JavaScript runtime the SDK is currently running in.
*
* Detection precedence (most specific first): react-native, deno, bun, browser, node.
* Falls back to "unknown" if detection fails or the environment is hostile.
*
* @internal
*/
export function getRuntimePlatform() {
try {
const g = globalThis;
if (typeof g.navigator !== "undefined" && g.navigator.product === "ReactNative") {
return "react-native";
}
if (typeof g.Deno !== "undefined")
return "deno";
if (typeof g.Bun !== "undefined")
return "bun";
if (typeof g.window !== "undefined" && typeof g.document !== "undefined")
return "browser";
if (g.process?.versions?.node)
return "node";
}
catch {
// Fall through to "unknown" in hostile environments.
}
return "unknown";
}
/**
* Returns a compact runtime tag for the `x-aptos-client` telemetry header.
* Node, Bun, and Deno expose their engine version cheaply and synchronously;
* browsers do not (userAgent is already forwarded as `User-Agent`), and React
* Native's JS-engine version is not reachable from `src/` without a
* platform-specific import.
*
* Shape examples:
* - "node/22.12.0"
* - "bun/1.1.38"
* - "deno/2.1.4"
* - "browser" | "react-native" | "unknown" (no version suffix)
*
* @internal
*/
export function getRuntimePlatformTag() {
const platform = getRuntimePlatform();
try {
const g = globalThis;
switch (platform) {
case "node": {
const v = g.process?.versions?.node;
return v ? `node/${v}` : "node";
}
case "bun": {
const v = g.Bun?.version ?? g.process?.versions?.bun;
return v ? `bun/${v}` : "bun";
}
case "deno": {
const v = g.Deno?.version?.deno;
return v ? `deno/${v}` : "deno";
}
default:
return platform;
}
}
catch {
return platform;
}
}
//# sourceMappingURL=runtime.js.map