Skip to content

Commit e2365d3

Browse files
committed
fix(http): count async POST cache hits and keep encoded SSR bodies intact
- Count a POST that opts into the transfer cache as a hit when its response arrives after subscribe. It matches on Angular's body-sensitive cache key only, never on an Analog URL entry. - Read Content-Encoding from the headers passed to writeHead as well, so a gzip body sent that way is recognised as encoded, and send unedited bodies as the original bytes instead of a UTF-8 round trip. - Give element injectors their own 2000 node budget and environment injectors what is left of 2500, so many environment injectors no longer push element injectors out of the reported tree. - Keep the instance total when the server cuts a pipe's per-instance rows, so counts cover every instance and not only the kept rows.
1 parent a71f979 commit e2365d3

9 files changed

Lines changed: 109 additions & 18 deletions

File tree

‎apps/docs/src/content/inspectors/injectors.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ See [Tools](../agents/tools.md) and [Resources](../agents/resources.md).
135135

136136
### Up to 2000 element injectors
137137

138-
The page reports at most 2000 element injectors. Past that, a notice above the tree says so, and a lookup path or a provider can point to an injector that isn't listed. `inspect-providers` says it too. Environment injectors have no cap.
138+
The page reports at most 2000 element injectors. Past that, a notice above the tree says so, and a lookup path or a provider can point to an injector that isn't listed. `inspect-providers` says it too. Environment injectors are kept after them, up to 2500 injectors in all, so they never push element injectors out.
139139

140140
### Some injectors can't be highlighted
141141

‎packages/devtools/src/__tests__/http.test.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,18 @@ describe('interceptor', () => {
559559
expect(httpRegistry().calls?.map((c) => c.cacheHit)).toEqual([true, false]);
560560
});
561561

