@api-client/core 0.3.3 → 0.3.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.
Files changed (259) hide show
  1. package/build/browser.d.ts +3 -0
  2. package/build/browser.js +9 -0
  3. package/build/browser.js.map +1 -1
  4. package/build/index.d.ts +11 -2
  5. package/build/index.js +20 -2
  6. package/build/index.js.map +1 -1
  7. package/build/src/lib/fs/Fs.d.ts +52 -0
  8. package/build/src/lib/fs/Fs.js +245 -0
  9. package/build/src/lib/fs/Fs.js.map +1 -0
  10. package/build/src/lib/timers/Timers.d.ts +5 -0
  11. package/build/src/lib/timers/Timers.js +10 -0
  12. package/build/src/lib/timers/Timers.js.map +1 -0
  13. package/build/src/mocking/ProjectMock.d.ts +13 -0
  14. package/build/src/mocking/ProjectMock.js +16 -0
  15. package/build/src/mocking/ProjectMock.js.map +1 -0
  16. package/build/src/mocking/lib/Request.d.ts +32 -0
  17. package/build/src/mocking/lib/Request.js +63 -0
  18. package/build/src/mocking/lib/Request.js.map +1 -0
  19. package/build/src/mocking/lib/Response.d.ts +33 -0
  20. package/build/src/mocking/lib/Response.js +79 -0
  21. package/build/src/mocking/lib/Response.js.map +1 -0
  22. package/build/src/models/ErrorResponse.d.ts +5 -4
  23. package/build/src/models/ErrorResponse.js +18 -5
  24. package/build/src/models/ErrorResponse.js.map +1 -1
  25. package/build/src/models/SerializableError.d.ts +30 -0
  26. package/build/src/models/SerializableError.js +63 -0
  27. package/build/src/models/SerializableError.js.map +1 -0
  28. package/build/src/runtime/http-engine/ArcEngine.js +8 -4
  29. package/build/src/runtime/http-engine/ArcEngine.js.map +1 -1
  30. package/build/src/runtime/http-engine/HttpEngine.d.ts +3 -3
  31. package/build/src/runtime/http-engine/HttpEngine.js +3 -3
  32. package/build/src/runtime/http-engine/HttpEngine.js.map +1 -1
  33. package/build/src/runtime/http-engine/NodeEngine.js +9 -4
  34. package/build/src/runtime/http-engine/NodeEngine.js.map +1 -1
  35. package/build/src/runtime/http-engine/NodeEngineDirect.js +8 -2
  36. package/build/src/runtime/http-engine/NodeEngineDirect.js.map +1 -1
  37. package/build/src/runtime/node/BaseRunner.d.ts +21 -0
  38. package/build/src/runtime/node/BaseRunner.js +27 -0
  39. package/build/src/runtime/node/BaseRunner.js.map +1 -0
  40. package/build/src/runtime/node/ProjectParallelRunner.d.ts +81 -0
  41. package/build/src/runtime/node/ProjectParallelRunner.js +173 -0
  42. package/build/src/runtime/node/ProjectParallelRunner.js.map +1 -0
  43. package/build/src/runtime/node/ProjectRequestRunner.d.ts +125 -0
  44. package/build/src/runtime/node/ProjectRequestRunner.js +185 -0
  45. package/build/src/runtime/node/ProjectRequestRunner.js.map +1 -0
  46. package/build/src/runtime/node/ProjectRunner.d.ts +166 -64
  47. package/build/src/runtime/node/ProjectRunner.js +191 -139
  48. package/build/src/runtime/node/ProjectRunner.js.map +1 -1
  49. package/build/src/runtime/node/ProjectRunnerWorker.d.ts +1 -0
  50. package/build/src/runtime/node/ProjectRunnerWorker.js +58 -0
  51. package/build/src/runtime/node/ProjectRunnerWorker.js.map +1 -0
  52. package/build/src/runtime/node/ProjectSerialRunner.d.ts +11 -0
  53. package/build/src/runtime/node/ProjectSerialRunner.js +34 -0
  54. package/build/src/runtime/node/ProjectSerialRunner.js.map +1 -0
  55. package/build/src/runtime/reporters/ProjectRunCliReporter.d.ts +7 -0
  56. package/build/src/runtime/reporters/ProjectRunCliReporter.js +73 -0
  57. package/build/src/runtime/reporters/ProjectRunCliReporter.js.map +1 -0
  58. package/build/src/runtime/reporters/Reporter.d.ts +62 -0
  59. package/build/src/runtime/reporters/Reporter.js +98 -0
  60. package/build/src/runtime/reporters/Reporter.js.map +1 -0
  61. package/build/src/testing/TestCliHelper.d.ts +23 -0
  62. package/build/src/testing/TestCliHelper.js +71 -0
  63. package/build/src/testing/TestCliHelper.js.map +1 -0
  64. package/build/src/testing/getPort.d.ts +52 -0
  65. package/build/src/testing/getPort.js +169 -0
  66. package/build/src/testing/getPort.js.map +1 -0
  67. package/package.json +2 -1
  68. package/src/data/DataReader.ts +11 -0
  69. package/src/data/DataUtils.ts +108 -0
  70. package/src/data/JmesparthReader.ts +26 -0
  71. package/src/data/Json2Xml.ts +190 -0
  72. package/src/data/JsonReader.ts +41 -0
  73. package/src/data/PayloadPointer.ts +48 -0
  74. package/src/data/RequestDataExtractor.ts +133 -0
  75. package/src/data/UrlEncodedReader.ts +20 -0
  76. package/src/data/XmlReader.ts +103 -0
  77. package/src/events/BaseEvents.ts +259 -0
  78. package/src/events/CustomEvent.ts +27 -0
  79. package/src/events/EventTypes.ts +19 -0
  80. package/src/events/Events.ts +19 -0
  81. package/src/events/authorization/AuthorizationEventTypes.ts +22 -0
  82. package/src/events/authorization/AuthorizationEvents.ts +61 -0
  83. package/src/events/cookies/CookieEventTypes.ts +13 -0
  84. package/src/events/cookies/CookieEvents.ts +157 -0
  85. package/src/events/encryption/EncryptionEventTypes.ts +4 -0
  86. package/src/events/encryption/EncryptionEvents.ts +51 -0
  87. package/src/events/environment/EnvironmentEventTypes.ts +3 -0
  88. package/src/events/environment/EnvironmentEvents.ts +24 -0
  89. package/src/events/models/ClientCertificateEvents.ts +87 -0
  90. package/src/events/models/ModelEventTypes.ts +47 -0
  91. package/src/events/models/ModelEvents.ts +7 -0
  92. package/src/events/models/ProjectEvents.ts +331 -0
  93. package/src/events/process/ProcessEventTypes.ts +5 -0
  94. package/src/events/process/ProcessEvents.ts +76 -0
  95. package/src/events/readme.md +22 -0
  96. package/src/events/reporting/ReportingEventTypes.ts +3 -0
  97. package/src/events/reporting/ReportingEvents.ts +28 -0
  98. package/src/events/telemetry/TelemetryEventTypes.ts +10 -0
  99. package/src/events/telemetry/TelemetryEvents.ts +156 -0
  100. package/src/lib/cookies/Cookie.ts +312 -0
  101. package/src/lib/cookies/Cookies.ts +326 -0
  102. package/src/lib/cookies/Utils.ts +168 -0
  103. package/src/lib/fs/Fs.ts +258 -0
  104. package/src/lib/headers/Headers.ts +219 -0
  105. package/src/lib/logging/DefaultLogger.ts +19 -0
  106. package/src/lib/logging/DummyLogger.ts +21 -0
  107. package/src/lib/logging/Logger.ts +16 -0
  108. package/src/lib/timers/Timers.ts +9 -0
  109. package/src/lib/transformers/PayloadSerializer.ts +332 -0
  110. package/src/lib/transformers/Utils.ts +18 -0
  111. package/src/lib/uuid.ts +40 -0
  112. package/src/mocking/LegacyInterfaces.ts +52 -0
  113. package/src/mocking/LegacyMock.ts +37 -0
  114. package/src/mocking/ProjectMock.ts +20 -0
  115. package/src/mocking/legacy/Authorization.ts +39 -0
  116. package/src/mocking/legacy/Certificates.ts +145 -0
  117. package/src/mocking/legacy/Cookies.ts +51 -0
  118. package/src/mocking/legacy/HostRules.ts +43 -0
  119. package/src/mocking/legacy/Http.ts +236 -0
  120. package/src/mocking/legacy/HttpResponse.ts +106 -0
  121. package/src/mocking/legacy/RestApi.ts +68 -0
  122. package/src/mocking/legacy/Urls.ts +44 -0
  123. package/src/mocking/legacy/Variables.ts +53 -0
  124. package/src/mocking/lib/Request.ts +85 -0
  125. package/src/mocking/lib/Response.ts +101 -0
  126. package/src/models/ArcResponse.ts +166 -0
  127. package/src/models/Authorization.ts +481 -0
  128. package/src/models/AuthorizationData.ts +60 -0
  129. package/src/models/Backend.ts +107 -0
  130. package/src/models/ClientCertificate.ts +68 -0
  131. package/src/models/Environment.ts +279 -0
  132. package/src/models/ErrorResponse.ts +113 -0
  133. package/src/models/HistoryIndex.ts +76 -0
  134. package/src/models/HistoryRequest.ts +28 -0
  135. package/src/models/HostRule.ts +163 -0
  136. package/src/models/HttpCookie.ts +285 -0
  137. package/src/models/HttpProject.ts +1294 -0
  138. package/src/models/HttpProjectListItem.ts +23 -0
  139. package/src/models/HttpRequest.ts +124 -0
  140. package/src/models/HttpResponse.ts +143 -0
  141. package/src/models/License.ts +113 -0
  142. package/src/models/ProjectDefinitionProperty.ts +40 -0
  143. package/src/models/ProjectFolder.ts +439 -0
  144. package/src/models/ProjectItem.ts +135 -0
  145. package/src/models/ProjectParent.ts +113 -0
  146. package/src/models/ProjectRequest.ts +277 -0
  147. package/src/models/ProjectSchema.ts +202 -0
  148. package/src/models/Property.ts +423 -0
  149. package/src/models/Provider.ts +98 -0
  150. package/src/models/README.md +20 -0
  151. package/src/models/Request.ts +452 -0
  152. package/src/models/RequestActions.ts +163 -0
  153. package/src/models/RequestAuthorization.ts +115 -0
  154. package/src/models/RequestConfig.ts +317 -0
  155. package/src/models/RequestLog.ts +159 -0
  156. package/src/models/RequestTime.ts +108 -0
  157. package/src/models/RequestUiMeta.ts +258 -0
  158. package/src/models/RequestsSize.ts +65 -0
  159. package/src/models/ResponseAuthorization.ts +104 -0
  160. package/src/models/ResponseRedirect.ts +158 -0
  161. package/src/models/RevisionInfo.ts +37 -0
  162. package/src/models/SentRequest.ts +125 -0
  163. package/src/models/SerializableError.ts +80 -0
  164. package/src/models/SerializablePayload.ts +68 -0
  165. package/src/models/Server.ts +153 -0
  166. package/src/models/Thing.ts +110 -0
  167. package/src/models/Url.ts +90 -0
  168. package/src/models/User.ts +120 -0
  169. package/src/models/WebApi.ts +234 -0
  170. package/src/models/WebApiIndex.ts +122 -0
  171. package/src/models/Workspace.ts +182 -0
  172. package/src/models/actions/Action.ts +213 -0
  173. package/src/models/actions/ActionView.ts +40 -0
  174. package/src/models/actions/Condition.ts +207 -0
  175. package/src/models/actions/ConditionView.ts +42 -0
  176. package/src/models/actions/Enums.ts +29 -0
  177. package/src/models/actions/RunnableAction.ts +144 -0
  178. package/src/models/actions/runnable/DeleteCookieAction.ts +113 -0
  179. package/src/models/actions/runnable/Runnable.ts +9 -0
  180. package/src/models/actions/runnable/SetCookieAction.ts +216 -0
  181. package/src/models/actions/runnable/SetVariableAction.ts +81 -0
  182. package/src/models/legacy/DataExport.ts +172 -0
  183. package/src/models/legacy/Normalizer.ts +110 -0
  184. package/src/models/legacy/actions/Actions.ts +269 -0
  185. package/src/models/legacy/authorization/Authorization.ts +572 -0
  186. package/src/models/legacy/models/ApiTypes.ts +202 -0
  187. package/src/models/legacy/models/ArcLegacyProject.ts +39 -0
  188. package/src/models/legacy/models/AuthData.ts +17 -0
  189. package/src/models/legacy/models/ClientCertificate.ts +95 -0
  190. package/src/models/legacy/models/Cookies.ts +52 -0
  191. package/src/models/legacy/models/HostRule.ts +35 -0
  192. package/src/models/legacy/models/RestApi.ts +49 -0
  193. package/src/models/legacy/models/UrlHistory.ts +37 -0
  194. package/src/models/legacy/models/Variable.ts +43 -0
  195. package/src/models/legacy/models/base.d.ts +95 -0
  196. package/src/models/legacy/request/ArcRequest.ts +405 -0
  197. package/src/models/legacy/request/ArcResponse.ts +177 -0
  198. package/src/models/legacy/request/HistoryData.ts +47 -0
  199. package/src/models/legacy/request/Legacy.ts +45 -0
  200. package/src/models/legacy/request/RequestBody.ts +87 -0
  201. package/src/models/transformers/ArcDexieTransformer.ts +323 -0
  202. package/src/models/transformers/ArcLegacyNormalizer.ts +85 -0
  203. package/src/models/transformers/ArcLegacyTransformer.ts +200 -0
  204. package/src/models/transformers/ArcPouchTransformer.ts +184 -0
  205. package/src/models/transformers/BaseTransformer.ts +116 -0
  206. package/src/models/transformers/ImportUtils.ts +141 -0
  207. package/src/models/transformers/LegacyDataExportToApiProject.ts +76 -0
  208. package/src/models/transformers/LegacyExportProcessor.ts +252 -0
  209. package/src/models/transformers/PostmanBackupTransformer.ts +306 -0
  210. package/src/models/transformers/PostmanDataTransformer.ts +50 -0
  211. package/src/models/transformers/PostmanTransformer.ts +106 -0
  212. package/src/models/transformers/PostmanV21Transformer.ts +311 -0
  213. package/src/models/transformers/PostmanV2Transformer.ts +308 -0
  214. package/src/models/transformers/har.ts +865 -0
  215. package/src/runtime/actions/ActionRunner.ts +83 -0
  216. package/src/runtime/actions/ConditionRunner.ts +194 -0
  217. package/src/runtime/actions/RunnableCondition.ts +57 -0
  218. package/src/runtime/actions/runnable/ActionRunnable.ts +19 -0
  219. package/src/runtime/actions/runnable/DeleteCookieRunnable.ts +39 -0
  220. package/src/runtime/actions/runnable/SetCookieRunnable.ts +92 -0
  221. package/src/runtime/actions/runnable/SetVariableRunnable.ts +53 -0
  222. package/src/runtime/http-engine/ArcEngine.ts +1068 -0
  223. package/src/runtime/http-engine/FormData.ts +85 -0
  224. package/src/runtime/http-engine/HttpEngine.ts +874 -0
  225. package/src/runtime/http-engine/HttpErrorCodes.ts +270 -0
  226. package/src/runtime/http-engine/NodeEngine.ts +792 -0
  227. package/src/runtime/http-engine/NodeEngineDirect.ts +482 -0
  228. package/src/runtime/http-engine/PayloadSupport.ts +84 -0
  229. package/src/runtime/http-engine/RequestUtils.ts +164 -0
  230. package/src/runtime/http-engine/ntlm/Des.ts +345 -0
  231. package/src/runtime/http-engine/ntlm/MD4.ts +135 -0
  232. package/src/runtime/http-engine/ntlm/NtlmAuth.ts +186 -0
  233. package/src/runtime/http-engine/ntlm/NtlmMessage.ts +57 -0
  234. package/src/runtime/modules/BasicAuthCache.ts +133 -0
  235. package/src/runtime/modules/ExecutionResponse.ts +4 -0
  236. package/src/runtime/modules/ModulesRegistry.ts +136 -0
  237. package/src/runtime/modules/RequestAuthorization.ts +110 -0
  238. package/src/runtime/modules/RequestCookies.ts +145 -0
  239. package/src/runtime/node/BaseRunner.ts +29 -0
  240. package/src/runtime/node/ProjectParallelRunner.ts +234 -0
  241. package/src/runtime/node/ProjectRequestRunner.ts +281 -0
  242. package/src/runtime/node/ProjectRunner.ts +374 -0
  243. package/src/runtime/node/ProjectRunnerWorker.ts +62 -0
  244. package/src/runtime/node/ProjectSerialRunner.ts +36 -0
  245. package/src/runtime/node/RequestFactory.ts +422 -0
  246. package/src/runtime/node/VariablesStore.ts +25 -0
  247. package/src/runtime/reporters/ProjectRunCliReporter.ts +79 -0
  248. package/src/runtime/reporters/Reporter.ts +142 -0
  249. package/src/runtime/store/StoreSdk.ts +838 -0
  250. package/src/runtime/variables/Cache.ts +53 -0
  251. package/src/runtime/variables/EvalFunctions.ts +132 -0
  252. package/src/runtime/variables/ProjectVariables.ts +6 -0
  253. package/src/runtime/variables/VariablesProcessor.ts +543 -0
  254. package/src/runtime/variables/VariablesTokenizer.ts +55 -0
  255. package/src/testing/TestCliHelper.ts +76 -0
  256. package/src/testing/getPort.ts +212 -0
  257. package/build/src/runtime/http-engine/Errors.d.ts +0 -10
  258. package/build/src/runtime/http-engine/Errors.js +0 -14
  259. package/build/src/runtime/http-engine/Errors.js.map +0 -1
