@elastic/synthetics 1.25.0 → 1.26.1

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.
@@ -64,18 +64,22 @@ export class NetworkManager {
64
64
  * to not result in exception
65
65
  */
66
66
  private _addBarrier(page: Page, promise: Promise<void>) {
67
- if (!page) return;
67
+ if (!page) {
68
+ promise.catch(error => {
69
+ log(`Plugins: failed to collect network metadata: ${error}`);
70
+ });
71
+ return;
72
+ }
68
73
  const race = Promise.race([
69
- new Promise<void>(resolve =>
70
- page.on('close', () => {
71
- this._barrierPromises.delete(race);
72
- resolve();
73
- })
74
- ),
74
+ new Promise<void>(resolve => page.once('close', () => resolve())),
75
75
  promise,
76
76
  ]);
77
77
  this._barrierPromises.add(race);
78
- race.then(() => this._barrierPromises.delete(race));
78
+ race
79
+ .catch(error => {
80
+ log(`Plugins: failed to collect network metadata: ${error}`);
81
+ })
82
+ .finally(() => this._barrierPromises.delete(race));
79
83
  }
80
84
 
81
85
  private _nullableFrameBarrier(req: Request): Frame | null {
@@ -98,10 +102,10 @@ export class NetworkManager {
98
102
  /**
99
103
  * Listen for all network events from PW context
100
104
  */
101
- context.on('request', this._onRequest.bind(this));
102
- context.on('response', this._onResponse.bind(this));
103
- context.on('requestfinished', this._onRequestCompleted.bind(this));
104
- context.on('requestfailed', this._onRequestCompleted.bind(this));
105
+ context.on('request', this._onRequest);
106
+ context.on('response', this._onResponse);
107
+ context.on('requestfinished', this._onRequestCompleted);
108
+ context.on('requestfailed', this._onRequestCompleted);
105
109
  }
106
110
 
107
111
  private _findNetworkEntry(
@@ -110,7 +114,7 @@ export class NetworkManager {
110
114
  return request[NETWORK_ENTRY_SUMBOL];
111
115
  }
112
116
 
113
- private _onRequest(request: Request) {
117
+ private _onRequest = (request: Request) => {
114
118
  const url = request.url();
115
119
  /**
116
120
  * Data URI should not show up as network requests
@@ -165,9 +169,9 @@ export class NetworkManager {
165
169
 
166
170
  request[NETWORK_ENTRY_SUMBOL] = networkEntry;
167
171
  this.results.push(networkEntry);
168
- }
172
+ };
169
173
 
170
- private async _onResponse(response: Response) {
174
+ private _onResponse = async (response: Response) => {
171
175
  const request = response.request();
172
176
  const networkEntry = this._findNetworkEntry(request);
173
177
  if (!networkEntry) return;
@@ -220,9 +224,9 @@ export class NetworkManager {
220
224
  if (details) networkEntry.response.securityDetails = details;
221
225
  })
222
226
  );
223
- }
227
+ };
224
228
 
225
- private async _onRequestCompleted(request: Request) {
229
+ private _onRequestCompleted = async (request: Request) => {
226
230
  const networkEntry = this._findNetworkEntry(request);
227
231
  if (!networkEntry) return;
228
232
 
@@ -255,9 +259,18 @@ export class NetworkManager {
255
259
  };
256
260
  })
257
261
  );
258
- }
262
+ };
259
263
 
260
264
  async stop() {
265
+ /**
266
+ * First detach the listeners so that no new network events are recorded
267
+ */
268
+ const context = this.driver.context;
269
+ context.off('request', this._onRequest);
270
+ context.off('response', this._onResponse);
271
+ context.off('requestfinished', this._onRequestCompleted);
272
+ context.off('requestfailed', this._onRequestCompleted);
273
+
261
274
  /**
262
275
  * Waiting for all network events is error prone and might hang the tests
263
276
  * from getting closed forever when there are upstream bugs in browsers or
@@ -266,11 +279,6 @@ export class NetworkManager {
266
279
  if (this._barrierPromises.size > 0) {
267
280
  log(`Plugins: dropping ${this._barrierPromises.size} network events`);
268
281
  }
269
- const context = this.driver.context;
270
- context.off('request', this._onRequest.bind(this));
271
- context.off('response', this._onResponse.bind(this));
272
- context.off('requestfinished', this._onRequestCompleted.bind(this));
273
- context.off('requestfailed', this._onRequestCompleted.bind(this));
274
282
  this._barrierPromises.clear();
275
283
  log(`Plugins: stopped collecting network events`);
276
284
  return this.results;
@@ -74,21 +74,30 @@ async function runNpmInstall(directory) {
74
74
 
75
75
  async function runTest(directory, schema: MonitorSchema) {
76
76
  return new Promise<void>((resolve, reject) => {
77
- const runTest = spawn(
78
- 'npx',
79
- [
80
- '@elastic/synthetics',
81
- '.',
82
- '--playwright-options',
83
- JSON.stringify(schema.playwrightOptions),
84
- '--params',
85
- JSON.stringify(schema.params),
86
- ],
87
- {
88
- cwd: directory,
89
- stdio: 'inherit',
77
+ const args = [
78
+ '@elastic/synthetics',
79
+ '.',
80
+ '--playwright-options',
81
+ JSON.stringify(schema.playwrightOptions),
82
+ '--params',
83
+ JSON.stringify(schema.params),
84
+ ];
85
+ const allowlist = schema.certificateErrorSpkiAllowlist;
86
+ if (allowlist != null) {
87
+ const entries = Array.isArray(allowlist) ? allowlist : [allowlist];
88
+ const pemEntries = entries
89
+ .map(entry =>
90
+ Buffer.isBuffer(entry) ? entry.toString('utf-8') : entry
91
+ )
92
+ .filter((entry): entry is string => Boolean(entry));
93
+ if (pemEntries.length > 0) {
94
+ args.push('--certificate-error-spki-allowlist', ...pemEntries);
90
95
  }
91
- );
96
+ }
97
+ const runTest = spawn('npx', args, {
98
+ cwd: directory,
99
+ stdio: 'inherit',
100
+ });
92
101
 
93
102
  runTest.on('close', resolve);
94
103
  runTest.on('error', err => {
@@ -1,7 +0,0 @@
1
- // Skip if husky is not installed (CI, production, embedded installs with --omit=dev)
2
- try {
3
- const husky = await import('husky');
4
- husky.default();
5
- } catch (e) {
6
- if (e.code !== 'ERR_MODULE_NOT_FOUND') throw e;
7
- }