@smplcty/auth 0.5.0 → 0.5.2
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/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/with-service-context.d.ts +24 -0
- package/dist/with-service-context.d.ts.map +1 -0
- package/dist/with-service-context.js +54 -0
- package/dist/with-service-context.js.map +1 -0
- package/package.json +3 -2
package/dist/index.d.ts
CHANGED
|
@@ -8,5 +8,6 @@ export { getUserRoleNames } from './get-user-role-names.js';
|
|
|
8
8
|
export { withSession } from './with-session.js';
|
|
9
9
|
export { withTransaction } from './with-transaction.js';
|
|
10
10
|
export { setSessionId, setRoleName, setTenantIds, setAllTenants, setSessionContext, SESSION_VAR_NAMES, } from './set-helpers.js';
|
|
11
|
+
export { withServiceContext } from './with-service-context.js';
|
|
11
12
|
export { verifyDevOtp, isDevOtpEnrolled, generateDevOtpSecret, getDevOtpEnrollmentUri, } from './dev-otp.js';
|
|
12
13
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EACL,SAAS,EACT,oBAAoB,EACpB,mBAAmB,EACnB,oBAAoB,EACpB,iBAAiB,GAClB,MAAM,aAAa,CAAC;AAGrB,YAAY,EACV,SAAS,EACT,OAAO,EACP,cAAc,EACd,WAAW,EACX,kBAAkB,EAClB,WAAW,EACX,yBAAyB,EACzB,aAAa,EACb,MAAM,EACN,kBAAkB,GACnB,MAAM,YAAY,CAAC;AAGpB,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACpD,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AACxD,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACpD,OA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kBAAkB,EAAE,MAAM,2BAA2B,CAAC;AAG/D,OAAO,EACL,YAAY,EACZ,gBAAgB,EAChB,oBAAoB,EACpB,sBAAsB,GACvB,MAAM,cAAc,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -11,6 +11,8 @@ export { withSession } from './with-session.js';
|
|
|
11
11
|
export { withTransaction } from './with-transaction.js';
|
|
12
12
|
// Low-level helpers — must be called inside an open transaction.
|
|
13
13
|
export { setSessionId, setRoleName, setTenantIds, setAllTenants, setSessionContext, SESSION_VAR_NAMES, } from './set-helpers.js';
|
|
14
|
+
// Background service context — for Lambdas with no user session.
|
|
15
|
+
export { withServiceContext } from './with-service-context.js';
|
|
14
16
|
// Developer OTP — for devs whose phones can't receive Twilio SMS.
|
|
15
17
|
export { verifyDevOtp, isDevOtpEnrolled, generateDevOtpSecret, getDevOtpEnrollmentUri, } from './dev-otp.js';
|
|
16
18
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,SAAS;AACT,OAAO,EACL,SAAS,EACT,oBAAoB,EACpB,mBAAmB,EACnB,oBAAoB,EACpB,iBAAiB,GAClB,MAAM,aAAa,CAAC;AAgBrB,oBAAoB;AACpB,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACpD,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AACxD,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACpD,OAAO,EAAE,6BAA6B,EAAE,MAAM,wCAAwC,CAAC;AAEvF,OAAO,EAAE,gBAAgB,EAAE,MAAM,0BAA0B,CAAC;AAE5D,wBAAwB;AACxB,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AAExD,iEAAiE;AACjE,OAAO,EACL,YAAY,EACZ,WAAW,EACX,YAAY,EACZ,aAAa,EACb,iBAAiB,EACjB,iBAAiB,GAClB,MAAM,kBAAkB,CAAC;AAE1B,kEAAkE;AAClE,OAAO,EACL,YAAY,EACZ,gBAAgB,EAChB,oBAAoB,EACpB,sBAAsB,GACvB,MAAM,cAAc,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,SAAS;AACT,OAAO,EACL,SAAS,EACT,oBAAoB,EACpB,mBAAmB,EACnB,oBAAoB,EACpB,iBAAiB,GAClB,MAAM,aAAa,CAAC;AAgBrB,oBAAoB;AACpB,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACpD,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AACxD,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACpD,OAAO,EAAE,6BAA6B,EAAE,MAAM,wCAAwC,CAAC;AAEvF,OAAO,EAAE,gBAAgB,EAAE,MAAM,0BAA0B,CAAC;AAE5D,wBAAwB;AACxB,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AAExD,iEAAiE;AACjE,OAAO,EACL,YAAY,EACZ,WAAW,EACX,YAAY,EACZ,aAAa,EACb,iBAAiB,EACjB,iBAAiB,GAClB,MAAM,kBAAkB,CAAC;AAE1B,iEAAiE;AACjE,OAAO,EAAE,kBAAkB,EAAE,MAAM,2BAA2B,CAAC;AAE/D,kEAAkE;AAClE,OAAO,EACL,YAAY,EACZ,gBAAgB,EAChB,oBAAoB,EACpB,sBAAsB,GACvB,MAAM,cAAc,CAAC"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { Pool, PoolClient } from 'pg';
|
|
2
|
+
/**
|
|
3
|
+
* Check out a client from the pool, set service-level GUCs
|
|
4
|
+
* (role=settings, all_tenants=true, session_id=serviceName), run the
|
|
5
|
+
* callback, and release the client.
|
|
6
|
+
*
|
|
7
|
+
* Use this for background Lambdas that need to bypass RLS without a
|
|
8
|
+
* real user session. The GUCs are set session-scoped (not
|
|
9
|
+
* transaction-local) so they persist across all queries on the held
|
|
10
|
+
* client without requiring a wrapping transaction.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* const pool = await getPool();
|
|
15
|
+
* return withServiceContext(pool, 'handle-hierarchy-refresh', async (client) => {
|
|
16
|
+
* const { rows } = await client.query('SELECT * FROM hierarchy_change_log');
|
|
17
|
+
* // ... business logic
|
|
18
|
+
* });
|
|
19
|
+
* ```
|
|
20
|
+
*
|
|
21
|
+
* @throws {InvalidInputError} If `serviceName` is empty.
|
|
22
|
+
*/
|
|
23
|
+
export declare function withServiceContext<T>(pool: Pool, serviceName: string, fn: (client: PoolClient) => Promise<T>): Promise<T>;
|
|
24
|
+
//# sourceMappingURL=with-service-context.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"with-service-context.d.ts","sourceRoot":"","sources":["../src/with-service-context.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,UAAU,EAAE,MAAM,IAAI,CAAC;AAW3C;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAsB,kBAAkB,CAAC,CAAC,EACxC,IAAI,EAAE,IAAI,EACV,WAAW,EAAE,MAAM,EACnB,EAAE,EAAE,CAAC,MAAM,EAAE,UAAU,KAAK,OAAO,CAAC,CAAC,CAAC,GACrC,OAAO,CAAC,CAAC,CAAC,CAuBZ"}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { InvalidInputError } from './errors.js';
|
|
2
|
+
import { SESSION_VAR_NAMES } from './set-helpers.js';
|
|
3
|
+
/**
|
|
4
|
+
* Session-scoped `set_config` — persists for the lifetime of the
|
|
5
|
+
* connection, not just the current transaction. Used by background
|
|
6
|
+
* jobs that run multiple statements outside a transaction.
|
|
7
|
+
*/
|
|
8
|
+
const SET_CONFIG_SESSION = 'SELECT set_config($1, $2, false)';
|
|
9
|
+
/**
|
|
10
|
+
* Check out a client from the pool, set service-level GUCs
|
|
11
|
+
* (role=settings, all_tenants=true, session_id=serviceName), run the
|
|
12
|
+
* callback, and release the client.
|
|
13
|
+
*
|
|
14
|
+
* Use this for background Lambdas that need to bypass RLS without a
|
|
15
|
+
* real user session. The GUCs are set session-scoped (not
|
|
16
|
+
* transaction-local) so they persist across all queries on the held
|
|
17
|
+
* client without requiring a wrapping transaction.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```ts
|
|
21
|
+
* const pool = await getPool();
|
|
22
|
+
* return withServiceContext(pool, 'handle-hierarchy-refresh', async (client) => {
|
|
23
|
+
* const { rows } = await client.query('SELECT * FROM hierarchy_change_log');
|
|
24
|
+
* // ... business logic
|
|
25
|
+
* });
|
|
26
|
+
* ```
|
|
27
|
+
*
|
|
28
|
+
* @throws {InvalidInputError} If `serviceName` is empty.
|
|
29
|
+
*/
|
|
30
|
+
export async function withServiceContext(pool, serviceName, fn) {
|
|
31
|
+
if (typeof serviceName !== 'string' || serviceName.length === 0) {
|
|
32
|
+
throw new InvalidInputError('serviceName must be a non-empty string');
|
|
33
|
+
}
|
|
34
|
+
const client = await pool.connect();
|
|
35
|
+
try {
|
|
36
|
+
await client.query(SET_CONFIG_SESSION, [
|
|
37
|
+
SESSION_VAR_NAMES.sessionId,
|
|
38
|
+
serviceName,
|
|
39
|
+
]);
|
|
40
|
+
await client.query(SET_CONFIG_SESSION, [
|
|
41
|
+
SESSION_VAR_NAMES.roleName,
|
|
42
|
+
'settings',
|
|
43
|
+
]);
|
|
44
|
+
await client.query(SET_CONFIG_SESSION, [
|
|
45
|
+
SESSION_VAR_NAMES.allTenants,
|
|
46
|
+
'true',
|
|
47
|
+
]);
|
|
48
|
+
return await fn(client);
|
|
49
|
+
}
|
|
50
|
+
finally {
|
|
51
|
+
client.release();
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
//# sourceMappingURL=with-service-context.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"with-service-context.js","sourceRoot":"","sources":["../src/with-service-context.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAChD,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AAErD;;;;GAIG;AACH,MAAM,kBAAkB,GAAG,kCAAkC,CAAC;AAE9D;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,CAAC,KAAK,UAAU,kBAAkB,CACtC,IAAU,EACV,WAAmB,EACnB,EAAsC;IAEtC,IAAI,OAAO,WAAW,KAAK,QAAQ,IAAI,WAAW,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QAChE,MAAM,IAAI,iBAAiB,CAAC,wCAAwC,CAAC,CAAC;IACxE,CAAC;IAED,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,OAAO,EAAE,CAAC;IACpC,IAAI,CAAC;QACH,MAAM,MAAM,CAAC,KAAK,CAAC,kBAAkB,EAAE;YACrC,iBAAiB,CAAC,SAAS;YAC3B,WAAW;SACZ,CAAC,CAAC;QACH,MAAM,MAAM,CAAC,KAAK,CAAC,kBAAkB,EAAE;YACrC,iBAAiB,CAAC,QAAQ;YAC1B,UAAU;SACX,CAAC,CAAC;QACH,MAAM,MAAM,CAAC,KAAK,CAAC,kBAAkB,EAAE;YACrC,iBAAiB,CAAC,UAAU;YAC5B,MAAM;SACP,CAAC,CAAC;QACH,OAAO,MAAM,EAAE,CAAC,MAAM,CAAC,CAAC;IAC1B,CAAC;YAAS,CAAC;QACT,MAAM,CAAC,OAAO,EAAE,CAAC;IACnB,CAAC;AACH,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@smplcty/auth",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.2",
|
|
4
4
|
"description": "Robust, type-safe session and role-based authentication primitives for PostgreSQL apps using Row-Level Security.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -51,7 +51,8 @@
|
|
|
51
51
|
"lint": "eslint src tests",
|
|
52
52
|
"typecheck": "tsc --noEmit",
|
|
53
53
|
"test": "vitest run",
|
|
54
|
-
"test:watch": "vitest"
|
|
54
|
+
"test:watch": "vitest",
|
|
55
|
+
"release": "git push origin main --follow-tags && gh release create v$(node -p \"require('./package.json').version\") --generate-notes"
|
|
55
56
|
},
|
|
56
57
|
"peerDependencies": {
|
|
57
58
|
"pg": ">=8.0.0"
|