@elastic/synthetics 1.0.0-beta.4 → 1.0.0-beta.41

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 (220) hide show
  1. package/README.md +4 -3
  2. package/dist/cli.js +137 -160
  3. package/dist/cli.js.map +1 -1
  4. package/dist/common_types.d.ts +161 -31
  5. package/dist/common_types.d.ts.map +1 -1
  6. package/dist/config.d.ts +3 -5
  7. package/dist/config.d.ts.map +1 -1
  8. package/dist/config.js +7 -6
  9. package/dist/config.js.map +1 -1
  10. package/dist/core/browser-service.d.ts +1 -1
  11. package/dist/core/browser-service.d.ts.map +1 -1
  12. package/dist/core/browser-service.js +7 -6
  13. package/dist/core/browser-service.js.map +1 -1
  14. package/dist/core/expect.d.ts +25 -25
  15. package/dist/core/expect.d.ts.map +1 -1
  16. package/dist/core/expect.js.map +1 -1
  17. package/dist/core/gatherer.d.ts +4 -8
  18. package/dist/core/gatherer.d.ts.map +1 -1
  19. package/dist/core/gatherer.js +53 -15
  20. package/dist/core/gatherer.js.map +1 -1
  21. package/dist/core/index.d.ts +5 -1
  22. package/dist/core/index.d.ts.map +1 -1
  23. package/dist/core/index.js +23 -11
  24. package/dist/core/index.js.map +1 -1
  25. package/dist/core/logger.d.ts +0 -1
  26. package/dist/core/logger.d.ts.map +1 -1
  27. package/dist/core/logger.js +3 -16
  28. package/dist/core/logger.js.map +1 -1
  29. package/dist/core/runner.d.ts +12 -63
  30. package/dist/core/runner.d.ts.map +1 -1
  31. package/dist/core/runner.js +156 -90
  32. package/dist/core/runner.js.map +1 -1
  33. package/dist/core/transform.d.ts +37 -0
  34. package/dist/core/transform.d.ts.map +1 -0
  35. package/dist/core/transform.js +148 -0
  36. package/dist/core/transform.js.map +1 -0
  37. package/dist/dsl/index.js +5 -1
  38. package/dist/dsl/index.js.map +1 -1
  39. package/dist/dsl/journey.d.ts +9 -4
  40. package/dist/dsl/journey.d.ts.map +1 -1
  41. package/dist/dsl/journey.js +37 -9
  42. package/dist/dsl/journey.js.map +1 -1
  43. package/dist/dsl/monitor.d.ts +83 -0
  44. package/dist/dsl/monitor.d.ts.map +1 -0
  45. package/dist/dsl/monitor.js +108 -0
  46. package/dist/dsl/monitor.js.map +1 -0
  47. package/dist/dsl/step.d.ts +3 -2
  48. package/dist/dsl/step.d.ts.map +1 -1
  49. package/dist/dsl/step.js +6 -1
  50. package/dist/dsl/step.js.map +1 -1
  51. package/dist/formatter/javascript.d.ts +98 -0
  52. package/dist/formatter/javascript.d.ts.map +1 -0
  53. package/dist/formatter/javascript.js +298 -0
  54. package/dist/formatter/javascript.js.map +1 -0
  55. package/dist/generator/index.d.ts +24 -0
  56. package/dist/generator/index.d.ts.map +1 -0
  57. package/dist/generator/index.js +241 -0
  58. package/dist/generator/index.js.map +1 -0
  59. package/dist/{reporters/reporter.js → generator/utils.d.ts} +5 -3
  60. package/dist/generator/utils.d.ts.map +1 -0
  61. package/dist/generator/utils.js +84 -0
  62. package/dist/generator/utils.js.map +1 -0
  63. package/dist/helpers.d.ts +28 -3
  64. package/dist/helpers.d.ts.map +1 -1
  65. package/dist/helpers.js +152 -20
  66. package/dist/helpers.js.map +1 -1
  67. package/dist/index.d.ts +6 -7
  68. package/dist/index.d.ts.map +1 -1
  69. package/dist/index.js +5 -26
  70. package/dist/index.js.map +1 -1
  71. package/dist/loader.d.ts +34 -0
  72. package/dist/loader.d.ts.map +1 -0
  73. package/dist/loader.js +149 -0
  74. package/dist/loader.js.map +1 -0
  75. package/dist/locations/index.d.ts +45 -0
  76. package/dist/locations/index.d.ts.map +1 -0
  77. package/dist/locations/index.js +83 -0
  78. package/dist/locations/index.js.map +1 -0
  79. package/dist/{reporters/reporter.d.ts → locations/public-locations.d.ts} +12 -8
  80. package/dist/locations/public-locations.d.ts.map +1 -0
  81. package/dist/locations/public-locations.js +41 -0
  82. package/dist/locations/public-locations.js.map +1 -0
  83. package/dist/options.d.ts +41 -0
  84. package/dist/options.d.ts.map +1 -0
  85. package/dist/options.js +198 -0
  86. package/dist/options.js.map +1 -0
  87. package/dist/plugins/browser-console.d.ts +5 -4
  88. package/dist/plugins/browser-console.d.ts.map +1 -1
  89. package/dist/plugins/browser-console.js +44 -24
  90. package/dist/plugins/browser-console.js.map +1 -1
  91. package/dist/plugins/index.js +5 -1
  92. package/dist/plugins/index.js.map +1 -1
  93. package/dist/plugins/network.d.ts +20 -33
  94. package/dist/plugins/network.d.ts.map +1 -1
  95. package/dist/plugins/network.js +177 -216
  96. package/dist/plugins/network.js.map +1 -1
  97. package/dist/plugins/performance.d.ts +4 -19
  98. package/dist/plugins/performance.d.ts.map +1 -1
  99. package/dist/plugins/performance.js +6 -5
  100. package/dist/plugins/performance.js.map +1 -1
  101. package/dist/plugins/plugin-manager.d.ts +9 -5
  102. package/dist/plugins/plugin-manager.d.ts.map +1 -1
  103. package/dist/plugins/plugin-manager.js +46 -30
  104. package/dist/plugins/plugin-manager.js.map +1 -1
  105. package/dist/plugins/tracing.d.ts +6 -6
  106. package/dist/plugins/tracing.d.ts.map +1 -1
  107. package/dist/plugins/tracing.js +10 -7
  108. package/dist/plugins/tracing.js.map +1 -1
  109. package/dist/push/bundler.d.ts +35 -0
  110. package/dist/push/bundler.d.ts.map +1 -0
  111. package/dist/push/bundler.js +131 -0
  112. package/dist/push/bundler.js.map +1 -0
  113. package/dist/push/index.d.ts +9 -0
  114. package/dist/push/index.d.ts.map +1 -0
  115. package/dist/push/index.js +227 -0
  116. package/dist/push/index.js.map +1 -0
  117. package/dist/push/kibana_api.d.ts +56 -0
  118. package/dist/push/kibana_api.d.ts.map +1 -0
  119. package/dist/push/kibana_api.js +135 -0
  120. package/dist/push/kibana_api.js.map +1 -0
  121. package/dist/push/monitor.d.ts +51 -0
  122. package/dist/push/monitor.d.ts.map +1 -0
  123. package/dist/push/monitor.js +244 -0
  124. package/dist/push/monitor.js.map +1 -0
  125. package/dist/{parse_args.d.ts → push/plugin.d.ts} +9 -6
  126. package/dist/push/plugin.d.ts.map +1 -0
  127. package/dist/push/plugin.js +66 -0
  128. package/dist/push/plugin.js.map +1 -0
  129. package/dist/push/request.d.ts +45 -0
  130. package/dist/push/request.d.ts.map +1 -0
  131. package/dist/push/request.js +109 -0
  132. package/dist/push/request.js.map +1 -0
  133. package/dist/push/utils.d.ts +31 -0
  134. package/dist/push/utils.d.ts.map +1 -0
  135. package/dist/push/utils.js +64 -0
  136. package/dist/push/utils.js.map +1 -0
  137. package/dist/reporters/base.d.ts +14 -6
  138. package/dist/reporters/base.d.ts.map +1 -1
  139. package/dist/reporters/base.js +58 -50
  140. package/dist/reporters/base.js.map +1 -1
  141. package/dist/reporters/index.d.ts +18 -8
  142. package/dist/reporters/index.d.ts.map +1 -1
  143. package/dist/reporters/index.js.map +1 -1
  144. package/dist/reporters/json.d.ts +17 -62
  145. package/dist/reporters/json.d.ts.map +1 -1
  146. package/dist/reporters/json.js +149 -179
  147. package/dist/reporters/json.js.map +1 -1
  148. package/dist/reporters/junit.d.ts +7 -1
  149. package/dist/reporters/junit.d.ts.map +1 -1
  150. package/dist/reporters/junit.js +90 -95
  151. package/dist/reporters/junit.js.map +1 -1
  152. package/dist/sdk/lh-trace-processor.d.ts +35 -9
  153. package/dist/sdk/lh-trace-processor.d.ts.map +1 -1
  154. package/dist/sdk/lh-trace-processor.js +114 -53
  155. package/dist/sdk/lh-trace-processor.js.map +1 -1
  156. package/dist/sdk/trace-metrics.d.ts +4 -4
  157. package/dist/sdk/trace-metrics.d.ts.map +1 -1
  158. package/dist/sdk/trace-metrics.js +3 -4
  159. package/dist/sdk/trace-metrics.js.map +1 -1
  160. package/dist/sdk/trace-processor.d.ts +11 -3
  161. package/dist/sdk/trace-processor.d.ts.map +1 -1
  162. package/dist/sdk/trace-processor.js +17 -9
  163. package/dist/sdk/trace-processor.js.map +1 -1
  164. package/package.json +39 -24
  165. package/src/cli.ts +219 -151
  166. package/src/common_types.ts +193 -31
  167. package/src/config.ts +3 -6
  168. package/src/core/expect.ts +58 -24
  169. package/src/core/gatherer.ts +64 -24
  170. package/src/core/index.ts +36 -15
  171. package/src/core/logger.ts +1 -12
  172. package/src/core/runner.ts +151 -136
  173. package/src/core/transform.ts +160 -0
  174. package/src/dsl/journey.ts +39 -6
  175. package/src/dsl/monitor.ts +140 -0
  176. package/src/dsl/step.ts +9 -2
  177. package/src/formatter/javascript.ts +367 -0
  178. package/src/generator/index.ts +308 -0
  179. package/src/generator/utils.ts +84 -0
  180. package/src/helpers.ts +159 -13
  181. package/src/index.ts +22 -22
  182. package/src/loader.ts +167 -0
  183. package/src/locations/index.ts +95 -0
  184. package/src/{reporters/reporter.ts → locations/public-locations.ts} +12 -9
  185. package/src/options.ts +227 -0
  186. package/src/plugins/browser-console.ts +30 -8
  187. package/src/plugins/network.ts +208 -267
  188. package/src/plugins/performance.ts +6 -22
  189. package/src/plugins/plugin-manager.ts +49 -31
  190. package/src/plugins/tracing.ts +8 -9
  191. package/src/push/bundler.ts +112 -0
  192. package/src/push/index.ts +282 -0
  193. package/src/push/kibana_api.ts +191 -0
  194. package/src/push/monitor.ts +276 -0
  195. package/src/push/plugin.ts +66 -0
  196. package/src/push/request.ts +141 -0
  197. package/src/push/utils.ts +77 -0
  198. package/src/reporters/base.ts +63 -52
  199. package/src/reporters/index.ts +26 -3
  200. package/src/reporters/json.ts +183 -202
  201. package/src/reporters/junit.ts +98 -93
  202. package/src/sdk/lh-trace-processor.ts +132 -68
  203. package/src/sdk/trace-metrics.ts +8 -4
  204. package/src/sdk/trace-processor.ts +28 -13
  205. package/templates/.github/workflows/run-synthetics.yml +27 -0
  206. package/templates/README.md +21 -0
  207. package/templates/journeys/advanced-example-helpers.ts +54 -0
  208. package/templates/journeys/advanced-example.journey.ts +43 -0
  209. package/templates/journeys/example.journey.ts +18 -0
  210. package/templates/lightweight/heartbeat.yml +8 -0
  211. package/templates/synthetics.config.ts +35 -0
  212. package/CHANGELOG.md +0 -131
  213. package/NOTICE.txt +0 -221
  214. package/dist/parse_args.d.ts.map +0 -1
  215. package/dist/parse_args.js +0 -95
  216. package/dist/parse_args.js.map +0 -1
  217. package/dist/reporters/reporter.d.ts.map +0 -1
  218. package/dist/reporters/reporter.js.map +0 -1
  219. package/dist/tsconfig.tsbuildinfo +0 -1888
  220. package/src/parse_args.ts +0 -135
