t3code-cli 0.5.0 → 0.5.1

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/src/rpc/layer.ts CHANGED
@@ -6,14 +6,13 @@ import * as Option from "effect/Option";
6
6
  import * as Schedule from "effect/Schedule";
7
7
  import * as Scope from "effect/Scope";
8
8
  import * as SynchronizedRef from "effect/SynchronizedRef";
9
- import * as NodeSocket from "@effect/platform-node/NodeSocket";
10
9
  import { HttpClientRequest } from "effect/unstable/http";
11
10
  import { RpcClient, RpcSerialization } from "effect/unstable/rpc";
12
11
  import * as Socket from "effect/unstable/socket/Socket";
13
12
 
14
- import { T3Auth } from "../auth/service.ts";
15
- import { T3Config } from "../config/service.ts";
16
- import { toWebSocketEndpointUrl } from "../config/url.ts";
13
+ import { T3AuthTransport } from "../auth/transport.ts";
14
+ import { T3CodeConnectionProvider } from "../connection/service.ts";
15
+ import { normalizeHttpBaseUrl, toWebSocketEndpointUrl } from "../config/url.ts";
17
16
  import { CliWsRpcGroup } from "./ws-group.ts";
18
17
  import { RpcError } from "./error.ts";
19
18
  import { T3Rpc, type WsClient } from "./service.ts";
@@ -27,15 +26,19 @@ type Connection = {
27
26
  };
28
27
 
