request-iframe 0.0.6 → 0.1.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.
- package/README.CN.md +220 -21
- package/README.md +221 -24
- package/esm/api/client.js +80 -0
- package/esm/api/server.js +61 -0
- package/esm/constants/index.js +289 -0
- package/esm/constants/messages.js +157 -0
- package/esm/core/client-server.js +294 -0
- package/esm/core/client.js +873 -0
- package/esm/core/request.js +27 -0
- package/esm/core/response.js +459 -0
- package/esm/core/server.js +776 -0
- package/esm/index.js +21 -0
- package/esm/interceptors/index.js +122 -0
- package/esm/message/channel.js +182 -0
- package/esm/message/dispatcher.js +418 -0
- package/esm/message/index.js +2 -0
- package/esm/stream/file-stream.js +289 -0
- package/esm/stream/index.js +44 -0
- package/esm/stream/readable-stream.js +539 -0
- package/esm/stream/stream-core.js +204 -0
- package/esm/stream/types.js +1 -0
- package/esm/stream/writable-stream.js +836 -0
- package/esm/types/index.js +1 -0
- package/esm/utils/ack.js +36 -0
- package/esm/utils/cache.js +147 -0
- package/esm/utils/cookie.js +352 -0
- package/esm/utils/debug.js +521 -0
- package/esm/utils/error.js +27 -0
- package/esm/utils/index.js +180 -0
- package/esm/utils/origin.js +30 -0
- package/esm/utils/path-match.js +148 -0
- package/esm/utils/protocol.js +157 -0
- package/library/api/client.d.ts.map +1 -1
- package/library/api/client.js +13 -5
- package/library/api/server.d.ts.map +1 -1
- package/library/api/server.js +6 -1
- package/library/constants/index.d.ts +59 -4
- package/library/constants/index.d.ts.map +1 -1
- package/library/constants/index.js +67 -9
- package/library/constants/messages.d.ts +8 -1
- package/library/constants/messages.d.ts.map +1 -1
- package/library/constants/messages.js +8 -1
- package/library/core/client-server.d.ts +7 -15
- package/library/core/client-server.d.ts.map +1 -1
- package/library/core/client-server.js +56 -44
- package/library/core/client.d.ts +4 -1
- package/library/core/client.d.ts.map +1 -1
- package/library/core/client.js +74 -31
- package/library/core/response.d.ts +21 -3
- package/library/core/response.d.ts.map +1 -1
- package/library/core/response.js +55 -7
- package/library/core/server.d.ts +34 -3
- package/library/core/server.d.ts.map +1 -1
- package/library/core/server.js +191 -21
- package/library/message/channel.d.ts +6 -0
- package/library/message/channel.d.ts.map +1 -1
- package/library/message/channel.js +2 -1
- package/library/message/dispatcher.d.ts +32 -0
- package/library/message/dispatcher.d.ts.map +1 -1
- package/library/message/dispatcher.js +131 -1
- package/library/stream/file-stream.d.ts +4 -0
- package/library/stream/file-stream.d.ts.map +1 -1
- package/library/stream/file-stream.js +61 -33
- package/library/stream/index.d.ts.map +1 -1
- package/library/stream/index.js +2 -0
- package/library/stream/readable-stream.d.ts +30 -11
- package/library/stream/readable-stream.d.ts.map +1 -1
- package/library/stream/readable-stream.js +368 -73
- package/library/stream/stream-core.d.ts +65 -0
- package/library/stream/stream-core.d.ts.map +1 -0
- package/library/stream/stream-core.js +211 -0
- package/library/stream/types.d.ts +203 -3
- package/library/stream/types.d.ts.map +1 -1
- package/library/stream/writable-stream.d.ts +59 -13
- package/library/stream/writable-stream.d.ts.map +1 -1
- package/library/stream/writable-stream.js +647 -197
- package/library/types/index.d.ts +70 -4
- package/library/types/index.d.ts.map +1 -1
- package/library/utils/ack.d.ts +2 -0
- package/library/utils/ack.d.ts.map +1 -0
- package/library/utils/ack.js +44 -0
- package/library/utils/debug.js +1 -1
- package/library/utils/index.d.ts +1 -0
- package/library/utils/index.d.ts.map +1 -1
- package/library/utils/index.js +19 -2
- package/library/utils/origin.d.ts +14 -0
- package/library/utils/origin.d.ts.map +1 -0
- package/library/utils/origin.js +35 -0
- package/package.json +30 -7
- package/react/README.md +16 -0
- package/react/esm/index.js +284 -0
- package/react/library/index.d.ts +1 -1
- package/react/library/index.d.ts.map +1 -1
- package/react/library/index.js +3 -3
- package/react/package.json +24 -2
|
@@ -0,0 +1,539 @@
|
|
|
1
|
+
import "core-js/modules/es.array.filter.js";
|
|
2
|
+
import "core-js/modules/es.object.get-own-property-descriptors.js";
|
|
3
|
+
import "core-js/modules/web.dom-collections.for-each.js";
|
|
4
|
+
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
|
|
5
|
+
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
|
|
6
|
+
import _regeneratorRuntime from "@babel/runtime/regenerator";
|
|
7
|
+
import "core-js/modules/es.symbol.async-iterator.js";
|
|
8
|
+
import "core-js/modules/es.array.iterator.js";
|
|
9
|
+
import "core-js/modules/es.array.slice.js";
|
|
10
|
+
import "core-js/modules/es.promise.js";
|
|
11
|
+
import "core-js/modules/es.promise.finally.js";
|
|
12
|
+
import "core-js/modules/web.dom-collections.iterator.js";
|
|
13
|
+
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; }
|
|
14
|
+
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) { _defineProperty(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; }
|
|
15
|
+
import { createPostMessage } from '../utils';
|
|
16
|
+
import { MessageType, Messages, StreamType as StreamTypeConstant, StreamState as StreamStateConstant, StreamInternalMessageType, formatMessage, StreamEvent } from '../constants';
|
|
17
|
+
import { IframeStreamCore } from './stream-core';
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Stream message handler interface
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* IframeReadableStream - Client-side readable stream
|
|
25
|
+
* Used to receive stream data sent from the server
|
|
26
|
+
*/
|
|
27
|
+
export class IframeReadableStream extends IframeStreamCore {
|
|
28
|
+
constructor(streamId, requestId, messageHandler, options = {}) {
|
|
29
|
+
var _options$type, _options$chunked, _options$consume;
|
|
30
|
+
super(streamId, (_options$type = options.type) !== null && _options$type !== void 0 ? _options$type : StreamTypeConstant.DATA, (_options$chunked = options.chunked) !== null && _options$chunked !== void 0 ? _options$chunked : true, options.metadata, (_options$consume = options.consume) !== null && _options$consume !== void 0 ? _options$consume : true, options.mode);
|
|
31
|
+
_defineProperty(this, "heartbeatInFlight", null);
|
|
32
|
+
_defineProperty(this, "lastActivityAt", Date.now());
|
|
33
|
+
/** pull/ack protocol */
|
|
34
|
+
_defineProperty(this, "pullScheduled", false);
|
|
35
|
+
_defineProperty(this, "highWaterMark", 16);
|
|
36
|
+
this.requestId = requestId;
|
|
37
|
+
this.messageHandler = messageHandler;
|
|
38
|
+
this.secretKey = options.secretKey;
|
|
39
|
+
this.idleTimeout = options.idleTimeout;
|
|
40
|
+
this.heartbeat = options.heartbeat;
|
|
41
|
+
|
|
42
|
+
// Register stream message handler
|
|
43
|
+
this.messageHandler.registerStreamHandler(streamId, this.handleStreamMessage.bind(this));
|
|
44
|
+
// Initial pull to start the stream (pull protocol)
|
|
45
|
+
this.requestMore(1);
|
|
46
|
+
|
|
47
|
+
// Observable: constructed and ready to receive data
|
|
48
|
+
this.emit(StreamEvent.START, {
|
|
49
|
+
streamId: this.streamId,
|
|
50
|
+
type: this.type,
|
|
51
|
+
chunked: this.chunked,
|
|
52
|
+
mode: this.mode,
|
|
53
|
+
metadata: this.metadata
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
postControl(type, body) {
|
|
57
|
+
var message = createPostMessage(type, this.requestId, {
|
|
58
|
+
secretKey: this.secretKey,
|
|
59
|
+
body: _objectSpread({
|
|
60
|
+
streamId: this.streamId
|
|
61
|
+
}, body)
|
|
62
|
+
});
|
|
63
|
+
this.messageHandler.postMessage(message);
|
|
64
|
+
}
|
|
65
|
+
requestMore(credit) {
|
|
66
|
+
if (!credit || credit <= 0) return;
|
|
67
|
+
if (this._state !== StreamStateConstant.PENDING && this._state !== StreamStateConstant.STREAMING) return;
|
|
68
|
+
try {
|
|
69
|
+
this.postControl(MessageType.STREAM_PULL, {
|
|
70
|
+
credit
|
|
71
|
+
});
|
|
72
|
+
this.emit(StreamEvent.PULL, {
|
|
73
|
+
credit
|
|
74
|
+
});
|
|
75
|
+
} catch (_unused) {
|
|
76
|
+
/** ignore */
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
schedulePullIfNeeded() {
|
|
80
|
+
if (this.pullScheduled) return;
|
|
81
|
+
this.pullScheduled = true;
|
|
82
|
+
Promise.resolve().then(() => {
|
|
83
|
+
this.pullScheduled = false;
|
|
84
|
+
if (this._state !== StreamStateConstant.PENDING && this._state !== StreamStateConstant.STREAMING) return;
|
|
85
|
+
var need = Math.max(0, this.highWaterMark - this.chunks.length);
|
|
86
|
+
if (need > 0) {
|
|
87
|
+
this.requestMore(need);
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** Get stream state */
|
|
93
|
+
get state() {
|
|
94
|
+
return super.state;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Handle stream message
|
|
99
|
+
*/
|
|
100
|
+
handleStreamMessage(data) {
|
|
101
|
+
// Mark activity on any incoming stream message
|
|
102
|
+
this.lastActivityAt = Date.now();
|
|
103
|
+
switch (data.type) {
|
|
104
|
+
case StreamInternalMessageType.DATA:
|
|
105
|
+
this.handleData(data.data, data.done, data.seq);
|
|
106
|
+
break;
|
|
107
|
+
case StreamInternalMessageType.END:
|
|
108
|
+
this.handleEnd();
|
|
109
|
+
break;
|
|
110
|
+
case StreamInternalMessageType.ERROR:
|
|
111
|
+
this.handleError(new Error(data.error || Messages.STREAM_ERROR));
|
|
112
|
+
break;
|
|
113
|
+
case StreamInternalMessageType.CANCEL:
|
|
114
|
+
this.handleCancel(data.reason);
|
|
115
|
+
break;
|
|
116
|
+
case StreamInternalMessageType.PULL:
|
|
117
|
+
// Control messages for writer side; ignore in readable stream
|
|
118
|
+
break;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Handle data chunk (internal method)
|
|
124
|
+
*/
|
|
125
|
+
handleData(data, done, seq) {
|
|
126
|
+
if (this._state !== StreamStateConstant.PENDING && this._state !== StreamStateConstant.STREAMING) return;
|
|
127
|
+
this._state = StreamStateConstant.STREAMING;
|
|
128
|
+
var decoded = this.decodeData(data);
|
|
129
|
+
this.chunks.push(decoded);
|
|
130
|
+
this.emit(StreamEvent.DATA, {
|
|
131
|
+
chunk: decoded,
|
|
132
|
+
done,
|
|
133
|
+
seq
|
|
134
|
+
});
|
|
135
|
+
this.emit(StreamEvent.STATE, {
|
|
136
|
+
state: this._state
|
|
137
|
+
});
|
|
138
|
+
this.notifyWaiters();
|
|
139
|
+
this.schedulePullIfNeeded();
|
|
140
|
+
if (done) {
|
|
141
|
+
this.handleEnd();
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Decode data (subclasses can override, e.g., FileStream needs Base64 decoding)
|
|
147
|
+
*/
|
|
148
|
+
decodeData(data) {
|
|
149
|
+
return data;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Stream ended (internal handling)
|
|
154
|
+
*/
|
|
155
|
+
handleEnd() {
|
|
156
|
+
var _this$onEndCallback;
|
|
157
|
+
if (this._state === StreamStateConstant.ENDED) return;
|
|
158
|
+
super.end();
|
|
159
|
+
this.messageHandler.unregisterStreamHandler(this.streamId);
|
|
160
|
+
this.notifyWaiters();
|
|
161
|
+
(_this$onEndCallback = this.onEndCallback) === null || _this$onEndCallback === void 0 || _this$onEndCallback.call(this);
|
|
162
|
+
this.clearAllListeners();
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Merge data chunks (subclasses can override)
|
|
167
|
+
*/
|
|
168
|
+
mergeChunks() {
|
|
169
|
+
if (this.chunks.length === 0) {
|
|
170
|
+
return undefined;
|
|
171
|
+
}
|
|
172
|
+
if (this.chunks.length === 1) {
|
|
173
|
+
return this.chunks[0];
|
|
174
|
+
}
|
|
175
|
+
// Default returns array
|
|
176
|
+
return this.chunks;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Stream error (internal method)
|
|
181
|
+
*/
|
|
182
|
+
handleError(error) {
|
|
183
|
+
var _this$onErrorCallback;
|
|
184
|
+
if (this._state === StreamStateConstant.ENDED || this._state === StreamStateConstant.ERROR) return;
|
|
185
|
+
super.fail(error);
|
|
186
|
+
this.messageHandler.unregisterStreamHandler(this.streamId);
|
|
187
|
+
this.notifyWaiters();
|
|
188
|
+
(_this$onErrorCallback = this.onErrorCallback) === null || _this$onErrorCallback === void 0 || _this$onErrorCallback.call(this, error);
|
|
189
|
+
this.clearAllListeners();
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Stream cancelled (internal method)
|
|
194
|
+
*/
|
|
195
|
+
handleCancel(reason) {
|
|
196
|
+
this.cancelInternal(reason, false);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Cancel/abort core logic
|
|
201
|
+
* @param reason cancel reason
|
|
202
|
+
* @param notifyRemote whether to notify remote side (send stream_cancel)
|
|
203
|
+
*/
|
|
204
|
+
cancelInternal(reason, notifyRemote = false) {
|
|
205
|
+
if (this._state === StreamStateConstant.ENDED || this._state === StreamStateConstant.ERROR || this._state === StreamStateConstant.CANCELLED) return;
|
|
206
|
+
super.cancel(formatMessage(Messages.STREAM_CANCELLED, reason || ''));
|
|
207
|
+
this.notifyWaiters();
|
|
208
|
+
this.emit(StreamEvent.CANCEL, {
|
|
209
|
+
reason,
|
|
210
|
+
remote: notifyRemote
|
|
211
|
+
});
|
|
212
|
+
if (notifyRemote) {
|
|
213
|
+
try {
|
|
214
|
+
/** Notify server to cancel */
|
|
215
|
+
var _message = createPostMessage(MessageType.STREAM_CANCEL, this.requestId, {
|
|
216
|
+
secretKey: this.secretKey,
|
|
217
|
+
body: {
|
|
218
|
+
streamId: this.streamId,
|
|
219
|
+
reason
|
|
220
|
+
}
|
|
221
|
+
});
|
|
222
|
+
this.messageHandler.postMessage(_message);
|
|
223
|
+
} catch (_unused2) {
|
|
224
|
+
/** ignore send failures on cancel/abort */
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
this.messageHandler.unregisterStreamHandler(this.streamId);
|
|
228
|
+
if (this.terminalError) {
|
|
229
|
+
var _this$onErrorCallback2;
|
|
230
|
+
(_this$onErrorCallback2 = this.onErrorCallback) === null || _this$onErrorCallback2 === void 0 || _this$onErrorCallback2.call(this, this.terminalError);
|
|
231
|
+
} else {
|
|
232
|
+
var _this$onErrorCallback3;
|
|
233
|
+
(_this$onErrorCallback3 = this.onErrorCallback) === null || _this$onErrorCallback3 === void 0 || _this$onErrorCallback3.call(this, new Error(Messages.STREAM_CANCELLED));
|
|
234
|
+
}
|
|
235
|
+
this.clearAllListeners();
|
|
236
|
+
}
|
|
237
|
+
performHeartbeat() {
|
|
238
|
+
var _this = this;
|
|
239
|
+
return _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
|
|
240
|
+
return _regeneratorRuntime.wrap(function (_context) {
|
|
241
|
+
while (1) switch (_context.prev = _context.next) {
|
|
242
|
+
case 0:
|
|
243
|
+
if (_this.heartbeat) {
|
|
244
|
+
_context.next = 1;
|
|
245
|
+
break;
|
|
246
|
+
}
|
|
247
|
+
return _context.abrupt("return", false);
|
|
248
|
+
case 1:
|
|
249
|
+
if (!_this.heartbeatInFlight) {
|
|
250
|
+
_this.heartbeatInFlight = Promise.resolve().then(() => _this.heartbeat()).catch(() => false).finally(() => {
|
|
251
|
+
_this.heartbeatInFlight = null;
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
return _context.abrupt("return", _this.heartbeatInFlight);
|
|
255
|
+
case 2:
|
|
256
|
+
case "end":
|
|
257
|
+
return _context.stop();
|
|
258
|
+
}
|
|
259
|
+
}, _callee);
|
|
260
|
+
}))();
|
|
261
|
+
}
|
|
262
|
+
waitForChangeWithIdleTimeout() {
|
|
263
|
+
var _this2 = this;
|
|
264
|
+
return _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee2() {
|
|
265
|
+
var state0, snapshot, timerId, stateAfter, ok;
|
|
266
|
+
return _regeneratorRuntime.wrap(function (_context2) {
|
|
267
|
+
while (1) switch (_context2.prev = _context2.next) {
|
|
268
|
+
case 0:
|
|
269
|
+
state0 = _this2._state;
|
|
270
|
+
if (!(state0 === StreamStateConstant.ENDED || state0 === StreamStateConstant.ERROR || state0 === StreamStateConstant.CANCELLED)) {
|
|
271
|
+
_context2.next = 1;
|
|
272
|
+
break;
|
|
273
|
+
}
|
|
274
|
+
return _context2.abrupt("return");
|
|
275
|
+
case 1:
|
|
276
|
+
if (!(!_this2.idleTimeout || _this2.idleTimeout <= 0)) {
|
|
277
|
+
_context2.next = 3;
|
|
278
|
+
break;
|
|
279
|
+
}
|
|
280
|
+
_context2.next = 2;
|
|
281
|
+
return _this2.waitForChange();
|
|
282
|
+
case 2:
|
|
283
|
+
return _context2.abrupt("return");
|
|
284
|
+
case 3:
|
|
285
|
+
snapshot = _this2.lastActivityAt;
|
|
286
|
+
timerId = null;
|
|
287
|
+
_context2.next = 4;
|
|
288
|
+
return Promise.race([_this2.waitForChange(), new Promise(resolve => {
|
|
289
|
+
timerId = setTimeout(resolve, _this2.idleTimeout);
|
|
290
|
+
})]);
|
|
291
|
+
case 4:
|
|
292
|
+
if (timerId) clearTimeout(timerId);
|
|
293
|
+
|
|
294
|
+
// If stream already moved to a terminal state, just return
|
|
295
|
+
stateAfter = _this2._state;
|
|
296
|
+
if (!(stateAfter === StreamStateConstant.ENDED || stateAfter === StreamStateConstant.ERROR || stateAfter === StreamStateConstant.CANCELLED)) {
|
|
297
|
+
_context2.next = 5;
|
|
298
|
+
break;
|
|
299
|
+
}
|
|
300
|
+
return _context2.abrupt("return");
|
|
301
|
+
case 5:
|
|
302
|
+
if (!(_this2.lastActivityAt !== snapshot)) {
|
|
303
|
+
_context2.next = 6;
|
|
304
|
+
break;
|
|
305
|
+
}
|
|
306
|
+
return _context2.abrupt("return");
|
|
307
|
+
case 6:
|
|
308
|
+
_context2.next = 7;
|
|
309
|
+
return _this2.performHeartbeat();
|
|
310
|
+
case 7:
|
|
311
|
+
ok = _context2.sent;
|
|
312
|
+
if (!ok) {
|
|
313
|
+
_context2.next = 8;
|
|
314
|
+
break;
|
|
315
|
+
}
|
|
316
|
+
// Treat heartbeat as activity to prevent immediate re-trigger
|
|
317
|
+
_this2.lastActivityAt = Date.now();
|
|
318
|
+
return _context2.abrupt("return");
|
|
319
|
+
case 8:
|
|
320
|
+
// Connection likely dead: fail the stream
|
|
321
|
+
_this2.emit(StreamEvent.TIMEOUT, {
|
|
322
|
+
timeout: _this2.idleTimeout
|
|
323
|
+
});
|
|
324
|
+
_this2.handleError(new Error(formatMessage(Messages.STREAM_TIMEOUT, _this2.idleTimeout)));
|
|
325
|
+
case 9:
|
|
326
|
+
case "end":
|
|
327
|
+
return _context2.stop();
|
|
328
|
+
}
|
|
329
|
+
}, _callee2);
|
|
330
|
+
}))();
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/**
|
|
334
|
+
* Read all data
|
|
335
|
+
*/
|
|
336
|
+
read() {
|
|
337
|
+
var _this3 = this;
|
|
338
|
+
return _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee3() {
|
|
339
|
+
var merged, _merged;
|
|
340
|
+
return _regeneratorRuntime.wrap(function (_context3) {
|
|
341
|
+
while (1) switch (_context3.prev = _context3.next) {
|
|
342
|
+
case 0:
|
|
343
|
+
if (!(_this3._state === StreamStateConstant.ENDED)) {
|
|
344
|
+
_context3.next = 1;
|
|
345
|
+
break;
|
|
346
|
+
}
|
|
347
|
+
merged = _this3.mergeChunks();
|
|
348
|
+
_this3.emit(StreamEvent.READ, {
|
|
349
|
+
value: merged
|
|
350
|
+
});
|
|
351
|
+
return _context3.abrupt("return", merged);
|
|
352
|
+
case 1:
|
|
353
|
+
if (!(_this3._state === StreamStateConstant.ERROR || _this3._state === StreamStateConstant.CANCELLED)) {
|
|
354
|
+
_context3.next = 2;
|
|
355
|
+
break;
|
|
356
|
+
}
|
|
357
|
+
throw _this3.terminalError || new Error(Messages.STREAM_READ_ERROR);
|
|
358
|
+
case 2:
|
|
359
|
+
if (!(_this3._state === StreamStateConstant.PENDING || _this3._state === StreamStateConstant.STREAMING)) {
|
|
360
|
+
_context3.next = 4;
|
|
361
|
+
break;
|
|
362
|
+
}
|
|
363
|
+
if (_this3.chunks.length === 0) {
|
|
364
|
+
_this3.requestMore(1);
|
|
365
|
+
}
|
|
366
|
+
_context3.next = 3;
|
|
367
|
+
return _this3.waitForChangeWithIdleTimeout();
|
|
368
|
+
case 3:
|
|
369
|
+
_context3.next = 2;
|
|
370
|
+
break;
|
|
371
|
+
case 4:
|
|
372
|
+
if (!(_this3._state === StreamStateConstant.ENDED)) {
|
|
373
|
+
_context3.next = 5;
|
|
374
|
+
break;
|
|
375
|
+
}
|
|
376
|
+
_merged = _this3.mergeChunks();
|
|
377
|
+
_this3.emit(StreamEvent.READ, {
|
|
378
|
+
value: _merged
|
|
379
|
+
});
|
|
380
|
+
return _context3.abrupt("return", _merged);
|
|
381
|
+
case 5:
|
|
382
|
+
throw _this3.terminalError || new Error(Messages.STREAM_READ_ERROR);
|
|
383
|
+
case 6:
|
|
384
|
+
case "end":
|
|
385
|
+
return _context3.stop();
|
|
386
|
+
}
|
|
387
|
+
}, _callee3);
|
|
388
|
+
}))();
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
/**
|
|
392
|
+
* Read all chunks as an array
|
|
393
|
+
*/
|
|
394
|
+
readAll() {
|
|
395
|
+
var _this4 = this;
|
|
396
|
+
return _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee4() {
|
|
397
|
+
var list, _list;
|
|
398
|
+
return _regeneratorRuntime.wrap(function (_context4) {
|
|
399
|
+
while (1) switch (_context4.prev = _context4.next) {
|
|
400
|
+
case 0:
|
|
401
|
+
if (!(_this4._state === StreamStateConstant.ENDED)) {
|
|
402
|
+
_context4.next = 1;
|
|
403
|
+
break;
|
|
404
|
+
}
|
|
405
|
+
list = _this4.chunks.slice();
|
|
406
|
+
_this4.emit(StreamEvent.READ, {
|
|
407
|
+
value: list
|
|
408
|
+
});
|
|
409
|
+
return _context4.abrupt("return", list);
|
|
410
|
+
case 1:
|
|
411
|
+
if (!(_this4._state === StreamStateConstant.ERROR || _this4._state === StreamStateConstant.CANCELLED)) {
|
|
412
|
+
_context4.next = 2;
|
|
413
|
+
break;
|
|
414
|
+
}
|
|
415
|
+
throw _this4.terminalError || new Error(Messages.STREAM_READ_ERROR);
|
|
416
|
+
case 2:
|
|
417
|
+
if (!(_this4._state === StreamStateConstant.PENDING || _this4._state === StreamStateConstant.STREAMING)) {
|
|
418
|
+
_context4.next = 4;
|
|
419
|
+
break;
|
|
420
|
+
}
|
|
421
|
+
_this4.schedulePullIfNeeded();
|
|
422
|
+
_context4.next = 3;
|
|
423
|
+
return _this4.waitForChangeWithIdleTimeout();
|
|
424
|
+
case 3:
|
|
425
|
+
_context4.next = 2;
|
|
426
|
+
break;
|
|
427
|
+
case 4:
|
|
428
|
+
if (!(_this4._state === StreamStateConstant.ENDED)) {
|
|
429
|
+
_context4.next = 5;
|
|
430
|
+
break;
|
|
431
|
+
}
|
|
432
|
+
_list = _this4.chunks.slice();
|
|
433
|
+
_this4.emit(StreamEvent.READ, {
|
|
434
|
+
value: _list
|
|
435
|
+
});
|
|
436
|
+
return _context4.abrupt("return", _list);
|
|
437
|
+
case 5:
|
|
438
|
+
throw _this4.terminalError || new Error(Messages.STREAM_READ_ERROR);
|
|
439
|
+
case 6:
|
|
440
|
+
case "end":
|
|
441
|
+
return _context4.stop();
|
|
442
|
+
}
|
|
443
|
+
}, _callee4);
|
|
444
|
+
}))();
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
/**
|
|
448
|
+
* Async iterator
|
|
449
|
+
*/
|
|
450
|
+
[Symbol.asyncIterator]() {
|
|
451
|
+
var index = 0;
|
|
452
|
+
var stream = this;
|
|
453
|
+
return {
|
|
454
|
+
next() {
|
|
455
|
+
return _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee5() {
|
|
456
|
+
var value;
|
|
457
|
+
return _regeneratorRuntime.wrap(function (_context5) {
|
|
458
|
+
while (1) switch (_context5.prev = _context5.next) {
|
|
459
|
+
case 0:
|
|
460
|
+
if (!(index >= stream.chunks.length)) {
|
|
461
|
+
_context5.next = 3;
|
|
462
|
+
break;
|
|
463
|
+
}
|
|
464
|
+
if (!(stream._state === StreamStateConstant.ENDED || stream._state === StreamStateConstant.ERROR || stream._state === StreamStateConstant.CANCELLED)) {
|
|
465
|
+
_context5.next = 1;
|
|
466
|
+
break;
|
|
467
|
+
}
|
|
468
|
+
return _context5.abrupt("return", {
|
|
469
|
+
done: true,
|
|
470
|
+
value: undefined
|
|
471
|
+
});
|
|
472
|
+
case 1:
|
|
473
|
+
// Consumer is ready: request at least 1 chunk
|
|
474
|
+
stream.requestMore(1);
|
|
475
|
+
_context5.next = 2;
|
|
476
|
+
return stream.waitForChangeWithIdleTimeout();
|
|
477
|
+
case 2:
|
|
478
|
+
_context5.next = 0;
|
|
479
|
+
break;
|
|
480
|
+
case 3:
|
|
481
|
+
value = stream.chunks[index++];
|
|
482
|
+
stream.emit(StreamEvent.READ, {
|
|
483
|
+
value
|
|
484
|
+
});
|
|
485
|
+
if (stream.consume) {
|
|
486
|
+
/**
|
|
487
|
+
* Drop already-consumed chunks to reduce memory usage.
|
|
488
|
+
* Use compaction (slice) to avoid O(n) shift per chunk.
|
|
489
|
+
*/
|
|
490
|
+
if (index > 128) {
|
|
491
|
+
stream.chunks = stream.chunks.slice(index);
|
|
492
|
+
index = 0;
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
return _context5.abrupt("return", {
|
|
496
|
+
done: false,
|
|
497
|
+
value
|
|
498
|
+
});
|
|
499
|
+
case 4:
|
|
500
|
+
case "end":
|
|
501
|
+
return _context5.stop();
|
|
502
|
+
}
|
|
503
|
+
}, _callee5);
|
|
504
|
+
}))();
|
|
505
|
+
}
|
|
506
|
+
};
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
/**
|
|
510
|
+
* Abort stream (is alias of cancel method)
|
|
511
|
+
*/
|
|
512
|
+
abort(reason) {
|
|
513
|
+
this.cancelInternal(reason, true);
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
/**
|
|
517
|
+
* Cancel stream
|
|
518
|
+
*/
|
|
519
|
+
cancel(reason) {
|
|
520
|
+
this.cancelInternal(reason, true);
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
/**
|
|
524
|
+
* Listen for stream end
|
|
525
|
+
*/
|
|
526
|
+
onEnd(callback) {
|
|
527
|
+
this.onEndCallback = callback;
|
|
528
|
+
if (this._state === StreamStateConstant.ENDED) {
|
|
529
|
+
callback();
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
/**
|
|
534
|
+
* Listen for stream error
|
|
535
|
+
*/
|
|
536
|
+
onError(callback) {
|
|
537
|
+
this.onErrorCallback = callback;
|
|
538
|
+
}
|
|
539
|
+
}
|