@eyepop.ai/eyepop 0.18.0 → 0.18.2

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.
@@ -9830,28 +9830,31 @@ var Endpoint = class {
9830
9830
  }
9831
9831
  }
9832
9832
  async changePopComp(popComp) {
9833
- if (!this._baseUrl || !this._client) {
9833
+ const client = this._client;
9834
+ const baseUrl = this._baseUrl;
9835
+ if (!baseUrl || !client) {
9834
9836
  return Promise.reject("endpoint not connected, use connect() before changePopComp()");
9835
9837
  }
9836
- const patch_url = `${this._baseUrl}/pipelines/${this._pipelineId}/inferencePipeline`;
9837
- const headers = {
9838
- "Authorization": await this.authorizationHeader(),
9839
- "Content-Type": "application/json"
9840
- };
9841
9838
  const body = {
9842
9839
  "pipeline": popComp
9843
9840
  };
9844
- this._requestLogger.debug("before PATCH %s", patch_url);
9845
- let response = await this._client.fetch(patch_url, {
9846
- method: "PATCH",
9847
- body: JSON.stringify(body),
9848
- headers
9841
+ let response = await this.fetchWithRetry(async () => {
9842
+ const session = await this.session();
9843
+ let headers = {
9844
+ "Authorization": session.accessToken,
9845
+ "Content-Type": "application/json"
9846
+ };
9847
+ const patch_url = `${session.baseUrl}/pipelines/${session.pipelineId}/inferencePipeline`;
9848
+ return client.fetch(patch_url, {
9849
+ method: "PATCH",
9850
+ body: JSON.stringify(body),
9851
+ headers
9852
+ });
9849
9853
  });
9850
9854
  if (response.status != 204) {
9851
9855
  const message = await response.text();
9852
9856
  return Promise.reject(`Unexpected status ${response.status}: ${message}`);
9853
9857
  }
9854
- this._requestLogger.debug("after PATCH %s", patch_url);
9855
9858
  if (this._pipeline) {
9856
9859
  this._pipeline.inferPipeline = popComp;
9857
9860
  }
@@ -9894,25 +9897,27 @@ var Endpoint = class {
9894
9897
  return Promise.reject("option secretKey or environment variable EYEPOP_SECRET_KEY is required");
9895
9898
  }
9896
9899
  this._client = await createHttpClient();
9900
+ const client = this._client;
9897
9901
  let result = await this.reconnect();
9898
9902
  if (this._baseUrl && this._options.stopJobs) {
9899
- const stop_url = `${this._baseUrl}/pipelines/${this._pipelineId}/source?mode=preempt&processing=sync`;
9900
9903
  const body = { "sourceType": "NONE" };
9901
- const headers = {
9902
- "Authorization": await this.authorizationHeader(),
9903
- "Content-Type": "application/json"
9904
- };
9905
- this._requestLogger.debug("before PATCH %s", stop_url);
9906
- let response = await this._client.fetch(stop_url, {
9907
- method: "PATCH",
9908
- headers,
9909
- body: JSON.stringify(body)
9904
+ let response = await this.fetchWithRetry(async () => {
9905
+ const session = await this.session();
9906
+ const headers = {
9907
+ "Authorization": session.accessToken,
9908
+ "Content-Type": "application/json"
9909
+ };
9910
+ const stop_url = `${session.baseUrl}/pipelines/${session.pipelineId}/source?mode=preempt&processing=sync`;
9911
+ return client.fetch(stop_url, {
9912
+ method: "PATCH",
9913
+ headers,
9914
+ body: JSON.stringify(body)
9915
+ });
9910
9916
  });
9911
9917
  if (response.status >= 300) {
9912
9918
  const message = await response.text();
9913
9919
  return Promise.reject(`Unexpected status ${response.status}: ${message}`);
9914
9920
  }
9915
- this._requestLogger.debug("after PATCH %s", stop_url);
9916
9921
  }
9917
9922
  if (!this._ingressEventWs && this._ingressEventHandler) {
9918
9923
  await this.startIngressWs();
@@ -9987,25 +9992,19 @@ var Endpoint = class {
9987
9992
  }
9988
9993
  }
9989
9994
  async startIngressWs() {
9990
- if (this._ingressEventWs || !this._baseUrl || !this._client) {
9995
+ const client = this._client;
9996
+ if (this._ingressEventWs || !this._baseUrl || !client) {
9991
9997
  return;
9992
9998
  }
9993
9999
  try {
9994
- const getTokenUrl = `${this._baseUrl}/liveIngress/events/token`;
9995
- const headers = {
9996
- "Authorization": await this.authorizationHeader()
9997
- };
9998
- this._requestLogger.debug("before GET %s", getTokenUrl);
9999
- let response = await this._client.fetch(getTokenUrl, { headers });
10000
- if (response.status == 401) {
10001
- this._requestLogger.debug("after GET %s: 401, about to retry with fresh access token", getTokenUrl);
10002
- this._token = null;
10003
- const headers2 = {
10004
- "Authorization": await this.authorizationHeader()
10000
+ const session = await this.session();
10001
+ let response = await this.fetchWithRetry(async () => {
10002
+ const headers = {
10003
+ "Authorization": session.accessToken
10005
10004
  };
10006
- this.updateState("FetchConfig" /* FetchConfig */);
10007
- response = await this._client.fetch(getTokenUrl, { headers: headers2 });
10008
- }
10005
+ const getTokenUrl = `${session.baseUrl}/liveIngress/events/token`;
10006
+ return client.fetch(getTokenUrl, { headers });
10007
+ });
10009
10008
  if (response.status != 200) {
10010
10009
  const message = await response.text();
10011
10010
  return Promise.reject(`Unexpected status ${response.status}: ${message}`);
@@ -10151,6 +10150,39 @@ var Endpoint = class {
10151
10150
  this._expire_token_time = null;
10152
10151
  }
10153
10152
  }
10153
+ async fetchWithRetry(fetcher, retries = 1) {
10154
+ while (true) {
10155
+ try {
10156
+ const response = await fetcher();
10157
+ if (response.status == 401) {
10158
+ this._token = null;
10159
+ this._expire_token_time = null;
10160
+ if (retries--) {
10161
+ this._requestLogger.info("received 401 response, attempt to reauthorize and retry");
10162
+ } else {
10163
+ return Promise.reject(`response ${response.status}: ${response.statusText}`);
10164
+ }
10165
+ } else if (response.status == 404) {
10166
+ this._pipelineId = null;
10167
+ this._baseUrl = null;
10168
+ if (retries--) {
10169
+ this._requestLogger.info("received 404 response, attempt to restart pop and retry");
10170
+ } else {
10171
+ return Promise.reject(`response ${response.status}: ${response.statusText}`);
10172
+ }
10173
+ } else {
10174
+ return response;
10175
+ }
10176
+ } catch (error) {
10177
+ this._pipelineId = null;
10178
+ this._baseUrl = null;
10179
+ if (retries--) {
10180
+ this._requestLogger.info("unknown, attempt to restart pop (as if we received 404)", error);
10181
+ return Promise.reject(error);
10182
+ }
10183
+ }
10184
+ }
10185
+ }
10154
10186
  async reconnect() {
10155
10187
  if (!this._client) {
10156
10188
  return Promise.reject("endpoint not initialized");
@@ -10176,7 +10208,6 @@ var Endpoint = class {
10176
10208
  this.updateState("FetchConfig" /* FetchConfig */);
10177
10209
  response = await this._client.fetch(config_url, {
10178
10210
  headers: headers2
10179
- // @ts-ignore
10180
10211
  });
10181
10212
  }
10182
10213
  if (response.status != 200) {
@@ -10194,7 +10225,6 @@ var Endpoint = class {
10194
10225
  this.updateState("StartingPop" /* StartingPop */);
10195
10226
  response = await this._client.fetch(auto_start_config_url, {
10196
10227
  headers: headers2
10197
- // @ts-ignore
10198
10228
  });
10199
10229
  if (response.status != 200) {
10200
10230
  this.updateState("Error" /* Error */);