@shivaedev/server 0.20.2 → 0.22.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 (83) hide show
  1. package/CHANGELOG.md +25 -0
  2. package/README.md +15 -5
  3. package/dist/UnixSocketRequest.d.ts +3 -0
  4. package/dist/UnixSocketRequest.d.ts.map +1 -0
  5. package/dist/UnixSocketRequest.js +3 -0
  6. package/dist/UnixSocketRequest.js.map +1 -0
  7. package/dist/adapters/hostHttpServices.d.ts +3 -0
  8. package/dist/adapters/hostHttpServices.d.ts.map +1 -0
  9. package/dist/adapters/hostHttpServices.js +6 -0
  10. package/dist/adapters/hostHttpServices.js.map +1 -0
  11. package/dist/adapters/nodeHttpServer.d.ts +3 -1
  12. package/dist/adapters/nodeHttpServer.d.ts.map +1 -1
  13. package/dist/adapters/nodeHttpServer.js +36 -7
  14. package/dist/adapters/nodeHttpServer.js.map +1 -1
  15. package/dist/adapters/socketState.d.ts +3 -0
  16. package/dist/adapters/socketState.d.ts.map +1 -0
  17. package/dist/adapters/socketState.js +24 -0
  18. package/dist/adapters/socketState.js.map +1 -0
  19. package/dist/adapters/unixHttpServer.d.ts +11 -0
  20. package/dist/adapters/unixHttpServer.d.ts.map +1 -0
  21. package/dist/adapters/unixHttpServer.js +56 -0
  22. package/dist/adapters/unixHttpServer.js.map +1 -0
  23. package/dist/appDatabase.js +1 -1
  24. package/dist/appDatabase.js.map +1 -1
  25. package/dist/appHostHttp.d.ts +225 -45
  26. package/dist/appHostHttp.d.ts.map +1 -1
  27. package/dist/appHostHttp.js +2 -2
  28. package/dist/appHostHttp.js.map +1 -1
  29. package/dist/hostOptions.d.ts +1 -0
  30. package/dist/hostOptions.d.ts.map +1 -1
  31. package/dist/hostRpc.d.ts +21 -1
  32. package/dist/hostRpc.d.ts.map +1 -1
  33. package/dist/hostRpc.js +2 -1
  34. package/dist/hostRpc.js.map +1 -1
  35. package/dist/hostRpcRoutes.d.ts +25 -5
  36. package/dist/hostRpcRoutes.d.ts.map +1 -1
  37. package/dist/hostRpcRoutes.js +5 -3
  38. package/dist/hostRpcRoutes.js.map +1 -1
  39. package/dist/hostTransport.d.ts +2 -0
  40. package/dist/hostTransport.d.ts.map +1 -1
  41. package/dist/hostTransport.js +2 -1
  42. package/dist/hostTransport.js.map +1 -1
  43. package/dist/socketRpc.d.ts.map +1 -1
  44. package/dist/socketRpc.js +2 -1
  45. package/dist/socketRpc.js.map +1 -1
  46. package/dist/testing/adapters/hostProcess.d.ts +1 -0
  47. package/dist/testing/adapters/hostProcess.d.ts.map +1 -1
  48. package/dist/testing/adapters/hostProcess.js +27 -0
  49. package/dist/testing/adapters/hostProcess.js.map +1 -1
  50. package/dist/testing/hostProcess.d.ts +4 -2
  51. package/dist/testing/hostProcess.d.ts.map +1 -1
  52. package/dist/testing/hostProcess.js +9 -2
  53. package/dist/testing/hostProcess.js.map +1 -1
  54. package/dist/testing/hostProcessChild.d.ts +1 -0
  55. package/dist/testing/hostProcessChild.d.ts.map +1 -1
  56. package/dist/testing/hostProcessChild.js +5 -1
  57. package/dist/testing/hostProcessChild.js.map +1 -1
  58. package/dist/testing/hostProcessProtocol.d.ts +3 -0
  59. package/dist/testing/hostProcessProtocol.d.ts.map +1 -1
  60. package/dist/testing/hostProcessProtocol.js +1 -0
  61. package/dist/testing/hostProcessProtocol.js.map +1 -1
  62. package/dist/testing/hostTime.d.ts +9 -0
  63. package/dist/testing/hostTime.d.ts.map +1 -0
  64. package/dist/testing/hostTime.js +27 -0
  65. package/dist/testing/hostTime.js.map +1 -0
  66. package/package.json +15 -15
  67. package/src/UnixSocketRequest.ts +3 -0
  68. package/src/adapters/hostHttpServices.ts +10 -0
  69. package/src/adapters/nodeHttpServer.ts +40 -7
  70. package/src/adapters/socketState.ts +22 -0
  71. package/src/adapters/unixHttpServer.ts +74 -0
  72. package/src/appDatabase.ts +1 -1
  73. package/src/appHostHttp.ts +2 -2
  74. package/src/hostOptions.ts +1 -0
  75. package/src/hostRpc.ts +6 -1
  76. package/src/hostRpcRoutes.ts +7 -3
  77. package/src/hostTransport.ts +9 -2
  78. package/src/socketRpc.ts +2 -1
  79. package/src/testing/adapters/hostProcess.ts +27 -0
  80. package/src/testing/hostProcess.ts +12 -3
  81. package/src/testing/hostProcessChild.ts +6 -1
  82. package/src/testing/hostProcessProtocol.ts +1 -0
  83. package/src/testing/hostTime.ts +33 -0
