@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.
Files changed (130) hide show
  1. package/package.json +8 -8
  2. package/script/build.ts +28 -28
  3. package/script/pack.ts +5 -5
  4. package/script/workerd-probe.ts +59 -59
  5. package/src/api.ts +12 -12
  6. package/src/auth.ts +70 -70
  7. package/src/content-headers.ts +32 -32
  8. package/src/cors.ts +34 -34
  9. package/src/event-feed.ts +93 -93
  10. package/src/fetch.ts +67 -67
  11. package/src/handlers/agent.ts +23 -23
  12. package/src/handlers/artifact.ts +242 -242
  13. package/src/handlers/automation.ts +63 -63
  14. package/src/handlers/blob.ts +55 -55
  15. package/src/handlers/command.ts +8 -8
  16. package/src/handlers/config.ts +26 -26
  17. package/src/handlers/credential.ts +87 -87
  18. package/src/handlers/debug.ts +57 -57
  19. package/src/handlers/event.ts +38 -38
  20. package/src/handlers/form.ts +15 -15
  21. package/src/handlers/fs.ts +180 -180
  22. package/src/handlers/generate.ts +117 -117
  23. package/src/handlers/integration.ts +310 -310
  24. package/src/handlers/job.ts +29 -29
  25. package/src/handlers/location.ts +33 -33
  26. package/src/handlers/machine.ts +336 -336
  27. package/src/handlers/mcp.ts +160 -160
  28. package/src/handlers/media.ts +44 -44
  29. package/src/handlers/message.ts +82 -82
  30. package/src/handlers/migration.ts +13 -13
  31. package/src/handlers/model.ts +25 -25
  32. package/src/handlers/permission.ts +131 -131
  33. package/src/handlers/persistent-pty.ts +285 -285
  34. package/src/handlers/plugin.ts +97 -97
  35. package/src/handlers/project.ts +23 -23
  36. package/src/handlers/provider.ts +32 -32
  37. package/src/handlers/pty-socket.ts +47 -47
  38. package/src/handlers/pty.ts +181 -181
  39. package/src/handlers/push.ts +67 -67
  40. package/src/handlers/reference.ts +8 -8
  41. package/src/handlers/rpc.ts +31 -31
  42. package/src/handlers/server.ts +58 -58
  43. package/src/handlers/session-error.ts +47 -47
  44. package/src/handlers/session.ts +722 -722
  45. package/src/handlers/shell.ts +72 -72
  46. package/src/handlers/skill.ts +34 -34
  47. package/src/handlers/usage.ts +23 -23
  48. package/src/handlers/vcs.ts +104 -104
  49. package/src/handlers/voice.ts +50 -50
  50. package/src/handlers/websearch.ts +50 -50
  51. package/src/handlers/worktree.ts +60 -60
  52. package/src/handlers.ts +69 -69
  53. package/src/location.ts +86 -86
  54. package/src/media-uri.ts +13 -13
  55. package/src/middleware/authorization.ts +111 -111
  56. package/src/middleware/defect-response.ts +39 -39
  57. package/src/middleware/form-location.ts +44 -44
  58. package/src/middleware/schema-error.ts +49 -49
  59. package/src/middleware/session-location.ts +30 -30
  60. package/src/options.ts +61 -61
  61. package/src/pairing.ts +36 -36
  62. package/src/process.ts +274 -274
  63. package/src/range.ts +16 -16
  64. package/src/request-site.ts +36 -36
  65. package/src/request-tracing.ts +9 -9
  66. package/src/routes.ts +261 -261
  67. package/src/server-info.ts +41 -41
  68. package/src/service-status.ts +30 -30
  69. package/src/workerd.ts +184 -184
  70. package/test/fixture/mcp-starts.cjs +20 -20
  71. package/test/fixture/routes.ts +37 -37
  72. package/test/fixture/server.ts +20 -20
  73. package/test/fixture/worktree-delegate/index.ts +13 -13
  74. package/test/fixture/worktree-plugin/index.ts +47 -47
  75. package/tsconfig.build.json +15 -15
  76. package/tsconfig.json +11 -11
  77. package/test/artifact.test.ts +0 -224
  78. package/test/auth.test.ts +0 -24
  79. package/test/automation.test.ts +0 -315
  80. package/test/client-mcp.test.ts +0 -369
  81. package/test/codemode-api.test.ts +0 -88
  82. package/test/config.test.ts +0 -104
  83. package/test/cors.test.ts +0 -24
  84. package/test/defect-response.test.ts +0 -83
  85. package/test/event-feed.test.ts +0 -201
  86. package/test/fetch-option.test.ts +0 -104
  87. package/test/fetch.test.ts +0 -698
  88. package/test/flag-channel.test.ts +0 -126
  89. package/test/fs.test.ts +0 -439
  90. package/test/generate.test.ts +0 -132
  91. package/test/handler-policy.test.ts +0 -43
  92. package/test/heap-dump.test.ts +0 -137
  93. package/test/integration-policy.test.ts +0 -93
  94. package/test/job.test.ts +0 -186
  95. package/test/machine-iroh.test.ts +0 -259
  96. package/test/machine-link.test.ts +0 -1234
  97. package/test/machine-routes.test.ts +0 -198
  98. package/test/media.test.ts +0 -343
  99. package/test/message.test.ts +0 -105
  100. package/test/model.test.ts +0 -62
  101. package/test/options.test.ts +0 -42
  102. package/test/permission-saved.test.ts +0 -73
  103. package/test/persistent-pty.test.ts +0 -1007
  104. package/test/process.test.ts +0 -261
  105. package/test/provider.test.ts +0 -88
  106. package/test/pty-socket.property.test.ts +0 -240
  107. package/test/pty-socket.test.ts +0 -89
  108. package/test/pty-transport.test.ts +0 -200
  109. package/test/push-routes.test.ts +0 -157
  110. package/test/request-site.test.ts +0 -71
  111. package/test/request-tracing.test.ts +0 -87
  112. package/test/rpc-handler-errors.test.ts +0 -76
  113. package/test/schema-error.test.ts +0 -68
  114. package/test/service-status.test.ts +0 -34
  115. package/test/session-admit-location.test.ts +0 -92
  116. package/test/session-create.test.ts +0 -65
  117. package/test/session-diff.test.ts +0 -100
  118. package/test/session-form.test.ts +0 -50
  119. package/test/session-import.test.ts +0 -152
  120. package/test/session-instances.test.ts +0 -335
  121. package/test/session-unarchive.test.ts +0 -73
  122. package/test/session-update.test.ts +0 -66
  123. package/test/skill.test.ts +0 -57
  124. package/test/sse-failure.test.ts +0 -71
  125. package/test/usage.test.ts +0 -88
  126. package/test/vcs.test.ts +0 -356
  127. package/test/video-generate.test.ts +0 -131
  128. package/test/voice.test.ts +0 -170
  129. package/test/workerd.test.ts +0 -227
  130. package/test/worktree.test.ts +0 -333
