@shivaedev/server 0.16.1 → 0.18.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 +39 -0
- package/README.md +35 -3
- package/dist/AppHostFactory.d.ts +3 -4
- package/dist/AppHostFactory.d.ts.map +1 -1
- package/dist/AppHostInput.d.ts +3 -4
- package/dist/AppHostInput.d.ts.map +1 -1
- package/dist/adapters/boundedOtlpResponse.d.ts +4 -0
- package/dist/adapters/boundedOtlpResponse.d.ts.map +1 -0
- package/dist/adapters/boundedOtlpResponse.js +19 -0
- package/dist/adapters/boundedOtlpResponse.js.map +1 -0
- package/dist/appDatabase.d.ts +1 -1
- package/dist/appDatabase.d.ts.map +1 -1
- package/dist/appDatabase.js +4 -3
- package/dist/appDatabase.js.map +1 -1
- package/dist/appHost.d.ts +5 -4
- package/dist/appHost.d.ts.map +1 -1
- package/dist/appHost.js.map +1 -1
- package/dist/appHostHttp.d.ts +58 -41
- package/dist/appHostHttp.d.ts.map +1 -1
- package/dist/appHostHttp.js +3 -2
- package/dist/appHostHttp.js.map +1 -1
- package/dist/bearerSocketRpc.d.ts +2 -2
- package/dist/bearerSocketRpc.d.ts.map +1 -1
- package/dist/bearerSocketRpc.js +5 -4
- package/dist/bearerSocketRpc.js.map +1 -1
- package/dist/browserErrorRoute.d.ts +6 -1
- package/dist/browserErrorRoute.d.ts.map +1 -1
- package/dist/browserErrorRoute.js +38 -8
- package/dist/browserErrorRoute.js.map +1 -1
- package/dist/hostChanges.d.ts +1 -2
- package/dist/hostChanges.d.ts.map +1 -1
- package/dist/hostChanges.js +14 -9
- package/dist/hostChanges.js.map +1 -1
- package/dist/hostOptions.d.ts +5 -3
- package/dist/hostOptions.d.ts.map +1 -1
- package/dist/hostRpcRoutes.d.ts +15 -7
- package/dist/hostRpcRoutes.d.ts.map +1 -1
- package/dist/hostRpcRoutes.js +6 -3
- package/dist/hostRpcRoutes.js.map +1 -1
- package/dist/otlpHttpClient.d.ts.map +1 -1
- package/dist/otlpHttpClient.js +14 -9
- package/dist/otlpHttpClient.js.map +1 -1
- package/dist/otlpTracing.d.ts.map +1 -1
- package/dist/otlpTracing.js +7 -4
- package/dist/otlpTracing.js.map +1 -1
- package/dist/socketRpc.d.ts +5 -2
- package/dist/socketRpc.d.ts.map +1 -1
- package/dist/socketRpc.js +4 -3
- package/dist/socketRpc.js.map +1 -1
- package/dist/trimPostgresPageCache.d.ts +10 -0
- package/dist/trimPostgresPageCache.d.ts.map +1 -0
- package/dist/trimPostgresPageCache.js +25 -0
- package/dist/trimPostgresPageCache.js.map +1 -0
- package/package.json +15 -15
- package/src/AppHostFactory.ts +0 -7
- package/src/AppHostInput.ts +0 -5
- package/src/adapters/boundedOtlpResponse.ts +27 -0
- package/src/appDatabase.ts +4 -3
- package/src/appHost.ts +3 -7
- package/src/appHostHttp.ts +11 -9
- package/src/bearerSocketRpc.ts +6 -6
- package/src/browserErrorRoute.ts +51 -8
- package/src/hostChanges.ts +15 -21
- package/src/hostOptions.ts +5 -3
- package/src/hostRpcRoutes.ts +36 -16
- package/src/otlpHttpClient.ts +19 -15
- package/src/otlpTracing.ts +12 -11
- package/src/socketRpc.ts +19 -6
- package/src/trimPostgresPageCache.ts +31 -0
package/dist/socketRpc.js
CHANGED
|
@@ -3,6 +3,7 @@ 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
5
|
import { localTransport } from "@shivaedev/identity/server/local.ts";
|
|
6
|
+
import { SessionPolicy } from "@shivaedev/identity/server/SessionPolicy.ts";
|
|
6
7
|
import { resolveSession } from "@shivaedev/identity/server/session.ts";
|
|
7
8
|
import { makeSocketSession, SocketSession } from "@shivaedev/identity/server/socketSession.ts";
|
|
8
9
|
import { bearerSocketRpc } from "#bearerSocketRpc.ts";
|
|
@@ -14,10 +15,10 @@ export function socketRpc(handler, policy, trustedProxy, nativeOrigins) {
|
|
|
14
15
|
}
|
|
15
16
|
if (policy.policy !== "loopback") {
|
|
16
17
|
if (new URL(request.url, "http://localhost").searchParams.get("auth") === "frame") {
|
|
17
|
-
return yield* bearerSocketRpc(handler
|
|
18
|
+
return yield* bearerSocketRpc(handler);
|
|
18
19
|
}
|
|
19
|
-
yield* resolveSession(
|
|
20
|
-
const session = yield* makeSocketSession(
|
|
20
|
+
yield* resolveSession(Headers.empty, "$connect");
|
|
21
|
+
const session = yield* makeSocketSession((yield* SessionPolicy).streamRevalidationInterval ?? "30 seconds");
|
|
21
22
|
return yield* Effect.provideService(handler, SocketSession, session);
|
|
22
23
|
}
|
|
23
24
|
return yield* handler;
|
package/dist/socketRpc.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"socketRpc.js","sourceRoot":"","sources":["../src/socketRpc.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAc,MAAM,QAAQ,CAAC;AAC5C,OAAO,EAAE,OAAO,EAAE,iBAAiB,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AAC7E,OAAO,KAAK,UAAU,MAAM,wBAAwB,CAAC;AACrD,OAAO,EAAE,iBAAiB,EAAE,WAAW,EAAE,MAAM,+BAA+B,CAAC;AAC/E,OAAO,EAAE,cAAc,EAAE,MAAM,qCAAqC,CAAC;AACrE,OAAO,EAAE,cAAc,
|
|
1
|
+
{"version":3,"file":"socketRpc.js","sourceRoot":"","sources":["../src/socketRpc.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAc,MAAM,QAAQ,CAAC;AAC5C,OAAO,EAAE,OAAO,EAAE,iBAAiB,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AAC7E,OAAO,KAAK,UAAU,MAAM,wBAAwB,CAAC;AACrD,OAAO,EAAE,iBAAiB,EAAE,WAAW,EAAE,MAAM,+BAA+B,CAAC;AAC/E,OAAO,EAAE,cAAc,EAAE,MAAM,qCAAqC,CAAC;AACrE,OAAO,EAAE,aAAa,EAAE,MAAM,6CAA6C,CAAC;AAC5E,OAAO,EAAE,cAAc,EAAE,MAAM,uCAAuC,CAAC;AACvE,OAAO,EAAE,iBAAiB,EAAE,aAAa,EAAE,MAAM,6CAA6C,CAAC;AAC/F,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AAetD,MAAM,UAAU,SAAS,CACxB,OAAuH,EACvH,MAAkB,EAClB,YAAgC,EAChC,aAAgC;IAEhC,OAAO,MAAM,CAAC,GAAG,CAAC,QAAQ,CAAC;QAC1B,MAAM,OAAO,GAAG,KAAK,CAAC,CAAC,iBAAiB,CAAC,iBAAiB,CAAC;QAC3D,IAAI,CAAC,cAAc,CAAC,YAAY,EAAE,aAAa,CAAC,CAAC,OAAO,CAAC,EAAE,CAAC;YAC3D,OAAO,kBAAkB,CAAC,IAAI,CAAC,iCAAiC,EAAE,EAAE,MAAM,EAAE,UAAU,CAAC,WAAW,CAAC,WAAW,CAAC,EAAE,CAAC,CAAC;QACpH,CAAC;QACD,IAAI,MAAM,CAAC,MAAM,KAAK,UAAU,EAAE,CAAC;YAClC,IAAI,IAAI,GAAG,CAAC,OAAO,CAAC,GAAG,EAAE,kBAAkB,CAAC,CAAC,YAAY,CAAC,GAAG,CAAC,MAAM,CAAC,KAAK,OAAO,EAAE,CAAC;gBACnF,OAAO,KAAK,CAAC,CAAC,eAAe,CAAC,OAAO,CAAC,CAAC;YACxC,CAAC;YACD,KAAK,CAAC,CAAC,cAAc,CAAC,OAAO,CAAC,KAAK,EAAE,UAAU,CAAC,CAAC;YACjD,MAAM,OAAO,GAAG,KAAK,CAAC,CAAC,iBAAiB,CAAC,CAAC,KAAK,CAAC,CAAC,aAAa,CAAC,CAAC,0BAA0B,IAAI,YAAY,CAAC,CAAC;YAC5G,OAAO,KAAK,CAAC,CAAC,MAAM,CAAC,cAAc,CAAC,OAAO,EAAE,aAAa,EAAE,OAAO,CAAC,CAAC;QACtE,CAAC;QACD,OAAO,KAAK,CAAC,CAAC,OAAO,CAAC;IACvB,CAAC,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,iBAAiB,EAAE,eAAe,CAAC,EAAE,MAAM,CAAC,OAAO,CAAC,WAAW,EAAE,SAAS,CAAC,CAAC,CAAC;AACrG,CAAC;AAED,SAAS,eAAe;IACvB,OAAO,MAAM,CAAC,OAAO,CAAC,kBAAkB,CAAC,KAAK,CAAC,EAAE,MAAM,EAAE,UAAU,CAAC,WAAW,CAAC,oBAAoB,CAAC,EAAE,CAAC,CAAC,CAAC;AAC3G,CAAC;AACD,SAAS,SAAS;IACjB,OAAO,MAAM,CAAC,OAAO,CAAC,kBAAkB,CAAC,KAAK,CAAC,EAAE,MAAM,EAAE,UAAU,CAAC,WAAW,CAAC,WAAW,CAAC,EAAE,CAAC,CAAC,CAAC;AAClG,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Effect, Schema } from "effect";
|
|
2
|
+
import { ChildProcessSpawner } from "effect/process";
|
|
3
|
+
declare const PostgresPageCacheTrimError_base: Schema.Class<PostgresPageCacheTrimError, Schema.TaggedStruct<"PostgresPageCacheTrimError", {
|
|
4
|
+
readonly exitCode: Schema.Number;
|
|
5
|
+
}>, import("effect/Cause").YieldableError>;
|
|
6
|
+
export declare class PostgresPageCacheTrimError extends PostgresPageCacheTrimError_base {
|
|
7
|
+
}
|
|
8
|
+
export declare const trimPostgresPageCache: (dataDirectory: string) => Effect.Effect<undefined, import("effect/PlatformError").PlatformError | PostgresPageCacheTrimError, ChildProcessSpawner.ChildProcessSpawner>;
|
|
9
|
+
export {};
|
|
10
|
+
//# sourceMappingURL=trimPostgresPageCache.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"trimPostgresPageCache.d.ts","sourceRoot":"","sources":["../src/trimPostgresPageCache.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AACxC,OAAO,EAAgB,mBAAmB,EAAE,MAAM,gBAAgB,CAAC;;;;AAEnE,qBAAa,0BAA2B,SAAQ,+BAE9C;CAAG;AAEL,eAAO,MAAM,qBAAqB,yKAuBhC,CAAC"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { Effect, Schema } from "effect";
|
|
2
|
+
import { ChildProcess, ChildProcessSpawner } from "effect/process";
|
|
3
|
+
export class PostgresPageCacheTrimError extends Schema.TaggedError()("PostgresPageCacheTrimError", {
|
|
4
|
+
exitCode: Schema.Number,
|
|
5
|
+
}) {
|
|
6
|
+
}
|
|
7
|
+
export const trimPostgresPageCache = Effect.fn("Server.trimPostgresPageCache")(function* (dataDirectory) {
|
|
8
|
+
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
|
|
9
|
+
const code = yield* spawner.exitCode(ChildProcess.make("find", [
|
|
10
|
+
dataDirectory,
|
|
11
|
+
"-type",
|
|
12
|
+
"f",
|
|
13
|
+
"-exec",
|
|
14
|
+
"sh",
|
|
15
|
+
"-c",
|
|
16
|
+
'for path do dd if="$path" iflag=nocache count=0 status=none || exit; done',
|
|
17
|
+
"sh",
|
|
18
|
+
"{}",
|
|
19
|
+
"+",
|
|
20
|
+
], { stderr: "inherit", stdout: "ignore" }));
|
|
21
|
+
if (code !== 0) {
|
|
22
|
+
return yield* new PostgresPageCacheTrimError({ exitCode: Number(code) });
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
//# sourceMappingURL=trimPostgresPageCache.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"trimPostgresPageCache.js","sourceRoot":"","sources":["../src/trimPostgresPageCache.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AACxC,OAAO,EAAE,YAAY,EAAE,mBAAmB,EAAE,MAAM,gBAAgB,CAAC;AAEnE,MAAM,OAAO,0BAA2B,SAAQ,MAAM,CAAC,WAAW,EAA8B,CAAC,4BAA4B,EAAE;IAC9H,QAAQ,EAAE,MAAM,CAAC,MAAM;CACvB,CAAC;CAAG;AAEL,MAAM,CAAC,MAAM,qBAAqB,GAAG,MAAM,CAAC,EAAE,CAAC,8BAA8B,CAAC,CAAC,QAAQ,CAAC,EAAE,aAAqB;IAC9G,MAAM,OAAO,GAAG,KAAK,CAAC,CAAC,mBAAmB,CAAC,mBAAmB,CAAC;IAC/D,MAAM,IAAI,GAAG,KAAK,CAAC,CAAC,OAAO,CAAC,QAAQ,CACnC,YAAY,CAAC,IAAI,CAChB,MAAM,EACN;QACC,aAAa;QACb,OAAO;QACP,GAAG;QACH,OAAO;QACP,IAAI;QACJ,IAAI;QACJ,2EAA2E;QAC3E,IAAI;QACJ,IAAI;QACJ,GAAG;KACH,EACD,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,CACvC,CACD,CAAC;IACF,IAAI,IAAI,KAAK,CAAC,EAAE,CAAC;QAChB,OAAO,KAAK,CAAC,CAAC,IAAI,0BAA0B,CAAC,EAAE,QAAQ,EAAE,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;IAC1E,CAAC;AACF,CAAC,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@shivaedev/server",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.18.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,25 @@
|
|
|
45
45
|
"README.md"
|
|
46
46
|
],
|
|
47
47
|
"dependencies": {
|
|
48
|
-
"@shivaedev/changes": "0.
|
|
49
|
-
"@shivaedev/contract": "0.30.
|
|
50
|
-
"@shivaedev/errors": "0.
|
|
51
|
-
"@shivaedev/http": "0.
|
|
52
|
-
"@shivaedev/identity": "0.
|
|
53
|
-
"@shivaedev/local-postgres": "0.
|
|
54
|
-
"@shivaedev/sql": "0.
|
|
48
|
+
"@shivaedev/changes": "0.7.0",
|
|
49
|
+
"@shivaedev/contract": "0.30.2",
|
|
50
|
+
"@shivaedev/errors": "0.5.0",
|
|
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"
|
|
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.3.
|
|
61
|
-
"@shivaedev/admin": "0.
|
|
62
|
-
"@shivaedev/jobs": "0.29.
|
|
63
|
-
"@shivaedev/quality": "0.
|
|
64
|
-
"@shivaedev/test": "0.12.
|
|
65
|
-
"@shivaedev/test-sql": "0.
|
|
66
|
-
"@shivaedev/test-story": "0.
|
|
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",
|
|
67
67
|
"@types/node": "24.19.2",
|
|
68
68
|
"better-auth": "1.7.12",
|
|
69
69
|
"effect": "4.0.6",
|
package/src/AppHostFactory.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import type { Crypto, Layer } from "effect";
|
|
2
2
|
import type { Rpc } from "effect/rpc";
|
|
3
|
-
import type { SessionShape } from "@shivaedev/identity/server/session.ts";
|
|
4
3
|
import type { AppHostInput } from "#AppHostInput.ts";
|
|
5
4
|
import type { AppDatabaseOptions, AppHttpHostOptions } from "#appHost.ts";
|
|
6
5
|
import type { appHostHttp, deferredHost } from "#appHostHttp.ts";
|
|
@@ -22,7 +21,6 @@ export interface AppHostFactory {
|
|
|
22
21
|
TServiceError = never,
|
|
23
22
|
TServiceRequirements = never,
|
|
24
23
|
TMigrations = never,
|
|
25
|
-
TSession extends SessionShape = SessionShape,
|
|
26
24
|
TApplicationError = never,
|
|
27
25
|
TApplicationRequirements = never,
|
|
28
26
|
TPreflight = never,
|
|
@@ -41,7 +39,6 @@ export interface AppHostFactory {
|
|
|
41
39
|
TServiceError,
|
|
42
40
|
TServiceRequirements,
|
|
43
41
|
TMigrations,
|
|
44
|
-
TSession,
|
|
45
42
|
TApplicationError,
|
|
46
43
|
TApplicationRequirements,
|
|
47
44
|
TPreflight,
|
|
@@ -62,7 +59,6 @@ export interface AppHostFactory {
|
|
|
62
59
|
TServiceError,
|
|
63
60
|
TServiceRequirements,
|
|
64
61
|
TMigrations,
|
|
65
|
-
TSession,
|
|
66
62
|
TApplicationError,
|
|
67
63
|
TApplicationRequirements,
|
|
68
64
|
TRouteError,
|
|
@@ -83,7 +79,6 @@ export interface AppHostFactory {
|
|
|
83
79
|
TServices = never,
|
|
84
80
|
TServiceError = never,
|
|
85
81
|
TServiceRequirements = never,
|
|
86
|
-
TSession extends SessionShape = SessionShape,
|
|
87
82
|
TPreflight = never,
|
|
88
83
|
TPreflightError = never,
|
|
89
84
|
TPreflightRequirements = never,
|
|
@@ -98,7 +93,6 @@ export interface AppHostFactory {
|
|
|
98
93
|
TServices,
|
|
99
94
|
TServiceError,
|
|
100
95
|
TServiceRequirements,
|
|
101
|
-
TSession,
|
|
102
96
|
TPreflight,
|
|
103
97
|
TPreflightError,
|
|
104
98
|
TPreflightRequirements,
|
|
@@ -115,7 +109,6 @@ export interface AppHostFactory {
|
|
|
115
109
|
TServices,
|
|
116
110
|
TServiceError,
|
|
117
111
|
TServiceRequirements,
|
|
118
|
-
TSession,
|
|
119
112
|
TRouteError,
|
|
120
113
|
TRouteRequirements
|
|
121
114
|
>
|
package/src/AppHostInput.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import type { Effect, Layer } from "effect";
|
|
2
2
|
import type { Rpc } from "effect/rpc";
|
|
3
|
-
import type { SessionShape } from "@shivaedev/identity/server/session.ts";
|
|
4
3
|
import type { AppDatabaseOptions, AppHostOptions, AppHttpHostOptions, AppLocalHostOptions } from "#appHost.ts";
|
|
5
4
|
import type { HostRouteRequirements } from "#hostOptions.ts";
|
|
6
5
|
|
|
@@ -17,7 +16,6 @@ export type AppHostInput<
|
|
|
17
16
|
TServiceError = never,
|
|
18
17
|
TServiceRequirements = never,
|
|
19
18
|
TMigrations = never,
|
|
20
|
-
TSession extends SessionShape = SessionShape,
|
|
21
19
|
TApplicationError = never,
|
|
22
20
|
TApplicationRequirements = never,
|
|
23
21
|
TPreflight = never,
|
|
@@ -38,7 +36,6 @@ export type AppHostInput<
|
|
|
38
36
|
TServices,
|
|
39
37
|
TServiceError,
|
|
40
38
|
TServiceRequirements,
|
|
41
|
-
TSession,
|
|
42
39
|
never,
|
|
43
40
|
never,
|
|
44
41
|
never,
|
|
@@ -58,7 +55,6 @@ export type AppHostInput<
|
|
|
58
55
|
TServiceError,
|
|
59
56
|
TServiceRequirements,
|
|
60
57
|
TMigrations,
|
|
61
|
-
TSession,
|
|
62
58
|
TPreflight,
|
|
63
59
|
TPreflightError,
|
|
64
60
|
TPreflightRequirements,
|
|
@@ -90,7 +86,6 @@ export type AppHostInput<
|
|
|
90
86
|
TServices,
|
|
91
87
|
TServiceError,
|
|
92
88
|
TServiceRequirements,
|
|
93
|
-
TSession,
|
|
94
89
|
TPreflight,
|
|
95
90
|
TPreflightError,
|
|
96
91
|
TPreflightRequirements,
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
import { HttpClientError, HttpClientResponse } from "effect/http";
|
|
3
|
+
import { readBoundedBody } from "@shivaedev/http/body.ts";
|
|
4
|
+
|
|
5
|
+
const responseLimit = 64 * 1024;
|
|
6
|
+
|
|
7
|
+
export const boundedOtlpResponse = Effect.fnUntraced(function* (response: HttpClientResponse.HttpClientResponse) {
|
|
8
|
+
const bytes = yield* readBoundedBody(response, responseLimit).pipe(
|
|
9
|
+
Effect.mapError((error) =>
|
|
10
|
+
HttpClientError.isHttpClientError(error)
|
|
11
|
+
? error
|
|
12
|
+
: new HttpClientError.HttpClientError({
|
|
13
|
+
reason: new HttpClientError.DecodeError({
|
|
14
|
+
description: `OTLP collector response exceeds ${responseLimit} bytes`,
|
|
15
|
+
request: response.request,
|
|
16
|
+
response,
|
|
17
|
+
}),
|
|
18
|
+
}),
|
|
19
|
+
),
|
|
20
|
+
);
|
|
21
|
+
const buffered = HttpClientResponse.fromWeb(
|
|
22
|
+
response.request,
|
|
23
|
+
new Response(bytes.byteLength === 0 ? null : bytes, { headers: response.headers, status: response.status }),
|
|
24
|
+
);
|
|
25
|
+
yield* buffered.arrayBuffer;
|
|
26
|
+
return buffered;
|
|
27
|
+
});
|
package/src/appDatabase.ts
CHANGED
|
@@ -32,16 +32,15 @@ export function appDatabase<R, E = never>(options: AppDatabaseOptions<R, E>) {
|
|
|
32
32
|
postgresLayer(url, config).pipe(Layer.provide(clock)),
|
|
33
33
|
),
|
|
34
34
|
);
|
|
35
|
-
const prepared = Layer.effectDiscard(prepareMigrations(options.migrations)).pipe(Layer.provideMerge(pool));
|
|
36
35
|
const live = LiveServer.layer.pipe(Layer.provideMerge(Reactivity.layer));
|
|
37
36
|
const migrations = options.migrations;
|
|
38
37
|
const worker =
|
|
39
38
|
migrations.mode === "wait" && migrations.release !== undefined
|
|
40
39
|
? Layer.effect(WorkerRelease, coordinateWorkerRelease(migrations.release, migrations.pollInterval, migrations.table))
|
|
41
40
|
: Layer.empty;
|
|
42
|
-
const base = Layer.merge(
|
|
41
|
+
const base = Layer.merge(pool, live);
|
|
43
42
|
const changes = options.config?.changes;
|
|
44
|
-
|
|
43
|
+
const connected = Layer.unwrap(
|
|
45
44
|
Effect.gen(function* () {
|
|
46
45
|
const client = yield* PgClient.PgClient;
|
|
47
46
|
const hints = yield* LiveServer;
|
|
@@ -71,6 +70,8 @@ export function appDatabase<R, E = never>(options: AppDatabaseOptions<R, E>) {
|
|
|
71
70
|
}).pipe(Layer.provide(clock));
|
|
72
71
|
}),
|
|
73
72
|
).pipe(Layer.provideMerge(base));
|
|
73
|
+
const prepared = Layer.effectDiscard(prepareMigrations(migrations)).pipe(Layer.provideMerge(connected));
|
|
74
|
+
return worker.pipe(Layer.provideMerge(prepared));
|
|
74
75
|
}
|
|
75
76
|
|
|
76
77
|
function prepareMigrations<R, E>(migrations: AppDatabaseOptions<R, E>["migrations"]) {
|
package/src/appHost.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
|
|
2
2
|
import { Layer } from "effect";
|
|
3
3
|
import type { Rpc } from "effect/rpc";
|
|
4
|
-
import type { SessionPolicy
|
|
4
|
+
import type { SessionPolicy } from "@shivaedev/identity/server/SessionPolicy.ts";
|
|
5
5
|
import type { AppHostFactory } from "#AppHostFactory.ts";
|
|
6
6
|
import type { AppHostInput, HostPreflight } from "#AppHostInput.ts";
|
|
7
7
|
import { appDatabase, type DatabaseConfig } from "#appDatabase.ts";
|
|
@@ -23,7 +23,6 @@ export type AppHostOptions<
|
|
|
23
23
|
TServiceError = never,
|
|
24
24
|
TServiceRequirements = never,
|
|
25
25
|
TMigrations = never,
|
|
26
|
-
TSession extends SessionShape = SessionShape,
|
|
27
26
|
TPreflight = never,
|
|
28
27
|
TPreflightError = never,
|
|
29
28
|
TPreflightRequirements = never,
|
|
@@ -35,7 +34,7 @@ export type AppHostOptions<
|
|
|
35
34
|
HostRoutes<TRouteError, TRouteRequirements> & {
|
|
36
35
|
readonly migrations: AppDatabaseOptions<TMigrations, TMigrationError>["migrations"];
|
|
37
36
|
readonly policy?: undefined;
|
|
38
|
-
readonly sessions: SessionPolicy
|
|
37
|
+
readonly sessions: Layer.Layer<SessionPolicy>;
|
|
39
38
|
};
|
|
40
39
|
|
|
41
40
|
export type AppLocalHostOptions<
|
|
@@ -69,7 +68,6 @@ export type AppHttpHostOptions<
|
|
|
69
68
|
TServices = never,
|
|
70
69
|
TServiceError = never,
|
|
71
70
|
TServiceRequirements = never,
|
|
72
|
-
TSession extends SessionShape = SessionShape,
|
|
73
71
|
TPreflight = never,
|
|
74
72
|
TPreflightError = never,
|
|
75
73
|
TPreflightRequirements = never,
|
|
@@ -78,7 +76,7 @@ export type AppHttpHostOptions<
|
|
|
78
76
|
> = HostPreflight<TPreflight, TPreflightError, TPreflightRequirements> &
|
|
79
77
|
HostCoreOptions<TRpcs, THandlers, THandlerError, THandlerRequirements, TServices, TServiceError, TServiceRequirements> &
|
|
80
78
|
HostRoutes<TRouteError, TRouteRequirements> &
|
|
81
|
-
HostPolicy
|
|
79
|
+
HostPolicy & { readonly migrations?: undefined };
|
|
82
80
|
export const appHost: AppHostFactory = <
|
|
83
81
|
TRpcs extends Rpc.Any,
|
|
84
82
|
THandlers,
|
|
@@ -88,7 +86,6 @@ export const appHost: AppHostFactory = <
|
|
|
88
86
|
TServiceError = never,
|
|
89
87
|
TServiceRequirements = never,
|
|
90
88
|
TMigrations = never,
|
|
91
|
-
TSession extends SessionShape = SessionShape,
|
|
92
89
|
TApplicationError = never,
|
|
93
90
|
TApplicationRequirements = never,
|
|
94
91
|
TPreflight = never,
|
|
@@ -107,7 +104,6 @@ export const appHost: AppHostFactory = <
|
|
|
107
104
|
TServiceError,
|
|
108
105
|
TServiceRequirements,
|
|
109
106
|
TMigrations,
|
|
110
|
-
TSession,
|
|
111
107
|
TApplicationError,
|
|
112
108
|
TApplicationRequirements,
|
|
113
109
|
TPreflight,
|
package/src/appHostHttp.ts
CHANGED
|
@@ -3,7 +3,6 @@ import { Effect, Layer } from "effect";
|
|
|
3
3
|
import { HttpRouter, HttpServer } from "effect/http";
|
|
4
4
|
import type { Rpc } from "effect/rpc";
|
|
5
5
|
import { schemaSensitiveKeys } from "@shivaedev/errors/schemaSensitiveKeys.ts";
|
|
6
|
-
import type { SessionShape } from "@shivaedev/identity/server/session.ts";
|
|
7
6
|
import type { appDatabase } from "#appDatabase.ts";
|
|
8
7
|
import type { AppHttpHostOptions } from "#appHost.ts";
|
|
9
8
|
import { browserErrorRoute } from "#browserErrorRoute.ts";
|
|
@@ -28,7 +27,6 @@ export function appHostHttp<
|
|
|
28
27
|
TServices,
|
|
29
28
|
TServiceError,
|
|
30
29
|
TServiceRequirements,
|
|
31
|
-
TSession extends SessionShape,
|
|
32
30
|
TRouteError = never,
|
|
33
31
|
TRouteRequirements = HostRouteRequirements,
|
|
34
32
|
>(
|
|
@@ -43,7 +41,7 @@ export function appHostHttp<
|
|
|
43
41
|
TRouteError,
|
|
44
42
|
TRouteRequirements
|
|
45
43
|
> &
|
|
46
|
-
HostPolicy
|
|
44
|
+
HostPolicy,
|
|
47
45
|
) {
|
|
48
46
|
return Layer.unwrap(
|
|
49
47
|
Effect.map(schemaSensitiveKeys(options.group, options.config?.sensitiveKeys), (sensitive) =>
|
|
@@ -54,9 +52,16 @@ export function appHostHttp<
|
|
|
54
52
|
const routes = hostRpcRoutes(options, transport, sensitive);
|
|
55
53
|
const assets = options.assets === undefined ? Layer.empty : hostAssets(options.assets);
|
|
56
54
|
const application = Layer.mergeAll(options.services ?? Layer.empty);
|
|
55
|
+
const sessions = Layer.mergeAll(Layer.empty, ...(options.sessions === undefined ? [] : [options.sessions]));
|
|
57
56
|
const server = nodeHttpServer(host, options.config);
|
|
58
57
|
const applicationRoutes = hostApplicationRoutes(options.routes, bodySize);
|
|
59
|
-
const httpRoutes = Layer.mergeAll(
|
|
58
|
+
const httpRoutes = Layer.mergeAll(
|
|
59
|
+
routes,
|
|
60
|
+
healthRoute,
|
|
61
|
+
browserErrorRoute(trustedProxy, options.browserErrors),
|
|
62
|
+
assets,
|
|
63
|
+
applicationRoutes,
|
|
64
|
+
).pipe(
|
|
60
65
|
Layer.provide(nativeClientOrigins(nativeOrigins, trustedProxy, false, options.rpcPath)),
|
|
61
66
|
Layer.provide(nativeClientOrigins(nativeOrigins, trustedProxy, options.nativeOrigins === undefined ? "preflight" : true)),
|
|
62
67
|
);
|
|
@@ -64,7 +69,7 @@ export function appHostHttp<
|
|
|
64
69
|
HttpRouter.toHttpEffect(httpRoutes).pipe(
|
|
65
70
|
Effect.map((handler) => HttpServer.serve(handler).pipe(HttpServer.withLogAddress, Layer.provideMerge(server))),
|
|
66
71
|
),
|
|
67
|
-
).pipe(Layer.provideMerge(application), Layer.provideMerge(NodeHttpServer.layerHttpServices));
|
|
72
|
+
).pipe(Layer.provideMerge(application), Layer.provide(sessions), Layer.provideMerge(NodeHttpServer.layerHttpServices));
|
|
68
73
|
}),
|
|
69
74
|
).pipe(Layer.provide(redactingDiagnostics(sensitive)), Layer.provide(otlpTracing), Layer.provide(Layer.succeed(RedactionPolicy, sensitive))),
|
|
70
75
|
),
|
|
@@ -80,7 +85,6 @@ export function migratedHost<
|
|
|
80
85
|
TServiceError,
|
|
81
86
|
TServiceRequirements,
|
|
82
87
|
TMigrations,
|
|
83
|
-
TSession extends SessionShape,
|
|
84
88
|
TRouteError = never,
|
|
85
89
|
TRouteRequirements = HostRouteRequirements,
|
|
86
90
|
TMigrationError = never,
|
|
@@ -96,7 +100,7 @@ export function migratedHost<
|
|
|
96
100
|
TRouteError,
|
|
97
101
|
TRouteRequirements
|
|
98
102
|
> &
|
|
99
|
-
HostPolicy
|
|
103
|
+
HostPolicy & {
|
|
100
104
|
readonly migrations: MigrationOptions<TMigrations, TMigrationError>;
|
|
101
105
|
},
|
|
102
106
|
database: ReturnType<typeof appDatabase<TMigrations, TMigrationError>>,
|
|
@@ -114,7 +118,6 @@ export function deferredHost<
|
|
|
114
118
|
TServiceError,
|
|
115
119
|
TServiceRequirements,
|
|
116
120
|
TMigrations,
|
|
117
|
-
TSession extends SessionShape,
|
|
118
121
|
TApplicationError,
|
|
119
122
|
TApplicationRequirements,
|
|
120
123
|
TRouteError = never,
|
|
@@ -130,7 +133,6 @@ export function deferredHost<
|
|
|
130
133
|
TServices,
|
|
131
134
|
TServiceError,
|
|
132
135
|
TServiceRequirements,
|
|
133
|
-
TSession,
|
|
134
136
|
never,
|
|
135
137
|
never,
|
|
136
138
|
never,
|
package/src/bearerSocketRpc.ts
CHANGED
|
@@ -4,12 +4,12 @@ import * as Socket from "effect/socket/Socket";
|
|
|
4
4
|
import { SocketAuthentication } from "@shivaedev/contract/socketAuthentication.ts";
|
|
5
5
|
import { Unauthorized } from "@shivaedev/errors/taxonomy.ts";
|
|
6
6
|
import { recheckSession } from "@shivaedev/identity/server/recheckSession.ts";
|
|
7
|
-
import {
|
|
7
|
+
import { SessionPolicy } from "@shivaedev/identity/server/SessionPolicy.ts";
|
|
8
|
+
import { resolveSession } from "@shivaedev/identity/server/session.ts";
|
|
8
9
|
import { makeSocketSession, SocketSession } from "@shivaedev/identity/server/socketSession.ts";
|
|
9
10
|
|
|
10
|
-
export const bearerSocketRpc = Effect.fnUntraced(function*
|
|
11
|
+
export const bearerSocketRpc = Effect.fnUntraced(function* (
|
|
11
12
|
handler: Effect.Effect<HttpServerResponse.HttpServerResponse, never, HttpServerRequest.HttpServerRequest | Scope.Scope>,
|
|
12
|
-
policy: SessionPolicy<TSession>,
|
|
13
13
|
) {
|
|
14
14
|
const request = yield* HttpServerRequest.HttpServerRequest;
|
|
15
15
|
const socket = yield* Effect.orDie(request.upgrade);
|
|
@@ -54,12 +54,12 @@ export const bearerSocketRpc = Effect.fnUntraced(function* <TSession extends Ses
|
|
|
54
54
|
get: (target, key, receiver) => (key === "upgrade" ? Effect.succeed(authenticatedSocket) : Reflect.get(target, key, receiver)),
|
|
55
55
|
});
|
|
56
56
|
return yield* Effect.gen(function* () {
|
|
57
|
-
const initial = yield* resolveSession(
|
|
57
|
+
const initial = yield* resolveSession(Headers.empty, "$connect");
|
|
58
58
|
if (Option.isNone(initial)) {
|
|
59
59
|
return yield* new Unauthorized({ message: "Authentication required" });
|
|
60
60
|
}
|
|
61
|
-
const session = yield* makeSocketSession(
|
|
62
|
-
const check = recheckSession(
|
|
61
|
+
const session = yield* makeSocketSession((yield* SessionPolicy).streamRevalidationInterval ?? "30 seconds");
|
|
62
|
+
const check = recheckSession(Headers.empty, "$connect").pipe(
|
|
63
63
|
Effect.flatMap((current) =>
|
|
64
64
|
Option.isSome(current) && current.value.user.id === initial.value.user.id
|
|
65
65
|
? Effect.void
|
package/src/browserErrorRoute.ts
CHANGED
|
@@ -2,7 +2,11 @@ import { ByteSize, Cause, Effect, ErrorReporter, Layer, Option, Schema } from "e
|
|
|
2
2
|
import { HttpIncomingMessage, HttpRouter, HttpServerRequest, HttpServerResponse } from "effect/http";
|
|
3
3
|
import * as HttpStatus from "effect/http/HttpStatus";
|
|
4
4
|
import { RateLimiter } from "effect/persistence";
|
|
5
|
+
import { redactBrowserText } from "@shivaedev/errors/redactBrowserText.ts";
|
|
6
|
+
import { isAuthUnavailable, isForbidden } from "@shivaedev/errors/taxonomy.ts";
|
|
5
7
|
import { localTransport, trustedProxyPeer } from "@shivaedev/identity/server/local.ts";
|
|
8
|
+
import { SessionPolicy } from "@shivaedev/identity/server/SessionPolicy.ts";
|
|
9
|
+
import { resolveSession } from "@shivaedev/identity/server/session.ts";
|
|
6
10
|
|
|
7
11
|
const BrowserErrorReport = Schema.Struct({
|
|
8
12
|
kind: Schema.Literals(["error", "unhandledrejection"]),
|
|
@@ -11,7 +15,18 @@ const BrowserErrorReport = Schema.Struct({
|
|
|
11
15
|
url: Schema.String,
|
|
12
16
|
});
|
|
13
17
|
|
|
14
|
-
|
|
18
|
+
export interface BrowserErrorOptions {
|
|
19
|
+
readonly includeUserId?: boolean;
|
|
20
|
+
readonly log?: boolean;
|
|
21
|
+
readonly requireSession?: boolean;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
class BrowserError extends Error {
|
|
25
|
+
override readonly name = "BrowserError";
|
|
26
|
+
override readonly [ErrorReporter.severity] = "Error";
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const receiveReport = Effect.fnUntraced(function* (options: BrowserErrorOptions, userId?: string) {
|
|
15
30
|
const parsed = yield* HttpServerRequest.schemaBodyJson(BrowserErrorReport).pipe(
|
|
16
31
|
Effect.provideService(HttpIncomingMessage.MaxBodySize, ByteSize.kibibytes(16)),
|
|
17
32
|
Effect.option,
|
|
@@ -19,19 +34,44 @@ const receiveReport = Effect.gen(function* () {
|
|
|
19
34
|
if (Option.isNone(parsed)) {
|
|
20
35
|
return HttpServerResponse.empty({ status: HttpStatus.fromLiteral("BadRequest") });
|
|
21
36
|
}
|
|
22
|
-
const error = new
|
|
23
|
-
error.name = "BrowserError";
|
|
37
|
+
const error = new BrowserError(redactBrowserText(parsed.value.message));
|
|
24
38
|
if (parsed.value.stack !== undefined) {
|
|
25
|
-
error.stack = parsed.value.stack;
|
|
39
|
+
error.stack = redactBrowserText(parsed.value.stack);
|
|
40
|
+
}
|
|
41
|
+
const cause = Cause.die(error);
|
|
42
|
+
if (userId !== undefined) {
|
|
43
|
+
yield* Effect.annotateCurrentSpan("user.id", userId);
|
|
26
44
|
}
|
|
27
|
-
yield*
|
|
45
|
+
yield* Effect.gen(function* () {
|
|
46
|
+
yield* ErrorReporter.report(cause);
|
|
47
|
+
if (options.log) {
|
|
48
|
+
yield* Effect.logError("Browser error", cause);
|
|
49
|
+
}
|
|
50
|
+
}).pipe(
|
|
51
|
+
Effect.annotateLogs({ "browser.kind": parsed.value.kind, "url.full": parsed.value.url, ...(userId === undefined ? {} : { "user.id": userId }) }),
|
|
52
|
+
);
|
|
28
53
|
return HttpServerResponse.empty({ status: HttpStatus.fromLiteral("Accepted") });
|
|
29
54
|
});
|
|
30
55
|
|
|
31
|
-
|
|
56
|
+
const receiveForSession = Effect.fnUntraced(function* (options: BrowserErrorOptions, policy: Option.Option<SessionPolicy["Service"]>) {
|
|
57
|
+
if (!(options.requireSession || options.includeUserId)) {
|
|
58
|
+
return yield* receiveReport(options);
|
|
59
|
+
}
|
|
60
|
+
const request = yield* HttpServerRequest.HttpServerRequest;
|
|
61
|
+
const session = Option.isSome(policy)
|
|
62
|
+
? yield* resolveSession(request.headers, "$browserError").pipe(Effect.provideService(SessionPolicy, policy.value))
|
|
63
|
+
: Option.none();
|
|
64
|
+
if (options.requireSession && Option.isNone(session)) {
|
|
65
|
+
return HttpServerResponse.empty({ status: HttpStatus.fromLiteral("Unauthorized") });
|
|
66
|
+
}
|
|
67
|
+
return yield* receiveReport(options, options.includeUserId && Option.isSome(session) ? session.value.user.id : undefined);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
export function browserErrorRoute(trustedProxy?: string, options: BrowserErrorOptions = {}) {
|
|
32
71
|
return HttpRouter.use((router) =>
|
|
33
72
|
Effect.gen(function* () {
|
|
34
73
|
const limiter = yield* RateLimiter.RateLimiter;
|
|
74
|
+
const policy = yield* Effect.serviceOption(SessionPolicy);
|
|
35
75
|
const allowed = localTransport(trustedProxy);
|
|
36
76
|
const trusted = trustedProxyPeer(trustedProxy);
|
|
37
77
|
yield* router.add(
|
|
@@ -48,8 +88,11 @@ export function browserErrorRoute(trustedProxy?: string) {
|
|
|
48
88
|
if (!accepted) {
|
|
49
89
|
return HttpServerResponse.empty({ status: HttpStatus.fromLiteral("TooManyRequests") });
|
|
50
90
|
}
|
|
51
|
-
return yield*
|
|
52
|
-
})
|
|
91
|
+
return yield* receiveForSession(options, policy);
|
|
92
|
+
}).pipe(
|
|
93
|
+
Effect.catchIf(isAuthUnavailable, () => Effect.succeed(HttpServerResponse.empty({ status: HttpStatus.fromLiteral("ServiceUnavailable") }))),
|
|
94
|
+
Effect.catchIf(isForbidden, () => Effect.succeed(HttpServerResponse.empty({ status: HttpStatus.fromLiteral("Forbidden") }))),
|
|
95
|
+
),
|
|
53
96
|
);
|
|
54
97
|
}),
|
|
55
98
|
).pipe(Layer.provide(RateLimiter.layer.pipe(Layer.provide(RateLimiter.layerStoreMemory))));
|
package/src/hostChanges.ts
CHANGED
|
@@ -7,14 +7,15 @@ import type { QueryReference } from "@shivaedev/contract/serverMetadata.ts";
|
|
|
7
7
|
import { Unauthorized } from "@shivaedev/errors/taxonomy.ts";
|
|
8
8
|
import { recheckSession } from "@shivaedev/identity/server/recheckSession.ts";
|
|
9
9
|
import { revalidateStream } from "@shivaedev/identity/server/revalidateStream.ts";
|
|
10
|
-
import
|
|
10
|
+
import { SessionPolicy } from "@shivaedev/identity/server/SessionPolicy.ts";
|
|
11
11
|
import { resolveChanges } from "@shivaedev/identity/server/sessionKeys.ts";
|
|
12
12
|
import { rerunQuery } from "#rerunQuery.ts";
|
|
13
13
|
|
|
14
|
-
export function hostChanges<R extends Rpc.Any
|
|
14
|
+
export function hostChanges<R extends Rpc.Any>(group: RpcGroup.RpcGroup<R>) {
|
|
15
15
|
return Layer.effectContext(
|
|
16
16
|
Effect.gen(function* () {
|
|
17
17
|
const changes = yield* Effect.serviceOption(LiveServer);
|
|
18
|
+
const sessions = yield* Effect.serviceOption(SessionPolicy);
|
|
18
19
|
const services = yield* Effect.context<never>();
|
|
19
20
|
const handlers = new Map<string, unknown>();
|
|
20
21
|
function subscribe(rpc: Rpc.AnyWithProps, queries: readonly QueryReference[], options: Parameters<Rpc.Handler<string>["handler"]>[1]) {
|
|
@@ -22,15 +23,18 @@ export function hostChanges<R extends Rpc.Any, TSession extends SessionShape>(gr
|
|
|
22
23
|
Effect.gen(function* () {
|
|
23
24
|
const { subscription, context } = yield* resolveChanges(rpc, queries);
|
|
24
25
|
const viewer = yield* CurrentViewer;
|
|
25
|
-
const authorized =
|
|
26
|
+
const authorized = Option.isNone(sessions)
|
|
27
|
+
? subscription.authorized
|
|
28
|
+
: authorizeSession(rpc._tag, viewer, subscription.authorized).pipe(Effect.provideService(SessionPolicy, sessions.value));
|
|
26
29
|
const stream = Option.isSome(changes)
|
|
27
30
|
? Stream.unwrap(changes.value.subscribe({ ...subscription, authorized }, context, rerunQuery(group, options)))
|
|
28
31
|
: Stream.concat(Stream.succeed({ _tag: "Resync" } as const), Stream.never);
|
|
29
|
-
if (sessions
|
|
32
|
+
if (Option.isNone(sessions) || Option.isNone(viewer)) {
|
|
30
33
|
return stream;
|
|
31
34
|
}
|
|
32
35
|
const revalidation = Effect.never.pipe(
|
|
33
|
-
revalidateStream(
|
|
36
|
+
revalidateStream(rpc, viewer.value.id, recheckSession(Headers.empty, rpc._tag)),
|
|
37
|
+
Effect.provideService(SessionPolicy, sessions.value),
|
|
34
38
|
);
|
|
35
39
|
return Stream.interruptWhen(stream, revalidation);
|
|
36
40
|
}),
|
|
@@ -57,20 +61,10 @@ export function hostChanges<R extends Rpc.Any, TSession extends SessionShape>(gr
|
|
|
57
61
|
);
|
|
58
62
|
}
|
|
59
63
|
|
|
60
|
-
function
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
policy: Effect.Effect<boolean, E, R>,
|
|
65
|
-
) {
|
|
66
|
-
if (sessions === undefined) {
|
|
67
|
-
return policy;
|
|
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" });
|
|
68
68
|
}
|
|
69
|
-
return
|
|
70
|
-
|
|
71
|
-
Effect.flatMap(
|
|
72
|
-
(allowed): Effect.Effect<boolean, E | Unauthorized, R> =>
|
|
73
|
-
allowed ? policy : Effect.fail(new Unauthorized({ message: "Authentication required" })),
|
|
74
|
-
),
|
|
75
|
-
);
|
|
76
|
-
}
|
|
69
|
+
return yield* policy;
|
|
70
|
+
});
|
package/src/hostOptions.ts
CHANGED
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
import type { Duration, Layer } from "effect";
|
|
2
2
|
import type { HttpRouter } from "effect/http";
|
|
3
3
|
import type { Rpc, RpcGroup } from "effect/rpc";
|
|
4
|
-
import type { SessionPolicy
|
|
4
|
+
import type { SessionPolicy } from "@shivaedev/identity/server/SessionPolicy.ts";
|
|
5
5
|
import type { AssetOptions } from "#AssetOptions.ts";
|
|
6
6
|
import type { AppDatabaseOptions } from "#appHost.ts";
|
|
7
|
+
import type { BrowserErrorOptions } from "#browserErrorRoute.ts";
|
|
7
8
|
|
|
8
9
|
export type HostRouteRequirements = HttpRouter.HttpRouter | HttpRouter.Request<"Error", unknown>;
|
|
9
10
|
|
|
10
11
|
export type HostCoreOptions<TRpcs extends Rpc.Any, THandlers, THandlerError, THandlerRequirements, TServices, TServiceError, TServiceRequirements> = {
|
|
11
12
|
readonly assets?: string | AssetOptions;
|
|
13
|
+
readonly browserErrors?: BrowserErrorOptions;
|
|
12
14
|
readonly config?: AppDatabaseOptions["config"] & {
|
|
13
15
|
readonly port?: number;
|
|
14
16
|
readonly host?: string;
|
|
@@ -44,6 +46,6 @@ export type HostOptions<
|
|
|
44
46
|
> = HostCoreOptions<TRpcs, THandlers, THandlerError, THandlerRequirements, TServices, TServiceError, TServiceRequirements> &
|
|
45
47
|
HostRoutes<TRouteError, TRouteRequirements>;
|
|
46
48
|
|
|
47
|
-
export type HostPolicy
|
|
48
|
-
| { readonly policy?: undefined; readonly sessions: SessionPolicy
|
|
49
|
+
export type HostPolicy =
|
|
50
|
+
| { readonly policy?: undefined; readonly sessions: Layer.Layer<SessionPolicy> }
|
|
49
51
|
| { readonly policy: "loopback"; readonly sessions?: never };
|