@sentio/runtime 2.36.2-rc.1 → 2.37.0-rc.3

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/src/index.ts CHANGED
@@ -5,3 +5,4 @@ export * from './endpoints.js'
5
5
  export * from './chain-config.js'
6
6
  export * from './service.js'
7
7
  export { GLOBAL_CONFIG, type GlobalConfig } from './global-config.js'
8
+ export * from './db-context.js'
package/src/plugin.ts CHANGED
@@ -1,4 +1,6 @@
1
1
  import { DataBinding, HandlerType, ProcessConfigResponse, ProcessResult, StartRequest } from '@sentio/protos'
2
+ import { StoreContext } from './db-context.js'
3
+ import { AsyncLocalStorage } from 'node:async_hooks'
2
4
 
3
5
  export abstract class Plugin {
4
6
  name: string
@@ -13,6 +15,7 @@ export abstract class Plugin {
13
15
  stateDiff(config: ProcessConfigResponse): boolean {
14
16
  return false
15
17
  }
18
+
16
19
  async processBinding(request: DataBinding): Promise<ProcessResult> {
17
20
  return ProcessResult.create()
18
21
  }
@@ -21,6 +24,7 @@ export abstract class Plugin {
21
24
  export class PluginManager {
22
25
  static INSTANCE = new PluginManager()
23
26
 
27
+ dbContextLocalStorage = new AsyncLocalStorage<StoreContext | undefined>()
24
28
  plugins: Plugin[] = []
25
29
  typesToPlugin = new Map<HandlerType, Plugin>()
26
30
 
@@ -54,11 +58,13 @@ export class PluginManager {
54
58
  return this.plugins.some((plugin) => plugin.stateDiff(config))
55
59
  }
56
60
 
57
- processBinding(request: DataBinding): Promise<ProcessResult> {
61
+ processBinding(request: DataBinding, dbContext?: StoreContext): Promise<ProcessResult> {
58
62
  const plugin = this.typesToPlugin.get(request.handlerType)
59
63
  if (!plugin) {
60
64
  throw new Error(`No plugin for ${request.handlerType}`)
61
65
  }
62
- return plugin.processBinding(request)
66
+ return this.dbContextLocalStorage.run(dbContext, () => {
67
+ return plugin.processBinding(request)
68
+ })
63
69
  }
64
70
  }
package/src/service.ts CHANGED
@@ -1,8 +1,11 @@
1
1
  import { CallContext, ServerError, Status } from 'nice-grpc'
2
- import { RichServerError, DebugInfo } from 'nice-grpc-error-details'
2
+ import { DebugInfo, RichServerError } from 'nice-grpc-error-details'
3
+ import { from } from 'ix/Ix.dom.asynciterable.js'
4
+ import { withAbort } from 'ix/Ix.dom.asynciterable.operators.js'
3
5
 
4
6
  import {
5
7
  DataBinding,
8
+ Empty,
6
9
  HandlerType,
7
10
  ProcessBindingResponse,
8
11
  ProcessBindingsRequest,
@@ -10,14 +13,15 @@ import {
10
13
  ProcessConfigResponse,
11
14
  ProcessorServiceImplementation,
12
15
  ProcessResult,
13
- StartRequest,
14
- Empty
16
+ ProcessStreamRequest,
17
+ StartRequest
15
18
  } from '@sentio/protos'
16
19
 
17
20
  import { PluginManager } from './plugin.js'
18
21
  import { errorString, mergeProcessResults } from './utils.js'
19
22
  import { freezeGlobalConfig, GLOBAL_CONFIG } from './global-config.js'
20
23
 
24
+ import { StoreContext } from './db-context.js'
21
25
  ;(BigInt.prototype as any).toJSON = function () {
22
26
  return this.toString()
23
27
  }
@@ -153,19 +157,34 @@ export class ProcessorServiceImpl implements ProcessorServiceImplementation {
153
157
  return result
154
158
  }
155
159
 
156
- async *processBindingsStream(requests: AsyncIterable<DataBinding>, context: CallContext) {
157
- for await (const request of requests) {
158
- const result = await this.processBinding(request)
159
- // let updated = false
160
- // if (PluginManager.INSTANCE.stateDiff(this.processorConfig)) {
161
- // await this.configure()
162
- // updated = true
163
- // }
164
- yield {
165
- result,
166
- configUpdated: result.states?.configUpdated || false
167
- }
160
+ async *processBindingsStream(requests: AsyncIterable<ProcessStreamRequest>, context: CallContext) {
161
+ if (!this.started) {
162
+ throw new ServerError(Status.UNAVAILABLE, 'Service Not started.')
168
163
  }
164
+
165
+ const dbContext = new StoreContext()
166
+ new Promise(async (resolve, reject) => {
167
+ for await (const request of requests) {
168
+ if (request.binding) {
169
+ const binding = request.binding
170
+ PluginManager.INSTANCE.processBinding(binding, dbContext).then((result) => {
171
+ dbContext.subject.next({
172
+ result,
173
+ processId: request.processId
174
+ })
175
+ // dbContext.subject.complete()
176
+ recordRuntimeInfo(result, binding.handlerType)
177
+ })
178
+ }
179
+ if (request.dbResult) {
180
+ dbContext.result(request.dbResult)
181
+ }
182
+ }
183
+ resolve(null)
184
+ }).then(() => {
185
+ dbContext.subject.complete()
186
+ })
187
+ yield* from(dbContext.subject).pipe(withAbort(context.signal))
169
188
  }
170
189
  }
171
190