@pbvision/cloud-run-service 0.0.3 → 0.0.4

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.3",
3
+ "version": "0.0.4",
4
4
  "description": "fastify-firestore-service Web Framework on Cloud Run",
5
5
  "license": "Apache-2.0",
6
6
  "type": "module",
@@ -29,7 +29,7 @@
29
29
  },
30
30
  "dependencies": {
31
31
  "@google-cloud/tasks": "^4.0.1",
32
- "@pbvision/fastify-firestore-service": "^0.0.19",
32
+ "@pbvision/fastify-firestore-service": "^0.0.20",
33
33
  "google-auth-library": "^9.4.2"
34
34
  },
35
35
  "devDependencies": {
@@ -3,7 +3,7 @@
3
3
  // works if this service has been granted access to the target service!)
4
4
  import { GoogleAuth } from 'google-auth-library'
5
5
 
6
- import { getServiceHost } from './utils.js'
6
+ import { getServiceProtocolAndHost } from './utils.js'
7
7
 
8
8
  const auth = new GoogleAuth()
9
9
 
@@ -13,12 +13,10 @@ export async function callServiceAPI ({
13
13
  body = undefined, qsParams = undefined,
14
14
  method = 'POST', headers = {}, isServiceInternal = true
15
15
  }) {
16
- const host = getServiceHost(serviceName)
17
- // istanbul ignore next
18
- const protocol = host === 'localhost' ? 'https' : 'http'
19
- const url = `${protocol}://${host}${path}`
16
+ const protocolAndHost = getServiceProtocolAndHost(serviceName)
17
+ const url = `${protocolAndHost}${path}`
20
18
  if (isServiceInternal) {
21
- const targetAudience = `https://${host}/`
19
+ const targetAudience = `${protocolAndHost}/`
22
20
  const client = await auth.getIdTokenClient(targetAudience)
23
21
  const token = await client.idTokenProvider.fetchIdToken(targetAudience)
24
22
  headers.Authorization = `Bearer ${token}`
package/src/tasks.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { CloudTasksClient } from '@google-cloud/tasks'
2
2
 
3
- import { getServiceHost } from './utils.js'
3
+ import { getServiceProtocolAndHost } from './utils.js'
4
4
 
5
5
  const tasksClient = new CloudTasksClient()
6
6
 
@@ -30,13 +30,14 @@ export async function enqueueCloudTask ({
30
30
  }) {
31
31
  const parent = tasksClient.queuePath(
32
32
  process.env.PROJECT, process.env.REGION, queue)
33
+ const protocolAndHost = getServiceProtocolAndHost(service)
33
34
  const task = {
34
35
  httpRequest: {
35
36
  headers: {
36
37
  'Content-Type': 'application/json'
37
38
  },
38
39
  httpMethod: 'POST',
39
- url: `https://${getServiceHost(service)}/${queue.replace(/-/g, '_')}`,
40
+ url: `${protocolAndHost}/${queue.replace(/-/g, '_')}`,
40
41
  body: Buffer.from(JSON.stringify(payload)).toString('base64'),
41
42
  oidcToken: {
42
43
  serviceAccountEmail: `cr-${process.env.SERVICE}@${process.env.PROJECT}.iam.gserviceaccount.com`
package/src/utils.js CHANGED
@@ -2,6 +2,13 @@ import assert from 'node:assert'
2
2
 
3
3
  import { port as portForThisService } from './port.js'
4
4
 
5
+ export function getServiceProtocolAndHost (serviceName) {
6
+ const host = getServiceHost(serviceName)
7
+ // istanbul ignore next
8
+ const protocol = host === 'localhost' ? 'https' : 'http'
9
+ return `${protocol}://${host}`
10
+ }
11
+
5
12
  export function getServiceHost (serviceName) {
6
13
  if (isLocalhost()) {
7
14
  if (process.env.SERVICE === serviceName) {
@@ -51,7 +51,7 @@ class TestCallServiceAPI extends AppTest {
51
51
  // make sure Google Auth was called with appropriate arguments (or not, if
52
52
  // this wasn't an internal call)
53
53
  if (shouldHaveToken) {
54
- const targetAudience = `https://localhost:${expPort}/`
54
+ const targetAudience = `http://localhost:${expPort}/`
55
55
  expect(GoogleAuth.prototype.getIdTokenClient).toHaveBeenCalledWith(targetAudience)
56
56
  expect(this.fetchIdToken).toHaveBeenCalledWith(targetAudience)
57
57
  } else {
@@ -49,7 +49,7 @@ class TestTasks extends BaseTest {
49
49
  oidcToken: {
50
50
  serviceAccountEmail: 'cr-tbd@xyz-dev.iam.gserviceaccount.com'
51
51
  },
52
- url: 'https://localhost:8888/test_queue'
52
+ url: 'http://localhost:8888/test_queue'
53
53
  },
54
54
  ...taskExpectations
55
55
  }