request-iframe 0.1.1 → 0.2.0

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.
Files changed (208) hide show
  1. package/QUICKSTART.CN.md +4 -2
  2. package/QUICKSTART.md +4 -2
  3. package/README.CN.md +129 -33
  4. package/README.md +116 -15
  5. package/cdn/request-iframe-react.umd.js +3354 -0
  6. package/cdn/request-iframe-react.umd.js.map +1 -0
  7. package/cdn/request-iframe-react.umd.min.js +2 -0
  8. package/cdn/request-iframe-react.umd.min.js.map +1 -0
  9. package/cdn/request-iframe.umd.js +19735 -0
  10. package/cdn/request-iframe.umd.js.map +1 -0
  11. package/cdn/request-iframe.umd.min.js +4 -0
  12. package/cdn/request-iframe.umd.min.js.map +1 -0
  13. package/esm/api/client.js +29 -21
  14. package/esm/api/endpoint.js +229 -0
  15. package/esm/api/server.js +16 -8
  16. package/esm/constants/debug.js +17 -0
  17. package/esm/constants/index.js +84 -67
  18. package/esm/constants/log.js +11 -0
  19. package/esm/constants/messages.js +3 -0
  20. package/esm/constants/warn-once.js +15 -0
  21. package/esm/endpoint/facade.js +390 -0
  22. package/esm/endpoint/heartbeat/heartbeat.js +60 -0
  23. package/esm/endpoint/heartbeat/ping.js +20 -0
  24. package/esm/endpoint/index.js +13 -0
  25. package/esm/endpoint/infra/hub.js +316 -0
  26. package/esm/endpoint/infra/inbox.js +232 -0
  27. package/esm/endpoint/infra/outbox.js +408 -0
  28. package/esm/endpoint/stream/dispatcher.js +58 -0
  29. package/esm/endpoint/stream/errors.js +27 -0
  30. package/esm/endpoint/stream/factory.js +76 -0
  31. package/esm/endpoint/stream/file-auto-resolve.js +34 -0
  32. package/esm/endpoint/stream/file-writable.js +105 -0
  33. package/esm/endpoint/stream/handler.js +26 -0
  34. package/esm/{core → impl}/client.js +240 -317
  35. package/esm/{core → impl}/response.js +113 -155
  36. package/esm/impl/server.js +568 -0
  37. package/esm/index.js +13 -6
  38. package/esm/message/ack.js +27 -0
  39. package/esm/message/channel-cache.js +108 -0
  40. package/esm/message/channel.js +90 -4
  41. package/esm/message/dispatcher.js +88 -75
  42. package/esm/stream/error.js +22 -0
  43. package/esm/stream/index.js +3 -1
  44. package/esm/stream/readable-stream.js +45 -9
  45. package/esm/stream/stream-core.js +7 -2
  46. package/esm/stream/writable-stream.js +97 -26
  47. package/esm/utils/blob.js +16 -0
  48. package/esm/utils/cache.js +25 -76
  49. package/esm/utils/content-type.js +81 -0
  50. package/esm/utils/debug.js +156 -179
  51. package/esm/utils/hooks.js +130 -0
  52. package/esm/utils/id.js +14 -0
  53. package/esm/utils/iframe.js +20 -0
  54. package/esm/utils/index.js +11 -163
  55. package/esm/utils/is.js +3 -0
  56. package/esm/utils/logger.js +55 -0
  57. package/esm/utils/promise.js +3 -0
  58. package/esm/utils/window.js +31 -0
  59. package/library/api/client.d.ts.map +1 -1
  60. package/library/api/client.js +30 -22
  61. package/library/api/endpoint.d.ts +23 -0
  62. package/library/api/endpoint.d.ts.map +1 -0
  63. package/library/api/endpoint.js +235 -0
  64. package/library/api/server.d.ts +4 -1
  65. package/library/api/server.d.ts.map +1 -1
  66. package/library/api/server.js +16 -8
  67. package/library/constants/debug.d.ts +18 -0
  68. package/library/constants/debug.d.ts.map +1 -0
  69. package/library/constants/debug.js +23 -0
  70. package/library/constants/index.d.ts +22 -2
  71. package/library/constants/index.d.ts.map +1 -1
  72. package/library/constants/index.js +110 -67
  73. package/library/constants/log.d.ts +12 -0
  74. package/library/constants/log.d.ts.map +1 -0
  75. package/library/constants/log.js +17 -0
  76. package/library/constants/messages.d.ts +3 -0
  77. package/library/constants/messages.d.ts.map +1 -1
  78. package/library/constants/messages.js +3 -0
  79. package/library/constants/warn-once.d.ts +12 -0
  80. package/library/constants/warn-once.d.ts.map +1 -0
  81. package/library/constants/warn-once.js +22 -0
  82. package/library/endpoint/facade.d.ts +238 -0
  83. package/library/endpoint/facade.d.ts.map +1 -0
  84. package/library/endpoint/facade.js +398 -0
  85. package/library/endpoint/heartbeat/heartbeat.d.ts +34 -0
  86. package/library/endpoint/heartbeat/heartbeat.d.ts.map +1 -0
  87. package/library/endpoint/heartbeat/heartbeat.js +67 -0
  88. package/library/endpoint/heartbeat/ping.d.ts +18 -0
  89. package/library/endpoint/heartbeat/ping.d.ts.map +1 -0
  90. package/library/endpoint/heartbeat/ping.js +26 -0
  91. package/library/endpoint/index.d.ts +16 -0
  92. package/library/endpoint/index.d.ts.map +1 -0
  93. package/library/endpoint/index.js +114 -0
  94. package/library/endpoint/infra/hub.d.ts +170 -0
  95. package/library/endpoint/infra/hub.d.ts.map +1 -0
  96. package/library/endpoint/infra/hub.js +323 -0
  97. package/library/endpoint/infra/inbox.d.ts +73 -0
  98. package/library/endpoint/infra/inbox.d.ts.map +1 -0
  99. package/library/endpoint/infra/inbox.js +239 -0
  100. package/library/endpoint/infra/outbox.d.ts +149 -0
  101. package/library/endpoint/infra/outbox.d.ts.map +1 -0
  102. package/library/endpoint/infra/outbox.js +415 -0
  103. package/library/endpoint/stream/dispatcher.d.ts +33 -0
  104. package/library/endpoint/stream/dispatcher.d.ts.map +1 -0
  105. package/library/endpoint/stream/dispatcher.js +66 -0
  106. package/library/endpoint/stream/errors.d.ts +20 -0
  107. package/library/endpoint/stream/errors.d.ts.map +1 -0
  108. package/library/endpoint/stream/errors.js +32 -0
  109. package/library/endpoint/stream/factory.d.ts +44 -0
  110. package/library/endpoint/stream/factory.d.ts.map +1 -0
  111. package/library/endpoint/stream/factory.js +82 -0
  112. package/library/endpoint/stream/file-auto-resolve.d.ts +26 -0
  113. package/library/endpoint/stream/file-auto-resolve.d.ts.map +1 -0
  114. package/library/endpoint/stream/file-auto-resolve.js +41 -0
  115. package/library/endpoint/stream/file-writable.d.ts +33 -0
  116. package/library/endpoint/stream/file-writable.d.ts.map +1 -0
  117. package/library/endpoint/stream/file-writable.js +115 -0
  118. package/library/endpoint/stream/handler.d.ts +20 -0
  119. package/library/endpoint/stream/handler.d.ts.map +1 -0
  120. package/library/endpoint/stream/handler.js +32 -0
  121. package/library/{core → impl}/client.d.ts +16 -13
  122. package/library/impl/client.d.ts.map +1 -0
  123. package/library/{core → impl}/client.js +251 -330
  124. package/library/{core → impl}/request.d.ts.map +1 -1
  125. package/library/{core → impl}/response.d.ts +5 -10
  126. package/library/impl/response.d.ts.map +1 -0
  127. package/library/{core → impl}/response.js +113 -155
  128. package/library/{core → impl}/server.d.ts +22 -55
  129. package/library/impl/server.d.ts.map +1 -0
  130. package/library/impl/server.js +575 -0
  131. package/library/index.d.ts +13 -6
  132. package/library/index.d.ts.map +1 -1
  133. package/library/index.js +16 -16
  134. package/library/message/ack.d.ts +15 -0
  135. package/library/message/ack.d.ts.map +1 -0
  136. package/library/message/ack.js +33 -0
  137. package/library/message/channel-cache.d.ts +26 -0
  138. package/library/message/channel-cache.d.ts.map +1 -0
  139. package/library/message/channel-cache.js +115 -0
  140. package/library/message/channel.d.ts +53 -6
  141. package/library/message/channel.d.ts.map +1 -1
  142. package/library/message/channel.js +94 -8
  143. package/library/message/dispatcher.d.ts +7 -0
  144. package/library/message/dispatcher.d.ts.map +1 -1
  145. package/library/message/dispatcher.js +89 -76
  146. package/library/stream/error.d.ts +24 -0
  147. package/library/stream/error.d.ts.map +1 -0
  148. package/library/stream/error.js +29 -0
  149. package/library/stream/index.d.ts +4 -1
  150. package/library/stream/index.d.ts.map +1 -1
  151. package/library/stream/index.js +7 -4
  152. package/library/stream/readable-stream.d.ts.map +1 -1
  153. package/library/stream/readable-stream.js +46 -10
  154. package/library/stream/stream-core.d.ts.map +1 -1
  155. package/library/stream/stream-core.js +6 -1
  156. package/library/stream/writable-stream.d.ts.map +1 -1
  157. package/library/stream/writable-stream.js +99 -28
  158. package/library/types/index.d.ts +15 -19
  159. package/library/types/index.d.ts.map +1 -1
  160. package/library/utils/blob.d.ts +3 -0
  161. package/library/utils/blob.d.ts.map +1 -0
  162. package/library/utils/blob.js +22 -0
  163. package/library/utils/cache.d.ts +10 -20
  164. package/library/utils/cache.d.ts.map +1 -1
  165. package/library/utils/cache.js +25 -79
  166. package/library/utils/content-type.d.ts +13 -0
  167. package/library/utils/content-type.d.ts.map +1 -0
  168. package/library/utils/content-type.js +87 -0
  169. package/library/utils/debug.d.ts.map +1 -1
  170. package/library/utils/debug.js +155 -177
  171. package/library/utils/hooks.d.ts +30 -0
  172. package/library/utils/hooks.d.ts.map +1 -0
  173. package/library/utils/hooks.js +139 -0
  174. package/library/utils/id.d.ts +9 -0
  175. package/library/utils/id.d.ts.map +1 -0
  176. package/library/utils/id.js +21 -0
  177. package/library/utils/iframe.d.ts +5 -0
  178. package/library/utils/iframe.d.ts.map +1 -0
  179. package/library/utils/iframe.js +25 -0
  180. package/library/utils/index.d.ts +7 -34
  181. package/library/utils/index.d.ts.map +1 -1
  182. package/library/utils/index.js +58 -194
  183. package/library/utils/is.d.ts +2 -0
  184. package/library/utils/is.d.ts.map +1 -0
  185. package/library/utils/is.js +9 -0
  186. package/library/utils/logger.d.ts +13 -0
  187. package/library/utils/logger.d.ts.map +1 -0
  188. package/library/utils/logger.js +63 -0
  189. package/library/utils/promise.d.ts +2 -0
  190. package/library/utils/promise.d.ts.map +1 -0
  191. package/library/utils/promise.js +9 -0
  192. package/library/utils/window.d.ts +2 -0
  193. package/library/utils/window.d.ts.map +1 -0
  194. package/library/utils/window.js +38 -0
  195. package/package.json +49 -2
  196. package/react/package.json +2 -1
  197. package/esm/core/client-server.js +0 -294
  198. package/esm/core/server.js +0 -776
  199. package/library/core/client-server.d.ts +0 -97
  200. package/library/core/client-server.d.ts.map +0 -1
  201. package/library/core/client-server.js +0 -301
  202. package/library/core/client.d.ts.map +0 -1
  203. package/library/core/response.d.ts.map +0 -1
  204. package/library/core/server.d.ts.map +0 -1
  205. package/library/core/server.js +0 -781
  206. /package/esm/{core → impl}/request.js +0 -0
  207. /package/library/{core → impl}/request.d.ts +0 -0
  208. /package/library/{core → impl}/request.js +0 -0
