@juzi/wechaty-puppet-service 1.0.73 → 1.0.74

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": "@juzi/wechaty-puppet-service",
3
- "version": "1.0.73",
3
+ "version": "1.0.74",
4
4
  "description": "Puppet Service for Wechaty",
5
5
  "type": "module",
6
6
  "exports": {
@@ -51,7 +51,7 @@ class PayloadStore {
51
51
  public room? : FlashStore<string, PUPPET.payloads.Room>
52
52
  public tag? : FlashStore<string, PUPPET.payloads.Tag>
53
53
  public tagGroup? : FlashStore<string, PUPPET.payloads.TagGroup>
54
- public miscellaneous : FlashStore<string, string>
54
+ public miscellaneous?: FlashStore<string, string>
55
55
 
56
56
  protected storeDir: string
57
57
  protected accountId?: string
@@ -72,7 +72,11 @@ class PayloadStore {
72
72
  fs.mkdirSync(this.storeDir, { recursive: true })
73
73
  }
74
74
 
75
- this.miscellaneous = new FlashStore(path.join(this.storeDir, 'miscellaneous')) // account-free data
75
+ try {
76
+ this.miscellaneous = new FlashStore(path.join(this.storeDir, 'miscellaneous')) // account-free data
77
+ } catch (e) {
78
+ log.error('PayloadStore', 'constructor() store failed to init, will continue without it.')
79
+ }
76
80
  }
77
81
 
78
82
  /**
@@ -155,7 +159,7 @@ class PayloadStore {
155
159
  throw new Error('Can not destroy() a start()-ed store. Call stop() to stop it first')
156
160
  }
157
161
 
158
- await this.miscellaneous.close()
162
+ await this.miscellaneous?.close()
159
163
 
160
164
  /**
161
165
  * Huan(202108): `fs.rm` was introduced from Node.js v14.14
@@ -143,9 +143,9 @@ class PuppetService extends PUPPET.Puppet {
143
143
  this._grpcManager = undefined
144
144
  }
145
145
 
146
- log.verbose('PuppetService', 'start() instanciating GrpcManager ...')
146
+ log.info('PuppetService', 'start() instanciating GrpcManager ...')
147
147
  const grpcManager = new GrpcManager(this.options)
148
- log.verbose('PuppetService', 'start() instanciating GrpcManager ... done')
148
+ log.info('PuppetService', 'start() instanciating GrpcManager ... done')
149
149
 
150
150
  /**
151
151
  * Huan(202108): when we started the event stream,
@@ -153,35 +153,35 @@ class PuppetService extends PUPPET.Puppet {
153
153
  */
154
154
  this._grpcManager = grpcManager
155
155
 
156
- log.verbose('PuppetService', 'start() setting up bridge grpc event stream ...')
156
+ log.info('PuppetService', 'start() setting up bridge grpc event stream ...')
157
157
  this.bridgeGrpcEventStream(grpcManager)
158
- log.verbose('PuppetService', 'start() setting up bridge grpc event stream ... done')
158
+ log.info('PuppetService', 'start() setting up bridge grpc event stream ... done')
159
159
 
160
- log.verbose('PuppetService', 'start() starting grpc manager...')
160
+ log.info('PuppetService', 'start() starting grpc manager...')
161
161
  const { lastEventSeq } = await this.getEventData()
162
- const accountId = await this._payloadStore.miscellaneous.get('accountId')
162
+ const accountId = await this._payloadStore.miscellaneous?.get('accountId') || ''
163
163
  await grpcManager.start(lastEventSeq, accountId)
164
- log.verbose('PuppetService', 'start() starting grpc manager... done')
164
+ log.info('PuppetService', 'start() starting grpc manager... done')
165
165
 
166
- log.verbose('PuppetService', 'start healthCheck')
166
+ log.info('PuppetService', 'start healthCheck')
167
167
  this.startHealthCheck()
168
168
 
169
- log.verbose('PuppetService', 'onStart() ... done')
169
+ log.info('PuppetService', 'onStart() ... done')
170
170
  }
171
171
 
172
172
  override async onStop (): Promise<void> {
173
- log.verbose('PuppetService', 'onStop()')
173
+ log.info('PuppetService', 'onStop()')
174
174
 
175
175
  if (this._grpcManager) {
176
- log.verbose('PuppetService', 'onStop() stopping grpc manager ...')
176
+ log.info('PuppetService', 'onStop() stopping grpc manager ...')
177
177
  const grpcManager = this._grpcManager
178
178
  this._grpcManager = undefined
179
179
  await grpcManager.stop()
180
- log.verbose('PuppetService', 'onStop() stopping grpc manager ... done')
180
+ log.info('PuppetService', 'onStop() stopping grpc manager ... done')
181
181
  }
182
182
 
183
- log.verbose('PuppetService', 'onStop() ... done')
184
- log.verbose('PuppetService', 'stop healthCheck')
183
+ log.info('PuppetService', 'onStop() ... done')
184
+ log.info('PuppetService', 'stop healthCheck')
185
185
  this.stopHealthCheck()
186
186
  }
