request-iframe 0.1.0 → 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 (223) hide show
  1. package/QUICKSTART.CN.md +4 -2
  2. package/QUICKSTART.md +4 -2
  3. package/README.CN.md +302 -54
  4. package/README.md +281 -36
  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 +31 -22
  14. package/esm/api/endpoint.js +229 -0
  15. package/esm/api/server.js +19 -9
  16. package/esm/constants/debug.js +17 -0
  17. package/esm/constants/index.js +115 -66
  18. package/esm/constants/log.js +11 -0
  19. package/esm/constants/messages.js +6 -1
  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 +243 -320
  35. package/esm/{core → impl}/response.js +120 -154
  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 +92 -5
  41. package/esm/message/dispatcher.js +149 -98
  42. package/esm/stream/error.js +22 -0
  43. package/esm/stream/index.js +3 -1
  44. package/esm/stream/readable-stream.js +101 -26
  45. package/esm/stream/stream-core.js +121 -3
  46. package/esm/stream/writable-stream.js +368 -43
  47. package/esm/utils/ack.js +36 -0
  48. package/esm/utils/blob.js +16 -0
  49. package/esm/utils/cache.js +25 -76
  50. package/esm/utils/content-type.js +81 -0
  51. package/esm/utils/debug.js +157 -180
  52. package/esm/utils/hooks.js +130 -0
  53. package/esm/utils/id.js +14 -0
  54. package/esm/utils/iframe.js +20 -0
  55. package/esm/utils/index.js +12 -162
  56. package/esm/utils/is.js +3 -0
  57. package/esm/utils/logger.js +55 -0
  58. package/esm/utils/origin.js +3 -1
  59. package/esm/utils/promise.js +3 -0
  60. package/esm/utils/window.js +31 -0
  61. package/library/api/client.d.ts.map +1 -1
  62. package/library/api/client.js +32 -23
  63. package/library/api/endpoint.d.ts +23 -0
  64. package/library/api/endpoint.d.ts.map +1 -0
  65. package/library/api/endpoint.js +235 -0
  66. package/library/api/server.d.ts +4 -1
  67. package/library/api/server.d.ts.map +1 -1
  68. package/library/api/server.js +19 -9
  69. package/library/constants/debug.d.ts +18 -0
  70. package/library/constants/debug.d.ts.map +1 -0
  71. package/library/constants/debug.js +23 -0
  72. package/library/constants/index.d.ts +58 -7
  73. package/library/constants/index.d.ts.map +1 -1
  74. package/library/constants/index.js +143 -67
  75. package/library/constants/log.d.ts +12 -0
  76. package/library/constants/log.d.ts.map +1 -0
  77. package/library/constants/log.js +17 -0
  78. package/library/constants/messages.d.ts +6 -1
  79. package/library/constants/messages.d.ts.map +1 -1
  80. package/library/constants/messages.js +6 -1
  81. package/library/constants/warn-once.d.ts +12 -0
  82. package/library/constants/warn-once.d.ts.map +1 -0
  83. package/library/constants/warn-once.js +22 -0
  84. package/library/endpoint/facade.d.ts +238 -0
  85. package/library/endpoint/facade.d.ts.map +1 -0
  86. package/library/endpoint/facade.js +398 -0
  87. package/library/endpoint/heartbeat/heartbeat.d.ts +34 -0
  88. package/library/endpoint/heartbeat/heartbeat.d.ts.map +1 -0
  89. package/library/endpoint/heartbeat/heartbeat.js +67 -0
  90. package/library/endpoint/heartbeat/ping.d.ts +18 -0
  91. package/library/endpoint/heartbeat/ping.d.ts.map +1 -0
  92. package/library/endpoint/heartbeat/ping.js +26 -0
  93. package/library/endpoint/index.d.ts +16 -0
  94. package/library/endpoint/index.d.ts.map +1 -0
  95. package/library/endpoint/index.js +114 -0
  96. package/library/endpoint/infra/hub.d.ts +170 -0
  97. package/library/endpoint/infra/hub.d.ts.map +1 -0
  98. package/library/endpoint/infra/hub.js +323 -0
  99. package/library/endpoint/infra/inbox.d.ts +73 -0
  100. package/library/endpoint/infra/inbox.d.ts.map +1 -0
  101. package/library/endpoint/infra/inbox.js +239 -0
  102. package/library/endpoint/infra/outbox.d.ts +149 -0
  103. package/library/endpoint/infra/outbox.d.ts.map +1 -0
  104. package/library/endpoint/infra/outbox.js +415 -0
  105. package/library/endpoint/stream/dispatcher.d.ts +33 -0
  106. package/library/endpoint/stream/dispatcher.d.ts.map +1 -0
  107. package/library/endpoint/stream/dispatcher.js +66 -0
  108. package/library/endpoint/stream/errors.d.ts +20 -0
  109. package/library/endpoint/stream/errors.d.ts.map +1 -0
  110. package/library/endpoint/stream/errors.js +32 -0
  111. package/library/endpoint/stream/factory.d.ts +44 -0
  112. package/library/endpoint/stream/factory.d.ts.map +1 -0
  113. package/library/endpoint/stream/factory.js +82 -0
  114. package/library/endpoint/stream/file-auto-resolve.d.ts +26 -0
  115. package/library/endpoint/stream/file-auto-resolve.d.ts.map +1 -0
  116. package/library/endpoint/stream/file-auto-resolve.js +41 -0
  117. package/library/endpoint/stream/file-writable.d.ts +33 -0
  118. package/library/endpoint/stream/file-writable.d.ts.map +1 -0
  119. package/library/endpoint/stream/file-writable.js +115 -0
  120. package/library/endpoint/stream/handler.d.ts +20 -0
  121. package/library/endpoint/stream/handler.d.ts.map +1 -0
  122. package/library/endpoint/stream/handler.js +32 -0
  123. package/library/{core → impl}/client.d.ts +16 -13
  124. package/library/impl/client.d.ts.map +1 -0
  125. package/library/{core → impl}/client.js +254 -333
  126. package/library/{core → impl}/request.d.ts.map +1 -1
  127. package/library/{core → impl}/response.d.ts +7 -12
  128. package/library/impl/response.d.ts.map +1 -0
  129. package/library/{core → impl}/response.js +120 -154
  130. package/library/{core → impl}/server.d.ts +26 -55
  131. package/library/impl/server.d.ts.map +1 -0
  132. package/library/impl/server.js +575 -0
  133. package/library/index.d.ts +13 -6
  134. package/library/index.d.ts.map +1 -1
  135. package/library/index.js +16 -16
  136. package/library/message/ack.d.ts +15 -0
  137. package/library/message/ack.d.ts.map +1 -0
  138. package/library/message/ack.js +33 -0
  139. package/library/message/channel-cache.d.ts +26 -0
  140. package/library/message/channel-cache.d.ts.map +1 -0
  141. package/library/message/channel-cache.js +115 -0
  142. package/library/message/channel.d.ts +53 -6
  143. package/library/message/channel.d.ts.map +1 -1
  144. package/library/message/channel.js +96 -9
  145. package/library/message/dispatcher.d.ts +17 -0
  146. package/library/message/dispatcher.d.ts.map +1 -1
  147. package/library/message/dispatcher.js +149 -98
  148. package/library/stream/error.d.ts +24 -0
  149. package/library/stream/error.d.ts.map +1 -0
  150. package/library/stream/error.js +29 -0
  151. package/library/stream/index.d.ts +4 -1
  152. package/library/stream/index.d.ts.map +1 -1
  153. package/library/stream/index.js +7 -4
  154. package/library/stream/readable-stream.d.ts.map +1 -1
  155. package/library/stream/readable-stream.js +102 -27
  156. package/library/stream/stream-core.d.ts +22 -1
  157. package/library/stream/stream-core.d.ts.map +1 -1
  158. package/library/stream/stream-core.js +120 -2
  159. package/library/stream/types.d.ts +115 -2
  160. package/library/stream/types.d.ts.map +1 -1
  161. package/library/stream/writable-stream.d.ts +20 -2
  162. package/library/stream/writable-stream.d.ts.map +1 -1
  163. package/library/stream/writable-stream.js +366 -41
  164. package/library/types/index.d.ts +17 -22
  165. package/library/types/index.d.ts.map +1 -1
  166. package/library/utils/ack.d.ts +2 -0
  167. package/library/utils/ack.d.ts.map +1 -0
  168. package/library/utils/ack.js +44 -0
  169. package/library/utils/blob.d.ts +3 -0
  170. package/library/utils/blob.d.ts.map +1 -0
  171. package/library/utils/blob.js +22 -0
  172. package/library/utils/cache.d.ts +10 -20
  173. package/library/utils/cache.d.ts.map +1 -1
  174. package/library/utils/cache.js +25 -79
  175. package/library/utils/content-type.d.ts +13 -0
  176. package/library/utils/content-type.d.ts.map +1 -0
  177. package/library/utils/content-type.js +87 -0
  178. package/library/utils/debug.d.ts.map +1 -1
  179. package/library/utils/debug.js +156 -178
  180. package/library/utils/hooks.d.ts +30 -0
  181. package/library/utils/hooks.d.ts.map +1 -0
  182. package/library/utils/hooks.js +139 -0
  183. package/library/utils/id.d.ts +9 -0
  184. package/library/utils/id.d.ts.map +1 -0
  185. package/library/utils/id.js +21 -0
  186. package/library/utils/iframe.d.ts +5 -0
  187. package/library/utils/iframe.d.ts.map +1 -0
  188. package/library/utils/iframe.js +25 -0
  189. package/library/utils/index.d.ts +7 -34
  190. package/library/utils/index.d.ts.map +1 -1
  191. package/library/utils/index.js +58 -193
  192. package/library/utils/is.d.ts +2 -0
  193. package/library/utils/is.d.ts.map +1 -0
  194. package/library/utils/is.js +9 -0
  195. package/library/utils/logger.d.ts +13 -0
  196. package/library/utils/logger.d.ts.map +1 -0
  197. package/library/utils/logger.js +63 -0
  198. package/library/utils/origin.d.ts.map +1 -1
  199. package/library/utils/origin.js +2 -1
  200. package/library/utils/promise.d.ts +2 -0
  201. package/library/utils/promise.d.ts.map +1 -0
  202. package/library/utils/promise.js +9 -0
  203. package/library/utils/window.d.ts +2 -0
  204. package/library/utils/window.d.ts.map +1 -0
  205. package/library/utils/window.js +38 -0
  206. package/package.json +49 -2
  207. package/react/package.json +2 -1
  208. package/esm/core/client-server.js +0 -329
  209. package/esm/core/server.js +0 -767
  210. package/esm/utils/ack-meta.js +0 -53
  211. package/library/core/client-server.d.ts +0 -106
  212. package/library/core/client-server.d.ts.map +0 -1
  213. package/library/core/client-server.js +0 -336
  214. package/library/core/client.d.ts.map +0 -1
  215. package/library/core/response.d.ts.map +0 -1
  216. package/library/core/server.d.ts.map +0 -1
  217. package/library/core/server.js +0 -772
  218. package/library/utils/ack-meta.d.ts +0 -2
  219. package/library/utils/ack-meta.d.ts.map +0 -1
  220. package/library/utils/ack-meta.js +0 -59
  221. /package/esm/{core → impl}/request.js +0 -0
  222. /package/library/{core → impl}/request.d.ts +0 -0
  223. /package/library/{core → impl}/request.js +0 -0
