request-iframe 0.0.2 → 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.
Files changed (84) hide show
  1. package/QUICKSTART.CN.md +35 -8
  2. package/QUICKSTART.md +35 -8
  3. package/README.CN.md +439 -36
  4. package/README.md +496 -30
  5. package/library/__tests__/channel.test.ts +420 -0
  6. package/library/__tests__/coverage-branches.test.ts +356 -0
  7. package/library/__tests__/debug.test.ts +588 -0
  8. package/library/__tests__/dispatcher.test.ts +481 -0
  9. package/library/__tests__/requestIframe.test.ts +3163 -185
  10. package/library/__tests__/server.test.ts +738 -0
  11. package/library/__tests__/stream.test.ts +46 -15
  12. package/library/api/client.d.ts.map +1 -1
  13. package/library/api/client.js +12 -6
  14. package/library/api/server.d.ts +4 -3
  15. package/library/api/server.d.ts.map +1 -1
  16. package/library/api/server.js +25 -7
  17. package/library/constants/index.d.ts +14 -4
  18. package/library/constants/index.d.ts.map +1 -1
  19. package/library/constants/index.js +15 -7
  20. package/library/constants/messages.d.ts +37 -0
  21. package/library/constants/messages.d.ts.map +1 -1
  22. package/library/constants/messages.js +38 -1
  23. package/library/core/client-server.d.ts +105 -0
  24. package/library/core/client-server.d.ts.map +1 -0
  25. package/library/core/client-server.js +289 -0
  26. package/library/core/client.d.ts +53 -10
  27. package/library/core/client.d.ts.map +1 -1
  28. package/library/core/client.js +529 -207
  29. package/library/core/request.d.ts +3 -1
  30. package/library/core/request.d.ts.map +1 -1
  31. package/library/core/request.js +2 -1
  32. package/library/core/response.d.ts +30 -4
  33. package/library/core/response.d.ts.map +1 -1
  34. package/library/core/response.js +176 -100
  35. package/library/core/server-client.d.ts +3 -1
  36. package/library/core/server-client.d.ts.map +1 -1
  37. package/library/core/server-client.js +19 -9
  38. package/library/core/server.d.ts +22 -1
  39. package/library/core/server.d.ts.map +1 -1
  40. package/library/core/server.js +304 -55
  41. package/library/index.d.ts +3 -2
  42. package/library/index.d.ts.map +1 -1
  43. package/library/index.js +34 -5
  44. package/library/interceptors/index.d.ts.map +1 -1
  45. package/library/message/channel.d.ts +3 -1
  46. package/library/message/channel.d.ts.map +1 -1
  47. package/library/message/dispatcher.d.ts +7 -2
  48. package/library/message/dispatcher.d.ts.map +1 -1
  49. package/library/message/dispatcher.js +48 -2
  50. package/library/message/index.d.ts.map +1 -1
  51. package/library/stream/file-stream.d.ts +5 -0
  52. package/library/stream/file-stream.d.ts.map +1 -1
  53. package/library/stream/file-stream.js +41 -12
  54. package/library/stream/index.d.ts +11 -1
  55. package/library/stream/index.d.ts.map +1 -1
  56. package/library/stream/index.js +21 -3
  57. package/library/stream/readable-stream.d.ts.map +1 -1
  58. package/library/stream/readable-stream.js +32 -30
  59. package/library/stream/types.d.ts +20 -2
  60. package/library/stream/types.d.ts.map +1 -1
  61. package/library/stream/writable-stream.d.ts +2 -1
  62. package/library/stream/writable-stream.d.ts.map +1 -1
  63. package/library/stream/writable-stream.js +13 -10
  64. package/library/types/index.d.ts +106 -32
  65. package/library/types/index.d.ts.map +1 -1
  66. package/library/utils/cache.d.ts +24 -0
  67. package/library/utils/cache.d.ts.map +1 -1
  68. package/library/utils/cache.js +76 -0
  69. package/library/utils/cookie.d.ts.map +1 -1
  70. package/library/utils/debug.d.ts.map +1 -1
  71. package/library/utils/debug.js +382 -20
  72. package/library/utils/index.d.ts +19 -0
  73. package/library/utils/index.d.ts.map +1 -1
  74. package/library/utils/index.js +113 -2
  75. package/library/utils/path-match.d.ts +16 -0
  76. package/library/utils/path-match.d.ts.map +1 -1
  77. package/library/utils/path-match.js +65 -0
  78. package/library/utils/protocol.d.ts.map +1 -1
  79. package/package.json +4 -1
  80. package/react/library/__tests__/index.test.tsx +274 -281
  81. package/react/library/index.d.ts +4 -3
  82. package/react/library/index.d.ts.map +1 -1
  83. package/react/library/index.js +225 -158
  84. package/react/package.json +7 -0
