Skip to content

Commit

Permalink
Support function to abort async operations
Browse files Browse the repository at this point in the history
  • Loading branch information
Borewit committed Sep 2, 2024
1 parent 6285e31 commit 59bed1c
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 17 deletions.
8 changes: 4 additions & 4 deletions lib/http-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const debug = initDebug('streaming-http-token-reader:http-client');
* Configuration options for the HTTP client.
*/
export type HttpClientConfig = {
resolveUrl?: boolean
resolveUrl?: boolean,
};

const DEFAULT_CONFIG = {
Expand All @@ -24,13 +24,13 @@ export class HttpClient implements IRangeRequestClient {
public resolvedUrl?: string;
private readonly config: HttpClientConfig;

constructor(private url: string, config?: HttpClientConfig) {
constructor(private url: string, private abortSignal: AbortSignal, config?: HttpClientConfig) {
this.config = DEFAULT_CONFIG;
Object.assign(this.config, config);
}

public async getHeadInfo(): Promise<IHeadRequestInfo> {
const response = new ResponseInfo(await fetch(this.url, {method: 'HEAD'}));
const response = new ResponseInfo(await fetch(this.url, {method: 'HEAD', signal: this.abortSignal}));
if (this.config.resolveUrl) this.resolvedUrl = response.response.url;
return response.toRangeRequestResponse();
}
Expand All @@ -44,7 +44,7 @@ export class HttpClient implements IRangeRequestClient {

const headers = new Headers();

const response = new ResponseInfo(await fetch(this.resolvedUrl || this.url, {method, headers}));
const response = new ResponseInfo(await fetch(this.resolvedUrl || this.url, {method, headers, signal: this.abortSignal}));
if (response.response.ok) {
if (this.config.resolveUrl) this.resolvedUrl = response.response.url;
return response.toRangeRequestResponse();
Expand Down
5 changes: 3 additions & 2 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export function makeTokenizer(
tokenizerConfig?: rangeTokenizer.IRangeRequestConfig,
httpClientConfig?: HttpClientConfig
): Promise<ITokenizer> {
const httpClient = new HttpClient(url, httpClientConfig);
return rangeTokenizer.tokenizer(httpClient, tokenizerConfig);
const abortController = tokenizerConfig?.abortController ?? new AbortController();
const httpClient = new HttpClient(url, abortController.signal, httpClientConfig);
return rangeTokenizer.tokenizer(httpClient, {...tokenizerConfig, abortController: abortController});
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@
"webpack-cli": "^5.1.4"
},
"dependencies": {
"@tokenizer/range": "^0.8.0",
"@tokenizer/range": "^0.9.0",
"debug": "^4.3.5",
"strtok3": "^8.0.1"
"strtok3": "^9.0.0"
},
"packageManager": "[email protected]"
}
47 changes: 38 additions & 9 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ __metadata:
dependencies:
"@biomejs/biome": "npm:^1.8.3"
"@music-metadata/test-audio": "npm:^0.1.0"
"@tokenizer/range": "npm:^0.8.0"
"@tokenizer/range": "npm:^0.9.0"
"@types/chai": "npm:^4.3.16"
"@types/debug": "npm:^4.1.12"
"@types/mocha": "npm:^10.0.7"
Expand All @@ -318,7 +318,7 @@ __metadata:
karma-webpack: "npm:^5.0.1"
mocha: "npm:^10.7.0"
music-metadata: "npm:^10.0.0"
strtok3: "npm:^8.0.1"
strtok3: "npm:^9.0.0"
ts-loader: "npm:^9.5.1"
ts-node: "npm:^10.9.2"
typescript: "npm:^5.5.3"
Expand All @@ -327,13 +327,13 @@ __metadata:
languageName: unknown
linkType: soft

"@tokenizer/range@npm:^0.8.0":
version: 0.8.0
resolution: "@tokenizer/range@npm:0.8.0"
"@tokenizer/range@npm:^0.9.0":
version: 0.9.0
resolution: "@tokenizer/range@npm:0.9.0"
dependencies:
debug: "npm:^4.3.5"
strtok3: "npm:^8.0.1"
checksum: 10c0/65a9a9712eb12c468cee84fc7ce295b034468a9416922b4d4052e371249ddeedb45fbb0c967cab41e1909ffa59000b9ffae12f70befe0a75169616707bf6207a
debug: "npm:^4.3.6"
strtok3: "npm:^9.0.0"
checksum: 10c0/394a3133d80d82b0bfee0f39e26de4689378945019ac61be8b8bcdbd4ef9f2cd047fc06f067ae67f9e2af795b063e30e0fe2f9a9a650b201adf610271f005f95
languageName: node
linkType: hard

Expand Down Expand Up @@ -1377,6 +1377,18 @@ __metadata:
languageName: node
linkType: hard

"debug@npm:^4.3.6":
version: 4.3.6
resolution: "debug@npm:4.3.6"
dependencies:
ms: "npm:2.1.2"
peerDependenciesMeta:
supports-color:
optional: true
checksum: 10c0/3293416bff072389c101697d4611c402a6bacd1900ac20c0492f61a9cdd6b3b29750fc7f5e299f8058469ef60ff8fb79b86395a30374fbd2490113c1c7112285
languageName: node
linkType: hard

"decamelize-keys@npm:^1.1.0":
version: 1.1.1
resolution: "decamelize-keys@npm:1.1.1"
Expand Down Expand Up @@ -3565,6 +3577,13 @@ __metadata:
languageName: node
linkType: hard

"peek-readable@npm:^5.2.0":
version: 5.2.0
resolution: "peek-readable@npm:5.2.0"
checksum: 10c0/7647d56786c94fc7f5f39923ef5f6ed1da7be92a2c221b743db74f1517821b1e944cc965d8f018a7e6984d969b6ced8a0c421aece7996c0b1981eac9daa4c323
languageName: node
linkType: hard

"performance-now@npm:^2.1.0":
version: 2.1.0
resolution: "performance-now@npm:2.1.0"
Expand Down Expand Up @@ -4262,7 +4281,7 @@ __metadata:
languageName: node
linkType: hard

"strtok3@npm:^8.0.0, strtok3@npm:^8.0.1":
"strtok3@npm:^8.0.0":
version: 8.0.1
resolution: "strtok3@npm:8.0.1"
dependencies:
Expand All @@ -4272,6 +4291,16 @@ __metadata:
languageName: node
linkType: hard

"strtok3@npm:^9.0.0":
version: 9.0.0
resolution: "strtok3@npm:9.0.0"
dependencies:
"@tokenizer/token": "npm:^0.3.0"
peek-readable: "npm:^5.2.0"
checksum: 10c0/7d7cde9c5a3b195131f3652c8b0cb20d96e80b10d30a19294379e4302d0d4952bdc24a516c8a6cfa82bcb96f5b47e49ec917e8cc23c81052044a2ec41d15fb30
languageName: node
linkType: hard

"supports-color@npm:^5.3.0":
version: 5.5.0
resolution: "supports-color@npm:5.5.0"
Expand Down

0 comments on commit 59bed1c

Please sign in to comment.