@@ -0,0 +1,27 @@
1
+ import { Effect, Layer } from "effect";
2
+ import { TestClock } from "effect/testing";
3
+ import { WorkerReleaseTesting } from "@shivaedev/sql/testing/WorkerRelease.ts";
4
+ export const hostTime = Effect.fn("HostTime.hostTime")(function* () {
5
+ const clock = yield* TestClock.testClockWith(Effect.succeed);
6
+ const ticks = new Set();
7
+ return {
8
+ layer: Layer.succeed(WorkerReleaseTesting, {
9
+ clock,
10
+ register: (tick) => Effect.acquireRelease(Effect.sync(() => {
11
+ ticks.add(tick);
12
+ }), () => Effect.sync(() => {
13
+ ticks.delete(tick);
14
+ })).pipe(Effect.asVoid),
15
+ }),
16
+ timePasses: (duration, options) => Effect.gen(function* () {
17
+ yield* clock.adjust(duration);
18
+ for (const tick of ticks) {
19
+ yield* tick.heartbeat;
20
+ if (options?.poll !== false) {
21
+ yield* tick.poll;
22
+ }
23
+ }
24
+ }),
25
+ };
26
+ });
27
+ //# sourceMappingURL=hostTime.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hostTime.js","sourceRoot":"","sources":["../../src/testing/hostTime.ts"],"names":[],"mappings":"AAAA,OAAO,EAAiB,MAAM,EAAE,KAAK,EAAE,MAAM,QAAQ,CAAC;AACtD,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAC3C,OAAO,EAAE,oBAAoB,EAAE,MAAM,yCAAyC,CAAC;AAE/E,MAAM,CAAC,MAAM,QAAQ,GAAG,MAAM,CAAC,EAAE,CAAC,mBAAmB,CAAC,CAAC,QAAQ,CAAC;IAC/D,MAAM,KAAK,GAAG,KAAK,CAAC,CAAC,SAAS,CAAC,aAAa,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;IAC7D,MAAM,KAAK,GAAG,IAAI,GAAG,EAA+D,CAAC;IACrF,OAAO;QACN,KAAK,EAAE,KAAK,CAAC,OAAO,CAAC,oBAAoB,EAAE;YAC1C,KAAK;YACL,QAAQ,EAAE,CAAC,IAAI,EAAE,EAAE,CAClB,MAAM,CAAC,cAAc,CACpB,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE;gBAChB,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;YACjB,CAAC,CAAC,EACF,GAAG,EAAE,CACJ,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE;gBAChB,KAAK,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;YACpB,CAAC,CAAC,CACH,CAAC,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC;SACtB,CAAC;QACF,UAAU,EAAE,CAAC,QAAwB,EAAE,OAAqC,EAAE,EAAE,CAC/E,MAAM,CAAC,GAAG,CAAC,QAAQ,CAAC;YACnB,KAAK,CAAC,CAAC,KAAK,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;YAC9B,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;gBAC1B,KAAK,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC;gBACtB,IAAI,OAAO,EAAE,IAAI,KAAK,KAAK,EAAE,CAAC;oBAC7B,KAAK,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC;gBAClB,CAAC;YACF,CAAC;QACF,CAAC,CAAC;KACH,CAAC;AACH,CAAC,CAAC,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shivaedev/server",
3
- "version": "0.20.2",
3
+ "version": "0.22.0",
4
4
  "description": "The Effect application host: HTTP and RPC serving, health, OTLP export, database and migrations, and development mode",
