alepha 0.1.0 → 0.3.0

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/LICENSE CHANGED
@@ -1,7 +1,21 @@
1
- Copyright 2024 Feunard
1
+ MIT License
2
2
 
3
- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
3
+ Copyright (c) 2025 Feunard
4
4
 
5
- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
6
11
 
7
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1 +1,29 @@
1
- # Alepha
1
+ # @alepha/core
2
+
3
+ ## Installation
4
+
5
+ ```bash
6
+ npm install @alepha/core
7
+ ```
8
+
9
+ ## Usage
10
+
11
+ ```typescript
12
+ import { Alepha, inject, t } from '@alepha/core';
13
+
14
+ class B {
15
+ env = inject(
16
+ t.object({
17
+ HELLO: t.string(),
18
+ })
19
+ );
20
+ }
21
+
22
+ class A {
23
+ b = inject(B);
24
+ }
25
+
26
+ const app = Alepha.create({ HELLO: "WORLD" });
27
+
28
+ app.log.info(app.get(A).b.env.HELLO); // WORLD
29
+ ```
package/dist/index.cjs CHANGED
@@ -1,9 +1,173 @@
1
1
  'use strict';
2
2
 
3
- class Alepha {
3
+ var cache = require('@alepha/cache');
4
+ var core = require('@alepha/core');
5
+ var store = require('@alepha/store');
6
+ var postgres = require('@alepha/postgres');
7
+ var queue = require('@alepha/queue');
8
+ var redis = require('@alepha/redis');
9
+ var scheduler = require('@alepha/scheduler');
10
+ var server = require('@alepha/server');
11
+ var topic = require('@alepha/topic');
12
+
13
+ const lockDescriptorKey = "LOCK";
14
+ const lock = (options) => core.createDescriptorValue({
15
+ kind: lockDescriptorKey,
16
+ options,
17
+ apply: (...args) => {
18
+ return options.handler(...args);
19
+ }
20
+ });
21
+ lock[core.KIND] = lockDescriptorKey;
22
+
23
+ class LockDescriptorProvider {
24
+ alepha = core.inject(core.Alepha);
25
+ dateTimeProvider = core.inject(core.DateTimeProvider);
26
+ storeProvider = core.inject(store.StoreProvider);
27
+ log = core.logger();
28
+ id = this.alepha.id;
29
+ configure = core.hook({
30
+ name: "configure",
31
+ handler: (alepha) => {
32
+ const descriptors = alepha.getDescriptorValues(lock);
33
+ for (const { instance, key, value } of descriptors) {
34
+ const { options } = value;
35
+ const item = {
36
+ options,
37
+ key: (...args) => {
38
+ if (options.key) {
39
+ if (typeof options.key === "string") {
40
+ return options.key;
41
+ }
42
+ return options.key(...args);
43
+ }
44
+ return key;
45
+ },
46
+ maxDuration: options.maxDuration ?? 60
47
+ };
48
+ const run = (...args) => this.run(item, ...args);
49
+ core.updateDescriptorValue(value, instance, {
50
+ apply: run
51
+ });
52
+ }
53
+ }
54
+ });
55
+ /**
56
+ * Run the lock handler.
57
+ *
58
+ * @param value
59
+ * @param args
60
+ */
61
+ async run(value, ...args) {
62
+ const key = `lock:${value.key(...args)}`;
63
+ const handler = value.options.handler;
64
+ const lock2 = await this.lock(key, value);
65
+ if (lock2.id !== this.id) {
66
+ return;
67
+ }
68
+ if (lock2.endedAt) {
69
+ return;
70
+ }
71
+ try {
72
+ await handler(...args);
73
+ } finally {
74
+ const gracePeriod = value.options.gracePeriod ? typeof value.options.gracePeriod === "function" ? value.options.gracePeriod(...args) : value.options.gracePeriod : void 0;
75
+ if (gracePeriod) {
76
+ await this.storeProvider.set(
77
+ key,
78
+ `${this.id},${lock2.createdAt.toISO()},${this.dateTimeProvider.nowISOString()}`,
79
+ {
80
+ px: this.dateTimeProvider.duration(gracePeriod).as("milliseconds")
81
+ }
82
+ );
83
+ } else {
84
+ await this.storeProvider.del(key);
85
+ }
86
+ }
87
+ }
88
+ async lock(key, item) {
89
+ const value = await this.storeProvider.set(
90
+ key,
91
+ `${this.id},${this.dateTimeProvider.nowISOString()}`,
92
+ {
93
+ nx: true,
94
+ px: this.dateTimeProvider.duration(item.maxDuration).as("milliseconds")
95
+ }
96
+ );
97
+ const [id, createdAtStr, endedAtStr] = value.split(",");
98
+ const createdAt = core.DateTime.fromISO(createdAtStr);
99
+ const endedAt = endedAtStr ? core.DateTime.fromISO(endedAtStr) : void 0;
100
+ return {
101
+ id,
102
+ createdAt,
103
+ endedAt
104
+ };
105
+ }
106
+ }
107
+
108
+ class LockModule {
109
+ alepha = core.inject(core.Alepha);
4
110
  constructor() {
5
- console.log("App is running");
111
+ this.alepha.register(store.StoreModule);
112
+ this.alepha.register(LockDescriptorProvider);
6
113
  }
7
114
  }
115
+ core.autoInject(lock, LockModule);
8
116
 
9
- exports.Alepha = Alepha;
117
+ exports.LockDescriptorProvider = LockDescriptorProvider;
118
+ exports.lock = lock;
119
+ exports.lockDescriptorKey = lockDescriptorKey;
120
+ Object.keys(cache).forEach(function (k) {
121
+ if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
122
+ enumerable: true,
123
+ get: function () { return cache[k]; }
124
+ });
125
+ });
126
+ Object.keys(core).forEach(function (k) {
127
+ if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
128
+ enumerable: true,
129
+ get: function () { return core[k]; }
130
+ });
131
+ });
132
+ Object.keys(store).forEach(function (k) {
133
+ if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
134
+ enumerable: true,
135
+ get: function () { return store[k]; }
136
+ });
137
+ });
138
+ Object.keys(postgres).forEach(function (k) {
139
+ if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
140
+ enumerable: true,
141
+ get: function () { return postgres[k]; }
142
+ });
143
+ });
144
+ Object.keys(queue).forEach(function (k) {
145
+ if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
146
+ enumerable: true,
147
+ get: function () { return queue[k]; }
148
+ });
149
+ });
150
+ Object.keys(redis).forEach(function (k) {
151
+ if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
152
+ enumerable: true,
153
+ get: function () { return redis[k]; }
154
+ });
155
+ });
156
+ Object.keys(scheduler).forEach(function (k) {
157
+ if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
158
+ enumerable: true,
159
+ get: function () { return scheduler[k]; }
160
+ });
161
+ });
162
+ Object.keys(server).forEach(function (k) {
163
+ if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
164
+ enumerable: true,
165
+ get: function () { return server[k]; }
166
+ });
167
+ });
168
+ Object.keys(topic).forEach(function (k) {
169
+ if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
170
+ enumerable: true,
171
+ get: function () { return topic[k]; }
172
+ });
173
+ });
package/dist/index.d.cts CHANGED
@@ -1,5 +1,76 @@
1
- declare class Alepha {
2
- constructor();
1
+ export * from '@alepha/cache';
2
+ import * as _alepha_core from '@alepha/core';
3
+ import { AsyncFn, DurationLike, KIND, Alepha, DateTimeProvider, DateTime } from '@alepha/core';
4
+ export * from '@alepha/core';
5
+ import { StoreProvider } from '@alepha/store';
6
+ export * from '@alepha/store';
7
+ export * from '@alepha/postgres';
8
+ export * from '@alepha/queue';
9
+ export * from '@alepha/redis';
10
+ export * from '@alepha/scheduler';
11
+ export * from '@alepha/server';
12
+ export * from '@alepha/topic';
13
+
14
+ interface LockDescriptorOptions<TFunc extends AsyncFn> {
15
+ /**
16
+ * Function executed when the lock is acquired.
17
+ */
18
+ handler: TFunc;
19
+ key?: string | ((...args: Parameters<TFunc>) => string);
20
+ maxDuration?: DurationLike;
21
+ gracePeriod?: DurationLike | ((...args: Parameters<TFunc>) => DurationLike | undefined);
22
+ }
23
+ declare const lockDescriptorKey = "LOCK";
24
+ /**
25
+ * Lock descriptor
26
+ *
27
+ * Make sure that only one instance of the handler is running at a time.
28
+ *
29
+ * When connected to a remote store, the lock is shared across all processes.
30
+ *
31
+ * @param options
32
+ */
33
+ declare const lock: {
34
+ <TFunc extends AsyncFn>(options: LockDescriptorOptions<TFunc>): _alepha_core.DescriptorInstance<{
35
+ kind: string;
36
+ options: LockDescriptorOptions<TFunc>;
37
+ apply: (...args: Parameters<TFunc>) => Promise<void>;
38
+ }>;
39
+ [KIND]: string;
40
+ };
41
+
42
+ declare class LockDescriptorProvider {
43
+ protected readonly alepha: Alepha;
44
+ protected readonly dateTimeProvider: DateTimeProvider;
45
+ protected readonly storeProvider: StoreProvider;
46
+ protected readonly log: _alepha_core.Logger;
47
+ protected readonly id: string;
48
+ protected readonly configure: _alepha_core.DescriptorInstance<{
49
+ kind: string;
50
+ options: {
51
+ name: "configure";
52
+ handler: (app: Alepha) => _alepha_core.Async<void>;
53
+ };
54
+ apply: (arg: Alepha) => _alepha_core.Async<void>;
55
+ }>;
56
+ /**
57
+ * Run the lock handler.
58
+ *
59
+ * @param value
60
+ * @param args
61
+ */
62
+ protected run(value: LockDescriptorValue, ...args: any[]): Promise<void>;
63
+ protected lock(key: string, item: LockDescriptorValue): Promise<LockObject>;
64
+ }
65
+ interface LockDescriptorValue {
66
+ options: LockDescriptorOptions<AsyncFn>;
67
+ key: (...args: any[]) => string;
68
+ maxDuration: DurationLike;
69
+ }
70
+ interface LockObject {
71
+ id: string;
72
+ createdAt: DateTime;
73
+ endedAt?: DateTime;
3
74
  }
4
75
 
5
- export { Alepha };
76
+ export { type LockDescriptorOptions, LockDescriptorProvider, type LockDescriptorValue, type LockObject, lock, lockDescriptorKey };
package/dist/index.d.mts CHANGED
@@ -1,5 +1,76 @@
1
- declare class Alepha {
2
- constructor();
1
+ export * from '@alepha/cache';
2
+ import * as _alepha_core from '@alepha/core';
3
+ import { AsyncFn, DurationLike, KIND, Alepha, DateTimeProvider, DateTime } from '@alepha/core';
4
+ export * from '@alepha/core';
5
+ import { StoreProvider } from '@alepha/store';
6
+ export * from '@alepha/store';
7
+ export * from '@alepha/postgres';
8
+ export * from '@alepha/queue';
9
+ export * from '@alepha/redis';
10
+ export * from '@alepha/scheduler';
11
+ export * from '@alepha/server';
12
+ export * from '@alepha/topic';
13
+
14
+ interface LockDescriptorOptions<TFunc extends AsyncFn> {
15
+ /**
16
+ * Function executed when the lock is acquired.
17
+ */
18
+ handler: TFunc;
19
+ key?: string | ((...args: Parameters<TFunc>) => string);
20
+ maxDuration?: DurationLike;
21
+ gracePeriod?: DurationLike | ((...args: Parameters<TFunc>) => DurationLike | undefined);
22
+ }
23
+ declare const lockDescriptorKey = "LOCK";
24
+ /**
25
+ * Lock descriptor
26
+ *
27
+ * Make sure that only one instance of the handler is running at a time.
28
+ *
29
+ * When connected to a remote store, the lock is shared across all processes.
30
+ *
31
+ * @param options
32
+ */
33
+ declare const lock: {
34
+ <TFunc extends AsyncFn>(options: LockDescriptorOptions<TFunc>): _alepha_core.DescriptorInstance<{
35
+ kind: string;
36
+ options: LockDescriptorOptions<TFunc>;
37
+ apply: (...args: Parameters<TFunc>) => Promise<void>;
38
+ }>;
39
+ [KIND]: string;
40
+ };
41
+
42
+ declare class LockDescriptorProvider {
43
+ protected readonly alepha: Alepha;
44
+ protected readonly dateTimeProvider: DateTimeProvider;
45
+ protected readonly storeProvider: StoreProvider;
46
+ protected readonly log: _alepha_core.Logger;
47
+ protected readonly id: string;
48
+ protected readonly configure: _alepha_core.DescriptorInstance<{
49
+ kind: string;
50
+ options: {
51
+ name: "configure";
52
+ handler: (app: Alepha) => _alepha_core.Async<void>;
53
+ };
54
+ apply: (arg: Alepha) => _alepha_core.Async<void>;
55
+ }>;
56
+ /**
57
+ * Run the lock handler.
58
+ *
59
+ * @param value
60
+ * @param args
61
+ */
62
+ protected run(value: LockDescriptorValue, ...args: any[]): Promise<void>;
63
+ protected lock(key: string, item: LockDescriptorValue): Promise<LockObject>;
64
+ }
65
+ interface LockDescriptorValue {
66
+ options: LockDescriptorOptions<AsyncFn>;
67
+ key: (...args: any[]) => string;
68
+ maxDuration: DurationLike;
69
+ }
70
+ interface LockObject {
71
+ id: string;
72
+ createdAt: DateTime;
73
+ endedAt?: DateTime;
3
74
  }
4
75
 
5
- export { Alepha };
76
+ export { type LockDescriptorOptions, LockDescriptorProvider, type LockDescriptorValue, type LockObject, lock, lockDescriptorKey };
package/dist/index.mjs CHANGED
@@ -1,7 +1,117 @@
1
- class Alepha {
1
+ export * from '@alepha/cache';
2
+ import { KIND, autoInject, createDescriptorValue, inject, Alepha, DateTimeProvider, logger, hook, updateDescriptorValue, DateTime } from '@alepha/core';
3
+ export * from '@alepha/core';
4
+ import { StoreProvider, StoreModule } from '@alepha/store';
5
+ export * from '@alepha/store';
6
+ export * from '@alepha/postgres';
7
+ export * from '@alepha/queue';
8
+ export * from '@alepha/redis';
9
+ export * from '@alepha/scheduler';
10
+ export * from '@alepha/server';
11
+ export * from '@alepha/topic';
12
+
13
+ const lockDescriptorKey = "LOCK";
14
+ const lock = (options) => createDescriptorValue({
15
+ kind: lockDescriptorKey,
16
+ options,
17
+ apply: (...args) => {
18
+ return options.handler(...args);
19
+ }
20
+ });
21
+ lock[KIND] = lockDescriptorKey;
22
+
23
+ class LockDescriptorProvider {
24
+ alepha = inject(Alepha);
25
+ dateTimeProvider = inject(DateTimeProvider);
26
+ storeProvider = inject(StoreProvider);
27
+ log = logger();
28
+ id = this.alepha.id;
29
+ configure = hook({
30
+ name: "configure",
31
+ handler: (alepha) => {
32
+ const descriptors = alepha.getDescriptorValues(lock);
33
+ for (const { instance, key, value } of descriptors) {
34
+ const { options } = value;
35
+ const item = {
36
+ options,
37
+ key: (...args) => {
38
+ if (options.key) {
39
+ if (typeof options.key === "string") {
40
+ return options.key;
41
+ }
42
+ return options.key(...args);
43
+ }
44
+ return key;
45
+ },
46
+ maxDuration: options.maxDuration ?? 60
47
+ };
48
+ const run = (...args) => this.run(item, ...args);
49
+ updateDescriptorValue(value, instance, {
50
+ apply: run
51
+ });
52
+ }
53
+ }
54
+ });
55
+ /**
56
+ * Run the lock handler.
57
+ *
58
+ * @param value
59
+ * @param args
60
+ */
61
+ async run(value, ...args) {
62
+ const key = `lock:${value.key(...args)}`;
63
+ const handler = value.options.handler;
64
+ const lock2 = await this.lock(key, value);
65
+ if (lock2.id !== this.id) {
66
+ return;
67
+ }
68
+ if (lock2.endedAt) {
69
+ return;
70
+ }
71
+ try {
72
+ await handler(...args);
73
+ } finally {
74
+ const gracePeriod = value.options.gracePeriod ? typeof value.options.gracePeriod === "function" ? value.options.gracePeriod(...args) : value.options.gracePeriod : void 0;
75
+ if (gracePeriod) {
76
+ await this.storeProvider.set(
77
+ key,
78
+ `${this.id},${lock2.createdAt.toISO()},${this.dateTimeProvider.nowISOString()}`,
79
+ {
80
+ px: this.dateTimeProvider.duration(gracePeriod).as("milliseconds")
81
+ }
82
+ );
83
+ } else {
84
+ await this.storeProvider.del(key);
85
+ }
86
+ }
87
+ }
88
+ async lock(key, item) {
89
+ const value = await this.storeProvider.set(
90
+ key,
91
+ `${this.id},${this.dateTimeProvider.nowISOString()}`,
92
+ {
93
+ nx: true,
94
+ px: this.dateTimeProvider.duration(item.maxDuration).as("milliseconds")
95
+ }
96
+ );
97
+ const [id, createdAtStr, endedAtStr] = value.split(",");
98
+ const createdAt = DateTime.fromISO(createdAtStr);
99
+ const endedAt = endedAtStr ? DateTime.fromISO(endedAtStr) : void 0;
100
+ return {
101
+ id,
102
+ createdAt,
103
+ endedAt
104
+ };
105
+ }
106
+ }
107
+
108
+ class LockModule {
109
+ alepha = inject(Alepha);
2
110
  constructor() {
3
- console.log("App is running");
111
+ this.alepha.register(StoreModule);
112
+ this.alepha.register(LockDescriptorProvider);
4
113
  }
5
114
  }
115
+ autoInject(lock, LockModule);
6
116
 
7
- export { Alepha };
117
+ export { LockDescriptorProvider, lock, lockDescriptorKey };
package/package.json CHANGED
@@ -1,25 +1,38 @@
1
1
  {
2
- "name": "alepha",
3
- "version": "0.1.0",
4
- "type": "module",
5
- "license": "MIT",
6
- "main": "./dist/index.cjs",
7
- "module": "./dist/index.mjs",
8
- "types": "./dist/index.d.cts",
9
- "exports": {
10
- "require": {
11
- "types": "./dist/index.d.cts",
12
- "default": "./dist/index.cjs"
13
- },
14
- "import": {
15
- "types": "./dist/index.d.mts",
16
- "default": "./dist/index.mjs"
17
- }
18
- },
19
- "devDependencies": {
20
- "pkgroll": "^2.6.0"
21
- },
22
- "scripts": {
23
- "build": "pkgroll"
24
- }
25
- }
2
+ "name": "alepha",
3
+ "version": "0.3.0",
4
+ "type": "module",
5
+ "license": "MIT",
6
+ "main": "./dist/index.cjs",
7
+ "module": "./dist/index.mjs",
8
+ "types": "./dist/index.d.cts",
9
+ "dependencies": {
10
+ "@alepha/cache": "0.3.0",
11
+ "@alepha/core": "0.3.0",
12
+ "@alepha/postgres": "0.3.0",
13
+ "@alepha/queue": "0.3.0",
14
+ "@alepha/redis": "0.3.0",
15
+ "@alepha/scheduler": "0.3.0",
16
+ "@alepha/server": "0.3.0",
17
+ "@alepha/store": "0.3.0",
18
+ "@alepha/topic": "0.3.0"
19
+ },
20
+ "devDependencies": {
21
+ "pkgroll": "^2.6.0",
22
+ "typescript": "^5.7.2",
23
+ "vitest": "^2.1.8"
24
+ },
25
+ "scripts": {
26
+ "build": "pkgroll --clean-dist"
27
+ },
28
+ "exports": {
29
+ "require": {
30
+ "types": "./dist/index.d.cts",
31
+ "default": "./dist/index.cjs"
32
+ },
33
+ "import": {
34
+ "types": "./dist/index.d.mts",
35
+ "default": "./dist/index.mjs"
36
+ }
37
+ }
38
+ }
package/src/index.ts ADDED
@@ -0,0 +1,10 @@
1
+ export * from "@alepha/cache";
2
+ export * from "@alepha/core";
3
+ export * from "@alepha/lock";
4
+ export * from "@alepha/postgres";
5
+ export * from "@alepha/queue";
6
+ export * from "@alepha/redis";
7
+ export * from "@alepha/scheduler";
8
+ export * from "@alepha/server";
9
+ export * from "@alepha/store";
10
+ export * from "@alepha/topic";