@pbvision/cloud-run-service 0.0.15 → 0.0.17

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": "@pbvision/cloud-run-service",
3
- "version": "0.0.15",
3
+ "version": "0.0.17",
4
4
  "description": "fastify-firestore-service Web Framework on Cloud Run",
5
5
  "license": "Apache-2.0",
6
6
  "type": "module",
@@ -28,7 +28,7 @@
28
28
  },
29
29
  "dependencies": {
30
30
  "@google-cloud/tasks": "^4.0.1",
31
- "@pbvision/fastify-firestore-service": "^0.0.27",
31
+ "@pbvision/fastify-firestore-service": "^0.0.28",
32
32
  "google-auth-library": "^9.4.2"
33
33
  },
34
34
  "devDependencies": {
package/src/main.js CHANGED
@@ -87,5 +87,6 @@ export async function runService (components) {
87
87
  // start the server
88
88
  service = await makeService(components)
89
89
  service.listen({ port, host: '0.0.0.0' })
90
+ return service
90
91
  }
91
92
  }
package/src/tasks.js CHANGED
@@ -1,3 +1,6 @@
1
+ import assert from 'node:assert'
2
+ import crypto from 'node:crypto'
3
+
1
4
  import { CloudTasksClient } from '@google-cloud/tasks'
2
5
  import { credentials } from '@grpc/grpc-js'
3
6
 
@@ -27,6 +30,10 @@ const tasksClient = (() => {
27
30
  * @param {any} payload the data to convert to JSON add send as the task's body
28
31
  * @param {string} [name] if provided, a name that is reused (on the same queue)
29
32
  * within about an hour will be rejected (TaskNameAlreadyExistsError)
33
+ * @param {Array<string>} [hashNameParts] if provided, the name param will be
34
+ * generated from the combination of the queue name (namespacing this name)
35
+ * and the name part(s) in this argument; the task name will be an md5 hash
36
+ * of all this
30
37
  * @param {string} [service="internal"] the service name that the task request
31
38
  * will be routed to
32
39
  * @param {boolean} [ignoreNameAlreadyUsedError=false] if true, no error is
@@ -38,7 +45,9 @@ const tasksClient = (() => {
38
45
  export async function enqueueCloudTask ({
39
46
  queue, payload,
40
47
  service = 'internal',
41
- name = undefined, ignoreNameAlreadyUsedError = false
48
+ name = undefined,
49
+ hashNameParts = undefined,
50
+ ignoreNameAlreadyUsedError = false
42
51
  }) {
43
52
  const project = process.env.GCLOUD_PROJECT
44
53
  const region = process.env.REGION
@@ -57,6 +66,12 @@ export async function enqueueCloudTask ({
57
66
  }
58
67
  }
59
68
  }
69
+ if (hashNameParts) {
70
+ assert(Array.isArray(hashNameParts))
71
+ assert(!name, 'cannot specify both name and hashNameParts')
72
+ const namespacedName = [queue, ...hashNameParts].join('|')
73
+ name = crypto.createHash('md5').update(namespacedName).digest('hex')
74
+ }
60
75
  if (name) {
61
76
  const fqName = tasksClient.taskPath(project, region, queue, name)
62
77
  task.name = fqName
@@ -1,3 +1,5 @@
1
+ import crypto from 'node:crypto'
2
+
1
3
  import { CloudTasksClient } from '@google-cloud/tasks'
2
4
  import { jest } from '@jest/globals'
3
5
 
@@ -66,6 +68,19 @@ class TestTasks extends BaseTest {
66
68
  { name: 'projects/localhost-emulator/locations/us-central1/queues/test-queue/tasks/x' })
67
69
  }
68
70
 
71
+ async testEnqueueTaskWithHashName () {
72
+ const partsToCheck = [
73
+ [], ['x'], ['x', 'yyy', '']
74
+ ]
75
+ for (const hashNameParts of partsToCheck) {
76
+ const expName = crypto.createHash('md5').update(
77
+ ['test-queue'].concat(hashNameParts).join('|')).digest('hex')
78
+ await this.check(
79
+ { hashNameParts, payload: { x: 3 } },
80
+ { name: 'projects/localhost-emulator/locations/us-central1/queues/test-queue/tasks/' + expName })
81
+ }
82
+ }
83
+
69
84
  async testEnqueueTaskWithNameThatWasRecentlyUsedNOTOkay () {
70
85
  this.createTaskReturnValue = new Promise((resolve, reject) => {
71
86
  const err = new Error('6 ALREADY_EXISTS and other random details')