29
28
  export const makeT3RpcLayer = Effect.fn("makeT3RpcLayer")(function* () {
30
- const config = yield* T3Config;
31
- const auth = yield* T3Auth;
29
+ const connectionProvider = yield* T3CodeConnectionProvider;
30
+ const authTransport = yield* T3AuthTransport;
31
+ const webSocketConstructor = yield* Socket.WebSocketConstructor;
32
32
  const parentScope = yield* Scope.Scope;
33
33
  const connection = yield* SynchronizedRef.make(Option.none<Connection>());
34
34
  const openConnection = Effect.fn("T3RpcLive.openConnection")(function* () {
35
35
  const scope = yield* Scope.fork(parentScope);
36
36
  return yield* Effect.gen(function* () {
37
- const url = yield* makeWsUrl({ auth, config });
38
- const protocol = yield* Layer.buildWithScope(makeProtocolLayer(url), scope);
37
+ const url = yield* makeWsUrl({ authTransport, connectionProvider });
38
+ const protocol = yield* Layer.buildWithScope(
39
+ makeProtocolLayer(url, webSocketConstructor),
40
+ scope,
41
+ );
39
42
  const client = yield* makeClient.pipe(
40
43
  Effect.provide(protocol),
41
44
  Effect.provideService(Scope.Scope, scope),
@@ -72,21 +75,28 @@ export const makeT3RpcLayer = Effect.fn("makeT3RpcLayer")(function* () {
72
75
  });
73
76
 
74
77
  const makeWsUrl = Effect.fn("makeWsUrl")(function* (input: {
75
- readonly config: T3Config["Service"];
76
- readonly auth: T3Auth["Service"];
78
+ readonly authTransport: T3AuthTransport["Service"];
79
+ readonly connectionProvider: T3CodeConnectionProvider["Service"];
77
80
  }) {
78
- const resolved = yield* input.config.resolve();
79
- const wsTicket = yield* input.auth.issueWebSocketTicket();
80
- const wsUrl = yield* toWebSocketEndpointUrl(resolved.url, "/ws");
81
+ const connection = yield* input.connectionProvider.get;
82
+ const origin = yield* normalizeHttpBaseUrl(connection.origin.url);
83
+ const wsTicket = yield* input.authTransport.issueWebSocketTicket({
84
+ url: origin,
85
+ token: connection.auth.token,
86
+ });
87
+ const wsUrl = yield* toWebSocketEndpointUrl(origin, "/ws");
81
88
  const request = HttpClientRequest.get(wsUrl).pipe(
82
89
  HttpClientRequest.setUrlParam("wsTicket", wsTicket.ticket),
83
90
  );
84
91
  return Option.getOrThrow(HttpClientRequest.toUrl(request)).toString();
85
92
  });
86
93
 
87
- function makeProtocolLayer(url: string) {
94
+ function makeProtocolLayer(
95
+ url: string,
96
+ webSocketConstructor: Socket.WebSocketConstructor["Service"],
97
+ ) {
88
98
  const socketLayer = Socket.layerWebSocket(url).pipe(
89
- Layer.provide(NodeSocket.layerWebSocketConstructor),
99
+ Layer.provide(Layer.succeed(Socket.WebSocketConstructor, webSocketConstructor)),
90
100
  );
91
101
  return RpcClient.layerProtocolSocket({ retryTransientErrors: true }).pipe(
92
102
  Layer.provide(socketLayer),
package/src/runtime.ts CHANGED
@@ -1,12 +1,18 @@
1
+ import * as Effect from "effect/Effect";
1
2
  import * as Layer from "effect/Layer";
2
3
  import * as NodeHttpClient from "@effect/platform-node/NodeHttpClient";
3
4
  import * as NodeSocket from "@effect/platform-node/NodeSocket";
4
5
 
5
6
  import { T3AuthLive } from "./auth/layer.ts";
6
7
  import { T3LocalAuthLive } from "./auth/local.ts";
8
+ import { T3LocalAuthOriginLive } from "./auth/local-origin.ts";
9
+ import { T3LocalAuthTokenLive } from "./auth/local-token.ts";
7
10
  import { T3AuthPairingLive } from "./auth/pairing.ts";
8
11
  import { T3AuthTransportLive } from "./auth/transport.ts";
12
+ import { T3CodeConnectionError } from "./connection/error.ts";
13
+ import { T3CodeConnectionProvider, makeT3CodeConnectionProvider } from "./connection/service.ts";
9
14
  import { T3ConfigLive } from "./config/layer.ts";
15
+ import { T3Config } from "./config/service.ts";
10
16
  import { T3ApplicationLive } from "./application/layer.ts";
11
17
  import { T3OrchestrationLive } from "./orchestration/layer.ts";
12
18
  import { T3RpcLive } from "./rpc/layer.ts";
@@ -15,15 +21,49 @@ import { NodeSqlClientFactoryLive } from "./sql/node-sqlite-client.ts";
15
21
  export const T3AuthTransportLayer = T3AuthTransportLive.pipe(
16
22
  Layer.provide(NodeHttpClient.layerUndici),
17
23
  );
18
- export const T3LocalAuthLayer = T3LocalAuthLive.pipe(Layer.provide(NodeSqlClientFactoryLive));
24
+ export const T3LocalAuthOriginLayer = T3LocalAuthOriginLive;
25
+ export const T3LocalAuthTokenLayer = T3LocalAuthTokenLive.pipe(
26
+ Layer.provide(NodeSqlClientFactoryLive),
27
+ );
28
+ export const T3LocalAuthLayer = T3LocalAuthLive.pipe(
29
+ Layer.provide(Layer.mergeAll(T3LocalAuthOriginLayer, T3LocalAuthTokenLayer)),
30
+ );
19
31
  export const T3AuthPairingLayer = T3AuthPairingLive.pipe(Layer.provide(T3AuthTransportLayer));
20
32
  export const T3AuthLayer = T3AuthLive.pipe(
21
33
  Layer.provide(
22
34
  Layer.mergeAll(T3ConfigLive, T3AuthTransportLayer, T3LocalAuthLayer, T3AuthPairingLayer),
23
35
  ),
24
36
  );
37
+ const T3ConfigConnectionProviderLayer = Layer.effect(
38
+ T3CodeConnectionProvider,
39
+ Effect.gen(function* () {
40
+ const config = yield* T3Config;
41
+ return makeT3CodeConnectionProvider(
42
+ config.resolve().pipe(
43
+ Effect.map((resolved) => ({
44
+ origin: { url: resolved.url },
45
+ auth: { token: resolved.token },
46
+ })),
47
+ Effect.mapError(
48
+ (error) =>
49
+ new T3CodeConnectionError({
50
+ message: "failed to resolve T3 Code connection from CLI config",
51
+ cause: error,
52
+ }),
53
+ ),
54
+ ),
55
+ );
56
+ }),
57
+ ).pipe(Layer.provide(T3ConfigLive));
58
+
25
59
  const T3RpcLayer = T3RpcLive.pipe(
26
- Layer.provide(Layer.mergeAll(T3ConfigLive, T3AuthLayer, NodeSocket.layerWebSocketConstructor)),
60
+ Layer.provide(
61
+ Layer.mergeAll(
62
+ T3ConfigConnectionProviderLayer,
63
+ T3AuthTransportLayer,
64
+ NodeSocket.layerWebSocketConstructor,
65
+ ),
66
+ ),
27
67
  );
28
68
  const T3OrchestrationLayer = T3OrchestrationLive.pipe(Layer.provide(T3RpcLayer));
29
69
  const T3ApplicationLayer = T3ApplicationLive.pipe(Layer.provide(T3OrchestrationLayer));