@shivaedev/server 0.20.2 → 0.23.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 (101) hide show
  1. package/CHANGELOG.md +36 -0
  2. package/README.md +50 -5
  3. package/dist/AppHostFactory.d.ts +2 -2
  4. package/dist/AppHostFactory.d.ts.map +1 -1
  5. package/dist/UnixSocketRequest.d.ts +3 -0
  6. package/dist/UnixSocketRequest.d.ts.map +1 -0
  7. package/dist/UnixSocketRequest.js +3 -0
  8. package/dist/UnixSocketRequest.js.map +1 -0
  9. package/dist/adapters/hostHttpServices.d.ts +3 -0
  10. package/dist/adapters/hostHttpServices.d.ts.map +1 -0
  11. package/dist/adapters/hostHttpServices.js +6 -0
  12. package/dist/adapters/hostHttpServices.js.map +1 -0
  13. package/dist/adapters/nodeHttpServer.d.ts +3 -1
  14. package/dist/adapters/nodeHttpServer.d.ts.map +1 -1
  15. package/dist/adapters/nodeHttpServer.js +36 -7
  16. package/dist/adapters/nodeHttpServer.js.map +1 -1
  17. package/dist/adapters/socketState.d.ts +3 -0
  18. package/dist/adapters/socketState.d.ts.map +1 -0
  19. package/dist/adapters/socketState.js +24 -0
  20. package/dist/adapters/socketState.js.map +1 -0
  21. package/dist/adapters/unixHttpServer.d.ts +11 -0
  22. package/dist/adapters/unixHttpServer.d.ts.map +1 -0
  23. package/dist/adapters/unixHttpServer.js +56 -0
  24. package/dist/adapters/unixHttpServer.js.map +1 -0
  25. package/dist/appDatabase.js +1 -1
  26. package/dist/appDatabase.js.map +1 -1
  27. package/dist/appHost.d.ts +3 -6
  28. package/dist/appHost.d.ts.map +1 -1
  29. package/dist/appHost.js +2 -2
  30. package/dist/appHost.js.map +1 -1
  31. package/dist/appHostHttp.d.ts +345 -111
  32. package/dist/appHostHttp.d.ts.map +1 -1
  33. package/dist/appHostHttp.js +7 -7
  34. package/dist/appHostHttp.js.map +1 -1
  35. package/dist/hostOptions.d.ts +12 -2
  36. package/dist/hostOptions.d.ts.map +1 -1
  37. package/dist/hostRpc.d.ts +21 -1
  38. package/dist/hostRpc.d.ts.map +1 -1
  39. package/dist/hostRpc.js +2 -1
  40. package/dist/hostRpc.js.map +1 -1
  41. package/dist/hostRpcRoutes.d.ts +36 -7
  42. package/dist/hostRpcRoutes.d.ts.map +1 -1
  43. package/dist/hostRpcRoutes.js +11 -6
  44. package/dist/hostRpcRoutes.js.map +1 -1
  45. package/dist/hostSensitiveKeys.d.ts +12 -0
  46. package/dist/hostSensitiveKeys.d.ts.map +1 -0
  47. package/dist/hostSensitiveKeys.js +8 -0
  48. package/dist/hostSensitiveKeys.js.map +1 -0
  49. package/dist/hostTransport.d.ts +2 -0
  50. package/dist/hostTransport.d.ts.map +1 -1
  51. package/dist/hostTransport.js +2 -1
  52. package/dist/hostTransport.js.map +1 -1
  53. package/dist/hostVersionLayer.d.ts +2 -0
  54. package/dist/hostVersionLayer.d.ts.map +1 -0
  55. package/dist/hostVersionLayer.js +8 -0
  56. package/dist/hostVersionLayer.js.map +1 -0
  57. package/dist/socketRpc.d.ts.map +1 -1
  58. package/dist/socketRpc.js +2 -1
  59. package/dist/socketRpc.js.map +1 -1
  60. package/dist/testing/adapters/hostProcess.d.ts +1 -0
  61. package/dist/testing/adapters/hostProcess.d.ts.map +1 -1
  62. package/dist/testing/adapters/hostProcess.js +27 -0
  63. package/dist/testing/adapters/hostProcess.js.map +1 -1
  64. package/dist/testing/hostProcess.d.ts +4 -2
  65. package/dist/testing/hostProcess.d.ts.map +1 -1
  66. package/dist/testing/hostProcess.js +9 -2
  67. package/dist/testing/hostProcess.js.map +1 -1
  68. package/dist/testing/hostProcessChild.d.ts +1 -0
  69. package/dist/testing/hostProcessChild.d.ts.map +1 -1
  70. package/dist/testing/hostProcessChild.js +5 -1
  71. package/dist/testing/hostProcessChild.js.map +1 -1
  72. package/dist/testing/hostProcessProtocol.d.ts +3 -0
  73. package/dist/testing/hostProcessProtocol.d.ts.map +1 -1
  74. package/dist/testing/hostProcessProtocol.js +1 -0
  75. package/dist/testing/hostProcessProtocol.js.map +1 -1
  76. package/dist/testing/hostTime.d.ts +9 -0
  77. package/dist/testing/hostTime.d.ts.map +1 -0
  78. package/dist/testing/hostTime.js +27 -0
  79. package/dist/testing/hostTime.js.map +1 -0
  80. package/package.json +15 -15
  81. package/src/AppHostFactory.ts +8 -8
  82. package/src/UnixSocketRequest.ts +3 -0
  83. package/src/adapters/hostHttpServices.ts +10 -0
  84. package/src/adapters/nodeHttpServer.ts +40 -7
  85. package/src/adapters/socketState.ts +22 -0
  86. package/src/adapters/unixHttpServer.ts +74 -0
  87. package/src/appDatabase.ts +1 -1
  88. package/src/appHost.ts +21 -23
  89. package/src/appHostHttp.ts +7 -7
  90. package/src/hostOptions.ts +11 -6
  91. package/src/hostRpc.ts +6 -1
  92. package/src/hostRpcRoutes.ts +14 -5
  93. package/src/hostSensitiveKeys.ts +11 -0
  94. package/src/hostTransport.ts +9 -2
  95. package/src/hostVersionLayer.ts +8 -0
  96. package/src/socketRpc.ts +2 -1
  97. package/src/testing/adapters/hostProcess.ts +27 -0
  98. package/src/testing/hostProcess.ts +12 -3
  99. package/src/testing/hostProcessChild.ts +6 -1
  100. package/src/testing/hostProcessProtocol.ts +1 -0
  101. package/src/testing/hostTime.ts +33 -0
