@zyro-sec/server 1.0.2 → 1.0.3
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/mcp.ts
CHANGED
|
@@ -1,160 +1,160 @@
|
|
|
1
|
-
import { Location } from "@zyro-sec/core/location"
|
|
2
|
-
import { LocationServiceMap } from "@zyro-sec/core/location-service-map"
|
|
3
|
-
import { Mcp } from "@zyro-sec/core/mcp/index"
|
|
4
|
-
import { PtyTicket } from "@zyro-sec/core/pty/ticket"
|
|
5
|
-
import { ForbiddenError, McpServerNotFoundError } from "@zyro-sec/protocol/errors"
|
|
6
|
-
import { PTY_CONNECT_TOKEN_HEADER, PTY_CONNECT_TOKEN_HEADER_VALUE } from "@zyro-sec/protocol/groups/pty"
|
|
7
|
-
import { Effect, Queue } from "effect"
|
|
8
|
-
import { HttpServerResponse } from "effect/http"
|
|
9
|
-
import { HttpApiBuilder, HttpApiSchema } from "effect/http-api"
|
|
10
|
-
import { Socket } from "effect/socket"
|
|
11
|
-
import { Api } from "../api"
|
|
12
|
-
import { CorsConfig, isAllowedRequestOrigin } from "../cors"
|
|
13
|
-
import { locationErrors, requestRef, response } from "../location"
|
|
14
|
-
import { holdLocation, type Outbound, runPtySocket } from "./pty-socket"
|
|
15
|
-
|
|
16
|
-
const notFound = <A, R>(effect: Effect.Effect<A, Mcp.NotFoundError, R>) =>
|
|
17
|
-
effect.pipe(Effect.mapError((error) => new McpServerNotFoundError({ server: error.server, message: error.message })))
|
|
18
|
-
|
|
19
|
-
export const McpHandler = HttpApiBuilder.group(Api, "server.mcp", (handlers) =>
|
|
20
|
-
Effect.gen(function* () {
|
|
21
|
-
const locations = yield* LocationServiceMap.Service
|
|
22
|
-
const cors = yield* CorsConfig
|
|
23
|
-
const tickets = yield* PtyTicket.Service
|
|
24
|
-
|
|
25
|
-
return handlers
|
|
26
|
-
.handle(
|
|
27
|
-
"mcp.list",
|
|
28
|
-
Effect.fn(function* () {
|
|
29
|
-
const service = yield* Mcp.Service
|
|
30
|
-
return yield* response(service.servers())
|
|
31
|
-
}),
|
|
32
|
-
)
|
|
33
|
-
.handle(
|
|
34
|
-
"mcp.add",
|
|
35
|
-
Effect.fn(function* (ctx) {
|
|
36
|
-
const service = yield* Mcp.Service
|
|
37
|
-
yield* service.add(ctx.params.server, ctx.payload.config)
|
|
38
|
-
return HttpApiSchema.NoContent.make()
|
|
39
|
-
}),
|
|
40
|
-
)
|
|
41
|
-
.handle(
|
|
42
|
-
"mcp.remove",
|
|
43
|
-
Effect.fn(function* (ctx) {
|
|
44
|
-
const service = yield* Mcp.Service
|
|
45
|
-
yield* notFound(service.remove(ctx.params.server))
|
|
46
|
-
return HttpApiSchema.NoContent.make()
|
|
47
|
-
}),
|
|
48
|
-
)
|
|
49
|
-
.handle(
|
|
50
|
-
"mcp.connect",
|
|
51
|
-
Effect.fn(function* (ctx) {
|
|
52
|
-
const service = yield* Mcp.Service
|
|
53
|
-
yield* notFound(service.connect(ctx.params.server))
|
|
54
|
-
return HttpApiSchema.NoContent.make()
|
|
55
|
-
}),
|
|
56
|
-
)
|
|
57
|
-
.handle(
|
|
58
|
-
"mcp.disconnect",
|
|
59
|
-
Effect.fn(function* (ctx) {
|
|
60
|
-
const service = yield* Mcp.Service
|
|
61
|
-
yield* notFound(service.disconnect(ctx.params.server))
|
|
62
|
-
return HttpApiSchema.NoContent.make()
|
|
63
|
-
}),
|
|
64
|
-
)
|
|
65
|
-
.handle(
|
|
66
|
-
"mcp.resource.catalog",
|
|
67
|
-
Effect.fn(function* () {
|
|
68
|
-
const service = yield* Mcp.Service
|
|
69
|
-
return yield* response(service.resourceCatalog())
|
|
70
|
-
}),
|
|
71
|
-
)
|
|
72
|
-
.handle(
|
|
73
|
-
"mcp.clientToken",
|
|
74
|
-
Effect.fn(function* (ctx) {
|
|
75
|
-
// The custom header forces a CORS preflight, so cross-origin pages cannot mint tickets.
|
|
76
|
-
if (
|
|
77
|
-
ctx.headers[PTY_CONNECT_TOKEN_HEADER] !== PTY_CONNECT_TOKEN_HEADER_VALUE ||
|
|
78
|
-
!isAllowedRequestOrigin(ctx.request.headers.origin, ctx.request.headers.host, cors)
|
|
79
|
-
)
|
|
80
|
-
return yield* new ForbiddenError({ message: "Invalid client MCP connect token request" })
|
|
81
|
-
const location = yield* Location.Service
|
|
82
|
-
return yield* response(
|
|
83
|
-
tickets.issue({
|
|
84
|
-
mcpClient: ctx.query.name,
|
|
85
|
-
directory: location.directory,
|
|
86
|
-
workspaceID: location.workspaceID,
|
|
87
|
-
}),
|
|
88
|
-
)
|
|
89
|
-
}),
|
|
90
|
-
)
|
|
91
|
-
.handleRaw(
|
|
92
|
-
"mcp.client",
|
|
93
|
-
Effect.fn("McpHandler.client")(function* (ctx) {
|
|
94
|
-
if (!isAllowedRequestOrigin(ctx.request.headers.origin, ctx.request.headers.host, cors))
|
|
95
|
-
return HttpServerResponse.empty({ status: 403 })
|
|
96
|
-
|
|
97
|
-
const name = ctx.query.name
|
|
98
|
-
const ref = LocationServiceMap.canonical(requestRef(ctx.request))
|
|
99
|
-
if (
|
|
100
|
-
ctx.query.ticket &&
|
|
101
|
-
!(yield* tickets.consume({
|
|
102
|
-
ticket: ctx.query.ticket,
|
|
103
|
-
mcpClient: name,
|
|
104
|
-
directory: ref.directory,
|
|
105
|
-
workspaceID: ref.workspaceID,
|
|
106
|
-
}))
|
|
107
|
-
)
|
|
108
|
-
return HttpServerResponse.empty({ status: 403 })
|
|
109
|
-
|
|
110
|
-
return yield* Effect.gen(function* () {
|
|
111
|
-
const mcp = yield* Mcp.Service
|
|
112
|
-
const { McpSocketClient } = yield* Effect.promise(() => import("@zyro-sec/core/mcp/socket-client"))
|
|
113
|
-
const socket = yield* Effect.orDie(ctx.request.upgrade)
|
|
114
|
-
const outbox = yield* Queue.unbounded<Outbound>()
|
|
115
|
-
yield* holdLocation(outbox)
|
|
116
|
-
const state = { attached: false, disconnected: false }
|
|
117
|
-
const transport = McpSocketClient.make({
|
|
118
|
-
send: (text) => Queue.offerUnsafe(outbox, text),
|
|
119
|
-
// A failed attach closes the transport before reporting the error; ignore that close so the
|
|
120
|
-
// socket closes with 1011 below instead of a normal 1000.
|
|
121
|
-
close: (code, reason) => {
|
|
122
|
-
if (state.attached) Queue.offerUnsafe(outbox, new Socket.CloseEvent(code, reason))
|
|
123
|
-
},
|
|
124
|
-
})
|
|
125
|
-
const decoder = new TextDecoder()
|
|
126
|
-
// The attachment lives in this scope, so it ends with the socket.
|
|
127
|
-
yield* mcp.attach(name, transport).pipe(
|
|
128
|
-
Effect.tap(() =>
|
|
129
|
-
Effect.sync(() => {
|
|
130
|
-
state.attached = true
|
|
131
|
-
}),
|
|
132
|
-
),
|
|
133
|
-
Effect.catch((error) =>
|
|
134
|
-
state.disconnected
|
|
135
|
-
? Effect.logInfo("client MCP disconnected during initialize", { name, error: error.message })
|
|
136
|
-
: Effect.logError("client MCP attach failed", { name, error: error.message }).pipe(
|
|
137
|
-
Effect.andThen(
|
|
138
|
-
Effect.sync(() => Queue.offerUnsafe(outbox, new Socket.CloseEvent(1011, "attach failed"))),
|
|
139
|
-
),
|
|
140
|
-
),
|
|
141
|
-
),
|
|
142
|
-
Effect.forkScoped,
|
|
143
|
-
)
|
|
144
|
-
yield* runPtySocket({
|
|
145
|
-
socket,
|
|
146
|
-
outbox,
|
|
147
|
-
// Frames should be text, but binary frames are accepted and decoded as UTF-8.
|
|
148
|
-
onMessage: (message) =>
|
|
149
|
-
Effect.sync(() => transport.deliver(typeof message === "string" ? message : decoder.decode(message))),
|
|
150
|
-
detach: () => {
|
|
151
|
-
state.disconnected = true
|
|
152
|
-
transport.ended()
|
|
153
|
-
},
|
|
154
|
-
})
|
|
155
|
-
return HttpServerResponse.empty()
|
|
156
|
-
}).pipe(Effect.scoped, Effect.provide(locations.get(ref)), locationErrors)
|
|
157
|
-
}),
|
|
158
|
-
)
|
|
159
|
-
}),
|
|
160
|
-
)
|
|
1
|
+
import { Location } from "@zyro-sec/core/location"
|
|
2
|
+
import { LocationServiceMap } from "@zyro-sec/core/location-service-map"
|
|
3
|
+
import { Mcp } from "@zyro-sec/core/mcp/index"
|
|
4
|
+
import { PtyTicket } from "@zyro-sec/core/pty/ticket"
|
|
5
|
+
import { ForbiddenError, McpServerNotFoundError } from "@zyro-sec/protocol/errors"
|
|
6
|
+
import { PTY_CONNECT_TOKEN_HEADER, PTY_CONNECT_TOKEN_HEADER_VALUE } from "@zyro-sec/protocol/groups/pty"
|
|
7
|
+
import { Effect, Queue } from "effect"
|
|
8
|
+
import { HttpServerResponse } from "effect/http"
|
|
9
|
+
import { HttpApiBuilder, HttpApiSchema } from "effect/http-api"
|
|
10
|
+
import { Socket } from "effect/socket"
|
|
11
|
+
import { Api } from "../api"
|
|
12
|
+
import { CorsConfig, isAllowedRequestOrigin } from "../cors"
|
|
13
|
+
import { locationErrors, requestRef, response } from "../location"
|
|
14
|
+
import { holdLocation, type Outbound, runPtySocket } from "./pty-socket"
|
|
15
|
+
|
|
16
|
+
const notFound = <A, R>(effect: Effect.Effect<A, Mcp.NotFoundError, R>) =>
|
|
17
|
+
effect.pipe(Effect.mapError((error) => new McpServerNotFoundError({ server: error.server, message: error.message })))
|
|
18
|
+
|
|
19
|
+
export const McpHandler = HttpApiBuilder.group(Api, "server.mcp", (handlers) =>
|
|
20
|
+
Effect.gen(function* () {
|
|
21
|
+
const locations = yield* LocationServiceMap.Service
|
|
22
|
+
const cors = yield* CorsConfig
|
|
23
|
+
const tickets = yield* PtyTicket.Service
|
|
24
|
+
|
|
25
|
+
return handlers
|
|
26
|
+
.handle(
|
|
27
|
+
"mcp.list",
|
|
28
|
+
Effect.fn(function* () {
|
|
29
|
+
const service = yield* Mcp.Service
|
|
30
|
+
return yield* response(service.servers())
|
|
31
|
+
}),
|
|
32
|
+
)
|
|
33
|
+
.handle(
|
|
34
|
+
"mcp.add",
|
|
35
|
+
Effect.fn(function* (ctx) {
|
|
36
|
+
const service = yield* Mcp.Service
|
|
37
|
+
yield* service.add(ctx.params.server, ctx.payload.config)
|
|
38
|
+
return HttpApiSchema.NoContent.make()
|
|
39
|
+
}),
|
|
40
|
+
)
|
|
41
|
+
.handle(
|
|
42
|
+
"mcp.remove",
|
|
43
|
+
Effect.fn(function* (ctx) {
|
|
44
|
+
const service = yield* Mcp.Service
|
|
45
|
+
yield* notFound(service.remove(ctx.params.server))
|
|
46
|
+
return HttpApiSchema.NoContent.make()
|
|
47
|
+
}),
|
|
48
|
+
)
|
|
49
|
+
.handle(
|
|
50
|
+
"mcp.connect",
|
|
51
|
+
Effect.fn(function* (ctx) {
|
|
52
|
+
const service = yield* Mcp.Service
|
|
53
|
+
yield* notFound(service.connect(ctx.params.server))
|
|
54
|
+
return HttpApiSchema.NoContent.make()
|
|
55
|
+
}),
|
|
56
|
+
)
|
|
57
|
+
.handle(
|
|
58
|
+
"mcp.disconnect",
|
|
59
|
+
Effect.fn(function* (ctx) {
|
|
60
|
+
const service = yield* Mcp.Service
|
|
61
|
+
yield* notFound(service.disconnect(ctx.params.server))
|
|
62
|
+
return HttpApiSchema.NoContent.make()
|
|
63
|
+
}),
|
|
64
|
+
)
|
|
65
|
+
.handle(
|
|
66
|
+
"mcp.resource.catalog",
|
|
67
|
+
Effect.fn(function* () {
|
|
68
|
+
const service = yield* Mcp.Service
|
|
69
|
+
return yield* response(service.resourceCatalog())
|
|
70
|
+
}),
|
|
71
|
+
)
|
|
72
|
+
.handle(
|
|
73
|
+
"mcp.clientToken",
|
|
74
|
+
Effect.fn(function* (ctx) {
|
|
75
|
+
// The custom header forces a CORS preflight, so cross-origin pages cannot mint tickets.
|
|
76
|
+
if (
|
|
77
|
+
ctx.headers[PTY_CONNECT_TOKEN_HEADER] !== PTY_CONNECT_TOKEN_HEADER_VALUE ||
|
|
78
|
+
!isAllowedRequestOrigin(ctx.request.headers.origin, ctx.request.headers.host, cors)
|
|
79
|
+
)
|
|
80
|
+
return yield* new ForbiddenError({ message: "Invalid client MCP connect token request" })
|
|
81
|
+
const location = yield* Location.Service
|
|
82
|
+
return yield* response(
|
|
83
|
+
tickets.issue({
|
|
84
|
+
mcpClient: ctx.query.name,
|
|
85
|
+
directory: location.directory,
|
|
86
|
+
workspaceID: location.workspaceID,
|
|
87
|
+
}),
|
|
88
|
+
)
|
|
89
|
+
}),
|
|
90
|
+
)
|
|
91
|
+
.handleRaw(
|
|
92
|
+
"mcp.client",
|
|
93
|
+
Effect.fn("McpHandler.client")(function* (ctx) {
|
|
94
|
+
if (!isAllowedRequestOrigin(ctx.request.headers.origin, ctx.request.headers.host, cors))
|
|
95
|
+
return HttpServerResponse.empty({ status: 403 })
|
|
96
|
+
|
|
97
|
+
const name = ctx.query.name
|
|
98
|
+
const ref = LocationServiceMap.canonical(requestRef(ctx.request))
|
|
99
|
+
if (
|
|
100
|
+
ctx.query.ticket &&
|
|
101
|
+
!(yield* tickets.consume({
|
|
102
|
+
ticket: ctx.query.ticket,
|
|
103
|
+
mcpClient: name,
|
|
104
|
+
directory: ref.directory,
|
|
105
|
+
workspaceID: ref.workspaceID,
|
|
106
|
+
}))
|
|
107
|
+
)
|
|
108
|
+
return HttpServerResponse.empty({ status: 403 })
|
|
109
|
+
|
|
110
|
+
return yield* Effect.gen(function* () {
|
|
111
|
+
const mcp = yield* Mcp.Service
|
|
112
|
+
const { McpSocketClient } = yield* Effect.promise(() => import("@zyro-sec/core/mcp/socket-client"))
|
|
113
|
+
const socket = yield* Effect.orDie(ctx.request.upgrade)
|
|
114
|
+
const outbox = yield* Queue.unbounded<Outbound>()
|
|
115
|
+
yield* holdLocation(outbox)
|
|
116
|
+
const state = { attached: false, disconnected: false }
|
|
117
|
+
const transport = McpSocketClient.make({
|
|
118
|
+
send: (text) => Queue.offerUnsafe(outbox, text),
|
|
119
|
+
// A failed attach closes the transport before reporting the error; ignore that close so the
|
|
120
|
+
// socket closes with 1011 below instead of a normal 1000.
|
|
121
|
+
close: (code, reason) => {
|
|
122
|
+
if (state.attached) Queue.offerUnsafe(outbox, new Socket.CloseEvent(code, reason))
|
|
123
|
+
},
|
|
124
|
+
})
|
|
125
|
+
const decoder = new TextDecoder()
|
|
126
|
+
// The attachment lives in this scope, so it ends with the socket.
|
|
127
|
+
yield* mcp.attach(name, transport).pipe(
|
|
128
|
+
Effect.tap(() =>
|
|
129
|
+
Effect.sync(() => {
|
|
130
|
+
state.attached = true
|
|
131
|
+
}),
|
|
132
|
+
),
|
|
133
|
+
Effect.catch((error) =>
|
|
134
|
+
state.disconnected
|
|
135
|
+
? Effect.logInfo("client MCP disconnected during initialize", { name, error: error.message })
|
|
136
|
+
: Effect.logError("client MCP attach failed", { name, error: error.message }).pipe(
|
|
137
|
+
Effect.andThen(
|
|
138
|
+
Effect.sync(() => Queue.offerUnsafe(outbox, new Socket.CloseEvent(1011, "attach failed"))),
|
|
139
|
+
),
|
|
140
|
+
),
|
|
141
|
+
),
|
|
142
|
+
Effect.forkScoped,
|
|
143
|
+
)
|
|
144
|
+
yield* runPtySocket({
|
|
145
|
+
socket,
|
|
146
|
+
outbox,
|
|
147
|
+
// Frames should be text, but binary frames are accepted and decoded as UTF-8.
|
|
148
|
+
onMessage: (message) =>
|
|
149
|
+
Effect.sync(() => transport.deliver(typeof message === "string" ? message : decoder.decode(message))),
|
|
150
|
+
detach: () => {
|
|
151
|
+
state.disconnected = true
|
|
152
|
+
transport.ended()
|
|
153
|
+
},
|
|
154
|
+
})
|
|
155
|
+
return HttpServerResponse.empty()
|
|
156
|
+
}).pipe(Effect.scoped, Effect.provide(locations.get(ref)), locationErrors)
|
|
157
|
+
}),
|
|
158
|
+
)
|
|
159
|
+
}),
|
|
160
|
+
)
|
package/src/handlers/media.ts
CHANGED
|
@@ -1,44 +1,44 @@
|
|
|
1
|
-
import { Database } from "@zyro-sec/core/database/database"
|
|
2
|
-
import { SessionBlob } from "@zyro-sec/core/session/blob"
|
|
3
|
-
import { FileNotFoundError } from "@zyro-sec/protocol/errors"
|
|
4
|
-
import { Effect } from "effect"
|
|
5
|
-
import { type HttpServerRequest, HttpServerResponse } from "effect/http"
|
|
6
|
-
import { ContentHeaders } from "../content-headers"
|
|
7
|
-
|
|
8
|
-
const INLINE_IMAGE_MIMES = new Set(["image/png", "image/jpeg", "image/gif", "image/webp"])
|
|
9
|
-
const CACHE_CONTROL = "private, max-age=31536000, immutable"
|
|
10
|
-
|
|
11
|
-
/** `media.get` in the `server.experimental` group; serves only blobs a message or inbox item still references. */
|
|
12
|
-
export const makeMediaGet = Effect.gen(function* () {
|
|
13
|
-
const { db } = yield* Database.Service
|
|
14
|
-
return (hash: string, request: HttpServerRequest.HttpServerRequest) =>
|
|
15
|
-
Effect.gen(function* () {
|
|
16
|
-
const path = `/api/experimental/media/${hash}`
|
|
17
|
-
const blob = yield* SessionBlob.read(db, hash)
|
|
18
|
-
if (!blob) return yield* new FileNotFoundError({ path, message: `Media blob not found: ${hash}` })
|
|
19
|
-
const etag = `"${hash}"`
|
|
20
|
-
const inline = INLINE_IMAGE_MIMES.has(blob.mime)
|
|
21
|
-
const headers = {
|
|
22
|
-
"cache-control": CACHE_CONTROL,
|
|
23
|
-
etag,
|
|
24
|
-
"content-disposition": ContentHeaders.disposition(""),
|
|
25
|
-
"content-security-policy": ContentHeaders.CONTENT_SECURITY_POLICY,
|
|
26
|
-
"x-content-type-options": "nosniff",
|
|
27
|
-
}
|
|
28
|
-
if (matchesIfNoneMatch(request.headers["if-none-match"], etag))
|
|
29
|
-
return HttpServerResponse.empty({ status: 304, headers })
|
|
30
|
-
return HttpServerResponse.uint8Array(blob.data, {
|
|
31
|
-
status: 200,
|
|
32
|
-
contentType: inline ? blob.mime : "application/octet-stream",
|
|
33
|
-
headers,
|
|
34
|
-
})
|
|
35
|
-
})
|
|
36
|
-
})
|
|
37
|
-
|
|
38
|
-
function matchesIfNoneMatch(ifNoneMatch: string | undefined, etag: string) {
|
|
39
|
-
if (ifNoneMatch === undefined) return false
|
|
40
|
-
return ifNoneMatch.split(",").some((tag) => {
|
|
41
|
-
const trimmed = tag.trim()
|
|
42
|
-
return trimmed === "*" || trimmed.replace(/^w\//i, "") === etag
|
|
43
|
-
})
|
|
44
|
-
}
|
|
1
|
+
import { Database } from "@zyro-sec/core/database/database"
|
|
2
|
+
import { SessionBlob } from "@zyro-sec/core/session/blob"
|
|
3
|
+
import { FileNotFoundError } from "@zyro-sec/protocol/errors"
|
|
4
|
+
import { Effect } from "effect"
|
|
5
|
+
import { type HttpServerRequest, HttpServerResponse } from "effect/http"
|
|
6
|
+
import { ContentHeaders } from "../content-headers"
|
|
7
|
+
|
|
8
|
+
const INLINE_IMAGE_MIMES = new Set(["image/png", "image/jpeg", "image/gif", "image/webp"])
|
|
9
|
+
const CACHE_CONTROL = "private, max-age=31536000, immutable"
|
|
10
|
+
|
|
11
|
+
/** `media.get` in the `server.experimental` group; serves only blobs a message or inbox item still references. */
|
|
12
|
+
export const makeMediaGet = Effect.gen(function* () {
|
|
13
|
+
const { db } = yield* Database.Service
|
|
14
|
+
return (hash: string, request: HttpServerRequest.HttpServerRequest) =>
|
|
15
|
+
Effect.gen(function* () {
|
|
16
|
+
const path = `/api/experimental/media/${hash}`
|
|
17
|
+
const blob = yield* SessionBlob.read(db, hash)
|
|
18
|
+
if (!blob) return yield* new FileNotFoundError({ path, message: `Media blob not found: ${hash}` })
|
|
19
|
+
const etag = `"${hash}"`
|
|
20
|
+
const inline = INLINE_IMAGE_MIMES.has(blob.mime)
|
|
21
|
+
const headers = {
|
|
22
|
+
"cache-control": CACHE_CONTROL,
|
|
23
|
+
etag,
|
|
24
|
+
"content-disposition": ContentHeaders.disposition(""),
|
|
25
|
+
"content-security-policy": ContentHeaders.CONTENT_SECURITY_POLICY,
|
|
26
|
+
"x-content-type-options": "nosniff",
|
|
27
|
+
}
|
|
28
|
+
if (matchesIfNoneMatch(request.headers["if-none-match"], etag))
|
|
29
|
+
return HttpServerResponse.empty({ status: 304, headers })
|
|
30
|
+
return HttpServerResponse.uint8Array(blob.data, {
|
|
31
|
+
status: 200,
|
|
32
|
+
contentType: inline ? blob.mime : "application/octet-stream",
|
|
33
|
+
headers,
|
|
34
|
+
})
|
|
35
|
+
})
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
function matchesIfNoneMatch(ifNoneMatch: string | undefined, etag: string) {
|
|
39
|
+
if (ifNoneMatch === undefined) return false
|
|
40
|
+
return ifNoneMatch.split(",").some((tag) => {
|
|
41
|
+
const trimmed = tag.trim()
|
|
42
|
+
return trimmed === "*" || trimmed.replace(/^w\//i, "") === etag
|
|
43
|
+
})
|
|
44
|
+
}
|
package/src/handlers/message.ts
CHANGED
|
@@ -1,82 +1,82 @@
|
|
|
1
|
-
import { SessionMessage } from "@zyro-sec/core/session/message"
|
|
2
|
-
import { Session } from "@zyro-sec/core/session"
|
|
3
|
-
import { Effect, Option, Schema } from "effect"
|
|
4
|
-
import { HttpServerResponse } from "effect/http"
|
|
5
|
-
import { HttpApiBuilder } from "effect/http-api"
|
|
6
|
-
import { Api } from "../api"
|
|
7
|
-
import { wantsMediaRefs } from "../media-uri"
|
|
8
|
-
import { InvalidCursorError } from "@zyro-sec/protocol/errors"
|
|
9
|
-
import { SessionMediaMessagesResponse } from "@zyro-sec/protocol/groups/message"
|
|
10
|
-
import { failedMessageDecode, missingSession } from "./session-error"
|
|
11
|
-
|
|
12
|
-
const DefaultMessagesLimit = 50
|
|
13
|
-
|
|
14
|
-
const Cursor = Schema.Struct({
|
|
15
|
-
id: SessionMessage.ID,
|
|
16
|
-
order: Schema.Union([Schema.Literal("asc"), Schema.Literal("desc")]),
|
|
17
|
-
direction: Schema.Union([Schema.Literal("previous"), Schema.Literal("next")]),
|
|
18
|
-
})
|
|
19
|
-
|
|
20
|
-
const decodeCursor = Schema.decodeUnknownOption(Schema.fromJsonString(Cursor))
|
|
21
|
-
|
|
22
|
-
const cursor = {
|
|
23
|
-
encode(message: { readonly id: SessionMessage.ID }, order: "asc" | "desc", direction: "previous" | "next") {
|
|
24
|
-
return Buffer.from(JSON.stringify({ id: message.id, order, direction })).toString("base64url")
|
|
25
|
-
},
|
|
26
|
-
decode(input: string) {
|
|
27
|
-
return decodeCursor(Buffer.from(input, "base64url").toString("utf8"))
|
|
28
|
-
},
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export const MessageHandler = HttpApiBuilder.group(Api, "server.message", (handlers) =>
|
|
32
|
-
Effect.gen(function* () {
|
|
33
|
-
const session = yield* Session.Service
|
|
34
|
-
|
|
35
|
-
return handlers.handle(
|
|
36
|
-
"session.messages",
|
|
37
|
-
Effect.fn(function* (ctx) {
|
|
38
|
-
if (ctx.query.cursor && ctx.query.order !== undefined)
|
|
39
|
-
return yield* new InvalidCursorError({ message: "Cursor cannot be combined with order" })
|
|
40
|
-
const decoded = ctx.query.cursor
|
|
41
|
-
? (Option.getOrUndefined(cursor.decode(ctx.query.cursor)) ??
|
|
42
|
-
(yield* new InvalidCursorError({ message: "Invalid cursor" })))
|
|
43
|
-
: undefined
|
|
44
|
-
const order = decoded?.order ?? ctx.query.order ?? "desc"
|
|
45
|
-
const input = {
|
|
46
|
-
sessionID: ctx.params.sessionID,
|
|
47
|
-
limit: ctx.query.limit ?? DefaultMessagesLimit,
|
|
48
|
-
order,
|
|
49
|
-
type: ctx.query.type,
|
|
50
|
-
cursor: decoded ? { id: decoded.id, direction: decoded.direction } : undefined,
|
|
51
|
-
}
|
|
52
|
-
const page = <Message extends { readonly id: SessionMessage.ID }>(messages: ReadonlyArray<Message>) => {
|
|
53
|
-
const first = messages[0]
|
|
54
|
-
const last = messages.at(-1)
|
|
55
|
-
return {
|
|
56
|
-
data: messages,
|
|
57
|
-
cursor: {
|
|
58
|
-
previous: first ? cursor.encode(first, order, "previous") : undefined,
|
|
59
|
-
next: last ? cursor.encode(last, order, "next") : undefined,
|
|
60
|
-
},
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
if (wantsMediaRefs(ctx.request)) {
|
|
64
|
-
const messages = yield* session
|
|
65
|
-
.messagesWithRefs(input)
|
|
66
|
-
.pipe(
|
|
67
|
-
Effect.catchTag("Session.NotFoundError", missingSession),
|
|
68
|
-
Effect.catchTag("Session.MessageDecodeError", failedMessageDecode),
|
|
69
|
-
)
|
|
70
|
-
return yield* HttpServerResponse.schemaJson(SessionMediaMessagesResponse)(page(messages)).pipe(Effect.orDie)
|
|
71
|
-
}
|
|
72
|
-
const messages = yield* session
|
|
73
|
-
.messages(input)
|
|
74
|
-
.pipe(
|
|
75
|
-
Effect.catchTag("Session.NotFoundError", missingSession),
|
|
76
|
-
Effect.catchTag("Session.MessageDecodeError", failedMessageDecode),
|
|
77
|
-
)
|
|
78
|
-
return page(messages)
|
|
79
|
-
}),
|
|
80
|
-
)
|
|
81
|
-
}),
|
|
82
|
-
)
|
|
1
|
+
import { SessionMessage } from "@zyro-sec/core/session/message"
|
|
2
|
+
import { Session } from "@zyro-sec/core/session"
|
|
3
|
+
import { Effect, Option, Schema } from "effect"
|
|
4
|
+
import { HttpServerResponse } from "effect/http"
|
|
5
|
+
import { HttpApiBuilder } from "effect/http-api"
|
|
6
|
+
import { Api } from "../api"
|
|
7
|
+
import { wantsMediaRefs } from "../media-uri"
|
|
8
|
+
import { InvalidCursorError } from "@zyro-sec/protocol/errors"
|
|
9
|
+
import { SessionMediaMessagesResponse } from "@zyro-sec/protocol/groups/message"
|
|
10
|
+
import { failedMessageDecode, missingSession } from "./session-error"
|
|
11
|
+
|
|
12
|
+
const DefaultMessagesLimit = 50
|
|
13
|
+
|
|
14
|
+
const Cursor = Schema.Struct({
|
|
15
|
+
id: SessionMessage.ID,
|
|
16
|
+
order: Schema.Union([Schema.Literal("asc"), Schema.Literal("desc")]),
|
|
17
|
+
direction: Schema.Union([Schema.Literal("previous"), Schema.Literal("next")]),
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
const decodeCursor = Schema.decodeUnknownOption(Schema.fromJsonString(Cursor))
|
|
21
|
+
|
|
22
|
+
const cursor = {
|
|
23
|
+
encode(message: { readonly id: SessionMessage.ID }, order: "asc" | "desc", direction: "previous" | "next") {
|
|
24
|
+
return Buffer.from(JSON.stringify({ id: message.id, order, direction })).toString("base64url")
|
|
25
|
+
},
|
|
26
|
+
decode(input: string) {
|
|
27
|
+
return decodeCursor(Buffer.from(input, "base64url").toString("utf8"))
|
|
28
|
+
},
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export const MessageHandler = HttpApiBuilder.group(Api, "server.message", (handlers) =>
|
|
32
|
+
Effect.gen(function* () {
|
|
33
|
+
const session = yield* Session.Service
|
|
34
|
+
|
|
35
|
+
return handlers.handle(
|
|
36
|
+
"session.messages",
|
|
37
|
+
Effect.fn(function* (ctx) {
|
|
38
|
+
if (ctx.query.cursor && ctx.query.order !== undefined)
|
|
39
|
+
return yield* new InvalidCursorError({ message: "Cursor cannot be combined with order" })
|
|
40
|
+
const decoded = ctx.query.cursor
|
|
41
|
+
? (Option.getOrUndefined(cursor.decode(ctx.query.cursor)) ??
|
|
42
|
+
(yield* new InvalidCursorError({ message: "Invalid cursor" })))
|
|
43
|
+
: undefined
|
|
44
|
+
const order = decoded?.order ?? ctx.query.order ?? "desc"
|
|
45
|
+
const input = {
|
|
46
|
+
sessionID: ctx.params.sessionID,
|
|
47
|
+
limit: ctx.query.limit ?? DefaultMessagesLimit,
|
|
48
|
+
order,
|
|
49
|
+
type: ctx.query.type,
|
|
50
|
+
cursor: decoded ? { id: decoded.id, direction: decoded.direction } : undefined,
|
|
51
|
+
}
|
|
52
|
+
const page = <Message extends { readonly id: SessionMessage.ID }>(messages: ReadonlyArray<Message>) => {
|
|
53
|
+
const first = messages[0]
|
|
54
|
+
const last = messages.at(-1)
|
|
55
|
+
return {
|
|
56
|
+
data: messages,
|
|
57
|
+
cursor: {
|
|
58
|
+
previous: first ? cursor.encode(first, order, "previous") : undefined,
|
|
59
|
+
next: last ? cursor.encode(last, order, "next") : undefined,
|
|
60
|
+
},
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
if (wantsMediaRefs(ctx.request)) {
|
|
64
|
+
const messages = yield* session
|
|
65
|
+
.messagesWithRefs(input)
|
|
66
|
+
.pipe(
|
|
67
|
+
Effect.catchTag("Session.NotFoundError", missingSession),
|
|
68
|
+
Effect.catchTag("Session.MessageDecodeError", failedMessageDecode),
|
|
69
|
+
)
|
|
70
|
+
return yield* HttpServerResponse.schemaJson(SessionMediaMessagesResponse)(page(messages)).pipe(Effect.orDie)
|
|
71
|
+
}
|
|
72
|
+
const messages = yield* session
|
|
73
|
+
.messages(input)
|
|
74
|
+
.pipe(
|
|
75
|
+
Effect.catchTag("Session.NotFoundError", missingSession),
|
|
76
|
+
Effect.catchTag("Session.MessageDecodeError", failedMessageDecode),
|
|
77
|
+
)
|
|
78
|
+
return page(messages)
|
|
79
|
+
}),
|
|
80
|
+
)
|
|
81
|
+
}),
|
|
82
|
+
)
|
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import { V1Migration } from "@zyro-sec/core/database/v1-migration"
|
|
2
|
-
import { HttpApiBuilder } from "effect/http-api"
|
|
3
|
-
import { Effect } from "effect"
|
|
4
|
-
import { Api } from "../api"
|
|
5
|
-
|
|
6
|
-
export const MigrationHandler = HttpApiBuilder.group(Api, "server.migration", (handlers) =>
|
|
7
|
-
handlers.handle(
|
|
8
|
-
"migration.v1.status",
|
|
9
|
-
Effect.fn(function* () {
|
|
10
|
-
return yield* V1Migration.status()
|
|
11
|
-
}),
|
|
12
|
-
),
|
|
13
|
-
)
|
|
1
|
+
import { V1Migration } from "@zyro-sec/core/database/v1-migration"
|
|
2
|
+
import { HttpApiBuilder } from "effect/http-api"
|
|
3
|
+
import { Effect } from "effect"
|
|
4
|
+
import { Api } from "../api"
|
|
5
|
+
|
|
6
|
+
export const MigrationHandler = HttpApiBuilder.group(Api, "server.migration", (handlers) =>
|
|
7
|
+
handlers.handle(
|
|
8
|
+
"migration.v1.status",
|
|
9
|
+
Effect.fn(function* () {
|
|
10
|
+
return yield* V1Migration.status()
|
|
11
|
+
}),
|
|
12
|
+
),
|
|
13
|
+
)
|
package/src/handlers/model.ts
CHANGED
|
@@ -1,25 +1,25 @@
|
|
|
1
|
-
import { Model } from "@zyro-sec/core/model"
|
|
2
|
-
import { Effect } from "effect"
|
|
3
|
-
import { HttpApiBuilder } from "effect/http-api"
|
|
4
|
-
import { Api } from "../api"
|
|
5
|
-
import { response } from "../location"
|
|
6
|
-
|
|
7
|
-
export const ModelHandler = HttpApiBuilder.group(Api, "server.model", (handlers) =>
|
|
8
|
-
Effect.gen(function* () {
|
|
9
|
-
return handlers
|
|
10
|
-
.handle(
|
|
11
|
-
"model.list",
|
|
12
|
-
Effect.fn(function* (ctx) {
|
|
13
|
-
const models = yield* Model.Service
|
|
14
|
-
return yield* response(models.available(ctx.query.types))
|
|
15
|
-
}),
|
|
16
|
-
)
|
|
17
|
-
.handle(
|
|
18
|
-
"model.default",
|
|
19
|
-
Effect.fn(function* () {
|
|
20
|
-
const models = yield* Model.Service
|
|
21
|
-
return yield* response(models.default())
|
|
22
|
-
}),
|
|
23
|
-
)
|
|
24
|
-
}),
|
|
25
|
-
)
|
|
1
|
+
import { Model } from "@zyro-sec/core/model"
|
|
2
|
+
import { Effect } from "effect"
|
|
3
|
+
import { HttpApiBuilder } from "effect/http-api"
|
|
4
|
+
import { Api } from "../api"
|
|
5
|
+
import { response } from "../location"
|
|
6
|
+
|
|
7
|
+
export const ModelHandler = HttpApiBuilder.group(Api, "server.model", (handlers) =>
|
|
8
|
+
Effect.gen(function* () {
|
|
9
|
+
return handlers
|
|
10
|
+
.handle(
|
|
11
|
+
"model.list",
|
|
12
|
+
Effect.fn(function* (ctx) {
|
|
13
|
+
const models = yield* Model.Service
|
|
14
|
+
return yield* response(models.available(ctx.query.types))
|
|
15
|
+
}),
|
|
16
|
+
)
|
|
17
|
+
.handle(
|
|
18
|
+
"model.default",
|
|
19
|
+
Effect.fn(function* () {
|
|
20
|
+
const models = yield* Model.Service
|
|
21
|
+
return yield* response(models.default())
|
|
22
|
+
}),
|
|
23
|
+
)
|
|
24
|
+
}),
|
|
25
|
+
)
|