@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/artifact.ts
CHANGED
|
@@ -1,242 +1,242 @@
|
|
|
1
|
-
import { Artifact } from "@zyro-sec/core/artifact"
|
|
2
|
-
import { KV } from "@zyro-sec/core/kv"
|
|
3
|
-
import { Location } from "@zyro-sec/core/location"
|
|
4
|
-
import { LocationServiceMap } from "@zyro-sec/core/location-service-map"
|
|
5
|
-
import { AbsolutePath } from "@zyro-sec/core/schema"
|
|
6
|
-
import { Session } from "@zyro-sec/core/session"
|
|
7
|
-
import {
|
|
8
|
-
ArtifactNotFoundError,
|
|
9
|
-
FileNotFoundError,
|
|
10
|
-
ForbiddenError,
|
|
11
|
-
InvalidRequestError,
|
|
12
|
-
ProviderNotFoundError,
|
|
13
|
-
ServiceUnavailableError,
|
|
14
|
-
} from "@zyro-sec/protocol/errors"
|
|
15
|
-
import { Effect } from "effect"
|
|
16
|
-
import { type HttpServerRequest, HttpServerResponse } from "effect/http"
|
|
17
|
-
import { HttpApiSchema } from "effect/http-api"
|
|
18
|
-
import { locationErrors, requestRef, response } from "../location"
|
|
19
|
-
import { missingSession } from "./session-error"
|
|
20
|
-
|
|
21
|
-
/** `artifacts.*` in the `server.experimental` group. Every route needs experimental.artifacts in the resolved Location. */
|
|
22
|
-
export const makeArtifact = Effect.gen(function* () {
|
|
23
|
-
const locations = yield* LocationServiceMap.Service
|
|
24
|
-
const sessions = yield* Session.Service
|
|
25
|
-
const kv = yield* KV.Service
|
|
26
|
-
|
|
27
|
-
const explicitDirectory = (
|
|
28
|
-
request: HttpServerRequest.HttpServerRequest,
|
|
29
|
-
location?: { readonly directory?: string; readonly workspace?: string },
|
|
30
|
-
) => {
|
|
31
|
-
if (location?.directory !== undefined) return location.directory
|
|
32
|
-
const query = new URL(request.url, "http://localhost").searchParams.get("location[directory]")
|
|
33
|
-
if (query) return query
|
|
34
|
-
const header = request.headers["x-opencode-directory"]
|
|
35
|
-
if (!header) return undefined
|
|
36
|
-
try {
|
|
37
|
-
return decodeURIComponent(header)
|
|
38
|
-
} catch {
|
|
39
|
-
return header
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
const resolveLocation = (
|
|
44
|
-
request: HttpServerRequest.HttpServerRequest,
|
|
45
|
-
options: {
|
|
46
|
-
readonly location?: { readonly directory?: string; readonly workspace?: string }
|
|
47
|
-
readonly sessionID?: Session.ID
|
|
48
|
-
readonly artifactID?: Artifact.ID
|
|
49
|
-
},
|
|
50
|
-
) =>
|
|
51
|
-
Effect.gen(function* () {
|
|
52
|
-
const requestedDir = explicitDirectory(request, options.location)
|
|
53
|
-
if (requestedDir !== undefined) {
|
|
54
|
-
return Location.Ref.make({ directory: AbsolutePath.make(requestedDir) })
|
|
55
|
-
}
|
|
56
|
-
if (options.sessionID !== undefined) {
|
|
57
|
-
const session = yield* sessions.get(options.sessionID).pipe(Effect.mapError(missingSession))
|
|
58
|
-
return session.location
|
|
59
|
-
}
|
|
60
|
-
if (options.artifactID !== undefined) {
|
|
61
|
-
const directory = yield* Artifact.locate(kv, options.artifactID)
|
|
62
|
-
if (directory !== undefined) {
|
|
63
|
-
return Location.Ref.make({ directory: AbsolutePath.make(directory) })
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
return requestRef(request)
|
|
67
|
-
})
|
|
68
|
-
|
|
69
|
-
const inLocation = <A, E>(
|
|
70
|
-
request: HttpServerRequest.HttpServerRequest,
|
|
71
|
-
options: {
|
|
72
|
-
readonly location?: { readonly directory?: string; readonly workspace?: string }
|
|
73
|
-
readonly sessionID?: Session.ID
|
|
74
|
-
readonly artifactID?: Artifact.ID
|
|
75
|
-
},
|
|
76
|
-
effect: Effect.Effect<A, E, Artifact.Service | Location.Service>,
|
|
77
|
-
) =>
|
|
78
|
-
Effect.gen(function* () {
|
|
79
|
-
const ref = yield* resolveLocation(request, options)
|
|
80
|
-
return yield* effect.pipe(Effect.provide(locations.get(ref)), locationErrors)
|
|
81
|
-
})
|
|
82
|
-
|
|
83
|
-
const mapCommon = <A, E>(effect: Effect.Effect<A, E>) =>
|
|
84
|
-
effect.pipe(
|
|
85
|
-
Effect.catchTags({
|
|
86
|
-
"Artifact.DisabledError": (error: Artifact.DisabledError) =>
|
|
87
|
-
Effect.fail(new ForbiddenError({ message: error.message })),
|
|
88
|
-
"Artifact.NotFoundError": (error: Artifact.NotFoundError) =>
|
|
89
|
-
Effect.fail(new ArtifactNotFoundError({ artifactID: error.artifactID, message: error.message })),
|
|
90
|
-
"Artifact.ProviderNotFoundError": (error: Artifact.ProviderNotFoundError) =>
|
|
91
|
-
Effect.fail(new ProviderNotFoundError({ providerID: error.providerID, message: error.message })),
|
|
92
|
-
"Artifact.InvalidError": (error: Artifact.InvalidError) =>
|
|
93
|
-
Effect.fail(
|
|
94
|
-
new InvalidRequestError({ message: error.message, kind: "artifact_invalid", field: error.field }),
|
|
95
|
-
),
|
|
96
|
-
"Artifact.ProviderError": (error: Artifact.ProviderError) =>
|
|
97
|
-
Effect.fail(new ServiceUnavailableError({ message: error.message, service: error.providerID })),
|
|
98
|
-
"Session.NotFoundError": (error: Session.NotFoundError) => Effect.fail(missingSession(error)),
|
|
99
|
-
} as never),
|
|
100
|
-
)
|
|
101
|
-
|
|
102
|
-
return {
|
|
103
|
-
providers: (
|
|
104
|
-
query: { readonly location?: { readonly directory?: string } },
|
|
105
|
-
request: HttpServerRequest.HttpServerRequest,
|
|
106
|
-
) =>
|
|
107
|
-
inLocation(
|
|
108
|
-
request,
|
|
109
|
-
{ location: query.location },
|
|
110
|
-
Effect.gen(function* () {
|
|
111
|
-
const artifacts = yield* Artifact.Service
|
|
112
|
-
return yield* response(artifacts.providers())
|
|
113
|
-
}),
|
|
114
|
-
).pipe(mapCommon),
|
|
115
|
-
|
|
116
|
-
list: (
|
|
117
|
-
query: {
|
|
118
|
-
readonly location?: { readonly directory?: string }
|
|
119
|
-
readonly sessionID?: Session.ID
|
|
120
|
-
readonly provider?: Artifact.ProviderID
|
|
121
|
-
},
|
|
122
|
-
request: HttpServerRequest.HttpServerRequest,
|
|
123
|
-
) =>
|
|
124
|
-
inLocation(
|
|
125
|
-
request,
|
|
126
|
-
{ location: query.location, sessionID: query.sessionID },
|
|
127
|
-
Effect.gen(function* () {
|
|
128
|
-
const artifacts = yield* Artifact.Service
|
|
129
|
-
return yield* response(artifacts.list({ sessionID: query.sessionID, provider: query.provider }))
|
|
130
|
-
}),
|
|
131
|
-
).pipe(mapCommon),
|
|
132
|
-
|
|
133
|
-
create: (
|
|
134
|
-
query: { readonly location?: { readonly directory?: string } },
|
|
135
|
-
payload: Artifact.CreateInput,
|
|
136
|
-
request: HttpServerRequest.HttpServerRequest,
|
|
137
|
-
) =>
|
|
138
|
-
inLocation(
|
|
139
|
-
request,
|
|
140
|
-
{ location: query.location, sessionID: payload.sessionID },
|
|
141
|
-
Effect.gen(function* () {
|
|
142
|
-
const artifacts = yield* Artifact.Service
|
|
143
|
-
return yield* response(artifacts.create(payload))
|
|
144
|
-
}),
|
|
145
|
-
).pipe(mapCommon),
|
|
146
|
-
|
|
147
|
-
get: (
|
|
148
|
-
artifactID: Artifact.ID,
|
|
149
|
-
query: { readonly location?: { readonly directory?: string } },
|
|
150
|
-
request: HttpServerRequest.HttpServerRequest,
|
|
151
|
-
) =>
|
|
152
|
-
inLocation(
|
|
153
|
-
request,
|
|
154
|
-
{ location: query.location, artifactID },
|
|
155
|
-
Effect.gen(function* () {
|
|
156
|
-
const artifacts = yield* Artifact.Service
|
|
157
|
-
return yield* response(artifacts.get(artifactID))
|
|
158
|
-
}),
|
|
159
|
-
).pipe(mapCommon),
|
|
160
|
-
|
|
161
|
-
update: (
|
|
162
|
-
artifactID: Artifact.ID,
|
|
163
|
-
query: { readonly location?: { readonly directory?: string } },
|
|
164
|
-
payload: Artifact.UpdateInput,
|
|
165
|
-
request: HttpServerRequest.HttpServerRequest,
|
|
166
|
-
) =>
|
|
167
|
-
inLocation(
|
|
168
|
-
request,
|
|
169
|
-
{ location: query.location, sessionID: payload.sessionID, artifactID },
|
|
170
|
-
Effect.gen(function* () {
|
|
171
|
-
const artifacts = yield* Artifact.Service
|
|
172
|
-
return yield* response(artifacts.update(artifactID, payload))
|
|
173
|
-
}),
|
|
174
|
-
).pipe(mapCommon),
|
|
175
|
-
|
|
176
|
-
remove: (
|
|
177
|
-
artifactID: Artifact.ID,
|
|
178
|
-
query: { readonly location?: { readonly directory?: string } },
|
|
179
|
-
request: HttpServerRequest.HttpServerRequest,
|
|
180
|
-
) =>
|
|
181
|
-
inLocation(
|
|
182
|
-
request,
|
|
183
|
-
{ location: query.location, artifactID },
|
|
184
|
-
Effect.gen(function* () {
|
|
185
|
-
const artifacts = yield* Artifact.Service
|
|
186
|
-
yield* artifacts.remove(artifactID)
|
|
187
|
-
return HttpApiSchema.NoContent.make()
|
|
188
|
-
}),
|
|
189
|
-
).pipe(mapCommon),
|
|
190
|
-
|
|
191
|
-
view: (
|
|
192
|
-
artifactID: Artifact.ID,
|
|
193
|
-
query: { readonly location?: { readonly directory?: string } },
|
|
194
|
-
request: HttpServerRequest.HttpServerRequest,
|
|
195
|
-
) =>
|
|
196
|
-
inLocation(
|
|
197
|
-
request,
|
|
198
|
-
{ location: query.location, artifactID },
|
|
199
|
-
Effect.gen(function* () {
|
|
200
|
-
const artifacts = yield* Artifact.Service
|
|
201
|
-
const parsed = new URL(request.url, "http://localhost")
|
|
202
|
-
const prefix = `/api/experimental/artifacts/${encodeURIComponent(artifactID)}/view`
|
|
203
|
-
if (parsed.pathname === prefix) {
|
|
204
|
-
yield* artifacts.info(artifactID)
|
|
205
|
-
return HttpServerResponse.empty({
|
|
206
|
-
status: 308,
|
|
207
|
-
headers: { location: `${prefix}/${parsed.search}` },
|
|
208
|
-
})
|
|
209
|
-
}
|
|
210
|
-
const subpath = extractViewSubpath(request.url, artifactID)
|
|
211
|
-
const file = yield* artifacts.readLocalFile(artifactID, subpath).pipe(
|
|
212
|
-
Effect.catchTag("Artifact.FileMissingError", (error) =>
|
|
213
|
-
Effect.fail(new FileNotFoundError({ path: error.path, message: error.message })),
|
|
214
|
-
),
|
|
215
|
-
)
|
|
216
|
-
return HttpServerResponse.uint8Array(file.bytes, {
|
|
217
|
-
status: 200,
|
|
218
|
-
contentType: file.mime,
|
|
219
|
-
headers: {
|
|
220
|
-
"access-control-allow-origin": "*",
|
|
221
|
-
"cache-control": "private, no-cache",
|
|
222
|
-
"content-security-policy": "sandbox allow-scripts",
|
|
223
|
-
"x-content-type-options": "nosniff",
|
|
224
|
-
},
|
|
225
|
-
})
|
|
226
|
-
}),
|
|
227
|
-
).pipe(mapCommon),
|
|
228
|
-
}
|
|
229
|
-
})
|
|
230
|
-
|
|
231
|
-
function extractViewSubpath(url: string, artifactID: Artifact.ID) {
|
|
232
|
-
const pathname = new URL(url, "http://localhost").pathname
|
|
233
|
-
const prefix = `/api/experimental/artifacts/${encodeURIComponent(artifactID)}/view/`
|
|
234
|
-
if (!pathname.startsWith(prefix)) return undefined
|
|
235
|
-
const raw = pathname.slice(prefix.length)
|
|
236
|
-
if (!raw) return undefined
|
|
237
|
-
try {
|
|
238
|
-
return decodeURIComponent(raw)
|
|
239
|
-
} catch {
|
|
240
|
-
return raw
|
|
241
|
-
}
|
|
242
|
-
}
|
|
1
|
+
import { Artifact } from "@zyro-sec/core/artifact"
|
|
2
|
+
import { KV } from "@zyro-sec/core/kv"
|
|
3
|
+
import { Location } from "@zyro-sec/core/location"
|
|
4
|
+
import { LocationServiceMap } from "@zyro-sec/core/location-service-map"
|
|
5
|
+
import { AbsolutePath } from "@zyro-sec/core/schema"
|
|
6
|
+
import { Session } from "@zyro-sec/core/session"
|
|
7
|
+
import {
|
|
8
|
+
ArtifactNotFoundError,
|
|
9
|
+
FileNotFoundError,
|
|
10
|
+
ForbiddenError,
|
|
11
|
+
InvalidRequestError,
|
|
12
|
+
ProviderNotFoundError,
|
|
13
|
+
ServiceUnavailableError,
|
|
14
|
+
} from "@zyro-sec/protocol/errors"
|
|
15
|
+
import { Effect } from "effect"
|
|
16
|
+
import { type HttpServerRequest, HttpServerResponse } from "effect/http"
|
|
17
|
+
import { HttpApiSchema } from "effect/http-api"
|
|
18
|
+
import { locationErrors, requestRef, response } from "../location"
|
|
19
|
+
import { missingSession } from "./session-error"
|
|
20
|
+
|
|
21
|
+
/** `artifacts.*` in the `server.experimental` group. Every route needs experimental.artifacts in the resolved Location. */
|
|
22
|
+
export const makeArtifact = Effect.gen(function* () {
|
|
23
|
+
const locations = yield* LocationServiceMap.Service
|
|
24
|
+
const sessions = yield* Session.Service
|
|
25
|
+
const kv = yield* KV.Service
|
|
26
|
+
|
|
27
|
+
const explicitDirectory = (
|
|
28
|
+
request: HttpServerRequest.HttpServerRequest,
|
|
29
|
+
location?: { readonly directory?: string; readonly workspace?: string },
|
|
30
|
+
) => {
|
|
31
|
+
if (location?.directory !== undefined) return location.directory
|
|
32
|
+
const query = new URL(request.url, "http://localhost").searchParams.get("location[directory]")
|
|
33
|
+
if (query) return query
|
|
34
|
+
const header = request.headers["x-opencode-directory"]
|
|
35
|
+
if (!header) return undefined
|
|
36
|
+
try {
|
|
37
|
+
return decodeURIComponent(header)
|
|
38
|
+
} catch {
|
|
39
|
+
return header
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const resolveLocation = (
|
|
44
|
+
request: HttpServerRequest.HttpServerRequest,
|
|
45
|
+
options: {
|
|
46
|
+
readonly location?: { readonly directory?: string; readonly workspace?: string }
|
|
47
|
+
readonly sessionID?: Session.ID
|
|
48
|
+
readonly artifactID?: Artifact.ID
|
|
49
|
+
},
|
|
50
|
+
) =>
|
|
51
|
+
Effect.gen(function* () {
|
|
52
|
+
const requestedDir = explicitDirectory(request, options.location)
|
|
53
|
+
if (requestedDir !== undefined) {
|
|
54
|
+
return Location.Ref.make({ directory: AbsolutePath.make(requestedDir) })
|
|
55
|
+
}
|
|
56
|
+
if (options.sessionID !== undefined) {
|
|
57
|
+
const session = yield* sessions.get(options.sessionID).pipe(Effect.mapError(missingSession))
|
|
58
|
+
return session.location
|
|
59
|
+
}
|
|
60
|
+
if (options.artifactID !== undefined) {
|
|
61
|
+
const directory = yield* Artifact.locate(kv, options.artifactID)
|
|
62
|
+
if (directory !== undefined) {
|
|
63
|
+
return Location.Ref.make({ directory: AbsolutePath.make(directory) })
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return requestRef(request)
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
const inLocation = <A, E>(
|
|
70
|
+
request: HttpServerRequest.HttpServerRequest,
|
|
71
|
+
options: {
|
|
72
|
+
readonly location?: { readonly directory?: string; readonly workspace?: string }
|
|
73
|
+
readonly sessionID?: Session.ID
|
|
74
|
+
readonly artifactID?: Artifact.ID
|
|
75
|
+
},
|
|
76
|
+
effect: Effect.Effect<A, E, Artifact.Service | Location.Service>,
|
|
77
|
+
) =>
|
|
78
|
+
Effect.gen(function* () {
|
|
79
|
+
const ref = yield* resolveLocation(request, options)
|
|
80
|
+
return yield* effect.pipe(Effect.provide(locations.get(ref)), locationErrors)
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
const mapCommon = <A, E>(effect: Effect.Effect<A, E>) =>
|
|
84
|
+
effect.pipe(
|
|
85
|
+
Effect.catchTags({
|
|
86
|
+
"Artifact.DisabledError": (error: Artifact.DisabledError) =>
|
|
87
|
+
Effect.fail(new ForbiddenError({ message: error.message })),
|
|
88
|
+
"Artifact.NotFoundError": (error: Artifact.NotFoundError) =>
|
|
89
|
+
Effect.fail(new ArtifactNotFoundError({ artifactID: error.artifactID, message: error.message })),
|
|
90
|
+
"Artifact.ProviderNotFoundError": (error: Artifact.ProviderNotFoundError) =>
|
|
91
|
+
Effect.fail(new ProviderNotFoundError({ providerID: error.providerID, message: error.message })),
|
|
92
|
+
"Artifact.InvalidError": (error: Artifact.InvalidError) =>
|
|
93
|
+
Effect.fail(
|
|
94
|
+
new InvalidRequestError({ message: error.message, kind: "artifact_invalid", field: error.field }),
|
|
95
|
+
),
|
|
96
|
+
"Artifact.ProviderError": (error: Artifact.ProviderError) =>
|
|
97
|
+
Effect.fail(new ServiceUnavailableError({ message: error.message, service: error.providerID })),
|
|
98
|
+
"Session.NotFoundError": (error: Session.NotFoundError) => Effect.fail(missingSession(error)),
|
|
99
|
+
} as never),
|
|
100
|
+
)
|
|
101
|
+
|
|
102
|
+
return {
|
|
103
|
+
providers: (
|
|
104
|
+
query: { readonly location?: { readonly directory?: string } },
|
|
105
|
+
request: HttpServerRequest.HttpServerRequest,
|
|
106
|
+
) =>
|
|
107
|
+
inLocation(
|
|
108
|
+
request,
|
|
109
|
+
{ location: query.location },
|
|
110
|
+
Effect.gen(function* () {
|
|
111
|
+
const artifacts = yield* Artifact.Service
|
|
112
|
+
return yield* response(artifacts.providers())
|
|
113
|
+
}),
|
|
114
|
+
).pipe(mapCommon),
|
|
115
|
+
|
|
116
|
+
list: (
|
|
117
|
+
query: {
|
|
118
|
+
readonly location?: { readonly directory?: string }
|
|
119
|
+
readonly sessionID?: Session.ID
|
|
120
|
+
readonly provider?: Artifact.ProviderID
|
|
121
|
+
},
|
|
122
|
+
request: HttpServerRequest.HttpServerRequest,
|
|
123
|
+
) =>
|
|
124
|
+
inLocation(
|
|
125
|
+
request,
|
|
126
|
+
{ location: query.location, sessionID: query.sessionID },
|
|
127
|
+
Effect.gen(function* () {
|
|
128
|
+
const artifacts = yield* Artifact.Service
|
|
129
|
+
return yield* response(artifacts.list({ sessionID: query.sessionID, provider: query.provider }))
|
|
130
|
+
}),
|
|
131
|
+
).pipe(mapCommon),
|
|
132
|
+
|
|
133
|
+
create: (
|
|
134
|
+
query: { readonly location?: { readonly directory?: string } },
|
|
135
|
+
payload: Artifact.CreateInput,
|
|
136
|
+
request: HttpServerRequest.HttpServerRequest,
|
|
137
|
+
) =>
|
|
138
|
+
inLocation(
|
|
139
|
+
request,
|
|
140
|
+
{ location: query.location, sessionID: payload.sessionID },
|
|
141
|
+
Effect.gen(function* () {
|
|
142
|
+
const artifacts = yield* Artifact.Service
|
|
143
|
+
return yield* response(artifacts.create(payload))
|
|
144
|
+
}),
|
|
145
|
+
).pipe(mapCommon),
|
|
146
|
+
|
|
147
|
+
get: (
|
|
148
|
+
artifactID: Artifact.ID,
|
|
149
|
+
query: { readonly location?: { readonly directory?: string } },
|
|
150
|
+
request: HttpServerRequest.HttpServerRequest,
|
|
151
|
+
) =>
|
|
152
|
+
inLocation(
|
|
153
|
+
request,
|
|
154
|
+
{ location: query.location, artifactID },
|
|
155
|
+
Effect.gen(function* () {
|
|
156
|
+
const artifacts = yield* Artifact.Service
|
|
157
|
+
return yield* response(artifacts.get(artifactID))
|
|
158
|
+
}),
|
|
159
|
+
).pipe(mapCommon),
|
|
160
|
+
|
|
161
|
+
update: (
|
|
162
|
+
artifactID: Artifact.ID,
|
|
163
|
+
query: { readonly location?: { readonly directory?: string } },
|
|
164
|
+
payload: Artifact.UpdateInput,
|
|
165
|
+
request: HttpServerRequest.HttpServerRequest,
|
|
166
|
+
) =>
|
|
167
|
+
inLocation(
|
|
168
|
+
request,
|
|
169
|
+
{ location: query.location, sessionID: payload.sessionID, artifactID },
|
|
170
|
+
Effect.gen(function* () {
|
|
171
|
+
const artifacts = yield* Artifact.Service
|
|
172
|
+
return yield* response(artifacts.update(artifactID, payload))
|
|
173
|
+
}),
|
|
174
|
+
).pipe(mapCommon),
|
|
175
|
+
|
|
176
|
+
remove: (
|
|
177
|
+
artifactID: Artifact.ID,
|
|
178
|
+
query: { readonly location?: { readonly directory?: string } },
|
|
179
|
+
request: HttpServerRequest.HttpServerRequest,
|
|
180
|
+
) =>
|
|
181
|
+
inLocation(
|
|
182
|
+
request,
|
|
183
|
+
{ location: query.location, artifactID },
|
|
184
|
+
Effect.gen(function* () {
|
|
185
|
+
const artifacts = yield* Artifact.Service
|
|
186
|
+
yield* artifacts.remove(artifactID)
|
|
187
|
+
return HttpApiSchema.NoContent.make()
|
|
188
|
+
}),
|
|
189
|
+
).pipe(mapCommon),
|
|
190
|
+
|
|
191
|
+
view: (
|
|
192
|
+
artifactID: Artifact.ID,
|
|
193
|
+
query: { readonly location?: { readonly directory?: string } },
|
|
194
|
+
request: HttpServerRequest.HttpServerRequest,
|
|
195
|
+
) =>
|
|
196
|
+
inLocation(
|
|
197
|
+
request,
|
|
198
|
+
{ location: query.location, artifactID },
|
|
199
|
+
Effect.gen(function* () {
|
|
200
|
+
const artifacts = yield* Artifact.Service
|
|
201
|
+
const parsed = new URL(request.url, "http://localhost")
|
|
202
|
+
const prefix = `/api/experimental/artifacts/${encodeURIComponent(artifactID)}/view`
|
|
203
|
+
if (parsed.pathname === prefix) {
|
|
204
|
+
yield* artifacts.info(artifactID)
|
|
205
|
+
return HttpServerResponse.empty({
|
|
206
|
+
status: 308,
|
|
207
|
+
headers: { location: `${prefix}/${parsed.search}` },
|
|
208
|
+
})
|
|
209
|
+
}
|
|
210
|
+
const subpath = extractViewSubpath(request.url, artifactID)
|
|
211
|
+
const file = yield* artifacts.readLocalFile(artifactID, subpath).pipe(
|
|
212
|
+
Effect.catchTag("Artifact.FileMissingError", (error) =>
|
|
213
|
+
Effect.fail(new FileNotFoundError({ path: error.path, message: error.message })),
|
|
214
|
+
),
|
|
215
|
+
)
|
|
216
|
+
return HttpServerResponse.uint8Array(file.bytes, {
|
|
217
|
+
status: 200,
|
|
218
|
+
contentType: file.mime,
|
|
219
|
+
headers: {
|
|
220
|
+
"access-control-allow-origin": "*",
|
|
221
|
+
"cache-control": "private, no-cache",
|
|
222
|
+
"content-security-policy": "sandbox allow-scripts",
|
|
223
|
+
"x-content-type-options": "nosniff",
|
|
224
|
+
},
|
|
225
|
+
})
|
|
226
|
+
}),
|
|
227
|
+
).pipe(mapCommon),
|
|
228
|
+
}
|
|
229
|
+
})
|
|
230
|
+
|
|
231
|
+
function extractViewSubpath(url: string, artifactID: Artifact.ID) {
|
|
232
|
+
const pathname = new URL(url, "http://localhost").pathname
|
|
233
|
+
const prefix = `/api/experimental/artifacts/${encodeURIComponent(artifactID)}/view/`
|
|
234
|
+
if (!pathname.startsWith(prefix)) return undefined
|
|
235
|
+
const raw = pathname.slice(prefix.length)
|
|
236
|
+
if (!raw) return undefined
|
|
237
|
+
try {
|
|
238
|
+
return decodeURIComponent(raw)
|
|
239
|
+
} catch {
|
|
240
|
+
return raw
|
|
241
|
+
}
|
|
242
|
+
}
|
|
@@ -1,63 +1,63 @@
|
|
|
1
|
-
import { Automation } from "@zyro-sec/core/automation"
|
|
2
|
-
import { Permission } from "@zyro-sec/core/permission"
|
|
3
|
-
import { AutomationNotFoundError, ForbiddenError, InvalidRequestError } from "@zyro-sec/protocol/errors"
|
|
4
|
-
import type { CreatePayload, ListQuery } from "@zyro-sec/protocol/groups/automation"
|
|
5
|
-
import { Effect } from "effect"
|
|
6
|
-
import { HttpApiSchema } from "effect/http-api"
|
|
7
|
-
import { missingSession } from "./session-error"
|
|
8
|
-
|
|
9
|
-
/** `automation.*` in the `server.experimental` group. Every route requires automations to be enabled where it runs. */
|
|
10
|
-
export const makeAutomation = Effect.gen(function* () {
|
|
11
|
-
const automations = yield* Automation.Service
|
|
12
|
-
|
|
13
|
-
return {
|
|
14
|
-
create: (payload: CreatePayload) =>
|
|
15
|
-
// Stateless HTTP callers have no guaranteed UI listener to answer an interactive ask, so settle asks unattended.
|
|
16
|
-
automations.create(payload).pipe(
|
|
17
|
-
Effect.provideService(Permission.Unattended, []),
|
|
18
|
-
Effect.map((data) => ({ data })),
|
|
19
|
-
Effect.catchTags({
|
|
20
|
-
"Automation.InvalidError": (error) =>
|
|
21
|
-
Effect.fail(new InvalidRequestError({ message: error.message, kind: "automation" })),
|
|
22
|
-
"Automation.DisabledError": () => Effect.fail(disabled()),
|
|
23
|
-
"Permission.BlockedError": (error) => Effect.fail(new ForbiddenError({ message: error.message })),
|
|
24
|
-
"Permission.CorrectedError": (error) => Effect.fail(new ForbiddenError({ message: error.feedback })),
|
|
25
|
-
"Session.NotFoundError": (error) => Effect.fail(missingSession(error)),
|
|
26
|
-
}),
|
|
27
|
-
),
|
|
28
|
-
list: (query: ListQuery) =>
|
|
29
|
-
automations.list(query).pipe(
|
|
30
|
-
Effect.map((data) => ({ data })),
|
|
31
|
-
Effect.catchTags({
|
|
32
|
-
"Automation.InvalidError": (error) =>
|
|
33
|
-
Effect.fail(new InvalidRequestError({ message: error.message, kind: "automation" })),
|
|
34
|
-
"Automation.DisabledError": () => Effect.fail(disabled()),
|
|
35
|
-
"Session.NotFoundError": (error) => Effect.fail(missingSession(error)),
|
|
36
|
-
}),
|
|
37
|
-
),
|
|
38
|
-
get: (id: Automation.ID) => managed(automations.get(id).pipe(Effect.map((data) => ({ data })))),
|
|
39
|
-
pause: (id: Automation.ID) => managed(automations.pause(id).pipe(Effect.map((data) => ({ data })))),
|
|
40
|
-
resume: (id: Automation.ID) => managed(automations.resume(id).pipe(Effect.map((data) => ({ data })))),
|
|
41
|
-
remove: (id: Automation.ID) => managed(automations.remove(id).pipe(Effect.as(HttpApiSchema.NoContent.make()))),
|
|
42
|
-
}
|
|
43
|
-
})
|
|
44
|
-
|
|
45
|
-
function managed<A>(effect: Effect.Effect<A, Automation.NotFoundError | Automation.DisabledError>) {
|
|
46
|
-
return effect.pipe(
|
|
47
|
-
Effect.catchTags({
|
|
48
|
-
"Automation.NotFoundError": (error) => Effect.fail(missingAutomation(error)),
|
|
49
|
-
"Automation.DisabledError": () => Effect.fail(disabled()),
|
|
50
|
-
}),
|
|
51
|
-
)
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
function missingAutomation(error: { readonly automationID: Automation.ID }) {
|
|
55
|
-
return new AutomationNotFoundError({
|
|
56
|
-
automationID: error.automationID,
|
|
57
|
-
message: `Automation not found: ${error.automationID}`,
|
|
58
|
-
})
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
function disabled() {
|
|
62
|
-
return new ForbiddenError({ message: new Automation.DisabledError().message })
|
|
63
|
-
}
|
|
1
|
+
import { Automation } from "@zyro-sec/core/automation"
|
|
2
|
+
import { Permission } from "@zyro-sec/core/permission"
|
|
3
|
+
import { AutomationNotFoundError, ForbiddenError, InvalidRequestError } from "@zyro-sec/protocol/errors"
|
|
4
|
+
import type { CreatePayload, ListQuery } from "@zyro-sec/protocol/groups/automation"
|
|
5
|
+
import { Effect } from "effect"
|
|
6
|
+
import { HttpApiSchema } from "effect/http-api"
|
|
7
|
+
import { missingSession } from "./session-error"
|
|
8
|
+
|
|
9
|
+
/** `automation.*` in the `server.experimental` group. Every route requires automations to be enabled where it runs. */
|
|
10
|
+
export const makeAutomation = Effect.gen(function* () {
|
|
11
|
+
const automations = yield* Automation.Service
|
|
12
|
+
|
|
13
|
+
return {
|
|
14
|
+
create: (payload: CreatePayload) =>
|
|
15
|
+
// Stateless HTTP callers have no guaranteed UI listener to answer an interactive ask, so settle asks unattended.
|
|
16
|
+
automations.create(payload).pipe(
|
|
17
|
+
Effect.provideService(Permission.Unattended, []),
|
|
18
|
+
Effect.map((data) => ({ data })),
|
|
19
|
+
Effect.catchTags({
|
|
20
|
+
"Automation.InvalidError": (error) =>
|
|
21
|
+
Effect.fail(new InvalidRequestError({ message: error.message, kind: "automation" })),
|
|
22
|
+
"Automation.DisabledError": () => Effect.fail(disabled()),
|
|
23
|
+
"Permission.BlockedError": (error) => Effect.fail(new ForbiddenError({ message: error.message })),
|
|
24
|
+
"Permission.CorrectedError": (error) => Effect.fail(new ForbiddenError({ message: error.feedback })),
|
|
25
|
+
"Session.NotFoundError": (error) => Effect.fail(missingSession(error)),
|
|
26
|
+
}),
|
|
27
|
+
),
|
|
28
|
+
list: (query: ListQuery) =>
|
|
29
|
+
automations.list(query).pipe(
|
|
30
|
+
Effect.map((data) => ({ data })),
|
|
31
|
+
Effect.catchTags({
|
|
32
|
+
"Automation.InvalidError": (error) =>
|
|
33
|
+
Effect.fail(new InvalidRequestError({ message: error.message, kind: "automation" })),
|
|
34
|
+
"Automation.DisabledError": () => Effect.fail(disabled()),
|
|
35
|
+
"Session.NotFoundError": (error) => Effect.fail(missingSession(error)),
|
|
36
|
+
}),
|
|
37
|
+
),
|
|
38
|
+
get: (id: Automation.ID) => managed(automations.get(id).pipe(Effect.map((data) => ({ data })))),
|
|
39
|
+
pause: (id: Automation.ID) => managed(automations.pause(id).pipe(Effect.map((data) => ({ data })))),
|
|
40
|
+
resume: (id: Automation.ID) => managed(automations.resume(id).pipe(Effect.map((data) => ({ data })))),
|
|
41
|
+
remove: (id: Automation.ID) => managed(automations.remove(id).pipe(Effect.as(HttpApiSchema.NoContent.make()))),
|
|
42
|
+
}
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
function managed<A>(effect: Effect.Effect<A, Automation.NotFoundError | Automation.DisabledError>) {
|
|
46
|
+
return effect.pipe(
|
|
47
|
+
Effect.catchTags({
|
|
48
|
+
"Automation.NotFoundError": (error) => Effect.fail(missingAutomation(error)),
|
|
49
|
+
"Automation.DisabledError": () => Effect.fail(disabled()),
|
|
50
|
+
}),
|
|
51
|
+
)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function missingAutomation(error: { readonly automationID: Automation.ID }) {
|
|
55
|
+
return new AutomationNotFoundError({
|
|
56
|
+
automationID: error.automationID,
|
|
57
|
+
message: `Automation not found: ${error.automationID}`,
|
|
58
|
+
})
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function disabled() {
|
|
62
|
+
return new ForbiddenError({ message: new Automation.DisabledError().message })
|
|
63
|
+
}
|