Skip to content

Commit

Permalink
fix(network): writeHead broken on nodejs 14.x
Browse files Browse the repository at this point in the history
  • Loading branch information
H4ad committed Apr 2, 2022
1 parent bbd2ea4 commit 252dfbe
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/network/request.ts
@@ -1,5 +1,5 @@
// ATTRIBUTION: https://github.com/dougmoscrop/serverless-http
import http from 'http';
import { IncomingMessage } from 'http';
import { AddressInfo } from 'net';
import { SingleValueHeaders } from '../@types';
import { NO_OP } from '../core';
Expand All @@ -14,7 +14,7 @@ export interface ServerlessRequestProps {
remoteAddress?: string;
}

export class ServerlessRequest extends http.IncomingMessage {
export class ServerlessRequest extends IncomingMessage {
constructor({
method,
url,
Expand Down
36 changes: 24 additions & 12 deletions src/network/response.ts
@@ -1,5 +1,5 @@
// ATTRIBUTION: https://github.com/dougmoscrop/serverless-http
import http, { IncomingMessage, OutgoingHttpHeaders } from 'http';
import { IncomingMessage, OutgoingHttpHeaders, ServerResponse } from 'http';
import { Socket } from 'net';
import { NO_OP } from '../core';

Expand All @@ -24,7 +24,7 @@ export interface ServerlessResponseProps {
method?: string;
}

export class ServerlessResponse extends http.ServerResponse {
export class ServerlessResponse extends ServerResponse {
constructor({ method }: ServerlessResponseProps) {
super({ method } as any);

Expand Down Expand Up @@ -114,22 +114,34 @@ export class ServerlessResponse extends http.ServerResponse {

writeHead(
statusCode: number,
reason?: string | OutgoingHttpHeaders,
obj?: OutgoingHttpHeaders,
statusMessage?: string | OutgoingHttpHeaders | OutgoingHttpHeaders[],
obj?: OutgoingHttpHeaders | OutgoingHttpHeaders[],
): any {
const headers = typeof reason === 'string' ? obj : reason;
const headersObjOrArray =
typeof statusMessage === 'string' ? obj : statusMessage;

for (const name in headers) {
this.setHeader(name, headers[name]!);
const arrayHeaders = Array.isArray(headersObjOrArray)
? headersObjOrArray
: [headersObjOrArray || {}];

if (!this._wroteHeader) {
// we only need to initiate super.headers once
// writeHead will add the other headers itself
break;
for (const headers of arrayHeaders) {
for (const name in headers) {
this.setHeader(name, headers[name]!);

if (!this._wroteHeader) {
// we only need to initiate super.headers once
// writeHead will add the other headers itself
break;
}
}
}

// I use ignore here because in nodejs 12.x, statusMessage can be string | OutgoingHttpHeaders
// But in nodejs >=14.x, statusMessage can also be OutgoingHttpHeaders[]
// I take care of these cases above, but here I can't handle it well, so I give up
// nodejs 12.x ref: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/node/v12/http.d.ts#L229
// nodejs 14.x ref: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/node/v14/http.d.ts#L263
// @ts-ignore
super.writeHead(statusCode, reason, obj);
return super.writeHead(statusCode, statusMessage, obj);
}
}

0 comments on commit 252dfbe

Please sign in to comment.