187
187
 
@@ -262,10 +262,12 @@ class PuppetService extends PUPPET.Puppet {
262
262
  }
263
263
 
264
264
  if (seq) {
265
- const lastEventSeq = await this._payloadStore.miscellaneous.get('eventSeq')
266
- if (!lastEventSeq || (seq > Number(lastEventSeq) || seq === 1)) {
267
- await this._payloadStore.miscellaneous.set('eventSeq', String(seq))
268
- await this._payloadStore.miscellaneous.set('eventTimestamp', timestamp)
265
+ if (this._payloadStore.miscellaneous) {
266
+ const lastEventSeq = await this._payloadStore.miscellaneous.get('eventSeq')
267
+ if (!lastEventSeq || (seq > Number(lastEventSeq) || seq === 1)) {
268
+ await this._payloadStore.miscellaneous.set('eventSeq', String(seq))
269
+ await this._payloadStore.miscellaneous.set('eventTimestamp', timestamp)
270
+ }
269
271
  }
270
272
  }
271
273
 
@@ -289,12 +291,14 @@ class PuppetService extends PUPPET.Puppet {
289
291
  return
290
292
  }
291
293
  const loginPayload = JSON.parse(payload) as PUPPET.payloads.EventLogin
292
- const accountId = await this._payloadStore.miscellaneous.get('accountId')
293
- if (accountId !== loginPayload.contactId) {
294
- await this._payloadStore.miscellaneous.delete('eventSeq')
294
+ if (this._payloadStore.miscellaneous) {
295
+ const accountId = await this._payloadStore.miscellaneous.get('accountId')
296
+ if (accountId !== loginPayload.contactId) {
297
+ await this._payloadStore.miscellaneous.delete('eventSeq')
298
+ }
299
+ await this._payloadStore.miscellaneous.set('accountId', loginPayload.contactId)
295
300
  }
296
- await this._payloadStore.miscellaneous.set('accountId', loginPayload.contactId)
297
- ;(
301
+ (
298
302
  async () => this.login(loginPayload.contactId)
299
303
  )().catch(e =>
300
304
  log.error('PuppetService', 'onGrpcStreamEvent() this.login() rejection %s',
@@ -306,7 +310,7 @@ class PuppetService extends PUPPET.Puppet {
306
310
  case grpcPuppet.EventType.EVENT_TYPE_LOGOUT:
307
311
  {
308
312
  const logoutPayload = JSON.parse(payload) as PUPPET.payloads.EventLogout
309
- await this._payloadStore.miscellaneous.delete('accountId')
313
+ await this._payloadStore.miscellaneous?.delete('accountId')
310
314
  ;(
311
315
  async () => this.logout(logoutPayload.data)
312
316
  )().catch(e =>
@@ -2649,7 +2653,7 @@ class PuppetService extends PUPPET.Puppet {
2649
2653
  return contactIdsList
2650
2654
  }
2651
2655
 
2652
- healthCheckInterval?: NodeJS.Timer
2656
+ healthCheckInterval?: NodeJS.Timeout
2653
2657
  startHealthCheck () {
2654
2658
  this.healthCheckInterval = setInterval(() => {
2655
2659
  this.ding('healthCheck')
@@ -2686,7 +2690,7 @@ class PuppetService extends PUPPET.Puppet {
2686
2690
 
2687
2691
  this.grpcManager.stopStream()
2688
2692
  const { lastEventSeq } = await this.getEventData()
2689
- const accountId = await this._payloadStore.miscellaneous.get('accountId')
2693
+ const accountId = await this._payloadStore.miscellaneous?.get('accountId') || ''
2690
2694
 
2691
2695
  const onLoginResolve = (resolve: () => void) => {
2692
2696
  const onLogin = (event: grpcPuppet.EventResponse) => {
@@ -2771,8 +2775,8 @@ class PuppetService extends PUPPET.Puppet {
2771
2775
  }
2772
2776
 
2773
2777
  async getEventData () {
2774
- const lastEventTimestamp = await this._payloadStore.miscellaneous.get('eventTimestamp')
2775
- let lastEventSeq = await this._payloadStore.miscellaneous.get('eventSeq')
2778
+ const lastEventTimestamp = await this._payloadStore.miscellaneous?.get('eventTimestamp') || 0
2779
+ let lastEventSeq = await this._payloadStore.miscellaneous?.get('eventSeq')
2776
2780
  if ((Date.now() - Number(lastEventTimestamp || 0)) > this.timeoutMilliseconds) {
2777
2781
  log.warn(`last event was ${(Date.now() - Number(lastEventTimestamp || 0)) / 1000} seconds ago, will not request event cache`)
2778
2782
  lastEventSeq = undefined
@@ -4,7 +4,7 @@
4
4
  import type { PackageJson } from 'type-fest'
5
5
  export const packageJson: PackageJson = {
6
6
  "name": "@juzi/wechaty-puppet-service",
7
- "version": "1.0.73",
7
+ "version": "1.0.74",
8
8
  "description": "Puppet Service for Wechaty",
9
9
  "type": "module",
10
10
  "exports": {