5
5
  "keywords": [
6
6
  "effect"
@@ -46,25 +46,25 @@
46
46
  ],
47
47
  "dependencies": {
48
48
  "@shivaedev/changes": "0.9.0",
49
- "@shivaedev/contract": "0.33.0",
50
- "@shivaedev/errors": "0.7.0",
49
+ "@shivaedev/contract": "0.34.0",
50
+ "@shivaedev/errors": "0.8.0",
51
51
  "@shivaedev/http": "0.5.0",
52
- "@shivaedev/identity": "0.22.0",
52
+ "@shivaedev/identity": "0.23.0",
53
53
  "@shivaedev/local-postgres": "0.12.0",
54
- "@shivaedev/sql": "0.35.0"
54
+ "@shivaedev/sql": "0.36.1"
55
55
  },
56
56
  "devDependencies": {
57
57
  "@effect/platform-node": "4.0.6",
58
58
  "@effect/sql-pg": "4.0.6",
59
59
  "@effect/vitest": "4.0.6",
60
- "@shivaedev/access": "0.4.1",
61
- "@shivaedev/admin": "0.19.2",
62
- "@shivaedev/jobs": "0.32.0",
63
- "@shivaedev/quality": "0.57.1",
64
- "@shivaedev/rate-limit": "0.3.1",
65
- "@shivaedev/test-kits": "0.1.1",
66
- "@shivaedev/test-sql": "0.18.0",
67
- "@shivaedev/test-story": "0.36.0",
60
+ "@shivaedev/access": "0.4.2",
61
+ "@shivaedev/admin": "0.19.4",
62
+ "@shivaedev/jobs": "0.34.0",
63
+ "@shivaedev/quality": "0.58.0",
64
+ "@shivaedev/rate-limit": "0.3.3",
65
+ "@shivaedev/test-kits": "0.2.1",
66
+ "@shivaedev/test-sql": "0.18.2",
67
+ "@shivaedev/test-story": "0.37.0",
68
68
  "@types/node": "24.19.2",
69
69
  "better-auth": "1.7.14",
70
70
  "effect": "4.0.6",
@@ -76,8 +76,8 @@
76
76
  "@effect/platform-node": "4.0.6",
77
77
  "@effect/sql-pg": "4.0.6",
78
78
  "@effect/vitest": "4.0.6",
79
- "@shivaedev/admin": "0.19.2",
80
- "@shivaedev/test-sql": "0.18.0",
79
+ "@shivaedev/admin": "0.19.4",
80
+ "@shivaedev/test-sql": "0.18.2",
81
81
  "better-auth": "1.7.14",
82
82
  "effect": "4.0.6"
83
83
  },
