request-iframe 0.0.3 → 0.0.4
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 +35 -8
- package/QUICKSTART.md +35 -8
- package/README.CN.md +170 -24
- package/README.md +230 -19
- package/library/__tests__/coverage-branches.test.ts +356 -0
- package/library/__tests__/requestIframe.test.ts +1008 -58
- package/library/__tests__/stream.test.ts +46 -15
- package/library/api/client.d.ts.map +1 -1
- package/library/api/client.js +1 -0
- package/library/constants/messages.d.ts +2 -0
- package/library/constants/messages.d.ts.map +1 -1
- package/library/constants/messages.js +2 -0
- package/library/core/client-server.d.ts +4 -0
- package/library/core/client-server.d.ts.map +1 -1
- package/library/core/client-server.js +45 -22
- package/library/core/client.d.ts +31 -4
- package/library/core/client.d.ts.map +1 -1
- package/library/core/client.js +471 -284
- package/library/core/request.d.ts +3 -1
- package/library/core/request.d.ts.map +1 -1
- package/library/core/request.js +2 -1
- package/library/core/response.d.ts +26 -4
- package/library/core/response.d.ts.map +1 -1
- package/library/core/response.js +142 -81
- package/library/core/server.d.ts +13 -0
- package/library/core/server.d.ts.map +1 -1
- package/library/core/server.js +211 -6
- package/library/index.d.ts +2 -1
- package/library/index.d.ts.map +1 -1
- package/library/index.js +32 -3
- package/library/message/dispatcher.d.ts.map +1 -1
- package/library/message/dispatcher.js +4 -3
- package/library/stream/index.d.ts +11 -1
- package/library/stream/index.d.ts.map +1 -1
- package/library/stream/index.js +21 -3
- package/library/stream/types.d.ts +2 -2
- package/library/stream/types.d.ts.map +1 -1
- package/library/stream/writable-stream.d.ts +1 -1
- package/library/stream/writable-stream.d.ts.map +1 -1
- package/library/stream/writable-stream.js +8 -10
- package/library/types/index.d.ts +26 -4
- package/library/types/index.d.ts.map +1 -1
- package/library/utils/index.d.ts +14 -0
- package/library/utils/index.d.ts.map +1 -1
- package/library/utils/index.js +99 -1
- package/library/utils/path-match.d.ts +16 -0
- package/library/utils/path-match.d.ts.map +1 -1
- package/library/utils/path-match.js +65 -0
- package/package.json +2 -1
- package/react/library/__tests__/index.test.tsx +44 -22
- package/react/library/index.d.ts.map +1 -1
- package/react/library/index.js +81 -23
- package/react/package.json +7 -0
package/library/types/index.d.ts
CHANGED
|
@@ -10,6 +10,12 @@ export interface RequestDefaults {
|
|
|
10
10
|
timeout?: number;
|
|
11
11
|
/** Async request timeout (milliseconds), timeout after the server indicates it's an async task, default 120000 */
|
|
12
12
|
asyncTimeout?: number;
|
|
13
|
+
/**
|
|
14
|
+
* Whether to directly return response.data instead of the full Response object.
|
|
15
|
+
* If true, send() will return Promise<T> instead of Promise<Response<T>>.
|
|
16
|
+
* Default is false.
|
|
17
|
+
*/
|
|
18
|
+
returnData?: boolean;
|
|
13
19
|
}
|
|
14
20
|
/**
|
|
15
21
|
* Headers value type (supports static value or dynamic function)
|
|
@@ -45,8 +51,8 @@ export interface RequestOptions extends RequestDefaults {
|
|
|
45
51
|
export interface RequestConfig extends RequestOptions {
|
|
46
52
|
/** Interaction event ID (equivalent to path) */
|
|
47
53
|
path: string;
|
|
48
|
-
/** Request body */
|
|
49
|
-
body?:
|
|
54
|
+
/** Request body (plain object, File, Blob, or stream for sendStream) */
|
|
55
|
+
body?: any;
|
|
50
56
|
/** Request headers */
|
|
51
57
|
headers?: HeadersConfig;
|
|
52
58
|
/** Request cookies */
|
|
@@ -186,6 +192,8 @@ export interface PostMessageData {
|
|
|
186
192
|
* Used to ensure messages are routed to the correct instance and avoid message confusion
|
|
187
193
|
*/
|
|
188
194
|
targetId?: string;
|
|
195
|
+
/** Stream ID (when request body is a stream, client sends stream; server receives stream_start next) */
|
|
196
|
+
streamId?: string;
|
|
189
197
|
}
|
|
190
198
|
/**
|
|
191
199
|
* Server Request object (similar to express)
|
|
@@ -193,12 +201,16 @@ export interface PostMessageData {
|
|
|
193
201
|
export interface ServerRequest {
|
|
194
202
|
/** Request body */
|
|
195
203
|
body: any;
|
|
204
|
+
/** Request stream (when client sent via sendStream) */
|
|
205
|
+
stream?: import('../stream').IIframeReadableStream;
|
|
196
206
|
/** Request headers */
|
|
197
207
|
headers: Record<string, string>;
|
|
198
208
|
/** Request cookies */
|
|
199
209
|
cookies: Record<string, string>;
|
|
200
210
|
/** Request path */
|
|
201
211
|
path: string;
|
|
212
|
+
/** Path parameters extracted from route pattern (e.g., { id: '123' } for '/api/users/:id' and '/api/users/123') */
|
|
213
|
+
params: Record<string, string>;
|
|
202
214
|
/** Request ID */
|
|
203
215
|
requestId: string;
|
|
204
216
|
/** Sender origin */
|
|
@@ -315,6 +327,8 @@ export type ServerEventName = 'request' | 'ack' | 'async' | 'response' | 'error'
|
|
|
315
327
|
* Client interface
|
|
316
328
|
*/
|
|
317
329
|
export interface RequestIframeClient {
|
|
330
|
+
/** Target window */
|
|
331
|
+
readonly targetWindow: Window;
|
|
318
332
|
/** Unique instance ID */
|
|
319
333
|
readonly id: string;
|
|
320
334
|
/** Whether message handling is enabled */
|
|
@@ -328,8 +342,16 @@ export interface RequestIframeClient {
|
|
|
328
342
|
request: import('../interceptors').RequestInterceptorManager;
|
|
329
343
|
response: import('../interceptors').ResponseInterceptorManager;
|
|
330
344
|
};
|
|
331
|
-
/** Send request */
|
|
332
|
-
send<T = any>(path: string, body?:
|
|
345
|
+
/** Send request (body can be plain data, File, Blob, or stream; auto-dispatches to sendFile/sendStream when applicable) */
|
|
346
|
+
send<T = any>(path: string, body?: any, options?: RequestOptions): Promise<Response<T> | T>;
|
|
347
|
+
/** Send file as request body (stream only; server receives stream or auto-resolved File/Blob via autoResolve) */
|
|
348
|
+
sendFile<T = any>(path: string, content: string | Blob | File, options?: RequestOptions & {
|
|
349
|
+
mimeType?: string;
|
|
350
|
+
fileName?: string;
|
|
351
|
+
autoResolve?: boolean;
|
|
352
|
+
}): Promise<Response<T> | T>;
|
|
353
|
+
/** Send stream as request body (server receives readable stream) */
|
|
354
|
+
sendStream<T = any>(path: string, stream: import('../stream').IframeWritableStream, options?: RequestOptions): Promise<Response<T> | T>;
|
|
333
355
|
/** Check if server is reachable */
|
|
334
356
|
isConnect(): Promise<boolean>;
|
|
335
357
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAEhD;;;GAGG;AACH,MAAM,WAAW,eAAe;IAC9B,gIAAgI;IAChI,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,4GAA4G;IAC5G,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,kHAAkH;IAClH,YAAY,CAAC,EAAE,MAAM,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAEhD;;;GAGG;AACH,MAAM,WAAW,eAAe;IAC9B,gIAAgI;IAChI,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,4GAA4G;IAC5G,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,kHAAkH;IAClH,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB;;;;OAIG;IACH,UAAU,CAAC,EAAE,OAAO,CAAC;CACtB;AAED;;GAEG;AACH,MAAM,MAAM,WAAW,GAAG,MAAM,GAAG,MAAM,EAAE,GAAG,CAAC,CAAC,MAAM,EAAE,aAAa,KAAK,MAAM,GAAG,MAAM,EAAE,CAAC,CAAC;AAE7F;;GAEG;AACH,MAAM,MAAM,aAAa,GAAG,MAAM,CAAC,MAAM,EAAE,WAAW,CAAC,CAAC;AAExD;;GAEG;AACH,MAAM,WAAW,cAAe,SAAQ,eAAe;IACrD,wBAAwB;IACxB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB;;;OAGG;IACH,OAAO,CAAC,EAAE,aAAa,CAAC;IACxB,sBAAsB;IACtB,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,M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IACH,QAAQ,CAAC,EAAE,OAAO,CAAC;CACpB"}
|
package/library/utils/index.d.ts
CHANGED
|
@@ -15,4 +15,18 @@ export { createPostMessage, isValidPostMessage, validatePostMessage, validatePro
|
|
|
15
15
|
export * from './cache';
|
|
16
16
|
export * from './path-match';
|
|
17
17
|
export * from './cookie';
|
|
18
|
+
/**
|
|
19
|
+
* Detect Content-Type based on data type
|
|
20
|
+
* @param data The data to detect Content-Type for
|
|
21
|
+
* @param options Options for detection
|
|
22
|
+
* @param options.checkStream Whether to check for IframeWritableStream (default: false)
|
|
23
|
+
* @param options.isIframeWritableStream Optional function to check if data is a stream (required if checkStream is true)
|
|
24
|
+
* @returns The detected Content-Type, or null if Content-Type should not be auto-set
|
|
25
|
+
*/
|
|
26
|
+
export declare function detectContentType(data: any, options?: {
|
|
27
|
+
checkStream?: boolean;
|
|
28
|
+
isIframeWritableStream?: (value: any) => boolean;
|
|
29
|
+
}): string | null;
|
|
30
|
+
/** Convert Blob to base64 string */
|
|
31
|
+
export declare function blobToBase64(blob: Blob): Promise<string>;
|
|
18
32
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,wBAAgB,iBAAiB,IAAI,MAAM,CAE1C;AAED;;GAEG;AACH,wBAAgB,kBAAkB,IAAI,MAAM,CAE3C;AAED;;GAEG;AACH,wBAAgB,qBAAqB,CAAC,MAAM,EAAE,iBAAiB,GAAG,MAAM,CASvE;AAGD,wBAAgB,SAAS,CAAC,CAAC,EAAE,KAAK,EAAE,GAAG,GAAG,KAAK,IAAI,OAAO,CAAC,CAAC,CAAC,CAE5D;AAED,OAAO,EACL,iBAAiB,EACjB,kBAAkB,EAClB,mBAAmB,EACnB,uBAAuB,EACvB,sBAAsB,EACtB,kBAAkB,EAClB,mBAAmB,EACpB,MAAM,YAAY,CAAC;AAGpB,cAAc,SAAS,CAAC;AAGxB,cAAc,cAAc,CAAC;AAG7B,cAAc,UAAU,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,wBAAgB,iBAAiB,IAAI,MAAM,CAE1C;AAED;;GAEG;AACH,wBAAgB,kBAAkB,IAAI,MAAM,CAE3C;AAED;;GAEG;AACH,wBAAgB,qBAAqB,CAAC,MAAM,EAAE,iBAAiB,GAAG,MAAM,CASvE;AAGD,wBAAgB,SAAS,CAAC,CAAC,EAAE,KAAK,EAAE,GAAG,GAAG,KAAK,IAAI,OAAO,CAAC,CAAC,CAAC,CAE5D;AAED,OAAO,EACL,iBAAiB,EACjB,kBAAkB,EAClB,mBAAmB,EACnB,uBAAuB,EACvB,sBAAsB,EACtB,kBAAkB,EAClB,mBAAmB,EACpB,MAAM,YAAY,CAAC;AAGpB,cAAc,SAAS,CAAC;AAGxB,cAAc,cAAc,CAAC;AAG7B,cAAc,UAAU,CAAC;AAEzB;;;;;;;GAOG;AACH,wBAAgB,iBAAiB,CAC/B,IAAI,EAAE,GAAG,EACT,OAAO,CAAC,EAAE;IAAE,WAAW,CAAC,EAAE,OAAO,CAAC;IAAC,sBAAsB,CAAC,EAAE,CAAC,KAAK,EAAE,GAAG,KAAK,OAAO,CAAA;CAAE,GACpF,MAAM,GAAG,IAAI,CAkEf;AAED,oCAAoC;AACpC,wBAAgB,YAAY,CAAC,IAAI,EAAE,IAAI,GAAG,OAAO,CAAC,MAAM,CAAC,CAWxD"}
|
package/library/utils/index.js
CHANGED
|
@@ -9,6 +9,8 @@ var _exportNames = {
|
|
|
9
9
|
generateInstanceId: true,
|
|
10
10
|
getIframeTargetOrigin: true,
|
|
11
11
|
isPromise: true,
|
|
12
|
+
detectContentType: true,
|
|
13
|
+
blobToBase64: true,
|
|
12
14
|
createPostMessage: true,
|
|
13
15
|
isValidPostMessage: true,
|
|
14
16
|
validatePostMessage: true,
|
|
@@ -17,12 +19,14 @@ var _exportNames = {
|
|
|
17
19
|
getProtocolVersion: true,
|
|
18
20
|
isCompatibleVersion: true
|
|
19
21
|
};
|
|
22
|
+
exports.blobToBase64 = blobToBase64;
|
|
20
23
|
Object.defineProperty(exports, "createPostMessage", {
|
|
21
24
|
enumerable: true,
|
|
22
25
|
get: function get() {
|
|
23
26
|
return _protocol.createPostMessage;
|
|
24
27
|
}
|
|
25
28
|
});
|
|
29
|
+
exports.detectContentType = detectContentType;
|
|
26
30
|
exports.generateInstanceId = generateInstanceId;
|
|
27
31
|
exports.generateRequestId = generateRequestId;
|
|
28
32
|
exports.getIframeTargetOrigin = getIframeTargetOrigin;
|
|
@@ -63,8 +67,11 @@ Object.defineProperty(exports, "validateProtocolVersion", {
|
|
|
63
67
|
return _protocol.validateProtocolVersion;
|
|
64
68
|
}
|
|
65
69
|
});
|
|
70
|
+
require("core-js/modules/es.array.includes.js");
|
|
66
71
|
require("core-js/modules/es.array.iterator.js");
|
|
72
|
+
require("core-js/modules/es.promise.js");
|
|
67
73
|
require("core-js/modules/es.regexp.to-string.js");
|
|
74
|
+
require("core-js/modules/es.string.includes.js");
|
|
68
75
|
require("core-js/modules/web.dom-collections.iterator.js");
|
|
69
76
|
require("core-js/modules/web.url.js");
|
|
70
77
|
require("core-js/modules/web.url.to-json.js");
|
|
@@ -142,4 +149,95 @@ function isPromise(value) {
|
|
|
142
149
|
|
|
143
150
|
// Export path matching functions
|
|
144
151
|
|
|
145
|
-
// Export Cookie-related functions
|
|
152
|
+
// Export Cookie-related functions
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Detect Content-Type based on data type
|
|
156
|
+
* @param data The data to detect Content-Type for
|
|
157
|
+
* @param options Options for detection
|
|
158
|
+
* @param options.checkStream Whether to check for IframeWritableStream (default: false)
|
|
159
|
+
* @param options.isIframeWritableStream Optional function to check if data is a stream (required if checkStream is true)
|
|
160
|
+
* @returns The detected Content-Type, or null if Content-Type should not be auto-set
|
|
161
|
+
*/
|
|
162
|
+
function detectContentType(data, options) {
|
|
163
|
+
if (data === null || data === undefined) return null;
|
|
164
|
+
var _ref = options || {},
|
|
165
|
+
_ref$checkStream = _ref.checkStream,
|
|
166
|
+
checkStream = _ref$checkStream === void 0 ? false : _ref$checkStream,
|
|
167
|
+
isIframeWritableStream = _ref.isIframeWritableStream;
|
|
168
|
+
|
|
169
|
+
// Stream - handled separately (only for response)
|
|
170
|
+
if (checkStream && isIframeWritableStream) {
|
|
171
|
+
if (isIframeWritableStream(data)) {
|
|
172
|
+
return null; // Stream will be handled by sendStream
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// File
|
|
177
|
+
if (typeof File !== 'undefined' && data instanceof File) {
|
|
178
|
+
return data.type || 'application/octet-stream';
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// Blob
|
|
182
|
+
if (typeof Blob !== 'undefined' && data instanceof Blob) {
|
|
183
|
+
return data.type || 'application/octet-stream';
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// ArrayBuffer
|
|
187
|
+
if (typeof ArrayBuffer !== 'undefined' && data instanceof ArrayBuffer) {
|
|
188
|
+
return 'application/octet-stream';
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
// FormData
|
|
192
|
+
if (typeof FormData !== 'undefined' && data instanceof FormData) {
|
|
193
|
+
// FormData typically doesn't need Content-Type header (browser sets it with boundary)
|
|
194
|
+
return null;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
// URLSearchParams
|
|
198
|
+
if (typeof URLSearchParams !== 'undefined' && data instanceof URLSearchParams) {
|
|
199
|
+
return 'application/x-www-form-urlencoded';
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
// String - check if it's JSON string
|
|
203
|
+
if (typeof data === 'string') {
|
|
204
|
+
// Try to parse as JSON, if successful, treat as JSON
|
|
205
|
+
try {
|
|
206
|
+
JSON.parse(data);
|
|
207
|
+
return 'application/json';
|
|
208
|
+
} catch (_unused) {
|
|
209
|
+
return 'text/plain; charset=utf-8';
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// Number, boolean - treat as JSON
|
|
214
|
+
if (typeof data === 'number' || typeof data === 'boolean') {
|
|
215
|
+
return 'application/json';
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
// Plain object or array - treat as JSON
|
|
219
|
+
if (typeof data === 'object') {
|
|
220
|
+
// Exclude common binary/file types (already checked above, but double-check for safety)
|
|
221
|
+
if (typeof Blob !== 'undefined' && data instanceof Blob) return null;
|
|
222
|
+
if (typeof File !== 'undefined' && data instanceof File) return null;
|
|
223
|
+
if (typeof ArrayBuffer !== 'undefined' && data instanceof ArrayBuffer) return null;
|
|
224
|
+
if (typeof FormData !== 'undefined' && data instanceof FormData) return null;
|
|
225
|
+
if (typeof URLSearchParams !== 'undefined' && data instanceof URLSearchParams) return null;
|
|
226
|
+
return 'application/json';
|
|
227
|
+
}
|
|
228
|
+
return null;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/** Convert Blob to base64 string */
|
|
232
|
+
function blobToBase64(blob) {
|
|
233
|
+
return new Promise((resolve, reject) => {
|
|
234
|
+
var reader = new FileReader();
|
|
235
|
+
reader.onloadend = () => {
|
|
236
|
+
var result = reader.result;
|
|
237
|
+
var base64 = result.includes(',') ? result.split(',')[1] : result;
|
|
238
|
+
resolve(base64);
|
|
239
|
+
};
|
|
240
|
+
reader.onerror = reject;
|
|
241
|
+
reader.readAsDataURL(blob);
|
|
242
|
+
});
|
|
243
|
+
}
|
|
@@ -14,4 +14,20 @@ export type PathPattern = string;
|
|
|
14
14
|
* @returns whether matches
|
|
15
15
|
*/
|
|
16
16
|
export declare function matchPath(path: string, matcher: PathMatcher): boolean;
|
|
17
|
+
/**
|
|
18
|
+
* Path match result with extracted parameters
|
|
19
|
+
*/
|
|
20
|
+
export interface PathMatchResult {
|
|
21
|
+
/** Whether the path matches */
|
|
22
|
+
match: boolean;
|
|
23
|
+
/** Extracted path parameters (e.g., { id: '123' } for '/api/users/:id' and '/api/users/123') */
|
|
24
|
+
params: Record<string, string>;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Match path with parameter extraction (supports :param syntax like Express)
|
|
28
|
+
* @param path request path
|
|
29
|
+
* @param pattern path pattern with parameters (e.g., '/api/users/:id')
|
|
30
|
+
* @returns match result with extracted parameters
|
|
31
|
+
*/
|
|
32
|
+
export declare function matchPathWithParams(path: string, pattern: string): PathMatchResult;
|
|
17
33
|
//# sourceMappingURL=path-match.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"path-match.d.ts","sourceRoot":"","sources":["../../src/utils/path-match.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,MAAM,WAAW,GAAG,MAAM,GAAG,MAAM,GAAG,WAAW,GAAG,KAAK,CAAC,MAAM,GAAG,MAAM,GAAG,WAAW,CAAC,CAAC;AAE/F;;;GAGG;AACH,MAAM,MAAM,WAAW,GAAG,MAAM,CAAC;AAEjC;;;;;GAKG;AACH,wBAAgB,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,WAAW,GAAG,OAAO,
|
|
1
|
+
{"version":3,"file":"path-match.d.ts","sourceRoot":"","sources":["../../src/utils/path-match.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,MAAM,WAAW,GAAG,MAAM,GAAG,MAAM,GAAG,WAAW,GAAG,KAAK,CAAC,MAAM,GAAG,MAAM,GAAG,WAAW,CAAC,CAAC;AAE/F;;;GAGG;AACH,MAAM,MAAM,WAAW,GAAG,MAAM,CAAC;AAEjC;;;;;GAKG;AACH,wBAAgB,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,WAAW,GAAG,OAAO,CAoDrE;AAkBD;;GAEG;AACH,MAAM,WAAW,eAAe;IAC9B,+BAA+B;IAC/B,KAAK,EAAE,OAAO,CAAC;IACf,gGAAgG;IAChG,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CAChC;AAED;;;;;GAKG;AACH,wBAAgB,mBAAmB,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,eAAe,CAiDlF"}
|
|
@@ -4,12 +4,14 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.matchPath = matchPath;
|
|
7
|
+
exports.matchPathWithParams = matchPathWithParams;
|
|
7
8
|
require("core-js/modules/es.array.includes.js");
|
|
8
9
|
require("core-js/modules/es.regexp.constructor.js");
|
|
9
10
|
require("core-js/modules/es.regexp.exec.js");
|
|
10
11
|
require("core-js/modules/es.regexp.to-string.js");
|
|
11
12
|
require("core-js/modules/es.string.ends-with.js");
|
|
12
13
|
require("core-js/modules/es.string.includes.js");
|
|
14
|
+
require("core-js/modules/es.string.match.js");
|
|
13
15
|
require("core-js/modules/es.string.replace.js");
|
|
14
16
|
require("core-js/modules/es.string.starts-with.js");
|
|
15
17
|
/**
|
|
@@ -69,6 +71,11 @@ function matchPath(path, matcher) {
|
|
|
69
71
|
if (matcher.includes('*')) {
|
|
70
72
|
return matchPattern(normalizedPath, normalizedMatcher);
|
|
71
73
|
}
|
|
74
|
+
|
|
75
|
+
// Support parameter patterns (e.g., '/api/users/:id')
|
|
76
|
+
if (matcher.includes(':')) {
|
|
77
|
+
return matchPathWithParams(normalizedPath, normalizedMatcher).match;
|
|
78
|
+
}
|
|
72
79
|
return false;
|
|
73
80
|
}
|
|
74
81
|
return false;
|
|
@@ -87,4 +94,62 @@ function matchPattern(path, pattern) {
|
|
|
87
94
|
.replace(/\*/g, '.*'); // Replace * with .*
|
|
88
95
|
var regex = new RegExp(`^${regexPattern}$`);
|
|
89
96
|
return regex.test(path);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Path match result with extracted parameters
|
|
101
|
+
*/
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Match path with parameter extraction (supports :param syntax like Express)
|
|
105
|
+
* @param path request path
|
|
106
|
+
* @param pattern path pattern with parameters (e.g., '/api/users/:id')
|
|
107
|
+
* @returns match result with extracted parameters
|
|
108
|
+
*/
|
|
109
|
+
function matchPathWithParams(path, pattern) {
|
|
110
|
+
// Normalize paths
|
|
111
|
+
var normalizedPath = path.startsWith('/') ? path : `/${path}`;
|
|
112
|
+
var normalizedPattern = pattern.startsWith('/') ? pattern : `/${pattern}`;
|
|
113
|
+
|
|
114
|
+
// Check if pattern contains parameters (:param)
|
|
115
|
+
if (!normalizedPattern.includes(':')) {
|
|
116
|
+
// No parameters, use exact match
|
|
117
|
+
return {
|
|
118
|
+
match: normalizedPath === normalizedPattern,
|
|
119
|
+
params: {}
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// Extract parameter names from pattern
|
|
124
|
+
var paramNames = [];
|
|
125
|
+
var paramRegex = /:([^/]+)/g;
|
|
126
|
+
var match;
|
|
127
|
+
while ((match = paramRegex.exec(normalizedPattern)) !== null) {
|
|
128
|
+
paramNames.push(match[1]);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// Convert pattern to regex, replacing :param with capture groups
|
|
132
|
+
// '/api/users/:id' -> '^/api/users/([^/]+)$'
|
|
133
|
+
// '/api/users/:id/posts/:postId' -> '^/api/users/([^/]+)/posts/([^/]+)$'
|
|
134
|
+
var regexPattern = normalizedPattern.replace(/[.+?^${}()|[\]\\]/g, '\\$&') // Escape special characters
|
|
135
|
+
.replace(/:[^/]+/g, '([^/]+)'); // Replace :param with capture group
|
|
136
|
+
|
|
137
|
+
var regex = new RegExp(`^${regexPattern}$`);
|
|
138
|
+
var regexMatch = regex.exec(normalizedPath);
|
|
139
|
+
if (!regexMatch) {
|
|
140
|
+
return {
|
|
141
|
+
match: false,
|
|
142
|
+
params: {}
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// Extract parameter values from match groups
|
|
147
|
+
var params = {};
|
|
148
|
+
for (var i = 0; i < paramNames.length; i++) {
|
|
149
|
+
params[paramNames[i]] = regexMatch[i + 1];
|
|
150
|
+
}
|
|
151
|
+
return {
|
|
152
|
+
match: true,
|
|
153
|
+
params
|
|
154
|
+
};
|
|
90
155
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "request-iframe",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.4",
|
|
4
4
|
"description": "Communicate with iframes like sending HTTP requests",
|
|
5
5
|
"main": "library/index.js",
|
|
6
6
|
"types": "library/index.d.ts",
|
|
@@ -20,6 +20,7 @@
|
|
|
20
20
|
"files": [
|
|
21
21
|
"library",
|
|
22
22
|
"react/library",
|
|
23
|
+
"react/package.json",
|
|
23
24
|
"README.md",
|
|
24
25
|
"README.CN.md",
|
|
25
26
|
"QUICKSTART.md",
|
|
@@ -221,14 +221,18 @@ describe('React Hooks', () => {
|
|
|
221
221
|
configurable: true
|
|
222
222
|
});
|
|
223
223
|
|
|
224
|
-
|
|
224
|
+
type Props = { ver: number };
|
|
225
|
+
const { result, rerender } = renderHook(
|
|
226
|
+
({ ver }: Props) => useClient(() => iframeRef.current, undefined, [ver]),
|
|
227
|
+
{ initialProps: { ver: 0 } as Props }
|
|
228
|
+
);
|
|
225
229
|
|
|
226
230
|
// Initially null (ref not set)
|
|
227
231
|
expect(result.current).toBeNull();
|
|
228
232
|
|
|
229
233
|
// Set ref
|
|
230
234
|
iframeRef.current = iframe;
|
|
231
|
-
rerender();
|
|
235
|
+
rerender({ ver: 1 } as Props);
|
|
232
236
|
|
|
233
237
|
await waitFor(() => {
|
|
234
238
|
expect(result.current).toBeDefined();
|
|
@@ -302,31 +306,39 @@ describe('React Hooks', () => {
|
|
|
302
306
|
});
|
|
303
307
|
|
|
304
308
|
describe('useServer', () => {
|
|
305
|
-
it('should create server instance', () => {
|
|
309
|
+
it('should create server instance', async () => {
|
|
306
310
|
const { result } = renderHook(() => useServer());
|
|
307
311
|
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
expect(result.current
|
|
311
|
-
|
|
312
|
+
await waitFor(() => {
|
|
313
|
+
expect(result.current).toBeDefined();
|
|
314
|
+
expect(result.current).not.toBeNull();
|
|
315
|
+
if (result.current) {
|
|
316
|
+
expect(result.current.isOpen).toBe(true);
|
|
317
|
+
}
|
|
318
|
+
}, { timeout: 2000 });
|
|
312
319
|
});
|
|
313
320
|
|
|
314
|
-
it('should create server with options', () => {
|
|
321
|
+
it('should create server with options', async () => {
|
|
315
322
|
const options = { secretKey: 'test-key', ackTimeout: 1000 };
|
|
316
323
|
const { result } = renderHook(() => useServer(options));
|
|
317
324
|
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
expect(result.current
|
|
321
|
-
|
|
325
|
+
await waitFor(() => {
|
|
326
|
+
expect(result.current).toBeDefined();
|
|
327
|
+
expect(result.current).not.toBeNull();
|
|
328
|
+
if (result.current) {
|
|
329
|
+
expect(result.current.secretKey).toBe('test-key');
|
|
330
|
+
}
|
|
331
|
+
}, { timeout: 2000 });
|
|
322
332
|
});
|
|
323
333
|
|
|
324
334
|
it('should destroy server on unmount', async () => {
|
|
325
335
|
const { result, unmount } = renderHook(() => useServer());
|
|
336
|
+
await waitFor(() => {
|
|
337
|
+
expect(result.current).toBeDefined();
|
|
338
|
+
expect(result.current).not.toBeNull();
|
|
339
|
+
}, { timeout: 2000 });
|
|
326
340
|
const server = result.current;
|
|
327
341
|
|
|
328
|
-
expect(server).toBeDefined();
|
|
329
|
-
|
|
330
342
|
unmount();
|
|
331
343
|
|
|
332
344
|
// Server should be destroyed
|
|
@@ -335,16 +347,20 @@ describe('React Hooks', () => {
|
|
|
335
347
|
}
|
|
336
348
|
});
|
|
337
349
|
|
|
338
|
-
it('should create server only once on mount', () => {
|
|
350
|
+
it('should create server only once on mount', async () => {
|
|
339
351
|
const { result, rerender } = renderHook(() => useServer());
|
|
352
|
+
await waitFor(() => {
|
|
353
|
+
expect(result.current).toBeDefined();
|
|
354
|
+
expect(result.current).not.toBeNull();
|
|
355
|
+
}, { timeout: 2000 });
|
|
340
356
|
const server1 = result.current;
|
|
341
357
|
|
|
342
|
-
expect(server1).toBeDefined();
|
|
343
|
-
|
|
344
358
|
rerender();
|
|
345
359
|
|
|
346
360
|
// Should return the same instance when deps is not provided (default empty array)
|
|
347
|
-
|
|
361
|
+
await waitFor(() => {
|
|
362
|
+
expect(result.current).toBeDefined();
|
|
363
|
+
}, { timeout: 2000 });
|
|
348
364
|
// Note: When deps is not provided, useEffect runs only once, so server should be the same
|
|
349
365
|
// But if deps changes, a new server might be created
|
|
350
366
|
expect(result.current).toBe(server1);
|
|
@@ -373,7 +389,7 @@ describe('React Hooks', () => {
|
|
|
373
389
|
});
|
|
374
390
|
|
|
375
391
|
describe('useServerHandler', () => {
|
|
376
|
-
it('should register handler when server is available', () => {
|
|
392
|
+
it('should register handler when server is available', async () => {
|
|
377
393
|
const handler = jest.fn((req, res) => {
|
|
378
394
|
res.send({ success: true });
|
|
379
395
|
});
|
|
@@ -388,7 +404,10 @@ describe('React Hooks', () => {
|
|
|
388
404
|
// Verify handler is registered by checking server internals
|
|
389
405
|
// Since we can't easily test the full message flow, we just verify
|
|
390
406
|
// that the hook doesn't throw and the server is created
|
|
391
|
-
|
|
407
|
+
await waitFor(() => {
|
|
408
|
+
expect(serverInstance).toBeDefined();
|
|
409
|
+
expect(serverInstance).not.toBeNull();
|
|
410
|
+
}, { timeout: 2000 });
|
|
392
411
|
expect(handler).not.toHaveBeenCalled(); // Handler not called yet, just registered
|
|
393
412
|
});
|
|
394
413
|
|
|
@@ -537,7 +556,7 @@ describe('React Hooks', () => {
|
|
|
537
556
|
});
|
|
538
557
|
|
|
539
558
|
describe('useServerHandlerMap', () => {
|
|
540
|
-
it('should register handlers using map when server is available', () => {
|
|
559
|
+
it('should register handlers using map when server is available', async () => {
|
|
541
560
|
const handlers = {
|
|
542
561
|
'api/user': jest.fn((req, res) => res.send({ user: 'test' })),
|
|
543
562
|
'api/post': jest.fn((req, res) => res.send({ post: 'test' }))
|
|
@@ -551,7 +570,10 @@ describe('React Hooks', () => {
|
|
|
551
570
|
});
|
|
552
571
|
|
|
553
572
|
// Verify server is created and handlers are registered
|
|
554
|
-
|
|
573
|
+
await waitFor(() => {
|
|
574
|
+
expect(serverInstance).toBeDefined();
|
|
575
|
+
expect(serverInstance).not.toBeNull();
|
|
576
|
+
}, { timeout: 2000 });
|
|
555
577
|
// Handlers not called yet, just registered
|
|
556
578
|
expect(handlers['api/user']).not.toHaveBeenCalled();
|
|
557
579
|
expect(handlers['api/post']).not.toHaveBeenCalled();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAqD,KAAK,SAAS,EAAE,MAAM,OAAO,CAAC;AAC1F,OAAO,EAGL,KAAK,mBAAmB,EACxB,KAAK,0BAA0B,EAC/B,KAAK,mBAAmB,EACxB,KAAK,0BAA0B,EAC/B,KAAK,aAAa,EACnB,MAAM,OAAO,CAAC;AAEf;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AACH,wBAAgB,SAAS,CACvB,aAAa,EAAE,CAAC,MAAM,iBAAiB,GAAG,MAAM,GAAG,IAAI,CAAC,GAAG,SAAS,CAAC,iBAAiB,GAAG,MAAM,GAAG,IAAI,CAAC,EACvG,OAAO,CAAC,EAAE,0BAA0B,EACpC,IAAI,CAAC,EAAE,SAAS,OAAO,EAAE,GACxB,mBAAmB,GAAG,IAAI,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAqD,KAAK,SAAS,EAAE,MAAM,OAAO,CAAC;AAC1F,OAAO,EAGL,KAAK,mBAAmB,EACxB,KAAK,0BAA0B,EAC/B,KAAK,mBAAmB,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