@vrobots/fastify 0.1.0
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/dist/index.d.ts +4 -0
- package/dist/index.js +53 -0
- package/dist/src/conf/compiler/types.d.ts +20 -0
- package/dist/src/conf/compiler/types.js +2 -0
- package/dist/src/conf/conf.d.ts +3 -0
- package/dist/src/conf/conf.js +27 -0
- package/dist/src/lib/fastify.d.ts +16 -0
- package/dist/src/lib/fastify.js +101 -0
- package/dist/src/lib/log.d.ts +3 -0
- package/dist/src/lib/log.js +20 -0
- package/package.json +48 -0
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.conf = exports.log = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const conf_1 = tslib_1.__importDefault(require("./src/conf/conf"));
|
|
6
|
+
const fastify_1 = tslib_1.__importDefault(require("./src/lib/fastify"));
|
|
7
|
+
const chalk_1 = tslib_1.__importDefault(require("chalk"));
|
|
8
|
+
const https_1 = tslib_1.__importDefault(require("https"));
|
|
9
|
+
const fastify_socket_1 = tslib_1.__importDefault(require("fastify-socket"));
|
|
10
|
+
var log_1 = require("./src/lib/log");
|
|
11
|
+
Object.defineProperty(exports, "log", { enumerable: true, get: function () { return tslib_1.__importDefault(log_1).default; } });
|
|
12
|
+
var conf_2 = require("./src/conf/conf");
|
|
13
|
+
Object.defineProperty(exports, "conf", { enumerable: true, get: function () { return tslib_1.__importDefault(conf_2).default; } });
|
|
14
|
+
const fastify = new fastify_1.default();
|
|
15
|
+
class Fastify {
|
|
16
|
+
constructor() {
|
|
17
|
+
this.init = async () => {
|
|
18
|
+
try {
|
|
19
|
+
console.log('---------------------------------------------------------');
|
|
20
|
+
console.log(chalk_1.default.yellow('Initializing @vrobot/fastify...'));
|
|
21
|
+
const appInitialization = await fastify.init();
|
|
22
|
+
if (!appInitialization)
|
|
23
|
+
throw Error('Application failed to initialize');
|
|
24
|
+
const { app, credentials } = appInitialization;
|
|
25
|
+
const { protocol, hostname, port, title } = conf_1.default.app;
|
|
26
|
+
const SERVER_URI = `${protocol}://${hostname}:${port}`;
|
|
27
|
+
app.register(fastify_socket_1.default);
|
|
28
|
+
console.log('---------------------------------------------------------');
|
|
29
|
+
console.log(chalk_1.default.green('Allowed Origins'));
|
|
30
|
+
conf_1.default.secure.allowOrigin.forEach(origin => {
|
|
31
|
+
console.log(chalk_1.default.green(origin));
|
|
32
|
+
});
|
|
33
|
+
console.log('---------------------------------------------------------');
|
|
34
|
+
console.log(chalk_1.default.green(title));
|
|
35
|
+
console.log(chalk_1.default.green(`Environment: ${process.env.NODE_ENV}`));
|
|
36
|
+
console.log(chalk_1.default.green(`Server: ${SERVER_URI}`));
|
|
37
|
+
console.log('---------------------------------------------------------');
|
|
38
|
+
if (protocol === 'https') {
|
|
39
|
+
const server = await https_1.default.createServer(credentials, app.callback());
|
|
40
|
+
await server.listen({ port, host: hostname });
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
await app.listen({ port, host: hostname });
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
catch (error) {
|
|
47
|
+
console.log(chalk_1.default.red('FAILED TO COMPILE', error));
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
const test = new Fastify();
|
|
53
|
+
exports.default = test.init();
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export interface Conf {
|
|
2
|
+
app: {
|
|
3
|
+
title: string;
|
|
4
|
+
description: string;
|
|
5
|
+
port: number;
|
|
6
|
+
hostname: string;
|
|
7
|
+
protocol: string;
|
|
8
|
+
version: string;
|
|
9
|
+
debugMode: boolean;
|
|
10
|
+
};
|
|
11
|
+
secure: {
|
|
12
|
+
ssl: boolean;
|
|
13
|
+
privateKey: string;
|
|
14
|
+
certificate: string;
|
|
15
|
+
ca: string;
|
|
16
|
+
allowOrigin: string[];
|
|
17
|
+
};
|
|
18
|
+
livereload: boolean;
|
|
19
|
+
assets: (string | undefined)[];
|
|
20
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const tslib_1 = require("tslib");
|
|
4
|
+
const customENV = tslib_1.__importStar(require("custom-env"));
|
|
5
|
+
customENV.env(process.env.NODE_ENV, 'secret');
|
|
6
|
+
const { APP_TITLE = '', APP_DESCRIPTION = '', APP_HOSTNAME = '127.0.0.1', APP_PORT = '8000', APP_PROTOCOL = 'http', APP_VERSION = '', APP_DEBUG_MODE = 'true', SECURE_ALLOW_ORIGIN = '', SECURE_SSL = '', SECURE_PRIVATE_KEY = '', SECURE_CERTIFICATE = '', SECURE_CA_CERTIFICATE = '', LIVE_RELOAD = 'true', ASSETS = '', } = process.env;
|
|
7
|
+
const conf = {
|
|
8
|
+
app: {
|
|
9
|
+
title: APP_TITLE,
|
|
10
|
+
description: APP_DESCRIPTION,
|
|
11
|
+
port: Number(APP_PORT),
|
|
12
|
+
hostname: APP_HOSTNAME,
|
|
13
|
+
protocol: APP_PROTOCOL,
|
|
14
|
+
version: APP_VERSION,
|
|
15
|
+
debugMode: APP_DEBUG_MODE === 'true',
|
|
16
|
+
},
|
|
17
|
+
secure: {
|
|
18
|
+
ssl: SECURE_SSL === 'true',
|
|
19
|
+
privateKey: SECURE_PRIVATE_KEY,
|
|
20
|
+
certificate: SECURE_CERTIFICATE,
|
|
21
|
+
ca: SECURE_CA_CERTIFICATE,
|
|
22
|
+
allowOrigin: SECURE_ALLOW_ORIGIN.split(',') || [SECURE_ALLOW_ORIGIN || ''],
|
|
23
|
+
},
|
|
24
|
+
livereload: LIVE_RELOAD === 'true',
|
|
25
|
+
assets: ASSETS.split(',').map((asset) => process.env[`ASSETS_${asset}`]) || []
|
|
26
|
+
};
|
|
27
|
+
exports.default = conf;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { FastifyInstance } from 'fastify';
|
|
2
|
+
interface Credentials {
|
|
3
|
+
key?: Buffer;
|
|
4
|
+
cert?: Buffer;
|
|
5
|
+
ca?: Buffer;
|
|
6
|
+
}
|
|
7
|
+
export default class FastifyInit {
|
|
8
|
+
init(): {
|
|
9
|
+
app: FastifyInstance;
|
|
10
|
+
credentials: Credentials;
|
|
11
|
+
} | false;
|
|
12
|
+
private initSSLValidation;
|
|
13
|
+
private initMiddleware;
|
|
14
|
+
private initModulesConfiguration;
|
|
15
|
+
}
|
|
16
|
+
export {};
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const tslib_1 = require("tslib");
|
|
4
|
+
const conf_1 = tslib_1.__importDefault(require("../conf/conf"));
|
|
5
|
+
const fastify_1 = tslib_1.__importDefault(require("fastify"));
|
|
6
|
+
const path_1 = tslib_1.__importDefault(require("path"));
|
|
7
|
+
const fs_1 = tslib_1.__importDefault(require("fs"));
|
|
8
|
+
const chalk_1 = tslib_1.__importDefault(require("chalk"));
|
|
9
|
+
const glob_1 = tslib_1.__importDefault(require("glob"));
|
|
10
|
+
const formbody_1 = tslib_1.__importDefault(require("@fastify/formbody"));
|
|
11
|
+
const log_1 = tslib_1.__importDefault(require("./log"));
|
|
12
|
+
const strip_ansi_1 = tslib_1.__importDefault(require("strip-ansi"));
|
|
13
|
+
const cors_1 = tslib_1.__importDefault(require("@fastify/cors"));
|
|
14
|
+
class FastifyInit {
|
|
15
|
+
init() {
|
|
16
|
+
const app = (0, fastify_1.default)({
|
|
17
|
+
logger: true,
|
|
18
|
+
});
|
|
19
|
+
const credentials = this.initSSLValidation();
|
|
20
|
+
if (!credentials)
|
|
21
|
+
return false;
|
|
22
|
+
this.initMiddleware(app);
|
|
23
|
+
this.initModulesConfiguration(app);
|
|
24
|
+
return {
|
|
25
|
+
app,
|
|
26
|
+
credentials,
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
initSSLValidation() {
|
|
30
|
+
console.log('---------------------------------------------------------');
|
|
31
|
+
console.log(chalk_1.default.yellow('Checking for SSL...'));
|
|
32
|
+
console.log(chalk_1.default.green(conf_1.default.secure.ca));
|
|
33
|
+
console.log(chalk_1.default.green(conf_1.default.secure.certificate));
|
|
34
|
+
console.log(chalk_1.default.green(conf_1.default.secure.privateKey));
|
|
35
|
+
const privateKeyExists = fs_1.default.existsSync(path_1.default.resolve(conf_1.default.secure.privateKey));
|
|
36
|
+
const certificateExists = fs_1.default.existsSync(path_1.default.resolve(conf_1.default.secure.certificate));
|
|
37
|
+
const { protocol } = conf_1.default.app;
|
|
38
|
+
if (!privateKeyExists || !certificateExists && protocol === 'https') {
|
|
39
|
+
console.log(chalk_1.default.red('+ Error: Certificate file or key file is missing, @32one/koa requires certificates to run'));
|
|
40
|
+
console.log(chalk_1.default.red(`+ MISSING: ${conf_1.default.secure.privateKey} AND/OR ${conf_1.default.secure.certificate}`));
|
|
41
|
+
conf_1.default.secure.ssl = false;
|
|
42
|
+
return {
|
|
43
|
+
key: void 0,
|
|
44
|
+
cert: void 0,
|
|
45
|
+
ca: void 0,
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
else if (protocol === 'https') {
|
|
49
|
+
const key = fs_1.default.readFileSync(path_1.default.resolve(conf_1.default.secure.privateKey));
|
|
50
|
+
const cert = fs_1.default.readFileSync(path_1.default.resolve(conf_1.default.secure.certificate));
|
|
51
|
+
const ca = fs_1.default.readFileSync(path_1.default.resolve(conf_1.default.secure.ca));
|
|
52
|
+
console.log(chalk_1.default.green('+ ADDED - SSL SUPPORT'));
|
|
53
|
+
return {
|
|
54
|
+
key,
|
|
55
|
+
cert,
|
|
56
|
+
ca,
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
return {
|
|
60
|
+
key: void 0,
|
|
61
|
+
cert: void 0,
|
|
62
|
+
ca: void 0,
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
initMiddleware(app) {
|
|
66
|
+
app.register(formbody_1.default);
|
|
67
|
+
app.setErrorHandler((err) => {
|
|
68
|
+
log_1.default.defaultMeta = { ...log_1.default.defaultMeta, error: err.toString() },
|
|
69
|
+
log_1.default.error((0, strip_ansi_1.default)(err));
|
|
70
|
+
});
|
|
71
|
+
function checkOriginAgainstWhitelist(ctx) {
|
|
72
|
+
const requestOrigin = ctx.accept.headers.origin;
|
|
73
|
+
const requestOriginParts = requestOrigin.split('.');
|
|
74
|
+
const wildcardOriginExists = !!conf_1.default.secure.allowOrigin.find(origin => origin.includes('*'));
|
|
75
|
+
const directOriginMatch = conf_1.default.secure.allowOrigin.includes(requestOrigin);
|
|
76
|
+
const wildcardMatchOrigin = conf_1.default.secure.allowOrigin.includes(`*.${requestOriginParts.slice(-2).join('.')}`) || conf_1.default.secure.allowOrigin.includes(`*.${requestOrigin.replace('https://', '').replace('http://', '')}`);
|
|
77
|
+
if ((wildcardOriginExists && !wildcardMatchOrigin) || (!wildcardOriginExists && !directOriginMatch)) {
|
|
78
|
+
return ctx.throw(`${requestOrigin} is not a valid origin`);
|
|
79
|
+
}
|
|
80
|
+
return requestOrigin;
|
|
81
|
+
}
|
|
82
|
+
app.register(cors_1.default, { origin: checkOriginAgainstWhitelist, exposedHeaders: ['Access-Control-Allow-Origin'], credentials: true });
|
|
83
|
+
}
|
|
84
|
+
initModulesConfiguration(app) {
|
|
85
|
+
const assets = conf_1.default.assets.map((asset) => glob_1.default?.sync(asset));
|
|
86
|
+
console.log('---------------------------------------------------------');
|
|
87
|
+
console.log(chalk_1.default.green(`Adding Assets...`));
|
|
88
|
+
console.log(assets);
|
|
89
|
+
assets?.forEach(asset => {
|
|
90
|
+
asset?.forEach((assetPath) => {
|
|
91
|
+
console.log(chalk_1.default.green(`+ ADDED - Asset: ${assetPath}`));
|
|
92
|
+
Promise.resolve(`${path_1.default.join(process.cwd(), assetPath)}`).then(s => tslib_1.__importStar(require(s))).then(module => {
|
|
93
|
+
if (module.default) {
|
|
94
|
+
module.default(app);
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
exports.default = FastifyInit;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const tslib_1 = require("tslib");
|
|
4
|
+
const conf_1 = tslib_1.__importDefault(require("../conf/conf"));
|
|
5
|
+
const winston_1 = tslib_1.__importDefault(require("winston"));
|
|
6
|
+
const log = winston_1.default.createLogger({
|
|
7
|
+
level: 'info',
|
|
8
|
+
format: winston_1.default.format.json(),
|
|
9
|
+
defaultMeta: { service: conf_1.default.app.title },
|
|
10
|
+
transports: [
|
|
11
|
+
new winston_1.default.transports.File({ filename: 'error.log', level: 'error' }),
|
|
12
|
+
new winston_1.default.transports.File({ filename: 'combined.log' }),
|
|
13
|
+
],
|
|
14
|
+
});
|
|
15
|
+
if (conf_1.default.app.debugMode) {
|
|
16
|
+
log.add(new winston_1.default.transports.Console({
|
|
17
|
+
format: winston_1.default.format.simple(),
|
|
18
|
+
}));
|
|
19
|
+
}
|
|
20
|
+
exports.default = log;
|
package/package.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@vrobots/fastify",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "",
|
|
5
|
+
"license": "ISC",
|
|
6
|
+
"author": "Bryan Ricci",
|
|
7
|
+
"type": "commonjs",
|
|
8
|
+
"main": "dist/index.js",
|
|
9
|
+
"types": "dist/index.d.ts",
|
|
10
|
+
"files": [
|
|
11
|
+
"dist"
|
|
12
|
+
],
|
|
13
|
+
"publishConfig": {
|
|
14
|
+
"access": "public"
|
|
15
|
+
},
|
|
16
|
+
"scripts": {
|
|
17
|
+
"test": "echo \"Error: no test specified\" && exit 1",
|
|
18
|
+
"build": "tsc -declaration",
|
|
19
|
+
"start": "nodemon --config \"./nodemon.json\"/",
|
|
20
|
+
"start-local": "NODE_ENV=local npm start",
|
|
21
|
+
"start-no-inspect": "nodemon dist/server/app.js --exec babel-node",
|
|
22
|
+
"pre-publish": "rimraf dist; npm run build"
|
|
23
|
+
},
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"@types/node": "^25.3.2",
|
|
26
|
+
"@typescript-eslint/eslint-plugin": "^8.56.1",
|
|
27
|
+
"@typescript-eslint/parser": "^8.56.1",
|
|
28
|
+
"chai": "^6.2.2",
|
|
29
|
+
"eslint": "^10.0.2",
|
|
30
|
+
"mocha": "^11.3.0",
|
|
31
|
+
"nodemon": "^3.1.14",
|
|
32
|
+
"rimraf": "^6.1.3",
|
|
33
|
+
"ts-node": "^10.9.2",
|
|
34
|
+
"typescript": "^5.9.3"
|
|
35
|
+
},
|
|
36
|
+
"dependencies": {
|
|
37
|
+
"@fastify/cors": "^11.2.0",
|
|
38
|
+
"@fastify/formbody": "^8.0.2",
|
|
39
|
+
"chalk": "^5.6.2",
|
|
40
|
+
"custom-env": "^2.0.6",
|
|
41
|
+
"fastify-formidable": "^3.0.2",
|
|
42
|
+
"fastify-socket": "^5.1.4",
|
|
43
|
+
"glob": "^13.0.6",
|
|
44
|
+
"socket.io": "^4.8.3",
|
|
45
|
+
"strip-ansi": "^7.2.0",
|
|
46
|
+
"winston": "^3.19.0"
|
|
47
|
+
}
|
|
48
|
+
}
|