@shivaedev/server 0.36.0 → 0.38.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.
- package/CHANGELOG.md +30 -0
- package/README.md +33 -14
- package/dist/AppModule.d.ts +3 -2
- package/dist/AppModule.d.ts.map +1 -1
- package/dist/adapters/migrationsIn.d.ts +5 -0
- package/dist/adapters/migrationsIn.d.ts.map +1 -0
- package/dist/adapters/migrationsIn.js +8 -0
- package/dist/adapters/migrationsIn.js.map +1 -0
- package/dist/appDatabaseHttp.d.ts +10 -2
- package/dist/appDatabaseHttp.d.ts.map +1 -1
- package/dist/appHost.d.ts +5 -1
- package/dist/appHost.d.ts.map +1 -1
- package/dist/appHostHttp.d.ts +5 -1
- package/dist/appHostHttp.d.ts.map +1 -1
- package/dist/appHostHttp.js +3 -7
- package/dist/appHostHttp.js.map +1 -1
- package/dist/appModules.d.ts +7 -5
- package/dist/appModules.d.ts.map +1 -1
- package/dist/appModules.js +9 -6
- package/dist/appModules.js.map +1 -1
- package/dist/hostRpc.d.ts +5 -1
- package/dist/hostRpc.d.ts.map +1 -1
- package/dist/hostRpcRoutes.d.ts +5 -1
- package/dist/hostRpcRoutes.d.ts.map +1 -1
- package/dist/providedModules.d.ts +24 -0
- package/dist/providedModules.d.ts.map +1 -0
- package/dist/providedModules.js +2 -0
- package/dist/providedModules.js.map +1 -0
- package/dist/redactedHost.d.ts +11 -0
- package/dist/redactedHost.d.ts.map +1 -0
- package/dist/redactedHost.js +11 -0
- package/dist/redactedHost.js.map +1 -0
- package/dist/workerHost.d.ts +6 -2
- package/dist/workerHost.d.ts.map +1 -1
- package/dist/workerHost.js +5 -2
- package/dist/workerHost.js.map +1 -1
- package/package.json +29 -29
- package/src/AppModule.ts +5 -4
- package/src/adapters/migrationsIn.ts +8 -0
- package/src/appHostHttp.ts +29 -40
- package/src/appModules.ts +21 -12
- package/src/providedModules.ts +38 -0
- package/src/redactedHost.ts +26 -0
- package/src/workerHost.ts +17 -3
package/src/appHostHttp.ts
CHANGED
|
@@ -1,21 +1,17 @@
|
|
|
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 { RepositoryModels } from "@shivaedev/sql/RepositoryModels.ts";
|
|
5
4
|
import { hostHttpServices } from "#adapters/hostHttpServices.ts";
|
|
6
5
|
import { nodeHttpServer } from "#adapters/nodeHttpServer.ts";
|
|
7
|
-
import { redactingDiagnostics } from "#adapters/redactingDiagnostics.ts";
|
|
8
6
|
import { browserErrorRoute } from "#browserErrorRoute.ts";
|
|
9
7
|
import { healthRoute } from "#healthRoute.ts";
|
|
10
8
|
import { hostApplicationRoutes } from "#hostApplicationRoutes.ts";
|
|
11
9
|
import { hostAssets } from "#hostAssets.ts";
|
|
12
10
|
import type { HostOptions, HostPolicy, HostRouteRequirements } from "#hostOptions.ts";
|
|
13
11
|
import { hostRpcRoutes } from "#hostRpcRoutes.ts";
|
|
14
|
-
import { hostSensitiveKeys } from "#hostSensitiveKeys.ts";
|
|
15
12
|
import { hostTransport } from "#hostTransport.ts";
|
|
16
13
|
import { nativeClientOrigins } from "#nativeClientOrigins.ts";
|
|
17
|
-
import {
|
|
18
|
-
import { RedactionPolicy } from "#RedactionPolicy.ts";
|
|
14
|
+
import { redactedHost } from "#redactedHost.ts";
|
|
19
15
|
|
|
20
16
|
export function appHostHttp<
|
|
21
17
|
TRpcs extends Rpc.Any,
|
|
@@ -46,41 +42,34 @@ export function appHostHttp<
|
|
|
46
42
|
HostPolicy,
|
|
47
43
|
) {
|
|
48
44
|
const assets = options.assets === undefined ? Layer.empty : hostAssets(options.assets);
|
|
49
|
-
return
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
}),
|
|
78
|
-
).pipe(
|
|
79
|
-
Layer.provide(redactingDiagnostics(sensitive)),
|
|
80
|
-
Layer.provide(otlpTracing),
|
|
81
|
-
Layer.provide(Layer.succeed(RedactionPolicy, sensitive)),
|
|
82
|
-
Layer.provide(Layer.succeed(RepositoryModels, sensitive.models)),
|
|
83
|
-
),
|
|
45
|
+
return redactedHost(options, (sensitive) =>
|
|
46
|
+
Layer.unwrap(
|
|
47
|
+
Effect.gen(function* () {
|
|
48
|
+
const transport = yield* hostTransport(options);
|
|
49
|
+
const { bodySize, host, trustedProxy, nativeOrigins } = transport;
|
|
50
|
+
const routes = options.group === undefined ? Layer.empty : hostRpcRoutes(options, transport, sensitive);
|
|
51
|
+
const application = Layer.mergeAll(options.services ?? Layer.empty);
|
|
52
|
+
const sessions = Layer.mergeAll(Layer.empty, ...(options.sessions === undefined ? [] : [options.sessions]));
|
|
53
|
+
const server = nodeHttpServer(host, { ...options.config, maxBodySize: bodySize });
|
|
54
|
+
const applicationRoutes = hostApplicationRoutes(options.routes, bodySize);
|
|
55
|
+
const httpRoutes = Layer.mergeAll(
|
|
56
|
+
routes,
|
|
57
|
+
healthRoute,
|
|
58
|
+
browserErrorRoute(trustedProxy, options.browserErrors),
|
|
59
|
+
assets,
|
|
60
|
+
applicationRoutes,
|
|
61
|
+
).pipe(
|
|
62
|
+
Layer.provide(options.group === undefined ? Layer.empty : nativeClientOrigins(nativeOrigins, trustedProxy, false, options.rpcPath)),
|
|
63
|
+
Layer.provide(nativeClientOrigins(nativeOrigins, trustedProxy, options.nativeOrigins === undefined ? "preflight" : true)),
|
|
64
|
+
);
|
|
65
|
+
return Layer.unwrap(
|
|
66
|
+
HttpRouter.toHttpEffect(httpRoutes).pipe(
|
|
67
|
+
// Building the routes built every repository, so the policy is complete before the server takes a request.
|
|
68
|
+
Effect.tap(() => sensitive.complete),
|
|
69
|
+
Effect.map((handler) => HttpServer.serve(handler).pipe(HttpServer.withLogAddress, Layer.provideMerge(server))),
|
|
70
|
+
),
|
|
71
|
+
).pipe(Layer.provideMerge(application), Layer.provide(sessions), Layer.provideMerge(hostHttpServices));
|
|
72
|
+
}),
|
|
84
73
|
),
|
|
85
74
|
);
|
|
86
75
|
}
|
package/src/appModules.ts
CHANGED
|
@@ -9,6 +9,7 @@ import type { SchemaComponent } from "@shivaedev/sql/SchemaComponent.ts";
|
|
|
9
9
|
import type { AppModule, ModuleAdmin, ModuleLayer } from "#AppModule.ts";
|
|
10
10
|
import { DuplicateModuleQueue } from "#DuplicateModuleQueue.ts";
|
|
11
11
|
import type { MigrationOptions } from "#MigrationOptions.ts";
|
|
12
|
+
import type { ProvidedModules } from "#providedModules.ts";
|
|
12
13
|
import type { GlobalServices } from "#requestServices.ts";
|
|
13
14
|
|
|
14
15
|
type ModuleField<TModules, TKey extends PropertyKey> = TModules extends unknown ? (TKey extends keyof TModules ? TModules[TKey] : never) : never;
|
|
@@ -17,7 +18,7 @@ type Layers<TModules extends readonly AppModule[], TKey extends "handlers" | "se
|
|
|
17
18
|
Layer.Any
|
|
18
19
|
>;
|
|
19
20
|
type ServedHandlers<TModule> = TModule extends { readonly module: infer TDefined extends AnyModule } ? ModuleHandlers<TDefined> : never;
|
|
20
|
-
// A registration typed as AppModule has lost its module's
|
|
21
|
+
// A registration typed as AppModule has lost its module's services, so ServedHandlers would serve it as requiring nothing.
|
|
21
22
|
// Only a tuple is checked: inference sees the bare constraint, and a check that refuses it breaks inference of every registration list.
|
|
22
23
|
type WidenedModule<TModule> = TModule extends { readonly module: infer TDefined } ? (TDefined extends AnyModule ? never : TModule) : never;
|
|
23
24
|
type ExactModules<TModules extends readonly AppModule[]> = number extends TModules["length"]
|
|
@@ -25,7 +26,7 @@ type ExactModules<TModules extends readonly AppModule[]> = number extends TModul
|
|
|
25
26
|
: {
|
|
26
27
|
readonly [TKey in keyof TModules]: [WidenedModule<TModules[TKey]>] extends [never]
|
|
27
28
|
? unknown
|
|
28
|
-
: { readonly "Declare a defined module's registration with satisfies AppModule so its type keeps the module's
|
|
29
|
+
: { readonly "Declare a defined module's registration with satisfies AppModule so its type keeps the module's services": never };
|
|
29
30
|
};
|
|
30
31
|
type Groups<TModules extends readonly AppModule[]> = Extract<
|
|
31
32
|
ModuleField<TModules[number], "group"> | ModuleField<TModules[number], "module">,
|
|
@@ -42,14 +43,16 @@ type Ports<TModules extends readonly AppModule[]> = Layers<TModules, "ports">;
|
|
|
42
43
|
type RequestLayer<TModule> = TModule extends { readonly requestServices: infer TLayer extends Layer.Any } ? TLayer : never;
|
|
43
44
|
type RequestServices<TModules extends readonly AppModule[]> = RequestLayer<TModules[number]>;
|
|
44
45
|
type Services<TModules extends readonly AppModule[]> = Layers<TModules, "services">;
|
|
45
|
-
type
|
|
46
|
+
type Migrations<TModules extends readonly AppModule[]> = ModuleField<TModules[number], "migrations">;
|
|
47
|
+
type MigrationRequirements<TModules extends readonly AppModule[]> = Effect.Services<ModuleField<Migrations<TModules>, "loader">>;
|
|
46
48
|
type Components<T> = T extends readonly (infer TComponent extends SchemaComponent<unknown, unknown>)[] ? TComponent : never;
|
|
47
|
-
type ModuleComponents<TModules extends AppModule> = Components<ModuleField<TModules
|
|
49
|
+
type ModuleComponents<TModules extends readonly AppModule[]> = Components<ModuleField<Migrations<TModules>, "components">>;
|
|
48
50
|
|
|
49
51
|
export function appModules<const TModules extends readonly AppModule[], R = never, E = never, TRequirements = never>(
|
|
50
52
|
modules: TModules &
|
|
51
53
|
GlobalServices<Layer.Services<Services<NoInfer<TModules>>>, Layer.Success<RequestServices<NoInfer<TModules>>>> &
|
|
52
|
-
ExactModules<NoInfer<TModules
|
|
54
|
+
ExactModules<NoInfer<TModules>> &
|
|
55
|
+
ProvidedModules<NoInfer<TModules>>,
|
|
53
56
|
options?: { readonly replacements?: Layer.Layer<R, E, TRequirements> },
|
|
54
57
|
): Effect.Effect<
|
|
55
58
|
{
|
|
@@ -74,7 +77,7 @@ export function appModules<const TModules extends readonly AppModule[], R = neve
|
|
|
74
77
|
>;
|
|
75
78
|
readonly migrations: {
|
|
76
79
|
readonly loader: Migrator.Loader<MigrationRequirements<TModules>>;
|
|
77
|
-
readonly components: readonly ModuleComponents<TModules
|
|
80
|
+
readonly components: readonly ModuleComponents<TModules>[];
|
|
78
81
|
};
|
|
79
82
|
},
|
|
80
83
|
DuplicateModuleQueue
|
|
@@ -104,9 +107,9 @@ const composeModules = Effect.fnUntraced(function* (modules: readonly AppModule[
|
|
|
104
107
|
for (const queue of module.queues ?? []) {
|
|
105
108
|
const firstModule = owners.get(queue.name);
|
|
106
109
|
if (firstModule !== undefined) {
|
|
107
|
-
return yield* new DuplicateModuleQueue({ firstModule, queue: queue.name, secondModule: module
|
|
110
|
+
return yield* new DuplicateModuleQueue({ firstModule, queue: queue.name, secondModule: moduleName(module) });
|
|
108
111
|
}
|
|
109
|
-
owners.set(queue.name, module
|
|
112
|
+
owners.set(queue.name, moduleName(module));
|
|
110
113
|
}
|
|
111
114
|
}
|
|
112
115
|
const queues = modules.flatMap((module) => module.queues ?? []);
|
|
@@ -117,18 +120,20 @@ const composeModules = Effect.fnUntraced(function* (modules: readonly AppModule[
|
|
|
117
120
|
return {
|
|
118
121
|
admin,
|
|
119
122
|
group: RpcGroup.make().merge(...modules.map((module) => (module.module === undefined ? module.group : module.module)), admin.group),
|
|
120
|
-
// The overload types each module's handlers from its
|
|
123
|
+
// The overload types each module's handlers from its operations' services; at run time moduleHandlers reads only its operations.
|
|
121
124
|
handlers: Layer.mergeAll(
|
|
122
125
|
admin.handlers,
|
|
123
126
|
...modules.map((module) =>
|
|
124
127
|
module.module === undefined
|
|
125
128
|
? module.handlers
|
|
126
|
-
: moduleHandlers({ "~requires":
|
|
129
|
+
: moduleHandlers({ "~requires": {}, application: module.module.application, declaration: module.module.declaration }),
|
|
127
130
|
),
|
|
128
131
|
),
|
|
129
132
|
migrations: {
|
|
130
|
-
components: modules.flatMap((module) => module.migrations
|
|
131
|
-
loader: Effect.all(modules.
|
|
133
|
+
components: modules.flatMap((module) => module.migrations?.components ?? []),
|
|
134
|
+
loader: Effect.all(modules.flatMap((module) => (module.migrations === undefined ? [] : [module.migrations.loader]))).pipe(
|
|
135
|
+
Effect.map((entries) => entries.flat()),
|
|
136
|
+
),
|
|
132
137
|
},
|
|
133
138
|
queues,
|
|
134
139
|
requestServices: Layer.mergeAll(Layer.empty, ...modules.map((module) => module.requestServices ?? Layer.empty)),
|
|
@@ -136,3 +141,7 @@ const composeModules = Effect.fnUntraced(function* (modules: readonly AppModule[
|
|
|
136
141
|
workers: Layer.mergeAll(Layer.empty, ...modules.map((module) => module.workers ?? Layer.empty)),
|
|
137
142
|
};
|
|
138
143
|
});
|
|
144
|
+
|
|
145
|
+
function moduleName(module: AppModule) {
|
|
146
|
+
return module.module === undefined ? module.name : module.module.declaration.name;
|
|
147
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { Layer } from "effect";
|
|
2
|
+
import type { AnyModule } from "@shivaedev/contract/moduleHandlers.ts";
|
|
3
|
+
import type { AppModule } from "#AppModule.ts";
|
|
4
|
+
import type { hostHttpServices } from "#adapters/hostHttpServices.ts";
|
|
5
|
+
import type { appDatabase } from "#appDatabase.ts";
|
|
6
|
+
|
|
7
|
+
export interface OperationServicesNotProvided<TModule extends string, TOperation extends PropertyKey, TMissing> {
|
|
8
|
+
readonly missingServices: TMissing;
|
|
9
|
+
readonly module: TModule;
|
|
10
|
+
readonly operation: TOperation;
|
|
11
|
+
readonly problem: "run or readPolicy uses services that no registration's services, ports or requestServices provide and the host does not provide";
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
type HostServices = Layer.Success<ReturnType<typeof appDatabase<never, never>>> | Layer.Success<typeof hostHttpServices>;
|
|
15
|
+
type Field<TModule, TKey extends PropertyKey> = TModule extends unknown ? (TKey extends keyof TModule ? TModule[TKey] : never) : never;
|
|
16
|
+
type Provided<TModules extends readonly AppModule[]> =
|
|
17
|
+
| Layer.Success<Extract<Field<TModules[number], "services" | "ports" | "requestServices">, Layer.Any>>
|
|
18
|
+
| HostServices;
|
|
19
|
+
type Unprovided<TDefined extends AnyModule, TProvided> = {
|
|
20
|
+
readonly [TOperation in keyof TDefined["~requires"]]: [Exclude<TDefined["~requires"][TOperation], TProvided>] extends [never]
|
|
21
|
+
? never
|
|
22
|
+
: OperationServicesNotProvided<TDefined["declaration"]["name"], TOperation, Exclude<TDefined["~requires"][TOperation], TProvided>>;
|
|
23
|
+
}[keyof TDefined["~requires"]];
|
|
24
|
+
// Rebuilding each problem drops the Unprovided alias, so the compiler's message shows the problem itself.
|
|
25
|
+
type Shown<TProblem> =
|
|
26
|
+
TProblem extends OperationServicesNotProvided<infer TName, infer TOperation, infer TMissing>
|
|
27
|
+
? OperationServicesNotProvided<TName, TOperation, TMissing>
|
|
28
|
+
: never;
|
|
29
|
+
type ModuleProblems<TModule, TProvided> = TModule extends { readonly module: infer TDefined extends AnyModule }
|
|
30
|
+
? Shown<Unprovided<TDefined, TProvided>>
|
|
31
|
+
: never;
|
|
32
|
+
|
|
33
|
+
// Replacements stay out: they override ports, and a generic replacements Layer would defer the check until it refused every list.
|
|
34
|
+
export type ProvidedModules<TModules extends readonly AppModule[]> = number extends TModules["length"]
|
|
35
|
+
? unknown
|
|
36
|
+
: [ModuleProblems<TModules[number], Provided<TModules>>] extends [never]
|
|
37
|
+
? unknown
|
|
38
|
+
: ModuleProblems<TModules[number], Provided<TModules>>;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { Effect, Layer } from "effect";
|
|
2
|
+
import type { Rpc, RpcGroup } from "effect/rpc";
|
|
3
|
+
import { RepositoryModels } from "@shivaedev/sql/RepositoryModels.ts";
|
|
4
|
+
import { redactingDiagnostics } from "#adapters/redactingDiagnostics.ts";
|
|
5
|
+
import { hostSensitiveKeys } from "#hostSensitiveKeys.ts";
|
|
6
|
+
import { otlpTracing } from "#otlpTracing.ts";
|
|
7
|
+
import { RedactionPolicy } from "#RedactionPolicy.ts";
|
|
8
|
+
|
|
9
|
+
export type HostSensitivePolicy = Effect.Success<ReturnType<typeof hostSensitiveKeys>>;
|
|
10
|
+
|
|
11
|
+
// Both hosts build their process inside one policy, so repositories declare their models to the same policy that masks every log, error report and exported span.
|
|
12
|
+
export function redactedHost<TRpcs extends Rpc.Any, A, E, R>(
|
|
13
|
+
options: { readonly group?: RpcGroup.RpcGroup<TRpcs>; readonly config?: { readonly sensitiveKeys?: readonly string[] } },
|
|
14
|
+
host: (sensitive: HostSensitivePolicy) => Layer.Layer<A, E, R>,
|
|
15
|
+
) {
|
|
16
|
+
return Layer.unwrap(
|
|
17
|
+
Effect.map(hostSensitiveKeys(options), (sensitive) =>
|
|
18
|
+
host(sensitive).pipe(
|
|
19
|
+
Layer.provide(redactingDiagnostics(sensitive)),
|
|
20
|
+
Layer.provide(otlpTracing),
|
|
21
|
+
Layer.provide(Layer.succeed(RedactionPolicy, sensitive)),
|
|
22
|
+
Layer.provide(Layer.succeed(RepositoryModels, sensitive.models)),
|
|
23
|
+
),
|
|
24
|
+
),
|
|
25
|
+
);
|
|
26
|
+
}
|
package/src/workerHost.ts
CHANGED
|
@@ -1,12 +1,26 @@
|
|
|
1
1
|
import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
|
|
2
|
-
import { Layer } from "effect";
|
|
3
|
-
import { type AppDatabaseOptions, appDatabase } from "#appDatabase.ts";
|
|
2
|
+
import { Effect, Layer } from "effect";
|
|
3
|
+
import { type AppDatabaseOptions, appDatabase, type DatabaseConfig } from "#appDatabase.ts";
|
|
4
|
+
import { redactedHost } from "#redactedHost.ts";
|
|
5
|
+
|
|
6
|
+
export interface WorkerHostConfig extends DatabaseConfig {
|
|
7
|
+
readonly sensitiveKeys?: readonly string[];
|
|
8
|
+
}
|
|
4
9
|
|
|
5
10
|
export function workerHost<R, E, TRequirements, TServices, TServiceError, TServiceRequirements, TMigrations = never, TMigrationError = never>(
|
|
6
11
|
options: AppDatabaseOptions<TMigrations, TMigrationError> & {
|
|
12
|
+
readonly config?: WorkerHostConfig;
|
|
7
13
|
readonly workers: Layer.Layer<R, E, TRequirements>;
|
|
8
14
|
readonly services: Layer.Layer<TServices, TServiceError, TServiceRequirements>;
|
|
9
15
|
},
|
|
10
16
|
) {
|
|
11
|
-
|
|
17
|
+
// A worker serves no RPC group, so its policy holds the built-in rules, config.sensitiveKeys and the models its repositories declare as they are built.
|
|
18
|
+
// The host announces readiness itself: an effect composed around the returned Layer runs outside its policy and exporter.
|
|
19
|
+
return redactedHost(options, () =>
|
|
20
|
+
Layer.effectDiscard(Effect.logInfo("Worker ready to take jobs")).pipe(
|
|
21
|
+
Layer.provideMerge(options.workers),
|
|
22
|
+
Layer.provideMerge(options.services),
|
|
23
|
+
Layer.provideMerge(appDatabase(options)),
|
|
24
|
+
),
|
|
25
|
+
).pipe(Layer.provide(NodeCrypto.layer));
|
|
12
26
|
}
|