alepha 0.2.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/dist/index.cjs CHANGED
@@ -1,47 +1,173 @@
1
1
  'use strict';
2
2
 
3
+ var cache = require('@alepha/cache');
3
4
  var core = require('@alepha/core');
4
- var messaging = require('@alepha/messaging');
5
- var orm = require('@alepha/orm');
5
+ var store = require('@alepha/store');
6
+ var postgres = require('@alepha/postgres');
7
+ var queue = require('@alepha/queue');
6
8
  var redis = require('@alepha/redis');
9
+ var scheduler = require('@alepha/scheduler');
7
10
  var server = require('@alepha/server');
8
- var store = require('@alepha/store');
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;
9
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
+ }
10
107
 
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);
11
116
 
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
+ });
12
126
  Object.keys(core).forEach(function (k) {
13
- if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
14
- enumerable: true,
15
- get: function () { return core[k]; }
16
- });
17
- });
18
- Object.keys(messaging).forEach(function (k) {
19
- if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
20
- enumerable: true,
21
- get: function () { return messaging[k]; }
22
- });
23
- });
24
- Object.keys(orm).forEach(function (k) {
25
- if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
26
- enumerable: true,
27
- get: function () { return orm[k]; }
28
- });
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
+ });
29
149
  });
30
150
  Object.keys(redis).forEach(function (k) {
31
- if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
32
- enumerable: true,
33
- get: function () { return redis[k]; }
34
- });
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
+ });
35
161
  });
36
162
  Object.keys(server).forEach(function (k) {
37
- if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
38
- enumerable: true,
39
- get: function () { return server[k]; }
40
- });
163
+ if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
164
+ enumerable: true,
165
+ get: function () { return server[k]; }
166
+ });
41
167
  });
42
- Object.keys(store).forEach(function (k) {
43
- if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
44
- enumerable: true,
45
- get: function () { return store[k]; }
46
- });
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
+ });
47
173
  });
package/dist/index.d.cts CHANGED
@@ -1,6 +1,76 @@
1
+ export * from '@alepha/cache';
2
+ import * as _alepha_core from '@alepha/core';
3
+ import { AsyncFn, DurationLike, KIND, Alepha, DateTimeProvider, DateTime } from '@alepha/core';
1
4
  export * from '@alepha/core';
2
- export * from '@alepha/messaging';
3
- export * from '@alepha/orm';
5
+ import { StoreProvider } from '@alepha/store';
6
+ export * from '@alepha/store';
7
+ export * from '@alepha/postgres';
8
+ export * from '@alepha/queue';
4
9
  export * from '@alepha/redis';
10
+ export * from '@alepha/scheduler';
5
11
  export * from '@alepha/server';
6
- export * from '@alepha/store';
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;
74
+ }
75
+
76
+ export { type LockDescriptorOptions, LockDescriptorProvider, type LockDescriptorValue, type LockObject, lock, lockDescriptorKey };
package/dist/index.d.mts CHANGED
@@ -1,6 +1,76 @@
1
+ export * from '@alepha/cache';
2
+ import * as _alepha_core from '@alepha/core';
3
+ import { AsyncFn, DurationLike, KIND, Alepha, DateTimeProvider, DateTime } from '@alepha/core';
1
4
  export * from '@alepha/core';
2
- export * from '@alepha/messaging';
3
- export * from '@alepha/orm';
5
+ import { StoreProvider } from '@alepha/store';
6
+ export * from '@alepha/store';
7
+ export * from '@alepha/postgres';
8
+ export * from '@alepha/queue';
4
9
  export * from '@alepha/redis';
10
+ export * from '@alepha/scheduler';
5
11
  export * from '@alepha/server';
6
- export * from '@alepha/store';
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;
74
+ }
75
+
76
+ export { type LockDescriptorOptions, LockDescriptorProvider, type LockDescriptorValue, type LockObject, lock, lockDescriptorKey };
package/dist/index.mjs CHANGED
@@ -1,6 +1,117 @@
1
+ export * from '@alepha/cache';
2
+ import { KIND, autoInject, createDescriptorValue, inject, Alepha, DateTimeProvider, logger, hook, updateDescriptorValue, DateTime } from '@alepha/core';
1
3
  export * from '@alepha/core';
2
- export * from '@alepha/messaging';
3
- export * from '@alepha/orm';
4
+ import { StoreProvider, StoreModule } from '@alepha/store';
5
+ export * from '@alepha/store';
6
+ export * from '@alepha/postgres';
7
+ export * from '@alepha/queue';
4
8
  export * from '@alepha/redis';
9
+ export * from '@alepha/scheduler';
5
10
  export * from '@alepha/server';
6
- export * from '@alepha/store';
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,18 +1,21 @@
1
1
  {
2
2
  "name": "alepha",
3
- "version": "0.2.0",
3
+ "version": "0.3.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/core": "0.2.0",
11
- "@alepha/messaging": "0.2.0",
12
- "@alepha/orm": "0.2.0",
13
- "@alepha/redis": "0.2.0",
14
- "@alepha/server": "0.2.0",
15
- "@alepha/store": "0.2.0"
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"
16
19
  },
17
20
  "devDependencies": {
18
21
  "pkgroll": "^2.6.0",
package/src/index.ts CHANGED
@@ -1,6 +1,10 @@
1
+ export * from "@alepha/cache";
1
2
  export * from "@alepha/core";
2
- export * from "@alepha/messaging";
3
- export * from "@alepha/orm";
3
+ export * from "@alepha/lock";
4
+ export * from "@alepha/postgres";
5
+ export * from "@alepha/queue";
4
6
  export * from "@alepha/redis";
7
+ export * from "@alepha/scheduler";
5
8
  export * from "@alepha/server";
6
9
  export * from "@alepha/store";
10
+ export * from "@alepha/topic";