@@ -0,0 +1 @@
1
+ {"version":3,"file":"promise.d.ts","sourceRoot":"","sources":["../../src/utils/promise.ts"],"names":[],"mappings":"AAAA,wBAAgB,SAAS,CAAC,CAAC,EAAE,KAAK,EAAE,GAAG,GAAG,KAAK,IAAI,OAAO,CAAC,CAAC,CAAC,CAE5D"}
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.isPromise = isPromise;
7
+ function isPromise(value) {
8
+ return value !== null && typeof value === 'object' && 'then' in value;
9
+ }
@@ -0,0 +1,2 @@
1
+ export declare function isWindowAvailable(targetWindow: Window | null | undefined): boolean;
2
+ //# sourceMappingURL=window.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"window.d.ts","sourceRoot":"","sources":["../../src/utils/window.ts"],"names":[],"mappings":"AAOA,wBAAgB,iBAAiB,CAAC,YAAY,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,GAAG,OAAO,CAyBlF"}
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.isWindowAvailable = isWindowAvailable;
7
+ var _is = require("./is");
8
+ /**
9
+ * Check if target window is still available (not closed/removed)
10
+ * @param targetWindow Target window to check
11
+ * @returns true if window is available, false otherwise
12
+ */
13
+
14
+ function isWindowAvailable(targetWindow) {
15
+ if (!targetWindow) {
16
+ return false;
17
+ }
18
+ try {
19
+ /** Must have postMessage to be a usable target */
20
+ if (!(0, _is.isFunction)(targetWindow.postMessage)) {
21
+ return false;
22
+ }
23
+
24
+ /** For windows opened via window.open(), check closed property */
25
+ if ('closed' in targetWindow && targetWindow.closed === true) {
26
+ return false;
27
+ }
28
+
29
+ /**
30
+ * Avoid touching cross-origin properties (like document) which may throw.
31
+ * If closed is not true and postMessage exists, treat as available.
32
+ */
33
+ return true;
34
+ } catch (e) {
35
+ /** If accessing window properties throws an error, window is likely closed */
36
+ return false;
37
+ }
38
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "request-iframe",
3
- "version": "0.1.1",
3
+ "version": "0.2.0",
4
4
  "description": "Communicate with iframes like sending HTTP requests",
5
5
  "main": "library/index.js",
6
6
  "module": "esm/index.js",
@@ -11,6 +11,46 @@
11
11
  "import": "./esm/index.js",
12
12
  "require": "./library/index.js"
13
13
  },
