@amedia/user 0.14.18 → 0.14.20
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/package.json +2 -3
- package/src/actions/authentication.spec.ts +0 -51
- package/src/actions/authentication.ts +0 -49
- package/src/actions/authorization.spec.ts +0 -377
- package/src/actions/authorization.ts +0 -120
- package/src/actions/preferences.spec.ts +0 -41
- package/src/actions/preferences.ts +0 -41
- package/src/actions/storage.ts +0 -22
- package/src/actions/userData.spec.ts +0 -133
- package/src/actions/userData.ts +0 -68
- package/src/autologin/index.spec.ts +0 -264
- package/src/autologin/index.ts +0 -113
- package/src/autologin/thirdPartyCookie.spec.ts +0 -95
- package/src/autologin/thirdPartyCookie.ts +0 -55
- package/src/cache/engines/global.spec.ts +0 -62
- package/src/cache/engines/global.ts +0 -49
- package/src/cache/index.spec.ts +0 -132
- package/src/cache/index.ts +0 -48
- package/src/cache/types.ts +0 -12
- package/src/clients/europa.spec.ts +0 -209
- package/src/clients/europa.ts +0 -183
- package/src/clients/jupiter.spec.ts +0 -178
- package/src/clients/jupiter.ts +0 -119
- package/src/clients/mercury.spec.ts +0 -126
- package/src/clients/mercury.ts +0 -105
- package/src/clients/mercuryLog.spec.ts +0 -20
- package/src/clients/mercuryLog.ts +0 -67
- package/src/clients/nebula.spec.ts +0 -70
- package/src/clients/nebula.ts +0 -83
- package/src/clients/pluto.spec.ts +0 -262
- package/src/clients/pluto.ts +0 -126
- package/src/eventHandlers/activation.ts +0 -40
- package/src/eventHandlers/authentication.ts +0 -34
- package/src/eventHandlers/eventMap.ts +0 -40
- package/src/eventHandlers/navigation.spec.ts +0 -189
- package/src/eventHandlers/navigation.ts +0 -58
- package/src/eventHandlers/state.spec.ts +0 -48
- package/src/eventHandlers/state.ts +0 -23
- package/src/eventHandlers/storage.spec.ts +0 -114
- package/src/eventHandlers/storage.ts +0 -45
- package/src/eventHandlers/userAttributes.spec.ts +0 -93
- package/src/eventHandlers/userAttributes.ts +0 -31
- package/src/exceptions/FetchTimeoutError.ts +0 -8
- package/src/exceptions/PaywallError.ts +0 -16
- package/src/index-authentcation.spec.ts +0 -26
- package/src/index-storage.spec.ts +0 -144
- package/src/index-subscription.spec.ts +0 -213
- package/src/index.ts +0 -46
- package/src/janitor/index.spec.ts +0 -18
- package/src/janitor/index.ts +0 -60
- package/src/janitor/jobs/accessCookieRefresher.spec.ts +0 -213
- package/src/janitor/jobs/accessCookieRefresher.ts +0 -96
- package/src/janitor/jobs/daxsubCookie.spec.ts +0 -109
- package/src/janitor/jobs/daxsubCookie.ts +0 -38
- package/src/janitor/jobs/removeUserStorage.spec.ts +0 -34
- package/src/janitor/jobs/removeUserStorage.ts +0 -12
- package/src/lib/PaywallUnlockRequest.spec.ts +0 -446
- package/src/lib/PaywallUnlockRequest.ts +0 -161
- package/src/lib/UserDataRequest.spec.ts +0 -231
- package/src/lib/UserDataRequest.ts +0 -267
- package/src/lib/cookie.ts +0 -121
- package/src/lib/rpc.ts +0 -19
- package/src/store/index.spec.ts +0 -49
- package/src/store/index.ts +0 -66
- package/src/types.ts +0 -22
- package/src/utils/customEventWrapper.spec.ts +0 -237
- package/src/utils/customEventWrapper.ts +0 -77
- package/src/utils/debouncer.spec.ts +0 -252
- package/src/utils/debouncer.ts +0 -123
- package/src/utils/deepEquals.ts +0 -42
- package/src/utils/expectBootLogs.ts +0 -9
- package/src/utils/getGlobalThis.ts +0 -17
- package/src/utils/getOrUpdate.spec.ts +0 -166
- package/src/utils/getOrUpdate.ts +0 -66
- package/src/utils/localhostCheck.ts +0 -10
- package/src/utils/location.spec.ts +0 -61
- package/src/utils/location.ts +0 -16
- package/src/utils/log.spec.ts +0 -177
- package/src/utils/log.ts +0 -112
- package/src/utils/loginState.ts +0 -24
- package/src/utils/networkError.ts +0 -11
- package/src/utils/queue.ts +0 -32
- package/src/utils/type.ts +0 -19
- package/src/utils/url.spec.ts +0 -33
- package/src/utils/url.ts +0 -37
package/src/utils/debouncer.ts
DELETED
|
@@ -1,123 +0,0 @@
|
|
|
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 site 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;
|
package/src/utils/deepEquals.ts
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
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
|
-
};
|
|
@@ -1,9 +0,0 @@
|
|
|
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
|
-
};
|
|
@@ -1,17 +0,0 @@
|
|
|
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;
|
|
@@ -1,166 +0,0 @@
|
|
|
1
|
-
import { Partial, String } from 'runtypes';
|
|
2
|
-
import cache from '../cache';
|
|
3
|
-
import getOrUpdate from './getOrUpdate';
|
|
4
|
-
import queue from './queue';
|
|
5
|
-
import spyOn = jest.spyOn;
|
|
6
|
-
|
|
7
|
-
const schema = Partial({ field: String, extraField: String });
|
|
8
|
-
|
|
9
|
-
beforeEach(async () => {
|
|
10
|
-
await cache.clear();
|
|
11
|
-
});
|
|
12
|
-
describe('getOrUpdate', () => {
|
|
13
|
-
it('should fetch if no cache is available', () => {
|
|
14
|
-
const fetcher = jest.fn().mockResolvedValue({
|
|
15
|
-
ttl: 0,
|
|
16
|
-
data: { field: 'fetchedValue' },
|
|
17
|
-
});
|
|
18
|
-
expect(getOrUpdate('client', schema, fetcher))
|
|
19
|
-
.resolves.toEqual({
|
|
20
|
-
field: 'fetchedValue',
|
|
21
|
-
})
|
|
22
|
-
.then(expect(fetcher).toHaveBeenCalled);
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
it('should add to cache after fetching', async () => {
|
|
26
|
-
const cacheSpy = spyOn(cache, 'set');
|
|
27
|
-
await getOrUpdate(
|
|
28
|
-
'client',
|
|
29
|
-
schema,
|
|
30
|
-
jest.fn().mockResolvedValue({
|
|
31
|
-
ttl: 10,
|
|
32
|
-
data: { field: 'value' },
|
|
33
|
-
})
|
|
34
|
-
);
|
|
35
|
-
expect(cacheSpy).toHaveBeenCalledWith('client', { field: 'value' }, 10);
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
it('should use cache if available instead of fetching', () => {
|
|
39
|
-
spyOn(cache, 'get').mockResolvedValue({
|
|
40
|
-
field: 'cachedValue',
|
|
41
|
-
});
|
|
42
|
-
const fetcher = jest.fn().mockResolvedValue({
|
|
43
|
-
ttl: 0,
|
|
44
|
-
data: { field: 'fetchedValue' },
|
|
45
|
-
});
|
|
46
|
-
expect(getOrUpdate('client', schema, fetcher))
|
|
47
|
-
.resolves.toEqual({ field: 'cachedValue' })
|
|
48
|
-
.then(expect(fetcher).not.toHaveBeenCalled);
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
it('should allow cache qualifier to override valid result', () => {
|
|
52
|
-
spyOn(cache, 'get').mockResolvedValue({
|
|
53
|
-
field: 'cachedValue',
|
|
54
|
-
});
|
|
55
|
-
const fetcher = jest.fn().mockResolvedValue({
|
|
56
|
-
ttl: 0,
|
|
57
|
-
data: { field: 'fetchedValue' },
|
|
58
|
-
});
|
|
59
|
-
const qualifier = jest.fn().mockReturnValue(false);
|
|
60
|
-
expect(getOrUpdate('client', schema, fetcher, qualifier))
|
|
61
|
-
.resolves.toEqual({ field: 'fetchedValue' })
|
|
62
|
-
.then(expect(fetcher).toHaveBeenCalled)
|
|
63
|
-
.then(() =>
|
|
64
|
-
expect(qualifier).toHaveBeenCalledWith({ field: 'cachedValue' })
|
|
65
|
-
);
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
it('should append to existing cache if any', async () => {
|
|
69
|
-
spyOn(cache, 'get').mockResolvedValue({
|
|
70
|
-
field: 'cachedValue',
|
|
71
|
-
});
|
|
72
|
-
const cacheSetSpy = spyOn(cache, 'set');
|
|
73
|
-
await getOrUpdate(
|
|
74
|
-
'client',
|
|
75
|
-
schema,
|
|
76
|
-
() => Promise.resolve({ ttl: 0, data: { extraField: 'extraValue' } }),
|
|
77
|
-
() => false
|
|
78
|
-
);
|
|
79
|
-
expect(cacheSetSpy).toHaveBeenCalledWith(
|
|
80
|
-
'client',
|
|
81
|
-
{ field: 'cachedValue', extraField: 'extraValue' },
|
|
82
|
-
0
|
|
83
|
-
);
|
|
84
|
-
});
|
|
85
|
-
|
|
86
|
-
it('should resolve queue on cached data', (done) => {
|
|
87
|
-
let resolvePromise: Function;
|
|
88
|
-
const promise = new Promise((resolve) => {
|
|
89
|
-
resolvePromise = resolve;
|
|
90
|
-
});
|
|
91
|
-
spyOn(cache, 'get').mockReturnValue(promise);
|
|
92
|
-
|
|
93
|
-
expect(
|
|
94
|
-
Promise.all([
|
|
95
|
-
getOrUpdate('client', schema, jest.fn()),
|
|
96
|
-
getOrUpdate('client', schema, jest.fn()),
|
|
97
|
-
getOrUpdate('client', schema, jest.fn()),
|
|
98
|
-
])
|
|
99
|
-
)
|
|
100
|
-
.resolves.toEqual([
|
|
101
|
-
{ field: 'value' },
|
|
102
|
-
{ field: 'value' },
|
|
103
|
-
{ field: 'value' },
|
|
104
|
-
])
|
|
105
|
-
.then(done);
|
|
106
|
-
|
|
107
|
-
expect(queue.peekQueueSize('client')).toEqual(3);
|
|
108
|
-
|
|
109
|
-
// @ts-ignore It has been assigned. Promise
|
|
110
|
-
resolvePromise({ field: 'value' });
|
|
111
|
-
});
|
|
112
|
-
|
|
113
|
-
it('should resolve queue on fetched data', (done) => {
|
|
114
|
-
let resolvePromise: Function;
|
|
115
|
-
const promise = new Promise((resolve) => {
|
|
116
|
-
resolvePromise = resolve;
|
|
117
|
-
});
|
|
118
|
-
|
|
119
|
-
spyOn(cache, 'get').mockResolvedValue(null);
|
|
120
|
-
|
|
121
|
-
expect(
|
|
122
|
-
Promise.all([
|
|
123
|
-
getOrUpdate('client', schema, jest.fn().mockReturnValue(promise)),
|
|
124
|
-
getOrUpdate('client', schema, jest.fn()),
|
|
125
|
-
getOrUpdate('client', schema, jest.fn()),
|
|
126
|
-
])
|
|
127
|
-
)
|
|
128
|
-
.resolves.toEqual([
|
|
129
|
-
{ field: 'value' },
|
|
130
|
-
{ field: 'value' },
|
|
131
|
-
{ field: 'value' },
|
|
132
|
-
])
|
|
133
|
-
.then(() => expect(queue.peekQueueSize('client')).toEqual(0))
|
|
134
|
-
.then(done);
|
|
135
|
-
|
|
136
|
-
expect(queue.peekQueueSize('client')).toEqual(3);
|
|
137
|
-
|
|
138
|
-
// @ts-ignore It has been assigned. Promise
|
|
139
|
-
resolvePromise({ ttl: 0, data: { field: 'value' } });
|
|
140
|
-
});
|
|
141
|
-
|
|
142
|
-
it('should reject queue if fetching fails', (done) => {
|
|
143
|
-
let rejectPromise: Function;
|
|
144
|
-
const promise = new Promise((resolve, reject) => {
|
|
145
|
-
rejectPromise = reject;
|
|
146
|
-
});
|
|
147
|
-
|
|
148
|
-
spyOn(cache, 'get').mockResolvedValue(null);
|
|
149
|
-
|
|
150
|
-
const a = jest.fn().mockReturnValue(promise);
|
|
151
|
-
const b = jest.fn();
|
|
152
|
-
const c = jest.fn();
|
|
153
|
-
|
|
154
|
-
expect(getOrUpdate('client', schema, a)).rejects.toEqual(undefined);
|
|
155
|
-
expect(getOrUpdate('client', schema, b)).rejects.toEqual(undefined);
|
|
156
|
-
expect(getOrUpdate('client', schema, c))
|
|
157
|
-
.rejects.toEqual(undefined)
|
|
158
|
-
.then(() => expect(queue.peekQueueSize('client')).toEqual(0))
|
|
159
|
-
.then(done);
|
|
160
|
-
|
|
161
|
-
expect(queue.peekQueueSize('client')).toEqual(3);
|
|
162
|
-
|
|
163
|
-
// @ts-ignore It has been assigned. Promise
|
|
164
|
-
rejectPromise();
|
|
165
|
-
});
|
|
166
|
-
});
|
package/src/utils/getOrUpdate.ts
DELETED
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
import { Runtype, Static } from 'runtypes';
|
|
2
|
-
import queue from './queue';
|
|
3
|
-
import cache from '../cache';
|
|
4
|
-
|
|
5
|
-
export type DataFetcherWithCacheTTL<T extends Runtype> = Promise<{
|
|
6
|
-
ttl: number;
|
|
7
|
-
data: Static<T>;
|
|
8
|
-
}>;
|
|
9
|
-
|
|
10
|
-
const mergeCache = <T>(oldCache: T, newCache: T): T => {
|
|
11
|
-
// Since we cannot know how to deal with duplicates when newCache is array
|
|
12
|
-
// always return newCache
|
|
13
|
-
if (!oldCache || Array.isArray(newCache)) {
|
|
14
|
-
return newCache;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
// TODO: Possible bug? :)
|
|
18
|
-
if (!newCache) {
|
|
19
|
-
return oldCache;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
if (typeof oldCache === 'object' && typeof newCache === 'object') {
|
|
23
|
-
return { ...oldCache, ...newCache };
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
// Cannot merge (eg type is string etc)
|
|
27
|
-
return newCache;
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
export default function getOrUpdate<T extends Runtype>(
|
|
31
|
-
key: string,
|
|
32
|
-
schema: T,
|
|
33
|
-
fetch: () => DataFetcherWithCacheTTL<T>,
|
|
34
|
-
cacheQualifier?: (cacheData: Static<T>) => boolean
|
|
35
|
-
): Promise<Static<T>> {
|
|
36
|
-
return new Promise((resolve, reject) => {
|
|
37
|
-
queue.add(key, { resolve, reject });
|
|
38
|
-
if (queue.peekQueueSize(key) > 1) {
|
|
39
|
-
return;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
cache.get(key, schema).then((cacheResponse) => {
|
|
43
|
-
if (cacheResponse && (!cacheQualifier || cacheQualifier(cacheResponse))) {
|
|
44
|
-
queue.getAll(key).forEach((promise) => {
|
|
45
|
-
promise.resolve(cacheResponse);
|
|
46
|
-
});
|
|
47
|
-
return;
|
|
48
|
-
}
|
|
49
|
-
fetch()
|
|
50
|
-
.then((res) => {
|
|
51
|
-
const newCache = cacheResponse
|
|
52
|
-
? mergeCache(cacheResponse, res.data)
|
|
53
|
-
: res.data;
|
|
54
|
-
cache.set(key, newCache, res.ttl);
|
|
55
|
-
queue.getAll(key).forEach((promise) => {
|
|
56
|
-
promise.resolve(res.data);
|
|
57
|
-
});
|
|
58
|
-
})
|
|
59
|
-
.catch(() => {
|
|
60
|
-
queue.getAll(key).forEach((promise) => {
|
|
61
|
-
promise.reject();
|
|
62
|
-
});
|
|
63
|
-
});
|
|
64
|
-
});
|
|
65
|
-
});
|
|
66
|
-
}
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import log from './log';
|
|
2
|
-
import getGlobalThis from './getGlobalThis';
|
|
3
|
-
|
|
4
|
-
export const isRunningOnLocalhost = (): boolean => {
|
|
5
|
-
if (getGlobalThis().location.hostname === 'localhost') {
|
|
6
|
-
log.error('@amedia/user cannot work on localhost.');
|
|
7
|
-
return true;
|
|
8
|
-
}
|
|
9
|
-
return false;
|
|
10
|
-
};
|
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
import { listenWithExclusivity } from './customEventWrapper';
|
|
2
|
-
import { AmediaUserEvents } from '../eventHandlers/eventMap';
|
|
3
|
-
import { navigateTo, reload, replace } from './location';
|
|
4
|
-
|
|
5
|
-
let handler: jest.Mock;
|
|
6
|
-
beforeEach(() => {
|
|
7
|
-
handler = jest.fn();
|
|
8
|
-
listenWithExclusivity(AmediaUserEvents.REQUEST_NAVIGATION, handler);
|
|
9
|
-
});
|
|
10
|
-
|
|
11
|
-
describe('reload', () => {
|
|
12
|
-
beforeEach(() => {
|
|
13
|
-
reload();
|
|
14
|
-
});
|
|
15
|
-
|
|
16
|
-
it('should execute reload handler', () => {
|
|
17
|
-
expect(handler).toHaveBeenCalledWith(
|
|
18
|
-
jasmine.objectContaining({
|
|
19
|
-
detail: {
|
|
20
|
-
type: 'reload',
|
|
21
|
-
},
|
|
22
|
-
})
|
|
23
|
-
);
|
|
24
|
-
});
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
describe('navigateTo', () => {
|
|
28
|
-
beforeEach(() => {
|
|
29
|
-
navigateTo('https://www.telen.no');
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
it('should execute reload handler', () => {
|
|
33
|
-
expect(handler).toHaveBeenCalledWith(
|
|
34
|
-
jasmine.objectContaining({
|
|
35
|
-
detail: {
|
|
36
|
-
type: 'location',
|
|
37
|
-
url: 'https://www.telen.no',
|
|
38
|
-
replace: false,
|
|
39
|
-
},
|
|
40
|
-
})
|
|
41
|
-
);
|
|
42
|
-
});
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
describe('replace', () => {
|
|
46
|
-
beforeEach(() => {
|
|
47
|
-
replace('https://www.telen.no');
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
it('should execute reload handler', () => {
|
|
51
|
-
expect(handler).toHaveBeenCalledWith(
|
|
52
|
-
jasmine.objectContaining({
|
|
53
|
-
detail: {
|
|
54
|
-
type: 'location',
|
|
55
|
-
url: 'https://www.telen.no',
|
|
56
|
-
replace: true,
|
|
57
|
-
},
|
|
58
|
-
})
|
|
59
|
-
);
|
|
60
|
-
});
|
|
61
|
-
});
|
package/src/utils/location.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { dispatch } from './customEventWrapper';
|
|
2
|
-
import { AmediaUserEvents } from '../eventHandlers/eventMap';
|
|
3
|
-
|
|
4
|
-
const requestNavigation = (type: string, url?: string, replace?: boolean) => {
|
|
5
|
-
dispatch(
|
|
6
|
-
new CustomEvent(AmediaUserEvents.REQUEST_NAVIGATION, {
|
|
7
|
-
detail: { type, url, replace },
|
|
8
|
-
})
|
|
9
|
-
);
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
export const reload = () => requestNavigation('reload');
|
|
13
|
-
export const replace = (url: string) =>
|
|
14
|
-
requestNavigation('location', url, true);
|
|
15
|
-
export const navigateTo = (url: string) =>
|
|
16
|
-
requestNavigation('location', url, false);
|
package/src/utils/log.spec.ts
DELETED
|
@@ -1,177 +0,0 @@
|
|
|
1
|
-
import log, { Logger, setLogger } from './log';
|
|
2
|
-
import { version } from '../../eik.json';
|
|
3
|
-
|
|
4
|
-
describe('Log', () => {
|
|
5
|
-
let logger: Logger;
|
|
6
|
-
beforeEach(() => {
|
|
7
|
-
logger = {
|
|
8
|
-
debug: jest.fn(),
|
|
9
|
-
info: jest.fn(),
|
|
10
|
-
warn: jest.fn(),
|
|
11
|
-
error: jest.fn(),
|
|
12
|
-
};
|
|
13
|
-
setLogger(logger);
|
|
14
|
-
});
|
|
15
|
-
|
|
16
|
-
function itShouldLogMessage(message: string, level: keyof Logger = 'error') {
|
|
17
|
-
it('should log message', () => {
|
|
18
|
-
expect(logger[level]).toHaveBeenCalledWith(message, expect.anything());
|
|
19
|
-
});
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
function itShouldLogContext(context: Object, level: keyof Logger = 'error') {
|
|
23
|
-
it('should log context', () => {
|
|
24
|
-
expect(logger[level]).toHaveBeenCalledWith(expect.anything(), {
|
|
25
|
-
...context,
|
|
26
|
-
origin: '@amedia/user',
|
|
27
|
-
originVersion: version,
|
|
28
|
-
});
|
|
29
|
-
});
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
function expectRewrittenError(message: string) {
|
|
33
|
-
const matcher = new RegExp(`Error: ${message}\n(^\\s+at .+$\\n){3,}`, 'm');
|
|
34
|
-
return {
|
|
35
|
-
message,
|
|
36
|
-
stack: expect.stringMatching(matcher),
|
|
37
|
-
};
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
describe('with stringed message', () => {
|
|
41
|
-
beforeEach(() => {
|
|
42
|
-
log.error('Epic fail');
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
itShouldLogMessage('Epic fail');
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
describe('with all error levels', () => {
|
|
49
|
-
beforeEach(() => {
|
|
50
|
-
log.debug('debug');
|
|
51
|
-
log.info('info');
|
|
52
|
-
log.warn('warn');
|
|
53
|
-
log.error('error');
|
|
54
|
-
});
|
|
55
|
-
itShouldLogMessage('debug', 'debug');
|
|
56
|
-
itShouldLogMessage('info', 'info');
|
|
57
|
-
itShouldLogMessage('warn', 'warn');
|
|
58
|
-
itShouldLogMessage('error', 'error');
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
describe('with stringed message and context', () => {
|
|
62
|
-
beforeEach(() => {
|
|
63
|
-
log.error('Epic fail', {
|
|
64
|
-
my: 'context',
|
|
65
|
-
});
|
|
66
|
-
});
|
|
67
|
-
itShouldLogMessage('Epic fail');
|
|
68
|
-
itShouldLogContext({
|
|
69
|
-
my: 'context',
|
|
70
|
-
});
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
describe('with stringed message and complex context', () => {
|
|
74
|
-
beforeEach(() => {
|
|
75
|
-
log.error('Epic fail', {
|
|
76
|
-
my: 'context',
|
|
77
|
-
more: {
|
|
78
|
-
nested: 'context',
|
|
79
|
-
},
|
|
80
|
-
});
|
|
81
|
-
});
|
|
82
|
-
itShouldLogMessage('Epic fail');
|
|
83
|
-
itShouldLogContext({
|
|
84
|
-
my: 'context',
|
|
85
|
-
more: {
|
|
86
|
-
nested: 'context',
|
|
87
|
-
},
|
|
88
|
-
});
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
describe('with Error as message', () => {
|
|
92
|
-
beforeEach(() => {
|
|
93
|
-
log.error(new Error('Epic fail'));
|
|
94
|
-
});
|
|
95
|
-
itShouldLogMessage('Epic fail');
|
|
96
|
-
itShouldLogContext({
|
|
97
|
-
error: expectRewrittenError('Epic fail'),
|
|
98
|
-
});
|
|
99
|
-
});
|
|
100
|
-
|
|
101
|
-
describe('with Error as message and context', () => {
|
|
102
|
-
beforeEach(() => {
|
|
103
|
-
log.error(new Error('Epic fail'), {
|
|
104
|
-
my: 'context',
|
|
105
|
-
});
|
|
106
|
-
});
|
|
107
|
-
itShouldLogMessage('Epic fail');
|
|
108
|
-
itShouldLogContext({
|
|
109
|
-
my: 'context',
|
|
110
|
-
error: expectRewrittenError('Epic fail'),
|
|
111
|
-
});
|
|
112
|
-
});
|
|
113
|
-
|
|
114
|
-
describe('with message as string and Errors in context', () => {
|
|
115
|
-
beforeEach(() => {
|
|
116
|
-
log.error('Epic fail', {
|
|
117
|
-
failError: new Error('Much fail'),
|
|
118
|
-
boomError: new Error('Very boom'),
|
|
119
|
-
my: 'context',
|
|
120
|
-
});
|
|
121
|
-
});
|
|
122
|
-
itShouldLogMessage('Epic fail');
|
|
123
|
-
itShouldLogContext({
|
|
124
|
-
my: 'context',
|
|
125
|
-
failError: expectRewrittenError('Much fail'),
|
|
126
|
-
boomError: expectRewrittenError('Very boom'),
|
|
127
|
-
});
|
|
128
|
-
});
|
|
129
|
-
|
|
130
|
-
describe('with Error directly as context', () => {
|
|
131
|
-
beforeEach(() => {
|
|
132
|
-
log.error('Epic fail', new Error('Downhill from here'));
|
|
133
|
-
});
|
|
134
|
-
itShouldLogMessage('Epic fail');
|
|
135
|
-
itShouldLogContext({
|
|
136
|
-
error: expectRewrittenError('Downhill from here'),
|
|
137
|
-
});
|
|
138
|
-
});
|
|
139
|
-
|
|
140
|
-
describe('with Error both at message and context', () => {
|
|
141
|
-
beforeEach(() => {
|
|
142
|
-
log.error(new Error('Epic fail'), new Error('Downhill from here'));
|
|
143
|
-
});
|
|
144
|
-
itShouldLogMessage('Epic fail');
|
|
145
|
-
itShouldLogContext({
|
|
146
|
-
error: expectRewrittenError('Epic fail'),
|
|
147
|
-
contextError: expectRewrittenError('Downhill from here'),
|
|
148
|
-
});
|
|
149
|
-
});
|
|
150
|
-
|
|
151
|
-
describe('when logging fetch response', () => {
|
|
152
|
-
beforeEach(() => {
|
|
153
|
-
log.error(
|
|
154
|
-
'Error log',
|
|
155
|
-
new Response('body says hi', {
|
|
156
|
-
headers: {
|
|
157
|
-
'Cache-Control': 'no-store',
|
|
158
|
-
},
|
|
159
|
-
})
|
|
160
|
-
);
|
|
161
|
-
});
|
|
162
|
-
|
|
163
|
-
itShouldLogMessage('Error log');
|
|
164
|
-
itShouldLogContext({
|
|
165
|
-
response: {
|
|
166
|
-
body: 'body says hi',
|
|
167
|
-
headers: {
|
|
168
|
-
'cache-control': 'no-store',
|
|
169
|
-
'content-type': 'text/plain;charset=UTF-8',
|
|
170
|
-
},
|
|
171
|
-
status: 200,
|
|
172
|
-
statusText: 'OK',
|
|
173
|
-
url: '',
|
|
174
|
-
},
|
|
175
|
-
});
|
|
176
|
-
});
|
|
177
|
-
});
|