@ovok/core 0.4.57 → 0.4.59-dev.556
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/conformance/capability-requirements.js +1 -1
- package/dist/next/resources/realtime/realtime-connection.d.ts +4 -4
- package/dist/next/resources/realtime/realtime-connection.js +44 -34
- package/dist/next/resources/realtime/realtime-resource.d.ts +6 -4
- package/dist/next/resources/realtime/realtime-resource.js +8 -5
- package/dist/next/resources/realtime/realtime-socket.d.ts +6 -5
- package/dist/next/resources/realtime/realtime-socket.js +16 -11
- package/package.json +1 -1
|
@@ -26,7 +26,7 @@ exports.OVOK_CORE_REQUIREMENTS = {
|
|
|
26
26
|
description: "The FHIR surface a server must provide for the @ovok/core client library's own methods to function.",
|
|
27
27
|
software: {
|
|
28
28
|
name: "@ovok/core",
|
|
29
|
-
version: "0.4.
|
|
29
|
+
version: "0.4.59-dev.556+6017fd9",
|
|
30
30
|
},
|
|
31
31
|
fhirVersion: "4.0.1",
|
|
32
32
|
format: ["json"],
|
|
@@ -7,11 +7,9 @@ export declare class RealtimeConnection {
|
|
|
7
7
|
private readonly fhir;
|
|
8
8
|
private readonly baseUrl;
|
|
9
9
|
private readonly session;
|
|
10
|
-
private readonly criteria;
|
|
11
10
|
private readonly handler;
|
|
12
|
-
private
|
|
11
|
+
private readonly entries;
|
|
13
12
|
private socket;
|
|
14
|
-
private binding;
|
|
15
13
|
private profileReference;
|
|
16
14
|
private accessToken;
|
|
17
15
|
private reconnectDelayMs;
|
|
@@ -23,9 +21,10 @@ export declare class RealtimeConnection {
|
|
|
23
21
|
private paused;
|
|
24
22
|
private stopped;
|
|
25
23
|
private deleted;
|
|
26
|
-
constructor(http: HttpClient, fhir: FhirResource, baseUrl: string, session: SessionResource, criteria: string, handler: RealtimeNotificationHandler);
|
|
24
|
+
constructor(http: HttpClient, fhir: FhirResource, baseUrl: string, session: SessionResource, criteria: readonly string[], handler: RealtimeNotificationHandler);
|
|
27
25
|
start(): Promise<void>;
|
|
28
26
|
unsubscribe(): Promise<void>;
|
|
27
|
+
private tokens;
|
|
29
28
|
private connect;
|
|
30
29
|
/**
|
|
31
30
|
* The server forgets a websocket Subscription once its socket has been gone a while, so a refused binding of one that
|
|
@@ -39,6 +38,7 @@ export declare class RealtimeConnection {
|
|
|
39
38
|
private socketClosed;
|
|
40
39
|
private sessionChanged;
|
|
41
40
|
private refreshBinding;
|
|
41
|
+
/** Every token is fetched again before the first of them expires. */
|
|
42
42
|
private scheduleBindingRefresh;
|
|
43
43
|
private scheduleReconnect;
|
|
44
44
|
private closeSocket;
|
|
@@ -17,11 +17,8 @@ class RealtimeConnection {
|
|
|
17
17
|
this.fhir = fhir;
|
|
18
18
|
this.baseUrl = baseUrl;
|
|
19
19
|
this.session = session;
|
|
20
|
-
this.criteria = criteria;
|
|
21
20
|
this.handler = handler;
|
|
22
|
-
this.subscriptionId = null;
|
|
23
21
|
this.socket = null;
|
|
24
|
-
this.binding = null;
|
|
25
22
|
this.profileReference = null;
|
|
26
23
|
this.accessToken = null;
|
|
27
24
|
this.reconnectDelayMs = INITIAL_RECONNECT_DELAY_MS;
|
|
@@ -33,10 +30,13 @@ class RealtimeConnection {
|
|
|
33
30
|
this.paused = false;
|
|
34
31
|
this.stopped = false;
|
|
35
32
|
this.deleted = false;
|
|
33
|
+
this.entries = criteria.map((each) => ({ criteria: each, subscriptionId: null, binding: null }));
|
|
36
34
|
}
|
|
37
35
|
async start() {
|
|
38
36
|
var _a, _b;
|
|
39
|
-
|
|
37
|
+
for (const entry of this.entries) {
|
|
38
|
+
entry.subscriptionId = await this.createSubscription(entry.criteria);
|
|
39
|
+
}
|
|
40
40
|
const session = await this.session.current();
|
|
41
41
|
this.profileReference = (_a = session === null || session === void 0 ? void 0 : session.profile.reference) !== null && _a !== void 0 ? _a : null;
|
|
42
42
|
this.accessToken = (_b = session === null || session === void 0 ? void 0 : session.accessToken) !== null && _b !== void 0 ? _b : null;
|
|
@@ -44,7 +44,7 @@ class RealtimeConnection {
|
|
|
44
44
|
await this.connect();
|
|
45
45
|
}
|
|
46
46
|
async unsubscribe() {
|
|
47
|
-
var _a, _b
|
|
47
|
+
var _a, _b;
|
|
48
48
|
if (this.deleted) {
|
|
49
49
|
return;
|
|
50
50
|
}
|
|
@@ -52,26 +52,34 @@ class RealtimeConnection {
|
|
|
52
52
|
(_a = this.removeSessionListener) === null || _a === void 0 ? void 0 : _a.call(this);
|
|
53
53
|
this.removeSessionListener = null;
|
|
54
54
|
this.clearTimers();
|
|
55
|
-
(_b = this.socket) === null || _b === void 0 ? void 0 : _b.close(
|
|
55
|
+
(_b = this.socket) === null || _b === void 0 ? void 0 : _b.close(this.tokens());
|
|
56
56
|
this.socket = null;
|
|
57
|
-
|
|
58
|
-
|
|
57
|
+
for (const entry of this.entries) {
|
|
58
|
+
if (entry.subscriptionId !== null) {
|
|
59
|
+
await this.fhir.delete("Subscription", entry.subscriptionId);
|
|
60
|
+
}
|
|
59
61
|
}
|
|
60
62
|
this.deleted = true;
|
|
61
63
|
}
|
|
64
|
+
tokens() {
|
|
65
|
+
return this.entries.flatMap((entry) => (entry.binding ? [entry.binding.token] : []));
|
|
66
|
+
}
|
|
62
67
|
async connect() {
|
|
63
68
|
if (this.stopped || this.paused || this.connecting || this.socket) {
|
|
64
69
|
return;
|
|
65
70
|
}
|
|
66
71
|
this.connecting = true;
|
|
67
72
|
try {
|
|
68
|
-
const
|
|
73
|
+
const bindings = [];
|
|
74
|
+
for (const entry of this.entries) {
|
|
75
|
+
bindings.push(await this.bindingOrFreshSubscription(entry));
|
|
76
|
+
}
|
|
69
77
|
if (this.stopped || this.paused) {
|
|
70
78
|
return;
|
|
71
79
|
}
|
|
72
|
-
this.binding =
|
|
73
|
-
this.scheduleBindingRefresh(
|
|
74
|
-
this.openSocket(
|
|
80
|
+
this.entries.forEach((entry, index) => { entry.binding = bindings[index]; });
|
|
81
|
+
this.scheduleBindingRefresh();
|
|
82
|
+
this.openSocket(bindings[0].url);
|
|
75
83
|
}
|
|
76
84
|
finally {
|
|
77
85
|
this.connecting = false;
|
|
@@ -81,36 +89,36 @@ class RealtimeConnection {
|
|
|
81
89
|
* The server forgets a websocket Subscription once its socket has been gone a while, so a refused binding of one that
|
|
82
90
|
* bound before gets a fresh Subscription; one refused before it ever bound ends the connection instead of retrying.
|
|
83
91
|
*/
|
|
84
|
-
async bindingOrFreshSubscription() {
|
|
92
|
+
async bindingOrFreshSubscription(entry) {
|
|
85
93
|
try {
|
|
86
|
-
return await this.fetchBinding();
|
|
94
|
+
return await this.fetchBinding(entry);
|
|
87
95
|
}
|
|
88
96
|
catch (error) {
|
|
89
97
|
if (!isForgottenSubscription(error)) {
|
|
90
98
|
throw error;
|
|
91
99
|
}
|
|
92
|
-
if (
|
|
100
|
+
if (entry.binding === null) {
|
|
93
101
|
await this.unsubscribe().catch(() => undefined);
|
|
94
102
|
throw error;
|
|
95
103
|
}
|
|
96
104
|
}
|
|
97
|
-
await this.replaceSubscription();
|
|
98
|
-
return this.bindingOrFreshSubscription();
|
|
105
|
+
await this.replaceSubscription(entry);
|
|
106
|
+
return this.bindingOrFreshSubscription(entry);
|
|
99
107
|
}
|
|
100
|
-
async replaceSubscription() {
|
|
101
|
-
const forgotten =
|
|
102
|
-
|
|
103
|
-
|
|
108
|
+
async replaceSubscription(entry) {
|
|
109
|
+
const forgotten = entry.subscriptionId;
|
|
110
|
+
entry.binding = null;
|
|
111
|
+
entry.subscriptionId = await this.createSubscription(entry.criteria);
|
|
104
112
|
if (forgotten !== null) {
|
|
105
113
|
await this.fhir.delete("Subscription", forgotten).catch(() => undefined);
|
|
106
114
|
}
|
|
107
115
|
}
|
|
108
|
-
async createSubscription() {
|
|
116
|
+
async createSubscription(criteria) {
|
|
109
117
|
const subscription = {
|
|
110
118
|
resourceType: "Subscription",
|
|
111
119
|
status: "active",
|
|
112
120
|
reason: "Ovok SDK realtime subscription",
|
|
113
|
-
criteria
|
|
121
|
+
criteria,
|
|
114
122
|
channel: { type: "websocket" },
|
|
115
123
|
};
|
|
116
124
|
const created = await this.fhir.create(subscription);
|
|
@@ -119,12 +127,12 @@ class RealtimeConnection {
|
|
|
119
127
|
}
|
|
120
128
|
return created.id;
|
|
121
129
|
}
|
|
122
|
-
fetchBinding() {
|
|
130
|
+
fetchBinding(entry) {
|
|
123
131
|
var _a;
|
|
124
|
-
return (0, realtime_binding_1.fetchRealtimeBinding)(this.http, (_a =
|
|
132
|
+
return (0, realtime_binding_1.fetchRealtimeBinding)(this.http, (_a = entry.subscriptionId) !== null && _a !== void 0 ? _a : "", this.baseUrl);
|
|
125
133
|
}
|
|
126
|
-
openSocket(
|
|
127
|
-
const socket = new realtime_socket_1.RealtimeSocket(
|
|
134
|
+
openSocket(url) {
|
|
135
|
+
const socket = new realtime_socket_1.RealtimeSocket(url, this.tokens(), this.handler, () => { this.reconnectDelayMs = INITIAL_RECONNECT_DELAY_MS; }, (closedSocket) => this.socketClosed(closedSocket));
|
|
128
136
|
this.socket = socket;
|
|
129
137
|
socket.open();
|
|
130
138
|
}
|
|
@@ -166,24 +174,26 @@ class RealtimeConnection {
|
|
|
166
174
|
if (this.refreshInFlight) {
|
|
167
175
|
return this.refreshInFlight;
|
|
168
176
|
}
|
|
169
|
-
this.refreshInFlight = this.fetchBinding()
|
|
170
|
-
.then((
|
|
177
|
+
this.refreshInFlight = Promise.all(this.entries.map((entry) => this.fetchBinding(entry)))
|
|
178
|
+
.then((bindings) => {
|
|
171
179
|
var _a;
|
|
172
180
|
if (this.stopped || this.paused) {
|
|
173
181
|
return;
|
|
174
182
|
}
|
|
175
|
-
this.binding =
|
|
176
|
-
this.scheduleBindingRefresh(
|
|
177
|
-
(_a = this.socket) === null || _a === void 0 ? void 0 : _a.bind(
|
|
183
|
+
this.entries.forEach((entry, index) => { entry.binding = bindings[index]; });
|
|
184
|
+
this.scheduleBindingRefresh();
|
|
185
|
+
(_a = this.socket) === null || _a === void 0 ? void 0 : _a.bind(this.tokens());
|
|
178
186
|
})
|
|
179
187
|
.finally(() => {
|
|
180
188
|
this.refreshInFlight = null;
|
|
181
189
|
});
|
|
182
190
|
return this.refreshInFlight;
|
|
183
191
|
}
|
|
184
|
-
|
|
192
|
+
/** Every token is fetched again before the first of them expires. */
|
|
193
|
+
scheduleBindingRefresh() {
|
|
185
194
|
this.clearRefreshTimer();
|
|
186
|
-
const
|
|
195
|
+
const expiresAt = Math.min(...this.entries.map((entry) => { var _a, _b; return (_b = (_a = entry.binding) === null || _a === void 0 ? void 0 : _a.expiresAt) !== null && _b !== void 0 ? _b : Infinity; }));
|
|
196
|
+
const delay = Math.max(TOKEN_REFRESH_MIN_DELAY_MS, expiresAt - Date.now() - TOKEN_REFRESH_MARGIN_MS);
|
|
187
197
|
this.refreshTimer = setTimeout(() => {
|
|
188
198
|
void this.refreshBinding().catch(() => { var _a; return (_a = this.socket) === null || _a === void 0 ? void 0 : _a.close(); });
|
|
189
199
|
}, delay);
|
|
@@ -15,10 +15,12 @@ export declare class RealtimeResource {
|
|
|
15
15
|
private readonly session;
|
|
16
16
|
constructor(http: HttpClient, fhir: FhirResource, baseUrl: string, session: SessionResource);
|
|
17
17
|
/**
|
|
18
|
-
* Subscribes to resources matching the FHIR search `criteria` and calls `handler` for each change. Creates
|
|
18
|
+
* Subscribes to resources matching the FHIR search `criteria` and calls `handler` for each change. Creates one
|
|
19
|
+
* `Subscription` resource per criteria and carries them all over one websocket, so several searches (a patient's
|
|
20
|
+
* Observations, Flags and DetectedIssues) cost one connection; a notification's `subscription` says which matched.
|
|
19
21
|
*
|
|
20
|
-
* @returns A function that unsubscribes.
|
|
21
|
-
* @throws OvokValidationError when
|
|
22
|
+
* @returns A function that unsubscribes from every criteria.
|
|
23
|
+
* @throws OvokValidationError when no criteria is given or one is empty.
|
|
22
24
|
*/
|
|
23
|
-
subscribe(criteria: string, handler: RealtimeNotificationHandler): Promise<RealtimeUnsubscribe>;
|
|
25
|
+
subscribe(criteria: string | readonly string[], handler: RealtimeNotificationHandler): Promise<RealtimeUnsubscribe>;
|
|
24
26
|
}
|
|
@@ -16,16 +16,19 @@ class RealtimeResource {
|
|
|
16
16
|
this.session = session;
|
|
17
17
|
}
|
|
18
18
|
/**
|
|
19
|
-
* Subscribes to resources matching the FHIR search `criteria` and calls `handler` for each change. Creates
|
|
19
|
+
* Subscribes to resources matching the FHIR search `criteria` and calls `handler` for each change. Creates one
|
|
20
|
+
* `Subscription` resource per criteria and carries them all over one websocket, so several searches (a patient's
|
|
21
|
+
* Observations, Flags and DetectedIssues) cost one connection; a notification's `subscription` says which matched.
|
|
20
22
|
*
|
|
21
|
-
* @returns A function that unsubscribes.
|
|
22
|
-
* @throws OvokValidationError when
|
|
23
|
+
* @returns A function that unsubscribes from every criteria.
|
|
24
|
+
* @throws OvokValidationError when no criteria is given or one is empty.
|
|
23
25
|
*/
|
|
24
26
|
async subscribe(criteria, handler) {
|
|
25
|
-
|
|
27
|
+
const searches = typeof criteria === "string" ? [criteria] : [...criteria];
|
|
28
|
+
if (searches.length === 0 || searches.some((search) => search.trim() === "")) {
|
|
26
29
|
throw new ovok_errors_1.OvokValidationError((0, ovok_errors_1.detailsFor)("A realtime subscription criteria string is required."));
|
|
27
30
|
}
|
|
28
|
-
const connection = new realtime_connection_1.RealtimeConnection(this.http, this.fhir, this.baseUrl, this.session,
|
|
31
|
+
const connection = new realtime_connection_1.RealtimeConnection(this.http, this.fhir, this.baseUrl, this.session, searches, handler);
|
|
29
32
|
try {
|
|
30
33
|
await connection.start();
|
|
31
34
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { RealtimeNotificationHandler } from "./realtime-types";
|
|
2
2
|
export declare class RealtimeSocket {
|
|
3
3
|
private readonly url;
|
|
4
|
-
private
|
|
4
|
+
private tokens;
|
|
5
5
|
private readonly handler;
|
|
6
6
|
private readonly onHandshake;
|
|
7
7
|
private readonly onClose;
|
|
@@ -9,15 +9,16 @@ export declare class RealtimeSocket {
|
|
|
9
9
|
private handshakeTimer;
|
|
10
10
|
private pingTimer;
|
|
11
11
|
private waitingForPong;
|
|
12
|
-
constructor(url: string,
|
|
12
|
+
constructor(url: string, tokens: readonly string[], handler: RealtimeNotificationHandler, onHandshake: () => void, onClose: (socket: RealtimeSocket) => void);
|
|
13
13
|
open(): void;
|
|
14
|
-
bind(
|
|
15
|
-
close(
|
|
14
|
+
bind(tokens: readonly string[]): void;
|
|
15
|
+
close(unbindTokens?: readonly string[]): void;
|
|
16
16
|
private socketOpened;
|
|
17
17
|
private socketMessage;
|
|
18
18
|
private socketClosed;
|
|
19
19
|
private sendPing;
|
|
20
|
-
|
|
20
|
+
/** One socket carries every subscription: each token is bound to it in turn. */
|
|
21
|
+
private sendBindings;
|
|
21
22
|
private clearTimers;
|
|
22
23
|
private clearHandshakeTimer;
|
|
23
24
|
}
|
|
@@ -6,9 +6,9 @@ const SOCKET_OPEN = 1;
|
|
|
6
6
|
const PING_INTERVAL_MS = 5000;
|
|
7
7
|
const SOCKET_HANDSHAKE_TIMEOUT_MS = 10000;
|
|
8
8
|
class RealtimeSocket {
|
|
9
|
-
constructor(url,
|
|
9
|
+
constructor(url, tokens, handler, onHandshake, onClose) {
|
|
10
10
|
this.url = url;
|
|
11
|
-
this.
|
|
11
|
+
this.tokens = tokens;
|
|
12
12
|
this.handler = handler;
|
|
13
13
|
this.onHandshake = onHandshake;
|
|
14
14
|
this.onClose = onClose;
|
|
@@ -28,18 +28,20 @@ class RealtimeSocket {
|
|
|
28
28
|
socket.addEventListener("error", () => socket.close());
|
|
29
29
|
socket.addEventListener("close", () => this.socketClosed(socket));
|
|
30
30
|
}
|
|
31
|
-
bind(
|
|
31
|
+
bind(tokens) {
|
|
32
32
|
var _a;
|
|
33
|
-
this.
|
|
33
|
+
this.tokens = tokens;
|
|
34
34
|
if (((_a = this.socket) === null || _a === void 0 ? void 0 : _a.readyState) === SOCKET_OPEN) {
|
|
35
|
-
this.
|
|
35
|
+
this.sendBindings(this.socket);
|
|
36
36
|
}
|
|
37
37
|
}
|
|
38
|
-
close(
|
|
38
|
+
close(unbindTokens = []) {
|
|
39
39
|
const socket = this.socket;
|
|
40
|
-
if ((socket === null || socket === void 0 ? void 0 : socket.readyState) === SOCKET_OPEN
|
|
40
|
+
if ((socket === null || socket === void 0 ? void 0 : socket.readyState) === SOCKET_OPEN) {
|
|
41
41
|
try {
|
|
42
|
-
|
|
42
|
+
for (const token of unbindTokens) {
|
|
43
|
+
socket.send(JSON.stringify({ type: "unbind-from-token", payload: { token } }));
|
|
44
|
+
}
|
|
43
45
|
}
|
|
44
46
|
catch (_a) {
|
|
45
47
|
socket.close();
|
|
@@ -53,7 +55,7 @@ class RealtimeSocket {
|
|
|
53
55
|
if (socket !== this.socket) {
|
|
54
56
|
return;
|
|
55
57
|
}
|
|
56
|
-
this.
|
|
58
|
+
this.sendBindings(socket);
|
|
57
59
|
this.handshakeTimer = setTimeout(() => socket.close(), SOCKET_HANDSHAKE_TIMEOUT_MS);
|
|
58
60
|
this.pingTimer = setInterval(() => this.sendPing(socket), PING_INTERVAL_MS);
|
|
59
61
|
}
|
|
@@ -118,9 +120,12 @@ class RealtimeSocket {
|
|
|
118
120
|
socket.close();
|
|
119
121
|
}
|
|
120
122
|
}
|
|
121
|
-
|
|
123
|
+
/** One socket carries every subscription: each token is bound to it in turn. */
|
|
124
|
+
sendBindings(socket) {
|
|
122
125
|
try {
|
|
123
|
-
|
|
126
|
+
for (const token of this.tokens) {
|
|
127
|
+
socket.send(JSON.stringify({ type: "bind-with-token", payload: { token } }));
|
|
128
|
+
}
|
|
124
129
|
}
|
|
125
130
|
catch (_a) {
|
|
126
131
|
socket.close();
|