package/src/hostRpc.ts CHANGED
@@ -3,6 +3,7 @@ import { type HttpEffect, HttpServerResponse } from "effect/http";
3
3
  import { type Rpc, type RpcGroup, RpcServer } from "effect/rpc";
4
4
  import { CommandGuard, commandGuardLayer, guardCommands } from "@shivaedev/contract/CommandGuard.ts";
5
5
  import { ReadPolicyGuard, readPolicyGuardLayer } from "@shivaedev/contract/ReadPolicyGuard.ts";
6
+ import { subjectLayers } from "@shivaedev/contract/subjectLayers.ts";
6
7
  import { publishCommand } from "#publishCommand.ts";
7
8
  import { tracedGroup } from "#tracedGroup.ts";
8
9
 
@@ -15,5 +16,9 @@ export const hostRpc = Effect.fnUntraced(function* <R extends Rpc.Any>(group: Rp
15
16
  const readPolicy = yield* ReadPolicyGuard.pipe(Effect.provide(readPolicyGuardLayer({ websocket })));
16
17
  const publication = yield* CommandGuard.pipe(Effect.provide(commandGuardLayer(publishCommand)));
17
18
  const handler = websocket ? RpcServer.toHttpEffectWebsocket(traced) : RpcServer.toHttpEffect(traced);
18
- return yield* handler.pipe(Effect.provideService(ReadPolicyGuard, readPolicy), Effect.provideService(CommandGuard, publication));
19
+ return yield* handler.pipe(
20
+ Effect.provideService(ReadPolicyGuard, readPolicy),
21
+ Effect.provideService(CommandGuard, publication),
22
+ Effect.provide(subjectLayers(group)),
23
+ );
19
24
  });
