@shivaedev/server 0.22.0 → 0.24.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 (56) hide show
  1. package/CHANGELOG.md +26 -0
  2. package/README.md +46 -18
  3. package/dist/AppHostFactory.d.ts +2 -2
  4. package/dist/AppHostFactory.d.ts.map +1 -1
  5. package/dist/adapters/localTraceDatabase.d.ts +6 -0
  6. package/dist/adapters/localTraceDatabase.d.ts.map +1 -0
  7. package/dist/adapters/localTraceDatabase.js +75 -0
  8. package/dist/adapters/localTraceDatabase.js.map +1 -0
  9. package/dist/appHost.d.ts +3 -6
  10. package/dist/appHost.d.ts.map +1 -1
  11. package/dist/appHost.js +2 -2
  12. package/dist/appHost.js.map +1 -1
  13. package/dist/appHostHttp.d.ts +129 -75
  14. package/dist/appHostHttp.d.ts.map +1 -1
  15. package/dist/appHostHttp.js +5 -5
  16. package/dist/appHostHttp.js.map +1 -1
  17. package/dist/development.js +1 -1
  18. package/dist/development.js.map +1 -1
  19. package/dist/hostOptions.d.ts +11 -2
  20. package/dist/hostOptions.d.ts.map +1 -1
  21. package/dist/hostRpcRoutes.d.ts +18 -9
  22. package/dist/hostRpcRoutes.d.ts.map +1 -1
  23. package/dist/hostRpcRoutes.js +6 -3
  24. package/dist/hostRpcRoutes.js.map +1 -1
  25. package/dist/hostSensitiveKeys.d.ts +12 -0
  26. package/dist/hostSensitiveKeys.d.ts.map +1 -0
  27. package/dist/hostSensitiveKeys.js +8 -0
  28. package/dist/hostSensitiveKeys.js.map +1 -0
  29. package/dist/hostVersionLayer.d.ts +2 -0
  30. package/dist/hostVersionLayer.d.ts.map +1 -0
  31. package/dist/hostVersionLayer.js +8 -0
  32. package/dist/hostVersionLayer.js.map +1 -0
  33. package/dist/localTraceRow.d.ts +29 -0
  34. package/dist/localTraceRow.d.ts.map +1 -0
  35. package/dist/localTraceRow.js +51 -0
  36. package/dist/localTraceRow.js.map +1 -0
  37. package/dist/localTracing.d.ts +10 -0
  38. package/dist/localTracing.d.ts.map +1 -0
  39. package/dist/localTracing.js +44 -0
  40. package/dist/localTracing.js.map +1 -0
  41. package/dist/otlpTracing.d.ts.map +1 -1
  42. package/dist/otlpTracing.js +14 -1
  43. package/dist/otlpTracing.js.map +1 -1
  44. package/package.json +20 -20
  45. package/src/AppHostFactory.ts +8 -8
  46. package/src/adapters/localTraceDatabase.ts +74 -0
  47. package/src/appHost.ts +21 -23
  48. package/src/appHostHttp.ts +5 -5
  49. package/src/development.ts +1 -1
  50. package/src/hostOptions.ts +10 -6
  51. package/src/hostRpcRoutes.ts +7 -2
  52. package/src/hostSensitiveKeys.ts +11 -0
  53. package/src/hostVersionLayer.ts +8 -0
  54. package/src/localTraceRow.ts +58 -0
  55. package/src/localTracing.ts +63 -0
  56. package/src/otlpTracing.ts +18 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shivaedev/server",
3
- "version": "0.22.0",
3
+ "version": "0.24.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"
@@ -20,14 +20,14 @@
20
20
  "imports": {
21
21
  "#test/*": "./src/test-support/*",
22
22
  "#*.ts": {
23
- "source": "./src/*.ts",
23
+ "@shivaedev/source": "./src/*.ts",
24
24
  "types": "./dist/*.d.ts",
25
25
  "default": "./dist/*.js"
26
26
  }
27
27
  },
28
28
  "exports": {
29
29
  "./*.ts": {
30
- "source": "./src/*.ts",
30
+ "@shivaedev/source": "./src/*.ts",
31
31
  "types": "./dist/*.d.ts",
32
32
  "default": "./dist/*.js"
33
33
  },
@@ -45,26 +45,26 @@
45
45
  "README.md"
46
46
  ],
