@elastic/synthetics 1.0.0-beta.8 → 1.0.0
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.
- package/README.md +5 -4
- package/dist/cli.js +139 -165
- package/dist/cli.js.map +1 -1
- package/dist/common_types.d.ts +158 -34
- package/dist/common_types.d.ts.map +1 -1
- package/dist/config.d.ts +1 -0
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +7 -6
- package/dist/config.js.map +1 -1
- package/dist/core/browser-service.d.ts +1 -1
- package/dist/core/browser-service.d.ts.map +1 -1
- package/dist/core/browser-service.js +7 -6
- package/dist/core/browser-service.js.map +1 -1
- package/dist/core/expect.d.ts +25 -25
- package/dist/core/expect.d.ts.map +1 -1
- package/dist/core/expect.js.map +1 -1
- package/dist/core/gatherer.d.ts +5 -8
- package/dist/core/gatherer.d.ts.map +1 -1
- package/dist/core/gatherer.js +75 -17
- package/dist/core/gatherer.js.map +1 -1
- package/dist/core/index.d.ts +5 -1
- package/dist/core/index.d.ts.map +1 -1
- package/dist/core/index.js +23 -11
- package/dist/core/index.js.map +1 -1
- package/dist/core/logger.d.ts +0 -1
- package/dist/core/logger.d.ts.map +1 -1
- package/dist/core/logger.js +10 -17
- package/dist/core/logger.js.map +1 -1
- package/dist/core/runner.d.ts +12 -63
- package/dist/core/runner.d.ts.map +1 -1
- package/dist/core/runner.js +159 -92
- package/dist/core/runner.js.map +1 -1
- package/dist/core/transform.d.ts +37 -0
- package/dist/core/transform.d.ts.map +1 -0
- package/dist/core/transform.js +148 -0
- package/dist/core/transform.js.map +1 -0
- package/dist/dsl/index.js +5 -1
- package/dist/dsl/index.js.map +1 -1
- package/dist/dsl/journey.d.ts +9 -7
- package/dist/dsl/journey.d.ts.map +1 -1
- package/dist/dsl/journey.js +32 -39
- package/dist/dsl/journey.js.map +1 -1
- package/dist/dsl/monitor.d.ts +93 -0
- package/dist/dsl/monitor.d.ts.map +1 -0
- package/dist/dsl/monitor.js +110 -0
- package/dist/dsl/monitor.js.map +1 -0
- package/dist/dsl/step.d.ts +3 -2
- package/dist/dsl/step.d.ts.map +1 -1
- package/dist/dsl/step.js +6 -1
- package/dist/dsl/step.js.map +1 -1
- package/dist/formatter/javascript.d.ts +115 -0
- package/dist/formatter/javascript.d.ts.map +1 -0
- package/dist/formatter/javascript.js +331 -0
- package/dist/formatter/javascript.js.map +1 -0
- package/dist/generator/index.d.ts +24 -0
- package/dist/generator/index.d.ts.map +1 -0
- package/dist/generator/index.js +241 -0
- package/dist/generator/index.js.map +1 -0
- package/dist/{reporters/reporter.js → generator/utils.d.ts} +5 -3
- package/dist/generator/utils.d.ts.map +1 -0
- package/dist/generator/utils.js +84 -0
- package/dist/generator/utils.js.map +1 -0
- package/dist/helpers.d.ts +36 -9
- package/dist/helpers.d.ts.map +1 -1
- package/dist/helpers.js +170 -24
- package/dist/helpers.js.map +1 -1
- package/dist/index.d.ts +5 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -26
- package/dist/index.js.map +1 -1
- package/dist/loader.d.ts +34 -0
- package/dist/loader.d.ts.map +1 -0
- package/dist/loader.js +153 -0
- package/dist/loader.js.map +1 -0
- package/dist/locations/index.d.ts +45 -0
- package/dist/locations/index.d.ts.map +1 -0
- package/dist/locations/index.js +83 -0
- package/dist/locations/index.js.map +1 -0
- package/dist/{reporters/reporter.d.ts → locations/public-locations.d.ts} +12 -8
- package/dist/locations/public-locations.d.ts.map +1 -0
- package/dist/locations/public-locations.js +41 -0
- package/dist/locations/public-locations.js.map +1 -0
- package/dist/options.d.ts +42 -0
- package/dist/options.d.ts.map +1 -0
- package/dist/options.js +146 -0
- package/dist/options.js.map +1 -0
- package/dist/plugins/browser-console.d.ts +5 -4
- package/dist/plugins/browser-console.d.ts.map +1 -1
- package/dist/plugins/browser-console.js +47 -24
- package/dist/plugins/browser-console.js.map +1 -1
- package/dist/plugins/index.js +5 -1
- package/dist/plugins/index.js.map +1 -1
- package/dist/plugins/network.d.ts +24 -31
- package/dist/plugins/network.d.ts.map +1 -1
- package/dist/plugins/network.js +226 -229
- package/dist/plugins/network.js.map +1 -1
- package/dist/plugins/performance.d.ts +4 -19
- package/dist/plugins/performance.d.ts.map +1 -1
- package/dist/plugins/performance.js +6 -5
- package/dist/plugins/performance.js.map +1 -1
- package/dist/plugins/plugin-manager.d.ts +9 -5
- package/dist/plugins/plugin-manager.d.ts.map +1 -1
- package/dist/plugins/plugin-manager.js +46 -30
- package/dist/plugins/plugin-manager.js.map +1 -1
- package/dist/plugins/tracing.d.ts +6 -6
- package/dist/plugins/tracing.d.ts.map +1 -1
- package/dist/plugins/tracing.js +13 -7
- package/dist/plugins/tracing.js.map +1 -1
- package/dist/push/bundler.d.ts +35 -0
- package/dist/push/bundler.d.ts.map +1 -0
- package/dist/push/bundler.js +131 -0
- package/dist/push/bundler.js.map +1 -0
- package/dist/push/index.d.ts +10 -0
- package/dist/push/index.d.ts.map +1 -0
- package/dist/push/index.js +235 -0
- package/dist/push/index.js.map +1 -0
- package/dist/push/kibana_api.d.ts +56 -0
- package/dist/push/kibana_api.d.ts.map +1 -0
- package/dist/push/kibana_api.js +135 -0
- package/dist/push/kibana_api.js.map +1 -0
- package/dist/push/monitor.d.ts +55 -0
- package/dist/push/monitor.d.ts.map +1 -0
- package/dist/push/monitor.js +268 -0
- package/dist/push/monitor.js.map +1 -0
- package/dist/{parse_args.d.ts → push/plugin.d.ts} +9 -6
- package/dist/push/plugin.d.ts.map +1 -0
- package/dist/push/plugin.js +66 -0
- package/dist/push/plugin.js.map +1 -0
- package/dist/push/request.d.ts +45 -0
- package/dist/push/request.d.ts.map +1 -0
- package/dist/push/request.js +109 -0
- package/dist/push/request.js.map +1 -0
- package/dist/push/utils.d.ts +31 -0
- package/dist/push/utils.d.ts.map +1 -0
- package/dist/push/utils.js +64 -0
- package/dist/push/utils.js.map +1 -0
- package/dist/reporters/base.d.ts +14 -6
- package/dist/reporters/base.d.ts.map +1 -1
- package/dist/reporters/base.js +58 -50
- package/dist/reporters/base.js.map +1 -1
- package/dist/reporters/index.d.ts +18 -8
- package/dist/reporters/index.d.ts.map +1 -1
- package/dist/reporters/index.js.map +1 -1
- package/dist/reporters/json.d.ts +17 -62
- package/dist/reporters/json.d.ts.map +1 -1
- package/dist/reporters/json.js +147 -177
- package/dist/reporters/json.js.map +1 -1
- package/dist/reporters/junit.d.ts +7 -1
- package/dist/reporters/junit.d.ts.map +1 -1
- package/dist/reporters/junit.js +90 -95
- package/dist/reporters/junit.js.map +1 -1
- package/dist/sdk/lh-trace-processor.d.ts +35 -9
- package/dist/sdk/lh-trace-processor.d.ts.map +1 -1
- package/dist/sdk/lh-trace-processor.js +114 -53
- package/dist/sdk/lh-trace-processor.js.map +1 -1
- package/dist/sdk/trace-metrics.d.ts +4 -4
- package/dist/sdk/trace-metrics.d.ts.map +1 -1
- package/dist/sdk/trace-metrics.js +3 -4
- package/dist/sdk/trace-metrics.js.map +1 -1
- package/dist/sdk/trace-processor.d.ts +11 -3
- package/dist/sdk/trace-processor.d.ts.map +1 -1
- package/dist/sdk/trace-processor.js +17 -9
- package/dist/sdk/trace-processor.js.map +1 -1
- package/package.json +38 -30
- package/src/cli.ts +223 -158
- package/src/common_types.ts +191 -34
- package/src/config.ts +1 -1
- package/src/core/expect.ts +58 -24
- package/src/core/gatherer.ts +92 -25
- package/src/core/index.ts +36 -15
- package/src/core/logger.ts +8 -12
- package/src/core/runner.ts +158 -148
- package/src/core/transform.ts +160 -0
- package/src/dsl/journey.ts +41 -21
- package/src/dsl/monitor.ts +152 -0
- package/src/dsl/step.ts +9 -2
- package/src/formatter/javascript.ts +414 -0
- package/src/generator/index.ts +308 -0
- package/src/generator/utils.ts +84 -0
- package/src/helpers.ts +182 -16
- package/src/index.ts +21 -22
- package/src/loader.ts +171 -0
- package/src/locations/index.ts +95 -0
- package/src/{reporters/reporter.ts → locations/public-locations.ts} +12 -9
- package/src/options.ts +175 -0
- package/src/plugins/browser-console.ts +33 -8
- package/src/plugins/network.ts +266 -284
- package/src/plugins/performance.ts +6 -22
- package/src/plugins/plugin-manager.ts +49 -31
- package/src/plugins/tracing.ts +11 -9
- package/src/push/bundler.ts +112 -0
- package/src/push/index.ts +291 -0
- package/src/push/kibana_api.ts +191 -0
- package/src/push/monitor.ts +307 -0
- package/src/push/plugin.ts +66 -0
- package/src/push/request.ts +141 -0
- package/src/push/utils.ts +77 -0
- package/src/reporters/base.ts +63 -52
- package/src/reporters/index.ts +26 -3
- package/src/reporters/json.ts +181 -201
- package/src/reporters/junit.ts +98 -93
- package/src/sdk/lh-trace-processor.ts +132 -68
- package/src/sdk/trace-metrics.ts +8 -4
- package/src/sdk/trace-processor.ts +28 -13
- package/templates/.github/workflows/run-synthetics.yml +27 -0
- package/templates/README.md +21 -0
- package/templates/journeys/advanced-example-helpers.ts +54 -0
- package/templates/journeys/advanced-example.journey.ts +43 -0
- package/templates/journeys/example.journey.ts +18 -0
- package/templates/lightweight/heartbeat.yml +9 -0
- package/templates/synthetics.config.ts +35 -0
- package/CHANGELOG.md +0 -181
- package/NOTICE.txt +0 -221
- package/dist/parse_args.d.ts.map +0 -1
- package/dist/parse_args.js +0 -98
- package/dist/parse_args.js.map +0 -1
- package/dist/reporters/reporter.d.ts.map +0 -1
- package/dist/reporters/reporter.js.map +0 -1
- package/dist/tsconfig.tsbuildinfo +0 -1895
- package/src/parse_args.ts +0 -146
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
*
|
|
24
24
|
*/
|
|
25
25
|
|
|
26
|
-
import { PluginOutput } from '../common_types';
|
|
26
|
+
import { PluginOutput, Driver } from '../common_types';
|
|
27
27
|
import {
|
|
28
28
|
BrowserConsole,
|
|
29
29
|
NetworkManager,
|
|
@@ -31,7 +31,6 @@ import {
|
|
|
31
31
|
Tracing,
|
|
32
32
|
TraceOptions,
|
|
33
33
|
} from './';
|
|
34
|
-
import { Driver } from '../core/gatherer';
|
|
35
34
|
import { Step } from '../dsl';
|
|
36
35
|
|
|
37
36
|
type PluginType = 'network' | 'trace' | 'performance' | 'browserconsole';
|
|
@@ -39,59 +38,78 @@ type Plugin = NetworkManager | Tracing | PerformanceManager | BrowserConsole;
|
|
|
39
38
|
type PluginOptions = TraceOptions;
|
|
40
39
|
|
|
41
40
|
export class PluginManager {
|
|
42
|
-
protected plugins = new Map<
|
|
43
|
-
|
|
41
|
+
protected plugins = new Map<PluginType, Plugin>();
|
|
42
|
+
public PLUGIN_TYPES: Array<PluginType> = [
|
|
43
|
+
'network',
|
|
44
|
+
'trace',
|
|
45
|
+
'performance',
|
|
46
|
+
'browserconsole',
|
|
47
|
+
];
|
|
44
48
|
constructor(private driver: Driver) {}
|
|
45
49
|
|
|
46
|
-
|
|
50
|
+
register(type: PluginType, options: PluginOptions) {
|
|
47
51
|
let instance: Plugin;
|
|
48
52
|
switch (type) {
|
|
49
53
|
case 'network':
|
|
50
|
-
instance = new NetworkManager();
|
|
51
|
-
await instance.start(this.driver.client);
|
|
54
|
+
instance = new NetworkManager(this.driver);
|
|
52
55
|
break;
|
|
53
56
|
case 'trace':
|
|
54
|
-
instance = new Tracing(options);
|
|
55
|
-
await instance.start(this.driver.client);
|
|
57
|
+
instance = new Tracing(this.driver, options);
|
|
56
58
|
break;
|
|
57
59
|
case 'performance':
|
|
58
|
-
instance = new PerformanceManager(this.driver
|
|
59
|
-
instance.start();
|
|
60
|
+
instance = new PerformanceManager(this.driver);
|
|
60
61
|
break;
|
|
61
62
|
case 'browserconsole':
|
|
62
|
-
instance = new BrowserConsole(this.driver
|
|
63
|
-
instance.start();
|
|
63
|
+
instance = new BrowserConsole(this.driver);
|
|
64
64
|
break;
|
|
65
65
|
}
|
|
66
|
+
instance && this.plugins.set(type, instance);
|
|
67
|
+
return instance;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
registerAll(options: PluginOptions) {
|
|
71
|
+
for (const type of this.PLUGIN_TYPES) {
|
|
72
|
+
this.register(type, options);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
unregisterAll() {
|
|
77
|
+
for (const type of this.PLUGIN_TYPES) {
|
|
78
|
+
this.plugins.delete(type);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
async stop(type: PluginType) {
|
|
83
|
+
const instance = this.plugins.get(type);
|
|
84
|
+
if (instance) {
|
|
85
|
+
return await instance.stop();
|
|
86
|
+
}
|
|
87
|
+
return {};
|
|
88
|
+
}
|
|
66
89
|
|
|
67
|
-
|
|
90
|
+
async start(type: PluginType) {
|
|
91
|
+
const instance = this.plugins.get(type);
|
|
92
|
+
instance && (await instance.start());
|
|
68
93
|
return instance;
|
|
69
94
|
}
|
|
70
95
|
|
|
71
|
-
get
|
|
72
|
-
return this.plugins.get(
|
|
96
|
+
get(type: PluginType) {
|
|
97
|
+
return this.plugins.get(type);
|
|
73
98
|
}
|
|
74
99
|
|
|
75
100
|
onStep(step: Step) {
|
|
76
|
-
this.get(
|
|
77
|
-
this.get(
|
|
101
|
+
(this.get('browserconsole') as BrowserConsole)._currentStep = step;
|
|
102
|
+
(this.get('network') as NetworkManager)._currentStep = step;
|
|
78
103
|
}
|
|
79
104
|
|
|
80
|
-
async output()
|
|
105
|
+
async output() {
|
|
81
106
|
const data: PluginOutput = {};
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
const result = await plugin.stop(this.driver.client);
|
|
88
|
-
Object.assign(data, { ...result });
|
|
89
|
-
} else if (plugin instanceof BrowserConsole) {
|
|
90
|
-
data.browserconsole = plugin.stop();
|
|
91
|
-
}
|
|
107
|
+
for (const [, plugin] of this.plugins) {
|
|
108
|
+
if (plugin instanceof NetworkManager) {
|
|
109
|
+
data.networkinfo = await plugin.stop();
|
|
110
|
+
} else if (plugin instanceof BrowserConsole) {
|
|
111
|
+
data.browserconsole = plugin.stop();
|
|
92
112
|
}
|
|
93
|
-
} catch (e) {
|
|
94
|
-
console.error('Error capturing data', e.message);
|
|
95
113
|
}
|
|
96
114
|
return data;
|
|
97
115
|
}
|
package/src/plugins/tracing.ts
CHANGED
|
@@ -23,8 +23,8 @@
|
|
|
23
23
|
*
|
|
24
24
|
*/
|
|
25
25
|
|
|
26
|
-
import {
|
|
27
|
-
import {
|
|
26
|
+
import { Driver, PluginOutput } from '../common_types';
|
|
27
|
+
import { log } from '../core/logger';
|
|
28
28
|
import { Filmstrips } from '../sdk/trace-metrics';
|
|
29
29
|
import { TraceEvent, TraceProcessor } from '../sdk/trace-processor';
|
|
30
30
|
|
|
@@ -38,9 +38,10 @@ export type TraceOptions = {
|
|
|
38
38
|
* https://chromedevtools.github.io/devtools-protocol/tot/Tracing/
|
|
39
39
|
*/
|
|
40
40
|
export class Tracing {
|
|
41
|
-
constructor(
|
|
41
|
+
constructor(private driver: Driver, private options: TraceOptions) {}
|
|
42
42
|
|
|
43
|
-
async start(
|
|
43
|
+
async start() {
|
|
44
|
+
log(`Plugins: started collecting trace events`);
|
|
44
45
|
const includedCategories = [
|
|
45
46
|
// exclude all default categories
|
|
46
47
|
'-*',
|
|
@@ -63,21 +64,21 @@ export class Tracing {
|
|
|
63
64
|
'disabled-by-default-devtools.timeline'
|
|
64
65
|
);
|
|
65
66
|
}
|
|
66
|
-
await client.send('Tracing.start', {
|
|
67
|
+
await this.driver.client.send('Tracing.start', {
|
|
67
68
|
/**
|
|
68
69
|
* Using `ReportEvents` makes gathering trace events
|
|
69
70
|
* much faster as opposed to using `ReturnAsStream` mode
|
|
70
71
|
*/
|
|
71
72
|
transferMode: 'ReportEvents',
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
},
|
|
73
|
+
categories: includedCategories.join(','),
|
|
74
|
+
options: 'sampling-frequency=10000', // 1000 is default
|
|
75
75
|
});
|
|
76
76
|
}
|
|
77
77
|
|
|
78
|
-
async stop(
|
|
78
|
+
async stop() {
|
|
79
79
|
const events = [];
|
|
80
80
|
const collectListener = payload => events.push(...payload.value);
|
|
81
|
+
const { client } = this.driver;
|
|
81
82
|
client.on('Tracing.dataCollected', collectListener);
|
|
82
83
|
|
|
83
84
|
const [traceEvents] = await Promise.all([
|
|
@@ -98,6 +99,7 @@ export class Tracing {
|
|
|
98
99
|
...TraceProcessor.computeTrace(traceEvents as Array<TraceEvent>),
|
|
99
100
|
});
|
|
100
101
|
}
|
|
102
|
+
log(`Plugins: stopped collecting trace events`);
|
|
101
103
|
return output;
|
|
102
104
|
}
|
|
103
105
|
}
|
|
@@ -0,0 +1,112 @@
|
|
|
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 path from 'path';
|
|
27
|
+
import { stat, unlink, readFile } from 'fs/promises';
|
|
28
|
+
import { createWriteStream } from 'fs';
|
|
29
|
+
import * as esbuild from 'esbuild';
|
|
30
|
+
import archiver from 'archiver';
|
|
31
|
+
import { commonOptions } from '../core/transform';
|
|
32
|
+
import { SyntheticsBundlePlugin } from './plugin';
|
|
33
|
+
|
|
34
|
+
const SIZE_LIMIT_KB = 800;
|
|
35
|
+
|
|
36
|
+
function relativeToCwd(entry: string) {
|
|
37
|
+
return path.relative(process.cwd(), entry);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export class Bundler {
|
|
41
|
+
moduleMap = new Map<string, string>();
|
|
42
|
+
constructor() {}
|
|
43
|
+
|
|
44
|
+
async prepare(absPath: string) {
|
|
45
|
+
const options: esbuild.BuildOptions = {
|
|
46
|
+
...commonOptions(),
|
|
47
|
+
...{
|
|
48
|
+
entryPoints: [absPath],
|
|
49
|
+
bundle: true,
|
|
50
|
+
write: false,
|
|
51
|
+
sourcemap: 'inline',
|
|
52
|
+
external: ['@elastic/synthetics'],
|
|
53
|
+
plugins: [SyntheticsBundlePlugin()],
|
|
54
|
+
},
|
|
55
|
+
};
|
|
56
|
+
const result = await esbuild.build(options);
|
|
57
|
+
if (result.errors.length > 0) {
|
|
58
|
+
throw result.errors;
|
|
59
|
+
}
|
|
60
|
+
this.moduleMap.set(absPath, result.outputFiles[0].text);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
async zip(outputPath: string) {
|
|
64
|
+
return new Promise((fulfill, reject) => {
|
|
65
|
+
const output = createWriteStream(outputPath);
|
|
66
|
+
const archive = archiver('zip', {
|
|
67
|
+
zlib: { level: 9 },
|
|
68
|
+
});
|
|
69
|
+
archive.on('error', reject);
|
|
70
|
+
output.on('close', fulfill);
|
|
71
|
+
archive.pipe(output);
|
|
72
|
+
for (const [path, content] of this.moduleMap.entries()) {
|
|
73
|
+
const relativePath = relativeToCwd(path);
|
|
74
|
+
// Date is fixed to Unix epoch so the file metadata is
|
|
75
|
+
// not modified everytime when files are bundled
|
|
76
|
+
archive.append(content, {
|
|
77
|
+
name: relativePath,
|
|
78
|
+
date: new Date('1970-01-01'),
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
archive.finalize();
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
async build(entry: string, output: string) {
|
|
86
|
+
await this.prepare(entry);
|
|
87
|
+
await this.zip(output);
|
|
88
|
+
const data = await this.encode(output);
|
|
89
|
+
await this.checkSize(output);
|
|
90
|
+
await this.cleanup(output);
|
|
91
|
+
return data;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
async encode(outputPath: string) {
|
|
95
|
+
return await readFile(outputPath, 'base64');
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
async checkSize(outputPath: string) {
|
|
99
|
+
const { size } = await stat(outputPath);
|
|
100
|
+
const sizeKb = size / 1024;
|
|
101
|
+
if (sizeKb > SIZE_LIMIT_KB) {
|
|
102
|
+
throw new Error(
|
|
103
|
+
`You have monitors whose size exceeds the ${SIZE_LIMIT_KB}KB limit.`
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
async cleanup(outputPath: string) {
|
|
109
|
+
this.moduleMap = new Map<string, string>();
|
|
110
|
+
await unlink(outputPath);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
@@ -0,0 +1,291 @@
|
|
|
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
|
+
import semver from 'semver';
|
|
26
|
+
import { readFile, writeFile } from 'fs/promises';
|
|
27
|
+
import { prompt } from 'enquirer';
|
|
28
|
+
import { bold, grey } from 'kleur/colors';
|
|
29
|
+
import {
|
|
30
|
+
getLocalMonitors,
|
|
31
|
+
buildMonitorSchema,
|
|
32
|
+
diffMonitors as diffMonitorHashIDs,
|
|
33
|
+
MonitorSchema,
|
|
34
|
+
} from './monitor';
|
|
35
|
+
import { ALLOWED_SCHEDULES, Monitor } from '../dsl/monitor';
|
|
36
|
+
import {
|
|
37
|
+
progress,
|
|
38
|
+
liveProgress,
|
|
39
|
+
write,
|
|
40
|
+
error,
|
|
41
|
+
warn,
|
|
42
|
+
indent,
|
|
43
|
+
done,
|
|
44
|
+
getMonitorManagementURL,
|
|
45
|
+
THROTTLING_WARNING_MSG,
|
|
46
|
+
} from '../helpers';
|
|
47
|
+
import type { PushOptions, ProjectSettings } from '../common_types';
|
|
48
|
+
import { findSyntheticsConfig, readConfig } from '../config';
|
|
49
|
+
import {
|
|
50
|
+
bulkDeleteMonitors,
|
|
51
|
+
bulkGetMonitors,
|
|
52
|
+
bulkPutMonitors,
|
|
53
|
+
getVersion,
|
|
54
|
+
createMonitorsLegacy,
|
|
55
|
+
CHUNK_SIZE,
|
|
56
|
+
} from './kibana_api';
|
|
57
|
+
import { getChunks, logDiff } from './utils';
|
|
58
|
+
|
|
59
|
+
export async function push(monitors: Monitor[], options: PushOptions) {
|
|
60
|
+
const duplicates = trackDuplicates(monitors);
|
|
61
|
+
if (duplicates.size > 0) {
|
|
62
|
+
throw error(formatDuplicateError(duplicates));
|
|
63
|
+
}
|
|
64
|
+
progress(`Pushing monitors for project: ${options.id}`);
|
|
65
|
+
|
|
66
|
+
const stackVersion = await getVersion(options);
|
|
67
|
+
const isV2 = semver.satisfies(stackVersion, '>=8.6.0');
|
|
68
|
+
if (!isV2) {
|
|
69
|
+
return await pushLegacy(monitors, options, stackVersion);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const local = getLocalMonitors(monitors);
|
|
73
|
+
const { monitors: remote } = await bulkGetMonitors(options);
|
|
74
|
+
const { newIDs, changedIDs, removedIDs, unchangedIDs } = diffMonitorHashIDs(
|
|
75
|
+
local,
|
|
76
|
+
remote
|
|
77
|
+
);
|
|
78
|
+
logDiff(newIDs, changedIDs, removedIDs, unchangedIDs);
|
|
79
|
+
|
|
80
|
+
const updatedMonitors = new Set<string>([...changedIDs, ...newIDs]);
|
|
81
|
+
if (updatedMonitors.size > 0) {
|
|
82
|
+
const toBundle = monitors.filter(m => updatedMonitors.has(m.config.id));
|
|
83
|
+
progress(`bundling ${toBundle.length} monitors`);
|
|
84
|
+
const schemas = await buildMonitorSchema(toBundle, true);
|
|
85
|
+
const chunks = getChunks(schemas, CHUNK_SIZE);
|
|
86
|
+
for (const chunk of chunks) {
|
|
87
|
+
await liveProgress(
|
|
88
|
+
bulkPutMonitors(options, chunk),
|
|
89
|
+
`creating or updating ${chunk.length} monitors`
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
if (removedIDs.size > 0) {
|
|
95
|
+
if (updatedMonitors.size === 0 && unchangedIDs.size === 0) {
|
|
96
|
+
await promptConfirmDeleteAll(options);
|
|
97
|
+
}
|
|
98
|
+
const chunks = getChunks(Array.from(removedIDs), CHUNK_SIZE);
|
|
99
|
+
for (const chunk of chunks) {
|
|
100
|
+
await liveProgress(
|
|
101
|
+
bulkDeleteMonitors(options, chunk),
|
|
102
|
+
`deleting ${chunk.length} monitors`
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
done(`Pushed: ${grey(getMonitorManagementURL(options.url))}`);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
async function promptConfirmDeleteAll(options: PushOptions) {
|
|
111
|
+
write('');
|
|
112
|
+
const { deleteAll } = await prompt<{ deleteAll: boolean }>({
|
|
113
|
+
type: 'confirm',
|
|
114
|
+
skip() {
|
|
115
|
+
if (options.yes) {
|
|
116
|
+
this.initial = process.env.TEST_OVERRIDE ?? true;
|
|
117
|
+
return true;
|
|
118
|
+
}
|
|
119
|
+
return false;
|
|
120
|
+
},
|
|
121
|
+
name: 'deleteAll',
|
|
122
|
+
message: `Pushing without any monitors will delete all monitors associated with the project.\n Do you want to continue?`,
|
|
123
|
+
initial: false,
|
|
124
|
+
});
|
|
125
|
+
if (!deleteAll) {
|
|
126
|
+
throw warn('Push command Aborted');
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function trackDuplicates(monitors: Monitor[]) {
|
|
131
|
+
const monitorMap: Map<string, Monitor> = new Map();
|
|
132
|
+
const duplicates: Set<Monitor> = new Set();
|
|
133
|
+
for (const monitor of monitors) {
|
|
134
|
+
const id = monitor.config.id;
|
|
135
|
+
if (monitorMap.has(id)) {
|
|
136
|
+
duplicates.add(monitorMap.get(id));
|
|
137
|
+
duplicates.add(monitor);
|
|
138
|
+
}
|
|
139
|
+
monitorMap.set(id, monitor);
|
|
140
|
+
}
|
|
141
|
+
return duplicates;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export function formatDuplicateError(monitors: Set<Monitor>) {
|
|
145
|
+
let outer = bold(`Aborted: Duplicate monitors found\n`);
|
|
146
|
+
|
|
147
|
+
let inner = '';
|
|
148
|
+
for (const monitor of monitors) {
|
|
149
|
+
const { config, source } = monitor;
|
|
150
|
+
inner += `* ${config.id} - ${source.file}:${source.line}:${source.column}\n`;
|
|
151
|
+
}
|
|
152
|
+
outer += indent(inner);
|
|
153
|
+
return outer;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const INSTALLATION_HELP = `Run 'npx @elastic/synthetics init' to create project with default settings.`;
|
|
157
|
+
|
|
158
|
+
export async function loadSettings() {
|
|
159
|
+
try {
|
|
160
|
+
const config = await readConfig(process.env['NODE_ENV'] || 'development');
|
|
161
|
+
// Missing config file, fake throw to capture as missing file
|
|
162
|
+
if (Object.keys(config).length === 0) {
|
|
163
|
+
throw '';
|
|
164
|
+
}
|
|
165
|
+
return config.project || ({} as ProjectSettings);
|
|
166
|
+
} catch (e) {
|
|
167
|
+
throw error(`Aborted (missing synthetics config file), Project not set up correctly.
|
|
168
|
+
|
|
169
|
+
${INSTALLATION_HELP}`);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
export function validateSettings(opts: PushOptions) {
|
|
174
|
+
const INVALID = 'Aborted. Invalid synthetics project settings.';
|
|
175
|
+
|
|
176
|
+
let reason = '';
|
|
177
|
+
if (!opts.id) {
|
|
178
|
+
reason = `Set project id via
|
|
179
|
+
- CLI '--id <id>'
|
|
180
|
+
- Config file 'project.id' field`;
|
|
181
|
+
} else if (!opts.locations && !opts.privateLocations) {
|
|
182
|
+
reason = `Set default location for all monitors via
|
|
183
|
+
- CLI '--locations <values...> or --privateLocations <values...>'
|
|
184
|
+
- Config file 'monitors.locations' | 'monitors.privateLocations' field`;
|
|
185
|
+
} else if (!opts.schedule) {
|
|
186
|
+
reason = `Set default schedule in minutes for all monitors via
|
|
187
|
+
- CLI '--schedule <mins>'
|
|
188
|
+
- Config file 'monitors.schedule' field`;
|
|
189
|
+
} else if (opts.schedule && !ALLOWED_SCHEDULES.includes(opts.schedule)) {
|
|
190
|
+
reason = `Set default schedule(${
|
|
191
|
+
opts.schedule
|
|
192
|
+
}) to one of the allowed values - ${ALLOWED_SCHEDULES.join(',')}`;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
if (!reason) return;
|
|
196
|
+
|
|
197
|
+
throw error(`${INVALID}
|
|
198
|
+
|
|
199
|
+
${reason}
|
|
200
|
+
|
|
201
|
+
${INSTALLATION_HELP}`);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
async function overrideSettings(oldValue: string, newValue: string) {
|
|
205
|
+
const cwd = process.cwd();
|
|
206
|
+
const configPath = await findSyntheticsConfig(cwd, cwd);
|
|
207
|
+
if (!configPath) {
|
|
208
|
+
throw warn(`Unable to find synthetics config file: ${configPath}`);
|
|
209
|
+
}
|
|
210
|
+
const config = await readFile(configPath, 'utf-8');
|
|
211
|
+
const updatedConfig = config.replace(
|
|
212
|
+
`id: '${oldValue}'`,
|
|
213
|
+
`id: '${newValue}'`
|
|
214
|
+
);
|
|
215
|
+
await writeFile(configPath, updatedConfig, 'utf-8');
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
export async function catchIncorrectSettings(
|
|
219
|
+
settings: ProjectSettings,
|
|
220
|
+
options: PushOptions
|
|
221
|
+
) {
|
|
222
|
+
let override = !settings.id;
|
|
223
|
+
if (settings.id && settings.id !== options.id) {
|
|
224
|
+
// Add an extra line to make it easier to read the prompt
|
|
225
|
+
write('');
|
|
226
|
+
({ override } = await prompt<{ override: boolean }>({
|
|
227
|
+
type: 'confirm',
|
|
228
|
+
name: 'override',
|
|
229
|
+
skip() {
|
|
230
|
+
if (options.yes) {
|
|
231
|
+
this.initial = process.env.TEST_OVERRIDE ?? true;
|
|
232
|
+
return true;
|
|
233
|
+
}
|
|
234
|
+
return false;
|
|
235
|
+
},
|
|
236
|
+
message: `Monitors were pushed under the '${settings.id}' project. Are you sure you want to push them under the new '${options.id}' (note that this will duplicate the monitors, the old ones being orphaned)`,
|
|
237
|
+
initial: false,
|
|
238
|
+
}));
|
|
239
|
+
if (!override) {
|
|
240
|
+
throw warn('Push command Aborted');
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
if (override) {
|
|
244
|
+
await overrideSettings(settings.id, options.id);
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
export async function pushLegacy(
|
|
249
|
+
monitors: Monitor[],
|
|
250
|
+
options: PushOptions,
|
|
251
|
+
version: number
|
|
252
|
+
) {
|
|
253
|
+
const noLightWeightSupport = semver.satisfies(version, '<8.5.0');
|
|
254
|
+
if (
|
|
255
|
+
noLightWeightSupport &&
|
|
256
|
+
monitors.some(monitor => monitor.type !== 'browser')
|
|
257
|
+
) {
|
|
258
|
+
throw error(
|
|
259
|
+
`Aborted: Lightweight monitors are not supported in ${version}. Please upgrade to 8.5.0 or above.`
|
|
260
|
+
);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
let schemas: MonitorSchema[] = [];
|
|
264
|
+
if (monitors.length > 0) {
|
|
265
|
+
progress(`bundling ${monitors.length} monitors`);
|
|
266
|
+
schemas = await buildMonitorSchema(monitors, false);
|
|
267
|
+
const chunks = getChunks(schemas, 10);
|
|
268
|
+
for (const chunk of chunks) {
|
|
269
|
+
await liveProgress(
|
|
270
|
+
createMonitorsLegacy({ schemas: chunk, keepStale: true, options }),
|
|
271
|
+
`creating or updating ${chunk.length} monitors`
|
|
272
|
+
);
|
|
273
|
+
}
|
|
274
|
+
} else {
|
|
275
|
+
await promptConfirmDeleteAll(options);
|
|
276
|
+
}
|
|
277
|
+
await liveProgress(
|
|
278
|
+
createMonitorsLegacy({ schemas, keepStale: false, options }),
|
|
279
|
+
`deleting all stale monitors`
|
|
280
|
+
);
|
|
281
|
+
|
|
282
|
+
done(`Pushed: ${grey(getMonitorManagementURL(options.url))}`);
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
// prints warning if any of the monitors has throttling settings enabled during push
|
|
286
|
+
export function warnIfThrottled(monitors: Monitor[]) {
|
|
287
|
+
const throttled = monitors.some(monitor => monitor.config.throttling != null);
|
|
288
|
+
if (throttled) {
|
|
289
|
+
warn(THROTTLING_WARNING_MSG);
|
|
290
|
+
}
|
|
291
|
+
}
|