@zyro-sec/server 1.0.1 → 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 +11 -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
|
@@ -1,126 +0,0 @@
|
|
|
1
|
-
import { expect } from "bun:test"
|
|
2
|
-
import { Agent } from "@zyro-sec/core/agent"
|
|
3
|
-
import { App } from "@zyro-sec/core/app"
|
|
4
|
-
import { Database } from "@zyro-sec/core/database/database"
|
|
5
|
-
import { llmClient } from "@zyro-sec/core/effect/app-node-platform"
|
|
6
|
-
import { Flag } from "@zyro-sec/core/flag"
|
|
7
|
-
import { Image } from "@zyro-sec/core/image"
|
|
8
|
-
import { LocationServiceMap } from "@zyro-sec/core/location-service-map"
|
|
9
|
-
import { Session } from "@zyro-sec/core/session"
|
|
10
|
-
import { SessionBlob } from "@zyro-sec/core/session/blob"
|
|
11
|
-
import { SessionRunnerModel } from "@zyro-sec/core/session/runner/model"
|
|
12
|
-
import { BlobTable, SessionMessageTable } from "@zyro-sec/core/session/sql"
|
|
13
|
-
import { SessionStored } from "@zyro-sec/core/session/stored"
|
|
14
|
-
import { Location } from "@zyro-sec/schema/location"
|
|
15
|
-
import { AbsolutePath } from "@zyro-sec/schema/schema"
|
|
16
|
-
import { Global } from "@zyro-sec/util/global"
|
|
17
|
-
import { Context, Effect, Layer } from "effect"
|
|
18
|
-
import { HttpServer } from "effect/http"
|
|
19
|
-
import { LanguageModel, LLMClient } from "../../ai/src/effect"
|
|
20
|
-
import { OpenAIChat } from "../../ai/src/protocols/openai-chat"
|
|
21
|
-
import { TestLLM } from "../../ai/src/testing"
|
|
22
|
-
import { tempGlobalLayer } from "../../core/test/fixture/global"
|
|
23
|
-
import { tmpdirScoped } from "../../core/test/fixture/tmpdir"
|
|
24
|
-
import { it } from "../../core/test/lib/effect"
|
|
25
|
-
import { imagePassthrough } from "../../core/test/lib/image"
|
|
26
|
-
import { createEmbeddedRoutes } from "../src/routes"
|
|
27
|
-
|
|
28
|
-
const pngBase64 = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII="
|
|
29
|
-
const pngHash = SessionBlob.hashBytes(Buffer.from(pngBase64, "base64"))
|
|
30
|
-
|
|
31
|
-
const runChannel = (channel: "dev" | "latest") =>
|
|
32
|
-
Effect.gen(function* () {
|
|
33
|
-
const dir = yield* tmpdirScoped(`opencode-flag-channel-${channel}-`)
|
|
34
|
-
const location = Location.Ref.make({ directory: AbsolutePath.make(dir.path) })
|
|
35
|
-
const llm = yield* TestLLM.Test.pipe(Effect.provide(TestLLM.testLayer()))
|
|
36
|
-
const model = SessionRunnerModel.resolved(
|
|
37
|
-
LanguageModel.make({ id: "flag-channel-model", provider: "test", route: OpenAIChat.route }),
|
|
38
|
-
{
|
|
39
|
-
capabilities: { tools: true, input: ["text", "image"], output: ["text"] },
|
|
40
|
-
cost: [],
|
|
41
|
-
limit: { context: 200_000, output: 8_192 },
|
|
42
|
-
},
|
|
43
|
-
)
|
|
44
|
-
|
|
45
|
-
const context = yield* Layer.build(
|
|
46
|
-
createEmbeddedRoutes(
|
|
47
|
-
{
|
|
48
|
-
app: { channel, version: "test" },
|
|
49
|
-
database: { path: ":memory:" },
|
|
50
|
-
config: { project: false, claude: false },
|
|
51
|
-
models: { fetch: false, snapshot: false },
|
|
52
|
-
fs: { filewatcher: false, fff: false },
|
|
53
|
-
},
|
|
54
|
-
[
|
|
55
|
-
Global.node.replace(tempGlobalLayer),
|
|
56
|
-
llmClient.replace(Layer.succeed(LLMClient.Service, llm)),
|
|
57
|
-
SessionRunnerModel.node.replace(
|
|
58
|
-
Layer.succeed(SessionRunnerModel.Service, { select: (session) => Effect.fail(new SessionRunnerModel.ModelNotSelectedError({ sessionID: session.id })), resolve: () => Effect.succeed(model) }),
|
|
59
|
-
),
|
|
60
|
-
Image.node.replace(imagePassthrough),
|
|
61
|
-
],
|
|
62
|
-
).pipe(Layer.provide(HttpServer.layerServices)),
|
|
63
|
-
)
|
|
64
|
-
|
|
65
|
-
const sessions = Context.get(context, Session.Service)
|
|
66
|
-
const locations = Context.get(context, LocationServiceMap.Service)
|
|
67
|
-
const { db } = Context.get(context, Database.Service)
|
|
68
|
-
expect(Context.get(context, App.Service)).toEqual({ name: "opencode", version: "test", channel })
|
|
69
|
-
|
|
70
|
-
const session = yield* sessions.create({
|
|
71
|
-
title: `channel-${channel}`,
|
|
72
|
-
agent: Agent.ID.make("build"),
|
|
73
|
-
model: model.ref,
|
|
74
|
-
location,
|
|
75
|
-
})
|
|
76
|
-
|
|
77
|
-
expect(yield* App.Service.pipe(Effect.provide(locations.get(session.location)))).toEqual({
|
|
78
|
-
name: "opencode",
|
|
79
|
-
version: "test",
|
|
80
|
-
channel,
|
|
81
|
-
})
|
|
82
|
-
|
|
83
|
-
yield* llm.push(TestLLM.text("done", "text_1"))
|
|
84
|
-
|
|
85
|
-
const admitted = yield* sessions.prompt({
|
|
86
|
-
sessionID: session.id,
|
|
87
|
-
text: "store image",
|
|
88
|
-
files: [{ uri: `data:image/png;base64,${pngBase64}`, name: "shot.png" }],
|
|
89
|
-
})
|
|
90
|
-
yield* sessions.wait(session.id)
|
|
91
|
-
|
|
92
|
-
const blobs = yield* db
|
|
93
|
-
.select({ hash: BlobTable.hash })
|
|
94
|
-
.from(BlobTable)
|
|
95
|
-
.all()
|
|
96
|
-
.pipe(Effect.orDie)
|
|
97
|
-
const rows = yield* db
|
|
98
|
-
.select()
|
|
99
|
-
.from(SessionMessageTable)
|
|
100
|
-
.all()
|
|
101
|
-
.pipe(Effect.orDie)
|
|
102
|
-
const userRow = rows.find((row) => row.id === admitted.id)
|
|
103
|
-
|
|
104
|
-
return {
|
|
105
|
-
blobs,
|
|
106
|
-
userFiles: userRow && "files" in userRow.data ? userRow.data.files : undefined,
|
|
107
|
-
}
|
|
108
|
-
}).pipe(Effect.provideService(Flag.Overrides, undefined), Effect.scoped)
|
|
109
|
-
|
|
110
|
-
it.live("enables media_blob by default on dev builds", () =>
|
|
111
|
-
Effect.gen(function* () {
|
|
112
|
-
expect(yield* runChannel("dev")).toEqual({
|
|
113
|
-
blobs: [{ hash: pngHash }],
|
|
114
|
-
userFiles: [{ uri: SessionStored.ref(pngHash), mime: "image/png", name: "shot.png", source: { type: "inline" } }],
|
|
115
|
-
})
|
|
116
|
-
}),
|
|
117
|
-
)
|
|
118
|
-
|
|
119
|
-
it.live("keeps media_blob off by default on latest builds", () =>
|
|
120
|
-
Effect.gen(function* () {
|
|
121
|
-
expect(yield* runChannel("latest")).toEqual({
|
|
122
|
-
blobs: [],
|
|
123
|
-
userFiles: [{ data: pngBase64, mime: "image/png", name: "shot.png", source: { type: "inline" } }],
|
|
124
|
-
})
|
|
125
|
-
}),
|
|
126
|
-
)
|
package/test/fs.test.ts
DELETED
|
@@ -1,439 +0,0 @@
|
|
|
1
|
-
import fs from "node:fs/promises"
|
|
2
|
-
import path from "node:path"
|
|
3
|
-
import { expect } from "bun:test"
|
|
4
|
-
import { Effect, Schedule } from "effect"
|
|
5
|
-
import { tmpdirScoped } from "../../core/test/fixture/tmpdir"
|
|
6
|
-
import { it } from "../../core/test/lib/effect"
|
|
7
|
-
import { startServer } from "./fixture/server"
|
|
8
|
-
|
|
9
|
-
it.live(
|
|
10
|
-
"browsing parents and siblings reuses the current Location and its MCP process",
|
|
11
|
-
() =>
|
|
12
|
-
Effect.gen(function* () {
|
|
13
|
-
const tmp = yield* tmpdirScoped()
|
|
14
|
-
const current = path.join(tmp.path, "root", "current")
|
|
15
|
-
const starts = path.join(tmp.path, "starts")
|
|
16
|
-
yield* Effect.promise(async () => {
|
|
17
|
-
await fs.mkdir(current, { recursive: true })
|
|
18
|
-
await fs.mkdir(path.join(tmp.path, "root", ".git"))
|
|
19
|
-
await fs.mkdir(path.join(tmp.path, "root", "sibling", "nested"), { recursive: true })
|
|
20
|
-
await fs.writeFile(path.join(tmp.path, "root", "sibling", "file.txt"), "outside")
|
|
21
|
-
await fs.writeFile(starts, "")
|
|
22
|
-
await fs.writeFile(
|
|
23
|
-
path.join(tmp.path, "root", "opencode.json"),
|
|
24
|
-
JSON.stringify({
|
|
25
|
-
mcp: {
|
|
26
|
-
servers: {
|
|
27
|
-
filesystem: {
|
|
28
|
-
type: "local",
|
|
29
|
-
command: [process.execPath, path.join(import.meta.dir, "fixture", "mcp-starts.cjs"), starts],
|
|
30
|
-
},
|
|
31
|
-
},
|
|
32
|
-
},
|
|
33
|
-
}),
|
|
34
|
-
)
|
|
35
|
-
})
|
|
36
|
-
const server = yield* startServer(path.join(tmp.path, "config"))
|
|
37
|
-
const list = (directory: string) =>
|
|
38
|
-
Effect.promise(async () => {
|
|
39
|
-
const url = new URL("/api/fs/list", server.base)
|
|
40
|
-
url.searchParams.set("location[directory]", current)
|
|
41
|
-
url.searchParams.set("path", directory)
|
|
42
|
-
const response = await fetch(url, { headers: server.headers })
|
|
43
|
-
expect(response.status).toBe(200)
|
|
44
|
-
const result = await response.json()
|
|
45
|
-
expect(result.location.directory).toBe(current)
|
|
46
|
-
return result.data
|
|
47
|
-
})
|
|
48
|
-
const loaded = Effect.promise(async () => {
|
|
49
|
-
const response = await fetch(new URL("/api/debug/location", server.base), { headers: server.headers })
|
|
50
|
-
expect(response.status).toBe(200)
|
|
51
|
-
return response.json()
|
|
52
|
-
})
|
|
53
|
-
const count = Effect.promise(
|
|
54
|
-
async () => (await fs.readFile(starts, "utf8")).trim().split("\n").filter(Boolean).length,
|
|
55
|
-
)
|
|
56
|
-
|
|
57
|
-
yield* list(".")
|
|
58
|
-
expect(yield* loaded).toEqual([{ directory: current }])
|
|
59
|
-
expect(
|
|
60
|
-
yield* count.pipe(
|
|
61
|
-
Effect.repeat({ while: (n) => n === 0, schedule: Schedule.spaced("25 millis") }),
|
|
62
|
-
Effect.timeout("5 seconds"),
|
|
63
|
-
),
|
|
64
|
-
).toBe(1)
|
|
65
|
-
|
|
66
|
-
yield* list("..")
|
|
67
|
-
const sibling = yield* list("../sibling")
|
|
68
|
-
expect(sibling).toEqual([
|
|
69
|
-
{ path: path.join("..", "sibling", "nested") + path.sep, type: "directory" },
|
|
70
|
-
{ path: path.join("..", "sibling", "file.txt"), type: "file" },
|
|
71
|
-
])
|
|
72
|
-
expect(yield* list(path.join(tmp.path, "root", "sibling"))).toEqual(sibling)
|
|
73
|
-
yield* list("../sibling/nested")
|
|
74
|
-
yield* list("../sibling")
|
|
75
|
-
expect(yield* loaded).toEqual([{ directory: current }])
|
|
76
|
-
expect(yield* count).toBe(1)
|
|
77
|
-
}),
|
|
78
|
-
15_000,
|
|
79
|
-
)
|
|
80
|
-
|
|
81
|
-
it.live(
|
|
82
|
-
"streams files with Range, conditional requests, confinement, and multi-megabyte byte integrity",
|
|
83
|
-
() =>
|
|
84
|
-
Effect.gen(function* () {
|
|
85
|
-
const tmp = yield* tmpdirScoped()
|
|
86
|
-
const current = path.join(tmp.path, "project")
|
|
87
|
-
const outside = path.join(tmp.path, "outside")
|
|
88
|
-
const largeBytes = Uint8Array.from({ length: 4 * 1024 * 1024 + 321 }, (_, i) => (i * 31 + (i >> 8)) & 0xff)
|
|
89
|
-
|
|
90
|
-
yield* Effect.promise(async () => {
|
|
91
|
-
await fs.mkdir(path.join(current, "nested"), { recursive: true })
|
|
92
|
-
await fs.mkdir(outside, { recursive: true })
|
|
93
|
-
await fs.writeFile(path.join(current, "clip.mp4"), "0123456789")
|
|
94
|
-
await fs.writeFile(path.join(current, "empty.bin"), "")
|
|
95
|
-
await fs.writeFile(path.join(current, "large.mp4"), largeBytes)
|
|
96
|
-
await fs.writeFile(path.join(current, "notes.txt"), "compressible ".repeat(1024))
|
|
97
|
-
await fs.writeFile(path.join(outside, "secret.txt"), "secret")
|
|
98
|
-
await fs.symlink(outside, path.join(current, "escape-link"), "junction")
|
|
99
|
-
})
|
|
100
|
-
|
|
101
|
-
const server = yield* startServer(path.join(tmp.path, "config"))
|
|
102
|
-
const readUrl = (relativePath: string) => {
|
|
103
|
-
const url = new URL(`/api/fs/read/${relativePath}`, server.base)
|
|
104
|
-
url.searchParams.set("location[directory]", current)
|
|
105
|
-
return url
|
|
106
|
-
}
|
|
107
|
-
const request = (relativePath: string, init?: BunFetchRequestInit) =>
|
|
108
|
-
Effect.promise(() =>
|
|
109
|
-
fetch(readUrl(relativePath), {
|
|
110
|
-
...init,
|
|
111
|
-
headers: {
|
|
112
|
-
...server.headers,
|
|
113
|
-
...init?.headers,
|
|
114
|
-
},
|
|
115
|
-
}),
|
|
116
|
-
)
|
|
117
|
-
|
|
118
|
-
// 1. Full 200 read headers and body
|
|
119
|
-
const full = yield* request("clip.mp4")
|
|
120
|
-
expect(full.status).toBe(200)
|
|
121
|
-
expect(full.headers.get("accept-ranges")).toBe("bytes")
|
|
122
|
-
expect(full.headers.get("content-length")).toBe("10")
|
|
123
|
-
expect(full.headers.get("content-type")).toBe("video/mp4")
|
|
124
|
-
expect(full.headers.get("content-encoding")).toBeNull()
|
|
125
|
-
expect(full.headers.get("cache-control")).toBe("no-cache, no-transform")
|
|
126
|
-
const etag = full.headers.get("etag")
|
|
127
|
-
const lastModified = full.headers.get("last-modified")
|
|
128
|
-
expect(etag).toMatch(/^W\/"[0-9a-f]+-[0-9a-f]+"$/)
|
|
129
|
-
expect(lastModified).toBeTruthy()
|
|
130
|
-
expect(yield* Effect.promise(() => full.text())).toBe("0123456789")
|
|
131
|
-
|
|
132
|
-
// 2. HEAD probe (and HEAD with Range ignores Range)
|
|
133
|
-
const head = yield* request("clip.mp4", { method: "HEAD", headers: { Range: "bytes=0-3" } })
|
|
134
|
-
expect(head.status).toBe(200)
|
|
135
|
-
expect(head.headers.get("accept-ranges")).toBe("bytes")
|
|
136
|
-
expect(head.headers.get("content-length")).toBe("10")
|
|
137
|
-
expect(head.headers.get("content-type")).toBe("video/mp4")
|
|
138
|
-
expect(head.headers.get("content-range")).toBeNull()
|
|
139
|
-
expect(head.headers.get("etag")).toBe(etag)
|
|
140
|
-
expect(head.headers.get("last-modified")).toBe(lastModified)
|
|
141
|
-
expect(yield* Effect.promise(() => head.text())).toBe("")
|
|
142
|
-
|
|
143
|
-
// 3. Explicit, open-ended, clamped, and suffix Range forms -> 206 Partial Content
|
|
144
|
-
yield* Effect.forEach(
|
|
145
|
-
[
|
|
146
|
-
["bytes=0-0", "0", "bytes 0-0/10"],
|
|
147
|
-
["bytes=2-5", "2345", "bytes 2-5/10"],
|
|
148
|
-
["bytes=7-99", "789", "bytes 7-9/10"],
|
|
149
|
-
["bytes=6-", "6789", "bytes 6-9/10"],
|
|
150
|
-
["bytes=-3", "789", "bytes 7-9/10"],
|
|
151
|
-
["bytes=-99", "0123456789", "bytes 0-9/10"],
|
|
152
|
-
] as const,
|
|
153
|
-
([range, expectedBody, expectedContentRange]) =>
|
|
154
|
-
Effect.gen(function* () {
|
|
155
|
-
const partial = yield* request("clip.mp4", { headers: { Range: range } })
|
|
156
|
-
expect({ range, status: partial.status }).toEqual({ range, status: 206 })
|
|
157
|
-
expect(partial.headers.get("accept-ranges")).toBe("bytes")
|
|
158
|
-
expect(partial.headers.get("content-range")).toBe(expectedContentRange)
|
|
159
|
-
expect(partial.headers.get("content-length")).toBe(String(expectedBody.length))
|
|
160
|
-
expect(partial.headers.get("content-type")).toBe("video/mp4")
|
|
161
|
-
expect(partial.headers.get("cache-control")).toBe("no-cache, no-transform")
|
|
162
|
-
expect(partial.headers.get("etag")).toBe(etag)
|
|
163
|
-
expect(partial.headers.get("last-modified")).toBe(lastModified)
|
|
164
|
-
expect(yield* Effect.promise(() => partial.text())).toBe(expectedBody)
|
|
165
|
-
}),
|
|
166
|
-
)
|
|
167
|
-
|
|
168
|
-
// 4. Unsatisfiable Range -> 416 with Content-Range: bytes */size
|
|
169
|
-
yield* Effect.forEach(
|
|
170
|
-
[
|
|
171
|
-
["clip.mp4", "bytes=10-12", 10],
|
|
172
|
-
["clip.mp4", "bytes=10-", 10],
|
|
173
|
-
["clip.mp4", "bytes=5-2", 10],
|
|
174
|
-
["clip.mp4", "bytes=-0", 10],
|
|
175
|
-
["empty.bin", "bytes=0-0", 0],
|
|
176
|
-
] as const,
|
|
177
|
-
([file, range, total]) =>
|
|
178
|
-
Effect.gen(function* () {
|
|
179
|
-
const unsatisfiable = yield* request(file, { headers: { Range: range } })
|
|
180
|
-
expect({ file, range, status: unsatisfiable.status }).toEqual({ file, range, status: 416 })
|
|
181
|
-
expect(unsatisfiable.headers.get("content-range")).toBe(`bytes */${total}`)
|
|
182
|
-
expect(yield* Effect.promise(() => unsatisfiable.text())).toBe("")
|
|
183
|
-
}),
|
|
184
|
-
)
|
|
185
|
-
|
|
186
|
-
// 5. Multi-range and malformed Range headers fall back to full 200
|
|
187
|
-
yield* Effect.forEach(
|
|
188
|
-
["bytes=0-1, 4-5", "items=0-3", "bytes=", "bytes=-", "bytes=abc-def", "bytes=0-1-2"] as const,
|
|
189
|
-
(range) =>
|
|
190
|
-
Effect.gen(function* () {
|
|
191
|
-
const fallback = yield* request("clip.mp4", { headers: { Range: range } })
|
|
192
|
-
expect({ range, status: fallback.status }).toEqual({ range, status: 200 })
|
|
193
|
-
expect(fallback.headers.get("content-range")).toBeNull()
|
|
194
|
-
expect(fallback.headers.get("content-length")).toBe("10")
|
|
195
|
-
expect(yield* Effect.promise(() => fallback.text())).toBe("0123456789")
|
|
196
|
-
}),
|
|
197
|
-
)
|
|
198
|
-
|
|
199
|
-
// 6. Conditional requests: If-None-Match & If-Modified-Since -> 304 Not Modified
|
|
200
|
-
if (!etag || !lastModified) return yield* Effect.die(new Error("Missing ETag or Last-Modified"))
|
|
201
|
-
const strongEtag = etag.slice(2)
|
|
202
|
-
yield* Effect.forEach(
|
|
203
|
-
[
|
|
204
|
-
["If-None-Match", etag],
|
|
205
|
-
["If-None-Match", strongEtag],
|
|
206
|
-
["If-None-Match", `"other", ${etag}`],
|
|
207
|
-
["If-None-Match", "*"],
|
|
208
|
-
["If-Modified-Since", lastModified],
|
|
209
|
-
] as const,
|
|
210
|
-
([header, value]) =>
|
|
211
|
-
Effect.gen(function* () {
|
|
212
|
-
const notModified = yield* request("clip.mp4", { headers: { [header]: value } })
|
|
213
|
-
expect(notModified.status).toBe(304)
|
|
214
|
-
expect(notModified.headers.get("etag")).toBe(etag)
|
|
215
|
-
expect(notModified.headers.get("last-modified")).toBe(lastModified)
|
|
216
|
-
expect(notModified.headers.get("cache-control")).toBe("no-cache, no-transform")
|
|
217
|
-
expect(yield* Effect.promise(() => notModified.text())).toBe("")
|
|
218
|
-
}),
|
|
219
|
-
)
|
|
220
|
-
|
|
221
|
-
const modifiedSincePast = yield* request("clip.mp4", {
|
|
222
|
-
headers: { "If-Modified-Since": "Wed, 01 Jan 2020 00:00:00 GMT" },
|
|
223
|
-
})
|
|
224
|
-
expect(modifiedSincePast.status).toBe(200)
|
|
225
|
-
expect(yield* Effect.promise(() => modifiedSincePast.text())).toBe("0123456789")
|
|
226
|
-
|
|
227
|
-
// Mismatched If-None-Match takes precedence over matching If-Modified-Since
|
|
228
|
-
const mismatchIfNoneMatch = yield* request("clip.mp4", {
|
|
229
|
-
headers: { "If-None-Match": 'W/"mismatch"', "If-Modified-Since": lastModified },
|
|
230
|
-
})
|
|
231
|
-
expect(mismatchIfNoneMatch.status).toBe(200)
|
|
232
|
-
expect(yield* Effect.promise(() => mismatchIfNoneMatch.text())).toBe("0123456789")
|
|
233
|
-
|
|
234
|
-
// 7. If-Range matching -> 206; If-Range mismatch -> full 200
|
|
235
|
-
yield* Effect.forEach([etag, strongEtag, lastModified], (ifRange) =>
|
|
236
|
-
Effect.gen(function* () {
|
|
237
|
-
const matchedRange = yield* request("clip.mp4", {
|
|
238
|
-
headers: { Range: "bytes=1-4", "If-Range": ifRange },
|
|
239
|
-
})
|
|
240
|
-
expect(matchedRange.status).toBe(206)
|
|
241
|
-
expect(matchedRange.headers.get("content-range")).toBe("bytes 1-4/10")
|
|
242
|
-
expect(yield* Effect.promise(() => matchedRange.text())).toBe("1234")
|
|
243
|
-
}),
|
|
244
|
-
)
|
|
245
|
-
|
|
246
|
-
yield* Effect.forEach(['W/"stale"', '"stale"', "Wed, 01 Jan 2020 00:00:00 GMT", "not-a-date"], (ifRange) =>
|
|
247
|
-
Effect.gen(function* () {
|
|
248
|
-
const mismatchedRange = yield* request("clip.mp4", {
|
|
249
|
-
headers: { Range: "bytes=1-4", "If-Range": ifRange },
|
|
250
|
-
})
|
|
251
|
-
expect(mismatchedRange.status).toBe(200)
|
|
252
|
-
expect(mismatchedRange.headers.get("content-range")).toBeNull()
|
|
253
|
-
expect(mismatchedRange.headers.get("content-length")).toBe("10")
|
|
254
|
-
expect(yield* Effect.promise(() => mismatchedRange.text())).toBe("0123456789")
|
|
255
|
-
}),
|
|
256
|
-
)
|
|
257
|
-
|
|
258
|
-
// 8. Confinement enforced (404 FileNotFoundError instead of 500)
|
|
259
|
-
yield* Effect.forEach(["..%2Foutside%2Fsecret.txt", "escape-link/secret.txt", "nested"], (forbiddenPath) =>
|
|
260
|
-
Effect.gen(function* () {
|
|
261
|
-
const escaped = yield* request(forbiddenPath)
|
|
262
|
-
expect({ forbiddenPath, status: escaped.status }).toEqual({ forbiddenPath, status: 404 })
|
|
263
|
-
expect(yield* Effect.promise(() => escaped.json())).toMatchObject({
|
|
264
|
-
_tag: "FileNotFoundError",
|
|
265
|
-
})
|
|
266
|
-
}),
|
|
267
|
-
)
|
|
268
|
-
|
|
269
|
-
// 9. Compressible files keep their exact Content-Length instead of being gzipped
|
|
270
|
-
const text = yield* request("notes.txt", { headers: { "Accept-Encoding": "gzip" }, decompress: false })
|
|
271
|
-
expect(text.status).toBe(200)
|
|
272
|
-
expect(text.headers.get("content-encoding")).toBeNull()
|
|
273
|
-
expect(text.headers.get("content-length")).toBe(String("compressible ".length * 1024))
|
|
274
|
-
expect(yield* Effect.promise(() => text.text())).toBe("compressible ".repeat(1024))
|
|
275
|
-
|
|
276
|
-
// 10. Multi-megabyte file streams byte-for-byte on full read and range slice
|
|
277
|
-
const fullLarge = yield* request("large.mp4")
|
|
278
|
-
expect(fullLarge.status).toBe(200)
|
|
279
|
-
expect(fullLarge.headers.get("content-length")).toBe(String(largeBytes.length))
|
|
280
|
-
const fullLargeReceived = new Uint8Array(yield* Effect.promise(() => fullLarge.arrayBuffer()))
|
|
281
|
-
expect(fullLargeReceived.length).toBe(largeBytes.length)
|
|
282
|
-
expect(Buffer.compare(fullLargeReceived, largeBytes)).toBe(0)
|
|
283
|
-
|
|
284
|
-
const sliceStart = 1024 * 1024 + 17
|
|
285
|
-
const sliceEnd = 2 * 1024 * 1024 + 519
|
|
286
|
-
const partialLarge = yield* request("large.mp4", {
|
|
287
|
-
headers: { Range: `bytes=${sliceStart}-${sliceEnd}` },
|
|
288
|
-
})
|
|
289
|
-
expect(partialLarge.status).toBe(206)
|
|
290
|
-
expect(partialLarge.headers.get("content-range")).toBe(`bytes ${sliceStart}-${sliceEnd}/${largeBytes.length}`)
|
|
291
|
-
expect(partialLarge.headers.get("content-length")).toBe(String(sliceEnd - sliceStart + 1))
|
|
292
|
-
const partialLargeReceived = new Uint8Array(yield* Effect.promise(() => partialLarge.arrayBuffer()))
|
|
293
|
-
expect(Buffer.compare(partialLargeReceived, largeBytes.subarray(sliceStart, sliceEnd + 1))).toBe(0)
|
|
294
|
-
}),
|
|
295
|
-
15_000,
|
|
296
|
-
)
|
|
297
|
-
|
|
298
|
-
it.live("serves file reads as downloads with explicit content headers", () =>
|
|
299
|
-
Effect.gen(function* () {
|
|
300
|
-
const tmp = yield* tmpdirScoped()
|
|
301
|
-
yield* Effect.promise(async () => {
|
|
302
|
-
await fs.writeFile(path.join(tmp.path, "page.html"), "<!doctype html><script>document.title = 'x'</script>")
|
|
303
|
-
await fs.writeFile(path.join(tmp.path, "logo.svg"), '<svg xmlns="http://www.w3.org/2000/svg"/>')
|
|
304
|
-
await fs.writeFile(path.join(tmp.path, "feed.xml"), "<feed/>")
|
|
305
|
-
await fs.writeFile(path.join(tmp.path, "app.js"), "export {}")
|
|
306
|
-
await fs.writeFile(path.join(tmp.path, "photo.png"), "png")
|
|
307
|
-
await fs.writeFile(path.join(tmp.path, "clip.mp4"), "0123456789")
|
|
308
|
-
await fs.writeFile(path.join(tmp.path, "naïve 100% (1).txt"), "text")
|
|
309
|
-
})
|
|
310
|
-
const server = yield* startServer(path.join(tmp.path, "config"))
|
|
311
|
-
const request = (file: string, init?: BunFetchRequestInit) =>
|
|
312
|
-
Effect.promise(() => {
|
|
313
|
-
const url = new URL(`/api/fs/read/${encodeURIComponent(file)}`, server.base)
|
|
314
|
-
url.searchParams.set("location[directory]", tmp.path)
|
|
315
|
-
return fetch(url, { ...init, headers: { ...server.headers, ...init?.headers } })
|
|
316
|
-
})
|
|
317
|
-
const expectDownload = (response: Response, filename: string) => {
|
|
318
|
-
expect(response.headers.get("content-disposition")).toBe(
|
|
319
|
-
`attachment; filename="${filename}"; filename*=UTF-8''${filename}`,
|
|
320
|
-
)
|
|
321
|
-
expect(response.headers.get("content-security-policy")).toBe(
|
|
322
|
-
"default-src 'none'; img-src 'self'; media-src 'self'; sandbox",
|
|
323
|
-
)
|
|
324
|
-
expect(response.headers.get("x-content-type-options")).toBe("nosniff")
|
|
325
|
-
}
|
|
326
|
-
|
|
327
|
-
yield* Effect.forEach(
|
|
328
|
-
[
|
|
329
|
-
["page.html", "text/plain; charset=utf-8"],
|
|
330
|
-
["logo.svg", "text/plain; charset=utf-8"],
|
|
331
|
-
["feed.xml", "text/plain; charset=utf-8"],
|
|
332
|
-
["app.js", "text/plain; charset=utf-8"],
|
|
333
|
-
["photo.png", "image/png"],
|
|
334
|
-
["clip.mp4", "video/mp4"],
|
|
335
|
-
] as const,
|
|
336
|
-
([file, type]) =>
|
|
337
|
-
Effect.gen(function* () {
|
|
338
|
-
const full = yield* request(file)
|
|
339
|
-
expect({ file, status: full.status, type: full.headers.get("content-type") }).toEqual({
|
|
340
|
-
file,
|
|
341
|
-
status: 200,
|
|
342
|
-
type,
|
|
343
|
-
})
|
|
344
|
-
expectDownload(full, file)
|
|
345
|
-
const head = yield* request(file, { method: "HEAD" })
|
|
346
|
-
expect(head.headers.get("content-type")).toBe(type)
|
|
347
|
-
expectDownload(head, file)
|
|
348
|
-
}),
|
|
349
|
-
)
|
|
350
|
-
|
|
351
|
-
const html = yield* request("page.html")
|
|
352
|
-
expect(yield* Effect.promise(() => html.text())).toBe("<!doctype html><script>document.title = 'x'</script>")
|
|
353
|
-
|
|
354
|
-
const partial = yield* request("clip.mp4", { headers: { Range: "bytes=2-5" } })
|
|
355
|
-
expect(partial.status).toBe(206)
|
|
356
|
-
expect(partial.headers.get("content-type")).toBe("video/mp4")
|
|
357
|
-
expectDownload(partial, "clip.mp4")
|
|
358
|
-
expect(yield* Effect.promise(() => partial.text())).toBe("2345")
|
|
359
|
-
|
|
360
|
-
const cached = yield* request("clip.mp4", { headers: { "If-None-Match": "*" } })
|
|
361
|
-
expect(cached.status).toBe(304)
|
|
362
|
-
expectDownload(cached, "clip.mp4")
|
|
363
|
-
|
|
364
|
-
const named = yield* request("naïve 100% (1).txt")
|
|
365
|
-
expect(named.headers.get("content-disposition")).toBe(
|
|
366
|
-
`attachment; filename="na_ve 100_ (1).txt"; filename*=UTF-8''na%C3%AFve%20100%25%20%281%29.txt`,
|
|
367
|
-
)
|
|
368
|
-
}),
|
|
369
|
-
)
|
|
370
|
-
|
|
371
|
-
it.live("opens no file for HEAD and closes it when a download is aborted", () =>
|
|
372
|
-
Effect.gen(function* () {
|
|
373
|
-
if (process.platform !== "linux" && process.platform !== "darwin") return
|
|
374
|
-
const tmp = yield* tmpdirScoped()
|
|
375
|
-
const file = path.join(tmp.path, "large.bin")
|
|
376
|
-
yield* Effect.promise(() => fs.writeFile(file, new Uint8Array(32 * 1024 * 1024)))
|
|
377
|
-
const real = yield* Effect.promise(() => fs.realpath(file))
|
|
378
|
-
const server = yield* startServer(path.join(tmp.path, "config"))
|
|
379
|
-
const url = new URL("/api/fs/read/large.bin", server.base)
|
|
380
|
-
url.searchParams.set("location[directory]", tmp.path)
|
|
381
|
-
const openHandles = Effect.promise(async () => {
|
|
382
|
-
if (process.platform === "darwin")
|
|
383
|
-
return Bun.spawnSync(["lsof", "-p", String(process.pid), "-Fn"])
|
|
384
|
-
.stdout.toString()
|
|
385
|
-
.split("\n")
|
|
386
|
-
.filter((line) => line === `n${real}`).length
|
|
387
|
-
const fds = await fs.readdir("/proc/self/fd")
|
|
388
|
-
const targets = await Promise.all(fds.map((fd) => fs.readlink(`/proc/self/fd/${fd}`).catch(() => "")))
|
|
389
|
-
return targets.filter((target) => target === real).length
|
|
390
|
-
})
|
|
391
|
-
|
|
392
|
-
const head = yield* Effect.promise(() => fetch(url, { method: "HEAD", headers: server.headers }))
|
|
393
|
-
expect(head.status).toBe(200)
|
|
394
|
-
expect(head.headers.get("content-length")).toBe(String(32 * 1024 * 1024))
|
|
395
|
-
expect(yield* openHandles).toBe(0)
|
|
396
|
-
|
|
397
|
-
const controller = new AbortController()
|
|
398
|
-
const download = yield* Effect.promise(() => fetch(url, { headers: server.headers, signal: controller.signal }))
|
|
399
|
-
const reader = download.body?.getReader()
|
|
400
|
-
yield* Effect.promise(async () => reader?.read())
|
|
401
|
-
expect(yield* openHandles).toBe(1)
|
|
402
|
-
controller.abort()
|
|
403
|
-
expect(
|
|
404
|
-
yield* openHandles.pipe(
|
|
405
|
-
Effect.repeat({ until: (count) => count === 0, schedule: Schedule.spaced("25 millis") }),
|
|
406
|
-
Effect.timeout("5 seconds"),
|
|
407
|
-
),
|
|
408
|
-
).toBe(0)
|
|
409
|
-
}),
|
|
410
|
-
)
|
|
411
|
-
|
|
412
|
-
it.live("confines watch requests like read and reports a disabled watcher", () =>
|
|
413
|
-
Effect.gen(function* () {
|
|
414
|
-
const tmp = yield* tmpdirScoped()
|
|
415
|
-
const project = path.join(tmp.path, "project")
|
|
416
|
-
yield* Effect.promise(async () => {
|
|
417
|
-
await fs.mkdir(project)
|
|
418
|
-
await fs.writeFile(path.join(project, "a.ts"), "a")
|
|
419
|
-
await fs.writeFile(path.join(tmp.path, "outside.ts"), "outside")
|
|
420
|
-
})
|
|
421
|
-
const server = yield* startServer(path.join(tmp.path, "config"))
|
|
422
|
-
const watch = (method: string, file: string) =>
|
|
423
|
-
Effect.promise(async () => {
|
|
424
|
-
const url = new URL("/api/experimental/fs/watch", server.base)
|
|
425
|
-
url.searchParams.set("location[directory]", project)
|
|
426
|
-
const response = await fetch(url, {
|
|
427
|
-
method,
|
|
428
|
-
headers: { ...server.headers, "content-type": "application/json" },
|
|
429
|
-
body: JSON.stringify({ path: file }),
|
|
430
|
-
})
|
|
431
|
-
return response.status
|
|
432
|
-
})
|
|
433
|
-
|
|
434
|
-
expect(yield* watch("POST", "../outside.ts")).toBe(404)
|
|
435
|
-
expect(yield* watch("POST", "missing.ts")).toBe(404)
|
|
436
|
-
expect(yield* watch("POST", "a.ts")).toBe(503)
|
|
437
|
-
expect(yield* watch("DELETE", "a.ts")).toBe(204)
|
|
438
|
-
}),
|
|
439
|
-
)
|