@privateaim/server-kit 0.7.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.
Files changed (154) hide show
  1. package/CHANGELOG.md +205 -0
  2. package/LICENSE +202 -0
  3. package/dist/core/aggregator/index.d.ts +2 -0
  4. package/dist/core/aggregator/index.d.ts.map +1 -0
  5. package/dist/core/aggregator/type.d.ts +4 -0
  6. package/dist/core/aggregator/type.d.ts.map +1 -0
  7. package/dist/core/component/error.d.ts +6 -0
  8. package/dist/core/component/error.d.ts.map +1 -0
  9. package/dist/core/component/helper.d.ts +3 -0
  10. package/dist/core/component/helper.d.ts.map +1 -0
  11. package/dist/core/component/index.d.ts +4 -0
  12. package/dist/core/component/index.d.ts.map +1 -0
  13. package/dist/core/component/type.d.ts +17 -0
  14. package/dist/core/component/type.d.ts.map +1 -0
  15. package/dist/core/index.d.ts +5 -0
  16. package/dist/core/index.d.ts.map +1 -0
  17. package/dist/core/queue/index.d.ts +3 -0
  18. package/dist/core/queue/index.d.ts.map +1 -0
  19. package/dist/core/queue/type.d.ts +12 -0
  20. package/dist/core/queue/type.d.ts.map +1 -0
  21. package/dist/core/queue/utils.d.ts +3 -0
  22. package/dist/core/queue/utils.d.ts.map +1 -0
  23. package/dist/core/queue-router/constants.d.ts +5 -0
  24. package/dist/core/queue-router/constants.d.ts.map +1 -0
  25. package/dist/core/queue-router/helpers.d.ts +4 -0
  26. package/dist/core/queue-router/helpers.d.ts.map +1 -0
  27. package/dist/core/queue-router/index.d.ts +6 -0
  28. package/dist/core/queue-router/index.d.ts.map +1 -0
  29. package/dist/core/queue-router/module.d.ts +9 -0
  30. package/dist/core/queue-router/module.d.ts.map +1 -0
  31. package/dist/core/queue-router/singleton.d.ts +4 -0
  32. package/dist/core/queue-router/singleton.d.ts.map +1 -0
  33. package/dist/core/queue-router/types.d.ts +31 -0
  34. package/dist/core/queue-router/types.d.ts.map +1 -0
  35. package/dist/domain-event/index.d.ts +5 -0
  36. package/dist/domain-event/index.d.ts.map +1 -0
  37. package/dist/domain-event/publish.d.ts +5 -0
  38. package/dist/domain-event/publish.d.ts.map +1 -0
  39. package/dist/domain-event/redis/index.d.ts +2 -0
  40. package/dist/domain-event/redis/index.d.ts.map +1 -0
  41. package/dist/domain-event/redis/publish.d.ts +5 -0
  42. package/dist/domain-event/redis/publish.d.ts.map +1 -0
  43. package/dist/domain-event/socket/index.d.ts +2 -0
  44. package/dist/domain-event/socket/index.d.ts.map +1 -0
  45. package/dist/domain-event/socket/publish.d.ts +5 -0
  46. package/dist/domain-event/socket/publish.d.ts.map +1 -0
  47. package/dist/domain-event/type.d.ts +7 -0
  48. package/dist/domain-event/type.d.ts.map +1 -0
  49. package/dist/domain-event/utils.d.ts +4 -0
  50. package/dist/domain-event/utils.d.ts.map +1 -0
  51. package/dist/index.cjs +492 -0
  52. package/dist/index.cjs.map +1 -0
  53. package/dist/index.d.ts +6 -0
  54. package/dist/index.d.ts.map +1 -0
  55. package/dist/index.mjs +422 -0
  56. package/dist/index.mjs.map +1 -0
  57. package/dist/services/amqp/index.d.ts +2 -0
  58. package/dist/services/amqp/index.d.ts.map +1 -0
  59. package/dist/services/amqp/module.d.ts +6 -0
  60. package/dist/services/amqp/module.d.ts.map +1 -0
  61. package/dist/services/authup/helpers.d.ts +3 -0
  62. package/dist/services/authup/helpers.d.ts.map +1 -0
  63. package/dist/services/authup/index.d.ts +4 -0
  64. package/dist/services/authup/index.d.ts.map +1 -0
  65. package/dist/services/authup/module.d.ts +7 -0
  66. package/dist/services/authup/module.d.ts.map +1 -0
  67. package/dist/services/authup/singleton.d.ts +6 -0
  68. package/dist/services/authup/singleton.d.ts.map +1 -0
  69. package/dist/services/index.d.ts +6 -0
  70. package/dist/services/index.d.ts.map +1 -0
  71. package/dist/services/logger/index.d.ts +4 -0
  72. package/dist/services/logger/index.d.ts.map +1 -0
  73. package/dist/services/logger/module.d.ts +4 -0
  74. package/dist/services/logger/module.d.ts.map +1 -0
  75. package/dist/services/logger/singleton.d.ts +7 -0
  76. package/dist/services/logger/singleton.d.ts.map +1 -0
  77. package/dist/services/logger/types.d.ts +8 -0
  78. package/dist/services/logger/types.d.ts.map +1 -0
  79. package/dist/services/redis/helpers.d.ts +3 -0
  80. package/dist/services/redis/helpers.d.ts.map +1 -0
  81. package/dist/services/redis/index.d.ts +6 -0
  82. package/dist/services/redis/index.d.ts.map +1 -0
  83. package/dist/services/redis/module.d.ts +3 -0
  84. package/dist/services/redis/module.d.ts.map +1 -0
  85. package/dist/services/redis/singleton-publish.d.ts +3 -0
  86. package/dist/services/redis/singleton-publish.d.ts.map +1 -0
  87. package/dist/services/redis/singleton-subscribe.d.ts +3 -0
  88. package/dist/services/redis/singleton-subscribe.d.ts.map +1 -0
  89. package/dist/services/redis/singleton.d.ts +7 -0
  90. package/dist/services/redis/singleton.d.ts.map +1 -0
  91. package/dist/services/vault/index.d.ts +3 -0
  92. package/dist/services/vault/index.d.ts.map +1 -0
  93. package/dist/services/vault/module.d.ts +3 -0
  94. package/dist/services/vault/module.d.ts.map +1 -0
  95. package/dist/services/vault/singleton.d.ts +6 -0
  96. package/dist/services/vault/singleton.d.ts.map +1 -0
  97. package/dist/type.d.ts +2 -0
  98. package/dist/type.d.ts.map +1 -0
  99. package/dist/utils/has-property.d.ts +2 -0
  100. package/dist/utils/has-property.d.ts.map +1 -0
  101. package/dist/utils/index.d.ts +2 -0
  102. package/dist/utils/index.d.ts.map +1 -0
  103. package/package.json +54 -0
  104. package/rollup.config.mjs +14 -0
  105. package/src/core/aggregator/index.ts +8 -0
  106. package/src/core/aggregator/type.ts +10 -0
  107. package/src/core/component/error.ts +18 -0
  108. package/src/core/component/helper.ts +28 -0
  109. package/src/core/component/index.ts +10 -0
  110. package/src/core/component/type.ts +32 -0
  111. package/src/core/index.ts +11 -0
  112. package/src/core/queue/index.ts +9 -0
  113. package/src/core/queue/type.ts +23 -0
  114. package/src/core/queue/utils.ts +17 -0
  115. package/src/core/queue-router/constants.ts +11 -0
  116. package/src/core/queue-router/helpers.ts +33 -0
  117. package/src/core/queue-router/index.ts +12 -0
  118. package/src/core/queue-router/module.ts +111 -0
  119. package/src/core/queue-router/singleton.ts +27 -0
  120. package/src/core/queue-router/types.ts +58 -0
  121. package/src/domain-event/index.ts +11 -0
  122. package/src/domain-event/publish.ts +21 -0
  123. package/src/domain-event/redis/index.ts +8 -0
  124. package/src/domain-event/redis/publish.ts +41 -0
  125. package/src/domain-event/socket/index.ts +8 -0
  126. package/src/domain-event/socket/publish.ts +57 -0
  127. package/src/domain-event/type.ts +14 -0
  128. package/src/domain-event/utils.ts +34 -0
  129. package/src/index.ts +12 -0
  130. package/src/services/amqp/index.ts +8 -0
  131. package/src/services/amqp/module.ts +26 -0
  132. package/src/services/authup/helpers.ts +28 -0
  133. package/src/services/authup/index.ts +10 -0
  134. package/src/services/authup/module.ts +28 -0
  135. package/src/services/authup/singleton.ts +26 -0
  136. package/src/services/index.ts +12 -0
  137. package/src/services/logger/index.ts +10 -0
  138. package/src/services/logger/module.ts +56 -0
  139. package/src/services/logger/singleton.ts +29 -0
  140. package/src/services/logger/types.ts +19 -0
  141. package/src/services/redis/helpers.ts +12 -0
  142. package/src/services/redis/index.ts +12 -0
  143. package/src/services/redis/module.ts +22 -0
  144. package/src/services/redis/singleton-publish.ts +19 -0
  145. package/src/services/redis/singleton-subscribe.ts +19 -0
  146. package/src/services/redis/singleton.ts +30 -0
  147. package/src/services/vault/index.ts +9 -0
  148. package/src/services/vault/module.ts +13 -0
  149. package/src/services/vault/singleton.ts +26 -0
  150. package/src/type.ts +8 -0
  151. package/src/utils/has-property.ts +13 -0
  152. package/src/utils/index.ts +8 -0
  153. package/tsconfig.build.json +11 -0
  154. package/tsconfig.json +9 -0
