firebase-functions 7.0.4 → 7.0.5

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.
@@ -10,6 +10,8 @@ var pubsub_exports = /* @__PURE__ */ __export({
10
10
  Message: () => Message,
11
11
  onMessagePublished: () => onMessagePublished
12
12
  });
13
+ const V1_CONTEXT = Symbol("v1Context");
14
+ const V1_MESSAGE = Symbol("v1Message");
13
15
  /**
14
16
  * Google Cloud Pub/Sub is a globally distributed message bus that automatically scales as you need it.
15
17
  * You can create a function ({@link onMessagePublished}) that handles pub/sub events by using functions.pubsub.
@@ -101,7 +103,67 @@ function onMessagePublished(topicOrOptions, handler) {
101
103
  }
102
104
  const func = (raw) => {
103
105
  const messagePublishedData = raw.data;
104
- messagePublishedData.message = new Message(messagePublishedData.message);
106
+ if (!(messagePublishedData.message instanceof Message)) {
107
+ messagePublishedData.message = new Message(messagePublishedData.message);
108
+ }
109
+ /**
110
+ * Get the v1-compatible EventContext object.
111
+ * This is a compatibility layer to ease migration from v1 to v2.
112
+ * @returns A v1 EventContext-like object.
113
+ */
114
+ Object.defineProperty(raw, "context", { get: function() {
115
+ if (this[V1_CONTEXT]) {
116
+ return this[V1_CONTEXT];
117
+ }
118
+ const data = this.data;
119
+ if (!data?.message) {
120
+ throw new Error("Malformed Pub/Sub event: 'data.message' is missing.");
121
+ }
122
+ const v1Context = {
123
+ eventId: data.message.messageId,
124
+ timestamp: data.message.publishTime,
125
+ eventType: "google.pubsub.topic.publish",
126
+ resource: {
127
+ service: "pubsub.googleapis.com",
128
+ name: this.source.replace("//pubsub.googleapis.com/", "")
129
+ },
130
+ params: {}
131
+ };
132
+ this[V1_CONTEXT] = v1Context;
133
+ return this[V1_CONTEXT];
134
+ } });
135
+ /**
136
+ * Get the v1-compatible Message object.
137
+ * This is a compatibility layer to ease migration from v1 to v2.
138
+ * @returns A plain object mimicking the v1 Message structure.
139
+ */
140
+ Object.defineProperty(raw, "message", { get: function() {
141
+ if (this[V1_MESSAGE]) {
142
+ return this[V1_MESSAGE];
143
+ }
144
+ const data = this.data;
145
+ if (!data || !data.message) {
146
+ throw new Error("Malformed Pub/Sub event: 'data.message' is missing.");
147
+ }
148
+ const v2Message = data.message;
149
+ const baseMessage = {
150
+ data: v2Message.data,
151
+ messageId: v2Message.messageId,
152
+ publishTime: v2Message.publishTime,
153
+ attributes: v2Message.attributes || {}
154
+ };
155
+ if (v2Message.orderingKey) {
156
+ baseMessage.orderingKey = v2Message.orderingKey;
157
+ }
158
+ this[V1_MESSAGE] = {
159
+ ...baseMessage,
160
+ get json() {
161
+ return v2Message.json;
162
+ },
163
+ toJSON: () => baseMessage
164
+ };
165
+ return this[V1_MESSAGE];
166
+ } });
105
167
  return wrapTraceContext(withInit(handler))(raw);
106
168
  };
107
169
  func.run = handler;
@@ -1,5 +1,6 @@
1
1
  import { ResetValue } from "../../common/options";
2
2
  import { CloudEvent, CloudFunction } from "../core";
3
+ import type { EventContext } from "../../v1/index";
3
4
  import { Expression } from "../../params";
4
5
  import * as options from "../options";
5
6
  import { SupportedSecretParam } from "../../params/types";
@@ -80,6 +81,33 @@ export interface MessagePublishedData<T = any> {
80
81
  /** A subscription resource. */
81
82
  readonly subscription: string;
82
83
  }
84
+ /**
85
+ * A v1-compatible Pub/Sub Message.
86
+ * Note: This is a plain object mimicking the v1 Message structure, not an instance of the v1 Message class.
87
+ * @typeParam T - Type of `json` payload.
88
+ */
89
+ export interface V1PubSubMessage<T = any> {
90
+ readonly data: string;
91
+ readonly attributes: {
92
+ [key: string]: string;
93
+ };
94
+ readonly messageId: string;
95
+ readonly publishTime: string;
96
+ readonly orderingKey?: string;
97
+ readonly json: T;
98
+ toJSON(): any;
99
+ }
100
+ export interface MessagePublishedEvent<T = any> extends CloudEvent<MessagePublishedData<T>> {
101
+ /**
102
+ * v1-compatible EventContext.
103
+ */
104
+ readonly context: EventContext;
105
+ /**
106
+ * v1-compatible Pub/Sub Message.
107
+ * Note: This is a plain object mimicking the v1 Message structure, not an instance of the v1 Message class.
108
+ */
109
+ readonly message: V1PubSubMessage<T>;
110
+ }
83
111
  /** PubSubOptions extend EventHandlerOptions but must include a topic. */
