@shivaedev/server 0.44.0 → 0.44.1

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.
@@ -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);