@eyepop.ai/eyepop 0.18.0 → 0.18.1

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