@jayfong/x-server 1.8.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/CHANGELOG.md +154 -0
- package/README.md +51 -0
- package/lib/cli/api_generator.d.ts +44 -0
- package/lib/cli/api_generator.js +339 -0
- package/lib/cli/build_util.d.ts +9 -0
- package/lib/cli/build_util.js +141 -0
- package/lib/cli/cli.d.ts +2 -0
- package/lib/cli/cli.js +216 -0
- package/lib/cli/deploy_util.d.ts +10 -0
- package/lib/cli/deploy_util.js +51 -0
- package/lib/cli/env_util.d.ts +29 -0
- package/lib/cli/env_util.js +139 -0
- package/lib/cli/register.d.ts +0 -0
- package/lib/cli/register.js +5 -0
- package/lib/cli/template_util.d.ts +3 -0
- package/lib/cli/template_util.js +18 -0
- package/lib/cli/templates/handlers.ts +3 -0
- package/lib/cli/templates/hooks.ts +2 -0
- package/lib/cli/templates/models.ts +22 -0
- package/lib/cli/templates/routes.ts +26 -0
- package/lib/cli/templates/tasks.ts +2 -0
- package/lib/core/define_bus.d.ts +38 -0
- package/lib/core/define_bus.js +15 -0
- package/lib/core/define_handler.d.ts +16 -0
- package/lib/core/define_handler.js +41 -0
- package/lib/core/define_hook.d.ts +2 -0
- package/lib/core/define_hook.js +8 -0
- package/lib/core/define_server.d.ts +3 -0
- package/lib/core/define_server.js +10 -0
- package/lib/core/define_task.d.ts +2 -0
- package/lib/core/define_task.js +27 -0
- package/lib/core/handler.d.ts +10 -0
- package/lib/core/handler.js +76 -0
- package/lib/core/http_error.d.ts +2 -0
- package/lib/core/http_error.js +8 -0
- package/lib/core/http_method.d.ts +3 -0
- package/lib/core/http_method.js +10 -0
- package/lib/core/server.d.ts +16 -0
- package/lib/core/server.js +137 -0
- package/lib/core/types.d.ts +123 -0
- package/lib/core/types.js +2 -0
- package/lib/index.d.ts +22 -0
- package/lib/index.js +41 -0
- package/lib/plugins/base.d.ts +4 -0
- package/lib/plugins/base.js +2 -0
- package/lib/plugins/cors.d.ts +21 -0
- package/lib/plugins/cors.js +39 -0
- package/lib/plugins/file_parser.d.ts +13 -0
- package/lib/plugins/file_parser.js +19 -0
- package/lib/plugins/ws_parser.d.ts +13 -0
- package/lib/plugins/ws_parser.js +19 -0
- package/lib/plugins/xml_parser.d.ts +19 -0
- package/lib/plugins/xml_parser.js +48 -0
- package/lib/services/base.d.ts +4 -0
- package/lib/services/base.js +2 -0
- package/lib/services/cache.d.ts +119 -0
- package/lib/services/cache.js +196 -0
- package/lib/services/captcha.d.ts +33 -0
- package/lib/services/captcha.js +34 -0
- package/lib/services/dispose.d.ts +17 -0
- package/lib/services/dispose.js +24 -0
- package/lib/services/jwt.d.ts +21 -0
- package/lib/services/jwt.js +50 -0
- package/lib/services/redis.d.ts +10 -0
- package/lib/services/redis.js +14 -0
- package/lib/x.d.ts +5 -0
- package/lib/x.js +11 -0
- package/package.json +86 -0
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.DisposeService = void 0;
|
|
7
|
+
const exit_hook_1 = __importDefault(require("exit-hook"));
|
|
8
|
+
class DisposeService {
|
|
9
|
+
constructor(options) {
|
|
10
|
+
this.options = options;
|
|
11
|
+
this.serviceName = 'dispose';
|
|
12
|
+
this.disposes = [];
|
|
13
|
+
if (this.options?.disposeOnExit) {
|
|
14
|
+
(0, exit_hook_1.default)(() => this.dispose());
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
add(fn) {
|
|
18
|
+
this.disposes.push(fn);
|
|
19
|
+
}
|
|
20
|
+
dispose() {
|
|
21
|
+
return Promise.all(this.disposes.map(dispose => dispose()));
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
exports.DisposeService = DisposeService;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { BaseService } from './base';
|
|
3
|
+
import { MsValue } from 'vtils/date';
|
|
4
|
+
import type { IncomingHttpHeaders } from 'http';
|
|
5
|
+
export interface JwtServiceOptions {
|
|
6
|
+
secret: string;
|
|
7
|
+
ttl: MsValue;
|
|
8
|
+
}
|
|
9
|
+
export declare class JwtService implements BaseService {
|
|
10
|
+
private options;
|
|
11
|
+
serviceName: string;
|
|
12
|
+
constructor(options: JwtServiceOptions);
|
|
13
|
+
sign<T extends Record<string, any>>(payload: T): string;
|
|
14
|
+
verify<T extends Record<string, any>>(token: string): Promise<T>;
|
|
15
|
+
verifyFromHttpHeaders<T extends Record<string, any>>(headers: IncomingHttpHeaders): Promise<T>;
|
|
16
|
+
}
|
|
17
|
+
declare module '../x' {
|
|
18
|
+
interface X {
|
|
19
|
+
jwt: JwtService;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.JwtService = void 0;
|
|
7
|
+
const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
|
|
8
|
+
const http_error_1 = require("../core/http_error");
|
|
9
|
+
const date_1 = require("vtils/date");
|
|
10
|
+
class JwtService {
|
|
11
|
+
constructor(options) {
|
|
12
|
+
this.options = options;
|
|
13
|
+
this.serviceName = 'jwt';
|
|
14
|
+
}
|
|
15
|
+
sign(payload) {
|
|
16
|
+
return jsonwebtoken_1.default.sign(payload, this.options.secret, {
|
|
17
|
+
expiresIn: (0, date_1.ms)(this.options.ttl, true),
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
async verify(token) {
|
|
21
|
+
return new Promise((resolve, reject) => {
|
|
22
|
+
jsonwebtoken_1.default.verify(token, this.options.secret, {
|
|
23
|
+
ignoreExpiration: false,
|
|
24
|
+
}, (err, data) => {
|
|
25
|
+
if (err) {
|
|
26
|
+
console.log(err);
|
|
27
|
+
reject(new http_error_1.HttpError.Unauthorized());
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
resolve(data);
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
async verifyFromHttpHeaders(headers) {
|
|
36
|
+
const authorization = headers.authorization ||
|
|
37
|
+
(headers['sec-websocket-protocol'] &&
|
|
38
|
+
`Bearer ${headers['sec-websocket-protocol']}`) ||
|
|
39
|
+
'';
|
|
40
|
+
if (!authorization) {
|
|
41
|
+
throw new http_error_1.HttpError.Unauthorized();
|
|
42
|
+
}
|
|
43
|
+
const [scheme, token] = authorization.split(' ');
|
|
44
|
+
if (scheme.toLowerCase() !== 'bearer') {
|
|
45
|
+
throw new http_error_1.HttpError.Unauthorized();
|
|
46
|
+
}
|
|
47
|
+
return this.verify(token);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
exports.JwtService = JwtService;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.RedisService = void 0;
|
|
7
|
+
const ioredis_1 = __importDefault(require("ioredis"));
|
|
8
|
+
class RedisService extends ioredis_1.default {
|
|
9
|
+
constructor() {
|
|
10
|
+
super(...arguments);
|
|
11
|
+
this.serviceName = 'redis';
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
exports.RedisService = RedisService;
|
package/lib/x.d.ts
ADDED
package/lib/x.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.x = void 0;
|
|
4
|
+
exports.x = {
|
|
5
|
+
register: (...services) => {
|
|
6
|
+
for (const service of services) {
|
|
7
|
+
// @ts-ignore
|
|
8
|
+
exports.x[service.serviceName] = service;
|
|
9
|
+
}
|
|
10
|
+
},
|
|
11
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@jayfong/x-server",
|
|
3
|
+
"version": "1.8.0",
|
|
4
|
+
"license": "ISC",
|
|
5
|
+
"main": "lib/index.js",
|
|
6
|
+
"types": "lib/index.d.ts",
|
|
7
|
+
"bin": {
|
|
8
|
+
"xs": "lib/cli/cli.js"
|
|
9
|
+
},
|
|
10
|
+
"files": [
|
|
11
|
+
"lib"
|
|
12
|
+
],
|
|
13
|
+
"scripts": {
|
|
14
|
+
"bootstrap": "tyn --frozen-lockfile",
|
|
15
|
+
"build": "rm -rf lib && tsc -p ./tsconfig.build.json && cp -r src/cli/templates lib/cli/templates",
|
|
16
|
+
"dev": "rm -rf lib && tsc -w -p ./tsconfig.build.json",
|
|
17
|
+
"release": "tyn test && standard-version -a && tyn build && npm publish && git push --follow-tags origin master",
|
|
18
|
+
"test": "tsc --noEmit && jest",
|
|
19
|
+
"updeps": "tnpx npm-check-updates --target minor --upgrade"
|
|
20
|
+
},
|
|
21
|
+
"husky": {
|
|
22
|
+
"hooks": {
|
|
23
|
+
"post-merge": "tyn bootstrap"
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
"dependencies": {
|
|
27
|
+
"@prisma/client": "^3.12.0",
|
|
28
|
+
"@types/bull": "^3.15.8",
|
|
29
|
+
"@types/busboy": "^0.3.2",
|
|
30
|
+
"@types/http-errors": "^1.8.2",
|
|
31
|
+
"@types/jsonwebtoken": "^8.5.8",
|
|
32
|
+
"@types/ws": "^8.5.3",
|
|
33
|
+
"bufferutil": "^4.0.6",
|
|
34
|
+
"bull": "^4.8.1",
|
|
35
|
+
"chokidar": "^3.5.3",
|
|
36
|
+
"comment-parser": "^1.3.1",
|
|
37
|
+
"compressing": "^1.5.1",
|
|
38
|
+
"cuid": "^2.1.8",
|
|
39
|
+
"debug": "^4.3.4",
|
|
40
|
+
"esbuild": "^0.14.36",
|
|
41
|
+
"esbuild-runner": "^2.2.1",
|
|
42
|
+
"execa": "^5.1.1",
|
|
43
|
+
"exit-hook": "^2.2.1",
|
|
44
|
+
"fast-xml-parser": "^3.21.1",
|
|
45
|
+
"fastify": "^3.28.0",
|
|
46
|
+
"fastify-cors": "^6.0.3",
|
|
47
|
+
"fastify-multipart": "^5.3.1",
|
|
48
|
+
"fastify-websocket": "^4.2.2",
|
|
49
|
+
"fs-extra": "^10.0.1",
|
|
50
|
+
"http-errors": "^1.8.1",
|
|
51
|
+
"ioredis": "^4.28.5",
|
|
52
|
+
"jsonwebtoken": "^8.5.1",
|
|
53
|
+
"node-ssh": "^12.0.4",
|
|
54
|
+
"pino-pretty": "^7.6.1",
|
|
55
|
+
"prisma": "^3.12.0",
|
|
56
|
+
"select-run": "^1.1.2",
|
|
57
|
+
"supports-color": "^8",
|
|
58
|
+
"svg-captcha": "^1.4.0",
|
|
59
|
+
"ts-morph": "^12.2.0",
|
|
60
|
+
"tsconfig-paths": "^3.14.1",
|
|
61
|
+
"utf-8-validate": "^5.0.9",
|
|
62
|
+
"vscode-generate-index-standalone": "^1.6.0",
|
|
63
|
+
"vtils": "^4.60.0",
|
|
64
|
+
"yargs": "^17.4.1"
|
|
65
|
+
},
|
|
66
|
+
"devDependencies": {
|
|
67
|
+
"@types/debug": "^4.1.7",
|
|
68
|
+
"@types/fs-extra": "^9.0.13",
|
|
69
|
+
"@types/ioredis": "^4.28.10",
|
|
70
|
+
"@types/json-schema": "^7.0.11",
|
|
71
|
+
"eslint": "^7.32.0",
|
|
72
|
+
"haoma": "^3.6.4",
|
|
73
|
+
"husky": "^4.3.8",
|
|
74
|
+
"ioredis-mock": "^5.9.1",
|
|
75
|
+
"jest": "^27.5.1",
|
|
76
|
+
"lint-staged": "^10.5.4",
|
|
77
|
+
"npm-check-updates": "^12.5.9",
|
|
78
|
+
"prettier": "^2.6.2",
|
|
79
|
+
"standard-version": "^9.3.2",
|
|
80
|
+
"typescript": "^4.6.3",
|
|
81
|
+
"typescript-snapshots-plugin": "^1.7.0"
|
|
82
|
+
},
|
|
83
|
+
"publishConfig": {
|
|
84
|
+
"access": "public"
|
|
85
|
+
}
|
|
86
|
+
}
|