@@ -0,0 +1,145 @@
1
+ import { CookieEvents } from '../../events/cookies/CookieEvents.js';
2
+ import { Cookies } from '../../lib/cookies/Cookies.js';
3
+ import { Headers } from '../../lib/headers/Headers.js';
4
+ import { IHttpRequest } from '../../models/HttpRequest.js';
5
+ import { ExecutionContext } from './ModulesRegistry.js';
6
+ import { IResponseRedirect } from '../../models/ResponseRedirect.js';
7
+ import { IArcResponse } from '../../models/ArcResponse.js';
8
+ import { IErrorResponse } from '../../models/ErrorResponse.js';
9
+ import { IRequestLog } from '../../models/RequestLog.js';
10
+ import { HttpCookie } from '../../models/HttpCookie.js';
11
+ import { Cookie } from '../../lib/cookies/Cookie.js';
12
+
13
+ /**
14
+ * Get cookies header value for given URL.
15
+ *
16
+ * @param eventsTarget
17
+ * @param url An URL for cookies.
18
+ * @returns A promise that resolves to header value string.
19
+ */
20
+ async function getCookiesHeaderValue(eventsTarget: EventTarget, url: string): Promise<string> {
21
+ const cookies = await CookieEvents.listUrl(eventsTarget, url);
22
+ if (!cookies || !cookies.length) {
23
+ return '';
24
+ }
25
+ return cookies.map((c) => `${c.name}=${c.value}`).join('; ');
26
+ }
27
+
28
+ /**
29
+ * Applies cookie header value to current request headers.
30
+ * If header to be applied is computed then it will alter headers string.
31
+ *
32
+ * Note, this element do not sends `request-headers-changed` event.
33
+ *
34
+ * @param header Computed headers string
35
+ * @param request The request object from the event.
36
+ */
37
+ function applyCookieHeader(header: string, request: IHttpRequest): void {
38
+ const trimmed = header.trim();
39
+ if (!trimmed) {
40
+ return;
41
+ }
42
+ const headers = new Headers(request.headers);
43
+ headers.append('cookie', trimmed);
44
+ request.headers = headers.toString();
45
+ }
46
+
47
+ /**
48
+ * Extracts cookies from the `response` object and returns an object with `cookies` and `expired` properties containing array of cookies, each.
49
+ *
50
+ * @param url The request URL.
51
+ * @param redirects List of redirect responses
52
+ * @returns An object with `cookies` and `expired` arrays of cookies.
53
+ */
54
+ function extract(response: IArcResponse, url: string, redirects?: IResponseRedirect[]): Record<'expired'|'cookies', Cookie[]> {
55
+ let expired: Cookie[] = [];
56
+ let parser;
57
+ let exp;
58
+ const parsers = [];
59
+ if (redirects && redirects.length) {
60
+ redirects.forEach((r) => {
61
+ const headers = new Headers(r.response.headers);
62
+ if (headers.has('set-cookie')) {
63
+ parser = new Cookies(headers.get('set-cookie'), r.url);
64
+ parser.filter();
65
+ exp = parser.clearExpired();
66
+ if (exp && exp.length) {
67
+ expired = expired.concat(exp);
68
+ }
69
+ parsers.push(parser);
70
+ }
71
+ });
72
+ }
73
+ const headers = new Headers(response.headers);
74
+ if (headers.has('set-cookie')) {
75
+ parser = new Cookies(headers.get('set-cookie'), url);
76
+ parser.filter();
77
+ exp = parser.clearExpired();
78
+ if (exp && exp.length) {
79
+ expired = expired.concat(exp);
80
+ }
81
+ parsers.push(parser);
82
+ }
83
+ let mainParser: Cookies|undefined;
84
+ parsers.forEach((item) => {
85
+ if (!mainParser) {
86
+ mainParser = item;
87
+ return;
88
+ }
89
+ mainParser.merge(item);
90
+ });
91
+ return {
92
+ cookies: mainParser ? mainParser.cookies : [],
93
+ expired
94
+ };
95
+ }
96
+
97
+ /**
98
+ * A request engine request module to apply session cookies to a request.
99
+ * It adds stored session cookies when application configuration applies for it (or request configuration, when apply)
100
+ *
101
+ * Unregister this module when the application settings change to not to use session storage.
102
+ *
103
+ * In ARC electron the session storage is a chrome persistent partition with a session shared with the "log in to a web service".
104
+ * This way cookies can be acquired in through the browser login and store in the application to use them with the request.
105
+ */
106
+ export async function processRequestCookies(request: IHttpRequest, context: ExecutionContext): Promise<void> {
107
+ const editorRequest = request;
108
+ const { config } = context;
109
+ const ignore = config && config.enabled === true && config.ignoreSessionCookies === true;
110
+ if (ignore) {
111
+ return;
112
+ }
113
+ const cookie = await getCookiesHeaderValue(context.eventsTarget, editorRequest.url);
114
+ applyCookieHeader(cookie, editorRequest);
115
+ }
116
+
117
+ /**
118
+ * Processes cookies data from the response and inserts them into the session storage
119
+ */
120
+ export async function processResponseCookies(log: IRequestLog, context: ExecutionContext): Promise<void> {
121
+ if (!log.response || !log.request) {
122
+ return;
123
+ }
124
+ const typedError = log.response as IErrorResponse;
125
+ if (typedError.error) {
126
+ return;
127
+ }
128
+ const config = context.config || {
129
+ kind: 'ARC#RequestConfig',
130
+ enabled: false,
131
+ };
132
+ let ignore = false;
133
+ if (config.enabled !== false && config.ignoreSessionCookies) {
134
+ ignore = true;
135
+ }
136
+
137
+ if (ignore) {
138
+ return;
139
+ }
140
+ const typedResponse = log.response as IArcResponse;
141
+ const result = extract(typedResponse, log.request.url, log.redirects);
142
+ if (result.cookies.length) {
143
+ await CookieEvents.updateBulk(context.eventsTarget, result.cookies.map(c => HttpCookie.fromCookieParser(c).toJSON()));
144
+ }
145
+ }
@@ -0,0 +1,29 @@
1
+ import { EventEmitter } from 'events';
2
+ import { IProjectExecutionIteration, IProjectExecutionLog } from '../reporters/Reporter.js';
3
+
4
+ export abstract class BaseRunner extends EventEmitter {
5
+ /**
6
+ * Iteration start time.
7
+ */
8
+ protected startTime?: number;
9
+ /**
10
+ * Iteration end time.
11
+ */
12
+ protected endTime?: number;
13
+ /**
14
+ * A list of already executed iterations.
15
+ */
16
+ protected executed: IProjectExecutionIteration[] = [];
17
+
18
+ /**
19
+ * Creates the report of the execution.
20
+ */
21
+ protected async createReport(): Promise<IProjectExecutionLog> {
22
+ const log: IProjectExecutionLog = {
23
+ started: this.startTime as number,
24
+ ended: this.endTime as number,
25
+ iterations: this.executed,
26
+ };
27
+ return log;
28
+ }
29
+ }
@@ -0,0 +1,234 @@
1
+ import cluster, { Worker } from 'cluster';
2
+ import { cpus } from 'os';
3
+ import { dirname, join } from 'path';
4
+ import { fileURLToPath } from 'url';
5
+ import { HttpProject, IHttpProject } from '../../models/HttpProject.js';
6
+ import { IProjectRunnerOptions } from './ProjectRunner.js';
7
+ import { IProjectExecutionLog, IProjectExecutionIteration } from '../reporters/Reporter.js';
8
+ import { BaseRunner } from './BaseRunner.js';
9
+
10
+ const numCPUs = cpus().length;
11
+ const __dirname = dirname(fileURLToPath(import.meta.url));
12
+
13
+ export type WorkerStatus = 'initializing' | 'ready' | 'running' | 'finished' | 'error';
14
+
15
+ export interface IWorkerInfo {
16
+ /**
17
+ * Whether the worker is online.
18
+ */
19
+ online: boolean;
20
+ /**
21
+ * The number of iterations the worker is performing.
22
+ */
23
+ iterations: number;
24
+ /**
25
+ * The current status of the worker.
26
+ */
27
+ status: WorkerStatus;
28
+ /**
29
+ * Optional error message received from the worker.
30
+ */
31
+ message?: string;
32
+ }
33
+
34
+ interface WorkerInfoInternal extends IWorkerInfo {
35
+ worker: Worker;
36
+ }
37
+
38
+ export interface IWorkerMessage {
39
+ cmd: string;
40
+ data?: unknown;
41
+ }
42
+
43
+ export interface IProjectParallelRunnerOptions extends IProjectRunnerOptions {
44
+ }
45
+
46
+ export interface IProjectParallelWorkerOptions extends IProjectRunnerOptions {
47
+ project: IHttpProject;
48
+ }
49
+
50
+ export interface ProjectParallelRunner {
51
+ /**
52
+ * Dispatched when a status of a worker change.
53
+ * This can be used to render the current status.
54
+ */
55
+ on(event: 'status', listener: (info: IWorkerInfo[]) => void): this;
56
+ /**
57
+ * Dispatched when a status of a worker change.
58
+ * This can be used to render the current status.
59
+ */
60
+ once(event: 'status', listener: (info: IWorkerInfo[]) => void): this;
61
+ }
62
+
63
+ /**
64
+ * Runs a project in parallel.
65
+ * It creates a number of workers determined by the number of CPUs available on the current machine
66
+ * and the number of iterations defined in the configuration options.
67
+ *
68
+ * When the number of iterations is greater then the number of CPUs then
69
+ * the program distributes the remaining iterations among created workers.
70
+ *
71
+ * The program dispatched the `status` event. It is dispatched each time when the worker status
72
+ * change. This event can be user to refresh the UI to reflect the newest state.
73
+ */
74
+ export class ProjectParallelRunner extends BaseRunner {
75
+ project: HttpProject;
76
+ options: IProjectParallelRunnerOptions;
77
+ workers: WorkerInfoInternal[] = [];
78
+ private mainResolver?: (report: IProjectExecutionLog) => void;
79
+ private mainRejecter?: (err: Error) => void;
80
+
81
+ constructor(project: HttpProject, opts: IProjectParallelRunnerOptions = {}) {
82
+ super();
83
+ this.project = project;
84
+ this.options = opts || {};
85
+
86
+ this._exitHandler = this._exitHandler.bind(this);
87
+ }
88
+
89
+ execute(): Promise<IProjectExecutionLog> {
90
+ return new Promise((resolve, reject) => {
91
+ this.mainResolver = resolve;
92
+ this.mainRejecter = reject;
93
+ this._execute();
94
+ });
95
+ }
96
+
97
+ private _execute(): void {
98
+ try {
99
+ cluster.setupPrimary({
100
+ exec: join(__dirname, 'ProjectRunnerWorker.js'),
101
+ silent: true,
102
+ });
103
+ const { iterations = 1 } = this.options;
104
+ const poolSize = Math.min(iterations, numCPUs);
105
+ for (let i = 0; i < poolSize; i++) {
106
+ const worker = cluster.fork();
107
+ this.setupWorker(worker);
108
+ }
109
+ this.distributeIterations();
110
+ this.emit('status', this.getStatusWorkers());
111
+ cluster.on('exit', this._exitHandler);
112
+ } catch (e) {
113
+ const cause = e as Error;
114
+ this.mainRejecter!(cause);
115
+ }
116
+ }
117
+
118
+ private getStatusWorkers(): IWorkerInfo[] {
119
+ const { workers } = this;
120
+ const result: IWorkerInfo[] = [];
121
+ workers.forEach((info) => {
122
+ const cp = { ...info } as any;
123
+ delete cp.worker;
124
+ result.push(cp);
125
+ });
126
+ return result;
127
+ }
128
+
129
+ private distributeIterations(): void {
130
+ const workers = this.workers.length;
131
+ const { iterations = 1 } = this.options;
132
+ let iterationsRemaining = iterations - workers;
133
+ let currentIndex = 0;
134
+ while (iterationsRemaining > 0) {
135
+ this.workers[currentIndex].iterations += 1;
136
+ iterationsRemaining--;
137
+ currentIndex++;
138
+ if (currentIndex + 1 === workers) {
139
+ currentIndex = 0;
140
+ }
141
+ }
142
+ }
143
+
144
+ private setupWorker(worker: Worker): void {
145
+ this.workers.push({
146
+ worker,
147
+ online: false,
148
+ iterations: 1,
149
+ status: 'initializing',
150
+ });
151
+ worker.on('message', this._messageHandler.bind(this, worker));
152
+ }
153
+
154
+ private _messageHandler(worker: Worker, message: IWorkerMessage): void {
155
+ switch (message.cmd) {
156
+ case 'online': this.setOnline(worker); break;
157
+ case 'result': this.setRunResult(worker, message); break;
158
+ case 'error': this.setRunError(worker, message); break;
159
+ }
160
+ }
161
+
162
+ private _exitHandler(worker: Worker): void {
163
+ const info = this.workers.find(i => i.worker === worker);
164
+ if (!info) {
165
+ return;
166
+ }
167
+ this.finishWhenReady();
168
+ }
169
+
170
+ private setOnline(worker: Worker): void {
171
+ const info = this.workers.find(i => i.worker === worker);
172
+ if (!info) {
173
+ return;
174
+ }
175
+ info.online = true;
176
+ info.status = 'ready';
177
+ this.runWhenReady();
178
+ this.emit('status', this.getStatusWorkers());
179
+ }
180
+
181
+ private setRunResult(worker: Worker, message: IWorkerMessage): void {
182
+ const reports = message.data as IProjectExecutionIteration[];
183
+ this.executed = this.executed.concat(reports);
184
+ worker.destroy();
185
+ const info = this.workers.find(i => i.worker === worker);
186
+ if (!info) {
187
+ return;
188
+ }
189
+ info.status = 'finished';
190
+ this.emit('status', this.getStatusWorkers());
191
+ }
192
+
193
+ private runWhenReady(): void {
194
+ const waiting = this.workers.some(i => !i.online);
195
+ if (waiting) {
196
+ return;
197
+ }
198
+ this.startTime = Date.now();
199
+ this.workers.forEach((info) => {
200
+ const opts: IProjectParallelWorkerOptions = { ...this.options, project: this.project.toJSON() };
201
+ opts.iterations = info.iterations;
202
+ info.status = 'running';
203
+ info.worker.send({
204
+ cmd: 'run',
205
+ data: opts,
206
+ });
207
+ });
208
+ }
209
+
210
+ private async finishWhenReady(): Promise<void> {
211
+ if (this.endTime) {
212
+ return;
213
+ }
214
+ const working = this.workers.some(i => !['finished', 'error'].includes(i.status));
215
+ if (working || !this.mainResolver) {
216
+ return;
217
+ }
218
+ this.endTime = Date.now();
219
+ const report = await this.createReport();
220
+ this.mainResolver(report);
221
+ cluster.off('exit', this._exitHandler);
222
+ }
223
+
224
+ private setRunError(worker: Worker, message: IWorkerMessage): void {
225
+ worker.destroy();
226
+ const info = this.workers.find(i => i.worker === worker);
227
+ if (!info) {
228
+ return;
229
+ }
230
+ info.status = 'error';
231
+ info.message = message.data as string;
232
+ this.emit('status', this.getStatusWorkers());
233
+ }
234
+ }
@@ -0,0 +1,281 @@
1
+ import { EventEmitter } from 'events';
2
+ import { Environment } from '../../models/Environment.js';
3
+ import { Logger } from '../../lib/logging/Logger.js';
4
+ import { IRequestLog, RequestLog } from '../../models/RequestLog.js';
5
+ import { Property } from '../../models/Property.js';
6
+ import { ProjectFolder, Kind as ProjectFolderKind } from '../../models/ProjectFolder.js';
7
+ import { ProjectRequest } from '../../models/ProjectRequest.js';
8
+ import { IHttpRequest } from '../../models/HttpRequest.js';
9
+ import { HttpProject, IProjectRequestIterator } from '../../models/HttpProject.js';
10
+ import { SentRequest } from '../../models/SentRequest.js';
11
+ import { ErrorResponse } from '../../models/ErrorResponse.js';
12
+ import { VariablesStore } from './VariablesStore.js';
13
+ import { VariablesProcessor } from '../variables/VariablesProcessor.js';
14
+ import { RequestFactory } from './RequestFactory.js';
15
+ import { EventTypes } from '../../events/EventTypes.js';
16
+
17
+ export interface ProjectRunnerOptions {
18
+ /**
19
+ * When provided it overrides any project / folder defined environment.
20
+ */
21
+ environment?: Environment;
22
+ /**
23
+ * Additional variables to pass to the selected environment.
24
+ * This can be use to pass system variables, when needed.
25
+ *
26
+ * To use system variables tou can use `init.variables = process.env`;
27
+ */
28
+ variables?: Record<string, string>;
29
+ /**
30
+ * Overrides the default logger (console).
31
+ */
32
+ logger?: Logger;
33
+ /**
34
+ * The event target to use.
35
+ * By default it creates its own target.
36
+ */
37
+ eventTarget?: EventTarget;
38
+ }
39
+
40
+ export interface ProjectRunnerRunOptions extends IProjectRequestIterator {
41
+ }
42
+
43
+ export interface RunResult {
44
+ /**
45
+ * The key of the request from the HttpProject that was executed.
46
+ */
47
+ key: string;
48
+ /**
49
+ * The key of parent folder of the executed request.
50
+ */
51
+ parent?: string;
52
+ /**
53
+ * Set when a fatal error occurred so the request couldn't be executed.
54
+ * This is not the same as error reported during a request. The log's response can still be IResponseError.
55
+ */
56
+ error?: boolean;
57
+ /**
58
+ * The error message. Always set when the `error` is `true`.
59
+ */
60
+ errorMessage?: string;
61
+ /**
62
+ * The request log.
63
+ * Always set when the `error` is `false`.
64
+ */
65
+ log?: IRequestLog;
66
+ }
67
+
68
+ export interface ProjectRequestRunner {
69
+ /**
70
+ * The request object is prepared and about to be sent to the HTTP engine
71
+ */
72
+ on(event: 'request', listener: (key: string, request: IHttpRequest) => void): this;
73
+ /**
74
+ * The response is ready.
75
+ */
76
+ on(event: 'response', listener: (key: string, log: IRequestLog) => void): this;
77
+ /**
78
+ * There was a general error during the request
79
+ */
80
+ on(event: 'error', listener: (key: string, log: IRequestLog, message: string) => void): this;
81
+ /**
82
+ * The request object is prepared and about to be sent to the HTTP engine
83
+ */
84
+ once(event: 'request', listener: (key: string, request: IHttpRequest) => void): this;
85
+ /**
86
+ * The response is ready.
87
+ */
88
+ once(event: 'response', listener: (key: string, log: IRequestLog) => void): this;
89
+ /**
90
+ * There was a general error during the request
91
+ */
92
+ once(event: 'error', listener: (key: string, log: IRequestLog, message: string) => void): this;
93
+ }
94
+
95
+ /**
96
+ * Runs requests in a project.
97
+ * Developers can run the entire project with the `recursive` flag set. They can also
98
+ * set the starting point with the `parent` options.
99
+ *
100
+ * Requests are executed in order defined in the folder.
101
+ */
102
+ export class ProjectRequestRunner extends EventEmitter {
103
+ eventTarget: EventTarget;
104
+ logger?: Logger;
105
+ project: HttpProject;
106
+
107
+ protected masterEnvironment?: Environment;
108
+ protected extraVariables?: Record<string, string>;
109
+
110
+ /**
111
+ * The variables processor instance.
112
+ */
113
+ protected variablesProcessor = new VariablesProcessor();
114
+
115
+ constructor(project: HttpProject, opts: ProjectRunnerOptions = {}) {
116
+ super();
117
+ this.project = project;
118
+ this.logger = opts.logger;
119
+ this.eventTarget = opts.eventTarget || new EventTarget();
120
+ this.masterEnvironment = opts.environment;
121
+ this.extraVariables = opts.variables;
122
+ }
123
+
124
+ /**
125
+ * Runs the request from the project root or a specified folder.
126
+ * @param options Run options.
127
+ * @returns A promise with the run result.
128
+ */
129
+ async run(options?: ProjectRunnerRunOptions): Promise<RunResult[]> {
130
+ const { project } = this;
131
+ const executed: RunResult[] = [];
132
+ for (const request of project.requestIterator(options)) {
133
+ const parent = request.getParent() || project;
134
+ let variables: Record<string, string>;
135
+ if (VariablesStore.has(parent)) {
136
+ variables = VariablesStore.get(parent);
137
+ } else {
138
+ variables = await this.getVariables(parent);
139
+ VariablesStore.set(parent, variables);
140
+ }
141
+ const info = await this.execute(request, variables);
142
+ executed.push(info);
143
+ }
144
+ return executed;
145
+ }
146
+
147
+ protected async execute(request: ProjectRequest, variables: Record<string, string>): Promise<RunResult> {
148
+ const config = request.getConfig();
149
+ const factory = new RequestFactory(this.eventTarget);
150
+
151
+ factory.variables = variables;
152
+ if (request.authorization) {
153
+ factory.authorization = request.authorization.map(i => i.toJSON());
154
+ }
155
+ if (request.actions) {
156
+ factory.actions = request.actions.toJSON();
157
+ }
158
+ if (request.clientCertificate) {
159
+ factory.certificates = [request.clientCertificate];
160
+ }
161
+ if (config.enabled !== false) {
162
+ factory.config = config.toJSON();
163
+ }
164
+ if (this.logger) {
165
+ factory.logger = this.logger;
166
+ }
167
+ const info: RunResult = {
168
+ key: request.key,
169
+ };
170
+ const requestData = request.expects.toJSON();
171
+ requestData.url = this.prepareRequestUrl(requestData.url, variables);
172
+
173
+ function variableHandler(e: CustomEvent): void {
174
+ if (e.defaultPrevented) {
175
+ return;
176
+ }
177
+ const { name, value } = e.detail;
178
+ variables[name] = value;
179
+ e.preventDefault();
180
+ e.detail.result = Promise.resolve();
181
+ }
182
+
183
+ this.eventTarget.addEventListener(EventTypes.Environment.set, variableHandler as any);
184
+
185
+ try {
186
+ // Below replaces the single call to the `run()` function of the factory to
187
+ // report via the events a request object that has evaluated with the Jexl library.
188
+ const requestCopy = await factory.processRequestVariables(requestData);
189
+ this.emit('request', request.key, { ...requestCopy });
190
+ await factory.processRequestLogic(requestCopy);
191
+ const result = await factory.executeRequest(requestCopy);
192
+ await factory.processResponse(result);
193
+ request.setLog(result);
194
+ info.log = result;
195
+ this.emit('response', request.key, { ...result });
196
+ } catch (e) {
197
+ const cause = e as Error;
198
+ info.error = true;
199
+ info.errorMessage = cause.message;
200
+ const sent = new SentRequest({ ...requestData, startTime: 0, endTime: 0, });
201
+ const response = ErrorResponse.fromError(info.errorMessage);
202
+ const log = RequestLog.fromRequestResponse(sent.toJSON(), response.toJSON()).toJSON();
203
+ this.emit('error', request.key, log, info.errorMessage);
204
+ }
205
+
206
+ this.eventTarget.removeEventListener(EventTypes.Environment.set, variableHandler as any);
207
+ return info;
208
+ }
209
+
210
+ protected async getVariables(parent: HttpProject | ProjectFolder): Promise<Record<string, string>> {
211
+ if (this.masterEnvironment) {
212
+ return this.applyVariables([this.masterEnvironment]);
213
+ }
214
+ return this.createEnvironment(parent);
215
+ }
216
+
217
+ protected async createEnvironment(parent: HttpProject | ProjectFolder): Promise<Record<string, string>> {
218
+ const envs = await this.readEnvironments(parent);
219
+ return this.applyVariables(envs);
220
+ }
221
+
222
+ /**
223
+ * Reads the list of the environments to apply to this runtime.
224
+ */
225
+ protected async readEnvironments(parent: HttpProject | ProjectFolder): Promise<Environment[]> {
226
+ const folderKey = parent.kind === ProjectFolderKind ? (parent as ProjectFolder).key : undefined;
227
+ return this.project.readEnvironments({ folderKey });
228
+ }
229
+
230
+ /**
231
+ * Reads the variables and the base URI from the passed environments.
232
+ */
233
+ protected async applyVariables(environments: Environment[]): Promise<Record<string, string>> {
234
+ let baseUri = '';
235
+ const variables: Property[] = [];
236
+ environments.forEach((environment) => {
237
+ const { server, variables: envVariables } = environment;
238
+ if (server) {
239
+ baseUri = server.readUri();
240
+ }
241
+ if (envVariables.length) {
242
+ envVariables.forEach((item) => {
243
+ const defined = variables.findIndex(i => i.name === item.name);
244
+ if (defined >= 0) {
245
+ variables[defined] = item;
246
+ } else {
247
+ variables.push(item);
248
+ }
249
+ });
250
+ }
251
+ });
252
+ const { extraVariables } = this;
253
+ const ctx = VariablesProcessor.createContextFromProperties(variables);
254
+ if (extraVariables) {
255
+ Object.keys(extraVariables).forEach((key) => {
256
+ ctx[key] = extraVariables[key];
257
+ });
258
+ }
259
+ // the `baseUri` is reserved and always set to the environment's `baseUri`.
260
+ ctx.baseUri = baseUri || '';
261
+ return this.variablesProcessor.buildContext(ctx);
262
+ }
263
+
264
+ /**
265
+ * When defined it applies the serve's base URI to relative URLs.
266
+ * @param currentUrl The URL to process.
267
+ */
268
+ protected prepareRequestUrl(currentUrl: string, variables: Record<string, string>): string {
269
+ const { baseUri } = variables;
270
+ if (!baseUri) {
271
+ return currentUrl;
272
+ }
273
+ if (currentUrl.startsWith('http:') || currentUrl.startsWith('https:')) {
274
+ return currentUrl;
275
+ }
276
+ if (currentUrl.startsWith('/')) {
277
+ return `${baseUri}${currentUrl}`;
278
+ }
279
+ return `${baseUri}/${currentUrl}`;
280
+ }
281
+ }