@shivaedev/server 0.19.0 → 0.20.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 +39 -0
- package/README.md +26 -16
- package/dist/{runtime/makePlatform.d.ts → adapters/makePlatformRuntime.d.ts} +2 -2
- package/dist/adapters/makePlatformRuntime.d.ts.map +1 -0
- package/dist/{runtime/makePlatform.js → adapters/makePlatformRuntime.js} +1 -1
- package/dist/adapters/makePlatformRuntime.js.map +1 -0
- package/dist/{nodeHttpServer.d.ts → adapters/nodeHttpServer.d.ts} +1 -0
- package/dist/adapters/nodeHttpServer.d.ts.map +1 -0
- package/dist/{nodeHttpServer.js → adapters/nodeHttpServer.js} +5 -0
- package/dist/adapters/nodeHttpServer.js.map +1 -0
- package/dist/adapters/subscriptionSignal.d.ts.map +1 -0
- package/dist/adapters/subscriptionSignal.js.map +1 -0
- package/dist/appDatabase.d.ts +2 -2
- package/dist/appDatabase.d.ts.map +1 -1
- package/dist/appDatabase.js +8 -3
- package/dist/appDatabase.js.map +1 -1
- package/dist/appHost.d.ts.map +1 -1
- package/dist/appHost.js +11 -4
- package/dist/appHost.js.map +1 -1
- package/dist/appHostHttp.d.ts +48 -30
- package/dist/appHostHttp.d.ts.map +1 -1
- package/dist/appHostHttp.js +2 -2
- package/dist/appHostHttp.js.map +1 -1
- package/dist/boundedRpcRequest.d.ts +3 -2
- package/dist/boundedRpcRequest.d.ts.map +1 -1
- package/dist/boundedRpcRequest.js +6 -5
- package/dist/boundedRpcRequest.js.map +1 -1
- package/dist/boundedRpcSocket.d.ts +4 -0
- package/dist/boundedRpcSocket.d.ts.map +1 -0
- package/dist/boundedRpcSocket.js +29 -0
- package/dist/boundedRpcSocket.js.map +1 -0
- package/dist/hostChanges.d.ts +4 -2
- package/dist/hostChanges.d.ts.map +1 -1
- package/dist/hostChanges.js +31 -33
- package/dist/hostChanges.js.map +1 -1
- package/dist/hostRpc.d.ts +4 -9
- package/dist/hostRpc.d.ts.map +1 -1
- package/dist/hostRpc.js +9 -17
- package/dist/hostRpc.js.map +1 -1
- package/dist/hostRpcRoutes.d.ts +5 -3
- package/dist/hostRpcRoutes.d.ts.map +1 -1
- package/dist/hostRpcRoutes.js +4 -3
- package/dist/hostRpcRoutes.js.map +1 -1
- package/dist/otlpHttpClient.js +2 -2
- package/dist/otlpHttpClient.js.map +1 -1
- package/dist/otlpTracing.d.ts.map +1 -1
- package/dist/otlpTracing.js +4 -4
- package/dist/otlpTracing.js.map +1 -1
- package/dist/publishCommand.d.ts +3 -0
- package/dist/publishCommand.d.ts.map +1 -0
- package/dist/publishCommand.js +36 -0
- package/dist/publishCommand.js.map +1 -0
- package/dist/redactingRpcSerialization.d.ts +3 -2
- package/dist/redactingRpcSerialization.d.ts.map +1 -1
- package/dist/redactingRpcSerialization.js +8 -8
- package/dist/redactingRpcSerialization.js.map +1 -1
- package/dist/rerunQuery.d.ts +7 -3
- package/dist/rerunQuery.d.ts.map +1 -1
- package/dist/rerunQuery.js +3 -4
- package/dist/rerunQuery.js.map +1 -1
- package/dist/setupDatabases.d.ts +1 -1
- package/dist/setupDatabases.d.ts.map +1 -1
- package/dist/setupDatabases.js +3 -3
- package/dist/setupDatabases.js.map +1 -1
- package/dist/testing/HostProcessError.d.ts +1 -1
- package/dist/testing/HostProcessError.d.ts.map +1 -1
- package/dist/testing/adapters/hostProcess.d.ts +4 -2
- package/dist/testing/adapters/hostProcess.d.ts.map +1 -1
- package/dist/testing/adapters/hostProcess.js +5 -2
- package/dist/testing/adapters/hostProcess.js.map +1 -1
- package/dist/testing/adapters/hostProcessReadouts.d.ts +10 -0
- package/dist/testing/adapters/hostProcessReadouts.d.ts.map +1 -0
- package/dist/testing/adapters/hostProcessReadouts.js +26 -0
- package/dist/testing/adapters/hostProcessReadouts.js.map +1 -0
- package/dist/testing/hostProcess.d.ts +4 -0
- package/dist/testing/hostProcess.d.ts.map +1 -1
- package/dist/testing/hostProcess.js +20 -2
- package/dist/testing/hostProcess.js.map +1 -1
- package/dist/testing/hostProcessChild.d.ts +1 -1
- package/dist/testing/hostProcessChild.d.ts.map +1 -1
- package/dist/testing/hostProcessChild.js +3 -4
- package/dist/testing/hostProcessChild.js.map +1 -1
- package/dist/testing/hostProcessProtocol.d.ts +6 -0
- package/dist/testing/hostProcessProtocol.d.ts.map +1 -1
- package/dist/testing/hostProcessProtocol.js +6 -0
- package/dist/testing/hostProcessProtocol.js.map +1 -1
- package/dist/testing/hostProcessReadouts.d.ts +7 -0
- package/dist/testing/hostProcessReadouts.d.ts.map +1 -0
- package/dist/testing/hostProcessReadouts.js +37 -0
- package/dist/testing/hostProcessReadouts.js.map +1 -0
- package/package.json +22 -19
- package/src/{runtime/makePlatform.ts → adapters/makePlatformRuntime.ts} +1 -1
- package/src/{nodeHttpServer.ts → adapters/nodeHttpServer.ts} +7 -0
- package/src/appDatabase.ts +9 -3
- package/src/appHost.ts +17 -7
- package/src/appHostHttp.ts +2 -2
- package/src/boundedRpcRequest.ts +6 -5
- package/src/boundedRpcSocket.ts +32 -0
- package/src/hostChanges.ts +60 -41
- package/src/hostRpc.ts +9 -23
- package/src/hostRpcRoutes.ts +21 -11
- package/src/otlpHttpClient.ts +2 -2
- package/src/otlpTracing.ts +8 -6
- package/src/publishCommand.ts +47 -0
- package/src/redactingRpcSerialization.ts +11 -12
- package/src/rerunQuery.ts +18 -6
- package/src/setupDatabases.ts +3 -3
- package/src/testing/HostProcessError.ts +1 -1
- package/src/testing/adapters/hostProcess.ts +8 -3
- package/src/testing/adapters/hostProcessReadouts.ts +29 -0
- package/src/testing/hostProcess.ts +26 -2
- package/src/testing/hostProcessChild.ts +3 -4
- package/src/testing/hostProcessProtocol.ts +6 -0
- package/src/testing/hostProcessReadouts.ts +46 -0
- package/dist/node-http/subscriptionSignal.d.ts.map +0 -1
- package/dist/node-http/subscriptionSignal.js.map +0 -1
- package/dist/nodeHttpServer.d.ts.map +0 -1
- package/dist/nodeHttpServer.js.map +0 -1
- package/dist/runtime/makePlatform.d.ts.map +0 -1
- package/dist/runtime/makePlatform.js.map +0 -1
- /package/dist/{node-http → adapters}/subscriptionSignal.d.ts +0 -0
- /package/dist/{node-http → adapters}/subscriptionSignal.js +0 -0
- /package/src/{node-http → adapters}/subscriptionSignal.ts +0 -0
|
@@ -8,6 +8,7 @@ export function nodeHttpServer(
|
|
|
8
8
|
readonly port?: number;
|
|
9
9
|
readonly gracefulShutdownTimeout?: Duration.Input;
|
|
10
10
|
readonly disablePreemptiveShutdown?: boolean;
|
|
11
|
+
readonly maxBodySize?: number;
|
|
11
12
|
},
|
|
12
13
|
) {
|
|
13
14
|
return Layer.unwrap(
|
|
@@ -17,8 +18,14 @@ export function nodeHttpServer(
|
|
|
17
18
|
gracefulShutdownTimeout: config?.gracefulShutdownTimeout,
|
|
18
19
|
host,
|
|
19
20
|
port,
|
|
21
|
+
websocket: config?.maxBodySize === undefined ? undefined : { maxPayload: socketPayloadLimit(config.maxBodySize) },
|
|
20
22
|
...(config?.disablePreemptiveShutdown === undefined ? {} : { disablePreemptiveShutdown: config.disablePreemptiveShutdown }),
|
|
21
23
|
});
|
|
22
24
|
}),
|
|
23
25
|
);
|
|
24
26
|
}
|
|
27
|
+
|
|
28
|
+
function socketPayloadLimit(bytes: number) {
|
|
29
|
+
// ws truncates maxPayload to a signed 32-bit integer; the RPC reader covers zero and larger limits.
|
|
30
|
+
return bytes > 0x7f_ff_ff_ff ? 0 : Math.max(1, bytes);
|
|
31
|
+
}
|
package/src/appDatabase.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { createHash } from "node:crypto";
|
|
2
1
|
import * as PgClient from "@effect/sql-pg/PgClient";
|
|
3
|
-
import { Clock, Config, Context, Duration, Effect, Fiber, Layer, Option, Redacted, Schedule } from "effect";
|
|
2
|
+
import { Clock, Config, Context, Crypto, Duration, Effect, Fiber, Layer, Option, Redacted, Schedule } from "effect";
|
|
3
|
+
import * as Hex from "effect/encoding/Hex";
|
|
4
4
|
import * as Reactivity from "effect/reactivity/Reactivity";
|
|
5
5
|
import { Migrator } from "effect/sql";
|
|
6
6
|
import { LiveServer } from "@shivaedev/contract/LiveServer.ts";
|
|
@@ -46,7 +46,7 @@ export function appDatabase<R, E = never>(options: AppDatabaseOptions<R, E>) {
|
|
|
46
46
|
const hints = yield* LiveServer;
|
|
47
47
|
const url = options.config?.databaseUrl ?? (yield* Config.Redacted("DATABASE_URL"));
|
|
48
48
|
const name = yield* Config.String("APP_NAME").pipe(Config.withDefault(new URL(Redacted.value(url)).pathname));
|
|
49
|
-
const channel = changes?.channel ??
|
|
49
|
+
const channel = changes?.channel ?? (yield* defaultChangesChannel(name));
|
|
50
50
|
const listenUrl = options.config?.listenDatabaseUrl ?? Option.getOrUndefined(yield* Config.Redacted("LISTEN_DATABASE_URL").pipe(Config.option));
|
|
51
51
|
const listenerClient =
|
|
52
52
|
listenUrl === undefined
|
|
@@ -74,6 +74,12 @@ export function appDatabase<R, E = never>(options: AppDatabaseOptions<R, E>) {
|
|
|
74
74
|
return worker.pipe(Layer.provideMerge(prepared));
|
|
75
75
|
}
|
|
76
76
|
|
|
77
|
+
const defaultChangesChannel = Effect.fnUntraced(function* (name: string) {
|
|
78
|
+
const crypto = yield* Crypto.Crypto;
|
|
79
|
+
const digest = yield* crypto.digest("SHA-256", new TextEncoder().encode(name)).pipe(Effect.orDie);
|
|
80
|
+
return `changes_${Hex.encode(digest).slice(0, 32)}`;
|
|
81
|
+
});
|
|
82
|
+
|
|
77
83
|
function prepareMigrations<R, E>(migrations: AppDatabaseOptions<R, E>["migrations"]) {
|
|
78
84
|
switch (migrations.mode) {
|
|
79
85
|
case "check": {
|
package/src/appHost.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
|
|
2
|
-
import { Layer } from "effect";
|
|
2
|
+
import { Effect, Layer } from "effect";
|
|
3
3
|
import type { Rpc } from "effect/rpc";
|
|
4
|
+
import { schemaSensitiveKeys } from "@shivaedev/errors/schemaSensitiveKeys.ts";
|
|
5
|
+
import { isSensitiveKey } from "@shivaedev/errors/sensitive.ts";
|
|
4
6
|
import type { SessionPolicy } from "@shivaedev/identity/server/SessionPolicy.ts";
|
|
5
7
|
import type { AppHostFactory } from "#AppHostFactory.ts";
|
|
6
8
|
import type { AppHostInput, HostPreflight } from "#AppHostInput.ts";
|
|
@@ -8,6 +10,7 @@ import { appDatabase, type DatabaseConfig } from "#appDatabase.ts";
|
|
|
8
10
|
import { appHostHttp, deferredHost, migratedHost } from "#appHostHttp.ts";
|
|
9
11
|
import type { HostCoreOptions, HostPolicy, HostRouteRequirements, HostRoutes } from "#hostOptions.ts";
|
|
10
12
|
import type { MigrationOptions } from "#MigrationOptions.ts";
|
|
13
|
+
import { redactingDiagnostics } from "#redactingDiagnostics.ts";
|
|
11
14
|
|
|
12
15
|
export interface AppDatabaseOptions<R = never, E = never> {
|
|
13
16
|
readonly config?: DatabaseConfig;
|
|
@@ -114,16 +117,23 @@ export const appHost: AppHostFactory = <
|
|
|
114
117
|
TMigrationError
|
|
115
118
|
>,
|
|
116
119
|
) => {
|
|
120
|
+
const startup = options.preflight;
|
|
121
|
+
const preflight =
|
|
122
|
+
startup === undefined
|
|
123
|
+
? Layer.empty
|
|
124
|
+
: Layer.unwrap(
|
|
125
|
+
Effect.map(
|
|
126
|
+
"application" in options ? Effect.succeed(isSensitiveKey) : schemaSensitiveKeys(options.group, options.config?.sensitiveKeys),
|
|
127
|
+
(sensitive) => startup.pipe(Layer.provide(redactingDiagnostics(sensitive))),
|
|
128
|
+
),
|
|
129
|
+
);
|
|
117
130
|
if ("application" in options) {
|
|
118
|
-
return deferredHost(options.application, appDatabase(options)).pipe(
|
|
119
|
-
Layer.provideMerge(options.preflight ?? Layer.empty),
|
|
120
|
-
Layer.provide(NodeCrypto.layer),
|
|
121
|
-
);
|
|
131
|
+
return deferredHost(options.application, appDatabase(options)).pipe(Layer.provideMerge(preflight), Layer.provide(NodeCrypto.layer));
|
|
122
132
|
}
|
|
123
133
|
return options.migrations === undefined
|
|
124
|
-
? appHostHttp(options).pipe(Layer.provideMerge(
|
|
134
|
+
? appHostHttp(options).pipe(Layer.provideMerge(preflight), Layer.provide(NodeCrypto.layer))
|
|
125
135
|
: migratedHost({ ...options, migrations: options.migrations }, appDatabase({ ...options, migrations: options.migrations })).pipe(
|
|
126
|
-
Layer.provideMerge(
|
|
136
|
+
Layer.provideMerge(preflight),
|
|
127
137
|
Layer.provide(NodeCrypto.layer),
|
|
128
138
|
);
|
|
129
139
|
};
|
package/src/appHostHttp.ts
CHANGED
|
@@ -3,6 +3,7 @@ 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 { nodeHttpServer } from "#adapters/nodeHttpServer.ts";
|
|
6
7
|
import type { appDatabase } from "#appDatabase.ts";
|
|
7
8
|
import type { AppHttpHostOptions } from "#appHost.ts";
|
|
8
9
|
import { browserErrorRoute } from "#browserErrorRoute.ts";
|
|
@@ -14,7 +15,6 @@ import { hostRpcRoutes } from "#hostRpcRoutes.ts";
|
|
|
14
15
|
import { hostTransport } from "#hostTransport.ts";
|
|
15
16
|
import type { MigrationOptions } from "#MigrationOptions.ts";
|
|
16
17
|
import { nativeClientOrigins } from "#nativeClientOrigins.ts";
|
|
17
|
-
import { nodeHttpServer } from "#nodeHttpServer.ts";
|
|
18
18
|
import { otlpTracing } from "#otlpTracing.ts";
|
|
19
19
|
import { RedactionPolicy } from "#RedactionPolicy.ts";
|
|
20
20
|
import { redactingDiagnostics } from "#redactingDiagnostics.ts";
|
|
@@ -53,7 +53,7 @@ export function appHostHttp<
|
|
|
53
53
|
const assets = options.assets === undefined ? Layer.empty : hostAssets(options.assets);
|
|
54
54
|
const application = Layer.mergeAll(options.services ?? Layer.empty);
|
|
55
55
|
const sessions = Layer.mergeAll(Layer.empty, ...(options.sessions === undefined ? [] : [options.sessions]));
|
|
56
|
-
const server = nodeHttpServer(host, options.config);
|
|
56
|
+
const server = nodeHttpServer(host, { ...options.config, maxBodySize: bodySize });
|
|
57
57
|
const applicationRoutes = hostApplicationRoutes(options.routes, bodySize);
|
|
58
58
|
const httpRoutes = Layer.mergeAll(
|
|
59
59
|
routes,
|
package/src/boundedRpcRequest.ts
CHANGED
|
@@ -1,16 +1,17 @@
|
|
|
1
1
|
import { Effect } from "effect";
|
|
2
|
-
import { HttpServerError,
|
|
2
|
+
import { HttpServerError, HttpServerRequest } from "effect/http";
|
|
3
3
|
import { readBoundedBody } from "@shivaedev/http/server/boundedBody.ts";
|
|
4
4
|
|
|
5
|
-
export
|
|
6
|
-
const
|
|
5
|
+
export const boundedRpcRequest = Effect.fnUntraced(function* () {
|
|
6
|
+
const request = yield* HttpServerRequest.HttpServerRequest;
|
|
7
|
+
const arrayBuffer = yield* readBoundedBody().pipe(
|
|
8
|
+
Effect.provideService(HttpServerRequest.HttpServerRequest, request),
|
|
7
9
|
Effect.mapError((cause) =>
|
|
8
10
|
cause._tag === "RequestBodyTooLarge"
|
|
9
11
|
? new HttpServerError.HttpServerError({ reason: new HttpServerError.RequestParseError({ cause, request }) })
|
|
10
12
|
: cause,
|
|
11
13
|
),
|
|
12
14
|
Effect.cached,
|
|
13
|
-
Effect.runSync,
|
|
14
15
|
);
|
|
15
16
|
const text = Effect.map(arrayBuffer, (body) => new TextDecoder().decode(body));
|
|
16
17
|
// The native text reader destroys the HTTP socket on overflow, preventing a 4xx response.
|
|
@@ -25,4 +26,4 @@ export function boundedRpcRequest(request: HttpServerRequest.HttpServerRequest):
|
|
|
25
26
|
return Reflect.get(target, key, receiver);
|
|
26
27
|
},
|
|
27
28
|
});
|
|
28
|
-
}
|
|
29
|
+
});
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
import { HttpServerRequest } from "effect/http";
|
|
3
|
+
import * as Socket from "effect/socket/Socket";
|
|
4
|
+
|
|
5
|
+
export const boundedRpcSocket = Effect.fnUntraced(function* (limit: number) {
|
|
6
|
+
const request = yield* HttpServerRequest.HttpServerRequest;
|
|
7
|
+
const upgrade = Effect.map(
|
|
8
|
+
request.upgrade,
|
|
9
|
+
(socket): Socket.Socket => ({
|
|
10
|
+
...socket,
|
|
11
|
+
reader: Effect.gen(function* () {
|
|
12
|
+
const reader = yield* socket.reader;
|
|
13
|
+
const writer = yield* socket.writer;
|
|
14
|
+
return {
|
|
15
|
+
...reader,
|
|
16
|
+
pull: Effect.flatMap(reader.pull, (frames) => {
|
|
17
|
+
if (frames.every((frame) => (typeof frame === "string" ? Buffer.byteLength(frame) : frame.byteLength) <= limit)) {
|
|
18
|
+
return Effect.succeed(frames);
|
|
19
|
+
}
|
|
20
|
+
const reason = new Socket.SocketCloseError({ closeReason: "RPC frame exceeds maxBodySize", code: 1009 });
|
|
21
|
+
return writer
|
|
22
|
+
.write(new Socket.CloseEvent(reason.code, reason.closeReason))
|
|
23
|
+
.pipe(Effect.andThen(Effect.fail(new Socket.SocketError({ reason }))));
|
|
24
|
+
}),
|
|
25
|
+
};
|
|
26
|
+
}),
|
|
27
|
+
}),
|
|
28
|
+
);
|
|
29
|
+
return new Proxy(request, {
|
|
30
|
+
get: (target, key, receiver) => (key === "upgrade" ? upgrade : Reflect.get(target, key, receiver)),
|
|
31
|
+
});
|
|
32
|
+
});
|
package/src/hostChanges.ts
CHANGED
|
@@ -1,60 +1,79 @@
|
|
|
1
1
|
import { Context, Effect, Layer, Option, Stream } from "effect";
|
|
2
2
|
import { Headers } from "effect/http";
|
|
3
|
-
import { Rpc,
|
|
3
|
+
import type { Rpc, RpcGroup } from "effect/rpc";
|
|
4
|
+
import { changesHandlerLayer } from "@shivaedev/contract/changesHandlerLayer.ts";
|
|
4
5
|
import { LiveServer } from "@shivaedev/contract/LiveServer.ts";
|
|
5
6
|
import { CurrentViewer, type Viewer } from "@shivaedev/contract/readPolicy.ts";
|
|
6
7
|
import type { QueryReference } from "@shivaedev/contract/serverMetadata.ts";
|
|
7
8
|
import { currentSessionContext } from "@shivaedev/identity/server/currentSessionContext.ts";
|
|
8
9
|
import { recheckSession } from "@shivaedev/identity/server/recheckSession.ts";
|
|
10
|
+
import { resolveChanges } from "@shivaedev/identity/server/resolveChanges.ts";
|
|
9
11
|
import { revalidateStream } from "@shivaedev/identity/server/revalidateStream.ts";
|
|
10
12
|
import { SessionPolicy } from "@shivaedev/identity/server/SessionPolicy.ts";
|
|
11
|
-
import { resolveChanges } from "@shivaedev/identity/server/sessionKeys.ts";
|
|
12
13
|
import { rerunQuery } from "#rerunQuery.ts";
|
|
13
14
|
|
|
14
|
-
export function hostChanges<R extends Rpc.Any>(
|
|
15
|
-
|
|
15
|
+
export function hostChanges<R extends Rpc.Any>(
|
|
16
|
+
group: RpcGroup.RpcGroup<R>,
|
|
17
|
+
): Layer.Layer<
|
|
18
|
+
Rpc.ToHandler<Extract<R, { readonly _tag: `${string}.$changes` }>>,
|
|
19
|
+
never,
|
|
20
|
+
Rpc.ExtractRequires<Extract<R, { readonly _tag: `${string}.$changes` }>, `${string}.$changes`>
|
|
21
|
+
>;
|
|
22
|
+
export function hostChanges(group: RpcGroup.RpcGroup<Rpc.Any>) {
|
|
23
|
+
return Layer.unwrap(
|
|
16
24
|
Effect.gen(function* () {
|
|
17
25
|
const changes = yield* Effect.serviceOption(LiveServer);
|
|
18
26
|
const sessions = yield* Effect.serviceOption(SessionPolicy);
|
|
19
|
-
const
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
if (Option.isNone(sessions) || Option.isNone(viewer)) {
|
|
31
|
-
return stream;
|
|
32
|
-
}
|
|
33
|
-
const revalidation = Effect.never.pipe(
|
|
34
|
-
revalidateStream(rpc, viewer.value.id, recheckSession(Headers.empty, rpc._tag)),
|
|
35
|
-
Effect.provideService(SessionPolicy, sessions.value),
|
|
36
|
-
);
|
|
37
|
-
return Stream.interruptWhen(stream, revalidation);
|
|
38
|
-
}),
|
|
39
|
-
);
|
|
40
|
-
}
|
|
41
|
-
for (const rpc of group.requests.values()) {
|
|
42
|
-
if (!(Rpc.isRpc(rpc) && rpc._tag.endsWith(".$changes"))) {
|
|
43
|
-
continue;
|
|
44
|
-
}
|
|
45
|
-
const provided = services.mapUnsafe.get(rpc.key);
|
|
46
|
-
if (Option.isNone(changes) && provided !== undefined) {
|
|
47
|
-
handlers.set(rpc.key, provided);
|
|
48
|
-
continue;
|
|
27
|
+
const retained: Layer.Layer<Rpc.Handler<string>>[] = [];
|
|
28
|
+
if (Option.isNone(changes)) {
|
|
29
|
+
for (const rpc of group.requests.values()) {
|
|
30
|
+
if (!rpc._tag.endsWith(".$changes")) {
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
const key = Context.Service<Rpc.Handler<string>>(rpc.key);
|
|
34
|
+
const provided = yield* Effect.serviceOption(key);
|
|
35
|
+
if (Option.isSome(provided)) {
|
|
36
|
+
retained.push(Layer.succeed(key, provided.value));
|
|
37
|
+
}
|
|
49
38
|
}
|
|
50
|
-
handlers.set(rpc.key, {
|
|
51
|
-
context: services,
|
|
52
|
-
handler: ({ queries }: { readonly queries: readonly QueryReference[] }, options: Parameters<Rpc.Handler<string>["handler"]>[1]) =>
|
|
53
|
-
subscribe(rpc, queries, options),
|
|
54
|
-
tag: rpc._tag,
|
|
55
|
-
});
|
|
56
39
|
}
|
|
57
|
-
|
|
40
|
+
const hosted = changesHandlerLayer(
|
|
41
|
+
group,
|
|
42
|
+
(rpc) =>
|
|
43
|
+
({ queries }: { readonly queries: readonly QueryReference[] }, options: Parameters<Rpc.Handler<string>["handler"]>[1]) =>
|
|
44
|
+
subscribe(group, changes, sessions, rpc, queries, options),
|
|
45
|
+
);
|
|
46
|
+
return Layer.mergeAll(hosted, ...retained);
|
|
47
|
+
}),
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function subscribe(
|
|
52
|
+
group: RpcGroup.RpcGroup<Rpc.Any>,
|
|
53
|
+
changes: Option.Option<Context.Service.Shape<typeof LiveServer>>,
|
|
54
|
+
sessions: Option.Option<typeof SessionPolicy.Service>,
|
|
55
|
+
rpc: Rpc.AnyWithProps,
|
|
56
|
+
queries: readonly QueryReference[],
|
|
57
|
+
options: Parameters<Rpc.Handler<string>["handler"]>[1],
|
|
58
|
+
) {
|
|
59
|
+
return Stream.unwrap(
|
|
60
|
+
Effect.gen(function* () {
|
|
61
|
+
const subscription = yield* resolveChanges(rpc, queries);
|
|
62
|
+
const viewer = yield* CurrentViewer;
|
|
63
|
+
const refresh = refreshSession(sessions, viewer, rpc._tag);
|
|
64
|
+
const read = rerunQuery(group, options);
|
|
65
|
+
type Failure = Effect.Error<typeof subscription.authorized | typeof refresh | ReturnType<typeof read>>;
|
|
66
|
+
const stream = Option.isSome(changes)
|
|
67
|
+
? Stream.unwrap(changes.value.subscribe<Failure, Effect.Services<ReturnType<typeof read>>, typeof read>({ ...subscription, refresh }, read))
|
|
68
|
+
: Stream.concat(Stream.succeed({ _tag: "Resync" } as const), Stream.never);
|
|
69
|
+
if (Option.isNone(sessions) || Option.isNone(viewer)) {
|
|
70
|
+
return stream;
|
|
71
|
+
}
|
|
72
|
+
const revalidation = Effect.never.pipe(
|
|
73
|
+
revalidateStream(rpc, viewer.value.id, recheckSession(Headers.empty, rpc._tag)),
|
|
74
|
+
Effect.provideService(SessionPolicy, sessions.value),
|
|
75
|
+
);
|
|
76
|
+
return Stream.interruptWhen(stream, revalidation);
|
|
58
77
|
}),
|
|
59
78
|
);
|
|
60
79
|
}
|
package/src/hostRpc.ts
CHANGED
|
@@ -1,33 +1,19 @@
|
|
|
1
|
-
import { Effect
|
|
1
|
+
import { Effect } from "effect";
|
|
2
2
|
import { type HttpEffect, HttpServerResponse } from "effect/http";
|
|
3
|
-
import { type Rpc, type RpcGroup,
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
3
|
+
import { type Rpc, type RpcGroup, RpcServer } from "effect/rpc";
|
|
4
|
+
import { CommandGuard, commandGuardLayer, guardCommands } from "@shivaedev/contract/CommandGuard.ts";
|
|
5
|
+
import { ReadPolicyGuard, readPolicyGuardLayer } from "@shivaedev/contract/ReadPolicyGuard.ts";
|
|
6
|
+
import { publishCommand } from "#publishCommand.ts";
|
|
7
7
|
import { tracedGroup } from "#tracedGroup.ts";
|
|
8
8
|
|
|
9
|
-
class HostedReadPolicy extends RpcMiddleware.Service<HostedReadPolicy>()("@shivaedev/server/HostedReadPolicy", {
|
|
10
|
-
error: Schema.Union([MissingReadPolicy, QueryReadDenied, WebSocketRequired]),
|
|
11
|
-
}) {}
|
|
12
|
-
|
|
13
|
-
export type { HostedReadPolicy };
|
|
14
|
-
|
|
15
9
|
export const rpcCacheControl: HttpEffect.PreResponseHandler = (_, response) =>
|
|
16
10
|
Effect.succeed(HttpServerResponse.setHeader(response, "cache-control", "private, no-store"));
|
|
17
11
|
|
|
18
12
|
export const hostRpc = Effect.fnUntraced(function* <R extends Rpc.Any>(group: RpcGroup.RpcGroup<R>, websocket = false) {
|
|
19
|
-
const guarded = group
|
|
20
|
-
// Keep the policy inside identity middleware so it sees the request's viewer.
|
|
21
|
-
for (const rpc of guarded.requests.values()) {
|
|
22
|
-
Object.assign(rpc, { middlewares: new Set([HostedReadPolicy, ...rpc.middlewares]) });
|
|
23
|
-
}
|
|
13
|
+
const guarded = yield* guardCommands(group);
|
|
24
14
|
const traced = tracedGroup(guarded);
|
|
15
|
+
const readPolicy = yield* ReadPolicyGuard.pipe(Effect.provide(readPolicyGuardLayer({ websocket })));
|
|
16
|
+
const publication = yield* CommandGuard.pipe(Effect.provide(commandGuardLayer(publishCommand)));
|
|
25
17
|
const handler = websocket ? RpcServer.toHttpEffectWebsocket(traced) : RpcServer.toHttpEffect(traced);
|
|
26
|
-
return yield* handler.pipe(
|
|
27
|
-
Effect.provideService(HostedReadPolicy, (effect, request) =>
|
|
28
|
-
!websocket && request.rpc._tag.endsWith(".$changes")
|
|
29
|
-
? Effect.fail(new WebSocketRequired({ message: `${request.rpc._tag} needs the WebSocket protocol` }))
|
|
30
|
-
: Effect.flatMap(CurrentViewer, (viewer) => authorizeReads(effect, request, viewer)),
|
|
31
|
-
),
|
|
32
|
-
);
|
|
18
|
+
return yield* handler.pipe(Effect.provideService(ReadPolicyGuard, readPolicy), Effect.provideService(CommandGuard, publication));
|
|
33
19
|
});
|
package/src/hostRpcRoutes.ts
CHANGED
|
@@ -8,6 +8,7 @@ import { localRpcGuard } from "@shivaedev/identity/server/adapters/local.ts";
|
|
|
8
8
|
import type { SessionPolicy } from "@shivaedev/identity/server/SessionPolicy.ts";
|
|
9
9
|
import { authenticatedLayer, maybeAuthenticatedLayer } from "@shivaedev/identity/server/session.ts";
|
|
10
10
|
import { boundedRpcRequest } from "#boundedRpcRequest.ts";
|
|
11
|
+
import { boundedRpcSocket } from "#boundedRpcSocket.ts";
|
|
11
12
|
import { hostChanges } from "#hostChanges.ts";
|
|
12
13
|
import type { HostPolicy } from "#hostOptions.ts";
|
|
13
14
|
import { hostRpc, rpcCacheControl } from "#hostRpc.ts";
|
|
@@ -57,7 +58,7 @@ function buildRpcRoutes<TRpcs extends Rpc.Any, THandlers, TError, TRequirements>
|
|
|
57
58
|
return HttpRouter.use((router) =>
|
|
58
59
|
Effect.gen(function* () {
|
|
59
60
|
yield* validateReadPolicies(options.group);
|
|
60
|
-
const wire = redactingRpcSerialization(yield* RpcSerialization.RpcSerialization, sensitive
|
|
61
|
+
const wire = redactingRpcSerialization(yield* RpcSerialization.RpcSerialization, sensitive);
|
|
61
62
|
const handler = yield* hostRpc(options.group).pipe(Effect.provideService(RpcSerialization.RpcSerialization, wire));
|
|
62
63
|
const guarded = localRpcGuard(handler, trustedProxy, nativeOrigins);
|
|
63
64
|
const websocket = yield* hostRpc(options.group, true).pipe(Effect.provideService(RpcSerialization.RpcSerialization, wire));
|
|
@@ -68,17 +69,26 @@ function buildRpcRoutes<TRpcs extends Rpc.Any, THandlers, TError, TRequirements>
|
|
|
68
69
|
status: HttpStatus.fromLiteral("Forbidden"),
|
|
69
70
|
}),
|
|
70
71
|
);
|
|
71
|
-
yield* router.add(
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
72
|
+
yield* router.add("GET", options.rpcPath ?? "/rpc", () =>
|
|
73
|
+
boundedRpcSocket(bodySize).pipe(
|
|
74
|
+
Effect.flatMap((request) =>
|
|
75
|
+
socketRpc(websocket, options, trustedProxy, nativeOrigins).pipe(
|
|
76
|
+
Effect.provideService(HttpServerRequest.HttpServerRequest, request),
|
|
77
|
+
Effect.provideService(RpcSerialization.RpcSerialization, wire),
|
|
78
|
+
),
|
|
79
|
+
),
|
|
80
|
+
),
|
|
75
81
|
);
|
|
76
|
-
yield* router.add("POST", options.rpcPath ?? "/rpc", (
|
|
77
|
-
|
|
78
|
-
Effect.
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
+
yield* router.add("POST", options.rpcPath ?? "/rpc", () =>
|
|
83
|
+
boundedRpcRequest().pipe(
|
|
84
|
+
Effect.flatMap((request) =>
|
|
85
|
+
guarded.pipe(
|
|
86
|
+
Effect.provideService(HttpIncomingMessage.MaxBodySize, ByteSize.bytes(bodySize)),
|
|
87
|
+
Effect.provideService(HttpServerRequest.HttpServerRequest, request),
|
|
88
|
+
Effect.provideService(RpcSerialization.RpcSerialization, wire),
|
|
89
|
+
HttpEffect.withPreResponseHandler(rpcCacheControl),
|
|
90
|
+
),
|
|
91
|
+
),
|
|
82
92
|
),
|
|
83
93
|
);
|
|
84
94
|
}),
|
package/src/otlpHttpClient.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Effect, Layer } from "effect";
|
|
2
2
|
import { FetchHttpClient, HttpClient, HttpClientError, type HttpClientRequest, type HttpClientResponse } from "effect/http";
|
|
3
3
|
import { redactText } from "@shivaedev/errors/sensitive.ts";
|
|
4
|
-
import {
|
|
4
|
+
import { checkStatus } from "@shivaedev/http/checkStatus.ts";
|
|
5
5
|
import { boundedOtlpResponse } from "#adapters/boundedOtlpResponse.ts";
|
|
6
6
|
import { RedactionPolicy } from "#RedactionPolicy.ts";
|
|
7
7
|
|
|
@@ -32,7 +32,7 @@ export function otlpHttpClient(timeout = 10_000) {
|
|
|
32
32
|
Effect.orElseSucceed(() => "Unable to read collector response"),
|
|
33
33
|
Effect.map((body) => ({ "otlp.response": redactText(body, sensitive), "otlp.status": response.status })),
|
|
34
34
|
);
|
|
35
|
-
const checked =
|
|
35
|
+
const checked = checkStatus(response);
|
|
36
36
|
return Effect.matchEffect(checked, {
|
|
37
37
|
onFailure: () => warnOnce(diagnostic),
|
|
38
38
|
onSuccess: () =>
|
package/src/otlpTracing.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Config, Effect, Layer, Schema } from "effect";
|
|
1
|
+
import { Config, Context, Effect, Layer, Schema } from "effect";
|
|
2
2
|
import { OtlpSerialization, OtlpTracer } from "effect/observability";
|
|
3
3
|
import { traceDiagnostics } from "#adapters/traceDiagnostics.ts";
|
|
4
4
|
import { otlpHttpClient } from "#otlpHttpClient.ts";
|
|
@@ -9,7 +9,7 @@ import { safeOtlpSerialization } from "#safeOtlpSerialization.ts";
|
|
|
9
9
|
|
|
10
10
|
const endpointUrl = Schema.URL.check(Schema.makeFilter((url) => url.protocol === "http:" || url.protocol === "https:"));
|
|
11
11
|
|
|
12
|
-
export const otlpTracing: Layer.Layer<never, Config.ConfigError> = Layer.
|
|
12
|
+
export const otlpTracing: Layer.Layer<never, Config.ConfigError> = Layer.effectContext(
|
|
13
13
|
Effect.gen(function* () {
|
|
14
14
|
const tracesEndpoint = yield* Config.schema(endpointUrl, "OTEL_EXPORTER_OTLP_TRACES_ENDPOINT").pipe(Config.withDefault(undefined));
|
|
15
15
|
const endpoint =
|
|
@@ -22,7 +22,7 @@ export const otlpTracing: Layer.Layer<never, Config.ConfigError> = Layer.unwrap(
|
|
|
22
22
|
Config.withDefault(undefined),
|
|
23
23
|
));
|
|
24
24
|
if (endpoint === undefined) {
|
|
25
|
-
return
|
|
25
|
+
return Context.empty();
|
|
26
26
|
}
|
|
27
27
|
const configuredProtocol = yield* Config.String("OTEL_EXPORTER_OTLP_TRACES_PROTOCOL").pipe(
|
|
28
28
|
Config.orElse(() => Config.String("OTEL_EXPORTER_OTLP_PROTOCOL")),
|
|
@@ -60,9 +60,11 @@ export const otlpTracing: Layer.Layer<never, Config.ConfigError> = Layer.unwrap(
|
|
|
60
60
|
Layer.provide(otlpHttpClient(requestTimeout)),
|
|
61
61
|
);
|
|
62
62
|
const sensitive = yield* RedactionPolicy;
|
|
63
|
-
return
|
|
64
|
-
|
|
65
|
-
|
|
63
|
+
return yield* Layer.build(
|
|
64
|
+
redactingDiagnostics(sensitive).pipe(
|
|
65
|
+
Layer.provideMerge(traceDiagnostics),
|
|
66
|
+
Layer.provideMerge(redactingTracer(sensitive).pipe(Layer.provideMerge(tracer))),
|
|
67
|
+
),
|
|
66
68
|
);
|
|
67
69
|
}),
|
|
68
70
|
);
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { Effect, Option, Schedule } from "effect";
|
|
2
|
+
import * as Reactivity from "effect/reactivity/Reactivity";
|
|
3
|
+
import { CommandPublication, type CommandPublicationError } from "@shivaedev/changes/commandPublication.ts";
|
|
4
|
+
import { ChangeObserver } from "@shivaedev/sql/transact.ts";
|
|
5
|
+
|
|
6
|
+
const recoverPublication = Effect.fnUntraced(function* (cause: CommandPublicationError) {
|
|
7
|
+
yield* Effect.logError("Committed command invalidation failed; resynchronizing live streams", cause);
|
|
8
|
+
const publication = yield* CommandPublication;
|
|
9
|
+
yield* publication.recover.pipe(
|
|
10
|
+
Effect.catch((error) => Effect.logError("Committed command Resync broadcast failed; other hosts may remain stale", error)),
|
|
11
|
+
);
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
export function publishCommand<A, E, R>(effect: Effect.Effect<A, E, R>, declaredKeys: (result: A) => readonly unknown[]) {
|
|
15
|
+
return Effect.uninterruptibleMask((restore) =>
|
|
16
|
+
Effect.gen(function* () {
|
|
17
|
+
const reactivity = yield* Effect.serviceOption(Reactivity.Reactivity);
|
|
18
|
+
if (Option.isNone(reactivity)) {
|
|
19
|
+
return yield* restore(effect);
|
|
20
|
+
}
|
|
21
|
+
const published = new Set<unknown>();
|
|
22
|
+
const observer = yield* ChangeObserver;
|
|
23
|
+
const result = yield* restore(effect).pipe(
|
|
24
|
+
Effect.provideService(ChangeObserver, (event) => {
|
|
25
|
+
if (event._tag === "Published") {
|
|
26
|
+
for (const key of event.changes) {
|
|
27
|
+
published.add(key);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return observer(event);
|
|
31
|
+
}),
|
|
32
|
+
);
|
|
33
|
+
const keys = declaredKeys(result).filter((key) => !published.has(key));
|
|
34
|
+
if (keys.length > 0) {
|
|
35
|
+
const publication = yield* CommandPublication;
|
|
36
|
+
yield* publication
|
|
37
|
+
.publish(keys)
|
|
38
|
+
.pipe(
|
|
39
|
+
Effect.provideService(Reactivity.Reactivity, reactivity.value),
|
|
40
|
+
Effect.retry({ schedule: Schedule.exponential("50 millis"), times: 2 }),
|
|
41
|
+
Effect.catch(recoverPublication),
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
return result;
|
|
45
|
+
}),
|
|
46
|
+
);
|
|
47
|
+
}
|
|
@@ -1,25 +1,24 @@
|
|
|
1
|
-
import { Predicate } from "effect";
|
|
1
|
+
import { type Effect, Predicate } from "effect";
|
|
2
2
|
import type { RpcSerialization } from "effect/rpc";
|
|
3
3
|
import { redactWire } from "@shivaedev/errors/redact.ts";
|
|
4
|
-
import {
|
|
4
|
+
import type { schemaSensitiveKeys } from "@shivaedev/errors/schemaSensitiveKeys.ts";
|
|
5
|
+
import { REDACTED } from "@shivaedev/errors/sensitive.ts";
|
|
5
6
|
|
|
6
7
|
function isRequestId(value: unknown): value is string | number {
|
|
7
8
|
return typeof value === "string" || typeof value === "number";
|
|
8
9
|
}
|
|
9
10
|
export function redactingRpcSerialization(
|
|
10
11
|
wire: RpcSerialization.RpcSerialization["Service"],
|
|
11
|
-
sensitive:
|
|
12
|
-
maskFailure: (tag: string | undefined, reason: "Fail" | "Die") => boolean = () => false,
|
|
13
|
-
sensitiveForRpc: (tag: string | undefined) => SensitiveKey = () => sensitive,
|
|
12
|
+
sensitive: Effect.Success<ReturnType<typeof schemaSensitiveKeys>>,
|
|
14
13
|
): typeof wire {
|
|
15
14
|
function reason(value: unknown, tag: string | undefined): unknown {
|
|
16
|
-
const redacted = redactWire(value,
|
|
15
|
+
const redacted = redactWire(value, sensitive.forRpc(tag));
|
|
17
16
|
if (Predicate.isObjectOrArray(redacted)) {
|
|
18
|
-
if (Predicate.isTagged(value, "Fail") &&
|
|
19
|
-
return { ...redacted, _tag: "Fail", error:
|
|
17
|
+
if (Predicate.isTagged(value, "Fail") && Predicate.hasProperty(value, "error")) {
|
|
18
|
+
return { ...redacted, _tag: "Fail", error: sensitive.redactFailure(tag, "Fail", value.error) };
|
|
20
19
|
}
|
|
21
|
-
if (Predicate.isTagged(value, "Die") &&
|
|
22
|
-
return { ...redacted, _tag: "Die", defect:
|
|
20
|
+
if (Predicate.isTagged(value, "Die") && Predicate.hasProperty(value, "defect")) {
|
|
21
|
+
return { ...redacted, _tag: "Die", defect: sensitive.redactFailure(tag, "Die", value.defect) };
|
|
23
22
|
}
|
|
24
23
|
}
|
|
25
24
|
if (
|
|
@@ -32,7 +31,7 @@ export function redactingRpcSerialization(
|
|
|
32
31
|
}
|
|
33
32
|
function exit(value: unknown, tag: string | undefined): unknown {
|
|
34
33
|
if (Predicate.isTagged(value, "Failure") && Predicate.hasProperty(value, "cause")) {
|
|
35
|
-
const cause = Array.isArray(value.cause) ? value.cause.map((item) => reason(item, tag)) : redactWire(value.cause,
|
|
34
|
+
const cause = Array.isArray(value.cause) ? value.cause.map((item) => reason(item, tag)) : redactWire(value.cause, sensitive.forRpc(tag));
|
|
36
35
|
return { ...value, cause };
|
|
37
36
|
}
|
|
38
37
|
return value;
|
|
@@ -47,7 +46,7 @@ export function redactingRpcSerialization(
|
|
|
47
46
|
return value.map(response);
|
|
48
47
|
}
|
|
49
48
|
if (Predicate.isTagged(value, "Defect") && Predicate.hasProperty(value, "defect")) {
|
|
50
|
-
return { ...value, defect: maskFailure(undefined, "Die") ? REDACTED : redactWire(value.defect, sensitive) };
|
|
49
|
+
return { ...value, defect: sensitive.maskFailure(undefined, "Die") ? REDACTED : redactWire(value.defect, sensitive) };
|
|
51
50
|
}
|
|
52
51
|
if (
|
|
53
52
|
Predicate.isTagged(value, "Exit")
|
package/src/rerunQuery.ts
CHANGED
|
@@ -1,23 +1,35 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Effect } from "effect";
|
|
2
2
|
import { Rpc, RpcGroup } from "effect/rpc";
|
|
3
|
-
import { authorizeReads, CurrentViewer } from "@shivaedev/contract/readPolicy.ts";
|
|
3
|
+
import { authorizeReads, CurrentViewer, type PolicyChannels } from "@shivaedev/contract/readPolicy.ts";
|
|
4
|
+
import type { MissingReadPolicy, QueryReadDenied } from "@shivaedev/contract/readPolicyErrors.ts";
|
|
4
5
|
import type { QueryRequest } from "@shivaedev/contract/serverMetadata.ts";
|
|
5
6
|
|
|
7
|
+
type Handlers<R extends Rpc.Any> = string extends R["_tag"] ? Rpc.Handler<string> : Rpc.ToHandler<R>;
|
|
8
|
+
|
|
9
|
+
export function rerunQuery<R extends Rpc.Any>(
|
|
10
|
+
group: { readonly requests: ReadonlyMap<string, R> },
|
|
11
|
+
options: Parameters<Rpc.Handler<string>["handler"]>[1],
|
|
12
|
+
): (
|
|
13
|
+
request: QueryRequest,
|
|
14
|
+
) => Effect.Effect<
|
|
15
|
+
unknown,
|
|
16
|
+
Rpc.ErrorExit<R> | PolicyChannels<R>["error"] | MissingReadPolicy | QueryReadDenied,
|
|
17
|
+
Handlers<R> | PolicyChannels<R>["services"]
|
|
18
|
+
>;
|
|
6
19
|
export function rerunQuery(group: { readonly requests: ReadonlyMap<string, Rpc.Any> }, options: Parameters<Rpc.Handler<string>["handler"]>[1]) {
|
|
7
20
|
const prefix = options.rpc._tag.slice(0, -"$changes".length);
|
|
8
|
-
return Effect.fnUntraced(function* ({ payload, query }: QueryRequest)
|
|
21
|
+
return Effect.fnUntraced(function* ({ payload, query }: QueryRequest) {
|
|
9
22
|
const tag = `${prefix}${query.name}`;
|
|
10
23
|
const rpc = group.requests.get(tag);
|
|
11
24
|
if (!Rpc.isRpc(rpc)) {
|
|
12
25
|
return yield* Effect.die(new Error(`Unknown query: ${tag}`));
|
|
13
26
|
}
|
|
14
|
-
const
|
|
15
|
-
const handler = yield* RpcGroup.make(rpc).accessHandler(tag).pipe(Effect.provideContext(services));
|
|
27
|
+
const handler = yield* RpcGroup.make(rpc).accessHandler(tag);
|
|
16
28
|
const viewer = yield* CurrentViewer;
|
|
17
29
|
const result = handler(payload, { ...options });
|
|
18
30
|
if (!Effect.isEffect(result)) {
|
|
19
31
|
return yield* Effect.die(new Error(`Query ${tag} must return an Effect`));
|
|
20
32
|
}
|
|
21
|
-
return yield* authorizeReads(result, { payload, rpc }, viewer)
|
|
33
|
+
return yield* authorizeReads(result, { payload, rpc }, viewer);
|
|
22
34
|
});
|
|
23
35
|
}
|
package/src/setupDatabases.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { Config, Effect } from "effect";
|
|
2
|
+
import { dockerEnvironment } from "@shivaedev/local-postgres/server/dockerEnvironment.ts";
|
|
2
3
|
import { localPostgres } from "@shivaedev/local-postgres/server/localPostgres.ts";
|
|
3
4
|
|
|
4
5
|
export const setupDatabases = Effect.fn("Platform.setupDatabases")(function* () {
|
|
5
6
|
const config = yield* Config.all({
|
|
6
7
|
"DATABASE_URL": Config.String("DATABASE_URL"),
|
|
7
|
-
"DOCKER_CONTEXT": Config.String("DOCKER_CONTEXT").pipe(Config.withDefault("")),
|
|
8
|
-
"DOCKER_HOST": Config.String("DOCKER_HOST").pipe(Config.withDefault("")),
|
|
9
8
|
"TEST_DATABASE_URL": Config.String("TEST_DATABASE_URL"),
|
|
10
9
|
});
|
|
11
|
-
yield*
|
|
10
|
+
const environment = yield* dockerEnvironment;
|
|
11
|
+
yield* localPostgres(environment).prepareDatabases([config.DATABASE_URL, config.TEST_DATABASE_URL]);
|
|
12
12
|
});
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Data } from "effect";
|
|
2
2
|
|
|
3
3
|
export class HostProcessError extends Data.TaggedError("HostProcessError")<{
|
|
4
|
-
readonly operation: "launch" | "signal" | "request";
|
|
4
|
+
readonly operation: "launch" | "signal" | "request" | "readout";
|
|
5
5
|
readonly cause: unknown;
|
|
6
6
|
}> {}
|