@oslokommune/auth-bff 1.4.0 → 1.5.0
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/middleware/oidc.d.mts +1 -1
- package/dist/middleware/oidc.d.mts.map +1 -1
- package/dist/middleware/oidc.mjs +47 -14
- package/dist/middleware/proxy-routes.mjs +1 -1
- package/dist/react/AuthContext.d.ts +6 -5
- package/dist/react/AuthContext.d.ts.map +1 -1
- package/dist/react/AuthContextProvider.d.ts +2 -1
- package/dist/react/AuthContextProvider.d.ts.map +1 -1
- package/dist/react/AuthContextProvider.jsx +79 -10
- package/dist/react/UseAuthContext.d.ts +1 -1
- package/dist/react/UseAuthContext.d.ts.map +1 -1
- package/dist/react/checker.d.ts +3 -0
- package/dist/react/checker.d.ts.map +1 -0
- package/dist/react/checker.js +43 -0
- package/dist/react/poller.d.ts +3 -0
- package/dist/react/poller.d.ts.map +1 -0
- package/dist/react/poller.js +39 -0
- package/package.json +2 -1
|
@@ -6,7 +6,7 @@ export class OidcMiddleware {
|
|
|
6
6
|
* @param clientManager
|
|
7
7
|
*/
|
|
8
8
|
private constructor();
|
|
9
|
-
get ensureFreshToken(): (req: any,
|
|
9
|
+
get ensureFreshToken(): (req: any, res: any, next: any) => void;
|
|
10
10
|
get login(): (req: any, res: any) => void;
|
|
11
11
|
get callback(): (req: any, res: any) => Promise<void>;
|
|
12
12
|
get user(): (req: any, res: any) => Promise<any>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"oidc.d.mts","sourceRoot":"","sources":["../../src/middleware/oidc.mjs"],"names":[],"mappings":"AAGA;
|
|
1
|
+
{"version":3,"file":"oidc.d.mts","sourceRoot":"","sources":["../../src/middleware/oidc.mjs"],"names":[],"mappings":"AAGA;IAeE,oDAIC;IAdD;;;;OAIG;IACH,sBAGC;IAiDD,yBACU,QAAG,EAAE,QAAG,EAAE,SAAI,UAUvB;IAED,cACU,QAAG,EAAE,QAAG,UAuBjB;IAED,iBACgB,QAAG,EAAE,QAAG,mBAmCvB;IAED,aACgB,QAAG,EAAE,QAAG,kBAevB;IAED,eACU,QAAG,EAAE,QAAG,UAYjB;;CACF"}
|
package/dist/middleware/oidc.mjs
CHANGED
|
@@ -18,7 +18,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
18
18
|
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
19
19
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
20
20
|
};
|
|
21
|
-
var _OidcMiddleware_instances, _OidcMiddleware_clientManager, _OidcMiddleware_config, _OidcMiddleware_getFreshTokenSet;
|
|
21
|
+
var _OidcMiddleware_instances, _OidcMiddleware_clientManager, _OidcMiddleware_config, _OidcMiddleware_refreshPromises, _OidcMiddleware_refreshTokenSet, _OidcMiddleware_getFreshTokenSet;
|
|
22
22
|
import { generators, TokenSet } from "openid-client";
|
|
23
23
|
import { OidcClientManager } from "../client.mjs";
|
|
24
24
|
export class OidcMiddleware {
|
|
@@ -31,6 +31,13 @@ export class OidcMiddleware {
|
|
|
31
31
|
_OidcMiddleware_instances.add(this);
|
|
32
32
|
_OidcMiddleware_clientManager.set(this, void 0);
|
|
33
33
|
_OidcMiddleware_config.set(this, void 0);
|
|
34
|
+
_OidcMiddleware_refreshPromises.set(this, {}
|
|
35
|
+
/**
|
|
36
|
+
* @private
|
|
37
|
+
* @param config
|
|
38
|
+
* @param clientManager
|
|
39
|
+
*/
|
|
40
|
+
);
|
|
34
41
|
__classPrivateFieldSet(this, _OidcMiddleware_clientManager, clientManager, "f");
|
|
35
42
|
__classPrivateFieldSet(this, _OidcMiddleware_config, config, "f");
|
|
36
43
|
}
|
|
@@ -42,10 +49,15 @@ export class OidcMiddleware {
|
|
|
42
49
|
});
|
|
43
50
|
}
|
|
44
51
|
get ensureFreshToken() {
|
|
45
|
-
return (req,
|
|
52
|
+
return (req, res, next) => {
|
|
46
53
|
__classPrivateFieldGet(this, _OidcMiddleware_instances, "m", _OidcMiddleware_getFreshTokenSet).call(this, req).then(tokenSet => {
|
|
47
|
-
|
|
48
|
-
|
|
54
|
+
if (tokenSet) {
|
|
55
|
+
req.tokenSet = tokenSet;
|
|
56
|
+
next();
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
res.sendStatus(401);
|
|
60
|
+
}
|
|
49
61
|
});
|
|
50
62
|
};
|
|
51
63
|
}
|
|
@@ -130,7 +142,35 @@ export class OidcMiddleware {
|
|
|
130
142
|
};
|
|
131
143
|
}
|
|
132
144
|
}
|
|
133
|
-
_OidcMiddleware_clientManager = new WeakMap(), _OidcMiddleware_config = new WeakMap(), _OidcMiddleware_instances = new WeakSet(),
|
|
145
|
+
_OidcMiddleware_clientManager = new WeakMap(), _OidcMiddleware_config = new WeakMap(), _OidcMiddleware_refreshPromises = new WeakMap(), _OidcMiddleware_instances = new WeakSet(), _OidcMiddleware_refreshTokenSet = function _OidcMiddleware_refreshTokenSet(req, tokenSet) {
|
|
146
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
147
|
+
var _a;
|
|
148
|
+
var _b;
|
|
149
|
+
const sessionId = req.session.id;
|
|
150
|
+
const refreshToken = tokenSet.refresh_token;
|
|
151
|
+
const doRefresh = () => __awaiter(this, void 0, void 0, function* () {
|
|
152
|
+
console.log(`Token refresh starting. sid=${sessionId}`);
|
|
153
|
+
try {
|
|
154
|
+
const refreshedTokenSet = yield __classPrivateFieldGet(this, _OidcMiddleware_clientManager, "f").client.refresh(refreshToken);
|
|
155
|
+
console.log(`Token refresh OK. sid=${sessionId}`);
|
|
156
|
+
return refreshedTokenSet;
|
|
157
|
+
}
|
|
158
|
+
catch (err) {
|
|
159
|
+
console.log(`Token refresh failed. sid=${sessionId}`, err);
|
|
160
|
+
return null;
|
|
161
|
+
}
|
|
162
|
+
});
|
|
163
|
+
const refreshPromise = (_a = (_b = __classPrivateFieldGet(this, _OidcMiddleware_refreshPromises, "f"))[refreshToken]) !== null && _a !== void 0 ? _a : (_b[refreshToken] = doRefresh().finally(() => {
|
|
164
|
+
console.log(`Token refresh finished. Cleaning up. sid=${sessionId}`);
|
|
165
|
+
setTimeout(() => {
|
|
166
|
+
delete __classPrivateFieldGet(this, _OidcMiddleware_refreshPromises, "f")[refreshToken];
|
|
167
|
+
}, 10000);
|
|
168
|
+
}));
|
|
169
|
+
const refreshedTokenSet = yield refreshPromise;
|
|
170
|
+
req.session.tokenSet = refreshedTokenSet;
|
|
171
|
+
return refreshedTokenSet;
|
|
172
|
+
});
|
|
173
|
+
}, _OidcMiddleware_getFreshTokenSet = function _OidcMiddleware_getFreshTokenSet(req) {
|
|
134
174
|
return __awaiter(this, void 0, void 0, function* () {
|
|
135
175
|
const tokenSet = req.session.tokenSet && new TokenSet(req.session.tokenSet);
|
|
136
176
|
if (!tokenSet) {
|
|
@@ -138,15 +178,8 @@ _OidcMiddleware_clientManager = new WeakMap(), _OidcMiddleware_config = new Weak
|
|
|
138
178
|
return;
|
|
139
179
|
}
|
|
140
180
|
if (tokenSet.expired()) {
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
Object.assign(req.session.tokenSet, refreshedTokenSet);
|
|
144
|
-
return new TokenSet(req.session.tokenSet);
|
|
145
|
-
}
|
|
146
|
-
catch (err) {
|
|
147
|
-
console.log("Token refresh failed", err);
|
|
148
|
-
req.session.tokenSet = null;
|
|
149
|
-
}
|
|
181
|
+
const newTokenSet = yield __classPrivateFieldGet(this, _OidcMiddleware_instances, "m", _OidcMiddleware_refreshTokenSet).call(this, req, tokenSet);
|
|
182
|
+
return newTokenSet && new TokenSet(newTokenSet);
|
|
150
183
|
}
|
|
151
184
|
else {
|
|
152
185
|
return tokenSet;
|
|
@@ -12,7 +12,7 @@ export function proxyRoutes(config, oidcMiddleware) {
|
|
|
12
12
|
const tokenSet = req.tokenSet;
|
|
13
13
|
if (!tokenSet) {
|
|
14
14
|
console.error("proxy: missing tokenSet");
|
|
15
|
-
return
|
|
15
|
+
return;
|
|
16
16
|
}
|
|
17
17
|
proxyReq.setHeader("Authorization", `Bearer ${tokenSet.access_token}`);
|
|
18
18
|
proxyReq.removeHeader("Cookie");
|
|
@@ -1,10 +1,11 @@
|
|
|
1
|
+
export type User = {
|
|
2
|
+
[k: string]: string | number;
|
|
3
|
+
};
|
|
1
4
|
export type AuthContextProps = {
|
|
2
|
-
state: 'pending' | 'authenticated' | 'unauthenticated';
|
|
3
|
-
user?:
|
|
4
|
-
pid: string;
|
|
5
|
-
};
|
|
5
|
+
state: 'pending' | 'authenticated' | 'unauthenticated' | 'expired';
|
|
6
|
+
user?: User;
|
|
6
7
|
login: () => void;
|
|
7
8
|
logout: () => void;
|
|
8
9
|
};
|
|
9
|
-
export declare const AuthContext:
|
|
10
|
+
export declare const AuthContext: import("react").Context<AuthContextProps>;
|
|
10
11
|
//# sourceMappingURL=AuthContext.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AuthContext.d.ts","sourceRoot":"","sources":["../../src/react/AuthContext.tsx"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"AuthContext.d.ts","sourceRoot":"","sources":["../../src/react/AuthContext.tsx"],"names":[],"mappings":"AAGA,MAAM,MAAM,IAAI,GAAG;IACjB,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,CAAA;CAC7B,CAAA;AAED,MAAM,MAAM,gBAAgB,GAAG;IAC7B,KAAK,EAAE,SAAS,GAAG,eAAe,GAAG,iBAAiB,GAAG,SAAS,CAAC;IACnE,IAAI,CAAC,EAAE,IAAI,CAAA;IACX,KAAK,EAAE,MAAM,IAAI,CAAA;IACjB,MAAM,EAAE,MAAM,IAAI,CAAA;CACnB,CAAA;AAED,eAAO,MAAM,WAAW,2CAAyD,CAAA"}
|
|
@@ -4,7 +4,8 @@ type AuthContextProviderProps = {
|
|
|
4
4
|
authRequired?: boolean;
|
|
5
5
|
loaderComponent: ReactNode;
|
|
6
6
|
baseUrl?: string;
|
|
7
|
+
pollInterval?: number;
|
|
7
8
|
};
|
|
8
|
-
export declare function AuthContextProvider({ children, authRequired, loaderComponent, baseUrl }: AuthContextProviderProps):
|
|
9
|
+
export declare function AuthContextProvider({ children, authRequired, loaderComponent, baseUrl, pollInterval }: AuthContextProviderProps): import("react").JSX.Element;
|
|
9
10
|
export {};
|
|
10
11
|
//# sourceMappingURL=AuthContextProvider.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AuthContextProvider.d.ts","sourceRoot":"","sources":["../../src/react/AuthContextProvider.tsx"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"AuthContextProvider.d.ts","sourceRoot":"","sources":["../../src/react/AuthContextProvider.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAC,SAAS,EAA8B,MAAM,OAAO,CAAC;AAK7D,KAAK,wBAAwB,GAAG;IAC9B,QAAQ,EAAE,SAAS,CAAA;IACnB,YAAY,CAAC,EAAE,OAAO,CAAA;IACtB,eAAe,EAAE,SAAS,CAAA;IAC1B,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,YAAY,CAAC,EAAE,MAAM,CAAA;CACtB,CAAA;AAED,wBAAgB,mBAAmB,CAAC,EACE,QAAQ,EACR,YAAoB,EACpB,eAAsB,EACtB,OAAY,EACZ,YAAY,EACb,EAAE,wBAAwB,+BA+F9D"}
|
|
@@ -1,7 +1,17 @@
|
|
|
1
|
-
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import { useEffect, useRef, useState } from "react";
|
|
2
11
|
import { AuthContext } from "./AuthContext";
|
|
3
12
|
import { setCurrentUser } from "./global-user";
|
|
4
|
-
|
|
13
|
+
import * as poller from './poller';
|
|
14
|
+
export function AuthContextProvider({ children, authRequired = false, loaderComponent = null, baseUrl = '', pollInterval }) {
|
|
5
15
|
const [user, setUser] = useState(undefined);
|
|
6
16
|
const [state, setState] = useState('pending');
|
|
7
17
|
const userPromise = useRef(undefined);
|
|
@@ -12,27 +22,86 @@ export function AuthContextProvider({ children, authRequired = false, loaderComp
|
|
|
12
22
|
const currentRelativeLocation = window.location.pathname + window.location.search + window.location.hash;
|
|
13
23
|
window.location.assign(`${baseUrl}/auth/login?redirectUrl=${encodeURIComponent(currentRelativeLocation)}`);
|
|
14
24
|
}
|
|
25
|
+
function getUser() {
|
|
26
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
27
|
+
const res = yield fetch(`${baseUrl}/auth/user`);
|
|
28
|
+
if (res.ok) {
|
|
29
|
+
try {
|
|
30
|
+
return yield res.json();
|
|
31
|
+
}
|
|
32
|
+
catch (e) {
|
|
33
|
+
console.error('failed to parse user', e);
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
function startPoller() {
|
|
43
|
+
const setExpiredIfNoUser = (user) => {
|
|
44
|
+
if (!user)
|
|
45
|
+
setState('expired');
|
|
46
|
+
};
|
|
47
|
+
poller.start(getUser, setExpiredIfNoUser, pollInterval);
|
|
48
|
+
}
|
|
49
|
+
function stopPoller() {
|
|
50
|
+
poller.stop();
|
|
51
|
+
}
|
|
52
|
+
function onVisibilityChange() {
|
|
53
|
+
if (document.visibilityState === 'visible') {
|
|
54
|
+
startPoller();
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
stopPoller();
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
useEffect(() => {
|
|
61
|
+
if (pollInterval && pollInterval > 0 && state === 'authenticated') {
|
|
62
|
+
startPoller();
|
|
63
|
+
document.addEventListener('visibilitychange', onVisibilityChange);
|
|
64
|
+
return () => {
|
|
65
|
+
stopPoller();
|
|
66
|
+
document.removeEventListener('visibilitychange', onVisibilityChange);
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
}, [pollInterval, state]);
|
|
15
70
|
useEffect(() => {
|
|
16
71
|
var _a;
|
|
17
|
-
(
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
setCurrentUser(json);
|
|
72
|
+
(_a = userPromise.current) !== null && _a !== void 0 ? _a : (userPromise.current = getUser().then(user => {
|
|
73
|
+
if (user) {
|
|
74
|
+
setUser(user);
|
|
75
|
+
setCurrentUser(user);
|
|
22
76
|
setState('authenticated');
|
|
23
77
|
}
|
|
24
78
|
else {
|
|
25
79
|
setState('unauthenticated');
|
|
26
80
|
}
|
|
27
|
-
});
|
|
81
|
+
}));
|
|
28
82
|
}, []);
|
|
29
83
|
useEffect(() => {
|
|
30
84
|
if (authRequired && state === 'unauthenticated') {
|
|
31
85
|
login();
|
|
32
86
|
}
|
|
33
87
|
}, [authRequired, state]);
|
|
88
|
+
function getChildComponent() {
|
|
89
|
+
if (state === 'pending') {
|
|
90
|
+
return loaderComponent;
|
|
91
|
+
}
|
|
92
|
+
else if (state === 'authenticated' || state === 'expired') {
|
|
93
|
+
return children;
|
|
94
|
+
}
|
|
95
|
+
else if (state === 'unauthenticated' && !authRequired) {
|
|
96
|
+
return children;
|
|
97
|
+
}
|
|
98
|
+
else {
|
|
99
|
+
//here you should already have been redirected to login
|
|
100
|
+
console.warn('not authenticated');
|
|
101
|
+
return undefined;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
34
104
|
return (<AuthContext.Provider value={{ user, state, login, logout }}>
|
|
35
|
-
{(
|
|
36
|
-
{(authRequired && state === 'authenticated' || !authRequired && state !== 'pending') && children}
|
|
105
|
+
{getChildComponent()}
|
|
37
106
|
</AuthContext.Provider>);
|
|
38
107
|
}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export declare function useAuthContext(required?: boolean):
|
|
1
|
+
export declare function useAuthContext(required?: boolean): import("./AuthContext").AuthContextProps;
|
|
2
2
|
//# sourceMappingURL=UseAuthContext.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"UseAuthContext.d.ts","sourceRoot":"","sources":["../../src/react/UseAuthContext.tsx"],"names":[],"mappings":"AAGA,wBAAgB,cAAc,CAAC,QAAQ,GAAE,OAAe,
|
|
1
|
+
{"version":3,"file":"UseAuthContext.d.ts","sourceRoot":"","sources":["../../src/react/UseAuthContext.tsx"],"names":[],"mappings":"AAGA,wBAAgB,cAAc,CAAC,QAAQ,GAAE,OAAe,4CAOvD"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"checker.d.ts","sourceRoot":"","sources":["../../src/react/checker.ts"],"names":[],"mappings":"AAsBA,wBAAgB,KAAK,CAAC,UAAU,EAAE,MAAM,OAAO,CAAC,OAAO,CAAC,EAAE,WAAW,EAAE,CAAC,GAAG,EAAE,OAAO,KAAK,IAAI,EAAE,cAAc,EAAE,MAAM,QAMpH;AAED,wBAAgB,KAAK,SAGpB"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
let timer;
|
|
11
|
+
let lastChecked = Date.now();
|
|
12
|
+
let callback;
|
|
13
|
+
let checker;
|
|
14
|
+
let minInterval;
|
|
15
|
+
function check() {
|
|
16
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
17
|
+
const now = Date.now();
|
|
18
|
+
const diff = now - lastChecked;
|
|
19
|
+
clearInterval(timer);
|
|
20
|
+
if (diff > minInterval) {
|
|
21
|
+
const res = yield checker();
|
|
22
|
+
callback(res);
|
|
23
|
+
lastChecked = now;
|
|
24
|
+
console.log('checked', diff);
|
|
25
|
+
timer = setInterval(check, minInterval);
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
console.log('waiting', minInterval - diff);
|
|
29
|
+
timer = setInterval(check, minInterval - diff);
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
export function start(newChecker, newCallback, newMinInterval) {
|
|
34
|
+
console.log('start checker');
|
|
35
|
+
checker = newChecker;
|
|
36
|
+
callback = newCallback;
|
|
37
|
+
minInterval = newMinInterval;
|
|
38
|
+
check();
|
|
39
|
+
}
|
|
40
|
+
export function pause() {
|
|
41
|
+
console.log('pause checker');
|
|
42
|
+
clearInterval(timer);
|
|
43
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"poller.d.ts","sourceRoot":"","sources":["../../src/react/poller.ts"],"names":[],"mappings":"AAoBA,wBAAgB,KAAK,CAAC,CAAC,EAAE,UAAU,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,EAAE,WAAW,EAAE,CAAC,GAAG,EAAE,CAAC,KAAK,IAAI,EAAE,cAAc,EAAE,MAAM,QAK3G;AAED,wBAAgB,IAAI,SAEnB"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
let timer;
|
|
11
|
+
let lastChecked = Date.now();
|
|
12
|
+
let callback;
|
|
13
|
+
let poller;
|
|
14
|
+
let minInterval;
|
|
15
|
+
function doPoll() {
|
|
16
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
17
|
+
const now = Date.now();
|
|
18
|
+
const diff = now - lastChecked;
|
|
19
|
+
clearInterval(timer);
|
|
20
|
+
if (diff > minInterval) {
|
|
21
|
+
const res = yield poller();
|
|
22
|
+
callback(res);
|
|
23
|
+
lastChecked = now;
|
|
24
|
+
timer = setInterval(doPoll, minInterval);
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
timer = setInterval(doPoll, minInterval - diff);
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
export function start(newChecker, newCallback, newMinInterval) {
|
|
32
|
+
poller = newChecker;
|
|
33
|
+
callback = newCallback;
|
|
34
|
+
minInterval = newMinInterval;
|
|
35
|
+
doPoll();
|
|
36
|
+
}
|
|
37
|
+
export function stop() {
|
|
38
|
+
clearInterval(timer);
|
|
39
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@oslokommune/auth-bff",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.5.0",
|
|
4
4
|
"repository": "https://github.com/oslokommune/auth-bff.git",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -25,6 +25,7 @@
|
|
|
25
25
|
"license": "",
|
|
26
26
|
"description": "",
|
|
27
27
|
"devDependencies": {
|
|
28
|
+
"@types/react": "17.0.87",
|
|
28
29
|
"@types/express": "^4.17.22",
|
|
29
30
|
"react": "17.0.2",
|
|
30
31
|
"typescript": "^5.8.3"
|