Skip to content

Commit

Permalink
feat(ges): prevent interceptor from postprocess response on unary call
Browse files Browse the repository at this point in the history
simplify ctx.response
  • Loading branch information
edvardchen committed Aug 13, 2019
1 parent 43edc74 commit 4d15bd0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ describe('grpc-experimental-server', () => {
});
runTest({
implementations: {
async getFeature(call, callback) {
getFeature(call: ServerUnaryCall<unknown>, callback: sendUnaryData<unknown>) {
const error: ServiceError = new Error('unexpected');
error.code = status.PERMISSION_DENIED;
callback(error, new Feature());
Expand All @@ -83,10 +83,14 @@ describe('grpc-experimental-server', () => {
let finished = false;
server().use(async (ctx, next) => {
ctx.onFinished(error => {
expect(error).toBeInstanceOf(Error);
try {
expect(error).toBeInstanceOf(Error);
} catch (e) {
done(e);
}
finished = true;
});
await next();
next().catch(() => undefined);
});
// @ts-ignore
client().getFeature(new Point(), () => {
Expand All @@ -101,10 +105,11 @@ describe('grpc-experimental-server', () => {
describe('postprocess', () => {
runTest({
testcase(server, client) {
it('should get Feature', () => {
it('should get Feature', done => {
server().use(async (ctx, next) => {
await next();
expect(ctx.response.value).toBeInstanceOf(Feature);
expect(ctx.response).toBeInstanceOf(Feature);
done();
});

// @ts-ignore
Expand Down
39 changes: 14 additions & 25 deletions packages/grpc-experimental-server/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import compose from 'koa-compose';
import {
Metadata,
Server,
UntypedServiceImplementation,
ServiceDefinition,
Expand All @@ -25,11 +24,7 @@ type ServerCall =
type ServerNonStreamCall = ServerUnaryCall<unknown> | ServerReadableStream<unknown>;

export class Context {
response: {
value?: unknown;
trailer?: Metadata;
flags?: number;
} = {};
response: unknown;
constructor(public call: ServerCall, public definition: MethodDefinition<unknown, unknown>) {}
onFinished(listener: (err: Error | null) => void): void {
const emitter = this.call as EventEmitter;
Expand Down Expand Up @@ -85,15 +80,24 @@ export default class ExperimentalServer extends Server {

const ctx = new Context(call, definition);

const pending = this.handleRequest(ctx, async () => {
this.handleRequest(ctx, async () => {
// unary call
if (grpcCallback) {
const nonStreamCall = call as ServerNonStreamCall;
return new Promise((resolve, reject) => {
const callback: sendUnaryData<unknown> = (error, value, trailer, flags) => {
if (error) return reject(error);
ctx.response = { ...ctx.response, value, trailer, flags };
const callback: sendUnaryData<unknown> = (error, value, ...rest) => {
if (error) {
grpcCallback(error, value);
reject(error);
(call as EventEmitter).emit('finish', error);
return;
}

// REAL SEND RESPONSE
grpcCallback(null, value, ...rest);
ctx.response = value;
resolve();
(call as EventEmitter).emit('finish');
};
// @ts-ignore
original(nonStreamCall, callback);
Expand All @@ -104,21 +108,6 @@ export default class ExperimentalServer extends Server {
original(call);
return;
});
if (grpcCallback) {
pending
.then(() => {
const {
response: { value, trailer, flags },
} = ctx;
// real send response
grpcCallback(null, value, trailer, flags);
(call as EventEmitter).emit('finish');
})
.catch(error => {
grpcCallback(error, null);
(call as EventEmitter).emit('finish', error);
});
}
};
}

Expand Down

0 comments on commit 4d15bd0

Please sign in to comment.