@shivaedev/server 0.18.0 → 0.19.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 (67) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/README.md +55 -1
  3. package/dist/AppModule.d.ts +22 -0
  4. package/dist/AppModule.d.ts.map +1 -0
  5. package/dist/AppModule.js +2 -0
  6. package/dist/AppModule.js.map +1 -0
  7. package/dist/DuplicateModuleQueue.d.ts +11 -0
  8. package/dist/DuplicateModuleQueue.d.ts.map +1 -0
  9. package/dist/DuplicateModuleQueue.js +11 -0
  10. package/dist/DuplicateModuleQueue.js.map +1 -0
  11. package/dist/appModules.d.ts +37 -0
  12. package/dist/appModules.d.ts.map +1 -0
  13. package/dist/appModules.js +37 -0
  14. package/dist/appModules.js.map +1 -0
  15. package/dist/browserErrorRoute.js +1 -1
  16. package/dist/browserErrorRoute.js.map +1 -1
  17. package/dist/hostChanges.d.ts.map +1 -1
  18. package/dist/hostChanges.js +8 -11
  19. package/dist/hostChanges.js.map +1 -1
  20. package/dist/hostRpcRoutes.js +1 -1
  21. package/dist/hostRpcRoutes.js.map +1 -1
  22. package/dist/hostTransport.js +1 -1
  23. package/dist/hostTransport.js.map +1 -1
  24. package/dist/nativeClientOrigins.js +1 -1
  25. package/dist/nativeClientOrigins.js.map +1 -1
  26. package/dist/socketRpc.js +1 -1
  27. package/dist/socketRpc.js.map +1 -1
  28. package/dist/testing/HostProcessError.d.ts +10 -0
  29. package/dist/testing/HostProcessError.d.ts.map +1 -0
  30. package/dist/testing/HostProcessError.js +4 -0
  31. package/dist/testing/HostProcessError.js.map +1 -0
  32. package/dist/testing/adapters/hostProcess.d.ts +21 -0
  33. package/dist/testing/adapters/hostProcess.d.ts.map +1 -0
  34. package/dist/testing/adapters/hostProcess.js +81 -0
  35. package/dist/testing/adapters/hostProcess.js.map +1 -0
  36. package/dist/testing/hostProcess.d.ts +20 -0
  37. package/dist/testing/hostProcess.d.ts.map +1 -0
  38. package/dist/testing/hostProcess.js +31 -0
  39. package/dist/testing/hostProcess.js.map +1 -0
  40. package/dist/testing/hostProcessChild.d.ts +6 -0
  41. package/dist/testing/hostProcessChild.d.ts.map +1 -0
  42. package/dist/testing/hostProcessChild.js +16 -0
  43. package/dist/testing/hostProcessChild.js.map +1 -0
  44. package/dist/testing/hostProcessProtocol.d.ts +11 -0
  45. package/dist/testing/hostProcessProtocol.d.ts.map +1 -0
  46. package/dist/testing/hostProcessProtocol.js +4 -0
  47. package/dist/testing/hostProcessProtocol.js.map +1 -0
  48. package/dist/workerHost.d.ts +7 -0
  49. package/dist/workerHost.d.ts.map +1 -0
  50. package/dist/workerHost.js +7 -0
  51. package/dist/workerHost.js.map +1 -0
  52. package/package.json +17 -12
  53. package/src/AppModule.ts +19 -0
  54. package/src/DuplicateModuleQueue.ts +11 -0
  55. package/src/appModules.ts +99 -0
  56. package/src/browserErrorRoute.ts +1 -1
  57. package/src/hostChanges.ts +8 -11
  58. package/src/hostRpcRoutes.ts +1 -1
  59. package/src/hostTransport.ts +1 -1
  60. package/src/nativeClientOrigins.ts +1 -1
  61. package/src/socketRpc.ts +1 -1
  62. package/src/testing/HostProcessError.ts +6 -0
  63. package/src/testing/adapters/hostProcess.ts +90 -0
  64. package/src/testing/hostProcess.ts +36 -0
  65. package/src/testing/hostProcessChild.ts +21 -0
  66. package/src/testing/hostProcessProtocol.ts +4 -0
  67. package/src/workerHost.ts +13 -0
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hostProcess.js","sourceRoot":"","sources":["../../../src/testing/adapters/hostProcess.ts"],"names":[],"mappings":"AAAA,OAAO,EAAuC,IAAI,EAAE,MAAM,oBAAoB,CAAC;AAC/E,OAAO,OAAO,MAAM,cAAc,CAAC;AACnC,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AACzC,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAUxC,MAAM,UAAU,QAAQ,CAAC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ProcessError } from "./HostProcessError.ts";
4
+ export declare const hostProcess: (options: HostProcessOptions) => Effect.Effect<{
5
+ headers: (path: string) => Effect.Effect<{
6
+ [k: string]: string;
7
+ }, HostProcessError, never>;
8
+ health: () => Effect.Effect<{
9
+ body: string;
10
+ headers: {
11
+ [k: string]: string;
12
+ };
13
+ status: number;
14
+ }, HostProcessError, never>;
15
+ pid: number;
16
+ signal: (delivered: NodeJS.Signals) => Effect.Effect<void, HostProcessError, never>;
17
+ stop: () => Effect.Effect<void, never, never>;
18
+ url: string;
19
+ }, HostProcessError, import("effect/Scope").Scope>;
20
+ //# sourceMappingURL=hostProcess.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hostProcess.d.ts","sourceRoot":"","sources":["../../src/testing/hostProcess.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAU,MAAM,QAAQ,CAAC;AACxC,OAAO,EAAY,KAAK,kBAAkB,EAA8C,MAAM,kCAAkC,CAAC;AACjI,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAGzD,eAAO,MAAM,WAAW;oBAgBN,MAAM;;;;;;;;;;;wBAGF,MAAM,CAAC,OAAO;;;kDAWlC,CAAC"}
@@ -0,0 +1,31 @@
1
+ import { Effect, Schema } from "effect";
2
+ import { forkHost, hostResponse, receiveHostMessage, stopHost } from "#testing/adapters/hostProcess.ts";
3
+ import { HostProcessError } from "./HostProcessError.js";
4
+ import { HostReady, HostSignal } from "./hostProcessProtocol.js";
5
+ export const hostProcess = Effect.fn("HostProcess.hostProcess")(function* (options) {
6
+ const child = yield* Effect.acquireRelease(Effect.try({ catch: (cause) => new HostProcessError({ cause, operation: "launch" }), try: () => forkHost(options) }), (acquired) => Effect.promise(() => stopHost(acquired)));
7
+ const { pid, url } = yield* Effect.tryPromise({
8
+ catch: (cause) => new HostProcessError({ cause, operation: "launch" }),
9
+ try: (signal) => receiveHostMessage(child, HostReady, signal),
10
+ });
11
+ function response(path) {
12
+ return Effect.tryPromise({
13
+ catch: (cause) => new HostProcessError({ cause, operation: "request" }),
14
+ try: (signal) => hostResponse(url, path, signal),
15
+ });
16
+ }
17
+ return {
18
+ headers: (path) => response(path).pipe(Effect.map((answer) => answer.headers)),
19
+ health: () => response("/health"),
20
+ pid,
21
+ signal: (delivered) => Effect.tryPromise({
22
+ catch: (cause) => new HostProcessError({ cause, operation: "signal" }),
23
+ try: (signal) => receiveHostMessage(child, HostSignal.pipe(Schema.check(Schema.makeFilter((ack) => ack.signal === delivered))), signal, () => {
24
+ child.kill(delivered);
25
+ }),
26
+ }).pipe(Effect.asVoid),
27
+ stop: () => Effect.promise(() => stopHost(child)),
28
+ url,
29
+ };
30
+ });
31
+ //# sourceMappingURL=hostProcess.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hostProcess.js","sourceRoot":"","sources":["../../src/testing/hostProcess.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AACxC,OAAO,EAAE,QAAQ,EAA2B,YAAY,EAAE,kBAAkB,EAAE,QAAQ,EAAE,MAAM,kCAAkC,CAAC;AACjI,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AACzD,OAAO,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,0BAA0B,CAAC;AAEjE,MAAM,CAAC,MAAM,WAAW,GAAG,MAAM,CAAC,EAAE,CAAC,yBAAyB,CAAC,CAAC,QAAQ,CAAC,EAAE,OAA2B;IACrG,MAAM,KAAK,GAAG,KAAK,CAAC,CAAC,MAAM,CAAC,cAAc,CACzC,MAAM,CAAC,GAAG,CAAC,EAAE,KAAK,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,IAAI,gBAAgB,CAAC,EAAE,KAAK,EAAE,SAAS,EAAE,QAAQ,EAAE,CAAC,EAAE,GAAG,EAAE,GAAG,EAAE,CAAC,QAAQ,CAAC,OAAO,CAAC,EAAE,CAAC,EACpH,CAAC,QA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AAE,CACf,kBAAkB,CAAC,KAAK,EAAE,UAAU,CAAC,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,MAAM,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,GAAG,EAAE;gBAC3H,KAAK,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;YACvB,CAAC,CAAC;SACH,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC;QACvB,IAAI,EAAE,GAAG,EAAE,CAAC,MAAM,CAAC,OAAO,CAAC,GAAG,EAAE,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;QACjD,GAAG;KACH,CAAC;AACH,CAAC,CAAC,CAAC"}
@@ -0,0 +1,6 @@
1
+ import { Effect } from "effect";
2
+ import { HttpServer } from "effect/http";
3
+ import { HostProcessError } from "./HostProcessError.ts";
4
+ export declare const reportHostReady: Effect.Effect<void, HostProcessError, HttpServer.HttpServer>;
5
+ export declare function acknowledgeHostSignal(signal: NodeJS.Signals): Effect.Effect<void, HostProcessError, never>;
6
+ //# sourceMappingURL=hostProcessChild.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hostProcessChild.d.ts","sourceRoot":"","sources":["../../src/testing/hostProcessChild.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAChC,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAEzC,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAEzD,eAAO,MAAM,eAAe,8DAO3B,CAAC;AAEF,wBAAgB,qBAAqB,CAAC,MAAM,EAAE,MAAM,CAAC,OAAO,gDAK3D"}
@@ -0,0 +1,16 @@
1
+ import process from "node:process";
2
+ import { Effect } from "effect";
3
+ import { HttpServer } from "effect/http";
4
+ import { sendHostMessage } from "#testing/adapters/hostProcess.ts";
5
+ import { HostProcessError } from "./HostProcessError.js";
6
+ export const reportHostReady = HttpServer.HttpServer.pipe(Effect.flatMap((server) => Effect.tryPromise({
7
+ catch: (cause) => new HostProcessError({ cause, operation: "launch" }),
8
+ try: () => sendHostMessage({ pid: process.pid, type: "host-ready", url: HttpServer.formatAddress(server.address) }),
9
+ })));
10
+ export function acknowledgeHostSignal(signal) {
11
+ return Effect.tryPromise({
12
+ catch: (cause) => new HostProcessError({ cause, operation: "signal" }),
13
+ try: () => sendHostMessage({ signal, type: "host-signal" }),
14
+ });
15
+ }
16
+ //# sourceMappingURL=hostProcessChild.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hostProcessChild.js","sourceRoot":"","sources":["../../src/testing/hostProcessChild.ts"],"names":[],"mappings":"AAAA,OAAO,OAAO,MAAM,cAAc,CAAC;AACnC,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAChC,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,eAAe,EAAE,MAAM,kCAAkC,CAAC;AACnE,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAEzD,MAAM,CAAC,MAAM,eAAe,GAAG,UAAU,CAAC,UAAU,CAAC,IAAI,CACxD,MAAM,CAAC,OAAO,CAAC,CAAC,MAAM,EAAE,EAAE,CACzB,MAAM,CAAC,UAAU,CAAC;IACjB,KAAK,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,IAAI,gBAAgB,CAAC,EAAE,KAAK,EAAE,SAAS,EAAE,QAAQ,EAAE,CAAC;IACtE,GAAG,EAAE,GAAG,EAAE,CAAC,eAAe,CAAC,EAAE,GAAG,EAAE,OAAO,CAAC,GAAG,EAAE,IAAI,EAAE,YAAY,EAAE,GAAG,EAAE,UAAU,CAAC,aAAa,CAAC,MAAM,CAAC,OAAO,CAAC,EAAE,CAAC;CACnH,CAAC,CACF,CACD,CAAC;AAEF,MAAM,UAAU,qBAAqB,CAAC,MAAsB;IAC3D,OAAO,MAAM,CAAC,UAAU,CAAC;QACxB,KAAK,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,IAAI,gBAAgB,CAAC,EAAE,KAAK,EAAE,SAAS,EAAE,QAAQ,EAAE,CAAC;QACtE,GAAG,EAAE,GAAG,EAAE,CAAC,eAAe,CAAC,EAAE,MAAM,EAAE,IAAI,EAAE,aAAa,EAAE,CAAC;KAC3D,CAAC,CAAC;AACJ,CAAC"}
@@ -0,0 +1,11 @@
1
+ import { Schema } from "effect";
2
+ export declare const HostReady: Schema.Struct<{
3
+ readonly pid: Schema.Number;
4
+ readonly type: Schema.Literal<"host-ready">;
5
+ readonly url: Schema.String;
6
+ }>;
7
+ export declare const HostSignal: Schema.Struct<{
8
+ readonly signal: Schema.String;
9
+ readonly type: Schema.Literal<"host-signal">;
10
+ }>;
11
+ //# sourceMappingURL=hostProcessProtocol.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hostProcessProtocol.d.ts","sourceRoot":"","sources":["../../src/testing/hostProcessProtocol.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAEhC,eAAO,MAAM,SAAS;;;;EAAgG,CAAC;AACvH,eAAO,MAAM,UAAU;;;EAAgF,CAAC"}
@@ -0,0 +1,4 @@
1
+ import { Schema } from "effect";
2
+ export const HostReady = Schema.Struct({ pid: Schema.Number, type: Schema.Literal("host-ready"), url: Schema.String });
3
+ export const HostSignal = Schema.Struct({ signal: Schema.String, type: Schema.Literal("host-signal") });
4
+ //# sourceMappingURL=hostProcessProtocol.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hostProcessProtocol.js","sourceRoot":"","sources":["../../src/testing/hostProcessProtocol.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAEhC,MAAM,CAAC,MAAM,SAAS,GAAG,MAAM,CAAC,MAAM,CAAC,EAAE,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,IAAI,EAAE,MAAM,CAAC,OAAO,CAAC,YAAY,CAAC,EAAE,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC;AACvH,MAAM,CAAC,MAAM,UAAU,GAAG,MAAM,CAAC,MAAM,CAAC,EAAE,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,IAAI,EAAE,MAAM,CAAC,OAAO,CAAC,aAAa,CAAC,EAAE,CAAC,CAAC"}
@@ -0,0 +1,7 @@
1
+ import { Layer } from "effect";
2
+ import type { AppDatabaseOptions } from "#appHost.ts";
3
+ export declare function workerHost<R, E, TRequirements, TServices, TServiceError, TServiceRequirements, TMigrations = never, TMigrationError = never>(options: AppDatabaseOptions<TMigrations, TMigrationError> & {
4
+ readonly workers: Layer.Layer<R, E, TRequirements>;
5
+ readonly services: Layer.Layer<TServices, TServiceError, TServiceRequirements>;
6
+ }): Layer.Layer<"@shivaedev/contract/LiveServer" | R | TServices | import("@effect/sql-pg/PgClient").PgClient | import("@effect/sql-pg/PgPool").PgPool | import("effect/reactivity/Reactivity").Reactivity | import("effect/sql/SqlClient").SqlClient, E | TMigrationError | TServiceError | import("effect/Config").ConfigError | import("effect/sql/Migrator").MigrationError | import("@shivaedev/sql/PostgresListenerError.ts").PostgresListenerError | import("effect/Schema").SchemaError | import("effect/sql/SqlError").SqlError, Exclude<Exclude<TServiceRequirements, "@shivaedev/contract/LiveServer" | import("@effect/sql-pg/PgClient").PgClient | import("@effect/sql-pg/PgPool").PgPool | import("effect/reactivity/Reactivity").Reactivity | import("effect/sql/SqlClient").SqlClient>, import("effect/Crypto").Crypto> | Exclude<Exclude<Exclude<TMigrations, import("effect/Scope").Scope>, "@shivaedev/contract/LiveServer" | import("@effect/sql-pg/PgClient").PgClient | import("@effect/sql-pg/PgPool").PgPool | import("effect/reactivity/Reactivity").Reactivity | import("effect/sql/SqlClient").SqlClient>, import("effect/Crypto").Crypto> | Exclude<Exclude<Exclude<TRequirements, TServices>, "@shivaedev/contract/LiveServer" | import("@effect/sql-pg/PgClient").PgClient | import("@effect/sql-pg/PgPool").PgPool | import("effect/reactivity/Reactivity").Reactivity | import("effect/sql/SqlClient").SqlClient>, import("effect/Crypto").Crypto>>;
7
+ //# sourceMappingURL=workerHost.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"workerHost.d.ts","sourceRoot":"","sources":["../src/workerHost.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,EAAE,MAAM,QAAQ,CAAC;AAE/B,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AAEtD,wBAAgB,UAAU,CAAC,CAAC,EAAE,CAAC,EAAE,aAAa,EAAE,SAAS,EAAE,aAAa,EAAE,oBAAoB,EAAE,WAAW,GAAG,KAAK,EAAE,eAAe,GAAG,KAAK,EAC3I,OAAO,EAAE,kBAAkB,CAAC,WAAW,EAAE,eAAe,CAAC,GAAG;IAC3D,QAAQ,CAAC,OAAO,EAAE,KAAK,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,EAAE,aAAa,CAAC,CAAC;IACnD,QAAQ,CAAC,QAAQ,EAAE,KAAK,CAAC,KAAK,CAAC,SAAS,EAAE,aAAa,EAAE,oBAAoB,CAAC,CAAC;CAC/E,m5CAGD"}
@@ -0,0 +1,7 @@
1
+ import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
2
+ import { Layer } from "effect";
3
+ import { appDatabase } from "#appDatabase.ts";
4
+ export function workerHost(options) {
5
+ return options.workers.pipe(Layer.provideMerge(options.services), Layer.provideMerge(appDatabase(options)), Layer.provide(NodeCrypto.layer));
6
+ }
7
+ //# sourceMappingURL=workerHost.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"workerHost.js","sourceRoot":"","sources":["../src/workerHost.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,UAAU,MAAM,kCAAkC,CAAC;AAC/D,OAAO,EAAE,KAAK,EAAE,MAAM,QAAQ,CAAC;AAC/B,OAAO,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAG9C,MAAM,UAAU,UAAU,CACzB,OAGC;IAED,OAAO,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,YAAY,CAAC,OAAO,CAAC,QAAQ,CAAC,EAAE,KAAK,CAAC,YAAY,CAAC,WAAW,CAAC,OAAO,CAAC,CAAC,EAAE,KAAK,CAAC,OAAO,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC,CAAC;AAC9I,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shivaedev/server",
3
- "version": "0.18.0",
3
+ "version": "0.19.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"
@@ -45,25 +45,26 @@
45
45
  "README.md"