@@ -2,25 +2,30 @@ import { ByteSize, Effect, Layer } from "effect";
2
2
  import { HttpEffect, HttpIncomingMessage, HttpRouter, HttpServerRequest, HttpServerResponse } from "effect/http";
3
3
  import * as HttpStatus from "effect/http/HttpStatus";
4
4
  import { type Rpc, type RpcGroup, RpcSerialization } from "effect/rpc";
5
+ import { HostVersion } from "@shivaedev/contract/HostVersion.ts";
5
6
  import { validateReadPolicies } from "@shivaedev/contract/validateReadPolicies.ts";
6
7
  import type { schemaSensitiveKeys } from "@shivaedev/errors/schemaSensitiveKeys.ts";
7
8
  import { localRpcGuard } from "@shivaedev/identity/server/adapters/local.ts";
8
9
  import type { SessionPolicy } from "@shivaedev/identity/server/SessionPolicy.ts";
9
10
  import { authenticatedLayer, maybeAuthenticatedLayer } from "@shivaedev/identity/server/session.ts";
11
+ import { subjectResolverLayer } from "@shivaedev/identity/server/subjectResolverLayer.ts";
10
12
  import { boundedRpcRequest } from "#boundedRpcRequest.ts";
11
13
  import { boundedRpcSocket } from "#boundedRpcSocket.ts";
12
14
  import { hostChanges } from "#hostChanges.ts";
13
15
  import type { HostPolicy } from "#hostOptions.ts";
14
16
  import { hostRpc, rpcCacheControl } from "#hostRpc.ts";
15
17
  import type { hostTransport } from "#hostTransport.ts";
18
+ import { hostVersionLayer } from "#hostVersionLayer.ts";
16
19
  import { redactingRpcSerialization } from "#redactingRpcSerialization.ts";
17
20
  import { rpcRequestTracing } from "#rpcRequestTracing.ts";
18
21
  import { socketRpc } from "#socketRpc.ts";
22
+ import { UnixSocketRequest } from "#UnixSocketRequest.ts";
19
23
 
20
24
  type BuiltRoutes<TRpcs extends Rpc.Any, THandlers, TError, TRequirements> = ReturnType<
21
25
  typeof buildRpcRoutes<TRpcs, THandlers, TError, TRequirements>
22
26
  >;
