@shivaedev/server 0.43.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.
Files changed (56) hide show
  1. package/CHANGELOG.md +21 -0
  2. package/README.md +2 -2
  3. package/dist/HostModels.d.ts +9 -0
  4. package/dist/HostModels.d.ts.map +1 -0
  5. package/dist/HostModels.js +27 -0
  6. package/dist/HostModels.js.map +1 -0
  7. package/dist/appDatabase.d.ts +1 -2
  8. package/dist/appDatabase.d.ts.map +1 -1
  9. package/dist/appDatabase.js +6 -9
  10. package/dist/appDatabase.js.map +1 -1
  11. package/dist/appDatabaseHttp.d.ts +68 -68
  12. package/dist/appDatabaseHttp.d.ts.map +1 -1
  13. package/dist/appDatabaseHttp.js +4 -2
  14. package/dist/appDatabaseHttp.js.map +1 -1
  15. package/dist/appHost.d.ts +34 -34
  16. package/dist/appHost.d.ts.map +1 -1
  17. package/dist/appHost.js +2 -1
  18. package/dist/appHost.js.map +1 -1
  19. package/dist/appHostHttp.d.ts +178 -34
  20. package/dist/appHostHttp.d.ts.map +1 -1
  21. package/dist/appHostHttp.js +6 -2
  22. package/dist/appHostHttp.js.map +1 -1
  23. package/dist/defaultChangesChannel.d.ts +4 -0
  24. package/dist/defaultChangesChannel.d.ts.map +1 -0
  25. package/dist/defaultChangesChannel.js +8 -0
  26. package/dist/defaultChangesChannel.js.map +1 -0
  27. package/dist/hostPreflight.d.ts +3 -2
  28. package/dist/hostPreflight.d.ts.map +1 -1
  29. package/dist/hostPreflight.js.map +1 -1
  30. package/dist/hostSensitiveKeys.d.ts +8 -10
  31. package/dist/hostSensitiveKeys.d.ts.map +1 -1
  32. package/dist/hostSensitiveKeys.js +25 -18
  33. package/dist/hostSensitiveKeys.js.map +1 -1
  34. package/dist/nativeClientOrigins.d.ts.map +1 -1
  35. package/dist/nativeClientOrigins.js +3 -1
  36. package/dist/nativeClientOrigins.js.map +1 -1
  37. package/dist/redactedHost.d.ts +1 -1
  38. package/dist/redactedHost.d.ts.map +1 -1
  39. package/dist/redactedHost.js +3 -14
  40. package/dist/redactedHost.js.map +1 -1
  41. package/dist/workerHost.d.ts +1 -1
  42. package/dist/workerHost.d.ts.map +1 -1
  43. package/dist/workerHost.js +2 -1
  44. package/dist/workerHost.js.map +1 -1
  45. package/package.json +6 -6
  46. package/src/HostModels.ts +30 -0
  47. package/src/appDatabase.ts +6 -10
  48. package/src/appDatabaseHttp.ts +8 -2
  49. package/src/appHost.ts +6 -1
  50. package/src/appHostHttp.ts +55 -30
  51. package/src/defaultChangesChannel.ts +8 -0
  52. package/src/hostPreflight.ts +4 -3
  53. package/src/hostSensitiveKeys.ts +37 -22
  54. package/src/nativeClientOrigins.ts +4 -1
  55. package/src/redactedHost.ts +2 -18
  56. package/src/workerHost.ts +2 -1
@@ -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
  }
@@ -0,0 +1,8 @@
1
+ import { Effect } from "effect";
2
+ import { Digest } from "effect/crypto";
3
+
4
+ export const defaultChangesChannel = Effect.fnUntraced(function* (name: string) {
5
+ const digest = yield* Digest.Digest;
6
+ const hash = yield* digest.digestHex(Digest.sha256, new TextEncoder().encode(name));
7
+ return `changes_${hash.slice(0, 32)}`;
8
+ });
@@ -1,5 +1,6 @@
1
1
  import { Effect, Layer } from "effect";
