@tsed/platform-serverless 8.11.1 → 8.12.0-rc.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/esm/builder/PlatformServerless.js +0 -1
- package/lib/esm/builder/PlatformServerlessHandler.js +0 -1
- package/lib/esm/domain/ServerlessContext.js +0 -1
- package/lib/esm/domain/ServerlessEvent.js +0 -1
- package/lib/esm/domain/ServerlessRequest.js +0 -1
- package/lib/esm/domain/ServerlessResponse.js +0 -1
- package/lib/esm/domain/ServerlessResponseStream.js +0 -1
- package/lib/esm/exports.js +0 -1
- package/lib/esm/index.js +0 -1
- package/lib/esm/utils/getRequestId.js +0 -1
- package/lib/esm/utils/isInAWS.js +0 -1
- package/lib/esm/utils/setResponseHeaders.js +0 -1
- package/package.json +16 -13
- package/lib/esm/builder/PlatformServerless.js.map +0 -1
- package/lib/esm/builder/PlatformServerlessHandler.js.map +0 -1
- package/lib/esm/domain/ServerlessContext.js.map +0 -1
- package/lib/esm/domain/ServerlessEvent.js.map +0 -1
- package/lib/esm/domain/ServerlessRequest.js.map +0 -1
- package/lib/esm/domain/ServerlessResponse.js.map +0 -1
- package/lib/esm/domain/ServerlessResponseStream.js.map +0 -1
- package/lib/esm/exports.js.map +0 -1
- package/lib/esm/index.js.map +0 -1
- package/lib/esm/utils/getRequestId.js.map +0 -1
- package/lib/esm/utils/isInAWS.js.map +0 -1
- package/lib/esm/utils/setResponseHeaders.js.map +0 -1
- package/src/builder/PlatformServerless.spec.ts +0 -219
- package/src/builder/PlatformServerless.ts +0 -202
- package/src/builder/PlatformServerlessHandler.spec.ts +0 -145
- package/src/builder/PlatformServerlessHandler.ts +0 -121
- package/src/domain/ServerlessContext.spec.ts +0 -106
- package/src/domain/ServerlessContext.ts +0 -60
- package/src/domain/ServerlessEvent.ts +0 -24
- package/src/domain/ServerlessRequest.spec.ts +0 -130
- package/src/domain/ServerlessRequest.ts +0 -106
- package/src/domain/ServerlessResponse.spec.ts +0 -139
- package/src/domain/ServerlessResponse.ts +0 -226
- package/src/domain/ServerlessResponseStream.spec.ts +0 -63
- package/src/domain/ServerlessResponseStream.ts +0 -113
- package/src/exports.ts +0 -1
- package/src/index.ts +0 -14
- package/src/utils/getRequestId.spec.ts +0 -43
- package/src/utils/getRequestId.ts +0 -16
- package/src/utils/isInAWS.ts +0 -8
- package/src/utils/setResponseHeaders.spec.ts +0 -156
- package/src/utils/setResponseHeaders.ts +0 -58
- package/vitest.config.mts +0 -21
|
@@ -1,139 +0,0 @@
|
|
|
1
|
-
import {PlatformServerlessTest} from "@tsed/platform-serverless-testing";
|
|
2
|
-
|
|
3
|
-
import {createServerlessContext} from "../../test/utils/createServerlessContext.js";
|
|
4
|
-
|
|
5
|
-
describe("ServerlessResponse", () => {
|
|
6
|
-
beforeEach(() => PlatformServerlessTest.create());
|
|
7
|
-
afterEach(() => PlatformServerlessTest.reset());
|
|
8
|
-
|
|
9
|
-
describe("isHeadersSent", () => {
|
|
10
|
-
it("should push callback", () => {
|
|
11
|
-
const context = createServerlessContext({
|
|
12
|
-
endpoint: {} as any
|
|
13
|
-
});
|
|
14
|
-
expect(context.response.isHeadersSent).toEqual(false);
|
|
15
|
-
|
|
16
|
-
context.response.destroy();
|
|
17
|
-
|
|
18
|
-
expect(context.response.isHeadersSent).toEqual(true);
|
|
19
|
-
});
|
|
20
|
-
});
|
|
21
|
-
describe("statusCode", () => {
|
|
22
|
-
it("should return the statusCode", () => {
|
|
23
|
-
const context = createServerlessContext({
|
|
24
|
-
endpoint: {} as any
|
|
25
|
-
});
|
|
26
|
-
context.response.statusCode = 201;
|
|
27
|
-
expect(context.response.statusCode).toEqual(201);
|
|
28
|
-
});
|
|
29
|
-
});
|
|
30
|
-
describe("locals()", () => {
|
|
31
|
-
it("should return the locals", () => {
|
|
32
|
-
const context = createServerlessContext({
|
|
33
|
-
endpoint: {} as any
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
expect(context.response.locals).toEqual({});
|
|
37
|
-
});
|
|
38
|
-
});
|
|
39
|
-
describe("contentLength()", () => {
|
|
40
|
-
it("should return the content length", () => {
|
|
41
|
-
const context = createServerlessContext({
|
|
42
|
-
endpoint: {} as any
|
|
43
|
-
});
|
|
44
|
-
context.response.contentLength(300);
|
|
45
|
-
expect(context.response.getContentLength()).toEqual(300);
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
it("should return the default content length", () => {
|
|
49
|
-
const context = createServerlessContext({
|
|
50
|
-
endpoint: {} as any
|
|
51
|
-
});
|
|
52
|
-
context.response.contentLength(" " as any);
|
|
53
|
-
expect(context.response.getContentLength()).toEqual(0);
|
|
54
|
-
});
|
|
55
|
-
});
|
|
56
|
-
describe("location()", () => {
|
|
57
|
-
it("should set location", () => {
|
|
58
|
-
const context = createServerlessContext({
|
|
59
|
-
endpoint: {} as any
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
context.response.location("/path/to");
|
|
63
|
-
|
|
64
|
-
expect(context.response.getHeaders()).toEqual({
|
|
65
|
-
location: "/path/to"
|
|
66
|
-
});
|
|
67
|
-
});
|
|
68
|
-
it("should set location (back)", () => {
|
|
69
|
-
const context = createServerlessContext({
|
|
70
|
-
endpoint: {} as any
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
context.request.headers["Referrer"] = "https://referrer.com";
|
|
74
|
-
context.response.location("back");
|
|
75
|
-
|
|
76
|
-
expect(context.response.getHeaders()).toEqual({
|
|
77
|
-
location: "https://referrer.com"
|
|
78
|
-
});
|
|
79
|
-
});
|
|
80
|
-
});
|
|
81
|
-
describe("redirect()", () => {
|
|
82
|
-
it("should set location", () => {
|
|
83
|
-
const context = createServerlessContext({
|
|
84
|
-
endpoint: {} as any
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
context.response.redirect(301, "/path/to");
|
|
88
|
-
|
|
89
|
-
expect(context.response.getHeaders()).toEqual({
|
|
90
|
-
"content-length": 42,
|
|
91
|
-
location: "/path/to"
|
|
92
|
-
});
|
|
93
|
-
expect(context.response.statusCode).toEqual(301);
|
|
94
|
-
expect(context.response.getBody()).toEqual("Moved Permanently. Redirecting to /path/to");
|
|
95
|
-
});
|
|
96
|
-
it("should set location (back)", () => {
|
|
97
|
-
const context = createServerlessContext({
|
|
98
|
-
endpoint: {} as any
|
|
99
|
-
});
|
|
100
|
-
|
|
101
|
-
context.request.headers["Referrer"] = "https://referrer.com";
|
|
102
|
-
context.response.redirect(301, "back");
|
|
103
|
-
|
|
104
|
-
expect(context.response.getHeaders()).toEqual({
|
|
105
|
-
"content-length": 54,
|
|
106
|
-
location: "https://referrer.com"
|
|
107
|
-
});
|
|
108
|
-
expect(context.response.statusCode).toEqual(301);
|
|
109
|
-
expect(context.response.getStatus()).toEqual(301);
|
|
110
|
-
expect(context.response.getContentLength()).toEqual(54);
|
|
111
|
-
expect(context.response.getContentType()).toEqual("");
|
|
112
|
-
expect(context.response.getBody()).toEqual("Moved Permanently. Redirecting to https://referrer.com");
|
|
113
|
-
});
|
|
114
|
-
});
|
|
115
|
-
describe("setHeader()", () => {
|
|
116
|
-
it("should set location", () => {
|
|
117
|
-
const context = createServerlessContext({
|
|
118
|
-
endpoint: {} as any
|
|
119
|
-
});
|
|
120
|
-
|
|
121
|
-
context.event.headers["Referrer"] = "https://referrer.com";
|
|
122
|
-
context.response.set("location", "back");
|
|
123
|
-
expect(context.response.getHeaders()).toEqual({
|
|
124
|
-
location: "https://referrer.com"
|
|
125
|
-
});
|
|
126
|
-
});
|
|
127
|
-
it("should set location back with default value", () => {
|
|
128
|
-
const context = createServerlessContext({
|
|
129
|
-
endpoint: {} as any
|
|
130
|
-
});
|
|
131
|
-
|
|
132
|
-
context.event.headers["Referrer"] = "";
|
|
133
|
-
context.response.set("location", "back");
|
|
134
|
-
expect(context.response.getHeaders()).toEqual({
|
|
135
|
-
location: "/"
|
|
136
|
-
});
|
|
137
|
-
});
|
|
138
|
-
});
|
|
139
|
-
});
|
|
@@ -1,226 +0,0 @@
|
|
|
1
|
-
import {getValue} from "@tsed/core";
|
|
2
|
-
import {getStatusMessage} from "@tsed/schema";
|
|
3
|
-
import type {APIGatewayProxyEvent} from "aws-lambda";
|
|
4
|
-
import encodeUrl from "encodeurl";
|
|
5
|
-
import mime from "mime";
|
|
6
|
-
|
|
7
|
-
import {ServerlessContext} from "./ServerlessContext.js";
|
|
8
|
-
|
|
9
|
-
export type HeaderValue = boolean | number | string;
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* @platform
|
|
13
|
-
*/
|
|
14
|
-
export class ServerlessResponse<Event extends object = APIGatewayProxyEvent> {
|
|
15
|
-
#status: number = 200;
|
|
16
|
-
#body: any = undefined;
|
|
17
|
-
#headers: Record<string, HeaderValue> = {};
|
|
18
|
-
#locals: Record<string, any> = {};
|
|
19
|
-
#isHeadersSent = false;
|
|
20
|
-
|
|
21
|
-
constructor(protected $ctx: ServerlessContext<Event>) {}
|
|
22
|
-
|
|
23
|
-
get event() {
|
|
24
|
-
return this.$ctx.event;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
get raw() {
|
|
28
|
-
return this.event;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
get request() {
|
|
32
|
-
return this.$ctx.request;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* Get the current statusCode
|
|
37
|
-
*/
|
|
38
|
-
get statusCode() {
|
|
39
|
-
return this.#status;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
set statusCode(status) {
|
|
43
|
-
this.#status = status;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* An object that contains response local variables scoped to the request, and therefore available only to the view(s) rendered during that request / response cycle (if any). Otherwise, this property is identical to app.locals.
|
|
48
|
-
*
|
|
49
|
-
* This property is useful for exposing request-level information such as the request path name, authenticated user, user settings, and so on.
|
|
50
|
-
*/
|
|
51
|
-
get locals() {
|
|
52
|
-
return this.#locals;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
get isHeadersSent() {
|
|
56
|
-
return this.#isHeadersSent;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
getStatus() {
|
|
60
|
-
return this.#status;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Sets the HTTP status for the response.
|
|
65
|
-
*
|
|
66
|
-
* @param status
|
|
67
|
-
*/
|
|
68
|
-
status(status: number) {
|
|
69
|
-
this.#status = status;
|
|
70
|
-
return this;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
set(name: string, value: any) {
|
|
74
|
-
return this.setHeader(name, value);
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* Returns the HTTP response header specified by field. The match is case-insensitive.
|
|
79
|
-
*
|
|
80
|
-
* ```typescript
|
|
81
|
-
* response.get('Content-Type') // => "text/plain"
|
|
82
|
-
* ```
|
|
83
|
-
*
|
|
84
|
-
* @param name
|
|
85
|
-
*/
|
|
86
|
-
get(name: string): string | number | boolean | undefined {
|
|
87
|
-
return getValue(this.#headers, name.toLowerCase());
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
getHeaders(): Record<string, HeaderValue> {
|
|
91
|
-
return {
|
|
92
|
-
...this.#headers
|
|
93
|
-
};
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
hasStatus() {
|
|
97
|
-
return this.#status !== 200;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
/**
|
|
101
|
-
* Set header `field` to `val`, or pass
|
|
102
|
-
* an object of header fields.
|
|
103
|
-
*
|
|
104
|
-
* Examples:
|
|
105
|
-
* ```typescript
|
|
106
|
-
* response.setHeaders({ Accept: 'text/plain', 'X-API-Key': 'tobi' });
|
|
107
|
-
* ```
|
|
108
|
-
*
|
|
109
|
-
* Aliased as `res.header()`.
|
|
110
|
-
*/
|
|
111
|
-
setHeaders(headers: Record<string, HeaderValue>) {
|
|
112
|
-
// apply headers
|
|
113
|
-
Object.entries(headers).forEach(([key, item]) => {
|
|
114
|
-
this.setHeader(key, item);
|
|
115
|
-
});
|
|
116
|
-
|
|
117
|
-
return this;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
setHeader(key: string, item: HeaderValue | null | undefined) {
|
|
121
|
-
if (item !== null && item !== undefined) {
|
|
122
|
-
key = key.toLowerCase();
|
|
123
|
-
|
|
124
|
-
if (key === "location") {
|
|
125
|
-
// "back" is an alias for the referrer
|
|
126
|
-
if (item === "back") {
|
|
127
|
-
item = this.request.get("Referrer") || "/";
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
item = encodeUrl(String(item));
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
this.#headers = {
|
|
134
|
-
...this.#headers,
|
|
135
|
-
[key.toLowerCase()]: item as any
|
|
136
|
-
};
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
return this;
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
/**
|
|
143
|
-
* Set `Content-Type` response header with `type` through `mime.lookup()`
|
|
144
|
-
* when it does not contain "/", or set the Content-Type to `type` otherwise.
|
|
145
|
-
*
|
|
146
|
-
* Examples:
|
|
147
|
-
*
|
|
148
|
-
* res.type('.html');
|
|
149
|
-
* res.type('html');
|
|
150
|
-
* res.type('json');
|
|
151
|
-
* res.type('application/json');
|
|
152
|
-
* res.type('png');
|
|
153
|
-
*/
|
|
154
|
-
contentType(contentType: string) {
|
|
155
|
-
this.setHeader("Content-Type", mime.getType(contentType));
|
|
156
|
-
|
|
157
|
-
return this;
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
contentLength(length: number) {
|
|
161
|
-
this.setHeader("Content-Length", length);
|
|
162
|
-
return this;
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
getContentLength() {
|
|
166
|
-
if (this.get("Content-Length")) {
|
|
167
|
-
return parseInt(this.get("Content-Length") as any, 10) || 0;
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
getContentType() {
|
|
172
|
-
return String(this.get("Content-Type") || "").split(";")[0];
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
/**
|
|
176
|
-
* Redirects to the URL derived from the specified path, with specified status, a positive integer that corresponds to an [HTTP status code](http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html).
|
|
177
|
-
* If not specified, status defaults to `302 Found`.
|
|
178
|
-
*
|
|
179
|
-
* @param status
|
|
180
|
-
* @param url
|
|
181
|
-
*/
|
|
182
|
-
redirect(status: number, url: string) {
|
|
183
|
-
// Set location header
|
|
184
|
-
const address = this.location(url).get("Location");
|
|
185
|
-
const body = `${getStatusMessage(status)}. Redirecting to ${address}`;
|
|
186
|
-
|
|
187
|
-
this.status(status).set("Content-Length", Buffer.byteLength(body)).body(body);
|
|
188
|
-
|
|
189
|
-
return this;
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
/**
|
|
193
|
-
* Sets the response Location HTTP header to the specified path parameter.
|
|
194
|
-
*
|
|
195
|
-
* @param url
|
|
196
|
-
*/
|
|
197
|
-
location(url: string) {
|
|
198
|
-
return this.set("Location", url);
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
/**
|
|
202
|
-
* Send any data to your consumer.
|
|
203
|
-
*
|
|
204
|
-
* This method accept a ReadableStream, a plain object, boolean, string, number, null and undefined data.
|
|
205
|
-
* It choose the better way to send the data.
|
|
206
|
-
*
|
|
207
|
-
* @param body
|
|
208
|
-
*/
|
|
209
|
-
body(body: any) {
|
|
210
|
-
this.#body = body;
|
|
211
|
-
|
|
212
|
-
return this;
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
getBody() {
|
|
216
|
-
return this.#body;
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
isDone(): boolean {
|
|
220
|
-
return this.#isHeadersSent;
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
destroy() {
|
|
224
|
-
this.#isHeadersSent = true;
|
|
225
|
-
}
|
|
226
|
-
}
|
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
import {ServerlessResponseStream} from "./ServerlessResponseStream.js";
|
|
2
|
-
|
|
3
|
-
describe("ServerlessResponseStream", () => {
|
|
4
|
-
describe("streamifyResponse", () => {
|
|
5
|
-
describe("when isInAWS", () => {
|
|
6
|
-
beforeEach(() => {
|
|
7
|
-
// @ts-ignore
|
|
8
|
-
globalThis.awslambda = {
|
|
9
|
-
streamifyResponse: vi.fn().mockImplementation((handler) => handler)
|
|
10
|
-
};
|
|
11
|
-
});
|
|
12
|
-
afterEach(() => {
|
|
13
|
-
// @ts-ignore
|
|
14
|
-
delete globalThis.awslambda;
|
|
15
|
-
});
|
|
16
|
-
|
|
17
|
-
it("should wrap then handler without fallback responseStream", async () => {
|
|
18
|
-
const handler = vi.fn();
|
|
19
|
-
const event = {};
|
|
20
|
-
const context = {};
|
|
21
|
-
const responseStream = new ServerlessResponseStream();
|
|
22
|
-
|
|
23
|
-
vi.spyOn(ServerlessResponseStream as any, "buildResponse").mockReturnValue({});
|
|
24
|
-
|
|
25
|
-
const wrapped = ServerlessResponseStream.streamifyResponse(handler);
|
|
26
|
-
|
|
27
|
-
const result = await wrapped(event as any, responseStream, context as any);
|
|
28
|
-
|
|
29
|
-
expect(result).toEqual(undefined);
|
|
30
|
-
expect(handler).toHaveBeenCalledWith(event, responseStream, context);
|
|
31
|
-
// @ts-ignore
|
|
32
|
-
expect(ServerlessResponseStream.buildResponse).not.toHaveBeenCalled();
|
|
33
|
-
});
|
|
34
|
-
it("should wrap then handler with fallback responseStream", async () => {
|
|
35
|
-
const handler = vi.fn();
|
|
36
|
-
const event = {};
|
|
37
|
-
const context = {};
|
|
38
|
-
|
|
39
|
-
vi.spyOn(ServerlessResponseStream as any, "buildResponse").mockReturnValue({
|
|
40
|
-
statusCode: 200
|
|
41
|
-
});
|
|
42
|
-
vi.spyOn(console, "warn").mockReturnValue(undefined);
|
|
43
|
-
|
|
44
|
-
const wrapped = ServerlessResponseStream.streamifyResponse(handler);
|
|
45
|
-
|
|
46
|
-
const result = await wrapped(event as any, context as any);
|
|
47
|
-
|
|
48
|
-
expect(handler).toHaveBeenCalledWith(event, expect.any(ServerlessResponseStream), context);
|
|
49
|
-
|
|
50
|
-
expect(result).toEqual({
|
|
51
|
-
statusCode: 200
|
|
52
|
-
});
|
|
53
|
-
// @ts-ignore
|
|
54
|
-
expect(ServerlessResponseStream.buildResponse).toHaveBeenCalledWith(expect.any(ServerlessResponseStream));
|
|
55
|
-
expect(console.warn).toHaveBeenCalledWith({
|
|
56
|
-
event: "WRONG_AWS_STREAM_CONFIGURATION",
|
|
57
|
-
message:
|
|
58
|
-
"The lambda didn't receive a ResponseStream from AWS. Ts.ED therefore generated a ServerlessResponseStream as a fallback. This mode is not optimized for use in PRODUCTION. Please check the AWS configuration of your lambda."
|
|
59
|
-
});
|
|
60
|
-
});
|
|
61
|
-
});
|
|
62
|
-
});
|
|
63
|
-
});
|
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
import {Stream} from "node:stream";
|
|
2
|
-
|
|
3
|
-
import type {APIGatewayProxyEventV2, Callback, Context, Handler} from "aws-lambda";
|
|
4
|
-
|
|
5
|
-
import {isInAWS} from "../utils/isInAWS.js";
|
|
6
|
-
|
|
7
|
-
export type ServerlessResponseMeta = {statusCode: number; headers: Record<string, string>};
|
|
8
|
-
|
|
9
|
-
declare global {
|
|
10
|
-
const awsLambda: {
|
|
11
|
-
streamifyResponse(handler: Handler): unknown | Promise<unknown>;
|
|
12
|
-
HttpResponseStream: {
|
|
13
|
-
from(resStream: ServerlessResponseStream | unknown, meta: ServerlessResponseMeta): ServerlessResponseStream;
|
|
14
|
-
};
|
|
15
|
-
};
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export type RequestHandler = (
|
|
19
|
-
ev: APIGatewayProxyEventV2,
|
|
20
|
-
streamResponse?: ServerlessResponseStream,
|
|
21
|
-
ctx?: Context,
|
|
22
|
-
callback?: Callback
|
|
23
|
-
) => unknown | Promise<unknown>;
|
|
24
|
-
|
|
25
|
-
export class ServerlessResponseStream extends Stream.Writable {
|
|
26
|
-
public _meta: ServerlessResponseMeta = {statusCode: 200, headers: {}};
|
|
27
|
-
#response: Buffer[];
|
|
28
|
-
|
|
29
|
-
constructor() {
|
|
30
|
-
super();
|
|
31
|
-
this.#response = [];
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
static setMeta(resStream: ServerlessResponseStream, meta: ServerlessResponseMeta) {
|
|
35
|
-
if (isInAWS()) {
|
|
36
|
-
// @ts-ignore
|
|
37
|
-
return awslambda.HttpResponseStream.from(resStream, meta);
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
resStream.setMeta(meta);
|
|
41
|
-
|
|
42
|
-
return resStream;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
static streamifyResponse(handler: RequestHandler): RequestHandler {
|
|
46
|
-
// Check for global awslambda
|
|
47
|
-
if (isInAWS()) {
|
|
48
|
-
// @ts-ignore
|
|
49
|
-
return awslambda.streamifyResponse(
|
|
50
|
-
async (event: APIGatewayProxyEventV2, responseStream: ServerlessResponseStream | Context | undefined, context?: Context) => {
|
|
51
|
-
const isFallbackMode = context === undefined;
|
|
52
|
-
|
|
53
|
-
if (context === undefined) {
|
|
54
|
-
context = responseStream as Context;
|
|
55
|
-
responseStream = new ServerlessResponseStream();
|
|
56
|
-
|
|
57
|
-
console.warn({
|
|
58
|
-
event: "WRONG_AWS_STREAM_CONFIGURATION",
|
|
59
|
-
message:
|
|
60
|
-
"The lambda didn't receive a ResponseStream from AWS. Ts.ED therefore generated a ServerlessResponseStream as a fallback. This mode is not optimized for use in PRODUCTION. Please check the AWS configuration of your lambda."
|
|
61
|
-
});
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
await handler(event, responseStream as ServerlessResponseStream, context);
|
|
65
|
-
|
|
66
|
-
if (isFallbackMode) {
|
|
67
|
-
return ServerlessResponseStream.buildResponse(responseStream as ServerlessResponseStream);
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
);
|
|
71
|
-
} else {
|
|
72
|
-
return async (event, response, context) => {
|
|
73
|
-
const args: any[] = patchArgs([event, response, context]);
|
|
74
|
-
const responseStream: ServerlessResponseStream = args[1];
|
|
75
|
-
|
|
76
|
-
await (handler as any)(...args);
|
|
77
|
-
|
|
78
|
-
return ServerlessResponseStream.buildResponse(responseStream);
|
|
79
|
-
};
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
private static buildResponse(responseStream: ServerlessResponseStream) {
|
|
84
|
-
return {
|
|
85
|
-
...responseStream._meta,
|
|
86
|
-
body: responseStream.getBufferedData().toString()
|
|
87
|
-
};
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
// streams, `chunk` may be any JavaScript value.
|
|
91
|
-
_write(chunk: any, encoding: BufferEncoding, callback: (error?: Error | null) => void): void {
|
|
92
|
-
this.#response.push(Buffer.from(chunk, encoding));
|
|
93
|
-
callback();
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
setMeta(meta: ServerlessResponseMeta) {
|
|
97
|
-
this._meta = meta;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
getBufferedData(): Buffer {
|
|
101
|
-
return Buffer.concat(this.#response);
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
function patchArgs(args: any[]) {
|
|
106
|
-
if (!(args[1] instanceof ServerlessResponseStream)) {
|
|
107
|
-
const responseStream = new ServerlessResponseStream();
|
|
108
|
-
|
|
109
|
-
return [args[0], responseStream, ...args.splice(1, -1)];
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
return args;
|
|
113
|
-
}
|
package/src/exports.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {Operation as Lambda} from "@tsed/schema";
|
package/src/index.ts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @file Automatically generated by @tsed/barrels.
|
|
3
|
-
*/
|
|
4
|
-
export * from "./builder/PlatformServerless.js";
|
|
5
|
-
export * from "./builder/PlatformServerlessHandler.js";
|
|
6
|
-
export * from "./domain/ServerlessContext.js";
|
|
7
|
-
export * from "./domain/ServerlessEvent.js";
|
|
8
|
-
export * from "./domain/ServerlessRequest.js";
|
|
9
|
-
export * from "./domain/ServerlessResponse.js";
|
|
10
|
-
export * from "./domain/ServerlessResponseStream.js";
|
|
11
|
-
export * from "./exports.js";
|
|
12
|
-
export * from "./utils/getRequestId.js";
|
|
13
|
-
export * from "./utils/isInAWS.js";
|
|
14
|
-
export * from "./utils/setResponseHeaders.js";
|
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import {getRequestId} from "./getRequestId.js";
|
|
2
|
-
|
|
3
|
-
describe("getRequestId", () => {
|
|
4
|
-
it("should return request id (uuid)", () => {
|
|
5
|
-
const event: any = {};
|
|
6
|
-
const context: any = {};
|
|
7
|
-
|
|
8
|
-
expect(typeof getRequestId(event, context)).toEqual("string");
|
|
9
|
-
});
|
|
10
|
-
|
|
11
|
-
it("should return request id (requestId)", () => {
|
|
12
|
-
const event: any = {
|
|
13
|
-
requestContext: {
|
|
14
|
-
requestId: "requestId"
|
|
15
|
-
}
|
|
16
|
-
};
|
|
17
|
-
const context: any = {};
|
|
18
|
-
|
|
19
|
-
expect(getRequestId(event, context)).toEqual("requestId");
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
it("should return request id (awsRequestId)", () => {
|
|
23
|
-
const event: any = {};
|
|
24
|
-
const context: any = {
|
|
25
|
-
awsRequestId: "awsRequestId"
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
expect(getRequestId(event, context)).toEqual("awsRequestId");
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
it("should return request id (headers)", () => {
|
|
32
|
-
const event: any = {
|
|
33
|
-
headers: {
|
|
34
|
-
"x-request-id": "x-request-id"
|
|
35
|
-
}
|
|
36
|
-
};
|
|
37
|
-
const context: any = {
|
|
38
|
-
awsRequestId: "awsRequestId"
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
expect(getRequestId(event, context)).toEqual("x-request-id");
|
|
42
|
-
});
|
|
43
|
-
});
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import type {Context} from "aws-lambda";
|
|
2
|
-
import {v4} from "uuid";
|
|
3
|
-
|
|
4
|
-
import type {ServerlessEvent} from "../domain/ServerlessEvent.js";
|
|
5
|
-
|
|
6
|
-
export function getRequestId(event: ServerlessEvent, context: Context) {
|
|
7
|
-
if ("headers" in event && event.headers["x-request-id"]) {
|
|
8
|
-
return event.headers["x-request-id"];
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
if ("requestContext" in event) {
|
|
12
|
-
return event?.requestContext?.requestId;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
return context?.awsRequestId || v4().replace(/-/gi, "");
|
|
16
|
-
}
|