@shivaedev/contract 0.55.0 → 0.57.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 +16 -15
- package/dist/AutomaticQueryOptions.d.ts +1 -1
- package/dist/AutomaticQueryOptions.d.ts.map +1 -1
- package/dist/{bind.d.ts → Bound.d.ts} +1 -19
- package/dist/Bound.d.ts.map +1 -0
- package/dist/Bound.js +2 -0
- package/dist/Bound.js.map +1 -0
- package/dist/ClientOptions.d.ts +2 -2
- package/dist/ClientOptions.d.ts.map +1 -1
- package/dist/automaticLive.d.ts +2 -1
- package/dist/automaticLive.d.ts.map +1 -1
- package/dist/automaticLive.js +4 -9
- package/dist/automaticLive.js.map +1 -1
- package/dist/automaticQuery.js +2 -2
- package/dist/automaticQuery.js.map +1 -1
- package/dist/clientActivities.d.ts +1 -15
- package/dist/clientActivities.d.ts.map +1 -1
- package/dist/clientActivities.js +0 -1
- package/dist/clientActivities.js.map +1 -1
- package/dist/clientOperations.d.ts +3 -2
- package/dist/clientOperations.d.ts.map +1 -1
- package/dist/clientOperations.js +3 -4
- package/dist/clientOperations.js.map +1 -1
- package/dist/makeClient.d.ts +1 -1
- package/dist/makeClient.d.ts.map +1 -1
- package/dist/makeClient.js +3 -3
- package/dist/makeClient.js.map +1 -1
- package/dist/queryDelivery.d.ts +1 -1
- package/dist/queryDelivery.d.ts.map +1 -1
- package/dist/queryDelivery.js.map +1 -1
- package/dist/reflectCommand.d.ts.map +1 -1
- package/dist/reflectCommand.js +7 -18
- package/dist/reflectCommand.js.map +1 -1
- package/dist/reflectedStream.d.ts +3 -3
- package/dist/reflectedStream.d.ts.map +1 -1
- package/dist/reflectedStream.js +4 -5
- package/dist/reflectedStream.js.map +1 -1
- package/dist/reflection.d.ts +4 -0
- package/dist/reflection.d.ts.map +1 -1
- package/dist/reflection.js +4 -0
- package/dist/reflection.js.map +1 -1
- package/dist/rpcClient.d.ts +29 -12
- package/dist/rpcClient.d.ts.map +1 -1
- package/dist/rpcClient.js +2 -3
- package/dist/rpcClient.js.map +1 -1
- package/dist/servedHandlers.d.ts.map +1 -1
- package/dist/servedHandlers.js +3 -2
- package/dist/servedHandlers.js.map +1 -1
- package/dist/socketAuthentication.d.ts +1 -11
- package/dist/socketAuthentication.d.ts.map +1 -1
- package/dist/socketAuthentication.js +15 -7
- package/dist/socketAuthentication.js.map +1 -1
- package/dist/socketProtocol.js +4 -8
- package/dist/socketProtocol.js.map +1 -1
- package/package.json +6 -6
- package/src/AutomaticQueryOptions.ts +1 -1
- package/src/{bind.ts → Bound.ts} +0 -46
- package/src/ClientOptions.ts +2 -2
- package/src/automaticLive.ts +9 -10
- package/src/automaticQuery.ts +2 -2
- package/src/clientActivities.ts +1 -2
- package/src/clientOperations.ts +5 -4
- package/src/makeClient.ts +4 -4
- package/src/queryDelivery.ts +2 -2
- package/src/reflectCommand.ts +7 -18
- package/src/reflectedStream.ts +5 -5
- package/src/reflection.ts +5 -0
- package/src/rpcClient.ts +2 -3
- package/src/servedHandlers.ts +5 -4
- package/src/socketAuthentication.ts +22 -7
- package/src/socketProtocol.ts +5 -10
- package/dist/bind.d.ts.map +0 -1
- package/dist/bind.js +0 -11
- package/dist/bind.js.map +0 -1
package/src/automaticLive.ts
CHANGED
|
@@ -9,7 +9,7 @@ import type { BindingOwner } from "#adapters/bindingOwner.ts";
|
|
|
9
9
|
import { changesClient } from "#adapters/erasedClient.ts";
|
|
10
10
|
import { aggregateLiveStatus } from "#aggregateLiveStatus.ts";
|
|
11
11
|
import { automaticQuery } from "#automaticQuery.ts";
|
|
12
|
-
import {
|
|
12
|
+
import type { ActivityTracker } from "#clientActivities.ts";
|
|
13
13
|
import { type LiveStatus, live } from "#live.ts";
|
|
14
14
|
import { queryDelivery } from "#queryDelivery.ts";
|
|
15
15
|
import { querySignals, shareQuerySignals } from "#querySignals.ts";
|
|
@@ -34,6 +34,7 @@ const pausedByRuntime = Atom.family((_runtime: object) => Atom.keepAlive(Atom.ma
|
|
|
34
34
|
|
|
35
35
|
export function automaticLive(
|
|
36
36
|
operations: AtomRpc.AtomRpcClient<unknown, string, Rpc.Any>,
|
|
37
|
+
activity: ActivityTracker,
|
|
37
38
|
name: string,
|
|
38
39
|
owner?: BindingOwner,
|
|
39
40
|
resume?: ResumeSource,
|
|
@@ -81,20 +82,18 @@ export function automaticLive(
|
|
|
81
82
|
resume: signal,
|
|
82
83
|
stream: Stream.unwrap(
|
|
83
84
|
service.use((client) =>
|
|
84
|
-
Effect.
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
return delivery.stream(
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
return delivery.stream(reflected.lifetime(opened), reflected.follower);
|
|
91
|
-
}),
|
|
85
|
+
Effect.flatMap(AtomRegistry.AtomRegistry, (registry) =>
|
|
86
|
+
Effect.map(followReflection(client, anchor(registry)), (reflected) => {
|
|
87
|
+
const opened = client(`${name}.$changes`, { queries: [reference] }, { headers: reflected.headers(headers) });
|
|
88
|
+
return delivery.stream(reflected.lifetime(opened), reflected.follower);
|
|
89
|
+
}),
|
|
90
|
+
),
|
|
92
91
|
),
|
|
93
92
|
),
|
|
94
93
|
});
|
|
95
94
|
const gated = automaticQuery({
|
|
96
95
|
anchor,
|
|
97
|
-
clientStatuses:
|
|
96
|
+
clientStatuses: activity.statuses,
|
|
98
97
|
equivalent,
|
|
99
98
|
ownerFailure,
|
|
100
99
|
paused,
|
package/src/automaticQuery.ts
CHANGED
|
@@ -51,7 +51,7 @@ export function automaticQuery<A, E>({
|
|
|
51
51
|
get.set(signals, recordSubscription(get.once(signals), value));
|
|
52
52
|
dispatcher.scheduleTask(() => {
|
|
53
53
|
if (active) {
|
|
54
|
-
for (const state of
|
|
54
|
+
for (const state of [statuses, clientStatuses]) {
|
|
55
55
|
registry.update(state, (current) => new Map([...current, [token, value]]));
|
|
56
56
|
}
|
|
57
57
|
}
|
|
@@ -63,7 +63,7 @@ export function automaticQuery<A, E>({
|
|
|
63
63
|
}
|
|
64
64
|
get.addFinalizer(() => {
|
|
65
65
|
active = false;
|
|
66
|
-
for (const state of
|
|
66
|
+
for (const state of [statuses, clientStatuses]) {
|
|
67
67
|
if (registry.getNodes().has(state)) {
|
|
68
68
|
registry.update(state, (current) => {
|
|
69
69
|
const next = new Map(current);
|
package/src/clientActivities.ts
CHANGED
|
@@ -6,8 +6,7 @@ import { clientConnection } from "#adapters/clientConnection.ts";
|
|
|
6
6
|
import { querySignals, shareQuerySignals } from "#querySignals.ts";
|
|
7
7
|
import { wrapAtom } from "#wrapAtom.ts";
|
|
8
8
|
|
|
9
|
-
type ActivityTracker = ReturnType<typeof makeActivity>;
|
|
10
|
-
export const clientActivities = new WeakMap<object, ActivityTracker>();
|
|
9
|
+
export type ActivityTracker = ReturnType<typeof makeActivity>;
|
|
11
10
|
|
|
12
11
|
export function makeActivity(factory: Atom.RuntimeFactory) {
|
|
13
12
|
const { activity, change, excuse, hold, rendered, setConnection } = activityState();
|
package/src/clientOperations.ts
CHANGED
|
@@ -7,8 +7,8 @@ import type { Rpc } from "effect/rpc";
|
|
|
7
7
|
import type { BindingOwner } from "#adapters/bindingOwner.ts";
|
|
8
8
|
import { streamingClient, unaryClient } from "#adapters/erasedClient.ts";
|
|
9
9
|
import type { automaticLive } from "#automaticLive.ts";
|
|
10
|
-
import type { QueryOptions, RunOptions } from "#
|
|
11
|
-
import {
|
|
10
|
+
import type { QueryOptions, RunOptions } from "#Bound.ts";
|
|
11
|
+
import type { ActivityTracker } from "#clientActivities.ts";
|
|
12
12
|
import { commandPayload } from "#commandPayload.ts";
|
|
13
13
|
import { COMMAND_REFLECTED } from "#commandReflected.ts";
|
|
14
14
|
import { CommandSignal, untilReflected } from "#commandSignal.ts";
|
|
@@ -28,6 +28,7 @@ function runSubject(options: unknown): string | undefined {
|
|
|
28
28
|
|
|
29
29
|
export function clientOperations(
|
|
30
30
|
service: AtomRpc.AtomRpcClient<unknown, string, Rpc.Any>,
|
|
31
|
+
activity: ActivityTracker,
|
|
31
32
|
automatic: ReturnType<typeof automaticLive>,
|
|
32
33
|
owner?: BindingOwner,
|
|
33
34
|
) {
|
|
@@ -42,7 +43,7 @@ export function clientOperations(
|
|
|
42
43
|
const reactivity = yield* Reactivity.Reactivity;
|
|
43
44
|
return yield* Effect.sync(
|
|
44
45
|
(): CommandObserver => ({
|
|
45
|
-
pending:
|
|
46
|
+
pending: activity.reflecting,
|
|
46
47
|
signal: (state) => {
|
|
47
48
|
signal(state);
|
|
48
49
|
if (state === "reflected") {
|
|
@@ -79,7 +80,7 @@ export function clientOperations(
|
|
|
79
80
|
function warm(get: Atom.AtomContext): boolean {
|
|
80
81
|
return get.once(warmQueries).has(tracked);
|
|
81
82
|
}
|
|
82
|
-
const watched =
|
|
83
|
+
const watched = activity.watch(result, warm);
|
|
83
84
|
const tracked = retainQuery(ordinaryClient.runtime, watched, stored(payload, inputSubject(headers)));
|
|
84
85
|
cached.set(atom, tracked);
|
|
85
86
|
return tracked;
|
package/src/makeClient.ts
CHANGED
|
@@ -6,7 +6,7 @@ import type { Rpc, RpcClient, RpcGroup } from "effect/rpc";
|
|
|
6
6
|
import type { RpcClientError } from "effect/rpc/RpcClientError";
|
|
7
7
|
import { bindingOwner } from "#adapters/bindingOwner.ts";
|
|
8
8
|
import { automaticLive, type LiveBound } from "#automaticLive.ts";
|
|
9
|
-
import type { Bound } from "#
|
|
9
|
+
import type { Bound } from "#Bound.ts";
|
|
10
10
|
import { bindOperations } from "#bindOperations.ts";
|
|
11
11
|
import type { ClientOptions } from "#ClientOptions.ts";
|
|
12
12
|
import type { ActiveClient } from "#clientActivity.ts";
|
|
@@ -64,12 +64,12 @@ export function makeClient(contracts: Contracts, options: ClientOptions = {}): L
|
|
|
64
64
|
annotations: Context.mergeAll(...list.map((contract) => contract.annotations)),
|
|
65
65
|
requests: new Map(list.flatMap((contract) => [...contract.requests])),
|
|
66
66
|
};
|
|
67
|
-
const connection = rpcClient("@shivaedev/contract/Contracts", source, options);
|
|
67
|
+
const { activity, connection } = rpcClient("@shivaedev/contract/Contracts", source, options);
|
|
68
68
|
const { registry, resume, resumeWindow } = options;
|
|
69
69
|
const bound = Object.entries(contracts).map(([key, { declaration }]) => {
|
|
70
70
|
const owner = bindingOwner(declaration.keyScope, registry);
|
|
71
|
-
const automatic = automaticLive(connection.Client, declaration.name, owner, resume, resumeWindow);
|
|
72
|
-
const operations = clientOperations(connection.Client, automatic, owner);
|
|
71
|
+
const automatic = automaticLive(connection.Client, activity, declaration.name, owner, resume, resumeWindow);
|
|
72
|
+
const operations = clientOperations(connection.Client, activity, automatic, owner);
|
|
73
73
|
return { api: [key, bindOperations(declaration, operations, automatic.subscription)] as const, owner, sync: operations.sync };
|
|
74
74
|
});
|
|
75
75
|
// A sync runs through the session's registry when the record holds a session-scoped contract, as that contract's own calls do.
|
package/src/queryDelivery.ts
CHANGED
|
@@ -19,7 +19,7 @@ export function queryDelivery<A, E>(decode: (value: unknown) => A, paused: Atom.
|
|
|
19
19
|
const pushed = Atom.make<AsyncResult.AsyncResult<A, E>>(AsyncResult.initial(true));
|
|
20
20
|
const deliver = Effect.fnUntraced(function* (
|
|
21
21
|
update: QueryUpdate,
|
|
22
|
-
follower: ReflectionFollower
|
|
22
|
+
follower: ReflectionFollower,
|
|
23
23
|
): Effect.fn.Return<SubscriptionEvent | undefined, never, AtomRegistry.AtomRegistry> {
|
|
24
24
|
const registry = yield* AtomRegistry.AtomRegistry;
|
|
25
25
|
const showing = !registry.get(paused);
|
|
@@ -47,7 +47,7 @@ export function queryDelivery<A, E>(decode: (value: unknown) => A, paused: Atom.
|
|
|
47
47
|
});
|
|
48
48
|
return {
|
|
49
49
|
atom: pushed,
|
|
50
|
-
stream: <TError, R>(stream: Stream.Stream<QueryUpdate, TError, R>, follower
|
|
50
|
+
stream: <TError, R>(stream: Stream.Stream<QueryUpdate, TError, R>, follower: ReflectionFollower) =>
|
|
51
51
|
stream.pipe(
|
|
52
52
|
Stream.mapEffect((update) => deliver(update, follower)),
|
|
53
53
|
Stream.filter((event) => event !== undefined),
|
package/src/reflectCommand.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Effect, Exit } from "effect";
|
|
2
2
|
import type { CommandState } from "#commandSignal.ts";
|
|
3
|
-
import {
|
|
3
|
+
import { reflectionScope } from "#reflection.ts";
|
|
4
4
|
import { commandSyncHeaders } from "#syncHeader.ts";
|
|
5
5
|
|
|
6
6
|
export interface CommandObserver {
|
|
@@ -14,34 +14,23 @@ export function reflectCommand<A, E, R>(
|
|
|
14
14
|
send: (options?: { readonly headers: Record<string, string> }) => Effect.Effect<A, E, R>,
|
|
15
15
|
standalone = false,
|
|
16
16
|
): Effect.Effect<A, E, R> {
|
|
17
|
-
return Effect.
|
|
18
|
-
const
|
|
19
|
-
const reflection = scope?.reflection;
|
|
20
|
-
let own: number | undefined;
|
|
21
|
-
if (scope !== undefined) {
|
|
22
|
-
own = standalone ? scope.reflection.flight() : scope.reflection.send(scope.piggyback);
|
|
23
|
-
}
|
|
17
|
+
return Effect.flatMap(reflectionScope(client), ({ piggyback, reflection }) => {
|
|
18
|
+
const own = standalone ? reflection.flight() : reflection.send(piggyback);
|
|
24
19
|
const finish = observer.pending();
|
|
25
20
|
observer.signal("sent");
|
|
26
|
-
const sent =
|
|
27
|
-
reflection === undefined || own === undefined ? send() : send({ headers: commandSyncHeaders({ client: reflection.client, id: own }) });
|
|
21
|
+
const sent = own === undefined ? send() : send({ headers: commandSyncHeaders({ client: reflection.client, id: own }) });
|
|
28
22
|
return Effect.onExit(sent, (exit) =>
|
|
29
23
|
Effect.sync(() => {
|
|
30
24
|
if (Exit.isFailure(exit)) {
|
|
31
|
-
reflection
|
|
25
|
+
reflection.failed(own);
|
|
32
26
|
finish();
|
|
33
27
|
return;
|
|
34
28
|
}
|
|
35
29
|
observer.signal("accepted");
|
|
36
|
-
|
|
30
|
+
reflection.accepted(own, () => {
|
|
37
31
|
finish();
|
|
38
32
|
observer.signal("reflected");
|
|
39
|
-
}
|
|
40
|
-
if (reflection === undefined) {
|
|
41
|
-
reflected();
|
|
42
|
-
} else {
|
|
43
|
-
reflection.accepted(own, reflected);
|
|
44
|
-
}
|
|
33
|
+
});
|
|
45
34
|
}),
|
|
46
35
|
);
|
|
47
36
|
});
|
package/src/reflectedStream.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Effect, Exit, Stream } from "effect";
|
|
2
2
|
import { Headers } from "effect/http";
|
|
3
3
|
import { retriesLive } from "#liveRetry.ts";
|
|
4
|
-
import { type FollowedStream, type Reflection,
|
|
4
|
+
import { type FollowedStream, type Reflection, reflectionScope } from "#reflection.ts";
|
|
5
5
|
import { streamSyncHeaders } from "#syncHeader.ts";
|
|
6
6
|
|
|
7
7
|
export interface StreamAnchor extends FollowedStream {
|
|
@@ -17,10 +17,10 @@ export interface ReflectionFollower {
|
|
|
17
17
|
|
|
18
18
|
// A mounted query keeps one place in its connection's waits across attempts; it leaves only when it unmounts or stops retrying.
|
|
19
19
|
export function followReflection(client: object, anchor: StreamAnchor) {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
20
|
+
return Effect.map(reflectionScope(client), ({ reflection }) => follow(reflection, anchor));
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function follow(reflection: Reflection, anchor: StreamAnchor) {
|
|
24
24
|
const follower: ReflectionFollower = {
|
|
25
25
|
armed: () => reflection.armed(anchor),
|
|
26
26
|
confirmed: (ids) => reflection.confirmed(anchor, ids),
|
package/src/reflection.ts
CHANGED
|
@@ -110,3 +110,8 @@ export const makeReflection = Effect.flatMap(Queue.sliding<void>(1), (catchUps)
|
|
|
110
110
|
export type Reflection = Effect.Success<typeof makeReflection>;
|
|
111
111
|
|
|
112
112
|
export const reflections = new WeakMap<object, { readonly piggyback: boolean; readonly reflection: Reflection }>();
|
|
113
|
+
|
|
114
|
+
export function reflectionScope(client: object) {
|
|
115
|
+
const scope = reflections.get(client);
|
|
116
|
+
return scope === undefined ? Effect.die(new Error("A client made by makeClient carries its connection's reflection")) : Effect.succeed(scope);
|
|
117
|
+
}
|
package/src/rpcClient.ts
CHANGED
|
@@ -5,7 +5,7 @@ import { type Rpc, RpcClient, RpcGroup } from "effect/rpc";
|
|
|
5
5
|
import type { RpcClientError } from "effect/rpc/RpcClientError";
|
|
6
6
|
import type { ClientOptions } from "#ClientOptions.ts";
|
|
7
7
|
import { catchUp } from "#catchUp.ts";
|
|
8
|
-
import {
|
|
8
|
+
import { makeActivity } from "#clientActivities.ts";
|
|
9
9
|
import { Activity } from "#clientActivity.ts";
|
|
10
10
|
import { ConnectionScope, connectionProtocol } from "#connectionProtocol.ts";
|
|
11
11
|
import { commandConnections } from "#connectionReadiness.ts";
|
|
@@ -86,14 +86,13 @@ export function rpcClient(name: string, source: RpcSource, options: ClientOption
|
|
|
86
86
|
protocol: typeof options.protocol === "object" ? protocol : (get) => protocolFor(get(socketIdentity)),
|
|
87
87
|
runtime: options.runtime,
|
|
88
88
|
});
|
|
89
|
-
clientActivities.set(Client, activity);
|
|
90
89
|
const directClient = withClientProtocol(protocol, make).pipe(
|
|
91
90
|
Effect.map((flat) =>
|
|
92
91
|
Object.fromEntries([...group.requests.keys()].map((tag) => [tag, (payload: never, callOptions: never) => flat(tag, payload, callOptions)])),
|
|
93
92
|
),
|
|
94
93
|
);
|
|
95
94
|
const layer = Layer.effect(Client, make).pipe(Layer.provide(protocol));
|
|
96
|
-
return Object.assign(layer, { [Activity]: activity.activity, Client, client: directClient, runtime: Client.runtime });
|
|
95
|
+
return { activity, connection: Object.assign(layer, { [Activity]: activity.activity, Client, client: directClient, runtime: Client.runtime }) };
|
|
97
96
|
}
|
|
98
97
|
|
|
99
98
|
function pageRpcUrl() {
|
package/src/servedHandlers.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { Effect, Layer, type Schema } from "effect";
|
|
1
|
+
import { Effect, Layer, type Option, type Schema } from "effect";
|
|
2
2
|
import { Rpc, RpcGroup, RpcSchema } from "effect/rpc";
|
|
3
3
|
import { ReadRecorder } from "@shivaedev/changes/readRecorder.ts";
|
|
4
4
|
import { rpcBoundary } from "@shivaedev/errors/boundary.ts";
|
|
5
5
|
import { InternalServerError } from "@shivaedev/errors/taxonomy.ts";
|
|
6
6
|
import type { Declared } from "#contract.ts";
|
|
7
7
|
import type { CommandShape, QueryShape, StreamShape } from "#operation.ts";
|
|
8
|
-
import { CurrentViewer, checkPolicy, type QueryReadPolicy } from "#readPolicy.ts";
|
|
8
|
+
import { CurrentViewer, checkPolicy, type QueryReadPolicy, type Viewer } from "#readPolicy.ts";
|
|
9
9
|
import { ServedReadPolicy } from "#servedReadPolicy.ts";
|
|
10
10
|
|
|
11
11
|
type Runnable = (payload: unknown) => Effect.Effect<unknown, unknown, unknown>;
|
|
@@ -36,10 +36,11 @@ function served(rpc: Rpc.AnyWithProps, operation: QueryShape | CommandShape, run
|
|
|
36
36
|
}).pipe(Effect.withSpan(span), boundary);
|
|
37
37
|
}
|
|
38
38
|
|
|
39
|
-
// Live subscriptions check the policy outside the handler, so the policy carries the handler's failure mapping.
|
|
39
|
+
// Live subscriptions check the policy outside the handler, so the policy carries the handler's failure mapping. It keeps the
|
|
40
|
+
// policy's own properties, such as the mark of a permission policy that collection reads require.
|
|
40
41
|
function servedPolicy(rpc: Rpc.AnyWithProps, policy: QueryReadPolicy<unknown, unknown, unknown>): QueryReadPolicy<unknown, unknown, unknown> {
|
|
41
42
|
const boundary = guarded(rpc);
|
|
42
|
-
return (payload, viewer) => boundary(Effect.suspend(() => policy(payload, viewer)));
|
|
43
|
+
return Object.assign((payload: unknown, viewer: Option.Option<Viewer>) => boundary(Effect.suspend(() => policy(payload, viewer))), policy);
|
|
43
44
|
}
|
|
44
45
|
|
|
45
46
|
type EffectRpc = Rpc.Rpc<string, Schema.Top, Schema.Top, Schema.Top>;
|
|
@@ -6,23 +6,38 @@ export const SocketAuthentication = Schema.Struct({
|
|
|
6
6
|
token: Schema.String.check(Schema.isPattern(/^[\x21-\x7e]+$/u)),
|
|
7
7
|
});
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
// Each connection reads the credential once, so a reconnect sends the one stored now and asks for a frame only when one follows.
|
|
10
|
+
export const authenticateSocket = Effect.fnUntraced(function* (
|
|
11
|
+
url: string,
|
|
12
|
+
credential: Effect.Effect<string | null>,
|
|
13
|
+
headers?: Readonly<Record<string, string>>,
|
|
14
|
+
) {
|
|
15
|
+
const framed = new URL(url);
|
|
16
|
+
framed.searchParams.set("auth", "frame");
|
|
17
|
+
let token: string | null = null;
|
|
18
|
+
const socket = yield* Socket.makeWebSocket(
|
|
19
|
+
Effect.map(credential, (current) => {
|
|
20
|
+
token = current;
|
|
21
|
+
return current === null ? url : framed.toString();
|
|
22
|
+
}),
|
|
23
|
+
{ headers },
|
|
24
|
+
);
|
|
11
25
|
const ready = yield* Latch.make(false);
|
|
12
26
|
const writer = yield* Effect.map(socket.writer, (nativeWriter) => ({
|
|
13
27
|
write: (chunk: Parameters<typeof nativeWriter.write>[0]) => ready.await.pipe(Effect.andThen(nativeWriter.write(chunk))),
|
|
14
28
|
writeAll: (chunks: Parameters<typeof nativeWriter.writeAll>[0]) => ready.await.pipe(Effect.andThen(nativeWriter.writeAll(chunks))),
|
|
15
29
|
}));
|
|
16
|
-
return {
|
|
17
|
-
...socket,
|
|
30
|
+
return Socket.make({
|
|
18
31
|
reader: Effect.gen(function* () {
|
|
19
32
|
yield* ready.close;
|
|
20
33
|
const reader = yield* socket.reader;
|
|
21
|
-
|
|
22
|
-
|
|
34
|
+
if (token !== null) {
|
|
35
|
+
const raw = yield* socket.writer;
|
|
36
|
+
yield* raw.write(JSON.stringify({ _tag: "Authenticate", token }));
|
|
37
|
+
}
|
|
23
38
|
yield* ready.open;
|
|
24
39
|
return { ...reader, pull: reader.pull.pipe(Effect.onError(() => ready.close)) };
|
|
25
40
|
}),
|
|
26
41
|
writer: Effect.succeed(writer),
|
|
27
|
-
};
|
|
42
|
+
});
|
|
28
43
|
});
|
package/src/socketProtocol.ts
CHANGED
|
@@ -35,16 +35,11 @@ export function socketProtocol(options: SocketOptions) {
|
|
|
35
35
|
|
|
36
36
|
const protocols = Atom.family(
|
|
37
37
|
(options: Omit<SocketOptions, "resumeWindow"> & { readonly resumeWindow: { readonly target: ResumeWindow } | undefined }) => {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
}
|
|
44
|
-
const raw = Socket.layerWebSocket(url, { headers: options.headers });
|
|
45
|
-
const authenticated = (
|
|
46
|
-
options.bearerToken === undefined ? raw : Layer.effect(Socket.Socket, authenticateSocket(options.bearerToken)).pipe(Layer.provide(raw))
|
|
47
|
-
).pipe(Layer.provide(options.webSocketConstructor ?? Socket.layerWebSocketConstructorGlobal));
|
|
38
|
+
const opened =
|
|
39
|
+
options.bearerToken === undefined
|
|
40
|
+
? Socket.layerWebSocket(options.url, { headers: options.headers })
|
|
41
|
+
: Layer.effect(Socket.Socket, authenticateSocket(options.url, options.bearerToken, options.headers));
|
|
42
|
+
const authenticated = opened.pipe(Layer.provide(options.webSocketConstructor ?? Socket.layerWebSocketConstructorGlobal));
|
|
48
43
|
const socket = Layer.effect(
|
|
49
44
|
Socket.Socket,
|
|
50
45
|
resumeSocket({
|
package/dist/bind.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"bind.d.ts","sourceRoot":"","sources":["../src/bind.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAC7C,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAC3C,OAAO,KAAK,KAAK,WAAW,MAAM,+BAA+B,CAAC;AAClE,OAAO,KAAK,KAAK,IAAI,MAAM,wBAAwB,CAAC;AACpD,OAAO,KAAK,KAAK,YAAY,MAAM,gCAAgC,CAAC;AACpE,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AAC,aAAa,CAAC,KAAK,EAAE,GAAG,EAAE,WAAW,CAAC,GAAG,cAAc,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,WAAW,CAAC,EACrG,GAAG,OAAO,EAAE,SAAS,SAAS,MAAM,GAAG,CAAC,OAAO,EAAE,WAAW,GAAG;IAAE,QAAQ,CAAC,QAAQ,EAAE,YAAY,CAAC,YAAY,CAAA;CAAE,CAAC,GAAG,CAAC,OAAO,CAAC,EAAE,WAAW,CAAC,GACxI,KAAK,CAAC,KAAK,EAAE,QAAQ,EAAE,SAAS,EAAE,KAAK,EAAE,KAAK,EAAE,QAAQ,CAAC,GAAG,SAAS,CAAC"}
|
package/dist/bind.js
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { bindingOwner } from "#adapters/bindingOwner.ts";
|
|
2
|
-
import { automaticLive } from "#automaticLive.ts";
|
|
3
|
-
import { bindOperations } from "#bindOperations.ts";
|
|
4
|
-
import { clientOperations } from "#clientOperations.ts";
|
|
5
|
-
export function bind(contract, service, options) {
|
|
6
|
-
const { name, keyScope } = contract.declaration;
|
|
7
|
-
const owner = bindingOwner(keyScope, options?.registry);
|
|
8
|
-
const automatic = automaticLive(service, name, owner, options?.resume, options?.resumeWindow);
|
|
9
|
-
return bindOperations(contract.declaration, clientOperations(service, automatic, owner), automatic.subscription);
|
|
10
|
-
}
|
|
11
|
-
//# sourceMappingURL=bind.js.map
|
package/dist/bind.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"bind.js","sourceRoot":"","sources":["../src/bind.ts"],"names":[],"mappings":"AASA,OAAO,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC;AACzD,OAAO,EAAE,aAAa,EAAkB,MAAM,mBAAmB,CAAC;AAClE,OAAO,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AACpD,OAAO,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AA2FxD,MAAM,UAAU,IAAI,CACnB,QAAmJ,EACnJ,OAAwD,EACxD,OAAqB;IAErB,MAAM,EAAE,IAAI,EAAE,QAAQ,EAAE,GAAG,QAAQ,CAAC,WAAW,CAAC;IAChD,MAAM,KAAK,GAAG,YAAY,CAAC,QAAQ,EAAE,OAAO,EAAE,QAAQ,CAAC,CAAC;IACxD,MAAM,SAAS,GAAG,aAAa,CAAC,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,YAAY,CAAC,CAAC;IAC9F,OAAO,cAAc,CAAC,QAAQ,CAAC,WAAW,EAAE,gBAAgB,CAAC,OAAO,EAAE,SAAS,EAAE,KAAK,CAAC,EAAE,SAAS,CAAC,YAAY,CAAC,CAAC;AAClH,CAAC"}
|