mirror of
https://github.com/roleypoly/roleypoly-v1.git
synced 2025-04-25 12:19:10 +00:00
[rpc/nats]: fix tests to properly hook handlers, handle encodings
This commit is contained in:
parent
ddabb03461
commit
5c31920289
2 changed files with 21 additions and 10 deletions
|
@ -1,5 +1,9 @@
|
||||||
import NATS, { connect, NatsError } from 'nats'
|
import NATS, { connect, NatsError } from 'nats'
|
||||||
import Bento, { Transport, IBentoSerializer } from '@kayteh/bento'
|
import Bento, { Transport, IBentoSerializer } from '@kayteh/bento'
|
||||||
|
import console = require('console')
|
||||||
|
|
||||||
|
const txtEnc = new TextEncoder()
|
||||||
|
const txtDec = new TextDecoder()
|
||||||
|
|
||||||
export default class NATSTransport extends Transport {
|
export default class NATSTransport extends Transport {
|
||||||
NATS: NATS.Client
|
NATS: NATS.Client
|
||||||
|
@ -7,8 +11,8 @@ export default class NATSTransport extends Transport {
|
||||||
constructor (
|
constructor (
|
||||||
bento: Bento,
|
bento: Bento,
|
||||||
serializer: IBentoSerializer,
|
serializer: IBentoSerializer,
|
||||||
addr: string = 'nats://localhsot:4222/',
|
addr: string = 'nats://localhost:4222/',
|
||||||
private prefix: string = ''
|
private prefix: string = 'roleypoly'
|
||||||
) {
|
) {
|
||||||
super(bento, serializer)
|
super(bento, serializer)
|
||||||
|
|
||||||
|
@ -19,22 +23,25 @@ export default class NATSTransport extends Transport {
|
||||||
}
|
}
|
||||||
|
|
||||||
public hookHandlers = () => {
|
public hookHandlers = () => {
|
||||||
for (const svc in this.bento.serviceRegistry.keys) {
|
for (let service of this.bento.serviceRegistry.keys()) {
|
||||||
this.NATS.subscribe(`${this.prefix}-rpc:${svc}`, this.rpcHandler)
|
this.NATS.subscribe(`rp:${this.prefix}:${service}`, this.rpcHandler)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
rpcHandler = async (request: ArrayBuffer, replyTo: string) => {
|
rpcHandler = async (request: string, replyTo: string) => {
|
||||||
this.NATS.publish(replyTo, await this.receiver({
|
// console.log('rpcHandler', { request, replyTo })
|
||||||
|
const output = await this.receiver({
|
||||||
ctx: {},
|
ctx: {},
|
||||||
buffer: request
|
buffer: txtEnc.encode(request)
|
||||||
}))
|
})
|
||||||
|
this.NATS.publish(replyTo, txtDec.decode(output))
|
||||||
}
|
}
|
||||||
|
|
||||||
sender (data: ArrayBuffer, { service }: { service: string }): Promise<ArrayBuffer> {
|
sender (data: ArrayBuffer, { service }: { service: string }): Promise<ArrayBuffer> {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
this.NATS.requestOne(`${this.prefix}-rpc:${service}`, data, 5000, (incoming: NatsError | Buffer) => {
|
this.NATS.requestOne(`rp:${this.prefix}:${service}`, txtDec.decode(data), 5000, (incoming: NatsError | ArrayBuffer) => {
|
||||||
if (incoming instanceof NatsError) {
|
if (incoming instanceof NatsError) {
|
||||||
|
console.error('NATSError', incoming)
|
||||||
reject(incoming)
|
reject(incoming)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,13 +9,17 @@ const getCC = () => {
|
||||||
const bento2 = new Bento()
|
const bento2 = new Bento()
|
||||||
|
|
||||||
const tt = new NATSTransport(bento, new JSONSerializer({ verbose: true }), 'nats://localhost:4222/', '' + NOW)
|
const tt = new NATSTransport(bento, new JSONSerializer({ verbose: true }), 'nats://localhost:4222/', '' + NOW)
|
||||||
|
const tt2 = new NATSTransport(bento2, new JSONSerializer({ verbose: true }), 'nats://localhost:4222/', '' + NOW)
|
||||||
|
|
||||||
bento.transport = tt
|
bento.transport = tt
|
||||||
bento2.transport = tt
|
bento2.transport = tt2
|
||||||
|
|
||||||
bento.service(MockBackendClient.__SERVICE__, MockBackendServer)
|
bento.service(MockBackendClient.__SERVICE__, MockBackendServer)
|
||||||
const cc = bento2.client(MockBackendClient)
|
const cc = bento2.client(MockBackendClient)
|
||||||
|
|
||||||
|
tt.hookHandlers()
|
||||||
|
tt2.hookHandlers()
|
||||||
|
|
||||||
return cc
|
return cc
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue