@hazeljs/grpc 0.2.0-alpha.1
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/LICENSE +192 -0
- package/README.md +161 -0
- package/dist/decorators/grpc-method.decorator.d.ts +32 -0
- package/dist/decorators/grpc-method.decorator.d.ts.map +1 -0
- package/dist/decorators/grpc-method.decorator.js +47 -0
- package/dist/decorators/grpc-method.decorator.test.d.ts +2 -0
- package/dist/decorators/grpc-method.decorator.test.d.ts.map +1 -0
- package/dist/decorators/grpc-method.decorator.test.js +130 -0
- package/dist/grpc.client.d.ts +50 -0
- package/dist/grpc.client.d.ts.map +1 -0
- package/dist/grpc.client.js +188 -0
- package/dist/grpc.client.module.d.ts +65 -0
- package/dist/grpc.client.module.d.ts.map +1 -0
- package/dist/grpc.client.module.js +104 -0
- package/dist/grpc.client.module.test.d.ts +2 -0
- package/dist/grpc.client.module.test.d.ts.map +1 -0
- package/dist/grpc.client.module.test.js +86 -0
- package/dist/grpc.client.test.d.ts +2 -0
- package/dist/grpc.client.test.d.ts.map +1 -0
- package/dist/grpc.client.test.js +216 -0
- package/dist/grpc.client.types.d.ts +66 -0
- package/dist/grpc.client.types.d.ts.map +1 -0
- package/dist/grpc.client.types.js +2 -0
- package/dist/grpc.module.d.ts +73 -0
- package/dist/grpc.module.d.ts.map +1 -0
- package/dist/grpc.module.js +134 -0
- package/dist/grpc.module.test.d.ts +2 -0
- package/dist/grpc.module.test.d.ts.map +1 -0
- package/dist/grpc.module.test.js +208 -0
- package/dist/grpc.server.d.ts +37 -0
- package/dist/grpc.server.d.ts.map +1 -0
- package/dist/grpc.server.js +208 -0
- package/dist/grpc.server.test.d.ts +2 -0
- package/dist/grpc.server.test.d.ts.map +1 -0
- package/dist/grpc.server.test.js +441 -0
- package/dist/grpc.types.d.ts +61 -0
- package/dist/grpc.types.d.ts.map +1 -0
- package/dist/grpc.types.js +2 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +20 -0
- package/package.json +57 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
Copyright 2024 HazelJS Team
|
|
6
|
+
|
|
7
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
8
|
+
you may not use this file except in compliance with the License.
|
|
9
|
+
You may obtain a copy of the License at
|
|
10
|
+
|
|
11
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
12
|
+
|
|
13
|
+
Unless required by applicable law or agreed to in writing, software
|
|
14
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
15
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
16
|
+
See the License for the specific language governing permissions and
|
|
17
|
+
limitations under the License.
|
|
18
|
+
|
|
19
|
+
---
|
|
20
|
+
|
|
21
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
22
|
+
|
|
23
|
+
1. Definitions.
|
|
24
|
+
|
|
25
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
26
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
27
|
+
|
|
28
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
29
|
+
the copyright owner that is granting the License.
|
|
30
|
+
|
|
31
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
32
|
+
other entities that control, are controlled by, or are under common
|
|
33
|
+
control with that entity. For the purposes of this definition,
|
|
34
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
35
|
+
direction or management of such entity, whether by contract or
|
|
36
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
37
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
38
|
+
|
|
39
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
40
|
+
exercising permissions granted by this License.
|
|
41
|
+
|
|
42
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
43
|
+
including but not limited to software source code, documentation
|
|
44
|
+
source, and configuration files.
|
|
45
|
+
|
|
46
|
+
"Object" form shall mean any form resulting from mechanical
|
|
47
|
+
transformation or translation of a Source form, including but
|
|
48
|
+
not limited to compiled object code, generated documentation,
|
|
49
|
+
and conversions to other media types.
|
|
50
|
+
|
|
51
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
52
|
+
Object form, made available under the License, as indicated by a
|
|
53
|
+
copyright notice that is included in or attached to the work
|
|
54
|
+
(an example is provided in the Appendix below).
|
|
55
|
+
|
|
56
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
57
|
+
form, that is based on (or derived from) the Work and for which the
|
|
58
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
59
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
60
|
+
of this License, Derivative Works shall not include works that remain
|
|
61
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
62
|
+
the Work and Derivative Works thereof.
|
|
63
|
+
|
|
64
|
+
"Contribution" shall mean any work of authorship, including
|
|
65
|
+
the original version of the Work and any modifications or additions
|
|
66
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
67
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
68
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
69
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
70
|
+
means any form of electronic, verbal, or written communication sent
|
|
71
|
+
to the Licensor or its representatives, including but not limited to
|
|
72
|
+
communication on electronic mailing lists, source code control systems,
|
|
73
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
74
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
75
|
+
excluding communication that is conspicuously marked or otherwise
|
|
76
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
77
|
+
|
|
78
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
79
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
80
|
+
subsequently incorporated within the Work.
|
|
81
|
+
|
|
82
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
83
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
84
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
85
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
86
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
87
|
+
Work and such Derivative Works in Source or Object form.
|
|
88
|
+
|
|
89
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
90
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
91
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
92
|
+
(except as stated in this section) patent license to make, have made,
|
|
93
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
94
|
+
where such license applies only to those patent claims licensable
|
|
95
|
+
by such Contributor that are necessarily infringed by their
|
|
96
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
97
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
98
|
+
institute patent litigation against any entity (including a
|
|
99
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
100
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
101
|
+
or contributory patent infringement, then any patent licenses
|
|
102
|
+
granted to You under this License for that Work shall terminate
|
|
103
|
+
as of the date such litigation is filed.
|
|
104
|
+
|
|
105
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
106
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
107
|
+
modifications, and in Source or Object form, provided that You
|
|
108
|
+
meet the following conditions:
|
|
109
|
+
|
|
110
|
+
(a) You must give any other recipients of the Work or
|
|
111
|
+
Derivative Works a copy of this License; and
|
|
112
|
+
|
|
113
|
+
(b) You must cause any modified files to carry prominent notices
|
|
114
|
+
stating that You changed the files; and
|
|
115
|
+
|
|
116
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
117
|
+
that You distribute, all copyright, patent, trademark, and
|
|
118
|
+
attribution notices from the Source form of the Work,
|
|
119
|
+
excluding those notices that do not pertain to any part of
|
|
120
|
+
the Derivative Works; and
|
|
121
|
+
|
|
122
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
123
|
+
distribution, then any Derivative Works that You distribute must
|
|
124
|
+
include a readable copy of the attribution notices contained
|
|
125
|
+
within such NOTICE file, excluding those notices that do not
|
|
126
|
+
pertain to any part of the Derivative Works, in at least one
|
|
127
|
+
of the following places: within a NOTICE text file distributed
|
|
128
|
+
as part of the Derivative Works; within the Source form or
|
|
129
|
+
documentation, if provided along with the Derivative Works; or,
|
|
130
|
+
within a display generated by the Derivative Works, if and
|
|
131
|
+
wherever such third-party notices normally appear. The contents
|
|
132
|
+
of the NOTICE file are for informational purposes only and
|
|
133
|
+
do not modify the License. You may add Your own attribution
|
|
134
|
+
notices within Derivative Works that You distribute, alongside
|
|
135
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
136
|
+
that such additional attribution notices cannot be construed
|
|
137
|
+
as modifying the License.
|
|
138
|
+
|
|
139
|
+
You may add Your own copyright statement to Your modifications and
|
|
140
|
+
may provide additional or different license terms and conditions
|
|
141
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
142
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
143
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
144
|
+
the conditions stated in this License.
|
|
145
|
+
|
|
146
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
147
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
148
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
149
|
+
this License, without any additional terms or conditions.
|
|
150
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
151
|
+
the terms of any separate license agreement you may have executed
|
|
152
|
+
with Licensor regarding such Contributions.
|
|
153
|
+
|
|
154
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
155
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
156
|
+
except as required for reasonable and customary use in describing the
|
|
157
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
158
|
+
|
|
159
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
160
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
161
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
162
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
163
|
+
implied, including, without limitation, any warranties or conditions
|
|
164
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
165
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
166
|
+
appropriateness of using or redistributing the Work and assume any
|
|
167
|
+
risks associated with Your exercise of permissions under this License.
|
|
168
|
+
|
|
169
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
170
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
171
|
+
unless required by applicable law (such as deliberate and grossly
|
|
172
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
173
|
+
liable to You for damages, including any direct, indirect, special,
|
|
174
|
+
incidental, or consequential damages of any character arising as a
|
|
175
|
+
result of this License or out of the use or inability to use the
|
|
176
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
177
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
178
|
+
other commercial damages or losses), even if such Contributor
|
|
179
|
+
has been advised of the possibility of such damages.
|
|
180
|
+
|
|
181
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
182
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
183
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
184
|
+
or other liability obligations and/or rights consistent with this
|
|
185
|
+
License. However, in accepting such obligations, You may act only
|
|
186
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
187
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
188
|
+
defend, and hold each Contributor harmless for any liability
|
|
189
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
190
|
+
of your accepting any such warranty or additional liability.
|
|
191
|
+
|
|
192
|
+
END OF TERMS AND CONDITIONS
|
package/README.md
ADDED
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
# @hazeljs/grpc
|
|
2
|
+
|
|
3
|
+
**gRPC Module for HazelJS**
|
|
4
|
+
|
|
5
|
+
RPC server support with decorator-based handlers. Built on [@grpc/grpc-js](https://www.npmjs.com/package/@grpc/grpc-js) and [@grpc/proto-loader](https://www.npmjs.com/package/@grpc/proto-loader).
|
|
6
|
+
|
|
7
|
+
[](https://www.npmjs.com/package/@hazeljs/grpc)
|
|
8
|
+
[](https://www.npmjs.com/package/@hazeljs/grpc)
|
|
9
|
+
[](https://www.apache.org/licenses/LICENSE-2.0)
|
|
10
|
+
|
|
11
|
+
## Features
|
|
12
|
+
|
|
13
|
+
- **Decorator-Based API** - `@GrpcMethod()` for declarative RPC handlers
|
|
14
|
+
- **DI Integration** - Controllers are resolved from the HazelJS container
|
|
15
|
+
- **Proto Loading** - Load `.proto` files at runtime with configurable options
|
|
16
|
+
- **Unary RPC** - Support for request-response RPC methods
|
|
17
|
+
|
|
18
|
+
## Installation
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
npm install @hazeljs/grpc
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Quick Start
|
|
25
|
+
|
|
26
|
+
### 1. Define your service in a .proto file
|
|
27
|
+
|
|
28
|
+
```proto
|
|
29
|
+
syntax = "proto3";
|
|
30
|
+
package hero;
|
|
31
|
+
|
|
32
|
+
service HeroService {
|
|
33
|
+
rpc FindOne (HeroById) returns (Hero);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
message HeroById {
|
|
37
|
+
int32 id = 1;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
message Hero {
|
|
41
|
+
int32 id = 1;
|
|
42
|
+
string name = 2;
|
|
43
|
+
}
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
### 2. Import GrpcModule and create a controller
|
|
47
|
+
|
|
48
|
+
```typescript
|
|
49
|
+
import { Injectable } from '@hazeljs/core';
|
|
50
|
+
import { GrpcMethod } from '@hazeljs/grpc';
|
|
51
|
+
import { join } from 'path';
|
|
52
|
+
import { HazelModule } from '@hazeljs/core';
|
|
53
|
+
import { GrpcModule } from '@hazeljs/grpc';
|
|
54
|
+
|
|
55
|
+
@Injectable()
|
|
56
|
+
export class HeroGrpcController {
|
|
57
|
+
@GrpcMethod('HeroService', 'FindOne')
|
|
58
|
+
findOne(data: { id: number }) {
|
|
59
|
+
return { id: data.id, name: 'Hero' };
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
@HazelModule({
|
|
64
|
+
imports: [
|
|
65
|
+
GrpcModule.forRoot({
|
|
66
|
+
protoPath: join(__dirname, 'hero.proto'),
|
|
67
|
+
package: 'hero',
|
|
68
|
+
url: '0.0.0.0:50051',
|
|
69
|
+
}),
|
|
70
|
+
],
|
|
71
|
+
providers: [HeroGrpcController],
|
|
72
|
+
})
|
|
73
|
+
export class AppModule {}
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
### 3. Register handlers and start the gRPC server
|
|
77
|
+
|
|
78
|
+
```typescript
|
|
79
|
+
import { HazelApp } from '@hazeljs/core';
|
|
80
|
+
import { GrpcModule, GrpcServer } from '@hazeljs/grpc';
|
|
81
|
+
import { Container } from '@hazeljs/core';
|
|
82
|
+
import { AppModule } from './app.module';
|
|
83
|
+
import { HeroGrpcController } from './hero.grpc-controller';
|
|
84
|
+
|
|
85
|
+
async function bootstrap() {
|
|
86
|
+
const app = new HazelApp(AppModule);
|
|
87
|
+
|
|
88
|
+
// Register gRPC handlers from controllers
|
|
89
|
+
GrpcModule.registerHandlersFromProviders([HeroGrpcController]);
|
|
90
|
+
|
|
91
|
+
// Start HTTP server
|
|
92
|
+
await app.listen(3000);
|
|
93
|
+
|
|
94
|
+
// Start gRPC server (runs on separate port)
|
|
95
|
+
const grpcServer = Container.getInstance().resolve(GrpcServer);
|
|
96
|
+
await grpcServer.start();
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
bootstrap();
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
## Configuration
|
|
103
|
+
|
|
104
|
+
```typescript
|
|
105
|
+
GrpcModule.forRoot({
|
|
106
|
+
protoPath: join(__dirname, 'hero.proto'), // or ['a.proto', 'b.proto']
|
|
107
|
+
package: 'hero', // package name from .proto
|
|
108
|
+
url: '0.0.0.0:50051', // bind address (default: 0.0.0.0:50051)
|
|
109
|
+
loader: { // @grpc/proto-loader options
|
|
110
|
+
keepCase: true,
|
|
111
|
+
longs: String,
|
|
112
|
+
enums: String,
|
|
113
|
+
defaults: true,
|
|
114
|
+
oneofs: true,
|
|
115
|
+
},
|
|
116
|
+
isGlobal: true, // global module (default)
|
|
117
|
+
});
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
## Async Configuration
|
|
121
|
+
|
|
122
|
+
```typescript
|
|
123
|
+
GrpcModule.forRootAsync({
|
|
124
|
+
useFactory: async (config: ConfigService) => ({
|
|
125
|
+
protoPath: config.get('GRPC_PROTO_PATH'),
|
|
126
|
+
package: config.get('GRPC_PACKAGE'),
|
|
127
|
+
url: config.get('GRPC_URL'),
|
|
128
|
+
}),
|
|
129
|
+
inject: [ConfigService],
|
|
130
|
+
});
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
## @GrpcMethod Decorator
|
|
134
|
+
|
|
135
|
+
```typescript
|
|
136
|
+
// Explicit service and method name
|
|
137
|
+
@GrpcMethod('HeroService', 'FindOne')
|
|
138
|
+
findOne(data: { id: number }) {
|
|
139
|
+
return { id: data.id, name: 'Hero' };
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// Method name defaults to the decorated method name
|
|
143
|
+
@GrpcMethod('HeroService')
|
|
144
|
+
findOne(data: { id: number }) {
|
|
145
|
+
return { id: data.id, name: 'Hero' };
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// Async handlers are supported
|
|
149
|
+
@GrpcMethod('HeroService', 'FindOne')
|
|
150
|
+
async findOne(data: { id: number }) {
|
|
151
|
+
const hero = await this.heroRepository.findById(data.id);
|
|
152
|
+
return hero;
|
|
153
|
+
}
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
## API
|
|
157
|
+
|
|
158
|
+
- **GrpcModule** - Module with `forRoot(options)`, `forRootAsync(options)`, `registerHandlersFromProvider(provider)`, `registerHandlersFromProviders(providerClasses)`
|
|
159
|
+
- **GrpcServer** - Injectable service with `configure(options)`, `start()`, `close()`, `registerHandlersFromProvider(provider)`, `registerHandlersFromProviders(providerClasses)`
|
|
160
|
+
- **GrpcMethod(serviceName, methodName?)** - Decorator for RPC method handlers
|
|
161
|
+
- **getGrpcMethodMetadata(target)** - Get @GrpcMethod metadata from a class
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import 'reflect-metadata';
|
|
2
|
+
import type { GrpcMethodMetadata } from '../grpc.types';
|
|
3
|
+
/**
|
|
4
|
+
* Metadata key for gRPC method handlers
|
|
5
|
+
*/
|
|
6
|
+
export declare const GRPC_METHOD_METADATA_KEY: unique symbol;
|
|
7
|
+
/**
|
|
8
|
+
* Decorator to mark a method as a gRPC RPC handler
|
|
9
|
+
*
|
|
10
|
+
* @param serviceName - Service name as defined in the .proto file (e.g. 'HeroService')
|
|
11
|
+
* @param methodName - Method name as defined in the .proto file. Defaults to the decorated method name.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```typescript
|
|
15
|
+
* @GrpcMethod('HeroService', 'FindOne')
|
|
16
|
+
* findOne(data: { id: number }) {
|
|
17
|
+
* return { id: data.id, name: 'Hero' };
|
|
18
|
+
* }
|
|
19
|
+
*
|
|
20
|
+
* // Method name defaults to decorated method name
|
|
21
|
+
* @GrpcMethod('HeroService')
|
|
22
|
+
* findOne(data: { id: number }) {
|
|
23
|
+
* return { id: data.id, name: 'Hero' };
|
|
24
|
+
* }
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
export declare function GrpcMethod(serviceName: string, methodName?: string): MethodDecorator;
|
|
28
|
+
/**
|
|
29
|
+
* Get @GrpcMethod metadata from a class
|
|
30
|
+
*/
|
|
31
|
+
export declare function getGrpcMethodMetadata(target: object): GrpcMethodMetadata[];
|
|
32
|
+
//# sourceMappingURL=grpc-method.decorator.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"grpc-method.decorator.d.ts","sourceRoot":"","sources":["../../src/decorators/grpc-method.decorator.ts"],"names":[],"mappings":"AAAA,OAAO,kBAAkB,CAAC;AAC1B,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAC;AAExD;;GAEG;AACH,eAAO,MAAM,wBAAwB,eAAwB,CAAC;AAE9D;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,UAAU,CAAC,WAAW,EAAE,MAAM,EAAE,UAAU,CAAC,EAAE,MAAM,GAAG,eAAe,CAapF;AAED;;GAEG;AACH,wBAAgB,qBAAqB,CAAC,MAAM,EAAE,MAAM,GAAG,kBAAkB,EAAE,CAE1E"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.GRPC_METHOD_METADATA_KEY = void 0;
|
|
4
|
+
exports.GrpcMethod = GrpcMethod;
|
|
5
|
+
exports.getGrpcMethodMetadata = getGrpcMethodMetadata;
|
|
6
|
+
require("reflect-metadata");
|
|
7
|
+
/**
|
|
8
|
+
* Metadata key for gRPC method handlers
|
|
9
|
+
*/
|
|
10
|
+
exports.GRPC_METHOD_METADATA_KEY = Symbol('grpc:method');
|
|
11
|
+
/**
|
|
12
|
+
* Decorator to mark a method as a gRPC RPC handler
|
|
13
|
+
*
|
|
14
|
+
* @param serviceName - Service name as defined in the .proto file (e.g. 'HeroService')
|
|
15
|
+
* @param methodName - Method name as defined in the .proto file. Defaults to the decorated method name.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```typescript
|
|
19
|
+
* @GrpcMethod('HeroService', 'FindOne')
|
|
20
|
+
* findOne(data: { id: number }) {
|
|
21
|
+
* return { id: data.id, name: 'Hero' };
|
|
22
|
+
* }
|
|
23
|
+
*
|
|
24
|
+
* // Method name defaults to decorated method name
|
|
25
|
+
* @GrpcMethod('HeroService')
|
|
26
|
+
* findOne(data: { id: number }) {
|
|
27
|
+
* return { id: data.id, name: 'Hero' };
|
|
28
|
+
* }
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
function GrpcMethod(serviceName, methodName) {
|
|
32
|
+
return (target, propertyKey, _descriptor) => {
|
|
33
|
+
const existing = Reflect.getMetadata(exports.GRPC_METHOD_METADATA_KEY, target.constructor) || [];
|
|
34
|
+
existing.push({
|
|
35
|
+
service: serviceName,
|
|
36
|
+
method: methodName ?? propertyKey.toString(),
|
|
37
|
+
methodName: propertyKey.toString(),
|
|
38
|
+
});
|
|
39
|
+
Reflect.defineMetadata(exports.GRPC_METHOD_METADATA_KEY, existing, target.constructor);
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Get @GrpcMethod metadata from a class
|
|
44
|
+
*/
|
|
45
|
+
function getGrpcMethodMetadata(target) {
|
|
46
|
+
return Reflect.getMetadata(exports.GRPC_METHOD_METADATA_KEY, target.constructor) || [];
|
|
47
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"grpc-method.decorator.test.d.ts","sourceRoot":"","sources":["../../src/decorators/grpc-method.decorator.test.ts"],"names":[],"mappings":"AAAA,OAAO,kBAAkB,CAAC"}
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
require("reflect-metadata");
|
|
13
|
+
const grpc_method_decorator_1 = require("./grpc-method.decorator");
|
|
14
|
+
describe('GrpcMethod decorator', () => {
|
|
15
|
+
it('should define GRPC_METHOD_METADATA_KEY', () => {
|
|
16
|
+
expect(typeof grpc_method_decorator_1.GRPC_METHOD_METADATA_KEY).toBe('symbol');
|
|
17
|
+
});
|
|
18
|
+
it('should add metadata for service and method', () => {
|
|
19
|
+
class TestController {
|
|
20
|
+
findOne(_data) {
|
|
21
|
+
return { id: 1, name: 'Hero' };
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
__decorate([
|
|
25
|
+
(0, grpc_method_decorator_1.GrpcMethod)('HeroService', 'FindOne'),
|
|
26
|
+
__metadata("design:type", Function),
|
|
27
|
+
__metadata("design:paramtypes", [Object]),
|
|
28
|
+
__metadata("design:returntype", void 0)
|
|
29
|
+
], TestController.prototype, "findOne", null);
|
|
30
|
+
const metadata = (0, grpc_method_decorator_1.getGrpcMethodMetadata)(new TestController());
|
|
31
|
+
expect(metadata).toHaveLength(1);
|
|
32
|
+
expect(metadata[0].service).toBe('HeroService');
|
|
33
|
+
expect(metadata[0].method).toBe('FindOne');
|
|
34
|
+
expect(metadata[0].methodName).toBe('findOne');
|
|
35
|
+
});
|
|
36
|
+
it('should default method name to property key when not provided', () => {
|
|
37
|
+
class TestController {
|
|
38
|
+
findOne(_data) {
|
|
39
|
+
return { id: 1, name: 'Hero' };
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
__decorate([
|
|
43
|
+
(0, grpc_method_decorator_1.GrpcMethod)('HeroService'),
|
|
44
|
+
__metadata("design:type", Function),
|
|
45
|
+
__metadata("design:paramtypes", [Object]),
|
|
46
|
+
__metadata("design:returntype", void 0)
|
|
47
|
+
], TestController.prototype, "findOne", null);
|
|
48
|
+
const metadata = (0, grpc_method_decorator_1.getGrpcMethodMetadata)(new TestController());
|
|
49
|
+
expect(metadata[0].method).toBe('findOne');
|
|
50
|
+
expect(metadata[0].methodName).toBe('findOne');
|
|
51
|
+
});
|
|
52
|
+
it('should add metadata for multiple methods on same class', () => {
|
|
53
|
+
class TestController {
|
|
54
|
+
findOne(_data) {
|
|
55
|
+
return { id: 1, name: 'Hero' };
|
|
56
|
+
}
|
|
57
|
+
findAll(_data) {
|
|
58
|
+
return [];
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
__decorate([
|
|
62
|
+
(0, grpc_method_decorator_1.GrpcMethod)('HeroService', 'FindOne'),
|
|
63
|
+
__metadata("design:type", Function),
|
|
64
|
+
__metadata("design:paramtypes", [Object]),
|
|
65
|
+
__metadata("design:returntype", void 0)
|
|
66
|
+
], TestController.prototype, "findOne", null);
|
|
67
|
+
__decorate([
|
|
68
|
+
(0, grpc_method_decorator_1.GrpcMethod)('HeroService', 'FindAll'),
|
|
69
|
+
__metadata("design:type", Function),
|
|
70
|
+
__metadata("design:paramtypes", [Object]),
|
|
71
|
+
__metadata("design:returntype", void 0)
|
|
72
|
+
], TestController.prototype, "findAll", null);
|
|
73
|
+
const metadata = (0, grpc_method_decorator_1.getGrpcMethodMetadata)(new TestController());
|
|
74
|
+
expect(metadata).toHaveLength(2);
|
|
75
|
+
expect(metadata[0].service).toBe('HeroService');
|
|
76
|
+
expect(metadata[0].method).toBe('FindOne');
|
|
77
|
+
expect(metadata[1].service).toBe('HeroService');
|
|
78
|
+
expect(metadata[1].method).toBe('FindAll');
|
|
79
|
+
});
|
|
80
|
+
it('should support different services on same class', () => {
|
|
81
|
+
class TestController {
|
|
82
|
+
findHero(_data) {
|
|
83
|
+
return { id: 1, name: 'Hero' };
|
|
84
|
+
}
|
|
85
|
+
getUser(_data) {
|
|
86
|
+
return { id: '1', name: 'User' };
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
__decorate([
|
|
90
|
+
(0, grpc_method_decorator_1.GrpcMethod)('HeroService', 'FindOne'),
|
|
91
|
+
__metadata("design:type", Function),
|
|
92
|
+
__metadata("design:paramtypes", [Object]),
|
|
93
|
+
__metadata("design:returntype", void 0)
|
|
94
|
+
], TestController.prototype, "findHero", null);
|
|
95
|
+
__decorate([
|
|
96
|
+
(0, grpc_method_decorator_1.GrpcMethod)('UserService', 'GetUser'),
|
|
97
|
+
__metadata("design:type", Function),
|
|
98
|
+
__metadata("design:paramtypes", [Object]),
|
|
99
|
+
__metadata("design:returntype", void 0)
|
|
100
|
+
], TestController.prototype, "getUser", null);
|
|
101
|
+
const metadata = (0, grpc_method_decorator_1.getGrpcMethodMetadata)(new TestController());
|
|
102
|
+
expect(metadata).toHaveLength(2);
|
|
103
|
+
expect(metadata[0].service).toBe('HeroService');
|
|
104
|
+
expect(metadata[1].service).toBe('UserService');
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
describe('getGrpcMethodMetadata', () => {
|
|
108
|
+
it('should return empty array for class without @GrpcMethod decorators', () => {
|
|
109
|
+
class PlainClass {
|
|
110
|
+
}
|
|
111
|
+
const metadata = (0, grpc_method_decorator_1.getGrpcMethodMetadata)(new PlainClass());
|
|
112
|
+
expect(metadata).toEqual([]);
|
|
113
|
+
});
|
|
114
|
+
it('should return metadata from class constructor', () => {
|
|
115
|
+
class TestController {
|
|
116
|
+
testMethod() { }
|
|
117
|
+
}
|
|
118
|
+
__decorate([
|
|
119
|
+
(0, grpc_method_decorator_1.GrpcMethod)('TestService', 'TestMethod'),
|
|
120
|
+
__metadata("design:type", Function),
|
|
121
|
+
__metadata("design:paramtypes", []),
|
|
122
|
+
__metadata("design:returntype", void 0)
|
|
123
|
+
], TestController.prototype, "testMethod", null);
|
|
124
|
+
const instance = new TestController();
|
|
125
|
+
const metadata = (0, grpc_method_decorator_1.getGrpcMethodMetadata)(instance);
|
|
126
|
+
expect(metadata).toHaveLength(1);
|
|
127
|
+
expect(metadata[0].service).toBe('TestService');
|
|
128
|
+
expect(metadata[0].method).toBe('TestMethod');
|
|
129
|
+
});
|
|
130
|
+
});
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { GrpcClientOptions } from './grpc.client.types';
|
|
2
|
+
type GrpcClientStub = Record<string, (req: unknown, cb: (err: Error | null, res: unknown) => void) => void>;
|
|
3
|
+
/**
|
|
4
|
+
* gRPC Client service - creates and caches gRPC client stubs for calling remote gRPC services
|
|
5
|
+
*/
|
|
6
|
+
export declare class GrpcClientService {
|
|
7
|
+
private packageDefinition;
|
|
8
|
+
private protoDescriptor;
|
|
9
|
+
private options;
|
|
10
|
+
private stubCache;
|
|
11
|
+
/**
|
|
12
|
+
* Configure the client with proto path and options
|
|
13
|
+
*/
|
|
14
|
+
configure(options: GrpcClientOptions): void;
|
|
15
|
+
/**
|
|
16
|
+
* Resolve URL from Discovery when configured
|
|
17
|
+
*/
|
|
18
|
+
private resolveUrl;
|
|
19
|
+
/**
|
|
20
|
+
* Build filter for Discovery.
|
|
21
|
+
* Passes user filter; for protocol: 'grpc' use metadata: { protocol: 'grpc' }
|
|
22
|
+
* since ServiceFilter filters by metadata.
|
|
23
|
+
*/
|
|
24
|
+
private buildDiscoveryFilter;
|
|
25
|
+
/**
|
|
26
|
+
* Get a gRPC client stub for the given service.
|
|
27
|
+
* When Discovery is configured and url is omitted, resolves URL from Discovery.
|
|
28
|
+
* Stubs are cached per (serviceName, url).
|
|
29
|
+
*
|
|
30
|
+
* @param serviceName - Service name as defined in proto (e.g. 'ProductService')
|
|
31
|
+
* @param url - Optional URL (e.g. 'localhost:50051'). Required if not using Discovery and no defaultUrl.
|
|
32
|
+
* @returns gRPC client stub with RPC methods
|
|
33
|
+
*/
|
|
34
|
+
getClient(serviceName: string, url?: string): GrpcClientStub;
|
|
35
|
+
/**
|
|
36
|
+
* Get a gRPC client stub asynchronously.
|
|
37
|
+
* Use this when Discovery is configured, as URL resolution is async.
|
|
38
|
+
*
|
|
39
|
+
* @param serviceName - Service name as defined in proto (e.g. 'ProductService')
|
|
40
|
+
* @param url - Optional URL. When omitted and Discovery is configured, resolves from Discovery.
|
|
41
|
+
* @returns Promise resolving to gRPC client stub
|
|
42
|
+
*/
|
|
43
|
+
getClientAsync(serviceName: string, url?: string): Promise<GrpcClientStub>;
|
|
44
|
+
/**
|
|
45
|
+
* Close all cached client channels (for graceful shutdown)
|
|
46
|
+
*/
|
|
47
|
+
close(): void;
|
|
48
|
+
}
|
|
49
|
+
export {};
|
|
50
|
+
//# sourceMappingURL=grpc.client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"grpc.client.d.ts","sourceRoot":"","sources":["../src/grpc.client.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EACV,iBAAiB,EAGlB,MAAM,qBAAqB,CAAC;AAE7B,KAAK,cAAc,GAAG,MAAM,CAC1B,MAAM,EACN,CAAC,GAAG,EAAE,OAAO,EAAE,EAAE,EAAE,CAAC,GAAG,EAAE,KAAK,GAAG,IAAI,EAAE,GAAG,EAAE,OAAO,KAAK,IAAI,KAAK,IAAI,CACtE,CAAC;AAEF;;GAEG;AACH,qBACa,iBAAiB;IAC5B,OAAO,CAAC,iBAAiB,CAAkC;IAC3D,OAAO,CAAC,eAAe,CAAwC;IAC/D,OAAO,CAAC,OAAO,CAAkC;IACjD,OAAO,CAAC,SAAS,CAAkD;IAEnE;;OAEG;IACH,SAAS,CAAC,OAAO,EAAE,iBAAiB,GAAG,IAAI;IAwB3C;;OAEG;YACW,UAAU;IA0BxB;;;;OAIG;IACH,OAAO,CAAC,oBAAoB;IAI5B;;;;;;;;OAQG;IACH,SAAS,CAAC,WAAW,EAAE,MAAM,EAAE,GAAG,CAAC,EAAE,MAAM,GAAG,cAAc;IA2D5D;;;;;;;OAOG;IACG,cAAc,CAAC,WAAW,EAAE,MAAM,EAAE,GAAG,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,cAAc,CAAC;IAKhF;;OAEG;IACH,KAAK,IAAI,IAAI;CAOd"}
|