| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
- import { VERSION } from "../version.mjs";
- export const isRunningInBrowser = () => {
- return (
- // @ts-ignore
- typeof window !== 'undefined' &&
- // @ts-ignore
- typeof window.document !== 'undefined' &&
- // @ts-ignore
- typeof navigator !== 'undefined');
- };
- /**
- * Note this does not detect 'browser'; for that, use getBrowserInfo().
- */
- function getDetectedPlatform() {
- if (typeof Deno !== 'undefined' && Deno.build != null) {
- return 'deno';
- }
- if (typeof EdgeRuntime !== 'undefined') {
- return 'edge';
- }
- if (Object.prototype.toString.call(typeof globalThis.process !== 'undefined' ? globalThis.process : 0) === '[object process]') {
- return 'node';
- }
- return 'unknown';
- }
- const getPlatformProperties = () => {
- const detectedPlatform = getDetectedPlatform();
- if (detectedPlatform === 'deno') {
- return {
- 'X-Stainless-Lang': 'js',
- 'X-Stainless-Package-Version': VERSION,
- 'X-Stainless-OS': normalizePlatform(Deno.build.os),
- 'X-Stainless-Arch': normalizeArch(Deno.build.arch),
- 'X-Stainless-Runtime': 'deno',
- 'X-Stainless-Runtime-Version': typeof Deno.version === 'string' ? Deno.version : Deno.version?.deno ?? 'unknown',
- };
- }
- if (typeof EdgeRuntime !== 'undefined') {
- return {
- 'X-Stainless-Lang': 'js',
- 'X-Stainless-Package-Version': VERSION,
- 'X-Stainless-OS': 'Unknown',
- 'X-Stainless-Arch': `other:${EdgeRuntime}`,
- 'X-Stainless-Runtime': 'edge',
- 'X-Stainless-Runtime-Version': globalThis.process.version,
- };
- }
- // Check if Node.js
- if (detectedPlatform === 'node') {
- return {
- 'X-Stainless-Lang': 'js',
- 'X-Stainless-Package-Version': VERSION,
- 'X-Stainless-OS': normalizePlatform(globalThis.process.platform ?? 'unknown'),
- 'X-Stainless-Arch': normalizeArch(globalThis.process.arch ?? 'unknown'),
- 'X-Stainless-Runtime': 'node',
- 'X-Stainless-Runtime-Version': globalThis.process.version ?? 'unknown',
- };
- }
- const browserInfo = getBrowserInfo();
- if (browserInfo) {
- return {
- 'X-Stainless-Lang': 'js',
- 'X-Stainless-Package-Version': VERSION,
- 'X-Stainless-OS': 'Unknown',
- 'X-Stainless-Arch': 'unknown',
- 'X-Stainless-Runtime': `browser:${browserInfo.browser}`,
- 'X-Stainless-Runtime-Version': browserInfo.version,
- };
- }
- // TODO add support for Cloudflare workers, etc.
- return {
- 'X-Stainless-Lang': 'js',
- 'X-Stainless-Package-Version': VERSION,
- 'X-Stainless-OS': 'Unknown',
- 'X-Stainless-Arch': 'unknown',
- 'X-Stainless-Runtime': 'unknown',
- 'X-Stainless-Runtime-Version': 'unknown',
- };
- };
- // Note: modified from https://github.com/JS-DevTools/host-environment/blob/b1ab79ecde37db5d6e163c050e54fe7d287d7c92/src/isomorphic.browser.ts
- function getBrowserInfo() {
- if (typeof navigator === 'undefined' || !navigator) {
- return null;
- }
- // NOTE: The order matters here!
- const browserPatterns = [
- { key: 'edge', pattern: /Edge(?:\W+(\d+)\.(\d+)(?:\.(\d+))?)?/ },
- { key: 'ie', pattern: /MSIE(?:\W+(\d+)\.(\d+)(?:\.(\d+))?)?/ },
- { key: 'ie', pattern: /Trident(?:.*rv\:(\d+)\.(\d+)(?:\.(\d+))?)?/ },
- { key: 'chrome', pattern: /Chrome(?:\W+(\d+)\.(\d+)(?:\.(\d+))?)?/ },
- { key: 'firefox', pattern: /Firefox(?:\W+(\d+)\.(\d+)(?:\.(\d+))?)?/ },
- { key: 'safari', pattern: /(?:Version\W+(\d+)\.(\d+)(?:\.(\d+))?)?(?:\W+Mobile\S*)?\W+Safari/ },
- ];
- // Find the FIRST matching browser
- for (const { key, pattern } of browserPatterns) {
- const match = pattern.exec(navigator.userAgent);
- if (match) {
- const major = match[1] || 0;
- const minor = match[2] || 0;
- const patch = match[3] || 0;
- return { browser: key, version: `${major}.${minor}.${patch}` };
- }
- }
- return null;
- }
- const normalizeArch = (arch) => {
- // Node docs:
- // - https://nodejs.org/api/process.html#processarch
- // Deno docs:
- // - https://doc.deno.land/deno/stable/~/Deno.build
- if (arch === 'x32')
- return 'x32';
- if (arch === 'x86_64' || arch === 'x64')
- return 'x64';
- if (arch === 'arm')
- return 'arm';
- if (arch === 'aarch64' || arch === 'arm64')
- return 'arm64';
- if (arch)
- return `other:${arch}`;
- return 'unknown';
- };
- const normalizePlatform = (platform) => {
- // Node platforms:
- // - https://nodejs.org/api/process.html#processplatform
- // Deno platforms:
- // - https://doc.deno.land/deno/stable/~/Deno.build
- // - https://github.com/denoland/deno/issues/14799
- platform = platform.toLowerCase();
- // NOTE: this iOS check is untested and may not work
- // Node does not work natively on IOS, there is a fork at
- // https://github.com/nodejs-mobile/nodejs-mobile
- // however it is unknown at the time of writing how to detect if it is running
- if (platform.includes('ios'))
- return 'iOS';
- if (platform === 'android')
- return 'Android';
- if (platform === 'darwin')
- return 'MacOS';
- if (platform === 'win32')
- return 'Windows';
- if (platform === 'freebsd')
- return 'FreeBSD';
- if (platform === 'openbsd')
- return 'OpenBSD';
- if (platform === 'linux')
- return 'Linux';
- if (platform)
- return `Other:${platform}`;
- return 'Unknown';
- };
- let _platformHeaders;
- export const getPlatformHeaders = () => {
- return (_platformHeaders ?? (_platformHeaders = getPlatformProperties()));
- };
- //# sourceMappingURL=detect-platform.mjs.map
|