@oneidentity/safeguard 7.4.0 → 8.0.0-pre36954
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/LICENSE +201 -201
- package/README.md +408 -279
- package/dist/browser-C_ziogDF.d.ts +447 -0
- package/dist/browser-C_ziogDF.d.ts.map +1 -0
- package/dist/browser.d.ts +447 -0
- package/dist/browser.js +756 -0
- package/dist/browser.js.map +1 -0
- package/dist/events-RcqBZBsR.d.ts +185 -0
- package/dist/events-RcqBZBsR.d.ts.map +1 -0
- package/dist/events-zHsrmRBj.d.cts +185 -0
- package/dist/events-zHsrmRBj.d.cts.map +1 -0
- package/dist/events.cjs +190 -0
- package/dist/events.cjs.map +1 -0
- package/dist/events.d.cts +166 -0
- package/dist/events.d.ts +166 -0
- package/dist/events.js +188 -0
- package/dist/events.js.map +1 -0
- package/dist/index-BN3IescB.d.cts +668 -0
- package/dist/index-BN3IescB.d.cts.map +1 -0
- package/dist/index-ir5Qeequ.d.ts +668 -0
- package/dist/index-ir5Qeequ.d.ts.map +1 -0
- package/dist/index.cjs +1248 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +668 -0
- package/dist/index.d.ts +668 -0
- package/dist/index.js +1215 -0
- package/dist/index.js.map +1 -0
- package/package.json +100 -45
- package/src/LocalStorage.js +0 -72
- package/src/README.md +0 -67
- package/src/SessionStorage.js +0 -170
- package/src/safeguard.js +0 -937
- package/src/safeguard.min.js +0 -1
- package/src/safeguardJsAccessTokenChecker.js +0 -131
- package/src/safeguardJsAccessTokenChecker.min.js +0 -1
package/dist/events.cjs
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
//#region src/events/listener.ts
|
|
3
|
+
/**
|
|
4
|
+
* One-shot event listener wrapping a SignalR HubConnection.
|
|
5
|
+
* For persistent auto-reconnect behavior, use PersistentSafeguardEventListener.
|
|
6
|
+
*/
|
|
7
|
+
var SafeguardEventListener = class {
|
|
8
|
+
#connection;
|
|
9
|
+
#handlers = [];
|
|
10
|
+
#state = "stopped";
|
|
11
|
+
constructor(connection) {
|
|
12
|
+
this.#connection = connection;
|
|
13
|
+
this.#connection.onclose(() => {
|
|
14
|
+
this.#setState("disconnected");
|
|
15
|
+
});
|
|
16
|
+
this.#connection.onreconnecting(() => {
|
|
17
|
+
this.#setState("reconnecting");
|
|
18
|
+
});
|
|
19
|
+
this.#connection.onreconnected(() => {
|
|
20
|
+
this.#setState("connected");
|
|
21
|
+
});
|
|
22
|
+
this.#connection.on("NotifyEventAsync", (event) => {
|
|
23
|
+
for (const handler of this.#handlers) handler(event);
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
/** Current connection state. */
|
|
27
|
+
get state() {
|
|
28
|
+
return this.#state;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Register an event handler for Safeguard notifications.
|
|
32
|
+
*/
|
|
33
|
+
on(_method, handler) {
|
|
34
|
+
this.#handlers.push(handler);
|
|
35
|
+
return this;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Start the SignalR connection.
|
|
39
|
+
*/
|
|
40
|
+
async start() {
|
|
41
|
+
this.#setState("starting");
|
|
42
|
+
await this.#connection.start();
|
|
43
|
+
this.#setState("connected");
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Stop the SignalR connection.
|
|
47
|
+
*/
|
|
48
|
+
async stop() {
|
|
49
|
+
await this.#connection.stop();
|
|
50
|
+
this.#setState("stopped");
|
|
51
|
+
}
|
|
52
|
+
#setState(state) {
|
|
53
|
+
this.#state = state;
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
//#endregion
|
|
57
|
+
//#region src/events/persistent.ts
|
|
58
|
+
const DEFAULT_RETRY_INTERVAL_MS = 5e3;
|
|
59
|
+
const TOKEN_REFRESH_MARGIN_MS = 6e4;
|
|
60
|
+
/**
|
|
61
|
+
* Persistent event listener with automatic reconnection and token refresh.
|
|
62
|
+
*
|
|
63
|
+
* Wraps SignalR's built-in `withAutomaticReconnect()` AND adds:
|
|
64
|
+
* - Token refresh on reconnect (re-authenticates if needed)
|
|
65
|
+
* - State change event notifications
|
|
66
|
+
* - Manual `stop()` that prevents further auto-reconnect
|
|
67
|
+
*/
|
|
68
|
+
var PersistentSafeguardEventListener = class {
|
|
69
|
+
#connection;
|
|
70
|
+
#auth;
|
|
71
|
+
#host;
|
|
72
|
+
#httpClient;
|
|
73
|
+
#storage;
|
|
74
|
+
#retryIntervalMs;
|
|
75
|
+
#maxRetries;
|
|
76
|
+
#handlers = [];
|
|
77
|
+
#stateChangeHandlers = [];
|
|
78
|
+
#state = "stopped";
|
|
79
|
+
#tokenSet;
|
|
80
|
+
#manualStop = false;
|
|
81
|
+
#retryCount = 0;
|
|
82
|
+
#retryTimer;
|
|
83
|
+
constructor(connection, auth, host, httpClient, storage, options, tokenSet) {
|
|
84
|
+
this.#connection = connection;
|
|
85
|
+
this.#auth = auth;
|
|
86
|
+
this.#host = host;
|
|
87
|
+
this.#httpClient = httpClient;
|
|
88
|
+
this.#storage = storage;
|
|
89
|
+
this.#retryIntervalMs = options?.retryIntervalMs ?? DEFAULT_RETRY_INTERVAL_MS;
|
|
90
|
+
this.#maxRetries = options?.maxRetries;
|
|
91
|
+
this.#tokenSet = tokenSet;
|
|
92
|
+
this.#connection.onclose(() => {
|
|
93
|
+
this.#setState("disconnected");
|
|
94
|
+
if (!this.#manualStop) this.#scheduleReconnect();
|
|
95
|
+
});
|
|
96
|
+
this.#connection.onreconnecting(() => {
|
|
97
|
+
this.#setState("reconnecting");
|
|
98
|
+
});
|
|
99
|
+
this.#connection.onreconnected(() => {
|
|
100
|
+
this.#retryCount = 0;
|
|
101
|
+
this.#setState("connected");
|
|
102
|
+
});
|
|
103
|
+
this.#connection.on("NotifyEventAsync", (event) => {
|
|
104
|
+
for (const handler of this.#handlers) handler(event);
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
/** Current connection state. */
|
|
108
|
+
get state() {
|
|
109
|
+
return this.#state;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Register an event handler for Safeguard notifications.
|
|
113
|
+
*/
|
|
114
|
+
on(_method, handler) {
|
|
115
|
+
this.#handlers.push(handler);
|
|
116
|
+
return this;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Register a state change handler.
|
|
120
|
+
*/
|
|
121
|
+
onStateChange(handler) {
|
|
122
|
+
this.#stateChangeHandlers.push(handler);
|
|
123
|
+
return this;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Start the persistent connection.
|
|
127
|
+
*/
|
|
128
|
+
async start() {
|
|
129
|
+
this.#manualStop = false;
|
|
130
|
+
this.#retryCount = 0;
|
|
131
|
+
this.#setState("starting");
|
|
132
|
+
await this.#connection.start();
|
|
133
|
+
this.#setState("connected");
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Stop the connection. Prevents auto-reconnect.
|
|
137
|
+
*/
|
|
138
|
+
async stop() {
|
|
139
|
+
this.#manualStop = true;
|
|
140
|
+
if (this.#retryTimer != null) {
|
|
141
|
+
clearTimeout(this.#retryTimer);
|
|
142
|
+
this.#retryTimer = void 0;
|
|
143
|
+
}
|
|
144
|
+
await this.#connection.stop();
|
|
145
|
+
this.#setState("stopped");
|
|
146
|
+
}
|
|
147
|
+
#setState(state) {
|
|
148
|
+
this.#state = state;
|
|
149
|
+
for (const handler of this.#stateChangeHandlers) handler(state);
|
|
150
|
+
}
|
|
151
|
+
async #scheduleReconnect() {
|
|
152
|
+
if (this.#manualStop) return;
|
|
153
|
+
if (this.#maxRetries != null && this.#retryCount >= this.#maxRetries) {
|
|
154
|
+
this.#setState("stopped");
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
this.#setState("reconnecting");
|
|
158
|
+
this.#retryTimer = setTimeout(() => {
|
|
159
|
+
this.#reconnect();
|
|
160
|
+
}, this.#retryIntervalMs);
|
|
161
|
+
}
|
|
162
|
+
async #reconnect() {
|
|
163
|
+
if (this.#manualStop) return;
|
|
164
|
+
this.#retryCount++;
|
|
165
|
+
try {
|
|
166
|
+
if (this.#isTokenExpired()) if (this.#auth.refreshToken) {
|
|
167
|
+
const refreshed = await this.#auth.refreshToken(this.#host, this.#httpClient, this.#storage);
|
|
168
|
+
if (refreshed) this.#tokenSet = refreshed;
|
|
169
|
+
else this.#tokenSet = await this.#auth.authenticate(this.#host, this.#httpClient, this.#storage);
|
|
170
|
+
} else this.#tokenSet = await this.#auth.authenticate(this.#host, this.#httpClient, this.#storage);
|
|
171
|
+
await this.#connection.start();
|
|
172
|
+
this.#retryCount = 0;
|
|
173
|
+
this.#setState("connected");
|
|
174
|
+
} catch {
|
|
175
|
+
this.#scheduleReconnect();
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
#isTokenExpired() {
|
|
179
|
+
if (!this.#tokenSet) return true;
|
|
180
|
+
if (!this.#tokenSet.expiresIn) return true;
|
|
181
|
+
const elapsed = Date.now() - this.#tokenSet.acquiredAt;
|
|
182
|
+
const expiresInMs = this.#tokenSet.expiresIn * 1e3;
|
|
183
|
+
return elapsed + TOKEN_REFRESH_MARGIN_MS >= expiresInMs;
|
|
184
|
+
}
|
|
185
|
+
};
|
|
186
|
+
//#endregion
|
|
187
|
+
exports.PersistentSafeguardEventListener = PersistentSafeguardEventListener;
|
|
188
|
+
exports.SafeguardEventListener = SafeguardEventListener;
|
|
189
|
+
|
|
190
|
+
//# sourceMappingURL=events.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"events.cjs","names":["#connection","#handlers","#setState","#state","#connection","#auth","#host","#httpClient","#storage","#retryIntervalMs","#maxRetries","#handlers","#stateChangeHandlers","#tokenSet","#setState","#manualStop","#scheduleReconnect","#retryCount","#state","#retryTimer","#reconnect","#isTokenExpired"],"sources":["../src/events/listener.ts","../src/events/persistent.ts"],"sourcesContent":["import type { HubConnection } from '@microsoft/signalr';\nimport type { EventHandler, EventListenerState, SafeguardEvent } from './types.js';\n\n/**\n * One-shot event listener wrapping a SignalR HubConnection.\n * For persistent auto-reconnect behavior, use PersistentSafeguardEventListener.\n */\nexport class SafeguardEventListener {\n readonly #connection: HubConnection;\n readonly #handlers: EventHandler[] = [];\n #state: EventListenerState = 'stopped';\n\n constructor(connection: HubConnection) {\n this.#connection = connection;\n\n this.#connection.onclose(() => {\n this.#setState('disconnected');\n });\n\n this.#connection.onreconnecting(() => {\n this.#setState('reconnecting');\n });\n\n this.#connection.onreconnected(() => {\n this.#setState('connected');\n });\n\n // Register for the Safeguard event method\n this.#connection.on('NotifyEventAsync', (event: SafeguardEvent) => {\n for (const handler of this.#handlers) {\n handler(event);\n }\n });\n }\n\n /** Current connection state. */\n get state(): EventListenerState {\n return this.#state;\n }\n\n /**\n * Register an event handler for Safeguard notifications.\n */\n on(_method: 'NotifyEventAsync', handler: EventHandler): this {\n this.#handlers.push(handler);\n return this;\n }\n\n /**\n * Start the SignalR connection.\n */\n async start(): Promise<void> {\n this.#setState('starting');\n await this.#connection.start();\n this.#setState('connected');\n }\n\n /**\n * Stop the SignalR connection.\n */\n async stop(): Promise<void> {\n await this.#connection.stop();\n this.#setState('stopped');\n }\n\n #setState(state: EventListenerState): void {\n this.#state = state;\n }\n}\n","import type { HubConnection } from '@microsoft/signalr';\nimport type { Auth, TokenSet } from '../auth/types.js';\nimport type { HttpClient } from '../http/types.js';\nimport type { StorageProvider } from '../storage/types.js';\nimport type {\n EventHandler,\n EventListenerState,\n PersistentListenerOptions,\n SafeguardEvent,\n StateChangeHandler,\n} from './types.js';\n\nconst DEFAULT_RETRY_INTERVAL_MS = 5_000;\nconst TOKEN_REFRESH_MARGIN_MS = 60_000; // refresh 60s before expiry\n\n/**\n * Persistent event listener with automatic reconnection and token refresh.\n *\n * Wraps SignalR's built-in `withAutomaticReconnect()` AND adds:\n * - Token refresh on reconnect (re-authenticates if needed)\n * - State change event notifications\n * - Manual `stop()` that prevents further auto-reconnect\n */\nexport class PersistentSafeguardEventListener {\n readonly #connection: HubConnection;\n readonly #auth: Auth;\n readonly #host: string;\n readonly #httpClient: HttpClient;\n readonly #storage: StorageProvider;\n readonly #retryIntervalMs: number;\n readonly #maxRetries: number | undefined;\n readonly #handlers: EventHandler[] = [];\n readonly #stateChangeHandlers: StateChangeHandler[] = [];\n\n #state: EventListenerState = 'stopped';\n #tokenSet: TokenSet | undefined;\n #manualStop = false;\n #retryCount = 0;\n #retryTimer: ReturnType<typeof setTimeout> | undefined;\n\n constructor(\n connection: HubConnection,\n auth: Auth,\n host: string,\n httpClient: HttpClient,\n storage: StorageProvider,\n options?: PersistentListenerOptions,\n tokenSet?: TokenSet,\n ) {\n this.#connection = connection;\n this.#auth = auth;\n this.#host = host;\n this.#httpClient = httpClient;\n this.#storage = storage;\n this.#retryIntervalMs = options?.retryIntervalMs ?? DEFAULT_RETRY_INTERVAL_MS;\n this.#maxRetries = options?.maxRetries;\n this.#tokenSet = tokenSet;\n\n this.#connection.onclose(() => {\n this.#setState('disconnected');\n if (!this.#manualStop) {\n void this.#scheduleReconnect();\n }\n });\n\n this.#connection.onreconnecting(() => {\n this.#setState('reconnecting');\n });\n\n this.#connection.onreconnected(() => {\n this.#retryCount = 0;\n this.#setState('connected');\n });\n\n this.#connection.on('NotifyEventAsync', (event: SafeguardEvent) => {\n for (const handler of this.#handlers) {\n handler(event);\n }\n });\n }\n\n /** Current connection state. */\n get state(): EventListenerState {\n return this.#state;\n }\n\n /**\n * Register an event handler for Safeguard notifications.\n */\n on(_method: 'NotifyEventAsync', handler: EventHandler): this {\n this.#handlers.push(handler);\n return this;\n }\n\n /**\n * Register a state change handler.\n */\n onStateChange(handler: StateChangeHandler): this {\n this.#stateChangeHandlers.push(handler);\n return this;\n }\n\n /**\n * Start the persistent connection.\n */\n async start(): Promise<void> {\n this.#manualStop = false;\n this.#retryCount = 0;\n this.#setState('starting');\n await this.#connection.start();\n this.#setState('connected');\n }\n\n /**\n * Stop the connection. Prevents auto-reconnect.\n */\n async stop(): Promise<void> {\n this.#manualStop = true;\n if (this.#retryTimer != null) {\n clearTimeout(this.#retryTimer);\n this.#retryTimer = undefined;\n }\n await this.#connection.stop();\n this.#setState('stopped');\n }\n\n // ─── Private ────────────────────────────────────────────────────────\n\n #setState(state: EventListenerState): void {\n this.#state = state;\n for (const handler of this.#stateChangeHandlers) {\n handler(state);\n }\n }\n\n async #scheduleReconnect(): Promise<void> {\n if (this.#manualStop) return;\n if (this.#maxRetries != null && this.#retryCount >= this.#maxRetries) {\n this.#setState('stopped');\n return;\n }\n\n this.#setState('reconnecting');\n this.#retryTimer = setTimeout(() => {\n void this.#reconnect();\n }, this.#retryIntervalMs);\n }\n\n async #reconnect(): Promise<void> {\n if (this.#manualStop) return;\n this.#retryCount++;\n\n try {\n // Only refresh the token if it's expired or near-expiry\n if (this.#isTokenExpired()) {\n if (this.#auth.refreshToken) {\n const refreshed = await this.#auth.refreshToken(this.#host, this.#httpClient, this.#storage);\n if (refreshed) {\n this.#tokenSet = refreshed;\n } else {\n // Refresh failed — full re-auth\n this.#tokenSet = await this.#auth.authenticate(this.#host, this.#httpClient, this.#storage);\n }\n } else {\n this.#tokenSet = await this.#auth.authenticate(this.#host, this.#httpClient, this.#storage);\n }\n }\n\n await this.#connection.start();\n this.#retryCount = 0;\n this.#setState('connected');\n } catch {\n // Reconnect failed — try again\n void this.#scheduleReconnect();\n }\n }\n\n #isTokenExpired(): boolean {\n if (!this.#tokenSet) return true; // no token at all — must authenticate\n if (!this.#tokenSet.expiresIn) return true; // can't determine lifetime — refresh to be safe\n\n const elapsed = Date.now() - this.#tokenSet.acquiredAt;\n const expiresInMs = this.#tokenSet.expiresIn * 1000;\n return elapsed + TOKEN_REFRESH_MARGIN_MS >= expiresInMs;\n }\n}\n"],"mappings":";;;;;;AAOA,IAAa,yBAAb,MAAoC;CAClC;CACA,YAAqC,CAAC;CACtC,SAA6B;CAE7B,YAA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KE;EAEL,IAAI;GAEF,IAAI,KAAKI,gBAAgB,GACvB,IAAI,KAAKhB,MAAM,cAAc;IAC3B,MAAM,YAAY,MAAM,KAAKA,MAAM,aAAa,KAAKC,OAAO,KAAKC,aAAa,KAAKC,QAAQ;IAC3F,IAAI,WACF,KAAKK,YAAY;SAGjB,KAAKA,YAAY,MAAM,KAAKR,MAAM,aAAa,KAAKC,OAAO,KAAKC,aAAa,KAAKC,QAAQ;GAE9F,OACE,KAAKK,YAAY,MAAM,KAAKR,MAAM,aAAa,KAAKC,OAAO,KAAKC,aAAa,KAAKC,QAAQ;GAI9F,MAAM,KAAKJ,YAAY,MAAM;GAC7B,KAAKa,cAAc;GACnB,KAAKH,UAAU,WAAW;EAC5B,QAAQ;GAEN,KAAUE,mBAAmB;EAC/B;CACF;CAEA,kBAA2B;EACzB,IAAI,CAAC,KAAKH,WAAW,OAAO;EAC5B,IAAI,CAAC,KAAKA,UAAU,WAAW,OAAO;EAEtC,MAAM,UAAU,KAAK,IAAI,IAAI,KAAKA,UAAU;EAC5C,MAAM,cAAc,KAAKA,UAAU,YAAY;EAC/C,OAAO,UAAU,2BAA2B;CAC9C;AACF"}
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import { HubConnection } from "@microsoft/signalr";
|
|
2
|
+
import { SecretValue } from './index.d.cts';
|
|
3
|
+
|
|
4
|
+
//#region src/events/types.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Event listener connection states.
|
|
7
|
+
*/
|
|
8
|
+
type EventListenerState = 'starting' | 'connected' | 'disconnected' | 'reconnecting' | 'stopped';
|
|
9
|
+
/**
|
|
10
|
+
* Handler for Safeguard events (NotifyEventAsync messages from SignalR).
|
|
11
|
+
*/
|
|
12
|
+
type EventHandler = (event: SafeguardEvent) => void;
|
|
13
|
+
/**
|
|
14
|
+
* Handler for state change notifications.
|
|
15
|
+
*/
|
|
16
|
+
type StateChangeHandler = (state: EventListenerState) => void;
|
|
17
|
+
/**
|
|
18
|
+
* A Safeguard event as delivered by SignalR.
|
|
19
|
+
*/
|
|
20
|
+
interface SafeguardEvent {
|
|
21
|
+
EventName: string;
|
|
22
|
+
[key: string]: unknown;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Options for persistent event listeners.
|
|
26
|
+
*/
|
|
27
|
+
interface PersistentListenerOptions {
|
|
28
|
+
/** Delay between reconnection attempts in milliseconds. Default: 5000. */
|
|
29
|
+
retryIntervalMs?: number;
|
|
30
|
+
/** Maximum number of reconnect attempts before giving up. Default: unlimited. */
|
|
31
|
+
maxRetries?: number;
|
|
32
|
+
}
|
|
33
|
+
//# sourceMappingURL=types.d.ts.map
|
|
34
|
+
//#endregion
|
|
35
|
+
//#region src/events/listener.d.ts
|
|
36
|
+
/**
|
|
37
|
+
* One-shot event listener wrapping a SignalR HubConnection.
|
|
38
|
+
* For persistent auto-reconnect behavior, use PersistentSafeguardEventListener.
|
|
39
|
+
*/
|
|
40
|
+
declare class SafeguardEventListener {
|
|
41
|
+
#private;
|
|
42
|
+
constructor(connection: HubConnection);
|
|
43
|
+
/** Current connection state. */
|
|
44
|
+
get state(): EventListenerState;
|
|
45
|
+
/**
|
|
46
|
+
* Register an event handler for Safeguard notifications.
|
|
47
|
+
*/
|
|
48
|
+
on(_method: 'NotifyEventAsync', handler: EventHandler): this;
|
|
49
|
+
/**
|
|
50
|
+
* Start the SignalR connection.
|
|
51
|
+
*/
|
|
52
|
+
start(): Promise<void>;
|
|
53
|
+
/**
|
|
54
|
+
* Stop the SignalR connection.
|
|
55
|
+
*/
|
|
56
|
+
stop(): Promise<void>;
|
|
57
|
+
}
|
|
58
|
+
//# sourceMappingURL=listener.d.ts.map
|
|
59
|
+
//#endregion
|
|
60
|
+
//#region src/http/types.d.ts
|
|
61
|
+
/**
|
|
62
|
+
* Platform-agnostic HTTP client interface for the Safeguard SDK.
|
|
63
|
+
*/
|
|
64
|
+
interface HttpRequestOptions {
|
|
65
|
+
url: string;
|
|
66
|
+
method: string;
|
|
67
|
+
headers?: Record<string, string>;
|
|
68
|
+
body?: string | Buffer | null;
|
|
69
|
+
signal?: AbortSignal;
|
|
70
|
+
timeout?: number;
|
|
71
|
+
}
|
|
72
|
+
interface HttpResponse {
|
|
73
|
+
status: number;
|
|
74
|
+
headers: Record<string, string>;
|
|
75
|
+
body: string;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Abstract HTTP client interface.
|
|
79
|
+
* Implementations: NodeHttpClient (undici), BrowserHttpClient (fetch).
|
|
80
|
+
*/
|
|
81
|
+
interface HttpClient {
|
|
82
|
+
request(options: HttpRequestOptions): Promise<HttpResponse>;
|
|
83
|
+
dispose?(): void;
|
|
84
|
+
}
|
|
85
|
+
//# sourceMappingURL=types.d.ts.map
|
|
86
|
+
//#endregion
|
|
87
|
+
//#region src/storage/types.d.ts
|
|
88
|
+
/**
|
|
89
|
+
* Interface for token/state storage backends.
|
|
90
|
+
*/
|
|
91
|
+
interface StorageProvider {
|
|
92
|
+
get(key: string): string | null;
|
|
93
|
+
set(key: string, value: string): void;
|
|
94
|
+
remove(key: string): void;
|
|
95
|
+
clear(): void;
|
|
96
|
+
}
|
|
97
|
+
/** Well-known storage keys used by the SDK. */
|
|
98
|
+
//#endregion
|
|
99
|
+
//#region src/auth/types.d.ts
|
|
100
|
+
/**
|
|
101
|
+
* Token set returned by authentication.
|
|
102
|
+
*/
|
|
103
|
+
interface TokenSet {
|
|
104
|
+
/** The Safeguard user access token (wrapped to prevent accidental logging). */
|
|
105
|
+
accessToken: SecretValue;
|
|
106
|
+
/** Token lifetime in seconds from when it was issued. */
|
|
107
|
+
expiresIn?: number;
|
|
108
|
+
/** Timestamp (ms since epoch) when the token was acquired. */
|
|
109
|
+
acquiredAt: number;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Auth strategy interface. All authentication methods implement this.
|
|
113
|
+
*/
|
|
114
|
+
interface Auth {
|
|
115
|
+
/**
|
|
116
|
+
* Authenticate against the Safeguard RSTS and return a token set.
|
|
117
|
+
* @param host - Appliance hostname
|
|
118
|
+
* @param httpClient - HTTP client to use for requests
|
|
119
|
+
* @param storage - Storage provider for persisting tokens/state
|
|
120
|
+
*/
|
|
121
|
+
authenticate(host: string, httpClient: HttpClient, storage: StorageProvider): Promise<TokenSet>;
|
|
122
|
+
/**
|
|
123
|
+
* Refresh the token if possible. Returns new token set or null if refresh not supported.
|
|
124
|
+
*/
|
|
125
|
+
refreshToken?(host: string, httpClient: HttpClient, storage: StorageProvider): Promise<TokenSet | null>;
|
|
126
|
+
/** Human-readable description of this auth strategy (for logging). */
|
|
127
|
+
readonly description: string;
|
|
128
|
+
}
|
|
129
|
+
//# sourceMappingURL=types.d.ts.map
|
|
130
|
+
//#endregion
|
|
131
|
+
//#region src/events/persistent.d.ts
|
|
132
|
+
/**
|
|
133
|
+
* Persistent event listener with automatic reconnection and token refresh.
|
|
134
|
+
*
|
|
135
|
+
* Wraps SignalR's built-in `withAutomaticReconnect()` AND adds:
|
|
136
|
+
* - Token refresh on reconnect (re-authenticates if needed)
|
|
137
|
+
* - State change event notifications
|
|
138
|
+
* - Manual `stop()` that prevents further auto-reconnect
|
|
139
|
+
*/
|
|
140
|
+
declare class PersistentSafeguardEventListener {
|
|
141
|
+
#private;
|
|
142
|
+
constructor(connection: HubConnection, auth: Auth, host: string, httpClient: HttpClient, storage: StorageProvider, options?: PersistentListenerOptions, tokenSet?: TokenSet);
|
|
143
|
+
/** Current connection state. */
|
|
144
|
+
get state(): EventListenerState;
|
|
145
|
+
/**
|
|
146
|
+
* Register an event handler for Safeguard notifications.
|
|
147
|
+
*/
|
|
148
|
+
on(_method: 'NotifyEventAsync', handler: EventHandler): this;
|
|
149
|
+
/**
|
|
150
|
+
* Register a state change handler.
|
|
151
|
+
*/
|
|
152
|
+
onStateChange(handler: StateChangeHandler): this;
|
|
153
|
+
/**
|
|
154
|
+
* Start the persistent connection.
|
|
155
|
+
*/
|
|
156
|
+
start(): Promise<void>;
|
|
157
|
+
/**
|
|
158
|
+
* Stop the connection. Prevents auto-reconnect.
|
|
159
|
+
*/
|
|
160
|
+
stop(): Promise<void>;
|
|
161
|
+
}
|
|
162
|
+
//# sourceMappingURL=persistent.d.ts.map
|
|
163
|
+
|
|
164
|
+
//#endregion
|
|
165
|
+
export { type EventHandler, type EventListenerState, type PersistentListenerOptions, PersistentSafeguardEventListener, type SafeguardEvent, SafeguardEventListener, type StateChangeHandler };
|
|
166
|
+
//# sourceMappingURL=events-zHsrmRBj.d.cts.map
|
package/dist/events.d.ts
ADDED
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import { HubConnection } from "@microsoft/signalr";
|
|
2
|
+
import { SecretValue } from './index.js';
|
|
3
|
+
|
|
4
|
+
//#region src/events/types.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Event listener connection states.
|
|
7
|
+
*/
|
|
8
|
+
type EventListenerState = 'starting' | 'connected' | 'disconnected' | 'reconnecting' | 'stopped';
|
|
9
|
+
/**
|
|
10
|
+
* Handler for Safeguard events (NotifyEventAsync messages from SignalR).
|
|
11
|
+
*/
|
|
12
|
+
type EventHandler = (event: SafeguardEvent) => void;
|
|
13
|
+
/**
|
|
14
|
+
* Handler for state change notifications.
|
|
15
|
+
*/
|
|
16
|
+
type StateChangeHandler = (state: EventListenerState) => void;
|
|
17
|
+
/**
|
|
18
|
+
* A Safeguard event as delivered by SignalR.
|
|
19
|
+
*/
|
|
20
|
+
interface SafeguardEvent {
|
|
21
|
+
EventName: string;
|
|
22
|
+
[key: string]: unknown;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Options for persistent event listeners.
|
|
26
|
+
*/
|
|
27
|
+
interface PersistentListenerOptions {
|
|
28
|
+
/** Delay between reconnection attempts in milliseconds. Default: 5000. */
|
|
29
|
+
retryIntervalMs?: number;
|
|
30
|
+
/** Maximum number of reconnect attempts before giving up. Default: unlimited. */
|
|
31
|
+
maxRetries?: number;
|
|
32
|
+
}
|
|
33
|
+
//# sourceMappingURL=types.d.ts.map
|
|
34
|
+
//#endregion
|
|
35
|
+
//#region src/events/listener.d.ts
|
|
36
|
+
/**
|
|
37
|
+
* One-shot event listener wrapping a SignalR HubConnection.
|
|
38
|
+
* For persistent auto-reconnect behavior, use PersistentSafeguardEventListener.
|
|
39
|
+
*/
|
|
40
|
+
declare class SafeguardEventListener {
|
|
41
|
+
#private;
|
|
42
|
+
constructor(connection: HubConnection);
|
|
43
|
+
/** Current connection state. */
|
|
44
|
+
get state(): EventListenerState;
|
|
45
|
+
/**
|
|
46
|
+
* Register an event handler for Safeguard notifications.
|
|
47
|
+
*/
|
|
48
|
+
on(_method: 'NotifyEventAsync', handler: EventHandler): this;
|
|
49
|
+
/**
|
|
50
|
+
* Start the SignalR connection.
|
|
51
|
+
*/
|
|
52
|
+
start(): Promise<void>;
|
|
53
|
+
/**
|
|
54
|
+
* Stop the SignalR connection.
|
|
55
|
+
*/
|
|
56
|
+
stop(): Promise<void>;
|
|
57
|
+
}
|
|
58
|
+
//# sourceMappingURL=listener.d.ts.map
|
|
59
|
+
//#endregion
|
|
60
|
+
//#region src/http/types.d.ts
|
|
61
|
+
/**
|
|
62
|
+
* Platform-agnostic HTTP client interface for the Safeguard SDK.
|
|
63
|
+
*/
|
|
64
|
+
interface HttpRequestOptions {
|
|
65
|
+
url: string;
|
|
66
|
+
method: string;
|
|
67
|
+
headers?: Record<string, string>;
|
|
68
|
+
body?: string | Buffer | null;
|
|
69
|
+
signal?: AbortSignal;
|
|
70
|
+
timeout?: number;
|
|
71
|
+
}
|
|
72
|
+
interface HttpResponse {
|
|
73
|
+
status: number;
|
|
74
|
+
headers: Record<string, string>;
|
|
75
|
+
body: string;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Abstract HTTP client interface.
|
|
79
|
+
* Implementations: NodeHttpClient (undici), BrowserHttpClient (fetch).
|
|
80
|
+
*/
|
|
81
|
+
interface HttpClient {
|
|
82
|
+
request(options: HttpRequestOptions): Promise<HttpResponse>;
|
|
83
|
+
dispose?(): void;
|
|
84
|
+
}
|
|
85
|
+
//# sourceMappingURL=types.d.ts.map
|
|
86
|
+
//#endregion
|
|
87
|
+
//#region src/storage/types.d.ts
|
|
88
|
+
/**
|
|
89
|
+
* Interface for token/state storage backends.
|
|
90
|
+
*/
|
|
91
|
+
interface StorageProvider {
|
|
92
|
+
get(key: string): string | null;
|
|
93
|
+
set(key: string, value: string): void;
|
|
94
|
+
remove(key: string): void;
|
|
95
|
+
clear(): void;
|
|
96
|
+
}
|
|
97
|
+
/** Well-known storage keys used by the SDK. */
|
|
98
|
+
//#endregion
|
|
99
|
+
//#region src/auth/types.d.ts
|
|
100
|
+
/**
|
|
101
|
+
* Token set returned by authentication.
|
|
102
|
+
*/
|
|
103
|
+
interface TokenSet {
|
|
104
|
+
/** The Safeguard user access token (wrapped to prevent accidental logging). */
|
|
105
|
+
accessToken: SecretValue;
|
|
106
|
+
/** Token lifetime in seconds from when it was issued. */
|
|
107
|
+
expiresIn?: number;
|
|
108
|
+
/** Timestamp (ms since epoch) when the token was acquired. */
|
|
109
|
+
acquiredAt: number;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Auth strategy interface. All authentication methods implement this.
|
|
113
|
+
*/
|
|
114
|
+
interface Auth {
|
|
115
|
+
/**
|
|
116
|
+
* Authenticate against the Safeguard RSTS and return a token set.
|
|
117
|
+
* @param host - Appliance hostname
|
|
118
|
+
* @param httpClient - HTTP client to use for requests
|
|
119
|
+
* @param storage - Storage provider for persisting tokens/state
|
|
120
|
+
*/
|
|
121
|
+
authenticate(host: string, httpClient: HttpClient, storage: StorageProvider): Promise<TokenSet>;
|
|
122
|
+
/**
|
|
123
|
+
* Refresh the token if possible. Returns new token set or null if refresh not supported.
|
|
124
|
+
*/
|
|
125
|
+
refreshToken?(host: string, httpClient: HttpClient, storage: StorageProvider): Promise<TokenSet | null>;
|
|
126
|
+
/** Human-readable description of this auth strategy (for logging). */
|
|
127
|
+
readonly description: string;
|
|
128
|
+
}
|
|
129
|
+
//# sourceMappingURL=types.d.ts.map
|
|
130
|
+
//#endregion
|
|
131
|
+
//#region src/events/persistent.d.ts
|
|
132
|
+
/**
|
|
133
|
+
* Persistent event listener with automatic reconnection and token refresh.
|
|
134
|
+
*
|
|
135
|
+
* Wraps SignalR's built-in `withAutomaticReconnect()` AND adds:
|
|
136
|
+
* - Token refresh on reconnect (re-authenticates if needed)
|
|
137
|
+
* - State change event notifications
|
|
138
|
+
* - Manual `stop()` that prevents further auto-reconnect
|
|
139
|
+
*/
|
|
140
|
+
declare class PersistentSafeguardEventListener {
|
|
141
|
+
#private;
|
|
142
|
+
constructor(connection: HubConnection, auth: Auth, host: string, httpClient: HttpClient, storage: StorageProvider, options?: PersistentListenerOptions, tokenSet?: TokenSet);
|
|
143
|
+
/** Current connection state. */
|
|
144
|
+
get state(): EventListenerState;
|
|
145
|
+
/**
|
|
146
|
+
* Register an event handler for Safeguard notifications.
|
|
147
|
+
*/
|
|
148
|
+
on(_method: 'NotifyEventAsync', handler: EventHandler): this;
|
|
149
|
+
/**
|
|
150
|
+
* Register a state change handler.
|
|
151
|
+
*/
|
|
152
|
+
onStateChange(handler: StateChangeHandler): this;
|
|
153
|
+
/**
|
|
154
|
+
* Start the persistent connection.
|
|
155
|
+
*/
|
|
156
|
+
start(): Promise<void>;
|
|
157
|
+
/**
|
|
158
|
+
* Stop the connection. Prevents auto-reconnect.
|
|
159
|
+
*/
|
|
160
|
+
stop(): Promise<void>;
|
|
161
|
+
}
|
|
162
|
+
//# sourceMappingURL=persistent.d.ts.map
|
|
163
|
+
|
|
164
|
+
//#endregion
|
|
165
|
+
export { type EventHandler, type EventListenerState, type PersistentListenerOptions, PersistentSafeguardEventListener, type SafeguardEvent, SafeguardEventListener, type StateChangeHandler };
|
|
166
|
+
//# sourceMappingURL=events-RcqBZBsR.d.ts.map
|