@zenweb/schedule 3.7.0 → 4.0.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/dist/index.d.ts +2 -2
- package/dist/index.js +10 -11
- package/dist/register.d.ts +2 -2
- package/dist/register.js +17 -20
- package/dist/types.d.ts +1 -2
- package/dist/types.js +1 -2
- package/package.json +12 -11
- package/CHANGELOG.md +0 -7
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { SetupFunction } from '@zenweb/core';
|
|
2
|
-
import { schedule, ScheduleRegister } from './register';
|
|
3
|
-
import { ScheduleOption } from './types';
|
|
2
|
+
import { schedule, ScheduleRegister } from './register.js';
|
|
3
|
+
import { ScheduleOption } from './types.js';
|
|
4
4
|
export { ScheduleOption, schedule };
|
|
5
5
|
export default function setup(opt?: ScheduleOption): SetupFunction;
|
|
6
6
|
declare module '@zenweb/core' {
|
package/dist/index.js
CHANGED
|
@@ -1,11 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
Object.defineProperty(exports, "schedule", { enumerable: true, get: function () { return register_1.schedule; } });
|
|
8
|
-
function setup(opt) {
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { pathToFileURL } from 'node:url';
|
|
3
|
+
import { globby } from 'globby';
|
|
4
|
+
import { schedule, ScheduleRegister } from './register.js';
|
|
5
|
+
export { schedule };
|
|
6
|
+
export default function setup(opt) {
|
|
9
7
|
const option = Object.assign({
|
|
10
8
|
paths: ['./app/schedule'],
|
|
11
9
|
disabled: process.env.ZENWEB_SCHEDULE_DISABLED === '1',
|
|
@@ -14,7 +12,7 @@ function setup(opt) {
|
|
|
14
12
|
setup.assertModuleExists('inject', '@zenweb/inject');
|
|
15
13
|
setup.assertModuleExists('router', '@zenweb/router');
|
|
16
14
|
setup.debug('option: %o', option);
|
|
17
|
-
const scheduleRegister = new
|
|
15
|
+
const scheduleRegister = new ScheduleRegister(setup.core, option);
|
|
18
16
|
setup.defineCoreProperty('scheduleRegister', { value: scheduleRegister });
|
|
19
17
|
if (option.paths && option.paths.length) {
|
|
20
18
|
for (let d of option.paths) {
|
|
@@ -22,7 +20,9 @@ function setup(opt) {
|
|
|
22
20
|
d = path.join(process.cwd(), d.slice(2));
|
|
23
21
|
}
|
|
24
22
|
for (const file of await globby(option.patterns || '**/*.{ts,js}', { cwd: d, absolute: true })) {
|
|
25
|
-
|
|
23
|
+
setup.debug('load:', file);
|
|
24
|
+
const importUrl = pathToFileURL(file).href;
|
|
25
|
+
const mod = await import(importUrl);
|
|
26
26
|
for (const i of Object.values(mod)) {
|
|
27
27
|
if (typeof i === 'function') {
|
|
28
28
|
scheduleRegister.register(i);
|
|
@@ -39,4 +39,3 @@ function setup(opt) {
|
|
|
39
39
|
});
|
|
40
40
|
};
|
|
41
41
|
}
|
|
42
|
-
exports.default = setup;
|
package/dist/register.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Core, Middleware } from '@zenweb/core';
|
|
2
2
|
import { RecurrenceRule, RecurrenceSpecDateRange, RecurrenceSpecObjLit } from 'node-schedule';
|
|
3
|
-
import { ScheduleOption } from './types';
|
|
3
|
+
import { ScheduleOption } from './types.js';
|
|
4
4
|
interface ScheduleMethodOption {
|
|
5
5
|
rule: RecurrenceRule | RecurrenceSpecDateRange | RecurrenceSpecObjLit | Date | string | number;
|
|
6
6
|
middleware?: Middleware | Middleware[];
|
|
@@ -8,7 +8,7 @@ interface ScheduleMethodOption {
|
|
|
8
8
|
/**
|
|
9
9
|
* 定时任务设定
|
|
10
10
|
*/
|
|
11
|
-
export declare function schedule(opt: ScheduleMethodOption): (target: Object, propertyKey: string | symbol, descriptor_or_paramtypes?:
|
|
11
|
+
export declare function schedule(opt: ScheduleMethodOption): (target: Object, propertyKey: string | symbol, descriptor_or_paramtypes?: PropertyDescriptor | any[]) => void;
|
|
12
12
|
export declare class ScheduleRegister {
|
|
13
13
|
private core;
|
|
14
14
|
private router;
|
package/dist/register.js
CHANGED
|
@@ -1,28 +1,27 @@
|
|
|
1
|
-
"
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
const
|
|
8
|
-
const router_1 = require("@zenweb/router");
|
|
9
|
-
const inject_1 = require("@zenweb/inject");
|
|
10
|
-
const scheduleDecorator = (0, decorator_make_1.makeMethodDecorator)();
|
|
1
|
+
/// <reference types="@zenweb/inject" />
|
|
2
|
+
import { ServerResponse } from 'node:http';
|
|
3
|
+
import { scheduleJob } from 'node-schedule';
|
|
4
|
+
import { randomUUID } from 'node:crypto';
|
|
5
|
+
import { makeMethodDecorator } from 'decorator-make';
|
|
6
|
+
import { Router } from '@zenweb/router';
|
|
7
|
+
const scheduleDecorator = makeMethodDecorator();
|
|
11
8
|
/**
|
|
12
9
|
* 定时任务设定
|
|
13
10
|
*/
|
|
14
|
-
function schedule(opt) {
|
|
11
|
+
export function schedule(opt) {
|
|
15
12
|
return scheduleDecorator.wrap((descriptor, target) => {
|
|
16
|
-
const path = `${
|
|
13
|
+
const path = `${randomUUID()}/${target.constructor.name}.${descriptor.handle.name}`;
|
|
17
14
|
return Object.assign({ path }, descriptor, opt);
|
|
18
15
|
});
|
|
19
16
|
}
|
|
20
|
-
|
|
21
|
-
|
|
17
|
+
export class ScheduleRegister {
|
|
18
|
+
core;
|
|
19
|
+
router;
|
|
20
|
+
option;
|
|
21
|
+
jobs = [];
|
|
22
22
|
constructor(core, option) {
|
|
23
|
-
this.jobs = [];
|
|
24
23
|
this.core = core;
|
|
25
|
-
this.router = new
|
|
24
|
+
this.router = new Router({
|
|
26
25
|
prefix: '/__schedule/',
|
|
27
26
|
});
|
|
28
27
|
this.option = option;
|
|
@@ -33,7 +32,6 @@ class ScheduleRegister {
|
|
|
33
32
|
register(target) {
|
|
34
33
|
const methods = scheduleDecorator.getMethods(target.prototype);
|
|
35
34
|
if (methods.length > 0) {
|
|
36
|
-
(0, inject_1.scope)('prototype', false)(target);
|
|
37
35
|
for (const item of methods) {
|
|
38
36
|
// 添加到路由中
|
|
39
37
|
this.router.post(item.path, ...(item.middleware ?
|
|
@@ -45,7 +43,7 @@ class ScheduleRegister {
|
|
|
45
43
|
continue;
|
|
46
44
|
}
|
|
47
45
|
// 启用定时器
|
|
48
|
-
const job =
|
|
46
|
+
const job = scheduleJob(item.rule, () => {
|
|
49
47
|
const path = '/__schedule/' + item.path;
|
|
50
48
|
const request = Object.assign({
|
|
51
49
|
headers: {
|
|
@@ -65,7 +63,7 @@ class ScheduleRegister {
|
|
|
65
63
|
remotePort: 7001,
|
|
66
64
|
},
|
|
67
65
|
});
|
|
68
|
-
const response = new
|
|
66
|
+
const response = new ServerResponse(request);
|
|
69
67
|
this.core.app.callback()(request, response);
|
|
70
68
|
});
|
|
71
69
|
this.jobs.push(job);
|
|
@@ -81,4 +79,3 @@ class ScheduleRegister {
|
|
|
81
79
|
}
|
|
82
80
|
}
|
|
83
81
|
}
|
|
84
|
-
exports.ScheduleRegister = ScheduleRegister;
|
package/dist/types.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
import { ServerResponse, IncomingMessage } from 'http';
|
|
1
|
+
import { ServerResponse, IncomingMessage } from 'node:http';
|
|
3
2
|
export type RequestCallback = (request: IncomingMessage, response: ServerResponse) => void;
|
|
4
3
|
export type JobCallback = (request: IncomingMessage, response: ServerResponse, callback: RequestCallback) => void;
|
|
5
4
|
export interface ScheduleOption {
|
package/dist/types.js
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zenweb/schedule",
|
|
3
|
-
"
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "4.0.1",
|
|
4
5
|
"description": "Zenweb Schedule module",
|
|
5
6
|
"exports": "./dist/index.js",
|
|
6
7
|
"types": "./dist/index.d.ts",
|
|
@@ -9,8 +10,8 @@
|
|
|
9
10
|
],
|
|
10
11
|
"scripts": {
|
|
11
12
|
"build": "rimraf dist && tsc",
|
|
12
|
-
"
|
|
13
|
-
"dev": "cd example &&
|
|
13
|
+
"prepack": "npm run build",
|
|
14
|
+
"dev": "cd example && node --env-file=.env --loader=ts-node/esm app.ts"
|
|
14
15
|
},
|
|
15
16
|
"author": {
|
|
16
17
|
"name": "YeFei",
|
|
@@ -34,18 +35,18 @@
|
|
|
34
35
|
"url": "https://github.com/yefei/zenweb-schedule/issues"
|
|
35
36
|
},
|
|
36
37
|
"devDependencies": {
|
|
37
|
-
"@
|
|
38
|
-
"@zenweb/
|
|
39
|
-
"@zenweb/
|
|
40
|
-
"
|
|
38
|
+
"@types/node": "^22.7.5",
|
|
39
|
+
"@zenweb/core": "^5.1.0",
|
|
40
|
+
"@zenweb/inject": "^5.1.0",
|
|
41
|
+
"@zenweb/router": "^5.0.0",
|
|
41
42
|
"rimraf": "^4.3.1",
|
|
42
43
|
"ts-node": "^10.9.1",
|
|
43
|
-
"typescript": "^
|
|
44
|
+
"typescript": "^5.6.3"
|
|
44
45
|
},
|
|
45
46
|
"dependencies": {
|
|
46
|
-
"@types/node-schedule": "^2.1.
|
|
47
|
-
"decorator-make": "^1.
|
|
48
|
-
"globby": "
|
|
47
|
+
"@types/node-schedule": "^2.1.7",
|
|
48
|
+
"decorator-make": "^1.5.0",
|
|
49
|
+
"globby": "^14.0.2",
|
|
49
50
|
"node-schedule": "^2.1.1"
|
|
50
51
|
}
|
|
51
52
|
}
|