Skip to content
This repository was archived by the owner on Oct 8, 2023. It is now read-only.

Commit 52bf2f0

Browse files
committed
change stuff
1 parent 7f776b6 commit 52bf2f0

21 files changed

+160
-65
lines changed

src/background-scheduled-task/index.mjs

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
const EventEmitter = require('events');
2-
const path = require('path');
3-
const { fork } = require('child_process');
4-
const uuid = require('uuid');
1+
import EventEmitter from 'events';
2+
import path from 'path';
3+
import { fork } from 'child_process';
4+
import uuid from '../uuid/v4.mjs'
55

6-
const daemonPath = `${__dirname}/daemon.js`;
6+
const daemonPath = `./daemon.js`;
77

88
class BackgroundScheduledTask extends EventEmitter {
99
constructor(cronExpression, taskPath, options){
@@ -17,7 +17,7 @@ class BackgroundScheduledTask extends EventEmitter {
1717
this.cronExpression = cronExpression;
1818
this.taskPath = taskPath;
1919
this.options = options;
20-
this.options.name = this.options.name || uuid.v4();
20+
this.options.name = this.options.name || uuid();
2121

2222
if(options.scheduled){
2323
this.start();
@@ -64,4 +64,4 @@ class BackgroundScheduledTask extends EventEmitter {
6464
}
6565
}
6666

67-
module.exports = BackgroundScheduledTask;
67+
export default BackgroundScheduledTask;

src/convert-expression/asterisk-to-range-conversion.mjs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
'use strict';
2-
module.exports = (() => {
1+
export default (() => {
32
function convertAsterisk(expression, replecement){
43
if(expression.indexOf('*') !== -1){
54
return expression.replace('*', replecement);

src/convert-expression/index.mjs

+6-8
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
'use strict';
1+
import monthNamesConversion from './month-names-conversion.mjs'
2+
import weekDayNamesConversion from './week-day-names-conversion.mjs';
3+
import convertAsterisksToRanges from './asterisk-to-range-conversion.mjs'
4+
import convertRanges from './range-conversion.mjs'
5+
import convertSteps from './step-values-conversion.mjs'
26

3-
const monthNamesConversion = require('./month-names-conversion');
4-
const weekDayNamesConversion = require('./week-day-names-conversion');
5-
const convertAsterisksToRanges = require('./asterisk-to-range-conversion');
6-
const convertRanges = require('./range-conversion');
7-
const convertSteps = require('./step-values-conversion');
8-
9-
module.exports = (() => {
7+
export default (() => {
108

119
function appendSeccondExpression(expressions){
1210
if(expressions.length === 5){

src/convert-expression/month-names-conversion.mjs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
'use strict';
2-
module.exports = (() => {
1+
export default (() => {
32
const months = ['january','february','march','april','may','june','july',
43
'august','september','october','november','december'];
54
const shortMonths = ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug',

src/convert-expression/range-conversion.mjs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
'use strict';
2-
module.exports = ( () => {
1+
export default ( () => {
32
function replaceWithRange(expression, text, init, end) {
43

54
const numbers = [];

src/convert-expression/step-values-conversion.mjs

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
'use strict';
2-
3-
module.exports = (() => {
1+
export default (() => {
42
function convertSteps(expressions){
53
var stepValuePattern = /^(.+)\/(\w+)$/;
64
for(var i = 0; i < expressions.length; i++){

src/convert-expression/week-day-names-conversion.mjs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
'use strict';
2-
module.exports = (() => {
1+
export default (() => {
32
const weekDays = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday',
43
'friday', 'saturday'];
54
const shortWeekDays = ['sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat'];

src/node-cron.mjs

+8-12
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
'use strict';
2-
3-
const ScheduledTask = require('./scheduled-task');
4-
const BackgroundScheduledTask = require('./background-scheduled-task');
5-
const validation = require('./pattern-validation');
6-
const storage = require('./storage');
1+
import ScheduledTask from './scheduled-task.mjs'
2+
import BackgroundScheduledTask from './background-scheduled-task/index.mjs'
3+
import validation from './pattern-validation.mjs'
4+
import storage from './storage.mjs'
75

86
/**
97
* @typedef {Object} CronScheduleOptions
@@ -21,7 +19,7 @@ const storage = require('./storage');
2119
* @param {CronScheduleOptions} [options] A set of options for the scheduled task.
2220
* @returns {ScheduledTask} The scheduled task.
2321
*/
24-
function schedule(expression, func, options) {
22+
export const schedule = function (expression, func, options) {
2523
const task = createTask(expression, func, options);
2624

2725
storage.save(task);
@@ -42,7 +40,7 @@ function createTask(expression, func, options) {
4240
* @param {string} expression The cron expression.
4341
* @returns {boolean} Whether the expression is valid or not.
4442
*/
45-
function validate(expression) {
43+
export const validate = function validate(expression) {
4644
try {
4745
validation(expression);
4846

@@ -57,8 +55,6 @@ function validate(expression) {
5755
*
5856
* @returns {ScheduledTask[]} The scheduled tasks.
5957
*/
60-
function getTasks() {
58+
export const getTasks = function getTasks() {
6159
return storage.getTasks();
62-
}
63-
64-
module.exports = { schedule, validate, getTasks };
60+
}

src/pattern-validation.mjs

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
'use strict';
2-
3-
const convertExpression = require('./convert-expression');
1+
import convertExpression from './convert-expression/index.mjs';
42

53
const validationRegex = /^(?:\d+|\*|\*\/\d+)$/;
64

@@ -121,4 +119,4 @@ function validate(pattern) {
121119
validateFields(patterns, executablePatterns);
122120
}
123121

124-
module.exports = validate;
122+
export default validate

src/scheduled-task.mjs

+6-8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
'use strict';
2-
3-
const EventEmitter = require('events');
4-
const Task = require('./task');
5-
const Scheduler = require('./scheduler');
6-
const uuid = require('uuid');
1+
import EventEmitter from 'events';
2+
import Task from './task.mjs'
3+
import Scheduler from './scheduler.mjs'
4+
import uuid from './uuid/v4.mjs'
75

86
class ScheduledTask extends EventEmitter {
97
constructor(cronExpression, func, options) {
@@ -16,7 +14,7 @@ class ScheduledTask extends EventEmitter {
1614
}
1715

1816
this.options = options;
19-
this.options.name = this.options.name || uuid.v4();
17+
this.options.name = this.options.name || uuid();
2018

2119
this._task = new Task(func);
2220
this._scheduler = new Scheduler(cronExpression, options.timezone, options.recoverMissedExecutions);
@@ -48,4 +46,4 @@ class ScheduledTask extends EventEmitter {
4846
}
4947
}
5048

51-
module.exports = ScheduledTask;
49+
export default ScheduledTask;

src/scheduler.mjs

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
'use strict';
2-
3-
const EventEmitter = require('events');
4-
const TimeMatcher = require('./time-matcher');
1+
import EventEmitter from 'events';
2+
import TimeMatcher from './time-matcher.mjs';
53

64
class Scheduler extends EventEmitter{
75
constructor(pattern, timezone, autorecover){
@@ -46,4 +44,4 @@ class Scheduler extends EventEmitter{
4644
}
4745
}
4846

49-
module.exports = Scheduler;
47+
export default Scheduler

src/storage.mjs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
module.exports = (() => {
1+
import uuid from './uuid/v4.mjs'
2+
export default (() => {
23
if(!global.scheduledTasks){
34
global.scheduledTasks = new Map();
45
}
56

67
return {
78
save: (task) => {
89
if(!task.options){
9-
const uuid = require('uuid');
1010
task.options = {};
11-
task.options.name = uuid.v4();
11+
task.options.name = uuid();
1212
}
1313
global.scheduledTasks.set(task.options.name, task);
1414
},

src/task.mjs

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
'use strict';
2-
3-
const EventEmitter = require('events');
1+
import EventEmitter from 'events';
42

53
class Task extends EventEmitter{
64
constructor(execution){
@@ -30,5 +28,5 @@ class Task extends EventEmitter{
3028
}
3129
}
3230

33-
module.exports = Task;
31+
export default Task
3432

src/time-matcher.mjs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const validatePattern = require('./pattern-validation');
2-
const convertExpression = require('./convert-expression');
1+
import validatePattern from './pattern-validation.mjs'
2+
import convertExpression from './convert-expression/index.mjs'
33

44
function matchPattern(pattern, value){
55
if( pattern.indexOf(',') !== -1 ){
@@ -51,4 +51,4 @@ class TimeMatcher{
5151
}
5252
}
5353

54-
module.exports = TimeMatcher;
54+
export default TimeMatcher

src/uuid/native.mjs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import crypto from 'crypto';
2+
3+
export default { randomUUID: crypto.randomUUID };

src/uuid/regex.mjs

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default /^(?:[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}|00000000-0000-0000-0000-000000000000)$/i;

src/uuid/rng.mjs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import crypto from 'crypto';
2+
3+
const rnds8Pool = new Uint8Array(256); // # of random values to pre-allocate
4+
let poolPtr = rnds8Pool.length;
5+
6+
export default function rng() {
7+
if (poolPtr > rnds8Pool.length - 16) {
8+
crypto.randomFillSync(rnds8Pool);
9+
poolPtr = 0;
10+
}
11+
return rnds8Pool.slice(poolPtr, (poolPtr += 16));
12+
}

src/uuid/stringify.mjs

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import validate from './validate.mjs';
2+
3+
/**
4+
* Convert array of 16 byte values to UUID string format of the form:
5+
* XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
6+
*/
7+
const byteToHex = [];
8+
9+
for (let i = 0; i < 256; ++i) {
10+
byteToHex.push((i + 0x100).toString(16).slice(1));
11+
}
12+
13+
export function unsafeStringify(arr, offset = 0) {
14+
// Note: Be careful editing this code! It's been tuned for performance
15+
// and works in ways you may not expect. See https://github.com/uuidjs/uuid/pull/434
16+
return (
17+
byteToHex[arr[offset + 0]] +
18+
byteToHex[arr[offset + 1]] +
19+
byteToHex[arr[offset + 2]] +
20+
byteToHex[arr[offset + 3]] +
21+
'-' +
22+
byteToHex[arr[offset + 4]] +
23+
byteToHex[arr[offset + 5]] +
24+
'-' +
25+
byteToHex[arr[offset + 6]] +
26+
byteToHex[arr[offset + 7]] +
27+
'-' +
28+
byteToHex[arr[offset + 8]] +
29+
byteToHex[arr[offset + 9]] +
30+
'-' +
31+
byteToHex[arr[offset + 10]] +
32+
byteToHex[arr[offset + 11]] +
33+
byteToHex[arr[offset + 12]] +
34+
byteToHex[arr[offset + 13]] +
35+
byteToHex[arr[offset + 14]] +
36+
byteToHex[arr[offset + 15]]
37+
).toLowerCase();
38+
}
39+
40+
function stringify(arr, offset = 0) {
41+
const uuid = unsafeStringify(arr, offset);
42+
// Consistency check for valid UUID. If this throws, it's likely due to one
43+
// of the following:
44+
// - One or more input array values don't map to a hex octet (leading to
45+
// "undefined" in the uuid)
46+
// - Invalid input values for the RFC `version` or `variant` fields
47+
if (!validate(uuid)) {
48+
throw TypeError('Stringified UUID is invalid');
49+
}
50+
51+
return uuid;
52+
}
53+
54+
export default stringify;

src/uuid/v4.mjs

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import native from './native.mjs';
2+
import rng from './rng.mjs';
3+
import { unsafeStringify } from './stringify.mjs';
4+
5+
function v4(options, buf, offset) {
6+
if (native.randomUUID && !buf && !options) {
7+
return native.randomUUID();
8+
}
9+
10+
options = options || {};
11+
12+
const rnds = options.random || (options.rng || rng)();
13+
14+
// Per 4.4, set bits for version and `clock_seq_hi_and_reserved`
15+
rnds[6] = (rnds[6] & 0x0f) | 0x40;
16+
rnds[8] = (rnds[8] & 0x3f) | 0x80;
17+
18+
// Copy bytes to buffer, if provided
19+
if (buf) {
20+
offset = offset || 0;
21+
22+
for (let i = 0; i < 16; ++i) {
23+
buf[offset + i] = rnds[i];
24+
}
25+
26+
return buf;
27+
}
28+
29+
return unsafeStringify(rnds);
30+
}
31+
32+
export default v4;

src/uuid/validate.mjs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import REGEX from './regex.mjs';
2+
3+
function validate(uuid) {
4+
return typeof uuid === 'string' && REGEX.test(uuid);
5+
}
6+
7+
export default validate;

testv2/test.mjs

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import {schedule, getTasks, validate} from '../src/node-cron.mjs'
2+
3+
schedule("* * * * *", ()=>{
4+
console.log("eeee")
5+
console.log(new Date().getMinutes())
6+
})

0 commit comments

Comments
 (0)