562+
it('counts an opted-in POST delivered later as a cache hit only for the same body', async () => {
563+
const post = (body: unknown) =>
564+
new HttpRequest('POST', '/api/search', body, { transferCache: true });
565+
document.body.innerHTML = stateScript(post({ q: 'a' }));
566+
const send = (req: HttpRequest<unknown>) =>
567+
new Promise<void>((resolve) => run(req, later([])).subscribe({ complete: resolve }));
568+
await send(post({ q: 'b' }));
569+
await send(new HttpRequest('POST', '/api/search', { q: 'a' }));
570+
await send(post({ q: 'a' }));
571+
expect(httpRegistry().calls?.map((c) => c.cacheHit)).toEqual([false, false, true]);
572+
});
573+
562574
it('does not count a request with other params as a cache hit', async () => {
563575
const page = (n: number) =>
564576
new HttpRequest('GET', '/api/feed', null, {

‎packages/devtools/src/__tests__/pipes-pages.test.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,9 @@ describe('pipe page targets', () => {
166166
expect((call.lastResult as string).length).toBeLessThan(1000);
167167
expect(call.lastCaller!.length).toBeLessThan(1000);
168168
expect(call.instances!.length).toBeLessThanOrEqual(10);
169+
expect(call.instanceTotal).toBe(40);
170+
report.pipes[0].call.instanceTotal = 90;
171+
expect(mergePipePageReport(pages, report, 1001).pipes[0].call!.instanceTotal).toBe(90);
169172
expect(state.async).toHaveLength(200);
170173
expect(state.asyncTotal).toBe(500);
171174
});

‎packages/devtools/src/__tests__/ssr-overrides.test.ts‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,35 @@ describe('ssrMiddleware overrides', () => {
252252
expect(text).toContain('mount `ssrMiddleware` after');
253253
});
254254

255+
it('sees a Content-Encoding passed only to writeHead and sends the bytes untouched', () => {
256+
ssrRegistry().overrides = [override({ kind: 'state-edit', key: 'a', value: '{"b":2}' })];
257+
const gzipped = gzipSync(STATE_HTML);
258+
for (const passed of [
259+
{ 'Content-Encoding': 'gzip' },
260+
['Content-Encoding', 'gzip'],
261+
[['content-encoding', 'gzip']],
262+
]) {
263+
recorded.length = 0;
264+
const res = new FakeResponse();
265+
let sent: unknown;
266+
const end = res.end;
267+
res.end = function (this: FakeResponse, chunk?: string | Buffer) {
268+
sent = chunk;
269+
return end.call(this, chunk);
270+
};
271+
createSsrMiddleware()(request(), res as unknown as ServerResponse, () => {});
272+
res.setHeader('content-type', 'text/html');
273+
res.setHeader('content-encoding', 'identity');
274+
(res.writeHead as (...a: unknown[]) => unknown)(200, passed);
275+
res.end(gzipped);
276+
expect(Buffer.isBuffer(sent) && sent.equals(gzipped)).toBe(true);
277+
expect(recorded[0]).toMatchObject({
278+
compressedBeforeTrace: true,
279+
overrides: [{ applied: false, note: expect.stringContaining('after compression') }],
280+
});
281+
}
282+
});
283+
255284
it('reads and edits the HTML when compression() is mounted before the middleware', () => {
256285
ssrRegistry().overrides = [override({ kind: 'state-edit', key: 'a', value: '{"b":2}' })];
257286
const res = new FakeResponse();

‎packages/devtools/src/http.ts‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,21 +129,24 @@ function readTransferEntries(doc: Document): TransferEntry[] {
129129
}
130130

131131
/**
132-
* True the first time a GET or HEAD response matches a TransferState entry,
133-
* so hits are detected even when another interceptor sits between this one
134-
* and the transfer cache. HttpClient entries match on Angular's cache key
135-
* (method, response type, URL, body and params); Analog entries on the URL.
132+
* True the first time a GET or HEAD response, or a POST that opted into the
133+
* transfer cache, matches a TransferState entry, so hits are detected even
134+
* when another interceptor sits between this one and the transfer cache.
135+
* HttpClient entries match on Angular's cache key (method, response type,
136+
* URL, body and params); Analog entries on the URL, for GET and HEAD only,
137+
* since a URL alone can't tell POST bodies apart.
136138
*/
137139
export function claimTransferEntry(req: HttpRequest<unknown>): boolean {
138-
if (req.method !== 'GET' && req.method !== 'HEAD') return false;
140+
const post = req.method === 'POST' && !!req.transferCache;
141+
if (req.method !== 'GET' && req.method !== 'HEAD' && !post) return false;
139142
if (typeof document === 'undefined') return false;
140143
transferEntries ??= readTransferEntries(document);
141144
const keys = new Set(transferCacheKeys(req));
142145
const urls = new Set([pathOf(req.url), pathOf(req.urlWithParams)]);
143146
const index = transferEntries.findIndex(
144147
(entry, i) =>
145148
!claimedTransfer.has(i) &&
146-
(entry.url === undefined ? keys.has(entry.key) : urls.has(entry.url)),
149+
(entry.url === undefined ? keys.has(entry.key) : !post && urls.has(entry.url)),
147150
);
148151
if (index < 0) return false;
149152
claimedTransfer.add(index);

‎packages/devtools/src/rpc/__tests__/injector-report.test.ts‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { describe, expect, it } from 'vitest';
2+
import { MAX_INJECTOR_NODES } from '../../injector-tree.ts';
23
import { MAX_REPORTED_INJECTORS, sanitizeInjectorTree } from '../injector-report.ts';
34

45
const node = (name: string, extra: Record<string, unknown> = {}) => ({
@@ -79,11 +80,23 @@ describe('sanitizeInjectorTree', () => {
7980
expect(root.providers[0].token.length).toBe(500);
8081
});
8182

82-
it('caps the node count and says the tree was cut', () => {
83-
const roots = Array.from({ length: MAX_REPORTED_INJECTORS + 10 }, (_, i) => node(`n${i}`));
84-
const tree = sanitizeInjectorTree(roots, [node('Root')]);
83+
it('caps element injectors, then gives environment injectors what is left', () => {
84+
const many = (n: number, prefix: string) =>
85+
Array.from({ length: n }, (_, i) => node(`${prefix}${i}`));
86+
const tree = sanitizeInjectorTree(many(MAX_INJECTOR_NODES + 10, 'n'), [node('Root')]);
87+
expect(tree.roots).toHaveLength(MAX_INJECTOR_NODES);
8588
expect(tree.environment).toHaveLength(1);
86-
expect(tree.roots).toHaveLength(MAX_REPORTED_INJECTORS - 1);
8789
expect(tree.truncated).toBe(true);
90+
91+
const crowded = sanitizeInjectorTree(many(10, 'n'), many(MAX_REPORTED_INJECTORS, 'e'));
92+
expect(crowded.roots).toHaveLength(10);
93+
expect(crowded.environment).toHaveLength(MAX_REPORTED_INJECTORS - 10);
94+
expect(crowded.truncated).toBe(true);
95+
96+
const full = sanitizeInjectorTree(
97+
many(MAX_INJECTOR_NODES, 'n'),
98+
many(MAX_REPORTED_INJECTORS - MAX_INJECTOR_NODES, 'e'),
99+
);
100+
expect(full.truncated).toBe(false);
88101
});
89102
});

‎packages/devtools/src/rpc/injector-report.ts‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,15 @@ function valid<T>(
6868
* Checks an injector tree a page pushed before it reaches the panel and
6969
* agents. Malformed nodes are dropped with their children, malformed
7070
* providers and dependencies are dropped, long strings are clipped, and the
71-
* tree is cut at `MAX_REPORTED_INJECTORS` nodes, with `truncated` set.
71+
* tree is cut with `truncated` set: element injectors at `MAX_INJECTOR_NODES`,
72+
* then environment injectors at what is left of `MAX_REPORTED_INJECTORS`, so
73+
* environment injectors never push element injectors out.
7274
*/
7375
export function sanitizeInjectorTree(
7476
roots: unknown,
7577
environment: unknown,
7678
): { roots: InjectorTreeNode[]; environment: InjectorTreeNode[]; truncated: boolean } {
77-
let budget = MAX_REPORTED_INJECTORS;
79+
let budget = MAX_INJECTOR_NODES;
7880
let truncated = false;
7981
const nodes = (list: unknown, depth: number): InjectorTreeNode[] => {
8082
const out: InjectorTreeNode[] = [];
@@ -109,7 +111,8 @@ export function sanitizeInjectorTree(
109111
}
110112
return out;
111113
};
112-
const environmentNodes = nodes(environment, 0);
113114
const rootNodes = nodes(roots, 0);
115+
budget += MAX_REPORTED_INJECTORS - MAX_INJECTOR_NODES;
116+
const environmentNodes = nodes(environment, 0);
114117
return { roots: rootNodes, environment: environmentNodes, truncated };
115118
}

‎packages/devtools/src/rpc/pipes-tools.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,11 @@ export function clampPipePageReport(report: PipePageReport): PipePageReport {
274274
...(pipe.call.instances
275275
? { instances: pipe.call.instances.slice(0, MAX_REPORT_INSTANCES).map(clampCall) }
276276
: {}),
277+
...(pipe.call.instances && pipe.call.instances.length > MAX_REPORT_INSTANCES
278+
? {
279+
instanceTotal: Math.max(pipe.call.instanceTotal ?? 0, pipe.call.instances.length),
280+
}
281+
: {}),
277282
},
278283
...(pipe.stale ? { stale: { detectedAt: pipe.stale.detectedAt } } : {}),
279284
})),

‎packages/devtools/src/ssr-middleware.ts‎

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,31 @@ function headerText(value: number | string | string[] | undefined): string | und
6767
export const COMPRESSED_NOTE =
6868
'the body was already compressed: mount ssrMiddleware after compression';
6969

70+
/**
71+
* The value the headers passed to writeHead give `name`, which wins over one set with setHeader.
72+
* Node takes an object, a flat `[name, value, ...]` array or `[name, value]` pairs.
73+
*/
74+
function passedHeader(args: unknown[], name: string): string | undefined {
75+
const headers = args.find((arg, i) => i > 0 && arg && typeof arg === 'object');
76+
if (!headers) return undefined;
77+
if (Array.isArray(headers)) {
78+
const flat: unknown[] = Array.isArray(headers[0]) ? headers.flat() : headers;
79+
let value: string | undefined;
80+
for (let i = 0; i + 1 < flat.length; i += 2) {
81+
if (String(flat[i]).toLowerCase() === name) {
82+
value = headerText(flat[i + 1] as string | string[]);
83+
}
84+
}
85+
return value;
86+
}
87+
const object = headers as OutgoingHttpHeaders;
88+
const key = Object.keys(object).find((k) => k.toLowerCase() === name);
89+
return key === undefined ? undefined : headerText(object[key]);
90+
}
91+
7092
/** True when the body is encoded, so its bytes can't be read as HTML. */
71-
function encoded(res: ServerResponse): boolean {
72-
const value = headerText(res.getHeader('content-encoding'))?.trim().toLowerCase();
93+
function encoded(res: ServerResponse, passed?: string): boolean {
94+
const value = (passed ?? headerText(res.getHeader('content-encoding')))?.trim().toLowerCase();
7395
return !!value && value !== 'identity';
7496
}
7597

@@ -120,7 +142,7 @@ export function createSsrMiddleware(options: SsrMiddlewareOptions = {}) {
120142

121143
const writeHead = res.writeHead;
122144
res.writeHead = function (this: ServerResponse, ...args: unknown[]) {
123-
encodedAtHead ??= encoded(res);
145+
encodedAtHead ??= encoded(res, passedHeader(args, 'content-encoding'));
124146
if (!renderMs) {
125147
renderMs = Date.now() - at;
126148
const timing = [
@@ -244,7 +266,8 @@ export function createSsrMiddleware(options: SsrMiddlewareOptions = {}) {
244266
: `could not edit the TransferState script for ${o.key}`,
245267
});
246268
}
247-
const out = compressed ? body : edited ? edited.html : html;
269+
// Bytes that were not edited go out as they came, in case they were not UTF-8 text.
270+
const out = edited ? edited.html : body;
248271
if (!res.headersSent) {
249272
res.removeHeader('content-length');
250273
res.setHeader('content-length', Buffer.byteLength(out));

0 commit comments

Comments
 (0)