Skip to content
Open
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
9 changes: 9 additions & 0 deletions packages/angular/ssr/node/src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,18 @@ export function createWebRequestFromNodeRequest(
const { headers, method = 'GET' } = nodeRequest;
const withBody = method !== 'GET' && method !== 'HEAD';
const referrer = headers.referer && URL.canParse(headers.referer) ? headers.referer : undefined;
const controller = new AbortController();
if (nodeRequest.aborted) {
controller.abort();
} else {
const onAbort = () => controller.abort();
nodeRequest.once('aborted', onAbort);
nodeRequest.once('close', () => nodeRequest.off('aborted', onAbort));
}

return new Request(createRequestUrl(nodeRequest, trustProxyHeadersNormalized), {
method,
signal: controller.signal,
headers: createRequestHeaders(headers),
body: withBody ? nodeRequest : undefined,
duplex: withBody ? 'half' : undefined,
Expand Down
20 changes: 20 additions & 0 deletions packages/angular/ssr/node/test/request_http1_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,24 @@ describe('createWebRequestFromNodeRequest (HTTP/1.1)', () => {
expect(await webRequest.text()).toBe('');
});
});

describe('abort handling', () => {
it('should abort the web request signal when the node request is aborted', async () => {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Consider: Add a test case to verify that when nodeRequest.aborted is already true before calling createWebRequestFromNodeRequest, the returned webRequest.signal.aborted is immediately true. This ensures explicit test coverage for the pre-aborted branch.

const nodeRequest = await extractNodeRequest(() => {
request({
hostname: 'localhost',
port,
path: '/abort',
method: 'GET',
}).end();
});

const webRequest = createWebRequestFromNodeRequest(nodeRequest);
expect(webRequest.signal.aborted).toBeFalse();

nodeRequest.emit('aborted');

expect(webRequest.signal.aborted).toBeTrue();
});
});
});
20 changes: 20 additions & 0 deletions packages/angular/ssr/node/test/request_http2_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,24 @@ describe('createWebRequestFromNodeRequest (HTTP/2)', () => {
expect(await webRequest.text()).toBe('');
});
});

describe('abort handling', () => {
it('should abort the web request signal when the node request is aborted', async () => {
const nodeRequest = await extractNodeRequest(() => {
client
.request({
':path': '/abort',
':method': 'GET',
})
.end();
});

const webRequest = createWebRequestFromNodeRequest(nodeRequest);
expect(webRequest.signal.aborted).toBeFalse();

nodeRequest.emit('aborted');

expect(webRequest.signal.aborted).toBeTrue();
});
});
});
Loading