@medplum/agent 3.2.7 → 3.2.8

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.
Files changed (2) hide show
  1. package/dist/cjs/index.cjs +39 -35
  2. package/package.json +5 -5
@@ -43104,7 +43104,7 @@ function Bn(r7) {
43104
43104
  return typeof r7 == "object" && !Array.isArray(r7) && !(r7 instanceof URLSearchParams) && (r7 = Object.fromEntries(Object.entries(r7).filter((e) => e[1] !== void 0))), new URLSearchParams(r7).toString();
43105
43105
  }
43106
43106
  var Io = /^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-_]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-_]*[A-Za-z0-9])$/;
43107
- function ju(r7) {
43107
+ function $u(r7) {
43108
43108
  return Io.test(r7);
43109
43109
  }
43110
43110
  function ct(r7, e) {
@@ -44141,9 +44141,9 @@ var Dt = class {
44141
44141
  throw new f(h("Not a valid URL"));
44142
44142
  }
44143
44143
  let o = n?.ReconnectingWebSocket ? new n.ReconnectingWebSocket(i, void 0, { debug: n?.debug, debugLogger: n?.debugLogger }) : new Vt(i, void 0, { debug: n?.debug, debugLogger: n?.debugLogger });
44144
- this.medplum = e, this.ws = o, this.masterSubEmitter = new je(), this.criteriaEntries = /* @__PURE__ */ new Map(), this.criteriaEntriesBySubscriptionId = /* @__PURE__ */ new Map(), this.wsClosed = false, this.pingIntervalMs = n?.pingIntervalMs ?? Gs, this.setupWebSocketListeners();
44144
+ this.medplum = e, this.ws = o, this.masterSubEmitter = new je(), this.criteriaEntries = /* @__PURE__ */ new Map(), this.criteriaEntriesBySubscriptionId = /* @__PURE__ */ new Map(), this.wsClosed = false, this.pingIntervalMs = n?.pingIntervalMs ?? Gs, this.currentProfile = e.getProfile(), this.setupListeners();
44145
44145
  }
44146
- setupWebSocketListeners() {
44146
+ setupListeners() {
44147
44147
  let e = this.ws;
44148
44148
  e.addEventListener("message", (t) => {
44149
44149
  try {
@@ -44201,6 +44201,9 @@ var Dt = class {
44201
44201
  }
44202
44202
  e.send(JSON.stringify({ type: "ping" })), this.waitingForPong = true;
44203
44203
  }, this.pingIntervalMs));
44204
+ }), this.medplum.addEventListener("change", () => {
44205
+ let t = this.medplum.getProfile();
44206
+ this.currentProfile && t === void 0 ? this.ws.close() : t && this.currentProfile?.id !== t.id && this.ws.reconnect(), this.currentProfile = t;
44204
44207
  });
44205
44208
  }
