@amedia/user 0.13.39 → 0.13.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 (86) hide show
  1. package/dist/user.cjs +1 -1
  2. package/dist/user.cjs.map +3 -3
  3. package/dist/user.d.ts +5 -5
  4. package/dist/user.js +1 -1
  5. package/dist/user.js.map +3 -3
  6. package/package.json +5 -3
  7. package/src/actions/authentication.spec.ts +51 -0
  8. package/src/actions/authentication.ts +43 -0
  9. package/src/actions/authorization.spec.ts +369 -0
  10. package/src/actions/authorization.ts +117 -0
  11. package/src/actions/preferences.spec.ts +41 -0
  12. package/src/actions/preferences.ts +41 -0
  13. package/src/actions/storage.ts +22 -0
  14. package/src/actions/userData.spec.ts +133 -0
  15. package/src/actions/userData.ts +68 -0
  16. package/src/autologin/index.spec.ts +239 -0
  17. package/src/autologin/index.ts +92 -0
  18. package/src/autologin/thirdPartyCookie.spec.ts +95 -0
  19. package/src/autologin/thirdPartyCookie.ts +55 -0
  20. package/src/cache/engines/global.spec.ts +62 -0
  21. package/src/cache/engines/global.ts +49 -0
  22. package/src/cache/index.spec.ts +130 -0
  23. package/src/cache/index.ts +48 -0
  24. package/src/cache/types.ts +12 -0
  25. package/src/clients/europa.spec.ts +209 -0
  26. package/src/clients/europa.ts +183 -0
  27. package/src/clients/jupiter.spec.ts +179 -0
  28. package/src/clients/jupiter.ts +119 -0
  29. package/src/clients/mercury.spec.ts +59 -0
  30. package/src/clients/mercury.ts +48 -0
  31. package/src/clients/mercuryLog.spec.ts +20 -0
  32. package/src/clients/mercuryLog.ts +67 -0
  33. package/src/clients/nebula.spec.ts +70 -0
  34. package/src/clients/nebula.ts +83 -0
  35. package/src/clients/pluto.spec.ts +165 -0
  36. package/src/clients/pluto.ts +118 -0
  37. package/src/eventHandlers/activation.ts +36 -0
  38. package/src/eventHandlers/authentication.ts +34 -0
  39. package/src/eventHandlers/eventMap.ts +34 -0
  40. package/src/eventHandlers/state.spec.ts +48 -0
  41. package/src/eventHandlers/state.ts +23 -0
  42. package/src/eventHandlers/storage.spec.ts +114 -0
  43. package/src/eventHandlers/storage.ts +45 -0
  44. package/src/eventHandlers/userAttributes.spec.ts +93 -0
  45. package/src/eventHandlers/userAttributes.ts +31 -0
  46. package/src/exceptions/FetchTimeoutError.ts +8 -0
  47. package/src/exceptions/PaywallError.ts +16 -0
  48. package/src/index-authentcation.spec.ts +26 -0
  49. package/src/index-storage.spec.ts +144 -0
  50. package/src/index-subscription.spec.ts +213 -0
  51. package/src/index.ts +40 -0
  52. package/src/janitor/index.spec.ts +18 -0
  53. package/src/janitor/index.ts +59 -0
  54. package/src/janitor/jobs/accessCookieRefresher.spec.ts +213 -0
  55. package/src/janitor/jobs/accessCookieRefresher.ts +98 -0
  56. package/src/janitor/jobs/daxsubCookie.spec.ts +109 -0
  57. package/src/janitor/jobs/daxsubCookie.ts +38 -0
  58. package/src/janitor/jobs/removeUserStorage.spec.ts +34 -0
  59. package/src/janitor/jobs/removeUserStorage.ts +12 -0
  60. package/src/lib/PaywallUnlockRequest.spec.ts +363 -0
  61. package/src/lib/PaywallUnlockRequest.ts +143 -0
  62. package/src/lib/UserDataRequest.spec.ts +231 -0
  63. package/src/lib/UserDataRequest.ts +267 -0
  64. package/src/lib/cookie.ts +121 -0
  65. package/src/lib/rpc.ts +19 -0
  66. package/src/store/index.spec.ts +49 -0
  67. package/src/store/index.ts +64 -0
  68. package/src/types.ts +22 -0
  69. package/src/utils/customEventWrapper.ts +58 -0
  70. package/src/utils/debouncer.spec.ts +252 -0
  71. package/src/utils/debouncer.ts +123 -0
  72. package/src/utils/deepEquals.ts +42 -0
  73. package/src/utils/expectBootLogs.ts +9 -0
  74. package/src/utils/getGlobalThis.ts +17 -0
  75. package/src/utils/getOrUpdate.spec.ts +166 -0
  76. package/src/utils/getOrUpdate.ts +66 -0
  77. package/src/utils/localhostCheck.ts +10 -0
  78. package/src/utils/location.ts +31 -0
  79. package/src/utils/log.spec.ts +148 -0
  80. package/src/utils/log.ts +124 -0
  81. package/src/utils/loginState.ts +24 -0
  82. package/src/utils/networkError.ts +11 -0
  83. package/src/utils/queue.ts +32 -0
  84. package/src/utils/type.ts +19 -0
  85. package/src/utils/url.spec.ts +33 -0
  86. package/src/utils/url.ts +37 -0
