@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,374 @@
1
+ import { SerializableError } from '../../models/SerializableError.js';
2
+ import { HttpProject } from '../../models/HttpProject.js';
3
+ import { ProjectFolder } from '../../models/ProjectFolder.js';
4
+ import { Environment, IEnvironment } from '../../models/Environment.js';
5
+ import { DummyLogger } from '../../lib/logging/DummyLogger.js';
6
+ import { Logger } from '../../lib/logging/Logger.js';
7
+ import { IRequestLog } from '../../models/RequestLog.js';
8
+ import { IHttpRequest } from '../../models/HttpRequest.js';
9
+ import { ProjectRequestRunner } from './ProjectRequestRunner.js';
10
+ import { IProjectExecutionIteration, IProjectExecutionLog } from '../reporters/Reporter.js';
11
+ import { pathExists, readJson } from '../../lib/fs/Fs.js';
12
+ import { BaseRunner } from './BaseRunner.js';
13
+
14
+ type ProjectParent = HttpProject | ProjectFolder;
15
+
16
+ export interface IProjectRunnerOptions {
17
+ /**
18
+ * The environment to use.
19
+ * This can be a name or the key of the environment located under the parent or root.
20
+ * It can also be a path to the environment definition. If the file exists it is used. Otherwise it tried to
21
+ * find the environment in the project.
22
+ */
23
+ environment?: string;
24
+ /**
25
+ * The parent folder to execute.
26
+ */
27
+ parent?: string;
28
+ /**
29
+ * The names or the keys of requests to execute.
30
+ * This can be used to limit the number of requests.
31
+ */
32
+ request?: string[];
33
+ /**
34
+ * The number of times the execution should be repeated.
35
+ * Default to 1.
36
+ */
37
+ iterations?: number;
38
+ /**
39
+ * The number of milliseconds to wait between each iteration.
40
+ * Default to the next frame (vary from 1 to tens of milliseconds).
41
+ */
42
+ iterationDelay?: number;
43
+ /**
44
+ * When set it performs parallel execution for each iteration.
45
+ * The number of executions at the same time depends on the number of processor cores
46
+ * available on the current machine. The maximum of the parallel execution
47
+ * is the number of available cores. When the `iterations` number is higher
48
+ * then the "rest" is added to the iterations per each core.
49
+ */
50
+ parallel?: boolean;
51
+ /**
52
+ * When set it includes requests in the current folder and sub-folder according to the order
53
+ * defined in the folder.
54
+ */
55
+ recursive?: boolean;
56
+ /**
57
+ * The opposite of the `requests`. The list of names or keys of requests or folders to ignore.
58
+ * Note, ignore is tested before the `requests`.
59
+ */
60
+ ignore?: string[];
61
+ /**
62
+ * The logger to use with the request factory.
63
+ * When not set it uses the dummy logger (no output).
64
+ */
65
+ logger?: Logger;
66
+ /**
67
+ * When true it copies all system variables to the execution environment.
68
+ * When an array of strings, only takes system variables that are listed in the array.
69
+ * When a map, it uses this map as a list of variables.
70
+ * When not set it does not read system variables.
71
+ */
72
+ variables?: boolean | string[] | Record<string, string>;
73
+ }
74
+
75
+ export interface ProjectRunner {
76
+ /**
77
+ * Event dispatched when an iteration is about to start.
78
+ */
79
+ on(event: 'before-iteration', listener: (index: number, iterated: boolean) => void): this;
80
+ /**
81
+ * Event dispatched when an iteration finished.
82
+ */
83
+ on(event: 'after-iteration', listener: (index: number, iterated: boolean) => void): this;
84
+ /**
85
+ * Event dispatched before the iteration is going to sleep for the set period of time.
86
+ */
87
+ on(event: 'before-sleep', listener: () => void): this;
88
+ /**
89
+ * Event dispatched after the iteration is woke up.
90
+ */
91
+ on(event: 'after-sleep', listener: () => void): this;
92
+ /**
93
+ * The request object is prepared and about to be sent to the HTTP engine
94
+ */
95
+ on(event: 'request', listener: (key: string, request: IHttpRequest) => void): this;
96
+ /**
97
+ * The response is ready.
98
+ */
99
+ on(event: 'response', listener: (key: string, log: IRequestLog) => void): this;
100
+ /**
101
+ * There was a general error during the request
102
+ */
103
+ on(event: 'error', listener: (key: string, log: IRequestLog, message: string) => void): this;
104
+ /**
105
+ * Event dispatched when an iteration is about to start.
106
+ */
107
+ once(event: 'before-iteration', listener: (index: number, iterated: boolean) => void): this;
108
+ /**
109
+ * Event dispatched when an iteration finished.
110
+ */
111
+ once(event: 'after-iteration', listener: (index: number, iterated: boolean) => void): this;
112
+ /**
113
+ * Event dispatched before the iteration is going to sleep for the set period of time.
114
+ */
115
+ once(event: 'before-sleep', listener: () => void): this;
116
+ /**
117
+ * Event dispatched after the iteration is woke up.
118
+ */
119
+ once(event: 'after-sleep', listener: () => void): this;
120
+ /**
121
+ * The request object is prepared and about to be sent to the HTTP engine
122
+ */
123
+ once(event: 'request', listener: (key: string, request: IHttpRequest) => void): this;
124
+ /**
125
+ * The response is ready.
126
+ */
127
+ once(event: 'response', listener: (key: string, log: IRequestLog) => void): this;
128
+ /**
129
+ * There was a general error during the request
130
+ */
131
+ once(event: 'error', listener: (key: string, log: IRequestLog, message: string) => void): this;
132
+ }
133
+
134
+ /**
135
+ * A class to be extended to run an entire API Project for the given configuration options.
136
+ *
137
+ * The main purpose of this class (and its children) is to iterate over requests
138
+ * in the project and execute them one-by-one.
139
+ *
140
+ * Implementations allow to execute the requests in a serial model or in parallel mode, where
141
+ * a separate workers are created to run the same HTTP requests in each worker.
142
+ *
143
+ * This class generates a run report which other programs can use to build all kinds of UIs
144
+ * around the data collected during the run.
145
+ */
146
+ export abstract class ProjectRunner extends BaseRunner {
147
+ /**
148
+ * The HTTP project to run requests from.
149
+ */
150
+ project?: HttpProject;
151
+ /**
152
+ * The execution options for the project.
153
+ */
154
+ options?: IProjectRunnerOptions;
155
+ /**
156
+ * The root object (project or a folder) where the program starts iterating over the requests.
157
+ */
158
+ root?: ProjectParent;
159
+ /**
160
+ * The selected environment to apply to the requests.
161
+ */
162
+ environment?: Environment;
163
+ /**
164
+ * The events target instance for events dispatched by the request factory.
165
+ */
166
+ target = new EventTarget();
167
+ /**
168
+ * This is used with `--iterations`. The index of the current iteration.
169
+ */
170
+ protected index = 0;
171
+ /**
172
+ * The currently executed iteration loop.
173
+ */
174
+ protected currentIteration?: IProjectExecutionIteration;
175
+ /**
176
+ * The number of remaining iterations to run.
177
+ */
178
+ protected remaining = 1;
179
+ /**
180
+ * Whether the configuration allows iterations (the parallel mode).
181
+ */
182
+ protected hasIterations = false;
183
+ /**
184
+ * When set it won't amit any event.
185
+ * This can be used in a background worker when events are never handled.
186
+ */
187
+ noEmit = false;
188
+
189
+ constructor() {
190
+ super();
191
+ this._requestHandler = this._requestHandler.bind(this);
192
+ this._responseHandler = this._responseHandler.bind(this);
193
+ this._errorHandler = this._errorHandler.bind(this);
194
+ }
195
+
196
+ /**
197
+ * A required step before running the project.
198
+ * It configures the execution context. It may throw an error when configuration is not valid.
199
+ */
200
+ async configure(project: HttpProject, opts: IProjectRunnerOptions = {}): Promise<void> {
201
+ this.project = project;
202
+ this.options = opts || {};
203
+ if (typeof this.options.iterations === 'number' && this.options.iterations >= 0) {
204
+ this.remaining = this.options.iterations;
205
+ }
206
+ this.hasIterations = this.remaining > 1;
207
+
208
+ const root = opts.parent ? project.findFolder(opts.parent) : project;
209
+ if (!root) {
210
+ throw new Error(`Unable to locate the folder: ${opts.parent}`);
211
+ }
212
+ this.root = root;
213
+ this.environment = await this.getEnvironment();
214
+ }
215
+
216
+ /**
217
+ * Executes the requests in the project.
218
+ * @returns The execution log created by calling the `createReport()` function.
219
+ */
220
+ abstract execute(): Promise<IProjectExecutionLog>;
221
+
222
+ /**
223
+ * Creates the report of the execution.
224
+ */
225
+ protected async createReport(): Promise<IProjectExecutionLog> {
226
+ const log: IProjectExecutionLog = {
227
+ started: this.startTime as number,
228
+ ended: this.endTime as number,
229
+ iterations: this.executed,
230
+ };
231
+ return log;
232
+ }
233
+
234
+ /**
235
+ * Reads the environment data to use with the execution.
236
+ * If the configured environment is a location of a file
237
+ * it is read as API Client's environment and used in the execution.
238
+ * Otherwise it searches for the environment in the list of the defined
239
+ * environments by the name of the key.
240
+ *
241
+ * It throws when the environment cannot be found or when the file contents is invalid.
242
+ */
243
+ protected async getEnvironment(): Promise<Environment | undefined> {
244
+ const { options } = this;
245
+ if (!options) {
246
+ throw new Error(`Run configure() first.`);
247
+ }
248
+ if (!options.environment) {
249
+ return;
250
+ }
251
+ const fileExists = await pathExists(options.environment);
252
+ if (fileExists) {
253
+ const contents = await readJson(options.environment);
254
+ return new Environment(contents as IEnvironment);
255
+ }
256
+ const root = this.root as ProjectParent;
257
+ const envs = root.getEnvironments();
258
+ const env = envs.find(i => i.key === options.environment || i.info.name === options.environment);
259
+ if (!env) {
260
+ throw new SerializableError(`The environment cannot be found: ${options.environment}.`, 'EENVNOTFOUND');
261
+ }
262
+ return env;
263
+ }
264
+
265
+ /**
266
+ * Runs the requests from the project as configured.
267
+ */
268
+ protected async executeIteration(): Promise<void> {
269
+ const { environment, project, options, hasIterations, index, noEmit } = this;
270
+ if (!options || !project) {
271
+ throw new Error(`Run configure() first.`);
272
+ }
273
+ if (!noEmit) {
274
+ this.emit('before-iteration', index, hasIterations);
275
+ }
276
+
277
+ const runner = new ProjectRequestRunner(project, {
278
+ environment,
279
+ logger: options.logger ? options.logger : new DummyLogger(),
280
+ eventTarget: this.target,
281
+ variables: this.getSystemVariables(),
282
+ });
283
+
284
+ runner.on('request', this._requestHandler);
285
+ runner.on('response', this._responseHandler);
286
+ runner.on('error', this._errorHandler);
287
+ this.currentIteration = {
288
+ index: this.index,
289
+ executed: [],
290
+ };
291
+ try {
292
+ await runner.run({
293
+ parent: options.parent,
294
+ requests: options.request,
295
+ ignore: options.ignore,
296
+ recursive: options.recursive
297
+ });
298
+ } catch (e) {
299
+ const cause = e as Error;
300
+ console.error(e);
301
+ this.currentIteration.error = cause.message || 'Unknown error ocurred';
302
+ // ...
303
+ }
304
+ this.executed.push(this.currentIteration);
305
+ if (!noEmit) {
306
+ this.emit('after-iteration', index, hasIterations);
307
+ }
308
+ }
309
+
310
+ /**
311
+ * Retargets the "request" event from the factory.
312
+ */
313
+ protected _requestHandler(key: string, request: IHttpRequest): void {
314
+ if (!this.noEmit) {
315
+ this.emit('request', key, request);
316
+ }
317
+ }
318
+
319
+ /**
320
+ * Retargets the "response" event from the factory.
321
+ */
322
+ protected _responseHandler(key: string, log: IRequestLog): void {
323
+ this.currentIteration?.executed.push(log);
324
+ if (!this.noEmit) {
325
+ this.emit('response', key, log);
326
+ }
327
+ }
328
+
329
+ /**
330
+ * Retargets the "error" event from the factory.
331
+ */
332
+ protected _errorHandler(key: string, log: IRequestLog, message: string): void {
333
+ this.currentIteration?.executed.push(log);
334
+ if (!this.noEmit) {
335
+ this.emit('error', key, log, message);
336
+ }
337
+ }
338
+
339
+ /**
340
+ * @returns Reads the system variables based on the library configuration.
341
+ */
342
+ protected getSystemVariables(): Record<string, string> {
343
+ const result: Record<string, string> = {};
344
+ const { options } = this;
345
+ if (!options) {
346
+ return result;
347
+ }
348
+ const { variables } = options;
349
+ if (typeof variables === 'undefined') {
350
+ return result;
351
+ }
352
+ if (typeof variables === 'boolean') {
353
+ return variables ? this._readSystemVariables() : result;
354
+ }
355
+ if (Array.isArray(variables)) {
356
+ return this._readSystemVariables(variables);
357
+ }
358
+ return variables;
359
+ }
360
+
361
+ private _readSystemVariables(names?: string[]): Record<string, string> {
362
+ const result: Record<string, string> = {};
363
+ Object.keys(process.env).forEach((key) => {
364
+ if (names && !names.includes(key)) {
365
+ return;
366
+ }
367
+ const value = process.env[key];
368
+ if (value) {
369
+ result[key] = value;
370
+ }
371
+ });
372
+ return result;
373
+ }
374
+ }
@@ -0,0 +1,62 @@
1
+ /* eslint-disable no-unused-vars */
2
+ import process from 'process';
3
+ import cluster from 'cluster';
4
+ import { HttpProject } from '../../models/HttpProject.js';
5
+ import { IProjectExecutionLog } from '../reporters/Reporter.js';
6
+ import { IWorkerMessage, IProjectParallelWorkerOptions } from './ProjectParallelRunner.js';
7
+ import { sleep } from '../../lib/timers/Timers.js';
8
+ import { ProjectRunner } from './ProjectRunner.js';
9
+
10
+ class ProjectExeWorker extends ProjectRunner {
11
+ initialize(): void {
12
+ if (cluster.isPrimary) {
13
+ throw new Error(`This file should not be called directly.`);
14
+ }
15
+ process.send!({ cmd: 'online' });
16
+ process.on('message', this.messageHandler.bind(this));
17
+ }
18
+
19
+ messageHandler(message: IWorkerMessage): void {
20
+ switch (message.cmd) {
21
+ case 'run': this.run(message.data as IProjectParallelWorkerOptions); break;
22
+ }
23
+ }
24
+
25
+ async run(options: IProjectParallelWorkerOptions): Promise<void> {
26
+ try {
27
+ await this.configure(new HttpProject(options.project), options);
28
+ await this.execute();
29
+ } catch (e) {
30
+ const cause = e as Error;
31
+ process.send!({ cmd: 'error', data: cause.message });
32
+ }
33
+ }
34
+
35
+ async execute(): Promise<IProjectExecutionLog> {
36
+ const { root } = this;
37
+ if (!root) {
38
+ throw new Error(`The project runner is not configured.`);
39
+ }
40
+ function unhandledRejection(): void {}
41
+ process.on('unhandledRejection', unhandledRejection);
42
+ this.startTime = Date.now();
43
+ while (this.remaining > 0) {
44
+ this.remaining--;
45
+ await this.executeIteration();
46
+ this.index++;
47
+ if (this.remaining && this.options?.iterationDelay) {
48
+ await sleep(this.options.iterationDelay);
49
+ }
50
+ }
51
+ process.off('unhandledRejection', unhandledRejection);
52
+ this.endTime = Date.now();
53
+
54
+ const log = await this.createReport();
55
+ process.send!({ cmd: 'result', data: log.iterations });
56
+ return log;
57
+ }
58
+ }
59
+
60
+ const instance = new ProjectExeWorker();
61
+ instance.noEmit = true;
62
+ instance.initialize();
@@ -0,0 +1,36 @@
1
+ import { SerializableError } from '../../models/SerializableError.js';
2
+ import { sleep } from '../../lib/timers/Timers.js';
3
+ import { ProjectRunner } from './ProjectRunner.js';
4
+ import { IProjectExecutionLog } from '../reporters/Reporter.js';
5
+
6
+ /**
7
+ * Project runner that runs the requests in the project one-by-one.
8
+ */
9
+ export class ProjectSerialRunner extends ProjectRunner {
10
+ /**
11
+ * Executes the requests in the project.
12
+ */
13
+ async execute(): Promise<IProjectExecutionLog> {
14
+ const { root } = this;
15
+ if (!root) {
16
+ throw new SerializableError(`The project runner is not configured.`, 'ECONFIGURE');
17
+ }
18
+
19
+ this.startTime = Date.now();
20
+ while (this.remaining > 0) {
21
+ this.remaining--;
22
+ await this.executeIteration();
23
+ this.index++;
24
+ if (this.remaining && this.options?.iterationDelay) {
25
+ this.emit('before-sleep');
26
+ await sleep(this.options.iterationDelay);
27
+ this.emit('after-sleep');
28
+ } else {
29
+ await sleep(0);
30
+ }
31
+ }
32
+
33
+ this.endTime = Date.now();
34
+ return this.createReport();
35
+ }
36
+ }