@microbt/environment 1.2.12 → 1.2.14

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.
@@ -0,0 +1,5 @@
1
+ export declare const SUPPORTED_DOMAINS: string[];
2
+ export declare function getBusinessHost(): string;
3
+ export declare function getBusinessHostname(): string;
4
+ export declare function getBusinessOrigin(): string;
5
+ export declare function isOriginalHostInjected(): boolean;
@@ -0,0 +1,90 @@
1
+ // src/business-host.ts
2
+ var SUPPORTED_DOMAINS = [
3
+ "superacme.com",
4
+ "eu-cinmoore.com",
5
+ "cinmoore.com",
6
+ "cinmoore.cn",
7
+ "safio365.com",
8
+ "sensforge.com",
9
+ "fronai.cn",
10
+ "superacmeglobal.com",
11
+ "cinvue.com",
12
+ "0.0.0.0",
13
+ "127.0.0.1",
14
+ "localhost"
15
+ ];
16
+ var ORIGINAL_HOST_REGEX = /(?:^|[\s;(])originalHost\s*:\s*([^;\s)]+)/i;
17
+ var HOST_PATTERN = /^(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?(?::\d{1,5})?$/i;
18
+ function getLocationHost() {
19
+ if (typeof location === "undefined") {
20
+ throw new Error("location is not defined");
21
+ }
22
+ return location.host;
23
+ }
24
+ function getUserAgent() {
25
+ if (typeof navigator === "undefined") {
26
+ return "";
27
+ }
28
+ return navigator.userAgent || "";
29
+ }
30
+ function getHostnameFromHost(host) {
31
+ return host.replace(/:\d+$/, "");
32
+ }
33
+ function isSupportedDomain(hostname) {
34
+ const normalizedHostname = hostname.toLowerCase();
35
+ return SUPPORTED_DOMAINS.some(
36
+ (domain) => normalizedHostname === domain || normalizedHostname.endsWith(`.${domain}`)
37
+ );
38
+ }
39
+ function normalizeOriginalHost(rawHost) {
40
+ const host = rawHost == null ? void 0 : rawHost.trim().toLowerCase();
41
+ if (!host || !HOST_PATTERN.test(host)) {
42
+ return void 0;
43
+ }
44
+ const portMatch = host.match(/:(\d+)$/);
45
+ if (portMatch) {
46
+ const port = Number(portMatch[1]);
47
+ if (port < 1 || port > 65535) {
48
+ return void 0;
49
+ }
50
+ }
51
+ const hostname = getHostnameFromHost(host);
52
+ if (!isSupportedDomain(hostname)) {
53
+ return void 0;
54
+ }
55
+ return host;
56
+ }
57
+ function resolveOriginalHost() {
58
+ const match = getUserAgent().match(ORIGINAL_HOST_REGEX);
59
+ return normalizeOriginalHost(match == null ? void 0 : match[1]);
60
+ }
61
+ function getBusinessHost() {
62
+ var _a;
63
+ return (_a = resolveOriginalHost()) != null ? _a : getLocationHost();
64
+ }
65
+ function getBusinessHostname() {
66
+ return getHostnameFromHost(getBusinessHost());
67
+ }
68
+ function getBusinessOrigin() {
69
+ const originalHost = resolveOriginalHost();
70
+ if (originalHost) {
71
+ return `https://${originalHost}`;
72
+ }
73
+ if (typeof location === "undefined") {
74
+ throw new Error("location is not defined");
75
+ }
76
+ if (location.origin) {
77
+ return location.origin;
78
+ }
79
+ return `${location.protocol}//${location.host}`;
80
+ }
81
+ function isOriginalHostInjected() {
82
+ return Boolean(resolveOriginalHost());
83
+ }
84
+ export {
85
+ SUPPORTED_DOMAINS,
86
+ getBusinessHost,
87
+ getBusinessHostname,
88
+ getBusinessOrigin,
89
+ isOriginalHostInjected
90
+ };
@@ -1,4 +1,5 @@
1
1
  // src/cookie.ts
2
+ import { getBusinessHostname } from "./business-host";
2
3
  var COOKIE_REGEX = (name) => new RegExp("(^| )" + name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&") + "=([^;]*)");
3
4
  function getCookie(name) {
4
5
  try {
@@ -12,7 +13,7 @@ function getCookie(name) {
12
13
  }
13
14
  }
14
15
  function getRUID() {
15
- return getCookie(`RUID_${location.hostname}`);
16
+ return getCookie(`RUID_${getBusinessHostname()}`);
16
17
  }
17
18
  export {
18
19
  getCookie,
@@ -1,4 +1,5 @@
1
- export declare const SUPPORTED_DOMAINS: string[];
1
+ import { SUPPORTED_DOMAINS } from '../business-host';
2
+ export { SUPPORTED_DOMAINS };
2
3
  export declare function environment(): string;
3
4
  export declare function region(): string;
4
5
  export declare function isAWSDomain(): boolean;
@@ -1,25 +1,10 @@
1
1
  // src/environment/index.ts
2
+ import { getBusinessHost, SUPPORTED_DOMAINS } from "../business-host";
2
3
  import { EnvironmentStrategyFactory } from "./factory";
3
- var SUPPORTED_DOMAINS = [
4
- "superacme.com",
5
- "eu-cinmoore.com",
6
- "cinmoore.com",
7
- "cinmoore.cn",
8
- "safio365.com",
9
- "sensforge.com",
10
- "fronai.cn",
11
- "superacmeglobal.com",
12
- "cinvue.com",
13
- "0.0.0.0",
14
- "127.0.0.1",
15
- "localhost"
16
- ];
17
4
  function getCurrentDomain() {
18
- if (typeof location === "undefined") {
19
- throw new Error("location is not defined");
20
- }
21
- const host = location.host;
22
- const domain = SUPPORTED_DOMAINS.find((d) => host.endsWith(d));
5
+ const host = getBusinessHost();
6
+ const hostname = host.replace(/:\d+$/, "").toLowerCase();
7
+ const domain = SUPPORTED_DOMAINS.find((d) => hostname === d || hostname.endsWith(`.${d}`));
23
8
  if (!domain) {
24
9
  throw new Error(`Unsupported domain: ${host}`);
25
10
  }
@@ -27,9 +12,10 @@ function getCurrentDomain() {
27
12
  }
28
13
  function environment() {
29
14
  try {
15
+ const host = getBusinessHost();
30
16
  const domain = getCurrentDomain();
31
17
  const strategy = EnvironmentStrategyFactory.getStrategy(domain);
32
- return strategy.getEnvironment(location.host);
18
+ return strategy.getEnvironment(host);
33
19
  } catch (error) {
34
20
  console.warn("Failed to determine environment:", error);
35
21
  return "unknown";
@@ -37,9 +23,10 @@ function environment() {
37
23
  }
38
24
  function region() {
39
25
  try {
26
+ const host = getBusinessHost();
40
27
  const domain = getCurrentDomain();
41
28
  const strategy = EnvironmentStrategyFactory.getStrategy(domain);
42
- return strategy.getRegion(location.host);
29
+ return strategy.getRegion(host);
43
30
  } catch (error) {
44
31
  console.warn("Failed to determine region:", error);
45
32
  return "unknown";
@@ -47,9 +34,10 @@ function region() {
47
34
  }
48
35
  function isAWSDomain() {
49
36
  try {
37
+ const host = getBusinessHost();
50
38
  const domain = getCurrentDomain();
51
39
  const strategy = EnvironmentStrategyFactory.getStrategy(domain);
52
- return strategy.isAWS(location.host);
40
+ return strategy.isAWS(host);
53
41
  } catch (error) {
54
42
  console.warn("Failed to determine AWS domain:", error);
55
43
  return false;
@@ -1,9 +1,13 @@
1
1
  // src/gateway/index.ts
2
- import { environment, isAWSDomain, region, SUPPORTED_DOMAINS } from "../environment/index";
2
+ import { getBusinessHost, SUPPORTED_DOMAINS } from "../business-host";
3
+ import { environment, isAWSDomain, region } from "../environment/index";
3
4
  import { URLStrategyFactory } from "./factory";
4
5
  function getCurrentDomain() {
5
- const host = location.host;
6
- const domain = SUPPORTED_DOMAINS.find((domain2) => host.endsWith(domain2));
6
+ const host = getBusinessHost();
7
+ const hostname = host.replace(/:\d+$/, "").toLowerCase();
8
+ const domain = SUPPORTED_DOMAINS.find(
9
+ (domain2) => hostname === domain2 || hostname.endsWith(`.${domain2}`)
10
+ );
7
11
  if (!domain) {
8
12
  throw new Error(`Unsupported domain: ${host}`);
9
13
  }
@@ -15,6 +15,10 @@ declare global {
15
15
  iOS_Native_InjectJavascript: {
16
16
  postMessage: (p: null) => void;
17
17
  };
18
+ // native 可随时 removeScriptMessageHandler 摘除,声明为可选
19
+ iOS_Native_FlushMessageQueue?: {
20
+ postMessage: (p: null) => void;
21
+ };
18
22
  };
19
23
  };
20
24
  __bl?: {
@@ -1,43 +1,84 @@
1
1
  // src/hybrid.ts
2
+ import { arms } from "./arms";
2
3
  import Logger from "./logger";
3
4
  import { iOSWebview } from "./platform";
4
5
  var queue = [];
5
- document.addEventListener("WebViewJavascriptBridgeReady", () => {
6
- queue.forEach(({ method, params, callback }) => {
7
- if (window.SuperAcme && typeof window.SuperAcme.call === "function") {
8
- window.SuperAcme.call(method, params, callback);
9
- }
10
- });
11
- queue.length = 0;
6
+ var isFlushMessageQueueAvailable = () => {
7
+ var _a, _b, _c;
8
+ if (!iOSWebview) {
9
+ return true;
10
+ }
11
+ const postMessage = (_c = (_b = (_a = window.webkit) == null ? void 0 : _a.messageHandlers) == null ? void 0 : _b.iOS_Native_FlushMessageQueue) == null ? void 0 : _c.postMessage;
12
+ return typeof postMessage === "function";
13
+ };
14
+ var hasReportedMissingHandler = false;
15
+ var reportMissingHandlerOnce = () => {
16
+ if (hasReportedMissingHandler) {
17
+ return;
18
+ }
19
+ hasReportedMissingHandler = true;
20
+ Logger.warn("iOS_Native_FlushMessageQueue unavailable, bridge call rejected");
21
+ arms.sum("BRIDGE_FLUSH_QUEUE_MISSING");
22
+ };
23
+ var buildErrorResponse = (code, method, params, message, stack) => JSON.stringify({
24
+ success: false,
25
+ error: {
26
+ code,
27
+ message,
28
+ method,
29
+ params,
30
+ stack,
31
+ timestamp: (/* @__PURE__ */ new Date()).toISOString()
32
+ }
12
33
  });
13
- function bridgeCall(method, params, callback) {
34
+ var invokeBridge = (method, params, callback) => {
14
35
  var _a;
15
- if (method === "log") {
16
- Logger.debug("bridgeCall", method, params);
17
- } else {
18
- Logger.info("bridgeCall", method, params);
19
- }
20
36
  try {
21
37
  if (((_a = window.SuperAcme) == null ? void 0 : _a.call) && typeof window.SuperAcme.call === "function") {
38
+ if (!isFlushMessageQueueAvailable()) {
39
+ reportMissingHandlerOnce();
40
+ callback(
41
+ buildErrorResponse(
42
+ "BRIDGE_FLUSH_QUEUE_MISSING",
43
+ method,
44
+ params,
45
+ "iOS_Native_FlushMessageQueue is unavailable"
46
+ )
47
+ );
48
+ return;
49
+ }
22
50
  window.SuperAcme.call(method, params, callback);
23
51
  } else {
24
52
  queue.push({ method, params, callback });
25
53
  }
26
54
  } catch (error) {
27
55
  Logger.error("bridgeCall failed:", error);
28
- const errorResponse = {
29
- success: false,
30
- error: {
31
- code: "BRIDGE_CALL_ERROR",
32
- message: error instanceof Error ? error.message : String(error),
56
+ callback(
57
+ buildErrorResponse(
58
+ "BRIDGE_CALL_ERROR",
33
59
  method,
34
60
  params,
35
- stack: error instanceof Error ? error.stack : void 0,
36
- timestamp: (/* @__PURE__ */ new Date()).toISOString()
37
- }
38
- };
39
- callback(JSON.stringify(errorResponse));
61
+ error instanceof Error ? error.message : String(error),
62
+ error instanceof Error ? error.stack : void 0
63
+ )
64
+ );
65
+ }
66
+ };
67
+ document.addEventListener("WebViewJavascriptBridgeReady", () => {
68
+ queue.forEach(({ method, params, callback }) => {
69
+ if (window.SuperAcme && typeof window.SuperAcme.call === "function") {
70
+ invokeBridge(method, params, callback);
71
+ }
72
+ });
73
+ queue.length = 0;
74
+ });
75
+ function bridgeCall(method, params, callback) {
76
+ if (method === "log") {
77
+ Logger.debug("bridgeCall", method, params);
78
+ } else {
79
+ Logger.info("bridgeCall", method, params);
40
80
  }
81
+ invokeBridge(method, params, callback);
41
82
  }
42
83
  function bridgeBootstrap() {
43
84
  function bootstrap(callback) {
@@ -1,4 +1,5 @@
1
1
  export * from './arms';
2
+ export * from './business-host';
2
3
  export * from './cookie';
3
4
  export * from './environment/index';
4
5
  export * from './gateway/index';
package/dist/esm/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  // src/index.ts
2
2
  export * from "./arms";
3
+ export * from "./business-host";
3
4
  export * from "./cookie";
4
5
  export * from "./environment/index";
5
6
  export * from "./gateway/index";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@microbt/environment",
3
- "version": "1.2.12",
3
+ "version": "1.2.14",
4
4
  "description": "microbt app library for environment",
5
5
  "keywords": [],
6
6
  "license": "MIT",