46
46
  ],
47
47
  "dependencies": {
48
- "@shivaedev/changes": "0.7.0",
49
- "@shivaedev/contract": "0.30.2",
48
+ "@shivaedev/changes": "0.8.0",
49
+ "@shivaedev/contract": "0.31.0",
50
50
  "@shivaedev/errors": "0.5.0",
51
51
  "@shivaedev/http": "0.4.0",
52
- "@shivaedev/identity": "0.18.0",
53
- "@shivaedev/local-postgres": "0.9.0",
54
- "@shivaedev/sql": "0.32.0"
52
+ "@shivaedev/identity": "0.19.0",
53
+ "@shivaedev/local-postgres": "0.10.0",
54
+ "@shivaedev/sql": "0.33.0"
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
60
  "@shivaedev/access": "0.3.6",
61
- "@shivaedev/admin": "0.18.0",
62
- "@shivaedev/jobs": "0.29.2",
63
- "@shivaedev/quality": "0.55.0",
64
- "@shivaedev/test": "0.12.1",
65
- "@shivaedev/test-sql": "0.16.1",
66
- "@shivaedev/test-story": "0.33.0",
61
+ "@shivaedev/admin": "0.18.1",
62
+ "@shivaedev/jobs": "0.30.0",
63
+ "@shivaedev/quality": "0.56.0",
64
+ "@shivaedev/rate-limit": "0.2.1",
65
+ "@shivaedev/test": "0.13.0",
66
+ "@shivaedev/test-sql": "0.16.2",
67
+ "@shivaedev/test-story": "0.34.0",
67
68
  "@types/node": "24.19.2",
68
69
  "better-auth": "1.7.12",
69
70
  "effect": "4.0.6",
@@ -75,6 +76,7 @@
75
76
  "@effect/platform-node": "4.0.6",
76
77
  "@effect/sql-pg": "4.0.6",
77
78
  "@effect/vitest": "4.0.6",
79
+ "@shivaedev/admin": "0.18.1",
78
80
  "better-auth": "1.7.12",
79
81
  "effect": "4.0.6"
80
82
  },