@@ -0,0 +1,3 @@
1
+ import { Context } from "effect";
2
+
3
+ export const UnixSocketRequest = Context.Reference<boolean>("@shivaedev/server/UnixSocketRequest", { defaultValue: () => false });
@@ -0,0 +1,10 @@
1
+ import * as NodeHttpServer from "@effect/platform-node/NodeHttpServer";
2
+ import { Effect, FileSystem, Layer, Option } from "effect";
3
+
4
+ export const hostHttpServices = Layer.unwrap(
5
+ Effect.map(Effect.serviceOption(FileSystem.FileSystem), (fileSystem) =>
6
+ Option.isSome(fileSystem)
7
+ ? Layer.merge(NodeHttpServer.layerHttpServices, Layer.succeed(FileSystem.FileSystem, fileSystem.value))
8
+ : NodeHttpServer.layerHttpServices,
9
+ ),
10
+ );
@@ -1,28 +1,61 @@
1
1
  import { createServer } from "node:http";
2
2
  import * as NodeHttpServer from "@effect/platform-node/NodeHttpServer";
3
- import { Config, type Duration, Effect, Layer } from "effect";
3
+ import { Config, type Duration, Effect, Layer, Scope } from "effect";
4
+ import { HttpServer } from "effect/http";
5
+ import { hostHttpServices } from "#adapters/hostHttpServices.ts";
6
+ import { unixHttpServer } from "#adapters/unixHttpServer.ts";
4
7
 
