detect-platform.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. "use strict";
  2. // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
  3. Object.defineProperty(exports, "__esModule", { value: true });
  4. exports.getPlatformHeaders = exports.isRunningInBrowser = void 0;
  5. const version_1 = require("../version.js");
  6. const isRunningInBrowser = () => {
  7. return (
  8. // @ts-ignore
  9. typeof window !== 'undefined' &&
  10. // @ts-ignore
  11. typeof window.document !== 'undefined' &&
  12. // @ts-ignore
  13. typeof navigator !== 'undefined');
  14. };
  15. exports.isRunningInBrowser = isRunningInBrowser;
  16. /**
  17. * Note this does not detect 'browser'; for that, use getBrowserInfo().
  18. */
  19. function getDetectedPlatform() {
  20. if (typeof Deno !== 'undefined' && Deno.build != null) {
  21. return 'deno';
  22. }
  23. if (typeof EdgeRuntime !== 'undefined') {
  24. return 'edge';
  25. }
  26. if (Object.prototype.toString.call(typeof globalThis.process !== 'undefined' ? globalThis.process : 0) === '[object process]') {
  27. return 'node';
  28. }
  29. return 'unknown';
  30. }
  31. const getPlatformProperties = () => {
  32. const detectedPlatform = getDetectedPlatform();
  33. if (detectedPlatform === 'deno') {
  34. return {
  35. 'X-Stainless-Lang': 'js',
  36. 'X-Stainless-Package-Version': version_1.VERSION,
  37. 'X-Stainless-OS': normalizePlatform(Deno.build.os),
  38. 'X-Stainless-Arch': normalizeArch(Deno.build.arch),
  39. 'X-Stainless-Runtime': 'deno',
  40. 'X-Stainless-Runtime-Version': typeof Deno.version === 'string' ? Deno.version : Deno.version?.deno ?? 'unknown',
  41. };
  42. }
  43. if (typeof EdgeRuntime !== 'undefined') {
  44. return {
  45. 'X-Stainless-Lang': 'js',
  46. 'X-Stainless-Package-Version': version_1.VERSION,
  47. 'X-Stainless-OS': 'Unknown',
  48. 'X-Stainless-Arch': `other:${EdgeRuntime}`,
  49. 'X-Stainless-Runtime': 'edge',
  50. 'X-Stainless-Runtime-Version': globalThis.process.version,
  51. };
  52. }
  53. // Check if Node.js
  54. if (detectedPlatform === 'node') {
  55. return {
  56. 'X-Stainless-Lang': 'js',
  57. 'X-Stainless-Package-Version': version_1.VERSION,
  58. 'X-Stainless-OS': normalizePlatform(globalThis.process.platform ?? 'unknown'),
  59. 'X-Stainless-Arch': normalizeArch(globalThis.process.arch ?? 'unknown'),
  60. 'X-Stainless-Runtime': 'node',
  61. 'X-Stainless-Runtime-Version': globalThis.process.version ?? 'unknown',
  62. };
  63. }
  64. const browserInfo = getBrowserInfo();
  65. if (browserInfo) {
  66. return {
  67. 'X-Stainless-Lang': 'js',
  68. 'X-Stainless-Package-Version': version_1.VERSION,
  69. 'X-Stainless-OS': 'Unknown',
  70. 'X-Stainless-Arch': 'unknown',
  71. 'X-Stainless-Runtime': `browser:${browserInfo.browser}`,
  72. 'X-Stainless-Runtime-Version': browserInfo.version,
  73. };
  74. }
  75. // TODO add support for Cloudflare workers, etc.
  76. return {
  77. 'X-Stainless-Lang': 'js',
  78. 'X-Stainless-Package-Version': version_1.VERSION,
  79. 'X-Stainless-OS': 'Unknown',
  80. 'X-Stainless-Arch': 'unknown',
  81. 'X-Stainless-Runtime': 'unknown',
  82. 'X-Stainless-Runtime-Version': 'unknown',
  83. };
  84. };
  85. // Note: modified from https://github.com/JS-DevTools/host-environment/blob/b1ab79ecde37db5d6e163c050e54fe7d287d7c92/src/isomorphic.browser.ts
  86. function getBrowserInfo() {
  87. if (typeof navigator === 'undefined' || !navigator) {
  88. return null;
  89. }
  90. // NOTE: The order matters here!
  91. const browserPatterns = [
  92. { key: 'edge', pattern: /Edge(?:\W+(\d+)\.(\d+)(?:\.(\d+))?)?/ },
  93. { key: 'ie', pattern: /MSIE(?:\W+(\d+)\.(\d+)(?:\.(\d+))?)?/ },
  94. { key: 'ie', pattern: /Trident(?:.*rv\:(\d+)\.(\d+)(?:\.(\d+))?)?/ },
  95. { key: 'chrome', pattern: /Chrome(?:\W+(\d+)\.(\d+)(?:\.(\d+))?)?/ },
  96. { key: 'firefox', pattern: /Firefox(?:\W+(\d+)\.(\d+)(?:\.(\d+))?)?/ },
  97. { key: 'safari', pattern: /(?:Version\W+(\d+)\.(\d+)(?:\.(\d+))?)?(?:\W+Mobile\S*)?\W+Safari/ },
  98. ];
  99. // Find the FIRST matching browser
  100. for (const { key, pattern } of browserPatterns) {
  101. const match = pattern.exec(navigator.userAgent);
  102. if (match) {
  103. const major = match[1] || 0;
  104. const minor = match[2] || 0;
  105. const patch = match[3] || 0;
  106. return { browser: key, version: `${major}.${minor}.${patch}` };
  107. }
  108. }
  109. return null;
  110. }
  111. const normalizeArch = (arch) => {
  112. // Node docs:
  113. // - https://nodejs.org/api/process.html#processarch
  114. // Deno docs:
  115. // - https://doc.deno.land/deno/stable/~/Deno.build
  116. if (arch === 'x32')
  117. return 'x32';
  118. if (arch === 'x86_64' || arch === 'x64')
  119. return 'x64';
  120. if (arch === 'arm')
  121. return 'arm';
  122. if (arch === 'aarch64' || arch === 'arm64')
  123. return 'arm64';
  124. if (arch)
  125. return `other:${arch}`;
  126. return 'unknown';
  127. };
  128. const normalizePlatform = (platform) => {
  129. // Node platforms:
  130. // - https://nodejs.org/api/process.html#processplatform
  131. // Deno platforms:
  132. // - https://doc.deno.land/deno/stable/~/Deno.build
  133. // - https://github.com/denoland/deno/issues/14799
  134. platform = platform.toLowerCase();
  135. // NOTE: this iOS check is untested and may not work
  136. // Node does not work natively on IOS, there is a fork at
  137. // https://github.com/nodejs-mobile/nodejs-mobile
  138. // however it is unknown at the time of writing how to detect if it is running
  139. if (platform.includes('ios'))
  140. return 'iOS';
  141. if (platform === 'android')
  142. return 'Android';
  143. if (platform === 'darwin')
  144. return 'MacOS';
  145. if (platform === 'win32')
  146. return 'Windows';
  147. if (platform === 'freebsd')
  148. return 'FreeBSD';
  149. if (platform === 'openbsd')
  150. return 'OpenBSD';
  151. if (platform === 'linux')
  152. return 'Linux';
  153. if (platform)
  154. return `Other:${platform}`;
  155. return 'Unknown';
  156. };
  157. let _platformHeaders;
  158. const getPlatformHeaders = () => {
  159. return (_platformHeaders ?? (_platformHeaders = getPlatformProperties()));
  160. };
  161. exports.getPlatformHeaders = getPlatformHeaders;
  162. //# sourceMappingURL=detect-platform.js.map