@hazeljs/grpc 0.2.0-beta.32
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 +160 -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/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.server.d.ts +37 -0
- package/dist/grpc.server.d.ts.map +1 -0
- package/dist/grpc.server.js +209 -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 +11 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +16 -0
- package/package.json +56 -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,160 @@
|
|
|
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.apache.org/licenses/LICENSE-2.0)
|
|
9
|
+
|
|
10
|
+
## Features
|
|
11
|
+
|
|
12
|
+
- **Decorator-Based API** - `@GrpcMethod()` for declarative RPC handlers
|
|
13
|
+
- **DI Integration** - Controllers are resolved from the HazelJS container
|
|
14
|
+
- **Proto Loading** - Load `.proto` files at runtime with configurable options
|
|
15
|
+
- **Unary RPC** - Support for request-response RPC methods
|
|
16
|
+
|
|
17
|
+
## Installation
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
npm install @hazeljs/grpc
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Quick Start
|
|
24
|
+
|
|
25
|
+
### 1. Define your service in a .proto file
|
|
26
|
+
|
|
27
|
+
```proto
|
|
28
|
+
syntax = "proto3";
|
|
29
|
+
package hero;
|
|
30
|
+
|
|
31
|
+
service HeroService {
|
|
32
|
+
rpc FindOne (HeroById) returns (Hero);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
message HeroById {
|
|
36
|
+
int32 id = 1;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
message Hero {
|
|
40
|
+
int32 id = 1;
|
|
41
|
+
string name = 2;
|
|
42
|
+
}
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
### 2. Import GrpcModule and create a controller
|
|
46
|
+
|
|
47
|
+
```typescript
|
|
48
|
+
import { Injectable } from '@hazeljs/core';
|
|
49
|
+
import { GrpcMethod } from '@hazeljs/grpc';
|
|
50
|
+
import { join } from 'path';
|
|
51
|
+
import { HazelModule } from '@hazeljs/core';
|
|
52
|
+
import { GrpcModule } from '@hazeljs/grpc';
|
|
53
|
+
|
|
54
|
+
@Injectable()
|
|
55
|
+
export class HeroGrpcController {
|
|
56
|
+
@GrpcMethod('HeroService', 'FindOne')
|
|
57
|
+
findOne(data: { id: number }) {
|
|
58
|
+
return { id: data.id, name: 'Hero' };
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
@HazelModule({
|
|
63
|
+
imports: [
|
|
64
|
+
GrpcModule.forRoot({
|
|
65
|
+
protoPath: join(__dirname, 'hero.proto'),
|
|
66
|
+
package: 'hero',
|
|
67
|
+
url: '0.0.0.0:50051',
|
|
68
|
+
}),
|
|
69
|
+
],
|
|
70
|
+
providers: [HeroGrpcController],
|
|
71
|
+
})
|
|
72
|
+
export class AppModule {}
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
### 3. Register handlers and start the gRPC server
|
|
76
|
+
|
|
77
|
+
```typescript
|
|
78
|
+
import { HazelApp } from '@hazeljs/core';
|
|
79
|
+
import { GrpcModule, GrpcServer } from '@hazeljs/grpc';
|
|
80
|
+
import { Container } from '@hazeljs/core';
|
|
81
|
+
import { AppModule } from './app.module';
|
|
82
|
+
import { HeroGrpcController } from './hero.grpc-controller';
|
|
83
|
+
|
|
84
|
+
async function bootstrap() {
|
|
85
|
+
const app = new HazelApp(AppModule);
|
|
86
|
+
|
|
87
|
+
// Register gRPC handlers from controllers
|
|
88
|
+
GrpcModule.registerHandlersFromProviders([HeroGrpcController]);
|
|
89
|
+
|
|
90
|
+
// Start HTTP server
|
|
91
|
+
await app.listen(3000);
|
|
92
|
+
|
|
93
|
+
// Start gRPC server (runs on separate port)
|
|
94
|
+
const grpcServer = Container.getInstance().resolve(GrpcServer);
|
|
95
|
+
await grpcServer.start();
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
bootstrap();
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
## Configuration
|
|
102
|
+
|
|
103
|
+
```typescript
|
|
104
|
+
GrpcModule.forRoot({
|
|
105
|
+
protoPath: join(__dirname, 'hero.proto'), // or ['a.proto', 'b.proto']
|
|
106
|
+
package: 'hero', // package name from .proto
|
|
107
|
+
url: '0.0.0.0:50051', // bind address (default: 0.0.0.0:50051)
|
|
108
|
+
loader: { // @grpc/proto-loader options
|
|
109
|
+
keepCase: true,
|
|
110
|
+
longs: String,
|
|
111
|
+
enums: String,
|
|
112
|
+
defaults: true,
|
|
113
|
+
oneofs: true,
|
|
114
|
+
},
|
|
115
|
+
isGlobal: true, // global module (default)
|
|
116
|
+
});
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
## Async Configuration
|
|
120
|
+
|
|
121
|
+
```typescript
|
|
122
|
+
GrpcModule.forRootAsync({
|
|
123
|
+
useFactory: async (config: ConfigService) => ({
|
|
124
|
+
protoPath: config.get('GRPC_PROTO_PATH'),
|
|
125
|
+
package: config.get('GRPC_PACKAGE'),
|
|
126
|
+
url: config.get('GRPC_URL'),
|
|
127
|
+
}),
|
|
128
|
+
inject: [ConfigService],
|
|
129
|
+
});
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
## @GrpcMethod Decorator
|
|
133
|
+
|
|
134
|
+
```typescript
|
|
135
|
+
// Explicit service and method name
|
|
136
|
+
@GrpcMethod('HeroService', 'FindOne')
|
|
137
|
+
findOne(data: { id: number }) {
|
|
138
|
+
return { id: data.id, name: 'Hero' };
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// Method name defaults to the decorated method name
|
|
142
|
+
@GrpcMethod('HeroService')
|
|
143
|
+
findOne(data: { id: number }) {
|
|
144
|
+
return { id: data.id, name: 'Hero' };
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// Async handlers are supported
|
|
148
|
+
@GrpcMethod('HeroService', 'FindOne')
|
|
149
|
+
async findOne(data: { id: number }) {
|
|
150
|
+
const hero = await this.heroRepository.findById(data.id);
|
|
151
|
+
return hero;
|
|
152
|
+
}
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
## API
|
|
156
|
+
|
|
157
|
+
- **GrpcModule** - Module with `forRoot(options)`, `forRootAsync(options)`, `registerHandlersFromProvider(provider)`, `registerHandlersFromProviders(providerClasses)`
|
|
158
|
+
- **GrpcServer** - Injectable service with `configure(options)`, `start()`, `close()`, `registerHandlersFromProvider(provider)`, `registerHandlersFromProviders(providerClasses)`
|
|
159
|
+
- **GrpcMethod(serviceName, methodName?)** - Decorator for RPC method handlers
|
|
160
|
+
- **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,73 @@
|
|
|
1
|
+
import { GrpcServer } from './grpc.server';
|
|
2
|
+
import type { GrpcModuleConfig, GrpcModuleOptions } from './grpc.types';
|
|
3
|
+
/**
|
|
4
|
+
* gRPC module for HazelJS
|
|
5
|
+
* Provides gRPC server support with decorator-based RPC handlers
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```typescript
|
|
9
|
+
* // app.module.ts
|
|
10
|
+
* @HazelModule({
|
|
11
|
+
* imports: [
|
|
12
|
+
* GrpcModule.forRoot({
|
|
13
|
+
* protoPath: join(__dirname, 'hero.proto'),
|
|
14
|
+
* package: 'hero',
|
|
15
|
+
* url: '0.0.0.0:50051',
|
|
16
|
+
* })
|
|
17
|
+
* ],
|
|
18
|
+
* providers: [HeroGrpcController],
|
|
19
|
+
* })
|
|
20
|
+
* export class AppModule {}
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
export declare class GrpcModule {
|
|
24
|
+
/**
|
|
25
|
+
* Configure gRPC module with options
|
|
26
|
+
*/
|
|
27
|
+
static forRoot(options: GrpcModuleConfig): {
|
|
28
|
+
module: typeof GrpcModule;
|
|
29
|
+
providers: Array<{
|
|
30
|
+
provide: typeof GrpcServer;
|
|
31
|
+
useFactory: () => GrpcServer;
|
|
32
|
+
}>;
|
|
33
|
+
exports: Array<typeof GrpcServer>;
|
|
34
|
+
global: boolean;
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* Configure gRPC module asynchronously
|
|
38
|
+
*/
|
|
39
|
+
static forRootAsync(options: {
|
|
40
|
+
useFactory: (...args: unknown[]) => Promise<GrpcModuleOptions> | GrpcModuleOptions;
|
|
41
|
+
inject?: unknown[];
|
|
42
|
+
}): {
|
|
43
|
+
module: typeof GrpcModule;
|
|
44
|
+
providers: Array<{
|
|
45
|
+
provide: string | typeof GrpcServer;
|
|
46
|
+
useFactory: unknown;
|
|
47
|
+
inject?: unknown[];
|
|
48
|
+
}>;
|
|
49
|
+
exports: Array<typeof GrpcServer>;
|
|
50
|
+
global: boolean;
|
|
51
|
+
};
|
|
52
|
+
/**
|
|
53
|
+
* Register gRPC handlers from a provider instance (with @GrpcMethod decorators)
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* ```typescript
|
|
57
|
+
* const heroController = container.resolve(HeroGrpcController);
|
|
58
|
+
* GrpcModule.registerHandlersFromProvider(heroController);
|
|
59
|
+
* ```
|
|
60
|
+
*/
|
|
61
|
+
static registerHandlersFromProvider(provider: object): void;
|
|
62
|
+
/**
|
|
63
|
+
* Register gRPC handlers from multiple provider classes
|
|
64
|
+
* Resolves each from the container and registers their @GrpcMethod handlers
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* ```typescript
|
|
68
|
+
* GrpcModule.registerHandlersFromProviders([HeroGrpcController, UserGrpcController]);
|
|
69
|
+
* ```
|
|
70
|
+
*/
|
|
71
|
+
static registerHandlersFromProviders(providerClasses: (new (...args: unknown[]) => object)[]): void;
|
|
72
|
+
}
|
|
73
|
+
//# sourceMappingURL=grpc.module.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"grpc.module.d.ts","sourceRoot":"","sources":["../src/grpc.module.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAC3C,OAAO,KAAK,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AAExE;;;;;;;;;;;;;;;;;;;GAmBG;AACH,qBAIa,UAAU;IACrB;;OAEG;IACH,MAAM,CAAC,OAAO,CAAC,OAAO,EAAE,gBAAgB,GAAG;QACzC,MAAM,EAAE,OAAO,UAAU,CAAC;QAC1B,SAAS,EAAE,KAAK,CAAC;YACf,OAAO,EAAE,OAAO,UAAU,CAAC;YAC3B,UAAU,EAAE,MAAM,UAAU,CAAC;SAC9B,CAAC,CAAC;QACH,OAAO,EAAE,KAAK,CAAC,OAAO,UAAU,CAAC,CAAC;QAClC,MAAM,EAAE,OAAO,CAAC;KACjB;IAsBD;;OAEG;IACH,MAAM,CAAC,YAAY,CAAC,OAAO,EAAE;QAC3B,UAAU,EAAE,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,OAAO,CAAC,iBAAiB,CAAC,GAAG,iBAAiB,CAAC;QACnF,MAAM,CAAC,EAAE,OAAO,EAAE,CAAC;KACpB,GAAG;QACF,MAAM,EAAE,OAAO,UAAU,CAAC;QAC1B,SAAS,EAAE,KAAK,CAAC;YACf,OAAO,EAAE,MAAM,GAAG,OAAO,UAAU,CAAC;YACpC,UAAU,EAAE,OAAO,CAAC;YACpB,MAAM,CAAC,EAAE,OAAO,EAAE,CAAC;SACpB,CAAC,CAAC;QACH,OAAO,EAAE,KAAK,CAAC,OAAO,UAAU,CAAC,CAAC;QAClC,MAAM,EAAE,OAAO,CAAC;KACjB;IAwBD;;;;;;;;OAQG;IACH,MAAM,CAAC,4BAA4B,CAAC,QAAQ,EAAE,MAAM,GAAG,IAAI;IAgB3D;;;;;;;;OAQG;IACH,MAAM,CAAC,6BAA6B,CAClC,eAAe,EAAE,CAAC,KAAK,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,MAAM,CAAC,EAAE,GACtD,IAAI;CAWR"}
|
|
@@ -0,0 +1,134 @@
|
|
|
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 __importDefault = (this && this.__importDefault) || function (mod) {
|
|
9
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
10
|
+
};
|
|
11
|
+
var GrpcModule_1;
|
|
12
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
+
exports.GrpcModule = void 0;
|
|
14
|
+
const core_1 = require("@hazeljs/core");
|
|
15
|
+
const core_2 = require("@hazeljs/core");
|
|
16
|
+
const core_3 = __importDefault(require("@hazeljs/core"));
|
|
17
|
+
const grpc_server_1 = require("./grpc.server");
|
|
18
|
+
/**
|
|
19
|
+
* gRPC module for HazelJS
|
|
20
|
+
* Provides gRPC server support with decorator-based RPC handlers
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```typescript
|
|
24
|
+
* // app.module.ts
|
|
25
|
+
* @HazelModule({
|
|
26
|
+
* imports: [
|
|
27
|
+
* GrpcModule.forRoot({
|
|
28
|
+
* protoPath: join(__dirname, 'hero.proto'),
|
|
29
|
+
* package: 'hero',
|
|
30
|
+
* url: '0.0.0.0:50051',
|
|
31
|
+
* })
|
|
32
|
+
* ],
|
|
33
|
+
* providers: [HeroGrpcController],
|
|
34
|
+
* })
|
|
35
|
+
* export class AppModule {}
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
let GrpcModule = GrpcModule_1 = class GrpcModule {
|
|
39
|
+
/**
|
|
40
|
+
* Configure gRPC module with options
|
|
41
|
+
*/
|
|
42
|
+
static forRoot(options) {
|
|
43
|
+
const { isGlobal = true, ...grpcOptions } = options;
|
|
44
|
+
core_3.default.info('Configuring gRPC module...');
|
|
45
|
+
const grpcServerProvider = {
|
|
46
|
+
provide: grpc_server_1.GrpcServer,
|
|
47
|
+
useFactory: () => {
|
|
48
|
+
const server = new grpc_server_1.GrpcServer();
|
|
49
|
+
server.configure(grpcOptions);
|
|
50
|
+
return server;
|
|
51
|
+
},
|
|
52
|
+
};
|
|
53
|
+
return {
|
|
54
|
+
module: GrpcModule_1,
|
|
55
|
+
providers: [grpcServerProvider],
|
|
56
|
+
exports: [grpc_server_1.GrpcServer],
|
|
57
|
+
global: isGlobal,
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Configure gRPC module asynchronously
|
|
62
|
+
*/
|
|
63
|
+
static forRootAsync(options) {
|
|
64
|
+
return {
|
|
65
|
+
module: GrpcModule_1,
|
|
66
|
+
providers: [
|
|
67
|
+
{
|
|
68
|
+
provide: 'GRPC_OPTIONS',
|
|
69
|
+
useFactory: options.useFactory,
|
|
70
|
+
inject: options.inject || [],
|
|
71
|
+
},
|
|
72
|
+
{
|
|
73
|
+
provide: grpc_server_1.GrpcServer,
|
|
74
|
+
useFactory: (grpcOptions) => {
|
|
75
|
+
const server = new grpc_server_1.GrpcServer();
|
|
76
|
+
server.configure(grpcOptions);
|
|
77
|
+
return server;
|
|
78
|
+
},
|
|
79
|
+
inject: ['GRPC_OPTIONS'],
|
|
80
|
+
},
|
|
81
|
+
],
|
|
82
|
+
exports: [grpc_server_1.GrpcServer],
|
|
83
|
+
global: true,
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Register gRPC handlers from a provider instance (with @GrpcMethod decorators)
|
|
88
|
+
*
|
|
89
|
+
* @example
|
|
90
|
+
* ```typescript
|
|
91
|
+
* const heroController = container.resolve(HeroGrpcController);
|
|
92
|
+
* GrpcModule.registerHandlersFromProvider(heroController);
|
|
93
|
+
* ```
|
|
94
|
+
*/
|
|
95
|
+
static registerHandlersFromProvider(provider) {
|
|
96
|
+
try {
|
|
97
|
+
const container = core_2.Container.getInstance();
|
|
98
|
+
const grpcServer = container.resolve(grpc_server_1.GrpcServer);
|
|
99
|
+
if (!grpcServer) {
|
|
100
|
+
core_3.default.warn('GrpcServer not found in DI container');
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
grpcServer.registerHandlersFromProvider(provider);
|
|
104
|
+
}
|
|
105
|
+
catch (error) {
|
|
106
|
+
core_3.default.error('Error registering gRPC handlers from provider:', error);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Register gRPC handlers from multiple provider classes
|
|
111
|
+
* Resolves each from the container and registers their @GrpcMethod handlers
|
|
112
|
+
*
|
|
113
|
+
* @example
|
|
114
|
+
* ```typescript
|
|
115
|
+
* GrpcModule.registerHandlersFromProviders([HeroGrpcController, UserGrpcController]);
|
|
116
|
+
* ```
|
|
117
|
+
*/
|
|
118
|
+
static registerHandlersFromProviders(providerClasses) {
|
|
119
|
+
const container = core_2.Container.getInstance();
|
|
120
|
+
const grpcServer = container.resolve(grpc_server_1.GrpcServer);
|
|
121
|
+
if (!grpcServer) {
|
|
122
|
+
core_3.default.warn('GrpcServer not found in DI container');
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
grpcServer.registerHandlersFromProviders(providerClasses);
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
exports.GrpcModule = GrpcModule;
|
|
129
|
+
exports.GrpcModule = GrpcModule = GrpcModule_1 = __decorate([
|
|
130
|
+
(0, core_1.HazelModule)({
|
|
131
|
+
providers: [],
|
|
132
|
+
exports: [],
|
|
133
|
+
})
|
|
134
|
+
], GrpcModule);
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import * as grpc from '@grpc/grpc-js';
|
|
2
|
+
import type { GrpcModuleOptions } from './grpc.types';
|
|
3
|
+
/**
|
|
4
|
+
* gRPC Server service - manages gRPC server lifecycle and RPC handler registration
|
|
5
|
+
*/
|
|
6
|
+
export declare class GrpcServer {
|
|
7
|
+
private server;
|
|
8
|
+
private packageDefinition;
|
|
9
|
+
private protoDescriptor;
|
|
10
|
+
private options;
|
|
11
|
+
private handlerMap;
|
|
12
|
+
/**
|
|
13
|
+
* Configure the server with proto path and options
|
|
14
|
+
*/
|
|
15
|
+
configure(options: GrpcModuleOptions): void;
|
|
16
|
+
/**
|
|
17
|
+
* Register RPC handlers from a provider instance (with @GrpcMethod decorators)
|
|
18
|
+
*/
|
|
19
|
+
registerHandlersFromProvider(provider: object): void;
|
|
20
|
+
/**
|
|
21
|
+
* Register handlers from multiple provider classes (resolved from DI container)
|
|
22
|
+
*/
|
|
23
|
+
registerHandlersFromProviders(providerClasses: (new (...args: unknown[]) => object)[]): void;
|
|
24
|
+
/**
|
|
25
|
+
* Start the gRPC server and bind to the configured URL
|
|
26
|
+
*/
|
|
27
|
+
start(): Promise<void>;
|
|
28
|
+
/**
|
|
29
|
+
* Shutdown the gRPC server gracefully
|
|
30
|
+
*/
|
|
31
|
+
close(): Promise<void>;
|
|
32
|
+
/**
|
|
33
|
+
* Get the underlying gRPC Server instance (for advanced use)
|
|
34
|
+
*/
|
|
35
|
+
getServer(): grpc.Server | null;
|
|
36
|
+
}
|
|
37
|
+
//# sourceMappingURL=grpc.server.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"grpc.server.d.ts","sourceRoot":"","sources":["../src/grpc.server.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,IAAI,MAAM,eAAe,CAAC;AAItC,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AAStD;;GAEG;AACH,qBACa,UAAU;IACrB,OAAO,CAAC,MAAM,CAA4B;IAC1C,OAAO,CAAC,iBAAiB,CAAkC;IAC3D,OAAO,CAAC,eAAe,CAAwC;IAC/D,OAAO,CAAC,OAAO,CAAkC;IACjD,OAAO,CAAC,UAAU,CACN;IAEZ;;OAEG;IACH,SAAS,CAAC,OAAO,EAAE,iBAAiB,GAAG,IAAI;IAuB3C;;OAEG;IACH,4BAA4B,CAAC,QAAQ,EAAE,MAAM,GAAG,IAAI;IA2BpD;;OAEG;IACH,6BAA6B,CAAC,eAAe,EAAE,CAAC,KAAK,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,MAAM,CAAC,EAAE,GAAG,IAAI;IAY5F;;OAEG;IACG,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAqE5B;;OAEG;IACG,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAmB5B;;OAEG;IACH,SAAS,IAAI,IAAI,CAAC,MAAM,GAAG,IAAI;CAGhC"}
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
19
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
20
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
21
|
+
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;
|
|
22
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
23
|
+
};
|
|
24
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
25
|
+
var ownKeys = function(o) {
|
|
26
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
27
|
+
var ar = [];
|
|
28
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
29
|
+
return ar;
|
|
30
|
+
};
|
|
31
|
+
return ownKeys(o);
|
|
32
|
+
};
|
|
33
|
+
return function (mod) {
|
|
34
|
+
if (mod && mod.__esModule) return mod;
|
|
35
|
+
var result = {};
|
|
36
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
37
|
+
__setModuleDefault(result, mod);
|
|
38
|
+
return result;
|
|
39
|
+
};
|
|
40
|
+
})();
|
|
41
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
42
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
43
|
+
};
|
|
44
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
45
|
+
exports.GrpcServer = void 0;
|
|
46
|
+
const core_1 = require("@hazeljs/core");
|
|
47
|
+
const grpc = __importStar(require("@grpc/grpc-js"));
|
|
48
|
+
const protoLoader = __importStar(require("@grpc/proto-loader"));
|
|
49
|
+
const core_2 = __importDefault(require("@hazeljs/core"));
|
|
50
|
+
const grpc_method_decorator_1 = require("./decorators/grpc-method.decorator");
|
|
51
|
+
const core_3 = require("@hazeljs/core");
|
|
52
|
+
/**
|
|
53
|
+
* gRPC Server service - manages gRPC server lifecycle and RPC handler registration
|
|
54
|
+
*/
|
|
55
|
+
let GrpcServer = class GrpcServer {
|
|
56
|
+
constructor() {
|
|
57
|
+
this.server = null;
|
|
58
|
+
this.packageDefinition = null;
|
|
59
|
+
this.protoDescriptor = null;
|
|
60
|
+
this.options = null;
|
|
61
|
+
this.handlerMap = new Map();
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Configure the server with proto path and options
|
|
65
|
+
*/
|
|
66
|
+
configure(options) {
|
|
67
|
+
this.options = options;
|
|
68
|
+
const protoPath = Array.isArray(options.protoPath) ? options.protoPath : [options.protoPath];
|
|
69
|
+
const loaderOptions = {
|
|
70
|
+
keepCase: true,
|
|
71
|
+
longs: String,
|
|
72
|
+
enums: String,
|
|
73
|
+
defaults: true,
|
|
74
|
+
oneofs: true,
|
|
75
|
+
...(options.loader ?? {}),
|
|
76
|
+
};
|
|
77
|
+
this.packageDefinition = protoLoader.loadSync(protoPath, loaderOptions);
|
|
78
|
+
this.protoDescriptor = grpc.loadPackageDefinition(this.packageDefinition);
|
|
79
|
+
core_2.default.info('gRPC server configured', {
|
|
80
|
+
package: options.package,
|
|
81
|
+
url: options.url ?? '0.0.0.0:50051',
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Register RPC handlers from a provider instance (with @GrpcMethod decorators)
|
|
86
|
+
*/
|
|
87
|
+
registerHandlersFromProvider(provider) {
|
|
88
|
+
const metadataList = (0, grpc_method_decorator_1.getGrpcMethodMetadata)(provider);
|
|
89
|
+
for (const meta of metadataList) {
|
|
90
|
+
const instance = provider;
|
|
91
|
+
const callback = instance[meta.methodName];
|
|
92
|
+
if (typeof callback !== 'function') {
|
|
93
|
+
core_2.default.warn(`@GrpcMethod: method "${meta.methodName}" not found on ${provider.constructor?.name}`);
|
|
94
|
+
continue;
|
|
95
|
+
}
|
|
96
|
+
const boundCallback = callback.bind(provider);
|
|
97
|
+
const handler = (data) => boundCallback(data);
|
|
98
|
+
let serviceMap = this.handlerMap.get(meta.service);
|
|
99
|
+
if (!serviceMap) {
|
|
100
|
+
serviceMap = new Map();
|
|
101
|
+
this.handlerMap.set(meta.service, serviceMap);
|
|
102
|
+
}
|
|
103
|
+
serviceMap.set(meta.method, handler);
|
|
104
|
+
core_2.default.debug(`Registered gRPC handler: ${meta.service}/${meta.method}`);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Register handlers from multiple provider classes (resolved from DI container)
|
|
109
|
+
*/
|
|
110
|
+
registerHandlersFromProviders(providerClasses) {
|
|
111
|
+
const container = core_3.Container.getInstance();
|
|
112
|
+
for (const Cls of providerClasses) {
|
|
113
|
+
const instance = container.resolve(Cls);
|
|
114
|
+
if (instance) {
|
|
115
|
+
this.registerHandlersFromProvider(instance);
|
|
116
|
+
}
|
|
117
|
+
else {
|
|
118
|
+
core_2.default.warn(`Provider ${Cls.name} not found in DI container`);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Start the gRPC server and bind to the configured URL
|
|
124
|
+
*/
|
|
125
|
+
async start() {
|
|
126
|
+
if (!this.options || !this.protoDescriptor) {
|
|
127
|
+
throw new Error('GrpcServer not configured. Call configure() or use GrpcModule.forRoot() first.');
|
|
128
|
+
}
|
|
129
|
+
const pkg = this.options.package;
|
|
130
|
+
const pkgObj = this.protoDescriptor[pkg];
|
|
131
|
+
if (!pkgObj) {
|
|
132
|
+
throw new Error(`Package "${pkg}" not found in proto descriptor. Check your proto file and package name.`);
|
|
133
|
+
}
|
|
134
|
+
this.server = new grpc.Server();
|
|
135
|
+
for (const [serviceName, serviceMap] of this.handlerMap) {
|
|
136
|
+
const serviceDef = pkgObj[serviceName];
|
|
137
|
+
if (!serviceDef?.service) {
|
|
138
|
+
core_2.default.warn(`Service "${serviceName}" not found in proto. Skipping.`);
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
141
|
+
const implementation = {};
|
|
142
|
+
for (const [methodName, handler] of serviceMap) {
|
|
143
|
+
implementation[methodName] = (call, callback) => {
|
|
144
|
+
const request = call.request;
|
|
145
|
+
try {
|
|
146
|
+
const result = handler(request);
|
|
147
|
+
if (result instanceof Promise) {
|
|
148
|
+
result
|
|
149
|
+
.then((r) => callback(null, r))
|
|
150
|
+
.catch((err) => callback(err, null));
|
|
151
|
+
}
|
|
152
|
+
else {
|
|
153
|
+
callback(null, result);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
catch (err) {
|
|
157
|
+
callback(err, null);
|
|
158
|
+
}
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
this.server.addService(serviceDef.service, implementation);
|
|
162
|
+
core_2.default.info(`Added gRPC service: ${serviceName}`);
|
|
163
|
+
}
|
|
164
|
+
const url = this.options.url ?? '0.0.0.0:50051';
|
|
165
|
+
return new Promise((resolve, reject) => {
|
|
166
|
+
this.server.bindAsync(url, grpc.ServerCredentials.createInsecure(), (err) => {
|
|
167
|
+
if (err) {
|
|
168
|
+
core_2.default.error('gRPC server bind failed:', err);
|
|
169
|
+
reject(err);
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
this.server.start();
|
|
173
|
+
core_2.default.info(`gRPC server listening on ${url}`);
|
|
174
|
+
resolve();
|
|
175
|
+
});
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Shutdown the gRPC server gracefully
|
|
180
|
+
*/
|
|
181
|
+
async close() {
|
|
182
|
+
if (!this.server)
|
|
183
|
+
return;
|
|
184
|
+
return new Promise((resolve) => {
|
|
185
|
+
this.server.tryShutdown((err) => {
|
|
186
|
+
if (err) {
|
|
187
|
+
core_2.default.warn('gRPC server graceful shutdown failed, forcing:', err);
|
|
188
|
+
this.server.forceShutdown();
|
|
189
|
+
}
|
|
190
|
+
this.server = null;
|
|
191
|
+
this.packageDefinition = null;
|
|
192
|
+
this.protoDescriptor = null;
|
|
193
|
+
this.handlerMap.clear();
|
|
194
|
+
core_2.default.info('gRPC server closed');
|
|
195
|
+
resolve();
|
|
196
|
+
});
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Get the underlying gRPC Server instance (for advanced use)
|
|
201
|
+
*/
|
|
202
|
+
getServer() {
|
|
203
|
+
return this.server;
|
|
204
|
+
}
|
|
205
|
+
};
|
|
206
|
+
exports.GrpcServer = GrpcServer;
|
|
207
|
+
exports.GrpcServer = GrpcServer = __decorate([
|
|
208
|
+
(0, core_1.Injectable)()
|
|
209
|
+
], GrpcServer);
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Options for @grpc/proto-loader
|
|
3
|
+
* @see https://www.npmjs.com/package/@grpc/proto-loader
|
|
4
|
+
*/
|
|
5
|
+
export interface ProtoLoaderOptions {
|
|
6
|
+
keepCase?: boolean;
|
|
7
|
+
longs?: string | number;
|
|
8
|
+
enums?: string;
|
|
9
|
+
bytes?: string | string[];
|
|
10
|
+
defaults?: boolean;
|
|
11
|
+
arrays?: boolean;
|
|
12
|
+
objects?: boolean;
|
|
13
|
+
oneofs?: boolean;
|
|
14
|
+
json?: boolean;
|
|
15
|
+
includeDirs?: string[];
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* gRPC module configuration options
|
|
19
|
+
*/
|
|
20
|
+
export interface GrpcModuleOptions {
|
|
21
|
+
/**
|
|
22
|
+
* Path to .proto file(s). Can be a single path or array of paths.
|
|
23
|
+
*/
|
|
24
|
+
protoPath: string | string[];
|
|
25
|
+
/**
|
|
26
|
+
* Package name as defined in the .proto file (e.g. 'hero', 'math')
|
|
27
|
+
*/
|
|
28
|
+
package: string;
|
|
29
|
+
/**
|
|
30
|
+
* Address to bind the gRPC server (e.g. '0.0.0.0:50051')
|
|
31
|
+
* @default '0.0.0.0:50051'
|
|
32
|
+
*/
|
|
33
|
+
url?: string;
|
|
34
|
+
/**
|
|
35
|
+
* Options passed to @grpc/proto-loader
|
|
36
|
+
*/
|
|
37
|
+
loader?: ProtoLoaderOptions;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Extended options for GrpcModule.forRoot
|
|
41
|
+
*/
|
|
42
|
+
export interface GrpcModuleConfig extends GrpcModuleOptions {
|
|
43
|
+
/**
|
|
44
|
+
* Whether this is a global module
|
|
45
|
+
* @default true
|
|
46
|
+
*/
|
|
47
|
+
isGlobal?: boolean;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Metadata for @GrpcMethod decorator
|
|
51
|
+
*/
|
|
52
|
+
export interface GrpcMethodMetadata {
|
|
53
|
+
service: string;
|
|
54
|
+
method: string;
|
|
55
|
+
methodName: string;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Loaded package definition from proto files (from loadPackageDefinition)
|
|
59
|
+
*/
|
|
60
|
+
export type GrpcPackageDefinition = Record<string, unknown>;
|
|
61
|
+
//# sourceMappingURL=grpc.types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"grpc.types.d.ts","sourceRoot":"","sources":["../src/grpc.types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,WAAW,kBAAkB;IACjC,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,KAAK,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;IACxB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,KAAK,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;IAC1B,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,IAAI,CAAC,EAAE,OAAO,CAAC;IACf,WAAW,CAAC,EAAE,MAAM,EAAE,CAAC;CACxB;AAED;;GAEG;AACH,MAAM,WAAW,iBAAiB;IAChC;;OAEG;IACH,SAAS,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;IAE7B;;OAEG;IACH,OAAO,EAAE,MAAM,CAAC;IAEhB;;;OAGG;IACH,GAAG,CAAC,EAAE,MAAM,CAAC;IAEb;;OAEG;IACH,MAAM,CAAC,EAAE,kBAAkB,CAAC;CAC7B;AAED;;GAEG;AACH,MAAM,WAAW,gBAAiB,SAAQ,iBAAiB;IACzD;;;OAGG;IACH,QAAQ,CAAC,EAAE,OAAO,CAAC;CACpB;AAED;;GAEG;AACH,MAAM,WAAW,kBAAkB;IACjC,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,EAAE,MAAM,CAAC;IACf,UAAU,EAAE,MAAM,CAAC;CACpB;AAED;;GAEG;AACH,MAAM,MAAM,qBAAqB,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @hazeljs/grpc - gRPC module for HazelJS
|
|
3
|
+
*
|
|
4
|
+
* RPC server support with decorator-based handlers.
|
|
5
|
+
* Built on @grpc/grpc-js and @grpc/proto-loader.
|
|
6
|
+
*/
|
|
7
|
+
export { GrpcModule } from './grpc.module';
|
|
8
|
+
export { GrpcServer } from './grpc.server';
|
|
9
|
+
export { GrpcMethod, getGrpcMethodMetadata } from './decorators/grpc-method.decorator';
|
|
10
|
+
export type { GrpcModuleOptions, GrpcModuleConfig, GrpcMethodMetadata, ProtoLoaderOptions, GrpcPackageDefinition, } from './grpc.types';
|
|
11
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAC3C,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAC3C,OAAO,EAAE,UAAU,EAAE,qBAAqB,EAAE,MAAM,oCAAoC,CAAC;AACvF,YAAY,EACV,iBAAiB,EACjB,gBAAgB,EAChB,kBAAkB,EAClB,kBAAkB,EAClB,qBAAqB,GACtB,MAAM,cAAc,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @hazeljs/grpc - gRPC module for HazelJS
|
|
4
|
+
*
|
|
5
|
+
* RPC server support with decorator-based handlers.
|
|
6
|
+
* Built on @grpc/grpc-js and @grpc/proto-loader.
|
|
7
|
+
*/
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.getGrpcMethodMetadata = exports.GrpcMethod = exports.GrpcServer = exports.GrpcModule = void 0;
|
|
10
|
+
var grpc_module_1 = require("./grpc.module");
|
|
11
|
+
Object.defineProperty(exports, "GrpcModule", { enumerable: true, get: function () { return grpc_module_1.GrpcModule; } });
|
|
12
|
+
var grpc_server_1 = require("./grpc.server");
|
|
13
|
+
Object.defineProperty(exports, "GrpcServer", { enumerable: true, get: function () { return grpc_server_1.GrpcServer; } });
|
|
14
|
+
var grpc_method_decorator_1 = require("./decorators/grpc-method.decorator");
|
|
15
|
+
Object.defineProperty(exports, "GrpcMethod", { enumerable: true, get: function () { return grpc_method_decorator_1.GrpcMethod; } });
|
|
16
|
+
Object.defineProperty(exports, "getGrpcMethodMetadata", { enumerable: true, get: function () { return grpc_method_decorator_1.getGrpcMethodMetadata; } });
|
package/package.json
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@hazeljs/grpc",
|
|
3
|
+
"version": "0.2.0-beta.32",
|
|
4
|
+
"description": "gRPC module for HazelJS framework - RPC server and client support with decorators",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist"
|
|
9
|
+
],
|
|
10
|
+
"scripts": {
|
|
11
|
+
"build": "tsc",
|
|
12
|
+
"test": "jest --coverage --passWithNoTests",
|
|
13
|
+
"lint": "eslint \"src/**/*.ts\"",
|
|
14
|
+
"lint:fix": "eslint \"src/**/*.ts\" --fix",
|
|
15
|
+
"clean": "rm -rf dist"
|
|
16
|
+
},
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"@grpc/grpc-js": "^1.13.0",
|
|
19
|
+
"@grpc/proto-loader": "^0.7.13"
|
|
20
|
+
},
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"@types/node": "^20.17.50",
|
|
23
|
+
"@typescript-eslint/eslint-plugin": "^8.18.2",
|
|
24
|
+
"@typescript-eslint/parser": "^8.18.2",
|
|
25
|
+
"eslint": "^8.56.0",
|
|
26
|
+
"jest": "^29.7.0",
|
|
27
|
+
"ts-jest": "^29.1.2",
|
|
28
|
+
"typescript": "^5.3.3"
|
|
29
|
+
},
|
|
30
|
+
"peerDependencies": {
|
|
31
|
+
"@hazeljs/core": ">=0.2.0-beta.0"
|
|
32
|
+
},
|
|
33
|
+
"publishConfig": {
|
|
34
|
+
"access": "public"
|
|
35
|
+
},
|
|
36
|
+
"repository": {
|
|
37
|
+
"type": "git",
|
|
38
|
+
"url": "git+https://github.com/hazel-js/hazeljs.git",
|
|
39
|
+
"directory": "packages/grpc"
|
|
40
|
+
},
|
|
41
|
+
"keywords": [
|
|
42
|
+
"hazeljs",
|
|
43
|
+
"grpc",
|
|
44
|
+
"rpc",
|
|
45
|
+
"microservices",
|
|
46
|
+
"protobuf",
|
|
47
|
+
"decorators"
|
|
48
|
+
],
|
|
49
|
+
"author": "Muhammad Arslan <muhammad.arslan@hazeljs.com>",
|
|
50
|
+
"license": "Apache-2.0",
|
|
51
|
+
"bugs": {
|
|
52
|
+
"url": "https://github.com/hazeljs/hazel-js/issues"
|
|
53
|
+
},
|
|
54
|
+
"homepage": "https://hazeljs.com",
|
|
55
|
+
"gitHead": "d162e4ebd54900d43e31a34c2c81ac520999d24a"
|
|
56
|
+
}
|