httpapi-compression.test.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import { afterEach, describe, expect, test } from "bun:test"
  2. import { gunzipSync, inflateSync } from "node:zlib"
  3. import * as Log from "@opencode-ai/core/util/log"
  4. import { Server } from "../../src/server/server"
  5. import { resetDatabase } from "../fixture/db"
  6. import { disposeAllInstances, tmpdir } from "../fixture/fixture"
  7. void Log.init({ print: false })
  8. afterEach(async () => {
  9. await disposeAllInstances()
  10. await resetDatabase()
  11. })
  12. function app() {
  13. return Server.Default().app
  14. }
  15. // /config echoes the config back. Padding the config pushes the response body
  16. // well past the 1024 B threshold so we can observe compression behavior.
  17. function fatConfig() {
  18. const instructions: string[] = []
  19. for (let i = 0; i < 50; i++) {
  20. instructions.push(`padding-instruction-${i}-${"x".repeat(40)}`)
  21. }
  22. return {
  23. formatter: false,
  24. lsp: false,
  25. username: "compression-test-user",
  26. instructions,
  27. }
  28. }
  29. describe("HttpApi compression", () => {
  30. describe("encodes responses", () => {
  31. test("gzips JSON when Accept-Encoding includes gzip and body exceeds threshold", async () => {
  32. await using tmp = await tmpdir({ config: fatConfig() })
  33. const response = await app().request("/config", {
  34. headers: { "x-opencode-directory": tmp.path, "accept-encoding": "gzip" },
  35. })
  36. expect(response.status).toBe(200)
  37. expect(response.headers.get("content-encoding")).toBe("gzip")
  38. const compressed = new Uint8Array(await response.arrayBuffer())
  39. const decompressed = gunzipSync(compressed)
  40. const json = JSON.parse(new TextDecoder().decode(decompressed))
  41. expect(json).toMatchObject({ username: "compression-test-user" })
  42. expect(compressed.byteLength).toBeLessThan(decompressed.byteLength)
  43. })
  44. test("uses deflate when only deflate is acceptable", async () => {
  45. await using tmp = await tmpdir({ config: fatConfig() })
  46. const response = await app().request("/config", {
  47. headers: { "x-opencode-directory": tmp.path, "accept-encoding": "deflate" },
  48. })
  49. expect(response.status).toBe(200)
  50. expect(response.headers.get("content-encoding")).toBe("deflate")
  51. const compressed = new Uint8Array(await response.arrayBuffer())
  52. const decompressed = inflateSync(compressed)
  53. const json = JSON.parse(new TextDecoder().decode(decompressed))
  54. expect(json).toMatchObject({ username: "compression-test-user" })
  55. })
  56. test("prefers gzip when both gzip and deflate are acceptable", async () => {
  57. await using tmp = await tmpdir({ config: fatConfig() })
  58. const response = await app().request("/config", {
  59. headers: { "x-opencode-directory": tmp.path, "accept-encoding": "gzip, deflate" },
  60. })
  61. expect(response.headers.get("content-encoding")).toBe("gzip")
  62. })
  63. test("does not include the original Content-Length when compressed", async () => {
  64. await using tmp = await tmpdir({ config: fatConfig() })
  65. const response = await app().request("/config", {
  66. headers: { "x-opencode-directory": tmp.path, "accept-encoding": "gzip" },
  67. })
  68. const compressed = new Uint8Array(await response.arrayBuffer())
  69. const declared = response.headers.get("content-length")
  70. // Either absent (transfer-encoding chunked) or matches the compressed length.
  71. if (declared !== null) expect(Number(declared)).toBe(compressed.byteLength)
  72. })
  73. })
  74. describe("skips", () => {
  75. test("when no Accept-Encoding header is present", async () => {
  76. await using tmp = await tmpdir({ config: fatConfig() })
  77. const response = await app().request("/config", {
  78. headers: { "x-opencode-directory": tmp.path },
  79. })
  80. expect(response.headers.get("content-encoding")).toBeNull()
  81. })
  82. test("when Accept-Encoding only allows unsupported encodings", async () => {
  83. await using tmp = await tmpdir({ config: fatConfig() })
  84. const response = await app().request("/config", {
  85. headers: { "x-opencode-directory": tmp.path, "accept-encoding": "br" },
  86. })
  87. expect(response.headers.get("content-encoding")).toBeNull()
  88. })
  89. test("when the response body is below the 1024-byte threshold", async () => {
  90. // A bare config produces a tiny response (~few hundred bytes).
  91. await using tmp = await tmpdir({ config: { formatter: false, lsp: false } })
  92. const response = await app().request("/config", {
  93. headers: { "x-opencode-directory": tmp.path, "accept-encoding": "gzip" },
  94. })
  95. expect(response.status).toBe(200)
  96. const body = new Uint8Array(await response.arrayBuffer())
  97. expect(body.byteLength).toBeLessThan(1024)
  98. expect(response.headers.get("content-encoding")).toBeNull()
  99. })
  100. test("HEAD requests", async () => {
  101. await using tmp = await tmpdir({ config: fatConfig() })
  102. const response = await app().request("/config", {
  103. method: "HEAD",
  104. headers: { "x-opencode-directory": tmp.path, "accept-encoding": "gzip" },
  105. })
  106. expect(response.headers.get("content-encoding")).toBeNull()
  107. })
  108. })
  109. describe("streaming exclusions", () => {
  110. test("/event SSE is not compressed", async () => {
  111. await using tmp = await tmpdir({ config: { formatter: false, lsp: false } })
  112. const controller = new AbortController()
  113. const response = await app().request("/event", {
  114. headers: { "x-opencode-directory": tmp.path, "accept-encoding": "gzip" },
  115. signal: controller.signal,
  116. })
  117. try {
  118. expect(response.status).toBe(200)
  119. expect(response.headers.get("content-encoding")).toBeNull()
  120. } finally {
  121. controller.abort()
  122. await response.body?.cancel().catch(() => {})
  123. }
  124. })
  125. test("/global/event SSE is not compressed", async () => {
  126. const controller = new AbortController()
  127. const response = await app().request("/global/event", {
  128. headers: { "accept-encoding": "gzip" },
  129. signal: controller.signal,
  130. })
  131. try {
  132. expect(response.status).toBe(200)
  133. expect(response.headers.get("content-encoding")).toBeNull()
  134. } finally {
  135. controller.abort()
  136. await response.body?.cancel().catch(() => {})
  137. }
  138. })
  139. })
  140. })