Skip to content

Commit a9dfc31

Browse files
committed
change to save data
1 parent 6d6a500 commit a9dfc31

3 files changed

Lines changed: 40 additions & 39 deletions

File tree

src/microservice/gcloud-pub-sub.server.ts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,11 @@ import { PubSub, Subscription, Message } from '@google-cloud/pubsub'
22
import { Server, CustomTransportStrategy } from '@nestjs/microservices'
33

44
import { MESSAGE, ERROR, PUB_SUB_DEFAULT_RETRY_CODES } from '../helpers/constants'
5-
import { GCloudPubSubServerOptions } from '../interfaces/gcloud-pub-sub.interface'
5+
import type { GCloudPubSubServerOptions } from '../interfaces/gcloud-pub-sub.interface'
66

77
const RETRY_INTERVAL = 5000
88

9-
export class GCloudPubSubServer
10-
extends Server
11-
implements CustomTransportStrategy
12-
{
9+
export class GCloudPubSubServer extends Server implements CustomTransportStrategy {
1310
public client: PubSub = null
1411
public subscriptions: Subscription[] = []
1512
public isShuttingDown: boolean = false
@@ -21,28 +18,28 @@ export class GCloudPubSubServer
2118
public listen(callback: () => void) {
2219
this.isShuttingDown = false
2320
this.client = new PubSub(this.options.authOptions)
24-
this.options.subscriptionIds.forEach((subcriptionName) => {
21+
this.options.subscriptionIds.forEach((subscriptionName) => {
2522
const subscription = this.client.subscription(
26-
subcriptionName,
23+
subscriptionName,
2724
this.options.subscriberOptions || {}
2825
)
29-
const handleMessage = this.handleMessageFactory(subcriptionName)
30-
const handleError = this.handleErrorFactory(subscription, subcriptionName)
26+
const handleMessage = this.handleMessageFactory(subscriptionName)
27+
const handleError = this.handleErrorFactory(subscription, subscriptionName)
3128
subscription.on(MESSAGE, handleMessage.bind(this))
3229
subscription.on(ERROR, handleError)
3330
this.subscriptions.push(subscription)
3431
})
3532
callback()
3633
}
3734

38-
public handleErrorFactory(subscription: Subscription, subcriptionName: string) {
39-
return (error): void => {
35+
public handleErrorFactory(subscription: Subscription, subscriptionName: string) {
36+
return (error: any): void => {
4037
this.handleError(error)
4138
if (!this.isShuttingDown && PUB_SUB_DEFAULT_RETRY_CODES.includes(error.code)) {
42-
this.logger.warn(`Closing subscription: ${subcriptionName}`)
39+
this.logger.warn(`Closing subscription: ${subscriptionName}`)
4340
subscription.close()
4441
setTimeout(() => {
45-
this.logger.warn(`Opening subscription: ${subcriptionName}`)
42+
this.logger.warn(`Opening subscription: ${subscriptionName}`)
4643
subscription.open()
4744
}, RETRY_INTERVAL)
4845
}

src/module/gcloud-pub-sub.service.spec.ts

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -38,99 +38,99 @@ describe('GcloudPubSubService', () => {
3838
const data = 'You Tried Your Best and You Failed Miserably. The Lesson Is Never Try'
3939
const gcloudPubSubLibMock = {
4040
topic: jest.fn().mockReturnThis(),
41-
publish: jest.fn((buffer) => {
42-
expect(buffer).toMatchSnapshot()
41+
publishMessage: jest.fn(({ data }) => {
42+
expect(data).toMatchSnapshot()
4343
}),
4444
}
4545

4646
service.gcloudPubSubLib = gcloudPubSubLibMock as any
4747
service.publishMessage(topic, data)
48-
expect(gcloudPubSubLibMock.publish).toHaveBeenCalled()
48+
expect(gcloudPubSubLibMock.publishMessage).toHaveBeenCalled()
4949
})
5050
it('handles Buffer as data', () => {
5151
const topic = 'Homer'
5252
const data = 'You Tried Your Best and You Failed Miserably. The Lesson Is Never Try'
5353
const gcloudPubSubLibMock = {
5454
topic: jest.fn().mockReturnThis(),
55-
publish: jest.fn((buffer) => {
56-
expect(buffer).toMatchSnapshot()
55+
publishMessage: jest.fn(({ data }) => {
56+
expect(data).toMatchSnapshot()
5757
}),
5858
}
5959

6060
service.gcloudPubSubLib = gcloudPubSubLibMock as any
6161
service.publishMessage(topic, Buffer.from(data))
62-
expect(gcloudPubSubLibMock.publish).toHaveBeenCalled()
62+
expect(gcloudPubSubLibMock.publishMessage).toHaveBeenCalled()
6363
})
6464
it('handles an array of numbers as data', () => {
6565
const topic = 'Homer'
6666
const data = [10, 20, 30, 40, 50]
6767
const gcloudPubSubLibMock = {
6868
topic: jest.fn().mockReturnThis(),
69-
publish: jest.fn((buffer) => {
70-
expect(buffer).toMatchSnapshot()
69+
publishMessage: jest.fn(({ data }) => {
70+
expect(data).toMatchSnapshot()
7171
}),
7272
}
7373

7474
service.gcloudPubSubLib = gcloudPubSubLibMock as any
7575
service.publishMessage(topic, data)
76-
expect(gcloudPubSubLibMock.publish).toHaveBeenCalled()
76+
expect(gcloudPubSubLibMock.publishMessage).toHaveBeenCalled()
7777
})
7878
it('handles an ArrayBuffer as data', () => {
7979
const topic = 'Homer'
8080
const data = new ArrayBuffer(1)
8181
const gcloudPubSubLibMock = {
8282
topic: jest.fn().mockReturnThis(),
83-
publish: jest.fn((buffer) => {
84-
expect(buffer).toMatchSnapshot()
83+
publishMessage: jest.fn(({ data }) => {
84+
expect(data).toMatchSnapshot()
8585
}),
8686
}
8787

8888
service.gcloudPubSubLib = gcloudPubSubLibMock as any
8989
service.publishMessage(topic, data)
90-
expect(gcloudPubSubLibMock.publish).toHaveBeenCalled()
90+
expect(gcloudPubSubLibMock.publishMessage).toHaveBeenCalled()
9191
})
9292
it('handles a SharedArrayBuffer as data', () => {
9393
const topic = 'Homer'
9494
const data = new SharedArrayBuffer(1)
9595
const gcloudPubSubLibMock = {
9696
topic: jest.fn().mockReturnThis(),
97-
publish: jest.fn((buffer) => {
98-
expect(buffer).toMatchSnapshot()
97+
publishMessage: jest.fn(({ data }) => {
98+
expect(data).toMatchSnapshot()
9999
}),
100100
}
101101

102102
service.gcloudPubSubLib = gcloudPubSubLibMock as any
103103
service.publishMessage(topic, data)
104-
expect(gcloudPubSubLibMock.publish).toHaveBeenCalled()
104+
expect(gcloudPubSubLibMock.publishMessage).toHaveBeenCalled()
105105
})
106106
it('handles a Uint8Array as data', () => {
107107
const topic = 'Homer'
108108
const data = new Uint8Array([1, 2, 3])
109109
const gcloudPubSubLibMock = {
110110
topic: jest.fn().mockReturnThis(),
111-
publish: jest.fn((buffer) => {
112-
expect(buffer).toMatchSnapshot()
111+
publishMessage: jest.fn(({ data }) => {
112+
expect(data).toMatchSnapshot()
113113
}),
114114
}
115115

116116
service.gcloudPubSubLib = gcloudPubSubLibMock as any
117117
service.publishMessage(topic, data)
118-
expect(gcloudPubSubLibMock.publish).toHaveBeenCalled()
118+
expect(gcloudPubSubLibMock.publishMessage).toHaveBeenCalled()
119119
})
120120
it('handles a string and binary encoding', () => {
121121
const topic = 'Homer'
122122
const data = 'You Tried Your Best and You Failed Miserably. The Lesson Is Never Try'
123123
const encoding = 'binary'
124124
const gcloudPubSubLibMock = {
125125
topic: jest.fn().mockReturnThis(),
126-
publish: jest.fn((buffer) => {
127-
expect(buffer).toMatchSnapshot()
126+
publishMessage: jest.fn(({ data }) => {
127+
expect(data).toMatchSnapshot()
128128
}),
129129
}
130130

131131
service.gcloudPubSubLib = gcloudPubSubLibMock as any
132132
service.publishMessage(topic, data, {}, encoding)
133-
expect(gcloudPubSubLibMock.publish).toHaveBeenCalled()
133+
expect(gcloudPubSubLibMock.publishMessage).toHaveBeenCalled()
134134
})
135135
})
136136
})

src/module/gcloud-pub-sub.service.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { Injectable } from '@nestjs/common'
22
import { PubSub } from '@google-cloud/pubsub'
3-
import { GoogleAuthOptions } from '../interfaces/gcloud-pub-sub.interface'
4-
import { PublishOptions } from '@google-cloud/pubsub/build/src/topic'
3+
import type { PublishOptions, Topic } from '@google-cloud/pubsub/build/src/topic'
4+
5+
import type { GoogleAuthOptions } from '../interfaces/gcloud-pub-sub.interface'
56

67
@Injectable()
78
export class GcloudPubSubService {
@@ -11,11 +12,11 @@ export class GcloudPubSubService {
1112
private readonly googleAuthOptions: GoogleAuthOptions,
1213
private readonly publishOptions: PublishOptions
1314
) {
14-
this.gcloudPubSubLib = new PubSub(googleAuthOptions)
15+
this.gcloudPubSubLib = new PubSub(this.googleAuthOptions)
1516
}
1617

1718
public publishMessage(
18-
topic: string,
19+
topicName: string,
1920
data: string | Uint8Array | number[] | ArrayBuffer | SharedArrayBuffer,
2021
attributes: { [key: string]: string } = {},
2122
encoding?: BufferEncoding
@@ -34,6 +35,9 @@ export class GcloudPubSubService {
3435
} else {
3536
dataBuffer = Buffer.from(data as string)
3637
}
37-
return this.gcloudPubSubLib.topic(topic, this.publishOptions).publish(dataBuffer, attributes)
38+
39+
const topic: Topic = this.gcloudPubSubLib.topic(topicName)
40+
// const topic = this.gcloudPubSubLib.topic(topicName, this.publishOptions)
41+
return topic.publishMessage({ data: dataBuffer, attributes })
3842
}
3943
}

0 commit comments

Comments
 (0)