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.
- package/QUICKSTART.CN.md +269 -0
- package/QUICKSTART.md +269 -0
- package/README.CN.md +1369 -0
- package/README.md +1016 -0
- package/library/__tests__/interceptors.test.ts +124 -0
- package/library/__tests__/requestIframe.test.ts +2216 -0
- package/library/__tests__/stream.test.ts +650 -0
- package/library/__tests__/utils.test.ts +433 -0
- package/library/api/client.d.ts +16 -0
- package/library/api/client.d.ts.map +1 -0
- package/library/api/client.js +72 -0
- package/library/api/server.d.ts +16 -0
- package/library/api/server.d.ts.map +1 -0
- package/library/api/server.js +44 -0
- package/library/constants/index.d.ts +209 -0
- package/library/constants/index.d.ts.map +1 -0
- package/library/constants/index.js +260 -0
- package/library/constants/messages.d.ts +80 -0
- package/library/constants/messages.d.ts.map +1 -0
- package/library/constants/messages.js +123 -0
- package/library/core/client.d.ts +99 -0
- package/library/core/client.d.ts.map +1 -0
- package/library/core/client.js +440 -0
- package/library/core/message-handler.d.ts +110 -0
- package/library/core/message-handler.d.ts.map +1 -0
- package/library/core/message-handler.js +320 -0
- package/library/core/request-response.d.ts +59 -0
- package/library/core/request-response.d.ts.map +1 -0
- package/library/core/request-response.js +337 -0
- package/library/core/request.d.ts +17 -0
- package/library/core/request.d.ts.map +1 -0
- package/library/core/request.js +34 -0
- package/library/core/response.d.ts +51 -0
- package/library/core/response.d.ts.map +1 -0
- package/library/core/response.js +323 -0
- package/library/core/server-base.d.ts +86 -0
- package/library/core/server-base.d.ts.map +1 -0
- package/library/core/server-base.js +257 -0
- package/library/core/server-client.d.ts +99 -0
- package/library/core/server-client.d.ts.map +1 -0
- package/library/core/server-client.js +256 -0
- package/library/core/server.d.ts +82 -0
- package/library/core/server.d.ts.map +1 -0
- package/library/core/server.js +338 -0
- package/library/index.d.ts +16 -0
- package/library/index.d.ts.map +1 -0
- package/library/index.js +211 -0
- package/library/interceptors/index.d.ts +41 -0
- package/library/interceptors/index.d.ts.map +1 -0
- package/library/interceptors/index.js +126 -0
- package/library/message/channel.d.ts +107 -0
- package/library/message/channel.d.ts.map +1 -0
- package/library/message/channel.js +184 -0
- package/library/message/dispatcher.d.ts +119 -0
- package/library/message/dispatcher.d.ts.map +1 -0
- package/library/message/dispatcher.js +249 -0
- package/library/message/index.d.ts +5 -0
- package/library/message/index.d.ts.map +1 -0
- package/library/message/index.js +25 -0
- package/library/stream/file-stream.d.ts +48 -0
- package/library/stream/file-stream.d.ts.map +1 -0
- package/library/stream/file-stream.js +240 -0
- package/library/stream/index.d.ts +15 -0
- package/library/stream/index.d.ts.map +1 -0
- package/library/stream/index.js +83 -0
- package/library/stream/readable-stream.d.ts +83 -0
- package/library/stream/readable-stream.d.ts.map +1 -0
- package/library/stream/readable-stream.js +249 -0
- package/library/stream/types.d.ts +165 -0
- package/library/stream/types.d.ts.map +1 -0
- package/library/stream/types.js +5 -0
- package/library/stream/writable-stream.d.ts +60 -0
- package/library/stream/writable-stream.d.ts.map +1 -0
- package/library/stream/writable-stream.js +348 -0
- package/library/types/index.d.ts +408 -0
- package/library/types/index.d.ts.map +1 -0
- package/library/types/index.js +5 -0
- package/library/utils/cache.d.ts +19 -0
- package/library/utils/cache.d.ts.map +1 -0
- package/library/utils/cache.js +83 -0
- package/library/utils/cookie.d.ts +117 -0
- package/library/utils/cookie.d.ts.map +1 -0
- package/library/utils/cookie.js +365 -0
- package/library/utils/debug.d.ts +11 -0
- package/library/utils/debug.d.ts.map +1 -0
- package/library/utils/debug.js +162 -0
- package/library/utils/index.d.ts +13 -0
- package/library/utils/index.d.ts.map +1 -0
- package/library/utils/index.js +132 -0
- package/library/utils/path-match.d.ts +17 -0
- package/library/utils/path-match.d.ts.map +1 -0
- package/library/utils/path-match.js +90 -0
- package/library/utils/protocol.d.ts +61 -0
- package/library/utils/protocol.d.ts.map +1 -0
- package/library/utils/protocol.js +169 -0
- package/package.json +58 -0
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
Object.defineProperty(exports, "__esModule", {
|
|
5
|
+
value: true
|
|
6
|
+
});
|
|
7
|
+
exports.ResponseInterceptorManager = exports.RequestInterceptorManager = exports.InterceptorManager = void 0;
|
|
8
|
+
exports.runRequestInterceptors = runRequestInterceptors;
|
|
9
|
+
exports.runResponseInterceptors = runResponseInterceptors;
|
|
10
|
+
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
|
|
11
|
+
require("core-js/modules/es.promise.js");
|
|
12
|
+
require("core-js/modules/web.dom-collections.for-each.js");
|
|
13
|
+
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
|
|
14
|
+
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
15
|
+
/**
|
|
16
|
+
* Interceptor manager
|
|
17
|
+
*/
|
|
18
|
+
class InterceptorManager {
|
|
19
|
+
constructor() {
|
|
20
|
+
(0, _defineProperty2.default)(this, "handlers", []);
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Add interceptor
|
|
24
|
+
*/
|
|
25
|
+
use(fulfilled, rejected) {
|
|
26
|
+
this.handlers.push({
|
|
27
|
+
fulfilled,
|
|
28
|
+
rejected
|
|
29
|
+
});
|
|
30
|
+
return this.handlers.length - 1;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Remove interceptor
|
|
35
|
+
*/
|
|
36
|
+
eject(id) {
|
|
37
|
+
if (this.handlers[id]) {
|
|
38
|
+
this.handlers[id] = null;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Iterate over all interceptors
|
|
44
|
+
*/
|
|
45
|
+
forEach(fn) {
|
|
46
|
+
this.handlers.forEach(h => {
|
|
47
|
+
if (h !== null) {
|
|
48
|
+
fn(h);
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Request interceptor manager
|
|
56
|
+
*/
|
|
57
|
+
exports.InterceptorManager = InterceptorManager;
|
|
58
|
+
class RequestInterceptorManager extends InterceptorManager {}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Response interceptor manager
|
|
62
|
+
*/
|
|
63
|
+
exports.RequestInterceptorManager = RequestInterceptorManager;
|
|
64
|
+
class ResponseInterceptorManager extends InterceptorManager {}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Execute request interceptor chain
|
|
68
|
+
*/
|
|
69
|
+
exports.ResponseInterceptorManager = ResponseInterceptorManager;
|
|
70
|
+
function runRequestInterceptors(_x, _x2) {
|
|
71
|
+
return _runRequestInterceptors.apply(this, arguments);
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Execute response interceptor chain
|
|
75
|
+
*/
|
|
76
|
+
function _runRequestInterceptors() {
|
|
77
|
+
_runRequestInterceptors = (0, _asyncToGenerator2.default)(/*#__PURE__*/_regenerator.default.mark(function _callee(interceptors, config) {
|
|
78
|
+
var promise;
|
|
79
|
+
return _regenerator.default.wrap(function (_context) {
|
|
80
|
+
while (1) switch (_context.prev = _context.next) {
|
|
81
|
+
case 0:
|
|
82
|
+
promise = Promise.resolve(config);
|
|
83
|
+
interceptors.forEach(interceptor => {
|
|
84
|
+
promise = promise.then(config => interceptor.fulfilled(config), error => {
|
|
85
|
+
if (interceptor.rejected) {
|
|
86
|
+
return interceptor.rejected(error);
|
|
87
|
+
}
|
|
88
|
+
return Promise.reject(error);
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
return _context.abrupt("return", promise);
|
|
92
|
+
case 1:
|
|
93
|
+
case "end":
|
|
94
|
+
return _context.stop();
|
|
95
|
+
}
|
|
96
|
+
}, _callee);
|
|
97
|
+
}));
|
|
98
|
+
return _runRequestInterceptors.apply(this, arguments);
|
|
99
|
+
}
|
|
100
|
+
function runResponseInterceptors(_x3, _x4) {
|
|
101
|
+
return _runResponseInterceptors.apply(this, arguments);
|
|
102
|
+
}
|
|
103
|
+
function _runResponseInterceptors() {
|
|
104
|
+
_runResponseInterceptors = (0, _asyncToGenerator2.default)(/*#__PURE__*/_regenerator.default.mark(function _callee2(interceptors, response) {
|
|
105
|
+
var promise;
|
|
106
|
+
return _regenerator.default.wrap(function (_context2) {
|
|
107
|
+
while (1) switch (_context2.prev = _context2.next) {
|
|
108
|
+
case 0:
|
|
109
|
+
promise = Promise.resolve(response);
|
|
110
|
+
interceptors.forEach(interceptor => {
|
|
111
|
+
promise = promise.then(response => interceptor.fulfilled(response), error => {
|
|
112
|
+
if (interceptor.rejected) {
|
|
113
|
+
return interceptor.rejected(error);
|
|
114
|
+
}
|
|
115
|
+
return Promise.reject(error);
|
|
116
|
+
});
|
|
117
|
+
});
|
|
118
|
+
return _context2.abrupt("return", promise);
|
|
119
|
+
case 1:
|
|
120
|
+
case "end":
|
|
121
|
+
return _context2.stop();
|
|
122
|
+
}
|
|
123
|
+
}, _callee2);
|
|
124
|
+
}));
|
|
125
|
+
return _runResponseInterceptors.apply(this, arguments);
|
|
126
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { PostMessageData } from '../types';
|
|
2
|
+
/**
|
|
3
|
+
* Message context (extracted from MessageEvent, transport-agnostic)
|
|
4
|
+
*/
|
|
5
|
+
export interface MessageContext {
|
|
6
|
+
/** Source window (where the message came from) */
|
|
7
|
+
source?: Window;
|
|
8
|
+
/** Origin of the message */
|
|
9
|
+
origin: string;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Message receiver callback
|
|
13
|
+
*/
|
|
14
|
+
export type MessageReceiver = (data: PostMessageData, context: MessageContext) => void;
|
|
15
|
+
/**
|
|
16
|
+
* Channel type constants
|
|
17
|
+
*/
|
|
18
|
+
export declare const ChannelType: {
|
|
19
|
+
/** postMessage channel type */
|
|
20
|
+
readonly POST_MESSAGE: "postMessage";
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* Channel type
|
|
24
|
+
*/
|
|
25
|
+
export type ChannelType = typeof ChannelType[keyof typeof ChannelType];
|
|
26
|
+
/**
|
|
27
|
+
* MessageChannel - Low-level communication channel for postMessage
|
|
28
|
+
*
|
|
29
|
+
* Responsibilities:
|
|
30
|
+
* - Listening to window.postMessage events
|
|
31
|
+
* - Filtering messages by secretKey
|
|
32
|
+
* - Extracting transport-specific information (MessageEvent) into generic MessageContext
|
|
33
|
+
* - Forwarding received messages to registered receivers
|
|
34
|
+
* - Sending messages to target windows
|
|
35
|
+
*
|
|
36
|
+
* This is the low-level communication layer that handles postMessage directly.
|
|
37
|
+
* All transport-specific details (like MessageEvent) are encapsulated here.
|
|
38
|
+
*/
|
|
39
|
+
export declare class MessageChannel {
|
|
40
|
+
/** Channel type (used for cache isolation) */
|
|
41
|
+
readonly type: ChannelType;
|
|
42
|
+
/** Secret key for message isolation */
|
|
43
|
+
readonly secretKey?: string;
|
|
44
|
+
/** Message receiver callbacks (support multiple receivers) */
|
|
45
|
+
private receivers;
|
|
46
|
+
/** Message listener function (bound to this) */
|
|
47
|
+
private readonly boundOnMessage;
|
|
48
|
+
/** Reference count (for cache management) */
|
|
49
|
+
private refCount;
|
|
50
|
+
constructor(secretKey?: string, type?: ChannelType);
|
|
51
|
+
/**
|
|
52
|
+
* Add message receiver callback
|
|
53
|
+
* When a message is received, it will be forwarded to all registered receivers
|
|
54
|
+
*/
|
|
55
|
+
addReceiver(receiver: MessageReceiver): void;
|
|
56
|
+
/**
|
|
57
|
+
* Remove message receiver callback
|
|
58
|
+
*/
|
|
59
|
+
removeReceiver(receiver: MessageReceiver): void;
|
|
60
|
+
/**
|
|
61
|
+
* Increment reference count (for cache management)
|
|
62
|
+
*/
|
|
63
|
+
addRef(): void;
|
|
64
|
+
/**
|
|
65
|
+
* Decrement reference count (for cache management)
|
|
66
|
+
* @returns current reference count
|
|
67
|
+
*/
|
|
68
|
+
release(): number;
|
|
69
|
+
/**
|
|
70
|
+
* Get reference count (for cache management)
|
|
71
|
+
*/
|
|
72
|
+
getRefCount(): number;
|
|
73
|
+
/**
|
|
74
|
+
* Extract MessageContext from MessageEvent
|
|
75
|
+
* This encapsulates transport-specific details
|
|
76
|
+
*/
|
|
77
|
+
private extractContext;
|
|
78
|
+
/**
|
|
79
|
+
* Message handling entry point
|
|
80
|
+
*/
|
|
81
|
+
private onMessage;
|
|
82
|
+
/**
|
|
83
|
+
* Send raw message to target window
|
|
84
|
+
* @param target target window
|
|
85
|
+
* @param message message data (already formatted as PostMessageData)
|
|
86
|
+
* @param targetOrigin target origin (defaults to '*')
|
|
87
|
+
*/
|
|
88
|
+
send(target: Window, message: PostMessageData, targetOrigin?: string): void;
|
|
89
|
+
/**
|
|
90
|
+
* Send typed message to target window (creates PostMessageData automatically)
|
|
91
|
+
* @param target target window
|
|
92
|
+
* @param targetOrigin target origin
|
|
93
|
+
* @param type message type
|
|
94
|
+
* @param requestId request ID
|
|
95
|
+
* @param data additional data
|
|
96
|
+
*/
|
|
97
|
+
sendMessage(target: Window, targetOrigin: string, type: PostMessageData['type'], requestId: string, data?: Partial<Omit<PostMessageData, '__requestIframe__' | 'type' | 'requestId' | 'timestamp'>>): void;
|
|
98
|
+
/**
|
|
99
|
+
* Add path prefix (for secretKey isolation)
|
|
100
|
+
*/
|
|
101
|
+
prefixPath(path: string): string;
|
|
102
|
+
/**
|
|
103
|
+
* Destroy channel (remove event listener)
|
|
104
|
+
*/
|
|
105
|
+
destroy(): void;
|
|
106
|
+
}
|
|
107
|
+
//# sourceMappingURL=channel.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"channel.d.ts","sourceRoot":"","sources":["../../src/message/channel.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,eAAe,EAChB,MAAM,UAAU,CAAC;AAGlB;;GAEG;AACH,MAAM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ACd,YAAY,EAAE,MAAM,EACpB,IAAI,EAAE,eAAe,CAAC,MAAM,CAAC,EAC7B,SAAS,EAAE,MAAM,EACjB,IAAI,CAAC,EAAE,OAAO,CAAC,IAAI,CAAC,eAAe,EAAE,mBAAmB,GAAG,MAAM,GAAG,WAAW,GAAG,WAAW,CAAC,CAAC,GAC9F,IAAI;IAQP;;OAEG;IACI,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM;IAIvC;;OAEG;IACI,OAAO,IAAI,IAAI;CAIvB"}
|
|
@@ -0,0 +1,184 @@
|
|
|
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
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
6
|
+
Object.defineProperty(exports, "__esModule", {
|
|
7
|
+
value: true
|
|
8
|
+
});
|
|
9
|
+
exports.MessageChannel = exports.ChannelType = void 0;
|
|
10
|
+
require("core-js/modules/es.array.iterator.js");
|
|
11
|
+
require("core-js/modules/es.set.js");
|
|
12
|
+
require("core-js/modules/web.dom-collections.for-each.js");
|
|
13
|
+
require("core-js/modules/web.dom-collections.iterator.js");
|
|
14
|
+
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
15
|
+
var _utils = require("../utils");
|
|
16
|
+
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; }
|
|
17
|
+
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; }
|
|
18
|
+
/**
|
|
19
|
+
* Message context (extracted from MessageEvent, transport-agnostic)
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Message receiver callback
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Channel type constants
|
|
28
|
+
*/
|
|
29
|
+
var ChannelType = exports.ChannelType = {
|
|
30
|
+
/** postMessage channel type */
|
|
31
|
+
POST_MESSAGE: 'postMessage'
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Channel type
|
|
36
|
+
*/
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* MessageChannel - Low-level communication channel for postMessage
|
|
40
|
+
*
|
|
41
|
+
* Responsibilities:
|
|
42
|
+
* - Listening to window.postMessage events
|
|
43
|
+
* - Filtering messages by secretKey
|
|
44
|
+
* - Extracting transport-specific information (MessageEvent) into generic MessageContext
|
|
45
|
+
* - Forwarding received messages to registered receivers
|
|
46
|
+
* - Sending messages to target windows
|
|
47
|
+
*
|
|
48
|
+
* This is the low-level communication layer that handles postMessage directly.
|
|
49
|
+
* All transport-specific details (like MessageEvent) are encapsulated here.
|
|
50
|
+
*/
|
|
51
|
+
class MessageChannel {
|
|
52
|
+
constructor(secretKey, type = ChannelType.POST_MESSAGE) {
|
|
53
|
+
/** Channel type (used for cache isolation) */
|
|
54
|
+
/** Secret key for message isolation */
|
|
55
|
+
/** Message receiver callbacks (support multiple receivers) */
|
|
56
|
+
(0, _defineProperty2.default)(this, "receivers", new Set());
|
|
57
|
+
/** Reference count (for cache management) */
|
|
58
|
+
(0, _defineProperty2.default)(this, "refCount", 0);
|
|
59
|
+
this.type = type;
|
|
60
|
+
this.secretKey = secretKey;
|
|
61
|
+
this.boundOnMessage = this.onMessage.bind(this);
|
|
62
|
+
window.addEventListener('message', this.boundOnMessage);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Add message receiver callback
|
|
67
|
+
* When a message is received, it will be forwarded to all registered receivers
|
|
68
|
+
*/
|
|
69
|
+
addReceiver(receiver) {
|
|
70
|
+
this.receivers.add(receiver);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Remove message receiver callback
|
|
75
|
+
*/
|
|
76
|
+
removeReceiver(receiver) {
|
|
77
|
+
this.receivers.delete(receiver);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Increment reference count (for cache management)
|
|
82
|
+
*/
|
|
83
|
+
addRef() {
|
|
84
|
+
this.refCount++;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Decrement reference count (for cache management)
|
|
89
|
+
* @returns current reference count
|
|
90
|
+
*/
|
|
91
|
+
release() {
|
|
92
|
+
return --this.refCount;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Get reference count (for cache management)
|
|
97
|
+
*/
|
|
98
|
+
getRefCount() {
|
|
99
|
+
return this.refCount;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Extract MessageContext from MessageEvent
|
|
104
|
+
* This encapsulates transport-specific details
|
|
105
|
+
*/
|
|
106
|
+
extractContext(event) {
|
|
107
|
+
return {
|
|
108
|
+
source: event.source,
|
|
109
|
+
origin: event.origin
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Message handling entry point
|
|
115
|
+
*/
|
|
116
|
+
onMessage(event) {
|
|
117
|
+
var data = event.data;
|
|
118
|
+
|
|
119
|
+
// Check if this is a request-iframe framework message (basic format validation)
|
|
120
|
+
if (!(0, _utils.isValidPostMessage)(data)) {
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// secretKey isolation
|
|
125
|
+
if (this.secretKey) {
|
|
126
|
+
if (data.secretKey !== this.secretKey) return;
|
|
127
|
+
} else {
|
|
128
|
+
if (data.secretKey) return;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// Extract context from transport-specific event
|
|
132
|
+
var context = this.extractContext(event);
|
|
133
|
+
|
|
134
|
+
// Forward to all registered receivers
|
|
135
|
+
this.receivers.forEach(receiver => {
|
|
136
|
+
try {
|
|
137
|
+
receiver(data, context);
|
|
138
|
+
} catch (e) {
|
|
139
|
+
console.error('[request-iframe] Receiver error:', e);
|
|
140
|
+
}
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Send raw message to target window
|
|
146
|
+
* @param target target window
|
|
147
|
+
* @param message message data (already formatted as PostMessageData)
|
|
148
|
+
* @param targetOrigin target origin (defaults to '*')
|
|
149
|
+
*/
|
|
150
|
+
send(target, message, targetOrigin = '*') {
|
|
151
|
+
target.postMessage(message, targetOrigin);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Send typed message to target window (creates PostMessageData automatically)
|
|
156
|
+
* @param target target window
|
|
157
|
+
* @param targetOrigin target origin
|
|
158
|
+
* @param type message type
|
|
159
|
+
* @param requestId request ID
|
|
160
|
+
* @param data additional data
|
|
161
|
+
*/
|
|
162
|
+
sendMessage(target, targetOrigin, type, requestId, data) {
|
|
163
|
+
var message = (0, _utils.createPostMessage)(type, requestId, _objectSpread(_objectSpread({}, data), {}, {
|
|
164
|
+
secretKey: this.secretKey
|
|
165
|
+
}));
|
|
166
|
+
this.send(target, message, targetOrigin);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Add path prefix (for secretKey isolation)
|
|
171
|
+
*/
|
|
172
|
+
prefixPath(path) {
|
|
173
|
+
return this.secretKey ? `${this.secretKey}:${path}` : path;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Destroy channel (remove event listener)
|
|
178
|
+
*/
|
|
179
|
+
destroy() {
|
|
180
|
+
window.removeEventListener('message', this.boundOnMessage);
|
|
181
|
+
this.receivers.clear();
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
exports.MessageChannel = MessageChannel;
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { PostMessageData } from '../types';
|
|
2
|
+
import { MessageChannel, type MessageContext } from './channel';
|
|
3
|
+
/**
|
|
4
|
+
* Message handler function type
|
|
5
|
+
*/
|
|
6
|
+
export type MessageHandlerFn = (data: PostMessageData, context: MessageContext) => void;
|
|
7
|
+
/**
|
|
8
|
+
* Message type matcher
|
|
9
|
+
* - string: exact match message type
|
|
10
|
+
* - RegExp: regex match message type
|
|
11
|
+
* - function: custom match function
|
|
12
|
+
*/
|
|
13
|
+
export type MessageTypeMatcher = string | RegExp | ((type: string) => boolean);
|
|
14
|
+
/**
|
|
15
|
+
* Protocol version validator function
|
|
16
|
+
* @param version protocol version in message
|
|
17
|
+
* @returns true if version is compatible, false otherwise
|
|
18
|
+
*/
|
|
19
|
+
export type VersionValidator = (version: number) => boolean;
|
|
20
|
+
/**
|
|
21
|
+
* Message handler registration options
|
|
22
|
+
*/
|
|
23
|
+
export interface HandlerOptions {
|
|
24
|
+
/** Priority (higher number = higher priority, default 0) */
|
|
25
|
+
priority?: number;
|
|
26
|
+
/** Protocol version validator (optional, no validation if not provided) */
|
|
27
|
+
versionValidator?: VersionValidator;
|
|
28
|
+
/** Error handler when version is incompatible (optional) */
|
|
29
|
+
onVersionError?: (data: PostMessageData, context: MessageContext, version: number) => void;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* MessageDispatcher - Message dispatcher for client/server interaction
|
|
33
|
+
*
|
|
34
|
+
* Responsibilities:
|
|
35
|
+
* - Using MessageChannel for receiving and sending messages
|
|
36
|
+
* - Dispatching received messages to registered handlers
|
|
37
|
+
* - Managing handler registration/unregistration
|
|
38
|
+
* - Protocol version validation
|
|
39
|
+
*
|
|
40
|
+
* This is the high-level interface used by client and server implementations.
|
|
41
|
+
* It works with transport-agnostic MessageContext instead of transport-specific MessageEvent.
|
|
42
|
+
*/
|
|
43
|
+
export declare class MessageDispatcher {
|
|
44
|
+
/** Secret key for message isolation */
|
|
45
|
+
readonly secretKey?: string;
|
|
46
|
+
/** Channel type */
|
|
47
|
+
readonly type: MessageChannel['type'];
|
|
48
|
+
/** Underlying message channel */
|
|
49
|
+
private readonly channel;
|
|
50
|
+
/** Message handler list */
|
|
51
|
+
private readonly handlers;
|
|
52
|
+
/** Message receiver callback (bound to this) */
|
|
53
|
+
private readonly boundReceiver;
|
|
54
|
+
/** Reference count (for determining if can be destroyed when cached) */
|
|
55
|
+
private refCount;
|
|
56
|
+
constructor(channel: MessageChannel);
|
|
57
|
+
/**
|
|
58
|
+
* Increment reference count
|
|
59
|
+
*/
|
|
60
|
+
addRef(): void;
|
|
61
|
+
/**
|
|
62
|
+
* Decrement reference count
|
|
63
|
+
* @returns current reference count
|
|
64
|
+
*/
|
|
65
|
+
release(): number;
|
|
66
|
+
/**
|
|
67
|
+
* Get reference count
|
|
68
|
+
*/
|
|
69
|
+
getRefCount(): number;
|
|
70
|
+
/**
|
|
71
|
+
* Register message handler
|
|
72
|
+
* @param matcher message type matcher
|
|
73
|
+
* @param handler handler function
|
|
74
|
+
* @param options registration options (priority, version validation, etc.)
|
|
75
|
+
* @returns function to unregister
|
|
76
|
+
*/
|
|
77
|
+
registerHandler(matcher: MessageTypeMatcher, handler: MessageHandlerFn, options?: HandlerOptions | number): () => void;
|
|
78
|
+
/**
|
|
79
|
+
* Unregister message handler
|
|
80
|
+
*/
|
|
81
|
+
unregisterHandler(handler: MessageHandlerFn): void;
|
|
82
|
+
/**
|
|
83
|
+
* Dispatch message to matching handlers
|
|
84
|
+
*/
|
|
85
|
+
private dispatchMessage;
|
|
86
|
+
/**
|
|
87
|
+
* Check if message type matches
|
|
88
|
+
*/
|
|
89
|
+
private matchType;
|
|
90
|
+
/**
|
|
91
|
+
* Send raw message to target window
|
|
92
|
+
* @param target target window
|
|
93
|
+
* @param message message data (already formatted as PostMessageData)
|
|
94
|
+
* @param targetOrigin target origin (defaults to '*')
|
|
95
|
+
*/
|
|
96
|
+
send(target: Window, message: PostMessageData, targetOrigin?: string): void;
|
|
97
|
+
/**
|
|
98
|
+
* Send typed message to target window (creates PostMessageData automatically)
|
|
99
|
+
* @param target target window
|
|
100
|
+
* @param targetOrigin target origin
|
|
101
|
+
* @param type message type
|
|
102
|
+
* @param requestId request ID
|
|
103
|
+
* @param data additional data
|
|
104
|
+
*/
|
|
105
|
+
sendMessage(target: Window, targetOrigin: string, type: PostMessageData['type'], requestId: string, data?: Partial<Omit<PostMessageData, '__requestIframe__' | 'type' | 'requestId' | 'timestamp'>>): void;
|
|
106
|
+
/**
|
|
107
|
+
* Add path prefix
|
|
108
|
+
*/
|
|
109
|
+
prefixPath(path: string): string;
|
|
110
|
+
/**
|
|
111
|
+
* Get the underlying message channel
|
|
112
|
+
*/
|
|
113
|
+
getChannel(): MessageChannel;
|
|
114
|
+
/**
|
|
115
|
+
* Destroy dispatcher (clear handlers, but don't destroy channel as it may be shared)
|
|
116
|
+
*/
|
|
117
|
+
destroy(): void;
|
|
118
|
+
}
|
|
119
|
+
//# sourceMappingURL=dispatcher.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dispatcher.d.ts","sourceRoot":"","sources":["../../src/message/dispatcher.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,eAAe,EAChB,MAAM,UAAU,CAAC;AAElB,OAAO,EAAE,cAAc,EAAE,KAAK,cAAc,EAAE,MAAM,WAAW,CAAC;AAEhE;;GAEG;AACH,MAAM,MAAM,gBAAgB,GAAG,CAAC,IAAI,EAAE,eAAe,EAAE,OAAO,EAAE,cAAc,KAAK,IAAI,CAAC;AAExF;;;;;GAKG;AACH,MAAM,MAAM,kBAAkB,GAAG,MAAM,GAAG,MAAM,GAAG,CAAC,CA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