typespeed 2.5.0 → 2.5.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.
Files changed (94) hide show
  1. package/CHANGELOG.md +1 -0
  2. package/dist/default/rabbitmq.class.js +18 -1
  3. package/dist/default/redis.class.js +14 -1
  4. package/dist/default/socket-io.class.js +53 -4
  5. package/dist/typespeed.d.ts +7 -7
  6. package/package.json +6 -2
  7. package/.github/workflows/test.yml +0 -50
  8. package/app/nodemon.json +0 -5
  9. package/app/package.json +0 -17
  10. package/app/src/aop-test.class.ts +0 -29
  11. package/app/src/config-development.json +0 -26
  12. package/app/src/config-production.json +0 -15
  13. package/app/src/config.json +0 -50
  14. package/app/src/custom-log.class.ts +0 -33
  15. package/app/src/entities/mutil-users.class.ts +0 -5
  16. package/app/src/entities/user-dto.class.ts +0 -3
  17. package/app/src/first-page.class.ts +0 -46
  18. package/app/src/jwt-authentication.class.ts +0 -30
  19. package/app/src/main.ts +0 -24
  20. package/app/src/second-page.class.ts +0 -43
  21. package/app/src/test-bind.class.ts +0 -41
  22. package/app/src/test-database.class.ts +0 -81
  23. package/app/src/test-log.class.ts +0 -11
  24. package/app/src/test-mq.class.ts +0 -21
  25. package/app/src/test-orm.class.ts +0 -63
  26. package/app/src/test-redis.class.ts +0 -45
  27. package/app/src/test-request.class.ts +0 -37
  28. package/app/src/test-socket.class.ts +0 -56
  29. package/app/src/user-model.class.ts +0 -50
  30. package/app/src/views/index.html +0 -1
  31. package/app/src/views/socket.html +0 -23
  32. package/app/src/views/upload.html +0 -14
  33. package/app/static/favicon.ico +0 -0
  34. package/app/static/k.jpg +0 -0
  35. package/app/tsconfig.json +0 -20
  36. package/introduction/decorator/example-import.class.ts +0 -6
  37. package/introduction/decorator/main-app.ts +0 -6
  38. package/introduction/decorator/main-decorator.ts +0 -108
  39. package/introduction/decorator/main-import.ts +0 -1
  40. package/introduction/decorator/main-reflect.ts +0 -25
  41. package/introduction/decorator-next/dist/main.js +0 -116
  42. package/introduction/decorator-next/package.json +0 -14
  43. package/introduction/decorator-next/src/main.ts +0 -64
  44. package/introduction/decorator-next/tsconfig.json +0 -17
  45. package/introduction/decorator-next//346/225/231/347/250/213.md +0 -384
  46. package/introduction/tsconfig.json +0 -23
  47. package/introduction/web-services/db-init.ts +0 -39
  48. package/introduction/web-services/db-singleton.ts +0 -59
  49. package/introduction/web-services/http-if.ts +0 -10
  50. package/introduction/web-services/http-init.ts +0 -6
  51. package/introduction/web-services/http-router.ts +0 -28
  52. package/src/bind.decorator.ts +0 -50
  53. package/src/core.decorator.ts +0 -184
  54. package/src/database.decorator.ts +0 -446
  55. package/src/decorator-utils.ts +0 -53
  56. package/src/default/default-authentication.class.ts +0 -10
  57. package/src/default/express-server.class.ts +0 -150
  58. package/src/default/log-default.class.ts +0 -9
  59. package/src/default/node-cache.class.ts +0 -40
  60. package/src/default/pages/404.html +0 -226
  61. package/src/default/pages/500.html +0 -713
  62. package/src/default/rabbitmq.class.ts +0 -61
  63. package/src/default/read-write-db.class.ts +0 -53
  64. package/src/default/redis.class.ts +0 -79
  65. package/src/default/socket-io.class.ts +0 -81
  66. package/src/factory/authentication-factory.class.ts +0 -9
  67. package/src/factory/cache-factory.class.ts +0 -7
  68. package/src/factory/data-source-factory.class.ts +0 -4
  69. package/src/factory/log-factory.class.ts +0 -8
  70. package/src/factory/server-factory.class.ts +0 -6
  71. package/src/route.decorator.ts +0 -334
  72. package/src/scaffold/command.ts +0 -40
  73. package/src/scaffold/templates/front-page.class.ts.tpl +0 -12
  74. package/src/scaffold/templates/main.ts.tpl +0 -13
  75. package/src/scaffold/templates/nodemon.json.tpl +0 -7
  76. package/src/scaffold/templates/package.json.tpl +0 -16
  77. package/src/scaffold/templates/tsconfig.json.tpl +0 -17
  78. package/src/typespeed.d.ts +0 -403
  79. package/src/typespeed.ts +0 -139
  80. package/test/bind.test.ts +0 -28
  81. package/test/database.test.ts +0 -76
  82. package/test/decorator-utils.test.ts +0 -42
  83. package/test/first-page.test.ts +0 -42
  84. package/test/hooks.ts +0 -13
  85. package/test/mq.test.ts +0 -17
  86. package/test/orm.test.ts +0 -66
  87. package/test/redis.test.ts +0 -48
  88. package/test/request.test.ts +0 -51
  89. package/test/second-page.test.ts +0 -50
  90. package/test/socket.test.ts +0 -60
  91. package/test-env/docker-compose.test.yml +0 -31
  92. package/test-env/mysql-init/init.sql +0 -8
  93. package/test-env//346/234/254/345/234/260Docker/346/265/213/350/257/225/346/226/271/346/241/210.md +0 -69
  94. package/tsconfig.json +0 -25
