@atlaspack/runtime-js 2.12.1-canary.3613 → 2.12.1-canary.3615

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.
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+
3
+ function sendAnalyticsEvent(detail) {
4
+ const ev = new globalThis.CustomEvent('atlaspack:analytics', {
5
+ detail
6
+ });
7
+ globalThis.dispatchEvent(ev);
8
+ }
9
+ module.exports = {
10
+ sendAnalyticsEvent
11
+ };
@@ -44,21 +44,29 @@ async function load(id) {
44
44
  // Append the current time to the request URL
45
45
  // to ensure it has not been cached by the browser
46
46
  // eslint-disable-next-line no-undef
47
- return await __parcel__import__(`${url}?t=${Date.now()}`);
47
+ const result = await __parcel__import__(`${url}?t=${Date.now()}`);
48
+ sendAnalyticsEvent('recovered', url, i);
49
+ return result;
48
50
  } catch (error) {
49
- if (i === maxRetries) throw error;
50
- // Dispatch event for reporting
51
- const event = {
52
- detail: {
53
- target: url,
54
- attempt: i
55
- }
56
- };
57
- globalThis.dispatchEvent(new CustomEvent('atlaspack:import_retry', event));
51
+ if (i === maxRetries) {
52
+ sendAnalyticsEvent('failure', url, i);
53
+ throw error;
54
+ }
55
+ sendAnalyticsEvent('progress', url, i);
58
56
  }
59
57
  }
60
58
  })();
61
59
  }
62
60
  return retryState[url];
63
61
  }
62
+ function sendAnalyticsEvent(status, targetUrl, attempt) {
63
+ require('./analytics/analytics.js').sendAnalyticsEvent({
64
+ action: 'importRetry',
65
+ attributes: {
66
+ status,
67
+ targetUrl,
68
+ attempt
69
+ }
70
+ });
71
+ }
64
72
  module.exports = load;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atlaspack/runtime-js",
3
- "version": "2.12.1-canary.3613+d904a75ee",
3
+ "version": "2.12.1-canary.3615+27eca1df6",
4
4
  "license": "(MIT OR Apache-2.0)",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -12,16 +12,16 @@
12
12
  "main": "lib/JSRuntime.js",
13
13
  "source": "src/JSRuntime.js",
14
14
  "engines": {
15
- "atlaspack": "2.12.1-canary.3613+d904a75ee",
15
+ "atlaspack": "2.12.1-canary.3615+27eca1df6",
16
16
  "node": ">= 16.0.0"
17
17
  },
18
18
  "dependencies": {
19
- "@atlaspack/diagnostic": "2.12.1-canary.3613+d904a75ee",
20
- "@atlaspack/domain-sharding": "2.12.1-canary.3613+d904a75ee",
21
- "@atlaspack/feature-flags": "2.12.1-canary.3613+d904a75ee",
22
- "@atlaspack/plugin": "2.12.1-canary.3613+d904a75ee",
23
- "@atlaspack/utils": "2.12.1-canary.3613+d904a75ee",
19
+ "@atlaspack/diagnostic": "2.12.1-canary.3615+27eca1df6",
20
+ "@atlaspack/domain-sharding": "2.12.1-canary.3615+27eca1df6",
21
+ "@atlaspack/feature-flags": "2.12.1-canary.3615+27eca1df6",
22
+ "@atlaspack/plugin": "2.12.1-canary.3615+27eca1df6",
23
+ "@atlaspack/utils": "2.12.1-canary.3615+27eca1df6",
24
24
  "nullthrows": "^1.1.1"
25
25
  },
26
- "gitHead": "d904a75eecaea2c2e62cd8ff18f217057d545fc4"
26
+ "gitHead": "27eca1df6f31abdf1df72f21350b035772302aea"
27
27
  }
