@arcote.tech/arc-adapter-db-postgres 0.5.2 → 0.5.6

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/index.js +24 -27
  2. package/package.json +2 -2
package/dist/index.js CHANGED
@@ -1401,6 +1401,9 @@ class AuthAdapter {
1401
1401
  localStorage.removeItem(TOKEN_PREFIX + scope);
1402
1402
  }
1403
1403
  }
1404
+ setDecoded(decoded, scope = "default") {
1405
+ this.scopes.set(scope, { raw: "", decoded });
1406
+ }
1404
1407
  loadPersisted() {
1405
1408
  if (!hasLocalStorage())
1406
1409
  return;
@@ -1598,7 +1601,8 @@ class EventWire {
1598
1601
  localId: e.localId,
1599
1602
  type: e.type,
1600
1603
  payload: e.payload,
1601
- createdAt: e.createdAt
1604
+ createdAt: e.createdAt,
1605
+ authContext: e.authContext
1602
1606
  }))
1603
1607
  }));
1604
1608
  }
@@ -1755,11 +1759,13 @@ class LocalEventPublisher {
1755
1759
  }
1756
1760
  async publish(event) {
1757
1761
  const allChanges = [];
1762
+ const eventAuthContext = event.authContext ?? null;
1758
1763
  const storedEvent = {
1759
1764
  _id: event.id,
1760
1765
  type: event.type,
1761
1766
  payload: JSON.stringify(event.payload),
1762
- createdAt: event.createdAt.toISOString()
1767
+ createdAt: event.createdAt.toISOString(),
1768
+ authContext: eventAuthContext ? JSON.stringify(eventAuthContext) : null
1763
1769
  };
1764
1770
  allChanges.push({
1765
1771
  store: EVENT_TABLES.events,
@@ -2897,11 +2903,14 @@ class ArcEvent extends ArcContextElement {
2897
2903
  if (!adapters.eventPublisher) {
2898
2904
  throw new Error(`Event "${this.data.name}" cannot be emitted: no eventPublisher adapter available`);
2899
2905
  }
2906
+ const decoded = adapters.authAdapter?.getDecoded() ?? null;
2907
+ const authContext = decoded ? { tokenName: decoded.tokenName, params: decoded.params } : null;
2900
2908
  const event = {
2901
2909
  type: this.data.name,
2902
2910
  payload,
2903
2911
  id: this.eventId.generate(),
2904
- createdAt: new Date
2912
+ createdAt: new Date,
2913
+ authContext
2905
2914
  };
2906
2915
  await adapters.eventPublisher.publish(event);
2907
2916
  }
@@ -3093,11 +3102,14 @@ class AggregateBase {
3093
3102
  if (!adapters.eventPublisher) {
3094
3103
  throw new Error(`Cannot emit event "${arcEvent.name}": no eventPublisher adapter available`);
3095
3104
  }
3105
+ const decoded = adapters.authAdapter?.getDecoded() ?? null;
3106
+ const authContext = decoded ? { tokenName: decoded.tokenName, params: decoded.params } : null;
3096
3107
  const eventInstance = {
3097
3108
  type: arcEvent.name,
3098
3109
  payload,
3099
3110
  id: `${Date.now()}_${Math.random().toString(36).slice(2)}`,
3100
- createdAt: new Date
3111
+ createdAt: new Date,
3112
+ authContext
3101
3113
  };
3102
3114
  await adapters.eventPublisher.publish(eventInstance);
3103
3115
  }
@@ -3316,31 +3328,15 @@ class ArcListener extends ArcContextElement {
3316
3328
  if (adapters.authAdapter?.isAuthenticated()) {
3317
3329
  return adapters;
3318
3330
  }
3319
- const allElements = [
3320
- ...this.data.queryElements,
3321
- ...this.data.mutationElements
3322
- ];
3323
- let tokenName = null;
3324
- for (const element of allElements) {
3325
- const protections = element.__aggregateProtections ?? element.data?.protections;
3326
- if (protections?.length > 0) {
3327
- tokenName = protections[0].token.name;
3328
- break;
3329
- }
3330
- }
3331
- if (!tokenName || !event2.payload) {
3331
+ const authContext = event2.authContext;
3332
+ if (!authContext) {
3332
3333
  return adapters;
3333
3334
  }
3334
3335
  const scopedAuth = new AuthAdapter;
3335
- scopedAuth.scopes = new Map([
3336
- ["default", {
3337
- raw: "",
3338
- decoded: {
3339
- tokenName,
3340
- params: event2.payload
3341
- }
3342
- }]
3343
- ]);
3336
+ scopedAuth.setDecoded({
3337
+ tokenName: authContext.tokenName,
3338
+ params: authContext.params
3339
+ });
3344
3340
  return { ...adapters, authAdapter: scopedAuth };
3345
3341
  }
3346
3342
  destroy() {
@@ -4467,7 +4463,8 @@ class StreamingEventPublisher {
4467
4463
  localId: event3.id,
4468
4464
  type: event3.type,
4469
4465
  payload: event3.payload,
4470
- createdAt: event3.createdAt.toISOString()
4466
+ createdAt: event3.createdAt.toISOString(),
4467
+ authContext: event3.authContext ?? null
4471
4468
  }
4472
4469
  ]);
4473
4470
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arcote.tech/arc-adapter-db-postgres",
3
- "version": "0.5.2",
3
+ "version": "0.5.6",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -23,7 +23,7 @@
23
23
  "postgres": "^3.4.4"
24
24
  },
25
25
  "peerDependencies": {
26
- "@arcote.tech/arc": "^0.5.2"
26
+ "@arcote.tech/arc": "^0.5.6"
27
27
  },
28
28
  "devDependencies": {
29
29
  "@types/pg": "^8.11.0",