Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,24 @@

All notable changes to this project will be documented in this file.

## [Unreleased]

### Fixed

- **Registry and docs responses are read within a size limit and timeouts.**
- A body over 50 MB is cancelled, and the tool returns an error instead of parsing it.
- A request may wait 30 seconds for headers and 30 seconds between chunks of its body, and a call takes at most 2 minutes, its retries included. A server that sends headers and then stalls no longer hangs the call, and a slow download that keeps arriving still finishes. A body read that ignores the abort signal, as some fetch replacements' do, still ends at the timeout.
- Documentation pages and the docs index use the same limits; they had no timeout before.
- **Error responses keep their status.** A 401, 403 or 404 whose body breaks off, stalls or is too large is still reported as that status, without a retry.
- **JSON bodies that start with a byte-order mark parse again**, as they did with `response.json()`.
- **A long `Retry-After` is not waited on.** When a server asks to wait longer than the backoff allows (10 seconds), the call returns rate limited instead of sleeping.
- **`lpm_docs` reports failed pages.** A search where no matching page could be read returns an error instead of an empty answer, and one where some failed names them after the pages it read.
- **`lpm_browse_source` lists a long tree 2,000 paths at a time.**
- A larger tree comes with `treeTotal`, `treeOffset`, `treeTruncated`, and a `treeSummary` counting files by top-level entry; pass the new `treeOffset` argument for the next page, so every path can be reached. A summary of more than 100 entries says so.
- With `path`, a tree too large to list whole is scoped to that directory and names it in `treeScope`. A `path` with no paths under it lists the whole package and says so.
- Before, up to 20,000 paths went to the model at once.
- **The session cache holds at most 500 entries**, dropping the least recently used. Its size must be a positive integer.

## [0.3.0] - 2026-11-08

### Changed
Expand Down
336 changes: 335 additions & 1 deletion lib/__tests__/api.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { Readable } from "node:stream"
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"
import { apiGet, registryGet } from "../api.js"
import { apiGet, registryGet, textGet } from "../api.js"
import {
MAX_RESPONSE_BYTES,
REQUEST_TIMEOUT_MS,
REQUEST_TOTAL_TIMEOUT_MS,
RETRY_MAX_DELAY_MS,
} from "../constants.js"

const mockFetch = vi.fn()

Expand Down Expand Up @@ -127,6 +134,333 @@ describe("api", () => {
})
})

