@shivaedev/server 0.35.1 → 0.37.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.
Files changed (44) hide show
  1. package/CHANGELOG.md +26 -0
  2. package/README.md +26 -14
  3. package/dist/AppModule.d.ts +3 -2
  4. package/dist/AppModule.d.ts.map +1 -1
  5. package/dist/adapters/migrationsIn.d.ts +5 -0
  6. package/dist/adapters/migrationsIn.d.ts.map +1 -0
  7. package/dist/adapters/migrationsIn.js +10 -0
  8. package/dist/adapters/migrationsIn.js.map +1 -0
  9. package/dist/appDatabaseHttp.d.ts +76 -68
  10. package/dist/appDatabaseHttp.d.ts.map +1 -1
  11. package/dist/appHost.d.ts +38 -34
  12. package/dist/appHost.d.ts.map +1 -1
  13. package/dist/appHostHttp.d.ts +38 -34
  14. package/dist/appHostHttp.d.ts.map +1 -1
  15. package/dist/appHostHttp.js +6 -7
  16. package/dist/appHostHttp.js.map +1 -1
  17. package/dist/appModules.d.ts +4 -3
  18. package/dist/appModules.d.ts.map +1 -1
  19. package/dist/appModules.js +7 -4
  20. package/dist/appModules.js.map +1 -1
  21. package/dist/hostRpc.d.ts +5 -1
  22. package/dist/hostRpc.d.ts.map +1 -1
  23. package/dist/hostRpcRoutes.d.ts +5 -1
  24. package/dist/hostRpcRoutes.d.ts.map +1 -1
  25. package/dist/hostSensitiveKeys.d.ts +10 -2
  26. package/dist/hostSensitiveKeys.d.ts.map +1 -1
  27. package/dist/hostSensitiveKeys.js +31 -5
  28. package/dist/hostSensitiveKeys.js.map +1 -1
  29. package/dist/redactedHost.d.ts +11 -0
  30. package/dist/redactedHost.d.ts.map +1 -0
  31. package/dist/redactedHost.js +11 -0
  32. package/dist/redactedHost.js.map +1 -0
  33. package/dist/workerHost.d.ts +6 -2
  34. package/dist/workerHost.d.ts.map +1 -1
  35. package/dist/workerHost.js +5 -2
  36. package/dist/workerHost.js.map +1 -1
  37. package/package.json +29 -29
  38. package/src/AppModule.ts +4 -3
  39. package/src/adapters/migrationsIn.ts +12 -0
  40. package/src/appHostHttp.ts +29 -32
  41. package/src/appModules.ts +14 -7
  42. package/src/hostSensitiveKeys.ts +35 -5
  43. package/src/redactedHost.ts +26 -0
  44. package/src/workerHost.ts +17 -3
@@ -1,11 +1,41 @@
1
+ import { Effect, MutableRef } from "effect";
1
2
  import { type Rpc, RpcGroup } from "effect/rpc";
3
+ import { schemaFieldKeys } from "@shivaedev/errors/schemaFieldScope.ts";
2
4
  import { schemaSensitiveKeys } from "@shivaedev/errors/schemaSensitiveKeys.ts";
3
5
 
