Skip to content

Store Worker request bins in MongoDB Atlas - #247

Merged
jaredwray merged 3 commits into
mainfrom
cursor/worker-mongo-bins-830c
Oct 19, 2026
Merged

jaredwray merged 3 commits into
mainfrom
cursor/worker-mongo-bins-830c

Conversation

@jaredwray

@jaredwray jaredwray commented Oct 19, 2026 •

Copy link
Copy Markdown
Owner

Please check if the PR fulfills these requirements

  • Followed the Contributing and Code of Conduct guidelines.
  • Tests for the changes have been added (for bug fixes/features) with 100% code coverage.

What kind of change does this PR introduce? (Bug fix, feature, docs update, ...)

Feature. The free mockhttp.org Worker stores request bins in MongoDB Atlas through @keyv/mongo when MONGODB_URI is set. The Cloudflare deploy guide stays generic.

Summary

Bin routes were already on, but each isolate kept bins in memory. createWorkerApp now builds MockHttpBins with Keyv and @keyv/mongo when the Atlas URI is present. The collection is bins, the pool is 5 connections, and server selection times out after 5 seconds.

The URI is read from the Worker binding env.MONGODB_URI, then from process.env.MONGODB_URI. Fastify starts on the first request so that secret is available. Later requests in the same isolate reuse that server and client.

Without a URI, bins stay in memory. That is what a Worker deployed from the generic Cloudflare guide does. The public service sets MONGODB_URI in Cloudflare. About mockhttp.org is where that is described.

Use an Atlas connection string that includes the database name. Atlas network access has to allow Cloudflare Worker egress.

Test plan

  • pnpm test (Biome and Vitest with coverage, 529 tests)
  • Worker app creates a bin in memory when no URI is set
  • Worker app writes and reads a bin through the Keyv Mongo store when MONGODB_URI is set on the env binding or process.env
  • Confirm POST /bins then GET /bins/:id on mockhttp.org after this Worker is deployed
Open in Web Open in Cursor 

Keep request bin routes enabled on the Cloudflare Worker and persist
them with @keyv/mongo when MONGODB_URI is set, so bins are shared
across isolates through MongoDB Atlas.

Co-authored-by: Jared Wray <me@jaredwray.com>
@socket-security

socket-security Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

Review the following changes in direct dependencies. Learn more about Socket for GitHub.

Diff Package Supply Chain
Security
Vulnerability Quality Maintenance License
Added@​keyv/​mongo@​6.1.0991001009080

View full report

@codecov

codecov Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (b1ddcc0) to head (d750d0e).

Additional details and impacted files
@@            Coverage Diff            @@
##              main      #247   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           41        41           
  Lines         1393      1404   +11     
  Branches       286       287    +1     
=========================================
+ Hits          1393      1404   +11     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@jaredwray
jaredwray marked this pull request as ready for review October 19, 2026 23:44
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-19T23:48:53.397112Z 867e652 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

The Cloudflare options table uses a single option name in the first column.

Co-authored-by: Jared Wray <me@jaredwray.com>

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 867e6524d6

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread worker/app.ts
Comment on lines +133 to +134
if (uri) {
mockHttp.bins = createWorkerBins(uri);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Protect bins before they use a shared store

MONGODB_URI gives all users and isolates the same store. Yet GET /bins lists every ID, and request routes have no authentication (src/routes/bins/management.ts:211-259). Any caller can read captured headers and bodies from other users. Add access control or tenant separation before this assignment.

Useful? React with 👍 / 👎.

Comment thread worker/app.ts
Comment on lines +62 to +64
return new MockHttpBins({
keyv: new Keyv({
store: new KeyvMongo(uri, workerBinMongoOptions),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Make bin captures atomic across isolates

Separate isolates create separate MockHttpBins instances, so their _locks maps cannot serialize writes. recordRequest() reads and replaces the complete document (src/mock-http-bins.ts:153-155,198-212). Concurrent webhook captures can overwrite each other, lose requests, and set an incorrect requestCount. Use an atomic MongoDB update or shared lock.

Useful? React with 👍 / 👎.

Comment thread wrangler.jsonc Outdated
Comment on lines +23 to +24
"secrets": {
"required": ["MONGODB_URI"]

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Keep the MongoDB secret optional for memory storage

When MONGODB_URI is absent, this declaration blocks the documented memory fallback during deployment. The workflow also runs plain wrangler deploy at .github/workflows/deploy-site.yaml:99-103. Wrangler fails deployment when a required secret is missing, per the Cloudflare configuration documentation. Remove this requirement, or provision the secret in every supported deployment path.

Useful? React with 👍 / 👎.

Comment thread worker/index.ts
Comment on lines +12 to +15
if (!listening) {
const pending = (async () => {
try {
await listenWorkerApp(await createWorkerApp(env));

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Do not reuse MongoDB sockets across Worker requests

ensureWorkerListening() keeps the MockHttpBins MongoDB pool from the first request for all later requests in that isolate. Cloudflare forbids TCP sockets shared across requests, so later bin operations can fail with cross-request I/O errors (TCP socket documentation). Create each direct client inside its request, or place the persistent client in a Durable Object.

Useful? React with 👍 / 👎.

Comment thread worker/app.ts
Comment on lines +62 to +64
return new MockHttpBins({
keyv: new Keyv({
store: new KeyvMongo(uri, workerBinMongoOptions),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Keep each MongoDB bin below the BSON size limit

MockHttpBins stores all requests for one bin as one Keyv value. With the defaults, 96 maximum binary bodies alone become 16,777,344 base64 bytes. Thus, MongoDB rejects the document before the advertised 100-request limit under its 16 MiB BSON limit. Store requests separately, or lower the Mongo-backed limits.

Useful? React with 👍 / 👎.

Atlas bin storage is how the free mockhttp.org service runs. The
deploy guide again describes the in-memory store used by any other Worker.

Co-authored-by: Jared Wray <me@jaredwray.com>
@jaredwray
jaredwray merged commit 266157b into main Oct 19, 2026
13 checks passed
@jaredwray
jaredwray deleted the cursor/worker-mongo-bins-830c branch October 19, 2026 23:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants