@shivaedev/server 0.16.1 → 0.18.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 +35 -3
- 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/adapters/boundedOtlpResponse.d.ts +4 -0
- package/dist/adapters/boundedOtlpResponse.d.ts.map +1 -0
- package/dist/adapters/boundedOtlpResponse.js +19 -0
- package/dist/adapters/boundedOtlpResponse.js.map +1 -0
- package/dist/appDatabase.d.ts +1 -1
- package/dist/appDatabase.d.ts.map +1 -1
- package/dist/appDatabase.js +4 -3
- package/dist/appDatabase.js.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 +3 -2
- 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/browserErrorRoute.d.ts +6 -1
- package/dist/browserErrorRoute.d.ts.map +1 -1
- package/dist/browserErrorRoute.js +38 -8
- package/dist/browserErrorRoute.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 +5 -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 +14 -9
- package/dist/otlpHttpClient.js.map +1 -1
- package/dist/otlpTracing.d.ts.map +1 -1
- package/dist/otlpTracing.js +7 -4
- package/dist/otlpTracing.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 +15 -15
- package/src/AppHostFactory.ts +0 -7
- package/src/AppHostInput.ts +0 -5
- package/src/adapters/boundedOtlpResponse.ts +27 -0
- package/src/appDatabase.ts +4 -3
- package/src/appHost.ts +3 -7
- package/src/appHostHttp.ts +11 -9
- package/src/bearerSocketRpc.ts +6 -6
- package/src/browserErrorRoute.ts +51 -8
- package/src/hostChanges.ts +15 -21
- package/src/hostOptions.ts +5 -3
- package/src/hostRpcRoutes.ts +36 -16
- package/src/otlpHttpClient.ts +19 -15
- package/src/otlpTracing.ts +12 -11
- package/src/socketRpc.ts +19 -6
- package/src/trimPostgresPageCache.ts +31 -0
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,8 @@
|
|
|
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
|
+
import { boundedOtlpResponse } from "#adapters/boundedOtlpResponse.ts";
|
|
5
6
|
import { RedactionPolicy } from "#RedactionPolicy.ts";
|
|
6
7
|
|
|
7
8
|
function timeoutError(request: HttpClientRequest.HttpClientRequest) {
|
|
@@ -26,8 +27,22 @@ export function otlpHttpClient(timeout = 10_000) {
|
|
|
26
27
|
return Effect.flatMap(diagnostic, (annotations) => Effect.logWarning("OTLP trace export failed").pipe(Effect.annotateLogs(annotations)));
|
|
27
28
|
});
|
|
28
29
|
}
|
|
30
|
+
function recordStatus(response: HttpClientResponse.HttpClientResponse) {
|
|
31
|
+
const diagnostic = response.text.pipe(
|
|
32
|
+
Effect.orElseSucceed(() => "Unable to read collector response"),
|
|
33
|
+
Effect.map((body) => ({ "otlp.response": redactText(body, sensitive), "otlp.status": response.status })),
|
|
34
|
+
);
|
|
35
|
+
const checked = filterStatus(HttpClient.makeWith(() => Effect.succeed(response), Effect.succeed)).execute(response.request);
|
|
36
|
+
return Effect.matchEffect(checked, {
|
|
37
|
+
onFailure: () => warnOnce(diagnostic),
|
|
38
|
+
onSuccess: () =>
|
|
39
|
+
Effect.sync(() => {
|
|
40
|
+
warned = false;
|
|
41
|
+
}),
|
|
42
|
+
});
|
|
43
|
+
}
|
|
29
44
|
return client.pipe(
|
|
30
|
-
HttpClient.
|
|
45
|
+
HttpClient.transformResponse(Effect.flatMap(boundedOtlpResponse)),
|
|
31
46
|
HttpClient.transform((requestEffect, request) =>
|
|
32
47
|
requestEffect.pipe(
|
|
33
48
|
Effect.timeoutOrElse({
|
|
@@ -36,18 +51,7 @@ export function otlpHttpClient(timeout = 10_000) {
|
|
|
36
51
|
}),
|
|
37
52
|
),
|
|
38
53
|
),
|
|
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
|
-
}),
|
|
54
|
+
HttpClient.tap(recordStatus),
|
|
51
55
|
HttpClient.tapError((error) => warnOnce(Effect.succeed({ "otlp.failure": error.reason._tag }))),
|
|
52
56
|
);
|
|
53
57
|
}),
|
package/src/otlpTracing.ts
CHANGED
|
@@ -7,19 +7,20 @@ import { redactingDiagnostics } from "#redactingDiagnostics.ts";
|
|
|
7
7
|
import { redactingTracer } from "#redactingTracer.ts";
|
|
8
8
|
import { safeOtlpSerialization } from "#safeOtlpSerialization.ts";
|
|
9
9
|
|
|
10
|
+
const endpointUrl = Schema.URL.check(Schema.makeFilter((url) => url.protocol === "http:" || url.protocol === "https:"));
|
|
11
|
+
|
|
10
12
|
export const otlpTracing: Layer.Layer<never, Config.ConfigError> = Layer.unwrap(
|
|
11
13
|
Effect.gen(function* () {
|
|
12
|
-
const
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
),
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
);
|
|
14
|
+
const tracesEndpoint = yield* Config.schema(endpointUrl, "OTEL_EXPORTER_OTLP_TRACES_ENDPOINT").pipe(Config.withDefault(undefined));
|
|
15
|
+
const endpoint =
|
|
16
|
+
tracesEndpoint
|
|
17
|
+
?? (yield* Config.schema(endpointUrl, "OTEL_EXPORTER_OTLP_ENDPOINT").pipe(
|
|
18
|
+
Config.map((url) => {
|
|
19
|
+
url.pathname = `${url.pathname.replace(/\/$/u, "")}/v1/traces`;
|
|
20
|
+
return url;
|
|
21
|
+
}),
|
|
22
|
+
Config.withDefault(undefined),
|
|
23
|
+
));
|
|
23
24
|
if (endpoint === undefined) {
|
|
24
25
|
return Layer.empty;
|
|
25
26
|
}
|
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
|
+
});
|