Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions apps/contract/src/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,8 @@ export type CliAction = typeof CliAction.Type;

/**
* A new server. A local one needs a home of its own, since a home names one server; a remote one
* needs its WebSocket URL. A blank or absent `name` is no override.
* needs its WebSocket URL and the token in `<home>/token` of its CodeWork home. A blank or absent
* `name` is no override.
*/
export const ServerAdd = Schema.Union([
Schema.Struct({
Expand All @@ -443,13 +444,18 @@ export const ServerAdd = Schema.Union([
home: Schema.String,
args: Schema.String,
}),
Schema.Struct({ kind: Schema.Literal("remote"), name: Schema.optionalKey(Schema.String), url: Schema.String }),
Schema.Struct({
kind: Schema.Literal("remote"),
name: Schema.optionalKey(Schema.String),
url: Schema.String,
token: Schema.String,
}),
]);
export type ServerAdd = typeof ServerAdd.Type;

/**
* Changes to a server; an absent field is left as it is. A `null` path goes back to the one found;
* a blank `name` removes the override; `env` replaces every variable. `url` is for a remote server only, the rest but `name` for a local one.
* a blank `name` removes the override; `env` replaces every variable. `url` and `token` are for a remote server only, the rest but `name` for a local one.
*/
export const ServerUpdate = Schema.Struct({
id: Schema.String,
Expand All @@ -459,6 +465,7 @@ export const ServerUpdate = Schema.Struct({
args: Schema.optionalKey(Schema.String),
env: Schema.optionalKey(Schema.Array(CliEnvVar)),
url: Schema.optionalKey(Schema.String),
token: Schema.optionalKey(Schema.String),
});
export type ServerUpdate = typeof ServerUpdate.Type;

Expand Down
2 changes: 1 addition & 1 deletion apps/desktop/src/ipc.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** Synchronous IPC: the renderer reads the app server's RPC URL once, at load. */
/** Synchronous IPC: the renderer reads the app server's RPC URL and token once, at load. */
export const SERVER_CHANNEL = "codework:server";
/** Synchronous IPC: the user's home directory, the folder a new project starts from. */
export const HOME_CHANNEL = "codework:home";
Expand Down
3 changes: 2 additions & 1 deletion apps/desktop/src/preload.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { exposeClerkBridge } from "@clerk/electron/preload";
import { contextBridge, ipcRenderer } from "electron";
import { HOME_CHANNEL, PICK_FOLDER_CHANNEL, REVEAL_CHANNEL, SERVER_CHANNEL } from "./ipc.ts";
import type { Endpoint } from "./server.ts";

contextBridge.exposeInMainWorld("desktopBridge", {
platform: process.platform,
// Main creates the window only after the server is ready, so this is set.
server: ipcRenderer.sendSync(SERVER_CHANNEL) as string,
server: ipcRenderer.sendSync(SERVER_CHANNEL) as Endpoint,
home: ipcRenderer.sendSync(HOME_CHANNEL) as string,
// On Windows the CodeWork server runs in WSL, which has no use for the Windows paths these deal in.
...(process.platform !== "win32" && {
Expand Down
19 changes: 13 additions & 6 deletions apps/desktop/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,14 @@ const ENTRY = app.isPackaged
// How long the server gets to stop by itself on quit before it is killed.
const STOP_GRACE_MS = 5000;

/** Starts the app server and resolves with its RPC URL once it listens. */
export function startServer(): Promise<string> {
/** Where the renderer reaches the app server, and the token its door takes. */
export interface Endpoint {
readonly url: string;
readonly token: string;
}

/** Starts the app server and resolves with its RPC endpoint once it listens. */
export function startServer(): Promise<Endpoint> {
// Electron's own binary in Node mode: no system Node needed, and the child
// does not become a second GUI app instance.
const child = spawn(process.execPath, [ENTRY], {
Expand Down Expand Up @@ -56,14 +62,15 @@ export function startServer(): Promise<string> {
});

createInterface({ input: child.stdout }).on("line", (line) => {
// The ready line carries the token, so it is the one line never echoed.
if (!ready && line.startsWith('{"type":"ready"')) {
ready = true;
const { port } = JSON.parse(line) as { port: number };
const url = `ws://127.0.0.1:${port}/rpc`;
const { port, token } = JSON.parse(line) as { port: number; token: string };
const endpoint: Endpoint = { url: `ws://127.0.0.1:${port}/rpc`, token };
ipcMain.on(SERVER_CHANNEL, (event) => {
event.returnValue = url;
event.returnValue = endpoint;
});
resolve(url);
resolve(endpoint);
return;
}
console.log(`[server] ${line}`);
Expand Down
1 change: 1 addition & 0 deletions apps/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
},
"dependencies": {
"@codeworksh/contract": "workspace:*",
"@codeworksh/schema": "workspace:*",
"@codeworksh/web": "workspace:*",
"@codeworksh/widget": "workspace:*",
"@effect/platform-node": "4.0.5",
Expand Down
10 changes: 6 additions & 4 deletions apps/server/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,7 @@ const executableOf = (row: Servers.LocalRow, foundOnce: typeof found) => {
export const liveAt = (home: string) =>
Effect.gen(function* () {
const registered = yield* Codework.registrationAt(home);
return Option.isSome(registered) && (yield* Codework.reachable(registered.value.url))
? registered
: Option.none();
return Option.isSome(registered) && (yield* Codework.reachable(registered.value)) ? registered : Option.none();
});

/**
Expand Down Expand Up @@ -160,7 +158,11 @@ export const statuses = Effect.gen(function* () {
yield* servers.list,
(row) =>
Effect.gen(function* () {
if (row.kind === "remote") return row satisfies ServerStatus;
// Its token stays here: the renderer is never sent one.
if (row.kind === "remote") {
const { token: _, ...status } = row;
return status satisfies ServerStatus;
}
const home = servers.homeOf(row);
const [cli, server] = yield* Effect.all(
[executableOf(row, foundOnce), serverAt(home.path, row.isDefault)],
Expand Down
100 changes: 62 additions & 38 deletions apps/server/src/codework.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,19 @@ import { Socket } from "effect/socket";
/* @effect-diagnostics nodeBuiltinImport:off -- a home's registration is a file path. */
import path from "node:path";
import { CodeworkUnavailable, HostError } from "@codeworksh/contract";
import { Door } from "@codeworksh/schema";
import { Servers } from "./servers.ts";

/** How a running server advertises itself: `<home>/service.json`, written by `codework serve`. */
const Registration = Schema.fromJsonString(
Schema.Struct({ pid: Schema.Int, version: Schema.String, url: Schema.String, port: Schema.Int }),
Schema.Struct({
pid: Schema.Int,
version: Schema.String,
url: Schema.String,
port: Schema.Int,
/** The home's token, offered as the socket opens. */
token: Schema.String,
}),
);

/** The server `<home>/service.json` names, read fresh; a missing or unreadable file is none. */
Expand All @@ -23,12 +31,34 @@ export const registrationAt = (home: string) =>
.pipe(Effect.flatMap(Schema.decodeUnknownEffect(Registration)), Effect.option);
});

/** Where a server is reached, and the token its door takes; a remote row without one is refused. */
interface Target {
readonly url: string;
readonly token: string | undefined;
}

/**
* Whether a server answers at `url`: its WebSocket opens. A registration's pid says nothing here, since
* the server may run on another OS than this app, as one in WSL does for the Windows app.
* A socket to the server `target` reads, read afresh on every open, so its URL and token always
* belong to the same server: a restarted one may have another port.
*/
export const reachable = (url: string) =>
Socket.makeWebSocket(url, { openTimeout: "2 seconds" }).pipe(
const open = <R>(target: Effect.Effect<Target, never, R>, options?: { readonly openTimeout?: Duration.Input }) =>
Socket.WebSocketConstructor.use((make) =>
Socket.fromWebSocket(
Effect.acquireRelease(
Effect.map(target, ({ url, token }) => make(url, Door.protocols(token))),
(ws) => Effect.sync(() => ws.close(1000)),
),
options,
),
);

/**
* Whether a server lets this app in at `target`: its WebSocket opens. A registration's pid says
* nothing here, since the server may run on another OS than this app, as one in WSL does for the
* Windows app.
*/
export const reachable = (target: Target) =>
open(Effect.succeed(target), { openTimeout: "2 seconds" }).pipe(
Effect.flatMap((socket) => Effect.scoped(socket.reader)),
Effect.isSuccess,
Effect.provide(NodeSocket.layerWebSocketConstructor),
Expand Down Expand Up @@ -66,60 +96,54 @@ const connect = <R>(socket: Layer.Layer<Socket.Socket, never, R>) =>
Layer.orDie,
);

type Found = { readonly url: string; readonly token: string };

/**
* The URL `registered` reads, waited for until there is one. The socket reads its URL while opening,
* What `registered` reads, waited for until there is one. The socket reads it while opening,
* which Effect runs uninterruptibly; waiting there for a server to register would otherwise keep the
* app server from ever shutting down.
*/
const waitFor = (registered: Effect.Effect<Option.Option<{ readonly url: string }>>) =>
const waitFor = (registered: Effect.Effect<Option.Option<Found>>) =>
registered.pipe(
Effect.flatMap(Effect.fromOption),
Effect.map((found) => found.url),
Effect.retry(Schedule.spaced("1 second")),
Effect.orDie,
Effect.interruptible,
);

/** A client of the local server `registered` names, read on every connect. */
const local = <R>(
registered: Effect.Effect<Option.Option<Found>, never, R>,
wrap: (socket: Socket.Socket) => Effect.Effect<Socket.Socket, never, R> = Effect.succeed,
) =>
Layer.unwrap(
Effect.gen(function* () {
const found = waitFor(Effect.provideContext(registered, yield* Effect.context<R>()));
return connect(Layer.effect(Socket.Socket, open(found).pipe(Effect.flatMap(wrap))));
}),
);

/**
* The app's connection to the default server. The registration is read on every connect, so a server
* restarted on a new port, a new home or a new default is picked up without restarting this app;
* until one is registered, connecting waits.
*/
export const layer = connect(
Layer.effect(
Socket.Socket,
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const servers = yield* Servers.Service;
const url = waitFor(
registration.pipe(
Effect.provideService(FileSystem.FileSystem, fs),
Effect.provideService(Servers.Service, servers),
),
);
return endOnHomeChange(yield* Socket.makeWebSocket(url), servers.homeChanges);
}),
),
export const layer = local(registration, (socket) =>
Servers.Service.use((servers) => Effect.succeed(endOnHomeChange(socket, servers.homeChanges))),
);

/**
* A connection to one machine's server: a remote one at its URL, a local one at what its home
* registers, read on every connect as {@link layer} does.
* A connection to one machine's server: a remote one at its URL with the token stored for it, a local
* one at what its home registers, read on every connect as {@link layer} does.
*/
export const machine = (row: Servers.Row) =>
connect(
Layer.effect(
Socket.Socket,
Effect.gen(function* () {
if (row.kind === "remote") return yield* Socket.makeWebSocket(row.url);
const fs = yield* FileSystem.FileSystem;
const home = (yield* Servers.Service).homeOf(row).path;
return yield* Socket.makeWebSocket(
waitFor(registrationAt(home).pipe(Effect.provideService(FileSystem.FileSystem, fs))),
);
}),
),
);
row.kind === "remote"
? connect(Layer.effect(Socket.Socket, open(Effect.succeed({ url: row.url, token: row.token ?? undefined }))))
: local(
Effect.gen(function* () {
return yield* registrationAt((yield* Servers.Service).homeOf(row).path);
}),
);

export interface CallOptions {
/** How long the server has to answer; 5 seconds unless given. */
Expand Down
6 changes: 6 additions & 0 deletions apps/server/src/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,12 @@ const migrations = {
// The one organization the UI uses until auth exists.
yield* sql`INSERT INTO organization ${sql.insert({ id: "personal", name: "Personal", createdAt: now, updatedAt: now })}`;
}),

// A remote machine's token, offered as its socket opens; a row from before has none and is refused.
"202611200001_token": Effect.gen(function* () {
const sql = yield* SqlClient.SqlClient;
yield* sql`ALTER TABLE machine ADD COLUMN token TEXT`;
}),
};

const setup = Effect.gen(function* () {
Expand Down
63 changes: 37 additions & 26 deletions apps/server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type { Socket } from "node:net";
import path from "node:path";
import { Codework } from "./codework.ts";
import { Contract } from "@codeworksh/contract";
import { Door } from "@codeworksh/schema";
import { Db } from "./db.ts";
import { Event } from "./event.ts";
import { Handlers } from "./handlers.ts";
Expand Down Expand Up @@ -36,15 +37,20 @@ const listener = () => {
return server;
};

// The port is chosen by the OS, so the supervisor learns it from this line.
const announceReady = Layer.effectDiscard(
Effect.gen(function* () {
const server = yield* HttpServer.HttpServer;
if (server.address._tag === "UnixPathAddress") return;
const port = server.address.port;
yield* Effect.sync(() => process.stdout.write(`${JSON.stringify({ type: "ready", port })}\n`));
}),
);
// The port is chosen by the OS and the token made at start, so the supervisor learns both from this
// line; it never logs it.
const announceReady = (token: string) =>
Layer.effectDiscard(
Effect.gen(function* () {
const server = yield* HttpServer.HttpServer;
if (server.address._tag === "UnixPathAddress") return;
const port = server.address.port;
yield* Effect.sync(() => process.stdout.write(`${JSON.stringify({ type: "ready", port, token })}\n`));
}),
);

/** The page's origins: the desktop app's scheme, packaged and from the repo. */
const ORIGINS = ["codework://app", "codeworkdev://app"];

const alive = (pid: number) =>
Effect.sync(() => {
Expand Down Expand Up @@ -95,25 +101,30 @@ const rpc = RpcServer.layerHttp({ group: Contract.Api, path: "/rpc", protocol: "
Layer.provide(Handlers.layer),
);

// One event log, kv and CodeWork connection for the RPC handlers and the widget backends alike.
export const layer = Layer.mergeAll(
HttpRouter.serve(rpc, { disableListenLog: true }),
Widgets.layer,
exitWithParent,
).pipe(
// The servers' status is checked from launch, so the first page already has it.
Layer.provide(
Layer.mergeAll(Health.layer, Sync.layer).pipe(
Layer.provideMerge(Layer.mergeAll(Event.layer, Kv.layer, Codework.layer)),
// One event log, kv and CodeWork connection for the RPC handlers and the widget backends alike. A
// fresh token per start: only the process that started this one learns it.
export const layer = Layer.unwrap(
Effect.map(Door.make, (token) =>
Layer.mergeAll(
HttpRouter.serve(Layer.merge(rpc, Door.layer({ token, origins: ORIGINS })), { disableListenLog: true }),
Widgets.layer,
exitWithParent,
).pipe(
// The servers' status is checked from launch, so the first page already has it.
Layer.provide(
Layer.mergeAll(Health.layer, Sync.layer).pipe(
Layer.provideMerge(Layer.mergeAll(Event.layer, Kv.layer, Codework.layer)),
),
),
// The app's servers decide which one its connection goes to, and the handlers manage them too.
Layer.provideMerge(Servers.layer),
Layer.provideMerge(announceReady(token)),
Layer.provideMerge(NodeHttpServer.layer(listener, { host: "127.0.0.1", port: 0 })),
Layer.provide(RpcSerialization.layerNdjson),
Layer.provide(database),
Layer.provide(NodeServices.layer),
),
),
// The app's servers decide which one its connection goes to, and the handlers manage them too.
Layer.provideMerge(Servers.layer),
Layer.provideMerge(announceReady),
Layer.provideMerge(NodeHttpServer.layer(listener, { host: "127.0.0.1", port: 0 })),
Layer.provide(RpcSerialization.layerNdjson),
Layer.provide(database),
Layer.provide(NodeServices.layer),
);

export * as Server from "./server.ts";
Loading