@zenweb/schedule 2.3.0 → 2.3.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zenweb/schedule",
3
- "version": "2.3.0",
3
+ "version": "2.3.1",
4
4
  "description": "Zenweb Schedule module",
5
5
  "exports": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
package/src/index.ts CHANGED
@@ -25,3 +25,9 @@ export function setup(option?: ScheduleOption): SetupFunction {
25
25
  }
26
26
  }
27
27
  }
28
+
29
+ declare module '@zenweb/core' {
30
+ interface Core {
31
+ schedule: ScheduleRegister;
32
+ }
33
+ }
package/dist/index.d.ts DELETED
@@ -1,4 +0,0 @@
1
- import { SetupFunction } from '@zenweb/core';
2
- import { ScheduleOption } from './types';
3
- export * from './types';
4
- export declare function setup(option?: ScheduleOption): SetupFunction;
package/dist/index.js DELETED
@@ -1,39 +0,0 @@
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
package/dist/index.js.map DELETED
@@ -1 +0,0 @@
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"}
@@ -1,14 +0,0 @@
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
- }
package/dist/register.js DELETED
@@ -1,71 +0,0 @@
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
@@ -1 +0,0 @@
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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,OAAO,QAAQ,CAAC,WAAW,CAAC,IAAI,EAAE,IAAI,EAAE,QAAQ,CAAC,CAAC;IACpD,CAAC;CACF;AA3DD,4CA2DC"}
package/dist/types.d.ts DELETED
@@ -1,8 +0,0 @@
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 DELETED
@@ -1,3 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- //# sourceMappingURL=types.js.map
package/dist/types.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
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
- }