mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-21 10:52:06 +01:00
## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work > - On Paperclip Cloud, the board UI sends browser analytics to PostHog through a same-origin proxy at `/_ph` > - The proxy limits requests in flight: 16 per client address and 256 in total > - Each open tab runs posthog-js, and one tab can have a few requests in flight at once > - On Railway without `TRUST_PROXY`, `req.ip` is the private edge hop, so all visitors share one 16-request limit > - This pull request raises the limits, makes them configurable, and stops the per-client limit when `req.ip` is a shared private hop > - The benefit is that busy stacks with many users and many tabs do not lose analytics to 429 answers, and ops can see when a limit is hit ## Linked Issues or Issue Description **Subsystem affected** Server: the PostHog reverse proxy for Cloud browser analytics (`server/src/routes/posthog-proxy.ts`). **Problem or motivation** The proxy limits are too low for busy Cloud stacks. One user with many tabs can use 16 requests in flight. Behind the Railway edge without `TRUST_PROXY`, `req.ip` is a private `100.64.x.x` hop, so all visitors share one 16-request limit. The proxy then answers 429 and posthog-js drops or delays events and replay uploads. Ops cannot change the limits and cannot see when the proxy refuses requests. **Proposed solution** Raise the defaults to 128 per public client address and 1024 in total. Read both limits from two environment variables with a strict parse and a ceiling. When `req.ip` is not a public address, apply only the total limit. Give each request a 60 second deadline. Log a warning at most once a minute when the proxy refuses requests at a limit. **Alternatives considered** Find the visitor for the per-client limit from `X-Forwarded-For`. A client can write that header, and an earlier security review asked the proxy to keep `req.ip` as the key, so this change does not do that. Scale the per-client limit for a shared hop. All visitors share the hop, so a scaled limit is only a second total limit. The proxy applies the total limit alone instead. **Additional context** Fleet operators can set the variables with an `env_config` campaign. `doc/cloud-browser-analytics.md` tells how. Refs #22012 (the proxy) and #22052 (visitor address behind a private hop). ## What Changed - Raised `DEFAULT_POSTHOG_PROXY_LIMITS` to 128 requests in flight per client address and 1024 in total. The 64 MiB body limit did not change. - Added `PAPERCLIP_POSTHOG_PROXY_MAX_IN_FLIGHT` and `PAPERCLIP_POSTHOG_PROXY_MAX_IN_FLIGHT_PER_CLIENT`. `resolvePostHogProxyLimits` reads them where `resolvePostHogProxyMount` builds the proxy. A value must be a whole number from 1 to 65536. An invalid value logs a warning and uses the default. The server does not stop. - Added `posthogProxyClientKey`. It returns `req.ip` only when it is a public address. For a private, carrier-grade NAT, loopback, link-local or other reserved address, or no address, the proxy applies only the total limit. The key never comes from a client header. - Added `createPostHogProxyLimitReporter`. When the proxy answers 429, it logs `PostHog proxy refused requests at its in-flight limit` at most once a minute. The fields are the refusal counts per limit since the last warning, the requests in flight, the limits and the time since the last warning. The warning has no addresses. It uses no timer: it checks the time when a refusal occurs. - Added a request deadline of 60 seconds from the time the proxy accepts a request. At the deadline, the proxy stops the request to PostHog and closes the browser response, also when PostHog already finished and the browser does not read the response. The slot is then free again. Before, the 30 second upstream timeout was an idle timeout, so a slow body that sent a byte at a time could hold a slot until the server request timeout (300 seconds). - The mount object now also returns the limits in use. - Updated `doc/cloud-browser-analytics.md`: the limits, why these defaults, the variables, the shared-hop behavior, the warning fields and how to tune a fleet with an `env_config` campaign. Why these defaults: - 128 per address covers about 10 people behind one office network, each with 4 tabs and 3 requests in flight per tab, with space left over. - 1024 in total keeps memory bounded. The proxy streams bodies, so a request holds two sockets and the stream buffers between them, not the full body. In a local test on this branch, 1024 held requests added about 96 MiB of resident memory with 4 KiB bodies and about 173 MiB with 256 KiB bodies (256 held requests with 4 KiB bodies: about 40 MiB). These numbers include the test client and the test upstream in the same process, so the proxy share is smaller. Operators can lower the limit for small stacks. ## Verification - `cd server && npx vitest run src/__tests__/cloud-posthog-browser.test.ts`: 87 tests pass (5 runs in a row). - New tests: - Defaults, values from the environment, an empty value, and invalid values (`0`, `-5`, `1.5`, `1e3`, `12abc`, `abc`, `0x10`, `+8`, `65537`, `1024000`, a value larger than a safe integer). `65536` is accepted. Each invalid value keeps the default and warns once per variable. - The mount passes the configured limits to the proxy. - Per-client limit for a public address (with `trust proxy` set): a second request from the same address gets 429, a request from another address gets 200. - Shared private hop (no trusted proxy, peer is loopback): the per-client limit does not apply, and the total limit still answers 429. - A forged `X-Forwarded-For` does not select the bucket when no proxy is trusted: three held requests with the same forged public address and a per-client limit of 1 all get 200. - Request deadline: an upstream that does not answer gives 502 at the deadline and frees the slot, a body that trickles in is cut off at the deadline, and a 16 MiB response that the browser does not read is closed at the deadline. - The tests wait until the held upstream receives each request, not for a fixed time. - Total limit across public addresses, and the reported reason (`total` or `per_client`) for each 429. - The warning rate limit: one warning for the first refusal, none in the next 60 seconds, then one warning with the counts of the refusals in between, and no addresses in any warning. - Red/green: with the shared-hop rule removed (`posthogProxyClientKey` returns `req.ip`), 9 of the new tests fail. With the deadline removed, the 2 deadline tests fail. With both, all pass. - `cd server && npx tsc --noEmit -p tsconfig.json`: passes. - `pnpm verify`: exit 2, because `doc/cloud-browser-analytics.md` is a global CI input. Required CI supplies the broad coverage. - Memory numbers come from a local script that held 1024 requests through the proxy against a local upstream (Node 26, macOS). ## Risks - Higher limits let more requests use memory and sockets at once. The worst case is bounded by the total limit, and operators can lower it with `PAPERCLIP_POSTHOG_PROXY_MAX_IN_FLIGHT`. - Behind a shared private hop, one client can use all of the total limit, and other visitors then get 429. The request deadline frees each slot after at most 60 seconds, but a client that sends again can keep the limit full. Before this change, the same visitors shared a limit of 16, which one client could also fill. A separate limit for the hop would not help the visitors behind it, because one client that fills it blocks all of them. Setting `TRUST_PROXY` brings back the per-client limit. - A PostHog request that takes longer than 60 seconds now fails with 502. posthog-js bodies are small and PostHog answers in well under a second, so this does not affect normal traffic. - If an operator sets `TRUST_PROXY` to trust every hop, `req.ip` comes from the client's `X-Forwarded-For`. This was already true before this change. The total limit still applies. - The proxy is mounted only on Cloud with browser analytics on. Self-hosted installs do not change. > For core feature work, check [`ROADMAP.md`](ROADMAP.md) first and discuss it in `#dev` before opening the PR. Feature PRs that overlap with planned core work may need to be redirected — check the roadmap first. See `CONTRIBUTING.md`. ## Model Used - Claude Opus 5.5 (claude-opus-5-5), Anthropic, used through Claude Code with tool use (shell, file edits, test runs). ## Checklist - [x] I have included a thinking path that traces from project context to this change - [x] I have specified the model used (with version and capability details) - [x] I have checked ROADMAP.md and confirmed this PR does not duplicate planned core work - [x] I have searched GitHub for duplicate or related PRs and linked them above - [x] I have either (a) linked existing issues with `Fixes: #` / `Closes #` / `Refs #` OR (b) described the issue in-PR following the relevant issue template - [x] I have not referenced internal/instance-local Paperclip issues or links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip` URLs) - [x] My branch name describes the change (e.g. `docs/...`, `fix/...`) and contains no internal Paperclip ticket id or instance-derived details - [x] I have run tests locally and they pass - [x] I have added or updated tests where applicable - [x] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [ ] All Paperclip CI gates are green - [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [x] I will address all Greptile and reviewer comments before requesting merge 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
355 lines
13 KiB
TypeScript
355 lines
13 KiB
TypeScript
// Same-origin reverse proxy for browser PostHog traffic on Paperclip Cloud.
|
|
//
|
|
// Ad blockers drop requests to PostHog's own hosts, so the board UI sends to
|
|
// `/_ph` on its own origin and this proxy forwards them.
|
|
//
|
|
// It is not a generic proxy. It forwards only the requests posthog-js makes:
|
|
// the method and path must match `POSTHOG_PROXY_ROUTES`, and the upstream is
|
|
// always the configured ingest host or its static assets host. Everything
|
|
// else answers 404 without contacting PostHog.
|
|
//
|
|
// `app.ts` mounts it only when browser analytics is on (see
|
|
// `services/cloud-posthog-browser.ts`), before any body parser, so request and
|
|
// response bodies stream through without buffering. It carries no Paperclip
|
|
// data and grants nothing: it builds a fresh header set from an allowlist, so
|
|
// the browser's Host, Cookie and Authorization headers never leave.
|
|
|
|
import http from "node:http";
|
|
import https from "node:https";
|
|
import { pipeline, Transform } from "node:stream";
|
|
import type { IncomingHttpHeaders, OutgoingHttpHeaders } from "node:http";
|
|
import { Router, type Request, type Response } from "express";
|
|
import { isPublicIpAddress } from "../lib/ip-address.js";
|
|
import { logger } from "../middleware/logger.js";
|
|
import type { CloudInstanceEnv } from "../services/cloud-instance.js";
|
|
import {
|
|
POSTHOG_PROXY_PATH,
|
|
posthogAssetsOrigin,
|
|
resolveBrowserPostHogConfig,
|
|
} from "../services/cloud-posthog-browser.js";
|
|
|
|
const UPSTREAM_TIMEOUT_MS = 30_000;
|
|
|
|
export type PostHogProxyLimits = {
|
|
/** Largest request body forwarded (PostHog's own proxy guide uses 64 MiB). */
|
|
maxBodyBytes: number;
|
|
/** Requests in flight from one client address. */
|
|
maxInFlightPerClient: number;
|
|
/** Requests in flight for the whole proxy. */
|
|
maxInFlight: number;
|
|
requestDeadlineMs: number;
|
|
};
|
|
|
|
export const DEFAULT_POSTHOG_PROXY_LIMITS: PostHogProxyLimits = {
|
|
maxBodyBytes: 64 * 1024 * 1024,
|
|
maxInFlightPerClient: 128,
|
|
maxInFlight: 1024,
|
|
requestDeadlineMs: 60_000,
|
|
};
|
|
|
|
export const POSTHOG_PROXY_MAX_IN_FLIGHT_ENV = "PAPERCLIP_POSTHOG_PROXY_MAX_IN_FLIGHT";
|
|
export const POSTHOG_PROXY_MAX_IN_FLIGHT_PER_CLIENT_ENV = "PAPERCLIP_POSTHOG_PROXY_MAX_IN_FLIGHT_PER_CLIENT";
|
|
|
|
const LIMIT_WARNING_INTERVAL_MS = 60_000;
|
|
const MAX_CONFIGURED_IN_FLIGHT = 65_536;
|
|
|
|
type LimitWarning = (fields: Record<string, unknown>, message: string) => void;
|
|
|
|
function warnWithLogger(fields: Record<string, unknown>, message: string) {
|
|
logger.warn(fields, message);
|
|
}
|
|
|
|
function parseInFlightLimit(
|
|
env: CloudInstanceEnv,
|
|
name: string,
|
|
fallback: number,
|
|
warn: LimitWarning,
|
|
): number {
|
|
const raw = env[name];
|
|
if (raw === undefined || raw.trim() === "") return fallback;
|
|
const value = raw.trim();
|
|
const parsed = Number(value);
|
|
if (/^[1-9][0-9]*$/.test(value) && parsed <= MAX_CONFIGURED_IN_FLIGHT) return parsed;
|
|
warn(
|
|
{ variable: name, defaultValue: fallback, maxValue: MAX_CONFIGURED_IN_FLIGHT },
|
|
`${name} is not an integer from 1 to ${MAX_CONFIGURED_IN_FLIGHT}; the PostHog proxy uses the default`,
|
|
);
|
|
return fallback;
|
|
}
|
|
|
|
export function resolvePostHogProxyLimits(
|
|
env: CloudInstanceEnv = process.env,
|
|
warn: LimitWarning = warnWithLogger,
|
|
): PostHogProxyLimits {
|
|
return {
|
|
maxBodyBytes: DEFAULT_POSTHOG_PROXY_LIMITS.maxBodyBytes,
|
|
requestDeadlineMs: DEFAULT_POSTHOG_PROXY_LIMITS.requestDeadlineMs,
|
|
maxInFlight: parseInFlightLimit(env, POSTHOG_PROXY_MAX_IN_FLIGHT_ENV, DEFAULT_POSTHOG_PROXY_LIMITS.maxInFlight, warn),
|
|
maxInFlightPerClient: parseInFlightLimit(
|
|
env,
|
|
POSTHOG_PROXY_MAX_IN_FLIGHT_PER_CLIENT_ENV,
|
|
DEFAULT_POSTHOG_PROXY_LIMITS.maxInFlightPerClient,
|
|
warn,
|
|
),
|
|
};
|
|
}
|
|
|
|
export function posthogProxyClientKey(ip: string | undefined): string | null {
|
|
return ip && isPublicIpAddress(ip) ? ip : null;
|
|
}
|
|
|
|
export type PostHogProxyLimitReason = "total" | "per_client";
|
|
|
|
export function createPostHogProxyLimitReporter(
|
|
limits: PostHogProxyLimits,
|
|
options: { now?: () => number; warn?: LimitWarning; intervalMs?: number } = {},
|
|
) {
|
|
const now = options.now ?? Date.now;
|
|
const warn = options.warn ?? warnWithLogger;
|
|
const intervalMs = options.intervalMs ?? LIMIT_WARNING_INTERVAL_MS;
|
|
let lastWarnedAt: number | null = null;
|
|
const refused: Record<PostHogProxyLimitReason, number> = { total: 0, per_client: 0 };
|
|
|
|
return (reason: PostHogProxyLimitReason, inFlight: number) => {
|
|
refused[reason] += 1;
|
|
const at = now();
|
|
if (lastWarnedAt !== null && at - lastWarnedAt < intervalMs) return;
|
|
warn(
|
|
{
|
|
refusedAtTotalLimit: refused.total,
|
|
refusedAtPerClientLimit: refused.per_client,
|
|
inFlight,
|
|
maxInFlight: limits.maxInFlight,
|
|
maxInFlightPerClient: limits.maxInFlightPerClient,
|
|
sinceMs: lastWarnedAt === null ? null : at - lastWarnedAt,
|
|
},
|
|
"PostHog proxy refused requests at its in-flight limit",
|
|
);
|
|
lastWarnedAt = at;
|
|
refused.total = 0;
|
|
refused.per_client = 0;
|
|
};
|
|
}
|
|
|
|
const BODY_TOO_LARGE = "posthog_proxy_body_too_large";
|
|
|
|
type PostHogUpstream = "ingest" | "assets";
|
|
|
|
/**
|
|
* Every request posthog-js makes with surveys, product tours, web experiments
|
|
* and conversations turned off (see `ui/src/lib/analytics/product-analytics.ts`).
|
|
*/
|
|
const POSTHOG_PROXY_ROUTES: ReadonlyArray<{ method: "GET" | "POST"; path: RegExp; upstream: PostHogUpstream }> = [
|
|
// Event batches, including the newer `/i/v0/e/` endpoint.
|
|
{ method: "POST", path: /^\/(?:i\/v0\/)?e\/?$/, upstream: "ingest" },
|
|
{ method: "POST", path: /^\/batch\/?$/, upstream: "ingest" },
|
|
// Session replay snapshots.
|
|
{ method: "POST", path: /^\/s\/?$/, upstream: "ingest" },
|
|
// Remote settings (they carry the project's replay and autocapture switches).
|
|
{ method: "POST", path: /^\/(?:flags|decide)\/?$/, upstream: "ingest" },
|
|
// Project config and lazily loaded extensions (recorder, web vitals, ...).
|
|
{ method: "GET", path: /^\/array\/[A-Za-z0-9_-]+\/config(?:\.js)?$/, upstream: "assets" },
|
|
// posthog-js first asks for a versioned path (`/static/1.442.1/x.js`).
|
|
{ method: "GET", path: /^\/static\/(?:\d+\.\d+\.\d+\/)?[A-Za-z0-9_-][A-Za-z0-9._-]*\.js$/, upstream: "assets" },
|
|
];
|
|
|
|
/** Request headers PostHog needs. Host, cookies, auth and tracing headers never leave. */
|
|
const FORWARDED_REQUEST_HEADERS = [
|
|
"accept",
|
|
"accept-encoding",
|
|
"accept-language",
|
|
"content-encoding",
|
|
"content-length",
|
|
"content-type",
|
|
"user-agent",
|
|
] as const;
|
|
|
|
/** Response headers that describe the connection or set state on our origin. */
|
|
const DROPPED_RESPONSE_HEADERS = new Set([
|
|
"connection",
|
|
"keep-alive",
|
|
"proxy-authenticate",
|
|
"proxy-connection",
|
|
"set-cookie",
|
|
"strict-transport-security",
|
|
"transfer-encoding",
|
|
"upgrade",
|
|
]);
|
|
|
|
/**
|
|
* The upstream URL for an allowed request, or null. `requestUrl` is the path
|
|
* and query below the proxy mount.
|
|
*/
|
|
export function posthogUpstreamUrl(ingestHost: string, method: string, requestUrl: string): URL | null {
|
|
const queryAt = requestUrl.indexOf("?");
|
|
const path = queryAt === -1 ? requestUrl : requestUrl.slice(0, queryAt);
|
|
const query = queryAt === -1 ? "" : requestUrl.slice(queryAt);
|
|
const route = POSTHOG_PROXY_ROUTES.find((candidate) => candidate.method === method && candidate.path.test(path));
|
|
if (!route) return null;
|
|
const origin = route.upstream === "assets" ? posthogAssetsOrigin(ingestHost) : new URL(ingestHost).origin;
|
|
// The path matched a fixed pattern, so it cannot name another host.
|
|
return new URL(`${origin}${path}${query}`);
|
|
}
|
|
|
|
export function posthogVisitorIp(peerIp: string | undefined, headers: IncomingHttpHeaders): string | undefined {
|
|
if (peerIp && isPublicIpAddress(peerIp)) return peerIp;
|
|
const raw = headers["x-forwarded-for"];
|
|
const entries = (Array.isArray(raw) ? raw.join(",") : raw ?? "").split(",").map((entry) => entry.trim());
|
|
for (let index = entries.length - 1; index >= 0; index -= 1) {
|
|
const entry = entries[index]!;
|
|
if (isPublicIpAddress(entry)) return entry;
|
|
}
|
|
return peerIp;
|
|
}
|
|
|
|
export function posthogForwardRequestHeaders(
|
|
headers: IncomingHttpHeaders,
|
|
clientIp: string | undefined,
|
|
upstreamHost: string,
|
|
): OutgoingHttpHeaders {
|
|
const forwarded: OutgoingHttpHeaders = { host: upstreamHost };
|
|
for (const name of FORWARDED_REQUEST_HEADERS) {
|
|
const value = headers[name];
|
|
if (value !== undefined) forwarded[name] = value;
|
|
}
|
|
// PostHog derives the visitor's location from this header behind a proxy.
|
|
if (clientIp) forwarded["x-forwarded-for"] = clientIp;
|
|
return forwarded;
|
|
}
|
|
|
|
export function posthogProxyRoutes(
|
|
ingestHost: string,
|
|
limits: PostHogProxyLimits = DEFAULT_POSTHOG_PROXY_LIMITS,
|
|
reportLimit: (reason: PostHogProxyLimitReason, inFlight: number) => void = createPostHogProxyLimitReporter(limits),
|
|
) {
|
|
const router = Router();
|
|
let inFlight = 0;
|
|
const inFlightByClient = new Map<string, number>();
|
|
|
|
router.use((req: Request, res: Response) => {
|
|
let upstream: URL | null;
|
|
try {
|
|
upstream = posthogUpstreamUrl(ingestHost, req.method, req.url);
|
|
} catch {
|
|
upstream = null;
|
|
}
|
|
if (!upstream) {
|
|
res.status(404).json({ error: "not_found" });
|
|
return;
|
|
}
|
|
const target = upstream;
|
|
let received = 0;
|
|
let bodyTooLarge = false;
|
|
|
|
const declaredLength = Number(req.headers["content-length"] ?? 0);
|
|
if (Number.isFinite(declaredLength) && declaredLength > limits.maxBodyBytes) {
|
|
res.status(413).json({ error: "payload_too_large" });
|
|
return;
|
|
}
|
|
|
|
// Bound the work one browser, or all of them, can put on this process.
|
|
const client = posthogProxyClientKey(req.ip);
|
|
const clientInFlight = client === null ? 0 : inFlightByClient.get(client) ?? 0;
|
|
const refusedBy: PostHogProxyLimitReason | null =
|
|
inFlight >= limits.maxInFlight
|
|
? "total"
|
|
: client !== null && clientInFlight >= limits.maxInFlightPerClient
|
|
? "per_client"
|
|
: null;
|
|
if (refusedBy) {
|
|
reportLimit(refusedBy, inFlight);
|
|
res.setHeader("Retry-After", "1");
|
|
res.status(429).json({ error: "too_many_requests" });
|
|
return;
|
|
}
|
|
inFlight += 1;
|
|
if (client !== null) inFlightByClient.set(client, clientInFlight + 1);
|
|
res.once("close", () => {
|
|
inFlight -= 1;
|
|
if (client === null) return;
|
|
const remaining = (inFlightByClient.get(client) ?? 1) - 1;
|
|
if (remaining > 0) inFlightByClient.set(client, remaining);
|
|
else inFlightByClient.delete(client);
|
|
});
|
|
|
|
const transport = target.protocol === "https:" ? https : http;
|
|
const upstreamReq = transport.request(target, {
|
|
method: req.method,
|
|
headers: posthogForwardRequestHeaders(req.headers, posthogVisitorIp(req.ip, req.headers), target.host),
|
|
timeout: UPSTREAM_TIMEOUT_MS,
|
|
});
|
|
|
|
upstreamReq.on("response", (upstreamRes) => {
|
|
const headers: OutgoingHttpHeaders = {};
|
|
for (const [name, value] of Object.entries(upstreamRes.headers)) {
|
|
if (value !== undefined && !DROPPED_RESPONSE_HEADERS.has(name)) headers[name] = value;
|
|
}
|
|
res.writeHead(upstreamRes.statusCode ?? 502, headers);
|
|
// pipeline() handles an error on either side (PostHog closing the
|
|
// connection mid-response, the browser going away) by destroying both
|
|
// streams, so no stream error goes unhandled.
|
|
pipeline(upstreamRes, res, (err) => {
|
|
if (err) logger.debug({ err, path: target.pathname }, "PostHog proxy response ended early");
|
|
});
|
|
});
|
|
upstreamReq.on("timeout", () => {
|
|
upstreamReq.destroy(new Error("posthog_proxy_timeout"));
|
|
});
|
|
const deadline = setTimeout(() => {
|
|
if (res.headersSent || upstreamReq.destroyed) res.destroy();
|
|
upstreamReq.destroy(new Error("posthog_proxy_deadline"));
|
|
}, limits.requestDeadlineMs);
|
|
res.once("close", () => clearTimeout(deadline));
|
|
upstreamReq.on("error", (err) => {
|
|
logger.debug({ err, path: target.pathname }, "PostHog proxy request failed");
|
|
if (!res.headersSent && bodyTooLarge) {
|
|
res.status(413).json({ error: "payload_too_large" });
|
|
} else if (!res.headersSent) {
|
|
res.status(502).json({ error: "analytics_upstream_unavailable" });
|
|
} else {
|
|
res.destroy();
|
|
}
|
|
});
|
|
// The browser went away: stop the upstream request too.
|
|
res.on("close", () => {
|
|
if (!res.writableFinished) upstreamReq.destroy();
|
|
});
|
|
|
|
// A body without (or with a false) Content-Length is counted as it
|
|
// streams and cut off at the limit.
|
|
const sizeLimit = new Transform({
|
|
transform(chunk: Buffer, _encoding, callback) {
|
|
received += chunk.length;
|
|
if (received > limits.maxBodyBytes) {
|
|
bodyTooLarge = true;
|
|
callback(new Error(BODY_TOO_LARGE));
|
|
} else {
|
|
callback(null, chunk);
|
|
}
|
|
},
|
|
});
|
|
// An aborted or oversized browser upload destroys the upstream request,
|
|
// whose error listener above answers or closes the browser response.
|
|
pipeline(req, sizeLimit, upstreamReq, () => {});
|
|
});
|
|
|
|
return router;
|
|
}
|
|
|
|
/**
|
|
* The proxy mount for this instance, or null when browser analytics is off.
|
|
* Self-hosted installs always get null, so `/_ph` falls through to the UI.
|
|
*/
|
|
export function resolvePostHogProxyMount(env: CloudInstanceEnv = process.env) {
|
|
const config = resolveBrowserPostHogConfig(env);
|
|
if (!config) return null;
|
|
try {
|
|
posthogAssetsOrigin(config.host);
|
|
} catch {
|
|
logger.warn("PAPERCLIP_POSTHOG_HOST is not a valid URL; the PostHog proxy is off");
|
|
return null;
|
|
}
|
|
const limits = resolvePostHogProxyLimits(env);
|
|
return { path: POSTHOG_PROXY_PATH, router: posthogProxyRoutes(config.host, limits), limits };
|
|
}
|