@@ -88,6 +90,9 @@
88
90
  "@effect/vitest": {
89
91
  "optional": true
90
92
  },
93
+ "@shivaedev/admin": {
94
+ "optional": true
95
+ },
91
96
  "better-auth": {
92
97
  "optional": true
93
98
  }
@@ -0,0 +1,19 @@
1
+ import type { Layer, Pipeable } from "effect";
2
+ import type { RpcGroup } from "effect/rpc";
3
+ import type { AnyAdminRegistry } from "@shivaedev/admin/registry.ts";
4
+ import type { MigrationOptions } from "#MigrationOptions.ts";
5
+
6
+ export type ModuleLayer = Layer.Any & Pipeable.Pipeable;
7
+ export type ModuleAdmin = AnyAdminRegistry & { readonly handlers: ModuleLayer };
8
+
9
+ export interface AppModule {
10
+ readonly admin?: ModuleAdmin;
11
+ readonly group: RpcGroup.Any;
12
+ readonly handlers: ModuleLayer;
13
+ readonly migrations: Pick<MigrationOptions<unknown, unknown>, "loader" | "components">;
14
+ readonly name: string;
15
+ readonly ports?: ModuleLayer;
16
+ readonly queues?: readonly { readonly name: string }[];
17
+ readonly services: ModuleLayer;
18
+ readonly workers?: ModuleLayer;
19
+ }
@@ -0,0 +1,11 @@
1
+ import { Schema } from "effect";
2
+
3
+ export class DuplicateModuleQueue extends Schema.TaggedError<DuplicateModuleQueue>()("DuplicateModuleQueue", {
4
+ firstModule: Schema.String,
5
+ queue: Schema.String,
6
+ secondModule: Schema.String,
7
+ }) {
8
+ override get message() {
9
+ return `Queue "${this.queue}" is declared by both "${this.firstModule}" and "${this.secondModule}".`;
10
+ }
11
+ }
@@ -0,0 +1,99 @@
1
+ import { Effect, Layer } from "effect";
2
+ import { RpcGroup } from "effect/rpc";
3
+ import type { Migrator } from "effect/sql";
4
+ import type { AdminPage } from "@shivaedev/admin/page.ts";
5
+ import type { AdminRegistry, AnyAdminRegistry } from "@shivaedev/admin/registry.ts";
6
+ import { mergeAdminRegistrations } from "@shivaedev/admin/server/mergeAdminRegistrations.ts";
7
+ import type { SchemaComponent } from "@shivaedev/sql/SchemaComponent.ts";
8
+ import type { AppModule, ModuleAdmin, ModuleLayer } from "#AppModule.ts";
9
+ import { DuplicateModuleQueue } from "#DuplicateModuleQueue.ts";
10
+ import type { MigrationOptions } from "#MigrationOptions.ts";
11
+
12
+ type ModuleField<TModules, TKey extends PropertyKey> = TModules extends unknown ? (TKey extends keyof TModules ? TModules[TKey] : never) : never;
13
+ type Layers<TModules extends readonly AppModule[], TKey extends "handlers" | "services" | "ports" | "workers"> = Extract<
14
+ ModuleField<TModules[number], TKey>,
15
+ Layer.Any
16
+ >;
17
+ type Admin<TModules extends readonly AppModule[]> = Extract<ModuleField<TModules[number], "admin">, ModuleAdmin>;
18
+ type Handlers<TModules extends readonly AppModule[]> = Layers<TModules, "handlers"> | Extract<ModuleField<Admin<TModules>, "handlers">, Layer.Any>;
19
+ type Pages<TModules extends readonly AppModule[]> = Extract<ModuleField<Admin<TModules>, "pages">, readonly AdminPage[]>;
20
+ type Queues<TModules extends readonly AppModule[]> = Extract<ModuleField<TModules[number], "queues">, readonly { readonly name: string }[]>[number];
21
+ type Ports<TModules extends readonly AppModule[]> = Layers<TModules, "ports">;
22
+ type Services<TModules extends readonly AppModule[]> = Layers<TModules, "services">;
23
+ type MigrationRequirements<TModules extends readonly AppModule[]> = Effect.Services<TModules[number]["migrations"]["loader"]>;
24
+ type Components<T> = T extends readonly (infer TComponent extends SchemaComponent<unknown, unknown>)[] ? TComponent : never;
25
+ type ModuleComponents<TModules extends AppModule> = Components<ModuleField<TModules["migrations"], "components">>;
26
+
27
+ export function appModules<const TModules extends readonly AppModule[], R = never, E = never, TRequirements = never>(
28
+ modules: TModules,
29
+ options?: { readonly replacements?: Layer.Layer<R, E, TRequirements> },
30
+ ): Effect.Effect<
31
+ {
32
+ readonly workers: Layer.Layer<
33
+ Layer.Success<Layers<TModules, "workers">>,
34
+ Layer.Error<Layers<TModules, "workers">>,
35
+ Layer.Services<Layers<TModules, "workers">>
36
+ >;
37
+ readonly queues: readonly Queues<TModules>[];
38
+ readonly admin: AdminRegistry<readonly Pages<TModules>[number][]>;
39
+ readonly group: RpcGroup.RpcGroup<RpcGroup.Rpcs<TModules[number]["group"] | ModuleField<Admin<TModules>, "group">>>;
40
+ readonly handlers: Layer.Layer<Layer.Success<Handlers<TModules>>, Layer.Error<Handlers<TModules>>, Layer.Services<Handlers<TModules>>>;
41
+ readonly services: Layer.Layer<
42
+ Layer.Success<Services<TModules> | Ports<TModules>> | R,
43
+ Layer.Error<Services<TModules> | Ports<TModules>> | E,
44
+ Exclude<Layer.Services<Services<TModules>>, Layer.Success<Ports<TModules>> | R> | Layer.Services<Ports<TModules>> | TRequirements
45
+ >;
46
+ readonly migrations: {
47
+ readonly loader: Migrator.Loader<MigrationRequirements<TModules>>;
48
+ readonly components: readonly ModuleComponents<TModules[number]>[];
49
+ };
50
+ },
51
+ DuplicateModuleQueue
52
+ >;
53
+ export function appModules(
54
+ modules: readonly AppModule[],
55
+ options?: { readonly replacements?: ModuleLayer },
56
+ ): Effect.Effect<
57
+ {
58
+ readonly workers: ModuleLayer;
59
+ readonly queues: readonly { readonly name: string }[];
60
+ readonly admin: AnyAdminRegistry;
61
+ readonly group: RpcGroup.Any;
62
+ readonly handlers: ModuleLayer;
63
+ readonly migrations: MigrationOptions<unknown, unknown>;
64
+ readonly services: ModuleLayer;
65
+ },
66
+ DuplicateModuleQueue
67
+ > {
68
+ return composeModules(modules, options);
69
+ }
70
+
71
+ const composeModules = Effect.fnUntraced(function* (modules: readonly AppModule[], options?: { readonly replacements?: ModuleLayer }) {
72
+ const owners = new Map<string, string>();
73
+ for (const module of modules) {
74
+ for (const queue of module.queues ?? []) {
75
+ const firstModule = owners.get(queue.name);
76
+ if (firstModule !== undefined) {
77
+ return yield* new DuplicateModuleQueue({ firstModule, queue: queue.name, secondModule: module.name });
78
+ }
79
+ owners.set(queue.name, module.name);
80
+ }
81
+ }
82
+ const queues = modules.flatMap((module) => module.queues ?? []);
83
+ const registrations = modules.flatMap((module) => (module.admin === undefined ? [] : [module.admin]));
84
+ const admin = mergeAdminRegistrations(...registrations);
85
+ const defaults = Layer.mergeAll(Layer.empty, ...modules.map((module) => module.ports ?? Layer.empty));
86
+ const ports = Layer.merge(defaults, options?.replacements ?? Layer.empty);
87
+ return {
88
+ admin,
89
+ group: RpcGroup.make().merge(...modules.map((module) => module.group), admin.group),
90
+ handlers: Layer.mergeAll(admin.handlers, ...modules.map((module) => module.handlers)),
91
+ migrations: {
92
+ components: modules.flatMap((module) => module.migrations.components ?? []),
93
+ loader: Effect.all(modules.map((module) => module.migrations.loader)).pipe(Effect.map((entries) => entries.flat())),
94
+ },
95
+ queues,
96
+ services: Layer.mergeAll(Layer.empty, ...modules.map((module) => module.services)).pipe(Layer.provideMerge(ports)),
97
+ workers: Layer.mergeAll(Layer.empty, ...modules.map((module) => module.workers ?? Layer.empty)),
98
+ };
99
+ });
@@ -4,7 +4,7 @@ import * as HttpStatus from "effect/http/HttpStatus";
4
4
  import { RateLimiter } from "effect/persistence";
