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
@@ -13,9 +13,12 @@ import "core-js/modules/es.object.get-own-property-descriptors.js";
13
13
  import "core-js/modules/es.regexp.to-string.js";
14
14
  import "core-js/modules/web.dom-collections.for-each.js";
15
15
  import "core-js/modules/web.dom-collections.iterator.js";
16
- import { createPostMessage, generateRequestId } from '../utils';
17
- import { MessageType, Messages, StreamType as StreamTypeConstant, StreamState as StreamStateConstant, StreamMode as StreamModeConstant, MessageRole, formatMessage, DefaultTimeout, StreamInternalMessageType, StreamEvent } from '../constants';
16
+ import { createPostMessage } from '../utils/protocol';
17
+ import { generateRequestId } from '../utils/id';
18
+ import { isFunction } from '../utils/is';
19
+ import { MessageType, Messages, StreamType as StreamTypeConstant, StreamState as StreamStateConstant, StreamMode as StreamModeConstant, MessageRole, formatMessage, DefaultTimeout, StreamInternalMessageType, StreamEvent, ErrorCode } from '../constants';
18
20
  import { IframeStreamCore } from './stream-core';
21
+ import { RequestIframeStreamError } from './error';
19
22
 
20
23
  /**
21
24
  * Generate a unique stream ID
@@ -68,14 +71,26 @@ export class IframeWritableStream extends IframeStreamCore {
68
71
  enqueue(item) {
69
72
  var max = this.maxPendingChunks;
70
73
  if (typeof max === 'number' && max > 0 && this.pendingQueue.length >= max) {
71
- throw new Error(formatMessage(Messages.STREAM_PENDING_QUEUE_OVERFLOW, max));
74
+ var _this$context;
75
+ throw new RequestIframeStreamError({
76
+ message: formatMessage(Messages.STREAM_PENDING_QUEUE_OVERFLOW, max),
77
+ code: ErrorCode.STREAM_PENDING_QUEUE_OVERFLOW,
78
+ streamId: this.streamId,
79
+ requestId: (_this$context = this.context) === null || _this$context === void 0 ? void 0 : _this$context.requestId
80
+ });
72
81
  }
73
82
  var bytes = this.estimateChunkBytes(item.data);
74
83
  var maxBytes = this.maxPendingBytes;
75
84
  if (typeof maxBytes === 'number' && maxBytes > 0) {
76
85
  var next = this.pendingBytes + bytes;
77
86
  if (!Number.isFinite(next) || next > maxBytes) {
78
- throw new Error(formatMessage(Messages.STREAM_PENDING_BYTES_OVERFLOW, maxBytes));
87
+ var _this$context2;
88
+ throw new RequestIframeStreamError({
89
+ message: formatMessage(Messages.STREAM_PENDING_BYTES_OVERFLOW, maxBytes),
90
+ code: ErrorCode.STREAM_PENDING_BYTES_OVERFLOW,
91
+ streamId: this.streamId,
92
+ requestId: (_this$context2 = this.context) === null || _this$context2 === void 0 ? void 0 : _this$context2.requestId
93
+ });
79
94
  }
80
95
  }
81
96
  this.pendingQueue.push(_objectSpread(_objectSpread({}, item), {}, {
@@ -143,13 +158,13 @@ export class IframeWritableStream extends IframeStreamCore {
143
158
  this.context = context;
144
159
  }
145
160
  registerControlHandler() {
146
- var _this$context;
147
- if (!((_this$context = this.context) !== null && _this$context !== void 0 && _this$context.registerStreamHandler)) return;
161
+ var _this$context3;
162
+ if (!((_this$context3 = this.context) !== null && _this$context3 !== void 0 && _this$context3.registerStreamHandler)) return;
148
163
  this.context.registerStreamHandler(this.streamId, this.handleControlMessage.bind(this));
149
164
  }
150
165
  unregisterControlHandler() {
151
- var _this$context2;
152
- if (!((_this$context2 = this.context) !== null && _this$context2 !== void 0 && _this$context2.unregisterStreamHandler)) return;
166
+ var _this$context4;
167
+ if (!((_this$context4 = this.context) !== null && _this$context4 !== void 0 && _this$context4.unregisterStreamHandler)) return;
153
168
  this.context.unregisterStreamHandler(this.streamId);
154
169
  }
155
170
  handleControlMessage(data) {
@@ -188,11 +203,11 @@ export class IframeWritableStream extends IframeStreamCore {
188
203
  performHeartbeat() {
189
204
  var _this = this;
190
205
  return _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
191
- var _this$context3;
206
+ var _this$context5;
192
207
  return _regeneratorRuntime.wrap(function (_context) {
193
208
  while (1) switch (_context.prev = _context.next) {
194
209
  case 0:
195
- if ((_this$context3 = _this.context) !== null && _this$context3 !== void 0 && _this$context3.heartbeat) {
210
+ if ((_this$context5 = _this.context) !== null && _this$context5 !== void 0 && _this$context5.heartbeat) {
196
211
  _context.next = 1;
197
212
  break;
198
213
  }
@@ -268,7 +283,11 @@ export class IframeWritableStream extends IframeStreamCore {
268
283
  sendMessage(type, data) {
269
284
  var _this$context$serverI;
270
285
  if (!this.context) {
271
- throw new Error(Messages.STREAM_NOT_BOUND);
286
+ throw new RequestIframeStreamError({
287
+ message: Messages.STREAM_NOT_BOUND,
288
+ code: ErrorCode.STREAM_NOT_BOUND,
289
+ streamId: this.streamId
290
+ });
272
291
  }
273
292
  var isClientStream = this.context.clientId !== undefined && this.context.serverId === undefined;
274
293
  var role = isClientStream ? MessageRole.CLIENT : MessageRole.SERVER;
@@ -288,7 +307,12 @@ export class IframeWritableStream extends IframeStreamCore {
288
307
  this.clearExpireTimer();
289
308
  // For most stream messages, if we cannot send, treat as a hard cancellation signal
290
309
  // so callers can stop further processing immediately.
291
- throw new Error(formatMessage(Messages.STREAM_CANCELLED, 'Target window closed'));
310
+ throw new RequestIframeStreamError({
311
+ message: formatMessage(Messages.STREAM_CANCELLED, Messages.TARGET_WINDOW_CLOSED),
312
+ code: ErrorCode.TARGET_WINDOW_CLOSED,
313
+ streamId: this.streamId,
314
+ requestId: this.context.requestId
315
+ });
292
316
  }
293
317
  return true;
294
318
  }
@@ -296,13 +320,13 @@ export class IframeWritableStream extends IframeStreamCore {
296
320
  if (this.ackReceiverRegistered) return;
297
321
  if (!this.context) return;
298
322
  var ch = this.context.channel;
299
- if (typeof ch.addReceiver !== 'function' || typeof ch.removeReceiver !== 'function') return;
323
+ if (!isFunction(ch.addReceiver) || !isFunction(ch.removeReceiver)) return;
300
324
  this.ackReceiver = (data, context) => {
301
325
  if (!data || data.type !== MessageType.ACK) return;
302
326
  var pending = this.ackWaiters.get(data.requestId);
303
327
  if (!pending) return;
304
328
  if (context && !context.handledBy) {
305
- context.handledBy = `stream:${this.streamId}`;
329
+ context.markHandledBy(`stream:${this.streamId}`);
306
330
  }
307
331
  clearTimeout(pending.timeoutId);
308
332
  this.ackWaiters.delete(data.requestId);
@@ -319,7 +343,7 @@ export class IframeWritableStream extends IframeStreamCore {
319
343
  this.ackWaiters.clear();
320
344
  if (this.ackReceiverRegistered && this.ackReceiver && this.context) {
321
345
  var ch = this.context.channel;
322
- if (typeof ch.removeReceiver === 'function') {
346
+ if (isFunction(ch.removeReceiver)) {
323
347
  try {
324
348
  ch.removeReceiver(this.ackReceiver);
325
349
  } catch (_unused6) {
@@ -377,13 +401,22 @@ export class IframeWritableStream extends IframeStreamCore {
377
401
  _context3.next = 1;
378
402
  break;
379
403
  }
380
- throw new Error(Messages.STREAM_NOT_BOUND);
404
+ throw new RequestIframeStreamError({
405
+ message: Messages.STREAM_NOT_BOUND,
406
+ code: ErrorCode.STREAM_NOT_BOUND,
407
+ streamId: _this3.streamId
408
+ });
381
409
  case 1:
382
410
  if (!(_this3._state !== StreamStateConstant.PENDING)) {
383
411
  _context3.next = 2;
384
412
  break;
385
413
  }
386
- throw new Error(Messages.STREAM_ALREADY_STARTED);
414
+ throw new RequestIframeStreamError({
415
+ message: Messages.STREAM_ALREADY_STARTED,
416
+ code: ErrorCode.STREAM_ALREADY_STARTED,
417
+ streamId: _this3.streamId,
418
+ requestId: _this3.context.requestId
419
+ });
387
420
  case 2:
388
421
  _this3.completionPromise = new Promise((resolve, reject) => {
389
422
  _this3.resolveCompletion = resolve;
@@ -456,16 +489,34 @@ export class IframeWritableStream extends IframeStreamCore {
456
489
  write(data, doneOrOptions = false, options) {
457
490
  var _opts$ackTimeout;
458
491
  if (this.mode !== StreamModeConstant.PUSH) {
459
- throw new Error(Messages.STREAM_WRITE_ONLY_IN_PUSH_MODE);
492
+ var _this$context6;
493
+ throw new RequestIframeStreamError({
494
+ message: Messages.STREAM_WRITE_ONLY_IN_PUSH_MODE,
495
+ code: ErrorCode.STREAM_WRITE_ONLY_IN_PUSH_MODE,
496
+ streamId: this.streamId,
497
+ requestId: (_this$context6 = this.context) === null || _this$context6 === void 0 ? void 0 : _this$context6.requestId
498
+ });
460
499
  }
461
500
  if (this._state === StreamStateConstant.PENDING) {
501
+ var _this$context7;
462
502
  /**
463
503
  * In push mode, users must call start() first so STREAM_START is sent and binding is complete.
464
504
  */
