@ovok/core 0.4.55 → 0.4.57

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.
@@ -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.55",
29
+ version: "0.4.57",
30
30
  },
31
31
  fhirVersion: "4.0.1",
32
32
  format: ["json"],
@@ -1,4 +1,4 @@
1
- import type { BundleEntry } from "@medplum/fhirtypes";
1
+ import type { BundleEntry, CodeableConcept } from "@medplum/fhirtypes";
2
2
  /**
3
3
  * What an outbox item holds: a whole reading, a growing series, one piece of a long recording, or a deletion.
4
4
  *
@@ -71,6 +71,8 @@ export type OutboxItem = OutboxItemSummary & {
71
71
  export type OutboxSentItem = Pick<OutboxItemSummary, "id" | "kind" | "group" | "seq" | "recordedAt"> & {
72
72
  /** When the server accepted it, in epoch milliseconds. */
73
73
  sentAt: number;
74
+ /** What the reading is: its main observation's code. A deletion has none. */
75
+ code?: CodeableConcept;
74
76
  };
75
77
  /**
76
78
  * How far a long recording has got.
@@ -29,6 +29,6 @@ export declare class RunnerState {
29
29
  supersede(id: string): void;
30
30
  /** True once if the item was superseded since it was planned. */
31
31
  wasSuperseded(id: string): boolean;
32
- remember({ id, kind, group, seq, recordedAt }: OutboxItem): void;
32
+ remember({ id, kind, group, seq, recordedAt, entries }: OutboxItem): void;
33
33
  recentlySent(): OutboxSentItem[];
34
34
  }
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.RunnerState = void 0;
4
+ const entry_observation_1 = require("./entry-observation");
4
5
  const failure_policy_1 = require("./failure-policy");
5
6
  const RECENTLY_SENT_LIMIT = 50;
6
7
  /** What the runner remembers between requests: pauses, connectivity, what is planned or sending, what was sent. */
