-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserverlessResponse.ts
139 lines (113 loc) · 3.79 KB
/
serverlessResponse.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import { IncomingHttpHeaders, ServerResponse } from 'http';
import { Socket } from 'node:net';
import { debug } from './common';
import ServerlessRequest from './serverlessRequest';
const headerEnd = '\r\n\r\n';
const BODY = Symbol('Response body');
const HEADERS = Symbol('Response headers');
const getString = (data: unknown): string => {
if (Buffer.isBuffer(data)) {
return data.toString('utf8');
} else if (typeof data === 'string') {
return data;
} else if (data instanceof Uint8Array) {
return new TextDecoder().decode(data);
} else {
throw new Error(`response.write() of unexpected type: ${typeof data}`);
}
};
const addData = (stream: ServerlessResponse, data: Buffer | string | Uint8Array): void => {
try {
stream[BODY].push(Buffer.from(data));
} catch (err) {
debug(`Error adding data to response: ${err}`);
throw new Error(`response.write() of unexpected type: ${typeof data}`);
}
};
export default class ServerlessResponse extends ServerResponse {
private _wroteHeader = false;
private _header: string;
[BODY]: Buffer[];
[HEADERS]: IncomingHttpHeaders;
static from(res: ServerlessRequest): ServerlessResponse {
const response = new ServerlessResponse(res);
const { statusCode = 0, headers, body } = res;
response.statusCode = statusCode;
response[HEADERS] = headers;
response[BODY] = body ? [Buffer.from(body)] : [];
response.end();
return response;
}
static body(res: ServerlessResponse): Buffer {
return Buffer.concat(res[BODY]);
}
static headers(res: ServerlessResponse): IncomingHttpHeaders {
const headers = typeof res.getHeaders === 'function' ? res.getHeaders() : res.headers;
return Object.assign(headers, res[HEADERS]);
}
get headers(): IncomingHttpHeaders {
return this[HEADERS];
}
setHeader(name: string, value: string | number | readonly string[]) {
if (this._wroteHeader) {
this[HEADERS][name] = value as string;
} else {
super.setHeader(name, value);
}
return this;
}
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
writeHead(statusCode: number, reason?: string | IncomingHttpHeaders, obj?: IncomingHttpHeaders) {
const headers = typeof reason === 'string' ? obj : (reason as IncomingHttpHeaders);
for (const name in headers) {
this.setHeader(name, headers[name] as string);
if (!this._wroteHeader) {
// we only need to initiate super.headers once
// writeHead will add the other headers itself
break;
}
}
super.writeHead(statusCode, reason as string, obj);
return this;
}
constructor(request: ServerlessRequest) {
super(request);
this[BODY] = [];
this[HEADERS] = {};
this.useChunkedEncodingByDefault = false;
this.chunkedEncoding = false;
this._header = '';
this.assignSocket({
_writableState: {},
writable: true,
on: Function.prototype,
removeListener: Function.prototype,
destroy: Function.prototype,
cork: Function.prototype,
uncork: Function.prototype,
write: (data: Buffer | string | Uint8Array, encoding?: string | null, cb?: () => void) => {
if (typeof encoding === 'function') {
cb = encoding;
encoding = null;
}
if (this._header === '' || this._wroteHeader) {
addData(this, data);
} else {
const string = getString(data);
const index = string.indexOf(headerEnd);
if (index !== -1) {
const remainder = string.slice(index + headerEnd.length);
if (remainder) {
addData(this, remainder);
}
this._wroteHeader = true;
}
}
if (typeof cb === 'function') {
cb();
}
},
} as unknown as Socket);
}
}