semola 0.5.0 → 0.5.2
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/README.md +75 -20
- package/dist/api/core/index.cjs +206 -0
- package/dist/api/core/index.d.cts +21 -0
- package/dist/api/core/index.d.cts.map +1 -0
- package/dist/api/core/index.d.mts +21 -0
- package/dist/api/core/index.d.mts.map +1 -0
- package/dist/api/core/index.mjs +208 -0
- package/dist/api/core/index.mjs.map +1 -0
- package/dist/api/core/types.d.cts +107 -0
- package/dist/api/core/types.d.cts.map +1 -0
- package/dist/api/core/types.d.mts +107 -0
- package/dist/api/core/types.d.mts.map +1 -0
- package/dist/api/middleware/index.cjs +8 -0
- package/dist/api/middleware/index.d.cts +11 -0
- package/dist/api/middleware/index.d.cts.map +1 -0
- package/dist/api/middleware/index.d.mts +11 -0
- package/dist/api/middleware/index.d.mts.map +1 -0
- package/dist/api/middleware/index.mjs +10 -0
- package/dist/api/middleware/index.mjs.map +1 -0
- package/dist/api/middleware/types.d.cts +16 -0
- package/dist/api/middleware/types.d.cts.map +1 -0
- package/dist/api/middleware/types.d.mts +16 -0
- package/dist/api/middleware/types.d.mts.map +1 -0
- package/dist/api/openapi/index.cjs +254 -0
- package/dist/api/openapi/index.mjs +256 -0
- package/dist/api/openapi/index.mjs.map +1 -0
- package/dist/api/openapi/types.d.cts +60 -0
- package/dist/api/openapi/types.d.cts.map +1 -0
- package/dist/api/openapi/types.d.mts +60 -0
- package/dist/api/openapi/types.d.mts.map +1 -0
- package/dist/api/validation/index.cjs +64 -0
- package/dist/api/validation/index.mjs +61 -0
- package/dist/api/validation/index.mjs.map +1 -0
- package/dist/cache/types.d.cts +17 -0
- package/dist/cache/types.d.cts.map +1 -0
- package/dist/cache/types.d.mts +17 -0
- package/dist/cache/types.d.mts.map +1 -0
- package/dist/cron/scanner.cjs +237 -0
- package/dist/cron/scanner.mjs +238 -0
- package/dist/cron/scanner.mjs.map +1 -0
- package/dist/cron/types.d.cts +11 -0
- package/dist/cron/types.d.cts.map +1 -0
- package/dist/cron/types.d.mts +11 -0
- package/dist/cron/types.d.mts.map +1 -0
- package/dist/errors/types.d.cts +5 -0
- package/dist/errors/types.d.cts.map +1 -0
- package/dist/errors/types.d.mts +5 -0
- package/dist/errors/types.d.mts.map +1 -0
- package/dist/i18n/types.d.cts +13 -0
- package/dist/i18n/types.d.cts.map +1 -0
- package/dist/i18n/types.d.mts +13 -0
- package/dist/i18n/types.d.mts.map +1 -0
- package/dist/lib/api/index.cjs +5 -0
- package/dist/lib/api/index.d.cts +5 -0
- package/dist/lib/api/index.d.mts +5 -0
- package/dist/lib/api/index.mjs +3 -0
- package/dist/lib/cache/index.cjs +74 -0
- package/dist/lib/cache/index.d.cts +36 -0
- package/dist/lib/cache/index.d.cts.map +1 -0
- package/dist/lib/cache/index.d.mts +36 -0
- package/dist/lib/cache/index.d.mts.map +1 -0
- package/dist/lib/cache/index.mjs +75 -0
- package/dist/lib/cache/index.mjs.map +1 -0
- package/dist/lib/cron/index.cjs +276 -0
- package/dist/lib/cron/index.d.cts +39 -0
- package/dist/lib/cron/index.d.cts.map +1 -0
- package/dist/lib/cron/index.d.mts +39 -0
- package/dist/lib/cron/index.d.mts.map +1 -0
- package/dist/lib/cron/index.mjs +277 -0
- package/dist/lib/cron/index.mjs.map +1 -0
- package/dist/lib/errors/index.cjs +30 -0
- package/dist/lib/errors/index.d.cts +13 -0
- package/dist/lib/errors/index.d.cts.map +1 -0
- package/dist/lib/errors/index.d.mts +13 -0
- package/dist/lib/errors/index.d.mts.map +1 -0
- package/dist/lib/errors/index.mjs +28 -0
- package/dist/lib/errors/index.mjs.map +1 -0
- package/dist/lib/i18n/index.cjs +37 -0
- package/dist/lib/i18n/index.d.cts +20 -0
- package/dist/lib/i18n/index.d.cts.map +1 -0
- package/dist/lib/i18n/index.d.mts +20 -0
- package/dist/lib/i18n/index.d.mts.map +1 -0
- package/dist/lib/i18n/index.mjs +38 -0
- package/dist/lib/i18n/index.mjs.map +1 -0
- package/dist/lib/policy/index.cjs +99 -0
- package/dist/lib/policy/index.d.cts +21 -0
- package/dist/lib/policy/index.d.cts.map +1 -0
- package/dist/lib/policy/index.d.mts +21 -0
- package/dist/lib/policy/index.d.mts.map +1 -0
- package/dist/lib/policy/index.mjs +81 -0
- package/dist/lib/policy/index.mjs.map +1 -0
- package/dist/lib/prompts/index.cjs +409 -0
- package/dist/lib/prompts/index.d.cts +31 -0
- package/dist/lib/prompts/index.d.cts.map +1 -0
- package/dist/lib/prompts/index.d.mts +31 -0
- package/dist/lib/prompts/index.d.mts.map +1 -0
- package/dist/lib/prompts/index.mjs +405 -0
- package/dist/lib/prompts/index.mjs.map +1 -0
- package/dist/lib/pubsub/index.cjs +48 -0
- package/dist/lib/pubsub/index.d.cts +27 -0
- package/dist/lib/pubsub/index.d.cts.map +1 -0
- package/dist/lib/pubsub/index.d.mts +27 -0
- package/dist/lib/pubsub/index.d.mts.map +1 -0
- package/dist/lib/pubsub/index.mjs +49 -0
- package/dist/lib/pubsub/index.mjs.map +1 -0
- package/dist/lib/queue/index.cjs +182 -0
- package/dist/lib/queue/index.d.cts +32 -0
- package/dist/lib/queue/index.d.cts.map +1 -0
- package/dist/lib/queue/index.d.mts +32 -0
- package/dist/lib/queue/index.d.mts.map +1 -0
- package/dist/lib/queue/index.mjs +183 -0
- package/dist/lib/queue/index.mjs.map +1 -0
- package/dist/node_modules/@standard-schema/spec/dist/index.d.cts +80 -0
- package/dist/node_modules/@standard-schema/spec/dist/index.d.cts.map +1 -0
- package/dist/node_modules/@standard-schema/spec/dist/index.d.mts +80 -0
- package/dist/node_modules/@standard-schema/spec/dist/index.d.mts.map +1 -0
- package/dist/policy/helpers.cjs +206 -0
- package/dist/policy/helpers.d.cts +50 -0
- package/dist/policy/helpers.d.cts.map +1 -0
- package/dist/policy/helpers.d.mts +50 -0
- package/dist/policy/helpers.d.mts.map +1 -0
- package/dist/policy/helpers.mjs +190 -0
- package/dist/policy/helpers.mjs.map +1 -0
- package/dist/policy/types.d.cts +16 -0
- package/dist/policy/types.d.cts.map +1 -0
- package/dist/policy/types.d.mts +16 -0
- package/dist/policy/types.d.mts.map +1 -0
- package/dist/prompts/core/keys.cjs +165 -0
- package/dist/prompts/core/keys.mjs +167 -0
- package/dist/prompts/core/keys.mjs.map +1 -0
- package/dist/prompts/core/runtime.cjs +104 -0
- package/dist/prompts/core/runtime.mjs +106 -0
- package/dist/prompts/core/runtime.mjs.map +1 -0
- package/dist/prompts/core/session.cjs +98 -0
- package/dist/prompts/core/session.mjs +100 -0
- package/dist/prompts/core/session.mjs.map +1 -0
- package/dist/prompts/core/types.d.cts +21 -0
- package/dist/prompts/core/types.d.cts.map +1 -0
- package/dist/prompts/core/types.d.mts +21 -0
- package/dist/prompts/core/types.d.mts.map +1 -0
- package/dist/prompts/types.d.cts +52 -0
- package/dist/prompts/types.d.cts.map +1 -0
- package/dist/prompts/types.d.mts +52 -0
- package/dist/prompts/types.d.mts.map +1 -0
- package/dist/pubsub/types.d.cts +10 -0
- package/dist/pubsub/types.d.cts.map +1 -0
- package/dist/pubsub/types.d.mts +10 -0
- package/dist/pubsub/types.d.mts.map +1 -0
- package/dist/queue/types.d.cts +47 -0
- package/dist/queue/types.d.cts.map +1 -0
- package/dist/queue/types.d.mts +47 -0
- package/dist/queue/types.d.mts.map +1 -0
- package/package.json +86 -16
- package/dist/lib/api/core/index.d.ts +0 -15
- package/dist/lib/api/core/index.d.ts.map +0 -1
- package/dist/lib/api/core/index.js +0 -166
- package/dist/lib/api/core/index.js.map +0 -1
- package/dist/lib/api/core/index.test.d.ts +0 -2
- package/dist/lib/api/core/index.test.d.ts.map +0 -1
- package/dist/lib/api/core/index.test.js +0 -219
- package/dist/lib/api/core/index.test.js.map +0 -1
- package/dist/lib/api/core/types.d.ts +0 -102
- package/dist/lib/api/core/types.d.ts.map +0 -1
- package/dist/lib/api/core/types.js +0 -2
- package/dist/lib/api/core/types.js.map +0 -1
- package/dist/lib/api/index.d.ts +0 -5
- package/dist/lib/api/index.d.ts.map +0 -1
- package/dist/lib/api/index.js +0 -3
- package/dist/lib/api/index.js.map +0 -1
- package/dist/lib/api/middleware/index.d.ts +0 -7
- package/dist/lib/api/middleware/index.d.ts.map +0 -1
- package/dist/lib/api/middleware/index.js +0 -7
- package/dist/lib/api/middleware/index.js.map +0 -1
- package/dist/lib/api/middleware/index.test.d.ts +0 -2
- package/dist/lib/api/middleware/index.test.d.ts.map +0 -1
- package/dist/lib/api/middleware/index.test.js +0 -67
- package/dist/lib/api/middleware/index.test.js.map +0 -1
- package/dist/lib/api/middleware/types.d.ts +0 -12
- package/dist/lib/api/middleware/types.d.ts.map +0 -1
- package/dist/lib/api/middleware/types.js +0 -2
- package/dist/lib/api/middleware/types.js.map +0 -1
- package/dist/lib/api/openapi/index.d.ts +0 -31
- package/dist/lib/api/openapi/index.d.ts.map +0 -1
- package/dist/lib/api/openapi/index.js +0 -328
- package/dist/lib/api/openapi/index.js.map +0 -1
- package/dist/lib/api/openapi/index.test.d.ts +0 -2
- package/dist/lib/api/openapi/index.test.d.ts.map +0 -1
- package/dist/lib/api/openapi/index.test.js +0 -359
- package/dist/lib/api/openapi/index.test.js.map +0 -1
- package/dist/lib/api/openapi/types.d.ts +0 -57
- package/dist/lib/api/openapi/types.d.ts.map +0 -1
- package/dist/lib/api/openapi/types.js +0 -5
- package/dist/lib/api/openapi/types.js.map +0 -1
- package/dist/lib/api/validation/index.d.ts +0 -33
- package/dist/lib/api/validation/index.d.ts.map +0 -1
- package/dist/lib/api/validation/index.js +0 -81
- package/dist/lib/api/validation/index.js.map +0 -1
- package/dist/lib/api/validation/index.test.d.ts +0 -2
- package/dist/lib/api/validation/index.test.d.ts.map +0 -1
- package/dist/lib/api/validation/index.test.js +0 -135
- package/dist/lib/api/validation/index.test.js.map +0 -1
- package/dist/lib/cache/index.d.ts +0 -25
- package/dist/lib/cache/index.d.ts.map +0 -1
- package/dist/lib/cache/index.js +0 -62
- package/dist/lib/cache/index.js.map +0 -1
- package/dist/lib/cache/index.test.d.ts +0 -2
- package/dist/lib/cache/index.test.d.ts.map +0 -1
- package/dist/lib/cache/index.test.js +0 -314
- package/dist/lib/cache/index.test.js.map +0 -1
- package/dist/lib/cache/types.d.ts +0 -5
- package/dist/lib/cache/types.d.ts.map +0 -1
- package/dist/lib/cache/types.js +0 -2
- package/dist/lib/cache/types.js.map +0 -1
- package/dist/lib/errors/index.d.ts +0 -9
- package/dist/lib/errors/index.d.ts.map +0 -1
- package/dist/lib/errors/index.js +0 -25
- package/dist/lib/errors/index.js.map +0 -1
- package/dist/lib/errors/index.test.d.ts +0 -2
- package/dist/lib/errors/index.test.d.ts.map +0 -1
- package/dist/lib/errors/index.test.js +0 -197
- package/dist/lib/errors/index.test.js.map +0 -1
- package/dist/lib/errors/types.d.ts +0 -2
- package/dist/lib/errors/types.d.ts.map +0 -1
- package/dist/lib/errors/types.js +0 -2
- package/dist/lib/errors/types.js.map +0 -1
- package/dist/lib/i18n/index.d.ts +0 -18
- package/dist/lib/i18n/index.d.ts.map +0 -1
- package/dist/lib/i18n/index.js +0 -38
- package/dist/lib/i18n/index.js.map +0 -1
- package/dist/lib/i18n/index.test.d.ts +0 -2
- package/dist/lib/i18n/index.test.d.ts.map +0 -1
- package/dist/lib/i18n/index.test.js +0 -402
- package/dist/lib/i18n/index.test.js.map +0 -1
- package/dist/lib/i18n/types.d.ts +0 -15
- package/dist/lib/i18n/types.d.ts.map +0 -1
- package/dist/lib/i18n/types.js +0 -2
- package/dist/lib/i18n/types.js.map +0 -1
- package/dist/lib/policy/index.d.ts +0 -9
- package/dist/lib/policy/index.d.ts.map +0 -1
- package/dist/lib/policy/index.js +0 -51
- package/dist/lib/policy/index.js.map +0 -1
- package/dist/lib/policy/index.test.d.ts +0 -2
- package/dist/lib/policy/index.test.d.ts.map +0 -1
- package/dist/lib/policy/index.test.js +0 -328
- package/dist/lib/policy/index.test.js.map +0 -1
- package/dist/lib/policy/types.d.ts +0 -27
- package/dist/lib/policy/types.d.ts.map +0 -1
- package/dist/lib/policy/types.js +0 -2
- package/dist/lib/policy/types.js.map +0 -1
- package/dist/lib/pubsub/index.d.ts +0 -23
- package/dist/lib/pubsub/index.d.ts.map +0 -1
- package/dist/lib/pubsub/index.js +0 -55
- package/dist/lib/pubsub/index.js.map +0 -1
- package/dist/lib/pubsub/index.test.d.ts +0 -2
- package/dist/lib/pubsub/index.test.d.ts.map +0 -1
- package/dist/lib/pubsub/index.test.js +0 -550
- package/dist/lib/pubsub/index.test.js.map +0 -1
- package/dist/lib/pubsub/types.d.ts +0 -7
- package/dist/lib/pubsub/types.d.ts.map +0 -1
- package/dist/lib/pubsub/types.js +0 -2
- package/dist/lib/pubsub/types.js.map +0 -1
- package/dist/lib/queue/index.d.ts +0 -28
- package/dist/lib/queue/index.d.ts.map +0 -1
- package/dist/lib/queue/index.js +0 -211
- package/dist/lib/queue/index.js.map +0 -1
- package/dist/lib/queue/index.test.d.ts +0 -2
- package/dist/lib/queue/index.test.d.ts.map +0 -1
- package/dist/lib/queue/index.test.js +0 -740
- package/dist/lib/queue/index.test.js.map +0 -1
- package/dist/lib/queue/types.d.ts +0 -52
- package/dist/lib/queue/types.d.ts.map +0 -1
- package/dist/lib/queue/types.js +0 -2
- package/dist/lib/queue/types.js.map +0 -1
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_lib_errors_index = require("../errors/index.cjs");
|
|
3
|
+
//#region src/lib/cache/index.ts
|
|
4
|
+
var Cache = class {
|
|
5
|
+
options;
|
|
6
|
+
serialize;
|
|
7
|
+
deserialize;
|
|
8
|
+
constructor(options) {
|
|
9
|
+
this.options = options;
|
|
10
|
+
this.serialize = options.serializer ?? JSON.stringify;
|
|
11
|
+
this.deserialize = options.deserializer ?? ((raw) => JSON.parse(raw));
|
|
12
|
+
}
|
|
13
|
+
async get(key) {
|
|
14
|
+
if (!this.isEnabled) return this.fail("NotFoundError", `Key ${key} not found`);
|
|
15
|
+
const resolvedKey = this.resolveKey(key);
|
|
16
|
+
const [error, value] = await require_lib_errors_index.mightThrow(this.options.redis.get(resolvedKey));
|
|
17
|
+
if (error) return this.fail("CacheError", `Unable to get value for key ${key}`);
|
|
18
|
+
if (value === null || value === void 0) return this.fail("NotFoundError", `Key ${key} not found`);
|
|
19
|
+
const [deserializeErr, deserialized] = require_lib_errors_index.mightThrowSync(() => this.deserialize(value));
|
|
20
|
+
if (deserializeErr) return this.fail("CacheError", `Unable to deserialize value for key ${key}`);
|
|
21
|
+
return require_lib_errors_index.ok(deserialized);
|
|
22
|
+
}
|
|
23
|
+
async set(key, value) {
|
|
24
|
+
if (!this.isEnabled) return require_lib_errors_index.ok(value);
|
|
25
|
+
const [serializeErr, serialized] = require_lib_errors_index.mightThrowSync(() => this.serialize(value));
|
|
26
|
+
if (serializeErr) return this.fail("CacheError", `Unable to serialize value for key ${key}`);
|
|
27
|
+
if (serialized === null || serialized === void 0) return this.fail("CacheError", `Unable to serialize value for key ${key}`);
|
|
28
|
+
const [ttlErr, ttl] = require_lib_errors_index.mightThrowSync(() => this.resolveTTL(key, value));
|
|
29
|
+
if (ttlErr) return this.fail("InvalidTTLError", `Unable to resolve ttl for key ${key}`);
|
|
30
|
+
if (!this.isTTLValid(ttl)) return this.fail("InvalidTTLError", `Unable to save records with ttl equal to ${ttl}`);
|
|
31
|
+
const resolvedKey = this.resolveKey(key);
|
|
32
|
+
const [setError] = await require_lib_errors_index.mightThrow(this.getSetPromise(resolvedKey, serialized, ttl));
|
|
33
|
+
if (setError) return this.fail("CacheError", `Unable to set value for key ${key}`);
|
|
34
|
+
return require_lib_errors_index.ok(value);
|
|
35
|
+
}
|
|
36
|
+
async delete(key) {
|
|
37
|
+
if (!this.isEnabled) return require_lib_errors_index.ok(0);
|
|
38
|
+
const resolvedKey = this.resolveKey(key);
|
|
39
|
+
const [error, data] = await require_lib_errors_index.mightThrow(this.options.redis.del(resolvedKey));
|
|
40
|
+
if (error) return this.fail("CacheError", `Unable to delete key ${key}`);
|
|
41
|
+
return require_lib_errors_index.ok(data);
|
|
42
|
+
}
|
|
43
|
+
fail(type, message) {
|
|
44
|
+
this.options.onError?.({
|
|
45
|
+
type,
|
|
46
|
+
message
|
|
47
|
+
});
|
|
48
|
+
return require_lib_errors_index.err(type, message);
|
|
49
|
+
}
|
|
50
|
+
get isEnabled() {
|
|
51
|
+
return this.options.enabled !== false;
|
|
52
|
+
}
|
|
53
|
+
resolveKey(key) {
|
|
54
|
+
if (this.options.prefix) return `${this.options.prefix}:${key}`;
|
|
55
|
+
return key;
|
|
56
|
+
}
|
|
57
|
+
resolveTTL(key, value) {
|
|
58
|
+
if (typeof this.options.ttl === "function") return this.options.ttl(key, value);
|
|
59
|
+
return this.options.ttl;
|
|
60
|
+
}
|
|
61
|
+
getSetPromise(key, value, ttl) {
|
|
62
|
+
if (ttl === void 0 || ttl === null) return this.options.redis.set(key, value);
|
|
63
|
+
return this.options.redis.set(key, value, "PX", ttl);
|
|
64
|
+
}
|
|
65
|
+
isTTLValid(ttl) {
|
|
66
|
+
if (ttl === void 0 || ttl === null) return true;
|
|
67
|
+
if (!Number.isFinite(ttl)) return false;
|
|
68
|
+
if (!Number.isInteger(ttl)) return false;
|
|
69
|
+
if (ttl < 0) return false;
|
|
70
|
+
return true;
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
//#endregion
|
|
74
|
+
exports.Cache = Cache;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { CacheOptions } from "../../cache/types.cjs";
|
|
2
|
+
|
|
3
|
+
//#region src/lib/cache/index.d.ts
|
|
4
|
+
declare class Cache<T> {
|
|
5
|
+
private options;
|
|
6
|
+
private serialize;
|
|
7
|
+
private deserialize;
|
|
8
|
+
constructor(options: CacheOptions<T>);
|
|
9
|
+
get(key: string): Promise<readonly [{
|
|
10
|
+
readonly type: "NotFoundError";
|
|
11
|
+
readonly message: string;
|
|
12
|
+
}, null] | readonly [{
|
|
13
|
+
readonly type: "CacheError";
|
|
14
|
+
readonly message: string;
|
|
15
|
+
}, null] | readonly [null, T | null]>;
|
|
16
|
+
set(key: string, value: T): Promise<readonly [{
|
|
17
|
+
readonly type: "CacheError";
|
|
18
|
+
readonly message: string;
|
|
19
|
+
}, null] | readonly [null, T] | readonly [{
|
|
20
|
+
readonly type: "InvalidTTLError";
|
|
21
|
+
readonly message: string;
|
|
22
|
+
}, null]>;
|
|
23
|
+
delete(key: string): Promise<readonly [{
|
|
24
|
+
readonly type: "CacheError";
|
|
25
|
+
readonly message: string;
|
|
26
|
+
}, null] | readonly [null, number | null]>;
|
|
27
|
+
private fail;
|
|
28
|
+
private get isEnabled();
|
|
29
|
+
private resolveKey;
|
|
30
|
+
private resolveTTL;
|
|
31
|
+
private getSetPromise;
|
|
32
|
+
private isTTLValid;
|
|
33
|
+
}
|
|
34
|
+
//#endregion
|
|
35
|
+
export { Cache };
|
|
36
|
+
//# sourceMappingURL=index.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.cts","names":[],"sources":["../../../src/lib/cache/index.ts"],"mappings":";;;cAGa,KAAA;EAAA,QACH,OAAA;EAAA,QACA,SAAA;EAAA,QACA,WAAA;cAEW,OAAA,EAAS,YAAA,CAAa,CAAA;EAM5B,GAAA,CAAI,GAAA,WAAW,OAAA;IAAA;;;;;;EAiCf,GAAA,CAAI,GAAA,UAAa,KAAA,EAAO,CAAA,GAAC,OAAA;IAAA;;;;;;EAoDzB,MAAA,CAAO,GAAA,WAAW,OAAA;IAAA;;;UAgBvB,IAAA;EAAA,YAMI,SAAA,CAAA;EAAA,QAIJ,UAAA;EAAA,QAQA,UAAA;EAAA,QAQA,aAAA;EAAA,QAYA,UAAA;AAAA"}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { CacheOptions } from "../../cache/types.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/lib/cache/index.d.ts
|
|
4
|
+
declare class Cache<T> {
|
|
5
|
+
private options;
|
|
6
|
+
private serialize;
|
|
7
|
+
private deserialize;
|
|
8
|
+
constructor(options: CacheOptions<T>);
|
|
9
|
+
get(key: string): Promise<readonly [{
|
|
10
|
+
readonly type: "NotFoundError";
|
|
11
|
+
readonly message: string;
|
|
12
|
+
}, null] | readonly [{
|
|
13
|
+
readonly type: "CacheError";
|
|
14
|
+
readonly message: string;
|
|
15
|
+
}, null] | readonly [null, T | null]>;
|
|
16
|
+
set(key: string, value: T): Promise<readonly [{
|
|
17
|
+
readonly type: "CacheError";
|
|
18
|
+
readonly message: string;
|
|
19
|
+
}, null] | readonly [null, T] | readonly [{
|
|
20
|
+
readonly type: "InvalidTTLError";
|
|
21
|
+
readonly message: string;
|
|
22
|
+
}, null]>;
|
|
23
|
+
delete(key: string): Promise<readonly [{
|
|
24
|
+
readonly type: "CacheError";
|
|
25
|
+
readonly message: string;
|
|
26
|
+
}, null] | readonly [null, number | null]>;
|
|
27
|
+
private fail;
|
|
28
|
+
private get isEnabled();
|
|
29
|
+
private resolveKey;
|
|
30
|
+
private resolveTTL;
|
|
31
|
+
private getSetPromise;
|
|
32
|
+
private isTTLValid;
|
|
33
|
+
}
|
|
34
|
+
//#endregion
|
|
35
|
+
export { Cache };
|
|
36
|
+
//# sourceMappingURL=index.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../../../src/lib/cache/index.ts"],"mappings":";;;cAGa,KAAA;EAAA,QACH,OAAA;EAAA,QACA,SAAA;EAAA,QACA,WAAA;cAEW,OAAA,EAAS,YAAA,CAAa,CAAA;EAM5B,GAAA,CAAI,GAAA,WAAW,OAAA;IAAA;;;;;;EAiCf,GAAA,CAAI,GAAA,UAAa,KAAA,EAAO,CAAA,GAAC,OAAA;IAAA;;;;;;EAoDzB,MAAA,CAAO,GAAA,WAAW,OAAA;IAAA;;;UAgBvB,IAAA;EAAA,YAMI,SAAA,CAAA;EAAA,QAIJ,UAAA;EAAA,QAQA,UAAA;EAAA,QAQA,aAAA;EAAA,QAYA,UAAA;AAAA"}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { err, mightThrow, mightThrowSync, ok } from "../errors/index.mjs";
|
|
2
|
+
//#region src/lib/cache/index.ts
|
|
3
|
+
var Cache = class {
|
|
4
|
+
options;
|
|
5
|
+
serialize;
|
|
6
|
+
deserialize;
|
|
7
|
+
constructor(options) {
|
|
8
|
+
this.options = options;
|
|
9
|
+
this.serialize = options.serializer ?? JSON.stringify;
|
|
10
|
+
this.deserialize = options.deserializer ?? ((raw) => JSON.parse(raw));
|
|
11
|
+
}
|
|
12
|
+
async get(key) {
|
|
13
|
+
if (!this.isEnabled) return this.fail("NotFoundError", `Key ${key} not found`);
|
|
14
|
+
const resolvedKey = this.resolveKey(key);
|
|
15
|
+
const [error, value] = await mightThrow(this.options.redis.get(resolvedKey));
|
|
16
|
+
if (error) return this.fail("CacheError", `Unable to get value for key ${key}`);
|
|
17
|
+
if (value === null || value === void 0) return this.fail("NotFoundError", `Key ${key} not found`);
|
|
18
|
+
const [deserializeErr, deserialized] = mightThrowSync(() => this.deserialize(value));
|
|
19
|
+
if (deserializeErr) return this.fail("CacheError", `Unable to deserialize value for key ${key}`);
|
|
20
|
+
return ok(deserialized);
|
|
21
|
+
}
|
|
22
|
+
async set(key, value) {
|
|
23
|
+
if (!this.isEnabled) return ok(value);
|
|
24
|
+
const [serializeErr, serialized] = mightThrowSync(() => this.serialize(value));
|
|
25
|
+
if (serializeErr) return this.fail("CacheError", `Unable to serialize value for key ${key}`);
|
|
26
|
+
if (serialized === null || serialized === void 0) return this.fail("CacheError", `Unable to serialize value for key ${key}`);
|
|
27
|
+
const [ttlErr, ttl] = mightThrowSync(() => this.resolveTTL(key, value));
|
|
28
|
+
if (ttlErr) return this.fail("InvalidTTLError", `Unable to resolve ttl for key ${key}`);
|
|
29
|
+
if (!this.isTTLValid(ttl)) return this.fail("InvalidTTLError", `Unable to save records with ttl equal to ${ttl}`);
|
|
30
|
+
const resolvedKey = this.resolveKey(key);
|
|
31
|
+
const [setError] = await mightThrow(this.getSetPromise(resolvedKey, serialized, ttl));
|
|
32
|
+
if (setError) return this.fail("CacheError", `Unable to set value for key ${key}`);
|
|
33
|
+
return ok(value);
|
|
34
|
+
}
|
|
35
|
+
async delete(key) {
|
|
36
|
+
if (!this.isEnabled) return ok(0);
|
|
37
|
+
const resolvedKey = this.resolveKey(key);
|
|
38
|
+
const [error, data] = await mightThrow(this.options.redis.del(resolvedKey));
|
|
39
|
+
if (error) return this.fail("CacheError", `Unable to delete key ${key}`);
|
|
40
|
+
return ok(data);
|
|
41
|
+
}
|
|
42
|
+
fail(type, message) {
|
|
43
|
+
this.options.onError?.({
|
|
44
|
+
type,
|
|
45
|
+
message
|
|
46
|
+
});
|
|
47
|
+
return err(type, message);
|
|
48
|
+
}
|
|
49
|
+
get isEnabled() {
|
|
50
|
+
return this.options.enabled !== false;
|
|
51
|
+
}
|
|
52
|
+
resolveKey(key) {
|
|
53
|
+
if (this.options.prefix) return `${this.options.prefix}:${key}`;
|
|
54
|
+
return key;
|
|
55
|
+
}
|
|
56
|
+
resolveTTL(key, value) {
|
|
57
|
+
if (typeof this.options.ttl === "function") return this.options.ttl(key, value);
|
|
58
|
+
return this.options.ttl;
|
|
59
|
+
}
|
|
60
|
+
getSetPromise(key, value, ttl) {
|
|
61
|
+
if (ttl === void 0 || ttl === null) return this.options.redis.set(key, value);
|
|
62
|
+
return this.options.redis.set(key, value, "PX", ttl);
|
|
63
|
+
}
|
|
64
|
+
isTTLValid(ttl) {
|
|
65
|
+
if (ttl === void 0 || ttl === null) return true;
|
|
66
|
+
if (!Number.isFinite(ttl)) return false;
|
|
67
|
+
if (!Number.isInteger(ttl)) return false;
|
|
68
|
+
if (ttl < 0) return false;
|
|
69
|
+
return true;
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
//#endregion
|
|
73
|
+
export { Cache };
|
|
74
|
+
|
|
75
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../../../src/lib/cache/index.ts"],"sourcesContent":["import { err, mightThrow, mightThrowSync, ok } from \"../errors/index.js\";\nimport type { CacheError, CacheOptions } from \"./types.js\";\n\nexport class Cache<T> {\n private options: CacheOptions<T>;\n private serialize: (value: T) => string;\n private deserialize: (raw: string) => T;\n\n public constructor(options: CacheOptions<T>) {\n this.options = options;\n this.serialize = options.serializer ?? JSON.stringify;\n this.deserialize = options.deserializer ?? ((raw) => JSON.parse(raw));\n }\n\n public async get(key: string) {\n if (!this.isEnabled) {\n return this.fail(\"NotFoundError\", `Key ${key} not found`);\n }\n\n const resolvedKey = this.resolveKey(key);\n\n const [error, value] = await mightThrow(\n this.options.redis.get(resolvedKey),\n );\n\n if (error) {\n return this.fail(\"CacheError\", `Unable to get value for key ${key}`);\n }\n\n if (value === null || value === undefined) {\n return this.fail(\"NotFoundError\", `Key ${key} not found`);\n }\n\n const [deserializeErr, deserialized] = mightThrowSync<T>(() =>\n this.deserialize(value),\n );\n\n if (deserializeErr) {\n return this.fail(\n \"CacheError\",\n `Unable to deserialize value for key ${key}`,\n );\n }\n\n return ok(deserialized);\n }\n\n public async set(key: string, value: T) {\n if (!this.isEnabled) {\n return ok(value);\n }\n\n const [serializeErr, serialized] = mightThrowSync(() =>\n this.serialize(value),\n );\n\n if (serializeErr) {\n return this.fail(\n \"CacheError\",\n `Unable to serialize value for key ${key}`,\n );\n }\n\n if (serialized === null || serialized === undefined) {\n return this.fail(\n \"CacheError\",\n `Unable to serialize value for key ${key}`,\n );\n }\n\n const [ttlErr, ttl] = mightThrowSync(() => this.resolveTTL(key, value));\n\n if (ttlErr) {\n return this.fail(\n \"InvalidTTLError\",\n `Unable to resolve ttl for key ${key}`,\n );\n }\n\n if (!this.isTTLValid(ttl)) {\n return this.fail(\n \"InvalidTTLError\",\n `Unable to save records with ttl equal to ${ttl}`,\n );\n }\n\n const resolvedKey = this.resolveKey(key);\n\n const setPromise = this.getSetPromise(resolvedKey, serialized, ttl);\n\n const [setError] = await mightThrow(setPromise);\n\n if (setError) {\n return this.fail(\"CacheError\", `Unable to set value for key ${key}`);\n }\n\n return ok(value);\n }\n\n public async delete(key: string) {\n if (!this.isEnabled) {\n return ok(0);\n }\n\n const resolvedKey = this.resolveKey(key);\n\n const [error, data] = await mightThrow(this.options.redis.del(resolvedKey));\n\n if (error) {\n return this.fail(\"CacheError\", `Unable to delete key ${key}`);\n }\n\n return ok(data);\n }\n\n private fail<E extends CacheError>(type: E, message: string) {\n this.options.onError?.({ type, message });\n\n return err(type, message);\n }\n\n private get isEnabled() {\n return this.options.enabled !== false;\n }\n\n private resolveKey(key: string) {\n if (this.options.prefix) {\n return `${this.options.prefix}:${key}`;\n }\n\n return key;\n }\n\n private resolveTTL(key: string, value: T) {\n if (typeof this.options.ttl === \"function\") {\n return this.options.ttl(key, value);\n }\n\n return this.options.ttl;\n }\n\n private getSetPromise(\n key: string,\n value: string,\n ttl: number | undefined | null,\n ) {\n if (ttl === undefined || ttl === null) {\n return this.options.redis.set(key, value);\n }\n\n return this.options.redis.set(key, value, \"PX\", ttl);\n }\n\n private isTTLValid(ttl: number | undefined | null) {\n if (ttl === undefined || ttl === null) return true;\n\n if (!Number.isFinite(ttl)) return false;\n\n if (!Number.isInteger(ttl)) return false;\n\n if (ttl < 0) return false;\n\n return true;\n }\n}\n"],"mappings":"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"}
|
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_lib_errors_index = require("../errors/index.cjs");
|
|
3
|
+
const require_scanner = require("../../cron/scanner.cjs");
|
|
4
|
+
//#region src/lib/cron/index.ts
|
|
5
|
+
const RETRY_DELAY_MS = 3600 * 1e3;
|
|
6
|
+
const ALIASES = {
|
|
7
|
+
"@yearly": "0 0 1 1 *",
|
|
8
|
+
"@monthly": "0 0 1 * *",
|
|
9
|
+
"@weekly": "0 0 * * 0",
|
|
10
|
+
"@daily": "0 0 * * *",
|
|
11
|
+
"@hourly": "0 * * * *",
|
|
12
|
+
"@minutely": "* * * * *"
|
|
13
|
+
};
|
|
14
|
+
const CronSecondRange = {
|
|
15
|
+
min: 0,
|
|
16
|
+
max: 59
|
|
17
|
+
};
|
|
18
|
+
const CronMinuteRange = {
|
|
19
|
+
min: 0,
|
|
20
|
+
max: 59
|
|
21
|
+
};
|
|
22
|
+
const CronHourRange = {
|
|
23
|
+
min: 0,
|
|
24
|
+
max: 23
|
|
25
|
+
};
|
|
26
|
+
const CronDayRange = {
|
|
27
|
+
min: 1,
|
|
28
|
+
max: 31
|
|
29
|
+
};
|
|
30
|
+
const CronMonthRange = {
|
|
31
|
+
min: 1,
|
|
32
|
+
max: 12
|
|
33
|
+
};
|
|
34
|
+
const CronDayOfWeekRange = {
|
|
35
|
+
min: 0,
|
|
36
|
+
max: 6
|
|
37
|
+
};
|
|
38
|
+
var Cron = class {
|
|
39
|
+
options;
|
|
40
|
+
status = "idle";
|
|
41
|
+
timeoutId = null;
|
|
42
|
+
second = Array(CronSecondRange.max + 1).fill(0);
|
|
43
|
+
minute = Array(CronMinuteRange.max + 1).fill(0);
|
|
44
|
+
hour = Array(CronHourRange.max + 1).fill(0);
|
|
45
|
+
day = Array(CronDayRange.max + 1).fill(0);
|
|
46
|
+
month = Array(CronMonthRange.max + 1).fill(0);
|
|
47
|
+
dayOfWeek = Array(CronDayOfWeekRange.max + 1).fill(0);
|
|
48
|
+
hasSeconds;
|
|
49
|
+
_dayWildcard = false;
|
|
50
|
+
_dowWildcard = false;
|
|
51
|
+
fillRange(values, min, max) {
|
|
52
|
+
for (let i = min; i <= max; i++) values[i] = 1;
|
|
53
|
+
}
|
|
54
|
+
handleStep(part, values, min, max) {
|
|
55
|
+
const [rangePart, stepStr] = part.split("/");
|
|
56
|
+
if (!rangePart) return require_lib_errors_index.err("InvalidValueError", `'${rangePart}' is empty`);
|
|
57
|
+
if (!stepStr) return require_lib_errors_index.err("InvalidValueError", `'${stepStr}' is empty`);
|
|
58
|
+
const step = Number(stepStr);
|
|
59
|
+
if (!Number.isInteger(step)) return require_lib_errors_index.err("InvalidValueError", `'${step}' is not a valid number`);
|
|
60
|
+
if (step <= 0) return require_lib_errors_index.err("OutOfBoundError", `Expected ${step} > 0`);
|
|
61
|
+
if (rangePart === "*") {
|
|
62
|
+
for (let i = min; i <= max; i += step) values[i] = 1;
|
|
63
|
+
return require_lib_errors_index.ok(true);
|
|
64
|
+
}
|
|
65
|
+
if (rangePart.includes("-")) return this.handleStepRange(rangePart, step, values, min, max);
|
|
66
|
+
return this.handleStepSingle(rangePart, step, values, min, max);
|
|
67
|
+
}
|
|
68
|
+
handleStepRange(range, step, values, min, max) {
|
|
69
|
+
const [startStr, endStr] = range.split("-");
|
|
70
|
+
if (!endStr) return require_lib_errors_index.err("InvalidValueError", `'${endStr}' is empty`);
|
|
71
|
+
let start = min;
|
|
72
|
+
if (startStr && startStr.length > 0) start = Number(startStr);
|
|
73
|
+
const end = Number(endStr);
|
|
74
|
+
if (!Number.isInteger(start)) return require_lib_errors_index.err("InvalidValueError", `'${start}' is not a valid number`);
|
|
75
|
+
if (!Number.isInteger(end)) return require_lib_errors_index.err("InvalidValueError", `'${end}' is not a valid number`);
|
|
76
|
+
if (start < min) return require_lib_errors_index.err("OutOfBoundError", `Expected ${start} >= ${min}`);
|
|
77
|
+
if (end > max) return require_lib_errors_index.err("OutOfBoundError", `Expected ${end} <= ${max}`);
|
|
78
|
+
if (start > end) return require_lib_errors_index.err("OutOfBoundError", `Expected ${start} <= ${end}`);
|
|
79
|
+
for (let i = start; i <= end; i += step) values[i] = 1;
|
|
80
|
+
return require_lib_errors_index.ok(true);
|
|
81
|
+
}
|
|
82
|
+
handleStepSingle(value, step, values, min, max) {
|
|
83
|
+
const start = Number(value);
|
|
84
|
+
if (!Number.isInteger(start)) return require_lib_errors_index.err("InvalidValueError", `'${start}' is not a valid number`);
|
|
85
|
+
if (start < min) return require_lib_errors_index.err("OutOfBoundError", `Expected ${start} >= ${min}`);
|
|
86
|
+
if (start > max) return require_lib_errors_index.err("OutOfBoundError", `Expected ${start} <= ${max}`);
|
|
87
|
+
for (let i = start; i <= max; i += step) values[i] = 1;
|
|
88
|
+
return require_lib_errors_index.ok(true);
|
|
89
|
+
}
|
|
90
|
+
handleRange(part, values, min, max) {
|
|
91
|
+
const [startStr, endStr] = part.split("-");
|
|
92
|
+
if (!startStr) return require_lib_errors_index.err("InvalidValueError", `'${startStr}' is empty`);
|
|
93
|
+
if (!endStr) return require_lib_errors_index.err("InvalidValueError", `'${endStr}' is empty`);
|
|
94
|
+
const start = Number(startStr);
|
|
95
|
+
const end = Number(endStr);
|
|
96
|
+
if (!Number.isInteger(start)) return require_lib_errors_index.err("InvalidValueError", `'${start}' is not a valid number`);
|
|
97
|
+
if (!Number.isInteger(end)) return require_lib_errors_index.err("InvalidValueError", `'${end}' is not a valid number`);
|
|
98
|
+
if (start < min) return require_lib_errors_index.err("OutOfBoundError", `Expected ${start} >= ${min}`);
|
|
99
|
+
if (end > max) return require_lib_errors_index.err("OutOfBoundError", `Expected ${end} <= ${max}`);
|
|
100
|
+
if (start > end) return require_lib_errors_index.err("OutOfBoundError", `Expected ${start} <= ${end}`);
|
|
101
|
+
for (let i = start; i <= end; i++) values[i] = 1;
|
|
102
|
+
return require_lib_errors_index.ok(true);
|
|
103
|
+
}
|
|
104
|
+
handleNumber(value, values, min, max) {
|
|
105
|
+
const n = Number(value);
|
|
106
|
+
if (!Number.isInteger(n)) return require_lib_errors_index.err("InvalidValueError", `'${value}' is not a valid number`);
|
|
107
|
+
if (n < min) return require_lib_errors_index.err("OutOfBoundError", `Expected ${n} >= ${min}`);
|
|
108
|
+
if (n > max) return require_lib_errors_index.err("OutOfBoundError", `Expected ${n} <= ${max}`);
|
|
109
|
+
values[n] = 1;
|
|
110
|
+
return require_lib_errors_index.ok(true);
|
|
111
|
+
}
|
|
112
|
+
constructor(options) {
|
|
113
|
+
this.options = options;
|
|
114
|
+
const expr = this.resolveAlias(options.schedule);
|
|
115
|
+
const [error, tokens] = new require_scanner.Scanner(expr).scan();
|
|
116
|
+
if (error) throw new Error(`${error.type}: ${error.message}`);
|
|
117
|
+
this.hasSeconds = expr.trim().split(/\s+/).length === require_scanner.FieldAmount.max;
|
|
118
|
+
const [parsingError, _] = this.parse(tokens);
|
|
119
|
+
if (parsingError) throw new Error(`${parsingError.type}: ${parsingError.message}`);
|
|
120
|
+
}
|
|
121
|
+
resolveAlias(schedule) {
|
|
122
|
+
return ALIASES[schedule] || schedule;
|
|
123
|
+
}
|
|
124
|
+
parse(tokens) {
|
|
125
|
+
for (let i = 0; i < tokens.length; i++) {
|
|
126
|
+
const token = tokens[i];
|
|
127
|
+
if (!token) return require_lib_errors_index.err("InvalidValueError", "Undefined token");
|
|
128
|
+
const tokenType = token.getTokenType();
|
|
129
|
+
switch (token.getField()) {
|
|
130
|
+
case "second": {
|
|
131
|
+
const [error, _] = this.handleField(token, this.second, CronSecondRange.min, CronSecondRange.max);
|
|
132
|
+
if (error) return require_lib_errors_index.err(error.type, `${error.message} in field '${token.getField()}'`);
|
|
133
|
+
break;
|
|
134
|
+
}
|
|
135
|
+
case "minute": {
|
|
136
|
+
const [error, _] = this.handleField(token, this.minute, CronMinuteRange.min, CronMinuteRange.max);
|
|
137
|
+
if (error) return require_lib_errors_index.err(error.type, `${error.message} in field '${token.getField()}'`);
|
|
138
|
+
break;
|
|
139
|
+
}
|
|
140
|
+
case "hour": {
|
|
141
|
+
const [error, _] = this.handleField(token, this.hour, CronHourRange.min, CronHourRange.max);
|
|
142
|
+
if (error) return require_lib_errors_index.err(error.type, `${error.message} in field '${token.getField()}'`);
|
|
143
|
+
break;
|
|
144
|
+
}
|
|
145
|
+
case "day": {
|
|
146
|
+
if (tokenType === "any") this._dayWildcard = true;
|
|
147
|
+
const [error, _] = this.handleField(token, this.day, CronDayRange.min, CronDayRange.max);
|
|
148
|
+
if (error) return require_lib_errors_index.err(error.type, `${error.message} in field '${token.getField()}'`);
|
|
149
|
+
break;
|
|
150
|
+
}
|
|
151
|
+
case "month": {
|
|
152
|
+
const [error, _] = this.handleField(token, this.month, CronMonthRange.min, CronMonthRange.max);
|
|
153
|
+
if (error) return require_lib_errors_index.err(error.type, `${error.message} in field '${token.getField()}'`);
|
|
154
|
+
break;
|
|
155
|
+
}
|
|
156
|
+
case "weekday": {
|
|
157
|
+
if (tokenType === "any") this._dowWildcard = true;
|
|
158
|
+
const [error, _] = this.handleField(token, this.dayOfWeek, CronDayOfWeekRange.min, CronDayOfWeekRange.max);
|
|
159
|
+
if (error) return require_lib_errors_index.err(error.type, `${error.message} in field '${token.getField()}'`);
|
|
160
|
+
break;
|
|
161
|
+
}
|
|
162
|
+
default: return require_lib_errors_index.err("InvalidValueError", `Invalid field '${token.getField()}'`);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
return require_lib_errors_index.ok(true);
|
|
166
|
+
}
|
|
167
|
+
handleField(token, field, min, max) {
|
|
168
|
+
switch (token.getTokenType()) {
|
|
169
|
+
case "any":
|
|
170
|
+
this.fillRange(field, min, max);
|
|
171
|
+
break;
|
|
172
|
+
case "number": {
|
|
173
|
+
const [error, _] = this.handleNumber(token.getComponent(), field, min, max);
|
|
174
|
+
if (error) return require_lib_errors_index.err(error.type, error.message);
|
|
175
|
+
break;
|
|
176
|
+
}
|
|
177
|
+
case "range": {
|
|
178
|
+
const component = token.getComponent();
|
|
179
|
+
const [error, _] = this.handleRange(component, field, min, max);
|
|
180
|
+
if (error) return require_lib_errors_index.err(error.type, error.message);
|
|
181
|
+
break;
|
|
182
|
+
}
|
|
183
|
+
case "step": {
|
|
184
|
+
const component = token.getComponent();
|
|
185
|
+
const [error, _] = this.handleStep(component, field, min, max);
|
|
186
|
+
if (error) return require_lib_errors_index.err(error.type, error.message);
|
|
187
|
+
break;
|
|
188
|
+
}
|
|
189
|
+
default: return require_lib_errors_index.err("InvalidValueError", `Invalid token type '${token.getTokenType()}'`);
|
|
190
|
+
}
|
|
191
|
+
return require_lib_errors_index.ok(true);
|
|
192
|
+
}
|
|
193
|
+
matches(date) {
|
|
194
|
+
const s = date.getSeconds();
|
|
195
|
+
const m = date.getMinutes();
|
|
196
|
+
const h = date.getHours();
|
|
197
|
+
const d = date.getDate();
|
|
198
|
+
const mon = date.getMonth();
|
|
199
|
+
const dow = date.getDay();
|
|
200
|
+
const isSecondMatch = this.hasSeconds ? this.second[s] === 1 : true;
|
|
201
|
+
const isMinuteMatch = this.minute[m] === 1;
|
|
202
|
+
const isHourMatch = this.hour[h] === 1;
|
|
203
|
+
const isMonthMatch = this.month[mon + 1] === 1;
|
|
204
|
+
let isDayOrDowMatch;
|
|
205
|
+
if (!this._dayWildcard && !this._dowWildcard) isDayOrDowMatch = this.day[d] === 1 || this.dayOfWeek[dow] === 1;
|
|
206
|
+
else isDayOrDowMatch = this.day[d] === 1 && this.dayOfWeek[dow] === 1;
|
|
207
|
+
return isSecondMatch && isMinuteMatch && isHourMatch && isDayOrDowMatch && isMonthMatch;
|
|
208
|
+
}
|
|
209
|
+
getNextRun() {
|
|
210
|
+
const date = /* @__PURE__ */ new Date(/* @__PURE__ */ new Date());
|
|
211
|
+
if (this.hasSeconds) {
|
|
212
|
+
date.setMilliseconds(0);
|
|
213
|
+
date.setSeconds(date.getSeconds() + 1);
|
|
214
|
+
} else {
|
|
215
|
+
date.setSeconds(0, 0);
|
|
216
|
+
date.setMinutes(date.getMinutes() + 1);
|
|
217
|
+
}
|
|
218
|
+
const maxIterations = this.hasSeconds ? 366 * 4 * 24 * 3600 : 366 * 4 * 24 * 60;
|
|
219
|
+
for (let i = 0; i < maxIterations; i++) {
|
|
220
|
+
if (this.matches(date)) {
|
|
221
|
+
if (date > /* @__PURE__ */ new Date()) return new Date(date);
|
|
222
|
+
}
|
|
223
|
+
if (this.hasSeconds) date.setSeconds(date.getSeconds() + 1);
|
|
224
|
+
else date.setMinutes(date.getMinutes() + 1);
|
|
225
|
+
}
|
|
226
|
+
return null;
|
|
227
|
+
}
|
|
228
|
+
start() {
|
|
229
|
+
if (this.status !== "idle") return;
|
|
230
|
+
this.status = "running";
|
|
231
|
+
this.next();
|
|
232
|
+
}
|
|
233
|
+
stop() {
|
|
234
|
+
this.status = "idle";
|
|
235
|
+
if (this.timeoutId) {
|
|
236
|
+
clearTimeout(this.timeoutId);
|
|
237
|
+
this.timeoutId = null;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
pause() {
|
|
241
|
+
if (this.status !== "running") return;
|
|
242
|
+
this.status = "paused";
|
|
243
|
+
if (this.timeoutId) {
|
|
244
|
+
clearTimeout(this.timeoutId);
|
|
245
|
+
this.timeoutId = null;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
resume() {
|
|
249
|
+
if (this.status !== "paused") return;
|
|
250
|
+
this.status = "running";
|
|
251
|
+
this.next();
|
|
252
|
+
}
|
|
253
|
+
getStatus() {
|
|
254
|
+
return this.status;
|
|
255
|
+
}
|
|
256
|
+
next() {
|
|
257
|
+
if (this.status !== "running") return;
|
|
258
|
+
const nextRun = this.getNextRun();
|
|
259
|
+
if (!nextRun) {
|
|
260
|
+
this.timeoutId = setTimeout(() => this.next(), RETRY_DELAY_MS);
|
|
261
|
+
return;
|
|
262
|
+
}
|
|
263
|
+
const delay = nextRun.getTime() - Date.now();
|
|
264
|
+
this.timeoutId = setTimeout(() => {
|
|
265
|
+
this.run();
|
|
266
|
+
}, Math.max(0, delay));
|
|
267
|
+
}
|
|
268
|
+
async run() {
|
|
269
|
+
if (this.status !== "running") return;
|
|
270
|
+
const handlerResult = this.options.handler();
|
|
271
|
+
await require_lib_errors_index.mightThrow(Promise.resolve(handlerResult));
|
|
272
|
+
if (this.status === "running") this.next();
|
|
273
|
+
}
|
|
274
|
+
};
|
|
275
|
+
//#endregion
|
|
276
|
+
exports.Cron = Cron;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { CronOptions, CronStatus } from "../../cron/types.cjs";
|
|
2
|
+
|
|
3
|
+
//#region src/lib/cron/index.d.ts
|
|
4
|
+
declare class Cron {
|
|
5
|
+
private options;
|
|
6
|
+
private status;
|
|
7
|
+
private timeoutId;
|
|
8
|
+
private second;
|
|
9
|
+
private minute;
|
|
10
|
+
private hour;
|
|
11
|
+
private day;
|
|
12
|
+
private month;
|
|
13
|
+
private dayOfWeek;
|
|
14
|
+
private hasSeconds;
|
|
15
|
+
private _dayWildcard;
|
|
16
|
+
private _dowWildcard;
|
|
17
|
+
private fillRange;
|
|
18
|
+
private handleStep;
|
|
19
|
+
private handleStepRange;
|
|
20
|
+
private handleStepSingle;
|
|
21
|
+
private handleRange;
|
|
22
|
+
private handleNumber;
|
|
23
|
+
constructor(options: CronOptions);
|
|
24
|
+
private resolveAlias;
|
|
25
|
+
private parse;
|
|
26
|
+
private handleField;
|
|
27
|
+
matches(date: Date): boolean;
|
|
28
|
+
getNextRun(): Date | null;
|
|
29
|
+
start(): void;
|
|
30
|
+
stop(): void;
|
|
31
|
+
pause(): void;
|
|
32
|
+
resume(): void;
|
|
33
|
+
getStatus(): CronStatus;
|
|
34
|
+
private next;
|
|
35
|
+
private run;
|
|
36
|
+
}
|
|
37
|
+
//#endregion
|
|
38
|
+
export { Cron };
|
|
39
|
+
//# sourceMappingURL=index.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.cts","names":[],"sources":["../../../src/lib/cron/index.ts"],"mappings":";;;cA6Ca,IAAA;EAAA,QACH,OAAA;EAAA,QACA,MAAA;EAAA,QACA,SAAA;EAAA,QAGA,MAAA;EAAA,QACA,MAAA;EAAA,QACA,IAAA;EAAA,QACA,GAAA;EAAA,QACA,KAAA;EAAA,QACA,SAAA;EAAA,QACA,UAAA;EAAA,QACA,YAAA;EAAA,QACA,YAAA;EAAA,QAGA,SAAA;EAAA,QAMA,UAAA;EAAA,QAkDA,eAAA;EAAA,QAiEA,gBAAA;EAAA,QAuCA,WAAA;EAAA,QAmEA,YAAA;cAkCW,OAAA,EAAS,WAAA;EAAA,QAoBpB,YAAA;EAAA,QAIA,KAAA;EAAA,QAoIA,WAAA;EAyCD,OAAA,CAAQ,IAAA,EAAM,IAAA;EAkCd,UAAA,CAAA,GAAU,IAAA;EAuCV,KAAA,CAAA;EAOA,IAAA,CAAA;EASA,KAAA,CAAA;EAWA,MAAA,CAAA;EAOA,SAAA,CAAA,GAAS,UAAA;EAAA,QAIR,IAAA;EAAA,QAmBM,GAAA;AAAA"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { CronOptions, CronStatus } from "../../cron/types.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/lib/cron/index.d.ts
|
|
4
|
+
declare class Cron {
|
|
5
|
+
private options;
|
|
6
|
+
private status;
|
|
7
|
+
private timeoutId;
|
|
8
|
+
private second;
|
|
9
|
+
private minute;
|
|
10
|
+
private hour;
|
|
11
|
+
private day;
|
|
12
|
+
private month;
|
|
13
|
+
private dayOfWeek;
|
|
14
|
+
private hasSeconds;
|
|
15
|
+
private _dayWildcard;
|
|
16
|
+
private _dowWildcard;
|
|
17
|
+
private fillRange;
|
|
18
|
+
private handleStep;
|
|
19
|
+
private handleStepRange;
|
|
20
|
+
private handleStepSingle;
|
|
21
|
+
private handleRange;
|
|
22
|
+
private handleNumber;
|
|
23
|
+
constructor(options: CronOptions);
|
|
24
|
+
private resolveAlias;
|
|
25
|
+
private parse;
|
|
26
|
+
private handleField;
|
|
27
|
+
matches(date: Date): boolean;
|
|
28
|
+
getNextRun(): Date | null;
|
|
29
|
+
start(): void;
|
|
30
|
+
stop(): void;
|
|
31
|
+
pause(): void;
|
|
32
|
+
resume(): void;
|
|
33
|
+
getStatus(): CronStatus;
|
|
34
|
+
private next;
|
|
35
|
+
private run;
|
|
36
|
+
}
|
|
37
|
+
//#endregion
|
|
38
|
+
export { Cron };
|
|
39
|
+
//# sourceMappingURL=index.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../../../src/lib/cron/index.ts"],"mappings":";;;cA6Ca,IAAA;EAAA,QACH,OAAA;EAAA,QACA,MAAA;EAAA,QACA,SAAA;EAAA,QAGA,MAAA;EAAA,QACA,MAAA;EAAA,QACA,IAAA;EAAA,QACA,GAAA;EAAA,QACA,KAAA;EAAA,QACA,SAAA;EAAA,QACA,UAAA;EAAA,QACA,YAAA;EAAA,QACA,YAAA;EAAA,QAGA,SAAA;EAAA,QAMA,UAAA;EAAA,QAkDA,eAAA;EAAA,QAiEA,gBAAA;EAAA,QAuCA,WAAA;EAAA,QAmEA,YAAA;cAkCW,OAAA,EAAS,WAAA;EAAA,QAoBpB,YAAA;EAAA,QAIA,KAAA;EAAA,QAoIA,WAAA;EAyCD,OAAA,CAAQ,IAAA,EAAM,IAAA;EAkCd,UAAA,CAAA,GAAU,IAAA;EAuCV,KAAA,CAAA;EAOA,IAAA,CAAA;EASA,KAAA,CAAA;EAWA,MAAA,CAAA;EAOA,SAAA,CAAA,GAAS,UAAA;EAAA,QAIR,IAAA;EAAA,QAmBM,GAAA;AAAA"}
|