@shivaedev/contract 0.30.2 → 0.32.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 +107 -38
- package/dist/CommandGuard.d.ts +10 -0
- package/dist/CommandGuard.d.ts.map +1 -0
- package/dist/CommandGuard.js +20 -0
- package/dist/CommandGuard.js.map +1 -0
- package/dist/LiveServer.d.ts +6 -4
- package/dist/LiveServer.d.ts.map +1 -1
- package/dist/LiveServer.js +10 -5
- package/dist/LiveServer.js.map +1 -1
- package/dist/ReadPolicyGuard.d.ts +19 -0
- package/dist/ReadPolicyGuard.d.ts.map +1 -0
- package/dist/ReadPolicyGuard.js +23 -0
- package/dist/ReadPolicyGuard.js.map +1 -0
- package/dist/adapters/activityState.d.ts +9 -0
- package/dist/adapters/activityState.d.ts.map +1 -0
- package/dist/adapters/activityState.js +11 -0
- package/dist/adapters/activityState.js.map +1 -0
- package/dist/{internal → adapters}/bindingOwner.d.ts +1 -1
- package/dist/{internal → adapters}/bindingOwner.d.ts.map +1 -1
- package/dist/{internal → adapters}/bindingOwner.js +2 -1
- package/dist/adapters/bindingOwner.js.map +1 -0
- package/dist/adapters/clientId.d.ts +2 -0
- package/dist/adapters/clientId.d.ts.map +1 -0
- package/dist/adapters/clientId.js +4 -0
- package/dist/adapters/clientId.js.map +1 -0
- package/dist/automaticLive.d.ts +1 -1
- package/dist/automaticLive.d.ts.map +1 -1
- package/dist/automaticLive.js +7 -2
- package/dist/automaticLive.js.map +1 -1
- package/dist/bind.d.ts +5 -5
- package/dist/bind.d.ts.map +1 -1
- package/dist/bind.js +1 -1
- package/dist/bind.js.map +1 -1
- package/dist/changesHandlerLayer.d.ts +19 -0
- package/dist/changesHandlerLayer.d.ts.map +1 -0
- package/dist/changesHandlerLayer.js +15 -0
- package/dist/changesHandlerLayer.js.map +1 -0
- package/dist/clientActivity.d.ts +10 -1
- package/dist/clientActivity.d.ts.map +1 -1
- package/dist/clientActivity.js +7 -0
- package/dist/clientActivity.js.map +1 -1
- package/dist/clientKeys.d.ts +7 -0
- package/dist/clientKeys.d.ts.map +1 -0
- package/dist/clientKeys.js +11 -0
- package/dist/clientKeys.js.map +1 -0
- package/dist/clientOptions.d.ts +3 -0
- package/dist/clientOptions.d.ts.map +1 -1
- package/dist/composeBindings.d.ts +8 -0
- package/dist/composeBindings.d.ts.map +1 -0
- package/dist/composeBindings.js +18 -0
- package/dist/composeBindings.js.map +1 -0
- package/dist/contract.d.ts +5 -8
- package/dist/contract.d.ts.map +1 -1
- package/dist/contract.js +11 -7
- package/dist/contract.js.map +1 -1
- package/dist/internal/automaticQuery.d.ts +3 -1
- package/dist/internal/automaticQuery.d.ts.map +1 -1
- package/dist/internal/automaticQuery.js +9 -2
- package/dist/internal/automaticQuery.js.map +1 -1
- package/dist/internal/changesSuccess.d.ts +7 -2
- package/dist/internal/changesSuccess.d.ts.map +1 -1
- package/dist/internal/changesSuccess.js +12 -6
- package/dist/internal/changesSuccess.js.map +1 -1
- package/dist/internal/clientActivity.d.ts +5 -6
- package/dist/internal/clientActivity.d.ts.map +1 -1
- package/dist/internal/clientActivity.js +6 -18
- package/dist/internal/clientActivity.js.map +1 -1
- package/dist/internal/clientOperations.d.ts +1 -1
- package/dist/internal/clientOperations.d.ts.map +1 -1
- package/dist/internal/clientOperations.js +7 -4
- package/dist/internal/clientOperations.js.map +1 -1
- package/dist/internal/compareQueries.d.ts +2 -1
- package/dist/internal/compareQueries.d.ts.map +1 -1
- package/dist/internal/compareQueries.js +5 -7
- package/dist/internal/compareQueries.js.map +1 -1
- package/dist/internal/declarationError.d.ts +7 -1
- package/dist/internal/declarationError.d.ts.map +1 -1
- package/dist/internal/declarationError.js.map +1 -1
- package/dist/internal/liveAttempt.d.ts +13 -0
- package/dist/internal/liveAttempt.d.ts.map +1 -0
- package/dist/internal/liveAttempt.js +28 -0
- package/dist/internal/liveAttempt.js.map +1 -0
- package/dist/internal/prependMiddleware.d.ts +10 -0
- package/dist/internal/prependMiddleware.d.ts.map +1 -0
- package/dist/internal/prependMiddleware.js +12 -0
- package/dist/internal/prependMiddleware.js.map +1 -0
- package/dist/internal/queryDelivery.d.ts +4 -21
- package/dist/internal/queryDelivery.d.ts.map +1 -1
- package/dist/internal/queryDelivery.js +8 -1
- package/dist/internal/queryDelivery.js.map +1 -1
- package/dist/internal/queryHints.d.ts +3 -2
- package/dist/internal/queryHints.d.ts.map +1 -1
- package/dist/internal/queryHints.js +1 -1
- package/dist/internal/queryHints.js.map +1 -1
- package/dist/internal/queryReactivity.d.ts.map +1 -1
- package/dist/internal/queryReactivity.js +3 -11
- package/dist/internal/queryReactivity.js.map +1 -1
- package/dist/internal/querySignals.d.ts +8 -0
- package/dist/internal/querySignals.d.ts.map +1 -0
- package/dist/internal/querySignals.js +42 -0
- package/dist/internal/querySignals.js.map +1 -0
- package/dist/internal/wrapAtom.d.ts +3 -0
- package/dist/internal/wrapAtom.d.ts.map +1 -0
- package/dist/internal/wrapAtom.js +15 -0
- package/dist/internal/wrapAtom.js.map +1 -0
- package/dist/live.d.ts +7 -4
- package/dist/live.d.ts.map +1 -1
- package/dist/live.js +21 -28
- package/dist/live.js.map +1 -1
- package/dist/makeLiveServer.d.ts +23 -6
- package/dist/makeLiveServer.d.ts.map +1 -1
- package/dist/makeLiveServer.js +42 -26
- package/dist/makeLiveServer.js.map +1 -1
- package/dist/publicConfig.d.ts +19 -3
- package/dist/publicConfig.d.ts.map +1 -1
- package/dist/querySignals.d.ts +9 -0
- package/dist/querySignals.d.ts.map +1 -0
- package/dist/querySignals.js +14 -0
- package/dist/querySignals.js.map +1 -0
- package/dist/querySubscription.d.ts +3 -2
- package/dist/querySubscription.d.ts.map +1 -1
- package/dist/querySubscription.js +11 -5
- package/dist/querySubscription.js.map +1 -1
- package/dist/queryUpdate.d.ts +7 -1
- package/dist/queryUpdate.d.ts.map +1 -1
- package/dist/testing.d.ts +4 -40
- package/dist/testing.d.ts.map +1 -1
- package/dist/testing.js.map +1 -1
- package/package.json +10 -10
- package/src/CommandGuard.ts +25 -0
- package/src/LiveServer.ts +18 -9
- package/src/ReadPolicyGuard.ts +31 -0
- package/src/adapters/activityState.ts +12 -0
- package/src/{internal → adapters}/bindingOwner.ts +3 -2
- package/src/adapters/clientId.ts +3 -0
- package/src/automaticLive.ts +8 -3
- package/src/bind.ts +10 -9
- package/src/changesHandlerLayer.ts +51 -0
- package/src/clientActivity.ts +8 -1
- package/src/clientKeys.ts +17 -0
- package/src/clientOptions.ts +3 -0
- package/src/composeBindings.ts +27 -0
- package/src/contract.ts +14 -20
- package/src/internal/automaticQuery.ts +11 -1
- package/src/internal/changesSuccess.ts +16 -9
- package/src/internal/clientActivity.ts +37 -52
- package/src/internal/clientOperations.ts +9 -5
- package/src/internal/compareQueries.ts +6 -8
- package/src/internal/declarationError.ts +12 -1
- package/src/internal/liveAttempt.ts +44 -0
- package/src/internal/prependMiddleware.ts +15 -0
- package/src/internal/queryDelivery.ts +11 -3
- package/src/internal/queryHints.ts +3 -2
- package/src/internal/queryReactivity.ts +4 -11
- package/src/internal/querySignals.ts +46 -0
- package/src/internal/wrapAtom.ts +14 -0
- package/src/live.ts +25 -31
- package/src/makeLiveServer.ts +73 -60
- package/src/querySignals.ts +22 -0
- package/src/querySubscription.ts +13 -8
- package/src/queryUpdate.ts +7 -1
- package/src/testing.ts +5 -5
- package/dist/internal/bindingOwner.js.map +0 -1
|
@@ -12,15 +12,21 @@ export function querySubscription(rpc, references, viewer) {
|
|
|
12
12
|
const context = yield* policyContext(Rpc.make(`${prefix}${query.name}`));
|
|
13
13
|
return {
|
|
14
14
|
check: Effect.provideContext(checkQuery(query, payload, viewer), context),
|
|
15
|
-
keys:
|
|
16
|
-
|
|
15
|
+
keys: Effect.gen(function* () {
|
|
16
|
+
const current = yield* policyContext(Rpc.make(`${prefix}${query.name}`));
|
|
17
|
+
return yield* queryReadKeys(query, payload, viewer).pipe(Effect.provideContext(current));
|
|
18
|
+
}),
|
|
19
|
+
permitted: Effect.gen(function* () {
|
|
20
|
+
const current = yield* policyContext(Rpc.make(`${prefix}${query.name}`));
|
|
21
|
+
return yield* Effect.suspend(() => query.readPolicy?.(payload, viewer) ?? Effect.succeed(false)).pipe(Effect.provideContext(current));
|
|
22
|
+
}),
|
|
17
23
|
};
|
|
18
24
|
}));
|
|
19
25
|
yield* Effect.forEach(policies, ({ check }) => check);
|
|
20
|
-
const
|
|
26
|
+
const readScopes = Effect.forEach(policies, ({ keys }) => keys);
|
|
27
|
+
const scopes = yield* readScopes;
|
|
21
28
|
const authorized = Effect.map(Effect.forEach(policies, ({ permitted }) => permitted), (permissions) => permissions.every(Boolean));
|
|
22
|
-
|
|
23
|
-
return { authorized, globalKeys, keys: scopes.flat(), queries, scopes };
|
|
29
|
+
return { authorized, keys: scopes.flat(), queries, readScopes, scopes };
|
|
24
30
|
})();
|
|
25
31
|
}
|
|
26
32
|
//# sourceMappingURL=querySubscription.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"querySubscription.js","sourceRoot":"","sources":["../src/querySubscription.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,EAAe,MAAM,QAAQ,CAAC;AACtD,OAAO,EAAE,GAAG,EAAE,MAAM,YAAY,CAAC;AAEjC,OAAO,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAC;AAC3D,OAAO,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAC;AAC3D,OAAO,EAAE,UAAU,EAAoC,MAAM,gBAAgB,CAAC;AAE9E,OAAO,EAAE,cAAc,EAA0C,MAAM,oBAAoB,CAAC;
|
|
1
|
+
{"version":3,"file":"querySubscription.js","sourceRoot":"","sources":["../src/querySubscription.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,EAAe,MAAM,QAAQ,CAAC;AACtD,OAAO,EAAE,GAAG,EAAE,MAAM,YAAY,CAAC;AAEjC,OAAO,EAAE,aAAa,EAAE,MA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EAAE,CAAC;AACN,CAAC"}
|
package/dist/queryUpdate.d.ts
CHANGED
|
@@ -1,9 +1,15 @@
|
|
|
1
1
|
import type { LiveHint } from "#live.ts";
|
|
2
|
+
import type { QueryReadDenied } from "#readPolicyErrors.ts";
|
|
2
3
|
export interface QueryResult {
|
|
3
4
|
readonly _tag: "Result";
|
|
4
5
|
readonly query: string;
|
|
5
6
|
readonly queryIndex: number;
|
|
6
7
|
readonly value: unknown;
|
|
7
8
|
}
|
|
8
|
-
export
|
|
9
|
+
export interface QueryUnchanged {
|
|
10
|
+
readonly _tag: "Unchanged";
|
|
11
|
+
readonly queryIndex: number;
|
|
12
|
+
}
|
|
13
|
+
export type SubscriptionHint = LiveHint | QueryReadDenied;
|
|
14
|
+
export type QueryUpdate = SubscriptionHint | QueryResult | QueryUnchanged;
|
|
9
15
|
//# sourceMappingURL=queryUpdate.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"queryUpdate.d.ts","sourceRoot":"","sources":["../src/queryUpdate.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC;
|
|
1
|
+
{"version":3,"file":"queryUpdate.d.ts","sourceRoot":"","sources":["../src/queryUpdate.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC;AACzC,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AAE5D,MAAM,WAAW,WAAW;IAC3B,QAAQ,CAAC,IAAI,EAAE,QAAQ,CAAC;IACxB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC5B,QAAQ,CAAC,KAAK,EAAE,OAAO,CAAC;CACxB;AACD,MAAM,WAAW,cAAc;IAC9B,QAAQ,CAAC,IAAI,EAAE,WAAW,CAAC;IAC3B,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;CAC5B;AACD,MAAM,MAAM,gBAAgB,GAAG,QAAQ,GAAG,eAAe,CAAC;AAC1D,MAAM,MAAM,WAAW,GAAG,gBAAgB,GAAG,WAAW,GAAG,cAAc,CAAC"}
|
package/dist/testing.d.ts
CHANGED
|
@@ -1,50 +1,14 @@
|
|
|
1
1
|
import { type Duration, Effect, Layer, Stream, SubscriptionRef } from "effect";
|
|
2
2
|
import { type Rpc, RpcClient, type RpcGroup, RpcSerialization, RpcServer } from "effect/rpc";
|
|
3
3
|
import type { Key } from "@shivaedev/changes/keys.ts";
|
|
4
|
-
import type {
|
|
4
|
+
import type { SubscriptionHint } from "#queryUpdate.ts";
|
|
5
5
|
export interface HintViewer<E> {
|
|
6
6
|
readonly finished: Effect.Effect<void, E>;
|
|
7
|
-
readonly hints: SubscriptionRef.SubscriptionRef<readonly
|
|
7
|
+
readonly hints: SubscriptionRef.SubscriptionRef<readonly SubscriptionHint[]>;
|
|
8
8
|
}
|
|
9
|
-
export declare const watchHints: <E, R>(stream: Stream.Stream<{
|
|
10
|
-
readonly _tag: "Changed";
|
|
11
|
-
readonly keys: readonly ({
|
|
12
|
-
readonly _tag: "Bucket";
|
|
13
|
-
readonly collection: string;
|
|
14
|
-
readonly field: string;
|
|
15
|
-
readonly value: string | number;
|
|
16
|
-
} | {
|
|
17
|
-
readonly _tag: "List";
|
|
18
|
-
readonly collection: string;
|
|
19
|
-
} | {
|
|
20
|
-
readonly _tag: "Item";
|
|
21
|
-
readonly collection: string;
|
|
22
|
-
readonly id: string | number;
|
|
23
|
-
})[];
|
|
24
|
-
readonly queryIndexes?: readonly number[] | undefined;
|
|
25
|
-
} | {
|
|
26
|
-
readonly _tag: "Resync";
|
|
27
|
-
}, E, R>) => Effect.Effect<{
|
|
9
|
+
export declare const watchHints: <E, R>(stream: Stream.Stream<SubscriptionHint, E, R>) => Effect.Effect<{
|
|
28
10
|
finished: Effect.Effect<void, E, never>;
|
|
29
|
-
hints: SubscriptionRef.SubscriptionRef<readonly
|
|
30
|
-
readonly _tag: "Changed";
|
|
31
|
-
readonly keys: readonly ({
|
|
32
|
-
readonly _tag: "Bucket";
|
|
33
|
-
readonly collection: string;
|
|
34
|
-
readonly field: string;
|
|
35
|
-
readonly value: string | number;
|
|
36
|
-
} | {
|
|
37
|
-
readonly _tag: "List";
|
|
38
|
-
readonly collection: string;
|
|
39
|
-
} | {
|
|
40
|
-
readonly _tag: "Item";
|
|
41
|
-
readonly collection: string;
|
|
42
|
-
readonly id: string | number;
|
|
43
|
-
})[];
|
|
44
|
-
readonly queryIndexes?: readonly number[] | undefined;
|
|
45
|
-
} | {
|
|
46
|
-
readonly _tag: "Resync";
|
|
47
|
-
})[]>;
|
|
11
|
+
hints: SubscriptionRef.SubscriptionRef<readonly SubscriptionHint[]>;
|
|
48
12
|
}, E, R | import("effect/Scope").Scope>;
|
|
49
13
|
export declare const heard: <E>(viewer: HintViewer<E>, keys: readonly Key[], options?: {
|
|
50
14
|
readonly within?: Duration.Input;
|
package/dist/testing.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"testing.d.ts","sourceRoot":"","sources":["../src/testing.ts"],"names":[],"mappings":"AAAA,OAAO,EAAY,KAAK,QAAQ,EAAE,MAAM,EAAS,KAAK,EAAS,MAAM,EAAE,eAAe,EAAE,MAAM,QAAQ,CAAC;AACvG,OAAO,EAAE,KAAK,GAAG,EAAE,SAAS,EAAE,KAAK,QAAQ,EAAmB,gBAAgB,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAC9G,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,4BAA4B,CAAC;AACtD,OAAO,KAAK,EAAE,
|
|
1
|
+
{"version":3,"file":"testing.d.ts","sourceRoot":"","sources":["../src/testing.ts"],"names":[],"mappings":"AAAA,OAAO,EAAY,KAAK,QAAQ,EAAE,MAAM,EAAS,KAAK,EAAS,MAAM,EAAE,eAAe,EAAE,MAAM,QAAQ,CAAC;AACvG,OAAO,EAAE,KAAK,GAAG,EAAE,SAAS,EAAE,KAAK,QAAQ,EAAmB,gBAAgB,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAC9G,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,4BAA4B,CAAC;AACtD,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAC;AAExD,MAAM,WAAW,UAAU,CAAC,CAAC;IAC5B,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC;IAC1C,QAAQ,CAAC,KAAK,EAAE,eAAe,CAAC,eAAe,CAAC,SAAS,gBAAgB,EAAE,CAAC,CAAC;CAC7E;AAED,eAAO,MAAM,UAAU,GAA2C,CAAC,EAAE,CAAC;;;uCAYpE,CAAC;AAEH,eAAO,MAAM,KAAK,GAAsC,CAAC;sBAG3B,QAAQ,CAAC,KAAK;kDAU1C,CAAC;AAMH,wBAAgB,mBAAmB,CAAC,KAAK,SAAS,GAAG,CAAC,GAAG,EAAE,KAAK,EAAE,QAAQ,CAAC,QAAQ,CAAC,KAAK,CAAC,4TAqCzF"}
|
package/dist/testing.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"testing.js","sourceRoot":"","sources":["../src/testing.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAiB,MAAM,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,eAAe,EAAE,MAAM,QAAQ,CAAC;AACvG,OAAO,EAAY,SAAS,EAAkC,gBAAgB,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAS9G,MAAM,CAAC,MAAM,UAAU,GAAG,MAAM,CAAC,EAAE,CAAC,iBAAiB,CAAC,CAAC,QAAQ,CAAC,EAAQ,
|
|
1
|
+
{"version":3,"file":"testing.js","sourceRoot":"","sources":["../src/testing.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAiB,MAAM,EAAE,KAAK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AAC,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC;gBAC3F,WAAW,EAAE,IAAI;gBACjB,qBAAqB,EAAE,KAAK;aAC5B,CAAC;QACH,CAAC,CAAC,CACF,CAAC;QACF,OAAO;YACN,GAAG,QAAQ;YACX,GAAG,EAAE,CAAC,QAAQ,EAAE,OAAO,EAAE,EAAE,CAAC,QAAQ,CAAC,GAAG,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,KAAK,CAAC,WAAW,EAAE,QAAQ,CAAC,CAAC,CAAC;SAC7E,CAAC;IAC3C,CAAC,CAAC,CACF,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,gBAAgB,CAAC,WAAW,CAAC,CAAC,CAAC;AACrD,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@shivaedev/contract",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.32.0",
|
|
4
4
|
"description": "Typed query and command contracts over native Effect RPC and reactivity",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"effect"
|
|
@@ -43,21 +43,21 @@
|
|
|
43
43
|
"README.md"
|
|
44
44
|
],
|
|
45
45
|
"dependencies": {
|
|
46
|
-
"@shivaedev/changes": "0.
|
|
46
|
+
"@shivaedev/changes": "0.9.0",
|
|
47
47
|
"@shivaedev/service": "0.6.1",
|
|
48
48
|
"@shivaedev/types": "0.3.0"
|
|
49
49
|
},
|
|
50
50
|
"devDependencies": {
|
|
51
51
|
"@effect/platform-node": "4.0.6",
|
|
52
52
|
"@effect/vitest": "4.0.6",
|
|
53
|
-
"@shivaedev/errors": "0.
|
|
54
|
-
"@shivaedev/identity": "0.
|
|
55
|
-
"@shivaedev/quality": "0.
|
|
56
|
-
"@shivaedev/server": "0.
|
|
57
|
-
"@shivaedev/sql": "0.
|
|
58
|
-
"@shivaedev/test": "0.
|
|
59
|
-
"@shivaedev/test-sql": "0.
|
|
60
|
-
"@shivaedev/test-story": "0.
|
|
53
|
+
"@shivaedev/errors": "0.6.0",
|
|
54
|
+
"@shivaedev/identity": "0.20.0",
|
|
55
|
+
"@shivaedev/quality": "0.57.0",
|
|
56
|
+
"@shivaedev/server": "0.20.0",
|
|
57
|
+
"@shivaedev/sql": "0.34.0",
|
|
58
|
+
"@shivaedev/test": "0.14.0",
|
|
59
|
+
"@shivaedev/test-sql": "0.17.0",
|
|
60
|
+
"@shivaedev/test-story": "0.35.0",
|
|
61
61
|
"@types/node": "24.19.2",
|
|
62
62
|
"effect": "4.0.6",
|
|
63
63
|
"esbuild": "0.28.2",
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { Context, Effect, Layer, Option } from "effect";
|
|
2
|
+
import { type Rpc, type RpcGroup, RpcMiddleware } from "effect/rpc";
|
|
3
|
+
import { invalidationKeys } from "@shivaedev/changes/keys.ts";
|
|
4
|
+
import { prependMiddleware } from "#internal/prependMiddleware.ts";
|
|
5
|
+
import { CurrentViewer } from "#readPolicy.ts";
|
|
6
|
+
import { CommandInvalidation } from "#serverMetadata.ts";
|
|
7
|
+
|
|
8
|
+
export type CommandBoundary = <A, E, R>(effect: Effect.Effect<A, E, R>, keys: (result: A) => readonly unknown[]) => Effect.Effect<A, E, R>;
|
|
9
|
+
|
|
10
|
+
export class CommandGuard extends RpcMiddleware.Service<CommandGuard>()("@shivaedev/contract/CommandGuard") {}
|
|
11
|
+
|
|
12
|
+
export function commandGuardLayer(boundary: CommandBoundary): Layer.Layer<CommandGuard> {
|
|
13
|
+
return Layer.succeed(CommandGuard, (effect, { rpc, payload }) => {
|
|
14
|
+
const declaration = Context.getOption(rpc.annotations, CommandInvalidation);
|
|
15
|
+
return Option.isNone(declaration)
|
|
16
|
+
? effect
|
|
17
|
+
: Effect.flatMap(CurrentViewer, (viewer) =>
|
|
18
|
+
boundary(effect, (result) => invalidationKeys(declaration.value.invalidates(payload, result, viewer))),
|
|
19
|
+
);
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function guardCommands<R extends Rpc.Any>(group: RpcGroup.RpcGroup<R>) {
|
|
24
|
+
return prependMiddleware(group.middleware(CommandGuard), CommandGuard.key);
|
|
25
|
+
}
|
package/src/LiveServer.ts
CHANGED
|
@@ -1,20 +1,29 @@
|
|
|
1
|
-
import { Effect } from "effect";
|
|
1
|
+
import { Effect, type Stream } from "effect";
|
|
2
2
|
import * as Reactivity from "effect/reactivity/Reactivity";
|
|
3
|
-
import type { Key
|
|
3
|
+
import type { Key } from "@shivaedev/changes/keys.ts";
|
|
4
4
|
import { defineService } from "@shivaedev/service/defineService.ts";
|
|
5
5
|
import { genericMethod } from "@shivaedev/service/genericMethod.ts";
|
|
6
6
|
import { makeLiveServer } from "#makeLiveServer.ts";
|
|
7
7
|
import type { QueryRerun, QuerySubscription } from "#querySubscription.ts";
|
|
8
|
+
import type { QueryUpdate, SubscriptionHint } from "#queryUpdate.ts";
|
|
9
|
+
import type { QueryReadDenied } from "#readPolicyErrors.ts";
|
|
8
10
|
|
|
9
11
|
export const LiveServer = defineService({
|
|
10
12
|
id: "@shivaedev/contract/LiveServer",
|
|
11
13
|
initialize: makeLiveServer,
|
|
12
|
-
methods: (server) =>
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
)
|
|
18
|
-
|
|
14
|
+
methods: (server) => {
|
|
15
|
+
function subscribe<E, R, TRead extends QueryRerun<unknown, unknown> | undefined = undefined>(
|
|
16
|
+
subscription: QuerySubscription<E, R>,
|
|
17
|
+
read?: TRead & (QueryRerun<E, R> | undefined),
|
|
18
|
+
): Effect.Effect<Stream.Stream<TRead extends undefined ? SubscriptionHint : QueryUpdate, E | QueryReadDenied, R>>;
|
|
19
|
+
function subscribe<E, R>(subscription: QuerySubscription<E, R>, read?: QueryRerun<E, R>) {
|
|
20
|
+
return Effect.succeed(read === undefined ? server.subscribe(subscription) : server.subscribe(subscription, read));
|
|
21
|
+
}
|
|
22
|
+
return {
|
|
23
|
+
resync: () => server.resync,
|
|
24
|
+
stream: (keys: readonly Key[]) => Effect.succeed(server.stream(keys)),
|
|
25
|
+
subscribe: genericMethod(subscribe),
|
|
26
|
+
};
|
|
27
|
+
},
|
|
19
28
|
requires: [Reactivity.Reactivity],
|
|
20
29
|
});
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { Context, Effect, Layer, Schema, type Types } from "effect";
|
|
2
|
+
import { type Rpc, RpcMiddleware } from "effect/rpc";
|
|
3
|
+
import { authorizeReads, CurrentViewer } from "#readPolicy.ts";
|
|
4
|
+
import { MissingReadPolicy, QueryReadDenied } from "#readPolicyErrors.ts";
|
|
5
|
+
import { WebSocketRequired } from "#WebSocketRequired.ts";
|
|
6
|
+
|
|
7
|
+
function readPolicyGuard<A, E, R>(
|
|
8
|
+
effect: Effect.Effect<A, E, R>,
|
|
9
|
+
request: { readonly rpc: Rpc.Any; readonly payload: unknown },
|
|
10
|
+
): Effect.Effect<A, E | MissingReadPolicy | QueryReadDenied | WebSocketRequired | Types.unhandled, R> {
|
|
11
|
+
return Effect.flatMap(CurrentViewer, (viewer) => authorizeReads(effect, request, viewer));
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const middleware = RpcMiddleware.Service<never>()("@shivaedev/contract/ReadPolicyGuard", {
|
|
15
|
+
error: Schema.Union([MissingReadPolicy, QueryReadDenied, WebSocketRequired]),
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
export const ReadPolicyGuard = Object.assign(Context.Reference("@shivaedev/contract/ReadPolicyGuard", { defaultValue: () => readPolicyGuard }), {
|
|
19
|
+
[RpcMiddleware.TypeId]: middleware[RpcMiddleware.TypeId],
|
|
20
|
+
"~ClientError": middleware["~ClientError"],
|
|
21
|
+
error: middleware.error,
|
|
22
|
+
requiredForClient: middleware.requiredForClient,
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
export function readPolicyGuardLayer(options: { readonly websocket?: boolean } = {}) {
|
|
26
|
+
return Layer.succeed(ReadPolicyGuard, (effect, request) =>
|
|
27
|
+
!options.websocket && request.rpc._tag.endsWith(".$changes")
|
|
28
|
+
? Effect.fail(new WebSocketRequired({ message: `${request.rpc._tag} needs the WebSocket protocol` }))
|
|
29
|
+
: readPolicyGuard(effect, request),
|
|
30
|
+
);
|
|
31
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Effect, SubscriptionRef } from "effect";
|
|
2
|
+
import type { ClientActivityState } from "#clientActivity.ts";
|
|
3
|
+
|
|
4
|
+
export function activityState() {
|
|
5
|
+
const state = Effect.runSync(SubscriptionRef.make<ClientActivityState>({ liveHints: 0, liveInvalidations: 0, refreshingAtoms: 0, rpcCalls: 0 }));
|
|
6
|
+
return {
|
|
7
|
+
activity: Object.assign(SubscriptionRef.changes(state), { current: SubscriptionRef.get(state) }),
|
|
8
|
+
change: (key: keyof ClientActivityState, delta: number) => {
|
|
9
|
+
Effect.runSync(SubscriptionRef.update(state, (current) => ({ ...current, [key]: current[key] + delta })));
|
|
10
|
+
},
|
|
11
|
+
};
|
|
12
|
+
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type * as AtomRegistry from "effect/reactivity/AtomRegistry";
|
|
2
|
-
import type { KeyContext } from "
|
|
2
|
+
import type { KeyContext } from "#clientKeys.ts";
|
|
3
|
+
import { clientId } from "./clientId.ts";
|
|
3
4
|
|
|
4
5
|
export interface BindingOwner {
|
|
5
6
|
readonly context: KeyContext;
|
|
@@ -17,7 +18,7 @@ export function bindingOwner(scope: "session" | undefined, registry?: AtomRegist
|
|
|
17
18
|
}
|
|
18
19
|
let context = contexts.get(registry);
|
|
19
20
|
if (context === undefined) {
|
|
20
|
-
context = { scope:
|
|
21
|
+
context = { scope: clientId() };
|
|
21
22
|
contexts.set(registry, context);
|
|
22
23
|
}
|
|
23
24
|
return { context, registry };
|
package/src/automaticLive.ts
CHANGED
|
@@ -5,13 +5,15 @@ import * as Atom from "effect/reactivity/Atom";
|
|
|
5
5
|
import type * as AtomRpc from "effect/reactivity/AtomRpc";
|
|
6
6
|
import type { Rpc, RpcSchema } from "effect/rpc";
|
|
7
7
|
import type { Key } from "@shivaedev/changes/keys.ts";
|
|
8
|
+
import type { BindingOwner } from "#adapters/bindingOwner.ts";
|
|
8
9
|
import { aggregateLiveStatus } from "#internal/aggregateLiveStatus.ts";
|
|
9
10
|
import { automaticQuery } from "#internal/automaticQuery.ts";
|
|
10
|
-
import type { BindingOwner } from "#internal/bindingOwner.ts";
|
|
11
11
|
import { queryDelivery } from "#internal/queryDelivery.ts";
|
|
12
12
|
import { localQueryKeys, queryHint } from "#internal/queryHints.ts";
|
|
13
|
+
import { queryProgress } from "#internal/querySignals.ts";
|
|
13
14
|
import { socketIdentity } from "#internal/rpcClient.ts";
|
|
14
15
|
import { type LiveStatus, live } from "#live.ts";
|
|
16
|
+
import { querySignals, shareQuerySignals } from "#querySignals.ts";
|
|
15
17
|
import type { QueryUpdate } from "#queryUpdate.ts";
|
|
16
18
|
import { type ResumeSource, type ResumeWindow, resumeSignal } from "#resume.ts";
|
|
17
19
|
import type { QueryReference } from "#serverMetadata.ts";
|
|
@@ -75,10 +77,11 @@ export function automaticLive(
|
|
|
75
77
|
headers?: Headers.Input,
|
|
76
78
|
) {
|
|
77
79
|
if (!enabled) {
|
|
78
|
-
return atom;
|
|
80
|
+
return queryProgress(atom);
|
|
79
81
|
}
|
|
80
82
|
const ready = Atom.make(false);
|
|
81
|
-
const
|
|
83
|
+
const signals = querySignals(atom);
|
|
84
|
+
const delivery = queryDelivery(atom, decode, paused, signals);
|
|
82
85
|
const source = decode === undefined ? delivery.atom.pipe(Atom.setIdleTTL(0)) : delivery.atom;
|
|
83
86
|
const subscription = live(service.runtime, {
|
|
84
87
|
initialResync: false,
|
|
@@ -100,10 +103,12 @@ export function automaticLive(
|
|
|
100
103
|
pushed: decode !== undefined,
|
|
101
104
|
ready,
|
|
102
105
|
signal,
|
|
106
|
+
signals,
|
|
103
107
|
source,
|
|
104
108
|
statuses,
|
|
105
109
|
subscription,
|
|
106
110
|
});
|
|
111
|
+
shareQuerySignals(gated, atom);
|
|
107
112
|
Object.assign(gated, { initialValueTarget: atom });
|
|
108
113
|
if (Atom.isSerializable(atom)) {
|
|
109
114
|
Object.assign(gated, { [Atom.SerializableTypeId]: atom[Atom.SerializableTypeId] });
|
package/src/bind.ts
CHANGED
|
@@ -7,10 +7,10 @@ import type * as AtomRpc from "effect/reactivity/AtomRpc";
|
|
|
7
7
|
import type * as Reactivity from "effect/reactivity/Reactivity";
|
|
8
8
|
import type { Rpc, RpcGroup } from "effect/rpc";
|
|
9
9
|
import type { RpcClientError } from "effect/rpc/RpcClientError";
|
|
10
|
+
import { bindingOwner } from "#adapters/bindingOwner.ts";
|
|
10
11
|
import { automaticLive, type LiveBound, LiveConnection } from "#automaticLive.ts";
|
|
11
12
|
import type { ClientOptions, MadeClient } from "#clientOptions.ts";
|
|
12
13
|
import type { Contract, Declared, Tag } from "#contract.ts";
|
|
13
|
-
import { bindingOwner } from "#internal/bindingOwner.ts";
|
|
14
14
|
import { clientOperations } from "#internal/clientOperations.ts";
|
|
15
15
|
import { type RpcSource, rpcClient } from "#internal/rpcClient.ts";
|
|
16
16
|
import type { CommandShape, QueryShape, StreamShape } from "#operation.ts";
|
|
@@ -18,13 +18,10 @@ import type { ResumeSource, ResumeWindow } from "#resume.ts";
|
|
|
18
18
|
|
|
19
19
|
export type Failure<R extends Rpc.Any> =
|
|
20
20
|
R extends Rpc.Rpc<infer _Tag, infer _Payload, infer _Success, infer _Error, infer TMiddleware, infer _Requires>
|
|
21
|
-
? Rpc.ErrorExit<R> | TMiddleware["error"]["Type"] | RpcClientError
|
|
21
|
+
? Rpc.ErrorExit<R> | TMiddleware["error"]["Type"] | TMiddleware["~ClientError"] | RpcClientError
|
|
22
22
|
: never;
|
|
23
23
|
|
|
24
|
-
export type RunFailure<R extends Rpc.Any> =
|
|
25
|
-
R extends Rpc.Rpc<infer _Tag, infer _Payload, infer _Success, infer _Error, infer TMiddleware, infer _Requires>
|
|
26
|
-
? Failure<R> | TMiddleware["~ClientError"]
|
|
27
|
-
: never;
|
|
24
|
+
export type RunFailure<R extends Rpc.Any> = Failure<R>;
|
|
28
25
|
|
|
29
26
|
export interface QueryOptions {
|
|
30
27
|
readonly headers?: Headers.Input;
|
|
@@ -32,8 +29,8 @@ export interface QueryOptions {
|
|
|
32
29
|
readonly timeToLive?: Duration.Input;
|
|
33
30
|
}
|
|
34
31
|
|
|
35
|
-
export interface BoundQuery<R extends Rpc.Any, TSelf> {
|
|
36
|
-
readonly query: (payload: Rpc.Payload<R>, options?: QueryOptions) => Atom.Atom<AsyncResult.AsyncResult<Rpc.Success<R>, Failure<R>>>;
|
|
32
|
+
export interface BoundQuery<R extends Rpc.Any, TSelf, TSubscription extends Rpc.Any = never> {
|
|
33
|
+
readonly query: (payload: Rpc.Payload<R>, options?: QueryOptions) => Atom.Atom<AsyncResult.AsyncResult<Rpc.Success<R>, Failure<R | TSubscription>>>;
|
|
37
34
|
readonly run: (payload: Rpc.Payload<R>) => Effect.Effect<Rpc.Success<R>, RunFailure<R>, TSelf>;
|
|
38
35
|
}
|
|
39
36
|
|
|
@@ -53,7 +50,11 @@ export type Bound<
|
|
|
53
50
|
TSelf,
|
|
54
51
|
TStreams extends readonly StreamShape[] = readonly [],
|
|
55
52
|
> = {
|
|
56
|
-
readonly [TQuery in TQueries[number] as TQuery["name"]]: BoundQuery<
|
|
53
|
+
readonly [TQuery in TQueries[number] as TQuery["name"]]: BoundQuery<
|
|
54
|
+
Rpc.ExtractTag<TRpcs, Tag<TName, TQuery["name"]>>,
|
|
55
|
+
TSelf,
|
|
56
|
+
Rpc.ExtractTag<TRpcs, Tag<TName, "$changes">>
|
|
57
|
+
>;
|
|
57
58
|
} & {
|
|
58
59
|
readonly [TCommand in TCommands[number] as TCommand["name"]]: BoundCommand<Rpc.ExtractTag<TRpcs, Tag<TName, TCommand["name"]>>, TSelf>;
|
|
59
60
|
} & {
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { Effect, Layer, Schema, type Scope, Stream } from "effect";
|
|
2
|
+
import { Rpc, RpcGroup, RpcSchema } from "effect/rpc";
|
|
3
|
+
import type { QueryUpdate } from "#queryUpdate.ts";
|
|
4
|
+
import type { QueryReference } from "#serverMetadata.ts";
|
|
5
|
+
|
|
6
|
+
type Changes<TRpcs extends Rpc.Any> = string extends TRpcs["_tag"] ? TRpcs : Extract<TRpcs, { readonly _tag: `${string}.$changes` }>;
|
|
7
|
+
type Definition<TRpc extends Rpc.Any> = TRpc extends Rpc.AnyWithProps ? TRpc : Rpc.AnyWithProps;
|
|
8
|
+
type Payload<TRpc extends Rpc.Any> = TRpc extends Rpc.AnyWithProps ? Rpc.Payload<TRpc> : { readonly queries: readonly QueryReference[] };
|
|
9
|
+
type Failure<TRpc extends Rpc.Any> = TRpc extends Rpc.AnyWithProps ? Rpc.ErrorExit<TRpc> : unknown;
|
|
10
|
+
type Handler<TRpc extends Rpc.Any, R> = (
|
|
11
|
+
payload: Payload<TRpc>,
|
|
12
|
+
options: Parameters<Rpc.ToHandlerFn<TRpc>>[1],
|
|
13
|
+
) => Stream.Stream<QueryUpdate, Failure<TRpc>, R>;
|
|
14
|
+
|
|
15
|
+
type RuntimeHandler = (
|
|
16
|
+
payload: { readonly queries: readonly QueryReference[] },
|
|
17
|
+
options: Parameters<Rpc.Handler<string>["handler"]>[1],
|
|
18
|
+
) => Stream.Stream<QueryUpdate, unknown, unknown>;
|
|
19
|
+
|
|
20
|
+
type Services<TRpcs extends Rpc.Any, R> = {
|
|
21
|
+
[TTag in TRpcs["_tag"]]: RpcGroup.HandlerServices<TRpcs, TTag, Handler<TRpcs, R>>;
|
|
22
|
+
}[TRpcs["_tag"]];
|
|
23
|
+
|
|
24
|
+
export function changesHandlerLayer<TRpcs extends Rpc.Any, R, TError = never, TServices = never>(
|
|
25
|
+
group: RpcGroup.RpcGroup<TRpcs>,
|
|
26
|
+
build: (rpc: Definition<Changes<TRpcs>>) => Handler<Changes<TRpcs>, R> | Effect.Effect<Handler<Changes<TRpcs>, R>, TError, TServices>,
|
|
27
|
+
): Layer.Layer<Rpc.ToHandler<Changes<TRpcs>>, TError, Exclude<TServices, Scope.Scope> | Services<Changes<TRpcs>, R>>;
|
|
28
|
+
export function changesHandlerLayer(
|
|
29
|
+
group: RpcGroup.RpcGroup<Rpc.Any>,
|
|
30
|
+
build: (rpc: Rpc.AnyWithProps) => RuntimeHandler | Effect.Effect<RuntimeHandler, unknown, unknown>,
|
|
31
|
+
) {
|
|
32
|
+
const layers: Layer.Layer<Rpc.Handler<string>, unknown, unknown>[] = [];
|
|
33
|
+
for (const rpc of group.requests.values()) {
|
|
34
|
+
if (!(Rpc.isRpc(rpc) && rpc._tag.endsWith(".$changes") && RpcSchema.isStreamSchema(rpc.successSchema))) {
|
|
35
|
+
continue;
|
|
36
|
+
}
|
|
37
|
+
const decode = Schema.decodeUnknownEffect(Schema.toType(rpc.successSchema.success));
|
|
38
|
+
const handler = build(rpc);
|
|
39
|
+
layers.push(
|
|
40
|
+
RpcGroup.make(rpc).toLayerHandler(
|
|
41
|
+
rpc._tag,
|
|
42
|
+
Effect.map(
|
|
43
|
+
Effect.isEffect(handler) ? handler : Effect.succeed(handler),
|
|
44
|
+
(subscribe) => (payload: { readonly queries: readonly QueryReference[] }, options: Parameters<Rpc.Handler<string>["handler"]>[1]) =>
|
|
45
|
+
subscribe(payload, options).pipe(Stream.mapEffect((update) => decode(update).pipe(Effect.orDie))),
|
|
46
|
+
),
|
|
47
|
+
),
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
return Layer.mergeAll(Layer.empty, ...layers);
|
|
51
|
+
}
|
package/src/clientActivity.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { Effect, Stream } from "effect";
|
|
2
2
|
|
|
3
3
|
export interface ClientActivityState {
|
|
4
4
|
readonly liveHints: number;
|
|
@@ -22,3 +22,10 @@ export function clientActivity(client: ActiveClient): ClientActivity {
|
|
|
22
22
|
export function isClientIdle(state: ClientActivityState): boolean {
|
|
23
23
|
return state.rpcCalls === 0 && state.liveInvalidations === 0 && state.refreshingAtoms === 0;
|
|
24
24
|
}
|
|
25
|
+
|
|
26
|
+
export function activityScreenSignal(activity: ClientActivity) {
|
|
27
|
+
function screenState(state: ClientActivityState) {
|
|
28
|
+
return { hints: state.liveHints, pending: !isClientIdle(state) };
|
|
29
|
+
}
|
|
30
|
+
return Object.assign(Stream.map(activity, screenState), { current: Effect.map(activity.current, screenState) });
|
|
31
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { invalidationKeys, type Key, readKeys } from "@shivaedev/changes/keys.ts";
|
|
2
|
+
|
|
3
|
+
export interface KeyContext {
|
|
4
|
+
readonly scope: string;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
function contextual(keys: readonly string[], context?: KeyContext): readonly string[] {
|
|
8
|
+
return context === undefined ? keys : keys.map((key) => `${JSON.stringify(["session", context.scope])}:${key}`);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function clientReadKeys(keys: readonly Key[], context?: KeyContext): readonly string[] {
|
|
12
|
+
return contextual(readKeys(keys), context);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function clientInvalidationKeys(keys: readonly Key[], context?: KeyContext): readonly string[] {
|
|
16
|
+
return contextual(invalidationKeys(keys), context);
|
|
17
|
+
}
|
package/src/clientOptions.ts
CHANGED
|
@@ -8,12 +8,15 @@ import type { LiveBound } from "#automaticLive.ts";
|
|
|
8
8
|
import type { Bound } from "#bind.ts";
|
|
9
9
|
import type { ActiveClient } from "#clientActivity.ts";
|
|
10
10
|
import type { CommandShape, QueryShape, StreamShape } from "#operation.ts";
|
|
11
|
+
import type { ResumeSource, ResumeWindow } from "#resume.ts";
|
|
11
12
|
|
|
12
13
|
export interface ClientOptions {
|
|
13
14
|
readonly bearerToken?: string;
|
|
14
15
|
readonly headers?: Readonly<Record<string, string>>;
|
|
15
16
|
readonly live?: boolean;
|
|
16
17
|
readonly protocol?: Layer.Layer<RpcClient.Protocol> | ((identity: string | undefined) => Layer.Layer<RpcClient.Protocol>);
|
|
18
|
+
readonly resume?: ResumeSource;
|
|
19
|
+
readonly resumeWindow?: ResumeWindow;
|
|
17
20
|
readonly url: string;
|
|
18
21
|
readonly webSocketConstructor?: Layer.Layer<Socket.WebSocketConstructor>;
|
|
19
22
|
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { type LiveBound, LiveConnection } from "#automaticLive.ts";
|
|
2
|
+
|
|
3
|
+
export const ComposedBindings = Symbol.for("@shivaedev/contract/ComposedBindings");
|
|
4
|
+
|
|
5
|
+
export interface BindingComposition {
|
|
6
|
+
readonly [ComposedBindings]: readonly LiveBound[];
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function composeBindings<TClient extends object>(client: TClient, bindings: readonly LiveBound[]): TClient & BindingComposition {
|
|
10
|
+
return { ...client, [ComposedBindings]: bindings };
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function isComposition(client: object): client is BindingComposition {
|
|
14
|
+
return ComposedBindings in client;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function liveBindings(client: unknown): readonly LiveBound[] {
|
|
18
|
+
if (typeof client !== "object" || client === null) {
|
|
19
|
+
return [];
|
|
20
|
+
}
|
|
21
|
+
if (isComposition(client)) {
|
|
22
|
+
return client[ComposedBindings];
|
|
23
|
+
}
|
|
24
|
+
return [client, ...Object.values(client)].filter(
|
|
25
|
+
(value): value is LiveBound => typeof value === "object" && value !== null && LiveConnection in value,
|
|
26
|
+
);
|
|
27
|
+
}
|
package/src/contract.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { Schema } from "effect";
|
|
2
2
|
import { Rpc, RpcGroup, type RpcMiddleware, type RpcSchema } from "effect/rpc";
|
|
3
|
-
import { declarationError } from "#internal/declarationError.ts";
|
|
3
|
+
import { declarationError, type Unique } from "#internal/declarationError.ts";
|
|
4
4
|
import type { CommandShape, OperationShape, QueryShape, StreamShape } from "#operation.ts";
|
|
5
|
+
import { ReadPolicyGuard } from "#ReadPolicyGuard.ts";
|
|
5
6
|
import { QueryReadDenied } from "#readPolicyErrors.ts";
|
|
6
7
|
import { changesRpc, KeyScope, operationAnnotations } from "#serverMetadata.ts";
|
|
7
8
|
|
|
@@ -21,7 +22,7 @@ export type OperationRpc<TContract extends string, TOperation> = TOperation exte
|
|
|
21
22
|
TOperation["payload"],
|
|
22
23
|
TOperation["success"],
|
|
23
24
|
OperationError<TOperation>,
|
|
24
|
-
never,
|
|
25
|
+
TOperation extends QueryShape ? typeof ReadPolicyGuard : never,
|
|
25
26
|
QueryServices<TOperation>
|
|
26
27
|
>
|
|
27
28
|
: never;
|
|
@@ -44,7 +45,9 @@ export interface Contract<
|
|
|
44
45
|
TName extends string,
|
|
45
46
|
TQueries extends readonly QueryShape[],
|
|
46
47
|
TCommands extends readonly CommandShape[],
|
|
47
|
-
TRpcs extends Rpc.Any =
|
|
48
|
+
TRpcs extends Rpc.Any =
|
|
49
|
+
| OperationRpc<TName, TQueries[number] | TCommands[number]>
|
|
50
|
+
| Rpc.AddMiddleware<ReturnType<typeof changesRpc<TName, TQueries>>, typeof ReadPolicyGuard>,
|
|
48
51
|
TStreams extends readonly StreamShape[] = readonly [],
|
|
49
52
|
TScope extends "session" | undefined = undefined,
|
|
50
53
|
> extends RpcGroup.RpcGroup<TRpcs> {
|
|
@@ -55,17 +58,6 @@ export interface Contract<
|
|
|
55
58
|
) => Contract<TName, TQueries, TCommands, Rpc.AddMiddleware<TRpcs, TM>, TStreams, TScope>;
|
|
56
59
|
}
|
|
57
60
|
|
|
58
|
-
type Duplicated<TOperations extends readonly OperationShape[], TSeen extends string = never> = TOperations extends readonly [
|
|
59
|
-
infer THead extends OperationShape,
|
|
60
|
-
...infer TTail extends readonly OperationShape[],
|
|
61
|
-
]
|
|
62
|
-
? (THead["name"] extends TSeen ? THead["name"] : never) | Duplicated<TTail, TSeen | THead["name"]>
|
|
63
|
-
: never;
|
|
64
|
-
|
|
65
|
-
type Unique<TOperations extends readonly OperationShape[]> = [Duplicated<TOperations>] extends [never]
|
|
66
|
-
? unknown
|
|
67
|
-
: `Operation names must be unique; duplicated: ${Duplicated<TOperations>}`;
|
|
68
|
-
|
|
69
61
|
function assertValid(name: string, operations: readonly (QueryShape | CommandShape | StreamShape)[]) {
|
|
70
62
|
const error = declarationError(name, operations);
|
|
71
63
|
if (error !== undefined) {
|
|
@@ -112,7 +104,8 @@ export function contract<
|
|
|
112
104
|
TName,
|
|
113
105
|
TQueries,
|
|
114
106
|
TCommands,
|
|
115
|
-
OperationRpc<TName, TQueries[number] | TCommands[number] | TStreams[number]>
|
|
107
|
+
| OperationRpc<TName, TQueries[number] | TCommands[number] | TStreams[number]>
|
|
108
|
+
| Rpc.AddMiddleware<ReturnType<typeof changesRpc<TName, TQueries>>, typeof ReadPolicyGuard>,
|
|
116
109
|
TStreams,
|
|
117
110
|
TScope
|
|
118
111
|
>;
|
|
@@ -127,16 +120,17 @@ export function contract(
|
|
|
127
120
|
): DeclaredGroup {
|
|
128
121
|
const { queries = [], commands = [], streams = [], keyScope } = operations;
|
|
129
122
|
assertValid(name, [...queries, ...commands, ...streams]);
|
|
130
|
-
const rpcs = [...queries, ...commands, ...streams].map((operation) =>
|
|
131
|
-
Rpc.make(`${name}.${operation.name}`, {
|
|
123
|
+
const rpcs = [...queries, ...commands, ...streams].map((operation) => {
|
|
124
|
+
const rpc = Rpc.make(`${name}.${operation.name}`, {
|
|
132
125
|
error: operation.kind === "query" ? Schema.Union([operation.error, QueryReadDenied]) : operation.error,
|
|
133
126
|
payload: operation.payload,
|
|
134
127
|
stream: operation.kind === "stream",
|
|
135
128
|
success: operation.success,
|
|
136
|
-
}).annotateMerge(operationAnnotations(operation, keyScope))
|
|
137
|
-
|
|
129
|
+
}).annotateMerge(operationAnnotations(operation, keyScope));
|
|
130
|
+
return operation.kind === "query" ? rpc.middleware(ReadPolicyGuard) : rpc;
|
|
131
|
+
});
|
|
138
132
|
const application = RpcGroup.make(...rpcs);
|
|
139
|
-
const changes = changesRpc(name, queries);
|
|
133
|
+
const changes = changesRpc(name, queries).middleware(ReadPolicyGuard);
|
|
140
134
|
return withDeclaration(application.add(keyScope === undefined ? changes : changes.annotate(KeyScope, keyScope)), application, {
|
|
141
135
|
commands,
|
|
142
136
|
keyScope,
|