2
2
  import { redactingDiagnostics } from "#adapters/redactingDiagnostics.ts";
3
+ import type { HostModels } from "#HostModels.ts";
3
4
  import type { hostCrypto } from "#hostCrypto.ts";
4
5
 
5
6
  export type HostPreflight<TServices = never, TError = never, TRequirements = never> =
@@ -11,12 +12,12 @@ type HostCrypto = Layer.Success<typeof hostCrypto>;
11
12
  export type PreflightHost<THost extends Layer.Any, TServices, TError, TRequirements> = Layer.Layer<
12
13
  Layer.Success<THost> | TServices,
13
14
  Layer.Error<THost> | TError,
14
- Exclude<Layer.Services<THost>, TServices | HostCrypto> | Exclude<TRequirements, HostCrypto>
15
+ Exclude<Layer.Services<THost>, TServices | HostCrypto | HostModels> | Exclude<TRequirements, HostCrypto>
15
16
  >;
16
17
 
17
- export function hostPreflight<TServices, TError, TRequirements, TSensitiveError>(
18
+ export function hostPreflight<TServices, TError, TRequirements, TSensitiveError, TSensitiveRequirements>(
18
19
  startup: Layer.Layer<TServices, TError, TRequirements> | undefined,
19
- sensitive: Effect.Effect<(key: string) => boolean, TSensitiveError>,
20
+ sensitive: Effect.Effect<(key: string) => boolean, TSensitiveError, TSensitiveRequirements>,
20
21
  ) {
21
22
  return Layer.mergeAll(
22
23
  Layer.empty,
@@ -1,41 +1,56 @@
1
1
  import { Effect, MutableRef } from "effect";
2
2
  import { type Rpc, RpcGroup } from "effect/rpc";
3
- import { schemaFieldKeys } from "@shivaedev/errors/schemaFieldScope.ts";
4
3
  import { schemaSensitiveKeys } from "@shivaedev/errors/schemaSensitiveKeys.ts";
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));
16
- const models = new Set<string>();
26
+ const current = MutableRef.make(yield* MutableRef.get(source).compile(MutableRef.get(source).configured));
27
+ const { fieldNames } = yield* HostModels;
17
28
  function sensitive(key: string) {
18
- return models.has(key) || MutableRef.get(current)(key);
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, ...models])).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
- return (key: string) => models.has(key) || forTag(key);
51
+ return (key: string) => fieldNames.has(key) || forTag(key);
29
52
  },
30
53
  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
54
  redactFailure: (tag: string | undefined, reason: "Fail" | "Die", value: unknown) => MutableRef.get(current).redactFailure(tag, reason, value),
40
55
  });
41
56
  });
@@ -1,6 +1,7 @@
1
1
  import { Effect, Option } from "effect";
2
2
  import { Headers, HttpMiddleware, HttpRouter, HttpServerRequest, HttpServerResponse } from "effect/http";
3
3
  import * as HttpStatus from "effect/http/HttpStatus";
4
+ import { accountPreferenceHeaders } from "@shivaedev/identity/accountPreferenceHeaders.ts";
4
5
  import { localTransport } from "@shivaedev/identity/server/adapters/local.ts";
5
6
  import { SessionPolicy } from "@shivaedev/identity/server/SessionPolicy.ts";
6
7
 
@@ -28,6 +29,8 @@ export function nativeClientOrigins(trustedProxy: string | undefined, route: "au
28
29
  ).layer;
29
30
  }
30
31
 
