request-iframe 0.0.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 (96) hide show
  1. package/QUICKSTART.CN.md +269 -0
  2. package/QUICKSTART.md +269 -0
  3. package/README.CN.md +1369 -0
  4. package/README.md +1016 -0
  5. package/library/__tests__/interceptors.test.ts +124 -0
  6. package/library/__tests__/requestIframe.test.ts +2216 -0
  7. package/library/__tests__/stream.test.ts +650 -0
  8. package/library/__tests__/utils.test.ts +433 -0
  9. package/library/api/client.d.ts +16 -0
  10. package/library/api/client.d.ts.map +1 -0
  11. package/library/api/client.js +72 -0
  12. package/library/api/server.d.ts +16 -0
  13. package/library/api/server.d.ts.map +1 -0
  14. package/library/api/server.js +44 -0
  15. package/library/constants/index.d.ts +209 -0
  16. package/library/constants/index.d.ts.map +1 -0
  17. package/library/constants/index.js +260 -0
  18. package/library/constants/messages.d.ts +80 -0
  19. package/library/constants/messages.d.ts.map +1 -0
  20. package/library/constants/messages.js +123 -0
  21. package/library/core/client.d.ts +99 -0
  22. package/library/core/client.d.ts.map +1 -0
  23. package/library/core/client.js +440 -0
  24. package/library/core/message-handler.d.ts +110 -0
  25. package/library/core/message-handler.d.ts.map +1 -0
  26. package/library/core/message-handler.js +320 -0
  27. package/library/core/request-response.d.ts +59 -0
  28. package/library/core/request-response.d.ts.map +1 -0
  29. package/library/core/request-response.js +337 -0
  30. package/library/core/request.d.ts +17 -0
  31. package/library/core/request.d.ts.map +1 -0
  32. package/library/core/request.js +34 -0
  33. package/library/core/response.d.ts +51 -0
  34. package/library/core/response.d.ts.map +1 -0
  35. package/library/core/response.js +323 -0
  36. package/library/core/server-base.d.ts +86 -0
  37. package/library/core/server-base.d.ts.map +1 -0
  38. package/library/core/server-base.js +257 -0
  39. package/library/core/server-client.d.ts +99 -0
  40. package/library/core/server-client.d.ts.map +1 -0
  41. package/library/core/server-client.js +256 -0
  42. package/library/core/server.d.ts +82 -0
  43. package/library/core/server.d.ts.map +1 -0
  44. package/library/core/server.js +338 -0
  45. package/library/index.d.ts +16 -0
  46. package/library/index.d.ts.map +1 -0
  47. package/library/index.js +211 -0
  48. package/library/interceptors/index.d.ts +41 -0
  49. package/library/interceptors/index.d.ts.map +1 -0
  50. package/library/interceptors/index.js +126 -0
  51. package/library/message/channel.d.ts +107 -0
  52. package/library/message/channel.d.ts.map +1 -0
  53. package/library/message/channel.js +184 -0
  54. package/library/message/dispatcher.d.ts +119 -0
  55. package/library/message/dispatcher.d.ts.map +1 -0
  56. package/library/message/dispatcher.js +249 -0
  57. package/library/message/index.d.ts +5 -0
  58. package/library/message/index.d.ts.map +1 -0
  59. package/library/message/index.js +25 -0
  60. package/library/stream/file-stream.d.ts +48 -0
  61. package/library/stream/file-stream.d.ts.map +1 -0
  62. package/library/stream/file-stream.js +240 -0
  63. package/library/stream/index.d.ts +15 -0
  64. package/library/stream/index.d.ts.map +1 -0
  65. package/library/stream/index.js +83 -0
  66. package/library/stream/readable-stream.d.ts +83 -0
  67. package/library/stream/readable-stream.d.ts.map +1 -0
  68. package/library/stream/readable-stream.js +249 -0
  69. package/library/stream/types.d.ts +165 -0
  70. package/library/stream/types.d.ts.map +1 -0
  71. package/library/stream/types.js +5 -0
  72. package/library/stream/writable-stream.d.ts +60 -0
  73. package/library/stream/writable-stream.d.ts.map +1 -0
  74. package/library/stream/writable-stream.js +348 -0
  75. package/library/types/index.d.ts +408 -0
  76. package/library/types/index.d.ts.map +1 -0
  77. package/library/types/index.js +5 -0
  78. package/library/utils/cache.d.ts +19 -0
  79. package/library/utils/cache.d.ts.map +1 -0
  80. package/library/utils/cache.js +83 -0
  81. package/library/utils/cookie.d.ts +117 -0
  82. package/library/utils/cookie.d.ts.map +1 -0
  83. package/library/utils/cookie.js +365 -0
  84. package/library/utils/debug.d.ts +11 -0
  85. package/library/utils/debug.d.ts.map +1 -0
  86. package/library/utils/debug.js +162 -0
  87. package/library/utils/index.d.ts +13 -0
  88. package/library/utils/index.d.ts.map +1 -0
  89. package/library/utils/index.js +132 -0
  90. package/library/utils/path-match.d.ts +17 -0
  91. package/library/utils/path-match.d.ts.map +1 -0
  92. package/library/utils/path-match.js +90 -0
  93. package/library/utils/protocol.d.ts +61 -0
  94. package/library/utils/protocol.d.ts.map +1 -0
  95. package/library/utils/protocol.js +169 -0
  96. package/package.json +58 -0
