Skip to content

Commit 483a3a4

Browse files
committed
fix(client): catch the connection error between request and getBuffer/getStream
1 parent fc5bbaa commit 483a3a4

File tree

4 files changed

+43
-4
lines changed

4 files changed

+43
-4
lines changed

CHANGES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changes Logs
22

3+
## v1.1.1
4+
5+
- fix(client): Catch the connection error between request and getBuffer/getStream.
6+
37
## v1.1.0
48

59
- fix(client): HTTP/2 should ignore body of DELETE method.

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@litert/http-client",
3-
"version": "1.1.0",
3+
"version": "1.1.1",
44
"description": "A simple HTTP client based on LiteRT.",
55
"main": "lib/index.js",
66
"types": "lib/index.d.ts",

src/lib/Internal/Response.ts

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ const EMPTY_BUFFER = Buffer.allocUnsafe(0);
3838

3939
export class HttpClientResponse implements C.IResponse {
4040

41+
private _error: unknown = null;
42+
4143
public constructor(
4244
private readonly _protocol: C.EProtocol,
4345
private readonly _stream: Readable,
@@ -49,6 +51,10 @@ export class HttpClientResponse implements C.IResponse {
4951
private readonly _noEntity: boolean = false
5052
) {
5153

54+
this._stream.on('error', (e) => {
55+
56+
this._error = e;
57+
});
5258
}
5359

5460
public abort(): void {
@@ -87,6 +93,16 @@ export class HttpClientResponse implements C.IResponse {
8793

8894
public getBuffer(maxLength: number = Infinity): Promise<Buffer> {
8995

96+
if (this._error) {
97+
98+
if (!this._stream.closed) {
99+
100+
this._stream.destroy();
101+
}
102+
103+
return Promise.reject(this._error);
104+
}
105+
90106
if (maxLength <= 0 || this.contentLength <= 0 || this._noEntity) {
91107

92108
return Promise.resolve(EMPTY_BUFFER);
@@ -155,6 +171,16 @@ export class HttpClientResponse implements C.IResponse {
155171

156172
public getStream(): Readable {
157173

174+
if (this._error) {
175+
176+
if (!this._stream.closed) {
177+
178+
this._stream.destroy();
179+
}
180+
181+
throw this._error;
182+
}
183+
158184
if (this._noEntity || this._statusCode === 204) {
159185

160186
throw new E.E_NO_RESPONSE_ENTITY();
@@ -176,6 +202,16 @@ export class HttpClientResponse implements C.IResponse {
176202

177203
public getRawStream(): Readable {
178204

205+
if (this._error) {
206+
207+
if (!this._stream.closed) {
208+
209+
this._stream.destroy();
210+
}
211+
212+
throw this._error;
213+
}
214+
179215
if (this._noEntity || this._statusCode === 204) {
180216

181217
throw new E.E_NO_RESPONSE_ENTITY();
@@ -217,5 +253,4 @@ export class HttpClientResponse implements C.IResponse {
217253

218254
return this._statusCode === HTTP_STATUS_CODE_UPGRADE;
219255
}
220-
221256
}

0 commit comments

Comments
 (0)