Skip to content

Commit

Permalink
fix: Cancel unused response bodies (#956)
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Haines <[email protected]>
  • Loading branch information
haines authored Jun 3, 2024
1 parent df08c99 commit 11430b5
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 2 deletions.
4 changes: 3 additions & 1 deletion packages/embedded/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

## [Unreleased]

No notable changes.
### Changed

- Cancel unused response bodies ([#956](https://github.com/cerbos/cerbos-sdk-javascript/pull/956))

## [0.7.2] - 2024-05-15

Expand Down
7 changes: 7 additions & 0 deletions packages/embedded/changelog.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
unreleased:
type: patch

changed:
- summary: Cancel unused response bodies
pull: 956

releases:
- version: 0.7.2
date: 2024-05-15
Expand Down
2 changes: 2 additions & 0 deletions packages/embedded/src/bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type {
} from "./loader";
import { CheckResourcesRequest } from "./protobuf/cerbos/request/v1/request";
import { CheckResourcesResponse } from "./protobuf/cerbos/response/v1/response";
import { cancelBody } from "./response";
import type { Allocator } from "./slice";
import { Slice } from "./slice";

Expand Down Expand Up @@ -170,6 +171,7 @@ async function instantiateStreaming(
imports: WebAssembly.Imports,
): Promise<Exports> {
if (!response.ok) {
cancelBody(response);
throw new Error(
`Failed to download from ${response.url}: HTTP ${response.status}`,
);
Expand Down
2 changes: 2 additions & 0 deletions packages/embedded/src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { _setErrorNameAndStack } from "@cerbos/core";

import { Bundle, download } from "./bundle";
import { constrainAutoUpdateInterval } from "./interval";
import { cancelBody } from "./response";
import { Transport } from "./transport";

type LoadResult =
Expand Down Expand Up @@ -437,6 +438,7 @@ export class AutoUpdatingLoader extends Loader {
const response = await download(this.url, request);

if (response.status === 304) {
cancelBody(response);
throw notModified;
}

Expand Down
5 changes: 5 additions & 0 deletions packages/embedded/src/response.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export function cancelBody(response: Response): void {
response.body?.cancel().catch(() => {
// ignore failure to cancel
});
}
4 changes: 3 additions & 1 deletion packages/http/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

## [Unreleased]

No notable changes.
### Changed

- Cancel unused response bodies ([#956](https://github.com/cerbos/cerbos-sdk-javascript/pull/956))

## [0.19.0] - 2024-05-15

Expand Down
7 changes: 7 additions & 0 deletions packages/http/changelog.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
unreleased:
type: patch

changed:
- summary: Cancel unused response bodies
pull: 956

releases:
- version: 0.19.0
date: 2024-05-15
Expand Down
4 changes: 4 additions & 0 deletions packages/http/src/transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,10 @@ export class Transport implements _Transport {
yield responseType.fromJSON(result);
}
} catch (error) {
response.body?.cancel().catch(() => {
// ignore failure to cancel
});

abortHandler.throwIfAborted();

if (error instanceof NotOK) {
Expand Down

0 comments on commit 11430b5

Please sign in to comment.