47
47
  "dependencies": {
48
- "@shivaedev/changes": "0.9.0",
49
- "@shivaedev/contract": "0.34.0",
50
- "@shivaedev/errors": "0.8.0",
51
- "@shivaedev/http": "0.5.0",
52
- "@shivaedev/identity": "0.23.0",
53
- "@shivaedev/local-postgres": "0.12.0",
54
- "@shivaedev/sql": "0.36.1"
48
+ "@shivaedev/changes": "0.10.0",
49
+ "@shivaedev/contract": "0.36.0",
50
+ "@shivaedev/errors": "0.9.0",
51
+ "@shivaedev/http": "0.6.0",
52
+ "@shivaedev/identity": "0.24.0",
53
+ "@shivaedev/local-postgres": "0.13.0",
54
+ "@shivaedev/sql": "0.37.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
- "@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",
60
+ "@shivaedev/access": "0.5.0",
61
+ "@shivaedev/admin": "0.20.0",
62
+ "@shivaedev/jobs": "0.35.0",
63
+ "@shivaedev/quality": "0.60.0",
64
+ "@shivaedev/rate-limit": "0.4.0",
65
+ "@shivaedev/test-kits": "0.3.0",
66
+ "@shivaedev/test-sql": "0.20.0",
67
+ "@shivaedev/test-story": "0.38.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.4",
80
- "@shivaedev/test-sql": "0.18.2",
79
+ "@shivaedev/admin": "0.20.0",
80
+ "@shivaedev/test-sql": "0.20.0",
81
81
  "better-auth": "1.7.14",
82
82
  "effect": "4.0.6"
83
83
  },
@@ -13,10 +13,10 @@ type PreflightHost<THost extends Layer.Any, TServices, TError, TRequirements> =
13
13
 
