@hylid/env 0.0.90011267721-dev.4 → 0.0.90011283857-dev.2
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.
- package/lib/constants.d.ts +0 -1
- package/lib/constants.js +1 -2
- package/lib/getClientName/getName4Mp.js +2 -10
- package/lib/getClientName/getName4MpWeb.js +2 -4
- package/lib/getClientName/getName4Web.js +2 -4
- package/lib/index.d.ts +3 -4
- package/lib/index.js +5 -19
- package/lib/utils.d.ts +0 -13
- package/lib/utils.js +16 -96
- package/lib/version.js +1 -1
- package/package.json +1 -1
package/lib/constants.d.ts
CHANGED
package/lib/constants.js
CHANGED
|
@@ -29,8 +29,7 @@ export var CLIENT = {
|
|
|
29
29
|
H5: 'h5',
|
|
30
30
|
WECHAT: 'wechat',
|
|
31
31
|
TNGD_H5NG: 'tngdh5ng',
|
|
32
|
-
WORLDFIRST: 'worldfirst'
|
|
33
|
-
SSR_NODE: 'SSR_NODE'
|
|
32
|
+
WORLDFIRST: 'worldfirst'
|
|
34
33
|
};
|
|
35
34
|
export var HAVE_MINIPROGRAM = /miniprogram/i;
|
|
36
35
|
export var NO_MINIPROGRAM = /^((?!miniprogram).)*$/i;
|
|
@@ -1,13 +1,5 @@
|
|
|
1
1
|
import { mpChecker } from "../checker";
|
|
2
|
-
import { getMatchName
|
|
2
|
+
import { getMatchName } from "../utils";
|
|
3
3
|
export function getName4MP() {
|
|
4
|
-
|
|
5
|
-
if (!browserAPI.isMiniProgram()) {
|
|
6
|
-
return undefined;
|
|
7
|
-
}
|
|
8
|
-
try {
|
|
9
|
-
return getMatchName(mpChecker, my.getSystemInfoSync().app);
|
|
10
|
-
} catch (e) {
|
|
11
|
-
return undefined;
|
|
12
|
-
}
|
|
4
|
+
return getMatchName(mpChecker, my.getSystemInfoSync().app);
|
|
13
5
|
}
|
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
import { mpWebChecker } from "../checker";
|
|
2
|
-
import { getMatchName
|
|
2
|
+
import { getMatchName } from "../utils";
|
|
3
3
|
export function getName4MpWeb() {
|
|
4
|
-
|
|
5
|
-
if (!navigator) return '';
|
|
6
|
-
return getMatchName(mpWebChecker, navigator.userAgent);
|
|
4
|
+
return getMatchName(mpWebChecker, window.navigator.userAgent);
|
|
7
5
|
}
|
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
import { webChecker } from "../checker";
|
|
2
2
|
import { CLIENT } from "../constants";
|
|
3
|
-
import { getMatchName
|
|
3
|
+
import { getMatchName } from "../utils";
|
|
4
4
|
export function getName4Web() {
|
|
5
|
-
|
|
6
|
-
if (!navigator) return CLIENT.H5;
|
|
7
|
-
return getMatchName(webChecker, navigator.userAgent) || CLIENT.H5;
|
|
5
|
+
return getMatchName(webChecker, window.navigator.userAgent) || CLIENT.H5;
|
|
8
6
|
}
|
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;
|
|
5
|
-
platform: string;
|
|
4
|
+
client: string | undefined;
|
|
5
|
+
platform: string | undefined;
|
|
6
6
|
};
|
|
7
|
-
declare let client: string, platform: string;
|
|
7
|
+
declare let client: string | undefined, platform: string | undefined;
|
|
8
8
|
interface CustomEnv {
|
|
9
9
|
client?: string;
|
|
10
10
|
platform?: string;
|
|
@@ -13,6 +13,5 @@ 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';
|
|
17
16
|
export { client, platform, clientEnv, platformEnv, version };
|
|
18
17
|
export type { ClientEnv, PlatformEnv, CustomEnv };
|
package/lib/index.js
CHANGED
|
@@ -1,17 +1,10 @@
|
|
|
1
|
-
import { createCommonEnv, getSettingName, isMP, notice
|
|
1
|
+
import { createCommonEnv, getSettingName, isMP, notice } 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
|
-
}
|
|
15
8
|
if (isMP()) {
|
|
16
9
|
client = getName4MP();
|
|
17
10
|
platform = PLATFORM.MP;
|
|
@@ -34,16 +27,10 @@ export function getDefaultDiscernEnv() {
|
|
|
34
27
|
}
|
|
35
28
|
if (!platform || !client) {
|
|
36
29
|
notice('Cannot identify your client.');
|
|
37
|
-
// SSR 兼容:提供默认值
|
|
38
|
-
return {
|
|
39
|
-
client: CLIENT.H5,
|
|
40
|
-
platform: PLATFORM.WEB
|
|
41
|
-
};
|
|
42
30
|
}
|
|
43
|
-
// 增加兜底值
|
|
44
31
|
return {
|
|
45
|
-
client: client
|
|
46
|
-
platform: platform
|
|
32
|
+
client: client,
|
|
33
|
+
platform: platform
|
|
47
34
|
};
|
|
48
35
|
}
|
|
49
36
|
var _a = getDefaultDiscernEnv(),
|
|
@@ -54,12 +41,12 @@ export function customEnv(config) {
|
|
|
54
41
|
client: client,
|
|
55
42
|
platform: platform
|
|
56
43
|
}) : config;
|
|
57
|
-
if (custom
|
|
44
|
+
if (custom.client) {
|
|
58
45
|
client = custom.client;
|
|
59
46
|
// @ts-ignore
|
|
60
47
|
clientEnv["is".concat(client.toUpperCase())] = true;
|
|
61
48
|
}
|
|
62
|
-
if (custom
|
|
49
|
+
if (custom.platform) {
|
|
63
50
|
platform = custom.platform;
|
|
64
51
|
// @ts-ignore
|
|
65
52
|
platformEnv["is".concat(platform.toUpperCase())] = true;
|
|
@@ -68,5 +55,4 @@ export function customEnv(config) {
|
|
|
68
55
|
var clientEnv = createCommonEnv(CLIENT, client);
|
|
69
56
|
var platformEnv = createCommonEnv(PLATFORM, platform);
|
|
70
57
|
export { CLIENT, PLATFORM } from "./constants";
|
|
71
|
-
export { browserAPI } from "./utils";
|
|
72
58
|
export { client, platform, clientEnv, platformEnv, version };
|
package/lib/utils.d.ts
CHANGED
|
@@ -1,17 +1,4 @@
|
|
|
1
1
|
import { Checker } from './types';
|
|
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
|
-
};
|
|
15
2
|
export declare function isMP(): boolean;
|
|
16
3
|
export declare function getMatchName(checks: Checker[], pattern: string): string | undefined;
|
|
17
4
|
export declare function createCommonEnv<T>(source: Record<string, string>, current?: string): T;
|
package/lib/utils.js
CHANGED
|
@@ -8,69 +8,11 @@ var __assign = this && this.__assign || function () {
|
|
|
8
8
|
};
|
|
9
9
|
return __assign.apply(this, arguments);
|
|
10
10
|
};
|
|
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
|
-
};
|
|
65
11
|
export function isMP() {
|
|
66
|
-
//
|
|
12
|
+
// 小程序一定有 my
|
|
67
13
|
if (typeof my === 'undefined') return false;
|
|
68
14
|
// SDKVersion 只在小程序有
|
|
69
|
-
|
|
70
|
-
if (typeof my.SDKVersion === 'string') return true;
|
|
71
|
-
} catch (e) {
|
|
72
|
-
return false;
|
|
73
|
-
}
|
|
15
|
+
if (typeof my.SDKVersion === 'string') return true;
|
|
74
16
|
return false;
|
|
75
17
|
}
|
|
76
18
|
export function getMatchName(checks, pattern) {
|
|
@@ -99,54 +41,32 @@ export function notice(text) {
|
|
|
99
41
|
console.info("[hylid-env]: ".concat(text));
|
|
100
42
|
}
|
|
101
43
|
export function getSettingName() {
|
|
102
|
-
var _a
|
|
44
|
+
var _a;
|
|
103
45
|
var ENV_KEY = '__hy_env__';
|
|
104
|
-
|
|
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;
|
|
46
|
+
var clientMatch = ((_a = window.location.search) === null || _a === void 0 ? void 0 : _a.match(/__app__=([^&]*)/)) || [];
|
|
112
47
|
var client = clientMatch[1];
|
|
113
|
-
var platformMatch =
|
|
114
|
-
_d = void 0;
|
|
48
|
+
var platformMatch = window.location.search.match(/__platform__=([^&]*)/) || [];
|
|
115
49
|
var platform = platformMatch[1];
|
|
116
|
-
|
|
117
|
-
var env = {};
|
|
118
|
-
try {
|
|
119
|
-
env = JSON.parse(browserAPI.localStorage.getItem(ENV_KEY) || '{}') || {};
|
|
120
|
-
} catch (e) {
|
|
121
|
-
env = {};
|
|
122
|
-
}
|
|
50
|
+
var env = JSON.parse(localStorage.getItem(ENV_KEY) || '{}') || {};
|
|
123
51
|
if (client) {
|
|
124
52
|
env.client = client;
|
|
125
53
|
}
|
|
126
54
|
if (platform) {
|
|
127
55
|
env.platform = platform;
|
|
128
56
|
}
|
|
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 = {};
|
|
144
57
|
try {
|
|
145
|
-
|
|
58
|
+
localStorage.setItem(ENV_KEY, JSON.stringify(env));
|
|
146
59
|
} catch (e) {
|
|
147
|
-
|
|
60
|
+
// 不处理
|
|
61
|
+
}
|
|
62
|
+
if (clientMatch[1]) {
|
|
63
|
+
var search = window.location.search.replace(new RegExp("".concat(clientMatch[0], "(&)*")), '');
|
|
64
|
+
var hash = window.location.hash || '';
|
|
65
|
+
var newUrl = "".concat(window.location.pathname).concat(search).concat(hash);
|
|
66
|
+
history.replaceState(null, '', newUrl);
|
|
148
67
|
}
|
|
149
|
-
var
|
|
68
|
+
var cached = JSON.parse(localStorage.getItem(ENV_KEY) || '{}');
|
|
69
|
+
var crossBridgeStorage = localStorage.getItem('_CB__app__');
|
|
150
70
|
return crossBridgeStorage ? __assign(__assign({}, cached), {
|
|
151
71
|
client: crossBridgeStorage
|
|
152
72
|
}) : cached;
|
package/lib/version.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export default '0.0.
|
|
1
|
+
export default '0.0.90011283857-dev.2';
|