@hylid/env 0.0.90011267721-dev.2 → 0.0.90011267721-dev.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -29,6 +29,7 @@ export declare const CLIENT: {
29
29
  WECHAT: string;
30
30
  TNGD_H5NG: string;
31
31
  WORLDFIRST: string;
32
+ SSR_NODE: string;
32
33
  };
33
34
  export declare const HAVE_MINIPROGRAM: RegExp;
34
35
  export declare const NO_MINIPROGRAM: RegExp;
package/lib/constants.js CHANGED
@@ -29,7 +29,8 @@ export var CLIENT = {
29
29
  H5: 'h5',
30
30
  WECHAT: 'wechat',
31
31
  TNGD_H5NG: 'tngdh5ng',
32
- WORLDFIRST: 'worldfirst'
32
+ WORLDFIRST: 'worldfirst',
33
+ SSR_NODE: 'SSR_NODE'
33
34
  };
34
35
  export var HAVE_MINIPROGRAM = /miniprogram/i;
35
36
  export var NO_MINIPROGRAM = /^((?!miniprogram).)*$/i;
@@ -1,5 +1,13 @@
1
1
  import { mpChecker } from "../checker";
2
- import { getMatchName } from "../utils";
2
+ import { getMatchName, browserAPI } from "../utils";
3
3
  export function getName4MP() {
4
- return getMatchName(mpChecker, my.getSystemInfoSync().app);
4
+ // SSR 兼容:检测是否在小程序环境
5
+ if (!browserAPI.isMiniProgram()) {
6
+ return undefined;
7
+ }
8
+ try {
9
+ return getMatchName(mpChecker, my.getSystemInfoSync().app);
10
+ } catch (e) {
11
+ return undefined;
12
+ }
5
13
  }
@@ -1,6 +1,7 @@
1
1
  import { mpWebChecker } from "../checker";
2
- import { getMatchName } from "../utils";
2
+ import { getMatchName, browserAPI } from "../utils";
3
3
  export function getName4MpWeb() {
4
- var _a;
5
- return getMatchName(mpWebChecker, ((_a = window === null || window === void 0 ? void 0 : window.navigator) === null || _a === void 0 ? void 0 : _a.userAgent) || '');
4
+ var navigator = browserAPI.getNavigator();
5
+ if (!navigator) return '';
6
+ return getMatchName(mpWebChecker, navigator.userAgent);
6
7
  }
@@ -1,7 +1,8 @@
1
1
  import { webChecker } from "../checker";
2
2
  import { CLIENT } from "../constants";
3
- import { getMatchName } from "../utils";
3
+ import { getMatchName, browserAPI } from "../utils";
4
4
  export function getName4Web() {
5
- var _a;
6
- return getMatchName(webChecker, ((_a = window === null || window === void 0 ? void 0 : window.navigator) === null || _a === void 0 ? void 0 : _a.userAgent) || '') || CLIENT.H5;
5
+ var navigator = browserAPI.getNavigator();
6
+ if (!navigator) return CLIENT.H5;
7
+ return getMatchName(webChecker, navigator.userAgent) || CLIENT.H5;
7
8
  }
package/lib/index.d.ts CHANGED
@@ -1,10 +1,10 @@
1
1
  import { ClientEnv, PlatformEnv } from './types';
2
2
  import version from './version';
3
3
  export declare function getDefaultDiscernEnv(): {
4
- client: string | undefined;
5
- platform: string | undefined;
4
+ client: string;
5
+ platform: string;
6
6
  };
