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,204 @@
|
|
|
1
|
+
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
|
|
2
|
+
import "core-js/modules/es.array.from.js";
|
|
3
|
+
import "core-js/modules/es.array.iterator.js";
|
|
4
|
+
import "core-js/modules/es.map.js";
|
|
5
|
+
import "core-js/modules/es.promise.js";
|
|
6
|
+
import "core-js/modules/es.set.js";
|
|
7
|
+
import "core-js/modules/web.dom-collections.for-each.js";
|
|
8
|
+
import "core-js/modules/web.dom-collections.iterator.js";
|
|
9
|
+
import { StreamState as StreamStateConstant, Messages, StreamEvent } from '../constants';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Shared stream core (internal).
|
|
13
|
+
*
|
|
14
|
+
* - Read side: buffer + waiters + consume compaction
|
|
15
|
+
* - Common: state + terminal error + basic waiter mechanics
|
|
16
|
+
*
|
|
17
|
+
* NOTE:
|
|
18
|
+
* This is an internal abstraction to share logic between Readable/Writable streams.
|
|
19
|
+
* Public API remains `IframeReadableStream` / `IframeWritableStream`.
|
|
20
|
+
*/
|
|
21
|
+
export class IframeStreamCore {
|
|
22
|
+
constructor(streamId, type, chunked, metadata, consume, mode) {
|
|
23
|
+
_defineProperty(this, "_state", StreamStateConstant.PENDING);
|
|
24
|
+
_defineProperty(this, "chunks", []);
|
|
25
|
+
_defineProperty(this, "waiters", []);
|
|
26
|
+
_defineProperty(this, "listeners", new Map());
|
|
27
|
+
this.streamId = streamId;
|
|
28
|
+
this.type = type;
|
|
29
|
+
this.chunked = chunked;
|
|
30
|
+
this.metadata = metadata;
|
|
31
|
+
this.consume = consume;
|
|
32
|
+
this.mode = mode;
|
|
33
|
+
}
|
|
34
|
+
get state() {
|
|
35
|
+
return this._state;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Subscribe to stream events.
|
|
40
|
+
*
|
|
41
|
+
* This is designed for observability (debugging / metrics / UI progress),
|
|
42
|
+
* and should not be used to drive protocol-critical logic.
|
|
43
|
+
*
|
|
44
|
+
* Returns an unsubscribe function.
|
|
45
|
+
*/
|
|
46
|
+
|
|
47
|
+
on(event, listener) {
|
|
48
|
+
if (!this.listeners.has(event)) {
|
|
49
|
+
this.listeners.set(event, new Set());
|
|
50
|
+
}
|
|
51
|
+
this.listeners.get(event).add(listener);
|
|
52
|
+
|
|
53
|
+
// Terminal events: if already reached, fire once immediately for convenience.
|
|
54
|
+
if (event === StreamEvent.END && this._state === StreamStateConstant.ENDED) {
|
|
55
|
+
this.safeCall(listener);
|
|
56
|
+
} else if (event === StreamEvent.ERROR && this._state === StreamStateConstant.ERROR) {
|
|
57
|
+
this.safeCall(listener, {
|
|
58
|
+
error: this.terminalError
|
|
59
|
+
});
|
|
60
|
+
} else if (event === StreamEvent.CANCEL && this._state === StreamStateConstant.CANCELLED) {
|
|
61
|
+
this.safeCall(listener, {
|
|
62
|
+
error: this.terminalError
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
return () => this.off(event, listener);
|
|
66
|
+
}
|
|
67
|
+
once(event, listener) {
|
|
68
|
+
// If already terminal, behave like a synchronous one-shot.
|
|
69
|
+
if (event === StreamEvent.END && this._state === StreamStateConstant.ENDED) {
|
|
70
|
+
this.safeCall(listener);
|
|
71
|
+
return () => {};
|
|
72
|
+
}
|
|
73
|
+
if (event === StreamEvent.ERROR && this._state === StreamStateConstant.ERROR) {
|
|
74
|
+
this.safeCall(listener, {
|
|
75
|
+
error: this.terminalError
|
|
76
|
+
});
|
|
77
|
+
return () => {};
|
|
78
|
+
}
|
|
79
|
+
if (event === StreamEvent.CANCEL && this._state === StreamStateConstant.CANCELLED) {
|
|
80
|
+
this.safeCall(listener, {
|
|
81
|
+
error: this.terminalError
|
|
82
|
+
});
|
|
83
|
+
return () => {};
|
|
84
|
+
}
|
|
85
|
+
var wrapped = payload => {
|
|
86
|
+
this.off(event, wrapped);
|
|
87
|
+
this.safeCall(listener, payload);
|
|
88
|
+
};
|
|
89
|
+
return this.on(event, wrapped);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Unsubscribe from stream events.
|
|
94
|
+
*/
|
|
95
|
+
|
|
96
|
+
off(event, listener) {
|
|
97
|
+
var set = this.listeners.get(event);
|
|
98
|
+
if (!set) return;
|
|
99
|
+
set.delete(listener);
|
|
100
|
+
if (set.size === 0) {
|
|
101
|
+
this.listeners.delete(event);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
emit(event, payload) {
|
|
105
|
+
var set = this.listeners.get(event);
|
|
106
|
+
if (!set || set.size === 0) return;
|
|
107
|
+
// Snapshot to avoid issues if listeners mutate subscriptions.
|
|
108
|
+
var list = Array.from(set);
|
|
109
|
+
list.forEach(fn => this.safeCall(fn, payload));
|
|
110
|
+
}
|
|
111
|
+
clearAllListeners() {
|
|
112
|
+
this.listeners.clear();
|
|
113
|
+
}
|
|
114
|
+
safeCall(fn, payload) {
|
|
115
|
+
try {
|
|
116
|
+
fn(payload);
|
|
117
|
+
} catch (_unused) {
|
|
118
|
+
/** ignore user listener errors */
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
notifyWaiters() {
|
|
122
|
+
if (this.waiters.length === 0) return;
|
|
123
|
+
var list = this.waiters;
|
|
124
|
+
this.waiters = [];
|
|
125
|
+
list.forEach(fn => {
|
|
126
|
+
try {
|
|
127
|
+
fn();
|
|
128
|
+
} catch (_unused2) {
|
|
129
|
+
/** ignore */
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
waitForChange() {
|
|
134
|
+
if (this._state === StreamStateConstant.ENDED || this._state === StreamStateConstant.ERROR || this._state === StreamStateConstant.CANCELLED) {
|
|
135
|
+
return Promise.resolve();
|
|
136
|
+
}
|
|
137
|
+
return new Promise(resolve => {
|
|
138
|
+
this.waiters.push(resolve);
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
fail(error) {
|
|
142
|
+
if (this._state === StreamStateConstant.ENDED || this._state === StreamStateConstant.ERROR) return;
|
|
143
|
+
this._state = StreamStateConstant.ERROR;
|
|
144
|
+
this.terminalError = error;
|
|
145
|
+
this.emit(StreamEvent.ERROR, {
|
|
146
|
+
error
|
|
147
|
+
});
|
|
148
|
+
this.emit(StreamEvent.STATE, {
|
|
149
|
+
state: this._state
|
|
150
|
+
});
|
|
151
|
+
this.notifyWaiters();
|
|
152
|
+
}
|
|
153
|
+
cancel(reason) {
|
|
154
|
+
if (this._state === StreamStateConstant.ENDED || this._state === StreamStateConstant.ERROR || this._state === StreamStateConstant.CANCELLED) return;
|
|
155
|
+
this._state = StreamStateConstant.CANCELLED;
|
|
156
|
+
this.terminalError = new Error(reason || Messages.STREAM_CANCELLED);
|
|
157
|
+
this.emit(StreamEvent.CANCEL, {
|
|
158
|
+
reason,
|
|
159
|
+
error: this.terminalError
|
|
160
|
+
});
|
|
161
|
+
this.emit(StreamEvent.STATE, {
|
|
162
|
+
state: this._state
|
|
163
|
+
});
|
|
164
|
+
this.notifyWaiters();
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Read-side: enqueue a decoded chunk
|
|
169
|
+
*/
|
|
170
|
+
pushChunk(chunk) {
|
|
171
|
+
if (this._state !== StreamStateConstant.PENDING && this._state !== StreamStateConstant.STREAMING) return;
|
|
172
|
+
this._state = StreamStateConstant.STREAMING;
|
|
173
|
+
this.chunks.push(chunk);
|
|
174
|
+
this.emit(StreamEvent.DATA, {
|
|
175
|
+
chunk
|
|
176
|
+
});
|
|
177
|
+
this.emit(StreamEvent.STATE, {
|
|
178
|
+
state: this._state
|
|
179
|
+
});
|
|
180
|
+
this.notifyWaiters();
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Read-side: mark end
|
|
185
|
+
*/
|
|
186
|
+
end() {
|
|
187
|
+
if (this._state === StreamStateConstant.ENDED) return;
|
|
188
|
+
this._state = StreamStateConstant.ENDED;
|
|
189
|
+
this.emit(StreamEvent.END);
|
|
190
|
+
this.emit(StreamEvent.STATE, {
|
|
191
|
+
state: this._state
|
|
192
|
+
});
|
|
193
|
+
this.notifyWaiters();
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Handle inbound message (shared).
|
|
198
|
+
* - ReadableStream will use DATA/END/ERROR/CANCEL
|
|
199
|
+
* - WritableStream may use PULL/ACK/CANCEL separately, so this is intentionally minimal.
|
|
200
|
+
*/
|
|
201
|
+
handleInboundMessage(_data) {
|
|
202
|
+
// Intentionally left blank in core.
|
|
203
|
+
}
|
|
204
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|