Skip to content

Commit f74bc4e

Browse files
committed
fix(@angular/ssr): abort web request signal when node request is aborted
1 parent 8d731ec commit f74bc4e

3 files changed

Lines changed: 43 additions & 0 deletions

File tree

packages/angular/ssr/node/src/request.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,12 @@ export function createWebRequestFromNodeRequest(
5555
const { headers, method = 'GET' } = nodeRequest;
5656
const withBody = method !== 'GET' && method !== 'HEAD';
5757
const referrer = headers.referer && URL.canParse(headers.referer) ? headers.referer : undefined;
58+
const controller = new AbortController();
59+
nodeRequest.once('aborted', () => controller.abort());
5860

5961
return new Request(createRequestUrl(nodeRequest, trustProxyHeadersNormalized), {
6062
method,
63+
signal: controller.signal,
6164
headers: createRequestHeaders(headers),
6265
body: withBody ? nodeRequest : undefined,
6366
duplex: withBody ? 'half' : undefined,

packages/angular/ssr/node/test/request_http1_spec.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,4 +189,24 @@ describe('createWebRequestFromNodeRequest (HTTP/1.1)', () => {
189189
expect(await webRequest.text()).toBe('');
190190
});
191191
});
192+
193+
describe('abort handling', () => {
194+
it('should abort the web request signal when the node request is aborted', async () => {
195+
const nodeRequest = await extractNodeRequest(() => {
196+
request({
197+
hostname: 'localhost',
198+
port,
199+
path: '/abort',
200+
method: 'GET',
201+
}).end();
202+
});
203+
204+
const webRequest = createWebRequestFromNodeRequest(nodeRequest);
205+
expect(webRequest.signal.aborted).toBeFalse();
206+
207+
nodeRequest.emit('aborted');
208+
209+
expect(webRequest.signal.aborted).toBeTrue();
210+
});
211+
});
192212
});

packages/angular/ssr/node/test/request_http2_spec.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,4 +188,24 @@ describe('createWebRequestFromNodeRequest (HTTP/2)', () => {
188188
expect(await webRequest.text()).toBe('');
189189
});
190190
});
191+
192+
describe('abort handling', () => {
193+
it('should abort the web request signal when the node request is aborted', async () => {
194+
const nodeRequest = await extractNodeRequest(() => {
195+
client
196+
.request({
197+
':path': '/abort',
198+
':method': 'GET',
199+
})
200+
.end();
201+
});
202+
203+
const webRequest = createWebRequestFromNodeRequest(nodeRequest);
204+
expect(webRequest.signal.aborted).toBeFalse();
205+
206+
nodeRequest.emit('aborted');
207+
208+
expect(webRequest.signal.aborted).toBeTrue();
209+
});
210+
});
191211
});

0 commit comments

Comments
 (0)