request-iframe 0.0.6 → 0.1.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 (93) hide show
  1. package/README.CN.md +53 -6
  2. package/README.md +63 -10
  3. package/esm/api/client.js +79 -0
  4. package/esm/api/server.js +59 -0
  5. package/esm/constants/index.js +257 -0
  6. package/esm/constants/messages.js +155 -0
  7. package/esm/core/client-server.js +329 -0
  8. package/esm/core/client.js +873 -0
  9. package/esm/core/request.js +27 -0
  10. package/esm/core/response.js +451 -0
  11. package/esm/core/server.js +767 -0
  12. package/esm/index.js +21 -0
  13. package/esm/interceptors/index.js +122 -0
  14. package/esm/message/channel.js +181 -0
  15. package/esm/message/dispatcher.js +380 -0
  16. package/esm/message/index.js +2 -0
  17. package/esm/stream/file-stream.js +289 -0
  18. package/esm/stream/index.js +44 -0
  19. package/esm/stream/readable-stream.js +500 -0
  20. package/esm/stream/stream-core.js +91 -0
  21. package/esm/stream/types.js +1 -0
  22. package/esm/stream/writable-stream.js +582 -0
  23. package/esm/types/index.js +1 -0
  24. package/esm/utils/ack-meta.js +53 -0
  25. package/esm/utils/cache.js +147 -0
  26. package/esm/utils/cookie.js +352 -0
  27. package/esm/utils/debug.js +521 -0
  28. package/esm/utils/error.js +27 -0
  29. package/esm/utils/index.js +178 -0
  30. package/esm/utils/origin.js +28 -0
  31. package/esm/utils/path-match.js +148 -0
  32. package/esm/utils/protocol.js +157 -0
  33. package/library/api/client.d.ts.map +1 -1
  34. package/library/api/client.js +8 -1
  35. package/library/api/server.d.ts.map +1 -1
  36. package/library/api/server.js +4 -1
  37. package/library/constants/index.d.ts +25 -1
  38. package/library/constants/index.d.ts.map +1 -1
  39. package/library/constants/index.js +30 -5
  40. package/library/constants/messages.d.ts +5 -0
  41. package/library/constants/messages.d.ts.map +1 -1
  42. package/library/constants/messages.js +5 -0
  43. package/library/core/client-server.d.ts +3 -2
  44. package/library/core/client-server.d.ts.map +1 -1
  45. package/library/core/client-server.js +51 -4
  46. package/library/core/client.d.ts +4 -1
  47. package/library/core/client.d.ts.map +1 -1
  48. package/library/core/client.js +74 -31
  49. package/library/core/response.d.ts +21 -3
  50. package/library/core/response.d.ts.map +1 -1
  51. package/library/core/response.js +46 -6
  52. package/library/core/server.d.ts +28 -1
  53. package/library/core/server.d.ts.map +1 -1
  54. package/library/core/server.js +180 -19
  55. package/library/message/channel.d.ts +6 -0
  56. package/library/message/channel.d.ts.map +1 -1
  57. package/library/message/dispatcher.d.ts +22 -0
  58. package/library/message/dispatcher.d.ts.map +1 -1
  59. package/library/message/dispatcher.js +92 -0
  60. package/library/stream/file-stream.d.ts +4 -0
  61. package/library/stream/file-stream.d.ts.map +1 -1
  62. package/library/stream/file-stream.js +61 -33
  63. package/library/stream/index.d.ts.map +1 -1
  64. package/library/stream/index.js +2 -0
  65. package/library/stream/readable-stream.d.ts +30 -11
  66. package/library/stream/readable-stream.d.ts.map +1 -1
  67. package/library/stream/readable-stream.js +329 -73
  68. package/library/stream/stream-core.d.ts +44 -0
  69. package/library/stream/stream-core.d.ts.map +1 -0
  70. package/library/stream/stream-core.js +98 -0
  71. package/library/stream/types.d.ts +90 -3
  72. package/library/stream/types.d.ts.map +1 -1
  73. package/library/stream/writable-stream.d.ts +40 -12
  74. package/library/stream/writable-stream.d.ts.map +1 -1
  75. package/library/stream/writable-stream.js +391 -195
  76. package/library/types/index.d.ts +70 -3
  77. package/library/types/index.d.ts.map +1 -1
  78. package/library/utils/ack-meta.d.ts +2 -0
  79. package/library/utils/ack-meta.d.ts.map +1 -0
  80. package/library/utils/ack-meta.js +59 -0
  81. package/library/utils/index.d.ts +1 -0
  82. package/library/utils/index.d.ts.map +1 -1
  83. package/library/utils/index.js +16 -0
  84. package/library/utils/origin.d.ts +14 -0
  85. package/library/utils/origin.d.ts.map +1 -0
  86. package/library/utils/origin.js +34 -0
  87. package/package.json +30 -7
  88. package/react/README.md +16 -0
  89. package/react/esm/index.js +284 -0
  90. package/react/library/index.d.ts +1 -1
  91. package/react/library/index.d.ts.map +1 -1
  92. package/react/library/index.js +3 -3
  93. package/react/package.json +24 -2