5
5
  import { redactBrowserText } from "@shivaedev/errors/redactBrowserText.ts";
6
6
  import { isAuthUnavailable, isForbidden } from "@shivaedev/errors/taxonomy.ts";
7
- import { localTransport, trustedProxyPeer } from "@shivaedev/identity/server/local.ts";
7
+ import { localTransport, trustedProxyPeer } from "@shivaedev/identity/server/adapters/local.ts";
8
8
  import { SessionPolicy } from "@shivaedev/identity/server/SessionPolicy.ts";
9
9
  import { resolveSession } from "@shivaedev/identity/server/session.ts";
10
10
 
@@ -4,7 +4,7 @@ import { Rpc, type RpcGroup } from "effect/rpc";
4
4
  import { LiveServer } from "@shivaedev/contract/LiveServer.ts";
5
5
  import { CurrentViewer, type Viewer } from "@shivaedev/contract/readPolicy.ts";
6
6
  import type { QueryReference } from "@shivaedev/contract/serverMetadata.ts";
7
- import { Unauthorized } from "@shivaedev/errors/taxonomy.ts";
7
+ import { currentSessionContext } from "@shivaedev/identity/server/currentSessionContext.ts";
8
8
  import { recheckSession } from "@shivaedev/identity/server/recheckSession.ts";
