@codeworksh/cli 0.9.0-canary.165.2.g336229f9 → 0.9.0-canary.166.1.gd8489101
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/{acp-wxLiqsce.mjs → acp-NXEQGwmj.mjs} +2 -2
- package/{acp-wxLiqsce.mjs.map → acp-NXEQGwmj.mjs.map} +1 -1
- package/{connect-Da_1ZXLf.mjs → connect-g9Ln2khv.mjs} +3 -3
- package/{connect-Da_1ZXLf.mjs.map → connect-g9Ln2khv.mjs.map} +1 -1
- package/{disconnect-B-Pgh2uG.mjs → disconnect-CfUb2fN5.mjs} +2 -2
- package/{disconnect-B-Pgh2uG.mjs.map → disconnect-CfUb2fN5.mjs.map} +1 -1
- package/{discovery-CYiDVyU6.mjs → discovery-Vf2_BF_z.mjs} +2 -2
- package/{discovery-CYiDVyU6.mjs.map → discovery-Vf2_BF_z.mjs.map} +1 -1
- package/{identity-M1WZUqu0.mjs → identity-BDrVSoYv.mjs} +2 -2
- package/{identity-M1WZUqu0.mjs.map → identity-BDrVSoYv.mjs.map} +1 -1
- package/index.mjs +9 -9
- package/package-BcFKZS-j.mjs +6 -0
- package/package-BcFKZS-j.mjs.map +1 -0
- package/package.json +4 -4
- package/{relay-CbB-VCsr.mjs → relay-x3PglJcM.mjs} +3 -3
- package/{relay-CbB-VCsr.mjs.map → relay-x3PglJcM.mjs.map} +1 -1
- package/{restart-bMtkLGXM.mjs → restart-D1qJcL0f.mjs} +2 -2
- package/{restart-bMtkLGXM.mjs.map → restart-D1qJcL0f.mjs.map} +1 -1
- package/{serve-Dba-6XyV.mjs → serve-C4LsoXFe.mjs} +10 -4
- package/serve-C4LsoXFe.mjs.map +1 -0
- package/{start-_OgO4fEe.mjs → start-D4cYakNA.mjs} +2 -2
- package/{start-_OgO4fEe.mjs.map → start-D4cYakNA.mjs.map} +1 -1
- package/{status-Dnw-3t4J.mjs → status-Hmj-XlPh.mjs} +2 -2
- package/{status-Dnw-3t4J.mjs.map → status-Hmj-XlPh.mjs.map} +1 -1
- package/{stop-BcbFFErk.mjs → stop-CdNtlmSF.mjs} +2 -2
- package/{stop-BcbFFErk.mjs.map → stop-CdNtlmSF.mjs.map} +1 -1
- package/package-DZfIu4Eq.mjs +0 -6
- package/package-DZfIu4Eq.mjs.map +0 -1
- package/serve-Dba-6XyV.mjs.map +0 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"relay-CbB-VCsr.mjs","names":["Machine","Private.write","pkg.version","File.read","File.update","File.checkId","File.removeLink","File.markLeaving"],"sources":["../../src/machine/file.ts","../../src/machine/relay.ts"],"sourcesContent":["import { Jws } from \"@codeworksh/schema/jws\";\nimport { Relay } from \"@codeworksh/schema/relay\";\nimport { Effect, FileSystem, Option, Schedule, Schema } from \"effect\";\nimport { uuidv7 } from \"uuidv7\";\nimport * as Private from \"../framework/private.ts\";\n\n/**\n * `<home>/machine.json`: who this home is as a machine, and its link to each connected workspace.\n *\n * It survives restarts (unlike `service.json`, which a server writes and removes) and holds private\n * keys, so it is written 0600, atomically, and under a lock shared by `serve` and `codework connect`.\n */\nconst Link = Schema.Struct({\n\tname: Schema.String,\n\taccount: Schema.Struct({ id: Schema.String, name: Schema.NullOr(Schema.String) }),\n\t/** The relay the link was made with; heartbeats and disconnect go there. */\n\trelay: Schema.String,\n\tkey: Jws.PrivateKey,\n\tkeyId: Schema.String,\n\t/** Left locally, but the relay hasn't confirmed: the key is kept to revoke it once the relay answers. */\n\tleaving: Schema.optionalKey(Schema.Boolean),\n});\n\n/** One key per home, vouching for each link key, so the relay knows every link of this home is one machine. */\nconst Identity = Schema.Struct({ key: Jws.PrivateKey, keyId: Schema.String });\ntype Identity = typeof Identity.Type;\n\nexport const Machine = Schema.Struct({\n\tid: Schema.String,\n\tsource: Relay.IdSource,\n\t/** The user's own name for the machine; shown in place of the OS host name when set. */\n\tgivenName: Schema.optional(Schema.String),\n\t// Optional only for homes connected before identities existed; `update` gives every home one.\n\tidentity: Schema.optional(Identity),\n\tworkspaces: Schema.Record(Schema.String, Link),\n});\nexport type Machine = typeof Machine.Type;\n/** The machine as {@link update} hands it out: always with its identity. */\ntype Identified = Machine & { readonly identity: Identity };\n\nconst json = Schema.fromJsonString(Machine);\n\nclass MachineError extends Schema.TaggedError<MachineError>()(\"Machine.MachineError\", {\n\tmessage: Schema.String,\n}) {}\n\nconst file = (home: string) => `${home}/machine.json`;\n\n/** The machine as stored, or none before its first run. */\nexport const read = Effect.fn(\"Machine.read\")(function* (home: string) {\n\tconst fs = yield* FileSystem.FileSystem;\n\tconst path = file(home);\n\tif (!(yield* fs.exists(path))) return Option.none<Machine>();\n\tconst text = yield* fs.readFileString(path);\n\treturn Option.some(\n\t\tyield* Schema.decodeEffect(json)(text).pipe(\n\t\t\tEffect.mapError(() => new MachineError({ message: `${path} is not a valid machine file` })),\n\t\t),\n\t);\n});\n\n// One writer at a time: creating a directory either succeeds or fails, so it serves as a lock.\nconst locked = <A, E, R>(home: string, effect: Effect.Effect<A, E, R>) =>\n\tEffect.gen(function* () {\n\t\tconst fs = yield* FileSystem.FileSystem;\n\t\tconst lock = `${file(home)}.lock`;\n\t\tyield* fs.makeDirectory(home, { recursive: true });\n\t\tyield* fs.makeDirectory(lock).pipe(\n\t\t\tEffect.retry({ schedule: Schedule.spaced(\"50 millis\"), times: 100 }),\n\t\t\tEffect.mapError(() => new MachineError({ message: `${lock} is held by another process` })),\n\t\t);\n\t\treturn yield* effect.pipe(Effect.ensuring(fs.remove(lock, { recursive: true }).pipe(Effect.ignore)));\n\t});\n\nconst write = (home: string, machine: Machine) =>\n\tSchema.encodeEffect(json)(machine).pipe(Effect.flatMap((encoded) => Private.write(file(home), encoded)));\n\nconst newIdentity = Effect.gen(function* () {\n\tconst key = yield* Jws.generate();\n\treturn { key, keyId: yield* Jws.thumbprint(Jws.publicOf(key)) } satisfies Identity;\n});\n\n/** Reads, changes and writes the machine under the lock, creating its identity on first use. */\nexport const update = <E, R>(\n\thome: string,\n\toptions: { readonly machineId?: string | undefined },\n\tchange: (machine: Identified) => Effect.Effect<Identified, E, R>,\n) =>\n\tlocked(\n\t\thome,\n\t\tEffect.gen(function* () {\n\t\t\tconst existing = yield* read(home);\n\t\t\tconst machine = yield* Option.match(existing, {\n\t\t\t\tonNone: () => Effect.succeed(created(options.machineId)),\n\t\t\t\tonSome: (machine) =>\n\t\t\t\t\toptions.machineId === undefined || options.machineId === machine.id\n\t\t\t\t\t\t? Effect.succeed(machine)\n\t\t\t\t\t\t: // `serve` generates an id at startup, so a rebuilt machine has one before it reclaims its old id;\n\t\t\t\t\t\t\t// one no workspace has seen yet can still give way.\n\t\t\t\t\t\t\tmachine.source === \"generated\" && Object.keys(machine.workspaces).length === 0\n\t\t\t\t\t\t\t? Effect.succeed({ ...machine, id: options.machineId, source: \"flag\" as const })\n\t\t\t\t\t\t\t: Effect.fail(\n\t\t\t\t\t\t\t\t\tnew MachineError({\n\t\t\t\t\t\t\t\t\t\tmessage: `This home is machine ${machine.id}; its id can't change to ${options.machineId}. Use another --home.`,\n\t\t\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t\t\t),\n\t\t\t});\n\t\t\tconst identity = machine.identity ?? (yield* newIdentity);\n\t\t\tconst next = yield* change({ ...machine, identity });\n\t\t\tyield* write(home, next);\n\t\t\treturn next;\n\t\t}),\n\t);\n\n/** The home's identity, created with the machine on first use. */\nexport const identity = Effect.fn(\"Machine.identity\")(function* (home: string) {\n\tconst stored = yield* read(home);\n\tif (Option.isSome(stored) && stored.value.identity !== undefined) return stored.value.identity;\n\treturn (yield* update(home, {}, Effect.succeed)).identity;\n});\n\nconst created = (machineId: string | undefined): Machine =>\n\tmachineId === undefined\n\t\t? { id: uuidv7(), source: \"generated\", workspaces: {} }\n\t\t: { id: machineId, source: \"flag\", workspaces: {} };\n\n/** Validates a `--machine-id` before anything is written. */\nexport const checkId = (machineId: string | undefined) =>\n\tmachineId === undefined || Schema.is(Relay.MachineId)(machineId)\n\t\t? Effect.void\n\t\t: Effect.fail(\n\t\t\t\tnew MachineError({\n\t\t\t\t\tmessage: `--machine-id must be 1-64 lowercase letters, digits or dashes, starting with a letter or digit`,\n\t\t\t\t}),\n\t\t\t);\n\n/**\n * Forgets the link to a workspace, only while it still holds `keyId`: a reconnect that saved a new key in\n * the meantime keeps its link.\n */\nexport const removeLink = (home: string, workspaceId: string, keyId: string) =>\n\tupdate(home, {}, (machine) => {\n\t\tconst { [workspaceId]: link, ...workspaces } = machine.workspaces;\n\t\treturn Effect.succeed(link === undefined || link.keyId !== keyId ? machine : { ...machine, workspaces });\n\t});\n\n/** Marks the link as leaving, only while it still holds `keyId`. */\nexport const markLeaving = (home: string, workspaceId: string, keyId: string) =>\n\tupdate(home, {}, (machine) => {\n\t\tconst link = machine.workspaces[workspaceId];\n\t\treturn Effect.succeed(\n\t\t\tlink === undefined || link.keyId !== keyId\n\t\t\t\t? machine\n\t\t\t\t: { ...machine, workspaces: { ...machine.workspaces, [workspaceId]: { ...link, leaving: true } } },\n\t\t);\n\t});\n","import { NodeServices } from \"@effect/platform-node\";\nimport { Jws } from \"@codeworksh/schema/jws\";\nimport { Machine } from \"@codeworksh/schema/machine\";\nimport { Relay } from \"@codeworksh/schema/relay\";\nimport { Clock, Config, Context, Duration, Effect, Layer } from \"effect\";\nimport { FetchHttpClient } from \"effect/http\";\nimport { HttpApiClient } from \"effect/http-api\";\nimport { uuidv7 } from \"uuidv7\";\nimport pkg from \"../../package.json\" with { type: \"json\" };\nimport { hostName } from \"../server/identity.ts\";\nimport * as File from \"./file.ts\";\n\n/** The relay's typed client at `url`. */\nconst at = (url: string) => HttpApiClient.make(Relay.Api, { baseUrl: url }).pipe(Effect.provide(FetchHttpClient.layer));\n\n/**\n * The relay new connects go to: `CODEWORK_RELAY_URL`, default https://relay.codework.sh. A link keeps\n * talking to the relay it was made with, whatever this says later.\n */\nexport class Client extends Context.Service<\n\tClient,\n\t{ readonly url: string; readonly api: HttpApiClient.ForApi<typeof Relay.Api> }\n>()(\"@codeworksh/cli/machine/relay/Client\") {\n\tstatic readonly layer = Layer.effect(\n\t\tClient,\n\t\tEffect.gen(function* () {\n\t\t\tconst url = yield* Config.String(\"CODEWORK_RELAY_URL\").pipe(Config.withDefault(\"https://relay.codework.sh\"));\n\t\t\treturn { url, api: yield* at(url) };\n\t\t}),\n\t);\n}\n\n/** What this machine says about itself to the relay. */\nconst describe = Effect.fn(\"Machine.describe\")(function* (machine: File.Machine) {\n\tconst localHostName = yield* hostName.pipe(Effect.provide(NodeServices.layer));\n\treturn {\n\t\tid: machine.id,\n\t\tsource: machine.source,\n\t\tlocalHostName,\n\t\tgivenName: machine.givenName ?? null,\n\t\tos: process.platform,\n\t\tarch: process.arch,\n\t\tversion: pkg.version,\n\t} satisfies Relay.MachineInfo;\n});\n\n/** This machine and its workspaces, as the desktop app shows them. */\nexport const status = Effect.fn(\"Machine.status\")(function* (home: string) {\n\tconst stored = yield* File.read(home);\n\tconst localHostName = yield* hostName.pipe(Effect.provide(NodeServices.layer));\n\tif (stored._tag === \"None\")\n\t\treturn { id: null, localHostName, givenName: null, workspaces: [] } satisfies Machine.Status;\n\tconst machine = stored.value;\n\treturn {\n\t\tid: machine.id,\n\t\tlocalHostName,\n\t\tgivenName: machine.givenName ?? null,\n\t\tworkspaces: Object.entries(machine.workspaces).map(([id, link]) => ({\n\t\t\tid,\n\t\t\tname: link.name,\n\t\t\tkeyId: link.keyId,\n\t\t\tleaving: link.leaving === true,\n\t\t})),\n\t} satisfies Machine.Status;\n});\n\n/**\n * Names this machine for people: empty clears the name back to the OS's. The relay hears it on the\n * next heartbeat.\n */\nexport const rename = Effect.fn(\"Machine.rename\")(function* (home: string, givenName: string) {\n\tconst name = givenName.trim();\n\tyield* File.update(home, {}, ({ givenName: _previous, ...machine }) =>\n\t\tEffect.succeed(name === \"\" ? machine : { ...machine, givenName: name }),\n\t);\n\treturn yield* status(home);\n});\n\n/**\n * Pairs this home with a workspace: registers a fresh key with the relay, shows the code a member\n * approves, and polls with that key until the relay links it, then saves the link. Each workspace gets\n * its own key, and only this process can take the link: a connect that stops is never linked.\n */\nexport const connect = Effect.fn(\"Machine.connect\")(function* (options: {\n\treadonly home: string;\n\treadonly workspaceId?: string | undefined;\n\treadonly machineId?: string | undefined;\n\treadonly onCode: (userCode: string) => Effect.Effect<void>;\n}) {\n\tconst client = yield* Client;\n\tyield* File.checkId(options.machineId);\n\tconst machine = yield* File.update(options.home, { machineId: options.machineId }, Effect.succeed);\n\tconst key = yield* Jws.generate();\n\tconst keyId = yield* Jws.thumbprint(Jws.publicOf(key));\n\tconst iat = yield* Clock.currentTimeMillis;\n\tconst binding: Relay.IdentityClaims = { op: \"identity.bind\", iat, keyId };\n\tconst claims: Relay.PairingClaims = {\n\t\top: \"pairing.create\",\n\t\tiat,\n\t\tmachine: yield* describe(machine),\n\t\tworkspaceId: options.workspaceId ?? null,\n\t\tidentity: yield* Jws.sign(binding, machine.identity.key, { embed: true }),\n\t};\n\tconst pairing = yield* client.api.machine\n\t\t.pair({ payload: { jws: yield* Jws.sign(claims, key, { embed: true }) } })\n\t\t.pipe(\n\t\t\tEffect.catchTag(\"Relay.NotFound\", () =>\n\t\t\t\tEffect.fail(new Machine.ConnectError({ message: \"No such workspace\" })),\n\t\t\t),\n\t\t);\n\tyield* options.onCode(pairing.userCode);\n\n\tconst poll = Effect.gen(function* () {\n\t\tconst claims: Relay.PollClaims = { op: \"pairing.poll\", iat: yield* Clock.currentTimeMillis };\n\t\treturn yield* client.api.machine.poll({ payload: { jws: yield* Jws.sign(claims, key, { kid: keyId }) } });\n\t}).pipe(\n\t\t// A dropped request or a relay restart doesn't end the connect; the next poll asks again.\n\t\tEffect.catchTag(\"HttpClientError\", (error) =>\n\t\t\tEffect.logDebug(\"Pairing poll failed\", error).pipe(Effect.as(undefined)),\n\t\t),\n\t);\n\tlet previous = machine;\n\tconst deadline = (yield* Clock.currentTimeMillis) + pairing.expiresIn * 1000;\n\twhile ((yield* Clock.currentTimeMillis) < deadline) {\n\t\tyield* Effect.sleep(Duration.seconds(pairing.interval));\n\t\tconst status = yield* poll;\n\t\tif (status === undefined) continue;\n\t\tif (status.status !== \"approved\") {\n\t\t\tif (status.status === \"pending\") continue;\n\t\t\tif (status.status === \"expired\") break;\n\t\t\treturn yield* new Machine.ConnectError({\n\t\t\t\tmessage:\n\t\t\t\t\tstatus.status === \"declined\"\n\t\t\t\t\t\t? \"The connect was declined; run connect again for a new code\"\n\t\t\t\t\t\t: \"Another connect for this machine finished first\",\n\t\t\t});\n\t\t}\n\t\tconst { connected } = status;\n\t\t// A poll response can arrive after another connect saved a different key.\n\t\t// Observe that change under the lock, then poll again before replacing it.\n\t\tconst id = connected.workspace.id;\n\t\tconst stored = yield* File.update(options.home, {}, (current) =>\n\t\t\tEffect.succeed(\n\t\t\t\tcurrent.workspaces[id]?.keyId !== previous.workspaces[id]?.keyId\n\t\t\t\t\t? current\n\t\t\t\t\t: {\n\t\t\t\t\t\t\t...current,\n\t\t\t\t\t\t\tworkspaces: {\n\t\t\t\t\t\t\t\t...current.workspaces,\n\t\t\t\t\t\t\t\t[connected.workspace.id]: {\n\t\t\t\t\t\t\t\t\tname: connected.workspace.name,\n\t\t\t\t\t\t\t\t\taccount: connected.account,\n\t\t\t\t\t\t\t\t\trelay: client.url,\n\t\t\t\t\t\t\t\t\tkey,\n\t\t\t\t\t\t\t\t\tkeyId,\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t),\n\t\t);\n\t\tif (stored.workspaces[id]?.keyId === keyId) return connected;\n\t\tprevious = stored;\n\t}\n\treturn yield* new Machine.ConnectError({ message: \"The code expired before it was approved; run connect again\" });\n});\n\n/**\n * Asks the link's relay to revoke it, signed with its own key: true once the relay confirms, or says it\n * was revoked already; false when it can't be reached in time.\n */\nconst revoke = (machineId: string, workspaceId: string, link: File.Machine[\"workspaces\"][string]) =>\n\tEffect.gen(function* () {\n\t\tconst claims: Relay.RevokeClaims = {\n\t\t\top: \"machine.revoke\",\n\t\t\tiat: yield* Clock.currentTimeMillis,\n\t\t\tjti: uuidv7(),\n\t\t\tworkspaceId,\n\t\t\tmachineId,\n\t\t};\n\t\tconst api = yield* at(link.relay);\n\t\tyield* api.machine.revoke({ payload: { jws: yield* Jws.sign(claims, link.key, { kid: link.keyId }) } });\n\t}).pipe(\n\t\t// Leave time for the local update before the desktop RPC expires.\n\t\tEffect.timeout(\"3 seconds\"),\n\t\tEffect.as(true),\n\t\tEffect.catchTag(\"Relay.MachineRevoked\", () => Effect.succeed(true)),\n\t\tEffect.catch((error) => Effect.logDebug(`Revoke at ${link.name} failed`, error).pipe(Effect.as(false))),\n\t);\n\n/**\n * One heartbeat per connected workspace, each to the relay its link was made with, all at once. A link\n * the relay refuses as revoked is removed, but only while it still holds the refused key. A leaving link\n * asks for its revoke again instead, and is removed once the relay confirms.\n */\nexport const heartbeat = Effect.fn(\"Machine.heartbeat\")(function* (home: string) {\n\tconst stored = yield* File.read(home);\n\tif (stored._tag === \"None\") return;\n\tconst machine = stored.value;\n\tconst info = yield* describe(machine);\n\tyield* Effect.forEach(\n\t\tObject.entries(machine.workspaces),\n\t\t([workspaceId, link]) =>\n\t\t\tlink.leaving === true\n\t\t\t\t? revoke(machine.id, workspaceId, link).pipe(\n\t\t\t\t\t\tEffect.flatMap((revoked) =>\n\t\t\t\t\t\t\trevoked ? File.removeLink(home, workspaceId, link.keyId).pipe(Effect.asVoid) : Effect.void,\n\t\t\t\t\t\t),\n\t\t\t\t\t\tEffect.catch((error) => Effect.logDebug(`Leaving ${link.name} failed`, error)),\n\t\t\t\t\t)\n\t\t\t\t: Effect.gen(function* () {\n\t\t\t\t\t\tconst api = yield* at(link.relay);\n\t\t\t\t\t\tconst claims: Relay.HeartbeatClaims = {\n\t\t\t\t\t\t\top: \"machine.heartbeat\",\n\t\t\t\t\t\t\tiat: yield* Clock.currentTimeMillis,\n\t\t\t\t\t\t\tworkspaceId,\n\t\t\t\t\t\t\tmachine: info,\n\t\t\t\t\t\t};\n\t\t\t\t\t\tyield* api.machine.heartbeat({\n\t\t\t\t\t\t\tpayload: { jws: yield* Jws.sign(claims, link.key, { kid: link.keyId }) },\n\t\t\t\t\t\t});\n\t\t\t\t\t}).pipe(\n\t\t\t\t\t\tEffect.timeout(\"10 seconds\"),\n\t\t\t\t\t\tEffect.catchTags({\n\t\t\t\t\t\t\t\"Relay.MachineRevoked\": (revoked) =>\n\t\t\t\t\t\t\t\tEffect.logWarning(`Disconnected from ${link.name}: ${revoked.message}`).pipe(\n\t\t\t\t\t\t\t\t\tEffect.andThen(File.removeLink(home, workspaceId, link.keyId)),\n\t\t\t\t\t\t\t\t),\n\t\t\t\t\t\t\t// Refused but not revoked, most often a clock more than a minute off: worth seeing in the log.\n\t\t\t\t\t\t\t\"Relay.Unauthorized\": (refused) =>\n\t\t\t\t\t\t\t\tEffect.logWarning(`Heartbeat to ${link.name} refused: ${refused.message}`),\n\t\t\t\t\t\t}),\n\t\t\t\t\t\tEffect.catch((error) => Effect.logDebug(`Heartbeat to ${link.name} failed`, error)),\n\t\t\t\t\t),\n\t\t{ concurrency: \"unbounded\", discard: true },\n\t);\n});\n\n/**\n * Leaves a workspace: asks the link's relay to revoke it, then forgets it locally. When the relay can't\n * confirm, the link is kept as leaving, with its key, and each heartbeat asks again until it does; the\n * relay still lists it until then. `revoked` says whether it confirmed now.\n */\nexport const disconnect = Effect.fn(\"Machine.disconnect\")(function* (\n\thome: string,\n\tworkspaceId: string,\n\texpectedKeyId?: string,\n) {\n\tconst stored = yield* File.read(home);\n\tconst link = stored._tag === \"Some\" ? stored.value.workspaces[workspaceId] : undefined;\n\tif (stored._tag === \"None\" || link === undefined) {\n\t\treturn yield* new Machine.ConnectError({ message: `This machine is not connected to workspace ${workspaceId}` });\n\t}\n\tif (expectedKeyId !== undefined && link.keyId !== expectedKeyId)\n\t\treturn yield* new Machine.ConnectError({ message: \"This machine link has changed\" });\n\tconst revoked = yield* revoke(stored.value.id, workspaceId, link);\n\tyield* revoked ? File.removeLink(home, workspaceId, link.keyId) : File.markLeaving(home, workspaceId, link.keyId);\n\treturn { workspace: link.name, revoked };\n});\n"],"mappings":";;;;;;;;;;;;;;;;;;AAYA,MAAM,OAAO,OAAO,OAAO;CAC1B,MAAM,OAAO;CACb,SAAS,OAAO,OAAO;EAAE,IAAI,OAAO;EAAQ,MAAM,OAAO,OAAO,OAAO,MAAM;CAAE,CAAC;;CAEhF,OAAO,OAAO;CACd,KAAK,IAAI;CACT,OAAO,OAAO;;CAEd,SAAS,OAAO,YAAY,OAAO,OAAO;AAC3C,CAAC;;AAGD,MAAM,WAAW,OAAO,OAAO;CAAE,KAAK,IAAI;CAAY,OAAO,OAAO;AAAO,CAAC;AAG5E,MAAaA,YAAU,OAAO,OAAO;CACpC,IAAI,OAAO;CACX,QAAQ,MAAM;;CAEd,WAAW,OAAO,SAAS,OAAO,MAAM;CAExC,UAAU,OAAO,SAAS,QAAQ;CAClC,YAAY,OAAO,OAAO,OAAO,QAAQ,IAAI;AAC9C,CAAC;AAKD,MAAM,OAAO,OAAO,eAAeA,SAAO;AAE1C,IAAM,eAAN,cAA2B,OAAO,YAA0B,CAAC,CAAC,wBAAwB,EACrF,SAAS,OAAO,OACjB,CAAC,CAAC,CAAC,CAAC;AAEJ,MAAM,QAAQ,SAAiB,GAAG,KAAK;;AAGvC,MAAa,OAAO,OAAO,GAAG,cAAc,CAAC,CAAC,WAAW,MAAc;CACtE,MAAM,KAAK,OAAO,WAAW;CAC7B,MAAM,OAAO,KAAK,IAAI;CACtB,IAAI,EAAE,OAAO,GAAG,OAAO,IAAI,IAAI,OAAO,OAAO,KAAc;CAC3D,MAAM,OAAO,OAAO,GAAG,eAAe,IAAI;CAC1C,OAAO,OAAO,KACb,OAAO,OAAO,aAAa,IAAI,CAAC,CAAC,IAAI,CAAC,CAAC,KACtC,OAAO,eAAe,IAAI,aAAa,EAAE,SAAS,GAAG,KAAK,8BAA8B,CAAC,CAAC,CAC3F,CACD;AACD,CAA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;CAChH,OAAO;EAAE,WAAW,KAAK;EAAM;CAAQ;AACxC,CAAC"}
|
|
1
|
+
{"version":3,"file":"relay-x3PglJcM.mjs","names":["Machine","Private.write","pkg.version","File.read","File.update","File.checkId","File.removeLink","File.markLeaving"],"sources":["../../src/machine/file.ts","../../src/machine/relay.ts"],"sourcesContent":["import { Jws } from \"@codeworksh/schema/jws\";\nimport { Relay } from \"@codeworksh/schema/relay\";\nimport { Effect, FileSystem, Option, Schedule, Schema } from \"effect\";\nimport { uuidv7 } from \"uuidv7\";\nimport * as Private from \"../framework/private.ts\";\n\n/**\n * `<home>/machine.json`: who this home is as a machine, and its link to each connected workspace.\n *\n * It survives restarts (unlike `service.json`, which a server writes and removes) and holds private\n * keys, so it is written 0600, atomically, and under a lock shared by `serve` and `codework connect`.\n */\nconst Link = Schema.Struct({\n\tname: Schema.String,\n\taccount: Schema.Struct({ id: Schema.String, name: Schema.NullOr(Schema.String) }),\n\t/** The relay the link was made with; heartbeats and disconnect go there. */\n\trelay: Schema.String,\n\tkey: Jws.PrivateKey,\n\tkeyId: Schema.String,\n\t/** Left locally, but the relay hasn't confirmed: the key is kept to revoke it once the relay answers. */\n\tleaving: Schema.optionalKey(Schema.Boolean),\n});\n\n/** One key per home, vouching for each link key, so the relay knows every link of this home is one machine. */\nconst Identity = Schema.Struct({ key: Jws.PrivateKey, keyId: Schema.String });\ntype Identity = typeof Identity.Type;\n\nexport const Machine = Schema.Struct({\n\tid: Schema.String,\n\tsource: Relay.IdSource,\n\t/** The user's own name for the machine; shown in place of the OS host name when set. */\n\tgivenName: Schema.optional(Schema.String),\n\t// Optional only for homes connected before identities existed; `update` gives every home one.\n\tidentity: Schema.optional(Identity),\n\tworkspaces: Schema.Record(Schema.String, Link),\n});\nexport type Machine = typeof Machine.Type;\n/** The machine as {@link update} hands it out: always with its identity. */\ntype Identified = Machine & { readonly identity: Identity };\n\nconst json = Schema.fromJsonString(Machine);\n\nclass MachineError extends Schema.TaggedError<MachineError>()(\"Machine.MachineError\", {\n\tmessage: Schema.String,\n}) {}\n\nconst file = (home: string) => `${home}/machine.json`;\n\n/** The machine as stored, or none before its first run. */\nexport const read = Effect.fn(\"Machine.read\")(function* (home: string) {\n\tconst fs = yield* FileSystem.FileSystem;\n\tconst path = file(home);\n\tif (!(yield* fs.exists(path))) return Option.none<Machine>();\n\tconst text = yield* fs.readFileString(path);\n\treturn Option.some(\n\t\tyield* Schema.decodeEffect(json)(text).pipe(\n\t\t\tEffect.mapError(() => new MachineError({ message: `${path} is not a valid machine file` })),\n\t\t),\n\t);\n});\n\n// One writer at a time: creating a directory either succeeds or fails, so it serves as a lock.\nconst locked = <A, E, R>(home: string, effect: Effect.Effect<A, E, R>) =>\n\tEffect.gen(function* () {\n\t\tconst fs = yield* FileSystem.FileSystem;\n\t\tconst lock = `${file(home)}.lock`;\n\t\tyield* fs.makeDirectory(home, { recursive: true });\n\t\tyield* fs.makeDirectory(lock).pipe(\n\t\t\tEffect.retry({ schedule: Schedule.spaced(\"50 millis\"), times: 100 }),\n\t\t\tEffect.mapError(() => new MachineError({ message: `${lock} is held by another process` })),\n\t\t);\n\t\treturn yield* effect.pipe(Effect.ensuring(fs.remove(lock, { recursive: true }).pipe(Effect.ignore)));\n\t});\n\nconst write = (home: string, machine: Machine) =>\n\tSchema.encodeEffect(json)(machine).pipe(Effect.flatMap((encoded) => Private.write(file(home), encoded)));\n\nconst newIdentity = Effect.gen(function* () {\n\tconst key = yield* Jws.generate();\n\treturn { key, keyId: yield* Jws.thumbprint(Jws.publicOf(key)) } satisfies Identity;\n});\n\n/** Reads, changes and writes the machine under the lock, creating its identity on first use. */\nexport const update = <E, R>(\n\thome: string,\n\toptions: { readonly machineId?: string | undefined },\n\tchange: (machine: Identified) => Effect.Effect<Identified, E, R>,\n) =>\n\tlocked(\n\t\thome,\n\t\tEffect.gen(function* () {\n\t\t\tconst existing = yield* read(home);\n\t\t\tconst machine = yield* Option.match(existing, {\n\t\t\t\tonNone: () => Effect.succeed(created(options.machineId)),\n\t\t\t\tonSome: (machine) =>\n\t\t\t\t\toptions.machineId === undefined || options.machineId === machine.id\n\t\t\t\t\t\t? Effect.succeed(machine)\n\t\t\t\t\t\t: // `serve` generates an id at startup, so a rebuilt machine has one before it reclaims its old id;\n\t\t\t\t\t\t\t// one no workspace has seen yet can still give way.\n\t\t\t\t\t\t\tmachine.source === \"generated\" && Object.keys(machine.workspaces).length === 0\n\t\t\t\t\t\t\t? Effect.succeed({ ...machine, id: options.machineId, source: \"flag\" as const })\n\t\t\t\t\t\t\t: Effect.fail(\n\t\t\t\t\t\t\t\t\tnew MachineError({\n\t\t\t\t\t\t\t\t\t\tmessage: `This home is machine ${machine.id}; its id can't change to ${options.machineId}. Use another --home.`,\n\t\t\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t\t\t),\n\t\t\t});\n\t\t\tconst identity = machine.identity ?? (yield* newIdentity);\n\t\t\tconst next = yield* change({ ...machine, identity });\n\t\t\tyield* write(home, next);\n\t\t\treturn next;\n\t\t}),\n\t);\n\n/** The home's identity, created with the machine on first use. */\nexport const identity = Effect.fn(\"Machine.identity\")(function* (home: string) {\n\tconst stored = yield* read(home);\n\tif (Option.isSome(stored) && stored.value.identity !== undefined) return stored.value.identity;\n\treturn (yield* update(home, {}, Effect.succeed)).identity;\n});\n\nconst created = (machineId: string | undefined): Machine =>\n\tmachineId === undefined\n\t\t? { id: uuidv7(), source: \"generated\", workspaces: {} }\n\t\t: { id: machineId, source: \"flag\", workspaces: {} };\n\n/** Validates a `--machine-id` before anything is written. */\nexport const checkId = (machineId: string | undefined) =>\n\tmachineId === undefined || Schema.is(Relay.MachineId)(machineId)\n\t\t? Effect.void\n\t\t: Effect.fail(\n\t\t\t\tnew MachineError({\n\t\t\t\t\tmessage: `--machine-id must be 1-64 lowercase letters, digits or dashes, starting with a letter or digit`,\n\t\t\t\t}),\n\t\t\t);\n\n/**\n * Forgets the link to a workspace, only while it still holds `keyId`: a reconnect that saved a new key in\n * the meantime keeps its link.\n */\nexport const removeLink = (home: string, workspaceId: string, keyId: string) =>\n\tupdate(home, {}, (machine) => {\n\t\tconst { [workspaceId]: link, ...workspaces } = machine.workspaces;\n\t\treturn Effect.succeed(link === undefined || link.keyId !== keyId ? machine : { ...machine, workspaces });\n\t});\n\n/** Marks the link as leaving, only while it still holds `keyId`. */\nexport const markLeaving = (home: string, workspaceId: string, keyId: string) =>\n\tupdate(home, {}, (machine) => {\n\t\tconst link = machine.workspaces[workspaceId];\n\t\treturn Effect.succeed(\n\t\t\tlink === undefined || link.keyId !== keyId\n\t\t\t\t? machine\n\t\t\t\t: { ...machine, workspaces: { ...machine.workspaces, [workspaceId]: { ...link, leaving: true } } },\n\t\t);\n\t});\n","import { NodeServices } from \"@effect/platform-node\";\nimport { Jws } from \"@codeworksh/schema/jws\";\nimport { Machine } from \"@codeworksh/schema/machine\";\nimport { Relay } from \"@codeworksh/schema/relay\";\nimport { Clock, Config, Context, Duration, Effect, Layer } from \"effect\";\nimport { FetchHttpClient } from \"effect/http\";\nimport { HttpApiClient } from \"effect/http-api\";\nimport { uuidv7 } from \"uuidv7\";\nimport pkg from \"../../package.json\" with { type: \"json\" };\nimport { hostName } from \"../server/identity.ts\";\nimport * as File from \"./file.ts\";\n\n/** The relay's typed client at `url`. */\nconst at = (url: string) => HttpApiClient.make(Relay.Api, { baseUrl: url }).pipe(Effect.provide(FetchHttpClient.layer));\n\n/**\n * The relay new connects go to: `CODEWORK_RELAY_URL`, default https://relay.codework.sh. A link keeps\n * talking to the relay it was made with, whatever this says later.\n */\nexport class Client extends Context.Service<\n\tClient,\n\t{ readonly url: string; readonly api: HttpApiClient.ForApi<typeof Relay.Api> }\n>()(\"@codeworksh/cli/machine/relay/Client\") {\n\tstatic readonly layer = Layer.effect(\n\t\tClient,\n\t\tEffect.gen(function* () {\n\t\t\tconst url = yield* Config.String(\"CODEWORK_RELAY_URL\").pipe(Config.withDefault(\"https://relay.codework.sh\"));\n\t\t\treturn { url, api: yield* at(url) };\n\t\t}),\n\t);\n}\n\n/** What this machine says about itself to the relay. */\nconst describe = Effect.fn(\"Machine.describe\")(function* (machine: File.Machine) {\n\tconst localHostName = yield* hostName.pipe(Effect.provide(NodeServices.layer));\n\treturn {\n\t\tid: machine.id,\n\t\tsource: machine.source,\n\t\tlocalHostName,\n\t\tgivenName: machine.givenName ?? null,\n\t\tos: process.platform,\n\t\tarch: process.arch,\n\t\tversion: pkg.version,\n\t} satisfies Relay.MachineInfo;\n});\n\n/** This machine and its workspaces, as the desktop app shows them. */\nexport const status = Effect.fn(\"Machine.status\")(function* (home: string) {\n\tconst stored = yield* File.read(home);\n\tconst localHostName = yield* hostName.pipe(Effect.provide(NodeServices.layer));\n\tif (stored._tag === \"None\")\n\t\treturn { id: null, localHostName, givenName: null, workspaces: [] } satisfies Machine.Status;\n\tconst machine = stored.value;\n\treturn {\n\t\tid: machine.id,\n\t\tlocalHostName,\n\t\tgivenName: machine.givenName ?? null,\n\t\tworkspaces: Object.entries(machine.workspaces).map(([id, link]) => ({\n\t\t\tid,\n\t\t\tname: link.name,\n\t\t\tkeyId: link.keyId,\n\t\t\tleaving: link.leaving === true,\n\t\t})),\n\t} satisfies Machine.Status;\n});\n\n/**\n * Names this machine for people: empty clears the name back to the OS's. The relay hears it on the\n * next heartbeat.\n */\nexport const rename = Effect.fn(\"Machine.rename\")(function* (home: string, givenName: string) {\n\tconst name = givenName.trim();\n\tyield* File.update(home, {}, ({ givenName: _previous, ...machine }) =>\n\t\tEffect.succeed(name === \"\" ? machine : { ...machine, givenName: name }),\n\t);\n\treturn yield* status(home);\n});\n\n/**\n * Pairs this home with a workspace: registers a fresh key with the relay, shows the code a member\n * approves, and polls with that key until the relay links it, then saves the link. Each workspace gets\n * its own key, and only this process can take the link: a connect that stops is never linked.\n */\nexport const connect = Effect.fn(\"Machine.connect\")(function* (options: {\n\treadonly home: string;\n\treadonly workspaceId?: string | undefined;\n\treadonly machineId?: string | undefined;\n\treadonly onCode: (userCode: string) => Effect.Effect<void>;\n}) {\n\tconst client = yield* Client;\n\tyield* File.checkId(options.machineId);\n\tconst machine = yield* File.update(options.home, { machineId: options.machineId }, Effect.succeed);\n\tconst key = yield* Jws.generate();\n\tconst keyId = yield* Jws.thumbprint(Jws.publicOf(key));\n\tconst iat = yield* Clock.currentTimeMillis;\n\tconst binding: Relay.IdentityClaims = { op: \"identity.bind\", iat, keyId };\n\tconst claims: Relay.PairingClaims = {\n\t\top: \"pairing.create\",\n\t\tiat,\n\t\tmachine: yield* describe(machine),\n\t\tworkspaceId: options.workspaceId ?? null,\n\t\tidentity: yield* Jws.sign(binding, machine.identity.key, { embed: true }),\n\t};\n\tconst pairing = yield* client.api.machine\n\t\t.pair({ payload: { jws: yield* Jws.sign(claims, key, { embed: true }) } })\n\t\t.pipe(\n\t\t\tEffect.catchTag(\"Relay.NotFound\", () =>\n\t\t\t\tEffect.fail(new Machine.ConnectError({ message: \"No such workspace\" })),\n\t\t\t),\n\t\t);\n\tyield* options.onCode(pairing.userCode);\n\n\tconst poll = Effect.gen(function* () {\n\t\tconst claims: Relay.PollClaims = { op: \"pairing.poll\", iat: yield* Clock.currentTimeMillis };\n\t\treturn yield* client.api.machine.poll({ payload: { jws: yield* Jws.sign(claims, key, { kid: keyId }) } });\n\t}).pipe(\n\t\t// A dropped request or a relay restart doesn't end the connect; the next poll asks again.\n\t\tEffect.catchTag(\"HttpClientError\", (error) =>\n\t\t\tEffect.logDebug(\"Pairing poll failed\", error).pipe(Effect.as(undefined)),\n\t\t),\n\t);\n\tlet previous = machine;\n\tconst deadline = (yield* Clock.currentTimeMillis) + pairing.expiresIn * 1000;\n\twhile ((yield* Clock.currentTimeMillis) < deadline) {\n\t\tyield* Effect.sleep(Duration.seconds(pairing.interval));\n\t\tconst status = yield* poll;\n\t\tif (status === undefined) continue;\n\t\tif (status.status !== \"approved\") {\n\t\t\tif (status.status === \"pending\") continue;\n\t\t\tif (status.status === \"expired\") break;\n\t\t\treturn yield* new Machine.ConnectError({\n\t\t\t\tmessage:\n\t\t\t\t\tstatus.status === \"declined\"\n\t\t\t\t\t\t? \"The connect was declined; run connect again for a new code\"\n\t\t\t\t\t\t: \"Another connect for this machine finished first\",\n\t\t\t});\n\t\t}\n\t\tconst { connected } = status;\n\t\t// A poll response can arrive after another connect saved a different key.\n\t\t// Observe that change under the lock, then poll again before replacing it.\n\t\tconst id = connected.workspace.id;\n\t\tconst stored = yield* File.update(options.home, {}, (current) =>\n\t\t\tEffect.succeed(\n\t\t\t\tcurrent.workspaces[id]?.keyId !== previous.workspaces[id]?.keyId\n\t\t\t\t\t? current\n\t\t\t\t\t: {\n\t\t\t\t\t\t\t...current,\n\t\t\t\t\t\t\tworkspaces: {\n\t\t\t\t\t\t\t\t...current.workspaces,\n\t\t\t\t\t\t\t\t[connected.workspace.id]: {\n\t\t\t\t\t\t\t\t\tname: connected.workspace.name,\n\t\t\t\t\t\t\t\t\taccount: connected.account,\n\t\t\t\t\t\t\t\t\trelay: client.url,\n\t\t\t\t\t\t\t\t\tkey,\n\t\t\t\t\t\t\t\t\tkeyId,\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t),\n\t\t);\n\t\tif (stored.workspaces[id]?.keyId === keyId) return connected;\n\t\tprevious = stored;\n\t}\n\treturn yield* new Machine.ConnectError({ message: \"The code expired before it was approved; run connect again\" });\n});\n\n/**\n * Asks the link's relay to revoke it, signed with its own key: true once the relay confirms, or says it\n * was revoked already; false when it can't be reached in time.\n */\nconst revoke = (machineId: string, workspaceId: string, link: File.Machine[\"workspaces\"][string]) =>\n\tEffect.gen(function* () {\n\t\tconst claims: Relay.RevokeClaims = {\n\t\t\top: \"machine.revoke\",\n\t\t\tiat: yield* Clock.currentTimeMillis,\n\t\t\tjti: uuidv7(),\n\t\t\tworkspaceId,\n\t\t\tmachineId,\n\t\t};\n\t\tconst api = yield* at(link.relay);\n\t\tyield* api.machine.revoke({ payload: { jws: yield* Jws.sign(claims, link.key, { kid: link.keyId }) } });\n\t}).pipe(\n\t\t// Leave time for the local update before the desktop RPC expires.\n\t\tEffect.timeout(\"3 seconds\"),\n\t\tEffect.as(true),\n\t\tEffect.catchTag(\"Relay.MachineRevoked\", () => Effect.succeed(true)),\n\t\tEffect.catch((error) => Effect.logDebug(`Revoke at ${link.name} failed`, error).pipe(Effect.as(false))),\n\t);\n\n/**\n * One heartbeat per connected workspace, each to the relay its link was made with, all at once. A link\n * the relay refuses as revoked is removed, but only while it still holds the refused key. A leaving link\n * asks for its revoke again instead, and is removed once the relay confirms.\n */\nexport const heartbeat = Effect.fn(\"Machine.heartbeat\")(function* (home: string) {\n\tconst stored = yield* File.read(home);\n\tif (stored._tag === \"None\") return;\n\tconst machine = stored.value;\n\tconst info = yield* describe(machine);\n\tyield* Effect.forEach(\n\t\tObject.entries(machine.workspaces),\n\t\t([workspaceId, link]) =>\n\t\t\tlink.leaving === true\n\t\t\t\t? revoke(machine.id, workspaceId, link).pipe(\n\t\t\t\t\t\tEffect.flatMap((revoked) =>\n\t\t\t\t\t\t\trevoked ? File.removeLink(home, workspaceId, link.keyId).pipe(Effect.asVoid) : Effect.void,\n\t\t\t\t\t\t),\n\t\t\t\t\t\tEffect.catch((error) => Effect.logDebug(`Leaving ${link.name} failed`, error)),\n\t\t\t\t\t)\n\t\t\t\t: Effect.gen(function* () {\n\t\t\t\t\t\tconst api = yield* at(link.relay);\n\t\t\t\t\t\tconst claims: Relay.HeartbeatClaims = {\n\t\t\t\t\t\t\top: \"machine.heartbeat\",\n\t\t\t\t\t\t\tiat: yield* Clock.currentTimeMillis,\n\t\t\t\t\t\t\tworkspaceId,\n\t\t\t\t\t\t\tmachine: info,\n\t\t\t\t\t\t};\n\t\t\t\t\t\tyield* api.machine.heartbeat({\n\t\t\t\t\t\t\tpayload: { jws: yield* Jws.sign(claims, link.key, { kid: link.keyId }) },\n\t\t\t\t\t\t});\n\t\t\t\t\t}).pipe(\n\t\t\t\t\t\tEffect.timeout(\"10 seconds\"),\n\t\t\t\t\t\tEffect.catchTags({\n\t\t\t\t\t\t\t\"Relay.MachineRevoked\": (revoked) =>\n\t\t\t\t\t\t\t\tEffect.logWarning(`Disconnected from ${link.name}: ${revoked.message}`).pipe(\n\t\t\t\t\t\t\t\t\tEffect.andThen(File.removeLink(home, workspaceId, link.keyId)),\n\t\t\t\t\t\t\t\t),\n\t\t\t\t\t\t\t// Refused but not revoked, most often a clock more than a minute off: worth seeing in the log.\n\t\t\t\t\t\t\t\"Relay.Unauthorized\": (refused) =>\n\t\t\t\t\t\t\t\tEffect.logWarning(`Heartbeat to ${link.name} refused: ${refused.message}`),\n\t\t\t\t\t\t}),\n\t\t\t\t\t\tEffect.catch((error) => Effect.logDebug(`Heartbeat to ${link.name} failed`, error)),\n\t\t\t\t\t),\n\t\t{ concurrency: \"unbounded\", discard: true },\n\t);\n});\n\n/**\n * Leaves a workspace: asks the link's relay to revoke it, then forgets it locally. When the relay can't\n * confirm, the link is kept as leaving, with its key, and each heartbeat asks again until it does; the\n * relay still lists it until then. `revoked` says whether it confirmed now.\n */\nexport const disconnect = Effect.fn(\"Machine.disconnect\")(function* (\n\thome: string,\n\tworkspaceId: string,\n\texpectedKeyId?: string,\n) {\n\tconst stored = yield* File.read(home);\n\tconst link = stored._tag === \"Some\" ? stored.value.workspaces[workspaceId] : undefined;\n\tif (stored._tag === \"None\" || link === undefined) {\n\t\treturn yield* new Machine.ConnectError({ message: `This machine is not connected to workspace ${workspaceId}` });\n\t}\n\tif (expectedKeyId !== undefined && link.keyId !== expectedKeyId)\n\t\treturn yield* new Machine.ConnectError({ message: \"This machine link has changed\" });\n\tconst revoked = yield* revoke(stored.value.id, workspaceId, link);\n\tyield* revoked ? File.removeLink(home, workspaceId, link.keyId) : File.markLeaving(home, workspaceId, link.keyId);\n\treturn { workspace: link.name, revoked };\n});\n"],"mappings":";;;;;;;;;;;;;;;;;;AAYA,MAAM,OAAO,OAAO,OAAO;CAC1B,MAAM,OAAO;CACb,SAAS,OAAO,OAAO;EAAE,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3
|
import { r as options } from "./service-B0O4PjbL.mjs";
|
|
4
|
-
import { n as ensure, r as stop } from "./discovery-
|
|
4
|
+
import { n as ensure, r as stop } from "./discovery-Vf2_BF_z.mjs";
|
|
5
5
|
import { Effect } from "effect";
|
|
6
6
|
//#region src/cli/cmd/handlers/service/restart.ts
|
|
7
7
|
var restart_default = handler(Cmd.commands.service.commands.restart, Effect.fn("CLI.service.restart")(function* ({ host, port }) {
|
|
@@ -19,4 +19,4 @@ var restart_default = handler(Cmd.commands.service.commands.restart, Effect.fn("
|
|
|
19
19
|
//#endregion
|
|
20
20
|
export { restart_default as default };
|
|
21
21
|
|
|
22
|
-
//# sourceMappingURL=restart-
|
|
22
|
+
//# sourceMappingURL=restart-D1qJcL0f.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"restart-
|
|
1
|
+
{"version":3,"file":"restart-D1qJcL0f.mjs","names":["Runtime.handler","options","Service.options","Discovery.stop","Discovery.ensure"],"sources":["../../src/cli/cmd/handlers/service/restart.ts"],"sourcesContent":["import { Effect } from \"effect\";\nimport * as Runtime from \"../../../../framework/runtime.ts\";\nimport * as Discovery from \"../../../../server/discovery.ts\";\nimport { reportFailure } from \"../../../error.ts\";\nimport { writeOut } from \"../../../output.ts\";\nimport * as Service from \"../../../service.ts\";\nimport { Cmd } from \"../../cmd.ts\";\n\nexport default Runtime.handler(\n\tCmd.commands.service.commands.restart,\n\tEffect.fn(\"CLI.service.restart\")(function* ({ host, port }) {\n\t\tconst options = yield* Service.options(yield* Cmd.spec, { host, port });\n\t\tyield* Discovery.stop(options.home);\n\t\tconst server = yield* Discovery.ensure({ ...options, replace: true });\n\t\tyield* writeOut(`${server.url}\\n`);\n\t}, Effect.catch(reportFailure)),\n);\n"],"mappings":";;;;;;AAQA,IAAA,kBAAeA,QACd,IAAI,SAAS,QAAQ,SAAS,SAC9B,OAAO,GAAG,qBAAqB,CAAC,CAAC,WAAW,EAAE,MAAM,QAAQ;CAC3D,MAAMC,YAAU,OAAOC,QAAgB,OAAO,IAAI,MAAM;EAAE;EAAM;CAAK,CAAC;CACtE,OAAOC,KAAeF,UAAQ,IAAI;CAClC,MAAM,SAAS,OAAOG,OAAiB;EAAE,GAAGH;EAAS,SAAS;CAAK,CAAC;CACpE,OAAO,SAAS,GAAG,OAAO,IAAI,GAAG;AAClC,GAAG,OAAO,MAAM,aAAa,CAAC,CAC/B"}
|
|
@@ -2,8 +2,8 @@ import { S as contract_exports, b as encode, f as writeError, i as renderError,
|
|
|
2
2
|
import { i as Cmd, t as handler } from "./runtime-Q4G4z83G.mjs";
|
|
3
3
|
import { t as harnessOptions } from "./harness-6d8TlAVk.mjs";
|
|
4
4
|
import { a as register, o as write, r as log } from "./registration-C6yiv31r.mjs";
|
|
5
|
-
import { i as layer$8, t as Service$5 } from "./identity-
|
|
6
|
-
import { a as rename, c as read, i as heartbeat, n as connect, o as status, r as disconnect, s as identity, t as Client } from "./relay-
|
|
5
|
+
import { i as layer$8, t as Service$5 } from "./identity-BDrVSoYv.mjs";
|
|
6
|
+
import { a as rename, c as read, i as heartbeat, n as connect, o as status, r as disconnect, s as identity, t as Client } from "./relay-x3PglJcM.mjs";
|
|
7
7
|
import { Cause, Clock, Config, Context, Data, DateTime, Deferred, Duration, Effect, Exit, Fiber, FileSystem, Layer, Logger, Option, Path, Predicate, PubSub, Queue, Ref, Schedule, Schema, Scope, Semaphore, Stream } from "effect";
|
|
8
8
|
import { Model } from "@codeworksh/aikit";
|
|
9
9
|
import { Headers, HttpRouter, HttpServer, HttpServerRequest, HttpServerResponse } from "effect/http";
|
|
@@ -20,6 +20,7 @@ import { homedir } from "node:os";
|
|
|
20
20
|
import { ChildProcess, ChildProcessSpawner } from "effect/process";
|
|
21
21
|
import * as NetAddress from "effect/net/NetAddress";
|
|
22
22
|
import { createServer } from "node:http";
|
|
23
|
+
import { Actor } from "@codeworksh/schema/actor";
|
|
23
24
|
import { Forwarded } from "@codeworksh/schema/forwarded";
|
|
24
25
|
import { Pairing } from "@codeworksh/schema/pairing";
|
|
25
26
|
import { Current, Current as Current$1, Door as Door$1, Forbidden, Forbidden as Forbidden$1, Unauthorized } from "@codeworksh/schema/host/principal";
|
|
@@ -758,6 +759,11 @@ const routes = HttpRouter.use((router) => Effect.gen(function* () {
|
|
|
758
759
|
}));
|
|
759
760
|
//#endregion
|
|
760
761
|
//#region src/server/principal.ts
|
|
762
|
+
/** Every caller the door admits today (local token or paired device) acts as root on this home. */
|
|
763
|
+
const actorOf = (_principal) => ({
|
|
764
|
+
kind: "root",
|
|
765
|
+
ref: Actor.root
|
|
766
|
+
});
|
|
761
767
|
/** `effect`, for a local caller only; a device is refused. */
|
|
762
768
|
const local = (effect) => Effect.gen(function* () {
|
|
763
769
|
if ((yield* Current).kind !== "local") return yield* new Forbidden({ message: "Only this computer manages its paired devices" });
|
|
@@ -856,7 +862,7 @@ const rpc = Layer.effect(Door$1, Effect.gen(function* () {
|
|
|
856
862
|
const connections = yield* Connections;
|
|
857
863
|
return Door$1.of((effect, { headers }) => {
|
|
858
864
|
const principal = connections.find(Option.getOrUndefined(Headers.get(headers, header)));
|
|
859
|
-
return principal === void 0 ? Effect.fail(new Unauthorized({ message: "This connection is not open to this server" })) : Effect.provideService(
|
|
865
|
+
return principal === void 0 ? Effect.fail(new Unauthorized({ message: "This connection is not open to this server" })) : effect.pipe(Effect.provideService(Current$1, principal), Effect.provideService(Actor.Current, actorOf(principal).ref));
|
|
860
866
|
});
|
|
861
867
|
}));
|
|
862
868
|
//#endregion
|
|
@@ -1913,4 +1919,4 @@ var serve_default = handler(Cmd.commands.serve, Effect.fn("CLI.serve", { level:
|
|
|
1913
1919
|
//#endregion
|
|
1914
1920
|
export { serve_default as default };
|
|
1915
1921
|
|
|
1916
|
-
//# sourceMappingURL=serve-
|
|
1922
|
+
//# sourceMappingURL=serve-C4LsoXFe.mjs.map
|