@shivaedev/contract 0.43.5 → 0.45.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 +43 -0
- package/README.md +265 -45
- package/dist/adapters/activityState.d.ts +3 -2
- package/dist/adapters/activityState.d.ts.map +1 -1
- package/dist/adapters/activityState.js +11 -1
- package/dist/adapters/activityState.js.map +1 -1
- package/dist/adapters/clientConnection.d.ts +1 -0
- package/dist/adapters/clientConnection.d.ts.map +1 -1
- package/dist/adapters/clientConnection.js +18 -10
- package/dist/adapters/clientConnection.js.map +1 -1
- package/dist/adapters/servedHandlers.d.ts +8 -0
- package/dist/adapters/servedHandlers.d.ts.map +1 -0
- package/dist/adapters/servedHandlers.js +51 -0
- package/dist/adapters/servedHandlers.js.map +1 -0
- package/dist/automaticQuery.js +1 -1
- package/dist/automaticQuery.js.map +1 -1
- package/dist/clientActivities.d.ts +2 -0
- package/dist/clientActivities.d.ts.map +1 -1
- package/dist/clientActivities.js +7 -3
- package/dist/clientActivities.js.map +1 -1
- package/dist/clientActivity.d.ts +2 -2
- package/dist/clientActivity.d.ts.map +1 -1
- package/dist/clientActivity.js +1 -1
- package/dist/clientActivity.js.map +1 -1
- package/dist/clientContract.d.ts +51 -0
- package/dist/clientContract.d.ts.map +1 -0
- package/dist/clientContract.js +32 -0
- package/dist/clientContract.js.map +1 -0
- package/dist/clientMatchesModule.d.ts +18 -0
- package/dist/clientMatchesModule.d.ts.map +1 -0
- package/dist/clientMatchesModule.js +2 -0
- package/dist/clientMatchesModule.js.map +1 -0
- package/dist/contract.d.ts +23 -1
- package/dist/contract.d.ts.map +1 -1
- package/dist/contract.js +9 -6
- package/dist/contract.js.map +1 -1
- package/dist/declarationError.d.ts +1 -2
- package/dist/declarationError.d.ts.map +1 -1
- package/dist/declarationError.js +3 -1
- package/dist/declarationError.js.map +1 -1
- package/dist/defineModule.d.ts +41 -0
- package/dist/defineModule.d.ts.map +1 -0
- package/dist/defineModule.js +17 -0
- package/dist/defineModule.js.map +1 -0
- package/dist/live-server/subscribe.d.ts.map +1 -1
- package/dist/live-server/subscribe.js +64 -6
- package/dist/live-server/subscribe.js.map +1 -1
- package/dist/live.d.ts +1 -0
- package/dist/live.d.ts.map +1 -1
- package/dist/live.js +5 -0
- package/dist/live.js.map +1 -1
- package/dist/liveAnswers.d.ts +9 -0
- package/dist/liveAnswers.d.ts.map +1 -0
- package/dist/liveAnswers.js +12 -0
- package/dist/liveAnswers.js.map +1 -0
- package/dist/moduleHandlers.d.ts +15 -0
- package/dist/moduleHandlers.d.ts.map +1 -0
- package/dist/moduleHandlers.js +5 -0
- package/dist/moduleHandlers.js.map +1 -0
- package/dist/moduleProof.d.ts +79 -0
- package/dist/moduleProof.d.ts.map +1 -0
- package/dist/moduleProof.js +2 -0
- package/dist/moduleProof.js.map +1 -0
- package/dist/operation.d.ts +14 -12
- package/dist/operation.d.ts.map +1 -1
- package/dist/operation.js +10 -4
- package/dist/operation.js.map +1 -1
- package/dist/operationDeclaration.d.ts +9 -1
- package/dist/operationDeclaration.d.ts.map +1 -1
- package/dist/operationDeclaration.js.map +1 -1
- package/dist/queryDelivery.js +1 -1
- package/dist/queryDelivery.js.map +1 -1
- package/dist/querySubscription.d.ts +1 -1
- package/dist/querySubscription.d.ts.map +1 -1
- package/dist/querySubscription.js +6 -4
- package/dist/querySubscription.js.map +1 -1
- package/dist/readPolicy.d.ts +2 -1
- package/dist/readPolicy.d.ts.map +1 -1
- package/dist/readPolicy.js +9 -5
- package/dist/readPolicy.js.map +1 -1
- package/dist/retainQuery.d.ts +1 -0
- package/dist/retainQuery.d.ts.map +1 -1
- package/dist/retainQuery.js +5 -0
- package/dist/retainQuery.js.map +1 -1
- package/dist/rpcClient.d.ts +1 -0
- package/dist/rpcClient.d.ts.map +1 -1
- package/dist/runProof.d.ts +69 -0
- package/dist/runProof.d.ts.map +1 -0
- package/dist/runProof.js +2 -0
- package/dist/runProof.js.map +1 -0
- package/dist/servedOperation.d.ts +72 -0
- package/dist/servedOperation.d.ts.map +1 -0
- package/dist/servedOperation.js +11 -0
- package/dist/servedOperation.js.map +1 -0
- package/dist/servedReadPolicy.d.ts +11 -0
- package/dist/servedReadPolicy.d.ts.map +1 -0
- package/dist/servedReadPolicy.js +9 -0
- package/dist/servedReadPolicy.js.map +1 -0
- package/dist/serverMetadata.d.ts +1 -4
- package/dist/serverMetadata.d.ts.map +1 -1
- package/dist/serverMetadata.js +3 -6
- package/dist/serverMetadata.js.map +1 -1
- package/dist/successProof.d.ts +51 -0
- package/dist/successProof.d.ts.map +1 -0
- package/dist/successProof.js +2 -0
- package/dist/successProof.js.map +1 -0
- package/dist/wire.d.ts +25 -0
- package/dist/wire.d.ts.map +1 -0
- package/dist/wire.js +13 -0
- package/dist/wire.js.map +1 -0
- package/package.json +16 -16
- package/src/adapters/activityState.ts +12 -3
- package/src/adapters/clientConnection.ts +24 -16
- package/src/adapters/servedHandlers.ts +69 -0
- package/src/automaticQuery.ts +1 -1
- package/src/clientActivities.ts +7 -3
- package/src/clientActivity.ts +3 -1
- package/src/clientContract.ts +128 -0
- package/src/clientMatchesModule.ts +24 -0
- package/src/contract.ts +24 -17
- package/src/declarationError.ts +5 -2
- package/src/defineModule.ts +112 -0
- package/src/live-server/subscribe.ts +74 -6
- package/src/live.ts +7 -0
- package/src/liveAnswers.ts +13 -0
- package/src/moduleHandlers.ts +23 -0
- package/src/moduleProof.ts +101 -0
- package/src/operation.ts +47 -33
- package/src/operationDeclaration.ts +19 -1
- package/src/queryDelivery.ts +1 -1
- package/src/querySubscription.ts +6 -4
- package/src/readPolicy.ts +15 -5
- package/src/retainQuery.ts +7 -0
- package/src/runProof.ts +96 -0
- package/src/servedOperation.ts +145 -0
- package/src/servedReadPolicy.ts +19 -0
- package/src/serverMetadata.ts +3 -6
- package/src/successProof.ts +120 -0
- package/src/wire.ts +45 -0
package/src/retainQuery.ts
CHANGED
|
@@ -5,6 +5,12 @@ import { retainedQueries } from "#adapters/retainedQueries.ts";
|
|
|
5
5
|
import { querySignals, shareQuerySignals } from "#querySignals.ts";
|
|
6
6
|
import { wrapAtom } from "#wrapAtom.ts";
|
|
7
7
|
|
|
8
|
+
const runtimes = new WeakMap<Atom.Atom<unknown>, Atom.AtomRuntime<unknown, unknown>>();
|
|
9
|
+
|
|
10
|
+
export function queryRuntime(query: Atom.Atom<unknown>): Atom.AtomRuntime<unknown, unknown> | undefined {
|
|
11
|
+
return runtimes.get(query);
|
|
12
|
+
}
|
|
13
|
+
|
|
8
14
|
export function retainQuery<A, E>(runtime: Atom.AtomRuntime<unknown, unknown>, atom: Atom.Atom<AsyncResult.AsyncResult<A, E>>) {
|
|
9
15
|
const snapshot = Atom.make<Option.Option<AsyncResult.Success<A, E>>>(Option.none()).pipe(Atom.setIdleTTL(0));
|
|
10
16
|
const signals = querySignals(atom);
|
|
@@ -32,5 +38,6 @@ export function retainQuery<A, E>(runtime: Atom.AtomRuntime<unknown, unknown>, a
|
|
|
32
38
|
return display(atom.read(context));
|
|
33
39
|
}).pipe(Atom.setIdleTTL(0));
|
|
34
40
|
shareQuerySignals(retained, atom);
|
|
41
|
+
runtimes.set(retained, runtime);
|
|
35
42
|
return retained;
|
|
36
43
|
}
|
package/src/runProof.ts
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import type { Effect, Schema } from "effect";
|
|
2
|
+
import type { InfrastructureFailure } from "@shivaedev/errors/infrastructure.ts";
|
|
3
|
+
import type { InternalServerError, ServiceUnavailable } from "@shivaedev/errors/taxonomy.ts";
|
|
4
|
+
import type { QueryReadDenied } from "#readPolicyErrors.ts";
|
|
5
|
+
import type { TaggedRejection } from "#rejection.ts";
|
|
6
|
+
import type { Identical, SuccessProblem } from "#successProof.ts";
|
|
7
|
+
|
|
8
|
+
export type RunBody<TPayload, TYielded, A> = (payload: TPayload) => Generator<TYielded, A, never>;
|
|
9
|
+
|
|
10
|
+
export type ErrorOf<TYielded> = [TYielded] extends [never] ? never : [TYielded] extends [Effect.Effect<unknown, infer E, unknown>] ? E : never;
|
|
11
|
+
export type ServicesOf<TYielded> = [TYielded] extends [never] ? never : [TYielded] extends [Effect.Effect<unknown, unknown, infer R>] ? R : never;
|
|
12
|
+
export type RunEffect<TYielded, A> = [A] extends [unknown] ? Effect.Effect<A, ErrorOf<TYielded>, ServicesOf<TYielded>> : never;
|
|
13
|
+
|
|
14
|
+
export interface ClassRejections {
|
|
15
|
+
readonly [tag: string]: TaggedRejection;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface Automatic {
|
|
19
|
+
readonly InternalServerError: typeof InternalServerError;
|
|
20
|
+
readonly ServiceUnavailable: typeof ServiceUnavailable;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export type Raised<E> = Exclude<E, InfrastructureFailure | QueryReadDenied | InternalServerError | ServiceUnavailable>;
|
|
24
|
+
|
|
25
|
+
export interface RunFailsOutsideRejections<TOperation extends string, TTags> {
|
|
26
|
+
readonly missingRejections: TTags;
|
|
27
|
+
readonly operation: TOperation;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface RejectionsRunNeverRaises<TOperation extends string, TTags> {
|
|
31
|
+
readonly neverRaised: TTags;
|
|
32
|
+
readonly operation: TOperation;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface RejectionAutomatic<TOperation extends string, TTags> {
|
|
36
|
+
readonly automatic: TTags;
|
|
37
|
+
readonly operation: TOperation;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface RejectionClassDiffers<TOperation extends string, TTags> {
|
|
41
|
+
readonly operation: TOperation;
|
|
42
|
+
readonly raisedClassDiffers: TTags;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export interface RunReturnsAny<TOperation extends string, TFix extends string> {
|
|
46
|
+
readonly fix: TFix;
|
|
47
|
+
readonly operation: TOperation;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
type Tags<E> = E extends { readonly _tag: infer TTag extends string } ? TTag : never;
|
|
51
|
+
type IsAny<T> = 0 extends 1 & T ? true : false;
|
|
52
|
+
type Declared<TSpecs> = TSpecs extends ClassRejections ? keyof TSpecs & string : never;
|
|
53
|
+
type AutomaticTags = keyof Automatic;
|
|
54
|
+
|
|
55
|
+
type DifferentClasses<TSpecs, E> = {
|
|
56
|
+
readonly [TTag in Declared<TSpecs>]: TSpecs extends { readonly [TKey in TTag]: new (...args: never) => infer TDeclared }
|
|
57
|
+
? Identical<TDeclared, Extract<Raised<E>, { readonly _tag: TTag }>> extends true
|
|
58
|
+
? never
|
|
59
|
+
: TTag
|
|
60
|
+
: never;
|
|
61
|
+
}[Declared<TSpecs>];
|
|
62
|
+
|
|
63
|
+
export type RejectionProblem<TName extends string, TSpecs, E> = TSpecs extends undefined
|
|
64
|
+
? never
|
|
65
|
+
: [Extract<Declared<TSpecs>, AutomaticTags>] extends [never]
|
|
66
|
+
? [Exclude<Tags<Raised<E>>, Declared<TSpecs>>] extends [never]
|
|
67
|
+
? [Exclude<Declared<TSpecs>, Tags<Raised<E>>>] extends [never]
|
|
68
|
+
? [DifferentClasses<TSpecs, E>] extends [never]
|
|
69
|
+
? never
|
|
70
|
+
: RejectionClassDiffers<TName, DifferentClasses<TSpecs, E>>
|
|
71
|
+
: RejectionsRunNeverRaises<TName, Exclude<Declared<TSpecs>, Tags<Raised<E>>>>
|
|
72
|
+
: RunFailsOutsideRejections<TName, Exclude<Tags<Raised<E>>, Declared<TSpecs>>>
|
|
73
|
+
: RejectionAutomatic<TName, Extract<Declared<TSpecs>, AutomaticTags>>;
|
|
74
|
+
|
|
75
|
+
type Problems<TName extends string, TSuccess, TSpecs, TReturn, E> = SuccessProblem<TName, TSuccess, TReturn> | RejectionProblem<TName, TSpecs, E>;
|
|
76
|
+
|
|
77
|
+
export type SuccessSchema<TSuccess> = TSuccess extends Schema.Top ? TSuccess : Schema.Void;
|
|
78
|
+
// A mapped type, unlike the Automatic interface, satisfies the index signature of RejectionSpecs.
|
|
79
|
+
type Flat<T> = { readonly [TKey in keyof T]: T[TKey] };
|
|
80
|
+
export type SpecsOf<TSpecs> = TSpecs extends ClassRejections ? Flat<TSpecs & Automatic> : Flat<Automatic>;
|
|
81
|
+
|
|
82
|
+
// TypeScript checks the declaration once with run skipped, before it infers run's types; Pending is the default it sees then.
|
|
83
|
+
export interface Pending {
|
|
84
|
+
readonly pendingInference: unique symbol;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
type IsPending<TReturn> = [TReturn] extends [never] ? false : [TReturn] extends [Pending] ? true : false;
|
|
88
|
+
|
|
89
|
+
export type RunCheck<TName extends string, TSuccess, TSpecs, TReturn, E> =
|
|
90
|
+
IsAny<TReturn> extends true
|
|
91
|
+
? { readonly operationProblem: RunReturnsAny<TName, "decode the value with a schema, or give run's return a type"> }
|
|
92
|
+
: IsPending<TReturn> extends true
|
|
93
|
+
? unknown
|
|
94
|
+
: [Problems<TName, TSuccess, TSpecs, TReturn, E>] extends [never]
|
|
95
|
+
? unknown
|
|
96
|
+
: { readonly operationProblem: Problems<TName, TSuccess, TSpecs, TReturn, E> };
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import { Effect, type Option, type Schema } from "effect";
|
|
2
|
+
import type { Key } from "@shivaedev/changes/keys.ts";
|
|
3
|
+
import { InternalServerError, ServiceUnavailable } from "@shivaedev/errors/taxonomy.ts";
|
|
4
|
+
import type { Operation, PayloadSchema } from "#operation.ts";
|
|
5
|
+
import type { Loose } from "#operationDeclaration.ts";
|
|
6
|
+
import type { PermissionReadPolicy } from "#permissionReadPolicy.ts";
|
|
7
|
+
import type { QueryServerReads } from "#QueryServerReads.ts";
|
|
8
|
+
import type { QueryReadPolicy, Viewer } from "#readPolicy.ts";
|
|
9
|
+
import type { MatchingTags, RejectionSpecs } from "#rejection.ts";
|
|
10
|
+
import type { ErrorOf, RunBody, RunCheck, RunEffect, ServicesOf, SpecsOf, SuccessSchema } from "#runProof.ts";
|
|
11
|
+
|
|
12
|
+
export interface ServedQuery<
|
|
13
|
+
TName extends string,
|
|
14
|
+
TPayload extends Schema.Top,
|
|
15
|
+
TSuccess extends Schema.Top,
|
|
16
|
+
TSpecs extends RejectionSpecs,
|
|
17
|
+
TPolicyError,
|
|
18
|
+
TPolicyServices,
|
|
19
|
+
TRun extends Effect.Effect<unknown, unknown, unknown>,
|
|
20
|
+
> extends Operation<TName, TPayload, TSuccess, TSpecs> {
|
|
21
|
+
readonly kind: "query";
|
|
22
|
+
readonly readPolicy: QueryReadPolicy<TPayload["Type"], TPolicyError, TPolicyServices>;
|
|
23
|
+
reads: (payload: TPayload["Type"], viewer: Option.Option<Viewer>) => readonly Key[];
|
|
24
|
+
readonly readsCollections?: readonly { readonly name: string }[] | undefined;
|
|
25
|
+
readonly run: (payload: TPayload["Type"]) => TRun;
|
|
26
|
+
readonly serverReads?: QueryServerReads<TPayload["Type"], TPolicyError, TPolicyServices>;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface ServedCommand<
|
|
30
|
+
TName extends string,
|
|
31
|
+
TPayload extends Schema.Top,
|
|
32
|
+
TSuccess extends Schema.Top,
|
|
33
|
+
TSpecs extends RejectionSpecs,
|
|
34
|
+
TRun extends Effect.Effect<unknown, unknown, unknown>,
|
|
35
|
+
> extends Operation<TName, TPayload, TSuccess, TSpecs> {
|
|
36
|
+
invalidates: (payload: TPayload["Type"], result: Effect.Success<TRun>, viewer?: Option.Option<Viewer>) => readonly Key[];
|
|
37
|
+
readonly kind: "command";
|
|
38
|
+
readonly run: (payload: TPayload["Type"]) => TRun;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
interface RunFields<TPayload extends Schema.Top | Schema.Struct.Fields, TYielded, TReturn, TSuccess, TSpecs> {
|
|
42
|
+
readonly payload?: TPayload;
|
|
43
|
+
readonly rejections?: TSpecs & MatchingTags<NonNullable<TSpecs>>;
|
|
44
|
+
readonly run?: RunBody<PayloadSchema<TPayload>["Type"], TYielded, TReturn>;
|
|
45
|
+
readonly success?: TSuccess;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// A run required where its types are inferred makes TypeScript report a declaration without run against default type
|
|
49
|
+
// arguments, hiding the declaration's own mistakes; requiring it apart keeps those errors.
|
|
50
|
+
export type RunDeclaration<TPayload extends Schema.Top | Schema.Struct.Fields, TYielded, TReturn, TSuccess, TSpecs> = RunFields<
|
|
51
|
+
TPayload,
|
|
52
|
+
TYielded,
|
|
53
|
+
TReturn,
|
|
54
|
+
TSuccess,
|
|
55
|
+
TSpecs
|
|
56
|
+
> &
|
|
57
|
+
NoInfer<{ readonly run: RunBody<never, unknown, unknown> }>;
|
|
58
|
+
|
|
59
|
+
export type CollectionReadPolicy<TCollections, TPayload, E, R> = TCollections extends undefined
|
|
60
|
+
? unknown
|
|
61
|
+
: { readonly readPolicy: PermissionReadPolicy<TPayload, E, R> };
|
|
62
|
+
|
|
63
|
+
export type RunQueryDeclaration<
|
|
64
|
+
TName extends string,
|
|
65
|
+
TPayload extends Schema.Top | Schema.Struct.Fields,
|
|
66
|
+
TYielded,
|
|
67
|
+
TReturn,
|
|
68
|
+
TSuccess,
|
|
69
|
+
TSpecs,
|
|
70
|
+
TPolicyError,
|
|
71
|
+
TPolicyServices,
|
|
72
|
+
TReads,
|
|
73
|
+
TCollections,
|
|
74
|
+
> = RunDeclaration<TPayload, TYielded, TReturn, TSuccess, TSpecs> &
|
|
75
|
+
CollectionReadPolicy<TCollections, PayloadSchema<TPayload>["Type"], TPolicyError, TPolicyServices> & {
|
|
76
|
+
readonly readPolicy?: QueryReadPolicy<PayloadSchema<TPayload>["Type"], TPolicyError, TPolicyServices>;
|
|
77
|
+
readonly reads?: (payload: PayloadSchema<TPayload>["Type"], viewer: Option.Option<Viewer>) => readonly Key[];
|
|
78
|
+
readonly readsCollections?: TCollections;
|
|
79
|
+
readonly serverReads?: (payload: PayloadSchema<TPayload>["Type"], viewer: Option.Option<Viewer>) => TReads;
|
|
80
|
+
} & NoInfer<RunCheck<TName, TSuccess, TSpecs, TReturn, ErrorOf<TYielded> | TPolicyError | ErrorOf<TReads>>>;
|
|
81
|
+
|
|
82
|
+
export type ServedQueryOf<
|
|
83
|
+
TName extends string,
|
|
84
|
+
TPayload extends Schema.Top | Schema.Struct.Fields,
|
|
85
|
+
TYielded,
|
|
86
|
+
TReturn,
|
|
87
|
+
TSuccess,
|
|
88
|
+
TSpecs,
|
|
89
|
+
TPolicyError,
|
|
90
|
+
TPolicyServices,
|
|
91
|
+
TReads,
|
|
92
|
+
> = [TName] extends [string]
|
|
93
|
+
? ServedQuery<
|
|
94
|
+
TName,
|
|
95
|
+
PayloadSchema<TPayload>,
|
|
96
|
+
SuccessSchema<TSuccess>,
|
|
97
|
+
SpecsOf<TSpecs>,
|
|
98
|
+
TPolicyError | ErrorOf<TReads>,
|
|
99
|
+
TPolicyServices | ServicesOf<TReads>,
|
|
100
|
+
RunEffect<TYielded, TReturn>
|
|
101
|
+
>
|
|
102
|
+
: never;
|
|
103
|
+
|
|
104
|
+
// Object keys are sorted, so invalidates is checked before run and cannot see run's inferred result; a declared success types it.
|
|
105
|
+
export interface ResultNeedsDeclaredSuccess {
|
|
106
|
+
readonly declareSuccess: unique symbol;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
type ResultOf<TSuccess> = TSuccess extends Schema.Top ? TSuccess["Type"] : ResultNeedsDeclaredSuccess;
|
|
110
|
+
|
|
111
|
+
export type RunCommandDeclaration<
|
|
112
|
+
TName extends string,
|
|
113
|
+
TPayload extends Schema.Top | Schema.Struct.Fields,
|
|
114
|
+
TYielded,
|
|
115
|
+
TReturn,
|
|
116
|
+
TSuccess,
|
|
117
|
+
TSpecs,
|
|
118
|
+
> = RunDeclaration<TPayload, TYielded, TReturn, TSuccess, TSpecs> & {
|
|
119
|
+
readonly invalidates?: (payload: PayloadSchema<TPayload>["Type"], result: ResultOf<TSuccess>, viewer?: Option.Option<Viewer>) => readonly Key[];
|
|
120
|
+
} & NoInfer<RunCheck<TName, TSuccess, TSpecs, TReturn, ErrorOf<TYielded>>>;
|
|
121
|
+
|
|
122
|
+
interface LooseRun extends Loose {
|
|
123
|
+
readonly run?: (payload: never) => Generator<Effect.Effect<unknown, unknown, unknown>, unknown, never>;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export interface LooseQuery extends LooseRun {
|
|
127
|
+
readonly readPolicy?: QueryReadPolicy<unknown, unknown, unknown>;
|
|
128
|
+
readonly reads?: (payload: unknown, viewer: Option.Option<Viewer>) => readonly Key[];
|
|
129
|
+
readonly readsCollections?: readonly { readonly name: string }[];
|
|
130
|
+
readonly serverReads?: QueryServerReads<unknown, unknown, unknown>;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export interface LooseCommand extends LooseRun {
|
|
134
|
+
readonly invalidates?: (payload: unknown, result: unknown, viewer?: Option.Option<Viewer>) => readonly Key[];
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export function withAutomatic(declaration: LooseRun): Loose {
|
|
138
|
+
return declaration.run === undefined
|
|
139
|
+
? declaration
|
|
140
|
+
: { ...declaration, rejections: { ...declaration.rejections, InternalServerError, ServiceUnavailable } };
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export function runOf({ run }: LooseRun) {
|
|
144
|
+
return run === undefined ? {} : { run: Effect.fnUntraced(run) };
|
|
145
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Context, Effect, Option } from "effect";
|
|
2
|
+
import type { Rpc } from "effect/rpc";
|
|
3
|
+
import type { QueryReadPolicy } from "#readPolicy.ts";
|
|
4
|
+
|
|
5
|
+
export class ServedReadPolicy extends Context.Service<ServedReadPolicy, { readonly check: QueryReadPolicy<unknown, unknown, unknown> }>()(
|
|
6
|
+
"@shivaedev/contract/ServedReadPolicy",
|
|
7
|
+
) {}
|
|
8
|
+
|
|
9
|
+
// Only a handler built by moduleHandlers carries the policy, so a hand-written handler keeps the guard's check. A call's
|
|
10
|
+
// middleware runs in its handler's context; a live rerun finds the query's handler by its RPC.
|
|
11
|
+
export function servedReadPolicy(rpc: Rpc.Any) {
|
|
12
|
+
return Effect.map(Effect.context<never>(), (request) =>
|
|
13
|
+
Option.orElse(Context.getOption(request, ServedReadPolicy), () =>
|
|
14
|
+
Option.flatMap(Context.getOption(request, Context.Service<Rpc.Handler<string>>(rpc.key)), (handler) =>
|
|
15
|
+
Context.getOption(handler.context, ServedReadPolicy),
|
|
16
|
+
),
|
|
17
|
+
).pipe(Option.map(({ check }) => check)),
|
|
18
|
+
);
|
|
19
|
+
}
|
package/src/serverMetadata.ts
CHANGED
|
@@ -24,8 +24,6 @@ export class CommandInvalidation extends Context.Service<CommandInvalidation, {
|
|
|
24
24
|
"@shivaedev/contract/CommandInvalidation",
|
|
25
25
|
) {}
|
|
26
26
|
|
|
27
|
-
export class KeyScope extends Context.Service<KeyScope, "session">()("@shivaedev/contract/KeyScope") {}
|
|
28
|
-
|
|
29
27
|
export interface QueryReference {
|
|
30
28
|
readonly payload: unknown;
|
|
31
29
|
readonly query: string;
|
|
@@ -66,10 +64,9 @@ export function changesRpc(name: string, queries: readonly QueryShape[]) {
|
|
|
66
64
|
}).annotate(ChangesQueries, { queries, resolve: (references) => resolveQueries(queries, references) });
|
|
67
65
|
}
|
|
68
66
|
|
|
69
|
-
export function operationAnnotations(operation: QueryShape | CommandShape | StreamShape
|
|
70
|
-
const scope = keyScope === undefined ? Context.empty() : Context.make(KeyScope, keyScope);
|
|
67
|
+
export function operationAnnotations(operation: QueryShape | CommandShape | StreamShape) {
|
|
71
68
|
if (operation.kind === "command") {
|
|
72
|
-
return Context.
|
|
69
|
+
return Context.make(CommandInvalidation, { invalidates: operation.invalidates });
|
|
73
70
|
}
|
|
74
|
-
return operation.kind === "query" ? Context.
|
|
71
|
+
return operation.kind === "query" ? Context.make(QueryDefinition, { definition: operation }) : Context.empty();
|
|
75
72
|
}
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import type { Option, Schema } from "effect";
|
|
2
|
+
|
|
3
|
+
export interface SuccessDiffersFromRun<TOperation extends string, TRun, TDeclared> {
|
|
4
|
+
readonly operation: TOperation;
|
|
5
|
+
readonly runReturns: TRun;
|
|
6
|
+
readonly successDeclares: TDeclared;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export interface ArrayMutabilityDiffers<TOperation extends string, TFix extends string> {
|
|
10
|
+
readonly fix: TFix;
|
|
11
|
+
readonly operation: TOperation;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface SuccessClassNotReturned<TOperation extends string, TRun, TClass> {
|
|
15
|
+
readonly operation: TOperation;
|
|
16
|
+
readonly runReturns: TRun;
|
|
17
|
+
readonly successClass: TClass;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
type Same<TLeft, TRight> = [TLeft] extends [TRight] ? ([TRight] extends [TLeft] ? true : false) : false;
|
|
21
|
+
export type Identical<TLeft, TRight> = (<T>() => T extends TLeft ? 1 : 2) extends <T>() => T extends TRight ? 1 : 2 ? true : false;
|
|
22
|
+
type ReadonlyArrays<T> = T extends readonly (infer TItem)[]
|
|
23
|
+
? readonly ReadonlyArrays<TItem>[]
|
|
24
|
+
: T extends (...args: never) => unknown
|
|
25
|
+
? T
|
|
26
|
+
: T extends object
|
|
27
|
+
? { readonly [TKey in keyof T]: ReadonlyArrays<T[TKey]> }
|
|
28
|
+
: T;
|
|
29
|
+
|
|
30
|
+
type Fields<TName extends string, TFields, TReturn> = {
|
|
31
|
+
readonly [TKey in keyof TFields]: NotEncodedAsReturned<TName, TFields[TKey], TKey extends keyof TReturn ? TReturn[TKey] : never>;
|
|
32
|
+
}[keyof TFields];
|
|
33
|
+
|
|
34
|
+
type Elements<TName extends string, TElements extends readonly unknown[], TReturn> = {
|
|
35
|
+
readonly [TIndex in keyof TElements]: NotEncodedAsReturned<TName, TElements[TIndex], TIndex extends keyof TReturn ? TReturn[TIndex] : never>;
|
|
36
|
+
}[number];
|
|
37
|
+
|
|
38
|
+
type Members<TName extends string, TMembers extends readonly unknown[], TReturn> = {
|
|
39
|
+
readonly [TIndex in keyof TMembers]: TMembers[TIndex] extends Schema.Top
|
|
40
|
+
? NotEncodedAsReturned<TName, TMembers[TIndex], Extract<TReturn, TMembers[TIndex]["Type"]>>
|
|
41
|
+
: never;
|
|
42
|
+
}[number];
|
|
43
|
+
|
|
44
|
+
type Matches<TReturn, TMembers extends readonly unknown[]> = true extends {
|
|
45
|
+
readonly [TIndex in keyof TMembers]: TMembers[TIndex] extends Schema.Top ? (TReturn extends TMembers[TIndex]["Type"] ? true : false) : false;
|
|
46
|
+
}[number]
|
|
47
|
+
? true
|
|
48
|
+
: false;
|
|
49
|
+
|
|
50
|
+
type Covered<TName extends string, TMembers extends readonly unknown[], TReturn> = {
|
|
51
|
+
readonly [TIndex in keyof TMembers]: TMembers[TIndex] extends Schema.Struct<infer TFields>
|
|
52
|
+
? Schema.Struct<TFields>["Type"] extends TReturn
|
|
53
|
+
? { readonly fields: Fields<TName, TFields, TReturn>; readonly type: Schema.Struct<TFields>["Type"] }
|
|
54
|
+
: never
|
|
55
|
+
: never;
|
|
56
|
+
}[number];
|
|
57
|
+
|
|
58
|
+
// A returned member whose discriminant is itself a union extends no single member, so it is compared with the members it covers.
|
|
59
|
+
type Unmatched<TName extends string, TMembers extends readonly unknown[], TReturn> = TReturn extends unknown
|
|
60
|
+
? Matches<TReturn, TMembers> extends true
|
|
61
|
+
? never
|
|
62
|
+
: Covered<TName, TMembers, TReturn> extends infer TCovered extends { readonly fields: unknown; readonly type: unknown }
|
|
63
|
+
? [TCovered] extends [never]
|
|
64
|
+
? never
|
|
65
|
+
: TCovered["fields"] | KeysDiffer<TName, TCovered["type"], TReturn>
|
|
66
|
+
: never
|
|
67
|
+
: never;
|
|
68
|
+
|
|
69
|
+
type InCollection<TName extends string, TSchema, TReturn> =
|
|
70
|
+
TSchema extends Schema.Union<infer TMembers>
|
|
71
|
+
? Members<TName, TMembers, TReturn> | Unmatched<TName, TMembers, TReturn>
|
|
72
|
+
: TSchema extends Schema.$Record<infer _TKey, infer TValue>
|
|
73
|
+
? NotEncodedAsReturned<TName, TValue, TReturn[keyof TReturn]>
|
|
74
|
+
: TSchema extends Schema.Option<infer TValue>
|
|
75
|
+
? NotEncodedAsReturned<TName, TValue, TReturn extends Option.Option<infer TReturned> ? TReturned : never>
|
|
76
|
+
: never;
|
|
77
|
+
|
|
78
|
+
type KeysOf<T> = T extends unknown ? keyof T : never;
|
|
79
|
+
|
|
80
|
+
type KeysDiffer<TName extends string, TType, TReturn> = [TReturn] extends [never]
|
|
81
|
+
? never
|
|
82
|
+
: Same<KeysOf<TType>, KeysOf<TReturn>> extends true
|
|
83
|
+
? never
|
|
84
|
+
: SuccessDiffersFromRun<TName, TReturn, TType>;
|
|
85
|
+
|
|
86
|
+
type InComposite<TName extends string, TSchema, TReturn> =
|
|
87
|
+
TSchema extends Schema.Struct<infer TFields>
|
|
88
|
+
? Fields<TName, TFields, TReturn> | KeysDiffer<TName, Schema.Struct<TFields>["Type"], TReturn>
|
|
89
|
+
: TSchema extends Schema.Tuple<infer TElements>
|
|
90
|
+
? Elements<TName, TElements, TReturn>
|
|
91
|
+
: InCollection<TName, TSchema, TReturn>;
|
|
92
|
+
|
|
93
|
+
type InContainer<TName extends string, TSchema, TReturn> = TSchema extends Schema.$Array<infer TItem> | Schema.NonEmptyArray<infer TItem>
|
|
94
|
+
? NotEncodedAsReturned<TName, TItem, TReturn extends readonly (infer TReturned)[] ? TReturned : never>
|
|
95
|
+
: TSchema extends Schema.mutable<infer TInner>
|
|
96
|
+
? NotEncodedAsReturned<TName, TInner, TReturn>
|
|
97
|
+
: TSchema extends Schema.mutableKey<infer TInner>
|
|
98
|
+
? NotEncodedAsReturned<TName, TInner, TReturn>
|
|
99
|
+
: TSchema extends Schema.optionalKey<infer TInner>
|
|
100
|
+
? NotEncodedAsReturned<TName, TInner, Exclude<TReturn, undefined>>
|
|
101
|
+
: InComposite<TName, TSchema, TReturn>;
|
|
102
|
+
|
|
103
|
+
// Mutual assignability passed, yet encoding still depends on what each position holds: a class position must carry exactly
|
|
104
|
+
// that class, or encoding it fails, and a struct position exactly its keys, since an optional key may sit on one side only.
|
|
105
|
+
type NotEncodedAsReturned<TName extends string, TSchema, TReturn> =
|
|
106
|
+
TSchema extends Schema.Class<infer TClass, infer _TStruct, infer _TInherited>
|
|
107
|
+
? Identical<TClass, TReturn> extends true
|
|
108
|
+
? never
|
|
109
|
+
: SuccessClassNotReturned<TName, TReturn, TClass>
|
|
110
|
+
: InContainer<TName, TSchema, TReturn>;
|
|
111
|
+
|
|
112
|
+
export type SuccessProblem<TName extends string, TSuccess, TReturn> = TSuccess extends Schema.Top
|
|
113
|
+
? Same<TSuccess["Type"], TReturn> extends true
|
|
114
|
+
? NotEncodedAsReturned<TName, TSuccess, TReturn>
|
|
115
|
+
: TSuccess extends Schema.Class<infer TClass, infer _TStruct, infer _TInherited>
|
|
116
|
+
? SuccessClassNotReturned<TName, TReturn, TClass>
|
|
117
|
+
: Same<ReadonlyArrays<TSuccess["Type"]>, ReadonlyArrays<TReturn>> extends true
|
|
118
|
+
? ArrayMutabilityDiffers<TName, "return a readonly array, or declare Schema.mutable for a mutable one">
|
|
119
|
+
: SuccessDiffersFromRun<TName, TReturn, TSuccess["Type"]>
|
|
120
|
+
: never;
|
package/src/wire.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import type { Schema } from "effect";
|
|
2
|
+
import { InternalServerError, ServiceUnavailable } from "@shivaedev/errors/taxonomy.ts";
|
|
3
|
+
import type { CommandShape, QueryShape } from "#operation.ts";
|
|
4
|
+
import { rejectionSet } from "#rejection.ts";
|
|
5
|
+
import type { ClassRejections, SpecsOf } from "#runProof.ts";
|
|
6
|
+
import type { ServedCommand, ServedQuery } from "#servedOperation.ts";
|
|
7
|
+
|
|
8
|
+
// The schemas an operation's run implies, as values: types are erased, so the server encodes and the browser decodes with these.
|
|
9
|
+
export interface WireEntry {
|
|
10
|
+
readonly rejections: ClassRejections;
|
|
11
|
+
readonly success: Schema.Top;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface Wire {
|
|
15
|
+
readonly [operation: string]: WireEntry;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export type WireEntryOf<TWire, TName> = TName extends keyof TWire ? (TWire[TName] extends WireEntry ? TWire[TName] : undefined) : undefined;
|
|
19
|
+
|
|
20
|
+
export type Wired<TOperation, TEntry> = TEntry extends WireEntry
|
|
21
|
+
? TOperation extends ServedQuery<infer TName, infer TPayload, infer _TSuccess, infer _TSpecs, infer TPolicyError, infer TPolicyServices, infer TRun>
|
|
22
|
+
? ServedQuery<TName, TPayload, TEntry["success"], SpecsOf<TEntry["rejections"]>, TPolicyError, TPolicyServices, TRun>
|
|
23
|
+
: TOperation extends ServedCommand<infer TName, infer TPayload, infer _TSuccess, infer _TSpecs, infer TRun>
|
|
24
|
+
? ServedCommand<TName, TPayload, TEntry["success"], SpecsOf<TEntry["rejections"]>, TRun>
|
|
25
|
+
: TOperation
|
|
26
|
+
: TOperation;
|
|
27
|
+
|
|
28
|
+
export type WiredAll<TOperations extends readonly unknown[], TWire> = {
|
|
29
|
+
readonly [TIndex in keyof TOperations]: Wired<
|
|
30
|
+
TOperations[TIndex],
|
|
31
|
+
WireEntryOf<TWire, TOperations[TIndex] extends { readonly name: infer TName } ? TName : never>
|
|
32
|
+
>;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export function wireRejections(rejections: ClassRejections) {
|
|
36
|
+
return { ...rejections, InternalServerError, ServiceUnavailable };
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function wired(operation: QueryShape | CommandShape, entry: WireEntry | undefined): QueryShape | CommandShape {
|
|
40
|
+
if (entry === undefined) {
|
|
41
|
+
return operation;
|
|
42
|
+
}
|
|
43
|
+
const rejections = wireRejections(entry.rejections);
|
|
44
|
+
return { ...operation, rejections, success: entry.success, ...rejectionSet(rejections) };
|
|
45
|
+
}
|