5
8
  export function nodeHttpServer(
6
9
  host: string,
7
10
  config?: {
8
11
  readonly port?: number;
12
+ readonly socketPath?: string;
9
13
  readonly gracefulShutdownTimeout?: Duration.Input;
10
14
  readonly disablePreemptiveShutdown?: boolean;
11
15
  readonly maxBodySize?: number;
12
16
  },
13
17
  ) {
14
- return Layer.unwrap(
18
+ return Layer.effect(
19
+ HttpServer.HttpServer,
15
20
  Effect.gen(function* () {
16
- const port = config?.port ?? (yield* Config.Int("PORT").pipe(Config.withDefault(3000)));
17
- return NodeHttpServer.layer(createServer, {
21
+ const socketPath = config?.socketPath;
22
+ const options = {
18
23
  gracefulShutdownTimeout: config?.gracefulShutdownTimeout,
19
- host,
20
- port,
21
24
  websocket: config?.maxBodySize === undefined ? undefined : { maxPayload: socketPayloadLimit(config.maxBodySize) },
22
25
  ...(config?.disablePreemptiveShutdown === undefined ? {} : { disablePreemptiveShutdown: config.disablePreemptiveShutdown }),
26
+ };
27
+ if (socketPath === undefined) {
28
+ const port = config?.port ?? (yield* Config.Int("PORT").pipe(Config.withDefault(3000)));
29
+ return yield* NodeHttpServer.make(createServer, { ...options, host, port });
30
+ }
31
+ const listeners = yield* Scope.fork(yield* Scope.Scope, "parallel");
32
+ const socketScope = yield* Scope.fork(listeners, "sequential");
33
+ const unix = yield* unixHttpServer(socketPath, options).pipe(Scope.provide(socketScope));
34
+ // Socket preparation emits filesystem events; keep TCP closed until it finishes.
35
+ const tcpScope = yield* Scope.fork(listeners, "sequential");
36
+ const tcp =
37
+ config?.port === undefined
38
+ ? undefined
39
+ : yield* NodeHttpServer.make(createServer, {
40
+ ...options,
41
+ host,
42
+ port: config.port,
43
+ }).pipe(Scope.provide(tcpScope));
44
+ return HttpServer.make({
45
+ address: tcp?.address ?? unix.address,
46
+ serve: (handler, middleware) =>
47
+ Effect.gen(function* () {
48
+ const servingListeners = yield* Scope.fork(yield* Scope.Scope, "parallel");
49
+ const unixServing = yield* Scope.fork(servingListeners, "sequential");
50
+ yield* (middleware === undefined ? unix.serve(handler) : unix.serve(handler, middleware)).pipe(Scope.provide(unixServing));
51
+ if (tcp !== undefined) {
52
+ const serving = yield* Scope.fork(servingListeners, "sequential");
53
+ yield* (middleware === undefined ? tcp.serve(handler) : tcp.serve(handler, middleware)).pipe(Scope.provide(serving));
54
+ }
55
+ }),
23
56
  });
24
57
  }),
25
- );
58
+ ).pipe(Layer.provideMerge(hostHttpServices));
26
59
  }
27
60
 
28
61
  function socketPayloadLimit(bytes: number) {
@@ -0,0 +1,22 @@
1
+ import { createConnection } from "node:net";
2
+ import { Effect } from "effect";
3
+
4
+ export function socketState(path: string) {
5
+ return Effect.callback<"live" | "missing" | "stale", Error>((resume) => {
6
+ const socket = createConnection(path);
7
+ socket.once("connect", () => {
8
+ socket.destroy();
9
+ resume(Effect.succeed("live"));
10
+ });
11
+ socket.once("error", (cause: NodeJS.ErrnoException) => {
12
+ if (cause.code === "ENOENT") {
13
+ resume(Effect.succeed("missing"));
14
+ } else if (cause.code === "ECONNREFUSED") {
15
+ resume(Effect.succeed("stale"));
16
+ } else {
17
+ resume(Effect.fail(cause));
18
+ }
19
+ });
20
+ return Effect.sync(() => socket.destroy());
21
+ });
22
+ }
@@ -0,0 +1,74 @@
1
+ import { createServer } from "node:http";
2
+ import * as NodeHttpServer from "@effect/platform-node/NodeHttpServer";
3
+ import { Duration, Effect, FileSystem, Path, Scope } from "effect";
4
+ import { HttpServer } from "effect/http";
5
+ import * as NetAddress from "effect/net/NetAddress";
6
+ import { socketState } from "#adapters/socketState.ts";
7
+ import { UnixSocketRequest } from "#UnixSocketRequest.ts";
8
+
9
+ export const unixHttpServer = Effect.fn("Server.unixHttpServer")(function* (path: string, options: NodeHttpServer.Options) {
10
+ const fs = yield* FileSystem.FileSystem;
11
+ const paths = yield* Path.Path;
12
+ const root = paths.dirname(path);
13
+ const scope = yield* Scope.Scope;
14
+ function occupied() {
15
+ return new Error(`A listener is already running at ${path}. Stop it before starting another.`);
16
+ }
17
+ return yield* Effect.scoped(
18
+ Effect.gen(function* () {
19
+ const lock = paths.join(root, `.${paths.basename(path)}.starting`);
20
+ yield* Effect.acquireRelease(
21
+ fs.makeDirectory(lock, { mode: 0o700 }).pipe(
22
+ Effect.catchIf(
23
+ (error) => error.reason._tag === "AlreadyExists",
24
+ () =>
25
+ Effect.fail(new Error(`${occupied().message} A startup lock exists at ${lock}; remove it only if its startup was forcibly killed.`)),
26
+ ),
27
+ ),
28
+ () => fs.remove(lock, { recursive: true }).pipe(Effect.orDie),
29
+ );
30
+ const state = yield* socketState(path);
31
+ if (state === "live") {
32
+ return yield* Effect.fail(occupied());
33
+ }
34
+ if (state === "stale") {
35
+ if ((yield* fs.stat(path)).type !== "Socket") {
36
+ return yield* Effect.fail(new Error(`The Unix socket path is not a socket: ${path}`));
37
+ }
38
+ yield* fs.remove(path, { force: true });
39
+ }
40
+ // Short private names preserve owner-only access within the public pathname budget.
41
+ const staging = yield* fs.makeTempDirectoryScoped({ directory: root, prefix: ".s" });
42
+ const pending = paths.join(staging, "s");
43
+ const node = createServer();
44
+ const server = yield* NodeHttpServer.make(() => node, {
45
+ ...options,
46
+ path: pending,
47
+ }).pipe(Scope.provide(scope));
48
+ yield* fs.chmod(pending, 0o600);
49
+ // Unpublish before Node closes its private bind path.
50
+ yield* Effect.acquireRelease(fs.rename(pending, path), () => fs.remove(path, { force: true }).pipe(Effect.orDie)).pipe(Scope.provide(scope));
51
+ return withUnixRequests({ ...server, address: NetAddress.unixPathAddress(path) }, node, options);
52
+ }),
53
+ );
54
+ });
55
+
56
+ function withUnixRequests(server: HttpServer.HttpServer["Service"], node: ReturnType<typeof createServer>, options: NodeHttpServer.Options) {
57
+ return HttpServer.make({
58
+ address: server.address,
59
+ serve: (handler, middleware) =>
60
+ Effect.gen(function* () {
61
+ const unix = handler.pipe(Effect.provideService(UnixSocketRequest, true));
62
+ yield* middleware === undefined ? server.serve(unix) : server.serve(unix, middleware);
63
+ yield* Effect.addFinalizer(() =>
64
+ Effect.sync(() => {
65
+ const deadline = setTimeout(
66
+ () => node.closeAllConnections(),
67
+ Duration.toMillis(Duration.fromInputUnsafe(options.gracefulShutdownTimeout ?? "20 seconds")),
68
+ );
69
+ node.once("close", () => clearTimeout(deadline));
70
+ }),
71
+ );
72
+ }),
73
+ });
74
+ }
@@ -36,7 +36,7 @@ export function appDatabase<R, E = never>(options: AppDatabaseOptions<R, E>) {
36
36
  const migrations = options.migrations;
37
37
  const worker =
38
38
  migrations.mode === "wait" && migrations.release !== undefined
39
- ? Layer.effect(WorkerRelease, coordinateWorkerRelease(migrations.release, migrations.pollInterval, migrations.table))
39
+ ? Layer.effect(WorkerRelease, coordinateWorkerRelease(migrations.release, migrations.pollInterval, migrations.table)).pipe(Layer.provide(clock))
40
40
  : Layer.empty;
41
41
  const base = Layer.merge(pool, live);
42
42
  const changes = options.config?.changes;
@@ -1,8 +1,8 @@
1
- import * as NodeHttpServer from "@effect/platform-node/NodeHttpServer";
2
1
  import { Effect, Layer } from "effect";
3
2
  import { HttpRouter, HttpServer } from "effect/http";
4
3
  import type { Rpc } from "effect/rpc";
5
4
  import { schemaSensitiveKeys } from "@shivaedev/errors/schemaSensitiveKeys.ts";
5
+ import { hostHttpServices } from "#adapters/hostHttpServices.ts";
6
6
  import { nodeHttpServer } from "#adapters/nodeHttpServer.ts";
7
7
  import type { appDatabase } from "#appDatabase.ts";
8
8
  import type { AppHttpHostOptions } from "#appHost.ts";
@@ -69,7 +69,7 @@ export function appHostHttp<
69
69
  HttpRouter.toHttpEffect(httpRoutes).pipe(
70
70
  Effect.map((handler) => HttpServer.serve(handler).pipe(HttpServer.withLogAddress, Layer.provideMerge(server))),
71
71
  ),
72
- ).pipe(Layer.provideMerge(application), Layer.provide(sessions), Layer.provideMerge(NodeHttpServer.layerHttpServices));
72
+ ).pipe(Layer.provideMerge(application), Layer.provide(sessions), Layer.provideMerge(hostHttpServices));
73
73
  }),
