@zyro-sec/server 1.0.2 → 1.0.4
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/package.json +8 -8
- package/script/build.ts +28 -28
- package/script/pack.ts +5 -5
- package/script/workerd-probe.ts +59 -59
- package/src/api.ts +12 -12
- package/src/auth.ts +70 -70
- package/src/content-headers.ts +32 -32
- package/src/cors.ts +34 -34
- package/src/event-feed.ts +93 -93
- package/src/fetch.ts +67 -67
- package/src/handlers/agent.ts +23 -23
- package/src/handlers/artifact.ts +242 -242
- package/src/handlers/automation.ts +63 -63
- package/src/handlers/blob.ts +55 -55
- package/src/handlers/command.ts +8 -8
- package/src/handlers/config.ts +26 -26
- package/src/handlers/credential.ts +87 -87
- package/src/handlers/debug.ts +57 -57
- package/src/handlers/event.ts +38 -38
- package/src/handlers/form.ts +15 -15
- package/src/handlers/fs.ts +180 -180
- package/src/handlers/generate.ts +117 -117
- package/src/handlers/integration.ts +310 -310
- package/src/handlers/job.ts +29 -29
- package/src/handlers/location.ts +33 -33
- package/src/handlers/machine.ts +336 -336
- package/src/handlers/mcp.ts +160 -160
- package/src/handlers/media.ts +44 -44
- package/src/handlers/message.ts +82 -82
- package/src/handlers/migration.ts +13 -13
- package/src/handlers/model.ts +25 -25
- package/src/handlers/permission.ts +131 -131
- package/src/handlers/persistent-pty.ts +285 -285
- package/src/handlers/plugin.ts +97 -97
- package/src/handlers/project.ts +23 -23
- package/src/handlers/provider.ts +32 -32
- package/src/handlers/pty-socket.ts +47 -47
- package/src/handlers/pty.ts +181 -181
- package/src/handlers/push.ts +67 -67
- package/src/handlers/reference.ts +8 -8
- package/src/handlers/rpc.ts +31 -31
- package/src/handlers/server.ts +58 -58
- package/src/handlers/session-error.ts +47 -47
- package/src/handlers/session.ts +722 -722
- package/src/handlers/shell.ts +72 -72
- package/src/handlers/skill.ts +34 -34
- package/src/handlers/usage.ts +23 -23
- package/src/handlers/vcs.ts +104 -104
- package/src/handlers/voice.ts +50 -50
- package/src/handlers/websearch.ts +50 -50
- package/src/handlers/worktree.ts +60 -60
- package/src/handlers.ts +69 -69
- package/src/location.ts +86 -86
- package/src/media-uri.ts +13 -13
- package/src/middleware/authorization.ts +111 -111
- package/src/middleware/defect-response.ts +39 -39
- package/src/middleware/form-location.ts +44 -44
- package/src/middleware/schema-error.ts +49 -49
- package/src/middleware/session-location.ts +30 -30
- package/src/options.ts +61 -61
- package/src/pairing.ts +36 -36
- package/src/process.ts +274 -274
- package/src/range.ts +16 -16
- package/src/request-site.ts +36 -36
- package/src/request-tracing.ts +9 -9
- package/src/routes.ts +261 -261
- package/src/server-info.ts +41 -41
- package/src/service-status.ts +30 -30
- package/src/workerd.ts +184 -184
- package/test/fixture/mcp-starts.cjs +20 -20
- package/test/fixture/routes.ts +37 -37
- package/test/fixture/server.ts +20 -20
- package/test/fixture/worktree-delegate/index.ts +13 -13
- package/test/fixture/worktree-plugin/index.ts +47 -47
- package/tsconfig.build.json +15 -15
- package/tsconfig.json +11 -11
- package/test/artifact.test.ts +0 -224
- package/test/auth.test.ts +0 -24
- package/test/automation.test.ts +0 -315
- package/test/client-mcp.test.ts +0 -369
- package/test/codemode-api.test.ts +0 -88
- package/test/config.test.ts +0 -104
- package/test/cors.test.ts +0 -24
- package/test/defect-response.test.ts +0 -83
- package/test/event-feed.test.ts +0 -201
- package/test/fetch-option.test.ts +0 -104
- package/test/fetch.test.ts +0 -698
- package/test/flag-channel.test.ts +0 -126
- package/test/fs.test.ts +0 -439
- package/test/generate.test.ts +0 -132
- package/test/handler-policy.test.ts +0 -43
- package/test/heap-dump.test.ts +0 -137
- package/test/integration-policy.test.ts +0 -93
- package/test/job.test.ts +0 -186
- package/test/machine-iroh.test.ts +0 -259
- package/test/machine-link.test.ts +0 -1234
- package/test/machine-routes.test.ts +0 -198
- package/test/media.test.ts +0 -343
- package/test/message.test.ts +0 -105
- package/test/model.test.ts +0 -62
- package/test/options.test.ts +0 -42
- package/test/permission-saved.test.ts +0 -73
- package/test/persistent-pty.test.ts +0 -1007
- package/test/process.test.ts +0 -261
- package/test/provider.test.ts +0 -88
- package/test/pty-socket.property.test.ts +0 -240
- package/test/pty-socket.test.ts +0 -89
- package/test/pty-transport.test.ts +0 -200
- package/test/push-routes.test.ts +0 -157
- package/test/request-site.test.ts +0 -71
- package/test/request-tracing.test.ts +0 -87
- package/test/rpc-handler-errors.test.ts +0 -76
- package/test/schema-error.test.ts +0 -68
- package/test/service-status.test.ts +0 -34
- package/test/session-admit-location.test.ts +0 -92
- package/test/session-create.test.ts +0 -65
- package/test/session-diff.test.ts +0 -100
- package/test/session-form.test.ts +0 -50
- package/test/session-import.test.ts +0 -152
- package/test/session-instances.test.ts +0 -335
- package/test/session-unarchive.test.ts +0 -73
- package/test/session-update.test.ts +0 -66
- package/test/skill.test.ts +0 -57
- package/test/sse-failure.test.ts +0 -71
- package/test/usage.test.ts +0 -88
- package/test/vcs.test.ts +0 -356
- package/test/video-generate.test.ts +0 -131
- package/test/voice.test.ts +0 -170
- package/test/workerd.test.ts +0 -227
- package/test/worktree.test.ts +0 -333
package/src/handlers/plugin.ts
CHANGED
|
@@ -1,97 +1,97 @@
|
|
|
1
|
-
import { Plugin } from "@zyro-sec/core/plugin"
|
|
2
|
-
import { PluginUpdate } from "@zyro-sec/core/plugin/update"
|
|
3
|
-
import { InvalidRequestError, PluginNotFoundError, ServiceUnavailableError } from "@zyro-sec/protocol/errors"
|
|
4
|
-
import { Cause, Effect, Exit } from "effect"
|
|
5
|
-
import { HttpApiBuilder } from "effect/http-api"
|
|
6
|
-
import { Api } from "../api"
|
|
7
|
-
import { response } from "../location"
|
|
8
|
-
|
|
9
|
-
export const PluginHandler = HttpApiBuilder.group(Api, "server.plugin", (handlers) =>
|
|
10
|
-
handlers
|
|
11
|
-
.handle("plugin.list", () =>
|
|
12
|
-
Effect.gen(function* () {
|
|
13
|
-
return yield* response(Plugin.Service.use((plugin) => plugin.list()))
|
|
14
|
-
}),
|
|
15
|
-
)
|
|
16
|
-
.handle("plugin.gui", (ctx) =>
|
|
17
|
-
Effect.gen(function* () {
|
|
18
|
-
const plugins = yield* Plugin.Service
|
|
19
|
-
const bundle = yield* plugins.gui(ctx.query.id)
|
|
20
|
-
if (!bundle)
|
|
21
|
-
return yield* new PluginNotFoundError({
|
|
22
|
-
pluginID: ctx.query.id,
|
|
23
|
-
message: `No active plugin with a ./gui-experimental bundle: ${ctx.query.id}`,
|
|
24
|
-
})
|
|
25
|
-
return yield* response(Effect.succeed(bundle))
|
|
26
|
-
}),
|
|
27
|
-
)
|
|
28
|
-
.handle("plugin.check", (ctx) =>
|
|
29
|
-
Effect.gen(function* () {
|
|
30
|
-
const plugins = yield* Plugin.Service
|
|
31
|
-
const inventory = yield* plugins.list()
|
|
32
|
-
const targets = [
|
|
33
|
-
...new Set(inventory.flatMap((plugin) => (plugin.source.type === "package" ? [plugin.source.target] : []))),
|
|
34
|
-
].filter((target) => ctx.payload.target === undefined || target === ctx.payload.target)
|
|
35
|
-
if (ctx.payload.target !== undefined && !targets.length)
|
|
36
|
-
return yield* new InvalidRequestError({
|
|
37
|
-
message: `Plugin package is not in the current server inventory: ${ctx.payload.target}`,
|
|
38
|
-
field: "target",
|
|
39
|
-
})
|
|
40
|
-
const updates = yield* PluginUpdate.Service
|
|
41
|
-
const outdated = new Map(
|
|
42
|
-
yield* Effect.forEach(
|
|
43
|
-
targets,
|
|
44
|
-
(target) => updates.check(target, { refresh: true }).pipe(Effect.map((value) => [target, value] as const)),
|
|
45
|
-
{ concurrency: "unbounded" },
|
|
46
|
-
),
|
|
47
|
-
)
|
|
48
|
-
return yield* response(
|
|
49
|
-
Effect.succeed(
|
|
50
|
-
inventory.map((plugin) => {
|
|
51
|
-
if (plugin.source.type !== "package" || !outdated.has(plugin.source.target)) return plugin
|
|
52
|
-
return {
|
|
53
|
-
...plugin,
|
|
54
|
-
source: {
|
|
55
|
-
type: "package" as const,
|
|
56
|
-
target: plugin.source.target,
|
|
57
|
-
...(plugin.source.version ? { version: plugin.source.version } : {}),
|
|
58
|
-
...(outdated.get(plugin.source.target) ? { outdated: true as const } : {}),
|
|
59
|
-
...(plugin.source.updating ? { updating: true as const } : {}),
|
|
60
|
-
},
|
|
61
|
-
}
|
|
62
|
-
}),
|
|
63
|
-
),
|
|
64
|
-
)
|
|
65
|
-
}),
|
|
66
|
-
)
|
|
67
|
-
.handle("plugin.update", (ctx) =>
|
|
68
|
-
Effect.gen(function* () {
|
|
69
|
-
const plugins = yield* Plugin.Service
|
|
70
|
-
const inventory = new Set(
|
|
71
|
-
(yield* plugins.list()).flatMap((plugin) => (plugin.source.type === "package" ? [plugin.source.target] : [])),
|
|
72
|
-
)
|
|
73
|
-
const unknown = ctx.payload.targets.filter((target) => !inventory.has(target))
|
|
74
|
-
if (unknown.length)
|
|
75
|
-
return yield* new InvalidRequestError({
|
|
76
|
-
message: `Plugin packages are not in the current server inventory: ${unknown.join(", ")}`,
|
|
77
|
-
field: "targets",
|
|
78
|
-
})
|
|
79
|
-
const updates = yield* PluginUpdate.Service
|
|
80
|
-
// Let every update run to completion instead of interrupting the rest on the first failure.
|
|
81
|
-
const failures = yield* Effect.forEach(
|
|
82
|
-
ctx.payload.targets,
|
|
83
|
-
(target) =>
|
|
84
|
-
updates.update(target).pipe(
|
|
85
|
-
Effect.exit,
|
|
86
|
-
Effect.map((exit) => (Exit.isFailure(exit) ? [`${target}: ${Cause.pretty(exit.cause)}`] : [])),
|
|
87
|
-
),
|
|
88
|
-
{ concurrency: "unbounded" },
|
|
89
|
-
).pipe(Effect.map((results) => results.flat()))
|
|
90
|
-
if (failures.length)
|
|
91
|
-
return yield* new ServiceUnavailableError({
|
|
92
|
-
message: `Failed to update plugin packages: ${failures.join("; ")}`,
|
|
93
|
-
service: "plugin",
|
|
94
|
-
})
|
|
95
|
-
}),
|
|
96
|
-
),
|
|
97
|
-
)
|
|
1
|
+
import { Plugin } from "@zyro-sec/core/plugin"
|
|
2
|
+
import { PluginUpdate } from "@zyro-sec/core/plugin/update"
|
|
3
|
+
import { InvalidRequestError, PluginNotFoundError, ServiceUnavailableError } from "@zyro-sec/protocol/errors"
|
|
4
|
+
import { Cause, Effect, Exit } from "effect"
|
|
5
|
+
import { HttpApiBuilder } from "effect/http-api"
|
|
6
|
+
import { Api } from "../api"
|
|
7
|
+
import { response } from "../location"
|
|
8
|
+
|
|
9
|
+
export const PluginHandler = HttpApiBuilder.group(Api, "server.plugin", (handlers) =>
|
|
10
|
+
handlers
|
|
11
|
+
.handle("plugin.list", () =>
|
|
12
|
+
Effect.gen(function* () {
|
|
13
|
+
return yield* response(Plugin.Service.use((plugin) => plugin.list()))
|
|
14
|
+
}),
|
|
15
|
+
)
|
|
16
|
+
.handle("plugin.gui", (ctx) =>
|
|
17
|
+
Effect.gen(function* () {
|
|
18
|
+
const plugins = yield* Plugin.Service
|
|
19
|
+
const bundle = yield* plugins.gui(ctx.query.id)
|
|
20
|
+
if (!bundle)
|
|
21
|
+
return yield* new PluginNotFoundError({
|
|
22
|
+
pluginID: ctx.query.id,
|
|
23
|
+
message: `No active plugin with a ./gui-experimental bundle: ${ctx.query.id}`,
|
|
24
|
+
})
|
|
25
|
+
return yield* response(Effect.succeed(bundle))
|
|
26
|
+
}),
|
|
27
|
+
)
|
|
28
|
+
.handle("plugin.check", (ctx) =>
|
|
29
|
+
Effect.gen(function* () {
|
|
30
|
+
const plugins = yield* Plugin.Service
|
|
31
|
+
const inventory = yield* plugins.list()
|
|
32
|
+
const targets = [
|
|
33
|
+
...new Set(inventory.flatMap((plugin) => (plugin.source.type === "package" ? [plugin.source.target] : []))),
|
|
34
|
+
].filter((target) => ctx.payload.target === undefined || target === ctx.payload.target)
|
|
35
|
+
if (ctx.payload.target !== undefined && !targets.length)
|
|
36
|
+
return yield* new InvalidRequestError({
|
|
37
|
+
message: `Plugin package is not in the current server inventory: ${ctx.payload.target}`,
|
|
38
|
+
field: "target",
|
|
39
|
+
})
|
|
40
|
+
const updates = yield* PluginUpdate.Service
|
|
41
|
+
const outdated = new Map(
|
|
42
|
+
yield* Effect.forEach(
|
|
43
|
+
targets,
|
|
44
|
+
(target) => updates.check(target, { refresh: true }).pipe(Effect.map((value) => [target, value] as const)),
|
|
45
|
+
{ concurrency: "unbounded" },
|
|
46
|
+
),
|
|
47
|
+
)
|
|
48
|
+
return yield* response(
|
|
49
|
+
Effect.succeed(
|
|
50
|
+
inventory.map((plugin) => {
|
|
51
|
+
if (plugin.source.type !== "package" || !outdated.has(plugin.source.target)) return plugin
|
|
52
|
+
return {
|
|
53
|
+
...plugin,
|
|
54
|
+
source: {
|
|
55
|
+
type: "package" as const,
|
|
56
|
+
target: plugin.source.target,
|
|
57
|
+
...(plugin.source.version ? { version: plugin.source.version } : {}),
|
|
58
|
+
...(outdated.get(plugin.source.target) ? { outdated: true as const } : {}),
|
|
59
|
+
...(plugin.source.updating ? { updating: true as const } : {}),
|
|
60
|
+
},
|
|
61
|
+
}
|
|
62
|
+
}),
|
|
63
|
+
),
|
|
64
|
+
)
|
|
65
|
+
}),
|
|
66
|
+
)
|
|
67
|
+
.handle("plugin.update", (ctx) =>
|
|
68
|
+
Effect.gen(function* () {
|
|
69
|
+
const plugins = yield* Plugin.Service
|
|
70
|
+
const inventory = new Set(
|
|
71
|
+
(yield* plugins.list()).flatMap((plugin) => (plugin.source.type === "package" ? [plugin.source.target] : [])),
|
|
72
|
+
)
|
|
73
|
+
const unknown = ctx.payload.targets.filter((target) => !inventory.has(target))
|
|
74
|
+
if (unknown.length)
|
|
75
|
+
return yield* new InvalidRequestError({
|
|
76
|
+
message: `Plugin packages are not in the current server inventory: ${unknown.join(", ")}`,
|
|
77
|
+
field: "targets",
|
|
78
|
+
})
|
|
79
|
+
const updates = yield* PluginUpdate.Service
|
|
80
|
+
// Let every update run to completion instead of interrupting the rest on the first failure.
|
|
81
|
+
const failures = yield* Effect.forEach(
|
|
82
|
+
ctx.payload.targets,
|
|
83
|
+
(target) =>
|
|
84
|
+
updates.update(target).pipe(
|
|
85
|
+
Effect.exit,
|
|
86
|
+
Effect.map((exit) => (Exit.isFailure(exit) ? [`${target}: ${Cause.pretty(exit.cause)}`] : [])),
|
|
87
|
+
),
|
|
88
|
+
{ concurrency: "unbounded" },
|
|
89
|
+
).pipe(Effect.map((results) => results.flat()))
|
|
90
|
+
if (failures.length)
|
|
91
|
+
return yield* new ServiceUnavailableError({
|
|
92
|
+
message: `Failed to update plugin packages: ${failures.join("; ")}`,
|
|
93
|
+
service: "plugin",
|
|
94
|
+
})
|
|
95
|
+
}),
|
|
96
|
+
),
|
|
97
|
+
)
|
package/src/handlers/project.ts
CHANGED
|
@@ -1,23 +1,23 @@
|
|
|
1
|
-
import { Project } from "@zyro-sec/core/project"
|
|
2
|
-
import { Effect } from "effect"
|
|
3
|
-
import { HttpApiBuilder } from "effect/http-api"
|
|
4
|
-
import { Api } from "../api"
|
|
5
|
-
import { ProjectNotFoundError } from "@zyro-sec/protocol/errors"
|
|
6
|
-
|
|
7
|
-
export const ProjectHandler = HttpApiBuilder.group(Api, "server.project", (handlers) =>
|
|
8
|
-
handlers
|
|
9
|
-
.handle("project.list", (ctx) => Project.Service.use((project) => project.list(ctx.query)))
|
|
10
|
-
.handle("project.update", (ctx) =>
|
|
11
|
-
Project.Service.use((project) =>
|
|
12
|
-
project.update({ ...ctx.payload, projectID: ctx.params.projectID }).pipe(
|
|
13
|
-
Effect.mapError(
|
|
14
|
-
() =>
|
|
15
|
-
new ProjectNotFoundError({
|
|
16
|
-
projectID: ctx.params.projectID,
|
|
17
|
-
message: `Project not found: ${ctx.params.projectID}`,
|
|
18
|
-
}),
|
|
19
|
-
),
|
|
20
|
-
),
|
|
21
|
-
),
|
|
22
|
-
),
|
|
23
|
-
)
|
|
1
|
+
import { Project } from "@zyro-sec/core/project"
|
|
2
|
+
import { Effect } from "effect"
|
|
3
|
+
import { HttpApiBuilder } from "effect/http-api"
|
|
4
|
+
import { Api } from "../api"
|
|
5
|
+
import { ProjectNotFoundError } from "@zyro-sec/protocol/errors"
|
|
6
|
+
|
|
7
|
+
export const ProjectHandler = HttpApiBuilder.group(Api, "server.project", (handlers) =>
|
|
8
|
+
handlers
|
|
9
|
+
.handle("project.list", (ctx) => Project.Service.use((project) => project.list(ctx.query)))
|
|
10
|
+
.handle("project.update", (ctx) =>
|
|
11
|
+
Project.Service.use((project) =>
|
|
12
|
+
project.update({ ...ctx.payload, projectID: ctx.params.projectID }).pipe(
|
|
13
|
+
Effect.mapError(
|
|
14
|
+
() =>
|
|
15
|
+
new ProjectNotFoundError({
|
|
16
|
+
projectID: ctx.params.projectID,
|
|
17
|
+
message: `Project not found: ${ctx.params.projectID}`,
|
|
18
|
+
}),
|
|
19
|
+
),
|
|
20
|
+
),
|
|
21
|
+
),
|
|
22
|
+
),
|
|
23
|
+
)
|
package/src/handlers/provider.ts
CHANGED
|
@@ -1,32 +1,32 @@
|
|
|
1
|
-
import { Provider } from "@zyro-sec/core/provider"
|
|
2
|
-
import { ProviderNotFoundError } from "@zyro-sec/protocol/errors"
|
|
3
|
-
import { Effect } from "effect"
|
|
4
|
-
import { HttpApiBuilder } from "effect/http-api"
|
|
5
|
-
import { Api } from "../api"
|
|
6
|
-
import { response } from "../location"
|
|
7
|
-
|
|
8
|
-
export const ProviderHandler = HttpApiBuilder.group(Api, "server.provider", (handlers) =>
|
|
9
|
-
Effect.gen(function* () {
|
|
10
|
-
return handlers
|
|
11
|
-
.handle(
|
|
12
|
-
"provider.list",
|
|
13
|
-
Effect.fn(function* () {
|
|
14
|
-
const providers = yield* Provider.Service
|
|
15
|
-
return yield* response(providers.available())
|
|
16
|
-
}),
|
|
17
|
-
)
|
|
18
|
-
.handle(
|
|
19
|
-
"provider.get",
|
|
20
|
-
Effect.fn(function* (ctx) {
|
|
21
|
-
const providers = yield* Provider.Service
|
|
22
|
-
const provider = yield* providers.get(ctx.params.providerID)
|
|
23
|
-
if (!provider)
|
|
24
|
-
return yield* new ProviderNotFoundError({
|
|
25
|
-
providerID: ctx.params.providerID,
|
|
26
|
-
message: `Provider not found: ${ctx.params.providerID}`,
|
|
27
|
-
})
|
|
28
|
-
return yield* response(Effect.succeed(provider))
|
|
29
|
-
}),
|
|
30
|
-
)
|
|
31
|
-
}),
|
|
32
|
-
)
|
|
1
|
+
import { Provider } from "@zyro-sec/core/provider"
|
|
2
|
+
import { ProviderNotFoundError } from "@zyro-sec/protocol/errors"
|
|
3
|
+
import { Effect } from "effect"
|
|
4
|
+
import { HttpApiBuilder } from "effect/http-api"
|
|
5
|
+
import { Api } from "../api"
|
|
6
|
+
import { response } from "../location"
|
|
7
|
+
|
|
8
|
+
export const ProviderHandler = HttpApiBuilder.group(Api, "server.provider", (handlers) =>
|
|
9
|
+
Effect.gen(function* () {
|
|
10
|
+
return handlers
|
|
11
|
+
.handle(
|
|
12
|
+
"provider.list",
|
|
13
|
+
Effect.fn(function* () {
|
|
14
|
+
const providers = yield* Provider.Service
|
|
15
|
+
return yield* response(providers.available())
|
|
16
|
+
}),
|
|
17
|
+
)
|
|
18
|
+
.handle(
|
|
19
|
+
"provider.get",
|
|
20
|
+
Effect.fn(function* (ctx) {
|
|
21
|
+
const providers = yield* Provider.Service
|
|
22
|
+
const provider = yield* providers.get(ctx.params.providerID)
|
|
23
|
+
if (!provider)
|
|
24
|
+
return yield* new ProviderNotFoundError({
|
|
25
|
+
providerID: ctx.params.providerID,
|
|
26
|
+
message: `Provider not found: ${ctx.params.providerID}`,
|
|
27
|
+
})
|
|
28
|
+
return yield* response(Effect.succeed(provider))
|
|
29
|
+
}),
|
|
30
|
+
)
|
|
31
|
+
}),
|
|
32
|
+
)
|
|
@@ -1,47 +1,47 @@
|
|
|
1
|
-
import { LocationLifecycle } from "@zyro-sec/core/location-lifecycle"
|
|
2
|
-
import { Effect, Queue } from "effect"
|
|
3
|
-
import { Socket } from "effect/socket"
|
|
4
|
-
|
|
5
|
-
export type Outbound = string | Uint8Array | Socket.CloseEvent
|
|
6
|
-
|
|
7
|
-
// Outbound frames flow through one queue drained by a single writer so replay, live output, and the close
|
|
8
|
-
// frame keep their order. Either side closing ends the connection normally, and every exit detaches.
|
|
9
|
-
export function runPtySocket<R>(input: {
|
|
10
|
-
readonly socket: Socket.Socket
|
|
11
|
-
readonly outbox: Queue.Dequeue<Outbound>
|
|
12
|
-
readonly onOpen?: Effect.Effect<void, never, R>
|
|
13
|
-
readonly onMessage: (message: string | Uint8Array) => Effect.Effect<void, never, R>
|
|
14
|
-
readonly detach: () => void
|
|
15
|
-
}) {
|
|
16
|
-
const drain = Effect.gen(function* () {
|
|
17
|
-
const writer = yield* input.socket.writer
|
|
18
|
-
while (true) {
|
|
19
|
-
const item = yield* Queue.take(input.outbox)
|
|
20
|
-
yield* writer.write(item)
|
|
21
|
-
if (item instanceof Socket.CloseEvent) return
|
|
22
|
-
}
|
|
23
|
-
})
|
|
24
|
-
const read = Effect.gen(function* () {
|
|
25
|
-
const reader = yield* input.socket.reader
|
|
26
|
-
if (input.onOpen) yield* input.onOpen
|
|
27
|
-
while (true) yield* Effect.forEach(yield* reader.pull, input.onMessage, { discard: true })
|
|
28
|
-
})
|
|
29
|
-
return Effect.raceFirst(drain, read).pipe(
|
|
30
|
-
Effect.ensuring(Effect.sync(input.detach)),
|
|
31
|
-
Effect.catchReason("SocketError", "SocketCloseError", () => Effect.void),
|
|
32
|
-
Effect.orDie,
|
|
33
|
-
)
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
// A socket borrows its Location for as long as it stays open. Its client reconnects right away when the socket
|
|
37
|
-
// closes, so it keeps the idle sweep from evicting the Location (eviction would only boot a fresh one). Any other
|
|
38
|
-
// shutdown (reload, VCS init, debug eviction) closes the socket with 1012, which releases the borrow so the
|
|
39
|
-
// Location's scope can close, and the client reconnects to the replacement.
|
|
40
|
-
export const holdLocation = Effect.fnUntraced(function* (outbox: Queue.Enqueue<Outbound>) {
|
|
41
|
-
const lifecycle = yield* LocationLifecycle.Service
|
|
42
|
-
yield* lifecycle.retain
|
|
43
|
-
yield* lifecycle.awaitShutdown.pipe(
|
|
44
|
-
Effect.andThen(Queue.offer(outbox, new Socket.CloseEvent(1012, "location shut down"))),
|
|
45
|
-
Effect.forkChild,
|
|
46
|
-
)
|
|
47
|
-
})
|
|
1
|
+
import { LocationLifecycle } from "@zyro-sec/core/location-lifecycle"
|
|
2
|
+
import { Effect, Queue } from "effect"
|
|
3
|
+
import { Socket } from "effect/socket"
|
|
4
|
+
|
|
5
|
+
export type Outbound = string | Uint8Array | Socket.CloseEvent
|
|
6
|
+
|
|
7
|
+
// Outbound frames flow through one queue drained by a single writer so replay, live output, and the close
|
|
8
|
+
// frame keep their order. Either side closing ends the connection normally, and every exit detaches.
|
|
9
|
+
export function runPtySocket<R>(input: {
|
|
10
|
+
readonly socket: Socket.Socket
|
|
11
|
+
readonly outbox: Queue.Dequeue<Outbound>
|
|
12
|
+
readonly onOpen?: Effect.Effect<void, never, R>
|
|
13
|
+
readonly onMessage: (message: string | Uint8Array) => Effect.Effect<void, never, R>
|
|
14
|
+
readonly detach: () => void
|
|
15
|
+
}) {
|
|
16
|
+
const drain = Effect.gen(function* () {
|
|
17
|
+
const writer = yield* input.socket.writer
|
|
18
|
+
while (true) {
|
|
19
|
+
const item = yield* Queue.take(input.outbox)
|
|
20
|
+
yield* writer.write(item)
|
|
21
|
+
if (item instanceof Socket.CloseEvent) return
|
|
22
|
+
}
|
|
23
|
+
})
|
|
24
|
+
const read = Effect.gen(function* () {
|
|
25
|
+
const reader = yield* input.socket.reader
|
|
26
|
+
if (input.onOpen) yield* input.onOpen
|
|
27
|
+
while (true) yield* Effect.forEach(yield* reader.pull, input.onMessage, { discard: true })
|
|
28
|
+
})
|
|
29
|
+
return Effect.raceFirst(drain, read).pipe(
|
|
30
|
+
Effect.ensuring(Effect.sync(input.detach)),
|
|
31
|
+
Effect.catchReason("SocketError", "SocketCloseError", () => Effect.void),
|
|
32
|
+
Effect.orDie,
|
|
33
|
+
)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// A socket borrows its Location for as long as it stays open. Its client reconnects right away when the socket
|
|
37
|
+
// closes, so it keeps the idle sweep from evicting the Location (eviction would only boot a fresh one). Any other
|
|
38
|
+
// shutdown (reload, VCS init, debug eviction) closes the socket with 1012, which releases the borrow so the
|
|
39
|
+
// Location's scope can close, and the client reconnects to the replacement.
|
|
40
|
+
export const holdLocation = Effect.fnUntraced(function* (outbox: Queue.Enqueue<Outbound>) {
|
|
41
|
+
const lifecycle = yield* LocationLifecycle.Service
|
|
42
|
+
yield* lifecycle.retain
|
|
43
|
+
yield* lifecycle.awaitShutdown.pipe(
|
|
44
|
+
Effect.andThen(Queue.offer(outbox, new Socket.CloseEvent(1012, "location shut down"))),
|
|
45
|
+
Effect.forkChild,
|
|
46
|
+
)
|
|
47
|
+
})
|