generated from codibre/boilerplate-base
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adding gRPC middleware support (#8)
- Loading branch information
1 parent
02af339
commit a36a92d
Showing
10 changed files
with
314 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export { Client } from './client'; | ||
export { ClientPool } from './client-pool'; | ||
export { ClientConfig } from './client-config'; | ||
export * from './client'; | ||
export * from './client-config'; | ||
export * from './client-pool'; | ||
export * from './types'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import { | ||
CallOptions, | ||
Metadata, | ||
ClientDuplexStream, | ||
ClientReadableStream, | ||
ClientWritableStream, | ||
Client, | ||
} from '@grpc/grpc-js'; | ||
|
||
export interface RawUnaryCall<P, R> { | ||
(param: P, callback: (err: Error, response: R) => void): void; | ||
( | ||
param: P, | ||
deadline: Partial<CallOptions> | undefined, | ||
callback: (err: Error, response: R) => void, | ||
): void; | ||
( | ||
param: P, | ||
metadata: Metadata, | ||
callback: (err: Error, response: R) => void, | ||
): void; | ||
( | ||
param: P, | ||
metadata: Metadata, | ||
deadline: Partial<CallOptions> | undefined, | ||
callback: (err: Error, response: R) => void, | ||
): void; | ||
} | ||
|
||
export interface StreamCall<P, R> { | ||
(param: P, deadline?: Partial<CallOptions> | undefined): ClientDuplexStream< | ||
P, | ||
R | ||
>; | ||
( | ||
param: P, | ||
metadata: Metadata, | ||
deadline?: Partial<CallOptions> | undefined, | ||
): ClientDuplexStream<P, R>; | ||
requestStream: ClientWritableStream<P>; | ||
responseStream: ClientReadableStream<R>; | ||
} | ||
|
||
export interface UnaryCall<P, R> { | ||
(param: P, deadline?: Partial<CallOptions> | undefined): PromiseLike<R>; | ||
( | ||
param: P, | ||
metadata: Metadata, | ||
deadline?: Partial<CallOptions> | undefined, | ||
): PromiseLike<R>; | ||
} | ||
|
||
export type GrpcFunction<P, R> = StreamCall<P, R> | UnaryCall<P, R>; | ||
|
||
export function isStreamCall<P, R>( | ||
action: GrpcFunction<P, R>, | ||
): action is StreamCall<P, R> { | ||
return (action as StreamCall<P, R>).requestStream || | ||
(action as StreamCall<P, R>).responseStream | ||
? true | ||
: false; | ||
} | ||
|
||
type KeyType = string | number | symbol; | ||
|
||
export type GrpcServiceDefinition< | ||
Methods extends KeyType = keyof GrpcServiceDefinition<KeyType>, | ||
> = Record<Methods, GrpcFunction<any, any>>; | ||
export type GrpcServiceClient<Methods extends KeyType = KeyType> = Client & | ||
GrpcServiceDefinition<Methods>; | ||
|
||
export interface FullGrpcParams< | ||
TService extends GrpcServiceDefinition<KeyType>, | ||
k extends keyof TService, | ||
> { | ||
( | ||
param: Parameters<TService[k]>[0], | ||
metadata: Metadata, | ||
options: Partial<CallOptions>, | ||
): void; | ||
} | ||
|
||
export interface OtherGrpcParams { | ||
(metadata: Metadata, options: Partial<CallOptions>): void; | ||
} | ||
|
||
export type DefaultGrpcMiddleware = ( | ||
params: Parameters<OtherGrpcParams>, | ||
) => void; | ||
export type GrpcMethodMiddleware< | ||
TService extends GrpcServiceDefinition<KeyType>, | ||
k extends keyof TService, | ||
> = (params: Parameters<FullGrpcParams<TService, k>>) => void; | ||
|
||
export type GrpcMiddlewares<TService extends GrpcServiceDefinition<KeyType>> = { | ||
[k in keyof TService]: GrpcMethodMiddleware<TService, k>[]; | ||
} & { | ||
'*'?: DefaultGrpcMiddleware[]; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { | ||
GrpcServiceDefinition, | ||
GrpcMethodMiddleware, | ||
DefaultGrpcMiddleware, | ||
} from './../types'; | ||
import { GrpcMiddlewares } from '../types'; | ||
import { CallOptions, Metadata } from '@grpc/grpc-js'; | ||
|
||
function callMiddlewaresFactory<TService extends GrpcServiceDefinition>( | ||
defaultMiddlewares: DefaultGrpcMiddleware[], | ||
methodMiddlewares: GrpcMethodMiddleware<TService, keyof TService>[] = [], | ||
) { | ||
if (!methodMiddlewares.length && !defaultMiddlewares.length) { | ||
return undefined; | ||
} | ||
|
||
return ( | ||
params: [ | ||
Parameters<TService[keyof TService]>[0], | ||
Metadata, | ||
Partial<CallOptions>, | ||
], | ||
) => { | ||
const [, ...others] = params; | ||
for (const middleware of defaultMiddlewares) { | ||
middleware(others); | ||
} | ||
params[1] = others[0]; | ||
params[2] = others[1]; | ||
for (const middleware of methodMiddlewares) { | ||
middleware(params); | ||
} | ||
}; | ||
} | ||
|
||
function getNewParameters<TService extends GrpcServiceDefinition>( | ||
args: [ | ||
Parameters<TService[keyof TService]>[0], | ||
Metadata | Partial<CallOptions> | undefined, | ||
Partial<CallOptions> | undefined, | ||
], | ||
): [Parameters<TService[keyof TService]>[0], Metadata, Partial<CallOptions>] { | ||
const [params] = args; | ||
let [, metadata, options] = args; | ||
if (!(metadata instanceof Metadata)) { | ||
options = metadata ? metadata : {}; | ||
metadata = new Metadata(); | ||
} else if (!options) { | ||
options = {}; | ||
} | ||
return [params, metadata, options]; | ||
} | ||
|
||
export function applyMiddlewares<TService extends GrpcServiceDefinition>( | ||
client: TService, | ||
serviceMiddlewares?: GrpcMiddlewares<TService>, | ||
) { | ||
if (serviceMiddlewares) { | ||
const defaultMiddlewares = serviceMiddlewares['*'] ?? []; | ||
for (const k in client) { | ||
if (client.hasOwnProperty(k)) { | ||
const middlewares = serviceMiddlewares[k]; | ||
const callMiddlewares = callMiddlewaresFactory( | ||
defaultMiddlewares, | ||
middlewares, | ||
); | ||
if (callMiddlewares) { | ||
const rawCall = client[k]!; | ||
client[k] = (( | ||
...args: [ | ||
Parameters<TService[keyof TService]>[0], | ||
Partial<CallOptions> | Metadata | undefined, | ||
Partial<CallOptions> | undefined, | ||
] | ||
) => { | ||
const newArgs = getNewParameters<TService>(args); | ||
callMiddlewares.call(serviceMiddlewares, newArgs); | ||
return (rawCall as Function).apply(client, newArgs); | ||
}) as TService[Extract<keyof TService, string>]; | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.