Skip to content

Conversation

@NorthBlue333
Copy link

@NorthBlue333 NorthBlue333 commented Sep 4, 2021

Description

While working on a work project, I had to implement gRPC in Loopback 4. After implementing streams and TLS, I thought it might be useful to others. I am sorry for not following all the rules to contribute to Loopback 4, but I am trying ! I am of course open to suggestions.

This PR adds :

  • Client streaming calls
  • Server streaming calls
  • Bidirectional streaming calls
  • Server TLS
  • Server and client mTLS

Also, I have updated the protoc binaries, and switched to proto-ts plugin to generate Typescript interfaces.

I have also added tests to cover all changes, but editing your hosts file is needed to run both TLS and mTLS tests.

Finally, I tried to add docs for all these implementations.

Note: most packages have been updated too.

Related issues

Checklist

  • New tests added or existing tests modified to cover all changes
  • Code conforms with the style
    guide

Signed-off-by: NorthBlue333 <[email protected]>
@NorthBlue333 NorthBlue333 force-pushed the streamings-and-tls branch 4 times, most recently from dafaf2b to 2ba4d42 Compare September 4, 2021 09:15
@NorthBlue333 NorthBlue333 changed the title Add streamings and tls Add streamings and TLS Sep 4, 2021
@NorthBlue333 NorthBlue333 force-pushed the streamings-and-tls branch 3 times, most recently from 885e7d4 to 3dbf936 Compare September 4, 2021 10:13
Copy link
Member

@achrinza achrinza left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for sharing your contributions, @NorthBlue333!

I've skimmed through and added my initial comments; These are not necessarily requests for changes, but more to initiate the discussion and better understand the changes.

I'll leave it to @raymondfeng to spearhead the review.

Comment on lines 107 to 118
const spec = getLoopbackMetadataFromMethodDescriptor(
findMethodInFileDescriptor(
proto,
target.constructor.name.replace(
options?.controllerNameRegex ?? new RegExp(/(Ctrl|Controller)$/, 'g'),
'',
),
`${methodName[0].toUpperCase()}${
methodName.length ? methodName.slice(1) : ''
}`,
),
);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to enable explicit declaration of the gRPC method name via the decorator instead (i.e. Like how it was before)?

I understand the rationale for inferring this automatically, but IMO doing things auto-magically has its pitfalls of being more difficult to debug. It's also not as aligned with the "LoopBack 4 approach" of giving developers control and flexibility.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well yes. I had done this because of limitations of the compiler.
I am currently trying to do something else, more explicit.

Copy link
Author

@NorthBlue333 NorthBlue333 Sep 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have modified the decorator. It now takes an object as argument, with all the necessary properties. I have also provided a helper function which takes the fileDescriptor and the method definition.
Sadly, I don't see any other way to do this because of how the typescript files are generated from the proto files.

Comment on lines 29 to 45
async clientStreamTest(
request: Observable<TestRequest>,
): Promise<TestResponse> {
const names: string[] = [];
let error;
const observer: PartialObserver<TestRequest> = {
next: (value) => names.push(value.name),
error: (err) => (error = err),
};
request.subscribe(observer);
await lastValueFrom(request);
if (error) {
throw error;
}
return {
message: 'Test ' + request.name,
message: names.join(' '),
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have an in-depth knowledge on rxjs; Is there a benefit over the previous "plain functions" for gRPC?

Copy link
Author

@NorthBlue333 NorthBlue333 Sep 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it adds anything more than encapsulation (and I think it is less efficient). I have updated my PR, using implementations of "plain functions" for @gRPC/grpc-js

@NorthBlue333 NorthBlue333 force-pushed the streamings-and-tls branch 2 times, most recently from 5c14227 to eb8fb44 Compare September 4, 2021 15:17
@NorthBlue333 NorthBlue333 force-pushed the streamings-and-tls branch 2 times, most recently from cd594c6 to 8a5f78b Compare September 4, 2021 18:34
Signed-off-by: NorthBlue333 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants