@shivaedev/server 0.16.1 → 0.17.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 +24 -0
- package/README.md +31 -2
- package/dist/AppHostFactory.d.ts +3 -4
- package/dist/AppHostFactory.d.ts.map +1 -1
- package/dist/AppHostInput.d.ts +3 -4
- package/dist/AppHostInput.d.ts.map +1 -1
- package/dist/appHost.d.ts +5 -4
- package/dist/appHost.d.ts.map +1 -1
- package/dist/appHost.js.map +1 -1
- package/dist/appHostHttp.d.ts +58 -41
- package/dist/appHostHttp.d.ts.map +1 -1
- package/dist/appHostHttp.js +2 -1
- package/dist/appHostHttp.js.map +1 -1
- package/dist/bearerSocketRpc.d.ts +2 -2
- package/dist/bearerSocketRpc.d.ts.map +1 -1
- package/dist/bearerSocketRpc.js +5 -4
- package/dist/bearerSocketRpc.js.map +1 -1
- package/dist/hostChanges.d.ts +1 -2
- package/dist/hostChanges.d.ts.map +1 -1
- package/dist/hostChanges.js +14 -9
- package/dist/hostChanges.js.map +1 -1
- package/dist/hostOptions.d.ts +3 -3
- package/dist/hostOptions.d.ts.map +1 -1
- package/dist/hostRpcRoutes.d.ts +15 -7
- package/dist/hostRpcRoutes.d.ts.map +1 -1
- package/dist/hostRpcRoutes.js +6 -3
- package/dist/hostRpcRoutes.js.map +1 -1
- package/dist/otlpHttpClient.d.ts.map +1 -1
- package/dist/otlpHttpClient.js +12 -8
- package/dist/otlpHttpClient.js.map +1 -1
- package/dist/socketRpc.d.ts +5 -2
- package/dist/socketRpc.d.ts.map +1 -1
- package/dist/socketRpc.js +4 -3
- package/dist/socketRpc.js.map +1 -1
- package/dist/trimPostgresPageCache.d.ts +10 -0
- package/dist/trimPostgresPageCache.d.ts.map +1 -0
- package/dist/trimPostgresPageCache.js +25 -0
- package/dist/trimPostgresPageCache.js.map +1 -0
- package/package.json +8 -8
- package/src/AppHostFactory.ts +0 -7
- package/src/AppHostInput.ts +0 -5
- package/src/appHost.ts +3 -7
- package/src/appHostHttp.ts +4 -8
- package/src/bearerSocketRpc.ts +6 -6
- package/src/hostChanges.ts +15 -21
- package/src/hostOptions.ts +3 -3
- package/src/hostRpcRoutes.ts +36 -16
- package/src/otlpHttpClient.ts +17 -14
- package/src/socketRpc.ts +19 -6
- package/src/trimPostgresPageCache.ts +31 -0
package/src/AppHostInput.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import type { Effect, Layer } from "effect";
|
|
2
2
|
import type { Rpc } from "effect/rpc";
|
|
3
|
-
import type { SessionShape } from "@shivaedev/identity/server/session.ts";
|
|
4
3
|
import type { AppDatabaseOptions, AppHostOptions, AppHttpHostOptions, AppLocalHostOptions } from "#appHost.ts";
|
|
5
4
|
import type { HostRouteRequirements } from "#hostOptions.ts";
|
|
6
5
|
|
|
@@ -17,7 +16,6 @@ export type AppHostInput<
|
|
|
17
16
|
TServiceError = never,
|
|
18
17
|
TServiceRequirements = never,
|
|
19
18
|
TMigrations = never,
|
|
20
|
-
TSession extends SessionShape = SessionShape,
|
|
21
19
|
TApplicationError = never,
|
|
22
20
|
TApplicationRequirements = never,
|
|
23
21
|
TPreflight = never,
|
|
@@ -38,7 +36,6 @@ export type AppHostInput<
|
|
|
38
36
|
TServices,
|
|
39
37
|
TServiceError,
|
|
40
38
|
TServiceRequirements,
|
|
41
|
-
TSession,
|
|
42
39
|
never,
|
|
43
40
|
never,
|
|
44
41
|
never,
|
|
@@ -58,7 +55,6 @@ export type AppHostInput<
|
|
|
58
55
|
TServiceError,
|
|
59
56
|
TServiceRequirements,
|
|
60
57
|
TMigrations,
|
|
61
|
-
TSession,
|
|
62
58
|
TPreflight,
|
|
63
59
|
TPreflightError,
|
|
64
60
|
TPreflightRequirements,
|
|
@@ -90,7 +86,6 @@ export type AppHostInput<
|
|
|
90
86
|
TServices,
|
|
91
87
|
TServiceError,
|
|
92
88
|
TServiceRequirements,
|
|
93
|
-
TSession,
|
|
94
89
|
TPreflight,
|
|
95
90
|
TPreflightError,
|
|
96
91
|
TPreflightRequirements,
|
package/src/appHost.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
|
|
2
2
|
import { Layer } from "effect";
|
|
3
3
|
import type { Rpc } from "effect/rpc";
|
|
4
|
-
import type { SessionPolicy
|
|
4
|
+
import type { SessionPolicy } from "@shivaedev/identity/server/SessionPolicy.ts";
|
|
5
5
|
import type { AppHostFactory } from "#AppHostFactory.ts";
|
|
6
6
|
import type { AppHostInput, HostPreflight } from "#AppHostInput.ts";
|
|
7
7
|
import { appDatabase, type DatabaseConfig } from "#appDatabase.ts";
|
|
@@ -23,7 +23,6 @@ export type AppHostOptions<
|
|
|
23
23
|
TServiceError = never,
|
|
24
24
|
TServiceRequirements = never,
|
|
25
25
|
TMigrations = never,
|
|
26
|
-
TSession extends SessionShape = SessionShape,
|
|
27
26
|
TPreflight = never,
|
|
28
27
|
TPreflightError = never,
|
|
29
28
|
TPreflightRequirements = never,
|
|
@@ -35,7 +34,7 @@ export type AppHostOptions<
|
|
|
35
34
|
HostRoutes<TRouteError, TRouteRequirements> & {
|
|
36
35
|
readonly migrations: AppDatabaseOptions<TMigrations, TMigrationError>["migrations"];
|
|
37
36
|
readonly policy?: undefined;
|
|
38
|
-
readonly sessions: SessionPolicy
|
|
37
|
+
readonly sessions: Layer.Layer<SessionPolicy>;
|
|
39
38
|
};
|
|
40
39
|
|
|
41
40
|
export type AppLocalHostOptions<
|
|
@@ -69,7 +68,6 @@ export type AppHttpHostOptions<
|
|
|
69
68
|
TServices = never,
|
|
70
69
|
TServiceError = never,
|
|
71
70
|
TServiceRequirements = never,
|
|
72
|
-
TSession extends SessionShape = SessionShape,
|
|
73
71
|
TPreflight = never,
|
|
74
72
|
TPreflightError = never,
|
|
75
73
|
TPreflightRequirements = never,
|
|
@@ -78,7 +76,7 @@ export type AppHttpHostOptions<
|
|
|
78
76
|
> = HostPreflight<TPreflight, TPreflightError, TPreflightRequirements> &
|
|
79
77
|
HostCoreOptions<TRpcs, THandlers, THandlerError, THandlerRequirements, TServices, TServiceError, TServiceRequirements> &
|
|
80
78
|
HostRoutes<TRouteError, TRouteRequirements> &
|
|
81
|
-
HostPolicy
|
|
79
|
+
HostPolicy & { readonly migrations?: undefined };
|
|
82
80
|
export const appHost: AppHostFactory = <
|
|
83
81
|
TRpcs extends Rpc.Any,
|
|
84
82
|
THandlers,
|
|
@@ -88,7 +86,6 @@ export const appHost: AppHostFactory = <
|
|
|
88
86
|
TServiceError = never,
|
|
89
87
|
TServiceRequirements = never,
|
|
90
88
|
TMigrations = never,
|
|
91
|
-
TSession extends SessionShape = SessionShape,
|
|
92
89
|
TApplicationError = never,
|
|
93
90
|
TApplicationRequirements = never,
|
|
94
91
|
TPreflight = never,
|
|
@@ -107,7 +104,6 @@ export const appHost: AppHostFactory = <
|
|
|
107
104
|
TServiceError,
|
|
108
105
|
TServiceRequirements,
|
|
109
106
|
TMigrations,
|
|
110
|
-
TSession,
|
|
111
107
|
TApplicationError,
|
|
112
108
|
TApplicationRequirements,
|
|
113
109
|
TPreflight,
|
package/src/appHostHttp.ts
CHANGED
|
@@ -3,7 +3,6 @@ import { Effect, Layer } from "effect";
|
|
|
3
3
|
import { HttpRouter, HttpServer } from "effect/http";
|
|
4
4
|
import type { Rpc } from "effect/rpc";
|
|
5
5
|
import { schemaSensitiveKeys } from "@shivaedev/errors/schemaSensitiveKeys.ts";
|
|
6
|
-
import type { SessionShape } from "@shivaedev/identity/server/session.ts";
|
|
7
6
|
import type { appDatabase } from "#appDatabase.ts";
|
|
8
7
|
import type { AppHttpHostOptions } from "#appHost.ts";
|
|
9
8
|
import { browserErrorRoute } from "#browserErrorRoute.ts";
|
|
@@ -28,7 +27,6 @@ export function appHostHttp<
|
|
|
28
27
|
TServices,
|
|
29
28
|
TServiceError,
|
|
30
29
|
TServiceRequirements,
|
|
31
|
-
TSession extends SessionShape,
|
|
32
30
|
TRouteError = never,
|
|
33
31
|
TRouteRequirements = HostRouteRequirements,
|
|
34
32
|
>(
|
|
@@ -43,7 +41,7 @@ export function appHostHttp<
|
|
|
43
41
|
TRouteError,
|
|
44
42
|
TRouteRequirements
|
|
45
43
|
> &
|
|
46
|
-
HostPolicy
|
|
44
|
+
HostPolicy,
|
|
47
45
|
) {
|
|
48
46
|
return Layer.unwrap(
|
|
49
47
|
Effect.map(schemaSensitiveKeys(options.group, options.config?.sensitiveKeys), (sensitive) =>
|
|
@@ -54,6 +52,7 @@ export function appHostHttp<
|
|
|
54
52
|
const routes = hostRpcRoutes(options, transport, sensitive);
|
|
55
53
|
const assets = options.assets === undefined ? Layer.empty : hostAssets(options.assets);
|
|
56
54
|
const application = Layer.mergeAll(options.services ?? Layer.empty);
|
|
55
|
+
const sessions = Layer.mergeAll(Layer.empty, ...(options.sessions === undefined ? [] : [options.sessions]));
|
|
57
56
|
const server = nodeHttpServer(host, options.config);
|
|
58
57
|
const applicationRoutes = hostApplicationRoutes(options.routes, bodySize);
|
|
59
58
|
const httpRoutes = Layer.mergeAll(routes, healthRoute, browserErrorRoute(trustedProxy), assets, applicationRoutes).pipe(
|
|
@@ -64,7 +63,7 @@ export function appHostHttp<
|
|
|
64
63
|
HttpRouter.toHttpEffect(httpRoutes).pipe(
|
|
65
64
|
Effect.map((handler) => HttpServer.serve(handler).pipe(HttpServer.withLogAddress, Layer.provideMerge(server))),
|
|
66
65
|
),
|
|
67
|
-
).pipe(Layer.provideMerge(application), Layer.provideMerge(NodeHttpServer.layerHttpServices));
|
|
66
|
+
).pipe(Layer.provideMerge(application), Layer.provide(sessions), Layer.provideMerge(NodeHttpServer.layerHttpServices));
|
|
68
67
|
}),
|
|
69
68
|
).pipe(Layer.provide(redactingDiagnostics(sensitive)), Layer.provide(otlpTracing), Layer.provide(Layer.succeed(RedactionPolicy, sensitive))),
|
|
70
69
|
),
|
|
@@ -80,7 +79,6 @@ export function migratedHost<
|
|
|
80
79
|
TServiceError,
|
|
81
80
|
TServiceRequirements,
|
|
82
81
|
TMigrations,
|
|
83
|
-
TSession extends SessionShape,
|
|
84
82
|
TRouteError = never,
|
|
85
83
|
TRouteRequirements = HostRouteRequirements,
|
|
86
84
|
TMigrationError = never,
|
|
@@ -96,7 +94,7 @@ export function migratedHost<
|
|
|
96
94
|
TRouteError,
|
|
97
95
|
TRouteRequirements
|
|
98
96
|
> &
|
|
99
|
-
HostPolicy
|
|
97
|
+
HostPolicy & {
|
|
100
98
|
readonly migrations: MigrationOptions<TMigrations, TMigrationError>;
|
|
101
99
|
},
|
|
102
100
|
database: ReturnType<typeof appDatabase<TMigrations, TMigrationError>>,
|
|
@@ -114,7 +112,6 @@ export function deferredHost<
|
|
|
114
112
|
TServiceError,
|
|
115
113
|
TServiceRequirements,
|
|
116
114
|
TMigrations,
|
|
117
|
-
TSession extends SessionShape,
|
|
118
115
|
TApplicationError,
|
|
119
116
|
TApplicationRequirements,
|
|
120
117
|
TRouteError = never,
|
|
@@ -130,7 +127,6 @@ export function deferredHost<
|
|
|
130
127
|
TServices,
|
|
131
128
|
TServiceError,
|
|
132
129
|
TServiceRequirements,
|
|
133
|
-
TSession,
|
|
134
130
|
never,
|
|
135
131
|
never,
|
|
136
132
|
never,
|
package/src/bearerSocketRpc.ts
CHANGED
|
@@ -4,12 +4,12 @@ import * as Socket from "effect/socket/Socket";
|
|
|
4
4
|
import { SocketAuthentication } from "@shivaedev/contract/socketAuthentication.ts";
|
|
5
5
|
import { Unauthorized } from "@shivaedev/errors/taxonomy.ts";
|
|
6
6
|
import { recheckSession } from "@shivaedev/identity/server/recheckSession.ts";
|
|
7
|
-
import {
|
|
7
|
+
import { SessionPolicy } from "@shivaedev/identity/server/SessionPolicy.ts";
|
|
8
|
+
import { resolveSession } from "@shivaedev/identity/server/session.ts";
|
|
8
9
|
import { makeSocketSession, SocketSession } from "@shivaedev/identity/server/socketSession.ts";
|
|
9
10
|
|
|
10
|
-
export const bearerSocketRpc = Effect.fnUntraced(function*
|
|
11
|
+
export const bearerSocketRpc = Effect.fnUntraced(function* (
|
|
11
12
|
handler: Effect.Effect<HttpServerResponse.HttpServerResponse, never, HttpServerRequest.HttpServerRequest | Scope.Scope>,
|
|
12
|
-
policy: SessionPolicy<TSession>,
|
|
13
13
|
) {
|
|
14
14
|
const request = yield* HttpServerRequest.HttpServerRequest;
|
|
15
15
|
const socket = yield* Effect.orDie(request.upgrade);
|
|
@@ -54,12 +54,12 @@ export const bearerSocketRpc = Effect.fnUntraced(function* <TSession extends Ses
|
|
|
54
54
|
get: (target, key, receiver) => (key === "upgrade" ? Effect.succeed(authenticatedSocket) : Reflect.get(target, key, receiver)),
|
|
55
55
|
});
|
|
56
56
|
return yield* Effect.gen(function* () {
|
|
57
|
-
const initial = yield* resolveSession(
|
|
57
|
+
const initial = yield* resolveSession(Headers.empty, "$connect");
|
|
58
58
|
if (Option.isNone(initial)) {
|
|
59
59
|
return yield* new Unauthorized({ message: "Authentication required" });
|
|
60
60
|
}
|
|
61
|
-
const session = yield* makeSocketSession(
|
|
62
|
-
const check = recheckSession(
|
|
61
|
+
const session = yield* makeSocketSession((yield* SessionPolicy).streamRevalidationInterval ?? "30 seconds");
|
|
62
|
+
const check = recheckSession(Headers.empty, "$connect").pipe(
|
|
63
63
|
Effect.flatMap((current) =>
|
|
64
64
|
Option.isSome(current) && current.value.user.id === initial.value.user.id
|
|
65
65
|
? Effect.void
|
package/src/hostChanges.ts
CHANGED
|
@@ -7,14 +7,15 @@ import type { QueryReference } from "@shivaedev/contract/serverMetadata.ts";
|
|
|
7
7
|
import { Unauthorized } from "@shivaedev/errors/taxonomy.ts";
|
|
8
8
|
import { recheckSession } from "@shivaedev/identity/server/recheckSession.ts";
|
|
9
9
|
import { revalidateStream } from "@shivaedev/identity/server/revalidateStream.ts";
|
|
10
|
-
import
|
|
10
|
+
import { SessionPolicy } from "@shivaedev/identity/server/SessionPolicy.ts";
|
|
11
11
|
import { resolveChanges } from "@shivaedev/identity/server/sessionKeys.ts";
|
|
12
12
|
import { rerunQuery } from "#rerunQuery.ts";
|
|
13
13
|
|
|
14
|
-
export function hostChanges<R extends Rpc.Any
|
|
14
|
+
export function hostChanges<R extends Rpc.Any>(group: RpcGroup.RpcGroup<R>) {
|
|
15
15
|
return Layer.effectContext(
|
|
16
16
|
Effect.gen(function* () {
|
|
17
17
|
const changes = yield* Effect.serviceOption(LiveServer);
|
|
18
|
+
const sessions = yield* Effect.serviceOption(SessionPolicy);
|
|
18
19
|
const services = yield* Effect.context<never>();
|
|
19
20
|
const handlers = new Map<string, unknown>();
|
|
20
21
|
function subscribe(rpc: Rpc.AnyWithProps, queries: readonly QueryReference[], options: Parameters<Rpc.Handler<string>["handler"]>[1]) {
|
|
@@ -22,15 +23,18 @@ export function hostChanges<R extends Rpc.Any, TSession extends SessionShape>(gr
|
|
|
22
23
|
Effect.gen(function* () {
|
|
23
24
|
const { subscription, context } = yield* resolveChanges(rpc, queries);
|
|
24
25
|
const viewer = yield* CurrentViewer;
|
|
25
|
-
const authorized =
|
|
26
|
+
const authorized = Option.isNone(sessions)
|
|
27
|
+
? subscription.authorized
|
|
28
|
+
: authorizeSession(rpc._tag, viewer, subscription.authorized).pipe(Effect.provideService(SessionPolicy, sessions.value));
|
|
26
29
|
const stream = Option.isSome(changes)
|
|
27
30
|
? Stream.unwrap(changes.value.subscribe({ ...subscription, authorized }, context, rerunQuery(group, options)))
|
|
28
31
|
: Stream.concat(Stream.succeed({ _tag: "Resync" } as const), Stream.never);
|
|
29
|
-
if (sessions
|
|
32
|
+
if (Option.isNone(sessions) || Option.isNone(viewer)) {
|
|
30
33
|
return stream;
|
|
31
34
|
}
|
|
32
35
|
const revalidation = Effect.never.pipe(
|
|
33
|
-
revalidateStream(
|
|
36
|
+
revalidateStream(rpc, viewer.value.id, recheckSession(Headers.empty, rpc._tag)),
|
|
37
|
+
Effect.provideService(SessionPolicy, sessions.value),
|
|
34
38
|
);
|
|
35
39
|
return Stream.interruptWhen(stream, revalidation);
|
|
36
40
|
}),
|
|
@@ -57,20 +61,10 @@ export function hostChanges<R extends Rpc.Any, TSession extends SessionShape>(gr
|
|
|
57
61
|
);
|
|
58
62
|
}
|
|
59
63
|
|
|
60
|
-
function
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
policy: Effect.Effect<boolean, E, R>,
|
|
65
|
-
) {
|
|
66
|
-
if (sessions === undefined) {
|
|
67
|
-
return policy;
|
|
64
|
+
const authorizeSession = Effect.fnUntraced(function* <E, R>(tag: string, viewer: Option.Option<Viewer>, policy: Effect.Effect<boolean, E, R>) {
|
|
65
|
+
const current = yield* recheckSession(Headers.empty, tag);
|
|
66
|
+
if (Option.isSome(viewer) && (Option.isNone(current) || current.value.user.id !== viewer.value.id)) {
|
|
67
|
+
return yield* new Unauthorized({ message: "Authentication required" });
|
|
68
68
|
}
|
|
69
|
-
return
|
|
70
|
-
|
|
71
|
-
Effect.flatMap(
|
|
72
|
-
(allowed): Effect.Effect<boolean, E | Unauthorized, R> =>
|
|
73
|
-
allowed ? policy : Effect.fail(new Unauthorized({ message: "Authentication required" })),
|
|
74
|
-
),
|
|
75
|
-
);
|
|
76
|
-
}
|
|
69
|
+
return yield* policy;
|
|
70
|
+
});
|
package/src/hostOptions.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { Duration, Layer } from "effect";
|
|
2
2
|
import type { HttpRouter } from "effect/http";
|
|
3
3
|
import type { Rpc, RpcGroup } from "effect/rpc";
|
|
4
|
-
import type { SessionPolicy
|
|
4
|
+
import type { SessionPolicy } from "@shivaedev/identity/server/SessionPolicy.ts";
|
|
5
5
|
import type { AssetOptions } from "#AssetOptions.ts";
|
|
6
6
|
import type { AppDatabaseOptions } from "#appHost.ts";
|
|
7
7
|
|
|
@@ -44,6 +44,6 @@ export type HostOptions<
|
|
|
44
44
|
> = HostCoreOptions<TRpcs, THandlers, THandlerError, THandlerRequirements, TServices, TServiceError, TServiceRequirements> &
|
|
45
45
|
HostRoutes<TRouteError, TRouteRequirements>;
|
|
46
46
|
|
|
47
|
-
export type HostPolicy
|
|
48
|
-
| { readonly policy?: undefined; readonly sessions: SessionPolicy
|
|
47
|
+
export type HostPolicy =
|
|
48
|
+
| { readonly policy?: undefined; readonly sessions: Layer.Layer<SessionPolicy> }
|
|
49
49
|
| { readonly policy: "loopback"; readonly sessions?: never };
|
package/src/hostRpcRoutes.ts
CHANGED
|
@@ -4,9 +4,9 @@ import * as HttpStatus from "effect/http/HttpStatus";
|
|
|
4
4
|
import { type Rpc, type RpcGroup, RpcSerialization } from "effect/rpc";
|
|
5
5
|
import { validateReadPolicies } from "@shivaedev/contract/validateReadPolicies.ts";
|
|
6
6
|
import type { schemaSensitiveKeys } from "@shivaedev/errors/schemaSensitiveKeys.ts";
|
|
7
|
-
import type { Authenticated, MaybeAuthenticated, RequestTracing } from "@shivaedev/identity/middleware.ts";
|
|
8
7
|
import { localRpcGuard } from "@shivaedev/identity/server/local.ts";
|
|
9
|
-
import {
|
|
8
|
+
import type { SessionPolicy } from "@shivaedev/identity/server/SessionPolicy.ts";
|
|
9
|
+
import { authenticatedLayer, maybeAuthenticatedLayer } from "@shivaedev/identity/server/session.ts";
|
|
10
10
|
import { boundedRpcRequest } from "#boundedRpcRequest.ts";
|
|
11
11
|
import { hostChanges } from "#hostChanges.ts";
|
|
12
12
|
import type { HostPolicy } from "#hostOptions.ts";
|
|
@@ -16,19 +16,44 @@ import { redactingRpcSerialization } from "#redactingRpcSerialization.ts";
|
|
|
16
16
|
import { rpcRequestTracing } from "#rpcRequestTracing.ts";
|
|
17
17
|
import { socketRpc } from "#socketRpc.ts";
|
|
18
18
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
19
|
+
type BuiltRoutes<TRpcs extends Rpc.Any, THandlers, TError, TRequirements> = ReturnType<
|
|
20
|
+
typeof buildRpcRoutes<TRpcs, THandlers, TError, TRequirements>
|
|
21
|
+
>;
|
|
22
|
+
type RouteOptions<TRpcs extends Rpc.Any, THandlers, TError, TRequirements> = {
|
|
23
|
+
readonly group: RpcGroup.RpcGroup<TRpcs>;
|
|
24
|
+
readonly handlers: Layer.Layer<THandlers, TError, TRequirements>;
|
|
25
|
+
readonly rpcPath?: HttpRouter.PathInput;
|
|
26
|
+
} & HostPolicy;
|
|
27
|
+
|
|
28
|
+
export function hostRpcRoutes<TRpcs extends Rpc.Any, THandlers, TError, TRequirements>(
|
|
29
|
+
options: RouteOptions<TRpcs, THandlers, TError, TRequirements> & { readonly policy: "loopback" },
|
|
30
|
+
transport: Effect.Success<ReturnType<typeof hostTransport>>,
|
|
31
|
+
sensitive: Effect.Success<ReturnType<typeof schemaSensitiveKeys>>,
|
|
32
|
+
): Layer.Layer<
|
|
33
|
+
Layer.Success<BuiltRoutes<TRpcs, THandlers, TError, TRequirements>>,
|
|
34
|
+
Layer.Error<BuiltRoutes<TRpcs, THandlers, TError, TRequirements>>,
|
|
35
|
+
| TRequirements
|
|
36
|
+
| Exclude<Rpc.ServicesServer<TRpcs>, THandlers>
|
|
37
|
+
| Exclude<Layer.Services<BuiltRoutes<TRpcs, THandlers, TError, TRequirements>>, SessionPolicy | HttpRouter.Request<"Requires", SessionPolicy>>
|
|
38
|
+
>;
|
|
39
|
+
export function hostRpcRoutes<TRpcs extends Rpc.Any, THandlers, TError, TRequirements>(
|
|
40
|
+
...args: Parameters<typeof buildRpcRoutes<TRpcs, THandlers, TError, TRequirements>>
|
|
41
|
+
): BuiltRoutes<TRpcs, THandlers, TError, TRequirements>;
|
|
42
|
+
export function hostRpcRoutes<TRpcs extends Rpc.Any, THandlers, TError, TRequirements>(
|
|
43
|
+
...args: Parameters<typeof buildRpcRoutes<TRpcs, THandlers, TError, TRequirements>>
|
|
44
|
+
) {
|
|
45
|
+
return buildRpcRoutes(...args);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function buildRpcRoutes<TRpcs extends Rpc.Any, THandlers, TError, TRequirements>(
|
|
49
|
+
options: RouteOptions<TRpcs, THandlers, TError, TRequirements>,
|
|
25
50
|
{ bodySize, trustedProxy, nativeOrigins }: Effect.Success<ReturnType<typeof hostTransport>>,
|
|
26
51
|
sensitive: Effect.Success<ReturnType<typeof schemaSensitiveKeys>>,
|
|
27
52
|
) {
|
|
28
|
-
const middleware
|
|
53
|
+
const middleware =
|
|
29
54
|
options.policy === "loopback"
|
|
30
55
|
? rpcRequestTracing(sensitive)
|
|
31
|
-
: Layer.mergeAll(authenticatedLayer(
|
|
56
|
+
: Layer.mergeAll(authenticatedLayer(), maybeAuthenticatedLayer(), rpcRequestTracing(sensitive));
|
|
32
57
|
return HttpRouter.use((router) =>
|
|
33
58
|
Effect.gen(function* () {
|
|
34
59
|
yield* validateReadPolicies(options.group);
|
|
@@ -58,12 +83,7 @@ export function hostRpcRoutes<TRpcs extends Rpc.Any, THandlers, TError, TRequire
|
|
|
58
83
|
);
|
|
59
84
|
}),
|
|
60
85
|
).pipe(
|
|
61
|
-
Layer.provide(
|
|
62
|
-
Layer.merge(
|
|
63
|
-
middleware,
|
|
64
|
-
hostChanges(options.group, options.policy === "loopback" ? undefined : options.sessions).pipe(Layer.provideMerge(options.handlers)),
|
|
65
|
-
),
|
|
66
|
-
),
|
|
86
|
+
Layer.provide(Layer.merge(middleware, hostChanges(options.group).pipe(Layer.provideMerge(options.handlers)))),
|
|
67
87
|
Layer.provide(RpcSerialization.layerNdjson),
|
|
68
88
|
);
|
|
69
89
|
}
|
package/src/otlpHttpClient.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Effect, Layer } from "effect";
|
|
2
|
-
import { FetchHttpClient, HttpClient, HttpClientError, type HttpClientRequest } from "effect/http";
|
|
3
|
-
import * as HttpStatus from "effect/http/HttpStatus";
|
|
2
|
+
import { FetchHttpClient, HttpClient, HttpClientError, type HttpClientRequest, type HttpClientResponse } from "effect/http";
|
|
4
3
|
import { redactText } from "@shivaedev/errors/sensitive.ts";
|
|
4
|
+
import { filterStatus } from "@shivaedev/http/filterStatus.ts";
|
|
5
5
|
import { RedactionPolicy } from "#RedactionPolicy.ts";
|
|
6
6
|
|
|
7
7
|
function timeoutError(request: HttpClientRequest.HttpClientRequest) {
|
|
@@ -26,6 +26,20 @@ export function otlpHttpClient(timeout = 10_000) {
|
|
|
26
26
|
return Effect.flatMap(diagnostic, (annotations) => Effect.logWarning("OTLP trace export failed").pipe(Effect.annotateLogs(annotations)));
|
|
27
27
|
});
|
|
28
28
|
}
|
|
29
|
+
function recordStatus(response: HttpClientResponse.HttpClientResponse) {
|
|
30
|
+
const diagnostic = response.text.pipe(
|
|
31
|
+
Effect.orElseSucceed(() => "Unable to read collector response"),
|
|
32
|
+
Effect.map((body) => ({ "otlp.response": redactText(body, sensitive), "otlp.status": response.status })),
|
|
33
|
+
);
|
|
34
|
+
const checked = filterStatus(HttpClient.makeWith(() => Effect.succeed(response), Effect.succeed)).execute(response.request);
|
|
35
|
+
return Effect.matchEffect(checked, {
|
|
36
|
+
onFailure: () => warnOnce(diagnostic),
|
|
37
|
+
onSuccess: () =>
|
|
38
|
+
Effect.sync(() => {
|
|
39
|
+
warned = false;
|
|
40
|
+
}),
|
|
41
|
+
});
|
|
42
|
+
}
|
|
29
43
|
return client.pipe(
|
|
30
44
|
HttpClient.tap((response) => response.arrayBuffer),
|
|
31
45
|
HttpClient.transform((requestEffect, request) =>
|
|
@@ -36,18 +50,7 @@ export function otlpHttpClient(timeout = 10_000) {
|
|
|
36
50
|
}),
|
|
37
51
|
),
|
|
38
52
|
),
|
|
39
|
-
HttpClient.tap(
|
|
40
|
-
if (response.status >= HttpStatus.fromLiteral("OK") && response.status < HttpStatus.fromLiteral("MultipleChoices")) {
|
|
41
|
-
warned = false;
|
|
42
|
-
return Effect.void;
|
|
43
|
-
}
|
|
44
|
-
return warnOnce(
|
|
45
|
-
response.text.pipe(
|
|
46
|
-
Effect.orElseSucceed(() => "Unable to read collector response"),
|
|
47
|
-
Effect.map((body) => ({ "otlp.response": redactText(body, sensitive), "otlp.status": response.status })),
|
|
48
|
-
),
|
|
49
|
-
);
|
|
50
|
-
}),
|
|
53
|
+
HttpClient.tap(recordStatus),
|
|
51
54
|
HttpClient.tapError((error) => warnOnce(Effect.succeed({ "otlp.failure": error.reason._tag }))),
|
|
52
55
|
);
|
|
53
56
|
}),
|
package/src/socketRpc.ts
CHANGED
|
@@ -3,14 +3,27 @@ import { Headers, HttpServerRequest, HttpServerResponse } from "effect/http";
|
|
|
3
3
|
import * as HttpStatus from "effect/http/HttpStatus";
|
|
4
4
|
import { isAuthUnavailable, isForbidden } from "@shivaedev/errors/taxonomy.ts";
|
|
5
5
|
import { localTransport } from "@shivaedev/identity/server/local.ts";
|
|
6
|
-
import {
|
|
6
|
+
import { SessionPolicy } from "@shivaedev/identity/server/SessionPolicy.ts";
|
|
7
|
+
import { resolveSession } from "@shivaedev/identity/server/session.ts";
|
|
7
8
|
import { makeSocketSession, SocketSession } from "@shivaedev/identity/server/socketSession.ts";
|
|
8
9
|
import { bearerSocketRpc } from "#bearerSocketRpc.ts";
|
|
9
10
|
import type { HostPolicy } from "#hostOptions.ts";
|
|
10
11
|
|
|
11
|
-
export function socketRpc
|
|
12
|
+
export function socketRpc(
|
|
12
13
|
handler: Effect.Effect<HttpServerResponse.HttpServerResponse, never, HttpServerRequest.HttpServerRequest | Scope.Scope>,
|
|
13
|
-
policy: HostPolicy
|
|
14
|
+
policy: Extract<HostPolicy, { readonly policy: "loopback" }>,
|
|
15
|
+
trustedProxy: string | undefined,
|
|
16
|
+
nativeOrigins: readonly string[],
|
|
17
|
+
): Effect.Effect<HttpServerResponse.HttpServerResponse, never, HttpServerRequest.HttpServerRequest | Scope.Scope>;
|
|
18
|
+
export function socketRpc(
|
|
19
|
+
handler: Effect.Effect<HttpServerResponse.HttpServerResponse, never, HttpServerRequest.HttpServerRequest | Scope.Scope>,
|
|
20
|
+
policy: HostPolicy,
|
|
21
|
+
trustedProxy: string | undefined,
|
|
22
|
+
nativeOrigins: readonly string[],
|
|
23
|
+
): Effect.Effect<HttpServerResponse.HttpServerResponse, never, HttpServerRequest.HttpServerRequest | Scope.Scope | SessionPolicy>;
|
|
24
|
+
export function socketRpc(
|
|
25
|
+
handler: Effect.Effect<HttpServerResponse.HttpServerResponse, never, HttpServerRequest.HttpServerRequest | Scope.Scope>,
|
|
26
|
+
policy: HostPolicy,
|
|
14
27
|
trustedProxy: string | undefined,
|
|
15
28
|
nativeOrigins: readonly string[],
|
|
16
29
|
) {
|
|
@@ -21,10 +34,10 @@ export function socketRpc<TSession extends SessionShape>(
|
|
|
21
34
|
}
|
|
22
35
|
if (policy.policy !== "loopback") {
|
|
23
36
|
if (new URL(request.url, "http://localhost").searchParams.get("auth") === "frame") {
|
|
24
|
-
return yield* bearerSocketRpc(handler
|
|
37
|
+
return yield* bearerSocketRpc(handler);
|
|
25
38
|
}
|
|
26
|
-
yield* resolveSession(
|
|
27
|
-
const session = yield* makeSocketSession(
|
|
39
|
+
yield* resolveSession(Headers.empty, "$connect");
|
|
40
|
+
const session = yield* makeSocketSession((yield* SessionPolicy).streamRevalidationInterval ?? "30 seconds");
|
|
28
41
|
return yield* Effect.provideService(handler, SocketSession, session);
|
|
29
42
|
}
|
|
30
43
|
return yield* handler;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { Effect, Schema } from "effect";
|
|
2
|
+
import { ChildProcess, ChildProcessSpawner } from "effect/process";
|
|
3
|
+
|
|
4
|
+
export class PostgresPageCacheTrimError extends Schema.TaggedError<PostgresPageCacheTrimError>()("PostgresPageCacheTrimError", {
|
|
5
|
+
exitCode: Schema.Number,
|
|
6
|
+
}) {}
|
|
7
|
+
|
|
8
|
+
export const trimPostgresPageCache = Effect.fn("Server.trimPostgresPageCache")(function* (dataDirectory: string) {
|
|
9
|
+
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
|
|
10
|
+
const code = yield* spawner.exitCode(
|
|
11
|
+
ChildProcess.make(
|
|
12
|
+
"find",
|
|
13
|
+
[
|
|
14
|
+
dataDirectory,
|
|
15
|
+
"-type",
|
|
16
|
+
"f",
|
|
17
|
+
"-exec",
|
|
18
|
+
"sh",
|
|
19
|
+
"-c",
|
|
20
|
+
'for path do dd if="$path" iflag=nocache count=0 status=none || exit; done',
|
|
21
|
+
"sh",
|
|
22
|
+
"{}",
|
|
23
|
+
"+",
|
|
24
|
+
],
|
|
25
|
+
{ stderr: "inherit", stdout: "ignore" },
|
|
26
|
+
),
|
|
27
|
+
);
|
|
28
|
+
if (code !== 0) {
|
|
29
|
+
return yield* new PostgresPageCacheTrimError({ exitCode: Number(code) });
|
|
30
|
+
}
|
|
31
|
+
});
|