14
+ "./constants": {
15
+ "types": "./library/constants/index.d.ts",
16
+ "import": "./esm/constants/index.js",
17
+ "require": "./library/constants/index.js"
18
+ },
19
+ "./types": {
20
+ "types": "./library/types/index.d.ts",
21
+ "import": "./esm/types/index.js",
22
+ "require": "./library/types/index.js"
23
+ },
24
+ "./message": {
25
+ "types": "./library/message/index.d.ts",
26
+ "import": "./esm/message/index.js",
27
+ "require": "./library/message/index.js"
28
+ },
29
+ "./message/ack": {
30
+ "types": "./library/message/ack.d.ts",
31
+ "import": "./esm/message/ack.js",
32
+ "require": "./library/message/ack.js"
33
+ },
34
+ "./message/channel-cache": {
35
+ "types": "./library/message/channel-cache.d.ts",
36
+ "import": "./esm/message/channel-cache.js",
37
+ "require": "./library/message/channel-cache.js"
38
+ },
39
+ "./stream": {
40
+ "types": "./library/stream/index.d.ts",
41
+ "import": "./esm/stream/index.js",
42
+ "require": "./library/stream/index.js"
43
+ },
44
+ "./endpoint": {
45
+ "types": "./library/endpoint/index.d.ts",
46
+ "import": "./esm/endpoint/index.js",
47
+ "require": "./library/endpoint/index.js"
48
+ },
49
+ "./interceptors": {
50
+ "types": "./library/interceptors/index.d.ts",
51
+ "import": "./esm/interceptors/index.js",
52
+ "require": "./library/interceptors/index.js"
53
+ },
14
54
  "./react": {
15
55
  "types": "./react/library/index.d.ts",
16
56
  "import": "./react/esm/index.js",
@@ -26,6 +66,8 @@
26
66
  "build-react:types": "tsc -p react/tsconfig.json --emitDeclarationOnly",
27
67
  "build-react:js": "babel react/src --config-file ./react/babel.config.js --out-dir react/library --extensions .ts,.tsx --copy-files --ignore \"**/__tests__/**\",\"**/*.test.ts\",\"**/*.test.tsx\",\"**/*.d.ts\",\"**/*.d.ts.map\"",
28
68
  "build-react:js:esm": "babel react/src --config-file ./react/babel.esm.config.js --out-dir react/esm --extensions .ts,.tsx --copy-files --ignore \"**/__tests__/**\",\"**/*.test.ts\",\"**/*.test.tsx\",\"**/*.d.ts\",\"**/*.d.ts.map\"",
69
+ "build:cdn:preclean": "node scripts/preclean-cdn.js",
70
+ "build:cdn": "rollup -c rollup.cdn.config.mjs",
29
71
  "build:clean": "node scripts/clean-build.js",
30
72
  "test": "jest",
31
73
  "test:watch": "jest --watch",
@@ -37,6 +79,7 @@
37
79
  "files": [
38
80
  "library",
39
81
  "esm",
82
+ "cdn",
40
83
  "react/library",
41
84
  "react/esm",
42
85
  "react/package.json",
@@ -60,7 +103,7 @@
60
103
  "author": "gxlmyacc",
61
104
  "license": "MIT",
62
105
  "dependencies": {
63
- "core-js": "^3.48.0"
106
+ "core-js": "^3"
64
107
  },
65
108
  "peerDependencies": {
66
109
  "react": ">=16.8.0"
@@ -77,6 +120,9 @@
77
120
  "@babel/preset-env": "^7.28.6",
78
121
  "@babel/preset-typescript": "^7.28.5",
79
122
  "@babel/runtime": "^7.28.6",
123
+ "@rollup/plugin-commonjs": "^29.0.0",
124
+ "@rollup/plugin-node-resolve": "^16.0.3",
125
+ "@rollup/plugin-terser": "^0.4.4",
80
126
  "@testing-library/dom": "^10.4.1",
81
127
  "@testing-library/react": "^16.3.2",
82
128
  "@types/jest": "^29.5.12",
@@ -91,6 +137,7 @@
91
137
  "jest-environment-jsdom": "^29.7.0",
92
138
  "react": "^19.2.3",
93
139
  "react-dom": "^19.2.3",
140
+ "rollup": "^4.57.1",
94
141
  "ts-jest": "^29.1.2",
95
142
  "typescript": "^5.3.3"
96
143
  }
@@ -14,7 +14,8 @@
14
14
  "sideEffects": false,
15
15
  "files": [
16
16
  "library",
17
- "esm"
17
+ "esm",
18
+ "cdn"
18
19
  ],
19
20
  "peerDependencies": {
20
21
  "react": ">=16.8.0",
@@ -1,294 +0,0 @@
1
- import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
2
- import "core-js/modules/es.array.iterator.js";
3
- import "core-js/modules/es.map.js";
4
- import "core-js/modules/es.set.js";
5
- import "core-js/modules/es.string.starts-with.js";
6
- import "core-js/modules/web.dom-collections.for-each.js";
7
- import "core-js/modules/web.dom-collections.iterator.js";
8
- import { MessageDispatcher } from '../message';
9
- import { getOrCreateMessageChannel, releaseMessageChannel } from '../utils/cache';
10
- import { isCompatibleVersion } from '../utils';
11
- import { MessageType, ProtocolVersion, Messages, formatMessage, MessageRole, OriginConstant } from '../constants';
12
-
13
- /**
14
- * Stream message handler callback
15
- */
16
-
17
- /**
18
- * Pending request awaiting response
19
- */
20
-
21
- /**
22
- * ClientServer configuration options
23
- */
24
-
25
- /**
26
- * RequestIframeClientServer - Client-side message server
27
- * Only handles responses, not requests
28
- * Uses shared MessageDispatcher (backed by MessageChannel) to listen for and send messages
29
- */
30
- export class RequestIframeClientServer {
31
- constructor(options, instanceId) {
32
- var _options$versionValid;
33
- /** Pending requests awaiting response */
34
- _defineProperty(this, "pendingRequests", new Map());
35
- /**
36
- * Avoid spamming logs for the same requestId when closed/destroyed
37
- */
38
- _defineProperty(this, "warnedMissingPendingWhenClosed", new Set());
39
- /** List of unregister handler functions */
40
- _defineProperty(this, "unregisterFns", []);
41
- /** Whether opened */
42
- _defineProperty(this, "_isOpen", false);
43
- this.versionValidator = (_options$versionValid = options === null || options === void 0 ? void 0 : options.versionValidator) !== null && _options$versionValid !== void 0 ? _options$versionValid : isCompatibleVersion;
44
-
45
- // Get or create shared channel and create dispatcher
46
- var channel = getOrCreateMessageChannel(options === null || options === void 0 ? void 0 : options.secretKey);
47
- this.dispatcher = new MessageDispatcher(channel, MessageRole.CLIENT, instanceId);
48
- this.dispatcher.setAutoAckLimits({
49
- maxMetaLength: options === null || options === void 0 ? void 0 : options.autoAckMaxMetaLength,
50
- maxIdLength: options === null || options === void 0 ? void 0 : options.autoAckMaxIdLength
51
- });
52
-
53
- // Auto-open by default (unless explicitly set to false)
54
- if ((options === null || options === void 0 ? void 0 : options.autoOpen) !== false) {
55
- this.open();
56
- }
57
- }
58
-
59
- /**
60
- * Open message handling (register message handlers)
61
- */
62
- open() {
63
- if (this._isOpen) return;
64
- this._isOpen = true;
65
- this.registerHandlers();
66
- }
67
-
68
- /**
69
- * Close message handling (unregister message handlers, but don't release channel)
70
- */
71
- close() {
72
- if (!this._isOpen) return;
73
- this._isOpen = false;
74
-
75
- // Unregister all handlers
76
- this.unregisterFns.forEach(fn => fn());
77
- this.unregisterFns.length = 0;
78
- }
79
-
80
- /**
81
- * Whether opened
82
- */
83
- get isOpen() {
84
- return this._isOpen;
85
- }
86
-
87
- /**
88
- * Register message handlers
89
- */
90
- registerHandlers() {
91
- var handlerOptions = {
92
- versionValidator: this.versionValidator,
93
- onVersionError: this.handleVersionError.bind(this)
94
- };
95
-
96
- // Bind handleClientResponse to ensure correct 'this' context
97
- var boundHandleClientResponse = this.handleClientResponse.bind(this);
98
-
99
- // Handle ACK messages
100
- this.unregisterFns.push(this.dispatcher.registerHandler(MessageType.ACK, boundHandleClientResponse, handlerOptions));
101
-
102
- // Handle ASYNC messages
103
- this.unregisterFns.push(this.dispatcher.registerHandler(MessageType.ASYNC, boundHandleClientResponse, handlerOptions));
104
-
105
- // Handle RESPONSE messages
106
- this.unregisterFns.push(this.dispatcher.registerHandler(MessageType.RESPONSE, boundHandleClientResponse, handlerOptions));
107
-
108
- // Handle ERROR messages
109
- this.unregisterFns.push(this.dispatcher.registerHandler(MessageType.ERROR, boundHandleClientResponse, handlerOptions));
110
-
111
- // Handle PONG messages
112
- this.unregisterFns.push(this.dispatcher.registerHandler(MessageType.PONG, this.handlePong.bind(this), handlerOptions));
113
-
114
- // Handle PING messages (server -> client heartbeat)
115
- this.unregisterFns.push(this.dispatcher.registerHandler(MessageType.PING, this.handlePing.bind(this), handlerOptions));
116
-
117
- // Handle stream_start messages (route to handleClientResponse so it reaches send callback)
118
- // Note: stream_start is handled in send callback, not through streamCallback
119
- this.unregisterFns.push(this.dispatcher.registerHandler(MessageType.STREAM_START, boundHandleClientResponse, handlerOptions));
120
-
121
- // Handle other stream messages (stream_data, stream_end, etc.)
122
- this.unregisterFns.push(this.dispatcher.registerHandler(type => type.startsWith('stream_') && type !== MessageType.STREAM_START, (data, context) => {
123
- var _this$streamCallback;
124
- return (_this$streamCallback = this.streamCallback) === null || _this$streamCallback === void 0 ? void 0 : _this$streamCallback.call(this, data, context);
125
- }, handlerOptions));
126
- }
127
- handlePing(data, context) {
128
- if (!context.source) return;
129
- // Mark accepted so MessageDispatcher can auto-send ACK when requireAck === true
130
- if (!context.handledBy) {
131
- context.accepted = true;
132
- context.handledBy = 'client';
133
- }
134
- // Reply PONG
135
- this.dispatcher.sendMessage(context.source, context.origin, MessageType.PONG, data.requestId, {
136
- targetId: data.creatorId
137
- });
138
- }
139
-
140
- /**
141
- * Handle protocol version error
142
- */
143
- handleVersionError(data, context, version) {
144
- // For response messages, we need to notify the waiting request
145
- var pending = this.pendingRequests.get(data.requestId);
146
- if (pending) {
147
- this.pendingRequests.delete(data.requestId);
148
- pending.reject(new Error(formatMessage(Messages.PROTOCOL_VERSION_TOO_LOW, version, ProtocolVersion.MIN_SUPPORTED)));
149
- }
150
- }
151
-
152
- /**
153
- * Handle client response
154
- */
155
- handleClientResponse(data, context) {
156
- var pending = this.pendingRequests.get(data.requestId);
157
- if (!pending) {
158
- /**
159
- * Pending request not found - ignore by default.
160
- *
161
- * If client server is already closed/destroyed, emit a warning to help debugging:
162
- * this usually means the client was recreated/unmounted before the response arrived.
163
- */
164
- if (!this._isOpen) {
165
- var key = data.requestId;
166
- if (!this.warnedMissingPendingWhenClosed.has(key)) {
167
- this.warnedMissingPendingWhenClosed.add(key);
168
- // eslint-disable-next-line no-console
169
- console.warn(formatMessage(Messages.CLIENT_SERVER_IGNORED_MESSAGE_WHEN_CLOSED, data.type, data.requestId));
170
- }
171
- }
172
- return;
173
- }
174
-
175
- // Validate origin
176
- if (pending.originValidator) {
177
- try {
178
- if (!pending.originValidator(context.origin, data, context)) {
179
- return;
180
- }
181
- } catch (_unused) {
182
- // If validator throws, treat as disallowed
183
- return;
184
- }
185
- } else if (pending.origin && pending.origin !== OriginConstant.ANY && context.origin !== pending.origin) {
186
- return;
187
- }
188
-
189
- /**
190
- * Mark as handled so:
191
- * - other client instances sharing the same channel won't also process it
192
- * - MessageDispatcher can run its generalized requireAck auto-ack logic
193
- */
194
- if (!context.handledBy) {
195
- context.accepted = true;
196
- context.handledBy = 'client';
197
- }
198
-
199
- // ack, async, and stream_start don't delete pending (stream_start needs to keep pending for stream_data/stream_end)
200
- if (data.type === MessageType.ACK || data.type === MessageType.ASYNC || data.type === MessageType.STREAM_START) {
201
- pending.resolve(data);
202
- return;
203
- }
204
-
205
- // response and error delete pending
206
- this.pendingRequests.delete(data.requestId);
207
- pending.resolve(data);
208
- }
209
-
210
- /**
211
- * Handle pong
212
- */
213
- handlePong(data, context) {
214
- var pending = this.pendingRequests.get(data.requestId);
215
- if (pending) {
216
- if (pending.originValidator) {
217
- try {
218
- if (!pending.originValidator(context.origin, data, context)) {
219
- return;
220
- }
221
- } catch (_unused2) {
222
- return;
223
- }
224
- } else if (pending.origin && pending.origin !== OriginConstant.ANY && context.origin !== pending.origin) {
225
- return;
226
- }
227
- if (!context.handledBy) {
228
- context.accepted = true;
229
- context.handledBy = 'client';
230
- }
231
- this.pendingRequests.delete(data.requestId);
232
- pending.resolve(data);
233
- }
234
- }
235
-
236
- /**
237
- * Set stream message handler callback
238
- */
239
- setStreamCallback(callback) {
240
- this.streamCallback = callback;
241
- }
242
-
243
- /** Get secretKey */
244
- get secretKey() {
245
- return this.dispatcher.secretKey;
246
- }
247
-
248
- /** Get the underlying MessageDispatcher */
249
- get messageDispatcher() {
250
- return this.dispatcher;
251
- }
252
-
253
- /**
254
- * Register pending request awaiting response
255
- */
256
- _registerPendingRequest(requestId, resolve, reject, origin, originValidator) {
257
- this.pendingRequests.set(requestId, {
258
- resolve,
259
- reject,
260
- origin,
261
- originValidator
262
- });
263
- }
264
-
265
- /**
266
- * Cancel pending response
267
- */
268
- _unregisterPendingRequest(requestId) {
269
- this.pendingRequests.delete(requestId);
270
- }
271
-
272
- /**
273
- * Send ping message
274
- */
275
- sendPing(targetWindow, targetOrigin, requestId) {
276
- this.dispatcher.sendMessage(targetWindow, targetOrigin, MessageType.PING, requestId);
277
- }
278
-
279
- /**
280
- * Destroy (close and release channel reference)
281
- */
282
- destroy() {
283
- // Close first
284
- this.close();
285
-
286
- // Clear pending
287
- this.pendingRequests.clear();
288
- this.warnedMissingPendingWhenClosed.clear();
289
-
290
- // Destroy dispatcher and release channel reference
291
- this.dispatcher.destroy();
292
- releaseMessageChannel(this.dispatcher.getChannel());
293
- }
294
- }