@amedia/user 0.14.17 → 0.14.19
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/dist/user.d.ts +1 -1
- 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
|
@@ -1,95 +0,0 @@
|
|
|
1
|
-
import fetch from 'jest-fetch-mock';
|
|
2
|
-
import cache from '../cache';
|
|
3
|
-
import thirdPartyCookie, { CACHE_KEY, CACHE_TTL } from './thirdPartyCookie';
|
|
4
|
-
import * as location from '../utils/location';
|
|
5
|
-
import * as url from '../utils/url';
|
|
6
|
-
|
|
7
|
-
let shouldRedirectToAutoLogin = false;
|
|
8
|
-
|
|
9
|
-
function itShouldSetAlreadyTriedCache() {
|
|
10
|
-
it('should set already tried cache', () => {
|
|
11
|
-
expect(cache.set).toHaveBeenCalledWith(CACHE_KEY, true, CACHE_TTL);
|
|
12
|
-
});
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
function itShouldRedirectToAutoLogin() {
|
|
16
|
-
it('should redirect to auto login', () => {
|
|
17
|
-
expect(shouldRedirectToAutoLogin).toBeTruthy();
|
|
18
|
-
});
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
function itShouldNotRedirectToAutoLogin() {
|
|
22
|
-
it('should not redirect to auto login', () => {
|
|
23
|
-
expect(shouldRedirectToAutoLogin).toBeFalsy();
|
|
24
|
-
});
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
describe('Autologin: Third Party Cookie', () => {
|
|
28
|
-
let cacheGetSpy: jasmine.Spy;
|
|
29
|
-
|
|
30
|
-
beforeEach(() => {
|
|
31
|
-
cacheGetSpy = spyOn(cache, 'get');
|
|
32
|
-
spyOn(cache, 'set');
|
|
33
|
-
spyOn(location, 'navigateTo');
|
|
34
|
-
spyOn(url, 'getAidDomain').and.returnValue('www.aid.no');
|
|
35
|
-
spyOn(url, 'getCurrentUrl').and.returnValue(
|
|
36
|
-
'https://avisnavn.no/abo/?query=param'
|
|
37
|
-
);
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
describe('when not logged in first party', () => {
|
|
41
|
-
describe('when already checked third party option', () => {
|
|
42
|
-
beforeEach(async () => {
|
|
43
|
-
cacheGetSpy.and.returnValue(true);
|
|
44
|
-
shouldRedirectToAutoLogin = await thirdPartyCookie();
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
it('should not set already tried cache', () => {
|
|
48
|
-
expect(cache.set).not.toHaveBeenCalled();
|
|
49
|
-
});
|
|
50
|
-
itShouldNotRedirectToAutoLogin();
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
describe('when not logged in via third party', () => {
|
|
54
|
-
beforeEach(async () => {
|
|
55
|
-
fetch.doMockIf(
|
|
56
|
-
`https://www.aid.no/api/pluto/v1/sessions/current`,
|
|
57
|
-
JSON.stringify({
|
|
58
|
-
errors: [
|
|
59
|
-
{
|
|
60
|
-
id: 'xxx-000000000000',
|
|
61
|
-
title: 'xxx',
|
|
62
|
-
detail: 'xxx',
|
|
63
|
-
status: '403',
|
|
64
|
-
code: 'session.not_specified',
|
|
65
|
-
},
|
|
66
|
-
],
|
|
67
|
-
}),
|
|
68
|
-
{
|
|
69
|
-
status: 403,
|
|
70
|
-
}
|
|
71
|
-
);
|
|
72
|
-
shouldRedirectToAutoLogin = await thirdPartyCookie();
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
itShouldSetAlreadyTriedCache();
|
|
76
|
-
itShouldNotRedirectToAutoLogin();
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
describe('when logged in via third party', () => {
|
|
80
|
-
beforeEach(async () => {
|
|
81
|
-
fetch.doMockIf(
|
|
82
|
-
`https://www.aid.no/api/pluto/v1/sessions/current`,
|
|
83
|
-
JSON.stringify({
|
|
84
|
-
user_name: 'Donald Duck',
|
|
85
|
-
tracking_key: 'key',
|
|
86
|
-
})
|
|
87
|
-
);
|
|
88
|
-
shouldRedirectToAutoLogin = await thirdPartyCookie();
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
itShouldSetAlreadyTriedCache();
|
|
92
|
-
itShouldRedirectToAutoLogin();
|
|
93
|
-
});
|
|
94
|
-
});
|
|
95
|
-
});
|
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
import { Boolean, Record, Static, String } from 'runtypes';
|
|
2
|
-
import cache from '../cache';
|
|
3
|
-
import log from '../utils/log';
|
|
4
|
-
import { getAidDomain } from '../utils/url';
|
|
5
|
-
import { resolveBadPlutoResponse } from '../clients/pluto';
|
|
6
|
-
|
|
7
|
-
export const CACHE_KEY = 'haveTriedThirdPartyLoginCheck';
|
|
8
|
-
export const CACHE_TTL = 60 * 60 * 1000; // One hour
|
|
9
|
-
|
|
10
|
-
const ServiceSessionSchema = Record({
|
|
11
|
-
user_name: String,
|
|
12
|
-
tracking_key: String,
|
|
13
|
-
});
|
|
14
|
-
|
|
15
|
-
const getAidSession = () =>
|
|
16
|
-
<Promise<Static<typeof ServiceSessionSchema> | null>>fetch(
|
|
17
|
-
`https://${getAidDomain()}/api/pluto/v1/sessions/current`,
|
|
18
|
-
{
|
|
19
|
-
mode: 'cors',
|
|
20
|
-
credentials: 'include',
|
|
21
|
-
}
|
|
22
|
-
)
|
|
23
|
-
.then((response) => {
|
|
24
|
-
if (!response.ok) {
|
|
25
|
-
return resolveBadPlutoResponse(response)
|
|
26
|
-
.then(() => null)
|
|
27
|
-
.catch(() => null);
|
|
28
|
-
}
|
|
29
|
-
return response.json();
|
|
30
|
-
})
|
|
31
|
-
.then((data) => {
|
|
32
|
-
if (!data) {
|
|
33
|
-
return null;
|
|
34
|
-
}
|
|
35
|
-
try {
|
|
36
|
-
return ServiceSessionSchema.check(data);
|
|
37
|
-
} catch (error) {
|
|
38
|
-
log.error(error, { data });
|
|
39
|
-
return null;
|
|
40
|
-
}
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
export default async function thirdPartyCookie() {
|
|
44
|
-
// TODO: Should perhaps use cookie for this? Cache as it stands now only survives on the current page until vi replace it with something more persistent
|
|
45
|
-
if (await cache.get(CACHE_KEY, Boolean)) {
|
|
46
|
-
return false;
|
|
47
|
-
}
|
|
48
|
-
await cache.set(CACHE_KEY, true, CACHE_TTL);
|
|
49
|
-
|
|
50
|
-
const aidSession = await getAidSession();
|
|
51
|
-
if (aidSession) {
|
|
52
|
-
return true;
|
|
53
|
-
}
|
|
54
|
-
return false;
|
|
55
|
-
}
|
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
import { clear, get, remove, set } from './global';
|
|
2
|
-
|
|
3
|
-
beforeEach(() => {
|
|
4
|
-
global.amediaUserCache = {};
|
|
5
|
-
});
|
|
6
|
-
|
|
7
|
-
describe('Global / window cache', () => {
|
|
8
|
-
it('should return undefined if not in cache', () => {
|
|
9
|
-
expect(get('test')).resolves.toBeUndefined();
|
|
10
|
-
});
|
|
11
|
-
|
|
12
|
-
it('should return if in cache', () => {
|
|
13
|
-
set('test', 'value', 1000);
|
|
14
|
-
expect(get('test')).resolves.toEqual({
|
|
15
|
-
key: 'test',
|
|
16
|
-
data: 'value',
|
|
17
|
-
expires: 1000,
|
|
18
|
-
});
|
|
19
|
-
expect(global.amediaUserCache).toEqual({
|
|
20
|
-
test: { key: 'test', data: 'value', expires: 1000 },
|
|
21
|
-
});
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
it('should be able to set most data', () => {
|
|
25
|
-
set('numeric', 5, 0);
|
|
26
|
-
expect(get('numeric')).resolves.toEqual({
|
|
27
|
-
key: 'numeric',
|
|
28
|
-
data: 5,
|
|
29
|
-
expires: 0,
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
set('object', { name: 'Donald Duck' }, 0);
|
|
33
|
-
expect(get('object')).resolves.toEqual({
|
|
34
|
-
key: 'object',
|
|
35
|
-
data: { name: 'Donald Duck' },
|
|
36
|
-
expires: 0,
|
|
37
|
-
});
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
it('should overwrite existing data', () => {
|
|
41
|
-
set('test', 'data', 0);
|
|
42
|
-
set('test', 'other', 0);
|
|
43
|
-
expect(get('test')).resolves.toEqual({
|
|
44
|
-
key: 'test',
|
|
45
|
-
data: 'other',
|
|
46
|
-
expires: 0,
|
|
47
|
-
});
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
it('should remove data from cache', () => {
|
|
51
|
-
set('test', 'value', 0);
|
|
52
|
-
expect(get('test')).resolves.not.toBeUndefined();
|
|
53
|
-
remove('test');
|
|
54
|
-
expect(get('test')).resolves.toBeUndefined();
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
it('should clear cache', () => {
|
|
58
|
-
set('test', 'value', 0);
|
|
59
|
-
clear();
|
|
60
|
-
expect(global.amediaUserCache).toEqual({});
|
|
61
|
-
});
|
|
62
|
-
});
|
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
import { CacheEntry, Engine } from '../types';
|
|
2
|
-
import getGlobalThis from '../../utils/getGlobalThis';
|
|
3
|
-
|
|
4
|
-
declare global {
|
|
5
|
-
// eslint-disable-next-line import/no-mutable-exports,no-var
|
|
6
|
-
export var amediaUserCache: { [index: string]: CacheEntry };
|
|
7
|
-
|
|
8
|
-
interface Window {
|
|
9
|
-
amediaUserCache?: { [index: string]: CacheEntry };
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
namespace NodeJS {
|
|
13
|
-
interface Global {
|
|
14
|
-
amediaUserCache?: { [index: string]: CacheEntry };
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
const getStorage = () => {
|
|
20
|
-
const rootObject = getGlobalThis();
|
|
21
|
-
// eslint-disable-next-line no-return-assign
|
|
22
|
-
return rootObject.amediaUserCache || (rootObject.amediaUserCache = {});
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
export const get = (key: string): Promise<CacheEntry> => {
|
|
26
|
-
const data = getStorage()[key];
|
|
27
|
-
return Promise.resolve(data);
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
export const set = (
|
|
31
|
-
key: string,
|
|
32
|
-
data: unknown,
|
|
33
|
-
expires: number
|
|
34
|
-
): Promise<void> => {
|
|
35
|
-
getStorage()[key] = { key, data, expires };
|
|
36
|
-
return Promise.resolve();
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
export const remove = (key: string): Promise<void> => {
|
|
40
|
-
delete getStorage()[key];
|
|
41
|
-
return Promise.resolve();
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
export const clear = (): Promise<void> => {
|
|
45
|
-
getGlobalThis().amediaUserCache = {};
|
|
46
|
-
return Promise.resolve();
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
export default { get, set, remove, clear } as Engine;
|
package/src/cache/index.spec.ts
DELETED
|
@@ -1,132 +0,0 @@
|
|
|
1
|
-
import { Record, String } from 'runtypes';
|
|
2
|
-
import * as MockDate from 'mockdate';
|
|
3
|
-
import cache, { setCacheEngine } from '.';
|
|
4
|
-
import { setLogger } from '../utils/log';
|
|
5
|
-
import { version } from '../../eik.json';
|
|
6
|
-
|
|
7
|
-
const TestDataSchema = Record({
|
|
8
|
-
test: String,
|
|
9
|
-
});
|
|
10
|
-
|
|
11
|
-
let log: any;
|
|
12
|
-
let mockEngine: any;
|
|
13
|
-
beforeEach(() => {
|
|
14
|
-
MockDate.reset();
|
|
15
|
-
// @ts-ignore
|
|
16
|
-
mockEngine = {
|
|
17
|
-
get: jest.fn(),
|
|
18
|
-
set: jest.fn(),
|
|
19
|
-
remove: jest.fn(),
|
|
20
|
-
clear: jest.fn(),
|
|
21
|
-
};
|
|
22
|
-
// @ts-ignore
|
|
23
|
-
setCacheEngine(mockEngine);
|
|
24
|
-
log = {
|
|
25
|
-
error: jest.fn(),
|
|
26
|
-
};
|
|
27
|
-
setLogger(log);
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
describe('Cache', () => {
|
|
31
|
-
describe('get', () => {
|
|
32
|
-
it('should get from engine', () => {
|
|
33
|
-
mockEngine.get.mockReturnValueOnce(Promise.resolve(null));
|
|
34
|
-
expect(cache.get('foo', TestDataSchema)).resolves.toBeUndefined();
|
|
35
|
-
expect(mockEngine.get).toHaveBeenCalledWith('foo');
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
it('should validate valid data from engine', () => {
|
|
39
|
-
mockEngine.get.mockReturnValueOnce(
|
|
40
|
-
Promise.resolve({
|
|
41
|
-
key: 'foo',
|
|
42
|
-
data: {
|
|
43
|
-
test: 'cache',
|
|
44
|
-
},
|
|
45
|
-
expires: Date.now() + 1000,
|
|
46
|
-
})
|
|
47
|
-
);
|
|
48
|
-
expect(cache.get('foo', TestDataSchema)).resolves.toEqual({
|
|
49
|
-
test: 'cache',
|
|
50
|
-
});
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
it('should return undefined and delete record on invalid data', async () => {
|
|
54
|
-
mockEngine.get.mockReturnValueOnce(
|
|
55
|
-
Promise.resolve({
|
|
56
|
-
key: 'foo',
|
|
57
|
-
data: {
|
|
58
|
-
invalid: 'data',
|
|
59
|
-
},
|
|
60
|
-
expires: Date.now() + 1000,
|
|
61
|
-
})
|
|
62
|
-
);
|
|
63
|
-
await expect(cache.get('foo', TestDataSchema)).resolves.toBeUndefined();
|
|
64
|
-
expect(mockEngine.get).toHaveBeenCalledWith('foo');
|
|
65
|
-
expect(mockEngine.remove).toHaveBeenCalledWith('foo');
|
|
66
|
-
expect(log.error).toHaveBeenCalledWith(
|
|
67
|
-
'Expected { test: string; }, but was incompatible',
|
|
68
|
-
{
|
|
69
|
-
error: {
|
|
70
|
-
code: 'CONTENT_INCORRECT',
|
|
71
|
-
details: {
|
|
72
|
-
test: 'Expected string, but was missing',
|
|
73
|
-
},
|
|
74
|
-
message: 'Expected { test: string; }, but was incompatible',
|
|
75
|
-
name: 'ValidationError',
|
|
76
|
-
stack: expect.stringMatching(
|
|
77
|
-
/ValidationError: Expected \{ test: string; \}, but was incompatible\n(^\s+at\s.+$\n){3,}/m
|
|
78
|
-
),
|
|
79
|
-
},
|
|
80
|
-
cacheData: { invalid: 'data' },
|
|
81
|
-
origin: '@amedia/user',
|
|
82
|
-
originVersion: version,
|
|
83
|
-
}
|
|
84
|
-
);
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
it('deletes expired data', async () => {
|
|
88
|
-
mockEngine.get.mockReturnValueOnce(
|
|
89
|
-
Promise.resolve({
|
|
90
|
-
key: 'foo',
|
|
91
|
-
data: {
|
|
92
|
-
test: 'cache',
|
|
93
|
-
},
|
|
94
|
-
expires: Date.now() - 1000,
|
|
95
|
-
})
|
|
96
|
-
);
|
|
97
|
-
await expect(cache.get('foo', TestDataSchema)).resolves.toBeUndefined();
|
|
98
|
-
expect(mockEngine.get).toHaveBeenCalledWith('foo');
|
|
99
|
-
expect(mockEngine.remove).toHaveBeenCalledWith('foo');
|
|
100
|
-
});
|
|
101
|
-
});
|
|
102
|
-
|
|
103
|
-
describe('set', () => {
|
|
104
|
-
it('should set', () => {
|
|
105
|
-
const EXPIRES = 987654321;
|
|
106
|
-
const MOCK_NOW_TIMESTAMP = 123456789;
|
|
107
|
-
MockDate.set(MOCK_NOW_TIMESTAMP);
|
|
108
|
-
cache.set('test-key', { test: 'data' }, EXPIRES);
|
|
109
|
-
expect(mockEngine.set).toHaveBeenCalledWith(
|
|
110
|
-
'test-key',
|
|
111
|
-
{ test: 'data' },
|
|
112
|
-
MOCK_NOW_TIMESTAMP + EXPIRES
|
|
113
|
-
);
|
|
114
|
-
});
|
|
115
|
-
});
|
|
116
|
-
|
|
117
|
-
describe('remove', () => {
|
|
118
|
-
it('should remove', () => {
|
|
119
|
-
expect(mockEngine.remove).not.toHaveBeenCalled();
|
|
120
|
-
cache.remove('some-key');
|
|
121
|
-
expect(mockEngine.remove).toHaveBeenCalledWith('some-key');
|
|
122
|
-
});
|
|
123
|
-
});
|
|
124
|
-
|
|
125
|
-
describe('clear', () => {
|
|
126
|
-
it('should clear', () => {
|
|
127
|
-
expect(mockEngine.clear).not.toHaveBeenCalled();
|
|
128
|
-
cache.clear();
|
|
129
|
-
expect(mockEngine.clear).toHaveBeenCalled();
|
|
130
|
-
});
|
|
131
|
-
});
|
|
132
|
-
});
|
package/src/cache/index.ts
DELETED
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
import { Static } from 'runtypes';
|
|
2
|
-
import { RuntypeBase } from 'runtypes/lib/runtype';
|
|
3
|
-
import log from '../utils/log';
|
|
4
|
-
import globalCache from './engines/global';
|
|
5
|
-
import { Engine } from './types';
|
|
6
|
-
|
|
7
|
-
let cacheEngine: Engine;
|
|
8
|
-
|
|
9
|
-
export const setCacheEngine = (engine: Engine) => {
|
|
10
|
-
cacheEngine = engine;
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
const getCacheEngine = (): Engine => cacheEngine || globalCache;
|
|
14
|
-
|
|
15
|
-
const set = (key: string, value: unknown, expires: number) =>
|
|
16
|
-
getCacheEngine().set(key, value, Date.now() + expires);
|
|
17
|
-
|
|
18
|
-
const remove = (key: string): Promise<void> => getCacheEngine().remove(key);
|
|
19
|
-
|
|
20
|
-
const get = async <T extends RuntypeBase>(
|
|
21
|
-
key: string,
|
|
22
|
-
validator: T
|
|
23
|
-
): Promise<Static<T> | void> => {
|
|
24
|
-
const cacheEntry = await getCacheEngine().get(key);
|
|
25
|
-
if (!cacheEntry) {
|
|
26
|
-
return Promise.resolve();
|
|
27
|
-
}
|
|
28
|
-
if (Date.now() > cacheEntry.expires) {
|
|
29
|
-
remove(key);
|
|
30
|
-
return Promise.resolve();
|
|
31
|
-
}
|
|
32
|
-
try {
|
|
33
|
-
return validator.check(cacheEntry.data);
|
|
34
|
-
} catch (error) {
|
|
35
|
-
log.error(error, { cacheData: cacheEntry.data });
|
|
36
|
-
remove(key);
|
|
37
|
-
return Promise.resolve();
|
|
38
|
-
}
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
const clear = () => getCacheEngine().clear();
|
|
42
|
-
|
|
43
|
-
export default {
|
|
44
|
-
get,
|
|
45
|
-
set,
|
|
46
|
-
remove,
|
|
47
|
-
clear,
|
|
48
|
-
};
|
package/src/cache/types.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
export type CacheEntry = {
|
|
2
|
-
key: string;
|
|
3
|
-
data: unknown;
|
|
4
|
-
expires: number;
|
|
5
|
-
};
|
|
6
|
-
|
|
7
|
-
export type Engine = {
|
|
8
|
-
get: (key: string) => Promise<CacheEntry | null>;
|
|
9
|
-
set: (key: string, data: unknown, expires: number) => Promise<void>;
|
|
10
|
-
remove: (key: string) => Promise<void>;
|
|
11
|
-
clear: () => Promise<void>;
|
|
12
|
-
};
|
|
@@ -1,209 +0,0 @@
|
|
|
1
|
-
import fetch from 'jest-fetch-mock';
|
|
2
|
-
import cache from '../cache';
|
|
3
|
-
import {
|
|
4
|
-
updateNamespace,
|
|
5
|
-
EuropaSchema,
|
|
6
|
-
clearLocalCache,
|
|
7
|
-
saveNamespace,
|
|
8
|
-
deleteNamespaceKey,
|
|
9
|
-
getNamespaces,
|
|
10
|
-
getAllNamespaces,
|
|
11
|
-
} from './europa';
|
|
12
|
-
|
|
13
|
-
beforeEach(() => {
|
|
14
|
-
cache.clear();
|
|
15
|
-
});
|
|
16
|
-
|
|
17
|
-
describe('Europa', () => {
|
|
18
|
-
it('saveNamespace should set namespace and return the new value', async () => {
|
|
19
|
-
const userInfo = { gender: 'U', post_code: 'U' };
|
|
20
|
-
fetch
|
|
21
|
-
.mockOnce(JSON.stringify(userInfo)) // data to save 'PUT'
|
|
22
|
-
.mockOnce(JSON.stringify({ namespaces: { user_info: userInfo } })); // data retrieved 'GET'
|
|
23
|
-
|
|
24
|
-
const namespaceData = await saveNamespace('user_info', userInfo);
|
|
25
|
-
expect(fetch).toHaveBeenCalledTimes(2);
|
|
26
|
-
expect(namespaceData).toMatchObject({ user_info: userInfo });
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
it('updateNamespace should update namespace', async () => {
|
|
30
|
-
const oldUserInfo = { gender: 'F', post_code: 'U' };
|
|
31
|
-
const newUserInfo = { gender: 'M', post_code: '2150' };
|
|
32
|
-
|
|
33
|
-
fetch.mockOnce(JSON.stringify({})); // initial response // 'PUT'
|
|
34
|
-
fetch.mockOnce(JSON.stringify({ namespaces: { user_info: oldUserInfo } })); // old 'GET'
|
|
35
|
-
fetch.mockOnce(JSON.stringify({ namespaces: { user_info: newUserInfo } })); // updated 'GET'
|
|
36
|
-
|
|
37
|
-
const old = await saveNamespace('user_info', oldUserInfo);
|
|
38
|
-
expect(fetch).toHaveBeenCalledTimes(2);
|
|
39
|
-
expect(old).toEqual({ user_info: oldUserInfo });
|
|
40
|
-
|
|
41
|
-
const updated = await updateNamespace('user_info', newUserInfo).then(() =>
|
|
42
|
-
cache.get('europa', EuropaSchema)
|
|
43
|
-
);
|
|
44
|
-
expect(updated).toMatchObject({ user_info: newUserInfo });
|
|
45
|
-
expect(fetch).toHaveBeenCalledTimes(3);
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
it('clearLocalCache should remove Europa cache', async () => {
|
|
49
|
-
fetch.mockResponse(JSON.stringify({ namespaces: { euucp: { x: 'x' } } }));
|
|
50
|
-
|
|
51
|
-
await saveNamespace('euucp', { x: 'x' });
|
|
52
|
-
expect(cache.get('europa', EuropaSchema)).resolves.toMatchObject({
|
|
53
|
-
euucp: { x: 'x' },
|
|
54
|
-
}); // expect something in cache
|
|
55
|
-
|
|
56
|
-
await clearLocalCache();
|
|
57
|
-
expect(cache.get('europa', EuropaSchema)).resolves.toBeUndefined(); // expect cache to be empty
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
it('deleteNamespaceKey should remove key from europa', async () => {
|
|
61
|
-
fetch
|
|
62
|
-
.mockOnce(JSON.stringify({})) // 'GET'
|
|
63
|
-
.mockOnce(JSON.stringify({ namespaces: { euucp: { x: 'x', y: 'y' } } })) // 'PUT'
|
|
64
|
-
.mockOnce(JSON.stringify({}), { status: 200 }) // 'DELETE'
|
|
65
|
-
.mockOnce(JSON.stringify({ namespaces: { euucp: { y: 'y' } } })); // 'PUT'
|
|
66
|
-
|
|
67
|
-
const data = { x: 'x', y: 'y' };
|
|
68
|
-
|
|
69
|
-
await saveNamespace('euucp', data);
|
|
70
|
-
// expect x and y
|
|
71
|
-
expect(await cache.get('europa', EuropaSchema)).toEqual(
|
|
72
|
-
expect.objectContaining({
|
|
73
|
-
euucp: data,
|
|
74
|
-
})
|
|
75
|
-
);
|
|
76
|
-
|
|
77
|
-
await deleteNamespaceKey('euucp', 'x');
|
|
78
|
-
// expect only y
|
|
79
|
-
expect(await cache.get('europa', EuropaSchema)).toEqual(
|
|
80
|
-
expect.objectContaining({
|
|
81
|
-
euucp: { y: 'y' },
|
|
82
|
-
})
|
|
83
|
-
);
|
|
84
|
-
});
|
|
85
|
-
|
|
86
|
-
it('getAllNamespaces should return all europa namespaces', async () => {
|
|
87
|
-
const data = {
|
|
88
|
-
euucp: { x: 'x' },
|
|
89
|
-
user_info: { gender: 'M', post_code: 'U' },
|
|
90
|
-
};
|
|
91
|
-
|
|
92
|
-
fetch.mockResponse(JSON.stringify({ namespaces: data }));
|
|
93
|
-
expect(await getAllNamespaces()).toMatchObject({ namespaces: data });
|
|
94
|
-
});
|
|
95
|
-
});
|
|
96
|
-
|
|
97
|
-
describe('getNamespaces', () => {
|
|
98
|
-
let response: any;
|
|
99
|
-
describe('with cold cache', () => {
|
|
100
|
-
describe('with one namespace', () => {
|
|
101
|
-
const data = { euucp: { x: 'x' } };
|
|
102
|
-
beforeEach(async () => {
|
|
103
|
-
fetch.mockResponse(JSON.stringify({ namespaces: data }));
|
|
104
|
-
response = await getNamespaces(['euucp']);
|
|
105
|
-
});
|
|
106
|
-
it('should return namespace', () => {
|
|
107
|
-
expect(response).toEqual(data);
|
|
108
|
-
});
|
|
109
|
-
});
|
|
110
|
-
|
|
111
|
-
describe('with multiple namespaces', () => {
|
|
112
|
-
const data = { euucp: { x: 'x' }, user_info: { a: 'a' } };
|
|
113
|
-
beforeEach(async () => {
|
|
114
|
-
fetch.mockResponse(JSON.stringify({ namespaces: data }));
|
|
115
|
-
response = await getNamespaces(['euucp', 'user_info']);
|
|
116
|
-
});
|
|
117
|
-
it('should return multiple namespaces', () => {
|
|
118
|
-
expect(response).toEqual(data);
|
|
119
|
-
});
|
|
120
|
-
});
|
|
121
|
-
|
|
122
|
-
describe('when doing sequence of calls for different namespaces', () => {
|
|
123
|
-
const data = { euucp: { x: 'x' } };
|
|
124
|
-
const data2 = { user_info: { a: 'a' } };
|
|
125
|
-
let response2: any;
|
|
126
|
-
beforeEach(async () => {
|
|
127
|
-
fetch.mockResponseOnce(JSON.stringify({ namespaces: data }));
|
|
128
|
-
fetch.mockResponseOnce(JSON.stringify({ namespaces: data2 }));
|
|
129
|
-
response = await getNamespaces(['euucp']);
|
|
130
|
-
response2 = await getNamespaces(['user_info']);
|
|
131
|
-
});
|
|
132
|
-
it('should return multiple namespaces', () => {
|
|
133
|
-
expect(response).toEqual(data);
|
|
134
|
-
expect(response2).toEqual(data2);
|
|
135
|
-
});
|
|
136
|
-
});
|
|
137
|
-
|
|
138
|
-
describe('when doing sequence of calls for different namespaces before first response returns', () => {
|
|
139
|
-
const data = { euucp: { x: 'x' } };
|
|
140
|
-
const data2 = { user_info: { a: 'a' } };
|
|
141
|
-
let response2: any;
|
|
142
|
-
beforeEach(async () => {
|
|
143
|
-
fetch.mockResponseOnce(
|
|
144
|
-
JSON.stringify({ namespaces: { ...data, ...data2 } })
|
|
145
|
-
);
|
|
146
|
-
const futureEuucp = getNamespaces(['euucp']);
|
|
147
|
-
response2 = await getNamespaces(['user_info']);
|
|
148
|
-
response = await futureEuucp;
|
|
149
|
-
});
|
|
150
|
-
it('should return euucp in first response', () => {
|
|
151
|
-
expect(response).toEqual(data);
|
|
152
|
-
});
|
|
153
|
-
it('should return user_info in second response', () => {
|
|
154
|
-
expect(response2).toEqual(data2);
|
|
155
|
-
});
|
|
156
|
-
});
|
|
157
|
-
});
|
|
158
|
-
|
|
159
|
-
describe('with one namespace in cache', () => {
|
|
160
|
-
const data = { euucp: { x: 'x' } };
|
|
161
|
-
beforeEach(() => {
|
|
162
|
-
cache.set('europa', data, 100000);
|
|
163
|
-
});
|
|
164
|
-
|
|
165
|
-
describe('when requesting cached namespace', () => {
|
|
166
|
-
beforeEach(async () => {
|
|
167
|
-
response = await getNamespaces(['euucp']);
|
|
168
|
-
});
|
|
169
|
-
it('should return namespace', () => {
|
|
170
|
-
expect(response).toEqual(data);
|
|
171
|
-
});
|
|
172
|
-
});
|
|
173
|
-
|
|
174
|
-
describe('when requesting namespace not found in cache', () => {
|
|
175
|
-
const expectedData = {
|
|
176
|
-
privacy_preferences: { allow_research_usage: true },
|
|
177
|
-
};
|
|
178
|
-
|
|
179
|
-
beforeEach(async () => {
|
|
180
|
-
fetch.mockResponse(JSON.stringify({ namespaces: expectedData }));
|
|
181
|
-
response = await getNamespaces(['privacy_preferences']);
|
|
182
|
-
});
|
|
183
|
-
it('should return namespace from backend, not from cache', () => {
|
|
184
|
-
expect(response).toEqual(expectedData);
|
|
185
|
-
});
|
|
186
|
-
});
|
|
187
|
-
|
|
188
|
-
describe('when requesting multiple namespaces and only one found in cache', () => {
|
|
189
|
-
const expectedData = {
|
|
190
|
-
privacy_preferences: { allow_research_usage: true },
|
|
191
|
-
euucp: { x: 'x' },
|
|
192
|
-
};
|
|
193
|
-
|
|
194
|
-
beforeEach(async () => {
|
|
195
|
-
fetch.mockResponse(JSON.stringify({ namespaces: expectedData }));
|
|
196
|
-
response = await getNamespaces(['privacy_preferences', 'euucp']);
|
|
197
|
-
});
|
|
198
|
-
it('should return namespace from backend, not from cache', () => {
|
|
199
|
-
expect(response).toEqual(expectedData);
|
|
200
|
-
});
|
|
201
|
-
});
|
|
202
|
-
});
|
|
203
|
-
|
|
204
|
-
it('should require at least one namespace', () => {
|
|
205
|
-
expect(getNamespaces([])).rejects.toThrowError(
|
|
206
|
-
new Error('namespaces array cannot be of length 0')
|
|
207
|
-
);
|
|
208
|
-
});
|
|
209
|
-
});
|