@zenweb/schedule 1.0.0 → 2.3.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.
@@ -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 declare 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,39 @@
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
+ exports.setup = void 0;
14
+ const globby = require("globby");
15
+ const path = require("path");
16
+ const register_1 = require("./register");
17
+ __exportStar(require("./types"), exports);
18
+ function setup(option) {
19
+ return async function schedule(setup) {
20
+ option = Object.assign({
21
+ paths: [path.join(process.cwd(), 'app', 'schedule')],
22
+ }, option);
23
+ setup.debug('option: %o', option);
24
+ const register = new register_1.ScheduleRegister(setup.core, option);
25
+ setup.defineCoreProperty('schedule', { value: register });
26
+ if (option.paths && option.paths.length) {
27
+ for (const d of option.paths) {
28
+ let count = 0;
29
+ for (const m of await globby(d, { cwd: d, absolute: true })) {
30
+ require(m);
31
+ count++;
32
+ }
33
+ setup.debug('load: %s %o files', d, count);
34
+ }
35
+ }
36
+ };
37
+ }
38
+ exports.setup = setup;
39
+ //# 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,SAAgB,KAAK,CAAC,MAAuB;IAC3C,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,sBAmBC"}
@@ -0,0 +1,14 @@
1
+ /// <reference types="koa__router" />
2
+ import '@zenweb/router';
3
+ import schedule = require('node-schedule');
4
+ import { Core } from '@zenweb/core';
5
+ import { RequestCallback, ScheduleOption } from './types';
6
+ import { Router } from '@zenweb/router';
7
+ export declare class ScheduleRegister {
8
+ option: ScheduleOption;
9
+ router: Router;
10
+ callback: RequestCallback;
11
+ private _index;
12
+ constructor(core: Core, option?: ScheduleOption);
13
+ job(...args: any[]): schedule.Job;
14
+ }
@@ -0,0 +1,71 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ScheduleRegister = void 0;
4
+ require("@zenweb/router");
5
+ const schedule = require("node-schedule");
6
+ const http_1 = require("http");
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(...args) {
25
+ let name = '';
26
+ let rule;
27
+ if (typeof args[1] === 'string') {
28
+ name = args[0];
29
+ rule = args[1];
30
+ args = args.slice(2);
31
+ }
32
+ else {
33
+ rule = args[0];
34
+ args = args.slice(1);
35
+ }
36
+ // 注册到路由
37
+ const path = `/___schedule/job/${this._index++}/${name}`;
38
+ this.router.post(path, safeCheck, ...args);
39
+ // 注册到 scheduleJob
40
+ const callback = () => {
41
+ const request = Object.assign({
42
+ headers: {
43
+ host: '127.0.0.1',
44
+ },
45
+ query: {},
46
+ querystring: '',
47
+ host: '127.0.0.1',
48
+ hostname: '127.0.0.1',
49
+ protocol: 'http',
50
+ secure: 'false',
51
+ method: 'POST',
52
+ url: path,
53
+ path: path,
54
+ socket: {
55
+ remoteAddress: SAFE_IP,
56
+ remotePort: 7001,
57
+ },
58
+ });
59
+ const response = new http_1.ServerResponse(request);
60
+ if (this.option.jobCallback) {
61
+ this.option.jobCallback(request, response, this.callback);
62
+ }
63
+ else {
64
+ this.callback(request, response);
65
+ }
66
+ };
67
+ return schedule.scheduleJob(name, rule, callback);
68
+ }
69
+ }
70
+ exports.ScheduleRegister = ScheduleRegister;
71
+ //# sourceMappingURL=register.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"register.js","sourceRoot":"","sources":["../src/register.ts"],"names":[],"mappings":";;;AAAA,0BAAwB;AACxB,0CAA2C;AAC3C,+BAAuD;AAMvD,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,CAAC,GAAG,IAAW;QAChB,IAAI,IAAI,GAAG,EAAE,CAAC;QACd,IAAI,IAAI,CAAC;QAET,IAAI,OAAO,IAAI,CAAC,CAAC,CAAC,KAAK,QAAQ,EAAE;YAC/B,IAAI,GAAG,IAAI,CAAC,CAAC,CAAC,CAAC;YACf,IAAI,GAAG,IAAI,CAAC,CAAC,CAAC,CAAC;YACf,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAA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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,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
package/package.json CHANGED
@@ -1,10 +1,14 @@
1
1
  {
2
2
  "name": "@zenweb/schedule",
3
- "version": "1.0.0",
3
+ "version": "2.3.3",
4
4
  "description": "Zenweb Schedule module",
5
- "main": "index.js",
5
+ "exports": "./dist/index.js",
6
+ "types": "./dist/index.d.ts",
7
+ "files": [
8
+ "dist"
9
+ ],
6
10
  "scripts": {
7
- "test": "echo \"Error: no test specified\" && exit 1"
11
+ "prepublishOnly": "rm -fr dist && tsc"
8
12
  },
9
13
  "author": {
10
14
  "name": "YeFei",
@@ -28,13 +32,13 @@
28
32
  "url": "https://github.com/yefei/zenweb-schedule/issues"
29
33
  },
30
34
  "devDependencies": {
31
- "eslint": "^7.16.0",
32
- "koa": "^2.13.1",
33
- "zenweb": "^1.5.1"
35
+ "@zenweb/core": "^2.3.2",
36
+ "@zenweb/router": "^2.3.0",
37
+ "zenweb": "^2.3.1"
34
38
  },
35
39
  "dependencies": {
36
- "@feiye/discover": "^1.0.0",
37
- "debug": "^4.3.1",
40
+ "@types/node-schedule": "^1.3.2",
41
+ "globby": "11.0.4",
38
42
  "node-schedule": "^2.0.0"
39
43
  }
40
44
  }