package/src/loader.ts ADDED
@@ -0,0 +1,167 @@
1
+ /**
2
+ * MIT License
3
+ *
4
+ * Copyright (c) 2020-present, Elastic NV
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in
14
+ * all copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
+ * THE SOFTWARE.
23
+ *
24
+ */
25
+
26
+ import { stdin, cwd } from 'process';
27
+ import { resolve } from 'path';
28
+ import { CliArgs } from './common_types';
29
+ import { step, journey } from './core';
30
+ import { log } from './core/logger';
31
+ import { expect } from './core/expect';
32
+ import {
33
+ isDepInstalled,
34
+ isDirectory,
35
+ totalist,
36
+ findPkgJsonByTraversing,
37
+ } from './helpers';
38
+ import { installTransform } from './core/transform';
39
+
40
+ const resolvedCwd = cwd();
41
+
42
+ /**
43
+ * Perform global setup process required for running the test suites
44
+ * and also for bundling the monitors. The process includes
45
+ * - Transpiling the TS/JS test files
46
+ * - Loading these files for running test suites
47
+ */
48
+ export async function globalSetup(options: CliArgs, args: string[]) {
49
+ const revert = installTransform();
50
+ await loadTestFiles(options, args);
51
+ return () => {
52
+ revert();
53
+ };
54
+ }
55
+
56
+ export async function loadTestFiles(options: CliArgs, args: string[]) {
57
+ /**
58
+ * Preload modules before running the tests
59
+ */
60
+ const modules = [].concat(options.require || []).filter(Boolean);
61
+ for (const name of modules) {
62
+ if (isDepInstalled(name)) {
63
+ require(name);
64
+ } else {
65
+ throw new Error(`cannot find module '${name}'`);
66
+ }
67
+ }
68
+
69
+ if (options.inline) {
70
+ const source = await readStdin();
71
+ loadInlineScript(source);
72
+ return;
73
+ }
74
+ /**
75
+ * Handle piped files by reading the STDIN
76
+ * ex: ls example/suites/*.js | npx @elastic/synthetics
77
+ */
78
+ const files =
79
+ args.length > 0 ? args : (await readStdin()).split('\n').filter(Boolean);
80
+ const suites = await prepareSuites(files, options.pattern);
81
+ requireSuites(suites);
82
+ }
83
+
84
+ const loadInlineScript = source => {
85
+ const scriptFn = new Function(
86
+ 'step',
87
+ 'page',
88
+ 'context',
89
+ 'browser',
90
+ 'params',
91
+ 'expect',
92
+ 'request',
93
+ source
94
+ );
95
+ journey('inline', ({ page, context, browser, params, request }) => {
96
+ scriptFn.apply(null, [
97
+ step,
98
+ page,
99
+ context,
100
+ browser,
101
+ params,
102
+ expect,
103
+ request,
104
+ ]);
105
+ });
106
+ };
107
+
108
+ /**
109
+ * Read the input from STDIN and run it as inline journey
110
+ */
111
+ async function readStdin() {
112
+ const chunks = [];
113
+ stdin.resume();
114
+ stdin.setEncoding('utf-8');
115
+ for await (const chunk of stdin) {
116
+ chunks.push(chunk);
117
+ }
118
+ return chunks.join();
119
+ }
120
+
121
+ function requireSuites(suites: Iterable<string>) {
122
+ for (const suite of suites) {
123
+ require(suite);
124
+ }
125
+ }
126
+
127
+ /**
128
+ * Handle both directory and files that are passed through TTY
129
+ * and add them to suites
130
+ */
131
+ async function prepareSuites(inputs: string[], filePattern?: string) {
132
+ const suites = new Set<string>();
133
+ const addSuite = absPath => {
134
+ log(`Processing file: ${absPath}`);
135
+ suites.add(require.resolve(absPath));
136
+ };
137
+ /**
138
+ * Match all files inside the directory with the
139
+ * .journey.{mjs|cjs|js|ts) extensions
140
+ */
141
+ const pattern = filePattern
142
+ ? new RegExp(filePattern, 'i')
143
+ : /.+\.journey\.([mc]js|[jt]s?)$/;
144
+ /**
145
+ * Ignore node_modules by default when running suites
146
+ */
147
+ const ignored = /node_modules/i;
148
+
149
+ for (const input of inputs) {
150
+ const absPath = resolve(resolvedCwd, input);
151
+ /**
152
+ * Validate for package.json file before running
153
+ * the suites
154
+ */
155
+ findPkgJsonByTraversing(absPath, resolvedCwd);
156
+ if (isDirectory(absPath)) {
157
+ await totalist(absPath, (rel, abs) => {
158
+ if (pattern.test(rel) && !ignored.test(rel)) {
159
+ addSuite(abs);
160
+ }
161
+ });
162
+ } else {
163
+ addSuite(absPath);
164
+ }
165
+ }
166
+ return suites.values();
167
+ }
@@ -0,0 +1,95 @@
1
+ /**
2
+ * MIT License
3
+ *
4
+ * Copyright (c) 2020-present, Elastic NV
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in
14
+ * all copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
+ * THE SOFTWARE.
23
+ *
24
+ */
25
+
26
+ import { sendReqAndHandleError } from '../push/request';
27
+ import { indent, write } from '../helpers';
28
+ import { generateURL } from '../push/utils';
29
+
30
+ export type LocationCmdOptions = {
31
+ auth: string;
32
+ url: string;
33
+ };
34
+
35
+ type LocationMetadata = {
36
+ id: string;
37
+ label: string;
38
+ isServiceManaged: boolean;
39
+ };
40
+
41
+ export type LocationAPIResponse = {
42
+ locations: Array<LocationMetadata>;
43
+ };
44
+
45
+ const PRIVATE_KEYWORD = '(private)';
46
+
47
+ export async function getLocations(options: LocationCmdOptions) {
48
+ const resp = await sendReqAndHandleError<LocationAPIResponse>({
49
+ url: generateURL(options, 'location'),
50
+ method: 'GET',
51
+ auth: options.auth,
52
+ });
53
+ return resp.locations;
54
+ }
55
+
56
+ export function formatLocations(locations: Array<LocationMetadata>) {
57
+ const formatted: Array<string> = [];
58
+ for (const location of locations) {
59
+ let name = location.label;
60
+ if (location.isServiceManaged) {
61
+ [, name] = name.includes('-') ? name.split('-') : [, name];
62
+ name = name.toLowerCase().trim().split(' ').join('_');
63
+ } else {
64
+ name = `${name}${PRIVATE_KEYWORD}`;
65
+ }
66
+ formatted.push(name);
67
+ }
68
+ return formatted;
69
+ }
70
+
71
+ export function groupLocations(locations: Array<string>) {
72
+ const grouped = { locations: [], privateLocations: [] };
73
+ for (const loc of locations) {
74
+ if (loc.includes(PRIVATE_KEYWORD)) {
75
+ grouped.privateLocations.push(loc.replace(PRIVATE_KEYWORD, ''));
76
+ } else {
77
+ grouped.locations.push(loc);
78
+ }
79
+ }
80
+ return grouped;
81
+ }
82
+
83
+ export function renderLocations(locations: Array<string>) {
84
+ let outer = 'Available locations: \n';
85
+ let inner = '';
86
+ for (const location of locations) {
87
+ inner += `* ${location}\n`;
88
+ }
89
+ outer += indent(inner);
90
+ outer += `\nSet default location for monitors via
91
+ - Synthetics config file 'monitors.locations' | 'monitors.privateLocations' field
92
+ - Monitor API 'monitor.use({ locations: ["japan"], privateLocations: ["custom-location"] }')`;
93
+
94
+ write(outer);
95
+ }
@@ -23,13 +23,16 @@
23
23
  *