4
- export function hostSensitiveKeys<TRpcs extends Rpc.Any>(options: {
6
+ export const hostSensitiveKeys = Effect.fnUntraced(function* <TRpcs extends Rpc.Any>(options: {
5
7
  readonly group?: RpcGroup.RpcGroup<TRpcs>;
6
8
  readonly config?: { readonly sensitiveKeys?: readonly string[] };
7
9
  }) {
8
- return options.group === undefined
9
- ? schemaSensitiveKeys(RpcGroup.make(), options.config?.sensitiveKeys)
10
- : schemaSensitiveKeys(options.group, options.config?.sensitiveKeys);
11
- }
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
+ }
14
+ // Model names only widen masking, so the configured policy already holds the startup's conflict verdict.
15
+ const current = MutableRef.make(yield* policy(configured));
16
+ const models = new Set<string>();
17
+ function sensitive(key: string) {
18
+ return models.has(key) || MutableRef.get(current)(key);
19
+ }
20
+ 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, ...models])).pipe(
23
+ Effect.map((completed) => MutableRef.set(current, completed)),
24
+ Effect.asVoid,
25
+ ),
26
+ forRpc: (tag: string | undefined) => {
27
+ const forTag = MutableRef.get(current).forRpc(tag);
28
+ return (key: string) => models.has(key) || forTag(key);
29
+ },
30
+ maskFailure: (tag: string | undefined, reason: "Fail" | "Die") => MutableRef.get(current).maskFailure(tag, reason),
31
+ models: {
32
+ declare: (model: { readonly ast: Parameters<typeof schemaFieldKeys>[0] }) =>
33
+ Effect.sync(() => {
34
+ for (const key of schemaFieldKeys(model.ast, [])) {
35
+ models.add(key);
36
+ }
37
+ }),
38
+ },
39
+ redactFailure: (tag: string | undefined, reason: "Fail" | "Die", value: unknown) => MutableRef.get(current).redactFailure(tag, reason, value),
40
+ });
41
+ });
@@ -0,0 +1,26 @@
1
+ import { Effect, Layer } from "effect";
2
+ import type { Rpc, RpcGroup } from "effect/rpc";
3
+ import { RepositoryModels } from "@shivaedev/sql/RepositoryModels.ts";
4
+ import { redactingDiagnostics } from "#adapters/redactingDiagnostics.ts";
5
+ import { hostSensitiveKeys } from "#hostSensitiveKeys.ts";
6
+ import { otlpTracing } from "#otlpTracing.ts";
7
+ import { RedactionPolicy } from "#RedactionPolicy.ts";
8
+
9
+ export type HostSensitivePolicy = Effect.Success<ReturnType<typeof hostSensitiveKeys>>;
10
+
11
+ // Both hosts build their process inside one policy, so repositories declare their models to the same policy that masks every log, error report and exported span.
12
+ export function redactedHost<TRpcs extends Rpc.Any, A, E, R>(
13
+ options: { readonly group?: RpcGroup.RpcGroup<TRpcs>; readonly config?: { readonly sensitiveKeys?: readonly string[] } },
14
+ host: (sensitive: HostSensitivePolicy) => Layer.Layer<A, E, R>,
15
+ ) {
16
+ return Layer.unwrap(
17
+ Effect.map(hostSensitiveKeys(options), (sensitive) =>
18
+ host(sensitive).pipe(
19
+ Layer.provide(redactingDiagnostics(sensitive)),
20
+ Layer.provide(otlpTracing),
21
+ Layer.provide(Layer.succeed(RedactionPolicy, sensitive)),
22
+ Layer.provide(Layer.succeed(RepositoryModels, sensitive.models)),
23
+ ),
24
+ ),
25
+ );
26
+ }
package/src/workerHost.ts CHANGED
@@ -1,12 +1,26 @@
1
1
  import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
2
- import { Layer } from "effect";
3
- import { type AppDatabaseOptions, appDatabase } from "#appDatabase.ts";
2
+ import { Effect, Layer } from "effect";
3
+ import { type AppDatabaseOptions, appDatabase, type DatabaseConfig } from "#appDatabase.ts";
4
+ import { redactedHost } from "#redactedHost.ts";
5
+
6
+ export interface WorkerHostConfig extends DatabaseConfig {
7
+ readonly sensitiveKeys?: readonly string[];
8
+ }
4
9
 
5
10
  export function workerHost<R, E, TRequirements, TServices, TServiceError, TServiceRequirements, TMigrations = never, TMigrationError = never>(
6
11
  options: AppDatabaseOptions<TMigrations, TMigrationError> & {
12
+ readonly config?: WorkerHostConfig;
7
13
  readonly workers: Layer.Layer<R, E, TRequirements>;
8
14
  readonly services: Layer.Layer<TServices, TServiceError, TServiceRequirements>;
9
15
  },
10
16
  ) {
11
- return options.workers.pipe(Layer.provideMerge(options.services), Layer.provideMerge(appDatabase(options)), Layer.provide(NodeCrypto.layer));
17
+ // A worker serves no RPC group, so its policy holds the built-in rules, config.sensitiveKeys and the models its repositories declare as they are built.
18
+ // The host announces readiness itself: an effect composed around the returned Layer runs outside its policy and exporter.
19
+ return redactedHost(options, () =>
20
+ Layer.effectDiscard(Effect.logInfo("Worker ready to take jobs")).pipe(
21
+ Layer.provideMerge(options.workers),
22
+ Layer.provideMerge(options.services),
23
+ Layer.provideMerge(appDatabase(options)),
24
+ ),
25
+ ).pipe(Layer.provide(NodeCrypto.layer));
12
26
  }