@@ -1,55 +1,55 @@
1
- import { BlobStore } from "@zyro-sec/core/blob-store"
2
- import { BlobNotFoundError } from "@zyro-sec/protocol/errors"
3
- import { FSUtil } from "@zyro-sec/util/fs-util"
4
- import { Effect } from "effect"
5
- import { type HttpServerRequest, HttpServerResponse } from "effect/http"
6
- import { ContentHeaders } from "../content-headers"
7
- import { ByteRange } from "../range"
8
-
9
- /** `blob.get` in the `server.experimental` group. Raw, so it can stream the file and answer Range and 304 itself. */
10
- export const makeBlobGet = Effect.gen(function* () {
11
- const blobs = yield* BlobStore.Service
12
- const fs = yield* FSUtil.Service
13
- return (id: BlobStore.ID, request: HttpServerRequest.HttpServerRequest) =>
14
- Effect.gen(function* () {
15
- const blob = yield* blobs
16
- .read(id)
17
- .pipe(Effect.mapError((error) => new BlobNotFoundError({ blobID: error.id, message: error.message })))
18
- // The ID names one immutable blob.
19
- const etag = `"${id}"`
20
- const headers = {
21
- ...ContentHeaders.download({ mime: blob.mime }),
22
- "cache-control": "private, max-age=31536000, immutable",
23
- etag,
24
- "accept-ranges": "bytes",
25
- }
26
- if (
27
- request.headers["if-none-match"]
28
- ?.split(",")
29
- .some((tag) => tag.trim() === "*" || tag.trim().replace(/^w\//i, "") === etag)
30
- )
31
- return HttpServerResponse.empty({ status: 304, headers })
32
- // If-Range takes only a strong match (RFC 9110 13.1.5); anything else serves the whole body.
33
- const ifRange = request.headers["if-range"]
34
- const range =
35
- ifRange === undefined || ifRange.trim() === etag
36
- ? ByteRange.parse(request.headers["range"], blob.bytes)
37
- : undefined
38
- if (range === "unsatisfiable")
39
- return HttpServerResponse.empty({
40
- status: 416,
41
- headers: { ...headers, "content-range": `bytes */${blob.bytes}` },
42
- })
43
- const offset = range?.start ?? 0
44
- const length = range === undefined ? blob.bytes : range.end - range.start + 1
45
- return HttpServerResponse.stream(fs.stream(blob.path, { offset, bytesToRead: length }), {
46
- status: range === undefined ? 200 : 206,
47
- contentType: headers["content-type"],
48
- contentLength: length,
49
- headers:
50
- range === undefined
51
- ? headers
52
- : { ...headers, "content-range": `bytes ${range.start}-${range.end}/${blob.bytes}` },
53
- })
54
- })
55
- })
1
+ import { BlobStore } from "@zyro-sec/core/blob-store"
2
+ import { BlobNotFoundError } from "@zyro-sec/protocol/errors"
3
+ import { FSUtil } from "@zyro-sec/util/fs-util"
4
+ import { Effect } from "effect"
5
+ import { type HttpServerRequest, HttpServerResponse } from "effect/http"
6
+ import { ContentHeaders } from "../content-headers"
7
+ import { ByteRange } from "../range"
8
+
9
+ /** `blob.get` in the `server.experimental` group. Raw, so it can stream the file and answer Range and 304 itself. */
10
+ export const makeBlobGet = Effect.gen(function* () {
11
+ const blobs = yield* BlobStore.Service
12
+ const fs = yield* FSUtil.Service
13
+ return (id: BlobStore.ID, request: HttpServerRequest.HttpServerRequest) =>
14
+ Effect.gen(function* () {
15
+ const blob = yield* blobs
16
+ .read(id)
17
+ .pipe(Effect.mapError((error) => new BlobNotFoundError({ blobID: error.id, message: error.message })))
18
+ // The ID names one immutable blob.
19
+ const etag = `"${id}"`
20
+ const headers = {
21
+ ...ContentHeaders.download({ mime: blob.mime }),
22
+ "cache-control": "private, max-age=31536000, immutable",
23
+ etag,
24
+ "accept-ranges": "bytes",
25
+ }
26
+ if (
27
+ request.headers["if-none-match"]
28
+ ?.split(",")
29
+ .some((tag) => tag.trim() === "*" || tag.trim().replace(/^w\//i, "") === etag)
30
+ )
31
+ return HttpServerResponse.empty({ status: 304, headers })
32
+ // If-Range takes only a strong match (RFC 9110 13.1.5); anything else serves the whole body.
33
+ const ifRange = request.headers["if-range"]
34
+ const range =
35
+ ifRange === undefined || ifRange.trim() === etag
36
+ ? ByteRange.parse(request.headers["range"], blob.bytes)
37
+ : undefined
38
+ if (range === "unsatisfiable")
39
+ return HttpServerResponse.empty({
40
+ status: 416,
41
+ headers: { ...headers, "content-range": `bytes */${blob.bytes}` },
42
+ })
43
+ const offset = range?.start ?? 0
44
+ const length = range === undefined ? blob.bytes : range.end - range.start + 1
45
+ return HttpServerResponse.stream(fs.stream(blob.path, { offset, bytesToRead: length }), {
46
+ status: range === undefined ? 200 : 206,
47
+ contentType: headers["content-type"],
48
+ contentLength: length,
49
+ headers:
50
+ range === undefined
51
+ ? headers
52
+ : { ...headers, "content-range": `bytes ${range.start}-${range.end}/${blob.bytes}` },
53
+ })
54
+ })
55
+ })
@@ -1,8 +1,8 @@
1
- import { Command } from "@zyro-sec/core/command"
2
- import { HttpApiBuilder } from "effect/http-api"
3
- import { Api } from "../api"
4
- import { response } from "../location"
5
-
6
- export const CommandHandler = HttpApiBuilder.group(Api, "server.command", (handlers) =>
7
- handlers.handle("command.list", () => response(Command.Service.use((command) => command.list()))),
8
- )
1
+ import { Command } from "@zyro-sec/core/command"
2
+ import { HttpApiBuilder } from "effect/http-api"
3
+ import { Api } from "../api"
4
+ import { response } from "../location"
5
+
6
+ export const CommandHandler = HttpApiBuilder.group(Api, "server.command", (handlers) =>
7
+ handlers.handle("command.list", () => response(Command.Service.use((command) => command.list()))),
8
+ )
@@ -1,26 +1,26 @@
1
- import { Config } from "@zyro-sec/core/config"
2
- import { ShellSelect } from "@zyro-sec/core/shell/select"
3
- import { Effect } from "effect"
4
- import { HttpApiBuilder } from "effect/http-api"
5
- import { Api } from "../api"
6
-
7
- export const ConfigHandler = HttpApiBuilder.group(Api, "server.config", (handlers) =>
8
- handlers
9
- .handle("config.get", () => Config.Service.use((config) => config.entries()))
10
- .handle(
11
- "config.update",
12
- Effect.fn(function* (ctx) {
13
- const config = yield* Config.Service
14
- if (!config.update) return yield* Effect.die(new Error("Config updates are unavailable"))
15
- return yield* config.update(ctx.payload).pipe(Effect.orDie)
16
- }),
17
- )
18
- .handle(
19
- "config.shells",
20
- Effect.fn(function* () {
21
- const shell = yield* ShellSelect.Service
22
- if (!shell.list) return yield* Effect.die(new Error("Shell discovery is unavailable"))
23
- return yield* shell.list()
24
- }),
25
- ),
26
- )
1
+ import { Config } from "@zyro-sec/core/config"
2
+ import { ShellSelect } from "@zyro-sec/core/shell/select"
3
+ import { Effect } from "effect"
4
+ import { HttpApiBuilder } from "effect/http-api"
5
+ import { Api } from "../api"
6
+
7
+ export const ConfigHandler = HttpApiBuilder.group(Api, "server.config", (handlers) =>
8
+ handlers
9
+ .handle("config.get", () => Config.Service.use((config) => config.entries()))
10
+ .handle(
11
+ "config.update",
12
+ Effect.fn(function* (ctx) {
13
+ const config = yield* Config.Service
14
+ if (!config.update) return yield* Effect.die(new Error("Config updates are unavailable"))
15
+ return yield* config.update(ctx.payload).pipe(Effect.orDie)
16
+ }),
17
+ )
18
+ .handle(
19
+ "config.shells",
20
+ Effect.fn(function* () {
21
+ const shell = yield* ShellSelect.Service
22
+ if (!shell.list) return yield* Effect.die(new Error("Shell discovery is unavailable"))
23
+ return yield* shell.list()
24
+ }),
25
+ ),
26
+ )
@@ -1,87 +1,87 @@
1
- import { Credential } from "@zyro-sec/core/credential"
2
- import { FileSystem } from "@zyro-sec/core/filesystem"
3
- import { Integration } from "@zyro-sec/core/integration"
4
- import { Location } from "@zyro-sec/core/location"
5
- import { LocationServiceMap } from "@zyro-sec/core/location-services"
6
- import { AbsolutePath } from "@zyro-sec/core/schema"
7
- import { ConflictError } from "@zyro-sec/protocol/errors"
8
- import { Global } from "@zyro-sec/util/global"
9
- import { Effect } from "effect"
10
- import { HttpApiBuilder, HttpApiSchema } from "effect/http-api"
11
- import { Api } from "../api"
12
- import { requireConnectable } from "./integration"
13
-
14
- export const CredentialHandler = HttpApiBuilder.group(Api, "server.credential", (handlers) =>
15
- Effect.gen(function* () {
16
- const global = yield* Global.Service
17
- const locations = yield* LocationServiceMap.Service
18
- // Credentials are global, so `integration.connect` is read from the global config Location, which carries managed
19
- // policy, the Console document, and user config.
20
- const services = locations.get(Location.Ref.make({ directory: AbsolutePath.make(global.config) }))
21
- return handlers
22
- .handle(
23
- "credential.list",
24
- Effect.fn(function* () {
25
- const credential = yield* Credential.Service
26
- return { data: entries(yield* credential.all()) }
27
- }),
28
- )
29
- .handle(
30
- "credential.create",
31
- Effect.fn(function* (ctx) {
32
- yield* requireConnectable(ctx.payload.integrationID).pipe(
33
- Effect.provide(services),
34
- // The global config directory always exists.
35
- Effect.catchIf((error) => error instanceof FileSystem.DirectoryNotFoundError, Effect.die),
36
- )
37
- const credential = yield* Credential.Service
38
- if (ctx.payload.id && (yield* credential.get(ctx.payload.id)))
39
- return yield* new ConflictError({
40
- resource: ctx.payload.id,
41
- message: `Credential already exists: ${ctx.payload.id}`,
42
- })
43
- const created = yield* credential.create(ctx.payload)
44
- const entry = entries(yield* credential.list(created.integrationID)).find((item) => item.id === created.id)
45
- return { data: entry ?? { ...created, active: false } }
46
- }),
47
- )
48
- .handle(
49
- "credential.update",
50
- Effect.fn(function* (ctx) {
51
- const credential = yield* Credential.Service
52
- yield* credential.update(ctx.params.credentialID, { label: ctx.payload.label })
53
- return HttpApiSchema.NoContent.make()
54
- }),
55
- )
56
- .handle(
57
- "credential.activate",
58
- Effect.fn(function* (ctx) {
59
- const credential = yield* Credential.Service
60
- yield* credential.activate(ctx.params.credentialID)
61
- return HttpApiSchema.NoContent.make()
62
- }),
63
- )
64
- .handle(
65
- "credential.remove",
66
- Effect.fn(function* (ctx) {
67
- // Removal goes through Integration so a sign-in method can revoke the credential remotely first.
68
- // The global config directory always exists.
69
- const integrations = yield* Integration.Service.pipe(Effect.provide(services), Effect.orDie)
70
- yield* integrations.connection.remove(ctx.params.credentialID)
71
- return HttpApiSchema.NoContent.make()
72
- }),
73
- )
74
- }),
75
- )
76
-
77
- // Credential listings order each integration's selected credential last.
78
- function entries(credentials: Credential.Info[]) {
79
- const selected = new Map(credentials.map((item) => [item.integrationID, item.id]))
80
- return credentials.map((item) => ({
81
- id: item.id,
82
- integrationID: item.integrationID,
83
- label: item.label,
84
- active: selected.get(item.integrationID) === item.id,
85
- value: item.value,
86
- }))
87
- }
1
+ import { Credential } from "@zyro-sec/core/credential"
2
+ import { FileSystem } from "@zyro-sec/core/filesystem"
3
+ import { Integration } from "@zyro-sec/core/integration"
4
+ import { Location } from "@zyro-sec/core/location"
5
+ import { LocationServiceMap } from "@zyro-sec/core/location-services"
6
+ import { AbsolutePath } from "@zyro-sec/core/schema"
7
+ import { ConflictError } from "@zyro-sec/protocol/errors"
8
+ import { Global } from "@zyro-sec/util/global"
9
+ import { Effect } from "effect"
10
+ import { HttpApiBuilder, HttpApiSchema } from "effect/http-api"
11
+ import { Api } from "../api"
12
+ import { requireConnectable } from "./integration"
13
+
14
+ export const CredentialHandler = HttpApiBuilder.group(Api, "server.credential", (handlers) =>
15
+ Effect.gen(function* () {
16
+ const global = yield* Global.Service
17
+ const locations = yield* LocationServiceMap.Service
18
+ // Credentials are global, so `integration.connect` is read from the global config Location, which carries managed
19
+ // policy, the Console document, and user config.
20
+ const services = locations.get(Location.Ref.make({ directory: AbsolutePath.make(global.config) }))
21
+ return handlers
22
+ .handle(
23
+ "credential.list",
24
+ Effect.fn(function* () {
25
+ const credential = yield* Credential.Service
26
+ return { data: entries(yield* credential.all()) }
27
+ }),
28
+ )
29
+ .handle(
30
+ "credential.create",
31
+ Effect.fn(function* (ctx) {
32
+ yield* requireConnectable(ctx.payload.integrationID).pipe(
33
+ Effect.provide(services),
34
+ // The global config directory always exists.
35
+ Effect.catchIf((error) => error instanceof FileSystem.DirectoryNotFoundError, Effect.die),
36
+ )
37
+ const credential = yield* Credential.Service
38
+ if (ctx.payload.id && (yield* credential.get(ctx.payload.id)))
39
+ return yield* new ConflictError({
40
+ resource: ctx.payload.id,
41
+ message: `Credential already exists: ${ctx.payload.id}`,
42
+ })
43
+ const created = yield* credential.create(ctx.payload)
44
+ const entry = entries(yield* credential.list(created.integrationID)).find((item) => item.id === created.id)
45
+ return { data: entry ?? { ...created, active: false } }
46
+ }),
47
+ )
48
+ .handle(
49
+ "credential.update",
50
+ Effect.fn(function* (ctx) {
51
+ const credential = yield* Credential.Service
52
+ yield* credential.update(ctx.params.credentialID, { label: ctx.payload.label })
53
+ return HttpApiSchema.NoContent.make()
54
+ }),
55
+ )
56
+ .handle(
57
+ "credential.activate",
58
+ Effect.fn(function* (ctx) {
59
+ const credential = yield* Credential.Service
60
+ yield* credential.activate(ctx.params.credentialID)
61
+ return HttpApiSchema.NoContent.make()
62
+ }),
63
+ )
64
+ .handle(
65
+ "credential.remove",
66
+ Effect.fn(function* (ctx) {
67
+ // Removal goes through Integration so a sign-in method can revoke the credential remotely first.
68
+ // The global config directory always exists.
69
+ const integrations = yield* Integration.Service.pipe(Effect.provide(services), Effect.orDie)
70
+ yield* integrations.connection.remove(ctx.params.credentialID)
71
+ return HttpApiSchema.NoContent.make()
72
+ }),
73
+ )
74
+ }),
75
+ )
76
+
77
+ // Credential listings order each integration's selected credential last.
78
+ function entries(credentials: Credential.Info[]) {
79
+ const selected = new Map(credentials.map((item) => [item.integrationID, item.id]))
80
+ return credentials.map((item) => ({
81
+ id: item.id,
82
+ integrationID: item.integrationID,
83
+ label: item.label,
84
+ active: selected.get(item.integrationID) === item.id,
85
+ value: item.value,
86
+ }))
87
+ }
@@ -1,57 +1,57 @@
1
- import { LocationServiceMap } from "@zyro-sec/core/location-service-map"
2
- import { InvalidRequestError, UnknownError } from "@zyro-sec/protocol/errors"
3
- import { Global } from "@zyro-sec/util/global"
4
- import { HeapSnapshot } from "@zyro-sec/util/heap-snapshot"
5
- import { Effect, Option, RcMap } from "effect"
6
- import { HttpApiBuilder } from "effect/http-api"
7
- import path from "node:path"
8
- import { Api } from "../api"
9
- import { requestRef } from "../location"
10
-
11
- export const DebugHandler = HttpApiBuilder.group(Api, "server.debug", (handlers) =>
12
- Effect.gen(function* () {
13
- const global = yield* Global.Service
14
- return handlers
15
- .handle(
16
- "debug.heapDump",
17
- Effect.fn("server.debug.heapDump")(function* (ctx) {
18
- const directory = ctx.query.directory
19
- if (directory !== undefined && !path.isAbsolute(directory))
20
- return yield* new InvalidRequestError({ message: "directory must be an absolute path", field: "directory" })
21
- return yield* HeapSnapshot.write(directory ?? global.log).pipe(
22
- Effect.catch((cause) => {
23
- const ref = `err_${crypto.randomUUID().slice(0, 8)}`
24
- return Effect.logError("failed to write heap snapshot", { cause }).pipe(
25
- Effect.annotateLogs({ ref }),
26
- Effect.andThen(
27
- // The reason (a missing permission, a file where the directory should be) is usually the caller's to fix.
28
- Effect.fail(
29
- new UnknownError({
30
- message: `Failed to write heap snapshot: ${cause.cause instanceof Error ? cause.cause.message : cause.message}`,
31
- ref,
32
- }),
33
- ),
34
- ),
35
- )
36
- }),
37
- )
38
- }),
39
- )
40
- .handle(
41
- "debug.location",
42
- Effect.fn(function* () {
43
- const locations = Option.getOrThrow(yield* Effect.serviceOption(LocationServiceMap.Service))
44
- return Array.from(yield* RcMap.keys(locations.rcMap))
45
- }),
46
- )
47
- .handle(
48
- "debug.location.evict",
49
- Effect.fn(function* (ctx) {
50
- const locations = Option.getOrThrow(yield* Effect.serviceOption(LocationServiceMap.Service))
51
- // Resolve through requestRef so the key matches the shape the location
52
- // middleware cached the services under.
53
- yield* locations.invalidate(requestRef(ctx.request))
54
- }),
55
- )
56
- }),
57
- )
1
+ import { LocationServiceMap } from "@zyro-sec/core/location-service-map"
2
+ import { InvalidRequestError, UnknownError } from "@zyro-sec/protocol/errors"
3
+ import { Global } from "@zyro-sec/util/global"
4
+ import { HeapSnapshot } from "@zyro-sec/util/heap-snapshot"
5
+ import { Effect, Option, RcMap } from "effect"
6
+ import { HttpApiBuilder } from "effect/http-api"
7
+ import path from "node:path"
8
+ import { Api } from "../api"
9
+ import { requestRef } from "../location"
10
+
11
+ export const DebugHandler = HttpApiBuilder.group(Api, "server.debug", (handlers) =>
12
+ Effect.gen(function* () {
13
+ const global = yield* Global.Service
14
+ return handlers
15
+ .handle(
16
+ "debug.heapDump",
17
+ Effect.fn("server.debug.heapDump")(function* (ctx) {
18
+ const directory = ctx.query.directory
19
+ if (directory !== undefined && !path.isAbsolute(directory))
20
+ return yield* new InvalidRequestError({ message: "directory must be an absolute path", field: "directory" })
21
+ return yield* HeapSnapshot.write(directory ?? global.log).pipe(
22
+ Effect.catch((cause) => {
23
+ const ref = `err_${crypto.randomUUID().slice(0, 8)}`
24
+ return Effect.logError("failed to write heap snapshot", { cause }).pipe(
25
+ Effect.annotateLogs({ ref }),
26
+ Effect.andThen(
27
+ // The reason (a missing permission, a file where the directory should be) is usually the caller's to fix.
28
+ Effect.fail(
29
+ new UnknownError({
30
+ message: `Failed to write heap snapshot: ${cause.cause instanceof Error ? cause.cause.message : cause.message}`,
31
+ ref,
32
+ }),
33
+ ),
34
+ ),
35
+ )
36
+ }),
37
+ )
38
+ }),
39
+ )
40
+ .handle(
41
+ "debug.location",
42
+ Effect.fn(function* () {
43
+ const locations = Option.getOrThrow(yield* Effect.serviceOption(LocationServiceMap.Service))
44
+ return Array.from(yield* RcMap.keys(locations.rcMap))
45
+ }),
46
+ )
47
+ .handle(
48
+ "debug.location.evict",
49
+ Effect.fn(function* (ctx) {
50
+ const locations = Option.getOrThrow(yield* Effect.serviceOption(LocationServiceMap.Service))
51
+ // Resolve through requestRef so the key matches the shape the location
52
+ // middleware cached the services under.
53
+ yield* locations.invalidate(requestRef(ctx.request))
54
+ }),
55
+ )
56
+ }),
57
+ )
@@ -1,38 +1,38 @@
1
- import { Event } from "@zyro-sec/schema/event"
2
- import { Effect, Stream } from "effect"
3
- import { HttpServerResponse } from "effect/http"
4
- import { HttpApiBuilder } from "effect/http-api"
5
- import { Api } from "../api"
6
- import { EventFeed } from "../event-feed"
7
-
8
- export const EventHandler = HttpApiBuilder.group(Api, "server.event", (handlers) =>
9
- Effect.gen(function* () {
10
- const feed = yield* EventFeed.Service
11
- return handlers.handleRaw("event.subscribe", (ctx) =>
12
- Effect.gen(function* () {
13
- const connected = {
14
- id: Event.ID.create(),
15
- type: "server.connected",
16
- data: {},
17
- } as const
18
- const output = Stream.unwrap(
19
- feed
20
- .subscribe(ctx.query.types)
21
- .pipe(Effect.map((live) => Stream.make(EventFeed.frame(connected)).pipe(Stream.concat(live)))),
22
- )
23
- const heartbeat = Stream.tick("15 seconds").pipe(Stream.map(() => ": heartbeat\n\n"))
24
- return HttpServerResponse.stream(
25
- output.pipe(Stream.merge(heartbeat, { haltStrategy: "left" }), Stream.encodeText),
26
- {
27
- contentType: "text/event-stream",
28
- headers: {
29
- "Cache-Control": "no-cache, no-transform",
30
- "X-Accel-Buffering": "no",
31
- "X-Content-Type-Options": "nosniff",
32
- },
33
- },
34
- )
35
- }),
36
- )
37
- }),
38
- )
1
+ import { Event } from "@zyro-sec/schema/event"
2
+ import { Effect, Stream } from "effect"
3
+ import { HttpServerResponse } from "effect/http"
4
+ import { HttpApiBuilder } from "effect/http-api"
5
+ import { Api } from "../api"
6
+ import { EventFeed } from "../event-feed"
7
+
8
+ export const EventHandler = HttpApiBuilder.group(Api, "server.event", (handlers) =>
9
+ Effect.gen(function* () {
10
+ const feed = yield* EventFeed.Service
11
+ return handlers.handleRaw("event.subscribe", (ctx) =>
12
+ Effect.gen(function* () {
13
+ const connected = {
14
+ id: Event.ID.create(),
15
+ type: "server.connected",
16
+ data: {},
17
+ } as const
18
+ const output = Stream.unwrap(
19
+ feed
20
+ .subscribe(ctx.query.types)
21
+ .pipe(Effect.map((live) => Stream.make(EventFeed.frame(connected)).pipe(Stream.concat(live)))),
22
+ )
23
+ const heartbeat = Stream.tick("15 seconds").pipe(Stream.map(() => ": heartbeat\n\n"))
24
+ return HttpServerResponse.stream(
25
+ output.pipe(Stream.merge(heartbeat, { haltStrategy: "left" }), Stream.encodeText),
26
+ {
27
+ contentType: "text/event-stream",
28
+ headers: {
29
+ "Cache-Control": "no-cache, no-transform",
30
+ "X-Accel-Buffering": "no",
31
+ "X-Content-Type-Options": "nosniff",
32
+ },
33
+ },
34
+ )
35
+ }),
36
+ )
37
+ }),
38
+ )
@@ -1,15 +1,15 @@
1
- import { Form } from "@zyro-sec/core/form"
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 FormHandler = HttpApiBuilder.group(Api, "server.form", (handlers) =>
8
- handlers.handle(
9
- "form.list",
10
- Effect.fn(function* () {
11
- const form = yield* Form.Service
12
- return yield* response(form.list())
13
- }),
14
- ),
15
- )
1
+ import { Form } from "@zyro-sec/core/form"
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 FormHandler = HttpApiBuilder.group(Api, "server.form", (handlers) =>
8
+ handlers.handle(
9
+ "form.list",
10
+ Effect.fn(function* () {
11
+ const form = yield* Form.Service
12
+ return yield* response(form.list())
13
+ }),
14
+ ),
15
+ )