@google-cloud/nodejs-common 2.5.0 → 2.5.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@google-cloud/nodejs-common",
3
- "version": "2.5.0",
3
+ "version": "2.5.1",
4
4
  "description": "A NodeJs common library for solutions based on Cloud Functions",
5
5
  "author": "Google Inc.",
6
6
  "license": "Apache-2.0",
@@ -21,7 +21,7 @@
21
21
  "@google-cloud/datastore": "^9.2.1",
22
22
  "@google-cloud/firestore": "^7.11.1",
23
23
  "@google-cloud/logging-winston": "^6.0.1",
24
- "@google-cloud/pubsub": "^5.1.0",
24
+ "@google-cloud/pubsub": "^5.2.0",
25
25
  "@google-cloud/scheduler": "^5.2.0",
26
26
  "@google-cloud/secret-manager": "^6.0.1",
27
27
  "@google-cloud/storage": "^7.16.0",
@@ -34,7 +34,15 @@ const {
34
34
  * 1. gets or creates a topic.
35
35
  * 2. gets or creates a subscription.
36
36
  * 3. publishes a message after confirms the topic exists.
37
- * 4. acknowledge message(s).
37
+ * 4. pull message(s).
38
+ * 5. acknowledge message(s).
39
+ *
40
+ * The 'pull' and 'acknowledge' are built on 'v1.SubscriberClient` which is
41
+ * considered as 'low-level', auto-generated gRPC API class. This class offers
42
+ * more direct control over the request-response cycle. Tentacles' queuing
43
+ * mechanism is built on this kind of precise control.
44
+ * There is also a high-level 'Pubsub' client, which automatically handles
45
+ * message streaming and flow control.
38
46
  */
39
47
  class EnhancedPubSub {
40
48
  /**
@@ -127,23 +135,53 @@ class EnhancedPubSub {
127
135
  }
128
136
  };
129
137
 
138
+ /**
139
+ * Pulls and returns message(s) from a pull subscription.
140
+ *
141
+ * @param {string} subscriptionName Subscription name.
142
+ * @param {number=} maxMessages
143
+ * @return {!Array<ReceivedMessage>}
144
+ */
145
+ async pull(subscriptionName, maxMessages = 1) {
146
+ const subscriptionPath = await this.getSubscriptionPath_(subscriptionName);
147
+ const [response] = await this.subClient.pull({
148
+ subscription: subscriptionPath,
149
+ maxMessages,
150
+ });
151
+ return response.receivedMessages ? response.receivedMessages : [];
152
+ }
153
+
130
154
  /**
131
155
  * Using `SubscriberClient` to acknowledge messages.
132
156
  * 2022.11.02 The method `ack()` in Message doesn't work properly due to a
133
157
  * unknown reason. Use this function to acknowledge a message for now.
158
+ * 2025.08.20 This function can't properly acknowledge messages pulled through
159
+ * 'Pubsub' object. Use a new 'pull' method to get the message and ack.
134
160
  *
135
- * @param {string} subscription Subscription name.
161
+ * @param {string} subscriptionName Subscription name.
136
162
  * @param {(string|!Array<string>)} ackIds Message ackIds.
137
163
  */
138
- async acknowledge(subscription, ackIds) {
139
- const projectId = await this.subClient.getProjectId();
164
+ async acknowledge(subscriptionName, ackIds) {
165
+ const subscriptionPath = await this.getSubscriptionPath_(subscriptionName);
140
166
  const ackRequest = {
141
- subscription: this.subClient.subscriptionPath(projectId, subscription),
167
+ subscription: subscriptionPath,
142
168
  ackIds: Array.isArray(ackIds) ? ackIds : [ackIds],
143
169
  };
144
170
  await this.subClient.acknowledge(ackRequest);
145
171
  }
146
172
 
173
+ /**
174
+ * Returns the complete subscription path for the `SubscriberClient` object.
175
+ * @see https://cloud.google.com/nodejs/docs/reference/pubsub/latest/pubsub/v1.subscriberclient
176
+ * @param {string} subscriptionName Subscription name.
177
+ * @return {string}
178
+ * @private
179
+ */
180
+ async getSubscriptionPath_(subscriptionName) {
181
+ const projectId = await this.subClient.getProjectId();
182
+ return this.subClient.subscriptionPath(projectId, subscriptionName);
183
+ }
184
+
147
185
  /**
148
186
  * Returns a new instance of this class. Using this function to replace
149
187
  * constructor to be more friendly to unit tests.
@@ -163,7 +201,7 @@ class EnhancedPubSub {
163
201
  * @return {string} The decoded message.
164
202
  */
165
203
  const getMessage = (message) => {
166
- return Buffer.from(message.data, 'base64').toString();
204
+ return Buffer.from(message.data || '', 'base64').toString();
167
205
  };
168
206
 
169
207
  module.exports = {