@zenweb/schedule 2.3.1 → 2.3.5
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 +10 -0
- package/dist/index.js +38 -0
- package/dist/index.js.map +1 -0
- package/dist/register.d.ts +15 -0
- package/dist/register.js +60 -0
- package/dist/register.js.map +1 -0
- package/dist/types.d.ts +8 -0
- package/dist/types.js +3 -0
- package/dist/types.js.map +1 -0
- package/package.json +7 -4
- package/example/app/schedule/echo.js +0 -8
- package/example/app.js +0 -11
- package/src/index.ts +0 -33
- package/src/register.ts +0 -80
- package/src/types.ts +0 -9
- package/tsconfig.json +0 -21
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { SetupFunction } from '@zenweb/core';
|
|
2
|
+
import { ScheduleRegister } from './register';
|
|
3
|
+
import { ScheduleOption } from './types';
|
|
4
|
+
export * from './types';
|
|
5
|
+
export default function setup(option?: ScheduleOption): SetupFunction;
|
|
6
|
+
declare module '@zenweb/core' {
|
|
7
|
+
interface Core {
|
|
8
|
+
schedule: ScheduleRegister;
|
|
9
|
+
}
|
|
10
|
+
}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
5
|
+
}) : (function(o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
o[k2] = m[k];
|
|
8
|
+
}));
|
|
9
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
10
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
11
|
+
};
|
|
12
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
+
const globby = require("globby");
|
|
14
|
+
const path = require("path");
|
|
15
|
+
const register_1 = require("./register");
|
|
16
|
+
__exportStar(require("./types"), exports);
|
|
17
|
+
function setup(option) {
|
|
18
|
+
return async function schedule(setup) {
|
|
19
|
+
option = Object.assign({
|
|
20
|
+
paths: [path.join(process.cwd(), 'app', 'schedule')],
|
|
21
|
+
}, option);
|
|
22
|
+
setup.debug('option: %o', option);
|
|
23
|
+
const register = new register_1.ScheduleRegister(setup.core, option);
|
|
24
|
+
setup.defineCoreProperty('schedule', { value: register });
|
|
25
|
+
if (option.paths && option.paths.length) {
|
|
26
|
+
for (const d of option.paths) {
|
|
27
|
+
let count = 0;
|
|
28
|
+
for (const m of await globby(d, { cwd: d, absolute: true })) {
|
|
29
|
+
require(m);
|
|
30
|
+
count++;
|
|
31
|
+
}
|
|
32
|
+
setup.debug('load: %s %o files', d, count);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
exports.default = setup;
|
|
38
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,iCAAiC;AAEjC,6BAA8B;AAC9B,yCAA8C;AAE9C,0CAAwB;AAExB,SAAwB,KAAK,CAAC,MAAuB;IACnD,OAAO,KAAK,UAAU,QAAQ,CAAC,KAAK;QAClC,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC;YACrB,KAAK,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,GAAG,EAAE,EAAE,KAAK,EAAE,UAAU,CAAC,CAAC;SACrD,EAAE,MAAM,CAAC,CAAC;QACX,KAAK,CAAC,KAAK,CAAC,YAAY,EAAE,MAAM,CAAC,CAAC;QAClC,MAAM,QAAQ,GAAG,IAAI,2BAAgB,CAAC,KAAK,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;QAC1D,KAAK,CAAC,kBAAkB,CAAC,UAAU,EAAE,EAAE,KAAK,EAAE,QAAQ,EAAE,CAAC,CAAC;QAC1D,IAAI,MAAM,CAAC,KAAK,IAAI,MAAM,CAAC,KAAK,CAAC,MAAM,EAAE;YACvC,KAAK,MAAM,CAAC,IAAI,MAAM,CAAC,KAAK,EAAE;gBAC5B,IAAI,KAAK,GAAG,CAAC,CAAC;gBACd,KAAK,MAAM,CAAC,IAAI,MAAM,MAAM,CAAC,CAAC,EAAE,EAAE,GAAG,EAAE,CAAC,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC,EAAE;oBAC3D,OAAO,CAAC,CAAC,CAAC,CAAC;oBACX,KAAK,EAAE,CAAC;iBACT;gBACD,KAAK,CAAC,KAAK,CAAC,mBAAmB,EAAE,CAAC,EAAE,KAAK,CAAC,CAAC;aAC5C;SACF;IACH,CAAC,CAAA;AACH,CAAC;AAnBD,wBAmBC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/// <reference types="koa__router" />
|
|
2
|
+
import '@zenweb/router';
|
|
3
|
+
import { Middleware } from 'koa';
|
|
4
|
+
import { Core } from '@zenweb/core';
|
|
5
|
+
import { RequestCallback, ScheduleOption } from './types';
|
|
6
|
+
import { Router } from '@zenweb/router';
|
|
7
|
+
import { RecurrenceRule, RecurrenceSpecDateRange, RecurrenceSpecObjLit } from 'node-schedule';
|
|
8
|
+
export declare class ScheduleRegister {
|
|
9
|
+
option: ScheduleOption;
|
|
10
|
+
router: Router;
|
|
11
|
+
callback: RequestCallback;
|
|
12
|
+
private _index;
|
|
13
|
+
constructor(core: Core, option?: ScheduleOption);
|
|
14
|
+
job(rule: RecurrenceRule | RecurrenceSpecDateRange | RecurrenceSpecObjLit | Date | string | number, ...middleware: Middleware[]): import("node-schedule").Job;
|
|
15
|
+
}
|
package/dist/register.js
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ScheduleRegister = void 0;
|
|
4
|
+
require("@zenweb/router");
|
|
5
|
+
const http_1 = require("http");
|
|
6
|
+
const node_schedule_1 = require("node-schedule");
|
|
7
|
+
const SAFE_IP = '127.0.0.1';
|
|
8
|
+
/**
|
|
9
|
+
* 安全检查,防止外部调用
|
|
10
|
+
*/
|
|
11
|
+
function safeCheck(ctx, next) {
|
|
12
|
+
if (!ctx.req.socket.remoteAddress || !ctx.req.socket.remoteAddress.endsWith(SAFE_IP)) {
|
|
13
|
+
ctx.throw(403);
|
|
14
|
+
}
|
|
15
|
+
return next();
|
|
16
|
+
}
|
|
17
|
+
class ScheduleRegister {
|
|
18
|
+
constructor(core, option) {
|
|
19
|
+
this.option = option || {};
|
|
20
|
+
this.router = core.router;
|
|
21
|
+
this.callback = core.koa.callback();
|
|
22
|
+
this._index = 0;
|
|
23
|
+
}
|
|
24
|
+
job(rule, ...middleware) {
|
|
25
|
+
// 注册到路由
|
|
26
|
+
const path = `/___schedule/job/${this._index++}`;
|
|
27
|
+
this.router.post(path, safeCheck, ...middleware);
|
|
28
|
+
// 注册到 scheduleJob
|
|
29
|
+
const callback = () => {
|
|
30
|
+
const request = Object.assign({
|
|
31
|
+
headers: {
|
|
32
|
+
host: '127.0.0.1',
|
|
33
|
+
},
|
|
34
|
+
query: {},
|
|
35
|
+
querystring: '',
|
|
36
|
+
host: '127.0.0.1',
|
|
37
|
+
hostname: '127.0.0.1',
|
|
38
|
+
protocol: 'http',
|
|
39
|
+
secure: 'false',
|
|
40
|
+
method: 'POST',
|
|
41
|
+
url: path,
|
|
42
|
+
path: path,
|
|
43
|
+
socket: {
|
|
44
|
+
remoteAddress: SAFE_IP,
|
|
45
|
+
remotePort: 7001,
|
|
46
|
+
},
|
|
47
|
+
});
|
|
48
|
+
const response = new http_1.ServerResponse(request);
|
|
49
|
+
if (this.option.jobCallback) {
|
|
50
|
+
this.option.jobCallback(request, response, this.callback);
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
this.callback(request, response);
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
return (0, node_schedule_1.scheduleJob)(rule, callback);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
exports.ScheduleRegister = ScheduleRegister;
|
|
60
|
+
//# sourceMappingURL=register.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"register.js","sourceRoot":"","sources":["../src/register.ts"],"names":[],"mappings":";;;AAAA,0BAAwB;AACxB,+BAAuD;AAKvD,iDAA2G;AAE3G,MAAM,OAAO,GAAG,WAAW,CAAC;AAE5B;;GAEG;AACH,SAAS,SAAS,CAAC,GAAY,EAAE,IAAU;IACzC,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,MAAM,CAAC,aAAa,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,MAAM,CAAC,aAAa,CAAC,QAAQ,CAAC,OAAO,CAAC,EAAE;QACpF,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;KAChB;IACD,OAAO,IAAI,EAAE,CAAC;AAChB,CAAC;AAED,MAAa,gBAAgB;IAM3B,YAAY,IAAU,EAAE,MAAuB;QAC7C,IAAI,CAAC,MAAM,GAAG,MAAM,IAAI,EAAE,CAAC;QAC3B,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC;QAC1B,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC,GAAG,CAAC,QAAQ,EAAE,CAAC;QACpC,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC;IAClB,CAAC;IAED,GAAG,CACD,IAA8F,EAC9F,GAAG,UAAwB;QAE3B,QAAQ;QACR,MAAM,IAAI,GAAG,oBAAoB,IAAI,CAAC,MAAM,EAAE,EAAE,CAAC;QACjD,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,EAAE,SAAS,EAAE,GAAG,UAAU,CAAC,CAAC;QAEjD,kBAAkB;QAClB,MAAM,QAAQ,GAAG,GAAG,EAAE;YACpB,MAAM,OAAO,GAAoB,MAAM,CAAC,MAAM,CAAC;gBAC7C,OAAO,EAAE;oBACP,IAAI,EAAE,WAAW;iBAClB;gBACD,KAAK,EAAE,EAAE;gBACT,WAAW,EAAE,EAAE;gBACf,IAAI,EAAE,WAAW;gBACjB,QAAQ,EAAE,WAAW;gBACrB,QAAQ,EAAE,MAAM;gBAChB,MAAM,EAAE,OAAO;gBACf,MAAM,EAAE,MAAM;gBACd,GAAG,EAAE,IAAI;gBACT,IAAI,EAAE,IAAI;gBACV,MAAM,EAAE;oBACN,aAAa,EAAE,OAAO;oBACtB,UAAU,EAAE,IAAI;iBACjB;aACF,CAAC,CAAC;YACH,MAAM,QAAQ,GAAG,IAAI,qBAAc,CAAC,OAAO,CAAC,CAAC;YAC7C,IAAI,IAAI,CAAC,MAAM,CAAC,WAAW,EAAE;gBAC3B,IAAI,CAAC,MAAM,CAAC,WAAW,CAAC,OAAO,EAAE,QAAQ,EAAE,IAAI,CAAC,QAAQ,CAAC,CAAC;aAC3D;iBAAM;gBACL,IAAI,CAAC,QAAQ,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC;aAClC;QACH,CAAC,CAAC;QACF,OAAO,IAAA,2BAAW,EAAC,IAAI,EAAE,QAAQ,CAAC,CAAC;IACrC,CAAC;CACF;AAlDD,4CAkDC"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { ServerResponse, IncomingMessage } from 'http';
|
|
3
|
+
export declare type RequestCallback = (request: IncomingMessage, response: ServerResponse) => void;
|
|
4
|
+
export declare type JobCallback = (request: IncomingMessage, response: ServerResponse, callback: RequestCallback) => void;
|
|
5
|
+
export interface ScheduleOption {
|
|
6
|
+
paths?: string[];
|
|
7
|
+
jobCallback?: JobCallback;
|
|
8
|
+
}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
|
package/package.json
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zenweb/schedule",
|
|
3
|
-
"version": "2.3.
|
|
3
|
+
"version": "2.3.5",
|
|
4
4
|
"description": "Zenweb Schedule module",
|
|
5
5
|
"exports": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist"
|
|
9
|
+
],
|
|
7
10
|
"scripts": {
|
|
8
11
|
"prepublishOnly": "rm -fr dist && tsc"
|
|
9
12
|
},
|
|
@@ -29,13 +32,13 @@
|
|
|
29
32
|
"url": "https://github.com/yefei/zenweb-schedule/issues"
|
|
30
33
|
},
|
|
31
34
|
"devDependencies": {
|
|
32
|
-
"@types/node-schedule": "^1.3.2",
|
|
33
35
|
"@zenweb/core": "^2.3.2",
|
|
34
36
|
"@zenweb/router": "^2.3.0",
|
|
35
37
|
"zenweb": "^2.3.1"
|
|
36
38
|
},
|
|
37
39
|
"dependencies": {
|
|
38
|
-
"node-schedule": "^
|
|
39
|
-
"globby": "11.0.4"
|
|
40
|
+
"@types/node-schedule": "^1.3.2",
|
|
41
|
+
"globby": "11.0.4",
|
|
42
|
+
"node-schedule": "^2.0.0"
|
|
40
43
|
}
|
|
41
44
|
}
|
package/example/app.js
DELETED
package/src/index.ts
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import * as globby from 'globby';
|
|
2
|
-
import { SetupFunction } from '@zenweb/core';
|
|
3
|
-
import path = require('path');
|
|
4
|
-
import { ScheduleRegister } from './register';
|
|
5
|
-
import { ScheduleOption } from './types';
|
|
6
|
-
export * from './types';
|
|
7
|
-
|
|
8
|
-
export function setup(option?: ScheduleOption): SetupFunction {
|
|
9
|
-
return async function schedule(setup) {
|
|
10
|
-
option = Object.assign({
|
|
11
|
-
paths: [path.join(process.cwd(), 'app', 'schedule')],
|
|
12
|
-
}, option);
|
|
13
|
-
setup.debug('option: %o', option);
|
|
14
|
-
const register = new ScheduleRegister(setup.core, option);
|
|
15
|
-
setup.defineCoreProperty('schedule', { value: register });
|
|
16
|
-
if (option.paths && option.paths.length) {
|
|
17
|
-
for (const d of option.paths) {
|
|
18
|
-
let count = 0;
|
|
19
|
-
for (const m of await globby(d, { cwd: d, absolute: true })) {
|
|
20
|
-
require(m);
|
|
21
|
-
count++;
|
|
22
|
-
}
|
|
23
|
-
setup.debug('load: %s %o files', d, count);
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
declare module '@zenweb/core' {
|
|
30
|
-
interface Core {
|
|
31
|
-
schedule: ScheduleRegister;
|
|
32
|
-
}
|
|
33
|
-
}
|
package/src/register.ts
DELETED
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
import '@zenweb/router';
|
|
2
|
-
import schedule = require('node-schedule');
|
|
3
|
-
import { ServerResponse, IncomingMessage } from 'http';
|
|
4
|
-
import { Context, Next } from 'koa';
|
|
5
|
-
import { Core } from '@zenweb/core';
|
|
6
|
-
import { RequestCallback, ScheduleOption } from './types';
|
|
7
|
-
import { Router } from '@zenweb/router';
|
|
8
|
-
|
|
9
|
-
const SAFE_IP = '127.0.0.1';
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* 安全检查,防止外部调用
|
|
13
|
-
*/
|
|
14
|
-
function safeCheck(ctx: Context, next: Next) {
|
|
15
|
-
if (!ctx.req.socket.remoteAddress || !ctx.req.socket.remoteAddress.endsWith(SAFE_IP)) {
|
|
16
|
-
ctx.throw(403);
|
|
17
|
-
}
|
|
18
|
-
return next();
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export class ScheduleRegister {
|
|
22
|
-
option: ScheduleOption;
|
|
23
|
-
router: Router;
|
|
24
|
-
callback: RequestCallback;
|
|
25
|
-
private _index: number;
|
|
26
|
-
|
|
27
|
-
constructor(core: Core, option?: ScheduleOption) {
|
|
28
|
-
this.option = option || {};
|
|
29
|
-
this.router = core.router;
|
|
30
|
-
this.callback = core.koa.callback();
|
|
31
|
-
this._index = 0;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
job(...args: any[]) {
|
|
35
|
-
let name = '';
|
|
36
|
-
let rule;
|
|
37
|
-
|
|
38
|
-
if (typeof args[1] === 'string') {
|
|
39
|
-
name = args[0];
|
|
40
|
-
rule = args[1];
|
|
41
|
-
args = args.slice(2);
|
|
42
|
-
} else {
|
|
43
|
-
rule = args[0];
|
|
44
|
-
args = args.slice(1);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
// 注册到路由
|
|
48
|
-
const path = `/___schedule/job/${this._index++}/${name}`;
|
|
49
|
-
this.router.post(path, safeCheck, ...args);
|
|
50
|
-
|
|
51
|
-
// 注册到 scheduleJob
|
|
52
|
-
const callback = () => {
|
|
53
|
-
const request: IncomingMessage = Object.assign({
|
|
54
|
-
headers: {
|
|
55
|
-
host: '127.0.0.1',
|
|
56
|
-
},
|
|
57
|
-
query: {},
|
|
58
|
-
querystring: '',
|
|
59
|
-
host: '127.0.0.1',
|
|
60
|
-
hostname: '127.0.0.1',
|
|
61
|
-
protocol: 'http',
|
|
62
|
-
secure: 'false',
|
|
63
|
-
method: 'POST',
|
|
64
|
-
url: path,
|
|
65
|
-
path: path,
|
|
66
|
-
socket: {
|
|
67
|
-
remoteAddress: SAFE_IP,
|
|
68
|
-
remotePort: 7001,
|
|
69
|
-
},
|
|
70
|
-
});
|
|
71
|
-
const response = new ServerResponse(request);
|
|
72
|
-
if (this.option.jobCallback) {
|
|
73
|
-
this.option.jobCallback(request, response, this.callback);
|
|
74
|
-
} else {
|
|
75
|
-
this.callback(request, response);
|
|
76
|
-
}
|
|
77
|
-
};
|
|
78
|
-
return schedule.scheduleJob(name, rule, callback);
|
|
79
|
-
}
|
|
80
|
-
}
|
package/src/types.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { ServerResponse, IncomingMessage } from 'http';
|
|
2
|
-
|
|
3
|
-
export type RequestCallback = (request: IncomingMessage, response: ServerResponse) => void;
|
|
4
|
-
export type JobCallback = (request: IncomingMessage, response: ServerResponse, callback: RequestCallback) => void;
|
|
5
|
-
|
|
6
|
-
export interface ScheduleOption {
|
|
7
|
-
paths?: string[];
|
|
8
|
-
jobCallback?: JobCallback;
|
|
9
|
-
}
|
package/tsconfig.json
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"target": "ES2019",
|
|
4
|
-
"lib": [
|
|
5
|
-
"ES2019"
|
|
6
|
-
],
|
|
7
|
-
"module": "commonjs",
|
|
8
|
-
"noImplicitAny": true,
|
|
9
|
-
"preserveConstEnums": true,
|
|
10
|
-
"sourceMap": true,
|
|
11
|
-
"declaration": true,
|
|
12
|
-
"outDir": "./dist"
|
|
13
|
-
},
|
|
14
|
-
"exclude": [
|
|
15
|
-
"node_modules",
|
|
16
|
-
"**/*.spec.ts"
|
|
17
|
-
],
|
|
18
|
-
"include": [
|
|
19
|
-
"src/**/*"
|
|
20
|
-
]
|
|
21
|
-
}
|