23
27
  type RouteOptions<TRpcs extends Rpc.Any, THandlers, TError, TRequirements> = {
28
+ readonly config?: { readonly buildVersion?: string };
24
29
  readonly group: RpcGroup.RpcGroup<TRpcs>;
25
30
  readonly handlers: Layer.Layer<THandlers, TError, TRequirements>;
26
31
  readonly rpcPath?: HttpRouter.PathInput;
@@ -51,17 +56,20 @@ function buildRpcRoutes<TRpcs extends Rpc.Any, THandlers, TError, TRequirements>
51
56
  { bodySize, trustedProxy, nativeOrigins }: Effect.Success<ReturnType<typeof hostTransport>>,
52
57
  sensitive: Effect.Success<ReturnType<typeof schemaSensitiveKeys>>,
53
58
  ) {
54
- const middleware =
59
+ const middleware = Layer.merge(
60
+ subjectResolverLayer(),
55
61
  options.policy === "loopback"
56
62
  ? rpcRequestTracing(sensitive)
57
- : Layer.mergeAll(authenticatedLayer(), maybeAuthenticatedLayer(), rpcRequestTracing(sensitive));
63
+ : Layer.mergeAll(authenticatedLayer(), maybeAuthenticatedLayer(), rpcRequestTracing(sensitive)),
64
+ );
58
65
  return HttpRouter.use((router) =>
59
66
  Effect.gen(function* () {
60
67
  yield* validateReadPolicies(options.group);
61
68
  const wire = redactingRpcSerialization(yield* RpcSerialization.RpcSerialization, sensitive);
62
- const handler = yield* hostRpc(options.group).pipe(Effect.provideService(RpcSerialization.RpcSerialization, wire));
63
- const guarded = localRpcGuard(handler, trustedProxy, nativeOrigins);
64
- const websocket = yield* hostRpc(options.group, true).pipe(Effect.provideService(RpcSerialization.RpcSerialization, wire));
69
+ const group = options.group.merge(HostVersion.application);
70
+ const handler = yield* hostRpc(group).pipe(Effect.provideService(RpcSerialization.RpcSerialization, wire));
71
+ const guarded = Effect.flatMap(UnixSocketRequest, (unix) => (unix ? handler : localRpcGuard(handler, trustedProxy, nativeOrigins)));
72
+ const websocket = yield* hostRpc(group, true).pipe(Effect.provideService(RpcSerialization.RpcSerialization, wire));
65
73
  yield* router.add(
66
74
  "OPTIONS",
67
75
  options.rpcPath ?? "/rpc",
@@ -94,6 +102,7 @@ function buildRpcRoutes<TRpcs extends Rpc.Any, THandlers, TError, TRequirements>
94
102
  }),
95
103
  ).pipe(
96
104
  Layer.provide(Layer.merge(middleware, hostChanges(options.group).pipe(Layer.provideMerge(options.handlers)))),
105
+ Layer.provide(hostVersionLayer(options.config?.buildVersion)),
97
106
  Layer.provide(RpcSerialization.layerNdjson),
98
107
  );
99
108
  }
@@ -0,0 +1,11 @@
1
+ import { type Rpc, RpcGroup } from "effect/rpc";
2
+ import { schemaSensitiveKeys } from "@shivaedev/errors/schemaSensitiveKeys.ts";
3
+
4
+ export function hostSensitiveKeys<TRpcs extends Rpc.Any>(options: {
5
+ readonly group?: RpcGroup.RpcGroup<TRpcs>;
6
+ readonly config?: { readonly sensitiveKeys?: readonly string[] };
7
+ }) {
8
+ return options.group === undefined
9
+ ? schemaSensitiveKeys(RpcGroup.make(), options.config?.sensitiveKeys)
10
+ : schemaSensitiveKeys(options.group, options.config?.sensitiveKeys);
11
+ }
@@ -5,7 +5,13 @@ import { NativeOrigins } from "#nativeClientOrigins.ts";
5
5
  import { TrustedProxyRequired } from "#TrustedProxyRequired.ts";
6
6
 
7
7
  interface Options {
8
- readonly config?: { readonly host?: string; readonly maxBodySize?: number; readonly trustedProxy?: string };
8
+ readonly config?: {
9
+ readonly host?: string;
10
+ readonly port?: number;
11
+ readonly socketPath?: string;
12
+ readonly maxBodySize?: number;
13
+ readonly trustedProxy?: string;
14
+ };
9
15
  readonly nativeOrigins?: readonly string[];
10
16
  readonly policy?: "loopback" | undefined;
11
17
  }
@@ -21,6 +27,7 @@ export const hostTransport = Effect.fn("Platform.hostTransport")(function* (opti
21
27
  if (trustedProxy !== undefined) {
22
28
  yield* Schema.decodeUnknownEffect(TrustedProxy)(trustedProxy).pipe(Effect.mapError((cause) => new Config.ConfigError(cause)));
23
29
  }
30
+ const socketOnly = options.config?.socketPath !== undefined && options.config.port === undefined;
24
31
  const host = options.config?.host ?? (yield* Config.String("HOST").pipe(Config.withDefault("127.0.0.1")));
25
32
  const loopback =
26
33
  host.toLowerCase() === "localhost"
@@ -28,7 +35,7 @@ export const hostTransport = Effect.fn("Platform.hostTransport")(function* (opti
28
35
  onFailure: () => false,
29
36
  onSuccess: (address) => NetAddress.isLoopback(NetAddress.toCanonical(address)),
30
37
  });
31
- if (options.policy !== "loopback" && !loopback && trustedProxy === undefined) {
38
+ if (!socketOnly && options.policy !== "loopback" && !loopback && trustedProxy === undefined) {
32
39
  return yield* Effect.fail(new TrustedProxyRequired());
33
40
  }
34
41
  const nativeOrigins = yield* Schema.decodeUnknownEffect(NativeOrigins)(options.nativeOrigins ?? []).pipe(
@@ -0,0 +1,8 @@
1
+ import { Stream } from "effect";
2
+ import { HostVersion } from "@shivaedev/contract/HostVersion.ts";
3
+
4
+ export function hostVersionLayer(version: string | undefined) {
5
+ return HostVersion.application.toLayer({
6
+ "platform.hostVersion.watch": () => (version === undefined ? Stream.never : Stream.concat(Stream.succeed(version), Stream.never)),
7
+ });
8
+ }
package/src/socketRpc.ts CHANGED
@@ -8,6 +8,7 @@ import { resolveSession } from "@shivaedev/identity/server/session.ts";
8
8
  import { makeSocketSession, SocketSession } from "@shivaedev/identity/server/socketSession.ts";
9
9
  import { bearerSocketRpc } from "#bearerSocketRpc.ts";
10
10
  import type { HostPolicy } from "#hostOptions.ts";
11
+ import { UnixSocketRequest } from "#UnixSocketRequest.ts";
11
12
 
12
13
  export function socketRpc(
13
14
  handler: Effect.Effect<HttpServerResponse.HttpServerResponse, never, HttpServerRequest.HttpServerRequest | Scope.Scope>,
@@ -29,7 +30,7 @@ export function socketRpc(
29
30
  ) {
30
31
  return Effect.gen(function* () {
31
32
  const request = yield* HttpServerRequest.HttpServerRequest;
32
- if (!localTransport(trustedProxy, nativeOrigins)(request)) {
33
+ if (!((yield* UnixSocketRequest) || localTransport(trustedProxy, nativeOrigins)(request))) {
33
34
  return HttpServerResponse.text("Local same-origin requests only", { status: HttpStatus.fromLiteral("Forbidden") });
34
35
  }
35
36
  if (policy.policy !== "loopback") {
@@ -89,6 +89,33 @@ export function sendHostReady(url: string) {
89
89
  return sendHostMessage({ pid: process.pid, type: "host-ready", url });
90
90
  }
91
91
 
92
+ export function waitForHostStart(signal: AbortSignal) {
93
+ return new Promise<void>((resolve, reject) => {
94
+ function cleanup() {
95
+ process.off("SIGUSR2", start);
96
+ signal.removeEventListener("abort", aborted);
97
+ }
98
+ function failed(cause: unknown) {
99
+ cleanup();
100
+ reject(cause);
101
+ }
102
+ function start() {
103
+ cleanup();
104
+ resolve();
105
+ }
106
+ function aborted() {
107
+ failed(signal.reason);
108
+ }
109
+ process.once("SIGUSR2", start);
110
+ signal.addEventListener("abort", aborted, { once: true });
111
+ if (signal.aborted) {
112
+ aborted();
113
+ } else {
114
+ void sendHostMessage({ type: "host-prepared" }).catch(failed);
115
+ }
116
+ });
117
+ }
118
+
92
119
  export async function hostResponse(url: string, path: string, signal: AbortSignal) {
93
120
  const response = await fetch(new URL(path, url), { redirect: "manual", signal });
94
121
  return { body: await response.text(), headers: Object.fromEntries(response.headers), status: response.status };
@@ -2,9 +2,11 @@ import { Effect, Redacted, Schema } from "effect";
2
2
  import { forkHost, type HostProcessOptions, hostResponse, receiveHostMessage, stopHost } from "#testing/adapters/hostProcess.ts";
3
3
  import { collectHostReadouts } from "#testing/adapters/hostProcessReadouts.ts";
4
4
  import { HostProcessError } from "./HostProcessError.ts";
5
- import { HostReady, HostSignal } from "./hostProcessProtocol.ts";
5
+ import { HostPrepared, HostReady, HostSignal } from "./hostProcessProtocol.ts";
6
6
 
7
- export const hostProcess = Effect.fn("HostProcess.hostProcess")(function* (options: HostProcessOptions) {
7
+ export const hostProcess = Effect.fn("HostProcess.hostProcess")(function* <E = never, R = never>(
8
+ options: HostProcessOptions & { readonly beforeStart?: Effect.Effect<void, E, R> },
9
+ ) {
8
10
  const databaseUrl = options.databaseUrl;
9
11
  const database =
10
12
  databaseUrl === undefined
@@ -17,9 +19,16 @@ export const hostProcess = Effect.fn("HostProcess.hostProcess")(function* (optio
17
19
  (acquired) => Effect.promise(() => stopHost(acquired)),
18
20
  );
19
21
  const read = collectHostReadouts(child);
22
+ if (options.beforeStart !== undefined) {
23
+ yield* Effect.tryPromise({
24
+ catch: (cause) => new HostProcessError({ cause, operation: "launch" }),
25
+ try: (signal) => receiveHostMessage(child, HostPrepared, signal),
26
+ });
27
+ yield* options.beforeStart;
28
+ }
20
29
  const { pid, url } = yield* Effect.tryPromise({
21
30
  catch: (cause) => new HostProcessError({ cause, operation: "launch" }),
22
- try: (signal) => receiveHostMessage(child, HostReady, signal),
31
+ try: (signal) => receiveHostMessage(child, HostReady, signal, options.beforeStart === undefined ? undefined : () => child.kill("SIGUSR2")),
23
32
  });
24
33
  function response(path: string) {
25
34
  return Effect.tryPromise({
@@ -1,8 +1,13 @@
1
1
  import { Effect } from "effect";
2
2
  import { HttpServer } from "effect/http";
3
- import { sendHostMessage, sendHostReady } from "#testing/adapters/hostProcess.ts";
3
+ import { sendHostMessage, sendHostReady, waitForHostStart } from "#testing/adapters/hostProcess.ts";
4
4
  import { HostProcessError } from "#testing/HostProcessError.ts";
5
5
 
6
+ export const awaitHostStart = Effect.tryPromise({
7
+ catch: (cause) => new HostProcessError({ cause, operation: "launch" }),
8
+ try: waitForHostStart,
9
+ });
10
+
6
11
  export const reportHostReady = HttpServer.HttpServer.pipe(
7
12
  Effect.flatMap((server) =>
8
13
  Effect.tryPromise({
@@ -1,6 +1,7 @@
1
1
  import { Schema } from "effect";
2
2
 
3
3
  export const HostReady = Schema.Struct({ pid: Schema.Number, type: Schema.Literal("host-ready"), url: Schema.String });
4
+ export const HostPrepared = Schema.Struct({ type: Schema.Literal("host-prepared") });
4
5
  export const HostSignal = Schema.Struct({ signal: Schema.String, type: Schema.Literal("host-signal") });
5
6
  export const HostReadouts = Schema.Struct({
6
7
  activeWork: Schema.Array(Schema.String),
@@ -0,0 +1,33 @@
1
+ import { type Duration, Effect, Layer } from "effect";
2
+ import { TestClock } from "effect/testing";
3
+ import { WorkerReleaseTesting } from "@shivaedev/sql/testing/WorkerRelease.ts";
4
+
5
+ export const hostTime = Effect.fn("HostTime.hostTime")(function* () {
6
+ const clock = yield* TestClock.testClockWith(Effect.succeed);
7
+ const ticks = new Set<Parameters<typeof WorkerReleaseTesting.Service.register>[0]>();
8
+ return {
9
+ layer: Layer.succeed(WorkerReleaseTesting, {
10
+ clock,
11
+ register: (tick) =>
12
+ Effect.acquireRelease(
13
+ Effect.sync(() => {
14
+ ticks.add(tick);
15
+ }),
16
+ () =>
17
+ Effect.sync(() => {
18
+ ticks.delete(tick);
19
+ }),
20
+ ).pipe(Effect.asVoid),
21
+ }),
22
+ timePasses: (duration: Duration.Input, options?: { readonly poll?: boolean }) =>
23
+ Effect.gen(function* () {
24
+ yield* clock.adjust(duration);
25
+ for (const tick of ticks) {
26
+ yield* tick.heartbeat;
27
+ if (options?.poll !== false) {
28
+ yield* tick.poll;
29
+ }
30
+ }
31
+ }),
32
+ };
33
+ });