@shivaedev/contract 0.32.0 → 0.33.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 +6 -0
- package/README.md +4 -5
- package/package.json +9 -8
- package/dist/testing.d.ts +0 -17
- package/dist/testing.d.ts.map +0 -1
- package/dist/testing.js +0 -51
- package/dist/testing.js.map +0 -1
- package/src/testing.ts +0 -81
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.33.0 - 2026-11-03
|
|
4
|
+
|
|
5
|
+
### Breaking
|
|
6
|
+
|
|
7
|
+
- Move `watchHints`, `heard`, `layerMemoryProtocol` and `HintViewer` from `@shivaedev/contract/testing.ts` to `@shivaedev/test-kits/contract/testing.ts`. Add test-kits as a development dependency.
|
|
8
|
+
|
|
3
9
|
## 0.32.0 - 2026-11-03
|
|
4
10
|
|
|
5
11
|
### Added
|
package/README.md
CHANGED
|
@@ -159,7 +159,7 @@ export const NotesHandlers = Notes.toLayer(
|
|
|
159
159
|
|
|
160
160
|
Save this module as `handlers.ts`. An application can put repository and service calls in these handlers. The contract checks handler result and rejection types; it does not implement persistence, authorization or transactions. This in-memory server keeps the reserved change stream open without publishing hints. When a Platform host supplies that handler, build application handlers from `Notes.application` and keep `Notes` as the host and client group.
|
|
161
161
|
|
|
162
|
-
To map unexpected failures once for a handler registration,
|
|
162
|
+
To map unexpected failures once for a handler registration, use `rpcBoundaryHandlers(Notes.application, options, handlers)` from [`@shivaedev/errors`](https://github.com/ShivaeDev/platform/tree/main/packages/errors#mapping-unexpected-failures-at-the-handler-edge), then pass the map to `Notes.application.toLayer`. The helper derives each operation's rejection schema independently, and every operation declares the fallback returned by `options.unexpected`. Payloads, successful results and required services retain their native types.
|
|
163
163
|
|
|
164
164
|
Native `.middleware(M)` returns a contract that retains its declarations. For example, a native middleware service can supply request identity:
|
|
165
165
|
|
|
@@ -695,12 +695,12 @@ The generated subscription uses the owning query handler's captured services too
|
|
|
695
695
|
|
|
696
696
|
### In-memory clients in story kits
|
|
697
697
|
|
|
698
|
-
`layerMemoryProtocol(group)` from `@shivaedev/contract/testing.ts` connects the native RPC client and server in the same process. Provide the group's handlers and middleware with `Layer.provide`, then pass the protocol to the application's client factory:
|
|
698
|
+
`layerMemoryProtocol(group)` from `@shivaedev/test-kits/contract/testing.ts` connects the native RPC client and server in the same process. Provide the group's handlers and middleware with `Layer.provide`, then pass the protocol to the application's client factory:
|
|
699
699
|
|
|
700
700
|
```ts
|
|
701
701
|
import { Layer } from "effect";
|
|
702
702
|
import { makeClient } from "@shivaedev/contract/bind.ts";
|
|
703
|
-
import { layerMemoryProtocol } from "@shivaedev/contract/testing.ts";
|
|
703
|
+
import { layerMemoryProtocol } from "@shivaedev/test-kits/contract/testing.ts";
|
|
704
704
|
|
|
705
705
|
const client = makeClient(Notes, {
|
|
706
706
|
protocol: layerMemoryProtocol(Notes).pipe(Layer.provide(handlers)),
|
|
@@ -712,7 +712,7 @@ The URL identifies the client configuration; the in-memory protocol opens no soc
|
|
|
712
712
|
|
|
713
713
|
### Live hints in stories
|
|
714
714
|
|
|
715
|
-
Use `watchHints` and `heard` from `@shivaedev/contract/testing.ts` in an application's story kit. Pass each viewer's authenticated live stream to `watchHints`; it starts a scoped subscription and waits for its first hint before returning the viewer handle.
|
|
715
|
+
Use `watchHints` and `heard` from `@shivaedev/test-kits/contract/testing.ts` in an application's story kit. Pass each viewer's authenticated live stream to `watchHints`; it starts a scoped subscription and waits for its first hint before returning the viewer handle.
|
|
716
716
|
|
|
717
717
|
```ts
|
|
718
718
|
const alice = yield* watchHints(aliceChanges);
|
|
@@ -751,7 +751,6 @@ Import the module that defines the name; the package has no root entry.
|
|
|
751
751
|
| `resume.ts` | `resumeSignal({ window?, native? })`, returning a native numeric signal atom. |
|
|
752
752
|
| `querySignals.ts` | `querySignals(query)` exposes completion counts and subscription status as a native atom. |
|
|
753
753
|
| `clientActivity.ts` | `clientActivity(client)` exposes a native activity Stream with a `current` Effect; `isClientIdle(state)` checks finite work; `activityScreenSignal(activity)` projects pending work and hints for screen waits. |
|
|
754
|
-
| `testing.ts` | `layerMemoryProtocol(group)` supplies an in-memory native RPC protocol backed by the group’s handlers and middleware; `watchHints(stream)` starts a scoped viewer subscription; `heard(viewer, keys, { within? }?)` waits for matching changed keys. |
|
|
755
754
|
|
|
756
755
|
A query atom takes native query options as its optional second argument, for example `api.get.query({ id: 1 }, { timeToLive: "1 minute" })`. A native resume source receives a callback and returns a cleanup function.
|
|
757
756
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@shivaedev/contract",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.33.0",
|
|
4
4
|
"description": "Typed query and command contracts over native Effect RPC and reactivity",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"effect"
|
|
@@ -50,14 +50,15 @@
|
|
|
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.57.
|
|
56
|
-
"@shivaedev/server": "0.20.
|
|
57
|
-
"@shivaedev/sql": "0.
|
|
53
|
+
"@shivaedev/errors": "0.7.0",
|
|
54
|
+
"@shivaedev/identity": "0.21.0",
|
|
55
|
+
"@shivaedev/quality": "0.57.1",
|
|
56
|
+
"@shivaedev/server": "0.20.1",
|
|
57
|
+
"@shivaedev/sql": "0.35.0",
|
|
58
58
|
"@shivaedev/test": "0.14.0",
|
|
59
|
-
"@shivaedev/test-
|
|
60
|
-
"@shivaedev/test-
|
|
59
|
+
"@shivaedev/test-kits": "0.1.0",
|
|
60
|
+
"@shivaedev/test-sql": "0.18.0",
|
|
61
|
+
"@shivaedev/test-story": "0.36.0",
|
|
61
62
|
"@types/node": "24.19.2",
|
|
62
63
|
"effect": "4.0.6",
|
|
63
64
|
"esbuild": "0.28.2",
|
package/dist/testing.d.ts
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import { type Duration, Effect, Layer, Stream, SubscriptionRef } from "effect";
|
|
2
|
-
import { type Rpc, RpcClient, type RpcGroup, RpcSerialization, RpcServer } from "effect/rpc";
|
|
3
|
-
import type { Key } from "@shivaedev/changes/keys.ts";
|
|
4
|
-
import type { SubscriptionHint } from "#queryUpdate.ts";
|
|
5
|
-
export interface HintViewer<E> {
|
|
6
|
-
readonly finished: Effect.Effect<void, E>;
|
|
7
|
-
readonly hints: SubscriptionRef.SubscriptionRef<readonly SubscriptionHint[]>;
|
|
8
|
-
}
|
|
9
|
-
export declare const watchHints: <E, R>(stream: Stream.Stream<SubscriptionHint, E, R>) => Effect.Effect<{
|
|
10
|
-
finished: Effect.Effect<void, E, never>;
|
|
11
|
-
hints: SubscriptionRef.SubscriptionRef<readonly SubscriptionHint[]>;
|
|
12
|
-
}, E, R | import("effect/Scope").Scope>;
|
|
13
|
-
export declare const heard: <E>(viewer: HintViewer<E>, keys: readonly Key[], options?: {
|
|
14
|
-
readonly within?: Duration.Input;
|
|
15
|
-
} | undefined) => Effect.Effect<boolean, E, never>;
|
|
16
|
-
export declare function layerMemoryProtocol<TRpcs extends Rpc.Any>(group: RpcGroup.RpcGroup<TRpcs>): Layer.Layer<RpcClient.Protocol, never, Exclude<Exclude<Exclude<Rpc.ServicesServer<TRpcs>, RpcServer.Protocol>, import("effect/Scope").Scope>, RpcSerialization.RpcSerialization> | Exclude<Exclude<Exclude<Rpc.ToHandler<TRpcs>, RpcServer.Protocol>, import("effect/Scope").Scope>, RpcSerialization.RpcSerialization>>;
|
|
17
|
-
//# sourceMappingURL=testing.d.ts.map
|
package/dist/testing.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
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
DELETED
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
import { Deferred, Effect, Fiber, Layer, Queue, Stream, SubscriptionRef } from "effect";
|
|
2
|
-
import { RpcClient, RpcSerialization, RpcServer } from "effect/rpc";
|
|
3
|
-
export const watchHints = Effect.fn("Live.watchHints")(function* (stream) {
|
|
4
|
-
const hints = yield* SubscriptionRef.make([]);
|
|
5
|
-
const ready = yield* Deferred.make();
|
|
6
|
-
const fiber = yield* stream.pipe(Stream.runForEach((hint) => SubscriptionRef.update(hints, (received) => [...received, hint]).pipe(Effect.andThen(Deferred.succeed(ready, undefined)))), Effect.forkScoped);
|
|
7
|
-
const finished = Fiber.join(fiber);
|
|
8
|
-
yield* Effect.raceFirst(Deferred.await(ready), finished);
|
|
9
|
-
return { finished, hints };
|
|
10
|
-
});
|
|
11
|
-
export const heard = Effect.fn("Live.heard")(function* (viewer, keys, options = {}) {
|
|
12
|
-
const matching = SubscriptionRef.changes(viewer.hints).pipe(Stream.filter((hints) => hints.some((hint) => namesKeys(hint, keys))), Stream.runHead, Effect.as(true));
|
|
13
|
-
return yield* Effect.raceFirst(matching, Effect.as(viewer.finished, false)).pipe(Effect.timeoutOrElse({ duration: options.within ?? "1 second", orElse: () => Effect.succeed(false) }));
|
|
14
|
-
});
|
|
15
|
-
function namesKeys(hint, keys) {
|
|
16
|
-
return hint._tag === "Changed" && keys.every((key) => hint.keys.some((actual) => JSON.stringify(actual) === JSON.stringify(key)));
|
|
17
|
-
}
|
|
18
|
-
export function layerMemoryProtocol(group) {
|
|
19
|
-
return Layer.effect(RpcClient.Protocol, Effect.gen(function* () {
|
|
20
|
-
const requests = yield* Queue.unbounded();
|
|
21
|
-
const disconnects = yield* Queue.unbounded();
|
|
22
|
-
const { codecFor } = yield* RpcSerialization.RpcSerialization;
|
|
23
|
-
const protocol = yield* RpcClient.Protocol.make((send, clientIds) => Effect.gen(function* () {
|
|
24
|
-
const server = RpcServer.Protocol.of({
|
|
25
|
-
clientIds: Effect.sync(() => clientIds),
|
|
26
|
-
codecFor,
|
|
27
|
-
disconnects,
|
|
28
|
-
end: () => Effect.void,
|
|
29
|
-
initialMessage: Effect.succeedNone,
|
|
30
|
-
run: (receive) => Effect.forever(Effect.flatMap(Queue.take(requests), ([id, message]) => receive(id, message))),
|
|
31
|
-
send,
|
|
32
|
-
supportsAck: true,
|
|
33
|
-
supportsNotifications: true,
|
|
34
|
-
supportsSpanPropagation: true,
|
|
35
|
-
supportsTransferables: false,
|
|
36
|
-
});
|
|
37
|
-
yield* RpcServer.make(group).pipe(Effect.provideService(RpcServer.Protocol, server), Effect.forkScoped);
|
|
38
|
-
return {
|
|
39
|
-
codecFor,
|
|
40
|
-
send: (clientId, request) => Queue.offer(requests, [clientId, request]).pipe(Effect.asVoid),
|
|
41
|
-
supportsAck: true,
|
|
42
|
-
supportsTransferables: false,
|
|
43
|
-
};
|
|
44
|
-
}));
|
|
45
|
-
return {
|
|
46
|
-
...protocol,
|
|
47
|
-
run: (clientId, receive) => protocol.run(clientId, receive).pipe(Effect.ensuring(Queue.offer(disconnects, clientId))),
|
|
48
|
-
};
|
|
49
|
-
})).pipe(Layer.provide(RpcSerialization.layerNdjson));
|
|
50
|
-
}
|
|
51
|
-
//# sourceMappingURL=testing.js.map
|
package/dist/testing.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
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,MAA6C;IACpH,MAAM,KAAK,GAAG,KAAK,CAAC,CAAC,eAAe,CAAC,IAAI,CAA8B,EAAE,CAAC,CAAC;IAC3E,MAAM,KAAK,GAAG,KAAK,CAAC,CAAC,QAAQ,CAAC,IAAI,EAAQ,CAAC;IAC3C,MAAM,KAAK,GAAG,KAAK,CAAC,CAAC,MAAM,CAAC,IAAI,CAC/B,MAAM,CAAC,UAAU,CAAC,CAAC,IAAI,EAAE,EAAE,CAC1B,eAAe,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAC,GAAG,QAAQ,EAAE,IAAI,CAAC,CAAC,CAAC,IAAI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RpcMessage, RpcSerialization, RpcServer } from "effect/rpc";
|
|
3
|
-
import type { Key } from "@shivaedev/changes/keys.ts";
|
|
4
|
-
import type { SubscriptionHint } from "#queryUpdate.ts";
|
|
5
|
-
|
|
6
|
-
export interface HintViewer<E> {
|
|
7
|
-
readonly finished: Effect.Effect<void, E>;
|
|
8
|
-
readonly hints: SubscriptionRef.SubscriptionRef<readonly SubscriptionHint[]>;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export const watchHints = Effect.fn("Live.watchHints")(function* <E, R>(stream: Stream.Stream<SubscriptionHint, E, R>) {
|
|
12
|
-
const hints = yield* SubscriptionRef.make<readonly SubscriptionHint[]>([]);
|
|
13
|
-
const ready = yield* Deferred.make<void>();
|
|
14
|
-
const fiber = yield* stream.pipe(
|
|
15
|
-
Stream.runForEach((hint) =>
|
|
16
|
-
SubscriptionRef.update(hints, (received) => [...received, hint]).pipe(Effect.andThen(Deferred.succeed(ready, undefined))),
|
|
17
|
-
),
|
|
18
|
-
Effect.forkScoped,
|
|
19
|
-
);
|
|
20
|
-
const finished = Fiber.join(fiber);
|
|
21
|
-
yield* Effect.raceFirst(Deferred.await(ready), finished);
|
|
22
|
-
return { finished, hints } satisfies HintViewer<E>;
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
export const heard = Effect.fn("Live.heard")(function* <E>(
|
|
26
|
-
viewer: HintViewer<E>,
|
|
27
|
-
keys: readonly Key[],
|
|
28
|
-
options: { readonly within?: Duration.Input } = {},
|
|
29
|
-
) {
|
|
30
|
-
const matching = SubscriptionRef.changes(viewer.hints).pipe(
|
|
31
|
-
Stream.filter((hints) => hints.some((hint) => namesKeys(hint, keys))),
|
|
32
|
-
Stream.runHead,
|
|
33
|
-
Effect.as(true),
|
|
34
|
-
);
|
|
35
|
-
return yield* Effect.raceFirst(matching, Effect.as(viewer.finished, false)).pipe(
|
|
36
|
-
Effect.timeoutOrElse({ duration: options.within ?? "1 second", orElse: () => Effect.succeed(false) }),
|
|
37
|
-
);
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
function namesKeys(hint: SubscriptionHint, keys: readonly Key[]): boolean {
|
|
41
|
-
return hint._tag === "Changed" && keys.every((key) => hint.keys.some((actual) => JSON.stringify(actual) === JSON.stringify(key)));
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
export function layerMemoryProtocol<TRpcs extends Rpc.Any>(group: RpcGroup.RpcGroup<TRpcs>) {
|
|
45
|
-
return Layer.effect(
|
|
46
|
-
RpcClient.Protocol,
|
|
47
|
-
Effect.gen(function* () {
|
|
48
|
-
const requests = yield* Queue.unbounded<readonly [number, RpcMessage.FromClientEncoded]>();
|
|
49
|
-
const disconnects = yield* Queue.unbounded<number>();
|
|
50
|
-
const { codecFor } = yield* RpcSerialization.RpcSerialization;
|
|
51
|
-
const protocol = yield* RpcClient.Protocol.make((send, clientIds) =>
|
|
52
|
-
Effect.gen(function* () {
|
|
53
|
-
const server = RpcServer.Protocol.of({
|
|
54
|
-
clientIds: Effect.sync(() => clientIds),
|
|
55
|
-
codecFor,
|
|
56
|
-
disconnects,
|
|
57
|
-
end: () => Effect.void,
|
|
58
|
-
initialMessage: Effect.succeedNone,
|
|
59
|
-
run: (receive) => Effect.forever(Effect.flatMap(Queue.take(requests), ([id, message]) => receive(id, message))),
|
|
60
|
-
send,
|
|
61
|
-
supportsAck: true,
|
|
62
|
-
supportsNotifications: true,
|
|
63
|
-
supportsSpanPropagation: true,
|
|
64
|
-
supportsTransferables: false,
|
|
65
|
-
});
|
|
66
|
-
yield* RpcServer.make(group).pipe(Effect.provideService(RpcServer.Protocol, server), Effect.forkScoped);
|
|
67
|
-
return {
|
|
68
|
-
codecFor,
|
|
69
|
-
send: (clientId, request) => Queue.offer(requests, [clientId, request]).pipe(Effect.asVoid),
|
|
70
|
-
supportsAck: true,
|
|
71
|
-
supportsTransferables: false,
|
|
72
|
-
};
|
|
73
|
-
}),
|
|
74
|
-
);
|
|
75
|
-
return {
|
|
76
|
-
...protocol,
|
|
77
|
-
run: (clientId, receive) => protocol.run(clientId, receive).pipe(Effect.ensuring(Queue.offer(disconnects, clientId))),
|
|
78
|
-
} satisfies RpcClient.Protocol["Service"];
|
|
79
|
-
}),
|
|
80
|
-
).pipe(Layer.provide(RpcSerialization.layerNdjson));
|
|
81
|
-
}
|