@tsed/platform-fastify 8.5.0-beta.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/.swcrc +21 -0
- package/lib/esm/components/PlatformFastify.js +283 -0
- package/lib/esm/components/PlatformFastify.js.map +1 -0
- package/lib/esm/index.js +9 -0
- package/lib/esm/index.js.map +1 -0
- package/lib/esm/interfaces/PlatformFastifySettings.js +2 -0
- package/lib/esm/interfaces/PlatformFastifySettings.js.map +1 -0
- package/lib/esm/interfaces/interfaces.js +2 -0
- package/lib/esm/interfaces/interfaces.js.map +1 -0
- package/lib/esm/services/PlatformFastifyRequest.js +34 -0
- package/lib/esm/services/PlatformFastifyRequest.js.map +1 -0
- package/lib/esm/services/PlatformFastifyResponse.js +76 -0
- package/lib/esm/services/PlatformFastifyResponse.js.map +1 -0
- package/lib/types/components/PlatformFastify.d.ts +63 -0
- package/lib/types/index.d.ts +8 -0
- package/lib/types/interfaces/PlatformFastifySettings.d.ts +4 -0
- package/lib/types/interfaces/interfaces.d.ts +22 -0
- package/lib/types/services/PlatformFastifyRequest.d.ts +41 -0
- package/lib/types/services/PlatformFastifyResponse.d.ts +56 -0
- package/package.json +118 -0
- package/readme.md +67 -0
- package/src/components/PlatformFastify.spec.ts +34 -0
- package/src/components/PlatformFastify.ts +361 -0
- package/src/index.ts +8 -0
- package/src/interfaces/PlatformFastifySettings.ts +5 -0
- package/src/interfaces/interfaces.ts +26 -0
- package/src/services/PlatformFastifyRequest.spec.ts +54 -0
- package/src/services/PlatformFastifyRequest.ts +61 -0
- package/src/services/PlatformFastifyResponse.spec.ts +114 -0
- package/src/services/PlatformFastifyResponse.ts +100 -0
- package/vitest.config.mts +21 -0
package/package.json
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tsed/platform-fastify",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "8.5.0-beta.1",
|
|
5
|
+
"description": "Fastify package for Ts.ED framework",
|
|
6
|
+
"source": "./src/index.ts",
|
|
7
|
+
"main": "./lib/esm/index.js",
|
|
8
|
+
"module": "./lib/esm/index.js",
|
|
9
|
+
"typings": "./lib/types/index.d.ts",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"@tsed/source": "./src/index.ts",
|
|
13
|
+
"types": "./lib/types/index.d.ts",
|
|
14
|
+
"import": "./lib/esm/index.js",
|
|
15
|
+
"default": "./lib/esm/index.js"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"scripts": {
|
|
19
|
+
"build": "yarn barrels && yarn build:ts",
|
|
20
|
+
"barrels": "barrels",
|
|
21
|
+
"start": "node --import @swc-node/register/esm-register test/app/index.ts",
|
|
22
|
+
"test": "vitest run -u",
|
|
23
|
+
"build:ts": "tsc --build tsconfig.json",
|
|
24
|
+
"test:ci": "vitest run --coverage.thresholds.autoUpdate=true"
|
|
25
|
+
},
|
|
26
|
+
"keywords": [
|
|
27
|
+
"Fastify",
|
|
28
|
+
"TypeScript",
|
|
29
|
+
"Decorator",
|
|
30
|
+
"decorators",
|
|
31
|
+
"fastify",
|
|
32
|
+
"Controller",
|
|
33
|
+
"Inject",
|
|
34
|
+
"ioc",
|
|
35
|
+
"di",
|
|
36
|
+
"mvc",
|
|
37
|
+
"swagger",
|
|
38
|
+
"swagger ui",
|
|
39
|
+
"ESM",
|
|
40
|
+
"ES6",
|
|
41
|
+
"server",
|
|
42
|
+
"rest",
|
|
43
|
+
"api",
|
|
44
|
+
"validation"
|
|
45
|
+
],
|
|
46
|
+
"author": "Romain Lenzotti",
|
|
47
|
+
"license": "MIT",
|
|
48
|
+
"bugs": {
|
|
49
|
+
"url": "https://github.com/tsedio/tsed/issues"
|
|
50
|
+
},
|
|
51
|
+
"homepage": "https://github.com/tsedio/tsed/tree/production/packages/platform/platform-fastify",
|
|
52
|
+
"repository": "https://github.com/tsedio/tsed",
|
|
53
|
+
"dependencies": {
|
|
54
|
+
"@fastify/accepts": ">=5.0.2",
|
|
55
|
+
"@fastify/middie": ">=9.0.2",
|
|
56
|
+
"@fastify/static": ">=8.0.4",
|
|
57
|
+
"content-disposition": ">=0.5.4",
|
|
58
|
+
"tslib": "2.7.0"
|
|
59
|
+
},
|
|
60
|
+
"devDependencies": {
|
|
61
|
+
"@fastify/cookie": "11.0.1",
|
|
62
|
+
"@fastify/formbody": "8.0.1",
|
|
63
|
+
"@fastify/session": "11.0.1",
|
|
64
|
+
"@tsed/barrels": "8.5.0-beta.1",
|
|
65
|
+
"@tsed/core": "8.5.0-beta.1",
|
|
66
|
+
"@tsed/di": "8.5.0-beta.1",
|
|
67
|
+
"@tsed/platform-http": "8.5.0-beta.1",
|
|
68
|
+
"@tsed/platform-test-sdk": "8.5.0-beta.1",
|
|
69
|
+
"@types/content-disposition": "^0.5.4",
|
|
70
|
+
"cross-env": "7.0.3",
|
|
71
|
+
"fastify": "5.2.1",
|
|
72
|
+
"fastify-raw-body": "5.0.0",
|
|
73
|
+
"ts-node": "10.9.2"
|
|
74
|
+
},
|
|
75
|
+
"peerDependencies": {
|
|
76
|
+
"@fastify/accepts": ">=4.3.0",
|
|
77
|
+
"@fastify/cookie": ">=9.3.1",
|
|
78
|
+
"@fastify/formbody": ">=7.4.0",
|
|
79
|
+
"@tsed/core": ">=8.5.0-beta.1",
|
|
80
|
+
"@tsed/di": ">=8.5.0-beta.1",
|
|
81
|
+
"@tsed/json-mapper": ">=8.5.0-beta.1",
|
|
82
|
+
"@tsed/logger": ">=7.0.0",
|
|
83
|
+
"@tsed/openspec": ">=8.5.0-beta.1",
|
|
84
|
+
"@tsed/platform-http": ">=8.5.0-beta.1",
|
|
85
|
+
"@tsed/schema": ">=8.5.0-beta.1",
|
|
86
|
+
"fastify": ">=5.1.0",
|
|
87
|
+
"fastify-raw-body": ">=5.0.0"
|
|
88
|
+
},
|
|
89
|
+
"peerDependenciesMeta": {
|
|
90
|
+
"@tsed/common": {
|
|
91
|
+
"optional": false
|
|
92
|
+
},
|
|
93
|
+
"@tsed/core": {
|
|
94
|
+
"optional": false
|
|
95
|
+
},
|
|
96
|
+
"@tsed/di": {
|
|
97
|
+
"optional": false
|
|
98
|
+
},
|
|
99
|
+
"@tsed/json-mapper": {
|
|
100
|
+
"optional": false
|
|
101
|
+
},
|
|
102
|
+
"@tsed/logger": {
|
|
103
|
+
"optional": false
|
|
104
|
+
},
|
|
105
|
+
"@tsed/openspec": {
|
|
106
|
+
"optional": false
|
|
107
|
+
},
|
|
108
|
+
"@tsed/schema": {
|
|
109
|
+
"optional": false
|
|
110
|
+
},
|
|
111
|
+
"fastify": {
|
|
112
|
+
"optional": false
|
|
113
|
+
}
|
|
114
|
+
},
|
|
115
|
+
"publishConfig": {
|
|
116
|
+
"tag": "beta"
|
|
117
|
+
}
|
|
118
|
+
}
|
package/readme.md
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
<p style="text-align: center" align="center">
|
|
2
|
+
<a href="https://tsed.io" target="_blank"><img src="https://tsed.io/tsed-og.png" width="200" alt="Ts.ED logo"/></a>
|
|
3
|
+
</p>
|
|
4
|
+
|
|
5
|
+
<div align="center">
|
|
6
|
+
<h1>Platform Fastify</h1>
|
|
7
|
+
|
|
8
|
+
[](https://github.com/tsedio/tsed/actions?query=workflow%3A%22Build+%26+Release%22)
|
|
9
|
+
[](https://github.com/tsedio/tsed/blob/master/CONTRIBUTING.md)
|
|
10
|
+
[](https://coveralls.io/github/tsedio/tsed?branch=production)
|
|
11
|
+
[](https://badge.fury.io/js/%40tsed%2Fcommon)
|
|
12
|
+
[](https://github.com/semantic-release/semantic-release)
|
|
13
|
+
[](https://github.com/prettier/prettier)
|
|
14
|
+
[](https://github.com/sponsors/romakita)
|
|
15
|
+
[](https://opencollective.com/tsed)
|
|
16
|
+
|
|
17
|
+
</div>
|
|
18
|
+
|
|
19
|
+
<div align="center">
|
|
20
|
+
<a href="https://tsed.io/">Website</a>
|
|
21
|
+
<span> • </span>
|
|
22
|
+
<a href="https://tsed.io/getting-started/">Getting started</a>
|
|
23
|
+
<span> • </span>
|
|
24
|
+
<a href="https://api.tsed.io/rest/slack/tsedio/tsed">Slack</a>
|
|
25
|
+
<span> • </span>
|
|
26
|
+
<a href="https://twitter.com/TsED_io">Twitter</a>
|
|
27
|
+
</div>
|
|
28
|
+
|
|
29
|
+
<hr />
|
|
30
|
+
|
|
31
|
+
A package of the Ts.ED framework. See the [Getting Started Guide](https://tsed.io/getting-started/).
|
|
32
|
+
|
|
33
|
+
## Installation
|
|
34
|
+
|
|
35
|
+
Run npm command (or yarn):
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
npm install --save @tsed/platform-fastify fastify
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Contributors
|
|
42
|
+
|
|
43
|
+
Please read [contributing guidelines here](https://tsed.io/CONTRIBUTING.html)
|
|
44
|
+
|
|
45
|
+
<a href="https://github.com/tsedio/ts-express-decorators/graphs/contributors"><img src="https://opencollective.com/tsed/contributors.svg?width=890" /></a>
|
|
46
|
+
|
|
47
|
+
## Backers
|
|
48
|
+
|
|
49
|
+
Thank you to all our backers! 🙏 [[Become a backer](https://opencollective.com/tsed#backer)]
|
|
50
|
+
|
|
51
|
+
<a href="https://opencollective.com/tsed#backers" target="_blank"><img src="https://opencollective.com/tsed/backers.svg?width=890"></a>
|
|
52
|
+
|
|
53
|
+
## Sponsors
|
|
54
|
+
|
|
55
|
+
Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [[Become a sponsor](https://opencollective.com/tsed#sponsor)]
|
|
56
|
+
|
|
57
|
+
## License
|
|
58
|
+
|
|
59
|
+
The MIT License (MIT)
|
|
60
|
+
|
|
61
|
+
Copyright (c) 2016 - today Romain Lenzotti
|
|
62
|
+
|
|
63
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
64
|
+
|
|
65
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
66
|
+
|
|
67
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import {PlatformBuilder} from "@tsed/platform-http";
|
|
2
|
+
|
|
3
|
+
import {PlatformFastify} from "./PlatformFastify.js";
|
|
4
|
+
|
|
5
|
+
class Server {}
|
|
6
|
+
|
|
7
|
+
describe("PlatformFastify", () => {
|
|
8
|
+
describe("create()", () => {
|
|
9
|
+
beforeEach(() => {
|
|
10
|
+
vi.spyOn(PlatformBuilder, "create").mockReturnValue({} as never);
|
|
11
|
+
});
|
|
12
|
+
afterEach(() => vi.resetAllMocks());
|
|
13
|
+
it("should create platform", () => {
|
|
14
|
+
PlatformFastify.create(Server, {});
|
|
15
|
+
|
|
16
|
+
expect(PlatformBuilder.create).toHaveBeenCalledWith(Server, {
|
|
17
|
+
adapter: PlatformFastify
|
|
18
|
+
});
|
|
19
|
+
});
|
|
20
|
+
});
|
|
21
|
+
describe("bootstrap()", () => {
|
|
22
|
+
beforeEach(() => {
|
|
23
|
+
vi.spyOn(PlatformBuilder, "bootstrap").mockReturnValue({} as never);
|
|
24
|
+
});
|
|
25
|
+
afterEach(() => vi.resetAllMocks());
|
|
26
|
+
it("should create platform", async () => {
|
|
27
|
+
await PlatformFastify.bootstrap(Server, {});
|
|
28
|
+
|
|
29
|
+
expect(PlatformBuilder.bootstrap).toHaveBeenCalledWith(Server, {
|
|
30
|
+
adapter: PlatformFastify
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
});
|
|
@@ -0,0 +1,361 @@
|
|
|
1
|
+
import * as Http from "node:http";
|
|
2
|
+
import {IncomingMessage, ServerResponse} from "node:http";
|
|
3
|
+
import * as Https from "node:https";
|
|
4
|
+
|
|
5
|
+
import fastifyMiddie from "@fastify/middie";
|
|
6
|
+
import fastifyStatics from "@fastify/static";
|
|
7
|
+
import {type Env, isFunction, isString, ReturnHostInfoFromPort, Type} from "@tsed/core";
|
|
8
|
+
import {constant, inject, logger, runInContext} from "@tsed/di";
|
|
9
|
+
import {NotFound} from "@tsed/exceptions";
|
|
10
|
+
import {PlatformExceptions} from "@tsed/platform-exceptions";
|
|
11
|
+
import {
|
|
12
|
+
adapter,
|
|
13
|
+
createContext,
|
|
14
|
+
createServer,
|
|
15
|
+
Platform,
|
|
16
|
+
PlatformAdapter,
|
|
17
|
+
PlatformApplication,
|
|
18
|
+
PlatformBuilder,
|
|
19
|
+
PlatformHandler,
|
|
20
|
+
PlatformMulterSettings,
|
|
21
|
+
PlatformRequest,
|
|
22
|
+
PlatformResponse,
|
|
23
|
+
PlatformStaticsOptions
|
|
24
|
+
} from "@tsed/platform-http";
|
|
25
|
+
import {PlatformHandlerMetadata, PlatformHandlerType, PlatformLayer} from "@tsed/platform-router";
|
|
26
|
+
import Fastify, {FastifyInstance, FastifyReply, FastifyRequest} from "fastify";
|
|
27
|
+
|
|
28
|
+
import type {PlatformFastifyPluginLoadingOptions, PlatformFastifyPluginSettings} from "../interfaces/interfaces.js";
|
|
29
|
+
import type {PlatformFastifySettings} from "../interfaces/PlatformFastifySettings.js";
|
|
30
|
+
import {PlatformFastifyRequest} from "../services/PlatformFastifyRequest.js";
|
|
31
|
+
import {PlatformFastifyResponse} from "../services/PlatformFastifyResponse.js";
|
|
32
|
+
|
|
33
|
+
declare global {
|
|
34
|
+
namespace TsED {
|
|
35
|
+
export interface Application extends FastifyInstance {}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
namespace TsED {}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export class PlatformFastify extends PlatformAdapter<FastifyInstance> {
|
|
42
|
+
readonly NAME = "fastify";
|
|
43
|
+
|
|
44
|
+
readonly providers = [
|
|
45
|
+
{
|
|
46
|
+
token: PlatformHandler
|
|
47
|
+
},
|
|
48
|
+
{token: PlatformApplication},
|
|
49
|
+
{token: Platform},
|
|
50
|
+
{
|
|
51
|
+
token: PlatformResponse,
|
|
52
|
+
useClass: PlatformFastifyResponse
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
token: PlatformRequest,
|
|
56
|
+
useClass: PlatformFastifyRequest
|
|
57
|
+
}
|
|
58
|
+
];
|
|
59
|
+
private staticsDecorated = false;
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Create new serverless application. In this mode, the component scan are disabled.
|
|
63
|
+
* @param module
|
|
64
|
+
* @param settings
|
|
65
|
+
*/
|
|
66
|
+
static create(module: Type<any>, settings: Partial<TsED.Configuration> = {}) {
|
|
67
|
+
return PlatformBuilder.create<FastifyInstance>(module, {
|
|
68
|
+
...settings,
|
|
69
|
+
adapter: PlatformFastify
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Bootstrap a server application
|
|
75
|
+
* @param module
|
|
76
|
+
* @param settings
|
|
77
|
+
*/
|
|
78
|
+
static bootstrap(module: Type<any>, settings: Partial<TsED.Configuration> = {}) {
|
|
79
|
+
return PlatformBuilder.bootstrap<FastifyInstance>(module, {
|
|
80
|
+
...settings,
|
|
81
|
+
adapter: PlatformFastify
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
mapLayers(layers: PlatformLayer[]) {
|
|
86
|
+
const {app} = this;
|
|
87
|
+
const rawApp: FastifyInstance = app.getApp();
|
|
88
|
+
|
|
89
|
+
layers.forEach((layer) => {
|
|
90
|
+
switch (layer.method) {
|
|
91
|
+
case "use":
|
|
92
|
+
if ((rawApp as any).use) {
|
|
93
|
+
(rawApp as any).use(...layer.getArgs());
|
|
94
|
+
}
|
|
95
|
+
return;
|
|
96
|
+
case "statics":
|
|
97
|
+
this.statics(layer.path as string, layer.opts as any);
|
|
98
|
+
|
|
99
|
+
// rawApp.register();
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
try {
|
|
103
|
+
rawApp.route({
|
|
104
|
+
method: layer.method.toUpperCase() as any,
|
|
105
|
+
url: layer.path as any,
|
|
106
|
+
handler: this.compose(layer),
|
|
107
|
+
config: {
|
|
108
|
+
rawBody: layer.handlers.some((handler) => handler.opts?.paramsTypes?.RAW_BODY)
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
} catch (er) {
|
|
112
|
+
logger().warn({
|
|
113
|
+
error_name: er.code,
|
|
114
|
+
error_message: er.message
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
mapHandler(handler: (...args: any[]) => any, metadata: PlatformHandlerMetadata) {
|
|
121
|
+
if (metadata.isRawMiddleware()) {
|
|
122
|
+
return handler;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
switch (metadata.type) {
|
|
126
|
+
case PlatformHandlerType.MIDDLEWARE:
|
|
127
|
+
return (request: IncomingMessage, _: ServerResponse, done: (err?: any) => void) => {
|
|
128
|
+
const {$ctx} = request;
|
|
129
|
+
|
|
130
|
+
$ctx.next = done;
|
|
131
|
+
|
|
132
|
+
return runInContext($ctx, () => handler($ctx));
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
default:
|
|
136
|
+
return async (request: FastifyRequest, _: FastifyReply, done: (err?: any) => void) => {
|
|
137
|
+
const {$ctx} = request;
|
|
138
|
+
|
|
139
|
+
$ctx.next = done;
|
|
140
|
+
|
|
141
|
+
await runInContext($ctx, () => handler($ctx));
|
|
142
|
+
|
|
143
|
+
if (metadata.type === PlatformHandlerType.CTX_FN) {
|
|
144
|
+
done();
|
|
145
|
+
}
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
async useContext() {
|
|
151
|
+
const {app} = this;
|
|
152
|
+
const invoke = createContext();
|
|
153
|
+
const rawApp: FastifyInstance = app.getApp();
|
|
154
|
+
|
|
155
|
+
rawApp.addHook("onRequest", async (request, reply) => {
|
|
156
|
+
const $ctx = invoke({
|
|
157
|
+
request: request as any,
|
|
158
|
+
response: reply as any
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
($ctx.request.getReq() as any).$ctx = $ctx;
|
|
162
|
+
|
|
163
|
+
await $ctx.start();
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
rawApp.addHook("onResponse", async (request, reply) => {
|
|
167
|
+
const {$ctx} = request;
|
|
168
|
+
($ctx.request.getReq() as any).$ctx = undefined;
|
|
169
|
+
await $ctx.finish();
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
await rawApp.register(fastifyMiddie, {
|
|
173
|
+
hook: "onRequest"
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
const plugins = await this.resolvePlugins();
|
|
177
|
+
|
|
178
|
+
for (const plugin of plugins) {
|
|
179
|
+
// console.log("register", (plugin.use as any)[Symbol.for("plugin-meta")].name, plugin.options)
|
|
180
|
+
await rawApp.register(plugin.use as any, plugin.options!);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
createApp() {
|
|
185
|
+
const {app, ...props} = constant<PlatformFastifySettings>("fastify") || {};
|
|
186
|
+
const httpPort = constant<number | false>("httpPort");
|
|
187
|
+
const httpOptions = constant<Http.ServerOptions>("httpOptions");
|
|
188
|
+
const httpsPort = constant<number | false>("httpsPort");
|
|
189
|
+
const httpsOptions = constant<Https.ServerOptions>("httpsOptions");
|
|
190
|
+
const opts = {
|
|
191
|
+
...props,
|
|
192
|
+
ignoreTrailingSlash: true,
|
|
193
|
+
http:
|
|
194
|
+
httpPort !== false
|
|
195
|
+
? {
|
|
196
|
+
...httpOptions
|
|
197
|
+
}
|
|
198
|
+
: null,
|
|
199
|
+
https:
|
|
200
|
+
httpsPort !== false
|
|
201
|
+
? {
|
|
202
|
+
...httpsOptions
|
|
203
|
+
}
|
|
204
|
+
: null
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
const instance: FastifyInstance = app || Fastify(opts);
|
|
208
|
+
|
|
209
|
+
instance.decorateRequest("$ctx", null as never);
|
|
210
|
+
instance.decorateReply("locals", null);
|
|
211
|
+
|
|
212
|
+
return {
|
|
213
|
+
app: instance,
|
|
214
|
+
callback: () => {
|
|
215
|
+
return async (request: IncomingMessage, response: ServerResponse) => {
|
|
216
|
+
await instance.ready();
|
|
217
|
+
instance.server.emit("request", request, response);
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
afterLoadRoutes() {
|
|
224
|
+
const rawApp: FastifyInstance = this.app.getApp();
|
|
225
|
+
|
|
226
|
+
rawApp.setErrorHandler((error, request, reply) => {
|
|
227
|
+
const {$ctx} = request;
|
|
228
|
+
$ctx.error = $ctx.error || error;
|
|
229
|
+
|
|
230
|
+
return inject<PlatformExceptions>(PlatformExceptions)?.catch(error, $ctx);
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
rawApp.setNotFoundHandler((request, reply) => {
|
|
234
|
+
const {$ctx} = request;
|
|
235
|
+
|
|
236
|
+
return inject<PlatformExceptions>(PlatformExceptions)?.catch(new NotFound(`Resource "${request.originalUrl}" not found`), $ctx);
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
return Promise.resolve();
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
getServers(): any[] {
|
|
243
|
+
const httpsPort = constant<string | false>("httpsPort");
|
|
244
|
+
const httpPort = constant<string | false>("httpPort");
|
|
245
|
+
|
|
246
|
+
const listen = (hostinfo: ReturnHostInfoFromPort) =>
|
|
247
|
+
this.app.getApp().listen({
|
|
248
|
+
host: hostinfo.address,
|
|
249
|
+
port: hostinfo.port
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
const server = () => this.app.getApp().server;
|
|
253
|
+
|
|
254
|
+
return [
|
|
255
|
+
createServer({
|
|
256
|
+
port: httpsPort,
|
|
257
|
+
type: "https",
|
|
258
|
+
token: Https.Server,
|
|
259
|
+
server,
|
|
260
|
+
listen
|
|
261
|
+
}),
|
|
262
|
+
createServer({
|
|
263
|
+
port: httpPort,
|
|
264
|
+
type: "http",
|
|
265
|
+
token: Http.Server,
|
|
266
|
+
server,
|
|
267
|
+
listen
|
|
268
|
+
})
|
|
269
|
+
];
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
bodyParser(type: string, opts: Record<string, any> | undefined): any {
|
|
273
|
+
return null;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
multipart(options: PlatformMulterSettings): any {
|
|
277
|
+
logger().warn({
|
|
278
|
+
event: "PLATFORM_UNSUPPORTED_FEATURE",
|
|
279
|
+
message: "Multipart method is not implemented yet",
|
|
280
|
+
options
|
|
281
|
+
});
|
|
282
|
+
return null;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
statics(endpoint: string, options: PlatformStaticsOptions): any {
|
|
286
|
+
this.app.getApp().register(fastifyStatics, {
|
|
287
|
+
root: options.root,
|
|
288
|
+
prefix: endpoint,
|
|
289
|
+
decorateReply: !this.staticsDecorated
|
|
290
|
+
});
|
|
291
|
+
this.staticsDecorated = true;
|
|
292
|
+
|
|
293
|
+
return null;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
protected compose(layer: PlatformLayer) {
|
|
297
|
+
return (req: FastifyRequest, reply: FastifyReply) => {
|
|
298
|
+
return runInContext(req.$ctx, async () => {
|
|
299
|
+
const $ctx = req.$ctx;
|
|
300
|
+
$ctx.next = null;
|
|
301
|
+
|
|
302
|
+
for (const metadata of layer.handlers) {
|
|
303
|
+
try {
|
|
304
|
+
if (!req.$ctx.isDone()) {
|
|
305
|
+
if (
|
|
306
|
+
($ctx.error && metadata.type === PlatformHandlerType.ERR_MIDDLEWARE) ||
|
|
307
|
+
(!$ctx.error && metadata.type !== PlatformHandlerType.ERR_MIDDLEWARE)
|
|
308
|
+
) {
|
|
309
|
+
await metadata.compiledHandler($ctx);
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
} catch (er) {
|
|
313
|
+
$ctx.error = er;
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
if (req.$ctx.error) {
|
|
318
|
+
// TODO maybe we can use platform exception here?
|
|
319
|
+
throw req.$ctx.error;
|
|
320
|
+
}
|
|
321
|
+
});
|
|
322
|
+
};
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
protected async resolvePlugins(): Promise<PlatformFastifyPluginLoadingOptions[]> {
|
|
326
|
+
let plugins = constant<PlatformFastifyPluginSettings[]>("plugins", []);
|
|
327
|
+
const env = constant<Env>("env");
|
|
328
|
+
|
|
329
|
+
const promises = plugins.map(async (plugin: PlatformFastifyPluginSettings): Promise<PlatformFastifyPluginLoadingOptions> => {
|
|
330
|
+
if (isFunction(plugin)) {
|
|
331
|
+
return {
|
|
332
|
+
env,
|
|
333
|
+
use: plugin
|
|
334
|
+
};
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
if (isString(plugin)) {
|
|
338
|
+
plugin = {env, use: plugin};
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
let {use} = plugin;
|
|
342
|
+
|
|
343
|
+
if (isString(use)) {
|
|
344
|
+
const mod = await import(use);
|
|
345
|
+
use = mod.default || mod;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
return {
|
|
349
|
+
env,
|
|
350
|
+
...plugin,
|
|
351
|
+
use
|
|
352
|
+
};
|
|
353
|
+
});
|
|
354
|
+
|
|
355
|
+
plugins = await Promise.all(promises);
|
|
356
|
+
|
|
357
|
+
return plugins.filter((middleware) => middleware.use).filter((middleware) => middleware.env === env);
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
adapter(PlatformFastify);
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file Automatically generated by @tsed/barrels.
|
|
3
|
+
*/
|
|
4
|
+
export * from "./components/PlatformFastify.js";
|
|
5
|
+
export * from "./interfaces/interfaces.js";
|
|
6
|
+
export * from "./interfaces/PlatformFastifySettings.js";
|
|
7
|
+
export * from "./services/PlatformFastifyRequest.js";
|
|
8
|
+
export * from "./services/PlatformFastifyResponse.js";
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type {Env} from "@tsed/core";
|
|
2
|
+
import type {FastifyPluginOptions, FastifyRegisterOptions} from "fastify";
|
|
3
|
+
|
|
4
|
+
import type {PlatformFastifySettings} from "./PlatformFastifySettings.js";
|
|
5
|
+
|
|
6
|
+
export * from "./PlatformFastifySettings.js";
|
|
7
|
+
|
|
8
|
+
export type PlatformFastifyPluginTypes = ((opts?: any) => any) | /*Type<any> |*/ string;
|
|
9
|
+
export type PlatformFastifyPluginLoadingOptions = {
|
|
10
|
+
env?: Env;
|
|
11
|
+
use: PlatformFastifyPluginTypes;
|
|
12
|
+
options?: FastifyRegisterOptions<FastifyPluginOptions>;
|
|
13
|
+
};
|
|
14
|
+
export type PlatformFastifyPluginSettings = PlatformFastifyPluginTypes | PlatformFastifyPluginLoadingOptions | any;
|
|
15
|
+
|
|
16
|
+
declare global {
|
|
17
|
+
namespace TsED {
|
|
18
|
+
export interface Configuration {
|
|
19
|
+
/**
|
|
20
|
+
* Configuration related to Fastify platform application.
|
|
21
|
+
*/
|
|
22
|
+
fastify: PlatformFastifySettings;
|
|
23
|
+
plugins: PlatformFastifyPluginSettings[];
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import {PlatformTest} from "@tsed/platform-http/testing";
|
|
2
|
+
|
|
3
|
+
import {PlatformFastifyRequest} from "./PlatformFastifyRequest.js";
|
|
4
|
+
|
|
5
|
+
function createRequest() {
|
|
6
|
+
const request = PlatformTest.createRequest();
|
|
7
|
+
const ctx = PlatformTest.createRequestContext({
|
|
8
|
+
event: {
|
|
9
|
+
request
|
|
10
|
+
},
|
|
11
|
+
RequestKlass: PlatformFastifyRequest
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
return {req: request, request: ctx.request};
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
describe("PlatformFastifyRequest", () => {
|
|
18
|
+
beforeEach(() => PlatformTest.create());
|
|
19
|
+
afterEach(() => PlatformTest.reset());
|
|
20
|
+
|
|
21
|
+
it("should create a PlatformRequest instance", () => {
|
|
22
|
+
const {req, request} = createRequest();
|
|
23
|
+
|
|
24
|
+
expect(request.raw).toEqual(req);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
describe("secure", () => {
|
|
28
|
+
it("should get secure info", () => {
|
|
29
|
+
const {req, request} = createRequest();
|
|
30
|
+
req.protocol = "https";
|
|
31
|
+
|
|
32
|
+
expect(request.secure).toEqual(true);
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
describe("host()", () => {
|
|
37
|
+
it("should return the host", () => {
|
|
38
|
+
const {req, request} = createRequest();
|
|
39
|
+
|
|
40
|
+
req.hostname = "host";
|
|
41
|
+
|
|
42
|
+
expect(request.host).toEqual("host");
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
describe("getReq()", () => {
|
|
47
|
+
it("should return nodejs request", () => {
|
|
48
|
+
const {req, request} = createRequest();
|
|
49
|
+
req.raw = {};
|
|
50
|
+
|
|
51
|
+
expect(request.getReq()).toEqual({});
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
});
|