Skip to content

Commit 87cb4ba

Browse files
committed
refactor: improve request and response
Signed-off-by: seven <zilisheng1996@gmail.com>
1 parent 03add4c commit 87cb4ba

File tree

2 files changed

+8
-17
lines changed

2 files changed

+8
-17
lines changed

src/serverlessRequest.ts

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// ATTRIBUTION: https://github.com/dougmoscrop/serverless-http
2-
31
import { IncomingMessage } from 'http';
42
import { Socket } from 'net';
53

@@ -14,20 +12,13 @@ interface ServerlessRequestOptions {
1412
isBase64Encoded: boolean;
1513
}
1614

17-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
18-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
19-
const NO_OP: (...args: any[]) => any = () => void 0;
15+
const NO_OP: (...args: unknown[]) => unknown = () => void 0;
2016

2117
export default class ServerlessRequest extends IncomingMessage {
22-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
23-
// @ts-expect-error
2418
ip: string;
25-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
26-
// @ts-expect-error
19+
2720
body: Buffer | string;
2821

29-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
30-
// @ts-expect-error
3122
isBase64Encoded: boolean;
3223

3324
constructor({
@@ -55,18 +46,19 @@ export default class ServerlessRequest extends IncomingMessage {
5546
);
5647

5748
Object.assign(this, {
58-
ip: remoteAddress,
5949
complete: true,
6050
httpVersion: '1.1',
6151
httpVersionMajor: '1',
6252
httpVersionMinor: '1',
6353
method,
64-
body,
6554
url,
6655
headers: combinedHeaders,
67-
isBase64Encoded,
6856
});
6957

58+
this.body = body;
59+
this.ip = remoteAddress;
60+
this.isBase64Encoded = isBase64Encoded;
61+
7062
this._read = () => {
7163
this.push(body);
7264
this.push(null);

src/serverlessResponse.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
// ATTRIBUTION: https://github.com/dougmoscrop/serverless-http
2-
31
import { IncomingHttpHeaders, ServerResponse } from 'http';
42
import { IncomingMessage } from 'node:http';
53
import ServerlessRequest from './serverlessRequest';
64
import { Socket } from 'node:net';
5+
import { debug } from './common';
76

87
const headerEnd = '\r\n\r\n';
98

@@ -25,8 +24,8 @@ const getString = (data: unknown): string => {
2524
const addData = (stream: ServerlessResponse, data: Buffer | string | Uint8Array): void => {
2625
try {
2726
stream[BODY].push(Buffer.from(data));
28-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
2927
} catch (err) {
28+
debug(`Error adding data to response: ${err}`);
3029
throw new Error(`response.write() of unexpected type: ${typeof data}`);
3130
}
3231
};

0 commit comments

Comments
 (0)