@@ -49,9 +50,11 @@ class RunnerState {
49
50
  wasSuperseded(id) {
50
51
  return this.superseded.delete(id);
51
52
  }
52
- remember({ id, kind, group, seq, recordedAt }) {
53
+ remember({ id, kind, group, seq, recordedAt, entries }) {
54
+ var _a;
53
55
  const position = { ...(group === undefined ? {} : { group }), ...(seq === undefined ? {} : { seq }) };
54
- this.recent.unshift({ id, kind, recordedAt, sentAt: this.now(), ...position });
56
+ const code = (_a = (0, entry_observation_1.observationOf)(entries[0])) === null || _a === void 0 ? void 0 : _a.code;
57
+ this.recent.unshift({ id, kind, recordedAt, sentAt: this.now(), ...position, ...(code && { code }) });
55
58
  this.recent.length = Math.min(this.recent.length, RECENTLY_SENT_LIMIT);
56
59
  }
57
60
  recentlySent() {
@@ -7,8 +7,9 @@ export declare class RealtimeConnection {
7
7
  private readonly fhir;
8
8
  private readonly baseUrl;
9
9
  private readonly session;
10
- private readonly subscriptionId;
10
+ private readonly criteria;
11
11
  private readonly handler;
12
+ private subscriptionId;
12
13
  private socket;
13
14
  private binding;
14
15
  private profileReference;
@@ -22,10 +23,18 @@ export declare class RealtimeConnection {
22
23
  private paused;
23
24
  private stopped;
24
25
  private deleted;
25
- constructor(http: HttpClient, fhir: FhirResource, baseUrl: string, session: SessionResource, subscriptionId: string, handler: RealtimeNotificationHandler);
26
+ constructor(http: HttpClient, fhir: FhirResource, baseUrl: string, session: SessionResource, criteria: string, handler: RealtimeNotificationHandler);
26
27
  start(): Promise<void>;
27
28
  unsubscribe(): Promise<void>;
28
29
  private connect;
30
+ /**
31
+ * The server forgets a websocket Subscription once its socket has been gone a while, so a refused binding of one that
32
+ * bound before gets a fresh Subscription; one refused before it ever bound ends the connection instead of retrying.
33
+ */
34
+ private bindingOrFreshSubscription;
35
+ private replaceSubscription;
36
+ private createSubscription;
37
+ private fetchBinding;
29
38
  private openSocket;
30
39
  private socketClosed;
31
40
  private sessionChanged;
@@ -1,20 +1,25 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.RealtimeConnection = void 0;
4
+ const ovok_errors_1 = require("../../errors/ovok-errors");
4
5
  const realtime_binding_1 = require("./realtime-binding");
5
6
  const realtime_socket_1 = require("./realtime-socket");
6
7
  const TOKEN_REFRESH_MARGIN_MS = 5 * 60000;
7
8
  const TOKEN_REFRESH_MIN_DELAY_MS = 60000;
8
9
  const INITIAL_RECONNECT_DELAY_MS = 1000;
9
10
  const MAX_RECONNECT_DELAY_MS = 30000;
11
+ /** What the server answers for the binding of a Subscription it no longer has. */
12
+ const FORGOTTEN_SUBSCRIPTION_STATUSES = new Set([400, 404, 410]);
13
+ const isForgottenSubscription = (error) => error instanceof ovok_errors_1.OvokError && error.status !== null && FORGOTTEN_SUBSCRIPTION_STATUSES.has(error.status);
10
14
  class RealtimeConnection {
11
- constructor(http, fhir, baseUrl, session, subscriptionId, handler) {
15
+ constructor(http, fhir, baseUrl, session, criteria, handler) {
12
16
  this.http = http;
13
17
  this.fhir = fhir;
14
18
  this.baseUrl = baseUrl;
15
19
  this.session = session;
16
- this.subscriptionId = subscriptionId;
20
+ this.criteria = criteria;
17
21
  this.handler = handler;
22
+ this.subscriptionId = null;
18
23
  this.socket = null;
19
24
  this.binding = null;
20
25
  this.profileReference = null;
@@ -31,6 +36,7 @@ class RealtimeConnection {
31
36
  }
32
37
  async start() {
33
38
  var _a, _b;
39
+ this.subscriptionId = await this.createSubscription();
34
40
  const session = await this.session.current();
35
41
  this.profileReference = (_a = session === null || session === void 0 ? void 0 : session.profile.reference) !== null && _a !== void 0 ? _a : null;
36
42
  this.accessToken = (_b = session === null || session === void 0 ? void 0 : session.accessToken) !== null && _b !== void 0 ? _b : null;
@@ -48,7 +54,9 @@ class RealtimeConnection {
48
54
  this.clearTimers();
49
55
  (_b = this.socket) === null || _b === void 0 ? void 0 : _b.close((_c = this.binding) === null || _c === void 0 ? void 0 : _c.token);
50
56
  this.socket = null;
51
- await this.fhir.delete("Subscription", this.subscriptionId);
57
+ if (this.subscriptionId !== null) {
58
+ await this.fhir.delete("Subscription", this.subscriptionId);
59
+ }
52
60
  this.deleted = true;
53
61
  }
54
62
  async connect() {
@@ -57,7 +65,7 @@ class RealtimeConnection {
57
65
  }
58
66
  this.connecting = true;
59
67
  try {
60
- const binding = await (0, realtime_binding_1.fetchRealtimeBinding)(this.http, this.subscriptionId, this.baseUrl);
68
+ const binding = await this.bindingOrFreshSubscription();
61
69
  if (this.stopped || this.paused) {
62
70
  return;
63
71
  }
@@ -69,6 +77,52 @@ class RealtimeConnection {
69
77
  this.connecting = false;
70
78
  }
71
79
  }
80
+ /**
81
+ * The server forgets a websocket Subscription once its socket has been gone a while, so a refused binding of one that
82
+ * bound before gets a fresh Subscription; one refused before it ever bound ends the connection instead of retrying.
83
+ */
84
+ async bindingOrFreshSubscription() {
85
+ try {
86
+ return await this.fetchBinding();
87
+ }
88
+ catch (error) {
89
+ if (!isForgottenSubscription(error)) {
90
+ throw error;
91
+ }
92
+ if (this.binding === null) {
93
+ await this.unsubscribe().catch(() => undefined);
94
+ throw error;
95
+ }
96
+ }
97
+ await this.replaceSubscription();
98
+ return this.bindingOrFreshSubscription();
99
+ }
100
+ async replaceSubscription() {
101
+ const forgotten = this.subscriptionId;
102
+ this.binding = null;
103
+ this.subscriptionId = await this.createSubscription();
104
+ if (forgotten !== null) {
105
+ await this.fhir.delete("Subscription", forgotten).catch(() => undefined);
106
+ }
107
+ }
108
+ async createSubscription() {
109
+ const subscription = {
110
+ resourceType: "Subscription",
111
+ status: "active",
112
+ reason: "Ovok SDK realtime subscription",
113
+ criteria: this.criteria,
114
+ channel: { type: "websocket" },
115
+ };
116
+ const created = await this.fhir.create(subscription);
117
+ if (!created.id) {
118
+ throw new ovok_errors_1.OvokServiceError((0, ovok_errors_1.detailsFor)("The realtime subscription response did not include an id."));
119
+ }
120
+ return created.id;
121
+ }
122
+ fetchBinding() {
123
+ var _a;
124
+ return (0, realtime_binding_1.fetchRealtimeBinding)(this.http, (_a = this.subscriptionId) !== null && _a !== void 0 ? _a : "", this.baseUrl);
125
+ }
72
126
  openSocket(binding) {
73
127
  const socket = new realtime_socket_1.RealtimeSocket(binding.url, binding.token, this.handler, () => { this.reconnectDelayMs = INITIAL_RECONNECT_DELAY_MS; }, (closedSocket) => this.socketClosed(closedSocket));
74
128
  this.socket = socket;
@@ -112,7 +166,7 @@ class RealtimeConnection {
112
166
  if (this.refreshInFlight) {
113
167
  return this.refreshInFlight;
114
168
  }
115
- this.refreshInFlight = (0, realtime_binding_1.fetchRealtimeBinding)(this.http, this.subscriptionId, this.baseUrl)
169
+ this.refreshInFlight = this.fetchBinding()
116
170
  .then((binding) => {
117
171
  var _a;
118
172
  if (this.stopped || this.paused) {
@@ -25,18 +25,7 @@ class RealtimeResource {
25
25
  if (criteria.trim() === "") {
26
26
  throw new ovok_errors_1.OvokValidationError((0, ovok_errors_1.detailsFor)("A realtime subscription criteria string is required."));
27
27
  }
28
- const subscription = {
29
- resourceType: "Subscription",
30
- status: "active",
31
- reason: "Ovok SDK realtime subscription",
32
- criteria,
33
- channel: { type: "websocket" },
34
- };
35
- const created = await this.fhir.create(subscription);
36
- if (!created.id) {
37
- throw new ovok_errors_1.OvokServiceError((0, ovok_errors_1.detailsFor)("The realtime subscription response did not include an id."));
38
- }
39
- const connection = new realtime_connection_1.RealtimeConnection(this.http, this.fhir, this.baseUrl, this.session, created.id, handler);
28
+ const connection = new realtime_connection_1.RealtimeConnection(this.http, this.fhir, this.baseUrl, this.session, criteria, handler);
40
29
  try {
41
30
  await connection.start();
42
31
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ovok/core",
3
- "version": "0.4.55",
3
+ "version": "0.4.57",
4
4
  "private": false,
5
5
  "main": "dist/next/index.js",
6
6
  "types": "dist/next/index.d.ts",