@arcote.tech/arc-adapter-db-sqlite 0.5.2 → 0.5.5

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