44206
44209
  emitError(e, t) {
@@ -44287,7 +44290,7 @@ var Dt = class {
44287
44290
  this.wsClosed || (this.wsClosed = true, this.ws.close());
44288
44291
  }
44289
44292
  reconnectWebSocket() {
44290
- this.ws.reconnect();
44293
+ this.ws.reconnect(), this.wsClosed = false;
44291
44294
  }
44292
44295
  getCriteriaCount() {
44293
44296
  return this.getAllCriteriaEmitters().length;
@@ -44296,24 +44299,25 @@ var Dt = class {
44296
44299
  return this.masterSubEmitter || (this.masterSubEmitter = new je(...Array.from(this.criteriaEntries.keys()))), this.masterSubEmitter;
44297
44300
  }
44298
44301
  };
44299
- var Ld = "3.2.7-a4111e333";
44302
+ var Ud = "3.2.8-b2b7ed8c5";
44300
44303
  var Ks = D.FHIR_JSON + ", */*; q=0.1";
44301
44304
  var zs = "https://api.medplum.com/";
44302
44305
  var Js = 1e3;
44303
44306
  var Ys = 6e4;
44304
44307
  var Zs = 0;
44305
- var Xs = "Binary/";
44308
+ var Xs = 3e5;
44309
+ var ea = "Binary/";
44306
44310
  var hi = { resourceType: "Device", id: "system", deviceName: [{ type: "model-name", name: "System" }] };
44307
- var ea = ((o) => (o.ClientCredentials = "client_credentials", o.AuthorizationCode = "authorization_code", o.RefreshToken = "refresh_token", o.JwtBearer = "urn:ietf:params:oauth:grant-type:jwt-bearer", o.TokenExchange = "urn:ietf:params:oauth:grant-type:token-exchange", o))(ea || {});
44308
- var ta = ((o) => (o.AccessToken = "urn:ietf:params:oauth:token-type:access_token", o.RefreshToken = "urn:ietf:params:oauth:token-type:refresh_token", o.IdToken = "urn:ietf:params:oauth:token-type:id_token", o.Saml1Token = "urn:ietf:params:oauth:token-type:saml1", o.Saml2Token = "urn:ietf:params:oauth:token-type:saml2", o))(ta || {});
44309
- var ra = ((o) => (o.ClientSecretBasic = "client_secret_basic", o.ClientSecretPost = "client_secret_post", o.ClientSecretJwt = "client_secret_jwt", o.PrivateKeyJwt = "private_key_jwt", o.None = "none", o))(ra || {});
44310
- var na = ((e) => (e.JwtBearer = "urn:ietf:params:oauth:client-assertion-type:jwt-bearer", e))(na || {});
44311
+ var ta = ((o) => (o.ClientCredentials = "client_credentials", o.AuthorizationCode = "authorization_code", o.RefreshToken = "refresh_token", o.JwtBearer = "urn:ietf:params:oauth:grant-type:jwt-bearer", o.TokenExchange = "urn:ietf:params:oauth:grant-type:token-exchange", o))(ta || {});
44312
+ var ra = ((o) => (o.AccessToken = "urn:ietf:params:oauth:token-type:access_token", o.RefreshToken = "urn:ietf:params:oauth:token-type:refresh_token", o.IdToken = "urn:ietf:params:oauth:token-type:id_token", o.Saml1Token = "urn:ietf:params:oauth:token-type:saml1", o.Saml2Token = "urn:ietf:params:oauth:token-type:saml2", o))(ra || {});
44313
+ var na = ((o) => (o.ClientSecretBasic = "client_secret_basic", o.ClientSecretPost = "client_secret_post", o.ClientSecretJwt = "client_secret_jwt", o.PrivateKeyJwt = "private_key_jwt", o.None = "none", o))(na || {});
44314
+ var ia = ((e) => (e.JwtBearer = "urn:ietf:params:oauth:client-assertion-type:jwt-bearer", e))(ia || {});
44311
44315
  var Mt = class extends H {
44312
44316
  constructor(t) {
44313
44317
  super();
44314
44318
  this.initComplete = true;
44315
44319
  if (t?.baseUrl && !t.baseUrl.startsWith("http")) throw new Error("Base URL must start with http or https");
44316
- this.options = t ?? {}, this.fetch = t?.fetch ?? ia(), this.storage = t?.storage ?? new qe(), this.createPdfImpl = t?.createPdf, this.baseUrl = vr(t?.baseUrl ?? zs), this.fhirBaseUrl = $(this.baseUrl, t?.fhirUrlPath ?? "fhir/R4"), this.authorizeUrl = $(this.baseUrl, t?.authorizeUrl ?? "oauth2/authorize"), this.tokenUrl = $(this.baseUrl, t?.tokenUrl ?? "oauth2/token"), this.logoutUrl = $(this.baseUrl, t?.logoutUrl ?? "oauth2/logout"), this.clientId = t?.clientId ?? "", this.clientSecret = t?.clientSecret ?? "", this.onUnauthenticated = t?.onUnauthenticated, this.cacheTime = t?.cacheTime ?? (typeof window > "u" ? Zs : Ys), this.cacheTime > 0 ? this.requestCache = new Pt(t?.resourceCacheSize ?? Js) : this.requestCache = void 0, t?.autoBatchTime ? (this.autoBatchTime = t.autoBatchTime, this.autoBatchQueue = []) : (this.autoBatchTime = 0, this.autoBatchQueue = void 0), t?.accessToken && this.setAccessToken(t.accessToken), this.storage.getInitPromise === void 0 ? (t?.accessToken || this.attemptResumeActiveLogin().catch(console.error), this.initPromise = Promise.resolve(), this.dispatchEvent({ type: "storageInitialized" })) : (this.initComplete = false, this.initPromise = this.storage.getInitPromise(), this.initPromise.then(() => {
44320
+ this.options = t ?? {}, this.fetch = t?.fetch ?? oa(), this.storage = t?.storage ?? new qe(), this.createPdfImpl = t?.createPdf, this.baseUrl = vr(t?.baseUrl ?? zs), this.fhirBaseUrl = $(this.baseUrl, t?.fhirUrlPath ?? "fhir/R4"), this.authorizeUrl = $(this.baseUrl, t?.authorizeUrl ?? "oauth2/authorize"), this.tokenUrl = $(this.baseUrl, t?.tokenUrl ?? "oauth2/token"), this.logoutUrl = $(this.baseUrl, t?.logoutUrl ?? "oauth2/logout"), this.clientId = t?.clientId ?? "", this.clientSecret = t?.clientSecret ?? "", this.onUnauthenticated = t?.onUnauthenticated, this.refreshGracePeriod = t?.refreshGracePeriod ?? Xs, this.cacheTime = t?.cacheTime ?? (typeof window > "u" ? Zs : Ys), this.cacheTime > 0 ? this.requestCache = new Pt(t?.resourceCacheSize ?? Js) : this.requestCache = void 0, t?.autoBatchTime ? (this.autoBatchTime = t.autoBatchTime, this.autoBatchQueue = []) : (this.autoBatchTime = 0, this.autoBatchQueue = void 0), t?.accessToken && this.setAccessToken(t.accessToken), this.storage.getInitPromise === void 0 ? (t?.accessToken || this.attemptResumeActiveLogin().catch(console.error), this.initPromise = Promise.resolve(), this.dispatchEvent({ type: "storageInitialized" })) : (this.initComplete = false, this.initPromise = this.storage.getInitPromise(), this.initPromise.then(() => {
44317
44321
  t?.accessToken || this.attemptResumeActiveLogin().catch(console.error), this.initComplete = true, this.dispatchEvent({ type: "storageInitialized" });
44318
44322
  }).catch((n) => {
44319
44323
  console.error(n), this.initComplete = true, this.dispatchEvent({ type: "storageInitFailed", payload: { error: n } });
@@ -44618,7 +44622,7 @@ var Mt = class extends H {
44618
44622
  }
44619
44623
  async createPdf(t, n, i, o) {
44620
44624
  if (!this.createPdfImpl) throw new Error("PDF creation not enabled");
44621
- let s = aa(t, n, i, o), a2 = typeof n == "object" ? n : {}, { docDefinition: c, tableLayouts: u2, fonts: l2, ...m2 } = s, g = await this.createPdfImpl(c, u2, l2), Q = { ...m2, data: g, contentType: "application/pdf" };
44625
+ let s = ca(t, n, i, o), a2 = typeof n == "object" ? n : {}, { docDefinition: c, tableLayouts: u2, fonts: l2, ...m2 } = s, g = await this.createPdfImpl(c, u2, l2), Q = { ...m2, data: g, contentType: "application/pdf" };
44622
44626
  return this.createBinary(Q, a2);
44623
44627
  }
44624
44628
  createComment(t, n, i) {
@@ -44725,7 +44729,7 @@ var Mt = class extends H {
44725
44729
  async download(t, n = {}) {
44726
44730
  this.refreshPromise && await this.refreshPromise;
44727
44731
  let i = t.toString();
44728
- i.startsWith(Xs) && (t = this.fhirUrl(i));
44732
+ i.startsWith(ea) && (t = this.fhirUrl(i));
44729
44733
  let o = n.headers;
44730
44734
  return o || (o = {}, n.headers = o), o.Accept || (o.Accept = "*/*"), this.addFetchOptionsDefaults(n), (await this.fetchWithRetry(t.toString(), n)).blob();
44731
44735
  }
@@ -44880,8 +44884,8 @@ var Mt = class extends H {
44880
44884
  }
44881
44885
  return this.fetchTokens(i);
44882
44886
  }
44883
- refreshIfExpired() {
44884
- return !this.refreshPromise && this.accessTokenExpires !== void 0 && this.accessTokenExpires < Date.now() && this.refresh(), this.refreshPromise ?? Promise.resolve();
44887
+ refreshIfExpired(t) {
44888
+ return t === void 0 && (t = this.refreshGracePeriod), !this.refreshPromise && this.accessTokenExpires !== void 0 && Date.now() > this.accessTokenExpires - t && this.refresh(), this.refreshPromise ?? Promise.resolve();
44885
44889
  }
44886
44890
  refresh() {
44887
44891
  if (this.refreshPromise) return this.refreshPromise;
@@ -44985,7 +44989,7 @@ var Mt = class extends H {
44985
44989
  return this.getSubscriptionManager().getMasterEmitter();
44986
44990
  }
44987
44991
  };
44988
- function ia() {
44992
+ function oa() {
44989
44993
  if (!globalThis.fetch) throw new Error("Fetch not available in this environment");
44990
44994
  return globalThis.fetch.bind(globalThis);
44991
44995
  }
@@ -45003,22 +45007,22 @@ function vi(r7) {
45003
45007
  let e = r7.entry?.map((t) => t.resource) ?? [];
45004
45008
  return Object.assign(e, { bundle: r7 });
45005
45009
  }
45006
- function oa(r7) {
45010
+ function sa(r7) {
45007
45011
  return b(r7) && "data" in r7 && "contentType" in r7;
45008
45012
  }
45009
45013
  function xi(r7, e, t, n) {
45010
- return oa(r7) ? r7 : { data: r7, filename: e, contentType: t, onProgress: n };
45014
+ return sa(r7) ? r7 : { data: r7, filename: e, contentType: t, onProgress: n };
45011
45015
  }
45012
- function sa(r7) {
45016
+ function aa(r7) {
45013
45017
  return b(r7) && "docDefinition" in r7;
45014
45018
  }
45015
- function aa(r7, e, t, n) {
45016
- return sa(r7) ? r7 : { docDefinition: r7, filename: e, tableLayouts: t, fonts: n };
45019
+ function ca(r7, e, t, n) {
45020
+ return aa(r7) ? r7 : { docDefinition: r7, filename: e, tableLayouts: t, fonts: n };
45017
45021
  }
45018
- var xa = [...Le, "->", "<<", ">>", "=="];
45019
- var Ea = Ue().registerInfix("->", { precedence: y.Arrow }).registerInfix(";", { precedence: y.Semicolon });
45020
- var Ba = [...Le, "eq", "ne", "co"];
45021
- var $a = Ue();
45022
+ var Ta = [...Le, "->", "<<", ">>", "=="];
45023
+ var ba = Ue().registerInfix("->", { precedence: y.Arrow }).registerInfix(";", { precedence: y.Semicolon });
45024
+ var qa = [...Le, "eq", "ne", "co"];
45025
+ var Wa = Ue();
45022
45026
  var se = class {
45023
45027
  constructor(e = "\r", t = "|", n = "^", i = "~", o = "\\", s = "&") {
45024
45028
  this.segmentSeparator = e;
@@ -45059,7 +45063,7 @@ var Ni = class r2 {
45059
45063
  }
45060
45064
  buildAck() {
45061
45065
  let e = /* @__PURE__ */ new Date(), t = this.getSegment("MSH"), n = t?.getField(3)?.toString() ?? "", i = t?.getField(4)?.toString() ?? "", o = t?.getField(5)?.toString() ?? "", s = t?.getField(6)?.toString() ?? "", a2 = t?.getField(10)?.toString() ?? "", c = t?.getField(12)?.toString() ?? "2.5.1";
45062
- return new r2([new Ge(["MSH", this.context.getMsh2(), o, s, n, i, Ga(e), "", this.buildAckMessageType(t), e.getTime().toString(), "P", c], this.context), new Ge(["MSA", "AA", a2, "OK"], this.context)]);
45066
+ return new r2([new Ge(["MSH", this.context.getMsh2(), o, s, n, i, Ha(e), "", this.buildAckMessageType(t), e.getTime().toString(), "P", c], this.context), new Ge(["MSA", "AA", a2, "OK"], this.context)]);
45063
45067
  }
45064
45068
  buildAckMessageType(e) {
45065
45069
  let t = e?.getField(9), n = t?.getComponent(2), i = t?.getComponent(3), o = "ACK";
@@ -45117,7 +45121,7 @@ var Ve = class r4 {
45117
45121
  return new r4(e.split(t.repetitionSeparator).map((n) => n.split(t.componentSeparator)), t);
45118
45122
  }
45119
45123
  };
45120
- function Ga(r7) {
45124
+ function Ha(r7) {
45121
45125
  let e = r7 instanceof Date ? r7 : new Date(r7), n = e.toISOString().replace(/[-:T]/g, "").replace(/(\.\d+)?Z$/, ""), i = e.getUTCMilliseconds();
45122
45126
  return i > 0 && (n += "." + i.toString()), n;
45123
45127
  }
@@ -45155,7 +45159,7 @@ var Fi = class r5 {
45155
45159
  `) }), this.write(JSON.stringify({ level: jr[e], timestamp: (/* @__PURE__ */ new Date()).toISOString(), msg: this.prefix ? `${this.prefix}${t}` : t, ...n, ...this.metadata })));
45156
45160
  }
45157
45161
  };
45158
- function Yf(r7) {
45162
+ function Zf(r7) {
45159
45163
  let e = jr[r7.toUpperCase()];
45160
45164
  if (e === void 0) throw new Error(`Invalid log level: ${r7}`);
45161
45165
  return e;
@@ -45743,7 +45747,7 @@ var App = class _App {
45743
45747
  if ((0, import_node_fs3.existsSync)(UPGRADE_MANIFEST_PATH)) {
45744
45748
  const upgradeFile = (0, import_node_fs3.readFileSync)(UPGRADE_MANIFEST_PATH, { encoding: "utf-8" });
45745
45749
  const upgradeDetails = JSON.parse(upgradeFile);
45746
- if (upgradeDetails.targetVersion === Ld.split("-")[0]) {
45750
+ if (upgradeDetails.targetVersion === Ud.split("-")[0]) {
45747
45751
  await this.sendToWebSocket({
45748
45752
  type: "agent:upgrade:response",
45749
45753
  statusCode: 200,
@@ -45751,7 +45755,7 @@ var App = class _App {
45751
45755
  });
45752
45756
  this.log.info(`Successfully upgraded to version ${upgradeDetails.targetVersion}`);
45753
45757
  } else {
45754
- const errMsg = `Failed to upgrade to version ${upgradeDetails.targetVersion}. Agent still running with version ${Ld}`;
45758
+ const errMsg = `Failed to upgrade to version ${upgradeDetails.targetVersion}. Agent still running with version ${Ud}`;
45755
45759
  await this.sendToWebSocket({
45756
45760
  type: "agent:error",
45757
45761
  body: errMsg,
@@ -45820,7 +45824,7 @@ var App = class _App {
45820
45824
  this.startWebSocketWorker();
45821
45825
  break;
45822
45826
  case "agent:heartbeat:request":
45823
- await this.sendToWebSocket({ type: "agent:heartbeat:response", version: Ld });
45827
+ await this.sendToWebSocket({ type: "agent:heartbeat:response", version: Ud });
45824
45828
  break;
45825
45829
  case "agent:heartbeat:response":
45826
45830
  break;
@@ -46096,7 +46100,7 @@ IPv6 is currently unsupported.`;
46096
46100
  this.log.error(errMsg);
46097
46101
  throw new Error(errMsg);
46098
46102
  }
46099
- if (!((0, import_node_net.isIPv4)(message.remote) || ju(message.remote))) {
46103
+ if (!((0, import_node_net.isIPv4)(message.remote) || $u(message.remote))) {
46100
46104
  const errMsg = `Attempted to ping an invalid host.
46101
46105
 
46102
46106
  "${message.remote}" is not a valid IPv4 address or a resolvable hostname.`;
@@ -46207,11 +46211,11 @@ ${result}`);
46207
46211
  await this.stop();
46208
46212
  this.log.info("Successfully stopped agent network services");
46209
46213
  const targetVersion = message.version ?? await fetchLatestVersionString();
46210
- this.log.info("Writing upgrade manifest...", { previousVersion: Ld, targetVersion });
46214
+ this.log.info("Writing upgrade manifest...", { previousVersion: Ud, targetVersion });
46211
46215
  (0, import_node_fs3.writeFileSync)(
46212
46216
  UPGRADE_MANIFEST_PATH,
46213
46217
  JSON.stringify({
46214
- previousVersion: Ld,
46218
+ previousVersion: Ud,
46215
46219
  targetVersion,
46216
46220
  callback: message.callback ?? null
46217
46221
  }),
@@ -46348,7 +46352,7 @@ async function agentMain(argv) {
46348
46352
  const { baseUrl, clientId, clientSecret, agentId } = args;
46349
46353
  const medplum = new Mt({ baseUrl, clientId });
46350
46354
  await medplum.startClientLogin(clientId, clientSecret);
46351
- const app = new App(medplum, agentId, Yf(args.logLevel ?? "INFO"));
46355
+ const app = new App(medplum, agentId, Zf(args.logLevel ?? "INFO"));
46352
46356
  await app.start();
46353
46357
  process.on("SIGINT", async () => {
46354
46358
  console.log("Gracefully shutting down from SIGINT (Ctrl-C)");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@medplum/agent",
3
- "version": "3.2.7",
3
+ "version": "3.2.8",
4
4
  "description": "Medplum Agent",
5
5
  "homepage": "https://www.medplum.com/",
6
6
  "bugs": {
@@ -24,15 +24,15 @@
24
24
  "test": "jest"
25
25
  },
26
26
  "dependencies": {
27
- "@medplum/core": "3.2.7",
28
- "@medplum/hl7": "3.2.7",
27
+ "@medplum/core": "3.2.8",
28
+ "@medplum/hl7": "3.2.8",
29
29
  "dcmjs-dimse": "0.1.28",
30
30
  "iconv-lite": "0.6.3",
31
31
  "ws": "8.18.0"
32
32
  },
33
33
  "devDependencies": {
34
- "@medplum/fhirtypes": "3.2.7",
35
- "@medplum/mock": "3.2.7",
34
+ "@medplum/fhirtypes": "3.2.8",
35
+ "@medplum/mock": "3.2.8",
36
36
  "@types/async-eventemitter": "0.2.4",
37
37
  "@types/ws": "8.5.12",
38
38
  "mock-socket": "9.3.1",