package/.eslintrc DELETED
@@ -1,14 +0,0 @@
1
- {
2
- "extends": "eslint:recommended",
3
- "env": {
4
- "node": true,
5
- "es6": true
6
- },
7
- "parserOptions": {
8
- "ecmaVersion": 8
9
- },
10
- "rules": {
11
- "no-unused-vars": ["warn", { "vars": "local", "args": "none" }],
12
- "semi": "warn"
13
- }
14
- }
@@ -1,8 +0,0 @@
1
- 'use strict';
2
-
3
- const app = require('../../app');
4
-
5
- app.schedule.job('*/1 * * * * *', ctx => {
6
- console.log('task echo');
7
- ctx.body = 'ok';
8
- });
package/example/app.js DELETED
@@ -1,11 +0,0 @@
1
- 'use strict';
2
-
3
- process.env.DEBUG = '*';
4
-
5
- const app = module.exports = require('zenweb').create();
6
- app.setup(require('..').setup, {
7
- jobCallback(request, response, callback) {
8
- callback(request, response);
9
- }
10
- });
11
- app.start();
package/index.d.ts DELETED
@@ -1,30 +0,0 @@
1
- import { Middleware } from 'koa';
2
- import { Core } from '@zenweb/core';
3
- import { ServerResponse, IncomingMessage } from 'http';
4
- import { RecurrenceRule, RecurrenceSpecDateRange, RecurrenceSpecObjLit, Job } from 'node-schedule';
5
-
6
- declare class ScheduleRegister<StateT = any, CustomT = {}> {
7
- job(
8
- rule: RecurrenceRule | RecurrenceSpecDateRange | RecurrenceSpecObjLit | Date | string | number,
9
- ...middleware: Middleware<StateT, CustomT>[]
10
- ): Job;
11
-
12
- job(
13
- name: string,
14
- rule: RecurrenceRule | RecurrenceSpecDateRange | RecurrenceSpecObjLit | Date | string | number,
15
- ...middleware: Middleware<StateT, CustomT>[]
16
- ): Job;
17
- }
18
-
19
- export interface ScheduleOptions {
20
- paths?: string[];
21
- jobCallback?: (request: IncomingMessage, response: ServerResponse, callback: (request: IncomingMessage, response: ServerResponse) => void) => void;
22
- }
23
-
24
- export declare function setup(core: Core, options?: ScheduleOptions): void;
25
-
26
- declare module '@zenweb/core' {
27
- interface Core {
28
- schedule: ScheduleRegister;
29
- }
30
- }
package/index.js DELETED
@@ -1,29 +0,0 @@
1
- 'use strict';
2
-
3
- const path = require('path');
4
- const { discover } = require('@feiye/discover');
5
- const debug = require('./lib/debug');
6
- const { ScheduleRegister } = require('./lib/register');
7
-
8
- /**
9
- * @param {import('@zenweb/core').Core} core
10
- * @param {*} [options]
11
- */
12
- function setup(core, options) {
13
- options = Object.assign({
14
- paths: [path.join(process.cwd(), 'app', 'schedule')],
15
- }, options);
16
- debug('options: %o', options);
17
- const register = new ScheduleRegister(core, options);
18
- Object.defineProperty(core, 'schedule', { value: register });
19
- if (options.paths && options.paths.length) {
20
- options.paths.forEach(path => {
21
- const count = discover(path);
22
- debug('load: %s %o files', path, count);
23
- });
24
- }
25
- }
26
-
27
- module.exports = {
28
- setup,
29
- };
package/lib/debug.js DELETED
@@ -1,3 +0,0 @@
1
- 'use strict';
2
-
3
- module.exports = require('debug')('zenweb:schedule');
package/lib/register.js DELETED
@@ -1,86 +0,0 @@
1
- 'use strict';
2
-
3
- const schedule = require('node-schedule');
4
- const { ServerResponse } = require('http');
5
-
6
- const SAFE_IP = '127.0.0.1';
7
-
8
- /**
9
- * 安全检查,防止外部调用
10
- * @param {import('koa').Context} ctx
11
- * @param {import('koa').Middleware} next
12
- */
13
- function safeCheck(ctx, next) {
14
- if (!ctx.req.socket.remoteAddress || !ctx.req.socket.remoteAddress.endsWith(SAFE_IP)) {
15
- ctx.throw(403);
16
- }
17
- return next();
18
- }
19
-
20
- class ScheduleRegister {
21
- /**
22
- * @param {import('@zenweb/core').Core} core
23
- * @param {object} [options]
24
- * @param {(req, res, callback: (req, res) => void) => void} [options.jobCallback] 自定义任务调用
25
- */
26
- constructor(core, options) {
27
- this.options = options || {};
28
- this.router = core.router;
29
- this.callback = core.koa.callback();
30
- this._index = 0;
31
- }
32
-
33
- job(...args) {
34
- let name = '';
35
- let rule;
36
-
37
- if (typeof args[1] === 'string') {
38
- name = args[0];
39
- rule = args[1];
40
- args = args.slice(2);
41
- } else {
42
- rule = args[0];
43
- args = args.slice(1);
44
- }
45
-
46
- // 注册到路由
47
- const path = `/___schedule/job/${this._index++}/${name}`;
48
- this.router.post(path, safeCheck, ...args);
49
-
50
- // 注册到 scheduleJob
51
- const jobArgs = [];
52
- if (name) jobArgs.push(name);
53
- jobArgs.push(rule);
54
- jobArgs.push(() => {
55
- const request = {
56
- headers: {
57
- host: '127.0.0.1',
58
- },
59
- query: {},
60
- querystring: '',
61
- host: '127.0.0.1',
62
- hostname: '127.0.0.1',
63
- protocol: 'http',
64
- secure: 'false',
65
- method: 'POST',
66
- url: path,
67
- path: path,
68
- socket: {
69
- remoteAddress: SAFE_IP,
70
- remotePort: 7001,
71
- },
72
- };
73
- const response = new ServerResponse(request);
74
- if (this.options.jobCallback) {
75
- this.options.jobCallback(request, response, this.callback);
76
- } else {
77
- this.callback(request, response);
78
- }
79
- });
80
- return schedule.scheduleJob(...jobArgs);
81
- }
82
- }
83
-
84
- module.exports = {
85
- ScheduleRegister,
86
- };