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.
Files changed (96) hide show
  1. package/QUICKSTART.CN.md +269 -0
  2. package/QUICKSTART.md +269 -0
  3. package/README.CN.md +1369 -0
  4. package/README.md +1016 -0
  5. package/library/__tests__/interceptors.test.ts +124 -0
  6. package/library/__tests__/requestIframe.test.ts +2216 -0
  7. package/library/__tests__/stream.test.ts +650 -0
  8. package/library/__tests__/utils.test.ts +433 -0
  9. package/library/api/client.d.ts +16 -0
  10. package/library/api/client.d.ts.map +1 -0
  11. package/library/api/client.js +72 -0
  12. package/library/api/server.d.ts +16 -0
  13. package/library/api/server.d.ts.map +1 -0
  14. package/library/api/server.js +44 -0
  15. package/library/constants/index.d.ts +209 -0
  16. package/library/constants/index.d.ts.map +1 -0
  17. package/library/constants/index.js +260 -0
  18. package/library/constants/messages.d.ts +80 -0
  19. package/library/constants/messages.d.ts.map +1 -0
  20. package/library/constants/messages.js +123 -0
  21. package/library/core/client.d.ts +99 -0
  22. package/library/core/client.d.ts.map +1 -0
  23. package/library/core/client.js +440 -0
  24. package/library/core/message-handler.d.ts +110 -0
  25. package/library/core/message-handler.d.ts.map +1 -0
  26. package/library/core/message-handler.js +320 -0
  27. package/library/core/request-response.d.ts +59 -0
  28. package/library/core/request-response.d.ts.map +1 -0
  29. package/library/core/request-response.js +337 -0
  30. package/library/core/request.d.ts +17 -0
  31. package/library/core/request.d.ts.map +1 -0
  32. package/library/core/request.js +34 -0
  33. package/library/core/response.d.ts +51 -0
  34. package/library/core/response.d.ts.map +1 -0
  35. package/library/core/response.js +323 -0
  36. package/library/core/server-base.d.ts +86 -0
  37. package/library/core/server-base.d.ts.map +1 -0
  38. package/library/core/server-base.js +257 -0
  39. package/library/core/server-client.d.ts +99 -0
  40. package/library/core/server-client.d.ts.map +1 -0
  41. package/library/core/server-client.js +256 -0
  42. package/library/core/server.d.ts +82 -0
  43. package/library/core/server.d.ts.map +1 -0
  44. package/library/core/server.js +338 -0
  45. package/library/index.d.ts +16 -0
  46. package/library/index.d.ts.map +1 -0
  47. package/library/index.js +211 -0
  48. package/library/interceptors/index.d.ts +41 -0
  49. package/library/interceptors/index.d.ts.map +1 -0
  50. package/library/interceptors/index.js +126 -0
  51. package/library/message/channel.d.ts +107 -0
  52. package/library/message/channel.d.ts.map +1 -0
  53. package/library/message/channel.js +184 -0
  54. package/library/message/dispatcher.d.ts +119 -0
  55. package/library/message/dispatcher.d.ts.map +1 -0
  56. package/library/message/dispatcher.js +249 -0
  57. package/library/message/index.d.ts +5 -0
  58. package/library/message/index.d.ts.map +1 -0
  59. package/library/message/index.js +25 -0
  60. package/library/stream/file-stream.d.ts +48 -0
  61. package/library/stream/file-stream.d.ts.map +1 -0
  62. package/library/stream/file-stream.js +240 -0
  63. package/library/stream/index.d.ts +15 -0
  64. package/library/stream/index.d.ts.map +1 -0
  65. package/library/stream/index.js +83 -0
  66. package/library/stream/readable-stream.d.ts +83 -0
  67. package/library/stream/readable-stream.d.ts.map +1 -0
  68. package/library/stream/readable-stream.js +249 -0
  69. package/library/stream/types.d.ts +165 -0
  70. package/library/stream/types.d.ts.map +1 -0
  71. package/library/stream/types.js +5 -0
  72. package/library/stream/writable-stream.d.ts +60 -0
  73. package/library/stream/writable-stream.d.ts.map +1 -0
  74. package/library/stream/writable-stream.js +348 -0
  75. package/library/types/index.d.ts +408 -0
  76. package/library/types/index.d.ts.map +1 -0
  77. package/library/types/index.js +5 -0
  78. package/library/utils/cache.d.ts +19 -0
  79. package/library/utils/cache.d.ts.map +1 -0
  80. package/library/utils/cache.js +83 -0
  81. package/library/utils/cookie.d.ts +117 -0
  82. package/library/utils/cookie.d.ts.map +1 -0
  83. package/library/utils/cookie.js +365 -0
  84. package/library/utils/debug.d.ts +11 -0
  85. package/library/utils/debug.d.ts.map +1 -0
  86. package/library/utils/debug.js +162 -0
  87. package/library/utils/index.d.ts +13 -0
  88. package/library/utils/index.d.ts.map +1 -0
  89. package/library/utils/index.js +132 -0
  90. package/library/utils/path-match.d.ts +17 -0
  91. package/library/utils/path-match.d.ts.map +1 -0
  92. package/library/utils/path-match.js +90 -0
  93. package/library/utils/protocol.d.ts +61 -0
  94. package/library/utils/protocol.d.ts.map +1 -0
  95. package/library/utils/protocol.js +169 -0
  96. package/package.json +58 -0
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Path matcher type
3
+ */
4
+ export type PathMatcher = string | RegExp | PathPattern | Array<string | RegExp | PathPattern>;
5
+ /**
6
+ * Path pattern (supports wildcards)
7
+ * Example: '/api/*' matches all paths starting with '/api/'
8
+ */
9
+ export type PathPattern = string;
10
+ /**
11
+ * Check if path matches the given matcher
12
+ * @param path request path
13
+ * @param matcher path matcher (string, RegExp, PathPattern, or array)
14
+ * @returns whether matches
15
+ */
16
+ export declare function matchPath(path: string, matcher: PathMatcher): boolean;
17
+ //# sourceMappingURL=path-match.d.ts.map
@@ -0,0 +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,CA+CrE"}
@@ -0,0 +1,90 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.matchPath = matchPath;
7
+ require("core-js/modules/es.array.includes.js");
8
+ require("core-js/modules/es.regexp.constructor.js");
9
+ require("core-js/modules/es.regexp.exec.js");
10
+ require("core-js/modules/es.regexp.to-string.js");
11
+ require("core-js/modules/es.string.ends-with.js");
12
+ require("core-js/modules/es.string.includes.js");
13
+ require("core-js/modules/es.string.replace.js");
14
+ require("core-js/modules/es.string.starts-with.js");
15
+ /**
16
+ * Path matcher type
17
+ */
18
+
19
+ /**
20
+ * Path pattern (supports wildcards)
21
+ * Example: '/api/*' matches all paths starting with '/api/'
22
+ */
23
+
24
+ /**
25
+ * Check if path matches the given matcher
26
+ * @param path request path
27
+ * @param matcher path matcher (string, RegExp, PathPattern, or array)
28
+ * @returns whether matches
29
+ */
30
+ function matchPath(path, matcher) {
31
+ // Normalize path (ensure starts with /)
32
+ var normalizedPath = path.startsWith('/') ? path : `/${path}`;
33
+
34
+ // If array, match if any one matches
35
+ if (Array.isArray(matcher)) {
36
+ return matcher.some(m => matchPath(normalizedPath, m));
37
+ }
38
+
39
+ // If RegExp
40
+ if (matcher instanceof RegExp) {
41
+ return matcher.test(normalizedPath);
42
+ }
43
+
44
+ // If string
45
+ if (typeof matcher === 'string') {
46
+ // Exact match
47
+ if (normalizedPath === matcher) {
48
+ return true;
49
+ }
50
+
51
+ // Normalize matcher (ensure starts with /)
52
+ var normalizedMatcher = matcher.startsWith('/') ? matcher : `/${matcher}`;
53
+
54
+ // Prefix match
55
+ if (normalizedPath.startsWith(normalizedMatcher)) {
56
+ // If matcher is '/api', path is '/api', match
57
+ // If matcher is '/api', path is '/api/users', match
58
+ // If matcher is '/api/', path is '/api/users', match
59
+ // If matcher is '/api', path is '/api2', no match
60
+ if (normalizedMatcher.endsWith('/')) {
61
+ return true;
62
+ }
63
+ // If matcher doesn't end with /, ensure path has / or ends after matcher
64
+ var nextChar = normalizedPath[normalizedMatcher.length];
65
+ return nextChar === undefined || nextChar === '/';
66
+ }
67
+
68
+ // Support wildcard patterns (e.g., '/api/*')
69
+ if (matcher.includes('*')) {
70
+ return matchPattern(normalizedPath, normalizedMatcher);
71
+ }
72
+ return false;
73
+ }
74
+ return false;
75
+ }
76
+
77
+ /**
78
+ * Match path pattern (supports wildcards)
79
+ * @param path request path
80
+ * @param pattern path pattern (e.g., '/api/*')
81
+ */
82
+ function matchPattern(path, pattern) {
83
+ // Convert pattern to regex
84
+ // '/api/*' -> '^/api/.*$'
85
+ // '/api/*/users' -> '^/api/.*/users$'
86
+ var regexPattern = pattern.replace(/[.+?^${}()|[\]\\]/g, '\\$&') // Escape special characters
87
+ .replace(/\*/g, '.*'); // Replace * with .*
88
+ var regex = new RegExp(`^${regexPattern}$`);
89
+ return regex.test(path);
90
+ }
@@ -0,0 +1,61 @@
1
+ import { PostMessageData } from '../types';
2
+ import { ProtocolValidationResult } from '../constants';
3
+ /**
4
+ * Validate protocol version
5
+ *
6
+ * Only checks minimum supported version, not maximum version
7
+ * Because new versions usually maintain backward compatibility with older message formats
8
+ *
9
+ * @param version protocol version number
10
+ * @returns validation result
11
+ */
12
+ export declare function validateProtocolVersion(version: number): ProtocolValidationResult;
13
+ /**
14
+ * Validate PostMessage data format (full validation)
15
+ * @param data data to validate
16
+ * @returns validation result, including protocol version info
17
+ */
18
+ export declare function validatePostMessage(data: any): ProtocolValidationResult & {
19
+ data?: PostMessageData;
20
+ };
21
+ /**
22
+ * Check if data is a request-iframe framework message (only checks basic format, doesn't validate version compatibility)
23
+ * @param data data to check
24
+ * @returns whether it's a request-iframe message
25
+ */
26
+ export declare function isRequestIframeMessage(data: any): boolean;
27
+ /**
28
+ * Create PostMessage data
29
+ * @param type message type
30
+ * @param requestId request ID
31
+ * @param data additional data
32
+ * @returns PostMessageData
33
+ */
34
+ export declare function createPostMessage(type: PostMessageData['type'], requestId: string, data?: Partial<Omit<PostMessageData, '__requestIframe__' | 'type' | 'requestId' | 'timestamp'>>): PostMessageData;
35
+ /**
36
+ * Validate PostMessage data format (only checks basic format, doesn't validate version compatibility)
37
+ * Used for quick determination of whether it's a request-iframe framework message
38
+ *
39
+ * Note: This method doesn't check protocol version compatibility, use isCompatibleVersion for version compatibility check
40
+ *
41
+ * @param data data to validate
42
+ * @returns whether it's a valid PostMessageData
43
+ */
44
+ export declare function isValidPostMessage(data: any): data is PostMessageData;
45
+ /**
46
+ * Get protocol version from message
47
+ * @param data PostMessageData
48
+ * @returns protocol version number, undefined if invalid
49
+ */
50
+ export declare function getProtocolVersion(data: any): number | undefined;
51
+ /**
52
+ * Check if protocol version is compatible
53
+ *
54
+ * Only checks minimum supported version, not maximum version
55
+ * Because new versions usually maintain backward compatibility with older message formats
56
+ *
57
+ * @param version protocol version number
58
+ * @returns whether compatible
59
+ */
60
+ export declare function isCompatibleVersion(version: number): boolean;
61
+ //# sourceMappingURL=protocol.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"protocol.d.ts","sourceRoot":"","sources":["../../src/utils/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,CAAC,CAAC,GAC9F,eAAe,CAQjB;AAED;;;;;;;;GAQG;AACH,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,GAAG,GAAG,IAAI,IAAI,eAAe,CAQrE;AAED;;;;GAIG;AACH,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,GAAG,GAAG,MAAM,GAAG,SAAS,CAKhE;AAED;;;;;;;;GAQG;AACH,wBAAgB,mBAAmB,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAE5D"}
@@ -0,0 +1,169 @@
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
+ require("core-js/modules/web.dom-collections.for-each.js");
6
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
7
+ Object.defineProperty(exports, "__esModule", {
8
+ value: true
9
+ });
10
+ exports.createPostMessage = createPostMessage;
11
+ exports.getProtocolVersion = getProtocolVersion;
12
+ exports.isCompatibleVersion = isCompatibleVersion;
13
+ exports.isRequestIframeMessage = isRequestIframeMessage;
14
+ exports.isValidPostMessage = isValidPostMessage;
15
+ exports.validatePostMessage = validatePostMessage;
16
+ exports.validateProtocolVersion = validateProtocolVersion;
17
+ var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
18
+ var _constants = require("../constants");
19
+ 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; }
20
+ 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; }
21
+ /**
22
+ * Validate protocol version
23
+ *
24
+ * Only checks minimum supported version, not maximum version
25
+ * Because new versions usually maintain backward compatibility with older message formats
26
+ *
27
+ * @param version protocol version number
28
+ * @returns validation result
29
+ */
30
+ function validateProtocolVersion(version) {
31
+ if (typeof version !== 'number' || !Number.isInteger(version)) {
32
+ return {
33
+ valid: false,
34
+ error: _constants.Messages.INVALID_PROTOCOL_VERSION_FORMAT,
35
+ errorCode: 'INVALID_FORMAT'
36
+ };
37
+ }
38
+ if (version < _constants.ProtocolVersion.MIN_SUPPORTED) {
39
+ return {
40
+ valid: false,
41
+ version,
42
+ error: (0, _constants.formatMessage)(_constants.Messages.PROTOCOL_VERSION_TOO_LOW, version, _constants.ProtocolVersion.MIN_SUPPORTED),
43
+ errorCode: 'VERSION_TOO_LOW'
44
+ };
45
+ }
46
+
47
+ // Don't check maximum version, new versions usually maintain backward compatibility
48
+ return {
49
+ valid: true,
50
+ version
51
+ };
52
+ }
53
+
54
+ /**
55
+ * Validate PostMessage data format (full validation)
56
+ * @param data data to validate
57
+ * @returns validation result, including protocol version info
58
+ */
59
+ function validatePostMessage(data) {
60
+ // Basic format validation
61
+ if (!data || typeof data !== 'object') {
62
+ return {
63
+ valid: false,
64
+ error: _constants.Messages.INVALID_MESSAGE_FORMAT_NOT_OBJECT,
65
+ errorCode: 'INVALID_FORMAT'
66
+ };
67
+ }
68
+
69
+ // Protocol identifier validation
70
+ if (data.__requestIframe__ === undefined) {
71
+ return {
72
+ valid: false,
73
+ error: _constants.Messages.INVALID_MESSAGE_FORMAT_MISSING_PROTOCOL,
74
+ errorCode: 'INVALID_FORMAT'
75
+ };
76
+ }
77
+
78
+ // Protocol version validation
79
+ var versionResult = validateProtocolVersion(data.__requestIframe__);
80
+ if (!versionResult.valid) {
81
+ return versionResult;
82
+ }
83
+
84
+ // Required field validation
85
+ if (typeof data.type !== 'string') {
86
+ return {
87
+ valid: false,
88
+ version: versionResult.version,
89
+ error: _constants.Messages.INVALID_MESSAGE_FORMAT_MISSING_TYPE,
90
+ errorCode: 'INVALID_FORMAT'
91
+ };
92
+ }
93
+ if (typeof data.requestId !== 'string') {
94
+ return {
95
+ valid: false,
96
+ version: versionResult.version,
97
+ error: _constants.Messages.INVALID_MESSAGE_FORMAT_MISSING_REQUEST_ID,
98
+ errorCode: 'INVALID_FORMAT'
99
+ };
100
+ }
101
+ return {
102
+ valid: true,
103
+ version: versionResult.version,
104
+ data: data
105
+ };
106
+ }
107
+
108
+ /**
109
+ * Check if data is a request-iframe framework message (only checks basic format, doesn't validate version compatibility)
110
+ * @param data data to check
111
+ * @returns whether it's a request-iframe message
112
+ */
113
+ function isRequestIframeMessage(data) {
114
+ return !!(data && typeof data === 'object' && typeof data.__requestIframe__ === 'number' && typeof data.type === 'string' && typeof data.requestId === 'string');
115
+ }
116
+
117
+ /**
118
+ * Create PostMessage data
119
+ * @param type message type
120
+ * @param requestId request ID
121
+ * @param data additional data
122
+ * @returns PostMessageData
123
+ */
124
+ function createPostMessage(type, requestId, data) {
125
+ return _objectSpread({
126
+ __requestIframe__: _constants.ProtocolVersion.CURRENT,
127
+ timestamp: Date.now(),
128
+ type,
129
+ requestId
130
+ }, data);
131
+ }
132
+
133
+ /**
134
+ * Validate PostMessage data format (only checks basic format, doesn't validate version compatibility)
135
+ * Used for quick determination of whether it's a request-iframe framework message
136
+ *
137
+ * Note: This method doesn't check protocol version compatibility, use isCompatibleVersion for version compatibility check
138
+ *
139
+ * @param data data to validate
140
+ * @returns whether it's a valid PostMessageData
141
+ */
142
+ function isValidPostMessage(data) {
143
+ return !!(data && typeof data === 'object' && typeof data.__requestIframe__ === 'number' && typeof data.type === 'string' && typeof data.requestId === 'string');
144
+ }
145
+
146
+ /**
147
+ * Get protocol version from message
148
+ * @param data PostMessageData
149
+ * @returns protocol version number, undefined if invalid
150
+ */
151
+ function getProtocolVersion(data) {
152
+ if (data && typeof data === 'object' && typeof data.__requestIframe__ === 'number') {
153
+ return data.__requestIframe__;
154
+ }
155
+ return undefined;
156
+ }
157
+
158
+ /**
159
+ * Check if protocol version is compatible
160
+ *
161
+ * Only checks minimum supported version, not maximum version
162
+ * Because new versions usually maintain backward compatibility with older message formats
163
+ *
164
+ * @param version protocol version number
165
+ * @returns whether compatible
166
+ */
167
+ function isCompatibleVersion(version) {
168
+ return version >= _constants.ProtocolVersion.MIN_SUPPORTED;
169
+ }
package/package.json ADDED
@@ -0,0 +1,58 @@
1
+ {
2
+ "name": "request-iframe",
3
+ "version": "0.0.1",
4
+ "description": "Communicate with iframes like sending HTTP requests",
5
+ "main": "library/index.js",
6
+ "types": "library/index.d.ts",
7
+ "scripts": {
8
+ "build": "npm run build:types && npm run build:js",
9
+ "build:types": "tsc --emitDeclarationOnly",
10
+ "build:js": "babel src --out-dir library --extensions .ts --copy-files",
11
+ "test": "jest",
12
+ "test:watch": "jest --watch",
13
+ "test:coverage": "jest --coverage",
14
+ "lint": "eslint src --ext .ts",
15
+ "lint:fix": "eslint src --ext .ts --fix",
16
+ "prepare": "npm run build"
17
+ },
18
+ "files": [
19
+ "library",
20
+ "README.md",
21
+ "README.CN.md",
22
+ "QUICKSTART.md",
23
+ "QUICKSTART.CN.md"
24
+ ],
25
+ "keywords": [
26
+ "iframe",
27
+ "postMessage",
28
+ "request",
29
+ "axios"
30
+ ],
31
+ "repository": {
32
+ "type": "git",
33
+ "url": "https://github.com/gxlmyacc/request-iframe"
34
+ },
35
+ "author": "gxlmyacc",
36
+ "license": "MIT",
37
+ "dependencies": {
38
+ "core-js": "^3.48.0"
39
+ },
40
+ "devDependencies": {
41
+ "@babel/cli": "^7.28.6",
42
+ "@babel/core": "^7.28.6",
43
+ "@babel/plugin-transform-runtime": "^7.28.5",
44
+ "@babel/preset-env": "^7.28.6",
45
+ "@babel/preset-typescript": "^7.28.5",
46
+ "@babel/runtime": "^7.28.6",
47
+ "@types/jest": "^29.5.12",
48
+ "@types/node": "^20.11.24",
49
+ "@typescript-eslint/eslint-plugin": "^6.21.0",
50
+ "@typescript-eslint/parser": "^6.21.0",
51
+ "coveralls": "^3.1.1",
52
+ "eslint": "^8.56.0",
53
+ "jest": "^29.7.0",
54
+ "jest-environment-jsdom": "^29.7.0",
55
+ "ts-jest": "^29.1.2",
56
+ "typescript": "^5.3.3"
57
+ }
58
+ }