Skip to content

Commit

Permalink
refactor: improve request and response
Browse files Browse the repository at this point in the history
Signed-off-by: seven <zilisheng1996@gmail.com>
  • Loading branch information
Blankll committed Oct 20, 2024
1 parent 03add4c commit 87cb4ba
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 17 deletions.
20 changes: 6 additions & 14 deletions src/serverlessRequest.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// ATTRIBUTION: https://github.com/dougmoscrop/serverless-http

import { IncomingMessage } from 'http';
import { Socket } from 'net';

Expand All @@ -14,20 +12,13 @@ interface ServerlessRequestOptions {
isBase64Encoded: boolean;
}

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

export default class ServerlessRequest extends IncomingMessage {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
ip: string;
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error

body: Buffer | string;

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

constructor({
Expand Down Expand Up @@ -55,18 +46,19 @@ export default class ServerlessRequest extends IncomingMessage {
);

Object.assign(this, {
ip: remoteAddress,
complete: true,
httpVersion: '1.1',
httpVersionMajor: '1',
httpVersionMinor: '1',
method,
body,
url,
headers: combinedHeaders,
isBase64Encoded,
});

this.body = body;
this.ip = remoteAddress;
this.isBase64Encoded = isBase64Encoded;

this._read = () => {
this.push(body);
this.push(null);
Expand Down
5 changes: 2 additions & 3 deletions src/serverlessResponse.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
// ATTRIBUTION: https://github.com/dougmoscrop/serverless-http

import { IncomingHttpHeaders, ServerResponse } from 'http';
import { IncomingMessage } from 'node:http';
import ServerlessRequest from './serverlessRequest';
import { Socket } from 'node:net';
import { debug } from './common';

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

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

0 comments on commit 87cb4ba

Please sign in to comment.