request-iframe 0.1.0 → 0.2.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.
- package/QUICKSTART.CN.md +4 -2
- package/QUICKSTART.md +4 -2
- package/README.CN.md +302 -54
- package/README.md +281 -36
- package/cdn/request-iframe-react.umd.js +3354 -0
- package/cdn/request-iframe-react.umd.js.map +1 -0
- package/cdn/request-iframe-react.umd.min.js +2 -0
- package/cdn/request-iframe-react.umd.min.js.map +1 -0
- package/cdn/request-iframe.umd.js +19735 -0
- package/cdn/request-iframe.umd.js.map +1 -0
- package/cdn/request-iframe.umd.min.js +4 -0
- package/cdn/request-iframe.umd.min.js.map +1 -0
- package/esm/api/client.js +31 -22
- package/esm/api/endpoint.js +229 -0
- package/esm/api/server.js +19 -9
- package/esm/constants/debug.js +17 -0
- package/esm/constants/index.js +115 -66
- package/esm/constants/log.js +11 -0
- package/esm/constants/messages.js +6 -1
- package/esm/constants/warn-once.js +15 -0
- package/esm/endpoint/facade.js +390 -0
- package/esm/endpoint/heartbeat/heartbeat.js +60 -0
- package/esm/endpoint/heartbeat/ping.js +20 -0
- package/esm/endpoint/index.js +13 -0
- package/esm/endpoint/infra/hub.js +316 -0
- package/esm/endpoint/infra/inbox.js +232 -0
- package/esm/endpoint/infra/outbox.js +408 -0
- package/esm/endpoint/stream/dispatcher.js +58 -0
- package/esm/endpoint/stream/errors.js +27 -0
- package/esm/endpoint/stream/factory.js +76 -0
- package/esm/endpoint/stream/file-auto-resolve.js +34 -0
- package/esm/endpoint/stream/file-writable.js +105 -0
- package/esm/endpoint/stream/handler.js +26 -0
- package/esm/{core → impl}/client.js +243 -320
- package/esm/{core → impl}/response.js +120 -154
- package/esm/impl/server.js +568 -0
- package/esm/index.js +13 -6
- package/esm/message/ack.js +27 -0
- package/esm/message/channel-cache.js +108 -0
- package/esm/message/channel.js +92 -5
- package/esm/message/dispatcher.js +149 -98
- package/esm/stream/error.js +22 -0
- package/esm/stream/index.js +3 -1
- package/esm/stream/readable-stream.js +101 -26
- package/esm/stream/stream-core.js +121 -3
- package/esm/stream/writable-stream.js +368 -43
- package/esm/utils/ack.js +36 -0
- package/esm/utils/blob.js +16 -0
- package/esm/utils/cache.js +25 -76
- package/esm/utils/content-type.js +81 -0
- package/esm/utils/debug.js +157 -180
- package/esm/utils/hooks.js +130 -0
- package/esm/utils/id.js +14 -0
- package/esm/utils/iframe.js +20 -0
- package/esm/utils/index.js +12 -162
- package/esm/utils/is.js +3 -0
- package/esm/utils/logger.js +55 -0
- package/esm/utils/origin.js +3 -1
- package/esm/utils/promise.js +3 -0
- package/esm/utils/window.js +31 -0
- package/library/api/client.d.ts.map +1 -1
- package/library/api/client.js +32 -23
- package/library/api/endpoint.d.ts +23 -0
- package/library/api/endpoint.d.ts.map +1 -0
- package/library/api/endpoint.js +235 -0
- package/library/api/server.d.ts +4 -1
- package/library/api/server.d.ts.map +1 -1
- package/library/api/server.js +19 -9
- package/library/constants/debug.d.ts +18 -0
- package/library/constants/debug.d.ts.map +1 -0
- package/library/constants/debug.js +23 -0
- package/library/constants/index.d.ts +58 -7
- package/library/constants/index.d.ts.map +1 -1
- package/library/constants/index.js +143 -67
- package/library/constants/log.d.ts +12 -0
- package/library/constants/log.d.ts.map +1 -0
- package/library/constants/log.js +17 -0
- package/library/constants/messages.d.ts +6 -1
- package/library/constants/messages.d.ts.map +1 -1
- package/library/constants/messages.js +6 -1
- package/library/constants/warn-once.d.ts +12 -0
- package/library/constants/warn-once.d.ts.map +1 -0
- package/library/constants/warn-once.js +22 -0
- package/library/endpoint/facade.d.ts +238 -0
- package/library/endpoint/facade.d.ts.map +1 -0
- package/library/endpoint/facade.js +398 -0
- package/library/endpoint/heartbeat/heartbeat.d.ts +34 -0
- package/library/endpoint/heartbeat/heartbeat.d.ts.map +1 -0
- package/library/endpoint/heartbeat/heartbeat.js +67 -0
- package/library/endpoint/heartbeat/ping.d.ts +18 -0
- package/library/endpoint/heartbeat/ping.d.ts.map +1 -0
- package/library/endpoint/heartbeat/ping.js +26 -0
- package/library/endpoint/index.d.ts +16 -0
- package/library/endpoint/index.d.ts.map +1 -0
- package/library/endpoint/index.js +114 -0
- package/library/endpoint/infra/hub.d.ts +170 -0
- package/library/endpoint/infra/hub.d.ts.map +1 -0
- package/library/endpoint/infra/hub.js +323 -0
- package/library/endpoint/infra/inbox.d.ts +73 -0
- package/library/endpoint/infra/inbox.d.ts.map +1 -0
- package/library/endpoint/infra/inbox.js +239 -0
- package/library/endpoint/infra/outbox.d.ts +149 -0
- package/library/endpoint/infra/outbox.d.ts.map +1 -0
- package/library/endpoint/infra/outbox.js +415 -0
- package/library/endpoint/stream/dispatcher.d.ts +33 -0
- package/library/endpoint/stream/dispatcher.d.ts.map +1 -0
- package/library/endpoint/stream/dispatcher.js +66 -0
- package/library/endpoint/stream/errors.d.ts +20 -0
- package/library/endpoint/stream/errors.d.ts.map +1 -0
- package/library/endpoint/stream/errors.js +32 -0
- package/library/endpoint/stream/factory.d.ts +44 -0
- package/library/endpoint/stream/factory.d.ts.map +1 -0
- package/library/endpoint/stream/factory.js +82 -0
- package/library/endpoint/stream/file-auto-resolve.d.ts +26 -0
- package/library/endpoint/stream/file-auto-resolve.d.ts.map +1 -0
- package/library/endpoint/stream/file-auto-resolve.js +41 -0
- package/library/endpoint/stream/file-writable.d.ts +33 -0
- package/library/endpoint/stream/file-writable.d.ts.map +1 -0
- package/library/endpoint/stream/file-writable.js +115 -0
- package/library/endpoint/stream/handler.d.ts +20 -0
- package/library/endpoint/stream/handler.d.ts.map +1 -0
- package/library/endpoint/stream/handler.js +32 -0
- package/library/{core → impl}/client.d.ts +16 -13
- package/library/impl/client.d.ts.map +1 -0
- package/library/{core → impl}/client.js +254 -333
- package/library/{core → impl}/request.d.ts.map +1 -1
- package/library/{core → impl}/response.d.ts +7 -12
- package/library/impl/response.d.ts.map +1 -0
- package/library/{core → impl}/response.js +120 -154
- package/library/{core → impl}/server.d.ts +26 -55
- package/library/impl/server.d.ts.map +1 -0
- package/library/impl/server.js +575 -0
- package/library/index.d.ts +13 -6
- package/library/index.d.ts.map +1 -1
- package/library/index.js +16 -16
- package/library/message/ack.d.ts +15 -0
- package/library/message/ack.d.ts.map +1 -0
- package/library/message/ack.js +33 -0
- package/library/message/channel-cache.d.ts +26 -0
- package/library/message/channel-cache.d.ts.map +1 -0
- package/library/message/channel-cache.js +115 -0
- package/library/message/channel.d.ts +53 -6
- package/library/message/channel.d.ts.map +1 -1
- package/library/message/channel.js +96 -9
- package/library/message/dispatcher.d.ts +17 -0
- package/library/message/dispatcher.d.ts.map +1 -1
- package/library/message/dispatcher.js +149 -98
- package/library/stream/error.d.ts +24 -0
- package/library/stream/error.d.ts.map +1 -0
- package/library/stream/error.js +29 -0
- package/library/stream/index.d.ts +4 -1
- package/library/stream/index.d.ts.map +1 -1
- package/library/stream/index.js +7 -4
- package/library/stream/readable-stream.d.ts.map +1 -1
- package/library/stream/readable-stream.js +102 -27
- package/library/stream/stream-core.d.ts +22 -1
- package/library/stream/stream-core.d.ts.map +1 -1
- package/library/stream/stream-core.js +120 -2
- package/library/stream/types.d.ts +115 -2
- package/library/stream/types.d.ts.map +1 -1
- package/library/stream/writable-stream.d.ts +20 -2
- package/library/stream/writable-stream.d.ts.map +1 -1
- package/library/stream/writable-stream.js +366 -41
- package/library/types/index.d.ts +17 -22
- 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/blob.d.ts +3 -0
- package/library/utils/blob.d.ts.map +1 -0
- package/library/utils/blob.js +22 -0
- package/library/utils/cache.d.ts +10 -20
- package/library/utils/cache.d.ts.map +1 -1
- package/library/utils/cache.js +25 -79
- package/library/utils/content-type.d.ts +13 -0
- package/library/utils/content-type.d.ts.map +1 -0
- package/library/utils/content-type.js +87 -0
- package/library/utils/debug.d.ts.map +1 -1
- package/library/utils/debug.js +156 -178
- package/library/utils/hooks.d.ts +30 -0
- package/library/utils/hooks.d.ts.map +1 -0
- package/library/utils/hooks.js +139 -0
- package/library/utils/id.d.ts +9 -0
- package/library/utils/id.d.ts.map +1 -0
- package/library/utils/id.js +21 -0
- package/library/utils/iframe.d.ts +5 -0
- package/library/utils/iframe.d.ts.map +1 -0
- package/library/utils/iframe.js +25 -0
- package/library/utils/index.d.ts +7 -34
- package/library/utils/index.d.ts.map +1 -1
- package/library/utils/index.js +58 -193
- package/library/utils/is.d.ts +2 -0
- package/library/utils/is.d.ts.map +1 -0
- package/library/utils/is.js +9 -0
- package/library/utils/logger.d.ts +13 -0
- package/library/utils/logger.d.ts.map +1 -0
- package/library/utils/logger.js +63 -0
- package/library/utils/origin.d.ts.map +1 -1
- package/library/utils/origin.js +2 -1
- package/library/utils/promise.d.ts +2 -0
- package/library/utils/promise.d.ts.map +1 -0
- package/library/utils/promise.js +9 -0
- package/library/utils/window.d.ts +2 -0
- package/library/utils/window.d.ts.map +1 -0
- package/library/utils/window.js +38 -0
- package/package.json +49 -2
- package/react/package.json +2 -1
- package/esm/core/client-server.js +0 -329
- package/esm/core/server.js +0 -767
- package/esm/utils/ack-meta.js +0 -53
- package/library/core/client-server.d.ts +0 -106
- package/library/core/client-server.d.ts.map +0 -1
- package/library/core/client-server.js +0 -336
- package/library/core/client.d.ts.map +0 -1
- package/library/core/response.d.ts.map +0 -1
- package/library/core/server.d.ts.map +0 -1
- package/library/core/server.js +0 -772
- package/library/utils/ack-meta.d.ts +0 -2
- package/library/utils/ack-meta.d.ts.map +0 -1
- package/library/utils/ack-meta.js +0 -59
- /package/esm/{core → impl}/request.js +0 -0
- /package/library/{core → impl}/request.d.ts +0 -0
- /package/library/{core → impl}/request.js +0 -0
package/esm/utils/cache.js
CHANGED
|
@@ -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
|
|
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
|
-
|
|
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
|
+
}
|