request-iframe 0.1.1 → 0.2.1

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 +19761 -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 +115 -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 +116 -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
package/esm/api/client.js CHANGED
@@ -1,8 +1,10 @@
1
- import { getIframeTargetOrigin, generateInstanceId } from '../utils';
2
- import { RequestIframeClientServer } from '../core/client-server';
3
- import { RequestIframeClientImpl } from '../core/client';
1
+ import { getIframeTargetOrigin } from '../utils/iframe';
2
+ import { generateInstanceId } from '../utils/id';
3
+ import { RequestIframeClientImpl } from '../impl/client';
4
4
  import { setupClientDebugInterceptors } from '../utils/debug';
5
- import { Messages, ErrorCode, OriginConstant } from '../constants';
5
+ import { setRequestIframeLogLevel } from '../utils/logger';
6
+ import { Messages, ErrorCode, OriginConstant, LogLevel } from '../constants';
7
+ import { clearMessageChannelCache } from '../message/channel-cache';
6
8
 
7
9
  /**
8
10
  * Create a client (for sending requests)
@@ -41,16 +43,8 @@ export function requestIframeClient(target, options) {
41
43
  // Generate instance ID first (will be used by both client and server)
42
44
  var instanceId = generateInstanceId();
43
45
 
44
- // Create ClientServer (internally obtains or creates a shared MessageChannel)
45
- var server = new RequestIframeClientServer({
46
- secretKey,
47
- autoOpen: options === null || options === void 0 ? void 0 : options.autoOpen,
48
- autoAckMaxMetaLength: options === null || options === void 0 ? void 0 : options.autoAckMaxMetaLength,
49
- autoAckMaxIdLength: options === null || options === void 0 ? void 0 : options.autoAckMaxIdLength
50
- }, instanceId);
51
-
52
- // Create client instance
53
- var client = new RequestIframeClientImpl(targetWindow, targetOrigin, server, {
46
+ // Create client instance (internally creates its core message server)
47
+ var client = new RequestIframeClientImpl(targetWindow, targetOrigin, {
54
48
  secretKey,
55
49
  ackTimeout: options === null || options === void 0 ? void 0 : options.ackTimeout,
56
50
  timeout: options === null || options === void 0 ? void 0 : options.timeout,
@@ -58,12 +52,23 @@ export function requestIframeClient(target, options) {
58
52
  returnData: options === null || options === void 0 ? void 0 : options.returnData,
59
53
  headers: options === null || options === void 0 ? void 0 : options.headers,
60
54
  allowedOrigins: options === null || options === void 0 ? void 0 : options.allowedOrigins,
61
- validateOrigin: options === null || options === void 0 ? void 0 : options.validateOrigin
55
+ validateOrigin: options === null || options === void 0 ? void 0 : options.validateOrigin,
56
+ autoOpen: options === null || options === void 0 ? void 0 : options.autoOpen,
57
+ autoAckMaxMetaLength: options === null || options === void 0 ? void 0 : options.autoAckMaxMetaLength,
58
+ autoAckMaxIdLength: options === null || options === void 0 ? void 0 : options.autoAckMaxIdLength
62
59
  }, instanceId);
63
60
 
64
- // If trace mode is enabled, register debug interceptors
61
+ /**
62
+ * Trace/log level:
63
+ * - default: only warn/error will be printed (logger default)
64
+ * - if trace enabled: raise log level and (optionally) enable detailed debug interceptors
65
+ */
65
66
  if (options !== null && options !== void 0 && options.trace) {
66
- setupClientDebugInterceptors(client);
67
+ var level = options.trace === true ? LogLevel.TRACE : options.trace;
68
+ setRequestIframeLogLevel(level);
69
+ if (level === LogLevel.TRACE || level === LogLevel.INFO) {
70
+ setupClientDebugInterceptors(client);
71
+ }
67
72
  }
68
73
  return client;
69
74
  }