74
74
  ).pipe(Layer.provide(redactingDiagnostics(sensitive)), Layer.provide(otlpTracing), Layer.provide(Layer.succeed(RedactionPolicy, sensitive))),
75
75
  ),
@@ -13,6 +13,7 @@ export type HostCoreOptions<TRpcs extends Rpc.Any, THandlers, THandlerError, THa
13
13
  readonly browserErrors?: BrowserErrorOptions;
14
14
  readonly config?: AppDatabaseOptions["config"] & {
15
15
  readonly port?: number;
16
+ readonly socketPath?: string;
16
17
  readonly host?: string;
17
18
  readonly trustedProxy?: string;
18
19
  readonly maxBodySize?: number;
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
  });
@@ -7,6 +7,7 @@ import type { schemaSensitiveKeys } from "@shivaedev/errors/schemaSensitiveKeys.
7
7
  import { localRpcGuard } from "@shivaedev/identity/server/adapters/local.ts";
8
8
  import type { SessionPolicy } from "@shivaedev/identity/server/SessionPolicy.ts";
9
9
  import { authenticatedLayer, maybeAuthenticatedLayer } from "@shivaedev/identity/server/session.ts";
10
+ import { subjectResolverLayer } from "@shivaedev/identity/server/subjectResolverLayer.ts";
10
11
  import { boundedRpcRequest } from "#boundedRpcRequest.ts";
