@api-client/core 0.3.5 → 0.3.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (84) hide show
  1. package/build/browser.d.ts +5 -0
  2. package/build/browser.js +14 -0
  3. package/build/browser.js.map +1 -1
  4. package/build/index.d.ts +13 -1
  5. package/build/index.js +25 -1
  6. package/build/index.js.map +1 -1
  7. package/build/src/lib/calculators/DataCalculator.d.ts +27 -0
  8. package/build/src/lib/calculators/DataCalculator.js +88 -0
  9. package/build/src/lib/calculators/DataCalculator.js.map +1 -0
  10. package/build/src/lib/fs/Fs.d.ts +52 -0
  11. package/build/src/lib/fs/Fs.js +245 -0
  12. package/build/src/lib/fs/Fs.js.map +1 -0
  13. package/build/src/lib/parsers/UrlEncoder.d.ts +51 -0
  14. package/build/src/lib/parsers/UrlEncoder.js +74 -0
  15. package/build/src/lib/parsers/UrlEncoder.js.map +1 -0
  16. package/build/src/lib/parsers/UrlParser.d.ts +104 -0
  17. package/build/src/lib/parsers/UrlParser.js +189 -0
  18. package/build/src/lib/parsers/UrlParser.js.map +1 -0
  19. package/build/src/lib/parsers/UrlValueParser.d.ts +92 -0
  20. package/build/src/lib/parsers/UrlValueParser.js +172 -0
  21. package/build/src/lib/parsers/UrlValueParser.js.map +1 -0
  22. package/build/src/lib/timers/Timers.d.ts +5 -0
  23. package/build/src/lib/timers/Timers.js +10 -0
  24. package/build/src/lib/timers/Timers.js.map +1 -0
  25. package/build/src/mocking/ProjectMock.d.ts +13 -0
  26. package/build/src/mocking/ProjectMock.js +16 -0
  27. package/build/src/mocking/ProjectMock.js.map +1 -0
  28. package/build/src/mocking/lib/Request.d.ts +32 -0
  29. package/build/src/mocking/lib/Request.js +63 -0
  30. package/build/src/mocking/lib/Request.js.map +1 -0
  31. package/build/src/mocking/lib/Response.d.ts +33 -0
  32. package/build/src/mocking/lib/Response.js +79 -0
  33. package/build/src/mocking/lib/Response.js.map +1 -0
  34. package/build/src/runtime/node/BaseRunner.d.ts +21 -0
  35. package/build/src/runtime/node/BaseRunner.js +27 -0
  36. package/build/src/runtime/node/BaseRunner.js.map +1 -0
  37. package/build/src/runtime/node/ProjectParallelRunner.d.ts +81 -0
  38. package/build/src/runtime/node/ProjectParallelRunner.js +173 -0
  39. package/build/src/runtime/node/ProjectParallelRunner.js.map +1 -0
  40. package/build/src/runtime/node/ProjectRequestRunner.d.ts +125 -0
  41. package/build/src/runtime/node/ProjectRequestRunner.js +185 -0
  42. package/build/src/runtime/node/ProjectRequestRunner.js.map +1 -0
  43. package/build/src/runtime/node/ProjectRunner.d.ts +164 -62
  44. package/build/src/runtime/node/ProjectRunner.js +191 -146
  45. package/build/src/runtime/node/ProjectRunner.js.map +1 -1
  46. package/build/src/runtime/node/ProjectRunnerWorker.d.ts +1 -0
  47. package/build/src/runtime/node/ProjectRunnerWorker.js +58 -0
  48. package/build/src/runtime/node/ProjectRunnerWorker.js.map +1 -0
  49. package/build/src/runtime/node/ProjectSerialRunner.d.ts +11 -0
  50. package/build/src/runtime/node/ProjectSerialRunner.js +34 -0
  51. package/build/src/runtime/node/ProjectSerialRunner.js.map +1 -0
  52. package/build/src/runtime/reporters/ProjectRunCliReporter.d.ts +7 -0
  53. package/build/src/runtime/reporters/ProjectRunCliReporter.js +73 -0
  54. package/build/src/runtime/reporters/ProjectRunCliReporter.js.map +1 -0
  55. package/build/src/runtime/reporters/Reporter.d.ts +62 -0
  56. package/build/src/runtime/reporters/Reporter.js +98 -0
  57. package/build/src/runtime/reporters/Reporter.js.map +1 -0
  58. package/build/src/testing/TestCliHelper.d.ts +29 -0
  59. package/build/src/testing/TestCliHelper.js +80 -0
  60. package/build/src/testing/TestCliHelper.js.map +1 -0
  61. package/build/src/testing/getPort.d.ts +52 -0
  62. package/build/src/testing/getPort.js +169 -0
  63. package/build/src/testing/getPort.js.map +1 -0
  64. package/package.json +3 -2
  65. package/src/lib/calculators/DataCalculator.ts +91 -0
  66. package/src/lib/fs/Fs.ts +258 -0
  67. package/src/lib/parsers/UrlEncoder.ts +74 -0
  68. package/src/lib/parsers/UrlParser.ts +201 -0
  69. package/src/lib/parsers/UrlValueParser.ts +211 -0
  70. package/src/lib/timers/Timers.ts +9 -0
  71. package/src/mocking/LegacyInterfaces.ts +1 -1
  72. package/src/mocking/ProjectMock.ts +20 -0
  73. package/src/mocking/lib/Request.ts +85 -0
  74. package/src/mocking/lib/Response.ts +101 -0
  75. package/src/runtime/node/BaseRunner.ts +29 -0
  76. package/src/runtime/node/ProjectParallelRunner.ts +234 -0
  77. package/src/runtime/node/ProjectRequestRunner.ts +281 -0
  78. package/src/runtime/node/ProjectRunner.ts +279 -186
  79. package/src/runtime/node/ProjectRunnerWorker.ts +62 -0
  80. package/src/runtime/node/ProjectSerialRunner.ts +36 -0
  81. package/src/runtime/reporters/ProjectRunCliReporter.ts +79 -0
  82. package/src/runtime/reporters/Reporter.ts +142 -0
  83. package/src/testing/TestCliHelper.ts +87 -0
  84. package/src/testing/getPort.ts +212 -0
