@guren/core 1.8.0 → 1.8.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.
- package/dist/bin.d.ts +1 -5
- package/dist/bin.js +1 -2
- package/dist/index.d.ts +113 -111
- package/dist/index.js +342 -287
- package/dist/internal/deploy-build.d.ts +71 -70
- package/dist/internal/deploy-build.js +467 -209
- package/dist/internal/zod-compat.d.ts +12 -11
- package/dist/internal/zod-compat.js +112 -60
- package/dist/lambda.d.ts +2 -1
- package/dist/lambda.js +2 -17
- package/dist/redis.d.ts +2 -1
- package/dist/redis.js +2 -23
- package/dist/runtime.d.ts +1 -1
- package/dist/runtime.js +1 -1
- package/dist/vite.d.ts +3 -2
- package/dist/vite.js +2 -5
- package/package.json +11 -7
package/dist/index.js
CHANGED
|
@@ -1,300 +1,355 @@
|
|
|
1
|
-
|
|
2
|
-
export * from "@guren/server";
|
|
3
|
-
import {
|
|
4
|
-
Model as Model4,
|
|
5
|
-
defineModel,
|
|
6
|
-
ModelNotFoundException,
|
|
7
|
-
MassAssignmentException,
|
|
8
|
-
QueryBuilder,
|
|
9
|
-
SoftDeletes,
|
|
10
|
-
DrizzleAdapter,
|
|
11
|
-
createPostgresDatabase,
|
|
12
|
-
createMySqlDatabase,
|
|
13
|
-
createSqliteDatabase,
|
|
14
|
-
createD1Database,
|
|
15
|
-
createAwsDataApiDatabase,
|
|
16
|
-
runSeeders,
|
|
17
|
-
defineSeeder
|
|
18
|
-
} from "@guren/orm";
|
|
19
|
-
|
|
20
|
-
// src/api-token-store.ts
|
|
21
|
-
import { Model } from "@guren/orm";
|
|
22
|
-
|
|
23
|
-
// src/store-utils.ts
|
|
1
|
+
import { DrizzleAdapter, MassAssignmentException, Model, Model as Model$1, ModelNotFoundException, QueryBuilder, SoftDeletes, createAwsDataApiDatabase, createD1Database, createMySqlDatabase, createPostgresDatabase, createSqliteDatabase, defineModel, defineSeeder, runSeeders } from "@guren/orm";
|
|
24
2
|
import { isExpired, toDate, toOptionalExpiry } from "@guren/server/support/expiry";
|
|
3
|
+
export * from "@guren/server";
|
|
4
|
+
//#region src/store-utils.ts
|
|
5
|
+
/**
|
|
6
|
+
* Decode a JSON-capable column that may hold either the native value
|
|
7
|
+
* (drizzle json mode) or a text-encoded string. Corrupt text falls back
|
|
8
|
+
* instead of throwing, so one bad row cannot break every read.
|
|
9
|
+
*
|
|
10
|
+
* Stays here: it is a drizzle column concern, and the Redis stores decode
|
|
11
|
+
* their own JSON payloads through `redis-values.ts` instead.
|
|
12
|
+
*/
|
|
25
13
|
function decodeJsonColumn(value, fallback) {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
}
|
|
33
|
-
return value ?? fallback;
|
|
14
|
+
if (typeof value === "string") try {
|
|
15
|
+
return JSON.parse(value);
|
|
16
|
+
} catch {
|
|
17
|
+
return fallback;
|
|
18
|
+
}
|
|
19
|
+
return value ?? fallback;
|
|
34
20
|
}
|
|
35
|
-
|
|
36
|
-
|
|
21
|
+
//#endregion
|
|
22
|
+
//#region src/api-token-store.ts
|
|
23
|
+
/**
|
|
24
|
+
* Database-backed API token store built on the Guren ORM.
|
|
25
|
+
*
|
|
26
|
+
* Pass the Drizzle table for your `api_tokens` schema. Column property
|
|
27
|
+
* names must match the {@link ApiToken} fields (`id`, `name`,
|
|
28
|
+
* `hashedToken`, `userId`, `abilities`, `lastUsedAt`, `expiresAt`,
|
|
29
|
+
* `createdAt`). The ORM must be configured (the standard
|
|
30
|
+
* `DatabaseProvider` setup) before the store is used.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```ts
|
|
34
|
+
* import { DatabaseApiTokenStore, createApiToken } from '@guren/core'
|
|
35
|
+
* import { apiTokens } from '@/db/schema'
|
|
36
|
+
*
|
|
37
|
+
* const store = new DatabaseApiTokenStore(apiTokens)
|
|
38
|
+
*
|
|
39
|
+
* const { plainTextToken } = await createApiToken(store, {
|
|
40
|
+
* name: 'Mobile App Token',
|
|
41
|
+
* userId: user.id,
|
|
42
|
+
* })
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
37
45
|
var DatabaseApiTokenStore = class {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
// decodes to something other than a list of strings degrades the same
|
|
90
|
-
// way: `tokenCan` would otherwise run `String.prototype.includes` on it,
|
|
91
|
-
// so a stored `'"*"'` would grant every ability.
|
|
92
|
-
abilities: Array.isArray(abilities) ? abilities.filter((ability) => typeof ability === "string") : [],
|
|
93
|
-
lastUsedAt: toDate(record.lastUsedAt),
|
|
94
|
-
// Null means "never expires"; a present-but-unparseable value means a
|
|
95
|
-
// malformed row, which must not read as immortal (`verifyApiToken`
|
|
96
|
-
// skips its expiry check entirely when this is null).
|
|
97
|
-
expiresAt: toOptionalExpiry(record.expiresAt),
|
|
98
|
-
createdAt: toDate(record.createdAt) ?? /* @__PURE__ */ new Date(0)
|
|
99
|
-
};
|
|
100
|
-
}
|
|
46
|
+
model;
|
|
47
|
+
abilitiesMode;
|
|
48
|
+
constructor(table, options = {}) {
|
|
49
|
+
this.abilitiesMode = options.abilitiesMode ?? "json";
|
|
50
|
+
this.model = class ApiTokenModel extends Model$1 {
|
|
51
|
+
static table = table;
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
async store(token) {
|
|
55
|
+
await this.model.forceCreate({
|
|
56
|
+
...token,
|
|
57
|
+
abilities: this.abilitiesMode === "text" ? JSON.stringify(token.abilities) : token.abilities
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
async findByHashedToken(hashedToken) {
|
|
61
|
+
const record = await this.model.where({ hashedToken }).first();
|
|
62
|
+
return record ? this.deserialize(record) : null;
|
|
63
|
+
}
|
|
64
|
+
async findByUserId(userId) {
|
|
65
|
+
return (await this.model.where({ userId }).get()).map((record) => this.deserialize(record));
|
|
66
|
+
}
|
|
67
|
+
async delete(id) {
|
|
68
|
+
await this.model.where({ id }).delete();
|
|
69
|
+
}
|
|
70
|
+
async deleteForUser(userId) {
|
|
71
|
+
await this.model.where({ userId }).delete();
|
|
72
|
+
}
|
|
73
|
+
async updateLastUsed(id, timestamp) {
|
|
74
|
+
await this.model.forceUpdate({ id }, { lastUsedAt: timestamp });
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Delete tokens whose expiration time has passed. Expired tokens are
|
|
78
|
+
* already rejected by `verifyApiToken`; call this from a scheduled
|
|
79
|
+
* job to keep the table small.
|
|
80
|
+
*/
|
|
81
|
+
async deleteExpired(now = /* @__PURE__ */ new Date()) {
|
|
82
|
+
await this.model.where("expiresAt", "<", now).delete();
|
|
83
|
+
}
|
|
84
|
+
deserialize(record) {
|
|
85
|
+
const abilities = decodeJsonColumn(record.abilities, []);
|
|
86
|
+
return {
|
|
87
|
+
id: String(record.id),
|
|
88
|
+
name: String(record.name),
|
|
89
|
+
hashedToken: String(record.hashedToken),
|
|
90
|
+
userId: typeof record.userId === "bigint" ? record.userId.toString() : record.userId,
|
|
91
|
+
abilities: Array.isArray(abilities) ? abilities.filter((ability) => typeof ability === "string") : [],
|
|
92
|
+
lastUsedAt: toDate(record.lastUsedAt),
|
|
93
|
+
expiresAt: toOptionalExpiry(record.expiresAt),
|
|
94
|
+
createdAt: toDate(record.createdAt) ?? /* @__PURE__ */ new Date(0)
|
|
95
|
+
};
|
|
96
|
+
}
|
|
101
97
|
};
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
98
|
+
//#endregion
|
|
99
|
+
//#region src/session-store.ts
|
|
100
|
+
/**
|
|
101
|
+
* Database-backed session store built on the Guren ORM — works on any
|
|
102
|
+
* configured connection (SQLite, Postgres, MySQL, or Cloudflare D1), which
|
|
103
|
+
* makes it the serverless default: no Redis required on Lambda, Vercel, or
|
|
104
|
+
* Workers, and reads are strongly consistent (login → redirect → read works).
|
|
105
|
+
*
|
|
106
|
+
* Pass the Drizzle table for your `sessions` schema. Column property names
|
|
107
|
+
* must match `id` (text primary key), `data`, and `expiresAt`:
|
|
108
|
+
*
|
|
109
|
+
* ```ts
|
|
110
|
+
* export const sessions = sqliteTable('sessions', {
|
|
111
|
+
* id: text('id').primaryKey(),
|
|
112
|
+
* data: text('data', { mode: 'json' }).$type<Record<string, unknown>>().notNull(),
|
|
113
|
+
* expiresAt: integer('expires_at', { mode: 'timestamp_ms' }).notNull(),
|
|
114
|
+
* })
|
|
115
|
+
* ```
|
|
116
|
+
*
|
|
117
|
+
* Session values must be JSON-serializable: Dates come back as ISO strings,
|
|
118
|
+
* `undefined` properties are dropped, and `bigint` values fail to serialize —
|
|
119
|
+
* unlike `MemorySessionStore`, which keeps live references. Store primitives
|
|
120
|
+
* and plain objects/arrays in sessions.
|
|
121
|
+
*
|
|
122
|
+
* @example
|
|
123
|
+
* ```ts
|
|
124
|
+
* import { DatabaseSessionStore } from '@guren/core'
|
|
125
|
+
* import { sessions } from '@/db/schema'
|
|
126
|
+
*
|
|
127
|
+
* app.use(createSessionMiddleware({ store: new DatabaseSessionStore(sessions) }))
|
|
128
|
+
* ```
|
|
129
|
+
*/
|
|
105
130
|
var DatabaseSessionStore = class {
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
131
|
+
model;
|
|
132
|
+
dataMode;
|
|
133
|
+
constructor(table, options = {}) {
|
|
134
|
+
this.dataMode = options.dataMode ?? "json";
|
|
135
|
+
this.model = class SessionModel extends Model$1 {
|
|
136
|
+
static table = table;
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
async read(id) {
|
|
140
|
+
const record = await this.model.where({ id }).first();
|
|
141
|
+
if (!record) return;
|
|
142
|
+
if (isExpired(record.expiresAt)) {
|
|
143
|
+
await this.model.where({
|
|
144
|
+
id,
|
|
145
|
+
expiresAt: record.expiresAt
|
|
146
|
+
}).delete();
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
return this.deserialize(record);
|
|
150
|
+
}
|
|
151
|
+
async write(id, data, ttlSeconds) {
|
|
152
|
+
const payload = {
|
|
153
|
+
data: this.dataMode === "text" ? JSON.stringify(data) : data,
|
|
154
|
+
expiresAt: this.expiryDate(ttlSeconds)
|
|
155
|
+
};
|
|
156
|
+
if (await this.model.where({ id }).first()) {
|
|
157
|
+
await this.model.forceUpdate({ id }, payload);
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
try {
|
|
161
|
+
await this.model.forceCreate({
|
|
162
|
+
id,
|
|
163
|
+
...payload
|
|
164
|
+
});
|
|
165
|
+
} catch (error) {
|
|
166
|
+
if (!await this.model.where({ id }).first()) throw error;
|
|
167
|
+
await this.model.forceUpdate({ id }, payload);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
async destroy(id) {
|
|
171
|
+
await this.model.where({ id }).delete();
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Refresh an existing session's TTL with a single conditional UPDATE — no
|
|
175
|
+
* data rewrite, no existence check. Missing rows and rows that have
|
|
176
|
+
* already expired (but not yet been swept) are left untouched, never
|
|
177
|
+
* resurrected — matching the `SessionStore.touch` contract and the memory
|
|
178
|
+
* store's behavior.
|
|
179
|
+
*/
|
|
180
|
+
async touch(id, ttlSeconds) {
|
|
181
|
+
await this.model.where({ id }).where("expiresAt", ">", /* @__PURE__ */ new Date()).forceUpdate({ expiresAt: this.expiryDate(ttlSeconds) });
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Delete sessions whose expiration time has passed. Expired rows are
|
|
185
|
+
* already treated as missing (and removed) by `read`; call this from a
|
|
186
|
+
* scheduled job to keep the table small.
|
|
187
|
+
*/
|
|
188
|
+
async deleteExpired(now = /* @__PURE__ */ new Date()) {
|
|
189
|
+
await this.model.where("expiresAt", "<=", now).delete();
|
|
190
|
+
}
|
|
191
|
+
deserialize(record) {
|
|
192
|
+
return decodeJsonColumn(record.data, {});
|
|
193
|
+
}
|
|
194
|
+
expiryDate(ttlSeconds) {
|
|
195
|
+
return new Date(Date.now() + ttlSeconds * 1e3);
|
|
196
|
+
}
|
|
172
197
|
};
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
198
|
+
//#endregion
|
|
199
|
+
//#region src/oauth-state-store.ts
|
|
200
|
+
/**
|
|
201
|
+
* Database-backed OAuth state store built on the Guren ORM.
|
|
202
|
+
*
|
|
203
|
+
* The default `MemoryOAuthStateStore` keeps state in per-isolate memory,
|
|
204
|
+
* which breaks on serverless: the OAuth authorize redirect and the callback
|
|
205
|
+
* that follows it can land on different instances, so an in-memory map
|
|
206
|
+
* never sees the state that was just written. Backing the store with the
|
|
207
|
+
* database makes state visible to whichever instance handles the callback.
|
|
208
|
+
*
|
|
209
|
+
* Pass the Drizzle table for your `oauth_states` schema. Column property
|
|
210
|
+
* names must match `stateHash` (text primary key), `provider`,
|
|
211
|
+
* `redirectTo`, `expiresAt`, and `binding`:
|
|
212
|
+
*
|
|
213
|
+
* ```ts
|
|
214
|
+
* export const oauthStates = sqliteTable('oauth_states', {
|
|
215
|
+
* stateHash: text('state_hash').primaryKey(),
|
|
216
|
+
* provider: text('provider').notNull(),
|
|
217
|
+
* redirectTo: text('redirect_to'),
|
|
218
|
+
* expiresAt: integer('expires_at', { mode: 'timestamp_ms' }).notNull(),
|
|
219
|
+
* binding: text('binding'),
|
|
220
|
+
* })
|
|
221
|
+
* ```
|
|
222
|
+
*
|
|
223
|
+
* `binding` is required for `authorize({ bindTo })` to survive the round trip
|
|
224
|
+
* to the provider. Without the column the store cannot persist it, and every
|
|
225
|
+
* bound state comes back unbound.
|
|
226
|
+
*
|
|
227
|
+
* @example
|
|
228
|
+
* ```ts
|
|
229
|
+
* import { createOAuthManager, DatabaseOAuthStateStore } from '@guren/core'
|
|
230
|
+
* import { oauthStates } from '@/db/schema'
|
|
231
|
+
*
|
|
232
|
+
* export const oauth = createOAuthManager({
|
|
233
|
+
* stateStore: new DatabaseOAuthStateStore(oauthStates),
|
|
234
|
+
* })
|
|
235
|
+
* ```
|
|
236
|
+
*/
|
|
176
237
|
var DatabaseOAuthStateStore = class {
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
238
|
+
model;
|
|
239
|
+
constructor(table) {
|
|
240
|
+
this.model = class OAuthStateModel extends Model$1 {
|
|
241
|
+
static table = table;
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
async store(stateHash, payload) {
|
|
245
|
+
await this.model.forceCreate({
|
|
246
|
+
stateHash,
|
|
247
|
+
provider: payload.provider,
|
|
248
|
+
redirectTo: payload.redirectTo ?? null,
|
|
249
|
+
expiresAt: payload.expiresAt,
|
|
250
|
+
binding: payload.binding ?? null
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
async find(stateHash) {
|
|
254
|
+
const record = await this.fetchLive(stateHash);
|
|
255
|
+
return record ? mapRecordToPayload(record) : null;
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* Fetch the row for `stateHash`, clearing it out and returning null if
|
|
259
|
+
* expired. Shared by `find` and `consume`, which otherwise diverge only
|
|
260
|
+
* in what they do with a live row.
|
|
261
|
+
*/
|
|
262
|
+
async fetchLive(stateHash) {
|
|
263
|
+
const record = await this.model.where({ stateHash }).first();
|
|
264
|
+
if (!record) return null;
|
|
265
|
+
if (isExpired(record.expiresAt)) {
|
|
266
|
+
await this.model.where({
|
|
267
|
+
stateHash,
|
|
268
|
+
expiresAt: record.expiresAt
|
|
269
|
+
}).delete();
|
|
270
|
+
return null;
|
|
271
|
+
}
|
|
272
|
+
return record;
|
|
273
|
+
}
|
|
274
|
+
async delete(stateHash) {
|
|
275
|
+
await this.model.where({ stateHash }).delete();
|
|
276
|
+
}
|
|
277
|
+
/**
|
|
278
|
+
* Atomically fetch and delete a state, guaranteeing exactly one concurrent
|
|
279
|
+
* caller for the same hash receives the payload.
|
|
280
|
+
*
|
|
281
|
+
* This guarantee depends on the configured ORM adapter's `delete()`
|
|
282
|
+
* reporting whether a row actually matched — either the deleted row
|
|
283
|
+
* (RETURNING) or an affected-row count. `DrizzleAdapter`, the framework's
|
|
284
|
+
* default and only shipped adapter, always does this: RETURNING drivers
|
|
285
|
+
* (Postgres, SQLite, D1) resolve to the deleted row or `undefined` only
|
|
286
|
+
* when zero rows matched; MySQL resolves to a result carrying
|
|
287
|
+
* `affectedRows` (verified by inspection, not a dedicated test).
|
|
288
|
+
*
|
|
289
|
+
* The `ORMAdapter.delete` contract also permits a bare `void` return.
|
|
290
|
+
* A custom adapter (via `Model.useAdapter()`) that returns `void`
|
|
291
|
+
* unconditionally — on both a successful delete and a no-op — gives
|
|
292
|
+
* `consume()` no way to tell which caller won: the value is identical for
|
|
293
|
+
* both, so no post-hoc check can attribute the deletion. Rather than
|
|
294
|
+
* silently rejecting every real login for such adapters,
|
|
295
|
+
* `deleteRemovedRow` treats an unrecognized non-null result as removed —
|
|
296
|
+
* this reopens the pre-consume find-then-delete race window (multiple
|
|
297
|
+
* concurrent callers can pass) for adapters that can't report a match,
|
|
298
|
+
* but does not break login. It is fail-closed (returns null / rejects)
|
|
299
|
+
* only for `null`/`undefined`, DrizzleAdapter's definitive "no rows
|
|
300
|
+
* matched" signal.
|
|
301
|
+
*/
|
|
302
|
+
async consume(stateHash) {
|
|
303
|
+
const record = await this.fetchLive(stateHash);
|
|
304
|
+
if (!record) return null;
|
|
305
|
+
return deleteRemovedRow(await this.model.where({
|
|
306
|
+
stateHash,
|
|
307
|
+
expiresAt: record.expiresAt
|
|
308
|
+
}).delete()) ? mapRecordToPayload(record) : null;
|
|
309
|
+
}
|
|
310
|
+
/**
|
|
311
|
+
* Delete OAuth states whose expiration time has passed. Expired states
|
|
312
|
+
* are already rejected by `find`; call this from a scheduled job to keep
|
|
313
|
+
* the table small.
|
|
314
|
+
*/
|
|
315
|
+
async deleteExpired(now = /* @__PURE__ */ new Date()) {
|
|
316
|
+
await this.model.where("expiresAt", "<=", now).delete();
|
|
317
|
+
}
|
|
257
318
|
};
|
|
319
|
+
/**
|
|
320
|
+
* Build the public payload from a live (non-expired) row. Shared by `find`
|
|
321
|
+
* and `consume` so the column-to-payload mapping only lives in one place.
|
|
322
|
+
*/
|
|
258
323
|
function mapRecordToPayload(record) {
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
};
|
|
324
|
+
return {
|
|
325
|
+
provider: String(record.provider),
|
|
326
|
+
redirectTo: record.redirectTo == null ? void 0 : String(record.redirectTo),
|
|
327
|
+
expiresAt: toDate(record.expiresAt),
|
|
328
|
+
binding: record.binding == null ? void 0 : String(record.binding)
|
|
329
|
+
};
|
|
266
330
|
}
|
|
331
|
+
/**
|
|
332
|
+
* Interpret an adapter delete result as "this call removed the row". See
|
|
333
|
+
* the `consume()` JSDoc above for the full adapter-support tradeoff: this
|
|
334
|
+
* is exact for DrizzleAdapter, and best-effort (assume removed) for any
|
|
335
|
+
* other result shape, since a bare `void` return carries no information to
|
|
336
|
+
* confirm or refute a match.
|
|
337
|
+
*/
|
|
267
338
|
function deleteRemovedRow(result) {
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
339
|
+
if (result == null) return false;
|
|
340
|
+
if (typeof result === "number") return result > 0;
|
|
341
|
+
if (Array.isArray(result)) return result.length > 0 && deleteRemovedRow(result[0]);
|
|
342
|
+
if (typeof result === "object") {
|
|
343
|
+
const record = result;
|
|
344
|
+
for (const key of [
|
|
345
|
+
"affectedRows",
|
|
346
|
+
"rowsAffected",
|
|
347
|
+
"rowCount",
|
|
348
|
+
"changes"
|
|
349
|
+
]) if (typeof record[key] === "number") return record[key] > 0;
|
|
350
|
+
return true;
|
|
351
|
+
}
|
|
352
|
+
return Boolean(result);
|
|
281
353
|
}
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
DatabaseOAuthStateStore,
|
|
285
|
-
DatabaseSessionStore,
|
|
286
|
-
DrizzleAdapter,
|
|
287
|
-
MassAssignmentException,
|
|
288
|
-
Model4 as Model,
|
|
289
|
-
ModelNotFoundException,
|
|
290
|
-
QueryBuilder,
|
|
291
|
-
SoftDeletes,
|
|
292
|
-
createAwsDataApiDatabase,
|
|
293
|
-
createD1Database,
|
|
294
|
-
createMySqlDatabase,
|
|
295
|
-
createPostgresDatabase,
|
|
296
|
-
createSqliteDatabase,
|
|
297
|
-
defineModel,
|
|
298
|
-
defineSeeder,
|
|
299
|
-
runSeeders
|
|
300
|
-
};
|
|
354
|
+
//#endregion
|
|
355
|
+
export { DatabaseApiTokenStore, DatabaseOAuthStateStore, DatabaseSessionStore, DrizzleAdapter, MassAssignmentException, Model, ModelNotFoundException, QueryBuilder, SoftDeletes, createAwsDataApiDatabase, createD1Database, createMySqlDatabase, createPostgresDatabase, createSqliteDatabase, defineModel, defineSeeder, runSeeders };
|