32
+ const CLIENT_HEADERS = ["Authorization", "Content-Type"];
33
+
31
34
  const withClientCors = Effect.fnUntraced(function* <E, R>(
32
35
  effect: Effect.Effect<HttpServerResponse.HttpServerResponse, E, R>,
33
36
  nativeOrigins: readonly string[],
@@ -43,7 +46,7 @@ const withClientCors = Effect.fnUntraced(function* <E, R>(
43
46
  }
44
47
  const transport = native ? request.modify({ headers: Headers.remove(request.headers, "cookie") }) : request;
45
48
  return yield* HttpMiddleware.cors({
46
- allowedHeaders: ["Authorization", "Content-Type"],
49
+ allowedHeaders: auth ? [...CLIENT_HEADERS, ...Object.values(accountPreferenceHeaders)] : CLIENT_HEADERS,
47
50
  allowedMethods: auth ? ["GET", "POST"] : ["POST"],
48
51
  allowedOrigins: [origin],
49
52
  credentials: credentialed,
@@ -1,6 +1,5 @@
1
- import { Effect, Layer, Schema } from "effect";
1
+ import { Effect, Layer } from "effect";
2
2
  import type { Rpc, RpcGroup } from "effect/rpc";
3
- import { collectRepositoryModels, RepositoryModels } from "@shivaedev/sql/RepositoryModels.ts";
4
3
  import { redactingDiagnostics } from "#adapters/redactingDiagnostics.ts";
5
4
  import { hostSensitiveKeys } from "#hostSensitiveKeys.ts";
6
5
  import { otlpTracing } from "#otlpTracing.ts";
@@ -8,21 +7,7 @@ import { RedactionPolicy } from "#RedactionPolicy.ts";
8
7
 
9
8
  export type HostSensitivePolicy = Effect.Success<ReturnType<typeof hostSensitiveKeys>>;
10
9
 
11
- // Sql's collector keeps the owned models; the host also hands each model to the policy that masks its sensitive fields.
12
- function maskedModels(sensitive: HostSensitivePolicy) {
13
- return Layer.effect(
14
- RepositoryModels,
15
- Effect.gen(function* () {
16
- const collected = yield* RepositoryModels;
17
- return RepositoryModels.of({
18
- declare: (declared) => Effect.andThen(sensitive.models.declare(Schema.Struct(declared.model.fields)), collected.declare(declared)),
19
- owned: collected.owned,
20
- });
21
- }),
22
- ).pipe(Layer.provide(collectRepositoryModels));
23
- }
24
-
25
- // 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.
10
+ // Both hosts build their process inside one policy, so every log, error report and exported span masks the same names, including the fields their repositories' models declare.
26
11
  export function redactedHost<TRpcs extends Rpc.Any, A, E, R>(
27
12
  options: { readonly group?: RpcGroup.RpcGroup<TRpcs>; readonly config?: { readonly sensitiveKeys?: readonly string[] } },
28
13
  host: (sensitive: HostSensitivePolicy) => Layer.Layer<A, E, R>,
@@ -33,7 +18,6 @@ export function redactedHost<TRpcs extends Rpc.Any, A, E, R>(
33
18
  Layer.provide(redactingDiagnostics(sensitive)),
34
19
  Layer.provide(otlpTracing),
35
20
  Layer.provide(Layer.succeed(RedactionPolicy, sensitive)),
36
- Layer.provide(maskedModels(sensitive)),
37
21
  ),
38
22
  ),
39
23
  );
package/src/workerHost.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { Effect, Layer } from "effect";
2
2
  import { type AppDatabaseOptions, appDatabase, type DatabaseConfig } from "#appDatabase.ts";
3
+ import { hostModels } from "#HostModels.ts";
3
4
  import { hostCrypto } from "#hostCrypto.ts";
4
5
  import { redactedHost } from "#redactedHost.ts";
5
6
 
@@ -22,5 +23,5 @@ export function workerHost<R, E, TRequirements, TServices, TServiceError, TServi
22
23
  Layer.provideMerge(options.services),
23
24
  Layer.provideMerge(appDatabase(options)),
24
25
  ),
25
- ).pipe(Layer.provide(hostCrypto));
26
+ ).pipe(Layer.provide(hostCrypto), Layer.provide(hostModels()));
26
27
  }