@@ -0,0 +1,79 @@
1
+ import { Table } from 'console-table-printer';
2
+ import chalk from 'chalk';
3
+ import { ErrorResponse, IErrorResponse } from '../../models/ErrorResponse.js';
4
+ import { IArcResponse } from '../../models/ArcResponse.js';
5
+ import { ISerializedError } from '../../models/SerializableError.js';
6
+ import { Reporter } from './Reporter.js';
7
+
8
+ /**
9
+ * HTTP project execution reporter for a terminal output.
10
+ */
11
+ export class ProjectRunCliReporter extends Reporter {
12
+ async generate(): Promise<void> {
13
+ const { info } = this;
14
+
15
+ const table = new Table({
16
+ title: 'Project execution summary',
17
+ columns: [
18
+ { name: 'position', title: ' ', alignment: 'left', },
19
+ { name: 'succeeded', title: 'Succeeded', alignment: 'right', },
20
+ { name: 'failed', title: 'Failed', alignment: 'right', },
21
+ { name: 'total', title: 'Total', alignment: 'right', },
22
+ ],
23
+ });
24
+
25
+ table.addRow({
26
+ position: 'Iterations',
27
+ succeeded: info.iterations.length,
28
+ failed: 0,
29
+ total: info.iterations.length,
30
+ });
31
+
32
+ const failed = this.computeFailed();
33
+ const succeeded = this.computeSucceeded();
34
+ table.addRow({
35
+ position: 'Requests',
36
+ succeeded,
37
+ failed: failed > 0 ? chalk.redBright(failed) : failed,
38
+ total: failed + succeeded,
39
+ });
40
+ table.printTable();
41
+ process.stdout.write('\n');
42
+
43
+ info.iterations.forEach((run, index) => {
44
+ const itNumber = index + 1;
45
+ if (run.error) {
46
+ process.stdout.write(`Iteration ${itNumber} failed: ${run.error}\n\n`);
47
+ return;
48
+ }
49
+ const failed = run.executed.filter(log => this.isFailedLog(log));
50
+ if (!failed.length) {
51
+ return;
52
+ }
53
+ process.stdout.write(`Iteration ${itNumber} Errors\n`);
54
+ failed.forEach((log) => {
55
+ let url = 'Unknown request URL.';
56
+ if (log.request) {
57
+ url = log.request.url;
58
+ }
59
+ const prefix = chalk.dim(`[${url}] `);
60
+ if (log.response && ErrorResponse.isErrorResponse(log.response)) {
61
+ const response = log.response as IErrorResponse;
62
+ let message = (response.error as ISerializedError).message ? (response.error as Error).message : response.error;
63
+ if (typeof message !== 'string') {
64
+ message = 'Unknown error.';
65
+ }
66
+ process.stdout.write(`${prefix}${message}\n`);
67
+ return;
68
+ }
69
+ if (!log.request) {
70
+ process.stdout.write('Request not executed.\n');
71
+ return;
72
+ }
73
+ const response = log.response as IArcResponse;
74
+ process.stdout.write(`${prefix} Status code is: ${response.status}\n`);
75
+ });
76
+ process.stdout.write('\n\n');
77
+ });
78
+ }
79
+ }
@@ -0,0 +1,142 @@
1
+ import { IRequestLog } from '../../models/RequestLog.js';
2
+ import { IArcResponse } from '../../models/ArcResponse.js';
3
+ import { ErrorResponse } from '../../models/ErrorResponse.js';
4
+
5
+ export interface IProjectExecutionIteration {
6
+ /**
7
+ * The index of the iteration.
8
+ */
9
+ index: number;
10
+ /**
11
+ * The list of requests executed in the iteration.
12
+ */
13
+ executed: IRequestLog[];
14
+ /**
15
+ * Optional general error message.
16
+ */
17
+ error?: string;
18
+ }
19
+
20
+ export interface IProjectExecutionLog {
21
+ /**
22
+ * The timestamp when the execution started
23
+ */
24
+ started: number;
25
+ /**
26
+ * The timestamp when the execution ended
27
+ */
28
+ ended: number;
29
+ /**
30
+ * The execution logs for each iteration.
31
+ */
32
+ iterations: IProjectExecutionIteration[];
33
+ }
34
+
35
+ /**
36
+ * Base class for project execution reporters.
37
+ */
38
+ export abstract class Reporter {
39
+ info: IProjectExecutionLog;
40
+
41
+ constructor(info: IProjectExecutionLog) {
42
+ this.info = info;
43
+ }
44
+
45
+ /**
46
+ * Generates the report for the current execution log.
47
+ */
48
+ abstract generate(): Promise<void>;
49
+
50
+ /**
51
+ * Checks whether the execution log should be considered a failure.
52
+ * @param log The execution log.
53
+ * @returns `true` when the request was a failure.
54
+ */
55
+ protected isFailedLog(log: IRequestLog): boolean {
56
+ if (!log.response || ErrorResponse.isErrorResponse(log.response)) {
57
+ return true;
58
+ }
59
+ const response = log.response as IArcResponse;
60
+ if (response.status >= 400) {
61
+ return true;
62
+ }
63
+ return false;
64
+ }
65
+
66
+ /**
67
+ * Computes the number of requests that failed.
68
+ */
69
+ computeFailed(): number {
70
+ let result = 0;
71
+ const { info } = this;
72
+ const { iterations } = info;
73
+ iterations.forEach((iteration) => {
74
+ iteration.executed.forEach((log) => {
75
+ if (this.isFailedLog(log)) {
76
+ result++;
77
+ }
78
+ });
79
+ });
80
+
81
+ return result;
82
+ }
83
+
84
+ /**
85
+ * Computes the number of requests that ended with the status code 399 at the most.
86
+ */
87
+ computeSucceeded(): number {
88
+ let result = 0;
89
+ const { info } = this;
90
+ const { iterations } = info;
91
+ iterations.forEach((iteration) => {
92
+ iteration.executed.forEach((log) => {
93
+ if (!log.response || ErrorResponse.isErrorResponse(log.response)) {
94
+ return;
95
+ }
96
+ const response = log.response as IArcResponse;
97
+ if (response.status < 400) {
98
+ result++;
99
+ }
100
+ });
101
+ });
102
+ return result;
103
+ }
104
+
105
+ /**
106
+ * Computes the total time of sending each request.
107
+ */
108
+ computeTotalTime(): number {
109
+ let result = 0;
110
+ const { info } = this;
111
+ const { iterations } = info;
112
+ iterations.forEach((iteration) => {
113
+ iteration.executed.forEach((log) => {
114
+ if (!log.response || ErrorResponse.isErrorResponse(log.response)) {
115
+ return;
116
+ }
117
+ const response = log.response as IArcResponse;
118
+ if (response.loadingTime && response.loadingTime > 0) {
119
+ result += response.loadingTime;
120
+ }
121
+ });
122
+ });
123
+ return result;
124
+ }
125
+
126
+ /**
127
+ * Computes the total size of received data.
128
+ */
129
+ computeTotalSize(): number {
130
+ let result = 0;
131
+ const { info } = this;
132
+ const { iterations } = info;
133
+ iterations.forEach((iteration) => {
134
+ iteration.executed.forEach((log) => {
135
+ if (log.size && log.size.response) {
136
+ result += log.size.response;
137
+ }
138
+ });
139
+ });
140
+ return result;
141
+ }
142
+ }
@@ -0,0 +1,87 @@
1
+ export interface ITestRunCommandOptions {
2
+ includeError?: boolean;
3
+ noCleaning?: boolean;
4
+ }
5
+
6
+ export class TestCliHelper {
7
+ /**
8
+ * The globally set test timeout.
9
+ * @default 2000 The mocha default test timeout.
10
+ */
11
+ static testTimeout = 2000;
12
+
13
+ static cleanTerminalOutput(s: string): string {
14
+ let result = s.trim();
15
+ result = result.replace(/[^\x20-\x7E\n]/gm, '');
16
+ // result = result.replace(/\[\d+m/gm, '');
17
+ result = result.replace(/\[\d+[a-zA-Z]/gm, '');
18
+ result = result.split('\n').filter(i => !!i.trim()).join('\n');
19
+ return result;
20
+ }
21
+
22
+ static splitLines(table: string): string[] {
23
+ const result: string[] = [];
24
+ table.split('\n').forEach((line) => {
25
+ const value = line.trim();
26
+ if (!value) {
27
+ return;
28
+ }
29
+ result.push(value);
30
+ });
31
+ return result;
32
+ }
33
+
34
+ /**
35
+ * Executes a passed asynchronous function and captures stdout.
36
+ * When the function fails, it cleans up output listeners and throws the error.
37
+ *
38
+ * ```javascript
39
+ * const out = grabOutput(async () => {
40
+ * // ...
41
+ * });
42
+ * console.log(out); // combined stdout and stderr.
43
+ * ```
44
+ *
45
+ * @param fn The function to execute.
46
+ * @param timeout The test timeout. After this time the output is reset.
47
+ * @returns The terminal output.
48
+ */
49
+ static async grabOutput(fn: () => Promise<void>, timeout=TestCliHelper.testTimeout): Promise<string> {
50
+ const messages: string[] = [];
51
+ function noop(): void {
52
+ //
53
+ }
54
+ const origOut = process.stdout.write;
55
+ const origErr = process.stderr.write;
56
+ const origClear = console.clear;
57
+ function messageHandler(buffer: string | Buffer): boolean {
58
+ if (typeof buffer === 'string') {
59
+ messages.push(buffer);
60
+ } else {
61
+ messages.push(buffer.toString('utf8'));
62
+ }
63
+ return true;
64
+ }
65
+ function stop(): void {
66
+ process.stdout.write = origOut;
67
+ process.stderr.write = origErr;
68
+ console.clear = origClear;
69
+ }
70
+ process.stdout.write = messageHandler;
71
+ process.stderr.write = messageHandler;
72
+ console.clear = noop;
73
+
74
+ const handle = setTimeout(() => stop(), timeout);
75
+
76
+ try {
77
+ await fn();
78
+ stop();
79
+ clearTimeout(handle);
80
+ } catch (e) {
81
+ stop();
82
+ clearTimeout(handle);
83
+ throw e;
84
+ }
85
+ return messages.join('');
86
+ }
87
+ }
@@ -0,0 +1,212 @@
1
+ import net from 'net';
2
+ import os from 'os';
3
+
4
+ /* global NodeJS */
5
+
6
+ export interface IGetPortOptions extends Omit<net.ListenOptions, 'port'> {
7
+ /**
8
+ A preferred port or an iterable of preferred ports to use.
9
+ */
10
+ readonly port?: number | Iterable<number>;
11
+
12
+ /**
13
+ The host on which port resolution should be performed. Can be either an IPv4 or IPv6 address.
14
+
15
+ By default, it checks availability on all local addresses defined in [OS network interfaces](https://nodejs.org/api/os.html#os_os_networkinterfaces). If this option is set, it will only check the given host.
16
+ */
17
+ readonly host?: string;
18
+ }
19
+
20
+ class Locked extends Error {
21
+ constructor(port = 0) {
22
+ super(`${port} is locked`);
23
+ }
24
+ }
25
+
26
+ const lockedPorts = {
27
+ old: new Set(),
28
+ young: new Set(),
29
+ };
30
+
31
+ // On this interval, the old locked ports are discarded,
32
+ // the young locked ports are moved to old locked ports,
33
+ // and a new young set for locked ports are created.
34
+ const releaseOldLockedPortsIntervalMs = 1000 * 15;
35
+
36
+ // Lazily create interval on first use
37
+ let interval:NodeJS.Timer;
38
+
39
+ const getLocalHosts = (): Set<string | undefined> => {
40
+ const interfaces = os.networkInterfaces();
41
+
42
+ // Add undefined value for createServer function to use default host,
43
+ // and default IPv4 host in case createServer defaults to IPv6.
44
+ const results = new Set([undefined, '0.0.0.0']);
45
+
46
+ for (const _interface of Object.values(interfaces)) {
47
+ if (_interface) {
48
+ for (const config of _interface) {
49
+ results.add(config.address);
50
+ }
51
+ }
52
+ }
53
+
54
+ return results;
55
+ };
56
+
57
+ const checkAvailablePort = (options: net.ListenOptions): Promise<number> =>
58
+ new Promise((resolve, reject) => {
59
+ const server = net.createServer();
60
+ server.unref();
61
+ server.on('error', reject);
62
+
63
+ server.listen(options, () => {
64
+ const {port} = server.address() as net.AddressInfo;
65
+ server.close(() => {
66
+ resolve(port);
67
+ });
68
+ });
69
+ });
70
+
71
+ const getAvailablePort = async (options: net.ListenOptions, hosts: Set<string | undefined>): Promise<number | undefined> => {
72
+ if (options.host || options.port === 0) {
73
+ return checkAvailablePort(options);
74
+ }
75
+
76
+ for (const host of hosts) {
77
+ try {
78
+ await checkAvailablePort({port: options.port, host}); // eslint-disable-line no-await-in-loop
79
+ } catch (error) {
80
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
81
+ const typed = error as any;
82
+ if (!['EADDRNOTAVAIL', 'EINVAL'].includes(typed.code)) {
83
+ throw typed;
84
+ }
85
+ }
86
+ }
87
+
88
+ return options.port;
89
+ };
90
+
91
+ const portCheckSequence = function * (ports?: number[] | Iterable<number>): IterableIterator<number|undefined> {
92
+ if (ports) {
93
+ yield * ports;
94
+ }
95
+
96
+ yield 0; // Fall back to 0 if anything else failed
97
+ };
98
+
99
+ /**
100
+ Get an available TCP port number.
101
+
102
+ @returns Port number.
103
+
104
+ @example
105
+ ```
106
+ import getPort from 'get-port';
107
+
108
+ console.log(await getPort());
109
+ //=> 51402
110
+
111
+ // Pass in a preferred port
112
+ console.log(await getPort({port: 3000}));
113
+ // Will use 3000 if available, otherwise fall back to a random port
114
+
115
+ // Pass in an array of preferred ports
116
+ console.log(await getPort({port: [3000, 3001, 3002]}));
117
+ // Will use any element in the preferred ports array if available, otherwise fall back to a random port
118
+ ```
119
+ */
120
+ export async function getPort(options?: IGetPortOptions): Promise<number> {
121
+ let ports: number[] | Iterable<number> = [];
122
+
123
+ if (options) {
124
+ if (typeof options.port === 'number') {
125
+ ports = [options.port];
126
+ } else if (options.port) {
127
+ ports = options.port;
128
+ }
129
+ }
130
+
131
+ if (interval === undefined) {
132
+ interval = setInterval(() => {
133
+ lockedPorts.old = lockedPorts.young;
134
+ lockedPorts.young = new Set();
135
+ }, releaseOldLockedPortsIntervalMs);
136
+
137
+ // Does not exist in some environments (Electron, Jest jsdom env, browser, etc).
138
+ if (interval.unref) {
139
+ interval.unref();
140
+ }
141
+ }
142
+
143
+ const hosts = getLocalHosts();
144
+
145
+ for (const port of portCheckSequence(ports)) {
146
+ try {
147
+ let availablePort = await getAvailablePort({...options, port}, hosts); // eslint-disable-line no-await-in-loop
148
+ while (lockedPorts.old.has(availablePort) || lockedPorts.young.has(availablePort)) {
149
+ if (port !== 0) {
150
+ throw new Locked(port);
151
+ }
152
+
153
+ availablePort = await getAvailablePort({...options, port}, hosts); // eslint-disable-line no-await-in-loop
154
+ }
155
+
156
+ lockedPorts.young.add(availablePort);
157
+ if (!availablePort) {
158
+ throw new Error('No available ports found');
159
+ }
160
+ return availablePort;
161
+ } catch (error) {
162
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
163
+ const typed = error as any;
164
+ if (!['EADDRINUSE', 'EACCES'].includes(typed.code) && !(typed instanceof Locked)) {
165
+ throw error;
166
+ }
167
+ }
168
+ }
169
+
170
+ throw new Error('No available ports found');
171
+ }
172
+
173
+ /**
174
+ Generate port numbers in the given range `from`...`to`.
175
+
176
+ @param from - The first port of the range. Must be in the range `1024`...`65535`.
177
+ @param to - The last port of the range. Must be in the range `1024`...`65535` and must be greater than `from`.
178
+ @returns The port numbers in the range.
179
+
180
+ @example
181
+ ```
182
+ import getPort, {portNumbers} from 'get-port';
183
+
184
+ console.log(await getPort({port: portNumbers(3000, 3100)}));
185
+ // Will use any port from 3000 to 3100, otherwise fall back to a random port
186
+ ```
187
+ */
188
+ export function portNumbers(from: number, to: number): Iterable<number> {
189
+ if (!Number.isInteger(from) || !Number.isInteger(to)) {
190
+ throw new TypeError('`from` and `to` must be integer numbers');
191
+ }
192
+
193
+ if (from < 1024 || from > 65_535) {
194
+ throw new RangeError('`from` must be between 1024 and 65535');
195
+ }
196
+
197
+ if (to < 1024 || to > 65_536) {
198
+ throw new RangeError('`to` must be between 1024 and 65536');
199
+ }
200
+
201
+ if (to < from) {
202
+ throw new RangeError('`to` must be greater than or equal to `from`');
203
+ }
204
+
205
+ const generator = function * (from: number, to: number): IterableIterator<number> {
206
+ for (let port = from; port <= to; port++) {
207
+ yield port;
208
+ }
209
+ };
210
+
211
+ return generator(from, to);
212
+ }