@jayfong/x-server 1.30.2 → 1.31.3

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 CHANGED
@@ -2,6 +2,24 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
4
4
 
5
+ ### [1.31.3](https://github.com/jfWorks/x-server/compare/v1.31.2...v1.31.3) (2022-05-30)
6
+
7
+
8
+ ### Bug Fixes
9
+
10
+ * defineCron ([5d72533](https://github.com/jfWorks/x-server/commit/5d72533c039645833391ce31f0403c96b2c5ef77))
11
+
12
+ ### [1.31.2](https://github.com/jfWorks/x-server/compare/v1.31.1...v1.31.2) (2022-05-30)
13
+
14
+ ### [1.31.1](https://github.com/jfWorks/x-server/compare/v1.31.0...v1.31.1) (2022-05-30)
15
+
16
+ ## [1.31.0](https://github.com/jfWorks/x-server/compare/v1.30.2...v1.31.0) (2022-05-30)
17
+
18
+
19
+ ### Features
20
+
21
+ * defineCron ([f9f362e](https://github.com/jfWorks/x-server/commit/f9f362ee0a9845f662af0b686781299390c5647d))
22
+
5
23
  ### [1.30.2](https://github.com/jfWorks/x-server/compare/v1.30.1...v1.30.2) (2022-05-26)
6
24
 
7
25
 
@@ -0,0 +1,2 @@
1
+ // @index(['../../src/crons/**/*.ts', '!**/_*'], (f, _) => `import '${f.path}'`)
2
+ // @endindex
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.defineCron = defineCron;
5
+
6
+ var _cron = require("cron");
7
+
8
+ function defineCron(options) {
9
+ return new _cron.CronJob({
10
+ cronTime: options.expression,
11
+ runOnInit: options.runOnInit,
12
+ start: true,
13
+ onTick: options.handle
14
+ });
15
+ }
@@ -21,14 +21,5 @@ function defineTask(options) {
21
21
  queue.process(async job => {
22
22
  return options.handle(job.data);
23
23
  });
24
-
25
- if (options.cron) {
26
- queue.add({}, {
27
- repeat: {
28
- cron: options.cron
29
- }
30
- });
31
- }
32
-
33
24
  return queue;
34
25
  }
@@ -31,7 +31,7 @@ class Server {
31
31
  await this.applyRoutes();
32
32
  await this.applyHooks();
33
33
  await this.startFastify();
34
- await this.startTasks();
34
+ await this.startCrons();
35
35
  await this.applyAutoClose();
36
36
  }
37
37
 
@@ -145,9 +145,9 @@ class Server {
145
145
  }
146
146
  }
147
147
 
148
- async startTasks() {
148
+ async startCrons() {
149
149
  // @ts-ignore
150
- await Promise.resolve().then(() => (0, _interopRequireWildcard2.default)(require('.x/tasks')));
150
+ await Promise.resolve().then(() => (0, _interopRequireWildcard2.default)(require('.x/crons')));
151
151
  }
152
152
 
153
153
  async applyAutoClose() {
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
 
3
3
  exports.__esModule = true;
4
- exports.XTask = exports.XServer = exports.XHandler = void 0;
4
+ exports.XTask = exports.XServer = exports.XHandler = exports.XCron = void 0;
5
5
  let XServer;
6
6
  exports.XServer = XServer;
7
7
 
@@ -15,4 +15,9 @@ exports.XHandler = XHandler;
15
15
  let XTask;
16
16
  exports.XTask = XTask;
17
17
 
18
- (function (_XTask) {})(XTask || (exports.XTask = XTask = {}));
18
+ (function (_XTask) {})(XTask || (exports.XTask = XTask = {}));
19
+
20
+ let XCron;
21
+ exports.XCron = XCron;
22
+
23
+ (function (_XCron) {})(XCron || (exports.XCron = XCron = {}));
package/lib/_cjs/index.js CHANGED
@@ -10,6 +10,14 @@ Object.keys(_define_bus).forEach(function (key) {
10
10
  exports[key] = _define_bus[key];
11
11
  });
12
12
 
13
+ var _define_cron = require("./core/define_cron");
14
+
15
+ Object.keys(_define_cron).forEach(function (key) {
16
+ if (key === "default" || key === "__esModule") return;
17
+ if (key in exports && exports[key] === _define_cron[key]) return;
18
+ exports[key] = _define_cron[key];
19
+ });
20
+
13
21
  var _define_handler = require("./core/define_handler");
14
22
 
15
23
  Object.keys(_define_handler).forEach(function (key) {
@@ -0,0 +1,2 @@
1
+ // @index(['../../src/crons/**/*.ts', '!**/_*'], (f, _) => `import '${f.path}'`)
2
+ // @endindex
@@ -0,0 +1,2 @@
1
+ import { XCron } from './types';
2
+ export declare function defineCron(options: XCron.Options): XCron.Cron;
@@ -0,0 +1,9 @@
1
+ import { CronJob } from 'cron';
2
+ export function defineCron(options) {
3
+ return new CronJob({
4
+ cronTime: options.expression,
5
+ runOnInit: options.runOnInit,
6
+ start: true,
7
+ onTick: options.handle
8
+ });
9
+ }
@@ -12,14 +12,5 @@ export function defineTask(options) {
12
12
  queue.process(async job => {
13
13
  return options.handle(job.data);
14
14
  });
15
-
16
- if (options.cron) {
17
- queue.add({}, {
18
- repeat: {
19
- cron: options.cron
20
- }
21
- });
22
- }
23
-
24
15
  return queue;
25
16
  }
@@ -14,6 +14,6 @@ export declare class Server {
14
14
  private applyServices;
15
15
  private applyPlugins;
16
16
  private applyRoutes;
17
- private startTasks;
17
+ private startCrons;
18
18
  private applyAutoClose;
19
19
  }
@@ -18,7 +18,7 @@ export class Server {
18
18
  await this.applyRoutes();
19
19
  await this.applyHooks();
20
20
  await this.startFastify();
21
- await this.startTasks();
21
+ await this.startCrons();
22
22
  await this.applyAutoClose();
23
23
  }
24
24
 
@@ -130,9 +130,9 @@ export class Server {
130
130
  }
131
131
  }
132
132
 
133
- async startTasks() {
133
+ async startCrons() {
134
134
  // @ts-ignore
135
- await import('.x/tasks');
135
+ await import('.x/crons');
136
136
  }
137
137
 
138
138
  async applyAutoClose() {
@@ -1,6 +1,7 @@
1
1
  /// <reference types="node" />
2
2
  import { BasePlugin } from '../plugins/base';
3
3
  import { BaseService } from '../services/base';
4
+ import { CronJob } from 'cron';
4
5
  import { FastifyReply, FastifyRequest, FastifyServerOptions } from 'fastify';
5
6
  import { yup } from 'vtils/validator';
6
7
  import type { AsyncOrSync, LiteralUnion, OneOrMore, RequiredDeep } from 'vtils/types';
@@ -139,17 +140,32 @@ export declare namespace XTask {
139
140
  */
140
141
  name: string;
141
142
  /**
142
- * 是否作为定时任务
143
+ * 处理器
144
+ */
145
+ handle: (data: T) => any;
146
+ }
147
+ interface Task<T> extends Queue<T> {
148
+ }
149
+ }
150
+ export declare namespace XCron {
151
+ interface Options {
152
+ /**
153
+ * 定时语法
143
154
  *
144
155
  * @see https://crontab.guru/
156
+ * @see https://crontab.cronhub.io/
157
+ */
158
+ expression: string;
159
+ /**
160
+ * 是否初始化后执行一次
145
161
  */
146
- cron?: string;
162
+ runOnInit?: boolean;
147
163
  /**
148
164
  * 处理器
149
165
  */
150
- handle: (data: T) => any;
166
+ handle: () => any;
151
167
  }
152
- interface Task<T> extends Queue<T> {
168
+ interface Cron extends CronJob {
153
169
  }
154
170
  }
155
171
  export declare type XFile = MultipartFile;
package/lib/core/types.js CHANGED
@@ -8,4 +8,8 @@ export let XHandler;
8
8
 
9
9
  export let XTask;
10
10
 
11
- (function (_XTask) {})(XTask || (XTask = {}));
11
+ (function (_XTask) {})(XTask || (XTask = {}));
12
+
13
+ export let XCron;
14
+
15
+ (function (_XCron) {})(XCron || (XCron = {}));
package/lib/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from './core/define_bus';
2
+ export * from './core/define_cron';
2
3
  export * from './core/define_handler';
3
4
  export * from './core/define_hook';
4
5
  export * from './core/define_server';
package/lib/index.js CHANGED
@@ -1,5 +1,6 @@
1
- // @index(['./**/*.ts', '!**/*.test.ts', '!./cli/**'], f => `export * from '${f.path}'`)
1
+ // @index(['./**/*.ts', '!**/*.test.ts', '!./cli/**', '!./client_helper.ts'], f => `export * from '${f.path}'`)
2
2
  export * from "./core/define_bus";
3
+ export * from "./core/define_cron";
3
4
  export * from "./core/define_handler";
4
5
  export * from "./core/define_hook";
5
6
  export * from "./core/define_server";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jayfong/x-server",
3
- "version": "1.30.2",
3
+ "version": "1.31.3",
4
4
  "license": "ISC",
5
5
  "sideEffects": false,
6
6
  "main": "lib/_cjs/index.js",
@@ -30,6 +30,7 @@
30
30
  "@prisma/client": "^3.12.0",
31
31
  "@types/bull": "^3.15.8",
32
32
  "@types/busboy": "^0.3.2",
33
+ "@types/cron": "^2.0.0",
33
34
  "@types/http-errors": "^1.8.2",
34
35
  "@types/jsonwebtoken": "^8.5.8",
35
36
  "@types/nodemailer": "^6.4.4",
@@ -40,6 +41,7 @@
40
41
  "chokidar": "^3.5.3",
41
42
  "comment-parser": "^1.3.1",
42
43
  "compressing": "^1.5.1",
44
+ "cron": "^2.0.0",
43
45
  "cuid": "^2.1.8",
44
46
  "debug": "^4.3.4",
45
47
  "esbuild": "^0.14.36",
@@ -1 +0,0 @@
1
- import '../../src/tasks/index'
@@ -1 +0,0 @@
1
- import '../../src/tasks/index'