@@ -0,0 +1,329 @@
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, DefaultTimeout, ProtocolVersion, Messages, formatMessage, MessageRole } from '../constants';
12
+
13
+ /**
14
+ * Stream message handler callback
15
+ */
16
+
17
+ /**
18
+ * Pending acknowledgment response
19
+ */
20
+
21
+ /**
22
+ * Pending request awaiting response
23
+ */
24
+
25
+ /**
26
+ * ClientServer configuration options
27
+ */
28
+
29
+ /**
30
+ * RequestIframeClientServer - Client-side message server
31
+ * Only handles responses, not requests
32
+ * Uses shared MessageDispatcher (backed by MessageChannel) to listen for and send messages
33
+ */
34
+ export class RequestIframeClientServer {
35
+ constructor(options, instanceId) {
36
+ var _options$ackTimeout, _options$versionValid;
37
+ /** Pending responses awaiting client acknowledgment */
38
+ _defineProperty(this, "pendingAcks", new Map());
39
+ /** Pending requests awaiting response */
40
+ _defineProperty(this, "pendingRequests", new Map());
41
+ /**
42
+ * Avoid spamming logs for the same requestId when closed/destroyed
43
+ */
44
+ _defineProperty(this, "warnedMissingPendingWhenClosed", new Set());
45
+ /** List of unregister handler functions */
46
+ _defineProperty(this, "unregisterFns", []);
47
+ /** Whether opened */
48
+ _defineProperty(this, "_isOpen", false);
49
+ this.ackTimeout = (_options$ackTimeout = options === null || options === void 0 ? void 0 : options.ackTimeout) !== null && _options$ackTimeout !== void 0 ? _options$ackTimeout : DefaultTimeout.ACK;
50
+ this.versionValidator = (_options$versionValid = options === null || options === void 0 ? void 0 : options.versionValidator) !== null && _options$versionValid !== void 0 ? _options$versionValid : isCompatibleVersion;
51
+
52
+ // Get or create shared channel and create dispatcher
53
+ var channel = getOrCreateMessageChannel(options === null || options === void 0 ? void 0 : options.secretKey);
54
+ this.dispatcher = new MessageDispatcher(channel, MessageRole.CLIENT, instanceId);
55
+
56
+ // Auto-open by default (unless explicitly set to false)
57
+ if ((options === null || options === void 0 ? void 0 : options.autoOpen) !== false) {
58
+ this.open();
59
+ }
60
+ }
61
+
62
+ /**
63
+ * Open message handling (register message handlers)
64
+ */
65
+ open() {
66
+ if (this._isOpen) return;
67
+ this._isOpen = true;
68
+ this.registerHandlers();
69
+ }
70
+
71
+ /**
72
+ * Close message handling (unregister message handlers, but don't release channel)
73
+ */
74
+ close() {
75
+ if (!this._isOpen) return;
76
+ this._isOpen = false;
77
+
78
+ // Unregister all handlers
79
+ this.unregisterFns.forEach(fn => fn());
80
+ this.unregisterFns.length = 0;
81
+ }
82
+
83
+ /**
84
+ * Whether opened
85
+ */
86
+ get isOpen() {
87
+ return this._isOpen;
88
+ }
89
+
90
+ /**
91
+ * Register message handlers
92
+ */
93
+ registerHandlers() {
94
+ var handlerOptions = {
95
+ versionValidator: this.versionValidator,
96
+ onVersionError: this.handleVersionError.bind(this)
97
+ };
98
+
99
+ // Bind handleClientResponse to ensure correct 'this' context
100
+ var boundHandleClientResponse = this.handleClientResponse.bind(this);
101
+
102
+ // Handle ACK messages
103
+ this.unregisterFns.push(this.dispatcher.registerHandler(MessageType.ACK, boundHandleClientResponse, handlerOptions));
104
+
105
+ // Handle ASYNC messages
106
+ this.unregisterFns.push(this.dispatcher.registerHandler(MessageType.ASYNC, boundHandleClientResponse, handlerOptions));
107
+
108
+ // Handle RESPONSE messages
109
+ this.unregisterFns.push(this.dispatcher.registerHandler(MessageType.RESPONSE, boundHandleClientResponse, handlerOptions));
110
+
111
+ // Handle ERROR messages
112
+ this.unregisterFns.push(this.dispatcher.registerHandler(MessageType.ERROR, boundHandleClientResponse, handlerOptions));
113
+
114
+ // Handle RECEIVED messages
115
+ this.unregisterFns.push(this.dispatcher.registerHandler(MessageType.RECEIVED, this.handleReceived.bind(this), handlerOptions));
116
+
117
+ // Handle PONG messages
118
+ this.unregisterFns.push(this.dispatcher.registerHandler(MessageType.PONG, this.handlePong.bind(this), handlerOptions));
119
+
120
+ // Handle PING messages (server -> client heartbeat)
121
+ this.unregisterFns.push(this.dispatcher.registerHandler(MessageType.PING, this.handlePing.bind(this), handlerOptions));
122
+
123
+ // Handle stream_start messages (route to handleClientResponse so it reaches send callback)
124
+ // Note: stream_start is handled in send callback, not through streamCallback
125
+ this.unregisterFns.push(this.dispatcher.registerHandler(MessageType.STREAM_START, boundHandleClientResponse, handlerOptions));
126
+
127
+ // Handle other stream messages (stream_data, stream_end, etc.)
128
+ this.unregisterFns.push(this.dispatcher.registerHandler(type => type.startsWith('stream_') && type !== MessageType.STREAM_START, (data, context) => {
129
+ var _this$streamCallback;
130
+ return (_this$streamCallback = this.streamCallback) === null || _this$streamCallback === void 0 ? void 0 : _this$streamCallback.call(this, data, context);
131
+ }, handlerOptions));
132
+ }
133
+ handlePing(data, context) {
134
+ if (!context.source) return;
135
+ // Mark accepted so MessageDispatcher can auto-send ACK when requireAck === true
136
+ if (!context.handledBy) {
137
+ context.accepted = true;
138
+ context.handledBy = 'client';
139
+ }
140
+ // Reply PONG
141
+ this.dispatcher.sendMessage(context.source, context.origin, MessageType.PONG, data.requestId, {
142
+ targetId: data.creatorId
143
+ });
144
+ }
145
+
146
+ /**
147
+ * Handle protocol version error
148
+ */
149
+ handleVersionError(data, context, version) {
150
+ // For response messages, we need to notify the waiting request
151
+ var pending = this.pendingRequests.get(data.requestId);
152
+ if (pending) {
153
+ this.pendingRequests.delete(data.requestId);
154
+ pending.reject(new Error(formatMessage(Messages.PROTOCOL_VERSION_TOO_LOW, version, ProtocolVersion.MIN_SUPPORTED)));
155
+ }
156
+ }
157
+
158
+ /**
159
+ * Handle client response
160
+ */
161
+ handleClientResponse(data, context) {
162
+ var pending = this.pendingRequests.get(data.requestId);
163
+ if (!pending) {
164
+ /**
165
+ * Pending request not found - ignore by default.
166
+ *
167
+ * If client server is already closed/destroyed, emit a warning to help debugging:
168
+ * this usually means the client was recreated/unmounted before the response arrived.
169
+ */
170
+ if (!this._isOpen) {
171
+ var key = data.requestId;
172
+ if (!this.warnedMissingPendingWhenClosed.has(key)) {
173
+ this.warnedMissingPendingWhenClosed.add(key);
174
+ // eslint-disable-next-line no-console
175
+ console.warn(formatMessage(Messages.CLIENT_SERVER_IGNORED_MESSAGE_WHEN_CLOSED, data.type, data.requestId));
176
+ }
177
+ }
178
+ return;
179
+ }
180
+
181
+ // Validate origin
182
+ if (pending.originValidator) {
183
+ try {
184
+ if (!pending.originValidator(context.origin, data, context)) {
185
+ return;
186
+ }
187
+ } catch (_unused) {
188
+ // If validator throws, treat as disallowed
189
+ return;
190
+ }
191
+ } else if (pending.origin && pending.origin !== '*' && context.origin !== pending.origin) {
192
+ return;
193
+ }
194
+
195
+ /**
196
+ * Mark as handled so:
197
+ * - other client instances sharing the same channel won't also process it
198
+ * - MessageDispatcher can run its generalized requireAck auto-ack logic
199
+ */
200
+ if (!context.handledBy) {
201
+ context.accepted = true;
202
+ context.handledBy = 'client';
203
+ }
204
+
205
+ // ack, async, and stream_start don't delete pending (stream_start needs to keep pending for stream_data/stream_end)
206
+ if (data.type === MessageType.ACK || data.type === MessageType.ASYNC || data.type === MessageType.STREAM_START) {
207
+ pending.resolve(data);
208
+ return;
209
+ }
210
+
211
+ // response and error delete pending
212
+ this.pendingRequests.delete(data.requestId);
213
+ pending.resolve(data);
214
+ }
215
+
216
+ /**
217
+ * Handle received acknowledgment
218
+ */
219
+ handleReceived(data) {
220
+ var pending = this.pendingAcks.get(data.requestId);
221
+ if (pending) {
222
+ clearTimeout(pending.timeoutId);
223
+ this.pendingAcks.delete(data.requestId);
224
+ pending.resolve(true);
225
+ }
226
+ }
227
+
228
+ /**
229
+ * Handle pong
230
+ */
231
+ handlePong(data, context) {
232
+ var pending = this.pendingRequests.get(data.requestId);
233
+ if (pending) {
234
+ if (pending.originValidator) {
235
+ try {
236
+ if (!pending.originValidator(context.origin, data, context)) {
237
+ return;
238
+ }
239
+ } catch (_unused2) {
240
+ return;
241
+ }
242
+ } else if (pending.origin && pending.origin !== '*' && context.origin !== pending.origin) {
243
+ return;
244
+ }
245
+ if (!context.handledBy) {
246
+ context.accepted = true;
247
+ context.handledBy = 'client';
248
+ }
249
+ this.pendingRequests.delete(data.requestId);
250
+ pending.resolve(data);
251
+ }
252
+ }
253
+
254
+ /**
255
+ * Set stream message handler callback
256
+ */
257
+ setStreamCallback(callback) {
258
+ this.streamCallback = callback;
259
+ }
260
+
261
+ /** Get secretKey */
262
+ get secretKey() {
263
+ return this.dispatcher.secretKey;
264
+ }
265
+
266
+ /** Get the underlying MessageDispatcher */
267
+ get messageDispatcher() {
268
+ return this.dispatcher;
269
+ }
270
+
271
+ /**
272
+ * Register pending acknowledgment response
273
+ */
274
+ _registerPendingAck(requestId, resolve, reject) {
275
+ var timeoutId = setTimeout(() => {
276
+ this.pendingAcks.delete(requestId);
277
+ resolve(false);
278
+ }, this.ackTimeout);
279
+ this.pendingAcks.set(requestId, {
280
+ resolve,
281
+ reject,
282
+ timeoutId
283
+ });
284
+ }
285
+
286
+ /**
287
+ * Register pending request awaiting response
288
+ */
289
+ _registerPendingRequest(requestId, resolve, reject, origin, originValidator) {
290
+ this.pendingRequests.set(requestId, {
291
+ resolve,
292
+ reject,
293
+ origin,
294
+ originValidator
295
+ });
296
+ }
297
+
298
+ /**
299
+ * Cancel pending response
300
+ */
301
+ _unregisterPendingRequest(requestId) {
302
+ this.pendingRequests.delete(requestId);
303
+ }
304
+
305
+ /**
306
+ * Send ping message
307
+ */
308
+ sendPing(targetWindow, targetOrigin, requestId) {
309
+ this.dispatcher.sendMessage(targetWindow, targetOrigin, MessageType.PING, requestId);
310
+ }
311
+
312
+ /**
313
+ * Destroy (close and release channel reference)
314
+ */
315
+ destroy() {
316
+ // Close first
317
+ this.close();
318
+
319
+ // Clear pending
320
+ this.pendingRequests.clear();
321
+ this.pendingAcks.forEach(pending => clearTimeout(pending.timeoutId));
322
+ this.pendingAcks.clear();
323
+ this.warnedMissingPendingWhenClosed.clear();
324
+
325
+ // Destroy dispatcher and release channel reference
326
+ this.dispatcher.destroy();
327
+ releaseMessageChannel(this.dispatcher.getChannel());
328
+ }
329
+ }