@graffiti-garden/implementation-decentralized 0.0.6 → 0.0.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.
@@ -37,6 +37,7 @@ import {
37
37
  type LabeledMessage,
38
38
  type MessageStream,
39
39
  } from "../1-services/4-inboxes";
40
+ import { GraffitiErrorUnauthorized } from "../1-services/utilities";
40
41
 
41
42
  import {
42
43
  StringEncoder,
@@ -205,6 +206,28 @@ export class GraffitiDecentralized implements Graffiti {
205
206
  this.login_(handle);
206
207
  }
207
208
  });
209
+
210
+ // If any methods throw an unauthorized error,
211
+ // log out the used session
212
+ for (const method of [
213
+ "post",
214
+ "get",
215
+ "delete",
216
+ "postMedia",
217
+ "getMedia",
218
+ "deleteMedia",
219
+ ]) {
220
+ const fn = (this as any)[method];
221
+ (this as any)[method] = async (...args: unknown[]) => {
222
+ try {
223
+ return await fn(...args);
224
+ } catch (e) {
225
+ const session = args[args.length - 1] as any;
226
+ await this.tryLogoutIfUnauthorized(e, session);
227
+ throw e;
228
+ }
229
+ };
230
+ }
208
231
  }
209
232
 
210
233
  readonly actorToHandle: Graffiti["actorToHandle"] =
@@ -720,6 +743,7 @@ export class GraffitiDecentralized implements Graffiti {
720
743
  error: next.error,
721
744
  origin: allInboxes[next.index].serviceEndpoint,
722
745
  };
746
+ this.tryLogoutIfUnauthorized(next.error, session);
723
747
  } else if (next.result.done) {
724
748
  // Store the cursor for future use
725
749
  const inbox = allInboxes[next.index];
@@ -783,6 +807,9 @@ export class GraffitiDecentralized implements Graffiti {
783
807
  }
784
808
  }
785
809
  }
810
+ } catch (e) {
811
+ this.tryLogoutIfUnauthorized(e, session);
812
+ throw e;
786
813
  } finally {
787
814
  await Promise.all(
788
815
  iterators.map<Promise<void>>(async (it) => {
@@ -823,6 +850,15 @@ export class GraffitiDecentralized implements Graffiti {
823
850
  return this.discoverMeta<{}>(channels, {}, cursors, session);
824
851
  };
825
852
 
853
+ protected async tryLogoutIfUnauthorized(
854
+ e: unknown,
855
+ session?: GraffitiSession | null,
856
+ ) {
857
+ if (e instanceof GraffitiErrorUnauthorized && session) {
858
+ await this.sessions.logout(session.actor);
859
+ }
860
+ }
861
+
826
862
  async announceObject(
827
863
  object: GraffitiObjectBase,
828
864
  tags: Uint8Array[],