14
14
  export interface AppHostFactory {
15
15
  <
16
- TRpcs extends Rpc.Any,
17
- THandlers,
18
- THandlerError,
19
- THandlerRequirements,
16
+ TRpcs extends Rpc.Any = never,
17
+ THandlers = never,
18
+ THandlerError = never,
19
+ THandlerRequirements = never,
20
20
  TServices = never,
21
21
  TServiceError = never,
22
22
  TServiceRequirements = never,
@@ -72,10 +72,10 @@ export interface AppHostFactory {
72
72
  >;
73
73
 
74
74
  <
75
- TRpcs extends Rpc.Any,
76
- THandlers,
77
- THandlerError,
78
- THandlerRequirements,
75
+ TRpcs extends Rpc.Any = never,
76
+ THandlers = never,
77
+ THandlerError = never,
78
+ THandlerRequirements = never,
79
79
  TServices = never,
80
80
  TServiceError = never,
81
81
  TServiceRequirements = never,
@@ -0,0 +1,74 @@
1
+ import { randomUUID } from "node:crypto";
2
+ import { mkdirSync } from "node:fs";
3
+ import { dirname } from "node:path";
4
+ import { DatabaseSync } from "node:sqlite";
5
+ import { type LocalTraceRow, traceJson } from "#localTraceRow.ts";
6
+
7
+ export function localTraceDatabase(path: string, retainedRuns: number, indexedAttributes: readonly string[], startedAtMillis: number) {
8
+ mkdirSync(dirname(path), { recursive: true });
9
+ const db = new DatabaseSync(path, { timeout: 1000 });
10
+ try {
11
+ db.exec(`
12
+ PRAGMA journal_mode = WAL;
13
+ PRAGMA foreign_keys = ON;
14
+ CREATE TABLE IF NOT EXISTS runs (id TEXT PRIMARY KEY, started_at_millis INTEGER NOT NULL, completed INTEGER NOT NULL DEFAULT 0);
15
+ CREATE TABLE IF NOT EXISTS spans (
16
+ run_id TEXT NOT NULL REFERENCES runs(id) ON DELETE CASCADE,
17
+ span_id TEXT PRIMARY KEY, trace_id TEXT NOT NULL, parent_span_id TEXT,
18
+ name TEXT NOT NULL, started_at_millis INTEGER NOT NULL, data TEXT NOT NULL
19
+ );
20
+ CREATE INDEX IF NOT EXISTS spans_trace ON spans(trace_id, started_at_millis);
21
+ CREATE TABLE IF NOT EXISTS span_attributes (
22
+ span_id TEXT NOT NULL REFERENCES spans(span_id) ON DELETE CASCADE,
23
+ key TEXT NOT NULL, value TEXT NOT NULL
24
+ );
25
+ CREATE INDEX IF NOT EXISTS span_attributes_lookup ON span_attributes(key, value, span_id);
26
+ CREATE VIEW IF NOT EXISTS logs AS
27
+ SELECT run_id, trace_id, parent_span_id AS span_id, started_at_millis AS at_millis,
28
+ json_extract(data, '$.events[0].attributes') AS data FROM spans WHERE name = 'effect.log';
29
+ `);
30
+ const runId = randomUUID();
31
+ db.prepare("INSERT INTO runs(id, started_at_millis) VALUES (?, ?)").run(runId, startedAtMillis);
32
+ const complete = db.prepare("UPDATE runs SET completed = (SELECT coalesce(max(completed), 0) + 1 FROM runs) WHERE id = ?");
33
+ const prune = db.prepare(
34
+ "DELETE FROM runs WHERE completed > 0 AND id NOT IN (SELECT id FROM runs WHERE completed > 0 ORDER BY completed DESC LIMIT ?)",
35
+ );
36
+ const insert = db.prepare("INSERT INTO spans VALUES (?, ?, ?, ?, ?, ?, ?)");
37
+ const index = db.prepare("INSERT INTO span_attributes VALUES (?, ?, ?)");
38
+ const indexed = new Set(indexedAttributes);
39
+ function append(row: LocalTraceRow) {
40
+ insert.run(runId, row.spanId, row.traceId, row.parentSpanId, row.name, row.startedAtMillis, traceJson(row));
41
+ for (const key of indexed) {
42
+ const value = row.attributes[key];
43
+ if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
44
+ index.run(row.spanId, key, String(value));
45
+ }
46
+ }
47
+ }
48
+ return {
49
+ close: () => {
50
+ try {
51
+ complete.run(runId);
52
+ prune.run(retainedRuns);
53
+ } finally {
54
+ db.close();
55
+ }
56
+ },
57
+ write: (rows: readonly LocalTraceRow[]) => {
58
+ db.exec("BEGIN");
59
+ try {
60
+ for (const row of rows) {
61
+ append(row);
62
+ }
63
+ db.exec("COMMIT");
64
+ } catch (error) {
65
+ db.exec("ROLLBACK");
66
+ throw error;
67
+ }
68
+ },
69
+ };
70
+ } catch (error) {
71
+ db.close();
72
+ throw error;
73
+ }
74
+ }
package/src/appHost.ts CHANGED
@@ -1,14 +1,13 @@
1
1
  import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
2
2
  import { Effect, Layer } from "effect";
3
3
  import type { Rpc } from "effect/rpc";
4
- import { schemaSensitiveKeys } from "@shivaedev/errors/schemaSensitiveKeys.ts";
5
4
  import { isSensitiveKey } from "@shivaedev/errors/sensitive.ts";
6
- import type { SessionPolicy } from "@shivaedev/identity/server/SessionPolicy.ts";
7
5
  import type { AppHostFactory } from "#AppHostFactory.ts";
8
6
  import type { AppHostInput, HostPreflight } from "#AppHostInput.ts";
9
7
  import { appDatabase, type DatabaseConfig } from "#appDatabase.ts";
10
8
  import { appHostHttp, deferredHost, migratedHost } from "#appHostHttp.ts";
11
9
  import type { HostCoreOptions, HostPolicy, HostRouteRequirements, HostRoutes } from "#hostOptions.ts";
10
+ import { hostSensitiveKeys } from "#hostSensitiveKeys.ts";
12
11
  import type { MigrationOptions } from "#MigrationOptions.ts";
13
12
  import { redactingDiagnostics } from "#redactingDiagnostics.ts";
14
13
 
@@ -18,10 +17,10 @@ export interface AppDatabaseOptions<R = never, E = never> {
18
17
  }
19
18
 
20
19
  export type AppHostOptions<
21
- TRpcs extends Rpc.Any,
22
- THandlers,
23
- THandlerError,
24
- THandlerRequirements,
20
+ TRpcs extends Rpc.Any = never,
21
+ THandlers = never,
22
+ THandlerError = never,
23
+ THandlerRequirements = never,
25
24
  TServices = never,
26
25
  TServiceError = never,
27
26
  TServiceRequirements = never,
@@ -34,17 +33,17 @@ export type AppHostOptions<
34
33
  TMigrationError = never,
35
34
  > = HostPreflight<TPreflight, TPreflightError, TPreflightRequirements> &
36
35
  HostCoreOptions<TRpcs, THandlers, THandlerError, THandlerRequirements, TServices, TServiceError, TServiceRequirements> &
37
- HostRoutes<TRouteError, TRouteRequirements> & {
36
+ HostRoutes<TRouteError, TRouteRequirements> &
37
+ HostPolicy & {
38
38
  readonly migrations: AppDatabaseOptions<TMigrations, TMigrationError>["migrations"];
39
39
  readonly policy?: undefined;
40
- readonly sessions: Layer.Layer<SessionPolicy>;
41
40
  };
42
41
 
43
42
  export type AppLocalHostOptions<
44
- TRpcs extends Rpc.Any,
45
- THandlers,
46
- THandlerError,
47
- THandlerRequirements,
43
+ TRpcs extends Rpc.Any = never,
44
+ THandlers = never,
45
+ THandlerError = never,
46
+ THandlerRequirements = never,
48
47
  TServices = never,
49
48
  TServiceError = never,
50
49
  TServiceRequirements = never,
@@ -64,10 +63,10 @@ export type AppLocalHostOptions<
64
63
  };
65
64
 
66
65
  export type AppHttpHostOptions<
67
- TRpcs extends Rpc.Any,
68
- THandlers,
69
- THandlerError,
70
- THandlerRequirements,
66
+ TRpcs extends Rpc.Any = never,
67
+ THandlers = never,
68
+ THandlerError = never,
69
+ THandlerRequirements = never,
71
70
  TServices = never,
72
71
  TServiceError = never,
73
72
  TServiceRequirements = never,
@@ -81,10 +80,10 @@ export type AppHttpHostOptions<
81
80
  HostRoutes<TRouteError, TRouteRequirements> &
82
81
  HostPolicy & { readonly migrations?: undefined };
83
82
  export const appHost: AppHostFactory = <
84
- TRpcs extends Rpc.Any,
85
- THandlers,
86
- THandlerError,
87
- THandlerRequirements,
83
+ TRpcs extends Rpc.Any = never,
84
+ THandlers = never,
85
+ THandlerError = never,
86
+ THandlerRequirements = never,
88
87
  TServices = never,
89
88
  TServiceError = never,
90
89
  TServiceRequirements = never,
@@ -122,9 +121,8 @@ export const appHost: AppHostFactory = <
122
121
  startup === undefined
123
122
  ? Layer.empty
124
123
  : Layer.unwrap(
125
- Effect.map(
126
- "application" in options ? Effect.succeed(isSensitiveKey) : schemaSensitiveKeys(options.group, options.config?.sensitiveKeys),
127
- (sensitive) => startup.pipe(Layer.provide(redactingDiagnostics(sensitive))),
124
+ Effect.map("application" in options ? Effect.succeed(isSensitiveKey) : hostSensitiveKeys(options), (sensitive) =>
125
+ startup.pipe(Layer.provide(redactingDiagnostics(sensitive))),
128
126
  ),
129
127
  );
130
128
  if ("application" in options) {
@@ -1,7 +1,6 @@
1
1
  import { Effect, Layer } from "effect";
2
2
  import { HttpRouter, HttpServer } from "effect/http";
3
3
  import type { Rpc } from "effect/rpc";
4
- import { schemaSensitiveKeys } from "@shivaedev/errors/schemaSensitiveKeys.ts";
5
4
  import { hostHttpServices } from "#adapters/hostHttpServices.ts";
6
5
  import { nodeHttpServer } from "#adapters/nodeHttpServer.ts";
7
6
  import type { appDatabase } from "#appDatabase.ts";
@@ -12,6 +11,7 @@ import { hostApplicationRoutes } from "#hostApplicationRoutes.ts";
12
11
  import { hostAssets } from "#hostAssets.ts";
13
12
  import type { HostOptions, HostPolicy, HostRouteRequirements } from "#hostOptions.ts";
14
13
  import { hostRpcRoutes } from "#hostRpcRoutes.ts";
14
+ import { hostSensitiveKeys } from "#hostSensitiveKeys.ts";
15
15
  import { hostTransport } from "#hostTransport.ts";
16
16
  import type { MigrationOptions } from "#MigrationOptions.ts";
17
17
  import { nativeClientOrigins } from "#nativeClientOrigins.ts";
@@ -43,14 +43,14 @@ export function appHostHttp<
43
43
  > &
44
44
  HostPolicy,
45
45
  ) {
46
+ const assets = options.assets === undefined ? Layer.empty : hostAssets(options.assets);
46
47
  return Layer.unwrap(
47
- Effect.map(schemaSensitiveKeys(options.group, options.config?.sensitiveKeys), (sensitive) =>
48
+ Effect.map(hostSensitiveKeys(options), (sensitive) =>
48
49
  Layer.unwrap(
49
50
  Effect.gen(function* () {
50
51
  const transport = yield* hostTransport(options);
51
52
  const { bodySize, host, trustedProxy, nativeOrigins } = transport;
52
- const routes = hostRpcRoutes(options, transport, sensitive);
53
- const assets = options.assets === undefined ? Layer.empty : hostAssets(options.assets);
53
+ const routes = options.group === undefined ? Layer.empty : hostRpcRoutes(options, transport, sensitive);
54
54
  const application = Layer.mergeAll(options.services ?? Layer.empty);
55
55
  const sessions = Layer.mergeAll(Layer.empty, ...(options.sessions === undefined ? [] : [options.sessions]));
56
56
  const server = nodeHttpServer(host, { ...options.config, maxBodySize: bodySize });
@@ -62,7 +62,7 @@ export function appHostHttp<
62
62
  assets,
63
63
  applicationRoutes,
64
64
  ).pipe(
65
- Layer.provide(nativeClientOrigins(nativeOrigins, trustedProxy, false, options.rpcPath)),
65
+ Layer.provide(options.group === undefined ? Layer.empty : nativeClientOrigins(nativeOrigins, trustedProxy, false, options.rpcPath)),
66
66
  Layer.provide(nativeClientOrigins(nativeOrigins, trustedProxy, options.nativeOrigins === undefined ? "preflight" : true)),
67
67
  );
68
68
  return Layer.unwrap(
@@ -12,7 +12,7 @@ export interface DevelopmentOptions {
12
12
 
13
13
  export const development = Effect.fn("Platform.development")(function* ({ server, worker, conditions = [] }: DevelopmentOptions) {
14
14
  const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
15
- const options = { stderr: "inherit", stdout: "inherit" } as const;
15
+ const options = { env: { "NODE_ENV": "development" }, extendEnv: true, stderr: "inherit", stdout: "inherit" } as const;
16
16
  const args = [...conditions.map((condition) => `--conditions=${condition}`), "--watch"];
17
17
  return yield* Effect.raceAll([
18
18
  spawner.exitCode(ChildProcess.make(process.execPath, [...args, server], options)),
@@ -12,6 +12,7 @@ export type HostCoreOptions<TRpcs extends Rpc.Any, THandlers, THandlerError, THa
12
12
  readonly assets?: string | AssetOptions;
13
13
  readonly browserErrors?: BrowserErrorOptions;
14
14
  readonly config?: AppDatabaseOptions["config"] & {
15
+ readonly buildVersion?: string;
15
16
  readonly port?: number;
16
17
  readonly socketPath?: string;
17
18
  readonly host?: string;
@@ -21,14 +22,16 @@ export type HostCoreOptions<TRpcs extends Rpc.Any, THandlers, THandlerError, THa
21
22
  readonly disablePreemptiveShutdown?: boolean;
22
23
  readonly gracefulShutdownTimeout?: Duration.Input;
23
24
  };
24
- readonly group: RpcGroup.RpcGroup<TRpcs>;
25
- readonly handlers: Layer.Layer<THandlers, THandlerError, THandlerRequirements>;
26
25
  readonly nativeOrigins?: readonly string[];
27
26
  readonly rpcPath?: HttpRouter.PathInput;
28
27
  } & (
29
- | { readonly services: Layer.Layer<TServices, TServiceError, TServiceRequirements> }
30
- | ([TServices | TServiceError | TServiceRequirements] extends [never] ? { readonly services?: never } : never)
31
- );
28
+ | { readonly group: RpcGroup.RpcGroup<TRpcs>; readonly handlers: Layer.Layer<THandlers, THandlerError, THandlerRequirements> }
29
+ | { readonly group?: never; readonly handlers?: never }
30
+ ) &
31
+ (
32
+ | { readonly services: Layer.Layer<TServices, TServiceError, TServiceRequirements> }
33
+ | ([TServices | TServiceError | TServiceRequirements] extends [never] ? { readonly services?: never } : never)
34
+ );
32
35
 
33
36
  export interface HostRoutes<TRouteError = never, TRouteRequirements = HostRouteRequirements> {
34
37
  readonly routes?: Layer.Layer<never, TRouteError, TRouteRequirements>;
@@ -49,4 +52,5 @@ export type HostOptions<
49
52
 
50
53
  export type HostPolicy =
51
54
  | { readonly policy?: undefined; readonly sessions: Layer.Layer<SessionPolicy> }
52
- | { readonly policy: "loopback"; readonly sessions?: never };
55
+ | { readonly policy: "loopback"; readonly sessions?: never }
56
+ | { readonly group?: never; readonly policy?: undefined; readonly sessions?: never };
@@ -2,6 +2,7 @@ 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";
@@ -14,6 +15,7 @@ import { hostChanges } from "#hostChanges.ts";
14
15
  import type { HostPolicy } from "#hostOptions.ts";
15
16
  import { hostRpc, rpcCacheControl } from "#hostRpc.ts";
16
17
  import type { hostTransport } from "#hostTransport.ts";
18
+ import { hostVersionLayer } from "#hostVersionLayer.ts";
17
19
  import { redactingRpcSerialization } from "#redactingRpcSerialization.ts";
18
20
  import { rpcRequestTracing } from "#rpcRequestTracing.ts";
19
21
  import { socketRpc } from "#socketRpc.ts";
@@ -23,6 +25,7 @@ type BuiltRoutes<TRpcs extends Rpc.Any, THandlers, TError, TRequirements> = Retu
23
25
  typeof buildRpcRoutes<TRpcs, THandlers, TError, TRequirements>
24
26
  >;
25
27
  type RouteOptions<TRpcs extends Rpc.Any, THandlers, TError, TRequirements> = {
28
+ readonly config?: { readonly buildVersion?: string };
26
29
  readonly group: RpcGroup.RpcGroup<TRpcs>;
27
30
  readonly handlers: Layer.Layer<THandlers, TError, TRequirements>;
28
31
  readonly rpcPath?: HttpRouter.PathInput;
@@ -63,9 +66,10 @@ function buildRpcRoutes<TRpcs extends Rpc.Any, THandlers, TError, TRequirements>
63
66
  Effect.gen(function* () {
64
67
  yield* validateReadPolicies(options.group);
65
68
  const wire = redactingRpcSerialization(yield* RpcSerialization.RpcSerialization, sensitive);
66
- const handler = yield* hostRpc(options.group).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));
67
71
  const guarded = Effect.flatMap(UnixSocketRequest, (unix) => (unix ? handler : localRpcGuard(handler, trustedProxy, nativeOrigins)));
68
- const websocket = yield* hostRpc(options.group, true).pipe(Effect.provideService(RpcSerialization.RpcSerialization, wire));
72
+ const websocket = yield* hostRpc(group, true).pipe(Effect.provideService(RpcSerialization.RpcSerialization, wire));
69
73
  yield* router.add(
70
74
  "OPTIONS",
71
75
  options.rpcPath ?? "/rpc",
@@ -98,6 +102,7 @@ function buildRpcRoutes<TRpcs extends Rpc.Any, THandlers, TError, TRequirements>
98
102
  }),
99
103
  ).pipe(
100
104
  Layer.provide(Layer.merge(middleware, hostChanges(options.group).pipe(Layer.provideMerge(options.handlers)))),
105
+ Layer.provide(hostVersionLayer(options.config?.buildVersion)),
101
106
  Layer.provide(RpcSerialization.layerNdjson),
102
107
  );
103
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
+ }
@@ -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
+ }
@@ -0,0 +1,58 @@
1
+ import { Cause, Exit, Option, type Tracer } from "effect";
2
+ import { redactRecord } from "@shivaedev/errors/redact.ts";
3
+ import { redactDiagnosticCause } from "@shivaedev/errors/redactCause.ts";
4
+ import { redactText, type SensitiveKey } from "@shivaedev/errors/sensitive.ts";
5
+ import { type HostRedactionPolicy, redactionForRpc } from "#RedactionPolicy.ts";
6
+
7
+ export type LocalTraceEvent = readonly [name: string, time: bigint, attributes: Record<string, unknown>];
8
+
9
+ function rpcMethod(span: Tracer.Span): unknown {
10
+ const method = span.attributes.get("rpc.method");
11
+ const parent = Option.getOrUndefined(span.parent);
12
+ return method ?? (parent?._tag === "Span" ? rpcMethod(parent) : undefined);
13
+ }
14
+
15
+ function traceError(cause: Cause.Cause<unknown>, sensitive: SensitiveKey): string {
16
+ return Cause.prettyErrors(cause)
17
+ .map((error) => Cause.pretty(redactDiagnosticCause(Cause.fail(error), sensitive)))
18
+ .join("\n");
19
+ }
20
+
21
+ export function localTraceRow(span: Tracer.Span, events: readonly LocalTraceEvent[], policy: HostRedactionPolicy) {
22
+ if (span.status._tag !== "Ended" || !span.sampled) {
23
+ return undefined;
24
+ }
25
+ const sensitive = redactionForRpc(policy, rpcMethod(span));
26
+ const { startTime, endTime, exit } = span.status;
27
+ const attributes = redactRecord(Object.fromEntries(span.attributes), sensitive);
28
+ const failedStatus = Exit.isFailure(exit) && Cause.hasInterruptsOnly(exit.cause) ? "interrupted" : "failure";
29
+ return {
30
+ attributes,
31
+ durationNanos: String(endTime - startTime),
32
+ endedAtMillis: Number.parseInt(String(endTime / 1_000_000n), 10),
33
+ error: Exit.isFailure(exit) ? redactText(traceError(exit.cause, sensitive), sensitive) : null,
34
+ events: events.map(([name, time, values]) => ({
35
+ attributes: redactRecord(values, sensitive),
36
+ name: redactText(name, sensitive),
37
+ time: String(time),
38
+ })),
39
+ kind: span.kind,
40
+ links: span.links.map(({ span: linked, attributes: values }) => ({
41
+ attributes: redactRecord(values, sensitive),
42
+ spanId: linked.spanId,
43
+ traceId: linked.traceId,
44
+ })),
45
+ name: redactText(span.name, sensitive),
46
+ parentSpanId: Option.getOrUndefined(span.parent)?.spanId ?? null,
47
+ spanId: span.spanId,
48
+ startedAtMillis: Number.parseInt(String(startTime / 1_000_000n), 10),
49
+ status: Exit.isSuccess(exit) ? "success" : failedStatus,
50
+ traceId: span.traceId,
51
+ };
52
+ }
53
+
54
+ export type LocalTraceRow = NonNullable<ReturnType<typeof localTraceRow>>;
55
+
56
+ export function traceJson(value: unknown): string {
57
+ return JSON.stringify(value, (_key, nested: unknown) => (typeof nested === "bigint" ? String(nested) : nested));
58
+ }
@@ -0,0 +1,63 @@
1
+ import { Clock, Effect, Layer, Tracer } from "effect";
2
+ import { localTraceDatabase } from "#adapters/localTraceDatabase.ts";
3
+ import { traceDiagnostics } from "#adapters/traceDiagnostics.ts";
4
+ import { type LocalTraceEvent, type LocalTraceRow, localTraceRow } from "#localTraceRow.ts";
5
+ import { RedactionPolicy } from "#RedactionPolicy.ts";
6
+ import { redactingDiagnostics } from "#redactingDiagnostics.ts";
7
+
8
+ export interface LocalTracingOptions {
9
+ readonly clock?: Clock.Clock;
10
+ readonly flushInterval?: number;
11
+ readonly indexedAttributes?: readonly string[];
12
+ readonly path?: string;
13
+ readonly retainedRuns?: number;
14
+ }
15
+
16
+ export function localTracing({
17
+ clock = Clock.Clock.defaultValue(),
18
+ path = ".platform/traces.db",
19
+ indexedAttributes = [],
20
+ retainedRuns = 5,
21
+ flushInterval = 1000,
22
+ }: LocalTracingOptions = {}) {
23
+ return Layer.unwrap(
24
+ Effect.gen(function* () {
25
+ const policy = yield* RedactionPolicy;
26
+ const underlying = yield* Tracer.Tracer;
27
+ const startedAtMillis = yield* Clock.currentTimeMillis;
28
+ const database = yield* Effect.acquireRelease(
29
+ Effect.try(() => localTraceDatabase(path, retainedRuns, indexedAttributes, startedAtMillis)),
30
+ (db) => Effect.sync(() => db.close()),
31
+ );
32
+ let pending: LocalTraceRow[] = [];
33
+ const flush = Effect.sync(() => {
34
+ database.write(pending);
35
+ pending = [];
36
+ });
37
+ yield* Effect.addFinalizer(() => flush);
38
+ yield* Effect.sleep(flushInterval).pipe(Effect.andThen(flush), Effect.forever, Effect.provideService(Clock.Clock, clock), Effect.forkScoped);
39
+ const tracer = Tracer.make({
40
+ ...underlying,
41
+ span: (options) => {
42
+ const span = underlying.span(options);
43
+ const events: LocalTraceEvent[] = [];
44
+ const event = span.event.bind(span);
45
+ const end = span.end.bind(span);
46
+ span.event = (name, time, attributes) => {
47
+ events.push([name, time, attributes ?? {}]);
48
+ event(name, time, attributes);
49
+ };
50
+ span.end = (time, exit) => {
51
+ end(time, exit);
52
+ const row = localTraceRow(span, events, policy);
53
+ if (row !== undefined) {
54
+ pending.push(row);
55
+ }
56
+ };
57
+ return span;
58
+ },
59
+ });
60
+ return redactingDiagnostics(policy).pipe(Layer.provideMerge(traceDiagnostics), Layer.provideMerge(Layer.succeed(Tracer.Tracer, tracer)));
61
+ }),
62
+ );
63
+ }
@@ -1,6 +1,7 @@
1
- import { Config, Context, Effect, Layer, Schema } from "effect";
1
+ import { Config, ConfigProvider, Context, Effect, Layer, Schema } from "effect";
2
2
  import { OtlpSerialization, OtlpTracer } from "effect/observability";
3
3
  import { traceDiagnostics } from "#adapters/traceDiagnostics.ts";
4
+ import { localTracing } from "#localTracing.ts";
4
5
  import { otlpHttpClient } from "#otlpHttpClient.ts";
5
6
  import { RedactionPolicy } from "#RedactionPolicy.ts";
6
7
  import { redactingDiagnostics } from "#redactingDiagnostics.ts";
@@ -22,6 +23,22 @@ export const otlpTracing: Layer.Layer<never, Config.ConfigError> = Layer.effectC
22
23
  Config.withDefault(undefined),
23
24
  ));
24
25
  if (endpoint === undefined) {
26
+ const mode = yield* Config.String("NODE_ENV").pipe(Config.withDefault("production"));
27
+ if (mode === "development") {
28
+ const path = yield* Config.String("PLATFORM_TRACE_STORE").pipe(Config.withDefault(".platform/traces.db"));
29
+ const indexed = yield* Config.String("PLATFORM_TRACE_INDEXED_ATTRIBUTES").pipe(Config.withDefault(""));
30
+ return yield* Layer.build(
31
+ localTracing({
32
+ indexedAttributes: indexed
33
+ .split(",")
34
+ .map((key) => key.trim())
35
+ .filter(Boolean),
36
+ path,
37
+ }),
38
+ ).pipe(
39
+ Effect.mapError((cause) => new Config.ConfigError(new ConfigProvider.SourceError({ cause, message: "Cannot open local trace store" }))),
40
+ );
41
+ }
25
42
  return Context.empty();
26
43
  }
27
44
  const configuredProtocol = yield* Config.String("OTEL_EXPORTER_OTLP_TRACES_PROTOCOL").pipe(