@@ -1,85 +1,15 @@
1
1
  import "core-js/modules/es.array.iterator.js";
2
2
  import "core-js/modules/es.map.js";
3
+ import "core-js/modules/es.string.ends-with.js";
4
+ import "core-js/modules/es.string.starts-with.js";
3
5
  import "core-js/modules/web.dom-collections.for-each.js";
4
6
  import "core-js/modules/web.dom-collections.iterator.js";
5
- import { MessageChannel, ChannelType } from '../message';
6
- /**
7
- * Global cache Symbol (used to store MessageChannel instance cache on window)
8
- * Using Symbol.for() ensures multiple library copies share the same cache, avoiding multiple instance creation
9
- */
10
- var MESSAGE_CHANNEL_CACHE_SYMBOL = Symbol.for('__requestIframeMessageChannelCache__');
11
-
12
7
  /**
13
8
  * Global cache Symbol for server instances
14
9
  * Using Symbol.for() ensures multiple library copies share the same cache
15
10
  */
16
11
  var SERVER_CACHE_SYMBOL = Symbol.for('__requestIframeServerCache__');
17
12
 
18
- /**
19
- * Get the MessageChannel cache from window
20
- */
21
- function getChannelCacheForWindow(win) {
22
- if (!(MESSAGE_CHANNEL_CACHE_SYMBOL in win)) {
23
- win[MESSAGE_CHANNEL_CACHE_SYMBOL] = new Map();
24
- }
25
- return win[MESSAGE_CHANNEL_CACHE_SYMBOL];
26
- }
27
-
28
- /**
29
- * Generate cache key
30
- * Cache key format: "type:secretKey" or "type:" when no secretKey
31
- * Different channel types use separate cache entries
32
- */
33
- function getCacheKey(type, secretKey) {
34
- return `${type}:${secretKey !== null && secretKey !== void 0 ? secretKey : ''}`;
35
- }
36
-
37
- /**
38
- * Get or create MessageChannel instance
39
- * - Within the same window, only one channel is created per type + secretKey combination
40
- * - Uses reference counting to manage lifecycle
41
- * @param secretKey secret key for message isolation
42
- * @param type channel type (defaults to postMessage)
43
- */
44
- export function getOrCreateMessageChannel(secretKey, type = ChannelType.POST_MESSAGE) {
45
- var cache = getChannelCacheForWindow(window);
46
- var key = getCacheKey(type, secretKey);
47
- var channel = cache.get(key);
48
- if (!channel) {
49
- channel = new MessageChannel(secretKey, type);
50
- cache.set(key, channel);
51
- }
52
- channel.addRef();
53
- return channel;
54
- }
55
-
56
- /**
57
- * Release MessageChannel reference
58
- * - When reference count reaches 0, destroy channel and remove from cache
59
- */
60
- export function releaseMessageChannel(channel) {
61
- var refCount = channel.release();
62
- if (refCount <= 0) {
63
- var cache = getChannelCacheForWindow(window);
64
- var key = getCacheKey(channel.type, channel.secretKey);
65
- if (cache.get(key) === channel) {
66
- cache.delete(key);
67
- channel.destroy();
68
- }
69
- }
70
- }
71
-
72
- /**
73
- * Clear all MessageChannel cache (mainly for testing)
74
- */
75
- export function clearMessageChannelCache() {
76
- var cache = getChannelCacheForWindow(window);
77
- cache.forEach(channel => {
78
- channel.destroy();
79
- });
80
- cache.clear();
81
- }
82
-
83
13
  /**
84
14
  * Get the server cache from window
85
15
  */
