@atls/nestjs-grpc-http-proxy 0.0.15 → 0.0.17
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/CHANGELOG.md +26 -0
- package/dist/authenticators/authentication.service.d.ts +1 -1
- package/dist/authenticators/private-key.authenticator.js +3 -3
- package/dist/controllers/grpc-http-proxy.controller.d.ts +3 -3
- package/dist/controllers/grpc-http-proxy.controller.js +1 -1
- package/dist/module/grpc-http-proxy-module-options.interface.d.ts +4 -2
- package/dist/module/grpc-http-proxy.module.js +8 -1
- package/dist/proto/proto.client.d.ts +5 -2
- package/dist/proto/proto.client.js +18 -6
- package/dist/proto/proto.registry.d.ts +1 -1
- package/dist/proto/proto.registry.js +6 -4
- package/package.json +1 -1
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
## [0.0.17](https://github.com/atls/nestjs/compare/@atls/nestjs-grpc-http-proxy@0.0.16...@atls/nestjs-grpc-http-proxy@0.0.17) (2026-01-08)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
## 0.0.15 (2025-12-31)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
### Bug Fixes
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
* **packages:** linter errors ([204ce22](https://github.com/atls/nestjs/commit/204ce229e375b09ffd69d93e47c08bd1d1fbea1b))
|
|
17
|
+
* **packages:** linter errors ([d41854b](https://github.com/atls/nestjs/commit/d41854b94d5502ebf2b9e18d22cac33f98c8fc41))
|
|
18
|
+
* **packages:** tests ([ae0f308](https://github.com/atls/nestjs/commit/ae0f308e695cfe39b4e2d38b6a33be4f7e5a8821))
|
|
19
|
+
|
|
20
|
+
### Features
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
* **batch-queue:** init ([6ecbffa](https://github.com/atls/nestjs/commit/6ecbffa3fc54f9bb33ac1ae57b274772b99c8e9d))
|
|
24
|
+
* **common:** bump yarn, trigger release ([#338](https://github.com/atls/nestjs/issues/338)) ([9837d48](https://github.com/atls/nestjs/commit/9837d482f75928a3ac132d0306ab6de04d8a04b9))
|
|
25
|
+
|
|
26
|
+
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { Request } from 'express';
|
|
2
2
|
import type { Response } from 'express';
|
|
3
|
-
import { GrpcHttpProxyModuleOptions } from '../module/index.js';
|
|
3
|
+
import type { GrpcHttpProxyModuleOptions } from '../module/index.js';
|
|
4
4
|
export declare class AuthenticationService {
|
|
5
5
|
private readonly options;
|
|
6
6
|
constructor(options: GrpcHttpProxyModuleOptions);
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { promises as fs } from 'fs';
|
|
2
|
-
import { sign } from 'jsonwebtoken';
|
|
1
|
+
import { promises as fs } from 'node:fs';
|
|
3
2
|
import { v4 as uuid } from 'uuid';
|
|
4
3
|
import cookie from 'cookie';
|
|
4
|
+
import jwt from 'jsonwebtoken';
|
|
5
5
|
export class PrivateKeyAuthenticator {
|
|
6
6
|
privateKey;
|
|
7
7
|
constructor(privateKey) {
|
|
@@ -22,7 +22,7 @@ export class PrivateKeyAuthenticator {
|
|
|
22
22
|
}));
|
|
23
23
|
}
|
|
24
24
|
const privateKey = await fs.readFile(this.privateKey, 'utf-8');
|
|
25
|
-
const token = sign({ sub: subject }, privateKey, { algorithm: 'RS256' });
|
|
25
|
+
const token = jwt.sign({ sub: subject }, privateKey, { algorithm: 'RS256' });
|
|
26
26
|
return `Bearer ${token}`;
|
|
27
27
|
}
|
|
28
28
|
return null;
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { Request } from 'express';
|
|
2
|
-
import { Response } from 'express';
|
|
1
|
+
import type { Request } from 'express';
|
|
2
|
+
import type { Response } from 'express';
|
|
3
3
|
import { AuthenticationService } from '../authenticators/index.js';
|
|
4
4
|
import { ProtoRegistry } from '../proto/index.js';
|
|
5
5
|
export declare class GrpcHttpProxyController {
|
|
6
6
|
private readonly protoRegistry;
|
|
7
7
|
private readonly authenticator;
|
|
8
8
|
constructor(protoRegistry: ProtoRegistry, authenticator: AuthenticationService);
|
|
9
|
-
call(service:
|
|
9
|
+
call(service: string, method: string, body: unknown, req: Request, res: Response): Promise<void>;
|
|
10
10
|
}
|
|
@@ -50,7 +50,7 @@ __decorate([
|
|
|
50
50
|
__param(3, Req()),
|
|
51
51
|
__param(4, Res()),
|
|
52
52
|
__metadata("design:type", Function),
|
|
53
|
-
__metadata("design:paramtypes", [
|
|
53
|
+
__metadata("design:paramtypes", [String, String, Object, Object, Object]),
|
|
54
54
|
__metadata("design:returntype", Promise)
|
|
55
55
|
], GrpcHttpProxyController.prototype, "call", null);
|
|
56
56
|
GrpcHttpProxyController = __decorate([
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
+
import type { InjectionToken } from '@nestjs/common/interfaces';
|
|
1
2
|
import type { ModuleMetadata } from '@nestjs/common/interfaces';
|
|
3
|
+
import type { OptionalFactoryDependency } from '@nestjs/common/interfaces';
|
|
2
4
|
import type { Type } from '@nestjs/common/interfaces';
|
|
3
5
|
import type { GrpcOptions } from '@nestjs/microservices';
|
|
4
6
|
import type { Authenticator } from '../authenticators/index.js';
|
|
@@ -12,6 +14,6 @@ export interface GrpcHttpProxyOptionsFactory {
|
|
|
12
14
|
export interface GrpcHttpProxyModuleAsyncOptions extends Pick<ModuleMetadata, 'imports'> {
|
|
13
15
|
useExisting?: Type<GrpcHttpProxyOptionsFactory>;
|
|
14
16
|
useClass?: Type<GrpcHttpProxyOptionsFactory>;
|
|
15
|
-
useFactory?: (...args: Array<
|
|
16
|
-
inject?: Array<
|
|
17
|
+
useFactory?: (...args: Array<unknown>) => GrpcHttpProxyModuleOptions | Promise<GrpcHttpProxyModuleOptions>;
|
|
18
|
+
inject?: Array<InjectionToken | OptionalFactoryDependency>;
|
|
17
19
|
}
|
|
@@ -38,6 +38,9 @@ let GrpcHttpProxyModule = GrpcHttpProxyModule_1 = class GrpcHttpProxyModule {
|
|
|
38
38
|
if (options.useExisting || options.useFactory) {
|
|
39
39
|
return [this.createAsyncOptionsProvider(options)];
|
|
40
40
|
}
|
|
41
|
+
if (!options.useClass) {
|
|
42
|
+
throw new Error('GrpcHttpProxyModule requires useClass when useExisting/useFactory not provided');
|
|
43
|
+
}
|
|
41
44
|
return [
|
|
42
45
|
this.createAsyncOptionsProvider(options),
|
|
43
46
|
{
|
|
@@ -54,10 +57,14 @@ let GrpcHttpProxyModule = GrpcHttpProxyModule_1 = class GrpcHttpProxyModule {
|
|
|
54
57
|
inject: options.inject || [],
|
|
55
58
|
};
|
|
56
59
|
}
|
|
60
|
+
const injectTarget = options.useExisting ?? options.useClass;
|
|
61
|
+
if (!injectTarget) {
|
|
62
|
+
throw new Error('GrpcHttpProxyModule requires useExisting, useClass, or useFactory');
|
|
63
|
+
}
|
|
57
64
|
return {
|
|
58
65
|
provide: GRPC_HTTP_PROXY_MODULE_OPTIONS,
|
|
59
66
|
useFactory: async (optionsFactory) => optionsFactory.createGrpcHttpProxyOptions(),
|
|
60
|
-
inject: [
|
|
67
|
+
inject: [injectTarget],
|
|
61
68
|
};
|
|
62
69
|
}
|
|
63
70
|
};
|
|
@@ -1,7 +1,10 @@
|
|
|
1
|
+
import type { ChannelCredentials } from '@grpc/grpc-js';
|
|
1
2
|
import type { Client } from '@grpc/grpc-js';
|
|
3
|
+
type ServiceClientConstructor = new (address: string, creds: ChannelCredentials, options: Record<string, unknown>) => Client;
|
|
2
4
|
export declare class ProtoClient {
|
|
3
5
|
private readonly client;
|
|
4
6
|
constructor(client: Client);
|
|
5
|
-
static create(
|
|
6
|
-
call(method: string, request:
|
|
7
|
+
static create(ServiceClient: ServiceClientConstructor, url?: string): ProtoClient;
|
|
8
|
+
call(method: string, request: unknown, meta?: Record<string, Buffer | string | null>): Promise<unknown>;
|
|
7
9
|
}
|
|
10
|
+
export {};
|
|
@@ -1,22 +1,33 @@
|
|
|
1
1
|
import { Metadata } from '@grpc/grpc-js';
|
|
2
2
|
import { credentials } from '@grpc/grpc-js';
|
|
3
|
+
const getClientMethod = (client, method) => {
|
|
4
|
+
const candidate = client[method];
|
|
5
|
+
if (typeof candidate !== 'function') {
|
|
6
|
+
throw new Error(`Unknown gRPC method: ${method}`);
|
|
7
|
+
}
|
|
8
|
+
return candidate;
|
|
9
|
+
};
|
|
3
10
|
export class ProtoClient {
|
|
4
11
|
client;
|
|
5
12
|
constructor(client) {
|
|
6
13
|
this.client = client;
|
|
7
14
|
}
|
|
8
|
-
static create(url = '0.0.0.0:50051'
|
|
15
|
+
static create(ServiceClient, url = '0.0.0.0:50051') {
|
|
9
16
|
return new ProtoClient(new ServiceClient(url, credentials.createInsecure(), {}));
|
|
10
17
|
}
|
|
11
18
|
async call(method, request, meta = {}) {
|
|
12
19
|
const metadata = new Metadata();
|
|
13
20
|
Object.keys(meta).forEach((key) => {
|
|
14
|
-
|
|
15
|
-
|
|
21
|
+
const value = meta[key];
|
|
22
|
+
if (typeof value === 'string' || value instanceof Buffer) {
|
|
23
|
+
metadata.add(key, value);
|
|
24
|
+
}
|
|
16
25
|
});
|
|
17
26
|
return new Promise((resolve, reject) => {
|
|
18
|
-
|
|
19
|
-
|
|
27
|
+
const methodImpl = getClientMethod(this.client, method);
|
|
28
|
+
const hasRequestStream = 'requestStream' in methodImpl && Boolean(methodImpl.requestStream);
|
|
29
|
+
if (hasRequestStream) {
|
|
30
|
+
const call = methodImpl.call(this.client, metadata);
|
|
20
31
|
call.write(request);
|
|
21
32
|
let response;
|
|
22
33
|
call.on('data', (data) => {
|
|
@@ -28,7 +39,8 @@ export class ProtoClient {
|
|
|
28
39
|
});
|
|
29
40
|
}
|
|
30
41
|
else {
|
|
31
|
-
|
|
42
|
+
;
|
|
43
|
+
methodImpl.call(this.client, request, metadata, (error, response) => {
|
|
32
44
|
if (error) {
|
|
33
45
|
reject(error);
|
|
34
46
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { OnApplicationBootstrap } from '@nestjs/common';
|
|
2
|
-
import { GrpcHttpProxyModuleOptions } from '../module/index.js';
|
|
2
|
+
import type { GrpcHttpProxyModuleOptions } from '../module/index.js';
|
|
3
3
|
import { ProtoClient } from './proto.client.js';
|
|
4
4
|
export declare class ProtoRegistry implements OnApplicationBootstrap {
|
|
5
5
|
private readonly options;
|
|
@@ -24,9 +24,11 @@ let ProtoRegistry = class ProtoRegistry {
|
|
|
24
24
|
this.options = options;
|
|
25
25
|
}
|
|
26
26
|
async onApplicationBootstrap() {
|
|
27
|
-
const
|
|
28
|
-
|
|
29
|
-
|
|
27
|
+
const protoPathOption = this.options.options.protoPath;
|
|
28
|
+
if (!protoPathOption) {
|
|
29
|
+
throw new Error('GrpcHttpProxyModule requires protoPath');
|
|
30
|
+
}
|
|
31
|
+
const protoPaths = Array.isArray(protoPathOption) ? protoPathOption : [protoPathOption];
|
|
30
32
|
this.definitions = await Promise.all(protoPaths.map(async (protoPath) => {
|
|
31
33
|
const packageDefinition = await load(protoPath, this.options.options.loader);
|
|
32
34
|
return loadPackageDefinition(packageDefinition);
|
|
@@ -42,7 +44,7 @@ let ProtoRegistry = class ProtoRegistry {
|
|
|
42
44
|
if (!client) {
|
|
43
45
|
throw new Error('GRPC service not found');
|
|
44
46
|
}
|
|
45
|
-
return ProtoClient.create(this.options.options.url
|
|
47
|
+
return ProtoClient.create(client, this.options.options.url);
|
|
46
48
|
}
|
|
47
49
|
};
|
|
48
50
|
ProtoRegistry = __decorate([
|