@shivaedev/platform 0.29.0 → 0.31.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.
- package/CHANGELOG.md +23 -0
- package/README.md +108 -17
- package/dist/server/appHostHttp.d.ts.map +1 -1
- package/dist/server/appHostHttp.js +6 -2
- package/dist/server/appHostHttp.js.map +1 -1
- package/dist/server/better-auth/bearerSessions.d.ts +9 -0
- package/dist/server/better-auth/bearerSessions.d.ts.map +1 -0
- package/dist/server/better-auth/bearerSessions.js +38 -0
- package/dist/server/better-auth/bearerSessions.js.map +1 -0
- package/dist/server/better-auth/mount.d.ts +3 -2
- package/dist/server/better-auth/mount.d.ts.map +1 -1
- package/dist/server/better-auth/mount.js +11 -17
- package/dist/server/better-auth/mount.js.map +1 -1
- package/dist/server/better-auth/mountRoutes.d.ts +20 -0
- package/dist/server/better-auth/mountRoutes.d.ts.map +1 -0
- package/dist/server/better-auth/mountRoutes.js +43 -0
- package/dist/server/better-auth/mountRoutes.js.map +1 -0
- package/dist/server/commandChanges.d.ts +1 -0
- package/dist/server/commandChanges.d.ts.map +1 -1
- package/dist/server/commandChanges.js +7 -2
- package/dist/server/commandChanges.js.map +1 -1
- package/dist/server/hostChanges.d.ts.map +1 -1
- package/dist/server/hostChanges.js +2 -3
- package/dist/server/hostChanges.js.map +1 -1
- package/dist/server/rpc-server/roles.d.ts +4 -1
- package/dist/server/rpc-server/roles.d.ts.map +1 -1
- package/dist/server/rpc-server/roles.js +17 -2
- package/dist/server/rpc-server/roles.js.map +1 -1
- package/dist/server/rpc-server/sensitive.js +1 -1
- package/dist/server/rpc-server/sensitive.js.map +1 -1
- package/dist/server/rpc-server/session.d.ts.map +1 -1
- package/dist/server/rpc-server/session.js +6 -5
- package/dist/server/rpc-server/session.js.map +1 -1
- package/dist/server/rpc-server/sessionKeys.d.ts +10 -0
- package/dist/server/rpc-server/sessionKeys.d.ts.map +1 -0
- package/dist/server/rpc-server/sessionKeys.js +22 -0
- package/dist/server/rpc-server/sessionKeys.js.map +1 -0
- package/dist/server/rpc-server/tracing.d.ts.map +1 -1
- package/dist/server/rpc-server/tracing.js +4 -2
- package/dist/server/rpc-server/tracing.js.map +1 -1
- package/package.json +6 -6
- package/src/server/appHostHttp.ts +10 -5
- package/src/server/better-auth/bearerSessions.ts +50 -0
- package/src/server/better-auth/mount.ts +12 -25
- package/src/server/better-auth/mountRoutes.ts +62 -0
- package/src/server/commandChanges.ts +8 -2
- package/src/server/hostChanges.ts +7 -3
- package/src/server/rpc-server/roles.ts +23 -2
- package/src/server/rpc-server/sensitive.ts +1 -1
- package/src/server/rpc-server/session.ts +7 -6
- package/src/server/rpc-server/sessionKeys.ts +25 -0
- package/src/server/rpc-server/tracing.ts +4 -2
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { Effect, Option } from "effect";
|
|
2
|
+
import { HttpRouter, HttpServerRequest, HttpServerResponse } from "effect/http";
|
|
3
|
+
import { type BearerSessionsOptions, cookieSessionResponse, hasSessionCookie, sessionHeaders } from "#server/better-auth/bearerSessions.ts";
|
|
4
|
+
import { trustedProxyPeer } from "#server/rpc-server/local.ts";
|
|
5
|
+
import { redact } from "#server/rpc-server/redact.ts";
|
|
6
|
+
|
|
7
|
+
interface MountedAuth {
|
|
8
|
+
readonly $context: Promise<{ readonly authCookies: { readonly sessionToken: { readonly name: string } } }>;
|
|
9
|
+
readonly handler: (request: Request) => Promise<Response>;
|
|
10
|
+
}
|
|
11
|
+
interface MountRouteOptions {
|
|
12
|
+
readonly bearerSessions?: BearerSessionsOptions | undefined;
|
|
13
|
+
readonly clientIpHeader: string;
|
|
14
|
+
readonly trustedProxy?: string | undefined;
|
|
15
|
+
}
|
|
16
|
+
function replacesSession(request: Request, response: Response, native: boolean) {
|
|
17
|
+
return (
|
|
18
|
+
native
|
|
19
|
+
&& response.ok
|
|
20
|
+
&& response.headers.has("set-auth-token")
|
|
21
|
+
&& request.headers.has("authorization")
|
|
22
|
+
&& new URL(request.url).pathname.startsWith("/api/auth/sign-in/")
|
|
23
|
+
);
|
|
24
|
+
}
|
|
25
|
+
async function authResponse(auth: MountedAuth, web: Request, options: MountRouteOptions) {
|
|
26
|
+
if (options.bearerSessions === undefined) {
|
|
27
|
+
return auth.handler(web);
|
|
28
|
+
}
|
|
29
|
+
const { authCookies } = await auth.$context;
|
|
30
|
+
const cookieName = authCookies.sessionToken.name;
|
|
31
|
+
const headers = sessionHeaders(web.headers, cookieName);
|
|
32
|
+
const native = options.bearerSessions.origins.includes(web.headers.get("origin") ?? "") && !hasSessionCookie(web.headers, cookieName);
|
|
33
|
+
const request = new Request(web, { headers });
|
|
34
|
+
const response = await auth.handler(request);
|
|
35
|
+
if (replacesSession(request, response, native)) {
|
|
36
|
+
const revoked = await auth.handler(new Request(new URL("/api/auth/sign-out", web.url), { body: "{}", headers, method: "POST" }));
|
|
37
|
+
await revoked.body?.cancel();
|
|
38
|
+
if (!revoked.ok) {
|
|
39
|
+
throw new Error("Session replacement failed");
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return native ? response : cookieSessionResponse(response);
|
|
43
|
+
}
|
|
44
|
+
export function mountRoutes(auth: MountedAuth, options: MountRouteOptions) {
|
|
45
|
+
const isTrustedProxy = trustedProxyPeer(options.trustedProxy);
|
|
46
|
+
return HttpRouter.use((router) =>
|
|
47
|
+
router.add("*", "/api/auth/*", (request) =>
|
|
48
|
+
Effect.gen(function* () {
|
|
49
|
+
const web = yield* HttpServerRequest.toWeb(request);
|
|
50
|
+
if (!Option.exists(request.remoteAddress, isTrustedProxy)) {
|
|
51
|
+
web.headers.delete("x-forwarded-for");
|
|
52
|
+
web.headers.delete(options.clientIpHeader);
|
|
53
|
+
}
|
|
54
|
+
return HttpServerResponse.fromWeb(
|
|
55
|
+
yield* Effect.tryPromise(() => authResponse(auth, web, options)).pipe(
|
|
56
|
+
Effect.tapError((error) => Effect.logError("Authentication provider failed", redact(error.cause))),
|
|
57
|
+
),
|
|
58
|
+
);
|
|
59
|
+
}).pipe(Effect.catch(() => Effect.succeed(HttpServerResponse.text("Authentication is temporarily unavailable", { status: 503 })))),
|
|
60
|
+
),
|
|
61
|
+
);
|
|
62
|
+
}
|
|
@@ -3,8 +3,13 @@ import * as Reactivity from "effect/reactivity/Reactivity";
|
|
|
3
3
|
import type { Rpc } from "effect/rpc";
|
|
4
4
|
import { SqlClient } from "effect/sql";
|
|
5
5
|
import { invalidationKeys } from "@shivaedev/effect-contract/keys.ts";
|
|
6
|
-
import { CommandInvalidation } from "@shivaedev/effect-contract/serverMetadata.ts";
|
|
6
|
+
import { CommandInvalidation, KeyScope } from "@shivaedev/effect-contract/serverMetadata.ts";
|
|
7
7
|
import { ChangeObserver, invalidateOnCommit, transact } from "@shivaedev/effect-sql/transact.ts";
|
|
8
|
+
import { sessionKeyContext } from "#server/rpc-server/sessionKeys.ts";
|
|
9
|
+
|
|
10
|
+
export function sessionCommandChanges<A, E, R>(effect: Effect.Effect<A, E, R>, rpc: Rpc.AnyWithProps, payload: unknown) {
|
|
11
|
+
return Option.isSome(Context.getOption(rpc.annotations, KeyScope)) ? commandChanges(effect, rpc, payload) : effect;
|
|
12
|
+
}
|
|
8
13
|
|
|
9
14
|
export function commandChanges<A, E, R>(effect: Effect.Effect<A, E, R>, rpc: Rpc.AnyWithProps, payload: unknown) {
|
|
10
15
|
const invalidates = Context.getOption(rpc.annotations, CommandInvalidation);
|
|
@@ -13,6 +18,7 @@ export function commandChanges<A, E, R>(effect: Effect.Effect<A, E, R>, rpc: Rpc
|
|
|
13
18
|
}
|
|
14
19
|
const declared = invalidates.value;
|
|
15
20
|
return Effect.gen(function* () {
|
|
21
|
+
const context = yield* sessionKeyContext(rpc);
|
|
16
22
|
const sql = yield* Effect.serviceOption(SqlClient.SqlClient);
|
|
17
23
|
const reactivity = yield* Effect.serviceOption(Reactivity.Reactivity);
|
|
18
24
|
if (Option.isNone(reactivity)) {
|
|
@@ -30,7 +36,7 @@ export function commandChanges<A, E, R>(effect: Effect.Effect<A, E, R>, rpc: Rpc
|
|
|
30
36
|
return observer(event);
|
|
31
37
|
}),
|
|
32
38
|
);
|
|
33
|
-
const keys = invalidationKeys(declared(payload, result)).filter((key) => !published.has(key));
|
|
39
|
+
const keys = invalidationKeys(declared(payload, result), context).filter((key) => !published.has(key));
|
|
34
40
|
if (keys.length > 0) {
|
|
35
41
|
const announce = Option.isNone(sql)
|
|
36
42
|
? reactivity.value.invalidate(keys)
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Context, Effect, Layer, Option, Stream } from "effect";
|
|
2
2
|
import type { Rpc, RpcGroup } from "effect/rpc";
|
|
3
|
-
import {
|
|
3
|
+
import type { QueryReference } from "@shivaedev/effect-contract/serverMetadata.ts";
|
|
4
|
+
import { resolveChanges } from "#server/rpc-server/sessionKeys.ts";
|
|
4
5
|
import type { AppChanges } from "./appHost.ts";
|
|
5
6
|
|
|
6
7
|
export function hostChanges<R extends Rpc.Any>(group: RpcGroup.RpcGroup<R>, changesService: typeof AppChanges) {
|
|
@@ -18,9 +19,12 @@ export function hostChanges<R extends Rpc.Any>(group: RpcGroup.RpcGroup<R>, chan
|
|
|
18
19
|
handlers.set(rpc.key, provided);
|
|
19
20
|
continue;
|
|
20
21
|
}
|
|
21
|
-
const reads = Context.getUnsafe(rpc.annotations, ChangesReads);
|
|
22
22
|
function handler({ queries }: { readonly queries: readonly QueryReference[] }) {
|
|
23
|
-
return
|
|
23
|
+
return Stream.unwrap(
|
|
24
|
+
Effect.map(resolveChanges(rpc, queries), ({ keys, context }) =>
|
|
25
|
+
Option.isSome(changes) ? changes.value.stream(keys, context) : Stream.never,
|
|
26
|
+
),
|
|
27
|
+
);
|
|
24
28
|
}
|
|
25
29
|
handlers.set(rpc.key, { context: services, handler, tag: rpc._tag });
|
|
26
30
|
}
|
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
import { Effect, Layer, Option } from "effect";
|
|
2
|
+
import { type HttpServerRequest, HttpServerResponse } from "effect/http";
|
|
2
3
|
import type { RpcMiddleware } from "effect/rpc";
|
|
3
4
|
import { type AuthUnavailable, Forbidden, type Unauthorized } from "#errors/taxonomy.ts";
|
|
4
5
|
import { hasRole } from "#rpc/hasRole.ts";
|
|
5
6
|
import { Identity, SessionUser, type SessionUserValue } from "#rpc/identity.ts";
|
|
6
7
|
import { AuthenticatedAdmin, AuthenticatedOwner } from "#rpc/middleware.ts";
|
|
8
|
+
import { requireAdmin } from "#rpc/requireAdmin.ts";
|
|
9
|
+
import { sessionCommandChanges } from "#server/commandChanges.ts";
|
|
7
10
|
import { revalidateStream } from "./revalidateStream.ts";
|
|
8
11
|
import { resolveSession, type SessionPolicy, type UserSessionShape, withSessionUser } from "./session.ts";
|
|
9
12
|
|
|
@@ -15,11 +18,11 @@ function requiringRole<TSession extends UserSessionShape>(
|
|
|
15
18
|
function permitted(user: SessionUserValue) {
|
|
16
19
|
return hasRole(user, role) ? Effect.void : Effect.fail(new Forbidden({ message }));
|
|
17
20
|
}
|
|
18
|
-
return (effect, { headers, rpc }) => {
|
|
21
|
+
return (effect, { headers, rpc, payload }) => {
|
|
19
22
|
const lookup = Effect.tap(resolveSession(policy, headers, rpc._tag), (session) =>
|
|
20
23
|
Option.isSome(session) ? permitted(session.value.user) : Effect.void,
|
|
21
24
|
);
|
|
22
|
-
return effect.pipe(
|
|
25
|
+
return sessionCommandChanges(effect, rpc, payload).pipe(
|
|
23
26
|
(invocation) => Effect.flatMap(Identity, ({ id }) => invocation.pipe(revalidateStream(policy, rpc, id, lookup))),
|
|
24
27
|
(invocation) => Effect.andThen(Effect.flatMap(SessionUser, permitted), invocation),
|
|
25
28
|
withSessionUser(policy, headers, rpc._tag),
|
|
@@ -29,6 +32,24 @@ function requiringRole<TSession extends UserSessionShape>(
|
|
|
29
32
|
export function authenticatedAdminLayer<TSession extends UserSessionShape>(policy: SessionPolicy<TSession>): Layer.Layer<AuthenticatedAdmin> {
|
|
30
33
|
return Layer.succeed(AuthenticatedAdmin, requiringRole(policy, "admin", "Administrator role required"));
|
|
31
34
|
}
|
|
35
|
+
function adminRejection(error: Unauthorized | Forbidden | AuthUnavailable) {
|
|
36
|
+
return HttpServerResponse.empty({
|
|
37
|
+
headers: { "cache-control": "private, no-store" },
|
|
38
|
+
status: { AuthUnavailable: 503, Forbidden: 403, Unauthorized: 401 }[error._tag],
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function adminRoute<TSession extends UserSessionShape>(policy: SessionPolicy<TSession>) {
|
|
43
|
+
return <E, R>(handler: (request: HttpServerRequest.HttpServerRequest) => Effect.Effect<HttpServerResponse.HttpServerResponse, E, R>) =>
|
|
44
|
+
(request: HttpServerRequest.HttpServerRequest): Effect.Effect<HttpServerResponse.HttpServerResponse, E, Exclude<R, Identity | SessionUser>> =>
|
|
45
|
+
Effect.andThen(requireAdmin, Effect.context<Identity | SessionUser>()).pipe(
|
|
46
|
+
withSessionUser(policy, request.headers, request.url),
|
|
47
|
+
Effect.matchEffect({
|
|
48
|
+
onFailure: (error) => Effect.succeed(adminRejection(error)),
|
|
49
|
+
onSuccess: (context) => Effect.provideContext(handler(request), context),
|
|
50
|
+
}),
|
|
51
|
+
);
|
|
52
|
+
}
|
|
32
53
|
export function authenticatedOwnerLayer<TSession extends UserSessionShape>(policy: SessionPolicy<TSession>): Layer.Layer<AuthenticatedOwner> {
|
|
33
54
|
return Layer.succeed(AuthenticatedOwner, requiringRole(policy, "owner", "Owner role required"));
|
|
34
55
|
}
|
|
@@ -38,7 +38,7 @@ export function isSensitiveKey(key: string): boolean {
|
|
|
38
38
|
|| wordsOf(key).some((word) => SENSITIVE_WORDS.includes(word))
|
|
39
39
|
);
|
|
40
40
|
}
|
|
41
|
-
const AUTH_SCHEME = /\b(?<scheme>Bearer|Basic)\s+[A-Za-z0-9
|
|
41
|
+
const AUTH_SCHEME = /\b(?<scheme>Bearer|Basic)\s+[A-Za-z0-9%._~+/-]+=*/giu;
|
|
42
42
|
const JWT = /\beyJ[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]*/gu;
|
|
43
43
|
const PRIVATE_KEY = /-----BEGIN (?<kind>(?:[A-Z0-9]+ )*PRIVATE KEY)-----[\s\S]*?-----END \k<kind>-----/gu;
|
|
44
44
|
const URL_PASSWORD = /(?<authority>\/\/[^:/\s@]+:)[^@/\s]+@/gu;
|
|
@@ -10,6 +10,7 @@ import {
|
|
|
10
10
|
type SessionUserWithProviderAccountsValue,
|
|
11
11
|
} from "#rpc/identity.ts";
|
|
12
12
|
import { Authenticated, AuthenticatedUser, AuthenticatedUserWithProviderAccounts, MaybeAuthenticated } from "#rpc/middleware.ts";
|
|
13
|
+
import { sessionCommandChanges } from "#server/commandChanges.ts";
|
|
13
14
|
import { type OriginPolicy, originRequest } from "./origin.ts";
|
|
14
15
|
import { revalidateStream } from "./revalidateStream.ts";
|
|
15
16
|
import { transportHeaders } from "./transportHeaders.ts";
|
|
@@ -43,14 +44,14 @@ function identified(id: string) {
|
|
|
43
44
|
return Effect.andThen(Effect.annotateCurrentSpan("user.id", id), Effect.succeed(id));
|
|
44
45
|
}
|
|
45
46
|
export function authenticatedLayer<TSession extends SessionShape>(policy: SessionPolicy<TSession>): Layer.Layer<Authenticated> {
|
|
46
|
-
return Layer.succeed(Authenticated, (effect, { headers, rpc }) =>
|
|
47
|
+
return Layer.succeed(Authenticated, (effect, { headers, rpc, payload }) =>
|
|
47
48
|
Effect.gen(function* () {
|
|
48
49
|
const session = yield* resolveSession(policy, headers, rpc._tag);
|
|
49
50
|
if (Option.isNone(session)) {
|
|
50
51
|
return yield* new Unauthorized({ message: "Authentication required" });
|
|
51
52
|
}
|
|
52
53
|
const id = yield* identified(session.value.user.id);
|
|
53
|
-
return yield* effect.pipe(
|
|
54
|
+
return yield* sessionCommandChanges(effect, rpc, payload).pipe(
|
|
54
55
|
revalidateStream(policy, rpc, id, resolveSession(policy, headers, rpc._tag)),
|
|
55
56
|
Effect.provideService(Identity, { id }),
|
|
56
57
|
Effect.annotateLogs({ userId: id }),
|
|
@@ -99,8 +100,8 @@ export function withSessionUser<TSession extends UserSessionShape>(policy: Sessi
|
|
|
99
100
|
});
|
|
100
101
|
}
|
|
101
102
|
export function authenticatedUserLayer<TSession extends UserSessionShape>(policy: SessionPolicy<TSession>): Layer.Layer<AuthenticatedUser> {
|
|
102
|
-
return Layer.succeed(AuthenticatedUser, (effect, { headers, rpc }) =>
|
|
103
|
-
effect.pipe(
|
|
103
|
+
return Layer.succeed(AuthenticatedUser, (effect, { headers, rpc, payload }) =>
|
|
104
|
+
sessionCommandChanges(effect, rpc, payload).pipe(
|
|
104
105
|
(invocation) =>
|
|
105
106
|
Effect.flatMap(Identity, ({ id }) => invocation.pipe(revalidateStream(policy, rpc, id, resolveSession(policy, headers, rpc._tag)))),
|
|
106
107
|
withSessionUser(policy, headers, rpc._tag),
|
|
@@ -114,7 +115,7 @@ export interface ProviderAccountsSessionShape extends UserSessionShape {
|
|
|
114
115
|
export function authenticatedUserWithProviderAccountsLayer<TSession extends ProviderAccountsSessionShape>(
|
|
115
116
|
policy: SessionPolicy<TSession>,
|
|
116
117
|
): Layer.Layer<AuthenticatedUserWithProviderAccounts> {
|
|
117
|
-
return Layer.succeed(AuthenticatedUserWithProviderAccounts, (effect, { headers, rpc }) =>
|
|
118
|
+
return Layer.succeed(AuthenticatedUserWithProviderAccounts, (effect, { headers, rpc, payload }) =>
|
|
118
119
|
Effect.gen(function* () {
|
|
119
120
|
const session = yield* resolveSession(policy, headers, rpc._tag);
|
|
120
121
|
if (Option.isNone(session)) {
|
|
@@ -127,7 +128,7 @@ export function authenticatedUserWithProviderAccountsLayer<TSession extends Prov
|
|
|
127
128
|
);
|
|
128
129
|
const user = { email, id, name, ...(role === undefined ? {} : { role }) };
|
|
129
130
|
yield* identified(id);
|
|
130
|
-
return yield* effect.pipe(
|
|
131
|
+
return yield* sessionCommandChanges(effect, rpc, payload).pipe(
|
|
131
132
|
revalidateStream(policy, rpc, id, resolveSession(policy, headers, rpc._tag)),
|
|
132
133
|
Effect.provideService(Identity, { id }),
|
|
133
134
|
Effect.provideService(SessionUser, user),
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { Context, Effect, Option } from "effect";
|
|
2
|
+
import type { Rpc } from "effect/rpc";
|
|
3
|
+
import type { KeyContext } from "@shivaedev/effect-contract/keys.ts";
|
|
4
|
+
import { ChangesReads, KeyScope, type QueryReference } from "@shivaedev/effect-contract/serverMetadata.ts";
|
|
5
|
+
import { Identity } from "#rpc/identity.ts";
|
|
6
|
+
|
|
7
|
+
export function sessionKeyContext(rpc: Pick<Rpc.AnyWithProps, "annotations">): Effect.Effect<KeyContext | undefined> {
|
|
8
|
+
if (Option.isNone(Context.getOption(rpc.annotations, KeyScope))) {
|
|
9
|
+
return Effect.succeed(undefined);
|
|
10
|
+
}
|
|
11
|
+
return Effect.gen(function* () {
|
|
12
|
+
const identity = yield* Effect.serviceOption(Identity);
|
|
13
|
+
if (Option.isNone(identity)) {
|
|
14
|
+
return yield* Effect.die(new Error("Session-scoped contract keys require request-local Identity"));
|
|
15
|
+
}
|
|
16
|
+
return { scope: identity.value.id };
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function resolveChanges(rpc: Pick<Rpc.AnyWithProps, "annotations">, references: readonly QueryReference[]) {
|
|
21
|
+
return Effect.map(sessionKeyContext(rpc), (context) => ({
|
|
22
|
+
context,
|
|
23
|
+
keys: Context.getUnsafe(rpc.annotations, ChangesReads)(references),
|
|
24
|
+
}));
|
|
25
|
+
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { Cause, Effect, Exit, Layer, Option, Predicate, Random } from "effect";
|
|
1
|
+
import { Cause, Context, Effect, Exit, Layer, Option, Predicate, Random } from "effect";
|
|
2
2
|
import { Headers } from "effect/http";
|
|
3
|
+
import { KeyScope } from "@shivaedev/effect-contract/serverMetadata.ts";
|
|
3
4
|
import { RequestId } from "#rpc/identity.ts";
|
|
4
5
|
import { RequestTracing } from "#rpc/middleware.ts";
|
|
5
6
|
import { commandChanges } from "#server/commandChanges.ts";
|
|
@@ -54,7 +55,8 @@ export function requestTracingLayer(options: RequestTracingOptions = {}): Layer.
|
|
|
54
55
|
Effect.gen(function* () {
|
|
55
56
|
const requestId = yield* requestIdOf(headers, options.header ?? "x-request-id");
|
|
56
57
|
yield* Effect.annotateCurrentSpan({ "request.id": requestId, "rpc.method": rpc._tag });
|
|
57
|
-
|
|
58
|
+
const invocation = Option.isSome(Context.getOption(rpc.annotations, KeyScope)) ? effect : commandChanges(effect, rpc, payload);
|
|
59
|
+
return yield* invocation.pipe(
|
|
58
60
|
Effect.catchCauseIf(Cause.hasDies, (cause) => Effect.failCause(redactCause(cause, options.sensitive))),
|
|
59
61
|
Effect.onExit((exit) => (Exit.isFailure(exit) ? logFailure(exit.cause, payload, options.sensitive) : Effect.void)),
|
|
60
62
|
Effect.provideService(RequestId, requestId),
|