@@ -0,0 +1,348 @@
1
+ "use strict";
2
+
3
+ require("core-js/modules/es.array.filter.js");
4
+ require("core-js/modules/es.object.get-own-property-descriptors.js");
5
+ require("core-js/modules/web.dom-collections.for-each.js");
6
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
7
+ Object.defineProperty(exports, "__esModule", {
8
+ value: true
9
+ });
10
+ exports.IframeWritableStream = void 0;
11
+ var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
12
+ var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
13
+ var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
14
+ require("core-js/modules/es.symbol.description.js");
15
+ require("core-js/modules/es.symbol.async-iterator.js");
16
+ require("core-js/modules/es.array.iterator.js");
17
+ require("core-js/modules/es.array.slice.js");
18
+ require("core-js/modules/es.promise.js");
19
+ require("core-js/modules/es.regexp.to-string.js");
20
+ require("core-js/modules/web.dom-collections.iterator.js");
21
+ var _utils = require("../utils");
22
+ var _constants = require("../constants");
23
+ function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
24
+ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2.default)(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
25
+ function _asyncIterator(r) { var n, t, o, e = 2; for ("undefined" != typeof Symbol && (t = Symbol.asyncIterator, o = Symbol.iterator); e--;) { if (t && null != (n = r[t])) return n.call(r); if (o && null != (n = r[o])) return new AsyncFromSyncIterator(n.call(r)); t = "@@asyncIterator", o = "@@iterator"; } throw new TypeError("Object is not async iterable"); }
26
+ function AsyncFromSyncIterator(r) { function AsyncFromSyncIteratorContinuation(r) { if (Object(r) !== r) return Promise.reject(new TypeError(r + " is not an object.")); var n = r.done; return Promise.resolve(r.value).then(function (r) { return { value: r, done: n }; }); } return AsyncFromSyncIterator = function AsyncFromSyncIterator(r) { this.s = r, this.n = r.next; }, AsyncFromSyncIterator.prototype = { s: null, n: null, next: function next() { return AsyncFromSyncIteratorContinuation(this.n.apply(this.s, arguments)); }, return: function _return(r) { var n = this.s.return; return void 0 === n ? Promise.resolve({ value: r, done: !0 }) : AsyncFromSyncIteratorContinuation(n.apply(this.s, arguments)); }, throw: function _throw(r) { var n = this.s.return; return void 0 === n ? Promise.reject(r) : AsyncFromSyncIteratorContinuation(n.apply(this.s, arguments)); } }, new AsyncFromSyncIterator(r); }
27
+ /**
28
+ * Generate a unique stream ID
29
+ */
30
+ function generateStreamId() {
31
+ return `stream_${Date.now()}_${Math.random().toString(36).slice(2, 11)}`;
32
+ }
33
+
34
+ /**
35
+ * IframeWritableStream - Server-side writable stream
36
+ * Used to send stream data to the client
37
+ */
38
+ class IframeWritableStream {
39
+ constructor(options = {}) {
40
+ var _options$type, _options$chunked;
41
+ (0, _defineProperty2.default)(this, "_state", _constants.StreamState.PENDING);
42
+ (0, _defineProperty2.default)(this, "context", null);
43
+ this.streamId = generateStreamId();
44
+ this.type = (_options$type = options.type) !== null && _options$type !== void 0 ? _options$type : _constants.StreamType.DATA;
45
+ this.chunked = (_options$chunked = options.chunked) !== null && _options$chunked !== void 0 ? _options$chunked : true;
46
+ this.iterator = options.iterator;
47
+ this.nextFn = options.next;
48
+ this.metadata = options.metadata;
49
+ }
50
+
51
+ /** Get stream state */
52
+ get state() {
53
+ return this._state;
54
+ }
55
+
56
+ /**
57
+ * Bind to request context
58
+ * Called during res.sendStream()
59
+ */
60
+ _bind(context) {
61
+ this.context = context;
62
+ }
63
+
64
+ /**
65
+ * Send message to client
66
+ */
67
+ sendMessage(type, data) {
68
+ if (!this.context) {
69
+ throw new Error(_constants.Messages.STREAM_NOT_BOUND);
70
+ }
71
+ var message = (0, _utils.createPostMessage)(type, this.context.requestId, {
72
+ secretKey: this.context.secretKey,
73
+ body: _objectSpread({
74
+ streamId: this.streamId
75
+ }, data)
76
+ });
77
+
78
+ // Use channel if available, otherwise use direct postMessage
79
+ if (this.context.channel) {
80
+ this.context.channel.send(this.context.targetWindow, message, this.context.targetOrigin);
81
+ } else {
82
+ this.context.targetWindow.postMessage(message, this.context.targetOrigin);
83
+ }
84
+ }
85
+
86
+ /**
87
+ * Start stream transfer
88
+ */
89
+ start() {
90
+ var _this = this;
91
+ return (0, _asyncToGenerator2.default)(/*#__PURE__*/_regenerator.default.mark(function _callee() {
92
+ var _t;
93
+ return _regenerator.default.wrap(function (_context) {
94
+ while (1) switch (_context.prev = _context.next) {
95
+ case 0:
96
+ if (_this.context) {
97
+ _context.next = 1;
98
+ break;
99
+ }
100
+ throw new Error(_constants.Messages.STREAM_NOT_BOUND);
101
+ case 1:
102
+ if (!(_this._state !== _constants.StreamState.PENDING)) {
103
+ _context.next = 2;
104
+ break;
105
+ }
106
+ throw new Error(_constants.Messages.STREAM_ALREADY_STARTED);
107
+ case 2:
108
+ _this._state = _constants.StreamState.STREAMING;
109
+
110
+ // Send stream start message
111
+ _this.sendMessage(_constants.MessageType.STREAM_START, {
112
+ type: _this.type,
113
+ chunked: _this.chunked,
114
+ metadata: _this.metadata
115
+ });
116
+ _context.prev = 3;
117
+ if (!_this.iterator) {
118
+ _context.next = 5;
119
+ break;
120
+ }
121
+ _context.next = 4;
122
+ return _this.streamFromIterator();
123
+ case 4:
124
+ _context.next = 8;
125
+ break;
126
+ case 5:
127
+ if (!_this.nextFn) {
128
+ _context.next = 7;
129
+ break;
130
+ }
131
+ _context.next = 6;
132
+ return _this.streamFromNext();
133
+ case 6:
134
+ _context.next = 8;
135
+ break;
136
+ case 7:
137
+ // No data source, end directly
138
+ _this.end();
139
+ case 8:
140
+ _context.next = 10;
141
+ break;
142
+ case 9:
143
+ _context.prev = 9;
144
+ _t = _context["catch"](3);
145
+ _this.error(_t.message || String(_t));
146
+ case 10:
147
+ case "end":
148
+ return _context.stop();
149
+ }
150
+ }, _callee, null, [[3, 9]]);
151
+ }))();
152
+ }
153
+
154
+ /**
155
+ * Generate data from iterator
156
+ */
157
+ streamFromIterator() {
158
+ var _this2 = this;
159
+ return (0, _asyncToGenerator2.default)(/*#__PURE__*/_regenerator.default.mark(function _callee2() {
160
+ var gen, _iteratorAbruptCompletion, _didIteratorError, _iteratorError, _iterator, _step, chunk, _t2, _t3;
161
+ return _regenerator.default.wrap(function (_context2) {
162
+ while (1) switch (_context2.prev = _context2.next) {
163
+ case 0:
164
+ if (_this2.iterator) {
165
+ _context2.next = 1;
166
+ break;
167
+ }
168
+ return _context2.abrupt("return");
169
+ case 1:
170
+ gen = _this2.iterator();
171
+ _context2.prev = 2;
172
+ _iteratorAbruptCompletion = false;
173
+ _didIteratorError = false;
174
+ _context2.prev = 3;
175
+ _iterator = _asyncIterator(gen);
176
+ case 4:
177
+ _context2.next = 5;
178
+ return _iterator.next();
179
+ case 5:
180
+ if (!(_iteratorAbruptCompletion = !(_step = _context2.sent).done)) {
181
+ _context2.next = 8;
182
+ break;
183
+ }
184
+ chunk = _step.value;
185
+ if (!(_this2._state !== _constants.StreamState.STREAMING)) {
186
+ _context2.next = 6;
187
+ break;
188
+ }
189
+ return _context2.abrupt("continue", 8);
190
+ case 6:
191
+ _this2.sendData(chunk);
192
+ case 7:
193
+ _iteratorAbruptCompletion = false;
194
+ _context2.next = 4;
195
+ break;
196
+ case 8:
197
+ _context2.next = 10;
198
+ break;
199
+ case 9:
200
+ _context2.prev = 9;
201
+ _t2 = _context2["catch"](3);
202
+ _didIteratorError = true;
203
+ _iteratorError = _t2;
204
+ case 10:
205
+ _context2.prev = 10;
206
+ _context2.prev = 11;
207
+ if (!(_iteratorAbruptCompletion && _iterator.return != null)) {
208
+ _context2.next = 12;
209
+ break;
210
+ }
211
+ _context2.next = 12;
212
+ return _iterator.return();
213
+ case 12:
214
+ _context2.prev = 12;
215
+ if (!_didIteratorError) {
216
+ _context2.next = 13;
217
+ break;
218
+ }
219
+ throw _iteratorError;
220
+ case 13:
221
+ return _context2.finish(12);
222
+ case 14:
223
+ return _context2.finish(10);
224
+ case 15:
225
+ if (_this2._state === _constants.StreamState.STREAMING) {
226
+ _this2.end();
227
+ }
228
+ _context2.next = 17;
229
+ break;
230
+ case 16:
231
+ _context2.prev = 16;
232
+ _t3 = _context2["catch"](2);
233
+ if (_this2._state === _constants.StreamState.STREAMING) {
234
+ _this2.error(_t3.message || String(_t3));
235
+ }
236
+ case 17:
237
+ case "end":
238
+ return _context2.stop();
239
+ }
240
+ }, _callee2, null, [[2, 16], [3, 9, 10, 15], [11,, 12, 14]]);
241
+ }))();
242
+ }
243
+
244
+ /**
245
+ * Generate data from next function
246
+ */
247
+ streamFromNext() {
248
+ var _this3 = this;
249
+ return (0, _asyncToGenerator2.default)(/*#__PURE__*/_regenerator.default.mark(function _callee3() {
250
+ var result, _t4;
251
+ return _regenerator.default.wrap(function (_context3) {
252
+ while (1) switch (_context3.prev = _context3.next) {
253
+ case 0:
254
+ if (_this3.nextFn) {
255
+ _context3.next = 1;
256
+ break;
257
+ }
258
+ return _context3.abrupt("return");
259
+ case 1:
260
+ _context3.prev = 1;
261
+ case 2:
262
+ if (!(_this3._state === _constants.StreamState.STREAMING)) {
263
+ _context3.next = 5;
264
+ break;
265
+ }
266
+ _context3.next = 3;
267
+ return Promise.resolve(_this3.nextFn());
268
+ case 3:
269
+ result = _context3.sent;
270
+ if (!result.done) {
271
+ _context3.next = 4;
272
+ break;
273
+ }
274
+ _this3.sendData(result.data, true);
275
+ _this3.end();
276
+ return _context3.abrupt("continue", 5);
277
+ case 4:
278
+ _this3.sendData(result.data);
279
+ _context3.next = 2;
280
+ break;
281
+ case 5:
282
+ _context3.next = 7;
283
+ break;
284
+ case 6:
285
+ _context3.prev = 6;
286
+ _t4 = _context3["catch"](1);
287
+ if (_this3._state === _constants.StreamState.STREAMING) {
288
+ _this3.error(_t4.message || String(_t4));
289
+ }
290
+ case 7:
291
+ case "end":
292
+ return _context3.stop();
293
+ }
294
+ }, _callee3, null, [[1, 6]]);
295
+ }))();
296
+ }
297
+
298
+ /**
299
+ * Send data chunk
300
+ */
301
+ sendData(data, done = false) {
302
+ this.sendMessage(_constants.MessageType.STREAM_DATA, {
303
+ data: this.encodeData(data),
304
+ done
305
+ });
306
+ }
307
+
308
+ /**
309
+ * Encode data (subclasses can override, e.g., FileStream needs Base64 encoding)
310
+ */
311
+ encodeData(data) {
312
+ return data;
313
+ }
314
+
315
+ /**
316
+ * End stream
317
+ */
318
+ end() {
319
+ if (this._state !== _constants.StreamState.STREAMING) return;
320
+ this._state = _constants.StreamState.ENDED;
321
+ this.sendMessage(_constants.MessageType.STREAM_END);
322
+ }
323
+
324
+ /**
325
+ * Send error
326
+ */
327
+ error(message) {
328
+ if (this._state !== _constants.StreamState.STREAMING) return;
329
+ this._state = _constants.StreamState.ERROR;
330
+ this.sendMessage(_constants.MessageType.STREAM_ERROR, {
331
+ error: message
332
+ });
333
+ }
334
+
335
+ /**
336
+ * Cancel stream transfer
337
+ */
338
+ cancel(reason) {
339
+ if (this._state !== _constants.StreamState.PENDING && this._state !== _constants.StreamState.STREAMING) return;
340
+ this._state = _constants.StreamState.CANCELLED;
341
+ if (this.context) {
342
+ this.sendMessage(_constants.MessageType.STREAM_CANCEL, {
343
+ reason
344
+ });
345
+ }
346
+ }
347
+ }
348
+ exports.IframeWritableStream = IframeWritableStream;