@zenweb/schedule 2.3.0 → 2.3.4

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 CHANGED
@@ -1,4 +1,10 @@
1
1
  import { SetupFunction } from '@zenweb/core';
2
+ import { ScheduleRegister } from './register';
2
3
  import { ScheduleOption } from './types';
3
4
  export * from './types';
4
- export declare function setup(option?: ScheduleOption): SetupFunction;
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 CHANGED
@@ -10,7 +10,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
10
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
11
  };
12
12
  Object.defineProperty(exports, "__esModule", { value: true });
13
- exports.setup = void 0;
14
13
  const globby = require("globby");
15
14
  const path = require("path");
16
15
  const register_1 = require("./register");
@@ -35,5 +34,5 @@ function setup(option) {
35
34
  }
36
35
  };
37
36
  }
38
- exports.setup = setup;
37
+ exports.default = setup;
39
38
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +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,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AC,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"}
package/package.json CHANGED
@@ -1,9 +1,12 @@
1
1
  {
2
2
  "name": "@zenweb/schedule",
3
- "version": "2.3.0",
3
+ "version": "2.3.4",
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": "^2.0.0",
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
  }
@@ -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/src/index.ts DELETED
@@ -1,27 +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
- }
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
- }