@@ -0,0 +1,6 @@
1
+ export * from './helpers';
2
+ export * from './module';
3
+ export * from './singleton';
4
+ export * from './singleton-publish';
5
+ export * from './singleton-subscribe';
6
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/services/redis/index.ts"],"names":[],"mappings":"AAOA,cAAc,WAAW,CAAC;AAC1B,cAAc,UAAU,CAAC;AACzB,cAAc,aAAa,CAAC;AAC5B,cAAc,qBAAqB,CAAC;AACpC,cAAc,uBAAuB,CAAC"}
@@ -0,0 +1,3 @@
1
+ import { Cache as RedisCache, Client as RedisClient, ClientOptions as RedisClientOptions, KeyPathID as RedisKeyPathID, createClient as createRedisClient } from 'redis-extension';
2
+ export { createRedisClient, RedisClient, RedisClientOptions, RedisKeyPathID, RedisCache, };
3
+ //# sourceMappingURL=module.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"module.d.ts","sourceRoot":"","sources":["../../../src/services/redis/module.ts"],"names":[],"mappings":"AAOA,OAAO,EACH,KAAK,IAAI,UAAU,EACnB,MAAM,IAAI,WAAW,EACrB,aAAa,IAAI,kBAAkB,EACnC,SAAS,IAAI,cAAc,EAC3B,YAAY,IAAI,iBAAiB,EACpC,MAAM,iBAAiB,CAAC;AAEzB,OAAO,EACH,iBAAiB,EACjB,WAAW,EACX,kBAAkB,EAClB,cAAc,EACd,UAAU,GACb,CAAC"}
@@ -0,0 +1,3 @@
1
+ import type { Client } from 'redis-extension';
2
+ export declare function useRedisPublishClient(): Client;
3
+ //# sourceMappingURL=singleton-publish.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"singleton-publish.d.ts","sourceRoot":"","sources":["../../../src/services/redis/singleton-publish.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAC;AAS9C,wBAAgB,qBAAqB,WAEpC"}
@@ -0,0 +1,3 @@
1
+ import type { Client } from 'redis-extension';
2
+ export declare function useRedisSubscribeClient(): Client;
3
+ //# sourceMappingURL=singleton-subscribe.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"singleton-subscribe.d.ts","sourceRoot":"","sources":["../../../src/services/redis/singleton-subscribe.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAC;AAS9C,wBAAgB,uBAAuB,WAEtC"}
@@ -0,0 +1,7 @@
1
+ import type { Client } from 'redis-extension';
2
+ import type { Factory } from 'singa';
3
+ export declare function setRedisFactory(factory: Factory<Client>): void;
4
+ export declare function isRedisClientUsable(): boolean;
5
+ export declare function setRedisClient(input: Client): void;
6
+ export declare function useRedisClient(): Client;
7
+ //# sourceMappingURL=singleton.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"singleton.d.ts","sourceRoot":"","sources":["../../../src/services/redis/singleton.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAC;AAC9C,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,OAAO,CAAC;AAOrC,wBAAgB,eAAe,CAAC,OAAO,EAAE,OAAO,CAAC,MAAM,CAAC,QAEvD;AAED,wBAAgB,mBAAmB,YAElC;AAED,wBAAgB,cAAc,CAAC,KAAK,EAAE,MAAM,QAE3C;AAED,wBAAgB,cAAc,WAE7B"}
@@ -0,0 +1,3 @@
1
+ export * from './module';
2
+ export * from './singleton';
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/services/vault/index.ts"],"names":[],"mappings":"AAOA,cAAc,UAAU,CAAC;AACzB,cAAc,aAAa,CAAC"}
@@ -0,0 +1,3 @@
1
+ import { VaultClient, createClient as createVaultClient } from '@hapic/vault';
2
+ export { VaultClient, createVaultClient, };
3
+ //# sourceMappingURL=module.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"module.d.ts","sourceRoot":"","sources":["../../../src/services/vault/module.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,WAAW,EAAE,YAAY,IAAI,iBAAiB,EAAE,MAAM,cAAc,CAAC;AAE9E,OAAO,EACH,WAAW,EACX,iBAAiB,GACpB,CAAC"}
@@ -0,0 +1,6 @@
1
+ import type { Factory } from 'singa';
2
+ import type { VaultClient } from '@hapic/vault';
3
+ export declare function setVaultFactory(factory: Factory<VaultClient>): void;
4
+ export declare function isVaultClientUsable(): boolean;
5
+ export declare function useVaultClient(): VaultClient;
6
+ //# sourceMappingURL=singleton.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"singleton.d.ts","sourceRoot":"","sources":["../../../src/services/vault/singleton.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,OAAO,CAAC;AAErC,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAMhD,wBAAgB,eAAe,CAAC,OAAO,EAAE,OAAO,CAAC,WAAW,CAAC,QAE5D;AAED,wBAAgB,mBAAmB,YAElC;AAED,wBAAgB,cAAc,gBAE7B"}
package/dist/type.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export type ObjectLiteral = Record<string, any>;
2
+ //# sourceMappingURL=type.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../src/type.ts"],"names":[],"mappings":"AAOA,MAAM,MAAM,aAAa,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC"}
@@ -0,0 +1,2 @@
1
+ export declare function hasOwnProperty<X extends Record<string, any>, Y extends PropertyKey>(obj: X, prop: Y): obj is X & Record<Y, unknown>;
2
+ //# sourceMappingURL=has-property.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"has-property.d.ts","sourceRoot":"","sources":["../../src/utils/has-property.ts"],"names":[],"mappings":"AAOA,wBAAgB,cAAc,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,CAAC,SAAS,WAAW,EAC/E,GAAG,EAAE,CAAC,EACN,IAAI,EAAE,CAAC,GACR,GAAG,IAAI,CAAC,GAAG,MAAM,CAAC,CAAC,EAAE,OAAO,CAAC,CAE/B"}
@@ -0,0 +1,2 @@
1
+ export * from './has-property';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAOA,cAAc,gBAAgB,CAAC"}
package/package.json ADDED
@@ -0,0 +1,54 @@
1
+ {
2
+ "name": "@privateaim/server-kit",
3
+ "version": "0.7.0",
4
+ "exports": {
5
+ "./package.json": "./package.json",
6
+ ".": {
7
+ "types": "./dist/index.d.ts",
8
+ "import": "./dist/index.mjs",
9
+ "require": "./dist/index.cjs"
10
+ }
11
+ },
12
+ "main": "./dist/index.cjs",
13
+ "module": "./dist/index.mjs",
14
+ "types": "./dist/index.d.ts",
15
+ "author": {
16
+ "name": "Peter Placzek",
17
+ "email": "contact@tada5hi.net",
18
+ "url": "https://tada5hi.net"
19
+ },
20
+ "license": "Apache-2.0",
21
+ "description": "This package contains common constants, functions, types, ... for the server side.",
22
+ "scripts": {
23
+ "build:types": "tsc --emitDeclarationOnly -p tsconfig.build.json",
24
+ "build:js": "rollup -c",
25
+ "build": "rimraf ./dist && cross-env NODE_ENV=production npm run build:js && npm run build:types"
26
+ },
27
+ "dependencies": {
28
+ "@ebec/http": "^2.3.0",
29
+ "@privateaim/core-kit": "^0.7.0",
30
+ "@privateaim/kit": "^0.7.0",
31
+ "@socket.io/redis-emitter": "^5.1.0",
32
+ "envix": "^1.5.0",
33
+ "hapic": "^2.5.1",
34
+ "singa": "^1.0.0",
35
+ "winston": "^3.13.0"
36
+ },
37
+ "devDependencies": {
38
+ "@authup/core-http-kit": "^1.0.0-beta.18",
39
+ "@hapic/vault": "^2.3.3",
40
+ "amqp-extension": "^4.0.0-beta.3",
41
+ "redis-extension": "^1.5.0",
42
+ "typeorm-extension": "^3.5.1"
43
+ },
44
+ "peerDependencies": {
45
+ "@authup/core-http-kit": "^1.0.0-beta.18",
46
+ "@hapic/vault": "^2.3.3",
47
+ "amqp-extension": "^4.0.0-beta.3",
48
+ "redis-extension": "^1.5.0",
49
+ "typeorm-extension": "^3.5.1"
50
+ },
51
+ "publishConfig": {
52
+ "access": "public"
53
+ }
54
+ }
@@ -0,0 +1,14 @@
1
+ /*
2
+ * Copyright (c) 2022.
3
+ * Author Peter Placzek (tada5hi)
4
+ * For the full copyright and license information,
5
+ * view the LICENSE file that was distributed with this source code.
6
+ */
7
+
8
+ import fs from 'node:fs';
9
+
10
+ import { createConfig } from '../../rollup.config.mjs';
11
+
12
+ export default createConfig({
13
+ pkg: JSON.parse(fs.readFileSync(new URL('./package.json', import.meta.url), {encoding: 'utf-8'})),
14
+ });
@@ -0,0 +1,8 @@
1
+ /*
2
+ * Copyright (c) 2023-2024.
3
+ * Author Peter Placzek (tada5hi)
4
+ * For the full copyright and license information,
5
+ * view the LICENSE file that was distributed with this source code.
6
+ */
7
+
8
+ export * from './type';
@@ -0,0 +1,10 @@
1
+ /*
2
+ * Copyright (c) 2023-2024.
3
+ * Author Peter Placzek (tada5hi)
4
+ * For the full copyright and license information,
5
+ * view the LICENSE file that was distributed with this source code.
6
+ */
7
+
8
+ export type Aggregator = {
9
+ start: () => void
10
+ };
@@ -0,0 +1,18 @@
1
+ /*
2
+ * Copyright (c) 2023-2024.
3
+ * Author Peter Placzek (tada5hi)
4
+ * For the full copyright and license information,
5
+ * view the LICENSE file that was distributed with this source code.
6
+ */
7
+
8
+ import type { ComponentErrorOptions } from './type';
9
+
10
+ export class ComponentError extends Error {
11
+ public code?: string | null;
12
+
13
+ constructor(input: ComponentErrorOptions) {
14
+ super(input.message, { cause: input.cause });
15
+
16
+ this.code = input.code;
17
+ }
18
+ }
@@ -0,0 +1,28 @@
1
+ /*
2
+ * Copyright (c) 2024.
3
+ * Author Peter Placzek (tada5hi)
4
+ * For the full copyright and license information,
5
+ * view the LICENSE file that was distributed with this source code.
6
+ */
7
+
8
+ import { isObject } from '@privateaim/kit';
9
+ import type { ComponentError } from './error';
10
+
11
+ export function isComponentError(input: unknown) : input is ComponentError {
12
+ if (!isObject(input)) {
13
+ return false;
14
+ }
15
+
16
+ if (
17
+ typeof input.message !== 'undefined' &&
18
+ typeof input.message !== 'string'
19
+ ) {
20
+ return false;
21
+ }
22
+
23
+ if (typeof input.code === 'undefined') {
24
+ return true;
25
+ }
26
+
27
+ return typeof input.code === 'string' || input.code === null;
28
+ }
@@ -0,0 +1,10 @@
1
+ /*
2
+ * Copyright (c) 2023-2024.
3
+ * Author Peter Placzek (tada5hi)
4
+ * For the full copyright and license information,
5
+ * view the LICENSE file that was distributed with this source code.
6
+ */
7
+
8
+ export * from './error';
9
+ export * from './helper';
10
+ export * from './type';
@@ -0,0 +1,32 @@
1
+ /*
2
+ * Copyright (c) 2023-2024.
3
+ * Author Peter Placzek (tada5hi)
4
+ * For the full copyright and license information,
5
+ * view the LICENSE file that was distributed with this source code.
6
+ */
7
+
8
+ import type { ObjectLiteral } from '../../type';
9
+ import type { ComponentError } from './error';
10
+
11
+ export type Component = {
12
+ start: () => void
13
+ };
14
+
15
+ export type ComponentErrorOptions = {
16
+ code?: string | null,
17
+ message?: string
18
+ cause?: unknown
19
+ };
20
+
21
+ export type ComponentContextWithError<
22
+ T extends ObjectLiteral = ObjectLiteral,
23
+ > = T & {
24
+ error: ComponentError | Error
25
+ };
26
+
27
+ export type ComponentContextWithCommand<
28
+ T extends ObjectLiteral = ObjectLiteral,
29
+ C extends string = string,
30
+ > = Omit<T, 'command'> & {
31
+ command: C
32
+ };
@@ -0,0 +1,11 @@
1
+ /*
2
+ * Copyright (c) 2024.
3
+ * Author Peter Placzek (tada5hi)
4
+ * For the full copyright and license information,
5
+ * view the LICENSE file that was distributed with this source code.
6
+ */
7
+
8
+ export * from './aggregator';
9
+ export * from './component';
10
+ export * from './queue';
11
+ export * from './queue-router';
@@ -0,0 +1,9 @@
1
+ /*
2
+ * Copyright (c) 2023-2024.
3
+ * Author Peter Placzek (tada5hi)
4
+ * For the full copyright and license information,
5
+ * view the LICENSE file that was distributed with this source code.
6
+ */
7
+
8
+ export * from './type';
9
+ export * from './utils';
@@ -0,0 +1,23 @@
1
+ /*
2
+ * Copyright (c) 2023-2024.
3
+ * Author Peter Placzek (tada5hi)
4
+ * For the full copyright and license information,
5
+ * view the LICENSE file that was distributed with this source code.
6
+ */
7
+
8
+ import type { ObjectLiteral } from '../../type';
9
+
10
+ export type QueuePayloadError = {
11
+ code: string,
12
+ message: string,
13
+ step?: string | number,
14
+ };
15
+
16
+ export type QueuePayload<
17
+ T extends ObjectLiteral = ObjectLiteral,
18
+ M extends ObjectLiteral = ObjectLiteral,
19
+ > = {
20
+ data: T,
21
+ metadata: M,
22
+ error?: QueuePayloadError
23
+ };
@@ -0,0 +1,17 @@
1
+ /*
2
+ * Copyright (c) 2023-2024.
3
+ * Author Peter Placzek (tada5hi)
4
+ * For the full copyright and license information,
5
+ * view the LICENSE file that was distributed with this source code.
6
+ */
7
+
8
+ import { hasOwnProperty, isObject } from '@privateaim/kit';
9
+ import type { QueuePayload } from './type';
10
+
11
+ export function isQueuePayload(input: unknown) : input is QueuePayload {
12
+ return isObject(input) &&
13
+ hasOwnProperty(input, 'data') &&
14
+ isObject(input.data) &&
15
+ hasOwnProperty(input, 'metadata') &&
16
+ isObject(input.metadata);
17
+ }
@@ -0,0 +1,11 @@
1
+ /*
2
+ * Copyright (c) 2024.
3
+ * Author Peter Placzek (tada5hi)
4
+ * For the full copyright and license information,
5
+ * view the LICENSE file that was distributed with this source code.
6
+ */
7
+
8
+ export enum QueueRouterRoutingType {
9
+ WORK = 'work',
10
+ PUB_SUB = 'pubSub',
11
+ }
@@ -0,0 +1,33 @@
1
+ /*
2
+ * Copyright (c) 2024.
3
+ * Author Peter Placzek (tada5hi)
4
+ * For the full copyright and license information,
5
+ * view the LICENSE file that was distributed with this source code.
6
+ */
7
+
8
+ import { createNanoID, hasOwnProperty, isObject } from '@privateaim/kit';
9
+ import type { QueueRouterPayload, QueueRouterPayloadInput } from './types';
10
+
11
+ export function buildQueueRouterPublishPayload(
12
+ input: QueueRouterPayloadInput,
13
+ ) : QueueRouterPayload {
14
+ return {
15
+ id: input.id || createNanoID(),
16
+ type: input.type,
17
+ data: input.data || {},
18
+ metadata: {
19
+ timestamp: Date.now(),
20
+ ...input.metadata,
21
+ },
22
+ };
23
+ }
24
+
25
+ export function isQueueRouterPayload(input: unknown) : input is QueueRouterPayload {
26
+ return isObject(input) &&
27
+ typeof input.id === 'string' &&
28
+ typeof input.type === 'string' &&
29
+ hasOwnProperty(input, 'data') &&
30
+ isObject(input.data) &&
31
+ hasOwnProperty(input, 'metadata') &&
32
+ isObject(input.metadata);
33
+ }
@@ -0,0 +1,12 @@
1
+ /*
2
+ * Copyright (c) 2024.
3
+ * Author Peter Placzek (tada5hi)
4
+ * For the full copyright and license information,
5
+ * view the LICENSE file that was distributed with this source code.
6
+ */
7
+
8
+ export * from './constants';
9
+ export * from './helpers';
10
+ export * from './module';
11
+ export * from './singleton';
12
+ export * from './types';
@@ -0,0 +1,111 @@
1
+ /*
2
+ * Copyright (c) 2024.
3
+ * Author Peter Placzek (tada5hi)
4
+ * For the full copyright and license information,
5
+ * view the LICENSE file that was distributed with this source code.
6
+ */
7
+
8
+ import { ExchangeType } from 'amqp-extension';
9
+ import type { Client } from 'amqp-extension';
10
+ import { isLoggerUsable, useLogger } from '../../services';
11
+ import { QueueRouterRoutingType } from './constants';
12
+ import { isQueueRouterPayload } from './helpers';
13
+ import type {
14
+ QueueRouterHandler,
15
+ QueueRouterHandlers,
16
+ QueueRouterPayload, QueueRouterRouting,
17
+ } from './types';
18
+
19
+ export class QueueRouter {
20
+ protected driver : Client;
21
+
22
+ //----------------------------------------------------------------
23
+
24
+ constructor(driver: Client) {
25
+ this.driver = driver;
26
+ }
27
+
28
+ //----------------------------------------------------------------
29
+
30
+ publish(message: QueueRouterPayload) : Promise<boolean> {
31
+ let exchange : Client;
32
+ if (message.metadata.routing.type === 'work') {
33
+ exchange = this.driver.of({
34
+ type: ExchangeType.DIRECT,
35
+ name: message.metadata.routing.namespace || '',
36
+ });
37
+ } else {
38
+ exchange = this.driver.of({
39
+ type: ExchangeType.TOPIC,
40
+ name: message.metadata.routing.namespace || 'FLAME',
41
+ });
42
+ }
43
+
44
+ if (isLoggerUsable()) {
45
+ useLogger()
46
+ .debug(`Publishing queue message ${message.type} in ${message.metadata.routing.key}`);
47
+ }
48
+
49
+ return exchange.publish(message.metadata.routing.key, message, {
50
+ type: message.type,
51
+ messageId: message.id,
52
+ persistent: message.metadata.persistent ??
53
+ message.metadata.routing.type === QueueRouterRoutingType.WORK,
54
+ });
55
+ }
56
+
57
+ consume(routing: QueueRouterRouting, handlers: QueueRouterHandlers) : Promise<void> {
58
+ let exchange : Client;
59
+ if (routing.type === 'work') {
60
+ exchange = this.driver.of({
61
+ type: ExchangeType.DIRECT,
62
+ name: routing.namespace || '',
63
+ });
64
+ } else {
65
+ exchange = this.driver.of({
66
+ type: ExchangeType.TOPIC,
67
+ name: routing.namespace || 'FLAME',
68
+ });
69
+ }
70
+
71
+ return exchange.consume(routing.key, {
72
+ prefetchCount: routing.type === QueueRouterRoutingType.WORK ? 1 : undefined,
73
+ // noAck: routing.type !== QueueRouterRoutingType.WORK,
74
+ requeueOnFailure: routing.type === QueueRouterRoutingType.WORK,
75
+ }, {
76
+ $any: async (input) => {
77
+ const payload = JSON.parse(input.content.toString('utf-8'));
78
+ if (!isQueueRouterPayload(payload)) {
79
+ return;
80
+ }
81
+
82
+ if (isLoggerUsable()) {
83
+ useLogger()
84
+ .debug(`Consuming queue message ${input.properties.type} in ${routing.key}`);
85
+ }
86
+
87
+ let handler : QueueRouterHandler | undefined;
88
+
89
+ if (
90
+ typeof input.properties.type === 'string' &&
91
+ handlers[input.properties.type]
92
+ ) {
93
+ handler = handlers[input.properties.type];
94
+ } else {
95
+ handler = handlers.$any;
96
+ }
97
+
98
+ if (typeof handler !== 'function') {
99
+ if (isLoggerUsable()) {
100
+ useLogger()
101
+ .debug(`No queue handler to consume message ${input.properties.type} in ${routing.key}`);
102
+ }
103
+
104
+ return;
105
+ }
106
+
107
+ await handler(payload);
108
+ },
109
+ });
110
+ }
111
+ }
@@ -0,0 +1,27 @@
1
+ /*
2
+ * Copyright (c) 2024.
3
+ * Author Peter Placzek (tada5hi)
4
+ * For the full copyright and license information,
5
+ * view the LICENSE file that was distributed with this source code.
6
+ */
7
+
8
+ import { singa } from 'singa';
9
+ import { isAmqpClientUsable, useAmqpClient } from '../../services';
10
+ import { QueueRouter } from './module';
11
+
12
+ const instance = singa<QueueRouter>({
13
+ name: 'queueRouter',
14
+ factory: () => {
15
+ const amqp = useAmqpClient();
16
+
17
+ return new QueueRouter(amqp);
18
+ },
19
+ });
20
+
21
+ export function isQueueRouterUsable() {
22
+ return instance.has() || isAmqpClientUsable();
23
+ }
24
+
25
+ export function useQueueRouter() {
26
+ return instance.use();
27
+ }
@@ -0,0 +1,58 @@
1
+ /*
2
+ * Copyright (c) 2024.
3
+ * Author Peter Placzek (tada5hi)
4
+ * For the full copyright and license information,
5
+ * view the LICENSE file that was distributed with this source code.
6
+ */
7
+
8
+ // QueueRouterPublishPayload
9
+ // QueueRouterPublishContext
10
+
11
+ // QueueRouterConsumePayload (metadata.routingKey, metadata.exchange, metadata.redelivered)
12
+ // QueueRouterConsumeContext
13
+
14
+ import type { QueueRouterRoutingType } from './constants';
15
+
16
+ export type QueueRouterRouting = {
17
+ type: `${QueueRouterRoutingType}`,
18
+ key: string,
19
+ namespace?: string,
20
+ };
21
+
22
+ export type QueueRouterPayloadMetadata = {
23
+ timestamp: number,
24
+ redelivered?: boolean,
25
+ persistent?: boolean,
26
+ routing: QueueRouterRouting
27
+ };
28
+
29
+ export type QueueRouterPayload<
30
+ T = string,
31
+ D = Record<string, any>,
32
+ > = {
33
+ id: string,
34
+ type: T,
35
+ data: D,
36
+ metadata: QueueRouterPayloadMetadata
37
+ };
38
+
39
+ export type QueueRouterPayloadInput<T = string, D = Record<string, any>> = {
40
+ id?: string,
41
+ type: T,
42
+ data?: D,
43
+ metadata: Partial<Pick<QueueRouterPayloadMetadata, 'timestamp'>> &
44
+ Omit<QueueRouterPayloadMetadata, 'timestamp'>
45
+ };
46
+
47
+ export type QueueRouterHandler<
48
+ T = string,
49
+ D = Record<string, any>,
50
+ > = (message: QueueRouterPayload<T, D>) => Promise<void> | void;
51
+
52
+ export type QueueRouterHandlers<
53
+ R extends Record<string, Record<string, any>> = Record<string, Record<string, any>>,
54
+ > = {
55
+ $any?: QueueRouterHandler
56
+ } & {
57
+ [K in keyof R]: QueueRouterHandler<K, R[K]>
58
+ };
@@ -0,0 +1,11 @@
1
+ /*
2
+ * Copyright (c) 2023-2024.
3
+ * Author Peter Placzek (tada5hi)
4
+ * For the full copyright and license information,
5
+ * view the LICENSE file that was distributed with this source code.
6
+ */
7
+
8
+ export * from './publish';
9
+ export * from './redis';
10
+ export * from './socket';
11
+ export * from './type';
@@ -0,0 +1,21 @@
1
+ /*
2
+ * Copyright (c) 2023-2024.
3
+ * Author Peter Placzek (tada5hi)
4
+ * For the full copyright and license information,
5
+ * view the LICENSE file that was distributed with this source code.
6
+ */
7
+
8
+ import type { DomainsEventContext } from '@privateaim/core-kit';
9
+ import type { Client } from 'redis-extension';
10
+ import { publishDomainRedisEvent } from './redis';
11
+ import { publishDomainSocketEvent } from './socket';
12
+ import type { DomainEventDestinations } from './type';
13
+
14
+ export async function publishDomainEvent(
15
+ client: Client,
16
+ context: DomainsEventContext,
17
+ destinations: DomainEventDestinations,
18
+ ) {
19
+ await publishDomainRedisEvent(client, context, destinations);
20
+ publishDomainSocketEvent(client, context, destinations);
21
+ }
@@ -0,0 +1,8 @@
1
+ /*
2
+ * Copyright (c) 2023-2024.
3
+ * Author Peter Placzek (tada5hi)
4
+ * For the full copyright and license information,
5
+ * view the LICENSE file that was distributed with this source code.
6
+ */
7
+
8
+ export * from './publish';