465
- throw new Error(Messages.STREAM_NOT_BOUND);
505
+ throw new RequestIframeStreamError({
506
+ message: Messages.STREAM_NOT_BOUND,
507
+ code: ErrorCode.STREAM_NOT_BOUND,
508
+ streamId: this.streamId,
509
+ requestId: (_this$context7 = this.context) === null || _this$context7 === void 0 ? void 0 : _this$context7.requestId
510
+ });
466
511
  }
467
512
  if (this._state !== StreamStateConstant.STREAMING) {
468
- throw new Error(Messages.STREAM_ENDED);
513
+ var _this$context8;
514
+ throw new RequestIframeStreamError({
515
+ message: Messages.STREAM_ENDED,
516
+ code: ErrorCode.STREAM_ENDED,
517
+ streamId: this.streamId,
518
+ requestId: (_this$context8 = this.context) === null || _this$context8 === void 0 ? void 0 : _this$context8.requestId
519
+ });
469
520
  }
470
521
  var done = typeof doneOrOptions === 'boolean' ? doneOrOptions : false;
471
522
  var opts = typeof doneOrOptions === 'object' ? doneOrOptions : options;
@@ -652,7 +703,11 @@ export class IframeWritableStream extends IframeStreamCore {
652
703
  sendData(data, done = false, options) {
653
704
  var _this$context$serverI2, _options$requestId;
654
705
  if (!this.context) {
655
- throw new Error(Messages.STREAM_NOT_BOUND);
706
+ throw new RequestIframeStreamError({
707
+ message: Messages.STREAM_NOT_BOUND,
708
+ code: ErrorCode.STREAM_NOT_BOUND,
709
+ streamId: this.streamId
710
+ });
656
711
  }
657
712
  var seq = this.seq++;
658
713
  var isClientStream = this.context.clientId !== undefined && this.context.serverId === undefined;
@@ -684,7 +739,12 @@ export class IframeWritableStream extends IframeStreamCore {
684
739
  if (!ok) {
685
740
  this._state = StreamStateConstant.CANCELLED;
686
741
  this.clearExpireTimer();
687
- throw new Error(formatMessage(Messages.STREAM_CANCELLED, 'Target window closed'));
742
+ throw new RequestIframeStreamError({
743
+ message: formatMessage(Messages.STREAM_CANCELLED, Messages.TARGET_WINDOW_CLOSED),
744
+ code: ErrorCode.TARGET_WINDOW_CLOSED,
745
+ streamId: this.streamId,
746
+ requestId: this.context.requestId
747
+ });
688
748
  }
689
749
  this.emit(StreamEvent.SEND, {
690
750
  seq,
@@ -720,7 +780,7 @@ export class IframeWritableStream extends IframeStreamCore {
720
780
  this.sendData(item.data, item.done);
721
781
  }
722
782
  } catch (e) {
723
- var _this$rejectCompletio;
783
+ var _this$rejectCompletio, _this$context9;
724
784
  // send failure treated as cancellation
725
785
  this._state = StreamStateConstant.CANCELLED;
726
786
  this.clearExpireTimer();
@@ -729,7 +789,13 @@ export class IframeWritableStream extends IframeStreamCore {
729
789
  this.pendingQueue.length = 0;
730
790
  this.pendingBytes = 0;
731
791
  this.cleanupAckWaiters();
732
- (_this$rejectCompletio = this.rejectCompletion) === null || _this$rejectCompletio === void 0 || _this$rejectCompletio.call(this, e instanceof Error ? e : new Error(String(e)));
792
+ (_this$rejectCompletio = this.rejectCompletion) === null || _this$rejectCompletio === void 0 || _this$rejectCompletio.call(this, e instanceof Error ? e : new RequestIframeStreamError({
793
+ message: String(e),
794
+ code: ErrorCode.STREAM_ERROR,
795
+ streamId: this.streamId,
796
+ requestId: (_this$context9 = this.context) === null || _this$context9 === void 0 ? void 0 : _this$context9.requestId,
797
+ cause: e
798
+ }));
733
799
  throw e;
734
800
  }
735
801
  if (item.done) {
@@ -772,7 +838,7 @@ export class IframeWritableStream extends IframeStreamCore {
772
838
  * Send error
773
839
  */
774
840
  error(message) {
775
- var _this$resolveCompleti2;
841
+ var _this$context0, _this$resolveCompleti2;
776
842
  if (this._state !== StreamStateConstant.STREAMING) return;
777
843
  this._state = StreamStateConstant.ERROR;
778
844
  this.clearExpireTimer();
@@ -784,7 +850,12 @@ export class IframeWritableStream extends IframeStreamCore {
784
850
  error: message
785
851
  });
786
852
  this.emit(StreamEvent.ERROR, {
787
- error: new Error(message)
853
+ error: new RequestIframeStreamError({
854
+ message,
855
+ code: ErrorCode.STREAM_ERROR,
856
+ streamId: this.streamId,
857
+ requestId: (_this$context0 = this.context) === null || _this$context0 === void 0 ? void 0 : _this$context0.requestId
858
+ })
788
859
  });
789
860
  this.emit(StreamEvent.STATE, {
790
861
  state: this._state
@@ -0,0 +1,16 @@
1
+ import "core-js/modules/es.array.includes.js";
2
+ import "core-js/modules/es.promise.js";
3
+ import "core-js/modules/es.string.includes.js";
4
+ /** Convert Blob to base64 string */
5
+ export function blobToBase64(blob) {
6
+ return new Promise((resolve, reject) => {
7
+ var reader = new FileReader();
8
+ reader.onloadend = () => {
9
+ var result = reader.result;
10
+ var base64 = result.includes(',') ? result.split(',')[1] : result;
11
+ resolve(base64);
12
+ };
13
+ reader.onerror = reject;
14
+ reader.readAsDataURL(blob);
15
+ });
16
+ }
@@ -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
+ }