@shivaedev/contract 0.23.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.
- package/CHANGELOG.md +11 -0
- package/dist/automaticLive.d.ts +12 -5
- package/dist/automaticLive.d.ts.map +1 -1
- package/dist/automaticLive.js +20 -65
- package/dist/automaticLive.js.map +1 -1
- package/dist/internal/automaticQuery.d.ts +21 -0
- package/dist/internal/automaticQuery.d.ts.map +1 -0
- package/dist/internal/automaticQuery.js +78 -0
- package/dist/internal/automaticQuery.js.map +1 -0
- package/dist/internal/changesSuccess.d.ts +13 -0
- package/dist/internal/changesSuccess.d.ts.map +1 -0
- package/dist/internal/changesSuccess.js +16 -0
- package/dist/internal/changesSuccess.js.map +1 -0
- package/dist/internal/clientOperations.d.ts.map +1 -1
- package/dist/internal/clientOperations.js +1 -1
- package/dist/internal/clientOperations.js.map +1 -1
- package/dist/internal/compareQueries.d.ts +23 -0
- package/dist/internal/compareQueries.d.ts.map +1 -0
- package/dist/internal/compareQueries.js +56 -0
- package/dist/internal/compareQueries.js.map +1 -0
- package/dist/internal/queryDelivery.d.ts +28 -0
- package/dist/internal/queryDelivery.d.ts.map +1 -0
- package/dist/internal/queryDelivery.js +22 -0
- package/dist/internal/queryDelivery.js.map +1 -0
- package/dist/makeLiveServer.d.ts +8 -39
- package/dist/makeLiveServer.d.ts.map +1 -1
- package/dist/makeLiveServer.js +14 -8
- package/dist/makeLiveServer.js.map +1 -1
- package/dist/operation.d.ts +6 -3
- package/dist/operation.d.ts.map +1 -1
- package/dist/operation.js +1 -0
- package/dist/operation.js.map +1 -1
- package/dist/publicConfig.d.ts +3 -3
- package/dist/publicConfig.d.ts.map +1 -1
- package/dist/querySubscription.d.ts +3 -1
- package/dist/querySubscription.d.ts.map +1 -1
- package/dist/querySubscription.js +1 -1
- package/dist/querySubscription.js.map +1 -1
- package/dist/queryUpdate.d.ts +9 -0
- package/dist/queryUpdate.d.ts.map +1 -0
- package/dist/queryUpdate.js +2 -0
- package/dist/queryUpdate.js.map +1 -0
- package/dist/serverMetadata.d.ts +2 -2
- package/dist/serverMetadata.d.ts.map +1 -1
- package/dist/serverMetadata.js +2 -2
- package/dist/serverMetadata.js.map +1 -1
- package/package.json +4 -4
- package/src/automaticLive.ts +35 -73
- package/src/internal/automaticQuery.ts +113 -0
- package/src/internal/changesSuccess.ts +32 -0
- package/src/internal/clientOperations.ts +1 -0
- package/src/internal/compareQueries.ts +65 -0
- package/src/internal/queryDelivery.ts +28 -0
- package/src/makeLiveServer.ts +24 -17
- package/src/operation.ts +15 -3
- package/src/querySubscription.ts +5 -2
- package/src/queryUpdate.ts +9 -0
- package/src/serverMetadata.ts +3 -3
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { Crypto, Effect, Schema } from "effect";
|
|
2
|
+
import * as Hex from "effect/encoding/Hex";
|
|
3
|
+
import type { Key } from "@shivaedev/changes/keys.ts";
|
|
4
|
+
import { changedHint } from "#internal/queryHints.ts";
|
|
5
|
+
import type { LiveHint } from "#live.ts";
|
|
6
|
+
import type { QueryRerun, QuerySubscription } from "#querySubscription.ts";
|
|
7
|
+
import type { QueryResult, QueryUpdate } from "#queryUpdate.ts";
|
|
8
|
+
import type { QueryRequest } from "#serverMetadata.ts";
|
|
9
|
+
|
|
10
|
+
function hintsFor(hint: LiveHint, subscription: QuerySubscription<unknown, unknown>): readonly LiveHint[] {
|
|
11
|
+
const queries = subscription.queries ?? [];
|
|
12
|
+
if (!queries.some(({ query }) => query.live === "rerun-and-compare")) {
|
|
13
|
+
if (hint._tag === "Resync") {
|
|
14
|
+
return [hint];
|
|
15
|
+
}
|
|
16
|
+
return hint.keys.length > 0 ? [changedHint(hint.keys, subscription.scopes)] : [];
|
|
17
|
+
}
|
|
18
|
+
const scopes = subscription.scopes.map((scope, index) => (queries[index]?.query.live === "rerun-and-compare" ? [] : scope));
|
|
19
|
+
if (queries.every(({ query }) => query.live === "rerun-and-compare")) {
|
|
20
|
+
return [];
|
|
21
|
+
}
|
|
22
|
+
if (hint._tag === "Resync") {
|
|
23
|
+
return [hint];
|
|
24
|
+
}
|
|
25
|
+
const keys = new Set(scopes.flatMap((scope) => scope.map((key) => JSON.stringify(key))));
|
|
26
|
+
const visible: readonly Key[] = hint.keys.filter((key) => keys.has(JSON.stringify(key)));
|
|
27
|
+
return visible.length === 0 ? [] : [changedHint(visible, scopes)];
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function compareQueries<E, R>(subscription: QuerySubscription<unknown, unknown>, read?: QueryRerun<E, R>) {
|
|
31
|
+
const queries = subscription.queries ?? [];
|
|
32
|
+
const hashes = new Map<number, string>();
|
|
33
|
+
const resultFor = Effect.fnUntraced(function* (request: QueryRequest, index: number) {
|
|
34
|
+
if (read === undefined) {
|
|
35
|
+
return yield* Effect.die(new Error("Rerun-and-compare subscriptions require an authorized query runner"));
|
|
36
|
+
}
|
|
37
|
+
const value = yield* read(request);
|
|
38
|
+
const encoded = yield* Schema.encodeUnknownEffect(Schema.toCodecJson(request.query.success))(value).pipe(Effect.orDie);
|
|
39
|
+
const crypto = yield* Crypto.Crypto;
|
|
40
|
+
const digest = yield* crypto.digest("SHA-256", new TextEncoder().encode(JSON.stringify(encoded))).pipe(Effect.orDie);
|
|
41
|
+
const hash = Hex.encode(digest);
|
|
42
|
+
if (hash === hashes.get(index)) {
|
|
43
|
+
return undefined;
|
|
44
|
+
}
|
|
45
|
+
hashes.set(index, hash);
|
|
46
|
+
return { _tag: "Result", query: request.query.name, queryIndex: index, value } satisfies QueryResult;
|
|
47
|
+
});
|
|
48
|
+
return Effect.fnUntraced(function* (hint: LiveHint) {
|
|
49
|
+
const updates: QueryUpdate[] = [];
|
|
50
|
+
const affected = hint._tag === "Resync" ? undefined : changedHint(hint.keys, subscription.scopes);
|
|
51
|
+
const matching = queries
|
|
52
|
+
.map((request, index) => ({ index, request }))
|
|
53
|
+
.filter(
|
|
54
|
+
({ index, request }) =>
|
|
55
|
+
request.query.live === "rerun-and-compare" && (affected?._tag !== "Changed" || affected.queryIndexes?.includes(index)),
|
|
56
|
+
);
|
|
57
|
+
for (const { index, request } of matching) {
|
|
58
|
+
const result = yield* resultFor(request, index);
|
|
59
|
+
if (result !== undefined) {
|
|
60
|
+
updates.push(result);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return [...updates, ...hintsFor(hint, subscription)];
|
|
64
|
+
});
|
|
65
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { Effect, Stream } from "effect";
|
|
2
|
+
import * as AsyncResult from "effect/reactivity/AsyncResult";
|
|
3
|
+
import * as Atom from "effect/reactivity/Atom";
|
|
4
|
+
import * as AtomRegistry from "effect/reactivity/AtomRegistry";
|
|
5
|
+
import type { LiveHint } from "#live.ts";
|
|
6
|
+
import type { QueryUpdate } from "#queryUpdate.ts";
|
|
7
|
+
|
|
8
|
+
export function queryDelivery<A, E>(
|
|
9
|
+
atom: Atom.Atom<AsyncResult.AsyncResult<A, E>>,
|
|
10
|
+
decode: ((value: unknown) => A) | undefined,
|
|
11
|
+
paused: Atom.Atom<boolean>,
|
|
12
|
+
) {
|
|
13
|
+
const pushed = Atom.make<AsyncResult.AsyncResult<A, E>>(AsyncResult.initial());
|
|
14
|
+
const deliver = Effect.fnUntraced(function* (update: QueryUpdate): Effect.fn.Return<LiveHint, never, AtomRegistry.AtomRegistry> {
|
|
15
|
+
if (update._tag !== "Result") {
|
|
16
|
+
return update;
|
|
17
|
+
}
|
|
18
|
+
const registry = yield* AtomRegistry.AtomRegistry;
|
|
19
|
+
if (decode !== undefined && !registry.get(paused)) {
|
|
20
|
+
registry.set(pushed, AsyncResult.success(decode(update.value)));
|
|
21
|
+
}
|
|
22
|
+
return { _tag: "Changed", keys: [], queryIndexes: [] };
|
|
23
|
+
});
|
|
24
|
+
return {
|
|
25
|
+
atom: decode === undefined ? atom : pushed,
|
|
26
|
+
stream: <TError, R>(stream: Stream.Stream<QueryUpdate, TError, R>) => Stream.mapEffect(stream, deliver),
|
|
27
|
+
};
|
|
28
|
+
}
|
package/src/makeLiveServer.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { Effect, Queue, Stream } from "effect";
|
|
2
2
|
import * as Reactivity from "effect/reactivity/Reactivity";
|
|
3
3
|
import { type Key, type KeyContext, readKeys } from "@shivaedev/changes/keys.ts";
|
|
4
|
+
import { compareQueries } from "#internal/compareQueries.ts";
|
|
4
5
|
import { liveHintQueue } from "#internal/liveHintQueue.ts";
|
|
5
|
-
import { changedHint } from "#internal/queryHints.ts";
|
|
6
6
|
import type { LiveHint } from "#live.ts";
|
|
7
|
-
import type { QuerySubscription } from "#querySubscription.ts";
|
|
7
|
+
import type { QueryRerun, QuerySubscription } from "#querySubscription.ts";
|
|
8
|
+
import type { QueryUpdate } from "#queryUpdate.ts";
|
|
8
9
|
import { QueryReadDenied } from "#readPolicyErrors.ts";
|
|
9
10
|
|
|
10
11
|
export const makeLiveServer = Effect.gen(function* () {
|
|
@@ -65,19 +66,25 @@ export const makeLiveServer = Effect.gen(function* () {
|
|
|
65
66
|
reactivity.invalidateUnsafe([resyncKey]);
|
|
66
67
|
return openStreams;
|
|
67
68
|
});
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
69
|
+
function subscribe<E, R>(subscription: QuerySubscription<E, R>, context?: KeyContext): Stream.Stream<LiveHint, E | QueryReadDenied, R>;
|
|
70
|
+
function subscribe<E, R>(
|
|
71
|
+
subscription: QuerySubscription<E, R>,
|
|
72
|
+
context: KeyContext | undefined,
|
|
73
|
+
read: QueryRerun<E, R>,
|
|
74
|
+
): Stream.Stream<QueryUpdate, E | QueryReadDenied, R>;
|
|
75
|
+
function subscribe<E, R>(subscription: QuerySubscription<E, R>, context?: KeyContext, read?: QueryRerun<E, R>) {
|
|
76
|
+
return Stream.unwrap(
|
|
77
|
+
Effect.sync(() => {
|
|
78
|
+
const compare = compareQueries(subscription, read);
|
|
79
|
+
const next = Effect.fnUntraced(function* (hint: LiveHint) {
|
|
80
|
+
if (!(yield* subscription.authorized)) {
|
|
81
|
+
return yield* new QueryReadDenied({ message: "Read no longer allowed for subscribed queries" });
|
|
82
|
+
}
|
|
83
|
+
return yield* compare(hint);
|
|
84
|
+
});
|
|
85
|
+
return open(subscription.keys, context).pipe(Stream.mapEffect(next), Stream.flatMap(Stream.fromIterable));
|
|
86
|
+
}),
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
return { resync, stream: open, subscribe };
|
|
83
90
|
});
|
package/src/operation.ts
CHANGED
|
@@ -24,6 +24,7 @@ export interface OperationShape {
|
|
|
24
24
|
|
|
25
25
|
export interface QueryShape<E = unknown, R = unknown> extends OperationShape {
|
|
26
26
|
readonly kind: "query";
|
|
27
|
+
readonly live?: "rerun-and-compare" | undefined;
|
|
27
28
|
readonly readPolicy?: Bivariant<QueryReadPolicy<unknown, E, R>>;
|
|
28
29
|
reads: Bivariant<(payload: unknown, viewer: Option.Option<Viewer>) => readonly Key[]>;
|
|
29
30
|
}
|
|
@@ -49,9 +50,16 @@ interface Operation<TName extends string, TPayload extends Schema.Top, TSuccess
|
|
|
49
50
|
readonly success: TSuccess;
|
|
50
51
|
}
|
|
51
52
|
|
|
52
|
-
export interface Query<
|
|
53
|
-
|
|
53
|
+
export interface Query<
|
|
54
|
+
TName extends string,
|
|
55
|
+
TPayload extends Schema.Top,
|
|
56
|
+
TSuccess extends Schema.Top,
|
|
57
|
+
TSpecs extends RejectionSpecs,
|
|
58
|
+
R = never,
|
|
59
|
+
TLive extends "rerun-and-compare" | undefined = undefined,
|
|
60
|
+
> extends Operation<TName, TPayload, TSuccess, TSpecs> {
|
|
54
61
|
readonly kind: "query";
|
|
62
|
+
readonly live?: TLive;
|
|
55
63
|
readonly readPolicy?: QueryReadPolicy<TPayload["Type"], RejectedBy<TSpecs>, R>;
|
|
56
64
|
reads: (payload: TPayload["Type"], viewer: Option.Option<Viewer>) => readonly Key[];
|
|
57
65
|
}
|
|
@@ -80,22 +88,26 @@ export function query<
|
|
|
80
88
|
TSuccess extends Schema.Top = Schema.Void,
|
|
81
89
|
const TSpecs extends RejectionSpecs = NoRejections,
|
|
82
90
|
R = never,
|
|
91
|
+
const TLive extends "rerun-and-compare" | undefined = undefined,
|
|
83
92
|
>(
|
|
84
93
|
name: TName,
|
|
85
94
|
declaration: Declaration<TPayload, TSuccess, TSpecs> & {
|
|
95
|
+
readonly live?: TLive;
|
|
86
96
|
readonly reads: (payload: PayloadSchema<TPayload>["Type"], viewer: Option.Option<Viewer>) => readonly Key[];
|
|
87
97
|
readonly readPolicy?: QueryReadPolicy<PayloadSchema<TPayload>["Type"], NoInfer<RejectedBy<TSpecs>>, R>;
|
|
88
98
|
},
|
|
89
|
-
): NoInfer<Query<TName, PayloadSchema<TPayload>, TSuccess, TSpecs, R>>;
|
|
99
|
+
): NoInfer<Query<TName, PayloadSchema<TPayload>, TSuccess, TSpecs, R, TLive>>;
|
|
90
100
|
export function query(
|
|
91
101
|
name: string,
|
|
92
102
|
declaration: Loose & {
|
|
103
|
+
readonly live?: "rerun-and-compare" | undefined;
|
|
93
104
|
readonly reads: (payload: unknown, viewer: Option.Option<Viewer>) => readonly Key[];
|
|
94
105
|
readonly readPolicy?: QueryReadPolicy<unknown, unknown, unknown>;
|
|
95
106
|
},
|
|
96
107
|
): QueryShape {
|
|
97
108
|
return {
|
|
98
109
|
...operation("query", name, declaration),
|
|
110
|
+
live: declaration.live,
|
|
99
111
|
reads: declaration.reads,
|
|
100
112
|
...(declaration.readPolicy === undefined ? {} : { readPolicy: declaration.readPolicy }),
|
|
101
113
|
};
|
package/src/querySubscription.ts
CHANGED
|
@@ -3,11 +3,14 @@ import type { Rpc } from "effect/rpc";
|
|
|
3
3
|
import type { Key } from "@shivaedev/changes/keys.ts";
|
|
4
4
|
import { checkQuery, type PolicyChannels, type Viewer } from "#readPolicy.ts";
|
|
5
5
|
import type { MissingReadPolicy, QueryReadDenied } from "#readPolicyErrors.ts";
|
|
6
|
-
import { ChangesQueries, type QueryReference } from "#serverMetadata.ts";
|
|
6
|
+
import { ChangesQueries, type QueryReference, type QueryRequest } from "#serverMetadata.ts";
|
|
7
|
+
|
|
8
|
+
export type QueryRerun<E = unknown, R = never> = (request: QueryRequest) => Effect.Effect<unknown, E, R>;
|
|
7
9
|
|
|
8
10
|
export interface QuerySubscription<E = never, R = never> {
|
|
9
11
|
readonly authorized: Effect.Effect<boolean, E, R>;
|
|
10
12
|
readonly keys: readonly Key[];
|
|
13
|
+
readonly queries?: readonly QueryRequest[];
|
|
11
14
|
readonly scopes: readonly (readonly Key[])[];
|
|
12
15
|
}
|
|
13
16
|
|
|
@@ -29,6 +32,6 @@ export function querySubscription(rpc: Rpc.Any, references: readonly QueryRefere
|
|
|
29
32
|
Effect.forEach(queries, ({ query, payload }) => query.readPolicy?.(payload, viewer) ?? Effect.succeed(false)),
|
|
30
33
|
(permissions) => permissions.every(Boolean),
|
|
31
34
|
);
|
|
32
|
-
return { authorized, keys: scopes.flat(), scopes } satisfies QuerySubscription<unknown, unknown>;
|
|
35
|
+
return { authorized, keys: scopes.flat(), queries, scopes } satisfies QuerySubscription<unknown, unknown>;
|
|
33
36
|
})();
|
|
34
37
|
}
|
package/src/serverMetadata.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { Context, Effect, Option, Schema } from "effect";
|
|
|
2
2
|
import { Rpc, type RpcSchema } from "effect/rpc";
|
|
3
3
|
import type { Key } from "@shivaedev/changes/keys.ts";
|
|
4
4
|
import type { QueryServices } from "#contract.ts";
|
|
5
|
-
import {
|
|
5
|
+
import { changesSuccess } from "#internal/changesSuccess.ts";
|
|
6
6
|
import type { CommandShape, QueryShape, StreamShape } from "#operation.ts";
|
|
7
7
|
import type { Viewer } from "#readPolicy.ts";
|
|
8
8
|
import { MissingReadPolicy, QueryReadDenied } from "#readPolicyErrors.ts";
|
|
@@ -67,7 +67,7 @@ export function changesRpc<const TName extends string, const TQueries extends re
|
|
|
67
67
|
): Rpc.Rpc<
|
|
68
68
|
`${TName}.$changes`,
|
|
69
69
|
Schema.Struct<{ queries: ReturnType<typeof referenceSchemas<TQueries>> }>,
|
|
70
|
-
RpcSchema.Stream<typeof
|
|
70
|
+
RpcSchema.Stream<ReturnType<typeof changesSuccess<TQueries>>, ChangesError<TQueries>>,
|
|
71
71
|
Schema.Never,
|
|
72
72
|
never,
|
|
73
73
|
QueryServices<TQueries[number]>
|
|
@@ -77,7 +77,7 @@ export function changesRpc(name: string, queries: readonly QueryShape[]) {
|
|
|
77
77
|
error: Schema.Union([MissingReadPolicy, QueryReadDenied, ...queries.map((query) => query.error)]),
|
|
78
78
|
payload: { queries: referenceSchemas(queries) },
|
|
79
79
|
stream: true,
|
|
80
|
-
success:
|
|
80
|
+
success: changesSuccess(queries),
|
|
81
81
|
}).annotate(ChangesQueries, { resolve: (references) => resolveQueries(queries, references) });
|
|
82
82
|
}
|
|
83
83
|
|