cloudflare-next-intl 0.9.4 → 0.9.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.
- package/dist/src/db/context.js +20 -38
- package/package.json +1 -1
package/dist/src/db/context.js
CHANGED
|
@@ -126,10 +126,6 @@ function injectUidComment(sql, userId) {
|
|
|
126
126
|
}
|
|
127
127
|
return sql;
|
|
128
128
|
}
|
|
129
|
-
function isSelectOnly(sql) {
|
|
130
|
-
const text = typeof sql === 'string' ? sql : sql?.text;
|
|
131
|
-
return typeof text === 'string' && /^(select|with)\b/i.test(text.trimStart());
|
|
132
|
-
}
|
|
133
129
|
export async function withUserDb(fn, uid, dbOverride) {
|
|
134
130
|
const config = await resolveDbConfig(dbOverride);
|
|
135
131
|
const db = config.db;
|
|
@@ -144,17 +140,29 @@ export async function withUserDb(fn, uid, dbOverride) {
|
|
|
144
140
|
return await withDbClient(config, async (client) => {
|
|
145
141
|
const rawClient = client;
|
|
146
142
|
const setSessionState = async () => {
|
|
147
|
-
await rawClient.query(`select set_config('request.jwt.claims', $1,
|
|
148
|
-
await rawClient.query(`set role "${role.replace(/"/g, '""')}"`);
|
|
143
|
+
await rawClient.query(`select set_config('request.jwt.claims', $1, true)`, [JSON.stringify({ sub: userId })]);
|
|
144
|
+
await rawClient.query(`set local role "${role.replace(/"/g, '""')}"`);
|
|
149
145
|
};
|
|
150
146
|
let inTransaction = false;
|
|
151
|
-
let sessionStateSet = false;
|
|
152
147
|
let gate = Promise.resolve();
|
|
153
148
|
const serialize = (op) => {
|
|
154
149
|
const run = gate.then(op, op);
|
|
155
150
|
gate = run.catch(() => undefined);
|
|
156
151
|
return run;
|
|
157
152
|
};
|
|
153
|
+
const beginWithIdentity = async () => {
|
|
154
|
+
if (inTransaction)
|
|
155
|
+
return;
|
|
156
|
+
await rawClient.query('begin');
|
|
157
|
+
try {
|
|
158
|
+
await setSessionState();
|
|
159
|
+
}
|
|
160
|
+
catch (error) {
|
|
161
|
+
await rawClient.query('rollback').catch(() => undefined);
|
|
162
|
+
throw error;
|
|
163
|
+
}
|
|
164
|
+
inTransaction = true;
|
|
165
|
+
};
|
|
158
166
|
const interceptingClient = new Proxy(client, {
|
|
159
167
|
get(target, prop) {
|
|
160
168
|
if (prop === 'query') {
|
|
@@ -164,16 +172,7 @@ export async function withUserDb(fn, uid, dbOverride) {
|
|
|
164
172
|
const isCommitOrRollback = /^(commit|rollback)\b/i.test(text.trimStart());
|
|
165
173
|
if (isBegin) {
|
|
166
174
|
return await serialize(async () => {
|
|
167
|
-
|
|
168
|
-
await rawClient.query('begin');
|
|
169
|
-
inTransaction = true;
|
|
170
|
-
await setSessionState();
|
|
171
|
-
sessionStateSet = true;
|
|
172
|
-
}
|
|
173
|
-
else if (!inTransaction) {
|
|
174
|
-
await rawClient.query('begin');
|
|
175
|
-
inTransaction = true;
|
|
176
|
-
}
|
|
175
|
+
await beginWithIdentity();
|
|
177
176
|
return { rows: [], rowCount: 0 };
|
|
178
177
|
});
|
|
179
178
|
}
|
|
@@ -187,27 +186,7 @@ export async function withUserDb(fn, uid, dbOverride) {
|
|
|
187
186
|
return { rows: [], rowCount: 0 };
|
|
188
187
|
});
|
|
189
188
|
}
|
|
190
|
-
await serialize(
|
|
191
|
-
if (!sessionStateSet) {
|
|
192
|
-
if (isSelectOnly(sql)) {
|
|
193
|
-
await rawClient.query('begin');
|
|
194
|
-
inTransaction = true;
|
|
195
|
-
await setSessionState();
|
|
196
|
-
await rawClient.query('commit');
|
|
197
|
-
inTransaction = false;
|
|
198
|
-
}
|
|
199
|
-
else {
|
|
200
|
-
await rawClient.query('begin');
|
|
201
|
-
inTransaction = true;
|
|
202
|
-
await setSessionState();
|
|
203
|
-
}
|
|
204
|
-
sessionStateSet = true;
|
|
205
|
-
}
|
|
206
|
-
else if (!inTransaction && !isSelectOnly(sql)) {
|
|
207
|
-
await rawClient.query('begin');
|
|
208
|
-
inTransaction = true;
|
|
209
|
-
}
|
|
210
|
-
});
|
|
189
|
+
await serialize(beginWithIdentity);
|
|
211
190
|
const targetClient = target;
|
|
212
191
|
return targetClient.query(injectUidComment(sql, userId), ...args);
|
|
213
192
|
};
|
|
@@ -218,15 +197,18 @@ export async function withUserDb(fn, uid, dbOverride) {
|
|
|
218
197
|
});
|
|
219
198
|
const { drizzle } = await import('drizzle-orm/node-postgres');
|
|
220
199
|
const drizzleHandle = drizzle(interceptingClient);
|
|
200
|
+
const resetRole = () => rawClient.query('reset role').catch(() => undefined);
|
|
221
201
|
try {
|
|
222
202
|
const result = await fn(await postgresDb(drizzleHandle, interceptingClient));
|
|
223
203
|
if (inTransaction)
|
|
224
204
|
await rawClient.query('commit');
|
|
205
|
+
await resetRole();
|
|
225
206
|
return result;
|
|
226
207
|
}
|
|
227
208
|
catch (err) {
|
|
228
209
|
if (inTransaction)
|
|
229
210
|
await rawClient.query('rollback').catch(() => undefined);
|
|
211
|
+
await resetRole();
|
|
230
212
|
throw err;
|
|
231
213
|
}
|
|
232
214
|
});
|