describe("response bodies", () => {
const encoder = new TextEncoder()
const tooLarge = new RegExp(
`larger than ${MAX_RESPONSE_BYTES / (1024 * 1024)} MB`,
)

/** A body that never ends, and counts how often it was read and cancelled. */
function endlessBody() {
const chunk = encoder.encode(`"${"x".repeat(256 * 1024)}",`)
const stats = { pulls: 0, cancels: 0 }
const body = new ReadableStream({
start(controller) {
controller.enqueue(encoder.encode("["))
},
pull(controller) {
stats.pulls++
controller.enqueue(chunk)
},
cancel() {
stats.cancels++
},
})
return { body, stats }
}

/** A body that sends `parts`, one every `everyMs`, then ends. */
function slowBody(parts, everyMs, signal) {
return new ReadableStream({
async start(controller) {
signal?.addEventListener("abort", () =>
controller.error(signal.reason),
)
for (const part of parts) {
await new Promise(resolve => setTimeout(resolve, everyMs))
if (signal?.aborted) return
controller.enqueue(encoder.encode(part))
}
controller.close()
},
})
}

/** A body that sends `part` and then neither ends nor sends more. */
function stalledBody(part, signal) {
return new ReadableStream({
start(controller) {
controller.enqueue(encoder.encode(part))
signal.addEventListener("abort", () =>
controller.error(signal.reason),
)
},
})
}

afterEach(() => {
vi.useRealTimers()
})

it("stops reading a body past the size limit and reports it without retrying", async () => {
const { body, stats } = endlessBody()
mockFetch.mockImplementation(async () => new Response(body))

const result = await apiGet("https://lpm.dev/api/test", null)

expect(mockFetch).toHaveBeenCalledTimes(1)
expect(stats.cancels).toBe(1)
expect(result.ok).toBe(false)
expect(result.data.error).toMatch(tooLarge)
})

it("doesn't read a body whose declared size is past the limit", async () => {
const { body, stats } = endlessBody()
mockFetch.mockImplementation(
async () =>
new Response(body, {
headers: { "content-length": String(MAX_RESPONSE_BYTES + 1) },
}),
)

const result = await apiGet("https://lpm.dev/api/test", null)

expect(stats.pulls).toBe(0)
expect(stats.cancels).toBe(1)
expect(result.data.error).toMatch(tooLarge)
})

it("bounds a body without a stream reader, as fetch replacements give", async () => {
function* endless() {
yield Buffer.from("[")
for (;;) yield Buffer.alloc(256 * 1024, 0x20)
}
const body = Readable.from(endless())
mockFetch.mockResolvedValueOnce({
ok: true,
status: 200,
body,
headers: { get: () => null },
json: () => Promise.reject(new Error("must not parse unbounded")),
})

const result = await apiGet("https://lpm.dev/api/test", null)

expect(result.data.error).toMatch(tooLarge)
expect(body.destroyed).toBe(true)
})

it("times out a body that stalls after its headers, on each attempt", async () => {
vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout"] })
mockFetch.mockImplementation(
async (_url, init) =>
new Response(stalledBody('{"versions":{', init.signal)),
)

const pending = apiGet("https://lpm.dev/api/test", null)
await vi.advanceTimersByTimeAsync((REQUEST_TIMEOUT_MS + 10_000) * 3)
const result = await pending

expect(mockFetch).toHaveBeenCalledTimes(3)
expect(result).toEqual({
ok: false,
status: 0,
data: { error: "Request timed out" },
})
})

it("lets a slow download finish while it keeps arriving", async () => {
vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout"] })
const gap = REQUEST_TIMEOUT_MS - 5_000
mockFetch.mockImplementationOnce(
async (_url, init) =>
new Response(slowBody(['{"a":', "1", "}"], gap, init.signal)),
)

const pending = apiGet("https://lpm.dev/api/test", null)
await vi.advanceTimersByTimeAsync(gap * 3 + 1_000)

expect(gap * 3).toBeGreaterThan(REQUEST_TIMEOUT_MS)
expect(await pending).toEqual({ ok: true, status: 200, data: { a: 1 } })
})

it("ends a download that trickles past the total timeout, which spans retries", async () => {
vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout"] })
const gap = REQUEST_TIMEOUT_MS - 5_000
const parts = Array.from(
{ length: Math.ceil(REQUEST_TOTAL_TIMEOUT_MS / gap) + 2 },
() => " ",
)
mockFetch.mockImplementation(
async (_url, init) => new Response(slowBody(parts, gap, init.signal)),
)

let settled = false
const pending = apiGet("https://lpm.dev/api/test", null).finally(() => {
settled = true
})
await vi.advanceTimersByTimeAsync(REQUEST_TOTAL_TIMEOUT_MS - 1)
expect(settled).toBe(false)
await vi.advanceTimersByTimeAsync(2)

expect(await pending).toEqual({
ok: false,
status: 0,
data: { error: "Request timed out" },
})
expect(mockFetch).toHaveBeenCalledTimes(1)
})

it("doesn't wait out a retry past the total timeout", async () => {
vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout"] })
mockFetch.mockImplementation(
async (_url, init) =>
new Response(stalledBody("{", init.signal), { status: 200 }),
)

const pending = apiGet("https://lpm.dev/api/test", null)
await vi.advanceTimersByTimeAsync(REQUEST_TOTAL_TIMEOUT_MS + 1)

expect(await pending).toEqual({
ok: false,
status: 0,
data: { error: "Request timed out" },
})
})

it("times out a body whose reader ignores the abort signal", async () => {
vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout"] })
let cancelled = 0
const reader = {
read: () => new Promise(() => {}),
cancel: async () => {
cancelled++
},
}
mockFetch.mockResolvedValue({
ok: true,
status: 200,
body: { getReader: () => reader },
headers: { get: () => null },
})

const pending = apiGet("https://lpm.dev/api/test", null)
await vi.advanceTimersByTimeAsync(REQUEST_TOTAL_TIMEOUT_MS + 1)

expect(await pending).toEqual({
ok: false,
status: 0,
data: { error: "Request timed out" },
})
expect(cancelled).toBeGreaterThan(0)
})

it("reads a JSON body that starts with a byte-order mark, as fetch does", async () => {
mockFetch.mockImplementationOnce(
async () => new Response('\uFEFF{"name":"valid"}'),
)

expect(await apiGet("https://lpm.dev/api/test", null)).toEqual({
ok: true,
status: 200,
data: { name: "valid" },
})
})

it("keeps an auth failure's status when its body breaks off or is too large, without retrying", async () => {
const broken = new ReadableStream({
start(controller) {
controller.enqueue(encoder.encode('{"err'))
controller.error(new TypeError("terminated"))
},
})
mockFetch.mockImplementationOnce(
async () => new Response(broken, { status: 401 }),
)
expect(await apiGet("https://lpm.dev/api/test", "t")).toEqual({
ok: false,
status: 401,
data: {},
})
expect(mockFetch).toHaveBeenCalledTimes(1)

mockFetch.mockImplementationOnce(
async () => new Response(endlessBody().body, { status: 403 }),
)
expect(await apiGet("https://lpm.dev/api/test", "t")).toEqual({
ok: false,
status: 403,
data: {},
})
})

it("keeps a not-found status when its body is too large", async () => {
mockFetch.mockImplementationOnce(
async () => new Response(endlessBody().body, { status: 404 }),
)

expect(await apiGet("https://lpm.dev/api/test", null)).toEqual({
ok: false,
status: 404,
data: {},
})
})

it("cancels the body of a response it retries", async () => {
const { body, stats } = endlessBody()
mockFetch
.mockImplementationOnce(async () => new Response(body, { status: 503 }))
.mockImplementationOnce(async () => new Response('{"ok":true}'))

const result = await apiGet("https://lpm.dev/api/test", null)

expect(result).toEqual({ ok: true, status: 200, data: { ok: true } })
expect(stats.cancels).toBe(1)
})

it("answers rate limited at once when the server asks to wait longer than the backoff allows", async () => {
vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout"] })
const seconds = Math.ceil(RETRY_MAX_DELAY_MS / 1000) + 1
mockFetch.mockImplementationOnce(
async () =>
new Response("", {
status: 429,
headers: { "Retry-After": String(seconds) },
}),
)

// Resolves without any timer advancing, so nothing waited.
const result = await apiGet("https://lpm.dev/api/test", null)

expect(result).toEqual({
ok: false,
status: 429,
data: { error: "Rate limited" },
})
expect(mockFetch).toHaveBeenCalledTimes(1)
})

it("reads text bodies within the same limit and timeouts", async () => {
mockFetch.mockImplementationOnce(
async () => new Response("\uFEFF# Docs\n", { status: 200 }),
)
expect(await textGet("https://lpm.dev/api/docs")).toEqual({
ok: true,
status: 200,
text: "# Docs\n",
})

mockFetch.mockImplementationOnce(
async () => new Response(endlessBody().body),
)
const oversized = await textGet("https://lpm.dev/api/docs")
expect(oversized.ok).toBe(false)
expect(oversized.error).toMatch(tooLarge)

vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout"] })
mockFetch.mockImplementationOnce(
async (_url, init) => new Response(stalledBody("# Do", init.signal)),
)
const pending = textGet("https://lpm.dev/api/docs")
await vi.advanceTimersByTimeAsync(REQUEST_TIMEOUT_MS + 1)
expect(await pending).toEqual({
ok: false,
status: 0,
error: "Request timed out",
})
})
})

describe("registryGet", () => {
it("prepends /api/registry to the path", async () => {
mockFetch.mockResolvedValueOnce(jsonResponse({ ok: true }))
Expand Down
Loading
Loading