9
9
  import { revalidateStream } from "@shivaedev/identity/server/revalidateStream.ts";
10
10
  import { SessionPolicy } from "@shivaedev/identity/server/SessionPolicy.ts";
@@ -23,11 +23,9 @@ export function hostChanges<R extends Rpc.Any>(group: RpcGroup.RpcGroup<R>) {
23
23
  Effect.gen(function* () {
24
24
  const { subscription, context } = yield* resolveChanges(rpc, queries);
25
25
  const viewer = yield* CurrentViewer;
26
- const authorized = Option.isNone(sessions)
27
- ? subscription.authorized
28
- : authorizeSession(rpc._tag, viewer, subscription.authorized).pipe(Effect.provideService(SessionPolicy, sessions.value));
26
+ const refresh = refreshSession(sessions, viewer, rpc._tag);
29
27
  const stream = Option.isSome(changes)
30
- ? Stream.unwrap(changes.value.subscribe({ ...subscription, authorized }, context, rerunQuery(group, options)))
28
+ ? Stream.unwrap(changes.value.subscribe({ ...subscription, refresh }, context, rerunQuery(group, options)))
31
29
  : Stream.concat(Stream.succeed({ _tag: "Resync" } as const), Stream.never);
32
30
  if (Option.isNone(sessions) || Option.isNone(viewer)) {
33
31
  return stream;
@@ -61,10 +59,9 @@ export function hostChanges<R extends Rpc.Any>(group: RpcGroup.RpcGroup<R>) {
61
59
  );
62
60
  }
63
61
 
64
- const authorizeSession = Effect.fnUntraced(function* <E, R>(tag: string, viewer: Option.Option<Viewer>, policy: Effect.Effect<boolean, E, R>) {
65
- const current = yield* recheckSession(Headers.empty, tag);
66
- if (Option.isSome(viewer) && (Option.isNone(current) || current.value.user.id !== viewer.value.id)) {
67
- return yield* new Unauthorized({ message: "Authentication required" });
62
+ function refreshSession(sessions: Option.Option<typeof SessionPolicy.Service>, viewer: Option.Option<Viewer>, tag: string) {
63
+ if (Option.isNone(sessions) || Option.isNone(viewer)) {
64
+ return Effect.context<never>();
68
65
  }
69
- return yield* policy;
70
- });
66
+ return currentSessionContext(viewer.value.id, recheckSession(Headers.empty, tag)).pipe(Effect.provideService(SessionPolicy, sessions.value));
67
+ }
@@ -4,7 +4,7 @@ import * as HttpStatus from "effect/http/HttpStatus";
4
4
  import { type Rpc, type RpcGroup, RpcSerialization } from "effect/rpc";
5
5
  import { validateReadPolicies } from "@shivaedev/contract/validateReadPolicies.ts";
6
6
  import type { schemaSensitiveKeys } from "@shivaedev/errors/schemaSensitiveKeys.ts";
7
- import { localRpcGuard } from "@shivaedev/identity/server/local.ts";
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
10
  import { boundedRpcRequest } from "#boundedRpcRequest.ts";
@@ -1,6 +1,6 @@
1
1
  import { Config, Effect, Option, Result, Schema } from "effect";
2
2
  import * as NetAddress from "effect/net/NetAddress";
3
- import { TrustedProxy } from "@shivaedev/identity/server/trustedProxy.ts";
3
+ import { TrustedProxy } from "@shivaedev/identity/server/adapters/trustedProxy.ts";
4
4
  import { NativeOrigins } from "#nativeClientOrigins.ts";
5
5
  import { TrustedProxyRequired } from "#TrustedProxyRequired.ts";
6
6
 
@@ -1,7 +1,7 @@
1
1
  import { Effect, Schema } from "effect";
2
2
  import { Headers, HttpMiddleware, HttpRouter, HttpServerRequest, HttpServerResponse } from "effect/http";
3
3
  import * as HttpStatus from "effect/http/HttpStatus";
4
- import { localTransport } from "@shivaedev/identity/server/local.ts";
4
+ import { localTransport } from "@shivaedev/identity/server/adapters/local.ts";
5
5
 
6
6
  export const NativeOrigins = Schema.Array(
7
7
  Schema.String.check(
package/src/socketRpc.ts CHANGED
@@ -2,7 +2,7 @@ import { Effect, type Scope } from "effect";
2
2
  import { Headers, HttpServerRequest, HttpServerResponse } from "effect/http";
3
3
  import * as HttpStatus from "effect/http/HttpStatus";
4
4
  import { isAuthUnavailable, isForbidden } from "@shivaedev/errors/taxonomy.ts";
5
- import { localTransport } from "@shivaedev/identity/server/local.ts";
5
+ import { localTransport } from "@shivaedev/identity/server/adapters/local.ts";
6
6
  import { SessionPolicy } from "@shivaedev/identity/server/SessionPolicy.ts";
7
7
  import { resolveSession } from "@shivaedev/identity/server/session.ts";
8
8
  import { makeSocketSession, SocketSession } from "@shivaedev/identity/server/socketSession.ts";
@@ -0,0 +1,6 @@
1
+ import { Data } from "effect";
2
+
3
+ export class HostProcessError extends Data.TaggedError("HostProcessError")<{
4
+ readonly operation: "launch" | "signal" | "request";
5
+ readonly cause: unknown;
6
+ }> {}
@@ -0,0 +1,90 @@
1
+ import { type ChildProcess, type ForkOptions, fork } from "node:child_process";
2
+ import process from "node:process";
3
+ import { fileURLToPath } from "node:url";
4
+ import { Option, Schema } from "effect";
5
+
6
+ export interface HostProcessOptions {
7
+ readonly args?: readonly string[];
8
+ readonly cwd?: string;
9
+ readonly env?: NodeJS.ProcessEnv;
10
+ readonly execArgv?: readonly string[];
11
+ readonly module: string | URL;
12
+ }
13
+
14
+ export function forkHost(options: HostProcessOptions) {
15
+ const settings: ForkOptions = {
16
+ cwd: options.cwd,
17
+ env: options.env,
18
+ execArgv: [...(options.execArgv ?? [])],
19
+ stdio: ["ignore", "ignore", "inherit", "ipc"],
20
+ };
21
+ return fork(typeof options.module === "string" ? options.module : fileURLToPath(options.module), [...(options.args ?? [])], settings);
22
+ }
23
+
24
+ export function receiveHostMessage<A>(child: ChildProcess, schema: Schema.Codec<A>, signal: AbortSignal, action?: () => void) {
25
+ return new Promise<A>((resolve, reject) => {
26
+ function cleanup() {
27
+ child.off("message", message);
28
+ child.off("exit", exited);
29
+ child.off("error", failed);
30
+ signal.removeEventListener("abort", aborted);
31
+ }
32
+ function failed(cause: unknown) {
33
+ cleanup();
34
+ reject(cause);
35
+ }
36
+ function exited(code: number | null, delivered: NodeJS.Signals | null) {
37
+ failed(new Error(`Host exited (${delivered ?? code}) before IPC acknowledgement`));
38
+ }
39
+ function aborted() {
40
+ failed(signal.reason);
41
+ }
42
+ function message(value: unknown) {
43
+ const decoded = Schema.decodeUnknownOption(schema)(value);
44
+ if (Option.isSome(decoded)) {
45
+ cleanup();
46
+ resolve(decoded.value);
47
+ }
48
+ }
49
+ child.on("message", message);
50
+ child.once("exit", exited);
51
+ child.once("error", failed);
52
+ signal.addEventListener("abort", aborted, { once: true });
53
+ if (signal.aborted) {
54
+ aborted();
55
+ } else if (child.exitCode !== null || child.signalCode !== null) {
56
+ exited(child.exitCode, child.signalCode);
57
+ } else {
58
+ try {
59
+ action?.();
60
+ } catch (cause) {
61
+ failed(cause);
62
+ }
63
+ }
64
+ });
65
+ }
66
+
67
+ export function stopHost(child: ChildProcess) {
68
+ if (child.pid === undefined || child.exitCode !== null || child.signalCode !== null) {
69
+ return Promise.resolve();
70
+ }
71
+ return new Promise<void>((resolve) => {
72
+ child.once("exit", () => resolve());
73
+ child.kill("SIGKILL");
74
+ });
75
+ }
76
+
77
+ export function sendHostMessage(message: unknown) {
78
+ return new Promise<void>((resolve, reject) => {
79
+ if (process.send === undefined) {
80
+ reject(new Error("Host process needs a parent IPC channel"));
81
+ return;
82
+ }
83
+ process.send(message, (error) => (error === null ? resolve() : reject(error)));
84
+ });
85
+ }
86
+
87
+ export async function hostResponse(url: string, path: string, signal: AbortSignal) {
88
+ const response = await fetch(new URL(path, url), { redirect: "manual", signal });
89
+ return { body: await response.text(), headers: Object.fromEntries(response.headers), status: response.status };
90
+ }
@@ -0,0 +1,36 @@
1
+ import { Effect, Schema } from "effect";
2
+ import { forkHost, type HostProcessOptions, hostResponse, receiveHostMessage, stopHost } from "#testing/adapters/hostProcess.ts";
3
+ import { HostProcessError } from "./HostProcessError.ts";
4
+ import { HostReady, HostSignal } from "./hostProcessProtocol.ts";
5
+
6
+ export const hostProcess = Effect.fn("HostProcess.hostProcess")(function* (options: HostProcessOptions) {
7
+ const child = yield* Effect.acquireRelease(
8
+ Effect.try({ catch: (cause) => new HostProcessError({ cause, operation: "launch" }), try: () => forkHost(options) }),
9
+ (acquired) => Effect.promise(() => stopHost(acquired)),
10
+ );
11
+ const { pid, url } = yield* Effect.tryPromise({
12
+ catch: (cause) => new HostProcessError({ cause, operation: "launch" }),
13
+ try: (signal) => receiveHostMessage(child, HostReady, signal),
14
+ });
15
+ function response(path: string) {
16
+ return Effect.tryPromise({
17
+ catch: (cause) => new HostProcessError({ cause, operation: "request" }),
18
+ try: (signal) => hostResponse(url, path, signal),
19
+ });
20
+ }
21
+ return {
22
+ headers: (path: string) => response(path).pipe(Effect.map((answer) => answer.headers)),
23
+ health: () => response("/health"),
24
+ pid,
25
+ signal: (delivered: NodeJS.Signals) =>
26
+ Effect.tryPromise({
27
+ catch: (cause) => new HostProcessError({ cause, operation: "signal" }),
28
+ try: (signal) =>
29
+ receiveHostMessage(child, HostSignal.pipe(Schema.check(Schema.makeFilter((ack) => ack.signal === delivered))), signal, () => {
30
+ child.kill(delivered);
31
+ }),
32
+ }).pipe(Effect.asVoid),
33
+ stop: () => Effect.promise(() => stopHost(child)),
34
+ url,
35
+ };
36
+ });
@@ -0,0 +1,21 @@
1
+ import process from "node:process";
2
+ import { Effect } from "effect";
3
+ import { HttpServer } from "effect/http";
4
+ import { sendHostMessage } from "#testing/adapters/hostProcess.ts";
5
+ import { HostProcessError } from "./HostProcessError.ts";
6
+
7
+ export const reportHostReady = HttpServer.HttpServer.pipe(
8
+ Effect.flatMap((server) =>
9
+ Effect.tryPromise({
10
+ catch: (cause) => new HostProcessError({ cause, operation: "launch" }),
11
+ try: () => sendHostMessage({ pid: process.pid, type: "host-ready", url: HttpServer.formatAddress(server.address) }),
12
+ }),
13
+ ),
14
+ );
15
+
16
+ export function acknowledgeHostSignal(signal: NodeJS.Signals) {
17
+ return Effect.tryPromise({
18
+ catch: (cause) => new HostProcessError({ cause, operation: "signal" }),
19
+ try: () => sendHostMessage({ signal, type: "host-signal" }),
20
+ });
21
+ }
@@ -0,0 +1,4 @@
1
+ import { Schema } from "effect";
2
+
3
+ export const HostReady = Schema.Struct({ pid: Schema.Number, type: Schema.Literal("host-ready"), url: Schema.String });
4
+ export const HostSignal = Schema.Struct({ signal: Schema.String, type: Schema.Literal("host-signal") });
@@ -0,0 +1,13 @@
1
+ import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
2
+ import { Layer } from "effect";
3
+ import { appDatabase } from "#appDatabase.ts";
4
+ import type { AppDatabaseOptions } from "#appHost.ts";
5
+
6
+ export function workerHost<R, E, TRequirements, TServices, TServiceError, TServiceRequirements, TMigrations = never, TMigrationError = never>(
7
+ options: AppDatabaseOptions<TMigrations, TMigrationError> & {
8
+ readonly workers: Layer.Layer<R, E, TRequirements>;
9
+ readonly services: Layer.Layer<TServices, TServiceError, TServiceRequirements>;
10
+ },
11
+ ) {
12
+ return options.workers.pipe(Layer.provideMerge(options.services), Layer.provideMerge(appDatabase(options)), Layer.provide(NodeCrypto.layer));
13
+ }