@amedia/user 0.13.38 → 0.13.40
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 +252 -24
- package/dist/user.cjs +1 -1
- package/dist/user.cjs.map +3 -3
- package/dist/user.d.ts +5 -5
- package/dist/user.js +1 -1
- package/dist/user.js.map +3 -3
- package/package.json +10 -8
- package/src/actions/authentication.spec.ts +51 -0
- package/src/actions/authentication.ts +43 -0
- package/src/actions/authorization.spec.ts +369 -0
- package/src/actions/authorization.ts +117 -0
- package/src/actions/preferences.spec.ts +41 -0
- package/src/actions/preferences.ts +41 -0
- package/src/actions/storage.ts +22 -0
- package/src/actions/userData.spec.ts +133 -0
- package/src/actions/userData.ts +68 -0
- package/src/autologin/index.spec.ts +239 -0
- package/src/autologin/index.ts +92 -0
- package/src/autologin/thirdPartyCookie.spec.ts +95 -0
- package/src/autologin/thirdPartyCookie.ts +55 -0
- package/src/cache/engines/global.spec.ts +62 -0
- package/src/cache/engines/global.ts +49 -0
- package/src/cache/index.spec.ts +130 -0
- package/src/cache/index.ts +48 -0
- package/src/cache/types.ts +12 -0
- package/src/clients/europa.spec.ts +209 -0
- package/src/clients/europa.ts +183 -0
- package/src/clients/jupiter.spec.ts +179 -0
- package/src/clients/jupiter.ts +119 -0
- package/src/clients/mercury.spec.ts +59 -0
- package/src/clients/mercury.ts +48 -0
- package/src/clients/mercuryLog.spec.ts +20 -0
- package/src/clients/mercuryLog.ts +67 -0
- package/src/clients/nebula.spec.ts +70 -0
- package/src/clients/nebula.ts +83 -0
- package/src/clients/pluto.spec.ts +165 -0
- package/src/clients/pluto.ts +118 -0
- package/src/eventHandlers/activation.ts +36 -0
- package/src/eventHandlers/authentication.ts +34 -0
- package/src/eventHandlers/eventMap.ts +34 -0
- package/src/eventHandlers/state.spec.ts +48 -0
- package/src/eventHandlers/state.ts +23 -0
- package/src/eventHandlers/storage.spec.ts +114 -0
- package/src/eventHandlers/storage.ts +45 -0
- package/src/eventHandlers/userAttributes.spec.ts +93 -0
- package/src/eventHandlers/userAttributes.ts +31 -0
- package/src/exceptions/FetchTimeoutError.ts +8 -0
- package/src/exceptions/PaywallError.ts +16 -0
- package/src/index-authentcation.spec.ts +26 -0
- package/src/index-storage.spec.ts +144 -0
- package/src/index-subscription.spec.ts +213 -0
- package/src/index.ts +40 -0
- package/src/janitor/index.spec.ts +18 -0
- package/src/janitor/index.ts +59 -0
- package/src/janitor/jobs/accessCookieRefresher.spec.ts +213 -0
- package/src/janitor/jobs/accessCookieRefresher.ts +98 -0
- package/src/janitor/jobs/daxsubCookie.spec.ts +109 -0
- package/src/janitor/jobs/daxsubCookie.ts +38 -0
- package/src/janitor/jobs/removeUserStorage.spec.ts +34 -0
- package/src/janitor/jobs/removeUserStorage.ts +12 -0
- package/src/lib/PaywallUnlockRequest.spec.ts +363 -0
- package/src/lib/PaywallUnlockRequest.ts +143 -0
- package/src/lib/UserDataRequest.spec.ts +231 -0
- package/src/lib/UserDataRequest.ts +267 -0
- package/src/lib/cookie.ts +121 -0
- package/src/lib/rpc.ts +19 -0
- package/src/store/index.spec.ts +49 -0
- package/src/store/index.ts +64 -0
- package/src/types.ts +22 -0
- package/src/utils/customEventWrapper.ts +58 -0
- package/src/utils/debouncer.spec.ts +252 -0
- package/src/utils/debouncer.ts +123 -0
- package/src/utils/deepEquals.ts +42 -0
- package/src/utils/expectBootLogs.ts +9 -0
- package/src/utils/getGlobalThis.ts +17 -0
- package/src/utils/getOrUpdate.spec.ts +166 -0
- package/src/utils/getOrUpdate.ts +66 -0
- package/src/utils/localhostCheck.ts +10 -0
- package/src/utils/location.ts +31 -0
- package/src/utils/log.spec.ts +148 -0
- package/src/utils/log.ts +124 -0
- package/src/utils/loginState.ts +24 -0
- package/src/utils/networkError.ts +11 -0
- package/src/utils/queue.ts +32 -0
- package/src/utils/type.ts +19 -0
- package/src/utils/url.spec.ts +33 -0
- package/src/utils/url.ts +37 -0
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import deepmerge from 'ts-deepmerge';
|
|
2
|
+
import { State, UserAttributes } from '../types';
|
|
3
|
+
|
|
4
|
+
type StoreDataMap = {
|
|
5
|
+
userAttributes: UserAttributes;
|
|
6
|
+
state: State;
|
|
7
|
+
storage: { [namespace: string]: { [index: string]: unknown } };
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
type Listener<T> = (data: T) => void;
|
|
11
|
+
type Listeners<T = StoreDataMap> = {
|
|
12
|
+
[key in keyof T]?: Array<Listener<T[key]>>;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
const store: { [index in keyof StoreDataMap]?: StoreDataMap[index] } = {};
|
|
16
|
+
const listeners: Listeners = {};
|
|
17
|
+
|
|
18
|
+
const emit = (key: keyof StoreDataMap): void => {
|
|
19
|
+
// @ts-ignore
|
|
20
|
+
listeners[key]?.forEach((listener) => listener(store[key]));
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const on = <K extends keyof StoreDataMap, T = StoreDataMap[K]>(
|
|
24
|
+
key: K,
|
|
25
|
+
listener: Listener<T>
|
|
26
|
+
) => {
|
|
27
|
+
if (!listeners[key]) {
|
|
28
|
+
listeners[key] = [];
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// @ts-ignore todo How can this possibly be undefined given the statement above?
|
|
32
|
+
listeners[key].push(listener);
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
const set = (key: keyof StoreDataMap, value: StoreDataMap[typeof key]) => {
|
|
36
|
+
// @ts-ignore todo Not sure why this type fails.
|
|
37
|
+
store[key] = value;
|
|
38
|
+
emit(key);
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const merge = <K extends keyof StoreDataMap>(
|
|
42
|
+
key: K,
|
|
43
|
+
value: Partial<StoreDataMap[K]>
|
|
44
|
+
) => {
|
|
45
|
+
// @ts-ignore
|
|
46
|
+
set(key, deepmerge(store[key] || {}, value));
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
const get = <K extends keyof StoreDataMap, T = StoreDataMap[K]>(
|
|
50
|
+
key: K
|
|
51
|
+
): T | undefined =>
|
|
52
|
+
// @ts-ignore Not sure why this fails
|
|
53
|
+
store[key];
|
|
54
|
+
const clear = () => {
|
|
55
|
+
store.state = undefined;
|
|
56
|
+
store.userAttributes = undefined;
|
|
57
|
+
store.storage = undefined;
|
|
58
|
+
Object.keys(listeners).forEach((key) => {
|
|
59
|
+
// @ts-ignore
|
|
60
|
+
listeners[key] = [];
|
|
61
|
+
});
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
export default { merge, set, get, on, clear };
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export type UserAttributes = {
|
|
2
|
+
uuid?: string | null;
|
|
3
|
+
name?: string | null;
|
|
4
|
+
email?: string | null;
|
|
5
|
+
phone?: string | null;
|
|
6
|
+
trackingKey?: string | null;
|
|
7
|
+
extraData?: { [index: string]: unknown };
|
|
8
|
+
access?: string[];
|
|
9
|
+
privacyPreferences?: PrivacyPreferences | null;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export type State = {
|
|
13
|
+
isLoggedIn: boolean;
|
|
14
|
+
emergencyMode?: string[];
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export type PrivacyPreferences = {
|
|
18
|
+
allowResearchUsage: boolean;
|
|
19
|
+
connectWithSurveys: boolean;
|
|
20
|
+
personalizedAds: boolean;
|
|
21
|
+
personalizedContent: boolean;
|
|
22
|
+
};
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import log from './log';
|
|
2
|
+
|
|
3
|
+
let rootElement: Window | Element | typeof globalThis;
|
|
4
|
+
|
|
5
|
+
export const setRootElement = (element: typeof rootElement) => {
|
|
6
|
+
rootElement = element;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
setRootElement(globalThis);
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* This listener MUST be used by all listeners in ../eventHandlers
|
|
13
|
+
* We want to ensure that only _one_ instance of this package actually
|
|
14
|
+
* orchestrates data and performs requests against backends.
|
|
15
|
+
* @param type
|
|
16
|
+
* @param listener
|
|
17
|
+
*/
|
|
18
|
+
export const listenWithExclusivity = <T extends keyof WindowEventMap>(
|
|
19
|
+
type: T,
|
|
20
|
+
listener: (this: typeof rootElement, ev: WindowEventMap[T]) => void
|
|
21
|
+
) => {
|
|
22
|
+
if (!rootElement) {
|
|
23
|
+
log.error('No root element to bind event to!');
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
rootElement.addEventListener(type, (evt) => {
|
|
27
|
+
// Since this lib might me loaded multiple times, we need to prevent all other processing of this event.
|
|
28
|
+
evt.stopImmediatePropagation();
|
|
29
|
+
listener.apply(rootElement, [evt as WindowEventMap[T]]);
|
|
30
|
+
});
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Use this for response listeners. (../index.ts) where we want
|
|
35
|
+
* all updates with data - no matter who sent the actual request
|
|
36
|
+
* @param type
|
|
37
|
+
* @param listener
|
|
38
|
+
*/
|
|
39
|
+
export const listenForAllEvents = <T extends keyof WindowEventMap>(
|
|
40
|
+
type: T,
|
|
41
|
+
listener: (this: typeof rootElement, ev: WindowEventMap[T]) => void
|
|
42
|
+
) => {
|
|
43
|
+
if (!rootElement) {
|
|
44
|
+
log.error('No root element to bind event to!');
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
rootElement.addEventListener(type, (evt) => {
|
|
48
|
+
listener.apply(rootElement, [evt as WindowEventMap[T]]);
|
|
49
|
+
});
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
export const dispatch = <T extends Event>(event: T) => {
|
|
53
|
+
if (!rootElement) {
|
|
54
|
+
log.error('No root element to dispatch event on!');
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
rootElement.dispatchEvent(event);
|
|
58
|
+
};
|
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
import Debouncer from './debouncer';
|
|
2
|
+
|
|
3
|
+
describe('Debouncer', () => {
|
|
4
|
+
describe('when params is list of arrays and response is key value object', () => {
|
|
5
|
+
let debouncer: Debouncer<string[], Record<string, string>>;
|
|
6
|
+
const params1 = ['foo', 'bar'];
|
|
7
|
+
const params2 = ['baz', '1337'];
|
|
8
|
+
describe('when workload succeeds', () => {
|
|
9
|
+
beforeEach(() => {
|
|
10
|
+
debouncer = new Debouncer<string[], Record<string, string>>(
|
|
11
|
+
10,
|
|
12
|
+
[],
|
|
13
|
+
Debouncer.stringListParamsAccumulator,
|
|
14
|
+
(params) =>
|
|
15
|
+
Promise.resolve(
|
|
16
|
+
params.reduce(
|
|
17
|
+
(map, param): Record<string, string> => ({
|
|
18
|
+
...map,
|
|
19
|
+
[param]: param.toUpperCase(),
|
|
20
|
+
}),
|
|
21
|
+
{} as Record<string, string>
|
|
22
|
+
)
|
|
23
|
+
)
|
|
24
|
+
);
|
|
25
|
+
});
|
|
26
|
+
describe('when called in quick succession', () => {
|
|
27
|
+
let result1: Record<string, string>;
|
|
28
|
+
let result2: Record<string, string>;
|
|
29
|
+
const expectedResult = {
|
|
30
|
+
foo: 'FOO',
|
|
31
|
+
bar: 'BAR',
|
|
32
|
+
baz: 'BAZ',
|
|
33
|
+
1337: '1337',
|
|
34
|
+
};
|
|
35
|
+
beforeEach(async () => {
|
|
36
|
+
const promise1 = debouncer.perform(params1);
|
|
37
|
+
const promise2 = debouncer.perform(params2);
|
|
38
|
+
result1 = await promise1;
|
|
39
|
+
result2 = await promise2;
|
|
40
|
+
});
|
|
41
|
+
it('Resolution to first promise is complete set', () => {
|
|
42
|
+
expect(result1).toEqual(expectedResult);
|
|
43
|
+
});
|
|
44
|
+
it('Resolution to second promise is complete set', () => {
|
|
45
|
+
expect(result2).toEqual(expectedResult);
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
describe('when waiting for first resolution before second call', () => {
|
|
49
|
+
let result1: Record<string, string>;
|
|
50
|
+
let result2: Record<string, string>;
|
|
51
|
+
const expectedResult1 = {
|
|
52
|
+
foo: 'FOO',
|
|
53
|
+
bar: 'BAR',
|
|
54
|
+
};
|
|
55
|
+
const expectedResult2 = {
|
|
56
|
+
baz: 'BAZ',
|
|
57
|
+
1337: '1337',
|
|
58
|
+
};
|
|
59
|
+
beforeEach(async () => {
|
|
60
|
+
result1 = await debouncer.perform(params1);
|
|
61
|
+
result2 = await debouncer.perform(params2);
|
|
62
|
+
});
|
|
63
|
+
it('Resolution to first promise is only result of first set', () => {
|
|
64
|
+
expect(result1).toEqual(expectedResult1);
|
|
65
|
+
});
|
|
66
|
+
it('Resolution to second promise is only result of second set', () => {
|
|
67
|
+
expect(result2).toEqual(expectedResult2);
|
|
68
|
+
});
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
describe('when workload fails', () => {
|
|
72
|
+
beforeEach(() => {
|
|
73
|
+
debouncer = new Debouncer<string[], Record<string, string>>(
|
|
74
|
+
10,
|
|
75
|
+
[],
|
|
76
|
+
Debouncer.stringListParamsAccumulator,
|
|
77
|
+
(params) =>
|
|
78
|
+
Promise.reject(new Error(`Cannot handle ${params.join(',')}`))
|
|
79
|
+
);
|
|
80
|
+
});
|
|
81
|
+
describe('when called in quick succession', () => {
|
|
82
|
+
let result1: string;
|
|
83
|
+
let result2: string;
|
|
84
|
+
beforeEach(async () => {
|
|
85
|
+
const promise1 = debouncer.perform(params1);
|
|
86
|
+
const promise2 = debouncer.perform(params2);
|
|
87
|
+
try {
|
|
88
|
+
await promise1;
|
|
89
|
+
} catch (e) {
|
|
90
|
+
result1 = e.message;
|
|
91
|
+
}
|
|
92
|
+
try {
|
|
93
|
+
await promise2;
|
|
94
|
+
} catch (e) {
|
|
95
|
+
result2 = e.message;
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
it('Resolution to first promise is error handling all params', () => {
|
|
99
|
+
expect(result1).toEqual('Cannot handle foo,bar,baz,1337');
|
|
100
|
+
});
|
|
101
|
+
it('Resolution to second promise is error handling all params', async () => {
|
|
102
|
+
expect(result2).toEqual('Cannot handle foo,bar,baz,1337');
|
|
103
|
+
});
|
|
104
|
+
});
|
|
105
|
+
describe('when waiting for first resolution before second call', () => {
|
|
106
|
+
let result1: string;
|
|
107
|
+
let result2: string;
|
|
108
|
+
beforeEach(async () => {
|
|
109
|
+
try {
|
|
110
|
+
await debouncer.perform(params1);
|
|
111
|
+
} catch (e) {
|
|
112
|
+
result1 = e.message;
|
|
113
|
+
}
|
|
114
|
+
try {
|
|
115
|
+
await debouncer.perform(params2);
|
|
116
|
+
} catch (e) {
|
|
117
|
+
result2 = e.message;
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
it('Resolution to first promise is error handling first set', () => {
|
|
121
|
+
expect(result1).toEqual('Cannot handle foo,bar');
|
|
122
|
+
});
|
|
123
|
+
it('Resolution to second promise is error handling second set', () => {
|
|
124
|
+
expect(result2).toEqual('Cannot handle baz,1337');
|
|
125
|
+
});
|
|
126
|
+
});
|
|
127
|
+
});
|
|
128
|
+
});
|
|
129
|
+
describe('when params is key value object response is list of strings', () => {
|
|
130
|
+
let debouncer: Debouncer<Record<string, string>, string[]>;
|
|
131
|
+
const params1 = {
|
|
132
|
+
foo: 'FOO',
|
|
133
|
+
bar: 'BAR',
|
|
134
|
+
};
|
|
135
|
+
const params2 = {
|
|
136
|
+
baz: 'BAZ',
|
|
137
|
+
1337: '1337',
|
|
138
|
+
};
|
|
139
|
+
describe('when workload succeeds', () => {
|
|
140
|
+
beforeEach(() => {
|
|
141
|
+
debouncer = new Debouncer<Record<string, string>, string[]>(
|
|
142
|
+
10,
|
|
143
|
+
{},
|
|
144
|
+
Debouncer.stringMapParamsAccumulator,
|
|
145
|
+
(params) =>
|
|
146
|
+
Promise.resolve(Object.entries(params).map(([k, v]) => `${k}=${v}`))
|
|
147
|
+
);
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
describe('when called in quick succession', () => {
|
|
151
|
+
let result1: string[];
|
|
152
|
+
let result2: string[];
|
|
153
|
+
const expectedResult = ['1337=1337', 'foo=FOO', 'bar=BAR', 'baz=BAZ'];
|
|
154
|
+
beforeEach(async () => {
|
|
155
|
+
const promise1 = debouncer.perform(params1);
|
|
156
|
+
const promise2 = debouncer.perform(params2);
|
|
157
|
+
result1 = await promise1;
|
|
158
|
+
result2 = await promise2;
|
|
159
|
+
});
|
|
160
|
+
it('Resolution to first promise is complete set', () => {
|
|
161
|
+
expect(result1).toEqual(expectedResult);
|
|
162
|
+
});
|
|
163
|
+
it('Resolution to second promise is complete set', () => {
|
|
164
|
+
expect(result2).toEqual(expectedResult);
|
|
165
|
+
});
|
|
166
|
+
});
|
|
167
|
+
describe('when waiting for first resolution before second call', () => {
|
|
168
|
+
let result1: string[];
|
|
169
|
+
let result2: string[];
|
|
170
|
+
const expectedResult1 = ['foo=FOO', 'bar=BAR'];
|
|
171
|
+
const expectedResult2 = ['1337=1337', 'baz=BAZ'];
|
|
172
|
+
beforeEach(async () => {
|
|
173
|
+
result1 = await debouncer.perform(params1);
|
|
174
|
+
result2 = await debouncer.perform(params2);
|
|
175
|
+
});
|
|
176
|
+
it('Resolution to first promise is only result of first set', () => {
|
|
177
|
+
expect(result1).toEqual(expectedResult1);
|
|
178
|
+
});
|
|
179
|
+
it('Resolution to second promise is only result of second set', () => {
|
|
180
|
+
expect(result2).toEqual(expectedResult2);
|
|
181
|
+
});
|
|
182
|
+
});
|
|
183
|
+
});
|
|
184
|
+
describe('when workload fails', () => {
|
|
185
|
+
beforeEach(() => {
|
|
186
|
+
debouncer = new Debouncer<Record<string, string>, string[]>(
|
|
187
|
+
10,
|
|
188
|
+
{},
|
|
189
|
+
Debouncer.stringMapParamsAccumulator,
|
|
190
|
+
(params) =>
|
|
191
|
+
Promise.reject(
|
|
192
|
+
new Error(
|
|
193
|
+
`Cannot handle ${Object.entries(params)
|
|
194
|
+
.map(([k, v]) => `${k}=${v}`)
|
|
195
|
+
.join(',')}`
|
|
196
|
+
)
|
|
197
|
+
)
|
|
198
|
+
);
|
|
199
|
+
});
|
|
200
|
+
describe('when called in quick succession', () => {
|
|
201
|
+
let result1: string;
|
|
202
|
+
let result2: string;
|
|
203
|
+
beforeEach(async () => {
|
|
204
|
+
const promise1 = debouncer.perform(params1);
|
|
205
|
+
const promise2 = debouncer.perform(params2);
|
|
206
|
+
try {
|
|
207
|
+
await promise1;
|
|
208
|
+
} catch (e) {
|
|
209
|
+
result1 = e.message;
|
|
210
|
+
}
|
|
211
|
+
try {
|
|
212
|
+
await promise2;
|
|
213
|
+
} catch (e) {
|
|
214
|
+
result2 = e.message;
|
|
215
|
+
}
|
|
216
|
+
});
|
|
217
|
+
it('Resolution to first promise is error handling all params', () => {
|
|
218
|
+
expect(result1).toEqual(
|
|
219
|
+
'Cannot handle 1337=1337,foo=FOO,bar=BAR,baz=BAZ'
|
|
220
|
+
);
|
|
221
|
+
});
|
|
222
|
+
it('Resolution to second promise is error handling all params', async () => {
|
|
223
|
+
expect(result2).toEqual(
|
|
224
|
+
'Cannot handle 1337=1337,foo=FOO,bar=BAR,baz=BAZ'
|
|
225
|
+
);
|
|
226
|
+
});
|
|
227
|
+
});
|
|
228
|
+
describe('when waiting for first resolution before second call', () => {
|
|
229
|
+
let result1: string;
|
|
230
|
+
let result2: string;
|
|
231
|
+
beforeEach(async () => {
|
|
232
|
+
try {
|
|
233
|
+
await debouncer.perform(params1);
|
|
234
|
+
} catch (e) {
|
|
235
|
+
result1 = e.message;
|
|
236
|
+
}
|
|
237
|
+
try {
|
|
238
|
+
await debouncer.perform(params2);
|
|
239
|
+
} catch (e) {
|
|
240
|
+
result2 = e.message;
|
|
241
|
+
}
|
|
242
|
+
});
|
|
243
|
+
it('Resolution to first promise is error handling first set', () => {
|
|
244
|
+
expect(result1).toEqual('Cannot handle foo=FOO,bar=BAR');
|
|
245
|
+
});
|
|
246
|
+
it('Resolution to second promise is error handling second set', () => {
|
|
247
|
+
expect(result2).toEqual('Cannot handle 1337=1337,baz=BAZ');
|
|
248
|
+
});
|
|
249
|
+
});
|
|
250
|
+
});
|
|
251
|
+
});
|
|
252
|
+
});
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import deepmerge from 'ts-deepmerge';
|
|
2
|
+
|
|
3
|
+
/** Naive implementation of deep copy needed for copying parameters when
|
|
4
|
+
* merging multiple calls to perform.
|
|
5
|
+
*
|
|
6
|
+
* @param obj object to copy deeply
|
|
7
|
+
*/
|
|
8
|
+
function deepCopy<T>(obj: T): T {
|
|
9
|
+
if (typeof obj === 'string' || obj instanceof String) {
|
|
10
|
+
return `${obj}` as unknown as T;
|
|
11
|
+
}
|
|
12
|
+
if (obj instanceof Array) {
|
|
13
|
+
return [...obj].map(deepCopy) as unknown as T;
|
|
14
|
+
}
|
|
15
|
+
return Object.entries(obj).reduce(
|
|
16
|
+
(copy, [k, v]) => ({
|
|
17
|
+
...copy,
|
|
18
|
+
[k]: deepCopy(v),
|
|
19
|
+
}),
|
|
20
|
+
{}
|
|
21
|
+
) as T;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* The Debouncer can be used when we want a clean way to say 'I want to fetch this and that, and once it's done,
|
|
26
|
+
* resolve my promise', but instead of the fetch being performed right away, we want to wait for more users to
|
|
27
|
+
* request other stuff. This will keep us from performing lots of individual fetches to the backend.
|
|
28
|
+
*
|
|
29
|
+
* Our main use case is a standard publication front page where one component on the page requests the euucp namespace
|
|
30
|
+
* from Europa, and then another component requests the privacy_preferences namespace. Instead of the Europa client
|
|
31
|
+
* calling two fetches to the server, one for euucp and another for privacy_preferences, we want the Debouncer to
|
|
32
|
+
* aggregate these calls into one call to the backend, that requests both euucp AND privacy_preferences, so the response
|
|
33
|
+
* to both components will be both namespaces (which the Europa client can filter before it actually reaches the
|
|
34
|
+
* component).
|
|
35
|
+
*/
|
|
36
|
+
class Debouncer<ParamsType, ResultType> {
|
|
37
|
+
private timer: ReturnType<typeof setTimeout> | undefined;
|
|
38
|
+
|
|
39
|
+
private params: ParamsType;
|
|
40
|
+
|
|
41
|
+
private queue: {
|
|
42
|
+
resolve: (arg0: any) => void;
|
|
43
|
+
reject: (reason?: any) => void;
|
|
44
|
+
}[] = [];
|
|
45
|
+
|
|
46
|
+
constructor(
|
|
47
|
+
// The number of milliseconds to wait for new calls to perform before actually performing workload
|
|
48
|
+
private timeoutMs: number,
|
|
49
|
+
|
|
50
|
+
// Default parameters if none provided, normally an empty object or list
|
|
51
|
+
private defaultParams: ParamsType,
|
|
52
|
+
|
|
53
|
+
// Function that can accumulate parameters from successive calls to perform, see examples below
|
|
54
|
+
private paramAccumulator: (
|
|
55
|
+
previousParams: ParamsType,
|
|
56
|
+
newParams: ParamsType
|
|
57
|
+
) => ParamsType,
|
|
58
|
+
|
|
59
|
+
// The workload we want to perform on accumulated parameters once timeout has been reached
|
|
60
|
+
private workload: (params: ParamsType) => Promise<ResultType>
|
|
61
|
+
) {
|
|
62
|
+
this.params = deepCopy(defaultParams);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// Caller indicates that it wants to perform workload on a set of parameters. We will wait this.timeoutMs milliseconds
|
|
66
|
+
// and see if we get more calls to perform workload, if we do, we accumulate parameters and wait this.timeoutMs
|
|
67
|
+
// more milliseconds until we finally perform workload and resolve promises.
|
|
68
|
+
perform(newParams: ParamsType): Promise<ResultType> {
|
|
69
|
+
return new Promise((resolve, reject) => {
|
|
70
|
+
if (this.timer) {
|
|
71
|
+
clearTimeout(this.timer);
|
|
72
|
+
}
|
|
73
|
+
this.params = this.paramAccumulator(this.params, newParams);
|
|
74
|
+
this.queue.push({ resolve, reject });
|
|
75
|
+
this.timer = setTimeout(async () => {
|
|
76
|
+
// Copy current state to use for performing workload and resolving promises after workload completes
|
|
77
|
+
const params = deepCopy(this.params);
|
|
78
|
+
const promisesToKeep = [...this.queue];
|
|
79
|
+
|
|
80
|
+
// Setup new clean state for future calls to perform
|
|
81
|
+
this.params = this.defaultParams;
|
|
82
|
+
this.queue = [];
|
|
83
|
+
|
|
84
|
+
// Perform workload and resolve promises for all callers with result
|
|
85
|
+
this.workload(params)
|
|
86
|
+
.then((result) => {
|
|
87
|
+
promisesToKeep.forEach((promise) => {
|
|
88
|
+
promise.resolve(result);
|
|
89
|
+
});
|
|
90
|
+
})
|
|
91
|
+
.catch((e) => {
|
|
92
|
+
// If workload fails, we need to reject all promises so callers won't be left hanging
|
|
93
|
+
promisesToKeep.forEach((promise) => {
|
|
94
|
+
promise.reject(e);
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
}, this.timeoutMs);
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// Accumulate parameters as lists of strings
|
|
102
|
+
static stringListParamsAccumulator(
|
|
103
|
+
accumulatedParams: string[],
|
|
104
|
+
newParams: string[]
|
|
105
|
+
) {
|
|
106
|
+
newParams.forEach((param) => {
|
|
107
|
+
if (!accumulatedParams.includes(param)) {
|
|
108
|
+
accumulatedParams.push(param);
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
return accumulatedParams;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// Accumulate parameters as map with string keys
|
|
115
|
+
static stringMapParamsAccumulator(
|
|
116
|
+
previousParams: Record<string, any>,
|
|
117
|
+
newParams: Record<string, any>
|
|
118
|
+
) {
|
|
119
|
+
return deepmerge(previousParams, newParams);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export default Debouncer;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @see https://dev.to/sanderdebr/deep-equality-checking-of-objects-in-vanilla-javascript-5592
|
|
3
|
+
*/
|
|
4
|
+
import { isFunction, isRecord } from './type';
|
|
5
|
+
|
|
6
|
+
export const deepEquals = (a: unknown, b: unknown): boolean => {
|
|
7
|
+
if (a === b) {
|
|
8
|
+
return true;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
if (!isRecord(a) || !isRecord(b) || a === null || b === null) {
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const keysA = Object.keys(a);
|
|
16
|
+
const keysB = Object.keys(b);
|
|
17
|
+
|
|
18
|
+
if (keysA.length !== keysB.length) {
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// eslint-disable-next-line no-restricted-syntax
|
|
23
|
+
for (const key of keysA) {
|
|
24
|
+
if (!keysB.includes(key)) {
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const currentA = a[key];
|
|
29
|
+
const currentB = b[key];
|
|
30
|
+
if (isFunction(currentA) || isFunction(currentB)) {
|
|
31
|
+
return isFunction(currentA) && isFunction(currentB)
|
|
32
|
+
? currentA.toString() !== currentB.toString()
|
|
33
|
+
: false;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (!deepEquals(currentA, currentB)) {
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return true;
|
|
42
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import log from './log';
|
|
2
|
+
|
|
3
|
+
jest.spyOn(log, 'warn').mockImplementation();
|
|
4
|
+
|
|
5
|
+
export default () => {
|
|
6
|
+
expect(log.warn).toHaveBeenCalledWith(
|
|
7
|
+
'@amedia/user started on localhost. In this scenario we have no way to resolve any login-, access- or userdata. Expect errors or redirects if you try.'
|
|
8
|
+
);
|
|
9
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/* eslint-disable no-restricted-globals */
|
|
2
|
+
const getGlobalThis = () => {
|
|
3
|
+
if (typeof globalThis !== 'undefined') {
|
|
4
|
+
return globalThis;
|
|
5
|
+
}
|
|
6
|
+
if (typeof window !== 'undefined') {
|
|
7
|
+
return window;
|
|
8
|
+
}
|
|
9
|
+
if (typeof global !== 'undefined') {
|
|
10
|
+
return global;
|
|
11
|
+
}
|
|
12
|
+
if (typeof self !== 'undefined') {
|
|
13
|
+
return self;
|
|
14
|
+
}
|
|
15
|
+
throw new Error('@amedia/user: Unable to acquire any variant of globalThis');
|
|
16
|
+
};
|
|
17
|
+
export default getGlobalThis;
|