11
12
  import { boundedRpcSocket } from "#boundedRpcSocket.ts";
12
13
  import { hostChanges } from "#hostChanges.ts";
@@ -16,6 +17,7 @@ import type { hostTransport } from "#hostTransport.ts";
16
17
  import { redactingRpcSerialization } from "#redactingRpcSerialization.ts";
17
18
  import { rpcRequestTracing } from "#rpcRequestTracing.ts";
18
19
  import { socketRpc } from "#socketRpc.ts";
20
+ import { UnixSocketRequest } from "#UnixSocketRequest.ts";
19
21
 
20
22
  type BuiltRoutes<TRpcs extends Rpc.Any, THandlers, TError, TRequirements> = ReturnType<
21
23
  typeof buildRpcRoutes<TRpcs, THandlers, TError, TRequirements>
@@ -51,16 +53,18 @@ function buildRpcRoutes<TRpcs extends Rpc.Any, THandlers, TError, TRequirements>
51
53
  { bodySize, trustedProxy, nativeOrigins }: Effect.Success<ReturnType<typeof hostTransport>>,
52
54
  sensitive: Effect.Success<ReturnType<typeof schemaSensitiveKeys>>,
53
55
  ) {
54
- const middleware =
56
+ const middleware = Layer.merge(
57
+ subjectResolverLayer(),
55
58
  options.policy === "loopback"
56
59
  ? rpcRequestTracing(sensitive)
57
- : Layer.mergeAll(authenticatedLayer(), maybeAuthenticatedLayer(), rpcRequestTracing(sensitive));
60
+ : Layer.mergeAll(authenticatedLayer(), maybeAuthenticatedLayer(), rpcRequestTracing(sensitive)),
61
+ );
58
62
  return HttpRouter.use((router) =>
59
63
  Effect.gen(function* () {
60
64
  yield* validateReadPolicies(options.group);
61
65
  const wire = redactingRpcSerialization(yield* RpcSerialization.RpcSerialization, sensitive);
62
66
  const handler = yield* hostRpc(options.group).pipe(Effect.provideService(RpcSerialization.RpcSerialization, wire));
63
- const guarded = localRpcGuard(handler, trustedProxy, nativeOrigins);
67
+ const guarded = Effect.flatMap(UnixSocketRequest, (unix) => (unix ? handler : localRpcGuard(handler, trustedProxy, nativeOrigins)));
64
68
  const websocket = yield* hostRpc(options.group, true).pipe(Effect.provideService(RpcSerialization.RpcSerialization, wire));
65
69
  yield* router.add(
66
70
  "OPTIONS",
@@ -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(
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
+ });