@@ -0,0 +1,6 @@
1
+ export type AtlaspackAnalyticsEvent = {
2
+ action: string;
3
+ attributes?: {[key: string]: string | number | boolean};
4
+ };
5
+
6
+ export function sendAnalyticsEvent(event: AtlaspackAnalyticsEvent): void;
@@ -0,0 +1,6 @@
1
+ function sendAnalyticsEvent(detail) {
2
+ const ev = new globalThis.CustomEvent('atlaspack:analytics', {detail});
3
+ globalThis.dispatchEvent(ev);
4
+ }
5
+
6
+ module.exports = {sendAnalyticsEvent};
@@ -0,0 +1,8 @@
1
+ // @flow
2
+
3
+ export type AtlaspackAnalyticsEvent = {|
4
+ action: string,
5
+ attributes?: { [key: string]: string | number | boolean },
6
+ |};
7
+
8
+ declare export function sendAnalyticsEvent(event: AtlaspackAnalyticsEvent): void;
@@ -42,14 +42,15 @@ async function load(id) {
42
42
  // Append the current time to the request URL
43
43
  // to ensure it has not been cached by the browser
44
44
  // eslint-disable-next-line no-undef
45
- return await __parcel__import__(`${url}?t=${Date.now()}`);
45
+ const result = await __parcel__import__(`${url}?t=${Date.now()}`);
46
+ sendAnalyticsEvent('recovered', url, i);
47
+ return result;
46
48
  } catch (error) {
47
- if (i === maxRetries) throw error;
48
- // Dispatch event for reporting
49
- const event = {detail: {target: url, attempt: i}};
50
- globalThis.dispatchEvent(
51
- new CustomEvent('atlaspack:import_retry', event),
52
- );
49
+ if (i === maxRetries) {
50
+ sendAnalyticsEvent('failure', url, i);
51
+ throw error;
52
+ }
53
+ sendAnalyticsEvent('progress', url, i);
53
54
  }
54
55
  }
55
56
  })();
@@ -58,4 +59,15 @@ async function load(id) {
58
59
  return retryState[url];
59
60
  }
60
61
 
62
+ function sendAnalyticsEvent(status, targetUrl, attempt) {
63
+ require('./analytics/analytics.js').sendAnalyticsEvent({
64
+ action: 'importRetry',
65
+ attributes: {
66
+ status,
67
+ targetUrl,
68
+ attempt,
69
+ },
70
+ });
71
+ }
72
+
61
73
  module.exports = load;
@@ -0,0 +1,47 @@
1
+ import {sendAnalyticsEvent} from '../src/helpers/browser/analytics/analytics.js';
2
+ import {mock} from 'node:test';
3
+ import type {Mock} from 'node:test';
4
+ import assert from 'node:assert';
5
+
6
+ describe('@atlaspack/analytics', () => {
7
+ let dispatchEventMock: Mock<Window['dispatchEvent']>;
8
+
9
+ beforeEach(() => {
10
+ // @ts-expect-error
11
+ globalThis.CustomEvent = MockCustomEvent;
12
+ dispatchEventMock = mock.fn();
13
+ globalThis.dispatchEvent = dispatchEventMock;
14
+ });
15
+
16
+ it('should not throw', () => {
17
+ assert.doesNotThrow(() =>
18
+ sendAnalyticsEvent({
19
+ action: 'test',
20
+ }),
21
+ );
22
+ });
23
+
24
+ it('should raise event on window', () => {
25
+ sendAnalyticsEvent({
26
+ action: 'test',
27
+ });
28
+
29
+ assert.equal(dispatchEventMock.mock.callCount(), 1);
30
+ assert.deepEqual(dispatchEventMock.mock.calls[0].arguments[0], {
31
+ eventName: 'atlaspack:analytics',
32
+ detail: {
33
+ action: 'test',
34
+ },
35
+ });
36
+ });
37
+ });
38
+
39
+ class MockCustomEvent {
40
+ eventName: string;
41
+ detail: any;
42
+
43
+ constructor(eventName: string, options: any = {}) {
44
+ this.eventName = eventName;
45
+ this.detail = options.detail;
46
+ }
47
+ }