7
- declare let client: string | undefined, platform: string | undefined;
7
+ declare let client: string, platform: string;
8
8
  interface CustomEnv {
9
9
  client?: string;
10
10
  platform?: string;
@@ -13,5 +13,6 @@ export declare function customEnv(config: CustomEnv | ((env: CustomEnv) => Custo
13
13
  declare const clientEnv: ClientEnv;
14
14
  declare const platformEnv: PlatformEnv;
15
15
  export { CLIENT, PLATFORM } from './constants';
16
+ export { browserAPI } from './utils';
16
17
  export { client, platform, clientEnv, platformEnv, version };
17
18
  export type { ClientEnv, PlatformEnv, CustomEnv };
package/lib/index.js CHANGED
@@ -1,10 +1,17 @@
1
- import { createCommonEnv, getSettingName, isMP, notice } from "./utils";
1
+ import { createCommonEnv, getSettingName, isMP, notice, browserAPI } from "./utils";
2
2
  import { getName4MP, getName4MpWeb, getName4Web } from "./getClientName";
3
3
  import { CLIENT, PLATFORM } from "./constants";
4
4
  import version from "./version";
5
5
  export function getDefaultDiscernEnv() {
6
6
  var client;
7
7
  var platform;
8
+ // SSR 兼容:在服务端返回默认值
9
+ if (!browserAPI.isBrowser() && !browserAPI.isMiniProgram()) {
10
+ return {
11
+ client: CLIENT.SSR_NODE,
12
+ platform: PLATFORM.WEB
13
+ };
14
+ }
8
15
  if (isMP()) {
9
16
  client = getName4MP();
10
17
  platform = PLATFORM.MP;
@@ -27,10 +34,16 @@ export function getDefaultDiscernEnv() {
27
34
  }
28
35
  if (!platform || !client) {
29
36
  notice('Cannot identify your client.');
37
+ // SSR 兼容:提供默认值
38
+ return {
39
+ client: CLIENT.H5,
40
+ platform: PLATFORM.WEB
41
+ };
30
42
  }
43
+ // 增加兜底值
31
44
  return {
32
- client: client,
33
- platform: platform
45
+ client: client || CLIENT.H5,
46
+ platform: platform || PLATFORM.WEB
34
47
  };
35
48
  }
36
49
  var _a = getDefaultDiscernEnv(),
@@ -41,12 +54,12 @@ export function customEnv(config) {
41
54
  client: client,
42
55
  platform: platform
43
56
  }) : config;
44
- if (custom.client) {
57
+ if (custom === null || custom === void 0 ? void 0 : custom.client) {
45
58
  client = custom.client;
46
59
  // @ts-ignore
47
60
  clientEnv["is".concat(client.toUpperCase())] = true;
48
61
  }
49
- if (custom.platform) {
62
+ if (custom === null || custom === void 0 ? void 0 : custom.platform) {
50
63
  platform = custom.platform;
51
64
  // @ts-ignore
52
65
  platformEnv["is".concat(platform.toUpperCase())] = true;
@@ -55,4 +68,5 @@ export function customEnv(config) {
55
68
  var clientEnv = createCommonEnv(CLIENT, client);
56
69
  var platformEnv = createCommonEnv(PLATFORM, platform);
57
70
  export { CLIENT, PLATFORM } from "./constants";
71
+ export { browserAPI } from "./utils";
58
72
  export { client, platform, clientEnv, platformEnv, version };
package/lib/utils.d.ts CHANGED
@@ -1,5 +1,17 @@
1
1
  import { Checker } from './types';
2
- export declare const isClient: boolean;
2
+ export declare const browserAPI: {
3
+ isBrowser: () => boolean;
4
+ isMiniProgram: () => boolean;
5
+ getWindow: () => (Window & typeof globalThis) | null;
6
+ getNavigator: () => Navigator | null;
7
+ localStorage: {
8
+ getItem: (key: string) => string | null;
9
+ setItem: (key: string, value: string) => boolean;
10
+ };
11
+ history: {
12
+ replaceState: (state: any, title: string, url: string) => boolean;
13
+ };
14
+ };
3
15
  export declare function isMP(): boolean;
4
16
  export declare function getMatchName(checks: Checker[], pattern: string): string | undefined;
5
17
  export declare function createCommonEnv<T>(source: Record<string, string>, current?: string): T;
package/lib/utils.js CHANGED
@@ -1,4 +1,3 @@
1
- import _typeof from "@babel/runtime/helpers/typeof";
2
1
  var __assign = this && this.__assign || function () {
3
2
  __assign = Object.assign || function (t) {
4
3
  for (var s, i = 1, n = arguments.length; i < n; i++) {
@@ -9,12 +8,69 @@ var __assign = this && this.__assign || function () {
9
8
  };
10
9
  return __assign.apply(this, arguments);
11
10
  };
12
- export var isClient = (typeof window === "undefined" ? "undefined" : _typeof(window)) === 'object';
11
+ // SSR 兼容性工具函数
12
+ export var browserAPI = {
13
+ // 检测是否在浏览器环境
14
+ isBrowser: function isBrowser() {
15
+ return typeof window !== 'undefined' && typeof document !== 'undefined';
16
+ },
17
+ // 检测是否在小程序环境
18
+ isMiniProgram: function isMiniProgram() {
19
+ return typeof my !== 'undefined';
20
+ },
21
+ // 安全的 window 访问
22
+ getWindow: function getWindow() {
23
+ return typeof window !== 'undefined' ? window : null;
24
+ },
25
+ // 安全的 navigator 访问
26
+ getNavigator: function getNavigator() {
27
+ if (typeof window === 'undefined' || typeof window.navigator === 'undefined') {
28
+ return null;
29
+ }
30
+ return window.navigator;
31
+ },
32
+ // 安全的 localStorage 访问
33
+ localStorage: {
34
+ getItem: function getItem(key) {
35
+ if (typeof window === 'undefined') return null;
36
+ try {
37
+ return localStorage.getItem(key);
38
+ } catch (e) {
39
+ return null;
40
+ }
41
+ },
42
+ setItem: function setItem(key, value) {
43
+ if (typeof window === 'undefined') return false;
44
+ try {
45
+ localStorage.setItem(key, value);
46
+ return true;
47
+ } catch (e) {
48
+ return false;
49
+ }
50
+ }
51
+ },
52
+ // 安全的 history 访问
53
+ history: {
54
+ replaceState: function replaceState(state, title, url) {
55
+ if (typeof window === 'undefined' || !window.history) return false;
56
+ try {
57
+ window.history.replaceState(state, title, url);
58
+ return true;
59
+ } catch (e) {
60
+ return false;
61
+ }
62
+ }
63
+ }
64
+ };
13
65
  export function isMP() {
14
- // 小程序一定有 my
66
+ // SSR 兼容:检测是否在小程序环境
15
67
  if (typeof my === 'undefined') return false;
16
68
  // SDKVersion 只在小程序有
17
- if (typeof my.SDKVersion === 'string') return true;
69
+ try {
70
+ if (typeof my.SDKVersion === 'string') return true;
71
+ } catch (e) {
72
+ return false;
73
+ }
18
74
  return false;
19
75
  }
20
76
  export function getMatchName(checks, pattern) {
@@ -45,30 +101,52 @@ export function notice(text) {
45
101
  export function getSettingName() {
46
102
  var _a, _b;
47
103
  var ENV_KEY = '__hy_env__';
48
- var clientMatch = isClient ? ((_a = window.location.search) === null || _a === void 0 ? void 0 : _a.match(/__app__=([^&]*)/)) || [] : [];
104
+ // SSR 兼容:检测是否在浏览器环境
105
+ if (!browserAPI.isBrowser()) {
106
+ return {};
107
+ }
108
+ var window = browserAPI.getWindow();
109
+ if (!window) return {};
110
+ var clientMatch = ((_a = window.location.search) === null || _a === void 0 ? void 0 : _a.match(/__app__=([^&]*)/)) || [],
111
+ _c = void 0;
49
112
  var client = clientMatch[1];
50
- var platformMatch = isClient ? ((_b = window.location.search) === null || _b === void 0 ? void 0 : _b.match(/__platform__=([^&]*)/)) || [] : [];
113
+ var platformMatch = ((_b = window.location.search) === null || _b === void 0 ? void 0 : _b.match(/__platform__=([^&]*)/)) || [],
114
+ _d = void 0;
51
115
  var platform = platformMatch[1];
52
- var env = isClient ? JSON.parse(localStorage.getItem(ENV_KEY) || '{}') || {} : {};
116
+ // SSR 兼容:安全的 localStorage 访问
117
+ var env = {};
118
+ try {
119
+ env = JSON.parse(browserAPI.localStorage.getItem(ENV_KEY) || '{}') || {};
120
+ } catch (e) {
121
+ env = {};
122
+ }
53
123
  if (client) {
54
124
  env.client = client;
55
125
  }
56
126
  if (platform) {
57
127
  env.platform = platform;
58
128
  }
129
+ // SSR 兼容:安全的 localStorage 设置
130
+ browserAPI.localStorage.setItem(ENV_KEY, JSON.stringify(env));
131
+ // SSR 兼容:安全的 history API 访问
132
+ if (clientMatch[1]) {
133
+ try {
134
+ var search = window.location.search.replace(new RegExp("".concat(clientMatch[0], "(&)*")), '');
135
+ var hash = window.location.hash || '';
136
+ var newUrl = "".concat(window.location.pathname).concat(search).concat(hash);
137
+ browserAPI.history.replaceState(null, '', newUrl);
138
+ } catch (e) {
139
+ // 不处理
140
+ }
141
+ }
142
+ // SSR 兼容:安全的缓存读取
143
+ var cached = {};
59
144
  try {
60
- localStorage.setItem(ENV_KEY, JSON.stringify(env));
145
+ cached = JSON.parse(browserAPI.localStorage.getItem(ENV_KEY) || '{}');
61
146
  } catch (e) {
62
- // 不处理
63
- }
64
- if (clientMatch[1]) {
65
- var search = window.location.search.replace(new RegExp("".concat(clientMatch[0], "(&)*")), '');
66
- var hash = window.location.hash || '';
67
- var newUrl = "".concat(window.location.pathname).concat(search).concat(hash);
68
- history.replaceState(null, '', newUrl);
147
+ cached = {};
69
148
  }
70
- var cached = isClient ? JSON.parse(localStorage.getItem(ENV_KEY) || '{}') : {};
71
- var crossBridgeStorage = isClient ? localStorage.getItem('_CB__app__') : undefined;
149
+ var crossBridgeStorage = browserAPI.localStorage.getItem('_CB__app__');
72
150
  return crossBridgeStorage ? __assign(__assign({}, cached), {
73
151
  client: crossBridgeStorage
74
152
  }) : cached;
package/lib/version.js CHANGED
@@ -1 +1 @@
1
- export default '0.0.90011267721-dev.2';
1
+ export default '0.0.90011267721-dev.4';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hylid/env",
3
- "version": "0.0.90011267721-dev.2",
3
+ "version": "0.0.90011267721-dev.4",
4
4
  "main": "lib/index.js",
5
5
  "files": [
6
6
  "lib"