@shivaedev/server 0.44.0 → 0.44.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -11,7 +11,7 @@ import type { HostOptions, HostPolicy, HostRouteRequirements } from "#hostOption
11
11
  import { hostRpcRoutes } from "#hostRpcRoutes.ts";
12
12
  import { hostTransport } from "#hostTransport.ts";
13
13
  import { nativeClientOrigins } from "#nativeClientOrigins.ts";
14
- import { redactedHost } from "#redactedHost.ts";
14
+ import { type HostSensitivePolicy, redactedHost } from "#redactedHost.ts";
15
15
 
16
16
  export function appHostHttp<
17
17
  TRpcs extends Rpc.Any,
@@ -40,36 +40,61 @@ export function appHostHttp<
40
40
  TRequestRequirements
41
41
  > &
42
42
  HostPolicy,
43
+ ) {
44
+ return redactedHost(options, (sensitive) => servedHost(options, sensitive));
45
+ }
46
+
47
+ // The host inside its redaction policy; a deferred application runs inside the same policy before it names this host.
48
+ export function servedHost<
49
+ TRpcs extends Rpc.Any,
50
+ THandlers,
51
+ THandlerError,
52
+ THandlerRequirements,
53
+ TServices,
54
+ TServiceError,
55
+ TServiceRequirements,
56
+ TRouteError = never,
57
+ TRouteRequirements = HostRouteRequirements,
58
+ TRequestServices = never,
59
+ TRequestRequirements = never,
60
+ >(
61
+ options: HostOptions<
62
+ TRpcs,
63
+ THandlers,
64
+ THandlerError,
65
+ THandlerRequirements,
66
+ TServices,
67
+ TServiceError,
68
+ TServiceRequirements,
69
+ TRouteError,
70
+ TRouteRequirements,
71
+ TRequestServices,
72
+ TRequestRequirements
73
+ > &
74
+ HostPolicy,
75
+ sensitive: HostSensitivePolicy,
43
76
  ) {
44
77
  const assets = options.assets === undefined ? Layer.empty : hostAssets(options.assets);
45
- return redactedHost(options, (sensitive) =>
46
- Layer.unwrap(
47
- Effect.gen(function* () {
48
- const transport = yield* hostTransport(options);
49
- const { bodySize, host, trustedProxy } = transport;
50
- const routes = options.group === undefined ? Layer.empty : hostRpcRoutes(options, transport, sensitive);
51
- const application = Layer.mergeAll(options.services ?? Layer.empty);
52
- const sessions = Layer.mergeAll(Layer.empty, ...(options.sessions === undefined ? [] : [options.sessions]));
53
- const server = nodeHttpServer(host, { ...options.config, maxBodySize: bodySize });
54
- const applicationRoutes = hostApplicationRoutes(options.routes, bodySize);
55
- const httpRoutes = Layer.mergeAll(
56
- routes,
57
- healthRoute,
58
- browserErrorRoute(trustedProxy, options.browserErrors),
59
- assets,
60
- applicationRoutes,
61
- ).pipe(
62
- Layer.provide(options.group === undefined ? Layer.empty : nativeClientOrigins(trustedProxy, "rpc", options.rpcPath)),
63
- Layer.provide(nativeClientOrigins(trustedProxy, "auth")),
64
- );
65
- return Layer.unwrap(
66
- HttpRouter.toHttpEffect(httpRoutes).pipe(
67
- // Building the routes built every repository, so the policy is complete before the server takes a request.
68
- Effect.tap(() => sensitive.complete),
69
- Effect.map((handler) => HttpServer.serve(handler).pipe(HttpServer.withLogAddress, Layer.provideMerge(server))),
70
- ),
71
- ).pipe(Layer.provideMerge(application), Layer.provide(sessions), Layer.provideMerge(hostHttpServices));
72
- }),
73
- ),
78
+ return Layer.unwrap(
79
+ Effect.gen(function* () {
80
+ const transport = yield* hostTransport(options);
81
+ const { bodySize, host, trustedProxy } = transport;
82
+ const routes = options.group === undefined ? Layer.empty : hostRpcRoutes(options, transport, sensitive);
83
+ const application = Layer.mergeAll(options.services ?? Layer.empty);
84
+ const sessions = Layer.mergeAll(Layer.empty, ...(options.sessions === undefined ? [] : [options.sessions]));
85
+ const server = nodeHttpServer(host, { ...options.config, maxBodySize: bodySize });
86
+ const applicationRoutes = hostApplicationRoutes(options.routes, bodySize);
87
+ const httpRoutes = Layer.mergeAll(routes, healthRoute, browserErrorRoute(trustedProxy, options.browserErrors), assets, applicationRoutes).pipe(
88
+ Layer.provide(options.group === undefined ? Layer.empty : nativeClientOrigins(trustedProxy, "rpc", options.rpcPath)),
89
+ Layer.provide(nativeClientOrigins(trustedProxy, "auth")),
90
+ );
91
+ return Layer.unwrap(
92
+ HttpRouter.toHttpEffect(httpRoutes).pipe(
93
+ // Building the routes built every repository, so the policy is complete before the server takes a request.
94
+ Effect.tap(() => sensitive.complete),
95
+ Effect.map((handler) => HttpServer.serve(handler).pipe(HttpServer.withLogAddress, Layer.provideMerge(server))),
96
+ ),
97
+ ).pipe(Layer.provideMerge(application), Layer.provide(sessions), Layer.provideMerge(hostHttpServices));
98
+ }),
74
99
  );
75
100
  }
@@ -3,26 +3,49 @@ import { type Rpc, RpcGroup } from "effect/rpc";
3
3
  import { schemaSensitiveKeys } from "@shivaedev/errors/schemaSensitiveKeys.ts";
4
4
  import { HostModels } from "#HostModels.ts";
5
5
 
6
- export const hostSensitiveKeys = Effect.fnUntraced(function* <TRpcs extends Rpc.Any>(options: {
7
- readonly group?: RpcGroup.RpcGroup<TRpcs>;
6
+ interface SensitiveSources<TRpcs extends Rpc.Any> {
8
7
  readonly config?: { readonly sensitiveKeys?: readonly string[] };
9
- }) {
10
- const configured = options.config?.sensitiveKeys ?? [];
11
- function policy(extra: readonly string[]) {
12
- return options.group === undefined ? schemaSensitiveKeys(RpcGroup.make(), extra) : schemaSensitiveKeys(options.group, extra);
13
- }
8
+ readonly group?: RpcGroup.RpcGroup<TRpcs>;
9
+ }
10
+
11
+ interface PolicySource {
12
+ readonly compile: (extra: readonly string[]) => ReturnType<typeof schemaSensitiveKeys>;
13
+ readonly configured: readonly string[];
14
+ }
15
+
16
+ function policySource<TRpcs extends Rpc.Any>({ config, group }: SensitiveSources<TRpcs>): PolicySource {
17
+ return {
18
+ compile: (extra) => (group === undefined ? schemaSensitiveKeys(RpcGroup.make(), extra) : schemaSensitiveKeys(group, extra)),
19
+ configured: config?.sensitiveKeys ?? [],
20
+ };
21
+ }
22
+
23
+ export const hostSensitiveKeys = Effect.fnUntraced(function* <TRpcs extends Rpc.Any>(options: SensitiveSources<TRpcs>) {
24
+ const source = MutableRef.make(policySource(options));
14
25
  // Model names only widen masking, so the configured policy already holds the startup's conflict verdict.
15
- const current = MutableRef.make(yield* policy(configured));
26
+ const current = MutableRef.make(yield* MutableRef.get(source).compile(MutableRef.get(source).configured));
16
27
  const { fieldNames } = yield* HostModels;
17
28
  function sensitive(key: string) {
18
29
  return fieldNames.has(key) || MutableRef.get(current)(key);
19
30
  }
20
31
  return Object.assign(sensitive, {
21
- // Repositories declare their models while the host builds them; complete recompiles every RPC's policy with their names, as config.sensitiveKeys.
22
- complete: Effect.suspend(() => policy([...configured, ...fieldNames])).pipe(
32
+ // Repositories declare their models while the host builds them, and a deferred application names its RPC group and configured keys
33
+ // only when it returns, after running inside this policy: complete and configure recompile every RPC's policy from what is known then.
34
+ complete: Effect.suspend(() => {
35
+ const { compile, configured } = MutableRef.get(source);
36
+ return compile([...configured, ...fieldNames]);
37
+ }).pipe(
23
38
  Effect.map((completed) => MutableRef.set(current, completed)),
24
39
  Effect.asVoid,
25
40
  ),
41
+ configure: <TConfigured extends Rpc.Any>(sources: SensitiveSources<TConfigured>) =>
42
+ Effect.suspend(() => {
43
+ const next = policySource(sources);
44
+ return Effect.map(next.compile(next.configured), (compiled) => {
45
+ MutableRef.set(source, next);
46
+ MutableRef.set(current, compiled);
47
+ });
48
+ }),
26
49
  forRpc: (tag: string | undefined) => {
27
50
  const forTag = MutableRef.get(current).forRpc(tag);
28
51
  return (key: string) => fieldNames.has(key) || forTag(key);