@@ -136,12 +66,31 @@ export function removeCachedServer(secretKey, id) {
136
66
  }
137
67
 
138
68
  /**
139
- * Clear all server cache (mainly for testing)
69
+ * Clear server cache (mainly for testing).
70
+ *
71
+ * - No args: clear all cached servers
72
+ * - With secretKey: only clear servers under that secretKey
73
+ * - With { secretKey, id }: only clear the specified server instance
74
+ * - With { id }: clear the specified id across all secretKeys
140
75
  */
141
- export function clearServerCache() {
76
+ export function clearServerCache(arg) {
142
77
  var cache = getServerCacheForWindow(window);
143
- cache.forEach(server => {
78
+ if (!arg) {
79
+ cache.forEach(server => {
80
+ server.destroy();
81
+ });
82
+ cache.clear();
83
+ return;
84
+ }
85
+ var params = typeof arg === 'string' ? {
86
+ secretKey: arg
87
+ } : arg;
88
+ var prefix = typeof params.secretKey === 'string' ? `${params.secretKey}:` : undefined;
89
+ var suffix = typeof params.id === 'string' ? `:${params.id}` : undefined;
90
+ cache.forEach((server, key) => {
91
+ if (prefix && !key.startsWith(prefix)) return;
92
+ if (suffix && !key.endsWith(suffix)) return;
93
+ cache.delete(key);
144
94
  server.destroy();
145
95
  });
146
- cache.clear();
147
96
  }
@@ -0,0 +1,81 @@
1
+ import "core-js/modules/es.array.iterator.js";
2
+ import "core-js/modules/web.dom-collections.iterator.js";
3
+ import "core-js/modules/web.url-search-params.js";
4
+ /**
5
+ * Detect Content-Type based on data type
6
+ * @param data The data to detect Content-Type for
7
+ * @param options Options for detection
8
+ * @param options.checkStream Whether to check for IframeWritableStream (default: false)
9
+ * @param options.isIframeWritableStream Optional function to check if data is a stream (required if checkStream is true)
10
+ * @returns The detected Content-Type, or null if Content-Type should not be auto-set
11
+ */
12
+ export function detectContentType(data, options) {
13
+ if (data === null || data === undefined) return null;
14
+ var _ref = options || {},
15
+ _ref$checkStream = _ref.checkStream,
16
+ checkStream = _ref$checkStream === void 0 ? false : _ref$checkStream,
17
+ isIframeWritableStream = _ref.isIframeWritableStream;
18
+
19
+ /** Stream - handled separately (only for response) */
20
+ if (checkStream && isIframeWritableStream) {
21
+ if (isIframeWritableStream(data)) {
22
+ return null; /** Stream will be handled by sendStream */
23
+ }
24
+ }
25
+
26
+ /** File */
27
+ if (typeof File !== 'undefined' && data instanceof File) {
28
+ return data.type || 'application/octet-stream';
29
+ }
30
+
31
+ /** Blob */
32
+ if (typeof Blob !== 'undefined' && data instanceof Blob) {
33
+ return data.type || 'application/octet-stream';
34
+ }
35
+
36
+ /** ArrayBuffer */
37
+ if (typeof ArrayBuffer !== 'undefined' && data instanceof ArrayBuffer) {
38
+ return 'application/octet-stream';
39
+ }
40
+
41
+ /** FormData */
42
+ if (typeof FormData !== 'undefined' && data instanceof FormData) {
43
+ /** FormData typically doesn't need Content-Type header (browser sets it with boundary) */
44
+ return null;
45
+ }
46
+
47
+ /** URLSearchParams */
48
+ if (typeof URLSearchParams !== 'undefined' && data instanceof URLSearchParams) {
49
+ return 'application/x-www-form-urlencoded';
50
+ }
51
+
52
+ /** String - check if it's JSON string */
53
+ if (typeof data === 'string') {
54
+ /** Try to parse as JSON, if successful, treat as JSON */
55
+ try {
56
+ JSON.parse(data);
57
+ return 'application/json';
58
+ } catch (_unused) {
59
+ return 'text/plain; charset=utf-8';
60
+ }
61
+ }
62
+
63
+ /** Number, boolean - treat as JSON */
64
+ if (typeof data === 'number' || typeof data === 'boolean') {
65
+ return 'application/json';
66
+ }
67
+
68
+ /** Plain object or array - treat as JSON */
69
+ if (typeof data === 'object') {
70
+ /**
71
+ * Exclude common binary/file types (already checked above, but double-check for safety)
72
+ */
73
+ if (typeof Blob !== 'undefined' && data instanceof Blob) return null;
74
+ if (typeof File !== 'undefined' && data instanceof File) return null;
75
+ if (typeof ArrayBuffer !== 'undefined' && data instanceof ArrayBuffer) return null;
76
+ if (typeof FormData !== 'undefined' && data instanceof FormData) return null;
77
+ if (typeof URLSearchParams !== 'undefined' && data instanceof URLSearchParams) return null;
78
+ return 'application/json';
79
+ }
80
+ return null;
81
+ }