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
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,18 @@ Bindings should be set in your `wrangler.jsonc`, or a `.dev.vars.local` file whe

`SURFACE_DECISIONS_IGNORE_PATHS` is a comma-separated list of regular expressions. Matching pathnames still proxy to the origin and rewrite origin links, but skip MonetizationOS surface decisions and component transforms.

## Optional: cookies forwarded to surface decisions

`SURFACE_DECISIONS_COOKIES` is a comma-separated list of regular expressions. Cookie names matching any pattern are forwarded to the MonetizationOS surface-decisions API as `http.cookies`. Matching cookies are read from the incoming request `Cookie` header and from the origin response `Set-Cookie` headers; when the same name appears in both, the origin value is used. When unset or when no cookies match, `http.cookies` is omitted from the surface-decisions payload.

In `wrangler.jsonc` `vars`:

```jsonc
"SURFACE_DECISIONS_COOKIES": "^__session$, ^theme$, ^mos_"
```

Each pattern is a regex tested against the cookie **name**. Plain names like `^__session$` match exactly; prefixes like `^mos_` match any cookie whose name starts with `mos_`.

## Optional: headers sent to the origin

`ORIGIN_REQUEST_HEADERS` adds or overrides outgoing headers on every upstream fetch to `ORIGIN_URL`. Set it to a JSON object mapping header names to values. Leave it as `{}` when no extra headers are needed.
Expand Down
17 changes: 4 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
"SURFACE_DECISIONS_IGNORE_PATHS": {
"description": "Comma-separated regex patterns for paths that should skip surface decisions."
},
"SURFACE_DECISIONS_COOKIES": {
"description": "Comma-separated regex patterns for cookie names forwarded to surface decisions as http.cookies."
},
"ORIGIN_REQUEST_HEADERS": {
"description": "Optional JSON object mapping header names to values added to every origin request (e.g. {\"X-Api-Key\":\"secret\"}). Use {} when unset."
}
Expand All @@ -53,6 +56,6 @@
"wrangler": "^4.95.0"
},
"dependencies": {
"@monetizationos/proxy": "1.4.2"
"@monetizationos/proxy": "1.4.3"
}
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const proxy = new MOSProxyBuilder()
authenticatedUserJwtCookieName: env.AUTHENTICATED_USER_JWT_COOKIE_NAME,
injectScriptUrl: env.INJECT_SCRIPT_URL || undefined,
surfaceDecisionsIgnorePaths: env.SURFACE_DECISIONS_IGNORE_PATHS,
surfaceDecisionsCookies: env.SURFACE_DECISIONS_COOKIES,
originRequestHeaders: env.ORIGIN_REQUEST_HEADERS ?? {},
})
.withOriginFetcher(fetch)
Expand Down
28 changes: 27 additions & 1 deletion test/proxyConfig.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { fetchMock } from 'cloudflare:test'
import { env } from 'cloudflare:workers'
import { afterEach, beforeAll, describe, expect, it, vi } from 'vitest'
import { mockSurfaceDecisionsFetch } from './helpers'
import { mockOriginFetch, mockSurfaceDecisionsFetch } from './helpers'

describe('proxy config', () => {
beforeAll(() => {
Expand Down Expand Up @@ -77,6 +77,32 @@ describe('proxy config', () => {
expect(await res.json()).toEqual({ ok: true })
})

it('forwards matching cookies to surface decisions when SURFACE_DECISIONS_COOKIES is set', async () => {
mockOriginFetch({
responseHeaders: { 'Set-Cookie': 'theme=from-origin; Path=/' },
})
const mockSurfaceDecision = mockSurfaceDecisionsFetch()

const res = await fetchWithFreshWorker(
new Request('https://test.example/index.html', {
headers: { Cookie: 'jwt-cookie=request-jwt; theme=old; ignored=1' },
}),
{ SURFACE_DECISIONS_COOKIES: '^jwt-cookie$, ^theme$' },
)

expect(res.status).toBe(200)
expect(mockSurfaceDecision).toHaveBeenCalledExactlyOnceWith(
expect.objectContaining({
http: expect.objectContaining({
cookies: {
'jwt-cookie': 'request-jwt',
theme: 'from-origin',
},
}),
}),
)
})

it('merges configured origin request headers and preserves untouched client headers', async () => {
fetchMock
.get('https://origin.example')
Expand Down
1 change: 1 addition & 0 deletions worker-configuration.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ declare namespace Cloudflare {
MONETIZATION_OS_ENDPOINTS_PREFIX: string;
MONETIZATION_OS_SECRET_KEY: string;
SURFACE_DECISIONS_IGNORE_PATHS: string;
SURFACE_DECISIONS_COOKIES: string;
ORIGIN_REQUEST_HEADERS: Record<string, string>;
}
}
Expand Down
Loading