request-iframe 0.0.4 → 0.0.6
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 +8 -1
- package/README.md +8 -1
- package/library/constants/index.d.ts +2 -0
- package/library/constants/index.d.ts.map +1 -1
- package/library/constants/index.js +3 -1
- package/library/constants/messages.d.ts +1 -0
- package/library/constants/messages.d.ts.map +1 -1
- package/library/constants/messages.js +1 -0
- package/library/core/client.d.ts +5 -0
- package/library/core/client.d.ts.map +1 -1
- package/library/core/client.js +54 -18
- package/library/core/response.d.ts.map +1 -1
- package/library/core/response.js +51 -32
- package/library/core/server.d.ts.map +1 -1
- package/library/core/server.js +10 -0
- package/library/index.d.ts +1 -1
- package/library/index.d.ts.map +1 -1
- package/library/index.js +7 -0
- package/library/message/channel.d.ts +2 -2
- package/library/message/channel.d.ts.map +1 -1
- package/library/message/channel.js +5 -1
- package/library/message/dispatcher.d.ts +2 -2
- package/library/message/dispatcher.d.ts.map +1 -1
- package/library/message/dispatcher.js +2 -2
- package/library/stream/writable-stream.d.ts.map +1 -1
- package/library/stream/writable-stream.js +81 -39
- package/library/types/index.d.ts +3 -1
- package/library/types/index.d.ts.map +1 -1
- package/library/utils/debug.d.ts.map +1 -1
- package/library/utils/debug.js +6 -2
- package/library/utils/error.d.ts +21 -0
- package/library/utils/error.d.ts.map +1 -0
- package/library/utils/error.js +34 -0
- package/library/utils/index.d.ts +7 -0
- package/library/utils/index.d.ts.map +1 -1
- package/library/utils/index.js +42 -1
- package/package.json +5 -4
- package/react/library/index.d.ts.map +1 -1
- package/react/library/index.js +4 -1
- package/library/__tests__/channel.test.ts +0 -420
- package/library/__tests__/coverage-branches.test.ts +0 -356
- package/library/__tests__/debug.test.ts +0 -588
- package/library/__tests__/dispatcher.test.ts +0 -481
- package/library/__tests__/interceptors.test.ts +0 -146
- package/library/__tests__/requestIframe.test.ts +0 -5384
- package/library/__tests__/server.test.ts +0 -738
- package/library/__tests__/stream.test.ts +0 -681
- package/library/__tests__/utils.test.ts +0 -433
- package/library/api/client.d.js +0 -5
- package/library/api/server.d.js +0 -5
- package/library/constants/index.d.js +0 -36
- package/library/constants/messages.d.js +0 -5
- package/library/core/client.d.js +0 -5
- package/library/core/message-handler.d.ts +0 -110
- package/library/core/message-handler.d.ts.map +0 -1
- package/library/core/message-handler.js +0 -320
- package/library/core/request-response.d.ts +0 -59
- package/library/core/request-response.d.ts.map +0 -1
- package/library/core/request-response.js +0 -337
- package/library/core/request.d.js +0 -5
- package/library/core/response.d.js +0 -5
- package/library/core/server-base.d.ts +0 -86
- package/library/core/server-base.d.ts.map +0 -1
- package/library/core/server-base.js +0 -257
- package/library/core/server-client.d.js +0 -5
- package/library/core/server-client.d.ts +0 -101
- package/library/core/server-client.d.ts.map +0 -1
- package/library/core/server-client.js +0 -266
- package/library/core/server.d.js +0 -5
- package/library/interceptors/index.d.js +0 -5
- package/library/message/channel.d.js +0 -5
- package/library/message/dispatcher.d.js +0 -5
- package/library/message/index.d.js +0 -25
- package/library/stream/file-stream.d.js +0 -4
- package/library/stream/index.d.js +0 -58
- package/library/stream/readable-stream.d.js +0 -5
- package/library/stream/types.d.js +0 -5
- package/library/stream/writable-stream.d.js +0 -5
- package/library/types/index.d.js +0 -5
- package/library/utils/cache.d.js +0 -5
- package/library/utils/cookie.d.js +0 -5
- package/library/utils/debug.d.js +0 -5
- package/library/utils/index.d.js +0 -94
- package/library/utils/path-match.d.js +0 -5
- package/library/utils/protocol.d.js +0 -5
- package/react/library/__tests__/index.test.d.ts +0 -2
- package/react/library/__tests__/index.test.d.ts.map +0 -1
- package/react/library/__tests__/index.test.tsx +0 -792
|
@@ -1,433 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
generateRequestId,
|
|
3
|
-
createPostMessage,
|
|
4
|
-
isValidPostMessage,
|
|
5
|
-
getIframeTargetOrigin,
|
|
6
|
-
parseSetCookie,
|
|
7
|
-
createSetCookie,
|
|
8
|
-
createClearCookie,
|
|
9
|
-
matchCookiePath,
|
|
10
|
-
CookieStore
|
|
11
|
-
} from '../utils';
|
|
12
|
-
import {
|
|
13
|
-
validateProtocolVersion,
|
|
14
|
-
validatePostMessage,
|
|
15
|
-
isRequestIframeMessage,
|
|
16
|
-
getProtocolVersion,
|
|
17
|
-
isCompatibleVersion
|
|
18
|
-
} from '../utils/protocol';
|
|
19
|
-
import { matchPath } from '../utils/path-match';
|
|
20
|
-
|
|
21
|
-
describe('utils', () => {
|
|
22
|
-
describe('generateRequestId', () => {
|
|
23
|
-
it('should generate unique request IDs', () => {
|
|
24
|
-
const id1 = generateRequestId();
|
|
25
|
-
const id2 = generateRequestId();
|
|
26
|
-
expect(id1).not.toBe(id2);
|
|
27
|
-
expect(id1).toMatch(/^req_\d+_[a-z0-9]+$/);
|
|
28
|
-
});
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
describe('createPostMessage', () => {
|
|
32
|
-
it('should create correct PostMessage data', () => {
|
|
33
|
-
const before = Date.now();
|
|
34
|
-
const message = createPostMessage('request', 'req123', {
|
|
35
|
-
path: 'test',
|
|
36
|
-
body: { param: 'value' }
|
|
37
|
-
});
|
|
38
|
-
const after = Date.now();
|
|
39
|
-
|
|
40
|
-
expect(message).toEqual({
|
|
41
|
-
__requestIframe__: 1,
|
|
42
|
-
timestamp: expect.any(Number),
|
|
43
|
-
type: 'request',
|
|
44
|
-
requestId: 'req123',
|
|
45
|
-
path: 'test',
|
|
46
|
-
body: { param: 'value' }
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
// Verify timestamp is within valid range
|
|
50
|
-
expect(message.timestamp).toBeGreaterThanOrEqual(before);
|
|
51
|
-
expect(message.timestamp).toBeLessThanOrEqual(after);
|
|
52
|
-
});
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
describe('isValidPostMessage', () => {
|
|
56
|
-
it('should validate correct PostMessage data', () => {
|
|
57
|
-
const valid = {
|
|
58
|
-
__requestIframe__: 1,
|
|
59
|
-
type: 'request',
|
|
60
|
-
requestId: 'req123'
|
|
61
|
-
};
|
|
62
|
-
expect(isValidPostMessage(valid)).toBe(true);
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
it('should reject invalid PostMessage data', () => {
|
|
66
|
-
expect(isValidPostMessage(null)).toBe(false);
|
|
67
|
-
expect(isValidPostMessage({})).toBe(false);
|
|
68
|
-
expect(isValidPostMessage({ __requestIframe__: 1, type: 'request' })).toBe(false);
|
|
69
|
-
expect(isValidPostMessage({ __requestIframe__: 1, requestId: 'req123' })).toBe(false);
|
|
70
|
-
expect(isValidPostMessage({ type: 'request', requestId: 'req123' })).toBe(false);
|
|
71
|
-
});
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
describe('getIframeTargetOrigin', () => {
|
|
75
|
-
beforeEach(() => {
|
|
76
|
-
document.querySelectorAll('iframe').forEach((iframe) => {
|
|
77
|
-
if (iframe.parentNode) {
|
|
78
|
-
iframe.parentNode.removeChild(iframe);
|
|
79
|
-
}
|
|
80
|
-
});
|
|
81
|
-
});
|
|
82
|
-
|
|
83
|
-
it('should parse origin from iframe.src', () => {
|
|
84
|
-
const iframe = document.createElement('iframe');
|
|
85
|
-
iframe.src = 'https://example.com/test.html';
|
|
86
|
-
document.body.appendChild(iframe);
|
|
87
|
-
|
|
88
|
-
const origin = getIframeTargetOrigin(iframe);
|
|
89
|
-
expect(origin).toBe('https://example.com');
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
it('should return * when iframe has no src', () => {
|
|
93
|
-
const iframe = document.createElement('iframe');
|
|
94
|
-
document.body.appendChild(iframe);
|
|
95
|
-
const origin = getIframeTargetOrigin(iframe);
|
|
96
|
-
expect(origin).toBe('*');
|
|
97
|
-
});
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
describe('Cookie utilities', () => {
|
|
101
|
-
describe('parseSetCookie', () => {
|
|
102
|
-
it('should parse basic Set-Cookie string', () => {
|
|
103
|
-
const cookie = parseSetCookie('token=abc123');
|
|
104
|
-
expect(cookie).toEqual({
|
|
105
|
-
name: 'token',
|
|
106
|
-
value: 'abc123',
|
|
107
|
-
path: '/'
|
|
108
|
-
});
|
|
109
|
-
});
|
|
110
|
-
|
|
111
|
-
it('should parse Set-Cookie string with Path', () => {
|
|
112
|
-
const cookie = parseSetCookie('token=abc123; Path=/api');
|
|
113
|
-
expect(cookie).toEqual({
|
|
114
|
-
name: 'token',
|
|
115
|
-
value: 'abc123',
|
|
116
|
-
path: '/api'
|
|
117
|
-
});
|
|
118
|
-
});
|
|
119
|
-
|
|
120
|
-
it('should parse Set-Cookie string with HttpOnly', () => {
|
|
121
|
-
const cookie = parseSetCookie('token=abc123; HttpOnly');
|
|
122
|
-
expect(cookie?.httpOnly).toBe(true);
|
|
123
|
-
});
|
|
124
|
-
|
|
125
|
-
it('should parse Set-Cookie string with Secure', () => {
|
|
126
|
-
const cookie = parseSetCookie('token=abc123; Secure');
|
|
127
|
-
expect(cookie?.secure).toBe(true);
|
|
128
|
-
});
|
|
129
|
-
|
|
130
|
-
it('should parse Set-Cookie string with Expires', () => {
|
|
131
|
-
const cookie = parseSetCookie('token=abc123; Expires=Thu, 01 Jan 2030 00:00:00 GMT');
|
|
132
|
-
expect(cookie?.expires).toBeDefined();
|
|
133
|
-
expect(cookie?.expires).toBeGreaterThan(Date.now());
|
|
134
|
-
});
|
|
135
|
-
|
|
136
|
-
it('should parse Set-Cookie string with Max-Age', () => {
|
|
137
|
-
const cookie = parseSetCookie('token=abc123; Max-Age=3600');
|
|
138
|
-
expect(cookie?.expires).toBeDefined();
|
|
139
|
-
expect(cookie?.expires).toBeGreaterThan(Date.now());
|
|
140
|
-
expect(cookie?.expires).toBeLessThan(Date.now() + 3600 * 1000 + 1000);
|
|
141
|
-
});
|
|
142
|
-
|
|
143
|
-
it('should parse Set-Cookie string with SameSite', () => {
|
|
144
|
-
const strict = parseSetCookie('token=abc123; SameSite=Strict');
|
|
145
|
-
expect(strict?.sameSite).toBe('Strict');
|
|
146
|
-
|
|
147
|
-
const lax = parseSetCookie('token=abc123; SameSite=Lax');
|
|
148
|
-
expect(lax?.sameSite).toBe('Lax');
|
|
149
|
-
|
|
150
|
-
const none = parseSetCookie('token=abc123; SameSite=None');
|
|
151
|
-
expect(none?.sameSite).toBe('None');
|
|
152
|
-
});
|
|
153
|
-
});
|
|
154
|
-
|
|
155
|
-
describe('createSetCookie', () => {
|
|
156
|
-
it('should create basic Set-Cookie string', () => {
|
|
157
|
-
const str = createSetCookie('token', 'abc123');
|
|
158
|
-
expect(str).toBe('token=abc123; Path=/');
|
|
159
|
-
});
|
|
160
|
-
|
|
161
|
-
it('should create Set-Cookie string with Path', () => {
|
|
162
|
-
const str = createSetCookie('token', 'abc123', { path: '/api' });
|
|
163
|
-
expect(str).toBe('token=abc123; Path=/api');
|
|
164
|
-
});
|
|
165
|
-
|
|
166
|
-
it('should create Set-Cookie string with HttpOnly', () => {
|
|
167
|
-
const str = createSetCookie('token', 'abc123', { httpOnly: true });
|
|
168
|
-
expect(str).toContain('HttpOnly');
|
|
169
|
-
});
|
|
170
|
-
});
|
|
171
|
-
|
|
172
|
-
describe('createClearCookie', () => {
|
|
173
|
-
it('should create Set-Cookie string to delete cookie', () => {
|
|
174
|
-
const str = createClearCookie('token');
|
|
175
|
-
expect(str).toContain('token=');
|
|
176
|
-
expect(str).toContain('Max-Age=0');
|
|
177
|
-
expect(str).toContain('Expires=');
|
|
178
|
-
});
|
|
179
|
-
});
|
|
180
|
-
|
|
181
|
-
describe('matchCookiePath', () => {
|
|
182
|
-
it('should return true for exact match', () => {
|
|
183
|
-
expect(matchCookiePath('/api', '/api')).toBe(true);
|
|
184
|
-
expect(matchCookiePath('/', '/')).toBe(true);
|
|
185
|
-
});
|
|
186
|
-
|
|
187
|
-
it('should return true when request path is sub-path of cookie path', () => {
|
|
188
|
-
expect(matchCookiePath('/api/users', '/api')).toBe(true);
|
|
189
|
-
expect(matchCookiePath('/api/users/123', '/api')).toBe(true);
|
|
190
|
-
expect(matchCookiePath('/api/users', '/')).toBe(true);
|
|
191
|
-
});
|
|
192
|
-
|
|
193
|
-
it('should return false for non-matching paths', () => {
|
|
194
|
-
expect(matchCookiePath('/api', '/admin')).toBe(false);
|
|
195
|
-
expect(matchCookiePath('/ap', '/api')).toBe(false);
|
|
196
|
-
expect(matchCookiePath('/apitest', '/api')).toBe(false);
|
|
197
|
-
});
|
|
198
|
-
});
|
|
199
|
-
|
|
200
|
-
describe('CookieStore', () => {
|
|
201
|
-
let store: CookieStore;
|
|
202
|
-
|
|
203
|
-
beforeEach(() => {
|
|
204
|
-
store = new CookieStore();
|
|
205
|
-
});
|
|
206
|
-
|
|
207
|
-
it('should set and get cookie', () => {
|
|
208
|
-
store.set({ name: 'token', value: 'abc', path: '/' });
|
|
209
|
-
expect(store.get('token')).toBe('abc');
|
|
210
|
-
});
|
|
211
|
-
|
|
212
|
-
it('should get cookies by path', () => {
|
|
213
|
-
store.set({ name: 'global', value: 'g', path: '/' });
|
|
214
|
-
store.set({ name: 'api', value: 'a', path: '/api' });
|
|
215
|
-
store.set({ name: 'admin', value: 'ad', path: '/admin' });
|
|
216
|
-
|
|
217
|
-
expect(store.getForPath('/')).toEqual({ global: 'g' });
|
|
218
|
-
expect(store.getForPath('/api')).toEqual({ global: 'g', api: 'a' });
|
|
219
|
-
expect(store.getForPath('/api/users')).toEqual({ global: 'g', api: 'a' });
|
|
220
|
-
expect(store.getForPath('/admin')).toEqual({ global: 'g', admin: 'ad' });
|
|
221
|
-
});
|
|
222
|
-
|
|
223
|
-
it('should remove cookie', () => {
|
|
224
|
-
store.set({ name: 'token', value: 'abc', path: '/' });
|
|
225
|
-
store.remove('token', '/');
|
|
226
|
-
expect(store.get('token')).toBeUndefined();
|
|
227
|
-
});
|
|
228
|
-
|
|
229
|
-
it('should set cookie from Set-Cookie string', () => {
|
|
230
|
-
store.setFromSetCookie('token=abc123; Path=/api');
|
|
231
|
-
expect(store.get('token', '/api')).toBe('abc123');
|
|
232
|
-
});
|
|
233
|
-
|
|
234
|
-
it('should remove expired cookie from Set-Cookie string', () => {
|
|
235
|
-
store.set({ name: 'token', value: 'old', path: '/' });
|
|
236
|
-
store.setFromSetCookie('token=; Path=/; Expires=Thu, 01 Jan 1970 00:00:00 GMT');
|
|
237
|
-
expect(store.get('token', '/')).toBeUndefined();
|
|
238
|
-
});
|
|
239
|
-
|
|
240
|
-
it('should clear all cookies', () => {
|
|
241
|
-
store.set({ name: 'a', value: '1', path: '/' });
|
|
242
|
-
store.set({ name: 'b', value: '2', path: '/api' });
|
|
243
|
-
store.clear();
|
|
244
|
-
expect(store.getAllSimple()).toEqual({});
|
|
245
|
-
});
|
|
246
|
-
|
|
247
|
-
it('should handle expired cookies in getForPath', () => {
|
|
248
|
-
store.set({ name: 'expired', value: 'old', path: '/', expires: Date.now() - 1000 });
|
|
249
|
-
expect(store.getForPath('/')).toEqual({});
|
|
250
|
-
});
|
|
251
|
-
|
|
252
|
-
it('should handle getAll method', () => {
|
|
253
|
-
store.set({ name: 'a', value: '1', path: '/' });
|
|
254
|
-
store.set({ name: 'b', value: '2', path: '/api' });
|
|
255
|
-
const all = store.getAll();
|
|
256
|
-
expect(all.length).toBe(2);
|
|
257
|
-
expect(all.some(c => c.name === 'a')).toBe(true);
|
|
258
|
-
expect(all.some(c => c.name === 'b')).toBe(true);
|
|
259
|
-
});
|
|
260
|
-
|
|
261
|
-
it('should handle cleanup method', () => {
|
|
262
|
-
store.set({ name: 'expired', value: 'old', path: '/', expires: Date.now() - 1000 });
|
|
263
|
-
store.set({ name: 'valid', value: 'new', path: '/' });
|
|
264
|
-
store.cleanup();
|
|
265
|
-
expect(store.get('expired')).toBeUndefined();
|
|
266
|
-
expect(store.get('valid')).toBe('new');
|
|
267
|
-
});
|
|
268
|
-
});
|
|
269
|
-
});
|
|
270
|
-
|
|
271
|
-
describe('Protocol utilities', () => {
|
|
272
|
-
describe('validateProtocolVersion', () => {
|
|
273
|
-
it('should validate compatible version', () => {
|
|
274
|
-
const result = validateProtocolVersion(1);
|
|
275
|
-
expect(result.valid).toBe(true);
|
|
276
|
-
expect(result.version).toBe(1);
|
|
277
|
-
});
|
|
278
|
-
|
|
279
|
-
it('should reject version too low', () => {
|
|
280
|
-
const result = validateProtocolVersion(0);
|
|
281
|
-
expect(result.valid).toBe(false);
|
|
282
|
-
expect(result.errorCode).toBe('VERSION_TOO_LOW');
|
|
283
|
-
});
|
|
284
|
-
|
|
285
|
-
it('should reject invalid format', () => {
|
|
286
|
-
const result1 = validateProtocolVersion('1' as any);
|
|
287
|
-
expect(result1.valid).toBe(false);
|
|
288
|
-
expect(result1.errorCode).toBe('INVALID_FORMAT');
|
|
289
|
-
|
|
290
|
-
const result2 = validateProtocolVersion(1.5);
|
|
291
|
-
expect(result2.valid).toBe(false);
|
|
292
|
-
expect(result2.errorCode).toBe('INVALID_FORMAT');
|
|
293
|
-
});
|
|
294
|
-
});
|
|
295
|
-
|
|
296
|
-
describe('validatePostMessage', () => {
|
|
297
|
-
it('should validate correct message', () => {
|
|
298
|
-
const result = validatePostMessage({
|
|
299
|
-
__requestIframe__: 1,
|
|
300
|
-
type: 'request',
|
|
301
|
-
requestId: 'req123'
|
|
302
|
-
});
|
|
303
|
-
expect(result.valid).toBe(true);
|
|
304
|
-
expect(result.data).toBeDefined();
|
|
305
|
-
});
|
|
306
|
-
|
|
307
|
-
it('should reject non-object', () => {
|
|
308
|
-
const result = validatePostMessage(null);
|
|
309
|
-
expect(result.valid).toBe(false);
|
|
310
|
-
expect(result.errorCode).toBe('INVALID_FORMAT');
|
|
311
|
-
});
|
|
312
|
-
|
|
313
|
-
it('should reject missing protocol identifier', () => {
|
|
314
|
-
const result = validatePostMessage({
|
|
315
|
-
type: 'request',
|
|
316
|
-
requestId: 'req123'
|
|
317
|
-
});
|
|
318
|
-
expect(result.valid).toBe(false);
|
|
319
|
-
expect(result.errorCode).toBe('INVALID_FORMAT');
|
|
320
|
-
});
|
|
321
|
-
|
|
322
|
-
it('should reject missing type', () => {
|
|
323
|
-
const result = validatePostMessage({
|
|
324
|
-
__requestIframe__: 1,
|
|
325
|
-
requestId: 'req123'
|
|
326
|
-
});
|
|
327
|
-
expect(result.valid).toBe(false);
|
|
328
|
-
expect(result.errorCode).toBe('INVALID_FORMAT');
|
|
329
|
-
});
|
|
330
|
-
|
|
331
|
-
it('should reject missing requestId', () => {
|
|
332
|
-
const result = validatePostMessage({
|
|
333
|
-
__requestIframe__: 1,
|
|
334
|
-
type: 'request'
|
|
335
|
-
});
|
|
336
|
-
expect(result.valid).toBe(false);
|
|
337
|
-
expect(result.errorCode).toBe('INVALID_FORMAT');
|
|
338
|
-
});
|
|
339
|
-
});
|
|
340
|
-
|
|
341
|
-
describe('isRequestIframeMessage', () => {
|
|
342
|
-
it('should return true for valid message', () => {
|
|
343
|
-
expect(isRequestIframeMessage({
|
|
344
|
-
__requestIframe__: 1,
|
|
345
|
-
type: 'request',
|
|
346
|
-
requestId: 'req123'
|
|
347
|
-
})).toBe(true);
|
|
348
|
-
});
|
|
349
|
-
|
|
350
|
-
it('should return false for invalid message', () => {
|
|
351
|
-
expect(isRequestIframeMessage(null)).toBe(false);
|
|
352
|
-
expect(isRequestIframeMessage({})).toBe(false);
|
|
353
|
-
expect(isRequestIframeMessage({
|
|
354
|
-
__requestIframe__: '1',
|
|
355
|
-
type: 'request',
|
|
356
|
-
requestId: 'req123'
|
|
357
|
-
})).toBe(false);
|
|
358
|
-
});
|
|
359
|
-
});
|
|
360
|
-
|
|
361
|
-
describe('getProtocolVersion', () => {
|
|
362
|
-
it('should extract version from message', () => {
|
|
363
|
-
expect(getProtocolVersion({
|
|
364
|
-
__requestIframe__: 1
|
|
365
|
-
})).toBe(1);
|
|
366
|
-
});
|
|
367
|
-
|
|
368
|
-
it('should return undefined for invalid message', () => {
|
|
369
|
-
expect(getProtocolVersion(null)).toBeUndefined();
|
|
370
|
-
expect(getProtocolVersion({})).toBeUndefined();
|
|
371
|
-
expect(getProtocolVersion({
|
|
372
|
-
__requestIframe__: '1'
|
|
373
|
-
})).toBeUndefined();
|
|
374
|
-
});
|
|
375
|
-
});
|
|
376
|
-
|
|
377
|
-
describe('isCompatibleVersion', () => {
|
|
378
|
-
it('should return true for compatible version', () => {
|
|
379
|
-
expect(isCompatibleVersion(1)).toBe(true);
|
|
380
|
-
expect(isCompatibleVersion(2)).toBe(true);
|
|
381
|
-
});
|
|
382
|
-
|
|
383
|
-
it('should return false for incompatible version', () => {
|
|
384
|
-
expect(isCompatibleVersion(0)).toBe(false);
|
|
385
|
-
});
|
|
386
|
-
});
|
|
387
|
-
});
|
|
388
|
-
|
|
389
|
-
describe('Path matching', () => {
|
|
390
|
-
describe('matchPath', () => {
|
|
391
|
-
it('should match exact string path', () => {
|
|
392
|
-
expect(matchPath('/api', '/api')).toBe(true);
|
|
393
|
-
expect(matchPath('/api/users', '/api/users')).toBe(true);
|
|
394
|
-
});
|
|
395
|
-
|
|
396
|
-
it('should match prefix path', () => {
|
|
397
|
-
expect(matchPath('/api/users', '/api')).toBe(true);
|
|
398
|
-
expect(matchPath('/api/users/123', '/api')).toBe(true);
|
|
399
|
-
});
|
|
400
|
-
|
|
401
|
-
it('should not match non-prefix path', () => {
|
|
402
|
-
expect(matchPath('/api2', '/api')).toBe(false);
|
|
403
|
-
expect(matchPath('/apitest', '/api')).toBe(false);
|
|
404
|
-
});
|
|
405
|
-
|
|
406
|
-
it('should match with trailing slash', () => {
|
|
407
|
-
expect(matchPath('/api/users', '/api/')).toBe(true);
|
|
408
|
-
});
|
|
409
|
-
|
|
410
|
-
it('should match RegExp', () => {
|
|
411
|
-
expect(matchPath('/api/users/123', /^\/api\/users\/\d+$/)).toBe(true);
|
|
412
|
-
expect(matchPath('/api/users/abc', /^\/api\/users\/\d+$/)).toBe(false);
|
|
413
|
-
});
|
|
414
|
-
|
|
415
|
-
it('should match array of matchers', () => {
|
|
416
|
-
expect(matchPath('/api/users', ['/admin', '/api'])).toBe(true);
|
|
417
|
-
expect(matchPath('/api/users', ['/admin', '/other'])).toBe(false);
|
|
418
|
-
});
|
|
419
|
-
|
|
420
|
-
it('should match wildcard patterns', () => {
|
|
421
|
-
expect(matchPath('/api/users/123', '/api/*')).toBe(true);
|
|
422
|
-
expect(matchPath('/api/users', '/api/*')).toBe(true);
|
|
423
|
-
expect(matchPath('/api/users/123/posts', '/api/*/posts')).toBe(true);
|
|
424
|
-
expect(matchPath('/other/users', '/api/*')).toBe(false);
|
|
425
|
-
});
|
|
426
|
-
|
|
427
|
-
it('should normalize paths', () => {
|
|
428
|
-
expect(matchPath('api', '/api')).toBe(true);
|
|
429
|
-
expect(matchPath('/api', 'api')).toBe(true);
|
|
430
|
-
});
|
|
431
|
-
});
|
|
432
|
-
});
|
|
433
|
-
});
|
package/library/api/client.d.js
DELETED
package/library/api/server.d.js
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
Object.defineProperty(exports, "Messages", {
|
|
7
|
-
enumerable: true,
|
|
8
|
-
get: function get() {
|
|
9
|
-
return _messages.Messages;
|
|
10
|
-
}
|
|
11
|
-
});
|
|
12
|
-
Object.defineProperty(exports, "formatMessage", {
|
|
13
|
-
enumerable: true,
|
|
14
|
-
get: function get() {
|
|
15
|
-
return _messages.formatMessage;
|
|
16
|
-
}
|
|
17
|
-
});
|
|
18
|
-
Object.defineProperty(exports, "getMessages", {
|
|
19
|
-
enumerable: true,
|
|
20
|
-
get: function get() {
|
|
21
|
-
return _messages.getMessages;
|
|
22
|
-
}
|
|
23
|
-
});
|
|
24
|
-
Object.defineProperty(exports, "resetMessages", {
|
|
25
|
-
enumerable: true,
|
|
26
|
-
get: function get() {
|
|
27
|
-
return _messages.resetMessages;
|
|
28
|
-
}
|
|
29
|
-
});
|
|
30
|
-
Object.defineProperty(exports, "setMessages", {
|
|
31
|
-
enumerable: true,
|
|
32
|
-
get: function get() {
|
|
33
|
-
return _messages.setMessages;
|
|
34
|
-
}
|
|
35
|
-
});
|
|
36
|
-
var _messages = require("./messages");
|
package/library/core/client.d.js
DELETED
|
@@ -1,110 +0,0 @@
|
|
|
1
|
-
import { PostMessageData, ServerEventName } from '../types';
|
|
2
|
-
/**
|
|
3
|
-
* 消息处理器配置
|
|
4
|
-
*/
|
|
5
|
-
export interface MessageHandlerOptions {
|
|
6
|
-
/** 消息隔离标识 */
|
|
7
|
-
uniqueKey?: string;
|
|
8
|
-
/** ACK 超时时间 */
|
|
9
|
-
ackTimeout?: number;
|
|
10
|
-
}
|
|
11
|
-
/**
|
|
12
|
-
* 消息处理回调
|
|
13
|
-
*/
|
|
14
|
-
export interface MessageHandlerCallbacks {
|
|
15
|
-
/** 处理请求 */
|
|
16
|
-
onRequest?: (data: PostMessageData, event: MessageEvent) => void;
|
|
17
|
-
/** 处理客户端响应(ack、async、response、error) */
|
|
18
|
-
onClientResponse?: (data: PostMessageData, event: MessageEvent) => void;
|
|
19
|
-
/** 处理 received 确认 */
|
|
20
|
-
onReceived?: (data: PostMessageData, event: MessageEvent) => void;
|
|
21
|
-
/** 处理 ping */
|
|
22
|
-
onPing?: (data: PostMessageData, event: MessageEvent) => void;
|
|
23
|
-
/** 处理 pong */
|
|
24
|
-
onPong?: (data: PostMessageData, event: MessageEvent) => void;
|
|
25
|
-
/** 处理流消息 */
|
|
26
|
-
onStream?: (data: PostMessageData, event: MessageEvent) => void;
|
|
27
|
-
}
|
|
28
|
-
/**
|
|
29
|
-
* IframeMessageHandler - 底层消息处理器
|
|
30
|
-
* 负责 postMessage 监听、协议验证、uniqueKey 隔离、事件分发
|
|
31
|
-
*/
|
|
32
|
-
export declare class IframeMessageHandler {
|
|
33
|
-
readonly uniqueKey?: string;
|
|
34
|
-
readonly ackTimeout: number;
|
|
35
|
-
private readonly listeners;
|
|
36
|
-
/** 等待客户端确认的响应 */
|
|
37
|
-
private readonly pendingAcks;
|
|
38
|
-
/** 等待响应的请求 */
|
|
39
|
-
private readonly pendingRequests;
|
|
40
|
-
/** 消息处理回调 */
|
|
41
|
-
private callbacks;
|
|
42
|
-
/** 消息监听函数(绑定 this) */
|
|
43
|
-
private readonly boundOnMessage;
|
|
44
|
-
constructor(options?: MessageHandlerOptions);
|
|
45
|
-
/**
|
|
46
|
-
* 设置消息处理回调
|
|
47
|
-
*/
|
|
48
|
-
setCallbacks(callbacks: MessageHandlerCallbacks): void;
|
|
49
|
-
/**
|
|
50
|
-
* 消息处理入口
|
|
51
|
-
*/
|
|
52
|
-
private onMessage;
|
|
53
|
-
/**
|
|
54
|
-
* 分发消息到具体处理器
|
|
55
|
-
*/
|
|
56
|
-
private dispatchMessage;
|
|
57
|
-
/**
|
|
58
|
-
* 处理客户端响应
|
|
59
|
-
*/
|
|
60
|
-
private handleClientResponse;
|
|
61
|
-
/**
|
|
62
|
-
* 处理 received 确认
|
|
63
|
-
*/
|
|
64
|
-
private handleReceived;
|
|
65
|
-
/**
|
|
66
|
-
* 处理 ping
|
|
67
|
-
*/
|
|
68
|
-
private handlePing;
|
|
69
|
-
/**
|
|
70
|
-
* 处理 pong
|
|
71
|
-
*/
|
|
72
|
-
private handlePong;
|
|
73
|
-
/**
|
|
74
|
-
* 注册等待确认的响应
|
|
75
|
-
*/
|
|
76
|
-
registerPendingAck(requestId: string, resolve: (received: boolean) => void, reject: (error: Error) => void): void;
|
|
77
|
-
/**
|
|
78
|
-
* 注册等待响应的请求
|
|
79
|
-
*/
|
|
80
|
-
registerPendingRequest(requestId: string, resolve: (data: PostMessageData) => void, reject: (error: Error) => void, origin?: string): void;
|
|
81
|
-
/**
|
|
82
|
-
* 取消等待响应
|
|
83
|
-
*/
|
|
84
|
-
unregisterPendingRequest(requestId: string): void;
|
|
85
|
-
/**
|
|
86
|
-
* 监听事件
|
|
87
|
-
*/
|
|
88
|
-
on(event: ServerEventName, fn: (payload: any, messageEvent?: MessageEvent) => void): void;
|
|
89
|
-
/**
|
|
90
|
-
* 取消监听
|
|
91
|
-
*/
|
|
92
|
-
off(event: ServerEventName, fn?: (payload: any, messageEvent?: MessageEvent) => void): void;
|
|
93
|
-
/**
|
|
94
|
-
* 触发事件
|
|
95
|
-
*/
|
|
96
|
-
private emit;
|
|
97
|
-
/**
|
|
98
|
-
* 添加 path 前缀
|
|
99
|
-
*/
|
|
100
|
-
prefixPath(path: string): string;
|
|
101
|
-
/**
|
|
102
|
-
* 发送协议版本错误
|
|
103
|
-
*/
|
|
104
|
-
private sendProtocolError;
|
|
105
|
-
/**
|
|
106
|
-
* 销毁
|
|
107
|
-
*/
|
|
108
|
-
destroy(): void;
|
|
109
|
-
}
|
|
110
|
-
//# sourceMappingURL=message-handler.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"message-handler.d.ts","sourceRoot":"","sources":["../../src/core/message-handler.ts"],"names":[],"mappings":"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"}
|