24
24
  */
25
25
 
26
- import Runner from '../core/runner';
27
-
28
- export type ReporterOptions = {
29
- fd?: number;
30
- colors?: boolean;
26
+ // DO NOT EDIT - UPDATE WITH `npm run build:locations`
27
+ export const LocationsMap = {
28
+ japan: 'asia-northeast1-a',
29
+ india: 'asia-south1-a',
30
+ singapore: 'asia-southeast1-a',
31
+ australia_east: 'australia-southeast1-a',
32
+ united_kingdom: 'europe-west2-a',
33
+ germany: 'europe-west3-a',
34
+ canada_east: 'northamerica-northeast1-a',
35
+ brazil: 'southamerica-east1-a',
36
+ us_east: 'us-east4-a',
37
+ us_west: 'us-west1-a',
31
38
  };
32
-
33
- export default interface Reporter {
34
- new (runner: Runner, options: ReporterOptions): any;
35
- }
package/src/options.ts ADDED
@@ -0,0 +1,227 @@
1
+ /**
2
+ * MIT License
3
+ *
4
+ * Copyright (c) 2020-present, Elastic NV
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in
14
+ * all copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
+ * THE SOFTWARE.
23
+ *
24
+ */
25
+
26
+ import merge from 'deepmerge';
27
+ import { createOption } from 'commander';
28
+ import { readConfig } from './config';
29
+ import { getNetworkConditions, DEFAULT_THROTTLING_OPTIONS } from './helpers';
30
+ import type { CliArgs, RunOptions, ThrottlingOptions } from './common_types';
31
+
32
+ /**
33
+ * Set debug based on DEBUG ENV and -d flags
34
+ * namespace - synthetics
35
+ */
36
+ const namespace = 'synthetics';
37
+ if (process.env.DEBUG && process.env.DEBUG.includes(namespace)) {
38
+ process.env.DEBUG = '1';
39
+ }
40
+
41
+ export function normalizeOptions(cliArgs: CliArgs): RunOptions {
42
+ const options: RunOptions = {
43
+ ...cliArgs,
44
+ environment: process.env['NODE_ENV'] || 'development',
45
+ };
46
+ /**
47
+ * Group all events that can be consumed by heartbeat and
48
+ * eventually by the Synthetics UI.
49
+ */
50
+ if (cliArgs.richEvents) {
51
+ options.reporter = cliArgs.reporter ?? 'json';
52
+ options.ssblocks = true;
53
+ options.network = true;
54
+ options.trace = true;
55
+ options.quietExitCode = true;
56
+ }
57
+
58
+ if (cliArgs.capability) {
59
+ const supportedCapabilities = [
60
+ 'trace',
61
+ 'network',
62
+ 'filmstrips',
63
+ 'metrics',
64
+ 'ssblocks',
65
+ ];
66
+ /**
67
+ * trace - record chrome trace events(LCP, FCP, CLS, etc.) for all journeys
68
+ * network - capture network information for all journeys
69
+ * filmstrips - record detailed filmstrips for all journeys
70
+ * metrics - capture performance metrics (DOM Nodes, Heap size, etc.) for each step
71
+ * ssblocks - Dedupes the screenshots in to blocks to save storage space
72
+ */
73
+ for (const flag of cliArgs.capability) {
74
+ if (supportedCapabilities.includes(flag)) {
75
+ options[flag] = true;
76
+ } else {
77
+ console.warn(
78
+ `Missing capability "${flag}", current supported capabilities are ${supportedCapabilities.join(
79
+ ', '
80
+ )}`
81
+ );
82
+ }
83
+ }
84
+ }
85
+
86
+ /**
87
+ * Validate and read synthetics config file
88
+ * based on the environment
89
+ */
90
+ const config =
91
+ cliArgs.config || !cliArgs.inline
92
+ ? readConfig(options.environment, cliArgs.config)
93
+ : {};
94
+
95
+ /**
96
+ * Order of preference for options that are used while running are
97
+ * 1. Local options configured via Runner API
98
+ * 2. CLI flags
99
+ * 3. Configuration file
100
+ */
101
+ options.params = Object.freeze(merge(config.params, cliArgs.params || {}));
102
+
103
+ /**
104
+ * Merge playwright options from CLI and Synthetics config
105
+ * and prefer individual options over other option
106
+ */
107
+ const playwrightOpts = merge(
108
+ config.playwrightOptions,
109
+ cliArgs.playwrightOptions || {}
110
+ );
111
+ options.playwrightOptions = {
112
+ ...playwrightOpts,
113
+ headless: cliArgs.headless,
114
+ chromiumSandbox: cliArgs.sandbox ?? playwrightOpts?.chromiumSandbox,
115
+ ignoreHTTPSErrors:
116
+ cliArgs.ignoreHttpsErrors ?? playwrightOpts?.ignoreHTTPSErrors,
117
+ };
118
+
119
+ /**
120
+ * Get the default monitor config from synthetics.config.ts file
121
+ */
122
+ const monitor = config.monitor;
123
+ if (cliArgs.throttling) {
124
+ const throttleConfig = merge.all([
125
+ DEFAULT_THROTTLING_OPTIONS,
126
+ toObject(monitor?.throttling),
127
+ toObject(cliArgs.throttling),
128
+ ]);
129
+ if (monitor?.throttling !== false) {
130
+ options.throttling = throttleConfig;
131
+ options.networkConditions = getNetworkConditions(throttleConfig);
132
+ } else {
133
+ /**
134
+ * If the throttling is disabled via Project monitor config, use it a source
135
+ * of truth and disable it for pushing those monitors.
136
+ */
137
+ options.throttling = false;
138
+ }
139
+ } else {
140
+ /**
141
+ * Do not apply throttling when `--no-throttling` flag is passed
142
+ */
143
+ options.throttling = false;
144
+ }
145
+
146
+ options.schedule = cliArgs.schedule ?? monitor?.schedule;
147
+ options.locations = cliArgs.locations ?? monitor?.locations;
148
+ options.privateLocations =
149
+ cliArgs.privateLocations ?? monitor?.privateLocations;
150
+
151
+ return options;
152
+ }
153
+
154
+ function toObject(value: boolean | Record<string, any>): Record<string, any> {
155
+ const defaulVal = {};
156
+ if (typeof value === 'boolean') {
157
+ return defaulVal;
158
+ }
159
+ return value || defaulVal;
160
+ }
161
+
162
+ /**
163
+ * Parses the throttling CLI settings and also
164
+ * adapts to the format to keep the backwards compatability
165
+ * - Accepts old format `<5d/3u/20l>`
166
+ * - Processess new format otherwise `{download: 5, upload: 3, latency: 20}`
167
+ */
168
+ export function parseThrottling(value: string, prev?: string) {
169
+ const THROTTLING_REGEX = /([0-9]{1,}u)|([0-9]{1,}d)|([0-9]{1,}l)/gm;
170
+ if (THROTTLING_REGEX.test(value)) {
171
+ const throttling: ThrottlingOptions = {};
172
+ const conditions = value.split('/');
173
+
174
+ conditions.forEach(condition => {
175
+ const setting = condition.slice(0, condition.length - 1);
176
+ const token = condition.slice(-1);
177
+ switch (token) {
178
+ case 'd':
179
+ throttling.download = Number(setting);
180
+ break;
181
+ case 'u':
182
+ throttling.upload = Number(setting);
183
+ break;
184
+ case 'l':
185
+ throttling.latency = Number(setting);
186
+ break;
187
+ }
188
+ });
189
+ return throttling;
190
+ }
191
+ return JSON.parse(value || prev);
192
+ }
193
+
194
+ export function getCommonCommandOpts() {
195
+ const params = createOption(
196
+ '-p, --params <jsonstring>',
197
+ 'JSON object that gets injected to all journeys'
198
+ );
199
+ params.argParser(JSON.parse);
200
+
201
+ const playwrightOpts = createOption(
202
+ '--playwright-options <jsonstring>',
203
+ 'JSON object to pass in custom Playwright options for the agent. Options passed will be merged with Playwright options defined in your synthetics.config.js file.'
204
+ );
205
+ playwrightOpts.argParser(JSON.parse);
206
+
207
+ const pattern = createOption(
208
+ '--pattern <pattern>',
209
+ 'RegExp pattern to look for journey files that are different from the default (ex: /*.journey.(ts|js)$/)'
210
+ );
211
+ const tags = createOption(
212
+ '--tags <name...>',
213
+ 'run only journeys with a tag that matches the glob'
214
+ );
215
+ const match = createOption(
216
+ '--match <name>',
217
+ 'run only journeys with a name or tag that matches the glob'
218
+ );
219
+
220
+ return {
221
+ params,
222
+ playwrightOpts,
223
+ pattern,
224
+ tags,
225
+ match,
226
+ };
227
+ }
@@ -23,8 +23,7 @@
23
23
  *
24
24
  */
25
25
 
26
- import { Page } from 'playwright-chromium';
27
- import { BrowserMessage } from '../common_types';
26
+ import { BrowserMessage, Driver } from '../common_types';
28
27
  import { Step } from '../dsl';
29
28
  import { getTimestamp } from '../helpers';
30
29
 
@@ -34,6 +33,8 @@ export class BrowserConsole {
34
33
  private messages: BrowserMessage[] = [];
35
34
  _currentStep: Partial<Step> = null;
36
35
 
36
+ constructor(private driver: Driver) {}
37
+
37
38
  private consoleEventListener = msg => {
38
39
  if (!this._currentStep) {
39
40
  return;
@@ -47,20 +48,41 @@ export class BrowserConsole {
47
48
  type,
48
49
  step: { name, index },
49
50
  });
50
- if (this.messages.length > defaultMessageLimit) {
51
- this.messages.splice(0, 1);
52
- }
51
+
52
+ this.enforceMessagesLimit();
53
+ }
54
+ };
55
+
56
+ private pageErrorEventListener = (error: Error) => {
57
+ if (!this._currentStep) {
58
+ return;
53
59
  }
60
+ const { name, index } = this._currentStep;
61
+ this.messages.push({
62
+ timestamp: getTimestamp(),
63
+ text: error.message,
64
+ type: 'error',
65
+ step: { name, index },
66
+ error,
67
+ });
68
+
69
+ this.enforceMessagesLimit();
54
70
  };
55
71
 
56
- constructor(private page: Page) {}
72
+ private enforceMessagesLimit() {
73
+ if (this.messages.length > defaultMessageLimit) {
74
+ this.messages.splice(0, 1);
75
+ }
76
+ }
57
77
 
58
78
  start() {
59
- this.page.on('console', this.consoleEventListener);
79
+ this.driver.page.on('console', this.consoleEventListener);
80
+ this.driver.page.on('pageerror', this.pageErrorEventListener);
60
81
  }
61
82
 
62
83
  stop() {
63
- this.page.off('console', this.consoleEventListener);
84
+ this.driver.page.off('console', this.consoleEventListener);
85
+ this.driver.page.off('pageerror', this.pageErrorEventListener);
64
86
  return this.messages;
65
87
  }
66
88
  }