@@ -0,0 +1,93 @@
1
+ import { dispatch, listenForAllEvents } from '../utils/customEventWrapper';
2
+ import { listenForUserAttributeRequests } from './userAttributes';
3
+ import * as userData from '../actions/userData';
4
+ import store from '../store';
5
+ import { AmediaUserEvents } from './eventMap';
6
+
7
+ beforeEach(() => {
8
+ store.clear();
9
+ });
10
+
11
+ describe('listenForUserAttributeRequests', () => {
12
+ let currentAttributes: any;
13
+ beforeEach(() => {
14
+ listenForAllEvents(AmediaUserEvents.ON_ATTRIBUTES_UPDATED, (evt) => {
15
+ currentAttributes = evt.detail;
16
+ });
17
+ listenForUserAttributeRequests();
18
+ jest
19
+ .spyOn(userData, 'requestAttributes')
20
+ .mockReturnValue(Promise.resolve());
21
+ });
22
+
23
+ describe('AmediaUserEvents.REQUEST_USER_ATTRIBUTES received', () => {
24
+ describe('no previous attributes', () => {
25
+ beforeEach(() => {
26
+ dispatch(
27
+ new CustomEvent(AmediaUserEvents.REQUEST_USER_ATTRIBUTES, {
28
+ detail: ['uuid', 'name', 'email'],
29
+ })
30
+ );
31
+ });
32
+ it('calls requestAttributes with requested attributes', () => {
33
+ expect(userData.requestAttributes).toHaveBeenCalledWith([
34
+ 'uuid',
35
+ 'name',
36
+ 'email',
37
+ ]);
38
+ });
39
+ it('does not receive an attribute event', () => {
40
+ expect(currentAttributes).toBeUndefined();
41
+ });
42
+ });
43
+
44
+ describe('with some attributes present already', () => {
45
+ beforeEach(() => {
46
+ store.set('userAttributes', {
47
+ uuid: 'someuuid',
48
+ });
49
+ dispatch(
50
+ new CustomEvent(AmediaUserEvents.REQUEST_USER_ATTRIBUTES, {
51
+ detail: ['uuid', 'name', 'email'],
52
+ })
53
+ );
54
+ });
55
+ it('calls requestAttributes with only attributes we did not already know the value of', () => {
56
+ expect(userData.requestAttributes).toHaveBeenCalledWith([
57
+ 'name',
58
+ 'email',
59
+ ]);
60
+ });
61
+ it('receives initial event with previous attributes', () => {
62
+ expect(currentAttributes).toEqual({
63
+ uuid: 'someuuid',
64
+ });
65
+ });
66
+ });
67
+
68
+ describe('with all attributes present already', () => {
69
+ beforeEach(() => {
70
+ store.set('userAttributes', {
71
+ uuid: 'someuuid',
72
+ name: 'Some Name',
73
+ email: 'some@where.com',
74
+ });
75
+ dispatch(
76
+ new CustomEvent(AmediaUserEvents.REQUEST_USER_ATTRIBUTES, {
77
+ detail: ['uuid', 'name', 'email'],
78
+ })
79
+ );
80
+ });
81
+ it('does not call requestAttributes', () => {
82
+ expect(userData.requestAttributes).not.toHaveBeenCalled();
83
+ });
84
+ it('receives initial event with all attributes', () => {
85
+ expect(currentAttributes).toEqual({
86
+ uuid: 'someuuid',
87
+ name: 'Some Name',
88
+ email: 'some@where.com',
89
+ });
90
+ });
91
+ });
92
+ });
93
+ });
@@ -0,0 +1,31 @@
1
+ import { requestAttributes } from '../actions/userData';
2
+ import { dispatch, listenWithExclusivity } from '../utils/customEventWrapper';
3
+ import store from '../store';
4
+ import { AmediaUserEvents } from './eventMap';
5
+
6
+ export const listenForUserAttributeRequests = () => {
7
+ store.on('userAttributes', (user) => {
8
+ dispatch(
9
+ new CustomEvent(AmediaUserEvents.ON_ATTRIBUTES_UPDATED, {
10
+ detail: user,
11
+ })
12
+ );
13
+ });
14
+ listenWithExclusivity(AmediaUserEvents.REQUEST_USER_ATTRIBUTES, (evt) => {
15
+ const requestedAttributes = evt.detail;
16
+ const currentAttributes = store.get('userAttributes') || {};
17
+ if (Object.keys(currentAttributes).length > 0) {
18
+ dispatch(
19
+ new CustomEvent(AmediaUserEvents.ON_ATTRIBUTES_UPDATED, {
20
+ detail: currentAttributes,
21
+ })
22
+ );
23
+ }
24
+ const missingAttributes = requestedAttributes.filter(
25
+ (attribute) => currentAttributes[attribute] === undefined
26
+ );
27
+ if (missingAttributes.length > 0) {
28
+ requestAttributes(missingAttributes);
29
+ }
30
+ });
31
+ };
@@ -0,0 +1,8 @@
1
+ export default class FetchTimeoutError extends Error {
2
+ partialData: Record<string, object>;
3
+
4
+ constructor(message: string, partialData: Record<string, object>) {
5
+ super(message);
6
+ this.partialData = partialData;
7
+ }
8
+ }
@@ -0,0 +1,16 @@
1
+ class PaywallError extends Error {
2
+ static ERROR_RELOADED_NO_ACCESS = 'RELOADED_NO_ACCESS';
3
+
4
+ static ERROR_ENABLING_ACCESS_FAILED = 'ENABLING_ACCESS_FAILED';
5
+
6
+ static ERROR_NO_ACCESS_AFTER_SUCCESSFUL_ACTIVATION_WITH_REQUESTED_ACCESS_FEATURES =
7
+ 'NO_ACCESS_AFTER_SUCCESSFUL_ACTIVATION_WITH_REQUESTED_ACCESS_FEATURES';
8
+
9
+ public code: string;
10
+
11
+ constructor(message: string, code: string) {
12
+ super(message);
13
+ this.code = code;
14
+ }
15
+ }
16
+ export default PaywallError;
@@ -0,0 +1,26 @@
1
+ import { goToLoginPage, logout } from './index';
2
+ import * as authentication from './actions/authentication';
3
+ import { listenForAuthenticationRequests } from './eventHandlers/authentication';
4
+
5
+ beforeEach(() => {
6
+ listenForAuthenticationRequests();
7
+ });
8
+
9
+ describe('Public endpoint - authentication', () => {
10
+ it('should call callback on logout complete', async () => {
11
+ jest
12
+ .spyOn(authentication, 'logout')
13
+ .mockImplementation(() => Promise.resolve());
14
+
15
+ await logout();
16
+ expect(authentication.logout).toHaveBeenCalledTimes(1);
17
+ });
18
+
19
+ it('should login', () => {
20
+ jest.spyOn(authentication, 'login').mockImplementation();
21
+ goToLoginPage('https://www.avisnavn.no');
22
+ expect(authentication.login).toHaveBeenCalledWith(
23
+ 'https://www.avisnavn.no'
24
+ );
25
+ });
26
+ });
@@ -0,0 +1,144 @@
1
+ import * as europa from './clients/europa';
2
+ import { saveStorage, updateStorage } from './index';
3
+ import store from './store';
4
+ import UserDataRequest from './lib/UserDataRequest';
5
+ import { listenForStorageRequests } from './eventHandlers/storage';
6
+ import { listenForStateRequests } from './eventHandlers/state';
7
+
8
+ beforeEach(() => {
9
+ // Store must be cleared before we listen for user attribute requests
10
+ store.clear();
11
+
12
+ listenForStateRequests();
13
+ listenForStorageRequests();
14
+ });
15
+
16
+ describe('Storage', () => {
17
+ beforeEach(() => {
18
+ spyOn(europa, 'saveNamespace');
19
+ spyOn(europa, 'updateNamespace');
20
+ });
21
+
22
+ describe('when is logged in', () => {
23
+ beforeEach(() => {
24
+ store.set('state', { isLoggedIn: true });
25
+ });
26
+
27
+ describe('when saving storage', () => {
28
+ beforeEach(() => {
29
+ saveStorage({
30
+ user_info: {
31
+ some: 'data',
32
+ },
33
+ });
34
+ });
35
+ it('should save storage', () => {
36
+ expect(europa.saveNamespace).toHaveBeenCalledWith('user_info', {
37
+ some: 'data',
38
+ });
39
+ });
40
+ });
41
+
42
+ describe('when updating storage', () => {
43
+ beforeEach(() => {
44
+ updateStorage({
45
+ user_info: {
46
+ some: 'data',
47
+ },
48
+ });
49
+ });
50
+ it('should update storage', () => {
51
+ expect(europa.updateNamespace).toHaveBeenCalledWith('user_info', {
52
+ some: 'data',
53
+ });
54
+ });
55
+ });
56
+
57
+ describe('when getting storage namespaces', () => {
58
+ let onUpdate: any;
59
+ beforeEach(() => {
60
+ store.set('storage', {
61
+ one: { key1: 'value1' },
62
+ two: { key2: 'value2' },
63
+ three: { key3: 'value3' },
64
+ });
65
+
66
+ onUpdate = jest.fn();
67
+ new UserDataRequest().withStorage(['one', 'three']).subscribe(onUpdate);
68
+ });
69
+
70
+ it('should only return requested', () => {
71
+ expect(onUpdate).toHaveBeenCalledWith({
72
+ storage: {
73
+ one: {
74
+ key1: 'value1',
75
+ },
76
+ three: {
77
+ key3: 'value3',
78
+ },
79
+ },
80
+ state: {
81
+ isLoggedIn: true,
82
+ },
83
+ });
84
+ });
85
+ });
86
+ });
87
+
88
+ describe('when not logged in', () => {
89
+ beforeEach(() => {
90
+ store.set('state', { isLoggedIn: false });
91
+ });
92
+
93
+ describe('when saving storage', () => {
94
+ beforeEach(() => {
95
+ saveStorage({
96
+ user_info: {
97
+ some: 'data',
98
+ },
99
+ });
100
+ });
101
+ it('should not attempt storage save', () => {
102
+ expect(europa.saveNamespace).not.toHaveBeenCalled();
103
+ });
104
+ });
105
+
106
+ describe('when updating storage', () => {
107
+ beforeEach(() => {
108
+ updateStorage({
109
+ user_info: {
110
+ some: 'data',
111
+ },
112
+ });
113
+ });
114
+ it('should not attempt storage update', () => {
115
+ expect(europa.updateNamespace).not.toHaveBeenCalled();
116
+ });
117
+ });
118
+
119
+ describe('when getting storage namespaces', () => {
120
+ let onUpdate: any;
121
+ beforeEach(() => {
122
+ store.set('storage', {
123
+ one: { key1: 'value1' },
124
+ two: { key2: 'value2' },
125
+ three: { key3: 'value3' },
126
+ });
127
+
128
+ onUpdate = jest.fn();
129
+ new UserDataRequest().withStorage(['one', 'three']).subscribe(onUpdate);
130
+ });
131
+
132
+ it('should have been called once', () => {
133
+ expect(onUpdate).toHaveBeenCalledTimes(1);
134
+ });
135
+
136
+ it('should never return namespaces', () => {
137
+ expect(onUpdate).toHaveBeenCalledWith({
138
+ state: { isLoggedIn: false },
139
+ storage: {},
140
+ });
141
+ });
142
+ });
143
+ });
144
+ });
@@ -0,0 +1,213 @@
1
+ import UserDataRequest from './lib/UserDataRequest';
2
+ import * as userData from './actions/userData';
3
+ import { listenForUserAttributeRequests } from './eventHandlers/userAttributes';
4
+ import store from './store';
5
+ import { listenForStateRequests } from './eventHandlers/state';
6
+
7
+ beforeEach(() => {
8
+ // Store must be cleared before we listen for user attribute requests
9
+ store.clear();
10
+
11
+ listenForUserAttributeRequests();
12
+ listenForStateRequests();
13
+ });
14
+
15
+ describe('Public endpoint - userdata', () => {
16
+ it('should alert about user data', () => {
17
+ jest.spyOn(userData, 'requestAttributes').mockImplementation(() => {
18
+ store.merge('userAttributes', { name: 'Donald Duck' });
19
+ return Promise.resolve();
20
+ });
21
+
22
+ const onUserUpdate = jest.fn();
23
+ new UserDataRequest().withAttributes(['name']).subscribe(onUserUpdate);
24
+
25
+ expect(onUserUpdate).toHaveBeenCalledWith({
26
+ attributes: {
27
+ name: 'Donald Duck',
28
+ },
29
+ state: {},
30
+ });
31
+ });
32
+
33
+ it('should update with more data', () => {
34
+ jest.spyOn(userData, 'requestAttributes').mockImplementation(() => {
35
+ store.merge('userAttributes', { name: 'Donald Duck' });
36
+ store.merge('userAttributes', { uuid: '1234-123-1234' });
37
+ return Promise.resolve();
38
+ });
39
+
40
+ const onUserUpdate = jest.fn();
41
+ new UserDataRequest()
42
+ .withAttributes(['name', 'uuid'])
43
+ .subscribe(onUserUpdate);
44
+
45
+ expect(onUserUpdate).nthCalledWith(1, {
46
+ attributes: {
47
+ name: 'Donald Duck',
48
+ },
49
+ state: {},
50
+ });
51
+ expect(onUserUpdate).nthCalledWith(2, {
52
+ attributes: {
53
+ uuid: '1234-123-1234',
54
+ name: 'Donald Duck',
55
+ },
56
+ state: {},
57
+ });
58
+ });
59
+
60
+ it('should not alert on same data', () => {
61
+ jest.spyOn(userData, 'requestAttributes').mockImplementation(() => {
62
+ store.merge('userAttributes', { name: 'Donald Duck' });
63
+ return Promise.resolve();
64
+ });
65
+
66
+ const onUserUpdate1 = jest.fn();
67
+ new UserDataRequest().withAttributes(['name']).subscribe(onUserUpdate1);
68
+
69
+ const onUserUpdate2 = jest.fn();
70
+ new UserDataRequest().withAttributes(['name']).subscribe(onUserUpdate2);
71
+
72
+ expect(onUserUpdate1).toHaveBeenCalledTimes(1);
73
+ expect(onUserUpdate1).toHaveBeenCalledWith({
74
+ attributes: {
75
+ name: 'Donald Duck',
76
+ },
77
+ state: {},
78
+ });
79
+
80
+ expect(onUserUpdate2).toHaveBeenCalledTimes(1);
81
+ expect(onUserUpdate2).toHaveBeenCalledWith({
82
+ attributes: {
83
+ name: 'Donald Duck',
84
+ },
85
+ state: {},
86
+ });
87
+ });
88
+
89
+ it('should alert when data changes', () => {
90
+ jest.spyOn(userData, 'requestAttributes').mockImplementation(() => {
91
+ store.merge('userAttributes', { name: 'Mickey Mouse' });
92
+ return Promise.resolve();
93
+ });
94
+
95
+ const firstOnUserUpdate = jest.fn();
96
+ new UserDataRequest().withAttributes(['name']).subscribe(firstOnUserUpdate);
97
+
98
+ expect(firstOnUserUpdate).toHaveBeenCalledTimes(1);
99
+ expect(firstOnUserUpdate).nthCalledWith(1, {
100
+ attributes: { name: 'Mickey Mouse' },
101
+ state: {},
102
+ });
103
+
104
+ // Change the name, this shoudl trigger new call on firstOnUserUpdate
105
+ store.merge('userAttributes', { name: 'Donald Duck' });
106
+ expect(firstOnUserUpdate).toHaveBeenCalledTimes(2);
107
+ expect(firstOnUserUpdate).nthCalledWith(2, {
108
+ attributes: {
109
+ name: 'Donald Duck',
110
+ },
111
+ state: {},
112
+ });
113
+ });
114
+
115
+ it('should not return more data that requested', () => {
116
+ jest.spyOn(userData, 'requestAttributes').mockImplementation(() => {
117
+ store.merge('userAttributes', {
118
+ name: 'Donald Duck',
119
+ email: 'donald.duck@andeby.no',
120
+ });
121
+ return Promise.resolve();
122
+ });
123
+
124
+ const onUserUpdate = jest.fn();
125
+ new UserDataRequest().withAttributes(['name']).subscribe(onUserUpdate);
126
+
127
+ expect(onUserUpdate).toHaveBeenCalledWith({
128
+ attributes: {
129
+ name: 'Donald Duck',
130
+ },
131
+ state: {},
132
+ });
133
+ });
134
+
135
+ it('should support returning multiple requested attributes', () => {
136
+ jest.spyOn(userData, 'requestAttributes').mockImplementation(() => {
137
+ store.merge('userAttributes', {
138
+ name: 'Donald Duck',
139
+ email: 'donald.duck@andeby.no',
140
+ });
141
+ return Promise.resolve();
142
+ });
143
+
144
+ const onUserUpdate = jest.fn();
145
+ new UserDataRequest()
146
+ .withAttributes(['name', 'email'])
147
+ .subscribe(onUserUpdate);
148
+
149
+ expect(onUserUpdate).toHaveBeenCalledWith({
150
+ attributes: {
151
+ name: 'Donald Duck',
152
+ email: 'donald.duck@andeby.no',
153
+ },
154
+ state: {},
155
+ });
156
+ });
157
+
158
+ it('should support requesting different attributes', () => {
159
+ jest.spyOn(userData, 'requestAttributes').mockImplementation(() => {
160
+ store.merge('userAttributes', {
161
+ name: 'Donald Duck',
162
+ email: 'donald.duck@andeby.no',
163
+ });
164
+ return Promise.resolve();
165
+ });
166
+
167
+ const firstOnUserUpdate = jest.fn();
168
+ new UserDataRequest()
169
+ .withAttributes(['name', 'email'])
170
+ .subscribe(firstOnUserUpdate);
171
+
172
+ const secondOnUserUpdate = jest.fn();
173
+ new UserDataRequest()
174
+ .withAttributes(['email'])
175
+ .subscribe(secondOnUserUpdate);
176
+
177
+ expect(firstOnUserUpdate).toHaveBeenCalledTimes(1);
178
+ expect(firstOnUserUpdate).nthCalledWith(1, {
179
+ attributes: {
180
+ name: 'Donald Duck',
181
+ email: 'donald.duck@andeby.no',
182
+ },
183
+ state: {},
184
+ });
185
+
186
+ expect(secondOnUserUpdate).toHaveBeenCalledTimes(1);
187
+ expect(secondOnUserUpdate).nthCalledWith(1, {
188
+ attributes: {
189
+ email: 'donald.duck@andeby.no',
190
+ },
191
+ state: {},
192
+ });
193
+ });
194
+
195
+ it('should also report state changes', () => {
196
+ jest.spyOn(userData, 'requestAttributes').mockImplementation(() => {
197
+ store.merge('userAttributes', {
198
+ name: 'Donald Duck',
199
+ });
200
+ store.merge('state', { isLoggedIn: true });
201
+ return Promise.resolve();
202
+ });
203
+
204
+ const onUpdate = jest.fn();
205
+
206
+ new UserDataRequest().withAttributes(['name']).subscribe(onUpdate);
207
+
208
+ expect(onUpdate).toHaveBeenCalledWith({
209
+ attributes: { name: 'Donald Duck' },
210
+ state: { isLoggedIn: true },
211
+ });
212
+ });
213
+ });
package/src/index.ts ADDED
@@ -0,0 +1,40 @@
1
+ import { dispatch } from './utils/customEventWrapper';
2
+ import * as authentication from './actions/authentication';
3
+ import { AmediaUserEvents } from './eventHandlers/eventMap';
4
+ import { getCurrentUrl } from './utils/url';
5
+ import { runJanitor } from './janitor';
6
+ import { listenForStateRequests } from './eventHandlers/state';
7
+ import { listenForUserAttributeRequests } from './eventHandlers/userAttributes';
8
+ import { listenForStorageRequests } from './eventHandlers/storage';
9
+ import getGlobalThis from './utils/getGlobalThis';
10
+
11
+ export { default as UserDataRequest } from './lib/UserDataRequest';
12
+ export { default as PaywallUnlockRequest } from './lib/PaywallUnlockRequest';
13
+
14
+ /**
15
+ * This will, if necessary, trigger a redirect to www.aid.no and return
16
+ * to the requestedUrl if supplied or the current url before redirecting away if not.
17
+ * @param requestedUrl
18
+ */
19
+ export const goToLoginPage = (requestedUrl?: string) =>
20
+ authentication.login(requestedUrl || getCurrentUrl());
21
+
22
+ export const logout = () => authentication.logout();
23
+
24
+ export const updateStorage = (storage: { [index: string]: Object }) => {
25
+ dispatch(
26
+ new CustomEvent(AmediaUserEvents.UPDATE_STORAGE, { detail: storage })
27
+ );
28
+ };
29
+
30
+ export const saveStorage = (storage: { [index: string]: Object }) => {
31
+ dispatch(new CustomEvent(AmediaUserEvents.SAVE_STORAGE, { detail: storage }));
32
+ };
33
+
34
+ // @ts-ignore
35
+ if (!getGlobalThis().amediaUserTestMode) {
36
+ listenForStateRequests();
37
+ listenForUserAttributeRequests();
38
+ listenForStorageRequests();
39
+ runJanitor();
40
+ }
@@ -0,0 +1,18 @@
1
+ import * as localhostCheck from '../utils/localhostCheck';
2
+ import { runJanitor } from './index';
3
+ import log from '../utils/log';
4
+
5
+ describe('when janitor is started on localhost', () => {
6
+ let logWarnSpy: jest.SpyInstance;
7
+ beforeEach(() => {
8
+ jest.spyOn(localhostCheck, 'isRunningOnLocalhost').mockReturnValue(true);
9
+ logWarnSpy = jest.spyOn(log, 'warn').mockImplementation();
10
+ runJanitor();
11
+ });
12
+
13
+ it('should log a warning', () => {
14
+ expect(logWarnSpy).toBeCalledWith(
15
+ '@amedia/user started on localhost. In this scenario we have no way to resolve any login-, access- or userdata. Expect errors and/or redirects if you try.'
16
+ );
17
+ });
18
+ });
@@ -0,0 +1,59 @@
1
+ import accessCookieRefresher from './jobs/accessCookieRefresher';
2
+ import { removeUserStorage } from './jobs/removeUserStorage';
3
+ import { dispatch, listenWithExclusivity } from '../utils/customEventWrapper';
4
+ import { attemptAutoLoginJob } from '../autologin';
5
+ import daxSubCookieHandler from './jobs/daxsubCookie';
6
+ import log from '../utils/log';
7
+
8
+ const jobs = [
9
+ attemptAutoLoginJob,
10
+ accessCookieRefresher,
11
+ daxSubCookieHandler,
12
+ removeUserStorage,
13
+ ];
14
+
15
+ async function performJobs() {
16
+ if (typeof window === 'undefined') {
17
+ // None of our current jobs make any sense to run in a web worker or other settings without a window object.
18
+ return;
19
+ }
20
+ if (globalThis?.location?.hostname === 'localhost') {
21
+ // Neither if we're running on localhost as this will never be able to resolve any data
22
+ // eslint-disable-next-line no-console
23
+ log.warn(
24
+ '@amedia/user started on localhost. In this scenario we have no way to resolve any login-, access- or userdata. Expect errors and/or redirects if you try.'
25
+ );
26
+ return;
27
+ }
28
+ // eslint-disable-next-line no-restricted-syntax
29
+ for (const job of jobs) {
30
+ // eslint-disable-next-line no-await-in-loop
31
+ await job();
32
+ }
33
+ }
34
+
35
+ enum JanitorEvents {
36
+ LOADED = 'amedia-user.janitor.loaded',
37
+ }
38
+
39
+ declare global {
40
+ interface WindowEventMap {
41
+ [JanitorEvents.LOADED]: CustomEvent<{}>;
42
+ }
43
+ }
44
+
45
+ // We want to ensure that the Janitor is started exactly once. The trick here is to
46
+ // listen to an event end if we receive it exclusively, we know that we are the first
47
+ // to load this script and therefore responsible for running the Janitor once.
48
+ export const runJanitor = async () => {
49
+ let janitorStarted = false;
50
+ listenWithExclusivity(JanitorEvents.LOADED, () => {
51
+ if (janitorStarted) {
52
+ return;
53
+ }
54
+
55
+ janitorStarted = true;
56
+ performJobs();
57
+ });
58
+ dispatch(new CustomEvent(JanitorEvents.LOADED));
59
+ };