@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,62 @@
1
+ import { IRequestLog } from '../../models/RequestLog.js';
2
+ export interface IProjectExecutionIteration {
3
+ /**
4
+ * The index of the iteration.
5
+ */
6
+ index: number;
7
+ /**
8
+ * The list of requests executed in the iteration.
9
+ */
10
+ executed: IRequestLog[];
11
+ /**
12
+ * Optional general error message.
13
+ */
14
+ error?: string;
15
+ }
16
+ export interface IProjectExecutionLog {
17
+ /**
18
+ * The timestamp when the execution started
19
+ */
20
+ started: number;
21
+ /**
22
+ * The timestamp when the execution ended
23
+ */
24
+ ended: number;
25
+ /**
26
+ * The execution logs for each iteration.
27
+ */
28
+ iterations: IProjectExecutionIteration[];
29
+ }
30
+ /**
31
+ * Base class for project execution reporters.
32
+ */
33
+ export declare abstract class Reporter {
34
+ info: IProjectExecutionLog;
35
+ constructor(info: IProjectExecutionLog);
36
+ /**
37
+ * Generates the report for the current execution log.
38
+ */
39
+ abstract generate(): Promise<void>;
40
+ /**
41
+ * Checks whether the execution log should be considered a failure.
42
+ * @param log The execution log.
43
+ * @returns `true` when the request was a failure.
44
+ */
45
+ protected isFailedLog(log: IRequestLog): boolean;
46
+ /**
47
+ * Computes the number of requests that failed.
48
+ */
49
+ computeFailed(): number;
50
+ /**
51
+ * Computes the number of requests that ended with the status code 399 at the most.
52
+ */
53
+ computeSucceeded(): number;
54
+ /**
55
+ * Computes the total time of sending each request.
56
+ */
57
+ computeTotalTime(): number;
58
+ /**
59
+ * Computes the total size of received data.
60
+ */
61
+ computeTotalSize(): number;
62
+ }
@@ -0,0 +1,98 @@
1
+ import { ErrorResponse } from '../../models/ErrorResponse.js';
2
+ /**
3
+ * Base class for project execution reporters.
4
+ */
5
+ export class Reporter {
6
+ info;
7
+ constructor(info) {
8
+ this.info = info;
9
+ }
10
+ /**
11
+ * Checks whether the execution log should be considered a failure.
12
+ * @param log The execution log.
13
+ * @returns `true` when the request was a failure.
14
+ */
15
+ isFailedLog(log) {
16
+ if (!log.response || ErrorResponse.isErrorResponse(log.response)) {
17
+ return true;
18
+ }
19
+ const response = log.response;
20
+ if (response.status >= 400) {
21
+ return true;
22
+ }
23
+ return false;
24
+ }
25
+ /**
26
+ * Computes the number of requests that failed.
27
+ */
28
+ computeFailed() {
29
+ let result = 0;
30
+ const { info } = this;
31
+ const { iterations } = info;
32
+ iterations.forEach((iteration) => {
33
+ iteration.executed.forEach((log) => {
34
+ if (this.isFailedLog(log)) {
35
+ result++;
36
+ }
37
+ });
38
+ });
39
+ return result;
40
+ }
41
+ /**
42
+ * Computes the number of requests that ended with the status code 399 at the most.
43
+ */
44
+ computeSucceeded() {
45
+ let result = 0;
46
+ const { info } = this;
47
+ const { iterations } = info;
48
+ iterations.forEach((iteration) => {
49
+ iteration.executed.forEach((log) => {
50
+ if (!log.response || ErrorResponse.isErrorResponse(log.response)) {
51
+ return;
52
+ }
53
+ const response = log.response;
54
+ if (response.status < 400) {
55
+ result++;
56
+ }
57
+ });
58
+ });
59
+ return result;
60
+ }
61
+ /**
62
+ * Computes the total time of sending each request.
63
+ */
64
+ computeTotalTime() {
65
+ let result = 0;
66
+ const { info } = this;
67
+ const { iterations } = info;
68
+ iterations.forEach((iteration) => {
69
+ iteration.executed.forEach((log) => {
70
+ if (!log.response || ErrorResponse.isErrorResponse(log.response)) {
71
+ return;
72
+ }
73
+ const response = log.response;
74
+ if (response.loadingTime && response.loadingTime > 0) {
75
+ result += response.loadingTime;
76
+ }
77
+ });
78
+ });
79
+ return result;
80
+ }
81
+ /**
82
+ * Computes the total size of received data.
83
+ */
84
+ computeTotalSize() {
85
+ let result = 0;
86
+ const { info } = this;
87
+ const { iterations } = info;
88
+ iterations.forEach((iteration) => {
89
+ iteration.executed.forEach((log) => {
90
+ if (log.size && log.size.response) {
91
+ result += log.size.response;
92
+ }
93
+ });
94
+ });
95
+ return result;
96
+ }
97
+ }
98
+ //# sourceMappingURL=Reporter.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Reporter.js","sourceRoot":"","sources":["../../../../src/runtime/reporters/Reporter.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,aAAa,EAAE,MAAM,+BAA+B,CAAC;AAgC9D;;GAEG;AACH,MAAM,OAAgB,QAAQ;IAC5B,IAAI,CAAuB;IAE3B,YAAY,IAA0B;QACpC,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;IACnB,CAAC;IAOD;;;;OAIG;IACO,WAAW,CAAC,GAAgB;QACpC,IAAI,CAAC,GAAG,CAAC,QAAQ,IAAI,aAAa,CAAC,eAAe,CAAC,GAAG,CAAC,QAAQ,CAAC,EAAE;YAChE,OAAO,IAAI,CAAC;SACb;QACD,MAAM,QAAQ,GAAG,GAAG,CAAC,QAAwB,CAAC;QAC9C,IAAI,QAAQ,CAAC,MAAM,IAAI,GAAG,EAAE;YAC1B,OAAO,IAAI,CAAC;SACb;QACD,OAAO,KAAK,CAAC;IACf,CAAC;IAED;;OAEG;IACH,aAAa;QACX,IAAI,MAAM,GAAG,CAAC,CAAC;QACf,MAAM,EAA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O,CAAC,CAAC,GAAG,EAAE,EAAE;gBACjC,IAAI,CAAC,GAAG,CAAC,QAAQ,IAAI,aAAa,CAAC,eAAe,CAAC,GAAG,CAAC,QAAQ,CAAC,EAAE;oBAChE,OAAO;iBACR;gBACD,MAAM,QAAQ,GAAG,GAAG,CAAC,QAAwB,CAAC;gBAC9C,IAAI,QAAQ,CAAC,WAAW,IAAI,QAAQ,CAAC,WAAW,GAAG,CAAC,EAAE;oBACpD,MAAM,IAAI,QAAQ,CAAC,WAAW,CAAC;iBAChC;YACH,CAAC,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;QACH,OAAO,MAAM,CAAC;IAChB,CAAC;IAED;;OAEG;IACH,gBAAgB;QACd,IAAI,MAAM,GAAG,CAAC,CAAC;QACf,MAAM,EAAE,IAAI,EAAE,GAAG,IAAI,CAAC;QACtB,MAAM,EAAE,UAAU,EAAE,GAAG,IAAI,CAAC;QAC5B,UAAU,CAAC,OAAO,CAAC,CAAC,SAAS,EAAE,EAAE;YAC/B,SAAS,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC,GAAG,EAAE,EAAE;gBACjC,IAAI,GAAG,CAAC,IAAI,IAAI,GAAG,CAAC,IAAI,CAAC,QAAQ,EAAE;oBACjC,MAAM,IAAI,GAAG,CAAC,IAAI,CAAC,QAAQ,CAAC;iBAC7B;YACH,CAAC,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;QACH,OAAO,MAAM,CAAC;IAChB,CAAC;CACF"}
@@ -0,0 +1,29 @@
1
+ export interface ITestRunCommandOptions {
2
+ includeError?: boolean;
3
+ noCleaning?: boolean;
4
+ }
5
+ export declare class TestCliHelper {
6
+ /**
7
+ * The globally set test timeout.
8
+ * @default 2000 The mocha default test timeout.
9
+ */
10
+ static testTimeout: number;
11
+ static cleanTerminalOutput(s: string): string;
12
+ static splitLines(table: string): string[];
13
+ /**
14
+ * Executes a passed asynchronous function and captures stdout.
15
+ * When the function fails, it cleans up output listeners and throws the error.
16
+ *
17
+ * ```javascript
18
+ * const out = grabOutput(async () => {
19
+ * // ...
20
+ * });
21
+ * console.log(out); // combined stdout and stderr.
22
+ * ```
23
+ *
24
+ * @param fn The function to execute.
25
+ * @param timeout The test timeout. After this time the output is reset.
26
+ * @returns The terminal output.
27
+ */
28
+ static grabOutput(fn: () => Promise<void>, timeout?: number): Promise<string>;
29
+ }
@@ -0,0 +1,80 @@
1
+ export class TestCliHelper {
2
+ /**
3
+ * The globally set test timeout.
4
+ * @default 2000 The mocha default test timeout.
5
+ */
6
+ static testTimeout = 2000;
7
+ static cleanTerminalOutput(s) {
8
+ let result = s.trim();
9
+ result = result.replace(/[^\x20-\x7E\n]/gm, '');
10
+ // result = result.replace(/\[\d+m/gm, '');
11
+ result = result.replace(/\[\d+[a-zA-Z]/gm, '');
12
+ result = result.split('\n').filter(i => !!i.trim()).join('\n');
13
+ return result;
14
+ }
15
+ static splitLines(table) {
16
+ const result = [];
17
+ table.split('\n').forEach((line) => {
18
+ const value = line.trim();
19
+ if (!value) {
20
+ return;
21
+ }
22
+ result.push(value);
23
+ });
24
+ return result;
25
+ }
26
+ /**
27
+ * Executes a passed asynchronous function and captures stdout.
28
+ * When the function fails, it cleans up output listeners and throws the error.
29
+ *
30
+ * ```javascript
31
+ * const out = grabOutput(async () => {
32
+ * // ...
33
+ * });
34
+ * console.log(out); // combined stdout and stderr.
35
+ * ```
36
+ *
37
+ * @param fn The function to execute.
38
+ * @param timeout The test timeout. After this time the output is reset.
39
+ * @returns The terminal output.
40
+ */
41
+ static async grabOutput(fn, timeout = TestCliHelper.testTimeout) {
42
+ const messages = [];
43
+ function noop() {
44
+ //
45
+ }
46
+ const origOut = process.stdout.write;
47
+ const origErr = process.stderr.write;
48
+ const origClear = console.clear;
49
+ function messageHandler(buffer) {
50
+ if (typeof buffer === 'string') {
51
+ messages.push(buffer);
52
+ }
53
+ else {
54
+ messages.push(buffer.toString('utf8'));
55
+ }
56
+ return true;
57
+ }
58
+ function stop() {
59
+ process.stdout.write = origOut;
60
+ process.stderr.write = origErr;
61
+ console.clear = origClear;
62
+ }
63
+ process.stdout.write = messageHandler;
64
+ process.stderr.write = messageHandler;
65
+ console.clear = noop;
66
+ const handle = setTimeout(() => stop(), timeout);
67
+ try {
68
+ await fn();
69
+ stop();
70
+ clearTimeout(handle);
71
+ }
72
+ catch (e) {
73
+ stop();
74
+ clearTimeout(handle);
75
+ throw e;
76
+ }
77
+ return messages.join('');
78
+ }
79
+ }
80
+ //# sourceMappingURL=TestCliHelper.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"TestCliHelper.js","sourceRoot":"","sources":["../../../src/testing/TestCliHelper.ts"],"names":[],"mappings":"AAKA,MAAM,OAAO,aAAa;IACxB;;;OAGG;IACH,MAAM,CAAC,WAAW,GAAG,IAAI,CAAC;IAE1B,MAAM,CAAC,mBAAmB,CAAC,CAAS;QAClC,IAAI,MAAM,GAAG,CAAC,CAAC,IAAI,EAAE,CAAC;QACtB,MAAM,GAAG,MAAM,CAAC,OAAO,CAAC,kBAAkB,EAAE,EAAE,CAAC,CAAC;QAChD,2CAA2C;QAC3C,MAAM,GAAG,MAAM,CAAC,OAAO,CAAC,iBAAiB,EAAE,EAAE,CAAC,CAAC;QAC/C,MAAM,GAAG,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAC/D,OAAO,MAAM,CAAC;IAChB,CAAC;IAED,MAAM,CAAC,UAAU,CAAC,KAAa;QAC7B,MAAM,MAAM,GAAa,EAAE,CAAC;QAC5B,KAAK,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,OAAO,CAAC,CAAC,IAAI,EAAE,EAAE;YACjC,MAAM,KAAK,GAAG,IAAI,CAAC,IAAI,EAAE,CAAC;YAC1B,IAAI,CAAC,KAAK,EAAE;gBACV,OAAO;aACR;YACD,M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,CAAC;SACtB;QAAC,OAAO,CAAC,EAAE;YACV,IAAI,EAAE,CAAC;YACP,YAAY,CAAC,MAAM,CAAC,CAAC;YACrB,MAAM,CAAC,CAAC;SACT;QACD,OAAO,QAAQ,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;IAC3B,CAAC"}
@@ -0,0 +1,52 @@
1
+ /// <reference types="node" />
2
+ import net from 'net';
3
+ export interface IGetPortOptions extends Omit<net.ListenOptions, 'port'> {
4
+ /**
5
+ A preferred port or an iterable of preferred ports to use.
6
+ */
7
+ readonly port?: number | Iterable<number>;
8
+ /**
9
+ The host on which port resolution should be performed. Can be either an IPv4 or IPv6 address.
10
+
11
+ 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.
12
+ */
13
+ readonly host?: string;
14
+ }
15
+ /**
16
+ Get an available TCP port number.
17
+
18
+ @returns Port number.
19
+
20
+ @example
21
+ ```
22
+ import getPort from 'get-port';
23
+
24
+ console.log(await getPort());
25
+ //=> 51402
26
+
27
+ // Pass in a preferred port
28
+ console.log(await getPort({port: 3000}));
29
+ // Will use 3000 if available, otherwise fall back to a random port
30
+
31
+ // Pass in an array of preferred ports
32
+ console.log(await getPort({port: [3000, 3001, 3002]}));
33
+ // Will use any element in the preferred ports array if available, otherwise fall back to a random port
34
+ ```
35
+ */
36
+ export declare function getPort(options?: IGetPortOptions): Promise<number>;
37
+ /**
38
+ Generate port numbers in the given range `from`...`to`.
39
+
40
+ @param from - The first port of the range. Must be in the range `1024`...`65535`.
41
+ @param to - The last port of the range. Must be in the range `1024`...`65535` and must be greater than `from`.
42
+ @returns The port numbers in the range.
43
+
44
+ @example
45
+ ```
46
+ import getPort, {portNumbers} from 'get-port';
47
+
48
+ console.log(await getPort({port: portNumbers(3000, 3100)}));
49
+ // Will use any port from 3000 to 3100, otherwise fall back to a random port
50
+ ```
51
+ */
52
+ export declare function portNumbers(from: number, to: number): Iterable<number>;
@@ -0,0 +1,169 @@
1
+ import net from 'net';
2
+ import os from 'os';
3
+ class Locked extends Error {
4
+ constructor(port = 0) {
5
+ super(`${port} is locked`);
6
+ }
7
+ }
8
+ const lockedPorts = {
9
+ old: new Set(),
10
+ young: new Set(),
11
+ };
12
+ // On this interval, the old locked ports are discarded,
13
+ // the young locked ports are moved to old locked ports,
14
+ // and a new young set for locked ports are created.
15
+ const releaseOldLockedPortsIntervalMs = 1000 * 15;
16
+ // Lazily create interval on first use
17
+ let interval;
18
+ const getLocalHosts = () => {
19
+ const interfaces = os.networkInterfaces();
20
+ // Add undefined value for createServer function to use default host,
21
+ // and default IPv4 host in case createServer defaults to IPv6.
22
+ const results = new Set([undefined, '0.0.0.0']);
23
+ for (const _interface of Object.values(interfaces)) {
24
+ if (_interface) {
25
+ for (const config of _interface) {
26
+ results.add(config.address);
27
+ }
28
+ }
29
+ }
30
+ return results;
31
+ };
32
+ const checkAvailablePort = (options) => new Promise((resolve, reject) => {
33
+ const server = net.createServer();
34
+ server.unref();
35
+ server.on('error', reject);
36
+ server.listen(options, () => {
37
+ const { port } = server.address();
38
+ server.close(() => {
39
+ resolve(port);
40
+ });
41
+ });
42
+ });
43
+ const getAvailablePort = async (options, hosts) => {
44
+ if (options.host || options.port === 0) {
45
+ return checkAvailablePort(options);
46
+ }
47
+ for (const host of hosts) {
48
+ try {
49
+ await checkAvailablePort({ port: options.port, host }); // eslint-disable-line no-await-in-loop
50
+ }
51
+ catch (error) {
52
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
53
+ const typed = error;
54
+ if (!['EADDRNOTAVAIL', 'EINVAL'].includes(typed.code)) {
55
+ throw typed;
56
+ }
57
+ }
58
+ }
59
+ return options.port;
60
+ };
61
+ const portCheckSequence = function* (ports) {
62
+ if (ports) {
63
+ yield* ports;
64
+ }
65
+ yield 0; // Fall back to 0 if anything else failed
66
+ };
67
+ /**
68
+ Get an available TCP port number.
69
+
70
+ @returns Port number.
71
+
72
+ @example
73
+ ```
74
+ import getPort from 'get-port';
75
+
76
+ console.log(await getPort());
77
+ //=> 51402
78
+
79
+ // Pass in a preferred port
80
+ console.log(await getPort({port: 3000}));
81
+ // Will use 3000 if available, otherwise fall back to a random port
82
+
83
+ // Pass in an array of preferred ports
84
+ console.log(await getPort({port: [3000, 3001, 3002]}));
85
+ // Will use any element in the preferred ports array if available, otherwise fall back to a random port
86
+ ```
87
+ */
88
+ export async function getPort(options) {
89
+ let ports = [];
90
+ if (options) {
91
+ if (typeof options.port === 'number') {
92
+ ports = [options.port];
93
+ }
94
+ else if (options.port) {
95
+ ports = options.port;
96
+ }
97
+ }
98
+ if (interval === undefined) {
99
+ interval = setInterval(() => {
100
+ lockedPorts.old = lockedPorts.young;
101
+ lockedPorts.young = new Set();
102
+ }, releaseOldLockedPortsIntervalMs);
103
+ // Does not exist in some environments (Electron, Jest jsdom env, browser, etc).
104
+ if (interval.unref) {
105
+ interval.unref();
106
+ }
107
+ }
108
+ const hosts = getLocalHosts();
109
+ for (const port of portCheckSequence(ports)) {
110
+ try {
111
+ let availablePort = await getAvailablePort({ ...options, port }, hosts); // eslint-disable-line no-await-in-loop
112
+ while (lockedPorts.old.has(availablePort) || lockedPorts.young.has(availablePort)) {
113
+ if (port !== 0) {
114
+ throw new Locked(port);
115
+ }
116
+ availablePort = await getAvailablePort({ ...options, port }, hosts); // eslint-disable-line no-await-in-loop
117
+ }
118
+ lockedPorts.young.add(availablePort);
119
+ if (!availablePort) {
120
+ throw new Error('No available ports found');
121
+ }
122
+ return availablePort;
123
+ }
124
+ catch (error) {
125
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
126
+ const typed = error;
127
+ if (!['EADDRINUSE', 'EACCES'].includes(typed.code) && !(typed instanceof Locked)) {
128
+ throw error;
129
+ }
130
+ }
131
+ }
132
+ throw new Error('No available ports found');
133
+ }
134
+ /**
135
+ Generate port numbers in the given range `from`...`to`.
136
+
137
+ @param from - The first port of the range. Must be in the range `1024`...`65535`.
138
+ @param to - The last port of the range. Must be in the range `1024`...`65535` and must be greater than `from`.
139
+ @returns The port numbers in the range.
140
+
141
+ @example
142
+ ```
143
+ import getPort, {portNumbers} from 'get-port';
144
+
145
+ console.log(await getPort({port: portNumbers(3000, 3100)}));
146
+ // Will use any port from 3000 to 3100, otherwise fall back to a random port
147
+ ```
148
+ */
149
+ export function portNumbers(from, to) {
150
+ if (!Number.isInteger(from) || !Number.isInteger(to)) {
151
+ throw new TypeError('`from` and `to` must be integer numbers');
152
+ }
153
+ if (from < 1024 || from > 65_535) {
154
+ throw new RangeError('`from` must be between 1024 and 65535');
155
+ }
156
+ if (to < 1024 || to > 65_536) {
157
+ throw new RangeError('`to` must be between 1024 and 65536');
158
+ }
159
+ if (to < from) {
160
+ throw new RangeError('`to` must be greater than or equal to `from`');
161
+ }
162
+ const generator = function* (from, to) {
163
+ for (let port = from; port <= to; port++) {
164
+ yield port;
165
+ }
166
+ };
167
+ return generator(from, to);
168
+ }
169
+ //# sourceMappingURL=getPort.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getPort.js","sourceRoot":"","sources":["../../../src/testing/getPort.ts"],"names":[],"mappings":"AAAA,OAAO,GAAG,MAAM,KAAK,CAAC;AACtB,OAAO,EAAE,MAAM,IAAI,CAAC;AAkBpB,MAAM,MAAO,SAAQ,KAAK;IACzB,YAAY,IAAI,GAAG,CAAC;QACnB,KAAK,CAAC,GAAG,IAAI,YAAY,CAAC,CAAC;IAC5B,CAAC;CACD;AAED,MAAM,WAAW,GAAG;IACnB,GAAG,EAAE,IAAI,GAAG,EAAE;IACd,KAAK,EAAE,IAAI,GAAG,EAAE;CAChB,CAAC;AAEF,wDAAwD;AACxD,wDAAwD;AACxD,oDAAoD;AACpD,MAAM,+BAA+B,GAAG,IAAI,GAAG,EAAE,CAAC;AAElD,sCAAsC;AACtC,IAAI,QAAqB,CAAC;AAE1B,MAAM,aAAa,GAAG,GAA4B,EAAE;IACnD,MAAM,UAAU,GAAG,EAAE,CAAC,iBAAiB,EAAE,CAA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AAC;KAC/D;IAED,IAAI,IAAI,GAAG,IAAI,IAAI,IAAI,GAAG,MAAM,EAAE;QACjC,MAAM,IAAI,UAAU,CAAC,uCAAuC,CAAC,CAAC;KAC9D;IAED,IAAI,EAAE,GAAG,IAAI,IAAI,EAAE,GAAG,MAAM,EAAE;QAC7B,MAAM,IAAI,UAAU,CAAC,qCAAqC,CAAC,CAAC;KAC5D;IAED,IAAI,EAAE,GAAG,IAAI,EAAE;QACd,MAAM,IAAI,UAAU,CAAC,8CAA8C,CAAC,CAAC;KACrE;IAED,MAAM,SAAS,GAAG,QAAS,CAAC,EAAE,IAAY,EAAE,EAAU;QACrD,KAAK,IAAI,IAAI,GAAG,IAAI,EAAE,IAAI,IAAI,EAAE,EAAE,IAAI,EAAE,EAAE;YACzC,MAAM,IAAI,CAAC;SACX;IACF,CAAC,CAAC;IAEF,OAAO,SAAS,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;AAC5B,CAAC"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@api-client/core",
3
3
  "description": "The ARC core client library. Works in NodeJS and in a ES enabled browser.",
4
- "version": "0.3.5",
4
+ "version": "0.3.8",
5
5
  "license": "Apache-2.0",
6
6
  "main": "build/index.js",
7
7
  "module": "build/index.js",
@@ -26,7 +26,6 @@
26
26
  "@commitlint/cli": "^16.2.1",
27
27
  "@commitlint/config-conventional": "^16.2.1",
28
28
  "@esm-bundle/chai": "^4.3.4-fix.0",
29
- "@pawel-up/data-mock": "^0.1.7",
30
29
  "@types/chai": "^4.2.22",
31
30
  "@types/chai-as-promised": "^7.1.5",
32
31
  "@types/cors": "^2.8.12",
@@ -88,7 +87,9 @@
88
87
  "dependencies": {
89
88
  "@metrichor/jmespath": "^0.3.1",
90
89
  "@pawel-up/jexl": "^3.0.0",
90
+ "@pawel-up/data-mock": "^0.1.7",
91
91
  "@xmldom/xmldom": "^0.8.1",
92
+ "console-table-printer": "^2.10.0",
92
93
  "form-data": "^4.0.0",
93
94
  "json8-patch": "^1.0.6",
94
95
  "ws": "^8.5.0",
@@ -0,0 +1,91 @@
1
+ /**
2
+ * Computations related to the data like size.
3
+ */
4
+ export class DataCalculator {
5
+ /**
6
+ * Computes size in the nearest units
7
+ */
8
+ static bytesToSize(bytes: number, decimals = 2): string {
9
+ if (bytes === 0) {
10
+ return '0 Bytes';
11
+ }
12
+
13
+ const k = 1024;
14
+ const dm = decimals < 0 ? 0 : decimals;
15
+ const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
16
+
17
+ const i = Math.floor(Math.log(bytes) / Math.log(k));
18
+ const result = parseFloat((bytes / k**i).toFixed(dm));
19
+ return `${result} ${sizes[i]}`;
20
+ }
21
+
22
+ /**
23
+ * Calculates size of the string
24
+ * @param str A string to compute size from.
25
+ * @returns The size of the string.
26
+ */
27
+ stringSize(str: string): number {
28
+ if (!str || !str.length || typeof str !== 'string') {
29
+ return 0;
30
+ }
31
+ let s = str.length;
32
+ for (let i = str.length - 1; i >= 0; i--) {
33
+ const code = str.charCodeAt(i);
34
+ if (code > 0x7f && code <= 0x7ff) {
35
+ s++;
36
+ } else if (code > 0x7ff && code <= 0xffff) {
37
+ /* istanbul ignore next */
38
+ s += 2;
39
+ }
40
+ /* istanbul ignore if */
41
+ if (code >= 0xDC00 && code <= 0xDFFF) {
42
+ i--; // trail surrogate
43
+ }
44
+ }
45
+ return s;
46
+ }
47
+
48
+ /**
49
+ * @param data The size of the form data
50
+ * @returns The size of the form data
51
+ */
52
+ async formDataSize(data: FormData): Promise<number> {
53
+ if (typeof Request === 'undefined') {
54
+ return 0;
55
+ }
56
+ const request = new Request('/', {
57
+ method: 'POST',
58
+ body: data,
59
+ });
60
+ if (!request.arrayBuffer) {
61
+ return 0;
62
+ }
63
+ const buffer = await request.arrayBuffer();
64
+ return buffer.byteLength;
65
+ }
66
+
67
+ /**
68
+ * Computes size of the payload.
69
+ *
70
+ * @param payload The payload to compute te size for
71
+ * @returns The size of the payload
72
+ */
73
+ async payloadSize(payload: unknown): Promise<number> {
74
+ if (!payload) {
75
+ return 0;
76
+ }
77
+ if (payload instanceof ArrayBuffer) {
78
+ return payload.byteLength;
79
+ }
80
+ if (typeof Buffer !== 'undefined' && payload instanceof Buffer) {
81
+ return payload.byteLength;
82
+ }
83
+ if (payload instanceof Blob) {
84
+ return payload.size;
85
+ }
86
+ if (payload instanceof FormData) {
87
+ return this.formDataSize(payload);
88
+ }
89
+ return this.stringSize(String(payload));
90
+ }
91
+ }