@palmetto/pubsub 1.1.0 → 2.0.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 +8 -8
- package/dist/interfaces.d.ts +17 -17
- package/dist/interfaces.js +7 -7
- package/dist/publisher.js +8 -4
- package/dist/subscriber.js +1 -1
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -27,7 +27,7 @@ yarn add @palmetto/pubsub zod
|
|
|
27
27
|
1. Create a publisher and send a message
|
|
28
28
|
|
|
29
29
|
```ts
|
|
30
|
-
import { z
|
|
30
|
+
import { z } from "zod";
|
|
31
31
|
import {
|
|
32
32
|
IdMetaSchema,
|
|
33
33
|
Publisher,
|
|
@@ -39,7 +39,7 @@ yarn add @palmetto/pubsub zod
|
|
|
39
39
|
const rabbitMqPublisher = new RabbitMqPublisher(...);
|
|
40
40
|
|
|
41
41
|
const schema = IdMetaSchema.extend({
|
|
42
|
-
message:
|
|
42
|
+
message: z.string(),
|
|
43
43
|
});
|
|
44
44
|
|
|
45
45
|
const Model = z.infer<typeof schema>;
|
|
@@ -48,7 +48,7 @@ yarn add @palmetto/pubsub zod
|
|
|
48
48
|
name: "TestModel",
|
|
49
49
|
schema: TestModelSchema,
|
|
50
50
|
transport: RABBITMQ_TRANSPORT,
|
|
51
|
-
|
|
51
|
+
exchangeType: "topic",
|
|
52
52
|
};
|
|
53
53
|
|
|
54
54
|
const publisher = new Publisher(console, [rabbitMqPublisher]);
|
|
@@ -65,7 +65,7 @@ yarn add @palmetto/pubsub zod
|
|
|
65
65
|
1. Create subscriber and subscriber to a message
|
|
66
66
|
|
|
67
67
|
```ts
|
|
68
|
-
import { z
|
|
68
|
+
import { z } from "zod";
|
|
69
69
|
import {
|
|
70
70
|
BaseMessage,
|
|
71
71
|
IdMetaSchema,
|
|
@@ -79,8 +79,8 @@ yarn add @palmetto/pubsub zod
|
|
|
79
79
|
const rabbitMqSubscriber = new RabbitMqSubscriber(...);
|
|
80
80
|
|
|
81
81
|
const schema = IdMetaSchema.extend({
|
|
82
|
-
message:
|
|
83
|
-
code:
|
|
82
|
+
message: z.string(),
|
|
83
|
+
code: z.string(),
|
|
84
84
|
});
|
|
85
85
|
|
|
86
86
|
const Model = z.infer<typeof schema>;
|
|
@@ -91,7 +91,7 @@ yarn add @palmetto/pubsub zod
|
|
|
91
91
|
retryDelay: 1_000,
|
|
92
92
|
retries: 20,
|
|
93
93
|
transport: RABBITMQ_TRANSPORT,
|
|
94
|
-
|
|
94
|
+
exchangeType: "fanout",
|
|
95
95
|
};
|
|
96
96
|
|
|
97
97
|
function onMessage(message: Model) {
|
|
@@ -116,7 +116,7 @@ For specific details about using BullMQ see [BullMQ README.md](src/bullmq/README
|
|
|
116
116
|
|
|
117
117
|
### Defining schemas
|
|
118
118
|
|
|
119
|
-
All schemas must be defined using `zod
|
|
119
|
+
All schemas must be defined using `zod` v4.1+. By convention, all messages should extend the `IdMetaSchema` schema, but it is not strictly required.
|
|
120
120
|
|
|
121
121
|
IdMetaSchema provides several fields useful for message tracking:
|
|
122
122
|
|
package/dist/interfaces.d.ts
CHANGED
|
@@ -1,22 +1,22 @@
|
|
|
1
|
-
import { z
|
|
2
|
-
export declare const MetaSchema:
|
|
3
|
-
createdAt:
|
|
4
|
-
schemaId:
|
|
5
|
-
publishedBy:
|
|
6
|
-
},
|
|
7
|
-
export declare const IdMetaSchema:
|
|
8
|
-
id:
|
|
9
|
-
meta:
|
|
10
|
-
createdAt:
|
|
11
|
-
schemaId:
|
|
12
|
-
publishedBy:
|
|
13
|
-
},
|
|
14
|
-
},
|
|
15
|
-
export type Meta =
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
export declare const MetaSchema: z.ZodObject<{
|
|
3
|
+
createdAt: z.ZodISODateTime;
|
|
4
|
+
schemaId: z.ZodString;
|
|
5
|
+
publishedBy: z.ZodString;
|
|
6
|
+
}, z.core.$strip>;
|
|
7
|
+
export declare const IdMetaSchema: z.ZodObject<{
|
|
8
|
+
id: z.ZodOptional<z.ZodString>;
|
|
9
|
+
meta: z.ZodOptional<z.ZodObject<{
|
|
10
|
+
createdAt: z.ZodISODateTime;
|
|
11
|
+
schemaId: z.ZodString;
|
|
12
|
+
publishedBy: z.ZodString;
|
|
13
|
+
}, z.core.$strip>>;
|
|
14
|
+
}, z.core.$strip>;
|
|
15
|
+
export type Meta = z.infer<typeof MetaSchema>;
|
|
16
16
|
/**
|
|
17
17
|
* All messages should be based on this schema
|
|
18
18
|
*/
|
|
19
|
-
export type BaseMessage =
|
|
19
|
+
export type BaseMessage = z.infer<typeof IdMetaSchema>;
|
|
20
20
|
/**
|
|
21
21
|
* All pubsub publishers must implement this interface
|
|
22
22
|
*/
|
|
@@ -37,7 +37,7 @@ export interface PubSubConfiguration {
|
|
|
37
37
|
/**
|
|
38
38
|
* The schema for the message - the message is verfied during publish and subscribe
|
|
39
39
|
*/
|
|
40
|
-
schema:
|
|
40
|
+
schema: z.ZodType;
|
|
41
41
|
/**
|
|
42
42
|
* The message transport to use (defined by the event-pubsub implementations)
|
|
43
43
|
*/
|
package/dist/interfaces.js
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.MessageResult = exports.IdMetaSchema = exports.MetaSchema = void 0;
|
|
4
|
-
const
|
|
5
|
-
exports.MetaSchema =
|
|
6
|
-
createdAt:
|
|
7
|
-
schemaId:
|
|
8
|
-
publishedBy:
|
|
4
|
+
const zod_1 = require("zod");
|
|
5
|
+
exports.MetaSchema = zod_1.z.object({
|
|
6
|
+
createdAt: zod_1.z.iso.datetime(),
|
|
7
|
+
schemaId: zod_1.z.string(),
|
|
8
|
+
publishedBy: zod_1.z.string(),
|
|
9
9
|
});
|
|
10
|
-
exports.IdMetaSchema =
|
|
11
|
-
id:
|
|
10
|
+
exports.IdMetaSchema = zod_1.z.object({
|
|
11
|
+
id: zod_1.z.string().optional(),
|
|
12
12
|
meta: exports.MetaSchema.optional(),
|
|
13
13
|
});
|
|
14
14
|
var MessageResult;
|
package/dist/publisher.js
CHANGED
|
@@ -10,18 +10,22 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.Publisher = void 0;
|
|
13
|
-
const
|
|
13
|
+
const zod_1 = require("zod");
|
|
14
14
|
const uuid_1 = require("uuid");
|
|
15
15
|
const crypto_1 = require("crypto");
|
|
16
16
|
const errors_js_1 = require("./errors.js");
|
|
17
17
|
class Publisher {
|
|
18
18
|
constructor(logger, providers) {
|
|
19
|
+
var _a, _b;
|
|
19
20
|
this.logger = logger;
|
|
20
21
|
this.hashes = new Map();
|
|
21
22
|
this.publisherProviders = new Map();
|
|
22
23
|
if (providers) {
|
|
23
24
|
providers.forEach((provider) => this.publisherProviders.set(provider.transport, provider));
|
|
24
25
|
}
|
|
26
|
+
(_b = (_a = this.logger).debug) === null || _b === void 0 ? void 0 : _b.call(_a, `Publisher initialized with providers: ${[
|
|
27
|
+
...this.publisherProviders.keys(),
|
|
28
|
+
].join(", ")}`);
|
|
25
29
|
}
|
|
26
30
|
addProvider(provider) {
|
|
27
31
|
var _a, _b;
|
|
@@ -64,12 +68,12 @@ class Publisher {
|
|
|
64
68
|
else {
|
|
65
69
|
message.meta.schemaId = schemaId;
|
|
66
70
|
}
|
|
67
|
-
const
|
|
68
|
-
const check = schema.safeParse(JSON.parse(json));
|
|
71
|
+
const check = schema.safeEncode(message);
|
|
69
72
|
if (!check.success) {
|
|
70
73
|
throw new errors_js_1.SchemaValidationError(`Schema did not accept the published message: ${check.error.message}`);
|
|
71
74
|
}
|
|
72
75
|
(_b = (_a = this.logger).debug) === null || _b === void 0 ? void 0 : _b.call(_a, `Publisher publishing message for ${provider.transport}`);
|
|
76
|
+
const json = JSON.stringify(check.data);
|
|
73
77
|
yield provider.publish(config, json);
|
|
74
78
|
});
|
|
75
79
|
}
|
|
@@ -92,7 +96,7 @@ class Publisher {
|
|
|
92
96
|
const { schema } = config;
|
|
93
97
|
let hash = this.hashes.get(schema);
|
|
94
98
|
if (!hash) {
|
|
95
|
-
const jsonSchema = JSON.stringify(
|
|
99
|
+
const jsonSchema = JSON.stringify(zod_1.z.toJSONSchema(schema, { io: "input" }), null, 3);
|
|
96
100
|
hash = (0, crypto_1.createHash)("sha256").update(jsonSchema).digest("hex");
|
|
97
101
|
this.hashes.set(schema, hash);
|
|
98
102
|
}
|
package/dist/subscriber.js
CHANGED
|
@@ -57,7 +57,7 @@ class Subscriber {
|
|
|
57
57
|
var _a;
|
|
58
58
|
const start = new Date().valueOf();
|
|
59
59
|
const jsonObject = JSON.parse(jsonStr);
|
|
60
|
-
const r = schema.
|
|
60
|
+
const r = schema.safeDecode(jsonObject);
|
|
61
61
|
if (!r.success) {
|
|
62
62
|
const id = interfaces_js_1.IdMetaSchema.safeParse(jsonObject);
|
|
63
63
|
if (id.success) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@palmetto/pubsub",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"main": "./dist/main.js",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"lint": "yarn run -T eslint --fix ./src",
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
"ts-node": "^10.9.2",
|
|
29
29
|
"typescript": "^5.8.3",
|
|
30
30
|
"vitest": "^3.2.4",
|
|
31
|
-
"zod": "^4.
|
|
31
|
+
"zod": "^4.1.13"
|
|
32
32
|
},
|
|
33
33
|
"files": [
|
|
34
34
|
"dist/**/*",
|
|
@@ -47,6 +47,6 @@
|
|
|
47
47
|
"amqp-connection-manager": "^4.1.14",
|
|
48
48
|
"amqplib": "^0.10.8",
|
|
49
49
|
"bullmq": "^5.58.0",
|
|
50
|
-
"zod": "^
|
|
50
|
+
"zod": "^4.1.13"
|
|
51
51
|
}
|
|
52
52
|
}
|