Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional wrapper function to callTarget #96

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 21 additions & 17 deletions soluto-kafka-grpc-target/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,35 +19,39 @@ const packageDefinition = loadSync(PROTO_PATH, {
});
const ProtobufMessage = loadPackageDefinition(packageDefinition) as ProtobufMessage;

const _callTarget = (run: any) => async (call: any, callback: any) => {
const _callTarget = (run: any, handlerWrapper?: any) => async (call: any, callback: any) => {
try {
const receivedTimestamp = Date.now();
const payload = JSON.parse(call.request.msgJson);
await run({
payload,
headers: {
recordOffset: parseInt(call.request.recordOffset) || -1,
recordTimestamp: parseInt(call.request.recordTimestamp) || -1,
topic: call.request.topic,
recordHeaders: call.request.headersJson ? JSON.parse(call.request.headersJson) : undefined,
},
});
callback(null, {statusCode: 200, receivedTimestamp, completedTimestamp: Date.now()});
handlerWrapper ? await handlerWrapper(() => handle(run,callback)) : await handle(run,callback);
} catch (e) {
callback(null, {statusCode: e.statusCode ?? e.status ?? 500});
}
};

const getServer = (execute: any) => {
const handle = async (run, callback) => {
const receivedTimestamp = Date.now();
const payload = JSON.parse(call.request.msgJson);
await run({
payload,
headers: {
recordOffset: parseInt(call.request.recordOffset) || -1,
recordTimestamp: parseInt(call.request.recordTimestamp) || -1,
topic: call.request.topic,
recordHeaders: call.request.headersJson ? JSON.parse(call.request.headersJson) : undefined,
},
});
callback(null, {statusCode: 200, receivedTimestamp, completedTimestamp: Date.now()});
}

const getServer = (execute: any, handlerWrapper?: any) => {
const server = new Server();
server.addService(ProtobufMessage.CallTarget.service, {
callTarget: _callTarget(execute),
callTarget: _callTarget(execute, handlerWrapper),
});
return server;
};

export const startServer = (port: string, execute: any) => {
const routeServer = getServer(execute);
export const startServer = (port: string, execute: any, handlerWrapper?: any) => {
const routeServer = getServer(execute, handlerWrapper);
routeServer.bind(`0.0.0.0:${port}`, ServerCredentials.createInsecure());
routeServer.start();
return routeServer;
Expand Down
2 changes: 1 addition & 1 deletion soluto-kafka-grpc-target/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@npmsoluto/soluto-kafka-grpc-target",
"version": "0.9.2",
"version": "0.9.3",
"main": "dist/index.js",
"repository": "https://github.com/Soluto-Private/soluto-kafka/",
"scripts": {
Expand Down