@@ -6,7 +6,11 @@ Object.defineProperty(exports, "__esModule", {
6
6
  });
7
7
  var _exportNames = {
8
8
  generateRequestId: true,
9
+ generateInstanceId: true,
9
10
  getIframeTargetOrigin: true,
11
+ isPromise: true,
12
+ detectContentType: true,
13
+ blobToBase64: true,
10
14
  createPostMessage: true,
11
15
  isValidPostMessage: true,
12
16
  validatePostMessage: true,
@@ -15,12 +19,15 @@ var _exportNames = {
15
19
  getProtocolVersion: true,
16
20
  isCompatibleVersion: true
17
21
  };
22
+ exports.blobToBase64 = blobToBase64;
18
23
  Object.defineProperty(exports, "createPostMessage", {
19
24
  enumerable: true,
20
25
  get: function get() {
21
26
  return _protocol.createPostMessage;
22
27
  }
23
28
  });
29
+ exports.detectContentType = detectContentType;
30
+ exports.generateInstanceId = generateInstanceId;
24
31
  exports.generateRequestId = generateRequestId;
25
32
  exports.getIframeTargetOrigin = getIframeTargetOrigin;
26
33
  Object.defineProperty(exports, "getProtocolVersion", {
@@ -35,6 +42,7 @@ Object.defineProperty(exports, "isCompatibleVersion", {
35
42
  return _protocol.isCompatibleVersion;
36
43
  }
37
44
  });
45
+ exports.isPromise = isPromise;
38
46
  Object.defineProperty(exports, "isRequestIframeMessage", {
39
47
  enumerable: true,
40
48
  get: function get() {
@@ -59,8 +67,11 @@ Object.defineProperty(exports, "validateProtocolVersion", {
59
67
  return _protocol.validateProtocolVersion;
60
68
  }
61
69
  });
70
+ require("core-js/modules/es.array.includes.js");
62
71
  require("core-js/modules/es.array.iterator.js");
72
+ require("core-js/modules/es.promise.js");
63
73
  require("core-js/modules/es.regexp.to-string.js");
74
+ require("core-js/modules/es.string.includes.js");
64
75
  require("core-js/modules/web.dom-collections.iterator.js");
65
76
  require("core-js/modules/web.url.js");
66
77
  require("core-js/modules/web.url.to-json.js");
@@ -109,6 +120,13 @@ function generateRequestId() {
109
120
  return `req_${Date.now()}_${Math.random().toString(36).substring(2, 11)}`;
110
121
  }
111
122
 
123
+ /**
124
+ * Generate unique instance ID
125
+ */
126
+ function generateInstanceId() {
127
+ return `inst_${Date.now()}_${Math.random().toString(36).substring(2, 11)}`;
128
+ }
129
+
112
130
  /**
113
131
  * Derive targetOrigin from iframe.src
114
132
  */
@@ -122,11 +140,104 @@ function getIframeTargetOrigin(iframe) {
122
140
  return '*';
123
141
  }
124
142
  }
125
-
143
+ function isPromise(value) {
144
+ return value !== null && typeof value === 'object' && 'then' in value;
145
+ }
126
146
  // Export protocol-related functions
127
147
 
128
148
  // Export cache-related functions
129
149
 
130
150
  // Export path matching functions
131
151
 
132
- // 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":["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,CA+CrE"}
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
  }
@@ -1 +1 @@
1
- {"version":3,"file":"protocol.d.ts","sourceRoot":"","sources":["protocol.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,UAAU,CAAC;AAC3C,OAAO,EAAmB,wBAAwB,EAA2B,MAAM,cAAc,CAAC;AAElG;;;;;;;;GAQG;AACH,wBAAgB,uBAAuB,CAAC,OAAO,EAAE,MAAM,GAAG,wBAAwB,CAuBjF;AAED;;;;GAIG;AACH,wBAAgB,mBAAmB,CAAC,IAAI,EAAE,GAAG,GAAG,wBAAwB,GAAG;IAAE,IAAI,CAAC,EAAE,eAAe,CAAA;CAAE,CAiDpG;AAED;;;;GAIG;AACH,wBAAgB,sBAAsB,CAAC,IAAI,EAAE,GAAG,GAAG,OAAO,CAQzD;AAED;;;;;;GAMG;AACH,wBAAgB,iBAAiB,CAC/B,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,CAA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ACH,wBAAgB,mBAAmB,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAE5D"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "request-iframe",
3
- "version": "0.0.2",
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,14 +20,17 @@
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",
26
27
  "QUICKSTART.CN.md"
27
28
  ],
28
29
  "keywords": [
30
+ "request-iframe",
29
31
  "iframe",
30
32
  "postMessage",
33
+ "http",
31
34
  "request",
32
35
  "axios"
33
36
  ],