@@ -73,8 +78,11 @@ export function requestIframeClient(target, options) {
73
78
  * Note: This clears the shared message channel for the specified secretKey
74
79
  */
75
80
  export function clearRequestIframeClientCache(secretKey) {
76
- // Now client is no longer cached, only need to clear MessageChannel cache
77
- // MessageChannel cleanup is handled by clearMessageChannelCache in cache.ts
78
- // Empty implementation kept here to maintain API compatibility
79
- void secretKey;
81
+ // Client is not cached; this helper only clears shared MessageChannel cache.
82
+ // If secretKey is provided, only clear channels under that secretKey.
83
+ if (typeof secretKey === 'string') {
84
+ clearMessageChannelCache(secretKey);
85
+ return;
86
+ }
87
+ clearMessageChannelCache();
80
88
  }
@@ -0,0 +1,229 @@
1
+ import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
2
+ import "core-js/modules/es.array.map.js";
3
+ import { getIframeTargetOrigin } from '../utils/iframe';
4
+ import { generateInstanceId } from '../utils/id';
5
+ import { isWindowAvailable } from '../utils/window';
6
+ import { RequestIframeClientImpl } from '../impl/client';
7
+ import { RequestIframeServerImpl } from '../impl/server';
8
+ import { setupClientDebugInterceptors, setupServerDebugListeners } from '../utils/debug';
9
+ import { setRequestIframeLogLevel } from '../utils/logger';
10
+ import { Messages, ErrorCode, OriginConstant, LogLevel } from '../constants';
11
+
12
+ /**
13
+ * Endpoint facade type (client + server).
14
+ */
15
+
16
+ /**
17
+ * Endpoint facade options (client + server).
18
+ *
19
+ * - For client-side sending: uses RequestIframeClientOptions fields (targetOrigin, headers, etc.)
20
+ * - For server-side handling: uses RequestIframeServerOptions fields (middlewares, allowedOrigins, etc.)
21
+ *
22
+ * Note: overlapping fields (autoOpen/trace/allowedOrigins/validateOrigin/ackTimeout/autoAckMax*) apply to both sides.
23
+ */
24
+
25
+ class RequestIframeEndpointApiFacade {
26
+ constructor(params) {
27
+ var _params$options;
28
+ _defineProperty(this, "desiredOpen_", true);
29
+ /** Client send APIs */
30
+ _defineProperty(this, "send", (path, body, options) => this.getClient().send(path, body, options));
31
+ _defineProperty(this, "sendFile", (path, content, options) => this.getClient().sendFile(path, content, options));
32
+ _defineProperty(this, "sendStream", (path, stream, options) => this.getClient().sendStream(path, stream, options));
33
+ _defineProperty(this, "isConnect", () => this.getClient().isConnect());
34
+ _defineProperty(this, "getCookies", path => this.getClient().getCookies(path));
35
+ _defineProperty(this, "getCookie", (name, path) => this.getClient().getCookie(name, path));
36
+ _defineProperty(this, "setCookie", (name, value, options) => this.getClient().setCookie(name, value, options));
37
+ _defineProperty(this, "removeCookie", (name, path) => this.getClient().removeCookie(name, path));
38
+ _defineProperty(this, "clearCookies", () => this.getClient().clearCookies());
39
+ /** Server routing/middleware APIs */
40
+ _defineProperty(this, "use", (pathOrMiddleware, middleware) => this.getServer().use(pathOrMiddleware, middleware));
41
+ _defineProperty(this, "on", (path, handler) => this.getServer().on(path, handler));
42
+ _defineProperty(this, "off", path => this.getServer().off(path));
43
+ _defineProperty(this, "map", handlers => this.getServer().map(handlers));
44
+ this.targetWindow_ = params.targetWindow;
45
+ this.targetOrigin_ = params.targetOrigin;
46
+ this.options_ = params.options;
47
+ this.endpointId_ = params.endpointId;
48
+ this.desiredOpen_ = ((_params$options = params.options) === null || _params$options === void 0 ? void 0 : _params$options.autoOpen) !== false;
49
+ }
50
+
51
+ /**
52
+ * Lazy create client.
53
+ */
54
+ getClient() {
55
+ if (!this.client_) {
56
+ var options = this.options_;
57
+ var client = new RequestIframeClientImpl(this.targetWindow_, this.targetOrigin_, {
58
+ secretKey: options === null || options === void 0 ? void 0 : options.secretKey,
59
+ ackTimeout: options === null || options === void 0 ? void 0 : options.ackTimeout,
60
+ timeout: options === null || options === void 0 ? void 0 : options.timeout,
61
+ asyncTimeout: options === null || options === void 0 ? void 0 : options.asyncTimeout,
62
+ returnData: options === null || options === void 0 ? void 0 : options.returnData,
63
+ headers: options === null || options === void 0 ? void 0 : options.headers,
64
+ allowedOrigins: options === null || options === void 0 ? void 0 : options.allowedOrigins,
65
+ validateOrigin: options === null || options === void 0 ? void 0 : options.validateOrigin,
66
+ autoOpen: false,
67
+ autoAckMaxMetaLength: options === null || options === void 0 ? void 0 : options.autoAckMaxMetaLength,
68
+ autoAckMaxIdLength: options === null || options === void 0 ? void 0 : options.autoAckMaxIdLength
69
+ }, this.endpointId_);
70
+ if (options !== null && options !== void 0 && options.trace) {
71
+ var level = options.trace === true ? LogLevel.TRACE : options.trace;
72
+ setRequestIframeLogLevel(level);
73
+ if (level === LogLevel.TRACE || level === LogLevel.INFO) {
74
+ setupClientDebugInterceptors(client);
75
+ }
76
+ }
77
+ this.client_ = client;
78
+ }
79
+ if (this.desiredOpen_ && !this.client_.isOpen) {
80
+ this.client_.open();
81
+ }
82
+ if (!this.desiredOpen_ && this.client_.isOpen) {
83
+ this.client_.close();
84
+ }
85
+ return this.client_;
86
+ }
87
+
88
+ /**
89
+ * Lazy create server.
90
+ */
91
+ getServer() {
92
+ if (!this.server_) {
93
+ var options = this.options_;
94
+ var server = new RequestIframeServerImpl({
95
+ secretKey: options === null || options === void 0 ? void 0 : options.secretKey,
96
+ id: this.endpointId_,
97
+ ackTimeout: options === null || options === void 0 ? void 0 : options.ackTimeout,
98
+ autoOpen: false,
99
+ allowedOrigins: options === null || options === void 0 ? void 0 : options.allowedOrigins,
100
+ validateOrigin: options === null || options === void 0 ? void 0 : options.validateOrigin,
101
+ maxConcurrentRequestsPerClient: options === null || options === void 0 ? void 0 : options.maxConcurrentRequestsPerClient,
102
+ autoAckMaxMetaLength: options === null || options === void 0 ? void 0 : options.autoAckMaxMetaLength,
103
+ autoAckMaxIdLength: options === null || options === void 0 ? void 0 : options.autoAckMaxIdLength
104
+ });
105
+ if (options !== null && options !== void 0 && options.trace) {
106
+ var level = options.trace === true ? LogLevel.TRACE : options.trace;
107
+ setRequestIframeLogLevel(level);
108
+ if (level === LogLevel.TRACE || level === LogLevel.INFO) {
109
+ setupServerDebugListeners(server);
110
+ }
111
+ }
112
+ this.server_ = server;
113
+ }
114
+ if (this.desiredOpen_ && !this.server_.isOpen) {
115
+ this.server_.open();
116
+ }
117
+ if (!this.desiredOpen_ && this.server_.isOpen) {
118
+ this.server_.close();
119
+ }
120
+ return this.server_;
121
+ }
122
+
123
+ /**
124
+ * Optional accessors for debugging.
125
+ * Note: accessing will trigger lazy creation.
126
+ */
127
+ get client() {
128
+ return this.getClient();
129
+ }
130
+ get server() {
131
+ return this.getServer();
132
+ }
133
+
134
+ /** Client fields */
135
+ get targetWindow() {
136
+ return this.targetWindow_;
137
+ }
138
+ get id() {
139
+ return this.endpointId_;
140
+ }
141
+ get isOpen() {
142
+ /**
143
+ * Facade-level open state.
144
+ * - When client/server is lazily created later, it will follow this state.
145
+ */
146
+ return this.desiredOpen_;
147
+ }
148
+ get interceptors() {
149
+ return this.getClient().interceptors;
150
+ }
151
+ set interceptors(value) {
152
+ this.getClient().interceptors = value;
153
+ }
154
+ isAvailable() {
155
+ return isWindowAvailable(this.targetWindow_);
156
+ }
157
+ open() {
158
+ var _this$client_, _this$server_;
159
+ this.desiredOpen_ = true;
160
+ (_this$client_ = this.client_) === null || _this$client_ === void 0 || _this$client_.open();
161
+ (_this$server_ = this.server_) === null || _this$server_ === void 0 || _this$server_.open();
162
+ }
163
+ close() {
164
+ var _this$client_2, _this$server_2;
165
+ this.desiredOpen_ = false;
166
+ (_this$client_2 = this.client_) === null || _this$client_2 === void 0 || _this$client_2.close();
167
+ (_this$server_2 = this.server_) === null || _this$server_2 === void 0 || _this$server_2.close();
168
+ }
169
+ destroy() {
170
+ var _this$client_3, _this$server_3;
171
+ /**
172
+ * Destroy created sides only.
173
+ * They share the underlying MessageChannel via cache ref-counting, so partial creation is safe.
174
+ */
175
+ (_this$client_3 = this.client_) === null || _this$client_3 === void 0 || _this$client_3.destroy();
176
+ (_this$server_3 = this.server_) === null || _this$server_3 === void 0 || _this$server_3.destroy();
177
+ this.client_ = undefined;
178
+ this.server_ = undefined;
179
+ }
180
+ /** Server fields */
181
+ get secretKey() {
182
+ var _this$options_;
183
+ return (_this$options_ = this.options_) === null || _this$options_ === void 0 ? void 0 : _this$options_.secretKey;
184
+ }
185
+ }
186
+
187
+ /**
188
+ * Create an endpoint facade (client + server) for a peer window/iframe.
189
+ *
190
+ * It can:
191
+ * - send requests to the peer (client)
192
+ * - handle requests from the peer (server)
193
+ */
194
+ export function requestIframeEndpoint(target, options) {
195
+ var _options$id;
196
+ var targetWindow = null;
197
+ var targetOrigin = OriginConstant.ANY;
198
+ if (target.tagName === 'IFRAME') {
199
+ var iframe = target;
200
+ targetWindow = iframe.contentWindow;
201
+ targetOrigin = getIframeTargetOrigin(iframe);
202
+ if (!targetWindow) {
203
+ throw {
204
+ message: Messages.IFRAME_NOT_READY,
205
+ code: ErrorCode.IFRAME_NOT_READY
206
+ };
207
+ }
208
+ } else {
209
+ targetWindow = target;
210
+ targetOrigin = OriginConstant.ANY;
211
+ }
212
+
213
+ /** Allow user to override targetOrigin explicitly */
214
+ if (options !== null && options !== void 0 && options.targetOrigin) {
215
+ targetOrigin = options.targetOrigin;
216
+ }
217
+
218
+ /**
219
+ * Endpoint uses ONE shared id by default, so it behaves like a single endpoint.
220
+ * If options.id is provided, it becomes the shared id for both client+server.
221
+ */
222
+ var endpointId = (_options$id = options === null || options === void 0 ? void 0 : options.id) !== null && _options$id !== void 0 ? _options$id : generateInstanceId();
223
+ return new RequestIframeEndpointApiFacade({
224
+ targetWindow,
225
+ targetOrigin,
226
+ options,
227
+ endpointId
228
+ });
229
+ }
package/esm/api/server.js CHANGED
@@ -1,6 +1,8 @@
1
- import { RequestIframeServerImpl } from '../core/server';
1
+ import { RequestIframeServerImpl } from '../impl/server';
2
2
  import { setupServerDebugListeners } from '../utils/debug';
3
+ import { setRequestIframeLogLevel } from '../utils/logger';
3
4
  import { getCachedServer, cacheServer, clearServerCache } from '../utils/cache';
5
+ import { LogLevel } from '../constants';
4
6
 
5
7
  /**
6
8
  * Create a server (for receiving and handling requests)
@@ -37,9 +39,17 @@ export function requestIframeServer(options) {
37
39
  autoAckMaxIdLength: options === null || options === void 0 ? void 0 : options.autoAckMaxIdLength
38
40
  });
39
41
 
40
- // If trace mode is enabled, register debug listeners
42
+ /**
43
+ * Trace/log level:
44
+ * - default: only warn/error will be printed (logger default)
45
+ * - if trace enabled: raise log level and (optionally) enable detailed debug listeners
46
+ */
41
47
  if (options !== null && options !== void 0 && options.trace) {
42
- setupServerDebugListeners(server);
48
+ var level = options.trace === true ? LogLevel.TRACE : options.trace;
49
+ setRequestIframeLogLevel(level);
50
+ if (level === LogLevel.TRACE || level === LogLevel.INFO) {
51
+ setupServerDebugListeners(server);
52
+ }
43
53
  }
44
54
 
45
55
  // Cache server if id is specified
@@ -53,9 +63,7 @@ export function requestIframeServer(options) {
53
63
  * Clear server cache (for testing or reset)
54
64
  * Note: This clears the cached server instances
55
65
  */
56
- export function clearRequestIframeServerCache(secretKey) {
57
- // Clear server cache
58
- clearServerCache();
59
- // MessageChannel cleanup is handled by clearMessageChannelCache in cache.ts
60
- void secretKey;
66
+ export function clearRequestIframeServerCache(arg) {
67
+ /** Clear server cache */
68
+ clearServerCache(arg);
61
69
  }
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Debug constants (event names for structured logs).
3
+ */
4
+
5
+ export var DebugEvent = {
6
+ CLIENT_REQUEST_START: 'client.request.start',
7
+ CLIENT_REQUEST_SUCCESS: 'client.request.success',
8
+ CLIENT_REQUEST_SUCCESS_FILE: 'client.request.success.file',
9
+ CLIENT_REQUEST_SUCCESS_STREAM: 'client.request.success.stream',
10
+ CLIENT_REQUEST_FAILED: 'client.request.failed',
11
+ CLIENT_MESSAGE_INBOUND: 'client.message.inbound',
12
+ CLIENT_MESSAGE_OUTBOUND: 'client.message.outbound',
13
+ SERVER_REQUEST_RECEIVED: 'server.request.received',
14
+ SERVER_RESPONSE_SEND: 'server.response.send',
15
+ SERVER_MESSAGE_INBOUND: 'server.message.inbound',
16
+ SERVER_MESSAGE_OUTBOUND: 'server.message.outbound'
17
+ };
@@ -1,11 +1,11 @@
1
- /**
2
- * Protocol version constants
3
- * Used to identify the protocol version of messages, for compatibility handling in future version upgrades
4
- *
5
- * Version compatibility strategy:
6
- * - Only check minimum supported version, reject deprecated old versions
7
- * - Don't check maximum version, as new versions usually maintain backward compatibility with old message formats
8
- * - This way new version servers can be compatible with old version clients, without forcing client upgrades
1
+ /**
2
+ * Protocol version constants
3
+ * Used to identify the protocol version of messages, for compatibility handling in future version upgrades
4
+ *
5
+ * Version compatibility strategy:
6
+ * - Only check minimum supported version, reject deprecated old versions
7
+ * - Don't check maximum version, as new versions usually maintain backward compatibility with old message formats
8
+ * - This way new version servers can be compatible with old version clients, without forcing client upgrades
9
9
  */
10
10
  export var ProtocolVersion = {
11
11
  /** Current protocol version */
@@ -14,16 +14,16 @@ export var ProtocolVersion = {
14
14
  MIN_SUPPORTED: 1
15
15
  };
16
16
 
17
- /**
18
- * Special origin values
17
+ /**
18
+ * Special origin values
19
19
  */
20
20
  export var OriginConstant = {
21
21
  /** Wildcard origin (postMessage targetOrigin = '*') */
22
22
  ANY: '*'
23
23
  };
24
24
 
25
- /**
26
- * Auto-ack (ACK-only) limits.
25
+ /**
26
+ * Auto-ack (ACK-only) limits.
27
27
  */
28
28
  export var AutoAckConstant = {
29
29
  /** Max length of ack.id to be echoed back in auto-ack */
@@ -32,16 +32,16 @@ export var AutoAckConstant = {
32
32
  MAX_META_LENGTH: 5120
33
33
  };
34
34
 
35
- /**
36
- * Protocol version type
35
+ /**
36
+ * Protocol version type
37
37
  */
38
38
 
39
- /**
40
- * Protocol validation result
39
+ /**
40
+ * Protocol validation result
41
41
  */
42
42
 
43
- /**
44
- * HTTP status code constants
43
+ /**
44
+ * HTTP status code constants
45
45
  */
46
46
  export var HttpStatus = {
47
47
  OK: 200,
@@ -58,8 +58,8 @@ export var HttpStatus = {
58
58
  SERVICE_UNAVAILABLE: 503
59
59
  };
60
60
 
61
- /**
62
- * HTTP status text constants
61
+ /**
62
+ * HTTP status text constants
63
63
  */
64
64
  export var HttpStatusText = {
65
65
  [HttpStatus.OK]: 'OK',
@@ -76,15 +76,15 @@ export var HttpStatusText = {
76
76
  [HttpStatus.SERVICE_UNAVAILABLE]: 'Service Unavailable'
77
77
  };
78
78
 
79
- /**
80
- * Get status text
79
+ /**
80
+ * Get status text
81
81
  */
82
82
  export function getStatusText(code) {
83
83
  return HttpStatusText[code] || 'Unknown';
84
84
  }
85
85
 
86
- /**
87
- * Error code constants
86
+ /**
87
+ * Error code constants
88
88
  */
89
89
  export var ErrorCode = {
90
90
  /** ACK confirmation timeout */
@@ -114,11 +114,27 @@ export var ErrorCode = {
114
114
  /** Too many concurrent requests (rate limiting) */
115
115
  TOO_MANY_REQUESTS: 'TOO_MANY_REQUESTS',
116
116
  /** Stream start not received in time */
117
- STREAM_START_TIMEOUT: 'STREAM_START_TIMEOUT'
117
+ STREAM_START_TIMEOUT: 'STREAM_START_TIMEOUT',
118
+ /** Stream has already started */
119
+ STREAM_ALREADY_STARTED: 'STREAM_ALREADY_STARTED',
120
+ /** Stream has ended */
121
+ STREAM_ENDED: 'STREAM_ENDED',
122
+ /** Stream idle timeout */
123
+ STREAM_TIMEOUT: 'STREAM_TIMEOUT',
124
+ /** Stream expired */
125
+ STREAM_EXPIRED: 'STREAM_EXPIRED',
126
+ /** Stream read error */
127
+ STREAM_READ_ERROR: 'STREAM_READ_ERROR',
128
+ /** Stream write() is only available when mode is push */
129
+ STREAM_WRITE_ONLY_IN_PUSH_MODE: 'STREAM_WRITE_ONLY_IN_PUSH_MODE',
130
+ /** Stream pending queue overflow */
131
+ STREAM_PENDING_QUEUE_OVERFLOW: 'STREAM_PENDING_QUEUE_OVERFLOW',
132
+ /** Stream pending bytes overflow */
133
+ STREAM_PENDING_BYTES_OVERFLOW: 'STREAM_PENDING_BYTES_OVERFLOW'
118
134
  };
119
135
 
120
- /**
121
- * Message type constants
136
+ /**
137
+ * Message type constants
122
138
  */
123
139
  export var MessageType = {
124
140
  /** Request message */
@@ -146,9 +162,7 @@ export var MessageType = {
146
162
  /** Stream cancel */
147
163
  STREAM_CANCEL: 'stream_cancel',
148
164
  /** Stream pull (receiver requests next chunks) */
149
- STREAM_PULL: 'stream_pull',
150
- /** Stream ack (receiver acknowledges a chunk) */
151
- STREAM_ACK: 'stream_ack'
165
+ STREAM_PULL: 'stream_pull'
152
166
  };
153
167
  export var MessageRole = {
154
168
  /** Server role */
@@ -156,15 +170,15 @@ export var MessageRole = {
156
170
  /** Client role */
157
171
  CLIENT: 'client'
158
172
  };
159
- /**
160
- * Default timeout configuration (milliseconds)
173
+ /**
174
+ * Default timeout configuration (milliseconds)
161
175
  */
162
176
  export var DefaultTimeout = {
163
- /**
164
- * ACK confirmation timeout: 1000ms (1s)
165
- * Used for requireAck confirmation (ACK-only).
166
- * Increased from 500ms to accommodate slower environments or busy browsers where postMessage
167
- * serialization/deserialization may take longer.
177
+ /**
178
+ * ACK confirmation timeout: 1000ms (1s)
179
+ * Used for requireAck confirmation (ACK-only).
180
+ * Increased from 500ms to accommodate slower environments or busy browsers where postMessage
181
+ * serialization/deserialization may take longer.
168
182
  */
169
183
  ACK: 1000,
170
184
  /** Request timeout: 5s */
@@ -173,8 +187,8 @@ export var DefaultTimeout = {
173
187
  ASYNC: 120000
174
188
  };
175
189
 
176
- /**
177
- * HTTP Header name constants
190
+ /**
191
+ * HTTP Header name constants
178
192
  */
179
193
  export var HttpHeader = {
180
194
  /** Set-Cookie (server sets cookie) */
@@ -189,20 +203,20 @@ export var HttpHeader = {
189
203
  COOKIE: 'Cookie'
190
204
  };
191
205
 
192
- /**
193
- * HTTP Header name type
206
+ /**
207
+ * HTTP Header name type
194
208
  */
195
209
 
196
- /**
197
- * Message type union type
210
+ /**
211
+ * Message type union type
198
212
  */
199
213
 
200
- /**
201
- * Error code union type
214
+ /**
215
+ * Error code union type
202
216
  */
203
217
 
204
- /**
205
- * Stream type constants
218
+ /**
219
+ * Stream type constants
206
220
  */
207
221
  export var StreamType = {
208
222
  /** Normal data stream */
@@ -211,18 +225,18 @@ export var StreamType = {
211
225
  FILE: 'file'
212
226
  };
213
227
 
214
- /**
215
- * Stream mode constants
216
- * - PULL: receiver pulls next chunks (backpressure)
217
- * - PUSH: producer pushes via write()
228
+ /**
229
+ * Stream mode constants
230
+ * - PULL: receiver pulls next chunks (backpressure)
231
+ * - PUSH: producer pushes via write()
218
232
  */
219
233
  export var StreamMode = {
220
234
  PULL: 'pull',
221
235
  PUSH: 'push'
222
236
  };
223
- /**
224
- * Stream internal message type constants (for stream internal message handling)
225
- * Note: These are MessageType.STREAM_* values with the stream_ prefix removed
237
+ /**
238
+ * Stream internal message type constants (for stream internal message handling)
239
+ * Note: These are MessageType.STREAM_* values with the stream_ prefix removed
226
240
  */
227
241
  export var StreamInternalMessageType = {
228
242
  /** Data message */
@@ -237,16 +251,16 @@ export var StreamInternalMessageType = {
237
251
  PULL: 'pull'
238
252
  };
239
253
 
240
- /**
241
- * Stream internal message type value type
254
+ /**
255
+ * Stream internal message type value type
242
256
  */
243
257
 
244
- /**
245
- * Stream type value type
258
+ /**
259
+ * Stream type value type
246
260
  */
247
261
 
248
- /**
249
- * Stream state constants
262
+ /**
263
+ * Stream state constants
250
264
  */
251
265
  export var StreamState = {
252
266
  /** Pending */
@@ -261,12 +275,12 @@ export var StreamState = {
261
275
  CANCELLED: 'cancelled'
262
276
  };
263
277
 
264
- /**
265
- * Stream state value type
278
+ /**
279
+ * Stream state value type
266
280
  */
267
281
 
268
- /**
269
- * Stream event name constants (for stream.on / observability)
282
+ /**
283
+ * Stream event name constants (for stream.on / observability)
270
284
  */
271
285
  export var StreamEvent = {
272
286
  START: 'start',
@@ -283,7 +297,10 @@ export var StreamEvent = {
283
297
  EXPIRED: 'expired',
284
298
  STATE: 'state'
285
299
  };
286
- /**
287
- * Message constants (for multi-language support)
300
+ /**
301
+ * Message constants (for multi-language support)
288
302
  */
289
- export { Messages, formatMessage, setMessages, resetMessages, getMessages } from './messages';
303
+ export { Messages, formatMessage, setMessages, resetMessages, getMessages } from './messages';
304
+ export { WarnOnceKey, buildWarnOnceKey } from './warn-once';
305
+ export { LogLevel } from './log';
306
+ export { DebugEvent } from './debug';
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Log level constants for request-iframe.
3
+ */
4
+
5
+ export var LogLevel = {
6
+ TRACE: 'trace',
7
+ INFO: 'info',
8
+ WARN: 'warn',
9
+ ERROR: 'error',
10
+ SILENT: 'silent'
11
+ };
@@ -41,11 +41,14 @@ var defaultMessages = {
41
41
  MIDDLEWARE_ERROR: 'Middleware error',
42
42
  ERROR: 'Error',
43
43
  TOO_MANY_REQUESTS: 'Too many concurrent requests (limit: {0})',
44
+ HUB_REGISTER_HANDLERS_NOT_SET: 'RequestIframeEndpointHub: registerHandlers is not set',
44
45
  /** Client errors */
45
46
  IFRAME_NOT_READY: 'iframe.contentWindow is not available',
46
47
  TARGET_WINDOW_CLOSED: 'Target window is closed or no longer available',
47
48
  /** ClientServer warnings */
48
49
  CLIENT_SERVER_IGNORED_MESSAGE_WHEN_CLOSED: 'Ignored message because client server is closed/destroyed (type: {0}, requestId: {1})',
50
+ /** Server warnings */
51
+ SERVER_IGNORED_MESSAGE_WHEN_CLOSED: 'Ignored message because server is closed/destroyed (type: {0}, requestId: {1})',
49
52
  /** Stream related messages */
50
53
  STREAM_NOT_BOUND: 'Stream is not bound to a request context',
51
54
  STREAM_ALREADY_STARTED: 'Stream has already started',