@mastra/google-cloud-pubsub 0.0.0-1.x-tester-20251106055847
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/CHANGELOG.md +877 -0
- package/LICENSE.md +15 -0
- package/dist/index.cjs +140 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +138 -0
- package/dist/index.js.map +1 -0
- package/package.json +66 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# Apache License 2.0
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Kepler Software, Inc.
|
|
4
|
+
|
|
5
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
you may not use this file except in compliance with the License.
|
|
7
|
+
You may obtain a copy of the License at
|
|
8
|
+
|
|
9
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
|
|
11
|
+
Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
See the License for the specific language governing permissions and
|
|
15
|
+
limitations under the License.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var pubsub = require('@google-cloud/pubsub');
|
|
4
|
+
var events = require('@mastra/core/events');
|
|
5
|
+
|
|
6
|
+
// src/index.ts
|
|
7
|
+
var GoogleCloudPubSub = class extends events.PubSub {
|
|
8
|
+
instanceId;
|
|
9
|
+
pubsub;
|
|
10
|
+
ackBuffer = {};
|
|
11
|
+
activeSubscriptions = {};
|
|
12
|
+
activeCbs = {};
|
|
13
|
+
constructor(config) {
|
|
14
|
+
super();
|
|
15
|
+
this.pubsub = new pubsub.PubSub(config);
|
|
16
|
+
this.instanceId = crypto.randomUUID();
|
|
17
|
+
}
|
|
18
|
+
getSubscriptionName(topic) {
|
|
19
|
+
return `${topic}-${this.instanceId}`;
|
|
20
|
+
}
|
|
21
|
+
async ackMessage(topic, message) {
|
|
22
|
+
try {
|
|
23
|
+
const ackResponse = Promise.race([message.ackWithResponse(), new Promise((resolve) => setTimeout(resolve, 5e3))]);
|
|
24
|
+
this.ackBuffer[topic + "-" + message.id] = ackResponse.catch(() => {
|
|
25
|
+
});
|
|
26
|
+
await ackResponse;
|
|
27
|
+
delete this.ackBuffer[topic + "-" + message.id];
|
|
28
|
+
} catch (e) {
|
|
29
|
+
console.error("Error acking message", e);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
async init(topicName) {
|
|
33
|
+
try {
|
|
34
|
+
await this.pubsub.createTopic(topicName);
|
|
35
|
+
} catch (error) {
|
|
36
|
+
}
|
|
37
|
+
try {
|
|
38
|
+
const [sub] = await this.pubsub.topic(topicName).createSubscription(this.getSubscriptionName(topicName), {
|
|
39
|
+
enableMessageOrdering: true,
|
|
40
|
+
enableExactlyOnceDelivery: topicName === "workflows" ? true : false
|
|
41
|
+
});
|
|
42
|
+
this.activeSubscriptions[topicName] = sub;
|
|
43
|
+
return sub;
|
|
44
|
+
} catch (error) {
|
|
45
|
+
}
|
|
46
|
+
return void 0;
|
|
47
|
+
}
|
|
48
|
+
async destroy(topicName) {
|
|
49
|
+
const subName = this.getSubscriptionName(topicName);
|
|
50
|
+
delete this.activeSubscriptions[topicName];
|
|
51
|
+
this.pubsub.subscription(subName).removeAllListeners();
|
|
52
|
+
await this.pubsub.subscription(subName).close();
|
|
53
|
+
await this.pubsub.subscription(subName).delete();
|
|
54
|
+
await this.pubsub.topic(topicName).delete();
|
|
55
|
+
}
|
|
56
|
+
async publish(topicName, event) {
|
|
57
|
+
if (topicName.startsWith("workflow.events.")) {
|
|
58
|
+
const parts = topicName.split(".");
|
|
59
|
+
if (parts[parts.length - 2] === "v2") {
|
|
60
|
+
topicName = "workflow.events.v2";
|
|
61
|
+
} else {
|
|
62
|
+
topicName = "workflow.events.v1";
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
let topic = this.pubsub.topic(topicName);
|
|
66
|
+
try {
|
|
67
|
+
await topic.publishMessage({
|
|
68
|
+
data: Buffer.from(JSON.stringify(event)),
|
|
69
|
+
orderingKey: "workflows"
|
|
70
|
+
});
|
|
71
|
+
} catch (e) {
|
|
72
|
+
if (e.code === 5) {
|
|
73
|
+
await this.pubsub.createTopic(topicName);
|
|
74
|
+
await this.publish(topicName, event);
|
|
75
|
+
} else {
|
|
76
|
+
throw e;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
async subscribe(topic, cb) {
|
|
81
|
+
if (topic.startsWith("workflow.events.")) {
|
|
82
|
+
const parts = topic.split(".");
|
|
83
|
+
if (parts[parts.length - 2] === "v2") {
|
|
84
|
+
topic = "workflow.events.v2";
|
|
85
|
+
} else {
|
|
86
|
+
topic = "workflow.events.v1";
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
const subscription = this.activeSubscriptions[topic] ?? await this.init(topic);
|
|
90
|
+
if (!subscription) {
|
|
91
|
+
throw new Error(`Failed to subscribe to topic: ${topic}`);
|
|
92
|
+
}
|
|
93
|
+
this.activeSubscriptions[topic] = subscription;
|
|
94
|
+
const activeCbs = this.activeCbs[topic] ?? /* @__PURE__ */ new Set();
|
|
95
|
+
activeCbs.add(cb);
|
|
96
|
+
this.activeCbs[topic] = activeCbs;
|
|
97
|
+
if (subscription.isOpen) {
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
subscription.on("message", async (message) => {
|
|
101
|
+
const event = JSON.parse(message.data.toString());
|
|
102
|
+
event.id = message.id;
|
|
103
|
+
event.createdAt = message.publishTime;
|
|
104
|
+
try {
|
|
105
|
+
const activeCbs2 = this.activeCbs[topic] ?? [];
|
|
106
|
+
for (const cb2 of activeCbs2) {
|
|
107
|
+
cb2(event, async () => {
|
|
108
|
+
try {
|
|
109
|
+
await this.ackMessage(topic, message);
|
|
110
|
+
} catch (e) {
|
|
111
|
+
console.error("Error acking message", e);
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
} catch (error) {
|
|
116
|
+
console.error("Error processing event", error);
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
subscription.on("error", async (error) => {
|
|
120
|
+
console.error("subscription error", error);
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
async unsubscribe(topic, cb) {
|
|
124
|
+
const subscription = this.activeSubscriptions[topic] ?? this.pubsub.subscription(this.getSubscriptionName(topic));
|
|
125
|
+
const activeCbs = this.activeCbs[topic] ?? /* @__PURE__ */ new Set();
|
|
126
|
+
activeCbs.delete(cb);
|
|
127
|
+
this.activeCbs[topic] = activeCbs;
|
|
128
|
+
if (activeCbs.size === 0) {
|
|
129
|
+
subscription.removeListener("message", cb);
|
|
130
|
+
await subscription.close();
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
async flush() {
|
|
134
|
+
await Promise.all(Object.values(this.ackBuffer));
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
exports.GoogleCloudPubSub = GoogleCloudPubSub;
|
|
139
|
+
//# sourceMappingURL=index.cjs.map
|
|
140
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"names":["PubSub","PubSubClient","activeCbs","cb"],"mappings":";;;;;;AAKO,IAAM,iBAAA,GAAN,cAAgCA,aAAA,CAAO;AAAA,EACpC,UAAA;AAAA,EACA,MAAA;AAAA,EACA,YAA0C,EAAC;AAAA,EAC3C,sBAAoD,EAAC;AAAA,EACrD,YAAmF,EAAC;AAAA,EAE5F,YAAY,MAAA,EAAsB;AAChC,IAAA,KAAA,EAAM;AACN,IAAA,IAAA,CAAK,MAAA,GAAS,IAAIC,aAAA,CAAa,MAAM,CAAA;AACrC,IAAA,IAA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I;AACnD,IAAA,SAAA,CAAU,OAAO,EAAE,CAAA;AACnB,IAAA,IAAA,CAAK,SAAA,CAAU,KAAK,CAAA,GAAI,SAAA;AAExB,IAAA,IAAI,SAAA,CAAU,SAAS,CAAA,EAAG;AACxB,MAAA,YAAA,CAAa,cAAA,CAAe,WAAW,EAAE,CAAA;AACzC,MAAA,MAAM,aAAa,KAAA,EAAM;AAAA,IAC3B;AAAA,EACF;AAAA,EAEA,MAAM,KAAA,GAAuB;AAC3B,IAAA,MAAM,QAAQ,GAAA,CAAI,MAAA,CAAO,MAAA,CAAO,IAAA,CAAK,SAAS,CAAC,CAAA;AAAA,EACjD;AACF","file":"index.cjs","sourcesContent":["import { PubSub as PubSubClient } from '@google-cloud/pubsub';\nimport type { ClientConfig, Message, Subscription } from '@google-cloud/pubsub';\nimport { PubSub } from '@mastra/core/events';\nimport type { Event } from '@mastra/core/events';\n\nexport class GoogleCloudPubSub extends PubSub {\n private instanceId: string;\n private pubsub: PubSubClient;\n private ackBuffer: Record<string, Promise<any>> = {};\n private activeSubscriptions: Record<string, Subscription> = {};\n private activeCbs: Record<string, Set<(event: Event, ack: () => Promise<void>) => void>> = {};\n\n constructor(config: ClientConfig) {\n super();\n this.pubsub = new PubSubClient(config);\n this.instanceId = crypto.randomUUID();\n }\n\n getSubscriptionName(topic: string) {\n return `${topic}-${this.instanceId}`;\n }\n\n async ackMessage(topic: string, message: Message) {\n try {\n const ackResponse = Promise.race([message.ackWithResponse(), new Promise(resolve => setTimeout(resolve, 5000))]);\n this.ackBuffer[topic + '-' + message.id] = ackResponse.catch(() => {});\n await ackResponse;\n delete this.ackBuffer[topic + '-' + message.id];\n } catch (e) {\n console.error('Error acking message', e);\n }\n }\n\n async init(topicName: string) {\n try {\n await this.pubsub.createTopic(topicName);\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n } catch (error) {\n // no-op\n }\n try {\n const [sub] = await this.pubsub.topic(topicName).createSubscription(this.getSubscriptionName(topicName), {\n enableMessageOrdering: true,\n enableExactlyOnceDelivery: topicName === 'workflows' ? true : false,\n });\n this.activeSubscriptions[topicName] = sub;\n return sub;\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n } catch (error) {\n // no-op\n }\n\n return undefined;\n }\n\n async destroy(topicName: string) {\n const subName = this.getSubscriptionName(topicName);\n delete this.activeSubscriptions[topicName];\n this.pubsub.subscription(subName).removeAllListeners();\n await this.pubsub.subscription(subName).close();\n await this.pubsub.subscription(subName).delete();\n await this.pubsub.topic(topicName).delete();\n }\n\n async publish(topicName: string, event: Omit<Event, 'id' | 'createdAt'>): Promise<void> {\n if (topicName.startsWith('workflow.events.')) {\n const parts = topicName.split('.');\n if (parts[parts.length - 2] === 'v2') {\n topicName = 'workflow.events.v2';\n } else {\n topicName = 'workflow.events.v1';\n }\n }\n\n let topic = this.pubsub.topic(topicName);\n\n try {\n await topic.publishMessage({\n data: Buffer.from(JSON.stringify(event)),\n orderingKey: 'workflows',\n });\n } catch (e: any) {\n if (e.code === 5) {\n await this.pubsub.createTopic(topicName);\n await this.publish(topicName, event);\n } else {\n throw e;\n }\n }\n }\n\n async subscribe(topic: string, cb: (event: Event, ack?: () => Promise<void>) => void): Promise<void> {\n if (topic.startsWith('workflow.events.')) {\n const parts = topic.split('.');\n if (parts[parts.length - 2] === 'v2') {\n topic = 'workflow.events.v2';\n } else {\n topic = 'workflow.events.v1';\n }\n }\n\n // Update tracked callbacks\n const subscription = this.activeSubscriptions[topic] ?? (await this.init(topic));\n if (!subscription) {\n throw new Error(`Failed to subscribe to topic: ${topic}`);\n }\n\n this.activeSubscriptions[topic] = subscription;\n\n const activeCbs = this.activeCbs[topic] ?? new Set();\n activeCbs.add(cb);\n this.activeCbs[topic] = activeCbs;\n\n if (subscription.isOpen) {\n return;\n }\n\n subscription.on('message', async message => {\n const event = JSON.parse(message.data.toString()) as Event;\n event.id = message.id;\n event.createdAt = message.publishTime;\n\n try {\n const activeCbs = this.activeCbs[topic] ?? [];\n for (const cb of activeCbs) {\n cb(event, async () => {\n try {\n await this.ackMessage(topic, message);\n } catch (e) {\n console.error('Error acking message', e);\n }\n });\n }\n } catch (error) {\n console.error('Error processing event', error);\n }\n });\n\n subscription.on('error', async error => {\n // if (error.code === 5) {\n // await this.init(topic);\n // } else {\n // // TODO: determine if other errors require re-subscription\n // // console.error('subscription error, retrying in 5 seconds', error);\n // // await new Promise(resolve => setTimeout(resolve, 5000));\n // // await this.subscribe(topic, cb);\n // console.error('subscription error', error);\n // }\n console.error('subscription error', error);\n });\n }\n\n async unsubscribe(topic: string, cb: (event: Event, ack?: () => Promise<void>) => void): Promise<void> {\n const subscription = this.activeSubscriptions[topic] ?? this.pubsub.subscription(this.getSubscriptionName(topic));\n const activeCbs = this.activeCbs[topic] ?? new Set();\n activeCbs.delete(cb);\n this.activeCbs[topic] = activeCbs;\n\n if (activeCbs.size === 0) {\n subscription.removeListener('message', cb);\n await subscription.close();\n }\n }\n\n async flush(): Promise<void> {\n await Promise.all(Object.values(this.ackBuffer));\n }\n}\n"]}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { ClientConfig, Message, Subscription } from '@google-cloud/pubsub';
|
|
2
|
+
import { PubSub } from '@mastra/core/events';
|
|
3
|
+
import type { Event } from '@mastra/core/events';
|
|
4
|
+
export declare class GoogleCloudPubSub extends PubSub {
|
|
5
|
+
private instanceId;
|
|
6
|
+
private pubsub;
|
|
7
|
+
private ackBuffer;
|
|
8
|
+
private activeSubscriptions;
|
|
9
|
+
private activeCbs;
|
|
10
|
+
constructor(config: ClientConfig);
|
|
11
|
+
getSubscriptionName(topic: string): string;
|
|
12
|
+
ackMessage(topic: string, message: Message): Promise<void>;
|
|
13
|
+
init(topicName: string): Promise<Subscription | undefined>;
|
|
14
|
+
destroy(topicName: string): Promise<void>;
|
|
15
|
+
publish(topicName: string, event: Omit<Event, 'id' | 'createdAt'>): Promise<void>;
|
|
16
|
+
subscribe(topic: string, cb: (event: Event, ack?: () => Promise<void>) => void): Promise<void>;
|
|
17
|
+
unsubscribe(topic: string, cb: (event: Event, ack?: () => Promise<void>) => void): Promise<void>;
|
|
18
|
+
flush(): Promise<void>;
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AAChF,OAAO,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAC7C,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,qBAAqB,CAAC;AAEjD,qBAAa,iBAAkB,SAAQ,MAAM;IAC3C,OAAO,CAAC,UAAU,CAAS;IAC3B,OAAO,CAAC,MAAM,CAAe;IAC7B,OAAO,CAAC,SAAS,CAAoC;IACrD,OAAO,CAAC,mBAAmB,CAAoC;IAC/D,OAAO,CAAC,SAAS,CAA6E;gBAElF,MAAM,EAAE,YAAY;IAMhC,mBAAmB,CAAC,KAAK,EAAE,MAAM;IAI3B,UAAU,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO;IAW1C,IAAI,CAAC,SAAS,EAAE,MAAM;IAsBtB,OAAO,CAAC,SAAS,EAAE,MAAM;IASzB,OAAO,CAAC,SAAS,EAAE,MAAM,EAAE,KAAK,EAAE,IAAI,CAAC,KAAK,EAAE,IAAI,GAAG,WAAW,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC;IA2BjF,SAAS,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE,EAAE,CAAC,KAAK,EAAE,KAAK,EAAE,GAAG,CAAC,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;IA6D9F,WAAW,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE,EAAE,CAAC,KAAK,EAAE,KAAK,EAAE,GAAG,CAAC,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;IAYhG,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;CAG7B"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { PubSub as PubSub$1 } from '@google-cloud/pubsub';
|
|
2
|
+
import { PubSub } from '@mastra/core/events';
|
|
3
|
+
|
|
4
|
+
// src/index.ts
|
|
5
|
+
var GoogleCloudPubSub = class extends PubSub {
|
|
6
|
+
instanceId;
|
|
7
|
+
pubsub;
|
|
8
|
+
ackBuffer = {};
|
|
9
|
+
activeSubscriptions = {};
|
|
10
|
+
activeCbs = {};
|
|
11
|
+
constructor(config) {
|
|
12
|
+
super();
|
|
13
|
+
this.pubsub = new PubSub$1(config);
|
|
14
|
+
this.instanceId = crypto.randomUUID();
|
|
15
|
+
}
|
|
16
|
+
getSubscriptionName(topic) {
|
|
17
|
+
return `${topic}-${this.instanceId}`;
|
|
18
|
+
}
|
|
19
|
+
async ackMessage(topic, message) {
|
|
20
|
+
try {
|
|
21
|
+
const ackResponse = Promise.race([message.ackWithResponse(), new Promise((resolve) => setTimeout(resolve, 5e3))]);
|
|
22
|
+
this.ackBuffer[topic + "-" + message.id] = ackResponse.catch(() => {
|
|
23
|
+
});
|
|
24
|
+
await ackResponse;
|
|
25
|
+
delete this.ackBuffer[topic + "-" + message.id];
|
|
26
|
+
} catch (e) {
|
|
27
|
+
console.error("Error acking message", e);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
async init(topicName) {
|
|
31
|
+
try {
|
|
32
|
+
await this.pubsub.createTopic(topicName);
|
|
33
|
+
} catch (error) {
|
|
34
|
+
}
|
|
35
|
+
try {
|
|
36
|
+
const [sub] = await this.pubsub.topic(topicName).createSubscription(this.getSubscriptionName(topicName), {
|
|
37
|
+
enableMessageOrdering: true,
|
|
38
|
+
enableExactlyOnceDelivery: topicName === "workflows" ? true : false
|
|
39
|
+
});
|
|
40
|
+
this.activeSubscriptions[topicName] = sub;
|
|
41
|
+
return sub;
|
|
42
|
+
} catch (error) {
|
|
43
|
+
}
|
|
44
|
+
return void 0;
|
|
45
|
+
}
|
|
46
|
+
async destroy(topicName) {
|
|
47
|
+
const subName = this.getSubscriptionName(topicName);
|
|
48
|
+
delete this.activeSubscriptions[topicName];
|
|
49
|
+
this.pubsub.subscription(subName).removeAllListeners();
|
|
50
|
+
await this.pubsub.subscription(subName).close();
|
|
51
|
+
await this.pubsub.subscription(subName).delete();
|
|
52
|
+
await this.pubsub.topic(topicName).delete();
|
|
53
|
+
}
|
|
54
|
+
async publish(topicName, event) {
|
|
55
|
+
if (topicName.startsWith("workflow.events.")) {
|
|
56
|
+
const parts = topicName.split(".");
|
|
57
|
+
if (parts[parts.length - 2] === "v2") {
|
|
58
|
+
topicName = "workflow.events.v2";
|
|
59
|
+
} else {
|
|
60
|
+
topicName = "workflow.events.v1";
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
let topic = this.pubsub.topic(topicName);
|
|
64
|
+
try {
|
|
65
|
+
await topic.publishMessage({
|
|
66
|
+
data: Buffer.from(JSON.stringify(event)),
|
|
67
|
+
orderingKey: "workflows"
|
|
68
|
+
});
|
|
69
|
+
} catch (e) {
|
|
70
|
+
if (e.code === 5) {
|
|
71
|
+
await this.pubsub.createTopic(topicName);
|
|
72
|
+
await this.publish(topicName, event);
|
|
73
|
+
} else {
|
|
74
|
+
throw e;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
async subscribe(topic, cb) {
|
|
79
|
+
if (topic.startsWith("workflow.events.")) {
|
|
80
|
+
const parts = topic.split(".");
|
|
81
|
+
if (parts[parts.length - 2] === "v2") {
|
|
82
|
+
topic = "workflow.events.v2";
|
|
83
|
+
} else {
|
|
84
|
+
topic = "workflow.events.v1";
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
const subscription = this.activeSubscriptions[topic] ?? await this.init(topic);
|
|
88
|
+
if (!subscription) {
|
|
89
|
+
throw new Error(`Failed to subscribe to topic: ${topic}`);
|
|
90
|
+
}
|
|
91
|
+
this.activeSubscriptions[topic] = subscription;
|
|
92
|
+
const activeCbs = this.activeCbs[topic] ?? /* @__PURE__ */ new Set();
|
|
93
|
+
activeCbs.add(cb);
|
|
94
|
+
this.activeCbs[topic] = activeCbs;
|
|
95
|
+
if (subscription.isOpen) {
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
subscription.on("message", async (message) => {
|
|
99
|
+
const event = JSON.parse(message.data.toString());
|
|
100
|
+
event.id = message.id;
|
|
101
|
+
event.createdAt = message.publishTime;
|
|
102
|
+
try {
|
|
103
|
+
const activeCbs2 = this.activeCbs[topic] ?? [];
|
|
104
|
+
for (const cb2 of activeCbs2) {
|
|
105
|
+
cb2(event, async () => {
|
|
106
|
+
try {
|
|
107
|
+
await this.ackMessage(topic, message);
|
|
108
|
+
} catch (e) {
|
|
109
|
+
console.error("Error acking message", e);
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
} catch (error) {
|
|
114
|
+
console.error("Error processing event", error);
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
subscription.on("error", async (error) => {
|
|
118
|
+
console.error("subscription error", error);
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
async unsubscribe(topic, cb) {
|
|
122
|
+
const subscription = this.activeSubscriptions[topic] ?? this.pubsub.subscription(this.getSubscriptionName(topic));
|
|
123
|
+
const activeCbs = this.activeCbs[topic] ?? /* @__PURE__ */ new Set();
|
|
124
|
+
activeCbs.delete(cb);
|
|
125
|
+
this.activeCbs[topic] = activeCbs;
|
|
126
|
+
if (activeCbs.size === 0) {
|
|
127
|
+
subscription.removeListener("message", cb);
|
|
128
|
+
await subscription.close();
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
async flush() {
|
|
132
|
+
await Promise.all(Object.values(this.ackBuffer));
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
export { GoogleCloudPubSub };
|
|
137
|
+
//# sourceMappingURL=index.js.map
|
|
138
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"names":["PubSubClient","activeCbs","cb"],"mappings":";;;;AAKO,IAAM,iBAAA,GAAN,cAAgC,MAAA,CAAO;AAAA,EACpC,UAAA;AAAA,EACA,MAAA;AAAA,EACA,YAA0C,EAAC;AAAA,EAC3C,sBAAoD,EAAC;AAAA,EACrD,YAAmF,EAAC;AAAA,EAE5F,YAAY,MAAA,EAAsB;AAChC,IAAA,KAAA,EAAM;AACN,IAAA,IAAA,CAAK,MAAA,GAAS,IAAIA,QAAA,CAAa,MAAM,CAAA;AACrC,IAAA,IAAA,CAAK,UAAA,GAAa,OAAO,UAAA,EAAW;AAAA,EACtC;AAAA,EAEA,oBAAoB,KAAA,EAAe;AACjC,IAAA,OAAO,CAAA,EAAG,KAAK,CAAA,CAAA,EAAI,IAAA,CAAK,UAAU,CAAA,CAAA;AAAA,EACpC;AAAA,EAEA,MAAM,UAAA,CAAW,KAAA,EAAe,OAAA,EAAkB;AAChD,IAAA,IAAI;AACF,MAAA,MAAM,WAAA,GAAc,OAAA,CAAQ,IAAA,CAAK,CAAC,QAAQ,eAAA,EAAgB,EAAG,IAAI,OAAA,CAAQ,aAAW,UAAA,CAAW,OAAA,EAAS,GAAI,CAAC,CAAC,CAAC,CAAA;AAC/G,MAAA,IAAA,CAAK,SAAA,CAAU,QAAQ,GAAA,GAAM,OAAA,CAAQ,EAAE,CAAA,GAAI,WAAA,CAAY,MAAM,MAAM;AAAA,MAAC,CAAC,CAAA;AACrE,MAAA,MAAM,WAAA;AACN,MAAA,OAAO,IAAA,CAAK,SAAA,CAAU,KAAA,GAAQ,GAAA,GAAM,QAAQ,EAAE,CAAA;AAAA,IAChD,SAAS,CAAA,EAA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AACd,QAAA,OAAA,CAAQ,KAAA,CAAM,0BAA0B,KAAK,CAAA;AAAA,MAC/C;AAAA,IACF,CAAC,CAAA;AAED,IAAA,YAAA,CAAa,EAAA,CAAG,OAAA,EAAS,OAAM,KAAA,KAAS;AAUtC,MAAA,OAAA,CAAQ,KAAA,CAAM,sBAAsB,KAAK,CAAA;AAAA,IAC3C,CAAC,CAAA;AAAA,EACH;AAAA,EAEA,MAAM,WAAA,CAAY,KAAA,EAAe,EAAA,EAAsE;AACrG,IAAA,MAAM,YAAA,GAAe,IAAA,CAAK,mBAAA,CAAoB,KAAK,CAAA,IAAK,IAAA,CAAK,MAAA,CAAO,YAAA,CAAa,IAAA,CAAK,mBAAA,CAAoB,KAAK,CAAC,CAAA;AAChH,IAAA,MAAM,YAAY,IAAA,CAAK,SAAA,CAAU,KAAK,CAAA,wBAAS,GAAA,EAAI;AACnD,IAAA,SAAA,CAAU,OAAO,EAAE,CAAA;AACnB,IAAA,IAAA,CAAK,SAAA,CAAU,KAAK,CAAA,GAAI,SAAA;AAExB,IAAA,IAAI,SAAA,CAAU,SAAS,CAAA,EAAG;AACxB,MAAA,YAAA,CAAa,cAAA,CAAe,WAAW,EAAE,CAAA;AACzC,MAAA,MAAM,aAAa,KAAA,EAAM;AAAA,IAC3B;AAAA,EACF;AAAA,EAEA,MAAM,KAAA,GAAuB;AAC3B,IAAA,MAAM,QAAQ,GAAA,CAAI,MAAA,CAAO,MAAA,CAAO,IAAA,CAAK,SAAS,CAAC,CAAA;AAAA,EACjD;AACF","file":"index.js","sourcesContent":["import { PubSub as PubSubClient } from '@google-cloud/pubsub';\nimport type { ClientConfig, Message, Subscription } from '@google-cloud/pubsub';\nimport { PubSub } from '@mastra/core/events';\nimport type { Event } from '@mastra/core/events';\n\nexport class GoogleCloudPubSub extends PubSub {\n private instanceId: string;\n private pubsub: PubSubClient;\n private ackBuffer: Record<string, Promise<any>> = {};\n private activeSubscriptions: Record<string, Subscription> = {};\n private activeCbs: Record<string, Set<(event: Event, ack: () => Promise<void>) => void>> = {};\n\n constructor(config: ClientConfig) {\n super();\n this.pubsub = new PubSubClient(config);\n this.instanceId = crypto.randomUUID();\n }\n\n getSubscriptionName(topic: string) {\n return `${topic}-${this.instanceId}`;\n }\n\n async ackMessage(topic: string, message: Message) {\n try {\n const ackResponse = Promise.race([message.ackWithResponse(), new Promise(resolve => setTimeout(resolve, 5000))]);\n this.ackBuffer[topic + '-' + message.id] = ackResponse.catch(() => {});\n await ackResponse;\n delete this.ackBuffer[topic + '-' + message.id];\n } catch (e) {\n console.error('Error acking message', e);\n }\n }\n\n async init(topicName: string) {\n try {\n await this.pubsub.createTopic(topicName);\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n } catch (error) {\n // no-op\n }\n try {\n const [sub] = await this.pubsub.topic(topicName).createSubscription(this.getSubscriptionName(topicName), {\n enableMessageOrdering: true,\n enableExactlyOnceDelivery: topicName === 'workflows' ? true : false,\n });\n this.activeSubscriptions[topicName] = sub;\n return sub;\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n } catch (error) {\n // no-op\n }\n\n return undefined;\n }\n\n async destroy(topicName: string) {\n const subName = this.getSubscriptionName(topicName);\n delete this.activeSubscriptions[topicName];\n this.pubsub.subscription(subName).removeAllListeners();\n await this.pubsub.subscription(subName).close();\n await this.pubsub.subscription(subName).delete();\n await this.pubsub.topic(topicName).delete();\n }\n\n async publish(topicName: string, event: Omit<Event, 'id' | 'createdAt'>): Promise<void> {\n if (topicName.startsWith('workflow.events.')) {\n const parts = topicName.split('.');\n if (parts[parts.length - 2] === 'v2') {\n topicName = 'workflow.events.v2';\n } else {\n topicName = 'workflow.events.v1';\n }\n }\n\n let topic = this.pubsub.topic(topicName);\n\n try {\n await topic.publishMessage({\n data: Buffer.from(JSON.stringify(event)),\n orderingKey: 'workflows',\n });\n } catch (e: any) {\n if (e.code === 5) {\n await this.pubsub.createTopic(topicName);\n await this.publish(topicName, event);\n } else {\n throw e;\n }\n }\n }\n\n async subscribe(topic: string, cb: (event: Event, ack?: () => Promise<void>) => void): Promise<void> {\n if (topic.startsWith('workflow.events.')) {\n const parts = topic.split('.');\n if (parts[parts.length - 2] === 'v2') {\n topic = 'workflow.events.v2';\n } else {\n topic = 'workflow.events.v1';\n }\n }\n\n // Update tracked callbacks\n const subscription = this.activeSubscriptions[topic] ?? (await this.init(topic));\n if (!subscription) {\n throw new Error(`Failed to subscribe to topic: ${topic}`);\n }\n\n this.activeSubscriptions[topic] = subscription;\n\n const activeCbs = this.activeCbs[topic] ?? new Set();\n activeCbs.add(cb);\n this.activeCbs[topic] = activeCbs;\n\n if (subscription.isOpen) {\n return;\n }\n\n subscription.on('message', async message => {\n const event = JSON.parse(message.data.toString()) as Event;\n event.id = message.id;\n event.createdAt = message.publishTime;\n\n try {\n const activeCbs = this.activeCbs[topic] ?? [];\n for (const cb of activeCbs) {\n cb(event, async () => {\n try {\n await this.ackMessage(topic, message);\n } catch (e) {\n console.error('Error acking message', e);\n }\n });\n }\n } catch (error) {\n console.error('Error processing event', error);\n }\n });\n\n subscription.on('error', async error => {\n // if (error.code === 5) {\n // await this.init(topic);\n // } else {\n // // TODO: determine if other errors require re-subscription\n // // console.error('subscription error, retrying in 5 seconds', error);\n // // await new Promise(resolve => setTimeout(resolve, 5000));\n // // await this.subscribe(topic, cb);\n // console.error('subscription error', error);\n // }\n console.error('subscription error', error);\n });\n }\n\n async unsubscribe(topic: string, cb: (event: Event, ack?: () => Promise<void>) => void): Promise<void> {\n const subscription = this.activeSubscriptions[topic] ?? this.pubsub.subscription(this.getSubscriptionName(topic));\n const activeCbs = this.activeCbs[topic] ?? new Set();\n activeCbs.delete(cb);\n this.activeCbs[topic] = activeCbs;\n\n if (activeCbs.size === 0) {\n subscription.removeListener('message', cb);\n await subscription.close();\n }\n }\n\n async flush(): Promise<void> {\n await Promise.all(Object.values(this.ackBuffer));\n }\n}\n"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@mastra/google-cloud-pubsub",
|
|
3
|
+
"version": "0.0.0-1.x-tester-20251106055847",
|
|
4
|
+
"description": "Mastra Google Cloud PubSub integration",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"files": [
|
|
9
|
+
"dist",
|
|
10
|
+
"CHANGELOG.md"
|
|
11
|
+
],
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"import": {
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"default": "./dist/index.js"
|
|
17
|
+
},
|
|
18
|
+
"require": {
|
|
19
|
+
"types": "./dist/index.d.ts",
|
|
20
|
+
"default": "./dist/index.cjs"
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"./package.json": "./package.json"
|
|
24
|
+
},
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"@google-cloud/pubsub": "^5.2.0",
|
|
27
|
+
"@inngest/realtime": "^0.4.4",
|
|
28
|
+
"@opentelemetry/api": "^1.9.0",
|
|
29
|
+
"inngest": "^3.44.2",
|
|
30
|
+
"zod": "^3.25.76"
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"ai": "^4.3.16",
|
|
34
|
+
"@types/node": "^20.19.0",
|
|
35
|
+
"typescript": "^5.8.3",
|
|
36
|
+
"eslint": "^9.37.0",
|
|
37
|
+
"vitest": "^3.2.4",
|
|
38
|
+
"tsup": "^8.4.0",
|
|
39
|
+
"@internal/lint": "0.0.0-1.x-tester-20251106055847",
|
|
40
|
+
"@internal/types-builder": "0.0.0-1.x-tester-20251106055847",
|
|
41
|
+
"@mastra/libsql": "0.0.0-1.x-tester-20251106055847",
|
|
42
|
+
"@mastra/deployer": "0.0.0-1.x-tester-20251106055847",
|
|
43
|
+
"@mastra/core": "0.0.0-1.x-tester-20251106055847"
|
|
44
|
+
},
|
|
45
|
+
"peerDependencies": {
|
|
46
|
+
"@mastra/core": "0.0.0-1.x-tester-20251106055847"
|
|
47
|
+
},
|
|
48
|
+
"homepage": "https://mastra.ai",
|
|
49
|
+
"repository": {
|
|
50
|
+
"type": "git",
|
|
51
|
+
"url": "git+https://github.com/mastra-ai/mastra.git",
|
|
52
|
+
"directory": "pubsub/google-cloud-pubsub"
|
|
53
|
+
},
|
|
54
|
+
"bugs": {
|
|
55
|
+
"url": "https://github.com/mastra-ai/mastra/issues"
|
|
56
|
+
},
|
|
57
|
+
"engines": {
|
|
58
|
+
"node": ">=22.13.0"
|
|
59
|
+
},
|
|
60
|
+
"scripts": {
|
|
61
|
+
"build": "tsup --silent --config tsup.config.ts",
|
|
62
|
+
"build:watch": "tsup --watch --silent --config tsup.config.ts",
|
|
63
|
+
"test": "vitest run --bail=1",
|
|
64
|
+
"lint": "eslint ."
|
|
65
|
+
}
|
|
66
|
+
}
|