alepha 0.3.0 → 0.4.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/README.md +2 -24
- package/dist/index.cjs +52 -143
- package/dist/index.d.cts +3 -68
- package/dist/index.d.mts +3 -68
- package/dist/index.mjs +3 -109
- package/package.json +14 -12
- package/src/index.ts +2 -1
package/README.md
CHANGED
|
@@ -1,29 +1,7 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Alepha
|
|
2
2
|
|
|
3
3
|
## Installation
|
|
4
4
|
|
|
5
5
|
```bash
|
|
6
|
-
npm install
|
|
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
|
|
6
|
+
npm install alepha
|
|
29
7
|
```
|
package/dist/index.cjs
CHANGED
|
@@ -2,172 +2,81 @@
|
|
|
2
2
|
|
|
3
3
|
var cache = require('@alepha/cache');
|
|
4
4
|
var core = require('@alepha/core');
|
|
5
|
-
var
|
|
5
|
+
var datastore = require('@alepha/datastore');
|
|
6
|
+
var lock = require('@alepha/lock');
|
|
6
7
|
var postgres = require('@alepha/postgres');
|
|
7
8
|
var queue = require('@alepha/queue');
|
|
8
9
|
var redis = require('@alepha/redis');
|
|
9
10
|
var scheduler = require('@alepha/scheduler');
|
|
11
|
+
var security = require('@alepha/security');
|
|
10
12
|
var server = require('@alepha/server');
|
|
11
13
|
var topic = require('@alepha/topic');
|
|
12
14
|
|
|
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
15
|
|
|
108
|
-
class LockModule {
|
|
109
|
-
alepha = core.inject(core.Alepha);
|
|
110
|
-
constructor() {
|
|
111
|
-
this.alepha.register(store.StoreModule);
|
|
112
|
-
this.alepha.register(LockDescriptorProvider);
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
core.autoInject(lock, LockModule);
|
|
116
16
|
|
|
117
|
-
exports.LockDescriptorProvider = LockDescriptorProvider;
|
|
118
|
-
exports.lock = lock;
|
|
119
|
-
exports.lockDescriptorKey = lockDescriptorKey;
|
|
120
17
|
Object.keys(cache).forEach(function (k) {
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
18
|
+
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
|
19
|
+
enumerable: true,
|
|
20
|
+
get: function () { return cache[k]; }
|
|
21
|
+
});
|
|
125
22
|
});
|
|
126
23
|
Object.keys(core).forEach(function (k) {
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
24
|
+
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
|
25
|
+
enumerable: true,
|
|
26
|
+
get: function () { return core[k]; }
|
|
27
|
+
});
|
|
131
28
|
});
|
|
132
|
-
Object.keys(
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
29
|
+
Object.keys(datastore).forEach(function (k) {
|
|
30
|
+
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
|
31
|
+
enumerable: true,
|
|
32
|
+
get: function () { return datastore[k]; }
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
Object.keys(lock).forEach(function (k) {
|
|
36
|
+
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
|
37
|
+
enumerable: true,
|
|
38
|
+
get: function () { return lock[k]; }
|
|
39
|
+
});
|
|
137
40
|
});
|
|
138
41
|
Object.keys(postgres).forEach(function (k) {
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
42
|
+
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
|
43
|
+
enumerable: true,
|
|
44
|
+
get: function () { return postgres[k]; }
|
|
45
|
+
});
|
|
143
46
|
});
|
|
144
47
|
Object.keys(queue).forEach(function (k) {
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
48
|
+
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
|
49
|
+
enumerable: true,
|
|
50
|
+
get: function () { return queue[k]; }
|
|
51
|
+
});
|
|
149
52
|
});
|
|
150
53
|
Object.keys(redis).forEach(function (k) {
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
54
|
+
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
|
55
|
+
enumerable: true,
|
|
56
|
+
get: function () { return redis[k]; }
|
|
57
|
+
});
|
|
155
58
|
});
|
|
156
59
|
Object.keys(scheduler).forEach(function (k) {
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
60
|
+
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
|
61
|
+
enumerable: true,
|
|
62
|
+
get: function () { return scheduler[k]; }
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
Object.keys(security).forEach(function (k) {
|
|
66
|
+
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
|
67
|
+
enumerable: true,
|
|
68
|
+
get: function () { return security[k]; }
|
|
69
|
+
});
|
|
161
70
|
});
|
|
162
71
|
Object.keys(server).forEach(function (k) {
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
72
|
+
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
|
73
|
+
enumerable: true,
|
|
74
|
+
get: function () { return server[k]; }
|
|
75
|
+
});
|
|
167
76
|
});
|
|
168
77
|
Object.keys(topic).forEach(function (k) {
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
78
|
+
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
|
79
|
+
enumerable: true,
|
|
80
|
+
get: function () { return topic[k]; }
|
|
81
|
+
});
|
|
173
82
|
});
|
package/dist/index.d.cts
CHANGED
|
@@ -1,76 +1,11 @@
|
|
|
1
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
2
|
export * from '@alepha/core';
|
|
5
|
-
|
|
6
|
-
export * from '@alepha/
|
|
3
|
+
export * from '@alepha/datastore';
|
|
4
|
+
export * from '@alepha/lock';
|
|
7
5
|
export * from '@alepha/postgres';
|
|
8
6
|
export * from '@alepha/queue';
|
|
9
7
|
export * from '@alepha/redis';
|
|
10
8
|
export * from '@alepha/scheduler';
|
|
9
|
+
export * from '@alepha/security';
|
|
11
10
|
export * from '@alepha/server';
|
|
12
11
|
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;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
export { type LockDescriptorOptions, LockDescriptorProvider, type LockDescriptorValue, type LockObject, lock, lockDescriptorKey };
|
package/dist/index.d.mts
CHANGED
|
@@ -1,76 +1,11 @@
|
|
|
1
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
2
|
export * from '@alepha/core';
|
|
5
|
-
|
|
6
|
-
export * from '@alepha/
|
|
3
|
+
export * from '@alepha/datastore';
|
|
4
|
+
export * from '@alepha/lock';
|
|
7
5
|
export * from '@alepha/postgres';
|
|
8
6
|
export * from '@alepha/queue';
|
|
9
7
|
export * from '@alepha/redis';
|
|
10
8
|
export * from '@alepha/scheduler';
|
|
9
|
+
export * from '@alepha/security';
|
|
11
10
|
export * from '@alepha/server';
|
|
12
11
|
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;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
export { type LockDescriptorOptions, LockDescriptorProvider, type LockDescriptorValue, type LockObject, lock, lockDescriptorKey };
|
package/dist/index.mjs
CHANGED
|
@@ -1,117 +1,11 @@
|
|
|
1
1
|
export * from '@alepha/cache';
|
|
2
|
-
import { KIND, autoInject, createDescriptorValue, inject, Alepha, DateTimeProvider, logger, hook, updateDescriptorValue, DateTime } from '@alepha/core';
|
|
3
2
|
export * from '@alepha/core';
|
|
4
|
-
|
|
5
|
-
export * from '@alepha/
|
|
3
|
+
export * from '@alepha/datastore';
|
|
4
|
+
export * from '@alepha/lock';
|
|
6
5
|
export * from '@alepha/postgres';
|
|
7
6
|
export * from '@alepha/queue';
|
|
8
7
|
export * from '@alepha/redis';
|
|
9
8
|
export * from '@alepha/scheduler';
|
|
9
|
+
export * from '@alepha/security';
|
|
10
10
|
export * from '@alepha/server';
|
|
11
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);
|
|
110
|
-
constructor() {
|
|
111
|
-
this.alepha.register(StoreModule);
|
|
112
|
-
this.alepha.register(LockDescriptorProvider);
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
autoInject(lock, LockModule);
|
|
116
|
-
|
|
117
|
-
export { LockDescriptorProvider, lock, lockDescriptorKey };
|
package/package.json
CHANGED
|
@@ -1,26 +1,28 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "alepha",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
7
7
|
"module": "./dist/index.mjs",
|
|
8
8
|
"types": "./dist/index.d.cts",
|
|
9
9
|
"dependencies": {
|
|
10
|
-
"@alepha/cache": "0.
|
|
11
|
-
"@alepha/core": "0.
|
|
12
|
-
"@alepha/
|
|
13
|
-
"@alepha/
|
|
14
|
-
"@alepha/
|
|
15
|
-
"@alepha/
|
|
16
|
-
"@alepha/
|
|
17
|
-
"@alepha/
|
|
18
|
-
"@alepha/
|
|
10
|
+
"@alepha/cache": "0.4.0",
|
|
11
|
+
"@alepha/core": "0.4.0",
|
|
12
|
+
"@alepha/datastore": "0.4.0",
|
|
13
|
+
"@alepha/lock": "0.4.0",
|
|
14
|
+
"@alepha/postgres": "0.4.0",
|
|
15
|
+
"@alepha/queue": "0.4.0",
|
|
16
|
+
"@alepha/redis": "0.4.0",
|
|
17
|
+
"@alepha/scheduler": "0.4.0",
|
|
18
|
+
"@alepha/security": "0.4.0",
|
|
19
|
+
"@alepha/server": "0.4.0",
|
|
20
|
+
"@alepha/testing": "0.4.0",
|
|
21
|
+
"@alepha/topic": "0.4.0"
|
|
19
22
|
},
|
|
20
23
|
"devDependencies": {
|
|
21
24
|
"pkgroll": "^2.6.0",
|
|
22
|
-
"typescript": "^5.7.2"
|
|
23
|
-
"vitest": "^2.1.8"
|
|
25
|
+
"typescript": "^5.7.2"
|
|
24
26
|
},
|
|
25
27
|
"scripts": {
|
|
26
28
|
"build": "pkgroll --clean-dist"
|
package/src/index.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
export * from "@alepha/cache";
|
|
2
2
|
export * from "@alepha/core";
|
|
3
|
+
export * from "@alepha/datastore";
|
|
3
4
|
export * from "@alepha/lock";
|
|
4
5
|
export * from "@alepha/postgres";
|
|
5
6
|
export * from "@alepha/queue";
|
|
6
7
|
export * from "@alepha/redis";
|
|
7
8
|
export * from "@alepha/scheduler";
|
|
9
|
+
export * from "@alepha/security";
|
|
8
10
|
export * from "@alepha/server";
|
|
9
|
-
export * from "@alepha/store";
|
|
10
11
|
export * from "@alepha/topic";
|