Skip to content

Commit 7222aad

Browse files
Addressing the PR comments
1 parent 30e6e10 commit 7222aad

File tree

2 files changed

+20
-85
lines changed

2 files changed

+20
-85
lines changed

spec/v2/providers/pubsub.spec.ts

Lines changed: 12 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -170,15 +170,15 @@ describe("onMessagePublished", () => {
170170
expect(json).to.deep.equal({ hello: "world" });
171171
});
172172

173-
it("should construct a CloudEvent with the correct context", async () => {
173+
it("should construct a CloudEvent with the correct context and message", async () => {
174174
const publishTime = new Date().toISOString();
175-
const message = {
175+
const messagePayload = {
176176
messageId: "uuid",
177177
data: Buffer.from(JSON.stringify({ hello: "world" })).toString("base64"),
178178
publishTime,
179179
};
180180
const data: pubsub.MessagePublishedData = {
181-
message: message as any,
181+
message: messagePayload as any,
182182
subscription: "projects/aProject/subscriptions/aSubscription",
183183
};
184184
const event: CloudEvent<pubsub.MessagePublishedData> = {
@@ -190,23 +190,20 @@ describe("onMessagePublished", () => {
190190
data,
191191
};
192192

193-
let receivedEvent: CloudEvent<pubsub.MessagePublishedData<any>>;
193+
let destructuredMessage: pubsub.Message<any>;
194+
let context: any;
194195
const func = pubsub.onMessagePublished("topic", (e) => {
195-
receivedEvent = e;
196+
({ message: destructuredMessage, context } = e as any);
196197
});
197198

198199
await func(event);
199200

200-
expect(receivedEvent.id).to.equal("uuid");
201-
expect(receivedEvent.time).to.equal(publishTime);
202-
expect(receivedEvent.type).to.equal("google.cloud.pubsub.topic.v1.messagePublished");
203-
expect(receivedEvent.source).to.equal("//pubsub.googleapis.com/projects/aProject/topics/topic");
204-
expect(receivedEvent.data.message.json).to.deep.equal({ hello: "world" });
205-
expect(receivedEvent.context).to.exist;
206-
expect(receivedEvent.context.eventId).to.equal("uuid");
207-
expect(receivedEvent.context.timestamp).to.equal(publishTime);
208-
expect(receivedEvent.context.eventType).to.equal("google.cloud.pubsub.topic.v1.messagePublished");
209-
expect(receivedEvent.context.resource).to.deep.equal({
201+
expect(destructuredMessage.json).to.deep.equal({ hello: "world" });
202+
expect(context).to.exist;
203+
expect(context.eventId).to.equal("uuid");
204+
expect(context.timestamp).to.equal(publishTime);
205+
expect(context.eventType).to.equal("google.cloud.pubsub.topic.v1.messagePublished");
206+
expect(context.resource).to.deep.equal({
210207
service: "pubsub.googleapis.com",
211208
name: "projects/aProject/topics/topic",
212209
});
@@ -236,77 +233,11 @@ describe("onMessagePublished", () => {
236233
);
237234
});
238235

239-
//Test case to ensure Idempotency. makes things dont break if there is already context present
240-
it("should not modify a CloudEvent that already has a context", async () => {
241-
const publishTime = new Date().toISOString();
242-
const message = {
243-
messageId: "uuid",
244-
data: Buffer.from(JSON.stringify({ hello: "world" })).toString("base64"),
245-
publishTime,
246-
};
247-
const data: pubsub.MessagePublishedData = {
248-
message: message as any,
249-
subscription: "projects/aProject/subscriptions/aSubscription",
250-
};
251-
const existingContext = {
252-
eventId: "custom-id",
253-
timestamp: publishTime,
254-
eventType: "custom.type",
255-
resource: "custom/resource",
256-
params: {},
257-
};
258-
const event: CloudEvent<pubsub.MessagePublishedData> = {
259-
specversion: "1.0",
260-
id: "uuid",
261-
time: publishTime,
262-
type: "google.cloud.pubsub.topic.v1.messagePublished",
263-
source: "//pubsub.googleapis.com/projects/aProject/topics/topic",
264-
data,
265-
context: existingContext as any,
266-
};
267236

268-
let receivedEvent: CloudEvent<pubsub.MessagePublishedData<any>>;
269-
const func = pubsub.onMessagePublished("topic", (e) => {
270-
receivedEvent = e;
271-
});
272237

273-
await func(event);
274238

275-
expect(receivedEvent.context).to.deep.equal(existingContext);
276-
});
277239

278240

279-
//Test case to ensure GCLOUD_PROJECT is used as fallback for resource name
280-
it("should use GCLOUD_PROJECT as fallback for resource name", async () => {
281-
const publishTime = new Date().toISOString();
282-
const message = {
283-
messageId: "uuid",
284-
data: Buffer.from(JSON.stringify({ hello: "world" })).toString("base64"),
285-
publishTime,
286-
};
287-
const data: pubsub.MessagePublishedData = {
288-
message: message as any,
289-
subscription: "projects/aProject/subscriptions/aSubscription",
290-
};
291-
const event: CloudEvent<pubsub.MessagePublishedData> = {
292-
specversion: "1.0",
293-
id: "uuid",
294-
time: publishTime,
295-
type: "google.cloud.pubsub.topic.v1.messagePublished",
296-
source: "//pubsub.googleapis.com/topics/topic", // Malformed source
297-
data,
298-
};
299-
300-
let receivedEvent: CloudEvent<pubsub.MessagePublishedData<any>>;
301-
const func = pubsub.onMessagePublished("topic", (e) => {
302-
receivedEvent = e;
303-
});
304-
305-
await func(event);
306-
307-
expect(receivedEvent.context.resource.name).to.equal("projects/aProject/topics/topic");
308-
});
309-
310241
it("should use 'unknown-project' as fallback for resource name", async () => {
311242
delete process.env.GCLOUD_PROJECT;
312243
const publishTime = new Date().toISOString();

src/v2/providers/pubsub.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ export function onMessagePublished<T = any>(
367367
*/
368368
function attachPubSubContext<T>(event: CloudEvent<MessagePublishedData<T>>, topic: string) {
369369
if ("context" in event && event.context) {
370-
return;
370+
throw new Error("Unexpected context in event.");
371371
}
372372

373373
const resourceName = getResourceName(event, topic);
@@ -394,6 +394,12 @@ function attachPubSubContext<T>(event: CloudEvent<MessagePublishedData<T>>, topi
394394
configurable: false,
395395

396396
});
397+
398+
Object.defineProperty(event, "message", {
399+
get: () => (event.data as MessagePublishedData<T>).message,
400+
enumerable: false,
401+
configurable: false,
402+
});
397403
}
398404

399405
/**
@@ -407,9 +413,7 @@ function attachPubSubContext<T>(event: CloudEvent<MessagePublishedData<T>>, topi
407413
*/
408414
function getResourceName(event: CloudEvent<MessagePublishedData<any>>, topic: string) {
409415
const match = event.source?.match(/projects\/([^/]+)\/topics\/([^/]+)/);
410-
const project =
411-
match?.[1] ?? process.env.GCLOUD_PROJECT ?? process.env.GCLOUD_PROJECT_ID ?? process.env.PROJECT_ID;
412-
416+
const project = match?.[1];
413417
const topicName = match?.[2] ?? topic;
414418

415419
if (!project) {

0 commit comments

Comments
 (0)