@@ -1,59 +0,0 @@
1
- import { createServer, Server, IncomingMessage, ServerResponse } from "http";
2
- import { createConnection, Connection } from "mysql2";
3
-
4
- interface Page {
5
- page(response: ServerResponse): void;
6
- }
7
-
8
- class First implements Page {
9
- page(response: ServerResponse): void {
10
- response.end("I am first page.");
11
- }
12
- }
13
-
14
- class User implements Page {
15
- page(response: ServerResponse): void {
16
- const database: Database = Database.getInstance();
17
- const databaseCopy: Database = Database.getInstance();
18
- console.log("两个对象是否一致:", database === databaseCopy)
19
- database.query('SELECT * FROM `user`', (err, results) => {
20
- response.end(JSON.stringify(results));
21
- });
22
- }
23
- }
24
-
25
- class Root implements Page {
26
- page(response: ServerResponse): void {
27
- response.end("I am main page.");
28
- }
29
- }
30
-
31
- class Database {
32
- private static instance: Database;
33
- private connection: Connection;
34
- private constructor(connection: Connection) {
35
- this.connection = connection;
36
- }
37
- static getInstance() {
38
- if (!Database.instance) {
39
- Database.instance = new Database(createConnection({ host: 'localhost', user: 'root', "password": "qwer1234", database: 'test' }));
40
- }
41
- return Database.instance;
42
- }
43
- query(sql: string, callback: (err: any, results: any) => void): void {
44
- this.connection.query('SELECT * FROM `user`', callback);
45
- }
46
- }
47
-
48
-
49
- const router = new Map<string, Page>();
50
- router.set("/first", new First());
51
- router.set("/main", new Root());
52
- router.set("/user", new User());
53
- const server = createServer((request: IncomingMessage, response: ServerResponse) => {
54
- let page = router.get(request.url === undefined ? "" : request.url);
55
- if (page === undefined) {
56
- page = new Root();
57
- }
58
- page.page(response);
59
- }).listen(3000);
@@ -1,10 +0,0 @@
1
- import { createServer, Server, IncomingMessage, ServerResponse } from "http";
2
-
3
- // 使用 IF 语句来判断请求的 URL
4
- const server = createServer((request: IncomingMessage, response: ServerResponse) => {
5
- if (request.url === "/first") {
6
- response.end("I am first page.");
7
- } else {
8
- response.end("I am main page.");
9
- }
10
- }).listen(3000);
@@ -1,6 +0,0 @@
1
- import {createServer, Server, IncomingMessage, ServerResponse} from "http";
2
-
3
- // 简单的 HTTP 服务
4
- const server = createServer((request: IncomingMessage, response: ServerResponse) => {
5
- response.end("Hello World");
6
- }).listen(3000);
@@ -1,28 +0,0 @@
1
- import { createServer, Server, IncomingMessage, ServerResponse } from "http";
2
-
3
- interface Page {
4
- page(response: ServerResponse): void;
5
- }
6
-
7
- class First implements Page {
8
- page(response: ServerResponse): void {
9
- response.end("I am first page.");
10
- }
11
- }
12
-
13
- class Root implements Page {
14
- page(response: ServerResponse): void {
15
- response.end("I am main page.");
16
- }
17
- }
18
-
19
- const router = new Map<string, Page>();
20
- router.set("/first", new First());
21
- router.set("/main", new Root());
22
- const server = createServer((request: IncomingMessage, response: ServerResponse) => {
23
- let page = router.get(request.url === undefined ? "" : request.url);
24
- if (page === undefined) {
25
- page = new Root();
26
- }
27
- page.page(response);
28
- }).listen(3000);
@@ -1,50 +0,0 @@
1
- import { isStd, getStdArgs } from "./decorator-utils";
2
-
3
- /**
4
- * 方法级参数绑定注册表:key = `[className, methodName]`,value = 绑定声明。
5
- * 标准装饰器删除了参数装饰器,@bind 是方法级替代(legacy 模式同样可用)。
6
- * 按 value 类型区分两种语义:
7
- * - database 场景:{ SQL占位符名: 参数索引 }(值为 number)
8
- * - route 场景:{ 参数名: 请求来源 }(值为 string,如 "reqParam" / "reqBody" / "reqQuery")
9
- */
10
- const bindParamMap: Map<string, Record<string, string | number>> = new Map();
11
-
12
- /**
13
- * @bind 方法级参数绑定装饰器。
14
- *
15
- * database 散参绑定(对象传参已是主路径,@bind 作散参补充):
16
- * ```
17
- * @insert("Insert into `user` (id, name) values (#{id}, #{name})")
18
- * @bind({ name: 0, id: 1 })
19
- * async addRow(newName: string, id: number) { }
20
- * ```
21
- *
22
- * route 参数绑定(替代 req/res/reqBody/reqParam/reqQuery/reqForm 参数装饰器):
23
- * ```
24
- * @getMapping("/user/:id")
25
- * @bind({ id: "reqParam", body: "reqBody", q: "reqQuery" })
26
- * async getUser(id: string, body: any, q: string) { }
27
- * ```
28
- */
29
- function bind(mapping: Record<string, string | number>) {
30
- return function (...args: any[]): any {
31
- if (isStd(args)) {
32
- const [, ctx] = getStdArgs(args);
33
- const methodName = String(ctx.name);
34
- ctx.addInitializer(function (this: any) {
35
- bindParamMap.set([this.constructor.name, methodName].toString(), mapping);
36
- });
37
- return;
38
- }
39
- const target = args[0];
40
- const propertyKey = args[1] as string;
41
- bindParamMap.set([target.constructor.name, propertyKey].toString(), mapping);
42
- };
43
- }
44
-
45
- /** 读取某方法上的 @bind 声明(route/database 内部使用) */
46
- function getBindMapping(className: string, methodName: string): Record<string, string | number> | undefined {
47
- return bindParamMap.get([className, methodName].toString());
48
- }
49
-
50
- export { bind, getBindMapping };
@@ -1,184 +0,0 @@
1
- import "reflect-metadata";
2
- import * as cron from "cron";
3
- import LogFactory from "./factory/log-factory.class";
4
- import { isStd, getStdArgs } from "./decorator-utils";
5
-
6
- const resourceObjects = new Map<string, object>();
7
- const beanMapper: Map<string, any> = new Map<string, any>();
8
- const objectMapper: Map<string, any> = new Map<string, any>();
9
-
10
- function component(...args: any[]): any {
11
- if (isStd(args)) {
12
- const [ctor, ctx] = getStdArgs(args);
13
- ctx.addInitializer(function (this: any) {
14
- objectMapper.set(this.name, new this());
15
- });
16
- return;
17
- }
18
- const constructorFunction = args[0];
19
- objectMapper.set(constructorFunction.name, new constructorFunction());
20
- }
21
-
22
- function getComponent(constructorFunction) {
23
- return objectMapper.get(constructorFunction.name);
24
- }
25
-
26
- function bean(...args: any[]): any {
27
- if (args.length >= 2) {
28
- // 直接装饰器形式:@bean(无 token,走 legacy design:returntype)
29
- return beanWithToken(undefined)(...args);
30
- }
31
- // 工厂形式:@bean(Token)(显式返回类型 token,标准模式必需)
32
- return beanWithToken(args[0]);
33
- }
34
-
35
- function beanWithToken(token?: any) {
36
- return function (...args: any[]): any {
37
- if (isStd(args)) {
38
- const [, ctx] = getStdArgs(args);
39
- const key = String(ctx.name);
40
- ctx.addInitializer(function (this: any) {
41
- beanMapper.set(token ? token.name : key, {
42
- "target": this, "propertyKey": key,
43
- "factory": this[key]()
44
- });
45
- });
46
- return;
47
- }
48
- const target = args[0];
49
- const propertyKey = args[1] as string;
50
- let returnType = token || Reflect.getMetadata("design:returntype", target, propertyKey);
51
- beanMapper.set(returnType.name, {
52
- "target": target, "propertyKey": propertyKey,
53
- "factory": target[propertyKey]()
54
- });
55
- };
56
- }
57
-
58
- function getBean(mappingClass: Function): any {
59
- const bean = beanMapper.get(mappingClass.name);
60
- return bean["factory"];
61
- }
62
-
63
-
64
- function autoware(...args: any[]): any {
65
- if (args.length >= 2) {
66
- // 直接装饰器形式:@autoware(无 token,走 legacy design:type)
67
- return autowareWithToken(undefined)(...args);
68
- }
69
- // 工厂形式:@autoware(Token)(显式 token,标准模式必需)
70
- return autowareWithToken(args[0]);
71
- }
72
-
73
- function autowareWithToken(token?: any) {
74
- return function (...args: any[]): any {
75
- if (isStd(args)) {
76
- // 标准 field 装饰器:标准模式无 design:type,有 token 才能注入。
77
- return (initialValue: any) => {
78
- if (!token) return initialValue;
79
- const bean = beanMapper.get(token.name);
80
- if (bean !== undefined) return bean["factory"];
81
- return new token();
82
- };
83
- }
84
- const target = args[0];
85
- const propertyKey = args[1] as string;
86
- const type = token || Reflect.getMetadata("design:type", target, propertyKey);
87
- Object.defineProperty(target, propertyKey, {
88
- get: () => {
89
- const targetObject = beanMapper.get(type.name);
90
- if (targetObject === undefined) {
91
- const resourceKey = [target.constructor.name, propertyKey, type.name].toString();
92
- if (!resourceObjects[resourceKey]) {
93
- resourceObjects[resourceKey] = new type();
94
- }
95
- return resourceObjects[resourceKey];
96
- }
97
- return targetObject["factory"];
98
- }
99
- });
100
- };
101
- }
102
-
103
- /**
104
- * 解析 @resource(...args) 的首参:若首参是 Function(类/token),作为显式 token,
105
- * 其余作为构造参数;否则全部作为构造参数(legacy 行为,如 @resource("user"))。
106
- */
107
- function extractTokenAndArgs(args: any[]): [any, any[]] {
108
- if (args.length > 0 && typeof args[0] === "function") {
109
- return [args[0], args.slice(1)];
110
- }
111
- return [undefined, args];
112
- }
113
-
114
- function resource(...args): any {
115
- const [token, initArgs] = extractTokenAndArgs(args);
116
- return (...decoratorArgs: any[]): any => {
117
- if (isStd(decoratorArgs)) {
118
- // 标准 field 装饰器:标准模式无 design:type,需显式 token。
119
- return (initialValue: any) => {
120
- if (!token) return initialValue;
121
- const bean = beanMapper.get(token.name);
122
- if (bean !== undefined) return bean["factory"];
123
- return new token(...initArgs);
124
- };
125
- }
126
- const target = decoratorArgs[0];
127
- const propertyKey = decoratorArgs[1] as string;
128
- const type = token || Reflect.getMetadata("design:type", target, propertyKey);
129
- Object.defineProperty(target, propertyKey, {
130
- get: () => {
131
- const resourceKey = [target.constructor.name, propertyKey, type.name].toString();
132
- if (!resourceObjects[resourceKey]) {
133
- resourceObjects[resourceKey] = new type(...initArgs);
134
- }
135
- return resourceObjects[resourceKey];
136
- }
137
- });
138
- }
139
- }
140
-
141
- function log(message?: any, ...optionalParams: any[]) {
142
- const logObject = beanMapper.get(LogFactory.name);
143
- if (logObject) {
144
- logObject["factory"].log(message, ...optionalParams);
145
- } else {
146
- console.log(message, ...optionalParams);
147
- }
148
- }
149
-
150
- function logx(message) {
151
- message = JSON.stringify(message);
152
- const logObject = beanMapper.get(LogFactory.name);
153
- if (logObject) {
154
- logObject["factory"].log(message);
155
- } else {
156
- console.log(message);
157
- }
158
- }
159
-
160
- function error(message?: any, ...optionalParams: any[]) {
161
- const logObject = beanMapper.get(LogFactory.name);
162
- if (logObject) {
163
- logObject["factory"].error(message, ...optionalParams);
164
- } else {
165
- console.error(message, ...optionalParams);
166
- }
167
- }
168
-
169
- function schedule(cronTime: string | Date) {
170
- return (...args: any[]): any => {
171
- if (isStd(args)) {
172
- const [, ctx] = getStdArgs(args);
173
- ctx.addInitializer(function (this: any) {
174
- new cron.CronJob(cronTime, this[String(ctx.name)]).start();
175
- });
176
- return;
177
- }
178
- const target = args[0];
179
- const propertyKey = args[1] as string;
180
- new cron.CronJob(cronTime, target[propertyKey]).start();
181
- }
182
- }
183
-
184
- export { component, bean, resource, log, logx, error, autoware, getBean, getComponent, schedule };