@adonis-agora/authkit-server 0.81.2 → 0.82.0

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.
@@ -57,6 +57,24 @@ import { isSettingLocked, SettingLockedError } from './config_locks.js';
57
57
  export function supportsSettings(obj) {
58
58
  return !!obj && typeof obj.getSetting === 'function';
59
59
  }
60
+ const sharedStates = new WeakMap();
61
+ function sharedStateFor(db, connection) {
62
+ const fresh = () => ({ cache: new Map(), tablePresent: null, tableAbsentUntil: 0 });
63
+ if (!db || (typeof db !== 'object' && typeof db !== 'function'))
64
+ return fresh();
65
+ let byConnection = sharedStates.get(db);
66
+ if (!byConnection) {
67
+ byConnection = new Map();
68
+ sharedStates.set(db, byConnection);
69
+ }
70
+ const name = connection ?? '';
71
+ let state = byConnection.get(name);
72
+ if (!state) {
73
+ state = fresh();
74
+ byConnection.set(name, state);
75
+ }
76
+ return state;
77
+ }
60
78
  /** Chave de cache: combina key + escopo de org (null = global). */
61
79
  function cacheKey(key, orgId) {
62
80
  return orgId ? `${key}\x00${orgId}` : key;
@@ -70,13 +88,15 @@ export class RuntimeSettings {
70
88
  db;
71
89
  ttlMs;
72
90
  connectionName;
73
- cache = new Map();
74
- /** null = não foi verificado ainda; false = tabela ausente; true = presente */
75
- tablePresent = null;
91
+ shared;
76
92
  constructor(db, opts = {}) {
77
93
  this.db = db;
78
94
  this.ttlMs = opts.ttlMs ?? 15_000;
79
95
  this.connectionName = opts.connection;
96
+ this.shared = sharedStateFor(db, opts.connection);
97
+ }
98
+ get cache() {
99
+ return this.shared.cache;
80
100
  }
81
101
  /**
82
102
  * Retorna o objeto de conexão Lucid correto.
@@ -96,17 +116,24 @@ export class RuntimeSettings {
96
116
  * detecta a tabela corretamente independente do schema.
97
117
  *
98
118
  * Fail-safe: qualquer erro → false (tabela considerada ausente, sem lançar).
119
+ * "Presente" vale pela vida do processo; "ausente" é re-verificado após o TTL,
120
+ * para que uma migração rodada com o app no ar (ou um erro transitório) não
121
+ * desligue as settings até o restart.
99
122
  */
100
123
  async hasTable() {
101
- if (this.tablePresent !== null)
102
- return this.tablePresent;
124
+ const shared = this.shared;
125
+ if (shared.tablePresent === true)
126
+ return true;
127
+ if (shared.tablePresent === false && shared.tableAbsentUntil > Date.now())
128
+ return false;
103
129
  try {
104
130
  await this.conn().from('auth_settings').select('key').limit(1);
105
- this.tablePresent = true;
131
+ shared.tablePresent = true;
106
132
  return true;
107
133
  }
108
134
  catch {
109
- this.tablePresent = false;
135
+ shared.tablePresent = false;
136
+ shared.tableAbsentUntil = Date.now() + this.ttlMs;
110
137
  return false;
111
138
  }
112
139
  }