84
112
  export interface PubSubOptions extends options.EventHandlerOptions {
85
113
  /** The Pub/Sub topic to watch for message events */
@@ -171,11 +199,11 @@ export interface PubSubOptions extends options.EventHandlerOptions {
171
199
  * @param handler - runs every time a Cloud Pub/Sub message is published
172
200
  * @typeParam T - Type representing `Message.data`'s JSON format
173
201
  */
174
- export declare function onMessagePublished<T = any>(topic: string, handler: (event: CloudEvent<MessagePublishedData<T>>) => any | Promise<any>): CloudFunction<CloudEvent<MessagePublishedData<T>>>;
202
+ export declare function onMessagePublished<T = any>(topic: string, handler: (event: MessagePublishedEvent<T>) => any | Promise<any>): CloudFunction<MessagePublishedEvent<T>>;
175
203
  /**
176
204
  * Handle a message being published to a Pub/Sub topic.
177
205
  * @param options - Option containing information (topic) for event
178
206
  * @param handler - runs every time a Cloud Pub/Sub message is published
179
207
  * @typeParam T - Type representing `Message.data`'s JSON format
180
208
  */
181
- export declare function onMessagePublished<T = any>(options: PubSubOptions, handler: (event: CloudEvent<MessagePublishedData<T>>) => any | Promise<any>): CloudFunction<CloudEvent<MessagePublishedData<T>>>;
209
+ export declare function onMessagePublished<T = any>(options: PubSubOptions, handler: (event: MessagePublishedEvent<T>) => any | Promise<any>): CloudFunction<MessagePublishedEvent<T>>;
@@ -10,6 +10,8 @@ var pubsub_exports = /* @__PURE__ */ require_rolldown_runtime.__export({
10
10
  Message: () => Message,
11
11
  onMessagePublished: () => onMessagePublished
12
12
  });
13
+ const V1_CONTEXT = Symbol("v1Context");
14
+ const V1_MESSAGE = Symbol("v1Message");
13
15
  /**
14
16
  * Google Cloud Pub/Sub is a globally distributed message bus that automatically scales as you need it.
15
17
  * You can create a function ({@link onMessagePublished}) that handles pub/sub events by using functions.pubsub.
@@ -101,7 +103,67 @@ function onMessagePublished(topicOrOptions, handler) {
101
103
  }
102
104
  const func = (raw) => {
103
105
  const messagePublishedData = raw.data;
104
- messagePublishedData.message = new Message(messagePublishedData.message);
106
+ if (!(messagePublishedData.message instanceof Message)) {
107
+ messagePublishedData.message = new Message(messagePublishedData.message);
108
+ }
109
+ /**
110
+ * Get the v1-compatible EventContext object.
111
+ * This is a compatibility layer to ease migration from v1 to v2.
112
+ * @returns A v1 EventContext-like object.
113
+ */
114
+ Object.defineProperty(raw, "context", { get: function() {
115
+ if (this[V1_CONTEXT]) {
116
+ return this[V1_CONTEXT];
117
+ }
118
+ const data = this.data;
119
+ if (!data?.message) {
120
+ throw new Error("Malformed Pub/Sub event: 'data.message' is missing.");
121
+ }
122
+ const v1Context = {
123
+ eventId: data.message.messageId,
124
+ timestamp: data.message.publishTime,
125
+ eventType: "google.pubsub.topic.publish",
126
+ resource: {
127
+ service: "pubsub.googleapis.com",
128
+ name: this.source.replace("//pubsub.googleapis.com/", "")
129
+ },
130
+ params: {}
131
+ };
132
+ this[V1_CONTEXT] = v1Context;
133
+ return this[V1_CONTEXT];
134
+ } });
135
+ /**
136
+ * Get the v1-compatible Message object.
137
+ * This is a compatibility layer to ease migration from v1 to v2.
138
+ * @returns A plain object mimicking the v1 Message structure.
139
+ */
140
+ Object.defineProperty(raw, "message", { get: function() {
141
+ if (this[V1_MESSAGE]) {
142
+ return this[V1_MESSAGE];
143
+ }
144
+ const data = this.data;
145
+ if (!data || !data.message) {
146
+ throw new Error("Malformed Pub/Sub event: 'data.message' is missing.");
147
+ }
148
+ const v2Message = data.message;
149
+ const baseMessage = {
150
+ data: v2Message.data,
151
+ messageId: v2Message.messageId,
152
+ publishTime: v2Message.publishTime,
153
+ attributes: v2Message.attributes || {}
154
+ };
155
+ if (v2Message.orderingKey) {
156
+ baseMessage.orderingKey = v2Message.orderingKey;
157
+ }
158
+ this[V1_MESSAGE] = {
159
+ ...baseMessage,
160
+ get json() {
161
+ return v2Message.json;
162
+ },
163
+ toJSON: () => baseMessage
164
+ };
165
+ return this[V1_MESSAGE];
166
+ } });
105
167
  return require_v2_trace.wrapTraceContext(require_common_onInit.withInit(handler))(raw);
106
168
  };
107
169
  func.run = handler;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "firebase-functions",
3
- "version": "7.0.4",
3
+ "version": "7.0.5",
4
4
  "description": "Firebase SDK for Cloud Functions",
5
5
  "keywords": [
6
6
  "firebase",